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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 static void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
98 ME_DisplayItem *p2, int nFlags)
103 p1->member.para.nFlags |= nFlags;
106 if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
107 p3 = p1, p1 = p2, p2 = p3;
109 p1->member.para.nFlags |= nFlags;
111 p1 = p1->member.para.next_para;
112 p1->member.para.nFlags |= nFlags;
116 static void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
119 ME_CursorFromCharOfs(editor, from, &c1);
120 ME_CursorFromCharOfs(editor, to, &c2);
122 ME_MarkParagraphRange(editor, ME_GetParagraph(c1.pRun), ME_GetParagraph(c2.pRun), nFlags);
125 static void ME_MarkSelectionForRepaint(ME_TextEditor *editor)
127 int from, to, from2, to2, end;
129 end = ME_GetTextLength(editor);
130 ME_GetSelection(editor, &from, &to);
131 from2 = editor->nLastSelStart;
132 to2 = editor->nLastSelEnd;
133 if (from<from2) ME_MarkOffsetRange(editor, from, from2, MEPF_REPAINT);
134 if (from>from2) ME_MarkOffsetRange(editor, from2, from, MEPF_REPAINT);
135 if (to<to2) ME_MarkOffsetRange(editor, to, to2, MEPF_REPAINT);
136 if (to>to2) ME_MarkOffsetRange(editor, to2, to, MEPF_REPAINT);
138 editor->nLastSelStart = from;
139 editor->nLastSelEnd = to;
142 void ME_Repaint(ME_TextEditor *editor)
144 ME_Cursor *pCursor = &editor->pCursors[0];
146 if (ME_WrapMarkedParagraphs(editor)) {
147 ME_UpdateScrollBar(editor);
151 ME_EnsureVisible(editor, pCursor->pRun);
152 UpdateWindow(editor->hWnd);
156 void ME_UpdateRepaint(ME_TextEditor *editor)
159 InvalidateRect(editor->hWnd, NULL, TRUE);
161 ME_SendOldNotify(editor, EN_CHANGE);
163 ME_SendOldNotify(editor, EN_UPDATE);
164 ME_SendSelChange(editor);
169 ME_RewrapRepaint(ME_TextEditor *editor)
171 ME_MarkAllForWrapping(editor);
172 ME_WrapMarkedParagraphs(editor);
173 ME_UpdateScrollBar(editor);
178 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
179 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
182 COLORREF rgbOld, rgbBack;
183 int yOffset = 0, yTwipsOffset = 0;
184 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
185 rgbBack = ME_GetBackColor(c->editor);
186 if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
187 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
189 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
190 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
191 yTwipsOffset = s->fmt.yOffset;
193 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
194 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
195 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
202 if (c->editor->nZoomNumerator)
204 numerator = c->editor->nZoomNumerator;
205 denominator = c->editor->nZoomDenominator;
207 yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
209 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
212 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
215 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);
223 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
225 SetTextColor(hDC, rgbOld);
226 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
229 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
230 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
231 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
232 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
233 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
234 SelectObject(hDC, hFont);
235 SetTextAlign(hDC, align);
236 SetTextColor(hDC, color);
239 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
240 ME_Paragraph *para, BOOL selected) {
242 int xs, ys, xe, ye, h, ym, width, eyes;
243 ME_GetGraphicsSize(c->editor, run, &sz);
252 /* draw a smiling face :) */
253 Ellipse(c->hDC, xs, ys, xe, ye);
254 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
255 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
256 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
257 LineTo(c->hDC, xs+width/8, ys+3*h/4);
258 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
259 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
262 /* descent is usually (always?) 0 for graphics */
263 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
267 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
268 ME_Run *run = &rundi->member.run;
269 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
270 int runofs = run->nCharOfs+para->nCharOfs;
271 int nSelFrom, nSelTo;
272 const WCHAR wszSpace[] = {' ', 0};
274 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
276 /* Draw selected end-of-paragraph mark */
277 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
278 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
279 c->pt.y + start->member.row.nYPos,
280 start->member.row.nHeight);
282 /* you can always comment it out if you need visible paragraph marks */
283 if (run->nFlags & (MERF_ENDPARA|MERF_TAB))
286 if (run->nFlags & MERF_GRAPHICS)
287 ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
289 ME_DrawTextWithStyle(c, x, y,
290 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
291 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
294 COLORREF ME_GetBackColor(ME_TextEditor *editor)
296 /* Looks like I was seriously confused
297 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
299 if (editor->rgbBackColor == -1)
300 return GetSysColor(COLOR_WINDOW);
302 return editor->rgbBackColor;
305 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
306 int align = SetTextAlign(c->hDC, TA_BASELINE);
309 ME_Paragraph *para = NULL;
312 int height = 0, baseline = 0, no=0, pno = 0;
317 c->pt.x = c->rcView.left;
318 rcPara.left = c->rcView.left;
319 rcPara.right = c->rcView.right;
320 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
323 para = &p->member.para;
327 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
328 xs = c->rcView.left+nMargWidth;
329 xe = c->rcView.right-para->nRightMargin;
332 rcPara.bottom = y+p->member.row.nHeight;
333 visible = RectVisible(c->hDC, &rcPara);
336 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
338 rc.left = c->rcView.left;
339 rc.right = c->rcView.left+nMargWidth;
341 rc.bottom = y+p->member.row.nHeight;
342 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
345 rc.right = c->rcView.right;
346 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
347 rc.left = c->rcView.left+nMargWidth;
349 FillRect(c->hDC, &rc, hbr);
354 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
357 wsprintfW(buf, wszRowDebug, no);
359 ME_DebugWrite(c->hDC, &pt, buf);
362 height = p->member.row.nHeight;
363 baseline = p->member.row.nBaseline;
368 run = &p->member.run;
369 if (visible && me_debug) {
370 rc.left = c->rcView.left+run->pt.x;
371 rc.right = c->rcView.left+run->pt.x+run->nWidth;
372 rc.top = c->pt.y+run->pt.y;
373 rc.bottom = c->pt.y+run->pt.y+height;
374 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
375 if (run->nFlags & MERF_SKIPPED)
376 DrawFocusRect(c->hDC, &rc);
378 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
381 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
384 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
385 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
389 pt.y = c->pt.y + run->pt.y;
390 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
391 ME_DebugWrite(c->hDC, &pt, buf);
393 /* c->pt.x += p->member.run.nWidth; */
400 SetTextAlign(c->hDC, align);
403 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
406 HWND hWnd = editor->hWnd;
408 si.cbSize = sizeof(SCROLLINFO);
410 GetScrollInfo(hWnd, SB_VERT, &si);
411 si.nPos = editor->nScrollPosY -= cy;
412 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
415 if (abs(cy) > editor->sizeWindow.cy)
416 InvalidateRect(editor->hWnd, NULL, TRUE);
418 ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
422 void ME_UpdateScrollBar(ME_TextEditor *editor)
424 HWND hWnd = editor->hWnd;
426 int nOldLen = editor->nTotalLength;
427 BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
428 BOOL bUpdateScrollBars;
429 si.cbSize = sizeof(si);
430 si.fMask = SIF_POS | SIF_RANGE;
431 GetScrollInfo(hWnd, SB_VERT, &si);
432 bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
434 if (bScrollY != editor->bScrollY)
436 si.fMask = SIF_RANGE | SIF_PAGE;
438 si.nPage = editor->sizeWindow.cy;
440 si.nMax = editor->nTotalLength;
444 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
445 ME_MarkAllForWrapping(editor);
446 editor->bScrollY = bScrollY;
447 ME_WrapMarkedParagraphs(editor);
448 bUpdateScrollBars = TRUE;
450 if (bUpdateScrollBars) {
452 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
453 if (editor->nTotalLength > editor->sizeWindow.cy) {
454 si.nMax = editor->nTotalLength;
455 si.nPage = editor->sizeWindow.cy;
456 if (si.nPos > si.nMax-si.nPage) {
457 nScroll = (si.nMax-si.nPage)-si.nPos;
458 si.nPos = si.nMax-si.nPage;
466 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
467 editor->nScrollPosY = si.nPos;
468 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
470 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
474 int ME_GetYScrollPos(ME_TextEditor *editor)
476 return editor->nScrollPosY;
479 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
481 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
482 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
483 int y, yrel, yheight, yold;
484 HWND hWnd = editor->hWnd;
489 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
490 yheight = pRow->member.row.nHeight;
491 yold = ME_GetYScrollPos(editor);
494 editor->nScrollPosY = y;
495 SetScrollPos(hWnd, SB_VERT, y, TRUE);
498 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
501 } else if (yrel + yheight > editor->sizeWindow.cy) {
502 int newy = y+yheight-editor->sizeWindow.cy;
503 editor->nScrollPosY = newy;
504 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
507 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
515 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
521 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
522 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
526 rc.bottom = y + height;
527 rc.right = editor->rcFormat.right;
528 InvalidateRect(editor->hWnd, &rc, FALSE);
533 ME_InvalidateSelection(ME_TextEditor *editor)
535 if (ME_IsSelection(editor) || editor->nLastSelStart != editor->nLastSelEnd)
539 int last_x, last_y, last_height;
540 int last_x2, last_y2, last_height2;
544 ME_GetCursorCoordinates(editor, &editor->pCursors[1], &x, &y, &height);
545 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x2, &y2, &height2);
546 ME_RunOfsFromCharOfs(editor, editor->nLastSelStart, &tmp.pRun, &tmp.nOffset);
547 ME_GetCursorCoordinates(editor, &tmp, &last_x, &last_y, &last_height);
548 ME_RunOfsFromCharOfs(editor, editor->nLastSelEnd, &tmp.pRun, &tmp.nOffset);
549 ME_GetCursorCoordinates(editor, &tmp, &last_x2, &last_y2, &last_height2);
553 rc.top = min(min(y, last_y), min(y2, last_y2));
554 rc.right = editor->rcFormat.right;
555 rc.bottom = max(max(y + height, last_y + last_height),
556 max(y2 + height2, last_y2 + last_height2));
557 InvalidateRect(editor->hWnd, &rc, FALSE);
560 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
565 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
567 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
572 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
574 /* TODO: Zoom images and objects */
578 if (denominator == 0)
580 if (1.0 / 64.0 > (float)numerator / (float)denominator
581 || (float)numerator / (float)denominator > 64.0)
585 editor->nZoomNumerator = numerator;
586 editor->nZoomDenominator = denominator;
588 ME_RewrapRepaint(editor);