Don't open device if already open.
[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 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 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 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   hDC = GetDC(editor->hWnd);
158   ME_HideCaret(editor);
159   ME_PaintContent(editor, hDC, TRUE, NULL);
160   ReleaseDC(editor->hWnd, hDC);
161   ME_ShowCaret(editor);
162   ME_EnsureVisible(editor, pCursor->pRun);
163 }
164
165 void ME_UpdateRepaint(ME_TextEditor *editor)
166 {
167 /*
168   InvalidateRect(editor->hWnd, NULL, TRUE);
169   */
170   ME_SendOldNotify(editor, EN_CHANGE);
171   ME_Repaint(editor);
172   ME_SendOldNotify(editor, EN_UPDATE);
173   ME_SendSelChange(editor);
174 }
175
176 void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars, 
177   ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
178   HDC hDC = c->hDC;
179   HGDIOBJ hOldFont;
180   COLORREF rgbOld, rgbBack;
181   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
182   rgbBack = ME_GetBackColor(c->editor);
183   if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
184     rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
185   else
186     rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
187   ExtTextOutW(hDC, x, y, 0, NULL, szText, nChars, NULL);
188   if (width) {
189     SIZE sz;
190     GetTextExtentPoint32W(hDC, szText, nChars, &sz);
191     *width = sz.cx;
192   }
193   if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
194   {
195     SIZE sz;
196     if (nSelFrom < 0) nSelFrom = 0;
197     if (nSelTo > nChars) nSelTo = nChars;
198     GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
199     x += sz.cx;
200     GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
201     PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
202   }
203   SetTextColor(hDC, rgbOld);
204   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
205 }
206
207 void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
208   int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
209   HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
210   COLORREF color = SetTextColor(hDC, RGB(128,128,128));
211   TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
212   SelectObject(hDC, hFont);
213   SetTextAlign(hDC, align);
214   SetTextColor(hDC, color);
215 }
216
217 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run, 
218                      ME_Paragraph *para, BOOL selected) {
219   SIZE sz;
220   int xs, ys, xe, ye, h, ym, width, eyes;
221   ME_GetGraphicsSize(c->editor, run, &sz);
222   xs = run->pt.x;
223   ys = y-sz.cy;
224   xe = xs+sz.cx;
225   ye = y;
226   h = ye-ys;
227   ym = ys+h/4;
228   width = sz.cx;
229   eyes = width/8;
230   /* draw a smiling face :) */
231   Ellipse(c->hDC, xs, ys, xe, ye);
232   Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
233   Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
234   MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
235   LineTo(c->hDC, xs+width/8, ys+3*h/4);
236   LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
237   LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
238   if (selected)
239   {
240     /* descent is usually (always?) 0 for graphics */
241     PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);    
242   }
243 }
244
245 void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
246   ME_Run *run = &rundi->member.run;
247   int runofs = run->nCharOfs+para->nCharOfs;
248   
249   /* you can always comment it out if you need visible paragraph marks */
250   if (run->nFlags & (MERF_ENDPARA|MERF_TAB)) 
251     return;
252   if (run->nFlags & MERF_GRAPHICS) {
253     int blfrom, blto;
254     ME_GetSelection(c->editor, &blfrom, &blto);
255     ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
256   } else
257   {
258     int blfrom, blto;
259     ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
260     ME_GetSelection(c->editor, &blfrom, &blto);
261     
262     ME_DrawTextWithStyle(c, x, y, 
263       run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, 
264         blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
265   }
266 }
267
268 COLORREF ME_GetBackColor(ME_TextEditor *editor)
269 {
270 /* Looks like I was seriously confused
271     return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
272 */
273   if (editor->rgbBackColor == -1)
274     return GetSysColor(COLOR_WINDOW);
275   else
276     return editor->rgbBackColor;
277 }
278
279 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
280   int align = SetTextAlign(c->hDC, TA_BASELINE);
281   ME_DisplayItem *p;
282   ME_Run *run;
283   ME_Paragraph *para = NULL;
284   RECT rc, rcPara;
285   int y = c->pt.y;
286   int height = 0, baseline = 0, no=0, pno = 0;
287   int xs, xe;
288   int visible = 0;
289   int nMargWidth = 0;
290   
291   c->pt.x = c->rcView.left;
292   rcPara.left = c->rcView.left;
293   rcPara.right = c->rcView.right;
294   for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
295     switch(p->type) {
296       case diParagraph:
297         para = &p->member.para;
298         break;
299       case diStartRow:
300         assert(para);
301         nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
302         xs = c->rcView.left+nMargWidth;
303         xe = c->rcView.right-para->nRightMargin;
304         y += height;
305         rcPara.top = y;
306         rcPara.bottom = y+p->member.row.nHeight;
307         visible = RectVisible(c->hDC, &rcPara);
308         if (visible) {
309           HBRUSH hbr;
310           hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
311           /* left margin */
312           rc.left = c->rcView.left;
313           rc.right = c->rcView.left+nMargWidth;
314           rc.top = y;
315           rc.bottom = y+p->member.row.nHeight;
316           FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
317           /* right margin */
318           rc.left = xe;
319           rc.right = c->rcView.right;
320           FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
321           rc.left = c->rcView.left+nMargWidth;
322           rc.right = xe;
323           FillRect(c->hDC, &rc, hbr);
324           DeleteObject(hbr);
325         }
326         if (me_debug)
327         {
328           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
329           WCHAR buf[128];
330           POINT pt = c->pt;
331           wsprintfW(buf, wszRowDebug, no);
332           pt.y = 12+y;
333           ME_DebugWrite(c->hDC, &pt, buf);
334         }
335         
336         height = p->member.row.nHeight;
337         baseline = p->member.row.nBaseline;
338         pno++;
339         break;
340       case diRun:
341         assert(para);
342         run = &p->member.run;
343         if (visible && me_debug) {
344           rc.left = c->rcView.left+run->pt.x;
345           rc.right = c->rcView.left+run->pt.x+run->nWidth;
346           rc.top = c->pt.y+run->pt.y;
347           rc.bottom = c->pt.y+run->pt.y+height;
348           TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
349           if (run->nFlags & MERF_SKIPPED)
350             DrawFocusRect(c->hDC, &rc);
351           else
352             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
353         }
354         if (visible)
355           ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
356         if (me_debug)
357         {
358           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
359           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
360           WCHAR buf[2560];
361           POINT pt;
362           pt.x = run->pt.x;
363           pt.y = c->pt.y + run->pt.y;
364           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
365           ME_DebugWrite(c->hDC, &pt, buf);
366         }
367         /* c->pt.x += p->member.run.nWidth; */
368         break;
369       default:
370         break;
371     }
372     no++;
373   }
374   SetTextAlign(c->hDC, align);
375 }
376
377 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
378 {
379   SCROLLINFO si;
380   HWND hWnd = editor->hWnd;
381
382   si.cbSize = sizeof(SCROLLINFO);
383   si.fMask = SIF_POS;
384   GetScrollInfo(hWnd, SB_VERT, &si);
385   si.nPos = editor->nScrollPosY -= cy;
386   SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
387   if (abs(cy) > editor->sizeWindow.cy)
388     InvalidateRect(editor->hWnd, NULL, TRUE);
389   else
390     ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
391 }
392
393 void ME_UpdateScrollBar(ME_TextEditor *editor)
394 {
395   HWND hWnd = editor->hWnd;
396   SCROLLINFO si;
397   int nOldLen = editor->nTotalLength;
398   BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
399   BOOL bUpdateScrollBars;
400   si.cbSize = sizeof(si);
401   si.fMask = SIF_POS | SIF_RANGE;
402   GetScrollInfo(hWnd, SB_VERT, &si);
403   bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
404   
405   if (bScrollY != editor->bScrollY)
406   {
407     si.fMask = SIF_RANGE | SIF_PAGE;
408     si.nMin = 0;
409     si.nPage = editor->sizeWindow.cy;
410     if (bScrollY) {
411       si.nMax = editor->nTotalLength;
412     } else {
413       si.nMax = 0;
414     }
415     SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
416     ME_MarkAllForWrapping(editor);
417     editor->bScrollY = bScrollY;
418     ME_WrapMarkedParagraphs(editor);
419     bUpdateScrollBars = TRUE;
420   }
421   if (bUpdateScrollBars) {
422     int nScroll = 0;
423     si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
424     if (editor->nTotalLength > editor->sizeWindow.cy) {
425       si.nMax = editor->nTotalLength;
426       si.nPage = editor->sizeWindow.cy;
427       if (si.nPos > si.nMax-si.nPage) {
428         nScroll = (si.nMax-si.nPage)-si.nPos;
429         si.nPos = si.nMax-si.nPage;
430       }
431     }
432     else {
433       si.nMax = 0;
434       si.nPage = 0;
435       si.nPos = 0;
436     }
437     TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
438     editor->nScrollPosY = si.nPos;
439     SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
440     if (nScroll)
441       ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
442   }
443 }
444
445 int ME_GetYScrollPos(ME_TextEditor *editor)
446 {
447   return editor->nScrollPosY;
448 }
449
450 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
451 {
452   ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
453   ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
454   int y, yrel, yheight, yold;
455   HWND hWnd = editor->hWnd;
456   
457   assert(pRow);
458   assert(pPara);
459   
460   y = pPara->member.para.nYPos+pRow->member.row.nYPos;
461   yheight = pRow->member.row.nHeight;
462   yold = ME_GetYScrollPos(editor);
463   yrel = y - yold;
464   if (yrel < 0) {
465     editor->nScrollPosY = y;
466     SetScrollPos(hWnd, SB_VERT, y, TRUE);
467     ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
468     UpdateWindow(hWnd);
469   } else if (yrel + yheight > editor->sizeWindow.cy) {
470     int newy = y+yheight-editor->sizeWindow.cy;
471     editor->nScrollPosY = newy;
472     SetScrollPos(hWnd, SB_VERT, newy, TRUE);
473     ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
474     UpdateWindow(hWnd);
475   }
476 }