richedit: Fixed centering and right align with word wrap disabled.
[wine/wine64.git] / dlls / riched20 / wrap.c
blob53f0bfbdbbdc10655b0d25fc0b5d92685ff3836c
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
82 - wc->context->editor->selofs;
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 = ME_GetParagraph(wc->pRowStart);
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 while (len && *(text--) == ' ')
119 len--;
120 if (len)
122 if (len == p->member.run.strText->nLen)
124 width += p->member.run.nWidth;
125 } else {
126 sz = ME_GetRunSize(wc->context, &para->member.para,
127 &p->member.run, len, p->member.run.pt.x);
128 width += sz.cx;
131 bSkippingSpaces = !len;
132 } else if (!(p->member.run.nFlags & MERF_ENDPARA))
133 width += p->member.run.nWidth;
137 row = ME_MakeRow(ascent+descent, ascent, width);
138 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
139 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
141 /* The text was shifted down in ME_BeginRow so move the wrap context
142 * back to where it should be. */
143 wc->pt.y--;
144 /* The height of the row is increased by the borders. */
145 row->member.row.nHeight += 2;
147 row->member.row.pt = wc->pt;
148 row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
149 row->member.row.nRMargin = wc->nRightMargin;
150 assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
151 align = para->member.para.pFmt->wAlignment;
152 if (align == PFA_CENTER)
153 shift = max((wc->nAvailWidth-width)/2, 0);
154 if (align == PFA_RIGHT)
155 shift = max(wc->nAvailWidth-width, 0);
156 for (p = wc->pRowStart; p!=pEnd; p = p->next)
158 if (p->type==diRun) { /* FIXME add more run types */
159 p->member.run.pt.x += row->member.row.nLMargin+shift;
162 ME_InsertBefore(wc->pRowStart, row);
163 wc->nRow++;
164 wc->pt.y += row->member.row.nHeight;
165 ME_BeginRow(wc, para);
168 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
170 ME_DisplayItem *para = p->member.para.prev_para;
171 PARAFORMAT2 *pFmt = para->member.para.pFmt;
172 if (wc->pRowStart)
173 ME_InsertRowStart(wc, p);
174 if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
175 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
177 /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
178 * text down by one pixel for the border, so fix up the wrap context. */
179 wc->pt.y--;
183 p = p->member.para.prev_para->next;
184 while(p) {
185 if (p->type == diParagraph || p->type == diTextEnd)
186 return;
187 if (p->type == diRun)
189 ME_Run *run = &p->member.run;
190 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
192 p = p->next;
197 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
199 /* FIXME compose style (out of character and paragraph styles) here */
201 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
203 ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
204 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
207 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
209 ME_DisplayItem *pp, *piter = p;
210 int j;
211 if (!i)
212 return NULL;
213 j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
214 if (j>0) {
215 pp = ME_SplitRun(wc, piter, j);
216 wc->pt.x += piter->member.run.nWidth;
217 return pp;
219 else
221 pp = piter;
222 /* omit all spaces before split point */
223 while(piter != wc->pRowStart)
225 piter = ME_FindItemBack(piter, diRun);
226 if (piter->member.run.nFlags & MERF_WHITESPACE)
228 pp = piter;
229 continue;
231 if (piter->member.run.nFlags & MERF_ENDWHITE)
233 j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
234 pp = ME_SplitRun(wc, piter, i);
235 wc->pt = pp->member.run.pt;
236 return pp;
238 /* this run is the end of spaces, so the run edge is a good point to split */
239 wc->pt = pp->member.run.pt;
240 wc->bOverflown = TRUE;
241 TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData));
242 return pp;
244 wc->pt = piter->member.run.pt;
245 return piter;
249 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
251 ME_DisplayItem *piter = p, *pp;
252 int i, idesp, len;
253 ME_Run *run = &p->member.run;
255 idesp = i = ME_CharFromPoint(wc->context, loc, run);
256 len = ME_StrVLen(run->strText);
257 assert(len>0);
258 assert(i<len);
259 if (i) {
260 /* don't split words */
261 i = ME_ReverseFindWhitespaceV(run->strText, i);
262 pp = ME_MaximizeSplit(wc, p, i);
263 if (pp)
264 return pp;
266 TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
267 if (wc->pLastSplittableRun)
269 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
271 wc->pt = wc->ptLastSplittableRun;
272 return wc->pLastSplittableRun;
274 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
276 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
277 they serve no other purpose */
278 ME_UpdateRunFlags(wc->context->editor, run);
279 assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
281 piter = wc->pLastSplittableRun;
282 run = &piter->member.run;
283 len = ME_StrVLen(run->strText);
284 /* don't split words */
285 i = ME_ReverseFindWhitespaceV(run->strText, len);
286 if (i == len)
287 i = ME_ReverseFindNonWhitespaceV(run->strText, len);
288 if (i) {
289 ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
290 wc->pt = piter2->member.run.pt;
291 return piter2;
293 /* splittable = must have whitespaces */
294 assert(0 == "Splittable, but no whitespaces");
296 else
298 /* restart from the first run beginning with spaces */
299 wc->pt = wc->ptLastSplittableRun;
300 return wc->pLastSplittableRun;
303 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData));
304 /* OK, no better idea, so assume we MAY split words if we can split at all*/
305 if (idesp)
306 return ME_SplitRun(wc, piter, idesp);
307 else
308 if (wc->pRowStart && piter != wc->pRowStart)
310 /* don't need to break current run, because it's possible to split
311 before this run */
312 wc->bOverflown = TRUE;
313 return piter;
315 else
317 /* split point inside first character - no choice but split after that char */
318 int chars = 1;
319 int pos2 = ME_StrRelPos(run->strText, 0, &chars);
320 if (pos2 != len) {
321 /* the run is more than 1 char, so we may split */
322 return ME_SplitRun(wc, piter, pos2);
324 /* the run is one char, can't split it */
325 return piter;
329 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
331 ME_DisplayItem *pp;
332 ME_Run *run;
333 int len;
335 assert(p->type == diRun);
336 if (!wc->pRowStart)
337 wc->pRowStart = p;
338 run = &p->member.run;
339 run->pt.x = wc->pt.x;
340 run->pt.y = wc->pt.y;
341 ME_WrapSizeRun(wc, p);
342 len = ME_StrVLen(run->strText);
344 if (wc->bOverflown) /* just skipping final whitespaces */
346 /* End paragraph run can't overflow to the next line by itself. */
347 if (run->nFlags & MERF_ENDPARA)
348 return p->next;
350 if (run->nFlags & MERF_WHITESPACE) {
351 p->member.run.nFlags |= MERF_SKIPPED;
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 p->member.run.nFlags |= MERF_SKIPPED;
365 ME_InsertRowStart(wc, pp);
366 return pp;
369 /* black run: the row goes from pRowStart to the previous run */
370 ME_InsertRowStart(wc, p);
371 return p;
373 /* simply end the current row and move on to next one */
374 if (run->nFlags & MERF_ENDROW)
376 p = p->next;
377 ME_InsertRowStart(wc, p);
378 return p;
381 /* will current run fit? */
382 if (wc->bWordWrap &&
383 wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
385 int loc = wc->context->pt.x + wc->nAvailWidth - wc->pt.x;
386 /* total white run ? */
387 if (run->nFlags & MERF_WHITESPACE) {
388 /* let the overflow logic handle it */
389 wc->bOverflown = TRUE;
390 return p;
392 /* TAB: we can split before */
393 if (run->nFlags & MERF_TAB) {
394 wc->bOverflown = TRUE;
395 if (wc->pRowStart == p)
396 /* Don't split before the start of the run, or we will get an
397 * endless loop. */
398 return p->next;
399 else
400 return p;
402 /* graphics: we can split before, if run's width is smaller than row's width */
403 if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
404 wc->bOverflown = TRUE;
405 return p;
407 /* can we separate out the last spaces ? (to use overflow logic later) */
408 if (run->nFlags & MERF_ENDWHITE)
410 /* we aren't sure if it's *really* necessary, it's a good start however */
411 int black = ME_ReverseFindNonWhitespaceV(run->strText, len);
412 ME_SplitRun(wc, p, black);
413 /* handle both parts again */
414 return p;
416 /* determine the split point by backtracking */
417 pp = ME_SplitByBacktracking(wc, p, loc);
418 if (pp == wc->pRowStart)
420 if (run->nFlags & MERF_STARTWHITE)
422 /* We had only spaces so far, so we must be on the first line of the
423 * paragraph (or the first line after MERF_ENDROW forced the line
424 * break within the paragraph), since no other lines of the paragraph
425 * start with spaces. */
427 /* The lines will only contain spaces, and the rest of the run will
428 * overflow onto the next line. */
429 wc->bOverflown = TRUE;
430 return p;
432 /* Couldn't split the first run, possible because we have a large font
433 * with a single character that caused an overflow.
435 wc->pt.x += run->nWidth;
436 return p->next;
438 if (p != pp) /* found a suitable split point */
440 wc->bOverflown = TRUE;
441 return pp;
443 /* we detected that it's best to split on start of this run */
444 if (wc->bOverflown)
445 return pp;
446 ERR("failure!\n");
447 /* not found anything - writing over margins is the only option left */
449 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
450 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
452 wc->pLastSplittableRun = p;
453 wc->ptLastSplittableRun = wc->pt;
455 wc->pt.x += run->nWidth;
456 return p->next;
459 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
461 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp, DWORD beginofs) {
462 ME_DisplayItem *p;
463 ME_WrapContext wc;
464 int border = 0;
465 int linespace = 0;
466 PARAFORMAT2 *pFmt;
468 assert(tp->type == diParagraph);
469 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
470 return;
472 ME_PrepareParagraphForWrapping(c, tp);
473 pFmt = tp->member.para.pFmt;
475 wc.context = c;
476 /* wc.para_style = tp->member.para.style; */
477 wc.style = NULL;
478 if (tp->member.para.nFlags & MEPF_ROWEND) {
479 wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
480 } else {
481 int dxStartIndent = pFmt->dxStartIndent;
482 if (tp->member.para.pCell) {
483 dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
485 wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
486 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
487 wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
489 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
490 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
492 wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
494 wc.nRow = 0;
495 wc.pt.y = 0;
496 if (pFmt->dwMask & PFM_SPACEBEFORE)
497 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
498 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
499 pFmt->dwMask & PFM_BORDER)
501 border = ME_GetParaBorderWidth(c->editor, tp->member.para.pFmt->wBorders);
502 if (pFmt->wBorders & 1) {
503 wc.nFirstMargin += border;
504 wc.nLeftMargin += border;
506 if (pFmt->wBorders & 2)
507 wc.nRightMargin -= border;
508 if (pFmt->wBorders & 4)
509 wc.pt.y += border;
512 linespace = ME_GetParaLineSpace(c, &tp->member.para);
514 ME_BeginRow(&wc, tp);
515 for (p = tp->next; p!=tp->member.para.next_para; ) {
516 assert(p->type != diStartRow);
517 if (p->type == diRun) {
518 p = ME_WrapHandleRun(&wc, p);
520 else p = p->next;
521 if (wc.nRow && p == wc.pRowStart)
522 wc.pt.y += linespace;
524 ME_WrapEndParagraph(&wc, p);
525 if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
526 (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
527 wc.pt.y += border;
528 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
529 wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
531 tp->member.para.nFlags &= ~MEPF_REWRAP;
532 tp->member.para.nHeight = wc.pt.y;
533 tp->member.para.nRows = wc.nRow;
537 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
538 ME_DisplayItem *p, *pRow;
540 /* remove all items that will be reinserted by paragraph wrapper anyway */
541 tp->member.para.nRows = 0;
542 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
543 switch(p->type) {
544 case diStartRow:
545 pRow = p;
546 p = p->prev;
547 ME_Remove(pRow);
548 ME_DestroyDisplayItem(pRow);
549 break;
550 default:
551 break;
554 /* join runs that can be joined, set up flags */
555 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
556 int changed = 0;
557 switch(p->type) {
558 case diStartRow: assert(0); break; /* should have deleted it */
559 case diRun:
560 while (p->next->type == diRun) { /* FIXME */
561 if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
562 ME_JoinRuns(c->editor, p);
563 changed = 1;
565 else
566 break;
568 p->member.run.nFlags &= ~MERF_CALCBYWRAP;
569 break;
570 default:
571 break;
576 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) {
577 ME_DisplayItem *item;
578 ME_Context c;
579 BOOL bModified = FALSE;
580 int yStart = -1;
581 int yLastPos = 0;
583 ME_InitContext(&c, editor, GetDC(editor->hWnd));
584 c.pt.x = editor->selofs;
585 editor->nHeight = 0;
586 item = editor->pBuffer->pFirst->next;
587 while(item != editor->pBuffer->pLast) {
588 BOOL bRedraw = FALSE;
590 assert(item->type == diParagraph);
591 editor->nHeight = max(editor->nHeight, item->member.para.pt.y);
592 if ((item->member.para.nFlags & MEPF_REWRAP)
593 || (item->member.para.pt.y != c.pt.y))
594 bRedraw = TRUE;
595 item->member.para.pt = c.pt;
597 ME_WrapTextParagraph(&c, item, editor->selofs);
599 if (bRedraw)
601 item->member.para.nFlags |= MEPF_REPAINT;
602 if (yStart == -1)
603 yStart = c.pt.y;
606 bModified = bModified | bRedraw;
608 yLastPos = max(yLastPos, c.pt.y);
610 if (item->member.para.nFlags & MEPF_ROWSTART)
612 ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
613 ME_DisplayItem *endRowPara;
614 int borderWidth = 0;
615 cell->member.cell.pt = c.pt;
616 /* Offset the text by the largest top border width. */
617 while (cell->member.cell.next_cell) {
618 borderWidth = max(borderWidth, cell->member.cell.border.top.width);
619 cell = cell->member.cell.next_cell;
621 endRowPara = ME_FindItemFwd(cell, diParagraph);
622 assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
623 if (borderWidth > 0)
625 borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
626 while (cell) {
627 cell->member.cell.yTextOffset = borderWidth;
628 cell = cell->member.cell.prev_cell;
630 c.pt.y += borderWidth;
632 if (endRowPara->member.para.pFmt->dxStartIndent > 0)
634 int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
635 cell = ME_FindItemFwd(item, diCell);
636 cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
637 c.pt.x = cell->member.cell.pt.x;
640 else if (item->member.para.nFlags & MEPF_ROWEND)
642 /* Set all the cells to the height of the largest cell */
643 ME_DisplayItem *startRowPara;
644 int prevHeight, nHeight, bottomBorder = 0;
645 ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
646 if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
648 /* Last row, the bottom border is added to the height. */
649 cell = cell->member.cell.prev_cell;
650 while (cell)
652 bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
653 cell = cell->member.cell.prev_cell;
655 bottomBorder = ME_twips2pointsY(&c, bottomBorder);
656 cell = ME_FindItemBack(item, diCell);
658 prevHeight = cell->member.cell.nHeight;
659 nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
660 cell->member.cell.nHeight = nHeight;
661 item->member.para.nHeight = nHeight;
662 cell = cell->member.cell.prev_cell;
663 cell->member.cell.nHeight = nHeight;
664 while (cell->member.cell.prev_cell)
666 cell = cell->member.cell.prev_cell;
667 cell->member.cell.nHeight = nHeight;
669 /* Also set the height of the start row paragraph */
670 startRowPara = ME_FindItemBack(cell, diParagraph);
671 startRowPara->member.para.nHeight = nHeight;
672 c.pt.x = startRowPara->member.para.pt.x;
673 c.pt.y = cell->member.cell.pt.y + nHeight;
674 if (prevHeight < nHeight)
676 /* The height of the cells has grown, so invalidate the bottom of
677 * the cells. */
678 item->member.para.nFlags |= MEPF_REPAINT;
679 cell = ME_FindItemBack(item, diCell);
680 while (cell) {
681 ME_FindItemBack(cell, diParagraph)->member.para.nFlags |= MEPF_REPAINT;
682 cell = cell->member.cell.prev_cell;
686 else if (item->member.para.pCell &&
687 item->member.para.pCell != item->member.para.next_para->member.para.pCell)
689 /* The next paragraph is in the next cell in the table row. */
690 ME_Cell *cell = &item->member.para.pCell->member.cell;
691 cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
693 /* Propagate the largest height to the end so that it can be easily
694 * sent back to all the cells at the end of the row. */
695 if (cell->prev_cell)
696 cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
698 c.pt.x = cell->pt.x + cell->nWidth;
699 c.pt.y = cell->pt.y;
700 cell->next_cell->member.cell.pt = c.pt;
701 c.pt.y += cell->yTextOffset;
703 else
705 if (item->member.para.pCell) {
706 /* Next paragraph in the same cell. */
707 c.pt.x = item->member.para.pCell->member.cell.pt.x;
708 } else {
709 /* Normal paragraph */
710 c.pt.x = editor->selofs;
712 c.pt.y += item->member.para.nHeight;
714 item = item->member.para.next_para;
716 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
717 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
719 editor->nTotalLength = c.pt.y;
720 editor->pBuffer->pLast->member.para.pt.x = 0;
721 editor->pBuffer->pLast->member.para.pt.y = yLastPos;
723 ME_DestroyContext(&c, editor->hWnd);
725 /* Each paragraph may contain multiple rows, which should be scrollable, even
726 if the containing paragraph has pt.y == 0 */
727 item = editor->pBuffer->pFirst;
728 while ((item = ME_FindItemFwd(item, diStartRow)) != NULL) {
729 assert(item->type == diStartRow);
730 editor->nHeight = max(editor->nHeight, item->member.row.pt.y);
733 if (bModified || editor->nTotalLength < editor->nLastTotalLength)
734 ME_InvalidateMarkedParagraphs(editor);
735 return bModified;
738 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) {
739 ME_Context c;
741 ME_InitContext(&c, editor, GetDC(editor->hWnd));
742 if (editor->bRedraw)
744 RECT rc = c.rcView;
745 int ofs = ME_GetYScrollPos(editor);
747 ME_DisplayItem *item = editor->pBuffer->pFirst;
748 while(item != editor->pBuffer->pLast) {
749 if (item->member.para.nFlags & MEPF_REPAINT) {
750 rc.top = item->member.para.pt.y - ofs;
751 rc.bottom = item->member.para.pt.y + item->member.para.nHeight - ofs;
752 InvalidateRect(editor->hWnd, &rc, TRUE);
754 item = item->member.para.next_para;
756 if (editor->nTotalLength < editor->nLastTotalLength)
758 rc.top = editor->nTotalLength - ofs;
759 rc.bottom = editor->nLastTotalLength - ofs;
760 InvalidateRect(editor->hWnd, &rc, TRUE);
763 ME_DestroyContext(&c, editor->hWnd);
767 void
768 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
770 if (editor->nEventMask & ENM_REQUESTRESIZE)
772 RECT rc;
774 GetClientRect(editor->hWnd, &rc);
776 if (force || rc.bottom != editor->nTotalLength)
778 REQRESIZE info;
780 info.nmhdr.hwndFrom = editor->hWnd;
781 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
782 info.nmhdr.code = EN_REQUESTRESIZE;
783 info.rc = rc;
784 info.rc.bottom = editor->nTotalLength;
786 editor->nEventMask &= ~ENM_REQUESTRESIZE;
787 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,
788 info.nmhdr.idFrom, (LPARAM)&info);
789 editor->nEventMask |= ENM_REQUESTRESIZE;