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
27 #include "wine/debug.h"
37 #include "wine/unicode.h"
38 #include "wine/list.h"
41 #include "msctf_internal.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
45 typedef struct tagContextSink {
50 /* ITfContextKeyEventSink *pITfContextKeyEventSink; */
51 /* ITfEditTransactionSink *pITfEditTransactionSink; */
52 /* ITfStatusSink *pITfStatusSink; */
53 ITfTextEditSink *pITfTextEditSink;
54 /* ITfTextLayoutSink *pITfTextLayoutSink; */
58 typedef struct tagContext {
59 const ITfContextVtbl *ContextVtbl;
60 const ITfSourceVtbl *SourceVtbl;
61 /* const ITfContextCompositionVtbl *ContextCompositionVtbl; */
62 /* const ITfContextOwnerCompositionServicesVtbl *ContextOwnerCompositionServicesVtbl; */
63 /* const ITfContextOwnerServicesVtbl *ContextOwnerServicesVtbl; */
64 /* const ITfInsertAtSelectionVtbl *InsertAtSelectionVtbl; */
65 /* const ITfMouseTrackerVtbl *MouseTrackerVtbl; */
66 /* const ITfQueryEmbeddedVtbl *QueryEmbeddedVtbl; */
67 /* const ITfSourceSingleVtbl *SourceSingleVtbl; */
72 TfEditCookie defaultCookie;
74 ITextStoreACP *pITextStoreACP;
75 ITfContextOwnerCompositionSink *pITfContextOwnerCompositionSink;
77 ITextStoreACPSink *pITextStoreACPSink;
78 ITfEditSession* currentEditSession;
80 /* kept as separate lists to reduce unnecessary iterations */
81 struct list pContextKeyEventSink;
82 struct list pEditTransactionSink;
83 struct list pStatusSink;
84 struct list pTextEditSink;
85 struct list pTextLayoutSink;
89 typedef struct tagEditCookie {
91 Context *pOwningContext;
94 typedef struct tagTextStoreACPSink {
95 const ITextStoreACPSinkVtbl *TextStoreACPSinkVtbl;
96 /* const ITextStoreACPServicesVtbl *TextStoreACPServicesVtbl; */
103 static HRESULT TextStoreACPSink_Constructor(ITextStoreACPSink **ppOut, Context *pContext);
105 static inline Context *impl_from_ITfSourceVtbl(ITfSource *iface)
107 return (Context *)((char *)iface - FIELD_OFFSET(Context,SourceVtbl));
110 static void free_sink(ContextSink *sink)
112 IUnknown_Release(sink->interfaces.pIUnknown);
113 HeapFree(GetProcessHeap(),0,sink);
116 static void Context_Destructor(Context *This)
118 struct list *cursor, *cursor2;
120 TRACE("destroying %p\n", This);
122 if (This->pITextStoreACPSink)
124 ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)This->pITextStoreACPSink);
125 ITextStoreACPSink_Release(This->pITextStoreACPSink);
128 if (This->pITextStoreACP)
129 ITextStoreACPSink_Release(This->pITextStoreACP);
131 if (This->pITfContextOwnerCompositionSink)
132 ITextStoreACPSink_Release(This->pITfContextOwnerCompositionSink);
134 if (This->defaultCookie)
136 cookie = remove_Cookie(This->defaultCookie);
137 HeapFree(GetProcessHeap(),0,cookie);
138 This->defaultCookie = 0;
141 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pContextKeyEventSink)
143 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
147 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pEditTransactionSink)
149 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
153 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pStatusSink)
155 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
159 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pTextEditSink)
161 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
165 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pTextLayoutSink)
167 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
172 HeapFree(GetProcessHeap(),0,This);
175 static HRESULT WINAPI Context_QueryInterface(ITfContext *iface, REFIID iid, LPVOID *ppvOut)
177 Context *This = (Context *)iface;
180 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfContext))
184 else if (IsEqualIID(iid, &IID_ITfSource))
186 *ppvOut = &This->SourceVtbl;
191 IUnknown_AddRef(iface);
195 WARN("unsupported interface: %s\n", debugstr_guid(iid));
196 return E_NOINTERFACE;
199 static ULONG WINAPI Context_AddRef(ITfContext *iface)
201 Context *This = (Context *)iface;
202 return InterlockedIncrement(&This->refCount);
205 static ULONG WINAPI Context_Release(ITfContext *iface)
207 Context *This = (Context *)iface;
210 ret = InterlockedDecrement(&This->refCount);
212 Context_Destructor(This);
216 /*****************************************************
217 * ITfContext functions
218 *****************************************************/
219 static HRESULT WINAPI Context_RequestEditSession (ITfContext *iface,
220 TfClientId tid, ITfEditSession *pes, DWORD dwFlags,
224 Context *This = (Context *)iface;
225 DWORD dwLockFlags = 0x0;
228 TRACE("(%p) %i %p %x %p\n",This, tid, pes, dwFlags, phrSession);
230 if (!(dwFlags & TF_ES_READ) && !(dwFlags & TF_ES_READWRITE))
232 *phrSession = E_FAIL;
236 if (!This->pITextStoreACP)
238 FIXME("No ITextStoreACP avaliable\n");
239 *phrSession = E_FAIL;
243 if (!(dwFlags & TF_ES_ASYNC))
244 dwLockFlags |= TS_LF_SYNC;
246 if ((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE)
247 dwLockFlags |= TS_LF_READWRITE;
248 else if (dwFlags & TF_ES_READ)
249 dwLockFlags |= TS_LF_READ;
251 /* TODO: cache this */
252 ITextStoreACP_GetStatus(This->pITextStoreACP, &status);
254 if (((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE) && (status.dwDynamicFlags & TS_SD_READONLY))
256 *phrSession = TS_E_READONLY;
260 if (FAILED (ITfEditSession_QueryInterface(pes, &IID_ITfEditSession, (LPVOID*)&This->currentEditSession)))
262 *phrSession = E_FAIL;
267 hr = ITextStoreACP_RequestLock(This->pITextStoreACP, dwLockFlags, phrSession);
272 static HRESULT WINAPI Context_InWriteSession (ITfContext *iface,
274 BOOL *pfWriteSession)
276 Context *This = (Context *)iface;
277 FIXME("STUB:(%p)\n",This);
281 static HRESULT WINAPI Context_GetSelection (ITfContext *iface,
282 TfEditCookie ec, ULONG ulIndex, ULONG ulCount,
283 TF_SELECTION *pSelection, ULONG *pcFetched)
285 Context *This = (Context *)iface;
288 ULONG totalFetched = 0;
291 if (!pSelection || !pcFetched)
296 if (!This->connected)
297 return TF_E_DISCONNECTED;
299 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
302 if (!This->pITextStoreACP)
304 FIXME("Context does not have a ITextStoreACP\n");
308 cookie = get_Cookie_data(ec);
310 if (ulIndex == TF_DEFAULT_SELECTION)
315 for (i = 0; i < count; i++)
318 TS_SELECTION_ACP acps;
320 hr = ITextStoreACP_GetSelection(This->pITextStoreACP, ulIndex + i,
323 if (hr == TS_E_NOLOCK)
325 else if (SUCCEEDED(hr))
327 pSelection[totalFetched].style.ase = acps.style.ase;
328 pSelection[totalFetched].style.fInterimChar = acps.style.fInterimChar;
329 Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, acps.acpStart, acps.acpEnd, &pSelection[totalFetched].range);
336 *pcFetched = totalFetched;
341 static HRESULT WINAPI Context_SetSelection (ITfContext *iface,
342 TfEditCookie ec, ULONG ulCount, const TF_SELECTION *pSelection)
344 Context *This = (Context *)iface;
345 FIXME("STUB:(%p)\n",This);
349 static HRESULT WINAPI Context_GetStart (ITfContext *iface,
350 TfEditCookie ec, ITfRange **ppStart)
352 Context *This = (Context *)iface;
354 TRACE("(%p) %i %p\n",This,ec,ppStart);
361 if (!This->connected)
362 return TF_E_DISCONNECTED;
364 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
367 cookie = get_Cookie_data(ec);
368 return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, 0, 0, ppStart);
371 static HRESULT WINAPI Context_GetEnd (ITfContext *iface,
372 TfEditCookie ec, ITfRange **ppEnd)
374 Context *This = (Context *)iface;
377 TRACE("(%p) %i %p\n",This,ec,ppEnd);
384 if (!This->connected)
385 return TF_E_DISCONNECTED;
387 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
390 if (!This->pITextStoreACP)
392 FIXME("Context does not have a ITextStoreACP\n");
396 cookie = get_Cookie_data(ec);
397 ITextStoreACP_GetEndACP(This->pITextStoreACP,&end);
399 return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, end, end, ppEnd);
402 static HRESULT WINAPI Context_GetActiveView (ITfContext *iface,
403 ITfContextView **ppView)
405 Context *This = (Context *)iface;
406 FIXME("STUB:(%p)\n",This);
410 static HRESULT WINAPI Context_EnumViews (ITfContext *iface,
411 IEnumTfContextViews **ppEnum)
413 Context *This = (Context *)iface;
414 FIXME("STUB:(%p)\n",This);
418 static HRESULT WINAPI Context_GetStatus (ITfContext *iface,
421 Context *This = (Context *)iface;
422 FIXME("STUB:(%p)\n",This);
426 static HRESULT WINAPI Context_GetProperty (ITfContext *iface,
427 REFGUID guidProp, ITfProperty **ppProp)
429 Context *This = (Context *)iface;
430 FIXME("STUB:(%p)\n",This);
434 static HRESULT WINAPI Context_GetAppProperty (ITfContext *iface,
435 REFGUID guidProp, ITfReadOnlyProperty **ppProp)
437 Context *This = (Context *)iface;
438 FIXME("STUB:(%p)\n",This);
442 static HRESULT WINAPI Context_TrackProperties (ITfContext *iface,
443 const GUID **prgProp, ULONG cProp, const GUID **prgAppProp,
444 ULONG cAppProp, ITfReadOnlyProperty **ppProperty)
446 Context *This = (Context *)iface;
447 FIXME("STUB:(%p)\n",This);
451 static HRESULT WINAPI Context_EnumProperties (ITfContext *iface,
452 IEnumTfProperties **ppEnum)
454 Context *This = (Context *)iface;
455 FIXME("STUB:(%p)\n",This);
459 static HRESULT WINAPI Context_GetDocumentMgr (ITfContext *iface,
460 ITfDocumentMgr **ppDm)
462 Context *This = (Context *)iface;
463 FIXME("STUB:(%p)\n",This);
467 static HRESULT WINAPI Context_CreateRangeBackup (ITfContext *iface,
468 TfEditCookie ec, ITfRange *pRange, ITfRangeBackup **ppBackup)
470 Context *This = (Context *)iface;
471 FIXME("STUB:(%p)\n",This);
475 static const ITfContextVtbl Context_ContextVtbl =
477 Context_QueryInterface,
481 Context_RequestEditSession,
482 Context_InWriteSession,
483 Context_GetSelection,
484 Context_SetSelection,
487 Context_GetActiveView,
491 Context_GetAppProperty,
492 Context_TrackProperties,
493 Context_EnumProperties,
494 Context_GetDocumentMgr,
495 Context_CreateRangeBackup
498 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
500 Context *This = impl_from_ITfSourceVtbl(iface);
501 return Context_QueryInterface((ITfContext *)This, iid, *ppvOut);
504 static ULONG WINAPI Source_AddRef(ITfSource *iface)
506 Context *This = impl_from_ITfSourceVtbl(iface);
507 return Context_AddRef((ITfContext *)This);
510 static ULONG WINAPI Source_Release(ITfSource *iface)
512 Context *This = impl_from_ITfSourceVtbl(iface);
513 return Context_Release((ITfContext *)This);
516 /*****************************************************
517 * ITfSource functions
518 *****************************************************/
519 static WINAPI HRESULT ContextSource_AdviseSink(ITfSource *iface,
520 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
523 Context *This = impl_from_ITfSourceVtbl(iface);
524 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
526 if (!riid || !punk || !pdwCookie)
529 if (IsEqualIID(riid, &IID_ITfTextEditSink))
531 es = HeapAlloc(GetProcessHeap(),0,sizeof(ContextSink));
533 return E_OUTOFMEMORY;
534 if (FAILED(IUnknown_QueryInterface(punk, riid, (LPVOID *)&es->interfaces.pITfTextEditSink)))
536 HeapFree(GetProcessHeap(),0,es);
537 return CONNECT_E_CANNOTCONNECT;
539 list_add_head(&This->pTextEditSink ,&es->entry);
540 *pdwCookie = generate_Cookie(COOKIE_MAGIC_CONTEXTSINK, es);
544 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
548 TRACE("cookie %x\n",*pdwCookie);
552 static WINAPI HRESULT ContextSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
555 Context *This = impl_from_ITfSourceVtbl(iface);
557 TRACE("(%p) %x\n",This,pdwCookie);
559 if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_CONTEXTSINK)
562 sink = (ContextSink*)remove_Cookie(pdwCookie);
564 return CONNECT_E_NOCONNECTION;
566 list_remove(&sink->entry);
572 static const ITfSourceVtbl Context_SourceVtbl =
574 Source_QueryInterface,
578 ContextSource_AdviseSink,
579 ContextSource_UnadviseSink,
582 HRESULT Context_Constructor(TfClientId tidOwner, IUnknown *punk, ITfContext **ppOut, TfEditCookie *pecTextStore)
587 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Context));
589 return E_OUTOFMEMORY;
591 cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
594 HeapFree(GetProcessHeap(),0,This);
595 return E_OUTOFMEMORY;
598 TRACE("(%p) %x %p %p %p\n",This, tidOwner, punk, ppOut, pecTextStore);
600 This->ContextVtbl= &Context_ContextVtbl;
601 This->SourceVtbl = &Context_SourceVtbl;
603 This->tidOwner = tidOwner;
604 This->connected = FALSE;
606 cookie->lockType = TF_ES_READ;
607 cookie->pOwningContext = This;
611 IUnknown_QueryInterface(punk, &IID_ITextStoreACP,
612 (LPVOID*)&This->pITextStoreACP);
614 IUnknown_QueryInterface(punk, &IID_ITfContextOwnerCompositionSink,
615 (LPVOID*)&This->pITfContextOwnerCompositionSink);
617 if (!This->pITextStoreACP && !This->pITfContextOwnerCompositionSink)
618 FIXME("Unhandled pUnk\n");
621 This->defaultCookie = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE,cookie);
622 *pecTextStore = This->defaultCookie;
624 list_init(&This->pContextKeyEventSink);
625 list_init(&This->pEditTransactionSink);
626 list_init(&This->pStatusSink);
627 list_init(&This->pTextEditSink);
628 list_init(&This->pTextLayoutSink);
630 *ppOut = (ITfContext*)This;
631 TRACE("returning %p\n", This);
636 HRESULT Context_Initialize(ITfContext *iface)
638 Context *This = (Context *)iface;
640 if (This->pITextStoreACP)
642 if (SUCCEEDED(TextStoreACPSink_Constructor(&This->pITextStoreACPSink, This)))
643 ITextStoreACP_AdviseSink(This->pITextStoreACP, &IID_ITextStoreACPSink,
644 (IUnknown*)This->pITextStoreACPSink, TS_AS_ALL_SINKS);
646 This->connected = TRUE;
650 HRESULT Context_Uninitialize(ITfContext *iface)
652 Context *This = (Context *)iface;
654 if (This->pITextStoreACPSink)
656 ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)This->pITextStoreACPSink);
657 if (ITextStoreACPSink_Release(This->pITextStoreACPSink) == 0)
658 This->pITextStoreACPSink = NULL;
660 This->connected = FALSE;
664 /**************************************************************************
666 **************************************************************************/
668 static void TextStoreACPSink_Destructor(TextStoreACPSink *This)
670 TRACE("destroying %p\n", This);
671 HeapFree(GetProcessHeap(),0,This);
674 static HRESULT WINAPI TextStoreACPSink_QueryInterface(ITextStoreACPSink *iface, REFIID iid, LPVOID *ppvOut)
676 TextStoreACPSink *This = (TextStoreACPSink *)iface;
679 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACPSink))
686 IUnknown_AddRef(iface);
690 WARN("unsupported interface: %s\n", debugstr_guid(iid));
691 return E_NOINTERFACE;
694 static ULONG WINAPI TextStoreACPSink_AddRef(ITextStoreACPSink *iface)
696 TextStoreACPSink *This = (TextStoreACPSink *)iface;
697 return InterlockedIncrement(&This->refCount);
700 static ULONG WINAPI TextStoreACPSink_Release(ITextStoreACPSink *iface)
702 TextStoreACPSink *This = (TextStoreACPSink *)iface;
705 ret = InterlockedDecrement(&This->refCount);
707 TextStoreACPSink_Destructor(This);
711 /*****************************************************
712 * ITextStoreACPSink functions
713 *****************************************************/
715 static HRESULT WINAPI TextStoreACPSink_OnTextChange(ITextStoreACPSink *iface,
716 DWORD dwFlags, const TS_TEXTCHANGE *pChange)
718 TextStoreACPSink *This = (TextStoreACPSink *)iface;
719 FIXME("STUB:(%p)\n",This);
723 static HRESULT WINAPI TextStoreACPSink_OnSelectionChange(ITextStoreACPSink *iface)
725 TextStoreACPSink *This = (TextStoreACPSink *)iface;
726 FIXME("STUB:(%p)\n",This);
730 static HRESULT WINAPI TextStoreACPSink_OnLayoutChange(ITextStoreACPSink *iface,
731 TsLayoutCode lcode, TsViewCookie vcView)
733 TextStoreACPSink *This = (TextStoreACPSink *)iface;
734 FIXME("STUB:(%p)\n",This);
738 static HRESULT WINAPI TextStoreACPSink_OnStatusChange(ITextStoreACPSink *iface,
741 TextStoreACPSink *This = (TextStoreACPSink *)iface;
742 FIXME("STUB:(%p)\n",This);
746 static HRESULT WINAPI TextStoreACPSink_OnAttrsChange(ITextStoreACPSink *iface,
747 LONG acpStart, LONG acpEnd, ULONG cAttrs, const TS_ATTRID *paAttrs)
749 TextStoreACPSink *This = (TextStoreACPSink *)iface;
750 FIXME("STUB:(%p)\n",This);
754 static HRESULT WINAPI TextStoreACPSink_OnLockGranted(ITextStoreACPSink *iface,
757 TextStoreACPSink *This = (TextStoreACPSink *)iface;
762 TRACE("(%p) %x\n",This, dwLockFlags);
764 if (!This->pContext || !This->pContext->currentEditSession)
766 ERR("OnLockGranted called on a context without a current edit session\n");
770 cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
772 return E_OUTOFMEMORY;
774 cookie->lockType = dwLockFlags;
775 cookie->pOwningContext = This->pContext;
776 ec = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE, cookie);
778 hr = ITfEditSession_DoEditSession(This->pContext->currentEditSession, ec);
780 ITfEditSession_Release(This->pContext->currentEditSession);
781 This->pContext->currentEditSession = NULL;
783 /* Edit Cookie is only valid during the edit session */
784 cookie = remove_Cookie(ec);
785 HeapFree(GetProcessHeap(),0,cookie);
790 static HRESULT WINAPI TextStoreACPSink_OnStartEditTransaction(ITextStoreACPSink *iface)
792 TextStoreACPSink *This = (TextStoreACPSink *)iface;
793 FIXME("STUB:(%p)\n",This);
797 static HRESULT WINAPI TextStoreACPSink_OnEndEditTransaction(ITextStoreACPSink *iface)
799 TextStoreACPSink *This = (TextStoreACPSink *)iface;
800 FIXME("STUB:(%p)\n",This);
804 static const ITextStoreACPSinkVtbl TextStoreACPSink_TextStoreACPSinkVtbl =
806 TextStoreACPSink_QueryInterface,
807 TextStoreACPSink_AddRef,
808 TextStoreACPSink_Release,
810 TextStoreACPSink_OnTextChange,
811 TextStoreACPSink_OnSelectionChange,
812 TextStoreACPSink_OnLayoutChange,
813 TextStoreACPSink_OnStatusChange,
814 TextStoreACPSink_OnAttrsChange,
815 TextStoreACPSink_OnLockGranted,
816 TextStoreACPSink_OnStartEditTransaction,
817 TextStoreACPSink_OnEndEditTransaction
820 static HRESULT TextStoreACPSink_Constructor(ITextStoreACPSink **ppOut, Context *pContext)
822 TextStoreACPSink *This;
824 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACPSink));
826 return E_OUTOFMEMORY;
828 This->TextStoreACPSinkVtbl= &TextStoreACPSink_TextStoreACPSinkVtbl;
831 This->pContext = pContext;
833 TRACE("returning %p\n", This);
834 *ppOut = (ITextStoreACPSink*)This;