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 wc
->nAvailWidth
= wc
->nTotalWidth
- (wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
) - wc
->nRightMargin
;
54 static void ME_InsertRowStart(ME_WrapContext
*wc
, const ME_DisplayItem
*pEnd
)
56 ME_DisplayItem
*p
, *row
, *para
;
57 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
59 para
= ME_GetParagraph(wc
->pRowStart
);
60 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
62 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
63 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
64 if (p
->member
.run
.nAscent
>ascent
)
65 ascent
= p
->member
.run
.nAscent
;
66 if (p
->member
.run
.nDescent
>descent
)
67 descent
= p
->member
.run
.nDescent
;
68 if (!(p
->member
.run
.nFlags
& (MERF_ENDPARA
|MERF_SKIPPED
)))
69 width
+= p
->member
.run
.nWidth
;
72 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
73 row
->member
.row
.nYPos
= wc
->pt
.y
;
74 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
75 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
76 assert(para
->member
.para
.pFmt
->dwMask
& PFM_ALIGNMENT
);
77 align
= para
->member
.para
.pFmt
->wAlignment
;
78 if (align
== PFA_CENTER
)
79 shift
= (wc
->nAvailWidth
-width
)/2;
80 if (align
== PFA_RIGHT
)
81 shift
= wc
->nAvailWidth
-width
;
82 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
84 if (p
->type
==diRun
) { /* FIXME add more run types */
85 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
88 ME_InsertBefore(wc
->pRowStart
, row
);
90 wc
->pt
.y
+= ascent
+descent
;
94 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
97 ME_InsertRowStart(wc
, p
->next
);
100 p = p->member.para.prev_para->next;
102 if (p->type == diParagraph || p->type == diTextEnd)
104 if (p->type == diRun)
106 ME_Run *run = &p->member.run;
107 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
114 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
116 /* FIXME compose style (out of character and paragraph styles) here */
118 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
120 ME_CalcRunExtent(wc
->context
, &ME_GetParagraph(p
)->member
.para
, &p
->member
.run
);
123 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
125 ME_DisplayItem
*pp
, *piter
= p
;
129 j
= ME_ReverseFindNonWhitespaceV(p
->member
.run
.strText
, i
);
131 pp
= ME_SplitRun(wc
->context
, piter
, j
);
132 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
138 /* omit all spaces before split point */
139 while(piter
!= wc
->pRowStart
)
141 piter
= ME_FindItemBack(piter
, diRun
);
142 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
147 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
149 j
= ME_ReverseFindNonWhitespaceV(piter
->member
.run
.strText
, i
);
150 pp
= ME_SplitRun(wc
->context
, piter
, i
);
151 wc
->pt
= pp
->member
.run
.pt
;
154 /* this run is the end of spaces, so the run edge is a good point to split */
155 wc
->pt
= pp
->member
.run
.pt
;
156 wc
->bOverflown
= TRUE
;
157 TRACE("Split point is: %s|%s\n", debugstr_w(piter
->member
.run
.strText
->szData
), debugstr_w(pp
->member
.run
.strText
->szData
));
160 wc
->pt
= piter
->member
.run
.pt
;
165 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
167 ME_DisplayItem
*piter
= p
, *pp
;
169 ME_Run
*run
= &p
->member
.run
;
171 idesp
= i
= ME_CharFromPoint(wc
->context
, loc
, run
);
172 len
= ME_StrVLen(run
->strText
);
176 /* don't split words */
177 i
= ME_ReverseFindWhitespaceV(run
->strText
, i
);
178 pp
= ME_MaximizeSplit(wc
, p
, i
);
182 TRACE("Must backtrack to split at: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
183 if (wc
->pLastSplittableRun
)
185 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
187 wc
->pt
= wc
->ptLastSplittableRun
;
188 return wc
->pLastSplittableRun
;
190 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
192 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
193 they serve no other purpose */
194 ME_UpdateRunFlags(wc
->context
->editor
, run
);
195 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
197 piter
= wc
->pLastSplittableRun
;
198 run
= &piter
->member
.run
;
199 len
= ME_StrVLen(run
->strText
);
200 /* don't split words */
201 i
= ME_ReverseFindWhitespaceV(run
->strText
, len
);
203 i
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
205 ME_DisplayItem
*piter2
= ME_SplitRun(wc
->context
, piter
, i
);
206 wc
->pt
= piter2
->member
.run
.pt
;
209 /* splittable = must have whitespaces */
210 assert(0 == "Splittable, but no whitespaces");
214 /* restart from the first run beginning with spaces */
215 wc
->pt
= wc
->ptLastSplittableRun
;
216 return wc
->pLastSplittableRun
;
219 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
220 /* OK, no better idea, so assume we MAY split words if we can split at all*/
222 return ME_SplitRun(wc
->context
, piter
, idesp
);
224 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
226 /* don't need to break current run, because it's possible to split
228 wc
->bOverflown
= TRUE
;
233 /* split point inside first character - no choice but split after that char */
235 int pos2
= ME_StrRelPos(run
->strText
, 0, &chars
);
237 /* the run is more than 1 char, so we may split */
238 return ME_SplitRun(wc
->context
, piter
, pos2
);
240 /* the run is one char, can't split it */
245 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
251 assert(p
->type
== diRun
);
254 run
= &p
->member
.run
;
255 run
->pt
.x
= wc
->pt
.x
;
256 run
->pt
.y
= wc
->pt
.y
;
257 ME_WrapSizeRun(wc
, p
);
258 len
= ME_StrVLen(run
->strText
);
260 if (wc
->bOverflown
) /* just skipping final whitespaces */
262 if (run
->nFlags
& (MERF_WHITESPACE
|MERF_TAB
)) {
263 p
->member
.run
.nFlags
|= MERF_SKIPPED
;
264 /* wc->pt.x += run->nWidth; */
265 /* skip runs consisting of only whitespaces */
269 if (run
->nFlags
& MERF_STARTWHITE
) {
270 /* try to split the run at the first non-white char */
272 black
= ME_FindNonWhitespaceV(run
->strText
, 0);
274 wc
->bOverflown
= FALSE
;
275 pp
= ME_SplitRun(wc
->context
, p
, black
);
276 p
->member
.run
.nFlags
|= MERF_SKIPPED
;
277 ME_InsertRowStart(wc
, pp
);
281 /* black run: the row goes from pRowStart to the previous run */
282 ME_InsertRowStart(wc
, p
);
285 /* we're not at the end of the row */
286 /* will current run fit? */
287 if (wc
->pt
.x
+ run
->nWidth
> wc
->nAvailWidth
)
289 int loc
= wc
->nAvailWidth
- wc
->pt
.x
;
290 /* total white run ? */
291 if (run
->nFlags
& MERF_WHITESPACE
) {
292 /* let the overflow logic handle it */
293 wc
->bOverflown
= TRUE
;
296 /* TAB: we can split before */
297 if (run
->nFlags
& MERF_TAB
) {
298 wc
->bOverflown
= TRUE
;
301 /* graphics: we can split before, if run's width is smaller than row's width */
302 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
303 wc
->bOverflown
= TRUE
;
306 /* can we separate out the last spaces ? (to use overflow logic later) */
307 if (run
->nFlags
& MERF_ENDWHITE
)
309 /* we aren't sure if it's *really* necessary, it's a good start however */
310 int black
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
311 ME_SplitRun(wc
->context
, p
, black
);
312 /* handle both parts again */
315 /* determine the split point by backtracking */
316 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
317 if (pp
== wc
->pRowStart
)
319 /* we had only spaces so far, entire content can be omitted */
323 if (p
!= pp
) /* found a suitable split point */
325 wc
->bOverflown
= TRUE
;
328 /* we detected that it's best to split on start of this run */
332 /* not found anything - writing over margins is the only option left */
334 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
335 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
337 wc
->pLastSplittableRun
= p
;
338 wc
->ptLastSplittableRun
= wc
->pt
;
340 wc
->pt
.x
+= run
->nWidth
;
344 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
);
346 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
, DWORD beginofs
) {
352 assert(tp
->type
== diParagraph
);
353 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
356 ME_PrepareParagraphForWrapping(c
, tp
);
359 /* wc.para_style = tp->member.para.style; */
361 wc
.nFirstMargin
= ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxStartIndent
) + beginofs
;
362 wc
.nLeftMargin
= wc
.nFirstMargin
+ ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxOffset
) + beginofs
;
363 wc
.nRightMargin
= ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxRightIndent
);
367 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEBEFORE
)
368 wc
.pt
.y
+= ME_twips2pointsY(c
, tp
->member
.para
.pFmt
->dySpaceBefore
);
369 if (tp
->member
.para
.pFmt
->dwMask
& PFM_BORDER
)
371 border
= ME_GetParaBorderWidth(c
->editor
, tp
->member
.para
.pFmt
->wBorders
);
372 if (tp
->member
.para
.pFmt
->wBorders
& 1) {
373 wc
.nFirstMargin
+= border
;
374 wc
.nLeftMargin
+= border
;
376 if (tp
->member
.para
.pFmt
->wBorders
& 2)
377 wc
.nRightMargin
-= border
;
378 if (tp
->member
.para
.pFmt
->wBorders
& 4)
382 wc
.nTotalWidth
= c
->rcView
.right
- c
->rcView
.left
;
383 wc
.nAvailWidth
= wc
.nTotalWidth
- wc
.nFirstMargin
- wc
.nRightMargin
;
386 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
389 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
390 assert(p
->type
!= diStartRow
);
391 if (p
->type
== diRun
) {
392 p
= ME_WrapHandleRun(&wc
, p
);
395 if (wc
.nRow
&& p
== wc
.pRowStart
)
396 wc
.pt
.y
+= linespace
;
398 ME_WrapEndParagraph(&wc
, p
);
399 if ((tp
->member
.para
.pFmt
->dwMask
& PFM_BORDER
) && (tp
->member
.para
.pFmt
->wBorders
& 8))
401 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEAFTER
)
402 wc
.pt
.y
+= ME_twips2pointsY(c
, tp
->member
.para
.pFmt
->dySpaceAfter
);
404 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
405 tp
->member
.para
.nHeight
= wc
.pt
.y
;
406 tp
->member
.para
.nRows
= wc
.nRow
;
410 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
411 ME_DisplayItem
*p
, *pRow
;
413 /* remove all items that will be reinserted by paragraph wrapper anyway */
414 tp
->member
.para
.nRows
= 0;
415 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; p
= p
->next
) {
421 ME_DestroyDisplayItem(pRow
);
427 /* join runs that can be joined, set up flags */
428 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; p
= p
->next
) {
431 case diStartRow
: assert(0); break; /* should have deleted it */
433 while (p
->next
->type
== diRun
) { /* FIXME */
434 if (ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
435 ME_JoinRuns(c
->editor
, p
);
441 p
->member
.run
.nFlags
&= ~MERF_CALCBYWRAP
;
449 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
) {
450 HWND hWnd
= editor
->hWnd
;
451 HDC hDC
= GetDC(hWnd
);
452 ME_DisplayItem
*item
;
454 BOOL bModified
= FALSE
;
455 int yStart
= -1, yEnd
= -1;
457 ME_InitContext(&c
, editor
, hDC
);
461 item
= editor
->pBuffer
->pFirst
->next
;
462 while(item
!= editor
->pBuffer
->pLast
) {
463 BOOL bRedraw
= FALSE
;
465 assert(item
->type
== diParagraph
);
466 editor
->nHeight
= max(editor
->nHeight
, item
->member
.para
.nYPos
);
467 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
468 || (item
->member
.para
.nYPos
!= c
.pt
.y
))
470 item
->member
.para
.nYPos
= c
.pt
.y
;
472 ME_WrapTextParagraph(&c
, item
, editor
->selofs
);
476 item
->member
.para
.nFlags
|= MEPF_REPAINT
;
481 bModified
= bModified
| bRedraw
;
483 c
.pt
.y
+= item
->member
.para
.nHeight
;
486 item
= item
->member
.para
.next_para
;
488 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
489 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
491 editor
->nTotalLength
= c
.pt
.y
;
493 ME_DestroyContext(&c
);
494 ReleaseDC(hWnd
, hDC
);
496 if (bModified
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
497 ME_InvalidateMarkedParagraphs(editor
);
501 void ME_InvalidateMarkedParagraphs(ME_TextEditor
*editor
) {
503 HDC hDC
= GetDC(editor
->hWnd
);
505 ME_InitContext(&c
, editor
, hDC
);
509 int ofs
= ME_GetYScrollPos(editor
);
511 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
;
512 while(item
!= editor
->pBuffer
->pLast
) {
513 if (item
->member
.para
.nFlags
& MEPF_REPAINT
) {
514 rc
.top
= item
->member
.para
.nYPos
- ofs
;
515 rc
.bottom
= item
->member
.para
.nYPos
+ item
->member
.para
.nHeight
- ofs
;
516 InvalidateRect(editor
->hWnd
, &rc
, TRUE
);
518 item
= item
->member
.para
.next_para
;
520 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
522 rc
.top
= editor
->nTotalLength
- ofs
;
523 rc
.bottom
= editor
->nLastTotalLength
- ofs
;
524 InvalidateRect(editor
->hWnd
, &rc
, TRUE
);
527 ME_DestroyContext(&c
);
528 ReleaseDC(editor
->hWnd
, hDC
);
533 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
535 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
539 GetClientRect(editor
->hWnd
, &rc
);
541 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
545 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
546 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
547 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
549 info
.rc
.bottom
= editor
->nTotalLength
;
551 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
552 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,
553 info
.nmhdr
.idFrom
, (LPARAM
)&info
);
554 editor
->nEventMask
|= ENM_REQUESTRESIZE
;