wined3d: Implement shader_sm4_is_end().
[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 #include "olectl.h"
36
37 #include "wine/unicode.h"
38 #include "wine/list.h"
39
40 #include "msctf.h"
41 #include "msctf_internal.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
44
45 typedef struct tagThreadMgrSink {
46     struct list         entry;
47     union {
48         /* ThreadMgr Sinks */
49         IUnknown            *pIUnknown;
50         /* ITfActiveLanguageProfileNotifySink *pITfActiveLanguageProfileNotifySink; */
51         /* ITfDisplayAttributeNotifySink *pITfDisplayAttributeNotifySink; */
52         /* ITfKeyTraceEventSink *pITfKeyTraceEventSink; */
53         /* ITfPreservedKeyNotifySink *pITfPreservedKeyNotifySink; */
54         /* ITfThreadFocusSink *pITfThreadFocusSink; */
55         ITfThreadMgrEventSink *pITfThreadMgrEventSink;
56     } interfaces;
57 } ThreadMgrSink;
58
59 typedef struct tagACLMulti {
60     const ITfThreadMgrVtbl *ThreadMgrVtbl;
61     const ITfSourceVtbl *SourceVtbl;
62     const ITfKeystrokeMgrVtbl *KeystrokeMgrVtbl;
63     LONG refCount;
64
65     const ITfThreadMgrEventSinkVtbl *ThreadMgrEventSinkVtbl; /* internal */
66
67     ITfDocumentMgr *focus;
68
69     /* kept as separate lists to reduce unnecessary iterations */
70     struct list     ActiveLanguageProfileNotifySink;
71     struct list     DisplayAttributeNotifySink;
72     struct list     KeyTraceEventSink;
73     struct list     PreservedKeyNotifySink;
74     struct list     ThreadFocusSink;
75     struct list     ThreadMgrEventSink;
76 } ThreadMgr;
77
78 static inline ThreadMgr *impl_from_ITfSourceVtbl(ITfSource *iface)
79 {
80     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,SourceVtbl));
81 }
82
83 static inline ThreadMgr *impl_from_ITfKeystrokeMgrVtbl(ITfKeystrokeMgr *iface)
84 {
85     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,KeystrokeMgrVtbl));
86 }
87
88 static inline ThreadMgr *impl_from_ITfThreadMgrEventSink(ITfThreadMgrEventSink *iface)
89 {
90     return (ThreadMgr *)((char *)iface - FIELD_OFFSET(ThreadMgr,ThreadMgrEventSinkVtbl));
91 }
92
93 static void free_sink(ThreadMgrSink *sink)
94 {
95         IUnknown_Release(sink->interfaces.pIUnknown);
96         HeapFree(GetProcessHeap(),0,sink);
97 }
98
99 static void ThreadMgr_Destructor(ThreadMgr *This)
100 {
101     struct list *cursor, *cursor2;
102
103     TlsSetValue(tlsIndex,NULL);
104     TRACE("destroying %p\n", This);
105     if (This->focus)
106         ITfDocumentMgr_Release(This->focus);
107
108     /* free sinks */
109     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ActiveLanguageProfileNotifySink)
110     {
111         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
112         list_remove(cursor);
113         free_sink(sink);
114     }
115     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->DisplayAttributeNotifySink)
116     {
117         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
118         list_remove(cursor);
119         free_sink(sink);
120     }
121     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->KeyTraceEventSink)
122     {
123         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
124         list_remove(cursor);
125         free_sink(sink);
126     }
127     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->PreservedKeyNotifySink)
128     {
129         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
130         list_remove(cursor);
131         free_sink(sink);
132     }
133     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadFocusSink)
134     {
135         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
136         list_remove(cursor);
137         free_sink(sink);
138     }
139     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->ThreadMgrEventSink)
140     {
141         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
142         list_remove(cursor);
143         free_sink(sink);
144     }
145
146     HeapFree(GetProcessHeap(),0,This);
147 }
148
149 static HRESULT WINAPI ThreadMgr_QueryInterface(ITfThreadMgr *iface, REFIID iid, LPVOID *ppvOut)
150 {
151     ThreadMgr *This = (ThreadMgr *)iface;
152     *ppvOut = NULL;
153
154     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfThreadMgr))
155     {
156         *ppvOut = This;
157     }
158     else if (IsEqualIID(iid, &IID_ITfSource))
159     {
160         *ppvOut = &This->SourceVtbl;
161     }
162     else if (IsEqualIID(iid, &IID_ITfKeystrokeMgr))
163     {
164         *ppvOut = &This->KeystrokeMgrVtbl;
165     }
166
167     if (*ppvOut)
168     {
169         IUnknown_AddRef(iface);
170         return S_OK;
171     }
172
173     WARN("unsupported interface: %s\n", debugstr_guid(iid));
174     return E_NOINTERFACE;
175 }
176
177 static ULONG WINAPI ThreadMgr_AddRef(ITfThreadMgr *iface)
178 {
179     ThreadMgr *This = (ThreadMgr *)iface;
180     return InterlockedIncrement(&This->refCount);
181 }
182
183 static ULONG WINAPI ThreadMgr_Release(ITfThreadMgr *iface)
184 {
185     ThreadMgr *This = (ThreadMgr *)iface;
186     ULONG ret;
187
188     ret = InterlockedDecrement(&This->refCount);
189     if (ret == 0)
190         ThreadMgr_Destructor(This);
191     return ret;
192 }
193
194 /*****************************************************
195  * ITfThreadMgr functions
196  *****************************************************/
197
198 static HRESULT WINAPI ThreadMgr_fnActivate( ITfThreadMgr* iface, TfClientId *ptid)
199 {
200     ThreadMgr *This = (ThreadMgr *)iface;
201     FIXME("STUB:(%p)\n",This);
202     return E_NOTIMPL;
203 }
204
205 static HRESULT WINAPI ThreadMgr_fnDeactivate( ITfThreadMgr* iface)
206 {
207     ThreadMgr *This = (ThreadMgr *)iface;
208     FIXME("STUB:(%p)\n",This);
209
210     if (This->focus)
211     {
212         ITfThreadMgrEventSink_OnSetFocus((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, 0, This->focus);
213         ITfDocumentMgr_Release(This->focus);
214         This->focus = 0;
215     }
216
217     return E_NOTIMPL;
218 }
219
220 static HRESULT WINAPI ThreadMgr_CreateDocumentMgr( ITfThreadMgr* iface, ITfDocumentMgr
221 **ppdim)
222 {
223     ThreadMgr *This = (ThreadMgr *)iface;
224     TRACE("(%p)\n",iface);
225     return DocumentMgr_Constructor((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, ppdim);
226 }
227
228 static HRESULT WINAPI ThreadMgr_EnumDocumentMgrs( ITfThreadMgr* iface, IEnumTfDocumentMgrs
229 **ppEnum)
230 {
231     ThreadMgr *This = (ThreadMgr *)iface;
232     FIXME("STUB:(%p)\n",This);
233     return E_NOTIMPL;
234 }
235
236 static HRESULT WINAPI ThreadMgr_GetFocus( ITfThreadMgr* iface, ITfDocumentMgr
237 **ppdimFocus)
238 {
239     ThreadMgr *This = (ThreadMgr *)iface;
240     TRACE("(%p)\n",This);
241
242     if (!ppdimFocus)
243         return E_INVALIDARG;
244
245     *ppdimFocus = This->focus;
246
247     TRACE("->%p\n",This->focus);
248
249     if (This->focus == NULL)
250         return S_FALSE;
251
252     ITfDocumentMgr_AddRef(This->focus);
253
254     return S_OK;
255 }
256
257 static HRESULT WINAPI ThreadMgr_SetFocus( ITfThreadMgr* iface, ITfDocumentMgr *pdimFocus)
258 {
259     ITfDocumentMgr *check;
260     ThreadMgr *This = (ThreadMgr *)iface;
261
262     TRACE("(%p) %p\n",This,pdimFocus);
263
264     if (!pdimFocus || FAILED(IUnknown_QueryInterface(pdimFocus,&IID_ITfDocumentMgr,(LPVOID*) &check)))
265         return E_INVALIDARG;
266
267     ITfThreadMgrEventSink_OnSetFocus((ITfThreadMgrEventSink*)&This->ThreadMgrEventSinkVtbl, check, This->focus);
268
269     if (This->focus)
270         ITfDocumentMgr_Release(This->focus);
271
272     This->focus = check;
273     return S_OK;
274 }
275
276 static HRESULT WINAPI ThreadMgr_AssociateFocus( ITfThreadMgr* iface, HWND hwnd,
277 ITfDocumentMgr *pdimNew, ITfDocumentMgr **ppdimPrev)
278 {
279     ThreadMgr *This = (ThreadMgr *)iface;
280     FIXME("STUB:(%p)\n",This);
281     return E_NOTIMPL;
282 }
283
284 static HRESULT WINAPI ThreadMgr_IsThreadFocus( ITfThreadMgr* iface, BOOL *pfThreadFocus)
285 {
286     ThreadMgr *This = (ThreadMgr *)iface;
287     FIXME("STUB:(%p)\n",This);
288     return E_NOTIMPL;
289 }
290
291 static HRESULT WINAPI ThreadMgr_GetFunctionProvider( ITfThreadMgr* iface, REFCLSID clsid,
292 ITfFunctionProvider **ppFuncProv)
293 {
294     ThreadMgr *This = (ThreadMgr *)iface;
295     FIXME("STUB:(%p)\n",This);
296     return E_NOTIMPL;
297 }
298
299 static HRESULT WINAPI ThreadMgr_EnumFunctionProviders( ITfThreadMgr* iface,
300 IEnumTfFunctionProviders **ppEnum)
301 {
302     ThreadMgr *This = (ThreadMgr *)iface;
303     FIXME("STUB:(%p)\n",This);
304     return E_NOTIMPL;
305 }
306
307 static HRESULT WINAPI ThreadMgr_GetGlobalCompartment( ITfThreadMgr* iface,
308 ITfCompartmentMgr **ppCompMgr)
309 {
310     ThreadMgr *This = (ThreadMgr *)iface;
311     FIXME("STUB:(%p)\n",This);
312     return E_NOTIMPL;
313 }
314
315 static const ITfThreadMgrVtbl ThreadMgr_ThreadMgrVtbl =
316 {
317     ThreadMgr_QueryInterface,
318     ThreadMgr_AddRef,
319     ThreadMgr_Release,
320
321     ThreadMgr_fnActivate,
322     ThreadMgr_fnDeactivate,
323     ThreadMgr_CreateDocumentMgr,
324     ThreadMgr_EnumDocumentMgrs,
325     ThreadMgr_GetFocus,
326     ThreadMgr_SetFocus,
327     ThreadMgr_AssociateFocus,
328     ThreadMgr_IsThreadFocus,
329     ThreadMgr_GetFunctionProvider,
330     ThreadMgr_EnumFunctionProviders,
331     ThreadMgr_GetGlobalCompartment
332 };
333
334
335 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
336 {
337     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
338     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
339 }
340
341 static ULONG WINAPI Source_AddRef(ITfSource *iface)
342 {
343     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
344     return ThreadMgr_AddRef((ITfThreadMgr*)This);
345 }
346
347 static ULONG WINAPI Source_Release(ITfSource *iface)
348 {
349     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
350     return ThreadMgr_Release((ITfThreadMgr *)This);
351 }
352
353 /*****************************************************
354  * ITfSource functions
355  *****************************************************/
356 static WINAPI HRESULT ThreadMgrSource_AdviseSink(ITfSource *iface,
357         REFIID riid, IUnknown *punk, DWORD *pdwCookie)
358 {
359     ThreadMgrSink *tms;
360     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
361
362     TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
363
364     if (!riid || !punk || !pdwCookie)
365         return E_INVALIDARG;
366
367     if (IsEqualIID(riid, &IID_ITfThreadMgrEventSink))
368     {
369         tms = HeapAlloc(GetProcessHeap(),0,sizeof(ThreadMgrSink));
370         if (!tms)
371             return E_OUTOFMEMORY;
372         if (!SUCCEEDED(IUnknown_QueryInterface(punk, riid, (LPVOID*)&tms->interfaces.pITfThreadMgrEventSink)))
373         {
374             HeapFree(GetProcessHeap(),0,tms);
375             return CONNECT_E_CANNOTCONNECT;
376         }
377         list_add_head(&This->ThreadMgrEventSink,&tms->entry);
378         *pdwCookie = generate_Cookie(COOKIE_MAGIC_TMSINK, tms);
379     }
380     else
381     {
382         FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
383         return E_NOTIMPL;
384     }
385
386     TRACE("cookie %x\n",*pdwCookie);
387
388     return S_OK;
389 }
390
391 static WINAPI HRESULT ThreadMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
392 {
393     ThreadMgrSink *sink;
394     ThreadMgr *This = impl_from_ITfSourceVtbl(iface);
395
396     TRACE("(%p) %x\n",This,pdwCookie);
397
398     if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_TMSINK)
399         return E_INVALIDARG;
400
401     sink = (ThreadMgrSink*)remove_Cookie(pdwCookie);
402     if (!sink)
403         return CONNECT_E_NOCONNECTION;
404
405     list_remove(&sink->entry);
406     free_sink(sink);
407
408     return S_OK;
409 }
410
411 static const ITfSourceVtbl ThreadMgr_SourceVtbl =
412 {
413     Source_QueryInterface,
414     Source_AddRef,
415     Source_Release,
416
417     ThreadMgrSource_AdviseSink,
418     ThreadMgrSource_UnadviseSink,
419 };
420
421 /*****************************************************
422  * ITfKeystrokeMgr functions
423  *****************************************************/
424
425 static HRESULT WINAPI KeystrokeMgr_QueryInterface(ITfKeystrokeMgr *iface, REFIID iid, LPVOID *ppvOut)
426 {
427     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
428     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
429 }
430
431 static ULONG WINAPI KeystrokeMgr_AddRef(ITfKeystrokeMgr *iface)
432 {
433     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
434     return ThreadMgr_AddRef((ITfThreadMgr*)This);
435 }
436
437 static ULONG WINAPI KeystrokeMgr_Release(ITfKeystrokeMgr *iface)
438 {
439     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
440     return ThreadMgr_Release((ITfThreadMgr *)This);
441 }
442
443 static HRESULT WINAPI KeystrokeMgr_AdviseKeyEventSink(ITfKeystrokeMgr *iface,
444         TfClientId tid, ITfKeyEventSink *pSink, BOOL fForeground)
445 {
446     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
447     FIXME("STUB:(%p)\n",This);
448     return E_NOTIMPL;
449 }
450
451 static HRESULT WINAPI KeystrokeMgr_UnadviseKeyEventSink(ITfKeystrokeMgr *iface,
452         TfClientId tid)
453 {
454     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
455     FIXME("STUB:(%p)\n",This);
456     return E_NOTIMPL;
457 }
458
459 static HRESULT WINAPI KeystrokeMgr_GetForeground(ITfKeystrokeMgr *iface,
460         CLSID *pclsid)
461 {
462     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
463     FIXME("STUB:(%p)\n",This);
464     return E_NOTIMPL;
465 }
466
467 static HRESULT WINAPI KeystrokeMgr_TestKeyDown(ITfKeystrokeMgr *iface,
468         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
469 {
470     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
471     FIXME("STUB:(%p)\n",This);
472     return E_NOTIMPL;
473 }
474
475 static HRESULT WINAPI KeystrokeMgr_TestKeyUp(ITfKeystrokeMgr *iface,
476         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
477 {
478     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
479     FIXME("STUB:(%p)\n",This);
480     return E_NOTIMPL;
481 }
482
483 static HRESULT WINAPI KeystrokeMgr_KeyDown(ITfKeystrokeMgr *iface,
484         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
485 {
486     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
487     FIXME("STUB:(%p)\n",This);
488     return E_NOTIMPL;
489 }
490
491 static HRESULT WINAPI KeystrokeMgr_KeyUp(ITfKeystrokeMgr *iface,
492         WPARAM wParam, LPARAM lParam, BOOL *pfEaten)
493 {
494     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
495     FIXME("STUB:(%p)\n",This);
496     return E_NOTIMPL;
497 }
498
499 static HRESULT WINAPI KeystrokeMgr_GetPreservedKey(ITfKeystrokeMgr *iface,
500         ITfContext *pic, const TF_PRESERVEDKEY *pprekey, GUID *pguid)
501 {
502     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
503     FIXME("STUB:(%p)\n",This);
504     return E_NOTIMPL;
505 }
506
507 static HRESULT WINAPI KeystrokeMgr_IsPreservedKey(ITfKeystrokeMgr *iface,
508         REFGUID rguid, const TF_PRESERVEDKEY *pprekey, BOOL *pfRegistered)
509 {
510     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
511     FIXME("STUB:(%p)\n",This);
512     return E_NOTIMPL;
513 }
514
515 static HRESULT WINAPI KeystrokeMgr_PreserveKey(ITfKeystrokeMgr *iface,
516         TfClientId tid, REFGUID rguid, const TF_PRESERVEDKEY *prekey,
517         const WCHAR *pchDesc, ULONG cchDesc)
518 {
519     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
520     FIXME("STUB:(%p)\n",This);
521     return E_NOTIMPL;
522 }
523
524 static HRESULT WINAPI KeystrokeMgr_UnpreserveKey(ITfKeystrokeMgr *iface,
525         REFGUID rguid, const TF_PRESERVEDKEY *pprekey)
526 {
527     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
528     FIXME("STUB:(%p)\n",This);
529     return E_NOTIMPL;
530 }
531
532 static HRESULT WINAPI KeystrokeMgr_SetPreservedKeyDescription(ITfKeystrokeMgr *iface,
533         REFGUID rguid, const WCHAR *pchDesc, ULONG cchDesc)
534 {
535     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
536     FIXME("STUB:(%p)\n",This);
537     return E_NOTIMPL;
538 }
539
540 static HRESULT WINAPI KeystrokeMgr_GetPreservedKeyDescription(ITfKeystrokeMgr *iface,
541         REFGUID rguid, BSTR *pbstrDesc)
542 {
543     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
544     FIXME("STUB:(%p)\n",This);
545     return E_NOTIMPL;
546 }
547
548 static HRESULT WINAPI KeystrokeMgr_SimulatePreservedKey(ITfKeystrokeMgr *iface,
549         ITfContext *pic, REFGUID rguid, BOOL *pfEaten)
550 {
551     ThreadMgr *This = impl_from_ITfKeystrokeMgrVtbl(iface);
552     FIXME("STUB:(%p)\n",This);
553     return E_NOTIMPL;
554 }
555
556 static const ITfKeystrokeMgrVtbl ThreadMgr_KeystrokeMgrVtbl =
557 {
558     KeystrokeMgr_QueryInterface,
559     KeystrokeMgr_AddRef,
560     KeystrokeMgr_Release,
561
562     KeystrokeMgr_AdviseKeyEventSink,
563     KeystrokeMgr_UnadviseKeyEventSink,
564     KeystrokeMgr_GetForeground,
565     KeystrokeMgr_TestKeyDown,
566     KeystrokeMgr_TestKeyUp,
567     KeystrokeMgr_KeyDown,
568     KeystrokeMgr_KeyUp,
569     KeystrokeMgr_GetPreservedKey,
570     KeystrokeMgr_IsPreservedKey,
571     KeystrokeMgr_PreserveKey,
572     KeystrokeMgr_UnpreserveKey,
573     KeystrokeMgr_SetPreservedKeyDescription,
574     KeystrokeMgr_GetPreservedKeyDescription,
575     KeystrokeMgr_SimulatePreservedKey
576 };
577
578 /*****************************************************
579  * ITfThreadMgrEventSink functions  (internal)
580  *****************************************************/
581 static HRESULT WINAPI ThreadMgrEventSink_QueryInterface(ITfThreadMgrEventSink *iface, REFIID iid, LPVOID *ppvOut)
582 {
583     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
584     return ThreadMgr_QueryInterface((ITfThreadMgr *)This, iid, *ppvOut);
585 }
586
587 static ULONG WINAPI ThreadMgrEventSink_AddRef(ITfThreadMgrEventSink *iface)
588 {
589     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
590     return ThreadMgr_AddRef((ITfThreadMgr*)This);
591 }
592
593 static ULONG WINAPI ThreadMgrEventSink_Release(ITfThreadMgrEventSink *iface)
594 {
595     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
596     return ThreadMgr_Release((ITfThreadMgr *)This);
597 }
598
599
600 static WINAPI HRESULT ThreadMgrEventSink_OnInitDocumentMgr(
601         ITfThreadMgrEventSink *iface,ITfDocumentMgr *pdim)
602 {
603     struct list *cursor;
604     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
605
606     TRACE("(%p) %p\n",This,pdim);
607
608     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
609     {
610         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
611         ITfThreadMgrEventSink_OnInitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
612     }
613
614     return S_OK;
615 }
616
617 static WINAPI HRESULT ThreadMgrEventSink_OnUninitDocumentMgr(
618         ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdim)
619 {
620     struct list *cursor;
621     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
622
623     TRACE("(%p) %p\n",This,pdim);
624
625     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
626     {
627         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
628         ITfThreadMgrEventSink_OnUninitDocumentMgr(sink->interfaces.pITfThreadMgrEventSink,pdim);
629     }
630
631     return S_OK;
632 }
633
634 static WINAPI HRESULT ThreadMgrEventSink_OnSetFocus(
635         ITfThreadMgrEventSink *iface, ITfDocumentMgr *pdimFocus,
636         ITfDocumentMgr *pdimPrevFocus)
637 {
638     struct list *cursor;
639     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
640
641     TRACE("(%p) %p %p\n",This,pdimFocus, pdimPrevFocus);
642
643     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
644     {
645         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
646         ITfThreadMgrEventSink_OnSetFocus(sink->interfaces.pITfThreadMgrEventSink, pdimFocus, pdimPrevFocus);
647     }
648
649     return S_OK;
650 }
651
652 static WINAPI HRESULT ThreadMgrEventSink_OnPushContext(
653         ITfThreadMgrEventSink *iface, ITfContext *pic)
654 {
655     struct list *cursor;
656     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
657
658     TRACE("(%p) %p\n",This,pic);
659
660     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
661     {
662         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
663         ITfThreadMgrEventSink_OnPushContext(sink->interfaces.pITfThreadMgrEventSink,pic);
664     }
665
666     return S_OK;
667 }
668
669 static WINAPI HRESULT ThreadMgrEventSink_OnPopContext(
670         ITfThreadMgrEventSink *iface, ITfContext *pic)
671 {
672     struct list *cursor;
673     ThreadMgr *This = impl_from_ITfThreadMgrEventSink(iface);
674
675     TRACE("(%p) %p\n",This,pic);
676
677     LIST_FOR_EACH(cursor, &This->ThreadMgrEventSink)
678     {
679         ThreadMgrSink* sink = LIST_ENTRY(cursor,ThreadMgrSink,entry);
680         ITfThreadMgrEventSink_OnPopContext(sink->interfaces.pITfThreadMgrEventSink,pic);
681     }
682
683     return S_OK;
684 }
685
686 static const ITfThreadMgrEventSinkVtbl ThreadMgr_ThreadMgrEventSinkVtbl =
687 {
688     ThreadMgrEventSink_QueryInterface,
689     ThreadMgrEventSink_AddRef,
690     ThreadMgrEventSink_Release,
691
692     ThreadMgrEventSink_OnInitDocumentMgr,
693     ThreadMgrEventSink_OnUninitDocumentMgr,
694     ThreadMgrEventSink_OnSetFocus,
695     ThreadMgrEventSink_OnPushContext,
696     ThreadMgrEventSink_OnPopContext
697 };
698
699 HRESULT ThreadMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
700 {
701     ThreadMgr *This;
702     if (pUnkOuter)
703         return CLASS_E_NOAGGREGATION;
704
705     /* Only 1 ThreadMgr is created per thread */
706     This = TlsGetValue(tlsIndex);
707     if (This)
708     {
709         ThreadMgr_AddRef((ITfThreadMgr*)This);
710         *ppOut = (IUnknown*)This;
711         return S_OK;
712     }
713
714     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ThreadMgr));
715     if (This == NULL)
716         return E_OUTOFMEMORY;
717
718     This->ThreadMgrVtbl= &ThreadMgr_ThreadMgrVtbl;
719     This->SourceVtbl = &ThreadMgr_SourceVtbl;
720     This->KeystrokeMgrVtbl= &ThreadMgr_KeystrokeMgrVtbl;
721     This->ThreadMgrEventSinkVtbl = &ThreadMgr_ThreadMgrEventSinkVtbl;
722     This->refCount = 1;
723     TlsSetValue(tlsIndex,This);
724
725     list_init(&This->ActiveLanguageProfileNotifySink);
726     list_init(&This->DisplayAttributeNotifySink);
727     list_init(&This->KeyTraceEventSink);
728     list_init(&This->PreservedKeyNotifySink);
729     list_init(&This->ThreadFocusSink);
730     list_init(&This->ThreadMgrEventSink);
731
732     TRACE("returning %p\n", This);
733     *ppOut = (IUnknown *)This;
734     return S_OK;
735 }