2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
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
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
30 * - center and right align in WordPad omits all spaces at the start, we don't
31 * - objects/images are not handled yet
35 /******************************************************************************
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
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;
47 SIZE size
= ME_GetRunSizeCommon( c
, para
, run
, run
->len
, startx
, &run
->nAscent
, &run
->nDescent
);
48 run
->nWidth
= size
.cx
;
52 /******************************************************************************
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
;
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
);
100 /******************************************************************************
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 if (!run
->len
|| cx
<= 0) return 0;
110 return ME_CharFromPointContext( c
, cx
, run
, FALSE
);
113 static ME_DisplayItem
*ME_MakeRow(int height
, int baseline
, int width
)
115 ME_DisplayItem
*item
= ME_MakeDI(diStartRow
);
117 item
->member
.row
.nHeight
= height
;
118 item
->member
.row
.nBaseline
= baseline
;
119 item
->member
.row
.nWidth
= width
;
123 static void ME_BeginRow(ME_WrapContext
*wc
)
126 ME_DisplayItem
*para
= wc
->pPara
;
128 pFmt
= para
->member
.para
.pFmt
;
129 wc
->pRowStart
= NULL
;
130 wc
->bOverflown
= FALSE
;
131 wc
->pLastSplittableRun
= NULL
;
132 wc
->bWordWrap
= wc
->context
->editor
->bWordWrap
;
133 if (para
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
)) {
135 wc
->bWordWrap
= FALSE
;
136 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
138 ME_Cell
*cell
= &ME_FindItemBack(para
, diCell
)->member
.cell
;
141 } else if (para
->member
.para
.pCell
) {
142 ME_Cell
*cell
= ¶
->member
.para
.pCell
->member
.cell
;
145 width
= cell
->nRightBoundary
;
147 width
-= cell
->prev_cell
->member
.cell
.nRightBoundary
;
148 if (!cell
->prev_cell
)
150 int rowIndent
= ME_GetTableRowEnd(para
)->member
.para
.pFmt
->dxStartIndent
;
153 cell
->nWidth
= max(ME_twips2pointsX(wc
->context
, width
), 0);
155 wc
->nAvailWidth
= cell
->nWidth
156 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
157 wc
->bWordWrap
= TRUE
;
159 wc
->nAvailWidth
= wc
->context
->nAvailWidth
160 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
162 wc
->pt
.x
= wc
->context
->pt
.x
;
163 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
164 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
165 /* Shift the text down because of the border. */
169 static void ME_InsertRowStart(ME_WrapContext
*wc
, const ME_DisplayItem
*pEnd
)
171 ME_DisplayItem
*p
, *row
, *para
;
172 BOOL bSkippingSpaces
= TRUE
;
173 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
177 pFmt
= para
->member
.para
.pFmt
;
179 for (p
= pEnd
->prev
; p
!=wc
->pRowStart
->prev
; p
= p
->prev
)
181 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
182 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
183 if (p
->member
.run
.nAscent
>ascent
)
184 ascent
= p
->member
.run
.nAscent
;
185 if (p
->member
.run
.nDescent
>descent
)
186 descent
= p
->member
.run
.nDescent
;
189 /* Exclude space characters from run width.
190 * Other whitespace or delimiters are not treated this way. */
192 int len
= p
->member
.run
.len
;
193 WCHAR
*text
= get_text( &p
->member
.run
, len
- 1 );
196 if (~p
->member
.run
.nFlags
& MERF_GRAPHICS
)
197 while (len
&& *(text
--) == ' ')
201 if (len
== p
->member
.run
.len
)
203 width
+= p
->member
.run
.nWidth
;
205 sz
= ME_GetRunSize(wc
->context
, ¶
->member
.para
,
206 &p
->member
.run
, len
, p
->member
.run
.pt
.x
);
210 bSkippingSpaces
= !len
;
211 } else if (!(p
->member
.run
.nFlags
& MERF_ENDPARA
))
212 width
+= p
->member
.run
.nWidth
;
216 para
->member
.para
.nWidth
= max(para
->member
.para
.nWidth
, width
);
217 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
218 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
219 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
221 /* The text was shifted down in ME_BeginRow so move the wrap context
222 * back to where it should be. */
224 /* The height of the row is increased by the borders. */
225 row
->member
.row
.nHeight
+= 2;
227 row
->member
.row
.pt
= wc
->pt
;
228 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
229 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
230 assert(para
->member
.para
.pFmt
->dwMask
& PFM_ALIGNMENT
);
231 align
= para
->member
.para
.pFmt
->wAlignment
;
232 if (align
== PFA_CENTER
)
233 shift
= max((wc
->nAvailWidth
-width
)/2, 0);
234 if (align
== PFA_RIGHT
)
235 shift
= max(wc
->nAvailWidth
-width
, 0);
236 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
238 if (p
->type
==diRun
) { /* FIXME add more run types */
239 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
242 ME_InsertBefore(wc
->pRowStart
, row
);
244 wc
->pt
.y
+= row
->member
.row
.nHeight
;
248 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
250 ME_DisplayItem
*para
= wc
->pPara
;
251 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
253 ME_InsertRowStart(wc
, p
);
254 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
255 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
257 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
258 * text down by one pixel for the border, so fix up the wrap context. */
265 if (p->type == diParagraph || p->type == diTextEnd)
267 if (p->type == diRun)
269 ME_Run *run = &p->member.run;
270 TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
277 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
279 /* FIXME compose style (out of character and paragraph styles) here */
281 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
283 calc_run_extent(wc
->context
, &wc
->pPara
->member
.para
,
284 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, &p
->member
.run
);
288 static int find_non_whitespace(const WCHAR
*s
, int len
, int start
)
291 for (i
= start
; i
< len
&& ME_IsWSpace( s
[i
] ); i
++)
297 /* note: these two really return the first matching offset (starting from EOS)+1
298 * in other words, an offset of the first trailing white/black */
300 /* note: returns offset of the first trailing whitespace */
301 static int reverse_find_non_whitespace(const WCHAR
*s
, int start
)
304 for (i
= start
; i
> 0 && ME_IsWSpace( s
[i
- 1] ); i
--)
310 /* note: returns offset of the first trailing nonwhitespace */
311 static int reverse_find_whitespace(const WCHAR
*s
, int start
)
314 for (i
= start
; i
> 0 && !ME_IsWSpace( s
[i
- 1] ); i
--)
320 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
322 ME_DisplayItem
*pp
, *piter
= p
;
326 j
= reverse_find_non_whitespace( get_text( &p
->member
.run
, 0 ), i
);
328 pp
= split_run_extents(wc
, piter
, j
);
329 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
335 /* omit all spaces before split point */
336 while(piter
!= wc
->pRowStart
)
338 piter
= ME_FindItemBack(piter
, diRun
);
339 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
344 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
346 i
= reverse_find_non_whitespace( get_text( &piter
->member
.run
, 0 ),
347 piter
->member
.run
.len
);
348 pp
= split_run_extents(wc
, piter
, i
);
349 wc
->pt
= pp
->member
.run
.pt
;
352 /* this run is the end of spaces, so the run edge is a good point to split */
353 wc
->pt
= pp
->member
.run
.pt
;
354 wc
->bOverflown
= TRUE
;
355 TRACE("Split point is: %s|%s\n", debugstr_run( &piter
->member
.run
), debugstr_run( &pp
->member
.run
));
358 wc
->pt
= piter
->member
.run
.pt
;
363 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
365 ME_DisplayItem
*piter
= p
, *pp
;
367 ME_Run
*run
= &p
->member
.run
;
369 idesp
= i
= find_split_point( wc
->context
, loc
, run
);
374 /* don't split words */
375 i
= reverse_find_whitespace( get_text( run
, 0 ), i
);
376 pp
= ME_MaximizeSplit(wc
, p
, i
);
380 TRACE("Must backtrack to split at: %s\n", debugstr_run( &p
->member
.run
));
381 if (wc
->pLastSplittableRun
)
383 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
385 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
386 return wc
->pLastSplittableRun
;
388 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
390 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
391 they serve no other purpose */
392 ME_UpdateRunFlags(wc
->context
->editor
, run
);
393 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
395 piter
= wc
->pLastSplittableRun
;
396 run
= &piter
->member
.run
;
398 /* don't split words */
399 i
= reverse_find_whitespace( get_text( run
, 0 ), len
);
401 i
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
403 ME_DisplayItem
*piter2
= split_run_extents(wc
, piter
, i
);
404 wc
->pt
= piter2
->member
.run
.pt
;
407 /* splittable = must have whitespaces */
408 assert(0 == "Splittable, but no whitespaces");
412 /* restart from the first run beginning with spaces */
413 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
414 return wc
->pLastSplittableRun
;
417 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_run( &p
->member
.run
));
418 /* OK, no better idea, so assume we MAY split words if we can split at all*/
420 return split_run_extents(wc
, piter
, idesp
);
422 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
424 /* don't need to break current run, because it's possible to split
426 wc
->bOverflown
= TRUE
;
431 /* split point inside first character - no choice but split after that char */
433 /* the run is more than 1 char, so we may split */
434 return split_run_extents(wc
, piter
, 1);
436 /* the run is one char, can't split it */
441 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
447 assert(p
->type
== diRun
);
450 run
= &p
->member
.run
;
451 run
->pt
.x
= wc
->pt
.x
;
452 run
->pt
.y
= wc
->pt
.y
;
453 ME_WrapSizeRun(wc
, p
);
456 if (wc
->bOverflown
) /* just skipping final whitespaces */
458 /* End paragraph run can't overflow to the next line by itself. */
459 if (run
->nFlags
& MERF_ENDPARA
)
462 if (run
->nFlags
& MERF_WHITESPACE
) {
463 wc
->pt
.x
+= run
->nWidth
;
464 /* skip runs consisting of only whitespaces */
468 if (run
->nFlags
& MERF_STARTWHITE
) {
469 /* try to split the run at the first non-white char */
471 black
= find_non_whitespace( get_text( run
, 0 ), run
->len
, 0 );
473 wc
->bOverflown
= FALSE
;
474 pp
= split_run_extents(wc
, p
, black
);
475 calc_run_extent(wc
->context
, &wc
->pPara
->member
.para
,
476 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
,
478 ME_InsertRowStart(wc
, pp
);
482 /* black run: the row goes from pRowStart to the previous run */
483 ME_InsertRowStart(wc
, p
);
486 /* simply end the current row and move on to next one */
487 if (run
->nFlags
& MERF_ENDROW
)
490 ME_InsertRowStart(wc
, p
);
494 /* will current run fit? */
496 wc
->pt
.x
+ run
->nWidth
- wc
->context
->pt
.x
> wc
->nAvailWidth
)
498 int loc
= wc
->context
->pt
.x
+ wc
->nAvailWidth
- wc
->pt
.x
;
499 /* total white run ? */
500 if (run
->nFlags
& MERF_WHITESPACE
) {
501 /* let the overflow logic handle it */
502 wc
->bOverflown
= TRUE
;
505 /* TAB: we can split before */
506 if (run
->nFlags
& MERF_TAB
) {
507 wc
->bOverflown
= TRUE
;
508 if (wc
->pRowStart
== p
)
509 /* Don't split before the start of the run, or we will get an
515 /* graphics: we can split before, if run's width is smaller than row's width */
516 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
517 wc
->bOverflown
= TRUE
;
520 /* can we separate out the last spaces ? (to use overflow logic later) */
521 if (run
->nFlags
& MERF_ENDWHITE
)
523 /* we aren't sure if it's *really* necessary, it's a good start however */
524 int black
= reverse_find_non_whitespace( get_text( run
, 0 ), len
);
525 split_run_extents(wc
, p
, black
);
526 /* handle both parts again */
529 /* determine the split point by backtracking */
530 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
531 if (pp
== wc
->pRowStart
)
533 if (run
->nFlags
& MERF_STARTWHITE
)
535 /* We had only spaces so far, so we must be on the first line of the
536 * paragraph (or the first line after MERF_ENDROW forced the line
537 * break within the paragraph), since no other lines of the paragraph
538 * start with spaces. */
540 /* The lines will only contain spaces, and the rest of the run will
541 * overflow onto the next line. */
542 wc
->bOverflown
= TRUE
;
545 /* Couldn't split the first run, possible because we have a large font
546 * with a single character that caused an overflow.
548 wc
->pt
.x
+= run
->nWidth
;
551 if (p
!= pp
) /* found a suitable split point */
553 wc
->bOverflown
= TRUE
;
556 /* we detected that it's best to split on start of this run */
560 /* not found anything - writing over margins is the only option left */
562 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
563 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
565 wc
->pLastSplittableRun
= p
;
567 wc
->pt
.x
+= run
->nWidth
;
571 static int ME_GetParaLineSpace(ME_Context
* c
, ME_Paragraph
* para
)
574 if (!(para
->pFmt
->dwMask
& PFM_LINESPACING
)) return 0;
576 /* FIXME: how to compute simply the line space in ls ??? */
577 /* FIXME: does line spacing include the line itself ??? */
578 switch (para
->pFmt
->bLineSpacingRule
)
580 case 0: sp
= ls
; break;
581 case 1: sp
= (3 * ls
) / 2; break;
582 case 2: sp
= 2 * ls
; break;
583 case 3: sp
= ME_twips2pointsY(c
, para
->pFmt
->dyLineSpacing
); if (sp
< ls
) sp
= ls
; break;
584 case 4: sp
= ME_twips2pointsY(c
, para
->pFmt
->dyLineSpacing
); break;
585 case 5: sp
= para
->pFmt
->dyLineSpacing
/ 20; break;
586 default: FIXME("Unsupported spacing rule value %d\n", para
->pFmt
->bLineSpacingRule
);
588 if (c
->editor
->nZoomNumerator
== 0)
591 return sp
* c
->editor
->nZoomNumerator
/ c
->editor
->nZoomDenominator
;
594 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
597 tp
->member
.para
.nWidth
= 0;
598 /* remove row start items as they will be reinserted by the
599 * paragraph wrapper anyway */
600 tp
->member
.para
.nRows
= 0;
601 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
602 if (p
->type
== diStartRow
) {
603 ME_DisplayItem
*pRow
= p
;
606 ME_DestroyDisplayItem(pRow
);
609 /* join runs that can be joined */
610 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
611 assert(p
->type
!= diStartRow
); /* should have been deleted above */
612 if (p
->type
== diRun
) {
613 while (p
->next
->type
== diRun
&& /* FIXME */
614 ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
615 ME_JoinRuns(c
->editor
, p
);
621 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
) {
628 assert(tp
->type
== diParagraph
);
629 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
632 ME_PrepareParagraphForWrapping(c
, tp
);
633 pFmt
= tp
->member
.para
.pFmt
;
637 /* wc.para_style = tp->member.para.style; */
639 if (tp
->member
.para
.nFlags
& MEPF_ROWEND
) {
640 wc
.nFirstMargin
= wc
.nLeftMargin
= wc
.nRightMargin
= 0;
642 int dxStartIndent
= pFmt
->dxStartIndent
;
643 if (tp
->member
.para
.pCell
) {
644 dxStartIndent
+= ME_GetTableRowEnd(tp
)->member
.para
.pFmt
->dxOffset
;
646 wc
.nFirstMargin
= ME_twips2pointsX(c
, dxStartIndent
);
647 wc
.nLeftMargin
= wc
.nFirstMargin
+ ME_twips2pointsX(c
, pFmt
->dxOffset
);
648 wc
.nRightMargin
= ME_twips2pointsX(c
, pFmt
->dxRightIndent
);
650 if (c
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
651 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
653 wc
.nFirstMargin
+= ME_twips2pointsX(c
, pFmt
->dxOffset
* 2);
657 if (pFmt
->dwMask
& PFM_SPACEBEFORE
)
658 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceBefore
);
659 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
660 pFmt
->dwMask
& PFM_BORDER
)
662 border
= ME_GetParaBorderWidth(c
, tp
->member
.para
.pFmt
->wBorders
);
663 if (pFmt
->wBorders
& 1) {
664 wc
.nFirstMargin
+= border
;
665 wc
.nLeftMargin
+= border
;
667 if (pFmt
->wBorders
& 2)
668 wc
.nRightMargin
-= border
;
669 if (pFmt
->wBorders
& 4)
673 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
676 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
677 assert(p
->type
!= diStartRow
);
678 if (p
->type
== diRun
) {
679 p
= ME_WrapHandleRun(&wc
, p
);
682 if (wc
.nRow
&& p
== wc
.pRowStart
)
683 wc
.pt
.y
+= linespace
;
685 ME_WrapEndParagraph(&wc
, p
);
686 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
687 (pFmt
->dwMask
& PFM_BORDER
) && (pFmt
->wBorders
& 8))
689 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEAFTER
)
690 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceAfter
);
692 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
693 tp
->member
.para
.nHeight
= wc
.pt
.y
;
694 tp
->member
.para
.nRows
= wc
.nRow
;
697 static void ME_MarkRepaintEnd(ME_DisplayItem
*para
,
698 ME_DisplayItem
**repaint_start
,
699 ME_DisplayItem
**repaint_end
)
702 *repaint_start
= para
;
706 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
)
708 ME_DisplayItem
*item
;
711 ME_DisplayItem
*repaint_start
= NULL
, *repaint_end
= NULL
;
713 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
715 item
= editor
->pBuffer
->pFirst
->next
;
716 while(item
!= editor
->pBuffer
->pLast
) {
717 BOOL bRedraw
= FALSE
;
719 assert(item
->type
== diParagraph
);
720 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
721 || (item
->member
.para
.pt
.y
!= c
.pt
.y
))
723 item
->member
.para
.pt
= c
.pt
;
725 ME_WrapTextParagraph(&c
, item
);
728 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
730 if (item
->member
.para
.nFlags
& MEPF_ROWSTART
)
732 ME_DisplayItem
*cell
= ME_FindItemFwd(item
, diCell
);
733 ME_DisplayItem
*endRowPara
;
735 cell
->member
.cell
.pt
= c
.pt
;
736 /* Offset the text by the largest top border width. */
737 while (cell
->member
.cell
.next_cell
) {
738 borderWidth
= max(borderWidth
, cell
->member
.cell
.border
.top
.width
);
739 cell
= cell
->member
.cell
.next_cell
;
741 endRowPara
= ME_FindItemFwd(cell
, diParagraph
);
742 assert(endRowPara
->member
.para
.nFlags
& MEPF_ROWEND
);
745 borderWidth
= max(ME_twips2pointsY(&c
, borderWidth
), 1);
747 cell
->member
.cell
.yTextOffset
= borderWidth
;
748 cell
= cell
->member
.cell
.prev_cell
;
750 c
.pt
.y
+= borderWidth
;
752 if (endRowPara
->member
.para
.pFmt
->dxStartIndent
> 0)
754 int dxStartIndent
= endRowPara
->member
.para
.pFmt
->dxStartIndent
;
755 cell
= ME_FindItemFwd(item
, diCell
);
756 cell
->member
.cell
.pt
.x
+= ME_twips2pointsX(&c
, dxStartIndent
);
757 c
.pt
.x
= cell
->member
.cell
.pt
.x
;
760 else if (item
->member
.para
.nFlags
& MEPF_ROWEND
)
762 /* Set all the cells to the height of the largest cell */
763 ME_DisplayItem
*startRowPara
;
764 int prevHeight
, nHeight
, bottomBorder
= 0;
765 ME_DisplayItem
*cell
= ME_FindItemBack(item
, diCell
);
766 item
->member
.para
.nWidth
= cell
->member
.cell
.pt
.x
+ cell
->member
.cell
.nWidth
;
767 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWSTART
))
769 /* Last row, the bottom border is added to the height. */
770 cell
= cell
->member
.cell
.prev_cell
;
773 bottomBorder
= max(bottomBorder
, cell
->member
.cell
.border
.bottom
.width
);
774 cell
= cell
->member
.cell
.prev_cell
;
776 bottomBorder
= ME_twips2pointsY(&c
, bottomBorder
);
777 cell
= ME_FindItemBack(item
, diCell
);
779 prevHeight
= cell
->member
.cell
.nHeight
;
780 nHeight
= cell
->member
.cell
.prev_cell
->member
.cell
.nHeight
+ bottomBorder
;
781 cell
->member
.cell
.nHeight
= nHeight
;
782 item
->member
.para
.nHeight
= nHeight
;
783 cell
= cell
->member
.cell
.prev_cell
;
784 cell
->member
.cell
.nHeight
= nHeight
;
785 while (cell
->member
.cell
.prev_cell
)
787 cell
= cell
->member
.cell
.prev_cell
;
788 cell
->member
.cell
.nHeight
= nHeight
;
790 /* Also set the height of the start row paragraph */
791 startRowPara
= ME_FindItemBack(cell
, diParagraph
);
792 startRowPara
->member
.para
.nHeight
= nHeight
;
793 c
.pt
.x
= startRowPara
->member
.para
.pt
.x
;
794 c
.pt
.y
= cell
->member
.cell
.pt
.y
+ nHeight
;
795 if (prevHeight
< nHeight
)
797 /* The height of the cells has grown, so invalidate the bottom of
799 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
800 cell
= ME_FindItemBack(item
, diCell
);
802 ME_MarkRepaintEnd(ME_FindItemBack(cell
, diParagraph
), &repaint_start
, &repaint_end
);
803 cell
= cell
->member
.cell
.prev_cell
;
807 else if (item
->member
.para
.pCell
&&
808 item
->member
.para
.pCell
!= item
->member
.para
.next_para
->member
.para
.pCell
)
810 /* The next paragraph is in the next cell in the table row. */
811 ME_Cell
*cell
= &item
->member
.para
.pCell
->member
.cell
;
812 cell
->nHeight
= c
.pt
.y
+ item
->member
.para
.nHeight
- cell
->pt
.y
;
814 /* Propagate the largest height to the end so that it can be easily
815 * sent back to all the cells at the end of the row. */
817 cell
->nHeight
= max(cell
->nHeight
, cell
->prev_cell
->member
.cell
.nHeight
);
819 c
.pt
.x
= cell
->pt
.x
+ cell
->nWidth
;
821 cell
->next_cell
->member
.cell
.pt
= c
.pt
;
822 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWEND
))
823 c
.pt
.y
+= cell
->yTextOffset
;
827 if (item
->member
.para
.pCell
) {
828 /* Next paragraph in the same cell. */
829 c
.pt
.x
= item
->member
.para
.pCell
->member
.cell
.pt
.x
;
831 /* Normal paragraph */
834 c
.pt
.y
+= item
->member
.para
.nHeight
;
837 totalWidth
= max(totalWidth
, item
->member
.para
.nWidth
);
838 item
= item
->member
.para
.next_para
;
840 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
841 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
843 editor
->nTotalLength
= c
.pt
.y
;
844 editor
->nTotalWidth
= totalWidth
;
845 editor
->pBuffer
->pLast
->member
.para
.pt
.x
= 0;
846 editor
->pBuffer
->pLast
->member
.para
.pt
.y
= c
.pt
.y
;
848 ME_DestroyContext(&c
);
850 if (repaint_start
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
851 ME_InvalidateParagraphRange(editor
, repaint_start
, repaint_end
);
852 return !!repaint_start
;
855 void ME_InvalidateParagraphRange(ME_TextEditor
*editor
,
856 ME_DisplayItem
*start_para
,
857 ME_DisplayItem
*last_para
)
863 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
865 ofs
= editor
->vert_si
.nPos
;
868 start_para
= ME_GetOuterParagraph(start_para
);
869 last_para
= ME_GetOuterParagraph(last_para
);
870 rc
.top
= c
.rcView
.top
+ start_para
->member
.para
.pt
.y
- ofs
;
872 rc
.top
= c
.rcView
.top
+ editor
->nTotalLength
- ofs
;
874 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
875 rc
.bottom
= c
.rcView
.top
+ editor
->nLastTotalLength
- ofs
;
877 rc
.bottom
= c
.rcView
.top
+ last_para
->member
.para
.pt
.y
+ last_para
->member
.para
.nHeight
- ofs
;
878 ITextHost_TxInvalidateRect(editor
->texthost
, &rc
, TRUE
);
880 ME_DestroyContext(&c
);
885 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
887 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
891 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
893 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
897 info
.nmhdr
.hwndFrom
= NULL
;
898 info
.nmhdr
.idFrom
= 0;
899 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
901 info
.rc
.right
= editor
->nTotalWidth
;
902 info
.rc
.bottom
= editor
->nTotalLength
;
904 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
905 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
906 editor
->nEventMask
|= ENM_REQUESTRESIZE
;