Release 1.5.29.
[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 /******************************************************************************
36  * calc_run_extent
37  *
38  * Updates the size of the run (fills width, ascent and descent). The height
39  * is calculated based on whole row's ascent and descent anyway, so no need
40  * to use it here.
41  */
42 static void calc_run_extent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
43 {
44     if (run->nFlags & MERF_HIDDEN) run->nWidth = 0;
45     else
46     {
47         SIZE size = ME_GetRunSizeCommon( c, para, run, run->len, startx, &run->nAscent, &run->nDescent );
48         run->nWidth = size.cx;
49     }
50 }
51
52 /******************************************************************************
53  * split_run_extents
54  *
55  * Splits a run into two in a given place. It also updates the screen position
56  * and size (extent) of the newly generated runs.
57  */
58 static ME_DisplayItem *split_run_extents(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
59 {
60   ME_TextEditor *editor = wc->context->editor;
61   ME_Run *run, *run2;
62   ME_Paragraph *para = &wc->pPara->member.para;
63   ME_Cursor cursor = {wc->pPara, item, nVChar};
64
65   assert(item->member.run.nCharOfs != -1);
66   if(TRACE_ON(richedit))
67   {
68     TRACE("Before check before split\n");
69     ME_CheckCharOffsets(editor);
70     TRACE("After check before split\n");
71   }
72
73   run = &item->member.run;
74
75   TRACE("Before split: %s(%d, %d)\n", debugstr_run( run ),
76         run->pt.x, run->pt.y);
77
78   ME_SplitRunSimple(editor, &cursor);
79
80   run2 = &cursor.pRun->member.run;
81
82   calc_run_extent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
83
84   run2->pt.x = run->pt.x+run->nWidth;
85   run2->pt.y = run->pt.y;
86
87   if(TRACE_ON(richedit))
88   {
89     TRACE("Before check after split\n");
90     ME_CheckCharOffsets(editor);
91     TRACE("After check after split\n");
92     TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
93       debugstr_run( run ), run->pt.x, run->pt.y,
94       debugstr_run( run2 ), run2->pt.x, run2->pt.y);
95   }
96
97   return cursor.pRun;
98 }
99
100 /******************************************************************************
101  * find_split_point
102  *
103  * Returns a character position to split inside the run given a run-relative
104  * pixel horizontal position. This version rounds left (ie. if the second
105  * character is at pixel position 8, then for cx=0..7 it returns 0).
106  */
107 static int find_split_point( ME_Context *c, int cx, ME_Run *run )
108 {
109     if (!run->len || cx <= 0) return 0;
110     return ME_CharFromPointContext( c, cx, run, FALSE, FALSE );
111 }
112
113 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
114 {
115   ME_DisplayItem *item = ME_MakeDI(diStartRow);
116
117   item->member.row.nHeight = height;
118   item->member.row.nBaseline = baseline;
119   item->member.row.nWidth = width;
120   return item;
121 }
122
123 static void ME_BeginRow(ME_WrapContext *wc)
124 {
125   PARAFORMAT2 *pFmt;
126   ME_DisplayItem *para = wc->pPara;
127
128   pFmt = para->member.para.pFmt;
129   wc->pRowStart = NULL;
130   wc->bOverflown = FALSE;
131   wc->pLastSplittableRun = NULL;
132   wc->bWordWrap = wc->context->editor->bWordWrap;
133   if (para->member.para.nFlags & (MEPF_ROWSTART|MEPF_ROWEND)) {
134     wc->nAvailWidth = 0;
135     wc->bWordWrap = FALSE;
136     if (para->member.para.nFlags & MEPF_ROWEND)
137     {
138       ME_Cell *cell = &ME_FindItemBack(para, diCell)->member.cell;
139       cell->nWidth = 0;
140     }
141   } else if (para->member.para.pCell) {
142     ME_Cell *cell = &para->member.para.pCell->member.cell;
143     int width;
144
145     width = cell->nRightBoundary;
146     if (cell->prev_cell)
147       width -= cell->prev_cell->member.cell.nRightBoundary;
148     if (!cell->prev_cell)
149     {
150       int rowIndent = ME_GetTableRowEnd(para)->member.para.pFmt->dxStartIndent;
151       width -= rowIndent;
152     }
153     cell->nWidth = max(ME_twips2pointsX(wc->context, width), 0);
154
155     wc->nAvailWidth = cell->nWidth
156         - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
157     wc->bWordWrap = TRUE;
158   } else {
159     wc->nAvailWidth = wc->context->nAvailWidth
160         - (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
161   }
162   wc->pt.x = wc->context->pt.x;
163   if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
164       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
165     /* Shift the text down because of the border. */
166     wc->pt.y++;
167 }
168
169 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
170 {
171   ME_DisplayItem *p, *row, *para;
172   BOOL bSkippingSpaces = TRUE;
173   int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
174   PARAFORMAT2 *pFmt;
175   /* wrap text */
176   para = wc->pPara;
177   pFmt = para->member.para.pFmt;
178
179   for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
180   {
181       /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
182       if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
183         if (p->member.run.nAscent>ascent)
184           ascent = p->member.run.nAscent;
185         if (p->member.run.nDescent>descent)
186           descent = p->member.run.nDescent;
187         if (bSkippingSpaces)
188         {
189           /* Exclude space characters from run width.
190            * Other whitespace or delimiters are not treated this way. */
191           int len = p->member.run.len;
192           WCHAR *text = get_text( &p->member.run, len - 1 );
193
194           assert (len);
195           if (~p->member.run.nFlags & MERF_GRAPHICS)
196             while (len && *(text--) == ' ')
197               len--;
198           if (len)
199           {
200               if (len == p->member.run.len)
201                   width += p->member.run.nWidth;
202               else
203                   width += ME_PointFromCharContext( wc->context, &p->member.run, len, FALSE );
204           }
205           bSkippingSpaces = !len;
206         } else if (!(p->member.run.nFlags & MERF_ENDPARA))
207           width += p->member.run.nWidth;
208       }
209   }
210
211   para->member.para.nWidth = max(para->member.para.nWidth, width);
212   row = ME_MakeRow(ascent+descent, ascent, width);
213   if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
214       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
215   {
216     /* The text was shifted down in ME_BeginRow so move the wrap context
217      * back to where it should be. */
218     wc->pt.y--;
219     /* The height of the row is increased by the borders. */
220     row->member.row.nHeight += 2;
221   }
222   row->member.row.pt = wc->pt;
223   row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
224   row->member.row.nRMargin = wc->nRightMargin;
225   assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
226   align = para->member.para.pFmt->wAlignment;
227   if (align == PFA_CENTER)
228     shift = max((wc->nAvailWidth-width)/2, 0);
229   if (align == PFA_RIGHT)
230     shift = max(wc->nAvailWidth-width, 0);
231   row->member.row.pt.x = row->member.row.nLMargin + shift;
232   for (p = wc->pRowStart; p!=pEnd; p = p->next)
233   {
234     if (p->type==diRun) { /* FIXME add more run types */
235       p->member.run.pt.x += row->member.row.nLMargin+shift;
236     }
237   }
238   ME_InsertBefore(wc->pRowStart, row);
239   wc->nRow++;
240   wc->pt.y += row->member.row.nHeight;
241   ME_BeginRow(wc);
242 }
243
244 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
245 {
246   ME_DisplayItem *para = wc->pPara;
247   PARAFORMAT2 *pFmt = para->member.para.pFmt;
248   if (wc->pRowStart)
249     ME_InsertRowStart(wc, p);
250   if (wc->context->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
251       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
252   {
253     /* ME_BeginRow was called an extra time for the paragraph, and it shifts the
254      * text down by one pixel for the border, so fix up the wrap context. */
255     wc->pt.y--;
256   }
257
258   /*
259   p = para->next;
260   while(p) {
261     if (p->type == diParagraph || p->type == diTextEnd)
262       return;
263     if (p->type == diRun)
264     {
265       ME_Run *run = &p->member.run;
266       TRACE("%s - (%d, %d)\n", debugstr_run(run), run->pt.x, run->pt.y);
267     }
268     p = p->next;
269   }
270   */
271 }
272
273 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
274 {
275   /* FIXME compose style (out of character and paragraph styles) here */
276
277   ME_UpdateRunFlags(wc->context->editor, &p->member.run);
278
279   calc_run_extent(wc->context, &wc->pPara->member.para,
280                   wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
281 }
282
283
284 static int find_non_whitespace(const WCHAR *s, int len, int start)
285 {
286   int i;
287   for (i = start; i < len && ME_IsWSpace( s[i] ); i++)
288     ;
289
290   return i;
291 }
292
293 /* note: these two really return the first matching offset (starting from EOS)+1
294  * in other words, an offset of the first trailing white/black */
295
296 /* note: returns offset of the first trailing whitespace */
297 static int reverse_find_non_whitespace(const WCHAR *s, int start)
298 {
299   int i;
300   for (i = start; i > 0 && ME_IsWSpace( s[i - 1] ); i--)
301     ;
302
303   return i;
304 }
305
306 /* note: returns offset of the first trailing nonwhitespace */
307 static int reverse_find_whitespace(const WCHAR *s, int start)
308 {
309   int i;
310   for (i = start; i > 0 && !ME_IsWSpace( s[i - 1] ); i--)
311     ;
312
313   return i;
314 }
315
316 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
317 {
318   ME_DisplayItem *pp, *piter = p;
319   int j;
320   if (!i)
321     return NULL;
322   j = reverse_find_non_whitespace( get_text( &p->member.run, 0 ), i);
323   if (j>0) {
324     pp = split_run_extents(wc, piter, j);
325     wc->pt.x += piter->member.run.nWidth;
326     return pp;
327   }
328   else
329   {
330     pp = piter;
331     /* omit all spaces before split point */
332     while(piter != wc->pRowStart)
333     {
334       piter = ME_FindItemBack(piter, diRun);
335       if (piter->member.run.nFlags & MERF_WHITESPACE)
336       {
337         pp = piter;
338         continue;
339       }
340       if (piter->member.run.nFlags & MERF_ENDWHITE)
341       {
342         i = reverse_find_non_whitespace( get_text( &piter->member.run, 0 ),
343                                          piter->member.run.len );
344         pp = split_run_extents(wc, piter, i);
345         wc->pt = pp->member.run.pt;
346         return pp;
347       }
348       /* this run is the end of spaces, so the run edge is a good point to split */
349       wc->pt = pp->member.run.pt;
350       wc->bOverflown = TRUE;
351       TRACE("Split point is: %s|%s\n", debugstr_run( &piter->member.run ), debugstr_run( &pp->member.run ));
352       return pp;
353     }
354     wc->pt = piter->member.run.pt;
355     return piter;
356   }
357 }
358
359 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
360 {
361   ME_DisplayItem *piter = p, *pp;
362   int i, idesp, len;
363   ME_Run *run = &p->member.run;
364
365   idesp = i = find_split_point( wc->context, loc, run );
366   len = run->len;
367   assert(len>0);
368   assert(i<len);
369   if (i) {
370     /* don't split words */
371     i = reverse_find_whitespace( get_text( run, 0 ), i );
372     pp = ME_MaximizeSplit(wc, p, i);
373     if (pp)
374       return pp;
375   }
376   TRACE("Must backtrack to split at: %s\n", debugstr_run( &p->member.run ));
377   if (wc->pLastSplittableRun)
378   {
379     if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
380     {
381       wc->pt = wc->pLastSplittableRun->member.run.pt;
382       return wc->pLastSplittableRun;
383     }
384     else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
385     {
386       /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
387          they serve no other purpose */
388       ME_UpdateRunFlags(wc->context->editor, run);
389       assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
390
391       piter = wc->pLastSplittableRun;
392       run = &piter->member.run;
393       len = run->len;
394       /* don't split words */
395       i = reverse_find_whitespace( get_text( run, 0 ), len );
396       if (i == len)
397         i = reverse_find_non_whitespace( get_text( run, 0 ), len );
398       if (i) {
399         ME_DisplayItem *piter2 = split_run_extents(wc, piter, i);
400         wc->pt = piter2->member.run.pt;
401         return piter2;
402       }
403       /* splittable = must have whitespaces */
404       assert(0 == "Splittable, but no whitespaces");
405     }
406     else
407     {
408       /* restart from the first run beginning with spaces */
409       wc->pt = wc->pLastSplittableRun->member.run.pt;
410       return wc->pLastSplittableRun;
411     }
412   }
413   TRACE("Backtracking failed, trying desperate: %s\n", debugstr_run( &p->member.run ));
414   /* OK, no better idea, so assume we MAY split words if we can split at all*/
415   if (idesp)
416     return split_run_extents(wc, piter, idesp);
417   else
418   if (wc->pRowStart && piter != wc->pRowStart)
419   {
420     /* don't need to break current run, because it's possible to split
421        before this run */
422     wc->bOverflown = TRUE;
423     return piter;
424   }
425   else
426   {
427     /* split point inside first character - no choice but split after that char */
428     if (len != 1) {
429       /* the run is more than 1 char, so we may split */
430       return split_run_extents(wc, piter, 1);
431     }
432     /* the run is one char, can't split it */
433     return piter;
434   }
435 }
436
437 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
438 {
439   ME_DisplayItem *pp;
440   ME_Run *run;
441   int len;
442
443   assert(p->type == diRun);
444   if (!wc->pRowStart)
445     wc->pRowStart = p;
446   run = &p->member.run;
447   run->pt.x = wc->pt.x;
448   run->pt.y = wc->pt.y;
449   ME_WrapSizeRun(wc, p);
450   len = run->len;
451
452   if (wc->bOverflown) /* just skipping final whitespaces */
453   {
454     /* End paragraph run can't overflow to the next line by itself. */
455     if (run->nFlags & MERF_ENDPARA)
456       return p->next;
457
458     if (run->nFlags & MERF_WHITESPACE) {
459       wc->pt.x += run->nWidth;
460       /* skip runs consisting of only whitespaces */
461       return p->next;
462     }
463
464     if (run->nFlags & MERF_STARTWHITE) {
465       /* try to split the run at the first non-white char */
466       int black;
467       black = find_non_whitespace( get_text( run, 0 ), run->len, 0 );
468       if (black) {
469         wc->bOverflown = FALSE;
470         pp = split_run_extents(wc, p, black);
471         calc_run_extent(wc->context, &wc->pPara->member.para,
472                         wc->nRow ? wc->nLeftMargin : wc->nFirstMargin,
473                         &pp->member.run);
474         ME_InsertRowStart(wc, pp);
475         return pp;
476       }
477     }
478     /* black run: the row goes from pRowStart to the previous run */
479     ME_InsertRowStart(wc, p);
480     return p;
481   }
482   /* simply end the current row and move on to next one */
483   if (run->nFlags & MERF_ENDROW)
484   {
485     p = p->next;
486     ME_InsertRowStart(wc, p);
487     return p;
488   }
489
490   /* will current run fit? */
491   if (wc->bWordWrap &&
492       wc->pt.x + run->nWidth - wc->context->pt.x > wc->nAvailWidth)
493   {
494     int loc = wc->context->pt.x + wc->nAvailWidth - wc->pt.x;
495     /* total white run ? */
496     if (run->nFlags & MERF_WHITESPACE) {
497       /* let the overflow logic handle it */
498       wc->bOverflown = TRUE;
499       return p;
500     }
501     /* TAB: we can split before */
502     if (run->nFlags & MERF_TAB) {
503       wc->bOverflown = TRUE;
504       if (wc->pRowStart == p)
505         /* Don't split before the start of the run, or we will get an
506          * endless loop. */
507         return p->next;
508       else
509         return p;
510     }
511     /* graphics: we can split before, if run's width is smaller than row's width */
512     if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
513       wc->bOverflown = TRUE;
514       return p;
515     }
516     /* can we separate out the last spaces ? (to use overflow logic later) */
517     if (run->nFlags & MERF_ENDWHITE)
518     {
519       /* we aren't sure if it's *really* necessary, it's a good start however */
520       int black = reverse_find_non_whitespace( get_text( run, 0 ), len );
521       split_run_extents(wc, p, black);
522       /* handle both parts again */
523       return p;
524     }
525     /* determine the split point by backtracking */
526     pp = ME_SplitByBacktracking(wc, p, loc);
527     if (pp == wc->pRowStart)
528     {
529       if (run->nFlags & MERF_STARTWHITE)
530       {
531           /* We had only spaces so far, so we must be on the first line of the
532            * paragraph (or the first line after MERF_ENDROW forced the line
533            * break within the paragraph), since no other lines of the paragraph
534            * start with spaces. */
535
536           /* The lines will only contain spaces, and the rest of the run will
537            * overflow onto the next line. */
538           wc->bOverflown = TRUE;
539           return p;
540       }
541       /* Couldn't split the first run, possible because we have a large font
542        * with a single character that caused an overflow.
543        */
544       wc->pt.x += run->nWidth;
545       return p->next;
546     }
547     if (p != pp) /* found a suitable split point */
548     {
549       wc->bOverflown = TRUE;
550       return pp;
551     }
552     /* we detected that it's best to split on start of this run */
553     if (wc->bOverflown)
554       return pp;
555     ERR("failure!\n");
556     /* not found anything - writing over margins is the only option left */
557   }
558   if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
559     || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
560   {
561     wc->pLastSplittableRun = p;
562   }
563   wc->pt.x += run->nWidth;
564   return p->next;
565 }
566
567 static int ME_GetParaLineSpace(ME_Context* c, ME_Paragraph* para)
568 {
569   int   sp = 0, ls = 0;
570   if (!(para->pFmt->dwMask & PFM_LINESPACING)) return 0;
571
572   /* FIXME: how to compute simply the line space in ls ??? */
573   /* FIXME: does line spacing include the line itself ??? */
574   switch (para->pFmt->bLineSpacingRule)
575   {
576   case 0:       sp = ls; break;
577   case 1:       sp = (3 * ls) / 2; break;
578   case 2:       sp = 2 * ls; break;
579   case 3:       sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); if (sp < ls) sp = ls; break;
580   case 4:       sp = ME_twips2pointsY(c, para->pFmt->dyLineSpacing); break;
581   case 5:       sp = para->pFmt->dyLineSpacing / 20; break;
582   default: FIXME("Unsupported spacing rule value %d\n", para->pFmt->bLineSpacingRule);
583   }
584   if (c->editor->nZoomNumerator == 0)
585     return sp;
586   else
587     return sp * c->editor->nZoomNumerator / c->editor->nZoomDenominator;
588 }
589
590 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
591   ME_DisplayItem *p;
592
593   tp->member.para.nWidth = 0;
594   /* remove row start items as they will be reinserted by the
595    * paragraph wrapper anyway */
596   tp->member.para.nRows = 0;
597   for (p = tp->next; p != tp->member.para.next_para; p = p->next) {
598     if (p->type == diStartRow) {
599       ME_DisplayItem *pRow = p;
600       p = p->prev;
601       ME_Remove(pRow);
602       ME_DestroyDisplayItem(pRow);
603     }
604   }
605   /* join runs that can be joined */
606   for (p = tp->next; p != tp->member.para.next_para; p = p->next) {
607     assert(p->type != diStartRow); /* should have been deleted above */
608     if (p->type == diRun) {
609       while (p->next->type == diRun && /* FIXME */
610              ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
611         ME_JoinRuns(c->editor, p);
612       }
613     }
614   }
615 }
616
617 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp) {
618   ME_DisplayItem *p;
619   ME_WrapContext wc;
620   int border = 0;
621   int linespace = 0;
622   PARAFORMAT2 *pFmt;
623
624   assert(tp->type == diParagraph);
625   if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
626     return;
627   }
628   ME_PrepareParagraphForWrapping(c, tp);
629   pFmt = tp->member.para.pFmt;
630
631   wc.context = c;
632   wc.pPara = tp;
633 /*   wc.para_style = tp->member.para.style; */
634   wc.style = NULL;
635   if (tp->member.para.nFlags & MEPF_ROWEND) {
636     wc.nFirstMargin = wc.nLeftMargin = wc.nRightMargin = 0;
637   } else {
638     int dxStartIndent = pFmt->dxStartIndent;
639     if (tp->member.para.pCell) {
640       dxStartIndent += ME_GetTableRowEnd(tp)->member.para.pFmt->dxOffset;
641     }
642     wc.nFirstMargin = ME_twips2pointsX(c, dxStartIndent);
643     wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, pFmt->dxOffset);
644     wc.nRightMargin = ME_twips2pointsX(c, pFmt->dxRightIndent);
645   }
646   if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
647       pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
648   {
649     wc.nFirstMargin += ME_twips2pointsX(c, pFmt->dxOffset * 2);
650   }
651   wc.nRow = 0;
652   wc.pt.y = 0;
653   if (pFmt->dwMask & PFM_SPACEBEFORE)
654     wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceBefore);
655   if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
656       pFmt->dwMask & PFM_BORDER)
657   {
658     border = ME_GetParaBorderWidth(c, tp->member.para.pFmt->wBorders);
659     if (pFmt->wBorders & 1) {
660       wc.nFirstMargin += border;
661       wc.nLeftMargin += border;
662     }
663     if (pFmt->wBorders & 2)
664       wc.nRightMargin -= border;
665     if (pFmt->wBorders & 4)
666       wc.pt.y += border;
667   }
668
669   linespace = ME_GetParaLineSpace(c, &tp->member.para);
670
671   ME_BeginRow(&wc);
672   for (p = tp->next; p!=tp->member.para.next_para; ) {
673     assert(p->type != diStartRow);
674     if (p->type == diRun) {
675       p = ME_WrapHandleRun(&wc, p);
676     }
677     else p = p->next;
678     if (wc.nRow && p == wc.pRowStart)
679       wc.pt.y += linespace;
680   }
681   ME_WrapEndParagraph(&wc, p);
682   if (!(pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE) &&
683       (pFmt->dwMask & PFM_BORDER) && (pFmt->wBorders & 8))
684     wc.pt.y += border;
685   if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
686     wc.pt.y += ME_twips2pointsY(c, pFmt->dySpaceAfter);
687
688   tp->member.para.nFlags &= ~MEPF_REWRAP;
689   tp->member.para.nHeight = wc.pt.y;
690   tp->member.para.nRows = wc.nRow;
691 }
692
693 static void ME_MarkRepaintEnd(ME_DisplayItem *para,
694                               ME_DisplayItem **repaint_start,
695                               ME_DisplayItem **repaint_end)
696 {
697     if (!*repaint_start)
698       *repaint_start = para;
699     *repaint_end = para;
700 }
701
702 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor)
703 {
704   ME_DisplayItem *item;
705   ME_Context c;
706   int totalWidth = 0;
707   ME_DisplayItem *repaint_start = NULL, *repaint_end = NULL;
708
709   ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
710   c.pt.x = 0;
711   item = editor->pBuffer->pFirst->next;
712   while(item != editor->pBuffer->pLast) {
713     BOOL bRedraw = FALSE;
714
715     assert(item->type == diParagraph);
716     if ((item->member.para.nFlags & MEPF_REWRAP)
717      || (item->member.para.pt.y != c.pt.y))
718       bRedraw = TRUE;
719     item->member.para.pt = c.pt;
720
721     ME_WrapTextParagraph(&c, item);
722
723     if (bRedraw)
724       ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
725
726     if (item->member.para.nFlags & MEPF_ROWSTART)
727     {
728       ME_DisplayItem *cell = ME_FindItemFwd(item, diCell);
729       ME_DisplayItem *endRowPara;
730       int borderWidth = 0;
731       cell->member.cell.pt = c.pt;
732       /* Offset the text by the largest top border width. */
733       while (cell->member.cell.next_cell) {
734         borderWidth = max(borderWidth, cell->member.cell.border.top.width);
735         cell = cell->member.cell.next_cell;
736       }
737       endRowPara = ME_FindItemFwd(cell, diParagraph);
738       assert(endRowPara->member.para.nFlags & MEPF_ROWEND);
739       if (borderWidth > 0)
740       {
741         borderWidth = max(ME_twips2pointsY(&c, borderWidth), 1);
742         while (cell) {
743           cell->member.cell.yTextOffset = borderWidth;
744           cell = cell->member.cell.prev_cell;
745         }
746         c.pt.y += borderWidth;
747       }
748       if (endRowPara->member.para.pFmt->dxStartIndent > 0)
749       {
750         int dxStartIndent = endRowPara->member.para.pFmt->dxStartIndent;
751         cell = ME_FindItemFwd(item, diCell);
752         cell->member.cell.pt.x += ME_twips2pointsX(&c, dxStartIndent);
753         c.pt.x = cell->member.cell.pt.x;
754       }
755     }
756     else if (item->member.para.nFlags & MEPF_ROWEND)
757     {
758       /* Set all the cells to the height of the largest cell */
759       ME_DisplayItem *startRowPara;
760       int prevHeight, nHeight, bottomBorder = 0;
761       ME_DisplayItem *cell = ME_FindItemBack(item, diCell);
762       item->member.para.nWidth = cell->member.cell.pt.x + cell->member.cell.nWidth;
763       if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWSTART))
764       {
765         /* Last row, the bottom border is added to the height. */
766         cell = cell->member.cell.prev_cell;
767         while (cell)
768         {
769           bottomBorder = max(bottomBorder, cell->member.cell.border.bottom.width);
770           cell = cell->member.cell.prev_cell;
771         }
772         bottomBorder = ME_twips2pointsY(&c, bottomBorder);
773         cell = ME_FindItemBack(item, diCell);
774       }
775       prevHeight = cell->member.cell.nHeight;
776       nHeight = cell->member.cell.prev_cell->member.cell.nHeight + bottomBorder;
777       cell->member.cell.nHeight = nHeight;
778       item->member.para.nHeight = nHeight;
779       cell = cell->member.cell.prev_cell;
780       cell->member.cell.nHeight = nHeight;
781       while (cell->member.cell.prev_cell)
782       {
783         cell = cell->member.cell.prev_cell;
784         cell->member.cell.nHeight = nHeight;
785       }
786       /* Also set the height of the start row paragraph */
787       startRowPara = ME_FindItemBack(cell, diParagraph);
788       startRowPara->member.para.nHeight = nHeight;
789       c.pt.x = startRowPara->member.para.pt.x;
790       c.pt.y = cell->member.cell.pt.y + nHeight;
791       if (prevHeight < nHeight)
792       {
793         /* The height of the cells has grown, so invalidate the bottom of
794          * the cells. */
795         ME_MarkRepaintEnd(item, &repaint_start, &repaint_end);
796         cell = ME_FindItemBack(item, diCell);
797         while (cell) {
798           ME_MarkRepaintEnd(ME_FindItemBack(cell, diParagraph), &repaint_start, &repaint_end);
799           cell = cell->member.cell.prev_cell;
800         }
801       }
802     }
803     else if (item->member.para.pCell &&
804              item->member.para.pCell != item->member.para.next_para->member.para.pCell)
805     {
806       /* The next paragraph is in the next cell in the table row. */
807       ME_Cell *cell = &item->member.para.pCell->member.cell;
808       cell->nHeight = c.pt.y + item->member.para.nHeight - cell->pt.y;
809
810       /* Propagate the largest height to the end so that it can be easily
811        * sent back to all the cells at the end of the row. */
812       if (cell->prev_cell)
813         cell->nHeight = max(cell->nHeight, cell->prev_cell->member.cell.nHeight);
814
815       c.pt.x = cell->pt.x + cell->nWidth;
816       c.pt.y = cell->pt.y;
817       cell->next_cell->member.cell.pt = c.pt;
818       if (!(item->member.para.next_para->member.para.nFlags & MEPF_ROWEND))
819         c.pt.y += cell->yTextOffset;
820     }
821     else
822     {
823       if (item->member.para.pCell) {
824         /* Next paragraph in the same cell. */
825         c.pt.x = item->member.para.pCell->member.cell.pt.x;
826       } else {
827         /* Normal paragraph */
828         c.pt.x = 0;
829       }
830       c.pt.y += item->member.para.nHeight;
831     }
832
833     totalWidth = max(totalWidth, item->member.para.nWidth);
834     item = item->member.para.next_para;
835   }
836   editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
837   editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
838
839   editor->nTotalLength = c.pt.y;
840   editor->nTotalWidth = totalWidth;
841   editor->pBuffer->pLast->member.para.pt.x = 0;
842   editor->pBuffer->pLast->member.para.pt.y = c.pt.y;
843
844   ME_DestroyContext(&c);
845
846   if (repaint_start || editor->nTotalLength < editor->nLastTotalLength)
847     ME_InvalidateParagraphRange(editor, repaint_start, repaint_end);
848   return !!repaint_start;
849 }
850
851 void ME_InvalidateParagraphRange(ME_TextEditor *editor,
852                                  ME_DisplayItem *start_para,
853                                  ME_DisplayItem *last_para)
854 {
855   ME_Context c;
856   RECT rc;
857   int ofs;
858
859   ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
860   rc = c.rcView;
861   ofs = editor->vert_si.nPos;
862
863   if (start_para) {
864     start_para = ME_GetOuterParagraph(start_para);
865     last_para = ME_GetOuterParagraph(last_para);
866     rc.top = c.rcView.top + start_para->member.para.pt.y - ofs;
867   } else {
868     rc.top = c.rcView.top + editor->nTotalLength - ofs;
869   }
870   if (editor->nTotalLength < editor->nLastTotalLength)
871     rc.bottom = c.rcView.top + editor->nLastTotalLength - ofs;
872   else
873     rc.bottom = c.rcView.top + last_para->member.para.pt.y + last_para->member.para.nHeight - ofs;
874   ITextHost_TxInvalidateRect(editor->texthost, &rc, TRUE);
875
876   ME_DestroyContext(&c);
877 }
878
879
880 void
881 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
882 {
883   if (editor->nEventMask & ENM_REQUESTRESIZE)
884   {
885     RECT rc;
886
887     ITextHost_TxGetClientRect(editor->texthost, &rc);
888
889     if (force || rc.bottom != editor->nTotalLength)
890     {
891       REQRESIZE info;
892
893       info.nmhdr.hwndFrom = NULL;
894       info.nmhdr.idFrom = 0;
895       info.nmhdr.code = EN_REQUESTRESIZE;
896       info.rc = rc;
897       info.rc.right = editor->nTotalWidth;
898       info.rc.bottom = editor->nTotalLength;
899
900       editor->nEventMask &= ~ENM_REQUESTRESIZE;
901       ITextHost_TxNotify(editor->texthost, info.nmhdr.code, &info);
902       editor->nEventMask |= ENM_REQUESTRESIZE;
903     }
904   }
905 }