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