msctf: Implement ITfInputProcessorProfiles::IsEnabledLanguageProfile.
[wine] / dlls / msctf / documentmgr.c
1 /*
2  *  ITfDocumentMgr implementation
3  *
4  *  Copyright 2009 Aric Stewart, CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "shlwapi.h"
33 #include "winerror.h"
34 #include "objbase.h"
35
36 #include "wine/unicode.h"
37
38 #include "msctf.h"
39 #include "msctf_internal.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
42
43 typedef struct tagDocumentMgr {
44     const ITfDocumentMgrVtbl *DocumentMgrVtbl;
45     const ITfSourceVtbl *SourceVtbl;
46     LONG refCount;
47
48     ITfContext*  contextStack[2]; /* limit of 2 contexts */
49 } DocumentMgr;
50
51 static inline DocumentMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
52 {
53     return (DocumentMgr *)((char *)iface - FIELD_OFFSET(DocumentMgr,SourceVtbl));
54 }
55
56 static void DocumentMgr_Destructor(DocumentMgr *This)
57 {
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);
64 }
65
66 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
67 {
68     DocumentMgr *This = (DocumentMgr *)iface;
69     *ppvOut = NULL;
70
71     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
72     {
73         *ppvOut = This;
74     }
75     else if (IsEqualIID(iid, &IID_ITfSource))
76     {
77         *ppvOut = &This->SourceVtbl;
78     }
79
80     if (*ppvOut)
81     {
82         IUnknown_AddRef(iface);
83         return S_OK;
84     }
85
86     WARN("unsupported interface: %s\n", debugstr_guid(iid));
87     return E_NOINTERFACE;
88 }
89
90 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
91 {
92     DocumentMgr *This = (DocumentMgr *)iface;
93     return InterlockedIncrement(&This->refCount);
94 }
95
96 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
97 {
98     DocumentMgr *This = (DocumentMgr *)iface;
99     ULONG ret;
100
101     ret = InterlockedDecrement(&This->refCount);
102     if (ret == 0)
103         DocumentMgr_Destructor(This);
104     return ret;
105 }
106
107 /*****************************************************
108  * ITfDocumentMgr functions
109  *****************************************************/
110 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
111         TfClientId tidOwner,
112         DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
113         TfEditCookie *pecTextStore)
114 {
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);
118 }
119
120 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
121 {
122     DocumentMgr *This = (DocumentMgr *)iface;
123     ITfContext *check;
124
125     TRACE("(%p) %p\n",This,pic);
126
127     if (This->contextStack[1])  /* FUll */
128         return TF_E_STACKFULL;
129
130     if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
131         return E_INVALIDARG;
132
133     This->contextStack[1] = This->contextStack[0];
134     This->contextStack[0] = check;
135
136     return S_OK;
137 }
138
139 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
140 {
141     DocumentMgr *This = (DocumentMgr *)iface;
142     TRACE("(%p) 0x%x\n",This,dwFlags);
143
144     if (dwFlags == TF_POPF_ALL)
145     {
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;
151         return S_OK;
152     }
153
154     if (dwFlags)
155         return E_INVALIDARG;
156
157     if (This->contextStack[0] == NULL) /* Cannot pop last context */
158         return E_FAIL;
159
160     ITfContext_Release(This->contextStack[0]);
161     This->contextStack[0] = This->contextStack[1];
162     This->contextStack[1] = NULL;
163
164     return S_OK;
165 }
166
167 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
168 {
169     DocumentMgr *This = (DocumentMgr *)iface;
170     TRACE("(%p)\n",This);
171     if (!ppic)
172         return E_INVALIDARG;
173
174     if (This->contextStack[0])
175         ITfContext_AddRef(This->contextStack[0]);
176
177     *ppic = This->contextStack[0];
178
179     return S_OK;
180 }
181
182 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
183 {
184     DocumentMgr *This = (DocumentMgr *)iface;
185     TRACE("(%p)\n",This);
186     if (!ppic)
187         return E_INVALIDARG;
188
189     if (This->contextStack[1])
190         ITfContext_AddRef(This->contextStack[1]);
191
192     *ppic = This->contextStack[1];
193
194     return S_OK;
195 }
196
197 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
198 {
199     DocumentMgr *This = (DocumentMgr *)iface;
200     FIXME("STUB:(%p)\n",This);
201     return E_NOTIMPL;
202 }
203
204 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
205 {
206     DocumentMgr_QueryInterface,
207     DocumentMgr_AddRef,
208     DocumentMgr_Release,
209
210     DocumentMgr_CreateContext,
211     DocumentMgr_Push,
212     DocumentMgr_Pop,
213     DocumentMgr_GetTop,
214     DocumentMgr_GetBase,
215     DocumentMgr_EnumContexts
216 };
217
218
219 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
220 {
221     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
222     return DocumentMgr_QueryInterface((ITfDocumentMgr*)This, iid, *ppvOut);
223 }
224
225 static ULONG WINAPI Source_AddRef(ITfSource *iface)
226 {
227     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
228     return DocumentMgr_AddRef((ITfDocumentMgr*)This);
229 }
230
231 static ULONG WINAPI Source_Release(ITfSource *iface)
232 {
233     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
234     return DocumentMgr_Release((ITfDocumentMgr*)This);
235 }
236
237 /*****************************************************
238  * ITfSource functions
239  *****************************************************/
240 static WINAPI HRESULT DocumentMgrSource_AdviseSink(ITfSource *iface,
241         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
242 {
243     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
244     FIXME("STUB:(%p)\n",This);
245     return E_NOTIMPL;
246 }
247
248 static WINAPI HRESULT DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
249 {
250     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
251     FIXME("STUB:(%p)\n",This);
252     return E_NOTIMPL;
253 }
254
255 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
256 {
257     Source_QueryInterface,
258     Source_AddRef,
259     Source_Release,
260
261     DocumentMgrSource_AdviseSink,
262     DocumentMgrSource_UnadviseSink,
263 };
264
265 HRESULT DocumentMgr_Constructor(ITfDocumentMgr **ppOut)
266 {
267     DocumentMgr *This;
268
269     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
270     if (This == NULL)
271         return E_OUTOFMEMORY;
272
273     This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
274     This->SourceVtbl = &DocumentMgr_SourceVtbl;
275     This->refCount = 1;
276
277     TRACE("returning %p\n", This);
278     *ppOut = (ITfDocumentMgr*)This;
279     return S_OK;
280 }