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