winetest: Add d3d9 tests.
[wine/winequartzdrv.git] / dlls / riched20 / run.c
blob568472c62f25c115c2fd6003062f2c6a5dd2bb76
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;
296 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
298 ME_Cursor tmp;
299 ME_DisplayItem *pDI;
300 ME_UndoItem *pUI;
302 assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
304 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
305 if (pUI) {
306 pUI->nStart = nCharOfs;
307 pUI->nLen = pItem->member.run.strText->nLen;
309 ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
310 if (tmp.nOffset) {
311 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
312 tmp.nOffset = 0;
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;
321 return pDI;
324 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
326 assert(run->nCharOfs != -1);
327 if (ME_IsSplitable(run->strText))
328 run->nFlags |= MERF_SPLITTABLE;
329 else
330 run->nFlags &= ~MERF_SPLITTABLE;
332 if (!(run->nFlags & MERF_NOTEXT)) {
333 if (ME_IsWhitespaces(run->strText))
334 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
335 else
337 run->nFlags &= ~MERF_WHITESPACE;
339 if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
340 run->nFlags |= MERF_STARTWHITE;
341 else
342 run->nFlags &= ~MERF_STARTWHITE;
344 if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
345 run->nFlags |= MERF_ENDWHITE;
346 else
347 run->nFlags &= ~MERF_ENDWHITE;
350 else
351 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
354 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
356 assert(run->nFlags & MERF_GRAPHICS);
357 pSize->cx = 64;
358 pSize->cy = 64;
361 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Paragraph *para, ME_Run *run)
363 int fit = 0;
364 HGDIOBJ hOldFont;
365 HDC hDC;
366 SIZE sz;
367 if (!run->strText->nLen)
368 return 0;
370 if (run->nFlags & MERF_TAB)
372 if (cx < run->nWidth/2)
373 return 0;
374 return 1;
376 if (run->nFlags & MERF_GRAPHICS)
378 SIZE sz;
379 ME_GetGraphicsSize(editor, run, &sz);
380 if (cx < sz.cx)
381 return 0;
382 return 1;
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;
393 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
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;
402 if (run->nFlags & MERF_TAB)
404 if (cx < run->nWidth/2)
405 return 0;
406 return 1;
408 if (run->nFlags & MERF_GRAPHICS)
410 SIZE sz;
411 ME_GetGraphicsSize(editor, run, &sz);
412 if (cx < sz.cx/2)
413 return 0;
414 return 1;
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)
423 int chars = 1;
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;
431 ME_UnselectStyleFont(editor, hDC, run->style, hOldFont);
432 ReleaseDC(editor->hWnd, hDC);
433 return fit;
436 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
438 SIZE size;
439 HDC hDC = GetDC(editor->hWnd);
440 HGDIOBJ hOldFont;
442 if (pRun->nFlags & MERF_GRAPHICS)
444 if (!nOffset) return 0;
445 ME_GetGraphicsSize(editor, pRun, &size);
446 return 1;
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;
455 void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s,
456 SIZE *size)
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);
465 SIZE ME_GetRunSizeCommon(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen, int *pAscent, int *pDescent)
467 SIZE size;
468 int nMaxLen = ME_StrVLen(run->strText);
470 if (nLen>nMaxLen)
471 nLen = nMaxLen;
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
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;
482 if (run->nFlags & MERF_TAB)
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)
490 pos = pFmt->rgxTabs[i]&0x00FFFFFF;
491 i++;
493 else
495 pos += 720-(pos%720);
497 ppos = pos*lpsx/1440;
498 if (ppos>run->pt.x) {
499 size.cx = ppos - run->pt.x;
500 break;
502 } while(1);
503 size.cy = *pAscent + *pDescent;
504 return size;
506 if (run->nFlags & MERF_GRAPHICS)
508 ME_GetGraphicsSize(c->editor, run, &size);
509 if (size.cy > *pAscent)
510 *pAscent = size.cy;
511 /* descent is unchanged */
512 return size;
515 return size;
518 SIZE ME_GetRunSize(ME_Context *c, ME_Paragraph *para, ME_Run *run, int nLen)
520 int asc, desc;
521 return ME_GetRunSizeCommon(c, para, run, nLen, &asc, &desc);
524 void ME_CalcRunExtent(ME_Context *c, ME_Paragraph *para, ME_Run *run)
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");
533 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
535 assert(para->type == diParagraph);
536 /* FIXME */
539 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
541 int nFrom, nTo;
542 ME_GetSelection(editor, &nFrom, &nTo);
543 if (nFrom == nTo)
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;
552 else
553 ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
556 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
558 ME_Cursor tmp, tmp2;
559 ME_DisplayItem *para;
561 ME_CursorFromCharOfs(editor, nOfs, &tmp);
562 if (tmp.nOffset)
563 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
565 ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
566 if (tmp2.nOffset)
567 tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
569 para = ME_GetParagraph(tmp.pRun);
570 para->member.para.nFlags |= MEPF_REWRAP;
572 while(tmp.pRun != tmp2.pRun)
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 */
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)
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;
596 assert(tmp.pRun);
600 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
602 ME_Style *style;
603 ME_UndoItem *undo;
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);
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; */
621 void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
623 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
626 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
628 int nFrom, nTo;
629 ME_GetSelection(editor, &nFrom, &nTo);
630 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
633 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
635 int nFrom, nTo;
636 ME_GetSelection(editor, &nFrom, &nTo);
637 if (nFrom == nTo && editor->pBuffer->pCharStyle)
639 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
640 return;
642 ME_GetCharFormat(editor, nFrom, nTo, pFmt);
645 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
647 ME_DisplayItem *run, *run_end;
648 int nOffset, nOffset2;
649 CHARFORMAT2W tmp;
651 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
652 if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
654 if (!nOffset)
656 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
657 if (tmp_run->type == diRun) {
658 ME_GetRunCharFormat(editor, tmp_run, pFmt);
659 return;
662 ME_GetRunCharFormat(editor, run, pFmt);
663 return;
666 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
667 nTo--;
668 ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
670 ME_GetRunCharFormat(editor, run, pFmt);
672 if (run == run_end) return;
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;
679 run = ME_FindItemFwd(run, diRun);
681 ZeroMemory(&tmp, sizeof(tmp));
682 tmp.cbSize = sizeof(tmp);
683 ME_GetRunCharFormat(editor, run, &tmp);
685 assert((tmp.dwMask & nAttribs) == nAttribs);
686 assert((tmp.dwMask & nEffects) == nEffects);
687 /* reset flags that differ */
689 if (pFmt->yHeight != tmp.yHeight)
690 pFmt->dwMask &= ~CFM_SIZE;
691 if (pFmt->dwMask & CFM_FACE)
693 if (!(tmp.dwMask & CFM_FACE))
694 pFmt->dwMask &= ~CFM_FACE;
695 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName))
696 pFmt->dwMask &= ~CFM_FACE;
698 if (pFmt->yHeight != tmp.yHeight)
699 pFmt->dwMask &= ~CFM_SIZE;
700 if (pFmt->dwMask & CFM_COLOR)
702 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
704 if (pFmt->crTextColor != tmp.crTextColor)
705 pFmt->dwMask &= ~CFM_COLOR;
709 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
711 } while(run != run_end);