push c669110d8ebd04a85775e86a8310fabd7dc4466e
[wine/hacks.git] / dlls / riched20 / para.c
blob0836390be2fc32987114bf6217bd771056cc6aee
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 void ME_MakeFirstParagraph(ME_TextEditor *editor)
28 ME_Context c;
29 CHARFORMAT2W cf;
30 LOGFONTW lf;
31 HFONT hf;
32 ME_TextBuffer *text = editor->pBuffer;
33 ME_DisplayItem *para = ME_MakeDI(diParagraph);
34 ME_DisplayItem *run;
35 ME_Style *style;
36 ME_String *eol_str;
37 WCHAR cr_lf[] = {'\r','\n',0};
39 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
41 hf = 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 eol_str = ME_MakeStringN(cr_lf, editor->bEmulateVersion10 ? 2 : 1);
68 run = ME_MakeRun(style, eol_str, MERF_ENDPARA);
69 run->member.run.nCharOfs = 0;
71 ME_InsertBefore(text->pLast, para);
72 ME_InsertBefore(text->pLast, run);
73 para->member.para.prev_para = text->pFirst;
74 para->member.para.next_para = text->pLast;
75 text->pFirst->member.para.next_para = para;
76 text->pLast->member.para.prev_para = para;
78 text->pLast->member.para.nCharOfs = editor->bEmulateVersion10 ? 2 : 1;
80 ME_DestroyContext(&c);
83 static void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
85 while(first != last)
87 first->member.para.nFlags |= MEPF_REWRAP;
88 first = first->member.para.next_para;
92 void ME_MarkAllForWrapping(ME_TextEditor *editor)
94 ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
97 void ME_MarkForPainting(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
99 while(first != last && first)
101 first->member.para.nFlags |= MEPF_REPAINT;
102 first = first->member.para.next_para;
106 static void ME_UpdateTableFlags(ME_DisplayItem *para)
108 para->member.para.pFmt->dwMask |= PFM_TABLE|PFM_TABLEROWDELIMITER;
109 if (para->member.para.pCell) {
110 para->member.para.nFlags |= MEPF_CELL;
111 } else {
112 para->member.para.nFlags &= ~MEPF_CELL;
114 if (para->member.para.nFlags & MEPF_ROWEND) {
115 para->member.para.pFmt->wEffects |= PFE_TABLEROWDELIMITER;
116 } else {
117 para->member.para.pFmt->wEffects &= ~PFE_TABLEROWDELIMITER;
119 if (para->member.para.nFlags & (MEPF_ROWSTART|MEPF_CELL|MEPF_ROWEND))
120 para->member.para.pFmt->wEffects |= PFE_TABLE;
121 else
122 para->member.para.pFmt->wEffects &= ~PFE_TABLE;
125 static BOOL ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, const PARAFORMAT2 *pFmt)
127 PARAFORMAT2 copy;
128 DWORD dwMask;
130 assert(para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
131 dwMask = pFmt->dwMask;
132 if (pFmt->cbSize < sizeof(PARAFORMAT))
133 return FALSE;
134 else if (pFmt->cbSize < sizeof(PARAFORMAT2))
135 dwMask &= PFM_ALL;
136 else
137 dwMask &= PFM_ALL2;
139 ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
141 copy = *para->member.para.pFmt;
143 #define COPY_FIELD(m, f) \
144 if (dwMask & (m)) { \
145 para->member.para.pFmt->dwMask |= m; \
146 para->member.para.pFmt->f = pFmt->f; \
149 COPY_FIELD(PFM_NUMBERING, wNumbering);
150 COPY_FIELD(PFM_STARTINDENT, dxStartIndent);
151 if (dwMask & PFM_OFFSETINDENT)
152 para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
153 COPY_FIELD(PFM_RIGHTINDENT, dxRightIndent);
154 COPY_FIELD(PFM_OFFSET, dxOffset);
155 COPY_FIELD(PFM_ALIGNMENT, wAlignment);
156 if (dwMask & PFM_TABSTOPS)
158 para->member.para.pFmt->cTabCount = pFmt->cTabCount;
159 memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(LONG));
162 if (dwMask & (PFM_ALL2 & ~PFM_ALL))
164 /* PARAFORMAT2 fields */
166 #define EFFECTS_MASK (PFM_RTLPARA|PFM_KEEP|PFM_KEEPNEXT|PFM_PAGEBREAKBEFORE| \
167 PFM_NOLINENUMBER|PFM_NOWIDOWCONTROL|PFM_DONOTHYPHEN|PFM_SIDEBYSIDE| \
168 PFM_TABLE)
169 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
170 if (dwMask & EFFECTS_MASK) {
171 para->member.para.pFmt->dwMask |= dwMask & EFFECTS_MASK;
172 para->member.para.pFmt->wEffects &= ~HIWORD(dwMask);
173 para->member.para.pFmt->wEffects |= pFmt->wEffects & HIWORD(dwMask);
175 #undef EFFECTS_MASK
177 COPY_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
178 COPY_FIELD(PFM_SPACEAFTER, dySpaceAfter);
179 COPY_FIELD(PFM_LINESPACING, dyLineSpacing);
180 COPY_FIELD(PFM_STYLE, sStyle);
181 COPY_FIELD(PFM_LINESPACING, bLineSpacingRule);
182 COPY_FIELD(PFM_SHADING, wShadingWeight);
183 COPY_FIELD(PFM_SHADING, wShadingStyle);
184 COPY_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
185 COPY_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
186 COPY_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
187 COPY_FIELD(PFM_BORDER, wBorderSpace);
188 COPY_FIELD(PFM_BORDER, wBorderWidth);
189 COPY_FIELD(PFM_BORDER, wBorders);
192 para->member.para.pFmt->dwMask |= dwMask;
193 #undef COPY_FIELD
195 if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
196 para->member.para.nFlags |= MEPF_REWRAP;
198 return TRUE;
201 /* split paragraph at the beginning of the run */
202 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run,
203 ME_Style *style, ME_String *eol_str,
204 int paraFlags)
206 ME_DisplayItem *next_para = NULL;
207 ME_DisplayItem *run_para = NULL;
208 ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
209 ME_DisplayItem *end_run;
210 ME_UndoItem *undo = NULL;
211 int ofs;
212 ME_DisplayItem *pp;
213 int run_flags = MERF_ENDPARA;
215 if (!editor->bEmulateVersion10) { /* v4.1 */
216 /* At most 1 of MEPF_CELL, MEPF_ROWSTART, or MEPF_ROWEND should be set. */
217 assert(!(paraFlags & ~(MEPF_CELL|MEPF_ROWSTART|MEPF_ROWEND)));
218 assert(!(paraFlags & (paraFlags-1)));
219 if (paraFlags == MEPF_CELL)
220 run_flags |= MERF_ENDCELL;
221 else if (paraFlags == MEPF_ROWSTART)
222 run_flags |= MERF_TABLESTART|MERF_HIDDEN;
223 } else { /* v1.0 - v3.0 */
224 assert(!(paraFlags & (MEPF_CELL|MEPF_ROWSTART|MEPF_ROWEND)));
226 end_run = ME_MakeRun(style, eol_str, run_flags);
228 assert(run->type == diRun);
229 run_para = ME_GetParagraph(run);
230 assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
232 ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
233 next_para = run_para->member.para.next_para;
234 assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
236 undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
237 if (undo)
238 undo->nStart = run_para->member.para.nCharOfs + ofs;
240 /* the new paragraph will have a different starting offset, so let's update its runs */
241 pp = run;
242 while(pp->type == diRun) {
243 pp->member.run.nCharOfs -= ofs;
244 pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
246 new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
247 new_para->member.para.nCharOfs += eol_str->nLen;
248 new_para->member.para.nFlags = MEPF_REWRAP;
250 /* FIXME initialize format style and call ME_SetParaFormat blah blah */
251 *new_para->member.para.pFmt = *run_para->member.para.pFmt;
252 new_para->member.para.border = run_para->member.para.border;
254 /* insert paragraph into paragraph double linked list */
255 new_para->member.para.prev_para = run_para;
256 new_para->member.para.next_para = next_para;
257 run_para->member.para.next_para = new_para;
258 next_para->member.para.prev_para = new_para;
260 /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
261 ME_InsertBefore(run, new_para);
262 ME_InsertBefore(new_para, end_run);
264 if (!editor->bEmulateVersion10) { /* v4.1 */
265 if (paraFlags & (MEPF_ROWSTART|MEPF_CELL))
267 ME_DisplayItem *cell = ME_MakeDI(diCell);
268 ME_InsertBefore(new_para, cell);
269 new_para->member.para.pCell = cell;
270 cell->member.cell.next_cell = NULL;
271 if (paraFlags & MEPF_ROWSTART)
273 run_para->member.para.nFlags |= MEPF_ROWSTART;
274 cell->member.cell.prev_cell = NULL;
275 cell->member.cell.parent_cell = run_para->member.para.pCell;
276 if (run_para->member.para.pCell)
277 cell->member.cell.nNestingLevel = run_para->member.para.pCell->member.cell.nNestingLevel + 1;
278 else
279 cell->member.cell.nNestingLevel = 1;
280 } else {
281 cell->member.cell.prev_cell = run_para->member.para.pCell;
282 assert(cell->member.cell.prev_cell);
283 cell->member.cell.prev_cell->member.cell.next_cell = cell;
284 assert(run_para->member.para.nFlags & MEPF_CELL);
285 assert(!(run_para->member.para.nFlags & MEPF_ROWSTART));
286 cell->member.cell.nNestingLevel = cell->member.cell.prev_cell->member.cell.nNestingLevel;
287 cell->member.cell.parent_cell = cell->member.cell.prev_cell->member.cell.parent_cell;
289 } else if (paraFlags & MEPF_ROWEND) {
290 run_para->member.para.nFlags |= MEPF_ROWEND;
291 run_para->member.para.pCell = run_para->member.para.pCell->member.cell.parent_cell;
292 new_para->member.para.pCell = run_para->member.para.pCell;
293 assert(run_para->member.para.prev_para->member.para.nFlags & MEPF_CELL);
294 assert(!(run_para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART));
295 if (new_para->member.para.pCell != new_para->member.para.next_para->member.para.pCell
296 && new_para->member.para.next_para->member.para.pCell
297 && !new_para->member.para.next_para->member.para.pCell->member.cell.prev_cell)
299 /* Row starts just after the row that was ended. */
300 new_para->member.para.nFlags |= MEPF_ROWSTART;
302 } else {
303 new_para->member.para.pCell = run_para->member.para.pCell;
305 ME_UpdateTableFlags(run_para);
306 ME_UpdateTableFlags(new_para);
309 /* force rewrap of the */
310 run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
311 new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
313 /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
314 ME_PropagateCharOffset(next_para, eol_str->nLen);
315 editor->nParagraphs++;
317 return new_para;
320 /* join tp with tp->member.para.next_para, keeping tp's style; this
321 * is consistent with the original */
322 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp,
323 BOOL keepFirstParaFormat)
325 ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
326 int i, shift;
327 ME_UndoItem *undo = NULL;
328 int end_len;
330 assert(tp->type == diParagraph);
331 assert(tp->member.para.next_para);
332 assert(tp->member.para.next_para->type == diParagraph);
334 pNext = tp->member.para.next_para;
336 /* Need to locate end-of-paragraph run here, in order to know end_len */
337 pRun = ME_FindItemBack(pNext, diRunOrParagraph);
339 assert(pRun);
340 assert(pRun->type == diRun);
341 assert(pRun->member.run.nFlags & MERF_ENDPARA);
343 end_len = pRun->member.run.strText->nLen;
346 /* null char format operation to store the original char format for the ENDPARA run */
347 CHARFORMAT2W fmt;
348 ME_InitCharFormat2W(&fmt);
349 ME_SetCharFormat(editor, pNext->member.para.nCharOfs - end_len, end_len, &fmt);
351 undo = ME_AddUndoItem(editor, diUndoSplitParagraph, pNext);
352 if (undo)
354 undo->nStart = pNext->member.para.nCharOfs - end_len;
355 undo->eol_str = pRun->member.run.strText;
356 pRun->member.run.strText = NULL; /* Avoid freeing the string */
358 if (!keepFirstParaFormat)
360 ME_AddUndoItem(editor, diUndoSetParagraphFormat, tp);
361 *tp->member.para.pFmt = *pNext->member.para.pFmt;
362 tp->member.para.border = pNext->member.para.border;
365 if (!editor->bEmulateVersion10) { /* v4.1 */
366 /* Table cell/row properties are always moved over from the removed para. */
367 tp->member.para.nFlags = pNext->member.para.nFlags;
368 tp->member.para.pCell = pNext->member.para.pCell;
370 /* Remove cell boundary if it is between the end paragraph run and the next
371 * paragraph display item. */
372 pTmp = pRun->next;
373 while (pTmp != pNext) {
374 if (pTmp->type == diCell)
376 ME_Cell *pCell = &pTmp->member.cell;
377 if (undo)
379 assert(!(undo->di.member.para.nFlags & MEPF_ROWEND));
380 if (!(undo->di.member.para.nFlags & MEPF_ROWSTART))
381 undo->di.member.para.nFlags |= MEPF_CELL;
382 undo->di.member.para.pCell = ALLOC_OBJ(ME_DisplayItem);
383 *undo->di.member.para.pCell = *pTmp;
384 undo->di.member.para.pCell->next = NULL;
385 undo->di.member.para.pCell->prev = NULL;
386 undo->di.member.para.pCell->member.cell.next_cell = NULL;
387 undo->di.member.para.pCell->member.cell.prev_cell = NULL;
389 ME_Remove(pTmp);
390 if (pCell->prev_cell)
391 pCell->prev_cell->member.cell.next_cell = pCell->next_cell;
392 if (pCell->next_cell)
393 pCell->next_cell->member.cell.prev_cell = pCell->prev_cell;
394 ME_DestroyDisplayItem(pTmp);
395 break;
397 pTmp = pTmp->next;
401 shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
403 pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
405 assert(pFirstRunInNext->type == diRun);
407 /* if some cursor points at end of paragraph, make it point to the first
408 run of the next joined paragraph */
409 for (i=0; i<editor->nCursors; i++) {
410 if (editor->pCursors[i].pRun == pRun) {
411 editor->pCursors[i].pRun = pFirstRunInNext;
412 editor->pCursors[i].nOffset = 0;
416 pTmp = pNext;
417 do {
418 pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
419 if (pTmp->type != diRun)
420 break;
421 TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
422 pTmp->member.run.nCharOfs += shift;
423 } while(1);
425 ME_Remove(pRun);
426 ME_DestroyDisplayItem(pRun);
428 if (editor->pLastSelStartPara == pNext)
429 editor->pLastSelStartPara = tp;
430 if (editor->pLastSelEndPara == pNext)
431 editor->pLastSelEndPara = tp;
433 tp->member.para.next_para = pNext->member.para.next_para;
434 pNext->member.para.next_para->member.para.prev_para = tp;
435 ME_Remove(pNext);
436 ME_DestroyDisplayItem(pNext);
438 ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
440 ME_CheckCharOffsets(editor);
442 editor->nParagraphs--;
443 tp->member.para.nFlags |= MEPF_REWRAP;
444 return tp;
447 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
448 return ME_FindItemBackOrHere(item, diParagraph);
451 void ME_DumpParaStyleToBuf(const PARAFORMAT2 *pFmt, char buf[2048])
453 char *p;
454 p = buf;
456 #define DUMP(mask, name, fmt, field) \
457 if (pFmt->dwMask & (mask)) p += sprintf(p, "%-22s" fmt "\n", name, pFmt->field); \
458 else p += sprintf(p, "%-22sN/A\n", name);
460 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
461 #define DUMP_EFFECT(mask, name) \
462 p += sprintf(p, "%-22s%s\n", name, (pFmt->dwMask & (mask)) ? ((pFmt->wEffects & ((mask) >> 8)) ? "yes" : "no") : "N/A");
464 DUMP(PFM_NUMBERING, "Numbering:", "%u", wNumbering);
465 DUMP_EFFECT(PFM_DONOTHYPHEN, "Disable auto-hyphen:");
466 DUMP_EFFECT(PFM_KEEP, "No page break in para:");
467 DUMP_EFFECT(PFM_KEEPNEXT, "No page break in para & next:");
468 DUMP_EFFECT(PFM_NOLINENUMBER, "No line number:");
469 DUMP_EFFECT(PFM_NOWIDOWCONTROL, "No widow & orphan:");
470 DUMP_EFFECT(PFM_PAGEBREAKBEFORE, "Page break before:");
471 DUMP_EFFECT(PFM_RTLPARA, "RTL para:");
472 DUMP_EFFECT(PFM_SIDEBYSIDE, "Side by side:");
473 DUMP_EFFECT(PFM_TABLE, "Table:");
474 DUMP(PFM_OFFSETINDENT, "Offset indent:", "%d", dxStartIndent);
475 DUMP(PFM_STARTINDENT, "Start indent:", "%d", dxStartIndent);
476 DUMP(PFM_RIGHTINDENT, "Right indent:", "%d", dxRightIndent);
477 DUMP(PFM_OFFSET, "Offset:", "%d", dxOffset);
478 if (pFmt->dwMask & PFM_ALIGNMENT) {
479 switch (pFmt->wAlignment) {
480 case PFA_LEFT : p += sprintf(p, "Alignment: left\n"); break;
481 case PFA_RIGHT : p += sprintf(p, "Alignment: right\n"); break;
482 case PFA_CENTER : p += sprintf(p, "Alignment: center\n"); break;
483 case PFA_JUSTIFY: p += sprintf(p, "Alignment: justify\n"); break;
484 default : p += sprintf(p, "Alignment: incorrect %d\n", pFmt->wAlignment); break;
487 else p += sprintf(p, "Alignment: N/A\n");
488 DUMP(PFM_TABSTOPS, "Tab Stops:", "%d", cTabCount);
489 if (pFmt->dwMask & PFM_TABSTOPS) {
490 int i;
491 p += sprintf(p, "\t");
492 for (i = 0; i < pFmt->cTabCount; i++) p += sprintf(p, "%x ", pFmt->rgxTabs[i]);
493 p += sprintf(p, "\n");
495 DUMP(PFM_SPACEBEFORE, "Space Before:", "%d", dySpaceBefore);
496 DUMP(PFM_SPACEAFTER, "Space After:", "%d", dySpaceAfter);
497 DUMP(PFM_LINESPACING, "Line spacing:", "%d", dyLineSpacing);
498 DUMP(PFM_STYLE, "Text style:", "%d", sStyle);
499 DUMP(PFM_LINESPACING, "Line spacing rule:", "%u", bLineSpacingRule);
500 /* bOutlineLevel should be 0 */
501 DUMP(PFM_SHADING, "Shading Weigth:", "%u", wShadingWeight);
502 DUMP(PFM_SHADING, "Shading Style:", "%u", wShadingStyle);
503 DUMP(PFM_NUMBERINGSTART, "Numbering Start:", "%u", wNumberingStart);
504 DUMP(PFM_NUMBERINGSTYLE, "Numbering Style:", "0x%x", wNumberingStyle);
505 DUMP(PFM_NUMBERINGTAB, "Numbering Tab:", "%u", wNumberingStyle);
506 DUMP(PFM_BORDER, "Border Space:", "%u", wBorderSpace);
507 DUMP(PFM_BORDER, "Border Width:", "%u", wBorderWidth);
508 DUMP(PFM_BORDER, "Borders:", "%u", wBorders);
510 #undef DUMP
511 #undef DUMP_EFFECT
514 void
515 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
517 ME_Cursor *pEndCursor = &editor->pCursors[1];
519 *para = ME_GetParagraph(editor->pCursors[0].pRun);
520 *para_end = ME_GetParagraph(editor->pCursors[1].pRun);
521 if (*para == *para_end)
522 return;
524 if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
525 ME_DisplayItem *tmp = *para;
527 *para = *para_end;
528 *para_end = tmp;
529 pEndCursor = &editor->pCursors[0];
532 /* The paragraph at the end of a non-empty selection isn't included
533 * if the selection ends at the start of the paragraph. */
534 if (!pEndCursor->pRun->member.run.nCharOfs && !pEndCursor->nOffset)
535 *para_end = (*para_end)->member.para.prev_para;
539 BOOL ME_SetSelectionParaFormat(ME_TextEditor *editor, const PARAFORMAT2 *pFmt)
541 ME_DisplayItem *para, *para_end;
543 ME_GetSelectionParas(editor, &para, &para_end);
545 do {
546 ME_SetParaFormat(editor, para, pFmt);
547 if (para == para_end)
548 break;
549 para = para->member.para.next_para;
550 } while(1);
552 return TRUE;
555 static void ME_GetParaFormat(ME_TextEditor *editor,
556 const ME_DisplayItem *para,
557 PARAFORMAT2 *pFmt)
559 UINT cbSize = pFmt->cbSize;
560 if (pFmt->cbSize >= sizeof(PARAFORMAT2)) {
561 *pFmt = *para->member.para.pFmt;
562 } else {
563 CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
564 pFmt->dwMask &= PFM_ALL;
566 pFmt->cbSize = cbSize;
569 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
571 ME_DisplayItem *para, *para_end;
572 PARAFORMAT2 *curFmt;
574 if (pFmt->cbSize < sizeof(PARAFORMAT)) {
575 pFmt->dwMask = 0;
576 return;
579 ME_GetSelectionParas(editor, &para, &para_end);
581 ME_GetParaFormat(editor, para, pFmt);
583 /* Invalidate values that change across the selected paragraphs. */
584 while (para != para_end)
586 para = para->member.para.next_para;
587 curFmt = para->member.para.pFmt;
589 #define CHECK_FIELD(m, f) \
590 if (pFmt->f != curFmt->f) pFmt->dwMask &= ~(m);
592 CHECK_FIELD(PFM_NUMBERING, wNumbering);
593 CHECK_FIELD(PFM_STARTINDENT, dxStartIndent);
594 CHECK_FIELD(PFM_RIGHTINDENT, dxRightIndent);
595 CHECK_FIELD(PFM_OFFSET, dxOffset);
596 CHECK_FIELD(PFM_ALIGNMENT, wAlignment);
597 if (pFmt->dwMask & PFM_TABSTOPS) {
598 if (pFmt->cTabCount != para->member.para.pFmt->cTabCount ||
599 memcmp(pFmt->rgxTabs, curFmt->rgxTabs, curFmt->cTabCount*sizeof(int)))
600 pFmt->dwMask &= ~PFM_TABSTOPS;
603 if (pFmt->dwMask >= sizeof(PARAFORMAT2))
605 pFmt->dwMask &= ~((pFmt->wEffects ^ curFmt->wEffects) << 16);
606 CHECK_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
607 CHECK_FIELD(PFM_SPACEAFTER, dySpaceAfter);
608 CHECK_FIELD(PFM_LINESPACING, dyLineSpacing);
609 CHECK_FIELD(PFM_STYLE, sStyle);
610 CHECK_FIELD(PFM_SPACEAFTER, bLineSpacingRule);
611 CHECK_FIELD(PFM_SHADING, wShadingWeight);
612 CHECK_FIELD(PFM_SHADING, wShadingStyle);
613 CHECK_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
614 CHECK_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
615 CHECK_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
616 CHECK_FIELD(PFM_BORDER, wBorderSpace);
617 CHECK_FIELD(PFM_BORDER, wBorderWidth);
618 CHECK_FIELD(PFM_BORDER, wBorders);
620 #undef CHECK_FIELD
624 void ME_SetDefaultParaFormat(PARAFORMAT2 *pFmt)
626 ZeroMemory(pFmt, sizeof(PARAFORMAT2));
627 pFmt->cbSize = sizeof(PARAFORMAT2);
628 pFmt->dwMask = PFM_ALL2;
629 pFmt->wAlignment = PFA_LEFT;
630 pFmt->sStyle = -1;
631 pFmt->bOutlineLevel = TRUE;