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 */
51 static inline DocumentMgr
*impl_from_ITfSourceVtbl(ITfSource
*iface
)
53 return (DocumentMgr
*)((char *)iface
- FIELD_OFFSET(DocumentMgr
,SourceVtbl
));
56 static void DocumentMgr_Destructor(DocumentMgr
*This
)
58 TRACE("destroying %p\n", This
);
59 if (This
->contextStack
[0])
60 ITfContext_Release(This
->contextStack
[0]);
61 if (This
->contextStack
[1])
62 ITfContext_Release(This
->contextStack
[1]);
63 HeapFree(GetProcessHeap(),0,This
);
66 static HRESULT WINAPI
DocumentMgr_QueryInterface(ITfDocumentMgr
*iface
, REFIID iid
, LPVOID
*ppvOut
)
68 DocumentMgr
*This
= (DocumentMgr
*)iface
;
71 if (IsEqualIID(iid
, &IID_IUnknown
) || IsEqualIID(iid
, &IID_ITfDocumentMgr
))
75 else if (IsEqualIID(iid
, &IID_ITfSource
))
77 *ppvOut
= &This
->SourceVtbl
;
82 IUnknown_AddRef(iface
);
86 WARN("unsupported interface: %s\n", debugstr_guid(iid
));
90 static ULONG WINAPI
DocumentMgr_AddRef(ITfDocumentMgr
*iface
)
92 DocumentMgr
*This
= (DocumentMgr
*)iface
;
93 return InterlockedIncrement(&This
->refCount
);
96 static ULONG WINAPI
DocumentMgr_Release(ITfDocumentMgr
*iface
)
98 DocumentMgr
*This
= (DocumentMgr
*)iface
;
101 ret
= InterlockedDecrement(&This
->refCount
);
103 DocumentMgr_Destructor(This
);
107 /*****************************************************
108 * ITfDocumentMgr functions
109 *****************************************************/
110 static HRESULT WINAPI
DocumentMgr_CreateContext(ITfDocumentMgr
*iface
,
112 DWORD dwFlags
, IUnknown
*punk
, ITfContext
**ppic
,
113 TfEditCookie
*pecTextStore
)
115 DocumentMgr
*This
= (DocumentMgr
*)iface
;
116 TRACE("(%p) 0x%x 0x%x %p %p %p\n",This
,tidOwner
,dwFlags
,punk
,ppic
,pecTextStore
);
117 return Context_Constructor(tidOwner
, punk
, ppic
, pecTextStore
);
120 static HRESULT WINAPI
DocumentMgr_Push(ITfDocumentMgr
*iface
, ITfContext
*pic
)
122 DocumentMgr
*This
= (DocumentMgr
*)iface
;
125 TRACE("(%p) %p\n",This
,pic
);
127 if (This
->contextStack
[1]) /* FUll */
128 return TF_E_STACKFULL
;
130 if (!pic
|| FAILED(IUnknown_QueryInterface(pic
,&IID_ITfContext
,(LPVOID
*) &check
)))
133 This
->contextStack
[1] = This
->contextStack
[0];
134 This
->contextStack
[0] = check
;
139 static HRESULT WINAPI
DocumentMgr_Pop(ITfDocumentMgr
*iface
, DWORD dwFlags
)
141 DocumentMgr
*This
= (DocumentMgr
*)iface
;
142 TRACE("(%p) 0x%x\n",This
,dwFlags
);
144 if (dwFlags
== TF_POPF_ALL
)
146 if (This
->contextStack
[0])
147 ITfContext_Release(This
->contextStack
[0]);
148 if (This
->contextStack
[1])
149 ITfContext_Release(This
->contextStack
[1]);
150 This
->contextStack
[0] = This
->contextStack
[1] = NULL
;
157 if (This
->contextStack
[0] == NULL
) /* Cannot pop last context */
160 ITfContext_Release(This
->contextStack
[0]);
161 This
->contextStack
[0] = This
->contextStack
[1];
162 This
->contextStack
[1] = NULL
;
167 static HRESULT WINAPI
DocumentMgr_GetTop(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
169 DocumentMgr
*This
= (DocumentMgr
*)iface
;
170 TRACE("(%p)\n",This
);
174 if (This
->contextStack
[0])
175 ITfContext_AddRef(This
->contextStack
[0]);
177 *ppic
= This
->contextStack
[0];
182 static HRESULT WINAPI
DocumentMgr_GetBase(ITfDocumentMgr
*iface
, ITfContext
**ppic
)
184 DocumentMgr
*This
= (DocumentMgr
*)iface
;
185 TRACE("(%p)\n",This
);
189 if (This
->contextStack
[1])
190 ITfContext_AddRef(This
->contextStack
[1]);
192 *ppic
= This
->contextStack
[1];
197 static HRESULT WINAPI
DocumentMgr_EnumContexts(ITfDocumentMgr
*iface
, IEnumTfContexts
**ppEnum
)
199 DocumentMgr
*This
= (DocumentMgr
*)iface
;
200 FIXME("STUB:(%p)\n",This
);
204 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl
=
206 DocumentMgr_QueryInterface
,
210 DocumentMgr_CreateContext
,
215 DocumentMgr_EnumContexts
219 static HRESULT WINAPI
Source_QueryInterface(ITfSource
*iface
, REFIID iid
, LPVOID
*ppvOut
)
221 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
222 return DocumentMgr_QueryInterface((ITfDocumentMgr
*)This
, iid
, *ppvOut
);
225 static ULONG WINAPI
Source_AddRef(ITfSource
*iface
)
227 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
228 return DocumentMgr_AddRef((ITfDocumentMgr
*)This
);
231 static ULONG WINAPI
Source_Release(ITfSource
*iface
)
233 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
234 return DocumentMgr_Release((ITfDocumentMgr
*)This
);
237 /*****************************************************
238 * ITfSource functions
239 *****************************************************/
240 static WINAPI HRESULT
DocumentMgrSource_AdviseSink(ITfSource
*iface
,
241 REFIID riid
, IUnknown
*punk
, DWORD
*pdwCookie
)
243 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
244 FIXME("STUB:(%p)\n",This
);
248 static WINAPI HRESULT
DocumentMgrSource_UnadviseSink(ITfSource
*iface
, DWORD pdwCookie
)
250 DocumentMgr
*This
= impl_from_ITfSourceVtbl(iface
);
251 FIXME("STUB:(%p)\n",This
);
255 static const ITfSourceVtbl DocumentMgr_SourceVtbl
=
257 Source_QueryInterface
,
261 DocumentMgrSource_AdviseSink
,
262 DocumentMgrSource_UnadviseSink
,
265 HRESULT
DocumentMgr_Constructor(ITfDocumentMgr
**ppOut
)
269 This
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(DocumentMgr
));
271 return E_OUTOFMEMORY
;
273 This
->DocumentMgrVtbl
= &DocumentMgr_DocumentMgrVtbl
;
274 This
->SourceVtbl
= &DocumentMgr_SourceVtbl
;
277 TRACE("returning %p\n", This
);
278 *ppOut
= (ITfDocumentMgr
*)This
;