wininet: Use the correct struct in a sizeof.
[wine/multimedia.git] / dlls / msctf / documentmgr.c
blob4e216244269a09cbbd757eafdbea9423bf5b05ad
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 ITfDocumentMgr ITfDocumentMgr_iface;
45 ITfSource ITfSource_iface;
46 LONG refCount;
48 /* Aggregation */
49 ITfCompartmentMgr *CompartmentMgr;
51 ITfContext* contextStack[2]; /* limit of 2 contexts */
52 ITfThreadMgrEventSink* ThreadMgrSink;
53 } DocumentMgr;
55 typedef struct tagEnumTfContext {
56 IEnumTfContexts IEnumTfContexts_iface;
57 LONG refCount;
59 DWORD index;
60 DocumentMgr *docmgr;
61 } EnumTfContext;
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)
82 ITfThreadMgr *tm;
83 TRACE("destroying %p\n", This);
85 TF_GetThreadMgr(&tm);
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);
99 *ppvOut = NULL;
101 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
103 *ppvOut = This;
105 else if (IsEqualIID(iid, &IID_ITfSource))
107 *ppvOut = &This->ITfSource_iface;
109 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
111 *ppvOut = This->CompartmentMgr;
114 if (*ppvOut)
116 IUnknown_AddRef(iface);
117 return S_OK;
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);
133 ULONG ret;
135 ret = InterlockedDecrement(&This->refCount);
136 if (ret == 0)
137 DocumentMgr_Destructor(This);
138 return ret;
141 /*****************************************************
142 * ITfDocumentMgr functions
143 *****************************************************/
144 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
145 TfClientId tidOwner,
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);
157 ITfContext *check;
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)))
165 return E_INVALIDARG;
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);
176 return S_OK;
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);
200 return S_OK;
203 if (dwFlags)
204 return E_INVALIDARG;
206 if (This->contextStack[1] == NULL) /* Cannot pop last context */
207 return E_FAIL;
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);
218 return S_OK;
221 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
223 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
224 TRACE("(%p)\n",This);
225 if (!ppic)
226 return E_INVALIDARG;
228 if (This->contextStack[0])
229 ITfContext_AddRef(This->contextStack[0]);
231 *ppic = This->contextStack[0];
233 return S_OK;
236 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
238 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
239 ITfContext *tgt;
241 TRACE("(%p)\n",This);
242 if (!ppic)
243 return E_INVALIDARG;
245 if (This->contextStack[1])
246 tgt = This->contextStack[1];
247 else
248 tgt = This->contextStack[0];
250 if (tgt)
251 ITfContext_AddRef(tgt);
253 *ppic = tgt;
255 return S_OK;
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,
268 DocumentMgr_AddRef,
269 DocumentMgr_Release,
271 DocumentMgr_CreateContext,
272 DocumentMgr_Push,
273 DocumentMgr_Pop,
274 DocumentMgr_GetTop,
275 DocumentMgr_GetBase,
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);
306 return E_NOTIMPL;
309 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
311 DocumentMgr *This = impl_from_ITfSource(iface);
312 FIXME("STUB:(%p)\n",This);
313 return E_NOTIMPL;
316 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
318 Source_QueryInterface,
319 Source_AddRef,
320 Source_Release,
322 DocumentMgrSource_AdviseSink,
323 DocumentMgrSource_UnadviseSink,
326 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
328 DocumentMgr *This;
330 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
331 if (This == NULL)
332 return E_OUTOFMEMORY;
334 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgr_DocumentMgrVtbl;
335 This->ITfSource_iface.lpVtbl = &DocumentMgr_SourceVtbl;
336 This->refCount = 1;
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;
343 return S_OK;
346 /**************************************************
347 * IEnumTfContexts implementation
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);
358 *ppvOut = NULL;
360 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
362 *ppvOut = This;
365 if (*ppvOut)
367 IUnknown_AddRef(iface);
368 return S_OK;
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);
384 ULONG ret;
386 ret = InterlockedDecrement(&This->refCount);
387 if (ret == 0)
388 EnumTfContext_Destructor(This);
389 return ret;
392 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
393 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
395 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
396 ULONG fetched = 0;
398 TRACE("(%p)\n",This);
400 if (rgContext == NULL) return E_POINTER;
402 while (fetched < ulCount)
404 if (This->index > 1)
405 break;
407 if (!This->docmgr->contextStack[This->index])
408 break;
410 *rgContext = This->docmgr->contextStack[This->index];
411 ITfContext_AddRef(*rgContext);
413 ++This->index;
414 ++fetched;
415 ++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);
426 This->index += celt;
427 return S_OK;
430 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
432 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
433 TRACE("(%p)\n",This);
434 This->index = 0;
435 return S_OK;
438 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
439 IEnumTfContexts **ppenum)
441 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
442 HRESULT res;
444 TRACE("(%p)\n",This);
446 if (ppenum == NULL) return E_POINTER;
448 res = EnumTfContext_Constructor(This->docmgr, ppenum);
449 if (SUCCEEDED(res))
451 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
452 new_This->index = This->index;
454 return res;
457 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
458 EnumTfContext_QueryInterface,
459 EnumTfContext_AddRef,
460 EnumTfContext_Release,
462 EnumTfContext_Clone,
463 EnumTfContext_Next,
464 EnumTfContext_Reset,
465 EnumTfContext_Skip
468 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
470 EnumTfContext *This;
472 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
473 if (This == NULL)
474 return E_OUTOFMEMORY;
476 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
477 This->refCount = 1;
478 This->docmgr = mgr;
480 TRACE("returning %p\n", This);
481 *ppOut = &This->IEnumTfContexts_iface;
482 return S_OK;