richole.c 154 KB
Newer Older
1
/*
2
 * RichEdit GUIDs and OLE interface
3 4
 *
 * Copyright 2004 by Krzysztof Foltman
5
 * Copyright 2004 Aric Stewart
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22
#include <stdarg.h>
23

24 25 26 27 28 29 30 31 32
#define NONAMELESSUNION
#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winuser.h"
#include "ole2.h"
#include "richole.h"
33
#include "editor.h"
34
#include "richedit.h"
35
#include "tom.h"
36
#include "wine/debug.h"
37

38
WINE_DEFAULT_DEBUG_CHANNEL(richedit);
39

40 41
/* there is no way to be consistent across different sets of headers - mingw, Wine, Win32 SDK*/

42
#include "initguid.h"
43 44

DEFINE_GUID(LIBID_tom, 0x8cc497c9, 0xa1df, 0x11ce, 0x80, 0x98, 0x00, 0xaa, 0x00, 0x47, 0xbe, 0x5d);
45 46 47
DEFINE_GUID(IID_ITextServices, 0x8d33f740, 0xcf58, 0x11ce, 0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5);
DEFINE_GUID(IID_ITextHost, 0x13e670f4,0x1a5a,0x11cf,0xab,0xeb,0x00,0xaa,0x00,0xb6,0x5e,0xa1);
DEFINE_GUID(IID_ITextHost2, 0x13e670f5,0x1a5a,0x11cf,0xab,0xeb,0x00,0xaa,0x00,0xb6,0x5e,0xa1);
48
DEFINE_GUID(IID_ITextDocument, 0x8cc497c0, 0xa1df, 0x11ce, 0x80, 0x98, 0x00, 0xaa, 0x00, 0x47, 0xbe, 0x5d);
49
DEFINE_GUID(IID_ITextDocument2Old, 0x01c25500, 0x4268, 0x11d1, 0x88, 0x3a, 0x3c, 0x8b, 0x00, 0xc1, 0x00, 0x00);
50 51
DEFINE_GUID(IID_ITextRange, 0x8cc497c2, 0xa1df, 0x11ce, 0x80, 0x98, 0x00, 0xaa, 0x00, 0x47, 0xbe, 0x5d);
DEFINE_GUID(IID_ITextSelection, 0x8cc497c1, 0xa1df, 0x11ce, 0x80, 0x98, 0x00, 0xaa, 0x00, 0x47, 0xbe, 0x5d);
52
DEFINE_GUID(IID_ITextFont, 0x8cc497c3, 0xa1df, 0x11ce, 0x80, 0x98, 0x00, 0xaa, 0x00, 0x47, 0xbe, 0x5d);
53
DEFINE_GUID(IID_ITextPara, 0x8cc497c4, 0xa1df, 0x11ce, 0x80, 0x98, 0x00, 0xaa, 0x00, 0x47, 0xbe, 0x5d);
54

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static ITypeLib *typelib;

enum tid_t {
    NULL_tid,
    ITextDocument_tid,
    ITextRange_tid,
    ITextSelection_tid,
    ITextFont_tid,
    ITextPara_tid,
    LAST_tid
};

static const IID * const tid_ids[] =
{
    &IID_NULL,
    &IID_ITextDocument,
    &IID_ITextRange,
    &IID_ITextSelection,
    &IID_ITextFont,
    &IID_ITextPara,
};
static ITypeInfo *typeinfos[LAST_tid];

static HRESULT load_typelib(void)
{
    ITypeLib *tl;
    HRESULT hr;

    hr = LoadRegTypeLib(&LIBID_tom, 1, 0, LOCALE_SYSTEM_DEFAULT, &tl);
    if (FAILED(hr)) {
        ERR("LoadRegTypeLib failed: %08x\n", hr);
        return hr;
    }

    if (InterlockedCompareExchangePointer((void**)&typelib, tl, NULL))
        ITypeLib_Release(tl);
    return hr;
}

void release_typelib(void)
{
    unsigned i;

    if (!typelib)
        return;

101
    for (i = 0; i < ARRAY_SIZE(typeinfos); i++)
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        if (typeinfos[i])
            ITypeInfo_Release(typeinfos[i]);

    ITypeLib_Release(typelib);
}

static HRESULT get_typeinfo(enum tid_t tid, ITypeInfo **typeinfo)
{
    HRESULT hr;

    if (!typelib)
        hr = load_typelib();
    if (!typelib)
        return hr;

    if (!typeinfos[tid])
    {
        ITypeInfo *ti;

        hr = ITypeLib_GetTypeInfoOfGuid(typelib, tid_ids[tid], &ti);
        if (FAILED(hr))
        {
            ERR("GetTypeInfoOfGuid(%s) failed: %08x\n", debugstr_guid(tid_ids[tid]), hr);
            return hr;
        }

        if (InterlockedCompareExchangePointer((void**)(typeinfos+tid), ti, NULL))
            ITypeInfo_Release(ti);
    }

    *typeinfo = typeinfos[tid];
    return S_OK;
}

136 137 138
/* private IID used to get back IRichEditOleImpl pointer */
DEFINE_GUID(IID_Igetrichole, 0xe3ce5c7a, 0x8247, 0x4622, 0x81, 0xad, 0x11, 0x81, 0x02, 0xaa, 0x01, 0x30);

139
typedef struct ITextSelectionImpl ITextSelectionImpl;
140
typedef struct IOleClientSiteImpl IOleClientSiteImpl;
141
typedef struct ITextRangeImpl ITextRangeImpl;
142

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
enum textfont_prop_id {
    FONT_ALLCAPS = 0,
    FONT_ANIMATION,
    FONT_BACKCOLOR,
    FONT_BOLD,
    FONT_EMBOSS,
    FONT_FORECOLOR,
    FONT_HIDDEN,
    FONT_ENGRAVE,
    FONT_ITALIC,
    FONT_KERNING,
    FONT_LANGID,
    FONT_NAME,
    FONT_OUTLINE,
    FONT_POSITION,
    FONT_PROTECTED,
    FONT_SHADOW,
    FONT_SIZE,
    FONT_SMALLCAPS,
    FONT_SPACING,
    FONT_STRIKETHROUGH,
    FONT_SUBSCRIPT,
    FONT_SUPERSCRIPT,
    FONT_UNDERLINE,
    FONT_WEIGHT,
168 169
    FONT_PROPID_LAST,
    FONT_PROPID_FIRST = FONT_ALLCAPS
170 171
};

172 173 174
static const DWORD textfont_prop_masks[][2] = {
    { CFM_ALLCAPS,     CFE_ALLCAPS },
    { CFM_ANIMATION },
175
    { CFM_BACKCOLOR,   CFE_AUTOBACKCOLOR },
176 177
    { CFM_BOLD,        CFE_BOLD },
    { CFM_EMBOSS,      CFE_EMBOSS },
178
    { CFM_COLOR,       CFE_AUTOCOLOR },
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    { CFM_HIDDEN,      CFE_HIDDEN },
    { CFM_IMPRINT,     CFE_IMPRINT },
    { CFM_ITALIC,      CFE_ITALIC },
    { CFM_KERNING },
    { CFM_LCID },
    { CFM_FACE },
    { CFM_OUTLINE,     CFE_OUTLINE },
    { CFM_OFFSET },
    { CFM_PROTECTED,   CFE_PROTECTED },
    { CFM_SHADOW,      CFE_SHADOW },
    { CFM_SIZE },
    { CFM_SMALLCAPS,   CFE_SMALLCAPS },
    { CFM_SPACING },
    { CFM_STRIKEOUT,   CFE_STRIKEOUT },
    { CFM_SUBSCRIPT,   CFE_SUBSCRIPT },
    { CFM_SUPERSCRIPT, CFE_SUPERSCRIPT },
    { CFM_UNDERLINE,   CFE_UNDERLINE },
    { CFM_WEIGHT }
197 198 199 200 201 202 203 204
};

typedef union {
    FLOAT f;
    LONG  l;
    BSTR  str;
} textfont_prop_val;

205 206 207 208
enum range_update_op {
    RANGE_UPDATE_DELETE
};

209
typedef struct IRichEditOleImpl {
210
    IUnknown IUnknown_inner;
211
    IRichEditOle IRichEditOle_iface;
212
    ITextDocument2Old ITextDocument2Old_iface;
213
    IUnknown *outer_unk;
214 215 216
    LONG ref;

    ME_TextEditor *editor;
217
    ITextSelectionImpl *txtSel;
218

219
    struct list rangelist;
220
    struct list clientsites;
221 222
} IRichEditOleImpl;

223 224 225 226 227
struct reole_child {
    struct list entry;
    IRichEditOleImpl *reole;
};

228
struct ITextRangeImpl {
229
    struct reole_child child;
230 231 232 233 234
    ITextRange ITextRange_iface;
    LONG ref;
    LONG start, end;
};

235
struct ITextSelectionImpl {
236
    ITextSelection ITextSelection_iface;
237 238 239 240 241
    LONG ref;

    IRichEditOleImpl *reOle;
};

242 243 244 245 246
typedef struct ITextFontImpl {
    ITextFont ITextFont_iface;
    LONG ref;

    ITextRange *range;
247
    textfont_prop_val props[FONT_PROPID_LAST];
248
    BOOL get_cache_enabled;
249
    BOOL set_cache_enabled;
250 251
} ITextFontImpl;

252 253 254 255 256 257 258
typedef struct ITextParaImpl {
    ITextPara ITextPara_iface;
    LONG ref;

    ITextRange *range;
} ITextParaImpl;

259
struct IOleClientSiteImpl {
260
    struct reole_child child;
261
    IOleClientSite IOleClientSite_iface;
262
    IOleInPlaceSite IOleInPlaceSite_iface;
263 264 265
    LONG ref;
};

266 267
static inline IRichEditOleImpl *impl_from_IRichEditOle(IRichEditOle *iface)
{
268
    return CONTAINING_RECORD(iface, IRichEditOleImpl, IRichEditOle_iface);
269 270
}

271
static inline IRichEditOleImpl *impl_from_ITextDocument2Old(ITextDocument2Old *iface)
272
{
273
    return CONTAINING_RECORD(iface, IRichEditOleImpl, ITextDocument2Old_iface);
274 275
}

276
static inline IRichEditOleImpl *impl_from_IUnknown(IUnknown *iface)
277
{
278 279
    return CONTAINING_RECORD(iface, IRichEditOleImpl, IUnknown_inner);
}
280

281 282 283 284 285
static inline IOleClientSiteImpl *impl_from_IOleInPlaceSite(IOleInPlaceSite *iface)
{
    return CONTAINING_RECORD(iface, IOleClientSiteImpl, IOleInPlaceSite_iface);
}

286 287 288 289 290
static inline ITextRangeImpl *impl_from_ITextRange(ITextRange *iface)
{
    return CONTAINING_RECORD(iface, ITextRangeImpl, ITextRange_iface);
}

291 292 293 294 295 296 297 298 299 300
static inline ITextSelectionImpl *impl_from_ITextSelection(ITextSelection *iface)
{
    return CONTAINING_RECORD(iface, ITextSelectionImpl, ITextSelection_iface);
}

static inline ITextFontImpl *impl_from_ITextFont(ITextFont *iface)
{
    return CONTAINING_RECORD(iface, ITextFontImpl, ITextFont_iface);
}

301 302 303 304 305
static inline ITextParaImpl *impl_from_ITextPara(ITextPara *iface)
{
    return CONTAINING_RECORD(iface, ITextParaImpl, ITextPara_iface);
}

306
static HRESULT create_textfont(ITextRange*, const ITextFontImpl*, ITextFont**);
307
static HRESULT create_textpara(ITextRange*, ITextPara**);
308
static ITextSelectionImpl *CreateTextSelection(IRichEditOleImpl*);
309

310 311 312 313 314 315 316 317 318
static HRESULT textrange_get_storylength(ME_TextEditor *editor, LONG *length)
{
    if (!length)
        return E_INVALIDARG;

    *length = ME_GetTextLength(editor) + 1;
    return S_OK;
}

319 320 321 322
static void textranges_update_ranges(IRichEditOleImpl *reole, LONG start, LONG end, enum range_update_op op)
{
    ITextRangeImpl *range;

323
    LIST_FOR_EACH_ENTRY(range, &reole->rangelist, ITextRangeImpl, child.entry) {
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
        switch (op)
        {
        case RANGE_UPDATE_DELETE:
            /* range fully covered by deleted range - collapse to insertion point */
            if (range->start >= start && range->end <= end)
                range->start = range->end = start;
            /* deleted range cuts from the right */
            else if (range->start < start && range->end <= end)
                range->end = start;
            /* deleted range cuts from the left */
            else if (range->start >= start && range->end > end) {
                range->start = start;
                range->end -= end - start;
            }
            /* deleted range cuts within */
            else
                range->end -= end - start;
            break;
        default:
            FIXME("unknown update op, %d\n", op);
        }
    }
}

348 349 350 351 352
static inline BOOL is_equal_textfont_prop_value(enum textfont_prop_id propid, textfont_prop_val *left,
    textfont_prop_val *right)
{
    switch (propid)
    {
353 354 355
    case FONT_ALLCAPS:
    case FONT_ANIMATION:
    case FONT_BACKCOLOR:
356
    case FONT_BOLD:
357
    case FONT_EMBOSS:
358
    case FONT_FORECOLOR:
359 360
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
361
    case FONT_ITALIC:
362
    case FONT_KERNING:
363
    case FONT_LANGID:
364 365 366 367
    case FONT_OUTLINE:
    case FONT_PROTECTED:
    case FONT_SHADOW:
    case FONT_SMALLCAPS:
368
    case FONT_STRIKETHROUGH:
369 370
    case FONT_SUBSCRIPT:
    case FONT_SUPERSCRIPT:
371
    case FONT_UNDERLINE:
372
    case FONT_WEIGHT:
373
        return left->l == right->l;
374
    case FONT_NAME:
375
        return !wcscmp(left->str, right->str);
376
    case FONT_POSITION:
377
    case FONT_SIZE:
378
    case FONT_SPACING:
379 380 381 382 383 384 385 386 387 388 389
       return left->f == right->f;
    default:
        FIXME("unhandled font property %d\n", propid);
        return FALSE;
    }
}

static inline void init_textfont_prop_value(enum textfont_prop_id propid, textfont_prop_val *v)
{
    switch (propid)
    {
390 391 392
    case FONT_ALLCAPS:
    case FONT_ANIMATION:
    case FONT_BACKCOLOR:
393
    case FONT_BOLD:
394
    case FONT_EMBOSS:
395
    case FONT_FORECOLOR:
396 397
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
398
    case FONT_ITALIC:
399
    case FONT_KERNING:
400
    case FONT_LANGID:
401 402 403 404
    case FONT_OUTLINE:
    case FONT_PROTECTED:
    case FONT_SHADOW:
    case FONT_SMALLCAPS:
405
    case FONT_STRIKETHROUGH:
406 407
    case FONT_SUBSCRIPT:
    case FONT_SUPERSCRIPT:
408
    case FONT_UNDERLINE:
409
    case FONT_WEIGHT:
410 411
        v->l = tomUndefined;
        return;
412 413 414
    case FONT_NAME:
        v->str = NULL;
        return;
415
    case FONT_POSITION:
416
    case FONT_SIZE:
417
    case FONT_SPACING:
418 419 420 421 422 423 424 425 426
        v->f = tomUndefined;
        return;
    default:
        FIXME("unhandled font property %d\n", propid);
        v->l = tomUndefined;
        return;
    }
}

427 428 429 430 431
static inline FLOAT twips_to_points(LONG value)
{
    return value * 72.0 / 1440;
}

432 433 434 435 436
static inline FLOAT points_to_twips(FLOAT value)
{
    return value * 1440 / 72.0;
}

437 438
static HRESULT get_textfont_prop_for_pos(const IRichEditOleImpl *reole, int pos, enum textfont_prop_id propid,
    textfont_prop_val *value)
439 440 441 442 443 444
{
    ME_Cursor from, to;
    CHARFORMAT2W fmt;

    memset(&fmt, 0, sizeof(fmt));
    fmt.cbSize = sizeof(fmt);
445
    fmt.dwMask = textfont_prop_masks[propid][0];
446 447 448

    ME_CursorFromCharOfs(reole->editor, pos, &from);
    to = from;
449
    ME_MoveCursorChars(reole->editor, &to, 1, FALSE);
450 451 452 453
    ME_GetCharFormat(reole->editor, &from, &to, &fmt);

    switch (propid)
    {
454
    case FONT_ALLCAPS:
455 456 457 458 459 460 461 462 463 464 465 466 467 468
    case FONT_BOLD:
    case FONT_EMBOSS:
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
    case FONT_ITALIC:
    case FONT_OUTLINE:
    case FONT_PROTECTED:
    case FONT_SHADOW:
    case FONT_SMALLCAPS:
    case FONT_STRIKETHROUGH:
    case FONT_SUBSCRIPT:
    case FONT_SUPERSCRIPT:
    case FONT_UNDERLINE:
        value->l = fmt.dwEffects & textfont_prop_masks[propid][1] ? tomTrue : tomFalse;
469 470 471 472 473 474 475
        break;
    case FONT_ANIMATION:
        value->l = fmt.bAnimation;
        break;
    case FONT_BACKCOLOR:
        value->l = fmt.dwEffects & CFE_AUTOBACKCOLOR ? GetSysColor(COLOR_WINDOW) : fmt.crBackColor;
        break;
476 477 478
    case FONT_FORECOLOR:
        value->l = fmt.dwEffects & CFE_AUTOCOLOR ? GetSysColor(COLOR_WINDOWTEXT) : fmt.crTextColor;
        break;
479
    case FONT_KERNING:
480
        value->f = twips_to_points(fmt.wKerning);
481
        break;
482 483 484
    case FONT_LANGID:
        value->l = fmt.lcid;
        break;
485 486 487 488 489 490
    case FONT_NAME:
        /* this case is used exclusively by GetName() */
        value->str = SysAllocString(fmt.szFaceName);
        if (!value->str)
            return E_OUTOFMEMORY;
        break;
491
    case FONT_POSITION:
492
        value->f = twips_to_points(fmt.yOffset);
493
        break;
494
    case FONT_SIZE:
495
        value->f = twips_to_points(fmt.yHeight);
496
        break;
497 498 499 500 501 502
    case FONT_SPACING:
        value->f = fmt.sSpacing;
        break;
    case FONT_WEIGHT:
        value->l = fmt.wWeight;
        break;
503 504 505 506 507 508 509 510
    default:
        FIXME("unhandled font property %d\n", propid);
        return E_FAIL;
    }

    return S_OK;
}

511 512 513 514 515 516 517
static inline const IRichEditOleImpl *get_range_reole(ITextRange *range)
{
    IRichEditOleImpl *reole = NULL;
    ITextRange_QueryInterface(range, &IID_Igetrichole, (void**)&reole);
    return reole;
}

