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