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