push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / msctf / documentmgr.c
blobc7aa8209ee10bde37256c06909785138e33c603f
1 /*
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
21 #include "config.h"
23 #include <stdarg.h>
25 #define COBJMACROS
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "shlwapi.h"
33 #include "winerror.h"
34 #include "objbase.h"
36 #include "wine/unicode.h"
38 #include "msctf.h"
39 #include "msctf_internal.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
43 typedef struct tagDocumentMgr {
44 const ITfDocumentMgrVtbl *DocumentMgrVtbl;
45 const ITfSourceVtbl *SourceVtbl;
46 LONG refCount;
48 /* Aggregation */
49 ITfCompartmentMgr *CompartmentMgr;
51 ITfContext* contextStack[2]; /* limit of 2 contexts */
52 ITfThreadMgrEventSink* ThreadMgrSink;
53 } DocumentMgr;
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;
74 *ppvOut = NULL;
76 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
78 *ppvOut = This;
80 else if (IsEqualIID(iid, &IID_ITfSource))
82 *ppvOut = &This->SourceVtbl;
84 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
86 *ppvOut = This->CompartmentMgr;
89 if (*ppvOut)
91 IUnknown_AddRef(iface);
92 return S_OK;
95 WARN("unsupported interface: %s\n", debugstr_guid(iid));
96 return E_NOINTERFACE;
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;
108 ULONG ret;
110 ret = InterlockedDecrement(&This->refCount);
111 if (ret == 0)
112 DocumentMgr_Destructor(This);
113 return ret;
116 /*****************************************************
117 * ITfDocumentMgr functions
118 *****************************************************/
119 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
120 TfClientId tidOwner,
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;
132 ITfContext *check;
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)))
140 return E_INVALIDARG;
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);
151 return S_OK;
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);
175 return S_OK;
178 if (dwFlags)
179 return E_INVALIDARG;
181 if (This->contextStack[1] == NULL) /* Cannot pop last context */
182 return E_FAIL;
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);
193 return S_OK;
196 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
198 DocumentMgr *This = (DocumentMgr *)iface;
199 TRACE("(%p)\n",This);
200 if (!ppic)
201 return E_INVALIDARG;
203 if (This->contextStack[0])
204 ITfContext_AddRef(This->contextStack[0]);
206 *ppic = This->contextStack[0];
208 return S_OK;
211 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
213 DocumentMgr *This = (DocumentMgr *)iface;
214 ITfContext *tgt;
216 TRACE("(%p)\n",This);
217 if (!ppic)
218 return E_INVALIDARG;
220 if (This->contextStack[1])
221 tgt = This->contextStack[1];
222 else
223 tgt = This->contextStack[0];
225 if (tgt)
226 ITfContext_AddRef(tgt);
228 *ppic = tgt;
230 return S_OK;
233 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
235 DocumentMgr *This = (DocumentMgr *)iface;
236 FIXME("STUB:(%p)\n",This);
237 return E_NOTIMPL;
240 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
242 DocumentMgr_QueryInterface,
243 DocumentMgr_AddRef,
244 DocumentMgr_Release,
246 DocumentMgr_CreateContext,
247 DocumentMgr_Push,
248 DocumentMgr_Pop,
249 DocumentMgr_GetTop,
250 DocumentMgr_GetBase,
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);
281 return E_NOTIMPL;
284 static WINAPI HRESULT DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
286 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
287 FIXME("STUB:(%p)\n",This);
288 return E_NOTIMPL;
291 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
293 Source_QueryInterface,
294 Source_AddRef,
295 Source_Release,
297 DocumentMgrSource_AdviseSink,
298 DocumentMgrSource_UnadviseSink,
301 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
303 DocumentMgr *This;
305 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
306 if (This == NULL)
307 return E_OUTOFMEMORY;
309 This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
310 This->SourceVtbl = &DocumentMgr_SourceVtbl;
311 This->refCount = 1;
312 This->ThreadMgrSink = ThreadMgrSink;
314 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
316 TRACE("returning %p\n", This);
317 *ppOut = (ITfDocumentMgr*)This;
318 return S_OK;