riched20: Use correct flag name (Coverity).
[wine] / dlls / riched20 / para.c
1 /*
2  * RichEdit - functions working on paragraphs of text (diParagraph).
3  * 
4  * Copyright 2004 by Krzysztof Foltman
5  * Copyright 2006 by Phil Krylov
6  *
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.
11  *
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.
16  *
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  */ 
21
22 #include "editor.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25
26 void ME_MakeFirstParagraph(ME_TextEditor *editor)
27 {
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};
38
39   ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
40
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;
51   
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;
63
64   style = ME_MakeStyle(&cf);
65   text->pDefaultStyle = style;
66
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;
70
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;
77
78   text->pLast->member.para.nCharOfs = editor->bEmulateVersion10 ? 2 : 1;
79
80   ME_DestroyContext(&c);
81 }
82
83 static void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
84 {
85   while(first != last)
86   {
87     first->member.para.nFlags |= MEPF_REWRAP;
88     first = first->member.para.next_para;
89   }
90 }
91
92 void ME_MarkAllForWrapping(ME_TextEditor *editor)
93 {
94   ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
95 }
96
97 void ME_MarkForPainting(ME_TextEditor *editor, ME_DisplayItem *first, const ME_DisplayItem *last)
98 {
99   while(first != last && first)
100   {
101     first->member.para.nFlags |= MEPF_REPAINT;
102     first = first->member.para.next_para;
103   }
104 }
105
106 static void ME_UpdateTableFlags(ME_DisplayItem *para)
107 {
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;
113   }
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;
118   }
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;
123 }
124
125 static BOOL ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, const PARAFORMAT2 *pFmt)
126 {
127   PARAFORMAT2 copy;
128   DWORD dwMask;
129
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;
138
139   ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
140
141   copy = *para->member.para.pFmt;
142
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;        \
147   }
148
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)
157   {
158     para->member.para.pFmt->cTabCount = pFmt->cTabCount;
159     memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(LONG));
160   }
161
162   if (dwMask & (PFM_ALL2 & ~PFM_ALL))
163   {
164     /* PARAFORMAT2 fields */
165
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);
174     }
175 #undef EFFECTS_MASK
176
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);
190   }
191
192   para->member.para.pFmt->dwMask |= dwMask;
193 #undef COPY_FIELD
194
195   if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
196     para->member.para.nFlags |= MEPF_REWRAP;
197
198   return TRUE;
199 }
200
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)
205 {
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, i;
212   ME_DisplayItem *pp;
213   int run_flags = MERF_ENDPARA;
214
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)));
225   }
226   end_run = ME_MakeRun(style, eol_str, run_flags);
227
228   assert(run->type == diRun);
229   run_para = ME_GetParagraph(run);
230   assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
231
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));
235   
236   undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
237   if (undo)
238     undo->nStart = run_para->member.para.nCharOfs + ofs;
239
240   /* Update selection cursors to point to the correct paragraph. */
241   for (i = 0; i < editor->nCursors; i++) {
242     if (editor->pCursors[i].pPara == run_para &&
243         run->member.run.nCharOfs <= editor->pCursors[i].pRun->member.run.nCharOfs)
244     {
245       editor->pCursors[i].pPara = new_para;
246     }
247   }
248
249   /* the new paragraph will have a different starting offset, so let's update its runs */
250   pp = run;
251   while(pp->type == diRun) {
252     pp->member.run.nCharOfs -= ofs;
253     pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
254   }
255   new_para->member.para.nCharOfs = run_para->member.para.nCharOfs + ofs;
256   new_para->member.para.nCharOfs += eol_str->nLen;
257   new_para->member.para.nFlags = MEPF_REWRAP;
258
259   /* FIXME initialize format style and call ME_SetParaFormat blah blah */
260   *new_para->member.para.pFmt = *run_para->member.para.pFmt;
261   new_para->member.para.border = run_para->member.para.border;
262
263   /* insert paragraph into paragraph double linked list */
264   new_para->member.para.prev_para = run_para;
265   new_para->member.para.next_para = next_para;
266   run_para->member.para.next_para = new_para;
267   next_para->member.para.prev_para = new_para;
268
269   /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
270   ME_InsertBefore(run, new_para);
271   ME_InsertBefore(new_para, end_run);
272
273   if (!editor->bEmulateVersion10) { /* v4.1 */
274     if (paraFlags & (MEPF_ROWSTART|MEPF_CELL))
275     {
276       ME_DisplayItem *cell = ME_MakeDI(diCell);
277       ME_InsertBefore(new_para, cell);
278       new_para->member.para.pCell = cell;
279       cell->member.cell.next_cell = NULL;
280       if (paraFlags & MEPF_ROWSTART)
281       {
282         run_para->member.para.nFlags |= MEPF_ROWSTART;
283         cell->member.cell.prev_cell = NULL;
284         cell->member.cell.parent_cell = run_para->member.para.pCell;
285         if (run_para->member.para.pCell)
286           cell->member.cell.nNestingLevel = run_para->member.para.pCell->member.cell.nNestingLevel + 1;
287         else
288           cell->member.cell.nNestingLevel = 1;
289       } else {
290         cell->member.cell.prev_cell = run_para->member.para.pCell;
291         assert(cell->member.cell.prev_cell);
292         cell->member.cell.prev_cell->member.cell.next_cell = cell;
293         assert(run_para->member.para.nFlags & MEPF_CELL);
294         assert(!(run_para->member.para.nFlags & MEPF_ROWSTART));
295         cell->member.cell.nNestingLevel = cell->member.cell.prev_cell->member.cell.nNestingLevel;
296         cell->member.cell.parent_cell = cell->member.cell.prev_cell->member.cell.parent_cell;
297       }
298     } else if (paraFlags & MEPF_ROWEND) {
299       run_para->member.para.nFlags |= MEPF_ROWEND;
300       run_para->member.para.pCell = run_para->member.para.pCell->member.cell.parent_cell;
301       new_para->member.para.pCell = run_para->member.para.pCell;
302       assert(run_para->member.para.prev_para->member.para.nFlags & MEPF_CELL);
303       assert(!(run_para->member.para.prev_para->member.para.nFlags & MEPF_ROWSTART));
304       if (new_para->member.para.pCell != new_para->member.para.next_para->member.para.pCell
305           && new_para->member.para.next_para->member.para.pCell
306           && !new_para->member.para.next_para->member.para.pCell->member.cell.prev_cell)
307       {
308         /* Row starts just after the row that was ended. */
309         new_para->member.para.nFlags |= MEPF_ROWSTART;
310       }
311     } else {
312       new_para->member.para.pCell = run_para->member.para.pCell;
313     }
314     ME_UpdateTableFlags(run_para);
315     ME_UpdateTableFlags(new_para);
316   }
317
318   /* force rewrap of the */
319   run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
320   new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
321
322   /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
323   ME_PropagateCharOffset(next_para, eol_str->nLen);
324   editor->nParagraphs++;
325
326   return new_para;
327 }
328
329 /* join tp with tp->member.para.next_para, keeping tp's style; this
330  * is consistent with the original */
331 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp,
332                                   BOOL keepFirstParaFormat)
333 {
334   ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
335   int i, shift;
336   ME_UndoItem *undo = NULL;
337   int end_len;
338   CHARFORMAT2W fmt;
339   ME_Cursor startCur, endCur;
340
341   assert(tp->type == diParagraph);
342   assert(tp->member.para.next_para);
343   assert(tp->member.para.next_para->type == diParagraph);
344
345   pNext = tp->member.para.next_para;
346
347   /* Need to locate end-of-paragraph run here, in order to know end_len */
348   pRun = ME_FindItemBack(pNext, diRunOrParagraph);
349
350   assert(pRun);
351   assert(pRun->type == diRun);
352   assert(pRun->member.run.nFlags & MERF_ENDPARA);
353
354   end_len = pRun->member.run.strText->nLen;
355
356   /* null char format operation to store the original char format for the ENDPARA run */
357   ME_InitCharFormat2W(&fmt);
358   endCur.pPara = pNext;
359   endCur.pRun = ME_FindItemFwd(pNext, diRun);
360   endCur.nOffset = 0;
361   startCur = endCur;
362   ME_PrevRun(&startCur.pPara, &startCur.pRun);
363   ME_SetCharFormat(editor, &startCur, &endCur, &fmt);
364
365   undo = ME_AddUndoItem(editor, diUndoSplitParagraph, pNext);
366   if (undo)
367   {
368     undo->nStart = pNext->member.para.nCharOfs - end_len;
369     undo->eol_str = pRun->member.run.strText;
370     pRun->member.run.strText = NULL; /* Avoid freeing the string */
371   }
372   if (!keepFirstParaFormat)
373   {
374     ME_AddUndoItem(editor, diUndoSetParagraphFormat, tp);
375     *tp->member.para.pFmt = *pNext->member.para.pFmt;
376     tp->member.para.border = pNext->member.para.border;
377   }
378
379   if (!editor->bEmulateVersion10) { /* v4.1 */
380     /* Table cell/row properties are always moved over from the removed para. */
381     tp->member.para.nFlags = pNext->member.para.nFlags;
382     tp->member.para.pCell = pNext->member.para.pCell;
383
384     /* Remove cell boundary if it is between the end paragraph run and the next
385      * paragraph display item. */
386     pTmp = pRun->next;
387     while (pTmp != pNext) {
388       if (pTmp->type == diCell)
389       {
390         ME_Cell *pCell = &pTmp->member.cell;
391         if (undo)
392         {
393           assert(!(undo->di.member.para.nFlags & MEPF_ROWEND));
394           if (!(undo->di.member.para.nFlags & MEPF_ROWSTART))
395             undo->di.member.para.nFlags |= MEPF_CELL;
396           undo->di.member.para.pCell = ALLOC_OBJ(ME_DisplayItem);
397           *undo->di.member.para.pCell = *pTmp;
398           undo->di.member.para.pCell->next = NULL;
399           undo->di.member.para.pCell->prev = NULL;
400           undo->di.member.para.pCell->member.cell.next_cell = NULL;
401           undo->di.member.para.pCell->member.cell.prev_cell = NULL;
402         }
403         ME_Remove(pTmp);
404         if (pCell->prev_cell)
405           pCell->prev_cell->member.cell.next_cell = pCell->next_cell;
406         if (pCell->next_cell)
407           pCell->next_cell->member.cell.prev_cell = pCell->prev_cell;
408         ME_DestroyDisplayItem(pTmp);
409         break;
410       }
411       pTmp = pTmp->next;
412     }
413   }
414
415   shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
416
417   pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
418
419   assert(pFirstRunInNext->type == diRun);
420
421   /* Update selection cursors so they don't point to the removed end
422    * paragraph run, and point to the correct paragraph. */
423   for (i=0; i < editor->nCursors; i++) {
424     if (editor->pCursors[i].pRun == pRun) {
425       editor->pCursors[i].pRun = pFirstRunInNext;
426       editor->pCursors[i].nOffset = 0;
427     } else if (editor->pCursors[i].pPara == pNext) {
428       editor->pCursors[i].pPara = tp;
429     }
430   }
431
432   pTmp = pNext;
433   do {
434     pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
435     if (pTmp->type != diRun)
436       break;
437     TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
438     pTmp->member.run.nCharOfs += shift;
439   } while(1);
440
441   ME_Remove(pRun);
442   ME_DestroyDisplayItem(pRun);
443
444   if (editor->pLastSelStartPara == pNext)
445     editor->pLastSelStartPara = tp;
446   if (editor->pLastSelEndPara == pNext)
447     editor->pLastSelEndPara = tp;
448
449   tp->member.para.next_para = pNext->member.para.next_para;
450   pNext->member.para.next_para->member.para.prev_para = tp;
451   ME_Remove(pNext);
452   ME_DestroyDisplayItem(pNext);
453
454   ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
455
456   ME_CheckCharOffsets(editor);
457
458   editor->nParagraphs--;
459   tp->member.para.nFlags |= MEPF_REWRAP;
460   return tp;
461 }
462
463 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
464   return ME_FindItemBackOrHere(item, diParagraph);
465 }
466
467 void ME_DumpParaStyleToBuf(const PARAFORMAT2 *pFmt, char buf[2048])
468 {
469   char *p;
470   p = buf;
471
472 #define DUMP(mask, name, fmt, field) \
473   if (pFmt->dwMask & (mask)) p += sprintf(p, "%-22s" fmt "\n", name, pFmt->field); \
474   else p += sprintf(p, "%-22sN/A\n", name);
475
476 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
477 #define DUMP_EFFECT(mask, name) \
478   p += sprintf(p, "%-22s%s\n", name, (pFmt->dwMask & (mask)) ? ((pFmt->wEffects & ((mask) >> 16)) ? "yes" : "no") : "N/A");
479
480   DUMP(PFM_NUMBERING,      "Numbering:",         "%u", wNumbering);
481   DUMP_EFFECT(PFM_DONOTHYPHEN,     "Disable auto-hyphen:");
482   DUMP_EFFECT(PFM_KEEP,            "No page break in para:");
483   DUMP_EFFECT(PFM_KEEPNEXT,        "No page break in para & next:");
484   DUMP_EFFECT(PFM_NOLINENUMBER,    "No line number:");
485   DUMP_EFFECT(PFM_NOWIDOWCONTROL,  "No widow & orphan:");
486   DUMP_EFFECT(PFM_PAGEBREAKBEFORE, "Page break before:");
487   DUMP_EFFECT(PFM_RTLPARA,         "RTL para:");
488   DUMP_EFFECT(PFM_SIDEBYSIDE,      "Side by side:");
489   DUMP_EFFECT(PFM_TABLE,           "Table:");
490   DUMP(PFM_OFFSETINDENT,   "Offset indent:",     "%d", dxStartIndent);
491   DUMP(PFM_STARTINDENT,    "Start indent:",      "%d", dxStartIndent);
492   DUMP(PFM_RIGHTINDENT,    "Right indent:",      "%d", dxRightIndent);
493   DUMP(PFM_OFFSET,         "Offset:",            "%d", dxOffset);
494   if (pFmt->dwMask & PFM_ALIGNMENT) {
495     switch (pFmt->wAlignment) {
496     case PFA_LEFT   : p += sprintf(p, "Alignment:            left\n"); break;
497     case PFA_RIGHT  : p += sprintf(p, "Alignment:            right\n"); break;
498     case PFA_CENTER : p += sprintf(p, "Alignment:            center\n"); break;
499     case PFA_JUSTIFY: p += sprintf(p, "Alignment:            justify\n"); break;
500     default         : p += sprintf(p, "Alignment:            incorrect %d\n", pFmt->wAlignment); break;
501     }
502   }
503   else p += sprintf(p, "Alignment:            N/A\n");
504   DUMP(PFM_TABSTOPS,       "Tab Stops:",         "%d", cTabCount);
505   if (pFmt->dwMask & PFM_TABSTOPS) {
506     int i;
507     p += sprintf(p, "\t");
508     for (i = 0; i < pFmt->cTabCount; i++) p += sprintf(p, "%x ", pFmt->rgxTabs[i]);
509     p += sprintf(p, "\n");
510   }
511   DUMP(PFM_SPACEBEFORE,    "Space Before:",      "%d", dySpaceBefore);
512   DUMP(PFM_SPACEAFTER,     "Space After:",       "%d", dySpaceAfter);
513   DUMP(PFM_LINESPACING,    "Line spacing:",      "%d", dyLineSpacing);
514   DUMP(PFM_STYLE,          "Text style:",        "%d", sStyle);
515   DUMP(PFM_LINESPACING,    "Line spacing rule:", "%u", bLineSpacingRule);
516   /* bOutlineLevel should be 0 */
517   DUMP(PFM_SHADING,        "Shading Weigth:",    "%u", wShadingWeight);
518   DUMP(PFM_SHADING,        "Shading Style:",     "%u", wShadingStyle);
519   DUMP(PFM_NUMBERINGSTART, "Numbering Start:",   "%u", wNumberingStart);
520   DUMP(PFM_NUMBERINGSTYLE, "Numbering Style:",   "0x%x", wNumberingStyle);
521   DUMP(PFM_NUMBERINGTAB,   "Numbering Tab:",     "%u", wNumberingStyle);
522   DUMP(PFM_BORDER,         "Border Space:",      "%u", wBorderSpace);
523   DUMP(PFM_BORDER,         "Border Width:",      "%u", wBorderWidth);
524   DUMP(PFM_BORDER,         "Borders:",           "%u", wBorders);
525
526 #undef DUMP
527 #undef DUMP_EFFECT
528 }
529
530 void
531 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
532 {
533   ME_Cursor *pEndCursor = &editor->pCursors[1];
534
535   *para = editor->pCursors[0].pPara;
536   *para_end = editor->pCursors[1].pPara;
537   if (*para == *para_end)
538     return;
539
540   if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
541     ME_DisplayItem *tmp = *para;
542
543     *para = *para_end;
544     *para_end = tmp;
545     pEndCursor = &editor->pCursors[0];
546   }
547
548   /* The paragraph at the end of a non-empty selection isn't included
549    * if the selection ends at the start of the paragraph. */
550   if (!pEndCursor->pRun->member.run.nCharOfs && !pEndCursor->nOffset)
551     *para_end = (*para_end)->member.para.prev_para;
552 }
553
554
555 BOOL ME_SetSelectionParaFormat(ME_TextEditor *editor, const PARAFORMAT2 *pFmt)
556 {
557   ME_DisplayItem *para, *para_end;
558
559   ME_GetSelectionParas(editor, &para, &para_end);
560
561   do {
562     ME_SetParaFormat(editor, para, pFmt);
563     if (para == para_end)
564       break;
565     para = para->member.para.next_para;
566   } while(1);
567
568   return TRUE;
569 }
570
571 static void ME_GetParaFormat(ME_TextEditor *editor,
572                              const ME_DisplayItem *para,
573                              PARAFORMAT2 *pFmt)
574 {
575   UINT cbSize = pFmt->cbSize;
576   if (pFmt->cbSize >= sizeof(PARAFORMAT2)) {
577     *pFmt = *para->member.para.pFmt;
578   } else {
579     CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
580     pFmt->dwMask &= PFM_ALL;
581   }
582   pFmt->cbSize = cbSize;
583 }
584
585 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
586 {
587   ME_DisplayItem *para, *para_end;
588   PARAFORMAT2 *curFmt;
589
590   if (pFmt->cbSize < sizeof(PARAFORMAT)) {
591     pFmt->dwMask = 0;
592     return;
593   }
594
595   ME_GetSelectionParas(editor, &para, &para_end);
596
597   ME_GetParaFormat(editor, para, pFmt);
598
599   /* Invalidate values that change across the selected paragraphs. */
600   while (para != para_end)
601   {
602     para = para->member.para.next_para;
603     curFmt = para->member.para.pFmt;
604
605 #define CHECK_FIELD(m, f) \
606     if (pFmt->f != curFmt->f) pFmt->dwMask &= ~(m);
607
608     CHECK_FIELD(PFM_NUMBERING, wNumbering);
609     CHECK_FIELD(PFM_STARTINDENT, dxStartIndent);
610     CHECK_FIELD(PFM_RIGHTINDENT, dxRightIndent);
611     CHECK_FIELD(PFM_OFFSET, dxOffset);
612     CHECK_FIELD(PFM_ALIGNMENT, wAlignment);
613     if (pFmt->dwMask & PFM_TABSTOPS) {
614       if (pFmt->cTabCount != para->member.para.pFmt->cTabCount ||
615           memcmp(pFmt->rgxTabs, curFmt->rgxTabs, curFmt->cTabCount*sizeof(int)))
616         pFmt->dwMask &= ~PFM_TABSTOPS;
617     }
618
619     if (pFmt->dwMask >= sizeof(PARAFORMAT2))
620     {
621       pFmt->dwMask &= ~((pFmt->wEffects ^ curFmt->wEffects) << 16);
622       CHECK_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
623       CHECK_FIELD(PFM_SPACEAFTER, dySpaceAfter);
624       CHECK_FIELD(PFM_LINESPACING, dyLineSpacing);
625       CHECK_FIELD(PFM_STYLE, sStyle);
626       CHECK_FIELD(PFM_SPACEAFTER, bLineSpacingRule);
627       CHECK_FIELD(PFM_SHADING, wShadingWeight);
628       CHECK_FIELD(PFM_SHADING, wShadingStyle);
629       CHECK_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
630       CHECK_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
631       CHECK_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
632       CHECK_FIELD(PFM_BORDER, wBorderSpace);
633       CHECK_FIELD(PFM_BORDER, wBorderWidth);
634       CHECK_FIELD(PFM_BORDER, wBorders);
635     }
636 #undef CHECK_FIELD
637   }
638 }
639
640 void ME_SetDefaultParaFormat(PARAFORMAT2 *pFmt)
641 {
642     ZeroMemory(pFmt, sizeof(PARAFORMAT2));
643     pFmt->cbSize = sizeof(PARAFORMAT2);
644     pFmt->dwMask = PFM_ALL2;
645     pFmt->wAlignment = PFA_LEFT;
646     pFmt->sStyle = -1;
647     pFmt->bOutlineLevel = TRUE;
648 }