documentmgr.c 13.7 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 35 36 37 38 39 40 41 42 43
/*
 *  ITfDocumentMgr 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"

#include "wine/unicode.h"

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

WINE_DEFAULT_DEBUG_CHANNEL(msctf);

typedef struct tagDocumentMgr {
44 45
    ITfDocumentMgr ITfDocumentMgr_iface;
    ITfSource ITfSource_iface;
46
    LONG refCount;
47

48 49 50
    /* Aggregation */
    ITfCompartmentMgr  *CompartmentMgr;

51
    ITfContext*  contextStack[2]; /* limit of 2 contexts */
52
    ITfThreadMgrEventSink* ThreadMgrSink;
53 54

    struct list TransitoryExtensionSink;
55 56
} DocumentMgr;

57
typedef struct tagEnumTfContext {
58
    IEnumTfContexts IEnumTfContexts_iface;
59 60 61 62 63 64 65 66
    LONG refCount;

    DWORD   index;
    DocumentMgr *docmgr;
} EnumTfContext;

static HRESULT EnumTfContext_Constructor(DocumentMgr* mgr, IEnumTfContexts **ppOut);

67
static inline DocumentMgr *impl_from_ITfDocumentMgr(ITfDocumentMgr *iface)
68
{
69 70 71 72 73 74 75 76
    return CONTAINING_RECORD(iface, DocumentMgr, ITfDocumentMgr_iface);
}

static inline DocumentMgr *impl_from_ITfSource(ITfSource *iface)
{
    return CONTAINING_RECORD(iface, DocumentMgr, ITfSource_iface);
}

77
static inline EnumTfContext *impl_from_IEnumTfContexts(IEnumTfContexts *iface)
78 79
{
    return CONTAINING_RECORD(iface, EnumTfContext, IEnumTfContexts_iface);
80 81
}

82 83
static void DocumentMgr_Destructor(DocumentMgr *This)
{
84
    ITfThreadMgr *tm;
85
    TRACE("destroying %p\n", This);
86 87

    TF_GetThreadMgr(&tm);
88
    ThreadMgr_OnDocumentMgrDestruction(tm, &This->ITfDocumentMgr_iface);
89

90 91 92 93
    if (This->contextStack[0])
        ITfContext_Release(This->contextStack[0]);
    if (This->contextStack[1])
        ITfContext_Release(This->contextStack[1]);
94
    free_sinks(&This->TransitoryExtensionSink);
95
    CompartmentMgr_Destructor(This->CompartmentMgr);
96 97 98 99 100
    HeapFree(GetProcessHeap(),0,This);
}

static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
{
101
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
102 103 104 105
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfDocumentMgr))
    {
106
        *ppvOut = &This->ITfDocumentMgr_iface;
107
    }
108 109
    else if (IsEqualIID(iid, &IID_ITfSource))
    {
110
        *ppvOut = &This->ITfSource_iface;
111
    }
112 113 114 115
    else if (IsEqualIID(iid, &IID_ITfCompartmentMgr))
    {
        *ppvOut = This->CompartmentMgr;
    }
116 117 118

    if (*ppvOut)
    {
119
        ITfDocumentMgr_AddRef(iface);
120 121 122 123 124 125 126 127 128
        return S_OK;
    }

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

