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