riched20: Add IDataObject implementation and use it for cut/copy.
[wine/hacks.git] / dlls / riched20 / paint.c
blob42166ede31cd34723042b07531dcf396df56d2ed
1 /*
2 * RichEdit - painting functions
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2005 by Phil Krylov
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.
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.
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
22 #include "editor.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 void ME_PaintContent(ME_TextEditor *editor, HDC hDC, BOOL bOnlyNew, RECT *rcUpdate) {
27 ME_DisplayItem *item;
28 ME_Context c;
29 int yoffset;
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))
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)
50 ME_DrawParagraph(&c, item);
51 if (!rcUpdate || (rcUpdate->top<=c.pt.y && rcUpdate->bottom>=ye))
52 item->member.para.nFlags &= ~MEPF_REPAINT;
55 c.pt.y = ye;
56 item = item->member.para.next_para;
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;
63 if (bOnlyNew)
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;
72 if (rcUpdate && ys!=ye)
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;
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);
88 if (ys == c.pt.y) /* don't overwrite the top bar */
89 ys++;
91 if (editor->nTotalLength != editor->nLastTotalLength)
92 ME_SendRequestResize(editor, FALSE);
93 editor->nLastTotalLength = editor->nTotalLength;
94 ME_DestroyContext(&c);
97 void ME_Repaint(ME_TextEditor *editor)
99 ME_Cursor *pCursor = &editor->pCursors[0];
101 if (ME_WrapMarkedParagraphs(editor)) {
102 ME_UpdateScrollBar(editor);
104 if (editor->bRedraw)
106 ME_EnsureVisible(editor, pCursor->pRun);
107 UpdateWindow(editor->hWnd);
111 void ME_UpdateRepaint(ME_TextEditor *editor)
114 InvalidateRect(editor->hWnd, NULL, TRUE);
116 ME_SendOldNotify(editor, EN_CHANGE);
117 ME_Repaint(editor);
118 ME_SendOldNotify(editor, EN_UPDATE);
119 ME_SendSelChange(editor);
123 void
124 ME_RewrapRepaint(ME_TextEditor *editor)
126 ME_MarkAllForWrapping(editor);
127 ME_WrapMarkedParagraphs(editor);
128 ME_UpdateScrollBar(editor);
129 ME_Repaint(editor);
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;
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;
152 if (yTwipsOffset)
154 int numerator = 1;
155 int denominator = 1;
157 if (c->editor->nZoomNumerator)
159 numerator = c->editor->nZoomNumerator;
160 denominator = c->editor->nZoomDenominator;
162 yOffset = yTwipsOffset * GetDeviceCaps(hDC, LOGPIXELSY) * numerator / denominator / 1440;
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;
170 if (nSelFrom < nChars && nSelTo >= 0 && nSelFrom<nSelTo)
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 PatBlt(hDC, x, ymin, sz.cx, cy, DSTINVERT);
180 SetTextColor(hDC, rgbOld);
181 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
184 static void ME_DebugWrite(HDC hDC, POINT *pt, WCHAR *szText) {
185 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
186 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
187 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
188 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
189 SelectObject(hDC, hFont);
190 SetTextAlign(hDC, align);
191 SetTextColor(hDC, color);
194 void ME_DrawGraphics(ME_Context *c, int x, int y, ME_Run *run,
195 ME_Paragraph *para, BOOL selected) {
196 SIZE sz;
197 int xs, ys, xe, ye, h, ym, width, eyes;
198 ME_GetGraphicsSize(c->editor, run, &sz);
199 xs = run->pt.x;
200 ys = y-sz.cy;
201 xe = xs+sz.cx;
202 ye = y;
203 h = ye-ys;
204 ym = ys+h/4;
205 width = sz.cx;
206 eyes = width/8;
207 /* draw a smiling face :) */
208 Ellipse(c->hDC, xs, ys, xe, ye);
209 Ellipse(c->hDC, xs+width/8, ym, x+width/8+eyes, ym+eyes);
210 Ellipse(c->hDC, xs+7*width/8-eyes, ym, xs+7*width/8, ym+eyes);
211 MoveToEx(c->hDC, xs+width/8, ys+3*h/4-eyes, NULL);
212 LineTo(c->hDC, xs+width/8, ys+3*h/4);
213 LineTo(c->hDC, xs+7*width/8, ys+3*h/4);
214 LineTo(c->hDC, xs+7*width/8, ys+3*h/4-eyes);
215 if (selected)
217 /* descent is usually (always?) 0 for graphics */
218 PatBlt(c->hDC, x, y-run->nAscent, sz.cx, run->nAscent+run->nDescent, DSTINVERT);
222 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para) {
223 ME_Run *run = &rundi->member.run;
224 ME_DisplayItem *start = ME_FindItemBack(rundi, diStartRow);
225 int runofs = run->nCharOfs+para->nCharOfs;
226 int nSelFrom, nSelTo;
227 const WCHAR wszSpace[] = {' ', 0};
229 if (run->nFlags & MERF_HIDDEN)
230 return;
232 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
234 /* Draw selected end-of-paragraph mark */
235 if (run->nFlags & MERF_ENDPARA && runofs >= nSelFrom && runofs < nSelTo)
236 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, NULL, 0, 1,
237 c->pt.y + start->member.row.nYPos,
238 start->member.row.nHeight);
240 /* you can always comment it out if you need visible paragraph marks */
241 if (run->nFlags & (MERF_ENDPARA | MERF_TAB | MERF_CELL))
242 return;
244 if (run->nFlags & MERF_GRAPHICS)
245 ME_DrawGraphics(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
246 else
247 ME_DrawTextWithStyle(c, x, y,
248 run->strText->szData, ME_StrVLen(run->strText), run->style, NULL,
249 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
252 COLORREF ME_GetBackColor(ME_TextEditor *editor)
254 /* Looks like I was seriously confused
255 return GetSysColor((GetWindowLong(editor->hWnd, GWL_STYLE) & ES_READONLY) ? COLOR_3DFACE: COLOR_WINDOW);
257 if (editor->rgbBackColor == -1)
258 return GetSysColor(COLOR_WINDOW);
259 else
260 return editor->rgbBackColor;
263 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
264 int align = SetTextAlign(c->hDC, TA_BASELINE);
265 ME_DisplayItem *p;
266 ME_Run *run;
267 ME_Paragraph *para = NULL;
268 RECT rc, rcPara;
269 int y = c->pt.y;
270 int height = 0, baseline = 0, no=0, pno = 0;
271 int xs, xe;
272 int visible = 0;
273 int nMargWidth = 0;
275 c->pt.x = c->rcView.left;
276 rcPara.left = c->rcView.left;
277 rcPara.right = c->rcView.right;
278 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
279 switch(p->type) {
280 case diParagraph:
281 para = &p->member.para;
282 break;
283 case diStartRow:
284 assert(para);
285 nMargWidth = (pno==0?para->nFirstMargin:para->nLeftMargin);
286 xs = c->rcView.left+nMargWidth;
287 xe = c->rcView.right-para->nRightMargin;
288 y += height;
289 rcPara.top = y;
290 rcPara.bottom = y+p->member.row.nHeight;
291 visible = RectVisible(c->hDC, &rcPara);
292 if (visible) {
293 HBRUSH hbr;
294 hbr = CreateSolidBrush(ME_GetBackColor(c->editor));
295 /* left margin */
296 rc.left = c->rcView.left;
297 rc.right = c->rcView.left+nMargWidth;
298 rc.top = y;
299 rc.bottom = y+p->member.row.nHeight;
300 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
301 /* right margin */
302 rc.left = xe;
303 rc.right = c->rcView.right;
304 FillRect(c->hDC, &rc, hbr/* c->hbrMargin */);
305 rc.left = c->rcView.left+nMargWidth;
306 rc.right = xe;
307 FillRect(c->hDC, &rc, hbr);
308 DeleteObject(hbr);
310 if (me_debug)
312 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
313 WCHAR buf[128];
314 POINT pt = c->pt;
315 wsprintfW(buf, wszRowDebug, no);
316 pt.y = 12+y;
317 ME_DebugWrite(c->hDC, &pt, buf);
320 height = p->member.row.nHeight;
321 baseline = p->member.row.nBaseline;
322 pno++;
323 break;
324 case diRun:
325 assert(para);
326 run = &p->member.run;
327 if (visible && me_debug) {
328 rc.left = c->rcView.left+run->pt.x;
329 rc.right = c->rcView.left+run->pt.x+run->nWidth;
330 rc.top = c->pt.y+run->pt.y;
331 rc.bottom = c->pt.y+run->pt.y+height;
332 TRACE("rc = (%ld, %ld, %ld, %ld)\n", rc.left, rc.top, rc.right, rc.bottom);
333 if (run->nFlags & MERF_SKIPPED)
334 DrawFocusRect(c->hDC, &rc);
335 else
336 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
338 if (visible)
339 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, &paragraph->member.para);
340 if (me_debug)
342 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
343 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
344 WCHAR buf[2560];
345 POINT pt;
346 pt.x = run->pt.x;
347 pt.y = c->pt.y + run->pt.y;
348 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
349 ME_DebugWrite(c->hDC, &pt, buf);
351 /* c->pt.x += p->member.run.nWidth; */
352 break;
353 default:
354 break;
356 no++;
358 SetTextAlign(c->hDC, align);
361 void ME_Scroll(ME_TextEditor *editor, int cx, int cy)
363 SCROLLINFO si;
364 HWND hWnd = editor->hWnd;
366 si.cbSize = sizeof(SCROLLINFO);
367 si.fMask = SIF_POS;
368 GetScrollInfo(hWnd, SB_VERT, &si);
369 si.nPos = editor->nScrollPosY -= cy;
370 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
371 if (editor->bRedraw)
373 if (abs(cy) > editor->sizeWindow.cy)
374 InvalidateRect(editor->hWnd, NULL, TRUE);
375 else
376 ScrollWindowEx(hWnd, cx, cy, NULL, NULL, NULL, NULL, SW_ERASE|SW_INVALIDATE);
380 void ME_UpdateScrollBar(ME_TextEditor *editor)
382 HWND hWnd = editor->hWnd;
383 SCROLLINFO si;
384 int nOldLen = editor->nTotalLength;
385 BOOL bScrollY = (editor->nTotalLength > editor->sizeWindow.cy);
386 BOOL bUpdateScrollBars;
387 si.cbSize = sizeof(si);
388 si.fMask = SIF_POS | SIF_RANGE;
389 GetScrollInfo(hWnd, SB_VERT, &si);
390 bUpdateScrollBars = (bScrollY || editor->bScrollY)&& ((si.nMax != nOldLen) || (si.nPage != editor->sizeWindow.cy));
392 if (bScrollY != editor->bScrollY)
394 si.fMask = SIF_RANGE | SIF_PAGE;
395 si.nMin = 0;
396 si.nPage = editor->sizeWindow.cy;
397 if (bScrollY) {
398 si.nMax = editor->nTotalLength;
399 } else {
400 si.nMax = 0;
402 SetScrollInfo(hWnd, SB_VERT, &si, FALSE);
403 ME_MarkAllForWrapping(editor);
404 editor->bScrollY = bScrollY;
405 ME_WrapMarkedParagraphs(editor);
406 bUpdateScrollBars = TRUE;
408 if (bUpdateScrollBars) {
409 int nScroll = 0;
410 si.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
411 if (editor->nTotalLength > editor->sizeWindow.cy) {
412 si.nMax = editor->nTotalLength;
413 si.nPage = editor->sizeWindow.cy;
414 if (si.nPos > si.nMax-si.nPage) {
415 nScroll = (si.nMax-si.nPage)-si.nPos;
416 si.nPos = si.nMax-si.nPage;
419 else {
420 si.nMax = 0;
421 si.nPage = 0;
422 si.nPos = 0;
424 TRACE("min=%d max=%d page=%d pos=%d shift=%d\n", si.nMin, si.nMax, si.nPage, si.nPos, nScroll);
425 editor->nScrollPosY = si.nPos;
426 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
427 if (nScroll)
428 ScrollWindow(hWnd, 0, -nScroll, NULL, NULL);
432 int ME_GetYScrollPos(ME_TextEditor *editor)
434 return editor->nScrollPosY;
437 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
439 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
440 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
441 int y, yrel, yheight, yold;
442 HWND hWnd = editor->hWnd;
444 assert(pRow);
445 assert(pPara);
447 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
448 yheight = pRow->member.row.nHeight;
449 yold = ME_GetYScrollPos(editor);
450 yrel = y - yold;
451 if (yrel < 0) {
452 editor->nScrollPosY = y;
453 SetScrollPos(hWnd, SB_VERT, y, TRUE);
454 if (editor->bRedraw)
456 ScrollWindow(hWnd, 0, -yrel, NULL, NULL);
457 UpdateWindow(hWnd);
459 } else if (yrel + yheight > editor->sizeWindow.cy) {
460 int newy = y+yheight-editor->sizeWindow.cy;
461 editor->nScrollPosY = newy;
462 SetScrollPos(hWnd, SB_VERT, newy, TRUE);
463 if (editor->bRedraw)
465 ScrollWindow(hWnd, 0, -(newy-yold), NULL, NULL);
466 UpdateWindow(hWnd);
472 void
473 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
475 RECT rc;
476 int x, y, height;
477 ME_Cursor tmp;
479 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
480 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
482 rc.left = 0;
483 rc.top = y;
484 rc.bottom = y + height;
485 rc.right = editor->rcFormat.right;
486 InvalidateRect(editor->hWnd, &rc, FALSE);
490 void
491 ME_InvalidateSelection(ME_TextEditor *editor)
493 if (ME_IsSelection(editor) || editor->nLastSelStart != editor->nLastSelEnd)
495 int x, y, height;
496 int x2, y2, height2;
497 int last_x, last_y, last_height;
498 int last_x2, last_y2, last_height2;
499 RECT rc;
500 ME_Cursor tmp;
502 ME_GetCursorCoordinates(editor, &editor->pCursors[1], &x, &y, &height);
503 ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x2, &y2, &height2);
504 ME_RunOfsFromCharOfs(editor, editor->nLastSelStart, &tmp.pRun, &tmp.nOffset);
505 ME_GetCursorCoordinates(editor, &tmp, &last_x, &last_y, &last_height);
506 ME_RunOfsFromCharOfs(editor, editor->nLastSelEnd, &tmp.pRun, &tmp.nOffset);
507 ME_GetCursorCoordinates(editor, &tmp, &last_x2, &last_y2, &last_height2);
509 rc.left = 0;
511 rc.top = min(min(y, last_y), min(y2, last_y2));
512 rc.right = editor->rcFormat.right;
513 rc.bottom = max(max(y + height, last_y + last_height),
514 max(y2 + height2, last_y2 + last_height2));
515 InvalidateRect(editor->hWnd, &rc, FALSE);
518 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
522 void
523 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
525 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
529 BOOL
530 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
532 /* TODO: Zoom images and objects */
534 if (numerator != 0)
536 if (denominator == 0)
537 return FALSE;
538 if (1.0 / 64.0 > (float)numerator / (float)denominator
539 || (float)numerator / (float)denominator > 64.0)
540 return FALSE;
543 editor->nZoomNumerator = numerator;
544 editor->nZoomDenominator = denominator;
546 ME_RewrapRepaint(editor);
547 return TRUE;