riched20: Use WARN() not ERR() on unknown tokens.
[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(HDC hDC, ME_TextBuffer *text)
29 {
30   PARAFORMAT2 fmt;
31   CHARFORMAT2W cf;
32   LOGFONTW lf;
33   HFONT hf;
34   ME_DisplayItem *para = ME_MakeDI(diParagraph);
35   ME_DisplayItem *run;
36   ME_Style *style;
37
38   hf = (HFONT)GetStockObject(SYSTEM_FONT);
39   assert(hf);
40   GetObjectW(hf, sizeof(LOGFONTW), &lf);
41   ZeroMemory(&cf, sizeof(cf));
42   cf.cbSize = sizeof(cf);
43   cf.dwMask = CFM_BACKCOLOR|CFM_COLOR|CFM_FACE|CFM_SIZE|CFM_CHARSET;
44   cf.dwMask |= CFM_ALLCAPS|CFM_BOLD|CFM_DISABLED|CFM_EMBOSS|CFM_HIDDEN;
45   cf.dwMask |= CFM_IMPRINT|CFM_ITALIC|CFM_LINK|CFM_OUTLINE|CFM_PROTECTED;
46   cf.dwMask |= CFM_REVISED|CFM_SHADOW|CFM_SMALLCAPS|CFM_STRIKEOUT;
47   cf.dwMask |= CFM_SUBSCRIPT|CFM_UNDERLINE;
48   
49   cf.dwEffects = CFE_AUTOCOLOR | CFE_AUTOBACKCOLOR;
50   lstrcpyW(cf.szFaceName, lf.lfFaceName);
51   cf.yHeight=lf.lfHeight*1440/GetDeviceCaps(hDC, LOGPIXELSY);
52   if (lf.lfWeight>=700) /* FIXME correct weight ? */
53     cf.dwEffects |= CFE_BOLD;
54   cf.wWeight = lf.lfWeight;
55   if (lf.lfItalic) cf.dwEffects |= CFE_ITALIC;
56   if (lf.lfUnderline) cf.dwEffects |= CFE_UNDERLINE;
57   if (lf.lfStrikeOut) cf.dwEffects |= CFE_STRIKEOUT;
58   
59   ZeroMemory(&fmt, sizeof(fmt));
60   fmt.cbSize = sizeof(fmt);
61   fmt.dwMask = PFM_ALIGNMENT | PFM_OFFSET | PFM_STARTINDENT | PFM_RIGHTINDENT | PFM_TABSTOPS;
62
63   CopyMemory(para->member.para.pFmt, &fmt, sizeof(PARAFORMAT2));
64   
65   style = ME_MakeStyle(&cf);
66   text->pDefaultStyle = style;
67   
68   run = ME_MakeRun(style, ME_MakeString(wszParagraphSign), 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 = 1;
79 }
80  
81 void ME_MarkAllForWrapping(ME_TextEditor *editor)
82 {
83   ME_MarkForWrapping(editor, editor->pBuffer->pFirst->member.para.next_para, editor->pBuffer->pLast);
84 }
85
86 void ME_MarkForWrapping(ME_TextEditor *editor, ME_DisplayItem *first, ME_DisplayItem *last)
87 {
88   while(first != last)
89   {
90     first->member.para.nFlags |= MEPF_REWRAP;
91     first = first->member.para.next_para;
92   }
93 }
94
95 void ME_MarkForPainting(ME_TextEditor *editor, ME_DisplayItem *first, ME_DisplayItem *last)
96 {
97   while(first != last)
98   {
99     first->member.para.nFlags |= MEPF_REPAINT;
100     first = first->member.para.next_para;
101   }
102 }
103
104 /* split paragraph at the beginning of the run */
105 ME_DisplayItem *ME_SplitParagraph(ME_TextEditor *editor, ME_DisplayItem *run, ME_Style *style)
106 {
107   ME_DisplayItem *next_para = NULL;
108   ME_DisplayItem *run_para = NULL;
109   ME_DisplayItem *new_para = ME_MakeDI(diParagraph);
110   ME_DisplayItem *end_run = ME_MakeRun(style,ME_MakeString(wszParagraphSign), MERF_ENDPARA);
111   ME_UndoItem *undo = NULL;
112   int ofs;
113   ME_DisplayItem *pp;
114   int end_len = (editor->bEmulateVersion10 ? 2 : 1);
115   
116   assert(run->type == diRun);  
117
118   run_para = ME_GetParagraph(run);
119   assert(run_para->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
120
121   ofs = end_run->member.run.nCharOfs = run->member.run.nCharOfs;
122   next_para = run_para->member.para.next_para;
123   assert(next_para == ME_FindItemFwd(run_para, diParagraphOrEnd));
124   
125   undo = ME_AddUndoItem(editor, diUndoJoinParagraphs, NULL);
126   if (undo)
127     undo->nStart = run_para->member.para.nCharOfs + ofs;
128   
129   /* the new paragraph will have a different starting offset, so let's update its runs */
130   pp = run;
131   while(pp->type == diRun) {
132     pp->member.run.nCharOfs -= ofs;
133     pp = ME_FindItemFwd(pp, diRunOrParagraphOrEnd);
134   }
135   new_para->member.para.nCharOfs = ME_GetParagraph(run)->member.para.nCharOfs+ofs;
136   new_para->member.para.nCharOfs += end_len;
137   
138   new_para->member.para.nFlags = MEPF_REWRAP; /* FIXME copy flags (if applicable) */
139   /* FIXME initialize format style and call ME_SetParaFormat blah blah */
140   CopyMemory(new_para->member.para.pFmt, run_para->member.para.pFmt, sizeof(PARAFORMAT2));
141   
142   /* FIXME remove this as soon as nLeftMargin etc are replaced with proper fields of PARAFORMAT2 */
143   new_para->member.para.nLeftMargin = run_para->member.para.nLeftMargin;
144   new_para->member.para.nRightMargin = run_para->member.para.nRightMargin;
145   new_para->member.para.nFirstMargin = run_para->member.para.nFirstMargin;
146
147   new_para->member.para.bTable = run_para->member.para.bTable;
148   
149   /* Inherit previous cell definitions if any */
150   new_para->member.para.pCells = NULL;
151   if (run_para->member.para.pCells)
152   {
153     ME_TableCell *pCell, *pNewCell;
154
155     for (pCell = run_para->member.para.pCells; pCell; pCell = pCell->next)
156     {
157       pNewCell = ALLOC_OBJ(ME_TableCell);
158       pNewCell->nRightBoundary = pCell->nRightBoundary;
159       pNewCell->next = NULL;
160       if (new_para->member.para.pCells)
161         new_para->member.para.pLastCell->next = pNewCell;
162       else
163         new_para->member.para.pCells = pNewCell;
164       new_para->member.para.pLastCell = pNewCell;
165     }
166   }
167     
168   /* fix paragraph properties. FIXME only needed when called from RTF reader */
169   if (run_para->member.para.pCells && !run_para->member.para.bTable)
170   {
171     /* Paragraph does not have an \intbl keyword, so any table definition
172      * stored is invalid */
173     ME_DestroyTableCellList(run_para);
174   }
175   
176   /* insert paragraph into paragraph double linked list */
177   new_para->member.para.prev_para = run_para;
178   new_para->member.para.next_para = next_para;
179   run_para->member.para.next_para = new_para;
180   next_para->member.para.prev_para = new_para;
181
182   /* insert end run of the old paragraph, and new paragraph, into DI double linked list */
183   ME_InsertBefore(run, new_para);
184   ME_InsertBefore(new_para, end_run);
185
186   /* force rewrap of the */
187   run_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
188   new_para->member.para.prev_para->member.para.nFlags |= MEPF_REWRAP;
189   
190   /* we've added the end run, so we need to modify nCharOfs in the next paragraphs */
191   ME_PropagateCharOffset(next_para, end_len);
192   editor->nParagraphs++;
193   
194   return new_para;
195 }
196
197 /* join tp with tp->member.para.next_para, keeping tp's style; this
198  * is consistent with the original */
199 ME_DisplayItem *ME_JoinParagraphs(ME_TextEditor *editor, ME_DisplayItem *tp)
200 {
201   ME_DisplayItem *pNext, *pFirstRunInNext, *pRun, *pTmp;
202   int i, shift;
203   ME_UndoItem *undo = NULL;
204   int end_len = (editor->bEmulateVersion10 ? 2 : 1);
205
206   assert(tp->type == diParagraph);
207   assert(tp->member.para.next_para);
208   assert(tp->member.para.next_para->type == diParagraph);
209   
210   pNext = tp->member.para.next_para;
211   
212   {
213     /* null char format operation to store the original char format for the ENDPARA run */
214     CHARFORMAT2W fmt;
215     ME_InitCharFormat2W(&fmt);
216     ME_SetCharFormat(editor, pNext->member.para.nCharOfs - end_len, end_len, &fmt);
217   }
218   undo = ME_AddUndoItem(editor, diUndoSplitParagraph, NULL);
219   if (undo)
220   {
221     undo->nStart = pNext->member.para.nCharOfs - end_len;
222     assert(pNext->member.para.pFmt->cbSize == sizeof(PARAFORMAT2));
223     CopyMemory(undo->di.member.para.pFmt, pNext->member.para.pFmt, sizeof(PARAFORMAT2));
224   }
225   
226   shift = pNext->member.para.nCharOfs - tp->member.para.nCharOfs - end_len;
227   
228   pRun = ME_FindItemBack(pNext, diRunOrParagraph);
229   pFirstRunInNext = ME_FindItemFwd(pNext, diRunOrParagraph);
230   
231   assert(pRun);
232   assert(pRun->type == diRun);
233   assert(pRun->member.run.nFlags & MERF_ENDPARA);
234   assert(pFirstRunInNext->type == diRun);
235   
236   /* if some cursor points at end of paragraph, make it point to the first
237      run of the next joined paragraph */
238   for (i=0; i<editor->nCursors; i++) {
239     if (editor->pCursors[i].pRun == pRun) {
240       editor->pCursors[i].pRun = pFirstRunInNext;
241       editor->pCursors[i].nOffset = 0;
242     }
243   }
244
245   pTmp = pNext;
246   do {
247     pTmp = ME_FindItemFwd(pTmp, diRunOrParagraphOrEnd);
248     if (pTmp->type != diRun)
249       break;
250     TRACE("shifting \"%s\" by %d (previous %d)\n", debugstr_w(pTmp->member.run.strText->szData), shift, pTmp->member.run.nCharOfs);
251     pTmp->member.run.nCharOfs += shift;
252   } while(1);
253   
254   ME_Remove(pRun);
255   ME_DestroyDisplayItem(pRun);
256
257   if (editor->pLastSelStartPara == pNext)
258     editor->pLastSelStartPara = tp;
259   if (editor->pLastSelEndPara == pNext)
260     editor->pLastSelEndPara = tp;
261     
262   tp->member.para.next_para = pNext->member.para.next_para;
263   pNext->member.para.next_para->member.para.prev_para = tp;
264   ME_Remove(pNext);
265   ME_DestroyDisplayItem(pNext);
266
267   ME_PropagateCharOffset(tp->member.para.next_para, -end_len);
268   
269   ME_CheckCharOffsets(editor);
270   
271   editor->nParagraphs--;
272   tp->member.para.nFlags |= MEPF_REWRAP;
273   return tp;
274 }
275
276 ME_DisplayItem *ME_GetParagraph(ME_DisplayItem *item) {
277   return ME_FindItemBackOrHere(item, diParagraph);
278 }
279
280 static void ME_DumpStyleEffect(char **p, const char *name, PARAFORMAT2 *fmt, int mask)
281 {
282   *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->wEffects & mask) ? "yes" : "no") : "N/A");
283 }
284
285 void ME_DumpParaStyleToBuf(PARAFORMAT2 *pFmt, char buf[2048])
286 {
287   /* FIXME only PARAFORMAT styles implemented */
288   char *p;
289   p = buf;
290   p += sprintf(p, "Alignment:            %s\n",
291     !(pFmt->dwMask & PFM_ALIGNMENT) ? "N/A" :
292       ((pFmt->wAlignment == PFA_LEFT) ? "left" :
293         ((pFmt->wAlignment == PFA_RIGHT) ? "right" :
294           ((pFmt->wAlignment == PFA_CENTER) ? "center" :
295             /*((pFmt->wAlignment == PFA_JUSTIFY) ? "justify" : "incorrect")*/
296             "incorrect"))));
297
298   if (pFmt->dwMask & PFM_OFFSET)
299     p += sprintf(p, "Offset:               %d\n", (int)pFmt->dxOffset);
300   else
301     p += sprintf(p, "Offset:               N/A\n");
302     
303   if (pFmt->dwMask & PFM_OFFSETINDENT)
304     p += sprintf(p, "Offset indent:        %d\n", (int)pFmt->dxStartIndent);
305   else
306     p += sprintf(p, "Offset indent:        N/A\n");
307     
308   if (pFmt->dwMask & PFM_STARTINDENT)
309     p += sprintf(p, "Start indent:         %d\n", (int)pFmt->dxStartIndent);
310   else
311     p += sprintf(p, "Start indent:         N/A\n");
312     
313   if (pFmt->dwMask & PFM_RIGHTINDENT)
314     p += sprintf(p, "Right indent:         %d\n", (int)pFmt->dxRightIndent);
315   else
316     p += sprintf(p, "Right indent:         N/A\n");
317     
318   ME_DumpStyleEffect(&p, "Page break before:", pFmt, PFM_PAGEBREAKBEFORE);
319 }
320
321 void ME_SetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, PARAFORMAT2 *pFmt)
322 {
323   PARAFORMAT2 copy;
324   assert(sizeof(*para->member.para.pFmt) == sizeof(PARAFORMAT2));
325   ME_AddUndoItem(editor, diUndoSetParagraphFormat, para);
326   
327   CopyMemory(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2));
328
329   if (pFmt->dwMask & PFM_ALIGNMENT)
330     para->member.para.pFmt->wAlignment = pFmt->wAlignment;
331   if (pFmt->dwMask & PFM_STARTINDENT)
332     para->member.para.pFmt->dxStartIndent = pFmt->dxStartIndent;
333   if (pFmt->dwMask & PFM_OFFSET)
334     para->member.para.pFmt->dxOffset = pFmt->dxOffset;
335   if (pFmt->dwMask & PFM_OFFSETINDENT)
336     para->member.para.pFmt->dxStartIndent += pFmt->dxStartIndent;
337     
338   if (pFmt->dwMask & PFM_TABSTOPS)
339   {
340     para->member.para.pFmt->cTabCount = pFmt->cTabCount;
341     memcpy(para->member.para.pFmt->rgxTabs, pFmt->rgxTabs, pFmt->cTabCount*sizeof(int));
342   }
343     
344   /* FIXME to be continued (indents, bulleting and such) */
345
346   if (memcmp(&copy, para->member.para.pFmt, sizeof(PARAFORMAT2)))
347     para->member.para.nFlags |= MEPF_REWRAP;
348 }
349
350
351 void
352 ME_GetSelectionParas(ME_TextEditor *editor, ME_DisplayItem **para, ME_DisplayItem **para_end)
353 {
354   ME_Cursor *pEndCursor = &editor->pCursors[1];
355   
356   *para = ME_GetParagraph(editor->pCursors[0].pRun);
357   *para_end = ME_GetParagraph(editor->pCursors[1].pRun);
358   if ((*para_end)->member.para.nCharOfs < (*para)->member.para.nCharOfs) {
359     ME_DisplayItem *tmp = *para;
360
361     *para = *para_end;
362     *para_end = tmp;
363     pEndCursor = &editor->pCursors[0];
364   }
365   
366   /* selection consists of chars from nFrom up to nTo-1 */
367   if ((*para_end)->member.para.nCharOfs > (*para)->member.para.nCharOfs) {
368     if (!pEndCursor->nOffset) {
369       *para_end = ME_GetParagraph(ME_FindItemBack(pEndCursor->pRun, diRun));
370     }
371   }
372 }
373
374
375 void ME_SetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
376 {
377   ME_DisplayItem *para, *para_end;
378   
379   ME_GetSelectionParas(editor, &para, &para_end);
380  
381   do {
382     ME_SetParaFormat(editor, para, pFmt);
383     if (para == para_end)
384       break;
385     para = para->member.para.next_para;
386   } while(1);
387 }
388
389 void ME_GetParaFormat(ME_TextEditor *editor, ME_DisplayItem *para, PARAFORMAT2 *pFmt)
390 {
391   if (pFmt->cbSize >= sizeof(PARAFORMAT2))
392   {
393     CopyMemory(pFmt, para->member.para.pFmt, sizeof(PARAFORMAT2));
394     return;
395   }
396   CopyMemory(pFmt, para->member.para.pFmt, pFmt->cbSize);  
397 }
398
399 void ME_GetSelectionParaFormat(ME_TextEditor *editor, PARAFORMAT2 *pFmt)
400 {
401   ME_DisplayItem *para, *para_end;
402   PARAFORMAT2 tmp;
403   
404   ME_GetSelectionParas(editor, &para, &para_end);
405   
406   ME_GetParaFormat(editor, para, pFmt);
407   if (para == para_end) return;
408   
409   do {
410     ZeroMemory(&tmp, sizeof(tmp));
411     tmp.cbSize = sizeof(tmp);
412     ME_GetParaFormat(editor, para, &tmp);
413     
414     assert(tmp.dwMask & PFM_ALIGNMENT);    
415     if (pFmt->wAlignment != tmp.wAlignment)
416       pFmt->dwMask &= ~PFM_ALIGNMENT;
417     
418     assert(tmp.dwMask & PFM_STARTINDENT);
419     if (pFmt->dxStartIndent != tmp.dxStartIndent)
420       pFmt->dwMask &= ~PFM_STARTINDENT;
421     
422     assert(tmp.dwMask & PFM_OFFSET);
423     if (pFmt->dxOffset != tmp.dxOffset)
424       pFmt->dwMask &= ~PFM_OFFSET;
425     
426     assert(tmp.dwMask & PFM_TABSTOPS);    
427     if (pFmt->dwMask & PFM_TABSTOPS) {
428       if (pFmt->cTabCount != tmp.cTabCount)
429         pFmt->dwMask &= ~PFM_TABSTOPS;
430       else
431       if (memcmp(pFmt->rgxTabs, tmp.rgxTabs, tmp.cTabCount*sizeof(int)))
432         pFmt->dwMask &= ~PFM_TABSTOPS;
433     }
434     
435     if (para == para_end)
436       return;
437     para = para->member.para.next_para;
438   } while(1);
439 }