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 /******************************************************************************
33 * Returns 1 if two runs can be safely merged into one, 0 otherwise.
35 int ME_CanJoinRuns(const ME_Run *run1, const ME_Run *run2)
37 if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
39 if (run1->style != run2->style)
41 if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
46 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
48 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
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.
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
65 if (p->type == diRun) /* propagate in all runs in this para */
67 TRACE("PropagateCharOffset(%s, %d)\n", debugstr_run( &p->member.run ), shift);
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.
78 if (p->type == diParagraph) /* propagate in all next paras */
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 /******************************************************************************
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);
111 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
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);
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;
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.strText->nLen, 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.strText->nLen);
131 ofs += p->member.run.strText->nLen;
134 TRACE_(richedit_check)("cell\n");
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
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,
179 ME_DisplayItem **ppPara,
180 ME_DisplayItem **ppRun,
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;
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);
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 /******************************************************************************
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;
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.strText->nLen;
233 ME_AppendString(p->member.run.strText, pNext->member.run.strText);
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 /******************************************************************************
248 * Splits a run into two in a given place. It also updates the screen position
249 * and size (extent) of the newly generated runs.
251 ME_DisplayItem *ME_SplitRun(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
253 ME_TextEditor *editor = wc->context->editor;
255 ME_Paragraph *para = &wc->pPara->member.para;
256 ME_Cursor cursor = {wc->pPara, item, nVChar};
258 assert(item->member.run.nCharOfs != -1);
259 if(TRACE_ON(richedit))
261 TRACE("Before check before split\n");
262 ME_CheckCharOffsets(editor);
263 TRACE("After check before split\n");
266 run = &item->member.run;
268 TRACE("Before split: %s(%d, %d)\n", debugstr_run( run ),
269 run->pt.x, run->pt.y);
271 ME_SplitRunSimple(editor, &cursor);
273 run2 = &cursor.pRun->member.run;
275 ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
277 run2->pt.x = run->pt.x+run->nWidth;
278 run2->pt.y = run->pt.y;
280 if(TRACE_ON(richedit))
282 TRACE("Before check after split\n");
283 ME_CheckCharOffsets(editor);
284 TRACE("After check after split\n");
285 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
286 debugstr_run( run ), run->pt.x, run->pt.y,
287 debugstr_run( run2 ), run2->pt.x, run2->pt.y);
293 /******************************************************************************
296 * Does the most basic job of splitting a run into two - it does not
297 * update the positions and extents.
299 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_Cursor *cursor)
301 ME_DisplayItem *run = cursor->pRun;
302 ME_DisplayItem *new_run;
304 int nOffset = cursor->nOffset;
306 assert(!(run->member.run.nFlags & MERF_NONTEXT));
308 new_run = ME_MakeRun(run->member.run.style,
309 ME_VSplitString(run->member.run.strText, nOffset),
310 run->member.run.nFlags & MERF_SPLITMASK);
312 new_run->member.run.nCharOfs = run->member.run.nCharOfs + nOffset;
313 new_run->member.run.para = run->member.run.para;
314 cursor->pRun = new_run;
317 ME_InsertBefore(run->next, new_run);
319 ME_UpdateRunFlags(editor, &run->member.run);
320 ME_UpdateRunFlags(editor, &new_run->member.run);
321 for (i = 0; i < editor->nCursors; i++) {
322 if (editor->pCursors[i].pRun == run &&
323 editor->pCursors[i].nOffset >= nOffset) {
324 editor->pCursors[i].pRun = new_run;
325 editor->pCursors[i].nOffset -= nOffset;
328 cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
332 /******************************************************************************
335 * A helper function to create run structures quickly.
337 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
339 ME_DisplayItem *item = ME_MakeDI(diRun);
340 item->member.run.style = s;
341 item->member.run.ole_obj = NULL;
342 item->member.run.strText = strData;
343 item->member.run.nFlags = nFlags;
344 item->member.run.nCharOfs = -1;
345 item->member.run.para = NULL;
350 /******************************************************************************
351 * ME_InsertRunAtCursor
353 * Inserts a new run with given style, flags and content at a given position,
354 * which is passed as a cursor structure (which consists of a run and
355 * a run-relative character offset).
358 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
359 const WCHAR *str, int len, int flags)
364 ME_SplitRunSimple(editor, cursor);
366 add_undo_delete_run( editor, cursor->pPara->member.para.nCharOfs +
367 cursor->pRun->member.run.nCharOfs, len );
369 pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
370 pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
371 pDI->member.run.para = cursor->pRun->member.run.para;
372 ME_InsertBefore(cursor->pRun, pDI);
373 TRACE("Shift length:%d\n", len);
374 ME_PropagateCharOffset(cursor->pRun, len);
375 cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
379 static BOOL run_is_splittable( const ME_Run *run )
381 WCHAR *str = get_text( run, 0 ), *p;
382 int i, len = run->strText->nLen;
383 BOOL found_ink = FALSE;
385 for (i = 0, p = str; i < len; i++, p++)
387 if (ME_IsWSpace( *p ))
389 if (found_ink) return TRUE;
397 static BOOL run_is_entirely_ws( const ME_Run *run )
399 WCHAR *str = get_text( run, 0 ), *p;
400 int i, len = run->strText->nLen;
402 for (i = 0, p = str; i < len; i++, p++)
403 if (!ME_IsWSpace( *p )) return FALSE;
408 /******************************************************************************
411 * Determine some of run attributes given its content (style, text content).
412 * Some flags cannot be determined by this function (MERF_GRAPHICS,
415 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
417 ME_String *strText = run->strText;
418 assert(run->nCharOfs >= 0);
420 if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
421 run->nFlags |= MERF_HIDDEN;
423 run->nFlags &= ~MERF_HIDDEN;
425 if (run_is_splittable( run ))
426 run->nFlags |= MERF_SPLITTABLE;
428 run->nFlags &= ~MERF_SPLITTABLE;
430 if (!(run->nFlags & MERF_NOTEXT))
432 if (run_is_entirely_ws( run ))
433 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
436 run->nFlags &= ~MERF_WHITESPACE;
438 if (ME_IsWSpace(strText->szData[0]))
439 run->nFlags |= MERF_STARTWHITE;
441 run->nFlags &= ~MERF_STARTWHITE;
443 if (ME_IsWSpace(strText->szData[strText->nLen - 1]))
444 run->nFlags |= MERF_ENDWHITE;
446 run->nFlags &= ~MERF_ENDWHITE;
450 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
453 /******************************************************************************
456 * Returns a character position inside the run given a run-relative
457 * pixel horizontal position. This version rounds left (ie. if the second
458 * character is at pixel position 8, then for cx=0..7 it returns 0).
460 int ME_CharFromPoint(ME_Context *c, int cx, ME_Run *run)
465 if (!run->strText->nLen || cx <= 0)
468 if (run->nFlags & MERF_TAB ||
469 (run->nFlags & (MERF_ENDCELL|MERF_ENDPARA)) == MERF_ENDCELL)
471 if (cx < run->nWidth/2)
475 if (run->nFlags & MERF_GRAPHICS)
478 ME_GetOLEObjectSize(c, run, &sz);
483 hOldFont = ME_SelectStyleFont(c, run->style);
485 if (c->editor->cPasswordMask)
487 ME_String *strMasked = ME_MakeStringR(c->editor->cPasswordMask, run->strText->nLen);
488 GetTextExtentExPointW(c->hDC, strMasked->szData, run->strText->nLen,
489 cx, &fit, NULL, &sz);
490 ME_DestroyString(strMasked);
494 GetTextExtentExPointW(c->hDC, get_text( run, 0 ), run->strText->nLen,
495 cx, &fit, NULL, &sz);
498 ME_UnselectStyleFont(c, run->style, hOldFont);
503 /******************************************************************************
504 * ME_CharFromPointCursor
506 * Returns a character position inside the run given a run-relative
507 * pixel horizontal position. This version rounds to the nearest character edge
508 * (ie. if the second character is at pixel position 8, then for cx=0..3
509 * it returns 0, and for cx=4..7 it returns 1).
511 * It is used for mouse click handling, for better usability (and compatibility
512 * with the native control).
514 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
516 ME_String *mask_text = NULL;
522 if (!run->strText->nLen || cx <= 0)
525 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
527 if (cx < run->nWidth/2)
531 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
532 if (run->nFlags & MERF_GRAPHICS)
535 ME_GetOLEObjectSize(&c, run, &sz);
536 ME_DestroyContext(&c);
542 len = run->strText->nLen;
543 if (editor->cPasswordMask)
545 mask_text = ME_MakeStringR( editor->cPasswordMask, len );
546 str = mask_text->szData;
549 str = get_text( run, 0 );
551 hOldFont = ME_SelectStyleFont(&c, run->style);
552 GetTextExtentExPointW(c.hDC, str, len,
553 cx, &fit, NULL, &sz);
556 GetTextExtentPoint32W(c.hDC, str, fit, &sz2);
557 GetTextExtentPoint32W(c.hDC, str, fit + 1, &sz3);
558 if (cx >= (sz2.cx+sz3.cx)/2)
562 ME_DestroyString( mask_text );
564 ME_UnselectStyleFont(&c, run->style, hOldFont);
565 ME_DestroyContext(&c);
569 /******************************************************************************
572 * Finds a width and a height of the text using a specified style
574 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
578 hOldFont = ME_SelectStyleFont(c, s);
579 GetTextExtentPoint32W(c->hDC, szText, nChars, size);
580 ME_UnselectStyleFont(c, s, hOldFont);
587 /******************************************************************************
590 * Returns a run-relative pixel position given a run-relative character
591 * position (character offset)
593 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
597 ME_String *mask_text = NULL;
601 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
602 if (pRun->nFlags & MERF_GRAPHICS)
605 ME_GetOLEObjectSize(&c, pRun, &size);
606 ME_DestroyContext(&c);
608 } else if (pRun->nFlags & MERF_ENDPARA) {
612 len = pRun->strText->nLen;
613 if (editor->cPasswordMask)
615 mask_text = ME_MakeStringR(editor->cPasswordMask, len);
616 str = mask_text->szData;
619 str = get_text( pRun, 0 );
621 ME_GetTextExtent(&c, str, nOffset, pRun->style, &size);
622 ME_DestroyContext(&c);
623 ME_DestroyString( mask_text );
627 /******************************************************************************
628 * ME_GetRunSizeCommon
630 * Finds width, height, ascent and descent of a run, up to given character
633 static SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
634 int startx, int *pAscent, int *pDescent)
637 int nMaxLen = run->strText->nLen;
642 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
643 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
647 if (c->editor->cPasswordMask)
649 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
650 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
651 ME_DestroyString(szMasked);
655 ME_GetTextExtent(c, get_text( run, 0 ), nLen, run->style, &size);
657 *pAscent = run->style->tm.tmAscent;
658 *pDescent = run->style->tm.tmDescent;
659 size.cy = *pAscent + *pDescent;
661 if (run->nFlags & MERF_TAB)
663 int pos = 0, i = 0, ppos, shift = 0;
664 PARAFORMAT2 *pFmt = para->pFmt;
666 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
667 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
668 /* The horizontal gap shifts the tab positions to leave the gap. */
669 shift = pFmt->dxOffset * 2;
671 if (i < pFmt->cTabCount)
673 /* Only one side of the horizontal gap is needed at the end of
675 if (i == pFmt->cTabCount -1)
677 pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
682 pos += lDefaultTab - (pos % lDefaultTab);
684 ppos = ME_twips2pointsX(c, pos);
685 if (ppos > startx + run->pt.x) {
686 size.cx = ppos - startx - run->pt.x;
690 size.cy = *pAscent + *pDescent;
693 if (run->nFlags & MERF_GRAPHICS)
695 ME_GetOLEObjectSize(c, run, &size);
696 if (size.cy > *pAscent)
698 /* descent is unchanged */
704 /******************************************************************************
707 * Finds width and height (but not ascent and descent) of a part of the run
708 * up to given character.
710 SIZE ME_GetRunSize(ME_Context *c, const ME_Paragraph *para,
711 ME_Run *run, int nLen, int startx)
714 return ME_GetRunSizeCommon(c, para, run, nLen, startx, &asc, &desc);
717 /******************************************************************************
720 * Updates the size of the run (fills width, ascent and descent). The height
721 * is calculated based on whole row's ascent and descent anyway, so no need
724 void ME_CalcRunExtent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
726 if (run->nFlags & MERF_HIDDEN)
730 int nEnd = run->strText->nLen;
731 SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, startx,
732 &run->nAscent, &run->nDescent);
733 run->nWidth = size.cx;
735 WARN("size.cx == 0\n");
739 /******************************************************************************
740 * ME_SetSelectionCharFormat
742 * Applies a style change, either to a current selection, or to insert cursor
743 * (ie. the style next typed characters will use).
745 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
747 if (!ME_IsSelection(editor))
750 if (!editor->pBuffer->pCharStyle)
751 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
752 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
753 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
754 editor->pBuffer->pCharStyle = s;
756 ME_Cursor *from, *to;
757 ME_GetSelection(editor, &from, &to);
758 ME_SetCharFormat(editor, from, to, pFmt);
762 /******************************************************************************
765 * Applies a style change to the specified part of the text
767 * The start and end cursors specify the part of the text. These cursors will
768 * be updated to stay valid, but this function may invalidate other
769 * non-selection cursors. The end cursor may be NULL to specify all the text
770 * following the start cursor.
772 * If no text is selected, then nothing is done.
774 void ME_SetCharFormat(ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *pFmt)
776 ME_DisplayItem *para;
778 ME_DisplayItem *end_run = NULL;
780 if (end && start->pRun == end->pRun && start->nOffset == end->nOffset)
785 /* SplitRunSimple may or may not update the cursors, depending on whether they
786 * are selection cursors, but we need to make sure they are valid. */
787 int split_offset = start->nOffset;
788 ME_DisplayItem *split_run = ME_SplitRunSimple(editor, start);
789 if (end && end->pRun == split_run)
791 end->pRun = start->pRun;
792 end->nOffset -= split_offset;
796 if (end && end->nOffset)
797 ME_SplitRunSimple(editor, end);
798 end_run = end ? end->pRun : NULL;
802 para->member.para.nFlags |= MEPF_REWRAP;
804 while(run != end_run)
806 ME_Style *new_style = ME_ApplyStyle(run->member.run.style, pFmt);
807 /* ME_DumpStyle(new_style); */
809 add_undo_set_char_fmt( editor, para->member.para.nCharOfs + run->member.run.nCharOfs,
810 run->member.run.strText->nLen, &run->member.run.style->fmt );
811 ME_ReleaseStyle(run->member.run.style);
812 run->member.run.style = new_style;
813 run = ME_FindItemFwd(run, diRunOrParagraph);
814 if (run && run->type == diParagraph)
817 run = ME_FindItemFwd(run, diRun);
819 para->member.para.nFlags |= MEPF_REWRAP;
824 /******************************************************************************
825 * ME_SetDefaultCharFormat
827 * Applies a style change to the default character style.
829 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
833 assert(mod->cbSize == sizeof(CHARFORMAT2W));
834 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
835 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
836 editor->pBuffer->pDefaultStyle->tm = style->tm;
837 ME_ReleaseStyle(style);
838 ME_MarkAllForWrapping(editor);
839 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
842 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
844 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
845 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_CF1UNDERLINE))
847 pFmt->dwMask |= CFM_UNDERLINE;
848 pFmt->dwEffects |= CFE_UNDERLINE;
850 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_UNDERLINENONE))
852 pFmt->dwMask |= CFM_UNDERLINE;
853 pFmt->dwEffects &= ~CFE_UNDERLINE;
857 /******************************************************************************
858 * ME_GetDefaultCharFormat
860 * Retrieves the current default character style (the one applied where no
861 * other style was applied) .
863 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
865 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
868 /******************************************************************************
869 * ME_GetSelectionCharFormat
871 * If selection exists, it returns all style elements that are set consistently
872 * in the whole selection. If not, it just returns the current style.
874 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
876 ME_Cursor *from, *to;
877 if (!ME_IsSelection(editor) && editor->pBuffer->pCharStyle)
879 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
882 ME_GetSelection(editor, &from, &to);
883 ME_GetCharFormat(editor, from, to, pFmt);
886 /******************************************************************************
889 * Returns the style consisting of those attributes which are consistently set
890 * in the whole character range.
892 void ME_GetCharFormat(ME_TextEditor *editor, const ME_Cursor *from,
893 const ME_Cursor *to, CHARFORMAT2W *pFmt)
895 ME_DisplayItem *run, *run_end;
899 /* special case - if selection is empty, take previous char's formatting */
900 if (from->pRun == to->pRun && from->nOffset == to->nOffset)
904 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
905 if (tmp_run->type == diRun) {
906 ME_GetRunCharFormat(editor, tmp_run, pFmt);
910 ME_GetRunCharFormat(editor, run, pFmt);
916 run_end = ME_FindItemBack(run_end, diRun);
918 ME_GetRunCharFormat(editor, run, pFmt);
920 if (run == run_end) return;
923 /* FIXME add more style feature comparisons */
924 DWORD dwAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
925 DWORD dwEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
927 run = ME_FindItemFwd(run, diRun);
929 ZeroMemory(&tmp, sizeof(tmp));
930 tmp.cbSize = sizeof(tmp);
931 ME_GetRunCharFormat(editor, run, &tmp);
933 assert((tmp.dwMask & dwAttribs) == dwAttribs);
934 /* reset flags that differ */
936 if (pFmt->yHeight != tmp.yHeight)
937 pFmt->dwMask &= ~CFM_SIZE;
938 if (pFmt->dwMask & CFM_FACE)
940 if (!(tmp.dwMask & CFM_FACE))
941 pFmt->dwMask &= ~CFM_FACE;
942 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
943 pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
944 pFmt->dwMask &= ~CFM_FACE;
946 if (pFmt->yHeight != tmp.yHeight)
947 pFmt->dwMask &= ~CFM_SIZE;
948 if (pFmt->bUnderlineType != tmp.bUnderlineType)
949 pFmt->dwMask &= ~CFM_UNDERLINETYPE;
950 if (pFmt->dwMask & CFM_COLOR)
952 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
954 if (pFmt->crTextColor != tmp.crTextColor)
955 pFmt->dwMask &= ~CFM_COLOR;
959 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & dwEffects);
960 pFmt->dwEffects = tmp.dwEffects;
962 } while(run != run_end);