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