Add support for more than one sound card.
[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 = GetScrollPos(editor->hWnd, SB_VERT);
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     assert(item->type == diParagraph);
39     if (!bOnlyNew || (item->member.para.nFlags & MEPF_REPAINT))
40     {
41       BOOL bPaint = (rcUpdate == NULL);
42       if (rcUpdate)
43         bPaint = c.pt.y<rcUpdate->bottom && 
44           c.pt.y+item->member.para.nHeight>rcUpdate->top;
45       if (bPaint)
46       {
47         ME_DrawParagraph(&c, item);
48         item->member.para.nFlags &= ~MEPF_REPAINT;
49       }
50     }
51     c.pt.y += item->member.para.nHeight;
52     item = item->member.para.next_para;
53   }
54   if (c.pt.y<c.rcView.bottom) {
55     RECT rc;
56     int xs = c.rcView.left, xe = c.rcView.right;
57     int ys = c.pt.y, ye = c.rcView.bottom;
58     
59     if (bOnlyNew)
60     {
61       int y1 = editor->nTotalLength-yoffset, y2 = editor->nLastTotalLength-yoffset;
62       if (y1<y2)
63         ys = y1, ye = y2+1;
64       else
65         ys = ye;
66     }
67     
68     if (rcUpdate && ys!=ye)
69     {
70       xs = rcUpdate->left, xe = rcUpdate->right;
71       if (rcUpdate->top > ys)
72         ys = rcUpdate->top;
73       if (rcUpdate->bottom < ye)
74         ye = rcUpdate->bottom;
75     }
76     
77     rc.left = xs; /* FIXME remove if it's not necessary anymore */
78     rc.top = c.pt.y;
79     rc.right = xe;
80     rc.bottom = c.pt.y+1;
81     FillRect(hDC, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));
82
83     if (ys == c.pt.y) /* don't overwrite the top bar */
84       ys++;
85     if (ye>ys) {
86       rc.left = xs;
87       rc.top = ys;
88       rc.right = xe;
89       rc.bottom = ye;
90       /* this is not supposed to be gray, I know, but lets keep it gray for now for debugging purposes */
91       FillRect(hDC, &rc, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
92     }
93   }
94   editor->nLastTotalLength = editor->nTotalLength;
95   ME_DestroyContext(&c);
96 }
97
98 void ME_MarkParagraphRange(ME_TextEditor *editor, ME_DisplayItem *p1,
99                            ME_DisplayItem *p2, int nFlags)
100 {
101   ME_DisplayItem *p3;  
102   if (p1 == p2)
103   {
104     p1->member.para.nFlags |= nFlags;
105     return;
106   }
107   if (p1->member.para.nCharOfs > p2->member.para.nCharOfs)
108     p3 = p1, p1 = p2, p2 = p3;
109     
110   p1->member.para.nFlags |= nFlags;
111   do {
112     p1 = p1->member.para.next_para;
113     p1->member.para.nFlags |= nFlags;
114   } while (p1 != p2);
115 }
116
117 void ME_MarkOffsetRange(ME_TextEditor *editor, int from, int to, int nFlags)
118 {
119   ME_Cursor c1, c2;
120   ME_CursorFromCharOfs(editor, from, &c1);
121   ME_CursorFromCharOfs(editor, to, &c2);
122   
123   ME_MarkParagraphRange(editor, ME_GetParagraph(c1.pRun), ME_GetParagraph(c2.pRun), nFlags);
124 }
125
126 void ME_MarkSelectionForRepaint(ME_TextEditor *editor)
127 {
128   int from, to, from2, to2, end;
129   
130   end = ME_GetTextLength(editor);
131   ME_GetSelection(editor, &from, &to);
132   from2 = editor->nLastSelStart;
133   to2 = editor->nLastSelEnd;
134   if (from<from2) ME_MarkOffsetRange(editor, from, from2, MEPF_REPAINT);
135   if (from>from2) ME_MarkOffsetRange(editor, from2, from, MEPF_REPAINT);
136   if (to<to2) ME_MarkOffsetRange(editor, to, to2, MEPF_REPAINT);
137   if (to>to2) ME_MarkOffsetRange(editor, to2, to, MEPF_REPAINT);
138
139   editor->nLastSelStart = from;
140   editor->nLastSelEnd = to;
141 }
142
143 void ME_Repaint(ME_TextEditor *editor)
144 {
145   ME_Cursor *pCursor = &editor->pCursors[0];
146   ME_DisplayItem *pRun = NULL;
147   int nOffset = -1;
148   HDC hDC;
149   int nCharOfs = ME_CharOfsFromRunOfs(editor, pCursor->pRun, pCursor->nOffset);
150   
151   ME_RunOfsFromCharOfs(editor, nCharOfs, &pRun, &nOffset);
152   assert(pRun == pCursor->pRun);
153   assert(nOffset == pCursor->nOffset);
154   ME_MarkSelectionForRepaint(editor);
155   ME_WrapMarkedParagraphs(editor);
156   hDC = GetDC(editor->hWnd);
157   ME_HideCaret(editor);
158   ME_PaintContent(editor, hDC, TRUE, NULL);
159   ReleaseDC(editor->hWnd, hDC);
160   ME_ShowCaret(editor);
161 }
162
163 void ME_UpdateRepaint(ME_TextEditor *editor)
164 {
165 /*
166   InvalidateRect(editor->hWnd, NULL, TRUE);
167   */
168   ME_SendOldNotify(editor, EN_CHANGE);
169   ME_Repaint(editor);
170   ME_SendOldNotify(editor, EN_UPDATE);
171   ME_SendSelChange(editor);
172 }
173
174 void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars, 
175   ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
176   HDC hDC = c->hDC;
177   HGDIOBJ hOldFont;
178   COLORREF rgbOld, rgbBack;
179   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
180   rgbBack = ME_GetBackColor(c->editor);
181   if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
182     rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
183   else
184     rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
185   ExtTextOutW(hDC, x, y, 0, NULL, szText, nChars, NULL);
186   if (width) {
187     SIZE sz;
188     GetTextExtentPoint32W(hDC, szText, nChars, &sz);
189     *width = sz.cx;
190   }
191   if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
192   {
193     SIZE sz;
194     if (nSelFrom < 0) nSelFrom = 0;
195     if (nSelTo > nChars) nSelTo = nChars;
196     GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
197     x += sz.cx;
198     GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
199     PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
200   }
201   SetTextColor(hDC, rgbOld);
202   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
203 }
204
205 void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
206   int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
207   HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
208   COLORREF color = SetTextColor(hDC, RGB(128,128,128));
209   TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
210   SelectObject(hDC, hFont);
211   SetTextAlign(hDC, align);
212   SetTextColor(hDC, color);
213 }
214
215 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run, 
216                      ME_Paragraph *para, BOOL selected) {
217   SIZE sz;
218   int xs, ys, xe, ye, h, ym, width, eyes;
219   ME_GetGraphicsSize(c->editor, run, &sz);
220   xs = run->pt.x;
221   ys = y-sz.cy;
222   xe = xs+sz.cx;
223   ye = y;
224   h = ye-ys;
225   ym = ys+h/4;
226   width = sz.cx;
227   eyes = width/8;
228   /* draw a smiling face :) */
229   Ellipse(c->hDC, xs, ys, xe, ye);
230   Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
231   Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
232   MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
233   LineTo(c->hDC, xs+width/8, ys+3*h/4);
234   LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
235   LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
236   if (selected)
237   {
238     /* descent is usually (always?) 0 for graphics */
239     PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);    
240   }
241 }
242
243 void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
244   ME_Run *run = &rundi->member.run;
245   int runofs = run->nCharOfs+para->nCharOfs;
246   
247   /* you can always comment it out if you need visible paragraph marks */
248   if (run->nFlags & MERF_ENDPARA) 
249     return;
250   if (run->nFlags & MERF_GRAPHICS) {
251     int blfrom, blto;
252     ME_GetSelection(c->editor, &blfrom, &blto);
253     ME_DrawGraphics(c, x, y, run, para, (runofs >= blfrom) && (runofs < blto));
254   } else
255   {
256     int blfrom, blto;
257     ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
258     ME_GetSelection(c->editor, &blfrom, &blto);
259     
260     ME_DrawTextWithStyle(c, x, y, 
261       run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, 
262         blfrom-runofs, blto-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
263   }
264 }
265
266 COLORREF ME_GetBackColor(ME_TextEditor *editor)
267 {
268 /* Looks like I was seriously confused
269     return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
270 */
271   if (editor->rgbBackColor == -1)
272     return GetSysColor(COLOR_WINDOW);
273   else
274     return editor->rgbBackColor;
275 }
276
277 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
278   int align = SetTextAlign(c->hDC, TA_BASELINE);
279   ME_DisplayItem *p;
280   ME_Run *run;
281   ME_Paragraph *para = NULL;
282   RECT rc, rcPara;
283   int y = c->pt.y;
284   int height = 0, baseline = 0, no=0, pno = 0;
285   int xs, xe;
286   int visible = 0;
287   int nMargWidth = 0;
288   
289   c->pt.x = c->rcView.left;
290   rcPara.left = c->rcView.left;
291   rcPara.right = c->rcView.right;
292   for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
293     switch(p->type) {
294       case diParagraph:
295         para = &p->member.para;
296         break;
297       case diStartRow:
298         assert(para);
299         nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
300         xs = c->rcView.left+nMargWidth;
301         xe = c->rcView.right-para->nRightMargin;
302         y += height;
303         rcPara.top = y;
304         rcPara.bottom = y+p->member.row.nHeight;
305         visible = RectVisible(c->hDC, &rcPara);
306         if (visible) {
307           HBRUSH hbr;
308           /* left margin */
309           rc.left = c->rcView.left;
310           rc.right = c->rcView.left+nMargWidth;
311           rc.top = y;
312           rc.bottom = y+p->member.row.nHeight;
313           FillRect(c->hDC, &rc, c->hbrMargin);
314           /* right margin */
315           rc.left = xe;
316           rc.right = c->rcView.right;
317           FillRect(c->hDC, &rc, c->hbrMargin);
318           rc.left = c->rcView.left+para->nLeftMargin;
319           rc.right = xe;
320           hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
321           FillRect(c->hDC, &rc, hbr);
322           DeleteObject(hbr);
323         }
324         if (me_debug)
325         {
326           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
327           WCHAR buf[128];
328           POINT pt = c->pt;
329           wsprintfW(buf, wszRowDebug, no);
330           pt.y = 12+y;
331           ME_DebugWrite(c->hDC, &pt, buf);
332         }
333         
334         height = p->member.row.nHeight;
335         baseline = p->member.row.nBaseline;
336         pno++;
337         break;
338       case diRun:
339         assert(para);
340         run = &p->member.run;
341         if (visible && me_debug) {
342           rc.left = c->rcView.left+run->pt.x;
343           rc.right = c->rcView.left+run->pt.x+run->nWidth;
344           rc.top = c->pt.y+run->pt.y;
345           rc.bottom = c->pt.y+run->pt.y+height;
346           TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
347           if (run->nFlags & MERF_SKIPPED)
348             DrawFocusRect(c->hDC, &rc);
349           else
350             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
351         }
352         if (visible)
353           ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
354         if (me_debug)
355         {
356           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
357           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
358           WCHAR buf[2560];
359           POINT pt;
360           pt.x = run->pt.x;
361           pt.y = c->pt.y + run->pt.y;
362           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
363           ME_DebugWrite(c->hDC, &pt, buf);
364         }
365         /* c->pt.x += p->member.run.nWidth; */
366         break;
367       default:
368         break;
369     }
370     no++;
371   }
372   SetTextAlign(c->hDC, align);
373 }
374
375 void ME_UpdateScrollBar(ME_TextEditor *editor, int ypos)
376 {
377   float perc = 0.0;
378   SCROLLINFO si;
379   HWND hWnd = editor->hWnd;
380   int overflow = editor->nTotalLength - editor->sizeWindow.cy;
381   si.cbSize = sizeof(SCROLLINFO);
382   si.fMask = SIF_PAGE|SIF_POS|SIF_RANGE|SIF_TRACKPOS;
383   GetScrollInfo(hWnd, SB_VERT, &si);
384   
385   if (ypos < 0) {
386     if (si.nMax<1) si.nMax = 1;
387     perc = 1.0*si.nPos/si.nMax;
388     ypos = perc*overflow;
389   }
390   if (ypos >= overflow && overflow > 0)
391     ypos = overflow - 1;
392
393   if (overflow > 0) {
394     EnableScrollBar(hWnd, SB_VERT, ESB_ENABLE_BOTH);
395     SetScrollRange(hWnd, SB_VERT, 0, overflow, FALSE);
396     SetScrollPos(hWnd, SB_VERT, ypos, TRUE);
397   } else {
398     EnableScrollBar(hWnd, SB_VERT, ESB_DISABLE_BOTH);
399     SetScrollRange(hWnd, SB_VERT, 0, 0, FALSE);
400     SetScrollPos(hWnd, SB_VERT, 0, TRUE);
401   }
402   if (ypos != si.nPos)
403   {
404     TRACE("ScrollWindow(%d, %d, %d, %0.4f)\n", si.nPos, si.nMax, ypos, perc);
405     ScrollWindow(hWnd, 0, si.nPos - ypos, NULL, NULL);
406     UpdateWindow(hWnd);
407   }
408 }
409
410 int ME_GetScrollPos(ME_TextEditor *editor)
411 {
412   return GetScrollPos(editor->hWnd, SB_VERT);
413 }
414
415 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
416 {
417   ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
418   ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
419   int y, yrel, yheight;
420   
421   assert(pRow);
422   assert(pPara);
423   
424   y = pPara->member.para.nYPos+pRow->member.row.nYPos;
425   yheight = pRow->member.row.nHeight;
426   yrel = y - ME_GetScrollPos(editor);
427   if (yrel < 0)
428     ME_UpdateScrollBar(editor, y);
429   else if (yrel + yheight > editor->sizeWindow.cy)
430   {
431     ME_UpdateScrollBar(editor, y + yheight - editor->sizeWindow.cy);  
432   }
433 }