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 "msctf_internal.h"
23 typedef struct tagDocumentMgr
{
24 ITfDocumentMgr ITfDocumentMgr_iface
;
25 ITfSource ITfSource_iface
;
29 ITfCompartmentMgr
*CompartmentMgr
;
31 ITfContext
* contextStack
[2]; /* limit of 2 contexts */
32 ITfThreadMgrEventSink
* ThreadMgrSink
;
35 typedef struct tagEnumTfContext
{
36 IEnumTfContexts IEnumTfContexts_iface
;
43 static HRESULT
EnumTfContext_Constructor(DocumentMgr
* mgr
, IEnumTfContexts
**ppOut
);
45 static inline DocumentMgr
*impl_from_ITfDocumentMgr(ITfDocumentMgr
*iface
)
47 return CONTAINING_RECORD(iface
, DocumentMgr
, ITfDocumentMgr_iface
);
50 static inline DocumentMgr
*impl_from_ITfSource(ITfSource
*iface
)
52 return CONTAINING_RECORD(iface
, DocumentMgr
, ITfSource_iface
);
55 static inline EnumTfContext
*impl_from_IEnumTfContexts(IEnumTfContexts
*iface
)
57 return CONTAINING_RECORD(iface
, EnumTfContext
, IEnumTfContexts_iface
);
60 static void DocumentMgr_Destructor(DocumentMgr
*This
)
63 TRACE("destroying %p\n", This
);
66 ThreadMgr_OnDocumentMgrDestruction(tm
, &This
->ITfDocumentMgr_iface
);
68 if (This
->contextStack
[0])
69 ITfContext_Release(This
->contextStack
[0]);
70 if (This
->contextStack
[1])
71 ITfContext_Release(This
->contextStack
[1]);
72 CompartmentMgr_Destructor(This
->CompartmentMgr
);
73 HeapFree(GetProcessHeap(),0,This
);
76 static HRESULT WINAPI
DocumentMgr_QueryInterface(ITfDocumentMgr
*iface
, REFIID iid
, LPVOID
*ppvOut
)
78 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
81 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_ITfDocumentMgr
))
85 else if (IsEqualIID(iid
, &IID_ITfSource
))
87 *ppvOut
= &This
->ITfSource_iface
;
89 else if (IsEqualIID(iid
, &IID_ITfCompartmentMgr
))
91 *ppvOut
= This
->CompartmentMgr
;
96 ITfDocumentMgr_AddRef(iface
);
100 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
101 return E_NOINTERFACE
;
104 static ULONG WINAPI
DocumentMgr_AddRef(ITfDocumentMgr
*iface
)
106 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
107 return InterlockedIncrement(&This
->refCount
);
110 static ULONG WINAPI
DocumentMgr_Release(ITfDocumentMgr
*iface
)
112 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
115 ret
= InterlockedDecrement(&This
->refCount
);
117 DocumentMgr_Destructor(This
);
121 /*****************************************************
122 * ITfDocumentMgr functions
123 *****************************************************/
124 static HRESULT WINAPI
DocumentMgr_CreateContext(ITfDocumentMgr
*iface
,
126 DWORD dwFlags
, IUnknown
*punk
, ITfContext
**ppic
,
127 TfEditCookie
*pecTextStore
)
129 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
130 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This
,tidOwner
,dwFlags
,punk
,ppic
,pecTextStore
);
131 return Context_Constructor(tidOwner
, punk
, iface
, ppic
, pecTextStore
);
134 static HRESULT WINAPI
DocumentMgr_Push(ITfDocumentMgr
*iface
, ITfContext
*pic
)
136 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
139 TRACE("(%p) %p\n",This
,pic
);
141 if (This
->contextStack
[1]) /* FUll */
142 return TF_E_STACKFULL
;
144 if (!pic
|| FAILED(ITfContext_QueryInterface(pic
,&IID_ITfContext
,(LPVOID
*) &check
)))
147 if (This
->contextStack
[0] == NULL
)
148 ITfThreadMgrEventSink_OnInitDocumentMgr(This
->ThreadMgrSink
,iface
);
150 This
->contextStack
[1] = This
->contextStack
[0];
151 This
->contextStack
[0] = check
;
153 Context_Initialize(check
, iface
);
154 ITfThreadMgrEventSink_OnPushContext(This
->ThreadMgrSink
,check
);
159 static HRESULT WINAPI
DocumentMgr_Pop(ITfDocumentMgr
*iface
, DWORD dwFlags
)
161 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
162 TRACE("(%p) 0x%x\n",This
,dwFlags
);
164 if (dwFlags
== TF_POPF_ALL
)
166 if (This
->contextStack
[0])
168 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[0]);
169 Context_Uninitialize(This
->contextStack
[0]);
170 ITfContext_Release(This
->contextStack
[0]);
172 if (This
->contextStack
[1])
174 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[1]);
175 Context_Uninitialize(This
->contextStack
[1]);
176 ITfContext_Release(This
->contextStack
[1]);
178 This
->contextStack
[0] = This
->contextStack
[1] = NULL
;
179 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
186 if (This
->contextStack
[1] == NULL
) /* Cannot pop last context */
189 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[0]);
190 Context_Uninitialize(This
->contextStack
[0]);
191 ITfContext_Release(This
->contextStack
[0]);
192 This
->contextStack
[0] = This
->contextStack
[1];
193 This
->contextStack
[1] = NULL
;
195 if (This
->contextStack
[0] == NULL
)
196 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
201 static HRESULT WINAPI
DocumentMgr_GetTop(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
203 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
204 TRACE("(%p)\n",This
);
208 if (This
->contextStack
[0])
209 ITfContext_AddRef(This
->contextStack
[0]);
211 *ppic
= This
->contextStack
[0];
216 static HRESULT WINAPI
DocumentMgr_GetBase(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
218 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
221 TRACE("(%p)\n",This
);
225 if (This
->contextStack
[1])
226 tgt
= This
->contextStack
[1];
228 tgt
= This
->contextStack
[0];
231 ITfContext_AddRef(tgt
);
238 static HRESULT WINAPI
DocumentMgr_EnumContexts(ITfDocumentMgr
*iface
, IEnumTfContexts
**ppEnum
)
240 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
241 TRACE("(%p) %p\n",This
,ppEnum
);
242 return EnumTfContext_Constructor(This
, ppEnum
);
245 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl
=
247 DocumentMgr_QueryInterface
,
251 DocumentMgr_CreateContext
,
256 DocumentMgr_EnumContexts
260 static HRESULT WINAPI
Source_QueryInterface(ITfSource
*iface
, REFIID iid
, LPVOID
*ppvOut
)
262 DocumentMgr
*This
= impl_from_ITfSource(iface
);
263 return DocumentMgr_QueryInterface(&This
->ITfDocumentMgr_iface
, iid
, *ppvOut
);
266 static ULONG WINAPI
Source_AddRef(ITfSource
*iface
)
268 DocumentMgr
*This
= impl_from_ITfSource(iface
);
269 return DocumentMgr_AddRef(&This
->ITfDocumentMgr_iface
);
272 static ULONG WINAPI
Source_Release(ITfSource
*iface
)
274 DocumentMgr
*This
= impl_from_ITfSource(iface
);
275 return DocumentMgr_Release(&This
->ITfDocumentMgr_iface
);
278 /*****************************************************
279 * ITfSource functions
280 *****************************************************/
281 static HRESULT WINAPI
DocumentMgrSource_AdviseSink(ITfSource
*iface
,
282 REFIID riid
, IUnknown
*punk
, DWORD
*pdwCookie
)
284 DocumentMgr
*This
= impl_from_ITfSource(iface
);
285 FIXME("STUB:(%p)\n",This
);
289 static HRESULT WINAPI
DocumentMgrSource_UnadviseSink(ITfSource
*iface
, DWORD pdwCookie
)
291 DocumentMgr
*This
= impl_from_ITfSource(iface
);
292 FIXME("STUB:(%p)\n",This
);
296 static const ITfSourceVtbl DocumentMgr_SourceVtbl
=
298 Source_QueryInterface
,
302 DocumentMgrSource_AdviseSink
,
303 DocumentMgrSource_UnadviseSink
,
306 HRESULT
DocumentMgr_Constructor(ITfThreadMgrEventSink
*ThreadMgrSink
, ITfDocumentMgr
**ppOut
)
310 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DocumentMgr
));
312 return E_OUTOFMEMORY
;
314 This
->ITfDocumentMgr_iface
.lpVtbl
= &DocumentMgr_DocumentMgrVtbl
;
315 This
->ITfSource_iface
.lpVtbl
= &DocumentMgr_SourceVtbl
;
317 This
->ThreadMgrSink
= ThreadMgrSink
;
319 CompartmentMgr_Constructor((IUnknown
*)This
, &IID_IUnknown
, (IUnknown
**)&This
->CompartmentMgr
);
321 TRACE("returning %p\n", This
);
322 *ppOut
= &This
->ITfDocumentMgr_iface
;
326 /**************************************************
327 * IEnumTfContexts implementation
328 **************************************************/
329 static void EnumTfContext_Destructor(EnumTfContext
*This
)
331 TRACE("destroying %p\n", This
);
332 HeapFree(GetProcessHeap(),0,This
);
335 static HRESULT WINAPI
EnumTfContext_QueryInterface(IEnumTfContexts
*iface
, REFIID iid
, LPVOID
*ppvOut
)
337 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
340 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_IEnumTfContexts
))
347 IEnumTfContexts_AddRef(iface
);
351 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
352 return E_NOINTERFACE
;
355 static ULONG WINAPI
EnumTfContext_AddRef(IEnumTfContexts
*iface
)
357 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
358 return InterlockedIncrement(&This
->refCount
);
361 static ULONG WINAPI
EnumTfContext_Release(IEnumTfContexts
*iface
)
363 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
366 ret
= InterlockedDecrement(&This
->refCount
);
368 EnumTfContext_Destructor(This
);
372 static HRESULT WINAPI
EnumTfContext_Next(IEnumTfContexts
*iface
,
373 ULONG ulCount
, ITfContext
**rgContext
, ULONG
*pcFetched
)
375 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
378 TRACE("(%p)\n",This
);
380 if (rgContext
== NULL
) return E_POINTER
;
382 while (fetched
< ulCount
)
387 if (!This
->docmgr
->contextStack
[This
->index
])
390 *rgContext
= This
->docmgr
->contextStack
[This
->index
];
391 ITfContext_AddRef(*rgContext
);
398 if (pcFetched
) *pcFetched
= fetched
;
399 return fetched
== ulCount
? S_OK
: S_FALSE
;
402 static HRESULT WINAPI
EnumTfContext_Skip( IEnumTfContexts
* iface
, ULONG celt
)
404 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
405 TRACE("(%p)\n",This
);
410 static HRESULT WINAPI
EnumTfContext_Reset( IEnumTfContexts
* iface
)
412 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
413 TRACE("(%p)\n",This
);
418 static HRESULT WINAPI
EnumTfContext_Clone( IEnumTfContexts
*iface
,
419 IEnumTfContexts
**ppenum
)
421 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
424 TRACE("(%p)\n",This
);
426 if (ppenum
== NULL
) return E_POINTER
;
428 res
= EnumTfContext_Constructor(This
->docmgr
, ppenum
);
431 EnumTfContext
*new_This
= impl_from_IEnumTfContexts(*ppenum
);
432 new_This
->index
= This
->index
;
437 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl
={
438 EnumTfContext_QueryInterface
,
439 EnumTfContext_AddRef
,
440 EnumTfContext_Release
,
448 static HRESULT
EnumTfContext_Constructor(DocumentMgr
*mgr
, IEnumTfContexts
**ppOut
)
452 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(EnumTfContext
));
454 return E_OUTOFMEMORY
;
456 This
->IEnumTfContexts_iface
.lpVtbl
= &IEnumTfContexts_Vtbl
;
460 TRACE("returning %p\n", This
);
461 *ppOut
= &This
->IEnumTfContexts_iface
;