1 /*
2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
3 * warned !
4 *
5 * Copyright 2004 by Krzysztof Foltman
6 *
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.
11 *
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.
16 *
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
20 */
21
22
23 #include "editor.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26
27 /*
28 * Unsolved problems:
29 *
30 * - center and right align in WordPad omits all spaces at the start, we don't
31 * - objects/images are not handled yet
32 * - no tabs
33 */
34
35 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
36 {
37 ME_DisplayItem *item = ME_MakeDI(diStartRow);
38
39 item->member.row.nHeight = height;
40 item->member.row.nBaseline = baseline;
41 item->member.row.nWidth = width;
42 return item;
43 }
44
45 static void ME_BeginRow(ME_WrapContext *wc)
46 {
47 wc->pRowStart = NULL;
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;
53 else
54 wc->nAvailWidth = ~0u >> 1;
55 wc->pt.x = 0;
56 }
57
58 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
59 {
60 ME_DisplayItem *p, *row, *para;
61 int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
62 /* wrap text */
63 para = ME_GetParagraph(wc->pRowStart);
64 for (p = wc->pRowStart; p!=pEnd; p = p->next)
65 {
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;
74 }
75 }
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)
87 {
88 if (p->type==diRun) { /* FIXME add more run types */
89 p->member.run.pt.x += row->member.row.nLMargin+shift;
90 }
91 }
92 ME_InsertBefore(wc->pRowStart, row);
93 wc->nRow++;
94 wc->pt.y += ascent+descent;
95 ME_BeginRow(wc);
96 }
97
98 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
99 {
100 if (wc->pRowStart)
101 ME_InsertRowStart(wc, p->next);
102
103 /*
104 p = p->member.para.prev_para->next;
105 while(p) {
106 if (p->type == diParagraph || p->type == diTextEnd)
107 return;
108 if (p->type == diRun)
109 {
110 ME_Run *run = &p->member.run;
111 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
112 }
113 p = p->next;
114 }
115 */
116 }
117
118 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
119 {
120 /* FIXME compose style (out of character and paragraph styles) here */
121
122 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
123
124 ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
125 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
126 }
127
128 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
129 {
130 ME_DisplayItem *pp, *piter = p;
131 int j;
132 if (!i)
133 return NULL;
134 j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
135 if (j>0) {
136 pp = ME_SplitRun(wc, piter, j);
137 wc->pt.x += piter->member.run.nWidth;
138 return pp;
139 }
140 else
141 {
142 pp = piter;
143 /* omit all spaces before split point */
144 while(piter != wc->pRowStart)
145 {
146 piter = ME_FindItemBack(piter, diRun);
147 if (piter->member.run.nFlags & MERF_WHITESPACE)
148 {
149 pp = piter;
150 continue;
151 }
152 if (piter->member.run.nFlags & MERF_ENDWHITE)
153 {
154 j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
155 pp = ME_SplitRun(wc, piter, i);
156 wc->pt = pp->member.run.pt;
157 return pp;
158 }
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));
163 return pp;
164 }
165 wc->pt = piter->member.run.pt;
166 return piter;
167 }
168 }
169
170 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
171 {
172 ME_DisplayItem *piter = p, *pp;
173 int i, idesp, len;
174 ME_Run *run = &p->member.run;
175
176 idesp = i = ME_CharFromPoint(wc->context, loc, run);
177 len = ME_StrVLen(run->strText);
178 assert(len>0);
179 assert(i<len);
180 if (i) {
181 /* don't split words */
182 i = ME_ReverseFindWhitespaceV(run->strText, i);
183 pp = ME_MaximizeSplit(wc, p, i);
184 if (pp)
185 return pp;
186 }
187 TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
188 if (wc->pLastSplittableRun)
189 {
190 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
191 {
192 wc->pt = wc->ptLastSplittableRun;
193 return wc->pLastSplittableRun;
194 }
195 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
196 {
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));
201
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);
207 if (i == len)
208 i = ME_ReverseFindNonWhitespaceV(run->strText, len);
209 if (i) {
210 ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
211 wc->pt = piter2->member.run.pt;
212 return piter2;
213 }
214 /* splittable = must have whitespaces */
215 assert(0 == "Splittable, but no whitespaces");
216 }
217 else
218 {
219 /* restart from the first run beginning with spaces */
220 wc->pt = wc->ptLastSplittableRun;
221 return wc->pLastSplittableRun;
222 }
223 }
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*/
226 if (idesp)
227 return ME_SplitRun(wc, piter, idesp);
228 else
229 if (wc->pRowStart && piter != wc->pRowStart)
230 {
231 /* don't need to break current run, because it's possible to split
232 before this run */
233 wc->bOverflown = TRUE;
234 return piter;
235 }
236 else
237 {
238 /* split point inside first character - no choice but split after that char */
239 int chars = 1;
240 int pos2 = ME_StrRelPos(run->strText, 0, &chars);
241 if (pos2 != len) {
242 /* the run is more than 1 char, so we may split */
243 return ME_SplitRun(wc, piter, pos2);
244 }
245 /* the run is one char, can't split it */
246 return piter;
247 }
248 }
249
250 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
251 {
252 ME_DisplayItem *pp;
253 ME_Run *run;
254 int len;
255
256 assert(p->type == diRun);
257 if (!wc->pRowStart)
258 wc->pRowStart = p;
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);
264
265 if (wc->bOverflown) /* just skipping final whitespaces */
266 {
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 */
271 return p->next;
272 }
273
274 if (run->nFlags & MERF_STARTWHITE) {
275 /* try to split the run at the first non-white char */
276 int black;
277 black = ME_FindNonWhitespaceV(run->strText, 0);
278 if (black) {
279 wc->bOverflown = FALSE;
280 pp = ME_SplitRun(wc, p, black);
281 p->member.run.nFlags |= MERF_SKIPPED;
282 ME_InsertRowStart(wc, pp);
283 return pp;
284 }
285 }
286 /* black run: the row goes from pRowStart to the previous run */
287 ME_InsertRowStart(wc, p);
288 return p;
289 }
290 /* simply end the current row and move on to next one */
291 if (run->nFlags & MERF_ENDROW)
292 {
293 p = p->next;
294 ME_InsertRowStart(wc, p);
295 return p;
296 }
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);
302 }
303
304 /* will current run fit? */
305 if (wc->pt.x + run->nWidth > wc->nAvailWidth)
306 {
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;
312 return p;
313 }
314 /* TAB: we can split before */
315 if (run->nFlags & MERF_TAB) {
316 wc->bOverflown = TRUE;
317 return p;
318 }
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;
322 return p;
323 }
324 /* can we separate out the last spaces ? (to use overflow logic later) */
325 if (run->nFlags & MERF_ENDWHITE)
326 {
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 */
331 return p;
332 }
333 /* determine the split point by backtracking */
334 pp = ME_SplitByBacktracking(wc, p, loc);
335 if (pp == wc->pRowStart)
336 {
337 /* we had only spaces so far, entire content can be omitted */
338 wc->pt.x = 0;
339 return p->next;
340 }
341 if (p != pp) /* found a suitable split point */
342 {
343 wc->bOverflown = TRUE;
344 return pp;
345 }
346 /* we detected that it's best to split on start of this run */
347 if (wc->bOverflown)
348 return pp;
349 ERR("failure!\n");
350 /* not found anything - writing over margins is the only option left */
351 }
352 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
353 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
354 {
355 wc->pLastSplittableRun = p;
356 wc->ptLastSplittableRun = wc->pt;
357 }
358 wc->pt.x += run->nWidth;
359 return p->next;
360 }
361
362 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
363
364 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp, DWORD beginofs) {
365 ME_DisplayItem *p;
366 ME_WrapContext wc;
367 int border = 0;
368 int linespace = 0;
369
370 assert(tp->type == diParagraph);
371 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
372 return;
373 }
374 ME_PrepareParagraphForWrapping(c, tp);
375
376 wc.context = c;
377 /* wc.para_style = tp->member.para.style; */
378 wc.style = NULL;
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);
381 wc.nRightMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxRightIndent);
382 wc.nRow = 0;
383 wc.pt.x = 0;
384 wc.pt.y = 0;
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)
388 {
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;
393 }
394 if (tp->member.para.pFmt->wBorders & 2)
395 wc.nRightMargin -= border;
396 if (tp->member.para.pFmt->wBorders & 4)
397 wc.pt.y += border;
398 }
399
400 if (c->editor->bWordWrap)
401 wc.nAvailWidth = c->rcView.right - c->rcView.left - wc.nFirstMargin - wc.nRightMargin;
402 else
403 wc.nAvailWidth = ~0u >> 1;
404 wc.pRowStart = NULL;
405
406 linespace = ME_GetParaLineSpace(c, &tp->member.para);
407
408 ME_BeginRow(&wc);
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);
413 }
414 else p = p->next;
415 if (wc.nRow && p == wc.pRowStart)
416 wc.pt.y += linespace;
417 }
418 ME_WrapEndParagraph(&wc, p);
419 if ((tp->member.para.pFmt->dwMask & PFM_BORDER) && (tp->member.para.pFmt->wBorders & 8))
420 wc.pt.y += border;
421 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
422 wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceAfter);
423
424 tp->member.para.nFlags &= ~MEPF_REWRAP;
425 tp->member.para.nHeight = wc.pt.y;
426 tp->member.para.nRows = wc.nRow;
427 }
428
429
430 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
431 ME_DisplayItem *p, *pRow;
432
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) {
436 switch(p->type) {
437 case diStartRow:
438 pRow = p;
439 p = p->prev;
440 ME_Remove(pRow);
441 ME_DestroyDisplayItem(pRow);
442 break;
443 default:
444 break;
445 }
446 }
447 /* join runs that can be joined, set up flags */
448 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
449 int changed = 0;
450 switch(p->type) {
451 case diStartRow: assert(0); break; /* should have deleted it */
452 case diRun:
453 while (p->next->type == diRun) { /* FIXME */
454 if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
455 ME_JoinRuns(c->editor, p);
456 changed = 1;
457 }
458 else
459 break;
460 }
461 p->member.run.nFlags &= ~MERF_CALCBYWRAP;
462 break;
463 default:
464 break;
465 }
466 }
467 }
468
469 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) {
470 ME_DisplayItem *item;
471 ME_Context c;
472 BOOL bModified = FALSE;
473 int yStart = -1;
474
475 ME_InitContext(&c, editor, GetDC(editor->hWnd));
476 editor->nHeight = 0;
477 item = editor->pBuffer->pFirst->next;
478 while(item != editor->pBuffer->pLast) {
479 BOOL bRedraw = FALSE;
480
481 assert(item->type == diParagraph);
482 editor->nHeight = max(editor->nHeight, item->member.para.nYPos);
483 if ((item->member.para.nFlags & MEPF_REWRAP)
484 || (item->member.para.nYPos != c.pt.y))
485 bRedraw = TRUE;
486 item->member.para.nYPos = c.pt.y;
487
488 ME_WrapTextParagraph(&c, item, editor->selofs);
489
490 if (bRedraw)
491 {
492 item->member.para.nFlags |= MEPF_REPAINT;
493 if (yStart == -1)
494 yStart = c.pt.y;
495 }
496
497 bModified = bModified | bRedraw;
498
499 c.pt.y += item->member.para.nHeight;
500 item = item->member.para.next_para;
501 }
502 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
503 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
504
505 editor->nTotalLength = c.pt.y;
506
507 ME_DestroyContext(&c, editor->hWnd);
508
509 /* Each paragraph may contain multiple rows, which should be scrollable, even
510 if the containing paragraph has nYPos == 0 */
511 item = editor->pBuffer->pFirst;
512 while ((item = ME_FindItemFwd(item, diStartRow)) != NULL) {
513 assert(item->type == diStartRow);
514 editor->nHeight = max(editor->nHeight, item->member.row.nYPos);
515 }
516
517 if (bModified || editor->nTotalLength < editor->nLastTotalLength)
518 ME_InvalidateMarkedParagraphs(editor);
519 return bModified;
520 }
521
522 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) {
523 ME_Context c;
524
525 ME_InitContext(&c, editor, GetDC(editor->hWnd));
526 if (editor->bRedraw)
527 {
528 RECT rc = c.rcView;
529 int ofs = ME_GetYScrollPos(editor);
530
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);
537 }
538 item = item->member.para.next_para;
539 }
540 if (editor->nTotalLength < editor->nLastTotalLength)
541 {
542 rc.top = editor->nTotalLength - ofs;
543 rc.bottom = editor->nLastTotalLength - ofs;
544 InvalidateRect(editor->hWnd, &rc, TRUE);
545 }
546 }
547 ME_DestroyContext(&c, editor->hWnd);
548 }
549
550
551 void
552 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
553 {
554 if (editor->nEventMask & ENM_REQUESTRESIZE)
555 {
556 RECT rc;
557
558 GetClientRect(editor->hWnd, &rc);
559
560 if (force || rc.bottom != editor->nTotalLength)
561 {
562 REQRESIZE info;
563
564 info.nmhdr.hwndFrom = editor->hWnd;
565 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
566 info.nmhdr.code = EN_REQUESTRESIZE;
567 info.rc = rc;
568 info.rc.bottom = editor->nTotalLength;
569
570 editor->nEventMask &= ~ENM_REQUESTRESIZE;
571 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,
572 info.nmhdr.idFrom, (LPARAM)&info);
573 editor->nEventMask |= ENM_REQUESTRESIZE;
574 }
575 }
576 }
577
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.