paint.c 46.3 KB
Newer Older
1 2 3 4
/*
 * RichEdit - painting functions
 *
 * Copyright 2004 by Krzysztof Foltman
5
 * Copyright 2005 by Phil Krylov
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 23 24 25
 */

#include "editor.h"

WINE_DEFAULT_DEBUG_CHANNEL(richedit);

26 27
static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph);

28
void ME_PaintContent(ME_TextEditor *editor, HDC hDC, const RECT *rcUpdate)
29
{
30 31
  ME_DisplayItem *item;
  ME_Context c;
32
  int ys, ye;
33 34 35 36 37 38 39 40 41 42
  HRGN oldRgn;

  oldRgn = CreateRectRgn(0, 0, 0, 0);
  if (!GetClipRgn(hDC, oldRgn))
  {
    DeleteObject(oldRgn);
    oldRgn = NULL;
  }
  IntersectClipRect(hDC, rcUpdate->left, rcUpdate->top,
                     rcUpdate->right, rcUpdate->bottom);
43 44 45

  ME_InitContext(&c, editor, hDC);
  SetBkMode(hDC, TRANSPARENT);
46

47
  item = editor->pBuffer->pFirst->next;
48 49
  /* This context point is an offset for the paragraph positions stored
   * during wrapping. It shouldn't be modified during painting. */
50 51
  c.pt.x = c.rcView.left - editor->horz_si.nPos;
  c.pt.y = c.rcView.top - editor->vert_si.nPos;
52 53
  while(item != editor->pBuffer->pLast)
  {
54
    assert(item->type == diParagraph);
55 56

    ys = c.pt.y + item->member.para.pt.y;
57 58 59 60 61
    if (item->member.para.pCell
        != item->member.para.next_para->member.para.pCell)
    {
      ME_Cell *cell = NULL;
      cell = &ME_FindItemBack(item->member.para.next_para, diCell)->member.cell;
62
      ye = c.pt.y + cell->pt.y + cell->nHeight;
63
    } else {
64
      ye = ys + item->member.para.nHeight;
65
    }
66
    if (item->member.para.pCell && !(item->member.para.nFlags & MEPF_ROWEND) &&
67 68 69
        item->member.para.pCell != item->member.para.prev_para->member.para.pCell)
    {
      /* the border shifts the text down */
70
      ys -= item->member.para.pCell->member.cell.yTextOffset;
71
    }
72

73 74 75
    /* Draw the paragraph if any of the paragraph is in the update region. */
    if (ys < rcUpdate->bottom && ye > rcUpdate->top)
      ME_DrawParagraph(&c, item);
76 77
    item = item->member.para.next_para;
  }
78
  if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
79 80
  {
    /* Fill space after the end of the text. */
81 82 83 84 85
    RECT rc;
    rc.top = c.pt.y + editor->nTotalLength;
    rc.left = c.rcView.left;
    rc.bottom = c.rcView.bottom;
    rc.right = c.rcView.right;
86

87
    IntersectRect(&rc, &rc, rcUpdate);
88

89
    if (!IsRectEmpty(&rc))
90
      PatBlt(hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
91
  }
92 93
  if (editor->nTotalLength != editor->nLastTotalLength ||
      editor->nTotalWidth != editor->nLastTotalWidth)
94
    ME_SendRequestResize(editor, FALSE);
95
  editor->nLastTotalLength = editor->nTotalLength;
96
  editor->nLastTotalWidth = editor->nTotalWidth;
97 98 99 100

  SelectClipRgn(hDC, oldRgn);
  if (oldRgn)
    DeleteObject(oldRgn);
101 102 103

  c.hDC = NULL;
  ME_DestroyContext(&c);
104 105 106 107
}

void ME_Repaint(ME_TextEditor *editor)
{
108
  if (ME_WrapMarkedParagraphs(editor))
109
  {
110 111
    ME_UpdateScrollBar(editor);
    FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
112
  }
113
  ITextHost_TxViewChange(editor->texthost, TRUE);
114 115
}

116
void ME_UpdateRepaint(ME_TextEditor *editor, BOOL update_now)
117
{
118
  /* Should be called whenever the contents of the control have changed */
119
  BOOL wrappedParagraphs;
120

121 122
  wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
  if (wrappedParagraphs)
123
    ME_UpdateScrollBar(editor);
124

125
  /* Ensure that the cursor is visible */
126
  ME_EnsureVisible(editor, &editor->pCursors[0]);
127

128 129
  ITextHost_TxViewChange(editor->texthost, update_now);

130 131
  ME_SendSelChange(editor);

132 133 134
  /* send EN_CHANGE if the event mask asks for it */
  if(editor->nEventMask & ENM_CHANGE)
  {
135
    editor->nEventMask &= ~ENM_CHANGE;
136
    ME_SendOldNotify(editor, EN_CHANGE);
137
    editor->nEventMask |= ENM_CHANGE;
138
  }
139 140
}

141 142
void
ME_RewrapRepaint(ME_TextEditor *editor)
143
{
144 145 146
  /* RewrapRepaint should be called whenever the control has changed in
   * looks, but not content. Like resizing. */
  
147
  ME_MarkAllForWrapping(editor);
148 149
  ME_WrapMarkedParagraphs(editor);
  ME_UpdateScrollBar(editor);
150
  ME_Repaint(editor);
151 152
}

153
int ME_twips2pointsX(const ME_Context *c, int x)
154 155
{
  if (c->editor->nZoomNumerator == 0)
156
    return x * c->dpi.cx / 1440;
157
  else
158 159 160
    return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
}

161
int ME_twips2pointsY(const ME_Context *c, int y)
162 163 164 165 166
{
  if (c->editor->nZoomNumerator == 0)
    return y * c->dpi.cy / 1440;
  else
    return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
167
}
168

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

static int calc_y_offset( const ME_Context *c, ME_Style *style )
{
    int offs = 0, twips = 0;

    if ((style->fmt.dwMask & style->fmt.dwEffects) & CFM_OFFSET)
        twips = style->fmt.yOffset;

    if ((style->fmt.dwMask & style->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT))
    {
        if (style->fmt.dwEffects & CFE_SUPERSCRIPT) twips = style->fmt.yHeight/3;
        if (style->fmt.dwEffects & CFE_SUBSCRIPT) twips = -style->fmt.yHeight/12;
    }

    if (twips) offs = ME_twips2pointsY( c, twips );

    return offs;
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
static COLORREF get_text_color( ME_Context *c, ME_Style *style, BOOL highlight )
{
    COLORREF color;

    if (highlight)
        color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHTTEXT );
    else if ((style->fmt.dwMask & CFM_LINK) && (style->fmt.dwEffects & CFE_LINK))
        color = RGB(0,0,255);
    else if ((style->fmt.dwMask & CFM_COLOR) && (style->fmt.dwEffects & CFE_AUTOCOLOR))
        color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_WINDOWTEXT );
    else
        color = style->fmt.crTextColor;

    return color;
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
static COLORREF get_back_color( ME_Context *c, ME_Style *style, BOOL highlight )
{
    COLORREF color;

    if (highlight)
        color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );
    else if ( (style->fmt.dwMask & CFM_BACKCOLOR)
            && !(style->fmt.dwEffects & CFE_AUTOBACKCOLOR) )
        color = style->fmt.crBackColor;
    else
        color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_WINDOW );

    return color;
}

