riched20: Removed a useless check in painting code.
[wine/hacks.git] / dlls / riched20 / paint.c
blob6e4cfb2f595dbf2be8df75bec909d91bab89e3ab
1 /*
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
21 #include "editor.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, RECT *rcUpdate) {
26 ME_DisplayItem *item;
27 ME_Context c;
28 int yoffset;
30 editor->nSequence++;
31 yoffset = ME_GetYScrollPos(editor);
32 ME_InitContext(&c, editor, hDC);
33 SetBkMode(hDC, TRANSPARENT);
34 ME_MoveCaret(editor);
35 item = editor->pBuffer->pFirst->next;
36 c.pt.y -= yoffset;
37 while(item != editor->pBuffer->pLast) {
38 int ye;
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);
44 if (rcUpdate)
45 bPaint = c.pt.y<rcUpdate->bottom &&
46 c.pt.y+item->member.para.nHeight>rcUpdate->top;
47 if (bPaint)
49 ME_DrawParagraph(&c, item);
50 if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
51 item->member.para.nFlags &= ~MEPF_REPAINT;
54 c.pt.y = ye;
55 item = item->member.para.next_para;
57 if (c.pt.y<c.rcView.bottom) {
58 RECT rc;
59 int xs = c.rcView.left, xe = c.rcView.right;
60 int ys = c.pt.y, ye = c.rcView.bottom;
62 if (bOnlyNew)
64 int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
65 if (y1<y2)
66 ys = y1, ye = y2+1;
67 else
68 ys = ye;
71 if (rcUpdate && ys!=ye)
73 xs = rcUpdate->left, xe = rcUpdate->right;
74 if (rcUpdate->top > ys)
75 ys = rcUpdate->top;
76 if (rcUpdate->bottom < ye)
77 ye = rcUpdate->bottom;
80 if (ye>ys) {
81 rc.left = xs;
82 rc.top = ys;
83 rc.right = xe;
84 rc.bottom = ye;
85 FillRect(hDC, &rc, c.editor->hbrBackground);
87 if (ys == c.pt.y) /* don't overwrite the top bar */
88 ys++;
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)
99 ME_DisplayItem *p3;
100 if (p1 == p2)
102 p1->member.para.nFlags |= nFlags;
103 return;
105 if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
106 p3 = p1, p1 = p2, p2 = p3;
108 p1->member.para.nFlags |= nFlags;
109 do {
110 p1 = p1->member.para.next_para;
111 p1->member.para.nFlags |= nFlags;
112 } while (p1 != p2);
115 static void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
117 ME_Cursor c1, c2;
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);
149 if (editor->bRedraw)
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);
161 ME_Repaint(editor);
162 ME_SendOldNotify(editor, EN_UPDATE);
163 ME_SendSelChange(editor);
167 void
168 ME_RewrapRepaint(ME_TextEditor *editor)
170 ME_MarkAllForWrapping(editor);
171 ME_WrapMarkedParagraphs(editor);
172 ME_UpdateScrollBar(editor);
173 ME_Repaint(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) {
179 HDC hDC = c->hDC;
180 HGDIOBJ hOldFont;
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));
187 else
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;
196 if (yTwipsOffset)
198 int numerator = 1;
199 int denominator = 1;
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);
209 if (width) {
210 SIZE sz;
211 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
212 *width = sz.cx;
214 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
216 SIZE sz;
217 if (nSelFrom < 0) nSelFrom = 0;
218 if (nSelTo > nChars) nSelTo = nChars;
219 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
220 x += sz.cx;
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) {
240 SIZE sz;
241 int xs, ys, xe, ye, h, ym, width, eyes;
242 ME_GetGraphicsSize(c->editor, run, &sz);
243 xs = run->pt.x;
244 ys = y-sz.cy;
245 xe = xs+sz.cx;
246 ye = y;
247 h = ye-ys;
248 ym = ys+h/4;
249 width = sz.cx;
250 eyes = width/8;
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);
259 if (selected)
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))
272 return;
273 if (run->nFlags & MERF_GRAPHICS) {
274 int blfrom, blto;
275 ME_GetSelection(c->editor, &blfrom, &blto);
276 ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
277 } else
279 int blfrom, 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);
296 else
297 return editor->rgbBackColor;
300 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
301 int align = SetTextAlign(c->hDC, TA_BASELINE);
302 ME_DisplayItem *p;
303 ME_Run *run;
304 ME_Paragraph *para = NULL;
305 RECT rc, rcPara;
306 int y = c->pt.y;
307 int height = 0, baseline = 0, no=0, pno = 0;
308 int xs, xe;
309 int visible = 0;
310 int nMargWidth = 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) {
316 switch(p->type) {
317 case diParagraph:
318 para = &p->member.para;
319 break;
320 case diStartRow:
321 assert(para);
322 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
323 xs = c->rcView.left+nMargWidth;
324 xe = c->rcView.right-para->nRightMargin;
325 y += height;
326 rcPara.top = y;
327 rcPara.bottom = y+p->member.row.nHeight;
328 visible = RectVisible(c->hDC, &rcPara);
329 if (visible) {
330 HBRUSH hbr;
331 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
332 /* left margin */
333 rc.left = c->rcView.left;
334 rc.right = c->rcView.left+nMargWidth;
335 rc.top = y;
336 rc.bottom = y+p->member.row.nHeight;
337 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
338 /* right margin */
339 rc.left = xe;
340 rc.right = c->rcView.right;
341 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
342 rc.left = c->rcView.left+nMargWidth;
343 rc.right = xe;
344 FillRect(c->hDC, &rc, hbr);
345 DeleteObject(hbr);
347 if (me_debug)
349 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
350 WCHAR buf[128];
351 POINT pt = c->pt;
352 wsprintfW(buf, wszRowDebug, no);
353 pt.y = 12+y;
354 ME_DebugWrite(c->hDC, &pt, buf);
357 height = p->member.row.nHeight;
358 baseline = p->member.row.nBaseline;
359 pno++;
360 break;
361 case diRun:
362 assert(para);
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);
372 else
373 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
375 if (visible)
376 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
377 if (me_debug)
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};
381 WCHAR buf[2560];
382 POINT pt;
383 pt.x = run->pt.x;
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; */
389 break;
390 default:
391 break;
393 no++;
395 SetTextAlign(c->hDC, align);
398 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
400 SCROLLINFO si;
401 HWND hWnd = editor->hWnd;
403 si.cbSize = sizeof(SCROLLINFO);
404 si.fMask = SIF_POS;
405 GetScrollInfo(hWnd, SB_VERT, &si);
406 si.nPos = editor->nScrollPosY -= cy;
407 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
408 if (editor->bRedraw)
410 if (abs(cy) > editor->sizeWindow.cy)
411 InvalidateRect(editor->hWnd, NULL, TRUE);
412 else
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;
420 SCROLLINFO si;
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;
432 si.nMin = 0;
433 si.nPage = editor->sizeWindow.cy;
434 if (bScrollY) {
435 si.nMax = editor->nTotalLength;
436 } else {
437 si.nMax = 0;
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) {
446 int nScroll = 0;
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;
456 else {
457 si.nMax = 0;
458 si.nPage = 0;
459 si.nPos = 0;
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);
464 if (nScroll)
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;
481 assert(pRow);
482 assert(pPara);
484 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
485 yheight = pRow->member.row.nHeight;
486 yold = ME_GetYScrollPos(editor);
487 yrel = y - yold;
488 if (yrel < 0) {
489 editor->nScrollPosY = y;
490 SetScrollPos(hWnd, SB_VERT, y, TRUE);
491 if (editor->bRedraw)
493 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
494 UpdateWindow(hWnd);
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);
500 if (editor->bRedraw)
502 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
503 UpdateWindow(hWnd);
509 void
510 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
512 RECT rc;
513 int x, y, height;
514 ME_Cursor tmp;
516 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
517 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
519 rc.left = 0;
520 rc.top = y;
521 rc.bottom = y + height;
522 rc.right = editor->rcFormat.right;
523 InvalidateRect(editor->hWnd, &rc, FALSE);
527 void
528 ME_InvalidateSelection(ME_TextEditor *editor)
530 if (ME_IsSelection(editor) || editor->nLastSelStart != editor->nLastSelEnd)
532 int x, y, height;
533 int x2, y2, height2;
534 int last_x, last_y, last_height;
535 int last_x2, last_y2, last_height2;
536 RECT rc;
537 ME_Cursor tmp;
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);
546 rc.left = 0;
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);
559 void
560 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
562 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
566 BOOL
567 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
569 /* TODO: Zoom images and objects */
571 if (numerator != 0)
573 if (denominator == 0)
574 return FALSE;
575 if (1.0 / 64.0 > (float)numerator / (float)denominator
576 || (float)numerator / (float)denominator > 64.0)
577 return FALSE;
580 editor->nZoomNumerator = numerator;
581 editor->nZoomDenominator = denominator;
583 ME_RewrapRepaint(editor);
584 return TRUE;