Pass correct pointer to SHFree in case of failure in
[wine] / dlls / riched20 / paint.c
1 /*
2  * RichEdit - painting functions
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, RECT *rcUpdate) {
26   ME_DisplayItem *item;
27   ME_Context c;
28   int yoffset;
29
30   editor->nSequence++;
31   yoffset = ME_GetYScrollPos(editor);
32   ME_InitContext(&c, editor, hDC);
33   SetBkMode(hDC, TRANSPARENT);
34   ME_MoveCaret(editor);
35   item = editor->pBuffer->pFirst->next;
36   c.pt.y -= yoffset;
37   while(item != editor->pBuffer->pLast) {
38     int ye;
39     assert(item->type == diParagraph);
40     ye = c.pt.y + item->member.para.nHeight;
41     if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
42     {
43       BOOL bPaint = (rcUpdate == NULL);
44       if (rcUpdate)
45         bPaint = c.pt.y<rcUpdate->bottom && 
46           c.pt.y+item->member.para.nHeight>rcUpdate->top;
47       if (bPaint)
48       {
49         ME_DrawParagraph(&c, item);
50         if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
51           item->member.para.nFlags &= ~MEPF_REPAINT;
52       }
53     }
54     c.pt.y = ye;
55     item = item->member.para.next_para;
56   }
57   if (c.pt.y<c.rcView.bottom) {
58     RECT rc;
59     int xs = c.rcView.left, xe = c.rcView.right;
60     int ys = c.pt.y, ye = c.rcView.bottom;
61     
62     if (bOnlyNew)
63     {
64       int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
65       if (y1<y2)
66         ys = y1, ye = y2+1;
67       else
68         ys = ye;
69     }
70     
71     if (rcUpdate && ys!=ye)
72     {
73       xs = rcUpdate->left, xe = rcUpdate->right;
74       if (rcUpdate->top > ys)
75         ys = rcUpdate->top;
76       if (rcUpdate->bottom < ye)
77         ye = rcUpdate->bottom;
78     }
79
80     if (ye>ys) {
81       HBRUSH hbr;
82       hbr = CreateSolidBrush(ME_GetBackColor(c.editor));
83       rc.left = xs;
84       rc.top = ys;
85       rc.right = xe;
86       rc.bottom = ye;
87       FillRect(hDC, &rc, hbr);
88       DeleteObject(hbr);
89     }
90     if (ys == c.pt.y) /* don't overwrite the top bar */
91       ys++;
92   }
93   editor->nLastTotalLength = editor->nTotalLength;
94   ME_DestroyContext(&c);
95 }
96
97 static void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
98                            ME_DisplayItem *p2, int nFlags)
99 {
100   ME_DisplayItem *p3;  
101   if (p1 == p2)
102   {
103     p1->member.para.nFlags |= nFlags;
104     return;
105   }
106   if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
107     p3 = p1, p1 = p2, p2 = p3;
108     
109   p1->member.para.nFlags |= nFlags;
110   do {
111     p1 = p1->member.para.next_para;
112     p1->member.para.nFlags |= nFlags;
113   } while (p1 != p2);
114 }
115
116 static void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
117 {
118   ME_Cursor c1, c2;
119   ME_CursorFromCharOfs(editor, from, &c1);
120   ME_CursorFromCharOfs(editor, to, &c2);
121   
122   ME_MarkParagraphRange(editor, ME_GetParagraph(c1.pRun), ME_GetParagraph(c2.pRun), nFlags);
123 }
124
125 static void ME_MarkSelectionForRepaint(ME_TextEditor *editor)
126 {
127   int from, to, from2, to2, end;
128   
129   end = ME_GetTextLength(editor);
130   ME_GetSelection(editor, &from, &to);
131   from2 = editor->nLastSelStart;
132   to2 = editor->nLastSelEnd;
133   if (from<from2) ME_MarkOffsetRange(editor, from, from2, MEPF_REPAINT);
134   if (from>from2) ME_MarkOffsetRange(editor, from2, from, MEPF_REPAINT);
135   if (to<to2) ME_MarkOffsetRange(editor, to, to2, MEPF_REPAINT);
136   if (to>to2) ME_MarkOffsetRange(editor, to2, to, MEPF_REPAINT);
137
138   editor->nLastSelStart = from;
139   editor->nLastSelEnd = to;
140 }
141
142 void ME_Repaint(ME_TextEditor *editor)
143 {
144   ME_Cursor *pCursor = &editor->pCursors[0];
145   ME_DisplayItem *pRun = NULL;
146   int nOffset = -1;
147   HDC hDC;
148   int nCharOfs = ME_CharOfsFromRunOfs(editor, pCursor->pRun, pCursor->nOffset);
149   
150   ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
151   assert(pRun == pCursor->pRun);
152   assert(nOffset == pCursor->nOffset);
153   ME_MarkSelectionForRepaint(editor);
154   if (ME_WrapMarkedParagraphs(editor)) {
155     ME_UpdateScrollBar(editor);
156   }
157   if (editor->bRedraw)
158   {
159     hDC = GetDC(editor->hWnd);
160     ME_HideCaret(editor);
161     ME_PaintContent(editor, hDC, TRUE, NULL);
162     ReleaseDC(editor->hWnd, hDC);
163     ME_ShowCaret(editor);
164     ME_EnsureVisible(editor, pCursor->pRun);
165   }
166 }
167
168 void ME_UpdateRepaint(ME_TextEditor *editor)
169 {
170 /*
171   InvalidateRect(editor->hWnd, NULL, TRUE);
172   */
173   ME_SendOldNotify(editor, EN_CHANGE);
174   ME_Repaint(editor);
175   ME_SendOldNotify(editor, EN_UPDATE);
176   ME_SendSelChange(editor);
177 }
178
179
180 void
181 ME_RewrapRepaint(ME_TextEditor *editor)
182 {
183   ME_MarkAllForWrapping(editor);
184   ME_WrapMarkedParagraphs(editor);
185   ME_UpdateScrollBar(editor);
186   ME_Repaint(editor);
187 }
188
189
190 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars, 
191   ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
192   HDC hDC = c->hDC;
193   HGDIOBJ hOldFont;
194   COLORREF rgbOld, rgbBack;
195   int yOffset = 0, yTwipsOffset = 0;
196   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
197   rgbBack = ME_GetBackColor(c->editor);
198   if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
199     rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
200   else
201     rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
202   if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
203     yTwipsOffset = s->fmt.yOffset;
204   }
205   if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
206     if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
207     if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
208   }
209   if (yTwipsOffset)
210   {
211     int numerator = 1;
212     int denominator = 1;
213     
214     if (c->editor->nZoomNumerator)
215     {
216       numerator = c->editor->nZoomNumerator;
217       denominator = c->editor->nZoomDenominator;
218     }
219     yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
220   }
221   ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
222   if (width) {
223     SIZE sz;
224     GetTextExtentPoint32W(hDC, szText, nChars, &sz);
225     *width = sz.cx;
226   }
227   if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
228   {
229     SIZE sz;
230     if (nSelFrom < 0) nSelFrom = 0;
231     if (nSelTo > nChars) nSelTo = nChars;
232     GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
233     x += sz.cx;
234     GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
235     PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
236   }
237   SetTextColor(hDC, rgbOld);
238   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
239 }
240
241 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
242   int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
243   HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
244   COLORREF color = SetTextColor(hDC, RGB(128,128,128));
245   TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
246   SelectObject(hDC, hFont);
247   SetTextAlign(hDC, align);
248   SetTextColor(hDC, color);
249 }
250
251 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run, 
252                      ME_Paragraph *para, BOOL selected) {
253   SIZE sz;
254   int xs, ys, xe, ye, h, ym, width, eyes;
255   ME_GetGraphicsSize(c->editor, run, &sz);
256   xs = run->pt.x;
257   ys = y-sz.cy;
258   xe = xs+sz.cx;
259   ye = y;
260   h = ye-ys;
261   ym = ys+h/4;
262   width = sz.cx;
263   eyes = width/8;
264   /* draw a smiling face :) */
265   Ellipse(c->hDC, xs, ys, xe, ye);
266   Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
267   Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
268   MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
269   LineTo(c->hDC, xs+width/8, ys+3*h/4);
270   LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
271   LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
272   if (selected)
273   {
274     /* descent is usually (always?) 0 for graphics */
275     PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);    
276   }
277 }
278
279 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
280   ME_Run *run = &rundi->member.run;
281   int runofs = run->nCharOfs+para->nCharOfs;
282   
283   /* you can always comment it out if you need visible paragraph marks */
284   if (run->nFlags & (MERF_ENDPARA|MERF_TAB)) 
285     return;
286   if (run->nFlags & MERF_GRAPHICS) {
287     int blfrom, blto;
288     ME_GetSelection(c->editor, &blfrom, &blto);
289     ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
290   } else
291   {
292     int blfrom, blto;
293     ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
294     ME_GetSelection(c->editor, &blfrom, &blto);
295     
296     ME_DrawTextWithStyle(c, x, y, 
297       run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, 
298         blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
299   }
300 }
301
302 COLORREF ME_GetBackColor(ME_TextEditor *editor)
303 {
304 /* Looks like I was seriously confused
305     return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
306 */
307   if (editor->rgbBackColor == -1)
308     return GetSysColor(COLOR_WINDOW);
309   else
310     return editor->rgbBackColor;
311 }
312
313 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
314   int align = SetTextAlign(c->hDC, TA_BASELINE);
315   ME_DisplayItem *p;
316   ME_Run *run;
317   ME_Paragraph *para = NULL;
318   RECT rc, rcPara;
319   int y = c->pt.y;
320   int height = 0, baseline = 0, no=0, pno = 0;
321   int xs, xe;
322   int visible = 0;
323   int nMargWidth = 0;
324   
325   c->pt.x = c->rcView.left;
326   rcPara.left = c->rcView.left;
327   rcPara.right = c->rcView.right;
328   for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
329     switch(p->type) {
330       case diParagraph:
331         para = &p->member.para;
332         break;
333       case diStartRow:
334         assert(para);
335         nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
336         xs = c->rcView.left+nMargWidth;
337         xe = c->rcView.right-para->nRightMargin;
338         y += height;
339         rcPara.top = y;
340         rcPara.bottom = y+p->member.row.nHeight;
341         visible = RectVisible(c->hDC, &rcPara);
342         if (visible) {
343           HBRUSH hbr;
344           hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
345           /* left margin */
346           rc.left = c->rcView.left;
347           rc.right = c->rcView.left+nMargWidth;
348           rc.top = y;
349           rc.bottom = y+p->member.row.nHeight;
350           FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
351           /* right margin */
352           rc.left = xe;
353           rc.right = c->rcView.right;
354           FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
355           rc.left = c->rcView.left+nMargWidth;
356           rc.right = xe;
357           FillRect(c->hDC, &rc, hbr);
358           DeleteObject(hbr);
359         }
360         if (me_debug)
361         {
362           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
363           WCHAR buf[128];
364           POINT pt = c->pt;
365           wsprintfW(buf, wszRowDebug, no);
366           pt.y = 12+y;
367           ME_DebugWrite(c->hDC, &pt, buf);
368         }
369         
370         height = p->member.row.nHeight;
371         baseline = p->member.row.nBaseline;
372         pno++;
373         break;
374       case diRun:
375         assert(para);
376         run = &p->member.run;
377         if (visible && me_debug) {
378           rc.left = c->rcView.left+run->pt.x;
379           rc.right = c->rcView.left+run->pt.x+run->nWidth;
380           rc.top = c->pt.y+run->pt.y;
381           rc.bottom = c->pt.y+run->pt.y+height;
382           TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
383           if (run->nFlags & MERF_SKIPPED)
384             DrawFocusRect(c->hDC, &rc);
385           else
386             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
387         }
388         if (visible)
389           ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
390         if (me_debug)
391         {
392           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
393           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
394           WCHAR buf[2560];
395           POINT pt;
396           pt.x = run->pt.x;
397           pt.y = c->pt.y + run->pt.y;
398           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
399           ME_DebugWrite(c->hDC, &pt, buf);
400         }
401         /* c->pt.x += p->member.run.nWidth; */
402         break;
403       default:
404         break;
405     }
406     no++;
407   }
408   SetTextAlign(c->hDC, align);
409 }
410
411 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
412 {
413   SCROLLINFO si;
414   HWND hWnd = editor->hWnd;
415
416   si.cbSize = sizeof(SCROLLINFO);
417   si.fMask = SIF_POS;
418   GetScrollInfo(hWnd, SB_VERT, &si);
419   si.nPos = editor->nScrollPosY -= cy;
420   SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
421   if (editor->bRedraw)
422   {
423     if (abs(cy) > editor->sizeWindow.cy)
424       InvalidateRect(editor->hWnd, NULL, TRUE);
425     else
426       ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
427   }
428 }
429
430 void ME_UpdateScrollBar(ME_TextEditor *editor)
431 {
432   HWND hWnd = editor->hWnd;
433   SCROLLINFO si;
434   int nOldLen = editor->nTotalLength;
435   BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
436   BOOL bUpdateScrollBars;
437   si.cbSize = sizeof(si);
438   si.fMask = SIF_POS | SIF_RANGE;
439   GetScrollInfo(hWnd, SB_VERT, &si);
440   bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
441   
442   if (bScrollY != editor->bScrollY)
443   {
444     si.fMask = SIF_RANGE | SIF_PAGE;
445     si.nMin = 0;
446     si.nPage = editor->sizeWindow.cy;
447     if (bScrollY) {
448       si.nMax = editor->nTotalLength;
449     } else {
450       si.nMax = 0;
451     }
452     SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
453     ME_MarkAllForWrapping(editor);
454     editor->bScrollY = bScrollY;
455     ME_WrapMarkedParagraphs(editor);
456     bUpdateScrollBars = TRUE;
457   }
458   if (bUpdateScrollBars) {
459     int nScroll = 0;
460     si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
461     if (editor->nTotalLength > editor->sizeWindow.cy) {
462       si.nMax = editor->nTotalLength;
463       si.nPage = editor->sizeWindow.cy;
464       if (si.nPos > si.nMax-si.nPage) {
465         nScroll = (si.nMax-si.nPage)-si.nPos;
466         si.nPos = si.nMax-si.nPage;
467       }
468     }
469     else {
470       si.nMax = 0;
471       si.nPage = 0;
472       si.nPos = 0;
473     }
474     TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
475     editor->nScrollPosY = si.nPos;
476     SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
477     if (nScroll)
478       ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
479   }
480 }
481
482 int ME_GetYScrollPos(ME_TextEditor *editor)
483 {
484   return editor->nScrollPosY;
485 }
486
487 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
488 {
489   ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
490   ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
491   int y, yrel, yheight, yold;
492   HWND hWnd = editor->hWnd;
493   
494   assert(pRow);
495   assert(pPara);
496   
497   y = pPara->member.para.nYPos+pRow->member.row.nYPos;
498   yheight = pRow->member.row.nHeight;
499   yold = ME_GetYScrollPos(editor);
500   yrel = y - yold;
501   if (yrel < 0) {
502     editor->nScrollPosY = y;
503     SetScrollPos(hWnd, SB_VERT, y, TRUE);
504     if (editor->bRedraw)
505     {
506       ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
507       UpdateWindow(hWnd);
508     }
509   } else if (yrel + yheight > editor->sizeWindow.cy) {
510     int newy = y+yheight-editor->sizeWindow.cy;
511     editor->nScrollPosY = newy;
512     SetScrollPos(hWnd, SB_VERT, newy, TRUE);
513     if (editor->bRedraw)
514     {
515       ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
516       UpdateWindow(hWnd);
517     }
518   }
519 }
520         
521
522 BOOL
523 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
524 {
525   /* TODO: Zoom images and objects */
526
527   if (numerator != 0)
528   {
529     if (denominator == 0)
530       return FALSE;
531     if (1.0 / 64.0 > (float)numerator / (float)denominator
532         || (float)numerator / (float)denominator > 64.0)
533       return FALSE;
534   }
535   
536   editor->nZoomNumerator = numerator;
537   editor->nZoomDenominator = denominator;
538   
539   ME_RewrapRepaint(editor);
540   return TRUE;
541 }