richedit: Removed redundant wrapping code.
[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   wc->pRowStart = NULL;
48   wc->bOverflown = FALSE;
49   wc->pLastSplittableRun = NULL;
50   if (wc->context->editor->bWordWrap)
51     wc->nAvailWidth = wc->context->rcView.right - wc->context->rcView.left -
52         (wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
53   else
54     wc->nAvailWidth = ~0u >> 1;
55   wc->pt.x = 0;
56 }
57
58 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
59 {
60   ME_DisplayItem *p, *row, *para;
61   BOOL bSkippingSpaces = TRUE;
62   int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
63   /* wrap text */
64   para = ME_GetParagraph(wc->pRowStart);
65
66   for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
67   {
68       /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
69       if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
70         if (p->member.run.nAscent>ascent)
71           ascent = p->member.run.nAscent;
72         if (p->member.run.nDescent>descent)
73           descent = p->member.run.nDescent;
74         if (bSkippingSpaces)
75         {
76           /* Exclude space characters from run width.
77            * Other whitespace or delimiters are not treated this way. */
78           SIZE sz;
79           int len = p->member.run.strText->nLen;
80           WCHAR *text = p->member.run.strText->szData + len - 1;
81
82           assert (len);
83           while (len && *(text--) == ' ')
84               len--;
85           if (len)
86           {
87               if (len == p->member.run.strText->nLen)
88               {
89                   width += p->member.run.nWidth;
90               } else {
91                   sz = ME_GetRunSize(wc->context, &para->member.para,
92                                      &p->member.run, len, p->member.run.pt.x);
93                   width += sz.cx;
94               }
95           }
96           bSkippingSpaces = !len;
97         } else if (!(p->member.run.nFlags & MERF_ENDPARA))
98           width += p->member.run.nWidth;
99       }
100   }
101
102   row = ME_MakeRow(ascent+descent, ascent, width);
103   row->member.row.nYPos = wc->pt.y;
104   row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
105   row->member.row.nRMargin = wc->nRightMargin;
106   assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
107   align = para->member.para.pFmt->wAlignment;
108   if (align == PFA_CENTER)
109     shift = (wc->nAvailWidth-width)/2;
110   if (align == PFA_RIGHT)
111     shift = wc->nAvailWidth-width;
112   for (p = wc->pRowStart; p!=pEnd; p = p->next)
113   {
114     if (p->type==diRun) { /* FIXME add more run types */
115       p->member.run.pt.x += row->member.row.nLMargin+shift;
116     }
117   }
118   ME_InsertBefore(wc->pRowStart, row);
119   wc->nRow++;
120   wc->pt.y += ascent+descent;
121   ME_BeginRow(wc);
122 }
123
124 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
125 {
126   if (wc->pRowStart)
127     ME_InsertRowStart(wc, p);
128
129   /*
130   p = p->member.para.prev_para->next;
131   while(p) {
132     if (p->type == diParagraph || p->type == diTextEnd)
133       return;
134     if (p->type == diRun)
135     {
136       ME_Run *run = &p->member.run;
137       TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
138     }
139     p = p->next;
140   }
141   */
142 }
143
144 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
145 {
146   /* FIXME compose style (out of character and paragraph styles) here */
147
148   ME_UpdateRunFlags(wc->context->editor, &p->member.run);
149
150   ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
151                    wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
152 }
153
154 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
155 {
156   ME_DisplayItem *pp, *piter = p;
157   int j;
158   if (!i)
159     return NULL;
160   j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
161   if (j>0) {
162     pp = ME_SplitRun(wc, piter, j);
163     wc->pt.x += piter->member.run.nWidth;
164     return pp;
165   }
166   else
167   {
168     pp = piter;
169     /* omit all spaces before split point */
170     while(piter != wc->pRowStart)
171     {
172       piter = ME_FindItemBack(piter, diRun);
173       if (piter->member.run.nFlags & MERF_WHITESPACE)
174       {
175         pp = piter;
176         continue;
177       }
178       if (piter->member.run.nFlags & MERF_ENDWHITE)
179       {
180         j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
181         pp = ME_SplitRun(wc, piter, i);
182         wc->pt = pp->member.run.pt;
183         return pp;
184       }
185       /* this run is the end of spaces, so the run edge is a good point to split */
186       wc->pt = pp->member.run.pt;
187       wc->bOverflown = TRUE;
188       TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData));
189       return pp;
190     }
191     wc->pt = piter->member.run.pt;
192     return piter;
193   }
194 }
195
196 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
197 {
198   ME_DisplayItem *piter = p, *pp;
199   int i, idesp, len;
200   ME_Run *run = &p->member.run;
201
202   idesp = i = ME_CharFromPoint(wc->context, loc, run);
203   len = ME_StrVLen(run->strText);
204   assert(len>0);
205   assert(i<len);
206   if (i) {
207     /* don't split words */
208     i = ME_ReverseFindWhitespaceV(run->strText, i);
209     pp = ME_MaximizeSplit(wc, p, i);
210     if (pp)
211       return pp;
212   }
213   TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
214   if (wc->pLastSplittableRun)
215   {
216     if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
217     {
218       wc->pt = wc->ptLastSplittableRun;
219       return wc->pLastSplittableRun;
220     }
221     else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
222     {
223       /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
224          they serve no other purpose */
225       ME_UpdateRunFlags(wc->context->editor, run);
226       assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
227
228       piter = wc->pLastSplittableRun;
229       run = &piter->member.run;
230       len = ME_StrVLen(run->strText);
231       /* don't split words */
232       i = ME_ReverseFindWhitespaceV(run->strText, len);
233       if (i == len)
234         i = ME_ReverseFindNonWhitespaceV(run->strText, len);
235       if (i) {
236         ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
237         wc->pt = piter2->member.run.pt;
238         return piter2;
239       }
240       /* splittable = must have whitespaces */
241       assert(0 == "Splittable, but no whitespaces");
242     }
243     else
244     {
245       /* restart from the first run beginning with spaces */
246       wc->pt = wc->ptLastSplittableRun;
247       return wc->pLastSplittableRun;
248     }
249   }
250   TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData));
251   /* OK, no better idea, so assume we MAY split words if we can split at all*/
252   if (idesp)
253     return ME_SplitRun(wc, piter, idesp);
254   else
255   if (wc->pRowStart && piter != wc->pRowStart)
256   {
257     /* don't need to break current run, because it's possible to split
258        before this run */
259     wc->bOverflown = TRUE;
260     return piter;
261   }
262   else
263   {
264     /* split point inside first character - no choice but split after that char */
265     int chars = 1;
266     int pos2 = ME_StrRelPos(run->strText, 0, &chars);
267     if (pos2 != len) {
268       /* the run is more than 1 char, so we may split */
269       return ME_SplitRun(wc, piter, pos2);
270     }
271     /* the run is one char, can't split it */
272     return piter;
273   }
274 }
275
276 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
277 {
278   ME_DisplayItem *pp;
279   ME_Run *run;
280   int len;
281
282   assert(p->type == diRun);
283   if (!wc->pRowStart)
284     wc->pRowStart = p;
285   run = &p->member.run;
286   run->pt.x = wc->pt.x;
287   run->pt.y = wc->pt.y;
288   ME_WrapSizeRun(wc, p);
289   len = ME_StrVLen(run->strText);
290
291   if (wc->bOverflown) /* just skipping final whitespaces */
292   {
293     if (run->nFlags & (MERF_WHITESPACE|MERF_TAB)) {
294       p->member.run.nFlags |= MERF_SKIPPED;
295       /* wc->pt.x += run->nWidth; */
296       /* skip runs consisting of only whitespaces */
297       return p->next;
298     }
299
300     if (run->nFlags & MERF_STARTWHITE) {
301       /* try to split the run at the first non-white char */
302       int black;
303       black = ME_FindNonWhitespaceV(run->strText, 0);
304       if (black) {
305         wc->bOverflown = FALSE;
306         pp = ME_SplitRun(wc, p, black);
307         p->member.run.nFlags |= MERF_SKIPPED;
308         ME_InsertRowStart(wc, pp);
309         return pp;
310       }
311     }
312     /* black run: the row goes from pRowStart to the previous run */
313     ME_InsertRowStart(wc, p);
314     return p;
315   }
316   /* simply end the current row and move on to next one */
317   if (run->nFlags & MERF_ENDROW)
318   {
319     p = p->next;
320     ME_InsertRowStart(wc, p);
321     return p;
322   }
323
324   /* will current run fit? */
325   if (wc->pt.x + run->nWidth > wc->nAvailWidth)
326   {
327     int loc = wc->nAvailWidth - wc->pt.x;
328     /* total white run ? */
329     if (run->nFlags & MERF_WHITESPACE) {
330       /* let the overflow logic handle it */
331       wc->bOverflown = TRUE;
332       return p;
333     }
334     /* TAB: we can split before */
335     if (run->nFlags & MERF_TAB) {
336       wc->bOverflown = TRUE;
337       return p;
338     }
339     /* graphics: we can split before, if run's width is smaller than row's width */
340     if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
341       wc->bOverflown = TRUE;
342       return p;
343     }
344     /* can we separate out the last spaces ? (to use overflow logic later) */
345     if (run->nFlags & MERF_ENDWHITE)
346     {
347       /* we aren't sure if it's *really* necessary, it's a good start however */
348       int black = ME_ReverseFindNonWhitespaceV(run->strText, len);
349       ME_SplitRun(wc, p, black);
350       /* handle both parts again */
351       return p;
352     }
353     /* determine the split point by backtracking */
354     pp = ME_SplitByBacktracking(wc, p, loc);
355     if (pp == wc->pRowStart)
356     {
357       /* we had only spaces so far, entire content can be omitted */
358       wc->pt.x = 0;
359       return p->next;
360     }
361     if (p != pp) /* found a suitable split point */
362     {
363       wc->bOverflown = TRUE;
364       return pp;
365     }
366     /* we detected that it's best to split on start of this run */
367     if (wc->bOverflown)
368       return pp;
369     ERR("failure!\n");
370     /* not found anything - writing over margins is the only option left */
371   }
372   if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
373     || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
374   {
375     wc->pLastSplittableRun = p;
376     wc->ptLastSplittableRun = wc->pt;
377   }
378   wc->pt.x += run->nWidth;
379   return p->next;
380 }
381
382 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
383
384 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp, DWORD beginofs) {
385   ME_DisplayItem *p;
386   ME_WrapContext wc;
387   int border = 0;
388   int linespace = 0;
389
390   assert(tp->type == diParagraph);
391   if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
392     return;
393   }
394   ME_PrepareParagraphForWrapping(c, tp);
395
396   wc.context = c;
397 /*   wc.para_style = tp->member.para.style; */
398   wc.style = NULL;
399   wc.nFirstMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxStartIndent) + beginofs;
400   wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, tp->member.para.pFmt->dxOffset);
401   wc.nRightMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxRightIndent);
402   wc.nRow = 0;
403   wc.pt.x = 0;
404   wc.pt.y = 0;
405   if (tp->member.para.pFmt->dwMask & PFM_SPACEBEFORE)
406     wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceBefore);
407   if (tp->member.para.pFmt->dwMask & PFM_BORDER)
408   {
409     border = ME_GetParaBorderWidth(c->editor, tp->member.para.pFmt->wBorders);
410     if (tp->member.para.pFmt->wBorders & 1) {
411       wc.nFirstMargin += border;
412       wc.nLeftMargin += border;
413     }
414     if (tp->member.para.pFmt->wBorders & 2)
415       wc.nRightMargin -= border;
416     if (tp->member.para.pFmt->wBorders & 4)
417       wc.pt.y += border;
418   }
419
420   linespace = ME_GetParaLineSpace(c, &tp->member.para);
421
422   ME_BeginRow(&wc);
423   for (p = tp->next; p!=tp->member.para.next_para; ) {
424     assert(p->type != diStartRow);
425     if (p->type == diRun) {
426       p = ME_WrapHandleRun(&wc, p);
427     }
428     else p = p->next;
429     if (wc.nRow && p == wc.pRowStart)
430       wc.pt.y += linespace;
431   }
432   ME_WrapEndParagraph(&wc, p);
433   if ((tp->member.para.pFmt->dwMask & PFM_BORDER) && (tp->member.para.pFmt->wBorders & 8))
434     wc.pt.y += border;
435   if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
436     wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceAfter);
437
438   tp->member.para.nFlags &= ~MEPF_REWRAP;
439   tp->member.para.nHeight = wc.pt.y;
440   tp->member.para.nRows = wc.nRow;
441 }
442
443
444 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
445   ME_DisplayItem *p, *pRow;
446
447   /* remove all items that will be reinserted by paragraph wrapper anyway */
448   tp->member.para.nRows = 0;
449   for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
450     switch(p->type) {
451       case diStartRow:
452         pRow = p;
453         p = p->prev;
454         ME_Remove(pRow);
455         ME_DestroyDisplayItem(pRow);
456         break;
457       default:
458         break;
459     }
460   }
461   /* join runs that can be joined, set up flags */
462   for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
463     int changed = 0;
464     switch(p->type) {
465       case diStartRow: assert(0); break; /* should have deleted it */
466       case diRun:
467         while (p->next->type == diRun) { /* FIXME */
468           if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
469             ME_JoinRuns(c->editor, p);
470             changed = 1;
471           }
472           else
473             break;
474         }
475         p->member.run.nFlags &= ~MERF_CALCBYWRAP;
476         break;
477       default:
478         break;
479     }
480   }
481 }
482
483 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) {
484   ME_DisplayItem *item;
485   ME_Context c;
486   BOOL bModified = FALSE;
487   int yStart = -1;
488   int yLastPos = 0;
489
490   ME_InitContext(&c, editor, GetDC(editor->hWnd));
491   editor->nHeight = 0;
492   item = editor->pBuffer->pFirst->next;
493   while(item != editor->pBuffer->pLast) {
494     BOOL bRedraw = FALSE;
495
496     assert(item->type == diParagraph);
497     editor->nHeight = max(editor->nHeight, item->member.para.nYPos);
498     if ((item->member.para.nFlags & MEPF_REWRAP)
499      || (item->member.para.nYPos != c.pt.y))
500       bRedraw = TRUE;
501     item->member.para.nYPos = c.pt.y;
502
503     ME_WrapTextParagraph(&c, item, editor->selofs);
504
505     if (bRedraw)
506     {
507       item->member.para.nFlags |= MEPF_REPAINT;
508       if (yStart == -1)
509         yStart = c.pt.y;
510     }
511
512     bModified = bModified | bRedraw;
513
514     yLastPos = c.pt.y;
515     c.pt.y += item->member.para.nHeight;
516     item = item->member.para.next_para;
517   }
518   editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
519   editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
520   
521   editor->nTotalLength = c.pt.y;
522   editor->pBuffer->pLast->member.para.nYPos = yLastPos;
523
524   ME_DestroyContext(&c, editor->hWnd);
525
526   /* Each paragraph may contain multiple rows, which should be scrollable, even
527      if the containing paragraph has nYPos == 0 */
528   item = editor->pBuffer->pFirst;
529   while ((item = ME_FindItemFwd(item, diStartRow)) != NULL) {
530     assert(item->type == diStartRow);
531     editor->nHeight = max(editor->nHeight, item->member.row.nYPos);
532   }
533
534   if (bModified || editor->nTotalLength < editor->nLastTotalLength)
535     ME_InvalidateMarkedParagraphs(editor);
536   return bModified;
537 }
538
539 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) {
540   ME_Context c;
541
542   ME_InitContext(&c, editor, GetDC(editor->hWnd));
543   if (editor->bRedraw)
544   {
545     RECT rc = c.rcView;
546     int ofs = ME_GetYScrollPos(editor); 
547      
548     ME_DisplayItem *item = editor->pBuffer->pFirst;
549     while(item != editor->pBuffer->pLast) {
550       if (item->member.para.nFlags & MEPF_REPAINT) { 
551         rc.top = item->member.para.nYPos - ofs;
552         rc.bottom = item->member.para.nYPos + item->member.para.nHeight - ofs;
553         InvalidateRect(editor->hWnd, &rc, TRUE);
554       }
555       item = item->member.para.next_para;
556     }
557     if (editor->nTotalLength < editor->nLastTotalLength)
558     {
559       rc.top = editor->nTotalLength - ofs;
560       rc.bottom = editor->nLastTotalLength - ofs;
561       InvalidateRect(editor->hWnd, &rc, TRUE);
562     }
563   }
564   ME_DestroyContext(&c, editor->hWnd);
565 }
566
567
568 void
569 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
570 {
571   if (editor->nEventMask & ENM_REQUESTRESIZE)
572   {
573     RECT rc;
574
575     GetClientRect(editor->hWnd, &rc);
576
577     if (force || rc.bottom != editor->nTotalLength)
578     {
579       REQRESIZE info;
580
581       info.nmhdr.hwndFrom = editor->hWnd;
582       info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
583       info.nmhdr.code = EN_REQUESTRESIZE;
584       info.rc = rc;
585       info.rc.bottom = editor->nTotalLength;
586
587       editor->nEventMask &= ~ENM_REQUESTRESIZE;
588       SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,
589                    info.nmhdr.idFrom, (LPARAM)&info);
590       editor->nEventMask |= ENM_REQUESTRESIZE;
591     }
592   }
593 }