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
))
83 *ppvOut
= &This
->ITfDocumentMgr_iface
;
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
)
168 for (i
= 0; i
< sizeof(This
->contextStack
)/sizeof(This
->contextStack
[0]); i
++)
169 if (This
->contextStack
[i
])
171 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
, This
->contextStack
[i
]);
172 Context_Uninitialize(This
->contextStack
[i
]);
173 ITfContext_Release(This
->contextStack
[i
]);
174 This
->contextStack
[i
] = NULL
;
177 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
184 if (This
->contextStack
[1] == NULL
) /* Cannot pop last context */
187 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[0]);
188 Context_Uninitialize(This
->contextStack
[0]);
189 ITfContext_Release(This
->contextStack
[0]);
190 This
->contextStack
[0] = This
->contextStack
[1];
191 This
->contextStack
[1] = NULL
;
193 if (This
->contextStack
[0] == NULL
)
194 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
199 static HRESULT WINAPI
DocumentMgr_GetTop(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
201 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
202 TRACE("(%p)\n",This
);
206 if (This
->contextStack
[0])
207 ITfContext_AddRef(This
->contextStack
[0]);
209 *ppic
= This
->contextStack
[0];
214 static HRESULT WINAPI
DocumentMgr_GetBase(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
216 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
219 TRACE("(%p)\n",This
);
223 if (This
->contextStack
[1])
224 tgt
= This
->contextStack
[1];
226 tgt
= This
->contextStack
[0];
229 ITfContext_AddRef(tgt
);
236 static HRESULT WINAPI
DocumentMgr_EnumContexts(ITfDocumentMgr
*iface
, IEnumTfContexts
**ppEnum
)
238 DocumentMgr
*This
= impl_from_ITfDocumentMgr(iface
);
239 TRACE("(%p) %p\n",This
,ppEnum
);
240 return EnumTfContext_Constructor(This
, ppEnum
);
243 static const ITfDocumentMgrVtbl DocumentMgrVtbl
=
245 DocumentMgr_QueryInterface
,
248 DocumentMgr_CreateContext
,
253 DocumentMgr_EnumContexts
256 static HRESULT WINAPI
DocumentMgrSource_QueryInterface(ITfSource
*iface
, REFIID iid
, LPVOID
*ppvOut
)
258 DocumentMgr
*This
= impl_from_ITfSource(iface
);
259 return ITfDocumentMgr_QueryInterface(&This
->ITfDocumentMgr_iface
, iid
, ppvOut
);
262 static ULONG WINAPI
DocumentMgrSource_AddRef(ITfSource
*iface
)
264 DocumentMgr
*This
= impl_from_ITfSource(iface
);
265 return ITfDocumentMgr_AddRef(&This
->ITfDocumentMgr_iface
);
268 static ULONG WINAPI
DocumentMgrSource_Release(ITfSource
*iface
)
270 DocumentMgr
*This
= impl_from_ITfSource(iface
);
271 return ITfDocumentMgr_Release(&This
->ITfDocumentMgr_iface
);
274 /*****************************************************
275 * ITfSource functions
276 *****************************************************/
277 static HRESULT WINAPI
DocumentMgrSource_AdviseSink(ITfSource
*iface
,
278 REFIID riid
, IUnknown
*punk
, DWORD
*pdwCookie
)
280 DocumentMgr
*This
= impl_from_ITfSource(iface
);
281 FIXME("STUB:(%p)\n",This
);
285 static HRESULT WINAPI
DocumentMgrSource_UnadviseSink(ITfSource
*iface
, DWORD pdwCookie
)
287 DocumentMgr
*This
= impl_from_ITfSource(iface
);
288 FIXME("STUB:(%p)\n",This
);
292 static const ITfSourceVtbl DocumentMgrSourceVtbl
=
294 DocumentMgrSource_QueryInterface
,
295 DocumentMgrSource_AddRef
,
296 DocumentMgrSource_Release
,
297 DocumentMgrSource_AdviseSink
,
298 DocumentMgrSource_UnadviseSink
,
301 HRESULT
DocumentMgr_Constructor(ITfThreadMgrEventSink
*ThreadMgrSink
, ITfDocumentMgr
**ppOut
)
305 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DocumentMgr
));
307 return E_OUTOFMEMORY
;
309 This
->ITfDocumentMgr_iface
.lpVtbl
= &DocumentMgrVtbl
;
310 This
->ITfSource_iface
.lpVtbl
= &DocumentMgrSourceVtbl
;
312 This
->ThreadMgrSink
= ThreadMgrSink
;
314 CompartmentMgr_Constructor((IUnknown
*)&This
->ITfDocumentMgr_iface
, &IID_IUnknown
, (IUnknown
**)&This
->CompartmentMgr
);
316 *ppOut
= &This
->ITfDocumentMgr_iface
;
317 TRACE("returning %p\n", *ppOut
);
321 /**************************************************
322 * IEnumTfContexts implementation
323 **************************************************/
324 static void EnumTfContext_Destructor(EnumTfContext
*This
)
326 TRACE("destroying %p\n", This
);
327 HeapFree(GetProcessHeap(),0,This
);
330 static HRESULT WINAPI
EnumTfContext_QueryInterface(IEnumTfContexts
*iface
, REFIID iid
, LPVOID
*ppvOut
)
332 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
335 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_IEnumTfContexts
))
337 *ppvOut
= &This
->IEnumTfContexts_iface
;
342 IEnumTfContexts_AddRef(iface
);
346 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
347 return E_NOINTERFACE
;
350 static ULONG WINAPI
EnumTfContext_AddRef(IEnumTfContexts
*iface
)
352 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
353 return InterlockedIncrement(&This
->refCount
);
356 static ULONG WINAPI
EnumTfContext_Release(IEnumTfContexts
*iface
)
358 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
361 ret
= InterlockedDecrement(&This
->refCount
);
363 EnumTfContext_Destructor(This
);
367 static HRESULT WINAPI
EnumTfContext_Next(IEnumTfContexts
*iface
,
368 ULONG ulCount
, ITfContext
**rgContext
, ULONG
*pcFetched
)
370 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
373 TRACE("(%p)\n",This
);
375 if (rgContext
== NULL
) return E_POINTER
;
377 while (fetched
< ulCount
)
382 if (!This
->docmgr
->contextStack
[This
->index
])
385 *rgContext
= This
->docmgr
->contextStack
[This
->index
];
386 ITfContext_AddRef(*rgContext
);
393 if (pcFetched
) *pcFetched
= fetched
;
394 return fetched
== ulCount
? S_OK
: S_FALSE
;
397 static HRESULT WINAPI
EnumTfContext_Skip( IEnumTfContexts
* iface
, ULONG celt
)
399 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
400 TRACE("(%p)\n",This
);
405 static HRESULT WINAPI
EnumTfContext_Reset( IEnumTfContexts
* iface
)
407 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
408 TRACE("(%p)\n",This
);
413 static HRESULT WINAPI
EnumTfContext_Clone( IEnumTfContexts
*iface
,
414 IEnumTfContexts
**ppenum
)
416 EnumTfContext
*This
= impl_from_IEnumTfContexts(iface
);
419 TRACE("(%p)\n",This
);
421 if (ppenum
== NULL
) return E_POINTER
;
423 res
= EnumTfContext_Constructor(This
->docmgr
, ppenum
);
426 EnumTfContext
*new_This
= impl_from_IEnumTfContexts(*ppenum
);
427 new_This
->index
= This
->index
;
432 static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl
={
433 EnumTfContext_QueryInterface
,
434 EnumTfContext_AddRef
,
435 EnumTfContext_Release
,
443 static HRESULT
EnumTfContext_Constructor(DocumentMgr
*mgr
, IEnumTfContexts
**ppOut
)
447 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(EnumTfContext
));
449 return E_OUTOFMEMORY
;
451 This
->IEnumTfContexts_iface
.lpVtbl
= &IEnumTfContexts_Vtbl
;
455 *ppOut
= &This
->IEnumTfContexts_iface
;
456 TRACE("returning %p\n", *ppOut
);