Improved RTF export.
[wine] / dlls / riched20 / para.c
1 /*
2  * RichEdit - functions working on paragraphs of text (diParagraph).
3  * 
4  * Copyright 2004 by Krzysztof Foltman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */ 
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 static WCHAR wszParagraphSign[] = {0xB6, 0};
26
27 void ME_MakeFirstParagraph(HDC hDC, ME_TextBuffer *text)
28 {
29   PARAFORMAT2 fmt;
30   CHARFORMAT2W cf;
31   LOGFONTW lf;
32   HFONT hf;
33   ME_DisplayItem *para = ME_MakeDI(diParagraph);
34   ME_DisplayItem *run;
35   ME_Style *style;
36
37   hf = (HFONT)GetStockObject(SYSTEM_FONT);
38   assert(hf);
39   GetObjectW(hf, sizeof(LOGFONTW), &lf);
40   ZeroMemory(&cf, sizeof(cf));
41   cf.cbSize = sizeof(cf);
42   cf.dwMask = CFM_BACKCOLOR|CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_CHARSET;
43   cf.dwMask |= CFM_ALLCAPS|CFM_BOLD|CFM_DISABLED|CFM_EMBOSS|CFM_HIDDEN;
44   cf.dwMask |= CFM_IMPRINT|CFM_ITALIC|CFM_LINK|CFM_OUTLINE|CFM_PROTECTED;
45   cf.dwMask |= CFM_REVISED|CFM_SHADOW|CFM_SMALLCAPS|CFM_STRIKEOUT;
46   cf.dwMask |= CFM_SUBSCRIPT|CFM_UNDERLINE;
47   
48   cf.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
49   lstrcpyW(cf.szFaceName, lf.lfFaceName);
50   cf.yHeight=lf.lfHeight*1440/GetDeviceCaps(hDC, LOGPIXELSY);
51   if (lf.lfWeight>=700) /* FIXME correct weight ? */
52     cf.dwEffects |= CFE_BOLD;
53   cf.wWeight = lf.lfWeight;
54   if (lf.lfItalic) cf.dwEffects |= CFE_ITALIC;
55   if (lf.lfUnderline) cf.dwEffects |= CFE_UNDERLINE;
56   if (lf.lfStrikeOut) cf.dwEffects |= CFE_STRIKEOUT;
57   
58   ZeroMemory(&fmt, sizeof(fmt));
59   fmt.cbSize = sizeof(fmt);
60   fmt.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_STARTINDENT | PFM_RIGHTINDENT;
61
62   CopyMemory(para->member.para.pFmt, &fmt, sizeof(PARAFORMAT2));
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
70   ME_InsertBefore(text->pLast, para);
71   ME_InsertBefore(text->pLast, run);
72   para->member.para.prev_para = text->pFirst;
73   para->member.para.next_para = text->pLast;
74   text->pFirst->member.para.next_para = para;
75   text->pLast->member.para.prev_para = para;
76
77   text->pLast->member.para.nCharOfs = 1;
78 }
79  
80 void ME_MarkAllForWrapping(ME_TextEditor *editor)
81 {
82   ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
83 }
84
85 void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, ME_DisplayItem *last)
86 {
87   while(first != last)
88   {
89     first->member.para.nFlags |= MEPF_REWRAP;
90     first = first->member.para.next_para;
91   }
92 }
93
94 /* split paragraph at the beginning of the run */
95 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run, ME_Style *style)
96 {
97   ME_DisplayItem *next_para = NULL;
98   ME_DisplayItem *run_para = NULL;
99   ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
100   ME_DisplayItem *end_run = ME_MakeRun(style,ME_MakeString(wszParagraphSign), MERF_ENDPARA);
101   ME_UndoItem *undo = NULL;
102   int ofs;
103   ME_DisplayItem *pp;
104   
105   assert(run->type == diRun);  
106
107   run_para = ME_GetParagraph(run);
108   assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
109
110   ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
111   next_para = run_para->member.para.next_para;
112   assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
113   
114   undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
115   if (undo)
116     undo->nStart = run_para->member.para.nCharOfs + ofs;
117   
118   /* the new paragraph will have a different starting offset, so let's update its runs */
119   pp = run;
120   while(pp->type == diRun) {
121     pp->member.run.nCharOfs -= ofs;
122     pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
123   }
124   new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
125   new_para->member.para.nCharOfs += 1;
126   
127   new_para->member.para.nFlags = MEPF_REWRAP; /* FIXME copy flags (if applicable) */
128   /* FIXME initialize format style and call ME_SetParaFormat blah blah */
129   CopyMemory(new_para->member.para.pFmt, run_para->member.para.pFmt, sizeof(PARAFORMAT2));
130   
131   /* FIXME remove this as soon as nLeftMargin etc are replaced with proper fields of PARAFORMAT2 */
132   new_para->member.para.nLeftMargin = run_para->member.para.nLeftMargin;
133   new_para->member.para.nRightMargin = run_para->member.para.nRightMargin;
134   new_para->member.para.nFirstMargin = run_para->member.para.nFirstMargin;
135   
136   /* insert paragraph into paragraph double linked list */
137   new_para->member.para.prev_para = run_para;
138   new_para->member.para.next_para = next_para;
139   run_para->member.para.next_para = new_para;
140   next_para->member.para.prev_para = new_para;
141
142   /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
143   ME_InsertBefore(run, new_para);
144   ME_InsertBefore(new_para, end_run);
145
146   /* force rewrap of the */
147   run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
148   new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
149   
150   /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
151   ME_PropagateCharOffset(next_para, 1);
152   editor->nParagraphs++;
153   
154   return new_para;
155 }
156
157 /* join tp with tp->member.para.next_para, keeping tp's style; this
158  * is consistent with the original */
159 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp)
160 {
161   ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
162   int i, shift;
163   ME_UndoItem *undo = NULL;
164
165   assert(tp->type == diParagraph);
166   assert(tp->member.para.next_para);
167   assert(tp->member.para.next_para->type == diParagraph);
168   
169   pNext = tp->member.para.next_para;
170   
171   {
172     /* null char format operation to store the original char format for the ENDPARA run */
173     CHARFORMAT2W fmt;
174     ME_InitCharFormat2W(&fmt);
175     ME_SetCharFormat(editor, pNext->member.para.nCharOfs-1, 1, &fmt);
176   }
177   undo = ME_AddUndoItem(editor, diUndoSplitParagraph, NULL);
178   if (undo)
179   {
180     undo->nStart = pNext->member.para.nCharOfs-1;
181     assert(pNext->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
182     CopyMemory(undo->di.member.para.pFmt, pNext->member.para.pFmt, sizeof(PARAFORMAT2));
183   }
184   
185   shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - 1;
186   
187   pRun = ME_FindItemBack(pNext, diRunOrParagraph);
188   pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
189   
190   assert(pRun);
191   assert(pRun->type == diRun);
192   assert(pRun->member.run.nFlags & MERF_ENDPARA);
193   assert(pFirstRunInNext->type == diRun);
194   
195   /* if some cursor points at end of paragraph, make it point to the first
196      run of the next joined paragraph */
197   for (i=0; i<editor->nCursors; i++) {
198     if (editor->pCursors[i].pRun == pRun) {
199       editor->pCursors[i].pRun = pFirstRunInNext;
200       editor->pCursors[i].nOffset = 0;
201     }
202   }
203
204   pTmp = pNext;
205   do {
206     pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
207     if (pTmp->type != diRun)
208       break;
209     TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
210     pTmp->member.run.nCharOfs += shift;
211   } while(1);
212   
213   ME_Remove(pRun);
214   ME_DestroyDisplayItem(pRun);
215
216   tp->member.para.next_para = pNext->member.para.next_para;
217   pNext->member.para.next_para->member.para.prev_para = tp;
218   ME_Remove(pNext);
219   ME_DestroyDisplayItem(pNext);
220
221   ME_PropagateCharOffset(tp->member.para.next_para, -1);
222   
223   ME_CheckCharOffsets(editor);
224   
225   editor->nParagraphs--;
226   tp->member.para.nFlags |= MEPF_REWRAP;
227   return tp;
228 }
229
230 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
231   return ME_FindItemBackOrHere(item, diParagraph);
232 }
233
234 static void ME_DumpStyleEffect(char **p, const char *name, PARAFORMAT2 *fmt, int mask)
235 {
236   *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->wEffects & mask) ? "yes" : "no") : "N/A");
237 }
238
239 void ME_DumpParaStyleToBuf(PARAFORMAT2 *pFmt, char buf[2048])
240 {
241   /* FIXME only PARAFORMAT styles implemented */
242   char *p;
243   p = buf;
244   p += sprintf(p, "Alignment:            %s\n",
245     !(pFmt->dwMask & PFM_ALIGNMENT) ? "N/A" :
246       ((pFmt->wAlignment == PFA_LEFT) ? "left" :
247         ((pFmt->wAlignment == PFA_RIGHT) ? "right" :
248           ((pFmt->wAlignment == PFA_CENTER) ? "center" :
249             /*((pFmt->wAlignment == PFA_JUSTIFY) ? "justify" : "incorrect")*/
250             "incorrect"))));
251
252   if (pFmt->dwMask & PFM_OFFSET)
253     p += sprintf(p, "Offset:               %d\n", (int)pFmt->dxOffset);
254   else
255     p += sprintf(p, "Offset:               N/A\n");
256     
257   if (pFmt->dwMask & PFM_OFFSETINDENT)
258     p += sprintf(p, "Offset indent:        %d\n", (int)pFmt->dxStartIndent);
259   else
260     p += sprintf(p, "Offset indent:        N/A\n");
261     
262   if (pFmt->dwMask & PFM_STARTINDENT)
263     p += sprintf(p, "Start indent:         %d\n", (int)pFmt->dxStartIndent);
264   else
265     p += sprintf(p, "Start indent:         N/A\n");
266     
267   if (pFmt->dwMask & PFM_RIGHTINDENT)
268     p += sprintf(p, "Right indent:         %d\n", (int)pFmt->dxRightIndent);
269   else
270     p += sprintf(p, "Right indent:         N/A\n");
271     
272   ME_DumpStyleEffect(&p, "Page break before:", pFmt, PFM_PAGEBREAKBEFORE);
273 }
274
275 void ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, PARAFORMAT2 *pFmt)
276 {
277   PARAFORMAT2 copy;
278   assert(sizeof(*para->member.para.pFmt) == sizeof(PARAFORMAT2));
279   ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
280   
281   CopyMemory(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2));
282
283   if (pFmt->dwMask & PFM_ALIGNMENT)
284     para->member.para.pFmt->wAlignment = pFmt->wAlignment;
285     
286   /* FIXME to be continued (indents, bulleting and such) */
287
288   if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
289     para->member.para.nFlags |= MEPF_REWRAP;
290 }
291
292 void ME_SetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
293 {
294   int nFrom, nTo;
295   ME_DisplayItem *para, *para_end, *run;
296   int nOffset;
297   
298   ME_GetSelection(editor, &nFrom, &nTo);
299   if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
300     nTo--;
301   
302   ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
303   para = ME_GetParagraph(run);
304   ME_RunOfsFromCharOfs(editor, nTo, &run, &nOffset);
305   para_end = ME_GetParagraph(run);
306   
307   do {
308     ME_SetParaFormat(editor, para, pFmt);
309     if (para == para_end)
310       break;
311     para = para->member.para.next_para;
312   } while(1);
313   ME_Repaint(editor);
314 }
315
316 void ME_GetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, PARAFORMAT2 *pFmt)
317 {
318   if (pFmt->cbSize >= sizeof(PARAFORMAT2))
319   {
320     CopyMemory(pFmt, para->member.para.pFmt, sizeof(PARAFORMAT2));
321     return;
322   }
323   CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);  
324 }
325
326 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
327 {
328   int nFrom, nTo;
329   ME_DisplayItem *para, *para_end, *run;
330   int nOffset;
331   PARAFORMAT2 tmp;
332   
333   ME_GetSelection(editor, &nFrom, &nTo);
334   if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
335     nTo--;
336   
337   ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
338   para = ME_GetParagraph(run);
339   ME_RunOfsFromCharOfs(editor, nTo, &run, &nOffset);
340   para_end = ME_GetParagraph(run);
341   
342   ME_GetParaFormat(editor, para, pFmt);
343   if (para == para_end) return;
344   
345   do {
346     ZeroMemory(&tmp, sizeof(tmp));
347     tmp.cbSize = sizeof(tmp);
348     ME_GetParaFormat(editor, para, &tmp);
349     assert(tmp.dwMask & PFM_ALIGNMENT);
350     
351     if (pFmt->wAlignment != tmp.wAlignment)
352       pFmt->dwMask &= ~PFM_ALIGNMENT;
353     
354     if (para == para_end)
355       return;
356     para = para->member.para.next_para;
357   } while(1);
358 }