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 static inline DocumentMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
57 return (DocumentMgr *)((char *)iface - FIELD_OFFSET(DocumentMgr,SourceVtbl));
60 static void DocumentMgr_Destructor(DocumentMgr *This)
62 TRACE("destroying %p\n", This);
63 if (This->contextStack[0])
64 ITfContext_Release(This->contextStack[0]);
65 if (This->contextStack[1])
66 ITfContext_Release(This->contextStack[1]);
67 CompartmentMgr_Destructor(This->CompartmentMgr);
68 HeapFree(GetProcessHeap(),0,This);
71 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
73 DocumentMgr *This = (DocumentMgr *)iface;
76 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
80 else if (IsEqualIID(iid, &IID_ITfSource))
82 *ppvOut = &This->SourceVtbl;
84 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
86 *ppvOut = This->CompartmentMgr;
91 IUnknown_AddRef(iface);
95 WARN("unsupported interface: %s\n", debugstr_guid(iid));
99 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
101 DocumentMgr *This = (DocumentMgr *)iface;
102 return InterlockedIncrement(&This->refCount);
105 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
107 DocumentMgr *This = (DocumentMgr *)iface;
110 ret = InterlockedDecrement(&This->refCount);
112 DocumentMgr_Destructor(This);
116 /*****************************************************
117 * ITfDocumentMgr functions
118 *****************************************************/
119 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
121 DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
122 TfEditCookie *pecTextStore)
124 DocumentMgr *This = (DocumentMgr *)iface;
125 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
126 return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
129 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
131 DocumentMgr *This = (DocumentMgr *)iface;
134 TRACE("(%p) %p\n",This,pic);
136 if (This->contextStack[1]) /* FUll */
137 return TF_E_STACKFULL;
139 if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
142 if (This->contextStack[0] == NULL)
143 ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
145 This->contextStack[1] = This->contextStack[0];
146 This->contextStack[0] = check;
148 ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
149 Context_Initialize(check, iface);
154 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
156 DocumentMgr *This = (DocumentMgr *)iface;
157 TRACE("(%p) 0x%x\n",This,dwFlags);
159 if (dwFlags == TF_POPF_ALL)
161 if (This->contextStack[0])
163 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
164 ITfContext_Release(This->contextStack[0]);
165 Context_Uninitialize(This->contextStack[0]);
167 if (This->contextStack[1])
169 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
170 ITfContext_Release(This->contextStack[1]);
171 Context_Uninitialize(This->contextStack[1]);
173 This->contextStack[0] = This->contextStack[1] = NULL;
174 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
181 if (This->contextStack[1] == NULL) /* Cannot pop last context */
184 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
185 ITfContext_Release(This->contextStack[0]);
186 Context_Uninitialize(This->contextStack[0]);
187 This->contextStack[0] = This->contextStack[1];
188 This->contextStack[1] = NULL;
190 if (This->contextStack[0] == NULL)
191 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
196 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
198 DocumentMgr *This = (DocumentMgr *)iface;
199 TRACE("(%p)\n",This);
203 if (This->contextStack[0])
204 ITfContext_AddRef(This->contextStack[0]);
206 *ppic = This->contextStack[0];
211 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
213 DocumentMgr *This = (DocumentMgr *)iface;
216 TRACE("(%p)\n",This);
220 if (This->contextStack[1])
221 tgt = This->contextStack[1];
223 tgt = This->contextStack[0];
226 ITfContext_AddRef(tgt);
233 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
235 DocumentMgr *This = (DocumentMgr *)iface;
236 FIXME("STUB:(%p)\n",This);
240 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
242 DocumentMgr_QueryInterface,
246 DocumentMgr_CreateContext,
251 DocumentMgr_EnumContexts
255 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
257 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
258 return DocumentMgr_QueryInterface((ITfDocumentMgr*)This, iid, *ppvOut);
261 static ULONG WINAPI Source_AddRef(ITfSource *iface)
263 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
264 return DocumentMgr_AddRef((ITfDocumentMgr*)This);
267 static ULONG WINAPI Source_Release(ITfSource *iface)
269 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
270 return DocumentMgr_Release((ITfDocumentMgr*)This);
273 /*****************************************************
274 * ITfSource functions
275 *****************************************************/
276 static WINAPI HRESULT DocumentMgrSource_AdviseSink(ITfSource *iface,
277 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
279 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
280 FIXME("STUB:(%p)\n",This);
284 static WINAPI HRESULT DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
286 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
287 FIXME("STUB:(%p)\n",This);
291 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
293 Source_QueryInterface,
297 DocumentMgrSource_AdviseSink,
298 DocumentMgrSource_UnadviseSink,
301 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
305 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
307 return E_OUTOFMEMORY;
309 This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
310 This->SourceVtbl = &DocumentMgr_SourceVtbl;
312 This->ThreadMgrSink = ThreadMgrSink;
314 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
316 TRACE("returning %p\n", This);
317 *ppOut = (ITfDocumentMgr*)This;