msi: Fix a typo.
[wine.git] / dlls / riched20 / wrap.c
bloba657b0373465f8927aa8d0b8fa94e25e6013110d
1 /*
2 * RichEdit - Paragraph wrapping. Don't try to understand it. You've been
3 * warned !
5 * Copyright 2004 by Krzysztof Foltman
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.
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.
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
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
28 * Unsolved problems:
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
35 static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
37 ME_DisplayItem *item = ME_MakeDI(diStartRow);
39 item->member.row.nHeight = height;
40 item->member.row.nBaseline = baseline;
41 item->member.row.nWidth = width;
42 return item;
45 static void ME_BeginRow(ME_WrapContext *wc)
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;
58 static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
60 ME_DisplayItem *p, *row, *para;
61 int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
62 /* wrap text */
63 para = ME_GetParagraph(wc->pRowStart);
64 for (p = wc->pRowStart; p!=pEnd; p = p->next)
66 /* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
67 if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
68 if (p->member.run.nAscent>ascent)
69 ascent = p->member.run.nAscent;
70 if (p->member.run.nDescent>descent)
71 descent = p->member.run.nDescent;
72 if (!(p->member.run.nFlags & (MERF_ENDPARA|MERF_SKIPPED)))
73 width += p->member.run.nWidth;
76 row = ME_MakeRow(ascent+descent, ascent, width);
77 row->member.row.nYPos = wc->pt.y;
78 row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
79 row->member.row.nRMargin = wc->nRightMargin;
80 assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
81 align = para->member.para.pFmt->wAlignment;
82 if (align == PFA_CENTER)
83 shift = (wc->nAvailWidth-width)/2;
84 if (align == PFA_RIGHT)
85 shift = wc->nAvailWidth-width;
86 for (p = wc->pRowStart; p!=pEnd; p = p->next)
88 if (p->type==diRun) { /* FIXME add more run types */
89 p->member.run.pt.x += row->member.row.nLMargin+shift;
92 ME_InsertBefore(wc->pRowStart, row);
93 wc->nRow++;
94 wc->pt.y += ascent+descent;
95 ME_BeginRow(wc);
98 static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
100 if (wc->pRowStart)
101 ME_InsertRowStart(wc, p->next);
104 p = p->member.para.prev_para->next;
105 while(p) {
106 if (p->type == diParagraph || p->type == diTextEnd)
107 return;
108 if (p->type == diRun)
110 ME_Run *run = &p->member.run;
111 TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
113 p = p->next;
118 static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
120 /* FIXME compose style (out of character and paragraph styles) here */
122 ME_UpdateRunFlags(wc->context->editor, &p->member.run);
124 ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
125 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
128 static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
130 ME_DisplayItem *pp, *piter = p;
131 int j;
132 if (!i)
133 return NULL;
134 j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
135 if (j>0) {
136 pp = ME_SplitRun(wc, piter, j);
137 wc->pt.x += piter->member.run.nWidth;
138 return pp;
140 else
142 pp = piter;
143 /* omit all spaces before split point */
144 while(piter != wc->pRowStart)
146 piter = ME_FindItemBack(piter, diRun);
147 if (piter->member.run.nFlags & MERF_WHITESPACE)
149 pp = piter;
150 continue;
152 if (piter->member.run.nFlags & MERF_ENDWHITE)
154 j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
155 pp = ME_SplitRun(wc, piter, i);
156 wc->pt = pp->member.run.pt;
157 return pp;
159 /* this run is the end of spaces, so the run edge is a good point to split */
160 wc->pt = pp->member.run.pt;
161 wc->bOverflown = TRUE;
162 TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData));
163 return pp;
165 wc->pt = piter->member.run.pt;
166 return piter;
170 static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
172 ME_DisplayItem *piter = p, *pp;
173 int i, idesp, len;
174 ME_Run *run = &p->member.run;
176 idesp = i = ME_CharFromPoint(wc->context, loc, run);
177 len = ME_StrVLen(run->strText);
178 assert(len>0);
179 assert(i<len);
180 if (i) {
181 /* don't split words */
182 i = ME_ReverseFindWhitespaceV(run->strText, i);
183 pp = ME_MaximizeSplit(wc, p, i);
184 if (pp)
185 return pp;
187 TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
188 if (wc->pLastSplittableRun)
190 if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
192 wc->pt = wc->ptLastSplittableRun;
193 return wc->pLastSplittableRun;
195 else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
197 /* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
198 they serve no other purpose */
199 ME_UpdateRunFlags(wc->context->editor, run);
200 assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
202 piter = wc->pLastSplittableRun;
203 run = &piter->member.run;
204 len = ME_StrVLen(run->strText);
205 /* don't split words */
206 i = ME_ReverseFindWhitespaceV(run->strText, len);
207 if (i == len)
208 i = ME_ReverseFindNonWhitespaceV(run->strText, len);
209 if (i) {
210 ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
211 wc->pt = piter2->member.run.pt;
212 return piter2;
214 /* splittable = must have whitespaces */
215 assert(0 == "Splittable, but no whitespaces");
217 else
219 /* restart from the first run beginning with spaces */
220 wc->pt = wc->ptLastSplittableRun;
221 return wc->pLastSplittableRun;
224 TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData));
225 /* OK, no better idea, so assume we MAY split words if we can split at all*/
226 if (idesp)
227 return ME_SplitRun(wc, piter, idesp);
228 else
229 if (wc->pRowStart && piter != wc->pRowStart)
231 /* don't need to break current run, because it's possible to split
232 before this run */
233 wc->bOverflown = TRUE;
234 return piter;
236 else
238 /* split point inside first character - no choice but split after that char */
239 int chars = 1;
240 int pos2 = ME_StrRelPos(run->strText, 0, &chars);
241 if (pos2 != len) {
242 /* the run is more than 1 char, so we may split */
243 return ME_SplitRun(wc, piter, pos2);
245 /* the run is one char, can't split it */
246 return piter;
250 static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
252 ME_DisplayItem *pp;
253 ME_Run *run;
254 int len;
256 assert(p->type == diRun);
257 if (!wc->pRowStart)
258 wc->pRowStart = p;
259 run = &p->member.run;
260 run->pt.x = wc->pt.x;
261 run->pt.y = wc->pt.y;
262 ME_WrapSizeRun(wc, p);
263 len = ME_StrVLen(run->strText);
265 if (wc->bOverflown) /* just skipping final whitespaces */
267 if (run->nFlags & (MERF_WHITESPACE|MERF_TAB)) {
268 p->member.run.nFlags |= MERF_SKIPPED;
269 /* wc->pt.x += run->nWidth; */
270 /* skip runs consisting of only whitespaces */
271 return p->next;
274 if (run->nFlags & MERF_STARTWHITE) {
275 /* try to split the run at the first non-white char */
276 int black;
277 black = ME_FindNonWhitespaceV(run->strText, 0);
278 if (black) {
279 wc->bOverflown = FALSE;
280 pp = ME_SplitRun(wc, p, black);
281 p->member.run.nFlags |= MERF_SKIPPED;
282 ME_InsertRowStart(wc, pp);
283 return pp;
286 /* black run: the row goes from pRowStart to the previous run */
287 ME_InsertRowStart(wc, p);
288 return p;
290 /* simply end the current row and move on to next one */
291 if (run->nFlags & MERF_ENDROW)
293 p = p->next;
294 ME_InsertRowStart(wc, p);
295 return p;
297 /* we're not at the end of the row */
298 if (run->nFlags & MERF_TAB) {
299 /* force recomputation of tabs' size as it depends on position */
300 ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
301 wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
304 /* will current run fit? */
305 if (wc->pt.x + run->nWidth > wc->nAvailWidth)
307 int loc = wc->nAvailWidth - wc->pt.x;
308 /* total white run ? */
309 if (run->nFlags & MERF_WHITESPACE) {
310 /* let the overflow logic handle it */
311 wc->bOverflown = TRUE;
312 return p;
314 /* TAB: we can split before */
315 if (run->nFlags & MERF_TAB) {
316 wc->bOverflown = TRUE;
317 return p;
319 /* graphics: we can split before, if run's width is smaller than row's width */
320 if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
321 wc->bOverflown = TRUE;
322 return p;
324 /* can we separate out the last spaces ? (to use overflow logic later) */
325 if (run->nFlags & MERF_ENDWHITE)
327 /* we aren't sure if it's *really* necessary, it's a good start however */
328 int black = ME_ReverseFindNonWhitespaceV(run->strText, len);
329 ME_SplitRun(wc, p, black);
330 /* handle both parts again */
331 return p;
333 /* determine the split point by backtracking */
334 pp = ME_SplitByBacktracking(wc, p, loc);
335 if (pp == wc->pRowStart)
337 /* we had only spaces so far, entire content can be omitted */
338 wc->pt.x = 0;
339 return p->next;
341 if (p != pp) /* found a suitable split point */
343 wc->bOverflown = TRUE;
344 return pp;
346 /* we detected that it's best to split on start of this run */
347 if (wc->bOverflown)
348 return pp;
349 ERR("failure!\n");
350 /* not found anything - writing over margins is the only option left */
352 if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
353 || ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
355 wc->pLastSplittableRun = p;
356 wc->ptLastSplittableRun = wc->pt;
358 wc->pt.x += run->nWidth;
359 return p->next;
362 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
364 static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp, DWORD beginofs) {
365 ME_DisplayItem *p;
366 ME_WrapContext wc;
367 int border = 0;
368 int linespace = 0;
370 assert(tp->type == diParagraph);
371 if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
372 return;
374 ME_PrepareParagraphForWrapping(c, tp);
376 wc.context = c;
377 /* wc.para_style = tp->member.para.style; */
378 wc.style = NULL;
379 wc.nFirstMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxStartIndent) + beginofs;
380 wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, tp->member.para.pFmt->dxOffset);
381 wc.nRightMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxRightIndent);
382 wc.nRow = 0;
383 wc.pt.x = 0;
384 wc.pt.y = 0;
385 if (tp->member.para.pFmt->dwMask & PFM_SPACEBEFORE)
386 wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceBefore);
387 if (tp->member.para.pFmt->dwMask & PFM_BORDER)
389 border = ME_GetParaBorderWidth(c->editor, tp->member.para.pFmt->wBorders);
390 if (tp->member.para.pFmt->wBorders & 1) {
391 wc.nFirstMargin += border;
392 wc.nLeftMargin += border;
394 if (tp->member.para.pFmt->wBorders & 2)
395 wc.nRightMargin -= border;
396 if (tp->member.para.pFmt->wBorders & 4)
397 wc.pt.y += border;
400 if (c->editor->bWordWrap)
401 wc.nAvailWidth = c->rcView.right - c->rcView.left - wc.nFirstMargin - wc.nRightMargin;
402 else
403 wc.nAvailWidth = ~0u >> 1;
404 wc.pRowStart = NULL;
406 linespace = ME_GetParaLineSpace(c, &tp->member.para);
408 ME_BeginRow(&wc);
409 for (p = tp->next; p!=tp->member.para.next_para; ) {
410 assert(p->type != diStartRow);
411 if (p->type == diRun) {
412 p = ME_WrapHandleRun(&wc, p);
414 else p = p->next;
415 if (wc.nRow && p == wc.pRowStart)
416 wc.pt.y += linespace;
418 ME_WrapEndParagraph(&wc, p);
419 if ((tp->member.para.pFmt->dwMask & PFM_BORDER) && (tp->member.para.pFmt->wBorders & 8))
420 wc.pt.y += border;
421 if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
422 wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceAfter);
424 tp->member.para.nFlags &= ~MEPF_REWRAP;
425 tp->member.para.nHeight = wc.pt.y;
426 tp->member.para.nRows = wc.nRow;
430 static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
431 ME_DisplayItem *p, *pRow;
433 /* remove all items that will be reinserted by paragraph wrapper anyway */
434 tp->member.para.nRows = 0;
435 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
436 switch(p->type) {
437 case diStartRow:
438 pRow = p;
439 p = p->prev;
440 ME_Remove(pRow);
441 ME_DestroyDisplayItem(pRow);
442 break;
443 default:
444 break;
447 /* join runs that can be joined, set up flags */
448 for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
449 int changed = 0;
450 switch(p->type) {
451 case diStartRow: assert(0); break; /* should have deleted it */
452 case diRun:
453 while (p->next->type == diRun) { /* FIXME */
454 if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
455 ME_JoinRuns(c->editor, p);
456 changed = 1;
458 else
459 break;
461 p->member.run.nFlags &= ~MERF_CALCBYWRAP;
462 break;
463 default:
464 break;
469 BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) {
470 ME_DisplayItem *item;
471 ME_Context c;
472 BOOL bModified = FALSE;
473 int yStart = -1, yEnd = -1;
475 ME_InitContext(&c, editor, GetDC(editor->hWnd));
476 c.pt.x = 0;
477 c.pt.y = 0;
478 editor->nHeight = 0;
479 item = editor->pBuffer->pFirst->next;
480 while(item != editor->pBuffer->pLast) {
481 BOOL bRedraw = FALSE;
483 assert(item->type == diParagraph);
484 editor->nHeight = max(editor->nHeight, item->member.para.nYPos);
485 if ((item->member.para.nFlags & MEPF_REWRAP)
486 || (item->member.para.nYPos != c.pt.y))
487 bRedraw = TRUE;
488 item->member.para.nYPos = c.pt.y;
490 ME_WrapTextParagraph(&c, item, editor->selofs);
492 if (bRedraw)
494 item->member.para.nFlags |= MEPF_REPAINT;
495 if (yStart == -1)
496 yStart = c.pt.y;
499 bModified = bModified | bRedraw;
501 c.pt.y += item->member.para.nHeight;
502 if (bRedraw)
503 yEnd = c.pt.y;
504 item = item->member.para.next_para;
506 editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
507 editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
509 editor->nTotalLength = c.pt.y;
511 ME_DestroyContext(&c, editor->hWnd);
513 if (bModified || editor->nTotalLength < editor->nLastTotalLength)
514 ME_InvalidateMarkedParagraphs(editor);
515 return bModified;
518 void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) {
519 ME_Context c;
521 ME_InitContext(&c, editor, GetDC(editor->hWnd));
522 if (editor->bRedraw)
524 RECT rc = c.rcView;
525 int ofs = ME_GetYScrollPos(editor);
527 ME_DisplayItem *item = editor->pBuffer->pFirst;
528 while(item != editor->pBuffer->pLast) {
529 if (item->member.para.nFlags & MEPF_REPAINT) {
530 rc.top = item->member.para.nYPos - ofs;
531 rc.bottom = item->member.para.nYPos + item->member.para.nHeight - ofs;
532 InvalidateRect(editor->hWnd, &rc, TRUE);
534 item = item->member.para.next_para;
536 if (editor->nTotalLength < editor->nLastTotalLength)
538 rc.top = editor->nTotalLength - ofs;
539 rc.bottom = editor->nLastTotalLength - ofs;
540 InvalidateRect(editor->hWnd, &rc, TRUE);
543 ME_DestroyContext(&c, editor->hWnd);
547 void
548 ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
550 if (editor->nEventMask & ENM_REQUESTRESIZE)
552 RECT rc;
554 GetClientRect(editor->hWnd, &rc);
556 if (force || rc.bottom != editor->nTotalLength)
558 REQRESIZE info;
560 info.nmhdr.hwndFrom = editor->hWnd;
561 info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
562 info.nmhdr.code = EN_REQUESTRESIZE;
563 info.rc = rc;
564 info.rc.bottom = editor->nTotalLength;
566 editor->nEventMask &= ~ENM_REQUESTRESIZE;
567 SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,
568 info.nmhdr.idFrom, (LPARAM)&info);
569 editor->nEventMask |= ENM_REQUESTRESIZE;