219
static HPEN get_underline_pen( ME_Style *style, COLORREF color )
220
{
221 222 223
    if (style->fmt.dwEffects & CFE_LINK)
        return CreatePen( PS_SOLID, 1, color );

224
    /* Choose the pen type for underlining the text. */
225
    if (style->fmt.dwEffects & CFE_UNDERLINE)
226 227 228 229 230 231
    {
        switch (style->fmt.bUnderlineType)
        {
        case CFU_UNDERLINE:
        case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
        case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
232
            return CreatePen( PS_SOLID, 1, color );
233
        case CFU_UNDERLINEDOTTED:
234
            return CreatePen( PS_DOT, 1, color );
235 236 237 238 239 240 241 242
        default:
            FIXME( "Unknown underline type (%u)\n", style->fmt.bUnderlineType );
            /* fall through */
        case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
        case CFU_UNDERLINENONE:
            break;
        }
    }
243
    return NULL;
244 245
}

246 247 248 249
static void draw_underline( ME_Context *c, ME_Run *run, int x, int y, COLORREF color )
{
    HPEN pen;

250
    pen = get_underline_pen( run->style, color );
251 252 253 254 255 256 257 258 259 260 261
    if (pen)
    {
        HPEN old_pen = SelectObject( c->hDC, pen );
        MoveToEx( c->hDC, x, y + 1, NULL );
        LineTo( c->hDC, x + run->nWidth, y + 1 );
        SelectObject( c->hDC, old_pen );
        DeleteObject( pen );
    }
    return;
}

262 263 264 265 266 267 268 269 270
/*********************************************************************
 *  draw_space
 *
 * Draw the end-of-paragraph or tab space.
 *
 * If actually_draw is TRUE then ensure any underline is drawn.
 */
static void draw_space( ME_Context *c, ME_Run *run, int x, int y,
                        BOOL selected, BOOL actually_draw, int ymin, int cy )
271
{
272 273
    HDC hdc = c->hDC;
    BOOL old_style_selected = FALSE;
274
    RECT rect;
275 276 277 278
    COLORREF back_color = 0;

    SetRect( &rect, x, ymin, x + run->nWidth, ymin + cy );

279 280
    if (c->editor->bHideSelection || (!c->editor->bHaveFocus &&
                !(c->editor->styleFlags & ES_NOHIDESEL))) selected = FALSE;
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    if (c->editor->bEmulateVersion10)
    {
        old_style_selected = selected;
        selected = FALSE;
    }

    if (selected)
        back_color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );

    if (actually_draw)
    {
        COLORREF text_color = get_text_color( c, run->style, selected );
        COLORREF old_text, old_back;
        int y_offset = calc_y_offset( c, run->style );
        static const WCHAR space[1] = {' '};

297
        select_style( c, run->style );
298 299 300 301 302 303 304 305
        old_text = SetTextColor( hdc, text_color );
        if (selected) old_back = SetBkColor( hdc, back_color );

        ExtTextOutW( hdc, x, y - y_offset, selected ? ETO_OPAQUE : 0, &rect, space, 1, &run->nWidth );

        if (selected) SetBkColor( hdc, old_back );
        SetTextColor( hdc, old_text );

306
        draw_underline( c, run, x, y - y_offset, text_color );
307 308 309 310 311 312 313 314 315 316
    }
    else if (selected)
    {
        HBRUSH brush = CreateSolidBrush( back_color );
        FillRect( hdc, &rect, brush );
        DeleteObject( brush );
    }

    if (old_style_selected)
        PatBlt( hdc, x, ymin, run->nWidth, cy, DSTINVERT );
317 318
}

319 320
static void get_selection_rect( ME_Context *c, ME_Run *run, int from, int to, int cy, RECT *r )
{
321 322
    from = max( 0, from );
    to = min( run->len, to );
323
    r->left = ME_PointFromCharContext( c, run, from, TRUE );
324
    r->top = 0;
325
    r->right = ME_PointFromCharContext( c, run, to, TRUE );
326 327 328
    r->bottom = cy;
    return;
}
329

330
static void draw_text( ME_Context *c, ME_Run *run, int x, int y, BOOL selected, RECT *sel_rect )
331 332
{
    COLORREF text_color = get_text_color( c, run->style, selected );
333
    COLORREF back_color = get_back_color( c, run->style, selected );
334
    COLORREF old_text, old_back = 0;
335 336
    const WCHAR *text = get_text( run, 0 );
    ME_String *masked = NULL;
337 338 339 340
    const BOOL paint_bg = ( selected
        || ( ( run->style->fmt.dwMask & CFM_BACKCOLOR )
            && !(CFE_AUTOBACKCOLOR & run->style->fmt.dwEffects) )
        );
341 342 343 344 345 346

    if (c->editor->cPasswordMask)
    {
        masked = ME_MakeStringR( c->editor->cPasswordMask, run->len );
        text = masked->szData;
    }
347 348

    old_text = SetTextColor( c->hDC, text_color );
349
    if (paint_bg) old_back = SetBkColor( c->hDC, back_color );
350

351
    if (run->para->nFlags & MEPF_COMPLEX)
352
        ScriptTextOut( c->hDC, &run->style->script_cache, x, y, paint_bg ? ETO_OPAQUE : 0, sel_rect,
353 354 355
                       &run->script_analysis, NULL, 0, run->glyphs, run->num_glyphs, run->advances,
                       NULL, run->offsets );
    else
356
        ExtTextOutW( c->hDC, x, y, paint_bg ? ETO_OPAQUE : 0, sel_rect, text, run->len, NULL );
357

358
    if (paint_bg) SetBkColor( c->hDC, old_back );
359 360
    SetTextColor( c->hDC, old_text );

361
    draw_underline( c, run, x, y, text_color );
362

363
    ME_DestroyString( masked );
364 365 366 367
    return;
}


