Assorted spelling fixes.
[wine/wine-kai.git] / dlls / riched20 / paint.c
blob41cedd628df8fb2e7570ef2306c414b3e5055b25
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 HBRUSH hbr;
82 hbr = CreateSolidBrush(ME_GetBackColor(c.editor));
83 rc.left = xs;
84 rc.top = ys;
85 rc.right = xe;
86 rc.bottom = ye;
87 FillRect(hDC, &rc, hbr);
88 DeleteObject(hbr);
90 if (ys == c.pt.y) /* don't overwrite the top bar */
91 ys++;
93 editor->nLastTotalLength = editor->nTotalLength;
94 ME_DestroyContext(&c);
97 static void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
98 ME_DisplayItem *p2, int nFlags)
100 ME_DisplayItem *p3;
101 if (p1 == p2)
103 p1->member.para.nFlags |= nFlags;
104 return;
106 if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
107 p3 = p1, p1 = p2, p2 = p3;
109 p1->member.para.nFlags |= nFlags;
110 do {
111 p1 = p1->member.para.next_para;
112 p1->member.para.nFlags |= nFlags;
113 } while (p1 != p2);
116 static void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
118 ME_Cursor c1, c2;
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 static 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;
146 int nOffset = -1;
147 HDC hDC;
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);
171 ME_Repaint(editor);
172 ME_SendOldNotify(editor, EN_UPDATE);
173 ME_SendSelChange(editor);
176 static 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) {
178 HDC hDC = c->hDC;
179 HGDIOBJ hOldFont;
180 COLORREF rgbOld, rgbBack;
181 int yOffset = 0, yTwipsOffset = 0;
182 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
183 rgbBack = ME_GetBackColor(c->editor);
184 if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
185 rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
186 else
187 rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
188 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
189 yTwipsOffset = s->fmt.yOffset;
191 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
192 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
193 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
195 if (yTwipsOffset)
196 yOffset = yTwipsOffset*GetDeviceCaps(hDC, LOGPIXELSY)/1440;
197 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
198 if (width) {
199 SIZE sz;
200 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
201 *width = sz.cx;
203 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
205 SIZE sz;
206 if (nSelFrom < 0) nSelFrom = 0;
207 if (nSelTo > nChars) nSelTo = nChars;
208 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
209 x += sz.cx;
210 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
211 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
213 SetTextColor(hDC, rgbOld);
214 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
217 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
218 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
219 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
220 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
221 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
222 SelectObject(hDC, hFont);
223 SetTextAlign(hDC, align);
224 SetTextColor(hDC, color);
227 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
228 ME_Paragraph *para, BOOL selected) {
229 SIZE sz;
230 int xs, ys, xe, ye, h, ym, width, eyes;
231 ME_GetGraphicsSize(c->editor, run, &sz);
232 xs = run->pt.x;
233 ys = y-sz.cy;
234 xe = xs+sz.cx;
235 ye = y;
236 h = ye-ys;
237 ym = ys+h/4;
238 width = sz.cx;
239 eyes = width/8;
240 /* draw a smiling face :) */
241 Ellipse(c->hDC, xs, ys, xe, ye);
242 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
243 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
244 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
245 LineTo(c->hDC, xs+width/8, ys+3*h/4);
246 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
247 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
248 if (selected)
250 /* descent is usually (always?) 0 for graphics */
251 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
255 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
256 ME_Run *run = &rundi->member.run;
257 int runofs = run->nCharOfs+para->nCharOfs;
259 /* you can always comment it out if you need visible paragraph marks */
260 if (run->nFlags & (MERF_ENDPARA|MERF_TAB))
261 return;
262 if (run->nFlags & MERF_GRAPHICS) {
263 int blfrom, blto;
264 ME_GetSelection(c->editor, &blfrom, &blto);
265 ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
266 } else
268 int blfrom, blto;
269 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
270 ME_GetSelection(c->editor, &blfrom, &blto);
272 ME_DrawTextWithStyle(c, x, y,
273 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
274 blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
278 COLORREF ME_GetBackColor(ME_TextEditor *editor)
280 /* Looks like I was seriously confused
281 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
283 if (editor->rgbBackColor == -1)
284 return GetSysColor(COLOR_WINDOW);
285 else
286 return editor->rgbBackColor;
289 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
290 int align = SetTextAlign(c->hDC, TA_BASELINE);
291 ME_DisplayItem *p;
292 ME_Run *run;
293 ME_Paragraph *para = NULL;
294 RECT rc, rcPara;
295 int y = c->pt.y;
296 int height = 0, baseline = 0, no=0, pno = 0;
297 int xs, xe;
298 int visible = 0;
299 int nMargWidth = 0;
301 c->pt.x = c->rcView.left;
302 rcPara.left = c->rcView.left;
303 rcPara.right = c->rcView.right;
304 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
305 switch(p->type) {
306 case diParagraph:
307 para = &p->member.para;
308 break;
309 case diStartRow:
310 assert(para);
311 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
312 xs = c->rcView.left+nMargWidth;
313 xe = c->rcView.right-para->nRightMargin;
314 y += height;
315 rcPara.top = y;
316 rcPara.bottom = y+p->member.row.nHeight;
317 visible = RectVisible(c->hDC, &rcPara);
318 if (visible) {
319 HBRUSH hbr;
320 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
321 /* left margin */
322 rc.left = c->rcView.left;
323 rc.right = c->rcView.left+nMargWidth;
324 rc.top = y;
325 rc.bottom = y+p->member.row.nHeight;
326 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
327 /* right margin */
328 rc.left = xe;
329 rc.right = c->rcView.right;
330 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
331 rc.left = c->rcView.left+nMargWidth;
332 rc.right = xe;
333 FillRect(c->hDC, &rc, hbr);
334 DeleteObject(hbr);
336 if (me_debug)
338 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
339 WCHAR buf[128];
340 POINT pt = c->pt;
341 wsprintfW(buf, wszRowDebug, no);
342 pt.y = 12+y;
343 ME_DebugWrite(c->hDC, &pt, buf);
346 height = p->member.row.nHeight;
347 baseline = p->member.row.nBaseline;
348 pno++;
349 break;
350 case diRun:
351 assert(para);
352 run = &p->member.run;
353 if (visible && me_debug) {
354 rc.left = c->rcView.left+run->pt.x;
355 rc.right = c->rcView.left+run->pt.x+run->nWidth;
356 rc.top = c->pt.y+run->pt.y;
357 rc.bottom = c->pt.y+run->pt.y+height;
358 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
359 if (run->nFlags & MERF_SKIPPED)
360 DrawFocusRect(c->hDC, &rc);
361 else
362 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
364 if (visible)
365 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
366 if (me_debug)
368 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
369 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
370 WCHAR buf[2560];
371 POINT pt;
372 pt.x = run->pt.x;
373 pt.y = c->pt.y + run->pt.y;
374 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
375 ME_DebugWrite(c->hDC, &pt, buf);
377 /* c->pt.x += p->member.run.nWidth; */
378 break;
379 default:
380 break;
382 no++;
384 SetTextAlign(c->hDC, align);
387 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
389 SCROLLINFO si;
390 HWND hWnd = editor->hWnd;
392 si.cbSize = sizeof(SCROLLINFO);
393 si.fMask = SIF_POS;
394 GetScrollInfo(hWnd, SB_VERT, &si);
395 si.nPos = editor->nScrollPosY -= cy;
396 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
397 if (abs(cy) > editor->sizeWindow.cy)
398 InvalidateRect(editor->hWnd, NULL, TRUE);
399 else
400 ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
403 void ME_UpdateScrollBar(ME_TextEditor *editor)
405 HWND hWnd = editor->hWnd;
406 SCROLLINFO si;
407 int nOldLen = editor->nTotalLength;
408 BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
409 BOOL bUpdateScrollBars;
410 si.cbSize = sizeof(si);
411 si.fMask = SIF_POS | SIF_RANGE;
412 GetScrollInfo(hWnd, SB_VERT, &si);
413 bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
415 if (bScrollY != editor->bScrollY)
417 si.fMask = SIF_RANGE | SIF_PAGE;
418 si.nMin = 0;
419 si.nPage = editor->sizeWindow.cy;
420 if (bScrollY) {
421 si.nMax = editor->nTotalLength;
422 } else {
423 si.nMax = 0;
425 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
426 ME_MarkAllForWrapping(editor);
427 editor->bScrollY = bScrollY;
428 ME_WrapMarkedParagraphs(editor);
429 bUpdateScrollBars = TRUE;
431 if (bUpdateScrollBars) {
432 int nScroll = 0;
433 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
434 if (editor->nTotalLength > editor->sizeWindow.cy) {
435 si.nMax = editor->nTotalLength;
436 si.nPage = editor->sizeWindow.cy;
437 if (si.nPos > si.nMax-si.nPage) {
438 nScroll = (si.nMax-si.nPage)-si.nPos;
439 si.nPos = si.nMax-si.nPage;
442 else {
443 si.nMax = 0;
444 si.nPage = 0;
445 si.nPos = 0;
447 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
448 editor->nScrollPosY = si.nPos;
449 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
450 if (nScroll)
451 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
455 int ME_GetYScrollPos(ME_TextEditor *editor)
457 return editor->nScrollPosY;
460 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
462 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
463 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
464 int y, yrel, yheight, yold;
465 HWND hWnd = editor->hWnd;
467 assert(pRow);
468 assert(pPara);
470 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
471 yheight = pRow->member.row.nHeight;
472 yold = ME_GetYScrollPos(editor);
473 yrel = y - yold;
474 if (yrel < 0) {
475 editor->nScrollPosY = y;
476 SetScrollPos(hWnd, SB_VERT, y, TRUE);
477 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
478 UpdateWindow(hWnd);
479 } else if (yrel + yheight > editor->sizeWindow.cy) {
480 int newy = y+yheight-editor->sizeWindow.cy;
481 editor->nScrollPosY = newy;
482 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
483 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
484 UpdateWindow(hWnd);