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 &&
47 c.pt.y+item->member.para.nHeight>rcUpdate->top;
50 ME_DrawParagraph(&c, item);
51 if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
52 item->member.para.nFlags &= ~MEPF_REPAINT;
56 item = item->member.para.next_para;
58 if (c.pt.y<c.rcView.bottom) {
60 int xs = c.rcView.left, xe = c.rcView.right;
61 int ys = c.pt.y, ye = c.rcView.bottom;
65 int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
72 if (rcUpdate && ys!=ye)
74 xs = rcUpdate->left, xe = rcUpdate->right;
75 if (rcUpdate->top > ys)
77 if (rcUpdate->bottom < ye)
78 ye = rcUpdate->bottom;
86 FillRect(hDC, &rc, c.editor->hbrBackground);
88 if (ys == c.pt.y) /* don't overwrite the top bar */
91 if (editor->nTotalLength != editor->nLastTotalLength)
92 ME_SendRequestResize(editor, FALSE);
93 editor->nLastTotalLength = editor->nTotalLength;
94 ME_DestroyContext(&c);
97 void ME_Repaint(ME_TextEditor *editor)
99 if (ME_WrapMarkedParagraphs(editor))
101 ME_UpdateScrollBar(editor);
102 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
104 ME_SendOldNotify(editor, EN_UPDATE);
105 UpdateWindow(editor->hWnd);
108 void ME_UpdateRepaint(ME_TextEditor *editor)
110 /* Should be called whenever the contents of the control have changed */
113 if (ME_WrapMarkedParagraphs(editor))
114 ME_UpdateScrollBar(editor);
116 /* Ensure that the cursor is visible */
117 pCursor = &editor->pCursors[0];
118 ME_EnsureVisible(editor, pCursor->pRun);
120 /* send EN_CHANGE if the event mask asks for it */
121 if(editor->nEventMask & ENM_CHANGE)
123 editor->nEventMask &= ~ENM_CHANGE;
124 ME_SendOldNotify(editor, EN_CHANGE);
125 editor->nEventMask |= ENM_CHANGE;
128 ME_SendSelChange(editor);
132 ME_RewrapRepaint(ME_TextEditor *editor)
134 /* RewrapRepaint should be called whenever the control has changed in
135 * looks, but not content. Like resizing. */
137 ME_MarkAllForWrapping(editor);
138 ME_WrapMarkedParagraphs(editor);
139 ME_UpdateScrollBar(editor);
145 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
146 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
149 COLORREF rgbOld, rgbBack;
150 int yOffset = 0, yTwipsOffset = 0;
154 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
155 rgbBack = ME_GetBackColor(c->editor);
156 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
158 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
159 rgb = GetSysColor(COLOR_WINDOWTEXT);
161 rgb = s->fmt.crTextColor;
162 rgbOld = SetTextColor(hDC, rgb);
163 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
164 yTwipsOffset = s->fmt.yOffset;
166 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
167 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
168 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
175 if (c->editor->nZoomNumerator)
177 numerator = c->editor->nZoomNumerator;
178 denominator = c->editor->nZoomDenominator;
180 yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
182 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
183 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
184 if (width) *width = sz.cx;
185 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
188 switch (s->fmt.bUnderlineType)
191 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
192 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
193 hPen = CreatePen(PS_SOLID, 1, rgb);
195 case CFU_UNDERLINEDOTTED:
196 hPen = CreatePen(PS_DOT, 1, rgb);
199 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
201 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
202 case CFU_UNDERLINENONE:
208 HPEN hOldPen = SelectObject(hDC, hPen);
209 /* FIXME: should use textmetrics info for Descent info */
210 MoveToEx(hDC, x, y - yOffset + 1, NULL);
211 LineTo(hDC, x + sz.cx, y - yOffset + 1);
212 SelectObject(hDC, hOldPen);
216 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
218 if (nSelFrom < 0) nSelFrom = 0;
219 if (nSelTo > nChars) nSelTo = nChars;
220 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
222 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
224 /* Invert selection if not hidden by EM_HIDESELECTION */
225 if (c->editor->bHideSelection == FALSE)
226 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
228 SetTextColor(hDC, rgbOld);
229 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
232 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
233 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
234 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
235 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
236 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
237 SelectObject(hDC, hFont);
238 SetTextAlign(hDC, align);
239 SetTextColor(hDC, color);
242 static void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
243 ME_Paragraph *para, BOOL selected) {
245 int xs, ys, xe, ye, h, ym, width, eyes;
246 ME_GetGraphicsSize(c->editor, run, &sz);
255 /* draw a smiling face :) */
256 Ellipse(c->hDC, xs, ys, xe, ye);
257 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
258 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
259 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
260 LineTo(c->hDC, xs+width/8, ys+3*h/4);
261 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
262 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
265 /* descent is usually (always?) 0 for graphics */
266 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
270 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
272 ME_Run *run = &rundi->member.run;
273 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
274 int runofs = run->nCharOfs+para->nCharOfs;
275 int nSelFrom, nSelTo;
276 const WCHAR wszSpace[] = {' ', 0};
278 if (run->nFlags & MERF_HIDDEN)
281 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
283 /* Draw selected end-of-paragraph mark */
284 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
285 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
286 c->pt.y + start->member.row.nYPos,
287 start->member.row.nHeight);
289 /* you can always comment it out if you need visible paragraph marks */
290 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
293 if (run->nFlags & MERF_GRAPHICS)
294 ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
297 if (c->editor->cPasswordMask)
299 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
300 ME_DrawTextWithStyle(c, x, y,
301 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
302 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
303 ME_DestroyString(szMasked);
306 ME_DrawTextWithStyle(c, x, y,
307 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
308 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
312 COLORREF ME_GetBackColor(const ME_TextEditor *editor)
314 /* Looks like I was seriously confused
315 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
317 if (editor->rgbBackColor == -1)
318 return GetSysColor(COLOR_WINDOW);
320 return editor->rgbBackColor;
323 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
324 int align = SetTextAlign(c->hDC, TA_BASELINE);
327 ME_Paragraph *para = NULL;
330 int height = 0, baseline = 0, no=0, pno = 0;
335 c->pt.x = c->rcView.left;
336 rcPara.left = c->rcView.left;
337 rcPara.right = c->rcView.right;
338 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
341 para = &p->member.para;
345 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
346 xs = c->rcView.left+nMargWidth;
347 xe = c->rcView.right-para->nRightMargin;
350 rcPara.bottom = y+p->member.row.nHeight;
351 visible = RectVisible(c->hDC, &rcPara);
354 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
356 rc.left = c->rcView.left;
357 rc.right = c->rcView.left+nMargWidth;
359 rc.bottom = y+p->member.row.nHeight;
360 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
363 rc.right = c->rcView.right;
364 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
365 rc.left = c->rcView.left+nMargWidth;
367 FillRect(c->hDC, &rc, hbr);
372 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
375 wsprintfW(buf, wszRowDebug, no);
377 ME_DebugWrite(c->hDC, &pt, buf);
380 height = p->member.row.nHeight;
381 baseline = p->member.row.nBaseline;
386 run = &p->member.run;
387 if (visible && me_debug) {
388 rc.left = c->rcView.left+run->pt.x;
389 rc.right = c->rcView.left+run->pt.x+run->nWidth;
390 rc.top = c->pt.y+run->pt.y;
391 rc.bottom = c->pt.y+run->pt.y+height;
392 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
393 if (run->nFlags & MERF_SKIPPED)
394 DrawFocusRect(c->hDC, &rc);
396 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
399 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
402 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
403 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
407 pt.y = c->pt.y + run->pt.y;
408 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
409 ME_DebugWrite(c->hDC, &pt, buf);
411 /* c->pt.x += p->member.run.nWidth; */
418 SetTextAlign(c->hDC, align);
421 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
423 ME_Scroll(editor, absY, 1);
426 void ME_ScrollUp(ME_TextEditor *editor, int cy)
428 ME_Scroll(editor, cy, 2);
431 void ME_ScrollDown(ME_TextEditor *editor, int cy)
433 ME_Scroll(editor, cy, 3);
436 void ME_Scroll(ME_TextEditor *editor, int value, int type)
439 int nOrigPos, nNewPos, nActualScroll;
441 nOrigPos = ME_GetYScrollPos(editor);
443 si.cbSize = sizeof(SCROLLINFO);
453 /* Scroll up - towards the beginning of the document */
454 si.nPos = nOrigPos - value;
457 /* Scroll down - towards the end of the document */
458 si.nPos = nOrigPos + value;
461 FIXME("ME_Scroll called incorrectly\n");
465 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
466 nActualScroll = nOrigPos - nNewPos;
469 if (abs(nActualScroll) > editor->sizeWindow.cy)
470 InvalidateRect(editor->hWnd, NULL, TRUE);
472 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
476 ME_UpdateScrollBar(editor);
480 void ME_UpdateScrollBar(ME_TextEditor *editor)
482 /* Note that this is the only funciton that should ever call SetScrolLInfo
483 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
488 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
490 if (ME_WrapMarkedParagraphs(editor))
491 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
494 si.cbSize = sizeof(si);
495 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
496 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
498 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
500 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
501 ME_MarkAllForWrapping(editor);
502 ME_WrapMarkedParagraphs(editor);
505 si.fMask = SIF_PAGE | SIF_RANGE;
506 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
507 si.fMask |= SIF_DISABLENOSCROLL;
510 si.nMax = editor->nTotalLength;
512 si.nPage = editor->sizeWindow.cy;
514 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
515 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
518 int ME_GetYScrollPos(ME_TextEditor *editor)
521 si.cbSize = sizeof(si);
523 GetScrollInfo(editor->hWnd, SB_VERT, &si);
527 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
528 { /* Returns true if the scrollbar is visible */
530 sbi.cbSize = sizeof(sbi);
531 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
532 return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
535 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
537 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
538 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
539 int y, yrel, yheight, yold;
544 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
545 yheight = pRow->member.row.nHeight;
546 yold = ME_GetYScrollPos(editor);
550 ME_ScrollAbs(editor,y);
551 else if (yrel + yheight > editor->sizeWindow.cy)
552 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
557 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
563 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
564 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
568 rc.bottom = y + height;
569 rc.right = editor->rcFormat.right;
570 InvalidateRect(editor->hWnd, &rc, FALSE);
575 ME_InvalidateSelection(ME_TextEditor *editor)
577 ME_DisplayItem *para1, *para2;
579 int len = ME_GetTextLength(editor);
581 ME_GetSelection(editor, &nStart, &nEnd);
582 /* if both old and new selection are 0-char (= caret only), then
583 there's no (inverted) area to be repainted, neither old nor new */
584 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
586 ME_WrapMarkedParagraphs(editor);
587 ME_GetSelectionParas(editor, ¶1, ¶2);
588 assert(para1->type == diParagraph);
589 assert(para2->type == diParagraph);
590 /* last selection markers aren't always updated, which means
591 they can point past the end of the document */
592 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
593 ME_MarkForPainting(editor,
594 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
595 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
597 /* if the start part of selection is being expanded or contracted... */
598 if (nStart < editor->nLastSelStart) {
599 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
601 if (nStart > editor->nLastSelStart) {
602 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
605 /* if the end part of selection is being contracted or expanded... */
606 if (nEnd < editor->nLastSelEnd) {
607 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
609 if (nEnd > editor->nLastSelEnd) {
610 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
614 ME_InvalidateMarkedParagraphs(editor);
615 /* remember the last invalidated position */
616 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
617 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
618 assert(editor->pLastSelStartPara->type == diParagraph);
619 assert(editor->pLastSelEndPara->type == diParagraph);
623 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
625 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
630 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
632 /* TODO: Zoom images and objects */
636 if (denominator == 0)
638 if (1.0 / 64.0 > (float)numerator / (float)denominator
639 || (float)numerator / (float)denominator > 64.0)
643 editor->nZoomNumerator = numerator;
644 editor->nZoomDenominator = denominator;
646 ME_RewrapRepaint(editor);