context.c 29.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 *  ITfContext implementation
 *
 *  Copyright 2009 Aric Stewart, CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include <stdarg.h>

#define COBJMACROS

#include "wine/debug.h"
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winuser.h"
#include "shlwapi.h"
#include "winerror.h"
#include "objbase.h"
35
#include "olectl.h"
36 37 38 39 40 41 42 43 44

#include "wine/unicode.h"

#include "msctf.h"
#include "msctf_internal.h"

WINE_DEFAULT_DEBUG_CHANNEL(msctf);

typedef struct tagContext {
45 46
    ITfContext ITfContext_iface;
    ITfSource ITfSource_iface;
47 48 49
    /* const ITfContextCompositionVtbl *ContextCompositionVtbl; */
    /* const ITfContextOwnerCompositionServicesVtbl *ContextOwnerCompositionServicesVtbl; */
    /* const ITfContextOwnerServicesVtbl *ContextOwnerServicesVtbl; */
50
    ITfInsertAtSelection ITfInsertAtSelection_iface;
51 52
    /* const ITfMouseTrackerVtbl *MouseTrackerVtbl; */
    /* const ITfQueryEmbeddedVtbl *QueryEmbeddedVtbl; */
53
    ITfSourceSingle ITfSourceSingle_iface;
54
    ITextStoreACPSink ITextStoreACPSink_iface;
55
    ITextStoreACPServices ITextStoreACPServices_iface;
56
    LONG refCount;
57
    BOOL connected;
58

59 60 61
    /* Aggregation */
    ITfCompartmentMgr  *CompartmentMgr;

62
    TfClientId tidOwner;
63
    TfEditCookie defaultCookie;
64
    TS_STATUS documentStatus;
65
    ITfDocumentMgr *manager;
66 67

    ITextStoreACP   *pITextStoreACP;
68
    ITfContextOwnerCompositionSink *pITfContextOwnerCompositionSink;
69

70
    ITfEditSession* currentEditSession;
71

72
    /* kept as separate lists to reduce unnecessary iterations */
73 74 75 76 77 78
    struct list     pContextKeyEventSink;
    struct list     pEditTransactionSink;
    struct list     pStatusSink;
    struct list     pTextEditSink;
    struct list     pTextLayoutSink;

79 80
} Context;

81 82 83 84
typedef struct tagEditCookie {
    DWORD lockType;
    Context *pOwningContext;
} EditCookie;
85

86
static inline Context *impl_from_ITfContext(ITfContext *iface)
87
{
88
    return CONTAINING_RECORD(iface, Context, ITfContext_iface);
89 90
}

91
static inline Context *impl_from_ITfSource(ITfSource *iface)
92
{
93
    return CONTAINING_RECORD(iface, Context, ITfSource_iface);
94 95
}

96
static inline Context *impl_from_ITfInsertAtSelection(ITfInsertAtSelection *iface)
97
{
98 99 100 101 102 103 104 105
    return CONTAINING_RECORD(iface, Context, ITfInsertAtSelection_iface);
}

static inline Context *impl_from_ITfSourceSingle(ITfSourceSingle* iface)
{
    return CONTAINING_RECORD(iface, Context, ITfSourceSingle_iface);
}

106
static inline Context *impl_from_ITextStoreACPSink(ITextStoreACPSink *iface)
107
{
108
    return CONTAINING_RECORD(iface, Context, ITextStoreACPSink_iface);
109 110
}

111 112 113 114 115
static inline Context *impl_from_ITextStoreACPServices(ITextStoreACPServices *iface)
{
    return CONTAINING_RECORD(iface, Context, ITextStoreACPServices_iface);
}

116 117
static void Context_Destructor(Context *This)
{
118
    EditCookie *cookie;
119
    TRACE("destroying %p\n", This);
120

121
    if (This->pITextStoreACP)
122
        ITextStoreACP_Release(This->pITextStoreACP);
123

124
    if (This->pITfContextOwnerCompositionSink)
125
        ITfContextOwnerCompositionSink_Release(This->pITfContextOwnerCompositionSink);
126

127 128 129 130 131 132 133
    if (This->defaultCookie)
    {
        cookie = remove_Cookie(This->defaultCookie);
        HeapFree(GetProcessHeap(),0,cookie);
        This->defaultCookie = 0;
    }

134 135 136 137 138
    free_sinks(&This->pContextKeyEventSink);
    free_sinks(&This->pEditTransactionSink);
    free_sinks(&This->pStatusSink);
    free_sinks(&This->pTextEditSink);
    free_sinks(&This->pTextLayoutSink);
139

140
    CompartmentMgr_Destructor(This->CompartmentMgr);
141 142 143 144 145
    HeapFree(GetProcessHeap(),0,This);
}

