kernel32: LOADPARAMS16->showCmd can be NULL, do not crash in that case.
[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_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
250                             ME_Paragraph *para, BOOL selected) {
251   SIZE sz;
252   int xs, ys, xe, ye, h, ym, width, eyes;
253   ME_GetGraphicsSize(c->editor, run, &sz);
254   xs = run->pt.x;
255   ys = y-sz.cy;
256   xe = xs+sz.cx;
257   ye = y;
258   h = ye-ys;
259   ym = ys+h/4;
260   width = sz.cx;
261   eyes = width/8;
262   /* draw a smiling face :) */
263   Ellipse(c->hDC, xs, ys, xe, ye);
264   Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
265   Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
266   MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
267   LineTo(c->hDC, xs+width/8, ys+3*h/4);
268   LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
269   LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
270   if (selected)
271   {
272     /* descent is usually (always?) 0 for graphics */
273     PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);    
274   }
275 }
276
277 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) 
278 {
279   ME_Run *run = &rundi->member.run;
280   ME_DisplayItem *start;
281   int runofs = run->nCharOfs+para->nCharOfs;
282   int nSelFrom, nSelTo;
283   const WCHAR wszSpace[] = {' ', 0};
284   
285   if (run->nFlags & MERF_HIDDEN)
286     return;
287
288   start = ME_FindItemBack(rundi, diStartRow);
289   ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
290
291   /* Draw selected end-of-paragraph mark */
292   if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
293     ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
294                          c->pt.y + start->member.row.nYPos,
295                          start->member.row.nHeight);
296           
297   /* you can always comment it out if you need visible paragraph marks */
298   if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL)) 
299     return;
300
301   if (run->nFlags & MERF_GRAPHICS)
302     ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
303   else
304   {
305     if (c->editor->cPasswordMask)
306     {
307       ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
308       ME_DrawTextWithStyle(c, x, y, 
309         szMasked->szData, ME_StrVLen(szMasked), run->style, NULL, 
310         nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
311       ME_DestroyString(szMasked);
312     }
313     else
314       ME_DrawTextWithStyle(c, x, y, 
315         run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, 
316         nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
317     }
318 }
319
320 static struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
321   /* none */            {0, 0, PS_SOLID, FALSE},
322   /* 3/4 */             {3, 4, PS_SOLID, FALSE},
323   /* 1 1/2 */           {3, 2, PS_SOLID, FALSE},
324   /* 2 1/4 */           {9, 4, PS_SOLID, FALSE},
325   /* 3 */               {3, 1, PS_SOLID, FALSE},
326   /* 4 1/2 */           {9, 2, PS_SOLID, FALSE},
327   /* 6 */               {6, 1, PS_SOLID, FALSE},
328   /* 3/4 double */      {3, 4, PS_SOLID, TRUE},
329   /* 1 1/2 double */    {3, 2, PS_SOLID, TRUE},
330   /* 2 1/4 double */    {9, 4, PS_SOLID, TRUE},
331   /* 3/4 gray */        {3, 4, PS_DOT /* FIXME */, FALSE},
332   /* 1 1/2 dashed */    {3, 2, PS_DASH, FALSE},
333 };
334
335 static COLORREF         pen_colors[16] = {
336   /* Black */           RGB(0x00, 0x00, 0x00),  /* Blue */            RGB(0x00, 0x00, 0xFF),
337   /* Cyan */            RGB(0x00, 0xFF, 0xFF),  /* Green */           RGB(0x00, 0xFF, 0x00),
338   /* Magenta */         RGB(0xFF, 0x00, 0xFF),  /* Red */             RGB(0xFF, 0x00, 0x00),
339   /* Yellow */          RGB(0xFF, 0xFF, 0x00),  /* White */           RGB(0xFF, 0xFF, 0xFF),
340   /* Dark blue */       RGB(0x00, 0x00, 0x80),  /* Dark cyan */       RGB(0x00, 0x80, 0x80),
341   /* Dark green */      RGB(0x00, 0x80, 0x80),  /* Dark magenta */    RGB(0x80, 0x00, 0x80),
342   /* Dark red */        RGB(0x80, 0x00, 0x00),  /* Dark yellow */     RGB(0x80, 0x80, 0x00),
343   /* Dark gray */       RGB(0x80, 0x80, 0x80),  /* Light gray */      RGB(0xc0, 0xc0, 0xc0),
344 };
345
346 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
347 {
348   int width;
349
350   if (editor->nZoomNumerator == 0)
351   {
352       width = border_details[idx].width_num + border_details[idx].width_den / 2;
353       width /= border_details[idx].width_den;
354   }
355   else
356   {
357       width = border_details[idx].width_num * editor->nZoomNumerator;
358       width += border_details[idx].width_den * editor->nZoomNumerator / 2;
359       width /= border_details[idx].width_den * editor->nZoomDenominator;
360   }
361   return width;
362 }
363
364 int  ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
365 {
366   int idx = (flags >> 8) & 0xF;
367   int width;
368
369   if (idx >= sizeof(border_details) / sizeof(border_details[0]))
370   {
371       FIXME("Unsupported border value %d\n", idx);
372       return 0;
373   }
374   width = ME_GetBorderPenWidth(editor, idx);
375   if (border_details[idx].dble) width = width * 2 + 1;
376   return width;
377 }
378
379 int  ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
380 {
381   int   sp = 0, ls = 0;
382   if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
383
384   /* FIXME: how to compute simply the line space in ls ??? */
385   /* FIXME: does line spacing include the line itself ??? */
386   switch (para->pFmt->bLineSpacingRule)
387   {
388   case 0:       sp = ls; break;
389   case 1:       sp = (3 * ls) / 2; break;
390   case 2:       sp = 2 * ls; break;
391   case 3:       sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
392   case 4:       sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
393   case 5:       sp = para->pFmt->dyLineSpacing / 20; break;
394   default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
395   }
396   if (c->editor->nZoomNumerator == 0)
397     return sp;
398   else
399     return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
400 }
401
402 static int ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y)
403 {
404   int           idx, border_width;
405   int           ybefore, yafter;
406   RECT          rc;
407
408   if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return 0;
409
410   if (para->pFmt->dwMask & PFM_SPACEBEFORE)
411   {
412     rc.left = c->rcView.left;
413     rc.right = c->rcView.right;
414     rc.top = y;
415     ybefore = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
416     rc.bottom = y + ybefore;
417     FillRect(c->hDC, &rc, c->editor->hbrBackground);
418   }
419   else ybefore = 0;
420   if (para->pFmt->dwMask & PFM_SPACEAFTER)
421   {
422     rc.left = c->rcView.left;
423     rc.right = c->rcView.right;
424     rc.bottom = y + para->nHeight;
425     yafter = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
426     rc.top = rc.bottom - yafter;
427     FillRect(c->hDC, &rc, c->editor->hbrBackground);
428   }
429   else yafter = 0;
430
431   border_width = 0;
432   idx = (para->pFmt->wBorders >> 8) & 0xF;
433   if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
434     int         pen_width;
435     COLORREF    pencr;
436     HPEN        pen = NULL, oldpen = NULL;
437     POINT       pt;
438
439     if (para->pFmt->wBorders & 0x00B0)
440       FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
441     border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
442
443     if (para->pFmt->wBorders & 64) /* autocolor */
444       pencr = GetSysColor(COLOR_WINDOWTEXT);
445     else
446       pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
447
448     pen_width = ME_GetBorderPenWidth(c->editor, idx);
449     pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
450     oldpen = SelectObject(c->hDC, pen);
451     MoveToEx(c->hDC, 0, 0, &pt);
452
453     /* before & after spaces are not included in border */
454     if (para->pFmt->wBorders & 1)
455     {
456       MoveToEx(c->hDC, c->rcView.left, y + ybefore, NULL);
457       LineTo(c->hDC, c->rcView.left, y + para->nHeight - yafter);
458       if (border_details[idx].dble) {
459         MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + ybefore + pen_width + 1, NULL);
460         LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - yafter - pen_width - 1);
461       }
462     }
463     if (para->pFmt->wBorders & 2)
464     {
465       MoveToEx(c->hDC, c->rcView.right, y + ybefore, NULL);
466       LineTo(c->hDC, c->rcView.right, y + para->nHeight - yafter);
467       if (border_details[idx].dble) {
468         MoveToEx(c->hDC, c->rcView.right - pen_width - 1, y + ybefore + pen_width + 1, NULL);
469         LineTo(c->hDC, c->rcView.right - pen_width - 1, y + para->nHeight - yafter - pen_width - 1);
470       }
471     }
472     if (para->pFmt->wBorders & 4)
473     {
474       MoveToEx(c->hDC, c->rcView.left, y + ybefore, NULL);
475       LineTo(c->hDC, c->rcView.right, y + ybefore);
476       if (border_details[idx].dble) {
477         MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + ybefore + pen_width + 1, NULL);
478         LineTo(c->hDC, c->rcView.right - pen_width - 1, y + ybefore + pen_width + 1);
479       }
480     }
481     if (para->pFmt->wBorders & 8)
482     {
483       MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - yafter - 1, NULL);
484       LineTo(c->hDC, c->rcView.right, y + para->nHeight - yafter - 1);
485       if (border_details[idx].dble) {
486         MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - yafter - 1 - pen_width - 1, NULL);
487         LineTo(c->hDC, c->rcView.right - pen_width - 1, y + para->nHeight - yafter - 1 - pen_width - 1);
488       }
489     }
490
491     MoveToEx(c->hDC, pt.x, pt.y, NULL);
492     SelectObject(c->hDC, oldpen);
493     DeleteObject(pen);
494   }
495   return ybefore +
496       ((para->pFmt->dwMask & PFM_BORDER) && (para->pFmt->wBorders & 4) ?
497        border_width : 0);
498 }
499
500 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
501   int align = SetTextAlign(c->hDC, TA_BASELINE);
502   ME_DisplayItem *p;
503   ME_Run *run;
504   ME_Paragraph *para = NULL;
505   RECT rc, rcPara;
506   int y = c->pt.y;
507   int height = 0, baseline = 0, no=0, pno = 0;
508   int xs = 0, xe = 0;
509   BOOL visible = FALSE;
510   int nMargWidth = 0;
511
512   c->pt.x = c->rcView.left;
513   rcPara.left = c->rcView.left;
514   rcPara.right = c->rcView.right;
515   for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
516     switch(p->type) {
517       case diParagraph:
518         para = &p->member.para;
519         assert(para);
520         nMargWidth = ME_twips2pointsX(c, para->pFmt->dxStartIndent);
521         if (pno != 0)
522           nMargWidth += ME_twips2pointsX(c, para->pFmt->dxOffset);
523         xs = c->rcView.left+nMargWidth;
524         xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
525         y += ME_DrawParaDecoration(c, para, y);
526         break;
527       case diStartRow:
528         y += height;
529         rcPara.top = y;
530         rcPara.bottom = y+p->member.row.nHeight;
531         visible = RectVisible(c->hDC, &rcPara);
532         if (visible) {
533           /* left margin */
534           rc.left = c->rcView.left;
535           rc.right = c->rcView.left+nMargWidth;
536           rc.top = y;
537           rc.bottom = y+p->member.row.nHeight;
538           FillRect(c->hDC, &rc, c->editor->hbrBackground);
539           /* right margin */
540           rc.left = xe;
541           rc.right = c->rcView.right;
542           FillRect(c->hDC, &rc, c->editor->hbrBackground);
543           rc.left = c->rcView.left+nMargWidth;
544           rc.right = xe;
545           FillRect(c->hDC, &rc, c->editor->hbrBackground);
546         }
547         if (me_debug)
548         {
549           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
550           WCHAR buf[128];
551           POINT pt = c->pt;
552           wsprintfW(buf, wszRowDebug, no);
553           pt.y = 12+y;
554           ME_DebugWrite(c->hDC, &pt, buf);
555         }
556         
557         height = p->member.row.nHeight;
558         baseline = p->member.row.nBaseline;
559         pno++;
560         break;
561       case diRun:
562         assert(para);
563         run = &p->member.run;
564         if (visible && me_debug) {
565           rc.left = c->rcView.left+run->pt.x;
566           rc.right = c->rcView.left+run->pt.x+run->nWidth;
567           rc.top = c->pt.y+run->pt.y;
568           rc.bottom = c->pt.y+run->pt.y+height;
569           TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
570           if (run->nFlags & MERF_SKIPPED)
571             DrawFocusRect(c->hDC, &rc);
572           else
573             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
574         }
575         if (visible)
576           ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
577         if (me_debug)
578         {
579           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
580           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
581           WCHAR buf[2560];
582           POINT pt;
583           pt.x = run->pt.x;
584           pt.y = c->pt.y + run->pt.y;
585           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
586           ME_DebugWrite(c->hDC, &pt, buf);
587         }
588         /* c->pt.x += p->member.run.nWidth; */
589         break;
590       default:
591         break;
592     }
593     no++;
594   }
595   SetTextAlign(c->hDC, align);
596 }
597
598 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
599 {
600   ME_Scroll(editor, absY, 1);
601 }
602
603 void ME_ScrollUp(ME_TextEditor *editor, int cy)
604 {
605   ME_Scroll(editor, cy, 2);
606 }
607
608 void ME_ScrollDown(ME_TextEditor *editor, int cy)
609
610   ME_Scroll(editor, cy, 3);
611 }
612
613 void ME_Scroll(ME_TextEditor *editor, int value, int type)
614 {
615   SCROLLINFO si;
616   int nOrigPos, nNewPos, nActualScroll;
617
618   nOrigPos = ME_GetYScrollPos(editor);
619   
620   si.cbSize = sizeof(SCROLLINFO);
621   si.fMask = SIF_POS;
622   
623   switch (type)
624   {
625     case 1:
626       /*Scroll absolutly*/
627       si.nPos = value;
628       break;
629     case 2:
630       /* Scroll up - towards the beginning of the document */
631       si.nPos = nOrigPos - value;
632       break;
633     case 3:
634       /* Scroll down - towards the end of the document */
635       si.nPos = nOrigPos + value;
636       break;
637     default:
638       FIXME("ME_Scroll called incorrectly\n");
639       si.nPos = 0;
640   }
641   
642   nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
643   nActualScroll = nOrigPos - nNewPos;
644   if (editor->bRedraw)
645   {
646     if (abs(nActualScroll) > editor->sizeWindow.cy)
647       InvalidateRect(editor->hWnd, NULL, TRUE);
648     else
649       ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
650     ME_Repaint(editor);
651   }
652   
653   ME_UpdateScrollBar(editor);
654 }
655
656  
657  void ME_UpdateScrollBar(ME_TextEditor *editor)
658
659   /* Note that this is the only funciton that should ever call SetScrolLInfo 
660    * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
661    * be used at all. */
662   
663   HWND hWnd;
664   SCROLLINFO si;
665   BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
666   
667   if (ME_WrapMarkedParagraphs(editor))
668     FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
669   
670   hWnd = editor->hWnd;
671   si.cbSize = sizeof(si);
672   bScrollBarWasVisible = ME_GetYScrollVisible(editor);
673   bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
674   
675   if (bScrollBarWasVisible != bScrollBarWillBeVisible)
676   {
677     ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
678     ME_MarkAllForWrapping(editor);
679     ME_WrapMarkedParagraphs(editor);
680   }
681   
682   si.fMask = SIF_PAGE | SIF_RANGE;
683   if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
684     si.fMask |= SIF_DISABLENOSCROLL;
685   
686   si.nMin = 0;  
687   si.nMax = editor->nTotalLength;
688   
689   si.nPage = editor->sizeWindow.cy;
690      
691   TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
692   SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
693 }
694
695 int ME_GetYScrollPos(ME_TextEditor *editor)
696 {
697   SCROLLINFO si;
698   si.cbSize = sizeof(si);
699   si.fMask = SIF_POS;
700   return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
701 }
702
703 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
704 { /* Returns true if the scrollbar is visible */
705   SCROLLBARINFO sbi;
706   sbi.cbSize = sizeof(sbi);
707   GetScrollBarInfo(editor->hWnd, OBJID_VSCROLL, &sbi);
708   return ((sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE) == 0);
709 }
710
711 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
712 {
713   ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
714   ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
715   int y, yrel, yheight, yold;
716   
717   assert(pRow);
718   assert(pPara);
719   
720   y = pPara->member.para.nYPos+pRow->member.row.nYPos;
721   yheight = pRow->member.row.nHeight;
722   yold = ME_GetYScrollPos(editor);
723   yrel = y - yold;
724   
725   if (y < yold)
726     ME_ScrollAbs(editor,y);
727   else if (yrel + yheight > editor->sizeWindow.cy) 
728     ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
729 }
730
731
732 void
733 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
734 {
735   RECT rc;
736   int x, y, height;
737   ME_Cursor tmp;
738
739   ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
740   ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
741
742   rc.left = 0;
743   rc.top = y;
744   rc.bottom = y + height;
745   rc.right = editor->rcFormat.right;
746   InvalidateRect(editor->hWnd, &rc, FALSE);
747 }
748
749
750 void
751 ME_InvalidateSelection(ME_TextEditor *editor)
752 {
753   ME_DisplayItem *para1, *para2;
754   int nStart, nEnd;
755   int len = ME_GetTextLength(editor);
756
757   ME_GetSelection(editor, &nStart, &nEnd);
758   /* if both old and new selection are 0-char (= caret only), then
759   there's no (inverted) area to be repainted, neither old nor new */
760   if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
761     return;
762   ME_WrapMarkedParagraphs(editor);
763   ME_GetSelectionParas(editor, &para1, &para2);
764   assert(para1->type == diParagraph);
765   assert(para2->type == diParagraph);
766   /* last selection markers aren't always updated, which means
767   they can point past the end of the document */ 
768   if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
769     ME_MarkForPainting(editor,
770         ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
771         ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
772   } else {
773     /* if the start part of selection is being expanded or contracted... */
774     if (nStart < editor->nLastSelStart) {
775       ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
776     } else
777     if (nStart > editor->nLastSelStart) {
778       ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
779     }
780
781     /* if the end part of selection is being contracted or expanded... */
782     if (nEnd < editor->nLastSelEnd) {
783       ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
784     } else
785     if (nEnd > editor->nLastSelEnd) {
786       ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
787     }
788   }
789
790   ME_InvalidateMarkedParagraphs(editor);
791   /* remember the last invalidated position */
792   ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
793   ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
794   assert(editor->pLastSelStartPara->type == diParagraph);
795   assert(editor->pLastSelEndPara->type == diParagraph);
796 }
797
798 void
799 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
800 {
801   editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
802 }
803
804
805 BOOL
806 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
807 {
808   /* TODO: Zoom images and objects */
809
810   if (numerator != 0)
811   {
812     if (denominator == 0)
813       return FALSE;
814     if (1.0 / 64.0 > (float)numerator / (float)denominator
815         || (float)numerator / (float)denominator > 64.0)
816       return FALSE;
817   }
818   
819   editor->nZoomNumerator = numerator;
820   editor->nZoomDenominator = denominator;
821   
822   ME_RewrapRepaint(editor);
823   return TRUE;
824 }