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_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
142 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
144 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
145 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
146 yTwipsOffset = s->fmt.yOffset;
148 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
149 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
150 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
157 if (c->editor->nZoomNumerator)
159 numerator = c->editor->nZoomNumerator;
160 denominator = c->editor->nZoomDenominator;
162 yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
164 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
167 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
170 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
173 if (nSelFrom < 0) nSelFrom = 0;
174 if (nSelTo > nChars) nSelTo = nChars;
175 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
177 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
179 /* Invert selection if not hidden by EM_HIDESELECTION */
180 if (c->editor->bHideSelection == FALSE)
181 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
183 SetTextColor(hDC, rgbOld);
184 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
187 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
188 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
189 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
190 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
191 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
192 SelectObject(hDC, hFont);
193 SetTextAlign(hDC, align);
194 SetTextColor(hDC, color);
197 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
198 ME_Paragraph *para, BOOL selected) {
200 int xs, ys, xe, ye, h, ym, width, eyes;
201 ME_GetGraphicsSize(c->editor, run, &sz);
210 /* draw a smiling face :) */
211 Ellipse(c->hDC, xs, ys, xe, ye);
212 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
213 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
214 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
215 LineTo(c->hDC, xs+width/8, ys+3*h/4);
216 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
217 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
220 /* descent is usually (always?) 0 for graphics */
221 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
225 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
227 ME_Run *run = &rundi->member.run;
228 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
229 int runofs = run->nCharOfs+para->nCharOfs;
230 int nSelFrom, nSelTo;
231 const WCHAR wszSpace[] = {' ', 0};
233 if (run->nFlags & MERF_HIDDEN)
236 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
238 /* Draw selected end-of-paragraph mark */
239 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
240 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
241 c->pt.y + start->member.row.nYPos,
242 start->member.row.nHeight);
244 /* you can always comment it out if you need visible paragraph marks */
245 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
248 if (run->nFlags & MERF_GRAPHICS)
249 ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
252 if (c->editor->cPasswordMask)
254 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
255 ME_DrawTextWithStyle(c, x, y,
256 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
257 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
258 ME_DestroyString(szMasked);
261 ME_DrawTextWithStyle(c, x, y,
262 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
263 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
267 COLORREF ME_GetBackColor(ME_TextEditor *editor)
269 /* Looks like I was seriously confused
270 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
272 if (editor->rgbBackColor == -1)
273 return GetSysColor(COLOR_WINDOW);
275 return editor->rgbBackColor;
278 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
279 int align = SetTextAlign(c->hDC, TA_BASELINE);
282 ME_Paragraph *para = NULL;
285 int height = 0, baseline = 0, no=0, pno = 0;
290 c->pt.x = c->rcView.left;
291 rcPara.left = c->rcView.left;
292 rcPara.right = c->rcView.right;
293 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
296 para = &p->member.para;
300 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
301 xs = c->rcView.left+nMargWidth;
302 xe = c->rcView.right-para->nRightMargin;
305 rcPara.bottom = y+p->member.row.nHeight;
306 visible = RectVisible(c->hDC, &rcPara);
309 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
311 rc.left = c->rcView.left;
312 rc.right = c->rcView.left+nMargWidth;
314 rc.bottom = y+p->member.row.nHeight;
315 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
318 rc.right = c->rcView.right;
319 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
320 rc.left = c->rcView.left+nMargWidth;
322 FillRect(c->hDC, &rc, hbr);
327 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
330 wsprintfW(buf, wszRowDebug, no);
332 ME_DebugWrite(c->hDC, &pt, buf);
335 height = p->member.row.nHeight;
336 baseline = p->member.row.nBaseline;
341 run = &p->member.run;
342 if (visible && me_debug) {
343 rc.left = c->rcView.left+run->pt.x;
344 rc.right = c->rcView.left+run->pt.x+run->nWidth;
345 rc.top = c->pt.y+run->pt.y;
346 rc.bottom = c->pt.y+run->pt.y+height;
347 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
348 if (run->nFlags & MERF_SKIPPED)
349 DrawFocusRect(c->hDC, &rc);
351 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
354 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
357 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
358 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
362 pt.y = c->pt.y + run->pt.y;
363 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
364 ME_DebugWrite(c->hDC, &pt, buf);
366 /* c->pt.x += p->member.run.nWidth; */
373 SetTextAlign(c->hDC, align);
376 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
379 HWND hWnd = editor->hWnd;
381 si.cbSize = sizeof(SCROLLINFO);
383 GetScrollInfo(hWnd, SB_VERT, &si);
384 si.nPos = editor->nScrollPosY -= cy;
385 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
388 if (abs(cy) > editor->sizeWindow.cy)
389 InvalidateRect(editor->hWnd, NULL, TRUE);
391 ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
395 void ME_UpdateScrollBar(ME_TextEditor *editor)
397 HWND hWnd = editor->hWnd;
399 int nOldLen = editor->nTotalLength;
400 BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
401 BOOL bUpdateScrollBars;
402 si.cbSize = sizeof(si);
403 si.fMask = SIF_POS | SIF_RANGE;
404 GetScrollInfo(hWnd, SB_VERT, &si);
405 bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
407 if (bScrollY != editor->bScrollY)
409 si.fMask = SIF_RANGE | SIF_PAGE;
411 si.nPage = editor->sizeWindow.cy;
413 si.nMax = editor->nTotalLength;
417 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
418 ME_MarkAllForWrapping(editor);
419 editor->bScrollY = bScrollY;
420 ME_WrapMarkedParagraphs(editor);
421 bUpdateScrollBars = TRUE;
423 if (bUpdateScrollBars) {
425 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
426 if (editor->nTotalLength > editor->sizeWindow.cy) {
427 si.nMax = editor->nTotalLength;
428 si.nPage = editor->sizeWindow.cy;
429 if (si.nPos > si.nMax-si.nPage) {
430 nScroll = (si.nMax-si.nPage)-si.nPos;
431 si.nPos = si.nMax-si.nPage;
439 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
440 editor->nScrollPosY = si.nPos;
441 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
443 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
447 int ME_GetYScrollPos(ME_TextEditor *editor)
449 return editor->nScrollPosY;
452 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
454 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
455 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
456 int y, yrel, yheight, yold;
457 HWND hWnd = editor->hWnd;
462 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
463 yheight = pRow->member.row.nHeight;
464 yold = ME_GetYScrollPos(editor);
467 editor->nScrollPosY = y;
468 SetScrollPos(hWnd, SB_VERT, y, TRUE);
471 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
474 } else if (yrel + yheight > editor->sizeWindow.cy) {
475 int newy = y+yheight-editor->sizeWindow.cy;
476 editor->nScrollPosY = newy;
477 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
480 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
488 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
494 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
495 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
499 rc.bottom = y + height;
500 rc.right = editor->rcFormat.right;
501 InvalidateRect(editor->hWnd, &rc, FALSE);
506 ME_InvalidateSelection(ME_TextEditor *editor)
508 if (ME_IsSelection(editor) || editor->nLastSelStart != editor->nLastSelEnd)
512 int last_x, last_y, last_height;
513 int last_x2, last_y2, last_height2;
517 ME_GetCursorCoordinates(editor, &editor->pCursors[1], &x, &y, &height);
518 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x2, &y2, &height2);
519 ME_RunOfsFromCharOfs(editor, editor->nLastSelStart, &tmp.pRun, &tmp.nOffset);
520 ME_GetCursorCoordinates(editor, &tmp, &last_x, &last_y, &last_height);
521 ME_RunOfsFromCharOfs(editor, editor->nLastSelEnd, &tmp.pRun, &tmp.nOffset);
522 ME_GetCursorCoordinates(editor, &tmp, &last_x2, &last_y2, &last_height2);
526 rc.top = min(min(y, last_y), min(y2, last_y2));
527 rc.right = editor->rcFormat.right;
528 rc.bottom = max(max(y + height, last_y + last_height),
529 max(y2 + height2, last_y2 + last_height2));
530 InvalidateRect(editor->hWnd, &rc, FALSE);
533 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
538 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
540 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
545 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
547 /* TODO: Zoom images and objects */
551 if (denominator == 0)
553 if (1.0 / 64.0 > (float)numerator / (float)denominator
554 || (float)numerator / (float)denominator > 64.0)
558 editor->nZoomNumerator = numerator;
559 editor->nZoomDenominator = denominator;
561 ME_RewrapRepaint(editor);