518 519 520
static void textrange_set_font(ITextRange *range, ITextFont *font)
{
    CHARFORMAT2W fmt;
521
    HRESULT hr;
522 523 524 525 526
    LONG value;
    BSTR str;
    FLOAT f;

#define CHARFORMAT_SET_B_FIELD(mask, value) \
527
    if (hr == S_OK && value != tomUndefined) { \
528 529 530 531 532 533 534 535 536
        fmt.dwMask |= CFM_##mask; \
        if (value == tomTrue) fmt.dwEffects |= CFE_##mask; \
    } \

    /* fill format data from font */
    memset(&fmt, 0, sizeof(fmt));
    fmt.cbSize = sizeof(fmt);

    value = tomUndefined;
537
    hr = ITextFont_GetAllCaps(font, &value);
538 539 540
    CHARFORMAT_SET_B_FIELD(ALLCAPS, value);

    value = tomUndefined;
541
    hr = ITextFont_GetBold(font, &value);
542 543 544
    CHARFORMAT_SET_B_FIELD(BOLD, value);

    value = tomUndefined;
545
    hr = ITextFont_GetEmboss(font, &value);
546 547 548
    CHARFORMAT_SET_B_FIELD(EMBOSS, value);

    value = tomUndefined;
549
    hr = ITextFont_GetHidden(font, &value);
550 551 552
    CHARFORMAT_SET_B_FIELD(HIDDEN, value);

    value = tomUndefined;
553
    hr = ITextFont_GetEngrave(font, &value);
554 555 556
    CHARFORMAT_SET_B_FIELD(IMPRINT, value);

    value = tomUndefined;
557
    hr = ITextFont_GetItalic(font, &value);
558 559 560
    CHARFORMAT_SET_B_FIELD(ITALIC, value);

    value = tomUndefined;
561
    hr = ITextFont_GetOutline(font, &value);
562 563 564
    CHARFORMAT_SET_B_FIELD(OUTLINE, value);

    value = tomUndefined;
565
    hr = ITextFont_GetProtected(font, &value);
566 567 568
    CHARFORMAT_SET_B_FIELD(PROTECTED, value);

    value = tomUndefined;
569
    hr = ITextFont_GetShadow(font, &value);
570 571 572
    CHARFORMAT_SET_B_FIELD(SHADOW, value);

    value = tomUndefined;
573
    hr = ITextFont_GetSmallCaps(font, &value);
574 575 576
    CHARFORMAT_SET_B_FIELD(SMALLCAPS, value);

    value = tomUndefined;
577
    hr = ITextFont_GetStrikeThrough(font, &value);
578 579 580
    CHARFORMAT_SET_B_FIELD(STRIKEOUT, value);

    value = tomUndefined;
581
    hr = ITextFont_GetSubscript(font, &value);
582 583 584
    CHARFORMAT_SET_B_FIELD(SUBSCRIPT, value);

    value = tomUndefined;
585
    hr = ITextFont_GetSuperscript(font, &value);
586 587 588
    CHARFORMAT_SET_B_FIELD(SUPERSCRIPT, value);

    value = tomUndefined;
589
    hr = ITextFont_GetUnderline(font, &value);
590 591 592 593 594
    CHARFORMAT_SET_B_FIELD(UNDERLINE, value);

#undef CHARFORMAT_SET_B_FIELD

    value = tomUndefined;
595 596
    hr = ITextFont_GetAnimation(font, &value);
    if (hr == S_OK && value != tomUndefined) {
597 598 599 600 601
        fmt.dwMask |= CFM_ANIMATION;
        fmt.bAnimation = value;
    }

    value = tomUndefined;
602 603
    hr = ITextFont_GetBackColor(font, &value);
    if (hr == S_OK && value != tomUndefined) {
604 605 606 607 608 609 610 611
        fmt.dwMask |= CFM_BACKCOLOR;
        if (value == tomAutoColor)
            fmt.dwEffects |= CFE_AUTOBACKCOLOR;
        else
            fmt.crBackColor = value;
    }

    value = tomUndefined;
612 613
    hr = ITextFont_GetForeColor(font, &value);
    if (hr == S_OK && value != tomUndefined) {
614 615 616 617 618 619 620 621
        fmt.dwMask |= CFM_COLOR;
        if (value == tomAutoColor)
            fmt.dwEffects |= CFE_AUTOCOLOR;
        else
            fmt.crTextColor = value;
    }

    value = tomUndefined;
622 623
    hr = ITextFont_GetKerning(font, &f);
    if (hr == S_OK && f != tomUndefined) {
624 625 626 627 628
        fmt.dwMask |= CFM_KERNING;
        fmt.wKerning = points_to_twips(f);
    }

    value = tomUndefined;
629 630
    hr = ITextFont_GetLanguageID(font, &value);
    if (hr == S_OK && value != tomUndefined) {
631 632 633 634 635 636
        fmt.dwMask |= CFM_LCID;
        fmt.lcid = value;
    }

    if (ITextFont_GetName(font, &str) == S_OK) {
        fmt.dwMask |= CFM_FACE;
637
        lstrcpynW(fmt.szFaceName, str, ARRAY_SIZE(fmt.szFaceName));
638 639 640
        SysFreeString(str);
    }

641 642
    hr = ITextFont_GetPosition(font, &f);
    if (hr == S_OK && f != tomUndefined) {
643 644 645 646
        fmt.dwMask |= CFM_OFFSET;
        fmt.yOffset = points_to_twips(f);
    }

647 648
    hr = ITextFont_GetSize(font, &f);
    if (hr == S_OK && f != tomUndefined) {
649 650 651 652
        fmt.dwMask |= CFM_SIZE;
        fmt.yHeight = points_to_twips(f);
    }

653 654
    hr = ITextFont_GetSpacing(font, &f);
    if (hr == S_OK && f != tomUndefined) {
655 656 657 658
        fmt.dwMask |= CFM_SPACING;
        fmt.sSpacing = f;
    }

659 660
    hr = ITextFont_GetWeight(font, &value);
    if (hr == S_OK && value != tomUndefined) {
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
        fmt.dwMask |= CFM_WEIGHT;
        fmt.wWeight = value;
    }

    if (fmt.dwMask) {
        const IRichEditOleImpl *reole = get_range_reole(range);
        ME_Cursor from, to;
        LONG start, end;

        ITextRange_GetStart(range, &start);
        ITextRange_GetEnd(range, &end);

        ME_CursorFromCharOfs(reole->editor, start, &from);
        ME_CursorFromCharOfs(reole->editor, end, &to);
        ME_SetCharFormat(reole->editor, &from, &to, &fmt);
    }
}

679
static HRESULT get_textfont_prop(const ITextFontImpl *font, enum textfont_prop_id propid, textfont_prop_val *value)
680
{
681
    const IRichEditOleImpl *reole;
682
    textfont_prop_val v;
683
    LONG start, end, i;
684 685
    HRESULT hr;

686
    /* when font is not attached to any range use cached values */
687
    if (!font->range || font->get_cache_enabled) {
688 689 690 691 692
        *value = font->props[propid];
        return S_OK;
    }

    if (!(reole = get_range_reole(font->range)))
693 694
        return CO_E_RELEASED;

695 696
    init_textfont_prop_value(propid, value);

697 698
    ITextRange_GetStart(font->range, &start);
    ITextRange_GetEnd(font->range, &end);
699

700
    /* iterate trough a range to see if property value is consistent */
701
    hr = get_textfont_prop_for_pos(reole, start, propid, &v);
702 703 704
    if (FAILED(hr))
        return hr;

705
    for (i = start + 1; i < end; i++) {
706
        textfont_prop_val cur;
707

708
        hr = get_textfont_prop_for_pos(reole, i, propid, &cur);
709 710 711
        if (FAILED(hr))
            return hr;

712
        if (!is_equal_textfont_prop_value(propid, &v, &cur))
713 714 715 716 717 718 719
            return S_OK;
    }

    *value = v;
    return S_OK;
}

720
static HRESULT get_textfont_propf(const ITextFontImpl *font, enum textfont_prop_id propid, FLOAT *value)
721 722 723 724 725 726 727
{
    textfont_prop_val v;
    HRESULT hr;

    if (!value)
        return E_INVALIDARG;

728
    hr = get_textfont_prop(font, propid, &v);
729 730 731 732
    *value = v.f;
    return hr;
}

733
static HRESULT get_textfont_propl(const ITextFontImpl *font, enum textfont_prop_id propid, LONG *value)
734 735 736 737 738 739 740
{
    textfont_prop_val v;
    HRESULT hr;

    if (!value)
        return E_INVALIDARG;

741
    hr = get_textfont_prop(font, propid, &v);
742 743 744 745
    *value = v.l;
    return hr;
}

746 747 748 749 750 751 752 753 754 755
/* Value should already have a terminal value, for boolean properties it means tomToggle is not handled */
static HRESULT set_textfont_prop(ITextFontImpl *font, enum textfont_prop_id propid, const textfont_prop_val *value)
{
    const IRichEditOleImpl *reole;
    ME_Cursor from, to;
    CHARFORMAT2W fmt;
    LONG start, end;

    /* when font is not attached to any range use cache */
    if (!font->range || font->set_cache_enabled) {
756 757 758 759 760 761
        if (propid == FONT_NAME) {
            SysFreeString(font->props[propid].str);
            font->props[propid].str = SysAllocString(value->str);
        }
        else
            font->props[propid] = *value;
762 763 764 765 766 767 768 769
        return S_OK;
    }

    if (!(reole = get_range_reole(font->range)))
        return CO_E_RELEASED;

    memset(&fmt, 0, sizeof(fmt));
    fmt.cbSize = sizeof(fmt);
770
    fmt.dwMask = textfont_prop_masks[propid][0];
771 772 773

    switch (propid)
    {
774 775 776 777 778
    case FONT_ALLCAPS:
    case FONT_BOLD:
    case FONT_EMBOSS:
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
779
    case FONT_ITALIC:
780 781 782 783 784 785 786 787 788
    case FONT_OUTLINE:
    case FONT_PROTECTED:
    case FONT_SHADOW:
    case FONT_SMALLCAPS:
    case FONT_STRIKETHROUGH:
    case FONT_SUBSCRIPT:
    case FONT_SUPERSCRIPT:
    case FONT_UNDERLINE:
        fmt.dwEffects = value->l == tomTrue ? textfont_prop_masks[propid][1] : 0;
789
        break;
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    case FONT_ANIMATION:
        fmt.bAnimation = value->l;
        break;
    case FONT_BACKCOLOR:
    case FONT_FORECOLOR:
        if (value->l == tomAutoColor)
            fmt.dwEffects = textfont_prop_masks[propid][1];
        else if (propid == FONT_BACKCOLOR)
            fmt.crBackColor = value->l;
        else
            fmt.crTextColor = value->l;
        break;
    case FONT_KERNING:
        fmt.wKerning = value->f;
        break;
    case FONT_LANGID:
        fmt.lcid = value->l;
        break;
    case FONT_POSITION:
        fmt.yOffset = value->f;
        break;
    case FONT_SIZE:
        fmt.yHeight = value->f;
        break;
    case FONT_SPACING:
        fmt.sSpacing = value->f;
        break;
    case FONT_WEIGHT:
        fmt.wWeight = value->l;
        break;
820
    case FONT_NAME:
821
        lstrcpynW(fmt.szFaceName, value->str, ARRAY_SIZE(fmt.szFaceName));
822
        break;
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    default:
        FIXME("unhandled font property %d\n", propid);
        return E_FAIL;
    }

    ITextRange_GetStart(font->range, &start);
    ITextRange_GetEnd(font->range, &end);

    ME_CursorFromCharOfs(reole->editor, start, &from);
    ME_CursorFromCharOfs(reole->editor, end, &to);
    ME_SetCharFormat(reole->editor, &from, &to, &fmt);

    return S_OK;
}

838 839 840 841 842 843 844 845 846 847 848 849 850 851
static inline HRESULT set_textfont_propl(ITextFontImpl *font, enum textfont_prop_id propid, LONG value)
{
    textfont_prop_val v;
    v.l = value;
    return set_textfont_prop(font, propid, &v);
}

static inline HRESULT set_textfont_propf(ITextFontImpl *font, enum textfont_prop_id propid, FLOAT value)
{
    textfont_prop_val v;
    v.f = value;
    return set_textfont_prop(font, propid, &v);
}

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
static HRESULT set_textfont_propd(ITextFontImpl *font, enum textfont_prop_id propid, LONG value)
{
    textfont_prop_val v;

    switch (value)
    {
    case tomUndefined:
        return S_OK;
    case tomToggle: {
        LONG oldvalue;
        get_textfont_propl(font, propid, &oldvalue);
        if (oldvalue == tomFalse)
            value = tomTrue;
        else if (oldvalue == tomTrue)
            value = tomFalse;
        else
            return E_INVALIDARG;
        /* fallthrough */
    }
    case tomTrue:
    case tomFalse:
        v.l = value;
        return set_textfont_prop(font, propid, &v);
    default:
        return E_INVALIDARG;
    }
}

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
static HRESULT textfont_getname_from_range(ITextRange *range, BSTR *ret)
{
    const IRichEditOleImpl *reole;
    textfont_prop_val v;
    HRESULT hr;
    LONG start;

    if (!(reole = get_range_reole(range)))
        return CO_E_RELEASED;

    ITextRange_GetStart(range, &start);
    hr = get_textfont_prop_for_pos(reole, start, FONT_NAME, &v);
    *ret = v.str;
    return hr;
}

static void textfont_cache_range_props(ITextFontImpl *font)
{
    enum textfont_prop_id propid;
    for (propid = FONT_PROPID_FIRST; propid < FONT_PROPID_LAST; propid++) {
        if (propid == FONT_NAME)
            textfont_getname_from_range(font->range, &font->props[propid].str);
        else
            get_textfont_prop(font, propid, &font->props[propid]);
    }
}

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
static HRESULT textrange_expand(ITextRange *range, LONG unit, LONG *delta)
{
    LONG expand_start, expand_end;

    switch (unit)
    {
    case tomStory:
        expand_start = 0;
        ITextRange_GetStoryLength(range, &expand_end);
        break;
    default:
        FIXME("unit %d is not supported\n", unit);
        return E_NOTIMPL;
    }

    if (delta) {
        LONG start, end;

        ITextRange_GetStart(range, &start);
        ITextRange_GetEnd(range, &end);
        *delta = expand_end - expand_start - (end - start);
    }

    ITextRange_SetStart(range, expand_start);
    ITextRange_SetEnd(range, expand_end);

    return S_OK;
}

936 937 938 939 940
static HRESULT WINAPI IRichEditOleImpl_inner_fnQueryInterface(IUnknown *iface, REFIID riid, LPVOID *ppvObj)
{
    IRichEditOleImpl *This = impl_from_IUnknown(iface);

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

942
    *ppvObj = NULL;
943 944 945
    if (IsEqualGUID(riid, &IID_IUnknown))
        *ppvObj = &This->IUnknown_inner;
    else if (IsEqualGUID(riid, &IID_IRichEditOle))
946
        *ppvObj = &This->IRichEditOle_iface;
947 948
    else if (IsEqualGUID(riid, &IID_ITextDocument) || IsEqualGUID(riid, &IID_ITextDocument2Old))
        *ppvObj = &This->ITextDocument2Old_iface;
949
    if (*ppvObj)
950
    {
951
        IUnknown_AddRef((IUnknown *)*ppvObj);
952 953
        return S_OK;
    }
954
    FIXME("%p: unhandled interface %s\n", This, debugstr_guid(riid));
955 956 957 958
 
    return E_NOINTERFACE;   
}

959
static ULONG WINAPI IRichEditOleImpl_inner_fnAddRef(IUnknown *iface)
960
{
961 962
    IRichEditOleImpl *This = impl_from_IUnknown(iface);
    ULONG ref = InterlockedIncrement(&This->ref);
963

964
    TRACE("%p ref = %u\n", This, ref);
965 966 967 968

    return ref;
}

969
static ULONG WINAPI IRichEditOleImpl_inner_fnRelease(IUnknown *iface)
970
{
971
    IRichEditOleImpl *This = impl_from_IUnknown(iface);
972 973
    ULONG ref = InterlockedDecrement(&This->ref);

974
    TRACE ("%p ref=%u\n", This, ref);
975 976

    if (!ref)
977
    {
978
        IOleClientSiteImpl *clientsite;
979 980 981
        ITextRangeImpl *txtRge;

        This->editor->reOle = NULL;
982 983 984 985
        if (This->txtSel) {
            This->txtSel->reOle = NULL;
            ITextSelection_Release(&This->txtSel->ITextSelection_iface);
        }
986

987 988
        LIST_FOR_EACH_ENTRY(txtRge, &This->rangelist, ITextRangeImpl, child.entry)
            txtRge->child.reole = NULL;
989 990 991 992

        LIST_FOR_EACH_ENTRY(clientsite, &This->clientsites, IOleClientSiteImpl, child.entry)
            clientsite->child.reole = NULL;

993 994
        heap_free(This);
    }
995 996 997
    return ref;
}

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
static const IUnknownVtbl reo_unk_vtbl =
{
    IRichEditOleImpl_inner_fnQueryInterface,
    IRichEditOleImpl_inner_fnAddRef,
    IRichEditOleImpl_inner_fnRelease
};

static HRESULT WINAPI
IRichEditOle_fnQueryInterface(IRichEditOle *me, REFIID riid, LPVOID *ppvObj)
{
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
    return IUnknown_QueryInterface(This->outer_unk, riid, ppvObj);
}

static ULONG WINAPI
IRichEditOle_fnAddRef(IRichEditOle *me)
{
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
    return IUnknown_AddRef(This->outer_unk);
}

static ULONG WINAPI
IRichEditOle_fnRelease(IRichEditOle *me)
{
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
    return IUnknown_Release(This->outer_unk);
}

1026 1027 1028
static HRESULT WINAPI
IRichEditOle_fnActivateAs(IRichEditOle *me, REFCLSID rclsid, REFCLSID rclsidAs)
{
1029
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1030 1031 1032 1033 1034 1035 1036
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnContextSensitiveHelp(IRichEditOle *me, BOOL fEnterMode)
{
1037
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1038 1039 1040 1041 1042 1043 1044 1045
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnConvertObject(IRichEditOle *me, LONG iob,
               REFCLSID rclsidNew, LPCSTR lpstrUserTypeNew)
{
1046
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1047 1048 1049 1050
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

1051 1052 1053 1054 1055
static inline IOleClientSiteImpl *impl_from_IOleClientSite(IOleClientSite *iface)
{
    return CONTAINING_RECORD(iface, IOleClientSiteImpl, IOleClientSite_iface);
}

1056 1057 1058
static HRESULT WINAPI
IOleClientSite_fnQueryInterface(IOleClientSite *me, REFIID riid, LPVOID *ppvObj)
{
1059
    IOleClientSiteImpl *This = impl_from_IOleClientSite(me);
1060 1061 1062 1063 1064 1065
    TRACE("%p %s\n", me, debugstr_guid(riid) );

    *ppvObj = NULL;
    if (IsEqualGUID(riid, &IID_IUnknown) ||
        IsEqualGUID(riid, &IID_IOleClientSite))
        *ppvObj = me;
1066 1067
    else if (IsEqualGUID(riid, &IID_IOleWindow) ||
             IsEqualGUID(riid, &IID_IOleInPlaceSite))
1068
        *ppvObj = &This->IOleInPlaceSite_iface;
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
    if (*ppvObj)
    {
        IOleClientSite_AddRef(me);
        return S_OK;
    }
    FIXME("%p: unhandled interface %s\n", me, debugstr_guid(riid) );

    return E_NOINTERFACE;
}

1079
static ULONG WINAPI IOleClientSite_fnAddRef(IOleClientSite *iface)
1080
{
1081
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1082 1083 1084
    ULONG ref = InterlockedIncrement(&This->ref);
    TRACE("(%p)->(%u)\n", This, ref);
    return ref;
1085 1086
}

1087
static ULONG WINAPI IOleClientSite_fnRelease(IOleClientSite *iface)
1088
{
1089
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1090
    ULONG ref = InterlockedDecrement(&This->ref);
1091 1092 1093 1094 1095 1096 1097 1098

    TRACE("(%p)->(%u)\n", This, ref);

    if (ref == 0) {
        if (This->child.reole) {
            list_remove(&This->child.entry);
            This->child.reole = NULL;
        }
1099
        heap_free(This);
1100
    }
1101 1102 1103
    return ref;
}

1104
static HRESULT WINAPI IOleClientSite_fnSaveObject(IOleClientSite *iface)
1105
{
1106
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1107
    if (!This->child.reole)
1108 1109
        return CO_E_RELEASED;

1110
    FIXME("stub %p\n", iface);
1111 1112 1113
    return E_NOTIMPL;
}

1114 1115
static HRESULT WINAPI IOleClientSite_fnGetMoniker(IOleClientSite *iface, DWORD dwAssign,
        DWORD dwWhichMoniker, IMoniker **ppmk)
1116
{
1117
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1118
    if (!This->child.reole)
1119 1120
        return CO_E_RELEASED;

1121
    FIXME("stub %p\n", iface);
1122 1123 1124
    return E_NOTIMPL;
}

1125 1126
static HRESULT WINAPI IOleClientSite_fnGetContainer(IOleClientSite *iface,
        IOleContainer **ppContainer)
1127
{
1128
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1129
    if (!This->child.reole)
1130 1131
        return CO_E_RELEASED;

1132
    FIXME("stub %p\n", iface);
1133 1134 1135
    return E_NOTIMPL;
}

1136
static HRESULT WINAPI IOleClientSite_fnShowObject(IOleClientSite *iface)
1137
{
1138
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1139
    if (!This->child.reole)
1140 1141
        return CO_E_RELEASED;

1142
    FIXME("stub %p\n", iface);
1143 1144 1145
    return E_NOTIMPL;
}

1146
static HRESULT WINAPI IOleClientSite_fnOnShowWindow(IOleClientSite *iface, BOOL fShow)
1147
{
1148
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1149
    if (!This->child.reole)
1150 1151
        return CO_E_RELEASED;

1152
    FIXME("stub %p\n", iface);
1153 1154 1155
    return E_NOTIMPL;
}

1156
static HRESULT WINAPI IOleClientSite_fnRequestNewObjectLayout(IOleClientSite *iface)
1157
{
1158
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1159
    if (!This->child.reole)
1160 1161
        return CO_E_RELEASED;

1162
    FIXME("stub %p\n", iface);
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
    return E_NOTIMPL;
}

static const IOleClientSiteVtbl ocst = {
    IOleClientSite_fnQueryInterface,
    IOleClientSite_fnAddRef,
    IOleClientSite_fnRelease,
    IOleClientSite_fnSaveObject,
    IOleClientSite_fnGetMoniker,
    IOleClientSite_fnGetContainer,
    IOleClientSite_fnShowObject,
    IOleClientSite_fnOnShowWindow,
    IOleClientSite_fnRequestNewObjectLayout
};

1178 1179
/* IOleInPlaceSite interface */
static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnQueryInterface(IOleInPlaceSite *iface, REFIID riid, void **ppvObj)
1180
{
1181
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1182 1183 1184
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppvObj);
}

1185
static ULONG STDMETHODCALLTYPE IOleInPlaceSite_fnAddRef(IOleInPlaceSite *iface)
1186
{
1187
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1188 1189 1190
    return IOleClientSite_AddRef(&This->IOleClientSite_iface);
}

1191
static ULONG STDMETHODCALLTYPE IOleInPlaceSite_fnRelease(IOleInPlaceSite *iface)
1192
{
1193
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1194 1195 1196
    return IOleClientSite_Release(&This->IOleClientSite_iface);
}

1197
static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnGetWindow(IOleInPlaceSite *iface, HWND *phwnd)
1198
{
1199
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1200

1201 1202
    TRACE("(%p)->(%p)\n", This, phwnd);

1203 1204 1205
    if (!This->child.reole)
        return CO_E_RELEASED;

1206 1207 1208
    if (!phwnd)
        return E_INVALIDARG;

1209
    *phwnd = This->child.reole->editor->hWnd;
1210
    return S_OK;
1211 1212
}

1213 1214 1215
static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnContextSensitiveHelp(IOleInPlaceSite *iface, BOOL fEnterMode)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1216 1217
    FIXME("not implemented: (%p)->(%d)\n", This, fEnterMode);
    return E_NOTIMPL;
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnCanInPlaceActivate(IOleInPlaceSite *iface)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnOnInPlaceActivate(IOleInPlaceSite *iface)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnOnUIActivate(IOleInPlaceSite *iface)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnGetWindowContext(IOleInPlaceSite *iface, IOleInPlaceFrame **ppFrame,
                                                                    IOleInPlaceUIWindow **ppDoc, LPRECT lprcPosRect,
                                                                    LPRECT lprcClipRect, LPOLEINPLACEFRAMEINFO lpFrameInfo)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1246
    FIXME("not implemented: (%p)->(%p %p %p %p %p)\n", This, ppFrame, ppDoc, lprcPosRect, lprcClipRect, lpFrameInfo);
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnScroll(IOleInPlaceSite *iface, SIZE scrollExtent)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnOnUIDeactivate(IOleInPlaceSite *iface, BOOL fUndoable)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)->(%d)\n", This, fUndoable);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnOnInPlaceDeactivate(IOleInPlaceSite *iface)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnDiscardUndoState(IOleInPlaceSite *iface)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnDeactivateAndUndo(IOleInPlaceSite *iface)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnOnPosRectChange(IOleInPlaceSite *iface, LPCRECT lprcPosRect)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
    FIXME("not implemented: (%p)->(%p)\n", This, lprcPosRect);
    return E_NOTIMPL;
}

