richole.c 160 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
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)) {
85
        ERR("LoadRegTypeLib failed: %08lx\n", hr);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        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
        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))
        {
124
            ERR("GetTypeInfoOfGuid(%s) failed: %08lx\n", debugstr_guid(tid_ids[tid]), hr);
125 126 127 128 129 130 131 132 133 134 135
            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 IOleClientSiteImpl IOleClientSiteImpl;
140
typedef struct ITextRangeImpl ITextRangeImpl;
141

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
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,
167 168
    FONT_PROPID_LAST,
    FONT_PROPID_FIRST = FONT_ALLCAPS
169 170
};

171 172 173
static const DWORD textfont_prop_masks[][2] = {
    { CFM_ALLCAPS,     CFE_ALLCAPS },
    { CFM_ANIMATION },
174
    { CFM_BACKCOLOR,   CFE_AUTOBACKCOLOR },
175 176
    { CFM_BOLD,        CFE_BOLD },
    { CFM_EMBOSS,      CFE_EMBOSS },
177
    { CFM_COLOR,       CFE_AUTOCOLOR },
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
    { 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 }
196 197 198 199 200 201 202 203
};

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

204 205 206 207
enum range_update_op {
    RANGE_UPDATE_DELETE
};

208 209
struct reole_child {
    struct list entry;
210
    struct text_services *reole;
211 212
};

213
struct ITextRangeImpl {
214
    struct reole_child child;
215 216 217 218 219
    ITextRange ITextRange_iface;
    LONG ref;
    LONG start, end;
};

220 221 222 223 224
typedef struct ITextFontImpl {
    ITextFont ITextFont_iface;
    LONG ref;

    ITextRange *range;
225
    textfont_prop_val props[FONT_PROPID_LAST];
226
    BOOL get_cache_enabled;
227
    BOOL set_cache_enabled;
228 229
} ITextFontImpl;

230 231 232 233 234 235 236
typedef struct ITextParaImpl {
    ITextPara ITextPara_iface;
    LONG ref;

    ITextRange *range;
} ITextParaImpl;

237
struct IOleClientSiteImpl {
238
    struct reole_child child;
239
    IOleClientSite IOleClientSite_iface;
240
    IOleInPlaceSite IOleInPlaceSite_iface;
241 242 243
    LONG ref;
};

244
static inline struct text_services *impl_from_IRichEditOle( IRichEditOle *iface )
245
{
246
    return CONTAINING_RECORD( iface, struct text_services, IRichEditOle_iface );
247 248
}

249
static inline struct text_services *impl_from_ITextDocument2Old( ITextDocument2Old *iface )
250
{
251
    return CONTAINING_RECORD( iface, struct text_services, ITextDocument2Old_iface );
252
}
253

254 255 256 257 258
static inline IOleClientSiteImpl *impl_from_IOleInPlaceSite(IOleInPlaceSite *iface)
{
    return CONTAINING_RECORD(iface, IOleClientSiteImpl, IOleInPlaceSite_iface);
}

259 260 261 262 263
static inline ITextRangeImpl *impl_from_ITextRange(ITextRange *iface)
{
    return CONTAINING_RECORD(iface, ITextRangeImpl, ITextRange_iface);
}

264
static inline struct text_selection *impl_from_ITextSelection(ITextSelection *iface)
265
{
266
    return CONTAINING_RECORD(iface, struct text_selection, ITextSelection_iface);
267 268 269 270 271 272 273
}

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

274 275 276 277 278
static inline ITextParaImpl *impl_from_ITextPara(ITextPara *iface)
{
    return CONTAINING_RECORD(iface, ITextParaImpl, ITextPara_iface);
}

279
static HRESULT create_textfont(ITextRange*, const ITextFontImpl*, ITextFont**);
280
static HRESULT create_textpara(ITextRange*, ITextPara**);
281
static struct text_selection *text_selection_create( struct text_services * );
282

283 284 285 286 287 288 289 290 291
static HRESULT textrange_get_storylength(ME_TextEditor *editor, LONG *length)
{
    if (!length)
        return E_INVALIDARG;

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

292
static void textranges_update_ranges(struct text_services *services, LONG start, LONG end, enum range_update_op op)
293 294 295
{
    ITextRangeImpl *range;

296
    LIST_FOR_EACH_ENTRY(range, &services->rangelist, ITextRangeImpl, child.entry) {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        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);
        }
    }
}

321 322 323 324 325
static inline BOOL is_equal_textfont_prop_value(enum textfont_prop_id propid, textfont_prop_val *left,
    textfont_prop_val *right)
{
    switch (propid)
    {
326 327 328
    case FONT_ALLCAPS:
    case FONT_ANIMATION:
    case FONT_BACKCOLOR:
329
    case FONT_BOLD:
330
    case FONT_EMBOSS:
331
    case FONT_FORECOLOR:
332 333
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
334
    case FONT_ITALIC:
335
    case FONT_KERNING:
336
    case FONT_LANGID:
337 338 339 340
    case FONT_OUTLINE:
    case FONT_PROTECTED:
    case FONT_SHADOW:
    case FONT_SMALLCAPS:
341
    case FONT_STRIKETHROUGH:
342 343
    case FONT_SUBSCRIPT:
    case FONT_SUPERSCRIPT:
344
    case FONT_UNDERLINE:
345
    case FONT_WEIGHT:
346
        return left->l == right->l;
347
    case FONT_NAME:
348
        return !wcscmp(left->str, right->str);
349
    case FONT_POSITION:
350
    case FONT_SIZE:
351
    case FONT_SPACING:
352 353 354 355 356 357 358 359 360 361 362
       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)
    {
363 364 365
    case FONT_ALLCAPS:
    case FONT_ANIMATION:
    case FONT_BACKCOLOR:
366
    case FONT_BOLD:
367
    case FONT_EMBOSS:
368
    case FONT_FORECOLOR:
369 370
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
371
    case FONT_ITALIC:
372
    case FONT_KERNING:
373
    case FONT_LANGID:
374 375 376 377
    case FONT_OUTLINE:
    case FONT_PROTECTED:
    case FONT_SHADOW:
    case FONT_SMALLCAPS:
378
    case FONT_STRIKETHROUGH:
379 380
    case FONT_SUBSCRIPT:
    case FONT_SUPERSCRIPT:
381
    case FONT_UNDERLINE:
382
    case FONT_WEIGHT:
383 384
        v->l = tomUndefined;
        return;
385 386 387
    case FONT_NAME:
        v->str = NULL;
        return;
388
    case FONT_POSITION:
389
    case FONT_SIZE:
390
    case FONT_SPACING:
391 392 393 394 395 396 397 398 399
        v->f = tomUndefined;
        return;
    default:
        FIXME("unhandled font property %d\n", propid);
        v->l = tomUndefined;
        return;
    }
}

400 401 402 403 404
static inline FLOAT twips_to_points(LONG value)
{
    return value * 72.0 / 1440;
}

405 406 407 408 409
static inline FLOAT points_to_twips(FLOAT value)
{
    return value * 1440 / 72.0;
}

410
static HRESULT get_textfont_prop_for_pos(const struct text_services *services, int pos, enum textfont_prop_id propid,
411
    textfont_prop_val *value)
412 413 414 415 416 417
{
    ME_Cursor from, to;
    CHARFORMAT2W fmt;

    memset(&fmt, 0, sizeof(fmt));
    fmt.cbSize = sizeof(fmt);
418
    fmt.dwMask = textfont_prop_masks[propid][0];
419

420
    cursor_from_char_ofs( services->editor, pos, &from );
421
    to = from;
422 423
    ME_MoveCursorChars( services->editor, &to, 1, FALSE );
    ME_GetCharFormat( services->editor, &from, &to, &fmt );
424 425 426

    switch (propid)
    {
427
    case FONT_ALLCAPS:
428 429 430 431 432 433 434 435 436 437 438 439 440 441
    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;
442 443 444 445 446 447 448
        break;
    case FONT_ANIMATION:
        value->l = fmt.bAnimation;
        break;
    case FONT_BACKCOLOR:
        value->l = fmt.dwEffects & CFE_AUTOBACKCOLOR ? GetSysColor(COLOR_WINDOW) : fmt.crBackColor;
        break;
449 450 451
    case FONT_FORECOLOR:
        value->l = fmt.dwEffects & CFE_AUTOCOLOR ? GetSysColor(COLOR_WINDOWTEXT) : fmt.crTextColor;
        break;
452
    case FONT_KERNING:
453
        value->f = twips_to_points(fmt.wKerning);
454
        break;
455 456 457
    case FONT_LANGID:
        value->l = fmt.lcid;
        break;
458 459 460 461 462 463
    case FONT_NAME:
        /* this case is used exclusively by GetName() */
        value->str = SysAllocString(fmt.szFaceName);
        if (!value->str)
            return E_OUTOFMEMORY;
        break;
464
    case FONT_POSITION:
465
        value->f = twips_to_points(fmt.yOffset);
466
        break;
467
    case FONT_SIZE:
468
        value->f = twips_to_points(fmt.yHeight);
469
        break;
470 471 472 473 474 475
    case FONT_SPACING:
        value->f = fmt.sSpacing;
        break;
    case FONT_WEIGHT:
        value->l = fmt.wWeight;
        break;
476 477 478 479 480 481 482 483
    default:
        FIXME("unhandled font property %d\n", propid);
        return E_FAIL;
    }

    return S_OK;
}

484
static inline const struct text_services *get_range_reole(ITextRange *range)
485
{
486 487 488
    struct text_services *services = NULL;
    ITextRange_QueryInterface(range, &IID_Igetrichole, (void**)&services);
    return services;
489 490
}

