richedit: Prevent division by zero when 0-width border is used.
[wine] / dlls / riched20 / paint.c
1 /*
2  * RichEdit - painting functions
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  * Copyright 2005 by Phil Krylov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "editor.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25
26 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, const RECT *rcUpdate) {
27   ME_DisplayItem *item;
28   ME_Context c;
29   int yoffset;
30
31   editor->nSequence++;
32   yoffset = ME_GetYScrollPos(editor);
33   ME_InitContext(&c, editor, hDC);
34   SetBkMode(hDC, TRANSPARENT);
35   ME_MoveCaret(editor);
36   item = editor->pBuffer->pFirst->next;
37   c.pt.y -= yoffset;
38   while(item != editor->pBuffer->pLast) {
39     int ye;
40     assert(item->type == diParagraph);
41     ye = c.pt.y + item->member.para.nHeight;
42     if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
43     {
44       BOOL bPaint = (rcUpdate == NULL);
45       if (rcUpdate)
46         bPaint = c.pt.y<rcUpdate->bottom && 
47           c.pt.y+item->member.para.nHeight>rcUpdate->top;
48       if (bPaint)
49       {
50         ME_DrawParagraph(&c, item);
51         if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
52           item->member.para.nFlags &= ~MEPF_REPAINT;
53       }
54     }
55     c.pt.y = ye;
56     item = item->member.para.next_para;
57   }
58   if (c.pt.y<c.rcView.bottom) {
59     RECT rc;
60     int xs = c.rcView.left, xe = c.rcView.right;
61     int ys = c.pt.y, ye = c.rcView.bottom;
62     
63     if (bOnlyNew)
64     {
65       int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
66       if (y1<y2)
67         ys = y1, ye = y2+1;
68       else
69         ys = ye;
70     }
71     
72     if (rcUpdate && ys!=ye)
73     {
74       xs = rcUpdate->left, xe = rcUpdate->right;
75       if (rcUpdate->top > ys)
76         ys = rcUpdate->top;
77       if (rcUpdate->bottom < ye)
78         ye = rcUpdate->bottom;
79     }
80
81     if (ye>ys) {
82       rc.left = xs;
83       rc.top = ys;
84       rc.right = xe;
85       rc.bottom = ye;
86       FillRect(hDC, &rc, c.editor->hbrBackground);
87     }
88     if (ys == c.pt.y) /* don't overwrite the top bar */
89       ys++;
90   }
91   if (editor->nTotalLength != editor->nLastTotalLength)
92     ME_SendRequestResize(editor, FALSE);
93   editor->nLastTotalLength = editor->nTotalLength;
94   ME_DestroyContext(&c);
95 }
96
97 void ME_Repaint(ME_TextEditor *editor)
98 {
99   if (ME_WrapMarkedParagraphs(editor))
100   {
101     ME_UpdateScrollBar(editor);
102     FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
103   }
104   ME_SendOldNotify(editor, EN_UPDATE);
105   UpdateWindow(editor->hWnd);
106 }
107
108 void ME_UpdateRepaint(ME_TextEditor *editor)
109 {
110   /* Should be called whenever the contents of the control have changed */
111   ME_Cursor *pCursor;
112
113   if (!editor->bRedraw) return;
114   if (ME_WrapMarkedParagraphs(editor))
115     ME_UpdateScrollBar(editor);
116   
117   /* Ensure that the cursor is visible */
118   pCursor = &editor->pCursors[0];
119   ME_EnsureVisible(editor, pCursor->pRun);
120   
121   /* send EN_CHANGE if the event mask asks for it */
122   if(editor->nEventMask & ENM_CHANGE)
123   {
124     editor->nEventMask &= ~ENM_CHANGE;
125     ME_SendOldNotify(editor, EN_CHANGE);
126     editor->nEventMask |= ENM_CHANGE;
127   }
128   ME_Repaint(editor);
129   ME_SendSelChange(editor);
130 }
131
132 void
133 ME_RewrapRepaint(ME_TextEditor *editor)
134
135   /* RewrapRepaint should be called whenever the control has changed in
136    * looks, but not content. Like resizing. */
137   
138   ME_MarkAllForWrapping(editor);
139   if (editor->bRedraw)
140   {
141     ME_WrapMarkedParagraphs(editor);
142     ME_UpdateScrollBar(editor);
143     ME_Repaint(editor);
144   }
145 }
146
147 int ME_twips2pointsX(ME_Context *c, int x)
148 {
149   if (c->editor->nZoomNumerator == 0)
150     return x * c->dpi.cx / 1440;
151   else
152     return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
153 }
154
155 int ME_twips2pointsY(ME_Context *c, int y)
156 {
157   if (c->editor->nZoomNumerator == 0)
158     return y * c->dpi.cy / 1440;
159   else
160     return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
161 }
162
163 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars, 
164   ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
165   HDC hDC = c->hDC;
166   HGDIOBJ hOldFont;
167   COLORREF rgbOld;
168   int yOffset = 0, yTwipsOffset = 0;
169   SIZE          sz;
170   COLORREF      rgb;
171
172   hOldFont = ME_SelectStyleFont(c, s);
173   if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
174     rgb = RGB(0,0,255);
175   else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
176     rgb = GetSysColor(COLOR_WINDOWTEXT);
177   else
178     rgb = s->fmt.crTextColor;
179   rgbOld = SetTextColor(hDC, rgb);
180   if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
181     yTwipsOffset = s->fmt.yOffset;
182   }
183   if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
184     if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
185     if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
186   }
187   if (yTwipsOffset)
188     yOffset = ME_twips2pointsY(c, yTwipsOffset);
189   ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
190   GetTextExtentPoint32W(hDC, szText, nChars, &sz);
191   if (width) *width = sz.cx;
192   if (s->fmt.dwMask & CFM_UNDERLINETYPE)
193   {
194     HPEN    hPen;
195     switch (s->fmt.bUnderlineType)
196     {
197     case CFU_UNDERLINE:
198     case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
199     case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
200       hPen = CreatePen(PS_SOLID, 1, rgb);
201       break;
202     case CFU_UNDERLINEDOTTED:
203       hPen = CreatePen(PS_DOT, 1, rgb);
204       break;
205     default:
206       WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
207       /* fall through */
208     case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
209     case CFU_UNDERLINENONE:
210       hPen = NULL;
211       break;
212     }
213     if (hPen != NULL)
214     {
215       HPEN hOldPen = SelectObject(hDC, hPen);
216       /* FIXME: should use textmetrics info for Descent info */
217       MoveToEx(hDC, x, y - yOffset + 1, NULL);
218       LineTo(hDC, x + sz.cx, y - yOffset + 1);
219       SelectObject(hDC, hOldPen);
220       DeleteObject(hPen);
221     }
222   }
223   if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
224   {
225     if (nSelFrom < 0) nSelFrom = 0;
226     if (nSelTo > nChars) nSelTo = nChars;
227     GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
228     x += sz.cx;
229     GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
230     
231     /* Invert selection if not hidden by EM_HIDESELECTION */
232     if (c->editor->bHideSelection == FALSE)
233         PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
234   }
235   SetTextColor(hDC, rgbOld);
236   ME_UnselectStyleFont(c, s, hOldFont);
237 }
238
239 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
240   int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
241   HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
242   COLORREF color = SetTextColor(hDC, RGB(128,128,128));
243   TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
244   SelectObject(hDC, hFont);
245   SetTextAlign(hDC, align);
246   SetTextColor(hDC, color);
247 }
248
249 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) 
250 {
251   ME_Run *run = &rundi->member.run;
252   ME_DisplayItem *start;
253   int runofs = run->nCharOfs+para->nCharOfs;
254   int nSelFrom, nSelTo;
255   const WCHAR wszSpace[] = {' ', 0};
256   
257   if (run->nFlags & MERF_HIDDEN)
258     return;
259
260   start = ME_FindItemBack(rundi, diStartRow);
261   ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
262
263   /* Draw selected end-of-paragraph mark */
264   if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
265     ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
266                          c->pt.y + start->member.row.nYPos,
267                          start->member.row.nHeight);
268           
269   /* you can always comment it out if you need visible paragraph marks */
270   if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL)) 
271     return;
272
273   if (run->nFlags & MERF_GRAPHICS)
274     ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
275   else
276   {
277     if (c->editor->cPasswordMask)
278     {
279       ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
280       ME_DrawTextWithStyle(c, x, y, 
281         szMasked->szData, ME_StrVLen(szMasked), run->style, NULL, 
282         nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
283       ME_DestroyString(szMasked);
284     }
285     else
286       ME_DrawTextWithStyle(c, x, y, 
287         run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, 
288         nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
289     }
290 }
291
292 static struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
293   /* none */            {0, 1, PS_SOLID, FALSE},
294   /* 3/4 */             {3, 4, PS_SOLID, FALSE},
295   /* 1 1/2 */           {3, 2, PS_SOLID, FALSE},
296   /* 2 1/4 */           {9, 4, PS_SOLID, FALSE},
297   /* 3 */               {3, 1, PS_SOLID, FALSE},
298   /* 4 1/2 */           {9, 2, PS_SOLID, FALSE},
299   /* 6 */               {6, 1, PS_SOLID, FALSE},
300   /* 3/4 double */      {3, 4, PS_SOLID, TRUE},
301   /* 1 1/2 double */    {3, 2, PS_SOLID, TRUE},
302   /* 2 1/4 double */    {9, 4, PS_SOLID, TRUE},
303   /* 3/4 gray */        {3, 4, PS_DOT /* FIXME */, FALSE},
304   /* 1 1/2 dashed */    {3, 2, PS_DASH, FALSE},
305 };
306
307 static COLORREF         pen_colors[16] = {
308   /* Black */           RGB(0x00, 0x00, 0x00),  /* Blue */            RGB(0x00, 0x00, 0xFF),
309   /* Cyan */            RGB(0x00, 0xFF, 0xFF),  /* Green */           RGB(0x00, 0xFF, 0x00),
310   /* Magenta */         RGB(0xFF, 0x00, 0xFF),  /* Red */             RGB(0xFF, 0x00, 0x00),
311   /* Yellow */          RGB(0xFF, 0xFF, 0x00),  /* White */           RGB(0xFF, 0xFF, 0xFF),
312   /* Dark blue */       RGB(0x00, 0x00, 0x80),  /* Dark cyan */       RGB(0x00, 0x80, 0x80),
313   /* Dark green */      RGB(0x00, 0x80, 0x80),  /* Dark magenta */    RGB(0x80, 0x00, 0x80),
314   /* Dark red */        RGB(0x80, 0x00, 0x00),  /* Dark yellow */     RGB(0x80, 0x80, 0x00),
315   /* Dark gray */       RGB(0x80, 0x80, 0x80),  /* Light gray */      RGB(0xc0, 0xc0, 0xc0),
316 };
317
318 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
319 {
320   int width;
321
322   if (editor->nZoomNumerator == 0)
323   {
324       width = border_details[idx].width_num + border_details[idx].width_den / 2;
325       width /= border_details[idx].width_den;
326   }
327   else
328   {
329       width = border_details[idx].width_num * editor->nZoomNumerator;
330       width += border_details[idx].width_den * editor->nZoomNumerator / 2;
331       width /= border_details[idx].width_den * editor->nZoomDenominator;
332   }
333   return width;
334 }
335
336 int  ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
337 {
338   int idx = (flags >> 8) & 0xF;
339   int width;
340
341   if (idx >= sizeof(border_details) / sizeof(border_details[0]))
342   {
343       FIXME("Unsupported border value %d\n", idx);
344       return 0;
345   }
346   width = ME_GetBorderPenWidth(editor, idx);
347   if (border_details[idx].dble) width = width * 2 + 1;
348   return width;
349 }
350
351 int  ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
352 {
353   int   sp = 0, ls = 0;
354   if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
355
356   /* FIXME: how to compute simply the line space in ls ??? */
357   /* FIXME: does line spacing include the line itself ??? */
358   switch (para->pFmt->bLineSpacingRule)
359   {
360   case 0:       sp = ls; break;
361   case 1:       sp = (3 * ls) / 2; break;
362   case 2:       sp = 2 * ls; break;
363   case 3:       sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
364   case 4:       sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
365   case 5:       sp = para->pFmt->dyLineSpacing / 20; break;
366   default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
367   }
368   if (c->editor->nZoomNumerator == 0)
369     return sp;
370   else
371     return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
372 }
373
374 static int ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y)
375 {
376   int           idx, border_width;
377   int           ybefore, yafter;
378   RECT          rc;
379
380   if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return 0;
381
382   if (para->pFmt->dwMask & PFM_SPACEBEFORE)
383   {
384     rc.left = c->rcView.left;
385     rc.right = c->rcView.right;
386     rc.top = y;
387     ybefore = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
388     rc.bottom = y + ybefore;
389     FillRect(c->hDC, &rc, c->editor->hbrBackground);
390   }
391   else ybefore = 0;
392   if (para->pFmt->dwMask & PFM_SPACEAFTER)
393   {
394     rc.left = c->rcView.left;
395     rc.right = c->rcView.right;
396     rc.bottom = y + para->nHeight;
397     yafter = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
398     rc.top = rc.bottom - yafter;
399     FillRect(c->hDC, &rc, c->editor->hbrBackground);
400   }
401   else yafter = 0;
402
403   border_width = 0;
404   idx = (para->pFmt->wBorders >> 8) & 0xF;
405   if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
406     int         pen_width;
407     COLORREF    pencr;
408     HPEN        pen = NULL, oldpen = NULL;
409     POINT       pt;
410
411     if (para->pFmt->wBorders & 0x00B0)
412       FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
413     border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
414
415     if (para->pFmt->wBorders & 64) /* autocolor */
416       pencr = GetSysColor(COLOR_WINDOWTEXT);
417     else
418       pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
419
420     pen_width = ME_GetBorderPenWidth(c->editor, idx);
421     pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
422     oldpen = SelectObject(c->hDC, pen);
423     MoveToEx(c->hDC, 0, 0, &pt);
424
425     /* before & after spaces are not included in border */
426     if (para->pFmt->wBorders & 1)
427     {
428       MoveToEx(c->hDC, c->rcView.left, y + ybefore, NULL);
429       LineTo(c->hDC, c->rcView.left, y + para->nHeight - yafter);
430       if (border_details[idx].dble) {
431         MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + ybefore + pen_width + 1, NULL);
432         LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - yafter - pen_width - 1);
433       }
434     }
435     if (para->pFmt->wBorders & 2)
436     {
437       MoveToEx(c->hDC, c->rcView.right, y + ybefore, NULL);
438       LineTo(c->hDC, c->rcView.right, y + para->nHeight - yafter);
439       if (border_details[idx].dble) {
440         MoveToEx(c->hDC, c->rcView.right - pen_width - 1, y + ybefore + pen_width + 1, NULL);
441         LineTo(c->hDC, c->rcView.right - pen_width - 1, y + para->nHeight - yafter - pen_width - 1);
442       }
443     }
444     if (para->pFmt->wBorders & 4)
445     {
446       MoveToEx(c->hDC, c->rcView.left, y + ybefore, NULL);
447       LineTo(c->hDC, c->rcView.right, y + ybefore);
448       if (border_details[idx].dble) {
449         MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + ybefore + pen_width + 1, NULL);
450         LineTo(c->hDC, c->rcView.right - pen_width - 1, y + ybefore + pen_width + 1);
451       }
452     }
453     if (para->pFmt->wBorders & 8)
454     {
455       MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - yafter - 1, NULL);
456       LineTo(c->hDC, c->rcView.right, y + para->nHeight - yafter - 1);
457       if (border_details[idx].dble) {
458         MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - yafter - 1 - pen_width - 1, NULL);
459         LineTo(c->hDC, c->rcView.right - pen_width - 1, y + para->nHeight - yafter - 1 - pen_width - 1);
460       }
461     }
462
463     MoveToEx(c->hDC, pt.x, pt.y, NULL);
464     SelectObject(c->hDC, oldpen);
465     DeleteObject(pen);
466   }
467   return ybefore +
468       ((para->pFmt->dwMask & PFM_BORDER) && (para->pFmt->wBorders & 4) ?
469        border_width : 0);
470 }
471
472 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
473   int align = SetTextAlign(c->hDC, TA_BASELINE);
474   ME_DisplayItem *p;
475   ME_Run *run;
476   ME_Paragraph *para = NULL;
477   RECT rc, rcPara;
478   int y = c->pt.y;
479   int height = 0, baseline = 0, no=0, pno = 0;
480   int xs = 0, xe = 0;
481   BOOL visible = FALSE;
482   int nMargWidth = 0;
483
484   c->pt.x = c->rcView.left;
485   rcPara.left = c->rcView.left;
486   rcPara.right = c->rcView.right;
487   for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
488     switch(p->type) {
489       case diParagraph:
490         para = &p->member.para;
491         assert(para);
492         nMargWidth = ME_twips2pointsX(c, para->pFmt->dxStartIndent);
493         if (pno != 0)
494           nMargWidth += ME_twips2pointsX(c, para->pFmt->dxOffset);
495         xs = c->rcView.left+nMargWidth;
496         xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
497         y += ME_DrawParaDecoration(c, para, y);
498         break;
499       case diStartRow:
500         y += height;
501         rcPara.top = y;
502         rcPara.bottom = y+p->member.row.nHeight;
503         visible = RectVisible(c->hDC, &rcPara);
504         if (visible) {
505           /* left margin */
506           rc.left = c->rcView.left;
507           rc.right = c->rcView.left+nMargWidth;
508           rc.top = y;
509           rc.bottom = y+p->member.row.nHeight;
510           FillRect(c->hDC, &rc, c->editor->hbrBackground);
511           /* right margin */
512           rc.left = xe;
513           rc.right = c->rcView.right;
514           FillRect(c->hDC, &rc, c->editor->hbrBackground);
515           rc.left = c->rcView.left+nMargWidth;
516           rc.right = xe;
517           FillRect(c->hDC, &rc, c->editor->hbrBackground);
518         }
519         if (me_debug)
520         {
521           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
522           WCHAR buf[128];
523           POINT pt = c->pt;
524           wsprintfW(buf, wszRowDebug, no);
525           pt.y = 12+y;
526           ME_DebugWrite(c->hDC, &pt, buf);
527         }
528         
529         height = p->member.row.nHeight;
530         baseline = p->member.row.nBaseline;
531         pno++;
532         break;
533       case diRun:
534         assert(para);
535         run = &p->member.run;
536         if (visible && me_debug) {
537           rc.left = c->rcView.left+run->pt.x;
538           rc.right = c->rcView.left+run->pt.x+run->nWidth;
539           rc.top = c->pt.y+run->pt.y;
540           rc.bottom = c->pt.y+run->pt.y+height;
541           TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
542           if (run->nFlags & MERF_SKIPPED)
543             DrawFocusRect(c->hDC, &rc);
544           else
545             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
546         }
547         if (visible)
548           ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
549         if (me_debug)
550         {
551           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
552           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
553           WCHAR buf[2560];
554           POINT pt;
555           pt.x = run->pt.x;
556           pt.y = c->pt.y + run->pt.y;
557           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
558           ME_DebugWrite(c->hDC, &pt, buf);
559         }
560         /* c->pt.x += p->member.run.nWidth; */
561         break;
562       default:
563         break;
564     }
565     no++;
566   }
567   SetTextAlign(c->hDC, align);
568 }
569
570 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
571 {
572   ME_Scroll(editor, absY, 1);
573 }
574
575 void ME_ScrollUp(ME_TextEditor *editor, int cy)
576 {
577   ME_Scroll(editor, cy, 2);
578 }
579
580 void ME_ScrollDown(ME_TextEditor *editor, int cy)
581
582   ME_Scroll(editor, cy, 3);
583 }
584
585 void ME_Scroll(ME_TextEditor *editor, int value, int type)
586 {
587   SCROLLINFO si;
588   int nOrigPos, nNewPos, nActualScroll;
589
590   nOrigPos = ME_GetYScrollPos(editor);
591   
592   si.cbSize = sizeof(SCROLLINFO);
593   si.fMask = SIF_POS;
594   
595   switch (type)
596   {
597     case 1:
598       /*Scroll absolutly*/
599       si.nPos = value;
600       break;
601     case 2:
602       /* Scroll up - towards the beginning of the document */
603       si.nPos = nOrigPos - value;
604       break;
605     case 3:
606       /* Scroll down - towards the end of the document */
607       si.nPos = nOrigPos + value;
608       break;
609     default:
610       FIXME("ME_Scroll called incorrectly\n");
611       si.nPos = 0;
612   }
613   
614   nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
615   nActualScroll = nOrigPos - nNewPos;
616   if (editor->bRedraw)
617   {
618     if (abs(nActualScroll) > editor->sizeWindow.cy)
619       InvalidateRect(editor->hWnd, NULL, TRUE);
620     else
621       ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
622     ME_Repaint(editor);
623   }
624   
625   ME_UpdateScrollBar(editor);
626 }
627
628  
629  void ME_UpdateScrollBar(ME_TextEditor *editor)
630
631   /* Note that this is the only funciton that should ever call SetScrolLInfo 
632    * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
633    * be used at all. */
634   
635   HWND hWnd;
636   SCROLLINFO si;
637   BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
638   
639   if (ME_WrapMarkedParagraphs(editor))
640     FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
641   
642   hWnd = editor->hWnd;
643   si.cbSize = sizeof(si);
644   bScrollBarWasVisible = ME_GetYScrollVisible(editor);
645   bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
646   
647   if (bScrollBarWasVisible != bScrollBarWillBeVisible)
648   {
649     ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
650     ME_MarkAllForWrapping(editor);
651     ME_WrapMarkedParagraphs(editor);
652   }
653   
654   si.fMask = SIF_PAGE | SIF_RANGE;
655   if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
656     si.fMask |= SIF_DISABLENOSCROLL;
657   
658   si.nMin = 0;  
659   si.nMax = editor->nTotalLength;
660   
661   si.nPage = editor->sizeWindow.cy;
662      
663   TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
664   SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
665 }
666
667 int ME_GetYScrollPos(ME_TextEditor *editor)
668 {
669   SCROLLINFO si;
670   si.cbSize = sizeof(si);
671   si.fMask = SIF_POS;
672   return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
673 }
674
675 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
676 { /* Returns true if the scrollbar is visible */
677   SCROLLBARINFO sbi;
678   sbi.cbSize = sizeof(sbi);
679   GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
680   return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
681 }
682
683 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
684 {
685   ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
686   ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
687   int y, yrel, yheight, yold;
688   
689   assert(pRow);
690   assert(pPara);
691   
692   y = pPara->member.para.nYPos+pRow->member.row.nYPos;
693   yheight = pRow->member.row.nHeight;
694   yold = ME_GetYScrollPos(editor);
695   yrel = y - yold;
696   
697   if (y < yold)
698     ME_ScrollAbs(editor,y);
699   else if (yrel + yheight > editor->sizeWindow.cy) 
700     ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
701 }
702
703
704 void
705 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
706 {
707   RECT rc;
708   int x, y, height;
709   ME_Cursor tmp;
710
711   ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
712   ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
713
714   rc.left = 0;
715   rc.top = y;
716   rc.bottom = y + height;
717   rc.right = editor->rcFormat.right;
718   InvalidateRect(editor->hWnd, &rc, FALSE);
719 }
720
721
722 void
723 ME_InvalidateSelection(ME_TextEditor *editor)
724 {
725   ME_DisplayItem *para1, *para2;
726   int nStart, nEnd;
727   int len = ME_GetTextLength(editor);
728
729   ME_GetSelection(editor, &nStart, &nEnd);
730   /* if both old and new selection are 0-char (= caret only), then
731   there's no (inverted) area to be repainted, neither old nor new */
732   if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
733     return;
734   ME_WrapMarkedParagraphs(editor);
735   ME_GetSelectionParas(editor, &para1, &para2);
736   assert(para1->type == diParagraph);
737   assert(para2->type == diParagraph);
738   /* last selection markers aren't always updated, which means
739   they can point past the end of the document */ 
740   if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
741     ME_MarkForPainting(editor,
742         ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
743         ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
744   } else {
745     /* if the start part of selection is being expanded or contracted... */
746     if (nStart < editor->nLastSelStart) {
747       ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
748     } else
749     if (nStart > editor->nLastSelStart) {
750       ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
751     }
752
753     /* if the end part of selection is being contracted or expanded... */
754     if (nEnd < editor->nLastSelEnd) {
755       ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
756     } else
757     if (nEnd > editor->nLastSelEnd) {
758       ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
759     }
760   }
761
762   ME_InvalidateMarkedParagraphs(editor);
763   /* remember the last invalidated position */
764   ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
765   ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
766   assert(editor->pLastSelStartPara->type == diParagraph);
767   assert(editor->pLastSelEndPara->type == diParagraph);
768 }
769
770 void
771 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
772 {
773   editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
774 }
775
776
777 BOOL
778 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
779 {
780   /* TODO: Zoom images and objects */
781
782   if (numerator != 0)
783   {
784     if (denominator == 0)
785       return FALSE;
786     if (1.0 / 64.0 > (float)numerator / (float)denominator
787         || (float)numerator / (float)denominator > 64.0)
788       return FALSE;
789   }
790   
791   editor->nZoomNumerator = numerator;
792   editor->nZoomDenominator = denominator;
793   
794   ME_RewrapRepaint(editor);
795   return TRUE;
796 }