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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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, const 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 && ye>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 rc.left = xs;
82 rc.top = ys;
83 rc.right = xe;
84 rc.bottom = ye;
85 FillRect(hDC, &rc, c.editor->hbrBackground);
86 }
87 }
88 if (editor->nTotalLength != editor->nLastTotalLength)
89 ME_SendRequestResize(editor, FALSE);
90 editor->nLastTotalLength = editor->nTotalLength;
91 ME_DestroyContext(&c, NULL);
92 }
93
94 void ME_Repaint(ME_TextEditor *editor)
95 {
96 if (ME_WrapMarkedParagraphs(editor))
97 {
98 ME_UpdateScrollBar(editor);
99 FIXME("ME_Repaint had to call ME_WrapMarkedParagraphs\n");
100 }
101 if (!editor->bEmulateVersion10 || (editor->nEventMask & ENM_UPDATE))
102 ME_SendOldNotify(editor, EN_UPDATE);
103 UpdateWindow(editor->hWnd);
104 }
105
106 void ME_UpdateRepaint(ME_TextEditor *editor)
107 {
108 /* Should be called whenever the contents of the control have changed */
109 ME_Cursor *pCursor;
110 BOOL wrappedParagraphs;
111
112 wrappedParagraphs = ME_WrapMarkedParagraphs(editor);
113 if (!editor->bRedraw) return;
114 if (wrappedParagraphs)
115 ME_UpdateScrollBar(editor);
116
117 /* Ensure that the cursor is visible */
118 pCursor = &editor->pCursors[0];
119 ME_EnsureVisible(editor, pCursor->pRun);
120
121 /* send EN_CHANGE if the event mask asks for it */
122 if(editor->nEventMask & ENM_CHANGE)
123 {
124 editor->nEventMask &= ~ENM_CHANGE;
125 ME_SendOldNotify(editor, EN_CHANGE);
126 editor->nEventMask |= ENM_CHANGE;
127 }
128 ME_Repaint(editor);
129 ME_SendSelChange(editor);
130 }
131
132 void
133 ME_RewrapRepaint(ME_TextEditor *editor)
134 {
135 /* RewrapRepaint should be called whenever the control has changed in
136 * looks, but not content. Like resizing. */
137
138 ME_MarkAllForWrapping(editor);
139 if (editor->bRedraw)
140 {
141 ME_WrapMarkedParagraphs(editor);
142 ME_UpdateScrollBar(editor);
143 ME_Repaint(editor);
144 }
145 }
146
147 int ME_twips2pointsX(ME_Context *c, int x)
148 {
149 if (c->editor->nZoomNumerator == 0)
150 return x * c->dpi.cx / 1440;
151 else
152 return x * c->dpi.cx * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
153 }
154
155 int ME_twips2pointsY(ME_Context *c, int y)
156 {
157 if (c->editor->nZoomNumerator == 0)
158 return y * c->dpi.cy / 1440;
159 else
160 return y * c->dpi.cy * c->editor->nZoomNumerator / 1440 / c->editor->nZoomDenominator;
161 }
162
163 static void ME_HighlightSpace(ME_Context *c, int x, int y, LPCWSTR szText,
164 int nChars, ME_Style *s, int width,
165 int nSelFrom, int nSelTo, int ymin, int cy)
166 {
167 HDC hDC = c->hDC;
168 HGDIOBJ hOldFont = NULL;
169 SIZE sz;
170 int selWidth;
171 /* Only highlight if there is a selection in the run and when
172 * EM_HIDESELECTION is not being used to hide the selection. */
173 if (nSelFrom >= nChars || nSelTo < 0 || nSelFrom >= nSelTo
174 || c->editor->bHideSelection)
175 return;
176 hOldFont = ME_SelectStyleFont(c, s);
177 if (width <= 0)
178 {
179 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
180 width = sz.cx;
181 }
182 if (nSelFrom < 0) nSelFrom = 0;
183 if (nSelTo > nChars) nSelTo = nChars;
184 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
185 x += sz.cx;
186 if (nSelTo != nChars)
187 {
188 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
189 selWidth = sz.cx;
190 } else {
191 selWidth = width - sz.cx;
192 }
193 ME_UnselectStyleFont(c, s, hOldFont);
194
195 if (c->editor->bEmulateVersion10)
196 PatBlt(hDC, x, ymin, selWidth, cy, DSTINVERT);
197 else
198 {
199 RECT rect;
200 HBRUSH hBrush;
201 rect.left = x;
202 rect.top = ymin;
203 rect.right = x + selWidth;
204 rect.bottom = ymin + cy;
205 hBrush = CreateSolidBrush(GetSysColor(COLOR_HIGHLIGHT));
206 FillRect(hDC, &rect, hBrush);
207 DeleteObject(hBrush);
208 }
209 }
210
211 static void ME_DrawTextWithStyle(ME_Context *c, int x, int y, LPCWSTR szText,
212 int nChars, ME_Style *s, int width,
213 int nSelFrom, int nSelTo, int ymin, int cy)
214 {
215 HDC hDC = c->hDC;
216 HGDIOBJ hOldFont;
217 COLORREF rgbOld;
218 int yOffset = 0, yTwipsOffset = 0;
219 SIZE sz;
220 COLORREF rgb;
221 HPEN hPen = NULL, hOldPen = NULL;
222 BOOL bHighlightedText = (nSelFrom < nChars && nSelTo >= 0
223 && nSelFrom < nSelTo && !c->editor->bHideSelection);
224 int xSelStart = x, xSelEnd = x;
225 int *lpDx = NULL;
226 /* lpDx is only needed for tabs to make sure the underline done automatically
227 * by the font extends to the end of the tab. Tabs are always stored as
228 * a single character run, so we can handle this case separately, since
229 * otherwise lpDx would need to specify the lengths of each character. */
230 if (width && nChars == 1)
231 lpDx = &width; /* Make sure underline for tab extends across tab space */
232
233 hOldFont = ME_SelectStyleFont(c, s);
234 if ((s->fmt.dwMask & s->fmt.dwEffects) & CFM_OFFSET) {
235 yTwipsOffset = s->fmt.yOffset;
236 }
237 if ((s->fmt.dwMask & s->fmt.dwEffects) & (CFM_SUPERSCRIPT | CFM_SUBSCRIPT)) {
238 if (s->fmt.dwEffects & CFE_SUPERSCRIPT) yTwipsOffset = s->fmt.yHeight/3;
239 if (s->fmt.dwEffects & CFE_SUBSCRIPT) yTwipsOffset = -s->fmt.yHeight/12;
240 }
241 if (yTwipsOffset)
242 yOffset = ME_twips2pointsY(c, yTwipsOffset);
243
244 if ((s->fmt.dwMask & CFM_LINK) && (s->fmt.dwEffects & CFE_LINK))
245 rgb = RGB(0,0,255);
246 else if ((s->fmt.dwMask & CFM_COLOR) && (s->fmt.dwEffects & CFE_AUTOCOLOR))
247 rgb = GetSysColor(COLOR_WINDOWTEXT);
248 else
249 rgb = s->fmt.crTextColor;
250
251 /* Determine the area that is selected in the run. */
252 GetTextExtentPoint32W(hDC, szText, nChars, &sz);
253 /* Treat width as an optional parameter. We can get the width from the
254 * text extent of the string if it isn't specified. */
255 if (!width) width = sz.cx;
256 if (bHighlightedText)
257 {
258 if (nSelFrom <= 0)
259 {
260 nSelFrom = 0;
261 }
262 else
263 {
264 GetTextExtentPoint32W(hDC, szText, nSelFrom, &sz);
265 xSelStart = x + sz.cx;
266 }
267 if (nSelTo >= nChars)
268 {
269 nSelTo = nChars;
270 xSelEnd = x + width;
271 }
272 else
273 {
274 GetTextExtentPoint32W(hDC, szText+nSelFrom, nSelTo-nSelFrom, &sz);
275 xSelEnd = xSelStart + sz.cx;
276 }
277 }
278
279 /* Choose the pen type for underlining the text. */
280 if (s->fmt.dwMask & CFM_UNDERLINETYPE)
281 {
282 switch (s->fmt.bUnderlineType)
283 {
284 case CFU_UNDERLINE:
285 case CFU_UNDERLINEWORD: /* native seems to map it to simple underline (MSDN) */
286 case CFU_UNDERLINEDOUBLE: /* native seems to map it to simple underline (MSDN) */
287 hPen = CreatePen(PS_SOLID, 1, rgb);
288 break;
289 case CFU_UNDERLINEDOTTED:
290 hPen = CreatePen(PS_DOT, 1, rgb);
291 break;
292 default:
293 WINE_FIXME("Unknown underline type (%u)\n", s->fmt.bUnderlineType);
294 /* fall through */
295 case CFU_CF1UNDERLINE: /* this type is supported in the font, do nothing */
296 case CFU_UNDERLINENONE:
297 hPen = NULL;
298 break;
299 }
300 if (hPen)
301 {
302 hOldPen = SelectObject(hDC, hPen);
303 }
304 }
305
306 rgbOld = SetTextColor(hDC, rgb);
307 if (bHighlightedText && !c->editor->bEmulateVersion10)
308 {
309 COLORREF rgbBackOld;
310 RECT dim;
311 /* FIXME: should use textmetrics info for Descent info */
312 if (hPen)
313 MoveToEx(hDC, x, y - yOffset + 1, NULL);
314 if (xSelStart > x)
315 {
316 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nSelFrom, NULL);
317 if (hPen)
318 LineTo(hDC, xSelStart, y - yOffset + 1);
319 }
320 dim.top = ymin;
321 dim.bottom = ymin + cy;
322 dim.left = xSelStart;
323 dim.right = xSelEnd;
324 SetTextColor(hDC, GetSysColor(COLOR_HIGHLIGHTTEXT));
325 rgbBackOld = SetBkColor(hDC, GetSysColor(COLOR_HIGHLIGHT));
326 ExtTextOutW(hDC, xSelStart, y-yOffset, ETO_OPAQUE, &dim,
327 szText+nSelFrom, nSelTo-nSelFrom, lpDx);
328 if (hPen)
329 LineTo(hDC, xSelEnd, y - yOffset + 1);
330 SetBkColor(hDC, rgbBackOld);
331 if (xSelEnd < x + width)
332 {
333 SetTextColor(hDC, rgb);
334 ExtTextOutW(hDC, xSelEnd, y-yOffset, 0, NULL, szText+nSelTo,
335 nChars-nSelTo, NULL);
336 if (hPen)
337 LineTo(hDC, x + width, y - yOffset + 1);
338 }
339 }
340 else
341 {
342 ExtTextOutW(hDC, x, y-yOffset, 0, NULL, szText, nChars, lpDx);
343
344 /* FIXME: should use textmetrics info for Descent info */
345 if (hPen)
346 {
347 MoveToEx(hDC, x, y - yOffset + 1, NULL);
348 LineTo(hDC, x + width, y - yOffset + 1);
349 }
350
351 if (bHighlightedText) /* v1.0 inverts the selection */
352 {
353 PatBlt(hDC, xSelStart, ymin, xSelEnd-xSelStart, cy, DSTINVERT);
354 }
355 }
356
357 if (hPen)
358 {
359 SelectObject(hDC, hOldPen);
360 DeleteObject(hPen);
361 }
362 SetTextColor(hDC, rgbOld);
363 ME_UnselectStyleFont(c, s, hOldFont);
364 }
365
366 static void ME_DebugWrite(HDC hDC, const POINT *pt, LPCWSTR szText) {
367 int align = SetTextAlign(hDC, TA_LEFT|TA_TOP);
368 HGDIOBJ hFont = SelectObject(hDC, GetStockObject(DEFAULT_GUI_FONT));
369 COLORREF color = SetTextColor(hDC, RGB(128,128,128));
370 TextOutW(hDC, pt->x, pt->y, szText, lstrlenW(szText));
371 SelectObject(hDC, hFont);
372 SetTextAlign(hDC, align);
373 SetTextColor(hDC, color);
374 }
375
376 static void ME_DrawRun(ME_Context *c, int x, int y, ME_DisplayItem *rundi, ME_Paragraph *para)
377 {
378 ME_Run *run = &rundi->member.run;
379 ME_DisplayItem *start;
380 int runofs = run->nCharOfs+para->nCharOfs;
381 int nSelFrom, nSelTo;
382 const WCHAR wszSpace[] = {' ', 0};
383
384 if (run->nFlags & MERF_HIDDEN)
385 return;
386
387 start = ME_FindItemBack(rundi, diStartRow);
388 ME_GetSelection(c->editor, &nSelFrom, &nSelTo);
389
390 /* Draw selected end-of-paragraph mark */
391 /* you can always comment it out if you need visible paragraph marks */
392 if (run->nFlags & MERF_ENDPARA)
393 {
394 if (runofs >= nSelFrom && runofs < nSelTo)
395 {
396 ME_HighlightSpace(c, x, y, wszSpace, 1, run->style, 0, 0, 1,
397 c->pt.y + start->member.row.nYPos,
398 start->member.row.nHeight);
399 }
400 return;
401 }
402
403 if (run->nFlags & (MERF_TAB | MERF_CELL))
404 {
405 /* wszSpace is used instead of the tab character because otherwise
406 * an unwanted symbol can be inserted instead. */
407 ME_DrawTextWithStyle(c, x, y, wszSpace, 1, run->style, run->nWidth,
408 nSelFrom-runofs,nSelTo-runofs,
409 c->pt.y + start->member.row.nYPos,
410 start->member.row.nHeight);
411 return;
412 }
413
414 if (run->nFlags & MERF_GRAPHICS)
415 ME_DrawOLE(c, x, y, run, para, (runofs >= nSelFrom) && (runofs < nSelTo));
416 else
417 {
418 if (c->editor->cPasswordMask)
419 {
420 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
421 ME_DrawTextWithStyle(c, x, y,
422 szMasked->szData, ME_StrVLen(szMasked), run->style, run->nWidth,
423 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
424 ME_DestroyString(szMasked);
425 }
426 else
427 ME_DrawTextWithStyle(c, x, y,
428 run->strText->szData, ME_StrVLen(run->strText), run->style, run->nWidth,
429 nSelFrom-runofs,nSelTo-runofs, c->pt.y+start->member.row.nYPos, start->member.row.nHeight);
430 }
431 }
432
433 static const struct {unsigned width_num : 4, width_den : 4, pen_style : 4, dble : 1;} border_details[] = {
434 /* none */ {0, 1, PS_SOLID, FALSE},
435 /* 3/4 */ {3, 4, PS_SOLID, FALSE},
436 /* 1 1/2 */ {3, 2, PS_SOLID, FALSE},
437 /* 2 1/4 */ {9, 4, PS_SOLID, FALSE},
438 /* 3 */ {3, 1, PS_SOLID, FALSE},
439 /* 4 1/2 */ {9, 2, PS_SOLID, FALSE},
440 /* 6 */ {6, 1, PS_SOLID, FALSE},
441 /* 3/4 double */ {3, 4, PS_SOLID, TRUE},
442 /* 1 1/2 double */ {3, 2, PS_SOLID, TRUE},
443 /* 2 1/4 double */ {9, 4, PS_SOLID, TRUE},
444 /* 3/4 gray */ {3, 4, PS_DOT /* FIXME */, FALSE},
445 /* 1 1/2 dashed */ {3, 2, PS_DASH, FALSE},
446 };
447
448 static const COLORREF pen_colors[16] = {
449 /* Black */ RGB(0x00, 0x00, 0x00), /* Blue */ RGB(0x00, 0x00, 0xFF),
450 /* Cyan */ RGB(0x00, 0xFF, 0xFF), /* Green */ RGB(0x00, 0xFF, 0x00),
451 /* Magenta */ RGB(0xFF, 0x00, 0xFF), /* Red */ RGB(0xFF, 0x00, 0x00),
452 /* Yellow */ RGB(0xFF, 0xFF, 0x00), /* White */ RGB(0xFF, 0xFF, 0xFF),
453 /* Dark blue */ RGB(0x00, 0x00, 0x80), /* Dark cyan */ RGB(0x00, 0x80, 0x80),
454 /* Dark green */ RGB(0x00, 0x80, 0x80), /* Dark magenta */ RGB(0x80, 0x00, 0x80),
455 /* Dark red */ RGB(0x80, 0x00, 0x00), /* Dark yellow */ RGB(0x80, 0x80, 0x00),
456 /* Dark gray */ RGB(0x80, 0x80, 0x80), /* Light gray */ RGB(0xc0, 0xc0, 0xc0),
457 };
458
459 static int ME_GetBorderPenWidth(ME_TextEditor* editor, int idx)
460 {
461 int width;
462
463 if (editor->nZoomNumerator == 0)
464 {
465 width = border_details[idx].width_num + border_details[idx].width_den / 2;
466 width /= border_details[idx].width_den;
467 }
468 else
469 {
470 width = border_details[idx].width_num * editor->nZoomNumerator;
471 width += border_details[idx].width_den * editor->nZoomNumerator / 2;
472 width /= border_details[idx].width_den * editor->nZoomDenominator;
473 }
474 return width;
475 }
476
477 int ME_GetParaBorderWidth(ME_TextEditor* editor, int flags)
478 {
479 int idx = (flags >> 8) & 0xF;
480 int width;
481
482 if (idx >= sizeof(border_details) / sizeof(border_details[0]))
483 {
484 FIXME("Unsupported border value %d\n", idx);
485 return 0;
486 }
487 width = ME_GetBorderPenWidth(editor, idx);
488 if (border_details[idx].dble) width = width * 2 + 1;
489 return width;
490 }
491
492 int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
493 {
494 int sp = 0, ls = 0;
495 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
496
497 /* FIXME: how to compute simply the line space in ls ??? */
498 /* FIXME: does line spacing include the line itself ??? */
499 switch (para->pFmt->bLineSpacingRule)
500 {
501 case 0: sp = ls; break;
502 case 1: sp = (3 * ls) / 2; break;
503 case 2: sp = 2 * ls; break;
504 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
505 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
506 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
507 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
508 }
509 if (c->editor->nZoomNumerator == 0)
510 return sp;
511 else
512 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
513 }
514
515 static void ME_DrawParaDecoration(ME_Context* c, ME_Paragraph* para, int y, RECT* bounds)
516 {
517 int idx, border_width, top_border, bottom_border;
518 RECT rc;
519
520 SetRectEmpty(bounds);
521 if (!(para->pFmt->dwMask & (PFM_BORDER | PFM_SPACEBEFORE | PFM_SPACEAFTER))) return;
522
523 border_width = top_border = bottom_border = 0;
524 idx = (para->pFmt->wBorders >> 8) & 0xF;
525 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF))
526 {
527 if (para->pFmt->wBorders & 0x00B0)
528 FIXME("Unsupported border flags %x\n", para->pFmt->wBorders);
529 border_width = ME_GetParaBorderWidth(c->editor, para->pFmt->wBorders);
530 if (para->pFmt->wBorders & 4) top_border = border_width;
531 if (para->pFmt->wBorders & 8) bottom_border = border_width;
532 }
533
534 if (para->pFmt->dwMask & PFM_SPACEBEFORE)
535 {
536 rc.left = c->rcView.left;
537 rc.right = c->rcView.right;
538 rc.top = y;
539 bounds->top = ME_twips2pointsY(c, para->pFmt->dySpaceBefore);
540 rc.bottom = y + bounds->top + top_border;
541 FillRect(c->hDC, &rc, c->editor->hbrBackground);
542 }
543
544 if (para->pFmt->dwMask & PFM_SPACEAFTER)
545 {
546 rc.left = c->rcView.left;
547 rc.right = c->rcView.right;
548 rc.bottom = y + para->nHeight;
549 bounds->bottom = ME_twips2pointsY(c, para->pFmt->dySpaceAfter);
550 rc.top = rc.bottom - bounds->bottom - bottom_border;
551 FillRect(c->hDC, &rc, c->editor->hbrBackground);
552 }
553
554 if ((para->pFmt->dwMask & PFM_BORDER) && idx != 0 && (para->pFmt->wBorders & 0xF)) {
555 int pen_width;
556 COLORREF pencr;
557 HPEN pen = NULL, oldpen = NULL;
558 POINT pt;
559
560 if (para->pFmt->wBorders & 64) /* autocolor */
561 pencr = GetSysColor(COLOR_WINDOWTEXT);
562 else
563 pencr = pen_colors[(para->pFmt->wBorders >> 12) & 0xF];
564
565 pen_width = ME_GetBorderPenWidth(c->editor, idx);
566 pen = CreatePen(border_details[idx].pen_style, pen_width, pencr);
567 oldpen = SelectObject(c->hDC, pen);
568 MoveToEx(c->hDC, 0, 0, &pt);
569
570 /* before & after spaces are not included in border */
571
572 /* helper to draw the double lines in case of corner */
573 #define DD(x) ((para->pFmt->wBorders & (x)) ? (pen_width + 1) : 0)
574
575 if (para->pFmt->wBorders & 1)
576 {
577 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
578 LineTo(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom);
579 if (border_details[idx].dble) {
580 rc.left = c->rcView.left + 1;
581 rc.right = rc.left + border_width;
582 rc.top = y + bounds->top;
583 rc.bottom = y + para->nHeight - bounds->bottom;
584 FillRect(c->hDC, &rc, c->editor->hbrBackground);
585 MoveToEx(c->hDC, c->rcView.left + pen_width + 1, y + bounds->top + DD(4), NULL);
586 LineTo(c->hDC, c->rcView.left + pen_width + 1, y + para->nHeight - bounds->bottom - DD(8));
587 }
588 bounds->left += border_width;
589 }
590 if (para->pFmt->wBorders & 2)
591 {
592 MoveToEx(c->hDC, c->rcView.right - 1, y + bounds->top, NULL);
593 LineTo(c->hDC, c->rcView.right - 1, y + para->nHeight - bounds->bottom);
594 if (border_details[idx].dble) {
595 rc.left = c->rcView.right - pen_width - 1;
596 rc.right = c->rcView.right - 1;
597 rc.top = y + bounds->top;
598 rc.bottom = y + para->nHeight - bounds->bottom;
599 FillRect(c->hDC, &rc, c->editor->hbrBackground);
600 MoveToEx(c->hDC, c->rcView.right - 1 - pen_width - 1, y + bounds->top + DD(4), NULL);
601 LineTo(c->hDC, c->rcView.right - 1 - pen_width - 1, y + para->nHeight - bounds->bottom - DD(8));
602 }
603 bounds->right += border_width;
604 }
605 if (para->pFmt->wBorders & 4)
606 {
607 MoveToEx(c->hDC, c->rcView.left, y + bounds->top, NULL);
608 LineTo(c->hDC, c->rcView.right, y + bounds->top);
609 if (border_details[idx].dble) {
610 MoveToEx(c->hDC, c->rcView.left + DD(1), y + bounds->top + pen_width + 1, NULL);
611 LineTo(c->hDC, c->rcView.right - DD(2), y + bounds->top + pen_width + 1);
612 }
613 bounds->top += border_width;
614 }
615 if (para->pFmt->wBorders & 8)
616 {
617 MoveToEx(c->hDC, c->rcView.left, y + para->nHeight - bounds->bottom - 1, NULL);
618 LineTo(c->hDC, c->rcView.right, y + para->nHeight - bounds->bottom - 1);
619 if (border_details[idx].dble) {
620 MoveToEx(c->hDC, c->rcView.left + DD(1), y + para->nHeight - bounds->bottom - 1 - pen_width - 1, NULL);
621 LineTo(c->hDC, c->rcView.right - DD(2), y + para->nHeight - bounds->bottom - 1 - pen_width - 1);
622 }
623 bounds->bottom += border_width;
624 }
625 #undef DD
626
627 MoveToEx(c->hDC, pt.x, pt.y, NULL);
628 SelectObject(c->hDC, oldpen);
629 DeleteObject(pen);
630 }
631 }
632
633 void ME_DrawParagraph(ME_Context *c, ME_DisplayItem *paragraph) {
634 int align = SetTextAlign(c->hDC, TA_BASELINE);
635 ME_DisplayItem *p;
636 ME_Run *run;
637 ME_Paragraph *para = NULL;
638 RECT rc, rcPara, bounds;
639 int y = c->pt.y;
640 int height = 0, baseline = 0, no=0, pno = 0;
641 int xs = 0, xe = 0;
642 BOOL visible = FALSE;
643
644 c->pt.x = c->rcView.left;
645 rcPara.left = c->rcView.left;
646 rcPara.right = c->rcView.right;
647 for (p = paragraph; p!=paragraph->member.para.next_para; p = p->next) {
648 switch(p->type) {
649 case diParagraph:
650 para = &p->member.para;
651 assert(para);
652 pno = 0;
653 xs = c->rcView.left + ME_twips2pointsX(c, para->pFmt->dxStartIndent);
654 xe = c->rcView.right - ME_twips2pointsX(c, para->pFmt->dxRightIndent);
655 ME_DrawParaDecoration(c, para, y, &bounds);
656 y += bounds.top;
657 break;
658 case diStartRow:
659 /* we should have seen a diParagraph before */
660 assert(para);
661 y += height;
662 rcPara.top = y;
663 rcPara.bottom = y+p->member.row.nHeight;
664 visible = RectVisible(c->hDC, &rcPara);
665 if (visible) {
666 /* left margin */
667 rc.left = c->rcView.left + bounds.left;
668 rc.right = xs;
669 rc.top = y;
670 rc.bottom = y+p->member.row.nHeight;
671 FillRect(c->hDC, &rc, c->editor->hbrBackground);
672 /* right margin */
673 rc.left = xe;
674 rc.right = c->rcView.right - bounds.right;
675 FillRect(c->hDC, &rc, c->editor->hbrBackground);
676 rc.left = xs;
677 rc.right = xe;
678 FillRect(c->hDC, &rc, c->editor->hbrBackground);
679 }
680 if (me_debug)
681 {
682 const WCHAR wszRowDebug[] = {'r','o','w','[','%','d',']',0};
683 WCHAR buf[128];
684 POINT pt = c->pt;
685 wsprintfW(buf, wszRowDebug, no);
686 pt.y = 12+y;
687 ME_DebugWrite(c->hDC, &pt, buf);
688 }
689
690 height = p->member.row.nHeight;
691 baseline = p->member.row.nBaseline;
692 if (!pno++)
693 xe += ME_twips2pointsX(c, para->pFmt->dxOffset);
694 break;
695 case diRun:
696 assert(para);
697 run = &p->member.run;
698 if (visible && me_debug) {
699 rc.left = c->rcView.left+run->pt.x;
700 rc.right = c->rcView.left+run->pt.x+run->nWidth;
701 rc.top = c->pt.y+run->pt.y;
702 rc.bottom = c->pt.y+run->pt.y+height;
703 TRACE("rc = (%d, %d, %d, %d)\n", rc.left, rc.top, rc.right, rc.bottom);
704 if (run->nFlags & MERF_SKIPPED)
705 DrawFocusRect(c->hDC, &rc);
706 else
707 FrameRect(c->hDC, &rc, GetSysColorBrush(COLOR_GRAYTEXT));
708 }
709 if (visible)
710 ME_DrawRun(c, run->pt.x, c->pt.y+run->pt.y+baseline, p, ¶graph->member.para);
711 if (me_debug)
712 {
713 /* I'm using %ls, hope wsprintfW is not going to use wrong (4-byte) WCHAR version */
714 const WCHAR wszRunDebug[] = {'[','%','d',':','%','x',']',' ','%','l','s',0};
715 WCHAR buf[2560];
716 POINT pt;
717 pt.x = run->pt.x;
718 pt.y = c->pt.y + run->pt.y;
719 wsprintfW(buf, wszRunDebug, no, p->member.run.nFlags, p->member.run.strText->szData);
720 ME_DebugWrite(c->hDC, &pt, buf);
721 }
722 /* c->pt.x += p->member.run.nWidth; */
723 break;
724 default:
725 break;
726 }
727 no++;
728 }
729 SetTextAlign(c->hDC, align);
730 }
731
732 void ME_ScrollAbs(ME_TextEditor *editor, int absY)
733 {
734 ME_Scroll(editor, absY, 1);
735 }
736
737 void ME_ScrollUp(ME_TextEditor *editor, int cy)
738 {
739 ME_Scroll(editor, cy, 2);
740 }
741
742 void ME_ScrollDown(ME_TextEditor *editor, int cy)
743 {
744 ME_Scroll(editor, cy, 3);
745 }
746
747 void ME_Scroll(ME_TextEditor *editor, int value, int type)
748 {
749 SCROLLINFO si;
750 int nOrigPos, nNewPos, nActualScroll;
751
752 nOrigPos = ME_GetYScrollPos(editor);
753
754 si.cbSize = sizeof(SCROLLINFO);
755 si.fMask = SIF_POS;
756
757 switch (type)
758 {
759 case 1:
760 /*Scroll absolutely*/
761 si.nPos = value;
762 break;
763 case 2:
764 /* Scroll up - towards the beginning of the document */
765 si.nPos = nOrigPos - value;
766 break;
767 case 3:
768 /* Scroll down - towards the end of the document */
769 si.nPos = nOrigPos + value;
770 break;
771 default:
772 FIXME("ME_Scroll called incorrectly\n");
773 si.nPos = 0;
774 }
775
776 nNewPos = SetScrollInfo(editor->hWnd, SB_VERT, &si, editor->bRedraw);
777 nActualScroll = nOrigPos - nNewPos;
778 if (editor->bRedraw)
779 {
780 if (abs(nActualScroll) > editor->sizeWindow.cy)
781 InvalidateRect(editor->hWnd, NULL, TRUE);
782 else
783 ScrollWindowEx(editor->hWnd, 0, nActualScroll, NULL, NULL, NULL, NULL, SW_INVALIDATE);
784 ME_Repaint(editor);
785 }
786
787 editor->vert_si.nMax = 0;
788 ME_UpdateScrollBar(editor);
789 }
790
791
792 void ME_UpdateScrollBar(ME_TextEditor *editor)
793 {
794 /* Note that this is the only function that should ever call SetScrolLInfo
795 * with SIF_PAGE or SIF_RANGE. SetScrollPos and SetScrollRange should never
796 * be used at all. */
797
798 HWND hWnd;
799 SCROLLINFO si;
800 BOOL bScrollBarWasVisible,bScrollBarWillBeVisible;
801
802 if (ME_WrapMarkedParagraphs(editor))
803 FIXME("ME_UpdateScrollBar had to call ME_WrapMarkedParagraphs\n");
804
805 hWnd = editor->hWnd;
806 si.cbSize = sizeof(si);
807 bScrollBarWasVisible = ME_GetYScrollVisible(editor);
808 bScrollBarWillBeVisible = editor->nHeight > editor->sizeWindow.cy;
809
810 si.fMask = SIF_PAGE | SIF_RANGE;
811 if (GetWindowLongW(hWnd, GWL_STYLE) & ES_DISABLENOSCROLL)
812 si.fMask |= SIF_DISABLENOSCROLL;
813 if ((si.fMask & SIF_DISABLENOSCROLL))
814 {
815 bScrollBarWillBeVisible = TRUE;
816 }
817
818 if (bScrollBarWasVisible != bScrollBarWillBeVisible)
819 {
820 ShowScrollBar(hWnd, SB_VERT, bScrollBarWillBeVisible);
821 ME_MarkAllForWrapping(editor);
822 ME_WrapMarkedParagraphs(editor);
823 }
824
825 si.nMin = 0;
826 si.nMax = editor->nTotalLength;
827
828 si.nPage = editor->sizeWindow.cy;
829
830 if (!(si.nMin == editor->vert_si.nMin && si.nMax == editor->vert_si.nMax && si.nPage == editor->vert_si.nPage))
831 {
832 TRACE("min=%d max=%d page=%d\n", si.nMin, si.nMax, si.nPage);
833 editor->vert_si.nMin = si.nMin;
834 editor->vert_si.nMax = si.nMax;
835 editor->vert_si.nPage = si.nPage;
836 if (bScrollBarWillBeVisible)
837 {
838 SetScrollInfo(hWnd, SB_VERT, &si, TRUE);
839 }
840 else
841 {
842 if (bScrollBarWasVisible && !(si.fMask & SIF_DISABLENOSCROLL))
843 ShowScrollBar(hWnd, SB_VERT, FALSE);
844 }
845 }
846 }
847
848 int ME_GetYScrollPos(ME_TextEditor *editor)
849 {
850 SCROLLINFO si;
851 si.cbSize = sizeof(si);
852 si.fMask = SIF_POS;
853 return GetScrollInfo(editor->hWnd, SB_VERT, &si) ? si.nPos : 0;
854 }
855
856 BOOL ME_GetYScrollVisible(ME_TextEditor *editor)
857 { /* Returns true if the scrollbar is visible */
858 return (editor->vert_si.nMax - editor->vert_si.nMin >= max(editor->vert_si.nPage - 1, 0));
859 }
860
861 void ME_EnsureVisible(ME_TextEditor *editor, ME_DisplayItem *pRun)
862 {
863 ME_DisplayItem *pRow = ME_FindItemBack(pRun, diStartRow);
864 ME_DisplayItem *pPara = ME_FindItemBack(pRun, diParagraph);
865 int y, yrel, yheight, yold;
866
867 assert(pRow);
868 assert(pPara);
869
870 y = pPara->member.para.nYPos+pRow->member.row.nYPos;
871 yheight = pRow->member.row.nHeight;
872 yold = ME_GetYScrollPos(editor);
873 yrel = y - yold;
874
875 if (y < yold)
876 ME_ScrollAbs(editor,y);
877 else if (yrel + yheight > editor->sizeWindow.cy)
878 ME_ScrollAbs(editor,y+yheight-editor->sizeWindow.cy);
879 }
880
881
882 void
883 ME_InvalidateFromOfs(ME_TextEditor *editor, int nCharOfs)
884 {
885 RECT rc;
886 int x, y, height;
887 ME_Cursor tmp;
888
889 ME_RunOfsFromCharOfs(editor, nCharOfs, &tmp.pRun, &tmp.nOffset);
890 ME_GetCursorCoordinates(editor, &tmp, &x, &y, &height);
891
892 rc.left = 0;
893 rc.top = y;
894 rc.bottom = y + height;
895 rc.right = editor->rcFormat.right;
896 InvalidateRect(editor->hWnd, &rc, FALSE);
897 }
898
899
900 void
901 ME_InvalidateSelection(ME_TextEditor *editor)
902 {
903 ME_DisplayItem *para1, *para2;
904 int nStart, nEnd;
905 int len = ME_GetTextLength(editor);
906
907 ME_GetSelection(editor, &nStart, &nEnd);
908 /* if both old and new selection are 0-char (= caret only), then
909 there's no (inverted) area to be repainted, neither old nor new */
910 if (nStart == nEnd && editor->nLastSelStart == editor->nLastSelEnd)
911 return;
912 ME_WrapMarkedParagraphs(editor);
913 ME_GetSelectionParas(editor, ¶1, ¶2);
914 assert(para1->type == diParagraph);
915 assert(para2->type == diParagraph);
916 /* last selection markers aren't always updated, which means
917 they can point past the end of the document */
918 if (editor->nLastSelStart > len || editor->nLastSelEnd > len) {
919 ME_MarkForPainting(editor,
920 ME_FindItemFwd(editor->pBuffer->pFirst, diParagraph),
921 ME_FindItemFwd(editor->pBuffer->pFirst, diTextEnd));
922 } else {
923 /* if the start part of selection is being expanded or contracted... */
924 if (nStart < editor->nLastSelStart) {
925 ME_MarkForPainting(editor, para1, ME_FindItemFwd(editor->pLastSelStartPara, diParagraphOrEnd));
926 } else
927 if (nStart > editor->nLastSelStart) {
928 ME_MarkForPainting(editor, editor->pLastSelStartPara, ME_FindItemFwd(para1, diParagraphOrEnd));
929 }
930
931 /* if the end part of selection is being contracted or expanded... */
932 if (nEnd < editor->nLastSelEnd) {
933 ME_MarkForPainting(editor, para2, ME_FindItemFwd(editor->pLastSelEndPara, diParagraphOrEnd));
934 } else
935 if (nEnd > editor->nLastSelEnd) {
936 ME_MarkForPainting(editor, editor->pLastSelEndPara, ME_FindItemFwd(para2, diParagraphOrEnd));
937 }
938 }
939
940 ME_InvalidateMarkedParagraphs(editor);
941 /* remember the last invalidated position */
942 ME_GetSelection(editor, &editor->nLastSelStart, &editor->nLastSelEnd);
943 ME_GetSelectionParas(editor, &editor->pLastSelStartPara, &editor->pLastSelEndPara);
944 assert(editor->pLastSelStartPara->type == diParagraph);
945 assert(editor->pLastSelEndPara->type == diParagraph);
946 }
947
948 void
949 ME_QueueInvalidateFromCursor(ME_TextEditor *editor, int nCursor)
950 {
951 editor->nInvalidOfs = ME_GetCursorOfs(editor, nCursor);
952 }
953
954
955 BOOL
956 ME_SetZoom(ME_TextEditor *editor, int numerator, int denominator)
957 {
958 /* TODO: Zoom images and objects */
959
960 if (numerator != 0)
961 {
962 if (denominator == 0)
963 return FALSE;
964 if (1.0 / 64.0 > (float)numerator / (float)denominator
965 || (float)numerator / (float)denominator > 64.0)
966 return FALSE;
967 }
968
969 editor->nZoomNumerator = numerator;
970 editor->nZoomDenominator = denominator;
971
972 ME_RewrapRepaint(editor);
973 return TRUE;
974 }
975
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.