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 const ITfDocumentMgrVtbl
*DocumentMgrVtbl
;
45 const ITfSourceVtbl
*SourceVtbl
;
48 ITfContext
* contextStack
[2]; /* limit of 2 contexts */
49 ITfThreadMgrEventSink
* ThreadMgrSink
;
52 static inline DocumentMgr
*impl_from_ITfSourceVtbl(ITfSource
*iface
)
54 return (DocumentMgr
*)((char *)iface
- FIELD_OFFSET(DocumentMgr
,SourceVtbl
));
57 static void DocumentMgr_Destructor(DocumentMgr
*This
)
59 TRACE("destroying %p\n", This
);
60 if (This
->contextStack
[0])
61 ITfContext_Release(This
->contextStack
[0]);
62 if (This
->contextStack
[1])
63 ITfContext_Release(This
->contextStack
[1]);
64 HeapFree(GetProcessHeap(),0,This
);
67 static HRESULT WINAPI
DocumentMgr_QueryInterface(ITfDocumentMgr
*iface
, REFIID iid
, LPVOID
*ppvOut
)
69 DocumentMgr
*This
= (DocumentMgr
*)iface
;
72 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_ITfDocumentMgr
))
76 else if (IsEqualIID(iid
, &IID_ITfSource
))
78 *ppvOut
= &This
->SourceVtbl
;
83 IUnknown_AddRef(iface
);
87 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
91 static ULONG WINAPI
DocumentMgr_AddRef(ITfDocumentMgr
*iface
)
93 DocumentMgr
*This
= (DocumentMgr
*)iface
;
94 return InterlockedIncrement(&This
->refCount
);
97 static ULONG WINAPI
DocumentMgr_Release(ITfDocumentMgr
*iface
)
99 DocumentMgr
*This
= (DocumentMgr
*)iface
;
102 ret
= InterlockedDecrement(&This
->refCount
);
104 DocumentMgr_Destructor(This
);
108 /*****************************************************
109 * ITfDocumentMgr functions
110 *****************************************************/
111 static HRESULT WINAPI
DocumentMgr_CreateContext(ITfDocumentMgr
*iface
,
113 DWORD dwFlags
, IUnknown
*punk
, ITfContext
**ppic
,
114 TfEditCookie
*pecTextStore
)
116 DocumentMgr
*This
= (DocumentMgr
*)iface
;
117 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This
,tidOwner
,dwFlags
,punk
,ppic
,pecTextStore
);
118 return Context_Constructor(tidOwner
, punk
, ppic
, pecTextStore
);
121 static HRESULT WINAPI
DocumentMgr_Push(ITfDocumentMgr
*iface
, ITfContext
*pic
)
123 DocumentMgr
*This
= (DocumentMgr
*)iface
;
126 TRACE("(%p) %p\n",This
,pic
);
128 if (This
->contextStack
[1]) /* FUll */
129 return TF_E_STACKFULL
;
131 if (!pic
|| FAILED(IUnknown_QueryInterface(pic
,&IID_ITfContext
,(LPVOID
*) &check
)))
134 if (This
->contextStack
[0] == NULL
)
135 ITfThreadMgrEventSink_OnInitDocumentMgr(This
->ThreadMgrSink
,iface
);
137 This
->contextStack
[1] = This
->contextStack
[0];
138 This
->contextStack
[0] = check
;
140 ITfThreadMgrEventSink_OnPushContext(This
->ThreadMgrSink
,check
);
145 static HRESULT WINAPI
DocumentMgr_Pop(ITfDocumentMgr
*iface
, DWORD dwFlags
)
147 DocumentMgr
*This
= (DocumentMgr
*)iface
;
148 TRACE("(%p) 0x%x\n",This
,dwFlags
);
150 if (dwFlags
== TF_POPF_ALL
)
152 if (This
->contextStack
[0])
154 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[0]);
155 ITfContext_Release(This
->contextStack
[0]);
157 if (This
->contextStack
[1])
159 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[1]);
160 ITfContext_Release(This
->contextStack
[1]);
162 This
->contextStack
[0] = This
->contextStack
[1] = NULL
;
163 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
170 if (This
->contextStack
[1] == NULL
) /* Cannot pop last context */
173 ITfThreadMgrEventSink_OnPopContext(This
->ThreadMgrSink
,This
->contextStack
[0]);
174 ITfContext_Release(This
->contextStack
[0]);
175 This
->contextStack
[0] = This
->contextStack
[1];
176 This
->contextStack
[1] = NULL
;
178 if (This
->contextStack
[0] == NULL
)
179 ITfThreadMgrEventSink_OnUninitDocumentMgr(This
->ThreadMgrSink
, iface
);
184 static HRESULT WINAPI
DocumentMgr_GetTop(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
186 DocumentMgr
*This
= (DocumentMgr
*)iface
;
187 TRACE("(%p)\n",This
);
191 if (This
->contextStack
[0])
192 ITfContext_AddRef(This
->contextStack
[0]);
194 *ppic
= This
->contextStack
[0];
199 static HRESULT WINAPI
DocumentMgr_GetBase(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
201 DocumentMgr
*This
= (DocumentMgr
*)iface
;
204 TRACE("(%p)\n",This
);
208 if (This
->contextStack
[1])
209 tgt
= This
->contextStack
[1];
211 tgt
= This
->contextStack
[0];
214 ITfContext_AddRef(tgt
);
221 static HRESULT WINAPI
DocumentMgr_EnumContexts(ITfDocumentMgr
*iface
, IEnumTfContexts
**ppEnum
)
223 DocumentMgr
*This
= (DocumentMgr
*)iface
;
224 FIXME("STUB:(%p)\n",This
);
228 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl
=
230 DocumentMgr_QueryInterface
,
234 DocumentMgr_CreateContext
,
239 DocumentMgr_EnumContexts
243 static HRESULT WINAPI
Source_QueryInterface(ITfSource
*iface
, REFIID iid
, LPVOID
*ppvOut
)
245 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
246 return DocumentMgr_QueryInterface((ITfDocumentMgr
*)This
, iid
, *ppvOut
);
249 static ULONG WINAPI
Source_AddRef(ITfSource
*iface
)
251 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
252 return DocumentMgr_AddRef((ITfDocumentMgr
*)This
);
255 static ULONG WINAPI
Source_Release(ITfSource
*iface
)
257 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
258 return DocumentMgr_Release((ITfDocumentMgr
*)This
);
261 /*****************************************************
262 * ITfSource functions
263 *****************************************************/
264 static WINAPI HRESULT
DocumentMgrSource_AdviseSink(ITfSource
*iface
,
265 REFIID riid
, IUnknown
*punk
, DWORD
*pdwCookie
)
267 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
268 FIXME("STUB:(%p)\n",This
);
272 static WINAPI HRESULT
DocumentMgrSource_UnadviseSink(ITfSource
*iface
, DWORD pdwCookie
)
274 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
275 FIXME("STUB:(%p)\n",This
);
279 static const ITfSourceVtbl DocumentMgr_SourceVtbl
=
281 Source_QueryInterface
,
285 DocumentMgrSource_AdviseSink
,
286 DocumentMgrSource_UnadviseSink
,
289 HRESULT
DocumentMgr_Constructor(ITfThreadMgrEventSink
*ThreadMgrSink
, ITfDocumentMgr
**ppOut
)
293 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DocumentMgr
));
295 return E_OUTOFMEMORY
;
297 This
->DocumentMgrVtbl
= &DocumentMgr_DocumentMgrVtbl
;
298 This
->SourceVtbl
= &DocumentMgr_SourceVtbl
;
300 This
->ThreadMgrSink
= ThreadMgrSink
;
302 TRACE("returning %p\n", This
);
303 *ppOut
= (ITfDocumentMgr
*)This
;