msctf: Add ITfSource interface to ThreadMgr.
[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     LONG refCount;
46
47     ITfContext*  contextStack[2]; /* limit of 2 contexts */
48 } DocumentMgr;
49
50 static void DocumentMgr_Destructor(DocumentMgr *This)
51 {
52     TRACE("destroying %p\n", This);
53     if (This->contextStack[0])
54         ITfContext_Release(This->contextStack[0]);
55     if (This->contextStack[1])
56         ITfContext_Release(This->contextStack[1]);
57     HeapFree(GetProcessHeap(),0,This);
58 }
59
60 static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
61 {
62     DocumentMgr *This = (DocumentMgr *)iface;
63     *ppvOut = NULL;
64
65     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
66     {
67         *ppvOut = This;
68     }
69
70     if (*ppvOut)
71     {
72         IUnknown_AddRef(iface);
73         return S_OK;
74     }
75
76     WARN("unsupported interface: %s\n", debugstr_guid(iid));
77     return E_NOINTERFACE;
78 }
79
80 static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
81 {
82     DocumentMgr *This = (DocumentMgr *)iface;
83     return InterlockedIncrement(&This->refCount);
84 }
85
86 static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
87 {
88     DocumentMgr *This = (DocumentMgr *)iface;
89     ULONG ret;
90
91     ret = InterlockedDecrement(&This->refCount);
92     if (ret == 0)
93         DocumentMgr_Destructor(This);
94     return ret;
95 }
96
97 /*****************************************************
98  * ITfDocumentMgr functions
99  *****************************************************/
100 static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
101         TfClientId tidOwner,
102         DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
103         TfEditCookie *pecTextStore)
104 {
105     DocumentMgr *This = (DocumentMgr *)iface;
106     TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
107     return Context_Constructor(tidOwner, punk, ppic, pecTextStore);
108 }
109
110 static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
111 {
112     DocumentMgr *This = (DocumentMgr *)iface;
113     ITfContext *check;
114
115     TRACE("(%p) %p\n",This,pic);
116
117     if (This->contextStack[1])  /* FUll */
118         return TF_E_STACKFULL;
119
120     if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
121         return E_INVALIDARG;
122
123     This->contextStack[1] = This->contextStack[0];
124     This->contextStack[0] = check;
125
126     return S_OK;
127 }
128
129 static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
130 {
131     DocumentMgr *This = (DocumentMgr *)iface;
132     TRACE("(%p) 0x%x\n",This,dwFlags);
133
134     if (dwFlags == TF_POPF_ALL)
135     {
136         if (This->contextStack[0])
137             ITfContext_Release(This->contextStack[0]);
138         if (This->contextStack[1])
139             ITfContext_Release(This->contextStack[1]);
140         This->contextStack[0] = This->contextStack[1] = NULL;
141         return S_OK;
142     }
143
144     if (dwFlags)
145         return E_INVALIDARG;
146
147     if (This->contextStack[0] == NULL) /* Cannot pop last context */
148         return E_FAIL;
149
150     ITfContext_Release(This->contextStack[0]);
151     This->contextStack[0] = This->contextStack[1];
152     This->contextStack[1] = NULL;
153
154     return S_OK;
155 }
156
157 static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
158 {
159     DocumentMgr *This = (DocumentMgr *)iface;
160     TRACE("(%p)\n",This);
161     if (!ppic)
162         return E_INVALIDARG;
163
164     if (This->contextStack[0])
165         ITfContext_AddRef(This->contextStack[0]);
166
167     *ppic = This->contextStack[0];
168
169     return S_OK;
170 }
171
172 static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
173 {
174     DocumentMgr *This = (DocumentMgr *)iface;
175     TRACE("(%p)\n",This);
176     if (!ppic)
177         return E_INVALIDARG;
178
179     if (This->contextStack[1])
180         ITfContext_AddRef(This->contextStack[1]);
181
182     *ppic = This->contextStack[1];
183
184     return S_OK;
185 }
186
187 static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
188 {
189     DocumentMgr *This = (DocumentMgr *)iface;
190     FIXME("STUB:(%p)\n",This);
191     return E_NOTIMPL;
192 }
193
194 static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
195 {
196     DocumentMgr_QueryInterface,
197     DocumentMgr_AddRef,
198     DocumentMgr_Release,
199
200     DocumentMgr_CreateContext,
201     DocumentMgr_Push,
202     DocumentMgr_Pop,
203     DocumentMgr_GetTop,
204     DocumentMgr_GetBase,
205     DocumentMgr_EnumContexts
206 };
207
208 HRESULT DocumentMgr_Constructor(ITfDocumentMgr **ppOut)
209 {
210     DocumentMgr *This;
211
212     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
213     if (This == NULL)
214         return E_OUTOFMEMORY;
215
216     This->DocumentMgrVtbl= &DocumentMgr_DocumentMgrVtbl;
217     This->refCount = 1;
218
219     TRACE("returning %p\n", This);
220     *ppOut = (ITfDocumentMgr*)This;
221     return S_OK;
222 }