368
static void ME_DrawTextWithStyle(ME_Context *c, ME_Run *run, int x, int y,
369
                                 int nSelFrom, int nSelTo, int ymin, int cy)
370
{
371
  HDC hDC = c->hDC;
372
  int yOffset = 0;
373
  BOOL selected = (nSelFrom < run->len && nSelTo >= 0
374 375
                   && nSelFrom < nSelTo && !c->editor->bHideSelection &&
                   (c->editor->bHaveFocus || (c->editor->styleFlags & ES_NOHIDESEL)));
376
  BOOL old_style_selected = FALSE;
377
  RECT sel_rect;
378
  HRGN clip = NULL, sel_rgn = NULL;
379

380
  yOffset = calc_y_offset( c, run->style );
381

382
  if (selected)
383
  {
384 385
    get_selection_rect( c, run, nSelFrom, nSelTo, cy, &sel_rect );
    OffsetRect( &sel_rect, x, ymin );
386

387
    if (c->editor->bEmulateVersion10)
388
    {
389 390
      old_style_selected = TRUE;
      selected = FALSE;
391
    }
392
    else
393
    {
394 395 396 397 398 399 400
      sel_rgn = CreateRectRgnIndirect( &sel_rect );
      clip = CreateRectRgn( 0, 0, 0, 0 );
      if (GetClipRgn( hDC, clip ) != 1)
      {
        DeleteObject( clip );
        clip = NULL;
      }
401
    }
402 403
  }

404
  select_style( c, run->style );
405

406
  if (sel_rgn) ExtSelectClipRgn( hDC, sel_rgn, RGN_DIFF );
407 408 409 410 411 412 413 414 415 416 417 418

  if (!(run->style->fmt.dwEffects & CFE_AUTOBACKCOLOR)
      && (run->style->fmt.dwMask & CFM_BACKCOLOR) )
  {
    RECT tmp_rect;
    get_selection_rect( c, run, 0, run->len, cy, &tmp_rect );
    OffsetRect( &tmp_rect, x, ymin );
    draw_text( c, run, x, y - yOffset, FALSE, &tmp_rect );
  }
  else
    draw_text( c, run, x, y - yOffset, FALSE, NULL );

419
  if (sel_rgn)
420
  {
421 422
    ExtSelectClipRgn( hDC, clip, RGN_COPY );
    ExtSelectClipRgn( hDC, sel_rgn, RGN_AND );
423
    draw_text( c, run, x, y - yOffset, TRUE, &sel_rect );
424 425 426
    ExtSelectClipRgn( hDC, clip, RGN_COPY );
    if (clip) DeleteObject( clip );
    DeleteObject( sel_rgn );
427
  }
428 429 430

  if (old_style_selected)
    PatBlt( hDC, sel_rect.left, ymin, sel_rect.right - sel_rect.left, cy, DSTINVERT );
431 432
}

433
static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
434 435 436 437 438 439 440 441 442
  int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
  HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
  COLORREF color = SetTextColor(hDC, RGB(128,128,128));
  TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
  SelectObject(hDC, hFont);
  SetTextAlign(hDC, align);
  SetTextColor(hDC, color);
}

443 444
static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) 
{
445
  ME_Run *run = &rundi->member.run;
446
  ME_DisplayItem *start;
447
  int runofs = run->nCharOfs+para->nCharOfs;
448
  int nSelFrom, nSelTo;
449
  
450 451 452
  if (run->nFlags & MERF_HIDDEN)
    return;

453
  start = ME_FindItemBack(rundi, diStartRow);
454
  ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
455 456

  /* Draw selected end-of-paragraph mark */
457
  if (run->nFlags & MERF_ENDPARA)
458 459 460
  {
    if (runofs >= nSelFrom && runofs < nSelTo)
    {
461 462 463
      draw_space( c, run, x, y, TRUE, FALSE,
                  c->pt.y + para->pt.y + start->member.row.pt.y,
                  start->member.row.nHeight );
464
    }
465
    return;
466
  }
467

468
  if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
469
  {
470 471 472 473 474
    BOOL selected = runofs >= nSelFrom && runofs < nSelTo;

    draw_space( c, run, x, y, selected, TRUE,
                c->pt.y + para->pt.y + start->member.row.pt.y,
                start->member.row.nHeight );
475 476 477
    return;
  }

478
  if (run->nFlags & MERF_GRAPHICS)
479
    ME_DrawOLE(c, x, y, run, (runofs >= nSelFrom) && (runofs < nSelTo));
480
  else
481
  {
482 483 484
    ME_DrawTextWithStyle(c, run, x, y, nSelFrom - runofs, nSelTo - runofs,
                         c->pt.y + para->pt.y + start->member.row.pt.y,
                         start->member.row.nHeight);
485
  }
486 487
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
/* The documented widths are in points (72 dpi), but converting them to
 * 96 dpi (standard display resolution) avoids dealing with fractions. */
static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
  /* none */            {0, PS_SOLID, FALSE},
  /* 3/4 */             {1, PS_SOLID, FALSE},
  /* 1 1/2 */           {2, PS_SOLID, FALSE},
  /* 2 1/4 */           {3, PS_SOLID, FALSE},
  /* 3 */               {4, PS_SOLID, FALSE},
  /* 4 1/2 */           {6, PS_SOLID, FALSE},
  /* 6 */               {8, PS_SOLID, FALSE},
  /* 3/4 double */      {1, PS_SOLID, TRUE},
  /* 1 1/2 double */    {2, PS_SOLID, TRUE},
  /* 2 1/4 double */    {3, PS_SOLID, TRUE},
  /* 3/4 gray */        {1, PS_DOT /* FIXME */, FALSE},
  /* 1 1/2 dashed */    {2, PS_DASH, FALSE},
503 504
};

