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