richedit: Move init code after some sanity checks to avoid unneeded computations.
[wine/hacks.git] / dlls / riched20 / paint.c
blobf9fb4ec064447e2b5754ce564ec36e27609421ed
1 /*
2 * RichEdit - painting functions
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "editor.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, const RECT *rcUpdate) {
27 ME_DisplayItem *item;
28 ME_Context c;
29 int yoffset;
31 editor->nSequence++;
32 yoffset = ME_GetYScrollPos(editor);
33 ME_InitContext(&c, editor, hDC);
34 SetBkMode(hDC, TRANSPARENT);
35 ME_MoveCaret(editor);
36 item = editor->pBuffer->pFirst->next;
37 c.pt.y -= yoffset;
38 while(item != editor->pBuffer->pLast) {
39 int ye;
40 assert(item->type == diParagraph);
41 ye = c.pt.y + item->member.para.nHeight;
42 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
44 BOOL bPaint = (rcUpdate == NULL);
45 if (rcUpdate)
46 bPaint = c.pt.y<rcUpdate->bottom &&
47 c.pt.y+item->member.para.nHeight>rcUpdate->top;
48 if (bPaint)
50 ME_DrawParagraph(&c, item);
51 if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
52 item->member.para.nFlags &= ~MEPF_REPAINT;
55 c.pt.y = ye;
56 item = item->member.para.next_para;
58 if (c.pt.y<c.rcView.bottom) {
59 RECT rc;
60 int xs = c.rcView.left, xe = c.rcView.right;
61 int ys = c.pt.y, ye = c.rcView.bottom;
63 if (bOnlyNew)
65 int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
66 if (y1<y2)
67 ys = y1, ye = y2+1;
68 else
69 ys = ye;
72 if (rcUpdate && ys!=ye)
74 xs = rcUpdate->left, xe = rcUpdate->right;
75 if (rcUpdate->top > ys)
76 ys = rcUpdate->top;
77 if (rcUpdate->bottom < ye)
78 ye = rcUpdate->bottom;
81 if (ye>ys) {
82 rc.left = xs;
83 rc.top = ys;
84 rc.right = xe;
85 rc.bottom = ye;
86 FillRect(hDC, &rc, c.editor->hbrBackground);
88 if (ys == c.pt.y) /* don't overwrite the top bar */
89 ys++;
91 if (editor->nTotalLength != editor->nLastTotalLength)
92 ME_SendRequestResize(editor, FALSE);
93 editor->nLastTotalLength = editor->nTotalLength;
94 ME_DestroyContext(&c);
97 void ME_Repaint(ME_TextEditor *editor)
99 if (ME_WrapMarkedParagraphs(editor))
101 ME_UpdateScrollBar(editor);
102 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
104 ME_SendOldNotify(editor, EN_UPDATE);
105 UpdateWindow(editor->hWnd);
108 void ME_UpdateRepaint(ME_TextEditor *editor)
110 /* Should be called whenever the contents of the control have changed */
111 ME_Cursor *pCursor;
113 if (ME_WrapMarkedParagraphs(editor))
114 ME_UpdateScrollBar(editor);
116 /* Ensure that the cursor is visible */
117 pCursor = &editor->pCursors[0];
118 ME_EnsureVisible(editor, pCursor->pRun);
120 /* send EN_CHANGE if the event mask asks for it */
121 if(editor->nEventMask & ENM_CHANGE)
123 editor->nEventMask &= ~ENM_CHANGE;
124 ME_SendOldNotify(editor, EN_CHANGE);
125 editor->nEventMask |= ENM_CHANGE;
127 ME_Repaint(editor);
128 ME_SendSelChange(editor);
131 void
132 ME_RewrapRepaint(ME_TextEditor *editor)
134 /* RewrapRepaint should be called whenever the control has changed in
135 * looks, but not content. Like resizing. */
137 ME_MarkAllForWrapping(editor);
138 ME_WrapMarkedParagraphs(editor);
139 ME_UpdateScrollBar(editor);
141 ME_Repaint(editor);
144 int ME_twips2points(ME_Context *c, int x, int dpi)
146 if (c->editor->nZoomNumerator == 0)
147 return x * dpi / 1440;
148 else
149 return x * dpi * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
152 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
153 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
154 HDC hDC = c->hDC;
155 HGDIOBJ hOldFont;
156 COLORREF rgbOld;
157 int yOffset = 0, yTwipsOffset = 0;
158 SIZE sz;
159 COLORREF rgb;
161 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
162 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
163 rgb = RGB(0,0,255);
164 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
165 rgb = GetSysColor(COLOR_WINDOWTEXT);
166 else
167 rgb = s->fmt.crTextColor;
168 rgbOld = SetTextColor(hDC, rgb);
169 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
170 yTwipsOffset = s->fmt.yOffset;
172 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
173 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
174 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
176 if (yTwipsOffset)
177 yOffset = ME_twips2points(c, yTwipsOffset, GetDeviceCaps(hDC, LOGPIXELSY));
179 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
180 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
181 if (width) *width = sz.cx;
182 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
184 HPEN hPen;
185 switch (s->fmt.bUnderlineType)
187 case CFU_UNDERLINE:
188 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
189 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
190 hPen = CreatePen(PS_SOLID, 1, rgb);
191 break;
192 case CFU_UNDERLINEDOTTED:
193 hPen = CreatePen(PS_DOT, 1, rgb);
194 break;
195 default:
196 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
197 /* fall through */
198 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
199 case CFU_UNDERLINENONE:
200 hPen = NULL;
201 break;
203 if (hPen != NULL)
205 HPEN hOldPen = SelectObject(hDC, hPen);
206 /* FIXME: should use textmetrics info for Descent info */
207 MoveToEx(hDC, x, y - yOffset + 1, NULL);
208 LineTo(hDC, x + sz.cx, y - yOffset + 1);
209 SelectObject(hDC, hOldPen);
210 DeleteObject(hPen);
213 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
215 if (nSelFrom < 0) nSelFrom = 0;
216 if (nSelTo > nChars) nSelTo = nChars;
217 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
218 x += sz.cx;
219 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
221 /* Invert selection if not hidden by EM_HIDESELECTION */
222 if (c->editor->bHideSelection == FALSE)
223 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
225 SetTextColor(hDC, rgbOld);
226 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
229 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
230 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
231 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
232 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
233 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
234 SelectObject(hDC, hFont);
235 SetTextAlign(hDC, align);
236 SetTextColor(hDC, color);
239 static void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
240 ME_Paragraph *para, BOOL selected) {
241 SIZE sz;
242 int xs, ys, xe, ye, h, ym, width, eyes;
243 ME_GetGraphicsSize(c->editor, run, &sz);
244 xs = run->pt.x;
245 ys = y-sz.cy;
246 xe = xs+sz.cx;
247 ye = y;
248 h = ye-ys;
249 ym = ys+h/4;
250 width = sz.cx;
251 eyes = width/8;
252 /* draw a smiling face :) */
253 Ellipse(c->hDC, xs, ys, xe, ye);
254 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
255 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
256 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
257 LineTo(c->hDC, xs+width/8, ys+3*h/4);
258 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
259 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
260 if (selected)
262 /* descent is usually (always?) 0 for graphics */
263 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
267 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
269 ME_Run *run = &rundi->member.run;
270 ME_DisplayItem *start;
271 int runofs = run->nCharOfs+para->nCharOfs;
272 int nSelFrom, nSelTo;
273 const WCHAR wszSpace[] = {' ', 0};
275 if (run->nFlags & MERF_HIDDEN)
276 return;
278 start = ME_FindItemBack(rundi, diStartRow);
279 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
281 /* Draw selected end-of-paragraph mark */
282 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
283 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
284 c->pt.y + start->member.row.nYPos,
285 start->member.row.nHeight);
287 /* you can always comment it out if you need visible paragraph marks */
288 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
289 return;
291 if (run->nFlags & MERF_GRAPHICS)
292 ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
293 else
295 if (c->editor->cPasswordMask)
297 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
298 ME_DrawTextWithStyle(c, x, y,
299 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
300 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
301 ME_DestroyString(szMasked);
303 else
304 ME_DrawTextWithStyle(c, x, y,
305 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
306 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
310 static struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
311 /* none */ {0, 0, PS_SOLID, FALSE},
312 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
313 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
314 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
315 /* 3 */ {3, 1, PS_SOLID, FALSE},
316 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
317 /* 6 */ {6, 1, PS_SOLID, FALSE},
318 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
319 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
320 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
321 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
322 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
325 static COLORREF pen_colors[16] = {
326 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
327 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
328 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
329 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
330 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
331 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
332 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
333 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
336 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
338 int width;
340 if (editor->nZoomNumerator == 0)
342 width = border_details[idx].width_num + border_details[idx].width_den / 2;
343 width /= border_details[idx].width_den;
345 else
347 width = border_details[idx].width_num * editor->nZoomNumerator;
348 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
349 width /= border_details[idx].width_den * editor->nZoomDenominator;
351 return width;
354 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
356 int idx = (flags >> 8) & 0xF;
357 int width;
359 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
361 FIXME("Unsupported border value %d\n", idx);
362 return 0;
364 width = ME_GetBorderPenWidth(editor, idx);
365 if (border_details[idx].dble) width = width * 2 + 1;
366 return width;
369 int ME_GetParaLineSpace(ME_TextEditor* editor, ME_Paragraph* para, int dpi)
371 int sp = 0, ls = 0;
372 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
374 /* FIXME: how to compute simply the line space in ls ??? */
375 /* FIXME: does line spacing include the line itself ??? */
376 switch (para->pFmt->bLineSpacingRule)
378 case 0: sp = ls; break;
379 case 1: sp = (3 * ls) / 2; break;
380 case 2: sp = 2 * ls; break;
381 case 3: sp = para->pFmt->dyLineSpacing * dpi / 1440; if (sp < ls) sp = ls; break;
382 case 4: sp = para->pFmt->dyLineSpacing * dpi / 1440; break;
383 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
384 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
386 if (editor->nZoomNumerator == 0)
387 return sp;
388 else
389 return sp * editor->nZoomNumerator / editor->nZoomDenominator;
392 static int ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, int dpi)
394 int idx, border_width;
395 int ybefore, yafter;
396 RECT rc;
398 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return 0;
400 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
402 rc.left = c->rcView.left;
403 rc.right = c->rcView.right;
404 rc.top = y;
405 ybefore = ME_twips2points(c, para->pFmt->dySpaceBefore, dpi);
406 rc.bottom = y + ybefore;
407 FillRect(c->hDC, &rc, c->editor->hbrBackground);
409 else ybefore = 0;
410 if (para->pFmt->dwMask & PFM_SPACEAFTER)
412 rc.left = c->rcView.left;
413 rc.right = c->rcView.right;
414 rc.bottom = y + para->nHeight;
415 yafter = ME_twips2points(c, para->pFmt->dySpaceAfter, dpi);
416 rc.top = rc.bottom - yafter;
417 FillRect(c->hDC, &rc, c->editor->hbrBackground);
419 else yafter = 0;
421 border_width = 0;
422 idx = (para->pFmt->wBorders >> 8) & 0xF;
423 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
424 int pen_width;
425 COLORREF pencr;
426 HPEN pen = NULL, oldpen = NULL;
427 POINT pt;
429 if (para->pFmt->wBorders & 0x00B0)
430 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
431 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
433 if (para->pFmt->wBorders & 64) /* autocolor */
434 pencr = GetSysColor(COLOR_WINDOWTEXT);
435 else
436 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
438 pen_width = ME_GetBorderPenWidth(c->editor, idx);
439 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
440 oldpen = SelectObject(c->hDC, pen);
441 MoveToEx(c->hDC, 0, 0, &pt);
443 /* before & after spaces are not included in border */
444 if (para->pFmt->wBorders & 1)
446 MoveToEx(c->hDC, c->rcView.left, y + ybefore, NULL);
447 LineTo(c->hDC, c->rcView.left, y + para->nHeight - yafter);
448 if (border_details[idx].dble) {
449 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + ybefore + pen_width + 1, NULL);
450 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - yafter - pen_width - 1);
453 if (para->pFmt->wBorders & 2)
455 MoveToEx(c->hDC, c->rcView.right, y + ybefore, NULL);
456 LineTo(c->hDC, c->rcView.right, y + para->nHeight - yafter);
457 if (border_details[idx].dble) {
458 MoveToEx(c->hDC, c->rcView.right - pen_width - 1, y + ybefore + pen_width + 1, NULL);
459 LineTo(c->hDC, c->rcView.right - pen_width - 1, y + para->nHeight - yafter - pen_width - 1);
462 if (para->pFmt->wBorders & 4)
464 MoveToEx(c->hDC, c->rcView.left, y + ybefore, NULL);
465 LineTo(c->hDC, c->rcView.right, y + ybefore);
466 if (border_details[idx].dble) {
467 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + ybefore + pen_width + 1, NULL);
468 LineTo(c->hDC, c->rcView.right - pen_width - 1, y + ybefore + pen_width + 1);
471 if (para->pFmt->wBorders & 8)
473 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - yafter - 1, NULL);
474 LineTo(c->hDC, c->rcView.right, y + para->nHeight - yafter - 1);
475 if (border_details[idx].dble) {
476 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - yafter - 1 - pen_width - 1, NULL);
477 LineTo(c->hDC, c->rcView.right - pen_width - 1, y + para->nHeight - yafter - 1 - pen_width - 1);
481 MoveToEx(c->hDC, pt.x, pt.y, NULL);
482 SelectObject(c->hDC, oldpen);
483 DeleteObject(pen);
485 return ybefore +
486 ((para->pFmt->dwMask & PFM_BORDER) && (para->pFmt->wBorders & 4) ?
487 border_width : 0);
490 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
491 int align = SetTextAlign(c->hDC, TA_BASELINE);
492 int dpi = GetDeviceCaps(c->hDC, LOGPIXELSX);
493 ME_DisplayItem *p;
494 ME_Run *run;
495 ME_Paragraph *para = NULL;
496 RECT rc, rcPara;
497 int y = c->pt.y;
498 int height = 0, baseline = 0, no=0, pno = 0;
499 int xs = 0, xe = 0;
500 BOOL visible = FALSE;
501 int nMargWidth = 0;
503 c->pt.x = c->rcView.left;
504 rcPara.left = c->rcView.left;
505 rcPara.right = c->rcView.right;
506 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
507 switch(p->type) {
508 case diParagraph:
509 para = &p->member.para;
510 assert(para);
511 nMargWidth = ME_twips2points(c, para->pFmt->dxStartIndent, dpi);
512 if (pno != 0)
513 nMargWidth += ME_twips2points(c, para->pFmt->dxOffset, dpi);
514 xs = c->rcView.left+nMargWidth;
515 xe = c->rcView.right - ME_twips2points(c, para->pFmt->dxRightIndent, dpi);
516 y += ME_DrawParaDecoration(c, para, y, dpi);
517 break;
518 case diStartRow:
519 y += height;
520 rcPara.top = y;
521 rcPara.bottom = y+p->member.row.nHeight;
522 visible = RectVisible(c->hDC, &rcPara);
523 if (visible) {
524 /* left margin */
525 rc.left = c->rcView.left;
526 rc.right = c->rcView.left+nMargWidth;
527 rc.top = y;
528 rc.bottom = y+p->member.row.nHeight;
529 FillRect(c->hDC, &rc, c->editor->hbrBackground);
530 /* right margin */
531 rc.left = xe;
532 rc.right = c->rcView.right;
533 FillRect(c->hDC, &rc, c->editor->hbrBackground);
534 rc.left = c->rcView.left+nMargWidth;
535 rc.right = xe;
536 FillRect(c->hDC, &rc, c->editor->hbrBackground);
538 if (me_debug)
540 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
541 WCHAR buf[128];
542 POINT pt = c->pt;
543 wsprintfW(buf, wszRowDebug, no);
544 pt.y = 12+y;
545 ME_DebugWrite(c->hDC, &pt, buf);
548 height = p->member.row.nHeight;
549 baseline = p->member.row.nBaseline;
550 pno++;
551 break;
552 case diRun:
553 assert(para);
554 run = &p->member.run;
555 if (visible && me_debug) {
556 rc.left = c->rcView.left+run->pt.x;
557 rc.right = c->rcView.left+run->pt.x+run->nWidth;
558 rc.top = c->pt.y+run->pt.y;
559 rc.bottom = c->pt.y+run->pt.y+height;
560 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
561 if (run->nFlags & MERF_SKIPPED)
562 DrawFocusRect(c->hDC, &rc);
563 else
564 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
566 if (visible)
567 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
568 if (me_debug)
570 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
571 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
572 WCHAR buf[2560];
573 POINT pt;
574 pt.x = run->pt.x;
575 pt.y = c->pt.y + run->pt.y;
576 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
577 ME_DebugWrite(c->hDC, &pt, buf);
579 /* c->pt.x += p->member.run.nWidth; */
580 break;
581 default:
582 break;
584 no++;
586 SetTextAlign(c->hDC, align);
589 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
591 ME_Scroll(editor, absY, 1);
594 void ME_ScrollUp(ME_TextEditor *editor, int cy)
596 ME_Scroll(editor, cy, 2);
599 void ME_ScrollDown(ME_TextEditor *editor, int cy)
601 ME_Scroll(editor, cy, 3);
604 void ME_Scroll(ME_TextEditor *editor, int value, int type)
606 SCROLLINFO si;
607 int nOrigPos, nNewPos, nActualScroll;
609 nOrigPos = ME_GetYScrollPos(editor);
611 si.cbSize = sizeof(SCROLLINFO);
612 si.fMask = SIF_POS;
614 switch (type)
616 case 1:
617 /*Scroll absolutly*/
618 si.nPos = value;
619 break;
620 case 2:
621 /* Scroll up - towards the beginning of the document */
622 si.nPos = nOrigPos - value;
623 break;
624 case 3:
625 /* Scroll down - towards the end of the document */
626 si.nPos = nOrigPos + value;
627 break;
628 default:
629 FIXME("ME_Scroll called incorrectly\n");
630 si.nPos = 0;
633 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
634 nActualScroll = nOrigPos - nNewPos;
635 if (editor->bRedraw)
637 if (abs(nActualScroll) > editor->sizeWindow.cy)
638 InvalidateRect(editor->hWnd, NULL, TRUE);
639 else
640 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
641 ME_Repaint(editor);
644 ME_UpdateScrollBar(editor);
648 void ME_UpdateScrollBar(ME_TextEditor *editor)
650 /* Note that this is the only funciton that should ever call SetScrolLInfo
651 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
652 * be used at all. */
654 HWND hWnd;
655 SCROLLINFO si;
656 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
658 if (ME_WrapMarkedParagraphs(editor))
659 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
661 hWnd = editor->hWnd;
662 si.cbSize = sizeof(si);
663 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
664 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
666 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
668 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
669 ME_MarkAllForWrapping(editor);
670 ME_WrapMarkedParagraphs(editor);
673 si.fMask = SIF_PAGE | SIF_RANGE;
674 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
675 si.fMask |= SIF_DISABLENOSCROLL;
677 si.nMin = 0;
678 si.nMax = editor->nTotalLength;
680 si.nPage = editor->sizeWindow.cy;
682 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
683 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
686 int ME_GetYScrollPos(ME_TextEditor *editor)
688 SCROLLINFO si;
689 si.cbSize = sizeof(si);
690 si.fMask = SIF_POS;
691 GetScrollInfo(editor->hWnd, SB_VERT, &si);
692 return si.nPos;
695 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
696 { /* Returns true if the scrollbar is visible */
697 SCROLLBARINFO sbi;
698 sbi.cbSize = sizeof(sbi);
699 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
700 return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
703 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
705 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
706 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
707 int y, yrel, yheight, yold;
709 assert(pRow);
710 assert(pPara);
712 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
713 yheight = pRow->member.row.nHeight;
714 yold = ME_GetYScrollPos(editor);
715 yrel = y - yold;
717 if (y < yold)
718 ME_ScrollAbs(editor,y);
719 else if (yrel + yheight > editor->sizeWindow.cy)
720 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
724 void
725 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
727 RECT rc;
728 int x, y, height;
729 ME_Cursor tmp;
731 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
732 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
734 rc.left = 0;
735 rc.top = y;
736 rc.bottom = y + height;
737 rc.right = editor->rcFormat.right;
738 InvalidateRect(editor->hWnd, &rc, FALSE);
742 void
743 ME_InvalidateSelection(ME_TextEditor *editor)
745 ME_DisplayItem *para1, *para2;
746 int nStart, nEnd;
747 int len = ME_GetTextLength(editor);
749 ME_GetSelection(editor, &nStart, &nEnd);
750 /* if both old and new selection are 0-char (= caret only), then
751 there's no (inverted) area to be repainted, neither old nor new */
752 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
753 return;
754 ME_WrapMarkedParagraphs(editor);
755 ME_GetSelectionParas(editor, &para1, &para2);
756 assert(para1->type == diParagraph);
757 assert(para2->type == diParagraph);
758 /* last selection markers aren't always updated, which means
759 they can point past the end of the document */
760 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
761 ME_MarkForPainting(editor,
762 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
763 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
764 } else {
765 /* if the start part of selection is being expanded or contracted... */
766 if (nStart < editor->nLastSelStart) {
767 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
768 } else
769 if (nStart > editor->nLastSelStart) {
770 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
773 /* if the end part of selection is being contracted or expanded... */
774 if (nEnd < editor->nLastSelEnd) {
775 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
776 } else
777 if (nEnd > editor->nLastSelEnd) {
778 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
782 ME_InvalidateMarkedParagraphs(editor);
783 /* remember the last invalidated position */
784 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
785 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
786 assert(editor->pLastSelStartPara->type == diParagraph);
787 assert(editor->pLastSelEndPara->type == diParagraph);
790 void
791 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
793 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
797 BOOL
798 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
800 /* TODO: Zoom images and objects */
802 if (numerator != 0)
804 if (denominator == 0)
805 return FALSE;
806 if (1.0 / 64.0 > (float)numerator / (float)denominator
807 || (float)numerator / (float)denominator > 64.0)
808 return FALSE;
811 editor->nZoomNumerator = numerator;
812 editor->nZoomDenominator = denominator;
814 ME_RewrapRepaint(editor);
815 return TRUE;