505
static const COLORREF pen_colors[16] = {
506 507 508 509 510 511 512 513 514 515
  /* Black */           RGB(0x00, 0x00, 0x00),  /* Blue */            RGB(0x00, 0x00, 0xFF),
  /* Cyan */            RGB(0x00, 0xFF, 0xFF),  /* Green */           RGB(0x00, 0xFF, 0x00),
  /* Magenta */         RGB(0xFF, 0x00, 0xFF),  /* Red */             RGB(0xFF, 0x00, 0x00),
  /* Yellow */          RGB(0xFF, 0xFF, 0x00),  /* White */           RGB(0xFF, 0xFF, 0xFF),
  /* Dark blue */       RGB(0x00, 0x00, 0x80),  /* Dark cyan */       RGB(0x00, 0x80, 0x80),
  /* Dark green */      RGB(0x00, 0x80, 0x80),  /* Dark magenta */    RGB(0x80, 0x00, 0x80),
  /* Dark red */        RGB(0x80, 0x00, 0x00),  /* Dark yellow */     RGB(0x80, 0x80, 0x00),
  /* Dark gray */       RGB(0x80, 0x80, 0x80),  /* Light gray */      RGB(0xc0, 0xc0, 0xc0),
};

516
static int ME_GetBorderPenWidth(const ME_Context* c, int idx)
517
{
518 519 520 521 522 523 524
  int width = border_details[idx].width;

  if (c->dpi.cx != 96)
    width = MulDiv(width, c->dpi.cx, 96);

  if (c->editor->nZoomNumerator != 0)
    width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
525 526 527 528

  return width;
}

529
int ME_GetParaBorderWidth(const ME_Context* c, int flags)
530 531 532 533
{
  int idx = (flags >> 8) & 0xF;
  int width;

534
  if (idx >= ARRAY_SIZE(border_details))
535 536 537 538
  {
      FIXME("Unsupported border value %d\n", idx);
      return 0;
  }
539
  width = ME_GetBorderPenWidth(c, idx);
540 541 542 543
  if (border_details[idx].dble) width = width * 2 + 1;
  return width;
}

