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