quartz/tests: Add more tests for IMediaSeeking.
[wine.git] / dlls / msctf / documentmgr.c
blobcf4c601cff5b01fc52b0f6aa983a581e0f72d54d
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;
54 struct list TransitoryExtensionSink;
55 } DocumentMgr;
57 typedef struct tagEnumTfContext {
58 IEnumTfContexts IEnumTfContexts_iface;
59 LONG refCount;
61 DWORD index;
62 DocumentMgr *docmgr;
63 } EnumTfContext;
65 static HRESULT EnumTfContext_Constructor(DocumentMgr* mgr, IEnumTfContexts **ppOut);
67 static inline DocumentMgr *impl_from_ITfDocumentMgr(ITfDocumentMgr *iface)
69 return CONTAINING_RECORD(iface, DocumentMgr, ITfDocumentMgr_iface);
72 static inline DocumentMgr *impl_from_ITfSource(ITfSource *iface)
74 return CONTAINING_RECORD(iface, DocumentMgr, ITfSource_iface);
77 static inline EnumTfContext *impl_from_IEnumTfContexts(IEnumTfContexts *iface)
79 return CONTAINING_RECORD(iface, EnumTfContext, IEnumTfContexts_iface);
82 static void DocumentMgr_Destructor(DocumentMgr *This)
84 ITfThreadMgr *tm;
85 TRACE("destroying %p\n", This);
87 TF_GetThreadMgr(&tm);
88 ThreadMgr_OnDocumentMgrDestruction(tm, &This->ITfDocumentMgr_iface);
90 if (This->contextStack[0])
91 ITfContext_Release(This->contextStack[0]);
92 if (This->contextStack[1])
93 ITfContext_Release(This->contextStack[1]);
94 free_sinks(&This->TransitoryExtensionSink);
95 CompartmentMgr_Destructor(This->CompartmentMgr);
96 HeapFree(GetProcessHeap(),0,This);
99 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
101 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
102 *ppvOut = NULL;
104 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
106 *ppvOut = &This->ITfDocumentMgr_iface;
108 else if (IsEqualIID(iid, &IID_ITfSource))
110 *ppvOut = &This->ITfSource_iface;
112 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
114 *ppvOut = This->CompartmentMgr;
117 if (*ppvOut)
119 ITfDocumentMgr_AddRef(iface);
120 return S_OK;
123 WARN("unsupported interface: %s\n", debugstr_guid(iid));
124 return E_NOINTERFACE;
127 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
129 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
130 return InterlockedIncrement(&This->refCount);
133 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
135 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
136 ULONG ret;
138 ret = InterlockedDecrement(&This->refCount);
139 if (ret == 0)
140 DocumentMgr_Destructor(This);
141 return ret;
144 /*****************************************************
145 * ITfDocumentMgr functions
146 *****************************************************/
147 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
148 TfClientId tidOwner,
149 DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
150 TfEditCookie *pecTextStore)
152 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
153 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
154 return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
157 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
159 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
160 ITfContext *check;
162 TRACE("(%p) %p\n",This,pic);
164 if (This->contextStack[1]) /* FUll */
165 return TF_E_STACKFULL;
167 if (!pic || FAILED(ITfContext_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
168 return E_INVALIDARG;
170 if (This->contextStack[0] == NULL)
171 ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
173 This->contextStack[1] = This->contextStack[0];
174 This->contextStack[0] = check;
176 Context_Initialize(check, iface);
177 ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
179 return S_OK;
182 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
184 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
185 TRACE("(%p) 0x%x\n",This,dwFlags);
187 if (dwFlags == TF_POPF_ALL)
189 int i;
191 for (i = 0; i < sizeof(This->contextStack)/sizeof(This->contextStack[0]); i++)
192 if (This->contextStack[i])
194 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink, This->contextStack[i]);
195 Context_Uninitialize(This->contextStack[i]);
196 ITfContext_Release(This->contextStack[i]);
197 This->contextStack[i] = NULL;
200 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
201 return S_OK;
204 if (dwFlags)
205 return E_INVALIDARG;
207 if (This->contextStack[1] == NULL) /* Cannot pop last context */
208 return E_FAIL;
210 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
211 Context_Uninitialize(This->contextStack[0]);
212 ITfContext_Release(This->contextStack[0]);
213 This->contextStack[0] = This->contextStack[1];
214 This->contextStack[1] = NULL;
216 if (This->contextStack[0] == NULL)
217 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
219 return S_OK;
222 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
224 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
225 TRACE("(%p)\n",This);
226 if (!ppic)
227 return E_INVALIDARG;
229 if (This->contextStack[0])
230 ITfContext_AddRef(This->contextStack[0]);
232 *ppic = This->contextStack[0];
234 return S_OK;
237 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
239 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
240 ITfContext *tgt;
242 TRACE("(%p)\n",This);
243 if (!ppic)
244 return E_INVALIDARG;
246 if (This->contextStack[1])
247 tgt = This->contextStack[1];
248 else
249 tgt = This->contextStack[0];
251 if (tgt)
252 ITfContext_AddRef(tgt);
254 *ppic = tgt;
256 return S_OK;
259 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
261 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
262 TRACE("(%p) %p\n",This,ppEnum);
263 return EnumTfContext_Constructor(This, ppEnum);
266 static const ITfDocumentMgrVtbl DocumentMgrVtbl =
268 DocumentMgr_QueryInterface,
269 DocumentMgr_AddRef,
270 DocumentMgr_Release,
271 DocumentMgr_CreateContext,
272 DocumentMgr_Push,
273 DocumentMgr_Pop,
274 DocumentMgr_GetTop,
275 DocumentMgr_GetBase,
276 DocumentMgr_EnumContexts
279 static HRESULT WINAPI DocumentMgrSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
281 DocumentMgr *This = impl_from_ITfSource(iface);
282 return ITfDocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, ppvOut);
285 static ULONG WINAPI DocumentMgrSource_AddRef(ITfSource *iface)
287 DocumentMgr *This = impl_from_ITfSource(iface);
288 return ITfDocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
291 static ULONG WINAPI DocumentMgrSource_Release(ITfSource *iface)
293 DocumentMgr *This = impl_from_ITfSource(iface);
294 return ITfDocumentMgr_Release(&This->ITfDocumentMgr_iface);
297 /*****************************************************
298 * ITfSource functions
299 *****************************************************/
300 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
301 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
303 DocumentMgr *This = impl_from_ITfSource(iface);
305 TRACE("(%p) %s %p %p\n", This, debugstr_guid(riid), punk, pdwCookie);
307 if (!riid || !punk || !pdwCookie)
308 return E_INVALIDARG;
310 if (IsEqualIID(riid, &IID_ITfTransitoryExtensionSink))
312 WARN("semi-stub for ITfTransitoryExtensionSink: callback won't be used.\n");
313 return advise_sink(&This->TransitoryExtensionSink, &IID_ITfTransitoryExtensionSink,
314 COOKIE_MAGIC_DMSINK, punk, pdwCookie);
317 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
318 return E_NOTIMPL;
321 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
323 DocumentMgr *This = impl_from_ITfSource(iface);
325 TRACE("(%p) %x\n",This,pdwCookie);
327 if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_DMSINK)
328 return E_INVALIDARG;
330 return unadvise_sink(pdwCookie);
333 static const ITfSourceVtbl DocumentMgrSourceVtbl =
335 DocumentMgrSource_QueryInterface,
336 DocumentMgrSource_AddRef,
337 DocumentMgrSource_Release,
338 DocumentMgrSource_AdviseSink,
339 DocumentMgrSource_UnadviseSink,
342 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
344 DocumentMgr *This;
346 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
347 if (This == NULL)
348 return E_OUTOFMEMORY;
350 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgrVtbl;
351 This->ITfSource_iface.lpVtbl = &DocumentMgrSourceVtbl;
352 This->refCount = 1;
353 This->ThreadMgrSink = ThreadMgrSink;
354 list_init(&This->TransitoryExtensionSink);
356 CompartmentMgr_Constructor((IUnknown*)&This->ITfDocumentMgr_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
358 *ppOut = &This->ITfDocumentMgr_iface;
359 TRACE("returning %p\n", *ppOut);
360 return S_OK;
363 /**************************************************
364 * IEnumTfContexts implementation
365 **************************************************/
366 static void EnumTfContext_Destructor(EnumTfContext *This)
368 TRACE("destroying %p\n", This);
369 HeapFree(GetProcessHeap(),0,This);
372 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
374 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
375 *ppvOut = NULL;
377 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
379 *ppvOut = &This->IEnumTfContexts_iface;
382 if (*ppvOut)
384 IEnumTfContexts_AddRef(iface);
385 return S_OK;
388 WARN("unsupported interface: %s\n", debugstr_guid(iid));
389 return E_NOINTERFACE;
392 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
394 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
395 return InterlockedIncrement(&This->refCount);
398 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
400 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
401 ULONG ret;
403 ret = InterlockedDecrement(&This->refCount);
404 if (ret == 0)
405 EnumTfContext_Destructor(This);
406 return ret;
409 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
410 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
412 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
413 ULONG fetched = 0;
415 TRACE("(%p)\n",This);
417 if (rgContext == NULL) return E_POINTER;
419 while (fetched < ulCount)
421 if (This->index > 1)
422 break;
424 if (!This->docmgr->contextStack[This->index])
425 break;
427 *rgContext = This->docmgr->contextStack[This->index];
428 ITfContext_AddRef(*rgContext);
430 ++This->index;
431 ++fetched;
432 ++rgContext;
435 if (pcFetched) *pcFetched = fetched;
436 return fetched == ulCount ? S_OK : S_FALSE;
439 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
441 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
442 TRACE("(%p)\n",This);
443 This->index += celt;
444 return S_OK;
447 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
449 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
450 TRACE("(%p)\n",This);
451 This->index = 0;
452 return S_OK;
455 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
456 IEnumTfContexts **ppenum)
458 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
459 HRESULT res;
461 TRACE("(%p)\n",This);
463 if (ppenum == NULL) return E_POINTER;
465 res = EnumTfContext_Constructor(This->docmgr, ppenum);
466 if (SUCCEEDED(res))
468 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
469 new_This->index = This->index;
471 return res;
474 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
475 EnumTfContext_QueryInterface,
476 EnumTfContext_AddRef,
477 EnumTfContext_Release,
479 EnumTfContext_Clone,
480 EnumTfContext_Next,
481 EnumTfContext_Reset,
482 EnumTfContext_Skip
485 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
487 EnumTfContext *This;
489 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
490 if (This == NULL)
491 return E_OUTOFMEMORY;
493 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
494 This->refCount = 1;
495 This->docmgr = mgr;
497 *ppOut = &This->IEnumTfContexts_iface;
498 TRACE("returning %p\n", *ppOut);
499 return S_OK;