riched20: Use PointFromCharContext now that we can specify logical ordering.
[wine.git] / dlls / riched20 / run.c
blob8929fc536cb5b3c4e2882efa37735e4592b60104
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 /******************************************************************************
31 * ME_CanJoinRuns
33 * Returns 1 if two runs can be safely merged into one, 0 otherwise.
34 */
35 int ME_CanJoinRuns(const ME_Run *run1, const ME_Run *run2)
37 if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
38 return 0;
39 if (run1->style != run2->style)
40 return 0;
41 if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
42 return 0;
43 return 1;
46 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
48 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
49 assert(p);
50 ME_PropagateCharOffset(p, shift);
53 /******************************************************************************
54 * ME_PropagateCharOffsets
56 * Shifts (increases or decreases) character offset (relative to beginning of
57 * the document) of the part of the text starting from given place.
58 */
59 void ME_PropagateCharOffset(ME_DisplayItem *p, int shift)
61 /* Runs in one paragraph contain character offset relative to their owning
62 * paragraph. If we start the shifting from the run, we need to shift
63 * all the relative offsets until the end of the paragraph
64 */
65 if (p->type == diRun) /* propagate in all runs in this para */
67 TRACE("PropagateCharOffset(%s, %d)\n", debugstr_run( &p->member.run ), shift);
68 do {
69 p->member.run.nCharOfs += shift;
70 assert(p->member.run.nCharOfs >= 0);
71 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
72 } while(p->type == diRun);
74 /* Runs in next paragraphs don't need their offsets updated, because they,
75 * again, those offsets are relative to their respective paragraphs.
76 * Instead of that, we're updating paragraphs' character offsets.
77 */
78 if (p->type == diParagraph) /* propagate in all next paras */
80 do {
81 p->member.para.nCharOfs += shift;
82 assert(p->member.para.nCharOfs >= 0);
83 p = p->member.para.next_para;
84 } while(p->type == diParagraph);
86 /* diTextEnd also has character offset in it, which makes finding text length
87 * easier. But it needs to be up to date first.
89 if (p->type == diTextEnd)
91 p->member.para.nCharOfs += shift;
92 assert(p->member.para.nCharOfs >= 0);
96 /******************************************************************************
97 * ME_CheckCharOffsets
99 * Checks if editor lists' validity and optionally dumps the document structure
101 void ME_CheckCharOffsets(ME_TextEditor *editor)
103 ME_DisplayItem *p = editor->pBuffer->pFirst;
104 int ofs = 0, ofsp = 0;
105 if(TRACE_ON(richedit_lists))
107 TRACE_(richedit_lists)("---\n");
108 ME_DumpDocument(editor->pBuffer);
110 do {
111 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
112 switch(p->type) {
113 case diTextEnd:
114 TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
115 assert(ofsp+ofs == p->member.para.nCharOfs);
116 return;
117 case diParagraph:
118 TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
119 assert(ofsp+ofs == p->member.para.nCharOfs);
120 ofsp = p->member.para.nCharOfs;
121 ofs = 0;
122 break;
123 case diRun:
124 TRACE_(richedit_check)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = %s, flags=%08x, fx&mask = %08x\n",
125 p->member.run.nCharOfs, p->member.run.nCharOfs+ofsp, ofsp+ofs,
126 p->member.run.len, debugstr_run( &p->member.run ),
127 p->member.run.nFlags,
128 p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
129 assert(ofs == p->member.run.nCharOfs);
130 assert(p->member.run.len);
131 ofs += p->member.run.len;
132 break;
133 case diCell:
134 TRACE_(richedit_check)("cell\n");
135 break;
136 default:
137 assert(0);
139 } while(1);
142 /******************************************************************************
143 * ME_CharOfsFromRunOfs
145 * Converts a character position relative to the start of the run, to a
146 * character position relative to the start of the document.
147 * Kind of a "local to global" offset conversion.
149 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara,
150 const ME_DisplayItem *pRun, int nOfs)
152 assert(pRun && pRun->type == diRun);
153 assert(pPara && pPara->type == diParagraph);
154 return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs + nOfs;
157 /******************************************************************************
158 * ME_CursorFromCharOfs
160 * Converts a character offset (relative to the start of the document) to
161 * a cursor structure (which contains a run and a position relative to that
162 * run).
164 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
166 ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pPara,
167 &pCursor->pRun, &pCursor->nOffset);
170 /******************************************************************************
171 * ME_RunOfsFromCharOfs
173 * Find a run and relative character offset given an absolute character offset
174 * (absolute offset being an offset relative to the start of the document).
175 * Kind of a "global to local" offset conversion.
177 void ME_RunOfsFromCharOfs(ME_TextEditor *editor,
178 int nCharOfs,
179 ME_DisplayItem **ppPara,
180 ME_DisplayItem **ppRun,
181 int *pOfs)
183 ME_DisplayItem *item, *next_item;
185 nCharOfs = max(nCharOfs, 0);
186 nCharOfs = min(nCharOfs, ME_GetTextLength(editor));
188 /* Find the paragraph at the offset. */
189 next_item = editor->pBuffer->pFirst->member.para.next_para;
190 do {
191 item = next_item;
192 next_item = item->member.para.next_para;
193 } while (next_item->member.para.nCharOfs <= nCharOfs);
194 assert(item->type == diParagraph);
195 nCharOfs -= item->member.para.nCharOfs;
196 if (ppPara) *ppPara = item;
198 /* Find the run at the offset. */
199 next_item = ME_FindItemFwd(item, diRun);
200 do {
201 item = next_item;
202 next_item = ME_FindItemFwd(item, diRunOrParagraphOrEnd);
203 } while (next_item->type == diRun &&
204 next_item->member.run.nCharOfs <= nCharOfs);
205 assert(item->type == diRun);
206 nCharOfs -= item->member.run.nCharOfs;
208 if (ppRun) *ppRun = item;
209 if (pOfs) *pOfs = nCharOfs;
212 /******************************************************************************
213 * ME_JoinRuns
215 * Merges two adjacent runs, the one given as a parameter and the next one.
217 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
219 ME_DisplayItem *pNext = p->next;
220 int i;
221 assert(p->type == diRun && pNext->type == diRun);
222 assert(p->member.run.nCharOfs != -1);
223 ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
225 /* Update all cursors so that they don't contain the soon deleted run */
226 for (i=0; i<editor->nCursors; i++) {
227 if (editor->pCursors[i].pRun == pNext) {
228 editor->pCursors[i].pRun = p;
229 editor->pCursors[i].nOffset += p->member.run.len;
233 p->member.run.len += pNext->member.run.len;
234 ME_Remove(pNext);
235 ME_DestroyDisplayItem(pNext);
236 ME_UpdateRunFlags(editor, &p->member.run);
237 if(TRACE_ON(richedit))
239 TRACE("Before check after join\n");
240 ME_CheckCharOffsets(editor);
241 TRACE("After check after join\n");
245 /******************************************************************************
246 * ME_SplitRunSimple
248 * Does the most basic job of splitting a run into two - it does not
249 * update the positions and extents.
251 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_Cursor *cursor)
253 ME_DisplayItem *run = cursor->pRun;
254 ME_DisplayItem *new_run;
255 int i;
256 int nOffset = cursor->nOffset;
258 assert(!(run->member.run.nFlags & MERF_NONTEXT));
260 new_run = ME_MakeRun(run->member.run.style,
261 run->member.run.nFlags & MERF_SPLITMASK);
262 new_run->member.run.nCharOfs = run->member.run.nCharOfs + nOffset;
263 new_run->member.run.len = run->member.run.len - nOffset;
264 new_run->member.run.para = run->member.run.para;
265 run->member.run.len = nOffset;
266 cursor->pRun = new_run;
267 cursor->nOffset = 0;
269 ME_InsertBefore(run->next, new_run);
271 ME_UpdateRunFlags(editor, &run->member.run);
272 ME_UpdateRunFlags(editor, &new_run->member.run);
273 for (i = 0; i < editor->nCursors; i++) {
274 if (editor->pCursors[i].pRun == run &&
275 editor->pCursors[i].nOffset >= nOffset) {
276 editor->pCursors[i].pRun = new_run;
277 editor->pCursors[i].nOffset -= nOffset;
280 cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
281 return run;
284 /******************************************************************************
285 * ME_MakeRun
287 * A helper function to create run structures quickly.
289 ME_DisplayItem *ME_MakeRun(ME_Style *s, int nFlags)
291 ME_DisplayItem *item = ME_MakeDI(diRun);
292 item->member.run.style = s;
293 item->member.run.ole_obj = NULL;
294 item->member.run.nFlags = nFlags;
295 item->member.run.nCharOfs = -1;
296 item->member.run.len = 0;
297 item->member.run.para = NULL;
298 ME_AddRefStyle(s);
299 return item;
302 /******************************************************************************
303 * ME_InsertRunAtCursor
305 * Inserts a new run with given style, flags and content at a given position,
306 * which is passed as a cursor structure (which consists of a run and
307 * a run-relative character offset).
309 ME_DisplayItem *
310 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
311 const WCHAR *str, int len, int flags)
313 ME_DisplayItem *pDI;
315 if (cursor->nOffset)
316 ME_SplitRunSimple(editor, cursor);
318 add_undo_delete_run( editor, cursor->pPara->member.para.nCharOfs +
319 cursor->pRun->member.run.nCharOfs, len );
321 pDI = ME_MakeRun(style, flags);
322 pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
323 pDI->member.run.len = len;
324 pDI->member.run.para = cursor->pRun->member.run.para;
325 ME_InsertString( pDI->member.run.para->text, pDI->member.run.nCharOfs, str, len );
326 ME_InsertBefore(cursor->pRun, pDI);
327 TRACE("Shift length:%d\n", len);
328 ME_PropagateCharOffset(cursor->pRun, len);
329 cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
330 return pDI;
333 static BOOL run_is_splittable( const ME_Run *run )
335 WCHAR *str = get_text( run, 0 ), *p;
336 int i;
337 BOOL found_ink = FALSE;
339 for (i = 0, p = str; i < run->len; i++, p++)
341 if (ME_IsWSpace( *p ))
343 if (found_ink) return TRUE;
345 else
346 found_ink = TRUE;
348 return FALSE;
351 static BOOL run_is_entirely_ws( const ME_Run *run )
353 WCHAR *str = get_text( run, 0 ), *p;
354 int i;
356 for (i = 0, p = str; i < run->len; i++, p++)
357 if (!ME_IsWSpace( *p )) return FALSE;
359 return TRUE;
362 /******************************************************************************
363 * ME_UpdateRunFlags
365 * Determine some of run attributes given its content (style, text content).
366 * Some flags cannot be determined by this function (MERF_GRAPHICS,
367 * MERF_ENDPARA)
369 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
371 assert(run->nCharOfs >= 0);
373 if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
374 run->nFlags |= MERF_HIDDEN;
375 else
376 run->nFlags &= ~MERF_HIDDEN;
378 if (run_is_splittable( run ))
379 run->nFlags |= MERF_SPLITTABLE;
380 else
381 run->nFlags &= ~MERF_SPLITTABLE;
383 if (!(run->nFlags & MERF_NOTEXT))
385 if (run_is_entirely_ws( run ))
386 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
387 else
389 run->nFlags &= ~MERF_WHITESPACE;
391 if (ME_IsWSpace( *get_text( run, 0 ) ))
392 run->nFlags |= MERF_STARTWHITE;
393 else
394 run->nFlags &= ~MERF_STARTWHITE;
396 if (ME_IsWSpace( *get_text( run, run->len - 1 ) ))
397 run->nFlags |= MERF_ENDWHITE;
398 else
399 run->nFlags &= ~MERF_ENDWHITE;
402 else
403 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
406 /******************************************************************************
407 * ME_CharFromPointContext
409 * Returns a character position inside the run given a run-relative
410 * pixel horizontal position.
412 * If closest is FALSE return the actual character
413 * If closest is TRUE will round to the closest leading edge.
414 * ie. if the second character is at pixel position 8 and third at 16 then for:
415 * closest = FALSE cx = 0..7 return 0, cx = 8..15 return 1
416 * closest = TRUE cx = 0..3 return 0, cx = 4..11 return 1.
418 int ME_CharFromPointContext(ME_Context *c, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
420 ME_String *mask_text = NULL;
421 WCHAR *str;
422 int fit = 0;
423 HGDIOBJ hOldFont;
424 SIZE sz, sz2, sz3;
425 if (!run->len || cx <= 0)
426 return 0;
428 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
430 if (!closest || cx < run->nWidth / 2) return 0;
431 return 1;
434 if (run->nFlags & MERF_GRAPHICS)
436 SIZE sz;
437 ME_GetOLEObjectSize(c, run, &sz);
438 if (!closest || cx < sz.cx / 2) return 0;
439 return 1;
442 if (c->editor->cPasswordMask)
444 mask_text = ME_MakeStringR( c->editor->cPasswordMask, run->len );
445 str = mask_text->szData;
447 else
448 str = get_text( run, 0 );
450 hOldFont = ME_SelectStyleFont(c, run->style);
451 GetTextExtentExPointW(c->hDC, str, run->len,
452 cx, &fit, NULL, &sz);
453 if (closest && fit != run->len)
455 GetTextExtentPoint32W(c->hDC, str, fit, &sz2);
456 GetTextExtentPoint32W(c->hDC, str, fit + 1, &sz3);
457 if (cx >= (sz2.cx+sz3.cx)/2)
458 fit = fit + 1;
461 ME_DestroyString( mask_text );
463 ME_UnselectStyleFont(c, run->style, hOldFont);
464 return fit;
467 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
469 ME_Context c;
470 int ret;
472 ME_InitContext( &c, editor, ITextHost_TxGetDC( editor->texthost ) );
473 ret = ME_CharFromPointContext( &c, cx, run, closest, visual_order );
474 ME_DestroyContext(&c);
475 return ret;
478 /******************************************************************************
479 * ME_GetTextExtent
481 * Finds a width and a height of the text using a specified style
483 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
485 HGDIOBJ hOldFont;
486 if (c->hDC) {
487 hOldFont = ME_SelectStyleFont(c, s);
488 GetTextExtentPoint32W(c->hDC, szText, nChars, size);
489 ME_UnselectStyleFont(c, s, hOldFont);
490 } else {
491 size->cx = 0;
492 size->cy = 0;
496 /******************************************************************************
497 * ME_PointFromCharContext
499 * Returns a run-relative pixel position given a run-relative character
500 * position (character offset)
502 int ME_PointFromCharContext(ME_Context *c, ME_Run *pRun, int nOffset, BOOL visual_order)
504 SIZE size;
505 ME_String *mask_text = NULL;
506 WCHAR *str;
508 if (pRun->nFlags & MERF_GRAPHICS)
510 if (nOffset)
511 ME_GetOLEObjectSize(c, pRun, &size);
512 return nOffset != 0;
513 } else if (pRun->nFlags & MERF_ENDPARA) {
514 nOffset = 0;
517 if (c->editor->cPasswordMask)
519 mask_text = ME_MakeStringR(c->editor->cPasswordMask, pRun->len);
520 str = mask_text->szData;
522 else
523 str = get_text( pRun, 0 );
525 ME_GetTextExtent(c, str, nOffset, pRun->style, &size);
526 ME_DestroyString( mask_text );
527 return size.cx;
530 /******************************************************************************
531 * ME_PointFromChar
533 * Calls ME_PointFromCharContext after first creating a context.
535 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset, BOOL visual_order)
537 ME_Context c;
538 int ret;
540 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
541 ret = ME_PointFromCharContext( &c, pRun, nOffset, visual_order );
542 ME_DestroyContext(&c);
544 return ret;
547 /******************************************************************************
548 * ME_GetRunSizeCommon
550 * Finds width, height, ascent and descent of a run, up to given character
551 * (nLen).
553 SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
554 int startx, int *pAscent, int *pDescent)
556 SIZE size;
557 int nMaxLen = run->len;
559 if (nLen>nMaxLen)
560 nLen = nMaxLen;
562 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
563 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
564 * in practice
567 if (c->editor->cPasswordMask)
569 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
570 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
571 ME_DestroyString(szMasked);
573 else
575 ME_GetTextExtent(c, get_text( run, 0 ), nLen, run->style, &size);
577 *pAscent = run->style->tm.tmAscent;
578 *pDescent = run->style->tm.tmDescent;
579 size.cy = *pAscent + *pDescent;
581 if (run->nFlags & MERF_TAB)
583 int pos = 0, i = 0, ppos, shift = 0;
584 PARAFORMAT2 *pFmt = para->pFmt;
586 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
587 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
588 /* The horizontal gap shifts the tab positions to leave the gap. */
589 shift = pFmt->dxOffset * 2;
590 do {
591 if (i < pFmt->cTabCount)
593 /* Only one side of the horizontal gap is needed at the end of
594 * the table row. */
595 if (i == pFmt->cTabCount -1)
596 shift = shift >> 1;
597 pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
598 i++;
600 else
602 pos += lDefaultTab - (pos % lDefaultTab);
604 ppos = ME_twips2pointsX(c, pos);
605 if (ppos > startx + run->pt.x) {
606 size.cx = ppos - startx - run->pt.x;
607 break;
609 } while(1);
610 size.cy = *pAscent + *pDescent;
611 return size;
613 if (run->nFlags & MERF_GRAPHICS)
615 ME_GetOLEObjectSize(c, run, &size);
616 if (size.cy > *pAscent)
617 *pAscent = size.cy;
618 /* descent is unchanged */
619 return size;
621 return size;
624 /******************************************************************************
625 * ME_SetSelectionCharFormat
627 * Applies a style change, either to a current selection, or to insert cursor
628 * (ie. the style next typed characters will use).
630 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
632 if (!ME_IsSelection(editor))
634 ME_Style *s;
635 if (!editor->pBuffer->pCharStyle)
636 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
637 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
638 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
639 editor->pBuffer->pCharStyle = s;
640 } else {
641 ME_Cursor *from, *to;
642 ME_GetSelection(editor, &from, &to);
643 ME_SetCharFormat(editor, from, to, pFmt);
647 /******************************************************************************
648 * ME_SetCharFormat
650 * Applies a style change to the specified part of the text
652 * The start and end cursors specify the part of the text. These cursors will
653 * be updated to stay valid, but this function may invalidate other
654 * non-selection cursors. The end cursor may be NULL to specify all the text
655 * following the start cursor.
657 * If no text is selected, then nothing is done.
659 void ME_SetCharFormat(ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *pFmt)
661 ME_DisplayItem *para;
662 ME_DisplayItem *run;
663 ME_DisplayItem *end_run = NULL;
665 if (end && start->pRun == end->pRun && start->nOffset == end->nOffset)
666 return;
668 if (start->nOffset)
670 /* SplitRunSimple may or may not update the cursors, depending on whether they
671 * are selection cursors, but we need to make sure they are valid. */
672 int split_offset = start->nOffset;
673 ME_DisplayItem *split_run = ME_SplitRunSimple(editor, start);
674 if (end && end->pRun == split_run)
676 end->pRun = start->pRun;
677 end->nOffset -= split_offset;
681 if (end && end->nOffset)
682 ME_SplitRunSimple(editor, end);
683 end_run = end ? end->pRun : NULL;
685 run = start->pRun;
686 para = start->pPara;
687 para->member.para.nFlags |= MEPF_REWRAP;
689 while(run != end_run)
691 ME_Style *new_style = ME_ApplyStyle(run->member.run.style, pFmt);
692 /* ME_DumpStyle(new_style); */
694 add_undo_set_char_fmt( editor, para->member.para.nCharOfs + run->member.run.nCharOfs,
695 run->member.run.len, &run->member.run.style->fmt );
696 ME_ReleaseStyle(run->member.run.style);
697 run->member.run.style = new_style;
698 run = ME_FindItemFwd(run, diRunOrParagraph);
699 if (run && run->type == diParagraph)
701 para = run;
702 run = ME_FindItemFwd(run, diRun);
703 if (run != end_run)
704 para->member.para.nFlags |= MEPF_REWRAP;
709 /******************************************************************************
710 * ME_SetDefaultCharFormat
712 * Applies a style change to the default character style.
714 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
716 ME_Style *style;
718 assert(mod->cbSize == sizeof(CHARFORMAT2W));
719 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
720 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
721 editor->pBuffer->pDefaultStyle->tm = style->tm;
722 ME_ReleaseStyle(style);
723 ME_MarkAllForWrapping(editor);
724 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
727 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
729 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
730 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_CF1UNDERLINE))
732 pFmt->dwMask |= CFM_UNDERLINE;
733 pFmt->dwEffects |= CFE_UNDERLINE;
735 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_UNDERLINENONE))
737 pFmt->dwMask |= CFM_UNDERLINE;
738 pFmt->dwEffects &= ~CFE_UNDERLINE;
742 /******************************************************************************
743 * ME_GetDefaultCharFormat
745 * Retrieves the current default character style (the one applied where no
746 * other style was applied) .
748 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
750 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
753 /******************************************************************************
754 * ME_GetSelectionCharFormat
756 * If selection exists, it returns all style elements that are set consistently
757 * in the whole selection. If not, it just returns the current style.
759 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
761 ME_Cursor *from, *to;
762 if (!ME_IsSelection(editor) && editor->pBuffer->pCharStyle)
764 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
765 return;
767 ME_GetSelection(editor, &from, &to);
768 ME_GetCharFormat(editor, from, to, pFmt);
771 /******************************************************************************
772 * ME_GetCharFormat
774 * Returns the style consisting of those attributes which are consistently set
775 * in the whole character range.
777 void ME_GetCharFormat(ME_TextEditor *editor, const ME_Cursor *from,
778 const ME_Cursor *to, CHARFORMAT2W *pFmt)
780 ME_DisplayItem *run, *run_end;
781 CHARFORMAT2W tmp;
783 run = from->pRun;
784 /* special case - if selection is empty, take previous char's formatting */
785 if (from->pRun == to->pRun && from->nOffset == to->nOffset)
787 if (!from->nOffset)
789 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
790 if (tmp_run->type == diRun) {
791 ME_GetRunCharFormat(editor, tmp_run, pFmt);
792 return;
795 ME_GetRunCharFormat(editor, run, pFmt);
796 return;
799 run_end = to->pRun;
800 if (!to->nOffset)
801 run_end = ME_FindItemBack(run_end, diRun);
803 ME_GetRunCharFormat(editor, run, pFmt);
805 if (run == run_end) return;
807 do {
808 /* FIXME add more style feature comparisons */
809 DWORD dwAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
810 DWORD dwEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
812 run = ME_FindItemFwd(run, diRun);
814 ZeroMemory(&tmp, sizeof(tmp));
815 tmp.cbSize = sizeof(tmp);
816 ME_GetRunCharFormat(editor, run, &tmp);
818 assert((tmp.dwMask & dwAttribs) == dwAttribs);
819 /* reset flags that differ */
821 if (pFmt->yHeight != tmp.yHeight)
822 pFmt->dwMask &= ~CFM_SIZE;
823 if (pFmt->dwMask & CFM_FACE)
825 if (!(tmp.dwMask & CFM_FACE))
826 pFmt->dwMask &= ~CFM_FACE;
827 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
828 pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
829 pFmt->dwMask &= ~CFM_FACE;
831 if (pFmt->yHeight != tmp.yHeight)
832 pFmt->dwMask &= ~CFM_SIZE;
833 if (pFmt->bUnderlineType != tmp.bUnderlineType)
834 pFmt->dwMask &= ~CFM_UNDERLINETYPE;
835 if (pFmt->dwMask & CFM_COLOR)
837 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
839 if (pFmt->crTextColor != tmp.crTextColor)
840 pFmt->dwMask &= ~CFM_COLOR;
844 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & dwEffects);
845 pFmt->dwEffects = tmp.dwEffects;
847 } while(run != run_end);