riched20: Added support for hidden text.
[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
344   if (RUN_IS_HIDDEN(run))
345     run->nFlags |= MERF_HIDDEN;
346   else
347     run->nFlags &= ~MERF_HIDDEN;
348
349   if (ME_IsSplitable(run->strText))
350     run->nFlags |= MERF_SPLITTABLE;
351   else
352     run->nFlags &= ~MERF_SPLITTABLE;
353
354   if (!(run->nFlags & MERF_NOTEXT)) {
355     if (ME_IsWhitespaces(run->strText))
356       run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
357     else
358     {
359       run->nFlags &= ~MERF_WHITESPACE;
360
361       if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
362         run->nFlags |= MERF_STARTWHITE;
363       else
364         run->nFlags &= ~MERF_STARTWHITE;
365
366       if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
367         run->nFlags |= MERF_ENDWHITE;
368       else
369         run->nFlags &= ~MERF_ENDWHITE;
370     }
371   }
372   else
373     run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
374 }
375
376 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
377 {
378   assert(run->nFlags & MERF_GRAPHICS);
379   pSize->cx = 64;
380   pSize->cy = 64;
381 }
382
383 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Paragraph *para, ME_Run *run)
384 {
385   int fit = 0;
386   HGDIOBJ hOldFont;
387   HDC hDC;
388   SIZE sz;
389   if (!run->strText->nLen)
390     return 0;
391
392   if (run->nFlags & MERF_TAB)
393   {
394     if (cx < run->nWidth/2)
395       return 0;
396     return 1;
397   }
398   if (run->nFlags & MERF_GRAPHICS)
399   {
400     SIZE sz;
401     ME_GetGraphicsSize(editor, run, &sz);
402     if (cx < sz.cx)
403       return 0;
404     return 1;
405   }
406   hDC = GetDC(editor->hWnd);
407   hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
408   GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
409     cx, &fit, NULL, &sz);
410   ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
411   ReleaseDC(editor->hWnd, hDC);
412   return fit;
413 }
414
415 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
416 {
417   int fit = 0, fit1 = 0;
418   HGDIOBJ hOldFont;
419   HDC hDC;
420   SIZE sz, sz2, sz3;
421   if (!run->strText->nLen)
422     return 0;
423
424   if (run->nFlags & MERF_TAB)
425   {
426     if (cx < run->nWidth/2)
427       return 0;
428     return 1;
429   }
430   if (run->nFlags & MERF_GRAPHICS)
431   {
432     SIZE sz;
433     ME_GetGraphicsSize(editor, run, &sz);
434     if (cx < sz.cx/2)
435       return 0;
436     return 1;
437   }
438
439   hDC = GetDC(editor->hWnd);
440   hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
441   GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
442     cx, &fit, NULL, &sz);
443   if (fit != run->strText->nLen)
444   {
445     int chars = 1;
446
447     GetTextExtentPoint32W(hDC, run->strText->szData, fit, &sz2);
448     fit1 = ME_StrRelPos(run->strText, fit, &chars);
449     GetTextExtentPoint32W(hDC, run->strText->szData, fit1, &sz3);
450     if (cx >= (sz2.cx+sz3.cx)/2)
451       fit = fit1;
452   }
453   ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
454   ReleaseDC(editor->hWnd, hDC);
455   return fit;
456 }
457
458 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
459 {
460   SIZE size;
461   HDC hDC = GetDC(editor->hWnd);
462   HGDIOBJ hOldFont;
463
464   if (pRun->nFlags & MERF_GRAPHICS)
465   {
466     if (!nOffset) return 0;
467     ME_GetGraphicsSize(editor, pRun, &size);
468     return 1;
469   }
470   hOldFont = ME_SelectStyleFont(editor, hDC, pRun->style);
471   GetTextExtentPoint32W(hDC, pRun->strText->szData, nOffset, &size);
472   ME_UnselectStyleFont(editor, hDC, pRun->style, hOldFont);
473   ReleaseDC(editor->hWnd, hDC);
474   return size.cx;
475 }
476
477 void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s,
478   SIZE *size)
479 {
480   HDC hDC = c->hDC;
481   HGDIOBJ hOldFont;
482   hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
483   GetTextExtentPoint32W(hDC, szText, nChars, size);
484   ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
485 }
486
487 SIZE ME_GetRunSizeCommon(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen, int *pAscent, int *pDescent)
488 {
489   SIZE size;
490   int nMaxLen = ME_StrVLen(run->strText);
491
492   if (nLen>nMaxLen)
493     nLen = nMaxLen;
494
495   /* FIXME the following call also ensures that TEXTMETRIC structure is filled
496    * this is wasteful for graphics and TAB runs, but that shouldn't matter
497    * in practice
498    */
499   ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
500   *pAscent = run->style->tm.tmAscent;
501   *pDescent = run->style->tm.tmDescent;
502   size.cy = *pAscent + *pDescent;
503
504   if (run->nFlags & MERF_TAB)
505   {
506     int pos = 0, i = 0, ppos;
507     int lpsx = GetDeviceCaps(c->hDC, LOGPIXELSX);
508     PARAFORMAT2 *pFmt = para->pFmt;
509     do {
510       if (i < pFmt->cTabCount)
511       {
512         pos = pFmt->rgxTabs[i]&0x00FFFFFF;
513         i++;
514       }
515       else
516       {
517         pos += 720-(pos%720);
518       }
519       ppos = pos*lpsx/1440;
520       if (ppos>run->pt.x) {
521         size.cx = ppos - run->pt.x;
522         break;
523       }
524     } while(1);
525     size.cy = *pAscent + *pDescent;
526     return size;
527   }
528   if (run->nFlags & MERF_GRAPHICS)
529   {
530     ME_GetGraphicsSize(c->editor, run, &size);
531     if (size.cy > *pAscent)
532       *pAscent = size.cy;
533     /* descent is unchanged */
534     return size;
535   }
536
537   return size;
538 }
539
540 SIZE ME_GetRunSize(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen)
541 {
542   int asc, desc;
543   return ME_GetRunSizeCommon(c, para, run, nLen, &asc, &desc);
544 }
545
546 void ME_CalcRunExtent(ME_Context *c, ME_Paragraph *para, ME_Run *run)
547 {
548   if (run->nFlags & MERF_HIDDEN)
549     run->nWidth = 0;
550   else
551   {
552     int nEnd = ME_StrVLen(run->strText);
553     SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, &run->nAscent, &run->nDescent);
554     run->nWidth = size.cx;
555     if (!size.cx)
556       WARN("size.cx == 0\n");
557   }
558 }
559
560 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
561 {
562   assert(para->type == diParagraph);
563   /* FIXME */
564 }
565
566 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
567 {
568   int nFrom, nTo;
569   ME_GetSelection(editor, &nFrom, &nTo);
570   if (nFrom == nTo)
571   {
572     ME_Style *s;
573     if (!editor->pBuffer->pCharStyle)
574       editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
575     s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
576     ME_ReleaseStyle(editor->pBuffer->pCharStyle);
577     editor->pBuffer->pCharStyle = s;
578   }
579   else
580     ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
581 }
582
583 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
584 {
585   ME_Cursor tmp, tmp2;
586   ME_DisplayItem *para;
587
588   ME_CursorFromCharOfs(editor, nOfs, &tmp);
589   if (tmp.nOffset)
590     tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
591
592   ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
593   if (tmp2.nOffset)
594     tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
595
596   para = ME_GetParagraph(tmp.pRun);
597   para->member.para.nFlags |= MEPF_REWRAP;
598
599   while(tmp.pRun != tmp2.pRun)
600   {
601     ME_UndoItem *undo = NULL;
602     ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
603     /* ME_DumpStyle(new_style); */
604     undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
605     if (undo) {
606       undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
607       undo->nLen = tmp.pRun->member.run.strText->nLen;
608       undo->di.member.ustyle = tmp.pRun->member.run.style;
609       /* we'd have to addref undo..ustyle and release tmp...style
610          but they'd cancel each other out so we can do nothing instead */
611     }
612     else
613       ME_ReleaseStyle(tmp.pRun->member.run.style);
614     tmp.pRun->member.run.style = new_style;
615     tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
616     if (tmp.pRun->type == diParagraph)
617     {
618       para = tmp.pRun;
619       tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
620       if (tmp.pRun != tmp2.pRun)
621         para->member.para.nFlags |= MEPF_REWRAP;
622     }
623     assert(tmp.pRun);
624   }
625 }
626
627 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
628 {
629   ME_Style *style;
630   ME_UndoItem *undo;
631
632   assert(mod->cbSize == sizeof(CHARFORMAT2W));
633   undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
634   if (undo) {
635     undo->nStart = -1;
636     undo->nLen = -1;
637     undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
638     ME_AddRefStyle(undo->di.member.ustyle);
639   }
640   style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
641   editor->pBuffer->pDefaultStyle->fmt = style->fmt;
642   editor->pBuffer->pDefaultStyle->tm = style->tm;
643   ME_ReleaseStyle(style);
644   ME_MarkAllForWrapping(editor);
645   /*  pcf = editor->pBuffer->pDefaultStyle->fmt; */
646 }
647
648 void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
649 {
650   ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
651 }
652
653 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
654 {
655   int nFrom, nTo;
656   ME_GetSelection(editor, &nFrom, &nTo);
657   ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
658 }
659
660 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
661 {
662   int nFrom, nTo;
663   ME_GetSelection(editor, &nFrom, &nTo);
664   if (nFrom == nTo && editor->pBuffer->pCharStyle)
665   {
666     ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
667     return;
668   }
669   ME_GetCharFormat(editor, nFrom, nTo, pFmt);
670 }
671
672 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
673 {
674   ME_DisplayItem *run, *run_end;
675   int nOffset, nOffset2;
676   CHARFORMAT2W tmp;
677
678   ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
679   if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
680   {
681     if (!nOffset)
682     {
683       ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
684       if (tmp_run->type == diRun) {
685         ME_GetRunCharFormat(editor, tmp_run, pFmt);
686         return;
687       }
688     }
689     ME_GetRunCharFormat(editor, run, pFmt);
690     return;
691   }
692   
693   if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
694     nTo--;
695   ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
696
697   ME_GetRunCharFormat(editor, run, pFmt);
698
699   if (run == run_end) return;
700
701   do {
702     /* FIXME add more style feature comparisons */
703     int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR;
704     int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
705
706     run = ME_FindItemFwd(run, diRun);
707
708     ZeroMemory(&tmp, sizeof(tmp));
709     tmp.cbSize = sizeof(tmp);
710     ME_GetRunCharFormat(editor, run, &tmp);
711
712     assert((tmp.dwMask & nAttribs) == nAttribs);
713     assert((tmp.dwMask & nEffects) == nEffects);
714     /* reset flags that differ */
715
716     if (pFmt->yHeight != tmp.yHeight)
717       pFmt->dwMask &= ~CFM_SIZE;
718     if (pFmt->dwMask & CFM_FACE)
719     {
720       if (!(tmp.dwMask & CFM_FACE))
721         pFmt->dwMask &= ~CFM_FACE;
722       else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName))
723         pFmt->dwMask &= ~CFM_FACE;
724     }
725     if (pFmt->yHeight != tmp.yHeight)
726       pFmt->dwMask &= ~CFM_SIZE;
727     if (pFmt->dwMask & CFM_COLOR)
728     {
729       if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
730       {
731         if (pFmt->crTextColor != tmp.crTextColor)
732           pFmt->dwMask &= ~CFM_COLOR;
733       }
734     }
735
736     pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
737
738   } while(run != run_end);
739 }