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];
145 ME_MarkSelectionForRepaint(editor
);
146 if (ME_WrapMarkedParagraphs(editor
)) {
147 ME_UpdateScrollBar(editor
);
151 ME_EnsureVisible(editor
, pCursor
->pRun
);
155 void ME_UpdateRepaint(ME_TextEditor
*editor
)
158 InvalidateRect(editor->hWnd, NULL, TRUE);
160 ME_SendOldNotify(editor
, EN_CHANGE
);
162 ME_SendOldNotify(editor
, EN_UPDATE
);
163 ME_SendSelChange(editor
);
168 ME_RewrapRepaint(ME_TextEditor
*editor
)
170 ME_MarkAllForWrapping(editor
);
171 ME_WrapMarkedParagraphs(editor
);
172 ME_UpdateScrollBar(editor
);
177 static void ME_DrawTextWithStyle(ME_Context
*c
, int x
, int y
, LPCWSTR szText
, int nChars
,
178 ME_Style
*s
, int *width
, int nSelFrom
, int nSelTo
, int ymin
, int cy
) {
181 COLORREF rgbOld
, rgbBack
;
182 int yOffset
= 0, yTwipsOffset
= 0;
183 hOldFont
= ME_SelectStyleFont(c
->editor
, hDC
, s
);
184 rgbBack
= ME_GetBackColor(c
->editor
);
185 if ((s
->fmt
.dwMask
& CFM_COLOR
) && (s
->fmt
.dwEffects
& CFE_AUTOCOLOR
))
186 rgbOld
= SetTextColor(hDC
, GetSysColor(COLOR_WINDOWTEXT
));
188 rgbOld
= SetTextColor(hDC
, s
->fmt
.crTextColor
);
189 if ((s
->fmt
.dwMask
& s
->fmt
.dwEffects
) & CFM_OFFSET
) {
190 yTwipsOffset
= s
->fmt
.yOffset
;
192 if ((s
->fmt
.dwMask
& s
->fmt
.dwEffects
) & (CFM_SUPERSCRIPT
| CFM_SUBSCRIPT
)) {
193 if (s
->fmt
.dwEffects
& CFE_SUPERSCRIPT
) yTwipsOffset
= s
->fmt
.yHeight
/3;
194 if (s
->fmt
.dwEffects
& CFE_SUBSCRIPT
) yTwipsOffset
= -s
->fmt
.yHeight
/12;
201 if (c
->editor
->nZoomNumerator
)
203 numerator
= c
->editor
->nZoomNumerator
;
204 denominator
= c
->editor
->nZoomDenominator
;
206 yOffset
= yTwipsOffset
* GetDeviceCaps(hDC
, LOGPIXELSY
) * numerator
/ denominator
/ 1440;
208 ExtTextOutW(hDC
, x
, y
-yOffset
, 0, NULL
, szText
, nChars
, NULL
);
211 GetTextExtentPoint32W(hDC
, szText
, nChars
, &sz
);
214 if (nSelFrom
< nChars
&& nSelTo
>= 0 && nSelFrom
<nSelTo
)
217 if (nSelFrom
< 0) nSelFrom
= 0;
218 if (nSelTo
> nChars
) nSelTo
= nChars
;
219 GetTextExtentPoint32W(hDC
, szText
, nSelFrom
, &sz
);
221 GetTextExtentPoint32W(hDC
, szText
+nSelFrom
, nSelTo
-nSelFrom
, &sz
);
222 PatBlt(hDC
, x
, ymin
, sz
.cx
, cy
, DSTINVERT
);
224 SetTextColor(hDC
, rgbOld
);
225 ME_UnselectStyleFont(c
->editor
, hDC
, s
, hOldFont
);
228 static void ME_DebugWrite(HDC hDC
, POINT
*pt
, WCHAR
*szText
) {
229 int align
= SetTextAlign(hDC
, TA_LEFT
|TA_TOP
);
230 HGDIOBJ hFont
= SelectObject(hDC
, GetStockObject(DEFAULT_GUI_FONT
));
231 COLORREF color
= SetTextColor(hDC
, RGB(128,128,128));
232 TextOutW(hDC
, pt
->x
, pt
->y
, szText
, lstrlenW(szText
));
233 SelectObject(hDC
, hFont
);
234 SetTextAlign(hDC
, align
);
235 SetTextColor(hDC
, color
);
238 void ME_DrawGraphics(ME_Context
*c
, int x
, int y
, ME_Run
*run
,
239 ME_Paragraph
*para
, BOOL selected
) {
241 int xs
, ys
, xe
, ye
, h
, ym
, width
, eyes
;
242 ME_GetGraphicsSize(c
->editor
, run
, &sz
);
251 /* draw a smiling face :) */
252 Ellipse(c
->hDC
, xs
, ys
, xe
, ye
);
253 Ellipse(c
->hDC
, xs
+width
/8, ym
, x
+width
/8+eyes
, ym
+eyes
);
254 Ellipse(c
->hDC
, xs
+7*width
/8-eyes
, ym
, xs
+7*width
/8, ym
+eyes
);
255 MoveToEx(c
->hDC
, xs
+width
/8, ys
+3*h
/4-eyes
, NULL
);
256 LineTo(c
->hDC
, xs
+width
/8, ys
+3*h
/4);
257 LineTo(c
->hDC
, xs
+7*width
/8, ys
+3*h
/4);
258 LineTo(c
->hDC
, xs
+7*width
/8, ys
+3*h
/4-eyes
);
261 /* descent is usually (always?) 0 for graphics */
262 PatBlt(c
->hDC
, x
, y
-run
->nAscent
, sz
.cx
, run
->nAscent
+run
->nDescent
, DSTINVERT
);
266 static void ME_DrawRun(ME_Context
*c
, int x
, int y
, ME_DisplayItem
*rundi
, ME_Paragraph
*para
) {
267 ME_Run
*run
= &rundi
->member
.run
;
268 int runofs
= run
->nCharOfs
+para
->nCharOfs
;
270 /* you can always comment it out if you need visible paragraph marks */
271 if (run
->nFlags
& (MERF_ENDPARA
|MERF_TAB
))
273 if (run
->nFlags
& MERF_GRAPHICS
) {
275 ME_GetSelection(c
->editor
, &blfrom
, &blto
);
276 ME_DrawGraphics(c
, x
, y
, run
, para
, (runofs
>= blfrom
) && (runofs
< blto
));
280 ME_DisplayItem
*start
= ME_FindItemBack(rundi
, diStartRow
);
281 ME_GetSelection(c
->editor
, &blfrom
, &blto
);
283 ME_DrawTextWithStyle(c
, x
, y
,
284 run
->strText
->szData
, ME_StrVLen(run
->strText
), run
->style
, NULL
,
285 blfrom
-runofs
, blto
-runofs
, c
->pt
.y
+start
->member
.row
.nYPos
, start
->member
.row
.nHeight
);
289 COLORREF
ME_GetBackColor(ME_TextEditor
*editor
)
291 /* Looks like I was seriously confused
292 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
294 if (editor
->rgbBackColor
== -1)
295 return GetSysColor(COLOR_WINDOW
);
297 return editor
->rgbBackColor
;
300 void ME_DrawParagraph(ME_Context
*c
, ME_DisplayItem
*paragraph
) {
301 int align
= SetTextAlign(c
->hDC
, TA_BASELINE
);
304 ME_Paragraph
*para
= NULL
;
307 int height
= 0, baseline
= 0, no
=0, pno
= 0;
312 c
->pt
.x
= c
->rcView
.left
;
313 rcPara
.left
= c
->rcView
.left
;
314 rcPara
.right
= c
->rcView
.right
;
315 for (p
= paragraph
; p
!=paragraph
->member
.para
.next_para
; p
= p
->next
) {
318 para
= &p
->member
.para
;
322 nMargWidth
= (pno
==0?para
->nFirstMargin
:para
->nLeftMargin
);
323 xs
= c
->rcView
.left
+nMargWidth
;
324 xe
= c
->rcView
.right
-para
->nRightMargin
;
327 rcPara
.bottom
= y
+p
->member
.row
.nHeight
;
328 visible
= RectVisible(c
->hDC
, &rcPara
);
331 hbr
= CreateSolidBrush(ME_GetBackColor(c
->editor
));
333 rc
.left
= c
->rcView
.left
;
334 rc
.right
= c
->rcView
.left
+nMargWidth
;
336 rc
.bottom
= y
+p
->member
.row
.nHeight
;
337 FillRect(c
->hDC
, &rc
, hbr
/* c->hbrMargin */);
340 rc
.right
= c
->rcView
.right
;
341 FillRect(c
->hDC
, &rc
, hbr
/* c->hbrMargin */);
342 rc
.left
= c
->rcView
.left
+nMargWidth
;
344 FillRect(c
->hDC
, &rc
, hbr
);
349 const WCHAR wszRowDebug
[] = {'r','o','w','[','%','d',']',0};
352 wsprintfW(buf
, wszRowDebug
, no
);
354 ME_DebugWrite(c
->hDC
, &pt
, buf
);
357 height
= p
->member
.row
.nHeight
;
358 baseline
= p
->member
.row
.nBaseline
;
363 run
= &p
->member
.run
;
364 if (visible
&& me_debug
) {
365 rc
.left
= c
->rcView
.left
+run
->pt
.x
;
366 rc
.right
= c
->rcView
.left
+run
->pt
.x
+run
->nWidth
;
367 rc
.top
= c
->pt
.y
+run
->pt
.y
;
368 rc
.bottom
= c
->pt
.y
+run
->pt
.y
+height
;
369 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc
.left
, rc
.top
, rc
.right
, rc
.bottom
);
370 if (run
->nFlags
& MERF_SKIPPED
)
371 DrawFocusRect(c
->hDC
, &rc
);
373 FrameRect(c
->hDC
, &rc
, GetSysColorBrush(COLOR_GRAYTEXT
));
376 ME_DrawRun(c
, run
->pt
.x
, c
->pt
.y
+run
->pt
.y
+baseline
, p
, ¶graph
->member
.para
);
379 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
380 const WCHAR wszRunDebug
[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
384 pt
.y
= c
->pt
.y
+ run
->pt
.y
;
385 wsprintfW(buf
, wszRunDebug
, no
, p
->member
.run
.nFlags
, p
->member
.run
.strText
->szData
);
386 ME_DebugWrite(c
->hDC
, &pt
, buf
);
388 /* c->pt.x += p->member.run.nWidth; */
395 SetTextAlign(c
->hDC
, align
);
398 void ME_Scroll(ME_TextEditor
*editor
, int cx
, int cy
)
401 HWND hWnd
= editor
->hWnd
;
403 si
.cbSize
= sizeof(SCROLLINFO
);
405 GetScrollInfo(hWnd
, SB_VERT
, &si
);
406 si
.nPos
= editor
->nScrollPosY
-= cy
;
407 SetScrollInfo(hWnd
, SB_VERT
, &si
, TRUE
);
410 if (abs(cy
) > editor
->sizeWindow
.cy
)
411 InvalidateRect(editor
->hWnd
, NULL
, TRUE
);
413 ScrollWindowEx(hWnd
, cx
, cy
, NULL
, NULL
, NULL
, NULL
, SW_ERASE
|SW_INVALIDATE
);
417 void ME_UpdateScrollBar(ME_TextEditor
*editor
)
419 HWND hWnd
= editor
->hWnd
;
421 int nOldLen
= editor
->nTotalLength
;
422 BOOL bScrollY
= (editor
->nTotalLength
> editor
->sizeWindow
.cy
);
423 BOOL bUpdateScrollBars
;
424 si
.cbSize
= sizeof(si
);
425 si
.fMask
= SIF_POS
| SIF_RANGE
;
426 GetScrollInfo(hWnd
, SB_VERT
, &si
);
427 bUpdateScrollBars
= (bScrollY
|| editor
->bScrollY
)&& ((si
.nMax
!= nOldLen
) || (si
.nPage
!= editor
->sizeWindow
.cy
));
429 if (bScrollY
!= editor
->bScrollY
)
431 si
.fMask
= SIF_RANGE
| SIF_PAGE
;
433 si
.nPage
= editor
->sizeWindow
.cy
;
435 si
.nMax
= editor
->nTotalLength
;
439 SetScrollInfo(hWnd
, SB_VERT
, &si
, FALSE
);
440 ME_MarkAllForWrapping(editor
);
441 editor
->bScrollY
= bScrollY
;
442 ME_WrapMarkedParagraphs(editor
);
443 bUpdateScrollBars
= TRUE
;
445 if (bUpdateScrollBars
) {
447 si
.fMask
= SIF_PAGE
| SIF_RANGE
| SIF_POS
;
448 if (editor
->nTotalLength
> editor
->sizeWindow
.cy
) {
449 si
.nMax
= editor
->nTotalLength
;
450 si
.nPage
= editor
->sizeWindow
.cy
;
451 if (si
.nPos
> si
.nMax
-si
.nPage
) {
452 nScroll
= (si
.nMax
-si
.nPage
)-si
.nPos
;
453 si
.nPos
= si
.nMax
-si
.nPage
;
461 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si
.nMin
, si
.nMax
, si
.nPage
, si
.nPos
, nScroll
);
462 editor
->nScrollPosY
= si
.nPos
;
463 SetScrollInfo(hWnd
, SB_VERT
, &si
, TRUE
);
465 ScrollWindow(hWnd
, 0, -nScroll
, NULL
, NULL
);
469 int ME_GetYScrollPos(ME_TextEditor
*editor
)
471 return editor
->nScrollPosY
;
474 void ME_EnsureVisible(ME_TextEditor
*editor
, ME_DisplayItem
*pRun
)
476 ME_DisplayItem
*pRow
= ME_FindItemBack(pRun
, diStartRow
);
477 ME_DisplayItem
*pPara
= ME_FindItemBack(pRun
, diParagraph
);
478 int y
, yrel
, yheight
, yold
;
479 HWND hWnd
= editor
->hWnd
;
484 y
= pPara
->member
.para
.nYPos
+pRow
->member
.row
.nYPos
;
485 yheight
= pRow
->member
.row
.nHeight
;
486 yold
= ME_GetYScrollPos(editor
);
489 editor
->nScrollPosY
= y
;
490 SetScrollPos(hWnd
, SB_VERT
, y
, TRUE
);
493 ScrollWindow(hWnd
, 0, -yrel
, NULL
, NULL
);
496 } else if (yrel
+ yheight
> editor
->sizeWindow
.cy
) {
497 int newy
= y
+yheight
-editor
->sizeWindow
.cy
;
498 editor
->nScrollPosY
= newy
;
499 SetScrollPos(hWnd
, SB_VERT
, newy
, TRUE
);
502 ScrollWindow(hWnd
, 0, -(newy
-yold
), NULL
, NULL
);
510 ME_InvalidateFromOfs(ME_TextEditor
*editor
, int nCharOfs
)
516 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &tmp
.pRun
, &tmp
.nOffset
);
517 ME_GetCursorCoordinates(editor
, &tmp
, &x
, &y
, &height
);
521 rc
.bottom
= y
+ height
;
522 rc
.right
= editor
->rcFormat
.right
;
523 InvalidateRect(editor
->hWnd
, &rc
, FALSE
);
528 ME_InvalidateSelection(ME_TextEditor
*editor
)
530 if (ME_IsSelection(editor
) || editor
->nLastSelStart
!= editor
->nLastSelEnd
)
534 int last_x
, last_y
, last_height
;
535 int last_x2
, last_y2
, last_height2
;
539 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[1], &x
, &y
, &height
);
540 ME_GetCursorCoordinates(editor
, &editor
->pCursors
[0], &x2
, &y2
, &height2
);
541 ME_RunOfsFromCharOfs(editor
, editor
->nLastSelStart
, &tmp
.pRun
, &tmp
.nOffset
);
542 ME_GetCursorCoordinates(editor
, &tmp
, &last_x
, &last_y
, &last_height
);
543 ME_RunOfsFromCharOfs(editor
, editor
->nLastSelEnd
, &tmp
.pRun
, &tmp
.nOffset
);
544 ME_GetCursorCoordinates(editor
, &tmp
, &last_x2
, &last_y2
, &last_height2
);
548 rc
.top
= min(min(y
, last_y
), min(y2
, last_y2
));
549 rc
.right
= editor
->rcFormat
.right
;
550 rc
.bottom
= max(max(y
+ height
, last_y
+ last_height
),
551 max(y2
+ height2
, last_y2
+ last_height2
));
552 InvalidateRect(editor
->hWnd
, &rc
, FALSE
);
555 ME_GetSelection(editor
, &editor
->nLastSelStart
, &editor
->nLastSelEnd
);
560 ME_QueueInvalidateFromCursor(ME_TextEditor
*editor
, int nCursor
)
562 editor
->nInvalidOfs
= ME_GetCursorOfs(editor
, nCursor
);
567 ME_SetZoom(ME_TextEditor
*editor
, int numerator
, int denominator
)
569 /* TODO: Zoom images and objects */
573 if (denominator
== 0)
575 if (1.0 / 64.0 > (float)numerator
/ (float)denominator
576 || (float)numerator
/ (float)denominator
> 64.0)
580 editor
->nZoomNumerator
= numerator
;
581 editor
->nZoomDenominator
= denominator
;
583 ME_RewrapRepaint(editor
);