wine.inf: Register msdaps.dll.
[wine/multimedia.git] / dlls / riched20 / paint.c
blob52127d7ed260852a4ff350dbc0fa92ac1556659a
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 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph);
28 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, const RECT *rcUpdate)
30 ME_DisplayItem *item;
31 ME_Context c;
32 int ys, ye;
33 HRGN oldRgn;
35 oldRgn = CreateRectRgn(0, 0, 0, 0);
36 if (!GetClipRgn(hDC, oldRgn))
38 DeleteObject(oldRgn);
39 oldRgn = NULL;
41 IntersectClipRect(hDC, rcUpdate->left, rcUpdate->top,
42 rcUpdate->right, rcUpdate->bottom);
44 editor->nSequence++;
45 ME_InitContext(&c, editor, hDC);
46 SetBkMode(hDC, TRANSPARENT);
47 ME_MoveCaret(editor);
48 item = editor->pBuffer->pFirst->next;
49 /* This context point is an offset for the paragraph positions stored
50 * during wrapping. It shouldn't be modified during painting. */
51 c.pt.x = c.rcView.left - editor->horz_si.nPos;
52 c.pt.y = c.rcView.top - editor->vert_si.nPos;
53 while(item != editor->pBuffer->pLast)
55 assert(item->type == diParagraph);
57 ys = c.pt.y + item->member.para.pt.y;
58 if (item->member.para.pCell
59 != item->member.para.next_para->member.para.pCell)
61 ME_Cell *cell = NULL;
62 cell = &ME_FindItemBack(item->member.para.next_para, diCell)->member.cell;
63 ye = c.pt.y + cell->pt.y + cell->nHeight;
64 } else {
65 ye = ys + item->member.para.nHeight;
67 if (item->member.para.pCell && !(item->member.para.nFlags & MEPF_ROWEND) &&
68 item->member.para.pCell != item->member.para.prev_para->member.para.pCell)
70 /* the border shifts the text down */
71 ys -= item->member.para.pCell->member.cell.yTextOffset;
74 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
76 /* Draw the pargraph if any of the paragraph is in the update region. */
77 if (ys < rcUpdate->bottom && ye > rcUpdate->top)
79 ME_DrawParagraph(&c, item);
80 /* Clear the repaint flag if the whole paragraph is in the
81 * update region. */
82 if (rcUpdate->top <= ys && rcUpdate->bottom >= ye)
83 item->member.para.nFlags &= ~MEPF_REPAINT;
86 item = item->member.para.next_para;
88 if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
90 /* Fill space after the end of the text. */
91 RECT rc;
92 rc.top = c.pt.y + editor->nTotalLength;
93 rc.left = c.rcView.left;
94 rc.bottom = c.rcView.bottom;
95 rc.right = c.rcView.right;
97 if (bOnlyNew)
99 /* Only erase region drawn from previous call to ME_PaintContent */
100 if (editor->nTotalLength < editor->nLastTotalLength)
101 rc.bottom = c.pt.y + editor->nLastTotalLength;
102 else
103 SetRectEmpty(&rc);
106 IntersectRect(&rc, &rc, rcUpdate);
108 if (!IsRectEmpty(&rc))
109 FillRect(hDC, &rc, c.editor->hbrBackground);
111 if (editor->nTotalLength != editor->nLastTotalLength ||
112 editor->nTotalWidth != editor->nLastTotalWidth)
113 ME_SendRequestResize(editor, FALSE);
114 editor->nLastTotalLength = editor->nTotalLength;
115 editor->nLastTotalWidth = editor->nTotalWidth;
117 SelectClipRgn(hDC, oldRgn);
118 if (oldRgn)
119 DeleteObject(oldRgn);
121 c.hDC = NULL;
122 ME_DestroyContext(&c);
125 void ME_Repaint(ME_TextEditor *editor)
127 if (ME_WrapMarkedParagraphs(editor))
129 ME_UpdateScrollBar(editor);
130 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
132 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
133 ME_SendOldNotify(editor, EN_UPDATE);
134 ITextHost_TxViewChange(editor->texthost, TRUE);
137 void ME_UpdateRepaint(ME_TextEditor *editor)
139 /* Should be called whenever the contents of the control have changed */
140 BOOL wrappedParagraphs;
142 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
143 if (wrappedParagraphs)
144 ME_UpdateScrollBar(editor);
146 /* Ensure that the cursor is visible */
147 ME_EnsureVisible(editor, &editor->pCursors[0]);
149 /* send EN_CHANGE if the event mask asks for it */
150 if(editor->nEventMask & ENM_CHANGE)
152 editor->nEventMask &= ~ENM_CHANGE;
153 ME_SendOldNotify(editor, EN_CHANGE);
154 editor->nEventMask |= ENM_CHANGE;
156 ME_Repaint(editor);
157 ME_SendSelChange(editor);
160 void
161 ME_RewrapRepaint(ME_TextEditor *editor)
163 /* RewrapRepaint should be called whenever the control has changed in
164 * looks, but not content. Like resizing. */
166 ME_MarkAllForWrapping(editor);
167 ME_WrapMarkedParagraphs(editor);
168 ME_UpdateScrollBar(editor);
169 ME_Repaint(editor);
172 int ME_twips2pointsX(ME_Context *c, int x)
174 if (c->editor->nZoomNumerator == 0)
175 return x * c->dpi.cx / 1440;
176 else
177 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
180 int ME_twips2pointsY(ME_Context *c, int y)
182 if (c->editor->nZoomNumerator == 0)
183 return y * c->dpi.cy / 1440;
184 else
185 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
188 static void ME_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
189 int nChars, ME_Style *s, int width,
190 int nSelFrom, int nSelTo, int ymin, int cy)
192 HDC hDC = c->hDC;
193 HGDIOBJ hOldFont = NULL;
194 SIZE sz;
195 int selWidth;
196 /* Only highlight if there is a selection in the run and when
197 * EM_HIDESELECTION is not being used to hide the selection. */
198 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
199 || c->editor->bHideSelection)
200 return;
201 hOldFont = ME_SelectStyleFont(c, s);
202 if (width <= 0)
204 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
205 width = sz.cx;
207 if (nSelFrom < 0) nSelFrom = 0;
208 if (nSelTo > nChars) nSelTo = nChars;
209 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
210 x += sz.cx;
211 if (nSelTo != nChars)
213 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
214 selWidth = sz.cx;
215 } else {
216 selWidth = width - sz.cx;
218 ME_UnselectStyleFont(c, s, hOldFont);
220 if (c->editor->bEmulateVersion10)
221 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
222 else
224 RECT rect;
225 HBRUSH hBrush;
226 rect.left = x;
227 rect.top = ymin;
228 rect.right = x + selWidth;
229 rect.bottom = ymin + cy;
230 hBrush = CreateSolidBrush(ITextHost_TxGetSysColor(c->editor->texthost,
231 COLOR_HIGHLIGHT));
232 FillRect(hDC, &rect, hBrush);
233 DeleteObject(hBrush);
237 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
238 int nChars, ME_Style *s, int width,
239 int nSelFrom, int nSelTo, int ymin, int cy)
241 HDC hDC = c->hDC;
242 HGDIOBJ hOldFont;
243 COLORREF rgbOld;
244 int yOffset = 0, yTwipsOffset = 0;
245 SIZE sz;
246 COLORREF rgb;
247 HPEN hPen = NULL, hOldPen = NULL;
248 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
249 && nSelFrom < nSelTo && !c->editor->bHideSelection);
250 int xSelStart = x, xSelEnd = x;
251 int *lpDx = NULL;
252 /* lpDx is only needed for tabs to make sure the underline done automatically
253 * by the font extends to the end of the tab. Tabs are always stored as
254 * a single character run, so we can handle this case separately, since
255 * otherwise lpDx would need to specify the lengths of each character. */
256 if (width && nChars == 1)
257 lpDx = &width; /* Make sure underline for tab extends across tab space */
259 hOldFont = ME_SelectStyleFont(c, s);
260 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
261 yTwipsOffset = s->fmt.yOffset;
263 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
264 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
265 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
267 if (yTwipsOffset)
268 yOffset = ME_twips2pointsY(c, yTwipsOffset);
270 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
271 rgb = RGB(0,0,255);
272 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
273 rgb = ITextHost_TxGetSysColor(c->editor->texthost, COLOR_WINDOWTEXT);
274 else
275 rgb = s->fmt.crTextColor;
277 /* Determine the area that is selected in the run. */
278 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
279 /* Treat width as an optional parameter. We can get the width from the
280 * text extent of the string if it isn't specified. */
281 if (!width) width = sz.cx;
282 if (bHighlightedText)
284 if (nSelFrom <= 0)
286 nSelFrom = 0;
288 else
290 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
291 xSelStart = x + sz.cx;
293 if (nSelTo >= nChars)
295 nSelTo = nChars;
296 xSelEnd = x + width;
298 else
300 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
301 xSelEnd = xSelStart + sz.cx;
305 /* Choose the pen type for underlining the text. */
306 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
308 switch (s->fmt.bUnderlineType)
310 case CFU_UNDERLINE:
311 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
312 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
313 hPen = CreatePen(PS_SOLID, 1, rgb);
314 break;
315 case CFU_UNDERLINEDOTTED:
316 hPen = CreatePen(PS_DOT, 1, rgb);
317 break;
318 default:
319 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
320 /* fall through */
321 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
322 case CFU_UNDERLINENONE:
323 hPen = NULL;
324 break;
326 if (hPen)
328 hOldPen = SelectObject(hDC, hPen);
332 rgbOld = SetTextColor(hDC, rgb);
333 if (bHighlightedText && !c->editor->bEmulateVersion10)
335 COLORREF rgbBackOld;
336 RECT dim;
337 /* FIXME: should use textmetrics info for Descent info */
338 if (hPen)
339 MoveToEx(hDC, x, y - yOffset + 1, NULL);
340 if (xSelStart > x)
342 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
343 if (hPen)
344 LineTo(hDC, xSelStart, y - yOffset + 1);
346 dim.top = ymin;
347 dim.bottom = ymin + cy;
348 dim.left = xSelStart;
349 dim.right = xSelEnd;
350 SetTextColor(hDC, ITextHost_TxGetSysColor(c->editor->texthost,
351 COLOR_HIGHLIGHTTEXT));
352 rgbBackOld = SetBkColor(hDC, ITextHost_TxGetSysColor(c->editor->texthost,
353 COLOR_HIGHLIGHT));
354 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
355 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
356 if (hPen)
357 LineTo(hDC, xSelEnd, y - yOffset + 1);
358 SetBkColor(hDC, rgbBackOld);
359 if (xSelEnd < x + width)
361 SetTextColor(hDC, rgb);
362 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
363 nChars-nSelTo, NULL);
364 if (hPen)
365 LineTo(hDC, x + width, y - yOffset + 1);
368 else
370 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
372 /* FIXME: should use textmetrics info for Descent info */
373 if (hPen)
375 MoveToEx(hDC, x, y - yOffset + 1, NULL);
376 LineTo(hDC, x + width, y - yOffset + 1);
379 if (bHighlightedText) /* v1.0 inverts the selection */
381 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
385 if (hPen)
387 SelectObject(hDC, hOldPen);
388 DeleteObject(hPen);
390 SetTextColor(hDC, rgbOld);
391 ME_UnselectStyleFont(c, s, hOldFont);
394 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
395 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
396 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
397 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
398 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
399 SelectObject(hDC, hFont);
400 SetTextAlign(hDC, align);
401 SetTextColor(hDC, color);
404 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
406 ME_Run *run = &rundi->member.run;
407 ME_DisplayItem *start;
408 int runofs = run->nCharOfs+para->nCharOfs;
409 int nSelFrom, nSelTo;
410 const WCHAR wszSpace[] = {' ', 0};
412 if (run->nFlags & MERF_HIDDEN)
413 return;
415 start = ME_FindItemBack(rundi, diStartRow);
416 ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
418 /* Draw selected end-of-paragraph mark */
419 if (run->nFlags & MERF_ENDPARA)
421 if (runofs >= nSelFrom && runofs < nSelTo)
423 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
424 c->pt.y + para->pt.y + start->member.row.pt.y,
425 start->member.row.nHeight);
427 return;
430 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
432 /* wszSpace is used instead of the tab character because otherwise
433 * an unwanted symbol can be inserted instead. */
434 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
435 nSelFrom-runofs, nSelTo-runofs,
436 c->pt.y + para->pt.y + start->member.row.pt.y,
437 start->member.row.nHeight);
438 return;
441 if (run->nFlags & MERF_GRAPHICS)
442 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
443 else
445 if (c->editor->cPasswordMask)
447 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask, run->strText->nLen);
448 ME_DrawTextWithStyle(c, x, y,
449 szMasked->szData, szMasked->nLen, run->style, run->nWidth,
450 nSelFrom-runofs,nSelTo-runofs,
451 c->pt.y + para->pt.y + start->member.row.pt.y,
452 start->member.row.nHeight);
453 ME_DestroyString(szMasked);
455 else
456 ME_DrawTextWithStyle(c, x, y,
457 run->strText->szData, run->strText->nLen, run->style, run->nWidth,
458 nSelFrom-runofs,nSelTo-runofs,
459 c->pt.y + para->pt.y + start->member.row.pt.y,
460 start->member.row.nHeight);
464 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
465 /* none */ {0, 1, PS_SOLID, FALSE},
466 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
467 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
468 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
469 /* 3 */ {3, 1, PS_SOLID, FALSE},
470 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
471 /* 6 */ {6, 1, PS_SOLID, FALSE},
472 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
473 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
474 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
475 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
476 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
479 static const COLORREF pen_colors[16] = {
480 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
481 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
482 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
483 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
484 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
485 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
486 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
487 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
490 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
492 int width;
494 if (editor->nZoomNumerator == 0)
496 width = border_details[idx].width_num + border_details[idx].width_den / 2;
497 width /= border_details[idx].width_den;
499 else
501 width = border_details[idx].width_num * editor->nZoomNumerator;
502 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
503 width /= border_details[idx].width_den * editor->nZoomDenominator;
505 return width;
508 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
510 int idx = (flags >> 8) & 0xF;
511 int width;
513 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
515 FIXME("Unsupported border value %d\n", idx);
516 return 0;
518 width = ME_GetBorderPenWidth(editor, idx);
519 if (border_details[idx].dble) width = width * 2 + 1;
520 return width;
523 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
525 int sp = 0, ls = 0;
526 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
528 /* FIXME: how to compute simply the line space in ls ??? */
529 /* FIXME: does line spacing include the line itself ??? */
530 switch (para->pFmt->bLineSpacingRule)
532 case 0: sp = ls; break;
533 case 1: sp = (3 * ls) / 2; break;
534 case 2: sp = 2 * ls; break;
535 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
536 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
537 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
538 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
540 if (c->editor->nZoomNumerator == 0)
541 return sp;
542 else
543 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
546 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
548 int idx, border_width, top_border, bottom_border;
549 RECT rc;
550 BOOL hasParaBorder;
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 hasParaBorder = (!(c->editor->bEmulateVersion10 &&
558 para->pFmt->dwMask & PFM_TABLE &&
559 para->pFmt->wEffects & PFE_TABLE) &&
560 (para->pFmt->dwMask & PFM_BORDER) &&
561 idx != 0 &&
562 (para->pFmt->wBorders & 0xF));
563 if (hasParaBorder)
565 /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
566 * controls. It actually stores the paragraph or row border style. Although
567 * the value isn't used for drawing, it is used for streaming out rich text.
569 * wBorders stores the border style for each side (top, left, bottom, right)
570 * using nibble (4 bits) to store each border style. The rich text format
571 * control words, and their associated value are the following:
572 * \brdrdash 0
573 * \brdrdashsm 1
574 * \brdrdb 2
575 * \brdrdot 3
576 * \brdrhair 4
577 * \brdrs 5
578 * \brdrth 6
579 * \brdrtriple 7
581 * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
582 * The mask corresponding to each side for the version are the following:
583 * mask v1.0-3.0 v4.1
584 * 0x000F top left
585 * 0x00F0 left top
586 * 0x0F00 bottom right
587 * 0xF000 right bottom
589 if (para->pFmt->wBorders & 0x00B0)
590 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
591 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
592 if (para->pFmt->wBorders & 4) top_border = border_width;
593 if (para->pFmt->wBorders & 8) bottom_border = border_width;
596 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
598 rc.left = c->rcView.left;
599 rc.right = c->rcView.right;
600 rc.top = y;
601 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
602 rc.bottom = y + bounds->top + top_border;
603 FillRect(c->hDC, &rc, c->editor->hbrBackground);
606 if (para->pFmt->dwMask & PFM_SPACEAFTER)
608 rc.left = c->rcView.left;
609 rc.right = c->rcView.right;
610 rc.bottom = y + para->nHeight;
611 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
612 rc.top = rc.bottom - bounds->bottom - bottom_border;
613 FillRect(c->hDC, &rc, c->editor->hbrBackground);
616 /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
617 * but might support it in later versions. */
618 if (hasParaBorder) {
619 int pen_width, rightEdge;
620 COLORREF pencr;
621 HPEN pen = NULL, oldpen = NULL;
622 POINT pt;
624 if (para->pFmt->wBorders & 64) /* autocolor */
625 pencr = ITextHost_TxGetSysColor(c->editor->texthost,
626 COLOR_WINDOWTEXT);
627 else
628 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
630 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
631 c->editor->nTotalWidth);
633 pen_width = ME_GetBorderPenWidth(c->editor, idx);
634 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
635 oldpen = SelectObject(c->hDC, pen);
636 MoveToEx(c->hDC, 0, 0, &pt);
638 /* before & after spaces are not included in border */
640 /* helper to draw the double lines in case of corner */
641 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
643 if (para->pFmt->wBorders & 1)
645 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
646 LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
647 if (border_details[idx].dble) {
648 rc.left = c->pt.x + 1;
649 rc.right = rc.left + border_width;
650 rc.top = y + bounds->top;
651 rc.bottom = y + para->nHeight - bounds->bottom;
652 FillRect(c->hDC, &rc, c->editor->hbrBackground);
653 MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
654 LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
656 bounds->left += border_width;
658 if (para->pFmt->wBorders & 2)
660 MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
661 LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
662 if (border_details[idx].dble) {
663 rc.left = rightEdge - pen_width - 1;
664 rc.right = rc.left + pen_width;
665 rc.top = y + bounds->top;
666 rc.bottom = y + para->nHeight - bounds->bottom;
667 FillRect(c->hDC, &rc, c->editor->hbrBackground);
668 MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
669 LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
671 bounds->right += border_width;
673 if (para->pFmt->wBorders & 4)
675 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
676 LineTo(c->hDC, rightEdge, y + bounds->top);
677 if (border_details[idx].dble) {
678 MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
679 LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
681 bounds->top += border_width;
683 if (para->pFmt->wBorders & 8)
685 MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
686 LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
687 if (border_details[idx].dble) {
688 MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
689 LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
691 bounds->bottom += border_width;
693 #undef DD
695 MoveToEx(c->hDC, pt.x, pt.y, NULL);
696 SelectObject(c->hDC, oldpen);
697 DeleteObject(pen);
701 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
703 ME_Paragraph *para = &paragraph->member.para;
704 if (!c->editor->bEmulateVersion10) /* v4.1 */
706 if (para->pCell)
708 RECT rc;
709 ME_Cell *cell = &para->pCell->member.cell;
710 ME_DisplayItem *paraAfterRow;
711 HPEN pen, oldPen;
712 LOGBRUSH logBrush;
713 HBRUSH brush;
714 COLORREF color;
715 POINT oldPt;
716 int width;
717 BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
718 BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
719 int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
720 int bottom = (atBottom ?
721 c->pt.y + cell->pt.y + cell->nHeight :
722 top + para->nHeight + (atTop ? cell->yTextOffset : 0));
723 rc.left = c->pt.x + cell->pt.x;
724 rc.right = rc.left + cell->nWidth;
725 if (atTop) {
726 /* Erase gap before text if not all borders are the same height. */
727 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
728 rc.top = top + width;
729 width = cell->yTextOffset - width;
730 rc.bottom = rc.top + width;
731 if (width) {
732 FillRect(c->hDC, &rc, c->editor->hbrBackground);
735 /* Draw cell borders.
736 * The order borders are draw in is left, top, bottom, right in order
737 * to be consistent with native richedit. This is noticeable from the
738 * overlap of borders of different colours. */
739 if (!(para->nFlags & MEPF_ROWEND)) {
740 rc.top = top;
741 rc.bottom = bottom;
742 if (cell->border.left.width > 0)
744 color = cell->border.left.colorRef;
745 width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
746 } else {
747 color = RGB(192,192,192);
748 width = 1;
750 logBrush.lbStyle = BS_SOLID;
751 logBrush.lbColor = color;
752 logBrush.lbHatch = 0;
753 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
754 width, &logBrush, 0, NULL);
755 oldPen = SelectObject(c->hDC, pen);
756 MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
757 LineTo(c->hDC, rc.left, rc.bottom);
758 SelectObject(c->hDC, oldPen);
759 DeleteObject(pen);
760 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
763 if (atTop) {
764 if (cell->border.top.width > 0)
766 brush = CreateSolidBrush(cell->border.top.colorRef);
767 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
768 } else {
769 brush = GetStockObject(LTGRAY_BRUSH);
770 width = 1;
772 rc.top = top;
773 rc.bottom = rc.top + width;
774 FillRect(c->hDC, &rc, brush);
775 if (cell->border.top.width > 0)
776 DeleteObject(brush);
779 /* Draw the bottom border if at the last paragraph in the cell, and when
780 * in the last row of the table. */
781 if (atBottom) {
782 int oldLeft = rc.left;
783 width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
784 paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
785 if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
786 ME_DisplayItem *nextEndCell;
787 nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
788 assert(nextEndCell && !nextEndCell->member.cell.next_cell);
789 rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
790 /* FIXME: Native draws FROM the bottom of the table rather than
791 * TO the bottom of the table in this case, but just doing so here
792 * will cause the next row to erase the border. */
794 rc.top = bottom;
795 rc.bottom = rc.top + width;
798 if (rc.left < rc.right) {
799 if (cell->border.bottom.width > 0) {
800 brush = CreateSolidBrush(cell->border.bottom.colorRef);
801 } else {
802 brush = GetStockObject(LTGRAY_BRUSH);
804 rc.bottom = bottom;
805 rc.top = rc.bottom - width;
806 FillRect(c->hDC, &rc, brush);
807 if (cell->border.bottom.width > 0)
808 DeleteObject(brush);
810 rc.left = oldLeft;
813 /* Right border only drawn if at the end of the table row. */
814 if (!cell->next_cell->member.cell.next_cell &&
815 !(para->nFlags & MEPF_ROWSTART))
817 rc.top = top;
818 rc.bottom = bottom;
819 if (cell->border.right.width > 0) {
820 color = cell->border.right.colorRef;
821 width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
822 } else {
823 color = RGB(192,192,192);
824 width = 1;
826 logBrush.lbStyle = BS_SOLID;
827 logBrush.lbColor = color;
828 logBrush.lbHatch = 0;
829 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
830 width, &logBrush, 0, NULL);
831 oldPen = SelectObject(c->hDC, pen);
832 MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
833 LineTo(c->hDC, rc.right - 1, rc.bottom);
834 SelectObject(c->hDC, oldPen);
835 DeleteObject(pen);
836 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
839 } else { /* v1.0 - 3.0 */
840 /* Draw simple table border */
841 if (para->pFmt->dwMask & PFM_TABLE && para->pFmt->wEffects & PFE_TABLE) {
842 HPEN pen = NULL, oldpen = NULL;
843 int i, firstX, startX, endX, rowY, rowBottom, nHeight;
844 POINT oldPt;
845 PARAFORMAT2 *pNextFmt;
847 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
848 oldpen = SelectObject(c->hDC, pen);
850 /* Find the start relative to the text */
851 firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
852 /* Go back by the horizontal gap, which is stored in dxOffset */
853 firstX -= ME_twips2pointsX(c, para->pFmt->dxOffset);
854 /* The left edge, stored in dxStartIndent affected just the first edge */
855 startX = firstX - ME_twips2pointsX(c, para->pFmt->dxStartIndent);
856 rowY = c->pt.y + para->pt.y;
857 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
858 rowY += ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
859 nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
860 rowBottom = rowY + nHeight;
862 /* Draw horizontal lines */
863 MoveToEx(c->hDC, firstX, rowY, &oldPt);
864 i = para->pFmt->cTabCount - 1;
865 endX = startX + ME_twips2pointsX(c, para->pFmt->rgxTabs[i] & 0x00ffffff) + 1;
866 LineTo(c->hDC, endX, rowY);
867 pNextFmt = para->next_para->member.para.pFmt;
868 /* The bottom of the row only needs to be drawn if the next row is
869 * not a table. */
870 if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
871 para->nRows == 1))
873 /* Decrement rowBottom to draw the bottom line within the row, and
874 * to not draw over this line when drawing the vertical lines. */
875 rowBottom--;
876 MoveToEx(c->hDC, firstX, rowBottom, NULL);
877 LineTo(c->hDC, endX, rowBottom);
880 /* Draw vertical lines */
881 MoveToEx(c->hDC, firstX, rowY, NULL);
882 LineTo(c->hDC, firstX, rowBottom);
883 for (i = 0; i < para->pFmt->cTabCount; i++)
885 int rightBoundary = para->pFmt->rgxTabs[i] & 0x00ffffff;
886 endX = startX + ME_twips2pointsX(c, rightBoundary);
887 MoveToEx(c->hDC, endX, rowY, NULL);
888 LineTo(c->hDC, endX, rowBottom);
891 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
892 SelectObject(c->hDC, oldpen);
893 DeleteObject(pen);
898 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
900 int align = SetTextAlign(c->hDC, TA_BASELINE);
901 ME_DisplayItem *p;
902 ME_Run *run;
903 ME_Paragraph *para = NULL;
904 RECT rc, bounds;
905 int y;
906 int height = 0, baseline = 0, no=0;
907 BOOL visible = FALSE;
909 rc.left = c->pt.x;
910 rc.right = c->rcView.right;
912 assert(paragraph);
913 para = &paragraph->member.para;
914 y = c->pt.y + para->pt.y;
915 if (para->pCell)
917 ME_Cell *cell = &para->pCell->member.cell;
918 rc.left = c->pt.x + cell->pt.x;
919 rc.right = rc.left + cell->nWidth;
921 if (para->nFlags & MEPF_ROWSTART) {
922 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
923 rc.right = c->pt.x + cell->pt.x;
924 } else if (para->nFlags & MEPF_ROWEND) {
925 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
926 rc.left = c->pt.x + cell->pt.x + cell->nWidth;
928 ME_DrawParaDecoration(c, para, y, &bounds);
929 y += bounds.top;
930 if (bounds.left || bounds.right) {
931 rc.left = max(rc.left, c->pt.x + bounds.left);
932 rc.right = min(rc.right, c->pt.x - bounds.right
933 + max(c->editor->sizeWindow.cx,
934 c->editor->nTotalWidth));
937 for (p = paragraph->next; p != para->next_para; p = p->next)
939 switch(p->type) {
940 case diParagraph:
941 assert(FALSE);
942 break;
943 case diStartRow:
944 y += height;
945 rc.top = y;
946 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
947 rc.bottom = y + para->nHeight;
948 } else {
949 rc.bottom = y + p->member.row.nHeight;
951 visible = RectVisible(c->hDC, &rc);
952 if (visible) {
953 FillRect(c->hDC, &rc, c->editor->hbrBackground);
955 if (bounds.right)
957 /* If scrolled to the right past the end of the text, then
958 * there may be space to the right of the paragraph border. */
959 RECT rcAfterBrdr = rc;
960 rcAfterBrdr.left = rc.right + bounds.right;
961 rcAfterBrdr.right = c->rcView.right;
962 if (RectVisible(c->hDC, &rcAfterBrdr))
963 FillRect(c->hDC, &rcAfterBrdr, c->editor->hbrBackground);
965 if (me_debug)
967 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
968 WCHAR buf[128];
969 POINT pt = c->pt;
970 wsprintfW(buf, wszRowDebug, no);
971 pt.y = 12+y;
972 ME_DebugWrite(c->hDC, &pt, buf);
975 height = p->member.row.nHeight;
976 baseline = p->member.row.nBaseline;
977 break;
978 case diRun:
979 assert(para);
980 run = &p->member.run;
981 if (visible && me_debug) {
982 RECT rc;
983 rc.left = c->pt.x + run->pt.x;
984 rc.right = rc.left + run->nWidth;
985 rc.top = c->pt.y + para->pt.y + run->pt.y;
986 rc.bottom = rc.bottom + height;
987 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
988 if (run->nFlags & MERF_SKIPPED)
989 DrawFocusRect(c->hDC, &rc);
990 else
991 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
993 if (visible)
994 ME_DrawRun(c, c->pt.x + run->pt.x,
995 c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
996 if (me_debug)
998 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
999 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
1000 WCHAR buf[2560];
1001 POINT pt;
1002 pt.x = c->pt.x + run->pt.x;
1003 pt.y = c->pt.y + para->pt.y + run->pt.y;
1004 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
1005 ME_DebugWrite(c->hDC, &pt, buf);
1007 break;
1008 case diCell:
1009 /* Clear any space at the bottom of the cell after the text. */
1010 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
1011 break;
1012 y += height;
1013 rc.top = c->pt.y + para->pt.y + para->nHeight;
1014 rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
1015 if (RectVisible(c->hDC, &rc))
1017 FillRect(c->hDC, &rc, c->editor->hbrBackground);
1019 break;
1020 default:
1021 break;
1023 no++;
1026 ME_DrawTableBorders(c, paragraph);
1028 SetTextAlign(c->hDC, align);
1031 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1033 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1034 int scrollX = 0, scrollY = 0;
1036 if (editor->horz_si.nPos != x) {
1037 x = min(x, editor->horz_si.nMax);
1038 x = max(x, editor->horz_si.nMin);
1039 scrollX = editor->horz_si.nPos - x;
1040 editor->horz_si.nPos = x;
1041 if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
1042 x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
1043 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1046 if (editor->vert_si.nPos != y) {
1047 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1048 y = max(y, editor->vert_si.nMin);
1049 scrollY = editor->vert_si.nPos - y;
1050 editor->vert_si.nPos = y;
1051 if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1052 y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1053 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1056 if (abs(scrollX) > editor->sizeWindow.cx ||
1057 abs(scrollY) > editor->sizeWindow.cy)
1058 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1059 else
1060 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1061 &editor->rcFormat, &editor->rcFormat,
1062 NULL, NULL, SW_INVALIDATE);
1063 ME_Repaint(editor);
1065 if (editor->hWnd)
1067 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1068 if (editor->styleFlags & WS_HSCROLL)
1070 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1071 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1072 && (editor->styleFlags & WS_HSCROLL))
1073 || (editor->styleFlags & ES_DISABLENOSCROLL);
1074 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1075 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1076 bScrollBarWillBeVisible);
1079 if (editor->styleFlags & WS_VSCROLL)
1081 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1082 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1083 && (editor->styleFlags & WS_VSCROLL)
1084 && (editor->styleFlags & ES_MULTILINE))
1085 || (editor->styleFlags & ES_DISABLENOSCROLL);
1086 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1087 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1088 bScrollBarWillBeVisible);
1091 ME_UpdateScrollBar(editor);
1094 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1096 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1099 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1101 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1104 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1106 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1109 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1111 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1114 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1116 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1119 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1121 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1124 /* Calculates the visiblity after a call to SetScrollRange or
1125 * SetScrollInfo with SIF_RANGE. */
1126 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1128 if (si->fMask & SIF_DISABLENOSCROLL)
1129 return TRUE;
1131 /* This must match the check in SetScrollInfo to determine whether
1132 * to show or hide the scrollbars. */
1133 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1136 void ME_UpdateScrollBar(ME_TextEditor *editor)
1138 /* Note that this is the only function that should ever call
1139 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1141 SCROLLINFO si;
1142 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1144 if (ME_WrapMarkedParagraphs(editor))
1145 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1147 si.cbSize = sizeof(si);
1148 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1149 si.nMin = 0;
1150 if (editor->styleFlags & ES_DISABLENOSCROLL)
1151 si.fMask |= SIF_DISABLENOSCROLL;
1153 /* Update horizontal scrollbar */
1154 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1155 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1156 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1158 ME_HScrollAbs(editor, 0);
1159 /* ME_HScrollAbs will call this function,
1160 * so nothing else needs to be done here. */
1161 return;
1164 si.nMax = editor->nTotalWidth;
1165 si.nPos = editor->horz_si.nPos;
1166 si.nPage = editor->sizeWindow.cx;
1168 if (si.nMax != editor->horz_si.nMax ||
1169 si.nPage != editor->horz_si.nPage)
1171 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1172 editor->horz_si.nMax = si.nMax;
1173 editor->horz_si.nPage = si.nPage;
1174 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1175 editor->styleFlags & WS_HSCROLL)
1177 if (si.nMax > 0xFFFF)
1179 /* Native scales the scrollbar info to 16-bit external values. */
1180 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1181 si.nMax = 0xFFFF;
1183 if (editor->hWnd) {
1184 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1185 } else {
1186 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1187 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1189 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1190 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1194 if (editor->styleFlags & WS_HSCROLL)
1196 if (si.fMask & SIF_DISABLENOSCROLL) {
1197 bScrollBarWillBeVisible = TRUE;
1198 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1199 bScrollBarWillBeVisible = FALSE;
1202 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1203 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1206 /* Update vertical scrollbar */
1207 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1208 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1209 (editor->styleFlags & ES_MULTILINE);
1211 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1213 ME_VScrollAbs(editor, 0);
1214 /* ME_VScrollAbs will call this function,
1215 * so nothing else needs to be done here. */
1216 return;
1219 si.nMax = editor->nTotalLength;
1220 si.nPos = editor->vert_si.nPos;
1221 si.nPage = editor->sizeWindow.cy;
1223 if (si.nMax != editor->vert_si.nMax ||
1224 si.nPage != editor->vert_si.nPage)
1226 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1227 editor->vert_si.nMax = si.nMax;
1228 editor->vert_si.nPage = si.nPage;
1229 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1230 editor->styleFlags & WS_VSCROLL)
1232 if (si.nMax > 0xFFFF)
1234 /* Native scales the scrollbar info to 16-bit external values. */
1235 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1236 si.nMax = 0xFFFF;
1238 if (editor->hWnd) {
1239 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1240 } else {
1241 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1242 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1244 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1245 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1249 if (editor->styleFlags & WS_VSCROLL)
1251 if (si.fMask & SIF_DISABLENOSCROLL) {
1252 bScrollBarWillBeVisible = TRUE;
1253 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1254 bScrollBarWillBeVisible = FALSE;
1257 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1258 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1259 bScrollBarWillBeVisible);
1263 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1265 ME_Run *pRun = &pCursor->pRun->member.run;
1266 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1267 ME_DisplayItem *pPara = pCursor->pPara;
1268 int x, y, yheight;
1270 assert(pRow);
1271 assert(pPara);
1273 if (editor->styleFlags & ES_AUTOHSCROLL)
1275 x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset);
1276 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1277 x = x + 1 - editor->sizeWindow.cx;
1278 else if (x > editor->horz_si.nPos)
1279 x = editor->horz_si.nPos;
1281 if (~editor->styleFlags & ES_AUTOVSCROLL)
1283 ME_HScrollAbs(editor, x);
1284 return;
1286 } else {
1287 if (~editor->styleFlags & ES_AUTOVSCROLL)
1288 return;
1289 x = editor->horz_si.nPos;
1292 y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1293 yheight = pRow->member.row.nHeight;
1295 if (y < editor->vert_si.nPos)
1296 ME_ScrollAbs(editor, x, y);
1297 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1298 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1299 else if (x != editor->horz_si.nPos)
1300 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1304 void
1305 ME_InvalidateSelection(ME_TextEditor *editor)
1307 ME_DisplayItem *para1, *para2;
1308 int nStart, nEnd;
1309 int len = ME_GetTextLength(editor);
1311 ME_GetSelectionOfs(editor, &nStart, &nEnd);
1312 /* if both old and new selection are 0-char (= caret only), then
1313 there's no (inverted) area to be repainted, neither old nor new */
1314 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1315 return;
1316 ME_WrapMarkedParagraphs(editor);
1317 ME_GetSelectionParas(editor, &para1, &para2);
1318 assert(para1->type == diParagraph);
1319 assert(para2->type == diParagraph);
1320 /* last selection markers aren't always updated, which means
1321 * they can point past the end of the document */
1322 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1323 ME_MarkForPainting(editor,
1324 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
1325 editor->pBuffer->pLast);
1326 } else {
1327 /* if the start part of selection is being expanded or contracted... */
1328 if (nStart < editor->nLastSelStart) {
1329 ME_MarkForPainting(editor, para1, editor->pLastSelStartPara->member.para.next_para);
1330 } else if (nStart > editor->nLastSelStart) {
1331 ME_MarkForPainting(editor, editor->pLastSelStartPara, para1->member.para.next_para);
1334 /* if the end part of selection is being contracted or expanded... */
1335 if (nEnd < editor->nLastSelEnd) {
1336 ME_MarkForPainting(editor, para2, editor->pLastSelEndPara->member.para.next_para);
1337 } else if (nEnd > editor->nLastSelEnd) {
1338 ME_MarkForPainting(editor, editor->pLastSelEndPara, para2->member.para.next_para);
1342 ME_InvalidateMarkedParagraphs(editor);
1343 /* remember the last invalidated position */
1344 ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1345 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1346 assert(editor->pLastSelStartPara->type == diParagraph);
1347 assert(editor->pLastSelEndPara->type == diParagraph);
1350 BOOL
1351 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1353 /* TODO: Zoom images and objects */
1355 if (numerator == 0 && denominator == 0)
1357 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1358 return TRUE;
1360 if (numerator <= 0 || denominator <= 0)
1361 return FALSE;
1362 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1363 return FALSE;
1365 editor->nZoomNumerator = numerator;
1366 editor->nZoomDenominator = denominator;
1368 ME_RewrapRepaint(editor);
1369 return TRUE;