riched20: Embed PARAFORMAT2 in the paragraph struct rather than its ptr.
[wine.git] / dlls / riched20 / paint.c
blob8e5177255e0235c33a20de7169026ca84aa20c69
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 COLORREF get_back_color( ME_Context *c, ME_Style *style, BOOL highlight )
206 COLORREF color;
208 if (highlight)
209 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );
210 else if ( (style->fmt.dwMask & CFM_BACKCOLOR)
211 && !(style->fmt.dwEffects & CFE_AUTOBACKCOLOR) )
212 color = style->fmt.crBackColor;
213 else
214 color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_WINDOW );
216 return color;
219 static void get_underline_pen( ME_Style *style, COLORREF color, HPEN *pen )
221 *pen = NULL;
222 /* Choose the pen type for underlining the text. */
223 if (style->fmt.dwMask & CFM_UNDERLINETYPE)
225 switch (style->fmt.bUnderlineType)
227 case CFU_UNDERLINE:
228 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
229 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
230 *pen = CreatePen( PS_SOLID, 1, color );
231 break;
232 case CFU_UNDERLINEDOTTED:
233 *pen = CreatePen( PS_DOT, 1, color );
234 break;
235 default:
236 FIXME( "Unknown underline type (%u)\n", style->fmt.bUnderlineType );
237 /* fall through */
238 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
239 case CFU_UNDERLINENONE:
240 break;
243 return;
246 static void draw_underline( ME_Context *c, ME_Run *run, int x, int y, COLORREF color )
248 HPEN pen;
250 get_underline_pen( run->style, color, &pen );
251 if (pen)
253 HPEN old_pen = SelectObject( c->hDC, pen );
254 MoveToEx( c->hDC, x, y + 1, NULL );
255 LineTo( c->hDC, x + run->nWidth, y + 1 );
256 SelectObject( c->hDC, old_pen );
257 DeleteObject( pen );
259 return;
262 /*********************************************************************
263 * draw_space
265 * Draw the end-of-paragraph or tab space.
267 * If actually_draw is TRUE then ensure any underline is drawn.
269 static void draw_space( ME_Context *c, ME_Run *run, int x, int y,
270 BOOL selected, BOOL actually_draw, int ymin, int cy )
272 HDC hdc = c->hDC;
273 BOOL old_style_selected = FALSE;
274 RECT rect;
275 COLORREF back_color = 0;
277 SetRect( &rect, x, ymin, x + run->nWidth, ymin + cy );
279 if (c->editor->bHideSelection || (!c->editor->bHaveFocus &&
280 !(c->editor->styleFlags & ES_NOHIDESEL))) selected = FALSE;
281 if (c->editor->bEmulateVersion10)
283 old_style_selected = selected;
284 selected = FALSE;
287 if (selected)
288 back_color = ITextHost_TxGetSysColor( c->editor->texthost, COLOR_HIGHLIGHT );
290 if (actually_draw)
292 COLORREF text_color = get_text_color( c, run->style, selected );
293 COLORREF old_text, old_back;
294 HFONT old_font = NULL;
295 int y_offset = calc_y_offset( c, run->style );
296 static const WCHAR space[1] = {' '};
298 old_font = ME_SelectStyleFont( c, run->style );
299 old_text = SetTextColor( hdc, text_color );
300 if (selected) old_back = SetBkColor( hdc, back_color );
302 ExtTextOutW( hdc, x, y - y_offset, selected ? ETO_OPAQUE : 0, &rect, space, 1, &run->nWidth );
304 if (selected) SetBkColor( hdc, old_back );
305 SetTextColor( hdc, old_text );
306 ME_UnselectStyleFont( c, run->style, old_font );
308 draw_underline( c, run, x, y - y_offset, text_color );
310 else if (selected)
312 HBRUSH brush = CreateSolidBrush( back_color );
313 FillRect( hdc, &rect, brush );
314 DeleteObject( brush );
317 if (old_style_selected)
318 PatBlt( hdc, x, ymin, run->nWidth, cy, DSTINVERT );
321 static void get_selection_rect( ME_Context *c, ME_Run *run, int from, int to, int cy, RECT *r )
323 from = max( 0, from );
324 to = min( run->len, to );
325 r->left = ME_PointFromCharContext( c, run, from, TRUE );
326 r->top = 0;
327 r->right = ME_PointFromCharContext( c, run, to, TRUE );
328 r->bottom = cy;
329 return;
332 static void draw_text( ME_Context *c, ME_Run *run, int x, int y, BOOL selected, RECT *sel_rect )
334 COLORREF text_color = get_text_color( c, run->style, selected );
335 COLORREF back_color = get_back_color( c, run->style, selected );
336 COLORREF old_text, old_back = 0;
337 const WCHAR *text = get_text( run, 0 );
338 ME_String *masked = NULL;
339 const BOOL paint_bg = ( selected
340 || ( ( run->style->fmt.dwMask & CFM_BACKCOLOR )
341 && !(CFE_AUTOBACKCOLOR & run->style->fmt.dwEffects) )
344 if (c->editor->cPasswordMask)
346 masked = ME_MakeStringR( c->editor->cPasswordMask, run->len );
347 text = masked->szData;
350 old_text = SetTextColor( c->hDC, text_color );
351 if (paint_bg) old_back = SetBkColor( c->hDC, back_color );
353 if (run->para->nFlags & MEPF_COMPLEX)
354 ScriptTextOut( c->hDC, &run->style->script_cache, x, y, paint_bg ? ETO_OPAQUE : 0, sel_rect,
355 &run->script_analysis, NULL, 0, run->glyphs, run->num_glyphs, run->advances,
356 NULL, run->offsets );
357 else
358 ExtTextOutW( c->hDC, x, y, paint_bg ? ETO_OPAQUE : 0, sel_rect, text, run->len, NULL );
360 if (paint_bg) SetBkColor( c->hDC, old_back );
361 SetTextColor( c->hDC, old_text );
363 draw_underline( c, run, x, y, text_color );
365 ME_DestroyString( masked );
366 return;
370 static void ME_DrawTextWithStyle(ME_Context *c, ME_Run *run, int x, int y,
371 int nSelFrom, int nSelTo, int ymin, int cy)
373 HDC hDC = c->hDC;
374 HGDIOBJ hOldFont;
375 int yOffset = 0;
376 BOOL selected = (nSelFrom < run->len && nSelTo >= 0
377 && nSelFrom < nSelTo && !c->editor->bHideSelection &&
378 (c->editor->bHaveFocus || (c->editor->styleFlags & ES_NOHIDESEL)));
379 BOOL old_style_selected = FALSE;
380 RECT sel_rect;
381 HRGN clip = NULL, sel_rgn = NULL;
383 yOffset = calc_y_offset( c, run->style );
385 if (selected)
387 get_selection_rect( c, run, nSelFrom, nSelTo, cy, &sel_rect );
388 OffsetRect( &sel_rect, x, ymin );
390 if (c->editor->bEmulateVersion10)
392 old_style_selected = TRUE;
393 selected = FALSE;
395 else
397 sel_rgn = CreateRectRgnIndirect( &sel_rect );
398 clip = CreateRectRgn( 0, 0, 0, 0 );
399 if (GetClipRgn( hDC, clip ) != 1)
401 DeleteObject( clip );
402 clip = NULL;
407 hOldFont = ME_SelectStyleFont( c, run->style );
409 if (sel_rgn) ExtSelectClipRgn( hDC, sel_rgn, RGN_DIFF );
411 if (!(run->style->fmt.dwEffects & CFE_AUTOBACKCOLOR)
412 && (run->style->fmt.dwMask & CFM_BACKCOLOR) )
414 RECT tmp_rect;
415 get_selection_rect( c, run, 0, run->len, cy, &tmp_rect );
416 OffsetRect( &tmp_rect, x, ymin );
417 draw_text( c, run, x, y - yOffset, FALSE, &tmp_rect );
419 else
420 draw_text( c, run, x, y - yOffset, FALSE, NULL );
422 if (sel_rgn)
424 ExtSelectClipRgn( hDC, clip, RGN_COPY );
425 ExtSelectClipRgn( hDC, sel_rgn, RGN_AND );
426 draw_text( c, run, x, y - yOffset, TRUE, &sel_rect );
427 ExtSelectClipRgn( hDC, clip, RGN_COPY );
428 if (clip) DeleteObject( clip );
429 DeleteObject( sel_rgn );
432 if (old_style_selected)
433 PatBlt( hDC, sel_rect.left, ymin, sel_rect.right - sel_rect.left, cy, DSTINVERT );
435 ME_UnselectStyleFont(c, run->style, hOldFont);
438 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
439 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
440 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
441 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
442 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
443 SelectObject(hDC, hFont);
444 SetTextAlign(hDC, align);
445 SetTextColor(hDC, color);
448 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
450 ME_Run *run = &rundi->member.run;
451 ME_DisplayItem *start;
452 int runofs = run->nCharOfs+para->nCharOfs;
453 int nSelFrom, nSelTo;
455 if (run->nFlags & MERF_HIDDEN)
456 return;
458 start = ME_FindItemBack(rundi, diStartRow);
459 ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
461 /* Draw selected end-of-paragraph mark */
462 if (run->nFlags & MERF_ENDPARA)
464 if (runofs >= nSelFrom && runofs < nSelTo)
466 draw_space( c, run, x, y, TRUE, FALSE,
467 c->pt.y + para->pt.y + start->member.row.pt.y,
468 start->member.row.nHeight );
470 return;
473 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
475 BOOL selected = runofs >= nSelFrom && runofs < nSelTo;
477 draw_space( c, run, x, y, selected, TRUE,
478 c->pt.y + para->pt.y + start->member.row.pt.y,
479 start->member.row.nHeight );
480 return;
483 if (run->nFlags & MERF_GRAPHICS)
484 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
485 else
487 ME_DrawTextWithStyle(c, run, x, y, nSelFrom - runofs, nSelTo - runofs,
488 c->pt.y + para->pt.y + start->member.row.pt.y,
489 start->member.row.nHeight);
493 /* The documented widths are in points (72 dpi), but converting them to
494 * 96 dpi (standard display resolution) avoids dealing with fractions. */
495 static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
496 /* none */ {0, PS_SOLID, FALSE},
497 /* 3/4 */ {1, PS_SOLID, FALSE},
498 /* 1 1/2 */ {2, PS_SOLID, FALSE},
499 /* 2 1/4 */ {3, PS_SOLID, FALSE},
500 /* 3 */ {4, PS_SOLID, FALSE},
501 /* 4 1/2 */ {6, PS_SOLID, FALSE},
502 /* 6 */ {8, PS_SOLID, FALSE},
503 /* 3/4 double */ {1, PS_SOLID, TRUE},
504 /* 1 1/2 double */ {2, PS_SOLID, TRUE},
505 /* 2 1/4 double */ {3, PS_SOLID, TRUE},
506 /* 3/4 gray */ {1, PS_DOT /* FIXME */, FALSE},
507 /* 1 1/2 dashed */ {2, PS_DASH, FALSE},
510 static const COLORREF pen_colors[16] = {
511 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
512 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
513 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
514 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
515 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
516 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
517 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
518 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
521 static int ME_GetBorderPenWidth(const ME_Context* c, int idx)
523 int width = border_details[idx].width;
525 if (c->dpi.cx != 96)
526 width = MulDiv(width, c->dpi.cx, 96);
528 if (c->editor->nZoomNumerator != 0)
529 width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
531 return width;
534 int ME_GetParaBorderWidth(const ME_Context* c, int flags)
536 int idx = (flags >> 8) & 0xF;
537 int width;
539 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
541 FIXME("Unsupported border value %d\n", idx);
542 return 0;
544 width = ME_GetBorderPenWidth(c, idx);
545 if (border_details[idx].dble) width = width * 2 + 1;
546 return width;
549 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
551 int idx, border_width, top_border, bottom_border;
552 RECT rc;
553 BOOL hasParaBorder;
555 SetRectEmpty(bounds);
556 if (!(para->fmt.dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
558 border_width = top_border = bottom_border = 0;
559 idx = (para->fmt.wBorders >> 8) & 0xF;
560 hasParaBorder = (!(c->editor->bEmulateVersion10 &&
561 para->fmt.dwMask & PFM_TABLE &&
562 para->fmt.wEffects & PFE_TABLE) &&
563 (para->fmt.dwMask & PFM_BORDER) &&
564 idx != 0 &&
565 (para->fmt.wBorders & 0xF));
566 if (hasParaBorder)
568 /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
569 * controls. It actually stores the paragraph or row border style. Although
570 * the value isn't used for drawing, it is used for streaming out rich text.
572 * wBorders stores the border style for each side (top, left, bottom, right)
573 * using nibble (4 bits) to store each border style. The rich text format
574 * control words, and their associated value are the following:
575 * \brdrdash 0
576 * \brdrdashsm 1
577 * \brdrdb 2
578 * \brdrdot 3
579 * \brdrhair 4
580 * \brdrs 5
581 * \brdrth 6
582 * \brdrtriple 7
584 * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
585 * The mask corresponding to each side for the version are the following:
586 * mask v1.0-3.0 v4.1
587 * 0x000F top left
588 * 0x00F0 left top
589 * 0x0F00 bottom right
590 * 0xF000 right bottom
592 if (para->fmt.wBorders & 0x00B0)
593 FIXME("Unsupported border flags %x\n", para->fmt.wBorders);
594 border_width = ME_GetParaBorderWidth(c, para->fmt.wBorders);
595 if (para->fmt.wBorders & 4) top_border = border_width;
596 if (para->fmt.wBorders & 8) bottom_border = border_width;
599 if (para->fmt.dwMask & PFM_SPACEBEFORE)
601 rc.left = c->rcView.left;
602 rc.right = c->rcView.right;
603 rc.top = y;
604 bounds->top = ME_twips2pointsY(c, para->fmt.dySpaceBefore);
605 rc.bottom = y + bounds->top + top_border;
606 FillRect(c->hDC, &rc, c->editor->hbrBackground);
609 if (para->fmt.dwMask & PFM_SPACEAFTER)
611 rc.left = c->rcView.left;
612 rc.right = c->rcView.right;
613 rc.bottom = y + para->nHeight;
614 bounds->bottom = ME_twips2pointsY(c, para->fmt.dySpaceAfter);
615 rc.top = rc.bottom - bounds->bottom - bottom_border;
616 FillRect(c->hDC, &rc, c->editor->hbrBackground);
619 /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
620 * but might support it in later versions. */
621 if (hasParaBorder) {
622 int pen_width, rightEdge;
623 COLORREF pencr;
624 HPEN pen = NULL, oldpen = NULL;
625 POINT pt;
627 if (para->fmt.wBorders & 64) /* autocolor */
628 pencr = ITextHost_TxGetSysColor(c->editor->texthost,
629 COLOR_WINDOWTEXT);
630 else
631 pencr = pen_colors[(para->fmt.wBorders >> 12) & 0xF];
633 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
634 c->editor->nTotalWidth);
636 pen_width = ME_GetBorderPenWidth(c, idx);
637 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
638 oldpen = SelectObject(c->hDC, pen);
639 MoveToEx(c->hDC, 0, 0, &pt);
641 /* before & after spaces are not included in border */
643 /* helper to draw the double lines in case of corner */
644 #define DD(x) ((para->fmt.wBorders & (x)) ? (pen_width + 1) : 0)
646 if (para->fmt.wBorders & 1)
648 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
649 LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
650 if (border_details[idx].dble) {
651 rc.left = c->pt.x + 1;
652 rc.right = rc.left + border_width;
653 rc.top = y + bounds->top;
654 rc.bottom = y + para->nHeight - bounds->bottom;
655 FillRect(c->hDC, &rc, c->editor->hbrBackground);
656 MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
657 LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
659 bounds->left += border_width;
661 if (para->fmt.wBorders & 2)
663 MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
664 LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
665 if (border_details[idx].dble) {
666 rc.left = rightEdge - pen_width - 1;
667 rc.right = rc.left + pen_width;
668 rc.top = y + bounds->top;
669 rc.bottom = y + para->nHeight - bounds->bottom;
670 FillRect(c->hDC, &rc, c->editor->hbrBackground);
671 MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
672 LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
674 bounds->right += border_width;
676 if (para->fmt.wBorders & 4)
678 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
679 LineTo(c->hDC, rightEdge, y + bounds->top);
680 if (border_details[idx].dble) {
681 MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
682 LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
684 bounds->top += border_width;
686 if (para->fmt.wBorders & 8)
688 MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
689 LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
690 if (border_details[idx].dble) {
691 MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
692 LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
694 bounds->bottom += border_width;
696 #undef DD
698 MoveToEx(c->hDC, pt.x, pt.y, NULL);
699 SelectObject(c->hDC, oldpen);
700 DeleteObject(pen);
704 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
706 ME_Paragraph *para = &paragraph->member.para;
707 if (!c->editor->bEmulateVersion10) /* v4.1 */
709 if (para->pCell)
711 RECT rc;
712 ME_Cell *cell = &para->pCell->member.cell;
713 ME_DisplayItem *paraAfterRow;
714 HPEN pen, oldPen;
715 LOGBRUSH logBrush;
716 HBRUSH brush;
717 COLORREF color;
718 POINT oldPt;
719 int width;
720 BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
721 BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
722 int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
723 int bottom = (atBottom ?
724 c->pt.y + cell->pt.y + cell->nHeight :
725 top + para->nHeight + (atTop ? cell->yTextOffset : 0));
726 rc.left = c->pt.x + cell->pt.x;
727 rc.right = rc.left + cell->nWidth;
728 if (atTop) {
729 /* Erase gap before text if not all borders are the same height. */
730 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
731 rc.top = top + width;
732 width = cell->yTextOffset - width;
733 rc.bottom = rc.top + width;
734 if (width) {
735 FillRect(c->hDC, &rc, c->editor->hbrBackground);
738 /* Draw cell borders.
739 * The order borders are draw in is left, top, bottom, right in order
740 * to be consistent with native richedit. This is noticeable from the
741 * overlap of borders of different colours. */
742 if (!(para->nFlags & MEPF_ROWEND)) {
743 rc.top = top;
744 rc.bottom = bottom;
745 if (cell->border.left.width > 0)
747 color = cell->border.left.colorRef;
748 width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
749 } else {
750 color = RGB(192,192,192);
751 width = 1;
753 logBrush.lbStyle = BS_SOLID;
754 logBrush.lbColor = color;
755 logBrush.lbHatch = 0;
756 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
757 width, &logBrush, 0, NULL);
758 oldPen = SelectObject(c->hDC, pen);
759 MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
760 LineTo(c->hDC, rc.left, rc.bottom);
761 SelectObject(c->hDC, oldPen);
762 DeleteObject(pen);
763 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
766 if (atTop) {
767 if (cell->border.top.width > 0)
769 brush = CreateSolidBrush(cell->border.top.colorRef);
770 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
771 } else {
772 brush = GetStockObject(LTGRAY_BRUSH);
773 width = 1;
775 rc.top = top;
776 rc.bottom = rc.top + width;
777 FillRect(c->hDC, &rc, brush);
778 if (cell->border.top.width > 0)
779 DeleteObject(brush);
782 /* Draw the bottom border if at the last paragraph in the cell, and when
783 * in the last row of the table. */
784 if (atBottom) {
785 int oldLeft = rc.left;
786 width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
787 paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
788 if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
789 ME_DisplayItem *nextEndCell;
790 nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
791 assert(nextEndCell && !nextEndCell->member.cell.next_cell);
792 rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
793 /* FIXME: Native draws FROM the bottom of the table rather than
794 * TO the bottom of the table in this case, but just doing so here
795 * will cause the next row to erase the border. */
797 rc.top = bottom;
798 rc.bottom = rc.top + width;
801 if (rc.left < rc.right) {
802 if (cell->border.bottom.width > 0) {
803 brush = CreateSolidBrush(cell->border.bottom.colorRef);
804 } else {
805 brush = GetStockObject(LTGRAY_BRUSH);
807 rc.bottom = bottom;
808 rc.top = rc.bottom - width;
809 FillRect(c->hDC, &rc, brush);
810 if (cell->border.bottom.width > 0)
811 DeleteObject(brush);
813 rc.left = oldLeft;
816 /* Right border only drawn if at the end of the table row. */
817 if (!cell->next_cell->member.cell.next_cell &&
818 !(para->nFlags & MEPF_ROWSTART))
820 rc.top = top;
821 rc.bottom = bottom;
822 if (cell->border.right.width > 0) {
823 color = cell->border.right.colorRef;
824 width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
825 } else {
826 color = RGB(192,192,192);
827 width = 1;
829 logBrush.lbStyle = BS_SOLID;
830 logBrush.lbColor = color;
831 logBrush.lbHatch = 0;
832 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
833 width, &logBrush, 0, NULL);
834 oldPen = SelectObject(c->hDC, pen);
835 MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
836 LineTo(c->hDC, rc.right - 1, rc.bottom);
837 SelectObject(c->hDC, oldPen);
838 DeleteObject(pen);
839 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
842 } else { /* v1.0 - 3.0 */
843 /* Draw simple table border */
844 if (para->fmt.dwMask & PFM_TABLE && para->fmt.wEffects & PFE_TABLE) {
845 HPEN pen = NULL, oldpen = NULL;
846 int i, firstX, startX, endX, rowY, rowBottom, nHeight;
847 POINT oldPt;
848 PARAFORMAT2 *pNextFmt;
850 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
851 oldpen = SelectObject(c->hDC, pen);
853 /* Find the start relative to the text */
854 firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
855 /* Go back by the horizontal gap, which is stored in dxOffset */
856 firstX -= ME_twips2pointsX(c, para->fmt.dxOffset);
857 /* The left edge, stored in dxStartIndent affected just the first edge */
858 startX = firstX - ME_twips2pointsX(c, para->fmt.dxStartIndent);
859 rowY = c->pt.y + para->pt.y;
860 if (para->fmt.dwMask & PFM_SPACEBEFORE)
861 rowY += ME_twips2pointsY(c, para->fmt.dySpaceBefore);
862 nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
863 rowBottom = rowY + nHeight;
865 /* Draw horizontal lines */
866 MoveToEx(c->hDC, firstX, rowY, &oldPt);
867 i = para->fmt.cTabCount - 1;
868 endX = startX + ME_twips2pointsX(c, para->fmt.rgxTabs[i] & 0x00ffffff) + 1;
869 LineTo(c->hDC, endX, rowY);
870 pNextFmt = &para->next_para->member.para.fmt;
871 /* The bottom of the row only needs to be drawn if the next row is
872 * not a table. */
873 if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
874 para->nRows == 1))
876 /* Decrement rowBottom to draw the bottom line within the row, and
877 * to not draw over this line when drawing the vertical lines. */
878 rowBottom--;
879 MoveToEx(c->hDC, firstX, rowBottom, NULL);
880 LineTo(c->hDC, endX, rowBottom);
883 /* Draw vertical lines */
884 MoveToEx(c->hDC, firstX, rowY, NULL);
885 LineTo(c->hDC, firstX, rowBottom);
886 for (i = 0; i < para->fmt.cTabCount; i++)
888 int rightBoundary = para->fmt.rgxTabs[i] & 0x00ffffff;
889 endX = startX + ME_twips2pointsX(c, rightBoundary);
890 MoveToEx(c->hDC, endX, rowY, NULL);
891 LineTo(c->hDC, endX, rowBottom);
894 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
895 SelectObject(c->hDC, oldpen);
896 DeleteObject(pen);
901 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
903 int align = SetTextAlign(c->hDC, TA_BASELINE);
904 ME_DisplayItem *p;
905 ME_Run *run;
906 ME_Paragraph *para = NULL;
907 RECT rc, bounds;
908 int y;
909 int height = 0, baseline = 0, no=0;
910 BOOL visible = FALSE;
912 rc.left = c->pt.x;
913 rc.right = c->rcView.right;
915 assert(paragraph);
916 para = &paragraph->member.para;
917 y = c->pt.y + para->pt.y;
918 if (para->pCell)
920 ME_Cell *cell = &para->pCell->member.cell;
921 rc.left = c->pt.x + cell->pt.x;
922 rc.right = rc.left + cell->nWidth;
924 if (para->nFlags & MEPF_ROWSTART) {
925 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
926 rc.right = c->pt.x + cell->pt.x;
927 } else if (para->nFlags & MEPF_ROWEND) {
928 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
929 rc.left = c->pt.x + cell->pt.x + cell->nWidth;
931 ME_DrawParaDecoration(c, para, y, &bounds);
932 y += bounds.top;
933 if (bounds.left || bounds.right) {
934 rc.left = max(rc.left, c->pt.x + bounds.left);
935 rc.right = min(rc.right, c->pt.x - bounds.right
936 + max(c->editor->sizeWindow.cx,
937 c->editor->nTotalWidth));
940 for (p = paragraph->next; p != para->next_para; p = p->next)
942 switch(p->type) {
943 case diParagraph:
944 assert(FALSE);
945 break;
946 case diStartRow:
947 y += height;
948 rc.top = y;
949 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
950 rc.bottom = y + para->nHeight;
951 } else {
952 rc.bottom = y + p->member.row.nHeight;
954 visible = RectVisible(c->hDC, &rc);
955 if (visible) {
956 FillRect(c->hDC, &rc, c->editor->hbrBackground);
958 if (bounds.right)
960 /* If scrolled to the right past the end of the text, then
961 * there may be space to the right of the paragraph border. */
962 RECT rcAfterBrdr = rc;
963 rcAfterBrdr.left = rc.right + bounds.right;
964 rcAfterBrdr.right = c->rcView.right;
965 if (RectVisible(c->hDC, &rcAfterBrdr))
966 FillRect(c->hDC, &rcAfterBrdr, c->editor->hbrBackground);
968 if (me_debug)
970 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
971 WCHAR buf[128];
972 POINT pt = c->pt;
973 wsprintfW(buf, wszRowDebug, no);
974 pt.y = 12+y;
975 ME_DebugWrite(c->hDC, &pt, buf);
978 height = p->member.row.nHeight;
979 baseline = p->member.row.nBaseline;
980 break;
981 case diRun:
982 assert(para);
983 run = &p->member.run;
984 if (visible && me_debug) {
985 RECT rc;
986 rc.left = c->pt.x + run->pt.x;
987 rc.right = rc.left + run->nWidth;
988 rc.top = c->pt.y + para->pt.y + run->pt.y;
989 rc.bottom = rc.top + height;
990 TRACE("rc = %s\n", wine_dbgstr_rect(&rc));
991 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
993 if (visible)
994 ME_DrawRun(c, c->pt.x + run->pt.x,
995 c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
996 if (me_debug)
998 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
999 WCHAR buf[2560];
1000 POINT pt;
1001 pt.x = c->pt.x + run->pt.x;
1002 pt.y = c->pt.y + para->pt.y + run->pt.y;
1003 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, get_text( &p->member.run, 0 ));
1004 ME_DebugWrite(c->hDC, &pt, buf);
1006 break;
1007 case diCell:
1008 /* Clear any space at the bottom of the cell after the text. */
1009 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
1010 break;
1011 y += height;
1012 rc.top = c->pt.y + para->pt.y + para->nHeight;
1013 rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
1014 if (RectVisible(c->hDC, &rc))
1016 FillRect(c->hDC, &rc, c->editor->hbrBackground);
1018 break;
1019 default:
1020 break;
1022 no++;
1025 ME_DrawTableBorders(c, paragraph);
1027 SetTextAlign(c->hDC, align);
1030 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1032 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1033 int scrollX = 0, scrollY = 0;
1035 if (editor->horz_si.nPos != x) {
1036 x = min(x, editor->horz_si.nMax);
1037 x = max(x, editor->horz_si.nMin);
1038 scrollX = editor->horz_si.nPos - x;
1039 editor->horz_si.nPos = x;
1040 if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
1041 x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
1042 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1045 if (editor->vert_si.nPos != y) {
1046 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1047 y = max(y, editor->vert_si.nMin);
1048 scrollY = editor->vert_si.nPos - y;
1049 editor->vert_si.nPos = y;
1050 if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1051 y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1052 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1055 if (abs(scrollX) > editor->sizeWindow.cx ||
1056 abs(scrollY) > editor->sizeWindow.cy)
1057 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1058 else
1059 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1060 &editor->rcFormat, &editor->rcFormat,
1061 NULL, NULL, SW_INVALIDATE);
1062 ME_Repaint(editor);
1064 if (editor->hWnd)
1066 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1067 if (editor->styleFlags & WS_HSCROLL)
1069 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1070 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1071 && (editor->styleFlags & WS_HSCROLL))
1072 || (editor->styleFlags & ES_DISABLENOSCROLL);
1073 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1074 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1075 bScrollBarWillBeVisible);
1078 if (editor->styleFlags & WS_VSCROLL)
1080 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1081 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1082 && (editor->styleFlags & WS_VSCROLL)
1083 && (editor->styleFlags & ES_MULTILINE))
1084 || (editor->styleFlags & ES_DISABLENOSCROLL);
1085 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1086 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1087 bScrollBarWillBeVisible);
1090 ME_UpdateScrollBar(editor);
1093 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1095 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1098 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1100 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1103 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1105 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1108 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1110 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1113 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1115 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1118 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1120 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1123 /* Calculates the visibility after a call to SetScrollRange or
1124 * SetScrollInfo with SIF_RANGE. */
1125 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1127 if (si->fMask & SIF_DISABLENOSCROLL)
1128 return TRUE;
1130 /* This must match the check in SetScrollInfo to determine whether
1131 * to show or hide the scrollbars. */
1132 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1135 void ME_UpdateScrollBar(ME_TextEditor *editor)
1137 /* Note that this is the only function that should ever call
1138 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1140 SCROLLINFO si;
1141 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1143 if (ME_WrapMarkedParagraphs(editor))
1144 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1146 si.cbSize = sizeof(si);
1147 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1148 si.nMin = 0;
1149 if (editor->styleFlags & ES_DISABLENOSCROLL)
1150 si.fMask |= SIF_DISABLENOSCROLL;
1152 /* Update horizontal scrollbar */
1153 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1154 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1155 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1157 ME_HScrollAbs(editor, 0);
1158 /* ME_HScrollAbs will call this function,
1159 * so nothing else needs to be done here. */
1160 return;
1163 si.nMax = editor->nTotalWidth;
1164 si.nPos = editor->horz_si.nPos;
1165 si.nPage = editor->sizeWindow.cx;
1167 if (si.nMax != editor->horz_si.nMax ||
1168 si.nPage != editor->horz_si.nPage)
1170 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1171 editor->horz_si.nMax = si.nMax;
1172 editor->horz_si.nPage = si.nPage;
1173 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1174 editor->styleFlags & WS_HSCROLL)
1176 if (si.nMax > 0xFFFF)
1178 /* Native scales the scrollbar info to 16-bit external values. */
1179 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1180 si.nMax = 0xFFFF;
1182 if (editor->hWnd) {
1183 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1184 } else {
1185 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1186 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1188 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1189 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1193 if (editor->styleFlags & WS_HSCROLL)
1195 if (si.fMask & SIF_DISABLENOSCROLL) {
1196 bScrollBarWillBeVisible = TRUE;
1197 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1198 bScrollBarWillBeVisible = FALSE;
1201 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1202 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1205 /* Update vertical scrollbar */
1206 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1207 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1208 (editor->styleFlags & ES_MULTILINE);
1210 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1212 ME_VScrollAbs(editor, 0);
1213 /* ME_VScrollAbs will call this function,
1214 * so nothing else needs to be done here. */
1215 return;
1218 si.nMax = editor->nTotalLength;
1219 si.nPos = editor->vert_si.nPos;
1220 si.nPage = editor->sizeWindow.cy;
1222 if (si.nMax != editor->vert_si.nMax ||
1223 si.nPage != editor->vert_si.nPage)
1225 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1226 editor->vert_si.nMax = si.nMax;
1227 editor->vert_si.nPage = si.nPage;
1228 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1229 editor->styleFlags & WS_VSCROLL)
1231 if (si.nMax > 0xFFFF)
1233 /* Native scales the scrollbar info to 16-bit external values. */
1234 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1235 si.nMax = 0xFFFF;
1237 if (editor->hWnd) {
1238 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1239 } else {
1240 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1241 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1243 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1244 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1248 if (editor->styleFlags & WS_VSCROLL)
1250 if (si.fMask & SIF_DISABLENOSCROLL) {
1251 bScrollBarWillBeVisible = TRUE;
1252 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1253 bScrollBarWillBeVisible = FALSE;
1256 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1257 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1258 bScrollBarWillBeVisible);
1262 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1264 ME_Run *pRun = &pCursor->pRun->member.run;
1265 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1266 ME_DisplayItem *pPara = pCursor->pPara;
1267 int x, y, yheight;
1269 assert(pRow);
1270 assert(pPara);
1272 if (editor->styleFlags & ES_AUTOHSCROLL)
1274 x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset, TRUE);
1275 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1276 x = x + 1 - editor->sizeWindow.cx;
1277 else if (x > editor->horz_si.nPos)
1278 x = editor->horz_si.nPos;
1280 if (~editor->styleFlags & ES_AUTOVSCROLL)
1282 ME_HScrollAbs(editor, x);
1283 return;
1285 } else {
1286 if (~editor->styleFlags & ES_AUTOVSCROLL)
1287 return;
1288 x = editor->horz_si.nPos;
1291 y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1292 yheight = pRow->member.row.nHeight;
1294 if (y < editor->vert_si.nPos)
1295 ME_ScrollAbs(editor, x, y);
1296 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1297 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1298 else if (x != editor->horz_si.nPos)
1299 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1303 void
1304 ME_InvalidateSelection(ME_TextEditor *editor)
1306 ME_DisplayItem *sel_start, *sel_end;
1307 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
1308 int nStart, nEnd;
1309 int len = ME_GetTextLength(editor);
1311 ME_GetSelectionOfs(editor, &nStart, &nEnd);
1312 /* if both old and new selection are 0-char (= caret only), then
1313 there's no (inverted) area to be repainted, neither old nor new */
1314 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1315 return;
1316 ME_WrapMarkedParagraphs(editor);
1317 ME_GetSelectionParas(editor, &sel_start, &sel_end);
1318 assert(sel_start->type == diParagraph);
1319 assert(sel_end->type == diParagraph);
1320 /* last selection markers aren't always updated, which means
1321 * they can point past the end of the document */
1322 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1323 repaint_start = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1324 repaint_end = editor->pBuffer->pLast->member.para.prev_para;
1325 } else {
1326 /* if the start part of selection is being expanded or contracted... */
1327 if (nStart < editor->nLastSelStart) {
1328 repaint_start = sel_start;
1329 repaint_end = editor->pLastSelStartPara;
1330 } else if (nStart > editor->nLastSelStart) {
1331 repaint_start = editor->pLastSelStartPara;
1332 repaint_end = sel_start;
1335 /* if the end part of selection is being contracted or expanded... */
1336 if (nEnd < editor->nLastSelEnd) {
1337 if (!repaint_start) repaint_start = sel_end;
1338 repaint_end = editor->pLastSelEndPara;
1339 } else if (nEnd > editor->nLastSelEnd) {
1340 if (!repaint_start) repaint_start = editor->pLastSelEndPara;
1341 repaint_end = sel_end;
1345 if (repaint_start)
1346 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1347 /* remember the last invalidated position */
1348 ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1349 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1350 assert(editor->pLastSelStartPara->type == diParagraph);
1351 assert(editor->pLastSelEndPara->type == diParagraph);
1354 BOOL
1355 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1357 /* TODO: Zoom images and objects */
1359 if (numerator == 0 && denominator == 0)
1361 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1362 return TRUE;
1364 if (numerator <= 0 || denominator <= 0)
1365 return FALSE;
1366 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1367 return FALSE;
1369 editor->nZoomNumerator = numerator;
1370 editor->nZoomDenominator = denominator;
1372 ME_RewrapRepaint(editor);
1373 return TRUE;