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
26 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
27 WINE_DECLARE_DEBUG_CHANNEL(richedit_check
);
28 WINE_DECLARE_DEBUG_CHANNEL(richedit_lists
);
30 ME_Run
*run_next( ME_Run
*run
)
32 ME_DisplayItem
*item
= run_get_di( run
);
34 if (ME_NextRun( NULL
, &item
, FALSE
))
35 return &item
->member
.run
;
40 ME_Run
*run_prev( ME_Run
*run
)
42 ME_DisplayItem
*item
= run_get_di( run
);
44 if (ME_PrevRun( NULL
, &item
, FALSE
))
45 return &item
->member
.run
;
50 /******************************************************************************
53 * Returns TRUE if two runs can be safely merged into one, FALSE otherwise.
55 BOOL
ME_CanJoinRuns(const ME_Run
*run1
, const ME_Run
*run2
)
57 if ((run1
->nFlags
| run2
->nFlags
) & MERF_NOJOIN
)
59 if (run1
->style
!= run2
->style
)
61 if ((run1
->nFlags
& MERF_STYLEFLAGS
) != (run2
->nFlags
& MERF_STYLEFLAGS
))
66 void ME_SkipAndPropagateCharOffset(ME_DisplayItem
*p
, int shift
)
68 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
70 ME_PropagateCharOffset(p
, shift
);
73 /******************************************************************************
74 * ME_PropagateCharOffsets
76 * Shifts (increases or decreases) character offset (relative to beginning of
77 * the document) of the part of the text starting from given place.
79 void ME_PropagateCharOffset(ME_DisplayItem
*p
, int shift
)
81 /* Runs in one paragraph contain character offset relative to their owning
82 * paragraph. If we start the shifting from the run, we need to shift
83 * all the relative offsets until the end of the paragraph
85 if (p
->type
== diRun
) /* propagate in all runs in this para */
87 TRACE("PropagateCharOffset(%s, %d)\n", debugstr_run( &p
->member
.run
), shift
);
89 p
->member
.run
.nCharOfs
+= shift
;
90 assert(p
->member
.run
.nCharOfs
>= 0);
91 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
92 } while(p
->type
== diRun
);
94 /* Runs in next paragraphs don't need their offsets updated, because they,
95 * again, those offsets are relative to their respective paragraphs.
96 * Instead of that, we're updating paragraphs' character offsets.
98 if (p
->type
== diParagraph
) /* propagate in all next paras */
101 p
->member
.para
.nCharOfs
+= shift
;
102 assert(p
->member
.para
.nCharOfs
>= 0);
103 p
= p
->member
.para
.next_para
;
104 } while(p
->type
== diParagraph
);
106 /* diTextEnd also has character offset in it, which makes finding text length
107 * easier. But it needs to be up to date first.
109 if (p
->type
== diTextEnd
)
111 p
->member
.para
.nCharOfs
+= shift
;
112 assert(p
->member
.para
.nCharOfs
>= 0);
116 /******************************************************************************
117 * ME_CheckCharOffsets
119 * Checks if editor lists' validity and optionally dumps the document structure
121 void ME_CheckCharOffsets(ME_TextEditor
*editor
)
123 ME_DisplayItem
*p
= editor
->pBuffer
->pFirst
;
124 int ofs
= 0, ofsp
= 0;
126 if (!TRACE_ON(richedit_check
))
129 TRACE_(richedit_check
)("Checking begin\n");
130 if(TRACE_ON(richedit_lists
))
132 TRACE_(richedit_lists
)("---\n");
133 ME_DumpDocument(editor
->pBuffer
);
136 p
= ME_FindItemFwd(p
, diRunOrParagraphOrEnd
);
139 TRACE_(richedit_check
)("tend, real ofsp = %d, counted = %d\n", p
->member
.para
.nCharOfs
, ofsp
+ofs
);
140 assert(ofsp
+ofs
== p
->member
.para
.nCharOfs
);
141 TRACE_(richedit_check
)("Checking finished\n");
144 TRACE_(richedit_check
)("para, real ofsp = %d, counted = %d\n", p
->member
.para
.nCharOfs
, ofsp
+ofs
);
145 assert(ofsp
+ofs
== p
->member
.para
.nCharOfs
);
146 ofsp
= p
->member
.para
.nCharOfs
;
150 TRACE_(richedit_check
)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = %s, flags=%08x, fx&mask = %08x\n",
151 p
->member
.run
.nCharOfs
, p
->member
.run
.nCharOfs
+ofsp
, ofsp
+ofs
,
152 p
->member
.run
.len
, debugstr_run( &p
->member
.run
),
153 p
->member
.run
.nFlags
,
154 p
->member
.run
.style
->fmt
.dwMask
& p
->member
.run
.style
->fmt
.dwEffects
);
155 assert(ofs
== p
->member
.run
.nCharOfs
);
156 assert(p
->member
.run
.len
);
157 ofs
+= p
->member
.run
.len
;
160 TRACE_(richedit_check
)("cell\n");
166 TRACE_(richedit_check
)("Checking finished\n");
169 /******************************************************************************
170 * ME_CharOfsFromRunOfs
172 * Converts a character position relative to the start of the run, to a
173 * character position relative to the start of the document.
174 * Kind of a "local to global" offset conversion.
176 int ME_CharOfsFromRunOfs(ME_TextEditor
*editor
, const ME_DisplayItem
*pPara
,
177 const ME_DisplayItem
*pRun
, int nOfs
)
179 assert(pRun
&& pRun
->type
== diRun
);
180 assert(pPara
&& pPara
->type
== diParagraph
);
181 return pPara
->member
.para
.nCharOfs
+ pRun
->member
.run
.nCharOfs
+ nOfs
;
184 /******************************************************************************
185 * ME_CursorFromCharOfs
187 * Converts a character offset (relative to the start of the document) to
188 * a cursor structure (which contains a run and a position relative to that
191 void ME_CursorFromCharOfs(ME_TextEditor
*editor
, int nCharOfs
, ME_Cursor
*pCursor
)
193 ME_RunOfsFromCharOfs(editor
, nCharOfs
, &pCursor
->pPara
,
194 &pCursor
->pRun
, &pCursor
->nOffset
);
197 /******************************************************************************
198 * ME_RunOfsFromCharOfs
200 * Find a run and relative character offset given an absolute character offset
201 * (absolute offset being an offset relative to the start of the document).
202 * Kind of a "global to local" offset conversion.
204 void ME_RunOfsFromCharOfs(ME_TextEditor
*editor
,
206 ME_DisplayItem
**ppPara
,
207 ME_DisplayItem
**ppRun
,
210 ME_DisplayItem
*item
, *next_item
;
212 nCharOfs
= max(nCharOfs
, 0);
213 nCharOfs
= min(nCharOfs
, ME_GetTextLength(editor
));
215 /* Find the paragraph at the offset. */
216 next_item
= editor
->pBuffer
->pFirst
->member
.para
.next_para
;
219 next_item
= item
->member
.para
.next_para
;
220 } while (next_item
->member
.para
.nCharOfs
<= nCharOfs
);
221 assert(item
->type
== diParagraph
);
222 nCharOfs
-= item
->member
.para
.nCharOfs
;
223 if (ppPara
) *ppPara
= item
;
225 /* Find the run at the offset. */
226 next_item
= ME_FindItemFwd(item
, diRun
);
229 next_item
= ME_FindItemFwd(item
, diRunOrParagraphOrEnd
);
230 } while (next_item
->type
== diRun
&&
231 next_item
->member
.run
.nCharOfs
<= nCharOfs
);
232 assert(item
->type
== diRun
);
233 nCharOfs
-= item
->member
.run
.nCharOfs
;
235 if (ppRun
) *ppRun
= item
;
236 if (pOfs
) *pOfs
= nCharOfs
;
239 /******************************************************************************
242 * Merges two adjacent runs, the one given as a parameter and the next one.
244 void run_join( ME_TextEditor
*editor
, ME_Run
*run
)
246 ME_Run
*next
= run_next( run
);
250 assert( run
->nCharOfs
!= -1 );
251 para_mark_rewrap( editor
, run
->para
);
253 /* Update all cursors so that they don't contain the soon deleted run */
254 for (i
= 0; i
< editor
->nCursors
; i
++)
256 if (&editor
->pCursors
[i
].pRun
->member
.run
== next
)
258 editor
->pCursors
[i
].pRun
= run_get_di( run
);
259 editor
->pCursors
[i
].nOffset
+= run
->len
;
263 run
->len
+= next
->len
;
264 ME_Remove( run_get_di( next
) );
265 ME_DestroyDisplayItem( run_get_di( next
) );
266 ME_UpdateRunFlags( editor
, run
);
267 ME_CheckCharOffsets( editor
);
270 /******************************************************************************
273 * Does the most basic job of splitting a run into two - it does not
274 * update the positions and extents.
276 ME_DisplayItem
*ME_SplitRunSimple( ME_TextEditor
*editor
, ME_Cursor
*cursor
)
278 ME_Run
*run
= &cursor
->pRun
->member
.run
, *new_run
;
280 int nOffset
= cursor
->nOffset
;
282 assert( !(run
->nFlags
& MERF_NONTEXT
) );
284 new_run
= run_create( run
->style
, run
->nFlags
& MERF_SPLITMASK
);
285 new_run
->nCharOfs
= run
->nCharOfs
+ nOffset
;
286 new_run
->len
= run
->len
- nOffset
;
287 new_run
->para
= run
->para
;
289 cursor
->pRun
= run_get_di( new_run
);
292 ME_InsertBefore( run_get_di( run
)->next
, run_get_di( new_run
) );
294 ME_UpdateRunFlags( editor
, run
);
295 ME_UpdateRunFlags( editor
, new_run
);
296 for (i
= 0; i
< editor
->nCursors
; i
++)
298 if (editor
->pCursors
[i
].pRun
== run_get_di( run
) &&
299 editor
->pCursors
[i
].nOffset
>= nOffset
)
301 editor
->pCursors
[i
].pRun
= run_get_di( new_run
);
302 editor
->pCursors
[i
].nOffset
-= nOffset
;
305 para_mark_rewrap( editor
, run
->para
);
306 return run_get_di( run
);
309 /******************************************************************************
312 * A helper function to create run structures quickly.
314 ME_Run
*run_create( ME_Style
*s
, int flags
)
316 ME_DisplayItem
*item
= ME_MakeDI( diRun
);
317 ME_Run
*run
= &item
->member
.run
;
319 if (!item
) return NULL
;
331 run
->vis_attrs
= NULL
;
332 run
->advances
= NULL
;
334 run
->max_clusters
= 0;
335 run
->clusters
= NULL
;
339 /******************************************************************************
340 * ME_InsertRunAtCursor
342 * Inserts a new run with given style, flags and content at a given position,
343 * which is passed as a cursor structure (which consists of a run and
344 * a run-relative character offset).
347 ME_InsertRunAtCursor(ME_TextEditor
*editor
, ME_Cursor
*cursor
, ME_Style
*style
,
348 const WCHAR
*str
, int len
, int flags
)
350 ME_DisplayItem
*insert_before
= cursor
->pRun
, *prev
;
355 if (cursor
->nOffset
== cursor
->pRun
->member
.run
.len
)
357 insert_before
= ME_FindItemFwd( cursor
->pRun
, diRun
);
358 if (!insert_before
) insert_before
= cursor
->pRun
; /* Always insert before the final eop run */
362 ME_SplitRunSimple( editor
, cursor
);
363 insert_before
= cursor
->pRun
;
367 add_undo_delete_run( editor
, insert_before
->member
.run
.para
->nCharOfs
+
368 insert_before
->member
.run
.nCharOfs
, len
);
370 run
= run_create( style
, flags
);
371 run
->nCharOfs
= insert_before
->member
.run
.nCharOfs
;
373 run
->para
= insert_before
->member
.run
.para
;
374 ME_InsertString( run
->para
->text
, run
->nCharOfs
, str
, len
);
375 ME_InsertBefore( insert_before
, run_get_di( run
) );
376 TRACE("Shift length:%d\n", len
);
377 ME_PropagateCharOffset( insert_before
, len
);
378 para_mark_rewrap( editor
, insert_before
->member
.run
.para
);
380 /* Move any cursors that were at the end of the previous run to the end of the inserted run */
381 prev
= ME_FindItemBack( run_get_di( run
), diRun
);
386 for (i
= 0; i
< editor
->nCursors
; i
++)
388 if (editor
->pCursors
[i
].pRun
== prev
&&
389 editor
->pCursors
[i
].nOffset
== prev
->member
.run
.len
)
391 editor
->pCursors
[i
].pRun
= run_get_di( run
);
392 editor
->pCursors
[i
].nOffset
= len
;
397 return run_get_di( run
);
400 static BOOL
run_is_splittable( const ME_Run
*run
)
402 WCHAR
*str
= get_text( run
, 0 ), *p
;
404 BOOL found_ink
= FALSE
;
406 for (i
= 0, p
= str
; i
< run
->len
; i
++, p
++)
408 if (ME_IsWSpace( *p
))
410 if (found_ink
) return TRUE
;
418 static BOOL
run_is_entirely_ws( const ME_Run
*run
)
420 WCHAR
*str
= get_text( run
, 0 ), *p
;
423 for (i
= 0, p
= str
; i
< run
->len
; i
++, p
++)
424 if (!ME_IsWSpace( *p
)) return FALSE
;
429 /******************************************************************************
432 * Determine some of run attributes given its content (style, text content).
433 * Some flags cannot be determined by this function (MERF_GRAPHICS,
436 void ME_UpdateRunFlags(ME_TextEditor
*editor
, ME_Run
*run
)
438 assert(run
->nCharOfs
>= 0);
440 if (RUN_IS_HIDDEN(run
) || run
->nFlags
& MERF_TABLESTART
)
441 run
->nFlags
|= MERF_HIDDEN
;
443 run
->nFlags
&= ~MERF_HIDDEN
;
445 if (run_is_splittable( run
))
446 run
->nFlags
|= MERF_SPLITTABLE
;
448 run
->nFlags
&= ~MERF_SPLITTABLE
;
450 if (!(run
->nFlags
& MERF_NOTEXT
))
452 if (run_is_entirely_ws( run
))
453 run
->nFlags
|= MERF_WHITESPACE
| MERF_STARTWHITE
| MERF_ENDWHITE
;
456 run
->nFlags
&= ~MERF_WHITESPACE
;
458 if (ME_IsWSpace( *get_text( run
, 0 ) ))
459 run
->nFlags
|= MERF_STARTWHITE
;
461 run
->nFlags
&= ~MERF_STARTWHITE
;
463 if (ME_IsWSpace( *get_text( run
, run
->len
- 1 ) ))
464 run
->nFlags
|= MERF_ENDWHITE
;
466 run
->nFlags
&= ~MERF_ENDWHITE
;
470 run
->nFlags
&= ~(MERF_WHITESPACE
| MERF_STARTWHITE
| MERF_ENDWHITE
);
473 /******************************************************************************
474 * ME_CharFromPointContext
476 * Returns a character position inside the run given a run-relative
477 * pixel horizontal position.
479 * If closest is FALSE return the actual character
480 * If closest is TRUE will round to the closest leading edge.
481 * ie. if the second character is at pixel position 8 and third at 16 then for:
482 * closest = FALSE cx = 0..7 return 0, cx = 8..15 return 1
483 * closest = TRUE cx = 0..3 return 0, cx = 4..11 return 1.
485 int ME_CharFromPointContext(ME_Context
*c
, int cx
, ME_Run
*run
, BOOL closest
, BOOL visual_order
)
487 ME_String
*mask_text
= NULL
;
491 if (!run
->len
|| cx
<= 0)
494 if (run
->nFlags
& (MERF_TAB
| MERF_ENDCELL
))
496 if (!closest
|| cx
< run
->nWidth
/ 2) return 0;
500 if (run
->nFlags
& MERF_GRAPHICS
)
503 ME_GetOLEObjectSize(c
, run
, &sz
);
504 if (!closest
|| cx
< sz
.cx
/ 2) return 0;
508 if (run
->para
->nFlags
& MEPF_COMPLEX
)
511 if (visual_order
&& run
->script_analysis
.fRTL
) cx
= run
->nWidth
- cx
- 1;
513 ScriptXtoCP( cx
, run
->len
, run
->num_glyphs
, run
->clusters
, run
->vis_attrs
, run
->advances
, &run
->script_analysis
,
515 TRACE("x %d cp %d trailing %d (run width %d) rtl %d log order %d\n", cx
, cp
, trailing
, run
->nWidth
,
516 run
->script_analysis
.fRTL
, run
->script_analysis
.fLogicalOrder
);
517 return closest
? cp
+ trailing
: cp
;
520 if (c
->editor
->cPasswordMask
)
522 mask_text
= ME_MakeStringR( c
->editor
->cPasswordMask
, run
->len
);
523 str
= mask_text
->szData
;
526 str
= get_text( run
, 0 );
528 select_style(c
, run
->style
);
529 GetTextExtentExPointW(c
->hDC
, str
, run
->len
,
530 cx
, &fit
, NULL
, &sz
);
531 if (closest
&& fit
!= run
->len
)
533 GetTextExtentPoint32W(c
->hDC
, str
, fit
, &sz2
);
534 GetTextExtentPoint32W(c
->hDC
, str
, fit
+ 1, &sz3
);
535 if (cx
>= (sz2
.cx
+sz3
.cx
)/2)
539 ME_DestroyString( mask_text
);
544 int ME_CharFromPoint(ME_TextEditor
*editor
, int cx
, ME_Run
*run
, BOOL closest
, BOOL visual_order
)
549 ME_InitContext( &c
, editor
, ITextHost_TxGetDC( editor
->texthost
) );
550 ret
= ME_CharFromPointContext( &c
, cx
, run
, closest
, visual_order
);
551 ME_DestroyContext(&c
);
555 /******************************************************************************
558 * Finds a width and a height of the text using a specified style
560 static void ME_GetTextExtent(ME_Context
*c
, LPCWSTR szText
, int nChars
, ME_Style
*s
, SIZE
*size
)
564 select_style( c
, s
);
565 GetTextExtentPoint32W( c
->hDC
, szText
, nChars
, size
);
574 /******************************************************************************
575 * ME_PointFromCharContext
577 * Returns a run-relative pixel position given a run-relative character
578 * position (character offset)
580 int ME_PointFromCharContext(ME_Context
*c
, ME_Run
*pRun
, int nOffset
, BOOL visual_order
)
583 ME_String
*mask_text
= NULL
;
586 if (pRun
->nFlags
& MERF_GRAPHICS
)
589 ME_GetOLEObjectSize(c
, pRun
, &size
);
591 } else if (pRun
->nFlags
& MERF_ENDPARA
) {
595 if (pRun
->para
->nFlags
& MEPF_COMPLEX
)
598 ScriptCPtoX( nOffset
, FALSE
, pRun
->len
, pRun
->num_glyphs
, pRun
->clusters
,
599 pRun
->vis_attrs
, pRun
->advances
, &pRun
->script_analysis
, &x
);
600 if (visual_order
&& pRun
->script_analysis
.fRTL
) x
= pRun
->nWidth
- x
- 1;
603 if (c
->editor
->cPasswordMask
)
605 mask_text
= ME_MakeStringR(c
->editor
->cPasswordMask
, pRun
->len
);
606 str
= mask_text
->szData
;
609 str
= get_text( pRun
, 0 );
611 ME_GetTextExtent(c
, str
, nOffset
, pRun
->style
, &size
);
612 ME_DestroyString( mask_text
);
616 /******************************************************************************
619 * Calls ME_PointFromCharContext after first creating a context.
621 int ME_PointFromChar(ME_TextEditor
*editor
, ME_Run
*pRun
, int nOffset
, BOOL visual_order
)
626 ME_InitContext(&c
, editor
, ITextHost_TxGetDC(editor
->texthost
));
627 ret
= ME_PointFromCharContext( &c
, pRun
, nOffset
, visual_order
);
628 ME_DestroyContext(&c
);
633 /******************************************************************************
634 * ME_GetRunSizeCommon
636 * Finds width, height, ascent and descent of a run, up to given character
639 SIZE
ME_GetRunSizeCommon(ME_Context
*c
, const ME_Paragraph
*para
, ME_Run
*run
, int nLen
,
640 int startx
, int *pAscent
, int *pDescent
)
642 static const WCHAR spaceW
[] = {' ',0};
645 nLen
= min( nLen
, run
->len
);
647 if (run
->nFlags
& MERF_ENDPARA
)
649 nLen
= min( nLen
, 1 );
650 ME_GetTextExtent(c
, spaceW
, nLen
, run
->style
, &size
);
652 else if (para
->nFlags
& MEPF_COMPLEX
)
654 size
.cx
= run
->nWidth
;
656 else if (c
->editor
->cPasswordMask
)
658 ME_String
*szMasked
= ME_MakeStringR(c
->editor
->cPasswordMask
,nLen
);
659 ME_GetTextExtent(c
, szMasked
->szData
, nLen
,run
->style
, &size
);
660 ME_DestroyString(szMasked
);
664 ME_GetTextExtent(c
, get_text( run
, 0 ), nLen
, run
->style
, &size
);
666 *pAscent
= run
->style
->tm
.tmAscent
;
667 *pDescent
= run
->style
->tm
.tmDescent
;
668 size
.cy
= *pAscent
+ *pDescent
;
670 if (run
->nFlags
& MERF_TAB
)
672 int pos
= 0, i
= 0, ppos
, shift
= 0;
673 const PARAFORMAT2
*pFmt
= ¶
->fmt
;
675 if (c
->editor
->bEmulateVersion10
&& /* v1.0 - 3.0 */
676 pFmt
->dwMask
& PFM_TABLE
&& pFmt
->wEffects
& PFE_TABLE
)
677 /* The horizontal gap shifts the tab positions to leave the gap. */
678 shift
= pFmt
->dxOffset
* 2;
680 if (i
< pFmt
->cTabCount
)
682 /* Only one side of the horizontal gap is needed at the end of
684 if (i
== pFmt
->cTabCount
-1)
686 pos
= shift
+ (pFmt
->rgxTabs
[i
]&0x00FFFFFF);
691 pos
+= lDefaultTab
- (pos
% lDefaultTab
);
693 ppos
= ME_twips2pointsX(c
, pos
);
694 if (ppos
> startx
+ run
->pt
.x
) {
695 size
.cx
= ppos
- startx
- run
->pt
.x
;
699 size
.cy
= *pAscent
+ *pDescent
;
702 if (run
->nFlags
& MERF_GRAPHICS
)
704 ME_GetOLEObjectSize(c
, run
, &size
);
705 if (size
.cy
> *pAscent
)
707 /* descent is unchanged */
713 /******************************************************************************
714 * ME_SetSelectionCharFormat
716 * Applies a style change, either to a current selection, or to insert cursor
717 * (ie. the style next typed characters will use).
719 void ME_SetSelectionCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*pFmt
)
721 if (!ME_IsSelection(editor
))
724 if (!editor
->pBuffer
->pCharStyle
)
725 editor
->pBuffer
->pCharStyle
= ME_GetInsertStyle(editor
, 0);
726 s
= ME_ApplyStyle(editor
, editor
->pBuffer
->pCharStyle
, pFmt
);
727 ME_ReleaseStyle(editor
->pBuffer
->pCharStyle
);
728 editor
->pBuffer
->pCharStyle
= s
;
730 ME_Cursor
*from
, *to
;
731 ME_GetSelection(editor
, &from
, &to
);
732 ME_SetCharFormat(editor
, from
, to
, pFmt
);
736 /******************************************************************************
739 * Applies a style change to the specified part of the text
741 * The start and end cursors specify the part of the text. These cursors will
742 * be updated to stay valid, but this function may invalidate other
743 * non-selection cursors. The end cursor may be NULL to specify all the text
744 * following the start cursor.
746 * If no text is selected, then nothing is done.
748 void ME_SetCharFormat(ME_TextEditor
*editor
, ME_Cursor
*start
, ME_Cursor
*end
, CHARFORMAT2W
*pFmt
)
750 ME_DisplayItem
*run
, *start_run
= start
->pRun
, *end_run
= NULL
;
752 if (end
&& start
->pRun
== end
->pRun
&& start
->nOffset
== end
->nOffset
)
755 if (start
->nOffset
== start
->pRun
->member
.run
.len
)
756 start_run
= ME_FindItemFwd( start
->pRun
, diRun
);
757 else if (start
->nOffset
)
759 /* SplitRunSimple may or may not update the cursors, depending on whether they
760 * are selection cursors, but we need to make sure they are valid. */
761 int split_offset
= start
->nOffset
;
762 ME_DisplayItem
*split_run
= ME_SplitRunSimple(editor
, start
);
763 start_run
= start
->pRun
;
764 if (end
&& end
->pRun
== split_run
)
766 end
->pRun
= start
->pRun
;
767 end
->nOffset
-= split_offset
;
773 if (end
->nOffset
== end
->pRun
->member
.run
.len
)
774 end_run
= ME_FindItemFwd( end
->pRun
, diRun
);
777 if (end
->nOffset
) ME_SplitRunSimple(editor
, end
);
782 for (run
= start_run
; run
!= end_run
; run
= ME_FindItemFwd( run
, diRun
))
784 ME_Style
*new_style
= ME_ApplyStyle(editor
, run
->member
.run
.style
, pFmt
);
785 ME_Paragraph
*para
= run
->member
.run
.para
;
787 add_undo_set_char_fmt( editor
, run
->member
.run
.para
->nCharOfs
+ run
->member
.run
.nCharOfs
,
788 run
->member
.run
.len
, &run
->member
.run
.style
->fmt
);
789 ME_ReleaseStyle(run
->member
.run
.style
);
790 run
->member
.run
.style
= new_style
;
792 /* The para numbering style depends on the eop style */
793 if ((run
->member
.run
.nFlags
& MERF_ENDPARA
) && para
->para_num
.style
)
795 ME_ReleaseStyle(para
->para_num
.style
);
796 para
->para_num
.style
= NULL
;
798 para_mark_rewrap( editor
, para
);
802 static void ME_GetRunCharFormat(ME_TextEditor
*editor
, ME_DisplayItem
*run
, CHARFORMAT2W
*pFmt
)
804 ME_CopyCharFormat(pFmt
, &run
->member
.run
.style
->fmt
);
807 /******************************************************************************
808 * ME_GetDefaultCharFormat
810 * Retrieves the current default character style (the one applied where no
811 * other style was applied) .
813 void ME_GetDefaultCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*pFmt
)
815 ME_CopyCharFormat(pFmt
, &editor
->pBuffer
->pDefaultStyle
->fmt
);
818 /******************************************************************************
819 * ME_GetSelectionCharFormat
821 * If selection exists, it returns all style elements that are set consistently
822 * in the whole selection. If not, it just returns the current style.
824 void ME_GetSelectionCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*pFmt
)
826 ME_Cursor
*from
, *to
;
827 if (!ME_IsSelection(editor
) && editor
->pBuffer
->pCharStyle
)
829 ME_CopyCharFormat(pFmt
, &editor
->pBuffer
->pCharStyle
->fmt
);
832 ME_GetSelection(editor
, &from
, &to
);
833 ME_GetCharFormat(editor
, from
, to
, pFmt
);
836 /******************************************************************************
839 * Returns the style consisting of those attributes which are consistently set
840 * in the whole character range.
842 void ME_GetCharFormat(ME_TextEditor
*editor
, const ME_Cursor
*from
,
843 const ME_Cursor
*to
, CHARFORMAT2W
*pFmt
)
845 ME_DisplayItem
*run
, *run_end
;
849 /* special case - if selection is empty, take previous char's formatting */
850 if (from
->pRun
== to
->pRun
&& from
->nOffset
== to
->nOffset
)
854 ME_DisplayItem
*tmp_run
= ME_FindItemBack(run
, diRunOrParagraph
);
855 if (tmp_run
->type
== diRun
) {
856 ME_GetRunCharFormat(editor
, tmp_run
, pFmt
);
860 ME_GetRunCharFormat(editor
, run
, pFmt
);
866 run_end
= ME_FindItemBack(run_end
, diRun
);
868 ME_GetRunCharFormat(editor
, run
, pFmt
);
870 if (run
== run_end
) return;
873 /* FIXME add more style feature comparisons */
874 DWORD dwAttribs
= CFM_SIZE
| CFM_FACE
| CFM_COLOR
| CFM_UNDERLINETYPE
;
875 DWORD dwEffects
= CFM_BOLD
| CFM_ITALIC
| CFM_UNDERLINE
| CFM_STRIKEOUT
| CFM_PROTECTED
| CFM_LINK
| CFM_SUPERSCRIPT
;
877 run
= ME_FindItemFwd(run
, diRun
);
879 ZeroMemory(&tmp
, sizeof(tmp
));
880 tmp
.cbSize
= sizeof(tmp
);
881 ME_GetRunCharFormat(editor
, run
, &tmp
);
883 assert((tmp
.dwMask
& dwAttribs
) == dwAttribs
);
884 /* reset flags that differ */
886 if (pFmt
->yHeight
!= tmp
.yHeight
)
887 pFmt
->dwMask
&= ~CFM_SIZE
;
888 if (pFmt
->dwMask
& CFM_FACE
)
890 if (!(tmp
.dwMask
& CFM_FACE
))
891 pFmt
->dwMask
&= ~CFM_FACE
;
892 else if (wcscmp(pFmt
->szFaceName
, tmp
.szFaceName
) ||
893 pFmt
->bPitchAndFamily
!= tmp
.bPitchAndFamily
)
894 pFmt
->dwMask
&= ~CFM_FACE
;
896 if (pFmt
->yHeight
!= tmp
.yHeight
)
897 pFmt
->dwMask
&= ~CFM_SIZE
;
898 if (pFmt
->bUnderlineType
!= tmp
.bUnderlineType
)
899 pFmt
->dwMask
&= ~CFM_UNDERLINETYPE
;
900 if (pFmt
->dwMask
& CFM_COLOR
)
902 if (!((pFmt
->dwEffects
&CFE_AUTOCOLOR
) & (tmp
.dwEffects
&CFE_AUTOCOLOR
)))
904 if (pFmt
->crTextColor
!= tmp
.crTextColor
)
905 pFmt
->dwMask
&= ~CFM_COLOR
;
909 pFmt
->dwMask
&= ~((pFmt
->dwEffects
^ tmp
.dwEffects
) & dwEffects
);
910 pFmt
->dwEffects
= tmp
.dwEffects
;
912 } while(run
!= run_end
);