winex11.drv: Update a comment.
[wine.git] / dlls / riched20 / paint.c
blobd15c47b72aa80db7ad00b246dad1cfe93102fa42
1 /*
2 * RichEdit - painting functions
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * 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
22 #include "editor.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 static void draw_paragraph( ME_Context *c, ME_Paragraph *para );
28 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, const RECT *rcUpdate)
30 ME_Paragraph *para;
31 ME_Context c;
32 ME_Cell *cell;
33 int ys, ye;
34 HRGN oldRgn;
36 oldRgn = CreateRectRgn(0, 0, 0, 0);
37 if (!GetClipRgn(hDC, oldRgn))
39 DeleteObject(oldRgn);
40 oldRgn = NULL;
42 IntersectClipRect(hDC, rcUpdate->left, rcUpdate->top,
43 rcUpdate->right, rcUpdate->bottom);
45 ME_InitContext(&c, editor, hDC);
46 SetBkMode(hDC, TRANSPARENT);
48 para = editor_first_para( editor );
49 /* This context point is an offset for the paragraph positions stored
50 * during wrapping. It shouldn't be modified during painting. */
51 c.pt.x = c.rcView.left - editor->horz_si.nPos;
52 c.pt.y = c.rcView.top - editor->vert_si.nPos;
53 while (para_next( para ))
55 ys = c.pt.y + para->pt.y;
56 cell = para_cell( para );
57 if (cell && para == cell_end_para( cell ))
58 ye = c.pt.y + cell->pt.y + cell->nHeight;
59 else ye = ys + para->nHeight;
61 if (cell && !(para->nFlags & MEPF_ROWEND) && para == cell_first_para( cell ))
63 /* the border shifts the text down */
64 ys -= para_cell( para )->yTextOffset;
67 /* Draw the paragraph if any of the paragraph is in the update region. */
68 if (ys < rcUpdate->bottom && ye > rcUpdate->top)
69 draw_paragraph( &c, para );
70 para = para_next( para );
72 if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
74 /* Fill space after the end of the text. */
75 RECT rc;
76 rc.top = c.pt.y + editor->nTotalLength;
77 rc.left = c.rcView.left;
78 rc.bottom = c.rcView.bottom;
79 rc.right = c.rcView.right;
81 IntersectRect(&rc, &rc, rcUpdate);
83 if (!IsRectEmpty(&rc))
84 PatBlt(hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
86 if (editor->nTotalLength != editor->nLastTotalLength ||
87 editor->nTotalWidth != editor->nLastTotalWidth)
88 ME_SendRequestResize(editor, FALSE);
89 editor->nLastTotalLength = editor->nTotalLength;
90 editor->nLastTotalWidth = editor->nTotalWidth;
92 SelectClipRgn(hDC, oldRgn);
93 if (oldRgn)
94 DeleteObject(oldRgn);
96 c.hDC = NULL;
97 ME_DestroyContext(&c);
100 void ME_Repaint(ME_TextEditor *editor)
102 if (ME_WrapMarkedParagraphs(editor))
104 ME_UpdateScrollBar(editor);
105 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
107 ITextHost_TxViewChange(editor->texthost, TRUE);
110 void ME_UpdateRepaint(ME_TextEditor *editor, BOOL update_now)
112 /* Should be called whenever the contents of the control have changed */
113 BOOL wrappedParagraphs;
115 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
116 if (wrappedParagraphs)
117 ME_UpdateScrollBar(editor);
119 /* Ensure that the cursor is visible */
120 editor_ensure_visible( editor, &editor->pCursors[0] );
122 ITextHost_TxViewChange(editor->texthost, update_now);
124 ME_SendSelChange(editor);
126 /* send EN_CHANGE if the event mask asks for it */
127 if(editor->nEventMask & ENM_CHANGE)
129 editor->nEventMask &= ~ENM_CHANGE;
130 ME_SendOldNotify(editor, EN_CHANGE);
131 editor->nEventMask |= ENM_CHANGE;
135 void
136 ME_RewrapRepaint(ME_TextEditor *editor)
138 /* RewrapRepaint should be called whenever the control has changed in
139 * looks, but not content. Like resizing. */
141 ME_MarkAllForWrapping(editor);
142 ME_WrapMarkedParagraphs(editor);
143 ME_UpdateScrollBar(editor);
144 ME_Repaint(editor);
147 int ME_twips2pointsX(const ME_Context *c, int x)
149 if (c->editor->nZoomNumerator == 0)
150 return x * c->dpi.cx / 1440;
151 else
152 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
155 int ME_twips2pointsY(const ME_Context *c, int y)
157 if (c->editor->nZoomNumerator == 0)
158 return y * c->dpi.cy / 1440;
159 else
160 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
164 static int calc_y_offset( const ME_Context *c, ME_Style *style )
166 int offs = 0, twips = 0;
168 if ((style->fmt.dwMask & style->fmt.dwEffects) & CFM_OFFSET)
169 twips = style->fmt.yOffset;
171 if ((style->fmt.dwMask & style->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT))
173 if (style->fmt.dwEffects & CFE_SUPERSCRIPT) twips = style->fmt.yHeight/3;
174 if (style->fmt.dwEffects & CFE_SUBSCRIPT) twips = -style->fmt.yHeight/12;
177 if (twips) offs = ME_twips2pointsY( c, twips );
179 return offs;
182 static COLORREF get_text_color( ME_Context *c, ME_Style *style, BOOL highlight )
184 COLORREF color;
186 if (highlight)
187 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHTTEXT );
188 else if ((style->fmt.dwMask & CFM_LINK) && (style->fmt.dwEffects & CFE_LINK))
189 color = RGB(0,0,255);
190 else if ((style->fmt.dwMask & CFM_COLOR) && (style->fmt.dwEffects & CFE_AUTOCOLOR))
191 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_WINDOWTEXT );
192 else
193 color = style->fmt.crTextColor;
195 return color;
198 static COLORREF get_back_color( ME_Context *c, ME_Style *style, BOOL highlight )
200 COLORREF color;
202 if (highlight)
203 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );
204 else if ( (style->fmt.dwMask & CFM_BACKCOLOR)
205 && !(style->fmt.dwEffects & CFE_AUTOBACKCOLOR) )
206 color = style->fmt.crBackColor;
207 else
208 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_WINDOW );
210 return color;
213 static HPEN get_underline_pen( ME_Style *style, COLORREF color )
215 if (style->fmt.dwEffects & CFE_LINK)
216 return CreatePen( PS_SOLID, 1, color );
218 /* Choose the pen type for underlining the text. */
219 if (style->fmt.dwEffects & CFE_UNDERLINE)
221 switch (style->fmt.bUnderlineType)
223 case CFU_UNDERLINE:
224 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
225 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
226 return CreatePen( PS_SOLID, 1, color );
227 case CFU_UNDERLINEDOTTED:
228 return CreatePen( PS_DOT, 1, color );
229 default:
230 FIXME( "Unknown underline type (%u)\n", style->fmt.bUnderlineType );
231 /* fall through */
232 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
233 case CFU_UNDERLINENONE:
234 break;
237 return NULL;
240 static void draw_underline( ME_Context *c, ME_Run *run, int x, int y, COLORREF color )
242 HPEN pen;
244 pen = get_underline_pen( run->style, color );
245 if (pen)
247 HPEN old_pen = SelectObject( c->hDC, pen );
248 MoveToEx( c->hDC, x, y + 1, NULL );
249 LineTo( c->hDC, x + run->nWidth, y + 1 );
250 SelectObject( c->hDC, old_pen );
251 DeleteObject( pen );
253 return;
256 /*********************************************************************
257 * draw_space
259 * Draw the end-of-paragraph or tab space.
261 * If actually_draw is TRUE then ensure any underline is drawn.
263 static void draw_space( ME_Context *c, ME_Run *run, int x, int y,
264 BOOL selected, BOOL actually_draw, int ymin, int cy )
266 HDC hdc = c->hDC;
267 BOOL old_style_selected = FALSE;
268 RECT rect;
269 COLORREF back_color = 0;
271 SetRect( &rect, x, ymin, x + run->nWidth, ymin + cy );
273 if (c->editor->bHideSelection || (!c->editor->bHaveFocus &&
274 !(c->editor->styleFlags & ES_NOHIDESEL))) selected = FALSE;
275 if (c->editor->bEmulateVersion10)
277 old_style_selected = selected;
278 selected = FALSE;
281 if (selected)
282 back_color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );
284 if (actually_draw)
286 COLORREF text_color = get_text_color( c, run->style, selected );
287 COLORREF old_text, old_back;
288 int y_offset = calc_y_offset( c, run->style );
289 static const WCHAR space[1] = {' '};
291 select_style( c, run->style );
292 old_text = SetTextColor( hdc, text_color );
293 if (selected) old_back = SetBkColor( hdc, back_color );
295 ExtTextOutW( hdc, x, y - y_offset, selected ? ETO_OPAQUE : 0, &rect, space, 1, &run->nWidth );
297 if (selected) SetBkColor( hdc, old_back );
298 SetTextColor( hdc, old_text );
300 draw_underline( c, run, x, y - y_offset, text_color );
302 else if (selected)
304 HBRUSH brush = CreateSolidBrush( back_color );
305 FillRect( hdc, &rect, brush );
306 DeleteObject( brush );
309 if (old_style_selected)
310 PatBlt( hdc, x, ymin, run->nWidth, cy, DSTINVERT );
313 static void get_selection_rect( ME_Context *c, ME_Run *run, int from, int to, int cy, RECT *r )
315 from = max( 0, from );
316 to = min( run->len, to );
317 r->left = ME_PointFromCharContext( c, run, from, TRUE );
318 r->top = 0;
319 r->right = ME_PointFromCharContext( c, run, to, TRUE );
320 r->bottom = cy;
321 return;
324 static void draw_text( ME_Context *c, ME_Run *run, int x, int y, BOOL selected, RECT *sel_rect )
326 COLORREF text_color = get_text_color( c, run->style, selected );
327 COLORREF back_color = get_back_color( c, run->style, selected );
328 COLORREF old_text, old_back = 0;
329 const WCHAR *text = get_text( run, 0 );
330 ME_String *masked = NULL;
331 const BOOL paint_bg = ( selected
332 || ( ( run->style->fmt.dwMask & CFM_BACKCOLOR )
333 && !(CFE_AUTOBACKCOLOR & run->style->fmt.dwEffects) )
336 if (c->editor->cPasswordMask)
338 masked = ME_MakeStringR( c->editor->cPasswordMask, run->len );
339 text = masked->szData;
342 old_text = SetTextColor( c->hDC, text_color );
343 if (paint_bg) old_back = SetBkColor( c->hDC, back_color );
345 if (run->para->nFlags & MEPF_COMPLEX)
346 ScriptTextOut( c->hDC, &run->style->script_cache, x, y, paint_bg ? ETO_OPAQUE : 0, sel_rect,
347 &run->script_analysis, NULL, 0, run->glyphs, run->num_glyphs, run->advances,
348 NULL, run->offsets );
349 else
350 ExtTextOutW( c->hDC, x, y, paint_bg ? ETO_OPAQUE : 0, sel_rect, text, run->len, NULL );
352 if (paint_bg) SetBkColor( c->hDC, old_back );
353 SetTextColor( c->hDC, old_text );
355 draw_underline( c, run, x, y, text_color );
357 ME_DestroyString( masked );
358 return;
362 static void draw_text_with_style( ME_Context *c, ME_Run *run, int x, int y,
363 int nSelFrom, int nSelTo, int ymin, int cy )
365 HDC hDC = c->hDC;
366 int yOffset = 0;
367 BOOL selected = (nSelFrom < run->len && nSelTo >= 0
368 && nSelFrom < nSelTo && !c->editor->bHideSelection &&
369 (c->editor->bHaveFocus || (c->editor->styleFlags & ES_NOHIDESEL)));
370 BOOL old_style_selected = FALSE;
371 RECT sel_rect;
372 HRGN clip = NULL, sel_rgn = NULL;
374 yOffset = calc_y_offset( c, run->style );
376 if (selected)
378 get_selection_rect( c, run, nSelFrom, nSelTo, cy, &sel_rect );
379 OffsetRect( &sel_rect, x, ymin );
381 if (c->editor->bEmulateVersion10)
383 old_style_selected = TRUE;
384 selected = FALSE;
386 else
388 sel_rgn = CreateRectRgnIndirect( &sel_rect );
389 clip = CreateRectRgn( 0, 0, 0, 0 );
390 if (GetClipRgn( hDC, clip ) != 1)
392 DeleteObject( clip );
393 clip = NULL;
398 select_style( c, run->style );
400 if (sel_rgn) ExtSelectClipRgn( hDC, sel_rgn, RGN_DIFF );
402 if (!(run->style->fmt.dwEffects & CFE_AUTOBACKCOLOR)
403 && (run->style->fmt.dwMask & CFM_BACKCOLOR) )
405 RECT tmp_rect;
406 get_selection_rect( c, run, 0, run->len, cy, &tmp_rect );
407 OffsetRect( &tmp_rect, x, ymin );
408 draw_text( c, run, x, y - yOffset, FALSE, &tmp_rect );
410 else
411 draw_text( c, run, x, y - yOffset, FALSE, NULL );
413 if (sel_rgn)
415 ExtSelectClipRgn( hDC, clip, RGN_COPY );
416 ExtSelectClipRgn( hDC, sel_rgn, RGN_AND );
417 draw_text( c, run, x, y - yOffset, TRUE, &sel_rect );
418 ExtSelectClipRgn( hDC, clip, RGN_COPY );
419 if (clip) DeleteObject( clip );
420 DeleteObject( sel_rgn );
423 if (old_style_selected)
424 PatBlt( hDC, sel_rect.left, ymin, sel_rect.right - sel_rect.left, cy, DSTINVERT );
427 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
428 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
429 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
430 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
431 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
432 SelectObject(hDC, hFont);
433 SetTextAlign(hDC, align);
434 SetTextColor(hDC, color);
437 static void draw_run( ME_Context *c, int x, int y, ME_Cursor *cursor )
439 ME_Row *row;
440 ME_Run *run = &cursor->pRun->member.run;
441 int runofs = run_char_ofs( run, cursor->nOffset );
442 int nSelFrom, nSelTo;
444 if (run->nFlags & MERF_HIDDEN) return;
446 row = row_from_cursor( cursor );
447 ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
449 /* Draw selected end-of-paragraph mark */
450 if (run->nFlags & MERF_ENDPARA)
452 if (runofs >= nSelFrom && runofs < nSelTo)
454 draw_space( c, run, x, y, TRUE, FALSE,
455 c->pt.y + run->para->pt.y + row->pt.y, row->nHeight );
457 return;
460 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
462 BOOL selected = runofs >= nSelFrom && runofs < nSelTo;
464 draw_space( c, run, x, y, selected, TRUE,
465 c->pt.y + run->para->pt.y + row->pt.y, row->nHeight );
466 return;
469 if (run->nFlags & MERF_GRAPHICS)
470 draw_ole( c, x, y, run, (runofs >= nSelFrom) && (runofs < nSelTo) );
471 else
472 draw_text_with_style( c, run, x, y, nSelFrom - runofs, nSelTo - runofs,
473 c->pt.y + run->para->pt.y + row->pt.y, row->nHeight );
476 /* The documented widths are in points (72 dpi), but converting them to
477 * 96 dpi (standard display resolution) avoids dealing with fractions. */
478 static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
479 /* none */ {0, PS_SOLID, FALSE},
480 /* 3/4 */ {1, PS_SOLID, FALSE},
481 /* 1 1/2 */ {2, PS_SOLID, FALSE},
482 /* 2 1/4 */ {3, PS_SOLID, FALSE},
483 /* 3 */ {4, PS_SOLID, FALSE},
484 /* 4 1/2 */ {6, PS_SOLID, FALSE},
485 /* 6 */ {8, PS_SOLID, FALSE},
486 /* 3/4 double */ {1, PS_SOLID, TRUE},
487 /* 1 1/2 double */ {2, PS_SOLID, TRUE},
488 /* 2 1/4 double */ {3, PS_SOLID, TRUE},
489 /* 3/4 gray */ {1, PS_DOT /* FIXME */, FALSE},
490 /* 1 1/2 dashed */ {2, PS_DASH, FALSE},
493 static const COLORREF pen_colors[16] = {
494 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
495 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
496 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
497 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
498 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
499 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
500 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
501 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
504 static int ME_GetBorderPenWidth(const ME_Context* c, int idx)
506 int width = border_details[idx].width;
508 if (c->dpi.cx != 96)
509 width = MulDiv(width, c->dpi.cx, 96);
511 if (c->editor->nZoomNumerator != 0)
512 width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
514 return width;
517 int ME_GetParaBorderWidth(const ME_Context* c, int flags)
519 int idx = (flags >> 8) & 0xF;
520 int width;
522 if (idx >= ARRAY_SIZE(border_details))
524 FIXME("Unsupported border value %d\n", idx);
525 return 0;
527 width = ME_GetBorderPenWidth(c, idx);
528 if (border_details[idx].dble) width = width * 2 + 1;
529 return width;
532 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
534 int idx, border_width, top_border, bottom_border;
535 RECT rc;
536 BOOL hasParaBorder;
538 SetRectEmpty(bounds);
539 if (!(para->fmt.dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
541 border_width = top_border = bottom_border = 0;
542 idx = (para->fmt.wBorders >> 8) & 0xF;
543 hasParaBorder = (!(c->editor->bEmulateVersion10 &&
544 para->fmt.dwMask & PFM_TABLE &&
545 para->fmt.wEffects & PFE_TABLE) &&
546 (para->fmt.dwMask & PFM_BORDER) &&
547 idx != 0 &&
548 (para->fmt.wBorders & 0xF));
549 if (hasParaBorder)
551 /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
552 * controls. It actually stores the paragraph or row border style. Although
553 * the value isn't used for drawing, it is used for streaming out rich text.
555 * wBorders stores the border style for each side (top, left, bottom, right)
556 * using nibble (4 bits) to store each border style. The rich text format
557 * control words, and their associated value are the following:
558 * \brdrdash 0
559 * \brdrdashsm 1
560 * \brdrdb 2
561 * \brdrdot 3
562 * \brdrhair 4
563 * \brdrs 5
564 * \brdrth 6
565 * \brdrtriple 7
567 * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
568 * The mask corresponding to each side for the version are the following:
569 * mask v1.0-3.0 v4.1
570 * 0x000F top left
571 * 0x00F0 left top
572 * 0x0F00 bottom right
573 * 0xF000 right bottom
575 if (para->fmt.wBorders & 0x00B0)
576 FIXME("Unsupported border flags %x\n", para->fmt.wBorders);
577 border_width = ME_GetParaBorderWidth(c, para->fmt.wBorders);
578 if (para->fmt.wBorders & 4) top_border = border_width;
579 if (para->fmt.wBorders & 8) bottom_border = border_width;
582 if (para->fmt.dwMask & PFM_SPACEBEFORE)
584 rc.left = c->rcView.left;
585 rc.right = c->rcView.right;
586 rc.top = y;
587 bounds->top = ME_twips2pointsY(c, para->fmt.dySpaceBefore);
588 rc.bottom = y + bounds->top + top_border;
589 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
592 if (para->fmt.dwMask & PFM_SPACEAFTER)
594 rc.left = c->rcView.left;
595 rc.right = c->rcView.right;
596 rc.bottom = y + para->nHeight;
597 bounds->bottom = ME_twips2pointsY(c, para->fmt.dySpaceAfter);
598 rc.top = rc.bottom - bounds->bottom - bottom_border;
599 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
602 /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
603 * but might support it in later versions. */
604 if (hasParaBorder) {
605 int pen_width, rightEdge;
606 COLORREF pencr;
607 HPEN pen = NULL, oldpen = NULL;
608 POINT pt;
610 if (para->fmt.wBorders & 64) /* autocolor */
611 pencr = ITextHost_TxGetSysColor(c->editor->texthost,
612 COLOR_WINDOWTEXT);
613 else
614 pencr = pen_colors[(para->fmt.wBorders >> 12) & 0xF];
616 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
617 c->editor->nTotalWidth);
619 pen_width = ME_GetBorderPenWidth(c, idx);
620 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
621 oldpen = SelectObject(c->hDC, pen);
622 MoveToEx(c->hDC, 0, 0, &pt);
624 /* before & after spaces are not included in border */
626 /* helper to draw the double lines in case of corner */
627 #define DD(x) ((para->fmt.wBorders & (x)) ? (pen_width + 1) : 0)
629 if (para->fmt.wBorders & 1)
631 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
632 LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
633 if (border_details[idx].dble) {
634 rc.left = c->pt.x + 1;
635 rc.right = rc.left + border_width;
636 rc.top = y + bounds->top;
637 rc.bottom = y + para->nHeight - bounds->bottom;
638 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
639 MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
640 LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
642 bounds->left += border_width;
644 if (para->fmt.wBorders & 2)
646 MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
647 LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
648 if (border_details[idx].dble) {
649 rc.left = rightEdge - pen_width - 1;
650 rc.right = rc.left + pen_width;
651 rc.top = y + bounds->top;
652 rc.bottom = y + para->nHeight - bounds->bottom;
653 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
654 MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
655 LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
657 bounds->right += border_width;
659 if (para->fmt.wBorders & 4)
661 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
662 LineTo(c->hDC, rightEdge, y + bounds->top);
663 if (border_details[idx].dble) {
664 MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
665 LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
667 bounds->top += border_width;
669 if (para->fmt.wBorders & 8)
671 MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
672 LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
673 if (border_details[idx].dble) {
674 MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
675 LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
677 bounds->bottom += border_width;
679 #undef DD
681 MoveToEx(c->hDC, pt.x, pt.y, NULL);
682 SelectObject(c->hDC, oldpen);
683 DeleteObject(pen);
687 static void draw_table_borders( ME_Context *c, ME_Paragraph *para )
689 if (!c->editor->bEmulateVersion10) /* v4.1 */
691 if (para_cell( para ))
693 RECT rc;
694 ME_Cell *cell = para_cell( para );
695 ME_Paragraph *after_row;
696 HPEN pen, oldPen;
697 LOGBRUSH logBrush;
698 HBRUSH brush;
699 COLORREF color;
700 POINT oldPt;
701 int width;
702 BOOL atTop = (para == cell_first_para( cell ));
703 BOOL atBottom = (para == cell_end_para( cell ));
704 int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
705 int bottom = (atBottom ?
706 c->pt.y + cell->pt.y + cell->nHeight :
707 top + para->nHeight + (atTop ? cell->yTextOffset : 0));
708 rc.left = c->pt.x + cell->pt.x;
709 rc.right = rc.left + cell->nWidth;
710 if (atTop)
712 /* Erase gap before text if not all borders are the same height. */
713 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
714 rc.top = top + width;
715 width = cell->yTextOffset - width;
716 rc.bottom = rc.top + width;
717 if (width)
718 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
720 /* Draw cell borders.
721 * The order borders are draw in is left, top, bottom, right in order
722 * to be consistent with native richedit. This is noticeable from the
723 * overlap of borders of different colours. */
724 if (!(para->nFlags & MEPF_ROWEND))
726 rc.top = top;
727 rc.bottom = bottom;
728 if (cell->border.left.width > 0)
730 color = cell->border.left.colorRef;
731 width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
733 else
735 color = RGB(192,192,192);
736 width = 1;
738 logBrush.lbStyle = BS_SOLID;
739 logBrush.lbColor = color;
740 logBrush.lbHatch = 0;
741 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
742 width, &logBrush, 0, NULL);
743 oldPen = SelectObject(c->hDC, pen);
744 MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
745 LineTo(c->hDC, rc.left, rc.bottom);
746 SelectObject(c->hDC, oldPen);
747 DeleteObject(pen);
748 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
751 if (atTop)
753 if (cell->border.top.width > 0)
755 brush = CreateSolidBrush(cell->border.top.colorRef);
756 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
758 else
760 brush = GetStockObject(LTGRAY_BRUSH);
761 width = 1;
763 rc.top = top;
764 rc.bottom = rc.top + width;
765 FillRect(c->hDC, &rc, brush);
766 if (cell->border.top.width > 0)
767 DeleteObject(brush);
770 /* Draw the bottom border if at the last paragraph in the cell, and when
771 * in the last row of the table. */
772 if (atBottom)
774 int oldLeft = rc.left;
775 width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
776 after_row = para_next( table_row_end( para ) );
777 if (after_row->nFlags & MEPF_ROWSTART)
779 ME_Cell *next_end;
780 next_end = table_row_end_cell( after_row );
781 assert( next_end && !cell_next( next_end ) );
782 rc.left = c->pt.x + next_end->pt.x;
784 if (rc.left < rc.right)
786 if (cell->border.bottom.width > 0)
787 brush = CreateSolidBrush(cell->border.bottom.colorRef);
788 else
789 brush = GetStockObject(LTGRAY_BRUSH);
790 rc.bottom = bottom;
791 rc.top = rc.bottom - width;
792 FillRect(c->hDC, &rc, brush);
793 if (cell->border.bottom.width > 0)
794 DeleteObject(brush);
796 rc.left = oldLeft;
799 /* Right border only drawn if at the end of the table row. */
800 if (!cell_next( cell_next( cell ) ) && !(para->nFlags & MEPF_ROWSTART))
802 rc.top = top;
803 rc.bottom = bottom;
804 if (cell->border.right.width > 0)
806 color = cell->border.right.colorRef;
807 width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
809 else
811 color = RGB(192,192,192);
812 width = 1;
814 logBrush.lbStyle = BS_SOLID;
815 logBrush.lbColor = color;
816 logBrush.lbHatch = 0;
817 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
818 width, &logBrush, 0, NULL);
819 oldPen = SelectObject(c->hDC, pen);
820 MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
821 LineTo(c->hDC, rc.right - 1, rc.bottom);
822 SelectObject(c->hDC, oldPen);
823 DeleteObject(pen);
824 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
828 else /* v1.0 - 3.0 */
830 /* Draw simple table border */
831 if (para_in_table( para ))
833 HPEN pen = NULL, oldpen = NULL;
834 int i, firstX, startX, endX, rowY, rowBottom, nHeight;
835 POINT oldPt;
837 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
838 oldpen = SelectObject(c->hDC, pen);
840 /* Find the start relative to the text */
841 firstX = c->pt.x + para_first_run( para )->pt.x;
842 /* Go back by the horizontal gap, which is stored in dxOffset */
843 firstX -= ME_twips2pointsX(c, para->fmt.dxOffset);
844 /* The left edge, stored in dxStartIndent affected just the first edge */
845 startX = firstX - ME_twips2pointsX(c, para->fmt.dxStartIndent);
846 rowY = c->pt.y + para->pt.y;
847 if (para->fmt.dwMask & PFM_SPACEBEFORE)
848 rowY += ME_twips2pointsY(c, para->fmt.dySpaceBefore);
849 nHeight = para_first_row( para )->nHeight;
850 rowBottom = rowY + nHeight;
852 /* Draw horizontal lines */
853 MoveToEx(c->hDC, firstX, rowY, &oldPt);
854 i = para->fmt.cTabCount - 1;
855 endX = startX + ME_twips2pointsX(c, para->fmt.rgxTabs[i] & 0x00ffffff) + 1;
856 LineTo(c->hDC, endX, rowY);
857 /* The bottom of the row only needs to be drawn if the next row is
858 * not a table. */
859 if (!(para_next( para ) && para_in_table( para_next( para ) ) && para->nRows == 1))
861 /* Decrement rowBottom to draw the bottom line within the row, and
862 * to not draw over this line when drawing the vertical lines. */
863 rowBottom--;
864 MoveToEx(c->hDC, firstX, rowBottom, NULL);
865 LineTo(c->hDC, endX, rowBottom);
868 /* Draw vertical lines */
869 MoveToEx(c->hDC, firstX, rowY, NULL);
870 LineTo(c->hDC, firstX, rowBottom);
871 for (i = 0; i < para->fmt.cTabCount; i++)
873 int rightBoundary = para->fmt.rgxTabs[i] & 0x00ffffff;
874 endX = startX + ME_twips2pointsX(c, rightBoundary);
875 MoveToEx(c->hDC, endX, rowY, NULL);
876 LineTo(c->hDC, endX, rowBottom);
879 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
880 SelectObject(c->hDC, oldpen);
881 DeleteObject(pen);
886 static void draw_para_number( ME_Context *c, ME_Paragraph *para )
888 int x, y;
889 COLORREF old_text;
891 if (para->fmt.wNumbering)
893 select_style( c, para->para_num.style );
894 old_text = SetTextColor( c->hDC, get_text_color( c, para->para_num.style, FALSE ) );
896 x = c->pt.x + para->para_num.pt.x;
897 y = c->pt.y + para->pt.y + para->para_num.pt.y;
899 ExtTextOutW( c->hDC, x, y, 0, NULL, para->para_num.text->szData, para->para_num.text->nLen, NULL );
901 SetTextColor( c->hDC, old_text );
905 static void draw_paragraph( ME_Context *c, ME_Paragraph *para )
907 int align = SetTextAlign(c->hDC, TA_BASELINE);
908 ME_DisplayItem *p;
909 ME_Cell *cell;
910 ME_Run *run;
911 RECT rc, bounds;
912 int y;
913 int height = 0, baseline = 0, no=0;
914 BOOL visible = FALSE;
916 rc.left = c->pt.x;
917 rc.right = c->rcView.right;
919 y = c->pt.y + para->pt.y;
920 if ((cell = para_cell( para )))
922 rc.left = c->pt.x + cell->pt.x;
923 rc.right = rc.left + cell->nWidth;
925 if (para->nFlags & MEPF_ROWSTART)
927 cell = table_row_first_cell( para );
928 rc.right = c->pt.x + cell->pt.x;
930 else if (para->nFlags & MEPF_ROWEND)
932 cell = table_row_end_cell( para );
933 rc.left = c->pt.x + cell->pt.x + cell->nWidth;
935 ME_DrawParaDecoration(c, para, y, &bounds);
936 y += bounds.top;
937 if (bounds.left || bounds.right)
939 rc.left = max(rc.left, c->pt.x + bounds.left);
940 rc.right = min(rc.right, c->pt.x - bounds.right
941 + max(c->editor->sizeWindow.cx,
942 c->editor->nTotalWidth));
945 for (p = para_get_di( para )->next; p != para_get_di( para_next( para ) ); p = p->next)
947 switch(p->type) {
948 case diParagraph:
949 assert(FALSE);
950 break;
951 case diStartRow:
952 y += height;
953 rc.top = y;
954 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
955 rc.bottom = y + para->nHeight;
956 } else {
957 rc.bottom = y + p->member.row.nHeight;
959 visible = RectVisible(c->hDC, &rc);
960 if (visible)
961 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
962 if (bounds.right)
964 /* If scrolled to the right past the end of the text, then
965 * there may be space to the right of the paragraph border. */
966 RECT after_bdr = rc;
967 after_bdr.left = rc.right + bounds.right;
968 after_bdr.right = c->rcView.right;
969 if (RectVisible(c->hDC, &after_bdr))
970 PatBlt(c->hDC, after_bdr.left, after_bdr.top, after_bdr.right - after_bdr.left,
971 after_bdr.bottom - after_bdr.top, PATCOPY);
973 if (me_debug)
975 static const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
976 WCHAR buf[128];
977 POINT pt = c->pt;
978 wsprintfW(buf, wszRowDebug, no);
979 pt.y = 12+y;
980 ME_DebugWrite(c->hDC, &pt, buf);
983 height = p->member.row.nHeight;
984 baseline = p->member.row.nBaseline;
985 break;
986 case diRun:
987 assert(para);
988 run = &p->member.run;
989 if (visible && me_debug) {
990 RECT rc;
991 rc.left = c->pt.x + run->pt.x;
992 rc.right = rc.left + run->nWidth;
993 rc.top = c->pt.y + para->pt.y + run->pt.y;
994 rc.bottom = rc.top + height;
995 TRACE("rc = %s\n", wine_dbgstr_rect(&rc));
996 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
998 if (visible)
1000 ME_Cursor cursor;
1002 cursor.pRun = run_get_di( run );
1003 cursor.pPara = para_get_di( para );
1004 cursor.nOffset = 0;
1005 draw_run( c, c->pt.x + run->pt.x, c->pt.y + para->pt.y + run->pt.y + baseline, &cursor );
1007 if (me_debug)
1009 static const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
1010 WCHAR buf[2560];
1011 POINT pt;
1012 pt.x = c->pt.x + run->pt.x;
1013 pt.y = c->pt.y + para->pt.y + run->pt.y;
1014 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, get_text( &p->member.run, 0 ));
1015 ME_DebugWrite(c->hDC, &pt, buf);
1017 break;
1018 case diCell:
1019 /* Clear any space at the bottom of the cell after the text. */
1020 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
1021 break;
1022 y += height;
1023 rc.top = c->pt.y + para->pt.y + para->nHeight;
1024 rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
1025 if (RectVisible(c->hDC, &rc))
1026 PatBlt(c->hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, PATCOPY);
1027 break;
1028 default:
1029 break;
1031 no++;
1034 draw_table_borders( c, para );
1035 draw_para_number( c, para );
1037 SetTextAlign(c->hDC, align);
1040 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1042 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1043 int scrollX = 0, scrollY = 0;
1045 if (editor->horz_si.nPos != x) {
1046 x = min(x, editor->horz_si.nMax);
1047 x = max(x, editor->horz_si.nMin);
1048 scrollX = editor->horz_si.nPos - x;
1049 editor->horz_si.nPos = x;
1050 if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
1051 x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
1052 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1055 if (editor->vert_si.nPos != y) {
1056 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1057 y = max(y, editor->vert_si.nMin);
1058 scrollY = editor->vert_si.nPos - y;
1059 editor->vert_si.nPos = y;
1060 if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1061 y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1062 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1065 if (abs(scrollX) > editor->sizeWindow.cx ||
1066 abs(scrollY) > editor->sizeWindow.cy)
1067 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1068 else
1069 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1070 &editor->rcFormat, &editor->rcFormat,
1071 NULL, NULL, SW_INVALIDATE);
1072 ME_Repaint(editor);
1074 if (editor->hWnd)
1076 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1077 if (editor->styleFlags & WS_HSCROLL)
1079 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1080 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1081 && (editor->styleFlags & WS_HSCROLL))
1082 || (editor->styleFlags & ES_DISABLENOSCROLL);
1083 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1084 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1085 bScrollBarWillBeVisible);
1088 if (editor->styleFlags & WS_VSCROLL)
1090 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1091 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1092 && (editor->styleFlags & WS_VSCROLL)
1093 && (editor->styleFlags & ES_MULTILINE))
1094 || (editor->styleFlags & ES_DISABLENOSCROLL);
1095 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1096 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1097 bScrollBarWillBeVisible);
1100 ME_UpdateScrollBar(editor);
1103 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1105 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1108 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1110 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1113 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1115 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1118 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1120 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1123 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1125 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1128 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1130 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1133 /* Calculates the visibility after a call to SetScrollRange or
1134 * SetScrollInfo with SIF_RANGE. */
1135 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1137 if (si->fMask & SIF_DISABLENOSCROLL)
1138 return TRUE;
1140 /* This must match the check in SetScrollInfo to determine whether
1141 * to show or hide the scrollbars. */
1142 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1145 void ME_UpdateScrollBar(ME_TextEditor *editor)
1147 /* Note that this is the only function that should ever call
1148 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1150 SCROLLINFO si;
1151 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1153 if (ME_WrapMarkedParagraphs(editor))
1154 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1156 si.cbSize = sizeof(si);
1157 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1158 si.nMin = 0;
1159 if (editor->styleFlags & ES_DISABLENOSCROLL)
1160 si.fMask |= SIF_DISABLENOSCROLL;
1162 /* Update horizontal scrollbar */
1163 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1164 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1165 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1167 ME_HScrollAbs(editor, 0);
1168 /* ME_HScrollAbs will call this function,
1169 * so nothing else needs to be done here. */
1170 return;
1173 si.nMax = editor->nTotalWidth;
1174 si.nPos = editor->horz_si.nPos;
1175 si.nPage = editor->sizeWindow.cx;
1177 if (si.nMax != editor->horz_si.nMax ||
1178 si.nPage != editor->horz_si.nPage)
1180 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1181 editor->horz_si.nMax = si.nMax;
1182 editor->horz_si.nPage = si.nPage;
1183 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1184 editor->styleFlags & WS_HSCROLL)
1186 if (si.nMax > 0xFFFF)
1188 /* Native scales the scrollbar info to 16-bit external values. */
1189 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1190 si.nMax = 0xFFFF;
1192 if (editor->hWnd) {
1193 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1194 } else {
1195 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1196 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1198 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1199 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1203 if (editor->styleFlags & WS_HSCROLL)
1205 if (si.fMask & SIF_DISABLENOSCROLL) {
1206 bScrollBarWillBeVisible = TRUE;
1207 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1208 bScrollBarWillBeVisible = FALSE;
1211 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1212 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1215 /* Update vertical scrollbar */
1216 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1217 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1218 (editor->styleFlags & ES_MULTILINE);
1220 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1222 ME_VScrollAbs(editor, 0);
1223 /* ME_VScrollAbs will call this function,
1224 * so nothing else needs to be done here. */
1225 return;
1228 si.nMax = editor->nTotalLength;
1229 si.nPos = editor->vert_si.nPos;
1230 si.nPage = editor->sizeWindow.cy;
1232 if (si.nMax != editor->vert_si.nMax ||
1233 si.nPage != editor->vert_si.nPage)
1235 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1236 editor->vert_si.nMax = si.nMax;
1237 editor->vert_si.nPage = si.nPage;
1238 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1239 editor->styleFlags & WS_VSCROLL)
1241 if (si.nMax > 0xFFFF)
1243 /* Native scales the scrollbar info to 16-bit external values. */
1244 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1245 si.nMax = 0xFFFF;
1247 if (editor->hWnd) {
1248 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1249 } else {
1250 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1251 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1253 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1254 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1258 if (editor->styleFlags & WS_VSCROLL)
1260 if (si.fMask & SIF_DISABLENOSCROLL) {
1261 bScrollBarWillBeVisible = TRUE;
1262 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1263 bScrollBarWillBeVisible = FALSE;
1266 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1267 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1268 bScrollBarWillBeVisible);
1272 void editor_ensure_visible( ME_TextEditor *editor, ME_Cursor *cursor )
1274 ME_Run *run = &cursor->pRun->member.run;
1275 ME_Row *row = row_from_cursor( cursor );
1276 ME_Paragraph *para = &cursor->pPara->member.para;
1277 int x, y, yheight;
1280 if (editor->styleFlags & ES_AUTOHSCROLL)
1282 x = run->pt.x + ME_PointFromChar( editor, run, cursor->nOffset, TRUE );
1283 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1284 x = x + 1 - editor->sizeWindow.cx;
1285 else if (x > editor->horz_si.nPos)
1286 x = editor->horz_si.nPos;
1288 if (~editor->styleFlags & ES_AUTOVSCROLL)
1290 ME_HScrollAbs(editor, x);
1291 return;
1294 else
1296 if (~editor->styleFlags & ES_AUTOVSCROLL) return;
1297 x = editor->horz_si.nPos;
1300 y = para->pt.y + row->pt.y;
1301 yheight = row->nHeight;
1303 if (y < editor->vert_si.nPos)
1304 ME_ScrollAbs(editor, x, y);
1305 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1306 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1307 else if (x != editor->horz_si.nPos)
1308 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1312 void
1313 ME_InvalidateSelection(ME_TextEditor *editor)
1315 ME_Paragraph *sel_start, *sel_end;
1316 ME_Paragraph *repaint_start = NULL, *repaint_end = NULL;
1317 int nStart, nEnd;
1318 int len = ME_GetTextLength(editor);
1320 ME_GetSelectionOfs(editor, &nStart, &nEnd);
1321 /* if both old and new selection are 0-char (= caret only), then
1322 there's no (inverted) area to be repainted, neither old nor new */
1323 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1324 return;
1325 ME_WrapMarkedParagraphs(editor);
1326 editor_get_selection_paras( editor, &sel_start, &sel_end );
1328 /* last selection markers aren't always updated, which means
1329 * they can point past the end of the document */
1330 if (editor->nLastSelStart > len || editor->nLastSelEnd > len)
1332 repaint_start = editor_first_para( editor );
1333 repaint_end = para_prev( editor_end_para( editor ) );
1335 else
1337 /* if the start part of selection is being expanded or contracted... */
1338 if (nStart < editor->nLastSelStart)
1340 repaint_start = sel_start;
1341 repaint_end = editor->last_sel_start_para;
1343 else if (nStart > editor->nLastSelStart)
1345 repaint_start = editor->last_sel_start_para;
1346 repaint_end = sel_start;
1349 /* if the end part of selection is being contracted or expanded... */
1350 if (nEnd < editor->nLastSelEnd)
1352 if (!repaint_start) repaint_start = sel_end;
1353 repaint_end = editor->last_sel_end_para;
1355 else if (nEnd > editor->nLastSelEnd)
1357 if (!repaint_start) repaint_start = editor->last_sel_end_para;
1358 repaint_end = sel_end;
1362 if (repaint_start)
1363 para_range_invalidate( editor, repaint_start, repaint_end );
1364 /* remember the last invalidated position */
1365 ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1366 editor_get_selection_paras( editor, &editor->last_sel_start_para, &editor->last_sel_end_para );
1369 BOOL
1370 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1372 /* TODO: Zoom images and objects */
1374 if (numerator == 0 && denominator == 0)
1376 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1377 return TRUE;
1379 if (numerator <= 0 || denominator <= 0)
1380 return FALSE;
1381 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1382 return FALSE;
1384 editor->nZoomNumerator = numerator;
1385 editor->nZoomDenominator = denominator;
1387 ME_RewrapRepaint(editor);
1388 return TRUE;