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