gdiplus: Use WIC to decode PNG files.
[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           if (~p->member.run.nFlags & MERF_GRAPHICS)
119             while (len && *(text--) == ' ')
120               len--;
121           if (len)
122           {
123               if (len == p->member.run.strText->nLen)
124               {
125                   width += p->member.run.nWidth;
126               } else {
127                   sz = ME_GetRunSize(wc->context, &para->member.para,
128                                      &p->member.run, len, p->member.run.pt.x);
129                   width += sz.cx;
130               }
131           }
132           bSkippingSpaces = !len;
133         } else if (!(p->member.run.nFlags & MERF_ENDPARA))
134           width += p->member.run.nWidth;
135       }
136   }
137
138   para->member.para.nWidth = max(para->member.para.nWidth, width);
139   row = ME_MakeRow(ascent+descent, ascent, width);
140   if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
141       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
142   {
143     /* The text was shifted down in ME_BeginRow so move the wrap context
144      * back to where it should be. */
145     wc->pt.y--;
146     /* The height of the row is increased by the borders. */
147     row->member.row.nHeight += 2;
148   }
149   row->member.row.pt = wc->pt;
150   row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
151   row->member.row.nRMargin = wc->nRightMargin;
152   assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
153   align = para->member.para.pFmt->wAlignment;
154   if (align == PFA_CENTER)
155     shift = max((wc->nAvailWidth-width)/2, 0);
156   if (align == PFA_RIGHT)
157     shift = max(wc->nAvailWidth-width, 0);
158   for (p = wc->pRowStart; p!=pEnd; p = p->next)
159   {
160     if (p->type==diRun) { /* FIXME add more run types */
161       p->member.run.pt.x += row->member.row.nLMargin+shift;
162     }
163   }
164   ME_InsertBefore(wc->pRowStart, row);
165   wc->nRow++;
166   wc->pt.y += row->member.row.nHeight;
167   ME_BeginRow(wc);
168 }
169
170 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
171 {
172   ME_DisplayItem *para = wc->pPara;
173   PARAFORMAT2 *pFmt = para->member.para.pFmt;
174   if (wc->pRowStart)
175     ME_InsertRowStart(wc, p);
176   if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
177       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
178   {
179     /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
180      * text down by one pixel for the border, so fix up the wrap context. */
181     wc->pt.y--;
182   }
183
184   /*
185   p = para->next;
186   while(p) {
187     if (p->type == diParagraph || p->type == diTextEnd)
188       return;
189     if (p->type == diRun)
190     {
191       ME_Run *run = &p->member.run;
192       TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
193     }
194     p = p->next;
195   }
196   */
197 }
198
199 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
200 {
201   /* FIXME compose style (out of character and paragraph styles) here */
202
203   ME_UpdateRunFlags(wc->context->editor, &p->member.run);
204
205   ME_CalcRunExtent(wc->context, &wc->pPara->member.para,
206                    wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
207 }
208
209 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
210 {
211   ME_DisplayItem *pp, *piter = p;
212   int j;
213   if (!i)
214     return NULL;
215   j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
216   if (j>0) {
217     pp = ME_SplitRun(wc, piter, j);
218     wc->pt.x += piter->member.run.nWidth;
219     return pp;
220   }
221   else
222   {
223     pp = piter;
224     /* omit all spaces before split point */
225     while(piter != wc->pRowStart)
226     {
227       piter = ME_FindItemBack(piter, diRun);
228       if (piter->member.run.nFlags & MERF_WHITESPACE)
229       {
230         pp = piter;
231         continue;
232       }
233       if (piter->member.run.nFlags & MERF_ENDWHITE)
234       {
235         j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
236         pp = ME_SplitRun(wc, piter, i);
237         wc->pt = pp->member.run.pt;
238         return pp;
239       }
240       /* this run is the end of spaces, so the run edge is a good point to split */
241       wc->pt = pp->member.run.pt;
242       wc->bOverflown = TRUE;
243       TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData));
244       return pp;
245     }
246     wc->pt = piter->member.run.pt;
247     return piter;
248   }
249 }
250
251 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
252 {
253   ME_DisplayItem *piter = p, *pp;
254   int i, idesp, len;
255   ME_Run *run = &p->member.run;
256
257   idesp = i = ME_CharFromPoint(wc->context, loc, run);
258   len = run->strText->nLen;
259   assert(len>0);
260   assert(i<len);
261   if (i) {
262     /* don't split words */
263     i = ME_ReverseFindWhitespaceV(run->strText, i);
264     pp = ME_MaximizeSplit(wc, p, i);
265     if (pp)
266       return pp;
267   }
268   TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
269   if (wc->pLastSplittableRun)
270   {
271     if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
272     {
273       wc->pt = wc->ptLastSplittableRun;
274       return wc->pLastSplittableRun;
275     }
276     else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
277     {
278       /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
279          they serve no other purpose */
280       ME_UpdateRunFlags(wc->context->editor, run);
281       assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
282
283       piter = wc->pLastSplittableRun;
284       run = &piter->member.run;
285       len = run->strText->nLen;
286       /* don't split words */
287       i = ME_ReverseFindWhitespaceV(run->strText, len);
288       if (i == len)
289         i = ME_ReverseFindNonWhitespaceV(run->strText, len);
290       if (i) {
291         ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
292         wc->pt = piter2->member.run.pt;
293         return piter2;
294       }
295       /* splittable = must have whitespaces */
296       assert(0 == "Splittable, but no whitespaces");
297     }
298     else
299     {
300       /* restart from the first run beginning with spaces */
301       wc->pt = wc->ptLastSplittableRun;
302       return wc->pLastSplittableRun;
303     }
304   }
305   TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData));
306   /* OK, no better idea, so assume we MAY split words if we can split at all*/
307   if (idesp)
308     return ME_SplitRun(wc, piter, idesp);
309   else
310   if (wc->pRowStart && piter != wc->pRowStart)
311   {
312     /* don't need to break current run, because it's possible to split
313        before this run */
314     wc->bOverflown = TRUE;
315     return piter;
316   }
317   else
318   {
319     /* split point inside first character - no choice but split after that char */
320     if (len != 1) {
321       /* the run is more than 1 char, so we may split */
322       return ME_SplitRun(wc, piter, 1);
323     }
324     /* the run is one char, can't split it */
325     return piter;
326   }
327 }
328
329 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
330 {
331   ME_DisplayItem *pp;
332   ME_Run *run;
333   int len;
334
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 = run->strText->nLen;
343
344   if (wc->bOverflown) /* just skipping final whitespaces */
345   {
346     /* End paragraph run can't overflow to the next line by itself. */
347     if (run->nFlags & MERF_ENDPARA)
348       return p->next;
349
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;
355     }
356
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;
367       }
368     }
369     /* black run: the row goes from pRowStart to the previous run */
370     ME_InsertRowStart(wc, p);
371     return p;
372   }
373   /* simply end the current row and move on to next one */
374   if (run->nFlags & MERF_ENDROW)
375   {
376     p = p->next;
377     ME_InsertRowStart(wc, p);
378     return p;
379   }
380
381   /* will current run fit? */
382   if (wc->bWordWrap &&
383       wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
384   {
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;
391     }
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;
401     }
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;
406     }
407     /* can we separate out the last spaces ? (to use overflow logic later) */
408     if (run->nFlags & MERF_ENDWHITE)
409     {
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;
415     }
416     /* determine the split point by backtracking */
417     pp = ME_SplitByBacktracking(wc, p, loc);
418     if (pp == wc->pRowStart)
419     {
420       if (run->nFlags & MERF_STARTWHITE)
421       {
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. */
426
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;
431       }
432       /* Couldn't split the first run, possible because we have a large font
433        * with a single character that caused an overflow.
434        */
435       wc->pt.x += run->nWidth;
436       return p->next;
437     }
438     if (p != pp) /* found a suitable split point */
439     {
440       wc->bOverflown = TRUE;
441       return pp;
442     }
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 */
448   }
449   if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
450     || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
451   {
452     wc->pLastSplittableRun = p;
453     wc->ptLastSplittableRun = wc->pt;
454   }
455   wc->pt.x += run->nWidth;
456   return p->next;
457 }
458
459 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
460
461 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
462   ME_DisplayItem *p;
463   ME_WrapContext wc;
464   int border = 0;
465   int linespace = 0;
466   PARAFORMAT2 *pFmt;
467
468   assert(tp->type == diParagraph);
469   if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
470     return;
471   }
472   ME_PrepareParagraphForWrapping(c, tp);
473   pFmt = tp->member.para.pFmt;
474
475   wc.context = c;
476   wc.pPara = tp;
477 /*   wc.para_style = tp->member.para.style; */
478   wc.style = NULL;
479   if (tp->member.para.nFlags & MEPF_ROWEND) {
480     wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
481   } else {
482     int dxStartIndent = pFmt->dxStartIndent;
483     if (tp->member.para.pCell) {
484       dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
485     }
486     wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
487     wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
488     wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
489   }
490   if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
491       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
492   {
493     wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
494   }
495   wc.nRow = 0;
496   wc.pt.y = 0;
497   if (pFmt->dwMask & PFM_SPACEBEFORE)
498     wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
499   if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
500       pFmt->dwMask & PFM_BORDER)
501   {
502     border = ME_GetParaBorderWidth(c->editor, tp->member.para.pFmt->wBorders);
503     if (pFmt->wBorders & 1) {
504       wc.nFirstMargin += border;
505       wc.nLeftMargin += border;
506     }
507     if (pFmt->wBorders & 2)
508       wc.nRightMargin -= border;
509     if (pFmt->wBorders & 4)
510       wc.pt.y += border;
511   }
512
513   linespace = ME_GetParaLineSpace(c, &tp->member.para);
514
515   ME_BeginRow(&wc);
516   for (p = tp->next; p!=tp->member.para.next_para; ) {
517     assert(p->type != diStartRow);
518     if (p->type == diRun) {
519       p = ME_WrapHandleRun(&wc, p);
520     }
521     else p = p->next;
522     if (wc.nRow && p == wc.pRowStart)
523       wc.pt.y += linespace;
524   }
525   ME_WrapEndParagraph(&wc, p);
526   if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
527       (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
528     wc.pt.y += border;
529   if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
530     wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
531
532   tp->member.para.nFlags &= ~MEPF_REWRAP;
533   tp->member.para.nHeight = wc.pt.y;
534   tp->member.para.nRows = wc.nRow;
535 }
536
537
538 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
539   ME_DisplayItem *p, *pRow;
540
541   tp->member.para.nWidth = 0;
542   /* remove all items that will be reinserted by paragraph wrapper anyway */
543   tp->member.para.nRows = 0;
544   for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
545     switch(p->type) {
546       case diStartRow:
547         pRow = p;
548         p = p->prev;
549         ME_Remove(pRow);
550         ME_DestroyDisplayItem(pRow);
551         break;
552       default:
553         break;
554     }
555   }
556   /* join runs that can be joined, set up flags */
557   for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
558     int changed = 0;
559     switch(p->type) {
560       case diStartRow: assert(0); break; /* should have deleted it */
561       case diRun:
562         while (p->next->type == diRun) { /* FIXME */
563           if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
564             ME_JoinRuns(c->editor, p);
565             changed = 1;
566           }
567           else
568             break;
569         }
570         p->member.run.nFlags &= ~MERF_CALCBYWRAP;
571         break;
572       default:
573         break;
574     }
575   }
576 }
577
578 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
579 {
580   ME_DisplayItem *item;
581   ME_Context c;
582   BOOL bModified = FALSE;
583   int yStart = -1;
584   int totalWidth = 0;
585
586   ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
587   c.pt.x = 0;
588   item = editor->pBuffer->pFirst->next;
589   while(item != editor->pBuffer->pLast) {
590     BOOL bRedraw = FALSE;
591
592     assert(item->type == diParagraph);
593     if ((item->member.para.nFlags & MEPF_REWRAP)
594      || (item->member.para.pt.y != c.pt.y))
595       bRedraw = TRUE;
596     item->member.para.pt = c.pt;
597
598     ME_WrapTextParagraph(&c, item);
599
600     if (bRedraw)
601     {
602       item->member.para.nFlags |= MEPF_REPAINT;
603       if (yStart == -1)
604         yStart = c.pt.y;
605     }
606
607     bModified = bModified | bRedraw;
608
609     if (item->member.para.nFlags & MEPF_ROWSTART)
610     {
611       ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
612       ME_DisplayItem *endRowPara;
613       int borderWidth = 0;
614       cell->member.cell.pt = c.pt;
615       /* Offset the text by the largest top border width. */
616       while (cell->member.cell.next_cell) {
617         borderWidth = max(borderWidth, cell->member.cell.border.top.width);
618         cell = cell->member.cell.next_cell;
619       }
620       endRowPara = ME_FindItemFwd(cell, diParagraph);
621       assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
622       if (borderWidth > 0)
623       {
624         borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
625         while (cell) {
626           cell->member.cell.yTextOffset = borderWidth;
627           cell = cell->member.cell.prev_cell;
628         }
629         c.pt.y += borderWidth;
630       }
631       if (endRowPara->member.para.pFmt->dxStartIndent > 0)
632       {
633         int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
634         cell = ME_FindItemFwd(item, diCell);
635         cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
636         c.pt.x = cell->member.cell.pt.x;
637       }
638     }
639     else if (item->member.para.nFlags & MEPF_ROWEND)
640     {
641       /* Set all the cells to the height of the largest cell */
642       ME_DisplayItem *startRowPara;
643       int prevHeight, nHeight, bottomBorder = 0;
644       ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
645       item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
646       if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
647       {
648         /* Last row, the bottom border is added to the height. */
649         cell = cell->member.cell.prev_cell;
650         while (cell)
651         {
652           bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
653           cell = cell->member.cell.prev_cell;
654         }
655         bottomBorder = ME_twips2pointsY(&c, bottomBorder);
656         cell = ME_FindItemBack(item, diCell);
657       }
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)
665       {
666         cell = cell->member.cell.prev_cell;
667         cell->member.cell.nHeight = nHeight;
668       }
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)
675       {
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;
683         }
684       }
685     }
686     else if (item->member.para.pCell &&
687              item->member.para.pCell != item->member.para.next_para->member.para.pCell)
688     {
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;
692
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);
697
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       if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
702         c.pt.y += cell->yTextOffset;
703     }
704     else
705     {
706       if (item->member.para.pCell) {
707         /* Next paragraph in the same cell. */
708         c.pt.x = item->member.para.pCell->member.cell.pt.x;
709       } else {
710         /* Normal paragraph */
711         c.pt.x = 0;
712       }
713       c.pt.y += item->member.para.nHeight;
714     }
715
716     totalWidth = max(totalWidth, item->member.para.nWidth);
717     item = item->member.para.next_para;
718   }
719   editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
720   editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
721
722   editor->nTotalLength = c.pt.y;
723   editor->nTotalWidth = totalWidth;
724   editor->pBuffer->pLast->member.para.pt.x = 0;
725   editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
726
727   ME_DestroyContext(&c);
728
729   if (bModified || editor->nTotalLength < editor->nLastTotalLength)
730     ME_InvalidateMarkedParagraphs(editor);
731   return bModified;
732 }
733
734 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor)
735 {
736   ME_Context c;
737   RECT rc;
738   int ofs;
739   ME_DisplayItem *item;
740
741   ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
742   rc = c.rcView;
743   ofs = editor->vert_si.nPos;
744
745   item = editor->pBuffer->pFirst;
746   while(item != editor->pBuffer->pLast) {
747     if (item->member.para.nFlags & MEPF_REPAINT) {
748       rc.top = c.rcView.top + item->member.para.pt.y - ofs;
749       rc.bottom = max(c.rcView.top + item->member.para.pt.y
750                       + item->member.para.nHeight - ofs,
751                       c.rcView.bottom);
752       ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
753     }
754     item = item->member.para.next_para;
755   }
756   if (editor->nTotalLength < editor->nLastTotalLength)
757   {
758     rc.top = c.rcView.top + editor->nTotalLength - ofs;
759     rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
760     ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
761   }
762   ME_DestroyContext(&c);
763 }
764
765
766 void
767 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
768 {
769   if (editor->nEventMask & ENM_REQUESTRESIZE)
770   {
771     RECT rc;
772
773     ITextHost_TxGetClientRect(editor->texthost, &rc);
774
775     if (force || rc.bottom != editor->nTotalLength)
776     {
777       REQRESIZE info;
778
779       info.nmhdr.code = EN_REQUESTRESIZE;
780       info.rc = rc;
781       info.rc.right = editor->nTotalWidth;
782       info.rc.bottom = editor->nTotalLength;
783
784       editor->nEventMask &= ~ENM_REQUESTRESIZE;
785       ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
786       editor->nEventMask |= ENM_REQUESTRESIZE;
787     }
788   }
789 }