[MSCMS] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / win32 / msctf / context.c
1 /*
2 * ITfContext implementation
3 *
4 * Copyright 2009 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 "msctf_internal.h"
22
23 typedef struct tagContext {
24 ITfContext ITfContext_iface;
25 ITfSource ITfSource_iface;
26 /* const ITfContextCompositionVtbl *ContextCompositionVtbl; */
27 /* const ITfContextOwnerCompositionServicesVtbl *ContextOwnerCompositionServicesVtbl; */
28 /* const ITfContextOwnerServicesVtbl *ContextOwnerServicesVtbl; */
29 ITfInsertAtSelection ITfInsertAtSelection_iface;
30 /* const ITfMouseTrackerVtbl *MouseTrackerVtbl; */
31 /* const ITfQueryEmbeddedVtbl *QueryEmbeddedVtbl; */
32 ITfSourceSingle ITfSourceSingle_iface;
33 ITextStoreACPSink ITextStoreACPSink_iface;
34 ITextStoreACPServices ITextStoreACPServices_iface;
35 LONG refCount;
36 BOOL connected;
37
38 /* Aggregation */
39 ITfCompartmentMgr *CompartmentMgr;
40
41 TfClientId tidOwner;
42 TfEditCookie defaultCookie;
43 TS_STATUS documentStatus;
44 ITfDocumentMgr *manager;
45
46 ITextStoreACP *pITextStoreACP;
47 ITfContextOwnerCompositionSink *pITfContextOwnerCompositionSink;
48
49 ITfEditSession* currentEditSession;
50
51 /* kept as separate lists to reduce unnecessary iterations */
52 struct list pContextKeyEventSink;
53 struct list pEditTransactionSink;
54 struct list pStatusSink;
55 struct list pTextEditSink;
56 struct list pTextLayoutSink;
57
58 } Context;
59
60 typedef struct tagEditCookie {
61 DWORD lockType;
62 Context *pOwningContext;
63 } EditCookie;
64
65 static inline Context *impl_from_ITfContext(ITfContext *iface)
66 {
67 return CONTAINING_RECORD(iface, Context, ITfContext_iface);
68 }
69
70 static inline Context *impl_from_ITfSource(ITfSource *iface)
71 {
72 return CONTAINING_RECORD(iface, Context, ITfSource_iface);
73 }
74
75 static inline Context *impl_from_ITfInsertAtSelection(ITfInsertAtSelection *iface)
76 {
77 return CONTAINING_RECORD(iface, Context, ITfInsertAtSelection_iface);
78 }
79
80 static inline Context *impl_from_ITfSourceSingle(ITfSourceSingle* iface)
81 {
82 return CONTAINING_RECORD(iface, Context, ITfSourceSingle_iface);
83 }
84
85 static inline Context *impl_from_ITextStoreACPSink(ITextStoreACPSink *iface)
86 {
87 return CONTAINING_RECORD(iface, Context, ITextStoreACPSink_iface);
88 }
89
90 static inline Context *impl_from_ITextStoreACPServices(ITextStoreACPServices *iface)
91 {
92 return CONTAINING_RECORD(iface, Context, ITextStoreACPServices_iface);
93 }
94
95 static void Context_Destructor(Context *This)
96 {
97 EditCookie *cookie;
98 TRACE("destroying %p\n", This);
99
100 if (This->pITextStoreACP)
101 ITextStoreACP_Release(This->pITextStoreACP);
102
103 if (This->pITfContextOwnerCompositionSink)
104 ITfContextOwnerCompositionSink_Release(This->pITfContextOwnerCompositionSink);
105
106 if (This->defaultCookie)
107 {
108 cookie = remove_Cookie(This->defaultCookie);
109 HeapFree(GetProcessHeap(),0,cookie);
110 This->defaultCookie = 0;
111 }
112
113 free_sinks(&This->pContextKeyEventSink);
114 free_sinks(&This->pEditTransactionSink);
115 free_sinks(&This->pStatusSink);
116 free_sinks(&This->pTextEditSink);
117 free_sinks(&This->pTextLayoutSink);
118
119 CompartmentMgr_Destructor(This->CompartmentMgr);
120 HeapFree(GetProcessHeap(),0,This);
121 }
122
123 static HRESULT WINAPI Context_QueryInterface(ITfContext *iface, REFIID iid, LPVOID *ppvOut)
124 {
125 Context *This = impl_from_ITfContext(iface);
126 *ppvOut = NULL;
127
128 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfContext))
129 {
130 *ppvOut = &This->ITfContext_iface;
131 }
132 else if (IsEqualIID(iid, &IID_ITfSource))
133 {
134 *ppvOut = &This->ITfSource_iface;
135 }
136 else if (IsEqualIID(iid, &IID_ITfInsertAtSelection))
137 {
138 *ppvOut = &This->ITfInsertAtSelection_iface;
139 }
140 else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
141 {
142 *ppvOut = This->CompartmentMgr;
143 }
144 else if (IsEqualIID(iid, &IID_ITfSourceSingle))
145 {
146 *ppvOut = &This->ITfSourceSingle_iface;
147 }
148
149 if (*ppvOut)
150 {
151 ITfContext_AddRef(iface);
152 return S_OK;
153 }
154
155 WARN("unsupported interface: %s\n", debugstr_guid(iid));
156 return E_NOINTERFACE;
157 }
158
159 static ULONG WINAPI Context_AddRef(ITfContext *iface)
160 {
161 Context *This = impl_from_ITfContext(iface);
162 return InterlockedIncrement(&This->refCount);
163 }
164
165 static ULONG WINAPI Context_Release(ITfContext *iface)
166 {
167 Context *This = impl_from_ITfContext(iface);
168 ULONG ret;
169
170 ret = InterlockedDecrement(&This->refCount);
171 if (ret == 0)
172 Context_Destructor(This);
173 return ret;
174 }
175
176 /*****************************************************
177 * ITfContext functions
178 *****************************************************/
179 static HRESULT WINAPI Context_RequestEditSession (ITfContext *iface,
180 TfClientId tid, ITfEditSession *pes, DWORD dwFlags,
181 HRESULT *phrSession)
182 {
183 Context *This = impl_from_ITfContext(iface);
184 HRESULT hr;
185 DWORD dwLockFlags = 0x0;
186
187 TRACE("(%p) %i %p %x %p\n",This, tid, pes, dwFlags, phrSession);
188
189 if (!(dwFlags & TF_ES_READ) && !(dwFlags & TF_ES_READWRITE))
190 {
191 *phrSession = E_FAIL;
192 return E_INVALIDARG;
193 }
194
195 if (!This->pITextStoreACP)
196 {
197 FIXME("No ITextStoreACP available\n");
198 *phrSession = E_FAIL;
199 return E_FAIL;
200 }
201
202 if (!(dwFlags & TF_ES_ASYNC))
203 dwLockFlags |= TS_LF_SYNC;
204
205 if ((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE)
206 dwLockFlags |= TS_LF_READWRITE;
207 else if (dwFlags & TF_ES_READ)
208 dwLockFlags |= TS_LF_READ;
209
210 if (!This->documentStatus.dwDynamicFlags)
211 ITextStoreACP_GetStatus(This->pITextStoreACP, &This->documentStatus);
212
213 if (((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE) && (This->documentStatus.dwDynamicFlags & TS_SD_READONLY))
214 {
215 *phrSession = TS_E_READONLY;
216 return S_OK;
217 }
218
219 if (FAILED (ITfEditSession_QueryInterface(pes, &IID_ITfEditSession, (LPVOID*)&This->currentEditSession)))
220 {
221 *phrSession = E_FAIL;
222 return E_INVALIDARG;
223 }
224
225 hr = ITextStoreACP_RequestLock(This->pITextStoreACP, dwLockFlags, phrSession);
226
227 return hr;
228 }
229
230 static HRESULT WINAPI Context_InWriteSession (ITfContext *iface,
231 TfClientId tid,
232 BOOL *pfWriteSession)
233 {
234 Context *This = impl_from_ITfContext(iface);
235 FIXME("STUB:(%p)\n",This);
236 return E_NOTIMPL;
237 }
238
239 static HRESULT WINAPI Context_GetSelection (ITfContext *iface,
240 TfEditCookie ec, ULONG ulIndex, ULONG ulCount,
241 TF_SELECTION *pSelection, ULONG *pcFetched)
242 {
243 Context *This = impl_from_ITfContext(iface);
244 EditCookie *cookie;
245 ULONG count, i;
246 ULONG totalFetched = 0;
247 HRESULT hr = S_OK;
248
249 if (!pSelection || !pcFetched)
250 return E_INVALIDARG;
251
252 *pcFetched = 0;
253
254 if (!This->connected)
255 return TF_E_DISCONNECTED;
256
257 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
258 return TF_E_NOLOCK;
259
260 if (!This->pITextStoreACP)
261 {
262 FIXME("Context does not have a ITextStoreACP\n");
263 return E_NOTIMPL;
264 }
265
266 cookie = get_Cookie_data(ec);
267
268 if (ulIndex == TF_DEFAULT_SELECTION)
269 count = 1;
270 else
271 count = ulCount;
272
273 for (i = 0; i < count; i++)
274 {
275 DWORD fetched;
276 TS_SELECTION_ACP acps;
277
278 hr = ITextStoreACP_GetSelection(This->pITextStoreACP, ulIndex + i,
279 1, &acps, &fetched);
280
281 if (hr == TS_E_NOLOCK)
282 return TF_E_NOLOCK;
283 else if (SUCCEEDED(hr))
284 {
285 pSelection[totalFetched].style.ase = acps.style.ase;
286 pSelection[totalFetched].style.fInterimChar = acps.style.fInterimChar;
287 Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, acps.acpStart, acps.acpEnd, &pSelection[totalFetched].range);
288 totalFetched ++;
289 }
290 else
291 break;
292 }
293
294 *pcFetched = totalFetched;
295
296 return hr;
297 }
298
299 static HRESULT WINAPI Context_SetSelection (ITfContext *iface,
300 TfEditCookie ec, ULONG ulCount, const TF_SELECTION *pSelection)
301 {
302 Context *This = impl_from_ITfContext(iface);
303 TS_SELECTION_ACP *acp;
304 ULONG i;
305 HRESULT hr;
306
307 TRACE("(%p) %i %i %p\n",This,ec,ulCount,pSelection);
308
309 if (!This->pITextStoreACP)
310 {
311 FIXME("Context does not have a ITextStoreACP\n");
312 return E_NOTIMPL;
313 }
314
315 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
316 return TF_E_NOLOCK;
317
318 acp = HeapAlloc(GetProcessHeap(), 0, sizeof(TS_SELECTION_ACP) * ulCount);
319 if (!acp)
320 return E_OUTOFMEMORY;
321
322 for (i = 0; i < ulCount; i++)
323 if (FAILED(TF_SELECTION_to_TS_SELECTION_ACP(&pSelection[i], &acp[i])))
324 {
325 TRACE("Selection Conversion Failed\n");
326 HeapFree(GetProcessHeap(), 0 , acp);
327 return E_FAIL;
328 }
329
330 hr = ITextStoreACP_SetSelection(This->pITextStoreACP, ulCount, acp);
331
332 HeapFree(GetProcessHeap(), 0, acp);
333
334 return hr;
335 }
336
337 static HRESULT WINAPI Context_GetStart (ITfContext *iface,
338 TfEditCookie ec, ITfRange **ppStart)
339 {
340 Context *This = impl_from_ITfContext(iface);
341 EditCookie *cookie;
342 TRACE("(%p) %i %p\n",This,ec,ppStart);
343
344 if (!ppStart)
345 return E_INVALIDARG;
346
347 *ppStart = NULL;
348
349 if (!This->connected)
350 return TF_E_DISCONNECTED;
351
352 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
353 return TF_E_NOLOCK;
354
355 cookie = get_Cookie_data(ec);
356 return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, 0, 0, ppStart);
357 }
358
359 static HRESULT WINAPI Context_GetEnd (ITfContext *iface,
360 TfEditCookie ec, ITfRange **ppEnd)
361 {
362 Context *This = impl_from_ITfContext(iface);
363 EditCookie *cookie;
364 LONG end;
365 TRACE("(%p) %i %p\n",This,ec,ppEnd);
366
367 if (!ppEnd)
368 return E_INVALIDARG;
369
370 *ppEnd = NULL;
371
372 if (!This->connected)
373 return TF_E_DISCONNECTED;
374
375 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
376 return TF_E_NOLOCK;
377
378 if (!This->pITextStoreACP)
379 {
380 FIXME("Context does not have a ITextStoreACP\n");
381 return E_NOTIMPL;
382 }
383
384 cookie = get_Cookie_data(ec);
385 ITextStoreACP_GetEndACP(This->pITextStoreACP,&end);
386
387 return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, end, end, ppEnd);
388 }
389
390 static HRESULT WINAPI Context_GetActiveView (ITfContext *iface,
391 ITfContextView **ppView)
392 {
393 Context *This = impl_from_ITfContext(iface);
394 FIXME("STUB:(%p)\n",This);
395 return E_NOTIMPL;
396 }
397
398 static HRESULT WINAPI Context_EnumViews (ITfContext *iface,
399 IEnumTfContextViews **ppEnum)
400 {
401 Context *This = impl_from_ITfContext(iface);
402 FIXME("STUB:(%p)\n",This);
403 return E_NOTIMPL;
404 }
405
406 static HRESULT WINAPI Context_GetStatus (ITfContext *iface,
407 TF_STATUS *pdcs)
408 {
409 Context *This = impl_from_ITfContext(iface);
410 TRACE("(%p) %p\n",This,pdcs);
411
412 if (!This->connected)
413 return TF_E_DISCONNECTED;
414
415 if (!pdcs)
416 return E_INVALIDARG;
417
418 if (!This->pITextStoreACP)
419 {
420 FIXME("Context does not have a ITextStoreACP\n");
421 return E_NOTIMPL;
422 }
423
424 ITextStoreACP_GetStatus(This->pITextStoreACP, &This->documentStatus);
425
426 *pdcs = This->documentStatus;
427
428 return S_OK;
429 }
430
431 static HRESULT WINAPI Context_GetProperty (ITfContext *iface,
432 REFGUID guidProp, ITfProperty **ppProp)
433 {
434 Context *This = impl_from_ITfContext(iface);
435 FIXME("STUB:(%p)\n",This);
436 return E_NOTIMPL;
437 }
438
439 static HRESULT WINAPI Context_GetAppProperty (ITfContext *iface,
440 REFGUID guidProp, ITfReadOnlyProperty **ppProp)
441 {
442 Context *This = impl_from_ITfContext(iface);
443 FIXME("STUB:(%p)\n",This);
444 return E_NOTIMPL;
445 }
446
447 static HRESULT WINAPI Context_TrackProperties (ITfContext *iface,
448 const GUID **prgProp, ULONG cProp, const GUID **prgAppProp,
449 ULONG cAppProp, ITfReadOnlyProperty **ppProperty)
450 {
451 Context *This = impl_from_ITfContext(iface);
452 FIXME("STUB:(%p)\n",This);
453 return E_NOTIMPL;
454 }
455
456 static HRESULT WINAPI Context_EnumProperties (ITfContext *iface,
457 IEnumTfProperties **ppEnum)
458 {
459 Context *This = impl_from_ITfContext(iface);
460 FIXME("STUB:(%p)\n",This);
461 return E_NOTIMPL;
462 }
463
464 static HRESULT WINAPI Context_GetDocumentMgr (ITfContext *iface,
465 ITfDocumentMgr **ppDm)
466 {
467 Context *This = impl_from_ITfContext(iface);
468 TRACE("(%p) %p\n",This,ppDm);
469
470 if (!ppDm)
471 return E_INVALIDARG;
472
473 *ppDm = This->manager;
474 if (!This->manager)
475 return S_FALSE;
476
477 ITfDocumentMgr_AddRef(This->manager);
478
479 return S_OK;
480 }
481
482 static HRESULT WINAPI Context_CreateRangeBackup (ITfContext *iface,
483 TfEditCookie ec, ITfRange *pRange, ITfRangeBackup **ppBackup)
484 {
485 Context *This = impl_from_ITfContext(iface);
486 FIXME("STUB:(%p)\n",This);
487 return E_NOTIMPL;
488 }
489
490 static const ITfContextVtbl ContextVtbl =
491 {
492 Context_QueryInterface,
493 Context_AddRef,
494 Context_Release,
495 Context_RequestEditSession,
496 Context_InWriteSession,
497 Context_GetSelection,
498 Context_SetSelection,
499 Context_GetStart,
500 Context_GetEnd,
501 Context_GetActiveView,
502 Context_EnumViews,
503 Context_GetStatus,
504 Context_GetProperty,
505 Context_GetAppProperty,
506 Context_TrackProperties,
507 Context_EnumProperties,
508 Context_GetDocumentMgr,
509 Context_CreateRangeBackup
510 };
511
512 static HRESULT WINAPI ContextSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
513 {
514 Context *This = impl_from_ITfSource(iface);
515 return ITfContext_QueryInterface(&This->ITfContext_iface, iid, ppvOut);
516 }
517
518 static ULONG WINAPI ContextSource_AddRef(ITfSource *iface)
519 {
520 Context *This = impl_from_ITfSource(iface);
521 return ITfContext_AddRef(&This->ITfContext_iface);
522 }
523
524 static ULONG WINAPI ContextSource_Release(ITfSource *iface)
525 {
526 Context *This = impl_from_ITfSource(iface);
527 return ITfContext_Release(&This->ITfContext_iface);
528 }
529
530 /*****************************************************
531 * ITfSource functions
532 *****************************************************/
533 static HRESULT WINAPI ContextSource_AdviseSink(ITfSource *iface,
534 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
535 {
536 Context *This = impl_from_ITfSource(iface);
537
538 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
539
540 if (!riid || !punk || !pdwCookie)
541 return E_INVALIDARG;
542
543 if (IsEqualIID(riid, &IID_ITfTextEditSink))
544 return advise_sink(&This->pTextEditSink, &IID_ITfTextEditSink, COOKIE_MAGIC_CONTEXTSINK, punk, pdwCookie);
545
546 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
547 return E_NOTIMPL;
548 }
549
550 static HRESULT WINAPI ContextSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
551 {
552 Context *This = impl_from_ITfSource(iface);
553
554 TRACE("(%p) %x\n",This,pdwCookie);
555
556 if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_CONTEXTSINK)
557 return E_INVALIDARG;
558
559 return unadvise_sink(pdwCookie);
560 }
561
562 static const ITfSourceVtbl ContextSourceVtbl =
563 {
564 ContextSource_QueryInterface,
565 ContextSource_AddRef,
566 ContextSource_Release,
567 ContextSource_AdviseSink,
568 ContextSource_UnadviseSink
569 };
570
571 /*****************************************************
572 * ITfInsertAtSelection functions
573 *****************************************************/
574 static HRESULT WINAPI InsertAtSelection_QueryInterface(ITfInsertAtSelection *iface, REFIID iid, LPVOID *ppvOut)
575 {
576 Context *This = impl_from_ITfInsertAtSelection(iface);
577 return ITfContext_QueryInterface(&This->ITfContext_iface, iid, ppvOut);
578 }
579
580 static ULONG WINAPI InsertAtSelection_AddRef(ITfInsertAtSelection *iface)
581 {
582 Context *This = impl_from_ITfInsertAtSelection(iface);
583 return ITfContext_AddRef(&This->ITfContext_iface);
584 }
585
586 static ULONG WINAPI InsertAtSelection_Release(ITfInsertAtSelection *iface)
587 {
588 Context *This = impl_from_ITfInsertAtSelection(iface);
589 return ITfContext_Release(&This->ITfContext_iface);
590 }
591
592 static HRESULT WINAPI InsertAtSelection_InsertTextAtSelection(
593 ITfInsertAtSelection *iface, TfEditCookie ec, DWORD dwFlags,
594 const WCHAR *pchText, LONG cch, ITfRange **ppRange)
595 {
596 Context *This = impl_from_ITfInsertAtSelection(iface);
597 EditCookie *cookie;
598 LONG acpStart, acpEnd;
599 TS_TEXTCHANGE change;
600 HRESULT hr;
601
602 TRACE("(%p) %i %x %s %p\n",This, ec, dwFlags, debugstr_wn(pchText,cch), ppRange);
603
604 if (!This->connected)
605 return TF_E_DISCONNECTED;
606
607 if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
608 return TF_E_NOLOCK;
609
610 cookie = get_Cookie_data(ec);
611
612 if ((cookie->lockType & TS_LF_READWRITE) != TS_LF_READWRITE )
613 return TS_E_READONLY;
614
615 if (!This->pITextStoreACP)
616 {
617 FIXME("Context does not have a ITextStoreACP\n");
618 return E_NOTIMPL;
619 }
620
621 hr = ITextStoreACP_InsertTextAtSelection(This->pITextStoreACP, dwFlags, pchText, cch, &acpStart, &acpEnd, &change);
622 if (SUCCEEDED(hr))
623 Range_Constructor(&This->ITfContext_iface, This->pITextStoreACP, cookie->lockType, change.acpStart, change.acpNewEnd, ppRange);
624
625 return hr;
626 }
627
628 static HRESULT WINAPI InsertAtSelection_InsertEmbeddedAtSelection(
629 ITfInsertAtSelection *iface, TfEditCookie ec, DWORD dwFlags,
630 IDataObject *pDataObject, ITfRange **ppRange)
631 {
632 Context *This = impl_from_ITfInsertAtSelection(iface);
633 FIXME("STUB:(%p)\n",This);
634 return E_NOTIMPL;
635 }
636
637 static const ITfInsertAtSelectionVtbl InsertAtSelectionVtbl =
638 {
639 InsertAtSelection_QueryInterface,
640 InsertAtSelection_AddRef,
641 InsertAtSelection_Release,
642 InsertAtSelection_InsertTextAtSelection,
643 InsertAtSelection_InsertEmbeddedAtSelection,
644 };
645
646 /*****************************************************
647 * ITfSourceSingle functions
648 *****************************************************/
649 static HRESULT WINAPI SourceSingle_QueryInterface(ITfSourceSingle *iface, REFIID iid, LPVOID *ppvOut)
650 {
651 Context *This = impl_from_ITfSourceSingle(iface);
652 return ITfContext_QueryInterface(&This->ITfContext_iface, iid, ppvOut);
653 }
654
655 static ULONG WINAPI SourceSingle_AddRef(ITfSourceSingle *iface)
656 {
657 Context *This = impl_from_ITfSourceSingle(iface);
658 return ITfContext_AddRef(&This->ITfContext_iface);
659 }
660
661 static ULONG WINAPI SourceSingle_Release(ITfSourceSingle *iface)
662 {
663 Context *This = impl_from_ITfSourceSingle(iface);
664 return ITfContext_Release(&This->ITfContext_iface);
665 }
666
667 static HRESULT WINAPI SourceSingle_AdviseSingleSink( ITfSourceSingle *iface,
668 TfClientId tid, REFIID riid, IUnknown *punk)
669 {
670 Context *This = impl_from_ITfSourceSingle(iface);
671 FIXME("STUB:(%p) %i %s %p\n",This, tid, debugstr_guid(riid),punk);
672 return E_NOTIMPL;
673 }
674
675 static HRESULT WINAPI SourceSingle_UnadviseSingleSink( ITfSourceSingle *iface,
676 TfClientId tid, REFIID riid)
677 {
678 Context *This = impl_from_ITfSourceSingle(iface);
679 FIXME("STUB:(%p) %i %s\n",This, tid, debugstr_guid(riid));
680 return E_NOTIMPL;
681 }
682
683 static const ITfSourceSingleVtbl ContextSourceSingleVtbl =
684 {
685 SourceSingle_QueryInterface,
686 SourceSingle_AddRef,
687 SourceSingle_Release,
688 SourceSingle_AdviseSingleSink,
689 SourceSingle_UnadviseSingleSink,
690 };
691
692 /**************************************************************************
693 * ITextStoreACPSink
694 **************************************************************************/
695
696 static HRESULT WINAPI TextStoreACPSink_QueryInterface(ITextStoreACPSink *iface, REFIID iid, LPVOID *ppvOut)
697 {
698 Context *This = impl_from_ITextStoreACPSink(iface);
699
700 *ppvOut = NULL;
701
702 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACPSink))
703 {
704 *ppvOut = &This->ITextStoreACPSink_iface;
705 }
706 else if (IsEqualIID(iid, &IID_ITextStoreACPServices))
707 *ppvOut = &This->ITextStoreACPServices_iface;
708
709 if (*ppvOut)
710 {
711 ITextStoreACPSink_AddRef(iface);
712 return S_OK;
713 }
714
715 WARN("unsupported interface: %s\n", debugstr_guid(iid));
716 return E_NOINTERFACE;
717 }
718
719 static ULONG WINAPI TextStoreACPSink_AddRef(ITextStoreACPSink *iface)
720 {
721 Context *This = impl_from_ITextStoreACPSink(iface);
722 return ITfContext_AddRef(&This->ITfContext_iface);
723 }
724
725 static ULONG WINAPI TextStoreACPSink_Release(ITextStoreACPSink *iface)
726 {
727 Context *This = impl_from_ITextStoreACPSink(iface);
728 return ITfContext_Release(&This->ITfContext_iface);
729 }
730
731 /*****************************************************
732 * ITextStoreACPSink functions
733 *****************************************************/
734
735 static HRESULT WINAPI TextStoreACPSink_OnTextChange(ITextStoreACPSink *iface,
736 DWORD dwFlags, const TS_TEXTCHANGE *pChange)
737 {
738 Context *This = impl_from_ITextStoreACPSink(iface);
739 FIXME("STUB:(%p)\n",This);
740 return E_NOTIMPL;
741 }
742
743 static HRESULT WINAPI TextStoreACPSink_OnSelectionChange(ITextStoreACPSink *iface)
744 {
745 Context *This = impl_from_ITextStoreACPSink(iface);
746 FIXME("STUB:(%p)\n",This);
747 return E_NOTIMPL;
748 }
749
750 static HRESULT WINAPI TextStoreACPSink_OnLayoutChange(ITextStoreACPSink *iface,
751 TsLayoutCode lcode, TsViewCookie vcView)
752 {
753 Context *This = impl_from_ITextStoreACPSink(iface);
754 FIXME("STUB:(%p)\n",This);
755 return E_NOTIMPL;
756 }
757
758 static HRESULT WINAPI TextStoreACPSink_OnStatusChange(ITextStoreACPSink *iface,
759 DWORD dwFlags)
760 {
761 Context *This = impl_from_ITextStoreACPSink(iface);
762 HRESULT hr, hrSession;
763
764 TRACE("(%p) %x\n",This, dwFlags);
765
766 if (!This->pITextStoreACP)
767 {
768 FIXME("Context does not have a ITextStoreACP\n");
769 return E_NOTIMPL;
770 }
771
772 hr = ITextStoreACP_RequestLock(This->pITextStoreACP, TS_LF_READ, &hrSession);
773
774 if(SUCCEEDED(hr) && SUCCEEDED(hrSession))
775 This->documentStatus.dwDynamicFlags = dwFlags;
776
777 return S_OK;
778 }
779
780 static HRESULT WINAPI TextStoreACPSink_OnAttrsChange(ITextStoreACPSink *iface,
781 LONG acpStart, LONG acpEnd, ULONG cAttrs, const TS_ATTRID *paAttrs)
782 {
783 Context *This = impl_from_ITextStoreACPSink(iface);
784 FIXME("STUB:(%p)\n",This);
785 return E_NOTIMPL;
786 }
787
788 static HRESULT WINAPI TextStoreACPSink_OnLockGranted(ITextStoreACPSink *iface,
789 DWORD dwLockFlags)
790 {
791 Context *This = impl_from_ITextStoreACPSink(iface);
792 HRESULT hr;
793 EditCookie *cookie,*sinkcookie;
794 TfEditCookie ec;
795 struct list *cursor;
796
797 TRACE("(%p) %x\n",This, dwLockFlags);
798
799 if (!This->currentEditSession)
800 {
801 FIXME("OnLockGranted called for something other than an EditSession\n");
802 return S_OK;
803 }
804
805 cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
806 if (!cookie)
807 return E_OUTOFMEMORY;
808
809 sinkcookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
810 if (!sinkcookie)
811 {
812 HeapFree(GetProcessHeap(), 0, cookie);
813 return E_OUTOFMEMORY;
814 }
815
816 cookie->lockType = dwLockFlags;
817 cookie->pOwningContext = This;
818 ec = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE, cookie);
819
820 hr = ITfEditSession_DoEditSession(This->currentEditSession, ec);
821
822 if ((dwLockFlags&TS_LF_READWRITE) == TS_LF_READWRITE)
823 {
824 ITfTextEditSink *sink;
825 TfEditCookie sc;
826
827 sinkcookie->lockType = TS_LF_READ;
828 sinkcookie->pOwningContext = This;
829 sc = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE, sinkcookie);
830
831 /*TODO: implement ITfEditRecord */
832 SINK_FOR_EACH(cursor, &This->pTextEditSink, ITfTextEditSink, sink)
833 {
834 ITfTextEditSink_OnEndEdit(sink, (ITfContext*) &This->ITfContext_iface, sc, NULL);
835 }
836 sinkcookie = remove_Cookie(sc);
837 }
838 HeapFree(GetProcessHeap(),0,sinkcookie);
839
840 ITfEditSession_Release(This->currentEditSession);
841 This->currentEditSession = NULL;
842
843 /* Edit Cookie is only valid during the edit session */
844 cookie = remove_Cookie(ec);
845 HeapFree(GetProcessHeap(),0,cookie);
846
847 return hr;
848 }
849
850 static HRESULT WINAPI TextStoreACPSink_OnStartEditTransaction(ITextStoreACPSink *iface)
851 {
852 Context *This = impl_from_ITextStoreACPSink(iface);
853 FIXME("STUB:(%p)\n",This);
854 return E_NOTIMPL;
855 }
856
857 static HRESULT WINAPI TextStoreACPSink_OnEndEditTransaction(ITextStoreACPSink *iface)
858 {
859 Context *This = impl_from_ITextStoreACPSink(iface);
860 FIXME("STUB:(%p)\n",This);
861 return E_NOTIMPL;
862 }
863
864 static const ITextStoreACPSinkVtbl TextStoreACPSinkVtbl =
865 {
866 TextStoreACPSink_QueryInterface,
867 TextStoreACPSink_AddRef,
868 TextStoreACPSink_Release,
869 TextStoreACPSink_OnTextChange,
870 TextStoreACPSink_OnSelectionChange,
871 TextStoreACPSink_OnLayoutChange,
872 TextStoreACPSink_OnStatusChange,
873 TextStoreACPSink_OnAttrsChange,
874 TextStoreACPSink_OnLockGranted,
875 TextStoreACPSink_OnStartEditTransaction,
876 TextStoreACPSink_OnEndEditTransaction
877 };
878
879 static HRESULT WINAPI TextStoreACPServices_QueryInterface(ITextStoreACPServices *iface, REFIID riid, void **obj)
880 {
881 Context *This = impl_from_ITextStoreACPServices(iface);
882 return ITextStoreACPSink_QueryInterface(&This->ITextStoreACPSink_iface, riid, obj);
883 }
884
885 static ULONG WINAPI TextStoreACPServices_AddRef(ITextStoreACPServices *iface)
886 {
887 Context *This = impl_from_ITextStoreACPServices(iface);
888 return ITextStoreACPSink_AddRef(&This->ITextStoreACPSink_iface);
889 }
890
891 static ULONG WINAPI TextStoreACPServices_Release(ITextStoreACPServices *iface)
892 {
893 Context *This = impl_from_ITextStoreACPServices(iface);
894 return ITextStoreACPSink_Release(&This->ITextStoreACPSink_iface);
895 }
896
897 static HRESULT WINAPI TextStoreACPServices_Serialize(ITextStoreACPServices *iface, ITfProperty *prop, ITfRange *range,
898 TF_PERSISTENT_PROPERTY_HEADER_ACP *header, IStream *stream)
899 {
900 Context *This = impl_from_ITextStoreACPServices(iface);
901
902 FIXME("stub: %p %p %p %p %p\n", This, prop, range, header, stream);
903
904 return E_NOTIMPL;
905 }
906
907 static HRESULT WINAPI TextStoreACPServices_Unserialize(ITextStoreACPServices *iface, ITfProperty *prop,
908 const TF_PERSISTENT_PROPERTY_HEADER_ACP *header, IStream *stream, ITfPersistentPropertyLoaderACP *loader)
909 {
910 Context *This = impl_from_ITextStoreACPServices(iface);
911
912 FIXME("stub: %p %p %p %p %p\n", This, prop, header, stream, loader);
913
914 return E_NOTIMPL;
915 }
916
917 static HRESULT WINAPI TextStoreACPServices_ForceLoadProperty(ITextStoreACPServices *iface, ITfProperty *prop)
918 {
919 Context *This = impl_from_ITextStoreACPServices(iface);
920
921 FIXME("stub: %p %p\n", This, prop);
922
923 return E_NOTIMPL;
924 }
925
926 static HRESULT WINAPI TextStoreACPServices_CreateRange(ITextStoreACPServices *iface,
927 LONG start, LONG end, ITfRangeACP **range)
928 {
929 Context *This = impl_from_ITextStoreACPServices(iface);
930
931 FIXME("stub: %p %d %d %p\n", This, start, end, range);
932
933 return S_OK;
934 }
935
936 static const ITextStoreACPServicesVtbl TextStoreACPServicesVtbl =
937 {
938 TextStoreACPServices_QueryInterface,
939 TextStoreACPServices_AddRef,
940 TextStoreACPServices_Release,
941 TextStoreACPServices_Serialize,
942 TextStoreACPServices_Unserialize,
943 TextStoreACPServices_ForceLoadProperty,
944 TextStoreACPServices_CreateRange
945 };
946
947 HRESULT Context_Constructor(TfClientId tidOwner, IUnknown *punk, ITfDocumentMgr *mgr, ITfContext **ppOut, TfEditCookie *pecTextStore)
948 {
949 Context *This;
950 EditCookie *cookie;
951
952 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Context));
953 if (This == NULL)
954 return E_OUTOFMEMORY;
955
956 cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
957 if (cookie == NULL)
958 {
959 HeapFree(GetProcessHeap(),0,This);
960 return E_OUTOFMEMORY;
961 }
962
963 TRACE("(%p) %x %p %p %p\n",This, tidOwner, punk, ppOut, pecTextStore);
964
965 This->ITfContext_iface.lpVtbl= &ContextVtbl;
966 This->ITfSource_iface.lpVtbl = &ContextSourceVtbl;
967 This->ITfInsertAtSelection_iface.lpVtbl = &InsertAtSelectionVtbl;
968 This->ITfSourceSingle_iface.lpVtbl = &ContextSourceSingleVtbl;
969 This->ITextStoreACPSink_iface.lpVtbl = &TextStoreACPSinkVtbl;
970 This->ITextStoreACPServices_iface.lpVtbl = &TextStoreACPServicesVtbl;
971 This->refCount = 1;
972 This->tidOwner = tidOwner;
973 This->connected = FALSE;
974 This->manager = mgr;
975
976 CompartmentMgr_Constructor((IUnknown*)&This->ITfContext_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
977
978 cookie->lockType = TF_ES_READ;
979 cookie->pOwningContext = This;
980
981 if (punk)
982 {
983 IUnknown_QueryInterface(punk, &IID_ITextStoreACP,
984 (LPVOID*)&This->pITextStoreACP);
985
986 IUnknown_QueryInterface(punk, &IID_ITfContextOwnerCompositionSink,
987 (LPVOID*)&This->pITfContextOwnerCompositionSink);
988
989 if (!This->pITextStoreACP && !This->pITfContextOwnerCompositionSink)
990 FIXME("Unhandled pUnk\n");
991 }
992
993 This->defaultCookie = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE,cookie);
994 *pecTextStore = This->defaultCookie;
995
996 list_init(&This->pContextKeyEventSink);
997 list_init(&This->pEditTransactionSink);
998 list_init(&This->pStatusSink);
999 list_init(&This->pTextEditSink);
1000 list_init(&This->pTextLayoutSink);
1001
1002 *ppOut = &This->ITfContext_iface;
1003 TRACE("returning %p\n", *ppOut);
1004
1005 return S_OK;
1006 }
1007
1008 HRESULT Context_Initialize(ITfContext *iface, ITfDocumentMgr *manager)
1009 {
1010 Context *This = impl_from_ITfContext(iface);
1011
1012 if (This->pITextStoreACP)
1013 ITextStoreACP_AdviseSink(This->pITextStoreACP, &IID_ITextStoreACPSink,
1014 (IUnknown*)&This->ITextStoreACPSink_iface, TS_AS_ALL_SINKS);
1015 This->connected = TRUE;
1016 This->manager = manager;
1017 return S_OK;
1018 }
1019
1020 HRESULT Context_Uninitialize(ITfContext *iface)
1021 {
1022 Context *This = impl_from_ITfContext(iface);
1023
1024 if (This->pITextStoreACP)
1025 ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)&This->ITextStoreACPSink_iface);
1026 This->connected = FALSE;
1027 This->manager = NULL;
1028 return S_OK;
1029 }