d3dx9_36/tests: Avoid a potential out of bound access.
[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 static void ME_UpdateTableFlags(ME_DisplayItem *para)
98 {
99   para->member.para.pFmt->dwMask |= PFM_TABLE|PFM_TABLEROWDELIMITER;
100   if (para->member.para.pCell) {
101     para->member.para.nFlags |= MEPF_CELL;
102   } else {
103     para->member.para.nFlags &= ~MEPF_CELL;
104   }
105   if (para->member.para.nFlags & MEPF_ROWEND) {
106     para->member.para.pFmt->wEffects |= PFE_TABLEROWDELIMITER;
107   } else {
108     para->member.para.pFmt->wEffects &= ~PFE_TABLEROWDELIMITER;
109   }
110   if (para->member.para.nFlags & (MEPF_ROWSTART|MEPF_CELL|MEPF_ROWEND))
111     para->member.para.pFmt->wEffects |= PFE_TABLE;
112   else
113     para->member.para.pFmt->wEffects &= ~PFE_TABLE;
114 }
115
116 static BOOL ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, const PARAFORMAT2 *pFmt)
117 {
118   PARAFORMAT2 copy;
119   DWORD dwMask;
120
121   assert(para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
122   dwMask = pFmt->dwMask;
123   if (pFmt->cbSize < sizeof(PARAFORMAT))
124     return FALSE;
125   else if (pFmt->cbSize < sizeof(PARAFORMAT2))
126     dwMask &= PFM_ALL;
127   else
128     dwMask &= PFM_ALL2;
129
130   ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
131
132   copy = *para->member.para.pFmt;
133
134 #define COPY_FIELD(m, f) \
135   if (dwMask & (m)) {                           \
136     para->member.para.pFmt->dwMask |= m;        \
137     para->member.para.pFmt->f = pFmt->f;        \
138   }
139
140   COPY_FIELD(PFM_NUMBERING, wNumbering);
141   COPY_FIELD(PFM_STARTINDENT, dxStartIndent);
142   if (dwMask & PFM_OFFSETINDENT)
143     para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
144   COPY_FIELD(PFM_RIGHTINDENT, dxRightIndent);
145   COPY_FIELD(PFM_OFFSET, dxOffset);
146   COPY_FIELD(PFM_ALIGNMENT, wAlignment);
147   if (dwMask & PFM_TABSTOPS)
148   {
149     para->member.para.pFmt->cTabCount = pFmt->cTabCount;
150     memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(LONG));
151   }
152
153   if (dwMask & (PFM_ALL2 & ~PFM_ALL))
154   {
155     /* PARAFORMAT2 fields */
156
157 #define EFFECTS_MASK (PFM_RTLPARA|PFM_KEEP|PFM_KEEPNEXT|PFM_PAGEBREAKBEFORE| \
158                       PFM_NOLINENUMBER|PFM_NOWIDOWCONTROL|PFM_DONOTHYPHEN|PFM_SIDEBYSIDE| \
159                       PFM_TABLE)
160     /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
161     if (dwMask & EFFECTS_MASK) {
162       para->member.para.pFmt->dwMask |= dwMask & EFFECTS_MASK;
163       para->member.para.pFmt->wEffects &= ~HIWORD(dwMask);
164       para->member.para.pFmt->wEffects |= pFmt->wEffects & HIWORD(dwMask);
165     }
166 #undef EFFECTS_MASK
167
168     COPY_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
169     COPY_FIELD(PFM_SPACEAFTER, dySpaceAfter);
170     COPY_FIELD(PFM_LINESPACING, dyLineSpacing);
171     COPY_FIELD(PFM_STYLE, sStyle);
172     COPY_FIELD(PFM_LINESPACING, bLineSpacingRule);
173     COPY_FIELD(PFM_SHADING, wShadingWeight);
174     COPY_FIELD(PFM_SHADING, wShadingStyle);
175     COPY_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
176     COPY_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
177     COPY_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
178     COPY_FIELD(PFM_BORDER, wBorderSpace);
179     COPY_FIELD(PFM_BORDER, wBorderWidth);
180     COPY_FIELD(PFM_BORDER, wBorders);
181   }
182
183   para->member.para.pFmt->dwMask |= dwMask;
184 #undef COPY_FIELD
185
186   if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
187     para->member.para.nFlags |= MEPF_REWRAP;
188
189   return TRUE;
190 }
191
192 /* split paragraph at the beginning of the run */
193 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run,
194                                   ME_Style *style, ME_String *eol_str,
195                                   int paraFlags)
196 {
197   ME_DisplayItem *next_para = NULL;
198   ME_DisplayItem *run_para = NULL;
199   ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
200   ME_DisplayItem *end_run;
201   ME_UndoItem *undo = NULL;
202   int ofs, i;
203   ME_DisplayItem *pp;
204   int run_flags = MERF_ENDPARA;
205
206   if (!editor->bEmulateVersion10) { /* v4.1 */
207     /* At most 1 of MEPF_CELL, MEPF_ROWSTART, or MEPF_ROWEND should be set. */
208     assert(!(paraFlags & ~(MEPF_CELL|MEPF_ROWSTART|MEPF_ROWEND)));
209     assert(!(paraFlags & (paraFlags-1)));
210     if (paraFlags == MEPF_CELL)
211       run_flags |= MERF_ENDCELL;
212     else if (paraFlags == MEPF_ROWSTART)
213       run_flags |= MERF_TABLESTART|MERF_HIDDEN;
214   } else { /* v1.0 - v3.0 */
215     assert(!(paraFlags & (MEPF_CELL|MEPF_ROWSTART|MEPF_ROWEND)));
216   }
217   end_run = ME_MakeRun(style, eol_str, run_flags);
218
219   assert(run->type == diRun);
220   run_para = ME_GetParagraph(run);
221   assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
222
223   ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
224   next_para = run_para->member.para.next_para;
225   assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
226   
227   undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
228   if (undo)
229     undo->nStart = run_para->member.para.nCharOfs + ofs;
230
231   /* Update selection cursors to point to the correct paragraph. */
232   for (i = 0; i < editor->nCursors; i++) {
233     if (editor->pCursors[i].pPara == run_para &&
234         run->member.run.nCharOfs <= editor->pCursors[i].pRun->member.run.nCharOfs)
235     {
236       editor->pCursors[i].pPara = new_para;
237     }
238   }
239
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);
245   }
246   new_para->member.para.nCharOfs = run_para->member.para.nCharOfs + ofs;
247   new_para->member.para.nCharOfs += eol_str->nLen;
248   new_para->member.para.nFlags = MEPF_REWRAP;
249
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;
253
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;
259
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);
263
264   if (!editor->bEmulateVersion10) { /* v4.1 */
265     if (paraFlags & (MEPF_ROWSTART|MEPF_CELL))
266     {
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)
272       {
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;
288       }
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)
298       {
299         /* Row starts just after the row that was ended. */
300         new_para->member.para.nFlags |= MEPF_ROWSTART;
301       }
302     } else {
303       new_para->member.para.pCell = run_para->member.para.pCell;
304     }
305     ME_UpdateTableFlags(run_para);
306     ME_UpdateTableFlags(new_para);
307   }
308
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;
312
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++;
316
317   return new_para;
318 }
319
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)
324 {
325   ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
326   int i, shift;
327   ME_UndoItem *undo = NULL;
328   int end_len;
329   CHARFORMAT2W fmt;
330   ME_Cursor startCur, endCur;
331
332   assert(tp->type == diParagraph);
333   assert(tp->member.para.next_para);
334   assert(tp->member.para.next_para->type == diParagraph);
335
336   pNext = tp->member.para.next_para;
337
338   /* Need to locate end-of-paragraph run here, in order to know end_len */
339   pRun = ME_FindItemBack(pNext, diRunOrParagraph);
340
341   assert(pRun);
342   assert(pRun->type == diRun);
343   assert(pRun->member.run.nFlags & MERF_ENDPARA);
344
345   end_len = pRun->member.run.strText->nLen;
346
347   /* null char format operation to store the original char format for the ENDPARA run */
348   ME_InitCharFormat2W(&fmt);
349   endCur.pPara = pNext;
350   endCur.pRun = ME_FindItemFwd(pNext, diRun);
351   endCur.nOffset = 0;
352   startCur = endCur;
353   ME_PrevRun(&startCur.pPara, &startCur.pRun);
354   ME_SetCharFormat(editor, &startCur, &endCur, &fmt);
355
356   undo = ME_AddUndoItem(editor, diUndoSplitParagraph, pNext);
357   if (undo)
358   {
359     undo->nStart = pNext->member.para.nCharOfs - end_len;
360     undo->eol_str = pRun->member.run.strText;
361     pRun->member.run.strText = NULL; /* Avoid freeing the string */
362   }
363   if (!keepFirstParaFormat)
364   {
365     ME_AddUndoItem(editor, diUndoSetParagraphFormat, tp);
366     *tp->member.para.pFmt = *pNext->member.para.pFmt;
367     tp->member.para.border = pNext->member.para.border;
368   }
369
370   if (!editor->bEmulateVersion10) { /* v4.1 */
371     /* Table cell/row properties are always moved over from the removed para. */
372     tp->member.para.nFlags = pNext->member.para.nFlags;
373     tp->member.para.pCell = pNext->member.para.pCell;
374
375     /* Remove cell boundary if it is between the end paragraph run and the next
376      * paragraph display item. */
377     pTmp = pRun->next;
378     while (pTmp != pNext) {
379       if (pTmp->type == diCell)
380       {
381         ME_Cell *pCell = &pTmp->member.cell;
382         if (undo)
383         {
384           assert(!(undo->di.member.para.nFlags & MEPF_ROWEND));
385           if (!(undo->di.member.para.nFlags & MEPF_ROWSTART))
386             undo->di.member.para.nFlags |= MEPF_CELL;
387           undo->di.member.para.pCell = ALLOC_OBJ(ME_DisplayItem);
388           *undo->di.member.para.pCell = *pTmp;
389           undo->di.member.para.pCell->next = NULL;
390           undo->di.member.para.pCell->prev = NULL;
391           undo->di.member.para.pCell->member.cell.next_cell = NULL;
392           undo->di.member.para.pCell->member.cell.prev_cell = NULL;
393         }
394         ME_Remove(pTmp);
395         if (pCell->prev_cell)
396           pCell->prev_cell->member.cell.next_cell = pCell->next_cell;
397         if (pCell->next_cell)
398           pCell->next_cell->member.cell.prev_cell = pCell->prev_cell;
399         ME_DestroyDisplayItem(pTmp);
400         break;
401       }
402       pTmp = pTmp->next;
403     }
404   }
405
406   shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
407
408   pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
409
410   assert(pFirstRunInNext->type == diRun);
411
412   /* Update selection cursors so they don't point to the removed end
413    * paragraph run, and point to the correct paragraph. */
414   for (i=0; i < editor->nCursors; i++) {
415     if (editor->pCursors[i].pRun == pRun) {
416       editor->pCursors[i].pRun = pFirstRunInNext;
417       editor->pCursors[i].nOffset = 0;
418     } else if (editor->pCursors[i].pPara == pNext) {
419       editor->pCursors[i].pPara = tp;
420     }
421   }
422
423   pTmp = pNext;
424   do {
425     pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
426     if (pTmp->type != diRun)
427       break;
428     TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
429     pTmp->member.run.nCharOfs += shift;
430   } while(1);
431
432   ME_Remove(pRun);
433   ME_DestroyDisplayItem(pRun);
434
435   if (editor->pLastSelStartPara == pNext)
436     editor->pLastSelStartPara = tp;
437   if (editor->pLastSelEndPara == pNext)
438     editor->pLastSelEndPara = tp;
439
440   tp->member.para.next_para = pNext->member.para.next_para;
441   pNext->member.para.next_para->member.para.prev_para = tp;
442   ME_Remove(pNext);
443   ME_DestroyDisplayItem(pNext);
444
445   ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
446
447   ME_CheckCharOffsets(editor);
448
449   editor->nParagraphs--;
450   tp->member.para.nFlags |= MEPF_REWRAP;
451   return tp;
452 }
453
454 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
455   return ME_FindItemBackOrHere(item, diParagraph);
456 }
457
458 void ME_DumpParaStyleToBuf(const PARAFORMAT2 *pFmt, char buf[2048])
459 {
460   char *p;
461   p = buf;
462
463 #define DUMP(mask, name, fmt, field) \
464   if (pFmt->dwMask & (mask)) p += sprintf(p, "%-22s" fmt "\n", name, pFmt->field); \
465   else p += sprintf(p, "%-22sN/A\n", name);
466
467 /* we take for granted that PFE_xxx is the hiword of the corresponding PFM_xxx */
468 #define DUMP_EFFECT(mask, name) \
469   p += sprintf(p, "%-22s%s\n", name, (pFmt->dwMask & (mask)) ? ((pFmt->wEffects & ((mask) >> 16)) ? "yes" : "no") : "N/A");
470
471   DUMP(PFM_NUMBERING,      "Numbering:",         "%u", wNumbering);
472   DUMP_EFFECT(PFM_DONOTHYPHEN,     "Disable auto-hyphen:");
473   DUMP_EFFECT(PFM_KEEP,            "No page break in para:");
474   DUMP_EFFECT(PFM_KEEPNEXT,        "No page break in para & next:");
475   DUMP_EFFECT(PFM_NOLINENUMBER,    "No line number:");
476   DUMP_EFFECT(PFM_NOWIDOWCONTROL,  "No widow & orphan:");
477   DUMP_EFFECT(PFM_PAGEBREAKBEFORE, "Page break before:");
478   DUMP_EFFECT(PFM_RTLPARA,         "RTL para:");
479   DUMP_EFFECT(PFM_SIDEBYSIDE,      "Side by side:");
480   DUMP_EFFECT(PFM_TABLE,           "Table:");
481   DUMP(PFM_OFFSETINDENT,   "Offset indent:",     "%d", dxStartIndent);
482   DUMP(PFM_STARTINDENT,    "Start indent:",      "%d", dxStartIndent);
483   DUMP(PFM_RIGHTINDENT,    "Right indent:",      "%d", dxRightIndent);
484   DUMP(PFM_OFFSET,         "Offset:",            "%d", dxOffset);
485   if (pFmt->dwMask & PFM_ALIGNMENT) {
486     switch (pFmt->wAlignment) {
487     case PFA_LEFT   : p += sprintf(p, "Alignment:            left\n"); break;
488     case PFA_RIGHT  : p += sprintf(p, "Alignment:            right\n"); break;
489     case PFA_CENTER : p += sprintf(p, "Alignment:            center\n"); break;
490     case PFA_JUSTIFY: p += sprintf(p, "Alignment:            justify\n"); break;
491     default         : p += sprintf(p, "Alignment:            incorrect %d\n", pFmt->wAlignment); break;
492     }
493   }
494   else p += sprintf(p, "Alignment:            N/A\n");
495   DUMP(PFM_TABSTOPS,       "Tab Stops:",         "%d", cTabCount);
496   if (pFmt->dwMask & PFM_TABSTOPS) {
497     int i;
498     p += sprintf(p, "\t");
499     for (i = 0; i < pFmt->cTabCount; i++) p += sprintf(p, "%x ", pFmt->rgxTabs[i]);
500     p += sprintf(p, "\n");
501   }
502   DUMP(PFM_SPACEBEFORE,    "Space Before:",      "%d", dySpaceBefore);
503   DUMP(PFM_SPACEAFTER,     "Space After:",       "%d", dySpaceAfter);
504   DUMP(PFM_LINESPACING,    "Line spacing:",      "%d", dyLineSpacing);
505   DUMP(PFM_STYLE,          "Text style:",        "%d", sStyle);
506   DUMP(PFM_LINESPACING,    "Line spacing rule:", "%u", bLineSpacingRule);
507   /* bOutlineLevel should be 0 */
508   DUMP(PFM_SHADING,        "Shading Weigth:",    "%u", wShadingWeight);
509   DUMP(PFM_SHADING,        "Shading Style:",     "%u", wShadingStyle);
510   DUMP(PFM_NUMBERINGSTART, "Numbering Start:",   "%u", wNumberingStart);
511   DUMP(PFM_NUMBERINGSTYLE, "Numbering Style:",   "0x%x", wNumberingStyle);
512   DUMP(PFM_NUMBERINGTAB,   "Numbering Tab:",     "%u", wNumberingStyle);
513   DUMP(PFM_BORDER,         "Border Space:",      "%u", wBorderSpace);
514   DUMP(PFM_BORDER,         "Border Width:",      "%u", wBorderWidth);
515   DUMP(PFM_BORDER,         "Borders:",           "%u", wBorders);
516
517 #undef DUMP
518 #undef DUMP_EFFECT
519 }
520
521 void
522 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
523 {
524   ME_Cursor *pEndCursor = &editor->pCursors[1];
525
526   *para = editor->pCursors[0].pPara;
527   *para_end = editor->pCursors[1].pPara;
528   if (*para == *para_end)
529     return;
530
531   if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
532     ME_DisplayItem *tmp = *para;
533
534     *para = *para_end;
535     *para_end = tmp;
536     pEndCursor = &editor->pCursors[0];
537   }
538
539   /* The paragraph at the end of a non-empty selection isn't included
540    * if the selection ends at the start of the paragraph. */
541   if (!pEndCursor->pRun->member.run.nCharOfs && !pEndCursor->nOffset)
542     *para_end = (*para_end)->member.para.prev_para;
543 }
544
545
546 BOOL ME_SetSelectionParaFormat(ME_TextEditor *editor, const PARAFORMAT2 *pFmt)
547 {
548   ME_DisplayItem *para, *para_end;
549
550   ME_GetSelectionParas(editor, &para, &para_end);
551
552   do {
553     ME_SetParaFormat(editor, para, pFmt);
554     if (para == para_end)
555       break;
556     para = para->member.para.next_para;
557   } while(1);
558
559   return TRUE;
560 }
561
562 static void ME_GetParaFormat(ME_TextEditor *editor,
563                              const ME_DisplayItem *para,
564                              PARAFORMAT2 *pFmt)
565 {
566   UINT cbSize = pFmt->cbSize;
567   if (pFmt->cbSize >= sizeof(PARAFORMAT2)) {
568     *pFmt = *para->member.para.pFmt;
569   } else {
570     CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);
571     pFmt->dwMask &= PFM_ALL;
572   }
573   pFmt->cbSize = cbSize;
574 }
575
576 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
577 {
578   ME_DisplayItem *para, *para_end;
579   PARAFORMAT2 *curFmt;
580
581   if (pFmt->cbSize < sizeof(PARAFORMAT)) {
582     pFmt->dwMask = 0;
583     return;
584   }
585
586   ME_GetSelectionParas(editor, &para, &para_end);
587
588   ME_GetParaFormat(editor, para, pFmt);
589
590   /* Invalidate values that change across the selected paragraphs. */
591   while (para != para_end)
592   {
593     para = para->member.para.next_para;
594     curFmt = para->member.para.pFmt;
595
596 #define CHECK_FIELD(m, f) \
597     if (pFmt->f != curFmt->f) pFmt->dwMask &= ~(m);
598
599     CHECK_FIELD(PFM_NUMBERING, wNumbering);
600     CHECK_FIELD(PFM_STARTINDENT, dxStartIndent);
601     CHECK_FIELD(PFM_RIGHTINDENT, dxRightIndent);
602     CHECK_FIELD(PFM_OFFSET, dxOffset);
603     CHECK_FIELD(PFM_ALIGNMENT, wAlignment);
604     if (pFmt->dwMask & PFM_TABSTOPS) {
605       if (pFmt->cTabCount != para->member.para.pFmt->cTabCount ||
606           memcmp(pFmt->rgxTabs, curFmt->rgxTabs, curFmt->cTabCount*sizeof(int)))
607         pFmt->dwMask &= ~PFM_TABSTOPS;
608     }
609
610     if (pFmt->dwMask >= sizeof(PARAFORMAT2))
611     {
612       pFmt->dwMask &= ~((pFmt->wEffects ^ curFmt->wEffects) << 16);
613       CHECK_FIELD(PFM_SPACEBEFORE, dySpaceBefore);
614       CHECK_FIELD(PFM_SPACEAFTER, dySpaceAfter);
615       CHECK_FIELD(PFM_LINESPACING, dyLineSpacing);
616       CHECK_FIELD(PFM_STYLE, sStyle);
617       CHECK_FIELD(PFM_SPACEAFTER, bLineSpacingRule);
618       CHECK_FIELD(PFM_SHADING, wShadingWeight);
619       CHECK_FIELD(PFM_SHADING, wShadingStyle);
620       CHECK_FIELD(PFM_NUMBERINGSTART, wNumberingStart);
621       CHECK_FIELD(PFM_NUMBERINGSTYLE, wNumberingStyle);
622       CHECK_FIELD(PFM_NUMBERINGTAB, wNumberingTab);
623       CHECK_FIELD(PFM_BORDER, wBorderSpace);
624       CHECK_FIELD(PFM_BORDER, wBorderWidth);
625       CHECK_FIELD(PFM_BORDER, wBorders);
626     }
627 #undef CHECK_FIELD
628   }
629 }
630
631 void ME_SetDefaultParaFormat(PARAFORMAT2 *pFmt)
632 {
633     ZeroMemory(pFmt, sizeof(PARAFORMAT2));
634     pFmt->cbSize = sizeof(PARAFORMAT2);
635     pFmt->dwMask = PFM_ALL2;
636     pFmt->wAlignment = PFA_LEFT;
637     pFmt->sStyle = -1;
638     pFmt->bOutlineLevel = TRUE;
639 }