491 492 493
static void textrange_set_font(ITextRange *range, ITextFont *font)
{
    CHARFORMAT2W fmt;
494
    HRESULT hr;
495 496 497 498 499
    LONG value;
    BSTR str;
    FLOAT f;

#define CHARFORMAT_SET_B_FIELD(mask, value) \
500
    if (hr == S_OK && value != tomUndefined) { \
501 502 503 504 505 506 507 508 509
        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;
510
    hr = ITextFont_GetAllCaps(font, &value);
511 512 513
    CHARFORMAT_SET_B_FIELD(ALLCAPS, value);

    value = tomUndefined;
514
    hr = ITextFont_GetBold(font, &value);
515 516 517
    CHARFORMAT_SET_B_FIELD(BOLD, value);

    value = tomUndefined;
518
    hr = ITextFont_GetEmboss(font, &value);
519 520 521
    CHARFORMAT_SET_B_FIELD(EMBOSS, value);

    value = tomUndefined;
522
    hr = ITextFont_GetHidden(font, &value);
523 524 525
    CHARFORMAT_SET_B_FIELD(HIDDEN, value);

    value = tomUndefined;
526
    hr = ITextFont_GetEngrave(font, &value);
527 528 529
    CHARFORMAT_SET_B_FIELD(IMPRINT, value);

    value = tomUndefined;
530
    hr = ITextFont_GetItalic(font, &value);
531 532 533
    CHARFORMAT_SET_B_FIELD(ITALIC, value);

    value = tomUndefined;
534
    hr = ITextFont_GetOutline(font, &value);
535 536 537
    CHARFORMAT_SET_B_FIELD(OUTLINE, value);

    value = tomUndefined;
538
    hr = ITextFont_GetProtected(font, &value);
539 540 541
    CHARFORMAT_SET_B_FIELD(PROTECTED, value);

    value = tomUndefined;
542
    hr = ITextFont_GetShadow(font, &value);
543 544 545
    CHARFORMAT_SET_B_FIELD(SHADOW, value);

    value = tomUndefined;
546
    hr = ITextFont_GetSmallCaps(font, &value);
547 548 549
    CHARFORMAT_SET_B_FIELD(SMALLCAPS, value);

    value = tomUndefined;
550
    hr = ITextFont_GetStrikeThrough(font, &value);
551 552 553
    CHARFORMAT_SET_B_FIELD(STRIKEOUT, value);

    value = tomUndefined;
554
    hr = ITextFont_GetSubscript(font, &value);
555 556 557
    CHARFORMAT_SET_B_FIELD(SUBSCRIPT, value);

    value = tomUndefined;
558
    hr = ITextFont_GetSuperscript(font, &value);
559 560 561
    CHARFORMAT_SET_B_FIELD(SUPERSCRIPT, value);

    value = tomUndefined;
562
    hr = ITextFont_GetUnderline(font, &value);
563 564 565 566 567
    CHARFORMAT_SET_B_FIELD(UNDERLINE, value);

#undef CHARFORMAT_SET_B_FIELD

    value = tomUndefined;
568 569
    hr = ITextFont_GetAnimation(font, &value);
    if (hr == S_OK && value != tomUndefined) {
570 571 572 573 574
        fmt.dwMask |= CFM_ANIMATION;
        fmt.bAnimation = value;
    }

    value = tomUndefined;
575 576
    hr = ITextFont_GetBackColor(font, &value);
    if (hr == S_OK && value != tomUndefined) {
577 578 579 580 581 582 583 584
        fmt.dwMask |= CFM_BACKCOLOR;
        if (value == tomAutoColor)
            fmt.dwEffects |= CFE_AUTOBACKCOLOR;
        else
            fmt.crBackColor = value;
    }

    value = tomUndefined;
585 586
    hr = ITextFont_GetForeColor(font, &value);
    if (hr == S_OK && value != tomUndefined) {
587 588 589 590 591 592 593 594
        fmt.dwMask |= CFM_COLOR;
        if (value == tomAutoColor)
            fmt.dwEffects |= CFE_AUTOCOLOR;
        else
            fmt.crTextColor = value;
    }

    value = tomUndefined;
595 596
    hr = ITextFont_GetKerning(font, &f);
    if (hr == S_OK && f != tomUndefined) {
597 598 599 600 601
        fmt.dwMask |= CFM_KERNING;
        fmt.wKerning = points_to_twips(f);
    }

    value = tomUndefined;
602 603
    hr = ITextFont_GetLanguageID(font, &value);
    if (hr == S_OK && value != tomUndefined) {
604 605 606 607 608 609
        fmt.dwMask |= CFM_LCID;
        fmt.lcid = value;
    }

    if (ITextFont_GetName(font, &str) == S_OK) {
        fmt.dwMask |= CFM_FACE;
610
        lstrcpynW(fmt.szFaceName, str, ARRAY_SIZE(fmt.szFaceName));
611 612 613
        SysFreeString(str);
    }

614 615
    hr = ITextFont_GetPosition(font, &f);
    if (hr == S_OK && f != tomUndefined) {
616 617 618 619
        fmt.dwMask |= CFM_OFFSET;
        fmt.yOffset = points_to_twips(f);
    }

620 621
    hr = ITextFont_GetSize(font, &f);
    if (hr == S_OK && f != tomUndefined) {
622 623 624 625
        fmt.dwMask |= CFM_SIZE;
        fmt.yHeight = points_to_twips(f);
    }

626 627
    hr = ITextFont_GetSpacing(font, &f);
    if (hr == S_OK && f != tomUndefined) {
628 629 630 631
        fmt.dwMask |= CFM_SPACING;
        fmt.sSpacing = f;
    }

632 633
    hr = ITextFont_GetWeight(font, &value);
    if (hr == S_OK && value != tomUndefined) {
634 635 636 637
        fmt.dwMask |= CFM_WEIGHT;
        fmt.wWeight = value;
    }

638 639
    if (fmt.dwMask)
    {
640
        const struct text_services *services = get_range_reole(range);
641 642 643 644 645 646
        ME_Cursor from, to;
        LONG start, end;

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

647 648 649
        cursor_from_char_ofs( services->editor, start, &from );
        cursor_from_char_ofs( services->editor, end, &to );
        ME_SetCharFormat( services->editor, &from, &to, &fmt );
650 651 652
    }
}

653
static HRESULT get_textfont_prop(const ITextFontImpl *font, enum textfont_prop_id propid, textfont_prop_val *value)
654
{
655
    const struct text_services *services;
656
    textfont_prop_val v;
657
    LONG start, end, i;
658 659
    HRESULT hr;

660
    /* when font is not attached to any range use cached values */
661
    if (!font->range || font->get_cache_enabled) {
662 663 664 665
        *value = font->props[propid];
        return S_OK;
    }

666
    if (!(services = get_range_reole(font->range)))
667 668
        return CO_E_RELEASED;

669 670
    init_textfont_prop_value(propid, value);

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

674
    /* iterate trough a range to see if property value is consistent */
675
    hr = get_textfont_prop_for_pos( services, start, propid, &v );
676 677 678
    if (FAILED(hr))
        return hr;

679
    for (i = start + 1; i < end; i++) {
680
        textfont_prop_val cur;
681

682
        hr = get_textfont_prop_for_pos( services, i, propid, &cur );
683 684 685
        if (FAILED(hr))
            return hr;

686
        if (!is_equal_textfont_prop_value(propid, &v, &cur))
687 688 689 690 691 692 693
            return S_OK;
    }

    *value = v;
    return S_OK;
}

694
static HRESULT get_textfont_propf(const ITextFontImpl *font, enum textfont_prop_id propid, FLOAT *value)
695 696 697 698 699 700 701
{
    textfont_prop_val v;
    HRESULT hr;

    if (!value)
        return E_INVALIDARG;

702
    hr = get_textfont_prop(font, propid, &v);
703 704 705 706
    *value = v.f;
    return hr;
}

707
static HRESULT get_textfont_propl(const ITextFontImpl *font, enum textfont_prop_id propid, LONG *value)
708 709 710 711 712 713 714
{
    textfont_prop_val v;
    HRESULT hr;

    if (!value)
        return E_INVALIDARG;

715
    hr = get_textfont_prop(font, propid, &v);
716 717 718 719
    *value = v.l;
    return hr;
}

720 721 722
/* 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)
{
723
    const struct text_services *services;
724 725 726 727 728 729
    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) {
730 731 732 733 734 735
        if (propid == FONT_NAME) {
            SysFreeString(font->props[propid].str);
            font->props[propid].str = SysAllocString(value->str);
        }
        else
            font->props[propid] = *value;
736 737 738
        return S_OK;
    }

739
    if (!(services = get_range_reole(font->range)))
740 741 742 743
        return CO_E_RELEASED;

    memset(&fmt, 0, sizeof(fmt));
    fmt.cbSize = sizeof(fmt);
744
    fmt.dwMask = textfont_prop_masks[propid][0];
745 746 747

    switch (propid)
    {
748 749 750 751 752
    case FONT_ALLCAPS:
    case FONT_BOLD:
    case FONT_EMBOSS:
    case FONT_HIDDEN:
    case FONT_ENGRAVE:
753
    case FONT_ITALIC:
754 755 756 757 758 759 760 761 762
    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;
763
        break;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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;
794
    case FONT_NAME:
795
        lstrcpynW(fmt.szFaceName, value->str, ARRAY_SIZE(fmt.szFaceName));
796
        break;
797 798 799 800 801 802 803 804
    default:
        FIXME("unhandled font property %d\n", propid);
        return E_FAIL;
    }

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

805 806 807
    cursor_from_char_ofs( services->editor, start, &from );
    cursor_from_char_ofs( services->editor, end, &to );
    ME_SetCharFormat( services->editor, &from, &to, &fmt );
808 809 810 811

    return S_OK;
}

812 813 814 815 816 817 818 819 820 821 822 823 824 825
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);
}

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
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;
    }
}

854 855
static HRESULT textfont_getname_from_range(ITextRange *range, BSTR *ret)
{
856
    const struct text_services *services;
857 858 859 860
    textfont_prop_val v;
    HRESULT hr;
    LONG start;

861
    if (!(services = get_range_reole( range )))
862 863 864
        return CO_E_RELEASED;

    ITextRange_GetStart(range, &start);
865
    hr = get_textfont_prop_for_pos( services, start, FONT_NAME, &v );
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    *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]);
    }
}

881 882 883 884 885 886 887 888 889 890 891
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:
892
        FIXME("unit %ld is not supported\n", unit);
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
        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;
}

910
static HRESULT WINAPI
911
IRichEditOle_fnQueryInterface(IRichEditOle *iface, REFIID riid, LPVOID *ppvObj)
912
{
913 914
    struct text_services *services = impl_from_IRichEditOle( iface );
    return IUnknown_QueryInterface( services->outer_unk, riid, ppvObj );
915 916 917
}

static ULONG WINAPI
918
IRichEditOle_fnAddRef(IRichEditOle *iface)
919
{
920 921
    struct text_services *services = impl_from_IRichEditOle( iface );
    return IUnknown_AddRef( services->outer_unk );
922 923 924
}

static ULONG WINAPI
925
IRichEditOle_fnRelease(IRichEditOle *iface)
926
{
927 928
    struct text_services *services = impl_from_IRichEditOle( iface );
    return IUnknown_Release( services->outer_unk );
929 930
}

931
static HRESULT WINAPI
932
IRichEditOle_fnActivateAs(IRichEditOle *iface, REFCLSID rclsid, REFCLSID rclsidAs)
933
{
934 935
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME( "stub %p\n", services );
936 937 938 939
    return E_NOTIMPL;
}

static HRESULT WINAPI
940
IRichEditOle_fnContextSensitiveHelp(IRichEditOle *iface, BOOL fEnterMode)
941
{
942 943
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME( "stub %p\n", services );
944 945 946 947
    return E_NOTIMPL;
}

static HRESULT WINAPI
948
IRichEditOle_fnConvertObject( IRichEditOle *iface, LONG iob, REFCLSID class, LPCSTR user_type )
949
{
950 951
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME( "stub %p\n", services );
952 953 954
    return E_NOTIMPL;
}

955 956 957 958 959
static inline IOleClientSiteImpl *impl_from_IOleClientSite(IOleClientSite *iface)
{
    return CONTAINING_RECORD(iface, IOleClientSiteImpl, IOleClientSite_iface);
}

960 961 962
static HRESULT WINAPI
IOleClientSite_fnQueryInterface(IOleClientSite *me, REFIID riid, LPVOID *ppvObj)
{
963
    IOleClientSiteImpl *This = impl_from_IOleClientSite(me);
964 965 966 967 968 969
    TRACE("%p %s\n", me, debugstr_guid(riid) );

    *ppvObj = NULL;
    if (IsEqualGUID(riid, &IID_IUnknown) ||
        IsEqualGUID(riid, &IID_IOleClientSite))
        *ppvObj = me;
970 971
    else if (IsEqualGUID(riid, &IID_IOleWindow) ||
             IsEqualGUID(riid, &IID_IOleInPlaceSite))
972
        *ppvObj = &This->IOleInPlaceSite_iface;
973 974 975 976 977 978 979 980 981 982
    if (*ppvObj)
    {
        IOleClientSite_AddRef(me);
        return S_OK;
    }
    FIXME("%p: unhandled interface %s\n", me, debugstr_guid(riid) );

    return E_NOINTERFACE;
}

983
static ULONG WINAPI IOleClientSite_fnAddRef(IOleClientSite *iface)
984
{
985
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
986
    ULONG ref = InterlockedIncrement(&This->ref);
987
    TRACE("(%p)->(%lu)\n", This, ref);
988
    return ref;
989 990
}

991
static ULONG WINAPI IOleClientSite_fnRelease(IOleClientSite *iface)
992
{
993
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
994
    ULONG ref = InterlockedDecrement(&This->ref);
995

996
    TRACE("(%p)->(%lu)\n", This, ref);
997 998 999 1000 1001 1002

    if (ref == 0) {
        if (This->child.reole) {
            list_remove(&This->child.entry);
            This->child.reole = NULL;
        }
1003
        heap_free(This);
1004
    }
1005 1006 1007
    return ref;
}

1008
static HRESULT WINAPI IOleClientSite_fnSaveObject(IOleClientSite *iface)
1009
{
1010
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1011
    if (!This->child.reole)
1012 1013
        return CO_E_RELEASED;

1014
    FIXME("stub %p\n", iface);
1015 1016 1017
    return E_NOTIMPL;
}

1018 1019
static HRESULT WINAPI IOleClientSite_fnGetMoniker(IOleClientSite *iface, DWORD dwAssign,
        DWORD dwWhichMoniker, IMoniker **ppmk)
1020
{
1021
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1022
    if (!This->child.reole)
1023 1024
        return CO_E_RELEASED;

1025
    FIXME("stub %p\n", iface);
1026 1027 1028
    return E_NOTIMPL;
}

1029 1030
static HRESULT WINAPI IOleClientSite_fnGetContainer(IOleClientSite *iface,
        IOleContainer **ppContainer)
1031
{
1032
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1033
    if (!This->child.reole)
1034 1035
        return CO_E_RELEASED;

1036
    FIXME("stub %p\n", iface);
1037 1038 1039
    return E_NOTIMPL;
}

1040
static HRESULT WINAPI IOleClientSite_fnShowObject(IOleClientSite *iface)
1041
{
1042
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1043
    if (!This->child.reole)
1044 1045
        return CO_E_RELEASED;

1046
    FIXME("stub %p\n", iface);
1047 1048 1049
    return E_NOTIMPL;
}

1050
static HRESULT WINAPI IOleClientSite_fnOnShowWindow(IOleClientSite *iface, BOOL fShow)
1051
{
1052
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1053
    if (!This->child.reole)
1054 1055
        return CO_E_RELEASED;

1056
    FIXME("stub %p\n", iface);
1057 1058 1059
    return E_NOTIMPL;
}

1060
static HRESULT WINAPI IOleClientSite_fnRequestNewObjectLayout(IOleClientSite *iface)
1061
{
1062
    IOleClientSiteImpl *This = impl_from_IOleClientSite(iface);
1063
    if (!This->child.reole)
1064 1065
        return CO_E_RELEASED;

1066
    FIXME("stub %p\n", iface);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    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
};

1082 1083
/* IOleInPlaceSite interface */
static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnQueryInterface(IOleInPlaceSite *iface, REFIID riid, void **ppvObj)
1084
{
1085
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1086 1087 1088
    return IOleClientSite_QueryInterface(&This->IOleClientSite_iface, riid, ppvObj);
}

1089
static ULONG STDMETHODCALLTYPE IOleInPlaceSite_fnAddRef(IOleInPlaceSite *iface)
1090
{
1091
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1092 1093 1094
    return IOleClientSite_AddRef(&This->IOleClientSite_iface);
}

1095
static ULONG STDMETHODCALLTYPE IOleInPlaceSite_fnRelease(IOleInPlaceSite *iface)
1096
{
1097
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1098 1099 1100
    return IOleClientSite_Release(&This->IOleClientSite_iface);
}

1101
static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnGetWindow( IOleInPlaceSite *iface, HWND *window )
1102
{
1103
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1104

1105
    TRACE( "(%p)->(%p)\n", This, window );
1106

1107 1108 1109
    if (!This->child.reole)
        return CO_E_RELEASED;

1110
    if (!window) return E_INVALIDARG;
1111

1112 1113
    if (!This->child.reole->editor->have_texthost2) return E_NOTIMPL;
    return ITextHost2_TxGetWindow( This->child.reole->editor->texthost, window );
1114 1115
}

1116 1117 1118
static HRESULT STDMETHODCALLTYPE IOleInPlaceSite_fnContextSensitiveHelp(IOleInPlaceSite *iface, BOOL fEnterMode)
{
    IOleClientSiteImpl *This = impl_from_IOleInPlaceSite(iface);
1119 1120
    FIXME("not implemented: (%p)->(%d)\n", This, fEnterMode);
    return E_NOTIMPL;
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
}

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);
1149
    FIXME("not implemented: (%p)->(%p %p %p %p %p)\n", This, ppFrame, ppDoc, lprcPosRect, lprcClipRect, lpFrameInfo);
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
    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
};

1214
static HRESULT CreateOleClientSite( struct text_services *services, IOleClientSite **ret )
1215 1216
{
    IOleClientSiteImpl *clientSite = heap_alloc(sizeof *clientSite);
1217

1218
    if (!clientSite)
1219
        return E_OUTOFMEMORY;
1220

1221
    clientSite->IOleClientSite_iface.lpVtbl = &ocst;
1222
    clientSite->IOleInPlaceSite_iface.lpVtbl = &olestvt;
1223
    clientSite->ref = 1;
1224 1225
    clientSite->child.reole = services;
    list_add_head( &services->clientsites, &clientSite->child.entry );
1226 1227 1228

    *ret = &clientSite->IOleClientSite_iface;
    return S_OK;
1229 1230
}

1231
static HRESULT WINAPI
1232
IRichEditOle_fnGetClientSite( IRichEditOle *iface, IOleClientSite **clientsite )
1233
{
1234
    struct text_services *services = impl_from_IRichEditOle( iface );
1235

1236
    TRACE("(%p)->(%p)\n", services, clientsite);
1237

1238
    if (!clientsite)
1239
        return E_INVALIDARG;
1240

1241
    return CreateOleClientSite( services, clientsite );
1242 1243 1244
}

static HRESULT WINAPI
1245
IRichEditOle_fnGetClipboardData(IRichEditOle *iface, CHARRANGE *lpchrg,
1246 1247
               DWORD reco, LPDATAOBJECT *lplpdataobj)
{
1248
    struct text_services *services = impl_from_IRichEditOle( iface );
1249 1250
    ME_Cursor start;
    int nChars;
1251

1252
    TRACE("(%p,%p,%ld)\n", services, lpchrg, reco);
1253 1254
    if(!lplpdataobj)
        return E_INVALIDARG;
1255 1256
    if(!lpchrg)
    {
1257 1258
        LONG nFrom, nTo;
        int nStartCur = ME_GetSelectionOfs( services->editor, &nFrom, &nTo );
1259
        start = services->editor->pCursors[nStartCur];
1260
        nChars = nTo - nFrom;
1261 1262 1263
    }
    else
    {
1264
        cursor_from_char_ofs( services->editor, lpchrg->cpMin, &start );
1265
        nChars = lpchrg->cpMax - lpchrg->cpMin;
1266
    }
1267
    return ME_GetDataObject( services->editor, &start, nChars, lplpdataobj );
1268 1269
}

1270
static LONG WINAPI IRichEditOle_fnGetLinkCount(IRichEditOle *iface)
1271
{
1272 1273
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1274 1275 1276 1277
    return E_NOTIMPL;
}

static HRESULT WINAPI
1278
IRichEditOle_fnGetObject(IRichEditOle *iface, LONG iob,
1279 1280
               REOBJECT *lpreobject, DWORD dwFlags)
{
1281
    struct text_services *services = impl_from_IRichEditOle( iface );
1282 1283 1284
    struct re_object *reobj = NULL;
    LONG count = 0;

1285
    TRACE("(%p)->(%lx, %p, %lx)\n", services, iob, lpreobject, dwFlags);
1286 1287 1288 1289 1290 1291 1292 1293

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

    if (iob == REO_IOB_USE_CP)
    {
        ME_Cursor cursor;

1294
        TRACE("character offset: %ld\n", lpreobject->cp);
1295
        cursor_from_char_ofs( services->editor, lpreobject->cp, &cursor );
1296
        if (!cursor.run->reobj)
1297 1298
            return E_INVALIDARG;
        else
1299
            reobj = cursor.run->reobj;
1300
    }
1301 1302 1303 1304
    else if (iob == REO_IOB_SELECTION)
    {
        ME_Cursor *from, *to;

1305
        ME_GetSelection(services->editor, &from, &to);
1306
        if (!from->run->reobj)
1307 1308
            return E_INVALIDARG;
        else
1309
            reobj = from->run->reobj;
1310
    }
1311 1312
    else
    {
1313
        if (iob < 0 || iob >= IRichEditOle_GetObjectCount( iface ))
1314
            return E_INVALIDARG;
1315
        LIST_FOR_EACH_ENTRY(reobj, &services->editor->reobj_list, struct re_object, entry)
1316 1317 1318 1319 1320 1321 1322
        {
            if (count == iob)
                break;
            count++;
        }
    }
    ME_CopyReObject(lpreobject, &reobj->obj, dwFlags);
1323
    lpreobject->cp = run_char_ofs( reobj->run, 0 );
1324
    return S_OK;
1325 1326 1327
}

static LONG WINAPI
1328
IRichEditOle_fnGetObjectCount( IRichEditOle *iface )
1329
{
1330 1331 1332
    struct text_services *services = impl_from_IRichEditOle( iface );
    TRACE("(%p)\n", services);
    return list_count( &services->editor->reobj_list );
1333 1334 1335
}

static HRESULT WINAPI
1336
IRichEditOle_fnHandsOffStorage(IRichEditOle *iface, LONG iob)
1337
{
1338 1339
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1340 1341 1342 1343
    return E_NOTIMPL;
}

static HRESULT WINAPI
1344
IRichEditOle_fnImportDataObject(IRichEditOle *iface, LPDATAOBJECT lpdataobj,
1345 1346
               CLIPFORMAT cf, HGLOBAL hMetaPict)
{
1347 1348
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1349 1350 1351 1352
    return E_NOTIMPL;
}

static HRESULT WINAPI
1353
IRichEditOle_fnInPlaceDeactivate(IRichEditOle *iface)
1354
{
1355 1356
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1357 1358 1359 1360
    return E_NOTIMPL;
}

static HRESULT WINAPI
1361
IRichEditOle_fnInsertObject(IRichEditOle *iface, REOBJECT *reo)
1362
{
1363
    struct text_services *services = impl_from_IRichEditOle( iface );
1364
    HRESULT hr;
1365

1366
    TRACE("(%p,%p)\n", services, reo);
1367

1368 1369 1370
    if (!reo)
        return E_INVALIDARG;

1371 1372
    if (reo->cbStruct < sizeof(*reo)) return STG_E_INVALIDPARAMETER;

1373 1374 1375 1376
    hr = editor_insert_oleobj(services->editor, reo);
    if (hr != S_OK)
        return hr;

1377 1378
    ME_CommitUndo(services->editor);
    ME_UpdateRepaint(services->editor, FALSE);
1379
    return S_OK;
1380 1381
}