544
static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
545
{
546
  int           idx, border_width, top_border, bottom_border;
547
  RECT          rc;
548
  BOOL          hasParaBorder;
549

550
  SetRectEmpty(bounds);
551
  if (!(para->fmt.dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
552 553

  border_width = top_border = bottom_border = 0;
554
  idx = (para->fmt.wBorders >> 8) & 0xF;
555
  hasParaBorder = (!(c->editor->bEmulateVersion10 &&
556 557 558
                     para->fmt.dwMask & PFM_TABLE &&
                     para->fmt.wEffects & PFE_TABLE) &&
                   (para->fmt.dwMask & PFM_BORDER) &&
559
                    idx != 0 &&
560
                    (para->fmt.wBorders & 0xF));
561
  if (hasParaBorder)
562
  {
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
     * controls. It actually stores the paragraph or row border style. Although
     * the value isn't used for drawing, it is used for streaming out rich text.
     *
     * wBorders stores the border style for each side (top, left, bottom, right)
     * using nibble (4 bits) to store each border style.  The rich text format
     * control words, and their associated value are the following:
     *   \brdrdash       0
     *   \brdrdashsm     1
     *   \brdrdb         2
     *   \brdrdot        3
     *   \brdrhair       4
     *   \brdrs          5
     *   \brdrth         6
     *   \brdrtriple     7
     *
     * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
     * The mask corresponding to each side for the version are the following:
     *     mask       v1.0-3.0    v4.1
     *     0x000F     top         left
     *     0x00F0     left        top
     *     0x0F00     bottom      right
     *     0xF000     right       bottom
     */
587 588 589 590 591
    if (para->fmt.wBorders & 0x00B0)
      FIXME("Unsupported border flags %x\n", para->fmt.wBorders);
    border_width = ME_GetParaBorderWidth(c, para->fmt.wBorders);
    if (para->fmt.wBorders & 4)       top_border = border_width;
    if (para->fmt.wBorders & 8)       bottom_border = border_width;
592
  }
593

594
  if (para->fmt.dwMask & PFM_SPACEBEFORE)
595 596 597 598
  {
    rc.left = c->rcView.left;
    rc.right = c->rcView.right;
    rc.top = y;
599
    bounds->top = ME_twips2pointsY(c, para->fmt.dySpaceBefore);
600
    rc.bottom = y + bounds->top + top_border;
601
    PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
602
  }
603

604
  if (para->fmt.dwMask & PFM_SPACEAFTER)
605 606 607 608
  {
    rc.left = c->rcView.left;
    rc.right = c->rcView.right;
    rc.bottom = y + para->nHeight;
609
    bounds->bottom = ME_twips2pointsY(c, para->fmt.dySpaceAfter);
610
    rc.top = rc.bottom - bounds->bottom - bottom_border;
611
    PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
612 613
  }

614 615 616
  /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
   * but might support it in later versions. */
  if (hasParaBorder) {
617
    int         pen_width, rightEdge;
618 619 620 621
    COLORREF    pencr;
    HPEN        pen = NULL, oldpen = NULL;
    POINT       pt;

622
    if (para->fmt.wBorders & 64) /* autocolor */
623 624
      pencr = ITextHost_TxGetSysColor(c->editor->texthost,
                                      COLOR_WINDOWTEXT);
625
    else
626
      pencr = pen_colors[(para->fmt.wBorders >> 12) & 0xF];
627

628 629 630
    rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
                              c->editor->nTotalWidth);

631
    pen_width = ME_GetBorderPenWidth(c, idx);
632 633 634 635 636
    pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
    oldpen = SelectObject(c->hDC, pen);
    MoveToEx(c->hDC, 0, 0, &pt);

    /* before & after spaces are not included in border */
637 638

    /* helper to draw the double lines in case of corner */
639
#define DD(x)   ((para->fmt.wBorders & (x)) ? (pen_width + 1) : 0)
640

641
    if (para->fmt.wBorders & 1)
642
    {
643 644
      MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
      LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
645
      if (border_details[idx].dble) {
646
        rc.left = c->pt.x + 1;
647 648 649
        rc.right = rc.left + border_width;
        rc.top = y + bounds->top;
        rc.bottom = y + para->nHeight - bounds->bottom;
650
        PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
651 652
        MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
        LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
653
      }
654
      bounds->left += border_width;
655
    }
656
    if (para->fmt.wBorders & 2)
657
    {
658 659
      MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
      LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
660
      if (border_details[idx].dble) {
661 662
        rc.left = rightEdge - pen_width - 1;
        rc.right = rc.left + pen_width;
663 664
        rc.top = y + bounds->top;
        rc.bottom = y + para->nHeight - bounds->bottom;
665
        PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
666 667
        MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
        LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
668
      }
669
      bounds->right += border_width;
670
    }
671
    if (para->fmt.wBorders & 4)
672
    {
673 674
      MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
      LineTo(c->hDC, rightEdge, y + bounds->top);
675
      if (border_details[idx].dble) {
676 677
        MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
        LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
678
      }
679
      bounds->top += border_width;
680
    }
681
    if (para->fmt.wBorders & 8)
682
    {
683 684
      MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
      LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
685
      if (border_details[idx].dble) {
686 687
        MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
        LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
688
      }
689
      bounds->bottom += border_width;
690
    }
691
#undef DD
692 693 694 695

    MoveToEx(c->hDC, pt.x, pt.y, NULL);
    SelectObject(c->hDC, oldpen);
    DeleteObject(pen);
696 697 698
  }
}

699 700 701
static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
{
  ME_Paragraph *para = &paragraph->member.para;
702
  if (!c->editor->bEmulateVersion10) /* v4.1 */
703
  {
704 705 706 707 708 709 710 711 712 713 714 715 716
    if (para->pCell)
    {
      RECT rc;
      ME_Cell *cell = &para->pCell->member.cell;
      ME_DisplayItem *paraAfterRow;
      HPEN pen, oldPen;
      LOGBRUSH logBrush;
      HBRUSH brush;
      COLORREF color;
      POINT oldPt;
      int width;
      BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
      BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
717
      int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
718
      int bottom = (atBottom ?
719
                    c->pt.y + cell->pt.y + cell->nHeight :
720
                    top + para->nHeight + (atTop ? cell->yTextOffset : 0));
721
      rc.left = c->pt.x + cell->pt.x;
722 723 724 725 726 727 728
      rc.right = rc.left + cell->nWidth;
      if (atTop) {
        /* Erase gap before text if not all borders are the same height. */
        width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
        rc.top = top + width;
        width = cell->yTextOffset - width;
        rc.bottom = rc.top + width;
729 730
        if (width)
          PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
731 732
      }
      /* Draw cell borders.
733
       * The order borders are draw in is left, top, bottom, right in order
734 735 736 737 738 739 740
       * to be consistent with native richedit.  This is noticeable from the
       * overlap of borders of different colours. */
      if (!(para->nFlags & MEPF_ROWEND)) {
        rc.top = top;
        rc.bottom = bottom;
        if (cell->border.left.width > 0)
        {
741
          color = cell->border.left.colorRef;
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
          width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
        } else {
          color = RGB(192,192,192);
          width = 1;
        }
        logBrush.lbStyle = BS_SOLID;
        logBrush.lbColor = color;
        logBrush.lbHatch = 0;
        pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
                           width, &logBrush, 0, NULL);
        oldPen = SelectObject(c->hDC, pen);
        MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
        LineTo(c->hDC, rc.left, rc.bottom);
        SelectObject(c->hDC, oldPen);
        DeleteObject(pen);
        MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
      }

      if (atTop) {
        if (cell->border.top.width > 0)
        {
763
          brush = CreateSolidBrush(cell->border.top.colorRef);
764 765 766 767 768 769 770 771
          width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
        } else {
          brush = GetStockObject(LTGRAY_BRUSH);
          width = 1;
        }
        rc.top = top;
        rc.bottom = rc.top + width;
        FillRect(c->hDC, &rc, brush);
772 773
        if (cell->border.top.width > 0)
          DeleteObject(brush);
774 775 776 777 778 779 780 781 782 783 784 785
      }

      /* Draw the bottom border if at the last paragraph in the cell, and when
       * in the last row of the table. */
      if (atBottom) {
        int oldLeft = rc.left;
        width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
        paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
        if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
          ME_DisplayItem *nextEndCell;
          nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
          assert(nextEndCell && !nextEndCell->member.cell.next_cell);
786
          rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
787 788
          /* FIXME: Native draws FROM the bottom of the table rather than
           * TO the bottom of the table in this case, but just doing so here
789
           * will cause the next row to erase the border. */
790 791 792 793 794 795 796
          /*
          rc.top = bottom;
          rc.bottom = rc.top + width;
           */
        }
        if (rc.left < rc.right) {
          if (cell->border.bottom.width > 0) {
797
            brush = CreateSolidBrush(cell->border.bottom.colorRef);
798 799 800 801 802 803
          } else {
            brush = GetStockObject(LTGRAY_BRUSH);
          }
          rc.bottom = bottom;
          rc.top = rc.bottom - width;
          FillRect(c->hDC, &rc, brush);
804 805
          if (cell->border.bottom.width > 0)
            DeleteObject(brush);
806 807 808 809 810 811 812 813 814 815 816
        }
        rc.left = oldLeft;
      }

      /* Right border only drawn if at the end of the table row. */
      if (!cell->next_cell->member.cell.next_cell &&
          !(para->nFlags & MEPF_ROWSTART))
      {
        rc.top = top;
        rc.bottom = bottom;
        if (cell->border.right.width > 0) {
817
          color = cell->border.right.colorRef;
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
          width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
        } else {
          color = RGB(192,192,192);
          width = 1;
        }
        logBrush.lbStyle = BS_SOLID;
        logBrush.lbColor = color;
        logBrush.lbHatch = 0;
        pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
                           width, &logBrush, 0, NULL);
        oldPen = SelectObject(c->hDC, pen);
        MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
        LineTo(c->hDC, rc.right - 1, rc.bottom);
        SelectObject(c->hDC, oldPen);
        DeleteObject(pen);
        MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
      }
    }
  } else { /* v1.0 - 3.0 */
    /* Draw simple table border */
838
    if (para->fmt.dwMask & PFM_TABLE && para->fmt.wEffects & PFE_TABLE) {
839 840 841 842 843
      HPEN pen = NULL, oldpen = NULL;
      int i, firstX, startX, endX, rowY, rowBottom, nHeight;
      POINT oldPt;
      PARAFORMAT2 *pNextFmt;

844
      pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
845 846 847
      oldpen = SelectObject(c->hDC, pen);

      /* Find the start relative to the text */
848
      firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
849
      /* Go back by the horizontal gap, which is stored in dxOffset */
850
      firstX -= ME_twips2pointsX(c, para->fmt.dxOffset);
851
      /* The left edge, stored in dxStartIndent affected just the first edge */
852
      startX = firstX - ME_twips2pointsX(c, para->fmt.dxStartIndent);
853
      rowY = c->pt.y + para->pt.y;
854 855
      if (para->fmt.dwMask & PFM_SPACEBEFORE)
        rowY += ME_twips2pointsY(c, para->fmt.dySpaceBefore);
856 857 858 859 860
      nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
      rowBottom = rowY + nHeight;

      /* Draw horizontal lines */
      MoveToEx(c->hDC, firstX, rowY, &oldPt);
861 862
      i = para->fmt.cTabCount - 1;
      endX = startX + ME_twips2pointsX(c, para->fmt.rgxTabs[i] & 0x00ffffff) + 1;
863
      LineTo(c->hDC, endX, rowY);
864
      pNextFmt = &para->next_para->member.para.fmt;
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
      /* The bottom of the row only needs to be drawn if the next row is
       * not a table. */
      if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
            para->nRows == 1))
      {
        /* Decrement rowBottom to draw the bottom line within the row, and
         * to not draw over this line when drawing the vertical lines. */
        rowBottom--;
        MoveToEx(c->hDC, firstX, rowBottom, NULL);
        LineTo(c->hDC, endX, rowBottom);
      }

      /* Draw vertical lines */
      MoveToEx(c->hDC, firstX, rowY, NULL);
      LineTo(c->hDC, firstX, rowBottom);
