riched20: Speed up text insertion.
[wine/wine64.git] / dlls / riched20 / run.c
blob5c97941aa5f5dc482ac8799376710b0aac23ddc8
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
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.
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.
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
23 #include "editor.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
27 WINE_DECLARE_DEBUG_CHANNEL(richedit_lists);
29 int ME_CanJoinRuns(ME_Run *run1, ME_Run *run2)
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;
40 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
42 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
43 assert(p);
44 ME_PropagateCharOffset(p, shift);
47 void ME_PropagateCharOffset(ME_DisplayItem *p, int shift)
49 if (p->type == diRun) /* propagate in all runs in this para */
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);
58 if (p->type == diParagraph) /* propagate in all next paras */
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);
66 if (p->type == diTextEnd)
68 p->member.para.nCharOfs += shift;
69 assert(p->member.para.nCharOfs >= 0);
73 void ME_CheckCharOffsets(ME_TextEditor *editor)
75 ME_DisplayItem *p = editor->pBuffer->pFirst;
76 int ofs = 0, ofsp = 0;
77 if(TRACE_ON(richedit_lists))
79 TRACE_(richedit_lists)("---\n");
80 ME_DumpDocument(editor->pBuffer);
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);
110 } while(1);
113 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
115 ME_DisplayItem *pPara;
117 assert(pRun->type == diRun);
118 assert(pRun->member.run.nCharOfs != -1);
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);
127 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
129 ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
132 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
134 ME_DisplayItem *pPara;
135 int nParaOfs;
137 pPara = editor->pBuffer->pFirst->member.para.next_para;
138 assert(pPara);
139 assert(ppRun);
140 assert(pOfs);
141 while (pPara->type == diParagraph)
143 nParaOfs = pPara->member.para.nCharOfs;
144 assert(nCharOfs >= nParaOfs);
146 if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
148 int eollen = 1;
149 *ppRun = ME_FindItemFwd(pPara, diRun);
150 assert(*ppRun);
151 while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
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;
161 *ppRun = pNext;
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;
171 pPara = pPara->member.para.next_para;
173 *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
174 *pOfs = 0;
175 assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
178 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
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;
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);
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))
201 TRACE("Before check after join\n");
202 ME_CheckCharOffsets(editor);
203 TRACE("After check after join\n");
207 ME_DisplayItem *ME_SplitRun(ME_Context *c, ME_DisplayItem *item, int nVChar)
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;
214 assert(item->member.run.nCharOfs != -1);
215 if(TRACE_ON(richedit))
217 TRACE("Before check before split\n");
218 ME_CheckCharOffsets(editor);
219 TRACE("After check before split\n");
222 run = &item->member.run;
224 TRACE("Before split: %s(%ld, %ld)\n", debugstr_w(run->strText->szData),
225 run->pt.x, run->pt.y);
227 item2 = ME_SplitRunSimple(editor, item, nVChar);
229 run2 = &item2->member.run;
231 ME_CalcRunExtent(c, para, run);
232 ME_CalcRunExtent(c, para, run2);
234 run2->pt.x = run->pt.x+run->nWidth;
235 run2->pt.y = run->pt.y;
237 if(TRACE_ON(richedit))
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);
247 return item2;
250 /* split a run starting from voffset */
251 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
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);
262 item2 = ME_MakeRun(run->style,
263 ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
265 item2->member.run.nCharOfs = item->member.run.nCharOfs+
266 ME_VPosToPos(item->member.run.strText, nVChar);
268 run2 = &item2->member.run;
269 ME_InsertBefore(item->next, item2);
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;
281 ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
282 return item2;
285 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
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;
297 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
299 ME_Cursor tmp;
300 ME_DisplayItem *pDI;
302 assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
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);
310 return pDI;
313 ME_DisplayItem *
314 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
315 const WCHAR *str, int len, int flags)
317 ME_DisplayItem *pDI;
318 ME_UndoItem *pUI;
320 if (cursor->nOffset) {
321 cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
322 cursor->nOffset = 0;
325 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
326 if (pUI) {
327 pUI->nStart = cursor->pRun->member.run.nCharOfs;
328 pUI->nLen = len;
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;
340 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
342 assert(run->nCharOfs != -1);
343 if (ME_IsSplitable(run->strText))
344 run->nFlags |= MERF_SPLITTABLE;
345 else
346 run->nFlags &= ~MERF_SPLITTABLE;
348 if (!(run->nFlags & MERF_NOTEXT)) {
349 if (ME_IsWhitespaces(run->strText))
350 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
351 else
353 run->nFlags &= ~MERF_WHITESPACE;
355 if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
356 run->nFlags |= MERF_STARTWHITE;
357 else
358 run->nFlags &= ~MERF_STARTWHITE;
360 if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
361 run->nFlags |= MERF_ENDWHITE;
362 else
363 run->nFlags &= ~MERF_ENDWHITE;
366 else
367 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
370 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
372 assert(run->nFlags & MERF_GRAPHICS);
373 pSize->cx = 64;
374 pSize->cy = 64;
377 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Paragraph *para, ME_Run *run)
379 int fit = 0;
380 HGDIOBJ hOldFont;
381 HDC hDC;
382 SIZE sz;
383 if (!run->strText->nLen)
384 return 0;
386 if (run->nFlags & MERF_TAB)
388 if (cx < run->nWidth/2)
389 return 0;
390 return 1;
392 if (run->nFlags & MERF_GRAPHICS)
394 SIZE sz;
395 ME_GetGraphicsSize(editor, run, &sz);
396 if (cx < sz.cx)
397 return 0;
398 return 1;
400 hDC = GetDC(editor->hWnd);
401 hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
402 GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
403 cx, &fit, NULL, &sz);
404 ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
405 ReleaseDC(editor->hWnd, hDC);
406 return fit;
409 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
411 int fit = 0, fit1 = 0;
412 HGDIOBJ hOldFont;
413 HDC hDC;
414 SIZE sz, sz2, sz3;
415 if (!run->strText->nLen)
416 return 0;
418 if (run->nFlags & MERF_TAB)
420 if (cx < run->nWidth/2)
421 return 0;
422 return 1;
424 if (run->nFlags & MERF_GRAPHICS)
426 SIZE sz;
427 ME_GetGraphicsSize(editor, run, &sz);
428 if (cx < sz.cx/2)
429 return 0;
430 return 1;
433 hDC = GetDC(editor->hWnd);
434 hOldFont = ME_SelectStyleFont(editor, hDC, run->style);
435 GetTextExtentExPointW(hDC, run->strText->szData, run->strText->nLen,
436 cx, &fit, NULL, &sz);
437 if (fit != run->strText->nLen)
439 int chars = 1;
441 GetTextExtentPoint32W(hDC, run->strText->szData, fit, &sz2);
442 fit1 = ME_StrRelPos(run->strText, fit, &chars);
443 GetTextExtentPoint32W(hDC, run->strText->szData, fit1, &sz3);
444 if (cx >= (sz2.cx+sz3.cx)/2)
445 fit = fit1;
447 ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
448 ReleaseDC(editor->hWnd, hDC);
449 return fit;
452 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
454 SIZE size;
455 HDC hDC = GetDC(editor->hWnd);
456 HGDIOBJ hOldFont;
458 if (pRun->nFlags & MERF_GRAPHICS)
460 if (!nOffset) return 0;
461 ME_GetGraphicsSize(editor, pRun, &size);
462 return 1;
464 hOldFont = ME_SelectStyleFont(editor, hDC, pRun->style);
465 GetTextExtentPoint32W(hDC, pRun->strText->szData, nOffset, &size);
466 ME_UnselectStyleFont(editor, hDC, pRun->style, hOldFont);
467 ReleaseDC(editor->hWnd, hDC);
468 return size.cx;
471 void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s,
472 SIZE *size)
474 HDC hDC = c->hDC;
475 HGDIOBJ hOldFont;
476 hOldFont = ME_SelectStyleFont(c->editor, hDC, s);
477 GetTextExtentPoint32W(hDC, szText, nChars, size);
478 ME_UnselectStyleFont(c->editor, hDC, s, hOldFont);
481 SIZE ME_GetRunSizeCommon(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen, int *pAscent, int *pDescent)
483 SIZE size;
484 int nMaxLen = ME_StrVLen(run->strText);
486 if (nLen>nMaxLen)
487 nLen = nMaxLen;
489 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
490 * this is wasteful for graphics and TAB runs, but that shouldn't matter
491 * in practice
493 ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
494 *pAscent = run->style->tm.tmAscent;
495 *pDescent = run->style->tm.tmDescent;
496 size.cy = *pAscent + *pDescent;
498 if (run->nFlags & MERF_TAB)
500 int pos = 0, i = 0, ppos;
501 int lpsx = GetDeviceCaps(c->hDC, LOGPIXELSX);
502 PARAFORMAT2 *pFmt = para->pFmt;
503 do {
504 if (i < pFmt->cTabCount)
506 pos = pFmt->rgxTabs[i]&0x00FFFFFF;
507 i++;
509 else
511 pos += 720-(pos%720);
513 ppos = pos*lpsx/1440;
514 if (ppos>run->pt.x) {
515 size.cx = ppos - run->pt.x;
516 break;
518 } while(1);
519 size.cy = *pAscent + *pDescent;
520 return size;
522 if (run->nFlags & MERF_GRAPHICS)
524 ME_GetGraphicsSize(c->editor, run, &size);
525 if (size.cy > *pAscent)
526 *pAscent = size.cy;
527 /* descent is unchanged */
528 return size;
531 return size;
534 SIZE ME_GetRunSize(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen)
536 int asc, desc;
537 return ME_GetRunSizeCommon(c, para, run, nLen, &asc, &desc);
540 void ME_CalcRunExtent(ME_Context *c, ME_Paragraph *para, ME_Run *run)
542 int nEnd = ME_StrVLen(run->strText);
543 SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, &run->nAscent, &run->nDescent);
544 run->nWidth = size.cx;
545 if (!size.cx)
546 WARN("size.cx == 0\n");
549 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
551 assert(para->type == diParagraph);
552 /* FIXME */
555 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
557 int nFrom, nTo;
558 ME_GetSelection(editor, &nFrom, &nTo);
559 if (nFrom == nTo)
561 ME_Style *s;
562 if (!editor->pBuffer->pCharStyle)
563 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
564 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
565 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
566 editor->pBuffer->pCharStyle = s;
568 else
569 ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
572 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
574 ME_Cursor tmp, tmp2;
575 ME_DisplayItem *para;
577 ME_CursorFromCharOfs(editor, nOfs, &tmp);
578 if (tmp.nOffset)
579 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
581 ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
582 if (tmp2.nOffset)
583 tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
585 para = ME_GetParagraph(tmp.pRun);
586 para->member.para.nFlags |= MEPF_REWRAP;
588 while(tmp.pRun != tmp2.pRun)
590 ME_UndoItem *undo = NULL;
591 ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
592 /* ME_DumpStyle(new_style); */
593 undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
594 if (undo) {
595 undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
596 undo->nLen = tmp.pRun->member.run.strText->nLen;
597 undo->di.member.ustyle = tmp.pRun->member.run.style;
598 /* we'd have to addref undo..ustyle and release tmp...style
599 but they'd cancel each other out so we can do nothing instead */
601 else
602 ME_ReleaseStyle(tmp.pRun->member.run.style);
603 tmp.pRun->member.run.style = new_style;
604 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
605 if (tmp.pRun->type == diParagraph)
607 para = tmp.pRun;
608 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
609 if (tmp.pRun != tmp2.pRun)
610 para->member.para.nFlags |= MEPF_REWRAP;
612 assert(tmp.pRun);
616 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
618 ME_Style *style;
619 ME_UndoItem *undo;
621 assert(mod->cbSize == sizeof(CHARFORMAT2W));
622 undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
623 if (undo) {
624 undo->nStart = -1;
625 undo->nLen = -1;
626 undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
627 ME_AddRefStyle(undo->di.member.ustyle);
629 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
630 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
631 editor->pBuffer->pDefaultStyle->tm = style->tm;
632 ME_ReleaseStyle(style);
633 ME_MarkAllForWrapping(editor);
634 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
637 void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
639 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
642 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
644 int nFrom, nTo;
645 ME_GetSelection(editor, &nFrom, &nTo);
646 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
649 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
651 int nFrom, nTo;
652 ME_GetSelection(editor, &nFrom, &nTo);
653 if (nFrom == nTo && editor->pBuffer->pCharStyle)
655 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
656 return;
658 ME_GetCharFormat(editor, nFrom, nTo, pFmt);
661 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
663 ME_DisplayItem *run, *run_end;
664 int nOffset, nOffset2;
665 CHARFORMAT2W tmp;
667 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
668 if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
670 if (!nOffset)
672 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
673 if (tmp_run->type == diRun) {
674 ME_GetRunCharFormat(editor, tmp_run, pFmt);
675 return;
678 ME_GetRunCharFormat(editor, run, pFmt);
679 return;
682 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
683 nTo--;
684 ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
686 ME_GetRunCharFormat(editor, run, pFmt);
688 if (run == run_end) return;
690 do {
691 /* FIXME add more style feature comparisons */
692 int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR;
693 int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE;
695 run = ME_FindItemFwd(run, diRun);
697 ZeroMemory(&tmp, sizeof(tmp));
698 tmp.cbSize = sizeof(tmp);
699 ME_GetRunCharFormat(editor, run, &tmp);
701 assert((tmp.dwMask & nAttribs) == nAttribs);
702 assert((tmp.dwMask & nEffects) == nEffects);
703 /* reset flags that differ */
705 if (pFmt->yHeight != tmp.yHeight)
706 pFmt->dwMask &= ~CFM_SIZE;
707 if (pFmt->dwMask & CFM_FACE)
709 if (!(tmp.dwMask & CFM_FACE))
710 pFmt->dwMask &= ~CFM_FACE;
711 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName))
712 pFmt->dwMask &= ~CFM_FACE;
714 if (pFmt->yHeight != tmp.yHeight)
715 pFmt->dwMask &= ~CFM_SIZE;
716 if (pFmt->dwMask & CFM_COLOR)
718 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
720 if (pFmt->crTextColor != tmp.crTextColor)
721 pFmt->dwMask &= ~CFM_COLOR;
725 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
727 } while(run != run_end);