documentmgr.c 13 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
} DocumentMgr;

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

    DWORD   index;
    DocumentMgr *docmgr;
} EnumTfContext;

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

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

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

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

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

    TF_GetThreadMgr(&tm);
86
    ThreadMgr_OnDocumentMgrDestruction(tm, &This->ITfDocumentMgr_iface);
87

88 89 90 91
    if (This->contextStack[0])
        ITfContext_Release(This->contextStack[0]);
    if (This->contextStack[1])
        ITfContext_Release(This->contextStack[1]);
92
    CompartmentMgr_Destructor(This->CompartmentMgr);
93 94 95 96 97
    HeapFree(GetProcessHeap(),0,This);
}

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

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

    if (*ppvOut)
    {
        IUnknown_AddRef(iface);
        return S_OK;
    }

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

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

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

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

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

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

    if (!pic || FAILED(IUnknown_QueryInterface(pic,&IID_ITfContext,(LPVOID*) &check)))
        return E_INVALIDARG;

167 168 169
    if (This->contextStack[0] == NULL)
        ITfThreadMgrEventSink_OnInitDocumentMgr(This->ThreadMgrSink,iface);

170 171 172
    This->contextStack[1] = This->contextStack[0];
    This->contextStack[0] = check;

173
    Context_Initialize(check, iface);
174
    ITfThreadMgrEventSink_OnPushContext(This->ThreadMgrSink,check);
175

176
    return S_OK;
177 178 179 180
}

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

    if (dwFlags == TF_POPF_ALL)
    {
        if (This->contextStack[0])
187 188
        {
            ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[0]);
189
            ITfContext_Release(This->contextStack[0]);
190
            Context_Uninitialize(This->contextStack[0]);
191
        }
192
        if (This->contextStack[1])
193 194
        {
            ITfThreadMgrEventSink_OnPopContext(This->ThreadMgrSink,This->contextStack[1]);
195
            ITfContext_Release(This->contextStack[1]);
196
            Context_Uninitialize(This->contextStack[1]);
197
        }
198
        This->contextStack[0] = This->contextStack[1] = NULL;
199
        ITfThreadMgrEventSink_OnUninitDocumentMgr(This->ThreadMgrSink, iface);
200 201 202 203 204 205
        return S_OK;
    }

    if (dwFlags)
        return E_INVALIDARG;

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

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

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

218
    return S_OK;
219 220 221 222
}

static HRESULT WINAPI DocumentMgr_GetTop(ITfDocumentMgr *iface, ITfContext **ppic)
{
223
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
224 225 226 227 228 229 230 231 232 233
    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;
234 235 236 237
}

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

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

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

    if (tgt)
        ITfContext_AddRef(tgt);
252

253
    *ppic = tgt;
254 255

    return S_OK;
256 257 258 259
}

static HRESULT WINAPI DocumentMgr_EnumContexts(ITfDocumentMgr *iface, IEnumTfContexts **ppEnum)
{
260
    DocumentMgr *This = impl_from_ITfDocumentMgr(iface);
261 262
    TRACE("(%p) %p\n",This,ppEnum);
    return EnumTfContext_Constructor(This, ppEnum);
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
}

static const ITfDocumentMgrVtbl DocumentMgr_DocumentMgrVtbl =
{
    DocumentMgr_QueryInterface,
    DocumentMgr_AddRef,
    DocumentMgr_Release,

    DocumentMgr_CreateContext,
    DocumentMgr_Push,
    DocumentMgr_Pop,
    DocumentMgr_GetTop,
    DocumentMgr_GetBase,
    DocumentMgr_EnumContexts
};

279 280 281

static HRESULT WINAPI Source_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
{
282 283
    DocumentMgr *This = impl_from_ITfSource(iface);
    return DocumentMgr_QueryInterface(&This->ITfDocumentMgr_iface, iid, *ppvOut);
284 285 286 287
}