880
      for (i = 0; i < para->fmt.cTabCount; i++)
881
      {
882
        int rightBoundary = para->fmt.rgxTabs[i] & 0x00ffffff;
883 884 885 886 887 888 889 890 891 892 893 894
        endX = startX + ME_twips2pointsX(c, rightBoundary);
        MoveToEx(c->hDC, endX, rowY, NULL);
        LineTo(c->hDC, endX, rowBottom);
      }

      MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
      SelectObject(c->hDC, oldpen);
      DeleteObject(pen);
    }
  }
}

895 896 897 898
static void draw_para_number( ME_Context *c, ME_DisplayItem *p )
{
    ME_Paragraph *para = &p->member.para;
    int x, y;
899
    COLORREF old_text;
900 901 902

    if (para->fmt.wNumbering)
    {
903
        select_style( c, para->para_num.style );
904
        old_text = SetTextColor( c->hDC, get_text_color( c, para->para_num.style, FALSE ) );
905 906 907 908 909 910

        x = c->pt.x + para->para_num.pt.x;
        y = c->pt.y + para->pt.y + para->para_num.pt.y;

        ExtTextOutW( c->hDC, x, y, 0, NULL, para->para_num.text->szData, para->para_num.text->nLen, NULL );

911
        SetTextColor( c->hDC, old_text );
912 913 914
    }
}

915
static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
916
{
917 918 919 920
  int align = SetTextAlign(c->hDC, TA_BASELINE);
  ME_DisplayItem *p;
  ME_Run *run;
  ME_Paragraph *para = NULL;
921
  RECT rc, bounds;
922
  int y;
923
  int height = 0, baseline = 0, no=0;
924
  BOOL visible = FALSE;
925

926
  rc.left = c->pt.x;
927
  rc.right = c->rcView.right;
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946

  assert(paragraph);
  para = &paragraph->member.para;
  y = c->pt.y + para->pt.y;
  if (para->pCell)
  {
    ME_Cell *cell = &para->pCell->member.cell;
    rc.left = c->pt.x + cell->pt.x;
    rc.right = rc.left + cell->nWidth;
  }
  if (para->nFlags & MEPF_ROWSTART) {
    ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
    rc.right = c->pt.x + cell->pt.x;
  } else if (para->nFlags & MEPF_ROWEND) {
    ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
    rc.left = c->pt.x + cell->pt.x + cell->nWidth;
  }
  ME_DrawParaDecoration(c, para, y, &bounds);
  y += bounds.top;
947 948 949 950 951 952
  if (bounds.left || bounds.right) {
    rc.left = max(rc.left, c->pt.x + bounds.left);
    rc.right = min(rc.right, c->pt.x - bounds.right
                             + max(c->editor->sizeWindow.cx,
                                   c->editor->nTotalWidth));
  }
953 954 955

  for (p = paragraph->next; p != para->next_para; p = p->next)
  {
956 957
    switch(p->type) {
      case diParagraph:
958
        assert(FALSE);
959 960
        break;
      case diStartRow:
961
        y += height;
962
        rc.top = y;
963 964
        if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
          rc.bottom = y + para->nHeight;
965
        } else {
966
          rc.bottom = y + p->member.row.nHeight;
967 968
        }
        visible = RectVisible(c->hDC, &rc);
969 970
        if (visible)
          PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
971 972 973 974
        if (bounds.right)
        {
          /* If scrolled to the right past the end of the text, then
           * there may be space to the right of the paragraph border. */
975 976 977 978 979 980
          RECT after_bdr = rc;
          after_bdr.left = rc.right + bounds.right;
          after_bdr.right = c->rcView.right;
          if (RectVisible(c->hDC, &after_bdr))
            PatBlt(c->hDC, after_bdr.left, after_bdr.top, after_bdr.right - after_bdr.left,
                   after_bdr.bottom - after_bdr.top, PATCOPY);
981
        }
982 983
        if (me_debug)
        {
984
          static const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
985 986 987 988 989 990
          WCHAR buf[128];
          POINT pt = c->pt;
          wsprintfW(buf, wszRowDebug, no);
          pt.y = 12+y;
          ME_DebugWrite(c->hDC, &pt, buf);
        }
991

992 993 994 995 996 997 998
        height = p->member.row.nHeight;
        baseline = p->member.row.nBaseline;
        break;
      case diRun:
        assert(para);
        run = &p->member.run;
        if (visible && me_debug) {
999 1000
          RECT rc;
          rc.left = c->pt.x + run->pt.x;
1001
          rc.right = rc.left + run->nWidth;
1002
          rc.top = c->pt.y + para->pt.y + run->pt.y;
1003
          rc.bottom = rc.top + height;
1004
          TRACE("rc = %s\n", wine_dbgstr_rect(&rc));
1005
          FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
1006 1007
        }
        if (visible)
1008 1009
          ME_DrawRun(c, c->pt.x + run->pt.x,
                     c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
1010 1011
        if (me_debug)
        {
1012
          static const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
1013 1014
          WCHAR buf[2560];
          POINT pt;
1015 1016
          pt.x = c->pt.x + run->pt.x;
          pt.y = c->pt.y + para->pt.y + run->pt.y;
1017
          wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, get_text( &p->member.run, 0 ));
1018 1019 1020
          ME_DebugWrite(c->hDC, &pt, buf);
        }
        break;
1021 1022
      case diCell:
        /* Clear any space at the bottom of the cell after the text. */
1023
        if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
1024 1025
          break;
        y += height;
1026 1027
        rc.top = c->pt.y + para->pt.y + para->nHeight;
        rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
1028
        if (RectVisible(c->hDC, &rc))
1029
          PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
1030
        break;
1031 1032 1033 1034 1035
      default:
        break;
    }
    no++;
  }
