push f2fb4b43e6a5ad960b5745b9c70e0a8fefaca935
[wine/hacks.git] / dlls / riched20 / run.c
blob2da891e7851a3536ea22aa4822454bd26e26e788
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_w(p->member.run.strText->szData), 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.strText->nLen, debugstr_w(p->member.run.strText->szData),
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 if (p->member.run.nFlags & MERF_ENDPARA)
131 ofs += (editor->bEmulateVersion10 ? 2 : 1);
132 else
133 ofs += ME_StrLen(p->member.run.strText);
134 break;
135 default:
136 assert(0);
138 } while(1);
141 /******************************************************************************
142 * ME_CharOfsFromRunOfs
144 * Converts a character position relative to the start of the run, to a
145 * character position relative to the start of the document.
146 * Kind of a "local to global" offset conversion.
148 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
150 ME_DisplayItem *pPara;
152 assert(pRun->type == diRun);
153 assert(pRun->member.run.nCharOfs != -1);
155 pPara = ME_FindItemBack(pRun, diParagraph);
156 assert(pPara);
157 assert(pPara->type==diParagraph);
158 return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs
159 + ME_VPosToPos(pRun->member.run.strText, nOfs);
162 /******************************************************************************
163 * ME_CursorFromCharOfs
165 * Converts a character offset (relative to the start of the document) to
166 * a cursor structure (which contains a run and a position relative to that
167 * run).
169 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
171 ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
174 /******************************************************************************
175 * ME_RunOfsFromCharOfs
177 * Find a run and relative character offset given an absolute character offset
178 * (absolute offset being an offset relative to the start of the document).
179 * Kind of a "global to local" offset conversion.
181 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
183 ME_DisplayItem *pPara;
184 int nParaOfs;
186 pPara = editor->pBuffer->pFirst->member.para.next_para;
187 assert(pPara);
188 assert(ppRun);
189 assert(pOfs);
190 while (pPara->type == diParagraph)
192 nParaOfs = pPara->member.para.nCharOfs;
193 assert(nCharOfs >= nParaOfs);
195 if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
197 int eollen = 1;
198 *ppRun = ME_FindItemFwd(pPara, diRun);
199 assert(*ppRun);
200 while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
202 ME_DisplayItem *pNext = ME_FindItemFwd(*ppRun, diRun);
203 assert(pNext);
204 assert(pNext->type == diRun);
205 if (nCharOfs < nParaOfs + pNext->member.run.nCharOfs) {
206 *pOfs = ME_PosToVPos((*ppRun)->member.run.strText,
207 nCharOfs - nParaOfs - (*ppRun)->member.run.nCharOfs);
208 return;
210 *ppRun = pNext;
212 /* the handling of bEmulateVersion10 may be a source of many bugs, I'm afraid */
213 eollen = (editor->bEmulateVersion10 ? 2 : 1);
214 if (nCharOfs >= nParaOfs + (*ppRun)->member.run.nCharOfs &&
215 nCharOfs < nParaOfs + (*ppRun)->member.run.nCharOfs + eollen) {
216 *pOfs = 0;
217 return;
220 pPara = pPara->member.para.next_para;
222 *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
223 *pOfs = 0;
224 assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
227 /******************************************************************************
228 * ME_JoinRuns
230 * Merges two adjacent runs, the one given as a parameter and the next one.
232 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
234 ME_DisplayItem *pNext = p->next;
235 int i;
236 assert(p->type == diRun && pNext->type == diRun);
237 assert(p->member.run.nCharOfs != -1);
238 ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
240 /* if we were at the end of screen line, and the next run is in the new
241 * line, then it's not the end of the line anymore */
242 if (editor->bCaretAtEnd && editor->pCursors[0].pRun == pNext)
243 editor->bCaretAtEnd = FALSE;
244 /* Update all cursors so that they don't contain the soon deleted run */
245 for (i=0; i<editor->nCursors; i++) {
246 if (editor->pCursors[i].pRun == pNext) {
247 editor->pCursors[i].pRun = p;
248 editor->pCursors[i].nOffset += ME_StrVLen(p->member.run.strText);
252 ME_AppendString(p->member.run.strText, pNext->member.run.strText);
253 ME_Remove(pNext);
254 ME_DestroyDisplayItem(pNext);
255 ME_UpdateRunFlags(editor, &p->member.run);
256 if(TRACE_ON(richedit))
258 TRACE("Before check after join\n");
259 ME_CheckCharOffsets(editor);
260 TRACE("After check after join\n");
264 /******************************************************************************
265 * ME_SplitRun
267 * Splits a run into two in a given place. It also updates the screen position
268 * and size (extent) of the newly generated runs.
270 ME_DisplayItem *ME_SplitRun(ME_Context *c, ME_DisplayItem *item, int nVChar)
272 ME_TextEditor *editor = c->editor;
273 ME_DisplayItem *item2 = NULL;
274 ME_Run *run, *run2;
275 ME_Paragraph *para = &ME_GetParagraph(item)->member.para;
277 assert(item->member.run.nCharOfs != -1);
278 if(TRACE_ON(richedit))
280 TRACE("Before check before split\n");
281 ME_CheckCharOffsets(editor);
282 TRACE("After check before split\n");
285 run = &item->member.run;
287 TRACE("Before split: %s(%d, %d)\n", debugstr_w(run->strText->szData),
288 run->pt.x, run->pt.y);
290 item2 = ME_SplitRunSimple(editor, item, nVChar);
292 run2 = &item2->member.run;
294 ME_CalcRunExtent(c, para, run);
295 ME_CalcRunExtent(c, para, run2);
297 run2->pt.x = run->pt.x+run->nWidth;
298 run2->pt.y = run->pt.y;
300 if(TRACE_ON(richedit))
302 TRACE("Before check after split\n");
303 ME_CheckCharOffsets(editor);
304 TRACE("After check after split\n");
305 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
306 debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
307 debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
310 return item2;
313 /******************************************************************************
314 * ME_SplitRunSimple
316 * Does the most basic job of splitting a run into two - it does not
317 * update the positions and extents.
319 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
321 ME_Run *run = &item->member.run;
322 ME_DisplayItem *item2;
323 ME_Run *run2;
324 int i;
325 assert(nVChar > 0 && nVChar < ME_StrVLen(run->strText));
326 assert(item->type == diRun);
327 assert(!(item->member.run.nFlags & MERF_NONTEXT));
328 assert(item->member.run.nCharOfs != -1);
330 item2 = ME_MakeRun(run->style,
331 ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
333 item2->member.run.nCharOfs = item->member.run.nCharOfs+
334 ME_VPosToPos(item->member.run.strText, nVChar);
336 run2 = &item2->member.run;
337 ME_InsertBefore(item->next, item2);
339 ME_UpdateRunFlags(editor, run);
340 ME_UpdateRunFlags(editor, run2);
341 for (i=0; i<editor->nCursors; i++) {
342 if (editor->pCursors[i].pRun == item &&
343 editor->pCursors[i].nOffset >= nVChar) {
344 assert(item2->type == diRun);
345 editor->pCursors[i].pRun = item2;
346 editor->pCursors[i].nOffset -= nVChar;
349 ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
350 return item2;
353 /******************************************************************************
354 * ME_MakeRun
356 * A helper function to create run structures quickly.
358 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
360 ME_DisplayItem *item = ME_MakeDI(diRun);
361 item->member.run.style = s;
362 item->member.run.strText = strData;
363 item->member.run.nFlags = nFlags;
364 item->member.run.nCharOfs = -1;
365 ME_AddRefStyle(s);
366 return item;
369 /******************************************************************************
370 * ME_InsertRun
372 * Inserts a run at a given character position (offset).
374 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
376 ME_Cursor tmp;
377 ME_DisplayItem *pDI;
379 assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
381 ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
382 pDI = ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
383 pItem->member.run.strText->szData,
384 pItem->member.run.strText->nLen,
385 pItem->member.run.nFlags);
387 return pDI;
390 /******************************************************************************
391 * ME_InsertRunAtCursor
393 * Inserts a new run with given style, flags and content at a given position,
394 * which is passed as a cursor structure (which consists of a run and
395 * a run-relative character offset).
397 ME_DisplayItem *
398 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
399 const WCHAR *str, int len, int flags)
401 ME_DisplayItem *pDI;
402 ME_UndoItem *pUI;
404 if (cursor->nOffset) {
405 /* We're inserting at the middle of the existing run, which means that
406 * that run must be split. It isn't always necessary, but */
407 cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
408 cursor->nOffset = 0;
411 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
412 if (pUI) {
413 pUI->nStart = (ME_GetParagraph(cursor->pRun)->member.para.nCharOfs
414 + cursor->pRun->member.run.nCharOfs);
415 pUI->nLen = len;
418 pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
419 pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
420 ME_InsertBefore(cursor->pRun, pDI);
421 TRACE("Shift length:%d\n", len);
422 ME_PropagateCharOffset(cursor->pRun, len);
423 ME_GetParagraph(cursor->pRun)->member.para.nFlags |= MEPF_REWRAP;
424 return pDI;
427 /******************************************************************************
428 * ME_UpdateRunFlags
430 * Determine some of run attributes given its content (style, text content).
431 * Some flags cannot be determined by this function (MERF_GRAPHICS,
432 * MERF_ENDPARA)
434 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
436 assert(run->nCharOfs != -1);
438 if (RUN_IS_HIDDEN(run))
439 run->nFlags |= MERF_HIDDEN;
440 else
441 run->nFlags &= ~MERF_HIDDEN;
443 if (ME_IsSplitable(run->strText))
444 run->nFlags |= MERF_SPLITTABLE;
445 else
446 run->nFlags &= ~MERF_SPLITTABLE;
448 if (!(run->nFlags & MERF_NOTEXT)) {
449 if (ME_IsWhitespaces(run->strText))
450 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
451 else
453 run->nFlags &= ~MERF_WHITESPACE;
455 if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
456 run->nFlags |= MERF_STARTWHITE;
457 else
458 run->nFlags &= ~MERF_STARTWHITE;
460 if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
461 run->nFlags |= MERF_ENDWHITE;
462 else
463 run->nFlags &= ~MERF_ENDWHITE;
466 else
467 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
470 /******************************************************************************
471 * ME_GetGraphicsSize
473 * Sets run extent for graphics runs. This functionality is just a placeholder
474 * for future OLE object support, and will be removed.
476 void ME_GetGraphicsSize(ME_TextEditor *editor, ME_Run *run, SIZE *pSize)
478 assert(run->nFlags & MERF_GRAPHICS);
479 pSize->cx = 64;
480 pSize->cy = 64;
483 /******************************************************************************
484 * ME_CharFromPoint
486 * Returns a character position inside the run given a run-relative
487 * pixel horizontal position. This version rounds left (ie. if the second
488 * character is at pixel position 8, then for cx=0..7 it returns 0).
490 int ME_CharFromPoint(ME_Context *c, int cx, ME_Run *run)
492 int fit = 0;
493 HGDIOBJ hOldFont;
494 SIZE sz;
495 if (!run->strText->nLen)
496 return 0;
498 if (run->nFlags & (MERF_TAB | MERF_CELL))
500 if (cx < run->nWidth/2)
501 return 0;
502 return 1;
504 if (run->nFlags & MERF_GRAPHICS)
506 SIZE sz;
507 ME_GetGraphicsSize(c->editor, run, &sz);
508 if (cx < sz.cx)
509 return 0;
510 return 1;
512 hOldFont = ME_SelectStyleFont(c, run->style);
514 if (c->editor->cPasswordMask)
516 ME_String *strMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
517 GetTextExtentExPointW(c->hDC, strMasked->szData, run->strText->nLen,
518 cx, &fit, NULL, &sz);
519 ME_DestroyString(strMasked);
521 else
523 GetTextExtentExPointW(c->hDC, run->strText->szData, run->strText->nLen,
524 cx, &fit, NULL, &sz);
527 ME_UnselectStyleFont(c, run->style, hOldFont);
529 return fit;
532 /******************************************************************************
533 * ME_CharFromPointCursor
535 * Returns a character position inside the run given a run-relative
536 * pixel horizontal position. This version rounds to the nearest character edge
537 * (ie. if the second character is at pixel position 8, then for cx=0..3
538 * it returns 0, and for cx=4..7 it returns 1).
540 * It is used for mouse click handling, for better usability (and compatibility
541 * with the native control).
543 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
545 ME_String *strRunText;
546 /* This could point to either the run's real text, or it's masked form in a password control */
548 int fit = 0, fit1 = 0;
549 ME_Context c;
550 HGDIOBJ hOldFont;
551 SIZE sz, sz2, sz3;
552 if (!run->strText->nLen)
553 return 0;
555 if (run->nFlags & (MERF_TAB | MERF_CELL))
557 if (cx < run->nWidth/2)
558 return 0;
559 return 1;
561 if (run->nFlags & MERF_GRAPHICS)
563 SIZE sz;
564 ME_GetGraphicsSize(editor, run, &sz);
565 if (cx < sz.cx/2)
566 return 0;
567 return 1;
570 if (editor->cPasswordMask)
571 strRunText = ME_MakeStringR(editor->cPasswordMask,ME_StrVLen(run->strText));
572 else
573 strRunText = run->strText;
575 ME_InitContext(&c, editor, GetDC(editor->hWnd));
576 hOldFont = ME_SelectStyleFont(&c, run->style);
577 GetTextExtentExPointW(c.hDC, strRunText->szData, strRunText->nLen,
578 cx, &fit, NULL, &sz);
579 if (fit != strRunText->nLen)
581 int chars = 1;
583 GetTextExtentPoint32W(c.hDC, strRunText->szData, fit, &sz2);
584 fit1 = ME_StrRelPos(strRunText, fit, &chars);
585 GetTextExtentPoint32W(c.hDC, strRunText->szData, fit1, &sz3);
586 if (cx >= (sz2.cx+sz3.cx)/2)
587 fit = fit1;
590 if (editor->cPasswordMask)
591 ME_DestroyString(strRunText);
593 ME_UnselectStyleFont(&c, run->style, hOldFont);
594 ReleaseDC(editor->hWnd, c.hDC);
595 return fit;
598 /******************************************************************************
599 * ME_GetTextExtent
601 * Finds a width and a height of the text using a specified style
603 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
605 HGDIOBJ hOldFont;
606 hOldFont = ME_SelectStyleFont(c, s);
607 GetTextExtentPoint32W(c->hDC, szText, nChars, size);
608 ME_UnselectStyleFont(c, s, hOldFont);
611 /******************************************************************************
612 * ME_PointFromChar
614 * Returns a run-relative pixel position given a run-relative character
615 * position (character offset)
617 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
619 SIZE size;
620 ME_Context c;
621 ME_String *strRunText;
622 /* This could point to either the run's real text, or it's masked form in a password control */
624 if (pRun->nFlags & MERF_GRAPHICS)
626 if (!nOffset) return 0;
627 ME_GetGraphicsSize(editor, pRun, &size);
628 return 1;
631 if (editor->cPasswordMask)
632 strRunText = ME_MakeStringR(editor->cPasswordMask,ME_StrVLen(pRun->strText));
633 else
634 strRunText = pRun->strText;
636 ME_InitContext(&c, editor, GetDC(editor->hWnd));
637 ME_GetTextExtent(&c, strRunText->szData, nOffset, pRun->style, &size);
638 ReleaseDC(editor->hWnd, c.hDC);
639 if (editor->cPasswordMask)
640 ME_DestroyString(strRunText);
641 return size.cx;
644 /******************************************************************************
645 * ME_GetRunSizeCommon
647 * Finds width, height, ascent and descent of a run, up to given character
648 * (nLen).
650 static SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
651 int *pAscent, int *pDescent)
653 SIZE size;
654 int nMaxLen = ME_StrVLen(run->strText);
656 if (nLen>nMaxLen)
657 nLen = nMaxLen;
659 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
660 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
661 * in practice
664 if (c->editor->cPasswordMask)
666 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
667 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
668 ME_DestroyString(szMasked);
670 else
672 ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
674 *pAscent = run->style->tm.tmAscent;
675 *pDescent = run->style->tm.tmDescent;
676 size.cy = *pAscent + *pDescent;
678 if (run->nFlags & MERF_TAB)
680 int pos = 0, i = 0, ppos;
682 PARAFORMAT2 *pFmt = para->pFmt;
683 do {
684 if (i < pFmt->cTabCount)
686 pos = pFmt->rgxTabs[i]&0x00FFFFFF;
687 i++;
689 else
691 pos += 720-(pos%720);
693 ppos = ME_twips2pointsX(c, pos);
694 if (ppos>run->pt.x) {
695 size.cx = ppos - run->pt.x;
696 break;
698 } while(1);
699 size.cy = *pAscent + *pDescent;
700 return size;
702 if (run->nFlags & MERF_GRAPHICS)
704 ME_GetGraphicsSize(c->editor, run, &size);
705 if (size.cy > *pAscent)
706 *pAscent = size.cy;
707 /* descent is unchanged */
708 return size;
710 if (run->nFlags & MERF_CELL)
712 size.cx = ME_twips2pointsX(c, run->pCell->nRightBoundary) - run->pt.x;
713 return size;
715 return size;
718 /******************************************************************************
719 * ME_GetRunSize
721 * Finds width and height (but not ascent and descent) of a part of the run
722 * up to given character.
724 SIZE ME_GetRunSize(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen)
726 int asc, desc;
727 return ME_GetRunSizeCommon(c, para, run, nLen, &asc, &desc);
730 /******************************************************************************
731 * ME_CalcRunExtent
733 * Updates the size of the run (fills width, ascent and descent). The height
734 * is calculated based on whole row's ascent and descent anyway, so no need
735 * to use it here.
737 void ME_CalcRunExtent(ME_Context *c, const ME_Paragraph *para, ME_Run *run)
739 if (run->nFlags & MERF_HIDDEN)
740 run->nWidth = 0;
741 else
743 int nEnd = ME_StrVLen(run->strText);
744 SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, &run->nAscent, &run->nDescent);
745 run->nWidth = size.cx;
746 if (!size.cx)
747 WARN("size.cx == 0\n");
751 /******************************************************************************
752 * ME_MustBeWrapped
754 * This should ensure that the given paragraph is wrapped so that its screen
755 * row structure may be used. But it doesn't, yet.
757 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
759 assert(para->type == diParagraph);
760 /* FIXME */
763 /******************************************************************************
764 * ME_SetSelectionCharFormat
766 * Applies a style change, either to a current selection, or to insert cursor
767 * (ie. the style next typed characters will use).
769 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
771 int nFrom, nTo;
772 ME_GetSelection(editor, &nFrom, &nTo);
773 if (nFrom == nTo)
775 ME_Style *s;
776 if (!editor->pBuffer->pCharStyle)
777 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
778 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
779 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
780 editor->pBuffer->pCharStyle = s;
782 else
783 ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
786 /******************************************************************************
787 * ME_SetCharFormat
789 * Applies a style change to the specified part of the text
791 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
793 ME_Cursor tmp, tmp2;
794 ME_DisplayItem *para;
796 ME_CursorFromCharOfs(editor, nOfs, &tmp);
797 if (tmp.nOffset)
798 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
800 ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
801 if (tmp2.nOffset)
802 tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
804 para = ME_GetParagraph(tmp.pRun);
805 para->member.para.nFlags |= MEPF_REWRAP;
807 while(tmp.pRun != tmp2.pRun)
809 ME_UndoItem *undo = NULL;
810 ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
811 /* ME_DumpStyle(new_style); */
812 undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
813 if (undo) {
814 undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
815 undo->nLen = tmp.pRun->member.run.strText->nLen;
816 undo->di.member.ustyle = tmp.pRun->member.run.style;
817 /* we'd have to addref undo..ustyle and release tmp...style
818 but they'd cancel each other out so we can do nothing instead */
820 else
821 ME_ReleaseStyle(tmp.pRun->member.run.style);
822 tmp.pRun->member.run.style = new_style;
823 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
824 if (tmp.pRun->type == diParagraph)
826 para = tmp.pRun;
827 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
828 if (tmp.pRun != tmp2.pRun)
829 para->member.para.nFlags |= MEPF_REWRAP;
831 assert(tmp.pRun);
835 /******************************************************************************
836 * ME_SetDefaultCharFormat
838 * Applies a style change to the default character style.
840 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
842 ME_Style *style;
843 ME_UndoItem *undo;
845 assert(mod->cbSize == sizeof(CHARFORMAT2W));
846 undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
847 if (undo) {
848 undo->nStart = -1;
849 undo->nLen = -1;
850 undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
851 ME_AddRefStyle(undo->di.member.ustyle);
853 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
854 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
855 editor->pBuffer->pDefaultStyle->tm = style->tm;
856 ME_ReleaseStyle(style);
857 ME_MarkAllForWrapping(editor);
858 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
861 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
863 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
866 /******************************************************************************
867 * ME_GetDefaultCharFormat
869 * Retrieves the current default character style (the one applied where no
870 * other style was applied) .
872 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
874 int nFrom, nTo;
875 ME_GetSelection(editor, &nFrom, &nTo);
876 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
879 /******************************************************************************
880 * ME_GetSelectionCharFormat
882 * If selection exists, it returns all style elements that are set consistently
883 * in the whole selection. If not, it just returns the current style.
885 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
887 int nFrom, nTo;
888 ME_GetSelection(editor, &nFrom, &nTo);
889 if (nFrom == nTo && editor->pBuffer->pCharStyle)
891 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
892 return;
894 ME_GetCharFormat(editor, nFrom, nTo, pFmt);
897 /******************************************************************************
898 * ME_GetCharFormat
900 * Returns the style consisting of those attributes which are consistently set
901 * in the whole character range.
903 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
905 ME_DisplayItem *run, *run_end;
906 int nOffset, nOffset2;
907 CHARFORMAT2W tmp;
909 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
910 if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
912 if (!nOffset)
914 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
915 if (tmp_run->type == diRun) {
916 ME_GetRunCharFormat(editor, tmp_run, pFmt);
917 return;
920 ME_GetRunCharFormat(editor, run, pFmt);
921 return;
924 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
925 nTo--;
926 ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
928 ME_GetRunCharFormat(editor, run, pFmt);
930 if (run == run_end) return;
932 do {
933 /* FIXME add more style feature comparisons */
934 int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
935 int nEffects = CFM_BOLD | CFM_ITALIC;
937 run = ME_FindItemFwd(run, diRun);
939 ZeroMemory(&tmp, sizeof(tmp));
940 tmp.cbSize = sizeof(tmp);
941 ME_GetRunCharFormat(editor, run, &tmp);
943 assert((tmp.dwMask & nAttribs) == nAttribs);
944 assert((tmp.dwMask & nEffects) == nEffects);
945 /* reset flags that differ */
947 if (pFmt->yHeight != tmp.yHeight)
948 pFmt->dwMask &= ~CFM_SIZE;
949 if (pFmt->dwMask & CFM_FACE)
951 if (!(tmp.dwMask & CFM_FACE))
952 pFmt->dwMask &= ~CFM_FACE;
953 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
954 pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
955 pFmt->dwMask &= ~CFM_FACE;
957 if (pFmt->yHeight != tmp.yHeight)
958 pFmt->dwMask &= ~CFM_SIZE;
959 if (pFmt->bUnderlineType != tmp.bUnderlineType)
960 pFmt->dwMask &= ~CFM_UNDERLINETYPE;
961 if (pFmt->dwMask & CFM_COLOR)
963 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
965 if (pFmt->crTextColor != tmp.crTextColor)
966 pFmt->dwMask &= ~CFM_COLOR;
970 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
972 } while(run != run_end);