msctf: Only 1 ITfThreadMgr is created per thread.
[wine] / dlls / msctf / threadmgr.c
1 /*
2  *  ITfThreadMgr implementation
3  *
4  *  Copyright 2008 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 tagACLMulti {
44     const ITfThreadMgrVtbl *ThreadMgrVtbl;
45     LONG refCount;
46
47     ITfDocumentMgr *focus;
48 } ThreadMgr;
49
50 static void ThreadMgr_Destructor(ThreadMgr *This)
51 {
52     TlsSetValue(tlsIndex,NULL);
53     TRACE("destroying %p\n", This);
54     if (This->focus)
55         ITfDocumentMgr_Release(This->focus);
56     HeapFree(GetProcessHeap(),0,This);
57 }
58
59 static HRESULT WINAPI ThreadMgr_QueryInterface(ITfThreadMgr *iface, REFIID iid, LPVOID *ppvOut)
60 {
61     ThreadMgr *This = (ThreadMgr *)iface;
62     *ppvOut = NULL;
63
64     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgr))
65     {
66         *ppvOut = This;
67     }
68
69     if (*ppvOut)
70     {
71         IUnknown_AddRef(iface);
72         return S_OK;
73     }
74
75     WARN("unsupported interface: %s\n", debugstr_guid(iid));
76     return E_NOINTERFACE;
77 }
78
79 static ULONG WINAPI ThreadMgr_AddRef(ITfThreadMgr *iface)
80 {
81     ThreadMgr *This = (ThreadMgr *)iface;
82     return InterlockedIncrement(&This->refCount);
83 }
84
85 static ULONG WINAPI ThreadMgr_Release(ITfThreadMgr *iface)
86 {
87     ThreadMgr *This = (ThreadMgr *)iface;
88     ULONG ret;
89
90     ret = InterlockedDecrement(&This->refCount);
91     if (ret == 0)
92         ThreadMgr_Destructor(This);
93     return ret;
94 }
95
96 /*****************************************************
97  * ITfThreadMgr functions
98  *****************************************************/
99
100 static HRESULT WINAPI ThreadMgr_fnActivate( ITfThreadMgr* iface, TfClientId *ptid)
101 {
102     ThreadMgr *This = (ThreadMgr *)iface;
103     FIXME("STUB:(%p)\n",This);
104     return E_NOTIMPL;
105 }
106
107 static HRESULT WINAPI ThreadMgr_fnDeactivate( ITfThreadMgr* iface)
108 {
109     ThreadMgr *This = (ThreadMgr *)iface;
110     FIXME("STUB:(%p)\n",This);
111     return E_NOTIMPL;
112 }
113
114 static HRESULT WINAPI ThreadMgr_CreateDocumentMgr( ITfThreadMgr* iface, ITfDocumentMgr
115 **ppdim)
116 {
117     TRACE("(%p)\n",iface);
118     return DocumentMgr_Constructor(ppdim);
119 }
120
121 static HRESULT WINAPI ThreadMgr_EnumDocumentMgrs( ITfThreadMgr* iface, IEnumTfDocumentMgrs
122 **ppEnum)
123 {
124     ThreadMgr *This = (ThreadMgr *)iface;
125     FIXME("STUB:(%p)\n",This);
126     return E_NOTIMPL;
127 }
128
129 static HRESULT WINAPI ThreadMgr_GetFocus( ITfThreadMgr* iface, ITfDocumentMgr
130 **ppdimFocus)
131 {
132     ThreadMgr *This = (ThreadMgr *)iface;
133     TRACE("(%p)\n",This);
134
135     if (!ppdimFocus)
136         return E_INVALIDARG;
137
138     *ppdimFocus = This->focus;
139
140     TRACE("->%p\n",This->focus);
141
142     if (This->focus == NULL)
143         return S_FALSE;
144
145     ITfDocumentMgr_AddRef(This->focus);
146
147     return S_OK;
148 }
149
150 static HRESULT WINAPI ThreadMgr_SetFocus( ITfThreadMgr* iface, ITfDocumentMgr *pdimFocus)
151 {
152     ITfDocumentMgr *check;
153     ThreadMgr *This = (ThreadMgr *)iface;
154
155     TRACE("(%p) %p\n",This,pdimFocus);
156
157     if (!pdimFocus || FAILED(IUnknown_QueryInterface(pdimFocus,&IID_ITfDocumentMgr,(LPVOID*) &check)))
158         return E_INVALIDARG;
159
160     if (This->focus)
161         ITfDocumentMgr_Release(This->focus);
162
163     This->focus = check;
164     return S_OK;
165 }
166
167 static HRESULT WINAPI ThreadMgr_AssociateFocus( ITfThreadMgr* iface, HWND hwnd,
168 ITfDocumentMgr *pdimNew, ITfDocumentMgr **ppdimPrev)
169 {
170     ThreadMgr *This = (ThreadMgr *)iface;
171     FIXME("STUB:(%p)\n",This);
172     return E_NOTIMPL;
173 }
174
175 static HRESULT WINAPI ThreadMgr_IsThreadFocus( ITfThreadMgr* iface, BOOL *pfThreadFocus)
176 {
177     ThreadMgr *This = (ThreadMgr *)iface;
178     FIXME("STUB:(%p)\n",This);
179     return E_NOTIMPL;
180 }
181
182 static HRESULT WINAPI ThreadMgr_GetFunctionProvider( ITfThreadMgr* iface, REFCLSID clsid,
183 ITfFunctionProvider **ppFuncProv)
184 {
185     ThreadMgr *This = (ThreadMgr *)iface;
186     FIXME("STUB:(%p)\n",This);
187     return E_NOTIMPL;
188 }
189
190 static HRESULT WINAPI ThreadMgr_EnumFunctionProviders( ITfThreadMgr* iface,
191 IEnumTfFunctionProviders **ppEnum)
192 {
193     ThreadMgr *This = (ThreadMgr *)iface;
194     FIXME("STUB:(%p)\n",This);
195     return E_NOTIMPL;
196 }
197
198 static HRESULT WINAPI ThreadMgr_GetGlobalCompartment( ITfThreadMgr* iface,
199 ITfCompartmentMgr **ppCompMgr)
200 {
201     ThreadMgr *This = (ThreadMgr *)iface;
202     FIXME("STUB:(%p)\n",This);
203     return E_NOTIMPL;
204 }
205
206 static const ITfThreadMgrVtbl ThreadMgr_ThreadMgrVtbl =
207 {
208     ThreadMgr_QueryInterface,
209     ThreadMgr_AddRef,
210     ThreadMgr_Release,
211
212     ThreadMgr_fnActivate,
213     ThreadMgr_fnDeactivate,
214     ThreadMgr_CreateDocumentMgr,
215     ThreadMgr_EnumDocumentMgrs,
216     ThreadMgr_GetFocus,
217     ThreadMgr_SetFocus,
218     ThreadMgr_AssociateFocus,
219     ThreadMgr_IsThreadFocus,
220     ThreadMgr_GetFunctionProvider,
221     ThreadMgr_EnumFunctionProviders,
222     ThreadMgr_GetGlobalCompartment
223 };
224
225 HRESULT ThreadMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
226 {
227     ThreadMgr *This;
228     if (pUnkOuter)
229         return CLASS_E_NOAGGREGATION;
230
231     /* Only 1 ThreadMgr is created per thread */
232     This = TlsGetValue(tlsIndex);
233     if (This)
234     {
235         ThreadMgr_AddRef((ITfThreadMgr*)This);
236         *ppOut = (IUnknown*)This;
237         return S_OK;
238     }
239
240     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgr));
241     if (This == NULL)
242         return E_OUTOFMEMORY;
243
244     This->ThreadMgrVtbl= &ThreadMgr_ThreadMgrVtbl;
245     This->refCount = 1;
246     TlsSetValue(tlsIndex,This);
247
248     TRACE("returning %p\n", This);
249     *ppOut = (IUnknown *)This;
250     return S_OK;
251 }