richedit: Each cell can contain multiple paragraphs in msftedit.
[wine.git] / dlls / riched20 / paint.c
blobc93a3e54631898ff1475a67e087157551ea21849
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); /* Calls ME_WrapMarkedParagraphs */
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 if (item->member.para.pCell
42 != item->member.para.next_para->member.para.pCell)
44 ME_Cell *cell = NULL;
45 cell = &ME_FindItemBack(item->member.para.next_para, diCell)->member.cell;
46 ye = cell->pt.y + cell->nHeight - yoffset;
47 } else {
48 ye = c.pt.y + item->member.para.nHeight;
50 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
52 BOOL bPaint = (rcUpdate == NULL);
53 if (rcUpdate)
54 bPaint = c.pt.y<rcUpdate->bottom && ye>rcUpdate->top;
55 if (bPaint)
57 ME_DrawParagraph(&c, item);
58 if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
59 item->member.para.nFlags &= ~MEPF_REPAINT;
62 if (item->member.para.pCell)
64 ME_Cell *cell = &item->member.para.pCell->member.cell;
65 c.pt.x = cell->pt.x + cell->nWidth;
66 if (item->member.para.pCell == item->member.para.next_para->member.para.pCell)
68 c.pt.y = ye;
69 } else {
70 if (item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART)
72 cell = &ME_FindItemFwd(item->member.para.next_para, diCell)->member.cell;
74 else if (item->member.para.next_para->member.para.nFlags & MEPF_ROWEND)
76 cell = &cell->next_cell->member.cell;
78 else
80 cell = &item->member.para.next_para->member.para.pCell->member.cell;
82 c.pt.y = cell->pt.y - yoffset;
84 } else if (!(item->member.para.nFlags & MEPF_ROWSTART)) {
85 c.pt.y = ye;
87 item = item->member.para.next_para;
89 if (c.pt.y<c.rcView.bottom) {
90 RECT rc;
91 int xs = c.rcView.left, xe = c.rcView.right;
92 int ys = c.pt.y, ye = c.rcView.bottom;
94 if (bOnlyNew)
96 int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
97 if (y1<y2)
98 ys = y1, ye = y2+1;
99 else
100 ys = ye;
103 if (rcUpdate && ys!=ye)
105 xs = rcUpdate->left, xe = rcUpdate->right;
106 if (rcUpdate->top > ys)
107 ys = rcUpdate->top;
108 if (rcUpdate->bottom < ye)
109 ye = rcUpdate->bottom;
112 if (ye>ys) {
113 rc.left = xs;
114 rc.top = ys;
115 rc.right = xe;
116 rc.bottom = ye;
117 FillRect(hDC, &rc, c.editor->hbrBackground);
120 if (editor->nTotalLength != editor->nLastTotalLength)
121 ME_SendRequestResize(editor, FALSE);
122 editor->nLastTotalLength = editor->nTotalLength;
123 ME_DestroyContext(&c, NULL);
126 void ME_Repaint(ME_TextEditor *editor)
128 if (ME_WrapMarkedParagraphs(editor))
130 ME_UpdateScrollBar(editor);
131 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
133 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
134 ME_SendOldNotify(editor, EN_UPDATE);
135 UpdateWindow(editor->hWnd);
138 void ME_UpdateRepaint(ME_TextEditor *editor)
140 /* Should be called whenever the contents of the control have changed */
141 ME_Cursor *pCursor;
142 BOOL wrappedParagraphs;
144 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
145 if (!editor->bRedraw) return;
146 if (wrappedParagraphs)
147 ME_UpdateScrollBar(editor);
149 /* Ensure that the cursor is visible */
150 pCursor = &editor->pCursors[0];
151 ME_EnsureVisible(editor, pCursor->pRun);
153 /* send EN_CHANGE if the event mask asks for it */
154 if(editor->nEventMask & ENM_CHANGE)
156 editor->nEventMask &= ~ENM_CHANGE;
157 ME_SendOldNotify(editor, EN_CHANGE);
158 editor->nEventMask |= ENM_CHANGE;
160 ME_Repaint(editor);
161 ME_SendSelChange(editor);
164 void
165 ME_RewrapRepaint(ME_TextEditor *editor)
167 /* RewrapRepaint should be called whenever the control has changed in
168 * looks, but not content. Like resizing. */
170 ME_MarkAllForWrapping(editor);
171 if (editor->bRedraw)
173 ME_WrapMarkedParagraphs(editor);
174 ME_UpdateScrollBar(editor);
175 ME_Repaint(editor);
179 int ME_twips2pointsX(ME_Context *c, int x)
181 if (c->editor->nZoomNumerator == 0)
182 return x * c->dpi.cx / 1440;
183 else
184 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
187 int ME_twips2pointsY(ME_Context *c, int y)
189 if (c->editor->nZoomNumerator == 0)
190 return y * c->dpi.cy / 1440;
191 else
192 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
195 static void ME_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
196 int nChars, ME_Style *s, int width,
197 int nSelFrom, int nSelTo, int ymin, int cy)
199 HDC hDC = c->hDC;
200 HGDIOBJ hOldFont = NULL;
201 SIZE sz;
202 int selWidth;
203 /* Only highlight if there is a selection in the run and when
204 * EM_HIDESELECTION is not being used to hide the selection. */
205 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
206 || c->editor->bHideSelection)
207 return;
208 hOldFont = ME_SelectStyleFont(c, s);
209 if (width <= 0)
211 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
212 width = sz.cx;
214 if (nSelFrom < 0) nSelFrom = 0;
215 if (nSelTo > nChars) nSelTo = nChars;
216 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
217 x += sz.cx;
218 if (nSelTo != nChars)
220 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
221 selWidth = sz.cx;
222 } else {
223 selWidth = width - sz.cx;
225 ME_UnselectStyleFont(c, s, hOldFont);
227 if (c->editor->bEmulateVersion10)
228 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
229 else
231 RECT rect;
232 HBRUSH hBrush;
233 rect.left = x;
234 rect.top = ymin;
235 rect.right = x + selWidth;
236 rect.bottom = ymin + cy;
237 hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
238 FillRect(hDC, &rect, hBrush);
239 DeleteObject(hBrush);
243 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
244 int nChars, ME_Style *s, int width,
245 int nSelFrom, int nSelTo, int ymin, int cy)
247 HDC hDC = c->hDC;
248 HGDIOBJ hOldFont;
249 COLORREF rgbOld;
250 int yOffset = 0, yTwipsOffset = 0;
251 SIZE sz;
252 COLORREF rgb;
253 HPEN hPen = NULL, hOldPen = NULL;
254 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
255 && nSelFrom < nSelTo && !c->editor->bHideSelection);
256 int xSelStart = x, xSelEnd = x;
257 int *lpDx = NULL;
258 /* lpDx is only needed for tabs to make sure the underline done automatically
259 * by the font extends to the end of the tab. Tabs are always stored as
260 * a single character run, so we can handle this case separately, since
261 * otherwise lpDx would need to specify the lengths of each character. */
262 if (width && nChars == 1)
263 lpDx = &width; /* Make sure underline for tab extends across tab space */
265 hOldFont = ME_SelectStyleFont(c, s);
266 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
267 yTwipsOffset = s->fmt.yOffset;
269 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
270 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
271 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
273 if (yTwipsOffset)
274 yOffset = ME_twips2pointsY(c, yTwipsOffset);
276 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
277 rgb = RGB(0,0,255);
278 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
279 rgb = GetSysColor(COLOR_WINDOWTEXT);
280 else
281 rgb = s->fmt.crTextColor;
283 /* Determine the area that is selected in the run. */
284 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
285 /* Treat width as an optional parameter. We can get the width from the
286 * text extent of the string if it isn't specified. */
287 if (!width) width = sz.cx;
288 if (bHighlightedText)
290 if (nSelFrom <= 0)
292 nSelFrom = 0;
294 else
296 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
297 xSelStart = x + sz.cx;
299 if (nSelTo >= nChars)
301 nSelTo = nChars;
302 xSelEnd = x + width;
304 else
306 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
307 xSelEnd = xSelStart + sz.cx;
311 /* Choose the pen type for underlining the text. */
312 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
314 switch (s->fmt.bUnderlineType)
316 case CFU_UNDERLINE:
317 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
318 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
319 hPen = CreatePen(PS_SOLID, 1, rgb);
320 break;
321 case CFU_UNDERLINEDOTTED:
322 hPen = CreatePen(PS_DOT, 1, rgb);
323 break;
324 default:
325 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
326 /* fall through */
327 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
328 case CFU_UNDERLINENONE:
329 hPen = NULL;
330 break;
332 if (hPen)
334 hOldPen = SelectObject(hDC, hPen);
338 rgbOld = SetTextColor(hDC, rgb);
339 if (bHighlightedText && !c->editor->bEmulateVersion10)
341 COLORREF rgbBackOld;
342 RECT dim;
343 /* FIXME: should use textmetrics info for Descent info */
344 if (hPen)
345 MoveToEx(hDC, x, y - yOffset + 1, NULL);
346 if (xSelStart > x)
348 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
349 if (hPen)
350 LineTo(hDC, xSelStart, y - yOffset + 1);
352 dim.top = ymin;
353 dim.bottom = ymin + cy;
354 dim.left = xSelStart;
355 dim.right = xSelEnd;
356 SetTextColor(hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
357 rgbBackOld = SetBkColor(hDC, GetSysColor(COLOR_HIGHLIGHT));
358 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
359 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
360 if (hPen)
361 LineTo(hDC, xSelEnd, y - yOffset + 1);
362 SetBkColor(hDC, rgbBackOld);
363 if (xSelEnd < x + width)
365 SetTextColor(hDC, rgb);
366 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
367 nChars-nSelTo, NULL);
368 if (hPen)
369 LineTo(hDC, x + width, y - yOffset + 1);
372 else
374 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
376 /* FIXME: should use textmetrics info for Descent info */
377 if (hPen)
379 MoveToEx(hDC, x, y - yOffset + 1, NULL);
380 LineTo(hDC, x + width, y - yOffset + 1);
383 if (bHighlightedText) /* v1.0 inverts the selection */
385 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
389 if (hPen)
391 SelectObject(hDC, hOldPen);
392 DeleteObject(hPen);
394 SetTextColor(hDC, rgbOld);
395 ME_UnselectStyleFont(c, s, hOldFont);
398 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
399 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
400 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
401 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
402 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
403 SelectObject(hDC, hFont);
404 SetTextAlign(hDC, align);
405 SetTextColor(hDC, color);
408 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
410 ME_Run *run = &rundi->member.run;
411 ME_DisplayItem *start;
412 int runofs = run->nCharOfs+para->nCharOfs;
413 int nSelFrom, nSelTo;
414 const WCHAR wszSpace[] = {' ', 0};
416 if (run->nFlags & MERF_HIDDEN)
417 return;
419 start = ME_FindItemBack(rundi, diStartRow);
420 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
422 /* Draw selected end-of-paragraph mark */
423 /* you can always comment it out if you need visible paragraph marks */
424 if (run->nFlags & MERF_ENDPARA)
426 if (runofs >= nSelFrom && runofs < nSelTo)
428 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
429 c->pt.y + start->member.row.pt.y,
430 start->member.row.nHeight);
432 return;
435 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
437 /* wszSpace is used instead of the tab character because otherwise
438 * an unwanted symbol can be inserted instead. */
439 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
440 nSelFrom-runofs,nSelTo-runofs,
441 c->pt.y + start->member.row.pt.y,
442 start->member.row.nHeight);
443 return;
446 if (run->nFlags & MERF_GRAPHICS)
447 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
448 else
450 if (c->editor->cPasswordMask)
452 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
453 ME_DrawTextWithStyle(c, x, y,
454 szMasked->szData, ME_StrVLen(szMasked), run->style, run->nWidth,
455 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.pt.y, start->member.row.nHeight);
456 ME_DestroyString(szMasked);
458 else
459 ME_DrawTextWithStyle(c, x, y,
460 run->strText->szData, ME_StrVLen(run->strText), run->style, run->nWidth,
461 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.pt.y, start->member.row.nHeight);
465 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
466 /* none */ {0, 1, PS_SOLID, FALSE},
467 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
468 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
469 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
470 /* 3 */ {3, 1, PS_SOLID, FALSE},
471 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
472 /* 6 */ {6, 1, PS_SOLID, FALSE},
473 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
474 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
475 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
476 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
477 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
480 static const COLORREF pen_colors[16] = {
481 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
482 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
483 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
484 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
485 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
486 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
487 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
488 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
491 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
493 int width;
495 if (editor->nZoomNumerator == 0)
497 width = border_details[idx].width_num + border_details[idx].width_den / 2;
498 width /= border_details[idx].width_den;
500 else
502 width = border_details[idx].width_num * editor->nZoomNumerator;
503 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
504 width /= border_details[idx].width_den * editor->nZoomDenominator;
506 return width;
509 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
511 int idx = (flags >> 8) & 0xF;
512 int width;
514 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
516 FIXME("Unsupported border value %d\n", idx);
517 return 0;
519 width = ME_GetBorderPenWidth(editor, idx);
520 if (border_details[idx].dble) width = width * 2 + 1;
521 return width;
524 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
526 int sp = 0, ls = 0;
527 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
529 /* FIXME: how to compute simply the line space in ls ??? */
530 /* FIXME: does line spacing include the line itself ??? */
531 switch (para->pFmt->bLineSpacingRule)
533 case 0: sp = ls; break;
534 case 1: sp = (3 * ls) / 2; break;
535 case 2: sp = 2 * ls; break;
536 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
537 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
538 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
539 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
541 if (c->editor->nZoomNumerator == 0)
542 return sp;
543 else
544 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
547 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
549 int idx, border_width, top_border, bottom_border;
550 RECT rc;
552 SetRectEmpty(bounds);
553 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
555 border_width = top_border = bottom_border = 0;
556 idx = (para->pFmt->wBorders >> 8) & 0xF;
557 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
559 if (para->pFmt->wBorders & 0x00B0)
560 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
561 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
562 if (para->pFmt->wBorders & 4) top_border = border_width;
563 if (para->pFmt->wBorders & 8) bottom_border = border_width;
566 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
568 rc.left = c->rcView.left;
569 rc.right = c->rcView.right;
570 rc.top = y;
571 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
572 rc.bottom = y + bounds->top + top_border;
573 FillRect(c->hDC, &rc, c->editor->hbrBackground);
576 if (para->pFmt->dwMask & PFM_SPACEAFTER)
578 rc.left = c->rcView.left;
579 rc.right = c->rcView.right;
580 rc.bottom = y + para->nHeight;
581 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
582 rc.top = rc.bottom - bounds->bottom - bottom_border;
583 FillRect(c->hDC, &rc, c->editor->hbrBackground);
586 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
587 int pen_width;
588 COLORREF pencr;
589 HPEN pen = NULL, oldpen = NULL;
590 POINT pt;
592 if (para->pFmt->wBorders & 64) /* autocolor */
593 pencr = GetSysColor(COLOR_WINDOWTEXT);
594 else
595 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
597 pen_width = ME_GetBorderPenWidth(c->editor, idx);
598 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
599 oldpen = SelectObject(c->hDC, pen);
600 MoveToEx(c->hDC, 0, 0, &pt);
602 /* before & after spaces are not included in border */
604 /* helper to draw the double lines in case of corner */
605 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
607 if (para->pFmt->wBorders & 1)
609 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
610 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
611 if (border_details[idx].dble) {
612 rc.left = c->rcView.left + 1;
613 rc.right = rc.left + border_width;
614 rc.top = y + bounds->top;
615 rc.bottom = y + para->nHeight - bounds->bottom;
616 FillRect(c->hDC, &rc, c->editor->hbrBackground);
617 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
618 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
620 bounds->left += border_width;
622 if (para->pFmt->wBorders & 2)
624 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
625 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
626 if (border_details[idx].dble) {
627 rc.left = c->rcView.right - pen_width - 1;
628 rc.right = c->rcView.right - 1;
629 rc.top = y + bounds->top;
630 rc.bottom = y + para->nHeight - bounds->bottom;
631 FillRect(c->hDC, &rc, c->editor->hbrBackground);
632 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
633 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
635 bounds->right += border_width;
637 if (para->pFmt->wBorders & 4)
639 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
640 LineTo(c->hDC, c->rcView.right, y + bounds->top);
641 if (border_details[idx].dble) {
642 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
643 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
645 bounds->top += border_width;
647 if (para->pFmt->wBorders & 8)
649 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
650 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
651 if (border_details[idx].dble) {
652 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
653 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
655 bounds->bottom += border_width;
657 #undef DD
659 MoveToEx(c->hDC, pt.x, pt.y, NULL);
660 SelectObject(c->hDC, oldpen);
661 DeleteObject(pen);
665 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
666 int align = SetTextAlign(c->hDC, TA_BASELINE);
667 ME_DisplayItem *p;
668 ME_Run *run;
669 ME_Paragraph *para = NULL;
670 RECT rc, rcText, bounds;
671 int y = c->pt.y;
672 int height = 0, baseline = 0, no=0, pno = 0;
673 BOOL visible = FALSE;
675 c->pt.x = c->rcView.left;
676 rc.left = c->rcView.left;
677 rc.right = c->rcView.right;
678 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
679 switch(p->type) {
680 case diParagraph:
681 para = &p->member.para;
682 assert(para);
683 pno = 0;
684 if (para->pCell)
686 ME_Cell *cell = &para->pCell->member.cell;
687 rc.left = cell->pt.x;
688 rc.right = rc.left + cell->nWidth;
689 rcText.left = cell->pt.x + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
690 rcText.right = cell->pt.x + cell->nWidth
691 - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
693 if (para->nFlags & MEPF_ROWSTART) {
694 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
695 rc.right = cell->pt.x;
696 } else if (para->nFlags & MEPF_ROWEND) {
697 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
698 rc.left = cell->pt.x + cell->nWidth;
700 rcText.left = rc.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
701 rcText.right = rc.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
702 ME_DrawParaDecoration(c, para, y, &bounds);
703 y += bounds.top;
704 break;
705 case diStartRow:
706 /* we should have seen a diParagraph before */
707 assert(para);
708 y += height;
709 rc.top = y;
710 if (para->nFlags & MEPF_ROWSTART) {
711 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
712 rc.bottom = y + cell->nHeight;
713 } else if (para->nFlags & MEPF_ROWEND) {
714 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
715 rc.bottom = y + cell->nHeight;
716 } else {
717 rc.bottom = y+p->member.row.nHeight;
719 visible = RectVisible(c->hDC, &rc);
720 if (visible) {
721 FillRect(c->hDC, &rc, c->editor->hbrBackground);
723 if (me_debug)
725 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
726 WCHAR buf[128];
727 POINT pt = c->pt;
728 wsprintfW(buf, wszRowDebug, no);
729 pt.y = 12+y;
730 ME_DebugWrite(c->hDC, &pt, buf);
733 height = p->member.row.nHeight;
734 baseline = p->member.row.nBaseline;
735 if (!pno++)
736 rcText.right += ME_twips2pointsX(c, para->pFmt->dxOffset);
737 break;
738 case diRun:
739 assert(para);
740 run = &p->member.run;
741 if (visible && me_debug) {
742 rc.left = c->rcView.left+run->pt.x;
743 rc.right = c->rcView.left+run->pt.x+run->nWidth;
744 rc.top = c->pt.y+run->pt.y;
745 rc.bottom = c->pt.y+run->pt.y+height;
746 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
747 if (run->nFlags & MERF_SKIPPED)
748 DrawFocusRect(c->hDC, &rc);
749 else
750 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
752 if (visible)
753 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
754 if (me_debug)
756 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
757 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
758 WCHAR buf[2560];
759 POINT pt;
760 pt.x = run->pt.x;
761 pt.y = c->pt.y + run->pt.y;
762 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
763 ME_DebugWrite(c->hDC, &pt, buf);
765 /* c->pt.x += p->member.run.nWidth; */
766 break;
767 case diCell:
768 /* Clear any space at the bottom of the cell after the text. */
769 if (para->nFlags & MEPF_ROWSTART)
770 break;
771 y += height;
772 rc.top = y;
773 rc.bottom = p->member.cell.pt.y + p->member.cell.nHeight
774 - ME_GetYScrollPos(c->editor);
775 if (RectVisible(c->hDC, &rc))
777 FillRect(c->hDC, &rc, c->editor->hbrBackground);
779 default:
780 break;
782 no++;
784 SetTextAlign(c->hDC, align);
787 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
789 ME_Scroll(editor, absY, 1);
792 void ME_ScrollUp(ME_TextEditor *editor, int cy)
794 ME_Scroll(editor, cy, 2);
797 void ME_ScrollDown(ME_TextEditor *editor, int cy)
799 ME_Scroll(editor, cy, 3);
802 void ME_Scroll(ME_TextEditor *editor, int value, int type)
804 SCROLLINFO si;
805 int nOrigPos, nNewPos, nActualScroll;
807 nOrigPos = ME_GetYScrollPos(editor);
809 si.cbSize = sizeof(SCROLLINFO);
810 si.fMask = SIF_POS;
812 switch (type)
814 case 1:
815 /*Scroll absolutely*/
816 si.nPos = value;
817 break;
818 case 2:
819 /* Scroll up - towards the beginning of the document */
820 si.nPos = nOrigPos - value;
821 break;
822 case 3:
823 /* Scroll down - towards the end of the document */
824 si.nPos = nOrigPos + value;
825 break;
826 default:
827 FIXME("ME_Scroll called incorrectly\n");
828 si.nPos = 0;
831 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
832 nActualScroll = nOrigPos - nNewPos;
833 if (editor->bRedraw)
835 if (abs(nActualScroll) > editor->sizeWindow.cy)
836 InvalidateRect(editor->hWnd, NULL, TRUE);
837 else
838 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
839 ME_Repaint(editor);
842 editor->vert_si.nMax = 0;
843 ME_UpdateScrollBar(editor);
847 void ME_UpdateScrollBar(ME_TextEditor *editor)
849 /* Note that this is the only function that should ever call SetScrolLInfo
850 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
851 * be used at all. */
853 HWND hWnd;
854 SCROLLINFO si;
855 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
857 if (ME_WrapMarkedParagraphs(editor))
858 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
860 hWnd = editor->hWnd;
861 si.cbSize = sizeof(si);
862 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
863 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
865 si.fMask = SIF_PAGE | SIF_RANGE;
866 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
867 si.fMask |= SIF_DISABLENOSCROLL;
868 if ((si.fMask & SIF_DISABLENOSCROLL))
870 bScrollBarWillBeVisible = TRUE;
873 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
875 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
876 ME_MarkAllForWrapping(editor);
877 ME_WrapMarkedParagraphs(editor);
880 si.nMin = 0;
881 si.nMax = editor->nTotalLength;
883 si.nPage = editor->sizeWindow.cy;
885 if (!(si.nMin == editor->vert_si.nMin && si.nMax == editor->vert_si.nMax && si.nPage == editor->vert_si.nPage))
887 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
888 editor->vert_si.nMin = si.nMin;
889 editor->vert_si.nMax = si.nMax;
890 editor->vert_si.nPage = si.nPage;
891 if (bScrollBarWillBeVisible)
893 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
895 else
897 if (bScrollBarWasVisible && !(si.fMask & SIF_DISABLENOSCROLL))
898 ShowScrollBar(hWnd, SB_VERT, FALSE);
903 int ME_GetYScrollPos(ME_TextEditor *editor)
905 SCROLLINFO si;
906 si.cbSize = sizeof(si);
907 si.fMask = SIF_POS;
908 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
911 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
912 { /* Returns true if the scrollbar is visible */
913 return (editor->vert_si.nMax - editor->vert_si.nMin >= max(editor->vert_si.nPage - 1, 0));
916 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
918 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
919 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
920 int y, yrel, yheight, yold;
922 assert(pRow);
923 assert(pPara);
925 y = pPara->member.para.pt.y+pRow->member.row.pt.y;
926 yheight = pRow->member.row.nHeight;
927 yold = ME_GetYScrollPos(editor);
928 yrel = y - yold;
930 if (y < yold)
931 ME_ScrollAbs(editor,y);
932 else if (yrel + yheight > editor->sizeWindow.cy)
933 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
937 void
938 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
940 RECT rc;
941 int x, y, height;
942 ME_Cursor tmp;
944 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
945 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
947 rc.left = 0;
948 rc.top = y;
949 rc.bottom = y + height;
950 rc.right = editor->rcFormat.right;
951 InvalidateRect(editor->hWnd, &rc, FALSE);
955 void
956 ME_InvalidateSelection(ME_TextEditor *editor)
958 ME_DisplayItem *para1, *para2;
959 int nStart, nEnd;
960 int len = ME_GetTextLength(editor);
962 ME_GetSelection(editor, &nStart, &nEnd);
963 /* if both old and new selection are 0-char (= caret only), then
964 there's no (inverted) area to be repainted, neither old nor new */
965 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
966 return;
967 ME_WrapMarkedParagraphs(editor);
968 ME_GetSelectionParas(editor, &para1, &para2);
969 assert(para1->type == diParagraph);
970 assert(para2->type == diParagraph);
971 /* last selection markers aren't always updated, which means
972 they can point past the end of the document */
973 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
974 ME_MarkForPainting(editor,
975 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
976 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
977 } else {
978 /* if the start part of selection is being expanded or contracted... */
979 if (nStart < editor->nLastSelStart) {
980 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
981 } else
982 if (nStart > editor->nLastSelStart) {
983 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
986 /* if the end part of selection is being contracted or expanded... */
987 if (nEnd < editor->nLastSelEnd) {
988 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
989 } else
990 if (nEnd > editor->nLastSelEnd) {
991 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
995 ME_InvalidateMarkedParagraphs(editor);
996 /* remember the last invalidated position */
997 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
998 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
999 assert(editor->pLastSelStartPara->type == diParagraph);
1000 assert(editor->pLastSelEndPara->type == diParagraph);
1003 void
1004 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
1006 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
1010 BOOL
1011 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1013 /* TODO: Zoom images and objects */
1015 if (numerator != 0)
1017 if (denominator == 0)
1018 return FALSE;
1019 if (1.0 / 64.0 > (float)numerator / (float)denominator
1020 || (float)numerator / (float)denominator > 64.0)
1021 return FALSE;
1024 editor->nZoomNumerator = numerator;
1025 editor->nZoomDenominator = denominator;
1027 ME_RewrapRepaint(editor);
1028 return TRUE;