push e1d8a1293d44015bb0894687d02c5c53339996f7
[wine/hacks.git] / dlls / msctf / context.c
blobe5cf602fb977342224a12a9d4d5d1b516daa33ab
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 /* 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; */
68 LONG refCount;
69 BOOL connected;
71 /* Aggregation */
72 ITfCompartmentMgr *CompartmentMgr;
74 TfClientId tidOwner;
75 TfEditCookie defaultCookie;
76 TS_STATUS documentStatus;
77 ITfDocumentMgr *manager;
79 ITextStoreACP *pITextStoreACP;
80 ITfContextOwnerCompositionSink *pITfContextOwnerCompositionSink;
82 ITextStoreACPSink *pITextStoreACPSink;
83 ITfEditSession* currentEditSession;
85 /* kept as separate lists to reduce unnecessary iterations */
86 struct list pContextKeyEventSink;
87 struct list pEditTransactionSink;
88 struct list pStatusSink;
89 struct list pTextEditSink;
90 struct list pTextLayoutSink;
92 } Context;
94 typedef struct tagEditCookie {
95 DWORD lockType;
96 Context *pOwningContext;
97 } EditCookie;
99 typedef struct tagTextStoreACPSink {
100 const ITextStoreACPSinkVtbl *TextStoreACPSinkVtbl;
101 /* const ITextStoreACPServicesVtbl *TextStoreACPServicesVtbl; */
102 LONG refCount;
104 Context *pContext;
105 } TextStoreACPSink;
108 static HRESULT TextStoreACPSink_Constructor(ITextStoreACPSink **ppOut, Context *pContext);
110 static inline Context *impl_from_ITfSourceVtbl(ITfSource *iface)
112 return (Context *)((char *)iface - FIELD_OFFSET(Context,SourceVtbl));
115 static inline Context *impl_from_ITfInsertAtSelectionVtbl(ITfInsertAtSelection*iface)
117 return (Context *)((char *)iface - FIELD_OFFSET(Context,InsertAtSelectionVtbl));
120 static void free_sink(ContextSink *sink)
122 IUnknown_Release(sink->interfaces.pIUnknown);
123 HeapFree(GetProcessHeap(),0,sink);
126 static void Context_Destructor(Context *This)
128 struct list *cursor, *cursor2;
129 EditCookie *cookie;
130 TRACE("destroying %p\n", This);
132 if (This->pITextStoreACPSink)
134 ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)This->pITextStoreACPSink);
135 ITextStoreACPSink_Release(This->pITextStoreACPSink);
138 if (This->pITextStoreACP)
139 ITextStoreACPSink_Release(This->pITextStoreACP);
141 if (This->pITfContextOwnerCompositionSink)
142 ITextStoreACPSink_Release(This->pITfContextOwnerCompositionSink);
144 if (This->defaultCookie)
146 cookie = remove_Cookie(This->defaultCookie);
147 HeapFree(GetProcessHeap(),0,cookie);
148 This->defaultCookie = 0;
151 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pContextKeyEventSink)
153 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
154 list_remove(cursor);
155 free_sink(sink);
157 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pEditTransactionSink)
159 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
160 list_remove(cursor);
161 free_sink(sink);
163 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pStatusSink)
165 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
166 list_remove(cursor);
167 free_sink(sink);
169 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pTextEditSink)
171 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
172 list_remove(cursor);
173 free_sink(sink);
175 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->pTextLayoutSink)
177 ContextSink* sink = LIST_ENTRY(cursor,ContextSink,entry);
178 list_remove(cursor);
179 free_sink(sink);
182 CompartmentMgr_Destructor(This->CompartmentMgr);
183 HeapFree(GetProcessHeap(),0,This);
186 static HRESULT WINAPI Context_QueryInterface(ITfContext *iface, REFIID iid, LPVOID *ppvOut)
188 Context *This = (Context *)iface;
189 *ppvOut = NULL;
191 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfContext))
193 *ppvOut = This;
195 else if (IsEqualIID(iid, &IID_ITfSource))
197 *ppvOut = &This->SourceVtbl;
199 else if (IsEqualIID(iid, &IID_ITfInsertAtSelection))
201 *ppvOut = &This->InsertAtSelectionVtbl;
203 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
205 *ppvOut = This->CompartmentMgr;
208 if (*ppvOut)
210 IUnknown_AddRef(iface);
211 return S_OK;
214 WARN("unsupported interface: %s\n", debugstr_guid(iid));
215 return E_NOINTERFACE;
218 static ULONG WINAPI Context_AddRef(ITfContext *iface)
220 Context *This = (Context *)iface;
221 return InterlockedIncrement(&This->refCount);
224 static ULONG WINAPI Context_Release(ITfContext *iface)
226 Context *This = (Context *)iface;
227 ULONG ret;
229 ret = InterlockedDecrement(&This->refCount);
230 if (ret == 0)
231 Context_Destructor(This);
232 return ret;
235 /*****************************************************
236 * ITfContext functions
237 *****************************************************/
238 static HRESULT WINAPI Context_RequestEditSession (ITfContext *iface,
239 TfClientId tid, ITfEditSession *pes, DWORD dwFlags,
240 HRESULT *phrSession)
242 HRESULT hr;
243 Context *This = (Context *)iface;
244 DWORD dwLockFlags = 0x0;
246 TRACE("(%p) %i %p %x %p\n",This, tid, pes, dwFlags, phrSession);
248 if (!(dwFlags & TF_ES_READ) && !(dwFlags & TF_ES_READWRITE))
250 *phrSession = E_FAIL;
251 return E_INVALIDARG;
254 if (!This->pITextStoreACP)
256 FIXME("No ITextStoreACP avaliable\n");
257 *phrSession = E_FAIL;
258 return E_FAIL;
261 if (!(dwFlags & TF_ES_ASYNC))
262 dwLockFlags |= TS_LF_SYNC;
264 if ((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE)
265 dwLockFlags |= TS_LF_READWRITE;
266 else if (dwFlags & TF_ES_READ)
267 dwLockFlags |= TS_LF_READ;
269 if (!This->documentStatus.dwDynamicFlags)
270 ITextStoreACP_GetStatus(This->pITextStoreACP, &This->documentStatus);
272 if (((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE) && (This->documentStatus.dwDynamicFlags & TS_SD_READONLY))
274 *phrSession = TS_E_READONLY;
275 return S_OK;
278 if (FAILED (ITfEditSession_QueryInterface(pes, &IID_ITfEditSession, (LPVOID*)&This->currentEditSession)))
280 *phrSession = E_FAIL;
281 return E_INVALIDARG;
284 hr = ITextStoreACP_RequestLock(This->pITextStoreACP, dwLockFlags, phrSession);
286 return hr;
289 static HRESULT WINAPI Context_InWriteSession (ITfContext *iface,
290 TfClientId tid,
291 BOOL *pfWriteSession)
293 Context *This = (Context *)iface;
294 FIXME("STUB:(%p)\n",This);
295 return E_NOTIMPL;
298 static HRESULT WINAPI Context_GetSelection (ITfContext *iface,
299 TfEditCookie ec, ULONG ulIndex, ULONG ulCount,
300 TF_SELECTION *pSelection, ULONG *pcFetched)
302 Context *This = (Context *)iface;
303 EditCookie *cookie;
304 ULONG count, i;
305 ULONG totalFetched = 0;
306 HRESULT hr = S_OK;
308 if (!pSelection || !pcFetched)
309 return E_INVALIDARG;
311 *pcFetched = 0;
313 if (!This->connected)
314 return TF_E_DISCONNECTED;
316 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
317 return TF_E_NOLOCK;
319 if (!This->pITextStoreACP)
321 FIXME("Context does not have a ITextStoreACP\n");
322 return E_NOTIMPL;
325 cookie = get_Cookie_data(ec);
327 if (ulIndex == TF_DEFAULT_SELECTION)
328 count = 1;
329 else
330 count = ulCount;
332 for (i = 0; i < count; i++)
334 DWORD fetched;
335 TS_SELECTION_ACP acps;
337 hr = ITextStoreACP_GetSelection(This->pITextStoreACP, ulIndex + i,
338 1, &acps, &fetched);
340 if (hr == TS_E_NOLOCK)
341 return TF_E_NOLOCK;
342 else if (SUCCEEDED(hr))
344 pSelection[totalFetched].style.ase = acps.style.ase;
345 pSelection[totalFetched].style.fInterimChar = acps.style.fInterimChar;
346 Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, acps.acpStart, acps.acpEnd, &pSelection[totalFetched].range);
347 totalFetched ++;
349 else
350 break;
353 *pcFetched = totalFetched;
355 return hr;
358 static HRESULT WINAPI Context_SetSelection (ITfContext *iface,
359 TfEditCookie ec, ULONG ulCount, const TF_SELECTION *pSelection)
361 TS_SELECTION_ACP *acp;
362 Context *This = (Context *)iface;
363 INT i;
364 HRESULT hr;
366 TRACE("(%p) %i %i %p\n",This,ec,ulCount,pSelection);
368 if (!This->pITextStoreACP)
370 FIXME("Context does not have a ITextStoreACP\n");
371 return E_NOTIMPL;
374 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
375 return TF_E_NOLOCK;
377 acp = HeapAlloc(GetProcessHeap(), 0, sizeof(TS_SELECTION_ACP) * ulCount);
378 if (!acp)
379 return E_OUTOFMEMORY;
381 for (i = 0; i < ulCount; i++)
382 if (FAILED(TF_SELECTION_to_TS_SELECTION_ACP(&pSelection[i], &acp[i])))
384 TRACE("Selection Conversion Failed\n");
385 HeapFree(GetProcessHeap(), 0 , acp);
386 return E_FAIL;
389 hr = ITextStoreACP_SetSelection(This->pITextStoreACP, ulCount, acp);
391 HeapFree(GetProcessHeap(), 0, acp);
393 return hr;
396 static HRESULT WINAPI Context_GetStart (ITfContext *iface,
397 TfEditCookie ec, ITfRange **ppStart)
399 Context *This = (Context *)iface;
400 EditCookie *cookie;
401 TRACE("(%p) %i %p\n",This,ec,ppStart);
403 if (!ppStart)
404 return E_INVALIDARG;
406 *ppStart = NULL;
408 if (!This->connected)
409 return TF_E_DISCONNECTED;
411 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
412 return TF_E_NOLOCK;
414 cookie = get_Cookie_data(ec);
415 return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, 0, 0, ppStart);
418 static HRESULT WINAPI Context_GetEnd (ITfContext *iface,
419 TfEditCookie ec, ITfRange **ppEnd)
421 Context *This = (Context *)iface;
422 EditCookie *cookie;
423 LONG end;
424 TRACE("(%p) %i %p\n",This,ec,ppEnd);
426 if (!ppEnd)
427 return E_INVALIDARG;
429 *ppEnd = NULL;
431 if (!This->connected)
432 return TF_E_DISCONNECTED;
434 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
435 return TF_E_NOLOCK;
437 if (!This->pITextStoreACP)
439 FIXME("Context does not have a ITextStoreACP\n");
440 return E_NOTIMPL;
443 cookie = get_Cookie_data(ec);
444 ITextStoreACP_GetEndACP(This->pITextStoreACP,&end);
446 return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, end, end, ppEnd);
449 static HRESULT WINAPI Context_GetActiveView (ITfContext *iface,
450 ITfContextView **ppView)
452 Context *This = (Context *)iface;
453 FIXME("STUB:(%p)\n",This);
454 return E_NOTIMPL;
457 static HRESULT WINAPI Context_EnumViews (ITfContext *iface,
458 IEnumTfContextViews **ppEnum)
460 Context *This = (Context *)iface;
461 FIXME("STUB:(%p)\n",This);
462 return E_NOTIMPL;
465 static HRESULT WINAPI Context_GetStatus (ITfContext *iface,
466 TF_STATUS *pdcs)
468 Context *This = (Context *)iface;
469 FIXME("STUB:(%p)\n",This);
470 return E_NOTIMPL;
473 static HRESULT WINAPI Context_GetProperty (ITfContext *iface,
474 REFGUID guidProp, ITfProperty **ppProp)
476 Context *This = (Context *)iface;
477 FIXME("STUB:(%p)\n",This);
478 return E_NOTIMPL;
481 static HRESULT WINAPI Context_GetAppProperty (ITfContext *iface,
482 REFGUID guidProp, ITfReadOnlyProperty **ppProp)
484 Context *This = (Context *)iface;
485 FIXME("STUB:(%p)\n",This);
486 return E_NOTIMPL;
489 static HRESULT WINAPI Context_TrackProperties (ITfContext *iface,
490 const GUID **prgProp, ULONG cProp, const GUID **prgAppProp,
491 ULONG cAppProp, ITfReadOnlyProperty **ppProperty)
493 Context *This = (Context *)iface;
494 FIXME("STUB:(%p)\n",This);
495 return E_NOTIMPL;
498 static HRESULT WINAPI Context_EnumProperties (ITfContext *iface,
499 IEnumTfProperties **ppEnum)
501 Context *This = (Context *)iface;
502 FIXME("STUB:(%p)\n",This);
503 return E_NOTIMPL;
506 static HRESULT WINAPI Context_GetDocumentMgr (ITfContext *iface,
507 ITfDocumentMgr **ppDm)
509 Context *This = (Context *)iface;
510 TRACE("(%p) %p\n",This,ppDm);
512 if (!ppDm)
513 return E_INVALIDARG;
515 *ppDm = This->manager;
516 if (!This->manager)
517 return S_FALSE;
518 return S_OK;
521 static HRESULT WINAPI Context_CreateRangeBackup (ITfContext *iface,
522 TfEditCookie ec, ITfRange *pRange, ITfRangeBackup **ppBackup)
524 Context *This = (Context *)iface;
525 FIXME("STUB:(%p)\n",This);
526 return E_NOTIMPL;
529 static const ITfContextVtbl Context_ContextVtbl =
531 Context_QueryInterface,
532 Context_AddRef,
533 Context_Release,
535 Context_RequestEditSession,
536 Context_InWriteSession,
537 Context_GetSelection,
538 Context_SetSelection,
539 Context_GetStart,
540 Context_GetEnd,
541 Context_GetActiveView,
542 Context_EnumViews,
543 Context_GetStatus,
544 Context_GetProperty,
545 Context_GetAppProperty,
546 Context_TrackProperties,
547 Context_EnumProperties,
548 Context_GetDocumentMgr,
549 Context_CreateRangeBackup
552 static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
554 Context *This = impl_from_ITfSourceVtbl(iface);
555 return Context_QueryInterface((ITfContext *)This, iid, *ppvOut);
558 static ULONG WINAPI Source_AddRef(ITfSource *iface)
560 Context *This = impl_from_ITfSourceVtbl(iface);
561 return Context_AddRef((ITfContext *)This);
564 static ULONG WINAPI Source_Release(ITfSource *iface)
566 Context *This = impl_from_ITfSourceVtbl(iface);
567 return Context_Release((ITfContext *)This);
570 /*****************************************************
571 * ITfSource functions
572 *****************************************************/
573 static WINAPI HRESULT ContextSource_AdviseSink(ITfSource *iface,
574 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
576 ContextSink *es;
577 Context *This = impl_from_ITfSourceVtbl(iface);
578 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
580 if (!riid || !punk || !pdwCookie)
581 return E_INVALIDARG;
583 if (IsEqualIID(riid, &IID_ITfTextEditSink))
585 es = HeapAlloc(GetProcessHeap(),0,sizeof(ContextSink));
586 if (!es)
587 return E_OUTOFMEMORY;
588 if (FAILED(IUnknown_QueryInterface(punk, riid, (LPVOID *)&es->interfaces.pITfTextEditSink)))
590 HeapFree(GetProcessHeap(),0,es);
591 return CONNECT_E_CANNOTCONNECT;
593 list_add_head(&This->pTextEditSink ,&es->entry);
594 *pdwCookie = generate_Cookie(COOKIE_MAGIC_CONTEXTSINK, es);
596 else
598 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
599 return E_NOTIMPL;
602 TRACE("cookie %x\n",*pdwCookie);
603 return S_OK;
606 static WINAPI HRESULT ContextSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
608 ContextSink *sink;
609 Context *This = impl_from_ITfSourceVtbl(iface);
611 TRACE("(%p) %x\n",This,pdwCookie);
613 if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_CONTEXTSINK)
614 return E_INVALIDARG;
616 sink = (ContextSink*)remove_Cookie(pdwCookie);
617 if (!sink)
618 return CONNECT_E_NOCONNECTION;
620 list_remove(&sink->entry);
621 free_sink(sink);
623 return S_OK;
626 static const ITfSourceVtbl Context_SourceVtbl =
628 Source_QueryInterface,
629 Source_AddRef,
630 Source_Release,
632 ContextSource_AdviseSink,
633 ContextSource_UnadviseSink,
636 /*****************************************************
637 * ITfInsertAtSelection functions
638 *****************************************************/
639 static HRESULT WINAPI InsertAtSelection_QueryInterface(ITfInsertAtSelection *iface, REFIID iid, LPVOID *ppvOut)
641 Context *This = impl_from_ITfInsertAtSelectionVtbl(iface);
642 return Context_QueryInterface((ITfContext *)This, iid, *ppvOut);
645 static ULONG WINAPI InsertAtSelection_AddRef(ITfInsertAtSelection *iface)
647 Context *This = impl_from_ITfInsertAtSelectionVtbl(iface);
648 return Context_AddRef((ITfContext *)This);
651 static ULONG WINAPI InsertAtSelection_Release(ITfInsertAtSelection *iface)
653 Context *This = impl_from_ITfInsertAtSelectionVtbl(iface);
654 return Context_Release((ITfContext *)This);
657 static WINAPI HRESULT InsertAtSelection_InsertTextAtSelection(
658 ITfInsertAtSelection *iface, TfEditCookie ec, DWORD dwFlags,
659 const WCHAR *pchText, LONG cch, ITfRange **ppRange)
661 Context *This = impl_from_ITfInsertAtSelectionVtbl(iface);
662 EditCookie *cookie;
663 LONG acpStart, acpEnd;
664 TS_TEXTCHANGE change;
665 HRESULT hr;
667 TRACE("(%p) %i %x %s %p\n",This, ec, dwFlags, debugstr_wn(pchText,cch), ppRange);
669 if (!This->connected)
670 return TF_E_DISCONNECTED;
672 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
673 return TF_E_NOLOCK;
675 cookie = get_Cookie_data(ec);
677 if ((cookie->lockType & TS_LF_READWRITE) != TS_LF_READWRITE )
678 return TS_E_READONLY;
680 if (!This->pITextStoreACP)
682 FIXME("Context does not have a ITextStoreACP\n");
683 return E_NOTIMPL;
686 hr = ITextStoreACP_InsertTextAtSelection(This->pITextStoreACP, dwFlags, pchText, cch, &acpStart, &acpEnd, &change);
687 if (SUCCEEDED(hr))
688 Range_Constructor((ITfContext*)This, This->pITextStoreACP, cookie->lockType, change.acpStart, change.acpNewEnd, ppRange);
690 return hr;
693 static WINAPI HRESULT InsertAtSelection_InsertEmbeddedAtSelection(
694 ITfInsertAtSelection *iface, TfEditCookie ec, DWORD dwFlags,
695 IDataObject *pDataObject, ITfRange **ppRange)
697 Context *This = impl_from_ITfInsertAtSelectionVtbl(iface);
698 FIXME("STUB:(%p)\n",This);
699 return E_NOTIMPL;
702 static const ITfInsertAtSelectionVtbl Context_InsertAtSelectionVtbl =
704 InsertAtSelection_QueryInterface,
705 InsertAtSelection_AddRef,
706 InsertAtSelection_Release,
708 InsertAtSelection_InsertTextAtSelection,
709 InsertAtSelection_InsertEmbeddedAtSelection,
712 HRESULT Context_Constructor(TfClientId tidOwner, IUnknown *punk, ITfDocumentMgr *mgr, ITfContext **ppOut, TfEditCookie *pecTextStore)
714 Context *This;
715 EditCookie *cookie;
717 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Context));
718 if (This == NULL)
719 return E_OUTOFMEMORY;
721 cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
722 if (cookie == NULL)
724 HeapFree(GetProcessHeap(),0,This);
725 return E_OUTOFMEMORY;
728 TRACE("(%p) %x %p %p %p\n",This, tidOwner, punk, ppOut, pecTextStore);
730 This->ContextVtbl= &Context_ContextVtbl;
731 This->SourceVtbl = &Context_SourceVtbl;
732 This->InsertAtSelectionVtbl = &Context_InsertAtSelectionVtbl;
733 This->refCount = 1;
734 This->tidOwner = tidOwner;
735 This->connected = FALSE;
736 This->manager = mgr;
738 CompartmentMgr_Constructor((IUnknown*)This, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
740 cookie->lockType = TF_ES_READ;
741 cookie->pOwningContext = This;
743 if (punk)
745 IUnknown_QueryInterface(punk, &IID_ITextStoreACP,
746 (LPVOID*)&This->pITextStoreACP);
748 IUnknown_QueryInterface(punk, &IID_ITfContextOwnerCompositionSink,
749 (LPVOID*)&This->pITfContextOwnerCompositionSink);
751 if (!This->pITextStoreACP && !This->pITfContextOwnerCompositionSink)
752 FIXME("Unhandled pUnk\n");
755 This->defaultCookie = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE,cookie);
756 *pecTextStore = This->defaultCookie;
758 list_init(&This->pContextKeyEventSink);
759 list_init(&This->pEditTransactionSink);
760 list_init(&This->pStatusSink);
761 list_init(&This->pTextEditSink);
762 list_init(&This->pTextLayoutSink);
764 *ppOut = (ITfContext*)This;
765 TRACE("returning %p\n", This);
767 return S_OK;
770 HRESULT Context_Initialize(ITfContext *iface, ITfDocumentMgr *manager)
772 Context *This = (Context *)iface;
774 if (This->pITextStoreACP)
776 if (SUCCEEDED(TextStoreACPSink_Constructor(&This->pITextStoreACPSink, This)))
777 ITextStoreACP_AdviseSink(This->pITextStoreACP, &IID_ITextStoreACPSink,
778 (IUnknown*)This->pITextStoreACPSink, TS_AS_ALL_SINKS);
780 This->connected = TRUE;
781 This->manager = manager;
782 return S_OK;
785 HRESULT Context_Uninitialize(ITfContext *iface)
787 Context *This = (Context *)iface;
789 if (This->pITextStoreACPSink)
791 ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)This->pITextStoreACPSink);
792 if (ITextStoreACPSink_Release(This->pITextStoreACPSink) == 0)
793 This->pITextStoreACPSink = NULL;
795 This->connected = FALSE;
796 This->manager = NULL;
797 return S_OK;
800 /**************************************************************************
801 * ITextStoreACPSink
802 **************************************************************************/
804 static void TextStoreACPSink_Destructor(TextStoreACPSink *This)
806 TRACE("destroying %p\n", This);
807 HeapFree(GetProcessHeap(),0,This);
810 static HRESULT WINAPI TextStoreACPSink_QueryInterface(ITextStoreACPSink *iface, REFIID iid, LPVOID *ppvOut)
812 TextStoreACPSink *This = (TextStoreACPSink *)iface;
813 *ppvOut = NULL;
815 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACPSink))
817 *ppvOut = This;
820 if (*ppvOut)
822 IUnknown_AddRef(iface);
823 return S_OK;
826 WARN("unsupported interface: %s\n", debugstr_guid(iid));
827 return E_NOINTERFACE;
830 static ULONG WINAPI TextStoreACPSink_AddRef(ITextStoreACPSink *iface)
832 TextStoreACPSink *This = (TextStoreACPSink *)iface;
833 return InterlockedIncrement(&This->refCount);
836 static ULONG WINAPI TextStoreACPSink_Release(ITextStoreACPSink *iface)
838 TextStoreACPSink *This = (TextStoreACPSink *)iface;
839 ULONG ret;
841 ret = InterlockedDecrement(&This->refCount);
842 if (ret == 0)
843 TextStoreACPSink_Destructor(This);
844 return ret;
847 /*****************************************************
848 * ITextStoreACPSink functions
849 *****************************************************/
851 static HRESULT WINAPI TextStoreACPSink_OnTextChange(ITextStoreACPSink *iface,
852 DWORD dwFlags, const TS_TEXTCHANGE *pChange)
854 TextStoreACPSink *This = (TextStoreACPSink *)iface;
855 FIXME("STUB:(%p)\n",This);
856 return E_NOTIMPL;
859 static HRESULT WINAPI TextStoreACPSink_OnSelectionChange(ITextStoreACPSink *iface)
861 TextStoreACPSink *This = (TextStoreACPSink *)iface;
862 FIXME("STUB:(%p)\n",This);
863 return E_NOTIMPL;
866 static HRESULT WINAPI TextStoreACPSink_OnLayoutChange(ITextStoreACPSink *iface,
867 TsLayoutCode lcode, TsViewCookie vcView)
869 TextStoreACPSink *This = (TextStoreACPSink *)iface;
870 FIXME("STUB:(%p)\n",This);
871 return E_NOTIMPL;
874 static HRESULT WINAPI TextStoreACPSink_OnStatusChange(ITextStoreACPSink *iface,
875 DWORD dwFlags)
877 TextStoreACPSink *This = (TextStoreACPSink *)iface;
878 HRESULT hr, hrSession;
880 TRACE("(%p) %x\n",This, dwFlags);
882 if (!This->pContext)
884 ERR("No context?\n");
885 return E_FAIL;
888 if (!This->pContext->pITextStoreACP)
890 FIXME("Context does not have a ITextStoreACP\n");
891 return E_NOTIMPL;
894 hr = ITextStoreACP_RequestLock(This->pContext->pITextStoreACP, TS_LF_READ, &hrSession);
896 if(SUCCEEDED(hr) && SUCCEEDED(hrSession))
897 This->pContext->documentStatus.dwDynamicFlags = dwFlags;
899 return S_OK;
902 static HRESULT WINAPI TextStoreACPSink_OnAttrsChange(ITextStoreACPSink *iface,
903 LONG acpStart, LONG acpEnd, ULONG cAttrs, const TS_ATTRID *paAttrs)
905 TextStoreACPSink *This = (TextStoreACPSink *)iface;
906 FIXME("STUB:(%p)\n",This);
907 return E_NOTIMPL;
910 static HRESULT WINAPI TextStoreACPSink_OnLockGranted(ITextStoreACPSink *iface,
911 DWORD dwLockFlags)
913 TextStoreACPSink *This = (TextStoreACPSink *)iface;
914 HRESULT hr;
915 EditCookie *cookie;
916 TfEditCookie ec;
918 TRACE("(%p) %x\n",This, dwLockFlags);
920 if (!This->pContext)
922 ERR("OnLockGranted called without a context\n");
923 return E_FAIL;
926 if (!This->pContext->currentEditSession)
928 FIXME("OnLockGranted called for something other than an EditSession\n");
929 return S_OK;
932 cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
933 if (!cookie)
934 return E_OUTOFMEMORY;
936 cookie->lockType = dwLockFlags;
937 cookie->pOwningContext = This->pContext;
938 ec = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE, cookie);
940 hr = ITfEditSession_DoEditSession(This->pContext->currentEditSession, ec);
942 ITfEditSession_Release(This->pContext->currentEditSession);
943 This->pContext->currentEditSession = NULL;
945 /* Edit Cookie is only valid during the edit session */
946 cookie = remove_Cookie(ec);
947 HeapFree(GetProcessHeap(),0,cookie);
949 return hr;
952 static HRESULT WINAPI TextStoreACPSink_OnStartEditTransaction(ITextStoreACPSink *iface)
954 TextStoreACPSink *This = (TextStoreACPSink *)iface;
955 FIXME("STUB:(%p)\n",This);
956 return E_NOTIMPL;
959 static HRESULT WINAPI TextStoreACPSink_OnEndEditTransaction(ITextStoreACPSink *iface)
961 TextStoreACPSink *This = (TextStoreACPSink *)iface;
962 FIXME("STUB:(%p)\n",This);
963 return E_NOTIMPL;
966 static const ITextStoreACPSinkVtbl TextStoreACPSink_TextStoreACPSinkVtbl =
968 TextStoreACPSink_QueryInterface,
969 TextStoreACPSink_AddRef,
970 TextStoreACPSink_Release,
972 TextStoreACPSink_OnTextChange,
973 TextStoreACPSink_OnSelectionChange,
974 TextStoreACPSink_OnLayoutChange,
975 TextStoreACPSink_OnStatusChange,
976 TextStoreACPSink_OnAttrsChange,
977 TextStoreACPSink_OnLockGranted,
978 TextStoreACPSink_OnStartEditTransaction,
979 TextStoreACPSink_OnEndEditTransaction
982 static HRESULT TextStoreACPSink_Constructor(ITextStoreACPSink **ppOut, Context *pContext)
984 TextStoreACPSink *This;
986 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(TextStoreACPSink));
987 if (This == NULL)
988 return E_OUTOFMEMORY;
990 This->TextStoreACPSinkVtbl= &TextStoreACPSink_TextStoreACPSinkVtbl;
991 This->refCount = 1;
993 This->pContext = pContext;
995 TRACE("returning %p\n", This);
996 *ppOut = (ITextStoreACPSink*)This;
997 return S_OK;