configure: Make the font warning more explicit about what package is missing.
[wine/multimedia.git] / dlls / riched20 / run.c
blobf5ea660fbf90f04d93351b9e22cb13626332d701
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.
6 * Copyright 2004 by Krzysztof Foltman
7 * Copyright 2006 by Phil Krylov
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "editor.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
28 WINE_DECLARE_DEBUG_CHANNEL(richedit_lists);
30 int ME_CanJoinRuns(ME_Run *run1, ME_Run *run2)
32 if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
33 return 0;
34 if (run1->style != run2->style)
35 return 0;
36 if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
37 return 0;
38 return 1;
41 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
43 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
44 assert(p);
45 ME_PropagateCharOffset(p, shift);
48 void ME_PropagateCharOffset(ME_DisplayItem *p, int shift)
50 if (p->type == diRun) /* propagate in all runs in this para */
52 TRACE("PropagateCharOffset(%s, %d)\n", debugstr_w(p->member.run.strText->szData), shift);
53 do {
54 p->member.run.nCharOfs += shift;
55 assert(p->member.run.nCharOfs >= 0);
56 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
57 } while(p->type == diRun);
59 if (p->type == diParagraph) /* propagate in all next paras */
61 do {
62 p->member.para.nCharOfs += shift;
63 assert(p->member.para.nCharOfs >= 0);
64 p = p->member.para.next_para;
65 } while(p->type == diParagraph);
67 if (p->type == diTextEnd)
69 p->member.para.nCharOfs += shift;
70 assert(p->member.para.nCharOfs >= 0);
74 void ME_CheckCharOffsets(ME_TextEditor *editor)
76 ME_DisplayItem *p = editor->pBuffer->pFirst;
77 int ofs = 0, ofsp = 0;
78 if(TRACE_ON(richedit_lists))
80 TRACE_(richedit_lists)("---\n");
81 ME_DumpDocument(editor->pBuffer);
83 do {
84 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
85 switch(p->type) {
86 case diTextEnd:
87 TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
88 assert(ofsp+ofs == p->member.para.nCharOfs);
89 return;
90 case diParagraph:
91 TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
92 assert(ofsp+ofs == p->member.para.nCharOfs);
93 ofsp = p->member.para.nCharOfs;
94 ofs = 0;
95 break;
96 case diRun:
97 TRACE_(richedit_check)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = \"%s\", flags=%08x, fx&mask = %08lx\n",
98 p->member.run.nCharOfs, p->member.run.nCharOfs+ofsp, ofsp+ofs,
99 p->member.run.strText->nLen, debugstr_w(p->member.run.strText->szData),
100 p->member.run.nFlags,
101 p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
102 assert(ofs == p->member.run.nCharOfs);
103 if (p->member.run.nFlags & MERF_ENDPARA)
104 ofs += (editor->bEmulateVersion10 ? 2 : 1);
105 else
106 ofs += ME_StrLen(p->member.run.strText);
107 break;
108 default:
109 assert(0);
111 } while(1);
114 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
116 ME_DisplayItem *pPara;
118 assert(pRun->type == diRun);
119 assert(pRun->member.run.nCharOfs != -1);
121 pPara = ME_FindItemBack(pRun, diParagraph);
122 assert(pPara);
123 assert(pPara->type==diParagraph);
124 return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs
125 + ME_VPosToPos(pRun->member.run.strText, nOfs);
128 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
130 ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
133 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
135 ME_DisplayItem *pPara;
136 int nParaOfs;
138 pPara = editor->pBuffer->pFirst->member.para.next_para;
139 assert(pPara);
140 assert(ppRun);
141 assert(pOfs);
142 while (pPara->type == diParagraph)
144 nParaOfs = pPara->member.para.nCharOfs;
145 assert(nCharOfs >= nParaOfs);
147 if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
149 int eollen = 1;
150 *ppRun = ME_FindItemFwd(pPara, diRun);
151 assert(*ppRun);
152 while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
154 ME_DisplayItem *pNext = ME_FindItemFwd(*ppRun, diRun);
155 assert(pNext);
156 assert(pNext->type == diRun);
157 if (nCharOfs < nParaOfs + pNext->member.run.nCharOfs) {
158 *pOfs = ME_PosToVPos((*ppRun)->member.run.strText,
159 nCharOfs - nParaOfs - (*ppRun)->member.run.nCharOfs);
160 return;
162 *ppRun = pNext;
164 /* the handling of bEmulateVersion10 may be a source of many bugs, I'm afraid */
165 eollen = (editor->bEmulateVersion10 ? 2 : 1);
166 if (nCharOfs >= nParaOfs + (*ppRun)->member.run.nCharOfs &&
167 nCharOfs < nParaOfs + (*ppRun)->member.run.nCharOfs + eollen) {
168 *pOfs = 0;
169 return;
172 pPara = pPara->member.para.next_para;
174 *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
175 *pOfs = 0;
176 assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
179 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
181 ME_DisplayItem *pNext = p->next;
182 int i;
183 assert(p->type == diRun && pNext->type == diRun);
184 assert(p->member.run.nCharOfs != -1);
185 ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
187 if (editor->bCaretAtEnd && editor->pCursors[0].pRun == pNext)
188 editor->bCaretAtEnd = FALSE;
189 for (i=0; i<editor->nCursors; i++) {
190 if (editor->pCursors[i].pRun == pNext) {
191 editor->pCursors[i].pRun = p;
192 editor->pCursors[i].nOffset += ME_StrVLen(p->member.run.strText);
196 ME_AppendString(p->member.run.strText, pNext->member.run.strText);
197 ME_Remove(pNext);
198 ME_DestroyDisplayItem(pNext);
199 ME_UpdateRunFlags(editor, &p->member.run);
200 if(TRACE_ON(richedit))
202 TRACE("Before check after join\n");
203 ME_CheckCharOffsets(editor);
204 TRACE("After check after join\n");
208 ME_DisplayItem *ME_SplitRun(ME_Context *c, ME_DisplayItem *item, int nVChar)
210 ME_TextEditor *editor = c->editor;
211 ME_DisplayItem *item2 = NULL;
212 ME_Run *run, *run2;
213 ME_Paragraph *para = &ME_GetParagraph(item)->member.para;
215 assert(item->member.run.nCharOfs != -1);
216 if(TRACE_ON(richedit))
218 TRACE("Before check before split\n");
219 ME_CheckCharOffsets(editor);
220 TRACE("After check before split\n");
223 run = &item->member.run;
225 TRACE("Before split: %s(%ld, %ld)\n", debugstr_w(run->strText->szData),
226 run->pt.x, run->pt.y);
228 item2 = ME_SplitRunSimple(editor, item, nVChar);
230 run2 = &item2->member.run;
232 ME_CalcRunExtent(c, para, run);
233 ME_CalcRunExtent(c, para, run2);
235 run2->pt.x = run->pt.x+run->nWidth;
236 run2->pt.y = run->pt.y;
238 if(TRACE_ON(richedit))
240 TRACE("Before check after split\n");
241 ME_CheckCharOffsets(editor);
242 TRACE("After check after split\n");
243 TRACE("After split: %s(%ld, %ld), %s(%ld, %ld)\n",
244 debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
245 debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
248 return item2;
251 /* split a run starting from voffset */
252 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
254 ME_Run *run = &item->member.run;
255 ME_DisplayItem *item2;
256 ME_Run *run2;
257 int i;
258 assert(nVChar > 0 && nVChar < ME_StrVLen(run->strText));
259 assert(item->type == diRun);
260 assert(!(item->member.run.nFlags & MERF_NONTEXT));
261 assert(item->member.run.nCharOfs != -1);
263 item2 = ME_MakeRun(run->style,
264 ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
266 item2->member.run.nCharOfs = item->member.run.nCharOfs+
267 ME_VPosToPos(item->member.run.strText, nVChar);
269 run2 = &item2->member.run;
270 ME_InsertBefore(item->next, item2);
272 ME_UpdateRunFlags(editor, run);
273 ME_UpdateRunFlags(editor, run2);
274 for (i=0; i<editor->nCursors; i++) {
275 if (editor->pCursors[i].pRun == item &&
276 editor->pCursors[i].nOffset >= nVChar) {
277 assert(item2->type == diRun);
278 editor->pCursors[i].pRun = item2;
279 editor->pCursors[i].nOffset -= nVChar;
282 ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
283 return item2;
286 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
288 ME_DisplayItem *item = ME_MakeDI(diRun);
289 item->member.run.style = s;
290 item->member.run.strText = strData;
291 item->member.run.nFlags = nFlags;
292 item->member.run.nCharOfs = -1;
293 ME_AddRefStyle(s);
294 return item;
298 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
300 ME_Cursor tmp;
301 ME_DisplayItem *pDI;
303 assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
305 ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
306 pDI = ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
307 pItem->member.run.strText->szData,
308 pItem->member.run.strText->nLen,
309 pItem->member.run.nFlags);
311 return pDI;
314 ME_DisplayItem *
315 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
316 const WCHAR *str, int len, int flags)
318 ME_DisplayItem *pDI;
319 ME_UndoItem *pUI;
321 if (cursor->nOffset) {
322 cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
323 cursor->nOffset = 0;
326 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
327 if (pUI) {
328 pUI->nStart = (ME_GetParagraph(cursor->pRun)->member.para.nCharOfs
329 + cursor->pRun->member.run.nCharOfs);
330 pUI->nLen = len;
333 pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
334 pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
335 ME_InsertBefore(cursor->pRun, pDI);
336 TRACE("Shift length:%d\n", len);
337 ME_PropagateCharOffset(cursor->pRun, len);
338 ME_GetParagraph(cursor->pRun)->member.para.nFlags |= MEPF_REWRAP;
339 return pDI;
342 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
344 assert(run->nCharOfs != -1);
346 if (RUN_IS_HIDDEN(run))
347 run->nFlags |= MERF_HIDDEN;
348 else
349 run->nFlags &= ~MERF_HIDDEN;
351 if (ME_IsSplitable(run->strText))
352 run->nFlags |= MERF_SPLITTABLE;
353 else
354 run->nFlags &= ~MERF_SPLITTABLE;
356 if (!(run->nFlags & MERF_NOTEXT)) {
357 if (ME_IsWhitespaces(run->strText))
358 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
359 else
361 run->nFlags &= ~MERF_WHITESPACE;
363 if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
364 run->nFlags |= MERF_STARTWHITE;
365 else
366 run->nFlags &= ~MERF_STARTWHITE;
368 if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
369 run->nFlags |= MERF_ENDWHITE;
370 else
371 run->nFlags &= ~MERF_ENDWHITE;
374 else
375 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
378 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
380 assert(run->nFlags & MERF_GRAPHICS);
381 pSize->cx = 64;
382 pSize->cy = 64;
385 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Run *run)
387 int fit = 0;
388 HGDIOBJ hOldFont;
389 HDC hDC;
390 SIZE sz;
391 if (!run->strText->nLen)
392 return 0;
394 if (run->nFlags & (MERF_TAB | MERF_CELL))
396 if (cx < run->nWidth/2)
397 return 0;
398 return 1;
400 if (run->nFlags & MERF_GRAPHICS)
402 SIZE sz;
403 ME_GetGraphicsSize(editor, run, &sz);
404 if (cx < sz.cx)
405 return 0;
406 return 1;
408 hDC = GetDC(editor->hWnd);
409 hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
410 GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
411 cx, &fit, NULL, &sz);
412 ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
413 ReleaseDC(editor->hWnd, hDC);
414 return fit;
417 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
419 int fit = 0, fit1 = 0;
420 HGDIOBJ hOldFont;
421 HDC hDC;
422 SIZE sz, sz2, sz3;
423 if (!run->strText->nLen)
424 return 0;
426 if (run->nFlags & (MERF_TAB | MERF_CELL))
428 if (cx < run->nWidth/2)
429 return 0;
430 return 1;
432 if (run->nFlags & MERF_GRAPHICS)
434 SIZE sz;
435 ME_GetGraphicsSize(editor, run, &sz);
436 if (cx < sz.cx/2)
437 return 0;
438 return 1;
441 hDC = GetDC(editor->hWnd);
442 hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
443 GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
444 cx, &fit, NULL, &sz);
445 if (fit != run->strText->nLen)
447 int chars = 1;
449 GetTextExtentPoint32W(hDC, run->strText->szData, fit, &sz2);
450 fit1 = ME_StrRelPos(run->strText, fit, &chars);
451 GetTextExtentPoint32W(hDC, run->strText->szData, fit1, &sz3);
452 if (cx >= (sz2.cx+sz3.cx)/2)
453 fit = fit1;
455 ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
456 ReleaseDC(editor->hWnd, hDC);
457 return fit;
460 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
462 SIZE size;
463 HDC hDC = GetDC(editor->hWnd);
464 HGDIOBJ hOldFont;
466 if (pRun->nFlags & MERF_GRAPHICS)
468 if (!nOffset) return 0;
469 ME_GetGraphicsSize(editor, pRun, &size);
470 return 1;
472 hOldFont = ME_SelectStyleFont(editor, hDC, pRun->style);
473 GetTextExtentPoint32W(hDC, pRun->strText->szData, nOffset, &size);
474 ME_UnselectStyleFont(editor, hDC, pRun->style, hOldFont);
475 ReleaseDC(editor->hWnd, hDC);
476 return size.cx;
479 void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s,
480 SIZE *size)
482 HDC hDC = c->hDC;
483 HGDIOBJ hOldFont;
484 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
485 GetTextExtentPoint32W(hDC, szText, nChars, size);
486 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
489 SIZE ME_GetRunSizeCommon(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen, int *pAscent, int *pDescent)
491 SIZE size;
492 int nMaxLen = ME_StrVLen(run->strText);
494 if (nLen>nMaxLen)
495 nLen = nMaxLen;
497 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
498 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
499 * in practice
501 ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
502 *pAscent = run->style->tm.tmAscent;
503 *pDescent = run->style->tm.tmDescent;
504 size.cy = *pAscent + *pDescent;
506 if (run->nFlags & MERF_TAB)
508 int pos = 0, i = 0, ppos;
509 int lpsx = GetDeviceCaps(c->hDC, LOGPIXELSX);
510 PARAFORMAT2 *pFmt = para->pFmt;
511 do {
512 if (i < pFmt->cTabCount)
514 pos = pFmt->rgxTabs[i]&0x00FFFFFF;
515 i++;
517 else
519 pos += 720-(pos%720);
521 ppos = pos*lpsx/1440;
522 if (ppos>run->pt.x) {
523 size.cx = ppos - run->pt.x;
524 break;
526 } while(1);
527 size.cy = *pAscent + *pDescent;
528 return size;
530 if (run->nFlags & MERF_GRAPHICS)
532 ME_GetGraphicsSize(c->editor, run, &size);
533 if (size.cy > *pAscent)
534 *pAscent = size.cy;
535 /* descent is unchanged */
536 return size;
538 if (run->nFlags & MERF_CELL)
540 int lpsx = GetDeviceCaps(c->hDC, LOGPIXELSX);
542 size.cx = run->pCell->nRightBoundary * lpsx / 1440 - run->pt.x;
543 return size;
545 return size;
548 SIZE ME_GetRunSize(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen)
550 int asc, desc;
551 return ME_GetRunSizeCommon(c, para, run, nLen, &asc, &desc);
554 void ME_CalcRunExtent(ME_Context *c, ME_Paragraph *para, ME_Run *run)
556 if (run->nFlags & MERF_HIDDEN)
557 run->nWidth = 0;
558 else
560 int nEnd = ME_StrVLen(run->strText);
561 SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, &run->nAscent, &run->nDescent);
562 run->nWidth = size.cx;
563 if (!size.cx)
564 WARN("size.cx == 0\n");
568 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
570 assert(para->type == diParagraph);
571 /* FIXME */
574 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
576 int nFrom, nTo;
577 ME_GetSelection(editor, &nFrom, &nTo);
578 if (nFrom == nTo)
580 ME_Style *s;
581 if (!editor->pBuffer->pCharStyle)
582 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
583 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
584 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
585 editor->pBuffer->pCharStyle = s;
587 else
588 ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
591 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
593 ME_Cursor tmp, tmp2;
594 ME_DisplayItem *para;
596 ME_CursorFromCharOfs(editor, nOfs, &tmp);
597 if (tmp.nOffset)
598 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
600 ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
601 if (tmp2.nOffset)
602 tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
604 para = ME_GetParagraph(tmp.pRun);
605 para->member.para.nFlags |= MEPF_REWRAP;
607 while(tmp.pRun != tmp2.pRun)
609 ME_UndoItem *undo = NULL;
610 ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
611 /* ME_DumpStyle(new_style); */
612 undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
613 if (undo) {
614 undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
615 undo->nLen = tmp.pRun->member.run.strText->nLen;
616 undo->di.member.ustyle = tmp.pRun->member.run.style;
617 /* we'd have to addref undo..ustyle and release tmp...style
618 but they'd cancel each other out so we can do nothing instead */
620 else
621 ME_ReleaseStyle(tmp.pRun->member.run.style);
622 tmp.pRun->member.run.style = new_style;
623 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
624 if (tmp.pRun->type == diParagraph)
626 para = tmp.pRun;
627 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
628 if (tmp.pRun != tmp2.pRun)
629 para->member.para.nFlags |= MEPF_REWRAP;
631 assert(tmp.pRun);
635 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
637 ME_Style *style;
638 ME_UndoItem *undo;
640 assert(mod->cbSize == sizeof(CHARFORMAT2W));
641 undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
642 if (undo) {
643 undo->nStart = -1;
644 undo->nLen = -1;
645 undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
646 ME_AddRefStyle(undo->di.member.ustyle);
648 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
649 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
650 editor->pBuffer->pDefaultStyle->tm = style->tm;
651 ME_ReleaseStyle(style);
652 ME_MarkAllForWrapping(editor);
653 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
656 void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
658 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
661 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
663 int nFrom, nTo;
664 ME_GetSelection(editor, &nFrom, &nTo);
665 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
668 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
670 int nFrom, nTo;
671 ME_GetSelection(editor, &nFrom, &nTo);
672 if (nFrom == nTo && editor->pBuffer->pCharStyle)
674 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
675 return;
677 ME_GetCharFormat(editor, nFrom, nTo, pFmt);
680 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
682 ME_DisplayItem *run, *run_end;
683 int nOffset, nOffset2;
684 CHARFORMAT2W tmp;
686 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
687 if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
689 if (!nOffset)
691 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
692 if (tmp_run->type == diRun) {
693 ME_GetRunCharFormat(editor, tmp_run, pFmt);
694 return;
697 ME_GetRunCharFormat(editor, run, pFmt);
698 return;
701 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
702 nTo--;
703 ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
705 ME_GetRunCharFormat(editor, run, pFmt);
707 if (run == run_end) return;
709 do {
710 /* FIXME add more style feature comparisons */
711 int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR;
712 int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
714 run = ME_FindItemFwd(run, diRun);
716 ZeroMemory(&tmp, sizeof(tmp));
717 tmp.cbSize = sizeof(tmp);
718 ME_GetRunCharFormat(editor, run, &tmp);
720 assert((tmp.dwMask & nAttribs) == nAttribs);
721 assert((tmp.dwMask & nEffects) == nEffects);
722 /* reset flags that differ */
724 if (pFmt->yHeight != tmp.yHeight)
725 pFmt->dwMask &= ~CFM_SIZE;
726 if (pFmt->dwMask & CFM_FACE)
728 if (!(tmp.dwMask & CFM_FACE))
729 pFmt->dwMask &= ~CFM_FACE;
730 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName))
731 pFmt->dwMask &= ~CFM_FACE;
733 if (pFmt->yHeight != tmp.yHeight)
734 pFmt->dwMask &= ~CFM_SIZE;
735 if (pFmt->dwMask & CFM_COLOR)
737 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
739 if (pFmt->crTextColor != tmp.crTextColor)
740 pFmt->dwMask &= ~CFM_COLOR;
744 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
746 } while(run != run_end);