static ULONG WINAPI Source_AddRef(ITfSource *iface)
{
288 289
    DocumentMgr *This = impl_from_ITfSource(iface);
    return DocumentMgr_AddRef(&This->ITfDocumentMgr_iface);
290 291 292 293
}

static ULONG WINAPI Source_Release(ITfSource *iface)
{
294 295
    DocumentMgr *This = impl_from_ITfSource(iface);
    return DocumentMgr_Release(&This->ITfDocumentMgr_iface);
296 297 298 299 300
}

/*****************************************************
 * ITfSource functions
 *****************************************************/
301
static HRESULT WINAPI DocumentMgrSource_AdviseSink(ITfSource *iface,
302 303
        REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
304
    DocumentMgr *This = impl_from_ITfSource(iface);
305 306 307 308
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

309
static HRESULT WINAPI DocumentMgrSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
310
{
311
    DocumentMgr *This = impl_from_ITfSource(iface);
312 313 314 315 316 317 318 319 320 321 322 323 324 325
    FIXME("STUB:(%p)\n",This);
    return E_NOTIMPL;
}

static const ITfSourceVtbl DocumentMgr_SourceVtbl =
{
    Source_QueryInterface,
    Source_AddRef,
    Source_Release,

    DocumentMgrSource_AdviseSink,
    DocumentMgrSource_UnadviseSink,
};

326
HRESULT DocumentMgr_Constructor(ITfThreadMgrEventSink *ThreadMgrSink, ITfDocumentMgr **ppOut)
327 328 329 330 331 332 333
{
    DocumentMgr *This;

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

334 335
    This->ITfDocumentMgr_iface.lpVtbl = &DocumentMgr_DocumentMgrVtbl;
    This->ITfSource_iface.lpVtbl = &DocumentMgr_SourceVtbl;
336
    This->refCount = 1;
337
    This->ThreadMgrSink = ThreadMgrSink;
338

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

341
    TRACE("returning %p\n", This);
342
    *ppOut = &This->ITfDocumentMgr_iface;
343 344
    return S_OK;
}
345 346

/**************************************************
347
 * IEnumTfContexts implementation
348 349 350 351 352 353 354 355 356
 **************************************************/
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)
{
357
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    *ppvOut = NULL;

    if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfContexts))
    {
        *ppvOut = This;
    }

    if (*ppvOut)
    {
        IUnknown_AddRef(iface);
        return S_OK;
    }

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

static ULONG WINAPI EnumTfContext_AddRef(IEnumTfContexts *iface)
{
377
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
378 379 380 381 382
    return InterlockedIncrement(&This->refCount);
}

static ULONG WINAPI EnumTfContext_Release(IEnumTfContexts *iface)
{
383
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
384 385 386 387 388 389 390 391 392 393 394
    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)
{
395
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    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)
{
424
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
425 426 427 428 429 430 431
    TRACE("(%p)\n",This);
    This->index += celt;
    return S_OK;
}

static HRESULT WINAPI EnumTfContext_Reset( IEnumTfContexts* iface)
{
432
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
433 434 435 436 437 438 439 440
    TRACE("(%p)\n",This);
    This->index = 0;
    return S_OK;
}

static HRESULT WINAPI EnumTfContext_Clone( IEnumTfContexts *iface,
    IEnumTfContexts **ppenum)
{
441
    EnumTfContext *This = impl_from_IEnumTfContexts(iface);
442 443 444 445 446 447 448 449 450
    HRESULT res;

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

    if (ppenum == NULL) return E_POINTER;

    res = EnumTfContext_Constructor(This->docmgr, ppenum);
    if (SUCCEEDED(res))
    {
451
        EnumTfContext *new_This = impl_from_IEnumTfContexts(*ppenum);
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        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;

476
    This->IEnumTfContexts_iface.lpVtbl = &IEnumTfContexts_Vtbl;
477 478 479 480
    This->refCount = 1;
    This->docmgr = mgr;

    TRACE("returning %p\n", This);
481
    *ppOut = &This->IEnumTfContexts_iface;
482 483
    return S_OK;
}