wined3d: No longer make sub-resources full resources.
[wine.git] / dlls / riched20 / paint.c
blob85805398b6b9c48dcfa7f8e3a6ba529cf3dc1323
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 ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph);
28 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, const RECT *rcUpdate)
30 ME_DisplayItem *item;
31 ME_Context c;
32 int ys, ye;
33 HRGN oldRgn;
35 oldRgn = CreateRectRgn(0, 0, 0, 0);
36 if (!GetClipRgn(hDC, oldRgn))
38 DeleteObject(oldRgn);
39 oldRgn = NULL;
41 IntersectClipRect(hDC, rcUpdate->left, rcUpdate->top,
42 rcUpdate->right, rcUpdate->bottom);
44 ME_InitContext(&c, editor, hDC);
45 SetBkMode(hDC, TRANSPARENT);
46 ME_MoveCaret(editor);
47 item = editor->pBuffer->pFirst->next;
48 /* This context point is an offset for the paragraph positions stored
49 * during wrapping. It shouldn't be modified during painting. */
50 c.pt.x = c.rcView.left - editor->horz_si.nPos;
51 c.pt.y = c.rcView.top - editor->vert_si.nPos;
52 while(item != editor->pBuffer->pLast)
54 assert(item->type == diParagraph);
56 ys = c.pt.y + item->member.para.pt.y;
57 if (item->member.para.pCell
58 != item->member.para.next_para->member.para.pCell)
60 ME_Cell *cell = NULL;
61 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;
66 if (item->member.para.pCell && !(item->member.para.nFlags & MEPF_ROWEND) &&
67 item->member.para.pCell != item->member.para.prev_para->member.para.pCell)
69 /* the border shifts the text down */
70 ys -= item->member.para.pCell->member.cell.yTextOffset;
73 /* Draw the paragraph if any of the paragraph is in the update region. */
74 if (ys < rcUpdate->bottom && ye > rcUpdate->top)
75 ME_DrawParagraph(&c, item);
76 item = item->member.para.next_para;
78 if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
80 /* Fill space after the end of the text. */
81 RECT rc;
82 rc.top = c.pt.y + editor->nTotalLength;
83 rc.left = c.rcView.left;
84 rc.bottom = c.rcView.bottom;
85 rc.right = c.rcView.right;
87 IntersectRect(&rc, &rc, rcUpdate);
89 if (!IsRectEmpty(&rc))
90 FillRect(hDC, &rc, c.editor->hbrBackground);
92 if (editor->nTotalLength != editor->nLastTotalLength ||
93 editor->nTotalWidth != editor->nLastTotalWidth)
94 ME_SendRequestResize(editor, FALSE);
95 editor->nLastTotalLength = editor->nTotalLength;
96 editor->nLastTotalWidth = editor->nTotalWidth;
98 SelectClipRgn(hDC, oldRgn);
99 if (oldRgn)
100 DeleteObject(oldRgn);
102 c.hDC = NULL;
103 ME_DestroyContext(&c);
106 void ME_Repaint(ME_TextEditor *editor)
108 if (ME_WrapMarkedParagraphs(editor))
110 ME_UpdateScrollBar(editor);
111 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
113 ITextHost_TxViewChange(editor->texthost, TRUE);
116 void ME_UpdateRepaint(ME_TextEditor *editor, BOOL update_now)
118 /* Should be called whenever the contents of the control have changed */
119 BOOL wrappedParagraphs;
121 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
122 if (wrappedParagraphs)
123 ME_UpdateScrollBar(editor);
125 /* Ensure that the cursor is visible */
126 ME_EnsureVisible(editor, &editor->pCursors[0]);
128 ITextHost_TxViewChange(editor->texthost, update_now);
130 ME_SendSelChange(editor);
132 /* send EN_CHANGE if the event mask asks for it */
133 if(editor->nEventMask & ENM_CHANGE)
135 editor->nEventMask &= ~ENM_CHANGE;
136 ME_SendOldNotify(editor, EN_CHANGE);
137 editor->nEventMask |= ENM_CHANGE;
141 void
142 ME_RewrapRepaint(ME_TextEditor *editor)
144 /* RewrapRepaint should be called whenever the control has changed in
145 * looks, but not content. Like resizing. */
147 ME_MarkAllForWrapping(editor);
148 ME_WrapMarkedParagraphs(editor);
149 ME_UpdateScrollBar(editor);
150 ME_Repaint(editor);
153 int ME_twips2pointsX(const ME_Context *c, int x)
155 if (c->editor->nZoomNumerator == 0)
156 return x * c->dpi.cx / 1440;
157 else
158 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
161 int ME_twips2pointsY(const ME_Context *c, int y)
163 if (c->editor->nZoomNumerator == 0)
164 return y * c->dpi.cy / 1440;
165 else
166 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
170 static int calc_y_offset( const ME_Context *c, ME_Style *style )
172 int offs = 0, twips = 0;
174 if ((style->fmt.dwMask & style->fmt.dwEffects) & CFM_OFFSET)
175 twips = style->fmt.yOffset;
177 if ((style->fmt.dwMask & style->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT))
179 if (style->fmt.dwEffects & CFE_SUPERSCRIPT) twips = style->fmt.yHeight/3;
180 if (style->fmt.dwEffects & CFE_SUBSCRIPT) twips = -style->fmt.yHeight/12;
183 if (twips) offs = ME_twips2pointsY( c, twips );
185 return offs;
188 static COLORREF get_text_color( ME_Context *c, ME_Style *style, BOOL highlight )
190 COLORREF color;
192 if (highlight)
193 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHTTEXT );
194 else if ((style->fmt.dwMask & CFM_LINK) && (style->fmt.dwEffects & CFE_LINK))
195 color = RGB(0,0,255);
196 else if ((style->fmt.dwMask & CFM_COLOR) && (style->fmt.dwEffects & CFE_AUTOCOLOR))
197 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_WINDOWTEXT );
198 else
199 color = style->fmt.crTextColor;
201 return color;
204 static void get_underline_pen( ME_Style *style, COLORREF color, HPEN *pen )
206 *pen = NULL;
207 /* Choose the pen type for underlining the text. */
208 if (style->fmt.dwMask & CFM_UNDERLINETYPE)
210 switch (style->fmt.bUnderlineType)
212 case CFU_UNDERLINE:
213 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
214 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
215 *pen = CreatePen( PS_SOLID, 1, color );
216 break;
217 case CFU_UNDERLINEDOTTED:
218 *pen = CreatePen( PS_DOT, 1, color );
219 break;
220 default:
221 FIXME( "Unknown underline type (%u)\n", style->fmt.bUnderlineType );
222 /* fall through */
223 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
224 case CFU_UNDERLINENONE:
225 break;
228 return;
231 static void draw_underline( ME_Context *c, ME_Run *run, int x, int y, COLORREF color )
233 HPEN pen;
235 get_underline_pen( run->style, color, &pen );
236 if (pen)
238 HPEN old_pen = SelectObject( c->hDC, pen );
239 MoveToEx( c->hDC, x, y + 1, NULL );
240 LineTo( c->hDC, x + run->nWidth, y + 1 );
241 SelectObject( c->hDC, old_pen );
242 DeleteObject( pen );
244 return;
247 /*********************************************************************
248 * draw_space
250 * Draw the end-of-paragraph or tab space.
252 * If actually_draw is TRUE then ensure any underline is drawn.
254 static void draw_space( ME_Context *c, ME_Run *run, int x, int y,
255 BOOL selected, BOOL actually_draw, int ymin, int cy )
257 HDC hdc = c->hDC;
258 BOOL old_style_selected = FALSE;
259 RECT rect;
260 COLORREF back_color = 0;
262 SetRect( &rect, x, ymin, x + run->nWidth, ymin + cy );
264 if (c->editor->bHideSelection) selected = FALSE;
265 if (c->editor->bEmulateVersion10)
267 old_style_selected = selected;
268 selected = FALSE;
271 if (selected)
272 back_color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );
274 if (actually_draw)
276 COLORREF text_color = get_text_color( c, run->style, selected );
277 COLORREF old_text, old_back;
278 HFONT old_font = NULL;
279 int y_offset = calc_y_offset( c, run->style );
280 static const WCHAR space[1] = {' '};
282 old_font = ME_SelectStyleFont( c, run->style );
283 old_text = SetTextColor( hdc, text_color );
284 if (selected) old_back = SetBkColor( hdc, back_color );
286 ExtTextOutW( hdc, x, y - y_offset, selected ? ETO_OPAQUE : 0, &rect, space, 1, &run->nWidth );
288 if (selected) SetBkColor( hdc, old_back );
289 SetTextColor( hdc, old_text );
290 ME_UnselectStyleFont( c, run->style, old_font );
292 draw_underline( c, run, x, y - y_offset, text_color );
294 else if (selected)
296 HBRUSH brush = CreateSolidBrush( back_color );
297 FillRect( hdc, &rect, brush );
298 DeleteObject( brush );
301 if (old_style_selected)
302 PatBlt( hdc, x, ymin, run->nWidth, cy, DSTINVERT );
305 static void get_selection_rect( ME_Context *c, ME_Run *run, int from, int to, int cy, RECT *r )
307 from = max( 0, from );
308 to = min( run->len, to );
309 r->left = ME_PointFromCharContext( c, run, from, TRUE );
310 r->top = 0;
311 r->right = ME_PointFromCharContext( c, run, to, TRUE );
312 r->bottom = cy;
313 return;
317 static void draw_text( ME_Context *c, ME_Run *run, int x, int y, BOOL selected, RECT *sel_rect )
319 COLORREF text_color = get_text_color( c, run->style, selected );
320 COLORREF back_color = selected ? ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT ) : 0;
321 COLORREF old_text, old_back = 0;
322 const WCHAR *text = get_text( run, 0 );
323 ME_String *masked = NULL;
325 if (c->editor->cPasswordMask)
327 masked = ME_MakeStringR( c->editor->cPasswordMask, run->len );
328 text = masked->szData;
331 old_text = SetTextColor( c->hDC, text_color );
332 if (selected) old_back = SetBkColor( c->hDC, back_color );
334 if (run->para->nFlags & MEPF_COMPLEX)
335 ScriptTextOut( c->hDC, &run->style->script_cache, x, y, selected ? ETO_OPAQUE : 0, sel_rect,
336 &run->script_analysis, NULL, 0, run->glyphs, run->num_glyphs, run->advances,
337 NULL, run->offsets );
338 else
339 ExtTextOutW( c->hDC, x, y, selected ? ETO_OPAQUE : 0, sel_rect, text, run->len, NULL );
341 if (selected) SetBkColor( c->hDC, old_back );
342 SetTextColor( c->hDC, old_text );
344 draw_underline( c, run, x, y, text_color );
346 ME_DestroyString( masked );
347 return;
351 static void ME_DrawTextWithStyle(ME_Context *c, ME_Run *run, int x, int y,
352 int nSelFrom, int nSelTo, int ymin, int cy)
354 HDC hDC = c->hDC;
355 HGDIOBJ hOldFont;
356 int yOffset = 0;
357 BOOL selected = (nSelFrom < run->len && nSelTo >= 0
358 && nSelFrom < nSelTo && !c->editor->bHideSelection);
359 BOOL old_style_selected = FALSE;
360 RECT sel_rect;
361 HRGN clip = NULL, sel_rgn = NULL;
363 yOffset = calc_y_offset( c, run->style );
365 if (selected)
367 get_selection_rect( c, run, nSelFrom, nSelTo, cy, &sel_rect );
368 OffsetRect( &sel_rect, x, ymin );
370 if (c->editor->bEmulateVersion10)
372 old_style_selected = TRUE;
373 selected = FALSE;
375 else
377 sel_rgn = CreateRectRgnIndirect( &sel_rect );
378 clip = CreateRectRgn( 0, 0, 0, 0 );
379 if (GetClipRgn( hDC, clip ) != 1)
381 DeleteObject( clip );
382 clip = NULL;
387 hOldFont = ME_SelectStyleFont( c, run->style );
389 if (sel_rgn) ExtSelectClipRgn( hDC, sel_rgn, RGN_DIFF );
390 draw_text( c, run, x, y - yOffset, FALSE, NULL );
391 if (sel_rgn)
393 ExtSelectClipRgn( hDC, clip, RGN_COPY );
394 ExtSelectClipRgn( hDC, sel_rgn, RGN_AND );
395 draw_text( c, run, x, y - yOffset, TRUE, &sel_rect );
396 ExtSelectClipRgn( hDC, clip, RGN_COPY );
397 if (clip) DeleteObject( clip );
398 DeleteObject( sel_rgn );
401 if (old_style_selected)
402 PatBlt( hDC, sel_rect.left, ymin, sel_rect.right - sel_rect.left, cy, DSTINVERT );
404 ME_UnselectStyleFont(c, run->style, hOldFont);
407 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
408 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
409 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
410 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
411 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
412 SelectObject(hDC, hFont);
413 SetTextAlign(hDC, align);
414 SetTextColor(hDC, color);
417 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
419 ME_Run *run = &rundi->member.run;
420 ME_DisplayItem *start;
421 int runofs = run->nCharOfs+para->nCharOfs;
422 int nSelFrom, nSelTo;
424 if (run->nFlags & MERF_HIDDEN)
425 return;
427 start = ME_FindItemBack(rundi, diStartRow);
428 ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
430 /* Draw selected end-of-paragraph mark */
431 if (run->nFlags & MERF_ENDPARA)
433 if (runofs >= nSelFrom && runofs < nSelTo)
435 draw_space( c, run, x, y, TRUE, FALSE,
436 c->pt.y + para->pt.y + start->member.row.pt.y,
437 start->member.row.nHeight );
439 return;
442 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
444 BOOL selected = runofs >= nSelFrom && runofs < nSelTo;
446 draw_space( c, run, x, y, selected, TRUE,
447 c->pt.y + para->pt.y + start->member.row.pt.y,
448 start->member.row.nHeight );
449 return;
452 if (run->nFlags & MERF_GRAPHICS)
453 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
454 else
456 ME_DrawTextWithStyle(c, run, x, y, nSelFrom - runofs, nSelTo - runofs,
457 c->pt.y + para->pt.y + start->member.row.pt.y,
458 start->member.row.nHeight);
462 /* The documented widths are in points (72 dpi), but converting them to
463 * 96 dpi (standard display resolution) avoids dealing with fractions. */
464 static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
465 /* none */ {0, PS_SOLID, FALSE},
466 /* 3/4 */ {1, PS_SOLID, FALSE},
467 /* 1 1/2 */ {2, PS_SOLID, FALSE},
468 /* 2 1/4 */ {3, PS_SOLID, FALSE},
469 /* 3 */ {4, PS_SOLID, FALSE},
470 /* 4 1/2 */ {6, PS_SOLID, FALSE},
471 /* 6 */ {8, PS_SOLID, FALSE},
472 /* 3/4 double */ {1, PS_SOLID, TRUE},
473 /* 1 1/2 double */ {2, PS_SOLID, TRUE},
474 /* 2 1/4 double */ {3, PS_SOLID, TRUE},
475 /* 3/4 gray */ {1, PS_DOT /* FIXME */, FALSE},
476 /* 1 1/2 dashed */ {2, PS_DASH, FALSE},
479 static const COLORREF pen_colors[16] = {
480 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
481 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
482 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
483 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
484 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
485 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
486 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
487 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
490 static int ME_GetBorderPenWidth(const ME_Context* c, int idx)
492 int width = border_details[idx].width;
494 if (c->dpi.cx != 96)
495 width = MulDiv(width, c->dpi.cx, 96);
497 if (c->editor->nZoomNumerator != 0)
498 width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
500 return width;
503 int ME_GetParaBorderWidth(const ME_Context* c, int flags)
505 int idx = (flags >> 8) & 0xF;
506 int width;
508 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
510 FIXME("Unsupported border value %d\n", idx);
511 return 0;
513 width = ME_GetBorderPenWidth(c, idx);
514 if (border_details[idx].dble) width = width * 2 + 1;
515 return width;
518 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
520 int idx, border_width, top_border, bottom_border;
521 RECT rc;
522 BOOL hasParaBorder;
524 SetRectEmpty(bounds);
525 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
527 border_width = top_border = bottom_border = 0;
528 idx = (para->pFmt->wBorders >> 8) & 0xF;
529 hasParaBorder = (!(c->editor->bEmulateVersion10 &&
530 para->pFmt->dwMask & PFM_TABLE &&
531 para->pFmt->wEffects & PFE_TABLE) &&
532 (para->pFmt->dwMask & PFM_BORDER) &&
533 idx != 0 &&
534 (para->pFmt->wBorders & 0xF));
535 if (hasParaBorder)
537 /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
538 * controls. It actually stores the paragraph or row border style. Although
539 * the value isn't used for drawing, it is used for streaming out rich text.
541 * wBorders stores the border style for each side (top, left, bottom, right)
542 * using nibble (4 bits) to store each border style. The rich text format
543 * control words, and their associated value are the following:
544 * \brdrdash 0
545 * \brdrdashsm 1
546 * \brdrdb 2
547 * \brdrdot 3
548 * \brdrhair 4
549 * \brdrs 5
550 * \brdrth 6
551 * \brdrtriple 7
553 * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
554 * The mask corresponding to each side for the version are the following:
555 * mask v1.0-3.0 v4.1
556 * 0x000F top left
557 * 0x00F0 left top
558 * 0x0F00 bottom right
559 * 0xF000 right bottom
561 if (para->pFmt->wBorders & 0x00B0)
562 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
563 border_width = ME_GetParaBorderWidth(c, para->pFmt->wBorders);
564 if (para->pFmt->wBorders & 4) top_border = border_width;
565 if (para->pFmt->wBorders & 8) bottom_border = border_width;
568 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
570 rc.left = c->rcView.left;
571 rc.right = c->rcView.right;
572 rc.top = y;
573 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
574 rc.bottom = y + bounds->top + top_border;
575 FillRect(c->hDC, &rc, c->editor->hbrBackground);
578 if (para->pFmt->dwMask & PFM_SPACEAFTER)
580 rc.left = c->rcView.left;
581 rc.right = c->rcView.right;
582 rc.bottom = y + para->nHeight;
583 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
584 rc.top = rc.bottom - bounds->bottom - bottom_border;
585 FillRect(c->hDC, &rc, c->editor->hbrBackground);
588 /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
589 * but might support it in later versions. */
590 if (hasParaBorder) {
591 int pen_width, rightEdge;
592 COLORREF pencr;
593 HPEN pen = NULL, oldpen = NULL;
594 POINT pt;
596 if (para->pFmt->wBorders & 64) /* autocolor */
597 pencr = ITextHost_TxGetSysColor(c->editor->texthost,
598 COLOR_WINDOWTEXT);
599 else
600 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
602 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
603 c->editor->nTotalWidth);
605 pen_width = ME_GetBorderPenWidth(c, idx);
606 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
607 oldpen = SelectObject(c->hDC, pen);
608 MoveToEx(c->hDC, 0, 0, &pt);
610 /* before & after spaces are not included in border */
612 /* helper to draw the double lines in case of corner */
613 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
615 if (para->pFmt->wBorders & 1)
617 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
618 LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
619 if (border_details[idx].dble) {
620 rc.left = c->pt.x + 1;
621 rc.right = rc.left + border_width;
622 rc.top = y + bounds->top;
623 rc.bottom = y + para->nHeight - bounds->bottom;
624 FillRect(c->hDC, &rc, c->editor->hbrBackground);
625 MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
626 LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
628 bounds->left += border_width;
630 if (para->pFmt->wBorders & 2)
632 MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
633 LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
634 if (border_details[idx].dble) {
635 rc.left = rightEdge - pen_width - 1;
636 rc.right = rc.left + pen_width;
637 rc.top = y + bounds->top;
638 rc.bottom = y + para->nHeight - bounds->bottom;
639 FillRect(c->hDC, &rc, c->editor->hbrBackground);
640 MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
641 LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
643 bounds->right += border_width;
645 if (para->pFmt->wBorders & 4)
647 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
648 LineTo(c->hDC, rightEdge, y + bounds->top);
649 if (border_details[idx].dble) {
650 MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
651 LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
653 bounds->top += border_width;
655 if (para->pFmt->wBorders & 8)
657 MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
658 LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
659 if (border_details[idx].dble) {
660 MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
661 LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
663 bounds->bottom += border_width;
665 #undef DD
667 MoveToEx(c->hDC, pt.x, pt.y, NULL);
668 SelectObject(c->hDC, oldpen);
669 DeleteObject(pen);
673 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
675 ME_Paragraph *para = &paragraph->member.para;
676 if (!c->editor->bEmulateVersion10) /* v4.1 */
678 if (para->pCell)
680 RECT rc;
681 ME_Cell *cell = &para->pCell->member.cell;
682 ME_DisplayItem *paraAfterRow;
683 HPEN pen, oldPen;
684 LOGBRUSH logBrush;
685 HBRUSH brush;
686 COLORREF color;
687 POINT oldPt;
688 int width;
689 BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
690 BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
691 int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
692 int bottom = (atBottom ?
693 c->pt.y + cell->pt.y + cell->nHeight :
694 top + para->nHeight + (atTop ? cell->yTextOffset : 0));
695 rc.left = c->pt.x + cell->pt.x;
696 rc.right = rc.left + cell->nWidth;
697 if (atTop) {
698 /* Erase gap before text if not all borders are the same height. */
699 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
700 rc.top = top + width;
701 width = cell->yTextOffset - width;
702 rc.bottom = rc.top + width;
703 if (width) {
704 FillRect(c->hDC, &rc, c->editor->hbrBackground);
707 /* Draw cell borders.
708 * The order borders are draw in is left, top, bottom, right in order
709 * to be consistent with native richedit. This is noticeable from the
710 * overlap of borders of different colours. */
711 if (!(para->nFlags & MEPF_ROWEND)) {
712 rc.top = top;
713 rc.bottom = bottom;
714 if (cell->border.left.width > 0)
716 color = cell->border.left.colorRef;
717 width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
718 } else {
719 color = RGB(192,192,192);
720 width = 1;
722 logBrush.lbStyle = BS_SOLID;
723 logBrush.lbColor = color;
724 logBrush.lbHatch = 0;
725 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
726 width, &logBrush, 0, NULL);
727 oldPen = SelectObject(c->hDC, pen);
728 MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
729 LineTo(c->hDC, rc.left, rc.bottom);
730 SelectObject(c->hDC, oldPen);
731 DeleteObject(pen);
732 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
735 if (atTop) {
736 if (cell->border.top.width > 0)
738 brush = CreateSolidBrush(cell->border.top.colorRef);
739 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
740 } else {
741 brush = GetStockObject(LTGRAY_BRUSH);
742 width = 1;
744 rc.top = top;
745 rc.bottom = rc.top + width;
746 FillRect(c->hDC, &rc, brush);
747 if (cell->border.top.width > 0)
748 DeleteObject(brush);
751 /* Draw the bottom border if at the last paragraph in the cell, and when
752 * in the last row of the table. */
753 if (atBottom) {
754 int oldLeft = rc.left;
755 width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
756 paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
757 if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
758 ME_DisplayItem *nextEndCell;
759 nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
760 assert(nextEndCell && !nextEndCell->member.cell.next_cell);
761 rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
762 /* FIXME: Native draws FROM the bottom of the table rather than
763 * TO the bottom of the table in this case, but just doing so here
764 * will cause the next row to erase the border. */
766 rc.top = bottom;
767 rc.bottom = rc.top + width;
770 if (rc.left < rc.right) {
771 if (cell->border.bottom.width > 0) {
772 brush = CreateSolidBrush(cell->border.bottom.colorRef);
773 } else {
774 brush = GetStockObject(LTGRAY_BRUSH);
776 rc.bottom = bottom;
777 rc.top = rc.bottom - width;
778 FillRect(c->hDC, &rc, brush);
779 if (cell->border.bottom.width > 0)
780 DeleteObject(brush);
782 rc.left = oldLeft;
785 /* Right border only drawn if at the end of the table row. */
786 if (!cell->next_cell->member.cell.next_cell &&
787 !(para->nFlags & MEPF_ROWSTART))
789 rc.top = top;
790 rc.bottom = bottom;
791 if (cell->border.right.width > 0) {
792 color = cell->border.right.colorRef;
793 width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
794 } else {
795 color = RGB(192,192,192);
796 width = 1;
798 logBrush.lbStyle = BS_SOLID;
799 logBrush.lbColor = color;
800 logBrush.lbHatch = 0;
801 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
802 width, &logBrush, 0, NULL);
803 oldPen = SelectObject(c->hDC, pen);
804 MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
805 LineTo(c->hDC, rc.right - 1, rc.bottom);
806 SelectObject(c->hDC, oldPen);
807 DeleteObject(pen);
808 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
811 } else { /* v1.0 - 3.0 */
812 /* Draw simple table border */
813 if (para->pFmt->dwMask & PFM_TABLE && para->pFmt->wEffects & PFE_TABLE) {
814 HPEN pen = NULL, oldpen = NULL;
815 int i, firstX, startX, endX, rowY, rowBottom, nHeight;
816 POINT oldPt;
817 PARAFORMAT2 *pNextFmt;
819 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
820 oldpen = SelectObject(c->hDC, pen);
822 /* Find the start relative to the text */
823 firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
824 /* Go back by the horizontal gap, which is stored in dxOffset */
825 firstX -= ME_twips2pointsX(c, para->pFmt->dxOffset);
826 /* The left edge, stored in dxStartIndent affected just the first edge */
827 startX = firstX - ME_twips2pointsX(c, para->pFmt->dxStartIndent);
828 rowY = c->pt.y + para->pt.y;
829 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
830 rowY += ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
831 nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
832 rowBottom = rowY + nHeight;
834 /* Draw horizontal lines */
835 MoveToEx(c->hDC, firstX, rowY, &oldPt);
836 i = para->pFmt->cTabCount - 1;
837 endX = startX + ME_twips2pointsX(c, para->pFmt->rgxTabs[i] & 0x00ffffff) + 1;
838 LineTo(c->hDC, endX, rowY);
839 pNextFmt = para->next_para->member.para.pFmt;
840 /* The bottom of the row only needs to be drawn if the next row is
841 * not a table. */
842 if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
843 para->nRows == 1))
845 /* Decrement rowBottom to draw the bottom line within the row, and
846 * to not draw over this line when drawing the vertical lines. */
847 rowBottom--;
848 MoveToEx(c->hDC, firstX, rowBottom, NULL);
849 LineTo(c->hDC, endX, rowBottom);
852 /* Draw vertical lines */
853 MoveToEx(c->hDC, firstX, rowY, NULL);
854 LineTo(c->hDC, firstX, rowBottom);
855 for (i = 0; i < para->pFmt->cTabCount; i++)
857 int rightBoundary = para->pFmt->rgxTabs[i] & 0x00ffffff;
858 endX = startX + ME_twips2pointsX(c, rightBoundary);
859 MoveToEx(c->hDC, endX, rowY, NULL);
860 LineTo(c->hDC, endX, rowBottom);
863 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
864 SelectObject(c->hDC, oldpen);
865 DeleteObject(pen);
870 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
872 int align = SetTextAlign(c->hDC, TA_BASELINE);
873 ME_DisplayItem *p;
874 ME_Run *run;
875 ME_Paragraph *para = NULL;
876 RECT rc, bounds;
877 int y;
878 int height = 0, baseline = 0, no=0;
879 BOOL visible = FALSE;
881 rc.left = c->pt.x;
882 rc.right = c->rcView.right;
884 assert(paragraph);
885 para = &paragraph->member.para;
886 y = c->pt.y + para->pt.y;
887 if (para->pCell)
889 ME_Cell *cell = &para->pCell->member.cell;
890 rc.left = c->pt.x + cell->pt.x;
891 rc.right = rc.left + cell->nWidth;
893 if (para->nFlags & MEPF_ROWSTART) {
894 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
895 rc.right = c->pt.x + cell->pt.x;
896 } else if (para->nFlags & MEPF_ROWEND) {
897 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
898 rc.left = c->pt.x + cell->pt.x + cell->nWidth;
900 ME_DrawParaDecoration(c, para, y, &bounds);
901 y += bounds.top;
902 if (bounds.left || bounds.right) {
903 rc.left = max(rc.left, c->pt.x + bounds.left);
904 rc.right = min(rc.right, c->pt.x - bounds.right
905 + max(c->editor->sizeWindow.cx,
906 c->editor->nTotalWidth));
909 for (p = paragraph->next; p != para->next_para; p = p->next)
911 switch(p->type) {
912 case diParagraph:
913 assert(FALSE);
914 break;
915 case diStartRow:
916 y += height;
917 rc.top = y;
918 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
919 rc.bottom = y + para->nHeight;
920 } else {
921 rc.bottom = y + p->member.row.nHeight;
923 visible = RectVisible(c->hDC, &rc);
924 if (visible) {
925 FillRect(c->hDC, &rc, c->editor->hbrBackground);
927 if (bounds.right)
929 /* If scrolled to the right past the end of the text, then
930 * there may be space to the right of the paragraph border. */
931 RECT rcAfterBrdr = rc;
932 rcAfterBrdr.left = rc.right + bounds.right;
933 rcAfterBrdr.right = c->rcView.right;
934 if (RectVisible(c->hDC, &rcAfterBrdr))
935 FillRect(c->hDC, &rcAfterBrdr, c->editor->hbrBackground);
937 if (me_debug)
939 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
940 WCHAR buf[128];
941 POINT pt = c->pt;
942 wsprintfW(buf, wszRowDebug, no);
943 pt.y = 12+y;
944 ME_DebugWrite(c->hDC, &pt, buf);
947 height = p->member.row.nHeight;
948 baseline = p->member.row.nBaseline;
949 break;
950 case diRun:
951 assert(para);
952 run = &p->member.run;
953 if (visible && me_debug) {
954 RECT rc;
955 rc.left = c->pt.x + run->pt.x;
956 rc.right = rc.left + run->nWidth;
957 rc.top = c->pt.y + para->pt.y + run->pt.y;
958 rc.bottom = rc.top + height;
959 TRACE("rc = %s\n", wine_dbgstr_rect(&rc));
960 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
962 if (visible)
963 ME_DrawRun(c, c->pt.x + run->pt.x,
964 c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
965 if (me_debug)
967 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
968 WCHAR buf[2560];
969 POINT pt;
970 pt.x = c->pt.x + run->pt.x;
971 pt.y = c->pt.y + para->pt.y + run->pt.y;
972 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, get_text( &p->member.run, 0 ));
973 ME_DebugWrite(c->hDC, &pt, buf);
975 break;
976 case diCell:
977 /* Clear any space at the bottom of the cell after the text. */
978 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
979 break;
980 y += height;
981 rc.top = c->pt.y + para->pt.y + para->nHeight;
982 rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
983 if (RectVisible(c->hDC, &rc))
985 FillRect(c->hDC, &rc, c->editor->hbrBackground);
987 break;
988 default:
989 break;
991 no++;
994 ME_DrawTableBorders(c, paragraph);
996 SetTextAlign(c->hDC, align);
999 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1001 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1002 int scrollX = 0, scrollY = 0;
1004 if (editor->horz_si.nPos != x) {
1005 x = min(x, editor->horz_si.nMax);
1006 x = max(x, editor->horz_si.nMin);
1007 scrollX = editor->horz_si.nPos - x;
1008 editor->horz_si.nPos = x;
1009 if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
1010 x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
1011 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1014 if (editor->vert_si.nPos != y) {
1015 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1016 y = max(y, editor->vert_si.nMin);
1017 scrollY = editor->vert_si.nPos - y;
1018 editor->vert_si.nPos = y;
1019 if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1020 y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1021 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1024 if (abs(scrollX) > editor->sizeWindow.cx ||
1025 abs(scrollY) > editor->sizeWindow.cy)
1026 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1027 else
1028 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1029 &editor->rcFormat, &editor->rcFormat,
1030 NULL, NULL, SW_INVALIDATE);
1031 ME_Repaint(editor);
1033 if (editor->hWnd)
1035 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1036 if (editor->styleFlags & WS_HSCROLL)
1038 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1039 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1040 && (editor->styleFlags & WS_HSCROLL))
1041 || (editor->styleFlags & ES_DISABLENOSCROLL);
1042 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1043 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1044 bScrollBarWillBeVisible);
1047 if (editor->styleFlags & WS_VSCROLL)
1049 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1050 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1051 && (editor->styleFlags & WS_VSCROLL)
1052 && (editor->styleFlags & ES_MULTILINE))
1053 || (editor->styleFlags & ES_DISABLENOSCROLL);
1054 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1055 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1056 bScrollBarWillBeVisible);
1059 ME_UpdateScrollBar(editor);
1062 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1064 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1067 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1069 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1072 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1074 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1077 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1079 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1082 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1084 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1087 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1089 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1092 /* Calculates the visibility after a call to SetScrollRange or
1093 * SetScrollInfo with SIF_RANGE. */
1094 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1096 if (si->fMask & SIF_DISABLENOSCROLL)
1097 return TRUE;
1099 /* This must match the check in SetScrollInfo to determine whether
1100 * to show or hide the scrollbars. */
1101 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1104 void ME_UpdateScrollBar(ME_TextEditor *editor)
1106 /* Note that this is the only function that should ever call
1107 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1109 SCROLLINFO si;
1110 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1112 if (ME_WrapMarkedParagraphs(editor))
1113 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1115 si.cbSize = sizeof(si);
1116 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1117 si.nMin = 0;
1118 if (editor->styleFlags & ES_DISABLENOSCROLL)
1119 si.fMask |= SIF_DISABLENOSCROLL;
1121 /* Update horizontal scrollbar */
1122 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1123 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1124 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1126 ME_HScrollAbs(editor, 0);
1127 /* ME_HScrollAbs will call this function,
1128 * so nothing else needs to be done here. */
1129 return;
1132 si.nMax = editor->nTotalWidth;
1133 si.nPos = editor->horz_si.nPos;
1134 si.nPage = editor->sizeWindow.cx;
1136 if (si.nMax != editor->horz_si.nMax ||
1137 si.nPage != editor->horz_si.nPage)
1139 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1140 editor->horz_si.nMax = si.nMax;
1141 editor->horz_si.nPage = si.nPage;
1142 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1143 editor->styleFlags & WS_HSCROLL)
1145 if (si.nMax > 0xFFFF)
1147 /* Native scales the scrollbar info to 16-bit external values. */
1148 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1149 si.nMax = 0xFFFF;
1151 if (editor->hWnd) {
1152 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1153 } else {
1154 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1155 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1157 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1158 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1162 if (editor->styleFlags & WS_HSCROLL)
1164 if (si.fMask & SIF_DISABLENOSCROLL) {
1165 bScrollBarWillBeVisible = TRUE;
1166 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1167 bScrollBarWillBeVisible = FALSE;
1170 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1171 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1174 /* Update vertical scrollbar */
1175 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1176 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1177 (editor->styleFlags & ES_MULTILINE);
1179 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1181 ME_VScrollAbs(editor, 0);
1182 /* ME_VScrollAbs will call this function,
1183 * so nothing else needs to be done here. */
1184 return;
1187 si.nMax = editor->nTotalLength;
1188 si.nPos = editor->vert_si.nPos;
1189 si.nPage = editor->sizeWindow.cy;
1191 if (si.nMax != editor->vert_si.nMax ||
1192 si.nPage != editor->vert_si.nPage)
1194 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1195 editor->vert_si.nMax = si.nMax;
1196 editor->vert_si.nPage = si.nPage;
1197 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1198 editor->styleFlags & WS_VSCROLL)
1200 if (si.nMax > 0xFFFF)
1202 /* Native scales the scrollbar info to 16-bit external values. */
1203 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1204 si.nMax = 0xFFFF;
1206 if (editor->hWnd) {
1207 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1208 } else {
1209 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1210 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1212 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1213 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1217 if (editor->styleFlags & WS_VSCROLL)
1219 if (si.fMask & SIF_DISABLENOSCROLL) {
1220 bScrollBarWillBeVisible = TRUE;
1221 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1222 bScrollBarWillBeVisible = FALSE;
1225 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1226 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1227 bScrollBarWillBeVisible);
1231 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1233 ME_Run *pRun = &pCursor->pRun->member.run;
1234 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1235 ME_DisplayItem *pPara = pCursor->pPara;
1236 int x, y, yheight;
1238 assert(pRow);
1239 assert(pPara);
1241 if (editor->styleFlags & ES_AUTOHSCROLL)
1243 x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset, TRUE);
1244 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1245 x = x + 1 - editor->sizeWindow.cx;
1246 else if (x > editor->horz_si.nPos)
1247 x = editor->horz_si.nPos;
1249 if (~editor->styleFlags & ES_AUTOVSCROLL)
1251 ME_HScrollAbs(editor, x);
1252 return;
1254 } else {
1255 if (~editor->styleFlags & ES_AUTOVSCROLL)
1256 return;
1257 x = editor->horz_si.nPos;
1260 y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1261 yheight = pRow->member.row.nHeight;
1263 if (y < editor->vert_si.nPos)
1264 ME_ScrollAbs(editor, x, y);
1265 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1266 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1267 else if (x != editor->horz_si.nPos)
1268 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1272 void
1273 ME_InvalidateSelection(ME_TextEditor *editor)
1275 ME_DisplayItem *sel_start, *sel_end;
1276 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
1277 int nStart, nEnd;
1278 int len = ME_GetTextLength(editor);
1280 ME_GetSelectionOfs(editor, &nStart, &nEnd);
1281 /* if both old and new selection are 0-char (= caret only), then
1282 there's no (inverted) area to be repainted, neither old nor new */
1283 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1284 return;
1285 ME_WrapMarkedParagraphs(editor);
1286 ME_GetSelectionParas(editor, &sel_start, &sel_end);
1287 assert(sel_start->type == diParagraph);
1288 assert(sel_end->type == diParagraph);
1289 /* last selection markers aren't always updated, which means
1290 * they can point past the end of the document */
1291 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1292 repaint_start = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1293 repaint_end = editor->pBuffer->pLast->member.para.prev_para;
1294 } else {
1295 /* if the start part of selection is being expanded or contracted... */
1296 if (nStart < editor->nLastSelStart) {
1297 repaint_start = sel_start;
1298 repaint_end = editor->pLastSelStartPara;
1299 } else if (nStart > editor->nLastSelStart) {
1300 repaint_start = editor->pLastSelStartPara;
1301 repaint_end = sel_start;
1304 /* if the end part of selection is being contracted or expanded... */
1305 if (nEnd < editor->nLastSelEnd) {
1306 if (!repaint_start) repaint_start = sel_end;
1307 repaint_end = editor->pLastSelEndPara;
1308 } else if (nEnd > editor->nLastSelEnd) {
1309 if (!repaint_start) repaint_start = editor->pLastSelEndPara;
1310 repaint_end = sel_end;
1314 if (repaint_start)
1315 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1316 /* remember the last invalidated position */
1317 ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1318 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1319 assert(editor->pLastSelStartPara->type == diParagraph);
1320 assert(editor->pLastSelEndPara->type == diParagraph);
1323 BOOL
1324 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1326 /* TODO: Zoom images and objects */
1328 if (numerator == 0 && denominator == 0)
1330 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1331 return TRUE;
1333 if (numerator <= 0 || denominator <= 0)
1334 return FALSE;
1335 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1336 return FALSE;
1338 editor->nZoomNumerator = numerator;
1339 editor->nZoomDenominator = denominator;
1341 ME_RewrapRepaint(editor);
1342 return TRUE;