1382
static HRESULT WINAPI IRichEditOle_fnSaveCompleted(IRichEditOle *iface, LONG iob,
1383 1384
               LPSTORAGE lpstg)
{
1385 1386
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1387 1388 1389 1390
    return E_NOTIMPL;
}

static HRESULT WINAPI
1391
IRichEditOle_fnSetDvaspect(IRichEditOle *iface, LONG iob, DWORD dvaspect)
1392
{
1393 1394
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1395 1396 1397
    return E_NOTIMPL;
}

1398
static HRESULT WINAPI IRichEditOle_fnSetHostNames(IRichEditOle *iface,
1399 1400
               LPCSTR lpstrContainerApp, LPCSTR lpstrContainerObj)
{
1401 1402
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p %s %s\n", services, lpstrContainerApp, lpstrContainerObj);
1403 1404 1405 1406
    return E_NOTIMPL;
}

static HRESULT WINAPI
1407
IRichEditOle_fnSetLinkAvailable(IRichEditOle *iface, LONG iob, BOOL fAvailable)
1408
{
1409 1410
    struct text_services *services = impl_from_IRichEditOle( iface );
    FIXME("stub %p\n", services);
1411 1412 1413
    return E_NOTIMPL;
}

1414 1415
const IRichEditOleVtbl re_ole_vtbl =
{
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
    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
};

1437 1438 1439
/* ITextRange interface */
static HRESULT WINAPI ITextRange_fnQueryInterface(ITextRange *me, REFIID riid, void **ppvObj)
{
1440 1441
    ITextRangeImpl *This = impl_from_ITextRange(me);

1442 1443 1444 1445 1446 1447 1448 1449 1450
    *ppvObj = NULL;
    if (IsEqualGUID(riid, &IID_IUnknown)
        || IsEqualGUID(riid, &IID_IDispatch)
        || IsEqualGUID(riid, &IID_ITextRange))
    {
        *ppvObj = me;
        ITextRange_AddRef(me);
        return S_OK;
    }
1451 1452
    else if (IsEqualGUID(riid, &IID_Igetrichole))
    {
1453
        *ppvObj = This->child.reole;
1454 1455
        return S_OK;
    }
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

    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);

1471
    TRACE ("%p ref=%lu\n", This, ref);
1472 1473
    if (ref == 0)
    {
1474
        if (This->child.reole)
1475
        {
1476 1477
            list_remove(&This->child.entry);
            This->child.reole = NULL;
1478
        }
1479 1480 1481 1482 1483 1484 1485 1486
        heap_free(This);
    }
    return ref;
}

static HRESULT WINAPI ITextRange_fnGetTypeInfoCount(ITextRange *me, UINT *pctinfo)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1487 1488 1489
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
1490 1491 1492 1493 1494 1495
}

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

1498
    TRACE("(%p)->(%u,%ld,%p)\n", This, iTInfo, lcid, ppTInfo);
1499 1500 1501 1502 1503

    hr = get_typeinfo(ITextRange_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
1504 1505 1506 1507 1508 1509
}

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

1513
    TRACE("(%p)->(%s, %p, %u, %ld, %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid,
1514 1515 1516 1517 1518 1519
            rgDispId);

    hr = get_typeinfo(ITextRange_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
1520 1521 1522 1523 1524 1525 1526 1527
}

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);
1528 1529
    ITypeInfo *ti;
    HRESULT hr;
1530

1531
    TRACE("(%p)->(%ld, %s, %ld, %u, %p, %p, %p, %p)\n", This, dispIdMember, debugstr_guid(riid),
1532
            lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1533 1534 1535 1536 1537

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

1540
static HRESULT WINAPI ITextRange_fnGetText(ITextRange *me, BSTR *str)
1541 1542
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1543
    ME_TextEditor *editor;
1544 1545 1546
    ME_Cursor start, end;
    int length;
    BOOL bEOP;
1547

1548
    TRACE("(%p)->(%p)\n", This, str);
1549

1550
    if (!This->child.reole)
1551 1552
        return CO_E_RELEASED;

1553
    if (!str)
1554 1555
        return E_INVALIDARG;

1556 1557 1558 1559 1560 1561
    /* return early for degenerate range */
    if (This->start == This->end) {
        *str = NULL;
        return S_OK;
    }

1562
    editor = This->child.reole->editor;
1563 1564
    cursor_from_char_ofs( editor, This->start, &start );
    cursor_from_char_ofs( editor, This->end, &end );
1565 1566 1567 1568 1569 1570

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

1571
    bEOP = (!para_next( para_next( end.para )) && This->end > ME_GetTextLength(editor));
1572
    ME_GetTextW(editor, *str, length, &start, length, FALSE, bEOP);
1573
    return S_OK;
1574 1575
}

1576
static HRESULT WINAPI ITextRange_fnSetText(ITextRange *me, BSTR str)
1577 1578
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1579 1580 1581 1582 1583 1584 1585
    ME_TextEditor *editor;
    ME_Cursor cursor;
    ME_Style *style;
    int len;

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

1586
    if (!This->child.reole)
1587 1588
        return CO_E_RELEASED;

1589
    editor = This->child.reole->editor;
1590 1591

    /* delete only where's something to delete */
1592 1593 1594
    if (This->start != This->end)
    {
        cursor_from_char_ofs( editor, This->start, &cursor );
1595 1596 1597
        ME_InternalDeleteText(editor, &cursor, This->end - This->start, FALSE);
    }

1598 1599
    if (!str || !*str)
    {
1600
        /* will update this range as well */
1601
        textranges_update_ranges(This->child.reole, This->start, This->end, RANGE_UPDATE_DELETE);
1602 1603 1604 1605
        return S_OK;
    }

    /* it's safer not to rely on stored BSTR length */
1606
    len = lstrlenW(str);
1607
    cursor = editor->pCursors[0];
1608
    cursor_from_char_ofs( editor, This->start, &editor->pCursors[0] );
1609
    style = style_get_insert_style( editor, editor->pCursors );
1610 1611 1612 1613 1614
    ME_InsertTextFromCursor(editor, 0, str, len, style);
    ME_ReleaseStyle(style);
    editor->pCursors[0] = cursor;

    if (len < This->end - This->start)
1615
        textranges_update_ranges(This->child.reole, This->start + len, This->end, RANGE_UPDATE_DELETE);
1616 1617 1618 1619
    else
        This->end = len - This->start;

    return S_OK;
1620 1621
}

1622 1623 1624 1625
static HRESULT range_GetChar(ME_TextEditor *editor, ME_Cursor *cursor, LONG *pch)
{
    WCHAR wch[2];

1626
    ME_GetTextW(editor, wch, 1, cursor, 1, FALSE, !para_next( para_next( cursor->para ) ));
1627 1628 1629 1630 1631
    *pch = wch[0];

    return S_OK;
}

1632 1633 1634
static HRESULT WINAPI ITextRange_fnGetChar(ITextRange *me, LONG *pch)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1635
    ME_TextEditor *editor;
1636 1637
    ME_Cursor cursor;

1638 1639
    TRACE("(%p)->(%p)\n", This, pch);

1640
    if (!This->child.reole)
1641
        return CO_E_RELEASED;
1642

1643 1644
    if (!pch)
        return E_INVALIDARG;
1645

1646
    editor = This->child.reole->editor;
1647
    cursor_from_char_ofs( editor, This->start, &cursor );
1648
    return range_GetChar(editor, &cursor, pch);
1649 1650 1651 1652 1653
}

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

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

1657
    if (!This->child.reole)
1658 1659 1660 1661 1662
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1663
static HRESULT CreateITextRange(struct text_services *services, LONG start, LONG end, ITextRange** ppRange);
1664

1665 1666 1667
static HRESULT WINAPI ITextRange_fnGetDuplicate(ITextRange *me, ITextRange **ppRange)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1668 1669 1670 1671

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

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

1674 1675 1676
    if (!ppRange)
        return E_INVALIDARG;

1677
    return CreateITextRange(This->child.reole, This->start, This->end, ppRange);
1678 1679
}

1680
static HRESULT WINAPI ITextRange_fnGetFormattedText(ITextRange *me, ITextRange **range)
1681 1682
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1683 1684 1685

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

1686
    if (!This->child.reole)
1687 1688 1689 1690 1691
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1692
static HRESULT WINAPI ITextRange_fnSetFormattedText(ITextRange *me, ITextRange *range)
1693 1694
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1695 1696 1697

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

1698
    if (!This->child.reole)
1699 1700 1701 1702 1703
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1704
static HRESULT WINAPI ITextRange_fnGetStart(ITextRange *me, LONG *start)
1705 1706
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1707 1708 1709

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

1710
    if (!This->child.reole)
1711 1712
        return CO_E_RELEASED;

1713
    if (!start)
1714
        return E_INVALIDARG;
1715 1716

    *start = This->start;
1717
    return S_OK;
1718 1719
}

1720
static HRESULT textrange_setstart(const struct text_services *services, LONG value, LONG *start, LONG *end)
1721
{
1722 1723
    int len;

1724 1725
    if (value < 0)
        value = 0;
1726

1727
    if (value == *start)
1728 1729
        return S_FALSE;

1730 1731
    if (value <= *end) {
        *start = value;
1732 1733 1734
        return S_OK;
    }

1735
    len = ME_GetTextLength(services->editor);
1736
    *start = *end = value > len ? len : value;
1737
    return S_OK;
1738 1739
}

1740
static HRESULT WINAPI ITextRange_fnSetStart(ITextRange *me, LONG value)
1741 1742
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1743

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

1746
    if (!This->child.reole)
1747 1748
        return CO_E_RELEASED;

1749
    return textrange_setstart(This->child.reole, value, &This->start, &This->end);
1750 1751
}

1752
static HRESULT WINAPI ITextRange_fnGetEnd(ITextRange *me, LONG *end)
1753 1754
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1755

1756
    TRACE("(%p)->(%p)\n", This, end);
1757

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

1761 1762 1763 1764 1765 1766 1767
    if (!end)
        return E_INVALIDARG;

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

1768
static HRESULT textrange_setend(const struct text_services *services, LONG value, LONG *start, LONG *end)
1769 1770 1771 1772
{
    int len;

    if (value == *end)
1773 1774
        return S_FALSE;

1775 1776
    if (value < *start) {
        *start = *end = max(0, value);
1777 1778 1779
        return S_OK;
    }

1780
    len = ME_GetTextLength( services->editor );
1781
    *end = value > len ? len + 1 : value;
1782
    return S_OK;
1783 1784
}

1785 1786 1787 1788
static HRESULT WINAPI ITextRange_fnSetEnd(ITextRange *me, LONG value)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);

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

1791
    if (!This->child.reole)
1792 1793
        return CO_E_RELEASED;

1794
    return textrange_setend(This->child.reole, value, &This->start, &This->end);
1795 1796
}

1797
static HRESULT WINAPI ITextRange_fnGetFont(ITextRange *me, ITextFont **font)
1798 1799
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1800 1801 1802

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

1803
    if (!This->child.reole)
1804 1805
        return CO_E_RELEASED;

1806 1807 1808
    if (!font)
        return E_INVALIDARG;

1809
    return create_textfont(me, NULL, font);
1810 1811
}

1812
static HRESULT WINAPI ITextRange_fnSetFont(ITextRange *me, ITextFont *font)
1813 1814
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1815 1816 1817 1818 1819 1820

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

    if (!font)
        return E_INVALIDARG;

1821
    if (!This->child.reole)
1822 1823
        return CO_E_RELEASED;

1824 1825
    textrange_set_font(me, font);
    return S_OK;
1826 1827
}

1828
static HRESULT WINAPI ITextRange_fnGetPara(ITextRange *me, ITextPara **para)
1829 1830
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1831 1832 1833

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

1834
    if (!This->child.reole)
1835 1836
        return CO_E_RELEASED;

1837 1838 1839 1840
    if (!para)
        return E_INVALIDARG;

    return create_textpara(me, para);
1841 1842
}

1843
static HRESULT WINAPI ITextRange_fnSetPara(ITextRange *me, ITextPara *para)
1844 1845
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1846

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

1849
    if (!This->child.reole)
1850 1851 1852 1853 1854
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1855
static HRESULT WINAPI ITextRange_fnGetStoryLength(ITextRange *me, LONG *length)
1856 1857
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1858 1859 1860

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

1861
    if (!This->child.reole)
1862 1863
        return CO_E_RELEASED;

1864
    return textrange_get_storylength(This->child.reole->editor, length);
1865 1866
}

1867
static HRESULT WINAPI ITextRange_fnGetStoryType(ITextRange *me, LONG *value)
1868 1869
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1870 1871 1872

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

1873
    if (!This->child.reole)
1874 1875
        return CO_E_RELEASED;

1876 1877 1878 1879 1880
    if (!value)
        return E_INVALIDARG;

    *value = tomUnknownStory;
    return S_OK;
1881 1882
}

1883 1884 1885 1886 1887
static HRESULT range_Collapse(LONG bStart, LONG *start, LONG *end)
{
  if (*end == *start)
      return S_FALSE;

1888
  if (bStart == tomEnd)
1889 1890 1891 1892 1893 1894
      *start = *end;
  else
      *end = *start;
  return S_OK;
}

1895 1896 1897
static HRESULT WINAPI ITextRange_fnCollapse(ITextRange *me, LONG bStart)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1898

1899
    TRACE("(%p)->(%ld)\n", This, bStart);
1900 1901

    if (!This->child.reole)
1902 1903
        return CO_E_RELEASED;

1904
    return range_Collapse(bStart, &This->start, &This->end);
1905 1906
}

1907
static HRESULT WINAPI ITextRange_fnExpand(ITextRange *me, LONG unit, LONG *delta)
1908 1909
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1910

1911
    TRACE("(%p)->(%ld %p)\n", This, unit, delta);
1912

1913
    if (!This->child.reole)
1914 1915
        return CO_E_RELEASED;

1916
    return textrange_expand(me, unit, delta);
1917 1918
}

1919
static HRESULT WINAPI ITextRange_fnGetIndex(ITextRange *me, LONG unit, LONG *index)
1920 1921
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1922

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

1925
    if (!This->child.reole)
1926 1927 1928 1929 1930
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1931 1932
static HRESULT WINAPI ITextRange_fnSetIndex(ITextRange *me, LONG unit, LONG index,
                                            LONG extend)
1933 1934
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1935

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

1938
    if (!This->child.reole)
1939 1940 1941 1942 1943
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
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;
}

1962
static HRESULT WINAPI ITextRange_fnSetRange(ITextRange *me, LONG anchor, LONG active)
1963 1964
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1965

1966
    TRACE("(%p)->(%ld %ld)\n", This, anchor, active);
1967

1968
    if (!This->child.reole)
1969 1970
        return CO_E_RELEASED;

1971 1972 1973 1974 1975 1976 1977
    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;
1978 1979
}

1980 1981 1982 1983 1984 1985 1986
static HRESULT textrange_inrange(LONG start, LONG end, ITextRange *range, LONG *ret)
{
    LONG from, to, v;

    if (!ret)
        ret = &v;

1987 1988 1989 1990 1991
    if (FAILED(ITextRange_GetStart(range, &from)) || FAILED(ITextRange_GetEnd(range, &to))) {
        *ret = tomFalse;
    }
    else
        *ret = (start >= from && end <= to) ? tomTrue : tomFalse;
1992 1993 1994 1995
    return *ret == tomTrue ? S_OK : S_FALSE;
}

static HRESULT WINAPI ITextRange_fnInRange(ITextRange *me, ITextRange *range, LONG *ret)
1996 1997
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
1998 1999 2000 2001 2002 2003

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

    if (ret)
        *ret = tomFalse;

2004
    if (!This->child.reole)
2005 2006
        return CO_E_RELEASED;

2007 2008 2009 2010
    if (!range)
        return S_FALSE;

    return textrange_inrange(This->start, This->end, range, ret);
2011 2012
}

2013
static HRESULT WINAPI ITextRange_fnInStory(ITextRange *me, ITextRange *pRange, LONG *ret)
2014 2015
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2016 2017 2018

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

2019
    if (!This->child.reole)
2020 2021 2022 2023 2024
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2025 2026 2027 2028 2029 2030 2031
static HRESULT textrange_isequal(LONG start, LONG end, ITextRange *range, LONG *ret)
{
    LONG from, to, v;

    if (!ret)
        ret = &v;

2032 2033 2034 2035 2036
    if (FAILED(ITextRange_GetStart(range, &from)) || FAILED(ITextRange_GetEnd(range, &to))) {
        *ret = tomFalse;
    }
    else
        *ret = (start == from && end == to) ? tomTrue : tomFalse;
2037 2038 2039 2040
    return *ret == tomTrue ? S_OK : S_FALSE;
}

static HRESULT WINAPI ITextRange_fnIsEqual(ITextRange *me, ITextRange *range, LONG *ret)
2041 2042
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2043 2044 2045 2046 2047 2048

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

    if (ret)
        *ret = tomFalse;

2049
    if (!This->child.reole)
2050 2051
        return CO_E_RELEASED;

2052 2053 2054 2055
    if (!range)
        return S_FALSE;

    return textrange_isequal(This->start, This->end, range, ret);
