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