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 = GetScrollPos(editor->hWnd, SB_VERT);
32 ME_InitContext(&c, editor, hDC);
33 SetBkMode(hDC, TRANSPARENT);
35 item = editor->pBuffer->pFirst->next;
37 while(item != editor->pBuffer->pLast) {
38 assert(item->type == diParagraph);
39 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
41 BOOL bPaint = (rcUpdate == NULL);
43 bPaint = c.pt.y<rcUpdate->bottom &&
44 c.pt.y+item->member.para.nHeight>rcUpdate->top;
47 ME_DrawParagraph(&c, item);
48 item->member.para.nFlags &= ~MEPF_REPAINT;
51 c.pt.y += item->member.para.nHeight;
52 item = item->member.para.next_para;
54 if (c.pt.y<c.rcView.bottom) {
56 int xs = c.rcView.left, xe = c.rcView.right;
57 int ys = c.pt.y, ye = c.rcView.bottom;
61 int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
68 if (rcUpdate && ys!=ye)
70 xs = rcUpdate->left, xe = rcUpdate->right;
71 if (rcUpdate->top > ys)
73 if (rcUpdate->bottom < ye)
74 ye = rcUpdate->bottom;
77 rc.left = xs; /* FIXME remove if it's not necessary anymore */
81 FillRect(hDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
83 if (ys == c.pt.y) /* don't overwrite the top bar */
90 /* this is not supposed to be gray, I know, but lets keep it gray for now for debugging purposes */
91 FillRect(hDC, &rc, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
94 editor->nLastTotalLength = editor->nTotalLength;
95 ME_DestroyContext(&c);
98 void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
99 ME_DisplayItem *p2, int nFlags)
104 p1->member.para.nFlags |= nFlags;
107 if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
108 p3 = p1, p1 = p2, p2 = p3;
110 p1->member.para.nFlags |= nFlags;
112 p1 = p1->member.para.next_para;
113 p1->member.para.nFlags |= nFlags;
117 void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
120 ME_CursorFromCharOfs(editor, from, &c1);
121 ME_CursorFromCharOfs(editor, to, &c2);
123 ME_MarkParagraphRange(editor, ME_GetParagraph(c1.pRun), ME_GetParagraph(c2.pRun), nFlags);
126 void ME_MarkSelectionForRepaint(ME_TextEditor *editor)
128 int from, to, from2, to2, end;
130 end = ME_GetTextLength(editor);
131 ME_GetSelection(editor, &from, &to);
132 from2 = editor->nLastSelStart;
133 to2 = editor->nLastSelEnd;
134 if (from<from2) ME_MarkOffsetRange(editor, from, from2, MEPF_REPAINT);
135 if (from>from2) ME_MarkOffsetRange(editor, from2, from, MEPF_REPAINT);
136 if (to<to2) ME_MarkOffsetRange(editor, to, to2, MEPF_REPAINT);
137 if (to>to2) ME_MarkOffsetRange(editor, to2, to, MEPF_REPAINT);
139 editor->nLastSelStart = from;
140 editor->nLastSelEnd = to;
143 void ME_Repaint(ME_TextEditor *editor)
145 ME_Cursor *pCursor = &editor->pCursors[0];
146 ME_DisplayItem *pRun = NULL;
149 int nCharOfs = ME_CharOfsFromRunOfs(editor, pCursor->pRun, pCursor->nOffset);
151 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
152 assert(pRun == pCursor->pRun);
153 assert(nOffset == pCursor->nOffset);
154 ME_MarkSelectionForRepaint(editor);
155 ME_WrapMarkedParagraphs(editor);
156 hDC = GetDC(editor->hWnd);
157 ME_HideCaret(editor);
158 ME_PaintContent(editor, hDC, TRUE, NULL);
159 ReleaseDC(editor->hWnd, hDC);
160 ME_ShowCaret(editor);
163 void ME_UpdateRepaint(ME_TextEditor *editor)
166 InvalidateRect(editor->hWnd, NULL, TRUE);
168 ME_SendOldNotify(editor, EN_CHANGE);
170 ME_SendOldNotify(editor, EN_UPDATE);
171 ME_SendSelChange(editor);
174 void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
175 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
178 COLORREF rgbOld, rgbBack;
179 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
180 rgbBack = ME_GetBackColor(c->editor);
181 if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
182 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
184 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
185 ExtTextOutW(hDC, x, y, 0, NULL, szText, nChars, NULL);
188 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
191 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
194 if (nSelFrom < 0) nSelFrom = 0;
195 if (nSelTo > nChars) nSelTo = nChars;
196 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
198 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
199 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
201 SetTextColor(hDC, rgbOld);
202 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
205 void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
206 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
207 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
208 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
209 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
210 SelectObject(hDC, hFont);
211 SetTextAlign(hDC, align);
212 SetTextColor(hDC, color);
215 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
216 ME_Paragraph *para, BOOL selected) {
218 int xs, ys, xe, ye, h, ym, width, eyes;
219 ME_GetGraphicsSize(c->editor, run, &sz);
228 /* draw a smiling face :) */
229 Ellipse(c->hDC, xs, ys, xe, ye);
230 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
231 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
232 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
233 LineTo(c->hDC, xs+width/8, ys+3*h/4);
234 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
235 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
238 /* descent is usually (always?) 0 for graphics */
239 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
243 void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
244 ME_Run *run = &rundi->member.run;
245 int runofs = run->nCharOfs+para->nCharOfs;
247 /* you can always comment it out if you need visible paragraph marks */
248 if (run->nFlags & MERF_ENDPARA)
250 if (run->nFlags & MERF_GRAPHICS) {
252 ME_GetSelection(c->editor, &blfrom, &blto);
253 ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
257 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
258 ME_GetSelection(c->editor, &blfrom, &blto);
260 ME_DrawTextWithStyle(c, x, y,
261 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
262 blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
266 COLORREF ME_GetBackColor(ME_TextEditor *editor)
268 /* Looks like I was seriously confused
269 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
271 if (editor->rgbBackColor == -1)
272 return GetSysColor(COLOR_WINDOW);
274 return editor->rgbBackColor;
277 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
278 int align = SetTextAlign(c->hDC, TA_BASELINE);
281 ME_Paragraph *para = NULL;
284 int height = 0, baseline = 0, no=0, pno = 0;
289 c->pt.x = c->rcView.left;
290 rcPara.left = c->rcView.left;
291 rcPara.right = c->rcView.right;
292 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
295 para = &p->member.para;
299 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
300 xs = c->rcView.left+nMargWidth;
301 xe = c->rcView.right-para->nRightMargin;
304 rcPara.bottom = y+p->member.row.nHeight;
305 visible = RectVisible(c->hDC, &rcPara);
309 rc.left = c->rcView.left;
310 rc.right = c->rcView.left+nMargWidth;
312 rc.bottom = y+p->member.row.nHeight;
313 FillRect(c->hDC, &rc, c->hbrMargin);
316 rc.right = c->rcView.right;
317 FillRect(c->hDC, &rc, c->hbrMargin);
318 rc.left = c->rcView.left+para->nLeftMargin;
320 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
321 FillRect(c->hDC, &rc, hbr);
326 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
329 wsprintfW(buf, wszRowDebug, no);
331 ME_DebugWrite(c->hDC, &pt, buf);
334 height = p->member.row.nHeight;
335 baseline = p->member.row.nBaseline;
340 run = &p->member.run;
341 if (visible && me_debug) {
342 rc.left = c->rcView.left+run->pt.x;
343 rc.right = c->rcView.left+run->pt.x+run->nWidth;
344 rc.top = c->pt.y+run->pt.y;
345 rc.bottom = c->pt.y+run->pt.y+height;
346 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
347 if (run->nFlags & MERF_SKIPPED)
348 DrawFocusRect(c->hDC, &rc);
350 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
353 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
356 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
357 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
361 pt.y = c->pt.y + run->pt.y;
362 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
363 ME_DebugWrite(c->hDC, &pt, buf);
365 /* c->pt.x += p->member.run.nWidth; */
372 SetTextAlign(c->hDC, align);
375 void ME_UpdateScrollBar(ME_TextEditor *editor, int ypos)
379 HWND hWnd = editor->hWnd;
380 int overflow = editor->nTotalLength - editor->sizeWindow.cy;
381 si.cbSize = sizeof(SCROLLINFO);
382 si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
383 GetScrollInfo(hWnd, SB_VERT, &si);
386 if (si.nMax<1) si.nMax = 1;
387 perc = 1.0*si.nPos/si.nMax;
388 ypos = perc*overflow;
390 if (ypos >= overflow && overflow > 0)
394 EnableScrollBar(hWnd, SB_VERT, ESB_ENABLE_BOTH);
395 SetScrollRange(hWnd, SB_VERT, 0, overflow, FALSE);
396 SetScrollPos(hWnd, SB_VERT, ypos, TRUE);
398 EnableScrollBar(hWnd, SB_VERT, ESB_DISABLE_BOTH);
399 SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
400 SetScrollPos(hWnd, SB_VERT, 0, TRUE);
404 TRACE("ScrollWindow(%d, %d, %d, %0.4f)\n", si.nPos, si.nMax, ypos, perc);
405 ScrollWindow(hWnd, 0, si.nPos - ypos, NULL, NULL);
410 int ME_GetScrollPos(ME_TextEditor *editor)
412 return GetScrollPos(editor->hWnd, SB_VERT);
415 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
417 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
418 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
419 int y, yrel, yheight;
424 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
425 yheight = pRow->member.row.nHeight;
426 yrel = y - ME_GetScrollPos(editor);
428 ME_UpdateScrollBar(editor, y);
429 else if (yrel + yheight > editor->sizeWindow.cy)
431 ME_UpdateScrollBar(editor, y + yheight - editor->sizeWindow.cy);