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 static ME_DisplayItem
*ME_MakeRow(int height
, int baseline
, int width
)
37 ME_DisplayItem
*item
= ME_MakeDI(diStartRow
);
39 item
->member
.row
.nHeight
= height
;
40 item
->member
.row
.nBaseline
= baseline
;
41 item
->member
.row
.nWidth
= width
;
45 static void ME_BeginRow(ME_WrapContext
*wc
)
48 ME_DisplayItem
*para
= wc
->pPara
;
50 pFmt
= para
->member
.para
.pFmt
;
52 wc
->bOverflown
= FALSE
;
53 wc
->pLastSplittableRun
= NULL
;
54 wc
->bWordWrap
= wc
->context
->editor
->bWordWrap
;
55 if (para
->member
.para
.nFlags
& (MEPF_ROWSTART
|MEPF_ROWEND
)) {
57 wc
->bWordWrap
= FALSE
;
58 if (para
->member
.para
.nFlags
& MEPF_ROWEND
)
60 ME_Cell
*cell
= &ME_FindItemBack(para
, diCell
)->member
.cell
;
63 } else if (para
->member
.para
.pCell
) {
64 ME_Cell
*cell
= ¶
->member
.para
.pCell
->member
.cell
;
67 width
= cell
->nRightBoundary
;
69 width
-= cell
->prev_cell
->member
.cell
.nRightBoundary
;
72 int rowIndent
= ME_GetTableRowEnd(para
)->member
.para
.pFmt
->dxStartIndent
;
75 cell
->nWidth
= max(ME_twips2pointsX(wc
->context
, width
), 0);
77 wc
->nAvailWidth
= cell
->nWidth
78 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
81 wc
->nAvailWidth
= wc
->context
->nAvailWidth
82 - (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
84 wc
->pt
.x
= wc
->context
->pt
.x
;
85 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
86 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
87 /* Shift the text down because of the border. */
91 static void ME_InsertRowStart(ME_WrapContext
*wc
, const ME_DisplayItem
*pEnd
)
93 ME_DisplayItem
*p
, *row
, *para
;
94 BOOL bSkippingSpaces
= TRUE
;
95 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
99 pFmt
= para
->member
.para
.pFmt
;
101 for (p
= pEnd
->prev
; p
!=wc
->pRowStart
->prev
; p
= p
->prev
)
103 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
104 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
105 if (p
->member
.run
.nAscent
>ascent
)
106 ascent
= p
->member
.run
.nAscent
;
107 if (p
->member
.run
.nDescent
>descent
)
108 descent
= p
->member
.run
.nDescent
;
111 /* Exclude space characters from run width.
112 * Other whitespace or delimiters are not treated this way. */
114 int len
= p
->member
.run
.strText
->nLen
;
115 WCHAR
*text
= p
->member
.run
.strText
->szData
+ len
- 1;
118 if (~p
->member
.run
.nFlags
& MERF_GRAPHICS
)
119 while (len
&& *(text
--) == ' ')
123 if (len
== p
->member
.run
.strText
->nLen
)
125 width
+= p
->member
.run
.nWidth
;
127 sz
= ME_GetRunSize(wc
->context
, ¶
->member
.para
,
128 &p
->member
.run
, len
, p
->member
.run
.pt
.x
);
132 bSkippingSpaces
= !len
;
133 } else if (!(p
->member
.run
.nFlags
& MERF_ENDPARA
))
134 width
+= p
->member
.run
.nWidth
;
138 para
->member
.para
.nWidth
= max(para
->member
.para
.nWidth
, width
);
139 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
140 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
141 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
143 /* The text was shifted down in ME_BeginRow so move the wrap context
144 * back to where it should be. */
146 /* The height of the row is increased by the borders. */
147 row
->member
.row
.nHeight
+= 2;
149 row
->member
.row
.pt
= wc
->pt
;
150 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
151 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
152 assert(para
->member
.para
.pFmt
->dwMask
& PFM_ALIGNMENT
);
153 align
= para
->member
.para
.pFmt
->wAlignment
;
154 if (align
== PFA_CENTER
)
155 shift
= max((wc
->nAvailWidth
-width
)/2, 0);
156 if (align
== PFA_RIGHT
)
157 shift
= max(wc
->nAvailWidth
-width
, 0);
158 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
160 if (p
->type
==diRun
) { /* FIXME add more run types */
161 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
164 ME_InsertBefore(wc
->pRowStart
, row
);
166 wc
->pt
.y
+= row
->member
.row
.nHeight
;
170 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
172 ME_DisplayItem
*para
= wc
->pPara
;
173 PARAFORMAT2
*pFmt
= para
->member
.para
.pFmt
;
175 ME_InsertRowStart(wc
, p
);
176 if (wc
->context
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
177 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
179 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
180 * text down by one pixel for the border, so fix up the wrap context. */
187 if (p->type == diParagraph || p->type == diTextEnd)
189 if (p->type == diRun)
191 ME_Run *run = &p->member.run;
192 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
199 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
201 /* FIXME compose style (out of character and paragraph styles) here */
203 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
205 ME_CalcRunExtent(wc
->context
, &wc
->pPara
->member
.para
,
206 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, &p
->member
.run
);
209 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
211 ME_DisplayItem
*pp
, *piter
= p
;
215 j
= ME_ReverseFindNonWhitespaceV(p
->member
.run
.strText
, i
);
217 pp
= ME_SplitRun(wc
, piter
, j
);
218 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
224 /* omit all spaces before split point */
225 while(piter
!= wc
->pRowStart
)
227 piter
= ME_FindItemBack(piter
, diRun
);
228 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
233 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
235 i
= ME_ReverseFindNonWhitespaceV(piter
->member
.run
.strText
,
236 piter
->member
.run
.strText
->nLen
);
237 pp
= ME_SplitRun(wc
, piter
, i
);
238 wc
->pt
= pp
->member
.run
.pt
;
241 /* this run is the end of spaces, so the run edge is a good point to split */
242 wc
->pt
= pp
->member
.run
.pt
;
243 wc
->bOverflown
= TRUE
;
244 TRACE("Split point is: %s|%s\n", debugstr_w(piter
->member
.run
.strText
->szData
), debugstr_w(pp
->member
.run
.strText
->szData
));
247 wc
->pt
= piter
->member
.run
.pt
;
252 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
254 ME_DisplayItem
*piter
= p
, *pp
;
256 ME_Run
*run
= &p
->member
.run
;
258 idesp
= i
= ME_CharFromPoint(wc
->context
, loc
, run
);
259 len
= run
->strText
->nLen
;
263 /* don't split words */
264 i
= ME_ReverseFindWhitespaceV(run
->strText
, i
);
265 pp
= ME_MaximizeSplit(wc
, p
, i
);
269 TRACE("Must backtrack to split at: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
270 if (wc
->pLastSplittableRun
)
272 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
274 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
275 return wc
->pLastSplittableRun
;
277 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
279 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
280 they serve no other purpose */
281 ME_UpdateRunFlags(wc
->context
->editor
, run
);
282 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
284 piter
= wc
->pLastSplittableRun
;
285 run
= &piter
->member
.run
;
286 len
= run
->strText
->nLen
;
287 /* don't split words */
288 i
= ME_ReverseFindWhitespaceV(run
->strText
, len
);
290 i
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
292 ME_DisplayItem
*piter2
= ME_SplitRun(wc
, piter
, i
);
293 wc
->pt
= piter2
->member
.run
.pt
;
296 /* splittable = must have whitespaces */
297 assert(0 == "Splittable, but no whitespaces");
301 /* restart from the first run beginning with spaces */
302 wc
->pt
= wc
->pLastSplittableRun
->member
.run
.pt
;
303 return wc
->pLastSplittableRun
;
306 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
307 /* OK, no better idea, so assume we MAY split words if we can split at all*/
309 return ME_SplitRun(wc
, piter
, idesp
);
311 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
313 /* don't need to break current run, because it's possible to split
315 wc
->bOverflown
= TRUE
;
320 /* split point inside first character - no choice but split after that char */
322 /* the run is more than 1 char, so we may split */
323 return ME_SplitRun(wc
, piter
, 1);
325 /* the run is one char, can't split it */
330 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
336 assert(p
->type
== diRun
);
339 run
= &p
->member
.run
;
340 run
->pt
.x
= wc
->pt
.x
;
341 run
->pt
.y
= wc
->pt
.y
;
342 ME_WrapSizeRun(wc
, p
);
343 len
= run
->strText
->nLen
;
345 if (wc
->bOverflown
) /* just skipping final whitespaces */
347 /* End paragraph run can't overflow to the next line by itself. */
348 if (run
->nFlags
& MERF_ENDPARA
)
351 if (run
->nFlags
& MERF_WHITESPACE
) {
352 wc
->pt
.x
+= run
->nWidth
;
353 /* skip runs consisting of only whitespaces */
357 if (run
->nFlags
& MERF_STARTWHITE
) {
358 /* try to split the run at the first non-white char */
360 black
= ME_FindNonWhitespaceV(run
->strText
, 0);
362 wc
->bOverflown
= FALSE
;
363 pp
= ME_SplitRun(wc
, p
, black
);
364 ME_CalcRunExtent(wc
->context
, &wc
->pPara
->member
.para
,
365 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
,
367 ME_InsertRowStart(wc
, pp
);
371 /* black run: the row goes from pRowStart to the previous run */
372 ME_InsertRowStart(wc
, p
);
375 /* simply end the current row and move on to next one */
376 if (run
->nFlags
& MERF_ENDROW
)
379 ME_InsertRowStart(wc
, p
);
383 /* will current run fit? */
385 wc
->pt
.x
+ run
->nWidth
- wc
->context
->pt
.x
> wc
->nAvailWidth
)
387 int loc
= wc
->context
->pt
.x
+ wc
->nAvailWidth
- wc
->pt
.x
;
388 /* total white run ? */
389 if (run
->nFlags
& MERF_WHITESPACE
) {
390 /* let the overflow logic handle it */
391 wc
->bOverflown
= TRUE
;
394 /* TAB: we can split before */
395 if (run
->nFlags
& MERF_TAB
) {
396 wc
->bOverflown
= TRUE
;
397 if (wc
->pRowStart
== p
)
398 /* Don't split before the start of the run, or we will get an
404 /* graphics: we can split before, if run's width is smaller than row's width */
405 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
406 wc
->bOverflown
= TRUE
;
409 /* can we separate out the last spaces ? (to use overflow logic later) */
410 if (run
->nFlags
& MERF_ENDWHITE
)
412 /* we aren't sure if it's *really* necessary, it's a good start however */
413 int black
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
414 ME_SplitRun(wc
, p
, black
);
415 /* handle both parts again */
418 /* determine the split point by backtracking */
419 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
420 if (pp
== wc
->pRowStart
)
422 if (run
->nFlags
& MERF_STARTWHITE
)
424 /* We had only spaces so far, so we must be on the first line of the
425 * paragraph (or the first line after MERF_ENDROW forced the line
426 * break within the paragraph), since no other lines of the paragraph
427 * start with spaces. */
429 /* The lines will only contain spaces, and the rest of the run will
430 * overflow onto the next line. */
431 wc
->bOverflown
= TRUE
;
434 /* Couldn't split the first run, possible because we have a large font
435 * with a single character that caused an overflow.
437 wc
->pt
.x
+= run
->nWidth
;
440 if (p
!= pp
) /* found a suitable split point */
442 wc
->bOverflown
= TRUE
;
445 /* we detected that it's best to split on start of this run */
449 /* not found anything - writing over margins is the only option left */
451 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
452 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
454 wc
->pLastSplittableRun
= p
;
456 wc
->pt
.x
+= run
->nWidth
;
460 static int ME_GetParaLineSpace(ME_Context
* c
, ME_Paragraph
* para
)
463 if (!(para
->pFmt
->dwMask
& PFM_LINESPACING
)) return 0;
465 /* FIXME: how to compute simply the line space in ls ??? */
466 /* FIXME: does line spacing include the line itself ??? */
467 switch (para
->pFmt
->bLineSpacingRule
)
469 case 0: sp
= ls
; break;
470 case 1: sp
= (3 * ls
) / 2; break;
471 case 2: sp
= 2 * ls
; break;
472 case 3: sp
= ME_twips2pointsY(c
, para
->pFmt
->dyLineSpacing
); if (sp
< ls
) sp
= ls
; break;
473 case 4: sp
= ME_twips2pointsY(c
, para
->pFmt
->dyLineSpacing
); break;
474 case 5: sp
= para
->pFmt
->dyLineSpacing
/ 20; break;
475 default: FIXME("Unsupported spacing rule value %d\n", para
->pFmt
->bLineSpacingRule
);
477 if (c
->editor
->nZoomNumerator
== 0)
480 return sp
* c
->editor
->nZoomNumerator
/ c
->editor
->nZoomDenominator
;
483 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
486 tp
->member
.para
.nWidth
= 0;
487 /* remove row start items as they will be reinserted by the
488 * paragraph wrapper anyway */
489 tp
->member
.para
.nRows
= 0;
490 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
491 if (p
->type
== diStartRow
) {
492 ME_DisplayItem
*pRow
= p
;
495 ME_DestroyDisplayItem(pRow
);
498 /* join runs that can be joined */
499 for (p
= tp
->next
; p
!= tp
->member
.para
.next_para
; p
= p
->next
) {
500 assert(p
->type
!= diStartRow
); /* should have been deleted above */
501 if (p
->type
== diRun
) {
502 while (p
->next
->type
== diRun
&& /* FIXME */
503 ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
504 ME_JoinRuns(c
->editor
, p
);
510 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
) {
517 assert(tp
->type
== diParagraph
);
518 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
521 ME_PrepareParagraphForWrapping(c
, tp
);
522 pFmt
= tp
->member
.para
.pFmt
;
526 /* wc.para_style = tp->member.para.style; */
528 if (tp
->member
.para
.nFlags
& MEPF_ROWEND
) {
529 wc
.nFirstMargin
= wc
.nLeftMargin
= wc
.nRightMargin
= 0;
531 int dxStartIndent
= pFmt
->dxStartIndent
;
532 if (tp
->member
.para
.pCell
) {
533 dxStartIndent
+= ME_GetTableRowEnd(tp
)->member
.para
.pFmt
->dxOffset
;
535 wc
.nFirstMargin
= ME_twips2pointsX(c
, dxStartIndent
);
536 wc
.nLeftMargin
= wc
.nFirstMargin
+ ME_twips2pointsX(c
, pFmt
->dxOffset
);
537 wc
.nRightMargin
= ME_twips2pointsX(c
, pFmt
->dxRightIndent
);
539 if (c
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
540 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
542 wc
.nFirstMargin
+= ME_twips2pointsX(c
, pFmt
->dxOffset
* 2);
546 if (pFmt
->dwMask
& PFM_SPACEBEFORE
)
547 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceBefore
);
548 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
549 pFmt
->dwMask
& PFM_BORDER
)
551 border
= ME_GetParaBorderWidth(c
, tp
->member
.para
.pFmt
->wBorders
);
552 if (pFmt
->wBorders
& 1) {
553 wc
.nFirstMargin
+= border
;
554 wc
.nLeftMargin
+= border
;
556 if (pFmt
->wBorders
& 2)
557 wc
.nRightMargin
-= border
;
558 if (pFmt
->wBorders
& 4)
562 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
565 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
566 assert(p
->type
!= diStartRow
);
567 if (p
->type
== diRun
) {
568 p
= ME_WrapHandleRun(&wc
, p
);
571 if (wc
.nRow
&& p
== wc
.pRowStart
)
572 wc
.pt
.y
+= linespace
;
574 ME_WrapEndParagraph(&wc
, p
);
575 if (!(pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
) &&
576 (pFmt
->dwMask
& PFM_BORDER
) && (pFmt
->wBorders
& 8))
578 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEAFTER
)
579 wc
.pt
.y
+= ME_twips2pointsY(c
, pFmt
->dySpaceAfter
);
581 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
582 tp
->member
.para
.nHeight
= wc
.pt
.y
;
583 tp
->member
.para
.nRows
= wc
.nRow
;
586 static void ME_MarkRepaintEnd(ME_DisplayItem
*para
,
587 ME_DisplayItem
**repaint_start
,
588 ME_DisplayItem
**repaint_end
)
591 *repaint_start
= para
;
595 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
)
597 ME_DisplayItem
*item
;
600 ME_DisplayItem
*repaint_start
= NULL
, *repaint_end
= NULL
;
602 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
604 item
= editor
->pBuffer
->pFirst
->next
;
605 while(item
!= editor
->pBuffer
->pLast
) {
606 BOOL bRedraw
= FALSE
;
608 assert(item
->type
== diParagraph
);
609 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
610 || (item
->member
.para
.pt
.y
!= c
.pt
.y
))
612 item
->member
.para
.pt
= c
.pt
;
614 ME_WrapTextParagraph(&c
, item
);
617 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
619 if (item
->member
.para
.nFlags
& MEPF_ROWSTART
)
621 ME_DisplayItem
*cell
= ME_FindItemFwd(item
, diCell
);
622 ME_DisplayItem
*endRowPara
;
624 cell
->member
.cell
.pt
= c
.pt
;
625 /* Offset the text by the largest top border width. */
626 while (cell
->member
.cell
.next_cell
) {
627 borderWidth
= max(borderWidth
, cell
->member
.cell
.border
.top
.width
);
628 cell
= cell
->member
.cell
.next_cell
;
630 endRowPara
= ME_FindItemFwd(cell
, diParagraph
);
631 assert(endRowPara
->member
.para
.nFlags
& MEPF_ROWEND
);
634 borderWidth
= max(ME_twips2pointsY(&c
, borderWidth
), 1);
636 cell
->member
.cell
.yTextOffset
= borderWidth
;
637 cell
= cell
->member
.cell
.prev_cell
;
639 c
.pt
.y
+= borderWidth
;
641 if (endRowPara
->member
.para
.pFmt
->dxStartIndent
> 0)
643 int dxStartIndent
= endRowPara
->member
.para
.pFmt
->dxStartIndent
;
644 cell
= ME_FindItemFwd(item
, diCell
);
645 cell
->member
.cell
.pt
.x
+= ME_twips2pointsX(&c
, dxStartIndent
);
646 c
.pt
.x
= cell
->member
.cell
.pt
.x
;
649 else if (item
->member
.para
.nFlags
& MEPF_ROWEND
)
651 /* Set all the cells to the height of the largest cell */
652 ME_DisplayItem
*startRowPara
;
653 int prevHeight
, nHeight
, bottomBorder
= 0;
654 ME_DisplayItem
*cell
= ME_FindItemBack(item
, diCell
);
655 item
->member
.para
.nWidth
= cell
->member
.cell
.pt
.x
+ cell
->member
.cell
.nWidth
;
656 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWSTART
))
658 /* Last row, the bottom border is added to the height. */
659 cell
= cell
->member
.cell
.prev_cell
;
662 bottomBorder
= max(bottomBorder
, cell
->member
.cell
.border
.bottom
.width
);
663 cell
= cell
->member
.cell
.prev_cell
;
665 bottomBorder
= ME_twips2pointsY(&c
, bottomBorder
);
666 cell
= ME_FindItemBack(item
, diCell
);
668 prevHeight
= cell
->member
.cell
.nHeight
;
669 nHeight
= cell
->member
.cell
.prev_cell
->member
.cell
.nHeight
+ bottomBorder
;
670 cell
->member
.cell
.nHeight
= nHeight
;
671 item
->member
.para
.nHeight
= nHeight
;
672 cell
= cell
->member
.cell
.prev_cell
;
673 cell
->member
.cell
.nHeight
= nHeight
;
674 while (cell
->member
.cell
.prev_cell
)
676 cell
= cell
->member
.cell
.prev_cell
;
677 cell
->member
.cell
.nHeight
= nHeight
;
679 /* Also set the height of the start row paragraph */
680 startRowPara
= ME_FindItemBack(cell
, diParagraph
);
681 startRowPara
->member
.para
.nHeight
= nHeight
;
682 c
.pt
.x
= startRowPara
->member
.para
.pt
.x
;
683 c
.pt
.y
= cell
->member
.cell
.pt
.y
+ nHeight
;
684 if (prevHeight
< nHeight
)
686 /* The height of the cells has grown, so invalidate the bottom of
688 ME_MarkRepaintEnd(item
, &repaint_start
, &repaint_end
);
689 cell
= ME_FindItemBack(item
, diCell
);
691 ME_MarkRepaintEnd(ME_FindItemBack(cell
, diParagraph
), &repaint_start
, &repaint_end
);
692 cell
= cell
->member
.cell
.prev_cell
;
696 else if (item
->member
.para
.pCell
&&
697 item
->member
.para
.pCell
!= item
->member
.para
.next_para
->member
.para
.pCell
)
699 /* The next paragraph is in the next cell in the table row. */
700 ME_Cell
*cell
= &item
->member
.para
.pCell
->member
.cell
;
701 cell
->nHeight
= c
.pt
.y
+ item
->member
.para
.nHeight
- cell
->pt
.y
;
703 /* Propagate the largest height to the end so that it can be easily
704 * sent back to all the cells at the end of the row. */
706 cell
->nHeight
= max(cell
->nHeight
, cell
->prev_cell
->member
.cell
.nHeight
);
708 c
.pt
.x
= cell
->pt
.x
+ cell
->nWidth
;
710 cell
->next_cell
->member
.cell
.pt
= c
.pt
;
711 if (!(item
->member
.para
.next_para
->member
.para
.nFlags
& MEPF_ROWEND
))
712 c
.pt
.y
+= cell
->yTextOffset
;
716 if (item
->member
.para
.pCell
) {
717 /* Next paragraph in the same cell. */
718 c
.pt
.x
= item
->member
.para
.pCell
->member
.cell
.pt
.x
;
720 /* Normal paragraph */
723 c
.pt
.y
+= item
->member
.para
.nHeight
;
726 totalWidth
= max(totalWidth
, item
->member
.para
.nWidth
);
727 item
= item
->member
.para
.next_para
;
729 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
730 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
732 editor
->nTotalLength
= c
.pt
.y
;
733 editor
->nTotalWidth
= totalWidth
;
734 editor
->pBuffer
->pLast
->member
.para
.pt
.x
= 0;
735 editor
->pBuffer
->pLast
->member
.para
.pt
.y
= c
.pt
.y
;
737 ME_DestroyContext(&c
);
739 if (repaint_start
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
740 ME_InvalidateParagraphRange(editor
, repaint_start
, repaint_end
);
741 return !!repaint_start
;
744 void ME_InvalidateParagraphRange(ME_TextEditor
*editor
,
745 ME_DisplayItem
*start_para
,
746 ME_DisplayItem
*last_para
)
752 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
754 ofs
= editor
->vert_si
.nPos
;
757 start_para
= ME_GetOuterParagraph(start_para
);
758 last_para
= ME_GetOuterParagraph(last_para
);
759 rc
.top
= c
.rcView
.top
+ start_para
->member
.para
.pt
.y
- ofs
;
761 rc
.top
= c
.rcView
.top
+ editor
->nTotalLength
- ofs
;
763 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
764 rc
.bottom
= c
.rcView
.top
+ editor
->nLastTotalLength
- ofs
;
766 rc
.bottom
= c
.rcView
.top
+ last_para
->member
.para
.pt
.y
+ last_para
->member
.para
.nHeight
- ofs
;
767 ITextHost_TxInvalidateRect(editor
->texthost
, &rc
, TRUE
);
769 ME_DestroyContext(&c
);
774 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
776 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
780 ITextHost_TxGetClientRect(editor
->texthost
, &rc
);
782 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
786 info
.nmhdr
.hwndFrom
= NULL
;
787 info
.nmhdr
.idFrom
= 0;
788 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
790 info
.rc
.right
= editor
->nTotalWidth
;
791 info
.rc
.bottom
= editor
->nTotalLength
;
793 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
794 ITextHost_TxNotify(editor
->texthost
, info
.nmhdr
.code
, &info
);
795 editor
->nEventMask
|= ENM_REQUESTRESIZE
;