static ULONG WINAPI DocumentMgr_AddRef(ITfDocumentMgr *iface)
{
129
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
130 131 132 133 134
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
{
135
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    ULONG ret;

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

/*****************************************************
 * ITfDocumentMgr functions
 *****************************************************/
static HRESULT WINAPI DocumentMgr_CreateContext(ITfDocumentMgr *iface,
        TfClientId tidOwner,
        DWORD dwFlags, IUnknown *punk, ITfContext **ppic,
        TfEditCookie *pecTextStore)
{
152
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
153
    TRACE("(%p) 0x%x 0x%x %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
154
    return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
155 156 157 158
}

static HRESULT WINAPI DocumentMgr_Push(ITfDocumentMgr *iface, ITfContext *pic)
{
159
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
160 161 162 163 164 165 166
    ITfContext *check;

    TRACE("(%p) %p\n",This,pic);

    if (This->contextStack[1])  /* FUll */
        return TF_E_STACKFULL;

167
    if (!pic || FAILED(ITfContext_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
168 169
        return E_INVALIDARG;

170 171 172
    if (This->contextStack[0] == NULL)
        ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);

173 174 175
    This->contextStack[1] = This->contextStack[0];
    This->contextStack[0] = check;

176
    Context_Initialize(check, iface);
177
    ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
178

179
    return S_OK;
180 181 182 183
}

static HRESULT WINAPI DocumentMgr_Pop(ITfDocumentMgr *iface, DWORD dwFlags)
{
184
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
185 186 187 188
    TRACE("(%p) 0x%x\n",This,dwFlags);

    if (dwFlags == TF_POPF_ALL)
    {
189 190 191 192 193 194 195 196 197 198 199
        int i;

        for (i = 0; i < sizeof(This->contextStack)/sizeof(This->contextStack[0]); i++)
            if (This->contextStack[i])
            {
                ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink, This->contextStack[i]);
                Context_Uninitialize(This->contextStack[i]);
                ITfContext_Release(This->contextStack[i]);
                This->contextStack[i] = NULL;
            }

200
        ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
201 202 203 204 205 206
        return S_OK;
    }

    if (dwFlags)
        return E_INVALIDARG;

207
    if (This->contextStack[1] == NULL) /* Cannot pop last context */
208 209
        return E_FAIL;

210
    ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
211
    Context_Uninitialize(This->contextStack[0]);
212
    ITfContext_Release(This->contextStack[0]);
213 214 215
    This->contextStack[0] = This->contextStack[1];
    This->contextStack[1] = NULL;

216 217 218
    if (This->contextStack[0] == NULL)
        ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);

219
    return S_OK;
220 221 222 223
}

static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
{
224
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
225 226 227 228 229 230 231 232 233 234
    TRACE("(%p)\n",This);
    if (!ppic)
        return E_INVALIDARG;

    if (This->contextStack[0])
        ITfContext_AddRef(This->contextStack[0]);

    *ppic = This->contextStack[0];

    return S_OK;
235 236 237 238
}

static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
{
239
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
240 241
    ITfContext *tgt;

242 243 244 245 246
    TRACE("(%p)\n",This);
    if (!ppic)
        return E_INVALIDARG;

    if (This->contextStack[1])
247 248 249 250 251 252
        tgt = This->contextStack[1];
    else
        tgt = This->contextStack[0];

    if (tgt)
        ITfContext_AddRef(tgt);
253

254
    *ppic = tgt;
255 256

    return S_OK;
257 258 259 260
}

static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
{
261
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
262 263
    TRACE("(%p) %p\n",This,ppEnum);
    return EnumTfContext_Constructor(This, ppEnum);
264 265
}

266
static const ITfDocumentMgrVtbl DocumentMgrVtbl =
267 268 269 270 271 272 273 274 275 276 277 278
{
    DocumentMgr_QueryInterface,
    DocumentMgr_AddRef,
    DocumentMgr_Release,
    DocumentMgr_CreateContext,
    DocumentMgr_Push,
    DocumentMgr_Pop,
    DocumentMgr_GetTop,
    DocumentMgr_GetBase,
    DocumentMgr_EnumContexts
};

279
static HRESULT WINAPI DocumentMgrSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
280
{
281
    DocumentMgr *This = impl_from_ITfSource(iface);
282
    return ITfDocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, ppvOut);
283 284
}

285
static ULONG WINAPI DocumentMgrSource_AddRef(ITfSource *iface)
286
{
287
    DocumentMgr *This = impl_from_ITfSource(iface);
288
    return ITfDocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
289 290
}

291
static ULONG WINAPI DocumentMgrSource_Release(ITfSource *iface)
292
{
293
    DocumentMgr *This = impl_from_ITfSource(iface);
294
    return ITfDocumentMgr_Release(&This->ITfDocumentMgr_iface);
295 296 297 298 299
}

/*****************************************************
 * ITfSource functions
 *****************************************************/
300
static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
301 302
        REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
303
    DocumentMgr *This = impl_from_ITfSource(iface);
304 305 306 307 308 309 310 311 312 313 314 315 316 317

    TRACE("(%p) %s %p %p\n", This, debugstr_guid(riid), punk, pdwCookie);

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

    if (IsEqualIID(riid, &IID_ITfTransitoryExtensionSink))
    {
        WARN("semi-stub for ITfTransitoryExtensionSink: callback won't be used.\n");
        return advise_sink(&This->TransitoryExtensionSink, &IID_ITfTransitoryExtensionSink,
                           COOKIE_MAGIC_DMSINK, punk, pdwCookie);
    }

    FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
318 319 320
    return E_NOTIMPL;
}

