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