push d49b3885cdb0e58b792f6bf912143b7a9a4ea546
[wine/hacks.git] / dlls / msctf / context.c
blob2fc651f101bff1b86c59caca233669717423c951
1 /*
2 * ITfContext implementation
4 * Copyright 2009 Aric Stewart, CodeWeavers
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
25 #define COBJMACROS
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"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
40 #include "msctf.h"
41 #include "msctf_internal.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
45 typedef struct tagContextSink {
46 struct list entry;
47 union {
48 /* Context Sinks */
49 IUnknown *pIUnknown;
50 /* ITfContextKeyEventSink *pITfContextKeyEventSink; */
51 /* ITfEditTransactionSink *pITfEditTransactionSink; */
52 /* ITfStatusSink *pITfStatusSink; */
53 ITfTextEditSink *pITfTextEditSink;
54 /* ITfTextLayoutSink *pITfTextLayoutSink; */
55 } interfaces;
56 } ContextSink;
58 typedef struct tagContext {
59 const ITfContextVtbl *ContextVtbl;
60 const ITfSourceVtbl *SourceVtbl;
61 LONG refCount;
63 TfClientId tidOwner;
65 ITextStoreACP *pITextStoreACP;
66 ITfContextOwnerCompositionSink *pITfContextOwnerCompositionSink;
68 ITextStoreACPSink *pITextStoreACPSink;
70 /* kept as separate lists to reduce unnecessary iterations */
71 struct list pContextKeyEventSink;
72 struct list pEditTransactionSink;
73 struct list pStatusSink;
74 struct list pTextEditSink;
75 struct list pTextLayoutSink;
77 } Context;
80 typedef struct tagTextStoreACPSink {
81 const ITextStoreACPSinkVtbl *TextStoreACPSinkVtbl;
82 LONG refCount;
84 Context *pContext;
85 } TextStoreACPSink;
88 static HRESULT TextStoreACPSink_Constructor(ITextStoreACPSink **ppOut, Context *pContext);
90 static inline Context *impl_from_ITfSourceVtbl(ITfSource *iface)
92 return (Context *)((char *)iface - FIELD_OFFSET(Context,SourceVtbl));
95 static void free_sink(ContextSink *sink)
97 IUnknown_Release(sink->interfaces.pIUnknown);
98 HeapFree(GetProcessHeap(),0,sink);
101 static void Context_Destructor(Context *This)
103 struct list *cursor, *cursor2;
104 TRACE("destroying %p\n", This);
106 if (This->pITextStoreACPSink)
108 ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)This->pITextStoreACPSink);
109 ITextStoreACPSink_Release(This->pITextStoreACPSink);
112 if (This->pITextStoreACP)
113 ITextStoreACPSink_Release(This->pITextStoreACP);
115 if (This->pITfContextOwnerCompositionSink)
116 ITextStoreACPSink_Release(This->pITfContextOwnerCompositionSink);
118 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pContextKeyEventSink)
120 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
121 list_remove(cursor);
122 free_sink(sink);
124 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pEditTransactionSink)
126 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
127 list_remove(cursor);
128 free_sink(sink);
130 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pStatusSink)
132 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
133 list_remove(cursor);
134 free_sink(sink);
136 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pTextEditSink)
138 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
139 list_remove(cursor);
140 free_sink(sink);
142 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pTextLayoutSink)
144 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
145 list_remove(cursor);
146 free_sink(sink);
149 HeapFree(GetProcessHeap(),0,This);
152 static HRESULT WINAPI Context_QueryInterface(ITfContext *iface, REFIID iid, LPVOID *ppvOut)
154 Context *This = (Context *)iface;
155 *ppvOut = NULL;
157 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfContext))
159 *ppvOut = This;
161 else if (IsEqualIID(iid, &IID_ITfSource))
163 *ppvOut = &This->SourceVtbl;
166 if (*ppvOut)
168 IUnknown_AddRef(iface);
169 return S_OK;
172 WARN("unsupported interface: %s\n", debugstr_guid(iid));
173 return E_NOINTERFACE;
176 static ULONG WINAPI Context_AddRef(ITfContext *iface)
178 Context *This = (Context *)iface;
179 return InterlockedIncrement(&This->refCount);
182 static ULONG WINAPI Context_Release(ITfContext *iface)
184 Context *This = (Context *)iface;
185 ULONG ret;
187 ret = InterlockedDecrement(&This->refCount);
188 if (ret == 0)
189 Context_Destructor(This);
190 return ret;
193 /*****************************************************
194 * ITfContext functions
195 *****************************************************/
196 static HRESULT WINAPI Context_RequestEditSession (ITfContext *iface,
197 TfClientId tid, ITfEditSession *pes, DWORD dwFlags,
198 HRESULT *phrSession)
200 Context *This = (Context *)iface;
201 FIXME("STUB:(%p)\n",This);
202 return E_NOTIMPL;
205 static HRESULT WINAPI Context_InWriteSession (ITfContext *iface,
206 TfClientId tid,
207 BOOL *pfWriteSession)
209 Context *This = (Context *)iface;
210 FIXME("STUB:(%p)\n",This);
211 return E_NOTIMPL;
214 static HRESULT WINAPI Context_GetSelection (ITfContext *iface,
215 TfEditCookie ec, ULONG ulIndex, ULONG ulCount,
216 TF_SELECTION *pSelection, ULONG *pcFetched)
218 Context *This = (Context *)iface;
219 FIXME("STUB:(%p)\n",This);
220 return E_NOTIMPL;
223 static HRESULT WINAPI Context_SetSelection (ITfContext *iface,
224 TfEditCookie ec, ULONG ulCount, const TF_SELECTION *pSelection)
226 Context *This = (Context *)iface;
227 FIXME("STUB:(%p)\n",This);
228 return E_NOTIMPL;
231 static HRESULT WINAPI Context_GetStart (ITfContext *iface,
232 TfEditCookie ec, ITfRange **ppStart)
234 Context *This = (Context *)iface;
235 FIXME("STUB:(%p)\n",This);
236 return E_NOTIMPL;
239 static HRESULT WINAPI Context_GetEnd (ITfContext *iface,
240 TfEditCookie ec, ITfRange **ppEnd)
242 Context *This = (Context *)iface;
243 FIXME("STUB:(%p)\n",This);
244 return E_NOTIMPL;
247 static HRESULT WINAPI Context_GetActiveView (ITfContext *iface,
248 ITfContextView **ppView)
250 Context *This = (Context *)iface;
251 FIXME("STUB:(%p)\n",This);
252 return E_NOTIMPL;
255 static HRESULT WINAPI Context_EnumViews (ITfContext *iface,
256 IEnumTfContextViews **ppEnum)
258 Context *This = (Context *)iface;
259 FIXME("STUB:(%p)\n",This);
260 return E_NOTIMPL;
263 static HRESULT WINAPI Context_GetStatus (ITfContext *iface,
264 TF_STATUS *pdcs)
266 Context *This = (Context *)iface;
267 FIXME("STUB:(%p)\n",This);
268 return E_NOTIMPL;
271 static HRESULT WINAPI Context_GetProperty (ITfContext *iface,
272 REFGUID guidProp, ITfProperty **ppProp)
274 Context *This = (Context *)iface;
275 FIXME("STUB:(%p)\n",This);
276 return E_NOTIMPL;
279 static HRESULT WINAPI Context_GetAppProperty (ITfContext *iface,
280 REFGUID guidProp, ITfReadOnlyProperty **ppProp)
282 Context *This = (Context *)iface;
283 FIXME("STUB:(%p)\n",This);
284 return E_NOTIMPL;
287 static HRESULT WINAPI Context_TrackProperties (ITfContext *iface,
288 const GUID **prgProp, ULONG cProp, const GUID **prgAppProp,
289 ULONG cAppProp, ITfReadOnlyProperty **ppProperty)
291 Context *This = (Context *)iface;
292 FIXME("STUB:(%p)\n",This);
293 return E_NOTIMPL;
296 static HRESULT WINAPI Context_EnumProperties (ITfContext *iface,
297 IEnumTfProperties **ppEnum)
299 Context *This = (Context *)iface;
300 FIXME("STUB:(%p)\n",This);
301 return E_NOTIMPL;
304 static HRESULT WINAPI Context_GetDocumentMgr (ITfContext *iface,
305 ITfDocumentMgr **ppDm)
307 Context *This = (Context *)iface;
308 FIXME("STUB:(%p)\n",This);
309 return E_NOTIMPL;
312 static HRESULT WINAPI Context_CreateRangeBackup (ITfContext *iface,
313 TfEditCookie ec, ITfRange *pRange, ITfRangeBackup **ppBackup)
315 Context *This = (Context *)iface;
316 FIXME("STUB:(%p)\n",This);
317 return E_NOTIMPL;
320 static const ITfContextVtbl Context_ContextVtbl =
322 Context_QueryInterface,
323 Context_AddRef,
324 Context_Release,
326 Context_RequestEditSession,
327 Context_InWriteSession,
328 Context_GetSelection,
329 Context_SetSelection,
330 Context_GetStart,
331 Context_GetEnd,
332 Context_GetActiveView,
333 Context_EnumViews,
334 Context_GetStatus,
335 Context_GetProperty,
336 Context_GetAppProperty,
337 Context_TrackProperties,
338 Context_EnumProperties,
339 Context_GetDocumentMgr,
340 Context_CreateRangeBackup
343 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
345 Context *This = impl_from_ITfSourceVtbl(iface);
346 return Context_QueryInterface((ITfContext *)This, iid, *ppvOut);
349 static ULONG WINAPI Source_AddRef(ITfSource *iface)
351 Context *This = impl_from_ITfSourceVtbl(iface);
352 return Context_AddRef((ITfContext *)This);
355 static ULONG WINAPI Source_Release(ITfSource *iface)
357 Context *This = impl_from_ITfSourceVtbl(iface);
358 return Context_Release((ITfContext *)This);
361 /*****************************************************
362 * ITfSource functions
363 *****************************************************/
364 static WINAPI HRESULT ContextSource_AdviseSink(ITfSource *iface,
365 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
367 ContextSink *es;
368 Context *This = impl_from_ITfSourceVtbl(iface);
369 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
371 if (!riid || !punk || !pdwCookie)
372 return E_INVALIDARG;
374 if (IsEqualIID(riid, &IID_ITfTextEditSink))
376 es = HeapAlloc(GetProcessHeap(),0,sizeof(ContextSink));
377 if (!es)
378 return E_OUTOFMEMORY;
379 if (!SUCCEEDED(IUnknown_QueryInterface(punk, riid, (LPVOID*)&es->interfaces.pITfTextEditSink)))
381 HeapFree(GetProcessHeap(),0,es);
382 return CONNECT_E_CANNOTCONNECT;
384 list_add_head(&This->pTextEditSink ,&es->entry);
385 *pdwCookie = (DWORD)es;
387 else
389 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
390 return E_NOTIMPL;
393 TRACE("cookie %x\n",*pdwCookie);
394 return S_OK;
397 static WINAPI HRESULT ContextSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
399 ContextSink *sink = (ContextSink*)pdwCookie;
400 Context *This = impl_from_ITfSourceVtbl(iface);
401 TRACE("(%p) %x\n",This,pdwCookie);
403 list_remove(&sink->entry);
404 free_sink(sink);
406 return S_OK;
409 static const ITfSourceVtbl Context_SourceVtbl =
411 Source_QueryInterface,
412 Source_AddRef,
413 Source_Release,
415 ContextSource_AdviseSink,
416 ContextSource_UnadviseSink,
419 HRESULT Context_Constructor(TfClientId tidOwner, IUnknown *punk, ITfContext **ppOut, TfEditCookie *pecTextStore)
421 Context *This;
423 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Context));
424 if (This == NULL)
425 return E_OUTOFMEMORY;
427 TRACE("(%p) %x %p %p %p\n",This, tidOwner, punk, ppOut, pecTextStore);
429 This->ContextVtbl= &Context_ContextVtbl;
430 This->SourceVtbl = &Context_SourceVtbl;
431 This->refCount = 1;
432 This->tidOwner = tidOwner;
434 if (punk)
436 if (SUCCEEDED(IUnknown_QueryInterface(punk, &IID_ITextStoreACP,
437 (LPVOID*)&This->pITextStoreACP)))
439 if (SUCCEEDED(TextStoreACPSink_Constructor(&This->pITextStoreACPSink, This)))
440 ITextStoreACP_AdviseSink(This->pITextStoreACP, &IID_ITextStoreACPSink,
441 (IUnknown*)This->pITextStoreACPSink, TS_AS_ALL_SINKS);
444 IUnknown_QueryInterface(punk, &IID_ITfContextOwnerCompositionSink,
445 (LPVOID*)&This->pITfContextOwnerCompositionSink);
447 if (!This->pITextStoreACP && !This->pITfContextOwnerCompositionSink)
448 FIXME("Unhandled pUnk\n");
451 TRACE("returning %p\n", This);
452 *ppOut = (ITfContext*)This;
453 /* FIXME */
454 *pecTextStore = 0xdeaddead;
456 list_init(&This->pContextKeyEventSink);
457 list_init(&This->pEditTransactionSink);
458 list_init(&This->pStatusSink);
459 list_init(&This->pTextEditSink);
460 list_init(&This->pTextLayoutSink);
462 return S_OK;
465 /**************************************************************************
466 * ITextStoreACPSink
467 **************************************************************************/
469 static void TextStoreACPSink_Destructor(TextStoreACPSink *This)
471 TRACE("destroying %p\n", This);
472 HeapFree(GetProcessHeap(),0,This);
475 static HRESULT WINAPI TextStoreACPSink_QueryInterface(ITextStoreACPSink *iface, REFIID iid, LPVOID *ppvOut)
477 TextStoreACPSink *This = (TextStoreACPSink *)iface;
478 *ppvOut = NULL;
480 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACPSink))
482 *ppvOut = This;
485 if (*ppvOut)
487 IUnknown_AddRef(iface);
488 return S_OK;
491 WARN("unsupported interface: %s\n", debugstr_guid(iid));
492 return E_NOINTERFACE;
495 static ULONG WINAPI TextStoreACPSink_AddRef(ITextStoreACPSink *iface)
497 TextStoreACPSink *This = (TextStoreACPSink *)iface;
498 return InterlockedIncrement(&This->refCount);
501 static ULONG WINAPI TextStoreACPSink_Release(ITextStoreACPSink *iface)
503 TextStoreACPSink *This = (TextStoreACPSink *)iface;
504 ULONG ret;
506 ret = InterlockedDecrement(&This->refCount);
507 if (ret == 0)
508 TextStoreACPSink_Destructor(This);
509 return ret;
512 /*****************************************************
513 * ITextStoreACPSink functions
514 *****************************************************/
516 static HRESULT WINAPI TextStoreACPSink_OnTextChange(ITextStoreACPSink *iface,
517 DWORD dwFlags, const TS_TEXTCHANGE *pChange)
519 TextStoreACPSink *This = (TextStoreACPSink *)iface;
520 FIXME("STUB:(%p)\n",This);
521 return E_NOTIMPL;
524 static HRESULT WINAPI TextStoreACPSink_OnSelectionChange(ITextStoreACPSink *iface)
526 TextStoreACPSink *This = (TextStoreACPSink *)iface;
527 FIXME("STUB:(%p)\n",This);
528 return E_NOTIMPL;
531 static HRESULT WINAPI TextStoreACPSink_OnLayoutChange(ITextStoreACPSink *iface,
532 TsLayoutCode lcode, TsViewCookie vcView)
534 TextStoreACPSink *This = (TextStoreACPSink *)iface;
535 FIXME("STUB:(%p)\n",This);
536 return E_NOTIMPL;
539 static HRESULT WINAPI TextStoreACPSink_OnStatusChange(ITextStoreACPSink *iface,
540 DWORD dwFlags)
542 TextStoreACPSink *This = (TextStoreACPSink *)iface;
543 FIXME("STUB:(%p)\n",This);
544 return E_NOTIMPL;
547 static HRESULT WINAPI TextStoreACPSink_OnAttrsChange(ITextStoreACPSink *iface,
548 LONG acpStart, LONG acpEnd, ULONG cAttrs, const TS_ATTRID *paAttrs)
550 TextStoreACPSink *This = (TextStoreACPSink *)iface;
551 FIXME("STUB:(%p)\n",This);
552 return E_NOTIMPL;
555 static HRESULT WINAPI TextStoreACPSink_OnLockGranted(ITextStoreACPSink *iface,
556 DWORD dwLockFlags)
558 TextStoreACPSink *This = (TextStoreACPSink *)iface;
559 FIXME("STUB:(%p)\n",This);
560 return E_NOTIMPL;
563 static HRESULT WINAPI TextStoreACPSink_OnStartEditTransaction(ITextStoreACPSink *iface)
565 TextStoreACPSink *This = (TextStoreACPSink *)iface;
566 FIXME("STUB:(%p)\n",This);
567 return E_NOTIMPL;
570 static HRESULT WINAPI TextStoreACPSink_OnEndEditTransaction(ITextStoreACPSink *iface)
572 TextStoreACPSink *This = (TextStoreACPSink *)iface;
573 FIXME("STUB:(%p)\n",This);
574 return E_NOTIMPL;
577 static const ITextStoreACPSinkVtbl TextStoreACPSink_TextStoreACPSinkVtbl =
579 TextStoreACPSink_QueryInterface,
580 TextStoreACPSink_AddRef,
581 TextStoreACPSink_Release,
583 TextStoreACPSink_OnTextChange,
584 TextStoreACPSink_OnSelectionChange,
585 TextStoreACPSink_OnLayoutChange,
586 TextStoreACPSink_OnStatusChange,
587 TextStoreACPSink_OnAttrsChange,
588 TextStoreACPSink_OnLockGranted,
589 TextStoreACPSink_OnStartEditTransaction,
590 TextStoreACPSink_OnEndEditTransaction
593 static HRESULT TextStoreACPSink_Constructor(ITextStoreACPSink **ppOut, Context *pContext)
595 TextStoreACPSink *This;
597 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACPSink));
598 if (This == NULL)
599 return E_OUTOFMEMORY;
601 This->TextStoreACPSinkVtbl= &TextStoreACPSink_TextStoreACPSinkVtbl;
602 This->refCount = 1;
604 This->pContext = pContext;
606 TRACE("returning %p\n", This);
607 *ppOut = (ITextStoreACPSink*)This;
608 return S_OK;