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
;
82 hbr
= CreateSolidBrush(ME_GetBackColor(c
.editor
));
87 FillRect(hDC
, &rc
, hbr
);
90 if (ys
== c
.pt
.y
) /* don't overwrite the top bar */
93 editor
->nLastTotalLength
= editor
->nTotalLength
;
94 ME_DestroyContext(&c
);
97 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 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 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];
145 ME_DisplayItem
*pRun
= NULL
;
148 int nCharOfs
= ME_CharOfsFromRunOfs(editor
, pCursor
->pRun
, pCursor
->nOffset
);
150 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pRun
, &nOffset
);
151 assert(pRun
== pCursor
->pRun
);
152 assert(nOffset
== pCursor
->nOffset
);
153 ME_MarkSelectionForRepaint(editor
);
154 if (ME_WrapMarkedParagraphs(editor
)) {
155 ME_UpdateScrollBar(editor
);
157 hDC
= GetDC(editor
->hWnd
);
158 ME_HideCaret(editor
);
159 ME_PaintContent(editor
, hDC
, TRUE
, NULL
);
160 ReleaseDC(editor
->hWnd
, hDC
);
161 ME_ShowCaret(editor
);
162 ME_EnsureVisible(editor
, pCursor
->pRun
);
165 void ME_UpdateRepaint(ME_TextEditor
*editor
)
168 InvalidateRect(editor->hWnd, NULL, TRUE);
170 ME_SendOldNotify(editor
, EN_CHANGE
);
172 ME_SendOldNotify(editor
, EN_UPDATE
);
173 ME_SendSelChange(editor
);
176 void ME_DrawTextWithStyle(ME_Context
*c
, int x
, int y
, LPCWSTR szText
, int nChars
,
177 ME_Style
*s
, int *width
, int nSelFrom
, int nSelTo
, int ymin
, int cy
) {
180 COLORREF rgbOld
, rgbBack
;
181 hOldFont
= ME_SelectStyleFont(c
->editor
, hDC
, s
);
182 rgbBack
= ME_GetBackColor(c
->editor
);
183 if ((s
->fmt
.dwMask
& CFM_COLOR
) && (s
->fmt
.dwEffects
& CFE_AUTOCOLOR
))
184 rgbOld
= SetTextColor(hDC
, GetSysColor(COLOR_WINDOWTEXT
));
186 rgbOld
= SetTextColor(hDC
, s
->fmt
.crTextColor
);
187 ExtTextOutW(hDC
, x
, y
, 0, NULL
, szText
, nChars
, NULL
);
190 GetTextExtentPoint32W(hDC
, szText
, nChars
, &sz
);
193 if (nSelFrom
< nChars
&& nSelTo
>= 0 && nSelFrom
<nSelTo
)
196 if (nSelFrom
< 0) nSelFrom
= 0;
197 if (nSelTo
> nChars
) nSelTo
= nChars
;
198 GetTextExtentPoint32W(hDC
, szText
, nSelFrom
, &sz
);
200 GetTextExtentPoint32W(hDC
, szText
+nSelFrom
, nSelTo
-nSelFrom
, &sz
);
201 PatBlt(hDC
, x
, ymin
, sz
.cx
, cy
, DSTINVERT
);
203 SetTextColor(hDC
, rgbOld
);
204 ME_UnselectStyleFont(c
->editor
, hDC
, s
, hOldFont
);
207 void ME_DebugWrite(HDC hDC
, POINT
*pt
, WCHAR
*szText
) {
208 int align
= SetTextAlign(hDC
, TA_LEFT
|TA_TOP
);
209 HGDIOBJ hFont
= SelectObject(hDC
, GetStockObject(DEFAULT_GUI_FONT
));
210 COLORREF color
= SetTextColor(hDC
, RGB(128,128,128));
211 TextOutW(hDC
, pt
->x
, pt
->y
, szText
, lstrlenW(szText
));
212 SelectObject(hDC
, hFont
);
213 SetTextAlign(hDC
, align
);
214 SetTextColor(hDC
, color
);
217 void ME_DrawGraphics(ME_Context
*c
, int x
, int y
, ME_Run
*run
,
218 ME_Paragraph
*para
, BOOL selected
) {
220 int xs
, ys
, xe
, ye
, h
, ym
, width
, eyes
;
221 ME_GetGraphicsSize(c
->editor
, run
, &sz
);
230 /* draw a smiling face :) */
231 Ellipse(c
->hDC
, xs
, ys
, xe
, ye
);
232 Ellipse(c
->hDC
, xs
+width
/8, ym
, x
+width
/8+eyes
, ym
+eyes
);
233 Ellipse(c
->hDC
, xs
+7*width
/8-eyes
, ym
, xs
+7*width
/8, ym
+eyes
);
234 MoveToEx(c
->hDC
, xs
+width
/8, ys
+3*h
/4-eyes
, NULL
);
235 LineTo(c
->hDC
, xs
+width
/8, ys
+3*h
/4);
236 LineTo(c
->hDC
, xs
+7*width
/8, ys
+3*h
/4);
237 LineTo(c
->hDC
, xs
+7*width
/8, ys
+3*h
/4-eyes
);
240 /* descent is usually (always?) 0 for graphics */
241 PatBlt(c
->hDC
, x
, y
-run
->nAscent
, sz
.cx
, run
->nAscent
+run
->nDescent
, DSTINVERT
);
245 void ME_DrawRun(ME_Context
*c
, int x
, int y
, ME_DisplayItem
*rundi
, ME_Paragraph
*para
) {
246 ME_Run
*run
= &rundi
->member
.run
;
247 int runofs
= run
->nCharOfs
+para
->nCharOfs
;
249 /* you can always comment it out if you need visible paragraph marks */
250 if (run
->nFlags
& (MERF_ENDPARA
|MERF_TAB
))
252 if (run
->nFlags
& MERF_GRAPHICS
) {
254 ME_GetSelection(c
->editor
, &blfrom
, &blto
);
255 ME_DrawGraphics(c
, x
, y
, run
, para
, (runofs
>= blfrom
) && (runofs
< blto
));
259 ME_DisplayItem
*start
= ME_FindItemBack(rundi
, diStartRow
);
260 ME_GetSelection(c
->editor
, &blfrom
, &blto
);
262 ME_DrawTextWithStyle(c
, x
, y
,
263 run
->strText
->szData
, ME_StrVLen(run
->strText
), run
->style
, NULL
,
264 blfrom
-runofs
, blto
-runofs
, c
->pt
.y
+start
->member
.row
.nYPos
, start
->member
.row
.nHeight
);
268 COLORREF
ME_GetBackColor(ME_TextEditor
*editor
)
270 /* Looks like I was seriously confused
271 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
273 if (editor
->rgbBackColor
== -1)
274 return GetSysColor(COLOR_WINDOW
);
276 return editor
->rgbBackColor
;
279 void ME_DrawParagraph(ME_Context
*c
, ME_DisplayItem
*paragraph
) {
280 int align
= SetTextAlign(c
->hDC
, TA_BASELINE
);
283 ME_Paragraph
*para
= NULL
;
286 int height
= 0, baseline
= 0, no
=0, pno
= 0;
291 c
->pt
.x
= c
->rcView
.left
;
292 rcPara
.left
= c
->rcView
.left
;
293 rcPara
.right
= c
->rcView
.right
;
294 for (p
= paragraph
; p
!=paragraph
->member
.para
.next_para
; p
= p
->next
) {
297 para
= &p
->member
.para
;
301 nMargWidth
= (pno
==0?para
->nFirstMargin
:para
->nLeftMargin
);
302 xs
= c
->rcView
.left
+nMargWidth
;
303 xe
= c
->rcView
.right
-para
->nRightMargin
;
306 rcPara
.bottom
= y
+p
->member
.row
.nHeight
;
307 visible
= RectVisible(c
->hDC
, &rcPara
);
310 hbr
= CreateSolidBrush(ME_GetBackColor(c
->editor
));
312 rc
.left
= c
->rcView
.left
;
313 rc
.right
= c
->rcView
.left
+nMargWidth
;
315 rc
.bottom
= y
+p
->member
.row
.nHeight
;
316 FillRect(c
->hDC
, &rc
, hbr
/* c->hbrMargin */);
319 rc
.right
= c
->rcView
.right
;
320 FillRect(c
->hDC
, &rc
, hbr
/* c->hbrMargin */);
321 rc
.left
= c
->rcView
.left
+nMargWidth
;
323 FillRect(c
->hDC
, &rc
, hbr
);
328 const WCHAR wszRowDebug
[] = {'r','o','w','[','%','d',']',0};
331 wsprintfW(buf
, wszRowDebug
, no
);
333 ME_DebugWrite(c
->hDC
, &pt
, buf
);
336 height
= p
->member
.row
.nHeight
;
337 baseline
= p
->member
.row
.nBaseline
;
342 run
= &p
->member
.run
;
343 if (visible
&& me_debug
) {
344 rc
.left
= c
->rcView
.left
+run
->pt
.x
;
345 rc
.right
= c
->rcView
.left
+run
->pt
.x
+run
->nWidth
;
346 rc
.top
= c
->pt
.y
+run
->pt
.y
;
347 rc
.bottom
= c
->pt
.y
+run
->pt
.y
+height
;
348 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
349 if (run
->nFlags
& MERF_SKIPPED
)
350 DrawFocusRect(c
->hDC
, &rc
);
352 FrameRect(c
->hDC
, &rc
, GetSysColorBrush(COLOR_GRAYTEXT
));
355 ME_DrawRun(c
, run
->pt
.x
, c
->pt
.y
+run
->pt
.y
+baseline
, p
, ¶graph
->member
.para
);
358 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
359 const WCHAR wszRunDebug
[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
363 pt
.y
= c
->pt
.y
+ run
->pt
.y
;
364 wsprintfW(buf
, wszRunDebug
, no
, p
->member
.run
.nFlags
, p
->member
.run
.strText
->szData
);
365 ME_DebugWrite(c
->hDC
, &pt
, buf
);
367 /* c->pt.x += p->member.run.nWidth; */
374 SetTextAlign(c
->hDC
, align
);
377 void ME_Scroll(ME_TextEditor
*editor
, int cx
, int cy
)
380 HWND hWnd
= editor
->hWnd
;
382 si
.cbSize
= sizeof(SCROLLINFO
);
384 GetScrollInfo(hWnd
, SB_VERT
, &si
);
385 si
.nPos
= editor
->nScrollPosY
-= cy
;
386 SetScrollInfo(hWnd
, SB_VERT
, &si
, TRUE
);
387 if (abs(cy
) > editor
->sizeWindow
.cy
)
388 InvalidateRect(editor
->hWnd
, NULL
, TRUE
);
390 ScrollWindowEx(hWnd
, cx
, cy
, NULL
, NULL
, NULL
, NULL
, SW_ERASE
|SW_INVALIDATE
);
393 void ME_UpdateScrollBar(ME_TextEditor
*editor
)
395 HWND hWnd
= editor
->hWnd
;
397 int nOldLen
= editor
->nTotalLength
;
398 BOOL bScrollY
= (editor
->nTotalLength
> editor
->sizeWindow
.cy
);
399 BOOL bUpdateScrollBars
;
400 si
.cbSize
= sizeof(si
);
401 si
.fMask
= SIF_POS
| SIF_RANGE
;
402 GetScrollInfo(hWnd
, SB_VERT
, &si
);
403 bUpdateScrollBars
= (bScrollY
|| editor
->bScrollY
)&& ((si
.nMax
!= nOldLen
) || (si
.nPage
!= editor
->sizeWindow
.cy
));
405 if (bScrollY
!= editor
->bScrollY
)
407 si
.fMask
= SIF_RANGE
| SIF_PAGE
;
409 si
.nPage
= editor
->sizeWindow
.cy
;
411 si
.nMax
= editor
->nTotalLength
;
415 SetScrollInfo(hWnd
, SB_VERT
, &si
, FALSE
);
416 ME_MarkAllForWrapping(editor
);
417 editor
->bScrollY
= bScrollY
;
418 ME_WrapMarkedParagraphs(editor
);
419 bUpdateScrollBars
= TRUE
;
421 if (bUpdateScrollBars
) {
423 si
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
424 if (editor
->nTotalLength
> editor
->sizeWindow
.cy
) {
425 si
.nMax
= editor
->nTotalLength
;
426 si
.nPage
= editor
->sizeWindow
.cy
;
427 if (si
.nPos
> si
.nMax
-si
.nPage
) {
428 nScroll
= (si
.nMax
-si
.nPage
)-si
.nPos
;
429 si
.nPos
= si
.nMax
-si
.nPage
;
437 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
, nScroll
);
438 editor
->nScrollPosY
= si
.nPos
;
439 SetScrollInfo(hWnd
, SB_VERT
, &si
, TRUE
);
441 ScrollWindow(hWnd
, 0, -nScroll
, NULL
, NULL
);
445 int ME_GetYScrollPos(ME_TextEditor
*editor
)
447 return editor
->nScrollPosY
;
450 void ME_EnsureVisible(ME_TextEditor
*editor
, ME_DisplayItem
*pRun
)
452 ME_DisplayItem
*pRow
= ME_FindItemBack(pRun
, diStartRow
);
453 ME_DisplayItem
*pPara
= ME_FindItemBack(pRun
, diParagraph
);
454 int y
, yrel
, yheight
, yold
;
455 HWND hWnd
= editor
->hWnd
;
460 y
= pPara
->member
.para
.nYPos
+pRow
->member
.row
.nYPos
;
461 yheight
= pRow
->member
.row
.nHeight
;
462 yold
= ME_GetYScrollPos(editor
);
465 editor
->nScrollPosY
= y
;
466 SetScrollPos(hWnd
, SB_VERT
, y
, TRUE
);
467 ScrollWindow(hWnd
, 0, -yrel
, NULL
, NULL
);
469 } else if (yrel
+ yheight
> editor
->sizeWindow
.cy
) {
470 int newy
= y
+yheight
-editor
->sizeWindow
.cy
;
471 editor
->nScrollPosY
= newy
;
472 SetScrollPos(hWnd
, SB_VERT
, newy
, TRUE
);
473 ScrollWindow(hWnd
, 0, -(newy
-yold
), NULL
, NULL
);