Release 0.9.61.
[wine/gsoc-2012-control.git] / dlls / riched20 / paint.c
blob2ec7fb9d8a3b4c0fb97dd5b4e4704351eb774752
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, NULL);
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;
112 BOOL wrappedParagraphs;
114 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
115 if (!editor->bRedraw) return;
116 if (wrappedParagraphs)
117 ME_UpdateScrollBar(editor);
119 /* Ensure that the cursor is visible */
120 pCursor = &editor->pCursors[0];
121 ME_EnsureVisible(editor, pCursor->pRun);
123 /* send EN_CHANGE if the event mask asks for it */
124 if(editor->nEventMask & ENM_CHANGE)
126 editor->nEventMask &= ~ENM_CHANGE;
127 ME_SendOldNotify(editor, EN_CHANGE);
128 editor->nEventMask |= ENM_CHANGE;
130 ME_Repaint(editor);
131 ME_SendSelChange(editor);
134 void
135 ME_RewrapRepaint(ME_TextEditor *editor)
137 /* RewrapRepaint should be called whenever the control has changed in
138 * looks, but not content. Like resizing. */
140 ME_MarkAllForWrapping(editor);
141 if (editor->bRedraw)
143 ME_WrapMarkedParagraphs(editor);
144 ME_UpdateScrollBar(editor);
145 ME_Repaint(editor);
149 int ME_twips2pointsX(ME_Context *c, int x)
151 if (c->editor->nZoomNumerator == 0)
152 return x * c->dpi.cx / 1440;
153 else
154 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
157 int ME_twips2pointsY(ME_Context *c, int y)
159 if (c->editor->nZoomNumerator == 0)
160 return y * c->dpi.cy / 1440;
161 else
162 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
165 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars,
166 ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
167 HDC hDC = c->hDC;
168 HGDIOBJ hOldFont;
169 COLORREF rgbOld;
170 int yOffset = 0, yTwipsOffset = 0;
171 SIZE sz;
172 COLORREF rgb;
174 hOldFont = ME_SelectStyleFont(c, s);
175 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
176 rgb = RGB(0,0,255);
177 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
178 rgb = GetSysColor(COLOR_WINDOWTEXT);
179 else
180 rgb = s->fmt.crTextColor;
181 rgbOld = SetTextColor(hDC, rgb);
182 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
183 yTwipsOffset = s->fmt.yOffset;
185 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
186 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
187 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
189 if (yTwipsOffset)
190 yOffset = ME_twips2pointsY(c, yTwipsOffset);
191 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
192 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
193 if (width) *width = sz.cx;
194 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
196 HPEN hPen;
197 switch (s->fmt.bUnderlineType)
199 case CFU_UNDERLINE:
200 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
201 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
202 hPen = CreatePen(PS_SOLID, 1, rgb);
203 break;
204 case CFU_UNDERLINEDOTTED:
205 hPen = CreatePen(PS_DOT, 1, rgb);
206 break;
207 default:
208 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
209 /* fall through */
210 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
211 case CFU_UNDERLINENONE:
212 hPen = NULL;
213 break;
215 if (hPen != NULL)
217 HPEN hOldPen = SelectObject(hDC, hPen);
218 /* FIXME: should use textmetrics info for Descent info */
219 MoveToEx(hDC, x, y - yOffset + 1, NULL);
220 LineTo(hDC, x + sz.cx, y - yOffset + 1);
221 SelectObject(hDC, hOldPen);
222 DeleteObject(hPen);
225 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
227 if (nSelFrom < 0) nSelFrom = 0;
228 if (nSelTo > nChars) nSelTo = nChars;
229 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
230 x += sz.cx;
231 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
233 /* Invert selection if not hidden by EM_HIDESELECTION */
234 if (c->editor->bHideSelection == FALSE)
235 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
237 SetTextColor(hDC, rgbOld);
238 ME_UnselectStyleFont(c, s, hOldFont);
241 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
242 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
243 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
244 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
245 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
246 SelectObject(hDC, hFont);
247 SetTextAlign(hDC, align);
248 SetTextColor(hDC, color);
251 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
253 ME_Run *run = &rundi->member.run;
254 ME_DisplayItem *start;
255 int runofs = run->nCharOfs+para->nCharOfs;
256 int nSelFrom, nSelTo;
257 const WCHAR wszSpace[] = {' ', 0};
259 if (run->nFlags & MERF_HIDDEN)
260 return;
262 start = ME_FindItemBack(rundi, diStartRow);
263 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
265 /* Draw selected end-of-paragraph mark */
266 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
267 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
268 c->pt.y + start->member.row.nYPos,
269 start->member.row.nHeight);
271 /* you can always comment it out if you need visible paragraph marks */
272 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
273 return;
275 if (run->nFlags & MERF_GRAPHICS)
276 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
277 else
279 if (c->editor->cPasswordMask)
281 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
282 ME_DrawTextWithStyle(c, x, y,
283 szMasked->szData, ME_StrVLen(szMasked), run->style, NULL,
284 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
285 ME_DestroyString(szMasked);
287 else
288 ME_DrawTextWithStyle(c, x, y,
289 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
290 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
294 static struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
295 /* none */ {0, 1, PS_SOLID, FALSE},
296 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
297 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
298 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
299 /* 3 */ {3, 1, PS_SOLID, FALSE},
300 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
301 /* 6 */ {6, 1, PS_SOLID, FALSE},
302 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
303 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
304 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
305 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
306 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
309 static COLORREF pen_colors[16] = {
310 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
311 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
312 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
313 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
314 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
315 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
316 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
317 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
320 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
322 int width;
324 if (editor->nZoomNumerator == 0)
326 width = border_details[idx].width_num + border_details[idx].width_den / 2;
327 width /= border_details[idx].width_den;
329 else
331 width = border_details[idx].width_num * editor->nZoomNumerator;
332 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
333 width /= border_details[idx].width_den * editor->nZoomDenominator;
335 return width;
338 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
340 int idx = (flags >> 8) & 0xF;
341 int width;
343 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
345 FIXME("Unsupported border value %d\n", idx);
346 return 0;
348 width = ME_GetBorderPenWidth(editor, idx);
349 if (border_details[idx].dble) width = width * 2 + 1;
350 return width;
353 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
355 int sp = 0, ls = 0;
356 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
358 /* FIXME: how to compute simply the line space in ls ??? */
359 /* FIXME: does line spacing include the line itself ??? */
360 switch (para->pFmt->bLineSpacingRule)
362 case 0: sp = ls; break;
363 case 1: sp = (3 * ls) / 2; break;
364 case 2: sp = 2 * ls; break;
365 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
366 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
367 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
368 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
370 if (c->editor->nZoomNumerator == 0)
371 return sp;
372 else
373 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
376 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
378 int idx, border_width, top_border, bottom_border;
379 RECT rc;
381 SetRectEmpty(bounds);
382 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
384 border_width = top_border = bottom_border = 0;
385 idx = (para->pFmt->wBorders >> 8) & 0xF;
386 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
388 if (para->pFmt->wBorders & 0x00B0)
389 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
390 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
391 if (para->pFmt->wBorders & 4) top_border = border_width;
392 if (para->pFmt->wBorders & 8) bottom_border = border_width;
395 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
397 rc.left = c->rcView.left;
398 rc.right = c->rcView.right;
399 rc.top = y;
400 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
401 rc.bottom = y + bounds->top + top_border;
402 FillRect(c->hDC, &rc, c->editor->hbrBackground);
405 if (para->pFmt->dwMask & PFM_SPACEAFTER)
407 rc.left = c->rcView.left;
408 rc.right = c->rcView.right;
409 rc.bottom = y + para->nHeight;
410 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
411 rc.top = rc.bottom - bounds->bottom - bottom_border;
412 FillRect(c->hDC, &rc, c->editor->hbrBackground);
415 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
416 int pen_width;
417 COLORREF pencr;
418 HPEN pen = NULL, oldpen = NULL;
419 POINT pt;
421 if (para->pFmt->wBorders & 64) /* autocolor */
422 pencr = GetSysColor(COLOR_WINDOWTEXT);
423 else
424 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
426 pen_width = ME_GetBorderPenWidth(c->editor, idx);
427 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
428 oldpen = SelectObject(c->hDC, pen);
429 MoveToEx(c->hDC, 0, 0, &pt);
431 /* before & after spaces are not included in border */
433 /* helper to draw the double lines in case of corner */
434 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
436 if (para->pFmt->wBorders & 1)
438 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
439 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
440 if (border_details[idx].dble) {
441 rc.left = c->rcView.left + 1;
442 rc.right = rc.left + border_width;
443 rc.top = y + bounds->top;
444 rc.bottom = y + para->nHeight - bounds->bottom;
445 FillRect(c->hDC, &rc, c->editor->hbrBackground);
446 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
447 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
449 bounds->left += border_width;
451 if (para->pFmt->wBorders & 2)
453 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
454 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
455 if (border_details[idx].dble) {
456 rc.left = c->rcView.right - pen_width - 1;
457 rc.right = c->rcView.right - 1;
458 rc.top = y + bounds->top;
459 rc.bottom = y + para->nHeight - bounds->bottom;
460 FillRect(c->hDC, &rc, c->editor->hbrBackground);
461 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
462 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
464 bounds->right += border_width;
466 if (para->pFmt->wBorders & 4)
468 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
469 LineTo(c->hDC, c->rcView.right, y + bounds->top);
470 if (border_details[idx].dble) {
471 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
472 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
474 bounds->top += border_width;
476 if (para->pFmt->wBorders & 8)
478 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
479 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
480 if (border_details[idx].dble) {
481 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
482 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
484 bounds->bottom += border_width;
486 #undef DD
488 MoveToEx(c->hDC, pt.x, pt.y, NULL);
489 SelectObject(c->hDC, oldpen);
490 DeleteObject(pen);
494 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
495 int align = SetTextAlign(c->hDC, TA_BASELINE);
496 ME_DisplayItem *p;
497 ME_Run *run;
498 ME_Paragraph *para = NULL;
499 RECT rc, rcPara, bounds;
500 int y = c->pt.y;
501 int height = 0, baseline = 0, no=0, pno = 0;
502 int xs = 0, xe = 0;
503 BOOL visible = FALSE;
505 c->pt.x = c->rcView.left;
506 rcPara.left = c->rcView.left;
507 rcPara.right = c->rcView.right;
508 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
509 switch(p->type) {
510 case diParagraph:
511 para = &p->member.para;
512 assert(para);
513 pno = 0;
514 xs = c->rcView.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
515 xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
516 ME_DrawParaDecoration(c, para, y, &bounds);
517 y += bounds.top;
518 break;
519 case diStartRow:
520 y += height;
521 rcPara.top = y;
522 rcPara.bottom = y+p->member.row.nHeight;
523 visible = RectVisible(c->hDC, &rcPara);
524 if (visible) {
525 /* left margin */
526 rc.left = c->rcView.left + bounds.left;
527 rc.right = xs;
528 rc.top = y;
529 rc.bottom = y+p->member.row.nHeight;
530 FillRect(c->hDC, &rc, c->editor->hbrBackground);
531 /* right margin */
532 rc.left = xe;
533 rc.right = c->rcView.right - bounds.right;
534 FillRect(c->hDC, &rc, c->editor->hbrBackground);
535 rc.left = xs;
536 rc.right = xe;
537 FillRect(c->hDC, &rc, c->editor->hbrBackground);
539 if (me_debug)
541 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
542 WCHAR buf[128];
543 POINT pt = c->pt;
544 wsprintfW(buf, wszRowDebug, no);
545 pt.y = 12+y;
546 ME_DebugWrite(c->hDC, &pt, buf);
549 height = p->member.row.nHeight;
550 baseline = p->member.row.nBaseline;
551 if (!pno++)
552 xe += ME_twips2pointsX(c, para->pFmt->dxOffset);
553 break;
554 case diRun:
555 assert(para);
556 run = &p->member.run;
557 if (visible && me_debug) {
558 rc.left = c->rcView.left+run->pt.x;
559 rc.right = c->rcView.left+run->pt.x+run->nWidth;
560 rc.top = c->pt.y+run->pt.y;
561 rc.bottom = c->pt.y+run->pt.y+height;
562 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
563 if (run->nFlags & MERF_SKIPPED)
564 DrawFocusRect(c->hDC, &rc);
565 else
566 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
568 if (visible)
569 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
570 if (me_debug)
572 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
573 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
574 WCHAR buf[2560];
575 POINT pt;
576 pt.x = run->pt.x;
577 pt.y = c->pt.y + run->pt.y;
578 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
579 ME_DebugWrite(c->hDC, &pt, buf);
581 /* c->pt.x += p->member.run.nWidth; */
582 break;
583 default:
584 break;
586 no++;
588 SetTextAlign(c->hDC, align);
591 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
593 ME_Scroll(editor, absY, 1);
596 void ME_ScrollUp(ME_TextEditor *editor, int cy)
598 ME_Scroll(editor, cy, 2);
601 void ME_ScrollDown(ME_TextEditor *editor, int cy)
603 ME_Scroll(editor, cy, 3);
606 void ME_Scroll(ME_TextEditor *editor, int value, int type)
608 SCROLLINFO si;
609 int nOrigPos, nNewPos, nActualScroll;
611 nOrigPos = ME_GetYScrollPos(editor);
613 si.cbSize = sizeof(SCROLLINFO);
614 si.fMask = SIF_POS;
616 switch (type)
618 case 1:
619 /*Scroll absolutely*/
620 si.nPos = value;
621 break;
622 case 2:
623 /* Scroll up - towards the beginning of the document */
624 si.nPos = nOrigPos - value;
625 break;
626 case 3:
627 /* Scroll down - towards the end of the document */
628 si.nPos = nOrigPos + value;
629 break;
630 default:
631 FIXME("ME_Scroll called incorrectly\n");
632 si.nPos = 0;
635 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
636 nActualScroll = nOrigPos - nNewPos;
637 if (editor->bRedraw)
639 if (abs(nActualScroll) > editor->sizeWindow.cy)
640 InvalidateRect(editor->hWnd, NULL, TRUE);
641 else
642 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
643 ME_Repaint(editor);
646 ME_UpdateScrollBar(editor);
650 void ME_UpdateScrollBar(ME_TextEditor *editor)
652 /* Note that this is the only function that should ever call SetScrolLInfo
653 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
654 * be used at all. */
656 HWND hWnd;
657 SCROLLINFO si;
658 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
660 if (ME_WrapMarkedParagraphs(editor))
661 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
663 hWnd = editor->hWnd;
664 si.cbSize = sizeof(si);
665 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
666 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
668 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
670 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
671 ME_MarkAllForWrapping(editor);
672 ME_WrapMarkedParagraphs(editor);
675 si.fMask = SIF_PAGE | SIF_RANGE;
676 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
677 si.fMask |= SIF_DISABLENOSCROLL;
679 si.nMin = 0;
680 si.nMax = editor->nTotalLength;
682 si.nPage = editor->sizeWindow.cy;
684 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
685 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
688 int ME_GetYScrollPos(ME_TextEditor *editor)
690 SCROLLINFO si;
691 si.cbSize = sizeof(si);
692 si.fMask = SIF_POS;
693 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
696 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
697 { /* Returns true if the scrollbar is visible */
698 SCROLLBARINFO sbi;
699 sbi.cbSize = sizeof(sbi);
700 GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
701 return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
704 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
706 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
707 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
708 int y, yrel, yheight, yold;
710 assert(pRow);
711 assert(pPara);
713 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
714 yheight = pRow->member.row.nHeight;
715 yold = ME_GetYScrollPos(editor);
716 yrel = y - yold;
718 if (y < yold)
719 ME_ScrollAbs(editor,y);
720 else if (yrel + yheight > editor->sizeWindow.cy)
721 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
725 void
726 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
728 RECT rc;
729 int x, y, height;
730 ME_Cursor tmp;
732 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
733 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
735 rc.left = 0;
736 rc.top = y;
737 rc.bottom = y + height;
738 rc.right = editor->rcFormat.right;
739 InvalidateRect(editor->hWnd, &rc, FALSE);
743 void
744 ME_InvalidateSelection(ME_TextEditor *editor)
746 ME_DisplayItem *para1, *para2;
747 int nStart, nEnd;
748 int len = ME_GetTextLength(editor);
750 ME_GetSelection(editor, &nStart, &nEnd);
751 /* if both old and new selection are 0-char (= caret only), then
752 there's no (inverted) area to be repainted, neither old nor new */
753 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
754 return;
755 ME_WrapMarkedParagraphs(editor);
756 ME_GetSelectionParas(editor, &para1, &para2);
757 assert(para1->type == diParagraph);
758 assert(para2->type == diParagraph);
759 /* last selection markers aren't always updated, which means
760 they can point past the end of the document */
761 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
762 ME_MarkForPainting(editor,
763 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
764 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
765 } else {
766 /* if the start part of selection is being expanded or contracted... */
767 if (nStart < editor->nLastSelStart) {
768 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
769 } else
770 if (nStart > editor->nLastSelStart) {
771 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
774 /* if the end part of selection is being contracted or expanded... */
775 if (nEnd < editor->nLastSelEnd) {
776 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
777 } else
778 if (nEnd > editor->nLastSelEnd) {
779 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
783 ME_InvalidateMarkedParagraphs(editor);
784 /* remember the last invalidated position */
785 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
786 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
787 assert(editor->pLastSelStartPara->type == diParagraph);
788 assert(editor->pLastSelEndPara->type == diParagraph);
791 void
792 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
794 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
798 BOOL
799 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
801 /* TODO: Zoom images and objects */
803 if (numerator != 0)
805 if (denominator == 0)
806 return FALSE;
807 if (1.0 / 64.0 > (float)numerator / (float)denominator
808 || (float)numerator / (float)denominator > 64.0)
809 return FALSE;
812 editor->nZoomNumerator = numerator;
813 editor->nZoomDenominator = denominator;
815 ME_RewrapRepaint(editor);
816 return TRUE;