2056 2057 2058 2059 2060
}

static HRESULT WINAPI ITextRange_fnSelect(ITextRange *me)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2061 2062 2063

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

2064
    if (!This->child.reole)
2065 2066
        return CO_E_RELEASED;

2067
    set_selection(This->child.reole->editor, This->start, This->end);
2068
    return S_OK;
2069 2070
}

2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
static HRESULT textrange_startof(ITextRange *range, LONG unit, LONG extend, LONG *delta)
{
    HRESULT hr;
    LONG start, end;
    LONG moved;

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

    switch (unit)
    {
    case tomCharacter:
    {
        moved = 0;
        if (extend == tomMove) {
            if (start != end) {
                ITextRange_SetEnd(range, start);
                moved = -1;
            }
        }
        if (delta)
            *delta = moved;
        hr = moved ? S_OK : S_FALSE;
        break;
    }
    default:
2097
        FIXME("unit %ld is not supported\n", unit);
2098 2099 2100 2101 2102
        return E_NOTIMPL;
    }
    return hr;
}

2103 2104
static HRESULT WINAPI ITextRange_fnStartOf(ITextRange *me, LONG unit, LONG extend,
                                           LONG *delta)
2105 2106
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2107

2108
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, extend, delta);
2109

2110
    if (!This->child.reole)
2111 2112
        return CO_E_RELEASED;

2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
    return textrange_startof(me, unit, extend, delta);
}

static HRESULT textrange_endof(ITextRange *range, ME_TextEditor *editor, LONG unit, LONG extend, LONG *delta)
{
    HRESULT hr;
    LONG old_start, old_end, new_end;
    LONG moved;

    ITextRange_GetStart(range, &old_start);
    ITextRange_GetEnd(range, &old_end);

    switch (unit)
    {
    case tomCharacter:
    {
        moved = 0;
        new_end = old_end;
2131 2132
        if (old_end == 0)
        {
2133
            ME_Cursor cursor;
2134
            cursor_from_char_ofs( editor, old_end, &cursor );
2135 2136 2137
            moved = ME_MoveCursorChars(editor, &cursor, 1, TRUE);
            new_end = old_end + moved;
        }
2138 2139 2140
        else if (extend == tomMove && old_start != old_end)
            moved = 1;

2141 2142 2143 2144 2145 2146 2147 2148 2149
        ITextRange_SetEnd(range, new_end);
        if (extend == tomMove)
            ITextRange_SetStart(range, new_end);
        if (delta)
            *delta = moved;
        hr = moved ? S_OK : S_FALSE;
        break;
    }
    default:
2150
        FIXME("unit %ld is not supported\n", unit);
2151 2152 2153
        return E_NOTIMPL;
    }
    return hr;
2154 2155
}

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

2161
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, extend, delta);
2162

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

2166
    return textrange_endof(me, This->child.reole->editor, unit, extend, delta);
2167 2168
}

2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190
static HRESULT textrange_move(ITextRange *range, ME_TextEditor *editor, LONG unit, LONG count, LONG *delta)
{
    LONG old_start, old_end, new_start, new_end;
    LONG move_by;
    LONG moved;
    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 tomCharacter:
    {
        ME_Cursor cursor;

2191 2192 2193
        if (count > 0)
        {
            cursor_from_char_ofs( editor, old_end, &cursor );
2194 2195 2196
            move_by = count;
            if (old_start != old_end)
                --move_by;
2197 2198 2199 2200
        }
        else
        {
            cursor_from_char_ofs( editor, old_start, &cursor );
2201 2202 2203 2204 2205
            move_by = count;
            if (old_start != old_end)
                ++move_by;
        }
        moved = ME_MoveCursorChars(editor, &cursor, move_by, FALSE);
2206 2207
        if (count > 0)
        {
2208 2209 2210 2211
            new_end = old_end + moved;
            new_start = new_end;
            if (old_start != old_end)
                ++moved;
2212 2213 2214
        }
        else
        {
2215 2216 2217 2218 2219
            new_start = old_start + moved;
            new_end = new_start;
            if (old_start != old_end)
                --moved;
        }
2220
        if (delta) *delta = moved;
2221 2222 2223
        break;
    }
    default:
2224
        FIXME("unit %ld is not supported\n", unit);
2225 2226 2227 2228 2229 2230 2231 2232 2233 2234
        return E_NOTIMPL;
    }
    if (moved == 0)
        hr = S_FALSE;
    ITextRange_SetStart(range, new_start);
    ITextRange_SetEnd(range, new_end);

    return hr;
}

2235
static HRESULT WINAPI ITextRange_fnMove(ITextRange *me, LONG unit, LONG count, LONG *delta)
2236 2237
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2238

2239
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, count, delta);
2240

2241
    if (!This->child.reole)
2242 2243
        return CO_E_RELEASED;

2244
    return textrange_move(me, This->child.reole->editor, unit, count, delta);
2245 2246
}

2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
static HRESULT textrange_movestart(ITextRange *range, ME_TextEditor *editor, 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 tomCharacter:
    {
        ME_Cursor cursor;
        LONG moved;

2268
        cursor_from_char_ofs( editor, old_start, &cursor );
2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
        moved = ME_MoveCursorChars(editor, &cursor, count, FALSE);
        new_start = old_start + moved;
        new_end = old_end;
        if (new_end < new_start)
            new_end = new_start;
        if (delta)
            *delta = moved;
        break;
    }
    default:
2279
        FIXME("unit %ld is not supported\n", unit);
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289
        return E_NOTIMPL;
    }
    if (new_start == old_start)
        hr = S_FALSE;
    ITextRange_SetStart(range, new_start);
    ITextRange_SetEnd(range, new_end);

    return hr;
}

2290 2291
static HRESULT WINAPI ITextRange_fnMoveStart(ITextRange *me, LONG unit, LONG count,
                                             LONG *delta)
2292 2293
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2294

2295
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, count, delta);
2296

2297
    if (!This->child.reole)
2298 2299
        return CO_E_RELEASED;

2300
    return textrange_movestart(me, This->child.reole->editor, unit, count, delta);
2301 2302
}

2303
static HRESULT textrange_moveend(ITextRange *range, ME_TextEditor *editor, LONG unit, LONG count, LONG *delta)
2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
{
    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)
    {
2319 2320 2321 2322 2323
    case tomCharacter:
    {
        ME_Cursor cursor;
        LONG moved;

2324
        cursor_from_char_ofs( editor, old_end, &cursor );
2325 2326 2327 2328 2329 2330 2331 2332 2333
        moved = ME_MoveCursorChars(editor, &cursor, count, TRUE);
        new_start = old_start;
        new_end = old_end + moved;
        if (new_end < new_start)
            new_start = new_end;
        if (delta)
            *delta = moved;
        break;
    }
2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
    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:
2353
        FIXME("unit %ld is not supported\n", unit);
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
        return E_NOTIMPL;
    }
    if (new_end == old_end)
        hr = S_FALSE;
    ITextRange_SetStart(range, new_start);
    ITextRange_SetEnd(range, new_end);

    return hr;
}

2364 2365
static HRESULT WINAPI ITextRange_fnMoveEnd(ITextRange *me, LONG unit, LONG count,
                                           LONG *delta)
2366 2367
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2368

2369
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, count, delta);
2370

2371
    if (!This->child.reole)
2372 2373
        return CO_E_RELEASED;

2374
    return textrange_moveend(me, This->child.reole->editor, unit, count, delta);
2375 2376
}

2377 2378
static HRESULT WINAPI ITextRange_fnMoveWhile(ITextRange *me, VARIANT *charset, LONG count,
                                             LONG *delta)
2379 2380
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2381

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

2384
    if (!This->child.reole)
2385 2386 2387 2388 2389
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2390 2391
static HRESULT WINAPI ITextRange_fnMoveStartWhile(ITextRange *me, VARIANT *charset, LONG count,
                                                  LONG *delta)
2392 2393
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2394

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

2397
    if (!This->child.reole)
2398 2399 2400 2401 2402
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2403 2404
static HRESULT WINAPI ITextRange_fnMoveEndWhile(ITextRange *me, VARIANT *charset, LONG count,
                                                LONG *delta)
2405 2406
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2407

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

2410
    if (!This->child.reole)
2411 2412 2413 2414 2415
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2416 2417
static HRESULT WINAPI ITextRange_fnMoveUntil(ITextRange *me, VARIANT *charset, LONG count,
                                             LONG *delta)
2418 2419
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2420

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

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

    return E_NOTIMPL;
}

2429 2430
static HRESULT WINAPI ITextRange_fnMoveStartUntil(ITextRange *me, VARIANT *charset, LONG count,
                                                  LONG *delta)
2431 2432
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2433

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

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

    return E_NOTIMPL;
}

2442 2443
static HRESULT WINAPI ITextRange_fnMoveEndUntil(ITextRange *me, VARIANT *charset, LONG count,
                                                LONG *delta)
2444 2445
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2446

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

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

    return E_NOTIMPL;
}

2455 2456
static HRESULT WINAPI ITextRange_fnFindText(ITextRange *me, BSTR text, LONG count, LONG flags,
                                            LONG *length)
2457 2458
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2459

2460
    FIXME("(%p)->(%s %ld %lx %p): stub\n", This, debugstr_w(text), count, flags, length);
2461

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

    return E_NOTIMPL;
}

2468 2469
static HRESULT WINAPI ITextRange_fnFindTextStart(ITextRange *me, BSTR text, LONG count,
                                                 LONG flags, LONG *length)
2470 2471
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2472

2473
    FIXME("(%p)->(%s %ld %lx %p): stub\n", This, debugstr_w(text), count, flags, length);
2474

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

    return E_NOTIMPL;
}

2481 2482
static HRESULT WINAPI ITextRange_fnFindTextEnd(ITextRange *me, BSTR text, LONG count,
                                               LONG flags, LONG *length)
2483 2484
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2485

2486
    FIXME("(%p)->(%s %ld %lx %p): stub\n", This, debugstr_w(text), count, flags, length);
2487

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

    return E_NOTIMPL;
}

2494
static HRESULT WINAPI ITextRange_fnDelete(ITextRange *me, LONG unit, LONG count, LONG *delta)
2495 2496
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2497

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

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

    return E_NOTIMPL;
}

2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530
static HRESULT textrange_copy_or_cut( ITextRange *range, ME_TextEditor *editor, BOOL cut, VARIANT *v )
{
    LONG start, end;
    ME_Cursor cursor;
    IDataObject **data_out = NULL;

    ITextRange_GetStart( range, &start );
    ITextRange_GetEnd( range, &end );
    if (start == end)
    {
        /* If the range is empty, all text is copied */
        LONG prev_end = end;
        ITextRange_SetEnd( range, MAXLONG );
        start = 0;
        ITextRange_GetEnd( range, &end );
        ITextRange_SetEnd( range, prev_end );
    }
    cursor_from_char_ofs( editor, start, &cursor );

    if (v && V_VT(v) == (VT_UNKNOWN | VT_BYREF) && V_UNKNOWNREF( v ))
        data_out = (IDataObject **)V_UNKNOWNREF( v );

    return editor_copy_or_cut( editor, cut, &cursor, end - start, data_out );
}

2531
static HRESULT WINAPI ITextRange_fnCut(ITextRange *me, VARIANT *v)
2532 2533
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2534

2535
    TRACE("(%p)->(%p)\n", This, v);
2536

2537
    if (!This->child.reole)
2538 2539
        return CO_E_RELEASED;

2540
    return textrange_copy_or_cut(me, This->child.reole->editor, TRUE, v);
2541 2542
}

2543
static HRESULT WINAPI ITextRange_fnCopy(ITextRange *me, VARIANT *v)
2544 2545
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2546

2547
    TRACE("(%p)->(%p)\n", This, v);
2548

2549
    if (!This->child.reole)
2550 2551
        return CO_E_RELEASED;

2552
    return textrange_copy_or_cut(me, This->child.reole->editor, FALSE, v);
2553 2554
}

2555
static HRESULT WINAPI ITextRange_fnPaste(ITextRange *me, VARIANT *v, LONG format)
2556 2557
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2558

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

2561
    if (!This->child.reole)
2562 2563 2564 2565 2566
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2567
static HRESULT WINAPI ITextRange_fnCanPaste(ITextRange *me, VARIANT *v, LONG format, LONG *ret)
2568 2569
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2570

2571
    FIXME("(%p)->(%s %lx %p): stub\n", This, debugstr_variant(v), format, ret);
2572

2573
    if (!This->child.reole)
2574 2575 2576 2577 2578
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2579
static HRESULT WINAPI ITextRange_fnCanEdit(ITextRange *me, LONG *ret)
2580 2581
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2582 2583 2584

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

2585
    if (!This->child.reole)
2586 2587 2588 2589 2590
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2591
static HRESULT WINAPI ITextRange_fnChangeCase(ITextRange *me, LONG type)
2592 2593
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2594

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

2597
    if (!This->child.reole)
2598 2599 2600 2601 2602
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2603
static HRESULT WINAPI ITextRange_fnGetPoint(ITextRange *me, LONG type, LONG *cx, LONG *cy)
2604 2605
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2606

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

2609
    if (!This->child.reole)
2610 2611 2612 2613 2614
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2615 2616
static HRESULT WINAPI ITextRange_fnSetPoint(ITextRange *me, LONG x, LONG y, LONG type,
                                            LONG extend)
2617 2618
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2619

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

2622
    if (!This->child.reole)
2623 2624 2625 2626 2627
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

2628
static HRESULT WINAPI ITextRange_fnScrollIntoView(ITextRange *me, LONG value)
2629 2630
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2631 2632 2633
    ME_TextEditor *editor;
    ME_Cursor cursor;
    int x, y, height;
2634

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

2637
    if (!This->child.reole)
2638 2639
        return CO_E_RELEASED;

2640 2641 2642 2643 2644
    editor = This->child.reole->editor;

    switch (value)
    {
    case tomStart:
2645
        cursor_from_char_ofs( editor, This->start, &cursor );
2646
        cursor_coords( editor, &cursor, &x, &y, &height );
2647
        break;
2648
    case tomEnd:
2649
        cursor_from_char_ofs( editor, This->end, &cursor );
2650
        cursor_coords( editor, &cursor, &x, &y, &height );
2651
        break;
2652
    default:
2653
        FIXME("bStart value %ld not handled\n", value);
2654 2655
        return E_NOTIMPL;
    }
2656
    scroll_abs( editor, x, y, TRUE );
2657
    return S_OK;
2658 2659 2660 2661 2662
}

static HRESULT WINAPI ITextRange_fnGetEmbeddedObject(ITextRange *me, IUnknown **ppv)
{
    ITextRangeImpl *This = impl_from_ITextRange(me);
2663 2664 2665

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

2666
    if (!This->child.reole)
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
        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
};

