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 const ITfDocumentMgrVtbl *DocumentMgrVtbl;
45 const ITfSourceVtbl *SourceVtbl;
49 ITfCompartmentMgr *CompartmentMgr;
51 ITfContext* contextStack[2]; /* limit of 2 contexts */
52 ITfThreadMgrEventSink* ThreadMgrSink;
55 typedef struct tagEnumTfContext {
56 const IEnumTfContextsVtbl *Vtbl;
63 static HRESULT EnumTfContext_Constructor(DocumentMgr* mgr, IEnumTfContexts **ppOut);
65 static inline DocumentMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
67 return (DocumentMgr *)((char *)iface - FIELD_OFFSET(DocumentMgr,SourceVtbl));
70 static void DocumentMgr_Destructor(DocumentMgr *This)
73 TRACE("destroying %p\n", This);
76 ThreadMgr_OnDocumentMgrDestruction(tm, (ITfDocumentMgr*)This);
78 if (This->contextStack[0])
79 ITfContext_Release(This->contextStack[0]);
80 if (This->contextStack[1])
81 ITfContext_Release(This->contextStack[1]);
82 CompartmentMgr_Destructor(This->CompartmentMgr);
83 HeapFree(GetProcessHeap(),0,This);
86 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
88 DocumentMgr *This = (DocumentMgr *)iface;
91 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
95 else if (IsEqualIID(iid, &IID_ITfSource))
97 *ppvOut = &This->SourceVtbl;
99 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
101 *ppvOut = This->CompartmentMgr;
106 IUnknown_AddRef(iface);
110 WARN("unsupported interface: %s\n", debugstr_guid(iid));
111 return E_NOINTERFACE;
114 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
116 DocumentMgr *This = (DocumentMgr *)iface;
117 return InterlockedIncrement(&This->refCount);
120 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
122 DocumentMgr *This = (DocumentMgr *)iface;
125 ret = InterlockedDecrement(&This->refCount);
127 DocumentMgr_Destructor(This);
131 /*****************************************************
132 * ITfDocumentMgr functions
133 *****************************************************/
134 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
136 DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
137 TfEditCookie *pecTextStore)
139 DocumentMgr *This = (DocumentMgr *)iface;
140 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
141 return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
144 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
146 DocumentMgr *This = (DocumentMgr *)iface;
149 TRACE("(%p) %p\n",This,pic);
151 if (This->contextStack[1]) /* FUll */
152 return TF_E_STACKFULL;
154 if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
157 if (This->contextStack[0] == NULL)
158 ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
160 This->contextStack[1] = This->contextStack[0];
161 This->contextStack[0] = check;
163 Context_Initialize(check, iface);
164 ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
169 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
171 DocumentMgr *This = (DocumentMgr *)iface;
172 TRACE("(%p) 0x%x\n",This,dwFlags);
174 if (dwFlags == TF_POPF_ALL)
176 if (This->contextStack[0])
178 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
179 ITfContext_Release(This->contextStack[0]);
180 Context_Uninitialize(This->contextStack[0]);
182 if (This->contextStack[1])
184 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
185 ITfContext_Release(This->contextStack[1]);
186 Context_Uninitialize(This->contextStack[1]);
188 This->contextStack[0] = This->contextStack[1] = NULL;
189 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
196 if (This->contextStack[1] == NULL) /* Cannot pop last context */
199 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
200 ITfContext_Release(This->contextStack[0]);
201 Context_Uninitialize(This->contextStack[0]);
202 This->contextStack[0] = This->contextStack[1];
203 This->contextStack[1] = NULL;
205 if (This->contextStack[0] == NULL)
206 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
211 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
213 DocumentMgr *This = (DocumentMgr *)iface;
214 TRACE("(%p)\n",This);
218 if (This->contextStack[0])
219 ITfContext_AddRef(This->contextStack[0]);
221 *ppic = This->contextStack[0];
226 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
228 DocumentMgr *This = (DocumentMgr *)iface;
231 TRACE("(%p)\n",This);
235 if (This->contextStack[1])
236 tgt = This->contextStack[1];
238 tgt = This->contextStack[0];
241 ITfContext_AddRef(tgt);
248 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
250 DocumentMgr *This = (DocumentMgr *)iface;
251 TRACE("(%p) %p\n",This,ppEnum);
252 return EnumTfContext_Constructor(This, ppEnum);
255 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
257 DocumentMgr_QueryInterface,
261 DocumentMgr_CreateContext,
266 DocumentMgr_EnumContexts
270 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
272 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
273 return DocumentMgr_QueryInterface((ITfDocumentMgr*)This, iid, *ppvOut);
276 static ULONG WINAPI Source_AddRef(ITfSource *iface)
278 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
279 return DocumentMgr_AddRef((ITfDocumentMgr*)This);
282 static ULONG WINAPI Source_Release(ITfSource *iface)
284 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
285 return DocumentMgr_Release((ITfDocumentMgr*)This);
288 /*****************************************************
289 * ITfSource functions
290 *****************************************************/
291 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
292 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
294 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
295 FIXME("STUB:(%p)\n",This);
299 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
301 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
302 FIXME("STUB:(%p)\n",This);
306 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
308 Source_QueryInterface,
312 DocumentMgrSource_AdviseSink,
313 DocumentMgrSource_UnadviseSink,
316 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
320 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
322 return E_OUTOFMEMORY;
324 This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
325 This->SourceVtbl = &DocumentMgr_SourceVtbl;
327 This->ThreadMgrSink = ThreadMgrSink;
329 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
331 TRACE("returning %p\n", This);
332 *ppOut = (ITfDocumentMgr*)This;
336 /**************************************************
337 * IEnumTfContexts implementaion
338 **************************************************/
339 static void EnumTfContext_Destructor(EnumTfContext *This)
341 TRACE("destroying %p\n", This);
342 HeapFree(GetProcessHeap(),0,This);
345 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
347 EnumTfContext *This = (EnumTfContext *)iface;
350 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
357 IUnknown_AddRef(iface);
361 WARN("unsupported interface: %s\n", debugstr_guid(iid));
362 return E_NOINTERFACE;
365 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
367 EnumTfContext *This = (EnumTfContext*)iface;
368 return InterlockedIncrement(&This->refCount);
371 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
373 EnumTfContext *This = (EnumTfContext *)iface;
376 ret = InterlockedDecrement(&This->refCount);
378 EnumTfContext_Destructor(This);
382 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
383 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
385 EnumTfContext *This = (EnumTfContext *)iface;
388 TRACE("(%p)\n",This);
390 if (rgContext == NULL) return E_POINTER;
392 while (fetched < ulCount)
397 if (!This->docmgr->contextStack[This->index])
400 *rgContext = This->docmgr->contextStack[This->index];
401 ITfContext_AddRef(*rgContext);
408 if (pcFetched) *pcFetched = fetched;
409 return fetched == ulCount ? S_OK : S_FALSE;
412 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
414 EnumTfContext *This = (EnumTfContext *)iface;
415 TRACE("(%p)\n",This);
420 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
422 EnumTfContext *This = (EnumTfContext *)iface;
423 TRACE("(%p)\n",This);
428 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
429 IEnumTfContexts **ppenum)
431 EnumTfContext *This = (EnumTfContext *)iface;
434 TRACE("(%p)\n",This);
436 if (ppenum == NULL) return E_POINTER;
438 res = EnumTfContext_Constructor(This->docmgr, ppenum);
441 EnumTfContext *new_This = (EnumTfContext *)*ppenum;
442 new_This->index = This->index;
447 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
448 EnumTfContext_QueryInterface,
449 EnumTfContext_AddRef,
450 EnumTfContext_Release,
458 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
462 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
464 return E_OUTOFMEMORY;
466 This->Vtbl= &IEnumTfContexts_Vtbl;
470 TRACE("returning %p\n", This);
471 *ppOut = (IEnumTfContexts*)This;