Sync to trunk revision 61757.
[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 #include "msctf_internal.h"
22
23 typedef struct tagDocumentMgr {
24 ITfDocumentMgr ITfDocumentMgr_iface;
25 ITfSource ITfSource_iface;
26 LONG refCount;
27
28 /* Aggregation */
29 ITfCompartmentMgr *CompartmentMgr;
30
31 ITfContext* contextStack[2]; /* limit of 2 contexts */
32 ITfThreadMgrEventSink* ThreadMgrSink;
33 } DocumentMgr;
34
35 typedef struct tagEnumTfContext {
36 IEnumTfContexts IEnumTfContexts_iface;
37 LONG refCount;
38
39 DWORD index;
40 DocumentMgr *docmgr;
41 } EnumTfContext;
42
43 static HRESULT EnumTfContext_Constructor(DocumentMgr* mgr, IEnumTfContexts **ppOut);
44
45 static inline DocumentMgr *impl_from_ITfDocumentMgr(ITfDocumentMgr *iface)
46 {
47 return CONTAINING_RECORD(iface, DocumentMgr, ITfDocumentMgr_iface);
48 }
49
50 static inline DocumentMgr *impl_from_ITfSource(ITfSource *iface)
51 {
52 return CONTAINING_RECORD(iface, DocumentMgr, ITfSource_iface);
53 }
54
55 static inline EnumTfContext *impl_from_IEnumTfContexts(IEnumTfContexts *iface)
56 {
57 return CONTAINING_RECORD(iface, EnumTfContext, IEnumTfContexts_iface);
58 }
59
60 static void DocumentMgr_Destructor(DocumentMgr *This)
61 {
62 ITfThreadMgr *tm;
63 TRACE("destroying %p\n", This);
64
65 TF_GetThreadMgr(&tm);
66 ThreadMgr_OnDocumentMgrDestruction(tm, &This->ITfDocumentMgr_iface);
67
68 if (This->contextStack[0])
69 ITfContext_Release(This->contextStack[0]);
70 if (This->contextStack[1])
71 ITfContext_Release(This->contextStack[1]);
72 CompartmentMgr_Destructor(This->CompartmentMgr);
73 HeapFree(GetProcessHeap(),0,This);
74 }
75
76 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
77 {
78 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
79 *ppvOut = NULL;
80
81 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
82 {
83 *ppvOut = This;
84 }
85 else if (IsEqualIID(iid, &IID_ITfSource))
86 {
87 *ppvOut = &This->ITfSource_iface;
88 }
89 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
90 {
91 *ppvOut = This->CompartmentMgr;
92 }
93
94 if (*ppvOut)
95 {
96 ITfDocumentMgr_AddRef(iface);
97 return S_OK;
98 }
99
100 WARN("unsupported interface: %s\n", debugstr_guid(iid));
101 return E_NOINTERFACE;
102 }
103
104 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
105 {
106 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
107 return InterlockedIncrement(&This->refCount);
108 }
109
110 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
111 {
112 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
113 ULONG ret;
114
115 ret = InterlockedDecrement(&This->refCount);
116 if (ret == 0)
117 DocumentMgr_Destructor(This);
118 return ret;
119 }
120
121 /*****************************************************
122 * ITfDocumentMgr functions
123 *****************************************************/
124 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
125 TfClientId tidOwner,
126 DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
127 TfEditCookie *pecTextStore)
128 {
129 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
130 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
131 return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
132 }
133
134 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
135 {
136 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
137 ITfContext *check;
138
139 TRACE("(%p) %p\n",This,pic);
140
141 if (This->contextStack[1]) /* FUll */
142 return TF_E_STACKFULL;
143
144 if (!pic || FAILED(ITfContext_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
145 return E_INVALIDARG;
146
147 if (This->contextStack[0] == NULL)
148 ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
149
150 This->contextStack[1] = This->contextStack[0];
151 This->contextStack[0] = check;
152
153 Context_Initialize(check, iface);
154 ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
155
156 return S_OK;
157 }
158
159 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
160 {
161 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
162 TRACE("(%p) 0x%x\n",This,dwFlags);
163
164 if (dwFlags == TF_POPF_ALL)
165 {
166 if (This->contextStack[0])
167 {
168 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
169 Context_Uninitialize(This->contextStack[0]);
170 ITfContext_Release(This->contextStack[0]);
171 }
172 if (This->contextStack[1])
173 {
174 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
175 Context_Uninitialize(This->contextStack[1]);
176 ITfContext_Release(This->contextStack[1]);
177 }
178 This->contextStack[0] = This->contextStack[1] = NULL;
179 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
180 return S_OK;
181 }
182
183 if (dwFlags)
184 return E_INVALIDARG;
185
186 if (This->contextStack[1] == NULL) /* Cannot pop last context */
187 return E_FAIL;
188
189 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
190 Context_Uninitialize(This->contextStack[0]);
191 ITfContext_Release(This->contextStack[0]);
192 This->contextStack[0] = This->contextStack[1];
193 This->contextStack[1] = NULL;
194
195 if (This->contextStack[0] == NULL)
196 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
197
198 return S_OK;
199 }
200
201 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
202 {
203 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
204 TRACE("(%p)\n",This);
205 if (!ppic)
206 return E_INVALIDARG;
207
208 if (This->contextStack[0])
209 ITfContext_AddRef(This->contextStack[0]);
210
211 *ppic = This->contextStack[0];
212
213 return S_OK;
214 }
215
216 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
217 {
218 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
219 ITfContext *tgt;
220
221 TRACE("(%p)\n",This);
222 if (!ppic)
223 return E_INVALIDARG;
224
225 if (This->contextStack[1])
226 tgt = This->contextStack[1];
227 else
228 tgt = This->contextStack[0];
229
230 if (tgt)
231 ITfContext_AddRef(tgt);
232
233 *ppic = tgt;
234
235 return S_OK;
236 }
237
238 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
239 {
240 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
241 TRACE("(%p) %p\n",This,ppEnum);
242 return EnumTfContext_Constructor(This, ppEnum);
243 }
244
245 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
246 {
247 DocumentMgr_QueryInterface,
248 DocumentMgr_AddRef,
249 DocumentMgr_Release,
250
251 DocumentMgr_CreateContext,
252 DocumentMgr_Push,
253 DocumentMgr_Pop,
254 DocumentMgr_GetTop,
255 DocumentMgr_GetBase,
256 DocumentMgr_EnumContexts
257 };
258
259
260 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
261 {
262 DocumentMgr *This = impl_from_ITfSource(iface);
263 return DocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, *ppvOut);
264 }
265
266 static ULONG WINAPI Source_AddRef(ITfSource *iface)
267 {
268 DocumentMgr *This = impl_from_ITfSource(iface);
269 return DocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
270 }
271
272 static ULONG WINAPI Source_Release(ITfSource *iface)
273 {
274 DocumentMgr *This = impl_from_ITfSource(iface);
275 return DocumentMgr_Release(&This->ITfDocumentMgr_iface);
276 }
277
278 /*****************************************************
279 * ITfSource functions
280 *****************************************************/
281 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
282 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
283 {
284 DocumentMgr *This = impl_from_ITfSource(iface);
285 FIXME("STUB:(%p)\n",This);
286 return E_NOTIMPL;
287 }
288
289 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
290 {
291 DocumentMgr *This = impl_from_ITfSource(iface);
292 FIXME("STUB:(%p)\n",This);
293 return E_NOTIMPL;
294 }
295
296 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
297 {
298 Source_QueryInterface,
299 Source_AddRef,
300 Source_Release,
301
302 DocumentMgrSource_AdviseSink,
303 DocumentMgrSource_UnadviseSink,
304 };
305
306 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
307 {
308 DocumentMgr *This;
309
310 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
311 if (This == NULL)
312 return E_OUTOFMEMORY;
313
314 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgr_DocumentMgrVtbl;
315 This->ITfSource_iface.lpVtbl = &DocumentMgr_SourceVtbl;
316 This->refCount = 1;
317 This->ThreadMgrSink = ThreadMgrSink;
318
319 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
320
321 TRACE("returning %p\n", This);
322 *ppOut = &This->ITfDocumentMgr_iface;
323 return S_OK;
324 }
325
326 /**************************************************
327 * IEnumTfContexts implementation
328 **************************************************/
329 static void EnumTfContext_Destructor(EnumTfContext *This)
330 {
331 TRACE("destroying %p\n", This);
332 HeapFree(GetProcessHeap(),0,This);
333 }
334
335 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
336 {
337 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
338 *ppvOut = NULL;
339
340 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
341 {
342 *ppvOut = This;
343 }
344
345 if (*ppvOut)
346 {
347 IEnumTfContexts_AddRef(iface);
348 return S_OK;
349 }
350
351 WARN("unsupported interface: %s\n", debugstr_guid(iid));
352 return E_NOINTERFACE;
353 }
354
355 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
356 {
357 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
358 return InterlockedIncrement(&This->refCount);
359 }
360
361 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
362 {
363 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
364 ULONG ret;
365
366 ret = InterlockedDecrement(&This->refCount);
367 if (ret == 0)
368 EnumTfContext_Destructor(This);
369 return ret;
370 }
371
372 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
373 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
374 {
375 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
376 ULONG fetched = 0;
377
378 TRACE("(%p)\n",This);
379
380 if (rgContext == NULL) return E_POINTER;
381
382 while (fetched < ulCount)
383 {
384 if (This->index > 1)
385 break;
386
387 if (!This->docmgr->contextStack[This->index])
388 break;
389
390 *rgContext = This->docmgr->contextStack[This->index];
391 ITfContext_AddRef(*rgContext);
392
393 ++This->index;
394 ++fetched;
395 ++rgContext;
396 }
397
398 if (pcFetched) *pcFetched = fetched;
399 return fetched == ulCount ? S_OK : S_FALSE;
400 }
401
402 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
403 {
404 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
405 TRACE("(%p)\n",This);
406 This->index += celt;
407 return S_OK;
408 }
409
410 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
411 {
412 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
413 TRACE("(%p)\n",This);
414 This->index = 0;
415 return S_OK;
416 }
417
418 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
419 IEnumTfContexts **ppenum)
420 {
421 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
422 HRESULT res;
423
424 TRACE("(%p)\n",This);
425
426 if (ppenum == NULL) return E_POINTER;
427
428 res = EnumTfContext_Constructor(This->docmgr, ppenum);
429 if (SUCCEEDED(res))
430 {
431 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
432 new_This->index = This->index;
433 }
434 return res;
435 }
436
437 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
438 EnumTfContext_QueryInterface,
439 EnumTfContext_AddRef,
440 EnumTfContext_Release,
441
442 EnumTfContext_Clone,
443 EnumTfContext_Next,
444 EnumTfContext_Reset,
445 EnumTfContext_Skip
446 };
447
448 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
449 {
450 EnumTfContext *This;
451
452 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
453 if (This == NULL)
454 return E_OUTOFMEMORY;
455
456 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
457 This->refCount = 1;
458 This->docmgr = mgr;
459
460 TRACE("returning %p\n", This);
461 *ppOut = &This->IEnumTfContexts_iface;
462 return S_OK;
463 }