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 cf.yHeight = ME_twips2pointsY(&c, lf.lfHeight);
56 if (lf.lfWeight > FW_NORMAL) cf.dwEffects |= CFE_BOLD;
57 cf.wWeight = lf.lfWeight;
58 if (lf.lfItalic) cf.dwEffects |= CFE_ITALIC;
59 cf.bUnderlineType = (lf.lfUnderline) ? CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
60 if (lf.lfStrikeOut) cf.dwEffects |= CFE_STRIKEOUT;
61 cf.bPitchAndFamily = lf.lfPitchAndFamily;
62 cf.bCharSet = lf.lfCharSet;
64 ZeroMemory(&fmt, sizeof(fmt));
65 fmt.cbSize = sizeof(fmt);
66 fmt.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_STARTINDENT | PFM_RIGHTINDENT | PFM_TABSTOPS;
67 fmt.wAlignment = PFA_LEFT;
69 *para->member.para.pFmt = fmt;
71 style = ME_MakeStyle(&cf);
72 text->pDefaultStyle = style;
74 run = ME_MakeRun(style, ME_MakeString(wszParagraphSign), MERF_ENDPARA);
75 run->member.run.nCharOfs = 0;
76 run->member.run.nCR = 1;
77 run->member.run.nLF = (editor->bEmulateVersion10) ? 1 : 0;
79 ME_InsertBefore(text->pLast, para);
80 ME_InsertBefore(text->pLast, run);
81 para->member.para.prev_para = text->pFirst;
82 para->member.para.next_para = text->pLast;
83 text->pFirst->member.para.next_para = para;
84 text->pLast->member.para.prev_para = para;
86 text->pLast->member.para.nCharOfs = 1;
88 ME_DestroyContext(&c, editor->hWnd);
91 void ME_MarkAllForWrapping(ME_TextEditor *editor)
93 ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
96 void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
100 first->member.para.nFlags |= MEPF_REWRAP;
101 first = first->member.para.next_para;
105 void ME_MarkForPainting(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
109 first->member.para.nFlags |= MEPF_REPAINT;
110 first = first->member.para.next_para;
114 /* split paragraph at the beginning of the run */
115 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run, ME_Style *style, int numCR, int numLF)
117 ME_DisplayItem *next_para = NULL;
118 ME_DisplayItem *run_para = NULL;
119 ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
120 ME_DisplayItem *end_run = ME_MakeRun(style,ME_MakeString(wszParagraphSign), MERF_ENDPARA);
121 ME_UndoItem *undo = NULL;
124 int end_len = numCR + numLF;
126 assert(run->type == diRun);
128 end_run->member.run.nCR = numCR;
129 end_run->member.run.nLF = numLF;
130 run_para = ME_GetParagraph(run);
131 assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
133 ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
134 next_para = run_para->member.para.next_para;
135 assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
137 undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
139 undo->nStart = run_para->member.para.nCharOfs + ofs;
141 /* the new paragraph will have a different starting offset, so let's update its runs */
143 while(pp->type == diRun) {
144 pp->member.run.nCharOfs -= ofs;
145 pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
147 new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
148 new_para->member.para.nCharOfs += end_len;
150 new_para->member.para.nFlags = MEPF_REWRAP; /* FIXME copy flags (if applicable) */
151 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
152 *new_para->member.para.pFmt = *run_para->member.para.pFmt;
154 new_para->member.para.bTable = run_para->member.para.bTable;
156 /* Inherit previous cell definitions if any */
157 new_para->member.para.pCells = NULL;
158 if (run_para->member.para.pCells)
160 ME_TableCell *pCell, *pNewCell;
162 for (pCell = run_para->member.para.pCells; pCell; pCell = pCell->next)
164 pNewCell = ALLOC_OBJ(ME_TableCell);
165 pNewCell->nRightBoundary = pCell->nRightBoundary;
166 pNewCell->next = NULL;
167 if (new_para->member.para.pCells)
168 new_para->member.para.pLastCell->next = pNewCell;
170 new_para->member.para.pCells = pNewCell;
171 new_para->member.para.pLastCell = pNewCell;
175 /* fix paragraph properties. FIXME only needed when called from RTF reader */
176 if (run_para->member.para.pCells && !run_para->member.para.bTable)
178 /* Paragraph does not have an \intbl keyword, so any table definition
179 * stored is invalid */
180 ME_DestroyTableCellList(run_para);
183 /* insert paragraph into paragraph double linked list */
184 new_para->member.para.prev_para = run_para;
185 new_para->member.para.next_para = next_para;
186 run_para->member.para.next_para = new_para;
187 next_para->member.para.prev_para = new_para;
189 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
190 ME_InsertBefore(run, new_para);
191 ME_InsertBefore(new_para, end_run);
193 /* force rewrap of the */
194 run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
195 new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
197 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
198 ME_PropagateCharOffset(next_para, end_len);
199 editor->nParagraphs++;
204 /* join tp with tp->member.para.next_para, keeping tp's style; this
205 * is consistent with the original */
206 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp)
208 ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
210 ME_UndoItem *undo = NULL;
213 assert(tp->type == diParagraph);
214 assert(tp->member.para.next_para);
215 assert(tp->member.para.next_para->type == diParagraph);
217 pNext = tp->member.para.next_para;
219 /* Need to locate end-of-paragraph run here, in order to know end_len */
220 pRun = ME_FindItemBack(pNext, diRunOrParagraph);
223 assert(pRun->type == diRun);
224 assert(pRun->member.run.nFlags & MERF_ENDPARA);
226 end_len = pRun->member.run.nCR + pRun->member.run.nLF;
229 /* null char format operation to store the original char format for the ENDPARA run */
231 ME_InitCharFormat2W(&fmt);
232 ME_SetCharFormat(editor, pNext->member.para.nCharOfs - end_len, end_len, &fmt);
234 undo = ME_AddUndoItem(editor, diUndoSplitParagraph, NULL);
237 undo->nStart = pNext->member.para.nCharOfs - end_len;
238 undo->nCR = pRun->member.run.nCR;
239 undo->nLF = pRun->member.run.nLF;
240 assert(pNext->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
241 *undo->di.member.para.pFmt = *pNext->member.para.pFmt;
244 shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
246 pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
248 assert(pFirstRunInNext->type == diRun);
250 /* if some cursor points at end of paragraph, make it point to the first
251 run of the next joined paragraph */
252 for (i=0; i<editor->nCursors; i++) {
253 if (editor->pCursors[i].pRun == pRun) {
254 editor->pCursors[i].pRun = pFirstRunInNext;
255 editor->pCursors[i].nOffset = 0;
261 pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
262 if (pTmp->type != diRun)
264 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
265 pTmp->member.run.nCharOfs += shift;
269 ME_DestroyDisplayItem(pRun);
271 if (editor->pLastSelStartPara == pNext)
272 editor->pLastSelStartPara = tp;
273 if (editor->pLastSelEndPara == pNext)
274 editor->pLastSelEndPara = tp;
276 tp->member.para.next_para = pNext->member.para.next_para;
277 pNext->member.para.next_para->member.para.prev_para = tp;
279 ME_DestroyDisplayItem(pNext);
281 ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
283 ME_CheckCharOffsets(editor);
285 editor->nParagraphs--;
286 tp->member.para.nFlags |= MEPF_REWRAP;
290 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
291 return ME_FindItemBackOrHere(item, diParagraph);
294 void ME_DumpParaStyleToBuf(const PARAFORMAT2 *pFmt, char buf[2048])
299 #define DUMP(mask, name, fmt, field) \
300 if (pFmt->dwMask & (mask)) p += sprintf(p, "%-22s" fmt "\n", name, pFmt->field); \
301 else p += sprintf(p, "%-22sN/A\n", name);
303 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
304 #define DUMP_EFFECT(mask, name) \
305 p += sprintf(p, "%-22s%s\n", name, (pFmt->dwMask & (mask)) ? ((pFmt->wEffects & ((mask) >> 8)) ? "yes" : "no") : "N/A");
307 DUMP(PFM_NUMBERING, "Numbering:", "%u", wNumbering);
308 DUMP_EFFECT(PFM_DONOTHYPHEN, "Disable auto-hyphen:");
309 DUMP_EFFECT(PFM_KEEP, "No page break in para:");
310 DUMP_EFFECT(PFM_KEEPNEXT, "No page break in para & next:");
311 DUMP_EFFECT(PFM_NOLINENUMBER, "No line number:");
312 DUMP_EFFECT(PFM_NOWIDOWCONTROL, "No widow & orphan:");
313 DUMP_EFFECT(PFM_PAGEBREAKBEFORE, "Page break before:");
314 DUMP_EFFECT(PFM_RTLPARA, "RTL para:");
315 DUMP_EFFECT(PFM_SIDEBYSIDE, "Side by side:");
316 DUMP_EFFECT(PFM_TABLE, "Table:");
317 DUMP(PFM_OFFSETINDENT, "Offset indent:", "%d", dxStartIndent);
318 DUMP(PFM_STARTINDENT, "Start indent:", "%d", dxStartIndent);
319 DUMP(PFM_RIGHTINDENT, "Right indent:", "%d", dxRightIndent);
320 DUMP(PFM_OFFSET, "Offset:", "%d", dxOffset);
321 if (pFmt->dwMask & PFM_ALIGNMENT) {
322 switch (pFmt->wAlignment) {
323 case PFA_LEFT : p += sprintf(p, "Alignment: left\n"); break;
324 case PFA_RIGHT : p += sprintf(p, "Alignment: right\n"); break;
325 case PFA_CENTER : p += sprintf(p, "Alignment: center\n"); break;
326 case PFA_JUSTIFY: p += sprintf(p, "Alignment: justify\n"); break;
327 default : p += sprintf(p, "Alignment: incorrect %d\n", pFmt->wAlignment); break;
330 else p += sprintf(p, "Alignment: N/A\n");
331 DUMP(PFM_TABSTOPS, "Tab Stops:", "%d", cTabCount);
332 if (pFmt->dwMask & PFM_TABSTOPS) {
334 p += sprintf(p, "\t");
335 for (i = 0; i < pFmt->cTabCount; i++) p += sprintf(p, "%x ", pFmt->rgxTabs[i]);
336 p += sprintf(p, "\n");
338 DUMP(PFM_SPACEBEFORE, "Space Before:", "%d", dySpaceBefore);
339 DUMP(PFM_SPACEAFTER, "Space After:", "%d", dySpaceAfter);
340 DUMP(PFM_LINESPACING, "Line spacing:", "%d", dyLineSpacing);
341 DUMP(PFM_STYLE, "Text style:", "%d", sStyle);
342 DUMP(PFM_LINESPACING, "Line spacing rule:", "%u", bLineSpacingRule);
343 /* bOutlineLevel should be 0 */
344 DUMP(PFM_SHADING, "Shading Weigth:", "%u", wShadingWeight);
345 DUMP(PFM_SHADING, "Shading Style:", "%u", wShadingStyle);
346 DUMP(PFM_NUMBERINGSTART, "Numbering Start:", "%u", wNumberingStart);
347 DUMP(PFM_NUMBERINGSTYLE, "Numbering Style:", "0x%x", wNumberingStyle);
348 DUMP(PFM_NUMBERINGTAB, "Numbering Tab:", "%u", wNumberingStyle);
349 DUMP(PFM_BORDER, "Border Space:", "%u", wBorderSpace);
350 DUMP(PFM_BORDER, "Border Width:", "%u", wBorderWidth);
351 DUMP(PFM_BORDER, "Borders:", "%u", wBorders);
357 void ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, const PARAFORMAT2 *pFmt)
360 assert(sizeof(*para->member.para.pFmt) == sizeof(PARAFORMAT2));
361 ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
363 copy = *para->member.para.pFmt;
365 #define COPY_FIELD(m, f) \
366 if (pFmt->dwMask & (m)) { \
367 para->member.para.pFmt->dwMask |= m; \
368 para->member.para.pFmt->f = pFmt->f; \
371 COPY_FIELD(PFM_NUMBERING, wNumbering);
372 #define EFFECTS_MASK (PFM_RTLPARA|PFM_KEEP|PFM_KEEPNEXT|PFM_PAGEBREAKBEFORE| \
373 PFM_NOLINENUMBER|PFM_NOWIDOWCONTROL|PFM_DONOTHYPHEN|PFM_SIDEBYSIDE| \
375 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
376 if (pFmt->dwMask & EFFECTS_MASK) {
377 para->member.para.pFmt->dwMask &= ~(pFmt->dwMask & EFFECTS_MASK);
378 para->member.para.pFmt->wEffects |= pFmt->wEffects & HIWORD(pFmt->dwMask);
382 COPY_FIELD(PFM_STARTINDENT, dxStartIndent);
383 if (pFmt->dwMask & PFM_OFFSETINDENT)
384 para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
385 COPY_FIELD(PFM_RIGHTINDENT, dxRightIndent);
386 COPY_FIELD(PFM_OFFSET, dxOffset);
387 COPY_FIELD(PFM_ALIGNMENT, wAlignment);
389 if (pFmt->dwMask & PFM_TABSTOPS)
391 para->member.para.pFmt->cTabCount = pFmt->cTabCount;
392 memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(LONG));
394 COPY_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
395 COPY_FIELD(PFM_SPACEAFTER, dySpaceAfter);
396 COPY_FIELD(PFM_LINESPACING, dyLineSpacing);
397 COPY_FIELD(PFM_STYLE, sStyle);
398 COPY_FIELD(PFM_LINESPACING, bLineSpacingRule);
399 COPY_FIELD(PFM_SHADING, wShadingWeight);
400 COPY_FIELD(PFM_SHADING, wShadingStyle);
401 COPY_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
402 COPY_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
403 COPY_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
404 COPY_FIELD(PFM_BORDER, wBorderSpace);
405 COPY_FIELD(PFM_BORDER, wBorderWidth);
406 COPY_FIELD(PFM_BORDER, wBorders);
408 para->member.para.pFmt->dwMask |= pFmt->dwMask;
411 if (memcmp(©, para->member.para.pFmt, sizeof(PARAFORMAT2)))
412 para->member.para.nFlags |= MEPF_REWRAP;
417 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
419 ME_Cursor *pEndCursor = &editor->pCursors[1];
421 *para = ME_GetParagraph(editor->pCursors[0].pRun);
422 *para_end = ME_GetParagraph(editor->pCursors[1].pRun);
423 if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
424 ME_DisplayItem *tmp = *para;
428 pEndCursor = &editor->pCursors[0];
431 /* selection consists of chars from nFrom up to nTo-1 */
432 if ((*para_end)->member.para.nCharOfs > (*para)->member.para.nCharOfs) {
433 if (!pEndCursor->nOffset) {
434 *para_end = ME_GetParagraph(ME_FindItemBack(pEndCursor->pRun, diRun));
440 void ME_SetSelectionParaFormat(ME_TextEditor *editor, const PARAFORMAT2 *pFmt)
442 ME_DisplayItem *para, *para_end;
444 ME_GetSelectionParas(editor, ¶, ¶_end);
447 ME_SetParaFormat(editor, para, pFmt);
448 if (para == para_end)
450 para = para->member.para.next_para;
454 void ME_GetParaFormat(ME_TextEditor *editor, const ME_DisplayItem *para, PARAFORMAT2 *pFmt)
456 if (pFmt->cbSize >= sizeof(PARAFORMAT2))
458 *pFmt = *para->member.para.pFmt;
461 CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
464 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
466 ME_DisplayItem *para, *para_end;
469 ME_GetSelectionParas(editor, ¶, ¶_end);
471 ME_GetParaFormat(editor, para, pFmt);
472 if (para == para_end) return;
475 ZeroMemory(&tmp, sizeof(tmp));
476 tmp.cbSize = sizeof(tmp);
477 ME_GetParaFormat(editor, para, &tmp);
479 #define CHECK_FIELD(m, f) \
480 if (pFmt->f != tmp.f) pFmt->dwMask &= ~(m);
482 CHECK_FIELD(PFM_NUMBERING, wNumbering);
483 /* para->member.para.pFmt->wEffects = pFmt->wEffects; */
484 assert(tmp.dwMask & PFM_ALIGNMENT);
485 CHECK_FIELD(PFM_NUMBERING, wNumbering);
486 assert(tmp.dwMask & PFM_STARTINDENT);
487 CHECK_FIELD(PFM_STARTINDENT, dxStartIndent);
488 assert(tmp.dwMask & PFM_RIGHTINDENT);
489 CHECK_FIELD(PFM_RIGHTINDENT, dxRightIndent);
490 assert(tmp.dwMask & PFM_OFFSET);
491 CHECK_FIELD(PFM_OFFSET, dxOffset);
492 CHECK_FIELD(PFM_ALIGNMENT, wAlignment);
494 assert(tmp.dwMask & PFM_TABSTOPS);
495 if (pFmt->dwMask & PFM_TABSTOPS) {
496 if (pFmt->cTabCount != tmp.cTabCount ||
497 memcmp(pFmt->rgxTabs, tmp.rgxTabs, tmp.cTabCount*sizeof(int)))
498 pFmt->dwMask &= ~PFM_TABSTOPS;
501 CHECK_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
502 CHECK_FIELD(PFM_SPACEAFTER, dySpaceAfter);
503 CHECK_FIELD(PFM_LINESPACING, dyLineSpacing);
504 CHECK_FIELD(PFM_STYLE, sStyle);
505 CHECK_FIELD(PFM_SPACEAFTER, bLineSpacingRule);
506 CHECK_FIELD(PFM_SHADING, wShadingWeight);
507 CHECK_FIELD(PFM_SHADING, wShadingStyle);
508 CHECK_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
509 CHECK_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
510 CHECK_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
511 CHECK_FIELD(PFM_BORDER, wBorderSpace);
512 CHECK_FIELD(PFM_BORDER, wBorderWidth);
513 CHECK_FIELD(PFM_BORDER, wBorders);
517 if (para == para_end)
519 para = para->member.para.next_para;