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 wc
->bOverflown
= FALSE
;
49 wc
->pLastSplittableRun
= NULL
;
50 if (wc
->context
->editor
->bWordWrap
)
51 wc
->nAvailWidth
= wc
->context
->rcView
.right
- wc
->context
->rcView
.left
-
52 (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
54 wc
->nAvailWidth
= ~0u >> 1;
58 static void ME_InsertRowStart(ME_WrapContext
*wc
, const ME_DisplayItem
*pEnd
)
60 ME_DisplayItem
*p
, *row
, *para
;
61 BOOL bSkippingSpaces
= TRUE
;
62 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
64 para
= ME_GetParagraph(wc
->pRowStart
);
66 for (p
= pEnd
->prev
; p
!=wc
->pRowStart
->prev
; p
= p
->prev
)
68 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
69 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
70 if (p
->member
.run
.nAscent
>ascent
)
71 ascent
= p
->member
.run
.nAscent
;
72 if (p
->member
.run
.nDescent
>descent
)
73 descent
= p
->member
.run
.nDescent
;
76 /* Exclude space characters from run width.
77 * Other whitespace or delimiters are not treated this way. */
79 int len
= p
->member
.run
.strText
->nLen
;
80 WCHAR
*text
= p
->member
.run
.strText
->szData
+ len
- 1;
83 while (len
&& *(text
--) == ' ')
87 if (len
== p
->member
.run
.strText
->nLen
)
89 width
+= p
->member
.run
.nWidth
;
91 sz
= ME_GetRunSize(wc
->context
, ¶
->member
.para
,
92 &p
->member
.run
, len
, p
->member
.run
.pt
.x
);
96 bSkippingSpaces
= !len
;
97 } else if (!(p
->member
.run
.nFlags
& MERF_ENDPARA
))
98 width
+= p
->member
.run
.nWidth
;
102 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
103 row
->member
.row
.nYPos
= wc
->pt
.y
;
104 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
105 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
106 assert(para
->member
.para
.pFmt
->dwMask
& PFM_ALIGNMENT
);
107 align
= para
->member
.para
.pFmt
->wAlignment
;
108 if (align
== PFA_CENTER
)
109 shift
= (wc
->nAvailWidth
-width
)/2;
110 if (align
== PFA_RIGHT
)
111 shift
= wc
->nAvailWidth
-width
;
112 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
114 if (p
->type
==diRun
) { /* FIXME add more run types */
115 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
118 ME_InsertBefore(wc
->pRowStart
, row
);
120 wc
->pt
.y
+= ascent
+descent
;
124 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
127 ME_InsertRowStart(wc
, p
);
130 p = p->member.para.prev_para->next;
132 if (p->type == diParagraph || p->type == diTextEnd)
134 if (p->type == diRun)
136 ME_Run *run = &p->member.run;
137 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
144 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
146 /* FIXME compose style (out of character and paragraph styles) here */
148 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
150 ME_CalcRunExtent(wc
->context
, &ME_GetParagraph(p
)->member
.para
,
151 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, &p
->member
.run
);
154 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
156 ME_DisplayItem
*pp
, *piter
= p
;
160 j
= ME_ReverseFindNonWhitespaceV(p
->member
.run
.strText
, i
);
162 pp
= ME_SplitRun(wc
, piter
, j
);
163 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
169 /* omit all spaces before split point */
170 while(piter
!= wc
->pRowStart
)
172 piter
= ME_FindItemBack(piter
, diRun
);
173 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
178 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
180 j
= ME_ReverseFindNonWhitespaceV(piter
->member
.run
.strText
, i
);
181 pp
= ME_SplitRun(wc
, piter
, i
);
182 wc
->pt
= pp
->member
.run
.pt
;
185 /* this run is the end of spaces, so the run edge is a good point to split */
186 wc
->pt
= pp
->member
.run
.pt
;
187 wc
->bOverflown
= TRUE
;
188 TRACE("Split point is: %s|%s\n", debugstr_w(piter
->member
.run
.strText
->szData
), debugstr_w(pp
->member
.run
.strText
->szData
));
191 wc
->pt
= piter
->member
.run
.pt
;
196 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
198 ME_DisplayItem
*piter
= p
, *pp
;
200 ME_Run
*run
= &p
->member
.run
;
202 idesp
= i
= ME_CharFromPoint(wc
->context
, loc
, run
);
203 len
= ME_StrVLen(run
->strText
);
207 /* don't split words */
208 i
= ME_ReverseFindWhitespaceV(run
->strText
, i
);
209 pp
= ME_MaximizeSplit(wc
, p
, i
);
213 TRACE("Must backtrack to split at: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
214 if (wc
->pLastSplittableRun
)
216 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
218 wc
->pt
= wc
->ptLastSplittableRun
;
219 return wc
->pLastSplittableRun
;
221 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
223 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
224 they serve no other purpose */
225 ME_UpdateRunFlags(wc
->context
->editor
, run
);
226 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
228 piter
= wc
->pLastSplittableRun
;
229 run
= &piter
->member
.run
;
230 len
= ME_StrVLen(run
->strText
);
231 /* don't split words */
232 i
= ME_ReverseFindWhitespaceV(run
->strText
, len
);
234 i
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
236 ME_DisplayItem
*piter2
= ME_SplitRun(wc
, piter
, i
);
237 wc
->pt
= piter2
->member
.run
.pt
;
240 /* splittable = must have whitespaces */
241 assert(0 == "Splittable, but no whitespaces");
245 /* restart from the first run beginning with spaces */
246 wc
->pt
= wc
->ptLastSplittableRun
;
247 return wc
->pLastSplittableRun
;
250 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
251 /* OK, no better idea, so assume we MAY split words if we can split at all*/
253 return ME_SplitRun(wc
, piter
, idesp
);
255 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
257 /* don't need to break current run, because it's possible to split
259 wc
->bOverflown
= TRUE
;
264 /* split point inside first character - no choice but split after that char */
266 int pos2
= ME_StrRelPos(run
->strText
, 0, &chars
);
268 /* the run is more than 1 char, so we may split */
269 return ME_SplitRun(wc
, piter
, pos2
);
271 /* the run is one char, can't split it */
276 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
282 assert(p
->type
== diRun
);
285 run
= &p
->member
.run
;
286 run
->pt
.x
= wc
->pt
.x
;
287 run
->pt
.y
= wc
->pt
.y
;
288 ME_WrapSizeRun(wc
, p
);
289 len
= ME_StrVLen(run
->strText
);
291 if (wc
->bOverflown
) /* just skipping final whitespaces */
293 if (run
->nFlags
& (MERF_WHITESPACE
|MERF_TAB
)) {
294 p
->member
.run
.nFlags
|= MERF_SKIPPED
;
295 /* wc->pt.x += run->nWidth; */
296 /* skip runs consisting of only whitespaces */
300 if (run
->nFlags
& MERF_STARTWHITE
) {
301 /* try to split the run at the first non-white char */
303 black
= ME_FindNonWhitespaceV(run
->strText
, 0);
305 wc
->bOverflown
= FALSE
;
306 pp
= ME_SplitRun(wc
, p
, black
);
307 p
->member
.run
.nFlags
|= MERF_SKIPPED
;
308 ME_InsertRowStart(wc
, pp
);
312 /* black run: the row goes from pRowStart to the previous run */
313 ME_InsertRowStart(wc
, p
);
316 /* simply end the current row and move on to next one */
317 if (run
->nFlags
& MERF_ENDROW
)
320 ME_InsertRowStart(wc
, p
);
323 /* we're not at the end of the row */
324 if (run
->nFlags
& MERF_TAB
) {
325 /* force recomputation of tabs' size as it depends on position */
326 ME_CalcRunExtent(wc
->context
, &ME_GetParagraph(p
)->member
.para
,
327 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, run
);
330 /* will current run fit? */
331 if (wc
->pt
.x
+ run
->nWidth
> wc
->nAvailWidth
)
333 int loc
= wc
->nAvailWidth
- wc
->pt
.x
;
334 /* total white run ? */
335 if (run
->nFlags
& MERF_WHITESPACE
) {
336 /* let the overflow logic handle it */
337 wc
->bOverflown
= TRUE
;
340 /* TAB: we can split before */
341 if (run
->nFlags
& MERF_TAB
) {
342 wc
->bOverflown
= TRUE
;
345 /* graphics: we can split before, if run's width is smaller than row's width */
346 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
347 wc
->bOverflown
= TRUE
;
350 /* can we separate out the last spaces ? (to use overflow logic later) */
351 if (run
->nFlags
& MERF_ENDWHITE
)
353 /* we aren't sure if it's *really* necessary, it's a good start however */
354 int black
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
355 ME_SplitRun(wc
, p
, black
);
356 /* handle both parts again */
359 /* determine the split point by backtracking */
360 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
361 if (pp
== wc
->pRowStart
)
363 /* we had only spaces so far, entire content can be omitted */
367 if (p
!= pp
) /* found a suitable split point */
369 wc
->bOverflown
= TRUE
;
372 /* we detected that it's best to split on start of this run */
376 /* not found anything - writing over margins is the only option left */
378 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
379 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
381 wc
->pLastSplittableRun
= p
;
382 wc
->ptLastSplittableRun
= wc
->pt
;
384 wc
->pt
.x
+= run
->nWidth
;
388 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
);
390 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
, DWORD beginofs
) {
396 assert(tp
->type
== diParagraph
);
397 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
400 ME_PrepareParagraphForWrapping(c
, tp
);
403 /* wc.para_style = tp->member.para.style; */
405 wc
.nFirstMargin
= ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxStartIndent
) + beginofs
;
406 wc
.nLeftMargin
= wc
.nFirstMargin
+ ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxOffset
);
407 wc
.nRightMargin
= ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxRightIndent
);
411 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEBEFORE
)
412 wc
.pt
.y
+= ME_twips2pointsY(c
, tp
->member
.para
.pFmt
->dySpaceBefore
);
413 if (tp
->member
.para
.pFmt
->dwMask
& PFM_BORDER
)
415 border
= ME_GetParaBorderWidth(c
->editor
, tp
->member
.para
.pFmt
->wBorders
);
416 if (tp
->member
.para
.pFmt
->wBorders
& 1) {
417 wc
.nFirstMargin
+= border
;
418 wc
.nLeftMargin
+= border
;
420 if (tp
->member
.para
.pFmt
->wBorders
& 2)
421 wc
.nRightMargin
-= border
;
422 if (tp
->member
.para
.pFmt
->wBorders
& 4)
426 if (c
->editor
->bWordWrap
)
427 wc
.nAvailWidth
= c
->rcView
.right
- c
->rcView
.left
- wc
.nFirstMargin
- wc
.nRightMargin
;
429 wc
.nAvailWidth
= ~0u >> 1;
432 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
435 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
436 assert(p
->type
!= diStartRow
);
437 if (p
->type
== diRun
) {
438 p
= ME_WrapHandleRun(&wc
, p
);
441 if (wc
.nRow
&& p
== wc
.pRowStart
)
442 wc
.pt
.y
+= linespace
;
444 ME_WrapEndParagraph(&wc
, p
);
445 if ((tp
->member
.para
.pFmt
->dwMask
& PFM_BORDER
) && (tp
->member
.para
.pFmt
->wBorders
& 8))
447 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEAFTER
)
448 wc
.pt
.y
+= ME_twips2pointsY(c
, tp
->member
.para
.pFmt
->dySpaceAfter
);
450 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
451 tp
->member
.para
.nHeight
= wc
.pt
.y
;
452 tp
->member
.para
.nRows
= wc
.nRow
;
456 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
457 ME_DisplayItem
*p
, *pRow
;
459 /* remove all items that will be reinserted by paragraph wrapper anyway */
460 tp
->member
.para
.nRows
= 0;
461 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; p
= p
->next
) {
467 ME_DestroyDisplayItem(pRow
);
473 /* join runs that can be joined, set up flags */
474 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; p
= p
->next
) {
477 case diStartRow
: assert(0); break; /* should have deleted it */
479 while (p
->next
->type
== diRun
) { /* FIXME */
480 if (ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
481 ME_JoinRuns(c
->editor
, p
);
487 p
->member
.run
.nFlags
&= ~MERF_CALCBYWRAP
;
495 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
) {
496 ME_DisplayItem
*item
;
498 BOOL bModified
= FALSE
;
502 ME_InitContext(&c
, editor
, GetDC(editor
->hWnd
));
504 item
= editor
->pBuffer
->pFirst
->next
;
505 while(item
!= editor
->pBuffer
->pLast
) {
506 BOOL bRedraw
= FALSE
;
508 assert(item
->type
== diParagraph
);
509 editor
->nHeight
= max(editor
->nHeight
, item
->member
.para
.nYPos
);
510 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
511 || (item
->member
.para
.nYPos
!= c
.pt
.y
))
513 item
->member
.para
.nYPos
= c
.pt
.y
;
515 ME_WrapTextParagraph(&c
, item
, editor
->selofs
);
519 item
->member
.para
.nFlags
|= MEPF_REPAINT
;
524 bModified
= bModified
| bRedraw
;
527 c
.pt
.y
+= item
->member
.para
.nHeight
;
528 item
= item
->member
.para
.next_para
;
530 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
531 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
533 editor
->nTotalLength
= c
.pt
.y
;
534 editor
->pBuffer
->pLast
->member
.para
.nYPos
= yLastPos
;
536 ME_DestroyContext(&c
, editor
->hWnd
);
538 /* Each paragraph may contain multiple rows, which should be scrollable, even
539 if the containing paragraph has nYPos == 0 */
540 item
= editor
->pBuffer
->pFirst
;
541 while ((item
= ME_FindItemFwd(item
, diStartRow
)) != NULL
) {
542 assert(item
->type
== diStartRow
);
543 editor
->nHeight
= max(editor
->nHeight
, item
->member
.row
.nYPos
);
546 if (bModified
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
547 ME_InvalidateMarkedParagraphs(editor
);
551 void ME_InvalidateMarkedParagraphs(ME_TextEditor
*editor
) {
554 ME_InitContext(&c
, editor
, GetDC(editor
->hWnd
));
558 int ofs
= ME_GetYScrollPos(editor
);
560 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
;
561 while(item
!= editor
->pBuffer
->pLast
) {
562 if (item
->member
.para
.nFlags
& MEPF_REPAINT
) {
563 rc
.top
= item
->member
.para
.nYPos
- ofs
;
564 rc
.bottom
= item
->member
.para
.nYPos
+ item
->member
.para
.nHeight
- ofs
;
565 InvalidateRect(editor
->hWnd
, &rc
, TRUE
);
567 item
= item
->member
.para
.next_para
;
569 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
571 rc
.top
= editor
->nTotalLength
- ofs
;
572 rc
.bottom
= editor
->nLastTotalLength
- ofs
;
573 InvalidateRect(editor
->hWnd
, &rc
, TRUE
);
576 ME_DestroyContext(&c
, editor
->hWnd
);
581 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
583 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
587 GetClientRect(editor
->hWnd
, &rc
);
589 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
593 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
594 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
595 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
597 info
.rc
.bottom
= editor
->nTotalLength
;
599 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
600 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,
601 info
.nmhdr
.idFrom
, (LPARAM
)&info
);
602 editor
->nEventMask
|= ENM_REQUESTRESIZE
;