ole32: Use share modes when creating storage files.
[wine/multimedia.git] / dlls / msctf / documentmgr.c
blobd9ba05735e957efe51dd1660aa650316e03a28f0
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->ITfDocumentMgr_iface;
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 ITfDocumentMgr_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(ITfContext_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 int i;
188 for (i = 0; i < sizeof(This->contextStack)/sizeof(This->contextStack[0]); i++)
189 if (This->contextStack[i])
191 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink, This->contextStack[i]);
192 Context_Uninitialize(This->contextStack[i]);
193 ITfContext_Release(This->contextStack[i]);
194 This->contextStack[i] = NULL;
197 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
198 return S_OK;
201 if (dwFlags)
202 return E_INVALIDARG;
204 if (This->contextStack[1] == NULL) /* Cannot pop last context */
205 return E_FAIL;
207 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
208 Context_Uninitialize(This->contextStack[0]);
209 ITfContext_Release(This->contextStack[0]);
210 This->contextStack[0] = This->contextStack[1];
211 This->contextStack[1] = NULL;
213 if (This->contextStack[0] == NULL)
214 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
216 return S_OK;
219 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
221 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
222 TRACE("(%p)\n",This);
223 if (!ppic)
224 return E_INVALIDARG;
226 if (This->contextStack[0])
227 ITfContext_AddRef(This->contextStack[0]);
229 *ppic = This->contextStack[0];
231 return S_OK;
234 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
236 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
237 ITfContext *tgt;
239 TRACE("(%p)\n",This);
240 if (!ppic)
241 return E_INVALIDARG;
243 if (This->contextStack[1])
244 tgt = This->contextStack[1];
245 else
246 tgt = This->contextStack[0];
248 if (tgt)
249 ITfContext_AddRef(tgt);
251 *ppic = tgt;
253 return S_OK;
256 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
258 DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
259 TRACE("(%p) %p\n",This,ppEnum);
260 return EnumTfContext_Constructor(This, ppEnum);
263 static const ITfDocumentMgrVtbl DocumentMgrVtbl =
265 DocumentMgr_QueryInterface,
266 DocumentMgr_AddRef,
267 DocumentMgr_Release,
268 DocumentMgr_CreateContext,
269 DocumentMgr_Push,
270 DocumentMgr_Pop,
271 DocumentMgr_GetTop,
272 DocumentMgr_GetBase,
273 DocumentMgr_EnumContexts
276 static HRESULT WINAPI DocumentMgrSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
278 DocumentMgr *This = impl_from_ITfSource(iface);
279 return ITfDocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, ppvOut);
282 static ULONG WINAPI DocumentMgrSource_AddRef(ITfSource *iface)
284 DocumentMgr *This = impl_from_ITfSource(iface);
285 return ITfDocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
288 static ULONG WINAPI DocumentMgrSource_Release(ITfSource *iface)
290 DocumentMgr *This = impl_from_ITfSource(iface);
291 return ITfDocumentMgr_Release(&This->ITfDocumentMgr_iface);
294 /*****************************************************
295 * ITfSource functions
296 *****************************************************/
297 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
298 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
300 DocumentMgr *This = impl_from_ITfSource(iface);
301 FIXME("STUB:(%p)\n",This);
302 return E_NOTIMPL;
305 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
307 DocumentMgr *This = impl_from_ITfSource(iface);
308 FIXME("STUB:(%p)\n",This);
309 return E_NOTIMPL;
312 static const ITfSourceVtbl DocumentMgrSourceVtbl =
314 DocumentMgrSource_QueryInterface,
315 DocumentMgrSource_AddRef,
316 DocumentMgrSource_Release,
317 DocumentMgrSource_AdviseSink,
318 DocumentMgrSource_UnadviseSink,
321 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
323 DocumentMgr *This;
325 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
326 if (This == NULL)
327 return E_OUTOFMEMORY;
329 This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgrVtbl;
330 This->ITfSource_iface.lpVtbl = &DocumentMgrSourceVtbl;
331 This->refCount = 1;
332 This->ThreadMgrSink = ThreadMgrSink;
334 CompartmentMgr_Constructor((IUnknown*)&This->ITfDocumentMgr_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
336 *ppOut = &This->ITfDocumentMgr_iface;
337 TRACE("returning %p\n", *ppOut);
338 return S_OK;
341 /**************************************************
342 * IEnumTfContexts implementation
343 **************************************************/
344 static void EnumTfContext_Destructor(EnumTfContext *This)
346 TRACE("destroying %p\n", This);
347 HeapFree(GetProcessHeap(),0,This);
350 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
352 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
353 *ppvOut = NULL;
355 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
357 *ppvOut = &This->IEnumTfContexts_iface;
360 if (*ppvOut)
362 IEnumTfContexts_AddRef(iface);
363 return S_OK;
366 WARN("unsupported interface: %s\n", debugstr_guid(iid));
367 return E_NOINTERFACE;
370 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
372 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
373 return InterlockedIncrement(&This->refCount);
376 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
378 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
379 ULONG ret;
381 ret = InterlockedDecrement(&This->refCount);
382 if (ret == 0)
383 EnumTfContext_Destructor(This);
384 return ret;
387 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
388 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
390 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
391 ULONG fetched = 0;
393 TRACE("(%p)\n",This);
395 if (rgContext == NULL) return E_POINTER;
397 while (fetched < ulCount)
399 if (This->index > 1)
400 break;
402 if (!This->docmgr->contextStack[This->index])
403 break;
405 *rgContext = This->docmgr->contextStack[This->index];
406 ITfContext_AddRef(*rgContext);
408 ++This->index;
409 ++fetched;
410 ++rgContext;
413 if (pcFetched) *pcFetched = fetched;
414 return fetched == ulCount ? S_OK : S_FALSE;
417 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
419 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
420 TRACE("(%p)\n",This);
421 This->index += celt;
422 return S_OK;
425 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
427 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
428 TRACE("(%p)\n",This);
429 This->index = 0;
430 return S_OK;
433 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
434 IEnumTfContexts **ppenum)
436 EnumTfContext *This = impl_from_IEnumTfContexts(iface);
437 HRESULT res;
439 TRACE("(%p)\n",This);
441 if (ppenum == NULL) return E_POINTER;
443 res = EnumTfContext_Constructor(This->docmgr, ppenum);
444 if (SUCCEEDED(res))
446 EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
447 new_This->index = This->index;
449 return res;
452 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
453 EnumTfContext_QueryInterface,
454 EnumTfContext_AddRef,
455 EnumTfContext_Release,
457 EnumTfContext_Clone,
458 EnumTfContext_Next,
459 EnumTfContext_Reset,
460 EnumTfContext_Skip
463 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
465 EnumTfContext *This;
467 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
468 if (This == NULL)
469 return E_OUTOFMEMORY;
471 This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
472 This->refCount = 1;
473 This->docmgr = mgr;
475 *ppOut = &This->IEnumTfContexts_iface;
476 TRACE("returning %p\n", *ppOut);
477 return S_OK;