Create a branch for header work.
[reactos.git] / 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, BOOL bOnlyNew, 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 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)
54 {
55 assert(item->type == diParagraph);
56
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)
60 {
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;
66 }
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)
69 {
70 /* the border shifts the text down */
71 ys -= item->member.para.pCell->member.cell.yTextOffset;
72 }
73
74 if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
75 {
76 /* Draw the pargraph if any of the paragraph is in the update region. */
77 if (ys < rcUpdate->bottom && ye > rcUpdate->top)
78 {
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;
84 }
85 }
86 item = item->member.para.next_para;
87 }
88 if (c.pt.y + editor->nTotalLength < c.rcView.bottom)
89 {
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;
96
97 if (bOnlyNew)
98 {
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);
104 }
105
106 IntersectRect(&rc, &rc, rcUpdate);
107
108 if (!IsRectEmpty(&rc))
109 FillRect(hDC, &rc, c.editor->hbrBackground);
110 }
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;
116
117 SelectClipRgn(hDC, oldRgn);
118 if (oldRgn)
119 DeleteObject(oldRgn);
120
121 c.hDC = NULL;
122 ME_DestroyContext(&c);
123 }
124
125 void ME_Repaint(ME_TextEditor *editor)
126 {
127 if (ME_WrapMarkedParagraphs(editor))
128 {
129 ME_UpdateScrollBar(editor);
130 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
131 }
132 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
133 ME_SendOldNotify(editor, EN_UPDATE);
134 ITextHost_TxViewChange(editor->texthost, TRUE);
135 }
136
137 void ME_UpdateRepaint(ME_TextEditor *editor)
138 {
139 /* Should be called whenever the contents of the control have changed */
140 BOOL wrappedParagraphs;
141
142 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
143 if (wrappedParagraphs)
144 ME_UpdateScrollBar(editor);
145
146 /* Ensure that the cursor is visible */
147 ME_EnsureVisible(editor, &editor->pCursors[0]);
148
149 /* send EN_CHANGE if the event mask asks for it */
150 if(editor->nEventMask & ENM_CHANGE)
151 {
152 editor->nEventMask &= ~ENM_CHANGE;
153 ME_SendOldNotify(editor, EN_CHANGE);
154 editor->nEventMask |= ENM_CHANGE;
155 }
156 ME_Repaint(editor);
157 ME_SendSelChange(editor);
158 }
159
160 void
161 ME_RewrapRepaint(ME_TextEditor *editor)
162 {
163 /* RewrapRepaint should be called whenever the control has changed in
164 * looks, but not content. Like resizing. */
165
166 ME_MarkAllForWrapping(editor);
167 ME_WrapMarkedParagraphs(editor);
168 ME_UpdateScrollBar(editor);
169 ME_Repaint(editor);
170 }
171
172 int ME_twips2pointsX(ME_Context *c, int x)
173 {
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;
178 }
179
180 int ME_twips2pointsY(ME_Context *c, int y)
181 {
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;
186 }
187
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)
191 {
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)
203 {
204 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
205 width = sz.cx;
206 }
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)
212 {
213 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
214 selWidth = sz.cx;
215 } else {
216 selWidth = width - sz.cx;
217 }
218 ME_UnselectStyleFont(c, s, hOldFont);
219
220 if (c->editor->bEmulateVersion10)
221 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
222 else
223 {
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);
234 }
235 }
236
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)
240 {
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 */
258
259 hOldFont = ME_SelectStyleFont(c, s);
260 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
261 yTwipsOffset = s->fmt.yOffset;
262 }
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;
266 }
267 if (yTwipsOffset)
268 yOffset = ME_twips2pointsY(c, yTwipsOffset);
269
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;
276
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)
283 {
284 if (nSelFrom <= 0)
285 {
286 nSelFrom = 0;
287 }
288 else
289 {
290 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
291 xSelStart = x + sz.cx;
292 }
293 if (nSelTo >= nChars)
294 {
295 nSelTo = nChars;
296 xSelEnd = x + width;
297 }
298 else
299 {
300 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
301 xSelEnd = xSelStart + sz.cx;
302 }
303 }
304
305 /* Choose the pen type for underlining the text. */
306 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
307 {
308 switch (s->fmt.bUnderlineType)
309 {
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;
325 }
326 if (hPen)
327 {
328 hOldPen = SelectObject(hDC, hPen);
329 }
330 }
331
332 rgbOld = SetTextColor(hDC, rgb);
333 if (bHighlightedText && !c->editor->bEmulateVersion10)
334 {
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)
341 {
342 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
343 if (hPen)
344 LineTo(hDC, xSelStart, y - yOffset + 1);
345 }
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)
360 {
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);
366 }
367 }
368 else
369 {
370 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
371
372 /* FIXME: should use textmetrics info for Descent info */
373 if (hPen)
374 {
375 MoveToEx(hDC, x, y - yOffset + 1, NULL);
376 LineTo(hDC, x + width, y - yOffset + 1);
377 }
378
379 if (bHighlightedText) /* v1.0 inverts the selection */
380 {
381 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
382 }
383 }
384
385 if (hPen)
386 {
387 SelectObject(hDC, hOldPen);
388 DeleteObject(hPen);
389 }
390 SetTextColor(hDC, rgbOld);
391 ME_UnselectStyleFont(c, s, hOldFont);
392 }
393
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);
402 }
403
404 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
405 {
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};
411
412 if (run->nFlags & MERF_HIDDEN)
413 return;
414
415 start = ME_FindItemBack(rundi, diStartRow);
416 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
417
418 /* Draw selected end-of-paragraph mark */
419 if (run->nFlags & MERF_ENDPARA)
420 {
421 if (runofs >= nSelFrom && runofs < nSelTo)
422 {
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);
426 }
427 return;
428 }
429
430 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
431 {
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;
439 }
440
441 if (run->nFlags & MERF_GRAPHICS)
442 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
443 else
444 {
445 if (c->editor->cPasswordMask)
446 {
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);
454 }
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);
461 }
462 }
463
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},
477 };
478
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),
488 };
489
490 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
491 {
492 int width;
493
494 if (editor->nZoomNumerator == 0)
495 {
496 width = border_details[idx].width_num + border_details[idx].width_den / 2;
497 width /= border_details[idx].width_den;
498 }
499 else
500 {
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;
504 }
505 return width;
506 }
507
508 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
509 {
510 int idx = (flags >> 8) & 0xF;
511 int width;
512
513 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
514 {
515 FIXME("Unsupported border value %d\n", idx);
516 return 0;
517 }
518 width = ME_GetBorderPenWidth(editor, idx);
519 if (border_details[idx].dble) width = width * 2 + 1;
520 return width;
521 }
522
523 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
524 {
525 int sp = 0, ls = 0;
526 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
527
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)
531 {
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);
539 }
540 if (c->editor->nZoomNumerator == 0)
541 return sp;
542 else
543 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
544 }
545
546 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
547 {
548 int idx, border_width, top_border, bottom_border;
549 RECT rc;
550 BOOL hasParaBorder;
551
552 SetRectEmpty(bounds);
553 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
554
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)
564 {
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.
568 *
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
580 *
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
588 */
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;
594 }
595
596 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
597 {
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);
604 }
605
606 if (para->pFmt->dwMask & PFM_SPACEAFTER)
607 {
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);
614 }
615
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;
623
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];
629
630 rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
631 c->editor->nTotalWidth);
632
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);
637
638 /* before & after spaces are not included in border */
639
640 /* helper to draw the double lines in case of corner */
641 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
642
643 if (para->pFmt->wBorders & 1)
644 {
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));
655 }
656 bounds->left += border_width;
657 }
658 if (para->pFmt->wBorders & 2)
659 {
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));
670 }
671 bounds->right += border_width;
672 }
673 if (para->pFmt->wBorders & 4)
674 {
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);
680 }
681 bounds->top += border_width;
682 }
683 if (para->pFmt->wBorders & 8)
684 {
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);
690 }
691 bounds->bottom += border_width;
692 }
693 #undef DD
694
695 MoveToEx(c->hDC, pt.x, pt.y, NULL);
696 SelectObject(c->hDC, oldpen);
697 DeleteObject(pen);
698 }
699 }
700
701 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
702 {
703 ME_Paragraph *para = &paragraph->member.para;
704 if (!c->editor->bEmulateVersion10) /* v4.1 */
705 {
706 if (para->pCell)
707 {
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);
733 }
734 }
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)
743 {
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;
749 }
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);
761 }
762
763 if (atTop) {
764 if (cell->border.top.width > 0)
765 {
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;
771 }
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);
777 }
778
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. */
793 /*
794 rc.top = bottom;
795 rc.bottom = rc.top + width;
796 */
797 }
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);
803 }
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);
809 }
810 rc.left = oldLeft;
811 }
812
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))
816 {
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;
825 }
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);
837 }
838 }
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;
846
847 pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
848 oldpen = SelectObject(c->hDC, pen);
849
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;
861
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))
872 {
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);
878 }
879
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++)
884 {
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);
889 }
890
891 MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
892 SelectObject(c->hDC, oldpen);
893 DeleteObject(pen);
894 }
895 }
896 }
897
898 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
899 {
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;
908
909 rc.left = c->pt.x;
910 rc.right = c->rcView.right;
911
912 assert(paragraph);
913 para = &paragraph->member.para;
914 y = c->pt.y + para->pt.y;
915 if (para->pCell)
916 {
917 ME_Cell *cell = &para->pCell->member.cell;
918 rc.left = c->pt.x + cell->pt.x;
919 rc.right = rc.left + cell->nWidth;
920 }
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;
927 }
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));
935 }
936
937 for (p = paragraph->next; p != para->next_para; p = p->next)
938 {
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;
950 }
951 visible = RectVisible(c->hDC, &rc);
952 if (visible) {
953 FillRect(c->hDC, &rc, c->editor->hbrBackground);
954 }
955 if (bounds.right)
956 {
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);
964 }
965 if (me_debug)
966 {
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);
973 }
974
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));
992 }
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)
997 {
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);
1006 }
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))
1016 {
1017 FillRect(c->hDC, &rc, c->editor->hbrBackground);
1018 }
1019 break;
1020 default:
1021 break;
1022 }
1023 no++;
1024 }
1025
1026 ME_DrawTableBorders(c, paragraph);
1027
1028 SetTextAlign(c->hDC, align);
1029 }
1030
1031 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1032 {
1033 BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1034 int scrollX = 0, scrollY = 0;
1035
1036 if (editor->horz_si.nPos != x) {
1037 x = min(x, editor->horz_si.nMax);
1038 x = max(x, editor->horz_si.nMin);
1039 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1040 scrollX = editor->horz_si.nPos - x;
1041 editor->horz_si.nPos = x;
1042 }
1043
1044 if (editor->vert_si.nPos != y) {
1045 y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1046 y = max(y, editor->vert_si.nMin);
1047 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1048 scrollY = editor->vert_si.nPos - y;
1049 editor->vert_si.nPos = y;
1050 }
1051
1052 if (abs(scrollX) > editor->sizeWindow.cx ||
1053 abs(scrollY) > editor->sizeWindow.cy)
1054 ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1055 else
1056 ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1057 &editor->rcFormat, &editor->rcFormat,
1058 NULL, NULL, SW_INVALIDATE);
1059 ME_Repaint(editor);
1060
1061 if (editor->hWnd)
1062 {
1063 LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1064 bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1065 bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1066 && (editor->styleFlags & WS_HSCROLL))
1067 || (editor->styleFlags & ES_DISABLENOSCROLL);
1068 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1069 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1070 bScrollBarWillBeVisible);
1071
1072 bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1073 bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1074 && (editor->styleFlags & WS_VSCROLL)
1075 && (editor->styleFlags & ES_MULTILINE))
1076 || (editor->styleFlags & ES_DISABLENOSCROLL);
1077 if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1078 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1079 bScrollBarWillBeVisible);
1080 }
1081 ME_UpdateScrollBar(editor);
1082 }
1083
1084 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1085 {
1086 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1087 }
1088
1089 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1090 {
1091 ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1092 }
1093
1094 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1095 {
1096 ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1097 }
1098
1099 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1100 {
1101 ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1102 }
1103
1104 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1105 {
1106 ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1107 }
1108
1109 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1110 {
1111 ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1112 }
1113
1114 /* Calculates the visiblity after a call to SetScrollRange or
1115 * SetScrollInfo with SIF_RANGE. */
1116 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1117 {
1118 if (si->fMask & SIF_DISABLENOSCROLL)
1119 return TRUE;
1120
1121 /* This must match the check in SetScrollInfo to determine whether
1122 * to show or hide the scrollbars. */
1123 return si->nMin < si->nMax - max(si->nPage - 1, 0);
1124 }
1125
1126 void ME_UpdateScrollBar(ME_TextEditor *editor)
1127 {
1128 /* Note that this is the only function that should ever call
1129 * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1130
1131 SCROLLINFO si;
1132 BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1133
1134 if (ME_WrapMarkedParagraphs(editor))
1135 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1136
1137 si.cbSize = sizeof(si);
1138 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1139 if (editor->styleFlags & ES_DISABLENOSCROLL)
1140 si.fMask |= SIF_DISABLENOSCROLL;
1141
1142 /* Update horizontal scrollbar */
1143 bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1144 bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1145 if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1146 {
1147 ME_HScrollAbs(editor, 0);
1148 /* ME_HScrollAbs will call this function,
1149 * so nothing else needs to be done here. */
1150 return;
1151 }
1152
1153 si.nMin = 0;
1154 si.nMax = editor->nTotalWidth;
1155 si.nPos = editor->horz_si.nPos;
1156 si.nPage = editor->sizeWindow.cx;
1157
1158 if (si.nMin != editor->horz_si.nMin ||
1159 si.nMax != editor->horz_si.nMax ||
1160 si.nPage != editor->horz_si.nPage)
1161 {
1162 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1163 editor->horz_si.nMin = si.nMin;
1164 editor->horz_si.nMax = si.nMax;
1165 editor->horz_si.nPage = si.nPage;
1166 if (bScrollBarWillBeVisible || bScrollBarWasVisible) {
1167 if (editor->hWnd) {
1168 SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1169 } else {
1170 ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1171 ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1172 }
1173 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1174 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1175 }
1176 }
1177
1178 if (si.fMask & SIF_DISABLENOSCROLL) {
1179 bScrollBarWillBeVisible = TRUE;
1180 } else if (!(editor->styleFlags & WS_HSCROLL)) {
1181 bScrollBarWillBeVisible = FALSE;
1182 }
1183
1184 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1185 ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1186
1187 /* Update vertical scrollbar */
1188 bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1189 bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1190 (editor->styleFlags & ES_MULTILINE);
1191
1192 if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1193 {
1194 ME_VScrollAbs(editor, 0);
1195 /* ME_VScrollAbs will call this function,
1196 * so nothing else needs to be done here. */
1197 return;
1198 }
1199
1200 si.nMax = editor->nTotalLength;
1201 si.nPos = editor->vert_si.nPos;
1202 si.nPage = editor->sizeWindow.cy;
1203
1204 if (si.nMin != editor->vert_si.nMin ||
1205 si.nMax != editor->vert_si.nMax ||
1206 si.nPage != editor->vert_si.nPage)
1207 {
1208 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1209 editor->vert_si.nMin = si.nMin;
1210 editor->vert_si.nMax = si.nMax;
1211 editor->vert_si.nPage = si.nPage;
1212 if (bScrollBarWillBeVisible || bScrollBarWasVisible) {
1213 if (editor->hWnd) {
1214 SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1215 } else {
1216 ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1217 ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1218 }
1219 /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1220 bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1221 }
1222 }
1223
1224 if (si.fMask & SIF_DISABLENOSCROLL) {
1225 bScrollBarWillBeVisible = TRUE;
1226 } else if (!(editor->styleFlags & WS_VSCROLL)) {
1227 bScrollBarWillBeVisible = FALSE;
1228 }
1229
1230 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1231 ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1232 bScrollBarWillBeVisible);
1233 }
1234
1235 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1236 {
1237 ME_Run *pRun = &pCursor->pRun->member.run;
1238 ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1239 ME_DisplayItem *pPara = pCursor->pPara;
1240 int x, y, yheight;
1241
1242 assert(pRow);
1243 assert(pPara);
1244
1245 x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset);
1246 if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1247 x = x + 1 - editor->sizeWindow.cx;
1248 else if (x > editor->horz_si.nPos)
1249 x = editor->horz_si.nPos;
1250
1251 y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1252 yheight = pRow->member.row.nHeight;
1253
1254 if (y < editor->vert_si.nPos)
1255 ME_ScrollAbs(editor, x, y);
1256 else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1257 ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1258 else if (x != editor->horz_si.nPos)
1259 ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1260 }
1261
1262
1263 void
1264 ME_InvalidateSelection(ME_TextEditor *editor)
1265 {
1266 ME_DisplayItem *para1, *para2;
1267 int nStart, nEnd;
1268 int len = ME_GetTextLength(editor);
1269
1270 ME_GetSelection(editor, &nStart, &nEnd);
1271 /* if both old and new selection are 0-char (= caret only), then
1272 there's no (inverted) area to be repainted, neither old nor new */
1273 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1274 return;
1275 ME_WrapMarkedParagraphs(editor);
1276 ME_GetSelectionParas(editor, &para1, &para2);
1277 assert(para1->type == diParagraph);
1278 assert(para2->type == diParagraph);
1279 /* last selection markers aren't always updated, which means
1280 * they can point past the end of the document */
1281 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1282 ME_MarkForPainting(editor,
1283 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
1284 editor->pBuffer->pLast);
1285 } else {
1286 /* if the start part of selection is being expanded or contracted... */
1287 if (nStart < editor->nLastSelStart) {
1288 ME_MarkForPainting(editor, para1, editor->pLastSelStartPara->member.para.next_para);
1289 } else if (nStart > editor->nLastSelStart) {
1290 ME_MarkForPainting(editor, editor->pLastSelStartPara, para1->member.para.next_para);
1291 }
1292
1293 /* if the end part of selection is being contracted or expanded... */
1294 if (nEnd < editor->nLastSelEnd) {
1295 ME_MarkForPainting(editor, para2, editor->pLastSelEndPara->member.para.next_para);
1296 } else if (nEnd > editor->nLastSelEnd) {
1297 ME_MarkForPainting(editor, editor->pLastSelEndPara, para2->member.para.next_para);
1298 }
1299 }
1300
1301 ME_InvalidateMarkedParagraphs(editor);
1302 /* remember the last invalidated position */
1303 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1304 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1305 assert(editor->pLastSelStartPara->type == diParagraph);
1306 assert(editor->pLastSelEndPara->type == diParagraph);
1307 }
1308
1309 BOOL
1310 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1311 {
1312 /* TODO: Zoom images and objects */
1313
1314 if (numerator == 0 && denominator == 0)
1315 {
1316 editor->nZoomNumerator = editor->nZoomDenominator = 0;
1317 return TRUE;
1318 }
1319 if (numerator <= 0 || denominator <= 0)
1320 return FALSE;
1321 if (numerator * 64 <= denominator || numerator / denominator >= 64)
1322 return FALSE;
1323
1324 editor->nZoomNumerator = numerator;
1325 editor->nZoomDenominator = denominator;
1326
1327 ME_RewrapRepaint(editor);
1328 return TRUE;
1329 }