push 7f8c39dca3a5819e8ef115eebf7abed537de3a22
[wine/hacks.git] / dlls / riched20 / paint.c
blob95befbc72f0a42caf793873e2daccae859b5f1a6
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 && ye>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);
88 if (editor->nTotalLength != editor->nLastTotalLength)
89 ME_SendRequestResize(editor, FALSE);
90 editor->nLastTotalLength = editor->nTotalLength;
91 ME_DestroyContext(&c, NULL);
94 void ME_Repaint(ME_TextEditor *editor)
96 if (ME_WrapMarkedParagraphs(editor))
98 ME_UpdateScrollBar(editor);
99 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
101 ME_SendOldNotify(editor, EN_UPDATE);
102 UpdateWindow(editor->hWnd);
105 void ME_UpdateRepaint(ME_TextEditor *editor)
107 /* Should be called whenever the contents of the control have changed */
108 ME_Cursor *pCursor;
109 BOOL wrappedParagraphs;
111 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
112 if (!editor->bRedraw) return;
113 if (wrappedParagraphs)
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 if (editor->bRedraw)
140 ME_WrapMarkedParagraphs(editor);
141 ME_UpdateScrollBar(editor);
142 ME_Repaint(editor);
146 int ME_twips2pointsX(ME_Context *c, int x)
148 if (c->editor->nZoomNumerator == 0)
149 return x * c->dpi.cx / 1440;
150 else
151 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
154 int ME_twips2pointsY(ME_Context *c, int y)
156 if (c->editor->nZoomNumerator == 0)
157 return y * c->dpi.cy / 1440;
158 else
159 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
162 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
163 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
164 HDC hDC = c->hDC;
165 HGDIOBJ hOldFont;
166 COLORREF rgbOld;
167 int yOffset = 0, yTwipsOffset = 0;
168 SIZE sz;
169 COLORREF rgb;
171 hOldFont = ME_SelectStyleFont(c, s);
172 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
173 rgb = RGB(0,0,255);
174 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
175 rgb = GetSysColor(COLOR_WINDOWTEXT);
176 else
177 rgb = s->fmt.crTextColor;
178 rgbOld = SetTextColor(hDC, rgb);
179 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
180 yTwipsOffset = s->fmt.yOffset;
182 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
183 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
184 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
186 if (yTwipsOffset)
187 yOffset = ME_twips2pointsY(c, yTwipsOffset);
188 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
189 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
190 if (width) *width = sz.cx;
191 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
193 HPEN hPen;
194 switch (s->fmt.bUnderlineType)
196 case CFU_UNDERLINE:
197 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
198 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
199 hPen = CreatePen(PS_SOLID, 1, rgb);
200 break;
201 case CFU_UNDERLINEDOTTED:
202 hPen = CreatePen(PS_DOT, 1, rgb);
203 break;
204 default:
205 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
206 /* fall through */
207 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
208 case CFU_UNDERLINENONE:
209 hPen = NULL;
210 break;
212 if (hPen != NULL)
214 HPEN hOldPen = SelectObject(hDC, hPen);
215 /* FIXME: should use textmetrics info for Descent info */
216 MoveToEx(hDC, x, y - yOffset + 1, NULL);
217 LineTo(hDC, x + sz.cx, y - yOffset + 1);
218 SelectObject(hDC, hOldPen);
219 DeleteObject(hPen);
222 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
224 if (nSelFrom < 0) nSelFrom = 0;
225 if (nSelTo > nChars) nSelTo = nChars;
226 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
227 x += sz.cx;
228 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
230 /* Invert selection if not hidden by EM_HIDESELECTION */
231 if (c->editor->bHideSelection == FALSE)
232 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
234 SetTextColor(hDC, rgbOld);
235 ME_UnselectStyleFont(c, s, hOldFont);
238 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
239 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
240 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
241 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
242 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
243 SelectObject(hDC, hFont);
244 SetTextAlign(hDC, align);
245 SetTextColor(hDC, color);
248 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
250 ME_Run *run = &rundi->member.run;
251 ME_DisplayItem *start;
252 int runofs = run->nCharOfs+para->nCharOfs;
253 int nSelFrom, nSelTo;
254 const WCHAR wszSpace[] = {' ', 0};
256 if (run->nFlags & MERF_HIDDEN)
257 return;
259 start = ME_FindItemBack(rundi, diStartRow);
260 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
262 /* Draw selected end-of-paragraph mark */
263 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
264 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
265 c->pt.y + start->member.row.nYPos,
266 start->member.row.nHeight);
268 /* you can always comment it out if you need visible paragraph marks */
269 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
270 return;
272 if (run->nFlags & MERF_GRAPHICS)
273 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
274 else
276 if (c->editor->cPasswordMask)
278 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
279 ME_DrawTextWithStyle(c, x, y,
280 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
281 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
282 ME_DestroyString(szMasked);
284 else
285 ME_DrawTextWithStyle(c, x, y,
286 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
287 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
291 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
292 /* none */ {0, 1, PS_SOLID, FALSE},
293 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
294 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
295 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
296 /* 3 */ {3, 1, PS_SOLID, FALSE},
297 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
298 /* 6 */ {6, 1, PS_SOLID, FALSE},
299 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
300 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
301 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
302 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
303 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
306 static const COLORREF pen_colors[16] = {
307 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
308 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
309 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
310 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
311 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
312 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
313 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
314 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
317 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
319 int width;
321 if (editor->nZoomNumerator == 0)
323 width = border_details[idx].width_num + border_details[idx].width_den / 2;
324 width /= border_details[idx].width_den;
326 else
328 width = border_details[idx].width_num * editor->nZoomNumerator;
329 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
330 width /= border_details[idx].width_den * editor->nZoomDenominator;
332 return width;
335 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
337 int idx = (flags >> 8) & 0xF;
338 int width;
340 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
342 FIXME("Unsupported border value %d\n", idx);
343 return 0;
345 width = ME_GetBorderPenWidth(editor, idx);
346 if (border_details[idx].dble) width = width * 2 + 1;
347 return width;
350 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
352 int sp = 0, ls = 0;
353 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
355 /* FIXME: how to compute simply the line space in ls ??? */
356 /* FIXME: does line spacing include the line itself ??? */
357 switch (para->pFmt->bLineSpacingRule)
359 case 0: sp = ls; break;
360 case 1: sp = (3 * ls) / 2; break;
361 case 2: sp = 2 * ls; break;
362 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
363 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
364 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
365 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
367 if (c->editor->nZoomNumerator == 0)
368 return sp;
369 else
370 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
373 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
375 int idx, border_width, top_border, bottom_border;
376 RECT rc;
378 SetRectEmpty(bounds);
379 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
381 border_width = top_border = bottom_border = 0;
382 idx = (para->pFmt->wBorders >> 8) & 0xF;
383 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
385 if (para->pFmt->wBorders & 0x00B0)
386 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
387 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
388 if (para->pFmt->wBorders & 4) top_border = border_width;
389 if (para->pFmt->wBorders & 8) bottom_border = border_width;
392 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
394 rc.left = c->rcView.left;
395 rc.right = c->rcView.right;
396 rc.top = y;
397 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
398 rc.bottom = y + bounds->top + top_border;
399 FillRect(c->hDC, &rc, c->editor->hbrBackground);
402 if (para->pFmt->dwMask & PFM_SPACEAFTER)
404 rc.left = c->rcView.left;
405 rc.right = c->rcView.right;
406 rc.bottom = y + para->nHeight;
407 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
408 rc.top = rc.bottom - bounds->bottom - bottom_border;
409 FillRect(c->hDC, &rc, c->editor->hbrBackground);
412 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
413 int pen_width;
414 COLORREF pencr;
415 HPEN pen = NULL, oldpen = NULL;
416 POINT pt;
418 if (para->pFmt->wBorders & 64) /* autocolor */
419 pencr = GetSysColor(COLOR_WINDOWTEXT);
420 else
421 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
423 pen_width = ME_GetBorderPenWidth(c->editor, idx);
424 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
425 oldpen = SelectObject(c->hDC, pen);
426 MoveToEx(c->hDC, 0, 0, &pt);
428 /* before & after spaces are not included in border */
430 /* helper to draw the double lines in case of corner */
431 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
433 if (para->pFmt->wBorders & 1)
435 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
436 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
437 if (border_details[idx].dble) {
438 rc.left = c->rcView.left + 1;
439 rc.right = rc.left + border_width;
440 rc.top = y + bounds->top;
441 rc.bottom = y + para->nHeight - bounds->bottom;
442 FillRect(c->hDC, &rc, c->editor->hbrBackground);
443 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
444 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
446 bounds->left += border_width;
448 if (para->pFmt->wBorders & 2)
450 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
451 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
452 if (border_details[idx].dble) {
453 rc.left = c->rcView.right - pen_width - 1;
454 rc.right = c->rcView.right - 1;
455 rc.top = y + bounds->top;
456 rc.bottom = y + para->nHeight - bounds->bottom;
457 FillRect(c->hDC, &rc, c->editor->hbrBackground);
458 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
459 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
461 bounds->right += border_width;
463 if (para->pFmt->wBorders & 4)
465 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
466 LineTo(c->hDC, c->rcView.right, y + bounds->top);
467 if (border_details[idx].dble) {
468 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
469 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
471 bounds->top += border_width;
473 if (para->pFmt->wBorders & 8)
475 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
476 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
477 if (border_details[idx].dble) {
478 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
479 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
481 bounds->bottom += border_width;
483 #undef DD
485 MoveToEx(c->hDC, pt.x, pt.y, NULL);
486 SelectObject(c->hDC, oldpen);
487 DeleteObject(pen);
491 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
492 int align = SetTextAlign(c->hDC, TA_BASELINE);
493 ME_DisplayItem *p;
494 ME_Run *run;
495 ME_Paragraph *para = NULL;
496 RECT rc, rcPara, bounds;
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;
502 c->pt.x = c->rcView.left;
503 rcPara.left = c->rcView.left;
504 rcPara.right = c->rcView.right;
505 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
506 switch(p->type) {
507 case diParagraph:
508 para = &p->member.para;
509 assert(para);
510 pno = 0;
511 xs = c->rcView.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
512 xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
513 ME_DrawParaDecoration(c, para, y, &bounds);
514 y += bounds.top;
515 break;
516 case diStartRow:
517 y += height;
518 rcPara.top = y;
519 rcPara.bottom = y+p->member.row.nHeight;
520 visible = RectVisible(c->hDC, &rcPara);
521 if (visible) {
522 /* left margin */
523 rc.left = c->rcView.left + bounds.left;
524 rc.right = xs;
525 rc.top = y;
526 rc.bottom = y+p->member.row.nHeight;
527 FillRect(c->hDC, &rc, c->editor->hbrBackground);
528 /* right margin */
529 rc.left = xe;
530 rc.right = c->rcView.right - bounds.right;
531 FillRect(c->hDC, &rc, c->editor->hbrBackground);
532 rc.left = xs;
533 rc.right = xe;
534 FillRect(c->hDC, &rc, c->editor->hbrBackground);
536 if (me_debug)
538 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
539 WCHAR buf[128];
540 POINT pt = c->pt;
541 wsprintfW(buf, wszRowDebug, no);
542 pt.y = 12+y;
543 ME_DebugWrite(c->hDC, &pt, buf);
546 height = p->member.row.nHeight;
547 baseline = p->member.row.nBaseline;
548 if (!pno++)
549 xe += ME_twips2pointsX(c, para->pFmt->dxOffset);
550 break;
551 case diRun:
552 assert(para);
553 run = &p->member.run;
554 if (visible && me_debug) {
555 rc.left = c->rcView.left+run->pt.x;
556 rc.right = c->rcView.left+run->pt.x+run->nWidth;
557 rc.top = c->pt.y+run->pt.y;
558 rc.bottom = c->pt.y+run->pt.y+height;
559 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
560 if (run->nFlags & MERF_SKIPPED)
561 DrawFocusRect(c->hDC, &rc);
562 else
563 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
565 if (visible)
566 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
567 if (me_debug)
569 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
570 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
571 WCHAR buf[2560];
572 POINT pt;
573 pt.x = run->pt.x;
574 pt.y = c->pt.y + run->pt.y;
575 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
576 ME_DebugWrite(c->hDC, &pt, buf);
578 /* c->pt.x += p->member.run.nWidth; */
579 break;
580 default:
581 break;
583 no++;
585 SetTextAlign(c->hDC, align);
588 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
590 ME_Scroll(editor, absY, 1);
593 void ME_ScrollUp(ME_TextEditor *editor, int cy)
595 ME_Scroll(editor, cy, 2);
598 void ME_ScrollDown(ME_TextEditor *editor, int cy)
600 ME_Scroll(editor, cy, 3);
603 void ME_Scroll(ME_TextEditor *editor, int value, int type)
605 SCROLLINFO si;
606 int nOrigPos, nNewPos, nActualScroll;
608 nOrigPos = ME_GetYScrollPos(editor);
610 si.cbSize = sizeof(SCROLLINFO);
611 si.fMask = SIF_POS;
613 switch (type)
615 case 1:
616 /*Scroll absolutely*/
617 si.nPos = value;
618 break;
619 case 2:
620 /* Scroll up - towards the beginning of the document */
621 si.nPos = nOrigPos - value;
622 break;
623 case 3:
624 /* Scroll down - towards the end of the document */
625 si.nPos = nOrigPos + value;
626 break;
627 default:
628 FIXME("ME_Scroll called incorrectly\n");
629 si.nPos = 0;
632 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
633 nActualScroll = nOrigPos - nNewPos;
634 if (editor->bRedraw)
636 if (abs(nActualScroll) > editor->sizeWindow.cy)
637 InvalidateRect(editor->hWnd, NULL, TRUE);
638 else
639 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
640 ME_Repaint(editor);
643 ME_UpdateScrollBar(editor);
647 void ME_UpdateScrollBar(ME_TextEditor *editor)
649 /* Note that this is the only function that should ever call SetScrolLInfo
650 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
651 * be used at all. */
653 HWND hWnd;
654 SCROLLINFO si;
655 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
657 if (ME_WrapMarkedParagraphs(editor))
658 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
660 hWnd = editor->hWnd;
661 si.cbSize = sizeof(si);
662 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
663 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
665 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
667 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
668 ME_MarkAllForWrapping(editor);
669 ME_WrapMarkedParagraphs(editor);
672 si.fMask = SIF_PAGE | SIF_RANGE;
673 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
674 si.fMask |= SIF_DISABLENOSCROLL;
676 si.nMin = 0;
677 si.nMax = editor->nTotalLength;
679 si.nPage = editor->sizeWindow.cy;
681 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
682 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
685 int ME_GetYScrollPos(ME_TextEditor *editor)
687 SCROLLINFO si;
688 si.cbSize = sizeof(si);
689 si.fMask = SIF_POS;
690 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
693 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
694 { /* Returns true if the scrollbar is visible */
695 SCROLLBARINFO sbi;
696 sbi.cbSize = sizeof(sbi);
697 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
698 return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
701 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
703 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
704 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
705 int y, yrel, yheight, yold;
707 assert(pRow);
708 assert(pPara);
710 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
711 yheight = pRow->member.row.nHeight;
712 yold = ME_GetYScrollPos(editor);
713 yrel = y - yold;
715 if (y < yold)
716 ME_ScrollAbs(editor,y);
717 else if (yrel + yheight > editor->sizeWindow.cy)
718 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
722 void
723 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
725 RECT rc;
726 int x, y, height;
727 ME_Cursor tmp;
729 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
730 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
732 rc.left = 0;
733 rc.top = y;
734 rc.bottom = y + height;
735 rc.right = editor->rcFormat.right;
736 InvalidateRect(editor->hWnd, &rc, FALSE);
740 void
741 ME_InvalidateSelection(ME_TextEditor *editor)
743 ME_DisplayItem *para1, *para2;
744 int nStart, nEnd;
745 int len = ME_GetTextLength(editor);
747 ME_GetSelection(editor, &nStart, &nEnd);
748 /* if both old and new selection are 0-char (= caret only), then
749 there's no (inverted) area to be repainted, neither old nor new */
750 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
751 return;
752 ME_WrapMarkedParagraphs(editor);
753 ME_GetSelectionParas(editor, &para1, &para2);
754 assert(para1->type == diParagraph);
755 assert(para2->type == diParagraph);
756 /* last selection markers aren't always updated, which means
757 they can point past the end of the document */
758 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
759 ME_MarkForPainting(editor,
760 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
761 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
762 } else {
763 /* if the start part of selection is being expanded or contracted... */
764 if (nStart < editor->nLastSelStart) {
765 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
766 } else
767 if (nStart > editor->nLastSelStart) {
768 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
771 /* if the end part of selection is being contracted or expanded... */
772 if (nEnd < editor->nLastSelEnd) {
773 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
774 } else
775 if (nEnd > editor->nLastSelEnd) {
776 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
780 ME_InvalidateMarkedParagraphs(editor);
781 /* remember the last invalidated position */
782 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
783 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
784 assert(editor->pLastSelStartPara->type == diParagraph);
785 assert(editor->pLastSelEndPara->type == diParagraph);
788 void
789 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
791 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
795 BOOL
796 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
798 /* TODO: Zoom images and objects */
800 if (numerator != 0)
802 if (denominator == 0)
803 return FALSE;
804 if (1.0 / 64.0 > (float)numerator / (float)denominator
805 || (float)numerator / (float)denominator > 64.0)
806 return FALSE;
809 editor->nZoomNumerator = numerator;
810 editor->nZoomDenominator = denominator;
812 ME_RewrapRepaint(editor);
813 return TRUE;