2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
/* 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);
2757
    TRACE("(%p)->(%lu)\n", This, ref);
2758 2759 2760 2761 2762 2763 2764 2765
    return ref;
}

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

2766
    TRACE("(%p)->(%lu)\n", This, ref);
2767 2768 2769

    if (!ref)
    {
2770 2771 2772
        if (This->range)
            ITextRange_Release(This->range);
        SysFreeString(This->props[FONT_NAME].str);
2773 2774 2775 2776 2777 2778 2779 2780
        heap_free(This);
    }

    return ref;
}

static HRESULT WINAPI TextFont_GetTypeInfoCount(ITextFont *iface, UINT *pctinfo)
{
2781 2782 2783 2784
    ITextFontImpl *This = impl_from_ITextFont(iface);
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
2785 2786 2787 2788 2789
}

static HRESULT WINAPI TextFont_GetTypeInfo(ITextFont *iface, UINT iTInfo, LCID lcid,
    ITypeInfo **ppTInfo)
{
2790 2791 2792
    ITextFontImpl *This = impl_from_ITextFont(iface);
    HRESULT hr;

2793
    TRACE("(%p)->(%u,%ld,%p)\n", This, iTInfo, lcid, ppTInfo);
2794 2795 2796 2797 2798

    hr = get_typeinfo(ITextFont_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
2799 2800 2801 2802 2803
}

static HRESULT WINAPI TextFont_GetIDsOfNames(ITextFont *iface, REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
2804 2805 2806 2807
    ITextFontImpl *This = impl_from_ITextFont(iface);
    ITypeInfo *ti;
    HRESULT hr;

2808
    TRACE("(%p)->(%s, %p, %u, %ld, %p)\n", This, debugstr_guid(riid),
2809
            rgszNames, cNames, lcid, rgDispId);
2810 2811 2812 2813 2814

    hr = get_typeinfo(ITextFont_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827
}

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

2832
    TRACE("(%p)->(%ld, %s, %ld, %u, %p, %p, %p, %p)\n", This, dispIdMember, debugstr_guid(riid),
2833
            lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2834 2835 2836 2837 2838

    hr = get_typeinfo(ITextFont_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_Invoke(ti, iface, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    return hr;
2839 2840 2841 2842 2843
}

static HRESULT WINAPI TextFont_GetDuplicate(ITextFont *iface, ITextFont **ret)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2844 2845 2846 2847 2848 2849 2850 2851 2852 2853

    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;

2854
    return create_textfont(NULL, This, ret);
2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
}

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);
2874
    FIXME("(%p)->(%p %p): stub\n", This, font, ret);
2875 2876 2877
    return E_NOTIMPL;
}

2878 2879 2880 2881
static void textfont_reset_to_default(ITextFontImpl *font)
{
    enum textfont_prop_id id;

2882
    for (id = FONT_PROPID_FIRST; id < FONT_PROPID_LAST; id++) {
2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
        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: {
            SysFreeString(font->props[id].str);
2917
            font->props[id].str = SysAllocString(L"System");
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
            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;

2933
    for (id = FONT_PROPID_FIRST; id < FONT_PROPID_LAST; id++) {
2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
        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);
        }
    }
}

2971 2972 2973 2974 2975 2976 2977
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]);
}

2978 2979 2980
static HRESULT WINAPI TextFont_Reset(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
2981

2982
    TRACE("(%p)->(%ld)\n", This, value);
2983 2984 2985 2986 2987 2988 2989

    /* 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;

2990 2991 2992
        switch (value)
        {
        case tomUndefined:
2993
            return E_INVALIDARG;
2994 2995 2996 2997 2998 2999 3000
        case tomCacheParms:
            textfont_cache_range_props(This);
            This->get_cache_enabled = TRUE;
            break;
        case tomTrackParms:
            This->get_cache_enabled = FALSE;
            break;
3001 3002 3003 3004 3005 3006 3007
        case tomApplyLater:
            This->set_cache_enabled = TRUE;
            break;
        case tomApplyNow:
            This->set_cache_enabled = FALSE;
            textfont_apply_range_props(This);
            break;
3008 3009 3010
        case tomUsePoints:
        case tomUseTwips:
            return E_INVALIDARG;
3011
        default:
3012
            FIXME("reset mode %ld not supported\n", value);
3013 3014 3015
        }

        return S_OK;
3016
    }
3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
    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;
3028 3029 3030 3031 3032
        case tomApplyNow:
        case tomApplyLater:
        case tomTrackParms:
        case tomCacheParms:
            return S_OK;
3033 3034 3035
        case tomUsePoints:
        case tomUseTwips:
            return E_INVALIDARG;
3036 3037
        }
    }
3038

3039
    FIXME("reset mode %ld not supported\n", value);
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052
    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);
3053
    FIXME("(%p)->(%ld): stub\n", This, value);
3054 3055 3056 3057 3058 3059
    return E_NOTIMPL;
}

static HRESULT WINAPI TextFont_GetAllCaps(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3060 3061
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_ALLCAPS, value);
3062 3063 3064 3065 3066
}

static HRESULT WINAPI TextFont_SetAllCaps(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3067
    TRACE("(%p)->(%ld)\n", This, value);
3068
    return set_textfont_propd(This, FONT_ALLCAPS, value);
3069 3070 3071 3072 3073
}

static HRESULT WINAPI TextFont_GetAnimation(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3074 3075
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_ANIMATION, value);
3076 3077 3078 3079 3080
}

static HRESULT WINAPI TextFont_SetAnimation(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3081

3082
    TRACE("(%p)->(%ld)\n", This, value);
3083 3084 3085 3086 3087

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

    return set_textfont_propl(This, FONT_ANIMATION, value);
3088 3089 3090 3091 3092
}

static HRESULT WINAPI TextFont_GetBackColor(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3093 3094
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_BACKCOLOR, value);
3095 3096 3097 3098 3099
}

static HRESULT WINAPI TextFont_SetBackColor(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3100
    TRACE("(%p)->(%ld)\n", This, value);
3101
    return set_textfont_propl(This, FONT_BACKCOLOR, value);
3102 3103 3104 3105 3106
}

static HRESULT WINAPI TextFont_GetBold(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3107
    TRACE("(%p)->(%p)\n", This, value);
3108
    return get_textfont_propl(This, FONT_BOLD, value);
3109 3110 3111 3112 3113
}

static HRESULT WINAPI TextFont_SetBold(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3114
    TRACE("(%p)->(%ld)\n", This, value);
3115
    return set_textfont_propd(This, FONT_BOLD, value);
3116 3117 3118 3119 3120
}

static HRESULT WINAPI TextFont_GetEmboss(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3121 3122
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_EMBOSS, value);
3123 3124 3125 3126 3127
}

static HRESULT WINAPI TextFont_SetEmboss(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3128
    TRACE("(%p)->(%ld)\n", This, value);
3129
    return set_textfont_propd(This, FONT_EMBOSS, value);
3130 3131 3132 3133 3134
}

static HRESULT WINAPI TextFont_GetForeColor(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3135
    TRACE("(%p)->(%p)\n", This, value);
3136
    return get_textfont_propl(This, FONT_FORECOLOR, value);
3137 3138 3139 3140 3141
}

static HRESULT WINAPI TextFont_SetForeColor(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3142
    TRACE("(%p)->(%ld)\n", This, value);
3143
    return set_textfont_propl(This, FONT_FORECOLOR, value);
3144 3145 3146 3147 3148
}

static HRESULT WINAPI TextFont_GetHidden(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3149 3150
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_HIDDEN, value);
3151 3152 3153 3154 3155
}

static HRESULT WINAPI TextFont_SetHidden(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3156
    TRACE("(%p)->(%ld)\n", This, value);
3157
    return set_textfont_propd(This, FONT_HIDDEN, value);
3158 3159 3160 3161 3162
}

static HRESULT WINAPI TextFont_GetEngrave(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3163 3164
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_ENGRAVE, value);
3165 3166 3167 3168 3169
}

static HRESULT WINAPI TextFont_SetEngrave(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3170
    TRACE("(%p)->(%ld)\n", This, value);
3171
    return set_textfont_propd(This, FONT_ENGRAVE, value);
3172 3173 3174 3175 3176
}

static HRESULT WINAPI TextFont_GetItalic(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3177
    TRACE("(%p)->(%p)\n", This, value);
3178
    return get_textfont_propl(This, FONT_ITALIC, value);
3179 3180 3181 3182 3183
}

static HRESULT WINAPI TextFont_SetItalic(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3184
    TRACE("(%p)->(%ld)\n", This, value);
3185
    return set_textfont_propd(This, FONT_ITALIC, value);
3186 3187 3188 3189 3190
}

static HRESULT WINAPI TextFont_GetKerning(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3191 3192
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propf(This, FONT_KERNING, value);
3193 3194 3195 3196 3197
}

static HRESULT WINAPI TextFont_SetKerning(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3198 3199
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_KERNING, value);
3200 3201 3202 3203 3204
}

static HRESULT WINAPI TextFont_GetLanguageID(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3205
    TRACE("(%p)->(%p)\n", This, value);
3206
    return get_textfont_propl(This, FONT_LANGID, value);
3207 3208 3209 3210 3211
}

static HRESULT WINAPI TextFont_SetLanguageID(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3212
    TRACE("(%p)->(%ld)\n", This, value);
3213
    return set_textfont_propl(This, FONT_LANGID, value);
3214 3215 3216 3217 3218
}

static HRESULT WINAPI TextFont_GetName(ITextFont *iface, BSTR *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3219 3220 3221 3222 3223 3224 3225 3226

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

    if (!value)
        return E_INVALIDARG;

    *value = NULL;

3227 3228 3229 3230 3231 3232 3233 3234
    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;
    }

3235
    return textfont_getname_from_range(This->range, value);
3236 3237 3238 3239 3240
}

static HRESULT WINAPI TextFont_SetName(ITextFont *iface, BSTR value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3241 3242 3243 3244 3245 3246
    textfont_prop_val v;

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

    v.str = value;
    return set_textfont_prop(This, FONT_NAME, &v);
3247 3248 3249 3250 3251
}

static HRESULT WINAPI TextFont_GetOutline(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3252 3253
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_OUTLINE, value);
3254 3255 3256 3257 3258
}

static HRESULT WINAPI TextFont_SetOutline(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3259
    TRACE("(%p)->(%ld)\n", This, value);
3260
    return set_textfont_propd(This, FONT_OUTLINE, value);
3261 3262 3263 3264 3265
}

static HRESULT WINAPI TextFont_GetPosition(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3266 3267
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propf(This, FONT_POSITION, value);
3268 3269 3270 3271 3272
}

static HRESULT WINAPI TextFont_SetPosition(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3273 3274
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_POSITION, value);
3275 3276 3277 3278 3279
}

static HRESULT WINAPI TextFont_GetProtected(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3280 3281
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_PROTECTED, value);
3282 3283 3284 3285 3286
}

static HRESULT WINAPI TextFont_SetProtected(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3287
    TRACE("(%p)->(%ld)\n", This, value);
3288
    return set_textfont_propd(This, FONT_PROTECTED, value);
3289 3290 3291 3292 3293
}

static HRESULT WINAPI TextFont_GetShadow(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3294 3295
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_SHADOW, value);
3296 3297 3298 3299 3300
}

static HRESULT WINAPI TextFont_SetShadow(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3301
    TRACE("(%p)->(%ld)\n", This, value);
3302
    return set_textfont_propd(This, FONT_SHADOW, value);
3303 3304 3305 3306 3307
}

static HRESULT WINAPI TextFont_GetSize(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3308
    TRACE("(%p)->(%p)\n", This, value);
3309
    return get_textfont_propf(This, FONT_SIZE, value);
3310 3311 3312 3313 3314
}

static HRESULT WINAPI TextFont_SetSize(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3315 3316
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_SIZE, value);
3317 3318 3319 3320 3321
}

static HRESULT WINAPI TextFont_GetSmallCaps(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3322 3323
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_SMALLCAPS, value);
3324 3325 3326 3327 3328
}

static HRESULT WINAPI TextFont_SetSmallCaps(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3329
    TRACE("(%p)->(%ld)\n", This, value);
3330
    return set_textfont_propd(This, FONT_SMALLCAPS, value);
3331 3332 3333 3334 3335
}

static HRESULT WINAPI TextFont_GetSpacing(ITextFont *iface, FLOAT *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3336 3337
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propf(This, FONT_SPACING, value);
3338 3339 3340 3341 3342
}

static HRESULT WINAPI TextFont_SetSpacing(ITextFont *iface, FLOAT value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3343 3344
    TRACE("(%p)->(%.2f)\n", This, value);
    return set_textfont_propf(This, FONT_SPACING, value);
3345 3346 3347 3348 3349
}

static HRESULT WINAPI TextFont_GetStrikeThrough(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3350
    TRACE("(%p)->(%p)\n", This, value);
3351
    return get_textfont_propl(This, FONT_STRIKETHROUGH, value);
3352 3353 3354 3355 3356
}

static HRESULT WINAPI TextFont_SetStrikeThrough(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3357
    TRACE("(%p)->(%ld)\n", This, value);
3358
    return set_textfont_propd(This, FONT_STRIKETHROUGH, value);
3359 3360 3361 3362 3363
}

static HRESULT WINAPI TextFont_GetSubscript(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3364
    TRACE("(%p)->(%p)\n", This, value);
3365
    return get_textfont_propl(This, FONT_SUBSCRIPT, value);
3366 3367 3368 3369 3370
}

static HRESULT WINAPI TextFont_SetSubscript(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3371
    TRACE("(%p)->(%ld)\n", This, value);
3372
    return set_textfont_propd(This, FONT_SUBSCRIPT, value);
3373 3374 3375 3376 3377
}

static HRESULT WINAPI TextFont_GetSuperscript(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3378
    TRACE("(%p)->(%p)\n", This, value);
3379
    return get_textfont_propl(This, FONT_SUPERSCRIPT, value);
3380 3381 3382 3383 3384
}

static HRESULT WINAPI TextFont_SetSuperscript(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3385
    TRACE("(%p)->(%ld)\n", This, value);
3386
    return set_textfont_propd(This, FONT_SUPERSCRIPT, value);
3387 3388 3389 3390 3391
}

static HRESULT WINAPI TextFont_GetUnderline(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3392
    TRACE("(%p)->(%p)\n", This, value);
3393
    return get_textfont_propl(This, FONT_UNDERLINE, value);
3394 3395 3396 3397 3398
}

static HRESULT WINAPI TextFont_SetUnderline(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3399
    TRACE("(%p)->(%ld)\n", This, value);
3400
    return set_textfont_propd(This, FONT_UNDERLINE, value);
3401 3402 3403 3404 3405
}

static HRESULT WINAPI TextFont_GetWeight(ITextFont *iface, LONG *value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3406 3407
    TRACE("(%p)->(%p)\n", This, value);
    return get_textfont_propl(This, FONT_WEIGHT, value);
3408 3409 3410 3411 3412
}

static HRESULT WINAPI TextFont_SetWeight(ITextFont *iface, LONG value)
{
    ITextFontImpl *This = impl_from_ITextFont(iface);
3413
    TRACE("(%p)->(%ld)\n", This, value);
3414
    return set_textfont_propl(This, FONT_WEIGHT, value);
3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481
}

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
};

3482
static HRESULT create_textfont(ITextRange *range, const ITextFontImpl *src, ITextFont **ret)
3483 3484 3485 3486 3487 3488 3489 3490 3491 3492
{
    ITextFontImpl *font;

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

    font->ITextFont_iface.lpVtbl = &textfontvtbl;
    font->ref = 1;
3493 3494 3495

    if (src) {
        font->range = NULL;
3496
        font->get_cache_enabled = TRUE;
3497
        font->set_cache_enabled = TRUE;
3498 3499 3500 3501 3502 3503 3504
        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);
3505 3506 3507

        /* cache current properties */
        font->get_cache_enabled = FALSE;
3508
        font->set_cache_enabled = FALSE;
3509
        textfont_cache_range_props(font);
3510
    }
3511 3512 3513 3514 3515

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

3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539
/* 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);
3540
    TRACE("(%p)->(%lu)\n", This, ref);
3541 3542 3543 3544 3545 3546 3547 3548
    return ref;
}

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

3549
    TRACE("(%p)->(%lu)\n", This, ref);
3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561

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

    return ref;
}

static HRESULT WINAPI TextPara_GetTypeInfoCount(ITextPara *iface, UINT *pctinfo)
{
3562 3563 3564 3565
    ITextParaImpl *This = impl_from_ITextPara(iface);
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
3566 3567 3568 3569 3570
}

static HRESULT WINAPI TextPara_GetTypeInfo(ITextPara *iface, UINT iTInfo, LCID lcid,
    ITypeInfo **ppTInfo)
{
3571 3572 3573
    ITextParaImpl *This = impl_from_ITextPara(iface);
    HRESULT hr;

3574
    TRACE("(%p)->(%u,%ld,%p)\n", This, iTInfo, lcid, ppTInfo);
3575 3576 3577 3578 3579

    hr = get_typeinfo(ITextPara_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
3580 3581 3582 3583 3584
}

static HRESULT WINAPI TextPara_GetIDsOfNames(ITextPara *iface, REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
{
3585 3586 3587 3588
    ITextParaImpl *This = impl_from_ITextPara(iface);
    ITypeInfo *ti;
    HRESULT hr;

3589
    TRACE("(%p)->(%s, %p, %u, %ld, %p)\n", This, debugstr_guid(riid), rgszNames,
3590
            cNames, lcid, rgDispId);
3591 3592 3593 3594 3595

    hr = get_typeinfo(ITextPara_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608
}

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

3613
    TRACE("(%p)->(%ld, %s, %ld, %u, %p, %p, %p, %p)\n", This, dispIdMember,
3614 3615
            debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
            pExcepInfo, puArgErr);
3616 3617 3618 3619 3620

    hr = get_typeinfo(ITextPara_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_Invoke(ti, iface, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
    return hr;
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
}

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);
3654
    FIXME("(%p)->(%ld)\n", This, value);
3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667
    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);
3668
    FIXME("(%p)->(%ld)\n", This, value);
3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681
    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);
3682
    FIXME("(%p)->(%ld)\n", This, value);
3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695
    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);
3696
    FIXME("(%p)->(%ld)\n", This, value);
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716
    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);
3717
    FIXME("(%p)->(%ld)\n", This, value);
3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730
    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);
3731
    FIXME("(%p)->(%ld)\n", This, value);
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
    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);
3766
    FIXME("(%p)->(%ld)\n", This, value);
3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779
    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);
3780
    FIXME("(%p)->(%ld)\n", This, value);
3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793
    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);
3794
    FIXME("(%p)->(%ld)\n", This, value);
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
    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);
3822
    FIXME("(%p)->(%ld)\n", This, value);
3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835
    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);
3836
    FIXME("(%p)->(%ld)\n", This, value);
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849
    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);
3850
    FIXME("(%p)->(%ld)\n", This, value);
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
    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);
3878
    FIXME("(%p)->(%ld %.2f)\n", This, LineSpacingRule, LineSpacing);
3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919
    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);
3920
    FIXME("(%p)->(%ld)\n", This, value);
3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933
    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);
3934
    FIXME("(%p)->(%.2f %ld %ld)\n", This, tbPos, tbAlign, tbLeader);
3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
    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);
3955
    FIXME("(%p)->(%ld %p %p %p)\n", This, iTab, ptbPos, ptbAlign, ptbLeader);
3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034
    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;
}

4035
/* ITextDocument */
4036 4037
static HRESULT WINAPI ITextDocument2Old_fnQueryInterface(ITextDocument2Old* iface, REFIID riid,
                                                         void **ppvObject)
