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