static const IOleInPlaceSiteVtbl olestvt =
{
    IOleInPlaceSite_fnQueryInterface,
    IOleInPlaceSite_fnAddRef,
    IOleInPlaceSite_fnRelease,
    IOleInPlaceSite_fnGetWindow,
    IOleInPlaceSite_fnContextSensitiveHelp,
    IOleInPlaceSite_fnCanInPlaceActivate,
    IOleInPlaceSite_fnOnInPlaceActivate,
    IOleInPlaceSite_fnOnUIActivate,
    IOleInPlaceSite_fnGetWindowContext,
    IOleInPlaceSite_fnScroll,
    IOleInPlaceSite_fnOnUIDeactivate,
    IOleInPlaceSite_fnOnInPlaceDeactivate,
    IOleInPlaceSite_fnDiscardUndoState,
    IOleInPlaceSite_fnDeactivateAndUndo,
    IOleInPlaceSite_fnOnPosRectChange
};

1311
static HRESULT CreateOleClientSite(IRichEditOleImpl *reOle, IOleClientSite **ret)
1312 1313
{
    IOleClientSiteImpl *clientSite = heap_alloc(sizeof *clientSite);
1314

1315
    if (!clientSite)
1316
        return E_OUTOFMEMORY;
1317

1318
    clientSite->IOleClientSite_iface.lpVtbl = &ocst;
1319
    clientSite->IOleInPlaceSite_iface.lpVtbl = &olestvt;
1320
    clientSite->ref = 1;
1321 1322 1323 1324 1325
    clientSite->child.reole = reOle;
    list_add_head(&reOle->clientsites, &clientSite->child.entry);

    *ret = &clientSite->IOleClientSite_iface;
    return S_OK;
1326 1327
}

1328
static HRESULT WINAPI
1329
IRichEditOle_fnGetClientSite(IRichEditOle *me, IOleClientSite **clientsite)
1330
{
1331
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1332

1333
    TRACE("(%p)->(%p)\n", This, clientsite);
1334

1335
    if (!clientsite)
1336
        return E_INVALIDARG;
1337 1338

    return CreateOleClientSite(This, clientsite);
1339 1340 1341 1342 1343 1344
}

static HRESULT WINAPI
IRichEditOle_fnGetClipboardData(IRichEditOle *me, CHARRANGE *lpchrg,
               DWORD reco, LPDATAOBJECT *lplpdataobj)
{
1345
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1346 1347
    ME_Cursor start;
    int nChars;
1348

1349
    TRACE("(%p,%p,%d)\n",This, lpchrg, reco);
1350 1351 1352
    if(!lplpdataobj)
        return E_INVALIDARG;
    if(!lpchrg) {
1353 1354 1355 1356 1357 1358
        int nFrom, nTo, nStartCur = ME_GetSelectionOfs(This->editor, &nFrom, &nTo);
        start = This->editor->pCursors[nStartCur];
        nChars = nTo - nFrom;
    } else {
        ME_CursorFromCharOfs(This->editor, lpchrg->cpMin, &start);
        nChars = lpchrg->cpMax - lpchrg->cpMin;
1359
    }
1360
    return ME_GetDataObject(This->editor, &start, nChars, lplpdataobj);
1361 1362 1363 1364
}

static LONG WINAPI IRichEditOle_fnGetLinkCount(IRichEditOle *me)
{
1365
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1366 1367 1368 1369 1370 1371 1372 1373
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnGetObject(IRichEditOle *me, LONG iob,
               REOBJECT *lpreobject, DWORD dwFlags)
{
1374
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
    struct re_object *reobj = NULL;
    LONG count = 0;

    TRACE("(%p)->(%x, %p, %x)\n", This, iob, lpreobject, dwFlags);

    if (!lpreobject || !lpreobject->cbStruct)
        return E_INVALIDARG;

    if (iob == REO_IOB_USE_CP)
    {
        ME_Cursor cursor;

        TRACE("character offset: %d\n", lpreobject->cp);
        ME_CursorFromCharOfs(This->editor, lpreobject->cp, &cursor);
        if (!cursor.pRun->member.run.reobj)
            return E_INVALIDARG;
        else
            reobj = cursor.pRun->member.run.reobj;
    }
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
    else if (iob == REO_IOB_SELECTION)
    {
        ME_Cursor *from, *to;

        ME_GetSelection(This->editor, &from, &to);
        if (!from->pRun->member.run.reobj)
            return E_INVALIDARG;
        else
            reobj = from->pRun->member.run.reobj;
    }
1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
    else
    {
        if (iob > IRichEditOle_GetObjectCount(me))
            return E_INVALIDARG;
        LIST_FOR_EACH_ENTRY(reobj, &This->editor->reobj_list, struct re_object, entry)
        {
            if (count == iob)
                break;
            count++;
        }
    }
    ME_CopyReObject(lpreobject, &reobj->obj, dwFlags);
    return S_OK;
1417 1418 1419 1420 1421
}

static LONG WINAPI
IRichEditOle_fnGetObjectCount(IRichEditOle *me)
{
1422
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1423 1424
    TRACE("(%p)\n",This);
    return list_count(&This->editor->reobj_list);
1425 1426 1427 1428 1429
}

static HRESULT WINAPI
IRichEditOle_fnHandsOffStorage(IRichEditOle *me, LONG iob)
{
1430
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1431 1432 1433 1434 1435 1436 1437 1438
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnImportDataObject(IRichEditOle *me, LPDATAOBJECT lpdataobj,
               CLIPFORMAT cf, HGLOBAL hMetaPict)
{
1439
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1440 1441 1442 1443 1444 1445 1446
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnInPlaceDeactivate(IRichEditOle *me)
{
1447
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1448 1449 1450 1451 1452
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
1453
IRichEditOle_fnInsertObject(IRichEditOle *me, REOBJECT *reo)
1454
{
1455
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1456

1457 1458
    TRACE("(%p,%p)\n", This, reo);

1459 1460 1461
    if (!reo)
        return E_INVALIDARG;

1462 1463 1464
    if (reo->cbStruct < sizeof(*reo)) return STG_E_INVALIDPARAMETER;

    ME_InsertOLEFromCursor(This->editor, reo, 0);
1465
    ME_CommitUndo(This->editor);
1466
    ME_UpdateRepaint(This->editor, FALSE);
1467
    return S_OK;
1468 1469 1470 1471 1472
}

static HRESULT WINAPI IRichEditOle_fnSaveCompleted(IRichEditOle *me, LONG iob,
               LPSTORAGE lpstg)
{
1473
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1474 1475 1476 1477 1478 1479 1480
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnSetDvaspect(IRichEditOle *me, LONG iob, DWORD dvaspect)
{
1481
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1482 1483 1484 1485 1486 1487 1488
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

static HRESULT WINAPI IRichEditOle_fnSetHostNames(IRichEditOle *me,
               LPCSTR lpstrContainerApp, LPCSTR lpstrContainerObj)
{
1489
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1490
    FIXME("stub %p %s %s\n",This, lpstrContainerApp, lpstrContainerObj);
1491 1492 1493 1494 1495 1496
    return E_NOTIMPL;
}

static HRESULT WINAPI
IRichEditOle_fnSetLinkAvailable(IRichEditOle *me, LONG iob, BOOL fAvailable)
{
1497
    IRichEditOleImpl *This = impl_from_IRichEditOle(me);
1498 1499 1500 1501
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

1502
static const IRichEditOleVtbl revt = {
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
    IRichEditOle_fnQueryInterface,
    IRichEditOle_fnAddRef,
    IRichEditOle_fnRelease,
    IRichEditOle_fnGetClientSite,
    IRichEditOle_fnGetObjectCount,
    IRichEditOle_fnGetLinkCount,
    IRichEditOle_fnGetObject,
    IRichEditOle_fnInsertObject,
    IRichEditOle_fnConvertObject,
    IRichEditOle_fnActivateAs,
    IRichEditOle_fnSetHostNames,
    IRichEditOle_fnSetLinkAvailable,
    IRichEditOle_fnSetDvaspect,
    IRichEditOle_fnHandsOffStorage,
    IRichEditOle_fnSaveCompleted,
    IRichEditOle_fnInPlaceDeactivate,
    IRichEditOle_fnContextSensitiveHelp,
    IRichEditOle_fnGetClipboardData,
    IRichEditOle_fnImportDataObject
};

1524 1525 1526
/* ITextRange interface */
static HRESULT WINAPI ITextRange_fnQueryInterface(ITextRange *me, REFIID riid, void **ppvObj)
{
1527 1528
    ITextRangeImpl *This = impl_from_ITextRange(me);

1529 1530 1531 1532 1533 1534 1535 1536 1537
    *ppvObj = NULL;
    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IDispatch)
        || IsEqualGUID(riid, &IID_ITextRange))
    {
        *ppvObj = me;
        ITextRange_AddRef(me);
        return S_OK;
    }
1538 1539
    else if (IsEqualGUID(riid, &IID_Igetrichole))
    {
1540
        *ppvObj = This->child.reole;
1541 1542
        return S_OK;
    }
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560

    return E_NOINTERFACE;
}

static ULONG WINAPI ITextRange_fnAddRef(ITextRange *me)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
    return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI ITextRange_fnRelease(ITextRange *me)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE ("%p ref=%u\n", This, ref);
    if (ref == 0)
    {
1561
        if (This->child.reole)
1562
        {
1563 1564
            list_remove(&This->child.entry);
            This->child.reole = NULL;
1565
        }
1566 1567 1568 1569 1570 1571 1572 1573
        heap_free(This);
    }
    return ref;
}

static HRESULT WINAPI ITextRange_fnGetTypeInfoCount(ITextRange *me, UINT *pctinfo)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1574 1575 1576
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
1577 1578 1579 1580 1581 1582
}

static HRESULT WINAPI ITextRange_fnGetTypeInfo(ITextRange *me, UINT iTInfo, LCID lcid,
                                               ITypeInfo **ppTInfo)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1583
    HRESULT hr;
1584

1585 1586 1587 1588 1589 1590
    TRACE("(%p)->(%u,%d,%p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(ITextRange_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
1591 1592 1593 1594 1595 1596
}

static HRESULT WINAPI ITextRange_fnGetIDsOfNames(ITextRange *me, REFIID riid, LPOLESTR *rgszNames,
                                                 UINT cNames, LCID lcid, DISPID *rgDispId)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1597 1598
    ITypeInfo *ti;
    HRESULT hr;
1599

1600
    TRACE("(%p)->(%s, %p, %u, %d, %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid,
1601 1602 1603 1604 1605 1606
            rgDispId);

    hr = get_typeinfo(ITextRange_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
1607 1608 1609 1610 1611 1612 1613 1614
}

static HRESULT WINAPI ITextRange_fnInvoke(ITextRange *me, DISPID dispIdMember, REFIID riid,
                                          LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
                                          VARIANT *pVarResult, EXCEPINFO *pExcepInfo,
                                          UINT *puArgErr)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1615 1616
    ITypeInfo *ti;
    HRESULT hr;
1617

1618 1619
    TRACE("(%p)->(%d, %s, %d, %u, %p, %p, %p, %p)\n", This, dispIdMember, debugstr_guid(riid),
            lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1620 1621 1622 1623 1624

    hr = get_typeinfo(ITextRange_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_Invoke(ti, me, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    return hr;
1625 1626
}

1627
static HRESULT WINAPI ITextRange_fnGetText(ITextRange *me, BSTR *str)
1628 1629
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1630
    ME_TextEditor *editor;
1631 1632 1633
    ME_Cursor start, end;
    int length;
    BOOL bEOP;
1634

1635
    TRACE("(%p)->(%p)\n", This, str);
1636

1637
    if (!This->child.reole)
1638 1639
        return CO_E_RELEASED;

1640
    if (!str)
1641 1642
        return E_INVALIDARG;

1643 1644 1645 1646 1647 1648
    /* return early for degenerate range */
    if (This->start == This->end) {
        *str = NULL;
        return S_OK;
    }

1649 1650 1651
    editor = This->child.reole->editor;
    ME_CursorFromCharOfs(editor, This->start, &start);
    ME_CursorFromCharOfs(editor, This->end, &end);
1652 1653 1654 1655 1656 1657

    length = This->end - This->start;
    *str = SysAllocStringLen(NULL, length);
    if (!*str)
        return E_OUTOFMEMORY;

1658 1659
    bEOP = (end.pRun->next->type == diTextEnd && This->end > ME_GetTextLength(editor));
    ME_GetTextW(editor, *str, length, &start, length, FALSE, bEOP);
1660
    return S_OK;
1661 1662
}

1663
static HRESULT WINAPI ITextRange_fnSetText(ITextRange *me, BSTR str)
1664 1665
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1666 1667 1668 1669 1670 1671 1672
    ME_TextEditor *editor;
    ME_Cursor cursor;
    ME_Style *style;
    int len;

    TRACE("(%p)->(%s)\n", This, debugstr_w(str));

1673
    if (!This->child.reole)
1674 1675
        return CO_E_RELEASED;

1676
    editor = This->child.reole->editor;
1677 1678 1679 1680 1681 1682 1683 1684 1685

    /* delete only where's something to delete */
    if (This->start != This->end) {
        ME_CursorFromCharOfs(editor, This->start, &cursor);
        ME_InternalDeleteText(editor, &cursor, This->end - This->start, FALSE);
    }

    if (!str || !*str) {
        /* will update this range as well */
1686
        textranges_update_ranges(This->child.reole, This->start, This->end, RANGE_UPDATE_DELETE);
1687 1688 1689 1690
        return S_OK;
    }

    /* it's safer not to rely on stored BSTR length */
1691
    len = lstrlenW(str);
1692 1693 1694 1695 1696 1697 1698 1699
    cursor = editor->pCursors[0];
    ME_CursorFromCharOfs(editor, This->start, &editor->pCursors[0]);
    style = ME_GetInsertStyle(editor, 0);
    ME_InsertTextFromCursor(editor, 0, str, len, style);
    ME_ReleaseStyle(style);
    editor->pCursors[0] = cursor;

    if (len < This->end - This->start)
1700
        textranges_update_ranges(This->child.reole, This->start + len, This->end, RANGE_UPDATE_DELETE);
1701 1702 1703 1704
    else
        This->end = len - This->start;

    return S_OK;
1705 1706
}

1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
static HRESULT range_GetChar(ME_TextEditor *editor, ME_Cursor *cursor, LONG *pch)
{
    WCHAR wch[2];

    ME_GetTextW(editor, wch, 1, cursor, 1, FALSE, cursor->pRun->next->type == diTextEnd);
    *pch = wch[0];

    return S_OK;
}

1717 1718 1719
static HRESULT WINAPI ITextRange_fnGetChar(ITextRange *me, LONG *pch)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1720
    ME_TextEditor *editor;
1721 1722
    ME_Cursor cursor;

1723 1724
    TRACE("(%p)->(%p)\n", This, pch);

1725
    if (!This->child.reole)
1726
        return CO_E_RELEASED;
1727

1728 1729
    if (!pch)
        return E_INVALIDARG;
1730

1731 1732 1733
    editor = This->child.reole->editor;
    ME_CursorFromCharOfs(editor, This->start, &cursor);
    return range_GetChar(editor, &cursor, pch);
1734 1735 1736 1737 1738
}

static HRESULT WINAPI ITextRange_fnSetChar(ITextRange *me, LONG ch)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1739 1740 1741

    FIXME("(%p)->(%x): stub\n", This, ch);

1742
    if (!This->child.reole)
1743 1744 1745 1746 1747
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1748 1749
static HRESULT CreateITextRange(IRichEditOleImpl *reOle, LONG start, LONG end, ITextRange** ppRange);

1750 1751 1752
static HRESULT WINAPI ITextRange_fnGetDuplicate(ITextRange *me, ITextRange **ppRange)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1753 1754 1755 1756

    TRACE("(%p)->(%p)\n", This, ppRange);

    if (!This->child.reole)
1757 1758
        return CO_E_RELEASED;

1759 1760 1761
    if (!ppRange)
        return E_INVALIDARG;

1762
    return CreateITextRange(This->child.reole, This->start, This->end, ppRange);
1763 1764
}

1765
static HRESULT WINAPI ITextRange_fnGetFormattedText(ITextRange *me, ITextRange **range)
1766 1767
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1768 1769 1770

    FIXME("(%p)->(%p): stub\n", This, range);

1771
    if (!This->child.reole)
1772 1773 1774 1775 1776
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1777
static HRESULT WINAPI ITextRange_fnSetFormattedText(ITextRange *me, ITextRange *range)
1778 1779
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1780 1781 1782

    FIXME("(%p)->(%p): stub\n", This, range);

1783
    if (!This->child.reole)
1784 1785 1786 1787 1788
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1789
static HRESULT WINAPI ITextRange_fnGetStart(ITextRange *me, LONG *start)
1790 1791
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1792 1793 1794

    TRACE("(%p)->(%p)\n", This, start);

1795
    if (!This->child.reole)
1796 1797
        return CO_E_RELEASED;

1798
    if (!start)
1799
        return E_INVALIDARG;
1800 1801

    *start = This->start;
1802
    return S_OK;
1803 1804
}

1805
static HRESULT textrange_setstart(const IRichEditOleImpl *reole, LONG value, LONG *start, LONG *end)
1806
{
1807 1808
    int len;

1809 1810
    if (value < 0)
        value = 0;
1811

1812
    if (value == *start)
1813 1814
        return S_FALSE;

1815 1816
    if (value <= *end) {
        *start = value;
1817 1818 1819
        return S_OK;
    }

1820 1821
    len = ME_GetTextLength(reole->editor);
    *start = *end = value > len ? len : value;
1822
    return S_OK;
1823 1824
}

1825
static HRESULT WINAPI ITextRange_fnSetStart(ITextRange *me, LONG value)
1826 1827
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1828 1829 1830

    TRACE("(%p)->(%d)\n", This, value);

1831
    if (!This->child.reole)
1832 1833
        return CO_E_RELEASED;

1834
    return textrange_setstart(This->child.reole, value, &This->start, &This->end);
