richedit: Protect deletion of cell boundaries when not deleting row.
[wine.git] / dlls / riched20 / paint.c
blob921d3faaed5b230284ecdc32caf8955955e0f6e2
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_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
164 int nChars, ME_Style *s, int width,
165 int nSelFrom, int nSelTo, int ymin, int cy)
167 HDC hDC = c->hDC;
168 HGDIOBJ hOldFont = NULL;
169 SIZE sz;
170 int selWidth;
171 /* Only highlight if there is a selection in the run and when
172 * EM_HIDESELECTION is not being used to hide the selection. */
173 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
174 || c->editor->bHideSelection)
175 return;
176 hOldFont = ME_SelectStyleFont(c, s);
177 if (width <= 0)
179 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
180 width = sz.cx;
182 if (nSelFrom < 0) nSelFrom = 0;
183 if (nSelTo > nChars) nSelTo = nChars;
184 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
185 x += sz.cx;
186 if (nSelTo != nChars)
188 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
189 selWidth = sz.cx;
190 } else {
191 selWidth = width - sz.cx;
193 ME_UnselectStyleFont(c, s, hOldFont);
195 if (c->editor->bEmulateVersion10)
196 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
197 else
199 RECT rect;
200 HBRUSH hBrush;
201 rect.left = x;
202 rect.top = ymin;
203 rect.right = x + selWidth;
204 rect.bottom = ymin + cy;
205 hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
206 FillRect(hDC, &rect, hBrush);
207 DeleteObject(hBrush);
211 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
212 int nChars, ME_Style *s, int width,
213 int nSelFrom, int nSelTo, int ymin, int cy)
215 HDC hDC = c->hDC;
216 HGDIOBJ hOldFont;
217 COLORREF rgbOld;
218 int yOffset = 0, yTwipsOffset = 0;
219 SIZE sz;
220 COLORREF rgb;
221 HPEN hPen = NULL, hOldPen = NULL;
222 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
223 && nSelFrom < nSelTo && !c->editor->bHideSelection);
224 int xSelStart = x, xSelEnd = x;
225 int *lpDx = NULL;
226 /* lpDx is only needed for tabs to make sure the underline done automatically
227 * by the font extends to the end of the tab. Tabs are always stored as
228 * a single character run, so we can handle this case separately, since
229 * otherwise lpDx would need to specify the lengths of each character. */
230 if (width && nChars == 1)
231 lpDx = &width; /* Make sure underline for tab extends across tab space */
233 hOldFont = ME_SelectStyleFont(c, s);
234 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
235 yTwipsOffset = s->fmt.yOffset;
237 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
238 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
239 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
241 if (yTwipsOffset)
242 yOffset = ME_twips2pointsY(c, yTwipsOffset);
244 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
245 rgb = RGB(0,0,255);
246 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
247 rgb = GetSysColor(COLOR_WINDOWTEXT);
248 else
249 rgb = s->fmt.crTextColor;
251 /* Determine the area that is selected in the run. */
252 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
253 /* Treat width as an optional parameter. We can get the width from the
254 * text extent of the string if it isn't specified. */
255 if (!width) width = sz.cx;
256 if (bHighlightedText)
258 if (nSelFrom <= 0)
260 nSelFrom = 0;
262 else
264 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
265 xSelStart = x + sz.cx;
267 if (nSelTo >= nChars)
269 nSelTo = nChars;
270 xSelEnd = x + width;
272 else
274 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
275 xSelEnd = xSelStart + sz.cx;
279 /* Choose the pen type for underlining the text. */
280 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
282 switch (s->fmt.bUnderlineType)
284 case CFU_UNDERLINE:
285 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
286 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
287 hPen = CreatePen(PS_SOLID, 1, rgb);
288 break;
289 case CFU_UNDERLINEDOTTED:
290 hPen = CreatePen(PS_DOT, 1, rgb);
291 break;
292 default:
293 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
294 /* fall through */
295 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
296 case CFU_UNDERLINENONE:
297 hPen = NULL;
298 break;
300 if (hPen)
302 hOldPen = SelectObject(hDC, hPen);
306 rgbOld = SetTextColor(hDC, rgb);
307 if (bHighlightedText && !c->editor->bEmulateVersion10)
309 COLORREF rgbBackOld;
310 RECT dim;
311 /* FIXME: should use textmetrics info for Descent info */
312 if (hPen)
313 MoveToEx(hDC, x, y - yOffset + 1, NULL);
314 if (xSelStart > x)
316 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
317 if (hPen)
318 LineTo(hDC, xSelStart, y - yOffset + 1);
320 dim.top = ymin;
321 dim.bottom = ymin + cy;
322 dim.left = xSelStart;
323 dim.right = xSelEnd;
324 SetTextColor(hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
325 rgbBackOld = SetBkColor(hDC, GetSysColor(COLOR_HIGHLIGHT));
326 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
327 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
328 if (hPen)
329 LineTo(hDC, xSelEnd, y - yOffset + 1);
330 SetBkColor(hDC, rgbBackOld);
331 if (xSelEnd < x + width)
333 SetTextColor(hDC, rgb);
334 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
335 nChars-nSelTo, NULL);
336 if (hPen)
337 LineTo(hDC, x + width, y - yOffset + 1);
340 else
342 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
344 /* FIXME: should use textmetrics info for Descent info */
345 if (hPen)
347 MoveToEx(hDC, x, y - yOffset + 1, NULL);
348 LineTo(hDC, x + width, y - yOffset + 1);
351 if (bHighlightedText) /* v1.0 inverts the selection */
353 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
357 if (hPen)
359 SelectObject(hDC, hOldPen);
360 DeleteObject(hPen);
362 SetTextColor(hDC, rgbOld);
363 ME_UnselectStyleFont(c, s, hOldFont);
366 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
367 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
368 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
369 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
370 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
371 SelectObject(hDC, hFont);
372 SetTextAlign(hDC, align);
373 SetTextColor(hDC, color);
376 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
378 ME_Run *run = &rundi->member.run;
379 ME_DisplayItem *start;
380 int runofs = run->nCharOfs+para->nCharOfs;
381 int nSelFrom, nSelTo;
382 const WCHAR wszSpace[] = {' ', 0};
384 if (run->nFlags & MERF_HIDDEN)
385 return;
387 start = ME_FindItemBack(rundi, diStartRow);
388 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
390 /* Draw selected end-of-paragraph mark */
391 /* you can always comment it out if you need visible paragraph marks */
392 if (run->nFlags & MERF_ENDPARA)
394 if (runofs >= nSelFrom && runofs < nSelTo)
396 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
397 c->pt.y + start->member.row.nYPos,
398 start->member.row.nHeight);
400 return;
403 if (run->nFlags & MERF_TAB)
405 /* wszSpace is used instead of the tab character because otherwise
406 * an unwanted symbol can be inserted instead. */
407 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
408 nSelFrom-runofs,nSelTo-runofs,
409 c->pt.y + start->member.row.nYPos,
410 start->member.row.nHeight);
411 return;
414 if (run->nFlags & MERF_GRAPHICS)
415 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
416 else
418 if (c->editor->cPasswordMask)
420 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
421 ME_DrawTextWithStyle(c, x, y,
422 szMasked->szData, ME_StrVLen(szMasked), run->style, run->nWidth,
423 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
424 ME_DestroyString(szMasked);
426 else
427 ME_DrawTextWithStyle(c, x, y,
428 run->strText->szData, ME_StrVLen(run->strText), run->style, run->nWidth,
429 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
433 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
434 /* none */ {0, 1, PS_SOLID, FALSE},
435 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
436 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
437 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
438 /* 3 */ {3, 1, PS_SOLID, FALSE},
439 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
440 /* 6 */ {6, 1, PS_SOLID, FALSE},
441 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
442 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
443 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
444 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
445 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
448 static const COLORREF pen_colors[16] = {
449 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
450 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
451 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
452 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
453 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
454 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
455 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
456 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
459 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
461 int width;
463 if (editor->nZoomNumerator == 0)
465 width = border_details[idx].width_num + border_details[idx].width_den / 2;
466 width /= border_details[idx].width_den;
468 else
470 width = border_details[idx].width_num * editor->nZoomNumerator;
471 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
472 width /= border_details[idx].width_den * editor->nZoomDenominator;
474 return width;
477 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
479 int idx = (flags >> 8) & 0xF;
480 int width;
482 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
484 FIXME("Unsupported border value %d\n", idx);
485 return 0;
487 width = ME_GetBorderPenWidth(editor, idx);
488 if (border_details[idx].dble) width = width * 2 + 1;
489 return width;
492 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
494 int sp = 0, ls = 0;
495 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
497 /* FIXME: how to compute simply the line space in ls ??? */
498 /* FIXME: does line spacing include the line itself ??? */
499 switch (para->pFmt->bLineSpacingRule)
501 case 0: sp = ls; break;
502 case 1: sp = (3 * ls) / 2; break;
503 case 2: sp = 2 * ls; break;
504 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
505 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
506 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
507 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
509 if (c->editor->nZoomNumerator == 0)
510 return sp;
511 else
512 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
515 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
517 int idx, border_width, top_border, bottom_border;
518 RECT rc;
520 SetRectEmpty(bounds);
521 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
523 border_width = top_border = bottom_border = 0;
524 idx = (para->pFmt->wBorders >> 8) & 0xF;
525 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
527 if (para->pFmt->wBorders & 0x00B0)
528 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
529 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
530 if (para->pFmt->wBorders & 4) top_border = border_width;
531 if (para->pFmt->wBorders & 8) bottom_border = border_width;
534 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
536 rc.left = c->rcView.left;
537 rc.right = c->rcView.right;
538 rc.top = y;
539 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
540 rc.bottom = y + bounds->top + top_border;
541 FillRect(c->hDC, &rc, c->editor->hbrBackground);
544 if (para->pFmt->dwMask & PFM_SPACEAFTER)
546 rc.left = c->rcView.left;
547 rc.right = c->rcView.right;
548 rc.bottom = y + para->nHeight;
549 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
550 rc.top = rc.bottom - bounds->bottom - bottom_border;
551 FillRect(c->hDC, &rc, c->editor->hbrBackground);
554 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
555 int pen_width;
556 COLORREF pencr;
557 HPEN pen = NULL, oldpen = NULL;
558 POINT pt;
560 if (para->pFmt->wBorders & 64) /* autocolor */
561 pencr = GetSysColor(COLOR_WINDOWTEXT);
562 else
563 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
565 pen_width = ME_GetBorderPenWidth(c->editor, idx);
566 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
567 oldpen = SelectObject(c->hDC, pen);
568 MoveToEx(c->hDC, 0, 0, &pt);
570 /* before & after spaces are not included in border */
572 /* helper to draw the double lines in case of corner */
573 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
575 if (para->pFmt->wBorders & 1)
577 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
578 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
579 if (border_details[idx].dble) {
580 rc.left = c->rcView.left + 1;
581 rc.right = rc.left + border_width;
582 rc.top = y + bounds->top;
583 rc.bottom = y + para->nHeight - bounds->bottom;
584 FillRect(c->hDC, &rc, c->editor->hbrBackground);
585 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
586 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
588 bounds->left += border_width;
590 if (para->pFmt->wBorders & 2)
592 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
593 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
594 if (border_details[idx].dble) {
595 rc.left = c->rcView.right - pen_width - 1;
596 rc.right = c->rcView.right - 1;
597 rc.top = y + bounds->top;
598 rc.bottom = y + para->nHeight - bounds->bottom;
599 FillRect(c->hDC, &rc, c->editor->hbrBackground);
600 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
601 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
603 bounds->right += border_width;
605 if (para->pFmt->wBorders & 4)
607 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
608 LineTo(c->hDC, c->rcView.right, y + bounds->top);
609 if (border_details[idx].dble) {
610 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
611 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
613 bounds->top += border_width;
615 if (para->pFmt->wBorders & 8)
617 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
618 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
619 if (border_details[idx].dble) {
620 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
621 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
623 bounds->bottom += border_width;
625 #undef DD
627 MoveToEx(c->hDC, pt.x, pt.y, NULL);
628 SelectObject(c->hDC, oldpen);
629 DeleteObject(pen);
633 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
634 int align = SetTextAlign(c->hDC, TA_BASELINE);
635 ME_DisplayItem *p;
636 ME_Run *run;
637 ME_Paragraph *para = NULL;
638 RECT rc, rcPara, bounds;
639 int y = c->pt.y;
640 int height = 0, baseline = 0, no=0, pno = 0;
641 int xs = 0, xe = 0;
642 BOOL visible = FALSE;
644 c->pt.x = c->rcView.left;
645 rcPara.left = c->rcView.left;
646 rcPara.right = c->rcView.right;
647 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
648 switch(p->type) {
649 case diParagraph:
650 para = &p->member.para;
651 assert(para);
652 pno = 0;
653 xs = c->rcView.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
654 xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
655 ME_DrawParaDecoration(c, para, y, &bounds);
656 y += bounds.top;
657 break;
658 case diStartRow:
659 /* we should have seen a diParagraph before */
660 assert(para);
661 y += height;
662 rcPara.top = y;
663 rcPara.bottom = y+p->member.row.nHeight;
664 visible = RectVisible(c->hDC, &rcPara);
665 if (visible) {
666 /* left margin */
667 rc.left = c->rcView.left + bounds.left;
668 rc.right = xs;
669 rc.top = y;
670 rc.bottom = y+p->member.row.nHeight;
671 FillRect(c->hDC, &rc, c->editor->hbrBackground);
672 /* right margin */
673 rc.left = xe;
674 rc.right = c->rcView.right - bounds.right;
675 FillRect(c->hDC, &rc, c->editor->hbrBackground);
676 rc.left = xs;
677 rc.right = xe;
678 FillRect(c->hDC, &rc, c->editor->hbrBackground);
680 if (me_debug)
682 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
683 WCHAR buf[128];
684 POINT pt = c->pt;
685 wsprintfW(buf, wszRowDebug, no);
686 pt.y = 12+y;
687 ME_DebugWrite(c->hDC, &pt, buf);
690 height = p->member.row.nHeight;
691 baseline = p->member.row.nBaseline;
692 if (!pno++)
693 xe += ME_twips2pointsX(c, para->pFmt->dxOffset);
694 break;
695 case diRun:
696 assert(para);
697 run = &p->member.run;
698 if (visible && me_debug) {
699 rc.left = c->rcView.left+run->pt.x;
700 rc.right = c->rcView.left+run->pt.x+run->nWidth;
701 rc.top = c->pt.y+run->pt.y;
702 rc.bottom = c->pt.y+run->pt.y+height;
703 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
704 if (run->nFlags & MERF_SKIPPED)
705 DrawFocusRect(c->hDC, &rc);
706 else
707 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
709 if (visible)
710 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
711 if (me_debug)
713 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
714 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
715 WCHAR buf[2560];
716 POINT pt;
717 pt.x = run->pt.x;
718 pt.y = c->pt.y + run->pt.y;
719 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
720 ME_DebugWrite(c->hDC, &pt, buf);
722 /* c->pt.x += p->member.run.nWidth; */
723 break;
724 default:
725 break;
727 no++;
729 SetTextAlign(c->hDC, align);
732 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
734 ME_Scroll(editor, absY, 1);
737 void ME_ScrollUp(ME_TextEditor *editor, int cy)
739 ME_Scroll(editor, cy, 2);
742 void ME_ScrollDown(ME_TextEditor *editor, int cy)
744 ME_Scroll(editor, cy, 3);
747 void ME_Scroll(ME_TextEditor *editor, int value, int type)
749 SCROLLINFO si;
750 int nOrigPos, nNewPos, nActualScroll;
752 nOrigPos = ME_GetYScrollPos(editor);
754 si.cbSize = sizeof(SCROLLINFO);
755 si.fMask = SIF_POS;
757 switch (type)
759 case 1:
760 /*Scroll absolutely*/
761 si.nPos = value;
762 break;
763 case 2:
764 /* Scroll up - towards the beginning of the document */
765 si.nPos = nOrigPos - value;
766 break;
767 case 3:
768 /* Scroll down - towards the end of the document */
769 si.nPos = nOrigPos + value;
770 break;
771 default:
772 FIXME("ME_Scroll called incorrectly\n");
773 si.nPos = 0;
776 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
777 nActualScroll = nOrigPos - nNewPos;
778 if (editor->bRedraw)
780 if (abs(nActualScroll) > editor->sizeWindow.cy)
781 InvalidateRect(editor->hWnd, NULL, TRUE);
782 else
783 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
784 ME_Repaint(editor);
787 editor->vert_si.nMax = 0;
788 ME_UpdateScrollBar(editor);
792 void ME_UpdateScrollBar(ME_TextEditor *editor)
794 /* Note that this is the only function that should ever call SetScrolLInfo
795 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
796 * be used at all. */
798 HWND hWnd;
799 SCROLLINFO si;
800 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
802 if (ME_WrapMarkedParagraphs(editor))
803 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
805 hWnd = editor->hWnd;
806 si.cbSize = sizeof(si);
807 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
808 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
810 si.fMask = SIF_PAGE | SIF_RANGE;
811 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
812 si.fMask |= SIF_DISABLENOSCROLL;
813 if ((si.fMask & SIF_DISABLENOSCROLL))
815 bScrollBarWillBeVisible = TRUE;
818 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
820 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
821 ME_MarkAllForWrapping(editor);
822 ME_WrapMarkedParagraphs(editor);
825 si.nMin = 0;
826 si.nMax = editor->nTotalLength;
828 si.nPage = editor->sizeWindow.cy;
830 if (!(si.nMin == editor->vert_si.nMin && si.nMax == editor->vert_si.nMax && si.nPage == editor->vert_si.nPage))
832 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
833 editor->vert_si.nMin = si.nMin;
834 editor->vert_si.nMax = si.nMax;
835 editor->vert_si.nPage = si.nPage;
836 if (bScrollBarWillBeVisible)
838 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
840 else
842 if (bScrollBarWasVisible && !(si.fMask & SIF_DISABLENOSCROLL))
843 ShowScrollBar(hWnd, SB_VERT, FALSE);
848 int ME_GetYScrollPos(ME_TextEditor *editor)
850 SCROLLINFO si;
851 si.cbSize = sizeof(si);
852 si.fMask = SIF_POS;
853 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
856 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
857 { /* Returns true if the scrollbar is visible */
858 return (editor->vert_si.nMax - editor->vert_si.nMin >= max(editor->vert_si.nPage - 1, 0));
861 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
863 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
864 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
865 int y, yrel, yheight, yold;
867 assert(pRow);
868 assert(pPara);
870 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
871 yheight = pRow->member.row.nHeight;
872 yold = ME_GetYScrollPos(editor);
873 yrel = y - yold;
875 if (y < yold)
876 ME_ScrollAbs(editor,y);
877 else if (yrel + yheight > editor->sizeWindow.cy)
878 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
882 void
883 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
885 RECT rc;
886 int x, y, height;
887 ME_Cursor tmp;
889 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
890 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
892 rc.left = 0;
893 rc.top = y;
894 rc.bottom = y + height;
895 rc.right = editor->rcFormat.right;
896 InvalidateRect(editor->hWnd, &rc, FALSE);
900 void
901 ME_InvalidateSelection(ME_TextEditor *editor)
903 ME_DisplayItem *para1, *para2;
904 int nStart, nEnd;
905 int len = ME_GetTextLength(editor);
907 ME_GetSelection(editor, &nStart, &nEnd);
908 /* if both old and new selection are 0-char (= caret only), then
909 there's no (inverted) area to be repainted, neither old nor new */
910 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
911 return;
912 ME_WrapMarkedParagraphs(editor);
913 ME_GetSelectionParas(editor, &para1, &para2);
914 assert(para1->type == diParagraph);
915 assert(para2->type == diParagraph);
916 /* last selection markers aren't always updated, which means
917 they can point past the end of the document */
918 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
919 ME_MarkForPainting(editor,
920 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
921 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
922 } else {
923 /* if the start part of selection is being expanded or contracted... */
924 if (nStart < editor->nLastSelStart) {
925 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
926 } else
927 if (nStart > editor->nLastSelStart) {
928 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
931 /* if the end part of selection is being contracted or expanded... */
932 if (nEnd < editor->nLastSelEnd) {
933 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
934 } else
935 if (nEnd > editor->nLastSelEnd) {
936 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
940 ME_InvalidateMarkedParagraphs(editor);
941 /* remember the last invalidated position */
942 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
943 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
944 assert(editor->pLastSelStartPara->type == diParagraph);
945 assert(editor->pLastSelEndPara->type == diParagraph);
948 void
949 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
951 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
955 BOOL
956 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
958 /* TODO: Zoom images and objects */
960 if (numerator != 0)
962 if (denominator == 0)
963 return FALSE;
964 if (1.0 / 64.0 > (float)numerator / (float)denominator
965 || (float)numerator / (float)denominator > 64.0)
966 return FALSE;
969 editor->nZoomNumerator = numerator;
970 editor->nZoomDenominator = denominator;
972 ME_RewrapRepaint(editor);
973 return TRUE;