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;
85 FillRect(hDC, &rc, c.editor->hbrBackground);
87 if (ys == c.pt.y) /* don't overwrite the top bar */
90 if (editor->nTotalLength != editor->nLastTotalLength)
91 ME_SendRequestResize(editor, FALSE);
92 editor->nLastTotalLength = editor->nTotalLength;
93 ME_DestroyContext(&c);
96 static void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
97 ME_DisplayItem *p2, int nFlags)
102 p1->member.para.nFlags |= nFlags;
105 if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
106 p3 = p1, p1 = p2, p2 = p3;
108 p1->member.para.nFlags |= nFlags;
110 p1 = p1->member.para.next_para;
111 p1->member.para.nFlags |= nFlags;
115 static void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
118 ME_CursorFromCharOfs(editor, from, &c1);
119 ME_CursorFromCharOfs(editor, to, &c2);
121 ME_MarkParagraphRange(editor, ME_GetParagraph(c1.pRun), ME_GetParagraph(c2.pRun), nFlags);
124 static void ME_MarkSelectionForRepaint(ME_TextEditor *editor)
126 int from, to, from2, to2, end;
128 end = ME_GetTextLength(editor);
129 ME_GetSelection(editor, &from, &to);
130 from2 = editor->nLastSelStart;
131 to2 = editor->nLastSelEnd;
132 if (from<from2) ME_MarkOffsetRange(editor, from, from2, MEPF_REPAINT);
133 if (from>from2) ME_MarkOffsetRange(editor, from2, from, MEPF_REPAINT);
134 if (to<to2) ME_MarkOffsetRange(editor, to, to2, MEPF_REPAINT);
135 if (to>to2) ME_MarkOffsetRange(editor, to2, to, MEPF_REPAINT);
137 editor->nLastSelStart = from;
138 editor->nLastSelEnd = to;
141 void ME_Repaint(ME_TextEditor *editor)
143 ME_Cursor *pCursor = &editor->pCursors[0];
144 ME_DisplayItem *pRun = NULL;
147 int nCharOfs = ME_CharOfsFromRunOfs(editor, pCursor->pRun, pCursor->nOffset);
149 ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
150 assert(pRun == pCursor->pRun);
151 assert(nOffset == pCursor->nOffset);
152 ME_MarkSelectionForRepaint(editor);
153 if (ME_WrapMarkedParagraphs(editor)) {
154 ME_UpdateScrollBar(editor);
158 hDC = GetDC(editor->hWnd);
159 ME_HideCaret(editor);
160 ME_PaintContent(editor, hDC, TRUE, NULL);
161 ReleaseDC(editor->hWnd, hDC);
162 ME_ShowCaret(editor);
163 ME_EnsureVisible(editor, pCursor->pRun);
167 void ME_UpdateRepaint(ME_TextEditor *editor)
170 InvalidateRect(editor->hWnd, NULL, TRUE);
172 ME_SendOldNotify(editor, EN_CHANGE);
174 ME_SendOldNotify(editor, EN_UPDATE);
175 ME_SendSelChange(editor);
180 ME_RewrapRepaint(ME_TextEditor *editor)
182 ME_MarkAllForWrapping(editor);
183 ME_WrapMarkedParagraphs(editor);
184 ME_UpdateScrollBar(editor);
189 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
190 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
193 COLORREF rgbOld, rgbBack;
194 int yOffset = 0, yTwipsOffset = 0;
195 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
196 rgbBack = ME_GetBackColor(c->editor);
197 if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
198 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
200 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
201 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
202 yTwipsOffset = s->fmt.yOffset;
204 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
205 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
206 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
213 if (c->editor->nZoomNumerator)
215 numerator = c->editor->nZoomNumerator;
216 denominator = c->editor->nZoomDenominator;
218 yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
220 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
223 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
226 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
229 if (nSelFrom < 0) nSelFrom = 0;
230 if (nSelTo > nChars) nSelTo = nChars;
231 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
233 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
234 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
236 SetTextColor(hDC, rgbOld);
237 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
240 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
241 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
242 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
243 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
244 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
245 SelectObject(hDC, hFont);
246 SetTextAlign(hDC, align);
247 SetTextColor(hDC, color);
250 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
251 ME_Paragraph *para, BOOL selected) {
253 int xs, ys, xe, ye, h, ym, width, eyes;
254 ME_GetGraphicsSize(c->editor, run, &sz);
263 /* draw a smiling face :) */
264 Ellipse(c->hDC, xs, ys, xe, ye);
265 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
266 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
267 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
268 LineTo(c->hDC, xs+width/8, ys+3*h/4);
269 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
270 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
273 /* descent is usually (always?) 0 for graphics */
274 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
278 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
279 ME_Run *run = &rundi->member.run;
280 int runofs = run->nCharOfs+para->nCharOfs;
282 /* you can always comment it out if you need visible paragraph marks */
283 if (run->nFlags & (MERF_ENDPARA|MERF_TAB))
285 if (run->nFlags & MERF_GRAPHICS) {
287 ME_GetSelection(c->editor, &blfrom, &blto);
288 ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
292 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
293 ME_GetSelection(c->editor, &blfrom, &blto);
295 ME_DrawTextWithStyle(c, x, y,
296 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
297 blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
301 COLORREF ME_GetBackColor(ME_TextEditor *editor)
303 /* Looks like I was seriously confused
304 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
306 if (editor->rgbBackColor == -1)
307 return GetSysColor(COLOR_WINDOW);
309 return editor->rgbBackColor;
312 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
313 int align = SetTextAlign(c->hDC, TA_BASELINE);
316 ME_Paragraph *para = NULL;
319 int height = 0, baseline = 0, no=0, pno = 0;
324 c->pt.x = c->rcView.left;
325 rcPara.left = c->rcView.left;
326 rcPara.right = c->rcView.right;
327 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
330 para = &p->member.para;
334 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
335 xs = c->rcView.left+nMargWidth;
336 xe = c->rcView.right-para->nRightMargin;
339 rcPara.bottom = y+p->member.row.nHeight;
340 visible = RectVisible(c->hDC, &rcPara);
343 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
345 rc.left = c->rcView.left;
346 rc.right = c->rcView.left+nMargWidth;
348 rc.bottom = y+p->member.row.nHeight;
349 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
352 rc.right = c->rcView.right;
353 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
354 rc.left = c->rcView.left+nMargWidth;
356 FillRect(c->hDC, &rc, hbr);
361 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
364 wsprintfW(buf, wszRowDebug, no);
366 ME_DebugWrite(c->hDC, &pt, buf);
369 height = p->member.row.nHeight;
370 baseline = p->member.row.nBaseline;
375 run = &p->member.run;
376 if (visible && me_debug) {
377 rc.left = c->rcView.left+run->pt.x;
378 rc.right = c->rcView.left+run->pt.x+run->nWidth;
379 rc.top = c->pt.y+run->pt.y;
380 rc.bottom = c->pt.y+run->pt.y+height;
381 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
382 if (run->nFlags & MERF_SKIPPED)
383 DrawFocusRect(c->hDC, &rc);
385 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
388 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
391 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
392 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
396 pt.y = c->pt.y + run->pt.y;
397 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
398 ME_DebugWrite(c->hDC, &pt, buf);
400 /* c->pt.x += p->member.run.nWidth; */
407 SetTextAlign(c->hDC, align);
410 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
413 HWND hWnd = editor->hWnd;
415 si.cbSize = sizeof(SCROLLINFO);
417 GetScrollInfo(hWnd, SB_VERT, &si);
418 si.nPos = editor->nScrollPosY -= cy;
419 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
422 if (abs(cy) > editor->sizeWindow.cy)
423 InvalidateRect(editor->hWnd, NULL, TRUE);
425 ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
429 void ME_UpdateScrollBar(ME_TextEditor *editor)
431 HWND hWnd = editor->hWnd;
433 int nOldLen = editor->nTotalLength;
434 BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
435 BOOL bUpdateScrollBars;
436 si.cbSize = sizeof(si);
437 si.fMask = SIF_POS | SIF_RANGE;
438 GetScrollInfo(hWnd, SB_VERT, &si);
439 bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
441 if (bScrollY != editor->bScrollY)
443 si.fMask = SIF_RANGE | SIF_PAGE;
445 si.nPage = editor->sizeWindow.cy;
447 si.nMax = editor->nTotalLength;
451 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
452 ME_MarkAllForWrapping(editor);
453 editor->bScrollY = bScrollY;
454 ME_WrapMarkedParagraphs(editor);
455 bUpdateScrollBars = TRUE;
457 if (bUpdateScrollBars) {
459 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
460 if (editor->nTotalLength > editor->sizeWindow.cy) {
461 si.nMax = editor->nTotalLength;
462 si.nPage = editor->sizeWindow.cy;
463 if (si.nPos > si.nMax-si.nPage) {
464 nScroll = (si.nMax-si.nPage)-si.nPos;
465 si.nPos = si.nMax-si.nPage;
473 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
474 editor->nScrollPosY = si.nPos;
475 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
477 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
481 int ME_GetYScrollPos(ME_TextEditor *editor)
483 return editor->nScrollPosY;
486 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
488 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
489 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
490 int y, yrel, yheight, yold;
491 HWND hWnd = editor->hWnd;
496 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
497 yheight = pRow->member.row.nHeight;
498 yold = ME_GetYScrollPos(editor);
501 editor->nScrollPosY = y;
502 SetScrollPos(hWnd, SB_VERT, y, TRUE);
505 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
508 } else if (yrel + yheight > editor->sizeWindow.cy) {
509 int newy = y+yheight-editor->sizeWindow.cy;
510 editor->nScrollPosY = newy;
511 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
514 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
522 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
524 /* TODO: Zoom images and objects */
528 if (denominator == 0)
530 if (1.0 / 64.0 > (float)numerator / (float)denominator
531 || (float)numerator / (float)denominator > 64.0)
535 editor->nZoomNumerator = numerator;
536 editor->nZoomDenominator = denominator;
538 ME_RewrapRepaint(editor);