stash
[wine/wine64.git] / dlls / riched20 / wrap.c
blob7eb844c733f99037cb2b10d7f367d83b4fb195c3
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, ME_DisplayItem *para)
47 PARAFORMAT2 *pFmt;
48 assert(para && para->type == diParagraph);
49 pFmt = para->member.para.pFmt;
50 wc->pRowStart = NULL;
51 wc->bOverflown = FALSE;
52 wc->pLastSplittableRun = NULL;
53 wc->bWordWrap = wc->context->editor->bWordWrap;
54 if (para->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
55 wc->nAvailWidth = 0;
56 wc->bWordWrap = FALSE;
57 if (para->member.para.nFlags & MEPF_ROWEND)
59 ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
60 cell->nWidth = 0;
62 } else if (para->member.para.pCell) {
63 ME_Cell *cell = &para->member.para.pCell->member.cell;
64 int width;
66 width = cell->nRightBoundary;
67 if (cell->prev_cell)
68 width -= cell->prev_cell->member.cell.nRightBoundary;
69 if (!cell->prev_cell)
71 int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
72 width -= rowIndent;
74 cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
76 wc->nAvailWidth = cell->nWidth
77 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
78 wc->bWordWrap = TRUE;
79 } else {
80 wc->nAvailWidth = wc->context->rcView.right - wc->context->rcView.left
81 - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
83 wc->pt.x = wc->context->pt.x;
84 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
85 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
86 /* Shift the text down because of the border. */
87 wc->pt.y++;
90 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
92 ME_DisplayItem *p, *row, *para;
93 BOOL bSkippingSpaces = TRUE;
94 int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
95 PARAFORMAT2 *pFmt;
96 /* wrap text */
97 para = ME_GetParagraph(wc->pRowStart);
98 pFmt = para->member.para.pFmt;
100 for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
102 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
103 if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
104 if (p->member.run.nAscent>ascent)
105 ascent = p->member.run.nAscent;
106 if (p->member.run.nDescent>descent)
107 descent = p->member.run.nDescent;
108 if (bSkippingSpaces)
110 /* Exclude space characters from run width.
111 * Other whitespace or delimiters are not treated this way. */
112 SIZE sz;
113 int len = p->member.run.strText->nLen;
114 WCHAR *text = p->member.run.strText->szData + len - 1;
116 assert (len);
117 while (len && *(text--) == ' ')
118 len--;
119 if (len)
121 if (len == p->member.run.strText->nLen)
123 width += p->member.run.nWidth;
124 } else {
125 sz = ME_GetRunSize(wc->context, &para->member.para,
126 &p->member.run, len, p->member.run.pt.x);
127 width += sz.cx;
130 bSkippingSpaces = !len;
131 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
132 width += p->member.run.nWidth;
136 row = ME_MakeRow(ascent+descent, ascent, width);
137 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
138 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
140 /* The text was shifted down in ME_BeginRow so move the wrap context
141 * back to where it should be. */
142 wc->pt.y--;
143 /* The height of the row is increased by the borders. */
144 row->member.row.nHeight += 2;
146 row->member.row.pt = wc->pt;
147 row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
148 row->member.row.nRMargin = wc->nRightMargin;
149 assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
150 align = para->member.para.pFmt->wAlignment;
151 if (align == PFA_CENTER)
152 shift = max((wc->nAvailWidth-width)/2, 0);
153 if (align == PFA_RIGHT)
154 shift = max(wc->nAvailWidth-width, 0);
155 for (p = wc->pRowStart; p!=pEnd; p = p->next)
157 if (p->type==diRun) { /* FIXME add more run types */
158 p->member.run.pt.x += row->member.row.nLMargin+shift;
161 ME_InsertBefore(wc->pRowStart, row);
162 wc->nRow++;
163 wc->pt.y += row->member.row.nHeight;
164 ME_BeginRow(wc, para);
167 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
169 ME_DisplayItem *para = p->member.para.prev_para;
170 PARAFORMAT2 *pFmt = para->member.para.pFmt;
171 if (wc->pRowStart)
172 ME_InsertRowStart(wc, p);
173 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
174 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
176 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
177 * text down by one pixel for the border, so fix up the wrap context. */
178 wc->pt.y--;
182 p = p->member.para.prev_para->next;
183 while(p) {
184 if (p->type == diParagraph || p->type == diTextEnd)
185 return;
186 if (p->type == diRun)
188 ME_Run *run = &p->member.run;
189 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
191 p = p->next;
196 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
198 /* FIXME compose style (out of character and paragraph styles) here */
200 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
202 ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
203 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
206 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
208 ME_DisplayItem *pp, *piter = p;
209 int j;
210 if (!i)
211 return NULL;
212 j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
213 if (j>0) {
214 pp = ME_SplitRun(wc, piter, j);
215 wc->pt.x += piter->member.run.nWidth;
216 return pp;
218 else
220 pp = piter;
221 /* omit all spaces before split point */
222 while(piter != wc->pRowStart)
224 piter = ME_FindItemBack(piter, diRun);
225 if (piter->member.run.nFlags & MERF_WHITESPACE)
227 pp = piter;
228 continue;
230 if (piter->member.run.nFlags & MERF_ENDWHITE)
232 j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
233 pp = ME_SplitRun(wc, piter, i);
234 wc->pt = pp->member.run.pt;
235 return pp;
237 /* this run is the end of spaces, so the run edge is a good point to split */
238 wc->pt = pp->member.run.pt;
239 wc->bOverflown = TRUE;
240 TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData));
241 return pp;
243 wc->pt = piter->member.run.pt;
244 return piter;
248 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
250 ME_DisplayItem *piter = p, *pp;
251 int i, idesp, len;
252 ME_Run *run = &p->member.run;
254 idesp = i = ME_CharFromPoint(wc->context, loc, run);
255 len = ME_StrVLen(run->strText);
256 assert(len>0);
257 assert(i<len);
258 if (i) {
259 /* don't split words */
260 i = ME_ReverseFindWhitespaceV(run->strText, i);
261 pp = ME_MaximizeSplit(wc, p, i);
262 if (pp)
263 return pp;
265 TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
266 if (wc->pLastSplittableRun)
268 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
270 wc->pt = wc->ptLastSplittableRun;
271 return wc->pLastSplittableRun;
273 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
275 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
276 they serve no other purpose */
277 ME_UpdateRunFlags(wc->context->editor, run);
278 assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
280 piter = wc->pLastSplittableRun;
281 run = &piter->member.run;
282 len = ME_StrVLen(run->strText);
283 /* don't split words */
284 i = ME_ReverseFindWhitespaceV(run->strText, len);
285 if (i == len)
286 i = ME_ReverseFindNonWhitespaceV(run->strText, len);
287 if (i) {
288 ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
289 wc->pt = piter2->member.run.pt;
290 return piter2;
292 /* splittable = must have whitespaces */
293 assert(0 == "Splittable, but no whitespaces");
295 else
297 /* restart from the first run beginning with spaces */
298 wc->pt = wc->ptLastSplittableRun;
299 return wc->pLastSplittableRun;
302 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData));
303 /* OK, no better idea, so assume we MAY split words if we can split at all*/
304 if (idesp)
305 return ME_SplitRun(wc, piter, idesp);
306 else
307 if (wc->pRowStart && piter != wc->pRowStart)
309 /* don't need to break current run, because it's possible to split
310 before this run */
311 wc->bOverflown = TRUE;
312 return piter;
314 else
316 /* split point inside first character - no choice but split after that char */
317 int chars = 1;
318 int pos2 = ME_StrRelPos(run->strText, 0, &chars);
319 if (pos2 != len) {
320 /* the run is more than 1 char, so we may split */
321 return ME_SplitRun(wc, piter, pos2);
323 /* the run is one char, can't split it */
324 return piter;
328 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
330 ME_DisplayItem *pp;
331 ME_Run *run;
332 int len;
334 assert(p->type == diRun);
335 if (!wc->pRowStart)
336 wc->pRowStart = p;
337 run = &p->member.run;
338 run->pt.x = wc->pt.x;
339 run->pt.y = wc->pt.y;
340 ME_WrapSizeRun(wc, p);
341 len = ME_StrVLen(run->strText);
343 if (wc->bOverflown) /* just skipping final whitespaces */
345 /* End paragraph run can't overflow to the next line by itself. */
346 if (run->nFlags & MERF_ENDPARA)
347 return p->next;
349 if (run->nFlags & MERF_WHITESPACE) {
350 p->member.run.nFlags |= MERF_SKIPPED;
351 wc->pt.x += run->nWidth;
352 /* skip runs consisting of only whitespaces */
353 return p->next;
356 if (run->nFlags & MERF_STARTWHITE) {
357 /* try to split the run at the first non-white char */
358 int black;
359 black = ME_FindNonWhitespaceV(run->strText, 0);
360 if (black) {
361 wc->bOverflown = FALSE;
362 pp = ME_SplitRun(wc, p, black);
363 p->member.run.nFlags |= MERF_SKIPPED;
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;
452 wc->ptLastSplittableRun = wc->pt;
454 wc->pt.x += run->nWidth;
455 return p->next;
458 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
460 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
461 ME_DisplayItem *p;
462 ME_WrapContext wc;
463 int border = 0;
464 int linespace = 0;
465 PARAFORMAT2 *pFmt;
467 assert(tp->type == diParagraph);
468 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
469 return;
471 ME_PrepareParagraphForWrapping(c, tp);
472 pFmt = tp->member.para.pFmt;
474 wc.context = c;
475 /* wc.para_style = tp->member.para.style; */
476 wc.style = NULL;
477 if (tp->member.para.nFlags & MEPF_ROWEND) {
478 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
479 } else {
480 int dxStartIndent = pFmt->dxStartIndent;
481 if (tp->member.para.pCell) {
482 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
484 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
485 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
486 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
488 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
489 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
491 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
493 wc.nRow = 0;
494 wc.pt.y = 0;
495 if (pFmt->dwMask & PFM_SPACEBEFORE)
496 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
497 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
498 pFmt->dwMask & PFM_BORDER)
500 border = ME_GetParaBorderWidth(c->editor, tp->member.para.pFmt->wBorders);
501 if (pFmt->wBorders & 1) {
502 wc.nFirstMargin += border;
503 wc.nLeftMargin += border;
505 if (pFmt->wBorders & 2)
506 wc.nRightMargin -= border;
507 if (pFmt->wBorders & 4)
508 wc.pt.y += border;
511 linespace = ME_GetParaLineSpace(c, &tp->member.para);
513 ME_BeginRow(&wc, tp);
514 for (p = tp->next; p!=tp->member.para.next_para; ) {
515 assert(p->type != diStartRow);
516 if (p->type == diRun) {
517 p = ME_WrapHandleRun(&wc, p);
519 else p = p->next;
520 if (wc.nRow && p == wc.pRowStart)
521 wc.pt.y += linespace;
523 ME_WrapEndParagraph(&wc, p);
524 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
525 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
526 wc.pt.y += border;
527 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
528 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
530 tp->member.para.nFlags &= ~MEPF_REWRAP;
531 tp->member.para.nHeight = wc.pt.y;
532 tp->member.para.nRows = wc.nRow;
536 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
537 ME_DisplayItem *p, *pRow;
539 /* remove all items that will be reinserted by paragraph wrapper anyway */
540 tp->member.para.nRows = 0;
541 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
542 switch(p->type) {
543 case diStartRow:
544 pRow = p;
545 p = p->prev;
546 ME_Remove(pRow);
547 ME_DestroyDisplayItem(pRow);
548 break;
549 default:
550 break;
553 /* join runs that can be joined, set up flags */
554 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
555 int changed = 0;
556 switch(p->type) {
557 case diStartRow: assert(0); break; /* should have deleted it */
558 case diRun:
559 while (p->next->type == diRun) { /* FIXME */
560 if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
561 ME_JoinRuns(c->editor, p);
562 changed = 1;
564 else
565 break;
567 p->member.run.nFlags &= ~MERF_CALCBYWRAP;
568 break;
569 default:
570 break;
575 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
577 ME_DisplayItem *item;
578 ME_Context c;
579 BOOL bModified = FALSE;
580 int yStart = -1;
582 ME_InitContext(&c, editor, GetDC(editor->hWnd));
583 c.pt.x = 0;
584 item = editor->pBuffer->pFirst->next;
585 while(item != editor->pBuffer->pLast) {
586 BOOL bRedraw = FALSE;
588 assert(item->type == diParagraph);
589 if ((item->member.para.nFlags & MEPF_REWRAP)
590 || (item->member.para.pt.y != c.pt.y))
591 bRedraw = TRUE;
592 item->member.para.pt = c.pt;
594 ME_WrapTextParagraph(&c, item);
596 if (bRedraw)
598 item->member.para.nFlags |= MEPF_REPAINT;
599 if (yStart == -1)
600 yStart = c.pt.y;
603 bModified = bModified | bRedraw;
605 if (item->member.para.nFlags & MEPF_ROWSTART)
607 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
608 ME_DisplayItem *endRowPara;
609 int borderWidth = 0;
610 cell->member.cell.pt = c.pt;
611 /* Offset the text by the largest top border width. */
612 while (cell->member.cell.next_cell) {
613 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
614 cell = cell->member.cell.next_cell;
616 endRowPara = ME_FindItemFwd(cell, diParagraph);
617 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
618 if (borderWidth > 0)
620 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
621 while (cell) {
622 cell->member.cell.yTextOffset = borderWidth;
623 cell = cell->member.cell.prev_cell;
625 c.pt.y += borderWidth;
627 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
629 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
630 cell = ME_FindItemFwd(item, diCell);
631 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
632 c.pt.x = cell->member.cell.pt.x;
635 else if (item->member.para.nFlags & MEPF_ROWEND)
637 /* Set all the cells to the height of the largest cell */
638 ME_DisplayItem *startRowPara;
639 int prevHeight, nHeight, bottomBorder = 0;
640 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
641 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
643 /* Last row, the bottom border is added to the height. */
644 cell = cell->member.cell.prev_cell;
645 while (cell)
647 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
648 cell = cell->member.cell.prev_cell;
650 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
651 cell = ME_FindItemBack(item, diCell);
653 prevHeight = cell->member.cell.nHeight;
654 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
655 cell->member.cell.nHeight = nHeight;
656 item->member.para.nHeight = nHeight;
657 cell = cell->member.cell.prev_cell;
658 cell->member.cell.nHeight = nHeight;
659 while (cell->member.cell.prev_cell)
661 cell = cell->member.cell.prev_cell;
662 cell->member.cell.nHeight = nHeight;
664 /* Also set the height of the start row paragraph */
665 startRowPara = ME_FindItemBack(cell, diParagraph);
666 startRowPara->member.para.nHeight = nHeight;
667 c.pt.x = startRowPara->member.para.pt.x;
668 c.pt.y = cell->member.cell.pt.y + nHeight;
669 if (prevHeight < nHeight)
671 /* The height of the cells has grown, so invalidate the bottom of
672 * the cells. */
673 item->member.para.nFlags |= MEPF_REPAINT;
674 cell = ME_FindItemBack(item, diCell);
675 while (cell) {
676 ME_FindItemBack(cell, diParagraph)->member.para.nFlags |= MEPF_REPAINT;
677 cell = cell->member.cell.prev_cell;
681 else if (item->member.para.pCell &&
682 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
684 /* The next paragraph is in the next cell in the table row. */
685 ME_Cell *cell = &item->member.para.pCell->member.cell;
686 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
688 /* Propagate the largest height to the end so that it can be easily
689 * sent back to all the cells at the end of the row. */
690 if (cell->prev_cell)
691 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
693 c.pt.x = cell->pt.x + cell->nWidth;
694 c.pt.y = cell->pt.y;
695 cell->next_cell->member.cell.pt = c.pt;
696 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
697 c.pt.y += cell->yTextOffset;
699 else
701 if (item->member.para.pCell) {
702 /* Next paragraph in the same cell. */
703 c.pt.x = item->member.para.pCell->member.cell.pt.x;
704 } else {
705 /* Normal paragraph */
706 c.pt.x = 0;
708 c.pt.y += item->member.para.nHeight;
710 item = item->member.para.next_para;
712 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
713 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
715 editor->nTotalLength = c.pt.y;
716 editor->pBuffer->pLast->member.para.pt.x = 0;
717 editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
719 ME_DestroyContext(&c, editor->hWnd);
721 if (bModified || editor->nTotalLength < editor->nLastTotalLength)
722 ME_InvalidateMarkedParagraphs(editor);
723 return bModified;
726 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor)
728 ME_Context c;
729 RECT rc;
730 int ofs;
731 ME_DisplayItem *item;
733 ME_InitContext(&c, editor, GetDC(editor->hWnd));
734 rc = c.rcView;
735 ofs = ME_GetYScrollPos(editor);
737 item = editor->pBuffer->pFirst;
738 while(item != editor->pBuffer->pLast) {
739 if (item->member.para.nFlags & MEPF_REPAINT) {
740 rc.top = c.rcView.top + item->member.para.pt.y - ofs;
741 rc.bottom = max(c.rcView.top + item->member.para.pt.y
742 + item->member.para.nHeight - ofs,
743 c.rcView.bottom);
744 InvalidateRect(editor->hWnd, &rc, TRUE);
746 item = item->member.para.next_para;
748 if (editor->nTotalLength < editor->nLastTotalLength)
750 rc.top = c.rcView.top + editor->nTotalLength - ofs;
751 rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
752 InvalidateRect(editor->hWnd, &rc, TRUE);
754 ME_DestroyContext(&c, editor->hWnd);
758 void
759 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
761 if (editor->nEventMask & ENM_REQUESTRESIZE)
763 RECT rc;
765 GetClientRect(editor->hWnd, &rc);
767 if (force || rc.bottom != editor->nTotalLength)
769 REQRESIZE info;
771 info.nmhdr.hwndFrom = editor->hWnd;
772 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
773 info.nmhdr.code = EN_REQUESTRESIZE;
774 info.rc = rc;
775 info.rc.bottom = editor->nTotalLength;
777 editor->nEventMask &= ~ENM_REQUESTRESIZE;
778 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,
779 info.nmhdr.idFrom, (LPARAM)&info);
780 editor->nEventMask |= ENM_REQUESTRESIZE;