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
27 #include "wine/debug.h"
36 #include "wine/unicode.h"
39 #include "msctf_internal.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf
);
43 typedef struct tagDocumentMgr
{
44 ITfDocumentMgr ITfDocumentMgr_iface
;
45 ITfSource ITfSource_iface
;
49 ITfCompartmentMgr
*CompartmentMgr
;
51 ITfContext
* contextStack
[2]; /* limit of 2 contexts */
52 ITfThreadMgrEventSink
* ThreadMgrSink
;
54 struct list TransitoryExtensionSink
;
57 typedef struct tagEnumTfContext
{
58 IEnumTfContexts IEnumTfContexts_iface
;
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
)
85 TRACE("destroying %p\n", This
);
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
);
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
;
119 ITfDocumentMgr_AddRef(iface
);
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
);
138 ret
= InterlockedDecrement(&This
->refCount
);
140 DocumentMgr_Destructor(This
);
144 /*****************************************************
145 * ITfDocumentMgr functions
146 *****************************************************/
147 static HRESULT WINAPI
DocumentMgr_CreateContext(ITfDocumentMgr
*iface
,
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
);
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
)))
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
);
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
)
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
);
207 if (This
->contextStack
[1] == NULL
) /* Cannot pop last context */
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
);
222 static HRESULT WINAPI
DocumentMgr_GetTop(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
224 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
225 TRACE("(%p)\n",This
);
229 if (This
->contextStack
[0])
230 ITfContext_AddRef(This
->contextStack
[0]);
232 *ppic
= This
->contextStack
[0];
237 static HRESULT WINAPI
DocumentMgr_GetBase(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
239 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
242 TRACE("(%p)\n",This
);
246 if (This
->contextStack
[1])
247 tgt
= This
->contextStack
[1];
249 tgt
= This
->contextStack
[0];
252 ITfContext_AddRef(tgt
);
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
,
271 DocumentMgr_CreateContext
,
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
)
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
));
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
)
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
)
346 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DocumentMgr
));
348 return E_OUTOFMEMORY
;
350 This
->ITfDocumentMgr_iface
.lpVtbl
= &DocumentMgrVtbl
;
351 This
->ITfSource_iface
.lpVtbl
= &DocumentMgrSourceVtbl
;
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
);
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
);
377 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_IEnumTfContexts
))
379 *ppvOut
= &This
->IEnumTfContexts_iface
;
384 IEnumTfContexts_AddRef(iface
);
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
);
403 ret
= InterlockedDecrement(&This
->refCount
);
405 EnumTfContext_Destructor(This
);
409 static HRESULT WINAPI
EnumTfContext_Next(IEnumTfContexts
*iface
,
410 ULONG ulCount
, ITfContext
**rgContext
, ULONG
*pcFetched
)
412 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
415 TRACE("(%p)\n",This
);
417 if (rgContext
== NULL
) return E_POINTER
;
419 while (fetched
< ulCount
)
424 if (!This
->docmgr
->contextStack
[This
->index
])
427 *rgContext
= This
->docmgr
->contextStack
[This
->index
];
428 ITfContext_AddRef(*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
);
447 static HRESULT WINAPI
EnumTfContext_Reset( IEnumTfContexts
* iface
)
449 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
450 TRACE("(%p)\n",This
);
455 static HRESULT WINAPI
EnumTfContext_Clone( IEnumTfContexts
*iface
,
456 IEnumTfContexts
**ppenum
)
458 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
461 TRACE("(%p)\n",This
);
463 if (ppenum
== NULL
) return E_POINTER
;
465 res
= EnumTfContext_Constructor(This
->docmgr
, ppenum
);
468 EnumTfContext
*new_This
= impl_from_IEnumTfContexts(*ppenum
);
469 new_This
->index
= This
->index
;
474 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl
={
475 EnumTfContext_QueryInterface
,
476 EnumTfContext_AddRef
,
477 EnumTfContext_Release
,
485 static HRESULT
EnumTfContext_Constructor(DocumentMgr
*mgr
, IEnumTfContexts
**ppOut
)
489 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(EnumTfContext
));
491 return E_OUTOFMEMORY
;
493 This
->IEnumTfContexts_iface
.lpVtbl
= &IEnumTfContexts_Vtbl
;
497 *ppOut
= &This
->IEnumTfContexts_iface
;
498 TRACE("returning %p\n", *ppOut
);