server: Move socket async activation to sock_poll_event.
[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   /* 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_GetSelectionOfs(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 /* The documented widths are in points (72 dpi), but converting them to
465  * 96 dpi (standard display resolution) avoids dealing with fractions. */
466 static const struct {unsigned width : 8, pen_style : 4, dble : 1;} border_details[] = {
467   /* none */            {0, PS_SOLID, FALSE},
468   /* 3/4 */             {1, PS_SOLID, FALSE},
469   /* 1 1/2 */           {2, PS_SOLID, FALSE},
470   /* 2 1/4 */           {3, PS_SOLID, FALSE},
471   /* 3 */               {4, PS_SOLID, FALSE},
472   /* 4 1/2 */           {6, PS_SOLID, FALSE},
473   /* 6 */               {8, PS_SOLID, FALSE},
474   /* 3/4 double */      {1, PS_SOLID, TRUE},
475   /* 1 1/2 double */    {2, PS_SOLID, TRUE},
476   /* 2 1/4 double */    {3, PS_SOLID, TRUE},
477   /* 3/4 gray */        {1, PS_DOT /* FIXME */, FALSE},
478   /* 1 1/2 dashed */    {2, PS_DASH, FALSE},
479 };
480
481 static const COLORREF pen_colors[16] = {
482   /* Black */           RGB(0x00, 0x00, 0x00),  /* Blue */            RGB(0x00, 0x00, 0xFF),
483   /* Cyan */            RGB(0x00, 0xFF, 0xFF),  /* Green */           RGB(0x00, 0xFF, 0x00),
484   /* Magenta */         RGB(0xFF, 0x00, 0xFF),  /* Red */             RGB(0xFF, 0x00, 0x00),
485   /* Yellow */          RGB(0xFF, 0xFF, 0x00),  /* White */           RGB(0xFF, 0xFF, 0xFF),
486   /* Dark blue */       RGB(0x00, 0x00, 0x80),  /* Dark cyan */       RGB(0x00, 0x80, 0x80),
487   /* Dark green */      RGB(0x00, 0x80, 0x80),  /* Dark magenta */    RGB(0x80, 0x00, 0x80),
488   /* Dark red */        RGB(0x80, 0x00, 0x00),  /* Dark yellow */     RGB(0x80, 0x80, 0x00),
489   /* Dark gray */       RGB(0x80, 0x80, 0x80),  /* Light gray */      RGB(0xc0, 0xc0, 0xc0),
490 };
491
492 static int ME_GetBorderPenWidth(ME_Context* c, int idx)
493 {
494   int width = border_details[idx].width;
495
496   if (c->dpi.cx != 96)
497     width = MulDiv(width, c->dpi.cx, 96);
498
499   if (c->editor->nZoomNumerator != 0)
500     width = MulDiv(width, c->editor->nZoomNumerator, c->editor->nZoomDenominator);
501
502   return width;
503 }
504
505 int ME_GetParaBorderWidth(ME_Context* c, int flags)
506 {
507   int idx = (flags >> 8) & 0xF;
508   int width;
509
510   if (idx >= sizeof(border_details) / sizeof(border_details[0]))
511   {
512       FIXME("Unsupported border value %d\n", idx);
513       return 0;
514   }
515   width = ME_GetBorderPenWidth(c, idx);
516   if (border_details[idx].dble) width = width * 2 + 1;
517   return width;
518 }
519
520 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
521 {
522   int           idx, border_width, top_border, bottom_border;
523   RECT          rc;
524   BOOL          hasParaBorder;
525
526   SetRectEmpty(bounds);
527   if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
528
529   border_width = top_border = bottom_border = 0;
530   idx = (para->pFmt->wBorders >> 8) & 0xF;
531   hasParaBorder = (!(c->editor->bEmulateVersion10 &&
532                      para->pFmt->dwMask & PFM_TABLE &&
533                      para->pFmt->wEffects & PFE_TABLE) &&
534                    (para->pFmt->dwMask & PFM_BORDER) &&
535                     idx != 0 &&
536                     (para->pFmt->wBorders & 0xF));
537   if (hasParaBorder)
538   {
539     /* FIXME: wBorders is not stored as MSDN says in v1.0 - 4.1 of richedit
540      * controls. It actually stores the paragraph or row border style. Although
541      * the value isn't used for drawing, it is used for streaming out rich text.
542      *
543      * wBorders stores the border style for each side (top, left, bottom, right)
544      * using nibble (4 bits) to store each border style.  The rich text format
545      * control words, and their associated value are the following:
546      *   \brdrdash       0
547      *   \brdrdashsm     1
548      *   \brdrdb         2
549      *   \brdrdot        3
550      *   \brdrhair       4
551      *   \brdrs          5
552      *   \brdrth         6
553      *   \brdrtriple     7
554      *
555      * The order of the sides stored actually differs from v1.0 to 3.0 and v4.1.
556      * The mask corresponding to each side for the version are the following:
557      *     mask       v1.0-3.0    v4.1
558      *     0x000F     top         left
559      *     0x00F0     left        top
560      *     0x0F00     bottom      right
561      *     0xF000     right       bottom
562      */
563     if (para->pFmt->wBorders & 0x00B0)
564       FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
565     border_width = ME_GetParaBorderWidth(c, para->pFmt->wBorders);
566     if (para->pFmt->wBorders & 4)       top_border = border_width;
567     if (para->pFmt->wBorders & 8)       bottom_border = border_width;
568   }
569
570   if (para->pFmt->dwMask & PFM_SPACEBEFORE)
571   {
572     rc.left = c->rcView.left;
573     rc.right = c->rcView.right;
574     rc.top = y;
575     bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
576     rc.bottom = y + bounds->top + top_border;
577     FillRect(c->hDC, &rc, c->editor->hbrBackground);
578   }
579
580   if (para->pFmt->dwMask & PFM_SPACEAFTER)
581   {
582     rc.left = c->rcView.left;
583     rc.right = c->rcView.right;
584     rc.bottom = y + para->nHeight;
585     bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
586     rc.top = rc.bottom - bounds->bottom - bottom_border;
587     FillRect(c->hDC, &rc, c->editor->hbrBackground);
588   }
589
590   /* Native richedit doesn't support paragraph borders in v1.0 - 4.1,
591    * but might support it in later versions. */
592   if (hasParaBorder) {
593     int         pen_width, rightEdge;
594     COLORREF    pencr;
595     HPEN        pen = NULL, oldpen = NULL;
596     POINT       pt;
597
598     if (para->pFmt->wBorders & 64) /* autocolor */
599       pencr = ITextHost_TxGetSysColor(c->editor->texthost,
600                                       COLOR_WINDOWTEXT);
601     else
602       pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
603
604     rightEdge = c->pt.x + max(c->editor->sizeWindow.cx,
605                               c->editor->nTotalWidth);
606
607     pen_width = ME_GetBorderPenWidth(c, idx);
608     pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
609     oldpen = SelectObject(c->hDC, pen);
610     MoveToEx(c->hDC, 0, 0, &pt);
611
612     /* before & after spaces are not included in border */
613
614     /* helper to draw the double lines in case of corner */
615 #define DD(x)   ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
616
617     if (para->pFmt->wBorders & 1)
618     {
619       MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
620       LineTo(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom);
621       if (border_details[idx].dble) {
622         rc.left = c->pt.x + 1;
623         rc.right = rc.left + border_width;
624         rc.top = y + bounds->top;
625         rc.bottom = y + para->nHeight - bounds->bottom;
626         FillRect(c->hDC, &rc, c->editor->hbrBackground);
627         MoveToEx(c->hDC, c->pt.x + pen_width + 1, y + bounds->top + DD(4), NULL);
628         LineTo(c->hDC, c->pt.x + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
629       }
630       bounds->left += border_width;
631     }
632     if (para->pFmt->wBorders & 2)
633     {
634       MoveToEx(c->hDC, rightEdge - 1, y + bounds->top, NULL);
635       LineTo(c->hDC, rightEdge - 1, y + para->nHeight - bounds->bottom);
636       if (border_details[idx].dble) {
637         rc.left = rightEdge - pen_width - 1;
638         rc.right = rc.left + pen_width;
639         rc.top = y + bounds->top;
640         rc.bottom = y + para->nHeight - bounds->bottom;
641         FillRect(c->hDC, &rc, c->editor->hbrBackground);
642         MoveToEx(c->hDC, rightEdge - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
643         LineTo(c->hDC, rightEdge - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
644       }
645       bounds->right += border_width;
646     }
647     if (para->pFmt->wBorders & 4)
648     {
649       MoveToEx(c->hDC, c->pt.x, y + bounds->top, NULL);
650       LineTo(c->hDC, rightEdge, y + bounds->top);
651       if (border_details[idx].dble) {
652         MoveToEx(c->hDC, c->pt.x + DD(1), y + bounds->top + pen_width + 1, NULL);
653         LineTo(c->hDC, rightEdge - DD(2), y + bounds->top + pen_width + 1);
654       }
655       bounds->top += border_width;
656     }
657     if (para->pFmt->wBorders & 8)
658     {
659       MoveToEx(c->hDC, c->pt.x, y + para->nHeight - bounds->bottom - 1, NULL);
660       LineTo(c->hDC, rightEdge, y + para->nHeight - bounds->bottom - 1);
661       if (border_details[idx].dble) {
662         MoveToEx(c->hDC, c->pt.x + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
663         LineTo(c->hDC, rightEdge - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
664       }
665       bounds->bottom += border_width;
666     }
667 #undef DD
668
669     MoveToEx(c->hDC, pt.x, pt.y, NULL);
670     SelectObject(c->hDC, oldpen);
671     DeleteObject(pen);
672   }
673 }
674
675 static void ME_DrawTableBorders(ME_Context *c, ME_DisplayItem *paragraph)
676 {
677   ME_Paragraph *para = &paragraph->member.para;
678   if (!c->editor->bEmulateVersion10) /* v4.1 */
679   {
680     if (para->pCell)
681     {
682       RECT rc;
683       ME_Cell *cell = &para->pCell->member.cell;
684       ME_DisplayItem *paraAfterRow;
685       HPEN pen, oldPen;
686       LOGBRUSH logBrush;
687       HBRUSH brush;
688       COLORREF color;
689       POINT oldPt;
690       int width;
691       BOOL atTop = (para->pCell != para->prev_para->member.para.pCell);
692       BOOL atBottom = (para->pCell != para->next_para->member.para.pCell);
693       int top = c->pt.y + (atTop ? cell->pt.y : para->pt.y);
694       int bottom = (atBottom ?
695                     c->pt.y + cell->pt.y + cell->nHeight :
696                     top + para->nHeight + (atTop ? cell->yTextOffset : 0));
697       rc.left = c->pt.x + cell->pt.x;
698       rc.right = rc.left + cell->nWidth;
699       if (atTop) {
700         /* Erase gap before text if not all borders are the same height. */
701         width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
702         rc.top = top + width;
703         width = cell->yTextOffset - width;
704         rc.bottom = rc.top + width;
705         if (width) {
706           FillRect(c->hDC, &rc, c->editor->hbrBackground);
707         }
708       }
709       /* Draw cell borders.
710        * The order borders are draw in is left, top, bottom, right in order
711        * to be consistent with native richedit.  This is noticeable from the
712        * overlap of borders of different colours. */
713       if (!(para->nFlags & MEPF_ROWEND)) {
714         rc.top = top;
715         rc.bottom = bottom;
716         if (cell->border.left.width > 0)
717         {
718           color = cell->border.left.colorRef;
719           width = max(ME_twips2pointsX(c, cell->border.left.width), 1);
720         } else {
721           color = RGB(192,192,192);
722           width = 1;
723         }
724         logBrush.lbStyle = BS_SOLID;
725         logBrush.lbColor = color;
726         logBrush.lbHatch = 0;
727         pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
728                            width, &logBrush, 0, NULL);
729         oldPen = SelectObject(c->hDC, pen);
730         MoveToEx(c->hDC, rc.left, rc.top, &oldPt);
731         LineTo(c->hDC, rc.left, rc.bottom);
732         SelectObject(c->hDC, oldPen);
733         DeleteObject(pen);
734         MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
735       }
736
737       if (atTop) {
738         if (cell->border.top.width > 0)
739         {
740           brush = CreateSolidBrush(cell->border.top.colorRef);
741           width = max(ME_twips2pointsY(c, cell->border.top.width), 1);
742         } else {
743           brush = GetStockObject(LTGRAY_BRUSH);
744           width = 1;
745         }
746         rc.top = top;
747         rc.bottom = rc.top + width;
748         FillRect(c->hDC, &rc, brush);
749         if (cell->border.top.width > 0)
750           DeleteObject(brush);
751       }
752
753       /* Draw the bottom border if at the last paragraph in the cell, and when
754        * in the last row of the table. */
755       if (atBottom) {
756         int oldLeft = rc.left;
757         width = max(ME_twips2pointsY(c, cell->border.bottom.width), 1);
758         paraAfterRow = ME_GetTableRowEnd(paragraph)->member.para.next_para;
759         if (paraAfterRow->member.para.nFlags & MEPF_ROWSTART) {
760           ME_DisplayItem *nextEndCell;
761           nextEndCell = ME_FindItemBack(ME_GetTableRowEnd(paraAfterRow), diCell);
762           assert(nextEndCell && !nextEndCell->member.cell.next_cell);
763           rc.left = c->pt.x + nextEndCell->member.cell.pt.x;
764           /* FIXME: Native draws FROM the bottom of the table rather than
765            * TO the bottom of the table in this case, but just doing so here
766            * will cause the next row to erase the border. */
767           /*
768           rc.top = bottom;
769           rc.bottom = rc.top + width;
770            */
771         }
772         if (rc.left < rc.right) {
773           if (cell->border.bottom.width > 0) {
774             brush = CreateSolidBrush(cell->border.bottom.colorRef);
775           } else {
776             brush = GetStockObject(LTGRAY_BRUSH);
777           }
778           rc.bottom = bottom;
779           rc.top = rc.bottom - width;
780           FillRect(c->hDC, &rc, brush);
781           if (cell->border.bottom.width > 0)
782             DeleteObject(brush);
783         }
784         rc.left = oldLeft;
785       }
786
787       /* Right border only drawn if at the end of the table row. */
788       if (!cell->next_cell->member.cell.next_cell &&
789           !(para->nFlags & MEPF_ROWSTART))
790       {
791         rc.top = top;
792         rc.bottom = bottom;
793         if (cell->border.right.width > 0) {
794           color = cell->border.right.colorRef;
795           width = max(ME_twips2pointsX(c, cell->border.right.width), 1);
796         } else {
797           color = RGB(192,192,192);
798           width = 1;
799         }
800         logBrush.lbStyle = BS_SOLID;
801         logBrush.lbColor = color;
802         logBrush.lbHatch = 0;
803         pen = ExtCreatePen(PS_GEOMETRIC|PS_SOLID|PS_ENDCAP_FLAT|PS_JOIN_MITER,
804                            width, &logBrush, 0, NULL);
805         oldPen = SelectObject(c->hDC, pen);
806         MoveToEx(c->hDC, rc.right - 1, rc.top, &oldPt);
807         LineTo(c->hDC, rc.right - 1, rc.bottom);
808         SelectObject(c->hDC, oldPen);
809         DeleteObject(pen);
810         MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
811       }
812     }
813   } else { /* v1.0 - 3.0 */
814     /* Draw simple table border */
815     if (para->pFmt->dwMask & PFM_TABLE && para->pFmt->wEffects & PFE_TABLE) {
816       HPEN pen = NULL, oldpen = NULL;
817       int i, firstX, startX, endX, rowY, rowBottom, nHeight;
818       POINT oldPt;
819       PARAFORMAT2 *pNextFmt;
820
821       pen = CreatePen(PS_SOLID, 0, para->border.top.colorRef);
822       oldpen = SelectObject(c->hDC, pen);
823
824       /* Find the start relative to the text */
825       firstX = c->pt.x + ME_FindItemFwd(paragraph, diRun)->member.run.pt.x;
826       /* Go back by the horizontal gap, which is stored in dxOffset */
827       firstX -= ME_twips2pointsX(c, para->pFmt->dxOffset);
828       /* The left edge, stored in dxStartIndent affected just the first edge */
829       startX = firstX - ME_twips2pointsX(c, para->pFmt->dxStartIndent);
830       rowY = c->pt.y + para->pt.y;
831       if (para->pFmt->dwMask & PFM_SPACEBEFORE)
832         rowY += ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
833       nHeight = ME_FindItemFwd(paragraph, diStartRow)->member.row.nHeight;
834       rowBottom = rowY + nHeight;
835
836       /* Draw horizontal lines */
837       MoveToEx(c->hDC, firstX, rowY, &oldPt);
838       i = para->pFmt->cTabCount - 1;
839       endX = startX + ME_twips2pointsX(c, para->pFmt->rgxTabs[i] & 0x00ffffff) + 1;
840       LineTo(c->hDC, endX, rowY);
841       pNextFmt = para->next_para->member.para.pFmt;
842       /* The bottom of the row only needs to be drawn if the next row is
843        * not a table. */
844       if (!(pNextFmt && pNextFmt->dwMask & PFM_TABLE && pNextFmt->wEffects &&
845             para->nRows == 1))
846       {
847         /* Decrement rowBottom to draw the bottom line within the row, and
848          * to not draw over this line when drawing the vertical lines. */
849         rowBottom--;
850         MoveToEx(c->hDC, firstX, rowBottom, NULL);
851         LineTo(c->hDC, endX, rowBottom);
852       }
853
854       /* Draw vertical lines */
855       MoveToEx(c->hDC, firstX, rowY, NULL);
856       LineTo(c->hDC, firstX, rowBottom);
857       for (i = 0; i < para->pFmt->cTabCount; i++)
858       {
859         int rightBoundary = para->pFmt->rgxTabs[i] & 0x00ffffff;
860         endX = startX + ME_twips2pointsX(c, rightBoundary);
861         MoveToEx(c->hDC, endX, rowY, NULL);
862         LineTo(c->hDC, endX, rowBottom);
863       }
864
865       MoveToEx(c->hDC, oldPt.x, oldPt.y, NULL);
866       SelectObject(c->hDC, oldpen);
867       DeleteObject(pen);
868     }
869   }
870 }
871
872 static void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph)
873 {
874   int align = SetTextAlign(c->hDC, TA_BASELINE);
875   ME_DisplayItem *p;
876   ME_Run *run;
877   ME_Paragraph *para = NULL;
878   RECT rc, bounds;
879   int y;
880   int height = 0, baseline = 0, no=0;
881   BOOL visible = FALSE;
882
883   rc.left = c->pt.x;
884   rc.right = c->rcView.right;
885
886   assert(paragraph);
887   para = &paragraph->member.para;
888   y = c->pt.y + para->pt.y;
889   if (para->pCell)
890   {
891     ME_Cell *cell = &para->pCell->member.cell;
892     rc.left = c->pt.x + cell->pt.x;
893     rc.right = rc.left + cell->nWidth;
894   }
895   if (para->nFlags & MEPF_ROWSTART) {
896     ME_Cell *cell = &para->next_para->member.para.pCell->member.cell;
897     rc.right = c->pt.x + cell->pt.x;
898   } else if (para->nFlags & MEPF_ROWEND) {
899     ME_Cell *cell = &para->prev_para->member.para.pCell->member.cell;
900     rc.left = c->pt.x + cell->pt.x + cell->nWidth;
901   }
902   ME_DrawParaDecoration(c, para, y, &bounds);
903   y += bounds.top;
904   if (bounds.left || bounds.right) {
905     rc.left = max(rc.left, c->pt.x + bounds.left);
906     rc.right = min(rc.right, c->pt.x - bounds.right
907                              + max(c->editor->sizeWindow.cx,
908                                    c->editor->nTotalWidth));
909   }
910
911   for (p = paragraph->next; p != para->next_para; p = p->next)
912   {
913     switch(p->type) {
914       case diParagraph:
915         assert(FALSE);
916         break;
917       case diStartRow:
918         y += height;
919         rc.top = y;
920         if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
921           rc.bottom = y + para->nHeight;
922         } else {
923           rc.bottom = y + p->member.row.nHeight;
924         }
925         visible = RectVisible(c->hDC, &rc);
926         if (visible) {
927           FillRect(c->hDC, &rc, c->editor->hbrBackground);
928         }
929         if (bounds.right)
930         {
931           /* If scrolled to the right past the end of the text, then
932            * there may be space to the right of the paragraph border. */
933           RECT rcAfterBrdr = rc;
934           rcAfterBrdr.left = rc.right + bounds.right;
935           rcAfterBrdr.right = c->rcView.right;
936           if (RectVisible(c->hDC, &rcAfterBrdr))
937             FillRect(c->hDC, &rcAfterBrdr, c->editor->hbrBackground);
938         }
939         if (me_debug)
940         {
941           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
942           WCHAR buf[128];
943           POINT pt = c->pt;
944           wsprintfW(buf, wszRowDebug, no);
945           pt.y = 12+y;
946           ME_DebugWrite(c->hDC, &pt, buf);
947         }
948
949         height = p->member.row.nHeight;
950         baseline = p->member.row.nBaseline;
951         break;
952       case diRun:
953         assert(para);
954         run = &p->member.run;
955         if (visible && me_debug) {
956           RECT rc;
957           rc.left = c->pt.x + run->pt.x;
958           rc.right = rc.left + run->nWidth;
959           rc.top = c->pt.y + para->pt.y + run->pt.y;
960           rc.bottom = rc.bottom + height;
961           TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
962           if (run->nFlags & MERF_SKIPPED)
963             DrawFocusRect(c->hDC, &rc);
964           else
965             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
966         }
967         if (visible)
968           ME_DrawRun(c, c->pt.x + run->pt.x,
969                      c->pt.y + para->pt.y + run->pt.y + baseline, p, para);
970         if (me_debug)
971         {
972           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
973           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
974           WCHAR buf[2560];
975           POINT pt;
976           pt.x = c->pt.x + run->pt.x;
977           pt.y = c->pt.y + para->pt.y + run->pt.y;
978           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
979           ME_DebugWrite(c->hDC, &pt, buf);
980         }
981         break;
982       case diCell:
983         /* Clear any space at the bottom of the cell after the text. */
984         if (para->nFlags & (MEPF_ROWSTART|MEPF_ROWEND))
985           break;
986         y += height;
987         rc.top = c->pt.y + para->pt.y + para->nHeight;
988         rc.bottom = c->pt.y + p->member.cell.pt.y + p->member.cell.nHeight;
989         if (RectVisible(c->hDC, &rc))
990         {
991           FillRect(c->hDC, &rc, c->editor->hbrBackground);
992         }
993         break;
994       default:
995         break;
996     }
997     no++;
998   }
999
1000   ME_DrawTableBorders(c, paragraph);
1001
1002   SetTextAlign(c->hDC, align);
1003 }
1004
1005 void ME_ScrollAbs(ME_TextEditor *editor, int x, int y)
1006 {
1007   BOOL bScrollBarIsVisible, bScrollBarWillBeVisible;
1008   int scrollX = 0, scrollY = 0;
1009
1010   if (editor->horz_si.nPos != x) {
1011     x = min(x, editor->horz_si.nMax);
1012     x = max(x, editor->horz_si.nMin);
1013     scrollX = editor->horz_si.nPos - x;
1014     editor->horz_si.nPos = x;
1015     if (editor->horz_si.nMax > 0xFFFF) /* scale to 16-bit value */
1016       x = MulDiv(x, 0xFFFF, editor->horz_si.nMax);
1017     ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, x, TRUE);
1018   }
1019
1020   if (editor->vert_si.nPos != y) {
1021     y = min(y, editor->vert_si.nMax - (int)editor->vert_si.nPage);
1022     y = max(y, editor->vert_si.nMin);
1023     scrollY = editor->vert_si.nPos - y;
1024     editor->vert_si.nPos = y;
1025     if (editor->vert_si.nMax > 0xFFFF) /* scale to 16-bit value */
1026       y = MulDiv(y, 0xFFFF, editor->vert_si.nMax);
1027     ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, y, TRUE);
1028   }
1029
1030   if (abs(scrollX) > editor->sizeWindow.cx ||
1031       abs(scrollY) > editor->sizeWindow.cy)
1032     ITextHost_TxInvalidateRect(editor->texthost, NULL, TRUE);
1033   else
1034     ITextHost_TxScrollWindowEx(editor->texthost, scrollX, scrollY,
1035                                &editor->rcFormat, &editor->rcFormat,
1036                                NULL, NULL, SW_INVALIDATE);
1037   ME_Repaint(editor);
1038
1039   if (editor->hWnd)
1040   {
1041     LONG winStyle = GetWindowLongW(editor->hWnd, GWL_STYLE);
1042     if (editor->styleFlags & WS_HSCROLL)
1043     {
1044       bScrollBarIsVisible = (winStyle & WS_HSCROLL) != 0;
1045       bScrollBarWillBeVisible = (editor->nTotalWidth > editor->sizeWindow.cx
1046                                  && (editor->styleFlags & WS_HSCROLL))
1047                                 || (editor->styleFlags & ES_DISABLENOSCROLL);
1048       if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1049         ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ,
1050                                   bScrollBarWillBeVisible);
1051     }
1052
1053     if (editor->styleFlags & WS_VSCROLL)
1054     {
1055       bScrollBarIsVisible = (winStyle & WS_VSCROLL) != 0;
1056       bScrollBarWillBeVisible = (editor->nTotalLength > editor->sizeWindow.cy
1057                                  && (editor->styleFlags & WS_VSCROLL)
1058                                  && (editor->styleFlags & ES_MULTILINE))
1059                                 || (editor->styleFlags & ES_DISABLENOSCROLL);
1060       if (bScrollBarIsVisible != bScrollBarWillBeVisible)
1061         ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1062                                   bScrollBarWillBeVisible);
1063     }
1064   }
1065   ME_UpdateScrollBar(editor);
1066 }
1067
1068 void ME_HScrollAbs(ME_TextEditor *editor, int x)
1069 {
1070   ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1071 }
1072
1073 void ME_VScrollAbs(ME_TextEditor *editor, int y)
1074 {
1075   ME_ScrollAbs(editor, editor->horz_si.nPos, y);
1076 }
1077
1078 void ME_ScrollUp(ME_TextEditor *editor, int cy)
1079 {
1080   ME_VScrollAbs(editor, editor->vert_si.nPos - cy);
1081 }
1082
1083 void ME_ScrollDown(ME_TextEditor *editor, int cy)
1084 {
1085   ME_VScrollAbs(editor, editor->vert_si.nPos + cy);
1086 }
1087
1088 void ME_ScrollLeft(ME_TextEditor *editor, int cx)
1089 {
1090   ME_HScrollAbs(editor, editor->horz_si.nPos - cx);
1091 }
1092
1093 void ME_ScrollRight(ME_TextEditor *editor, int cx)
1094 {
1095   ME_HScrollAbs(editor, editor->horz_si.nPos + cx);
1096 }
1097
1098 /* Calculates the visiblity after a call to SetScrollRange or
1099  * SetScrollInfo with SIF_RANGE. */
1100 static BOOL ME_PostSetScrollRangeVisibility(SCROLLINFO *si)
1101 {
1102   if (si->fMask & SIF_DISABLENOSCROLL)
1103     return TRUE;
1104
1105   /* This must match the check in SetScrollInfo to determine whether
1106    * to show or hide the scrollbars. */
1107   return si->nMin < si->nMax - max(si->nPage - 1, 0);
1108 }
1109
1110 void ME_UpdateScrollBar(ME_TextEditor *editor)
1111 {
1112   /* Note that this is the only function that should ever call
1113    * SetScrollInfo with SIF_PAGE or SIF_RANGE. */
1114
1115   SCROLLINFO si;
1116   BOOL bScrollBarWasVisible, bScrollBarWillBeVisible;
1117
1118   if (ME_WrapMarkedParagraphs(editor))
1119     FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
1120
1121   si.cbSize = sizeof(si);
1122   si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
1123   si.nMin = 0;
1124   if (editor->styleFlags & ES_DISABLENOSCROLL)
1125     si.fMask |= SIF_DISABLENOSCROLL;
1126
1127   /* Update horizontal scrollbar */
1128   bScrollBarWasVisible = editor->horz_si.nMax > editor->horz_si.nPage;
1129   bScrollBarWillBeVisible = editor->nTotalWidth > editor->sizeWindow.cx;
1130   if (editor->horz_si.nPos && !bScrollBarWillBeVisible)
1131   {
1132     ME_HScrollAbs(editor, 0);
1133     /* ME_HScrollAbs will call this function,
1134      * so nothing else needs to be done here. */
1135     return;
1136   }
1137
1138   si.nMax = editor->nTotalWidth;
1139   si.nPos = editor->horz_si.nPos;
1140   si.nPage = editor->sizeWindow.cx;
1141
1142   if (si.nMax != editor->horz_si.nMax ||
1143       si.nPage != editor->horz_si.nPage)
1144   {
1145     TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1146     editor->horz_si.nMax = si.nMax;
1147     editor->horz_si.nPage = si.nPage;
1148     if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1149         editor->styleFlags & WS_HSCROLL)
1150     {
1151       if (si.nMax > 0xFFFF)
1152       {
1153         /* Native scales the scrollbar info to 16-bit external values. */
1154         si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1155         si.nMax = 0xFFFF;
1156       }
1157       if (editor->hWnd) {
1158         SetScrollInfo(editor->hWnd, SB_HORZ, &si, TRUE);
1159       } else {
1160         ITextHost_TxSetScrollRange(editor->texthost, SB_HORZ, si.nMin, si.nMax, FALSE);
1161         ITextHost_TxSetScrollPos(editor->texthost, SB_HORZ, si.nPos, TRUE);
1162       }
1163       /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1164       bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1165     }
1166   }
1167
1168   if (editor->styleFlags & WS_HSCROLL)
1169   {
1170     if (si.fMask & SIF_DISABLENOSCROLL) {
1171       bScrollBarWillBeVisible = TRUE;
1172     } else if (!(editor->styleFlags & WS_HSCROLL)) {
1173       bScrollBarWillBeVisible = FALSE;
1174     }
1175
1176     if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1177       ITextHost_TxShowScrollBar(editor->texthost, SB_HORZ, bScrollBarWillBeVisible);
1178   }
1179
1180   /* Update vertical scrollbar */
1181   bScrollBarWasVisible = editor->vert_si.nMax > editor->vert_si.nPage;
1182   bScrollBarWillBeVisible = editor->nTotalLength > editor->sizeWindow.cy &&
1183                             (editor->styleFlags & ES_MULTILINE);
1184
1185   if (editor->vert_si.nPos && !bScrollBarWillBeVisible)
1186   {
1187     ME_VScrollAbs(editor, 0);
1188     /* ME_VScrollAbs will call this function,
1189      * so nothing else needs to be done here. */
1190     return;
1191   }
1192
1193   si.nMax = editor->nTotalLength;
1194   si.nPos = editor->vert_si.nPos;
1195   si.nPage = editor->sizeWindow.cy;
1196
1197   if (si.nMax != editor->vert_si.nMax ||
1198       si.nPage != editor->vert_si.nPage)
1199   {
1200     TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
1201     editor->vert_si.nMax = si.nMax;
1202     editor->vert_si.nPage = si.nPage;
1203     if ((bScrollBarWillBeVisible || bScrollBarWasVisible) &&
1204         editor->styleFlags & WS_VSCROLL)
1205     {
1206       if (si.nMax > 0xFFFF)
1207       {
1208         /* Native scales the scrollbar info to 16-bit external values. */
1209         si.nPos = MulDiv(si.nPos, 0xFFFF, si.nMax);
1210         si.nMax = 0xFFFF;
1211       }
1212       if (editor->hWnd) {
1213         SetScrollInfo(editor->hWnd, SB_VERT, &si, TRUE);
1214       } else {
1215         ITextHost_TxSetScrollRange(editor->texthost, SB_VERT, si.nMin, si.nMax, FALSE);
1216         ITextHost_TxSetScrollPos(editor->texthost, SB_VERT, si.nPos, TRUE);
1217       }
1218       /* SetScrollInfo or SetScrollRange change scrollbar visibility. */
1219       bScrollBarWasVisible = ME_PostSetScrollRangeVisibility(&si);
1220     }
1221   }
1222
1223   if (editor->styleFlags & WS_VSCROLL)
1224   {
1225     if (si.fMask & SIF_DISABLENOSCROLL) {
1226       bScrollBarWillBeVisible = TRUE;
1227     } else if (!(editor->styleFlags & WS_VSCROLL)) {
1228       bScrollBarWillBeVisible = FALSE;
1229     }
1230
1231     if (bScrollBarWasVisible != bScrollBarWillBeVisible)
1232       ITextHost_TxShowScrollBar(editor->texthost, SB_VERT,
1233                                 bScrollBarWillBeVisible);
1234   }
1235 }
1236
1237 void ME_EnsureVisible(ME_TextEditor *editor, ME_Cursor *pCursor)
1238 {
1239   ME_Run *pRun = &pCursor->pRun->member.run;
1240   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1241   ME_DisplayItem *pPara = pCursor->pPara;
1242   int x, y, yheight;
1243
1244   assert(pRow);
1245   assert(pPara);
1246
1247   if (editor->styleFlags & ES_AUTOHSCROLL)
1248   {
1249     x = pRun->pt.x + ME_PointFromChar(editor, pRun, pCursor->nOffset);
1250     if (x > editor->horz_si.nPos + editor->sizeWindow.cx)
1251       x = x + 1 - editor->sizeWindow.cx;
1252     else if (x > editor->horz_si.nPos)
1253       x = editor->horz_si.nPos;
1254
1255     if (~editor->styleFlags & ES_AUTOVSCROLL)
1256     {
1257       ME_HScrollAbs(editor, x);
1258       return;
1259     }
1260   } else {
1261     if (~editor->styleFlags & ES_AUTOVSCROLL)
1262       return;
1263     x = editor->horz_si.nPos;
1264   }
1265
1266   y = pPara->member.para.pt.y + pRow->member.row.pt.y;
1267   yheight = pRow->member.row.nHeight;
1268
1269   if (y < editor->vert_si.nPos)
1270     ME_ScrollAbs(editor, x, y);
1271   else if (y + yheight > editor->vert_si.nPos + editor->sizeWindow.cy)
1272     ME_ScrollAbs(editor, x, y + yheight - editor->sizeWindow.cy);
1273   else if (x != editor->horz_si.nPos)
1274     ME_ScrollAbs(editor, x, editor->vert_si.nPos);
1275 }
1276
1277
1278 void
1279 ME_InvalidateSelection(ME_TextEditor *editor)
1280 {
1281   ME_DisplayItem *para1, *para2;
1282   int nStart, nEnd;
1283   int len = ME_GetTextLength(editor);
1284
1285   ME_GetSelectionOfs(editor, &nStart, &nEnd);
1286   /* if both old and new selection are 0-char (= caret only), then
1287   there's no (inverted) area to be repainted, neither old nor new */
1288   if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
1289     return;
1290   ME_WrapMarkedParagraphs(editor);
1291   ME_GetSelectionParas(editor, &para1, &para2);
1292   assert(para1->type == diParagraph);
1293   assert(para2->type == diParagraph);
1294   /* last selection markers aren't always updated, which means
1295    * they can point past the end of the document */
1296   if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
1297     ME_MarkForPainting(editor,
1298         ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
1299         editor->pBuffer->pLast);
1300   } else {
1301     /* if the start part of selection is being expanded or contracted... */
1302     if (nStart < editor->nLastSelStart) {
1303       ME_MarkForPainting(editor, para1, editor->pLastSelStartPara->member.para.next_para);
1304     } else if (nStart > editor->nLastSelStart) {
1305       ME_MarkForPainting(editor, editor->pLastSelStartPara, para1->member.para.next_para);
1306     }
1307
1308     /* if the end part of selection is being contracted or expanded... */
1309     if (nEnd < editor->nLastSelEnd) {
1310       ME_MarkForPainting(editor, para2, editor->pLastSelEndPara->member.para.next_para);
1311     } else if (nEnd > editor->nLastSelEnd) {
1312       ME_MarkForPainting(editor, editor->pLastSelEndPara, para2->member.para.next_para);
1313     }
1314   }
1315
1316   ME_InvalidateMarkedParagraphs(editor);
1317   /* remember the last invalidated position */
1318   ME_GetSelectionOfs(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
1319   ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
1320   assert(editor->pLastSelStartPara->type == diParagraph);
1321   assert(editor->pLastSelEndPara->type == diParagraph);
1322 }
1323
1324 BOOL
1325 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
1326 {
1327   /* TODO: Zoom images and objects */
1328
1329   if (numerator == 0 && denominator == 0)
1330   {
1331     editor->nZoomNumerator = editor->nZoomDenominator = 0;
1332     return TRUE;
1333   }
1334   if (numerator <= 0 || denominator <= 0)
1335     return FALSE;
1336   if (numerator * 64 <= denominator || numerator / denominator >= 64)
1337     return FALSE;
1338
1339   editor->nZoomNumerator = numerator;
1340   editor->nZoomDenominator = denominator;
1341
1342   ME_RewrapRepaint(editor);
1343   return TRUE;
1344 }