richedit: Use ME_Cursor as parameter to ME_SplitRunSimple.
[wine/hramrach.git] / dlls / riched20 / wrap.c
bloba7c7fd33168dc404bf1dc4f44d558bd173367098
1 /*
2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
3 * warned !
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
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28 * Unsolved problems:
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
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;
42 return item;
45 static void ME_BeginRow(ME_WrapContext *wc)
47 PARAFORMAT2 *pFmt;
48 ME_DisplayItem *para = wc->pPara;
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)
60 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
61 cell->nWidth = 0;
63 } else if (para->member.para.pCell) {
64 ME_Cell *cell = &para->member.para.pCell->member.cell;
65 int width;
67 width = cell->nRightBoundary;
68 if (cell->prev_cell)
69 width -= cell->prev_cell->member.cell.nRightBoundary;
70 if (!cell->prev_cell)
72 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
73 width -= rowIndent;
75 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
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;
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++;
91 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
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;
101 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
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)
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;
117 assert (len);
118 if (~p->member.run.nFlags & MERF_GRAPHICS)
119 while (len && *(text--) == ' ')
120 len--;
121 if (len)
123 if (len == p->member.run.strText->nLen)
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;
132 bSkippingSpaces = !len;
133 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
134 width += p->member.run.nWidth;
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)
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;
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)
160 if (p->type==diRun) { /* FIXME add more run types */
161 p->member.run.pt.x += row->member.row.nLMargin+shift;
164 ME_InsertBefore(wc->pRowStart, row);
165 wc->nRow++;
166 wc->pt.y += row->member.row.nHeight;
167 ME_BeginRow(wc);
170 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
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)
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--;
185 p = para->next;
186 while(p) {
187 if (p->type == diParagraph || p->type == diTextEnd)
188 return;
189 if (p->type == diRun)
191 ME_Run *run = &p->member.run;
192 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
194 p = p->next;
199 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
201 /* FIXME compose style (out of character and paragraph styles) here */
203 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
205 ME_CalcRunExtent(wc->context, &wc->pPara->member.para,
206 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
209 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
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;
221 else
223 pp = piter;
224 /* omit all spaces before split point */
225 while(piter != wc->pRowStart)
227 piter = ME_FindItemBack(piter, diRun);
228 if (piter->member.run.nFlags & MERF_WHITESPACE)
230 pp = piter;
231 continue;
233 if (piter->member.run.nFlags & MERF_ENDWHITE)
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;
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;
247 wc->pt = piter->member.run.pt;
248 return piter;
252 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
254 ME_DisplayItem *piter = p, *pp;
255 int i, idesp, len;
256 ME_Run *run = &p->member.run;
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;
269 TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
270 if (wc->pLastSplittableRun)
272 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
274 wc->pt = wc->pLastSplittableRun->member.run.pt;
275 return wc->pLastSplittableRun;
277 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
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));
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;
296 /* splittable = must have whitespaces */
297 assert(0 == "Splittable, but no whitespaces");
299 else
301 /* restart from the first run beginning with spaces */
302 wc->pt = wc->pLastSplittableRun->member.run.pt;
303 return wc->pLastSplittableRun;
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)
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;
318 else
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);
325 /* the run is one char, can't split it */
326 return piter;
330 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
332 ME_DisplayItem *pp;
333 ME_Run *run;
334 int len;
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;
345 if (wc->bOverflown) /* just skipping final whitespaces */
347 /* End paragraph run can't overflow to the next line by itself. */
348 if (run->nFlags & MERF_ENDPARA)
349 return p->next;
351 if (run->nFlags & MERF_WHITESPACE) {
352 wc->pt.x += run->nWidth;
353 /* skip runs consisting of only whitespaces */
354 return p->next;
357 if (run->nFlags & MERF_STARTWHITE) {
358 /* try to split the run at the first non-white char */
359 int black;
360 black = ME_FindNonWhitespaceV(run->strText, 0);
361 if (black) {
362 wc->bOverflown = FALSE;
363 pp = ME_SplitRun(wc, p, black);
364 ME_InsertRowStart(wc, pp);
365 return pp;
368 /* black run: the row goes from pRowStart to the previous run */
369 ME_InsertRowStart(wc, p);
370 return p;
372 /* simply end the current row and move on to next one */
373 if (run->nFlags & MERF_ENDROW)
375 p = p->next;
376 ME_InsertRowStart(wc, p);
377 return p;
380 /* will current run fit? */
381 if (wc->bWordWrap &&
382 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
384 int loc = wc->context->pt.x + wc->nAvailWidth - wc->pt.x;
385 /* total white run ? */
386 if (run->nFlags & MERF_WHITESPACE) {
387 /* let the overflow logic handle it */
388 wc->bOverflown = TRUE;
389 return p;
391 /* TAB: we can split before */
392 if (run->nFlags & MERF_TAB) {
393 wc->bOverflown = TRUE;
394 if (wc->pRowStart == p)
395 /* Don't split before the start of the run, or we will get an
396 * endless loop. */
397 return p->next;
398 else
399 return p;
401 /* graphics: we can split before, if run's width is smaller than row's width */
402 if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
403 wc->bOverflown = TRUE;
404 return p;
406 /* can we separate out the last spaces ? (to use overflow logic later) */
407 if (run->nFlags & MERF_ENDWHITE)
409 /* we aren't sure if it's *really* necessary, it's a good start however */
410 int black = ME_ReverseFindNonWhitespaceV(run->strText, len);
411 ME_SplitRun(wc, p, black);
412 /* handle both parts again */
413 return p;
415 /* determine the split point by backtracking */
416 pp = ME_SplitByBacktracking(wc, p, loc);
417 if (pp == wc->pRowStart)
419 if (run->nFlags & MERF_STARTWHITE)
421 /* We had only spaces so far, so we must be on the first line of the
422 * paragraph (or the first line after MERF_ENDROW forced the line
423 * break within the paragraph), since no other lines of the paragraph
424 * start with spaces. */
426 /* The lines will only contain spaces, and the rest of the run will
427 * overflow onto the next line. */
428 wc->bOverflown = TRUE;
429 return p;
431 /* Couldn't split the first run, possible because we have a large font
432 * with a single character that caused an overflow.
434 wc->pt.x += run->nWidth;
435 return p->next;
437 if (p != pp) /* found a suitable split point */
439 wc->bOverflown = TRUE;
440 return pp;
442 /* we detected that it's best to split on start of this run */
443 if (wc->bOverflown)
444 return pp;
445 ERR("failure!\n");
446 /* not found anything - writing over margins is the only option left */
448 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
449 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
451 wc->pLastSplittableRun = p;
453 wc->pt.x += run->nWidth;
454 return p->next;
457 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
459 int sp = 0, ls = 0;
460 if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
462 /* FIXME: how to compute simply the line space in ls ??? */
463 /* FIXME: does line spacing include the line itself ??? */
464 switch (para->pFmt->bLineSpacingRule)
466 case 0: sp = ls; break;
467 case 1: sp = (3 * ls) / 2; break;
468 case 2: sp = 2 * ls; break;
469 case 3: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
470 case 4: sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
471 case 5: sp = para->pFmt->dyLineSpacing / 20; break;
472 default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
474 if (c->editor->nZoomNumerator == 0)
475 return sp;
476 else
477 return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
480 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
482 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
483 ME_DisplayItem *p;
484 ME_WrapContext wc;
485 int border = 0;
486 int linespace = 0;
487 PARAFORMAT2 *pFmt;
489 assert(tp->type == diParagraph);
490 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
491 return;
493 ME_PrepareParagraphForWrapping(c, tp);
494 pFmt = tp->member.para.pFmt;
496 wc.context = c;
497 wc.pPara = tp;
498 /* wc.para_style = tp->member.para.style; */
499 wc.style = NULL;
500 if (tp->member.para.nFlags & MEPF_ROWEND) {
501 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
502 } else {
503 int dxStartIndent = pFmt->dxStartIndent;
504 if (tp->member.para.pCell) {
505 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
507 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
508 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
509 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
511 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
512 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
514 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
516 wc.nRow = 0;
517 wc.pt.y = 0;
518 if (pFmt->dwMask & PFM_SPACEBEFORE)
519 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
520 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
521 pFmt->dwMask & PFM_BORDER)
523 border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
524 if (pFmt->wBorders & 1) {
525 wc.nFirstMargin += border;
526 wc.nLeftMargin += border;
528 if (pFmt->wBorders & 2)
529 wc.nRightMargin -= border;
530 if (pFmt->wBorders & 4)
531 wc.pt.y += border;
534 linespace = ME_GetParaLineSpace(c, &tp->member.para);
536 ME_BeginRow(&wc);
537 for (p = tp->next; p!=tp->member.para.next_para; ) {
538 assert(p->type != diStartRow);
539 if (p->type == diRun) {
540 p = ME_WrapHandleRun(&wc, p);
542 else p = p->next;
543 if (wc.nRow && p == wc.pRowStart)
544 wc.pt.y += linespace;
546 ME_WrapEndParagraph(&wc, p);
547 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
548 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
549 wc.pt.y += border;
550 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
551 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
553 tp->member.para.nFlags &= ~MEPF_REWRAP;
554 tp->member.para.nHeight = wc.pt.y;
555 tp->member.para.nRows = wc.nRow;
559 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
560 ME_DisplayItem *p, *pRow;
562 tp->member.para.nWidth = 0;
563 /* remove all items that will be reinserted by paragraph wrapper anyway */
564 tp->member.para.nRows = 0;
565 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
566 switch(p->type) {
567 case diStartRow:
568 pRow = p;
569 p = p->prev;
570 ME_Remove(pRow);
571 ME_DestroyDisplayItem(pRow);
572 break;
573 default:
574 break;
577 /* join runs that can be joined, set up flags */
578 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
579 switch(p->type) {
580 case diStartRow: assert(0); break; /* should have deleted it */
581 case diRun:
582 while (p->next->type == diRun) { /* FIXME */
583 if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
584 ME_JoinRuns(c->editor, p);
586 else
587 break;
589 break;
590 default:
591 break;
596 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
598 ME_DisplayItem *item;
599 ME_Context c;
600 BOOL bModified = FALSE;
601 int totalWidth = 0;
603 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
604 c.pt.x = 0;
605 item = editor->pBuffer->pFirst->next;
606 while(item != editor->pBuffer->pLast) {
607 BOOL bRedraw = FALSE;
609 assert(item->type == diParagraph);
610 if ((item->member.para.nFlags & MEPF_REWRAP)
611 || (item->member.para.pt.y != c.pt.y))
612 bRedraw = TRUE;
613 item->member.para.pt = c.pt;
615 ME_WrapTextParagraph(&c, item);
617 if (bRedraw)
618 item->member.para.nFlags |= MEPF_REPAINT;
620 bModified = bModified | bRedraw;
622 if (item->member.para.nFlags & MEPF_ROWSTART)
624 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
625 ME_DisplayItem *endRowPara;
626 int borderWidth = 0;
627 cell->member.cell.pt = c.pt;
628 /* Offset the text by the largest top border width. */
629 while (cell->member.cell.next_cell) {
630 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
631 cell = cell->member.cell.next_cell;
633 endRowPara = ME_FindItemFwd(cell, diParagraph);
634 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
635 if (borderWidth > 0)
637 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
638 while (cell) {
639 cell->member.cell.yTextOffset = borderWidth;
640 cell = cell->member.cell.prev_cell;
642 c.pt.y += borderWidth;
644 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
646 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
647 cell = ME_FindItemFwd(item, diCell);
648 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
649 c.pt.x = cell->member.cell.pt.x;
652 else if (item->member.para.nFlags & MEPF_ROWEND)
654 /* Set all the cells to the height of the largest cell */
655 ME_DisplayItem *startRowPara;
656 int prevHeight, nHeight, bottomBorder = 0;
657 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
658 item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
659 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
661 /* Last row, the bottom border is added to the height. */
662 cell = cell->member.cell.prev_cell;
663 while (cell)
665 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
666 cell = cell->member.cell.prev_cell;
668 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
669 cell = ME_FindItemBack(item, diCell);
671 prevHeight = cell->member.cell.nHeight;
672 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
673 cell->member.cell.nHeight = nHeight;
674 item->member.para.nHeight = nHeight;
675 cell = cell->member.cell.prev_cell;
676 cell->member.cell.nHeight = nHeight;
677 while (cell->member.cell.prev_cell)
679 cell = cell->member.cell.prev_cell;
680 cell->member.cell.nHeight = nHeight;
682 /* Also set the height of the start row paragraph */
683 startRowPara = ME_FindItemBack(cell, diParagraph);
684 startRowPara->member.para.nHeight = nHeight;
685 c.pt.x = startRowPara->member.para.pt.x;
686 c.pt.y = cell->member.cell.pt.y + nHeight;
687 if (prevHeight < nHeight)
689 /* The height of the cells has grown, so invalidate the bottom of
690 * the cells. */
691 item->member.para.nFlags |= MEPF_REPAINT;
692 cell = ME_FindItemBack(item, diCell);
693 while (cell) {
694 ME_FindItemBack(cell, diParagraph)->member.para.nFlags |= MEPF_REPAINT;
695 cell = cell->member.cell.prev_cell;
699 else if (item->member.para.pCell &&
700 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
702 /* The next paragraph is in the next cell in the table row. */
703 ME_Cell *cell = &item->member.para.pCell->member.cell;
704 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
706 /* Propagate the largest height to the end so that it can be easily
707 * sent back to all the cells at the end of the row. */
708 if (cell->prev_cell)
709 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
711 c.pt.x = cell->pt.x + cell->nWidth;
712 c.pt.y = cell->pt.y;
713 cell->next_cell->member.cell.pt = c.pt;
714 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
715 c.pt.y += cell->yTextOffset;
717 else
719 if (item->member.para.pCell) {
720 /* Next paragraph in the same cell. */
721 c.pt.x = item->member.para.pCell->member.cell.pt.x;
722 } else {
723 /* Normal paragraph */
724 c.pt.x = 0;
726 c.pt.y += item->member.para.nHeight;
729 totalWidth = max(totalWidth, item->member.para.nWidth);
730 item = item->member.para.next_para;
732 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
733 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
735 editor->nTotalLength = c.pt.y;
736 editor->nTotalWidth = totalWidth;
737 editor->pBuffer->pLast->member.para.pt.x = 0;
738 editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
740 ME_DestroyContext(&c);
742 if (bModified || editor->nTotalLength < editor->nLastTotalLength)
743 ME_InvalidateMarkedParagraphs(editor);
744 return bModified;
747 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor)
749 ME_Context c;
750 RECT rc;
751 int ofs;
752 ME_DisplayItem *item;
754 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
755 rc = c.rcView;
756 ofs = editor->vert_si.nPos;
758 item = editor->pBuffer->pFirst;
759 while(item != editor->pBuffer->pLast) {
760 if (item->member.para.nFlags & MEPF_REPAINT) {
761 rc.top = c.rcView.top + item->member.para.pt.y - ofs;
762 rc.bottom = max(c.rcView.top + item->member.para.pt.y
763 + item->member.para.nHeight - ofs,
764 c.rcView.bottom);
765 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
767 item = item->member.para.next_para;
769 if (editor->nTotalLength < editor->nLastTotalLength)
771 rc.top = c.rcView.top + editor->nTotalLength - ofs;
772 rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
773 ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
775 ME_DestroyContext(&c);
779 void
780 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
782 if (editor->nEventMask & ENM_REQUESTRESIZE)
784 RECT rc;
786 ITextHost_TxGetClientRect(editor->texthost, &rc);
788 if (force || rc.bottom != editor->nTotalLength)
790 REQRESIZE info;
792 info.nmhdr.hwndFrom = NULL;
793 info.nmhdr.idFrom = 0;
794 info.nmhdr.code = EN_REQUESTRESIZE;
795 info.rc = rc;
796 info.rc.right = editor->nTotalWidth;
797 info.rc.bottom = editor->nTotalLength;
799 editor->nEventMask &= ~ENM_REQUESTRESIZE;
800 ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
801 editor->nEventMask |= ENM_REQUESTRESIZE;