321
static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
322
{
323
    DocumentMgr *This = impl_from_ITfSource(iface);
324 325 326 327 328 329 330

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

    if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_DMSINK)
        return E_INVALIDARG;

    return unadvise_sink(pdwCookie);
331 332
}

333
static const ITfSourceVtbl DocumentMgrSourceVtbl =
334
{
335 336 337
    DocumentMgrSource_QueryInterface,
    DocumentMgrSource_AddRef,
    DocumentMgrSource_Release,
338 339 340 341
    DocumentMgrSource_AdviseSink,
    DocumentMgrSource_UnadviseSink,
};

342
HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
343 344 345 346 347 348 349
{
    DocumentMgr *This;

    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(DocumentMgr));
    if (This == NULL)
        return E_OUTOFMEMORY;

350 351
    This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgrVtbl;
    This->ITfSource_iface.lpVtbl = &DocumentMgrSourceVtbl;
352
    This->refCount = 1;
353
    This->ThreadMgrSink = ThreadMgrSink;
354
    list_init(&This->TransitoryExtensionSink);
355

356
    CompartmentMgr_Constructor((IUnknown*)&This->ITfDocumentMgr_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
357

358
    *ppOut = &This->ITfDocumentMgr_iface;
359
    TRACE("returning %p\n", *ppOut);
360 361
    return S_OK;
}
362 363

/**************************************************
364
 * IEnumTfContexts implementation
365 366 367 368 369 370 371 372 373
 **************************************************/
static void EnumTfContext_Destructor(EnumTfContext *This)
{
    TRACE("destroying %p\n", This);
    HeapFree(GetProcessHeap(),0,This);
}

static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
{
374
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
375 376 377 378
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
    {
379
        *ppvOut = &This->IEnumTfContexts_iface;
380 381 382 383
    }

    if (*ppvOut)
    {
384
        IEnumTfContexts_AddRef(iface);
385 386 387 388 389 390 391 392 393
        return S_OK;
    }

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

static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
{
394
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
395 396 397 398 399
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
{
400
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
401 402 403 404 405 406 407 408 409 410 411
    ULONG ret;

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

static HRESULT WINAPI EnumTfContext_Next(IEnumTfContexts *iface,
    ULONG ulCount, ITfContext **rgContext, ULONG *pcFetched)
{
412
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    ULONG fetched = 0;

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

    if (rgContext == NULL) return E_POINTER;

    while (fetched < ulCount)
    {
        if (This->index > 1)
            break;

        if (!This->docmgr->contextStack[This->index])
            break;

        *rgContext = This->docmgr->contextStack[This->index];
        ITfContext_AddRef(*rgContext);

        ++This->index;
        ++fetched;
        ++rgContext;
    }

    if (pcFetched) *pcFetched = fetched;
    return fetched == ulCount ? S_OK : S_FALSE;
}

static HRESULT WINAPI EnumTfContext_Skip( IEnumTfContexts* iface, ULONG celt)
{
441
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
442 443 444 445 446 447 448
    TRACE("(%p)\n",This);
    This->index += celt;
    return S_OK;
}

static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
{
449
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
450 451 452 453 454 455 456 457
    TRACE("(%p)\n",This);
    This->index = 0;
    return S_OK;
}

static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
    IEnumTfContexts **ppenum)
{
458
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
459 460 461 462 463 464 465 466 467
    HRESULT res;

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

    if (ppenum == NULL) return E_POINTER;

    res = EnumTfContext_Constructor(This->docmgr, ppenum);
    if (SUCCEEDED(res))
    {
468
        EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
        new_This->index = This->index;
    }
    return res;
}

static const IEnumTfContextsVtbl IEnumTfContexts_Vtbl ={
    EnumTfContext_QueryInterface,
    EnumTfContext_AddRef,
    EnumTfContext_Release,

    EnumTfContext_Clone,
    EnumTfContext_Next,
    EnumTfContext_Reset,
    EnumTfContext_Skip
};

static HRESULT EnumTfContext_Constructor(DocumentMgr *mgr, IEnumTfContexts **ppOut)
{
    EnumTfContext *This;

    This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfContext));
    if (This == NULL)
        return E_OUTOFMEMORY;

493
    This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
494 495 496
    This->refCount = 1;
    This->docmgr = mgr;

497
    *ppOut = &This->IEnumTfContexts_iface;
498
    TRACE("returning %p\n", *ppOut);
499 500
    return S_OK;
}