1835 1836
}

1837
static HRESULT WINAPI ITextRange_fnGetEnd(ITextRange *me, LONG *end)
1838 1839
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1840

1841
    TRACE("(%p)->(%p)\n", This, end);
1842

1843
    if (!This->child.reole)
1844 1845
        return CO_E_RELEASED;

1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
    if (!end)
        return E_INVALIDARG;

    *end = This->end;
    return S_OK;
}

static HRESULT textrange_setend(const IRichEditOleImpl *reole, LONG value, LONG *start, LONG *end)
{
    int len;

    if (value == *end)
1858 1859
        return S_FALSE;

1860 1861
    if (value < *start) {
        *start = *end = max(0, value);
1862 1863 1864
        return S_OK;
    }

1865 1866
    len = ME_GetTextLength(reole->editor);
    *end = value > len ? len + 1 : value;
1867
    return S_OK;
1868 1869
}

1870 1871 1872 1873 1874 1875
static HRESULT WINAPI ITextRange_fnSetEnd(ITextRange *me, LONG value)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);

    TRACE("(%p)->(%d)\n", This, value);

1876
    if (!This->child.reole)
1877 1878
        return CO_E_RELEASED;

1879
    return textrange_setend(This->child.reole, value, &This->start, &This->end);
1880 1881
}

1882
static HRESULT WINAPI ITextRange_fnGetFont(ITextRange *me, ITextFont **font)
1883 1884
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1885 1886 1887

    TRACE("(%p)->(%p)\n", This, font);

1888
    if (!This->child.reole)
1889 1890
        return CO_E_RELEASED;

1891 1892 1893
    if (!font)
        return E_INVALIDARG;

1894
    return create_textfont(me, NULL, font);
1895 1896
}

1897
static HRESULT WINAPI ITextRange_fnSetFont(ITextRange *me, ITextFont *font)
1898 1899
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1900 1901 1902 1903 1904 1905

    TRACE("(%p)->(%p)\n", This, font);

    if (!font)
        return E_INVALIDARG;

1906
    if (!This->child.reole)
1907 1908
        return CO_E_RELEASED;

1909 1910
    textrange_set_font(me, font);
    return S_OK;
1911 1912
}

1913
static HRESULT WINAPI ITextRange_fnGetPara(ITextRange *me, ITextPara **para)
1914 1915
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1916 1917 1918

    TRACE("(%p)->(%p)\n", This, para);

1919
    if (!This->child.reole)
1920 1921
        return CO_E_RELEASED;

1922 1923 1924 1925
    if (!para)
        return E_INVALIDARG;

    return create_textpara(me, para);
1926 1927
}

1928
static HRESULT WINAPI ITextRange_fnSetPara(ITextRange *me, ITextPara *para)
1929 1930
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1931

1932 1933
    FIXME("(%p)->(%p): stub\n", This, para);

1934
    if (!This->child.reole)
1935 1936 1937 1938 1939
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1940
static HRESULT WINAPI ITextRange_fnGetStoryLength(ITextRange *me, LONG *length)
1941 1942
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1943 1944 1945

    TRACE("(%p)->(%p)\n", This, length);

1946
    if (!This->child.reole)
1947 1948
        return CO_E_RELEASED;

1949
    return textrange_get_storylength(This->child.reole->editor, length);
1950 1951
}

1952
static HRESULT WINAPI ITextRange_fnGetStoryType(ITextRange *me, LONG *value)
1953 1954
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1955 1956 1957

    TRACE("(%p)->(%p)\n", This, value);

1958
    if (!This->child.reole)
1959 1960
        return CO_E_RELEASED;

1961 1962 1963 1964 1965
    if (!value)
        return E_INVALIDARG;

    *value = tomUnknownStory;
    return S_OK;
1966 1967
}

1968 1969 1970 1971 1972
static HRESULT range_Collapse(LONG bStart, LONG *start, LONG *end)
{
  if (*end == *start)
      return S_FALSE;

1973
  if (bStart == tomEnd)
1974 1975 1976 1977 1978 1979
      *start = *end;
  else
      *end = *start;
  return S_OK;
}

1980 1981 1982
static HRESULT WINAPI ITextRange_fnCollapse(ITextRange *me, LONG bStart)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1983 1984 1985 1986

    TRACE("(%p)->(%d)\n", This, bStart);

    if (!This->child.reole)
1987 1988
        return CO_E_RELEASED;

1989
    return range_Collapse(bStart, &This->start, &This->end);
1990 1991
}

1992
static HRESULT WINAPI ITextRange_fnExpand(ITextRange *me, LONG unit, LONG *delta)
1993 1994
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1995

1996
    TRACE("(%p)->(%d %p)\n", This, unit, delta);
1997

1998
    if (!This->child.reole)
1999 2000
        return CO_E_RELEASED;

2001
    return textrange_expand(me, unit, delta);
2002 2003
}

2004
static HRESULT WINAPI ITextRange_fnGetIndex(ITextRange *me, LONG unit, LONG *index)
2005 2006
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2007 2008 2009

    FIXME("(%p)->(%d %p): stub\n", This, unit, index);

2010
    if (!This->child.reole)
2011 2012 2013 2014 2015
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2016 2017
static HRESULT WINAPI ITextRange_fnSetIndex(ITextRange *me, LONG unit, LONG index,
                                            LONG extend)
2018 2019
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2020 2021 2022

    FIXME("(%p)->(%d %d %d): stub\n", This, unit, index, extend);

2023
    if (!This->child.reole)
2024 2025 2026 2027 2028
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
static void cp2range(ME_TextEditor *editor, LONG *cp1, LONG *cp2)
{
    int len = ME_GetTextLength(editor) + 1;

    *cp1 = max(*cp1, 0);
    *cp2 = max(*cp2, 0);
    *cp1 = min(*cp1, len);
    *cp2 = min(*cp2, len);
    if (*cp1 > *cp2)
    {
        int tmp = *cp1;
        *cp1 = *cp2;
        *cp2 = tmp;
    }
    if (*cp1 == len)
        *cp1 = *cp2 = len - 1;
}

2047
static HRESULT WINAPI ITextRange_fnSetRange(ITextRange *me, LONG anchor, LONG active)
2048 2049
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2050 2051 2052

    FIXME("(%p)->(%d %d): stub\n", This, anchor, active);

2053
    if (!This->child.reole)
2054 2055
        return CO_E_RELEASED;

2056 2057 2058 2059 2060 2061 2062
    cp2range(This->child.reole->editor, &anchor, &active);
    if (anchor == This->start && active == This->end)
        return S_FALSE;

    This->start = anchor;
    This->end = active;
    return S_OK;
2063 2064
}

2065 2066 2067 2068 2069 2070 2071
static HRESULT textrange_inrange(LONG start, LONG end, ITextRange *range, LONG *ret)
{
    LONG from, to, v;

    if (!ret)
        ret = &v;

2072 2073 2074 2075 2076
    if (FAILED(ITextRange_GetStart(range, &from)) || FAILED(ITextRange_GetEnd(range, &to))) {
        *ret = tomFalse;
    }
    else
        *ret = (start >= from && end <= to) ? tomTrue : tomFalse;
2077 2078 2079 2080
    return *ret == tomTrue ? S_OK : S_FALSE;
}

static HRESULT WINAPI ITextRange_fnInRange(ITextRange *me, ITextRange *range, LONG *ret)
2081 2082
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2083 2084 2085 2086 2087 2088

    TRACE("(%p)->(%p %p)\n", This, range, ret);

    if (ret)
        *ret = tomFalse;

2089
    if (!This->child.reole)
2090 2091
        return CO_E_RELEASED;

2092 2093 2094 2095
    if (!range)
        return S_FALSE;

    return textrange_inrange(This->start, This->end, range, ret);
2096 2097
}

2098
static HRESULT WINAPI ITextRange_fnInStory(ITextRange *me, ITextRange *pRange, LONG *ret)
2099 2100
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2101 2102 2103

    FIXME("(%p)->(%p): stub\n", This, ret);

2104
    if (!This->child.reole)
2105 2106 2107 2108 2109
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2110 2111 2112 2113 2114 2115 2116
static HRESULT textrange_isequal(LONG start, LONG end, ITextRange *range, LONG *ret)
{
    LONG from, to, v;

    if (!ret)
        ret = &v;

2117 2118 2119 2120 2121
    if (FAILED(ITextRange_GetStart(range, &from)) || FAILED(ITextRange_GetEnd(range, &to))) {
        *ret = tomFalse;
    }
    else
        *ret = (start == from && end == to) ? tomTrue : tomFalse;
2122 2123 2124 2125
    return *ret == tomTrue ? S_OK : S_FALSE;
}

static HRESULT WINAPI ITextRange_fnIsEqual(ITextRange *me, ITextRange *range, LONG *ret)
2126 2127
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2128 2129 2130 2131 2132 2133

    TRACE("(%p)->(%p %p)\n", This, range, ret);

    if (ret)
        *ret = tomFalse;

2134
    if (!This->child.reole)
2135 2136
        return CO_E_RELEASED;

2137 2138 2139 2140
    if (!range)
        return S_FALSE;

    return textrange_isequal(This->start, This->end, range, ret);
2141 2142 2143 2144 2145
}

static HRESULT WINAPI ITextRange_fnSelect(ITextRange *me)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2146 2147 2148

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

2149
    if (!This->child.reole)
2150 2151
        return CO_E_RELEASED;

2152
    set_selection(This->child.reole->editor, This->start, This->end);
2153
    return S_OK;
2154 2155
}

2156 2157
static HRESULT WINAPI ITextRange_fnStartOf(ITextRange *me, LONG unit, LONG extend,
                                           LONG *delta)
2158 2159
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2160 2161 2162

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, extend, delta);

2163
    if (!This->child.reole)
2164 2165 2166 2167 2168
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2169 2170
static HRESULT WINAPI ITextRange_fnEndOf(ITextRange *me, LONG unit, LONG extend,
                                         LONG *delta)
2171 2172
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2173 2174 2175

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, extend, delta);

2176
    if (!This->child.reole)
2177 2178 2179 2180 2181
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2182
static HRESULT WINAPI ITextRange_fnMove(ITextRange *me, LONG unit, LONG count, LONG *delta)
2183 2184
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2185 2186 2187

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, count, delta);

2188
    if (!This->child.reole)
2189 2190 2191 2192 2193
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2194 2195
static HRESULT WINAPI ITextRange_fnMoveStart(ITextRange *me, LONG unit, LONG count,
                                             LONG *delta)
2196 2197
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2198 2199 2200

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, count, delta);

2201
    if (!This->child.reole)
2202 2203 2204 2205 2206
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
static HRESULT textrange_moveend(ITextRange *range, LONG unit, LONG count, LONG *delta)
{
    LONG old_start, old_end, new_start, new_end;
    HRESULT hr = S_OK;

    if (!count)
    {
        if (delta)
            *delta = 0;
        return S_FALSE;
    }

    ITextRange_GetStart(range, &old_start);
    ITextRange_GetEnd(range, &old_end);
    switch (unit)
    {
    case tomStory:
        if (count < 0)
            new_start = new_end = 0;
        else
        {
            new_start = old_start;
            ITextRange_GetStoryLength(range, &new_end);
        }
        if (delta)
        {
            if (new_end < old_end)
                *delta = -1;
            else if (new_end == old_end)
                *delta = 0;
            else
                *delta = 1;
        }
        break;
    default:
        FIXME("unit %d is not supported\n", unit);
        return E_NOTIMPL;
    }
    if (new_end == old_end)
        hr = S_FALSE;
    ITextRange_SetStart(range, new_start);
    ITextRange_SetEnd(range, new_end);

    return hr;
}

2253 2254
static HRESULT WINAPI ITextRange_fnMoveEnd(ITextRange *me, LONG unit, LONG count,
                                           LONG *delta)
2255 2256
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2257

2258
    TRACE("(%p)->(%d %d %p)\n", This, unit, count, delta);
2259

2260
    if (!This->child.reole)
2261 2262
        return CO_E_RELEASED;

2263
    return textrange_moveend(me, unit, count, delta);
2264 2265
}

2266 2267
static HRESULT WINAPI ITextRange_fnMoveWhile(ITextRange *me, VARIANT *charset, LONG count,
                                             LONG *delta)
2268 2269
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2270 2271 2272

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

2273
    if (!This->child.reole)
2274 2275 2276 2277 2278
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2279 2280
static HRESULT WINAPI ITextRange_fnMoveStartWhile(ITextRange *me, VARIANT *charset, LONG count,
                                                  LONG *delta)
2281 2282
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2283 2284 2285

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

2286
    if (!This->child.reole)
2287 2288 2289 2290 2291
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2292 2293
static HRESULT WINAPI ITextRange_fnMoveEndWhile(ITextRange *me, VARIANT *charset, LONG count,
                                                LONG *delta)
2294 2295
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2296 2297 2298

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

2299
    if (!This->child.reole)
2300 2301 2302 2303 2304
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2305 2306
static HRESULT WINAPI ITextRange_fnMoveUntil(ITextRange *me, VARIANT *charset, LONG count,
                                             LONG *delta)
2307 2308
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2309 2310 2311

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

2312
    if (!This->child.reole)
2313 2314 2315 2316 2317
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2318 2319
static HRESULT WINAPI ITextRange_fnMoveStartUntil(ITextRange *me, VARIANT *charset, LONG count,
                                                  LONG *delta)
2320 2321
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2322 2323 2324

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

2325
    if (!This->child.reole)
2326 2327 2328 2329 2330
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2331 2332
static HRESULT WINAPI ITextRange_fnMoveEndUntil(ITextRange *me, VARIANT *charset, LONG count,
                                                LONG *delta)
2333 2334
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2335 2336 2337

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

2338
    if (!This->child.reole)
2339 2340 2341 2342 2343
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2344 2345
static HRESULT WINAPI ITextRange_fnFindText(ITextRange *me, BSTR text, LONG count, LONG flags,
                                            LONG *length)
2346 2347
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2348 2349 2350

    FIXME("(%p)->(%s %d %x %p): stub\n", This, debugstr_w(text), count, flags, length);

2351
    if (!This->child.reole)
2352 2353 2354 2355 2356
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2357 2358
static HRESULT WINAPI ITextRange_fnFindTextStart(ITextRange *me, BSTR text, LONG count,
                                                 LONG flags, LONG *length)
2359 2360
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2361 2362 2363

    FIXME("(%p)->(%s %d %x %p): stub\n", This, debugstr_w(text), count, flags, length);

2364
    if (!This->child.reole)
2365 2366 2367 2368 2369
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2370 2371
static HRESULT WINAPI ITextRange_fnFindTextEnd(ITextRange *me, BSTR text, LONG count,
                                               LONG flags, LONG *length)
2372 2373
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2374 2375 2376

    FIXME("(%p)->(%s %d %x %p): stub\n", This, debugstr_w(text), count, flags, length);

2377
    if (!This->child.reole)
2378 2379 2380 2381 2382
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2383
static HRESULT WINAPI ITextRange_fnDelete(ITextRange *me, LONG unit, LONG count, LONG *delta)
2384 2385
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2386 2387 2388

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, count, delta);

2389
    if (!This->child.reole)
2390 2391 2392 2393 2394
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2395
static HRESULT WINAPI ITextRange_fnCut(ITextRange *me, VARIANT *v)
2396 2397
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2398 2399 2400

    FIXME("(%p)->(%p): stub\n", This, v);

2401
    if (!This->child.reole)
2402 2403 2404 2405 2406
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2407
static HRESULT WINAPI ITextRange_fnCopy(ITextRange *me, VARIANT *v)
2408 2409
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2410 2411 2412

    FIXME("(%p)->(%p): stub\n", This, v);

2413
    if (!This->child.reole)
2414 2415 2416 2417 2418
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2419
static HRESULT WINAPI ITextRange_fnPaste(ITextRange *me, VARIANT *v, LONG format)
2420 2421
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2422 2423 2424

    FIXME("(%p)->(%s %x): stub\n", This, debugstr_variant(v), format);

2425
    if (!This->child.reole)
2426 2427 2428 2429 2430
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2431
static HRESULT WINAPI ITextRange_fnCanPaste(ITextRange *me, VARIANT *v, LONG format, LONG *ret)
2432 2433
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2434

2435
    FIXME("(%p)->(%s %x %p): stub\n", This, debugstr_variant(v), format, ret);
2436

2437
    if (!This->child.reole)
2438 2439 2440 2441 2442
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2443
static HRESULT WINAPI ITextRange_fnCanEdit(ITextRange *me, LONG *ret)
2444 2445
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2446 2447 2448

    FIXME("(%p)->(%p): stub\n", This, ret);

2449
    if (!This->child.reole)
2450 2451 2452 2453 2454
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2455
static HRESULT WINAPI ITextRange_fnChangeCase(ITextRange *me, LONG type)
2456 2457
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2458 2459 2460

    FIXME("(%p)->(%d): stub\n", This, type);

2461
    if (!This->child.reole)
2462 2463 2464 2465 2466
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2467
static HRESULT WINAPI ITextRange_fnGetPoint(ITextRange *me, LONG type, LONG *cx, LONG *cy)
2468 2469
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2470 2471 2472

    FIXME("(%p)->(%d %p %p): stub\n", This, type, cx, cy);

2473
    if (!This->child.reole)
2474 2475 2476 2477 2478
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2479 2480
static HRESULT WINAPI ITextRange_fnSetPoint(ITextRange *me, LONG x, LONG y, LONG type,
                                            LONG extend)
2481 2482
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2483 2484 2485

    FIXME("(%p)->(%d %d %d %d): stub\n", This, x, y, type, extend);

2486
    if (!This->child.reole)
2487 2488 2489 2490 2491
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2492
static HRESULT WINAPI ITextRange_fnScrollIntoView(ITextRange *me, LONG value)
2493 2494
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2495 2496 2497
    ME_TextEditor *editor;
    ME_Cursor cursor;
    int x, y, height;
2498

2499
    TRACE("(%p)->(%d)\n", This, value);
2500

2501
    if (!This->child.reole)
2502 2503
        return CO_E_RELEASED;

2504 2505 2506 2507 2508 2509 2510 2511
    editor = This->child.reole->editor;

    switch (value)
    {
    case tomStart:
        ME_CursorFromCharOfs(editor, This->start, &cursor);
        ME_GetCursorCoordinates(editor, &cursor, &x, &y, &height);
        break;
2512 2513 2514 2515
    case tomEnd:
        ME_CursorFromCharOfs(editor, This->end, &cursor);
        ME_GetCursorCoordinates(editor, &cursor, &x, &y, &height);
        break;
2516 2517 2518 2519 2520 2521
    default:
        FIXME("bStart value %d not handled\n", value);
        return E_NOTIMPL;
    }
    ME_ScrollAbs(editor, x, y);
    return S_OK;
2522 2523 2524 2525 2526
}

static HRESULT WINAPI ITextRange_fnGetEmbeddedObject(ITextRange *me, IUnknown **ppv)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2527 2528 2529

    FIXME("(%p)->(%p): stub\n", This, ppv);

2530
    if (!This->child.reole)
2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

static const ITextRangeVtbl trvt = {
    ITextRange_fnQueryInterface,
    ITextRange_fnAddRef,
    ITextRange_fnRelease,
    ITextRange_fnGetTypeInfoCount,
    ITextRange_fnGetTypeInfo,
    ITextRange_fnGetIDsOfNames,
    ITextRange_fnInvoke,
    ITextRange_fnGetText,
    ITextRange_fnSetText,
    ITextRange_fnGetChar,
    ITextRange_fnSetChar,
    ITextRange_fnGetDuplicate,
    ITextRange_fnGetFormattedText,
    ITextRange_fnSetFormattedText,
    ITextRange_fnGetStart,
    ITextRange_fnSetStart,
    ITextRange_fnGetEnd,
    ITextRange_fnSetEnd,
    ITextRange_fnGetFont,
    ITextRange_fnSetFont,
    ITextRange_fnGetPara,
    ITextRange_fnSetPara,
    ITextRange_fnGetStoryLength,
    ITextRange_fnGetStoryType,
    ITextRange_fnCollapse,
    ITextRange_fnExpand,
    ITextRange_fnGetIndex,
    ITextRange_fnSetIndex,
    ITextRange_fnSetRange,
    ITextRange_fnInRange,
    ITextRange_fnInStory,
    ITextRange_fnIsEqual,
    ITextRange_fnSelect,
    ITextRange_fnStartOf,
    ITextRange_fnEndOf,
    ITextRange_fnMove,
    ITextRange_fnMoveStart,
    ITextRange_fnMoveEnd,
    ITextRange_fnMoveWhile,
    ITextRange_fnMoveStartWhile,
    ITextRange_fnMoveEndWhile,
    ITextRange_fnMoveUntil,
    ITextRange_fnMoveStartUntil,
    ITextRange_fnMoveEndUntil,
    ITextRange_fnFindText,
    ITextRange_fnFindTextStart,
    ITextRange_fnFindTextEnd,
    ITextRange_fnDelete,
    ITextRange_fnCut,
    ITextRange_fnCopy,
    ITextRange_fnPaste,
    ITextRange_fnCanPaste,
    ITextRange_fnCanEdit,
    ITextRange_fnChangeCase,
    ITextRange_fnGetPoint,
    ITextRange_fnSetPoint,
    ITextRange_fnScrollIntoView,
    ITextRange_fnGetEmbeddedObject
};

