[RICHED20]
[reactos.git] / reactos / dll / win32 / riched20 / paint.c
1 /*
2 * RichEdit - painting functions
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "editor.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25
26 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph);
27
28 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, const RECT *rcUpdate)
29 {
30 ME_DisplayItem *item;
31 ME_Context c;
32 int ys, ye;
33 HRGN oldRgn;
34
35 oldRgn = CreateRectRgn(0, 0, 0, 0);
36 if (!GetClipRgn(hDC, oldRgn))
37 {
38 DeleteObject(oldRgn);
39 oldRgn = NULL;
40 }
41 IntersectClipRect(hDC, rcUpdate->left, rcUpdate->top,
42 rcUpdate->right, rcUpdate->bottom);
43
44 ME_InitContext(&c, editor, hDC);
45 SetBkMode(hDC, TRANSPARENT);
46 ME_MoveCaret(editor);
47 item = editor->pBuffer->pFirst->next;
48 /* This context point is an offset for the paragraph positions stored
49 * during wrapping. It shouldn't be modified during painting. */
50 c.pt.x = c.rcView.left - editor->horz_si.nPos;
51 c.pt.y = c.rcView.top - editor->vert_si.nPos;
52 while(item != editor->pBuffer->pLast)
53 {
54 assert(item->type == diParagraph);
55
56 ys = c.pt.y + item->member.para.pt.y;
57 if (item->member.para.pCell
58 != item->member.para.next_para->member.para.pCell)
59 {
60 ME_Cell *cell = NULL;
61 cell = &ME_FindItemBack(item->member.para.next_para, diCell)->member.cell;
62 ye = c.pt.y + cell->pt.y + cell->nHeight;
63 } else {
64 ye = ys + item->member.para.nHeight;
65 }
66 if (item->member.para.pCell && !(item->member.para.nFlags & MEPF_ROWEND) &&
67 item->member.para.pCell != item->member.para.prev_para->member.para.pCell)
68 {
69 /* the border shifts the text down */
70 ys -= item->member.para.pCell->member.cell.yTextOffset;
71 }
72
73 /* Draw the paragraph if any of the paragraph is in the update region. */
74 if (ys < rcUpdate->bottom && ye > rcUpdate->top)
75 ME_DrawParagraph(&c, item);
76 item = item->member.para.next_para;
77 }
78 if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
79 {
80 /* Fill space after the end of the text. */
81 RECT rc;
82 rc.top = c.pt.y + editor->nTotalLength;
83 rc.left = c.rcView.left;
84 rc.bottom = c.rcView.bottom;
85 rc.right = c.rcView.right;
86
87 IntersectRect(&rc, &rc, rcUpdate);
88
89 if (!IsRectEmpty(&rc))
90 FillRect(hDC, &rc, c.editor->hbrBackground);
91 }
92 if (editor->nTotalLength != editor->nLastTotalLength ||
93 editor->nTotalWidth != editor->nLastTotalWidth)
94 ME_SendRequestResize(editor, FALSE);
95 editor->nLastTotalLength = editor->nTotalLength;
96 editor->nLastTotalWidth = editor->nTotalWidth;
97
98 SelectClipRgn(hDC, oldRgn);
99 if (oldRgn)
100 DeleteObject(oldRgn);
101
102 c.hDC = NULL;
103 ME_DestroyContext(&c);
104 }
105
106 void ME_Repaint(ME_TextEditor *editor)
107 {
108 if (ME_WrapMarkedParagraphs(editor))
109 {
110 ME_UpdateScrollBar(editor);
111 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
112 }
113 ITextHost_TxViewChange(editor->texthost, TRUE);
114 }
115
116 void ME_UpdateRepaint(ME_TextEditor *editor, BOOL update_now)
117 {
118 /* Should be called whenever the contents of the control have changed */
119 BOOL wrappedParagraphs;
120
121 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
122 if (wrappedParagraphs)
123 ME_UpdateScrollBar(editor);
124
125 /* Ensure that the cursor is visible */
126 ME_EnsureVisible(editor, &editor->pCursors[0]);
127
128 ITextHost_TxViewChange(editor->texthost, update_now);
129
130 ME_SendSelChange(editor);
131
132 /* send EN_CHANGE if the event mask asks for it */
133 if(editor->nEventMask & ENM_CHANGE)
134 {
135 editor->nEventMask &= ~ENM_CHANGE;
136 ME_SendOldNotify(editor, EN_CHANGE);
137 editor->nEventMask |= ENM_CHANGE;
138 }
139 }
140
141 void
142 ME_RewrapRepaint(ME_TextEditor *editor)
143 {
144 /* RewrapRepaint should be called whenever the control has changed in
145 * looks, but not content. Like resizing. */
146
147 ME_MarkAllForWrapping(editor);
148 ME_WrapMarkedParagraphs(editor);
149 ME_UpdateScrollBar(editor);
150 ME_Repaint(editor);
151 }
152
153 int ME_twips2pointsX(const ME_Context *c, int x)
154 {
155 if (c->editor->nZoomNumerator == 0)
156 return x * c->dpi.cx / 1440;
157 else
158 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
159 }
160
161 int ME_twips2pointsY(const ME_Context *c, int y)
162 {
163 if (c->editor->nZoomNumerator == 0)
164 return y * c->dpi.cy / 1440;
165 else
166 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
167 }
168
169 static void ME_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
170 int nChars, ME_Style *s, int width,
171 int nSelFrom, int nSelTo, int ymin, int cy)
172 {
173 HDC hDC = c->hDC;
174 HGDIOBJ hOldFont = NULL;
175 SIZE sz;
176 int selWidth;
177 /* Only highlight if there is a selection in the run and when
178 * EM_HIDESELECTION is not being used to hide the selection. */
179 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
180 || c->editor->bHideSelection)
181 return;
182 hOldFont = ME_SelectStyleFont(c, s);
183 if (width <= 0)
184 {
185 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
186 width = sz.cx;
187 }
188 if (nSelFrom < 0) nSelFrom = 0;
189 if (nSelTo > nChars) nSelTo = nChars;
190 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
191 x += sz.cx;
192 if (nSelTo != nChars)
193 {
194 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
195 selWidth = sz.cx;
196 } else {
197 selWidth = width - sz.cx;
198 }
199 ME_UnselectStyleFont(c, s, hOldFont);
200
201 if (c->editor->bEmulateVersion10)
202 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
203 else
204 {
205 RECT rect;
206 HBRUSH hBrush;
207 rect.left = x;
208 rect.top = ymin;
209 rect.right = x + selWidth;
210 rect.bottom = ymin + cy;
211 hBrush = CreateSolidBrush(ITextHost_TxGetSysColor(c->editor->texthost,
212 COLOR_HIGHLIGHT));
213 FillRect(hDC, &rect, hBrush);
214 DeleteObject(hBrush);
215 }
216 }
217
218 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
219 int nChars, ME_Style *s, int width,
220 int nSelFrom, int nSelTo, int ymin, int cy)
221 {
222 HDC hDC = c->hDC;
223 HGDIOBJ hOldFont;
224 COLORREF rgbOld;
225 int yOffset = 0, yTwipsOffset = 0;
226 SIZE sz;
227 COLORREF rgb;
228 HPEN hPen = NULL, hOldPen = NULL;
229 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
230 && nSelFrom < nSelTo && !c->editor->bHideSelection);
231 int xSelStart = x, xSelEnd = x;
232 int *lpDx = NULL;
233 /* lpDx is only needed for tabs to make sure the underline done automatically
234 * by the font extends to the end of the tab. Tabs are always stored as
235 * a single character run, so we can handle this case separately, since
236 * otherwise lpDx would need to specify the lengths of each character. */
237 if (width && nChars == 1)
238 lpDx = &width; /* Make sure underline for tab extends across tab space */
239
240 hOldFont = ME_SelectStyleFont(c, s);
241 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
242 yTwipsOffset = s->fmt.yOffset;
243 }
244 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
245 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
246 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
247 }
248 if (yTwipsOffset)
249 yOffset = ME_twips2pointsY(c, yTwipsOffset);
250
251 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
252 rgb = RGB(0,0,255);
253 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
254 rgb = ITextHost_TxGetSysColor(c->editor->texthost, COLOR_WINDOWTEXT);
255 else
256 rgb = s->fmt.crTextColor;
257
258 /* Determine the area that is selected in the run. */
259 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
260 /* Treat width as an optional parameter. We can get the width from the
261 * text extent of the string if it isn't specified. */
262 if (!width) width = sz.cx;
263 if (bHighlightedText)
264 {
265 if (nSelFrom <= 0)
266 {
267 nSelFrom = 0;
268 }
269 else
270 {
271 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
272 xSelStart = x + sz.cx;
273 }
274 if (nSelTo >= nChars)
275 {
276 nSelTo = nChars;
277 xSelEnd = x + width;
278 }
279 else
280 {
281 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
282 xSelEnd = xSelStart + sz.cx;
283 }
284 }
285
286 /* Choose the pen type for underlining the text. */
287 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
288 {
289 switch (s->fmt.bUnderlineType)
290 {
291 case CFU_UNDERLINE:
292 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
293 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
294 hPen = CreatePen(PS_SOLID, 1, rgb);
295 break;
296 case CFU_UNDERLINEDOTTED:
297 hPen = CreatePen(PS_DOT, 1, rgb);
298 break;
299 default:
300 FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
301 /* fall through */
302 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
303 case CFU_UNDERLINENONE:
304 hPen = NULL;
305 break;
306 }
307 if (hPen)
308 {
309 hOldPen = SelectObject(hDC, hPen);
310 }
311 }
312
313 rgbOld = SetTextColor(hDC, rgb);
314 if (bHighlightedText && !c->editor->bEmulateVersion10)
315 {
316 COLORREF rgbBackOld;
317 RECT dim;
318 /* FIXME: should use textmetrics info for Descent info */
319 if (hPen)
320 MoveToEx(hDC, x, y - yOffset + 1, NULL);
321 if (xSelStart > x)
322 {
323 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
324 if (hPen)
325 LineTo(hDC, xSelStart, y - yOffset + 1);
326 }
327 dim.top = ymin;
328 dim.bottom = ymin + cy;
329 dim.left = xSelStart;
330 dim.right = xSelEnd;
331 SetTextColor(hDC, ITextHost_TxGetSysColor(c->editor->texthost,
332 COLOR_HIGHLIGHTTEXT));
333 rgbBackOld = SetBkColor(hDC, ITextHost_TxGetSysColor(c->editor->texthost,
334 COLOR_HIGHLIGHT));
335 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
336 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
337 if (hPen)
338 LineTo(hDC, xSelEnd, y - yOffset + 1);
339 SetBkColor(hDC, rgbBackOld);
340 if (xSelEnd < x + width)
341 {
342 SetTextColor(hDC, rgb);
343 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
344 nChars-nSelTo, NULL);
345 if (hPen)
346 LineTo(hDC, x + width, y - yOffset + 1);
347 }
348 }
349 else
350 {
351 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
352
353 /* FIXME: should use textmetrics info for Descent info */
354 if (hPen)
355 {
356 MoveToEx(hDC, x, y - yOffset + 1, NULL);
357 LineTo(hDC, x + width, y - yOffset + 1);
358 }
359
360 if (bHighlightedText) /* v1.0 inverts the selection */
361 {
362 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
363 }
364 }
365
366 if (hPen)
367 {
368 SelectObject(hDC, hOldPen);
369 DeleteObject(hPen);
370 }
371 SetTextColor(hDC, rgbOld);
372 ME_UnselectStyleFont(c, s, hOldFont);
373 }
374
375 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
376 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
377 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
378 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
379 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
380 SelectObject(hDC, hFont);
381 SetTextAlign(hDC, align);
382 SetTextColor(hDC, color);
383 }
384
385 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
386 {
387 ME_Run *run = &rundi->member.run;
388 ME_DisplayItem *start;
389 int runofs = run->nCharOfs+para->nCharOfs;
390 int nSelFrom, nSelTo;
391 const WCHAR wszSpace[] = {' ', 0};
392
393 if (run->nFlags & MERF_HIDDEN)
394 return;
395
396 start = ME_FindItemBack(rundi, diStartRow);
397 ME_GetSelectionOfs(c->editor, &nSelFrom, &nSelTo);
398
399 /* Draw selected end-of-paragraph mark */
400 if (run->nFlags & MERF_ENDPARA)
401 {
402 if (runofs >= nSelFrom && runofs < nSelTo)
403 {
404 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
405 c->pt.y + para->pt.y + start->member.row.pt.y,
406 start->member.row.nHeight);
407 }
408 return;
409 }
410
411 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
412 {
413 /* wszSpace is used instead of the tab character because otherwise
414 * an unwanted symbol can be inserted instead. */
415 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
416 nSelFrom-runofs, nSelTo-runofs,
417 c->pt.y + para->pt.y + start->member.row.pt.y,
418 start->member.row.nHeight);
419 return;
420 }
421
422 if (run->nFlags & MERF_GRAPHICS)
423 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
424 else
425 {
426 if (c->editor->cPasswordMask)
427 {
428 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask, run->strText->nLen);
429 ME_DrawTextWithStyle(c, x, y,
430 szMasked->szData, szMasked->nLen, run->style, run->nWidth,
431 nSelFrom-runofs,nSelTo-runofs,
432 c->pt.y + para->pt.y + start->member.row.pt.y,
433 start->member.row.nHeight);
434 ME_DestroyString(szMasked);
435 }
436 else
437 ME_DrawTextWithStyle(c, x, y,
438 run->strText->szData, run->strText->nLen, run->style, run->nWidth,
439 nSelFrom-runofs,nSelTo-runofs,
440 c->pt.y + para->pt.y + start->member.row.pt.y,
441 start->member.row.nHeight);
442 }
443 }
444
445 /* The documented widths are in points (72 dpi), but converting them to
446 * 96 dpi (standard display resolution) avoids dealing with fractions. */
447 static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
448 /* none */ {0, PS_SOLID, FALSE},
449 /* 3/4 */ {1, PS_SOLID, FALSE},
450 /* 1 1/2 */ {2, PS_SOLID, FALSE},
451 /* 2 1/4 */ {3, PS_SOLID, FALSE},
452 /* 3 */ {4, PS_SOLID, FALSE},
453 /* 4 1/2 */ {6, PS_SOLID, FALSE},
454 /* 6 */ {8, PS_SOLID, FALSE},
455 /* 3/4 double */ {1, PS_SOLID, TRUE},
456 /* 1 1/2 double */ {2, PS_SOLID, TRUE},
457 /* 2 1/4 double */ {3, PS_SOLID, TRUE},
458 /* 3/4 gray */ {1, PS_DOT /* FIXME */, FALSE},
459 /* 1 1/2 dashed */ {2, PS_DASH, FALSE},
460 };
461
462 static const COLORREF pen_colors[16] = {
463 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
464 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
465 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
466 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
467 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
468 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
469 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
470 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
471 };
472
473 static int ME_GetBorderPenWidth(const ME_Context* c, int idx)
474 {
475 int width = border_details[idx].width;
476
477 if (c->dpi.cx != 96)
478 width = MulDiv(width, c->dpi.cx, 96);
479
480 if (c->editor->nZoomNumerator != 0)
481 width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
482
483 return width;
484 }
485
486 int ME_GetParaBorderWidth(const ME_Context* c, int flags)
487 {
488 int idx = (flags >> 8) & 0xF;
489 int width;
490
491 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
492 {
493 FIXME("Unsupported border value %d\n", idx);
494 return 0;
495 }
496 width = ME_GetBorderPenWidth(c, idx);
497 if (border_details[idx].dble) width = width * 2 + 1;
498 return width;
499 }
500
501 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
502 {
503 int idx, border_width, top_border, bottom_border;
504 RECT rc;
505 BOOL hasParaBorder;
506
507 SetRectEmpty(bounds);
508 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
509
510 border_width = top_border = bottom_border = 0;
511 idx = (para->pFmt->wBorders >> 8) & 0xF;
512 hasParaBorder = (!(c->editor->bEmulateVersion10 &&
513 para->pFmt->dwMask & PFM_TABLE &&
514 para->pFmt->wEffects & PFE_TABLE) &&
515 (para->pFmt->dwMask & PFM_BORDER) &&
516 idx != 0 &&
517 (para->pFmt->wBorders & 0xF));
518 if (hasParaBorder)
519 {
520 /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
521 * controls. It actually stores the paragraph or row border style. Although
522 * the value isn't used for drawing, it is used for streaming out rich text.
523 *
524 * wBorders stores the border style for each side (top, left, bottom, right)
525 * using nibble (4 bits) to store each border style. The rich text format
526 * control words, and their associated value are the following:
527 * \brdrdash 0
528 * \brdrdashsm 1
529 * \brdrdb 2
530 * \brdrdot 3
531 * \brdrhair 4
532 * \brdrs 5
533 * \brdrth 6
534 * \brdrtriple 7
535 *
536 * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
537 * The mask corresponding to each side for the version are the following:
538 * mask v1.0-3.0 v4.1
539 * 0x000F top left
540 * 0x00F0 left top
541 * 0x0F00 bottom right
542 * 0xF000 right bottom
543 */
544 if (para->pFmt->wBorders & 0x00B0)
545 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
546 border_width = ME_GetParaBorderWidth(c, para->pFmt->wBorders);
547 if (para->pFmt->wBorders & 4) top_border = border_width;
548 if (para->pFmt->wBorders & 8) bottom_border = border_width;
549 }
550
551 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
552 {
553 rc.left = c->rcView.left;
554 rc.right = c->rcView.right;
555 rc.top = y;
556 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
557 rc.bottom = y + bounds->top + top_border;
558 FillRect(c->hDC, &rc, c->editor->hbrBackground);
559 }
560
561 if (para->pFmt->dwMask & PFM_SPACEAFTER)
562 {
563 rc.left = c->rcView.left;
564 rc.right = c->rcView.right;
565 rc.bottom = y + para->nHeight;
566 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
567 rc.top = rc.bottom - bounds->bottom - bottom_border;
568 FillRect(c->hDC, &rc, c->editor->hbrBackground);
569 }
570
571 /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
572 * but might support it in later versions. */
573 if (hasParaBorder) {
574 int pen_width, rightEdge;
575 COLORREF pencr;
576 HPEN pen = NULL, oldpen = NULL;
577 POINT pt;
578
579 if (para->pFmt->wBorders & 64) /* autocolor */
580 pencr = ITextHost_TxGetSysColor(c->editor->texthost,
581 COLOR_WINDOWTEXT);
582 else
583 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
584
585 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
586 c->editor->nTotalWidth);
587
588 pen_width = ME_GetBorderPenWidth(c, idx);
589 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
590 oldpen = SelectObject(c->hDC, pen);
591 MoveToEx(c->hDC, 0, 0, &pt);
592
593 /* before & after spaces are not included in border */
594
595 /* helper to draw the double lines in case of corner */
596 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
597
598 if (para->pFmt->wBorders & 1)
599 {
600 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
601 LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
602 if (border_details[idx].dble) {
603 rc.left = c->pt.x + 1;
604 rc.right = rc.left + border_width;
605 rc.top = y + bounds->top;
606 rc.bottom = y + para->nHeight - bounds->bottom;
607 FillRect(c->hDC, &rc, c->editor->hbrBackground);
608 MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
609 LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
610 }
611 bounds->left += border_width;
612 }
613 if (para->pFmt->wBorders & 2)
614 {
615 MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
616 LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
617 if (border_details[idx].dble) {
618 rc.left = rightEdge - pen_width - 1;
619 rc.right = rc.left + pen_width;
620 rc.top = y + bounds->top;
621 rc.bottom = y + para->nHeight - bounds->bottom;
622 FillRect(c->hDC, &rc, c->editor->hbrBackground);
623 MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
624 LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
625 }
626 bounds->right += border_width;
627 }
628 if (para->pFmt->wBorders & 4)
629 {
630 MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
631 LineTo(c->hDC, rightEdge, y + bounds->top);
632 if (border_details[idx].dble) {
633 MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
634 LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
635 }
636 bounds->top += border_width;
637 }
638 if (para->pFmt->wBorders & 8)
639 {
640 MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
641 LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
642 if (border_details[idx].dble) {
643 MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
644 LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
645 }
646 bounds->bottom += border_width;
647 }
648 #undef DD
649
650 MoveToEx(c->hDC, pt.x, pt.y, NULL);
651 SelectObject(c->hDC, oldpen);
652 DeleteObject(pen);
653 }
654 }
655
656 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
657 {
658 ME_Paragraph *para = &paragraph->member.para;
659 if (!c->editor->bEmulateVersion10) /* v4.1 */
660 {
661 if (para->pCell)
662 {
663 RECT rc;
664 ME_Cell *cell = &para->pCell->member.cell;
665 ME_DisplayItem *paraAfterRow;
666 HPEN pen, oldPen;
667 LOGBRUSH logBrush;
668 HBRUSH brush;
669 COLORREF color;
670 POINT oldPt;
671 int width;
672 BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
673 BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
674 int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
675 int bottom = (atBottom ?
676 c->pt.y + cell->pt.y + cell->nHeight :
677 top + para->nHeight + (atTop ? cell->yTextOffset : 0));
678 rc.left = c->pt.x + cell->pt.x;
679 rc.right = rc.left + cell->nWidth;
680 if (atTop) {
681 /* Erase gap before text if not all borders are the same height. */
682 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
683 rc.top = top + width;
684 width = cell->yTextOffset - width;
685 rc.bottom = rc.top + width;
686 if (width) {
687 FillRect(c->hDC, &rc, c->editor->hbrBackground);
688 }
689 }
690 /* Draw cell borders.
691 * The order borders are draw in is left, top, bottom, right in order
692 * to be consistent with native richedit. This is noticeable from the
693 * overlap of borders of different colours. */
694 if (!(para->nFlags & MEPF_ROWEND)) {
695 rc.top = top;
696 rc.bottom = bottom;
697 if (cell->border.left.width > 0)
698 {
699 color = cell->border.left.colorRef;
700 width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
701 } else {
702 color = RGB(192,192,192);
703 width = 1;
704 }
705 logBrush.lbStyle = BS_SOLID;
706 logBrush.lbColor = color;
707 logBrush.lbHatch = 0;
708 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
709 width, &logBrush, 0, NULL);
710 oldPen = SelectObject(c->hDC, pen);
711 MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
712 LineTo(c->hDC, rc.left, rc.bottom);
713 SelectObject(c->hDC, oldPen);
714 DeleteObject(pen);
715 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
716 }
717
718 if (atTop) {
719 if (cell->border.top.width > 0)
720 {
721 brush = CreateSolidBrush(cell->border.top.colorRef);
722 width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
723 } else {
724 brush = GetStockObject(LTGRAY_BRUSH);
725 width = 1;
726 }
727 rc.top = top;
728 rc.bottom = rc.top + width;
729 FillRect(c->hDC, &rc, brush);
730 if (cell->border.top.width > 0)
731 DeleteObject(brush);
732 }
733
734 /* Draw the bottom border if at the last paragraph in the cell, and when
735 * in the last row of the table. */
736 if (atBottom) {
737 int oldLeft = rc.left;
738 width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
739 paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
740 if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
741 ME_DisplayItem *nextEndCell;
742 nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
743 assert(nextEndCell && !nextEndCell->member.cell.next_cell);
744 rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
745 /* FIXME: Native draws FROM the bottom of the table rather than
746 * TO the bottom of the table in this case, but just doing so here
747 * will cause the next row to erase the border. */
748 /*
749 rc.top = bottom;
750 rc.bottom = rc.top + width;
751 */
752 }
753 if (rc.left < rc.right) {
754 if (cell->border.bottom.width > 0) {
755 brush = CreateSolidBrush(cell->border.bottom.colorRef);
756 } else {
757 brush = GetStockObject(LTGRAY_BRUSH);
758 }
759 rc.bottom = bottom;
760 rc.top = rc.bottom - width;
761 FillRect(c->hDC, &rc, brush);
762 if (cell->border.bottom.width > 0)
763 DeleteObject(brush);
764 }
765 rc.left = oldLeft;
766 }
767
768 /* Right border only drawn if at the end of the table row. */
769 if (!cell->next_cell->member.cell.next_cell &&
770 !(para->nFlags & MEPF_ROWSTART))
771 {
772 rc.top = top;
773 rc.bottom = bottom;
774 if (cell->border.right.width > 0) {
775 color = cell->border.right.colorRef;
776 width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
777 } else {
778 color = RGB(192,192,192);
779 width = 1;
780 }
781 logBrush.lbStyle = BS_SOLID;
782 logBrush.lbColor = color;
783 logBrush.lbHatch = 0;
784 pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
785 width, &logBrush, 0, NULL);
786 oldPen = SelectObject(c->hDC, pen);
787 MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
788 LineTo(c->hDC, rc.right - 1, rc.bottom);
789 SelectObject(c->hDC, oldPen);
790 DeleteObject(pen);
791 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
792 }
793 }
794 } else { /* v1.0 - 3.0 */
795 /* Draw simple table border */
796 if (para->pFmt->dwMask & PFM_TABLE && para->pFmt->wEffects & PFE_TABLE) {
797 HPEN pen = NULL, oldpen = NULL;
798 int i, firstX, startX, endX, rowY, rowBottom, nHeight;
799 POINT oldPt;
800 PARAFORMAT2 *pNextFmt;
801
802 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
803 oldpen = SelectObject(c->hDC, pen);
804
805 /* Find the start relative to the text */
806 firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
807 /* Go back by the horizontal gap, which is stored in dxOffset */
808 firstX -= ME_twips2pointsX(c, para->pFmt->dxOffset);
809 /* The left edge, stored in dxStartIndent affected just the first edge */
810 startX = firstX - ME_twips2pointsX(c, para->pFmt->dxStartIndent);
811 rowY = c->pt.y + para->pt.y;
812 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
813 rowY += ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
814 nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
815 rowBottom = rowY + nHeight;
816
817 /* Draw horizontal lines */
818 MoveToEx(c->hDC, firstX, rowY, &oldPt);
819 i = para->pFmt->cTabCount - 1;
820 endX = startX + ME_twips2pointsX(c, para->pFmt->rgxTabs[i] & 0x00ffffff) + 1;
821 LineTo(c->hDC, endX, rowY);
822 pNextFmt = para->next_para->member.para.pFmt;
823 /* The bottom of the row only needs to be drawn if the next row is
824 * not a table. */
825 if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
826 para->nRows == 1))
827 {
828 /* Decrement rowBottom to draw the bottom line within the row, and
829 * to not draw over this line when drawing the vertical lines. */
830 rowBottom--;
831 MoveToEx(c->hDC, firstX, rowBottom, NULL);
832 LineTo(c->hDC, endX, rowBottom);
833 }
834
835 /* Draw vertical lines */
836 MoveToEx(c->hDC, firstX, rowY, NULL);
837 LineTo(c->hDC, firstX, rowBottom);
838 for (i = 0; i < para->pFmt->cTabCount; i++)
839 {
840 int rightBoundary = para->pFmt->rgxTabs[i] & 0x00ffffff;
841 endX = startX + ME_twips2pointsX(c, rightBoundary);
842 MoveToEx(c->hDC, endX, rowY, NULL);
843 LineTo(c->hDC, endX, rowBottom);
844 }
845
846 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
847 SelectObject(c->hDC, oldpen);
848 DeleteObject(pen);
849 }
850 }
851 }
852
853 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
854 {
855 int align = SetTextAlign(c->hDC, TA_BASELINE);
856 ME_DisplayItem *p;
857 ME_Run *run;
858 ME_Paragraph *para = NULL;
859 RECT rc, bounds;
860 int y;
861 int height = 0, baseline = 0, no=0;
862 BOOL visible = FALSE;
863
864 rc.left = c->pt.x;
865 rc.right = c->rcView.right;
866
867 assert(paragraph);
868 para = &paragraph->member.para;
869 y = c->pt.y + para->pt.y;
870 if (para->pCell)
871 {
872 ME_Cell *cell = &para->pCell->member.cell;
873 rc.left = c->pt.x + cell->pt.x;
874 rc.right = rc.left + cell->nWidth;
875 }
876 if (para->nFlags & MEPF_ROWSTART) {
877 ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
878 rc.right = c->pt.x + cell->pt.x;
879 } else if (para->nFlags & MEPF_ROWEND) {
880 ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
881 rc.left = c->pt.x + cell->pt.x + cell->nWidth;
882 }
883 ME_DrawParaDecoration(c, para, y, &bounds);
884 y += bounds.top;
885 if (bounds.left || bounds.right) {
886 rc.left = max(rc.left, c->pt.x + bounds.left);
887 rc.right = min(rc.right, c->pt.x - bounds.right
888 + max(c->editor->sizeWindow.cx,
889 c->editor->nTotalWidth));
890 }
891
892 for (p = paragraph->next; p != para->next_para; p = p->next)
893 {
894 switch(p->type) {
895 case diParagraph:
896 assert(FALSE);
897 break;
898 case diStartRow:
899 y += height;
900 rc.top = y;
901 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
902 rc.bottom = y + para->nHeight;
903 } else {
904 rc.bottom = y + p->member.row.nHeight;
905 }
906 visible = RectVisible(c->hDC, &rc);
907 if (visible) {
908 FillRect(c->hDC, &rc, c->editor->hbrBackground);
909 }
910 if (bounds.right)
911 {
912 /* If scrolled to the right past the end of the text, then
913 * there may be space to the right of the paragraph border. */
914 RECT rcAfterBrdr = rc;
915 rcAfterBrdr.left = rc.right + bounds.right;
916 rcAfterBrdr.right = c->rcView.right;
917 if (RectVisible(c->hDC, &rcAfterBrdr))
918 FillRect(c->hDC, &rcAfterBrdr, c->editor->hbrBackground);
919 }
920 if (me_debug)
921 {
922 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
923 WCHAR buf[128];
924 POINT pt = c->pt;
925 wsprintfW(buf, wszRowDebug, no);
926 pt.y = 12+y;
927 ME_DebugWrite(c->hDC, &pt, buf);
928 }
929
930 height = p->member.row.nHeight;
931 baseline = p->member.row.nBaseline;
932 break;
933 case diRun:
934 assert(para);
935 run = &p->member.run;
936 if (visible && me_debug) {
937 RECT rc;
938 rc.left = c->pt.x + run->pt.x;
939 rc.right = rc.left + run->nWidth;
940 rc.top = c->pt.y + para->pt.y + run->pt.y;
941 rc.bottom = rc.top + height;
942 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
943 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
944 }
945 if (visible)
946 ME_DrawRun(c, c->pt.x + run->pt.x,
947 c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
948 if (me_debug)
949 {
950 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
951 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
952 WCHAR buf[2560];
953 POINT pt;
954 pt.x = c->pt.x + run->pt.x;
955 pt.y = c->pt.y + para->pt.y + run->pt.y;
956 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
957 ME_DebugWrite(c->hDC, &pt, buf);
958 }
959 break;
960 case diCell:
961 /* Clear any space at the bottom of the cell after the text. */
962 if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
963 break;
964 y += height;
965 rc.top = c->pt.y + para->pt.y + para->nHeight;
966 rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
967 if (RectVisible(c->hDC, &rc))
968 {
969 FillRect(c->hDC, &rc, c->editor->hbrBackground);
970 }
971 break;
972 default:
973 break;
974 }
975 no++;
976 }
977
978 ME_DrawTableBorders(c, paragraph);
979
980 SetTextAlign(c->hDC, align);
981 }
982
983 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
984 {
985 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
986 int scrollX = 0, scrollY = 0;
987
988 if (editor->horz_si.nPos != x) {
989 x = min(x, editor->horz_si.nMax);
990 x = max(x, editor->horz_si.nMin);
991 scrollX = editor->horz_si.nPos - x;
992 editor->horz_si.nPos = x;
993 if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
994 x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
995 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
996 }
997
998 if (editor->vert_si.nPos != y) {
999 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1000 y = max(y, editor->vert_si.nMin);
1001 scrollY = editor->vert_si.nPos - y;
1002 editor->vert_si.nPos = y;
1003 if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1004 y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1005 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1006 }
1007
1008 if (abs(scrollX) > editor->sizeWindow.cx ||
1009 abs(scrollY) > editor->sizeWindow.cy)
1010 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1011 else
1012 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1013 &editor->rcFormat, &editor->rcFormat,
1014 NULL, NULL, SW_INVALIDATE);
1015 ME_Repaint(editor);
1016
1017 if (editor->hWnd)
1018 {
1019 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1020 if (editor->styleFlags & WS_HSCROLL)
1021 {
1022 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1023 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1024 && (editor->styleFlags & WS_HSCROLL))
1025 || (editor->styleFlags & ES_DISABLENOSCROLL);
1026 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1027 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1028 bScrollBarWillBeVisible);
1029 }
1030
1031 if (editor->styleFlags & WS_VSCROLL)
1032 {
1033 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1034 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1035 && (editor->styleFlags & WS_VSCROLL)
1036 && (editor->styleFlags & ES_MULTILINE))
1037 || (editor->styleFlags & ES_DISABLENOSCROLL);
1038 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1039 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1040 bScrollBarWillBeVisible);
1041 }
1042 }
1043 ME_UpdateScrollBar(editor);
1044 }
1045
1046 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1047 {
1048 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1049 }
1050
1051 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1052 {
1053 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1054 }
1055
1056 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1057 {
1058 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1059 }
1060
1061 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1062 {
1063 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1064 }
1065
1066 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1067 {
1068 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1069 }
1070
1071 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1072 {
1073 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1074 }
1075
1076 /* Calculates the visibility after a call to SetScrollRange or
1077 * SetScrollInfo with SIF_RANGE. */
1078 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1079 {
1080 if (si->fMask & SIF_DISABLENOSCROLL)
1081 return TRUE;
1082
1083 /* This must match the check in SetScrollInfo to determine whether
1084 * to show or hide the scrollbars. */
1085 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1086 }
1087
1088 void ME_UpdateScrollBar(ME_TextEditor *editor)
1089 {
1090 /* Note that this is the only function that should ever call
1091 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1092
1093 SCROLLINFO si;
1094 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1095
1096 if (ME_WrapMarkedParagraphs(editor))
1097 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1098
1099 si.cbSize = sizeof(si);
1100 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1101 si.nMin = 0;
1102 if (editor->styleFlags & ES_DISABLENOSCROLL)
1103 si.fMask |= SIF_DISABLENOSCROLL;
1104
1105 /* Update horizontal scrollbar */
1106 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1107 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1108 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1109 {
1110 ME_HScrollAbs(editor, 0);
1111 /* ME_HScrollAbs will call this function,
1112 * so nothing else needs to be done here. */
1113 return;
1114 }
1115
1116 si.nMax = editor->nTotalWidth;
1117 si.nPos = editor->horz_si.nPos;
1118 si.nPage = editor->sizeWindow.cx;
1119
1120 if (si.nMax != editor->horz_si.nMax ||
1121 si.nPage != editor->horz_si.nPage)
1122 {
1123 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1124 editor->horz_si.nMax = si.nMax;
1125 editor->horz_si.nPage = si.nPage;
1126 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1127 editor->styleFlags & WS_HSCROLL)
1128 {
1129 if (si.nMax > 0xFFFF)
1130 {
1131 /* Native scales the scrollbar info to 16-bit external values. */
1132 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1133 si.nMax = 0xFFFF;
1134 }
1135 if (editor->hWnd) {
1136 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1137 } else {
1138 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1139 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1140 }
1141 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1142 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1143 }
1144 }
1145
1146 if (editor->styleFlags & WS_HSCROLL)
1147 {
1148 if (si.fMask & SIF_DISABLENOSCROLL) {
1149 bScrollBarWillBeVisible = TRUE;
1150 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1151 bScrollBarWillBeVisible = FALSE;
1152 }
1153
1154 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1155 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1156 }
1157
1158 /* Update vertical scrollbar */
1159 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1160 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1161 (editor->styleFlags & ES_MULTILINE);
1162
1163 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1164 {
1165 ME_VScrollAbs(editor, 0);
1166 /* ME_VScrollAbs will call this function,
1167 * so nothing else needs to be done here. */
1168 return;
1169 }
1170
1171 si.nMax = editor->nTotalLength;
1172 si.nPos = editor->vert_si.nPos;
1173 si.nPage = editor->sizeWindow.cy;
1174
1175 if (si.nMax != editor->vert_si.nMax ||
1176 si.nPage != editor->vert_si.nPage)
1177 {
1178 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1179 editor->vert_si.nMax = si.nMax;
1180 editor->vert_si.nPage = si.nPage;
1181 if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1182 editor->styleFlags & WS_VSCROLL)
1183 {
1184 if (si.nMax > 0xFFFF)
1185 {
1186 /* Native scales the scrollbar info to 16-bit external values. */
1187 si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1188 si.nMax = 0xFFFF;
1189 }
1190 if (editor->hWnd) {
1191 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1192 } else {
1193 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1194 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1195 }
1196 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1197 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1198 }
1199 }
1200
1201 if (editor->styleFlags & WS_VSCROLL)
1202 {
1203 if (si.fMask & SIF_DISABLENOSCROLL) {
1204 bScrollBarWillBeVisible = TRUE;
1205 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1206 bScrollBarWillBeVisible = FALSE;
1207 }
1208
1209 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1210 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1211 bScrollBarWillBeVisible);
1212 }
1213 }
1214
1215 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1216 {
1217 ME_Run *pRun = &pCursor->pRun->member.run;
1218 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1219 ME_DisplayItem *pPara = pCursor->pPara;
1220 int x, y, yheight;
1221
1222 assert(pRow);
1223 assert(pPara);
1224
1225 if (editor->styleFlags & ES_AUTOHSCROLL)
1226 {
1227 x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset);
1228 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1229 x = x + 1 - editor->sizeWindow.cx;
1230 else if (x > editor->horz_si.nPos)
1231 x = editor->horz_si.nPos;
1232
1233 if (~editor->styleFlags & ES_AUTOVSCROLL)
1234 {
1235 ME_HScrollAbs(editor, x);
1236 return;
1237 }
1238 } else {
1239 if (~editor->styleFlags & ES_AUTOVSCROLL)
1240 return;
1241 x = editor->horz_si.nPos;
1242 }
1243
1244 y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1245 yheight = pRow->member.row.nHeight;
1246
1247 if (y < editor->vert_si.nPos)
1248 ME_ScrollAbs(editor, x, y);
1249 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1250 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1251 else if (x != editor->horz_si.nPos)
1252 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1253 }
1254
1255
1256 void
1257 ME_InvalidateSelection(ME_TextEditor *editor)
1258 {
1259 ME_DisplayItem *sel_start, *sel_end;
1260 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
1261 int nStart, nEnd;
1262 int len = ME_GetTextLength(editor);
1263
1264 ME_GetSelectionOfs(editor, &nStart, &nEnd);
1265 /* if both old and new selection are 0-char (= caret only), then
1266 there's no (inverted) area to be repainted, neither old nor new */
1267 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1268 return;
1269 ME_WrapMarkedParagraphs(editor);
1270 ME_GetSelectionParas(editor, &sel_start, &sel_end);
1271 assert(sel_start->type == diParagraph);
1272 assert(sel_end->type == diParagraph);
1273 /* last selection markers aren't always updated, which means
1274 * they can point past the end of the document */
1275 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1276 repaint_start = ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph);
1277 repaint_end = editor->pBuffer->pLast->member.para.prev_para;
1278 } else {
1279 /* if the start part of selection is being expanded or contracted... */
1280 if (nStart < editor->nLastSelStart) {
1281 repaint_start = sel_start;
1282 repaint_end = editor->pLastSelStartPara;
1283 } else if (nStart > editor->nLastSelStart) {
1284 repaint_start = editor->pLastSelStartPara;
1285 repaint_end = sel_start;
1286 }
1287
1288 /* if the end part of selection is being contracted or expanded... */
1289 if (nEnd < editor->nLastSelEnd) {
1290 if (!repaint_start) repaint_start = sel_end;
1291 repaint_end = editor->pLastSelEndPara;
1292 } else if (nEnd > editor->nLastSelEnd) {
1293 if (!repaint_start) repaint_start = editor->pLastSelEndPara;
1294 repaint_end = sel_end;
1295 }
1296 }
1297
1298 if (repaint_start)
1299 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
1300 /* remember the last invalidated position */
1301 ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1302 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1303 assert(editor->pLastSelStartPara->type == diParagraph);
1304 assert(editor->pLastSelEndPara->type == diParagraph);
1305 }
1306
1307 BOOL
1308 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1309 {
1310 /* TODO: Zoom images and objects */
1311
1312 if (numerator == 0 && denominator == 0)
1313 {
1314 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1315 return TRUE;
1316 }
1317 if (numerator <= 0 || denominator <= 0)
1318 return FALSE;
1319 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1320 return FALSE;
1321
1322 editor->nZoomNumerator = numerator;
1323 editor->nZoomDenominator = denominator;
1324
1325 ME_RewrapRepaint(editor);
1326 return TRUE;
1327 }