Add support for more than one sound card.
[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_ENDPARA | MERF_GRAPHICS))
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         ofs += ME_StrLen(p->member.run.strText);
101         break;
102       default:
103         assert(0);
104     }
105   } while(1);
106 }
107
108 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
109 {
110   ME_DisplayItem *pPara;
111   
112   assert(pRun->type == diRun);
113   assert(pRun->member.run.nCharOfs != -1);
114
115   pPara = ME_FindItemBack(pRun, diParagraph);
116   assert(pPara);
117   assert(pPara->type==diParagraph);
118   return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs 
119     + ME_VPosToPos(pRun->member.run.strText, nOfs);
120 }
121
122 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
123 {
124   ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
125 }
126
127 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
128 {
129   ME_DisplayItem *pPara;
130   int nParaOfs;
131   
132   pPara = editor->pBuffer->pFirst->member.para.next_para;
133   assert(pPara);
134   assert(ppRun);
135   assert(pOfs);
136   while (pPara->type == diParagraph)
137   {
138     nParaOfs = pPara->member.para.nCharOfs;
139     assert(nCharOfs >= nParaOfs);
140     
141     if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
142     {
143       *ppRun = ME_FindItemFwd(pPara, diRun);
144       assert(*ppRun);      
145       while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
146       {
147         ME_DisplayItem *pNext = ME_FindItemFwd(*ppRun, diRun);
148         assert(pNext);
149         assert(pNext->type == diRun);
150         if (nCharOfs < nParaOfs + pNext->member.run.nCharOfs) {
151           *pOfs = ME_PosToVPos((*ppRun)->member.run.strText, 
152             nCharOfs - nParaOfs - (*ppRun)->member.run.nCharOfs);
153           return;
154         }
155         *ppRun = pNext;
156       }
157       if (nCharOfs == nParaOfs + (*ppRun)->member.run.nCharOfs) {
158         *pOfs = 0;
159         return;
160       }        
161     }
162     pPara = pPara->member.para.next_para;
163   }
164   *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
165   *pOfs = 0;  
166   assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
167 }
168
169 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
170 {
171   ME_DisplayItem *pNext = p->next;
172   int i;
173   assert(p->type == diRun && pNext->type == diRun);
174   assert(p->member.run.nCharOfs != -1);
175   ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
176
177   for (i=0; i<editor->nCursors; i++) {
178     if (editor->pCursors[i].pRun == pNext) {
179       editor->pCursors[i].pRun = p;
180       editor->pCursors[i].nOffset += ME_StrVLen(p->member.run.strText);
181     }
182   }
183   
184   ME_AppendString(p->member.run.strText, pNext->member.run.strText);
185   ME_Remove(pNext);
186   ME_DestroyDisplayItem(pNext);
187   ME_UpdateRunFlags(editor, &p->member.run);
188   if(TRACE_ON(richedit))
189   {
190     TRACE("Before check after join\n");
191     ME_CheckCharOffsets(editor);
192     TRACE("After check after join\n");
193   }
194 }
195
196 ME_DisplayItem *ME_SplitRun(ME_Context *c, ME_DisplayItem *item, int nVChar)
197 {
198   ME_TextEditor *editor = c->editor;
199   ME_DisplayItem *item2 = NULL;
200   ME_Run *run, *run2;
201   
202   assert(item->member.run.nCharOfs != -1);
203   if(TRACE_ON(richedit))
204   {
205     TRACE("Before check before split\n");
206     ME_CheckCharOffsets(editor);
207     TRACE("After check before split\n");
208   }
209
210   run = &item->member.run;
211
212   TRACE("Before split: %s(%ld, %ld)\n", debugstr_w(run->strText->szData),
213         run->pt.x, run->pt.y);
214
215   item2 = ME_SplitRunSimple(editor, item, nVChar);
216   
217   run2 = &item2->member.run;
218   
219   ME_CalcRunExtent(c, run);
220   ME_CalcRunExtent(c, run2);
221     
222   run2->pt.x = run->pt.x+run->nWidth;
223   run2->pt.y = run->pt.y;
224   
225   if(TRACE_ON(richedit))
226   {
227     TRACE("Before check after split\n");
228     ME_CheckCharOffsets(editor);
229     TRACE("After check after split\n");
230     TRACE("After split: %s(%ld, %ld), %s(%ld, %ld)\n", 
231       debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
232       debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
233   }
234
235   return item2;
236 }
237   
238 /* split a run starting from voffset */
239 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
240 {
241   ME_Run *run = &item->member.run;
242   ME_DisplayItem *item2;
243   ME_Run *run2;
244   int i;
245   assert(nVChar > 0 && nVChar < ME_StrVLen(run->strText));
246   assert(item->type == diRun);
247   assert(!(item->member.run.nFlags & MERF_GRAPHICS));
248   assert(item->member.run.nCharOfs != -1);
249
250   item2 = ME_MakeRun(run->style, 
251       ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
252   
253   item2->member.run.nCharOfs = item->member.run.nCharOfs+
254     ME_VPosToPos(item->member.run.strText, nVChar);
255
256   run2 = &item2->member.run;
257   ME_InsertBefore(item->next, item2);
258   
259   ME_UpdateRunFlags(editor, run);
260   ME_UpdateRunFlags(editor, run2);
261   for (i=0; i<editor->nCursors; i++) {
262     if (editor->pCursors[i].pRun == item && 
263         editor->pCursors[i].nOffset >= nVChar) {
264       assert(item2->type == diRun);
265       editor->pCursors[i].pRun = item2;
266       editor->pCursors[i].nOffset -= nVChar;
267     }
268   }
269   ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
270   return item2;
271 }
272
273 /* split the start and final whitespace into separate runs */
274 /* returns the last run added */
275 /*
276 ME_DisplayItem *ME_SplitFurther(ME_TextEditor *editor, ME_DisplayItem *item)
277 {
278   int i, nVLen, nChanged;
279   assert(item->type == diRun);
280   assert(!(item->member.run.nFlags & MERF_GRAPHICS));
281   return item;
282 }
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 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
297 {
298   ME_Cursor tmp;
299   ME_DisplayItem *pDI;
300   ME_UndoItem *pUI;
301   
302   assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
303   
304   pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
305   if (pUI) {
306     pUI->nStart = nCharOfs;
307     pUI->nLen = pItem->member.run.strText->nLen;
308   }
309   ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
310   if (tmp.nOffset) {
311     tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
312     tmp.nOffset = 0;
313   }
314   pDI = ME_MakeRun(pItem->member.run.style, ME_StrDup(pItem->member.run.strText), pItem->member.run.nFlags);
315   pDI->member.run.nCharOfs = tmp.pRun->member.run.nCharOfs;
316   ME_InsertBefore(tmp.pRun, pDI);
317   TRACE("Shift length:%d\n", pDI->member.run.strText->nLen);
318   ME_PropagateCharOffset(tmp.pRun, pDI->member.run.strText->nLen);
319   ME_GetParagraph(tmp.pRun)->member.para.nFlags |= MEPF_REWRAP;
320   
321   return pDI;
322 }
323
324 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
325 {
326   assert(run->nCharOfs != -1);
327   if (ME_IsSplitable(run->strText))
328     run->nFlags |= MERF_SPLITTABLE;
329   else
330     run->nFlags &= ~MERF_SPLITTABLE;
331     
332   if (!(run->nFlags & MERF_GRAPHICS)) {
333     if (ME_IsWhitespaces(run->strText))
334       run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
335     else
336     {
337       run->nFlags &= ~MERF_WHITESPACE;
338     
339       if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
340         run->nFlags |= MERF_STARTWHITE;
341       else
342         run->nFlags &= ~MERF_STARTWHITE;
343     
344       if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
345         run->nFlags |= MERF_ENDWHITE;
346       else
347         run->nFlags &= ~MERF_ENDWHITE;
348     }
349   }
350   else
351     run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
352 }
353
354 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
355 {
356   assert(run->nFlags & MERF_GRAPHICS);
357   pSize->cx = 64;
358   pSize->cy = 64;
359 }
360
361 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Run *run)
362 {
363   int fit = 0;
364   HGDIOBJ hOldFont;
365   HDC hDC;
366   SIZE sz;
367   if (!run->strText->nLen)
368     return 0;
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_GRAPHICS)
397   {
398     SIZE sz;
399     ME_GetGraphicsSize(editor, run, &sz);
400     if (cx < sz.cx/2)
401       return 0;
402     return 1;
403   }
404
405   hDC = GetDC(editor->hWnd);
406   hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
407   GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen, 
408     cx, &fit, NULL, &sz);
409   if (fit != run->strText->nLen)
410   {
411     int chars = 1;
412     
413     GetTextExtentPoint32W(hDC, run->strText->szData, fit, &sz2);
414     fit1 = ME_StrRelPos(run->strText, fit, &chars);
415     GetTextExtentPoint32W(hDC, run->strText->szData, fit1, &sz3);
416     if (cx >= (sz2.cx+sz3.cx)/2)
417       fit = fit1;
418   }
419   ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);  
420   ReleaseDC(editor->hWnd, hDC);
421   return fit;
422 }
423
424 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
425 {
426   SIZE size;
427   HDC hDC = GetDC(editor->hWnd);
428   HGDIOBJ hOldFont;
429   
430   if (pRun->nFlags & MERF_GRAPHICS)
431   {
432     if (!nOffset) return 0;
433     ME_GetGraphicsSize(editor, pRun, &size);
434     return 1;
435   }
436   hOldFont = ME_SelectStyleFont(editor, hDC, pRun->style);
437   GetTextExtentPoint32W(hDC, pRun->strText->szData, nOffset, &size);
438   ME_UnselectStyleFont(editor, hDC, pRun->style, hOldFont);  
439   ReleaseDC(editor->hWnd, hDC);
440   return size.cx;
441 }
442
443 void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, 
444   SIZE *size)
445 {
446   HDC hDC = c->hDC;
447   HGDIOBJ hOldFont;
448   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
449   GetTextExtentPoint32W(hDC, szText, nChars, size);
450   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);  
451 }
452
453 SIZE ME_GetRunSize(ME_Context *c, ME_Run *run, int nLen)
454 {
455   SIZE size;
456   int nMaxLen = ME_StrVLen(run->strText);
457
458   if (nLen>nMaxLen)
459     nLen = nMaxLen;
460     
461   if (run->nFlags & MERF_GRAPHICS)
462   {
463     ME_GetGraphicsSize(c->editor, run, &size);
464     return size;
465   }
466
467   ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
468
469   return size;
470 }
471
472 void ME_CalcRunExtent(ME_Context *c, ME_Run *run)
473 {
474   SIZE size;
475   int nEnd = ME_StrVLen(run->strText);
476   
477   if (run->nFlags & MERF_GRAPHICS) {
478     ME_GetGraphicsSize(c->editor, run, &size);
479     run->nWidth = size.cx;
480     run->nAscent = size.cy;
481     run->nDescent = 0;
482     return;
483   }
484   ME_GetTextExtent(c, run->strText->szData, nEnd, run->style, &size);
485   run->nWidth = size.cx;
486   run->nAscent = run->style->tm.tmAscent;
487   run->nDescent = run->style->tm.tmDescent;
488 }
489
490 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
491 {
492   assert(para->type == diParagraph);
493   /* FIXME */
494 }
495
496 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
497 {
498   int nFrom, nTo;
499   ME_GetSelection(editor, &nFrom, &nTo);
500   if (nFrom == nTo)
501   {
502     ME_Style *s;
503     if (!editor->pBuffer->pCharStyle)
504       editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
505     s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
506     ME_ReleaseStyle(editor->pBuffer->pCharStyle);
507     editor->pBuffer->pCharStyle = s;
508   }
509   else
510     ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
511 }
512
513 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
514 {
515   ME_Cursor tmp, tmp2;
516   ME_DisplayItem *para;
517   
518   ME_CursorFromCharOfs(editor, nOfs, &tmp);
519   if (tmp.nOffset)
520     tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
521
522   ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
523   if (tmp2.nOffset)
524     tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
525
526   para = ME_GetParagraph(tmp.pRun);
527   para->member.para.nFlags |= MEPF_REWRAP;
528     
529   while(tmp.pRun != tmp2.pRun)
530   {
531     ME_UndoItem *undo = NULL;
532     ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
533     /* ME_DumpStyle(new_style); */
534     undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
535     if (undo) {
536       undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
537       undo->nLen = tmp.pRun->member.run.strText->nLen;
538       undo->di.member.ustyle = tmp.pRun->member.run.style;
539       /* we'd have to addref undo..ustyle and release tmp...style
540          but they'd cancel each other out so we can do nothing instead */
541     }
542     else
543       ME_ReleaseStyle(tmp.pRun->member.run.style);
544     tmp.pRun->member.run.style = new_style;
545     tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
546     if (tmp.pRun->type == diParagraph)
547     {
548       para = tmp.pRun;
549       tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
550       if (tmp.pRun != tmp2.pRun)
551         para->member.para.nFlags |= MEPF_REWRAP;
552     }
553     assert(tmp.pRun);
554   }
555 }
556
557 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
558 {
559   ME_Style *style; 
560   ME_UndoItem *undo;
561   
562   assert(mod->cbSize == sizeof(CHARFORMAT2W));
563   undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
564   if (undo) {
565     undo->nStart = -1;
566     undo->nLen = -1;
567     undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
568     ME_AddRefStyle(undo->di.member.ustyle);
569   }
570   style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
571   editor->pBuffer->pDefaultStyle->fmt = style->fmt;
572   editor->pBuffer->pDefaultStyle->tm = style->tm;
573   ME_ReleaseStyle(style);
574   ME_MarkAllForWrapping(editor);
575   /*  pcf = editor->pBuffer->pDefaultStyle->fmt; */
576 }
577
578 void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
579 {
580   ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
581 }
582
583 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
584 {
585   int nFrom, nTo;
586   ME_GetSelection(editor, &nFrom, &nTo);
587   ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
588 }
589
590 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
591 {
592   int nFrom, nTo;
593   ME_GetSelection(editor, &nFrom, &nTo);
594   if (nFrom == nTo && editor->pBuffer->pCharStyle)
595   {
596     ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
597     return;
598   }
599   ME_GetCharFormat(editor, nFrom, nTo, pFmt);
600 }
601
602 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
603 {
604   ME_DisplayItem *run, *run_end;
605   int nOffset, nOffset2;
606   CHARFORMAT2W tmp;
607   
608   if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
609     nTo--;
610   
611   ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
612   if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
613   {
614     if (!nOffset)
615     {
616       ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
617       if (tmp_run->type == diRun) {
618         ME_GetRunCharFormat(editor, tmp_run, pFmt);
619         return;
620       }
621     }
622     ME_GetRunCharFormat(editor, run, pFmt);
623     return;
624   }
625   ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
626   
627   ME_GetRunCharFormat(editor, run, pFmt);
628   
629   if (run == run_end) return;
630   
631   do {
632     /* FIXME add more style feature comparisons */
633     int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR;
634     int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
635
636     run = ME_FindItemFwd(run, diRun);
637     
638     ZeroMemory(&tmp, sizeof(tmp));
639     tmp.cbSize = sizeof(tmp);
640     ME_GetRunCharFormat(editor, run, &tmp);
641     
642     assert((tmp.dwMask & nAttribs) == nAttribs);
643     assert((tmp.dwMask & nEffects) == nEffects);
644     /* reset flags that differ */
645
646     if (pFmt->yHeight != tmp.yHeight)
647       pFmt->dwMask &= ~CFM_SIZE;
648     if (pFmt->dwMask & CFM_FACE)
649     {
650       if (!(tmp.dwMask & CFM_FACE))
651         pFmt->dwMask &= ~CFM_FACE;
652       else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName))
653         pFmt->dwMask &= ~CFM_FACE;
654     }
655     if (pFmt->yHeight != tmp.yHeight)
656       pFmt->dwMask &= ~CFM_SIZE;
657     if (pFmt->dwMask & CFM_COLOR)
658     {
659       if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
660       {
661         if (pFmt->crTextColor != tmp.crTextColor)
662           pFmt->dwMask &= ~CFM_COLOR;
663       }
664     }
665       
666     pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
667     
668   } while(run != run_end);
669 }