4038
{
4039 4040
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    return IUnknown_QueryInterface( services->outer_unk, riid, ppvObject );
4041 4042
}

4043
static ULONG WINAPI ITextDocument2Old_fnAddRef(ITextDocument2Old *iface)
4044
{
4045 4046
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    return IUnknown_AddRef( services->outer_unk );
4047 4048
}

4049
static ULONG WINAPI ITextDocument2Old_fnRelease(ITextDocument2Old *iface)
4050
{
4051 4052
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    return IUnknown_Release( services->outer_unk );
4053 4054
}

4055 4056
static HRESULT WINAPI ITextDocument2Old_fnGetTypeInfoCount(ITextDocument2Old *iface,
                                                           UINT *pctinfo)
4057
{
4058 4059
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    TRACE("(%p)->(%p)\n", services, pctinfo);
4060 4061
    *pctinfo = 1;
    return S_OK;
4062 4063
}

4064 4065
static HRESULT WINAPI ITextDocument2Old_fnGetTypeInfo(ITextDocument2Old *iface, UINT iTInfo, LCID lcid,
                                                      ITypeInfo **ppTInfo)
4066
{
4067
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4068 4069
    HRESULT hr;

4070
    TRACE("(%p)->(%u,%ld,%p)\n", services, iTInfo, lcid, ppTInfo);
4071 4072 4073 4074 4075

    hr = get_typeinfo(ITextDocument_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
4076 4077
}

4078 4079 4080
static HRESULT WINAPI ITextDocument2Old_fnGetIDsOfNames(ITextDocument2Old *iface, REFIID riid,
                                                        LPOLESTR *rgszNames, UINT cNames,
                                                        LCID lcid, DISPID *rgDispId)
4081
{
4082
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4083 4084 4085
    ITypeInfo *ti;
    HRESULT hr;

4086
    TRACE("(%p)->(%s, %p, %u, %ld, %p)\n", services, debugstr_guid(riid),
4087
            rgszNames, cNames, lcid, rgDispId);
4088 4089 4090 4091 4092

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

4095 4096 4097 4098
static HRESULT WINAPI ITextDocument2Old_fnInvoke(ITextDocument2Old *iface, DISPID dispIdMember,
                                                 REFIID riid, LCID lcid, WORD wFlags,
                                                 DISPPARAMS *pDispParams, VARIANT *pVarResult,
                                                 EXCEPINFO *pExcepInfo, UINT *puArgErr)
4099
{
4100
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4101 4102 4103
    ITypeInfo *ti;
    HRESULT hr;

4104
    TRACE("(%p)->(%ld, %s, %ld, %u, %p, %p, %p, %p)\n", services, dispIdMember,
4105 4106
            debugstr_guid(riid), lcid, wFlags, pDispParams, pVarResult,
            pExcepInfo, puArgErr);
4107 4108 4109

    hr = get_typeinfo(ITextDocument_tid, &ti);
    if (SUCCEEDED(hr))
4110
        hr = ITypeInfo_Invoke(ti, iface, dispIdMember, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
4111
    return hr;
4112 4113
}

4114
static HRESULT WINAPI ITextDocument2Old_fnGetName(ITextDocument2Old *iface, BSTR *pName)
4115
{
4116 4117
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4118 4119 4120
    return E_NOTIMPL;
}

4121
static HRESULT WINAPI ITextDocument2Old_fnGetSelection(ITextDocument2Old *iface, ITextSelection **selection)
4122
{
4123
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4124

4125
    TRACE("(%p)->(%p)\n", iface, selection);
4126 4127

    if (!selection)
4128
      return E_INVALIDARG;
4129

4130 4131 4132 4133 4134 4135 4136 4137
    if (!services->text_selection)
    {
        services->text_selection = text_selection_create( services );
        if (!services->text_selection)
        {
            *selection = NULL;
            return E_OUTOFMEMORY;
        }
4138 4139
    }

4140
    *selection = &services->text_selection->ITextSelection_iface;
4141
    ITextSelection_AddRef(*selection);
4142
    return S_OK;
4143 4144
}

4145
static HRESULT WINAPI ITextDocument2Old_fnGetStoryCount(ITextDocument2Old *iface, LONG *pCount)
4146
{
4147 4148
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4149 4150 4151
    return E_NOTIMPL;
}

4152 4153
static HRESULT WINAPI ITextDocument2Old_fnGetStoryRanges(ITextDocument2Old *iface,
                                                         ITextStoryRanges **ppStories)
4154
{
4155 4156
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4157 4158 4159
    return E_NOTIMPL;
}

4160
static HRESULT WINAPI ITextDocument2Old_fnGetSaved(ITextDocument2Old *iface, LONG *pValue)
4161
{
4162 4163
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4164 4165 4166
    return E_NOTIMPL;
}

4167
static HRESULT WINAPI ITextDocument2Old_fnSetSaved(ITextDocument2Old *iface, LONG Value)
4168
{
4169 4170
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4171 4172 4173
    return E_NOTIMPL;
}

4174
static HRESULT WINAPI ITextDocument2Old_fnGetDefaultTabStop(ITextDocument2Old *iface, float *pValue)
4175
{
4176 4177
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4178 4179 4180
    return E_NOTIMPL;
}

4181
static HRESULT WINAPI ITextDocument2Old_fnSetDefaultTabStop(ITextDocument2Old *iface, float Value)
4182
{
4183 4184
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4185 4186 4187
    return E_NOTIMPL;
}

4188
static HRESULT WINAPI ITextDocument2Old_fnNew(ITextDocument2Old *iface)
4189
{
4190 4191
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4192 4193 4194
    return E_NOTIMPL;
}

4195 4196
static HRESULT WINAPI ITextDocument2Old_fnOpen(ITextDocument2Old *iface, VARIANT *pVar,
                                               LONG Flags, LONG CodePage)
4197
{
4198 4199
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4200 4201 4202
    return E_NOTIMPL;
}

4203 4204
static HRESULT WINAPI ITextDocument2Old_fnSave(ITextDocument2Old *iface, VARIANT *pVar,
                                               LONG Flags, LONG CodePage)
4205
{
4206 4207
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4208 4209 4210
    return E_NOTIMPL;
}

4211
static HRESULT WINAPI ITextDocument2Old_fnFreeze(ITextDocument2Old *iface, LONG *pCount)
4212
{
4213 4214
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4215 4216 4217
    return E_NOTIMPL;
}

4218
static HRESULT WINAPI ITextDocument2Old_fnUnfreeze(ITextDocument2Old *iface, LONG *pCount)
4219
{
4220 4221
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4222 4223 4224
    return E_NOTIMPL;
}

4225
static HRESULT WINAPI ITextDocument2Old_fnBeginEditCollection(ITextDocument2Old *iface)
4226
{
4227 4228
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4229 4230 4231
    return E_NOTIMPL;
}

4232
static HRESULT WINAPI ITextDocument2Old_fnEndEditCollection(ITextDocument2Old *iface)
4233
{
4234 4235
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4236 4237 4238
    return E_NOTIMPL;
}

4239
static HRESULT WINAPI ITextDocument2Old_fnUndo(ITextDocument2Old *iface, LONG Count, LONG *prop)
4240
{
4241 4242
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4243 4244 4245
    return E_NOTIMPL;
}

4246
static HRESULT WINAPI ITextDocument2Old_fnRedo(ITextDocument2Old *iface, LONG Count, LONG *prop)
4247
{
4248 4249
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4250 4251 4252
    return E_NOTIMPL;
}

4253
static HRESULT CreateITextRange(struct text_services *services, LONG start, LONG end, ITextRange** ppRange)
4254 4255 4256 4257 4258 4259 4260
{
    ITextRangeImpl *txtRge = heap_alloc(sizeof(ITextRangeImpl));

    if (!txtRge)
        return E_OUTOFMEMORY;
    txtRge->ITextRange_iface.lpVtbl = &trvt;
    txtRge->ref = 1;
4261
    txtRge->child.reole = services;
4262 4263
    txtRge->start = start;
    txtRge->end = end;
4264
    list_add_head( &services->rangelist, &txtRge->child.entry );
4265 4266 4267 4268
    *ppRange = &txtRge->ITextRange_iface;
    return S_OK;
}

4269 4270
static HRESULT WINAPI ITextDocument2Old_fnRange(ITextDocument2Old *iface, LONG cp1, LONG cp2,
                                                ITextRange **ppRange)
4271
{
4272
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4273

4274
    TRACE("%p %p %ld %ld\n", services, ppRange, cp1, cp2);
4275 4276 4277
    if (!ppRange)
        return E_INVALIDARG;

4278 4279
    cp2range(services->editor, &cp1, &cp2);
    return CreateITextRange(services, cp1, cp2, ppRange);
4280 4281
}

4282 4283
static HRESULT WINAPI ITextDocument2Old_fnRangeFromPoint(ITextDocument2Old *iface, LONG x, LONG y,
                                                         ITextRange **ppRange)
4284
{
4285 4286
    struct text_services *services = impl_from_ITextDocument2Old(iface);
    FIXME("stub %p\n", services);
4287 4288 4289
    return E_NOTIMPL;
}

4290 4291 4292
/* ITextDocument2Old methods */
static HRESULT WINAPI ITextDocument2Old_fnAttachMsgFilter(ITextDocument2Old *iface, IUnknown *filter)
{
4293
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4294

4295
    FIXME("(%p)->(%p): stub\n", services, filter);
4296 4297 4298 4299 4300 4301

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSetEffectColor(ITextDocument2Old *iface, LONG index, COLORREF cr)
{
4302
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4303

4304
    FIXME("(%p)->(%ld, 0x%lx): stub\n", services, index, cr);
4305 4306 4307 4308 4309 4310

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetEffectColor(ITextDocument2Old *iface, LONG index, COLORREF *cr)
{
4311
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4312

4313
    FIXME("(%p)->(%ld, %p): stub\n", services, index, cr);
4314 4315 4316 4317 4318 4319

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetCaretType(ITextDocument2Old *iface, LONG *type)
{
4320
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4321

4322
    FIXME("(%p)->(%p): stub\n", services, type);
4323 4324 4325 4326 4327 4328

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSetCaretType(ITextDocument2Old *iface, LONG type)
{
4329
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4330

4331
    FIXME("(%p)->(%ld): stub\n", services, type);
4332 4333 4334 4335 4336 4337

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetImmContext(ITextDocument2Old *iface, LONG *context)
{
4338
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4339

4340
    FIXME("(%p)->(%p): stub\n", services, context);
4341 4342 4343 4344 4345 4346

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnReleaseImmContext(ITextDocument2Old *iface, LONG context)
{
4347
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4348

4349
    FIXME("(%p)->(%ld): stub\n", services, context);
4350 4351 4352 4353 4354 4355 4356 4357

    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)
{
4358
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4359

4360
    FIXME("(%p)->(%ld, %ld, %ld, %ld, %ld, %p, %p, %p): stub\n", services, cp, charrep, options, current_charrep,
4361 4362 4363 4364 4365 4366 4367
          current_fontsize, bstr, pitch_family, new_fontsize);

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetNotificationMode(ITextDocument2Old *iface, LONG *mode)
{
4368
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4369

4370
    FIXME("(%p)->(%p): stub\n", services, mode);
4371 4372 4373 4374 4375 4376

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSetNotificationMode(ITextDocument2Old *iface, LONG mode)
{
4377
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4378

4379
    FIXME("(%p)->(0x%lx): stub\n", services, mode);
4380 4381 4382 4383 4384 4385 4386

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetClientRect(ITextDocument2Old *iface, LONG type, LONG *left, LONG *top,
                                                        LONG *right, LONG *bottom)
{
4387
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4388

4389
    FIXME("(%p)->(%ld, %p, %p, %p, %p): stub\n", services, type, left, top, right, bottom);
4390 4391 4392 4393 4394 4395

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetSelectionEx(ITextDocument2Old *iface, ITextSelection **selection)
{
4396
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4397

4398
    FIXME("(%p)->(%p): stub\n", services, selection);
4399 4400 4401 4402 4403 4404

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetWindow(ITextDocument2Old *iface, LONG *hwnd)
{
4405
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4406

4407
    FIXME("(%p)->(%p): stub\n", services, hwnd);
4408 4409 4410 4411 4412 4413

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnGetFEFlags(ITextDocument2Old *iface, LONG *flags)
{
4414
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4415

4416
    FIXME("(%p)->(%p): stub\n", services, flags);
4417 4418 4419 4420 4421 4422

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnUpdateWindow(ITextDocument2Old *iface)
{
4423
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4424

4425
    FIXME("(%p): stub\n", services);
4426 4427 4428 4429 4430 4431

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnCheckTextLimit(ITextDocument2Old *iface, LONG cch, LONG *exceed)
{
4432
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4433

4434
    FIXME("(%p)->(%ld, %p): stub\n", services, cch, exceed);
4435 4436 4437 4438 4439 4440

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnIMEInProgress(ITextDocument2Old *iface, LONG mode)
{
4441
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4442

4443
    FIXME("(%p)->(0x%lx): stub\n", services, mode);
4444 4445 4446 4447 4448 4449

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnSysBeep(ITextDocument2Old *iface)
{
4450
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4451

4452
    FIXME("(%p): stub\n", services);
4453 4454 4455 4456 4457 4458

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnUpdate(ITextDocument2Old *iface, LONG mode)
{
4459
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4460

4461
    FIXME("(%p)->(0x%lx): stub\n", services, mode);
4462 4463 4464 4465 4466 4467

    return E_NOTIMPL;
}

static HRESULT WINAPI ITextDocument2Old_fnNotify(ITextDocument2Old *iface, LONG notify)
{
4468
    struct text_services *services = impl_from_ITextDocument2Old(iface);
4469

4470
    FIXME("(%p)->(%ld): stub\n", services, notify);
4471 4472 4473 4474

    return E_NOTIMPL;
}

4475 4476
const ITextDocument2OldVtbl text_doc2old_vtbl =
{
4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523
    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
4524 4525
};

4526
/* ITextSelection */
4527 4528 4529 4530 4531
static HRESULT WINAPI ITextSelection_fnQueryInterface(
    ITextSelection *me,
    REFIID riid,
    void **ppvObj)
{
4532
    struct text_selection *This = impl_from_ITextSelection(me);
4533

4534 4535 4536 4537 4538 4539 4540 4541 4542 4543
    *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;
    }
4544 4545
    else if (IsEqualGUID(riid, &IID_Igetrichole))
    {
4546
        *ppvObj = This->services;
4547 4548
        return S_OK;
    }
4549 4550 4551 4552

    return E_NOINTERFACE;
}

4553
static ULONG WINAPI ITextSelection_fnAddRef(ITextSelection *me)
4554
{
4555
    struct text_selection *This = impl_from_ITextSelection(me);
4556 4557 4558
    return InterlockedIncrement(&This->ref);
}

4559
static ULONG WINAPI ITextSelection_fnRelease(ITextSelection *me)
4560
{
4561
    struct text_selection *This = impl_from_ITextSelection(me);
4562 4563 4564 4565 4566 4567
    ULONG ref = InterlockedDecrement(&This->ref);
    if (ref == 0)
        heap_free(This);
    return ref;
}

4568
static HRESULT WINAPI ITextSelection_fnGetTypeInfoCount(ITextSelection *me, UINT *pctinfo)
4569
{
4570
    struct text_selection *This = impl_from_ITextSelection(me);
4571 4572 4573
    TRACE("(%p)->(%p)\n", This, pctinfo);
    *pctinfo = 1;
    return S_OK;
4574 4575
}

4576
static HRESULT WINAPI ITextSelection_fnGetTypeInfo(ITextSelection *me, UINT iTInfo, LCID lcid,
4577 4578
    ITypeInfo **ppTInfo)
{
4579
    struct text_selection *This = impl_from_ITextSelection(me);
4580
    HRESULT hr;
4581

4582
    TRACE("(%p)->(%u,%ld,%p)\n", This, iTInfo, lcid, ppTInfo);
4583 4584 4585 4586 4587

    hr = get_typeinfo(ITextSelection_tid, ppTInfo);
    if (SUCCEEDED(hr))
        ITypeInfo_AddRef(*ppTInfo);
    return hr;
4588 4589
}

4590 4591
static HRESULT WINAPI ITextSelection_fnGetIDsOfNames(ITextSelection *me, REFIID riid,
    LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
4592
{
4593
    struct text_selection *This = impl_from_ITextSelection(me);
4594 4595
    ITypeInfo *ti;
    HRESULT hr;
4596

4597
    TRACE("(%p)->(%s, %p, %u, %ld, %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid,
4598 4599 4600 4601 4602 4603
            rgDispId);

    hr = get_typeinfo(ITextSelection_tid, &ti);
    if (SUCCEEDED(hr))
        hr = ITypeInfo_GetIDsOfNames(ti, rgszNames, cNames, rgDispId);
    return hr;
4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616
}

static HRESULT WINAPI ITextSelection_fnInvoke(
    ITextSelection *me,
    DISPID dispIdMember,
    REFIID riid,
    LCID lcid,
    WORD wFlags,
    DISPPARAMS *pDispParams,
    VARIANT *pVarResult,
    EXCEPINFO *pExcepInfo,
    UINT *puArgErr)
{
4617
    struct text_selection *This = impl_from_ITextSelection(me);
4618 4619 4620
    ITypeInfo *ti;
    HRESULT hr;

4621
    TRACE("(%p)->(%ld, %s, %ld, %u, %p, %p, %p, %p)\n", This, dispIdMember, debugstr_guid(riid), lcid,
4622 4623 4624 4625 4626 4627
            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;
4628 4629 4630
}

/*** ITextRange methods ***/
4631
static HRESULT WINAPI ITextSelection_fnGetText(ITextSelection *me, BSTR *pbstr)
4632
{
4633
    struct text_selection *This = impl_from_ITextSelection(me);
4634 4635 4636 4637
    ME_Cursor *start = NULL, *end = NULL;
    int nChars, endOfs;
    BOOL bEOP;

4638 4639
    TRACE("(%p)->(%p)\n", This, pbstr);

4640
    if (!This->services)
4641
        return CO_E_RELEASED;
4642

4643 4644
    if (!pbstr)
        return E_INVALIDARG;
4645

4646
    ME_GetSelection(This->services->editor, &start, &end);
4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658
    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;

4659 4660
    bEOP = (!para_next( para_next( end->para ) ) && endOfs > ME_GetTextLength(This->services->editor));
    ME_GetTextW(This->services->editor, *pbstr, nChars, start, nChars, FALSE, bEOP);
4661 4662 4663
    TRACE("%s\n", wine_dbgstr_w(*pbstr));

    return S_OK;
4664 4665
}

4666
static HRESULT WINAPI ITextSelection_fnSetText(ITextSelection *me, BSTR str)
4667
{
4668
    struct text_selection *This = impl_from_ITextSelection(me);
4669
    ME_TextEditor *editor;
4670 4671
    int len;
    LONG to, from;
4672 4673 4674

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

4675
    if (!This->services)
4676 4677
        return CO_E_RELEASED;

4678
    editor = This->services->editor;
4679
    len = lstrlenW(str);
4680 4681 4682 4683
    ME_GetSelectionOfs(editor, &from, &to);
    ME_ReplaceSel(editor, FALSE, str, len);

    if (len < to - from)
4684
        textranges_update_ranges(This->services, from, len, RANGE_UPDATE_DELETE);
4685 4686

    return S_OK;
4687 4688
}

4689
static HRESULT WINAPI ITextSelection_fnGetChar(ITextSelection *me, LONG *pch)
4690
{
4691
    struct text_selection *This = impl_from_ITextSelection(me);
4692 4693
    ME_Cursor *start = NULL, *end = NULL;

4694 4695
    TRACE("(%p)->(%p)\n", This, pch);

4696
    if (!This->services)
4697
        return CO_E_RELEASED;
4698

4699 4700
    if (!pch)
        return E_INVALIDARG;
4701

4702 4703
    ME_GetSelection(This->services->editor, &start, &end);
    return range_GetChar(This->services->editor, start, pch);
4704 4705
}

4706
static HRESULT WINAPI ITextSelection_fnSetChar(ITextSelection *me, LONG ch)
4707
{
4708
    struct text_selection *This = impl_from_ITextSelection(me);
4709

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

4712
    if (!This->services)
4713 4714 4715 4716 4717
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4718
static HRESULT WINAPI ITextSelection_fnGetDuplicate(ITextSelection *me, ITextRange **range)
4719
{
4720
    struct text_selection *This = impl_from_ITextSelection(me);
4721 4722 4723 4724
    LONG start, end;

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

4725
    if (!This->services)
4726 4727
        return CO_E_RELEASED;

4728 4729 4730 4731 4732
    if (!range)
        return E_INVALIDARG;

    ITextSelection_GetStart(me, &start);
    ITextSelection_GetEnd(me, &end);
4733
    return CreateITextRange(This->services, start, end, range);
4734 4735
}

4736
static HRESULT WINAPI ITextSelection_fnGetFormattedText(ITextSelection *me, ITextRange **range)
4737
{
4738
    struct text_selection *This = impl_from_ITextSelection(me);
4739 4740 4741

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

4742
    if (!This->services)
4743 4744 4745 4746 4747
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4748
static HRESULT WINAPI ITextSelection_fnSetFormattedText(ITextSelection *me, ITextRange *range)
4749
{
4750
    struct text_selection *This = impl_from_ITextSelection(me);
4751 4752 4753

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

4754
    if (!This->services)
4755 4756 4757 4758 4759 4760
        return CO_E_RELEASED;

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

4761
static HRESULT WINAPI ITextSelection_fnGetStart(ITextSelection *me, LONG *pcpFirst)
4762
{
4763
    struct text_selection *This = impl_from_ITextSelection(me);
4764
    LONG lim;
4765 4766 4767

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

4768
    if (!This->services)
4769 4770
        return CO_E_RELEASED;

4771 4772
    if (!pcpFirst)
        return E_INVALIDARG;
4773
    ME_GetSelectionOfs(This->services->editor, pcpFirst, &lim);
4774
    return S_OK;
4775 4776
}

4777
static HRESULT WINAPI ITextSelection_fnSetStart(ITextSelection *me, LONG value)
4778
{
4779
    struct text_selection *This = impl_from_ITextSelection(me);
4780 4781 4782
    LONG start, end;
    HRESULT hr;

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

4785
    if (!This->services)
4786 4787
        return CO_E_RELEASED;

4788 4789
    ME_GetSelectionOfs(This->services->editor, &start, &end);
    hr = textrange_setstart(This->services, value, &start, &end);
4790
    if (hr == S_OK)
4791
        set_selection(This->services->editor, start, end);
4792 4793

    return hr;
4794 4795
}

4796
static HRESULT WINAPI ITextSelection_fnGetEnd(ITextSelection *me, LONG *pcpLim)
4797
{
4798
    struct text_selection *This = impl_from_ITextSelection(me);
4799
    LONG first;
4800 4801 4802

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

4803
    if (!This->services)
4804 4805
        return CO_E_RELEASED;

4806 4807
    if (!pcpLim)
        return E_INVALIDARG;
4808
    ME_GetSelectionOfs(This->services->editor, &first, pcpLim);
4809
    return S_OK;
4810 4811
}

4812
static HRESULT WINAPI ITextSelection_fnSetEnd(ITextSelection *me, LONG value)
4813
{
4814
    struct text_selection *This = impl_from_ITextSelection(me);
4815 4816 4817
    LONG start, end;
    HRESULT hr;

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

4820
    if (!This->services)
4821 4822
        return CO_E_RELEASED;

4823 4824
    ME_GetSelectionOfs(This->services->editor, &start, &end);
    hr = textrange_setend(This->services, value, &start, &end);
4825
    if (hr == S_OK)
4826
        set_selection(This->services->editor, start, end);
4827 4828

    return hr;
4829 4830
}

4831
static HRESULT WINAPI ITextSelection_fnGetFont(ITextSelection *me, ITextFont **font)
4832
{
4833
    struct text_selection *This = impl_from_ITextSelection(me);
4834 4835
    ITextRange *range = NULL;
    HRESULT hr;
4836 4837 4838

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

4839
    if (!This->services)
4840 4841
        return CO_E_RELEASED;

4842 4843 4844
    if (!font)
        return E_INVALIDARG;

4845 4846 4847 4848
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = create_textfont(range, NULL, font);
    ITextRange_Release(range);
    return hr;
4849 4850
}

4851
static HRESULT WINAPI ITextSelection_fnSetFont(ITextSelection *me, ITextFont *font)
4852
{
4853
    struct text_selection *This = impl_from_ITextSelection(me);
4854
    ITextRange *range = NULL;
4855 4856 4857 4858 4859 4860

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

    if (!font)
        return E_INVALIDARG;

4861
    if (!This->services)
4862 4863
        return CO_E_RELEASED;

4864 4865 4866
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    textrange_set_font(range, font);
    ITextRange_Release(range);
4867
    return S_OK;
4868 4869
}

4870
static HRESULT WINAPI ITextSelection_fnGetPara(ITextSelection *me, ITextPara **para)
4871
{
4872
    struct text_selection *This = impl_from_ITextSelection(me);
4873 4874
    ITextRange *range = NULL;
    HRESULT hr;
4875 4876 4877

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

4878
    if (!This->services)
4879 4880
        return CO_E_RELEASED;

4881 4882 4883
    if (!para)
        return E_INVALIDARG;

4884 4885 4886 4887
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = create_textpara(range, para);
    ITextRange_Release(range);
    return hr;
4888 4889
}

4890
static HRESULT WINAPI ITextSelection_fnSetPara(ITextSelection *me, ITextPara *para)
4891
{
4892
    struct text_selection *This = impl_from_ITextSelection(me);
4893 4894 4895

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

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

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

4903
static HRESULT WINAPI ITextSelection_fnGetStoryLength(ITextSelection *me, LONG *length)
4904
{
4905
    struct text_selection *This = impl_from_ITextSelection(me);
4906 4907 4908

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

4909
    if (!This->services)
4910 4911
        return CO_E_RELEASED;

4912
    return textrange_get_storylength(This->services->editor, length);
4913 4914
}

4915
static HRESULT WINAPI ITextSelection_fnGetStoryType(ITextSelection *me, LONG *value)
4916
{
4917
    struct text_selection *This = impl_from_ITextSelection(me);
4918 4919 4920

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

4921
    if (!This->services)
4922 4923
        return CO_E_RELEASED;

4924 4925 4926 4927 4928
    if (!value)
        return E_INVALIDARG;

    *value = tomUnknownStory;
    return S_OK;
4929 4930
}

4931
static HRESULT WINAPI ITextSelection_fnCollapse(ITextSelection *me, LONG bStart)
4932
{
4933
    struct text_selection *This = impl_from_ITextSelection(me);
4934 4935
    LONG start, end;
    HRESULT hres;
4936

4937
    TRACE("(%p)->(%ld)\n", This, bStart);
4938

4939
    if (!This->services)
4940 4941
        return CO_E_RELEASED;

4942
    ME_GetSelectionOfs(This->services->editor, &start, &end);
4943 4944
    hres = range_Collapse(bStart, &start, &end);
    if (SUCCEEDED(hres))
4945
        set_selection(This->services->editor, start, end);
4946
    return hres;
4947 4948
}

4949
static HRESULT WINAPI ITextSelection_fnExpand(ITextSelection *me, LONG unit, LONG *delta)
4950
{
4951
    struct text_selection *This = impl_from_ITextSelection(me);
4952 4953
    ITextRange *range = NULL;
    HRESULT hr;
4954

4955
    TRACE("(%p)->(%ld %p)\n", This, unit, delta);
4956

4957
    if (!This->services)
4958 4959
        return CO_E_RELEASED;

4960 4961 4962 4963
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = textrange_expand(range, unit, delta);
    ITextRange_Release(range);
    return hr;
4964 4965
}

4966
static HRESULT WINAPI ITextSelection_fnGetIndex(ITextSelection *me, LONG unit, LONG *index)
4967
{
4968
    struct text_selection *This = impl_from_ITextSelection(me);
4969

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

4972
    if (!This->services)
4973 4974 4975 4976 4977
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4978 4979
static HRESULT WINAPI ITextSelection_fnSetIndex(ITextSelection *me, LONG unit, LONG index,
    LONG extend)
4980
{
4981
    struct text_selection *This = impl_from_ITextSelection(me);
4982

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

4985
    if (!This->services)
4986 4987 4988 4989 4990
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

4991
static HRESULT WINAPI ITextSelection_fnSetRange(ITextSelection *me, LONG anchor, LONG active)
4992
{
4993
    struct text_selection *This = impl_from_ITextSelection(me);
4994

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

4997
    if (!This->services)
4998 4999 5000 5001 5002
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5003
static HRESULT WINAPI ITextSelection_fnInRange(ITextSelection *me, ITextRange *range, LONG *ret)
5004
{
5005
    struct text_selection *This = impl_from_ITextSelection(me);
5006 5007 5008 5009 5010 5011 5012 5013
    ITextSelection *selection = NULL;
    LONG start, end;

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

    if (ret)
        *ret = tomFalse;

5014
    if (!This->services)
5015 5016
        return CO_E_RELEASED;

5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027
    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);
5028 5029
}

5030
static HRESULT WINAPI ITextSelection_fnInStory(ITextSelection *me, ITextRange *range, LONG *ret)
5031
{
5032
    struct text_selection *This = impl_from_ITextSelection(me);
5033 5034 5035

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

5036
    if (!This->services)
5037 5038 5039 5040 5041
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5042
static HRESULT WINAPI ITextSelection_fnIsEqual(ITextSelection *me, ITextRange *range, LONG *ret)
5043
{
5044
    struct text_selection *This = impl_from_ITextSelection(me);
5045 5046 5047 5048 5049 5050 5051 5052
    ITextSelection *selection = NULL;
    LONG start, end;

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

    if (ret)
        *ret = tomFalse;

5053
    if (!This->services)
5054 5055
        return CO_E_RELEASED;

5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066
    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);
5067 5068
}

5069
static HRESULT WINAPI ITextSelection_fnSelect(ITextSelection *me)
5070
{
5071
    struct text_selection *This = impl_from_ITextSelection(me);
5072 5073 5074

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

5075
    if (!This->services)
5076 5077
        return CO_E_RELEASED;

5078 5079
    /* nothing to do */
    return S_OK;
5080 5081
}

5082 5083
static HRESULT WINAPI ITextSelection_fnStartOf(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
5084
{
5085
    struct text_selection *This = impl_from_ITextSelection(me);
5086 5087
    ITextRange *range = NULL;
    HRESULT hr;
5088

5089
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, extend, delta);
5090

5091
    if (!This->services)
5092 5093
        return CO_E_RELEASED;

5094 5095 5096 5097
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
    hr = textrange_startof(range, unit, extend, delta);
    ITextRange_Release(range);
    return hr;
5098 5099
}

5100 5101
static HRESULT WINAPI ITextSelection_fnEndOf(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
5102
{
5103
    struct text_selection *This = impl_from_ITextSelection(me);
5104 5105
    ITextRange *range = NULL;
    HRESULT hr;
5106

5107
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, extend, delta);
5108

5109
    if (!This->services)
5110 5111
        return CO_E_RELEASED;

5112
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
5113
    hr = textrange_endof(range, This->services->editor, unit, extend, delta);
5114 5115
    ITextRange_Release(range);
    return hr;
5116 5117
}

5118
static HRESULT WINAPI ITextSelection_fnMove(ITextSelection *me, LONG unit, LONG count, LONG *delta)
5119
{
5120
    struct text_selection *This = impl_from_ITextSelection(me);
5121 5122
    ITextRange *range = NULL;
    HRESULT hr;
5123

5124
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, count, delta);
5125

5126
    if (!This->services)
5127 5128
        return CO_E_RELEASED;

5129
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
5130
    hr = textrange_movestart(range, This->services->editor, unit, count, delta);
5131 5132
    ITextRange_Release(range);
    return hr;
5133 5134
}

5135 5136
static HRESULT WINAPI ITextSelection_fnMoveStart(ITextSelection *me, LONG unit, LONG count,
    LONG *delta)
5137
{
5138
    struct text_selection *This = impl_from_ITextSelection(me);
5139 5140
    ITextRange *range = NULL;
    HRESULT hr;
5141

5142
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, count, delta);
5143

5144
    if (!This->services)
5145 5146
        return CO_E_RELEASED;

5147
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
5148
    hr = textrange_movestart(range, This->services->editor, unit, count, delta);
5149 5150
    ITextRange_Release(range);
    return hr;
5151 5152
}

5153 5154
static HRESULT WINAPI ITextSelection_fnMoveEnd(ITextSelection *me, LONG unit, LONG count,
    LONG *delta)
5155
{
5156
    struct text_selection *This = impl_from_ITextSelection(me);
5157 5158
    ITextRange *range = NULL;
    HRESULT hr;
5159

5160
    TRACE("(%p)->(%ld %ld %p)\n", This, unit, count, delta);
5161

5162
    if (!This->services)
5163 5164
        return CO_E_RELEASED;

5165
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
5166
    hr = textrange_moveend(range, This->services->editor, unit, count, delta);
5167 5168
    ITextRange_Release(range);
    return hr;
5169 5170
}

5171 5172
static HRESULT WINAPI ITextSelection_fnMoveWhile(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5173
{
5174
    struct text_selection *This = impl_from_ITextSelection(me);
5175

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

5178
    if (!This->services)
5179 5180 5181 5182 5183
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5184 5185
static HRESULT WINAPI ITextSelection_fnMoveStartWhile(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5186
{
5187
    struct text_selection *This = impl_from_ITextSelection(me);
5188

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

5191
    if (!This->services)
5192 5193 5194 5195 5196
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5197 5198
static HRESULT WINAPI ITextSelection_fnMoveEndWhile(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5199
{
5200
    struct text_selection *This = impl_from_ITextSelection(me);
5201

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

5204
    if (!This->services)
5205 5206 5207 5208 5209
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5210 5211
static HRESULT WINAPI ITextSelection_fnMoveUntil(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5212
{
5213
    struct text_selection *This = impl_from_ITextSelection(me);
5214

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

5217
    if (!This->services)
5218 5219 5220 5221 5222
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5223 5224
static HRESULT WINAPI ITextSelection_fnMoveStartUntil(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5225
{
5226
    struct text_selection *This = impl_from_ITextSelection(me);
5227

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

5230
    if (!This->services)
5231 5232 5233 5234 5235
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5236 5237
static HRESULT WINAPI ITextSelection_fnMoveEndUntil(ITextSelection *me, VARIANT *charset, LONG count,
    LONG *delta)
5238
{
5239
    struct text_selection *This = impl_from_ITextSelection(me);
5240

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

5243
    if (!This->services)
5244 5245 5246 5247 5248
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5249 5250
static HRESULT WINAPI ITextSelection_fnFindText(ITextSelection *me, BSTR text, LONG count, LONG flags,
    LONG *length)
5251
{
5252
    struct text_selection *This = impl_from_ITextSelection(me);
5253

5254
    FIXME("(%p)->(%s %ld %lx %p): stub\n", This, debugstr_w(text), count, flags, length);
5255

5256
    if (!This->services)
5257 5258 5259 5260 5261 5262
        return CO_E_RELEASED;

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

5263 5264
static HRESULT WINAPI ITextSelection_fnFindTextStart(ITextSelection *me, BSTR text, LONG count,
    LONG flags, LONG *length)
5265
{
5266
    struct text_selection *This = impl_from_ITextSelection(me);
5267

5268
    FIXME("(%p)->(%s %ld %lx %p): stub\n", This, debugstr_w(text), count, flags, length);
5269

5270
    if (!This->services)
5271 5272 5273 5274 5275
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5276 5277
static HRESULT WINAPI ITextSelection_fnFindTextEnd(ITextSelection *me, BSTR text, LONG count,
    LONG flags, LONG *length)
5278
{
5279
    struct text_selection *This = impl_from_ITextSelection(me);
5280

5281
    FIXME("(%p)->(%s %ld %lx %p): stub\n", This, debugstr_w(text), count, flags, length);
5282

5283
    if (!This->services)
5284 5285 5286 5287 5288
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5289 5290
static HRESULT WINAPI ITextSelection_fnDelete(ITextSelection *me, LONG unit, LONG count,
    LONG *delta)
5291
{
5292
    struct text_selection *This = impl_from_ITextSelection(me);
5293

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

5296
    if (!This->services)
5297 5298 5299 5300 5301
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5302
static HRESULT WINAPI ITextSelection_fnCut(ITextSelection *me, VARIANT *v)
5303
{
5304
    struct text_selection *This = impl_from_ITextSelection(me);
5305 5306
    ITextRange *range = NULL;
    HRESULT hr;
5307

5308
    TRACE("(%p)->(%p): stub\n", This, v);
5309

5310
    if (!This->services)
5311 5312
        return CO_E_RELEASED;

5313
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
5314
    hr = textrange_copy_or_cut(range, This->services->editor, TRUE, v);
5315 5316
    ITextRange_Release(range);
    return hr;
5317 5318
}

5319
static HRESULT WINAPI ITextSelection_fnCopy(ITextSelection *me, VARIANT *v)
5320
{
5321
    struct text_selection *This = impl_from_ITextSelection(me);
5322 5323
    ITextRange *range = NULL;
    HRESULT hr;
5324

5325
    TRACE("(%p)->(%p)\n", This, v);
5326

5327
    if (!This->services)
5328 5329
        return CO_E_RELEASED;

5330
    ITextSelection_QueryInterface(me, &IID_ITextRange, (void**)&range);
5331
    hr = textrange_copy_or_cut(range, This->services->editor, FALSE, v);
5332 5333
    ITextRange_Release(range);
    return hr;
5334 5335
}

5336
static HRESULT WINAPI ITextSelection_fnPaste(ITextSelection *me, VARIANT *v, LONG format)
5337
{
5338
    struct text_selection *This = impl_from_ITextSelection(me);
5339

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

5342
    if (!This->services)
5343 5344 5345 5346 5347
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5348 5349
static HRESULT WINAPI ITextSelection_fnCanPaste(ITextSelection *me, VARIANT *v, LONG format,
    LONG *ret)
5350
{
5351
    struct text_selection *This = impl_from_ITextSelection(me);
5352

5353
    FIXME("(%p)->(%s %lx %p): stub\n", This, debugstr_variant(v), format, ret);
5354

5355
    if (!This->services)
5356 5357 5358 5359 5360
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5361
static HRESULT WINAPI ITextSelection_fnCanEdit(ITextSelection *me, LONG *ret)
5362
{
5363
    struct text_selection *This = impl_from_ITextSelection(me);
5364 5365 5366

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

5367
    if (!This->services)
5368 5369 5370 5371 5372
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5373
static HRESULT WINAPI ITextSelection_fnChangeCase(ITextSelection *me, LONG type)
5374
{
5375
    struct text_selection *This = impl_from_ITextSelection(me);
5376

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

5379
    if (!This->services)
5380 5381 5382 5383 5384
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5385
static HRESULT WINAPI ITextSelection_fnGetPoint(ITextSelection *me, LONG type, LONG *cx, LONG *cy)
5386
{
5387
    struct text_selection *This = impl_from_ITextSelection(me);
5388

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

5391
    if (!This->services)
5392 5393 5394 5395 5396
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5397 5398
static HRESULT WINAPI ITextSelection_fnSetPoint(ITextSelection *me, LONG x, LONG y, LONG type,
    LONG extend)
5399
{
5400
    struct text_selection *This = impl_from_ITextSelection(me);
5401

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

5404
    if (!This->services)
5405 5406 5407 5408 5409
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5410
static HRESULT WINAPI ITextSelection_fnScrollIntoView(ITextSelection *me, LONG value)
5411
{
5412
    struct text_selection *This = impl_from_ITextSelection(me);
5413

5414
    FIXME("(%p)->(%ld): stub\n", This, value);
5415

5416
    if (!This->services)
5417 5418 5419 5420 5421
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5422
static HRESULT WINAPI ITextSelection_fnGetEmbeddedObject(ITextSelection *me, IUnknown **ppv)
5423
{
5424
    struct text_selection *This = impl_from_ITextSelection(me);
5425 5426 5427

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

5428
    if (!This->services)
5429 5430 5431 5432 5433 5434
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

/*** ITextSelection methods ***/
5435
static HRESULT WINAPI ITextSelection_fnGetFlags(ITextSelection *me, LONG *flags)
5436
{
5437
    struct text_selection *This = impl_from_ITextSelection(me);
5438 5439 5440

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

5441
    if (!This->services)
5442 5443 5444 5445 5446
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5447
static HRESULT WINAPI ITextSelection_fnSetFlags(ITextSelection *me, LONG flags)
5448
{
5449
    struct text_selection *This = impl_from_ITextSelection(me);
5450

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

5453
    if (!This->services)
5454 5455 5456 5457 5458
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5459
static HRESULT WINAPI ITextSelection_fnGetType(ITextSelection *me, LONG *type)
5460
{
5461
    struct text_selection *This = impl_from_ITextSelection(me);
5462 5463 5464

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

5465
    if (!This->services)
5466 5467 5468 5469 5470
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5471 5472
static HRESULT WINAPI ITextSelection_fnMoveLeft(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5473
{
5474
    struct text_selection *This = impl_from_ITextSelection(me);
5475

5476
    FIXME("(%p)->(%ld %ld %ld %p): stub\n", This, unit, count, extend, delta);
5477

5478
    if (!This->services)
5479 5480 5481 5482 5483
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5484 5485
static HRESULT WINAPI ITextSelection_fnMoveRight(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5486
{
5487
    struct text_selection *This = impl_from_ITextSelection(me);
5488

5489
    FIXME("(%p)->(%ld %ld %ld %p): stub\n", This, unit, count, extend, delta);
5490

5491
    if (!This->services)
5492 5493 5494 5495 5496
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5497 5498
static HRESULT WINAPI ITextSelection_fnMoveUp(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5499
{
5500
    struct text_selection *This = impl_from_ITextSelection(me);
5501

5502
    FIXME("(%p)->(%ld %ld %ld %p): stub\n", This, unit, count, extend, delta);
5503

5504
    if (!This->services)
5505 5506 5507 5508 5509
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5510 5511
static HRESULT WINAPI ITextSelection_fnMoveDown(ITextSelection *me, LONG unit, LONG count,
    LONG extend, LONG *delta)
5512
{
5513
    struct text_selection *This = impl_from_ITextSelection(me);
5514

5515
    FIXME("(%p)->(%ld %ld %ld %p): stub\n", This, unit, count, extend, delta);
5516

5517
    if (!This->services)
5518 5519 5520 5521 5522
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5523 5524
static HRESULT WINAPI ITextSelection_fnHomeKey(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
5525
{
5526
    struct text_selection *This = impl_from_ITextSelection(me);
5527

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

5530
    if (!This->services)
5531 5532 5533 5534 5535
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5536 5537
static HRESULT WINAPI ITextSelection_fnEndKey(ITextSelection *me, LONG unit, LONG extend,
    LONG *delta)
5538
{
5539
    struct text_selection *This = impl_from_ITextSelection(me);
5540

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

5543
    if (!This->services)
5544 5545 5546 5547 5548
        return CO_E_RELEASED;

    return E_NOTIMPL;
}

5549
static HRESULT WINAPI ITextSelection_fnTypeText(ITextSelection *me, BSTR text)
5550
{
5551
    struct text_selection *This = impl_from_ITextSelection(me);
5552 5553 5554

    FIXME("(%p)->(%s): stub\n", This, debugstr_w(text));

5555
    if (!This->services)
5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631
        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
};

5632
static struct text_selection *text_selection_create(struct text_services *services)
5633
{
5634
    struct text_selection *txtSel = heap_alloc(sizeof *txtSel);
5635 5636 5637
    if (!txtSel)
        return NULL;

5638
    txtSel->ITextSelection_iface.lpVtbl = &tsvt;
5639
    txtSel->ref = 1;
5640
    txtSel->services = services;
5641 5642 5643
    return txtSel;
}

5644
static void convert_sizel(const ME_Context *c, const SIZEL* szl, SIZE* sz)
5645 5646 5647 5648 5649 5650
{
  /* 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);
}

5651 5652 5653 5654 5655
/******************************************************************************
 * ME_GetOLEObjectSize
 *
 * Sets run extent for OLE objects.
 */
5656
void ME_GetOLEObjectSize(const ME_Context *c, ME_Run *run, SIZE *pSize)
5657 5658 5659 5660 5661 5662 5663 5664
{
  IDataObject*  ido;
  FORMATETC     fmt;
  STGMEDIUM     stgm;
  DIBSECTION    dibsect;
  ENHMETAHEADER emh;

  assert(run->nFlags & MERF_GRAPHICS);
5665
  assert(run->reobj);
5666

5667
  if (run->reobj->obj.sizel.cx != 0 || run->reobj->obj.sizel.cy != 0)
5668
  {
5669
    convert_sizel(c, &run->reobj->obj.sizel, pSize);
5670 5671 5672 5673 5674
    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);
    }
5675 5676 5677
    return;
  }

5678
  if (!run->reobj->obj.poleobj)
5679 5680 5681 5682 5683
  {
    pSize->cx = pSize->cy = 0;
    return;
  }

5684
  if (IOleObject_QueryInterface(run->reobj->obj.poleobj, &IID_IDataObject, (void**)&ido) != S_OK)
5685 5686 5687 5688 5689
  {
      FIXME("Query Interface IID_IDataObject failed!\n");
      pSize->cx = pSize->cy = 0;
      return;
  }
5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706
  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;
    }
  }
5707
  IDataObject_Release(ido);
5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721

  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:
5722
    FIXME("Unsupported tymed %ld\n", stgm.tymed);
5723 5724
    break;
  }
5725
  ReleaseStgMedium(&stgm);
5726
  if (c->editor->nZoomNumerator != 0)
5727
  {
5728 5729
    pSize->cx = MulDiv(pSize->cx, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
    pSize->cy = MulDiv(pSize->cy, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
5730
  }
5731 5732
}

5733
void draw_ole( ME_Context *c, int x, int y, ME_Run *run, BOOL selected )
5734 5735 5736 5737 5738 5739 5740
{
  IDataObject*  ido;
  FORMATETC     fmt;
  STGMEDIUM     stgm;
  DIBSECTION    dibsect;
  ENHMETAHEADER emh;
  HDC           hMemDC;
5741
  SIZE          sz;
5742
  BOOL          has_size;
5743
  HBITMAP       old_bm;
5744
  RECT          rc;
5745 5746

  assert(run->nFlags & MERF_GRAPHICS);
5747 5748
  assert(run->reobj);
  if (IOleObject_QueryInterface(run->reobj->obj.poleobj, &IID_IDataObject, (void**)&ido) != S_OK)
5749 5750 5751 5752
  {
    FIXME("Couldn't get interface\n");
    return;
  }
5753
  has_size = run->reobj->obj.sizel.cx != 0 || run->reobj->obj.sizel.cy != 0;
5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769
  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;
    }
  }
5770 5771
  IDataObject_Release(ido);

5772 5773 5774 5775 5776
  switch (stgm.tymed)
  {
  case TYMED_GDI:
    GetObjectW(stgm.u.hBitmap, sizeof(dibsect), &dibsect);
    hMemDC = CreateCompatibleDC(c->hDC);
5777
    old_bm = SelectObject(hMemDC, stgm.u.hBitmap);
5778
    if (has_size)
5779
    {
5780
      convert_sizel(c, &run->reobj->obj.sizel, &sz);
5781
    } else {
5782 5783
      sz.cx = dibsect.dsBm.bmWidth;
      sz.cy = dibsect.dsBm.bmHeight;
5784 5785 5786 5787 5788
    }
    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);
5789
    }
5790 5791 5792
    StretchBlt(c->hDC, x, y - sz.cy, sz.cx, sz.cy,
               hMemDC, 0, 0, dibsect.dsBm.bmWidth, dibsect.dsBm.bmHeight, SRCCOPY);

5793
    SelectObject(hMemDC, old_bm);
5794
    DeleteDC(hMemDC);
5795 5796 5797
    break;
  case TYMED_ENHMF:
    GetEnhMetaFileHeader(stgm.u.hEnhMetaFile, sizeof(emh), &emh);
5798
    if (has_size)
5799
    {
5800
      convert_sizel(c, &run->reobj->obj.sizel, &sz);
5801
    } else {
5802 5803
      sz.cx = emh.rclBounds.right - emh.rclBounds.left;
      sz.cy = emh.rclBounds.bottom - emh.rclBounds.top;
5804
    }
5805
    if (c->editor->nZoomNumerator != 0)
5806
    {
5807 5808
      sz.cx = MulDiv(sz.cx, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
      sz.cy = MulDiv(sz.cy, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
5809
    }
5810

5811 5812 5813 5814 5815
    rc.left = x;
    rc.top = y - sz.cy;
    rc.right = x + sz.cx;
    rc.bottom = y;
    PlayEnhMetaFile(c->hDC, stgm.u.hEnhMetaFile, &rc);
5816 5817
    break;
  default:
5818
    FIXME("Unsupported tymed %ld\n", stgm.tymed);
5819
    selected = FALSE;
5820 5821
    break;
  }
5822 5823
  ReleaseStgMedium(&stgm);

5824 5825
  if (selected && !c->editor->bHideSelection)
    PatBlt(c->hDC, x, y - sz.cy, sz.cx, sz.cy, DSTINVERT);
5826 5827
}

5828
void ME_DeleteReObject(struct re_object *reobj)
5829
{
5830 5831 5832 5833
    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);
5834 5835
}

5836
void ME_CopyReObject(REOBJECT *dst, const REOBJECT *src, DWORD flags)
5837 5838
{
    *dst = *src;
5839 5840 5841
    dst->poleobj = NULL;
    dst->pstg = NULL;
    dst->polesite = NULL;
5842

5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857
    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);
    }
5858
}
5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876

void richole_release_children( struct text_services *services )
{
    ITextRangeImpl *range;
    IOleClientSiteImpl *site;

    if (services->text_selection)
    {
        services->text_selection->services = NULL;
        ITextSelection_Release( &services->text_selection->ITextSelection_iface );
    }

    LIST_FOR_EACH_ENTRY( range, &services->rangelist, ITextRangeImpl, child.entry )
        range->child.reole = NULL;

    LIST_FOR_EACH_ENTRY( site, &services->clientsites, IOleClientSiteImpl, child.entry )
        site->child.reole = NULL;
}