widl: Generate helper macros for WinRT implementation.
[wine/zf.git] / dlls / riched20 / run.c
blobe099c0be6dc082fa930041526dcbd449b8ac5307
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_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 para->nCharOfs += shift;
164 para = para_next( para );
165 } while (para);
168 /******************************************************************************
169 * ME_CheckCharOffsets
171 * Checks if editor lists' validity and optionally dumps the document structure
173 void ME_CheckCharOffsets(ME_TextEditor *editor)
175 ME_DisplayItem *p = editor->pBuffer->pFirst;
176 int ofs = 0, ofsp = 0;
178 if (!TRACE_ON(richedit_check))
179 return;
181 TRACE_(richedit_check)("Checking begin\n");
182 if(TRACE_ON(richedit_lists))
184 TRACE_(richedit_lists)("---\n");
185 ME_DumpDocument(editor->pBuffer);
187 do {
188 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
189 switch(p->type) {
190 case diTextEnd:
191 TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
192 assert(ofsp+ofs == p->member.para.nCharOfs);
193 TRACE_(richedit_check)("Checking finished\n");
194 return;
195 case diParagraph:
196 TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
197 assert(ofsp+ofs == p->member.para.nCharOfs);
198 ofsp = p->member.para.nCharOfs;
199 ofs = 0;
200 break;
201 case diRun:
202 TRACE_(richedit_check)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = %s, flags=%08x, fx&mask = %08x\n",
203 p->member.run.nCharOfs, p->member.run.nCharOfs+ofsp, ofsp+ofs,
204 p->member.run.len, debugstr_run( &p->member.run ),
205 p->member.run.nFlags,
206 p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
207 assert(ofs == p->member.run.nCharOfs);
208 assert(p->member.run.len);
209 ofs += p->member.run.len;
210 break;
211 case diCell:
212 TRACE_(richedit_check)("cell\n");
213 break;
214 default:
215 assert(0);
217 } while(1);
218 TRACE_(richedit_check)("Checking finished\n");
221 /******************************************************************************
222 * run_char_ofs
224 * Converts a character position relative to the start of the run to a
225 * character position relative to the start of the document.
228 int run_char_ofs( ME_Run *run, int ofs )
230 return run->para->nCharOfs + run->nCharOfs + ofs;
233 /******************************************************************************
234 * cursor_from_char_ofs
236 * Converts a character offset (relative to the start of the document) to
237 * a cursor structure (which contains a run and a position relative to that
238 * run).
240 void cursor_from_char_ofs( ME_TextEditor *editor, int char_ofs, ME_Cursor *cursor )
242 ME_Paragraph *para;
243 ME_Run *run;
245 char_ofs = min( max( char_ofs, 0 ), ME_GetTextLength( editor ) );
247 /* Find the paragraph at the offset. */
248 for (para = editor_first_para( editor );
249 para_next( para )->nCharOfs <= char_ofs;
250 para = para_next( para ))
253 char_ofs -= para->nCharOfs;
255 /* Find the run at the offset. */
256 for (run = para_first_run( para );
257 run_next( run ) && run_next( run )->nCharOfs <= char_ofs;
258 run = run_next( run ))
261 char_ofs -= run->nCharOfs;
263 cursor->para = para;
264 cursor->run = run;
265 cursor->nOffset = char_ofs;
268 /******************************************************************************
269 * run_join
271 * Merges two adjacent runs, the one given as a parameter and the next one.
273 void run_join( ME_TextEditor *editor, ME_Run *run )
275 ME_Run *next = run_next( run );
276 int i;
278 assert( run );
279 assert( run->nCharOfs != -1 );
280 para_mark_rewrap( editor, run->para );
282 /* Update all cursors so that they don't contain the soon deleted run */
283 for (i = 0; i < editor->nCursors; i++)
285 if (editor->pCursors[i].run == next)
287 editor->pCursors[i].run = run;
288 editor->pCursors[i].nOffset += run->len;
292 run->len += next->len;
293 ME_Remove( run_get_di( next ) );
294 ME_DestroyDisplayItem( run_get_di( next ) );
295 ME_UpdateRunFlags( editor, run );
296 ME_CheckCharOffsets( editor );
299 /******************************************************************************
300 * run_split
302 * Does the most basic job of splitting a run into two - it does not
303 * update the positions and extents.
305 ME_Run *run_split( ME_TextEditor *editor, ME_Cursor *cursor )
307 ME_Run *run = cursor->run, *new_run;
308 int i;
309 int nOffset = cursor->nOffset;
311 assert( !(run->nFlags & MERF_NONTEXT) );
313 new_run = run_create( run->style, run->nFlags & MERF_SPLITMASK );
314 new_run->nCharOfs = run->nCharOfs + nOffset;
315 new_run->len = run->len - nOffset;
316 new_run->para = run->para;
317 run->len = nOffset;
318 cursor->run = new_run;
319 cursor->nOffset = 0;
321 ME_InsertBefore( run_get_di( run )->next, run_get_di( new_run ) );
323 ME_UpdateRunFlags( editor, run );
324 ME_UpdateRunFlags( editor, new_run );
325 for (i = 0; i < editor->nCursors; i++)
327 if (editor->pCursors[i].run == run &&
328 editor->pCursors[i].nOffset >= nOffset)
330 editor->pCursors[i].run = new_run;
331 editor->pCursors[i].nOffset -= nOffset;
334 para_mark_rewrap( editor, run->para );
335 return run;
338 /******************************************************************************
339 * run_create
341 * A helper function to create run structures quickly.
343 ME_Run *run_create( ME_Style *s, int flags )
345 ME_DisplayItem *item = ME_MakeDI( diRun );
346 ME_Run *run = &item->member.run;
348 if (!item) return NULL;
350 ME_AddRefStyle( s );
351 run->style = s;
352 run->reobj = NULL;
353 run->nFlags = flags;
354 run->nCharOfs = -1;
355 run->len = 0;
356 run->para = NULL;
357 run->num_glyphs = 0;
358 run->max_glyphs = 0;
359 run->glyphs = NULL;
360 run->vis_attrs = NULL;
361 run->advances = NULL;
362 run->offsets = NULL;
363 run->max_clusters = 0;
364 run->clusters = NULL;
365 return run;
368 /******************************************************************************
369 * run_insert
371 * Inserts a new run with given style, flags and content at a given position,
372 * which is passed as a cursor structure (which consists of a run and
373 * a run-relative character offset).
375 ME_Run *run_insert( ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
376 const WCHAR *str, int len, int flags )
378 ME_Run *insert_before = cursor->run, *run, *prev;
380 if (cursor->nOffset)
382 if (cursor->nOffset == insert_before->len)
384 insert_before = run_next_all_paras( insert_before );
385 if (!insert_before) insert_before = cursor->run; /* Always insert before the final eop run */
387 else
389 run_split( editor, cursor );
390 insert_before = cursor->run;
394 add_undo_delete_run( editor, insert_before->para->nCharOfs + insert_before->nCharOfs, len );
396 run = run_create( style, flags );
397 run->nCharOfs = insert_before->nCharOfs;
398 run->len = len;
399 run->para = insert_before->para;
400 ME_InsertString( run->para->text, run->nCharOfs, str, len );
401 ME_InsertBefore( run_get_di( insert_before ), run_get_di( run ) );
402 TRACE("Shift length:%d\n", len);
403 editor_propagate_char_ofs( NULL, insert_before, len );
404 para_mark_rewrap( editor, insert_before->para );
406 /* Move any cursors that were at the end of the previous run to the end of the inserted run */
407 prev = run_prev_all_paras( run );
408 if (prev)
410 int i;
412 for (i = 0; i < editor->nCursors; i++)
414 if (editor->pCursors[i].run == prev &&
415 editor->pCursors[i].nOffset == prev->len)
417 editor->pCursors[i].run = run;
418 editor->pCursors[i].nOffset = len;
423 return run;
426 static BOOL run_is_splittable( const ME_Run *run )
428 WCHAR *str = get_text( run, 0 ), *p;
429 int i;
430 BOOL found_ink = FALSE;
432 for (i = 0, p = str; i < run->len; i++, p++)
434 if (ME_IsWSpace( *p ))
436 if (found_ink) return TRUE;
438 else
439 found_ink = TRUE;
441 return FALSE;
444 static BOOL run_is_entirely_ws( const ME_Run *run )
446 WCHAR *str = get_text( run, 0 ), *p;
447 int i;
449 for (i = 0, p = str; i < run->len; i++, p++)
450 if (!ME_IsWSpace( *p )) return FALSE;
452 return TRUE;
455 /******************************************************************************
456 * ME_UpdateRunFlags
458 * Determine some of run attributes given its content (style, text content).
459 * Some flags cannot be determined by this function (MERF_GRAPHICS,
460 * MERF_ENDPARA)
462 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
464 assert(run->nCharOfs >= 0);
466 if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
467 run->nFlags |= MERF_HIDDEN;
468 else
469 run->nFlags &= ~MERF_HIDDEN;
471 if (run_is_splittable( run ))
472 run->nFlags |= MERF_SPLITTABLE;
473 else
474 run->nFlags &= ~MERF_SPLITTABLE;
476 if (!(run->nFlags & MERF_NOTEXT))
478 if (run_is_entirely_ws( run ))
479 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
480 else
482 run->nFlags &= ~MERF_WHITESPACE;
484 if (ME_IsWSpace( *get_text( run, 0 ) ))
485 run->nFlags |= MERF_STARTWHITE;
486 else
487 run->nFlags &= ~MERF_STARTWHITE;
489 if (ME_IsWSpace( *get_text( run, run->len - 1 ) ))
490 run->nFlags |= MERF_ENDWHITE;
491 else
492 run->nFlags &= ~MERF_ENDWHITE;
495 else
496 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
499 /******************************************************************************
500 * ME_CharFromPointContext
502 * Returns a character position inside the run given a run-relative
503 * pixel horizontal position.
505 * If closest is FALSE return the actual character
506 * If closest is TRUE will round to the closest leading edge.
507 * ie. if the second character is at pixel position 8 and third at 16 then for:
508 * closest = FALSE cx = 0..7 return 0, cx = 8..15 return 1
509 * closest = TRUE cx = 0..3 return 0, cx = 4..11 return 1.
511 int ME_CharFromPointContext(ME_Context *c, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
513 ME_String *mask_text = NULL;
514 WCHAR *str;
515 int fit = 0;
516 SIZE sz, sz2, sz3;
517 if (!run->len || cx <= 0)
518 return 0;
520 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
522 if (!closest || cx < run->nWidth / 2) return 0;
523 return 1;
526 if (run->nFlags & MERF_GRAPHICS)
528 SIZE sz;
529 ME_GetOLEObjectSize(c, run, &sz);
530 if (!closest || cx < sz.cx / 2) return 0;
531 return 1;
534 if (run->para->nFlags & MEPF_COMPLEX)
536 int cp, trailing;
537 if (visual_order && run->script_analysis.fRTL) cx = run->nWidth - cx - 1;
539 ScriptXtoCP( cx, run->len, run->num_glyphs, run->clusters, run->vis_attrs, run->advances, &run->script_analysis,
540 &cp, &trailing );
541 TRACE("x %d cp %d trailing %d (run width %d) rtl %d log order %d\n", cx, cp, trailing, run->nWidth,
542 run->script_analysis.fRTL, run->script_analysis.fLogicalOrder);
543 return closest ? cp + trailing : cp;
546 if (c->editor->cPasswordMask)
548 mask_text = ME_MakeStringR( c->editor->cPasswordMask, run->len );
549 str = mask_text->szData;
551 else
552 str = get_text( run, 0 );
554 select_style(c, run->style);
555 GetTextExtentExPointW(c->hDC, str, run->len,
556 cx, &fit, NULL, &sz);
557 if (closest && fit != run->len)
559 GetTextExtentPoint32W(c->hDC, str, fit, &sz2);
560 GetTextExtentPoint32W(c->hDC, str, fit + 1, &sz3);
561 if (cx >= (sz2.cx+sz3.cx)/2)
562 fit = fit + 1;
565 ME_DestroyString( mask_text );
567 return fit;
570 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
572 ME_Context c;
573 int ret;
575 ME_InitContext( &c, editor, ITextHost_TxGetDC( editor->texthost ) );
576 ret = ME_CharFromPointContext( &c, cx, run, closest, visual_order );
577 ME_DestroyContext(&c);
578 return ret;
581 /******************************************************************************
582 * ME_GetTextExtent
584 * Finds a width and a height of the text using a specified style
586 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
588 if (c->hDC)
590 select_style( c, s );
591 GetTextExtentPoint32W( c->hDC, szText, nChars, size );
593 else
595 size->cx = 0;
596 size->cy = 0;
600 /******************************************************************************
601 * ME_PointFromCharContext
603 * Returns a run-relative pixel position given a run-relative character
604 * position (character offset)
606 int ME_PointFromCharContext(ME_Context *c, ME_Run *pRun, int nOffset, BOOL visual_order)
608 SIZE size;
609 ME_String *mask_text = NULL;
610 WCHAR *str;
612 if (pRun->nFlags & MERF_GRAPHICS)
614 if (nOffset)
615 ME_GetOLEObjectSize(c, pRun, &size);
616 return nOffset != 0;
617 } else if (pRun->nFlags & MERF_ENDPARA) {
618 nOffset = 0;
621 if (pRun->para->nFlags & MEPF_COMPLEX)
623 int x;
624 ScriptCPtoX( nOffset, FALSE, pRun->len, pRun->num_glyphs, pRun->clusters,
625 pRun->vis_attrs, pRun->advances, &pRun->script_analysis, &x );
626 if (visual_order && pRun->script_analysis.fRTL) x = pRun->nWidth - x - 1;
627 return x;
629 if (c->editor->cPasswordMask)
631 mask_text = ME_MakeStringR(c->editor->cPasswordMask, pRun->len);
632 str = mask_text->szData;
634 else
635 str = get_text( pRun, 0 );
637 ME_GetTextExtent(c, str, nOffset, pRun->style, &size);
638 ME_DestroyString( mask_text );
639 return size.cx;
642 /******************************************************************************
643 * ME_PointFromChar
645 * Calls ME_PointFromCharContext after first creating a context.
647 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset, BOOL visual_order)
649 ME_Context c;
650 int ret;
652 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
653 ret = ME_PointFromCharContext( &c, pRun, nOffset, visual_order );
654 ME_DestroyContext(&c);
656 return ret;
659 /******************************************************************************
660 * ME_GetRunSizeCommon
662 * Finds width, height, ascent and descent of a run, up to given character
663 * (nLen).
665 SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
666 int startx, int *pAscent, int *pDescent)
668 SIZE size;
670 nLen = min( nLen, run->len );
672 if (run->nFlags & MERF_ENDPARA)
674 nLen = min( nLen, 1 );
675 ME_GetTextExtent( c, L" ", nLen, run->style, &size );
677 else if (para->nFlags & MEPF_COMPLEX)
679 size.cx = run->nWidth;
681 else if (c->editor->cPasswordMask)
683 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
684 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
685 ME_DestroyString(szMasked);
687 else
689 ME_GetTextExtent(c, get_text( run, 0 ), nLen, run->style, &size);
691 *pAscent = run->style->tm.tmAscent;
692 *pDescent = run->style->tm.tmDescent;
693 size.cy = *pAscent + *pDescent;
695 if (run->nFlags & MERF_TAB)
697 int pos = 0, i = 0, ppos, shift = 0;
698 const PARAFORMAT2 *pFmt = &para->fmt;
700 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
701 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
702 /* The horizontal gap shifts the tab positions to leave the gap. */
703 shift = pFmt->dxOffset * 2;
704 do {
705 if (i < pFmt->cTabCount)
707 /* Only one side of the horizontal gap is needed at the end of
708 * the table row. */
709 if (i == pFmt->cTabCount -1)
710 shift = shift >> 1;
711 pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
712 i++;
714 else
716 pos += lDefaultTab - (pos % lDefaultTab);
718 ppos = ME_twips2pointsX(c, pos);
719 if (ppos > startx + run->pt.x) {
720 size.cx = ppos - startx - run->pt.x;
721 break;
723 } while(1);
724 size.cy = *pAscent + *pDescent;
725 return size;
727 if (run->nFlags & MERF_GRAPHICS)
729 ME_GetOLEObjectSize(c, run, &size);
730 if (size.cy > *pAscent)
731 *pAscent = size.cy;
732 /* descent is unchanged */
733 return size;
735 return size;
738 /******************************************************************************
739 * ME_SetSelectionCharFormat
741 * Applies a style change, either to a current selection, or to insert cursor
742 * (ie. the style next typed characters will use).
744 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
746 if (!ME_IsSelection(editor))
748 ME_Style *s;
749 if (!editor->pBuffer->pCharStyle)
750 editor->pBuffer->pCharStyle = style_get_insert_style( editor, editor->pCursors );
751 s = ME_ApplyStyle(editor, editor->pBuffer->pCharStyle, pFmt);
752 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
753 editor->pBuffer->pCharStyle = s;
754 } else {
755 ME_Cursor *from, *to;
756 ME_GetSelection(editor, &from, &to);
757 ME_SetCharFormat(editor, from, to, pFmt);
761 /******************************************************************************
762 * ME_SetCharFormat
764 * Applies a style change to the specified part of the text
766 * The start and end cursors specify the part of the text. These cursors will
767 * be updated to stay valid, but this function may invalidate other
768 * non-selection cursors. The end cursor may be NULL to specify all the text
769 * following the start cursor.
771 * If no text is selected, then nothing is done.
773 void ME_SetCharFormat( ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *fmt )
775 ME_Run *run, *start_run = start->run, *end_run = NULL;
777 if (end && start->run == end->run && start->nOffset == end->nOffset)
778 return;
780 if (start->nOffset == start->run->len)
781 start_run = run_next_all_paras( start->run );
782 else if (start->nOffset)
784 /* run_split() may or may not update the cursors, depending on whether they
785 * are selection cursors, but we need to make sure they are valid. */
786 int split_offset = start->nOffset;
787 ME_Run *split_run = run_split( editor, start );
788 start_run = start->run;
789 if (end && end->run == split_run)
791 end->run = start->run;
792 end->nOffset -= split_offset;
796 if (end)
798 if (end->nOffset == end->run->len)
799 end_run = run_next_all_paras( end->run );
800 else
802 if (end->nOffset) run_split( editor, end );
803 end_run = end->run;
807 for (run = start_run; run != end_run; run = run_next_all_paras( run ))
809 ME_Style *new_style = ME_ApplyStyle( editor, run->style, fmt );
810 ME_Paragraph *para = run->para;
812 add_undo_set_char_fmt( editor, para->nCharOfs + run->nCharOfs,
813 run->len, &run->style->fmt );
814 ME_ReleaseStyle( run->style );
815 run->style = new_style;
817 /* The para numbering style depends on the eop style */
818 if ((run->nFlags & MERF_ENDPARA) && para->para_num.style)
820 ME_ReleaseStyle(para->para_num.style);
821 para->para_num.style = NULL;
823 para_mark_rewrap( editor, para );
827 static void run_copy_char_fmt( ME_Run *run, CHARFORMAT2W *fmt )
829 ME_CopyCharFormat( fmt, &run->style->fmt );
832 /******************************************************************************
833 * ME_GetDefaultCharFormat
835 * Retrieves the current default character style (the one applied where no
836 * other style was applied) .
838 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
840 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
843 /******************************************************************************
844 * ME_GetSelectionCharFormat
846 * If selection exists, it returns all style elements that are set consistently
847 * in the whole selection. If not, it just returns the current style.
849 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
851 ME_Cursor *from, *to;
852 if (!ME_IsSelection(editor) && editor->pBuffer->pCharStyle)
854 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
855 return;
857 ME_GetSelection(editor, &from, &to);
858 ME_GetCharFormat(editor, from, to, pFmt);
861 /******************************************************************************
862 * ME_GetCharFormat
864 * Returns the style consisting of those attributes which are consistently set
865 * in the whole character range.
867 void ME_GetCharFormat( ME_TextEditor *editor, const ME_Cursor *from,
868 const ME_Cursor *to, CHARFORMAT2W *fmt )
870 ME_Run *run, *run_end, *prev_run;
871 CHARFORMAT2W tmp;
873 run = from->run;
874 /* special case - if selection is empty, take previous char's formatting */
875 if (from->run == to->run && from->nOffset == to->nOffset)
877 if (!from->nOffset && (prev_run = run_prev( run ))) run = prev_run;
878 run_copy_char_fmt( run, fmt );
879 return;
882 run_end = to->run;
883 if (!to->nOffset) run_end = run_prev_all_paras( run_end );
885 run_copy_char_fmt( run, fmt );
887 if (run == run_end) return;
889 do {
890 /* FIXME add more style feature comparisons */
891 DWORD dwAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
892 DWORD dwEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
894 run = run_next_all_paras( run );
896 memset( &tmp, 0, sizeof(tmp) );
897 tmp.cbSize = sizeof(tmp);
898 run_copy_char_fmt( run, &tmp );
900 assert((tmp.dwMask & dwAttribs) == dwAttribs);
901 /* reset flags that differ */
903 if (fmt->yHeight != tmp.yHeight) fmt->dwMask &= ~CFM_SIZE;
904 if (fmt->dwMask & CFM_FACE)
906 if (!(tmp.dwMask & CFM_FACE))
907 fmt->dwMask &= ~CFM_FACE;
908 else if (wcscmp( fmt->szFaceName, tmp.szFaceName ) ||
909 fmt->bPitchAndFamily != tmp.bPitchAndFamily)
910 fmt->dwMask &= ~CFM_FACE;
912 if (fmt->yHeight != tmp.yHeight) fmt->dwMask &= ~CFM_SIZE;
913 if (fmt->bUnderlineType != tmp.bUnderlineType) fmt->dwMask &= ~CFM_UNDERLINETYPE;
914 if (fmt->dwMask & CFM_COLOR)
916 if (!((fmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
918 if (fmt->crTextColor != tmp.crTextColor)
919 fmt->dwMask &= ~CFM_COLOR;
923 fmt->dwMask &= ~((fmt->dwEffects ^ tmp.dwEffects) & dwEffects);
924 fmt->dwEffects = tmp.dwEffects;
926 } while(run != run_end);