2 * ITfDocumentMgr implementation
4 * Copyright 2009 Aric Stewart, CodeWeavers
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.
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.
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
27 #include "wine/debug.h"
36 #include "wine/unicode.h"
39 #include "msctf_internal.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf
);
43 typedef struct tagDocumentMgr
{
44 ITfDocumentMgr ITfDocumentMgr_iface
;
45 ITfSource ITfSource_iface
;
49 ITfCompartmentMgr
*CompartmentMgr
;
51 ITfContext
* contextStack
[2]; /* limit of 2 contexts */
52 ITfThreadMgrEventSink
* ThreadMgrSink
;
55 typedef struct tagEnumTfContext
{
56 IEnumTfContexts IEnumTfContexts_iface
;
63 static HRESULT
EnumTfContext_Constructor(DocumentMgr
* mgr
, IEnumTfContexts
**ppOut
);
65 static inline DocumentMgr
*impl_from_ITfDocumentMgr(ITfDocumentMgr
*iface
)
67 return CONTAINING_RECORD(iface
, DocumentMgr
, ITfDocumentMgr_iface
);
70 static inline DocumentMgr
*impl_from_ITfSource(ITfSource
*iface
)
72 return CONTAINING_RECORD(iface
, DocumentMgr
, ITfSource_iface
);
75 static inline EnumTfContext
*impl_from_IEnumTfContexts(IEnumTfContexts
*iface
)
77 return CONTAINING_RECORD(iface
, EnumTfContext
, IEnumTfContexts_iface
);
80 static void DocumentMgr_Destructor(DocumentMgr
*This
)
83 TRACE("destroying %p\n", This
);
86 ThreadMgr_OnDocumentMgrDestruction(tm
, &This
->ITfDocumentMgr_iface
);
88 if (This
->contextStack
[0])
89 ITfContext_Release(This
->contextStack
[0]);
90 if (This
->contextStack
[1])
91 ITfContext_Release(This
->contextStack
[1]);
92 CompartmentMgr_Destructor(This
->CompartmentMgr
);
93 HeapFree(GetProcessHeap(),0,This
);
96 static HRESULT WINAPI
DocumentMgr_QueryInterface(ITfDocumentMgr
*iface
, REFIID iid
, LPVOID
*ppvOut
)
98 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
101 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_ITfDocumentMgr
))
103 *ppvOut
= &This
->ITfDocumentMgr_iface
;
105 else if (IsEqualIID(iid
, &IID_ITfSource
))
107 *ppvOut
= &This
->ITfSource_iface
;
109 else if (IsEqualIID(iid
, &IID_ITfCompartmentMgr
))
111 *ppvOut
= This
->CompartmentMgr
;
116 ITfDocumentMgr_AddRef(iface
);
120 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
121 return E_NOINTERFACE
;
124 static ULONG WINAPI
DocumentMgr_AddRef(ITfDocumentMgr
*iface
)
126 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
127 return InterlockedIncrement(&This
->refCount
);
130 static ULONG WINAPI
DocumentMgr_Release(ITfDocumentMgr
*iface
)
132 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
135 ret
= InterlockedDecrement(&This
->refCount
);
137 DocumentMgr_Destructor(This
);
141 /*****************************************************
142 * ITfDocumentMgr functions
143 *****************************************************/
144 static HRESULT WINAPI
DocumentMgr_CreateContext(ITfDocumentMgr
*iface
,
146 DWORD dwFlags
, IUnknown
*punk
, ITfContext
**ppic
,
147 TfEditCookie
*pecTextStore
)
149 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
150 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This
,tidOwner
,dwFlags
,punk
,ppic
,pecTextStore
);
151 return Context_Constructor(tidOwner
, punk
, iface
, ppic
, pecTextStore
);
154 static HRESULT WINAPI
DocumentMgr_Push(ITfDocumentMgr
*iface
, ITfContext
*pic
)
156 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
159 TRACE("(%p) %p\n",This
,pic
);
161 if (This
->contextStack
[1]) /* FUll */
162 return TF_E_STACKFULL
;
164 if (!pic
|| FAILED(ITfContext_QueryInterface(pic
,&IID_ITfContext
,(LPVOID
*) &check
)))
167 if (This
->contextStack
[0] == NULL
)
168 ITfThreadMgrEventSink_OnInitDocumentMgr(This
->ThreadMgrSink
,iface
);
170 This
->contextStack
[1] = This
->contextStack
[0];
171 This
->contextStack
[0] = check
;
173 Context_Initialize(check
, iface
);
174 ITfThreadMgrEventSink_OnPushContext(This
->ThreadMgrSink
,check
);
179 static HRESULT WINAPI
DocumentMgr_Pop(ITfDocumentMgr
*iface
, DWORD dwFlags
)
181 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
182 TRACE("(%p) 0x%x\n",This
,dwFlags
);
184 if (dwFlags
== TF_POPF_ALL
)
188 for (i
= 0; i
< sizeof(This
->contextStack
)/sizeof(This
->contextStack
[0]); i
++)
189 if (This
->contextStack
[i
])
191 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
, This
->contextStack
[i
]);
192 Context_Uninitialize(This
->contextStack
[i
]);
193 ITfContext_Release(This
->contextStack
[i
]);
194 This
->contextStack
[i
] = NULL
;
197 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
204 if (This
->contextStack
[1] == NULL
) /* Cannot pop last context */
207 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[0]);
208 Context_Uninitialize(This
->contextStack
[0]);
209 ITfContext_Release(This
->contextStack
[0]);
210 This
->contextStack
[0] = This
->contextStack
[1];
211 This
->contextStack
[1] = NULL
;
213 if (This
->contextStack
[0] == NULL
)
214 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
219 static HRESULT WINAPI
DocumentMgr_GetTop(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
221 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
222 TRACE("(%p)\n",This
);
226 if (This
->contextStack
[0])
227 ITfContext_AddRef(This
->contextStack
[0]);
229 *ppic
= This
->contextStack
[0];
234 static HRESULT WINAPI
DocumentMgr_GetBase(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
236 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
239 TRACE("(%p)\n",This
);
243 if (This
->contextStack
[1])
244 tgt
= This
->contextStack
[1];
246 tgt
= This
->contextStack
[0];
249 ITfContext_AddRef(tgt
);
256 static HRESULT WINAPI
DocumentMgr_EnumContexts(ITfDocumentMgr
*iface
, IEnumTfContexts
**ppEnum
)
258 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
259 TRACE("(%p) %p\n",This
,ppEnum
);
260 return EnumTfContext_Constructor(This
, ppEnum
);
263 static const ITfDocumentMgrVtbl DocumentMgrVtbl
=
265 DocumentMgr_QueryInterface
,
268 DocumentMgr_CreateContext
,
273 DocumentMgr_EnumContexts
276 static HRESULT WINAPI
DocumentMgrSource_QueryInterface(ITfSource
*iface
, REFIID iid
, LPVOID
*ppvOut
)
278 DocumentMgr
*This
= impl_from_ITfSource(iface
);
279 return ITfDocumentMgr_QueryInterface(&This
->ITfDocumentMgr_iface
, iid
, ppvOut
);
282 static ULONG WINAPI
DocumentMgrSource_AddRef(ITfSource
*iface
)
284 DocumentMgr
*This
= impl_from_ITfSource(iface
);
285 return ITfDocumentMgr_AddRef(&This
->ITfDocumentMgr_iface
);
288 static ULONG WINAPI
DocumentMgrSource_Release(ITfSource
*iface
)
290 DocumentMgr
*This
= impl_from_ITfSource(iface
);
291 return ITfDocumentMgr_Release(&This
->ITfDocumentMgr_iface
);
294 /*****************************************************
295 * ITfSource functions
296 *****************************************************/
297 static HRESULT WINAPI
DocumentMgrSource_AdviseSink(ITfSource
*iface
,
298 REFIID riid
, IUnknown
*punk
, DWORD
*pdwCookie
)
300 DocumentMgr
*This
= impl_from_ITfSource(iface
);
301 FIXME("STUB:(%p)\n",This
);
305 static HRESULT WINAPI
DocumentMgrSource_UnadviseSink(ITfSource
*iface
, DWORD pdwCookie
)
307 DocumentMgr
*This
= impl_from_ITfSource(iface
);
308 FIXME("STUB:(%p)\n",This
);
312 static const ITfSourceVtbl DocumentMgrSourceVtbl
=
314 DocumentMgrSource_QueryInterface
,
315 DocumentMgrSource_AddRef
,
316 DocumentMgrSource_Release
,
317 DocumentMgrSource_AdviseSink
,
318 DocumentMgrSource_UnadviseSink
,
321 HRESULT
DocumentMgr_Constructor(ITfThreadMgrEventSink
*ThreadMgrSink
, ITfDocumentMgr
**ppOut
)
325 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DocumentMgr
));
327 return E_OUTOFMEMORY
;
329 This
->ITfDocumentMgr_iface
.lpVtbl
= &DocumentMgrVtbl
;
330 This
->ITfSource_iface
.lpVtbl
= &DocumentMgrSourceVtbl
;
332 This
->ThreadMgrSink
= ThreadMgrSink
;
334 CompartmentMgr_Constructor((IUnknown
*)&This
->ITfDocumentMgr_iface
, &IID_IUnknown
, (IUnknown
**)&This
->CompartmentMgr
);
336 *ppOut
= &This
->ITfDocumentMgr_iface
;
337 TRACE("returning %p\n", *ppOut
);
341 /**************************************************
342 * IEnumTfContexts implementation
343 **************************************************/
344 static void EnumTfContext_Destructor(EnumTfContext
*This
)
346 TRACE("destroying %p\n", This
);
347 HeapFree(GetProcessHeap(),0,This
);
350 static HRESULT WINAPI
EnumTfContext_QueryInterface(IEnumTfContexts
*iface
, REFIID iid
, LPVOID
*ppvOut
)
352 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
355 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_IEnumTfContexts
))
357 *ppvOut
= &This
->IEnumTfContexts_iface
;
362 IEnumTfContexts_AddRef(iface
);
366 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
367 return E_NOINTERFACE
;
370 static ULONG WINAPI
EnumTfContext_AddRef(IEnumTfContexts
*iface
)
372 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
373 return InterlockedIncrement(&This
->refCount
);
376 static ULONG WINAPI
EnumTfContext_Release(IEnumTfContexts
*iface
)
378 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
381 ret
= InterlockedDecrement(&This
->refCount
);
383 EnumTfContext_Destructor(This
);
387 static HRESULT WINAPI
EnumTfContext_Next(IEnumTfContexts
*iface
,
388 ULONG ulCount
, ITfContext
**rgContext
, ULONG
*pcFetched
)
390 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
393 TRACE("(%p)\n",This
);
395 if (rgContext
== NULL
) return E_POINTER
;
397 while (fetched
< ulCount
)
402 if (!This
->docmgr
->contextStack
[This
->index
])
405 *rgContext
= This
->docmgr
->contextStack
[This
->index
];
406 ITfContext_AddRef(*rgContext
);
413 if (pcFetched
) *pcFetched
= fetched
;
414 return fetched
== ulCount
? S_OK
: S_FALSE
;
417 static HRESULT WINAPI
EnumTfContext_Skip( IEnumTfContexts
* iface
, ULONG celt
)
419 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
420 TRACE("(%p)\n",This
);
425 static HRESULT WINAPI
EnumTfContext_Reset( IEnumTfContexts
* iface
)
427 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
428 TRACE("(%p)\n",This
);
433 static HRESULT WINAPI
EnumTfContext_Clone( IEnumTfContexts
*iface
,
434 IEnumTfContexts
**ppenum
)
436 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
439 TRACE("(%p)\n",This
);
441 if (ppenum
== NULL
) return E_POINTER
;
443 res
= EnumTfContext_Constructor(This
->docmgr
, ppenum
);
446 EnumTfContext
*new_This
= impl_from_IEnumTfContexts(*ppenum
);
447 new_This
->index
= This
->index
;
452 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl
={
453 EnumTfContext_QueryInterface
,
454 EnumTfContext_AddRef
,
455 EnumTfContext_Release
,
463 static HRESULT
EnumTfContext_Constructor(DocumentMgr
*mgr
, IEnumTfContexts
**ppOut
)
467 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(EnumTfContext
));
469 return E_OUTOFMEMORY
;
471 This
->IEnumTfContexts_iface
.lpVtbl
= &IEnumTfContexts_Vtbl
;
475 *ppOut
= &This
->IEnumTfContexts_iface
;
476 TRACE("returning %p\n", *ppOut
);