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))
105 else if (IsEqualIID(iid, &IID_ITfSource))
107 *ppvOut = &This->ITfSource_iface;
109 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
111 *ppvOut = This->CompartmentMgr;
116 IUnknown_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(IUnknown_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)
186 if (This->contextStack[0])
188 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
189 ITfContext_Release(This->contextStack[0]);
190 Context_Uninitialize(This->contextStack[0]);
192 if (This->contextStack[1])
194 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
195 ITfContext_Release(This->contextStack[1]);
196 Context_Uninitialize(This->contextStack[1]);
198 This->contextStack[0] = This->contextStack[1] = NULL;
199 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
206 if (This->contextStack[1] == NULL) /* Cannot pop last context */
209 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
210 ITfContext_Release(This->contextStack[0]);
211 Context_Uninitialize(This->contextStack[0]);
212 This->contextStack[0] = This->contextStack[1];
213 This->contextStack[1] = NULL;
215 if (This->contextStack[0] == NULL)
216 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
221 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
223 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
224 TRACE("(%p)\n",This);
228 if (This->contextStack[0])
229 ITfContext_AddRef(This->contextStack[0]);
231 *ppic = This->contextStack[0];
236 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
238 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
241 TRACE("(%p)\n",This);
245 if (This->contextStack[1])
246 tgt = This->contextStack[1];
248 tgt = This->contextStack[0];
251 ITfContext_AddRef(tgt);
258 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
260 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
261 TRACE("(%p) %p\n",This,ppEnum);
262 return EnumTfContext_Constructor(This, ppEnum);
265 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
267 DocumentMgr_QueryInterface,
271 DocumentMgr_CreateContext,
276 DocumentMgr_EnumContexts
280 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
282 DocumentMgr *This = impl_from_ITfSource(iface);
283 return DocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, *ppvOut);
286 static ULONG WINAPI Source_AddRef(ITfSource *iface)
288 DocumentMgr *This = impl_from_ITfSource(iface);
289 return DocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
292 static ULONG WINAPI Source_Release(ITfSource *iface)
294 DocumentMgr *This = impl_from_ITfSource(iface);
295 return DocumentMgr_Release(&This->ITfDocumentMgr_iface);
298 /*****************************************************
299 * ITfSource functions
300 *****************************************************/
301 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
302 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
304 DocumentMgr *This = impl_from_ITfSource(iface);
305 FIXME("STUB:(%p)\n",This);
309 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
311 DocumentMgr *This = impl_from_ITfSource(iface);
312 FIXME("STUB:(%p)\n",This);
316 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
318 Source_QueryInterface,
322 DocumentMgrSource_AdviseSink,
323 DocumentMgrSource_UnadviseSink,
326 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
330 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
332 return E_OUTOFMEMORY;
334 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgr_DocumentMgrVtbl;
335 This->ITfSource_iface.lpVtbl = &DocumentMgr_SourceVtbl;
337 This->ThreadMgrSink = ThreadMgrSink;
339 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
341 TRACE("returning %p\n", This);
342 *ppOut = &This->ITfDocumentMgr_iface;
346 /**************************************************
347 * IEnumTfContexts implementaion
348 **************************************************/
349 static void EnumTfContext_Destructor(EnumTfContext *This)
351 TRACE("destroying %p\n", This);
352 HeapFree(GetProcessHeap(),0,This);
355 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
357 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
360 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
367 IUnknown_AddRef(iface);
371 WARN("unsupported interface: %s\n", debugstr_guid(iid));
372 return E_NOINTERFACE;
375 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
377 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
378 return InterlockedIncrement(&This->refCount);
381 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
383 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
386 ret = InterlockedDecrement(&This->refCount);
388 EnumTfContext_Destructor(This);
392 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
393 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
395 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
398 TRACE("(%p)\n",This);
400 if (rgContext == NULL) return E_POINTER;
402 while (fetched < ulCount)
407 if (!This->docmgr->contextStack[This->index])
410 *rgContext = This->docmgr->contextStack[This->index];
411 ITfContext_AddRef(*rgContext);
418 if (pcFetched) *pcFetched = fetched;
419 return fetched == ulCount ? S_OK : S_FALSE;
422 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
424 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
425 TRACE("(%p)\n",This);
430 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
432 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
433 TRACE("(%p)\n",This);
438 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
439 IEnumTfContexts **ppenum)
441 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
444 TRACE("(%p)\n",This);
446 if (ppenum == NULL) return E_POINTER;
448 res = EnumTfContext_Constructor(This->docmgr, ppenum);
451 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
452 new_This->index = This->index;
457 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
458 EnumTfContext_QueryInterface,
459 EnumTfContext_AddRef,
460 EnumTfContext_Release,
468 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
472 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
474 return E_OUTOFMEMORY;
476 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
480 TRACE("returning %p\n", This);
481 *ppOut = &This->IEnumTfContexts_iface;