2005-03-05 12:19:14 +01:00
|
|
|
/*
|
|
|
|
* RichEdit - Paragraph wrapping. Don't try to understand it. You've been
|
|
|
|
* warned !
|
|
|
|
*
|
|
|
|
* Copyright 2004 by Krzysztof Foltman
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2005-03-05 12:19:14 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "editor.h"
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(richedit);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unsolved problems:
|
2005-04-16 12:48:35 +02:00
|
|
|
*
|
2005-03-05 12:19:14 +01:00
|
|
|
* - center and right align in WordPad omits all spaces at the start, we don't
|
|
|
|
* - objects/images are not handled yet
|
2005-04-16 12:48:35 +02:00
|
|
|
* - no tabs
|
|
|
|
*/
|
|
|
|
|
2007-12-14 07:45:28 +01:00
|
|
|
static ME_DisplayItem *ME_MakeRow(int height, int baseline, int width)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
ME_DisplayItem *item = ME_MakeDI(diStartRow);
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
item->member.row.nHeight = height;
|
|
|
|
item->member.row.nBaseline = baseline;
|
|
|
|
item->member.row.nWidth = width;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2005-07-06 17:53:04 +02:00
|
|
|
static void ME_BeginRow(ME_WrapContext *wc)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
wc->pRowStart = NULL;
|
|
|
|
wc->bOverflown = FALSE;
|
|
|
|
wc->pLastSplittableRun = NULL;
|
2008-03-16 21:48:05 +01:00
|
|
|
if (wc->context->editor->bWordWrap)
|
|
|
|
wc->nAvailWidth = wc->context->rcView.right - wc->context->rcView.left -
|
|
|
|
(wc->nRow ? wc->nLeftMargin : wc->nFirstMargin) - wc->nRightMargin;
|
|
|
|
else
|
|
|
|
wc->nAvailWidth = ~0u >> 1;
|
2005-03-05 12:19:14 +01:00
|
|
|
wc->pt.x = 0;
|
2005-04-16 12:48:35 +02:00
|
|
|
}
|
2005-03-05 12:19:14 +01:00
|
|
|
|
2007-12-14 07:45:28 +01:00
|
|
|
static void ME_InsertRowStart(ME_WrapContext *wc, const ME_DisplayItem *pEnd)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
ME_DisplayItem *p, *row, *para;
|
2008-06-23 23:09:53 +02:00
|
|
|
BOOL bSkippingSpaces = TRUE;
|
2005-03-05 12:19:14 +01:00
|
|
|
int ascent = 0, descent = 0, width=0, shift = 0, align = 0;
|
|
|
|
/* wrap text */
|
|
|
|
para = ME_GetParagraph(wc->pRowStart);
|
2008-06-23 23:09:53 +02:00
|
|
|
|
|
|
|
for (p = pEnd->prev; p!=wc->pRowStart->prev; p = p->prev)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
2008-06-23 23:09:53 +02:00
|
|
|
/* ENDPARA run shouldn't affect row height, except if it's the only run in the paragraph */
|
|
|
|
if (p->type==diRun && ((p==wc->pRowStart) || !(p->member.run.nFlags & MERF_ENDPARA))) { /* FIXME add more run types */
|
|
|
|
if (p->member.run.nAscent>ascent)
|
|
|
|
ascent = p->member.run.nAscent;
|
|
|
|
if (p->member.run.nDescent>descent)
|
|
|
|
descent = p->member.run.nDescent;
|
|
|
|
if (bSkippingSpaces)
|
|
|
|
{
|
|
|
|
/* Exclude space characters from run width.
|
|
|
|
* Other whitespace or delimiters are not treated this way. */
|
|
|
|
SIZE sz;
|
|
|
|
int len = p->member.run.strText->nLen;
|
|
|
|
WCHAR *text = p->member.run.strText->szData + len - 1;
|
|
|
|
|
|
|
|
assert (len);
|
|
|
|
while (len && *(text--) == ' ')
|
|
|
|
len--;
|
|
|
|
if (len)
|
|
|
|
{
|
|
|
|
if (len == p->member.run.strText->nLen)
|
|
|
|
{
|
|
|
|
width += p->member.run.nWidth;
|
|
|
|
} else {
|
|
|
|
sz = ME_GetRunSize(wc->context, ¶->member.para,
|
|
|
|
&p->member.run, len, p->member.run.pt.x);
|
|
|
|
width += sz.cx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bSkippingSpaces = !len;
|
|
|
|
} else if (!(p->member.run.nFlags & MERF_ENDPARA))
|
|
|
|
width += p->member.run.nWidth;
|
|
|
|
}
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
2008-06-23 23:09:53 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
row = ME_MakeRow(ascent+descent, ascent, width);
|
|
|
|
row->member.row.nYPos = wc->pt.y;
|
|
|
|
row->member.row.nLMargin = (!wc->nRow ? wc->nFirstMargin : wc->nLeftMargin);
|
|
|
|
row->member.row.nRMargin = wc->nRightMargin;
|
|
|
|
assert(para->member.para.pFmt->dwMask & PFM_ALIGNMENT);
|
|
|
|
align = para->member.para.pFmt->wAlignment;
|
|
|
|
if (align == PFA_CENTER)
|
|
|
|
shift = (wc->nAvailWidth-width)/2;
|
|
|
|
if (align == PFA_RIGHT)
|
|
|
|
shift = wc->nAvailWidth-width;
|
|
|
|
for (p = wc->pRowStart; p!=pEnd; p = p->next)
|
|
|
|
{
|
|
|
|
if (p->type==diRun) { /* FIXME add more run types */
|
|
|
|
p->member.run.pt.x += row->member.row.nLMargin+shift;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ME_InsertBefore(wc->pRowStart, row);
|
|
|
|
wc->nRow++;
|
|
|
|
wc->pt.y += ascent+descent;
|
|
|
|
ME_BeginRow(wc);
|
|
|
|
}
|
|
|
|
|
2005-07-06 17:53:04 +02:00
|
|
|
static void ME_WrapEndParagraph(ME_WrapContext *wc, ME_DisplayItem *p)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
if (wc->pRowStart)
|
2008-06-23 23:09:53 +02:00
|
|
|
ME_InsertRowStart(wc, p);
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
/*
|
|
|
|
p = p->member.para.prev_para->next;
|
|
|
|
while(p) {
|
|
|
|
if (p->type == diParagraph || p->type == diTextEnd)
|
|
|
|
return;
|
|
|
|
if (p->type == diRun)
|
|
|
|
{
|
|
|
|
ME_Run *run = &p->member.run;
|
|
|
|
TRACE("%s - (%d, %d)\n", debugstr_w(run->strText->szData), run->pt.x, run->pt.y);
|
|
|
|
}
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
*/
|
2005-04-16 12:48:35 +02:00
|
|
|
}
|
2005-03-05 12:19:14 +01:00
|
|
|
|
2005-07-06 17:53:04 +02:00
|
|
|
static void ME_WrapSizeRun(ME_WrapContext *wc, ME_DisplayItem *p)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
/* FIXME compose style (out of character and paragraph styles) here */
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
ME_UpdateRunFlags(wc->context->editor, &p->member.run);
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2008-03-16 21:47:08 +01:00
|
|
|
ME_CalcRunExtent(wc->context, &ME_GetParagraph(p)->member.para,
|
|
|
|
wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, &p->member.run);
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
|
2005-07-06 17:53:04 +02:00
|
|
|
static ME_DisplayItem *ME_MaximizeSplit(ME_WrapContext *wc, ME_DisplayItem *p, int i)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
ME_DisplayItem *pp, *piter = p;
|
|
|
|
int j;
|
|
|
|
if (!i)
|
|
|
|
return NULL;
|
|
|
|
j = ME_ReverseFindNonWhitespaceV(p->member.run.strText, i);
|
|
|
|
if (j>0) {
|
2008-03-16 21:47:08 +01:00
|
|
|
pp = ME_SplitRun(wc, piter, j);
|
2005-03-05 12:19:14 +01:00
|
|
|
wc->pt.x += piter->member.run.nWidth;
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pp = piter;
|
|
|
|
/* omit all spaces before split point */
|
|
|
|
while(piter != wc->pRowStart)
|
|
|
|
{
|
|
|
|
piter = ME_FindItemBack(piter, diRun);
|
2005-04-16 12:48:35 +02:00
|
|
|
if (piter->member.run.nFlags & MERF_WHITESPACE)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
pp = piter;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (piter->member.run.nFlags & MERF_ENDWHITE)
|
|
|
|
{
|
|
|
|
j = ME_ReverseFindNonWhitespaceV(piter->member.run.strText, i);
|
2008-03-16 21:47:08 +01:00
|
|
|
pp = ME_SplitRun(wc, piter, i);
|
2005-03-05 12:19:14 +01:00
|
|
|
wc->pt = pp->member.run.pt;
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
/* this run is the end of spaces, so the run edge is a good point to split */
|
|
|
|
wc->pt = pp->member.run.pt;
|
|
|
|
wc->bOverflown = TRUE;
|
|
|
|
TRACE("Split point is: %s|%s\n", debugstr_w(piter->member.run.strText->szData), debugstr_w(pp->member.run.strText->szData));
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
wc->pt = piter->member.run.pt;
|
|
|
|
return piter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-06 17:53:04 +02:00
|
|
|
static ME_DisplayItem *ME_SplitByBacktracking(ME_WrapContext *wc, ME_DisplayItem *p, int loc)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
ME_DisplayItem *piter = p, *pp;
|
|
|
|
int i, idesp, len;
|
|
|
|
ME_Run *run = &p->member.run;
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2008-01-01 22:05:28 +01:00
|
|
|
idesp = i = ME_CharFromPoint(wc->context, loc, run);
|
2005-03-05 12:19:14 +01:00
|
|
|
len = ME_StrVLen(run->strText);
|
|
|
|
assert(len>0);
|
|
|
|
assert(i<len);
|
|
|
|
if (i) {
|
|
|
|
/* don't split words */
|
|
|
|
i = ME_ReverseFindWhitespaceV(run->strText, i);
|
|
|
|
pp = ME_MaximizeSplit(wc, p, i);
|
|
|
|
if (pp)
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
TRACE("Must backtrack to split at: %s\n", debugstr_w(p->member.run.strText->szData));
|
|
|
|
if (wc->pLastSplittableRun)
|
|
|
|
{
|
2005-04-16 12:48:35 +02:00
|
|
|
if (wc->pLastSplittableRun->member.run.nFlags & (MERF_GRAPHICS|MERF_TAB))
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
wc->pt = wc->ptLastSplittableRun;
|
|
|
|
return wc->pLastSplittableRun;
|
|
|
|
}
|
|
|
|
else if (wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE)
|
|
|
|
{
|
|
|
|
/* the following two lines are just to check if we forgot to call UpdateRunFlags earlier,
|
|
|
|
they serve no other purpose */
|
|
|
|
ME_UpdateRunFlags(wc->context->editor, run);
|
|
|
|
assert((wc->pLastSplittableRun->member.run.nFlags & MERF_SPLITTABLE));
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
piter = wc->pLastSplittableRun;
|
|
|
|
run = &piter->member.run;
|
|
|
|
len = ME_StrVLen(run->strText);
|
|
|
|
/* don't split words */
|
|
|
|
i = ME_ReverseFindWhitespaceV(run->strText, len);
|
|
|
|
if (i == len)
|
|
|
|
i = ME_ReverseFindNonWhitespaceV(run->strText, len);
|
2005-04-16 12:48:35 +02:00
|
|
|
if (i) {
|
2008-03-16 21:47:08 +01:00
|
|
|
ME_DisplayItem *piter2 = ME_SplitRun(wc, piter, i);
|
2005-03-05 12:19:14 +01:00
|
|
|
wc->pt = piter2->member.run.pt;
|
|
|
|
return piter2;
|
|
|
|
}
|
|
|
|
/* splittable = must have whitespaces */
|
|
|
|
assert(0 == "Splittable, but no whitespaces");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* restart from the first run beginning with spaces */
|
|
|
|
wc->pt = wc->ptLastSplittableRun;
|
|
|
|
return wc->pLastSplittableRun;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TRACE("Backtracking failed, trying desperate: %s\n", debugstr_w(p->member.run.strText->szData));
|
|
|
|
/* OK, no better idea, so assume we MAY split words if we can split at all*/
|
|
|
|
if (idesp)
|
2008-03-16 21:47:08 +01:00
|
|
|
return ME_SplitRun(wc, piter, idesp);
|
2005-03-05 12:19:14 +01:00
|
|
|
else
|
|
|
|
if (wc->pRowStart && piter != wc->pRowStart)
|
|
|
|
{
|
|
|
|
/* don't need to break current run, because it's possible to split
|
|
|
|
before this run */
|
|
|
|
wc->bOverflown = TRUE;
|
|
|
|
return piter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* split point inside first character - no choice but split after that char */
|
|
|
|
int chars = 1;
|
|
|
|
int pos2 = ME_StrRelPos(run->strText, 0, &chars);
|
|
|
|
if (pos2 != len) {
|
|
|
|
/* the run is more than 1 char, so we may split */
|
2008-03-16 21:47:08 +01:00
|
|
|
return ME_SplitRun(wc, piter, pos2);
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
/* the run is one char, can't split it */
|
|
|
|
return piter;
|
2005-04-16 12:48:35 +02:00
|
|
|
}
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
|
2005-07-06 17:53:04 +02:00
|
|
|
static ME_DisplayItem *ME_WrapHandleRun(ME_WrapContext *wc, ME_DisplayItem *p)
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
ME_DisplayItem *pp;
|
|
|
|
ME_Run *run;
|
|
|
|
int len;
|
|
|
|
|
2005-04-16 12:48:35 +02:00
|
|
|
assert(p->type == diRun);
|
2005-03-05 12:19:14 +01:00
|
|
|
if (!wc->pRowStart)
|
|
|
|
wc->pRowStart = p;
|
|
|
|
run = &p->member.run;
|
|
|
|
run->pt.x = wc->pt.x;
|
|
|
|
run->pt.y = wc->pt.y;
|
2005-04-16 12:48:35 +02:00
|
|
|
ME_WrapSizeRun(wc, p);
|
|
|
|
len = ME_StrVLen(run->strText);
|
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
if (wc->bOverflown) /* just skipping final whitespaces */
|
2005-04-16 12:48:35 +02:00
|
|
|
{
|
2008-07-10 16:17:30 +02:00
|
|
|
/* End paragraph run can't overflow to the next line by itself. */
|
|
|
|
if (run->nFlags & MERF_ENDPARA)
|
|
|
|
return p->next;
|
|
|
|
|
|
|
|
if (run->nFlags & MERF_WHITESPACE) {
|
2005-03-05 12:19:14 +01:00
|
|
|
p->member.run.nFlags |= MERF_SKIPPED;
|
2008-07-10 16:17:39 +02:00
|
|
|
wc->pt.x += run->nWidth;
|
2005-03-05 12:19:14 +01:00
|
|
|
/* skip runs consisting of only whitespaces */
|
|
|
|
return p->next;
|
|
|
|
}
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
if (run->nFlags & MERF_STARTWHITE) {
|
|
|
|
/* try to split the run at the first non-white char */
|
|
|
|
int black;
|
|
|
|
black = ME_FindNonWhitespaceV(run->strText, 0);
|
|
|
|
if (black) {
|
|
|
|
wc->bOverflown = FALSE;
|
2008-03-16 21:47:08 +01:00
|
|
|
pp = ME_SplitRun(wc, p, black);
|
2005-03-05 12:19:14 +01:00
|
|
|
p->member.run.nFlags |= MERF_SKIPPED;
|
|
|
|
ME_InsertRowStart(wc, pp);
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* black run: the row goes from pRowStart to the previous run */
|
|
|
|
ME_InsertRowStart(wc, p);
|
|
|
|
return p;
|
|
|
|
}
|
2008-03-16 21:46:36 +01:00
|
|
|
/* simply end the current row and move on to next one */
|
|
|
|
if (run->nFlags & MERF_ENDROW)
|
|
|
|
{
|
|
|
|
p = p->next;
|
|
|
|
ME_InsertRowStart(wc, p);
|
|
|
|
return p;
|
|
|
|
}
|
2008-03-16 21:47:08 +01:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
/* will current run fit? */
|
|
|
|
if (wc->pt.x + run->nWidth > wc->nAvailWidth)
|
|
|
|
{
|
2005-04-16 12:48:35 +02:00
|
|
|
int loc = wc->nAvailWidth - wc->pt.x;
|
2005-03-05 12:19:14 +01:00
|
|
|
/* total white run ? */
|
|
|
|
if (run->nFlags & MERF_WHITESPACE) {
|
|
|
|
/* let the overflow logic handle it */
|
|
|
|
wc->bOverflown = TRUE;
|
|
|
|
return p;
|
|
|
|
}
|
2008-01-27 19:01:58 +01:00
|
|
|
/* TAB: we can split before */
|
|
|
|
if (run->nFlags & MERF_TAB) {
|
|
|
|
wc->bOverflown = TRUE;
|
2008-07-17 17:55:19 +02:00
|
|
|
if (wc->pRowStart == p)
|
|
|
|
/* Don't split before the start of the run, or we will get an
|
|
|
|
* endless loop. */
|
|
|
|
return p->next;
|
|
|
|
else
|
|
|
|
return p;
|
2008-01-27 19:01:58 +01:00
|
|
|
}
|
|
|
|
/* graphics: we can split before, if run's width is smaller than row's width */
|
|
|
|
if ((run->nFlags & MERF_GRAPHICS) && run->nWidth <= wc->nAvailWidth) {
|
2005-03-05 12:19:14 +01:00
|
|
|
wc->bOverflown = TRUE;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
/* can we separate out the last spaces ? (to use overflow logic later) */
|
|
|
|
if (run->nFlags & MERF_ENDWHITE)
|
|
|
|
{
|
|
|
|
/* we aren't sure if it's *really* necessary, it's a good start however */
|
|
|
|
int black = ME_ReverseFindNonWhitespaceV(run->strText, len);
|
2008-03-16 21:47:08 +01:00
|
|
|
ME_SplitRun(wc, p, black);
|
2005-03-05 12:19:14 +01:00
|
|
|
/* handle both parts again */
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
/* determine the split point by backtracking */
|
|
|
|
pp = ME_SplitByBacktracking(wc, p, loc);
|
|
|
|
if (pp == wc->pRowStart)
|
|
|
|
{
|
2008-07-10 16:17:43 +02:00
|
|
|
if (run->nFlags & MERF_STARTWHITE)
|
|
|
|
{
|
|
|
|
/* we had only spaces so far, so we must be on the first line of the
|
|
|
|
* paragraph, since no other lines of the paragraph start with spaces. */
|
|
|
|
assert(!wc->nRow);
|
|
|
|
/* The lines will only contain spaces, and the rest of the run will
|
|
|
|
* overflow onto the next line. */
|
|
|
|
wc->bOverflown = TRUE;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
/* Couldn't split the first run, possible because we have a large font
|
|
|
|
* with a single character that caused an overflow.
|
|
|
|
*/
|
2008-07-10 16:17:39 +02:00
|
|
|
wc->pt.x += run->nWidth;
|
2005-03-05 12:19:14 +01:00
|
|
|
return p->next;
|
|
|
|
}
|
|
|
|
if (p != pp) /* found a suitable split point */
|
|
|
|
{
|
|
|
|
wc->bOverflown = TRUE;
|
|
|
|
return pp;
|
|
|
|
}
|
|
|
|
/* we detected that it's best to split on start of this run */
|
|
|
|
if (wc->bOverflown)
|
|
|
|
return pp;
|
|
|
|
ERR("failure!\n");
|
|
|
|
/* not found anything - writing over margins is the only option left */
|
|
|
|
}
|
2005-04-16 12:48:35 +02:00
|
|
|
if ((run->nFlags & (MERF_SPLITTABLE | MERF_STARTWHITE))
|
|
|
|
|| ((run->nFlags & (MERF_GRAPHICS|MERF_TAB)) && (p != wc->pRowStart)))
|
2005-03-05 12:19:14 +01:00
|
|
|
{
|
|
|
|
wc->pLastSplittableRun = p;
|
|
|
|
wc->ptLastSplittableRun = wc->pt;
|
|
|
|
}
|
|
|
|
wc->pt.x += run->nWidth;
|
|
|
|
return p->next;
|
|
|
|
}
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2007-12-14 07:45:28 +01:00
|
|
|
static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp);
|
|
|
|
|
2007-12-14 21:29:37 +01:00
|
|
|
static void ME_WrapTextParagraph(ME_Context *c, ME_DisplayItem *tp, DWORD beginofs) {
|
2005-03-05 12:19:14 +01:00
|
|
|
ME_DisplayItem *p;
|
|
|
|
ME_WrapContext wc;
|
2008-01-01 22:04:46 +01:00
|
|
|
int border = 0;
|
2008-01-01 22:04:58 +01:00
|
|
|
int linespace = 0;
|
2005-03-05 12:19:14 +01:00
|
|
|
|
2005-04-16 12:48:35 +02:00
|
|
|
assert(tp->type == diParagraph);
|
2005-03-09 12:48:59 +01:00
|
|
|
if (!(tp->member.para.nFlags & MEPF_REWRAP)) {
|
2005-03-05 12:19:14 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ME_PrepareParagraphForWrapping(c, tp);
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
wc.context = c;
|
|
|
|
/* wc.para_style = tp->member.para.style; */
|
|
|
|
wc.style = NULL;
|
2008-01-01 22:05:33 +01:00
|
|
|
wc.nFirstMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxStartIndent) + beginofs;
|
2008-03-23 09:22:30 +01:00
|
|
|
wc.nLeftMargin = wc.nFirstMargin + ME_twips2pointsX(c, tp->member.para.pFmt->dxOffset);
|
2008-01-01 22:05:33 +01:00
|
|
|
wc.nRightMargin = ME_twips2pointsX(c, tp->member.para.pFmt->dxRightIndent);
|
2005-03-05 12:19:14 +01:00
|
|
|
wc.nRow = 0;
|
|
|
|
wc.pt.x = 0;
|
|
|
|
wc.pt.y = 0;
|
2008-01-01 22:04:52 +01:00
|
|
|
if (tp->member.para.pFmt->dwMask & PFM_SPACEBEFORE)
|
2008-01-01 22:05:33 +01:00
|
|
|
wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceBefore);
|
2008-01-01 22:04:46 +01:00
|
|
|
if (tp->member.para.pFmt->dwMask & PFM_BORDER)
|
|
|
|
{
|
|
|
|
border = ME_GetParaBorderWidth(c->editor, tp->member.para.pFmt->wBorders);
|
|
|
|
if (tp->member.para.pFmt->wBorders & 1) {
|
|
|
|
wc.nFirstMargin += border;
|
|
|
|
wc.nLeftMargin += border;
|
|
|
|
}
|
|
|
|
if (tp->member.para.pFmt->wBorders & 2)
|
|
|
|
wc.nRightMargin -= border;
|
|
|
|
if (tp->member.para.pFmt->wBorders & 4)
|
|
|
|
wc.pt.y += border;
|
|
|
|
}
|
|
|
|
|
2008-01-01 22:05:33 +01:00
|
|
|
linespace = ME_GetParaLineSpace(c, &tp->member.para);
|
2008-01-01 22:04:58 +01:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
ME_BeginRow(&wc);
|
|
|
|
for (p = tp->next; p!=tp->member.para.next_para; ) {
|
|
|
|
assert(p->type != diStartRow);
|
|
|
|
if (p->type == diRun) {
|
|
|
|
p = ME_WrapHandleRun(&wc, p);
|
|
|
|
}
|
2008-01-01 22:04:58 +01:00
|
|
|
else p = p->next;
|
|
|
|
if (wc.nRow && p == wc.pRowStart)
|
|
|
|
wc.pt.y += linespace;
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
ME_WrapEndParagraph(&wc, p);
|
2008-01-01 22:04:46 +01:00
|
|
|
if ((tp->member.para.pFmt->dwMask & PFM_BORDER) && (tp->member.para.pFmt->wBorders & 8))
|
|
|
|
wc.pt.y += border;
|
2008-01-01 22:04:52 +01:00
|
|
|
if (tp->member.para.pFmt->dwMask & PFM_SPACEAFTER)
|
2008-01-01 22:05:33 +01:00
|
|
|
wc.pt.y += ME_twips2pointsY(c, tp->member.para.pFmt->dySpaceAfter);
|
2008-01-01 22:04:46 +01:00
|
|
|
|
2005-03-09 12:48:59 +01:00
|
|
|
tp->member.para.nFlags &= ~MEPF_REWRAP;
|
2005-03-05 12:19:14 +01:00
|
|
|
tp->member.para.nHeight = wc.pt.y;
|
2005-07-05 22:56:14 +02:00
|
|
|
tp->member.para.nRows = wc.nRow;
|
2005-03-05 12:19:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-14 07:45:28 +01:00
|
|
|
static void ME_PrepareParagraphForWrapping(ME_Context *c, ME_DisplayItem *tp) {
|
2006-01-10 20:23:41 +01:00
|
|
|
ME_DisplayItem *p, *pRow;
|
2005-06-30 20:10:22 +02:00
|
|
|
|
2005-03-05 12:19:14 +01:00
|
|
|
/* remove all items that will be reinserted by paragraph wrapper anyway */
|
2005-06-30 20:10:22 +02:00
|
|
|
tp->member.para.nRows = 0;
|
2005-03-05 12:19:14 +01:00
|
|
|
for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
|
|
|
|
switch(p->type) {
|
|
|
|
case diStartRow:
|
2006-01-10 20:23:41 +01:00
|
|
|
pRow = p;
|
2005-03-05 12:19:14 +01:00
|
|
|
p = p->prev;
|
2006-01-10 20:23:41 +01:00
|
|
|
ME_Remove(pRow);
|
|
|
|
ME_DestroyDisplayItem(pRow);
|
2005-03-05 12:19:14 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* join runs that can be joined, set up flags */
|
|
|
|
for (p = tp->next; p!=tp->member.para.next_para; p = p->next) {
|
|
|
|
int changed = 0;
|
|
|
|
switch(p->type) {
|
|
|
|
case diStartRow: assert(0); break; /* should have deleted it */
|
|
|
|
case diRun:
|
|
|
|
while (p->next->type == diRun) { /* FIXME */
|
|
|
|
if (ME_CanJoinRuns(&p->member.run, &p->next->member.run)) {
|
|
|
|
ME_JoinRuns(c->editor, p);
|
|
|
|
changed = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p->member.run.nFlags &= ~MERF_CALCBYWRAP;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-09 12:48:59 +01:00
|
|
|
|
2005-03-19 18:06:17 +01:00
|
|
|
BOOL ME_WrapMarkedParagraphs(ME_TextEditor *editor) {
|
2005-03-09 12:48:59 +01:00
|
|
|
ME_DisplayItem *item;
|
|
|
|
ME_Context c;
|
2005-03-19 18:06:17 +01:00
|
|
|
BOOL bModified = FALSE;
|
2008-04-23 23:13:07 +02:00
|
|
|
int yStart = -1;
|
2008-06-23 20:49:49 +02:00
|
|
|
int yLastPos = 0;
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2008-03-16 21:48:13 +01:00
|
|
|
ME_InitContext(&c, editor, GetDC(editor->hWnd));
|
2007-08-15 19:39:43 +02:00
|
|
|
editor->nHeight = 0;
|
2005-03-09 12:48:59 +01:00
|
|
|
item = editor->pBuffer->pFirst->next;
|
|
|
|
while(item != editor->pBuffer->pLast) {
|
|
|
|
BOOL bRedraw = FALSE;
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2005-03-09 12:48:59 +01:00
|
|
|
assert(item->type == diParagraph);
|
2007-08-15 19:39:43 +02:00
|
|
|
editor->nHeight = max(editor->nHeight, item->member.para.nYPos);
|
2005-03-09 12:48:59 +01:00
|
|
|
if ((item->member.para.nFlags & MEPF_REWRAP)
|
|
|
|
|| (item->member.para.nYPos != c.pt.y))
|
|
|
|
bRedraw = TRUE;
|
|
|
|
item->member.para.nYPos = c.pt.y;
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2007-12-14 21:29:37 +01:00
|
|
|
ME_WrapTextParagraph(&c, item, editor->selofs);
|
2005-03-09 12:48:59 +01:00
|
|
|
|
|
|
|
if (bRedraw)
|
2006-01-10 20:23:41 +01:00
|
|
|
{
|
2005-03-09 12:48:59 +01:00
|
|
|
item->member.para.nFlags |= MEPF_REPAINT;
|
2006-01-10 20:23:41 +01:00
|
|
|
if (yStart == -1)
|
|
|
|
yStart = c.pt.y;
|
|
|
|
}
|
2005-03-09 12:48:59 +01:00
|
|
|
|
2005-03-19 18:06:17 +01:00
|
|
|
bModified = bModified | bRedraw;
|
|
|
|
|
2008-06-23 20:49:49 +02:00
|
|
|
yLastPos = c.pt.y;
|
2005-03-09 12:48:59 +01:00
|
|
|
c.pt.y += item->member.para.nHeight;
|
|
|
|
item = item->member.para.next_para;
|
|
|
|
}
|
|
|
|
editor->sizeWindow.cx = c.rcView.right-c.rcView.left;
|
|
|
|
editor->sizeWindow.cy = c.rcView.bottom-c.rcView.top;
|
2005-11-03 10:52:29 +01:00
|
|
|
|
2005-03-09 19:43:18 +01:00
|
|
|
editor->nTotalLength = c.pt.y;
|
2008-06-23 20:49:49 +02:00
|
|
|
editor->pBuffer->pLast->member.para.nYPos = yLastPos;
|
2005-04-16 12:48:35 +02:00
|
|
|
|
2008-03-16 21:48:13 +01:00
|
|
|
ME_DestroyContext(&c, editor->hWnd);
|
|
|
|
|
2008-04-28 05:25:47 +02:00
|
|
|
/* Each paragraph may contain multiple rows, which should be scrollable, even
|
|
|
|
if the containing paragraph has nYPos == 0 */
|
|
|
|
item = editor->pBuffer->pFirst;
|
|
|
|
while ((item = ME_FindItemFwd(item, diStartRow)) != NULL) {
|
|
|
|
assert(item->type == diStartRow);
|
|
|
|
editor->nHeight = max(editor->nHeight, item->member.row.nYPos);
|
|
|
|
}
|
|
|
|
|
2006-08-07 17:27:16 +02:00
|
|
|
if (bModified || editor->nTotalLength < editor->nLastTotalLength)
|
|
|
|
ME_InvalidateMarkedParagraphs(editor);
|
|
|
|
return bModified;
|
|
|
|
}
|
2006-01-10 20:23:41 +01:00
|
|
|
|
2006-08-07 17:27:16 +02:00
|
|
|
void ME_InvalidateMarkedParagraphs(ME_TextEditor *editor) {
|
|
|
|
ME_Context c;
|
|
|
|
|
2008-03-16 21:48:13 +01:00
|
|
|
ME_InitContext(&c, editor, GetDC(editor->hWnd));
|
2006-01-10 20:23:41 +01:00
|
|
|
if (editor->bRedraw)
|
|
|
|
{
|
2006-08-07 17:27:16 +02:00
|
|
|
RECT rc = c.rcView;
|
|
|
|
int ofs = ME_GetYScrollPos(editor);
|
2006-01-10 20:23:41 +01:00
|
|
|
|
2006-08-07 17:27:16 +02:00
|
|
|
ME_DisplayItem *item = editor->pBuffer->pFirst;
|
|
|
|
while(item != editor->pBuffer->pLast) {
|
|
|
|
if (item->member.para.nFlags & MEPF_REPAINT) {
|
|
|
|
rc.top = item->member.para.nYPos - ofs;
|
|
|
|
rc.bottom = item->member.para.nYPos + item->member.para.nHeight - ofs;
|
|
|
|
InvalidateRect(editor->hWnd, &rc, TRUE);
|
|
|
|
}
|
|
|
|
item = item->member.para.next_para;
|
|
|
|
}
|
|
|
|
if (editor->nTotalLength < editor->nLastTotalLength)
|
|
|
|
{
|
|
|
|
rc.top = editor->nTotalLength - ofs;
|
|
|
|
rc.bottom = editor->nLastTotalLength - ofs;
|
|
|
|
InvalidateRect(editor->hWnd, &rc, TRUE);
|
|
|
|
}
|
2006-01-10 20:23:41 +01:00
|
|
|
}
|
2008-03-16 21:48:13 +01:00
|
|
|
ME_DestroyContext(&c, editor->hWnd);
|
2005-03-09 12:48:59 +01:00
|
|
|
}
|
2005-11-03 10:52:29 +01:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ME_SendRequestResize(ME_TextEditor *editor, BOOL force)
|
|
|
|
{
|
|
|
|
if (editor->nEventMask & ENM_REQUESTRESIZE)
|
|
|
|
{
|
|
|
|
RECT rc;
|
|
|
|
|
|
|
|
GetClientRect(editor->hWnd, &rc);
|
|
|
|
|
|
|
|
if (force || rc.bottom != editor->nTotalLength)
|
|
|
|
{
|
|
|
|
REQRESIZE info;
|
|
|
|
|
|
|
|
info.nmhdr.hwndFrom = editor->hWnd;
|
|
|
|
info.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
|
|
|
|
info.nmhdr.code = EN_REQUESTRESIZE;
|
|
|
|
info.rc = rc;
|
|
|
|
info.rc.bottom = editor->nTotalLength;
|
2008-01-01 22:03:52 +01:00
|
|
|
|
|
|
|
editor->nEventMask &= ~ENM_REQUESTRESIZE;
|
2005-11-03 10:52:29 +01:00
|
|
|
SendMessageW(GetParent(editor->hWnd), WM_NOTIFY,
|
|
|
|
info.nmhdr.idFrom, (LPARAM)&info);
|
2008-01-01 22:03:52 +01:00
|
|
|
editor->nEventMask |= ENM_REQUESTRESIZE;
|
2005-11-03 10:52:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|