[LT2013]
[reactos.git] / dll / win32 / msctf / documentmgr.c
1 /*
2 * ITfDocumentMgr implementation
3 *
4 * Copyright 2009 Aric Stewart, CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <config.h>
26
27 //#include <stdarg.h>
28
29 #define COBJMACROS
30
31 #include <wine/debug.h>
32 //#include "windef.h"
33 #include <winbase.h>
34 //#include "winreg.h"
35 //#include "winuser.h"
36 //#include "shlwapi.h"
37 //#include "winerror.h"
38 #include <objbase.h>
39
40 //#include "wine/unicode.h"
41
42 #include <msctf.h>
43 #include "msctf_internal.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
46
47 typedef struct tagDocumentMgr {
48 ITfDocumentMgr ITfDocumentMgr_iface;
49 ITfSource ITfSource_iface;
50 LONG refCount;
51
52 /* Aggregation */
53 ITfCompartmentMgr *CompartmentMgr;
54
55 ITfContext* contextStack[2]; /* limit of 2 contexts */
56 ITfThreadMgrEventSink* ThreadMgrSink;
57 } DocumentMgr;
58
59 typedef struct tagEnumTfContext {
60 IEnumTfContexts IEnumTfContexts_iface;
61 LONG refCount;
62
63 DWORD index;
64 DocumentMgr *docmgr;
65 } EnumTfContext;
66
67 static HRESULT EnumTfContext_Constructor(DocumentMgr* mgr, IEnumTfContexts **ppOut);
68
69 static inline DocumentMgr *impl_from_ITfDocumentMgr(ITfDocumentMgr *iface)
70 {
71 return CONTAINING_RECORD(iface, DocumentMgr, ITfDocumentMgr_iface);
72 }
73
74 static inline DocumentMgr *impl_from_ITfSource(ITfSource *iface)
75 {
76 return CONTAINING_RECORD(iface, DocumentMgr, ITfSource_iface);
77 }
78
79 static inline EnumTfContext *impl_from_IEnumTfContexts(IEnumTfContexts *iface)\
80 {
81 return CONTAINING_RECORD(iface, EnumTfContext, IEnumTfContexts_iface);
82 }
83
84 static void DocumentMgr_Destructor(DocumentMgr *This)
85 {
86 ITfThreadMgr *tm;
87 TRACE("destroying %p\n", This);
88
89 TF_GetThreadMgr(&tm);
90 ThreadMgr_OnDocumentMgrDestruction(tm, &This->ITfDocumentMgr_iface);
91
92 if (This->contextStack[0])
93 ITfContext_Release(This->contextStack[0]);
94 if (This->contextStack[1])
95 ITfContext_Release(This->contextStack[1]);
96 CompartmentMgr_Destructor(This->CompartmentMgr);
97 HeapFree(GetProcessHeap(),0,This);
98 }
99
100 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
101 {
102 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
103 *ppvOut = NULL;
104
105 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
106 {
107 *ppvOut = This;
108 }
109 else if (IsEqualIID(iid, &IID_ITfSource))
110 {
111 *ppvOut = &This->ITfSource_iface;
112 }
113 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
114 {
115 *ppvOut = This->CompartmentMgr;
116 }
117
118 if (*ppvOut)
119 {
120 IUnknown_AddRef(iface);
121 return S_OK;
122 }
123
124 WARN("unsupported interface: %s\n", debugstr_guid(iid));
125 return E_NOINTERFACE;
126 }
127
128 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
129 {
130 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
131 return InterlockedIncrement(&This->refCount);
132 }
133
134 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
135 {
136 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
137 ULONG ret;
138
139 ret = InterlockedDecrement(&This->refCount);
140 if (ret == 0)
141 DocumentMgr_Destructor(This);
142 return ret;
143 }
144
145 /*****************************************************
146 * ITfDocumentMgr functions
147 *****************************************************/
148 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
149 TfClientId tidOwner,
150 DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
151 TfEditCookie *pecTextStore)
152 {
153 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
154 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
155 return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
156 }
157
158 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
159 {
160 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
161 ITfContext *check;
162
163 TRACE("(%p) %p\n",This,pic);
164
165 if (This->contextStack[1]) /* FUll */
166 return TF_E_STACKFULL;
167
168 if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
169 return E_INVALIDARG;
170
171 if (This->contextStack[0] == NULL)
172 ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
173
174 This->contextStack[1] = This->contextStack[0];
175 This->contextStack[0] = check;
176
177 Context_Initialize(check, iface);
178 ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
179
180 return S_OK;
181 }
182
183 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
184 {
185 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
186 TRACE("(%p) 0x%x\n",This,dwFlags);
187
188 if (dwFlags == TF_POPF_ALL)
189 {
190 if (This->contextStack[0])
191 {
192 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
193 ITfContext_Release(This->contextStack[0]);
194 Context_Uninitialize(This->contextStack[0]);
195 }
196 if (This->contextStack[1])
197 {
198 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
199 ITfContext_Release(This->contextStack[1]);
200 Context_Uninitialize(This->contextStack[1]);
201 }
202 This->contextStack[0] = This->contextStack[1] = NULL;
203 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
204 return S_OK;
205 }
206
207 if (dwFlags)
208 return E_INVALIDARG;
209
210 if (This->contextStack[1] == NULL) /* Cannot pop last context */
211 return E_FAIL;
212
213 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
214 ITfContext_Release(This->contextStack[0]);
215 Context_Uninitialize(This->contextStack[0]);
216 This->contextStack[0] = This->contextStack[1];
217 This->contextStack[1] = NULL;
218
219 if (This->contextStack[0] == NULL)
220 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
221
222 return S_OK;
223 }
224
225 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
226 {
227 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
228 TRACE("(%p)\n",This);
229 if (!ppic)
230 return E_INVALIDARG;
231
232 if (This->contextStack[0])
233 ITfContext_AddRef(This->contextStack[0]);
234
235 *ppic = This->contextStack[0];
236
237 return S_OK;
238 }
239
240 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
241 {
242 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
243 ITfContext *tgt;
244
245 TRACE("(%p)\n",This);
246 if (!ppic)
247 return E_INVALIDARG;
248
249 if (This->contextStack[1])
250 tgt = This->contextStack[1];
251 else
252 tgt = This->contextStack[0];
253
254 if (tgt)
255 ITfContext_AddRef(tgt);
256
257 *ppic = tgt;
258
259 return S_OK;
260 }
261
262 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
263 {
264 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
265 TRACE("(%p) %p\n",This,ppEnum);
266 return EnumTfContext_Constructor(This, ppEnum);
267 }
268
269 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
270 {
271 DocumentMgr_QueryInterface,
272 DocumentMgr_AddRef,
273 DocumentMgr_Release,
274
275 DocumentMgr_CreateContext,
276 DocumentMgr_Push,
277 DocumentMgr_Pop,
278 DocumentMgr_GetTop,
279 DocumentMgr_GetBase,
280 DocumentMgr_EnumContexts
281 };
282
283
284 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
285 {
286 DocumentMgr *This = impl_from_ITfSource(iface);
287 return DocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, *ppvOut);
288 }
289
290 static ULONG WINAPI Source_AddRef(ITfSource *iface)
291 {
292 DocumentMgr *This = impl_from_ITfSource(iface);
293 return DocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
294 }
295
296 static ULONG WINAPI Source_Release(ITfSource *iface)
297 {
298 DocumentMgr *This = impl_from_ITfSource(iface);
299 return DocumentMgr_Release(&This->ITfDocumentMgr_iface);
300 }
301
302 /*****************************************************
303 * ITfSource functions
304 *****************************************************/
305 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
306 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
307 {
308 DocumentMgr *This = impl_from_ITfSource(iface);
309 FIXME("STUB:(%p)\n",This);
310 return E_NOTIMPL;
311 }
312
313 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
314 {
315 DocumentMgr *This = impl_from_ITfSource(iface);
316 FIXME("STUB:(%p)\n",This);
317 return E_NOTIMPL;
318 }
319
320 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
321 {
322 Source_QueryInterface,
323 Source_AddRef,
324 Source_Release,
325
326 DocumentMgrSource_AdviseSink,
327 DocumentMgrSource_UnadviseSink,
328 };
329
330 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
331 {
332 DocumentMgr *This;
333
334 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
335 if (This == NULL)
336 return E_OUTOFMEMORY;
337
338 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgr_DocumentMgrVtbl;
339 This->ITfSource_iface.lpVtbl = &DocumentMgr_SourceVtbl;
340 This->refCount = 1;
341 This->ThreadMgrSink = ThreadMgrSink;
342
343 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
344
345 TRACE("returning %p\n", This);
346 *ppOut = &This->ITfDocumentMgr_iface;
347 return S_OK;
348 }
349
350 /**************************************************
351 * IEnumTfContexts implementation
352 **************************************************/
353 static void EnumTfContext_Destructor(EnumTfContext *This)
354 {
355 TRACE("destroying %p\n", This);
356 HeapFree(GetProcessHeap(),0,This);
357 }
358
359 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
360 {
361 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
362 *ppvOut = NULL;
363
364 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
365 {
366 *ppvOut = This;
367 }
368
369 if (*ppvOut)
370 {
371 IUnknown_AddRef(iface);
372 return S_OK;
373 }
374
375 WARN("unsupported interface: %s\n", debugstr_guid(iid));
376 return E_NOINTERFACE;
377 }
378
379 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
380 {
381 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
382 return InterlockedIncrement(&This->refCount);
383 }
384
385 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
386 {
387 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
388 ULONG ret;
389
390 ret = InterlockedDecrement(&This->refCount);
391 if (ret == 0)
392 EnumTfContext_Destructor(This);
393 return ret;
394 }
395
396 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
397 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
398 {
399 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
400 ULONG fetched = 0;
401
402 TRACE("(%p)\n",This);
403
404 if (rgContext == NULL) return E_POINTER;
405
406 while (fetched < ulCount)
407 {
408 if (This->index > 1)
409 break;
410
411 if (!This->docmgr->contextStack[This->index])
412 break;
413
414 *rgContext = This->docmgr->contextStack[This->index];
415 ITfContext_AddRef(*rgContext);
416
417 ++This->index;
418 ++fetched;
419 ++rgContext;
420 }
421
422 if (pcFetched) *pcFetched = fetched;
423 return fetched == ulCount ? S_OK : S_FALSE;
424 }
425
426 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
427 {
428 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
429 TRACE("(%p)\n",This);
430 This->index += celt;
431 return S_OK;
432 }
433
434 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
435 {
436 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
437 TRACE("(%p)\n",This);
438 This->index = 0;
439 return S_OK;
440 }
441
442 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
443 IEnumTfContexts **ppenum)
444 {
445 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
446 HRESULT res;
447
448 TRACE("(%p)\n",This);
449
450 if (ppenum == NULL) return E_POINTER;
451
452 res = EnumTfContext_Constructor(This->docmgr, ppenum);
453 if (SUCCEEDED(res))
454 {
455 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
456 new_This->index = This->index;
457 }
458 return res;
459 }
460
461 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
462 EnumTfContext_QueryInterface,
463 EnumTfContext_AddRef,
464 EnumTfContext_Release,
465
466 EnumTfContext_Clone,
467 EnumTfContext_Next,
468 EnumTfContext_Reset,
469 EnumTfContext_Skip
470 };
471
472 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
473 {
474 EnumTfContext *This;
475
476 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
477 if (This == NULL)
478 return E_OUTOFMEMORY;
479
480 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
481 This->refCount = 1;
482 This->docmgr = mgr;
483
484 TRACE("returning %p\n", This);
485 *ppOut = &This->IEnumTfContexts_iface;
486 return S_OK;
487 }