push 63c1876572cbb61a874995ad42ef27c14590d232
[wine/hacks.git] / dlls / riched20 / para.c
blob5289fdd5771677194ef7aae5b180d3802c794758
1 /*
2 * RichEdit - functions working on paragraphs of text (diParagraph).
3 *
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
20 */
22 #include "editor.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 static const WCHAR wszParagraphSign[] = {0xB6, 0};
28 void ME_MakeFirstParagraph(ME_TextEditor *editor)
30 ME_Context c;
31 CHARFORMAT2W cf;
32 LOGFONTW lf;
33 HFONT hf;
34 ME_TextBuffer *text = editor->pBuffer;
35 ME_DisplayItem *para = ME_MakeDI(diParagraph);
36 ME_DisplayItem *run;
37 ME_Style *style;
39 ME_InitContext(&c, editor, GetDC(editor->hWnd));
41 hf = (HFONT)GetStockObject(SYSTEM_FONT);
42 assert(hf);
43 GetObjectW(hf, sizeof(LOGFONTW), &lf);
44 ZeroMemory(&cf, sizeof(cf));
45 cf.cbSize = sizeof(cf);
46 cf.dwMask = CFM_BACKCOLOR|CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_CHARSET;
47 cf.dwMask |= CFM_ALLCAPS|CFM_BOLD|CFM_DISABLED|CFM_EMBOSS|CFM_HIDDEN;
48 cf.dwMask |= CFM_IMPRINT|CFM_ITALIC|CFM_LINK|CFM_OUTLINE|CFM_PROTECTED;
49 cf.dwMask |= CFM_REVISED|CFM_SHADOW|CFM_SMALLCAPS|CFM_STRIKEOUT;
50 cf.dwMask |= CFM_SUBSCRIPT|CFM_UNDERLINETYPE|CFM_WEIGHT;
52 cf.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
53 lstrcpyW(cf.szFaceName, lf.lfFaceName);
54 /* Convert system font height from logical units to twips for cf.yHeight */
55 cf.yHeight = (lf.lfHeight * 72 * 1440) / (c.dpi.cy * c.dpi.cy);
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 style = ME_MakeStyle(&cf);
65 text->pDefaultStyle = style;
67 run = ME_MakeRun(style, ME_MakeString(wszParagraphSign), MERF_ENDPARA);
68 run->member.run.nCharOfs = 0;
69 run->member.run.nCR = 1;
70 run->member.run.nLF = (editor->bEmulateVersion10) ? 1 : 0;
72 ME_InsertBefore(text->pLast, para);
73 ME_InsertBefore(text->pLast, run);
74 para->member.para.prev_para = text->pFirst;
75 para->member.para.next_para = text->pLast;
76 text->pFirst->member.para.next_para = para;
77 text->pLast->member.para.prev_para = para;
79 text->pLast->member.para.nCharOfs = 1;
81 ME_DestroyContext(&c, editor->hWnd);
84 void ME_MarkAllForWrapping(ME_TextEditor *editor)
86 ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
89 void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
91 while(first != last)
93 first->member.para.nFlags |= MEPF_REWRAP;
94 first = first->member.para.next_para;
98 void ME_MarkForPainting(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
100 while(first != last && first)
102 first->member.para.nFlags |= MEPF_REPAINT;
103 first = first->member.para.next_para;
107 /* split paragraph at the beginning of the run */
108 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run, ME_Style *style, int numCR, int numLF)
110 ME_DisplayItem *next_para = NULL;
111 ME_DisplayItem *run_para = NULL;
112 ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
113 ME_DisplayItem *end_run = ME_MakeRun(style,ME_MakeString(wszParagraphSign), MERF_ENDPARA);
114 ME_UndoItem *undo = NULL;
115 int ofs;
116 ME_DisplayItem *pp;
117 int end_len = numCR + numLF;
119 assert(run->type == diRun);
121 end_run->member.run.nCR = numCR;
122 end_run->member.run.nLF = numLF;
123 run_para = ME_GetParagraph(run);
124 assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
126 ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
127 next_para = run_para->member.para.next_para;
128 assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
130 undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
131 if (undo)
132 undo->nStart = run_para->member.para.nCharOfs + ofs;
134 /* the new paragraph will have a different starting offset, so let's update its runs */
135 pp = run;
136 while(pp->type == diRun) {
137 pp->member.run.nCharOfs -= ofs;
138 pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
140 new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
141 new_para->member.para.nCharOfs += end_len;
143 new_para->member.para.nFlags = MEPF_REWRAP; /* FIXME copy flags (if applicable) */
144 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
145 *new_para->member.para.pFmt = *run_para->member.para.pFmt;
147 /* insert paragraph into paragraph double linked list */
148 new_para->member.para.prev_para = run_para;
149 new_para->member.para.next_para = next_para;
150 run_para->member.para.next_para = new_para;
151 next_para->member.para.prev_para = new_para;
153 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
154 ME_InsertBefore(run, new_para);
155 ME_InsertBefore(new_para, end_run);
157 /* force rewrap of the */
158 run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
159 new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
161 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
162 ME_PropagateCharOffset(next_para, end_len);
163 editor->nParagraphs++;
165 return new_para;
168 /* join tp with tp->member.para.next_para, keeping tp's style; this
169 * is consistent with the original */
170 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp,
171 BOOL keepFirstParaFormat)
173 ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
174 int i, shift;
175 ME_UndoItem *undo = NULL;
176 int end_len;
178 assert(tp->type == diParagraph);
179 assert(tp->member.para.next_para);
180 assert(tp->member.para.next_para->type == diParagraph);
182 pNext = tp->member.para.next_para;
184 /* Need to locate end-of-paragraph run here, in order to know end_len */
185 pRun = ME_FindItemBack(pNext, diRunOrParagraph);
187 assert(pRun);
188 assert(pRun->type == diRun);
189 assert(pRun->member.run.nFlags & MERF_ENDPARA);
191 end_len = pRun->member.run.nCR + pRun->member.run.nLF;
194 /* null char format operation to store the original char format for the ENDPARA run */
195 CHARFORMAT2W fmt;
196 ME_InitCharFormat2W(&fmt);
197 ME_SetCharFormat(editor, pNext->member.para.nCharOfs - end_len, end_len, &fmt);
199 undo = ME_AddUndoItem(editor, diUndoSplitParagraph, pNext);
200 if (undo)
202 undo->nStart = pNext->member.para.nCharOfs - end_len;
203 undo->nCR = pRun->member.run.nCR;
204 undo->nLF = pRun->member.run.nLF;
206 if (!keepFirstParaFormat)
208 ME_AddUndoItem(editor, diUndoSetParagraphFormat, tp);
209 *tp->member.para.pFmt = *pNext->member.para.pFmt;
212 shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
214 pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
216 assert(pFirstRunInNext->type == diRun);
218 /* if some cursor points at end of paragraph, make it point to the first
219 run of the next joined paragraph */
220 for (i=0; i<editor->nCursors; i++) {
221 if (editor->pCursors[i].pRun == pRun) {
222 editor->pCursors[i].pRun = pFirstRunInNext;
223 editor->pCursors[i].nOffset = 0;
227 pTmp = pNext;
228 do {
229 pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
230 if (pTmp->type != diRun)
231 break;
232 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
233 pTmp->member.run.nCharOfs += shift;
234 } while(1);
236 ME_Remove(pRun);
237 ME_DestroyDisplayItem(pRun);
239 if (editor->pLastSelStartPara == pNext)
240 editor->pLastSelStartPara = tp;
241 if (editor->pLastSelEndPara == pNext)
242 editor->pLastSelEndPara = tp;
244 tp->member.para.next_para = pNext->member.para.next_para;
245 pNext->member.para.next_para->member.para.prev_para = tp;
246 ME_Remove(pNext);
247 ME_DestroyDisplayItem(pNext);
249 ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
251 ME_CheckCharOffsets(editor);
253 editor->nParagraphs--;
254 tp->member.para.nFlags |= MEPF_REWRAP;
255 return tp;
258 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
259 return ME_FindItemBackOrHere(item, diParagraph);
262 void ME_DumpParaStyleToBuf(const PARAFORMAT2 *pFmt, char buf[2048])
264 char *p;
265 p = buf;
267 #define DUMP(mask, name, fmt, field) \
268 if (pFmt->dwMask & (mask)) p += sprintf(p, "%-22s" fmt "\n", name, pFmt->field); \
269 else p += sprintf(p, "%-22sN/A\n", name);
271 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
272 #define DUMP_EFFECT(mask, name) \
273 p += sprintf(p, "%-22s%s\n", name, (pFmt->dwMask & (mask)) ? ((pFmt->wEffects & ((mask) >> 8)) ? "yes" : "no") : "N/A");
275 DUMP(PFM_NUMBERING, "Numbering:", "%u", wNumbering);
276 DUMP_EFFECT(PFM_DONOTHYPHEN, "Disable auto-hyphen:");
277 DUMP_EFFECT(PFM_KEEP, "No page break in para:");
278 DUMP_EFFECT(PFM_KEEPNEXT, "No page break in para & next:");
279 DUMP_EFFECT(PFM_NOLINENUMBER, "No line number:");
280 DUMP_EFFECT(PFM_NOWIDOWCONTROL, "No widow & orphan:");
281 DUMP_EFFECT(PFM_PAGEBREAKBEFORE, "Page break before:");
282 DUMP_EFFECT(PFM_RTLPARA, "RTL para:");
283 DUMP_EFFECT(PFM_SIDEBYSIDE, "Side by side:");
284 DUMP_EFFECT(PFM_TABLE, "Table:");
285 DUMP(PFM_OFFSETINDENT, "Offset indent:", "%d", dxStartIndent);
286 DUMP(PFM_STARTINDENT, "Start indent:", "%d", dxStartIndent);
287 DUMP(PFM_RIGHTINDENT, "Right indent:", "%d", dxRightIndent);
288 DUMP(PFM_OFFSET, "Offset:", "%d", dxOffset);
289 if (pFmt->dwMask & PFM_ALIGNMENT) {
290 switch (pFmt->wAlignment) {
291 case PFA_LEFT : p += sprintf(p, "Alignment: left\n"); break;
292 case PFA_RIGHT : p += sprintf(p, "Alignment: right\n"); break;
293 case PFA_CENTER : p += sprintf(p, "Alignment: center\n"); break;
294 case PFA_JUSTIFY: p += sprintf(p, "Alignment: justify\n"); break;
295 default : p += sprintf(p, "Alignment: incorrect %d\n", pFmt->wAlignment); break;
298 else p += sprintf(p, "Alignment: N/A\n");
299 DUMP(PFM_TABSTOPS, "Tab Stops:", "%d", cTabCount);
300 if (pFmt->dwMask & PFM_TABSTOPS) {
301 int i;
302 p += sprintf(p, "\t");
303 for (i = 0; i < pFmt->cTabCount; i++) p += sprintf(p, "%x ", pFmt->rgxTabs[i]);
304 p += sprintf(p, "\n");
306 DUMP(PFM_SPACEBEFORE, "Space Before:", "%d", dySpaceBefore);
307 DUMP(PFM_SPACEAFTER, "Space After:", "%d", dySpaceAfter);
308 DUMP(PFM_LINESPACING, "Line spacing:", "%d", dyLineSpacing);
309 DUMP(PFM_STYLE, "Text style:", "%d", sStyle);
310 DUMP(PFM_LINESPACING, "Line spacing rule:", "%u", bLineSpacingRule);
311 /* bOutlineLevel should be 0 */
312 DUMP(PFM_SHADING, "Shading Weigth:", "%u", wShadingWeight);
313 DUMP(PFM_SHADING, "Shading Style:", "%u", wShadingStyle);
314 DUMP(PFM_NUMBERINGSTART, "Numbering Start:", "%u", wNumberingStart);
315 DUMP(PFM_NUMBERINGSTYLE, "Numbering Style:", "0x%x", wNumberingStyle);
316 DUMP(PFM_NUMBERINGTAB, "Numbering Tab:", "%u", wNumberingStyle);
317 DUMP(PFM_BORDER, "Border Space:", "%u", wBorderSpace);
318 DUMP(PFM_BORDER, "Border Width:", "%u", wBorderWidth);
319 DUMP(PFM_BORDER, "Borders:", "%u", wBorders);
321 #undef DUMP
322 #undef DUMP_EFFECT
325 void ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, const PARAFORMAT2 *pFmt)
327 PARAFORMAT2 copy;
328 assert(sizeof(*para->member.para.pFmt) == sizeof(PARAFORMAT2));
329 ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
331 copy = *para->member.para.pFmt;
333 #define COPY_FIELD(m, f) \
334 if (pFmt->dwMask & (m)) { \
335 para->member.para.pFmt->dwMask |= m; \
336 para->member.para.pFmt->f = pFmt->f; \
339 COPY_FIELD(PFM_NUMBERING, wNumbering);
340 #define EFFECTS_MASK (PFM_RTLPARA|PFM_KEEP|PFM_KEEPNEXT|PFM_PAGEBREAKBEFORE| \
341 PFM_NOLINENUMBER|PFM_NOWIDOWCONTROL|PFM_DONOTHYPHEN|PFM_SIDEBYSIDE| \
342 PFM_TABLE)
343 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
344 if (pFmt->dwMask & EFFECTS_MASK) {
345 para->member.para.pFmt->dwMask |= pFmt->dwMask & EFFECTS_MASK;
346 para->member.para.pFmt->wEffects &= ~HIWORD(pFmt->dwMask);
347 para->member.para.pFmt->wEffects |= pFmt->wEffects & HIWORD(pFmt->dwMask);
349 #undef EFFECTS_MASK
351 COPY_FIELD(PFM_STARTINDENT, dxStartIndent);
352 if (pFmt->dwMask & PFM_OFFSETINDENT)
353 para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
354 COPY_FIELD(PFM_RIGHTINDENT, dxRightIndent);
355 COPY_FIELD(PFM_OFFSET, dxOffset);
356 COPY_FIELD(PFM_ALIGNMENT, wAlignment);
358 if (pFmt->dwMask & PFM_TABSTOPS)
360 para->member.para.pFmt->cTabCount = pFmt->cTabCount;
361 memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(LONG));
363 COPY_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
364 COPY_FIELD(PFM_SPACEAFTER, dySpaceAfter);
365 COPY_FIELD(PFM_LINESPACING, dyLineSpacing);
366 COPY_FIELD(PFM_STYLE, sStyle);
367 COPY_FIELD(PFM_LINESPACING, bLineSpacingRule);
368 COPY_FIELD(PFM_SHADING, wShadingWeight);
369 COPY_FIELD(PFM_SHADING, wShadingStyle);
370 COPY_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
371 COPY_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
372 COPY_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
373 COPY_FIELD(PFM_BORDER, wBorderSpace);
374 COPY_FIELD(PFM_BORDER, wBorderWidth);
375 COPY_FIELD(PFM_BORDER, wBorders);
377 para->member.para.pFmt->dwMask |= pFmt->dwMask;
378 #undef COPY_FIELD
380 if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
381 para->member.para.nFlags |= MEPF_REWRAP;
385 void
386 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
388 ME_Cursor *pEndCursor = &editor->pCursors[1];
390 *para = ME_GetParagraph(editor->pCursors[0].pRun);
391 *para_end = ME_GetParagraph(editor->pCursors[1].pRun);
392 if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
393 ME_DisplayItem *tmp = *para;
395 *para = *para_end;
396 *para_end = tmp;
397 pEndCursor = &editor->pCursors[0];
400 /* selection consists of chars from nFrom up to nTo-1 */
401 if ((*para_end)->member.para.nCharOfs > (*para)->member.para.nCharOfs) {
402 if (!pEndCursor->nOffset) {
403 *para_end = ME_GetParagraph(ME_FindItemBack(pEndCursor->pRun, diRun));
409 void ME_SetSelectionParaFormat(ME_TextEditor *editor, const PARAFORMAT2 *pFmt)
411 ME_DisplayItem *para, *para_end;
413 ME_GetSelectionParas(editor, &para, &para_end);
415 do {
416 ME_SetParaFormat(editor, para, pFmt);
417 if (para == para_end)
418 break;
419 para = para->member.para.next_para;
420 } while(1);
423 void ME_GetParaFormat(ME_TextEditor *editor, const ME_DisplayItem *para, PARAFORMAT2 *pFmt)
425 if (pFmt->cbSize >= sizeof(PARAFORMAT2))
427 *pFmt = *para->member.para.pFmt;
428 return;
430 CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
433 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
435 ME_DisplayItem *para, *para_end;
436 PARAFORMAT2 tmp;
438 ME_GetSelectionParas(editor, &para, &para_end);
440 ME_GetParaFormat(editor, para, pFmt);
441 if (para == para_end) return;
443 /* Invalidate values that change across the selected paragraphs. */
444 do {
445 ZeroMemory(&tmp, sizeof(tmp));
446 tmp.cbSize = sizeof(tmp);
447 ME_GetParaFormat(editor, para, &tmp);
449 #define CHECK_FIELD(m, f) \
450 if (pFmt->f != tmp.f) pFmt->dwMask &= ~(m);
452 pFmt->dwMask &= ~((pFmt->wEffects ^ tmp.wEffects) << 16);
453 CHECK_FIELD(PFM_NUMBERING, wNumbering);
454 assert(tmp.dwMask & PFM_ALIGNMENT);
455 CHECK_FIELD(PFM_NUMBERING, wNumbering);
456 assert(tmp.dwMask & PFM_STARTINDENT);
457 CHECK_FIELD(PFM_STARTINDENT, dxStartIndent);
458 assert(tmp.dwMask & PFM_RIGHTINDENT);
459 CHECK_FIELD(PFM_RIGHTINDENT, dxRightIndent);
460 assert(tmp.dwMask & PFM_OFFSET);
461 CHECK_FIELD(PFM_OFFSET, dxOffset);
462 CHECK_FIELD(PFM_ALIGNMENT, wAlignment);
464 assert(tmp.dwMask & PFM_TABSTOPS);
465 if (pFmt->dwMask & PFM_TABSTOPS) {
466 if (pFmt->cTabCount != tmp.cTabCount ||
467 memcmp(pFmt->rgxTabs, tmp.rgxTabs, tmp.cTabCount*sizeof(int)))
468 pFmt->dwMask &= ~PFM_TABSTOPS;
471 CHECK_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
472 CHECK_FIELD(PFM_SPACEAFTER, dySpaceAfter);
473 CHECK_FIELD(PFM_LINESPACING, dyLineSpacing);
474 CHECK_FIELD(PFM_STYLE, sStyle);
475 CHECK_FIELD(PFM_SPACEAFTER, bLineSpacingRule);
476 CHECK_FIELD(PFM_SHADING, wShadingWeight);
477 CHECK_FIELD(PFM_SHADING, wShadingStyle);
478 CHECK_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
479 CHECK_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
480 CHECK_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
481 CHECK_FIELD(PFM_BORDER, wBorderSpace);
482 CHECK_FIELD(PFM_BORDER, wBorderWidth);
483 CHECK_FIELD(PFM_BORDER, wBorders);
485 #undef CHECK_FIELD
487 if (para == para_end)
488 return;
489 para = para->member.para.next_para;
490 } while(1);
493 void ME_SetDefaultParaFormat(PARAFORMAT2 *pFmt)
495 ZeroMemory(pFmt, sizeof(PARAFORMAT2));
496 pFmt->cbSize = sizeof(PARAFORMAT2);
497 pFmt->dwMask = PFM_ALL2;
498 pFmt->wAlignment = PFA_LEFT;
499 pFmt->sStyle = -1;
500 pFmt->bOutlineLevel = TRUE;