msctf: Use FAILED instead of !SUCCEDED.
[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     ITfThreadMgrEventSink* ThreadMgrSink;
50 } DocumentMgr;
51
52 static inline DocumentMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
53 {
54     return (DocumentMgr *)((char *)iface - FIELD_OFFSET(DocumentMgr,SourceVtbl));
55 }
56
57 static void DocumentMgr_Destructor(DocumentMgr *This)
58 {
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);
65 }
66
67 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
68 {
69     DocumentMgr *This = (DocumentMgr *)iface;
70     *ppvOut = NULL;
71
72     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
73     {
74         *ppvOut = This;
75     }
76     else if (IsEqualIID(iid, &IID_ITfSource))
77     {
78         *ppvOut = &This->SourceVtbl;
79     }
80
81     if (*ppvOut)
82     {
83         IUnknown_AddRef(iface);
84         return S_OK;
85     }
86
87     WARN("unsupported interface: %s\n", debugstr_guid(iid));
88     return E_NOINTERFACE;
89 }
90
91 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
92 {
93     DocumentMgr *This = (DocumentMgr *)iface;
94     return InterlockedIncrement(&This->refCount);
95 }
96
97 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
98 {
99     DocumentMgr *This = (DocumentMgr *)iface;
100     ULONG ret;
101
102     ret = InterlockedDecrement(&This->refCount);
103     if (ret == 0)
104         DocumentMgr_Destructor(This);
105     return ret;
106 }
107
108 /*****************************************************
109  * ITfDocumentMgr functions
110  *****************************************************/
111 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
112         TfClientId tidOwner,
113         DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
114         TfEditCookie *pecTextStore)
115 {
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);
119 }
120
121 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
122 {
123     DocumentMgr *This = (DocumentMgr *)iface;
124     ITfContext *check;
125
126     TRACE("(%p) %p\n",This,pic);
127
128     if (This->contextStack[1])  /* FUll */
129         return TF_E_STACKFULL;
130
131     if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
132         return E_INVALIDARG;
133
134     if (This->contextStack[0] == NULL)
135         ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);
136
137     This->contextStack[1] = This->contextStack[0];
138     This->contextStack[0] = check;
139
140     ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
141     Context_Initialize(check);
142
143     return S_OK;
144 }
145
146 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
147 {
148     DocumentMgr *This = (DocumentMgr *)iface;
149     TRACE("(%p) 0x%x\n",This,dwFlags);
150
151     if (dwFlags == TF_POPF_ALL)
152     {
153         if (This->contextStack[0])
154         {
155             ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
156             ITfContext_Release(This->contextStack[0]);
157             Context_Uninitialize(This->contextStack[0]);
158         }
159         if (This->contextStack[1])
160         {
161             ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
162             ITfContext_Release(This->contextStack[1]);
163             Context_Uninitialize(This->contextStack[1]);
164         }
165         This->contextStack[0] = This->contextStack[1] = NULL;
166         ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
167         return S_OK;
168     }
169
170     if (dwFlags)
171         return E_INVALIDARG;
172
173     if (This->contextStack[1] == NULL) /* Cannot pop last context */
174         return E_FAIL;
175
176     ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
177     ITfContext_Release(This->contextStack[0]);
178     Context_Uninitialize(This->contextStack[0]);
179     This->contextStack[0] = This->contextStack[1];
180     This->contextStack[1] = NULL;
181
182     if (This->contextStack[0] == NULL)
183         ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
184
185     return S_OK;
186 }
187
188 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
189 {
190     DocumentMgr *This = (DocumentMgr *)iface;
191     TRACE("(%p)\n",This);
192     if (!ppic)
193         return E_INVALIDARG;
194
195     if (This->contextStack[0])
196         ITfContext_AddRef(This->contextStack[0]);
197
198     *ppic = This->contextStack[0];
199
200     return S_OK;
201 }
202
203 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
204 {
205     DocumentMgr *This = (DocumentMgr *)iface;
206     ITfContext *tgt;
207
208     TRACE("(%p)\n",This);
209     if (!ppic)
210         return E_INVALIDARG;
211
212     if (This->contextStack[1])
213         tgt = This->contextStack[1];
214     else
215         tgt = This->contextStack[0];
216
217     if (tgt)
218         ITfContext_AddRef(tgt);
219
220     *ppic = tgt;
221
222     return S_OK;
223 }
224
225 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
226 {
227     DocumentMgr *This = (DocumentMgr *)iface;
228     FIXME("STUB:(%p)\n",This);
229     return E_NOTIMPL;
230 }
231
232 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
233 {
234     DocumentMgr_QueryInterface,
235     DocumentMgr_AddRef,
236     DocumentMgr_Release,
237
238     DocumentMgr_CreateContext,
239     DocumentMgr_Push,
240     DocumentMgr_Pop,
241     DocumentMgr_GetTop,
242     DocumentMgr_GetBase,
243     DocumentMgr_EnumContexts
244 };
245
246
247 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
248 {
249     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
250     return DocumentMgr_QueryInterface((ITfDocumentMgr*)This, iid, *ppvOut);
251 }
252
253 static ULONG WINAPI Source_AddRef(ITfSource *iface)
254 {
255     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
256     return DocumentMgr_AddRef((ITfDocumentMgr*)This);
257 }
258
259 static ULONG WINAPI Source_Release(ITfSource *iface)
260 {
261     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
262     return DocumentMgr_Release((ITfDocumentMgr*)This);
263 }
264
265 /*****************************************************
266  * ITfSource functions
267  *****************************************************/
268 static WINAPI HRESULT DocumentMgrSource_AdviseSink(ITfSource *iface,
269         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
270 {
271     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
272     FIXME("STUB:(%p)\n",This);
273     return E_NOTIMPL;
274 }
275
276 static WINAPI HRESULT DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
277 {
278     DocumentMgr *This = impl_from_ITfSourceVtbl(iface);
279     FIXME("STUB:(%p)\n",This);
280     return E_NOTIMPL;
281 }
282
283 static const ITfSourceVtbl DocumentMgr_SourceVtbl =
284 {
285     Source_QueryInterface,
286     Source_AddRef,
287     Source_Release,
288
289     DocumentMgrSource_AdviseSink,
290     DocumentMgrSource_UnadviseSink,
291 };
292
293 HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
294 {
295     DocumentMgr *This;
296
297     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
298     if (This == NULL)
299         return E_OUTOFMEMORY;
300
301     This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
302     This->SourceVtbl = &DocumentMgr_SourceVtbl;
303     This->refCount = 1;
304     This->ThreadMgrSink = ThreadMgrSink;
305
306     TRACE("returning %p\n", This);
307     *ppOut = (ITfDocumentMgr*)This;
308     return S_OK;
309 }