push 585d938e6dafa7e4b71343fb5b928a9214e5518c
[wine/hacks.git] / dlls / riched20 / paint.c
bloba2bc7c522360ab701ea85ae37ee212894d6095a7
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 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
102 ME_SendOldNotify(editor, EN_UPDATE);
103 UpdateWindow(editor->hWnd);
106 void ME_UpdateRepaint(ME_TextEditor *editor)
108 /* Should be called whenever the contents of the control have changed */
109 ME_Cursor *pCursor;
110 BOOL wrappedParagraphs;
112 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
113 if (!editor->bRedraw) return;
114 if (wrappedParagraphs)
115 ME_UpdateScrollBar(editor);
117 /* Ensure that the cursor is visible */
118 pCursor = &editor->pCursors[0];
119 ME_EnsureVisible(editor, pCursor->pRun);
121 /* send EN_CHANGE if the event mask asks for it */
122 if(editor->nEventMask & ENM_CHANGE)
124 editor->nEventMask &= ~ENM_CHANGE;
125 ME_SendOldNotify(editor, EN_CHANGE);
126 editor->nEventMask |= ENM_CHANGE;
128 ME_Repaint(editor);
129 ME_SendSelChange(editor);
132 void
133 ME_RewrapRepaint(ME_TextEditor *editor)
135 /* RewrapRepaint should be called whenever the control has changed in
136 * looks, but not content. Like resizing. */
138 ME_MarkAllForWrapping(editor);
139 if (editor->bRedraw)
141 ME_WrapMarkedParagraphs(editor);
142 ME_UpdateScrollBar(editor);
143 ME_Repaint(editor);
147 int ME_twips2pointsX(ME_Context *c, int x)
149 if (c->editor->nZoomNumerator == 0)
150 return x * c->dpi.cx / 1440;
151 else
152 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
155 int ME_twips2pointsY(ME_Context *c, int y)
157 if (c->editor->nZoomNumerator == 0)
158 return y * c->dpi.cy / 1440;
159 else
160 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
163 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
164 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
165 HDC hDC = c->hDC;
166 HGDIOBJ hOldFont;
167 COLORREF rgbOld;
168 int yOffset = 0, yTwipsOffset = 0;
169 SIZE sz;
170 COLORREF rgb;
172 hOldFont = ME_SelectStyleFont(c, s);
173 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
174 rgb = RGB(0,0,255);
175 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
176 rgb = GetSysColor(COLOR_WINDOWTEXT);
177 else
178 rgb = s->fmt.crTextColor;
179 rgbOld = SetTextColor(hDC, rgb);
180 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
181 yTwipsOffset = s->fmt.yOffset;
183 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
184 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
185 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
187 if (yTwipsOffset)
188 yOffset = ME_twips2pointsY(c, yTwipsOffset);
189 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
190 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
191 if (width) *width = sz.cx;
192 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
194 HPEN hPen;
195 switch (s->fmt.bUnderlineType)
197 case CFU_UNDERLINE:
198 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
199 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
200 hPen = CreatePen(PS_SOLID, 1, rgb);
201 break;
202 case CFU_UNDERLINEDOTTED:
203 hPen = CreatePen(PS_DOT, 1, rgb);
204 break;
205 default:
206 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
207 /* fall through */
208 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
209 case CFU_UNDERLINENONE:
210 hPen = NULL;
211 break;
213 if (hPen != NULL)
215 HPEN hOldPen = SelectObject(hDC, hPen);
216 /* FIXME: should use textmetrics info for Descent info */
217 MoveToEx(hDC, x, y - yOffset + 1, NULL);
218 LineTo(hDC, x + sz.cx, y - yOffset + 1);
219 SelectObject(hDC, hOldPen);
220 DeleteObject(hPen);
223 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
225 if (nSelFrom < 0) nSelFrom = 0;
226 if (nSelTo > nChars) nSelTo = nChars;
227 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
228 x += sz.cx;
229 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
231 /* Invert selection if not hidden by EM_HIDESELECTION */
232 if (c->editor->bHideSelection == FALSE)
233 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
235 SetTextColor(hDC, rgbOld);
236 ME_UnselectStyleFont(c, s, hOldFont);
239 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
240 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
241 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
242 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
243 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
244 SelectObject(hDC, hFont);
245 SetTextAlign(hDC, align);
246 SetTextColor(hDC, color);
249 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
251 ME_Run *run = &rundi->member.run;
252 ME_DisplayItem *start;
253 int runofs = run->nCharOfs+para->nCharOfs;
254 int nSelFrom, nSelTo;
255 const WCHAR wszSpace[] = {' ', 0};
257 if (run->nFlags & MERF_HIDDEN)
258 return;
260 start = ME_FindItemBack(rundi, diStartRow);
261 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
263 /* Draw selected end-of-paragraph mark */
264 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
265 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
266 c->pt.y + start->member.row.nYPos,
267 start->member.row.nHeight);
269 /* you can always comment it out if you need visible paragraph marks */
270 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
271 return;
273 if (run->nFlags & MERF_GRAPHICS)
274 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
275 else
277 if (c->editor->cPasswordMask)
279 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
280 ME_DrawTextWithStyle(c, x, y,
281 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
282 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
283 ME_DestroyString(szMasked);
285 else
286 ME_DrawTextWithStyle(c, x, y,
287 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
288 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
292 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
293 /* none */ {0, 1, PS_SOLID, FALSE},
294 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
295 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
296 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
297 /* 3 */ {3, 1, PS_SOLID, FALSE},
298 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
299 /* 6 */ {6, 1, PS_SOLID, FALSE},
300 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
301 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
302 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
303 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
304 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
307 static const COLORREF pen_colors[16] = {
308 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
309 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
310 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
311 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
312 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
313 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
314 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
315 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
318 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
320 int width;
322 if (editor->nZoomNumerator == 0)
324 width = border_details[idx].width_num + border_details[idx].width_den / 2;
325 width /= border_details[idx].width_den;
327 else
329 width = border_details[idx].width_num * editor->nZoomNumerator;
330 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
331 width /= border_details[idx].width_den * editor->nZoomDenominator;
333 return width;
336 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
338 int idx = (flags >> 8) & 0xF;
339 int width;
341 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
343 FIXME("Unsupported border value %d\n", idx);
344 return 0;
346 width = ME_GetBorderPenWidth(editor, idx);
347 if (border_details[idx].dble) width = width * 2 + 1;
348 return width;
351 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
353 int sp = 0, ls = 0;
354 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
356 /* FIXME: how to compute simply the line space in ls ??? */
357 /* FIXME: does line spacing include the line itself ??? */
358 switch (para->pFmt->bLineSpacingRule)
360 case 0: sp = ls; break;
361 case 1: sp = (3 * ls) / 2; break;
362 case 2: sp = 2 * ls; break;
363 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
364 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
365 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
366 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
368 if (c->editor->nZoomNumerator == 0)
369 return sp;
370 else
371 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
374 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
376 int idx, border_width, top_border, bottom_border;
377 RECT rc;
379 SetRectEmpty(bounds);
380 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
382 border_width = top_border = bottom_border = 0;
383 idx = (para->pFmt->wBorders >> 8) & 0xF;
384 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
386 if (para->pFmt->wBorders & 0x00B0)
387 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
388 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
389 if (para->pFmt->wBorders & 4) top_border = border_width;
390 if (para->pFmt->wBorders & 8) bottom_border = border_width;
393 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
395 rc.left = c->rcView.left;
396 rc.right = c->rcView.right;
397 rc.top = y;
398 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
399 rc.bottom = y + bounds->top + top_border;
400 FillRect(c->hDC, &rc, c->editor->hbrBackground);
403 if (para->pFmt->dwMask & PFM_SPACEAFTER)
405 rc.left = c->rcView.left;
406 rc.right = c->rcView.right;
407 rc.bottom = y + para->nHeight;
408 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
409 rc.top = rc.bottom - bounds->bottom - bottom_border;
410 FillRect(c->hDC, &rc, c->editor->hbrBackground);
413 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
414 int pen_width;
415 COLORREF pencr;
416 HPEN pen = NULL, oldpen = NULL;
417 POINT pt;
419 if (para->pFmt->wBorders & 64) /* autocolor */
420 pencr = GetSysColor(COLOR_WINDOWTEXT);
421 else
422 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
424 pen_width = ME_GetBorderPenWidth(c->editor, idx);
425 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
426 oldpen = SelectObject(c->hDC, pen);
427 MoveToEx(c->hDC, 0, 0, &pt);
429 /* before & after spaces are not included in border */
431 /* helper to draw the double lines in case of corner */
432 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
434 if (para->pFmt->wBorders & 1)
436 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
437 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
438 if (border_details[idx].dble) {
439 rc.left = c->rcView.left + 1;
440 rc.right = rc.left + border_width;
441 rc.top = y + bounds->top;
442 rc.bottom = y + para->nHeight - bounds->bottom;
443 FillRect(c->hDC, &rc, c->editor->hbrBackground);
444 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
445 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
447 bounds->left += border_width;
449 if (para->pFmt->wBorders & 2)
451 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
452 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
453 if (border_details[idx].dble) {
454 rc.left = c->rcView.right - pen_width - 1;
455 rc.right = c->rcView.right - 1;
456 rc.top = y + bounds->top;
457 rc.bottom = y + para->nHeight - bounds->bottom;
458 FillRect(c->hDC, &rc, c->editor->hbrBackground);
459 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
460 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
462 bounds->right += border_width;
464 if (para->pFmt->wBorders & 4)
466 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
467 LineTo(c->hDC, c->rcView.right, y + bounds->top);
468 if (border_details[idx].dble) {
469 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
470 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
472 bounds->top += border_width;
474 if (para->pFmt->wBorders & 8)
476 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
477 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
478 if (border_details[idx].dble) {
479 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
480 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
482 bounds->bottom += border_width;
484 #undef DD
486 MoveToEx(c->hDC, pt.x, pt.y, NULL);
487 SelectObject(c->hDC, oldpen);
488 DeleteObject(pen);
492 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
493 int align = SetTextAlign(c->hDC, TA_BASELINE);
494 ME_DisplayItem *p;
495 ME_Run *run;
496 ME_Paragraph *para = NULL;
497 RECT rc, rcPara, bounds;
498 int y = c->pt.y;
499 int height = 0, baseline = 0, no=0, pno = 0;
500 int xs = 0, xe = 0;
501 BOOL visible = FALSE;
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 pno = 0;
512 xs = c->rcView.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
513 xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
514 ME_DrawParaDecoration(c, para, y, &bounds);
515 y += bounds.top;
516 break;
517 case diStartRow:
518 y += height;
519 rcPara.top = y;
520 rcPara.bottom = y+p->member.row.nHeight;
521 visible = RectVisible(c->hDC, &rcPara);
522 if (visible) {
523 /* left margin */
524 rc.left = c->rcView.left + bounds.left;
525 rc.right = xs;
526 rc.top = y;
527 rc.bottom = y+p->member.row.nHeight;
528 FillRect(c->hDC, &rc, c->editor->hbrBackground);
529 /* right margin */
530 rc.left = xe;
531 rc.right = c->rcView.right - bounds.right;
532 FillRect(c->hDC, &rc, c->editor->hbrBackground);
533 rc.left = xs;
534 rc.right = xe;
535 FillRect(c->hDC, &rc, c->editor->hbrBackground);
537 if (me_debug)
539 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
540 WCHAR buf[128];
541 POINT pt = c->pt;
542 wsprintfW(buf, wszRowDebug, no);
543 pt.y = 12+y;
544 ME_DebugWrite(c->hDC, &pt, buf);
547 height = p->member.row.nHeight;
548 baseline = p->member.row.nBaseline;
549 if (!pno++)
550 xe += ME_twips2pointsX(c, para->pFmt->dxOffset);
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 absolutely*/
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 function 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 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
694 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
695 { /* Returns true if the scrollbar is visible */
696 SCROLLBARINFO sbi;
697 sbi.cbSize = sizeof(sbi);
698 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
699 return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
702 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
704 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
705 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
706 int y, yrel, yheight, yold;
708 assert(pRow);
709 assert(pPara);
711 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
712 yheight = pRow->member.row.nHeight;
713 yold = ME_GetYScrollPos(editor);
714 yrel = y - yold;
716 if (y < yold)
717 ME_ScrollAbs(editor,y);
718 else if (yrel + yheight > editor->sizeWindow.cy)
719 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
723 void
724 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
726 RECT rc;
727 int x, y, height;
728 ME_Cursor tmp;
730 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
731 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
733 rc.left = 0;
734 rc.top = y;
735 rc.bottom = y + height;
736 rc.right = editor->rcFormat.right;
737 InvalidateRect(editor->hWnd, &rc, FALSE);
741 void
742 ME_InvalidateSelection(ME_TextEditor *editor)
744 ME_DisplayItem *para1, *para2;
745 int nStart, nEnd;
746 int len = ME_GetTextLength(editor);
748 ME_GetSelection(editor, &nStart, &nEnd);
749 /* if both old and new selection are 0-char (= caret only), then
750 there's no (inverted) area to be repainted, neither old nor new */
751 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
752 return;
753 ME_WrapMarkedParagraphs(editor);
754 ME_GetSelectionParas(editor, &para1, &para2);
755 assert(para1->type == diParagraph);
756 assert(para2->type == diParagraph);
757 /* last selection markers aren't always updated, which means
758 they can point past the end of the document */
759 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
760 ME_MarkForPainting(editor,
761 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
762 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
763 } else {
764 /* if the start part of selection is being expanded or contracted... */
765 if (nStart < editor->nLastSelStart) {
766 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
767 } else
768 if (nStart > editor->nLastSelStart) {
769 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
772 /* if the end part of selection is being contracted or expanded... */
773 if (nEnd < editor->nLastSelEnd) {
774 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
775 } else
776 if (nEnd > editor->nLastSelEnd) {
777 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
781 ME_InvalidateMarkedParagraphs(editor);
782 /* remember the last invalidated position */
783 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
784 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
785 assert(editor->pLastSelStartPara->type == diParagraph);
786 assert(editor->pLastSelEndPara->type == diParagraph);
789 void
790 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
792 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
796 BOOL
797 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
799 /* TODO: Zoom images and objects */
801 if (numerator != 0)
803 if (denominator == 0)
804 return FALSE;
805 if (1.0 / 64.0 > (float)numerator / (float)denominator
806 || (float)numerator / (float)denominator > 64.0)
807 return FALSE;
810 editor->nZoomNumerator = numerator;
811 editor->nZoomDenominator = denominator;
813 ME_RewrapRepaint(editor);
814 return TRUE;