2 * RichEdit - painting functions
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, RECT *rcUpdate) {
31 yoffset = ME_GetYScrollPos(editor);
32 ME_InitContext(&c, editor, hDC);
33 SetBkMode(hDC, TRANSPARENT);
35 item = editor->pBuffer->pFirst->next;
37 while(item != editor->pBuffer->pLast) {
39 assert(item->type == diParagraph);
40 ye = c.pt.y + item->member.para.nHeight;
41 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
43 BOOL bPaint = (rcUpdate == NULL);
45 bPaint = c.pt.y<rcUpdate->bottom &&
46 c.pt.y+item->member.para.nHeight>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;
80 rc.left = xs; /* FIXME remove if it's not necessary anymore */
84 FillRect(hDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
86 if (ys == c.pt.y) /* don't overwrite the top bar */
93 /* this is not supposed to be gray, I know, but lets keep it gray for now for debugging purposes */
94 FillRect(hDC, &rc, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
97 editor->nLastTotalLength = editor->nTotalLength;
98 ME_DestroyContext(&c);
101 void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
102 ME_DisplayItem *p2, int nFlags)
107 p1->member.para.nFlags |= nFlags;
110 if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
111 p3 = p1, p1 = p2, p2 = p3;
113 p1->member.para.nFlags |= nFlags;
115 p1 = p1->member.para.next_para;
116 p1->member.para.nFlags |= nFlags;
120 void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
123 ME_CursorFromCharOfs(editor, from, &c1);
124 ME_CursorFromCharOfs(editor, to, &c2);
126 ME_MarkParagraphRange(editor, ME_GetParagraph(c1.pRun), ME_GetParagraph(c2.pRun), nFlags);
129 void ME_MarkSelectionForRepaint(ME_TextEditor *editor)
131 int from, to, from2, to2, end;
133 end = ME_GetTextLength(editor);
134 ME_GetSelection(editor, &from, &to);
135 from2 = editor->nLastSelStart;
136 to2 = editor->nLastSelEnd;
137 if (from<from2) ME_MarkOffsetRange(editor, from, from2, MEPF_REPAINT);
138 if (from>from2) ME_MarkOffsetRange(editor, from2, from, MEPF_REPAINT);
139 if (to<to2) ME_MarkOffsetRange(editor, to, to2, MEPF_REPAINT);
140 if (to>to2) ME_MarkOffsetRange(editor, to2, to, MEPF_REPAINT);
142 editor->nLastSelStart = from;
143 editor->nLastSelEnd = to;
146 void ME_Repaint(ME_TextEditor *editor)
148 ME_Cursor *pCursor = &editor->pCursors[0];
149 ME_DisplayItem *pRun = NULL;
152 int nCharOfs = ME_CharOfsFromRunOfs(editor, pCursor->pRun, pCursor->nOffset);
154 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
155 assert(pRun == pCursor->pRun);
156 assert(nOffset == pCursor->nOffset);
157 ME_MarkSelectionForRepaint(editor);
158 if (ME_WrapMarkedParagraphs(editor)) {
159 ME_UpdateScrollBar(editor);
161 hDC = GetDC(editor->hWnd);
162 ME_HideCaret(editor);
163 ME_PaintContent(editor, hDC, TRUE, NULL);
164 ReleaseDC(editor->hWnd, hDC);
165 ME_ShowCaret(editor);
166 ME_EnsureVisible(editor, pCursor->pRun);
169 void ME_UpdateRepaint(ME_TextEditor *editor)
172 InvalidateRect(editor->hWnd, NULL, TRUE);
174 ME_SendOldNotify(editor, EN_CHANGE);
176 ME_SendOldNotify(editor, EN_UPDATE);
177 ME_SendSelChange(editor);
180 void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
181 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
184 COLORREF rgbOld, rgbBack;
185 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
186 rgbBack = ME_GetBackColor(c->editor);
187 if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
188 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
190 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
191 ExtTextOutW(hDC, x, y, 0, NULL, szText, nChars, NULL);
194 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
197 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
200 if (nSelFrom < 0) nSelFrom = 0;
201 if (nSelTo > nChars) nSelTo = nChars;
202 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
204 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
205 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
207 SetTextColor(hDC, rgbOld);
208 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
211 void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
212 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
213 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
214 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
215 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
216 SelectObject(hDC, hFont);
217 SetTextAlign(hDC, align);
218 SetTextColor(hDC, color);
221 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
222 ME_Paragraph *para, BOOL selected) {
224 int xs, ys, xe, ye, h, ym, width, eyes;
225 ME_GetGraphicsSize(c->editor, run, &sz);
234 /* draw a smiling face :) */
235 Ellipse(c->hDC, xs, ys, xe, ye);
236 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
237 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
238 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
239 LineTo(c->hDC, xs+width/8, ys+3*h/4);
240 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
241 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
244 /* descent is usually (always?) 0 for graphics */
245 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
249 void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
250 ME_Run *run = &rundi->member.run;
251 int runofs = run->nCharOfs+para->nCharOfs;
253 /* you can always comment it out if you need visible paragraph marks */
254 if (run->nFlags & (MERF_ENDPARA|MERF_TAB))
256 if (run->nFlags & MERF_GRAPHICS) {
258 ME_GetSelection(c->editor, &blfrom, &blto);
259 ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
263 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
264 ME_GetSelection(c->editor, &blfrom, &blto);
266 ME_DrawTextWithStyle(c, x, y,
267 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
268 blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
272 COLORREF ME_GetBackColor(ME_TextEditor *editor)
274 /* Looks like I was seriously confused
275 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
277 if (editor->rgbBackColor == -1)
278 return GetSysColor(COLOR_WINDOW);
280 return editor->rgbBackColor;
283 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
284 int align = SetTextAlign(c->hDC, TA_BASELINE);
287 ME_Paragraph *para = NULL;
290 int height = 0, baseline = 0, no=0, pno = 0;
295 c->pt.x = c->rcView.left;
296 rcPara.left = c->rcView.left;
297 rcPara.right = c->rcView.right;
298 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
301 para = &p->member.para;
305 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
306 xs = c->rcView.left+nMargWidth;
307 xe = c->rcView.right-para->nRightMargin;
310 rcPara.bottom = y+p->member.row.nHeight;
311 visible = RectVisible(c->hDC, &rcPara);
314 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
316 rc.left = c->rcView.left;
317 rc.right = c->rcView.left+nMargWidth;
319 rc.bottom = y+p->member.row.nHeight;
320 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
323 rc.right = c->rcView.right;
324 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
325 rc.left = c->rcView.left+nMargWidth;
327 FillRect(c->hDC, &rc, hbr);
332 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
335 wsprintfW(buf, wszRowDebug, no);
337 ME_DebugWrite(c->hDC, &pt, buf);
340 height = p->member.row.nHeight;
341 baseline = p->member.row.nBaseline;
346 run = &p->member.run;
347 if (visible && me_debug) {
348 rc.left = c->rcView.left+run->pt.x;
349 rc.right = c->rcView.left+run->pt.x+run->nWidth;
350 rc.top = c->pt.y+run->pt.y;
351 rc.bottom = c->pt.y+run->pt.y+height;
352 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
353 if (run->nFlags & MERF_SKIPPED)
354 DrawFocusRect(c->hDC, &rc);
356 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
359 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
362 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
363 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
367 pt.y = c->pt.y + run->pt.y;
368 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
369 ME_DebugWrite(c->hDC, &pt, buf);
371 /* c->pt.x += p->member.run.nWidth; */
378 SetTextAlign(c->hDC, align);
381 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
384 HWND hWnd = editor->hWnd;
386 si.cbSize = sizeof(SCROLLINFO);
388 GetScrollInfo(hWnd, SB_VERT, &si);
389 si.nPos = editor->nScrollPosY -= cy;
390 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
391 if (abs(cy) > editor->sizeWindow.cy)
392 InvalidateRect(editor->hWnd, NULL, TRUE);
394 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 int nOldLen = editor->nTotalLength;
402 BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
403 BOOL bUpdateScrollBars;
404 si.cbSize = sizeof(si);
405 si.fMask = SIF_POS | SIF_RANGE;
406 GetScrollInfo(hWnd, SB_VERT, &si);
407 bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
409 if (bScrollY != editor->bScrollY)
411 si.fMask = SIF_RANGE | SIF_PAGE;
413 si.nPage = editor->sizeWindow.cy;
415 si.nMax = editor->nTotalLength;
419 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
420 ME_MarkAllForWrapping(editor);
421 editor->bScrollY = bScrollY;
422 ME_WrapMarkedParagraphs(editor);
423 bUpdateScrollBars = TRUE;
425 if (bUpdateScrollBars) {
427 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
428 if (editor->nTotalLength > editor->sizeWindow.cy) {
429 si.nMax = editor->nTotalLength;
430 si.nPage = editor->sizeWindow.cy;
431 if (si.nPos > si.nMax-si.nPage) {
432 nScroll = (si.nMax-si.nPage)-si.nPos;
433 si.nPos = si.nMax-si.nPage;
441 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
442 editor->nScrollPosY = si.nPos;
443 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
445 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
449 int ME_GetYScrollPos(ME_TextEditor *editor)
451 return editor->nScrollPosY;
454 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
456 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
457 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
458 int y, yrel, yheight, yold;
459 HWND hWnd = editor->hWnd;
464 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
465 yheight = pRow->member.row.nHeight;
466 yold = ME_GetYScrollPos(editor);
469 editor->nScrollPosY = y;
470 SetScrollPos(hWnd, SB_VERT, y, TRUE);
471 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
473 } else if (yrel + yheight > editor->sizeWindow.cy) {
474 int newy = y+yheight-editor->sizeWindow.cy;
475 editor->nScrollPosY = newy;
476 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
477 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);