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