richedit: Avoid scanning all text for paragraphs to invalidate.
[wine/multimedia.git] / dlls / riched20 / paint.c
blob0a2e1a9f55ad738ad4a5f119de9ae63e588c776c
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 editor->nSequence++;
45 ME_InitContext(&c, editor, hDC);
46 SetBkMode(hDC, TRANSPARENT);
47 ME_MoveCaret(editor);
48 item = editor->pBuffer->pFirst->next;
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(item != editor->pBuffer->pLast)
55 assert(item->type == diParagraph);
57 ys = c.pt.y + item->member.para.pt.y;
58 if (item->member.para.pCell
59 != item->member.para.next_para->member.para.pCell)
61 ME_Cell *cell = NULL;
62 cell = &ME_FindItemBack(item->member.para.next_para, diCell)->member.cell;
63 ye = c.pt.y + cell->pt.y + cell->nHeight;
64 } else {
65 ye = ys + item->member.para.nHeight;
67 if (item->member.para.pCell && !(item->member.para.nFlags & MEPF_ROWEND) &&
68 item->member.para.pCell != item->member.para.prev_para->member.para.pCell)
70 /* the border shifts the text down */
71 ys -= item->member.para.pCell->member.cell.yTextOffset;
74 /* Draw the paragraph if any of the paragraph is in the update region. */
75 if (ys < rcUpdate->bottom && ye > rcUpdate->top)
76 ME_DrawParagraph(&c, item);
77 item = item->member.para.next_para;
79 if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
81 /* Fill space after the end of the text. */
82 RECT rc;
83 rc.top = c.pt.y + editor->nTotalLength;
84 rc.left = c.rcView.left;
85 rc.bottom = c.rcView.bottom;
86 rc.right = c.rcView.right;
88 IntersectRect(&rc, &rc, rcUpdate);
90 if (!IsRectEmpty(&rc))
91 FillRect(hDC, &rc, c.editor->hbrBackground);
93 if (editor->nTotalLength != editor->nLastTotalLength ||
94 editor->nTotalWidth != editor->nLastTotalWidth)
95 ME_SendRequestResize(editor, FALSE);
96 editor->nLastTotalLength = editor->nTotalLength;
97 editor->nLastTotalWidth = editor->nTotalWidth;
99 SelectClipRgn(hDC, oldRgn);
100 if (oldRgn)
101 DeleteObject(oldRgn);
103 c.hDC = NULL;
104 ME_DestroyContext(&c);
107 void ME_Repaint(ME_TextEditor *editor)
109 if (ME_WrapMarkedParagraphs(editor))
111 ME_UpdateScrollBar(editor);
112 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
114 ITextHost_TxViewChange(editor->texthost, TRUE);
117 void ME_UpdateRepaint(ME_TextEditor *editor, BOOL update_now)
119 /* Should be called whenever the contents of the control have changed */
120 BOOL wrappedParagraphs;
122 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
123 if (wrappedParagraphs)
124 ME_UpdateScrollBar(editor);
126 /* Ensure that the cursor is visible */
127 ME_EnsureVisible(editor, &editor->pCursors[0]);
129 ITextHost_TxViewChange(editor->texthost, update_now);
131 ME_SendSelChange(editor);
133 /* send EN_CHANGE if the event mask asks for it */
134 if(editor->nEventMask & ENM_CHANGE)
136 editor->nEventMask &= ~ENM_CHANGE;
137 ME_SendOldNotify(editor, EN_CHANGE);
138 editor->nEventMask |= ENM_CHANGE;
142 void
143 ME_RewrapRepaint(ME_TextEditor *editor)
145 /* RewrapRepaint should be called whenever the control has changed in
146 * looks, but not content. Like resizing. */
148 ME_MarkAllForWrapping(editor);
149 ME_WrapMarkedParagraphs(editor);
150 ME_UpdateScrollBar(editor);
151 ME_Repaint(editor);
154 int ME_twips2pointsX(const ME_Context *c, int x)
156 if (c->editor->nZoomNumerator == 0)
157 return x * c->dpi.cx / 1440;
158 else
159 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
162 int ME_twips2pointsY(const ME_Context *c, int y)
164 if (c->editor->nZoomNumerator == 0)
165 return y * c->dpi.cy / 1440;
166 else
167 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
170 static void ME_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
171 int nChars, ME_Style *s, int width,
172 int nSelFrom, int nSelTo, int ymin, int cy)
174 HDC hDC = c->hDC;
175 HGDIOBJ hOldFont = NULL;
176 SIZE sz;
177 int selWidth;
178 /* Only highlight if there is a selection in the run and when
179 * EM_HIDESELECTION is not being used to hide the selection. */
180 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
181 || c->editor->bHideSelection)
182 return;
183 hOldFont = ME_SelectStyleFont(c, s);
184 if (width <= 0)
186 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
187 width = sz.cx;
189 if (nSelFrom < 0) nSelFrom = 0;
190 if (nSelTo > nChars) nSelTo = nChars;
191 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
192 x += sz.cx;
193 if (nSelTo != nChars)
195 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
196 selWidth = sz.cx;
197 } else {
198 selWidth = width - sz.cx;
200 ME_UnselectStyleFont(c, s, hOldFont);
202 if (c->editor->bEmulateVersion10)
203 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
204 else
206 RECT rect;
207 HBRUSH hBrush;
208 rect.left = x;
209 rect.top = ymin;
210 rect.right = x + selWidth;
211 rect.bottom = ymin + cy;
212 hBrush = CreateSolidBrush(ITextHost_TxGetSysColor(c->editor->texthost,
213 COLOR_HIGHLIGHT));
214 FillRect(hDC, &rect, hBrush);
215 DeleteObject(hBrush);
219 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
220 int nChars, ME_Style *s, int width,
221 int nSelFrom, int nSelTo, int ymin, int cy)
223 HDC hDC = c->hDC;
224 HGDIOBJ hOldFont;
225 COLORREF rgbOld;
226 int yOffset = 0, yTwipsOffset = 0;
227 SIZE sz;
228 COLORREF rgb;
229 HPEN hPen = NULL, hOldPen = NULL;
230 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
231 && nSelFrom < nSelTo && !c->editor->bHideSelection);
232 int xSelStart = x, xSelEnd = x;
233 int *lpDx = NULL;
234 /* lpDx is only needed for tabs to make sure the underline done automatically
235 * by the font extends to the end of the tab. Tabs are always stored as
236 * a single character run, so we can handle this case separately, since
237 * otherwise lpDx would need to specify the lengths of each character. */
238 if (width && nChars == 1)
239 lpDx = &width; /* Make sure underline for tab extends across tab space */
241 hOldFont = ME_SelectStyleFont(c, s);
242 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
243 yTwipsOffset = s->fmt.yOffset;
245 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
246 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
247 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
249 if (yTwipsOffset)
250 yOffset = ME_twips2pointsY(c, yTwipsOffset);
252 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
253 rgb = RGB(0,0,255);
254 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
255 rgb = ITextHost_TxGetSysColor(c->editor->texthost, COLOR_WINDOWTEXT);
256 else
257 rgb = s->fmt.crTextColor;
259 /* Determine the area that is selected in the run. */
260 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
261 /* Treat width as an optional parameter. We can get the width from the
262 * text extent of the string if it isn't specified. */
263 if (!width) width = sz.cx;
264 if (bHighlightedText)
266 if (nSelFrom <= 0)
268 nSelFrom = 0;
270 else
272 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
273 xSelStart = x + sz.cx;
275 if (nSelTo >= nChars)
277 nSelTo = nChars;
278 xSelEnd = x + width;
280 else
282 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
283 xSelEnd = xSelStart + sz.cx;
287 /* Choose the pen type for underlining the text. */
288 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
290 switch (s->fmt.bUnderlineType)
292 case CFU_UNDERLINE:
293 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
294 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
295 hPen = CreatePen(PS_SOLID, 1, rgb);
296 break;
297 case CFU_UNDERLINEDOTTED:
298 hPen = CreatePen(PS_DOT, 1, rgb);
299 break;
300 default:
301 FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
302 /* fall through */
303 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
304 case CFU_UNDERLINENONE:
305 hPen = NULL;
306 break;
308 if (hPen)
310 hOldPen = SelectObject(hDC, hPen);
314 rgbOld = SetTextColor(hDC, rgb);
315 if (bHighlightedText && !c->editor->bEmulateVersion10)
317 COLORREF rgbBackOld;
318 RECT dim;
319 /* FIXME: should use textmetrics info for Descent info */
320 if (hPen)
321 MoveToEx(hDC, x, y - yOffset + 1, NULL);
322 if (xSelStart > x)
324 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
325 if (hPen)
326 LineTo(hDC, xSelStart, y - yOffset + 1);
328 dim.top = ymin;
329 dim.bottom = ymin + cy;
330 dim.left = xSelStart;
331 dim.right = xSelEnd;
332 SetTextColor(hDC, ITextHost_TxGetSysColor(c->editor->texthost,
333 COLOR_HIGHLIGHTTEXT));
334 rgbBackOld = SetBkColor(hDC, ITextHost_TxGetSysColor(c->editor->texthost,
335 COLOR_HIGHLIGHT));
336 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
337 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
338 if (hPen)
339 LineTo(hDC, xSelEnd, y - yOffset + 1);
340 SetBkColor(hDC, rgbBackOld);
341 if (xSelEnd < x + width)
343 SetTextColor(hDC, rgb);
344 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
345 nChars-nSelTo, NULL);
346 if (hPen)
347 LineTo(hDC, x + width, y - yOffset + 1);
350 else
352 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
354 /* FIXME: should use textmetrics info for Descent info */
355 if (hPen)
357 MoveToEx(hDC, x, y - yOffset + 1, NULL);
358 LineTo(hDC, x + width, y - yOffset + 1);
361 if (bHighlightedText) /* v1.0 inverts the selection */
363 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
367 if (hPen)
369 SelectObject(hDC, hOldPen);
370 DeleteObject(hPen);
372 SetTextColor(hDC, rgbOld);
373 ME_UnselectStyleFont(c, s, hOldFont);
376 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
377 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
378 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
379 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
380 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
381 SelectObject(hDC, hFont);
382 SetTextAlign(hDC, align);
383 SetTextColor(hDC, color);
386 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
388 ME_Run *run = &rundi->member.run;
389 ME_DisplayItem *start;
390 int runofs = run->nCharOfs+para->nCharOfs;
391 int nSelFrom, nSelTo;
392 const WCHAR wszSpace[] = {' ', 0};
394 if (run->nFlags & MERF_HIDDEN)
395 return;
397 start = ME_FindItemBack(rundi, diStartRow);
398 ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
400 /* Draw selected end-of-paragraph mark */
401 if (run->nFlags & MERF_ENDPARA)
403 if (runofs >= nSelFrom && runofs < nSelTo)
405 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
406 c->pt.y + para->pt.y + start->member.row.pt.y,
407 start->member.row.nHeight);
409 return;
412 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
414 /* wszSpace is used instead of the tab character because otherwise
415 * an unwanted symbol can be inserted instead. */
416 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
417 nSelFrom-runofs, nSelTo-runofs,
418 c->pt.y + para->pt.y + start->member.row.pt.y,
419 start->member.row.nHeight);
420 return;
423 if (run->nFlags & MERF_GRAPHICS)
424 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
425 else
427 if (c->editor->cPasswordMask)
429 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask, run->strText->nLen);
430 ME_DrawTextWithStyle(c, x, y,
431 szMasked->szData, szMasked->nLen, run->style, run->nWidth,
432 nSelFrom-runofs,nSelTo-runofs,
433 c->pt.y + para->pt.y + start->member.row.pt.y,
434 start->member.row.nHeight);
435 ME_DestroyString(szMasked);
437 else
438 ME_DrawTextWithStyle(c, x, y,
439 run->strText->szData, run->strText->nLen, run->style, run->nWidth,
440 nSelFrom-runofs,nSelTo-runofs,
441 c->pt.y + para->pt.y + start->member.row.pt.y,
442 start->member.row.nHeight);
446 /* The documented widths are in points (72 dpi), but converting them to
447 * 96 dpi (standard display resolution) avoids dealing with fractions. */
448 static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
449 /* none */ {0, PS_SOLID, FALSE},
450 /* 3/4 */ {1, PS_SOLID, FALSE},
451 /* 1 1/2 */ {2, PS_SOLID, FALSE},
452 /* 2 1/4 */ {3, PS_SOLID, FALSE},
453 /* 3 */ {4, PS_SOLID, FALSE},
454 /* 4 1/2 */ {6, PS_SOLID, FALSE},
455 /* 6 */ {8, PS_SOLID, FALSE},
456 /* 3/4 double */ {1, PS_SOLID, TRUE},
457 /* 1 1/2 double */ {2, PS_SOLID, TRUE},
458 /* 2 1/4 double */ {3, PS_SOLID, TRUE},
459 /* 3/4 gray */ {1, PS_DOT /* FIXME */, FALSE},
460 /* 1 1/2 dashed */ {2, PS_DASH, FALSE},
463 static const COLORREF pen_colors[16] = {
464 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
465 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
466 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
467 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
468 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
469 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
470 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
471 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
474 static int ME_GetBorderPenWidth(const ME_Context* c, int idx)
476 int width = border_details[idx].width;
478 if (c->dpi.cx != 96)
479 width = MulDiv(width, c->dpi.cx, 96);
481 if (c->editor->nZoomNumerator != 0)
482 width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
484 return width;
487 int ME_GetParaBorderWidth(const ME_Context* c, int flags)
489 int idx = (flags >> 8) & 0xF;
490 int width;
492 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
494 FIXME("Unsupported border value %d\n", idx);
495 return 0;
497 width = ME_GetBorderPenWidth(c, idx);
498 if (border_details[idx].dble) width = width * 2 + 1;
499 return width;
502 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
504 int idx, border_width, top_border, bottom_border;
505 RECT rc;
506 BOOL hasParaBorder;
508 SetRectEmpty(bounds);
509 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
511 border_width = top_border = bottom_border = 0;
512 idx = (para->pFmt->wBorders >> 8) & 0xF;
513 hasParaBorder = (!(c->editor->bEmulateVersion10 &&
514 para->pFmt->dwMask & PFM_TABLE &&
515 para->pFmt->wEffects & PFE_TABLE) &&
516 (para->pFmt->dwMask & PFM_BORDER) &&
517 idx != 0 &&
518 (para->pFmt->wBorders & 0xF));
519 if (hasParaBorder)
521 /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
522 * controls. It actually stores the paragraph or row border style. Although
523 * the value isn't used for drawing, it is used for streaming out rich text.
525 * wBorders stores the border style for each side (top, left, bottom, right)
526 * using nibble (4 bits) to store each border style. The rich text format
527 * control words, and their associated value are the following:
528 * \brdrdash 0
529 * \brdrdashsm 1
530 * \brdrdb 2
531 * \brdrdot 3
532 * \brdrhair 4
533 * \brdrs 5
534 * \brdrth 6
535 * \brdrtriple 7
537 * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
538 * The mask corresponding to each side for the version are the following:
539 * mask v1.0-3.0 v4.1
540 * 0x000F top left
541 * 0x00F0 left top
542 * 0x0F00 bottom right
543 * 0xF000 right bottom
545 if (para->pFmt->wBorders & 0x00B0)
546 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
547 border_width = ME_GetParaBorderWidth(c, para->pFmt->wBorders);
548 if (para->pFmt->wBorders & 4) top_border = border_width;
549 if (para->pFmt->wBorders & 8) bottom_border = border_width;
552 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
554 rc.left = c->rcView.left;
555 rc.right = c->rcView.right;
556 rc.top = y;
557 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
558 rc.bottom = y + bounds->top + top_border;
559 FillRect(c->hDC, &rc, c->editor->hbrBackground);
562 if (para->pFmt->dwMask & PFM_SPACEAFTER)
564 rc.left = c->rcView.left;
565 rc.right = c->rcView.right;
566 rc.bottom = y + para->nHeight;
567 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
568 rc.top = rc.bottom - bounds->bottom - bottom_border;
569 FillRect(c->hDC, &rc, c->editor->hbrBackground);
572 /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
573 * but might support it in later versions. */
574 if (hasParaBorder) {
575 int pen_width, rightEdge;
576 COLORREF pencr;
577 HPEN pen = NULL, oldpen = NULL;
578 POINT pt;
580 if (para->pFmt->wBorders & 64) /* autocolor */
581 pencr = ITextHost_TxGetSysColor(c->editor->texthost,
582 COLOR_WINDOWTEXT);
583 else
584 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
586 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
587 c->editor->nTotalWidth);
589 pen_width = ME_GetBorderPenWidth(c, idx);
590 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
591 oldpen = SelectObject(c->hDC, pen);
592 MoveToEx(c->hDC, 0, 0, &pt);
594 /* before & after spaces are not included in border */
596 /* helper to draw the double lines in case of corner */
597 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
599 if (para->pFmt->wBorders & 1)
601 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
602 LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
603 if (border_details[idx].dble) {
604 rc.left = c->pt.x + 1;
605 rc.right = rc.left + border_width;
606 rc.top = y + bounds->top;
607 rc.bottom = y + para->nHeight - bounds->bottom;
608 FillRect(c->hDC, &rc, c->editor->hbrBackground);
609 MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
610 LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
612 bounds->left += border_width;
614 if (para->pFmt->wBorders & 2)
616 MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
617 LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
618 if (border_details[idx].dble) {
619 rc.left = rightEdge - pen_width - 1;
620 rc.right = rc.left + pen_width;
621 rc.top = y + bounds->top;
622 rc.bottom = y + para->nHeight - bounds->bottom;
623 FillRect(c->hDC, &rc, c->editor->hbrBackground);
624 MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
625 LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
627 bounds->right += border_width;
629 if (para->pFmt->wBorders & 4)
631 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
632 LineTo(c->hDC, rightEdge, y + bounds->top);
633 if (border_details[idx].dble) {
634 MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
635 LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
637 bounds->top += border_width;
639 if (para->pFmt->wBorders & 8)
641 MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
642 LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
643 if (border_details[idx].dble) {
644 MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
645 LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
647 bounds->bottom += border_width;
649 #undef DD
651 MoveToEx(c->hDC, pt.x, pt.y, NULL);
652 SelectObject(c->hDC, oldpen);
653 DeleteObject(pen);
657 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
659 ME_Paragraph *para = &paragraph->member.para;
660 if (!c->editor->bEmulateVersion10) /* v4.1 */
662 if (para->pCell)
664 RECT rc;
665 ME_Cell *cell = &para->pCell->member.cell;
666 ME_DisplayItem *paraAfterRow;
667 HPEN pen, oldPen;
668 LOGBRUSH logBrush;
669 HBRUSH brush;
670 COLORREF color;
671 POINT oldPt;
672 int width;
673 BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
674 BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
675 int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
676 int bottom = (atBottom ?
677 c->pt.y + cell->pt.y + cell->nHeight :
678 top + para->nHeight + (atTop ? cell->yTextOffset : 0));
679 rc.left = c->pt.x + cell->pt.x;
680 rc.right = rc.left + cell->nWidth;
681 if (atTop) {
682 /* Erase gap before text if not all borders are the same height. */
683 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
684 rc.top = top + width;
685 width = cell->yTextOffset - width;
686 rc.bottom = rc.top + width;
687 if (width) {
688 FillRect(c->hDC, &rc, c->editor->hbrBackground);
691 /* Draw cell borders.
692 * The order borders are draw in is left, top, bottom, right in order
693 * to be consistent with native richedit. This is noticeable from the
694 * overlap of borders of different colours. */
695 if (!(para->nFlags & MEPF_ROWEND)) {
696 rc.top = top;
697 rc.bottom = bottom;
698 if (cell->border.left.width > 0)
700 color = cell->border.left.colorRef;
701 width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
702 } else {
703 color = RGB(192,192,192);
704 width = 1;
706 logBrush.lbStyle = BS_SOLID;
707 logBrush.lbColor = color;
708 logBrush.lbHatch = 0;
709 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
710 width, &logBrush, 0, NULL);
711 oldPen = SelectObject(c->hDC, pen);
712 MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
713 LineTo(c->hDC, rc.left, rc.bottom);
714 SelectObject(c->hDC, oldPen);
715 DeleteObject(pen);
716 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
719 if (atTop) {
720 if (cell->border.top.width > 0)
722 brush = CreateSolidBrush(cell->border.top.colorRef);
723 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
724 } else {
725 brush = GetStockObject(LTGRAY_BRUSH);
726 width = 1;
728 rc.top = top;
729 rc.bottom = rc.top + width;
730 FillRect(c->hDC, &rc, brush);
731 if (cell->border.top.width > 0)
732 DeleteObject(brush);
735 /* Draw the bottom border if at the last paragraph in the cell, and when
736 * in the last row of the table. */
737 if (atBottom) {
738 int oldLeft = rc.left;
739 width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
740 paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
741 if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
742 ME_DisplayItem *nextEndCell;
743 nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
744 assert(nextEndCell && !nextEndCell->member.cell.next_cell);
745 rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
746 /* FIXME: Native draws FROM the bottom of the table rather than
747 * TO the bottom of the table in this case, but just doing so here
748 * will cause the next row to erase the border. */
750 rc.top = bottom;
751 rc.bottom = rc.top + width;
754 if (rc.left < rc.right) {
755 if (cell->border.bottom.width > 0) {
756 brush = CreateSolidBrush(cell->border.bottom.colorRef);
757 } else {
758 brush = GetStockObject(LTGRAY_BRUSH);
760 rc.bottom = bottom;
761 rc.top = rc.bottom - width;
762 FillRect(c->hDC, &rc, brush);
763 if (cell->border.bottom.width > 0)
764 DeleteObject(brush);
766 rc.left = oldLeft;
769 /* Right border only drawn if at the end of the table row. */
770 if (!cell->next_cell->member.cell.next_cell &&
771 !(para->nFlags & MEPF_ROWSTART))
773 rc.top = top;
774 rc.bottom = bottom;
775 if (cell->border.right.width > 0) {
776 color = cell->border.right.colorRef;
777 width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
778 } else {
779 color = RGB(192,192,192);
780 width = 1;
782 logBrush.lbStyle = BS_SOLID;
783 logBrush.lbColor = color;
784 logBrush.lbHatch = 0;
785 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
786 width, &logBrush, 0, NULL);
787 oldPen = SelectObject(c->hDC, pen);
788 MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
789 LineTo(c->hDC, rc.right - 1, rc.bottom);
790 SelectObject(c->hDC, oldPen);
791 DeleteObject(pen);
792 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
795 } else { /* v1.0 - 3.0 */
796 /* Draw simple table border */
797 if (para->pFmt->dwMask & PFM_TABLE && para->pFmt->wEffects & PFE_TABLE) {
798 HPEN pen = NULL, oldpen = NULL;
799 int i, firstX, startX, endX, rowY, rowBottom, nHeight;
800 POINT oldPt;
801 PARAFORMAT2 *pNextFmt;
803 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
804 oldpen = SelectObject(c->hDC, pen);
806 /* Find the start relative to the text */
807 firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
808 /* Go back by the horizontal gap, which is stored in dxOffset */
809 firstX -= ME_twips2pointsX(c, para->pFmt->dxOffset);
810 /* The left edge, stored in dxStartIndent affected just the first edge */
811 startX = firstX - ME_twips2pointsX(c, para->pFmt->dxStartIndent);
812 rowY = c->pt.y + para->pt.y;
813 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
814 rowY += ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
815 nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
816 rowBottom = rowY + nHeight;
818 /* Draw horizontal lines */
819 MoveToEx(c->hDC, firstX, rowY, &oldPt);
820 i = para->pFmt->cTabCount - 1;
821 endX = startX + ME_twips2pointsX(c, para->pFmt->rgxTabs[i] & 0x00ffffff) + 1;
822 LineTo(c->hDC, endX, rowY);
823 pNextFmt = para->next_para->member.para.pFmt;
824 /* The bottom of the row only needs to be drawn if the next row is
825 * not a table. */
826 if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
827 para->nRows == 1))
829 /* Decrement rowBottom to draw the bottom line within the row, and
830 * to not draw over this line when drawing the vertical lines. */
831 rowBottom--;
832 MoveToEx(c->hDC, firstX, rowBottom, NULL);
833 LineTo(c->hDC, endX, rowBottom);
836 /* Draw vertical lines */
837 MoveToEx(c->hDC, firstX, rowY, NULL);
838 LineTo(c->hDC, firstX, rowBottom);
839 for (i = 0; i < para->pFmt->cTabCount; i++)
841 int rightBoundary = para->pFmt->rgxTabs[i] & 0x00ffffff;
842 endX = startX + ME_twips2pointsX(c, rightBoundary);
843 MoveToEx(c->hDC, endX, rowY, NULL);
844 LineTo(c->hDC, endX, rowBottom);
847 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
848 SelectObject(c->hDC, oldpen);
849 DeleteObject(pen);
854 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
856 int align = SetTextAlign(c->hDC, TA_BASELINE);
857 ME_DisplayItem *p;
858 ME_Run *run;
859 ME_Paragraph *para = NULL;
860 RECT rc, bounds;
861 int y;
862 int height = 0, baseline = 0, no=0;
863 BOOL visible = FALSE;
865 rc.left = c->pt.x;
866 rc.right = c->rcView.right;
868 assert(paragraph);
869 para = &paragraph->member.para;
870 y = c->pt.y + para->pt.y;
871 if (para->pCell)
873 ME_Cell *cell = &para->pCell->member.cell;
874 rc.left = c->pt.x + cell->pt.x;
875 rc.right = rc.left + cell->nWidth;
877 if (para->nFlags & MEPF_ROWSTART) {
878 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
879 rc.right = c->pt.x + cell->pt.x;
880 } else if (para->nFlags & MEPF_ROWEND) {
881 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
882 rc.left = c->pt.x + cell->pt.x + cell->nWidth;
884 ME_DrawParaDecoration(c, para, y, &bounds);
885 y += bounds.top;
886 if (bounds.left || bounds.right) {
887 rc.left = max(rc.left, c->pt.x + bounds.left);
888 rc.right = min(rc.right, c->pt.x - bounds.right
889 + max(c->editor->sizeWindow.cx,
890 c->editor->nTotalWidth));
893 for (p = paragraph->next; p != para->next_para; p = p->next)
895 switch(p->type) {
896 case diParagraph:
897 assert(FALSE);
898 break;
899 case diStartRow:
900 y += height;
901 rc.top = y;
902 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
903 rc.bottom = y + para->nHeight;
904 } else {
905 rc.bottom = y + p->member.row.nHeight;
907 visible = RectVisible(c->hDC, &rc);
908 if (visible) {
909 FillRect(c->hDC, &rc, c->editor->hbrBackground);
911 if (bounds.right)
913 /* If scrolled to the right past the end of the text, then
914 * there may be space to the right of the paragraph border. */
915 RECT rcAfterBrdr = rc;
916 rcAfterBrdr.left = rc.right + bounds.right;
917 rcAfterBrdr.right = c->rcView.right;
918 if (RectVisible(c->hDC, &rcAfterBrdr))
919 FillRect(c->hDC, &rcAfterBrdr, c->editor->hbrBackground);
921 if (me_debug)
923 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
924 WCHAR buf[128];
925 POINT pt = c->pt;
926 wsprintfW(buf, wszRowDebug, no);
927 pt.y = 12+y;
928 ME_DebugWrite(c->hDC, &pt, buf);
931 height = p->member.row.nHeight;
932 baseline = p->member.row.nBaseline;
933 break;
934 case diRun:
935 assert(para);
936 run = &p->member.run;
937 if (visible && me_debug) {
938 RECT rc;
939 rc.left = c->pt.x + run->pt.x;
940 rc.right = rc.left + run->nWidth;
941 rc.top = c->pt.y + para->pt.y + run->pt.y;
942 rc.bottom = rc.top + height;
943 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
944 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
946 if (visible)
947 ME_DrawRun(c, c->pt.x + run->pt.x,
948 c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
949 if (me_debug)
951 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
952 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
953 WCHAR buf[2560];
954 POINT pt;
955 pt.x = c->pt.x + run->pt.x;
956 pt.y = c->pt.y + para->pt.y + run->pt.y;
957 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
958 ME_DebugWrite(c->hDC, &pt, buf);
960 break;
961 case diCell:
962 /* Clear any space at the bottom of the cell after the text. */
963 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
964 break;
965 y += height;
966 rc.top = c->pt.y + para->pt.y + para->nHeight;
967 rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
968 if (RectVisible(c->hDC, &rc))
970 FillRect(c->hDC, &rc, c->editor->hbrBackground);
972 break;
973 default:
974 break;
976 no++;
979 ME_DrawTableBorders(c, paragraph);
981 SetTextAlign(c->hDC, align);
984 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
986 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
987 int scrollX = 0, scrollY = 0;
989 if (editor->horz_si.nPos != x) {
990 x = min(x, editor->horz_si.nMax);
991 x = max(x, editor->horz_si.nMin);
992 scrollX = editor->horz_si.nPos - x;
993 editor->horz_si.nPos = x;
994 if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
995 x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
996 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
999 if (editor->vert_si.nPos != y) {
1000 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1001 y = max(y, editor->vert_si.nMin);
1002 scrollY = editor->vert_si.nPos - y;
1003 editor->vert_si.nPos = y;
1004 if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1005 y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1006 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1009 if (abs(scrollX) > editor->sizeWindow.cx ||
1010 abs(scrollY) > editor->sizeWindow.cy)
1011 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1012 else
1013 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1014 &editor->rcFormat, &editor->rcFormat,
1015 NULL, NULL, SW_INVALIDATE);
1016 ME_Repaint(editor);
1018 if (editor->hWnd)
1020 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1021 if (editor->styleFlags & WS_HSCROLL)
1023 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1024 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1025 && (editor->styleFlags & WS_HSCROLL))
1026 || (editor->styleFlags & ES_DISABLENOSCROLL);
1027 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1028 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1029 bScrollBarWillBeVisible);
1032 if (editor->styleFlags & WS_VSCROLL)
1034 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1035 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1036 && (editor->styleFlags & WS_VSCROLL)
1037 && (editor->styleFlags & ES_MULTILINE))
1038 || (editor->styleFlags & ES_DISABLENOSCROLL);
1039 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1040 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1041 bScrollBarWillBeVisible);
1044 ME_UpdateScrollBar(editor);
1047 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1049 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1052 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1054 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1057 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1059 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1062 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1064 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1067 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1069 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1072 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1074 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1077 /* Calculates the visibility after a call to SetScrollRange or
1078 * SetScrollInfo with SIF_RANGE. */
1079 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1081 if (si->fMask & SIF_DISABLENOSCROLL)
1082 return TRUE;
1084 /* This must match the check in SetScrollInfo to determine whether
1085 * to show or hide the scrollbars. */
1086 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1089 void ME_UpdateScrollBar(ME_TextEditor *editor)
1091 /* Note that this is the only function that should ever call
1092 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1094 SCROLLINFO si;
1095 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1097 if (ME_WrapMarkedParagraphs(editor))
1098 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1100 si.cbSize = sizeof(si);
1101 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1102 si.nMin = 0;
1103 if (editor->styleFlags & ES_DISABLENOSCROLL)
1104 si.fMask |= SIF_DISABLENOSCROLL;
1106 /* Update horizontal scrollbar */
1107 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1108 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1109 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1111 ME_HScrollAbs(editor, 0);
1112 /* ME_HScrollAbs will call this function,
1113 * so nothing else needs to be done here. */
1114 return;
1117 si.nMax = editor->nTotalWidth;
1118 si.nPos = editor->horz_si.nPos;
1119 si.nPage = editor->sizeWindow.cx;
1121 if (si.nMax != editor->horz_si.nMax ||
1122 si.nPage != editor->horz_si.nPage)
1124 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1125 editor->horz_si.nMax = si.nMax;
1126 editor->horz_si.nPage = si.nPage;
1127 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1128 editor->styleFlags & WS_HSCROLL)
1130 if (si.nMax > 0xFFFF)
1132 /* Native scales the scrollbar info to 16-bit external values. */
1133 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1134 si.nMax = 0xFFFF;
1136 if (editor->hWnd) {
1137 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1138 } else {
1139 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1140 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1142 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1143 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1147 if (editor->styleFlags & WS_HSCROLL)
1149 if (si.fMask & SIF_DISABLENOSCROLL) {
1150 bScrollBarWillBeVisible = TRUE;
1151 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1152 bScrollBarWillBeVisible = FALSE;
1155 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1156 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1159 /* Update vertical scrollbar */
1160 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1161 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1162 (editor->styleFlags & ES_MULTILINE);
1164 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1166 ME_VScrollAbs(editor, 0);
1167 /* ME_VScrollAbs will call this function,
1168 * so nothing else needs to be done here. */
1169 return;
1172 si.nMax = editor->nTotalLength;
1173 si.nPos = editor->vert_si.nPos;
1174 si.nPage = editor->sizeWindow.cy;
1176 if (si.nMax != editor->vert_si.nMax ||
1177 si.nPage != editor->vert_si.nPage)
1179 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1180 editor->vert_si.nMax = si.nMax;
1181 editor->vert_si.nPage = si.nPage;
1182 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1183 editor->styleFlags & WS_VSCROLL)
1185 if (si.nMax > 0xFFFF)
1187 /* Native scales the scrollbar info to 16-bit external values. */
1188 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1189 si.nMax = 0xFFFF;
1191 if (editor->hWnd) {
1192 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1193 } else {
1194 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1195 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1197 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1198 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1202 if (editor->styleFlags & WS_VSCROLL)
1204 if (si.fMask & SIF_DISABLENOSCROLL) {
1205 bScrollBarWillBeVisible = TRUE;
1206 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1207 bScrollBarWillBeVisible = FALSE;
1210 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1211 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1212 bScrollBarWillBeVisible);
1216 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1218 ME_Run *pRun = &pCursor->pRun->member.run;
1219 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1220 ME_DisplayItem *pPara = pCursor->pPara;
1221 int x, y, yheight;
1223 assert(pRow);
1224 assert(pPara);
1226 if (editor->styleFlags & ES_AUTOHSCROLL)
1228 x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset);
1229 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1230 x = x + 1 - editor->sizeWindow.cx;
1231 else if (x > editor->horz_si.nPos)
1232 x = editor->horz_si.nPos;
1234 if (~editor->styleFlags & ES_AUTOVSCROLL)
1236 ME_HScrollAbs(editor, x);
1237 return;
1239 } else {
1240 if (~editor->styleFlags & ES_AUTOVSCROLL)
1241 return;
1242 x = editor->horz_si.nPos;
1245 y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1246 yheight = pRow->member.row.nHeight;
1248 if (y < editor->vert_si.nPos)
1249 ME_ScrollAbs(editor, x, y);
1250 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1251 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1252 else if (x != editor->horz_si.nPos)
1253 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1257 void
1258 ME_InvalidateSelection(ME_TextEditor *editor)
1260 ME_DisplayItem *sel_start, *sel_end;
1261 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
1262 int nStart, nEnd;
1263 int len = ME_GetTextLength(editor);
1265 ME_GetSelectionOfs(editor, &nStart, &nEnd);
1266 /* if both old and new selection are 0-char (= caret only), then
1267 there's no (inverted) area to be repainted, neither old nor new */
1268 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1269 return;
1270 ME_WrapMarkedParagraphs(editor);
1271 ME_GetSelectionParas(editor, &sel_start, &sel_end);
1272 assert(sel_start->type == diParagraph);
1273 assert(sel_end->type == diParagraph);
1274 /* last selection markers aren't always updated, which means
1275 * they can point past the end of the document */
1276 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1277 repaint_start = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1278 repaint_end = editor->pBuffer->pLast;
1279 ME_MarkForPainting(editor, repaint_start, repaint_end);
1280 } else {
1281 /* if the start part of selection is being expanded or contracted... */
1282 if (nStart < editor->nLastSelStart) {
1283 repaint_start = sel_start;
1284 repaint_end = editor->pLastSelStartPara->member.para.next_para;
1285 } else if (nStart > editor->nLastSelStart) {
1286 repaint_start = editor->pLastSelStartPara;
1287 repaint_end = sel_start->member.para.next_para;
1289 ME_MarkForPainting(editor, repaint_start, repaint_end);
1291 /* if the end part of selection is being contracted or expanded... */
1292 if (nEnd < editor->nLastSelEnd) {
1293 if (!repaint_start) repaint_start = sel_end;
1294 repaint_end = editor->pLastSelEndPara->member.para.next_para;
1295 ME_MarkForPainting(editor, sel_end, repaint_end);
1296 } else if (nEnd > editor->nLastSelEnd) {
1297 if (!repaint_start) repaint_start = editor->pLastSelEndPara;
1298 repaint_end = sel_end->member.para.next_para;
1299 ME_MarkForPainting(editor, editor->pLastSelEndPara, repaint_end);
1303 ME_InvalidateMarkedParagraphs(editor, repaint_start, repaint_end);
1304 /* remember the last invalidated position */
1305 ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1306 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1307 assert(editor->pLastSelStartPara->type == diParagraph);
1308 assert(editor->pLastSelEndPara->type == diParagraph);
1311 BOOL
1312 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1314 /* TODO: Zoom images and objects */
1316 if (numerator == 0 && denominator == 0)
1318 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1319 return TRUE;
1321 if (numerator <= 0 || denominator <= 0)
1322 return FALSE;
1323 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1324 return FALSE;
1326 editor->nZoomNumerator = numerator;
1327 editor->nZoomDenominator = denominator;
1329 ME_RewrapRepaint(editor);
1330 return TRUE;