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, 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 ME_Cursor *pCursor = &editor->pCursors[0];
101 if (ME_WrapMarkedParagraphs(editor)) {
102 ME_UpdateScrollBar(editor);
106 ME_EnsureVisible(editor, pCursor->pRun);
107 UpdateWindow(editor->hWnd);
111 void ME_UpdateRepaint(ME_TextEditor *editor)
114 InvalidateRect(editor->hWnd, NULL, TRUE);
116 ME_SendOldNotify(editor, EN_CHANGE);
118 ME_SendOldNotify(editor, EN_UPDATE);
119 ME_SendSelChange(editor);
124 ME_RewrapRepaint(ME_TextEditor *editor)
126 ME_MarkAllForWrapping(editor);
127 ME_WrapMarkedParagraphs(editor);
128 ME_UpdateScrollBar(editor);
133 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
134 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
137 COLORREF rgbOld, rgbBack;
138 int yOffset = 0, yTwipsOffset = 0;
139 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
140 rgbBack = ME_GetBackColor(c->editor);
141 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
142 rgbOld = SetTextColor(hDC, RGB(0,0,255));
143 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
144 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
146 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
147 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
148 yTwipsOffset = s->fmt.yOffset;
150 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
151 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
152 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
159 if (c->editor->nZoomNumerator)
161 numerator = c->editor->nZoomNumerator;
162 denominator = c->editor->nZoomDenominator;
164 yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
166 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
169 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
172 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
175 if (nSelFrom < 0) nSelFrom = 0;
176 if (nSelTo > nChars) nSelTo = nChars;
177 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
179 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
181 /* Invert selection if not hidden by EM_HIDESELECTION */
182 if (c->editor->bHideSelection == FALSE)
183 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
185 SetTextColor(hDC, rgbOld);
186 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
189 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
190 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
191 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
192 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
193 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
194 SelectObject(hDC, hFont);
195 SetTextAlign(hDC, align);
196 SetTextColor(hDC, color);
199 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
200 ME_Paragraph *para, BOOL selected) {
202 int xs, ys, xe, ye, h, ym, width, eyes;
203 ME_GetGraphicsSize(c->editor, run, &sz);
212 /* draw a smiling face :) */
213 Ellipse(c->hDC, xs, ys, xe, ye);
214 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
215 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
216 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
217 LineTo(c->hDC, xs+width/8, ys+3*h/4);
218 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
219 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
222 /* descent is usually (always?) 0 for graphics */
223 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
227 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
229 ME_Run *run = &rundi->member.run;
230 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
231 int runofs = run->nCharOfs+para->nCharOfs;
232 int nSelFrom, nSelTo;
233 const WCHAR wszSpace[] = {' ', 0};
235 if (run->nFlags & MERF_HIDDEN)
238 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
240 /* Draw selected end-of-paragraph mark */
241 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
242 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
243 c->pt.y + start->member.row.nYPos,
244 start->member.row.nHeight);
246 /* you can always comment it out if you need visible paragraph marks */
247 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
250 if (run->nFlags & MERF_GRAPHICS)
251 ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
254 if (c->editor->cPasswordMask)
256 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
257 ME_DrawTextWithStyle(c, x, y,
258 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
259 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
260 ME_DestroyString(szMasked);
263 ME_DrawTextWithStyle(c, x, y,
264 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
265 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
269 COLORREF ME_GetBackColor(ME_TextEditor *editor)
271 /* Looks like I was seriously confused
272 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
274 if (editor->rgbBackColor == -1)
275 return GetSysColor(COLOR_WINDOW);
277 return editor->rgbBackColor;
280 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
281 int align = SetTextAlign(c->hDC, TA_BASELINE);
284 ME_Paragraph *para = NULL;
287 int height = 0, baseline = 0, no=0, pno = 0;
292 c->pt.x = c->rcView.left;
293 rcPara.left = c->rcView.left;
294 rcPara.right = c->rcView.right;
295 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
298 para = &p->member.para;
302 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
303 xs = c->rcView.left+nMargWidth;
304 xe = c->rcView.right-para->nRightMargin;
307 rcPara.bottom = y+p->member.row.nHeight;
308 visible = RectVisible(c->hDC, &rcPara);
311 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
313 rc.left = c->rcView.left;
314 rc.right = c->rcView.left+nMargWidth;
316 rc.bottom = y+p->member.row.nHeight;
317 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
320 rc.right = c->rcView.right;
321 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
322 rc.left = c->rcView.left+nMargWidth;
324 FillRect(c->hDC, &rc, hbr);
329 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
332 wsprintfW(buf, wszRowDebug, no);
334 ME_DebugWrite(c->hDC, &pt, buf);
337 height = p->member.row.nHeight;
338 baseline = p->member.row.nBaseline;
343 run = &p->member.run;
344 if (visible && me_debug) {
345 rc.left = c->rcView.left+run->pt.x;
346 rc.right = c->rcView.left+run->pt.x+run->nWidth;
347 rc.top = c->pt.y+run->pt.y;
348 rc.bottom = c->pt.y+run->pt.y+height;
349 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
350 if (run->nFlags & MERF_SKIPPED)
351 DrawFocusRect(c->hDC, &rc);
353 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
356 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
359 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
360 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
364 pt.y = c->pt.y + run->pt.y;
365 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
366 ME_DebugWrite(c->hDC, &pt, buf);
368 /* c->pt.x += p->member.run.nWidth; */
375 SetTextAlign(c->hDC, align);
378 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
381 HWND hWnd = editor->hWnd;
383 si.cbSize = sizeof(SCROLLINFO);
385 GetScrollInfo(hWnd, SB_VERT, &si);
386 si.nPos = editor->nScrollPosY -= cy;
387 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
390 if (abs(cy) > editor->sizeWindow.cy)
391 InvalidateRect(editor->hWnd, NULL, TRUE);
393 ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
397 void ME_UpdateScrollBar(ME_TextEditor *editor)
399 HWND hWnd = editor->hWnd;
401 BOOL bUpdateScrollBars;
402 si.cbSize = sizeof(si);
403 si.fMask = SIF_PAGE | SIF_POS | SIF_RANGE;
404 GetScrollInfo(hWnd, SB_VERT, &si);
405 bUpdateScrollBars = (editor->bScrollY)&& ((si.nMax != editor->nTotalLength) || (si.nPage != editor->sizeWindow.cy));
407 if (editor->bScrollY != (si.nMax > 0))
408 { /* The scroll bar needs to be shown or hidden */
409 si.fMask = SIF_RANGE | SIF_PAGE;
410 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
411 si.fMask |= SIF_DISABLENOSCROLL;
414 si.nPage = editor->sizeWindow.cy;
416 if (editor->bScrollY)
417 si.nMax = editor->nTotalLength;
421 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
422 ME_MarkAllForWrapping(editor);
423 ME_WrapMarkedParagraphs(editor);
425 bUpdateScrollBars = TRUE;
427 if (bUpdateScrollBars)
430 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
431 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
432 si.fMask |= SIF_DISABLENOSCROLL;
433 if (editor->bScrollY)
435 si.nMax = editor->nTotalLength;
436 si.nPage = editor->sizeWindow.cy;
437 if (si.nPos > si.nMax-si.nPage)
439 nScroll = (si.nMax-si.nPage)-si.nPos;
440 si.nPos = si.nMax-si.nPage;
449 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
450 editor->nScrollPosY = si.nPos;
451 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
453 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
457 int ME_GetYScrollPos(ME_TextEditor *editor)
459 return editor->nScrollPosY;
462 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
464 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
465 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
466 int y, yrel, yheight, yold;
467 HWND hWnd = editor->hWnd;
472 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
473 yheight = pRow->member.row.nHeight;
474 yold = ME_GetYScrollPos(editor);
477 editor->nScrollPosY = y;
478 SetScrollPos(hWnd, SB_VERT, y, TRUE);
481 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
484 } else if (yrel + yheight > editor->sizeWindow.cy) {
485 int newy = y+yheight-editor->sizeWindow.cy;
486 editor->nScrollPosY = newy;
487 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
490 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
498 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
504 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
505 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
509 rc.bottom = y + height;
510 rc.right = editor->rcFormat.right;
511 InvalidateRect(editor->hWnd, &rc, FALSE);
516 ME_InvalidateSelection(ME_TextEditor *editor)
518 ME_DisplayItem *para1, *para2;
520 int len = ME_GetTextLength(editor);
522 ME_GetSelection(editor, &nStart, &nEnd);
523 /* if both old and new selection are 0-char (= caret only), then
524 there's no (inverted) area to be repainted, neither old nor new */
525 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
527 ME_WrapMarkedParagraphs(editor);
528 ME_GetSelectionParas(editor, ¶1, ¶2);
529 assert(para1->type == diParagraph);
530 assert(para2->type == diParagraph);
531 /* last selection markers aren't always updated, which means
532 they can point past the end of the document */
533 if (editor->nLastSelStart > len)
534 editor->nLastSelEnd = len;
535 if (editor->nLastSelEnd > len)
536 editor->nLastSelEnd = len;
538 /* if the start part of selection is being expanded or contracted... */
539 if (nStart < editor->nLastSelStart) {
540 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
542 if (nStart > editor->nLastSelStart) {
543 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
546 /* if the end part of selection is being contracted or expanded... */
547 if (nEnd < editor->nLastSelEnd) {
548 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
550 if (nEnd > editor->nLastSelEnd) {
551 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
554 ME_InvalidateMarkedParagraphs(editor);
555 /* remember the last invalidated position */
556 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
557 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
558 assert(editor->pLastSelStartPara->type == diParagraph);
559 assert(editor->pLastSelEndPara->type == diParagraph);
563 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
565 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
570 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
572 /* TODO: Zoom images and objects */
576 if (denominator == 0)
578 if (1.0 / 64.0 > (float)numerator / (float)denominator
579 || (float)numerator / (float)denominator > 64.0)
583 editor->nZoomNumerator = numerator;
584 editor->nZoomDenominator = denominator;
586 ME_RewrapRepaint(editor);