Sync with trunk (r48008)
[reactos.git] / dll / win32 / riched20 / wrap.c
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 PARAFORMAT2 *pFmt;
48 ME_DisplayItem *para = wc->pPara;
49
50 pFmt = para->member.para.pFmt;
51 wc->pRowStart = NULL;
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)) {
56 wc->nAvailWidth = 0;
57 wc->bWordWrap = FALSE;
58 if (para->member.para.nFlags & MEPF_ROWEND)
59 {
60 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
61 cell->nWidth = 0;
62 }
63 } else if (para->member.para.pCell) {
64 ME_Cell *cell = &para->member.para.pCell->member.cell;
65 int width;
66
67 width = cell->nRightBoundary;
68 if (cell->prev_cell)
69 width -= cell->prev_cell->member.cell.nRightBoundary;
70 if (!cell->prev_cell)
71 {
72 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
73 width -= rowIndent;
74 }
75 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
76
77 wc->nAvailWidth = cell->nWidth
78 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
79 wc->bWordWrap = TRUE;
80 } else {
81 wc->nAvailWidth = wc->context->nAvailWidth
82 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
83 }
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. */
88 wc->pt.y++;
89 }
90
91 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
92 {
93 ME_DisplayItem *p, *row, *para;
94 BOOL bSkippingSpaces = TRUE;
95 int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
96 PARAFORMAT2 *pFmt;
97 /* wrap text */
98 para = wc->pPara;
99 pFmt = para->member.para.pFmt;
100
101 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
102 {
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;
109 if (bSkippingSpaces)
110 {
111 /* Exclude space characters from run width.
112 * Other whitespace or delimiters are not treated this way. */
113 SIZE sz;
114 int len = p->member.run.strText->nLen;
115 WCHAR *text = p->member.run.strText->szData + len - 1;
116
117 assert (len);
118 if (~p->member.run.nFlags & MERF_GRAPHICS)
119 while (len && *(text--) == ' ')
120 len--;
121 if (len)
122 {
123 if (len == p->member.run.strText->nLen)
124 {
125 width += p->member.run.nWidth;
126 } else {
127 sz = ME_GetRunSize(wc->context, &para->member.para,
128 &p->member.run, len, p->member.run.pt.x);
129 width += sz.cx;
130 }
131 }
132 bSkippingSpaces = !len;
133 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
134 width += p->member.run.nWidth;
135 }
136 }
137
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)
142 {
143 /* The text was shifted down in ME_BeginRow so move the wrap context
144 * back to where it should be. */
145 wc->pt.y--;
146 /* The height of the row is increased by the borders. */
147 row->member.row.nHeight += 2;
148 }
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)
159 {
160 if (p->type==diRun) { /* FIXME add more run types */
161 p->member.run.pt.x += row->member.row.nLMargin+shift;
162 }
163 }
164 ME_InsertBefore(wc->pRowStart, row);
165 wc->nRow++;
166 wc->pt.y += row->member.row.nHeight;
167 ME_BeginRow(wc);
168 }
169
170 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
171 {
172 ME_DisplayItem *para = wc->pPara;
173 PARAFORMAT2 *pFmt = para->member.para.pFmt;
174 if (wc->pRowStart)
175 ME_InsertRowStart(wc, p);
176 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
177 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
178 {
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. */
181 wc->pt.y--;
182 }
183
184 /*
185 p = para->next;
186 while(p) {
187 if (p->type == diParagraph || p->type == diTextEnd)
188 return;
189 if (p->type == diRun)
190 {
191 ME_Run *run = &p->member.run;
192 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
193 }
194 p = p->next;
195 }
196 */
197 }
198
199 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
200 {
201 /* FIXME compose style (out of character and paragraph styles) here */
202
203 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
204
205 ME_CalcRunExtent(wc->context, &wc->pPara->member.para,
206 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
207 }
208
209 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
210 {
211 ME_DisplayItem *pp, *piter = p;
212 int j;
213 if (!i)
214 return NULL;
215 j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
216 if (j>0) {
217 pp = ME_SplitRun(wc, piter, j);
218 wc->pt.x += piter->member.run.nWidth;
219 return pp;
220 }
221 else
222 {
223 pp = piter;
224 /* omit all spaces before split point */
225 while(piter != wc->pRowStart)
226 {
227 piter = ME_FindItemBack(piter, diRun);
228 if (piter->member.run.nFlags & MERF_WHITESPACE)
229 {
230 pp = piter;
231 continue;
232 }
233 if (piter->member.run.nFlags & MERF_ENDWHITE)
234 {
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;
239 return pp;
240 }
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));
245 return pp;
246 }
247 wc->pt = piter->member.run.pt;
248 return piter;
249 }
250 }
251
252 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
253 {
254 ME_DisplayItem *piter = p, *pp;
255 int i, idesp, len;
256 ME_Run *run = &p->member.run;
257
258 idesp = i = ME_CharFromPoint(wc->context, loc, run);
259 len = run->strText->nLen;
260 assert(len>0);
261 assert(i<len);
262 if (i) {
263 /* don't split words */
264 i = ME_ReverseFindWhitespaceV(run->strText, i);
265 pp = ME_MaximizeSplit(wc, p, i);
266 if (pp)
267 return pp;
268 }
269 TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
270 if (wc->pLastSplittableRun)
271 {
272 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
273 {
274 wc->pt = wc->ptLastSplittableRun;
275 return wc->pLastSplittableRun;
276 }
277 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
278 {
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));
283
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);
289 if (i == len)
290 i = ME_ReverseFindNonWhitespaceV(run->strText, len);
291 if (i) {
292 ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
293 wc->pt = piter2->member.run.pt;
294 return piter2;
295 }
296 /* splittable = must have whitespaces */
297 assert(0 == "Splittable, but no whitespaces");
298 }
299 else
300 {
301 /* restart from the first run beginning with spaces */
302 wc->pt = wc->ptLastSplittableRun;
303 return wc->pLastSplittableRun;
304 }
305 }
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*/
308 if (idesp)
309 return ME_SplitRun(wc, piter, idesp);
310 else
311 if (wc->pRowStart && piter != wc->pRowStart)
312 {
313 /* don't need to break current run, because it's possible to split
314 before this run */
315 wc->bOverflown = TRUE;
316 return piter;
317 }
318 else
319 {
320 /* split point inside first character - no choice but split after that char */
321 if (len != 1) {
322 /* the run is more than 1 char, so we may split */
323 return ME_SplitRun(wc, piter, 1);
324 }
325 /* the run is one char, can't split it */
326 return piter;
327 }
328 }
329
330 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
331 {
332 ME_DisplayItem *pp;
333 ME_Run *run;
334 int len;
335
336 assert(p->type == diRun);
337 if (!wc->pRowStart)
338 wc->pRowStart = p;
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;
344
345 if (wc->bOverflown) /* just skipping final whitespaces */
346 {
347 /* End paragraph run can't overflow to the next line by itself. */
348 if (run->nFlags & MERF_ENDPARA)
349 return p->next;
350
351 if (run->nFlags & MERF_WHITESPACE) {
352 p->member.run.nFlags |= MERF_SKIPPED;
353 wc->pt.x += run->nWidth;
354 /* skip runs consisting of only whitespaces */
355 return p->next;
356 }
357
358 if (run->nFlags & MERF_STARTWHITE) {
359 /* try to split the run at the first non-white char */
360 int black;
361 black = ME_FindNonWhitespaceV(run->strText, 0);
362 if (black) {
363 wc->bOverflown = FALSE;
364 pp = ME_SplitRun(wc, p, black);
365 p->member.run.nFlags |= MERF_SKIPPED;
366 ME_InsertRowStart(wc, pp);
367 return pp;
368 }
369 }
370 /* black run: the row goes from pRowStart to the previous run */
371 ME_InsertRowStart(wc, p);
372 return p;
373 }
374 /* simply end the current row and move on to next one */
375 if (run->nFlags & MERF_ENDROW)
376 {
377 p = p->next;
378 ME_InsertRowStart(wc, p);
379 return p;
380 }
381
382 /* will current run fit? */
383 if (wc->bWordWrap &&
384 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
385 {
386 int loc = wc->context->pt.x + wc->nAvailWidth - wc->pt.x;
387 /* total white run ? */
388 if (run->nFlags & MERF_WHITESPACE) {
389 /* let the overflow logic handle it */
390 wc->bOverflown = TRUE;
391 return p;
392 }
393 /* TAB: we can split before */
394 if (run->nFlags & MERF_TAB) {
395 wc->bOverflown = TRUE;
396 if (wc->pRowStart == p)
397 /* Don't split before the start of the run, or we will get an
398 * endless loop. */
399 return p->next;
400 else
401 return p;
402 }
403 /* graphics: we can split before, if run's width is smaller than row's width */
404 if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
405 wc->bOverflown = TRUE;
406 return p;
407 }
408 /* can we separate out the last spaces ? (to use overflow logic later) */
409 if (run->nFlags & MERF_ENDWHITE)
410 {
411 /* we aren't sure if it's *really* necessary, it's a good start however */
412 int black = ME_ReverseFindNonWhitespaceV(run->strText, len);
413 ME_SplitRun(wc, p, black);
414 /* handle both parts again */
415 return p;
416 }
417 /* determine the split point by backtracking */
418 pp = ME_SplitByBacktracking(wc, p, loc);
419 if (pp == wc->pRowStart)
420 {
421 if (run->nFlags & MERF_STARTWHITE)
422 {
423 /* We had only spaces so far, so we must be on the first line of the
424 * paragraph (or the first line after MERF_ENDROW forced the line
425 * break within the paragraph), since no other lines of the paragraph
426 * start with spaces. */
427
428 /* The lines will only contain spaces, and the rest of the run will
429 * overflow onto the next line. */
430 wc->bOverflown = TRUE;
431 return p;
432 }
433 /* Couldn't split the first run, possible because we have a large font
434 * with a single character that caused an overflow.
435 */
436 wc->pt.x += run->nWidth;
437 return p->next;
438 }
439 if (p != pp) /* found a suitable split point */
440 {
441 wc->bOverflown = TRUE;
442 return pp;
443 }
444 /* we detected that it's best to split on start of this run */
445 if (wc->bOverflown)
446 return pp;
447 ERR("failure!\n");
448 /* not found anything - writing over margins is the only option left */
449 }
450 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
451 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
452 {
453 wc->pLastSplittableRun = p;
454 wc->ptLastSplittableRun = wc->pt;
455 }
456 wc->pt.x += run->nWidth;
457 return p->next;
458 }
459
460 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
461 {
462 int sp = 0, ls = 0;
463 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
464
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)
468 {
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);
476 }
477 if (c->editor->nZoomNumerator == 0)
478 return sp;
479 else
480 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
481 }
482
483 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
484
485 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
486 ME_DisplayItem *p;
487 ME_WrapContext wc;
488 int border = 0;
489 int linespace = 0;
490 PARAFORMAT2 *pFmt;
491
492 assert(tp->type == diParagraph);
493 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
494 return;
495 }
496 ME_PrepareParagraphForWrapping(c, tp);
497 pFmt = tp->member.para.pFmt;
498
499 wc.context = c;
500 wc.pPara = tp;
501 /* wc.para_style = tp->member.para.style; */
502 wc.style = NULL;
503 if (tp->member.para.nFlags & MEPF_ROWEND) {
504 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
505 } else {
506 int dxStartIndent = pFmt->dxStartIndent;
507 if (tp->member.para.pCell) {
508 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
509 }
510 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
511 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
512 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
513 }
514 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
515 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
516 {
517 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
518 }
519 wc.nRow = 0;
520 wc.pt.y = 0;
521 if (pFmt->dwMask & PFM_SPACEBEFORE)
522 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
523 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
524 pFmt->dwMask & PFM_BORDER)
525 {
526 border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
527 if (pFmt->wBorders & 1) {
528 wc.nFirstMargin += border;
529 wc.nLeftMargin += border;
530 }
531 if (pFmt->wBorders & 2)
532 wc.nRightMargin -= border;
533 if (pFmt->wBorders & 4)
534 wc.pt.y += border;
535 }
536
537 linespace = ME_GetParaLineSpace(c, &tp->member.para);
538
539 ME_BeginRow(&wc);
540 for (p = tp->next; p!=tp->member.para.next_para; ) {
541 assert(p->type != diStartRow);
542 if (p->type == diRun) {
543 p = ME_WrapHandleRun(&wc, p);
544 }
545 else p = p->next;
546 if (wc.nRow && p == wc.pRowStart)
547 wc.pt.y += linespace;
548 }
549 ME_WrapEndParagraph(&wc, p);
550 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
551 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
552 wc.pt.y += border;
553 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
554 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
555
556 tp->member.para.nFlags &= ~MEPF_REWRAP;
557 tp->member.para.nHeight = wc.pt.y;
558 tp->member.para.nRows = wc.nRow;
559 }
560
561
562 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
563 ME_DisplayItem *p, *pRow;
564
565 tp->member.para.nWidth = 0;
566 /* remove all items that will be reinserted by paragraph wrapper anyway */
567 tp->member.para.nRows = 0;
568 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
569 switch(p->type) {
570 case diStartRow:
571 pRow = p;
572 p = p->prev;
573 ME_Remove(pRow);
574 ME_DestroyDisplayItem(pRow);
575 break;
576 default:
577 break;
578 }
579 }
580 /* join runs that can be joined, set up flags */
581 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
582 switch(p->type) {
583 case diStartRow: assert(0); break; /* should have deleted it */
584 case diRun:
585 while (p->next->type == diRun) { /* FIXME */
586 if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
587 ME_JoinRuns(c->editor, p);
588 }
589 else
590 break;
591 }
592 p->member.run.nFlags &= ~MERF_CALCBYWRAP;
593 break;
594 default:
595 break;
596 }
597 }
598 }
599
600 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
601 {
602 ME_DisplayItem *item;
603 ME_Context c;
604 BOOL bModified = FALSE;
605 int totalWidth = 0;
606
607 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
608 c.pt.x = 0;
609 item = editor->pBuffer->pFirst->next;
610 while(item != editor->pBuffer->pLast) {
611 BOOL bRedraw = FALSE;
612
613 assert(item->type == diParagraph);
614 if ((item->member.para.nFlags & MEPF_REWRAP)
615 || (item->member.para.pt.y != c.pt.y))
616 bRedraw = TRUE;
617 item->member.para.pt = c.pt;
618
619 ME_WrapTextParagraph(&c, item);
620
621 if (bRedraw)
622 item->member.para.nFlags |= MEPF_REPAINT;
623
624 bModified = bModified | bRedraw;
625
626 if (item->member.para.nFlags & MEPF_ROWSTART)
627 {
628 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
629 ME_DisplayItem *endRowPara;
630 int borderWidth = 0;
631 cell->member.cell.pt = c.pt;
632 /* Offset the text by the largest top border width. */
633 while (cell->member.cell.next_cell) {
634 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
635 cell = cell->member.cell.next_cell;
636 }
637 endRowPara = ME_FindItemFwd(cell, diParagraph);
638 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
639 if (borderWidth > 0)
640 {
641 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
642 while (cell) {
643 cell->member.cell.yTextOffset = borderWidth;
644 cell = cell->member.cell.prev_cell;
645 }
646 c.pt.y += borderWidth;
647 }
648 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
649 {
650 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
651 cell = ME_FindItemFwd(item, diCell);
652 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
653 c.pt.x = cell->member.cell.pt.x;
654 }
655 }
656 else if (item->member.para.nFlags & MEPF_ROWEND)
657 {
658 /* Set all the cells to the height of the largest cell */
659 ME_DisplayItem *startRowPara;
660 int prevHeight, nHeight, bottomBorder = 0;
661 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
662 item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
663 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
664 {
665 /* Last row, the bottom border is added to the height. */
666 cell = cell->member.cell.prev_cell;
667 while (cell)
668 {
669 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
670 cell = cell->member.cell.prev_cell;
671 }
672 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
673 cell = ME_FindItemBack(item, diCell);
674 }
675 prevHeight = cell->member.cell.nHeight;
676 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
677 cell->member.cell.nHeight = nHeight;
678 item->member.para.nHeight = nHeight;
679 cell = cell->member.cell.prev_cell;
680 cell->member.cell.nHeight = nHeight;
681 while (cell->member.cell.prev_cell)
682 {
683 cell = cell->member.cell.prev_cell;
684 cell->member.cell.nHeight = nHeight;
685 }
686 /* Also set the height of the start row paragraph */
687 startRowPara = ME_FindItemBack(cell, diParagraph);
688 startRowPara->member.para.nHeight = nHeight;
689 c.pt.x = startRowPara->member.para.pt.x;
690 c.pt.y = cell->member.cell.pt.y + nHeight;
691 if (prevHeight < nHeight)
692 {
693 /* The height of the cells has grown, so invalidate the bottom of
694 * the cells. */
695 item->member.para.nFlags |= MEPF_REPAINT;
696 cell = ME_FindItemBack(item, diCell);
697 while (cell) {
698 ME_FindItemBack(cell, diParagraph)->member.para.nFlags |= MEPF_REPAINT;
699 cell = cell->member.cell.prev_cell;
700 }
701 }
702 }
703 else if (item->member.para.pCell &&
704 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
705 {
706 /* The next paragraph is in the next cell in the table row. */
707 ME_Cell *cell = &item->member.para.pCell->member.cell;
708 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
709
710 /* Propagate the largest height to the end so that it can be easily
711 * sent back to all the cells at the end of the row. */
712 if (cell->prev_cell)
713 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
714
715 c.pt.x = cell->pt.x + cell->nWidth;
716 c.pt.y = cell->pt.y;
717 cell->next_cell->member.cell.pt = c.pt;
718 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
719 c.pt.y += cell->yTextOffset;
720 }
721 else
722 {
723 if (item->member.para.pCell) {
724 /* Next paragraph in the same cell. */
725 c.pt.x = item->member.para.pCell->member.cell.pt.x;
726 } else {
727 /* Normal paragraph */
728 c.pt.x = 0;
729 }
730 c.pt.y += item->member.para.nHeight;
731 }
732
733 totalWidth = max(totalWidth, item->member.para.nWidth);
734 item = item->member.para.next_para;
735 }
736 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
737 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
738
739 editor->nTotalLength = c.pt.y;
740 editor->nTotalWidth = totalWidth;
741 editor->pBuffer->pLast->member.para.pt.x = 0;
742 editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
743
744 ME_DestroyContext(&c);
745
746 if (bModified || editor->nTotalLength < editor->nLastTotalLength)
747 ME_InvalidateMarkedParagraphs(editor);
748 return bModified;
749 }
750
751 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor)
752 {
753 ME_Context c;
754 RECT rc;
755 int ofs;
756 ME_DisplayItem *item;
757
758 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
759 rc = c.rcView;
760 ofs = editor->vert_si.nPos;
761
762 item = editor->pBuffer->pFirst;
763 while(item != editor->pBuffer->pLast) {
764 if (item->member.para.nFlags & MEPF_REPAINT) {
765 rc.top = c.rcView.top + item->member.para.pt.y - ofs;
766 rc.bottom = max(c.rcView.top + item->member.para.pt.y
767 + item->member.para.nHeight - ofs,
768 c.rcView.bottom);
769 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
770 }
771 item = item->member.para.next_para;
772 }
773 if (editor->nTotalLength < editor->nLastTotalLength)
774 {
775 rc.top = c.rcView.top + editor->nTotalLength - ofs;
776 rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
777 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
778 }
779 ME_DestroyContext(&c);
780 }
781
782
783 void
784 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
785 {
786 if (editor->nEventMask & ENM_REQUESTRESIZE)
787 {
788 RECT rc;
789
790 ITextHost_TxGetClientRect(editor->texthost, &rc);
791
792 if (force || rc.bottom != editor->nTotalLength)
793 {
794 REQRESIZE info;
795
796 info.nmhdr.hwndFrom = NULL;
797 info.nmhdr.idFrom = 0;
798 info.nmhdr.code = EN_REQUESTRESIZE;
799 info.rc = rc;
800 info.rc.right = editor->nTotalWidth;
801 info.rc.bottom = editor->nTotalLength;
802
803 editor->nEventMask &= ~ENM_REQUESTRESIZE;
804 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
805 editor->nEventMask |= ENM_REQUESTRESIZE;
806 }
807 }
808 }