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