winmm: dwBytesRecorded controls how much is played, not dwBufferLength.
[wine/hramrach.git] / dlls / msctf / documentmgr.c
blobfec3de3786ef355c2a3feb4de82077d6d9ed32d6
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 typedef struct tagEnumTfContext {
56 const IEnumTfContextsVtbl *Vtbl;
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_ITfSourceVtbl(ITfSource *iface)
67 return (DocumentMgr *)((char *)iface - FIELD_OFFSET(DocumentMgr,SourceVtbl));
70 static void DocumentMgr_Destructor(DocumentMgr *This)
72 ITfThreadMgr *tm;
73 TRACE("destroying %p\n", This);
75 TF_GetThreadMgr(&tm);
76 ThreadMgr_OnDocumentMgrDestruction(tm, (ITfDocumentMgr*)This);
78 if (This->contextStack[0])
79 ITfContext_Release(This->contextStack[0]);
80 if (This->contextStack[1])
81 ITfContext_Release(This->contextStack[1]);
82 CompartmentMgr_Destructor(This->CompartmentMgr);
83 HeapFree(GetProcessHeap(),0,This);
86 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
88 DocumentMgr *This = (DocumentMgr *)iface;
89 *ppvOut = NULL;
91 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
93 *ppvOut = This;
95 else if (IsEqualIID(iid, &IID_ITfSource))
97 *ppvOut = &This->SourceVtbl;
99 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
101 *ppvOut = This->CompartmentMgr;
104 if (*ppvOut)
106 IUnknown_AddRef(iface);
107 return S_OK;
110 WARN("unsupported interface: %s\n", debugstr_guid(iid));
111 return E_NOINTERFACE;
114 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
116 DocumentMgr *This = (DocumentMgr *)iface;
117 return InterlockedIncrement(&This->refCount);
120 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
122 DocumentMgr *This = (DocumentMgr *)iface;
123 ULONG ret;
125 ret = InterlockedDecrement(&This->refCount);
126 if (ret == 0)
127 DocumentMgr_Destructor(This);
128 return ret;
131 /*****************************************************
132 * ITfDocumentMgr functions
133 *****************************************************/
134 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
135 TfClientId tidOwner,
136 DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
137 TfEditCookie *pecTextStore)
139 DocumentMgr *This = (DocumentMgr *)iface;
140 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
141 return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
144 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
146 DocumentMgr *This = (DocumentMgr *)iface;
147 ITfContext *check;
149 TRACE("(%p) %p\n",This,pic);
151 if (This->contextStack[1]) /* FUll */
152 return TF_E_STACKFULL;
154 if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
155 return E_INVALIDARG;
157 if (This->contextStack[0] == NULL)
158 ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
160 This->contextStack[1] = This->contextStack[0];
161 This->contextStack[0] = check;
163 Context_Initialize(check, iface);
164 ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
166 return S_OK;
169 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
171 DocumentMgr *This = (DocumentMgr *)iface;
172 TRACE("(%p) 0x%x\n",This,dwFlags);
174 if (dwFlags == TF_POPF_ALL)
176 if (This->contextStack[0])
178 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
179 ITfContext_Release(This->contextStack[0]);
180 Context_Uninitialize(This->contextStack[0]);
182 if (This->contextStack[1])
184 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
185 ITfContext_Release(This->contextStack[1]);
186 Context_Uninitialize(This->contextStack[1]);
188 This->contextStack[0] = This->contextStack[1] = NULL;
189 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
190 return S_OK;
193 if (dwFlags)
194 return E_INVALIDARG;
196 if (This->contextStack[1] == NULL) /* Cannot pop last context */
197 return E_FAIL;
199 ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
200 ITfContext_Release(This->contextStack[0]);
201 Context_Uninitialize(This->contextStack[0]);
202 This->contextStack[0] = This->contextStack[1];
203 This->contextStack[1] = NULL;
205 if (This->contextStack[0] == NULL)
206 ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
208 return S_OK;
211 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
213 DocumentMgr *This = (DocumentMgr *)iface;
214 TRACE("(%p)\n",This);
215 if (!ppic)
216 return E_INVALIDARG;
218 if (This->contextStack[0])
219 ITfContext_AddRef(This->contextStack[0]);
221 *ppic = This->contextStack[0];
223 return S_OK;
226 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
228 DocumentMgr *This = (DocumentMgr *)iface;
229 ITfContext *tgt;
231 TRACE("(%p)\n",This);
232 if (!ppic)
233 return E_INVALIDARG;
235 if (This->contextStack[1])
236 tgt = This->contextStack[1];
237 else
238 tgt = This->contextStack[0];
240 if (tgt)
241 ITfContext_AddRef(tgt);
243 *ppic = tgt;
245 return S_OK;
248 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
250 DocumentMgr *This = (DocumentMgr *)iface;
251 TRACE("(%p) %p\n",This,ppEnum);
252 return EnumTfContext_Constructor(This, ppEnum);
255 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
257 DocumentMgr_QueryInterface,
258 DocumentMgr_AddRef,
259 DocumentMgr_Release,
261 DocumentMgr_CreateContext,
262 DocumentMgr_Push,
263 DocumentMgr_Pop,
264 DocumentMgr_GetTop,
265 DocumentMgr_GetBase,
266 DocumentMgr_EnumContexts
270 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
272 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
273 return DocumentMgr_QueryInterface((ITfDocumentMgr*)This, iid, *ppvOut);
276 static ULONG WINAPI Source_AddRef(ITfSource *iface)
278 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
279 return DocumentMgr_AddRef((ITfDocumentMgr*)This);
282 static ULONG WINAPI Source_Release(ITfSource *iface)
284 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
285 return DocumentMgr_Release((ITfDocumentMgr*)This);
288 /*****************************************************
289 * ITfSource functions
290 *****************************************************/
291 static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
292 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
294 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
295 FIXME("STUB:(%p)\n",This);
296 return E_NOTIMPL;
299 static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
301 DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
302 FIXME("STUB:(%p)\n",This);
303 return E_NOTIMPL;
306 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
308 Source_QueryInterface,
309 Source_AddRef,
310 Source_Release,
312 DocumentMgrSource_AdviseSink,
313 DocumentMgrSource_UnadviseSink,
316 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
318 DocumentMgr *This;
320 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
321 if (This == NULL)
322 return E_OUTOFMEMORY;
324 This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
325 This->SourceVtbl = &DocumentMgr_SourceVtbl;
326 This->refCount = 1;
327 This->ThreadMgrSink = ThreadMgrSink;
329 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
331 TRACE("returning %p\n", This);
332 *ppOut = (ITfDocumentMgr*)This;
333 return S_OK;
336 /**************************************************
337 * IEnumTfContexts implementaion
338 **************************************************/
339 static void EnumTfContext_Destructor(EnumTfContext *This)
341 TRACE("destroying %p\n", This);
342 HeapFree(GetProcessHeap(),0,This);
345 static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
347 EnumTfContext *This = (EnumTfContext *)iface;
348 *ppvOut = NULL;
350 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
352 *ppvOut = This;
355 if (*ppvOut)
357 IUnknown_AddRef(iface);
358 return S_OK;
361 WARN("unsupported interface: %s\n", debugstr_guid(iid));
362 return E_NOINTERFACE;
365 static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
367 EnumTfContext *This = (EnumTfContext*)iface;
368 return InterlockedIncrement(&This->refCount);
371 static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
373 EnumTfContext *This = (EnumTfContext *)iface;
374 ULONG ret;
376 ret = InterlockedDecrement(&This->refCount);
377 if (ret == 0)
378 EnumTfContext_Destructor(This);
379 return ret;
382 static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
383 ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
385 EnumTfContext *This = (EnumTfContext *)iface;
386 ULONG fetched = 0;
388 TRACE("(%p)\n",This);
390 if (rgContext == NULL) return E_POINTER;
392 while (fetched < ulCount)
394 if (This->index > 1)
395 break;
397 if (!This->docmgr->contextStack[This->index])
398 break;
400 *rgContext = This->docmgr->contextStack[This->index];
401 ITfContext_AddRef(*rgContext);
403 ++This->index;
404 ++fetched;
405 ++rgContext;
408 if (pcFetched) *pcFetched = fetched;
409 return fetched == ulCount ? S_OK : S_FALSE;
412 static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
414 EnumTfContext *This = (EnumTfContext *)iface;
415 TRACE("(%p)\n",This);
416 This->index += celt;
417 return S_OK;
420 static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
422 EnumTfContext *This = (EnumTfContext *)iface;
423 TRACE("(%p)\n",This);
424 This->index = 0;
425 return S_OK;
428 static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
429 IEnumTfContexts **ppenum)
431 EnumTfContext *This = (EnumTfContext *)iface;
432 HRESULT res;
434 TRACE("(%p)\n",This);
436 if (ppenum == NULL) return E_POINTER;
438 res = EnumTfContext_Constructor(This->docmgr, ppenum);
439 if (SUCCEEDED(res))
441 EnumTfContext *new_This = (EnumTfContext *)*ppenum;
442 new_This->index = This->index;
444 return res;
447 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
448 EnumTfContext_QueryInterface,
449 EnumTfContext_AddRef,
450 EnumTfContext_Release,
452 EnumTfContext_Clone,
453 EnumTfContext_Next,
454 EnumTfContext_Reset,
455 EnumTfContext_Skip
458 static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
460 EnumTfContext *This;
462 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
463 if (This == NULL)
464 return E_OUTOFMEMORY;
466 This->Vtbl= &IEnumTfContexts_Vtbl;
467 This->refCount = 1;
468 This->docmgr = mgr;
470 TRACE("returning %p\n", This);
471 *ppOut = (IEnumTfContexts*)This;
472 return S_OK;