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