2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633
/* ITextFont */
static HRESULT WINAPI TextFont_QueryInterface(ITextFont *iface, REFIID riid, void **ppv)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);

    if (IsEqualIID(riid, &IID_ITextFont) ||
        IsEqualIID(riid, &IID_IDispatch) ||
        IsEqualIID(riid, &IID_IUnknown))
    {
        *ppv = iface;
        ITextFont_AddRef(iface);
        return S_OK;
    }

    *ppv = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI TextFont_AddRef(ITextFont *iface)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
    ULONG ref = InterlockedIncrement(&This->ref);
    TRACE("(%p)->(%u)\n", This, ref);
    return ref;
}

static ULONG WINAPI TextFont_Release(ITextFont *iface)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p)->(%u)\n", This, ref);

    if (!ref)
    {
2634 2635 2636
        if (This->range)
            ITextRange_Release(This->range);
        SysFreeString(This->props[FONT_NAME].str);
2637 2638 2639 2640 2641 2642 2643 2644
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI TextFont_GetTypeInfoCount(ITextFont *iface, UINT *pctinfo)
{
2645 2646 2647 2648
    ITextFontImpl *This = impl_from_ITextFont(iface);
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
2649 2650 2651 2652 2653
}

static HRESULT WINAPI TextFont_GetTypeInfo(ITextFont *iface, UINT iTInfo, LCID lcid,
    ITypeInfo **ppTInfo)
{
2654 2655 2656 2657 2658 2659 2660 2661 2662
    ITextFontImpl *This = impl_from_ITextFont(iface);
    HRESULT hr;

    TRACE("(%p)->(%u,%d,%p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(ITextFont_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
2663 2664 2665 2666 2667
}

static HRESULT WINAPI TextFont_GetIDsOfNames(ITextFont *iface, REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
2668 2669 2670 2671
    ITextFontImpl *This = impl_from_ITextFont(iface);
    ITypeInfo *ti;
    HRESULT hr;

2672 2673
    TRACE("(%p)->(%s, %p, %u, %d, %p)\n", This, debugstr_guid(riid),
            rgszNames, cNames, lcid, rgDispId);
2674 2675 2676 2677 2678

    hr = get_typeinfo(ITextFont_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
}

static HRESULT WINAPI TextFont_Invoke(
    ITextFont *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS *pDispParams,
    VARIANT *pVarResult,
    EXCEPINFO *pExcepInfo,
    UINT *puArgErr)
{
2692 2693 2694 2695
    ITextFontImpl *This = impl_from_ITextFont(iface);
    ITypeInfo *ti;
    HRESULT hr;

2696 2697
    TRACE("(%p)->(%d, %s, %d, %u, %p, %p, %p, %p)\n", This, dispIdMember, debugstr_guid(riid),
            lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2698 2699 2700 2701 2702

    hr = get_typeinfo(ITextFont_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_Invoke(ti, iface, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    return hr;
2703 2704 2705 2706 2707
}

static HRESULT WINAPI TextFont_GetDuplicate(ITextFont *iface, ITextFont **ret)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717

    TRACE("(%p)->(%p)\n", This, ret);

    if (!ret)
        return E_INVALIDARG;

    *ret = NULL;
    if (This->range && !get_range_reole(This->range))
        return CO_E_RELEASED;

2718
    return create_textfont(NULL, This, ret);
2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
}

static HRESULT WINAPI TextFont_SetDuplicate(ITextFont *iface, ITextFont *pFont)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
    FIXME("(%p)->(%p): stub\n", This, pFont);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextFont_CanChange(ITextFont *iface, LONG *ret)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
    FIXME("(%p)->(%p): stub\n", This, ret);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextFont_IsEqual(ITextFont *iface, ITextFont *font, LONG *ret)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2738
    FIXME("(%p)->(%p %p): stub\n", This, font, ret);
2739 2740 2741
    return E_NOTIMPL;
}

2742 2743 2744 2745
static void textfont_reset_to_default(ITextFontImpl *font)
{
    enum textfont_prop_id id;

2746
    for (id = FONT_PROPID_FIRST; id < FONT_PROPID_LAST; id++) {
2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797
        switch (id)
        {
        case FONT_ALLCAPS:
        case FONT_ANIMATION:
        case FONT_BOLD:
        case FONT_EMBOSS:
        case FONT_HIDDEN:
        case FONT_ENGRAVE:
        case FONT_ITALIC:
        case FONT_OUTLINE:
        case FONT_PROTECTED:
        case FONT_SHADOW:
        case FONT_SMALLCAPS:
        case FONT_STRIKETHROUGH:
        case FONT_SUBSCRIPT:
        case FONT_SUPERSCRIPT:
        case FONT_UNDERLINE:
            font->props[id].l = tomFalse;
            break;
        case FONT_BACKCOLOR:
        case FONT_FORECOLOR:
            font->props[id].l = tomAutoColor;
            break;
        case FONT_KERNING:
        case FONT_POSITION:
        case FONT_SIZE:
        case FONT_SPACING:
            font->props[id].f = 0.0;
            break;
        case FONT_LANGID:
            font->props[id].l = GetSystemDefaultLCID();
            break;
        case FONT_NAME: {
            static const WCHAR sysW[] = {'S','y','s','t','e','m',0};
            SysFreeString(font->props[id].str);
            font->props[id].str = SysAllocString(sysW);
            break;
        }
        case FONT_WEIGHT:
            font->props[id].l = FW_NORMAL;
            break;
        default:
            FIXME("font property %d not handled\n", id);
        }
    }
}

static void textfont_reset_to_undefined(ITextFontImpl *font)
{
    enum textfont_prop_id id;

2798
    for (id = FONT_PROPID_FIRST; id < FONT_PROPID_LAST; id++) {
2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
        switch (id)
        {
        case FONT_ALLCAPS:
        case FONT_ANIMATION:
        case FONT_BOLD:
        case FONT_EMBOSS:
        case FONT_HIDDEN:
        case FONT_ENGRAVE:
        case FONT_ITALIC:
        case FONT_OUTLINE:
        case FONT_PROTECTED:
        case FONT_SHADOW:
        case FONT_SMALLCAPS:
        case FONT_STRIKETHROUGH:
        case FONT_SUBSCRIPT:
        case FONT_SUPERSCRIPT:
        case FONT_UNDERLINE:
        case FONT_BACKCOLOR:
        case FONT_FORECOLOR:
        case FONT_LANGID:
        case FONT_WEIGHT:
            font->props[id].l = tomUndefined;
            break;
        case FONT_KERNING:
        case FONT_POSITION:
        case FONT_SIZE:
        case FONT_SPACING:
            font->props[id].f = tomUndefined;
            break;
        case FONT_NAME:
            break;
        default:
            FIXME("font property %d not handled\n", id);
        }
    }
}

2836 2837 2838 2839 2840 2841 2842
static void textfont_apply_range_props(ITextFontImpl *font)
{
    enum textfont_prop_id propid;
    for (propid = FONT_PROPID_FIRST; propid < FONT_PROPID_LAST; propid++)
        set_textfont_prop(font, propid, &font->props[propid]);
}

2843 2844 2845
static HRESULT WINAPI TextFont_Reset(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2846 2847 2848 2849 2850 2851 2852 2853 2854

    TRACE("(%p)->(%d)\n", This, value);

    /* If font is attached to a range, released or not, we can't
       reset to undefined */
    if (This->range) {
        if (!get_range_reole(This->range))
            return CO_E_RELEASED;

2855 2856 2857
        switch (value)
        {
        case tomUndefined:
2858
            return E_INVALIDARG;
2859 2860 2861 2862 2863 2864 2865
        case tomCacheParms:
            textfont_cache_range_props(This);
            This->get_cache_enabled = TRUE;
            break;
        case tomTrackParms:
            This->get_cache_enabled = FALSE;
            break;
2866 2867 2868 2869 2870 2871 2872
        case tomApplyLater:
            This->set_cache_enabled = TRUE;
            break;
        case tomApplyNow:
            This->set_cache_enabled = FALSE;
            textfont_apply_range_props(This);
            break;
2873 2874 2875
        case tomUsePoints:
        case tomUseTwips:
            return E_INVALIDARG;
2876 2877 2878 2879 2880
        default:
            FIXME("reset mode %d not supported\n", value);
        }

        return S_OK;
2881
    }
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892
    else {
        switch (value)
        {
        /* reset to global defaults */
        case tomDefault:
            textfont_reset_to_default(This);
            return S_OK;
        /* all properties are set to tomUndefined, font name is retained */
        case tomUndefined:
            textfont_reset_to_undefined(This);
            return S_OK;
2893 2894 2895 2896 2897
        case tomApplyNow:
        case tomApplyLater:
        case tomTrackParms:
        case tomCacheParms:
            return S_OK;
2898 2899 2900
        case tomUsePoints:
        case tomUseTwips:
            return E_INVALIDARG;
2901 2902
        }
    }
2903 2904

    FIXME("reset mode %d not supported\n", value);
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924
    return E_NOTIMPL;
}

static HRESULT WINAPI TextFont_GetStyle(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
    FIXME("(%p)->(%p): stub\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextFont_SetStyle(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
    FIXME("(%p)->(%d): stub\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextFont_GetAllCaps(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2925 2926
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_ALLCAPS, value);
2927 2928 2929 2930 2931
}

static HRESULT WINAPI TextFont_SetAllCaps(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2932 2933
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_ALLCAPS, value);
2934 2935 2936 2937 2938
}

static HRESULT WINAPI TextFont_GetAnimation(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2939 2940
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_ANIMATION, value);
2941 2942 2943 2944 2945
}

static HRESULT WINAPI TextFont_SetAnimation(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2946 2947 2948 2949 2950 2951 2952

    TRACE("(%p)->(%d)\n", This, value);

    if (value < tomNoAnimation || value > tomAnimationMax)
        return E_INVALIDARG;

    return set_textfont_propl(This, FONT_ANIMATION, value);
2953 2954 2955 2956 2957
}

static HRESULT WINAPI TextFont_GetBackColor(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2958 2959
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_BACKCOLOR, value);
2960 2961 2962 2963 2964
}

static HRESULT WINAPI TextFont_SetBackColor(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2965 2966
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propl(This, FONT_BACKCOLOR, value);
2967 2968 2969 2970 2971
}

static HRESULT WINAPI TextFont_GetBold(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2972
    TRACE("(%p)->(%p)\n", This, value);
2973
    return get_textfont_propl(This, FONT_BOLD, value);
2974 2975 2976 2977 2978
}

static HRESULT WINAPI TextFont_SetBold(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2979 2980
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_BOLD, value);
2981 2982 2983 2984 2985
}

static HRESULT WINAPI TextFont_GetEmboss(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2986 2987
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_EMBOSS, value);
2988 2989 2990 2991 2992
}

static HRESULT WINAPI TextFont_SetEmboss(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2993 2994
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_EMBOSS, value);
2995 2996 2997 2998 2999
}

static HRESULT WINAPI TextFont_GetForeColor(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3000
    TRACE("(%p)->(%p)\n", This, value);
3001
    return get_textfont_propl(This, FONT_FORECOLOR, value);
3002 3003 3004 3005 3006
}

static HRESULT WINAPI TextFont_SetForeColor(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3007 3008
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propl(This, FONT_FORECOLOR, value);
3009 3010 3011 3012 3013
}

static HRESULT WINAPI TextFont_GetHidden(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3014 3015
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_HIDDEN, value);
3016 3017 3018 3019 3020
}

static HRESULT WINAPI TextFont_SetHidden(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3021 3022
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_HIDDEN, value);
3023 3024 3025 3026 3027
}

static HRESULT WINAPI TextFont_GetEngrave(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3028 3029
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_ENGRAVE, value);
3030 3031 3032 3033 3034
}

static HRESULT WINAPI TextFont_SetEngrave(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3035 3036
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_ENGRAVE, value);
3037 3038 3039 3040 3041
}

static HRESULT WINAPI TextFont_GetItalic(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3042
    TRACE("(%p)->(%p)\n", This, value);
3043
    return get_textfont_propl(This, FONT_ITALIC, value);
3044 3045 3046 3047 3048
}

static HRESULT WINAPI TextFont_SetItalic(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3049 3050
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_ITALIC, value);
3051 3052 3053 3054 3055
}

static HRESULT WINAPI TextFont_GetKerning(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3056 3057
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propf(This, FONT_KERNING, value);
3058 3059 3060 3061 3062
}

static HRESULT WINAPI TextFont_SetKerning(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3063 3064
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_KERNING, value);
3065 3066 3067 3068 3069
}

static HRESULT WINAPI TextFont_GetLanguageID(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3070
    TRACE("(%p)->(%p)\n", This, value);
3071
    return get_textfont_propl(This, FONT_LANGID, value);
3072 3073 3074 3075 3076
}

static HRESULT WINAPI TextFont_SetLanguageID(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3077 3078
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propl(This, FONT_LANGID, value);
3079 3080 3081 3082 3083
}

static HRESULT WINAPI TextFont_GetName(ITextFont *iface, BSTR *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3084 3085 3086 3087 3088 3089 3090 3091

    TRACE("(%p)->(%p)\n", This, value);

    if (!value)
        return E_INVALIDARG;

    *value = NULL;

3092 3093 3094 3095 3096 3097 3098 3099
    if (!This->range) {
        if (This->props[FONT_NAME].str)
            *value = SysAllocString(This->props[FONT_NAME].str);
        else
            *value = SysAllocStringLen(NULL, 0);
        return *value ? S_OK : E_OUTOFMEMORY;
    }

3100
    return textfont_getname_from_range(This->range, value);
3101 3102 3103 3104 3105
}

static HRESULT WINAPI TextFont_SetName(ITextFont *iface, BSTR value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3106 3107 3108 3109 3110 3111
    textfont_prop_val v;

    TRACE("(%p)->(%s)\n", This, debugstr_w(value));

    v.str = value;
    return set_textfont_prop(This, FONT_NAME, &v);
3112 3113 3114 3115 3116
}

static HRESULT WINAPI TextFont_GetOutline(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3117 3118
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_OUTLINE, value);
3119 3120 3121 3122 3123
}

static HRESULT WINAPI TextFont_SetOutline(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3124 3125
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_OUTLINE, value);
3126 3127 3128 3129 3130
}

static HRESULT WINAPI TextFont_GetPosition(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3131 3132
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propf(This, FONT_POSITION, value);
3133 3134 3135 3136 3137
}

static HRESULT WINAPI TextFont_SetPosition(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3138 3139
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_POSITION, value);
3140 3141 3142 3143 3144
}

static HRESULT WINAPI TextFont_GetProtected(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3145 3146
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_PROTECTED, value);
3147 3148 3149 3150 3151
}

static HRESULT WINAPI TextFont_SetProtected(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3152 3153
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_PROTECTED, value);
3154 3155 3156 3157 3158
}

static HRESULT WINAPI TextFont_GetShadow(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3159 3160
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_SHADOW, value);
3161 3162 3163 3164 3165
}

static HRESULT WINAPI TextFont_SetShadow(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3166 3167
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_SHADOW, value);
3168 3169 3170 3171 3172
}

static HRESULT WINAPI TextFont_GetSize(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3173
    TRACE("(%p)->(%p)\n", This, value);
3174
    return get_textfont_propf(This, FONT_SIZE, value);
3175 3176 3177 3178 3179
}

static HRESULT WINAPI TextFont_SetSize(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3180 3181
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_SIZE, value);
3182 3183 3184 3185 3186
}

static HRESULT WINAPI TextFont_GetSmallCaps(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3187 3188
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_SMALLCAPS, value);
3189 3190 3191 3192 3193
}

static HRESULT WINAPI TextFont_SetSmallCaps(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3194 3195
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_SMALLCAPS, value);
3196 3197 3198 3199 3200
}

static HRESULT WINAPI TextFont_GetSpacing(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3201 3202
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propf(This, FONT_SPACING, value);
3203 3204 3205 3206 3207
}

static HRESULT WINAPI TextFont_SetSpacing(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3208 3209
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_SPACING, value);
3210 3211 3212 3213 3214
}

static HRESULT WINAPI TextFont_GetStrikeThrough(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3215
    TRACE("(%p)->(%p)\n", This, value);
3216
    return get_textfont_propl(This, FONT_STRIKETHROUGH, value);
3217 3218 3219 3220 3221
}

static HRESULT WINAPI TextFont_SetStrikeThrough(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3222 3223
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_STRIKETHROUGH, value);
3224 3225 3226 3227 3228
}

static HRESULT WINAPI TextFont_GetSubscript(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3229
    TRACE("(%p)->(%p)\n", This, value);
3230
    return get_textfont_propl(This, FONT_SUBSCRIPT, value);
3231 3232 3233 3234 3235
}

static HRESULT WINAPI TextFont_SetSubscript(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3236 3237
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_SUBSCRIPT, value);
3238 3239 3240 3241 3242
}

static HRESULT WINAPI TextFont_GetSuperscript(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3243
    TRACE("(%p)->(%p)\n", This, value);
3244
    return get_textfont_propl(This, FONT_SUPERSCRIPT, value);
3245 3246 3247 3248 3249
}

static HRESULT WINAPI TextFont_SetSuperscript(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3250 3251
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_SUPERSCRIPT, value);
3252 3253 3254 3255 3256
}

static HRESULT WINAPI TextFont_GetUnderline(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3257
    TRACE("(%p)->(%p)\n", This, value);
3258
    return get_textfont_propl(This, FONT_UNDERLINE, value);
3259 3260 3261 3262 3263
}

static HRESULT WINAPI TextFont_SetUnderline(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3264 3265
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propd(This, FONT_UNDERLINE, value);
3266 3267 3268 3269 3270
}

static HRESULT WINAPI TextFont_GetWeight(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3271 3272
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_WEIGHT, value);
3273 3274 3275 3276 3277
}

static HRESULT WINAPI TextFont_SetWeight(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3278 3279
    TRACE("(%p)->(%d)\n", This, value);
    return set_textfont_propl(This, FONT_WEIGHT, value);
3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346
}

static ITextFontVtbl textfontvtbl = {
    TextFont_QueryInterface,
    TextFont_AddRef,
    TextFont_Release,
    TextFont_GetTypeInfoCount,
    TextFont_GetTypeInfo,
    TextFont_GetIDsOfNames,
    TextFont_Invoke,
    TextFont_GetDuplicate,
    TextFont_SetDuplicate,
    TextFont_CanChange,
    TextFont_IsEqual,
    TextFont_Reset,
    TextFont_GetStyle,
    TextFont_SetStyle,
    TextFont_GetAllCaps,
    TextFont_SetAllCaps,
    TextFont_GetAnimation,
    TextFont_SetAnimation,
    TextFont_GetBackColor,
    TextFont_SetBackColor,
    TextFont_GetBold,
    TextFont_SetBold,
    TextFont_GetEmboss,
    TextFont_SetEmboss,
    TextFont_GetForeColor,
    TextFont_SetForeColor,
    TextFont_GetHidden,
    TextFont_SetHidden,
    TextFont_GetEngrave,
    TextFont_SetEngrave,
    TextFont_GetItalic,
    TextFont_SetItalic,
    TextFont_GetKerning,
    TextFont_SetKerning,
    TextFont_GetLanguageID,
    TextFont_SetLanguageID,
    TextFont_GetName,
    TextFont_SetName,
    TextFont_GetOutline,
    TextFont_SetOutline,
    TextFont_GetPosition,
    TextFont_SetPosition,
    TextFont_GetProtected,
    TextFont_SetProtected,
    TextFont_GetShadow,
    TextFont_SetShadow,
    TextFont_GetSize,
    TextFont_SetSize,
    TextFont_GetSmallCaps,
    TextFont_SetSmallCaps,
    TextFont_GetSpacing,
    TextFont_SetSpacing,
    TextFont_GetStrikeThrough,
    TextFont_SetStrikeThrough,
    TextFont_GetSubscript,
    TextFont_SetSubscript,
    TextFont_GetSuperscript,
    TextFont_SetSuperscript,
    TextFont_GetUnderline,
    TextFont_SetUnderline,
    TextFont_GetWeight,
    TextFont_SetWeight
};

