vbscript: Added IRegExp2_QueryInterface tests.
[wine/multimedia.git] / dlls / riched20 / wrap.c
blobaed04806317533d68e1785607bba0097bb8c8749
1 /*
2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
3 * warned !
5 * Copyright 2004 by Krzysztof Foltman
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28 * Unsolved problems:
30 * - center and right align in WordPad omits all spaces at the start, we don't
31 * - objects/images are not handled yet
32 * - no tabs
35 /******************************************************************************
36 * calc_run_extent
38 * Updates the size of the run (fills width, ascent and descent). The height
39 * is calculated based on whole row's ascent and descent anyway, so no need
40 * to use it here.
42 static void calc_run_extent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
44 if (run->nFlags & MERF_HIDDEN) run->nWidth = 0;
45 else
47 SIZE size = ME_GetRunSizeCommon( c, para, run, run->len, startx, &run->nAscent, &run->nDescent );
48 run->nWidth = size.cx;
52 /******************************************************************************
53 * split_run_extents
55 * Splits a run into two in a given place. It also updates the screen position
56 * and size (extent) of the newly generated runs.
58 static ME_DisplayItem *split_run_extents(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
60 ME_TextEditor *editor = wc->context->editor;
61 ME_Run *run, *run2;
62 ME_Paragraph *para = &wc->pPara->member.para;
63 ME_Cursor cursor = {wc->pPara, item, nVChar};
65 assert(item->member.run.nCharOfs != -1);
66 if(TRACE_ON(richedit))
68 TRACE("Before check before split\n");
69 ME_CheckCharOffsets(editor);
70 TRACE("After check before split\n");
73 run = &item->member.run;
75 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run ),
76 run->pt.x, run->pt.y);
78 ME_SplitRunSimple(editor, &cursor);
80 run2 = &cursor.pRun->member.run;
82 calc_run_extent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
84 run2->pt.x = run->pt.x+run->nWidth;
85 run2->pt.y = run->pt.y;
87 if(TRACE_ON(richedit))
89 TRACE("Before check after split\n");
90 ME_CheckCharOffsets(editor);
91 TRACE("After check after split\n");
92 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
93 debugstr_run( run ), run->pt.x, run->pt.y,
94 debugstr_run( run2 ), run2->pt.x, run2->pt.y);
97 return cursor.pRun;
100 /******************************************************************************
101 * find_split_point
103 * Returns a character position to split inside the run given a run-relative
104 * pixel horizontal position. This version rounds left (ie. if the second
105 * character is at pixel position 8, then for cx=0..7 it returns 0).
107 static int find_split_point( ME_Context *c, int cx, ME_Run *run )
109 int fit = 0;
110 HGDIOBJ hOldFont;
111 SIZE sz;
113 if (!run->len || cx <= 0) return 0;
115 if (run->nFlags & MERF_TAB ||
116 (run->nFlags & (MERF_ENDCELL|MERF_ENDPARA)) == MERF_ENDCELL)
118 if (cx < run->nWidth / 2) return 0;
119 return 1;
121 if (run->nFlags & MERF_GRAPHICS)
123 SIZE sz;
124 ME_GetOLEObjectSize( c, run, &sz );
125 if (cx < sz.cx) return 0;
126 return 1;
128 hOldFont = ME_SelectStyleFont( c, run->style );
130 if (c->editor->cPasswordMask)
132 ME_String *strMasked = ME_MakeStringR( c->editor->cPasswordMask, run->len );
133 GetTextExtentExPointW( c->hDC, strMasked->szData, run->len, cx, &fit, NULL, &sz );
134 ME_DestroyString( strMasked );
136 else
138 GetTextExtentExPointW( c->hDC, get_text( run, 0 ), run->len, cx, &fit, NULL, &sz );
141 ME_UnselectStyleFont( c, run->style, hOldFont );
143 return fit;
146 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
148 ME_DisplayItem *item = ME_MakeDI(diStartRow);
150 item->member.row.nHeight = height;
151 item->member.row.nBaseline = baseline;
152 item->member.row.nWidth = width;
153 return item;
156 static void ME_BeginRow(ME_WrapContext *wc)
158 PARAFORMAT2 *pFmt;
159 ME_DisplayItem *para = wc->pPara;
161 pFmt = para->member.para.pFmt;
162 wc->pRowStart = NULL;
163 wc->bOverflown = FALSE;
164 wc->pLastSplittableRun = NULL;
165 wc->bWordWrap = wc->context->editor->bWordWrap;
166 if (para->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
167 wc->nAvailWidth = 0;
168 wc->bWordWrap = FALSE;
169 if (para->member.para.nFlags & MEPF_ROWEND)
171 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
172 cell->nWidth = 0;
174 } else if (para->member.para.pCell) {
175 ME_Cell *cell = &para->member.para.pCell->member.cell;
176 int width;
178 width = cell->nRightBoundary;
179 if (cell->prev_cell)
180 width -= cell->prev_cell->member.cell.nRightBoundary;
181 if (!cell->prev_cell)
183 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
184 width -= rowIndent;
186 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
188 wc->nAvailWidth = cell->nWidth
189 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
190 wc->bWordWrap = TRUE;
191 } else {
192 wc->nAvailWidth = wc->context->nAvailWidth
193 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
195 wc->pt.x = wc->context->pt.x;
196 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
197 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
198 /* Shift the text down because of the border. */
199 wc->pt.y++;
202 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
204 ME_DisplayItem *p, *row, *para;
205 BOOL bSkippingSpaces = TRUE;
206 int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
207 PARAFORMAT2 *pFmt;
208 /* wrap text */
209 para = wc->pPara;
210 pFmt = para->member.para.pFmt;
212 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
214 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
215 if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
216 if (p->member.run.nAscent>ascent)
217 ascent = p->member.run.nAscent;
218 if (p->member.run.nDescent>descent)
219 descent = p->member.run.nDescent;
220 if (bSkippingSpaces)
222 /* Exclude space characters from run width.
223 * Other whitespace or delimiters are not treated this way. */
224 SIZE sz;
225 int len = p->member.run.len;
226 WCHAR *text = get_text( &p->member.run, len - 1 );
228 assert (len);
229 if (~p->member.run.nFlags & MERF_GRAPHICS)
230 while (len && *(text--) == ' ')
231 len--;
232 if (len)
234 if (len == p->member.run.len)
236 width += p->member.run.nWidth;
237 } else {
238 sz = ME_GetRunSize(wc->context, &para->member.para,
239 &p->member.run, len, p->member.run.pt.x);
240 width += sz.cx;
243 bSkippingSpaces = !len;
244 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
245 width += p->member.run.nWidth;
249 para->member.para.nWidth = max(para->member.para.nWidth, width);
250 row = ME_MakeRow(ascent+descent, ascent, width);
251 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
252 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
254 /* The text was shifted down in ME_BeginRow so move the wrap context
255 * back to where it should be. */
256 wc->pt.y--;
257 /* The height of the row is increased by the borders. */
258 row->member.row.nHeight += 2;
260 row->member.row.pt = wc->pt;
261 row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
262 row->member.row.nRMargin = wc->nRightMargin;
263 assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
264 align = para->member.para.pFmt->wAlignment;
265 if (align == PFA_CENTER)
266 shift = max((wc->nAvailWidth-width)/2, 0);
267 if (align == PFA_RIGHT)
268 shift = max(wc->nAvailWidth-width, 0);
269 for (p = wc->pRowStart; p!=pEnd; p = p->next)
271 if (p->type==diRun) { /* FIXME add more run types */
272 p->member.run.pt.x += row->member.row.nLMargin+shift;
275 ME_InsertBefore(wc->pRowStart, row);
276 wc->nRow++;
277 wc->pt.y += row->member.row.nHeight;
278 ME_BeginRow(wc);
281 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
283 ME_DisplayItem *para = wc->pPara;
284 PARAFORMAT2 *pFmt = para->member.para.pFmt;
285 if (wc->pRowStart)
286 ME_InsertRowStart(wc, p);
287 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
288 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
290 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
291 * text down by one pixel for the border, so fix up the wrap context. */
292 wc->pt.y--;
296 p = para->next;
297 while(p) {
298 if (p->type == diParagraph || p->type == diTextEnd)
299 return;
300 if (p->type == diRun)
302 ME_Run *run = &p->member.run;
303 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
305 p = p->next;
310 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
312 /* FIXME compose style (out of character and paragraph styles) here */
314 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
316 calc_run_extent(wc->context, &wc->pPara->member.para,
317 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
321 static int find_non_whitespace(const WCHAR *s, int len, int start)
323 int i;
324 for (i = start; i < len && ME_IsWSpace( s[i] ); i++)
327 return i;
330 /* note: these two really return the first matching offset (starting from EOS)+1
331 * in other words, an offset of the first trailing white/black */
333 /* note: returns offset of the first trailing whitespace */
334 static int reverse_find_non_whitespace(const WCHAR *s, int start)
336 int i;
337 for (i = start; i > 0 && ME_IsWSpace( s[i - 1] ); i--)
340 return i;
343 /* note: returns offset of the first trailing nonwhitespace */
344 static int reverse_find_whitespace(const WCHAR *s, int start)
346 int i;
347 for (i = start; i > 0 && !ME_IsWSpace( s[i - 1] ); i--)
350 return i;
353 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
355 ME_DisplayItem *pp, *piter = p;
356 int j;
357 if (!i)
358 return NULL;
359 j = reverse_find_non_whitespace( get_text( &p->member.run, 0 ), i);
360 if (j>0) {
361 pp = split_run_extents(wc, piter, j);
362 wc->pt.x += piter->member.run.nWidth;
363 return pp;
365 else
367 pp = piter;
368 /* omit all spaces before split point */
369 while(piter != wc->pRowStart)
371 piter = ME_FindItemBack(piter, diRun);
372 if (piter->member.run.nFlags & MERF_WHITESPACE)
374 pp = piter;
375 continue;
377 if (piter->member.run.nFlags & MERF_ENDWHITE)
379 i = reverse_find_non_whitespace( get_text( &piter->member.run, 0 ),
380 piter->member.run.len );
381 pp = split_run_extents(wc, piter, i);
382 wc->pt = pp->member.run.pt;
383 return pp;
385 /* this run is the end of spaces, so the run edge is a good point to split */
386 wc->pt = pp->member.run.pt;
387 wc->bOverflown = TRUE;
388 TRACE("Split point is: %s|%s\n", debugstr_run( &piter->member.run ), debugstr_run( &pp->member.run ));
389 return pp;
391 wc->pt = piter->member.run.pt;
392 return piter;
396 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
398 ME_DisplayItem *piter = p, *pp;
399 int i, idesp, len;
400 ME_Run *run = &p->member.run;
402 idesp = i = find_split_point( wc->context, loc, run );
403 len = run->len;
404 assert(len>0);
405 assert(i<len);
406 if (i) {
407 /* don't split words */
408 i = reverse_find_whitespace( get_text( run, 0 ), i );
409 pp = ME_MaximizeSplit(wc, p, i);
410 if (pp)
411 return pp;
413 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p->member.run ));
414 if (wc->pLastSplittableRun)
416 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
418 wc->pt = wc->pLastSplittableRun->member.run.pt;
419 return wc->pLastSplittableRun;
421 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
423 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
424 they serve no other purpose */
425 ME_UpdateRunFlags(wc->context->editor, run);
426 assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
428 piter = wc->pLastSplittableRun;
429 run = &piter->member.run;
430 len = run->len;
431 /* don't split words */
432 i = reverse_find_whitespace( get_text( run, 0 ), len );
433 if (i == len)
434 i = reverse_find_non_whitespace( get_text( run, 0 ), len );
435 if (i) {
436 ME_DisplayItem *piter2 = split_run_extents(wc, piter, i);
437 wc->pt = piter2->member.run.pt;
438 return piter2;
440 /* splittable = must have whitespaces */
441 assert(0 == "Splittable, but no whitespaces");
443 else
445 /* restart from the first run beginning with spaces */
446 wc->pt = wc->pLastSplittableRun->member.run.pt;
447 return wc->pLastSplittableRun;
450 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_run( &p->member.run ));
451 /* OK, no better idea, so assume we MAY split words if we can split at all*/
452 if (idesp)
453 return split_run_extents(wc, piter, idesp);
454 else
455 if (wc->pRowStart && piter != wc->pRowStart)
457 /* don't need to break current run, because it's possible to split
458 before this run */
459 wc->bOverflown = TRUE;
460 return piter;
462 else
464 /* split point inside first character - no choice but split after that char */
465 if (len != 1) {
466 /* the run is more than 1 char, so we may split */
467 return split_run_extents(wc, piter, 1);
469 /* the run is one char, can't split it */
470 return piter;
474 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
476 ME_DisplayItem *pp;
477 ME_Run *run;
478 int len;
480 assert(p->type == diRun);
481 if (!wc->pRowStart)
482 wc->pRowStart = p;
483 run = &p->member.run;
484 run->pt.x = wc->pt.x;
485 run->pt.y = wc->pt.y;
486 ME_WrapSizeRun(wc, p);
487 len = run->len;
489 if (wc->bOverflown) /* just skipping final whitespaces */
491 /* End paragraph run can't overflow to the next line by itself. */
492 if (run->nFlags & MERF_ENDPARA)
493 return p->next;
495 if (run->nFlags & MERF_WHITESPACE) {
496 wc->pt.x += run->nWidth;
497 /* skip runs consisting of only whitespaces */
498 return p->next;
501 if (run->nFlags & MERF_STARTWHITE) {
502 /* try to split the run at the first non-white char */
503 int black;
504 black = find_non_whitespace( get_text( run, 0 ), run->len, 0 );
505 if (black) {
506 wc->bOverflown = FALSE;
507 pp = split_run_extents(wc, p, black);
508 calc_run_extent(wc->context, &wc->pPara->member.para,
509 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin,
510 &pp->member.run);
511 ME_InsertRowStart(wc, pp);
512 return pp;
515 /* black run: the row goes from pRowStart to the previous run */
516 ME_InsertRowStart(wc, p);
517 return p;
519 /* simply end the current row and move on to next one */
520 if (run->nFlags & MERF_ENDROW)
522 p = p->next;
523 ME_InsertRowStart(wc, p);
524 return p;
527 /* will current run fit? */
528 if (wc->bWordWrap &&
529 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
531 int loc = wc->context->pt.x + wc->nAvailWidth - wc->pt.x;
532 /* total white run ? */
533 if (run->nFlags & MERF_WHITESPACE) {
534 /* let the overflow logic handle it */
535 wc->bOverflown = TRUE;
536 return p;
538 /* TAB: we can split before */
539 if (run->nFlags & MERF_TAB) {
540 wc->bOverflown = TRUE;
541 if (wc->pRowStart == p)
542 /* Don't split before the start of the run, or we will get an
543 * endless loop. */
544 return p->next;
545 else
546 return p;
548 /* graphics: we can split before, if run's width is smaller than row's width */
549 if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
550 wc->bOverflown = TRUE;
551 return p;
553 /* can we separate out the last spaces ? (to use overflow logic later) */
554 if (run->nFlags & MERF_ENDWHITE)
556 /* we aren't sure if it's *really* necessary, it's a good start however */
557 int black = reverse_find_non_whitespace( get_text( run, 0 ), len );
558 split_run_extents(wc, p, black);
559 /* handle both parts again */
560 return p;
562 /* determine the split point by backtracking */
563 pp = ME_SplitByBacktracking(wc, p, loc);
564 if (pp == wc->pRowStart)
566 if (run->nFlags & MERF_STARTWHITE)
568 /* We had only spaces so far, so we must be on the first line of the
569 * paragraph (or the first line after MERF_ENDROW forced the line
570 * break within the paragraph), since no other lines of the paragraph
571 * start with spaces. */
573 /* The lines will only contain spaces, and the rest of the run will
574 * overflow onto the next line. */
575 wc->bOverflown = TRUE;
576 return p;
578 /* Couldn't split the first run, possible because we have a large font
579 * with a single character that caused an overflow.
581 wc->pt.x += run->nWidth;
582 return p->next;
584 if (p != pp) /* found a suitable split point */
586 wc->bOverflown = TRUE;
587 return pp;
589 /* we detected that it's best to split on start of this run */
590 if (wc->bOverflown)
591 return pp;
592 ERR("failure!\n");
593 /* not found anything - writing over margins is the only option left */
595 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
596 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
598 wc->pLastSplittableRun = p;
600 wc->pt.x += run->nWidth;
601 return p->next;
604 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
606 int sp = 0, ls = 0;
607 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
609 /* FIXME: how to compute simply the line space in ls ??? */
610 /* FIXME: does line spacing include the line itself ??? */
611 switch (para->pFmt->bLineSpacingRule)
613 case 0: sp = ls; break;
614 case 1: sp = (3 * ls) / 2; break;
615 case 2: sp = 2 * ls; break;
616 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
617 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
618 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
619 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
621 if (c->editor->nZoomNumerator == 0)
622 return sp;
623 else
624 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
627 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
628 ME_DisplayItem *p;
630 tp->member.para.nWidth = 0;
631 /* remove row start items as they will be reinserted by the
632 * paragraph wrapper anyway */
633 tp->member.para.nRows = 0;
634 for (p = tp->next; p != tp->member.para.next_para; p = p->next) {
635 if (p->type == diStartRow) {
636 ME_DisplayItem *pRow = p;
637 p = p->prev;
638 ME_Remove(pRow);
639 ME_DestroyDisplayItem(pRow);
642 /* join runs that can be joined */
643 for (p = tp->next; p != tp->member.para.next_para; p = p->next) {
644 assert(p->type != diStartRow); /* should have been deleted above */
645 if (p->type == diRun) {
646 while (p->next->type == diRun && /* FIXME */
647 ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
648 ME_JoinRuns(c->editor, p);
654 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
655 ME_DisplayItem *p;
656 ME_WrapContext wc;
657 int border = 0;
658 int linespace = 0;
659 PARAFORMAT2 *pFmt;
661 assert(tp->type == diParagraph);
662 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
663 return;
665 ME_PrepareParagraphForWrapping(c, tp);
666 pFmt = tp->member.para.pFmt;
668 wc.context = c;
669 wc.pPara = tp;
670 /* wc.para_style = tp->member.para.style; */
671 wc.style = NULL;
672 if (tp->member.para.nFlags & MEPF_ROWEND) {
673 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
674 } else {
675 int dxStartIndent = pFmt->dxStartIndent;
676 if (tp->member.para.pCell) {
677 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
679 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
680 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
681 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
683 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
684 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
686 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
688 wc.nRow = 0;
689 wc.pt.y = 0;
690 if (pFmt->dwMask & PFM_SPACEBEFORE)
691 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
692 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
693 pFmt->dwMask & PFM_BORDER)
695 border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
696 if (pFmt->wBorders & 1) {
697 wc.nFirstMargin += border;
698 wc.nLeftMargin += border;
700 if (pFmt->wBorders & 2)
701 wc.nRightMargin -= border;
702 if (pFmt->wBorders & 4)
703 wc.pt.y += border;
706 linespace = ME_GetParaLineSpace(c, &tp->member.para);
708 ME_BeginRow(&wc);
709 for (p = tp->next; p!=tp->member.para.next_para; ) {
710 assert(p->type != diStartRow);
711 if (p->type == diRun) {
712 p = ME_WrapHandleRun(&wc, p);
714 else p = p->next;
715 if (wc.nRow && p == wc.pRowStart)
716 wc.pt.y += linespace;
718 ME_WrapEndParagraph(&wc, p);
719 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
720 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
721 wc.pt.y += border;
722 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
723 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
725 tp->member.para.nFlags &= ~MEPF_REWRAP;
726 tp->member.para.nHeight = wc.pt.y;
727 tp->member.para.nRows = wc.nRow;
730 static void ME_MarkRepaintEnd(ME_DisplayItem *para,
731 ME_DisplayItem **repaint_start,
732 ME_DisplayItem **repaint_end)
734 if (!*repaint_start)
735 *repaint_start = para;
736 *repaint_end = para;
739 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
741 ME_DisplayItem *item;
742 ME_Context c;
743 int totalWidth = 0;
744 ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
746 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
747 c.pt.x = 0;
748 item = editor->pBuffer->pFirst->next;
749 while(item != editor->pBuffer->pLast) {
750 BOOL bRedraw = FALSE;
752 assert(item->type == diParagraph);
753 if ((item->member.para.nFlags & MEPF_REWRAP)
754 || (item->member.para.pt.y != c.pt.y))
755 bRedraw = TRUE;
756 item->member.para.pt = c.pt;
758 ME_WrapTextParagraph(&c, item);
760 if (bRedraw)
761 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
763 if (item->member.para.nFlags & MEPF_ROWSTART)
765 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
766 ME_DisplayItem *endRowPara;
767 int borderWidth = 0;
768 cell->member.cell.pt = c.pt;
769 /* Offset the text by the largest top border width. */
770 while (cell->member.cell.next_cell) {
771 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
772 cell = cell->member.cell.next_cell;
774 endRowPara = ME_FindItemFwd(cell, diParagraph);
775 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
776 if (borderWidth > 0)
778 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
779 while (cell) {
780 cell->member.cell.yTextOffset = borderWidth;
781 cell = cell->member.cell.prev_cell;
783 c.pt.y += borderWidth;
785 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
787 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
788 cell = ME_FindItemFwd(item, diCell);
789 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
790 c.pt.x = cell->member.cell.pt.x;
793 else if (item->member.para.nFlags & MEPF_ROWEND)
795 /* Set all the cells to the height of the largest cell */
796 ME_DisplayItem *startRowPara;
797 int prevHeight, nHeight, bottomBorder = 0;
798 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
799 item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
800 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
802 /* Last row, the bottom border is added to the height. */
803 cell = cell->member.cell.prev_cell;
804 while (cell)
806 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
807 cell = cell->member.cell.prev_cell;
809 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
810 cell = ME_FindItemBack(item, diCell);
812 prevHeight = cell->member.cell.nHeight;
813 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
814 cell->member.cell.nHeight = nHeight;
815 item->member.para.nHeight = nHeight;
816 cell = cell->member.cell.prev_cell;
817 cell->member.cell.nHeight = nHeight;
818 while (cell->member.cell.prev_cell)
820 cell = cell->member.cell.prev_cell;
821 cell->member.cell.nHeight = nHeight;
823 /* Also set the height of the start row paragraph */
824 startRowPara = ME_FindItemBack(cell, diParagraph);
825 startRowPara->member.para.nHeight = nHeight;
826 c.pt.x = startRowPara->member.para.pt.x;
827 c.pt.y = cell->member.cell.pt.y + nHeight;
828 if (prevHeight < nHeight)
830 /* The height of the cells has grown, so invalidate the bottom of
831 * the cells. */
832 ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
833 cell = ME_FindItemBack(item, diCell);
834 while (cell) {
835 ME_MarkRepaintEnd(ME_FindItemBack(cell, diParagraph), &repaint_start, &repaint_end);
836 cell = cell->member.cell.prev_cell;
840 else if (item->member.para.pCell &&
841 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
843 /* The next paragraph is in the next cell in the table row. */
844 ME_Cell *cell = &item->member.para.pCell->member.cell;
845 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
847 /* Propagate the largest height to the end so that it can be easily
848 * sent back to all the cells at the end of the row. */
849 if (cell->prev_cell)
850 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
852 c.pt.x = cell->pt.x + cell->nWidth;
853 c.pt.y = cell->pt.y;
854 cell->next_cell->member.cell.pt = c.pt;
855 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
856 c.pt.y += cell->yTextOffset;
858 else
860 if (item->member.para.pCell) {
861 /* Next paragraph in the same cell. */
862 c.pt.x = item->member.para.pCell->member.cell.pt.x;
863 } else {
864 /* Normal paragraph */
865 c.pt.x = 0;
867 c.pt.y += item->member.para.nHeight;
870 totalWidth = max(totalWidth, item->member.para.nWidth);
871 item = item->member.para.next_para;
873 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
874 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
876 editor->nTotalLength = c.pt.y;
877 editor->nTotalWidth = totalWidth;
878 editor->pBuffer->pLast->member.para.pt.x = 0;
879 editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
881 ME_DestroyContext(&c);
883 if (repaint_start || editor->nTotalLength < editor->nLastTotalLength)
884 ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
885 return !!repaint_start;
888 void ME_InvalidateParagraphRange(ME_TextEditor *editor,
889 ME_DisplayItem *start_para,
890 ME_DisplayItem *last_para)
892 ME_Context c;
893 RECT rc;
894 int ofs;
896 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
897 rc = c.rcView;
898 ofs = editor->vert_si.nPos;
900 if (start_para) {
901 start_para = ME_GetOuterParagraph(start_para);
902 last_para = ME_GetOuterParagraph(last_para);
903 rc.top = c.rcView.top + start_para->member.para.pt.y - ofs;
904 } else {
905 rc.top = c.rcView.top + editor->nTotalLength - ofs;
907 if (editor->nTotalLength < editor->nLastTotalLength)
908 rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
909 else
910 rc.bottom = c.rcView.top + last_para->member.para.pt.y + last_para->member.para.nHeight - ofs;
911 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
913 ME_DestroyContext(&c);
917 void
918 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
920 if (editor->nEventMask & ENM_REQUESTRESIZE)
922 RECT rc;
924 ITextHost_TxGetClientRect(editor->texthost, &rc);
926 if (force || rc.bottom != editor->nTotalLength)
928 REQRESIZE info;
930 info.nmhdr.hwndFrom = NULL;
931 info.nmhdr.idFrom = 0;
932 info.nmhdr.code = EN_REQUESTRESIZE;
933 info.rc = rc;
934 info.rc.right = editor->nTotalWidth;
935 info.rc.bottom = editor->nTotalLength;
937 editor->nEventMask &= ~ENM_REQUESTRESIZE;
938 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
939 editor->nEventMask |= ENM_REQUESTRESIZE;