1036 1037

  ME_DrawTableBorders(c, paragraph);
1038
  draw_para_number(c, paragraph);
1039

1040 1041 1042
  SetTextAlign(c->hDC, align);
}

1043
void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1044
{
1045
  BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1046 1047 1048 1049 1050 1051 1052
  int scrollX = 0, scrollY = 0;

  if (editor->horz_si.nPos != x) {
    x = min(x, editor->horz_si.nMax);
    x = max(x, editor->horz_si.nMin);
    scrollX = editor->horz_si.nPos - x;
    editor->horz_si.nPos = x;
1053 1054 1055
    if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
      x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
    ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1056
  }
1057

1058 1059 1060 1061 1062
  if (editor->vert_si.nPos != y) {
    y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
    y = max(y, editor->vert_si.nMin);
    scrollY = editor->vert_si.nPos - y;
    editor->vert_si.nPos = y;
1063 1064 1065
    if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
      y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
    ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1066
  }
1067 1068 1069

  if (abs(scrollX) > editor->sizeWindow.cx ||
      abs(scrollY) > editor->sizeWindow.cy)
1070
    ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1071
  else
1072 1073 1074
    ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
                               &editor->rcFormat, &editor->rcFormat,
                               NULL, NULL, SW_INVALIDATE);
1075
  ME_Repaint(editor);
1076

1077 1078 1079
  if (editor->hWnd)
  {
    LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    if (editor->styleFlags & WS_HSCROLL)
    {
      bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
      bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
                                 && (editor->styleFlags & WS_HSCROLL))
                                || (editor->styleFlags & ES_DISABLENOSCROLL);
      if (bScrollBarIsVisible != bScrollBarWillBeVisible)
        ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
                                  bScrollBarWillBeVisible);
    }
1090

1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
    if (editor->styleFlags & WS_VSCROLL)
    {
      bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
      bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
                                 && (editor->styleFlags & WS_VSCROLL)
                                 && (editor->styleFlags & ES_MULTILINE))
                                || (editor->styleFlags & ES_DISABLENOSCROLL);
      if (bScrollBarIsVisible != bScrollBarWillBeVisible)
        ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
                                  bScrollBarWillBeVisible);
    }
1102
  }
1103
  ME_UpdateScrollBar(editor);
1104
}
1105

1106
void ME_HScrollAbs(ME_TextEditor *editor, int x)
1107
{
1108 1109 1110 1111 1112 1113
  ME_ScrollAbs(editor, x, editor->vert_si.nPos);
}

void ME_VScrollAbs(ME_TextEditor *editor, int y)
{
  ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1114 1115 1116 1117
}

void ME_ScrollUp(ME_TextEditor *editor, int cy)
{
1118
  ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1119 1120 1121 1122
}

void ME_ScrollDown(ME_TextEditor *editor, int cy)
{
1123
  ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1124 1125
}

1126 1127 1128 1129 1130 1131 1132 1133
void ME_ScrollLeft(ME_TextEditor *editor, int cx)
{
  ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
}

void ME_ScrollRight(ME_TextEditor *editor, int cx)
{
  ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1134 1135
}

1136
/* Calculates the visibility after a call to SetScrollRange or
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
 * SetScrollInfo with SIF_RANGE. */
static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
{
  if (si->fMask & SIF_DISABLENOSCROLL)
    return TRUE;

  /* This must match the check in SetScrollInfo to determine whether
   * to show or hide the scrollbars. */
  return si->nMin < si->nMax - max(si->nPage - 1, 0);
}

1148
void ME_UpdateScrollBar(ME_TextEditor *editor)
1149 1150 1151 1152
{
  /* Note that this is the only function that should ever call
   * SetScrollInfo with SIF_PAGE or SIF_RANGE. */

1153
  SCROLLINFO si;
1154 1155
  BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;

1156 1157
  if (ME_WrapMarkedParagraphs(editor))
    FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1158

1159
  si.cbSize = sizeof(si);
1160
  si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1161
  si.nMin = 0;
1162
  if (editor->styleFlags & ES_DISABLENOSCROLL)
1163
    si.fMask |= SIF_DISABLENOSCROLL;
1164

1165 1166 1167 1168
  /* Update horizontal scrollbar */
  bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
  bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
  if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1169
  {
1170 1171
    ME_HScrollAbs(editor, 0);
    /* ME_HScrollAbs will call this function,
1172 1173 1174 1175
     * so nothing else needs to be done here. */
    return;
  }

1176 1177 1178 1179
  si.nMax = editor->nTotalWidth;
  si.nPos = editor->horz_si.nPos;
  si.nPage = editor->sizeWindow.cx;

1180
  if (si.nMax != editor->horz_si.nMax ||
1181
      si.nPage != editor->horz_si.nPage)
1182
  {
1183 1184 1185
    TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
    editor->horz_si.nMax = si.nMax;
    editor->horz_si.nPage = si.nPage;
1186 1187 1188
    if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
        editor->styleFlags & WS_HSCROLL)
    {
1189 1190 1191 1192 1193 1194
      if (si.nMax > 0xFFFF)
      {
        /* Native scales the scrollbar info to 16-bit external values. */
        si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
        si.nMax = 0xFFFF;
      }
1195 1196 1197 1198 1199 1200
      if (editor->hWnd) {
        SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
      } else {
        ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
        ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
      }
1201 1202
      /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
      bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1203
    }
1204 1205
  }