3347
static HRESULT create_textfont(ITextRange *range, const ITextFontImpl *src, ITextFont **ret)
3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
{
    ITextFontImpl *font;

    *ret = NULL;
    font = heap_alloc(sizeof(*font));
    if (!font)
        return E_OUTOFMEMORY;

    font->ITextFont_iface.lpVtbl = &textfontvtbl;
    font->ref = 1;
3358 3359 3360

    if (src) {
        font->range = NULL;
3361
        font->get_cache_enabled = TRUE;
3362
        font->set_cache_enabled = TRUE;
3363 3364 3365 3366 3367 3368 3369
        memcpy(&font->props, &src->props, sizeof(font->props));
        if (font->props[FONT_NAME].str)
            font->props[FONT_NAME].str = SysAllocString(font->props[FONT_NAME].str);
    }
    else {
        font->range = range;
        ITextRange_AddRef(range);
3370 3371 3372

        /* cache current properties */
        font->get_cache_enabled = FALSE;
3373
        font->set_cache_enabled = FALSE;
3374
        textfont_cache_range_props(font);
3375
    }
3376 3377 3378 3379 3380

    *ret = &font->ITextFont_iface;
    return S_OK;
}

3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426
/* ITextPara */
static HRESULT WINAPI TextPara_QueryInterface(ITextPara *iface, REFIID riid, void **ppv)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);

    TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);

    if (IsEqualIID(riid, &IID_ITextPara) ||
        IsEqualIID(riid, &IID_IDispatch) ||
        IsEqualIID(riid, &IID_IUnknown))
    {
        *ppv = iface;
        ITextPara_AddRef(iface);
        return S_OK;
    }

    *ppv = NULL;
    return E_NOINTERFACE;
}

static ULONG WINAPI TextPara_AddRef(ITextPara *iface)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    ULONG ref = InterlockedIncrement(&This->ref);
    TRACE("(%p)->(%u)\n", This, ref);
    return ref;
}

static ULONG WINAPI TextPara_Release(ITextPara *iface)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    ULONG ref = InterlockedDecrement(&This->ref);

    TRACE("(%p)->(%u)\n", This, ref);

    if (!ref)
    {
        ITextRange_Release(This->range);
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI TextPara_GetTypeInfoCount(ITextPara *iface, UINT *pctinfo)
{
3427 3428 3429 3430
    ITextParaImpl *This = impl_from_ITextPara(iface);
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
3431 3432 3433 3434 3435
}

static HRESULT WINAPI TextPara_GetTypeInfo(ITextPara *iface, UINT iTInfo, LCID lcid,
    ITypeInfo **ppTInfo)
{
3436 3437 3438 3439 3440 3441 3442 3443 3444
    ITextParaImpl *This = impl_from_ITextPara(iface);
    HRESULT hr;

    TRACE("(%p)->(%u,%d,%p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(ITextPara_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
3445 3446 3447 3448 3449
}

static HRESULT WINAPI TextPara_GetIDsOfNames(ITextPara *iface, REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
3450 3451 3452 3453
    ITextParaImpl *This = impl_from_ITextPara(iface);
    ITypeInfo *ti;
    HRESULT hr;

3454 3455
    TRACE("(%p)->(%s, %p, %u, %d, %p)\n", This, debugstr_guid(riid), rgszNames,
            cNames, lcid, rgDispId);
3456 3457 3458 3459 3460

    hr = get_typeinfo(ITextPara_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473
}

static HRESULT WINAPI TextPara_Invoke(
    ITextPara *iface,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS *pDispParams,
    VARIANT *pVarResult,
    EXCEPINFO *pExcepInfo,
    UINT *puArgErr)
{
3474 3475 3476 3477
    ITextParaImpl *This = impl_from_ITextPara(iface);
    ITypeInfo *ti;
    HRESULT hr;

3478 3479 3480
    TRACE("(%p)->(%d, %s, %d, %u, %p, %p, %p, %p)\n", This, dispIdMember,
            debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
            pExcepInfo, puArgErr);
3481 3482 3483 3484 3485

    hr = get_typeinfo(ITextPara_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_Invoke(ti, iface, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    return hr;
3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899
}

static HRESULT WINAPI TextPara_GetDuplicate(ITextPara *iface, ITextPara **ret)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, ret);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetDuplicate(ITextPara *iface, ITextPara *para)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, para);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_CanChange(ITextPara *iface, LONG *ret)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, ret);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_IsEqual(ITextPara *iface, ITextPara *para, LONG *ret)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p %p)\n", This, para, ret);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_Reset(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetStyle(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetStyle(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetAlignment(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetAlignment(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetHyphenation(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetHyphenation(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetFirstLineIndent(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetKeepTogether(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetKeepTogether(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetKeepWithNext(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetKeepWithNext(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetLeftIndent(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetLineSpacing(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetLineSpacingRule(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetListAlignment(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetListAlignment(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetListLevelIndex(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetListLevelIndex(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetListStart(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetListStart(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetListTab(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetListTab(ITextPara *iface, FLOAT value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetListType(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetListType(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetNoLineNumber(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetNoLineNumber(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetPageBreakBefore(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetPageBreakBefore(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetRightIndent(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetRightIndent(ITextPara *iface, FLOAT value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetIndents(ITextPara *iface, FLOAT StartIndent, FLOAT LeftIndent, FLOAT RightIndent)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f %.2f %.2f)\n", This, StartIndent, LeftIndent, RightIndent);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetLineSpacing(ITextPara *iface, LONG LineSpacingRule, FLOAT LineSpacing)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d %.2f)\n", This, LineSpacingRule, LineSpacing);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetSpaceAfter(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetSpaceAfter(ITextPara *iface, FLOAT value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetSpaceBefore(ITextPara *iface, FLOAT *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetSpaceBefore(ITextPara *iface, FLOAT value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetWidowControl(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_SetWidowControl(ITextPara *iface, LONG value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetTabCount(ITextPara *iface, LONG *value)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%p)\n", This, value);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_AddTab(ITextPara *iface, FLOAT tbPos, LONG tbAlign, LONG tbLeader)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f %d %d)\n", This, tbPos, tbAlign, tbLeader);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_ClearAllTabs(ITextPara *iface)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)\n", This);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_DeleteTab(ITextPara *iface, FLOAT pos)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%.2f)\n", This, pos);
    return E_NOTIMPL;
}

static HRESULT WINAPI TextPara_GetTab(ITextPara *iface, LONG iTab, FLOAT *ptbPos, LONG *ptbAlign, LONG *ptbLeader)
{
    ITextParaImpl *This = impl_from_ITextPara(iface);
    FIXME("(%p)->(%d %p %p %p)\n", This, iTab, ptbPos, ptbAlign, ptbLeader);
    return E_NOTIMPL;
}

static ITextParaVtbl textparavtbl = {
    TextPara_QueryInterface,
    TextPara_AddRef,
    TextPara_Release,
    TextPara_GetTypeInfoCount,
    TextPara_GetTypeInfo,
    TextPara_GetIDsOfNames,
    TextPara_Invoke,
    TextPara_GetDuplicate,
    TextPara_SetDuplicate,
    TextPara_CanChange,
    TextPara_IsEqual,
    TextPara_Reset,
    TextPara_GetStyle,
    TextPara_SetStyle,
    TextPara_GetAlignment,
    TextPara_SetAlignment,
    TextPara_GetHyphenation,
    TextPara_SetHyphenation,
    TextPara_GetFirstLineIndent,
    TextPara_GetKeepTogether,
    TextPara_SetKeepTogether,
    TextPara_GetKeepWithNext,
    TextPara_SetKeepWithNext,
    TextPara_GetLeftIndent,
    TextPara_GetLineSpacing,
    TextPara_GetLineSpacingRule,
    TextPara_GetListAlignment,
    TextPara_SetListAlignment,
    TextPara_GetListLevelIndex,
    TextPara_SetListLevelIndex,
    TextPara_GetListStart,
    TextPara_SetListStart,
    TextPara_GetListTab,
    TextPara_SetListTab,
    TextPara_GetListType,
    TextPara_SetListType,
    TextPara_GetNoLineNumber,
    TextPara_SetNoLineNumber,
    TextPara_GetPageBreakBefore,
    TextPara_SetPageBreakBefore,
    TextPara_GetRightIndent,
    TextPara_SetRightIndent,
    TextPara_SetIndents,
    TextPara_SetLineSpacing,
    TextPara_GetSpaceAfter,
    TextPara_SetSpaceAfter,
    TextPara_GetSpaceBefore,
    TextPara_SetSpaceBefore,
    TextPara_GetWidowControl,
    TextPara_SetWidowControl,
    TextPara_GetTabCount,
    TextPara_AddTab,
    TextPara_ClearAllTabs,
    TextPara_DeleteTab,
    TextPara_GetTab
};

static HRESULT create_textpara(ITextRange *range, ITextPara **ret)
{
    ITextParaImpl *para;

    *ret = NULL;
    para = heap_alloc(sizeof(*para));
    if (!para)
        return E_OUTOFMEMORY;

    para->ITextPara_iface.lpVtbl = &textparavtbl;
    para->ref = 1;
    para->range = range;
    ITextRange_AddRef(range);

    *ret = &para->ITextPara_iface;
    return S_OK;
}

3900
/* ITextDocument */
3901 3902
static HRESULT WINAPI ITextDocument2Old_fnQueryInterface(ITextDocument2Old* iface, REFIID riid,
                                                         void **ppvObject)
3903
{
3904
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3905
    return IRichEditOle_QueryInterface(&This->IRichEditOle_iface, riid, ppvObject);
3906 3907
}

3908
static ULONG WINAPI ITextDocument2Old_fnAddRef(ITextDocument2Old *iface)
3909
{
3910
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3911
    return IRichEditOle_AddRef(&This->IRichEditOle_iface);
3912 3913
}

3914
static ULONG WINAPI ITextDocument2Old_fnRelease(ITextDocument2Old *iface)
3915
{
3916
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3917
    return IRichEditOle_Release(&This->IRichEditOle_iface);
3918 3919
}

3920 3921
static HRESULT WINAPI ITextDocument2Old_fnGetTypeInfoCount(ITextDocument2Old *iface,
                                                           UINT *pctinfo)
3922
{
3923
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3924 3925 3926
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
3927 3928
}

3929 3930
static HRESULT WINAPI ITextDocument2Old_fnGetTypeInfo(ITextDocument2Old *iface, UINT iTInfo, LCID lcid,
                                                      ITypeInfo **ppTInfo)
