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
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, const RECT *rcUpdate) {
32 yoffset = ME_GetYScrollPos(editor);
33 ME_InitContext(&c, editor, hDC);
34 SetBkMode(hDC, TRANSPARENT);
36 item = editor->pBuffer->pFirst->next;
38 while(item != editor->pBuffer->pLast) {
40 assert(item->type == diParagraph);
41 ye = c.pt.y + item->member.para.nHeight;
42 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
44 BOOL bPaint = (rcUpdate == NULL);
46 bPaint = c.pt.y<rcUpdate->bottom && ye>rcUpdate->top;
49 ME_DrawParagraph(&c, item);
50 if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
51 item->member.para.nFlags &= ~MEPF_REPAINT;
55 item = item->member.para.next_para;
57 if (c.pt.y<c.rcView.bottom) {
59 int xs = c.rcView.left, xe = c.rcView.right;
60 int ys = c.pt.y, ye = c.rcView.bottom;
64 int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
71 if (rcUpdate && ys!=ye)
73 xs = rcUpdate->left, xe = rcUpdate->right;
74 if (rcUpdate->top > ys)
76 if (rcUpdate->bottom < ye)
77 ye = rcUpdate->bottom;
85 FillRect(hDC, &rc, c.editor->hbrBackground);
88 if (editor->nTotalLength != editor->nLastTotalLength)
89 ME_SendRequestResize(editor, FALSE);
90 editor->nLastTotalLength = editor->nTotalLength;
91 ME_DestroyContext(&c, NULL);
94 void ME_Repaint(ME_TextEditor *editor)
96 if (ME_WrapMarkedParagraphs(editor))
98 ME_UpdateScrollBar(editor);
99 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
101 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
102 ME_SendOldNotify(editor, EN_UPDATE);
103 UpdateWindow(editor->hWnd);
106 void ME_UpdateRepaint(ME_TextEditor *editor)
108 /* Should be called whenever the contents of the control have changed */
110 BOOL wrappedParagraphs;
112 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
113 if (!editor->bRedraw) return;
114 if (wrappedParagraphs)
115 ME_UpdateScrollBar(editor);
117 /* Ensure that the cursor is visible */
118 pCursor = &editor->pCursors[0];
119 ME_EnsureVisible(editor, pCursor->pRun);
121 /* send EN_CHANGE if the event mask asks for it */
122 if(editor->nEventMask & ENM_CHANGE)
124 editor->nEventMask &= ~ENM_CHANGE;
125 ME_SendOldNotify(editor, EN_CHANGE);
126 editor->nEventMask |= ENM_CHANGE;
129 ME_SendSelChange(editor);
133 ME_RewrapRepaint(ME_TextEditor *editor)
135 /* RewrapRepaint should be called whenever the control has changed in
136 * looks, but not content. Like resizing. */
138 ME_MarkAllForWrapping(editor);
141 ME_WrapMarkedParagraphs(editor);
142 ME_UpdateScrollBar(editor);
147 int ME_twips2pointsX(ME_Context *c, int x)
149 if (c->editor->nZoomNumerator == 0)
150 return x * c->dpi.cx / 1440;
152 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
155 int ME_twips2pointsY(ME_Context *c, int y)
157 if (c->editor->nZoomNumerator == 0)
158 return y * c->dpi.cy / 1440;
160 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
163 static void ME_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
164 int nChars, ME_Style *s, int width,
165 int nSelFrom, int nSelTo, int ymin, int cy)
168 HGDIOBJ hOldFont = NULL;
171 /* Only highlight if there is a selection in the run and when
172 * EM_HIDESELECTION is not being used to hide the selection. */
173 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
174 || c->editor->bHideSelection)
176 hOldFont = ME_SelectStyleFont(c, s);
179 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
182 if (nSelFrom < 0) nSelFrom = 0;
183 if (nSelTo > nChars) nSelTo = nChars;
184 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
186 if (nSelTo != nChars)
188 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
191 selWidth = width - sz.cx;
193 ME_UnselectStyleFont(c, s, hOldFont);
195 if (c->editor->bEmulateVersion10)
196 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
203 rect.right = x + selWidth;
204 rect.bottom = ymin + cy;
205 hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
206 FillRect(hDC, &rect, hBrush);
207 DeleteObject(hBrush);
211 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
212 int nChars, ME_Style *s, int width,
213 int nSelFrom, int nSelTo, int ymin, int cy)
218 int yOffset = 0, yTwipsOffset = 0;
221 HPEN hPen = NULL, hOldPen = NULL;
222 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
223 && nSelFrom < nSelTo && !c->editor->bHideSelection);
224 int xSelStart = x, xSelEnd = x;
226 /* lpDx is only needed for tabs to make sure the underline done automatically
227 * by the font extends to the end of the tab. Tabs are always stored as
228 * a single character run, so we can handle this case separately, since
229 * otherwise lpDx would need to specify the lengths of each character. */
230 if (width && nChars == 1)
231 lpDx = &width; /* Make sure underline for tab extends across tab space */
233 hOldFont = ME_SelectStyleFont(c, s);
234 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
235 yTwipsOffset = s->fmt.yOffset;
237 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
238 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
239 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
242 yOffset = ME_twips2pointsY(c, yTwipsOffset);
244 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
246 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
247 rgb = GetSysColor(COLOR_WINDOWTEXT);
249 rgb = s->fmt.crTextColor;
251 /* Determine the area that is selected in the run. */
252 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
253 /* Treat width as an optional parameter. We can get the width from the
254 * text extent of the string if it isn't specified. */
255 if (!width) width = sz.cx;
256 if (bHighlightedText)
264 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
265 xSelStart = x + sz.cx;
267 if (nSelTo >= nChars)
274 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
275 xSelEnd = xSelStart + sz.cx;
279 /* Choose the pen type for underlining the text. */
280 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
282 switch (s->fmt.bUnderlineType)
285 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
286 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
287 hPen = CreatePen(PS_SOLID, 1, rgb);
289 case CFU_UNDERLINEDOTTED:
290 hPen = CreatePen(PS_DOT, 1, rgb);
293 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
295 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
296 case CFU_UNDERLINENONE:
302 hOldPen = SelectObject(hDC, hPen);
306 rgbOld = SetTextColor(hDC, rgb);
307 if (bHighlightedText && !c->editor->bEmulateVersion10)
311 /* FIXME: should use textmetrics info for Descent info */
313 MoveToEx(hDC, x, y - yOffset + 1, NULL);
316 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
318 LineTo(hDC, xSelStart, y - yOffset + 1);
321 dim.bottom = ymin + cy;
322 dim.left = xSelStart;
324 SetTextColor(hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
325 rgbBackOld = SetBkColor(hDC, GetSysColor(COLOR_HIGHLIGHT));
326 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
327 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
329 LineTo(hDC, xSelEnd, y - yOffset + 1);
330 SetBkColor(hDC, rgbBackOld);
331 if (xSelEnd < x + width)
333 SetTextColor(hDC, rgb);
334 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
335 nChars-nSelTo, NULL);
337 LineTo(hDC, x + width, y - yOffset + 1);
342 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
344 /* FIXME: should use textmetrics info for Descent info */
347 MoveToEx(hDC, x, y - yOffset + 1, NULL);
348 LineTo(hDC, x + width, y - yOffset + 1);
351 if (bHighlightedText) /* v1.0 inverts the selection */
353 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
359 SelectObject(hDC, hOldPen);
362 SetTextColor(hDC, rgbOld);
363 ME_UnselectStyleFont(c, s, hOldFont);
366 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
367 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
368 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
369 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
370 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
371 SelectObject(hDC, hFont);
372 SetTextAlign(hDC, align);
373 SetTextColor(hDC, color);
376 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
378 ME_Run *run = &rundi->member.run;
379 ME_DisplayItem *start;
380 int runofs = run->nCharOfs+para->nCharOfs;
381 int nSelFrom, nSelTo;
382 const WCHAR wszSpace[] = {' ', 0};
384 if (run->nFlags & MERF_HIDDEN)
387 start = ME_FindItemBack(rundi, diStartRow);
388 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
390 /* Draw selected end-of-paragraph mark */
391 /* you can always comment it out if you need visible paragraph marks */
392 if (run->nFlags & MERF_ENDPARA)
394 if (runofs >= nSelFrom && runofs < nSelTo)
396 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
397 c->pt.y + start->member.row.nYPos,
398 start->member.row.nHeight);
403 if (run->nFlags & (MERF_TAB | MERF_CELL))
405 /* wszSpace is used instead of the tab character because otherwise
406 * an unwanted symbol can be inserted instead. */
407 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
408 nSelFrom-runofs,nSelTo-runofs,
409 c->pt.y + start->member.row.nYPos,
410 start->member.row.nHeight);
414 if (run->nFlags & MERF_GRAPHICS)
415 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
418 if (c->editor->cPasswordMask)
420 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
421 ME_DrawTextWithStyle(c, x, y,
422 szMasked->szData, ME_StrVLen(szMasked), run->style, run->nWidth,
423 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
424 ME_DestroyString(szMasked);
427 ME_DrawTextWithStyle(c, x, y,
428 run->strText->szData, ME_StrVLen(run->strText), run->style, run->nWidth,
429 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
433 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
434 /* none */ {0, 1, PS_SOLID, FALSE},
435 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
436 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
437 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
438 /* 3 */ {3, 1, PS_SOLID, FALSE},
439 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
440 /* 6 */ {6, 1, PS_SOLID, FALSE},
441 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
442 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
443 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
444 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
445 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
448 static const COLORREF pen_colors[16] = {
449 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
450 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
451 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
452 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
453 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
454 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
455 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
456 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
459 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
463 if (editor->nZoomNumerator == 0)
465 width = border_details[idx].width_num + border_details[idx].width_den / 2;
466 width /= border_details[idx].width_den;
470 width = border_details[idx].width_num * editor->nZoomNumerator;
471 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
472 width /= border_details[idx].width_den * editor->nZoomDenominator;
477 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
479 int idx = (flags >> 8) & 0xF;
482 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
484 FIXME("Unsupported border value %d\n", idx);
487 width = ME_GetBorderPenWidth(editor, idx);
488 if (border_details[idx].dble) width = width * 2 + 1;
492 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
495 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
497 /* FIXME: how to compute simply the line space in ls ??? */
498 /* FIXME: does line spacing include the line itself ??? */
499 switch (para->pFmt->bLineSpacingRule)
501 case 0: sp = ls; break;
502 case 1: sp = (3 * ls) / 2; break;
503 case 2: sp = 2 * ls; break;
504 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
505 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
506 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
507 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
509 if (c->editor->nZoomNumerator == 0)
512 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
515 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
517 int idx, border_width, top_border, bottom_border;
520 SetRectEmpty(bounds);
521 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
523 border_width = top_border = bottom_border = 0;
524 idx = (para->pFmt->wBorders >> 8) & 0xF;
525 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
527 if (para->pFmt->wBorders & 0x00B0)
528 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
529 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
530 if (para->pFmt->wBorders & 4) top_border = border_width;
531 if (para->pFmt->wBorders & 8) bottom_border = border_width;
534 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
536 rc.left = c->rcView.left;
537 rc.right = c->rcView.right;
539 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
540 rc.bottom = y + bounds->top + top_border;
541 FillRect(c->hDC, &rc, c->editor->hbrBackground);
544 if (para->pFmt->dwMask & PFM_SPACEAFTER)
546 rc.left = c->rcView.left;
547 rc.right = c->rcView.right;
548 rc.bottom = y + para->nHeight;
549 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
550 rc.top = rc.bottom - bounds->bottom - bottom_border;
551 FillRect(c->hDC, &rc, c->editor->hbrBackground);
554 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
557 HPEN pen = NULL, oldpen = NULL;
560 if (para->pFmt->wBorders & 64) /* autocolor */
561 pencr = GetSysColor(COLOR_WINDOWTEXT);
563 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
565 pen_width = ME_GetBorderPenWidth(c->editor, idx);
566 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
567 oldpen = SelectObject(c->hDC, pen);
568 MoveToEx(c->hDC, 0, 0, &pt);
570 /* before & after spaces are not included in border */
572 /* helper to draw the double lines in case of corner */
573 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
575 if (para->pFmt->wBorders & 1)
577 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
578 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
579 if (border_details[idx].dble) {
580 rc.left = c->rcView.left + 1;
581 rc.right = rc.left + border_width;
582 rc.top = y + bounds->top;
583 rc.bottom = y + para->nHeight - bounds->bottom;
584 FillRect(c->hDC, &rc, c->editor->hbrBackground);
585 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
586 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
588 bounds->left += border_width;
590 if (para->pFmt->wBorders & 2)
592 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
593 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
594 if (border_details[idx].dble) {
595 rc.left = c->rcView.right - pen_width - 1;
596 rc.right = c->rcView.right - 1;
597 rc.top = y + bounds->top;
598 rc.bottom = y + para->nHeight - bounds->bottom;
599 FillRect(c->hDC, &rc, c->editor->hbrBackground);
600 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
601 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
603 bounds->right += border_width;
605 if (para->pFmt->wBorders & 4)
607 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
608 LineTo(c->hDC, c->rcView.right, y + bounds->top);
609 if (border_details[idx].dble) {
610 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
611 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
613 bounds->top += border_width;
615 if (para->pFmt->wBorders & 8)
617 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
618 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
619 if (border_details[idx].dble) {
620 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
621 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
623 bounds->bottom += border_width;
627 MoveToEx(c->hDC, pt.x, pt.y, NULL);
628 SelectObject(c->hDC, oldpen);
633 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
634 int align = SetTextAlign(c->hDC, TA_BASELINE);
637 ME_Paragraph *para = NULL;
638 RECT rc, rcPara, bounds;
640 int height = 0, baseline = 0, no=0, pno = 0;
642 BOOL visible = FALSE;
644 c->pt.x = c->rcView.left;
645 rcPara.left = c->rcView.left;
646 rcPara.right = c->rcView.right;
647 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
650 para = &p->member.para;
653 xs = c->rcView.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
654 xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
655 ME_DrawParaDecoration(c, para, y, &bounds);
659 /* we should have seen a diParagraph before */
663 rcPara.bottom = y+p->member.row.nHeight;
664 visible = RectVisible(c->hDC, &rcPara);
667 rc.left = c->rcView.left + bounds.left;
670 rc.bottom = y+p->member.row.nHeight;
671 FillRect(c->hDC, &rc, c->editor->hbrBackground);
674 rc.right = c->rcView.right - bounds.right;
675 FillRect(c->hDC, &rc, c->editor->hbrBackground);
678 FillRect(c->hDC, &rc, c->editor->hbrBackground);
682 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
685 wsprintfW(buf, wszRowDebug, no);
687 ME_DebugWrite(c->hDC, &pt, buf);
690 height = p->member.row.nHeight;
691 baseline = p->member.row.nBaseline;
693 xe += ME_twips2pointsX(c, para->pFmt->dxOffset);
697 run = &p->member.run;
698 if (visible && me_debug) {
699 rc.left = c->rcView.left+run->pt.x;
700 rc.right = c->rcView.left+run->pt.x+run->nWidth;
701 rc.top = c->pt.y+run->pt.y;
702 rc.bottom = c->pt.y+run->pt.y+height;
703 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
704 if (run->nFlags & MERF_SKIPPED)
705 DrawFocusRect(c->hDC, &rc);
707 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
710 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
713 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
714 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
718 pt.y = c->pt.y + run->pt.y;
719 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
720 ME_DebugWrite(c->hDC, &pt, buf);
722 /* c->pt.x += p->member.run.nWidth; */
729 SetTextAlign(c->hDC, align);
732 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
734 ME_Scroll(editor, absY, 1);
737 void ME_ScrollUp(ME_TextEditor *editor, int cy)
739 ME_Scroll(editor, cy, 2);
742 void ME_ScrollDown(ME_TextEditor *editor, int cy)
744 ME_Scroll(editor, cy, 3);
747 void ME_Scroll(ME_TextEditor *editor, int value, int type)
750 int nOrigPos, nNewPos, nActualScroll;
752 nOrigPos = ME_GetYScrollPos(editor);
754 si.cbSize = sizeof(SCROLLINFO);
760 /*Scroll absolutely*/
764 /* Scroll up - towards the beginning of the document */
765 si.nPos = nOrigPos - value;
768 /* Scroll down - towards the end of the document */
769 si.nPos = nOrigPos + value;
772 FIXME("ME_Scroll called incorrectly\n");
776 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
777 nActualScroll = nOrigPos - nNewPos;
780 if (abs(nActualScroll) > editor->sizeWindow.cy)
781 InvalidateRect(editor->hWnd, NULL, TRUE);
783 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
787 ME_UpdateScrollBar(editor);
791 void ME_UpdateScrollBar(ME_TextEditor *editor)
793 /* Note that this is the only function that should ever call SetScrolLInfo
794 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
799 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
801 if (ME_WrapMarkedParagraphs(editor))
802 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
805 si.cbSize = sizeof(si);
806 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
807 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
809 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
811 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
812 ME_MarkAllForWrapping(editor);
813 ME_WrapMarkedParagraphs(editor);
816 si.fMask = SIF_PAGE | SIF_RANGE;
817 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
818 si.fMask |= SIF_DISABLENOSCROLL;
821 si.nMax = editor->nTotalLength;
823 si.nPage = editor->sizeWindow.cy;
825 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
826 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
829 int ME_GetYScrollPos(ME_TextEditor *editor)
832 si.cbSize = sizeof(si);
834 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
837 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
838 { /* Returns true if the scrollbar is visible */
840 sbi.cbSize = sizeof(sbi);
841 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
842 return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
845 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
847 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
848 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
849 int y, yrel, yheight, yold;
854 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
855 yheight = pRow->member.row.nHeight;
856 yold = ME_GetYScrollPos(editor);
860 ME_ScrollAbs(editor,y);
861 else if (yrel + yheight > editor->sizeWindow.cy)
862 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
867 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
873 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
874 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
878 rc.bottom = y + height;
879 rc.right = editor->rcFormat.right;
880 InvalidateRect(editor->hWnd, &rc, FALSE);
885 ME_InvalidateSelection(ME_TextEditor *editor)
887 ME_DisplayItem *para1, *para2;
889 int len = ME_GetTextLength(editor);
891 ME_GetSelection(editor, &nStart, &nEnd);
892 /* if both old and new selection are 0-char (= caret only), then
893 there's no (inverted) area to be repainted, neither old nor new */
894 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
896 ME_WrapMarkedParagraphs(editor);
897 ME_GetSelectionParas(editor, ¶1, ¶2);
898 assert(para1->type == diParagraph);
899 assert(para2->type == diParagraph);
900 /* last selection markers aren't always updated, which means
901 they can point past the end of the document */
902 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
903 ME_MarkForPainting(editor,
904 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
905 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
907 /* if the start part of selection is being expanded or contracted... */
908 if (nStart < editor->nLastSelStart) {
909 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
911 if (nStart > editor->nLastSelStart) {
912 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
915 /* if the end part of selection is being contracted or expanded... */
916 if (nEnd < editor->nLastSelEnd) {
917 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
919 if (nEnd > editor->nLastSelEnd) {
920 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
924 ME_InvalidateMarkedParagraphs(editor);
925 /* remember the last invalidated position */
926 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
927 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
928 assert(editor->pLastSelStartPara->type == diParagraph);
929 assert(editor->pLastSelEndPara->type == diParagraph);
933 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
935 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
940 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
942 /* TODO: Zoom images and objects */
946 if (denominator == 0)
948 if (1.0 / 64.0 > (float)numerator / (float)denominator
949 || (float)numerator / (float)denominator > 64.0)
953 editor->nZoomNumerator = numerator;
954 editor->nZoomDenominator = denominator;
956 ME_RewrapRepaint(editor);