2 * RichEdit - functions working on paragraphs of text (diParagraph).
4 * Copyright 2004 by Krzysztof Foltman
5 * Copyright 2006 by Phil Krylov
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
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 static const WCHAR wszParagraphSign[] = {0xB6, 0};
28 void ME_MakeFirstParagraph(ME_TextEditor *editor)
35 ME_TextBuffer *text = editor->pBuffer;
36 ME_DisplayItem *para = ME_MakeDI(diParagraph);
40 ME_InitContext(&c, editor, GetDC(editor->hWnd));
42 hf = (HFONT)GetStockObject(SYSTEM_FONT);
44 GetObjectW(hf, sizeof(LOGFONTW), &lf);
45 ZeroMemory(&cf, sizeof(cf));
46 cf.cbSize = sizeof(cf);
47 cf.dwMask = CFM_BACKCOLOR|CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_CHARSET;
48 cf.dwMask |= CFM_ALLCAPS|CFM_BOLD|CFM_DISABLED|CFM_EMBOSS|CFM_HIDDEN;
49 cf.dwMask |= CFM_IMPRINT|CFM_ITALIC|CFM_LINK|CFM_OUTLINE|CFM_PROTECTED;
50 cf.dwMask |= CFM_REVISED|CFM_SHADOW|CFM_SMALLCAPS|CFM_STRIKEOUT;
51 cf.dwMask |= CFM_SUBSCRIPT|CFM_UNDERLINETYPE|CFM_WEIGHT;
53 cf.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
54 lstrcpyW(cf.szFaceName, lf.lfFaceName);
55 /* Convert system font height from logical units to twips for cf.yHeight */
56 cf.yHeight = (lf.lfHeight * 72 * 1440) / (c.dpi.cy * c.dpi.cy);
57 if (lf.lfWeight > FW_NORMAL) cf.dwEffects |= CFE_BOLD;
58 cf.wWeight = lf.lfWeight;
59 if (lf.lfItalic) cf.dwEffects |= CFE_ITALIC;
60 cf.bUnderlineType = (lf.lfUnderline) ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
61 if (lf.lfStrikeOut) cf.dwEffects |= CFE_STRIKEOUT;
62 cf.bPitchAndFamily = lf.lfPitchAndFamily;
63 cf.bCharSet = lf.lfCharSet;
65 ZeroMemory(&fmt, sizeof(fmt));
66 fmt.cbSize = sizeof(fmt);
67 fmt.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_STARTINDENT | PFM_RIGHTINDENT | PFM_TABSTOPS;
68 fmt.wAlignment = PFA_LEFT;
70 *para->member.para.pFmt = fmt;
72 style = ME_MakeStyle(&cf);
73 text->pDefaultStyle = style;
75 run = ME_MakeRun(style, ME_MakeString(wszParagraphSign), MERF_ENDPARA);
76 run->member.run.nCharOfs = 0;
77 run->member.run.nCR = 1;
78 run->member.run.nLF = (editor->bEmulateVersion10) ? 1 : 0;
80 ME_InsertBefore(text->pLast, para);
81 ME_InsertBefore(text->pLast, run);
82 para->member.para.prev_para = text->pFirst;
83 para->member.para.next_para = text->pLast;
84 text->pFirst->member.para.next_para = para;
85 text->pLast->member.para.prev_para = para;
87 text->pLast->member.para.nCharOfs = 1;
89 ME_DestroyContext(&c, editor->hWnd);
92 void ME_MarkAllForWrapping(ME_TextEditor *editor)
94 ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
97 void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
101 first->member.para.nFlags |= MEPF_REWRAP;
102 first = first->member.para.next_para;
106 void ME_MarkForPainting(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
108 while(first != last && first)
110 first->member.para.nFlags |= MEPF_REPAINT;
111 first = first->member.para.next_para;
115 /* split paragraph at the beginning of the run */
116 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run, ME_Style *style, int numCR, int numLF)
118 ME_DisplayItem *next_para = NULL;
119 ME_DisplayItem *run_para = NULL;
120 ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
121 ME_DisplayItem *end_run = ME_MakeRun(style,ME_MakeString(wszParagraphSign), MERF_ENDPARA);
122 ME_UndoItem *undo = NULL;
125 int end_len = numCR + numLF;
127 assert(run->type == diRun);
129 end_run->member.run.nCR = numCR;
130 end_run->member.run.nLF = numLF;
131 run_para = ME_GetParagraph(run);
132 assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
134 ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
135 next_para = run_para->member.para.next_para;
136 assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
138 undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
140 undo->nStart = run_para->member.para.nCharOfs + ofs;
142 /* the new paragraph will have a different starting offset, so let's update its runs */
144 while(pp->type == diRun) {
145 pp->member.run.nCharOfs -= ofs;
146 pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
148 new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
149 new_para->member.para.nCharOfs += end_len;
151 new_para->member.para.nFlags = MEPF_REWRAP; /* FIXME copy flags (if applicable) */
152 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
153 *new_para->member.para.pFmt = *run_para->member.para.pFmt;
155 new_para->member.para.bTable = run_para->member.para.bTable;
157 /* Inherit previous cell definitions if any */
158 new_para->member.para.pCells = NULL;
159 if (run_para->member.para.pCells)
161 ME_TableCell *pCell, *pNewCell;
163 for (pCell = run_para->member.para.pCells; pCell; pCell = pCell->next)
165 pNewCell = ALLOC_OBJ(ME_TableCell);
166 pNewCell->nRightBoundary = pCell->nRightBoundary;
167 pNewCell->next = NULL;
168 if (new_para->member.para.pCells)
169 new_para->member.para.pLastCell->next = pNewCell;
171 new_para->member.para.pCells = pNewCell;
172 new_para->member.para.pLastCell = pNewCell;
176 /* fix paragraph properties. FIXME only needed when called from RTF reader */
177 if (run_para->member.para.pCells && !run_para->member.para.bTable)
179 /* Paragraph does not have an \intbl keyword, so any table definition
180 * stored is invalid */
181 ME_DestroyTableCellList(run_para);
184 /* insert paragraph into paragraph double linked list */
185 new_para->member.para.prev_para = run_para;
186 new_para->member.para.next_para = next_para;
187 run_para->member.para.next_para = new_para;
188 next_para->member.para.prev_para = new_para;
190 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
191 ME_InsertBefore(run, new_para);
192 ME_InsertBefore(new_para, end_run);
194 /* force rewrap of the */
195 run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
196 new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
198 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
199 ME_PropagateCharOffset(next_para, end_len);
200 editor->nParagraphs++;
205 /* join tp with tp->member.para.next_para, keeping tp's style; this
206 * is consistent with the original */
207 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp)
209 ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
211 ME_UndoItem *undo = NULL;
214 assert(tp->type == diParagraph);
215 assert(tp->member.para.next_para);
216 assert(tp->member.para.next_para->type == diParagraph);
218 pNext = tp->member.para.next_para;
220 /* Need to locate end-of-paragraph run here, in order to know end_len */
221 pRun = ME_FindItemBack(pNext, diRunOrParagraph);
224 assert(pRun->type == diRun);
225 assert(pRun->member.run.nFlags & MERF_ENDPARA);
227 end_len = pRun->member.run.nCR + pRun->member.run.nLF;
230 /* null char format operation to store the original char format for the ENDPARA run */
232 ME_InitCharFormat2W(&fmt);
233 ME_SetCharFormat(editor, pNext->member.para.nCharOfs - end_len, end_len, &fmt);
235 undo = ME_AddUndoItem(editor, diUndoSplitParagraph, NULL);
238 undo->nStart = pNext->member.para.nCharOfs - end_len;
239 undo->nCR = pRun->member.run.nCR;
240 undo->nLF = pRun->member.run.nLF;
241 assert(pNext->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
242 *undo->di.member.para.pFmt = *pNext->member.para.pFmt;
245 shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
247 pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
249 assert(pFirstRunInNext->type == diRun);
251 /* if some cursor points at end of paragraph, make it point to the first
252 run of the next joined paragraph */
253 for (i=0; i<editor->nCursors; i++) {
254 if (editor->pCursors[i].pRun == pRun) {
255 editor->pCursors[i].pRun = pFirstRunInNext;
256 editor->pCursors[i].nOffset = 0;
262 pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
263 if (pTmp->type != diRun)
265 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
266 pTmp->member.run.nCharOfs += shift;
270 ME_DestroyDisplayItem(pRun);
272 if (editor->pLastSelStartPara == pNext)
273 editor->pLastSelStartPara = tp;
274 if (editor->pLastSelEndPara == pNext)
275 editor->pLastSelEndPara = tp;
277 tp->member.para.next_para = pNext->member.para.next_para;
278 pNext->member.para.next_para->member.para.prev_para = tp;
280 ME_DestroyDisplayItem(pNext);
282 ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
284 ME_CheckCharOffsets(editor);
286 editor->nParagraphs--;
287 tp->member.para.nFlags |= MEPF_REWRAP;
291 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
292 return ME_FindItemBackOrHere(item, diParagraph);
295 void ME_DumpParaStyleToBuf(const PARAFORMAT2 *pFmt, char buf[2048])
300 #define DUMP(mask, name, fmt, field) \
301 if (pFmt->dwMask & (mask)) p += sprintf(p, "%-22s" fmt "\n", name, pFmt->field); \
302 else p += sprintf(p, "%-22sN/A\n", name);
304 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
305 #define DUMP_EFFECT(mask, name) \
306 p += sprintf(p, "%-22s%s\n", name, (pFmt->dwMask & (mask)) ? ((pFmt->wEffects & ((mask) >> 8)) ? "yes" : "no") : "N/A");
308 DUMP(PFM_NUMBERING, "Numbering:", "%u", wNumbering);
309 DUMP_EFFECT(PFM_DONOTHYPHEN, "Disable auto-hyphen:");
310 DUMP_EFFECT(PFM_KEEP, "No page break in para:");
311 DUMP_EFFECT(PFM_KEEPNEXT, "No page break in para & next:");
312 DUMP_EFFECT(PFM_NOLINENUMBER, "No line number:");
313 DUMP_EFFECT(PFM_NOWIDOWCONTROL, "No widow & orphan:");
314 DUMP_EFFECT(PFM_PAGEBREAKBEFORE, "Page break before:");
315 DUMP_EFFECT(PFM_RTLPARA, "RTL para:");
316 DUMP_EFFECT(PFM_SIDEBYSIDE, "Side by side:");
317 DUMP_EFFECT(PFM_TABLE, "Table:");
318 DUMP(PFM_OFFSETINDENT, "Offset indent:", "%d", dxStartIndent);
319 DUMP(PFM_STARTINDENT, "Start indent:", "%d", dxStartIndent);
320 DUMP(PFM_RIGHTINDENT, "Right indent:", "%d", dxRightIndent);
321 DUMP(PFM_OFFSET, "Offset:", "%d", dxOffset);
322 if (pFmt->dwMask & PFM_ALIGNMENT) {
323 switch (pFmt->wAlignment) {
324 case PFA_LEFT : p += sprintf(p, "Alignment: left\n"); break;
325 case PFA_RIGHT : p += sprintf(p, "Alignment: right\n"); break;
326 case PFA_CENTER : p += sprintf(p, "Alignment: center\n"); break;
327 case PFA_JUSTIFY: p += sprintf(p, "Alignment: justify\n"); break;
328 default : p += sprintf(p, "Alignment: incorrect %d\n", pFmt->wAlignment); break;
331 else p += sprintf(p, "Alignment: N/A\n");
332 DUMP(PFM_TABSTOPS, "Tab Stops:", "%d", cTabCount);
333 if (pFmt->dwMask & PFM_TABSTOPS) {
335 p += sprintf(p, "\t");
336 for (i = 0; i < pFmt->cTabCount; i++) p += sprintf(p, "%x ", pFmt->rgxTabs[i]);
337 p += sprintf(p, "\n");
339 DUMP(PFM_SPACEBEFORE, "Space Before:", "%d", dySpaceBefore);
340 DUMP(PFM_SPACEAFTER, "Space After:", "%d", dySpaceAfter);
341 DUMP(PFM_LINESPACING, "Line spacing:", "%d", dyLineSpacing);
342 DUMP(PFM_STYLE, "Text style:", "%d", sStyle);
343 DUMP(PFM_LINESPACING, "Line spacing rule:", "%u", bLineSpacingRule);
344 /* bOutlineLevel should be 0 */
345 DUMP(PFM_SHADING, "Shading Weigth:", "%u", wShadingWeight);
346 DUMP(PFM_SHADING, "Shading Style:", "%u", wShadingStyle);
347 DUMP(PFM_NUMBERINGSTART, "Numbering Start:", "%u", wNumberingStart);
348 DUMP(PFM_NUMBERINGSTYLE, "Numbering Style:", "0x%x", wNumberingStyle);
349 DUMP(PFM_NUMBERINGTAB, "Numbering Tab:", "%u", wNumberingStyle);
350 DUMP(PFM_BORDER, "Border Space:", "%u", wBorderSpace);
351 DUMP(PFM_BORDER, "Border Width:", "%u", wBorderWidth);
352 DUMP(PFM_BORDER, "Borders:", "%u", wBorders);
358 void ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, const PARAFORMAT2 *pFmt)
361 assert(sizeof(*para->member.para.pFmt) == sizeof(PARAFORMAT2));
362 ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
364 copy = *para->member.para.pFmt;
366 #define COPY_FIELD(m, f) \
367 if (pFmt->dwMask & (m)) { \
368 para->member.para.pFmt->dwMask |= m; \
369 para->member.para.pFmt->f = pFmt->f; \
372 COPY_FIELD(PFM_NUMBERING, wNumbering);
373 #define EFFECTS_MASK (PFM_RTLPARA|PFM_KEEP|PFM_KEEPNEXT|PFM_PAGEBREAKBEFORE| \
374 PFM_NOLINENUMBER|PFM_NOWIDOWCONTROL|PFM_DONOTHYPHEN|PFM_SIDEBYSIDE| \
376 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
377 if (pFmt->dwMask & EFFECTS_MASK) {
378 para->member.para.pFmt->dwMask &= ~(pFmt->dwMask & EFFECTS_MASK);
379 para->member.para.pFmt->wEffects |= pFmt->wEffects & HIWORD(pFmt->dwMask);
383 COPY_FIELD(PFM_STARTINDENT, dxStartIndent);
384 if (pFmt->dwMask & PFM_OFFSETINDENT)
385 para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
386 COPY_FIELD(PFM_RIGHTINDENT, dxRightIndent);
387 COPY_FIELD(PFM_OFFSET, dxOffset);
388 COPY_FIELD(PFM_ALIGNMENT, wAlignment);
390 if (pFmt->dwMask & PFM_TABSTOPS)
392 para->member.para.pFmt->cTabCount = pFmt->cTabCount;
393 memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(LONG));
395 COPY_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
396 COPY_FIELD(PFM_SPACEAFTER, dySpaceAfter);
397 COPY_FIELD(PFM_LINESPACING, dyLineSpacing);
398 COPY_FIELD(PFM_STYLE, sStyle);
399 COPY_FIELD(PFM_LINESPACING, bLineSpacingRule);
400 COPY_FIELD(PFM_SHADING, wShadingWeight);
401 COPY_FIELD(PFM_SHADING, wShadingStyle);
402 COPY_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
403 COPY_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
404 COPY_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
405 COPY_FIELD(PFM_BORDER, wBorderSpace);
406 COPY_FIELD(PFM_BORDER, wBorderWidth);
407 COPY_FIELD(PFM_BORDER, wBorders);
409 para->member.para.pFmt->dwMask |= pFmt->dwMask;
412 if (memcmp(©, para->member.para.pFmt, sizeof(PARAFORMAT2)))
413 para->member.para.nFlags |= MEPF_REWRAP;
418 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
420 ME_Cursor *pEndCursor = &editor->pCursors[1];
422 *para = ME_GetParagraph(editor->pCursors[0].pRun);
423 *para_end = ME_GetParagraph(editor->pCursors[1].pRun);
424 if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
425 ME_DisplayItem *tmp = *para;
429 pEndCursor = &editor->pCursors[0];
432 /* selection consists of chars from nFrom up to nTo-1 */
433 if ((*para_end)->member.para.nCharOfs > (*para)->member.para.nCharOfs) {
434 if (!pEndCursor->nOffset) {
435 *para_end = ME_GetParagraph(ME_FindItemBack(pEndCursor->pRun, diRun));
441 void ME_SetSelectionParaFormat(ME_TextEditor *editor, const PARAFORMAT2 *pFmt)
443 ME_DisplayItem *para, *para_end;
445 ME_GetSelectionParas(editor, ¶, ¶_end);
448 ME_SetParaFormat(editor, para, pFmt);
449 if (para == para_end)
451 para = para->member.para.next_para;
455 void ME_GetParaFormat(ME_TextEditor *editor, const ME_DisplayItem *para, PARAFORMAT2 *pFmt)
457 if (pFmt->cbSize >= sizeof(PARAFORMAT2))
459 *pFmt = *para->member.para.pFmt;
462 CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
465 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
467 ME_DisplayItem *para, *para_end;
470 ME_GetSelectionParas(editor, ¶, ¶_end);
472 ME_GetParaFormat(editor, para, pFmt);
473 if (para == para_end) return;
476 ZeroMemory(&tmp, sizeof(tmp));
477 tmp.cbSize = sizeof(tmp);
478 ME_GetParaFormat(editor, para, &tmp);
480 #define CHECK_FIELD(m, f) \
481 if (pFmt->f != tmp.f) pFmt->dwMask &= ~(m);
483 CHECK_FIELD(PFM_NUMBERING, wNumbering);
484 /* para->member.para.pFmt->wEffects = pFmt->wEffects; */
485 assert(tmp.dwMask & PFM_ALIGNMENT);
486 CHECK_FIELD(PFM_NUMBERING, wNumbering);
487 assert(tmp.dwMask & PFM_STARTINDENT);
488 CHECK_FIELD(PFM_STARTINDENT, dxStartIndent);
489 assert(tmp.dwMask & PFM_RIGHTINDENT);
490 CHECK_FIELD(PFM_RIGHTINDENT, dxRightIndent);
491 assert(tmp.dwMask & PFM_OFFSET);
492 CHECK_FIELD(PFM_OFFSET, dxOffset);
493 CHECK_FIELD(PFM_ALIGNMENT, wAlignment);
495 assert(tmp.dwMask & PFM_TABSTOPS);
496 if (pFmt->dwMask & PFM_TABSTOPS) {
497 if (pFmt->cTabCount != tmp.cTabCount ||
498 memcmp(pFmt->rgxTabs, tmp.rgxTabs, tmp.cTabCount*sizeof(int)))
499 pFmt->dwMask &= ~PFM_TABSTOPS;
502 CHECK_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
503 CHECK_FIELD(PFM_SPACEAFTER, dySpaceAfter);
504 CHECK_FIELD(PFM_LINESPACING, dyLineSpacing);
505 CHECK_FIELD(PFM_STYLE, sStyle);
506 CHECK_FIELD(PFM_SPACEAFTER, bLineSpacingRule);
507 CHECK_FIELD(PFM_SHADING, wShadingWeight);
508 CHECK_FIELD(PFM_SHADING, wShadingStyle);
509 CHECK_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
510 CHECK_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
511 CHECK_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
512 CHECK_FIELD(PFM_BORDER, wBorderSpace);
513 CHECK_FIELD(PFM_BORDER, wBorderWidth);
514 CHECK_FIELD(PFM_BORDER, wBorders);
518 if (para == para_end)
520 para = para->member.para.next_para;