3931
{
3932
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3933 3934 3935 3936 3937 3938 3939 3940
    HRESULT hr;

    TRACE("(%p)->(%u,%d,%p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(ITextDocument_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
3941 3942
}

3943 3944 3945
static HRESULT WINAPI ITextDocument2Old_fnGetIDsOfNames(ITextDocument2Old *iface, REFIID riid,
                                                        LPOLESTR *rgszNames, UINT cNames,
                                                        LCID lcid, DISPID *rgDispId)
3946
{
3947
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3948 3949 3950
    ITypeInfo *ti;
    HRESULT hr;

3951 3952
    TRACE("(%p)->(%s, %p, %u, %d, %p)\n", This, debugstr_guid(riid),
            rgszNames, cNames, lcid, rgDispId);
3953 3954 3955 3956 3957

    hr = get_typeinfo(ITextDocument_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
3958 3959
}

3960 3961 3962 3963
static HRESULT WINAPI ITextDocument2Old_fnInvoke(ITextDocument2Old *iface, DISPID dispIdMember,
                                                 REFIID riid, LCID lcid, WORD wFlags,
                                                 DISPPARAMS *pDispParams, VARIANT *pVarResult,
                                                 EXCEPINFO *pExcepInfo, UINT *puArgErr)
3964
{
3965
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3966 3967 3968
    ITypeInfo *ti;
    HRESULT hr;

3969 3970 3971
    TRACE("(%p)->(%d, %s, %d, %u, %p, %p, %p, %p)\n", This, dispIdMember,
            debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
            pExcepInfo, puArgErr);
3972 3973 3974

    hr = get_typeinfo(ITextDocument_tid, &ti);
    if (SUCCEEDED(hr))
3975
        hr = ITypeInfo_Invoke(ti, iface, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3976
    return hr;
3977 3978
}

3979
static HRESULT WINAPI ITextDocument2Old_fnGetName(ITextDocument2Old *iface, BSTR *pName)
3980
{
3981
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3982 3983 3984 3985
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

3986
static HRESULT WINAPI ITextDocument2Old_fnGetSelection(ITextDocument2Old *iface, ITextSelection **selection)
3987
{
3988
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
3989

3990
    TRACE("(%p)->(%p)\n", iface, selection);
3991 3992

    if (!selection)
3993
      return E_INVALIDARG;
3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004

    if (!This->txtSel) {
      This->txtSel = CreateTextSelection(This);
      if (!This->txtSel) {
        *selection = NULL;
        return E_OUTOFMEMORY;
      }
    }

    *selection = &This->txtSel->ITextSelection_iface;
    ITextSelection_AddRef(*selection);
4005
    return S_OK;
4006 4007
}

4008
static HRESULT WINAPI ITextDocument2Old_fnGetStoryCount(ITextDocument2Old *iface, LONG *pCount)
4009
{
4010
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4011 4012 4013 4014
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4015 4016
static HRESULT WINAPI ITextDocument2Old_fnGetStoryRanges(ITextDocument2Old *iface,
                                                         ITextStoryRanges **ppStories)
4017
{
4018
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4019 4020 4021 4022
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4023
static HRESULT WINAPI ITextDocument2Old_fnGetSaved(ITextDocument2Old *iface, LONG *pValue)
4024
{
4025
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4026 4027 4028 4029
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4030
static HRESULT WINAPI ITextDocument2Old_fnSetSaved(ITextDocument2Old *iface, LONG Value)
4031
{
4032
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4033 4034 4035 4036
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4037
static HRESULT WINAPI ITextDocument2Old_fnGetDefaultTabStop(ITextDocument2Old *iface, float *pValue)
4038
{
4039
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4040 4041 4042 4043
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4044
static HRESULT WINAPI ITextDocument2Old_fnSetDefaultTabStop(ITextDocument2Old *iface, float Value)
4045
{
4046
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4047 4048 4049 4050
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4051
static HRESULT WINAPI ITextDocument2Old_fnNew(ITextDocument2Old *iface)
4052
{
4053
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4054 4055 4056 4057
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4058 4059
static HRESULT WINAPI ITextDocument2Old_fnOpen(ITextDocument2Old *iface, VARIANT *pVar,
                                               LONG Flags, LONG CodePage)
4060
{
4061
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4062 4063 4064 4065
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4066 4067
static HRESULT WINAPI ITextDocument2Old_fnSave(ITextDocument2Old *iface, VARIANT *pVar,
                                               LONG Flags, LONG CodePage)
4068
{
4069
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4070 4071 4072 4073
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4074
static HRESULT WINAPI ITextDocument2Old_fnFreeze(ITextDocument2Old *iface, LONG *pCount)
4075
{
4076
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4077 4078 4079 4080
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4081
static HRESULT WINAPI ITextDocument2Old_fnUnfreeze(ITextDocument2Old *iface, LONG *pCount)
4082
{
4083
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4084 4085 4086 4087
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4088
static HRESULT WINAPI ITextDocument2Old_fnBeginEditCollection(ITextDocument2Old *iface)
4089
{
4090
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4091 4092 4093 4094
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4095
static HRESULT WINAPI ITextDocument2Old_fnEndEditCollection(ITextDocument2Old *iface)
4096
{
4097
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4098 4099 4100 4101
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4102
static HRESULT WINAPI ITextDocument2Old_fnUndo(ITextDocument2Old *iface, LONG Count, LONG *prop)
4103
{
4104
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4105 4106 4107 4108
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4109
static HRESULT WINAPI ITextDocument2Old_fnRedo(ITextDocument2Old *iface, LONG Count, LONG *prop)
4110
{
4111
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4112 4113 4114 4115
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4116 4117 4118 4119 4120 4121 4122 4123
static HRESULT CreateITextRange(IRichEditOleImpl *reOle, LONG start, LONG end, ITextRange** ppRange)
{
    ITextRangeImpl *txtRge = heap_alloc(sizeof(ITextRangeImpl));

    if (!txtRge)
        return E_OUTOFMEMORY;
    txtRge->ITextRange_iface.lpVtbl = &trvt;
    txtRge->ref = 1;
4124
    txtRge->child.reole = reOle;
4125 4126
    txtRge->start = start;
    txtRge->end = end;
4127
    list_add_head(&reOle->rangelist, &txtRge->child.entry);
4128 4129 4130 4131
    *ppRange = &txtRge->ITextRange_iface;
    return S_OK;
}

4132 4133
static HRESULT WINAPI ITextDocument2Old_fnRange(ITextDocument2Old *iface, LONG cp1, LONG cp2,
                                                ITextRange **ppRange)
4134
{
4135
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4136 4137 4138 4139 4140

    TRACE("%p %p %d %d\n", This, ppRange, cp1, cp2);
    if (!ppRange)
        return E_INVALIDARG;

4141
    cp2range(This->editor, &cp1, &cp2);
4142
    return CreateITextRange(This, cp1, cp2, ppRange);
4143 4144
}

4145 4146
static HRESULT WINAPI ITextDocument2Old_fnRangeFromPoint(ITextDocument2Old *iface, LONG x, LONG y,
                                                         ITextRange **ppRange)
4147
{
4148
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);
4149 4150 4151 4152
    FIXME("stub %p\n",This);
    return E_NOTIMPL;
}

4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385
/* ITextDocument2Old methods */
static HRESULT WINAPI ITextDocument2Old_fnAttachMsgFilter(ITextDocument2Old *iface, IUnknown *filter)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, filter);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSetEffectColor(ITextDocument2Old *iface, LONG index, COLORREF cr)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d, 0x%x): stub\n", This, index, cr);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetEffectColor(ITextDocument2Old *iface, LONG index, COLORREF *cr)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d, %p): stub\n", This, index, cr);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetCaretType(ITextDocument2Old *iface, LONG *type)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, type);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSetCaretType(ITextDocument2Old *iface, LONG type)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d): stub\n", This, type);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetImmContext(ITextDocument2Old *iface, LONG *context)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, context);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnReleaseImmContext(ITextDocument2Old *iface, LONG context)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d): stub\n", This, context);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetPreferredFont(ITextDocument2Old *iface, LONG cp, LONG charrep,
                                                           LONG options, LONG current_charrep, LONG current_fontsize,
                                                           BSTR *bstr, LONG *pitch_family, LONG *new_fontsize)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d, %d, %d, %d, %d, %p, %p, %p): stub\n", This, cp, charrep, options, current_charrep,
          current_fontsize, bstr, pitch_family, new_fontsize);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetNotificationMode(ITextDocument2Old *iface, LONG *mode)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, mode);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSetNotificationMode(ITextDocument2Old *iface, LONG mode)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(0x%x): stub\n", This, mode);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetClientRect(ITextDocument2Old *iface, LONG type, LONG *left, LONG *top,
                                                        LONG *right, LONG *bottom)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d, %p, %p, %p, %p): stub\n", This, type, left, top, right, bottom);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetSelectionEx(ITextDocument2Old *iface, ITextSelection **selection)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, selection);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetWindow(ITextDocument2Old *iface, LONG *hwnd)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, hwnd);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetFEFlags(ITextDocument2Old *iface, LONG *flags)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%p): stub\n", This, flags);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnUpdateWindow(ITextDocument2Old *iface)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

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

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnCheckTextLimit(ITextDocument2Old *iface, LONG cch, LONG *exceed)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d, %p): stub\n", This, cch, exceed);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnIMEInProgress(ITextDocument2Old *iface, LONG mode)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(0x%x): stub\n", This, mode);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSysBeep(ITextDocument2Old *iface)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

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

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnUpdate(ITextDocument2Old *iface, LONG mode)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(0x%x): stub\n", This, mode);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnNotify(ITextDocument2Old *iface, LONG notify)
{
    IRichEditOleImpl *This = impl_from_ITextDocument2Old(iface);

    FIXME("(%p)->(%d): stub\n", This, notify);

    return E_NOTIMPL;
}

static const ITextDocument2OldVtbl tdvt = {
    ITextDocument2Old_fnQueryInterface,
    ITextDocument2Old_fnAddRef,
    ITextDocument2Old_fnRelease,
    ITextDocument2Old_fnGetTypeInfoCount,
    ITextDocument2Old_fnGetTypeInfo,
    ITextDocument2Old_fnGetIDsOfNames,
    ITextDocument2Old_fnInvoke,
    ITextDocument2Old_fnGetName,
    ITextDocument2Old_fnGetSelection,
    ITextDocument2Old_fnGetStoryCount,
    ITextDocument2Old_fnGetStoryRanges,
    ITextDocument2Old_fnGetSaved,
    ITextDocument2Old_fnSetSaved,
    ITextDocument2Old_fnGetDefaultTabStop,
    ITextDocument2Old_fnSetDefaultTabStop,
    ITextDocument2Old_fnNew,
    ITextDocument2Old_fnOpen,
    ITextDocument2Old_fnSave,
    ITextDocument2Old_fnFreeze,
    ITextDocument2Old_fnUnfreeze,
    ITextDocument2Old_fnBeginEditCollection,
    ITextDocument2Old_fnEndEditCollection,
    ITextDocument2Old_fnUndo,
    ITextDocument2Old_fnRedo,
    ITextDocument2Old_fnRange,
    ITextDocument2Old_fnRangeFromPoint,
    /* ITextDocument2Old methods */
    ITextDocument2Old_fnAttachMsgFilter,
    ITextDocument2Old_fnSetEffectColor,
    ITextDocument2Old_fnGetEffectColor,
    ITextDocument2Old_fnGetCaretType,
    ITextDocument2Old_fnSetCaretType,
    ITextDocument2Old_fnGetImmContext,
    ITextDocument2Old_fnReleaseImmContext,
    ITextDocument2Old_fnGetPreferredFont,
    ITextDocument2Old_fnGetNotificationMode,
    ITextDocument2Old_fnSetNotificationMode,
    ITextDocument2Old_fnGetClientRect,
    ITextDocument2Old_fnGetSelectionEx,
    ITextDocument2Old_fnGetWindow,
    ITextDocument2Old_fnGetFEFlags,
    ITextDocument2Old_fnUpdateWindow,
    ITextDocument2Old_fnCheckTextLimit,
    ITextDocument2Old_fnIMEInProgress,
    ITextDocument2Old_fnSysBeep,
    ITextDocument2Old_fnUpdate,
    ITextDocument2Old_fnNotify
4386 4387
};

4388
/* ITextSelection */
4389 4390 4391 4392 4393
static HRESULT WINAPI ITextSelection_fnQueryInterface(
    ITextSelection *me,
    REFIID riid,
    void **ppvObj)
{
4394 4395
    ITextSelectionImpl *This = impl_from_ITextSelection(me);

4396 4397 4398 4399 4400 4401 4402 4403 4404 4405
    *ppvObj = NULL;
    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IDispatch)
        || IsEqualGUID(riid, &IID_ITextRange)
        || IsEqualGUID(riid, &IID_ITextSelection))
    {
        *ppvObj = me;
        ITextSelection_AddRef(me);
        return S_OK;
    }
4406 4407 4408 4409 4410
    else if (IsEqualGUID(riid, &IID_Igetrichole))
    {
        *ppvObj = This->reOle;
        return S_OK;
    }
4411 4412 4413 4414

    return E_NOINTERFACE;
}

4415
static ULONG WINAPI ITextSelection_fnAddRef(ITextSelection *me)
4416
{
4417
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4418 4419 4420
    return InterlockedIncrement(&This->ref);
}

4421
static ULONG WINAPI ITextSelection_fnRelease(ITextSelection *me)
4422
{
4423
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4424 4425 4426 4427 4428 4429
    ULONG ref = InterlockedDecrement(&This->ref);
    if (ref == 0)
        heap_free(This);
    return ref;
}

4430
static HRESULT WINAPI ITextSelection_fnGetTypeInfoCount(ITextSelection *me, UINT *pctinfo)
4431
{
4432
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4433 4434 4435
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
4436 4437
}

4438
static HRESULT WINAPI ITextSelection_fnGetTypeInfo(ITextSelection *me, UINT iTInfo, LCID lcid,
4439 4440
    ITypeInfo **ppTInfo)
{
4441
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4442
    HRESULT hr;
4443

4444 4445 4446 4447 4448 4449
    TRACE("(%p)->(%u,%d,%p)\n", This, iTInfo, lcid, ppTInfo);

    hr = get_typeinfo(ITextSelection_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
4450 4451
}

4452 4453
static HRESULT WINAPI ITextSelection_fnGetIDsOfNames(ITextSelection *me, REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
4454
{
4455
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4456 4457
    ITypeInfo *ti;
    HRESULT hr;
4458

4459
    TRACE("(%p)->(%s, %p, %u, %d, %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid,
4460 4461 4462 4463 4464 4465
            rgDispId);

    hr = get_typeinfo(ITextSelection_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478
}

static HRESULT WINAPI ITextSelection_fnInvoke(
    ITextSelection *me,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS *pDispParams,
    VARIANT *pVarResult,
    EXCEPINFO *pExcepInfo,
    UINT *puArgErr)
{
4479 4480 4481 4482
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
    ITypeInfo *ti;
    HRESULT hr;

4483
    TRACE("(%p)->(%d, %s, %d, %u, %p, %p, %p, %p)\n", This, dispIdMember, debugstr_guid(riid), lcid,
4484 4485 4486 4487 4488 4489
            wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);

    hr = get_typeinfo(ITextSelection_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_Invoke(ti, me, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    return hr;
4490 4491 4492
}

/*** ITextRange methods ***/
4493
static HRESULT WINAPI ITextSelection_fnGetText(ITextSelection *me, BSTR *pbstr)
4494
{
4495
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4496 4497 4498 4499
    ME_Cursor *start = NULL, *end = NULL;
    int nChars, endOfs;
    BOOL bEOP;

4500 4501
    TRACE("(%p)->(%p)\n", This, pbstr);

4502 4503
    if (!This->reOle)
        return CO_E_RELEASED;
4504

4505 4506
    if (!pbstr)
        return E_INVALIDARG;
4507

4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525
    ME_GetSelection(This->reOle->editor, &start, &end);
    endOfs = ME_GetCursorOfs(end);
    nChars = endOfs - ME_GetCursorOfs(start);
    if (!nChars)
    {
        *pbstr = NULL;
        return S_OK;
    }

    *pbstr = SysAllocStringLen(NULL, nChars);
    if (!*pbstr)
        return E_OUTOFMEMORY;

    bEOP = (end->pRun->next->type == diTextEnd && endOfs > ME_GetTextLength(This->reOle->editor));
    ME_GetTextW(This->reOle->editor, *pbstr, nChars, start, nChars, FALSE, bEOP);
    TRACE("%s\n", wine_dbgstr_w(*pbstr));

    return S_OK;
4526 4527
}

4528
static HRESULT WINAPI ITextSelection_fnSetText(ITextSelection *me, BSTR str)
4529
{
4530
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4531 4532 4533 4534 4535
    ME_TextEditor *editor;
    int len, to, from;

    TRACE("(%p)->(%s)\n", This, debugstr_w(str));

4536 4537 4538
    if (!This->reOle)
        return CO_E_RELEASED;

4539
    editor = This->reOle->editor;
4540
    len = lstrlenW(str);
4541 4542 4543 4544 4545 4546 4547
    ME_GetSelectionOfs(editor, &from, &to);
    ME_ReplaceSel(editor, FALSE, str, len);

    if (len < to - from)
        textranges_update_ranges(This->reOle, from, len, RANGE_UPDATE_DELETE);

    return S_OK;
4548 4549
}

4550
static HRESULT WINAPI ITextSelection_fnGetChar(ITextSelection *me, LONG *pch)
4551
{
4552
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4553 4554
    ME_Cursor *start = NULL, *end = NULL;

4555 4556
    TRACE("(%p)->(%p)\n", This, pch);

4557 4558
    if (!This->reOle)
        return CO_E_RELEASED;
4559

4560 4561
    if (!pch)
        return E_INVALIDARG;
4562

4563 4564
    ME_GetSelection(This->reOle->editor, &start, &end);
    return range_GetChar(This->reOle->editor, start, pch);
4565 4566
}

4567
static HRESULT WINAPI ITextSelection_fnSetChar(ITextSelection *me, LONG ch)
4568
{
4569
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4570 4571 4572

    FIXME("(%p)->(%x): stub\n", This, ch);

4573 4574 4575 4576 4577 4578
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4579
static HRESULT WINAPI ITextSelection_fnGetDuplicate(ITextSelection *me, ITextRange **range)
4580
{
4581
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4582 4583 4584 4585
    LONG start, end;

    TRACE("(%p)->(%p)\n", This, range);

4586 4587 4588
    if (!This->reOle)
        return CO_E_RELEASED;

4589 4590 4591 4592 4593 4594
    if (!range)
        return E_INVALIDARG;

    ITextSelection_GetStart(me, &start);
    ITextSelection_GetEnd(me, &end);
    return CreateITextRange(This->reOle, start, end, range);
4595 4596
}

4597
static HRESULT WINAPI ITextSelection_fnGetFormattedText(ITextSelection *me, ITextRange **range)
4598
{
4599
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4600 4601 4602

    FIXME("(%p)->(%p): stub\n", This, range);

4603 4604 4605 4606 4607 4608
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4609
static HRESULT WINAPI ITextSelection_fnSetFormattedText(ITextSelection *me, ITextRange *range)
4610
{
4611
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4612 4613 4614

    FIXME("(%p)->(%p): stub\n", This, range);

4615 4616 4617 4618 4619 4620 4621
    if (!This->reOle)
        return CO_E_RELEASED;

    FIXME("not implemented\n");
    return E_NOTIMPL;
}

4622
static HRESULT WINAPI ITextSelection_fnGetStart(ITextSelection *me, LONG *pcpFirst)
4623
{
4624
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4625
    LONG lim;
4626 4627 4628

    TRACE("(%p)->(%p)\n", This, pcpFirst);

4629 4630 4631
    if (!This->reOle)
        return CO_E_RELEASED;

4632 4633 4634 4635
    if (!pcpFirst)
        return E_INVALIDARG;
    ME_GetSelectionOfs(This->reOle->editor, pcpFirst, &lim);
    return S_OK;
4636 4637
}

4638
static HRESULT WINAPI ITextSelection_fnSetStart(ITextSelection *me, LONG value)
4639
{
4640
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4641 4642 4643 4644 4645
    LONG start, end;
    HRESULT hr;

    TRACE("(%p)->(%d)\n", This, value);

4646 4647 4648
    if (!This->reOle)
        return CO_E_RELEASED;

4649 4650 4651
    ME_GetSelectionOfs(This->reOle->editor, &start, &end);
    hr = textrange_setstart(This->reOle, value, &start, &end);
    if (hr == S_OK)
4652
        set_selection(This->reOle->editor, start, end);
4653 4654

    return hr;
4655 4656
}

4657
static HRESULT WINAPI ITextSelection_fnGetEnd(ITextSelection *me, LONG *pcpLim)
4658
{
4659
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4660
    LONG first;
4661 4662 4663

    TRACE("(%p)->(%p)\n", This, pcpLim);

4664 4665 4666
    if (!This->reOle)
        return CO_E_RELEASED;

4667 4668 4669 4670
    if (!pcpLim)
        return E_INVALIDARG;
    ME_GetSelectionOfs(This->reOle->editor, &first, pcpLim);
    return S_OK;
4671 4672
}

4673
static HRESULT WINAPI ITextSelection_fnSetEnd(ITextSelection *me, LONG value)
4674
{
4675
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4676 4677 4678 4679 4680
    LONG start, end;
    HRESULT hr;

    TRACE("(%p)->(%d)\n", This, value);

4681 4682 4683
    if (!This->reOle)
        return CO_E_RELEASED;

4684 4685 4686
    ME_GetSelectionOfs(This->reOle->editor, &start, &end);
    hr = textrange_setend(This->reOle, value, &start, &end);
    if (hr == S_OK)
4687
        set_selection(This->reOle->editor, start, end);
4688 4689

    return hr;
4690 4691
}

4692
static HRESULT WINAPI ITextSelection_fnGetFont(ITextSelection *me, ITextFont **font)
4693
{
4694
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4695 4696
    ITextRange *range = NULL;
    HRESULT hr;
4697 4698 4699

    TRACE("(%p)->(%p)\n", This, font);

4700 4701 4702
    if (!This->reOle)
        return CO_E_RELEASED;

4703 4704 4705
    if (!font)
        return E_INVALIDARG;

4706 4707 4708 4709
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = create_textfont(range, NULL, font);
    ITextRange_Release(range);
    return hr;
4710 4711
}

4712
static HRESULT WINAPI ITextSelection_fnSetFont(ITextSelection *me, ITextFont *font)
4713
{
4714
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4715
    ITextRange *range = NULL;
4716 4717 4718 4719 4720 4721

    TRACE("(%p)->(%p)\n", This, font);

    if (!font)
        return E_INVALIDARG;

4722 4723 4724
    if (!This->reOle)
        return CO_E_RELEASED;

4725 4726 4727
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    textrange_set_font(range, font);
    ITextRange_Release(range);
4728
    return S_OK;
4729 4730
}

4731
static HRESULT WINAPI ITextSelection_fnGetPara(ITextSelection *me, ITextPara **para)
4732
{
4733
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4734 4735
    ITextRange *range = NULL;
    HRESULT hr;
4736 4737 4738

    TRACE("(%p)->(%p)\n", This, para);

4739 4740 4741
    if (!This->reOle)
        return CO_E_RELEASED;

4742 4743 4744
    if (!para)
        return E_INVALIDARG;

4745 4746 4747 4748
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = create_textpara(range, para);
    ITextRange_Release(range);
    return hr;
4749 4750
}

4751
static HRESULT WINAPI ITextSelection_fnSetPara(ITextSelection *me, ITextPara *para)
4752
{
4753
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4754 4755 4756

    FIXME("(%p)->(%p): stub\n", This, para);

4757 4758 4759 4760 4761 4762 4763
    if (!This->reOle)
        return CO_E_RELEASED;

    FIXME("not implemented\n");
    return E_NOTIMPL;
}

4764
static HRESULT WINAPI ITextSelection_fnGetStoryLength(ITextSelection *me, LONG *length)
4765
{
4766
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4767 4768 4769

    TRACE("(%p)->(%p)\n", This, length);

4770 4771 4772
    if (!This->reOle)
        return CO_E_RELEASED;

4773
    return textrange_get_storylength(This->reOle->editor, length);
4774 4775
}

4776
static HRESULT WINAPI ITextSelection_fnGetStoryType(ITextSelection *me, LONG *value)
4777
{
4778
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4779 4780 4781

    TRACE("(%p)->(%p)\n", This, value);

4782 4783 4784
    if (!This->reOle)
        return CO_E_RELEASED;

4785 4786 4787 4788 4789
    if (!value)
        return E_INVALIDARG;

    *value = tomUnknownStory;
    return S_OK;
4790 4791
}

4792
static HRESULT WINAPI ITextSelection_fnCollapse(ITextSelection *me, LONG bStart)
4793
{
4794
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4795 4796
    LONG start, end;
    HRESULT hres;
4797 4798 4799

    TRACE("(%p)->(%d)\n", This, bStart);

4800 4801 4802
    if (!This->reOle)
        return CO_E_RELEASED;

4803 4804 4805
    ME_GetSelectionOfs(This->reOle->editor, &start, &end);
    hres = range_Collapse(bStart, &start, &end);
    if (SUCCEEDED(hres))
4806
        set_selection(This->reOle->editor, start, end);
4807
    return hres;
4808 4809
}

4810
static HRESULT WINAPI ITextSelection_fnExpand(ITextSelection *me, LONG unit, LONG *delta)
4811
{
4812
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4813 4814
    ITextRange *range = NULL;
    HRESULT hr;
4815 4816 4817

    TRACE("(%p)->(%d %p)\n", This, unit, delta);

4818 4819 4820
    if (!This->reOle)
        return CO_E_RELEASED;

4821 4822 4823 4824
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = textrange_expand(range, unit, delta);
    ITextRange_Release(range);
    return hr;
4825 4826
}

4827
static HRESULT WINAPI ITextSelection_fnGetIndex(ITextSelection *me, LONG unit, LONG *index)
4828
{
4829
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4830 4831 4832

    FIXME("(%p)->(%d %p): stub\n", This, unit, index);

4833 4834 4835 4836 4837 4838
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4839 4840
static HRESULT WINAPI ITextSelection_fnSetIndex(ITextSelection *me, LONG unit, LONG index,
    LONG extend)
4841
{
4842
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4843 4844 4845

    FIXME("(%p)->(%d %d %d): stub\n", This, unit, index, extend);

4846 4847 4848 4849 4850 4851
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4852
static HRESULT WINAPI ITextSelection_fnSetRange(ITextSelection *me, LONG anchor, LONG active)
4853
{
4854
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4855 4856 4857

    FIXME("(%p)->(%d %d): stub\n", This, anchor, active);

4858 4859 4860 4861 4862 4863
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4864
static HRESULT WINAPI ITextSelection_fnInRange(ITextSelection *me, ITextRange *range, LONG *ret)
4865
{
4866
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4867 4868 4869 4870 4871 4872 4873 4874
    ITextSelection *selection = NULL;
    LONG start, end;

    TRACE("(%p)->(%p %p)\n", This, range, ret);

    if (ret)
        *ret = tomFalse;

4875 4876 4877
    if (!This->reOle)
        return CO_E_RELEASED;

4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888
    if (!range)
        return S_FALSE;

    ITextRange_QueryInterface(range, &IID_ITextSelection, (void**)&selection);
    if (!selection)
        return S_FALSE;
    ITextSelection_Release(selection);

    ITextSelection_GetStart(me, &start);
    ITextSelection_GetEnd(me, &end);
    return textrange_inrange(start, end, range, ret);
4889 4890
}

4891
static HRESULT WINAPI ITextSelection_fnInStory(ITextSelection *me, ITextRange *range, LONG *ret)
4892
{
4893
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4894 4895 4896

    FIXME("(%p)->(%p %p): stub\n", This, range, ret);

4897 4898 4899 4900 4901 4902
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4903
static HRESULT WINAPI ITextSelection_fnIsEqual(ITextSelection *me, ITextRange *range, LONG *ret)
4904
{
4905
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4906 4907 4908 4909 4910 4911 4912 4913
    ITextSelection *selection = NULL;
    LONG start, end;

    TRACE("(%p)->(%p %p)\n", This, range, ret);

    if (ret)
        *ret = tomFalse;

4914 4915 4916
    if (!This->reOle)
        return CO_E_RELEASED;

4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927
    if (!range)
        return S_FALSE;

    ITextRange_QueryInterface(range, &IID_ITextSelection, (void**)&selection);
    if (!selection)
        return S_FALSE;
    ITextSelection_Release(selection);

    ITextSelection_GetStart(me, &start);
    ITextSelection_GetEnd(me, &end);
    return textrange_isequal(start, end, range, ret);
4928 4929
}

4930
static HRESULT WINAPI ITextSelection_fnSelect(ITextSelection *me)
4931
{
4932
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4933 4934 4935

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

4936 4937 4938
    if (!This->reOle)
        return CO_E_RELEASED;

4939 4940
    /* nothing to do */
    return S_OK;
4941 4942
}

4943 4944
static HRESULT WINAPI ITextSelection_fnStartOf(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
4945
{
4946
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4947 4948 4949

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, extend, delta);

4950 4951 4952 4953 4954 4955
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4956 4957
static HRESULT WINAPI ITextSelection_fnEndOf(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
4958
{
4959
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4960 4961 4962

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, extend, delta);

4963 4964 4965 4966 4967 4968
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4969
static HRESULT WINAPI ITextSelection_fnMove(ITextSelection *me, LONG unit, LONG count, LONG *delta)
4970
{
4971
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4972 4973 4974

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, count, delta);

4975 4976 4977 4978 4979 4980
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4981 4982
static HRESULT WINAPI ITextSelection_fnMoveStart(ITextSelection *me, LONG unit, LONG count,
    LONG *delta)
4983
{
4984
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4985 4986 4987

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, count, delta);

4988 4989 4990 4991 4992 4993
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4994 4995
static HRESULT WINAPI ITextSelection_fnMoveEnd(ITextSelection *me, LONG unit, LONG count,
    LONG *delta)
4996
{
4997
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
4998 4999
    ITextRange *range = NULL;
    HRESULT hr;
5000

5001
    TRACE("(%p)->(%d %d %p)\n", This, unit, count, delta);
5002

5003 5004 5005
    if (!This->reOle)
        return CO_E_RELEASED;

5006 5007 5008 5009
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = textrange_moveend(range, unit, count, delta);
    ITextRange_Release(range);
    return hr;
5010 5011
}

5012 5013
static HRESULT WINAPI ITextSelection_fnMoveWhile(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5014
{
5015
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5016 5017 5018

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

5019 5020 5021 5022 5023 5024
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5025 5026
static HRESULT WINAPI ITextSelection_fnMoveStartWhile(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5027
{
5028
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5029 5030 5031

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

5032 5033 5034 5035 5036 5037
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5038 5039
static HRESULT WINAPI ITextSelection_fnMoveEndWhile(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5040
{
5041
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5042 5043 5044

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

5045 5046 5047 5048 5049 5050
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5051 5052
static HRESULT WINAPI ITextSelection_fnMoveUntil(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5053
{
5054
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5055 5056 5057

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

5058 5059 5060 5061 5062 5063
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5064 5065
static HRESULT WINAPI ITextSelection_fnMoveStartUntil(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5066
{
5067
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5068 5069 5070

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

5071 5072 5073 5074 5075 5076
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5077 5078
static HRESULT WINAPI ITextSelection_fnMoveEndUntil(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5079
{
5080
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5081 5082 5083

    FIXME("(%p)->(%s %d %p): stub\n", This, debugstr_variant(charset), count, delta);

5084 5085 5086 5087 5088 5089
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5090 5091
static HRESULT WINAPI ITextSelection_fnFindText(ITextSelection *me, BSTR text, LONG count, LONG flags,
    LONG *length)
5092
{
5093
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5094 5095 5096

    FIXME("(%p)->(%s %d %x %p): stub\n", This, debugstr_w(text), count, flags, length);

5097 5098 5099 5100 5101 5102 5103
    if (!This->reOle)
        return CO_E_RELEASED;

    FIXME("not implemented\n");
    return E_NOTIMPL;
}

5104 5105
static HRESULT WINAPI ITextSelection_fnFindTextStart(ITextSelection *me, BSTR text, LONG count,
    LONG flags, LONG *length)
5106
{
5107
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5108 5109 5110

    FIXME("(%p)->(%s %d %x %p): stub\n", This, debugstr_w(text), count, flags, length);

5111 5112 5113 5114 5115 5116
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5117 5118
static HRESULT WINAPI ITextSelection_fnFindTextEnd(ITextSelection *me, BSTR text, LONG count,
    LONG flags, LONG *length)
5119
{
5120
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5121 5122 5123

    FIXME("(%p)->(%s %d %x %p): stub\n", This, debugstr_w(text), count, flags, length);

5124 5125 5126 5127 5128 5129
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5130 5131
static HRESULT WINAPI ITextSelection_fnDelete(ITextSelection *me, LONG unit, LONG count,
    LONG *delta)
5132
{
5133
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5134 5135 5136

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, count, delta);

5137 5138 5139 5140 5141 5142
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5143
static HRESULT WINAPI ITextSelection_fnCut(ITextSelection *me, VARIANT *v)
5144
{
5145
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5146 5147 5148

    FIXME("(%p)->(%p): stub\n", This, v);

5149 5150 5151 5152 5153 5154
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5155
static HRESULT WINAPI ITextSelection_fnCopy(ITextSelection *me, VARIANT *v)
5156
{
5157
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5158 5159 5160

    FIXME("(%p)->(%p): stub\n", This, v);

5161 5162 5163 5164 5165 5166
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5167
static HRESULT WINAPI ITextSelection_fnPaste(ITextSelection *me, VARIANT *v, LONG format)
5168
{
5169
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5170 5171 5172

    FIXME("(%p)->(%s %x): stub\n", This, debugstr_variant(v), format);

5173 5174 5175 5176 5177 5178
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5179 5180
static HRESULT WINAPI ITextSelection_fnCanPaste(ITextSelection *me, VARIANT *v, LONG format,
    LONG *ret)
5181
{
5182
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5183 5184 5185

    FIXME("(%p)->(%s %x %p): stub\n", This, debugstr_variant(v), format, ret);

5186 5187 5188 5189 5190 5191
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5192
static HRESULT WINAPI ITextSelection_fnCanEdit(ITextSelection *me, LONG *ret)
5193
{
5194
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5195 5196 5197

    FIXME("(%p)->(%p): stub\n", This, ret);

5198 5199 5200 5201 5202 5203
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5204
static HRESULT WINAPI ITextSelection_fnChangeCase(ITextSelection *me, LONG type)
5205
{
5206
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5207 5208 5209

    FIXME("(%p)->(%d): stub\n", This, type);

5210 5211 5212 5213 5214 5215
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5216
static HRESULT WINAPI ITextSelection_fnGetPoint(ITextSelection *me, LONG type, LONG *cx, LONG *cy)
5217
{
5218
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5219 5220 5221

    FIXME("(%p)->(%d %p %p): stub\n", This, type, cx, cy);

5222 5223 5224 5225 5226 5227
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5228 5229
static HRESULT WINAPI ITextSelection_fnSetPoint(ITextSelection *me, LONG x, LONG y, LONG type,
    LONG extend)
5230
{
5231
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5232 5233 5234

    FIXME("(%p)->(%d %d %d %d): stub\n", This, x, y, type, extend);

5235 5236 5237 5238 5239 5240
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5241
static HRESULT WINAPI ITextSelection_fnScrollIntoView(ITextSelection *me, LONG value)
5242
{
5243
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5244 5245 5246

    FIXME("(%p)->(%d): stub\n", This, value);

5247 5248 5249 5250 5251 5252
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5253
static HRESULT WINAPI ITextSelection_fnGetEmbeddedObject(ITextSelection *me, IUnknown **ppv)
5254
{
5255
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5256 5257 5258

    FIXME("(%p)->(%p): stub\n", This, ppv);

5259 5260 5261 5262 5263 5264 5265
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

/*** ITextSelection methods ***/
5266
static HRESULT WINAPI ITextSelection_fnGetFlags(ITextSelection *me, LONG *flags)
5267
{
5268
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5269 5270 5271

    FIXME("(%p)->(%p): stub\n", This, flags);

5272 5273 5274 5275 5276 5277
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5278
static HRESULT WINAPI ITextSelection_fnSetFlags(ITextSelection *me, LONG flags)
5279
{
5280
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5281 5282 5283

    FIXME("(%p)->(%x): stub\n", This, flags);

5284 5285 5286 5287 5288 5289
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5290
static HRESULT WINAPI ITextSelection_fnGetType(ITextSelection *me, LONG *type)
5291
{
5292
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5293 5294 5295

    FIXME("(%p)->(%p): stub\n", This, type);

5296 5297 5298 5299 5300 5301
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5302 5303
static HRESULT WINAPI ITextSelection_fnMoveLeft(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5304
{
5305
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5306 5307 5308

    FIXME("(%p)->(%d %d %d %p): stub\n", This, unit, count, extend, delta);

5309 5310 5311 5312 5313 5314
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5315 5316
static HRESULT WINAPI ITextSelection_fnMoveRight(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5317
{
5318
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5319 5320 5321

    FIXME("(%p)->(%d %d %d %p): stub\n", This, unit, count, extend, delta);

5322 5323 5324 5325 5326 5327
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5328 5329
static HRESULT WINAPI ITextSelection_fnMoveUp(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5330
{
5331
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5332 5333 5334

    FIXME("(%p)->(%d %d %d %p): stub\n", This, unit, count, extend, delta);

5335 5336 5337 5338 5339 5340
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5341 5342
static HRESULT WINAPI ITextSelection_fnMoveDown(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5343
{
5344
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5345 5346 5347

    FIXME("(%p)->(%d %d %d %p): stub\n", This, unit, count, extend, delta);

5348 5349 5350 5351 5352 5353
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5354 5355
static HRESULT WINAPI ITextSelection_fnHomeKey(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
5356
{
5357
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5358 5359 5360

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, extend, delta);

5361 5362 5363 5364 5365 5366
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5367 5368
static HRESULT WINAPI ITextSelection_fnEndKey(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
5369
{
5370
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5371 5372 5373

    FIXME("(%p)->(%d %d %p): stub\n", This, unit, extend, delta);

5374 5375 5376 5377 5378 5379
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5380
static HRESULT WINAPI ITextSelection_fnTypeText(ITextSelection *me, BSTR text)
5381
{
5382
    ITextSelectionImpl *This = impl_from_ITextSelection(me);
5383 5384 5385

    FIXME("(%p)->(%s): stub\n", This, debugstr_w(text));

5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469
    if (!This->reOle)
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

static const ITextSelectionVtbl tsvt = {
    ITextSelection_fnQueryInterface,
    ITextSelection_fnAddRef,
    ITextSelection_fnRelease,
    ITextSelection_fnGetTypeInfoCount,
    ITextSelection_fnGetTypeInfo,
    ITextSelection_fnGetIDsOfNames,
    ITextSelection_fnInvoke,
    ITextSelection_fnGetText,
    ITextSelection_fnSetText,
    ITextSelection_fnGetChar,
    ITextSelection_fnSetChar,
    ITextSelection_fnGetDuplicate,
    ITextSelection_fnGetFormattedText,
    ITextSelection_fnSetFormattedText,
    ITextSelection_fnGetStart,
    ITextSelection_fnSetStart,
    ITextSelection_fnGetEnd,
    ITextSelection_fnSetEnd,
    ITextSelection_fnGetFont,
    ITextSelection_fnSetFont,
    ITextSelection_fnGetPara,
    ITextSelection_fnSetPara,
    ITextSelection_fnGetStoryLength,
    ITextSelection_fnGetStoryType,
    ITextSelection_fnCollapse,
    ITextSelection_fnExpand,
    ITextSelection_fnGetIndex,
    ITextSelection_fnSetIndex,
    ITextSelection_fnSetRange,
    ITextSelection_fnInRange,
    ITextSelection_fnInStory,
    ITextSelection_fnIsEqual,
    ITextSelection_fnSelect,
    ITextSelection_fnStartOf,
    ITextSelection_fnEndOf,
    ITextSelection_fnMove,
    ITextSelection_fnMoveStart,
    ITextSelection_fnMoveEnd,
    ITextSelection_fnMoveWhile,
    ITextSelection_fnMoveStartWhile,
    ITextSelection_fnMoveEndWhile,
    ITextSelection_fnMoveUntil,
    ITextSelection_fnMoveStartUntil,
    ITextSelection_fnMoveEndUntil,
    ITextSelection_fnFindText,
    ITextSelection_fnFindTextStart,
    ITextSelection_fnFindTextEnd,
    ITextSelection_fnDelete,
    ITextSelection_fnCut,
    ITextSelection_fnCopy,
    ITextSelection_fnPaste,
    ITextSelection_fnCanPaste,
    ITextSelection_fnCanEdit,
    ITextSelection_fnChangeCase,
    ITextSelection_fnGetPoint,
    ITextSelection_fnSetPoint,
    ITextSelection_fnScrollIntoView,
    ITextSelection_fnGetEmbeddedObject,
    ITextSelection_fnGetFlags,
    ITextSelection_fnSetFlags,
    ITextSelection_fnGetType,
    ITextSelection_fnMoveLeft,
    ITextSelection_fnMoveRight,
    ITextSelection_fnMoveUp,
    ITextSelection_fnMoveDown,
    ITextSelection_fnHomeKey,
    ITextSelection_fnEndKey,
    ITextSelection_fnTypeText
};

static ITextSelectionImpl *
CreateTextSelection(IRichEditOleImpl *reOle)
{
    ITextSelectionImpl *txtSel = heap_alloc(sizeof *txtSel);
    if (!txtSel)
        return NULL;

5470
    txtSel->ITextSelection_iface.lpVtbl = &tsvt;
5471 5472 5473 5474 5475
    txtSel->ref = 1;
    txtSel->reOle = reOle;
    return txtSel;
}

5476
LRESULT CreateIRichEditOle(IUnknown *outer_unk, ME_TextEditor *editor, LPVOID *ppvObj)
5477 5478 5479
{
    IRichEditOleImpl *reo;

5480
    reo = heap_alloc(sizeof(IRichEditOleImpl));
5481 5482 5483
    if (!reo)
        return 0;

5484
    reo->IUnknown_inner.lpVtbl = &reo_unk_vtbl;
5485
    reo->IRichEditOle_iface.lpVtbl = &revt;
5486
    reo->ITextDocument2Old_iface.lpVtbl = &tdvt;
5487
    reo->ref = 1;
5488
    reo->editor = editor;
5489
    reo->txtSel = NULL;
5490

5491
    TRACE("Created %p\n",reo);
5492
    list_init(&reo->rangelist);
5493
    list_init(&reo->clientsites);
5494 5495 5496 5497
    if (outer_unk)
        reo->outer_unk = outer_unk;
    else
        reo->outer_unk = &reo->IUnknown_inner;
5498
    *ppvObj = &reo->IUnknown_inner;
5499 5500 5501

    return 1;
}
5502

5503
static void convert_sizel(const ME_Context *c, const SIZEL* szl, SIZE* sz)
5504 5505 5506 5507 5508 5509
{
  /* sizel is in .01 millimeters, sz in pixels */
  sz->cx = MulDiv(szl->cx, c->dpi.cx, 2540);
  sz->cy = MulDiv(szl->cy, c->dpi.cy, 2540);
}

5510 5511 5512 5513 5514
/******************************************************************************
 * ME_GetOLEObjectSize
 *
 * Sets run extent for OLE objects.
 */
5515
void ME_GetOLEObjectSize(const ME_Context *c, ME_Run *run, SIZE *pSize)
5516 5517 5518 5519 5520 5521 5522 5523
{
  IDataObject*  ido;
  FORMATETC     fmt;
  STGMEDIUM     stgm;
  DIBSECTION    dibsect;
  ENHMETAHEADER emh;

  assert(run->nFlags & MERF_GRAPHICS);
5524
  assert(run->reobj);
5525

5526
  if (run->reobj->obj.sizel.cx != 0 || run->reobj->obj.sizel.cy != 0)
5527
  {
5528
    convert_sizel(c, &run->reobj->obj.sizel, pSize);
5529 5530 5531 5532 5533
    if (c->editor->nZoomNumerator != 0)
    {
      pSize->cx = MulDiv(pSize->cx, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
      pSize->cy = MulDiv(pSize->cy, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
    }
5534 5535 5536
    return;
  }

5537
  if (!run->reobj->obj.poleobj)
5538 5539 5540 5541 5542
  {
    pSize->cx = pSize->cy = 0;
    return;
  }

5543
  if (IOleObject_QueryInterface(run->reobj->obj.poleobj, &IID_IDataObject, (void**)&ido) != S_OK)
5544 5545 5546 5547 5548
  {
      FIXME("Query Interface IID_IDataObject failed!\n");
      pSize->cx = pSize->cy = 0;
      return;
  }
5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565
  fmt.cfFormat = CF_BITMAP;
  fmt.ptd = NULL;
  fmt.dwAspect = DVASPECT_CONTENT;
  fmt.lindex = -1;
  fmt.tymed = TYMED_GDI;
  if (IDataObject_GetData(ido, &fmt, &stgm) != S_OK)
  {
    fmt.cfFormat = CF_ENHMETAFILE;
    fmt.tymed = TYMED_ENHMF;
    if (IDataObject_GetData(ido, &fmt, &stgm) != S_OK)
    {
      FIXME("unsupported format\n");
      pSize->cx = pSize->cy = 0;
      IDataObject_Release(ido);
      return;
    }
  }
5566
  IDataObject_Release(ido);
5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583

  switch (stgm.tymed)
  {
  case TYMED_GDI:
    GetObjectW(stgm.u.hBitmap, sizeof(dibsect), &dibsect);
    pSize->cx = dibsect.dsBm.bmWidth;
    pSize->cy = dibsect.dsBm.bmHeight;
    break;
  case TYMED_ENHMF:
    GetEnhMetaFileHeader(stgm.u.hEnhMetaFile, sizeof(emh), &emh);
    pSize->cx = emh.rclBounds.right - emh.rclBounds.left;
    pSize->cy = emh.rclBounds.bottom - emh.rclBounds.top;
    break;
  default:
    FIXME("Unsupported tymed %d\n", stgm.tymed);
    break;
  }
5584
  ReleaseStgMedium(&stgm);
5585
  if (c->editor->nZoomNumerator != 0)
5586
  {
5587 5588
    pSize->cx = MulDiv(pSize->cx, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
    pSize->cy = MulDiv(pSize->cy, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
5589
  }
5590 5591
}

5592
void ME_DrawOLE(ME_Context *c, int x, int y, ME_Run *run, BOOL selected)
5593 5594 5595 5596 5597 5598 5599
{
  IDataObject*  ido;
  FORMATETC     fmt;
  STGMEDIUM     stgm;
  DIBSECTION    dibsect;
  ENHMETAHEADER emh;
  HDC           hMemDC;
5600
  SIZE          sz;
5601
  BOOL          has_size;
5602
  HBITMAP       old_bm;
5603
  RECT          rc;
5604 5605

  assert(run->nFlags & MERF_GRAPHICS);
5606 5607
  assert(run->reobj);
  if (IOleObject_QueryInterface(run->reobj->obj.poleobj, &IID_IDataObject, (void**)&ido) != S_OK)
5608 5609 5610 5611
  {
    FIXME("Couldn't get interface\n");
    return;
  }
5612
  has_size = run->reobj->obj.sizel.cx != 0 || run->reobj->obj.sizel.cy != 0;
5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628
  fmt.cfFormat = CF_BITMAP;
  fmt.ptd = NULL;
  fmt.dwAspect = DVASPECT_CONTENT;
  fmt.lindex = -1;
  fmt.tymed = TYMED_GDI;
  if (IDataObject_GetData(ido, &fmt, &stgm) != S_OK)
  {
    fmt.cfFormat = CF_ENHMETAFILE;
    fmt.tymed = TYMED_ENHMF;
    if (IDataObject_GetData(ido, &fmt, &stgm) != S_OK)
    {
      FIXME("Couldn't get storage medium\n");
      IDataObject_Release(ido);
      return;
    }
  }
5629 5630
  IDataObject_Release(ido);

5631 5632 5633 5634 5635
  switch (stgm.tymed)
  {
  case TYMED_GDI:
    GetObjectW(stgm.u.hBitmap, sizeof(dibsect), &dibsect);
    hMemDC = CreateCompatibleDC(c->hDC);
5636
    old_bm = SelectObject(hMemDC, stgm.u.hBitmap);
5637
    if (has_size)
5638
    {
5639
      convert_sizel(c, &run->reobj->obj.sizel, &sz);
5640
    } else {
5641 5642
      sz.cx = dibsect.dsBm.bmWidth;
      sz.cy = dibsect.dsBm.bmHeight;
5643 5644 5645 5646 5647
    }
    if (c->editor->nZoomNumerator != 0)
    {
      sz.cx = MulDiv(sz.cx, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
      sz.cy = MulDiv(sz.cy, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
5648
    }
5649 5650 5651
    StretchBlt(c->hDC, x, y - sz.cy, sz.cx, sz.cy,
               hMemDC, 0, 0, dibsect.dsBm.bmWidth, dibsect.dsBm.bmHeight, SRCCOPY);

5652
    SelectObject(hMemDC, old_bm);
5653
    DeleteDC(hMemDC);
5654 5655 5656
    break;
  case TYMED_ENHMF:
    GetEnhMetaFileHeader(stgm.u.hEnhMetaFile, sizeof(emh), &emh);
5657
    if (has_size)
5658
    {
5659
      convert_sizel(c, &run->reobj->obj.sizel, &sz);
5660
    } else {
5661 5662
      sz.cx = emh.rclBounds.right - emh.rclBounds.left;
      sz.cy = emh.rclBounds.bottom - emh.rclBounds.top;
5663
    }
5664
    if (c->editor->nZoomNumerator != 0)
5665
    {
5666 5667
      sz.cx = MulDiv(sz.cx, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
      sz.cy = MulDiv(sz.cy, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
5668
    }
5669

5670 5671 5672 5673 5674
    rc.left = x;
    rc.top = y - sz.cy;
    rc.right = x + sz.cx;
    rc.bottom = y;
    PlayEnhMetaFile(c->hDC, stgm.u.hEnhMetaFile, &rc);
5675 5676 5677
    break;
  default:
    FIXME("Unsupported tymed %d\n", stgm.tymed);
5678
    selected = FALSE;
5679 5680
    break;
  }
5681 5682
  ReleaseStgMedium(&stgm);

5683 5684
  if (selected && !c->editor->bHideSelection)
    PatBlt(c->hDC, x, y - sz.cy, sz.cx, sz.cy, DSTINVERT);
5685 5686
}

5687
void ME_DeleteReObject(struct re_object *reobj)
5688
{
5689 5690 5691 5692
    if (reobj->obj.poleobj)   IOleObject_Release(reobj->obj.poleobj);
    if (reobj->obj.pstg)      IStorage_Release(reobj->obj.pstg);
    if (reobj->obj.polesite)  IOleClientSite_Release(reobj->obj.polesite);
    heap_free(reobj);
5693 5694
}

5695
void ME_CopyReObject(REOBJECT *dst, const REOBJECT *src, DWORD flags)
5696 5697
{
    *dst = *src;
5698 5699 5700
    dst->poleobj = NULL;
    dst->pstg = NULL;
    dst->polesite = NULL;
5701

5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716
    if ((flags & REO_GETOBJ_POLEOBJ) && src->poleobj)
    {
        dst->poleobj = src->poleobj;
        IOleObject_AddRef(dst->poleobj);
    }
    if ((flags & REO_GETOBJ_PSTG) && src->pstg)
    {
        dst->pstg = src->pstg;
        IStorage_AddRef(dst->pstg);
    }
    if ((flags & REO_GETOBJ_POLESITE) && src->polesite)
    {
        dst->polesite = src->polesite;
        IOleClientSite_AddRef(dst->polesite);
    }
5717
}