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 int ascent
= 0, descent
= 0, width
=0, shift
= 0, align
= 0;
63 para
= ME_GetParagraph(wc
->pRowStart
);
64 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
66 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
67 if (p
->type
==diRun
&& ((p
==wc
->pRowStart
) || !(p
->member
.run
.nFlags
& MERF_ENDPARA
))) { /* FIXME add more run types */
68 if (p
->member
.run
.nAscent
>ascent
)
69 ascent
= p
->member
.run
.nAscent
;
70 if (p
->member
.run
.nDescent
>descent
)
71 descent
= p
->member
.run
.nDescent
;
72 if (!(p
->member
.run
.nFlags
& (MERF_ENDPARA
|MERF_SKIPPED
)))
73 width
+= p
->member
.run
.nWidth
;
76 row
= ME_MakeRow(ascent
+descent
, ascent
, width
);
77 row
->member
.row
.nYPos
= wc
->pt
.y
;
78 row
->member
.row
.nLMargin
= (!wc
->nRow
? wc
->nFirstMargin
: wc
->nLeftMargin
);
79 row
->member
.row
.nRMargin
= wc
->nRightMargin
;
80 assert(para
->member
.para
.pFmt
->dwMask
& PFM_ALIGNMENT
);
81 align
= para
->member
.para
.pFmt
->wAlignment
;
82 if (align
== PFA_CENTER
)
83 shift
= (wc
->nAvailWidth
-width
)/2;
84 if (align
== PFA_RIGHT
)
85 shift
= wc
->nAvailWidth
-width
;
86 for (p
= wc
->pRowStart
; p
!=pEnd
; p
= p
->next
)
88 if (p
->type
==diRun
) { /* FIXME add more run types */
89 p
->member
.run
.pt
.x
+= row
->member
.row
.nLMargin
+shift
;
92 ME_InsertBefore(wc
->pRowStart
, row
);
94 wc
->pt
.y
+= ascent
+descent
;
98 static void ME_WrapEndParagraph(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
101 ME_InsertRowStart(wc
, p
->next
);
104 p = p->member.para.prev_para->next;
106 if (p->type == diParagraph || p->type == diTextEnd)
108 if (p->type == diRun)
110 ME_Run *run = &p->member.run;
111 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
118 static void ME_WrapSizeRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
120 /* FIXME compose style (out of character and paragraph styles) here */
122 ME_UpdateRunFlags(wc
->context
->editor
, &p
->member
.run
);
124 ME_CalcRunExtent(wc
->context
, &ME_GetParagraph(p
)->member
.para
,
125 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, &p
->member
.run
);
128 static ME_DisplayItem
*ME_MaximizeSplit(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int i
)
130 ME_DisplayItem
*pp
, *piter
= p
;
134 j
= ME_ReverseFindNonWhitespaceV(p
->member
.run
.strText
, i
);
136 pp
= ME_SplitRun(wc
, piter
, j
);
137 wc
->pt
.x
+= piter
->member
.run
.nWidth
;
143 /* omit all spaces before split point */
144 while(piter
!= wc
->pRowStart
)
146 piter
= ME_FindItemBack(piter
, diRun
);
147 if (piter
->member
.run
.nFlags
& MERF_WHITESPACE
)
152 if (piter
->member
.run
.nFlags
& MERF_ENDWHITE
)
154 j
= ME_ReverseFindNonWhitespaceV(piter
->member
.run
.strText
, i
);
155 pp
= ME_SplitRun(wc
, piter
, i
);
156 wc
->pt
= pp
->member
.run
.pt
;
159 /* this run is the end of spaces, so the run edge is a good point to split */
160 wc
->pt
= pp
->member
.run
.pt
;
161 wc
->bOverflown
= TRUE
;
162 TRACE("Split point is: %s|%s\n", debugstr_w(piter
->member
.run
.strText
->szData
), debugstr_w(pp
->member
.run
.strText
->szData
));
165 wc
->pt
= piter
->member
.run
.pt
;
170 static ME_DisplayItem
*ME_SplitByBacktracking(ME_WrapContext
*wc
, ME_DisplayItem
*p
, int loc
)
172 ME_DisplayItem
*piter
= p
, *pp
;
174 ME_Run
*run
= &p
->member
.run
;
176 idesp
= i
= ME_CharFromPoint(wc
->context
, loc
, run
);
177 len
= ME_StrVLen(run
->strText
);
181 /* don't split words */
182 i
= ME_ReverseFindWhitespaceV(run
->strText
, i
);
183 pp
= ME_MaximizeSplit(wc
, p
, i
);
187 TRACE("Must backtrack to split at: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
188 if (wc
->pLastSplittableRun
)
190 if (wc
->pLastSplittableRun
->member
.run
.nFlags
& (MERF_GRAPHICS
|MERF_TAB
))
192 wc
->pt
= wc
->ptLastSplittableRun
;
193 return wc
->pLastSplittableRun
;
195 else if (wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
)
197 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
198 they serve no other purpose */
199 ME_UpdateRunFlags(wc
->context
->editor
, run
);
200 assert((wc
->pLastSplittableRun
->member
.run
.nFlags
& MERF_SPLITTABLE
));
202 piter
= wc
->pLastSplittableRun
;
203 run
= &piter
->member
.run
;
204 len
= ME_StrVLen(run
->strText
);
205 /* don't split words */
206 i
= ME_ReverseFindWhitespaceV(run
->strText
, len
);
208 i
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
210 ME_DisplayItem
*piter2
= ME_SplitRun(wc
, piter
, i
);
211 wc
->pt
= piter2
->member
.run
.pt
;
214 /* splittable = must have whitespaces */
215 assert(0 == "Splittable, but no whitespaces");
219 /* restart from the first run beginning with spaces */
220 wc
->pt
= wc
->ptLastSplittableRun
;
221 return wc
->pLastSplittableRun
;
224 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p
->member
.run
.strText
->szData
));
225 /* OK, no better idea, so assume we MAY split words if we can split at all*/
227 return ME_SplitRun(wc
, piter
, idesp
);
229 if (wc
->pRowStart
&& piter
!= wc
->pRowStart
)
231 /* don't need to break current run, because it's possible to split
233 wc
->bOverflown
= TRUE
;
238 /* split point inside first character - no choice but split after that char */
240 int pos2
= ME_StrRelPos(run
->strText
, 0, &chars
);
242 /* the run is more than 1 char, so we may split */
243 return ME_SplitRun(wc
, piter
, pos2
);
245 /* the run is one char, can't split it */
250 static ME_DisplayItem
*ME_WrapHandleRun(ME_WrapContext
*wc
, ME_DisplayItem
*p
)
256 assert(p
->type
== diRun
);
259 run
= &p
->member
.run
;
260 run
->pt
.x
= wc
->pt
.x
;
261 run
->pt
.y
= wc
->pt
.y
;
262 ME_WrapSizeRun(wc
, p
);
263 len
= ME_StrVLen(run
->strText
);
265 if (wc
->bOverflown
) /* just skipping final whitespaces */
267 if (run
->nFlags
& (MERF_WHITESPACE
|MERF_TAB
)) {
268 p
->member
.run
.nFlags
|= MERF_SKIPPED
;
269 /* wc->pt.x += run->nWidth; */
270 /* skip runs consisting of only whitespaces */
274 if (run
->nFlags
& MERF_STARTWHITE
) {
275 /* try to split the run at the first non-white char */
277 black
= ME_FindNonWhitespaceV(run
->strText
, 0);
279 wc
->bOverflown
= FALSE
;
280 pp
= ME_SplitRun(wc
, p
, black
);
281 p
->member
.run
.nFlags
|= MERF_SKIPPED
;
282 ME_InsertRowStart(wc
, pp
);
286 /* black run: the row goes from pRowStart to the previous run */
287 ME_InsertRowStart(wc
, p
);
290 /* simply end the current row and move on to next one */
291 if (run
->nFlags
& MERF_ENDROW
)
294 ME_InsertRowStart(wc
, p
);
297 /* we're not at the end of the row */
298 if (run
->nFlags
& MERF_TAB
) {
299 /* force recomputation of tabs' size as it depends on position */
300 ME_CalcRunExtent(wc
->context
, &ME_GetParagraph(p
)->member
.para
,
301 wc
->nRow
? wc
->nLeftMargin
: wc
->nFirstMargin
, run
);
304 /* will current run fit? */
305 if (wc
->pt
.x
+ run
->nWidth
> wc
->nAvailWidth
)
307 int loc
= wc
->nAvailWidth
- wc
->pt
.x
;
308 /* total white run ? */
309 if (run
->nFlags
& MERF_WHITESPACE
) {
310 /* let the overflow logic handle it */
311 wc
->bOverflown
= TRUE
;
314 /* TAB: we can split before */
315 if (run
->nFlags
& MERF_TAB
) {
316 wc
->bOverflown
= TRUE
;
319 /* graphics: we can split before, if run's width is smaller than row's width */
320 if ((run
->nFlags
& MERF_GRAPHICS
) && run
->nWidth
<= wc
->nAvailWidth
) {
321 wc
->bOverflown
= TRUE
;
324 /* can we separate out the last spaces ? (to use overflow logic later) */
325 if (run
->nFlags
& MERF_ENDWHITE
)
327 /* we aren't sure if it's *really* necessary, it's a good start however */
328 int black
= ME_ReverseFindNonWhitespaceV(run
->strText
, len
);
329 ME_SplitRun(wc
, p
, black
);
330 /* handle both parts again */
333 /* determine the split point by backtracking */
334 pp
= ME_SplitByBacktracking(wc
, p
, loc
);
335 if (pp
== wc
->pRowStart
)
337 /* we had only spaces so far, entire content can be omitted */
341 if (p
!= pp
) /* found a suitable split point */
343 wc
->bOverflown
= TRUE
;
346 /* we detected that it's best to split on start of this run */
350 /* not found anything - writing over margins is the only option left */
352 if ((run
->nFlags
& (MERF_SPLITTABLE
| MERF_STARTWHITE
))
353 || ((run
->nFlags
& (MERF_GRAPHICS
|MERF_TAB
)) && (p
!= wc
->pRowStart
)))
355 wc
->pLastSplittableRun
= p
;
356 wc
->ptLastSplittableRun
= wc
->pt
;
358 wc
->pt
.x
+= run
->nWidth
;
362 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
);
364 static void ME_WrapTextParagraph(ME_Context
*c
, ME_DisplayItem
*tp
, DWORD beginofs
) {
370 assert(tp
->type
== diParagraph
);
371 if (!(tp
->member
.para
.nFlags
& MEPF_REWRAP
)) {
374 ME_PrepareParagraphForWrapping(c
, tp
);
377 /* wc.para_style = tp->member.para.style; */
379 wc
.nFirstMargin
= ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxStartIndent
) + beginofs
;
380 wc
.nLeftMargin
= wc
.nFirstMargin
+ ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxOffset
) + beginofs
;
381 wc
.nRightMargin
= ME_twips2pointsX(c
, tp
->member
.para
.pFmt
->dxRightIndent
);
385 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEBEFORE
)
386 wc
.pt
.y
+= ME_twips2pointsY(c
, tp
->member
.para
.pFmt
->dySpaceBefore
);
387 if (tp
->member
.para
.pFmt
->dwMask
& PFM_BORDER
)
389 border
= ME_GetParaBorderWidth(c
->editor
, tp
->member
.para
.pFmt
->wBorders
);
390 if (tp
->member
.para
.pFmt
->wBorders
& 1) {
391 wc
.nFirstMargin
+= border
;
392 wc
.nLeftMargin
+= border
;
394 if (tp
->member
.para
.pFmt
->wBorders
& 2)
395 wc
.nRightMargin
-= border
;
396 if (tp
->member
.para
.pFmt
->wBorders
& 4)
400 if (c
->editor
->bWordWrap
)
401 wc
.nAvailWidth
= c
->rcView
.right
- c
->rcView
.left
- wc
.nFirstMargin
- wc
.nRightMargin
;
403 wc
.nAvailWidth
= ~0u >> 1;
406 linespace
= ME_GetParaLineSpace(c
, &tp
->member
.para
);
409 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; ) {
410 assert(p
->type
!= diStartRow
);
411 if (p
->type
== diRun
) {
412 p
= ME_WrapHandleRun(&wc
, p
);
415 if (wc
.nRow
&& p
== wc
.pRowStart
)
416 wc
.pt
.y
+= linespace
;
418 ME_WrapEndParagraph(&wc
, p
);
419 if ((tp
->member
.para
.pFmt
->dwMask
& PFM_BORDER
) && (tp
->member
.para
.pFmt
->wBorders
& 8))
421 if (tp
->member
.para
.pFmt
->dwMask
& PFM_SPACEAFTER
)
422 wc
.pt
.y
+= ME_twips2pointsY(c
, tp
->member
.para
.pFmt
->dySpaceAfter
);
424 tp
->member
.para
.nFlags
&= ~MEPF_REWRAP
;
425 tp
->member
.para
.nHeight
= wc
.pt
.y
;
426 tp
->member
.para
.nRows
= wc
.nRow
;
430 static void ME_PrepareParagraphForWrapping(ME_Context
*c
, ME_DisplayItem
*tp
) {
431 ME_DisplayItem
*p
, *pRow
;
433 /* remove all items that will be reinserted by paragraph wrapper anyway */
434 tp
->member
.para
.nRows
= 0;
435 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; p
= p
->next
) {
441 ME_DestroyDisplayItem(pRow
);
447 /* join runs that can be joined, set up flags */
448 for (p
= tp
->next
; p
!=tp
->member
.para
.next_para
; p
= p
->next
) {
451 case diStartRow
: assert(0); break; /* should have deleted it */
453 while (p
->next
->type
== diRun
) { /* FIXME */
454 if (ME_CanJoinRuns(&p
->member
.run
, &p
->next
->member
.run
)) {
455 ME_JoinRuns(c
->editor
, p
);
461 p
->member
.run
.nFlags
&= ~MERF_CALCBYWRAP
;
469 BOOL
ME_WrapMarkedParagraphs(ME_TextEditor
*editor
) {
470 HWND hWnd
= editor
->hWnd
;
471 HDC hDC
= GetDC(hWnd
);
472 ME_DisplayItem
*item
;
474 BOOL bModified
= FALSE
;
475 int yStart
= -1, yEnd
= -1;
477 ME_InitContext(&c
, editor
, hDC
);
481 item
= editor
->pBuffer
->pFirst
->next
;
482 while(item
!= editor
->pBuffer
->pLast
) {
483 BOOL bRedraw
= FALSE
;
485 assert(item
->type
== diParagraph
);
486 editor
->nHeight
= max(editor
->nHeight
, item
->member
.para
.nYPos
);
487 if ((item
->member
.para
.nFlags
& MEPF_REWRAP
)
488 || (item
->member
.para
.nYPos
!= c
.pt
.y
))
490 item
->member
.para
.nYPos
= c
.pt
.y
;
492 ME_WrapTextParagraph(&c
, item
, editor
->selofs
);
496 item
->member
.para
.nFlags
|= MEPF_REPAINT
;
501 bModified
= bModified
| bRedraw
;
503 c
.pt
.y
+= item
->member
.para
.nHeight
;
506 item
= item
->member
.para
.next_para
;
508 editor
->sizeWindow
.cx
= c
.rcView
.right
-c
.rcView
.left
;
509 editor
->sizeWindow
.cy
= c
.rcView
.bottom
-c
.rcView
.top
;
511 editor
->nTotalLength
= c
.pt
.y
;
513 ME_DestroyContext(&c
);
514 ReleaseDC(hWnd
, hDC
);
516 if (bModified
|| editor
->nTotalLength
< editor
->nLastTotalLength
)
517 ME_InvalidateMarkedParagraphs(editor
);
521 void ME_InvalidateMarkedParagraphs(ME_TextEditor
*editor
) {
523 HDC hDC
= GetDC(editor
->hWnd
);
525 ME_InitContext(&c
, editor
, hDC
);
529 int ofs
= ME_GetYScrollPos(editor
);
531 ME_DisplayItem
*item
= editor
->pBuffer
->pFirst
;
532 while(item
!= editor
->pBuffer
->pLast
) {
533 if (item
->member
.para
.nFlags
& MEPF_REPAINT
) {
534 rc
.top
= item
->member
.para
.nYPos
- ofs
;
535 rc
.bottom
= item
->member
.para
.nYPos
+ item
->member
.para
.nHeight
- ofs
;
536 InvalidateRect(editor
->hWnd
, &rc
, TRUE
);
538 item
= item
->member
.para
.next_para
;
540 if (editor
->nTotalLength
< editor
->nLastTotalLength
)
542 rc
.top
= editor
->nTotalLength
- ofs
;
543 rc
.bottom
= editor
->nLastTotalLength
- ofs
;
544 InvalidateRect(editor
->hWnd
, &rc
, TRUE
);
547 ME_DestroyContext(&c
);
548 ReleaseDC(editor
->hWnd
, hDC
);
553 ME_SendRequestResize(ME_TextEditor
*editor
, BOOL force
)
555 if (editor
->nEventMask
& ENM_REQUESTRESIZE
)
559 GetClientRect(editor
->hWnd
, &rc
);
561 if (force
|| rc
.bottom
!= editor
->nTotalLength
)
565 info
.nmhdr
.hwndFrom
= editor
->hWnd
;
566 info
.nmhdr
.idFrom
= GetWindowLongW(editor
->hWnd
, GWLP_ID
);
567 info
.nmhdr
.code
= EN_REQUESTRESIZE
;
569 info
.rc
.bottom
= editor
->nTotalLength
;
571 editor
->nEventMask
&= ~ENM_REQUESTRESIZE
;
572 SendMessageW(GetParent(editor
->hWnd
), WM_NOTIFY
,
573 info
.nmhdr
.idFrom
, (LPARAM
)&info
);
574 editor
->nEventMask
|= ENM_REQUESTRESIZE
;