1206 1207 1208 1209 1210 1211 1212
  if (editor->styleFlags & WS_HSCROLL)
  {
    if (si.fMask & SIF_DISABLENOSCROLL) {
      bScrollBarWillBeVisible = TRUE;
    } else if (!(editor->styleFlags & WS_HSCROLL)) {
      bScrollBarWillBeVisible = FALSE;
    }
1213

1214 1215 1216
    if (bScrollBarWasVisible != bScrollBarWillBeVisible)
      ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
  }
1217 1218 1219

  /* Update vertical scrollbar */
  bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1220 1221
  bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
                            (editor->styleFlags & ES_MULTILINE);
1222 1223 1224 1225 1226 1227 1228

  if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
  {
    ME_VScrollAbs(editor, 0);
    /* ME_VScrollAbs will call this function,
     * so nothing else needs to be done here. */
    return;
1229 1230
  }

1231
  si.nMax = editor->nTotalLength;
1232
  si.nPos = editor->vert_si.nPos;
1233
  si.nPage = editor->sizeWindow.cy;
1234

1235
  if (si.nMax != editor->vert_si.nMax ||
1236
      si.nPage != editor->vert_si.nPage)
1237 1238 1239 1240
  {
    TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
    editor->vert_si.nMax = si.nMax;
    editor->vert_si.nPage = si.nPage;
1241 1242 1243
    if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
        editor->styleFlags & WS_VSCROLL)
    {
1244 1245 1246 1247 1248 1249
      if (si.nMax > 0xFFFF)
      {
        /* Native scales the scrollbar info to 16-bit external values. */
        si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
        si.nMax = 0xFFFF;
      }
1250 1251 1252 1253 1254 1255
      if (editor->hWnd) {
        SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
      } else {
        ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
        ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
      }
1256 1257
      /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
      bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1258
    }
1259
  }
1260

1261 1262 1263 1264 1265 1266 1267
  if (editor->styleFlags & WS_VSCROLL)
  {
    if (si.fMask & SIF_DISABLENOSCROLL) {
      bScrollBarWillBeVisible = TRUE;
    } else if (!(editor->styleFlags & WS_VSCROLL)) {
      bScrollBarWillBeVisible = FALSE;
    }
1268

1269 1270 1271 1272
    if (bScrollBarWasVisible != bScrollBarWillBeVisible)
      ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
                                bScrollBarWillBeVisible);
  }
1273 1274
}

1275
void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1276
{
1277 1278
  ME_Run *pRun = &pCursor->pRun->member.run;
  ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1279
  ME_DisplayItem *pPara = pCursor->pPara;
1280
  int x, y, yheight;
1281

1282 1283
  assert(pRow);
  assert(pPara);
1284

1285 1286
  if (editor->styleFlags & ES_AUTOHSCROLL)
  {
1287
    x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset, TRUE);
1288 1289 1290 1291 1292 1293
    if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
      x = x + 1 - editor->sizeWindow.cx;
    else if (x > editor->horz_si.nPos)
      x = editor->horz_si.nPos;

    if (~editor->styleFlags & ES_AUTOVSCROLL)
1294
    {
1295
      ME_HScrollAbs(editor, x);
1296 1297
      return;
    }
1298 1299 1300
  } else {
    if (~editor->styleFlags & ES_AUTOVSCROLL)
      return;
1301
    x = editor->horz_si.nPos;
1302
  }
1303 1304

  y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1305
  yheight = pRow->member.row.nHeight;
1306 1307 1308 1309 1310 1311 1312

  if (y < editor->vert_si.nPos)
    ME_ScrollAbs(editor, x, y);
  else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
    ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
  else if (x != editor->horz_si.nPos)
    ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1313
}
1314 1315 1316 1317 1318


void
ME_InvalidateSelection(ME_TextEditor *editor)
{
1319 1320
  ME_DisplayItem *sel_start, *sel_end;
  ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
1321 1322 1323
  int nStart, nEnd;
  int len = ME_GetTextLength(editor);

1324
  ME_GetSelectionOfs(editor, &nStart, &nEnd);
1325 1326 1327 1328
  /* if both old and new selection are 0-char (= caret only), then
  there's no (inverted) area to be repainted, neither old nor new */
  if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
    return;
1329
  ME_WrapMarkedParagraphs(editor);
1330 1331 1332
  ME_GetSelectionParas(editor, &sel_start, &sel_end);
  assert(sel_start->type == diParagraph);
  assert(sel_end->type == diParagraph);
1333
  /* last selection markers aren't always updated, which means
1334
   * they can point past the end of the document */
1335
  if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1336
    repaint_start = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1337
    repaint_end = editor->pBuffer->pLast->member.para.prev_para;
1338 1339 1340
  } else {
    /* if the start part of selection is being expanded or contracted... */
    if (nStart < editor->nLastSelStart) {
1341
      repaint_start = sel_start;
1342
      repaint_end = editor->pLastSelStartPara;
1343
    } else if (nStart > editor->nLastSelStart) {
1344
      repaint_start = editor->pLastSelStartPara;
1345
      repaint_end = sel_start;
1346
    }
1347

1348 1349
    /* if the end part of selection is being contracted or expanded... */
    if (nEnd < editor->nLastSelEnd) {
1350
      if (!repaint_start) repaint_start = sel_end;
1351
      repaint_end = editor->pLastSelEndPara;
1352
    } else if (nEnd > editor->nLastSelEnd) {
1353
      if (!repaint_start) repaint_start = editor->pLastSelEndPara;
1354
      repaint_end = sel_end;
1355
    }
1356
  }
1357

1358 1359
  if (repaint_start)
    ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1360
  /* remember the last invalidated position */
1361
  ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1362 1363 1364
  ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
  assert(editor->pLastSelStartPara->type == diParagraph);
  assert(editor->pLastSelEndPara->type == diParagraph);
1365 1366
}

1367 1368 1369 1370 1371
BOOL
ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
{
  /* TODO: Zoom images and objects */

1372
  if (numerator == 0 && denominator == 0)
1373
  {
1374 1375
    editor->nZoomNumerator = editor->nZoomDenominator = 0;
    return TRUE;
1376
  }
1377 1378 1379 1380 1381
  if (numerator <= 0 || denominator <= 0)
    return FALSE;
  if (numerator * 64 <= denominator || numerator / denominator >= 64)
    return FALSE;

1382 1383
  editor->nZoomNumerator = numerator;
  editor->nZoomDenominator = denominator;
1384

1385 1386 1387
  ME_RewrapRepaint(editor);
  return TRUE;
}