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
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
27 int ME_CanJoinRuns(ME_Run
*run1
, ME_Run
*run2
)
29 if ((run1
->nFlags
| run2
->nFlags
) & MERF_NOJOIN
)
31 if (run1
->style
!= run2
->style
)
33 if ((run1
->nFlags
& MERF_STYLEFLAGS
) != (run2
->nFlags
& MERF_STYLEFLAGS
))
38 void ME_SkipAndPropagateCharOffset(ME_DisplayItem
*p
, int shift
)
40 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
42 ME_PropagateCharOffset(p
, shift
);
45 void ME_PropagateCharOffset(ME_DisplayItem
*p
, int shift
)
47 if (p
->type
== diRun
) /* propagate in all runs in this para */
49 TRACE("PropagateCharOffset(%s, %d)\n", debugstr_w(p
->member
.run
.strText
->szData
), shift
);
51 p
->member
.run
.nCharOfs
+= shift
;
52 assert(p
->member
.run
.nCharOfs
>= 0);
53 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
54 } while(p
->type
== diRun
);
56 if (p
->type
== diParagraph
) /* propagate in all next paras */
59 p
->member
.para
.nCharOfs
+= shift
;
60 assert(p
->member
.para
.nCharOfs
>= 0);
61 p
= p
->member
.para
.next_para
;
62 } while(p
->type
== diParagraph
);
64 if (p
->type
== diTextEnd
)
66 p
->member
.para
.nCharOfs
+= shift
;
67 assert(p
->member
.para
.nCharOfs
>= 0);
71 void ME_CheckCharOffsets(ME_TextEditor
*editor
)
73 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
74 int ofs
= 0, ofsp
= 0;
75 if(TRACE_ON(richedit
))
78 ME_DumpDocument(editor
->pBuffer
);
81 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
84 TRACE("tend, real ofsp = %d, counted = %d\n", p
->member
.para
.nCharOfs
, ofsp
+ofs
);
85 assert(ofsp
+ofs
== p
->member
.para
.nCharOfs
);
88 TRACE("para, real ofsp = %d, counted = %d\n", p
->member
.para
.nCharOfs
, ofsp
+ofs
);
89 assert(ofsp
+ofs
== p
->member
.para
.nCharOfs
);
90 ofsp
= p
->member
.para
.nCharOfs
;
94 TRACE("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = \"%s\", flags=%08x, fx&mask = %08lx\n",
95 p
->member
.run
.nCharOfs
, p
->member
.run
.nCharOfs
+ofsp
, ofsp
+ofs
,
96 p
->member
.run
.strText
->nLen
, debugstr_w(p
->member
.run
.strText
->szData
),
98 p
->member
.run
.style
->fmt
.dwMask
& p
->member
.run
.style
->fmt
.dwEffects
);
99 assert(ofs
== p
->member
.run
.nCharOfs
);
100 if (p
->member
.run
.nFlags
& MERF_ENDPARA
)
101 ofs
+= (editor
->bEmulateVersion10
? 2 : 1);
103 ofs
+= ME_StrLen(p
->member
.run
.strText
);
111 int ME_CharOfsFromRunOfs(ME_TextEditor
*editor
, ME_DisplayItem
*pRun
, int nOfs
)
113 ME_DisplayItem
*pPara
;
115 assert(pRun
->type
== diRun
);
116 assert(pRun
->member
.run
.nCharOfs
!= -1);
118 pPara
= ME_FindItemBack(pRun
, diParagraph
);
120 assert(pPara
->type
==diParagraph
);
121 return pPara
->member
.para
.nCharOfs
+ pRun
->member
.run
.nCharOfs
122 + ME_VPosToPos(pRun
->member
.run
.strText
, nOfs
);
125 void ME_CursorFromCharOfs(ME_TextEditor
*editor
, int nCharOfs
, ME_Cursor
*pCursor
)
127 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pCursor
->pRun
, &pCursor
->nOffset
);
130 void ME_RunOfsFromCharOfs(ME_TextEditor
*editor
, int nCharOfs
, ME_DisplayItem
**ppRun
, int *pOfs
)
132 ME_DisplayItem
*pPara
;
135 pPara
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
139 while (pPara
->type
== diParagraph
)
141 nParaOfs
= pPara
->member
.para
.nCharOfs
;
142 assert(nCharOfs
>= nParaOfs
);
144 if (nCharOfs
< pPara
->member
.para
.next_para
->member
.para
.nCharOfs
)
146 *ppRun
= ME_FindItemFwd(pPara
, diRun
);
148 while (!((*ppRun
)->member
.run
.nFlags
& MERF_ENDPARA
))
150 ME_DisplayItem
*pNext
= ME_FindItemFwd(*ppRun
, diRun
);
152 assert(pNext
->type
== diRun
);
153 if (nCharOfs
< nParaOfs
+ pNext
->member
.run
.nCharOfs
) {
154 *pOfs
= ME_PosToVPos((*ppRun
)->member
.run
.strText
,
155 nCharOfs
- nParaOfs
- (*ppRun
)->member
.run
.nCharOfs
);
160 if (nCharOfs
== nParaOfs
+ (*ppRun
)->member
.run
.nCharOfs
) {
165 pPara
= pPara
->member
.para
.next_para
;
167 *ppRun
= ME_FindItemBack(editor
->pBuffer
->pLast
, diRun
);
169 assert((*ppRun
)->member
.run
.nFlags
& MERF_ENDPARA
);
172 void ME_JoinRuns(ME_TextEditor
*editor
, ME_DisplayItem
*p
)
174 ME_DisplayItem
*pNext
= p
->next
;
176 assert(p
->type
== diRun
&& pNext
->type
== diRun
);
177 assert(p
->member
.run
.nCharOfs
!= -1);
178 ME_GetParagraph(p
)->member
.para
.nFlags
|= MEPF_REWRAP
;
180 if (editor
->bCaretAtEnd
&& editor
->pCursors
[0].pRun
== pNext
)
181 editor
->bCaretAtEnd
= FALSE
;
182 for (i
=0; i
<editor
->nCursors
; i
++) {
183 if (editor
->pCursors
[i
].pRun
== pNext
) {
184 editor
->pCursors
[i
].pRun
= p
;
185 editor
->pCursors
[i
].nOffset
+= ME_StrVLen(p
->member
.run
.strText
);
189 ME_AppendString(p
->member
.run
.strText
, pNext
->member
.run
.strText
);
191 ME_DestroyDisplayItem(pNext
);
192 ME_UpdateRunFlags(editor
, &p
->member
.run
);
193 if(TRACE_ON(richedit
))
195 TRACE("Before check after join\n");
196 ME_CheckCharOffsets(editor
);
197 TRACE("After check after join\n");
201 ME_DisplayItem
*ME_SplitRun(ME_Context
*c
, ME_DisplayItem
*item
, int nVChar
)
203 ME_TextEditor
*editor
= c
->editor
;
204 ME_DisplayItem
*item2
= NULL
;
206 ME_Paragraph
*para
= &ME_GetParagraph(item
)->member
.para
;
208 assert(item
->member
.run
.nCharOfs
!= -1);
209 if(TRACE_ON(richedit
))
211 TRACE("Before check before split\n");
212 ME_CheckCharOffsets(editor
);
213 TRACE("After check before split\n");
216 run
= &item
->member
.run
;
218 TRACE("Before split: %s(%ld, %ld)\n", debugstr_w(run
->strText
->szData
),
219 run
->pt
.x
, run
->pt
.y
);
221 item2
= ME_SplitRunSimple(editor
, item
, nVChar
);
223 run2
= &item2
->member
.run
;
225 ME_CalcRunExtent(c
, para
, run
);
226 ME_CalcRunExtent(c
, para
, run2
);
228 run2
->pt
.x
= run
->pt
.x
+run
->nWidth
;
229 run2
->pt
.y
= run
->pt
.y
;
231 if(TRACE_ON(richedit
))
233 TRACE("Before check after split\n");
234 ME_CheckCharOffsets(editor
);
235 TRACE("After check after split\n");
236 TRACE("After split: %s(%ld, %ld), %s(%ld, %ld)\n",
237 debugstr_w(run
->strText
->szData
), run
->pt
.x
, run
->pt
.y
,
238 debugstr_w(run2
->strText
->szData
), run2
->pt
.x
, run2
->pt
.y
);
244 /* split a run starting from voffset */
245 ME_DisplayItem
*ME_SplitRunSimple(ME_TextEditor
*editor
, ME_DisplayItem
*item
, int nVChar
)
247 ME_Run
*run
= &item
->member
.run
;
248 ME_DisplayItem
*item2
;
251 assert(nVChar
> 0 && nVChar
< ME_StrVLen(run
->strText
));
252 assert(item
->type
== diRun
);
253 assert(!(item
->member
.run
.nFlags
& (MERF_GRAPHICS
| MERF_TAB
)));
254 assert(item
->member
.run
.nCharOfs
!= -1);
256 item2
= ME_MakeRun(run
->style
,
257 ME_VSplitString(run
->strText
, nVChar
), run
->nFlags
&MERF_SPLITMASK
);
259 item2
->member
.run
.nCharOfs
= item
->member
.run
.nCharOfs
+
260 ME_VPosToPos(item
->member
.run
.strText
, nVChar
);
262 run2
= &item2
->member
.run
;
263 ME_InsertBefore(item
->next
, item2
);
265 ME_UpdateRunFlags(editor
, run
);
266 ME_UpdateRunFlags(editor
, run2
);
267 for (i
=0; i
<editor
->nCursors
; i
++) {
268 if (editor
->pCursors
[i
].pRun
== item
&&
269 editor
->pCursors
[i
].nOffset
>= nVChar
) {
270 assert(item2
->type
== diRun
);
271 editor
->pCursors
[i
].pRun
= item2
;
272 editor
->pCursors
[i
].nOffset
-= nVChar
;
275 ME_GetParagraph(item
)->member
.para
.nFlags
|= MEPF_REWRAP
;
279 ME_DisplayItem
*ME_MakeRun(ME_Style
*s
, ME_String
*strData
, int nFlags
)
281 ME_DisplayItem
*item
= ME_MakeDI(diRun
);
282 item
->member
.run
.style
= s
;
283 item
->member
.run
.strText
= strData
;
284 item
->member
.run
.nFlags
= nFlags
;
285 item
->member
.run
.nCharOfs
= -1;
290 ME_DisplayItem
*ME_InsertRun(ME_TextEditor
*editor
, int nCharOfs
, ME_DisplayItem
*pItem
)
296 assert(pItem
->type
== diRun
|| pItem
->type
== diUndoInsertRun
);
298 pUI
= ME_AddUndoItem(editor
, diUndoDeleteRun
, NULL
);
300 pUI
->nStart
= nCharOfs
;
301 pUI
->nLen
= pItem
->member
.run
.strText
->nLen
;
303 ME_CursorFromCharOfs(editor
, nCharOfs
, &tmp
);
305 tmp
.pRun
= ME_SplitRunSimple(editor
, tmp
.pRun
, tmp
.nOffset
);
308 pDI
= ME_MakeRun(pItem
->member
.run
.style
, ME_StrDup(pItem
->member
.run
.strText
), pItem
->member
.run
.nFlags
);
309 pDI
->member
.run
.nCharOfs
= tmp
.pRun
->member
.run
.nCharOfs
;
310 ME_InsertBefore(tmp
.pRun
, pDI
);
311 TRACE("Shift length:%d\n", pDI
->member
.run
.strText
->nLen
);
312 ME_PropagateCharOffset(tmp
.pRun
, pDI
->member
.run
.strText
->nLen
);
313 ME_GetParagraph(tmp
.pRun
)->member
.para
.nFlags
|= MEPF_REWRAP
;
318 void ME_UpdateRunFlags(ME_TextEditor
*editor
, ME_Run
*run
)
320 assert(run
->nCharOfs
!= -1);
321 if (ME_IsSplitable(run
->strText
))
322 run
->nFlags
|= MERF_SPLITTABLE
;
324 run
->nFlags
&= ~MERF_SPLITTABLE
;
326 if (!(run
->nFlags
& MERF_NOTEXT
)) {
327 if (ME_IsWhitespaces(run
->strText
))
328 run
->nFlags
|= MERF_WHITESPACE
| MERF_STARTWHITE
| MERF_ENDWHITE
;
331 run
->nFlags
&= ~MERF_WHITESPACE
;
333 if (ME_IsWSpace(ME_GetCharFwd(run
->strText
,0)))
334 run
->nFlags
|= MERF_STARTWHITE
;
336 run
->nFlags
&= ~MERF_STARTWHITE
;
338 if (ME_IsWSpace(ME_GetCharBack(run
->strText
,0)))
339 run
->nFlags
|= MERF_ENDWHITE
;
341 run
->nFlags
&= ~MERF_ENDWHITE
;
345 run
->nFlags
&= ~(MERF_WHITESPACE
| MERF_STARTWHITE
| MERF_ENDWHITE
);
348 void ME_GetGraphicsSize(ME_TextEditor
*editor
, ME_Run
*run
, SIZE
*pSize
)
350 assert(run
->nFlags
& MERF_GRAPHICS
);
355 int ME_CharFromPoint(ME_TextEditor
*editor
, int cx
, ME_Paragraph
*para
, ME_Run
*run
)
361 if (!run
->strText
->nLen
)
364 if (run
->nFlags
& MERF_TAB
)
366 if (cx
< run
->nWidth
/2)
370 if (run
->nFlags
& MERF_GRAPHICS
)
373 ME_GetGraphicsSize(editor
, run
, &sz
);
378 hDC
= GetDC(editor
->hWnd
);
379 hOldFont
= ME_SelectStyleFont(editor
, hDC
, run
->style
);
380 GetTextExtentExPointW(hDC
, run
->strText
->szData
, run
->strText
->nLen
,
381 cx
, &fit
, NULL
, &sz
);
382 ME_UnselectStyleFont(editor
, hDC
, run
->style
, hOldFont
);
383 ReleaseDC(editor
->hWnd
, hDC
);
387 int ME_CharFromPointCursor(ME_TextEditor
*editor
, int cx
, ME_Run
*run
)
389 int fit
= 0, fit1
= 0;
393 if (!run
->strText
->nLen
)
396 if (run
->nFlags
& MERF_TAB
)
398 if (cx
< run
->nWidth
/2)
402 if (run
->nFlags
& MERF_GRAPHICS
)
405 ME_GetGraphicsSize(editor
, run
, &sz
);
411 hDC
= GetDC(editor
->hWnd
);
412 hOldFont
= ME_SelectStyleFont(editor
, hDC
, run
->style
);
413 GetTextExtentExPointW(hDC
, run
->strText
->szData
, run
->strText
->nLen
,
414 cx
, &fit
, NULL
, &sz
);
415 if (fit
!= run
->strText
->nLen
)
419 GetTextExtentPoint32W(hDC
, run
->strText
->szData
, fit
, &sz2
);
420 fit1
= ME_StrRelPos(run
->strText
, fit
, &chars
);
421 GetTextExtentPoint32W(hDC
, run
->strText
->szData
, fit1
, &sz3
);
422 if (cx
>= (sz2
.cx
+sz3
.cx
)/2)
425 ME_UnselectStyleFont(editor
, hDC
, run
->style
, hOldFont
);
426 ReleaseDC(editor
->hWnd
, hDC
);
430 int ME_PointFromChar(ME_TextEditor
*editor
, ME_Run
*pRun
, int nOffset
)
433 HDC hDC
= GetDC(editor
->hWnd
);
436 if (pRun
->nFlags
& MERF_GRAPHICS
)
438 if (!nOffset
) return 0;
439 ME_GetGraphicsSize(editor
, pRun
, &size
);
442 hOldFont
= ME_SelectStyleFont(editor
, hDC
, pRun
->style
);
443 GetTextExtentPoint32W(hDC
, pRun
->strText
->szData
, nOffset
, &size
);
444 ME_UnselectStyleFont(editor
, hDC
, pRun
->style
, hOldFont
);
445 ReleaseDC(editor
->hWnd
, hDC
);
449 void ME_GetTextExtent(ME_Context
*c
, LPCWSTR szText
, int nChars
, ME_Style
*s
,
454 hOldFont
= ME_SelectStyleFont(c
->editor
, hDC
, s
);
455 GetTextExtentPoint32W(hDC
, szText
, nChars
, size
);
456 ME_UnselectStyleFont(c
->editor
, hDC
, s
, hOldFont
);
459 SIZE
ME_GetRunSizeCommon(ME_Context
*c
, ME_Paragraph
*para
, ME_Run
*run
, int nLen
, int *pAscent
, int *pDescent
)
462 int nMaxLen
= ME_StrVLen(run
->strText
);
467 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
468 * this is wasteful for graphics and TAB runs, but that shouldn't matter
471 ME_GetTextExtent(c
, run
->strText
->szData
, nLen
, run
->style
, &size
);
472 *pAscent
= run
->style
->tm
.tmAscent
;
473 *pDescent
= run
->style
->tm
.tmDescent
;
474 size
.cy
= *pAscent
+ *pDescent
;
476 if (run
->nFlags
& MERF_TAB
)
478 int pos
= 0, i
= 0, ppos
;
479 int lpsx
= GetDeviceCaps(c
->hDC
, LOGPIXELSX
);
480 PARAFORMAT2
*pFmt
= para
->pFmt
;
482 if (i
< pFmt
->cTabCount
)
484 pos
= pFmt
->rgxTabs
[i
]&0x00FFFFFF;
489 pos
+= 720-(pos
%720);
491 ppos
= pos
*lpsx
/1440;
492 if (ppos
>run
->pt
.x
) {
493 size
.cx
= ppos
- run
->pt
.x
;
497 size
.cy
= *pAscent
+ *pDescent
;
500 if (run
->nFlags
& MERF_GRAPHICS
)
502 ME_GetGraphicsSize(c
->editor
, run
, &size
);
503 if (size
.cy
> *pAscent
)
505 /* descent is unchanged */
512 SIZE
ME_GetRunSize(ME_Context
*c
, ME_Paragraph
*para
, ME_Run
*run
, int nLen
)
515 return ME_GetRunSizeCommon(c
, para
, run
, nLen
, &asc
, &desc
);
518 void ME_CalcRunExtent(ME_Context
*c
, ME_Paragraph
*para
, ME_Run
*run
)
520 int nEnd
= ME_StrVLen(run
->strText
);
521 SIZE size
= ME_GetRunSizeCommon(c
, para
, run
, nEnd
, &run
->nAscent
, &run
->nDescent
);
522 run
->nWidth
= size
.cx
;
524 WARN("size.cx == 0\n");
527 void ME_MustBeWrapped(ME_Context
*c
, ME_DisplayItem
*para
)
529 assert(para
->type
== diParagraph
);
533 void ME_SetSelectionCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*pFmt
)
536 ME_GetSelection(editor
, &nFrom
, &nTo
);
540 if (!editor
->pBuffer
->pCharStyle
)
541 editor
->pBuffer
->pCharStyle
= ME_GetInsertStyle(editor
, 0);
542 s
= ME_ApplyStyle(editor
->pBuffer
->pCharStyle
, pFmt
);
543 ME_ReleaseStyle(editor
->pBuffer
->pCharStyle
);
544 editor
->pBuffer
->pCharStyle
= s
;
547 ME_SetCharFormat(editor
, nFrom
, nTo
-nFrom
, pFmt
);
550 void ME_SetCharFormat(ME_TextEditor
*editor
, int nOfs
, int nChars
, CHARFORMAT2W
*pFmt
)
553 ME_DisplayItem
*para
;
555 ME_CursorFromCharOfs(editor
, nOfs
, &tmp
);
557 tmp
.pRun
= ME_SplitRunSimple(editor
, tmp
.pRun
, tmp
.nOffset
);
559 ME_CursorFromCharOfs(editor
, nOfs
+nChars
, &tmp2
);
561 tmp2
.pRun
= ME_SplitRunSimple(editor
, tmp2
.pRun
, tmp2
.nOffset
);
563 para
= ME_GetParagraph(tmp
.pRun
);
564 para
->member
.para
.nFlags
|= MEPF_REWRAP
;
566 while(tmp
.pRun
!= tmp2
.pRun
)
568 ME_UndoItem
*undo
= NULL
;
569 ME_Style
*new_style
= ME_ApplyStyle(tmp
.pRun
->member
.run
.style
, pFmt
);
570 /* ME_DumpStyle(new_style); */
571 undo
= ME_AddUndoItem(editor
, diUndoSetCharFormat
, NULL
);
573 undo
->nStart
= tmp
.pRun
->member
.run
.nCharOfs
+para
->member
.para
.nCharOfs
;
574 undo
->nLen
= tmp
.pRun
->member
.run
.strText
->nLen
;
575 undo
->di
.member
.ustyle
= tmp
.pRun
->member
.run
.style
;
576 /* we'd have to addref undo..ustyle and release tmp...style
577 but they'd cancel each other out so we can do nothing instead */
580 ME_ReleaseStyle(tmp
.pRun
->member
.run
.style
);
581 tmp
.pRun
->member
.run
.style
= new_style
;
582 tmp
.pRun
= ME_FindItemFwd(tmp
.pRun
, diRunOrParagraph
);
583 if (tmp
.pRun
->type
== diParagraph
)
586 tmp
.pRun
= ME_FindItemFwd(tmp
.pRun
, diRun
);
587 if (tmp
.pRun
!= tmp2
.pRun
)
588 para
->member
.para
.nFlags
|= MEPF_REWRAP
;
594 void ME_SetDefaultCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*mod
)
599 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
600 undo
= ME_AddUndoItem(editor
, diUndoSetDefaultCharFormat
, NULL
);
604 undo
->di
.member
.ustyle
= editor
->pBuffer
->pDefaultStyle
;
605 ME_AddRefStyle(undo
->di
.member
.ustyle
);
607 style
= ME_ApplyStyle(editor
->pBuffer
->pDefaultStyle
, mod
);
608 editor
->pBuffer
->pDefaultStyle
->fmt
= style
->fmt
;
609 editor
->pBuffer
->pDefaultStyle
->tm
= style
->tm
;
610 ME_ReleaseStyle(style
);
611 ME_MarkAllForWrapping(editor
);
612 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
615 void ME_GetRunCharFormat(ME_TextEditor
*editor
, ME_DisplayItem
*run
, CHARFORMAT2W
*pFmt
)
617 ME_CopyCharFormat(pFmt
, &run
->member
.run
.style
->fmt
);
620 void ME_GetDefaultCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*pFmt
)
623 ME_GetSelection(editor
, &nFrom
, &nTo
);
624 ME_CopyCharFormat(pFmt
, &editor
->pBuffer
->pDefaultStyle
->fmt
);
627 void ME_GetSelectionCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*pFmt
)
630 ME_GetSelection(editor
, &nFrom
, &nTo
);
631 if (nFrom
== nTo
&& editor
->pBuffer
->pCharStyle
)
633 ME_CopyCharFormat(pFmt
, &editor
->pBuffer
->pCharStyle
->fmt
);
636 ME_GetCharFormat(editor
, nFrom
, nTo
, pFmt
);
639 void ME_GetCharFormat(ME_TextEditor
*editor
, int nFrom
, int nTo
, CHARFORMAT2W
*pFmt
)
641 ME_DisplayItem
*run
, *run_end
;
642 int nOffset
, nOffset2
;
645 ME_RunOfsFromCharOfs(editor
, nFrom
, &run
, &nOffset
);
646 if (nFrom
== nTo
) /* special case - if selection is empty, take previous char's formatting */
650 ME_DisplayItem
*tmp_run
= ME_FindItemBack(run
, diRunOrParagraph
);
651 if (tmp_run
->type
== diRun
) {
652 ME_GetRunCharFormat(editor
, tmp_run
, pFmt
);
656 ME_GetRunCharFormat(editor
, run
, pFmt
);
660 if (nTo
>nFrom
) /* selection consists of chars from nFrom up to nTo-1 */
662 ME_RunOfsFromCharOfs(editor
, nTo
, &run_end
, &nOffset2
);
664 ME_GetRunCharFormat(editor
, run
, pFmt
);
666 if (run
== run_end
) return;
669 /* FIXME add more style feature comparisons */
670 int nAttribs
= CFM_SIZE
| CFM_FACE
| CFM_COLOR
;
671 int nEffects
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
;
673 run
= ME_FindItemFwd(run
, diRun
);
675 ZeroMemory(&tmp
, sizeof(tmp
));
676 tmp
.cbSize
= sizeof(tmp
);
677 ME_GetRunCharFormat(editor
, run
, &tmp
);
679 assert((tmp
.dwMask
& nAttribs
) == nAttribs
);
680 assert((tmp
.dwMask
& nEffects
) == nEffects
);
681 /* reset flags that differ */
683 if (pFmt
->yHeight
!= tmp
.yHeight
)
684 pFmt
->dwMask
&= ~CFM_SIZE
;
685 if (pFmt
->dwMask
& CFM_FACE
)
687 if (!(tmp
.dwMask
& CFM_FACE
))
688 pFmt
->dwMask
&= ~CFM_FACE
;
689 else if (lstrcmpW(pFmt
->szFaceName
, tmp
.szFaceName
))
690 pFmt
->dwMask
&= ~CFM_FACE
;
692 if (pFmt
->yHeight
!= tmp
.yHeight
)
693 pFmt
->dwMask
&= ~CFM_SIZE
;
694 if (pFmt
->dwMask
& CFM_COLOR
)
696 if (!((pFmt
->dwEffects
&CFE_AUTOCOLOR
) & (tmp
.dwEffects
&CFE_AUTOCOLOR
)))
698 if (pFmt
->crTextColor
!= tmp
.crTextColor
)
699 pFmt
->dwMask
&= ~CFM_COLOR
;
703 pFmt
->dwMask
&= ~((pFmt
->dwEffects
^ tmp
.dwEffects
) & nEffects
);
705 } while(run
!= run_end
);