imagehlp: Use the IMAGE_FIRST_SECTION helper macro.
[wine.git] / dlls / riched20 / run.c
blob8de2ba345cfda945595ae2cf251db17d7dbfa9f8
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 BOOL cursor_next_run( ME_Cursor *cursor, BOOL all_para )
32 ME_DisplayItem *p = run_get_di( cursor->run )->next;
34 while (p->type != diTextEnd)
36 if (p->type == diParagraph && !all_para) return FALSE;
37 else if (p->type == diRun)
39 cursor->run = &p->member.run;
40 cursor->para = cursor->run->para;
41 cursor->nOffset = 0;
42 return TRUE;
44 p = p->next;
46 return FALSE;
49 BOOL cursor_prev_run( ME_Cursor *cursor, BOOL all_para )
51 ME_DisplayItem *p = run_get_di( cursor->run )->prev;
53 while (p->type != diTextStart)
55 if (p->type == diParagraph && !all_para) return FALSE;
56 else if (p->type == diRun)
58 cursor->run = &p->member.run;
59 cursor->para = cursor->run->para;
60 cursor->nOffset = 0;
61 return TRUE;
63 p = p->prev;
65 return FALSE;
68 ME_Run *run_next( ME_Run *run )
70 ME_Cursor cursor;
72 cursor.run = run;
73 cursor.para = run->para;
74 cursor.nOffset = 0;
76 if (cursor_next_run( &cursor, FALSE ))
77 return cursor.run;
79 return NULL;
82 ME_Run *run_prev( ME_Run *run )
84 ME_Cursor cursor;
86 cursor.run = run;
87 cursor.para = run->para;
88 cursor.nOffset = 0;
90 if (cursor_prev_run( &cursor, FALSE ))
91 return cursor.run;
93 return NULL;
96 ME_Run *run_next_all_paras( ME_Run *run )
98 ME_Cursor cursor;
100 cursor.run = run;
101 cursor.para = run->para;
102 cursor.nOffset = 0;
104 if (cursor_next_run( &cursor, TRUE ))
105 return cursor.run;
107 return NULL;
110 ME_Run *run_prev_all_paras( ME_Run *run )
112 ME_Cursor cursor;
114 cursor.run = run;
115 cursor.para = run->para;
116 cursor.nOffset = 0;
118 if (cursor_prev_run( &cursor, TRUE ))
119 return cursor.run;
121 return NULL;
124 /******************************************************************************
125 * ME_CanJoinRuns
127 * Returns TRUE if two runs can be safely merged into one, FALSE otherwise.
129 BOOL ME_CanJoinRuns(const ME_Run *run1, const ME_Run *run2)
131 if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
132 return FALSE;
133 if (run1->style != run2->style)
134 return FALSE;
135 if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
136 return FALSE;
137 return TRUE;
140 /******************************************************************************
141 * editor_propagate_char_ofs
143 * Shifts (increases or decreases) character offset (relative to beginning of
144 * the document) of the part of the text starting from given place.
145 * Call with only one of para or run non-NULL.
147 void editor_propagate_char_ofs( ME_TextEditor *editor, ME_Paragraph *para, ME_Run *run, int shift )
149 assert( !para ^ !run );
151 if (run)
153 para = para_next( run->para );
156 run->nCharOfs += shift;
157 run = run_next( run );
158 } while (run);
163 /* update position in marked tree, if added */
164 if (para->nFlags & MEPF_REWRAP)
165 para_mark_remove( editor, para );
166 para->nCharOfs += shift;
167 if (para->nFlags & MEPF_REWRAP)
168 para_mark_add( editor, para );
169 para = para_next( para );
170 } while (para);
173 /******************************************************************************
174 * ME_CheckCharOffsets
176 * Checks if editor lists' validity and optionally dumps the document structure
178 void ME_CheckCharOffsets(ME_TextEditor *editor)
180 ME_DisplayItem *p = editor->pBuffer->pFirst;
181 int ofs = 0, ofsp = 0;
183 if (!TRACE_ON(richedit_check))
184 return;
186 TRACE_(richedit_check)("Checking begin\n");
187 if(TRACE_ON(richedit_lists))
189 TRACE_(richedit_lists)("---\n");
190 ME_DumpDocument(editor->pBuffer);
192 do {
193 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
194 switch(p->type) {
195 case diTextEnd:
196 TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
197 assert(ofsp+ofs == p->member.para.nCharOfs);
198 TRACE_(richedit_check)("Checking finished\n");
199 return;
200 case diParagraph:
201 TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
202 assert(ofsp+ofs == p->member.para.nCharOfs);
203 ofsp = p->member.para.nCharOfs;
204 ofs = 0;
205 break;
206 case diRun:
207 TRACE_(richedit_check)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = %s, flags=%08x, fx&mask = %08lx\n",
208 p->member.run.nCharOfs, p->member.run.nCharOfs+ofsp, ofsp+ofs,
209 p->member.run.len, debugstr_run( &p->member.run ),
210 p->member.run.nFlags,
211 p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
212 assert(ofs == p->member.run.nCharOfs);
213 assert(p->member.run.len);
214 ofs += p->member.run.len;
215 break;
216 case diCell:
217 TRACE_(richedit_check)("cell\n");
218 break;
219 default:
220 assert(0);
222 } while(1);
223 TRACE_(richedit_check)("Checking finished\n");
226 /******************************************************************************
227 * run_char_ofs
229 * Converts a character position relative to the start of the run to a
230 * character position relative to the start of the document.
233 int run_char_ofs( ME_Run *run, int ofs )
235 return run->para->nCharOfs + run->nCharOfs + ofs;
238 /******************************************************************************
239 * cursor_from_char_ofs
241 * Converts a character offset (relative to the start of the document) to
242 * a cursor structure (which contains a run and a position relative to that
243 * run).
245 void cursor_from_char_ofs( ME_TextEditor *editor, int char_ofs, ME_Cursor *cursor )
247 ME_Paragraph *para;
248 ME_Run *run;
250 char_ofs = min( max( char_ofs, 0 ), ME_GetTextLength( editor ) );
252 /* Find the paragraph at the offset. */
253 for (para = editor_first_para( editor );
254 para_next( para )->nCharOfs <= char_ofs;
255 para = para_next( para ))
258 char_ofs -= para->nCharOfs;
260 /* Find the run at the offset. */
261 for (run = para_first_run( para );
262 run_next( run ) && run_next( run )->nCharOfs <= char_ofs;
263 run = run_next( run ))
266 char_ofs -= run->nCharOfs;
268 cursor->para = para;
269 cursor->run = run;
270 cursor->nOffset = char_ofs;
273 /******************************************************************************
274 * run_join
276 * Merges two adjacent runs, the one given as a parameter and the next one.
278 void run_join( ME_TextEditor *editor, ME_Run *run )
280 ME_Run *next = run_next( run );
281 int i;
283 assert( run );
284 assert( run->nCharOfs != -1 );
285 para_mark_rewrap( editor, run->para );
287 /* Update all cursors so that they don't contain the soon deleted run */
288 for (i = 0; i < editor->nCursors; i++)
290 if (editor->pCursors[i].run == next)
292 editor->pCursors[i].run = run;
293 editor->pCursors[i].nOffset += run->len;
297 run->len += next->len;
298 ME_Remove( run_get_di( next ) );
299 ME_DestroyDisplayItem( run_get_di( next ) );
300 ME_UpdateRunFlags( editor, run );
301 ME_CheckCharOffsets( editor );
304 /******************************************************************************
305 * run_split
307 * Does the most basic job of splitting a run into two - it does not
308 * update the positions and extents.
310 ME_Run *run_split( ME_TextEditor *editor, ME_Cursor *cursor )
312 ME_Run *run = cursor->run, *new_run;
313 int i;
314 int nOffset = cursor->nOffset;
316 assert( !(run->nFlags & MERF_NONTEXT) );
318 new_run = run_create( run->style, run->nFlags & MERF_SPLITMASK );
319 new_run->nCharOfs = run->nCharOfs + nOffset;
320 new_run->len = run->len - nOffset;
321 new_run->para = run->para;
322 run->len = nOffset;
323 cursor->run = new_run;
324 cursor->nOffset = 0;
326 ME_InsertBefore( run_get_di( run )->next, run_get_di( new_run ) );
328 ME_UpdateRunFlags( editor, run );
329 ME_UpdateRunFlags( editor, new_run );
330 for (i = 0; i < editor->nCursors; i++)
332 if (editor->pCursors[i].run == run &&
333 editor->pCursors[i].nOffset >= nOffset)
335 editor->pCursors[i].run = new_run;
336 editor->pCursors[i].nOffset -= nOffset;
339 para_mark_rewrap( editor, run->para );
340 return run;
343 /******************************************************************************
344 * run_create
346 * A helper function to create run structures quickly.
348 ME_Run *run_create( ME_Style *s, int flags )
350 ME_DisplayItem *item = ME_MakeDI( diRun );
351 ME_Run *run = &item->member.run;
353 if (!item) return NULL;
355 ME_AddRefStyle( s );
356 run->style = s;
357 run->reobj = NULL;
358 run->nFlags = flags;
359 run->nCharOfs = -1;
360 run->len = 0;
361 run->para = NULL;
362 run->num_glyphs = 0;
363 run->max_glyphs = 0;
364 run->glyphs = NULL;
365 run->vis_attrs = NULL;
366 run->advances = NULL;
367 run->offsets = NULL;
368 run->max_clusters = 0;
369 run->clusters = NULL;
370 return run;
373 /******************************************************************************
374 * run_insert
376 * Inserts a new run with given style, flags and content at a given position,
377 * which is passed as a cursor structure (which consists of a run and
378 * a run-relative character offset).
380 ME_Run *run_insert( ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
381 const WCHAR *str, int len, int flags )
383 ME_Run *insert_before = cursor->run, *run, *prev;
385 if (cursor->nOffset)
387 if (cursor->nOffset == insert_before->len)
389 insert_before = run_next_all_paras( insert_before );
390 if (!insert_before) insert_before = cursor->run; /* Always insert before the final eop run */
392 else
394 run_split( editor, cursor );
395 insert_before = cursor->run;
399 add_undo_delete_run( editor, insert_before->para->nCharOfs + insert_before->nCharOfs, len );
401 run = run_create( style, flags );
402 run->nCharOfs = insert_before->nCharOfs;
403 run->len = len;
404 run->para = insert_before->para;
405 ME_InsertString( run->para->text, run->nCharOfs, str, len );
406 ME_InsertBefore( run_get_di( insert_before ), run_get_di( run ) );
407 TRACE("Shift length:%d\n", len);
408 editor_propagate_char_ofs( editor, NULL, insert_before, len );
409 para_mark_rewrap( editor, insert_before->para );
411 /* Move any cursors that were at the end of the previous run to the end of the inserted run */
412 prev = run_prev_all_paras( run );
413 if (prev)
415 int i;
417 for (i = 0; i < editor->nCursors; i++)
419 if (editor->pCursors[i].run == prev &&
420 editor->pCursors[i].nOffset == prev->len)
422 editor->pCursors[i].run = run;
423 editor->pCursors[i].nOffset = len;
428 return run;
431 static BOOL run_is_splittable( const ME_Run *run )
433 WCHAR *str = get_text( run, 0 ), *p;
434 int i;
435 BOOL found_ink = FALSE;
437 for (i = 0, p = str; i < run->len; i++, p++)
439 if (ME_IsWSpace( *p ))
441 if (found_ink) return TRUE;
443 else
444 found_ink = TRUE;
446 return FALSE;
449 static BOOL run_is_entirely_ws( const ME_Run *run )
451 WCHAR *str = get_text( run, 0 ), *p;
452 int i;
454 for (i = 0, p = str; i < run->len; i++, p++)
455 if (!ME_IsWSpace( *p )) return FALSE;
457 return TRUE;
460 /******************************************************************************
461 * ME_UpdateRunFlags
463 * Determine some of run attributes given its content (style, text content).
464 * Some flags cannot be determined by this function (MERF_GRAPHICS,
465 * MERF_ENDPARA)
467 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
469 assert(run->nCharOfs >= 0);
471 if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
472 run->nFlags |= MERF_HIDDEN;
473 else
474 run->nFlags &= ~MERF_HIDDEN;
476 if (run_is_splittable( run ))
477 run->nFlags |= MERF_SPLITTABLE;
478 else
479 run->nFlags &= ~MERF_SPLITTABLE;
481 if (!(run->nFlags & MERF_NOTEXT))
483 if (run_is_entirely_ws( run ))
484 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
485 else
487 run->nFlags &= ~MERF_WHITESPACE;
489 if (ME_IsWSpace( *get_text( run, 0 ) ))
490 run->nFlags |= MERF_STARTWHITE;
491 else
492 run->nFlags &= ~MERF_STARTWHITE;
494 if (ME_IsWSpace( *get_text( run, run->len - 1 ) ))
495 run->nFlags |= MERF_ENDWHITE;
496 else
497 run->nFlags &= ~MERF_ENDWHITE;
500 else
501 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
504 /******************************************************************************
505 * ME_CharFromPointContext
507 * Returns a character position inside the run given a run-relative
508 * pixel horizontal position.
510 * If closest is FALSE return the actual character
511 * If closest is TRUE will round to the closest leading edge.
512 * ie. if the second character is at pixel position 8 and third at 16 then for:
513 * closest = FALSE cx = 0..7 return 0, cx = 8..15 return 1
514 * closest = TRUE cx = 0..3 return 0, cx = 4..11 return 1.
516 int ME_CharFromPointContext(ME_Context *c, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
518 ME_String *mask_text = NULL;
519 WCHAR *str;
520 int fit = 0;
521 SIZE sz, sz2, sz3;
522 if (!run->len || cx <= 0)
523 return 0;
525 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
527 if (!closest || cx < run->nWidth / 2) return 0;
528 return 1;
531 if (run->nFlags & MERF_GRAPHICS)
533 SIZE sz;
534 ME_GetOLEObjectSize(c, run, &sz);
535 if (!closest || cx < sz.cx / 2) return 0;
536 return 1;
539 if (run->para->nFlags & MEPF_COMPLEX)
541 int cp, trailing;
542 if (visual_order && run->script_analysis.fRTL) cx = run->nWidth - cx - 1;
544 ScriptXtoCP( cx, run->len, run->num_glyphs, run->clusters, run->vis_attrs, run->advances, &run->script_analysis,
545 &cp, &trailing );
546 TRACE("x %d cp %d trailing %d (run width %d) rtl %d log order %d\n", cx, cp, trailing, run->nWidth,
547 run->script_analysis.fRTL, run->script_analysis.fLogicalOrder);
548 return closest ? cp + trailing : cp;
551 if (c->editor->password_char)
553 mask_text = ME_MakeStringR( c->editor->password_char, run->len );
554 str = mask_text->szData;
556 else
557 str = get_text( run, 0 );
559 select_style(c, run->style);
560 GetTextExtentExPointW(c->hDC, str, run->len,
561 cx, &fit, NULL, &sz);
562 if (closest && fit != run->len)
564 GetTextExtentPoint32W(c->hDC, str, fit, &sz2);
565 GetTextExtentPoint32W(c->hDC, str, fit + 1, &sz3);
566 if (cx >= (sz2.cx+sz3.cx)/2)
567 fit = fit + 1;
570 ME_DestroyString( mask_text );
572 return fit;
575 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
577 ME_Context c;
578 int ret;
579 HDC hdc = ITextHost_TxGetDC( editor->texthost );
581 ME_InitContext( &c, editor, hdc );
582 ret = ME_CharFromPointContext( &c, cx, run, closest, visual_order );
583 ME_DestroyContext(&c);
584 ITextHost_TxReleaseDC( editor->texthost, hdc );
585 return ret;
588 /******************************************************************************
589 * ME_GetTextExtent
591 * Finds a width and a height of the text using a specified style
593 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
595 if (c->hDC)
597 select_style( c, s );
598 GetTextExtentPoint32W( c->hDC, szText, nChars, size );
600 else
602 size->cx = 0;
603 size->cy = 0;
607 /******************************************************************************
608 * ME_PointFromCharContext
610 * Returns a run-relative pixel position given a run-relative character
611 * position (character offset)
613 int ME_PointFromCharContext(ME_Context *c, ME_Run *pRun, int nOffset, BOOL visual_order)
615 SIZE size;
616 ME_String *mask_text = NULL;
617 WCHAR *str;
619 if (pRun->nFlags & MERF_GRAPHICS)
621 if (nOffset)
622 ME_GetOLEObjectSize(c, pRun, &size);
623 return nOffset != 0;
624 } else if (pRun->nFlags & MERF_ENDPARA) {
625 nOffset = 0;
628 if (pRun->para->nFlags & MEPF_COMPLEX)
630 int x;
631 ScriptCPtoX( nOffset, FALSE, pRun->len, pRun->num_glyphs, pRun->clusters,
632 pRun->vis_attrs, pRun->advances, &pRun->script_analysis, &x );
633 if (visual_order && pRun->script_analysis.fRTL) x = pRun->nWidth - x - 1;
634 return x;
636 if (c->editor->password_char)
638 mask_text = ME_MakeStringR( c->editor->password_char, pRun->len );
639 str = mask_text->szData;
641 else
642 str = get_text( pRun, 0 );
644 ME_GetTextExtent(c, str, nOffset, pRun->style, &size);
645 ME_DestroyString( mask_text );
646 return size.cx;
649 /******************************************************************************
650 * ME_PointFromChar
652 * Calls ME_PointFromCharContext after first creating a context.
654 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset, BOOL visual_order)
656 ME_Context c;
657 int ret;
658 HDC hdc = ITextHost_TxGetDC( editor->texthost );
660 ME_InitContext( &c, editor, hdc );
661 ret = ME_PointFromCharContext( &c, pRun, nOffset, visual_order );
662 ME_DestroyContext(&c);
663 ITextHost_TxReleaseDC( editor->texthost, hdc );
665 return ret;
668 /******************************************************************************
669 * ME_GetRunSizeCommon
671 * Finds width, height, ascent and descent of a run, up to given character
672 * (nLen).
674 SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
675 int startx, int *pAscent, int *pDescent)
677 SIZE size;
679 nLen = min( nLen, run->len );
681 if (run->nFlags & MERF_ENDPARA)
683 nLen = min( nLen, 1 );
684 ME_GetTextExtent( c, L" ", nLen, run->style, &size );
686 else if (para->nFlags & MEPF_COMPLEX)
688 size.cx = run->nWidth;
690 else if (c->editor->password_char)
692 ME_String *szMasked = ME_MakeStringR( c->editor->password_char, nLen );
693 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
694 ME_DestroyString(szMasked);
696 else
698 ME_GetTextExtent(c, get_text( run, 0 ), nLen, run->style, &size);
700 *pAscent = run->style->tm.tmAscent;
701 *pDescent = run->style->tm.tmDescent;
702 size.cy = *pAscent + *pDescent;
704 if (run->nFlags & MERF_TAB)
706 int pos = 0, i = 0, ppos, shift = 0;
707 const PARAFORMAT2 *pFmt = &para->fmt;
709 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
710 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
711 /* The horizontal gap shifts the tab positions to leave the gap. */
712 shift = pFmt->dxOffset * 2;
713 do {
714 if (i < pFmt->cTabCount)
716 /* Only one side of the horizontal gap is needed at the end of
717 * the table row. */
718 if (i == pFmt->cTabCount -1)
719 shift = shift >> 1;
720 pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
721 i++;
723 else
725 pos += lDefaultTab - (pos % lDefaultTab);
727 ppos = ME_twips2pointsX(c, pos);
728 if (ppos > startx + run->pt.x) {
729 size.cx = ppos - startx - run->pt.x;
730 break;
732 } while(1);
733 size.cy = *pAscent + *pDescent;
734 return size;
736 if (run->nFlags & MERF_GRAPHICS)
738 ME_GetOLEObjectSize(c, run, &size);
739 if (size.cy > *pAscent)
740 *pAscent = size.cy;
741 /* descent is unchanged */
742 return size;
744 return size;
747 /******************************************************************************
748 * ME_SetSelectionCharFormat
750 * Applies a style change, either to a current selection, or to insert cursor
751 * (ie. the style next typed characters will use).
753 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
755 if (!ME_IsSelection(editor))
757 ME_Style *s;
758 if (!editor->pBuffer->pCharStyle)
759 editor->pBuffer->pCharStyle = style_get_insert_style( editor, editor->pCursors );
760 s = ME_ApplyStyle(editor, editor->pBuffer->pCharStyle, pFmt);
761 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
762 editor->pBuffer->pCharStyle = s;
763 } else {
764 ME_Cursor *from, *to;
765 ME_GetSelection(editor, &from, &to);
766 ME_SetCharFormat(editor, from, to, pFmt);
770 /******************************************************************************
771 * ME_SetCharFormat
773 * Applies a style change to the specified part of the text
775 * The start and end cursors specify the part of the text. These cursors will
776 * be updated to stay valid, but this function may invalidate other
777 * non-selection cursors. The end cursor may be NULL to specify all the text
778 * following the start cursor.
780 * If no text is selected, then nothing is done.
782 void ME_SetCharFormat( ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *fmt )
784 ME_Run *run, *start_run = start->run, *end_run = NULL;
786 if (end && start->run == end->run && start->nOffset == end->nOffset)
787 return;
789 if (start->nOffset == start->run->len)
790 start_run = run_next_all_paras( start->run );
791 else if (start->nOffset)
793 /* run_split() may or may not update the cursors, depending on whether they
794 * are selection cursors, but we need to make sure they are valid. */
795 int split_offset = start->nOffset;
796 ME_Run *split_run = run_split( editor, start );
797 start_run = start->run;
798 if (end && end->run == split_run)
800 end->run = start->run;
801 end->nOffset -= split_offset;
805 if (end)
807 if (end->nOffset == end->run->len)
808 end_run = run_next_all_paras( end->run );
809 else
811 if (end->nOffset) run_split( editor, end );
812 end_run = end->run;
816 for (run = start_run; run != end_run; run = run_next_all_paras( run ))
818 ME_Style *new_style = ME_ApplyStyle( editor, run->style, fmt );
819 ME_Paragraph *para = run->para;
821 add_undo_set_char_fmt( editor, para->nCharOfs + run->nCharOfs,
822 run->len, &run->style->fmt );
823 ME_ReleaseStyle( run->style );
824 run->style = new_style;
826 /* The para numbering style depends on the eop style */
827 if ((run->nFlags & MERF_ENDPARA) && para->para_num.style)
829 ME_ReleaseStyle(para->para_num.style);
830 para->para_num.style = NULL;
832 para_mark_rewrap( editor, para );
836 static void run_copy_char_fmt( ME_Run *run, CHARFORMAT2W *fmt )
838 ME_CopyCharFormat( fmt, &run->style->fmt );
841 /******************************************************************************
842 * ME_GetDefaultCharFormat
844 * Retrieves the current default character style (the one applied where no
845 * other style was applied) .
847 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
849 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
852 /******************************************************************************
853 * ME_GetSelectionCharFormat
855 * If selection exists, it returns all style elements that are set consistently
856 * in the whole selection. If not, it just returns the current style.
858 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
860 ME_Cursor *from, *to;
861 if (!ME_IsSelection(editor) && editor->pBuffer->pCharStyle)
863 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
864 return;
866 ME_GetSelection(editor, &from, &to);
867 ME_GetCharFormat(editor, from, to, pFmt);
870 /******************************************************************************
871 * ME_GetCharFormat
873 * Returns the style consisting of those attributes which are consistently set
874 * in the whole character range.
876 void ME_GetCharFormat( ME_TextEditor *editor, const ME_Cursor *from,
877 const ME_Cursor *to, CHARFORMAT2W *fmt )
879 ME_Run *run, *run_end, *prev_run;
880 CHARFORMAT2W tmp;
882 run = from->run;
883 /* special case - if selection is empty, take previous char's formatting */
884 if (from->run == to->run && from->nOffset == to->nOffset)
886 if (!from->nOffset && (prev_run = run_prev( run ))) run = prev_run;
887 run_copy_char_fmt( run, fmt );
888 return;
891 run_end = to->run;
892 if (!to->nOffset) run_end = run_prev_all_paras( run_end );
894 run_copy_char_fmt( run, fmt );
896 if (run == run_end) return;
898 do {
899 /* FIXME add more style feature comparisons */
900 DWORD dwAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
901 DWORD dwEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
903 run = run_next_all_paras( run );
905 memset( &tmp, 0, sizeof(tmp) );
906 tmp.cbSize = sizeof(tmp);
907 run_copy_char_fmt( run, &tmp );
909 assert((tmp.dwMask & dwAttribs) == dwAttribs);
911 /* reset flags that differ */
912 if (fmt->dwMask & CFM_FACE)
914 if (!(tmp.dwMask & CFM_FACE))
915 fmt->dwMask &= ~CFM_FACE;
916 else if (wcscmp( fmt->szFaceName, tmp.szFaceName ) ||
917 fmt->bPitchAndFamily != tmp.bPitchAndFamily)
918 fmt->dwMask &= ~CFM_FACE;
920 if (fmt->yHeight != tmp.yHeight) fmt->dwMask &= ~CFM_SIZE;
921 if (fmt->bUnderlineType != tmp.bUnderlineType) fmt->dwMask &= ~CFM_UNDERLINETYPE;
922 if (fmt->dwMask & CFM_COLOR)
924 if (!((fmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
926 if (fmt->crTextColor != tmp.crTextColor)
927 fmt->dwMask &= ~CFM_COLOR;
931 fmt->dwMask &= ~((fmt->dwEffects ^ tmp.dwEffects) & dwEffects);
932 fmt->dwEffects = tmp.dwEffects;
934 } while(run != run_end);