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