documentmgr.c 13.9 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
/*
 *  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 <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 "msctf.h"
#include "msctf_internal.h"

WINE_DEFAULT_DEBUG_CHANNEL(msctf);

typedef struct tagDocumentMgr {
40 41
    ITfDocumentMgr ITfDocumentMgr_iface;
    ITfSource ITfSource_iface;
42
    LONG refCount;
43

44 45 46
    /* Aggregation */
    ITfCompartmentMgr  *CompartmentMgr;

47
    ITfContext* initialContext;
48
    ITfContext*  contextStack[2]; /* limit of 2 contexts */
49
    ITfThreadMgrEventSink* ThreadMgrSink;
50 51

    struct list TransitoryExtensionSink;
52 53
} DocumentMgr;

54
typedef struct tagEnumTfContext {
55
    IEnumTfContexts IEnumTfContexts_iface;
56 57 58 59 60 61 62 63
    LONG refCount;

    DWORD   index;
    DocumentMgr *docmgr;
} EnumTfContext;

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

64
static inline DocumentMgr *impl_from_ITfDocumentMgr(ITfDocumentMgr *iface)
65
{
66 67 68 69 70 71 72 73
    return CONTAINING_RECORD(iface, DocumentMgr, ITfDocumentMgr_iface);
}

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

74
static inline EnumTfContext *impl_from_IEnumTfContexts(IEnumTfContexts *iface)
75 76
{
    return CONTAINING_RECORD(iface, EnumTfContext, IEnumTfContexts_iface);
77 78
}

79 80
static void DocumentMgr_Destructor(DocumentMgr *This)
{
81
    ITfThreadMgr *tm = NULL;
82
    TRACE("destroying %p\n", This);
83 84

    TF_GetThreadMgr(&tm);
85 86 87 88 89
    if (tm)
    {
        ThreadMgr_OnDocumentMgrDestruction(tm, &This->ITfDocumentMgr_iface);
        ITfThreadMgr_Release(tm);
    }
90

91 92
    if (This->initialContext)
        ITfContext_Release(This->initialContext);
93 94 95 96
    if (This->contextStack[0])
        ITfContext_Release(This->contextStack[0]);
    if (This->contextStack[1])
        ITfContext_Release(This->contextStack[1]);
97
    free_sinks(&This->TransitoryExtensionSink);
98
    CompartmentMgr_Destructor(This->CompartmentMgr);
99
    free(This);
100 101 102 103
}

static HRESULT WINAPI DocumentMgr_QueryInterface(ITfDocumentMgr *iface, REFIID iid, LPVOID *ppvOut)
{
104
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
105 106 107 108
    *ppvOut = NULL;

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

    if (*ppvOut)
    {
122
        ITfDocumentMgr_AddRef(iface);
123 124 125 126 127 128 129 130 131
        return S_OK;
    }

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

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

static ULONG WINAPI DocumentMgr_Release(ITfDocumentMgr *iface)
{
138
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    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)
{
155
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
156
    TRACE("(%p) 0x%lx 0x%lx %p %p %p\n",This,tidOwner,dwFlags,punk,ppic,pecTextStore);
157
    return Context_Constructor(tidOwner, punk, iface, ppic, pecTextStore);
158 159 160 161
}

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

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

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

170
    if (!pic || FAILED(ITfContext_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
171 172
        return E_INVALIDARG;

173 174 175
    if (This->contextStack[0] == NULL)
        ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);

176 177 178
    This->contextStack[1] = This->contextStack[0];
    This->contextStack[0] = check;

179
    Context_Initialize(check, iface);
180
    ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
181

182
    return S_OK;
183 184 185 186
}

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

    if (dwFlags == TF_POPF_ALL)
    {
192 193
        int i;

194
        for (i = 0; i < ARRAY_SIZE(This->contextStack); i++)
195 196 197 198 199 200 201 202
            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;
            }

203
        ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
204 205 206 207 208 209
        return S_OK;
    }

    if (dwFlags)
        return E_INVALIDARG;

210
    if (This->contextStack[1] == NULL) /* Cannot pop last context */
211 212
        return E_FAIL;

213
    ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
214
    Context_Uninitialize(This->contextStack[0]);
215
    ITfContext_Release(This->contextStack[0]);
216 217 218
    This->contextStack[0] = This->contextStack[1];
    This->contextStack[1] = NULL;

219 220 221
    if (This->contextStack[0] == NULL)
        ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);

222
    return S_OK;
223 224 225 226
}

static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
{
227
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
228 229
    ITfContext *tgt;

230 231 232 233 234
    TRACE("(%p)\n",This);
    if (!ppic)
        return E_INVALIDARG;

    if (This->contextStack[0])
235 236 237 238 239 240
        tgt = This->contextStack[0];
    else
        tgt = This->initialContext;

    if (tgt)
        ITfContext_AddRef(tgt);
241

242
    *ppic = tgt;
243 244

    return S_OK;
245 246 247 248
}

static HRESULT WINAPI DocumentMgr_GetBase(ITfDocumentMgr *iface, ITfContext **ppic)
{
249
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
250 251
    ITfContext *tgt;

252 253 254 255 256
    TRACE("(%p)\n",This);
    if (!ppic)
        return E_INVALIDARG;

    if (This->contextStack[1])
257
        tgt = This->contextStack[1];
258
    else if (This->contextStack[0])
259
        tgt = This->contextStack[0];
260 261
    else
        tgt = This->initialContext;
262 263 264

    if (tgt)
        ITfContext_AddRef(tgt);
265

266
    *ppic = tgt;
267 268

    return S_OK;
269 270 271 272
}

