crypt32: Use the caller-supplied encoding type when decoding certs.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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, 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   ME_Cursor *pCursor = &editor->pCursors[0];
100
101   if (ME_WrapMarkedParagraphs(editor)) {
102     ME_UpdateScrollBar(editor);
103   }
104   if (editor->bRedraw)
105   {
106     ME_EnsureVisible(editor, pCursor->pRun);
107     UpdateWindow(editor->hWnd);
108   }
109 }
110
111 void ME_UpdateRepaint(ME_TextEditor *editor)
112 {
113 /*
114   InvalidateRect(editor->hWnd, NULL, TRUE);
115   */
116   ME_SendOldNotify(editor, EN_CHANGE);
117   ME_Repaint(editor);
118   ME_SendOldNotify(editor, EN_UPDATE);
119   ME_SendSelChange(editor);
120 }
121
122
123 void
124 ME_RewrapRepaint(ME_TextEditor *editor)
125 {
126   ME_MarkAllForWrapping(editor);
127   ME_WrapMarkedParagraphs(editor);
128   ME_UpdateScrollBar(editor);
129   ME_Repaint(editor);
130 }
131
132
133 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText, int nChars, 
134   ME_Style *s, int *width, int nSelFrom, int nSelTo, int ymin, int cy) {
135   HDC hDC = c->hDC;
136   HGDIOBJ hOldFont;
137   COLORREF rgbOld, rgbBack;
138   int yOffset = 0, yTwipsOffset = 0;
139   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
140   rgbBack = ME_GetBackColor(c->editor);
141   if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
142     rgbOld = SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
143   else
144     rgbOld = SetTextColor(hDC, s->fmt.crTextColor);
145   if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
146     yTwipsOffset = s->fmt.yOffset;
147   }
148   if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
149     if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
150     if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
151   }
152   if (yTwipsOffset)
153   {
154     int numerator = 1;
155     int denominator = 1;
156     
157     if (c->editor->nZoomNumerator)
158     {
159       numerator = c->editor->nZoomNumerator;
160       denominator = c->editor->nZoomDenominator;
161     }
162     yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
163   }
164   ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, NULL);
165   if (width) {
166     SIZE sz;
167     GetTextExtentPoint32W(hDC, szText, nChars, &sz);
168     *width = sz.cx;
169   }
170   if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
171   {
172     SIZE sz;
173     if (nSelFrom < 0) nSelFrom = 0;
174     if (nSelTo > nChars) nSelTo = nChars;
175     GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
176     x += sz.cx;
177     GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
178     
179     /* Invert selection if not hidden by EM_HIDESELECTION */
180     if (c->editor->bHideSelection == FALSE)
181         PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
182   }
183   SetTextColor(hDC, rgbOld);
184   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
185 }
186
187 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
188   int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
189   HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
190   COLORREF color = SetTextColor(hDC, RGB(128,128,128));
191   TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
192   SelectObject(hDC, hFont);
193   SetTextAlign(hDC, align);
194   SetTextColor(hDC, color);
195 }
196
197 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run, 
198                      ME_Paragraph *para, BOOL selected) {
199   SIZE sz;
200   int xs, ys, xe, ye, h, ym, width, eyes;
201   ME_GetGraphicsSize(c->editor, run, &sz);
202   xs = run->pt.x;
203   ys = y-sz.cy;
204   xe = xs+sz.cx;
205   ye = y;
206   h = ye-ys;
207   ym = ys+h/4;
208   width = sz.cx;
209   eyes = width/8;
210   /* draw a smiling face :) */
211   Ellipse(c->hDC, xs, ys, xe, ye);
212   Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
213   Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
214   MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
215   LineTo(c->hDC, xs+width/8, ys+3*h/4);
216   LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
217   LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
218   if (selected)
219   {
220     /* descent is usually (always?) 0 for graphics */
221     PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);    
222   }
223 }
224
225 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
226   ME_Run *run = &rundi->member.run;
227   ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
228   int runofs = run->nCharOfs+para->nCharOfs;
229   int nSelFrom, nSelTo;
230   const WCHAR wszSpace[] = {' ', 0};
231   
232   if (run->nFlags & MERF_HIDDEN)
233     return;
234
235   ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
236
237   /* Draw selected end-of-paragraph mark */
238   if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
239     ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
240                          c->pt.y + start->member.row.nYPos,
241                          start->member.row.nHeight);
242           
243   /* you can always comment it out if you need visible paragraph marks */
244   if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL)) 
245     return;
246
247   if (run->nFlags & MERF_GRAPHICS)
248     ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
249   else
250     ME_DrawTextWithStyle(c, x, y, 
251       run->strText->szData, ME_StrVLen(run->strText), run->style, NULL, 
252         nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
253 }
254
255 COLORREF ME_GetBackColor(ME_TextEditor *editor)
256 {
257 /* Looks like I was seriously confused
258     return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
259 */
260   if (editor->rgbBackColor == -1)
261     return GetSysColor(COLOR_WINDOW);
262   else
263     return editor->rgbBackColor;
264 }
265
266 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
267   int align = SetTextAlign(c->hDC, TA_BASELINE);
268   ME_DisplayItem *p;
269   ME_Run *run;
270   ME_Paragraph *para = NULL;
271   RECT rc, rcPara;
272   int y = c->pt.y;
273   int height = 0, baseline = 0, no=0, pno = 0;
274   int xs, xe;
275   int visible = 0;
276   int nMargWidth = 0;
277   
278   c->pt.x = c->rcView.left;
279   rcPara.left = c->rcView.left;
280   rcPara.right = c->rcView.right;
281   for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
282     switch(p->type) {
283       case diParagraph:
284         para = &p->member.para;
285         break;
286       case diStartRow:
287         assert(para);
288         nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
289         xs = c->rcView.left+nMargWidth;
290         xe = c->rcView.right-para->nRightMargin;
291         y += height;
292         rcPara.top = y;
293         rcPara.bottom = y+p->member.row.nHeight;
294         visible = RectVisible(c->hDC, &rcPara);
295         if (visible) {
296           HBRUSH hbr;
297           hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
298           /* left margin */
299           rc.left = c->rcView.left;
300           rc.right = c->rcView.left+nMargWidth;
301           rc.top = y;
302           rc.bottom = y+p->member.row.nHeight;
303           FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
304           /* right margin */
305           rc.left = xe;
306           rc.right = c->rcView.right;
307           FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
308           rc.left = c->rcView.left+nMargWidth;
309           rc.right = xe;
310           FillRect(c->hDC, &rc, hbr);
311           DeleteObject(hbr);
312         }
313         if (me_debug)
314         {
315           const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
316           WCHAR buf[128];
317           POINT pt = c->pt;
318           wsprintfW(buf, wszRowDebug, no);
319           pt.y = 12+y;
320           ME_DebugWrite(c->hDC, &pt, buf);
321         }
322         
323         height = p->member.row.nHeight;
324         baseline = p->member.row.nBaseline;
325         pno++;
326         break;
327       case diRun:
328         assert(para);
329         run = &p->member.run;
330         if (visible && me_debug) {
331           rc.left = c->rcView.left+run->pt.x;
332           rc.right = c->rcView.left+run->pt.x+run->nWidth;
333           rc.top = c->pt.y+run->pt.y;
334           rc.bottom = c->pt.y+run->pt.y+height;
335           TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
336           if (run->nFlags & MERF_SKIPPED)
337             DrawFocusRect(c->hDC, &rc);
338           else
339             FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
340         }
341         if (visible)
342           ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
343         if (me_debug)
344         {
345           /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
346           const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
347           WCHAR buf[2560];
348           POINT pt;
349           pt.x = run->pt.x;
350           pt.y = c->pt.y + run->pt.y;
351           wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
352           ME_DebugWrite(c->hDC, &pt, buf);
353         }
354         /* c->pt.x += p->member.run.nWidth; */
355         break;
356       default:
357         break;
358     }
359     no++;
360   }
361   SetTextAlign(c->hDC, align);
362 }
363
364 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
365 {
366   SCROLLINFO si;
367   HWND hWnd = editor->hWnd;
368
369   si.cbSize = sizeof(SCROLLINFO);
370   si.fMask = SIF_POS;
371   GetScrollInfo(hWnd, SB_VERT, &si);
372   si.nPos = editor->nScrollPosY -= cy;
373   SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
374   if (editor->bRedraw)
375   {
376     if (abs(cy) > editor->sizeWindow.cy)
377       InvalidateRect(editor->hWnd, NULL, TRUE);
378     else
379       ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
380   }
381 }
382
383 void ME_UpdateScrollBar(ME_TextEditor *editor)
384 {
385   HWND hWnd = editor->hWnd;
386   SCROLLINFO si;
387   int nOldLen = editor->nTotalLength;
388   BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
389   BOOL bUpdateScrollBars;
390   si.cbSize = sizeof(si);
391   si.fMask = SIF_POS | SIF_RANGE;
392   GetScrollInfo(hWnd, SB_VERT, &si);
393   bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
394   
395   if (bScrollY != editor->bScrollY)
396   {
397     si.fMask = SIF_RANGE | SIF_PAGE;
398     si.nMin = 0;
399     si.nPage = editor->sizeWindow.cy;
400     if (bScrollY) {
401       si.nMax = editor->nTotalLength;
402     } else {
403       si.nMax = 0;
404     }
405     SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
406     ME_MarkAllForWrapping(editor);
407     editor->bScrollY = bScrollY;
408     ME_WrapMarkedParagraphs(editor);
409     bUpdateScrollBars = TRUE;
410   }
411   if (bUpdateScrollBars) {
412     int nScroll = 0;
413     si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
414     if (editor->nTotalLength > editor->sizeWindow.cy) {
415       si.nMax = editor->nTotalLength;
416       si.nPage = editor->sizeWindow.cy;
417       if (si.nPos > si.nMax-si.nPage) {
418         nScroll = (si.nMax-si.nPage)-si.nPos;
419         si.nPos = si.nMax-si.nPage;
420       }
421     }
422     else {
423       si.nMax = 0;
424       si.nPage = 0;
425       si.nPos = 0;
426     }
427     TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
428     editor->nScrollPosY = si.nPos;
429     SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
430     if (nScroll)
431       ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
432   }
433 }
434
435 int ME_GetYScrollPos(ME_TextEditor *editor)
436 {
437   return editor->nScrollPosY;
438 }
439
440 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
441 {
442   ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
443   ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
444   int y, yrel, yheight, yold;
445   HWND hWnd = editor->hWnd;
446   
447   assert(pRow);
448   assert(pPara);
449   
450   y = pPara->member.para.nYPos+pRow->member.row.nYPos;
451   yheight = pRow->member.row.nHeight;
452   yold = ME_GetYScrollPos(editor);
453   yrel = y - yold;
454   if (yrel < 0) {
455     editor->nScrollPosY = y;
456     SetScrollPos(hWnd, SB_VERT, y, TRUE);
457     if (editor->bRedraw)
458     {
459       ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
460       UpdateWindow(hWnd);
461     }
462   } else if (yrel + yheight > editor->sizeWindow.cy) {
463     int newy = y+yheight-editor->sizeWindow.cy;
464     editor->nScrollPosY = newy;
465     SetScrollPos(hWnd, SB_VERT, newy, TRUE);
466     if (editor->bRedraw)
467     {
468       ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
469       UpdateWindow(hWnd);
470     }
471   }
472 }
473
474
475 void
476 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
477 {
478   RECT rc;
479   int x, y, height;
480   ME_Cursor tmp;
481
482   ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
483   ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
484
485   rc.left = 0;
486   rc.top = y;
487   rc.bottom = y + height;
488   rc.right = editor->rcFormat.right;
489   InvalidateRect(editor->hWnd, &rc, FALSE);
490 }
491
492
493 void
494 ME_InvalidateSelection(ME_TextEditor *editor)
495 {
496   if (ME_IsSelection(editor) || editor->nLastSelStart != editor->nLastSelEnd)
497   {
498     int x, y, height;
499     int x2, y2, height2;
500     int last_x, last_y, last_height;
501     int last_x2, last_y2, last_height2;
502     RECT rc;
503     ME_Cursor tmp;
504   
505     ME_GetCursorCoordinates(editor, &editor->pCursors[1], &x, &y, &height);
506     ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x2, &y2, &height2);
507     ME_RunOfsFromCharOfs(editor, editor->nLastSelStart, &tmp.pRun, &tmp.nOffset);
508     ME_GetCursorCoordinates(editor, &tmp, &last_x, &last_y, &last_height);
509     ME_RunOfsFromCharOfs(editor, editor->nLastSelEnd, &tmp.pRun, &tmp.nOffset);
510     ME_GetCursorCoordinates(editor, &tmp, &last_x2, &last_y2, &last_height2);
511     {
512       rc.left = 0;
513
514       rc.top = min(min(y, last_y), min(y2, last_y2));
515       rc.right = editor->rcFormat.right;
516       rc.bottom = max(max(y + height, last_y + last_height),
517                       max(y2 + height2, last_y2 + last_height2));
518       InvalidateRect(editor->hWnd, &rc, FALSE);
519     }
520   }
521   ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
522 }
523
524
525 void
526 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
527 {
528   editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
529 }
530
531
532 BOOL
533 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
534 {
535   /* TODO: Zoom images and objects */
536
537   if (numerator != 0)
538   {
539     if (denominator == 0)
540       return FALSE;
541     if (1.0 / 64.0 > (float)numerator / (float)denominator
542         || (float)numerator / (float)denominator > 64.0)
543       return FALSE;
544   }
545   
546   editor->nZoomNumerator = numerator;
547   editor->nZoomDenominator = denominator;
548   
549   ME_RewrapRepaint(editor);
550   return TRUE;
551 }