static HRESULT WINAPI Context_QueryInterface(ITfContext *iface, REFIID iid, LPVOID *ppvOut)
{
146
    Context *This = impl_from_ITfContext(iface);
147 148 149 150
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfContext))
    {
151
        *ppvOut = &This->ITfContext_iface;
152
    }
153 154
    else if (IsEqualIID(iid, &IID_ITfSource))
    {
155
        *ppvOut = &This->ITfSource_iface;
156
    }
157 158
    else if (IsEqualIID(iid, &IID_ITfInsertAtSelection))
    {
159
        *ppvOut = &This->ITfInsertAtSelection_iface;
160
    }
161 162 163 164
    else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
    {
        *ppvOut = This->CompartmentMgr;
    }
165 166
    else if (IsEqualIID(iid, &IID_ITfSourceSingle))
    {
167
        *ppvOut = &This->ITfSourceSingle_iface;
168
    }
169 170 171

    if (*ppvOut)
    {
172
        ITfContext_AddRef(iface);
173 174 175 176 177 178 179 180 181
        return S_OK;
    }

    WARN("unsupported interface: %s\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI Context_AddRef(ITfContext *iface)
{
182
    Context *This = impl_from_ITfContext(iface);
183 184 185 186 187
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI Context_Release(ITfContext *iface)
{
188
    Context *This = impl_from_ITfContext(iface);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    ULONG ret;

    ret = InterlockedDecrement(&This->refCount);
    if (ret == 0)
        Context_Destructor(This);
    return ret;
}

/*****************************************************
 * ITfContext functions
 *****************************************************/
static HRESULT WINAPI Context_RequestEditSession (ITfContext *iface,
        TfClientId tid, ITfEditSession *pes, DWORD dwFlags,
        HRESULT *phrSession)
{
204
    Context *This = impl_from_ITfContext(iface);
205 206 207 208 209 210 211 212 213 214 215 216 217
    HRESULT hr;
    DWORD  dwLockFlags = 0x0;

    TRACE("(%p) %i %p %x %p\n",This, tid, pes, dwFlags, phrSession);

    if (!(dwFlags & TF_ES_READ) && !(dwFlags & TF_ES_READWRITE))
    {
        *phrSession = E_FAIL;
        return E_INVALIDARG;
    }

    if (!This->pITextStoreACP)
    {
218
        FIXME("No ITextStoreACP available\n");
219 220 221 222 223
        *phrSession = E_FAIL;
        return E_FAIL;
    }

    if (!(dwFlags & TF_ES_ASYNC))
224
        dwLockFlags |= TS_LF_SYNC;
225

226
    if ((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE)
227
        dwLockFlags |= TS_LF_READWRITE;
228 229
    else if (dwFlags & TF_ES_READ)
        dwLockFlags |= TS_LF_READ;
230

231 232
    if (!This->documentStatus.dwDynamicFlags)
        ITextStoreACP_GetStatus(This->pITextStoreACP, &This->documentStatus);
233

234
    if (((dwFlags & TF_ES_READWRITE) == TF_ES_READWRITE) && (This->documentStatus.dwDynamicFlags & TS_SD_READONLY))
235 236 237 238 239 240 241 242 243 244 245 246 247 248
    {
        *phrSession = TS_E_READONLY;
        return S_OK;
    }

    if (FAILED (ITfEditSession_QueryInterface(pes, &IID_ITfEditSession, (LPVOID*)&This->currentEditSession)))
    {
        *phrSession = E_FAIL;
        return E_INVALIDARG;
    }

    hr = ITextStoreACP_RequestLock(This->pITextStoreACP, dwLockFlags, phrSession);

    return hr;
249 250 251 252 253 254
}

static HRESULT WINAPI Context_InWriteSession (ITfContext *iface,
         TfClientId tid,
         BOOL *pfWriteSession)
{
255
    Context *This = impl_from_ITfContext(iface);
256 257 258 259 260 261 262 263
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_GetSelection (ITfContext *iface,
        TfEditCookie ec, ULONG ulIndex, ULONG ulCount,
        TF_SELECTION *pSelection, ULONG *pcFetched)
{
264
    Context *This = impl_from_ITfContext(iface);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    EditCookie *cookie;
    ULONG count, i;
    ULONG totalFetched = 0;
    HRESULT hr = S_OK;

    if (!pSelection || !pcFetched)
        return E_INVALIDARG;

    *pcFetched = 0;

    if (!This->connected)
        return TF_E_DISCONNECTED;

    if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
        return TF_E_NOLOCK;

    if (!This->pITextStoreACP)
    {
        FIXME("Context does not have a ITextStoreACP\n");
        return E_NOTIMPL;
    }

    cookie = get_Cookie_data(ec);

    if (ulIndex == TF_DEFAULT_SELECTION)
        count = 1;
    else
        count = ulCount;

    for (i = 0; i < count; i++)
    {
        DWORD fetched;
        TS_SELECTION_ACP acps;

        hr = ITextStoreACP_GetSelection(This->pITextStoreACP, ulIndex + i,
                1, &acps, &fetched);

        if (hr == TS_E_NOLOCK)
            return TF_E_NOLOCK;
        else if (SUCCEEDED(hr))
        {
            pSelection[totalFetched].style.ase = acps.style.ase;
            pSelection[totalFetched].style.fInterimChar = acps.style.fInterimChar;
            Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, acps.acpStart, acps.acpEnd, &pSelection[totalFetched].range);
            totalFetched ++;
        }
        else
            break;
    }

    *pcFetched = totalFetched;

    return hr;
318 319 320 321 322
}

static HRESULT WINAPI Context_SetSelection (ITfContext *iface,
        TfEditCookie ec, ULONG ulCount, const TF_SELECTION *pSelection)
{
323
    Context *This = impl_from_ITfContext(iface);
324
    TS_SELECTION_ACP *acp;
325
    ULONG i;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    HRESULT hr;

    TRACE("(%p) %i %i %p\n",This,ec,ulCount,pSelection);

    if (!This->pITextStoreACP)
    {
        FIXME("Context does not have a ITextStoreACP\n");
        return E_NOTIMPL;
    }

    if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
        return TF_E_NOLOCK;

    acp = HeapAlloc(GetProcessHeap(), 0, sizeof(TS_SELECTION_ACP) * ulCount);
    if (!acp)
        return E_OUTOFMEMORY;

    for (i = 0; i < ulCount; i++)
        if (FAILED(TF_SELECTION_to_TS_SELECTION_ACP(&pSelection[i], &acp[i])))
        {
            TRACE("Selection Conversion Failed\n");
            HeapFree(GetProcessHeap(), 0 , acp);
            return E_FAIL;
        }

    hr = ITextStoreACP_SetSelection(This->pITextStoreACP, ulCount, acp);

    HeapFree(GetProcessHeap(), 0, acp);

    return hr;
356 357 358 359 360
}

static HRESULT WINAPI Context_GetStart (ITfContext *iface,
        TfEditCookie ec, ITfRange **ppStart)
{
361
    Context *This = impl_from_ITfContext(iface);
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    EditCookie *cookie;
    TRACE("(%p) %i %p\n",This,ec,ppStart);

    if (!ppStart)
        return E_INVALIDARG;

    *ppStart = NULL;

    if (!This->connected)
        return TF_E_DISCONNECTED;

    if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
        return TF_E_NOLOCK;

    cookie = get_Cookie_data(ec);
    return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, 0, 0, ppStart);
378 379 380 381 382
}

static HRESULT WINAPI Context_GetEnd (ITfContext *iface,
        TfEditCookie ec, ITfRange **ppEnd)
{
383
    Context *This = impl_from_ITfContext(iface);
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
    EditCookie *cookie;
    LONG end;
    TRACE("(%p) %i %p\n",This,ec,ppEnd);

    if (!ppEnd)
        return E_INVALIDARG;

    *ppEnd = NULL;

    if (!This->connected)
        return TF_E_DISCONNECTED;

    if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
        return TF_E_NOLOCK;

    if (!This->pITextStoreACP)
    {
        FIXME("Context does not have a ITextStoreACP\n");
        return E_NOTIMPL;
    }

    cookie = get_Cookie_data(ec);
    ITextStoreACP_GetEndACP(This->pITextStoreACP,&end);

    return Range_Constructor(iface, This->pITextStoreACP, cookie->lockType, end, end, ppEnd);
409 410 411 412 413
}

static HRESULT WINAPI Context_GetActiveView (ITfContext *iface,
  ITfContextView **ppView)
{
414
    Context *This = impl_from_ITfContext(iface);
415 416 417 418 419 420 421
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_EnumViews (ITfContext *iface,
        IEnumTfContextViews **ppEnum)
{
422
    Context *This = impl_from_ITfContext(iface);
423 424 425 426 427 428 429
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_GetStatus (ITfContext *iface,
        TF_STATUS *pdcs)
{
430
    Context *This = impl_from_ITfContext(iface);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
    TRACE("(%p) %p\n",This,pdcs);

    if (!This->connected)
        return TF_E_DISCONNECTED;

    if (!pdcs)
        return E_INVALIDARG;

    if (!This->pITextStoreACP)
    {
        FIXME("Context does not have a ITextStoreACP\n");
        return E_NOTIMPL;
    }

    ITextStoreACP_GetStatus(This->pITextStoreACP, &This->documentStatus);

    *pdcs = This->documentStatus;

    return S_OK;
450 451 452 453 454
}

static HRESULT WINAPI Context_GetProperty (ITfContext *iface,
        REFGUID guidProp, ITfProperty **ppProp)
{
455
    Context *This = impl_from_ITfContext(iface);
456 457 458 459 460 461 462
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_GetAppProperty (ITfContext *iface,
        REFGUID guidProp, ITfReadOnlyProperty **ppProp)
{
463
    Context *This = impl_from_ITfContext(iface);
464 465 466 467 468 469 470 471
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_TrackProperties (ITfContext *iface,
        const GUID **prgProp, ULONG cProp, const GUID **prgAppProp,
        ULONG cAppProp, ITfReadOnlyProperty **ppProperty)
{
472
    Context *This = impl_from_ITfContext(iface);
473 474 475 476 477 478 479
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_EnumProperties (ITfContext *iface,
        IEnumTfProperties **ppEnum)
{
480
    Context *This = impl_from_ITfContext(iface);
481 482 483 484 485 486 487
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI Context_GetDocumentMgr (ITfContext *iface,
        ITfDocumentMgr **ppDm)
{
488
    Context *This = impl_from_ITfContext(iface);
489 490 491 492 493 494 495 496
    TRACE("(%p) %p\n",This,ppDm);

    if (!ppDm)
        return E_INVALIDARG;

    *ppDm = This->manager;
    if (!This->manager)
        return S_FALSE;
497 498 499

    ITfDocumentMgr_AddRef(This->manager);

500
    return S_OK;
501 502 503 504 505
}

static HRESULT WINAPI Context_CreateRangeBackup (ITfContext *iface,
        TfEditCookie ec, ITfRange *pRange, ITfRangeBackup **ppBackup)
{
506
    Context *This = impl_from_ITfContext(iface);
507 508 509 510
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

511
static const ITfContextVtbl ContextVtbl =
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
{
    Context_QueryInterface,
    Context_AddRef,
    Context_Release,
    Context_RequestEditSession,
    Context_InWriteSession,
    Context_GetSelection,
    Context_SetSelection,
    Context_GetStart,
    Context_GetEnd,
    Context_GetActiveView,
    Context_EnumViews,
    Context_GetStatus,
    Context_GetProperty,
    Context_GetAppProperty,
    Context_TrackProperties,
    Context_EnumProperties,
    Context_GetDocumentMgr,
    Context_CreateRangeBackup
};

533
static HRESULT WINAPI ContextSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
534
{
535
    Context *This = impl_from_ITfSource(iface);
536
    return ITfContext_QueryInterface(&This->ITfContext_iface, iid, ppvOut);
537 538
}

539
static ULONG WINAPI ContextSource_AddRef(ITfSource *iface)
540
{
541 542
    Context *This = impl_from_ITfSource(iface);
    return ITfContext_AddRef(&This->ITfContext_iface);
543 544
}

545
static ULONG WINAPI ContextSource_Release(ITfSource *iface)
546
{
547 548
    Context *This = impl_from_ITfSource(iface);
    return ITfContext_Release(&This->ITfContext_iface);
549 550 551 552 553
}

/*****************************************************
 * ITfSource functions
 *****************************************************/
554
static HRESULT WINAPI ContextSource_AdviseSink(ITfSource *iface,
555 556
        REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
557
    Context *This = impl_from_ITfSource(iface);
558

559 560 561 562 563 564
    TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);

    if (!riid || !punk || !pdwCookie)
        return E_INVALIDARG;

    if (IsEqualIID(riid, &IID_ITfTextEditSink))
565
        return advise_sink(&This->pTextEditSink, &IID_ITfTextEditSink, COOKIE_MAGIC_CONTEXTSINK, punk, pdwCookie);
566

567 568
    FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
    return E_NOTIMPL;
569 570
}

571
static HRESULT WINAPI ContextSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
572
{
573
    Context *This = impl_from_ITfSource(iface);
574

575 576
    TRACE("(%p) %x\n",This,pdwCookie);

577 578 579
    if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_CONTEXTSINK)
        return E_INVALIDARG;

580
    return unadvise_sink(pdwCookie);
581 582
}

583
static const ITfSourceVtbl ContextSourceVtbl =
584
{
585 586 587
    ContextSource_QueryInterface,
    ContextSource_AddRef,
    ContextSource_Release,
588
    ContextSource_AdviseSink,
589
    ContextSource_UnadviseSink
590 591
};

592 593 594 595 596
/*****************************************************
 * ITfInsertAtSelection functions
 *****************************************************/
static HRESULT WINAPI InsertAtSelection_QueryInterface(ITfInsertAtSelection *iface, REFIID iid, LPVOID *ppvOut)
{
597
    Context *This = impl_from_ITfInsertAtSelection(iface);
598
    return ITfContext_QueryInterface(&This->ITfContext_iface, iid, ppvOut);
599 600 601 602
}

static ULONG WINAPI InsertAtSelection_AddRef(ITfInsertAtSelection *iface)
{
603 604
    Context *This = impl_from_ITfInsertAtSelection(iface);
    return ITfContext_AddRef(&This->ITfContext_iface);
605 606 607 608
}

static ULONG WINAPI InsertAtSelection_Release(ITfInsertAtSelection *iface)
{
609 610
    Context *This = impl_from_ITfInsertAtSelection(iface);
    return ITfContext_Release(&This->ITfContext_iface);
611 612
}

613
static HRESULT WINAPI InsertAtSelection_InsertTextAtSelection(
614 615 616
        ITfInsertAtSelection *iface, TfEditCookie ec, DWORD dwFlags,
        const WCHAR *pchText, LONG cch, ITfRange **ppRange)
{
617
    Context *This = impl_from_ITfInsertAtSelection(iface);
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    EditCookie *cookie;
    LONG acpStart, acpEnd;
    TS_TEXTCHANGE change;
    HRESULT hr;

    TRACE("(%p) %i %x %s %p\n",This, ec, dwFlags, debugstr_wn(pchText,cch), ppRange);

    if (!This->connected)
        return TF_E_DISCONNECTED;

    if (get_Cookie_magic(ec)!=COOKIE_MAGIC_EDITCOOKIE)
        return TF_E_NOLOCK;

    cookie = get_Cookie_data(ec);

    if ((cookie->lockType & TS_LF_READWRITE) != TS_LF_READWRITE )
        return TS_E_READONLY;

    if (!This->pITextStoreACP)
    {
        FIXME("Context does not have a ITextStoreACP\n");
        return E_NOTIMPL;
    }

    hr = ITextStoreACP_InsertTextAtSelection(This->pITextStoreACP, dwFlags, pchText, cch, &acpStart, &acpEnd, &change);
    if (SUCCEEDED(hr))
644
        Range_Constructor(&This->ITfContext_iface, This->pITextStoreACP, cookie->lockType, change.acpStart, change.acpNewEnd, ppRange);
645 646

    return hr;
647 648
}

649
static HRESULT WINAPI InsertAtSelection_InsertEmbeddedAtSelection(
650 651 652
        ITfInsertAtSelection *iface, TfEditCookie ec, DWORD dwFlags,
        IDataObject *pDataObject, ITfRange **ppRange)
{
653
    Context *This = impl_from_ITfInsertAtSelection(iface);
654 655 656 657
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

658
static const ITfInsertAtSelectionVtbl InsertAtSelectionVtbl =
659 660 661 662 663 664 665 666
{
    InsertAtSelection_QueryInterface,
    InsertAtSelection_AddRef,
    InsertAtSelection_Release,
    InsertAtSelection_InsertTextAtSelection,
    InsertAtSelection_InsertEmbeddedAtSelection,
};

667 668 669 670 671
/*****************************************************
 * ITfSourceSingle functions
 *****************************************************/
static HRESULT WINAPI SourceSingle_QueryInterface(ITfSourceSingle *iface, REFIID iid, LPVOID *ppvOut)
{
672
    Context *This = impl_from_ITfSourceSingle(iface);
673
    return ITfContext_QueryInterface(&This->ITfContext_iface, iid, ppvOut);
674 675 676 677
}

static ULONG WINAPI SourceSingle_AddRef(ITfSourceSingle *iface)
{
678
    Context *This = impl_from_ITfSourceSingle(iface);
679
    return ITfContext_AddRef(&This->ITfContext_iface);
680 681 682 683
}

static ULONG WINAPI SourceSingle_Release(ITfSourceSingle *iface)
{
684
    Context *This = impl_from_ITfSourceSingle(iface);
685
    return ITfContext_Release(&This->ITfContext_iface);
686 687
}

688
static HRESULT WINAPI SourceSingle_AdviseSingleSink( ITfSourceSingle *iface,
689 690
    TfClientId tid, REFIID riid, IUnknown *punk)
{
691
    Context *This = impl_from_ITfSourceSingle(iface);
692 693 694 695
    FIXME("STUB:(%p) %i %s %p\n",This, tid, debugstr_guid(riid),punk);
    return E_NOTIMPL;
}

696
static HRESULT WINAPI SourceSingle_UnadviseSingleSink( ITfSourceSingle *iface,
697 698
    TfClientId tid, REFIID riid)
{
699
    Context *This = impl_from_ITfSourceSingle(iface);
700 701 702 703
    FIXME("STUB:(%p) %i %s\n",This, tid, debugstr_guid(riid));
    return E_NOTIMPL;
}

704
static const ITfSourceSingleVtbl ContextSourceSingleVtbl =
705 706 707 708 709 710 711 712
{
    SourceSingle_QueryInterface,
    SourceSingle_AddRef,
    SourceSingle_Release,
    SourceSingle_AdviseSingleSink,
    SourceSingle_UnadviseSingleSink,
};

713 714 715 716 717 718
/**************************************************************************
 *  ITextStoreACPSink
 **************************************************************************/

static HRESULT WINAPI TextStoreACPSink_QueryInterface(ITextStoreACPSink *iface, REFIID iid, LPVOID *ppvOut)
{
719 720
    Context *This = impl_from_ITextStoreACPSink(iface);

721 722 723 724
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITextStoreACPSink))
    {
725
        *ppvOut = &This->ITextStoreACPSink_iface;
726
    }
727 728
    else if (IsEqualIID(iid, &IID_ITextStoreACPServices))
        *ppvOut = &This->ITextStoreACPServices_iface;
729 730 731

    if (*ppvOut)
    {
732
        ITextStoreACPSink_AddRef(iface);
733 734 735 736 737 738 739 740 741
        return S_OK;
    }

    WARN("unsupported interface: %s\n", debugstr_guid(iid));
    return E_NOINTERFACE;
}

static ULONG WINAPI TextStoreACPSink_AddRef(ITextStoreACPSink *iface)
{
742 743
    Context *This = impl_from_ITextStoreACPSink(iface);
    return ITfContext_AddRef(&This->ITfContext_iface);
744 745 746 747
}

static ULONG WINAPI TextStoreACPSink_Release(ITextStoreACPSink *iface)
{
748 749
    Context *This = impl_from_ITextStoreACPSink(iface);
    return ITfContext_Release(&This->ITfContext_iface);
750 751 752 753 754 755 756 757 758
}

/*****************************************************
 * ITextStoreACPSink functions
 *****************************************************/

static HRESULT WINAPI TextStoreACPSink_OnTextChange(ITextStoreACPSink *iface,
        DWORD dwFlags, const TS_TEXTCHANGE *pChange)
{
759
    Context *This = impl_from_ITextStoreACPSink(iface);
760 761 762 763 764 765
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPSink_OnSelectionChange(ITextStoreACPSink *iface)
{
766
    Context *This = impl_from_ITextStoreACPSink(iface);
767 768 769 770 771 772 773
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPSink_OnLayoutChange(ITextStoreACPSink *iface,
    TsLayoutCode lcode, TsViewCookie vcView)
{
774
    Context *This = impl_from_ITextStoreACPSink(iface);
775 776 777 778 779 780 781
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPSink_OnStatusChange(ITextStoreACPSink *iface,
        DWORD dwFlags)
{
782
    Context *This = impl_from_ITextStoreACPSink(iface);
783 784 785 786
    HRESULT hr, hrSession;

    TRACE("(%p) %x\n",This, dwFlags);

787
    if (!This->pITextStoreACP)
788 789 790 791 792
    {
        FIXME("Context does not have a ITextStoreACP\n");
        return E_NOTIMPL;
    }

793
    hr = ITextStoreACP_RequestLock(This->pITextStoreACP, TS_LF_READ, &hrSession);
794 795

    if(SUCCEEDED(hr) && SUCCEEDED(hrSession))
796
        This->documentStatus.dwDynamicFlags = dwFlags;
797 798

    return S_OK;
799 800 801 802 803
}

static HRESULT WINAPI TextStoreACPSink_OnAttrsChange(ITextStoreACPSink *iface,
        LONG acpStart, LONG acpEnd, ULONG cAttrs, const TS_ATTRID *paAttrs)
{
804
    Context *This = impl_from_ITextStoreACPSink(iface);
805 806 807 808 809 810 811
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPSink_OnLockGranted(ITextStoreACPSink *iface,
        DWORD dwLockFlags)
{
812
    Context *This = impl_from_ITextStoreACPSink(iface);
813
    HRESULT hr;
814
    EditCookie *cookie,*sinkcookie;
815
    TfEditCookie ec;
816
    struct list *cursor;
817 818 819

    TRACE("(%p) %x\n",This, dwLockFlags);

820
    if (!This->currentEditSession)
821 822 823 824 825
    {
        FIXME("OnLockGranted called for something other than an EditSession\n");
        return S_OK;
    }

826 827 828 829
    cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
    if (!cookie)
        return E_OUTOFMEMORY;

830 831
    sinkcookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
    if (!sinkcookie)
832 833
    {
        HeapFree(GetProcessHeap(), 0, cookie);
834
        return E_OUTOFMEMORY;
835
    }
836

837
    cookie->lockType = dwLockFlags;
838
    cookie->pOwningContext = This;
839 840
    ec = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE, cookie);

841
    hr = ITfEditSession_DoEditSession(This->currentEditSession, ec);
842

843 844
    if ((dwLockFlags&TS_LF_READWRITE) == TS_LF_READWRITE)
    {
845
        ITfTextEditSink *sink;
846 847 848
        TfEditCookie sc;

        sinkcookie->lockType = TS_LF_READ;
849
        sinkcookie->pOwningContext = This;
850 851 852
        sc = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE, sinkcookie);

        /*TODO: implement ITfEditRecord */
853
        SINK_FOR_EACH(cursor, &This->pTextEditSink, ITfTextEditSink, sink)
854
        {
855
            ITfTextEditSink_OnEndEdit(sink, (ITfContext*) &This->ITfContext_iface, sc, NULL);
856 857 858 859 860
        }
        sinkcookie = remove_Cookie(sc);
    }
    HeapFree(GetProcessHeap(),0,sinkcookie);

861 862
    ITfEditSession_Release(This->currentEditSession);
    This->currentEditSession = NULL;
863

864 865 866 867
    /* Edit Cookie is only valid during the edit session */
    cookie = remove_Cookie(ec);
    HeapFree(GetProcessHeap(),0,cookie);

868
    return hr;
869 870 871 872
}

static HRESULT WINAPI TextStoreACPSink_OnStartEditTransaction(ITextStoreACPSink *iface)
{
873
    Context *This = impl_from_ITextStoreACPSink(iface);
874 875 876 877 878 879
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPSink_OnEndEditTransaction(ITextStoreACPSink *iface)
{
880
    Context *This = impl_from_ITextStoreACPSink(iface);
881 882 883 884
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

885
static const ITextStoreACPSinkVtbl TextStoreACPSinkVtbl =
886 887 888 889 890 891 892 893 894 895 896 897 898 899
{
    TextStoreACPSink_QueryInterface,
    TextStoreACPSink_AddRef,
    TextStoreACPSink_Release,
    TextStoreACPSink_OnTextChange,
    TextStoreACPSink_OnSelectionChange,
    TextStoreACPSink_OnLayoutChange,
    TextStoreACPSink_OnStatusChange,
    TextStoreACPSink_OnAttrsChange,
    TextStoreACPSink_OnLockGranted,
    TextStoreACPSink_OnStartEditTransaction,
    TextStoreACPSink_OnEndEditTransaction
};

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
static HRESULT WINAPI TextStoreACPServices_QueryInterface(ITextStoreACPServices *iface, REFIID riid, void **obj)
{
    Context *This = impl_from_ITextStoreACPServices(iface);
    return ITextStoreACPSink_QueryInterface(&This->ITextStoreACPSink_iface, riid, obj);
}

static ULONG WINAPI TextStoreACPServices_AddRef(ITextStoreACPServices *iface)
{
    Context *This = impl_from_ITextStoreACPServices(iface);
    return ITextStoreACPSink_AddRef(&This->ITextStoreACPSink_iface);
}

static ULONG WINAPI TextStoreACPServices_Release(ITextStoreACPServices *iface)
{
    Context *This = impl_from_ITextStoreACPServices(iface);
    return ITextStoreACPSink_Release(&This->ITextStoreACPSink_iface);
}

static HRESULT WINAPI TextStoreACPServices_Serialize(ITextStoreACPServices *iface, ITfProperty *prop, ITfRange *range,
    TF_PERSISTENT_PROPERTY_HEADER_ACP *header, IStream *stream)
{
    Context *This = impl_from_ITextStoreACPServices(iface);

    FIXME("stub: %p %p %p %p %p\n", This, prop, range, header, stream);

    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPServices_Unserialize(ITextStoreACPServices *iface, ITfProperty *prop,
    const TF_PERSISTENT_PROPERTY_HEADER_ACP *header, IStream *stream, ITfPersistentPropertyLoaderACP *loader)
{
    Context *This = impl_from_ITextStoreACPServices(iface);

    FIXME("stub: %p %p %p %p %p\n", This, prop, header, stream, loader);

    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPServices_ForceLoadProperty(ITextStoreACPServices *iface, ITfProperty *prop)
{
    Context *This = impl_from_ITextStoreACPServices(iface);

    FIXME("stub: %p %p\n", This, prop);

    return E_NOTIMPL;
}

static HRESULT WINAPI TextStoreACPServices_CreateRange(ITextStoreACPServices *iface,
    LONG start, LONG end, ITfRangeACP **range)
{
    Context *This = impl_from_ITextStoreACPServices(iface);

    FIXME("stub: %p %d %d %p\n", This, start, end, range);

    return S_OK;
}

static const ITextStoreACPServicesVtbl TextStoreACPServicesVtbl =
{
    TextStoreACPServices_QueryInterface,
    TextStoreACPServices_AddRef,
    TextStoreACPServices_Release,
    TextStoreACPServices_Serialize,
    TextStoreACPServices_Unserialize,
    TextStoreACPServices_ForceLoadProperty,
    TextStoreACPServices_CreateRange
};

968
HRESULT Context_Constructor(TfClientId tidOwner, IUnknown *punk, ITfDocumentMgr *mgr, ITfContext **ppOut, TfEditCookie *pecTextStore)
969
{
970 971
    Context *This;
    EditCookie *cookie;
972

973
    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Context));
974 975 976
    if (This == NULL)
        return E_OUTOFMEMORY;

977 978 979 980 981 982 983 984 985 986 987 988 989 990
    cookie = HeapAlloc(GetProcessHeap(),0,sizeof(EditCookie));
    if (cookie == NULL)
    {
        HeapFree(GetProcessHeap(),0,This);
        return E_OUTOFMEMORY;
    }

    TRACE("(%p) %x %p %p %p\n",This, tidOwner, punk, ppOut, pecTextStore);

    This->ITfContext_iface.lpVtbl= &ContextVtbl;
    This->ITfSource_iface.lpVtbl = &ContextSourceVtbl;
    This->ITfInsertAtSelection_iface.lpVtbl = &InsertAtSelectionVtbl;
    This->ITfSourceSingle_iface.lpVtbl = &ContextSourceSingleVtbl;
    This->ITextStoreACPSink_iface.lpVtbl = &TextStoreACPSinkVtbl;
991
    This->ITextStoreACPServices_iface.lpVtbl = &TextStoreACPServicesVtbl;
992
    This->refCount = 1;
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    This->tidOwner = tidOwner;
    This->connected = FALSE;
    This->manager = mgr;

    CompartmentMgr_Constructor((IUnknown*)&This->ITfContext_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);

    cookie->lockType = TF_ES_READ;
    cookie->pOwningContext = This;

    if (punk)
    {
        IUnknown_QueryInterface(punk, &IID_ITextStoreACP,
                          (LPVOID*)&This->pITextStoreACP);

        IUnknown_QueryInterface(punk, &IID_ITfContextOwnerCompositionSink,
                                (LPVOID*)&This->pITfContextOwnerCompositionSink);
1009

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
        if (!This->pITextStoreACP && !This->pITfContextOwnerCompositionSink)
            FIXME("Unhandled pUnk\n");
    }

    This->defaultCookie = generate_Cookie(COOKIE_MAGIC_EDITCOOKIE,cookie);
    *pecTextStore = This->defaultCookie;

    list_init(&This->pContextKeyEventSink);
    list_init(&This->pEditTransactionSink);
    list_init(&This->pStatusSink);
    list_init(&This->pTextEditSink);
    list_init(&This->pTextLayoutSink);
1022

1023
    *ppOut = &This->ITfContext_iface;
1024
    TRACE("returning %p\n", *ppOut);
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

    return S_OK;
}

HRESULT Context_Initialize(ITfContext *iface, ITfDocumentMgr *manager)
{
    Context *This = impl_from_ITfContext(iface);

    if (This->pITextStoreACP)
        ITextStoreACP_AdviseSink(This->pITextStoreACP, &IID_ITextStoreACPSink,
            (IUnknown*)&This->ITextStoreACPSink_iface, TS_AS_ALL_SINKS);
    This->connected = TRUE;
    This->manager = manager;
    return S_OK;
}

HRESULT Context_Uninitialize(ITfContext *iface)
{
    Context *This = impl_from_ITfContext(iface);

    if (This->pITextStoreACP)
        ITextStoreACP_UnadviseSink(This->pITextStoreACP, (IUnknown*)&This->ITextStoreACPSink_iface);
    This->connected = FALSE;
    This->manager = NULL;
1049 1050
    return S_OK;
}