Make more arrays const.
[wine] / dlls / riched20 / run.c
1 /*
2  * RichEdit - operations on runs (diRun, rectangular pieces of paragraphs).
3  * Splitting/joining runs. Adjusting offsets after deleting/adding content.
4  * Character/pixel conversions.
5  *
6  * Copyright 2004 by Krzysztof Foltman
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "editor.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
27 WINE_DECLARE_DEBUG_CHANNEL(richedit_lists);
28
29 int ME_CanJoinRuns(ME_Run *run1, ME_Run *run2)
30 {
31   if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
32     return 0;
33   if (run1->style != run2->style)
34     return 0;
35   if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
36     return 0;
37   return 1;
38 }
39
40 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
41 {
42   p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
43   assert(p);
44   ME_PropagateCharOffset(p, shift);
45 }
46
47 void ME_PropagateCharOffset(ME_DisplayItem *p, int shift)
48 {
49   if (p->type == diRun) /* propagate in all runs in this para */
50   {
51     TRACE("PropagateCharOffset(%s, %d)\n", debugstr_w(p->member.run.strText->szData), shift);
52     do {
53       p->member.run.nCharOfs += shift;
54       assert(p->member.run.nCharOfs >= 0);
55       p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
56     } while(p->type == diRun);
57   }
58   if (p->type == diParagraph) /* propagate in all next paras */
59   {
60     do {
61       p->member.para.nCharOfs += shift;
62       assert(p->member.para.nCharOfs >= 0);
63       p = p->member.para.next_para;
64     } while(p->type == diParagraph);
65   }
66   if (p->type == diTextEnd)
67   {
68     p->member.para.nCharOfs += shift;
69     assert(p->member.para.nCharOfs >= 0);
70   }
71 }
72
73 void ME_CheckCharOffsets(ME_TextEditor *editor)
74 {
75   ME_DisplayItem *p = editor->pBuffer->pFirst;
76   int ofs = 0, ofsp = 0;
77   if(TRACE_ON(richedit_lists))
78   {
79     TRACE_(richedit_lists)("---\n");
80     ME_DumpDocument(editor->pBuffer);
81   }
82   do {
83     p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
84     switch(p->type) {
85       case diTextEnd:
86         TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
87         assert(ofsp+ofs == p->member.para.nCharOfs);
88         return;
89       case diParagraph:
90         TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
91         assert(ofsp+ofs == p->member.para.nCharOfs);
92         ofsp = p->member.para.nCharOfs;
93         ofs = 0;
94         break;
95       case diRun:
96         TRACE_(richedit_check)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = \"%s\", flags=%08x, fx&mask = %08lx\n",
97           p->member.run.nCharOfs, p->member.run.nCharOfs+ofsp, ofsp+ofs,
98           p->member.run.strText->nLen, debugstr_w(p->member.run.strText->szData),
99           p->member.run.nFlags,
100           p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
101         assert(ofs == p->member.run.nCharOfs);
102         if (p->member.run.nFlags & MERF_ENDPARA)
103           ofs += (editor->bEmulateVersion10 ? 2 : 1);
104         else
105           ofs += ME_StrLen(p->member.run.strText);
106         break;
107       default:
108         assert(0);
109     }
110   } while(1);
111 }
112
113 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
114 {
115   ME_DisplayItem *pPara;
116
117   assert(pRun->type == diRun);
118   assert(pRun->member.run.nCharOfs != -1);
119
120   pPara = ME_FindItemBack(pRun, diParagraph);
121   assert(pPara);
122   assert(pPara->type==diParagraph);
123   return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs
124     + ME_VPosToPos(pRun->member.run.strText, nOfs);
125 }
126
127 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
128 {
129   ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
130 }
131
132 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
133 {
134   ME_DisplayItem *pPara;
135   int nParaOfs;
136
137   pPara = editor->pBuffer->pFirst->member.para.next_para;
138   assert(pPara);
139   assert(ppRun);
140   assert(pOfs);
141   while (pPara->type == diParagraph)
142   {
143     nParaOfs = pPara->member.para.nCharOfs;
144     assert(nCharOfs >= nParaOfs);
145
146     if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
147     {
148       int eollen = 1;
149       *ppRun = ME_FindItemFwd(pPara, diRun);
150       assert(*ppRun);
151       while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
152       {
153         ME_DisplayItem *pNext = ME_FindItemFwd(*ppRun, diRun);
154         assert(pNext);
155         assert(pNext->type == diRun);
156         if (nCharOfs < nParaOfs + pNext->member.run.nCharOfs) {
157           *pOfs = ME_PosToVPos((*ppRun)->member.run.strText,
158             nCharOfs - nParaOfs - (*ppRun)->member.run.nCharOfs);
159           return;
160         }
161         *ppRun = pNext;
162       }
163       /* the handling of bEmulateVersion10 may be a source of many bugs, I'm afraid */
164       eollen = (editor->bEmulateVersion10 ? 2 : 1);
165       if (nCharOfs >= nParaOfs + (*ppRun)->member.run.nCharOfs &&
166         nCharOfs < nParaOfs + (*ppRun)->member.run.nCharOfs + eollen) {
167         *pOfs = 0;
168         return;
169       }
170     }
171     pPara = pPara->member.para.next_para;
172   }
173   *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
174   *pOfs = 0;
175   assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
176 }
177
178 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
179 {
180   ME_DisplayItem *pNext = p->next;
181   int i;
182   assert(p->type == diRun && pNext->type == diRun);
183   assert(p->member.run.nCharOfs != -1);
184   ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
185
186   if (editor->bCaretAtEnd && editor->pCursors[0].pRun == pNext)
187     editor->bCaretAtEnd = FALSE;
188   for (i=0; i<editor->nCursors; i++) {
189     if (editor->pCursors[i].pRun == pNext) {
190       editor->pCursors[i].pRun = p;
191       editor->pCursors[i].nOffset += ME_StrVLen(p->member.run.strText);
192     }
193   }
194
195   ME_AppendString(p->member.run.strText, pNext->member.run.strText);
196   ME_Remove(pNext);
197   ME_DestroyDisplayItem(pNext);
198   ME_UpdateRunFlags(editor, &p->member.run);
199   if(TRACE_ON(richedit))
200   {
201     TRACE("Before check after join\n");
202     ME_CheckCharOffsets(editor);
203     TRACE("After check after join\n");
204   }
205 }
206
207 ME_DisplayItem *ME_SplitRun(ME_Context *c, ME_DisplayItem *item, int nVChar)
208 {
209   ME_TextEditor *editor = c->editor;
210   ME_DisplayItem *item2 = NULL;
211   ME_Run *run, *run2;
212   ME_Paragraph *para = &ME_GetParagraph(item)->member.para;
213
214   assert(item->member.run.nCharOfs != -1);
215   if(TRACE_ON(richedit))
216   {
217     TRACE("Before check before split\n");
218     ME_CheckCharOffsets(editor);
219     TRACE("After check before split\n");
220   }
221
222   run = &item->member.run;
223
224   TRACE("Before split: %s(%ld, %ld)\n", debugstr_w(run->strText->szData),
225         run->pt.x, run->pt.y);
226
227   item2 = ME_SplitRunSimple(editor, item, nVChar);
228
229   run2 = &item2->member.run;
230
231   ME_CalcRunExtent(c, para, run);
232   ME_CalcRunExtent(c, para, run2);
233
234   run2->pt.x = run->pt.x+run->nWidth;
235   run2->pt.y = run->pt.y;
236
237   if(TRACE_ON(richedit))
238   {
239     TRACE("Before check after split\n");
240     ME_CheckCharOffsets(editor);
241     TRACE("After check after split\n");
242     TRACE("After split: %s(%ld, %ld), %s(%ld, %ld)\n",
243       debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
244       debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
245   }
246
247   return item2;
248 }
249
250 /* split a run starting from voffset */
251 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
252 {
253   ME_Run *run = &item->member.run;
254   ME_DisplayItem *item2;
255   ME_Run *run2;
256   int i;
257   assert(nVChar > 0 && nVChar < ME_StrVLen(run->strText));
258   assert(item->type == diRun);
259   assert(!(item->member.run.nFlags & (MERF_GRAPHICS | MERF_TAB)));
260   assert(item->member.run.nCharOfs != -1);
261
262   item2 = ME_MakeRun(run->style,
263       ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
264
265   item2->member.run.nCharOfs = item->member.run.nCharOfs+
266     ME_VPosToPos(item->member.run.strText, nVChar);
267
268   run2 = &item2->member.run;
269   ME_InsertBefore(item->next, item2);
270
271   ME_UpdateRunFlags(editor, run);
272   ME_UpdateRunFlags(editor, run2);
273   for (i=0; i<editor->nCursors; i++) {
274     if (editor->pCursors[i].pRun == item &&
275         editor->pCursors[i].nOffset >= nVChar) {
276       assert(item2->type == diRun);
277       editor->pCursors[i].pRun = item2;
278       editor->pCursors[i].nOffset -= nVChar;
279     }
280   }
281   ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
282   return item2;
283 }
284
285 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
286 {
287   ME_DisplayItem *item = ME_MakeDI(diRun);
288   item->member.run.style = s;
289   item->member.run.strText = strData;
290   item->member.run.nFlags = nFlags;
291   item->member.run.nCharOfs = -1;
292   ME_AddRefStyle(s);
293   return item;
294 }
295
296
297 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
298 {
299   ME_Cursor tmp;
300   ME_DisplayItem *pDI;
301
302   assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
303
304   ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
305   pDI = ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
306                              pItem->member.run.strText->szData,
307                              pItem->member.run.strText->nLen,
308                              pItem->member.run.nFlags);
309   
310   return pDI;
311 }
312
313 ME_DisplayItem *
314 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
315                      const WCHAR *str, int len, int flags)
316 {
317   ME_DisplayItem *pDI;
318   ME_UndoItem *pUI;
319   
320   if (cursor->nOffset) {
321     cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
322     cursor->nOffset = 0;
323   }
324   
325   pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
326   if (pUI) {
327     pUI->nStart = cursor->pRun->member.run.nCharOfs;
328     pUI->nLen = len;
329   }
330   
331   pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
332   pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
333   ME_InsertBefore(cursor->pRun, pDI);
334   TRACE("Shift length:%d\n", len);
335   ME_PropagateCharOffset(cursor->pRun, len);
336   ME_GetParagraph(cursor->pRun)->member.para.nFlags |= MEPF_REWRAP;
337   return pDI;
338 }
339
340 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
341 {
342   assert(run->nCharOfs != -1);
343   if (ME_IsSplitable(run->strText))
344     run->nFlags |= MERF_SPLITTABLE;
345   else
346     run->nFlags &= ~MERF_SPLITTABLE;
347
348   if (!(run->nFlags & MERF_NOTEXT)) {
349     if (ME_IsWhitespaces(run->strText))
350       run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
351     else
352     {
353       run->nFlags &= ~MERF_WHITESPACE;
354
355       if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
356         run->nFlags |= MERF_STARTWHITE;
357       else
358         run->nFlags &= ~MERF_STARTWHITE;
359
360       if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
361         run->nFlags |= MERF_ENDWHITE;
362       else
363         run->nFlags &= ~MERF_ENDWHITE;
364     }
365   }
366   else
367     run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
368 }
369
370 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
371 {
372   assert(run->nFlags & MERF_GRAPHICS);
373   pSize->cx = 64;
374   pSize->cy = 64;
375 }
376
377 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Paragraph *para, ME_Run *run)
378 {
379   int fit = 0;
380   HGDIOBJ hOldFont;
381   HDC hDC;
382   SIZE sz;
383   if (!run->strText->nLen)
384     return 0;
385
386   if (run->nFlags & MERF_TAB)
387   {
388     if (cx < run->nWidth/2)
389       return 0;
390     return 1;
391   }
392   if (run->nFlags & MERF_GRAPHICS)
393   {
394     SIZE sz;
395     ME_GetGraphicsSize(editor, run, &sz);
396     if (cx < sz.cx)
397       return 0;
398     return 1;
399   }
400   hDC = GetDC(editor->hWnd);
401   hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
402   GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
403     cx, &fit, NULL, &sz);
404   ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
405   ReleaseDC(editor->hWnd, hDC);
406   return fit;
407 }
408
409 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
410 {
411   int fit = 0, fit1 = 0;
412   HGDIOBJ hOldFont;
413   HDC hDC;
414   SIZE sz, sz2, sz3;
415   if (!run->strText->nLen)
416     return 0;
417
418   if (run->nFlags & MERF_TAB)
419   {
420     if (cx < run->nWidth/2)
421       return 0;
422     return 1;
423   }
424   if (run->nFlags & MERF_GRAPHICS)
425   {
426     SIZE sz;
427     ME_GetGraphicsSize(editor, run, &sz);
428     if (cx < sz.cx/2)
429       return 0;
430     return 1;
431   }
432
433   hDC = GetDC(editor->hWnd);
434   hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
435   GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
436     cx, &fit, NULL, &sz);
437   if (fit != run->strText->nLen)
438   {
439     int chars = 1;
440
441     GetTextExtentPoint32W(hDC, run->strText->szData, fit, &sz2);
442     fit1 = ME_StrRelPos(run->strText, fit, &chars);
443     GetTextExtentPoint32W(hDC, run->strText->szData, fit1, &sz3);
444     if (cx >= (sz2.cx+sz3.cx)/2)
445       fit = fit1;
446   }
447   ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
448   ReleaseDC(editor->hWnd, hDC);
449   return fit;
450 }
451
452 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
453 {
454   SIZE size;
455   HDC hDC = GetDC(editor->hWnd);
456   HGDIOBJ hOldFont;
457
458   if (pRun->nFlags & MERF_GRAPHICS)
459   {
460     if (!nOffset) return 0;
461     ME_GetGraphicsSize(editor, pRun, &size);
462     return 1;
463   }
464   hOldFont = ME_SelectStyleFont(editor, hDC, pRun->style);
465   GetTextExtentPoint32W(hDC, pRun->strText->szData, nOffset, &size);
466   ME_UnselectStyleFont(editor, hDC, pRun->style, hOldFont);
467   ReleaseDC(editor->hWnd, hDC);
468   return size.cx;
469 }
470
471 void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s,
472   SIZE *size)
473 {
474   HDC hDC = c->hDC;
475   HGDIOBJ hOldFont;
476   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
477   GetTextExtentPoint32W(hDC, szText, nChars, size);
478   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
479 }
480
481 SIZE ME_GetRunSizeCommon(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen, int *pAscent, int *pDescent)
482 {
483   SIZE size;
484   int nMaxLen = ME_StrVLen(run->strText);
485
486   if (nLen>nMaxLen)
487     nLen = nMaxLen;
488
489   /* FIXME the following call also ensures that TEXTMETRIC structure is filled
490    * this is wasteful for graphics and TAB runs, but that shouldn't matter
491    * in practice
492    */
493   ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
494   *pAscent = run->style->tm.tmAscent;
495   *pDescent = run->style->tm.tmDescent;
496   size.cy = *pAscent + *pDescent;
497
498   if (run->nFlags & MERF_TAB)
499   {
500     int pos = 0, i = 0, ppos;
501     int lpsx = GetDeviceCaps(c->hDC, LOGPIXELSX);
502     PARAFORMAT2 *pFmt = para->pFmt;
503     do {
504       if (i < pFmt->cTabCount)
505       {
506         pos = pFmt->rgxTabs[i]&0x00FFFFFF;
507         i++;
508       }
509       else
510       {
511         pos += 720-(pos%720);
512       }
513       ppos = pos*lpsx/1440;
514       if (ppos>run->pt.x) {
515         size.cx = ppos - run->pt.x;
516         break;
517       }
518     } while(1);
519     size.cy = *pAscent + *pDescent;
520     return size;
521   }
522   if (run->nFlags & MERF_GRAPHICS)
523   {
524     ME_GetGraphicsSize(c->editor, run, &size);
525     if (size.cy > *pAscent)
526       *pAscent = size.cy;
527     /* descent is unchanged */
528     return size;
529   }
530
531   return size;
532 }
533
534 SIZE ME_GetRunSize(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen)
535 {
536   int asc, desc;
537   return ME_GetRunSizeCommon(c, para, run, nLen, &asc, &desc);
538 }
539
540 void ME_CalcRunExtent(ME_Context *c, ME_Paragraph *para, ME_Run *run)
541 {
542   int nEnd = ME_StrVLen(run->strText);
543   SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, &run->nAscent, &run->nDescent);
544   run->nWidth = size.cx;
545   if (!size.cx)
546     WARN("size.cx == 0\n");
547 }
548
549 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
550 {
551   assert(para->type == diParagraph);
552   /* FIXME */
553 }
554
555 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
556 {
557   int nFrom, nTo;
558   ME_GetSelection(editor, &nFrom, &nTo);
559   if (nFrom == nTo)
560   {
561     ME_Style *s;
562     if (!editor->pBuffer->pCharStyle)
563       editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
564     s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
565     ME_ReleaseStyle(editor->pBuffer->pCharStyle);
566     editor->pBuffer->pCharStyle = s;
567   }
568   else
569     ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
570 }
571
572 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
573 {
574   ME_Cursor tmp, tmp2;
575   ME_DisplayItem *para;
576
577   ME_CursorFromCharOfs(editor, nOfs, &tmp);
578   if (tmp.nOffset)
579     tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
580
581   ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
582   if (tmp2.nOffset)
583     tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
584
585   para = ME_GetParagraph(tmp.pRun);
586   para->member.para.nFlags |= MEPF_REWRAP;
587
588   while(tmp.pRun != tmp2.pRun)
589   {
590     ME_UndoItem *undo = NULL;
591     ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
592     /* ME_DumpStyle(new_style); */
593     undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
594     if (undo) {
595       undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
596       undo->nLen = tmp.pRun->member.run.strText->nLen;
597       undo->di.member.ustyle = tmp.pRun->member.run.style;
598       /* we'd have to addref undo..ustyle and release tmp...style
599          but they'd cancel each other out so we can do nothing instead */
600     }
601     else
602       ME_ReleaseStyle(tmp.pRun->member.run.style);
603     tmp.pRun->member.run.style = new_style;
604     tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
605     if (tmp.pRun->type == diParagraph)
606     {
607       para = tmp.pRun;
608       tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
609       if (tmp.pRun != tmp2.pRun)
610         para->member.para.nFlags |= MEPF_REWRAP;
611     }
612     assert(tmp.pRun);
613   }
614 }
615
616 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
617 {
618   ME_Style *style;
619   ME_UndoItem *undo;
620
621   assert(mod->cbSize == sizeof(CHARFORMAT2W));
622   undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
623   if (undo) {
624     undo->nStart = -1;
625     undo->nLen = -1;
626     undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
627     ME_AddRefStyle(undo->di.member.ustyle);
628   }
629   style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
630   editor->pBuffer->pDefaultStyle->fmt = style->fmt;
631   editor->pBuffer->pDefaultStyle->tm = style->tm;
632   ME_ReleaseStyle(style);
633   ME_MarkAllForWrapping(editor);
634   /*  pcf = editor->pBuffer->pDefaultStyle->fmt; */
635 }
636
637 void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
638 {
639   ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
640 }
641
642 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
643 {
644   int nFrom, nTo;
645   ME_GetSelection(editor, &nFrom, &nTo);
646   ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
647 }
648
649 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
650 {
651   int nFrom, nTo;
652   ME_GetSelection(editor, &nFrom, &nTo);
653   if (nFrom == nTo && editor->pBuffer->pCharStyle)
654   {
655     ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
656     return;
657   }
658   ME_GetCharFormat(editor, nFrom, nTo, pFmt);
659 }
660
661 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
662 {
663   ME_DisplayItem *run, *run_end;
664   int nOffset, nOffset2;
665   CHARFORMAT2W tmp;
666
667   ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
668   if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
669   {
670     if (!nOffset)
671     {
672       ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
673       if (tmp_run->type == diRun) {
674         ME_GetRunCharFormat(editor, tmp_run, pFmt);
675         return;
676       }
677     }
678     ME_GetRunCharFormat(editor, run, pFmt);
679     return;
680   }
681   
682   if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
683     nTo--;
684   ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
685
686   ME_GetRunCharFormat(editor, run, pFmt);
687
688   if (run == run_end) return;
689
690   do {
691     /* FIXME add more style feature comparisons */
692     int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR;
693     int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
694
695     run = ME_FindItemFwd(run, diRun);
696
697     ZeroMemory(&tmp, sizeof(tmp));
698     tmp.cbSize = sizeof(tmp);
699     ME_GetRunCharFormat(editor, run, &tmp);
700
701     assert((tmp.dwMask & nAttribs) == nAttribs);
702     assert((tmp.dwMask & nEffects) == nEffects);
703     /* reset flags that differ */
704
705     if (pFmt->yHeight != tmp.yHeight)
706       pFmt->dwMask &= ~CFM_SIZE;
707     if (pFmt->dwMask & CFM_FACE)
708     {
709       if (!(tmp.dwMask & CFM_FACE))
710         pFmt->dwMask &= ~CFM_FACE;
711       else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName))
712         pFmt->dwMask &= ~CFM_FACE;
713     }
714     if (pFmt->yHeight != tmp.yHeight)
715       pFmt->dwMask &= ~CFM_SIZE;
716     if (pFmt->dwMask & CFM_COLOR)
717     {
718       if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
719       {
720         if (pFmt->crTextColor != tmp.crTextColor)
721           pFmt->dwMask &= ~CFM_COLOR;
722       }
723     }
724
725     pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
726
727   } while(run != run_end);
728 }