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