static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
{
273
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
274 275
    TRACE("(%p) %p\n",This,ppEnum);
    return EnumTfContext_Constructor(This, ppEnum);
276 277
}

278
static const ITfDocumentMgrVtbl DocumentMgrVtbl =
279 280 281 282 283 284 285 286 287 288 289 290
{
    DocumentMgr_QueryInterface,
    DocumentMgr_AddRef,
    DocumentMgr_Release,
    DocumentMgr_CreateContext,
    DocumentMgr_Push,
    DocumentMgr_Pop,
    DocumentMgr_GetTop,
    DocumentMgr_GetBase,
    DocumentMgr_EnumContexts
};

291
static HRESULT WINAPI DocumentMgrSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
292
{
293
    DocumentMgr *This = impl_from_ITfSource(iface);
294
    return ITfDocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, ppvOut);
295 296
}

297
static ULONG WINAPI DocumentMgrSource_AddRef(ITfSource *iface)
298
{
299
    DocumentMgr *This = impl_from_ITfSource(iface);
300
    return ITfDocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
301 302
}

303
static ULONG WINAPI DocumentMgrSource_Release(ITfSource *iface)
304
{
305
    DocumentMgr *This = impl_from_ITfSource(iface);
306
    return ITfDocumentMgr_Release(&This->ITfDocumentMgr_iface);
307 308 309 310 311
}

/*****************************************************
 * ITfSource functions
 *****************************************************/
312
static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
313 314
        REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
315
    DocumentMgr *This = impl_from_ITfSource(iface);
316 317 318 319 320 321 322 323 324 325 326 327 328 329

    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));
330 331 332
    return E_NOTIMPL;
}

333
static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
334
{
335
    DocumentMgr *This = impl_from_ITfSource(iface);
336

337
    TRACE("(%p) %lx\n",This,pdwCookie);
338 339 340 341 342

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

    return unadvise_sink(pdwCookie);
343 344
}

345
static const ITfSourceVtbl DocumentMgrSourceVtbl =
346
{
347 348 349
    DocumentMgrSource_QueryInterface,
    DocumentMgrSource_AddRef,
    DocumentMgrSource_Release,
350 351 352 353
    DocumentMgrSource_AdviseSink,
    DocumentMgrSource_UnadviseSink,
};

354
HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
355 356
{
    DocumentMgr *This;
357
    DWORD cookie;
358

359
    This = calloc(1, sizeof(DocumentMgr));
360 361 362
    if (This == NULL)
        return E_OUTOFMEMORY;

363 364
    This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgrVtbl;
    This->ITfSource_iface.lpVtbl = &DocumentMgrSourceVtbl;
365
    This->refCount = 1;
366
    This->ThreadMgrSink = ThreadMgrSink;
367
    list_init(&This->TransitoryExtensionSink);
368

369
    CompartmentMgr_Constructor((IUnknown*)&This->ITfDocumentMgr_iface, &IID_IUnknown, (IUnknown**)&This->CompartmentMgr);
370
    Context_Constructor(processId,NULL,&This->ITfDocumentMgr_iface, &This->initialContext, &cookie);
371

372
    *ppOut = &This->ITfDocumentMgr_iface;
373
    TRACE("returning %p\n", *ppOut);
374 375
    return S_OK;
}
376 377

/**************************************************
378
 * IEnumTfContexts implementation
379 380 381 382
 **************************************************/
static void EnumTfContext_Destructor(EnumTfContext *This)
{
    TRACE("destroying %p\n", This);
383
    free(This);
384 385 386 387
}

static HRESULT WINAPI EnumTfContext_QueryInterface(IEnumTfContexts *iface, REFIID iid, LPVOID *ppvOut)
{
388
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
389 390 391 392
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
    {
393
        *ppvOut = &This->IEnumTfContexts_iface;
394 395 396 397
    }

    if (*ppvOut)
    {
398
        IEnumTfContexts_AddRef(iface);
399 400 401 402 403 404 405 406 407
        return S_OK;
    }

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

static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
{
408
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
409 410 411 412 413
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
{
414
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
415 416 417 418 419 420 421 422 423 424 425
    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)
{
426
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
    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)
{
455
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
456 457 458 459 460 461 462
    TRACE("(%p)\n",This);
    This->index += celt;
    return S_OK;
}

static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
{
463
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
464 465 466 467 468 469 470 471
    TRACE("(%p)\n",This);
    This->index = 0;
    return S_OK;
}

static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
    IEnumTfContexts **ppenum)
{
472
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
473 474 475 476 477 478 479 480 481
    HRESULT res;

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

    if (ppenum == NULL) return E_POINTER;

    res = EnumTfContext_Constructor(This->docmgr, ppenum);
    if (SUCCEEDED(res))
    {
482
        EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
        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;

503
    This = calloc(1, sizeof(EnumTfContext));
504 505 506
    if (This == NULL)
        return E_OUTOFMEMORY;

507
    This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
508 509 510
    This->refCount = 1;
    This->docmgr = mgr;

511
    *ppOut = &This->IEnumTfContexts_iface;
512
    TRACE("returning %p\n", *ppOut);
513 514
    return S_OK;
}