Fix merge r65567.
[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->ITfDocumentMgr_iface;
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 int i;
167
168 for (i = 0; i < sizeof(This->contextStack)/sizeof(This->contextStack[0]); i++)
169 if (This->contextStack[i])
170 {
171 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink, This->contextStack[i]);
172 Context_Uninitialize(This->contextStack[i]);
173 ITfContext_Release(This->contextStack[i]);
174 This->contextStack[i] = NULL;
175 }
176
177 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
178 return S_OK;
179 }
180
181 if (dwFlags)
182 return E_INVALIDARG;
183
184 if (This->contextStack[1] == NULL) /* Cannot pop last context */
185 return E_FAIL;
186
187 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
188 Context_Uninitialize(This->contextStack[0]);
189 ITfContext_Release(This->contextStack[0]);
190 This->contextStack[0] = This->contextStack[1];
191 This->contextStack[1] = NULL;
192
193 if (This->contextStack[0] == NULL)
194 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
195
196 return S_OK;
197 }
198
199 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
200 {
201 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
202 TRACE("(%p)\n",This);
203 if (!ppic)
204 return E_INVALIDARG;
205
206 if (This->contextStack[0])
207 ITfContext_AddRef(This->contextStack[0]);
208
209 *ppic = This->contextStack[0];
210
211 return S_OK;
212 }
213
214 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
215 {
216 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
217 ITfContext *tgt;
218
219 TRACE("(%p)\n",This);
220 if (!ppic)
221 return E_INVALIDARG;
222
223 if (This->contextStack[1])
224 tgt = This->contextStack[1];
225 else
226 tgt = This->contextStack[0];
227
228 if (tgt)
229 ITfContext_AddRef(tgt);
230
231 *ppic = tgt;
232
233 return S_OK;
234 }
235
236 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
237 {
238 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
239 TRACE("(%p) %p\n",This,ppEnum);
240 return EnumTfContext_Constructor(This, ppEnum);
241 }
242
243 static const ITfDocumentMgrVtbl DocumentMgrVtbl =
244 {
245 DocumentMgr_QueryInterface,
246 DocumentMgr_AddRef,
247 DocumentMgr_Release,
248 DocumentMgr_CreateContext,
249 DocumentMgr_Push,
250 DocumentMgr_Pop,
251 DocumentMgr_GetTop,
252 DocumentMgr_GetBase,
253 DocumentMgr_EnumContexts
254 };
255
256 static HRESULT WINAPI DocumentMgrSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
257 {
258 DocumentMgr *This = impl_from_ITfSource(iface);
259 return ITfDocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, ppvOut);
260 }
261
262 static ULONG WINAPI DocumentMgrSource_AddRef(ITfSource *iface)
263 {
264 DocumentMgr *This = impl_from_ITfSource(iface);
265 return ITfDocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
266 }
267
268 static ULONG WINAPI DocumentMgrSource_Release(ITfSource *iface)
269 {
270 DocumentMgr *This = impl_from_ITfSource(iface);
271 return ITfDocumentMgr_Release(&This->ITfDocumentMgr_iface);
272 }
273
274 /*****************************************************
275 * ITfSource functions
276 *****************************************************/
277 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
278 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
279 {
280 DocumentMgr *This = impl_from_ITfSource(iface);
281 FIXME("STUB:(%p)\n",This);
282 return E_NOTIMPL;
283 }
284
285 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
286 {
287 DocumentMgr *This = impl_from_ITfSource(iface);
288 FIXME("STUB:(%p)\n",This);
289 return E_NOTIMPL;
290 }
291
292 static const ITfSourceVtbl DocumentMgrSourceVtbl =
293 {
294 DocumentMgrSource_QueryInterface,
295 DocumentMgrSource_AddRef,
296 DocumentMgrSource_Release,
297 DocumentMgrSource_AdviseSink,
298 DocumentMgrSource_UnadviseSink,
299 };
300
301 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
302 {
303 DocumentMgr *This;
304
305 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
306 if (This == NULL)
307 return E_OUTOFMEMORY;
308
309 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgrVtbl;
310 This->ITfSource_iface.lpVtbl = &DocumentMgrSourceVtbl;
311 This->refCount = 1;
312 This->ThreadMgrSink = ThreadMgrSink;
313
314 CompartmentMgr_Constructor((IUnknown*)&This->ITfDocumentMgr_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
315
316 *ppOut = &This->ITfDocumentMgr_iface;
317 TRACE("returning %p\n", *ppOut);
318 return S_OK;
319 }
320
321 /**************************************************
322 * IEnumTfContexts implementation
323 **************************************************/
324 static void EnumTfContext_Destructor(EnumTfContext *This)
325 {
326 TRACE("destroying %p\n", This);
327 HeapFree(GetProcessHeap(),0,This);
328 }
329
330 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
331 {
332 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
333 *ppvOut = NULL;
334
335 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
336 {
337 *ppvOut = &This->IEnumTfContexts_iface;
338 }
339
340 if (*ppvOut)
341 {
342 IEnumTfContexts_AddRef(iface);
343 return S_OK;
344 }
345
346 WARN("unsupported interface: %s\n", debugstr_guid(iid));
347 return E_NOINTERFACE;
348 }
349
350 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
351 {
352 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
353 return InterlockedIncrement(&This->refCount);
354 }
355
356 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
357 {
358 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
359 ULONG ret;
360
361 ret = InterlockedDecrement(&This->refCount);
362 if (ret == 0)
363 EnumTfContext_Destructor(This);
364 return ret;
365 }
366
367 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
368 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
369 {
370 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
371 ULONG fetched = 0;
372
373 TRACE("(%p)\n",This);
374
375 if (rgContext == NULL) return E_POINTER;
376
377 while (fetched < ulCount)
378 {
379 if (This->index > 1)
380 break;
381
382 if (!This->docmgr->contextStack[This->index])
383 break;
384
385 *rgContext = This->docmgr->contextStack[This->index];
386 ITfContext_AddRef(*rgContext);
387
388 ++This->index;
389 ++fetched;
390 ++rgContext;
391 }
392
393 if (pcFetched) *pcFetched = fetched;
394 return fetched == ulCount ? S_OK : S_FALSE;
395 }
396
397 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
398 {
399 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
400 TRACE("(%p)\n",This);
401 This->index += celt;
402 return S_OK;
403 }
404
405 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
406 {
407 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
408 TRACE("(%p)\n",This);
409 This->index = 0;
410 return S_OK;
411 }
412
413 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
414 IEnumTfContexts **ppenum)
415 {
416 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
417 HRESULT res;
418
419 TRACE("(%p)\n",This);
420
421 if (ppenum == NULL) return E_POINTER;
422
423 res = EnumTfContext_Constructor(This->docmgr, ppenum);
424 if (SUCCEEDED(res))
425 {
426 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
427 new_This->index = This->index;
428 }
429 return res;
430 }
431
432 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
433 EnumTfContext_QueryInterface,
434 EnumTfContext_AddRef,
435 EnumTfContext_Release,
436
437 EnumTfContext_Clone,
438 EnumTfContext_Next,
439 EnumTfContext_Reset,
440 EnumTfContext_Skip
441 };
442
443 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
444 {
445 EnumTfContext *This;
446
447 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
448 if (This == NULL)
449 return E_OUTOFMEMORY;
450
451 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
452 This->refCount = 1;
453 This->docmgr = mgr;
454
455 *ppOut = &This->IEnumTfContexts_iface;
456 TRACE("returning %p\n", *ppOut);
457 return S_OK;
458 }