richedit: Use tabstops to store cell positions.
[wine/multimedia.git] / dlls / riched20 / run.c
blobe7ce035980c7f879d366a949c3195f0b0cd7d04b
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 assert(p->member.run.nCR + p->member.run.nLF > 0);
132 ofs += p->member.run.nCR + p->member.run.nLF;
134 else
135 ofs += ME_StrLen(p->member.run.strText);
136 break;
137 default:
138 assert(0);
140 } while(1);
143 /******************************************************************************
144 * ME_CharOfsFromRunOfs
146 * Converts a character position relative to the start of the run, to a
147 * character position relative to the start of the document.
148 * Kind of a "local to global" offset conversion.
150 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
152 ME_DisplayItem *pPara;
154 assert(pRun->type == diRun);
155 assert(pRun->member.run.nCharOfs != -1);
157 pPara = ME_FindItemBack(pRun, diParagraph);
158 assert(pPara);
159 assert(pPara->type==diParagraph);
160 return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs
161 + ME_VPosToPos(pRun->member.run.strText, nOfs);
164 /******************************************************************************
165 * ME_CursorFromCharOfs
167 * Converts a character offset (relative to the start of the document) to
168 * a cursor structure (which contains a run and a position relative to that
169 * run).
171 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
173 ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
176 /******************************************************************************
177 * ME_RunOfsFromCharOfs
179 * Find a run and relative character offset given an absolute character offset
180 * (absolute offset being an offset relative to the start of the document).
181 * Kind of a "global to local" offset conversion.
183 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
185 ME_DisplayItem *pPara;
186 int nParaOfs;
188 pPara = editor->pBuffer->pFirst->member.para.next_para;
189 assert(pPara);
190 assert(ppRun);
191 assert(pOfs);
192 while (pPara->type == diParagraph)
194 nParaOfs = pPara->member.para.nCharOfs;
195 assert(nCharOfs >= nParaOfs);
197 if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
199 int eollen = 1;
200 *ppRun = ME_FindItemFwd(pPara, diRun);
201 assert(*ppRun);
202 while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
204 ME_DisplayItem *pNext = ME_FindItemFwd(*ppRun, diRun);
205 assert(pNext);
206 assert(pNext->type == diRun);
207 if (nCharOfs < nParaOfs + pNext->member.run.nCharOfs) {
208 *pOfs = ME_PosToVPos((*ppRun)->member.run.strText,
209 nCharOfs - nParaOfs - (*ppRun)->member.run.nCharOfs);
210 return;
212 *ppRun = pNext;
214 /* Recover proper character length of this line break */
215 eollen = (*ppRun)->member.run.nCR + (*ppRun)->member.run.nLF;
216 if (nCharOfs >= nParaOfs + (*ppRun)->member.run.nCharOfs &&
217 nCharOfs < nParaOfs + (*ppRun)->member.run.nCharOfs + eollen) {
218 /* FIXME: Might cause problems when actually requiring an offset in the
219 middle of a run that is considered a single line break */
220 *pOfs = 0;
221 return;
224 pPara = pPara->member.para.next_para;
226 *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
227 *pOfs = 0;
228 assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
231 /******************************************************************************
232 * ME_JoinRuns
234 * Merges two adjacent runs, the one given as a parameter and the next one.
236 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
238 ME_DisplayItem *pNext = p->next;
239 int i;
240 assert(p->type == diRun && pNext->type == diRun);
241 assert(p->member.run.nCharOfs != -1);
242 ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
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_WrapContext *wc, ME_DisplayItem *item, int nVChar)
272 ME_TextEditor *editor = wc->context->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(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
295 ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, 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.ole_obj = NULL;
363 item->member.run.strText = strData;
364 item->member.run.nFlags = nFlags;
365 item->member.run.nCharOfs = -1;
366 ME_AddRefStyle(s);
367 return item;
370 /******************************************************************************
371 * ME_InsertRun
373 * Inserts a run at a given character position (offset).
375 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
377 ME_Cursor tmp;
378 ME_DisplayItem *pDI;
380 assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
382 ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
383 pDI = ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
384 pItem->member.run.strText->szData,
385 pItem->member.run.strText->nLen,
386 pItem->member.run.nFlags);
388 return pDI;
391 /******************************************************************************
392 * ME_InsertRunAtCursor
394 * Inserts a new run with given style, flags and content at a given position,
395 * which is passed as a cursor structure (which consists of a run and
396 * a run-relative character offset).
398 ME_DisplayItem *
399 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
400 const WCHAR *str, int len, int flags)
402 ME_DisplayItem *pDI;
403 ME_UndoItem *pUI;
405 if (cursor->nOffset) {
406 /* We're inserting at the middle of the existing run, which means that
407 * that run must be split. It isn't always necessary, but */
408 cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
409 cursor->nOffset = 0;
412 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
413 if (pUI) {
414 pUI->nStart = (ME_GetParagraph(cursor->pRun)->member.para.nCharOfs
415 + cursor->pRun->member.run.nCharOfs);
416 pUI->nLen = len;
419 pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
420 pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
421 ME_InsertBefore(cursor->pRun, pDI);
422 TRACE("Shift length:%d\n", len);
423 ME_PropagateCharOffset(cursor->pRun, len);
424 ME_GetParagraph(cursor->pRun)->member.para.nFlags |= MEPF_REWRAP;
425 return pDI;
428 /******************************************************************************
429 * ME_UpdateRunFlags
431 * Determine some of run attributes given its content (style, text content).
432 * Some flags cannot be determined by this function (MERF_GRAPHICS,
433 * MERF_ENDPARA)
435 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
437 assert(run->nCharOfs != -1);
439 if (RUN_IS_HIDDEN(run))
440 run->nFlags |= MERF_HIDDEN;
441 else
442 run->nFlags &= ~MERF_HIDDEN;
444 if (ME_IsSplitable(run->strText))
445 run->nFlags |= MERF_SPLITTABLE;
446 else
447 run->nFlags &= ~MERF_SPLITTABLE;
449 if (!(run->nFlags & MERF_NOTEXT)) {
450 if (ME_IsWhitespaces(run->strText))
451 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
452 else
454 run->nFlags &= ~MERF_WHITESPACE;
456 if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
457 run->nFlags |= MERF_STARTWHITE;
458 else
459 run->nFlags &= ~MERF_STARTWHITE;
461 if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
462 run->nFlags |= MERF_ENDWHITE;
463 else
464 run->nFlags &= ~MERF_ENDWHITE;
467 else
468 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
471 /******************************************************************************
472 * ME_CharFromPoint
474 * Returns a character position inside the run given a run-relative
475 * pixel horizontal position. This version rounds left (ie. if the second
476 * character is at pixel position 8, then for cx=0..7 it returns 0).
478 int ME_CharFromPoint(ME_Context *c, int cx, ME_Run *run)
480 int fit = 0;
481 HGDIOBJ hOldFont;
482 SIZE sz;
483 if (!run->strText->nLen)
484 return 0;
486 if (run->nFlags & MERF_TAB)
488 if (cx < run->nWidth/2)
489 return 0;
490 return 1;
492 if (run->nFlags & MERF_GRAPHICS)
494 SIZE sz;
495 ME_GetOLEObjectSize(c, run, &sz);
496 if (cx < sz.cx)
497 return 0;
498 return 1;
500 hOldFont = ME_SelectStyleFont(c, run->style);
502 if (c->editor->cPasswordMask)
504 ME_String *strMasked = ME_MakeStringR(c->editor->cPasswordMask,ME_StrVLen(run->strText));
505 GetTextExtentExPointW(c->hDC, strMasked->szData, run->strText->nLen,
506 cx, &fit, NULL, &sz);
507 ME_DestroyString(strMasked);
509 else
511 GetTextExtentExPointW(c->hDC, run->strText->szData, run->strText->nLen,
512 cx, &fit, NULL, &sz);
515 ME_UnselectStyleFont(c, run->style, hOldFont);
517 return fit;
520 /******************************************************************************
521 * ME_CharFromPointCursor
523 * Returns a character position inside the run given a run-relative
524 * pixel horizontal position. This version rounds to the nearest character edge
525 * (ie. if the second character is at pixel position 8, then for cx=0..3
526 * it returns 0, and for cx=4..7 it returns 1).
528 * It is used for mouse click handling, for better usability (and compatibility
529 * with the native control).
531 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
533 ME_String *strRunText;
534 /* This could point to either the run's real text, or it's masked form in a password control */
536 int fit = 0, fit1 = 0;
537 ME_Context c;
538 HGDIOBJ hOldFont;
539 SIZE sz, sz2, sz3;
540 if (!run->strText->nLen)
541 return 0;
543 if (run->nFlags & MERF_TAB)
545 if (cx < run->nWidth/2)
546 return 0;
547 return 1;
549 ME_InitContext(&c, editor, GetDC(editor->hWnd));
550 if (run->nFlags & MERF_GRAPHICS)
552 SIZE sz;
553 ME_GetOLEObjectSize(&c, run, &sz);
554 ME_DestroyContext(&c, editor->hWnd);
555 if (cx < sz.cx/2)
556 return 0;
557 return 1;
560 if (editor->cPasswordMask)
561 strRunText = ME_MakeStringR(editor->cPasswordMask,ME_StrVLen(run->strText));
562 else
563 strRunText = run->strText;
565 hOldFont = ME_SelectStyleFont(&c, run->style);
566 GetTextExtentExPointW(c.hDC, strRunText->szData, strRunText->nLen,
567 cx, &fit, NULL, &sz);
568 if (fit != strRunText->nLen)
570 int chars = 1;
572 GetTextExtentPoint32W(c.hDC, strRunText->szData, fit, &sz2);
573 fit1 = ME_StrRelPos(strRunText, fit, &chars);
574 GetTextExtentPoint32W(c.hDC, strRunText->szData, fit1, &sz3);
575 if (cx >= (sz2.cx+sz3.cx)/2)
576 fit = fit1;
579 if (editor->cPasswordMask)
580 ME_DestroyString(strRunText);
582 ME_UnselectStyleFont(&c, run->style, hOldFont);
583 ME_DestroyContext(&c, editor->hWnd);
584 return fit;
587 /******************************************************************************
588 * ME_GetTextExtent
590 * Finds a width and a height of the text using a specified style
592 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
594 HGDIOBJ hOldFont;
595 hOldFont = ME_SelectStyleFont(c, s);
596 GetTextExtentPoint32W(c->hDC, szText, nChars, size);
597 ME_UnselectStyleFont(c, s, hOldFont);
600 /******************************************************************************
601 * ME_PointFromChar
603 * Returns a run-relative pixel position given a run-relative character
604 * position (character offset)
606 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
608 SIZE size;
609 ME_Context c;
610 ME_String *strRunText;
611 /* This could point to either the run's real text, or it's masked form in a password control */
613 ME_InitContext(&c, editor, GetDC(editor->hWnd));
614 if (pRun->nFlags & MERF_GRAPHICS)
616 if (nOffset)
617 ME_GetOLEObjectSize(&c, pRun, &size);
618 ReleaseDC(editor->hWnd, c.hDC);
619 return nOffset != 0;
622 if (editor->cPasswordMask)
623 strRunText = ME_MakeStringR(editor->cPasswordMask,ME_StrVLen(pRun->strText));
624 else
625 strRunText = pRun->strText;
627 ME_GetTextExtent(&c, strRunText->szData, nOffset, pRun->style, &size);
628 ReleaseDC(editor->hWnd, c.hDC);
629 if (editor->cPasswordMask)
630 ME_DestroyString(strRunText);
631 return size.cx;
634 /******************************************************************************
635 * ME_GetRunSizeCommon
637 * Finds width, height, ascent and descent of a run, up to given character
638 * (nLen).
640 static SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
641 int startx, int *pAscent, int *pDescent)
643 SIZE size;
644 int nMaxLen = ME_StrVLen(run->strText);
646 if (nLen>nMaxLen)
647 nLen = nMaxLen;
649 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
650 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
651 * in practice
654 if (c->editor->cPasswordMask)
656 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
657 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
658 ME_DestroyString(szMasked);
660 else
662 ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
664 *pAscent = run->style->tm.tmAscent;
665 *pDescent = run->style->tm.tmDescent;
666 size.cy = *pAscent + *pDescent;
668 if (run->nFlags & MERF_TAB)
670 int pos = 0, i = 0, ppos;
671 PARAFORMAT2 *pFmt = para->pFmt;
673 do {
674 if (i < pFmt->cTabCount)
676 pos = pFmt->rgxTabs[i]&0x00FFFFFF;
677 i++;
679 else
681 pos += lDefaultTab - (pos % lDefaultTab);
683 ppos = ME_twips2pointsX(c, pos) + c->editor->selofs;
684 if (ppos > startx + run->pt.x) {
685 size.cx = ppos - startx - run->pt.x;
686 break;
688 } while(1);
689 size.cy = *pAscent + *pDescent;
690 return size;
692 if (run->nFlags & MERF_GRAPHICS)
694 ME_GetOLEObjectSize(c, run, &size);
695 if (size.cy > *pAscent)
696 *pAscent = size.cy;
697 /* descent is unchanged */
698 return size;
700 return size;
703 /******************************************************************************
704 * ME_GetRunSize
706 * Finds width and height (but not ascent and descent) of a part of the run
707 * up to given character.
709 SIZE ME_GetRunSize(ME_Context *c, const ME_Paragraph *para,
710 ME_Run *run, int nLen, int startx)
712 int asc, desc;
713 return ME_GetRunSizeCommon(c, para, run, nLen, startx, &asc, &desc);
716 /******************************************************************************
717 * ME_CalcRunExtent
719 * Updates the size of the run (fills width, ascent and descent). The height
720 * is calculated based on whole row's ascent and descent anyway, so no need
721 * to use it here.
723 void ME_CalcRunExtent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
725 if (run->nFlags & MERF_HIDDEN)
726 run->nWidth = 0;
727 else
729 int nEnd = ME_StrVLen(run->strText);
730 SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, startx,
731 &run->nAscent, &run->nDescent);
732 run->nWidth = size.cx;
733 if (!size.cx)
734 WARN("size.cx == 0\n");
738 /******************************************************************************
739 * ME_MustBeWrapped
741 * This should ensure that the given paragraph is wrapped so that its screen
742 * row structure may be used. But it doesn't, yet.
744 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
746 assert(para->type == diParagraph);
747 /* FIXME */
750 /******************************************************************************
751 * ME_SetSelectionCharFormat
753 * Applies a style change, either to a current selection, or to insert cursor
754 * (ie. the style next typed characters will use).
756 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
758 int nFrom, nTo;
759 ME_GetSelection(editor, &nFrom, &nTo);
760 if (nFrom == nTo)
762 ME_Style *s;
763 if (!editor->pBuffer->pCharStyle)
764 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
765 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
766 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
767 editor->pBuffer->pCharStyle = s;
769 else
770 ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
773 /******************************************************************************
774 * ME_SetCharFormat
776 * Applies a style change to the specified part of the text
778 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
780 ME_Cursor tmp, tmp2;
781 ME_DisplayItem *para;
783 ME_CursorFromCharOfs(editor, nOfs, &tmp);
784 if (tmp.nOffset)
785 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
787 ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
788 if (tmp2.nOffset)
789 tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
791 para = ME_GetParagraph(tmp.pRun);
792 para->member.para.nFlags |= MEPF_REWRAP;
794 while(tmp.pRun != tmp2.pRun)
796 ME_UndoItem *undo = NULL;
797 ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
798 /* ME_DumpStyle(new_style); */
799 undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
800 if (undo) {
801 undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
802 undo->nLen = tmp.pRun->member.run.strText->nLen;
803 undo->di.member.ustyle = tmp.pRun->member.run.style;
804 /* we'd have to addref undo..ustyle and release tmp...style
805 but they'd cancel each other out so we can do nothing instead */
807 else
808 ME_ReleaseStyle(tmp.pRun->member.run.style);
809 tmp.pRun->member.run.style = new_style;
810 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
811 if (tmp.pRun->type == diParagraph)
813 para = tmp.pRun;
814 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
815 if (tmp.pRun != tmp2.pRun)
816 para->member.para.nFlags |= MEPF_REWRAP;
818 assert(tmp.pRun);
822 /******************************************************************************
823 * ME_SetDefaultCharFormat
825 * Applies a style change to the default character style.
827 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
829 ME_Style *style;
831 assert(mod->cbSize == sizeof(CHARFORMAT2W));
832 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
833 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
834 editor->pBuffer->pDefaultStyle->tm = style->tm;
835 ME_ReleaseStyle(style);
836 ME_MarkAllForWrapping(editor);
837 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
840 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
842 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
843 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_CF1UNDERLINE))
845 pFmt->dwMask |= CFM_UNDERLINE;
846 pFmt->dwEffects |= CFE_UNDERLINE;
848 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_UNDERLINENONE))
850 pFmt->dwMask |= CFM_UNDERLINE;
851 pFmt->dwEffects &= ~CFE_UNDERLINE;
855 /******************************************************************************
856 * ME_GetDefaultCharFormat
858 * Retrieves the current default character style (the one applied where no
859 * other style was applied) .
861 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
863 int nFrom, nTo;
864 ME_GetSelection(editor, &nFrom, &nTo);
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 int nFrom, nTo;
877 ME_GetSelection(editor, &nFrom, &nTo);
878 if (nFrom == nTo && editor->pBuffer->pCharStyle)
880 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
881 return;
883 ME_GetCharFormat(editor, nFrom, nTo, pFmt);
886 /******************************************************************************
887 * ME_GetCharFormat
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, int nFrom, int nTo, CHARFORMAT2W *pFmt)
894 ME_DisplayItem *run, *run_end;
895 int nOffset, nOffset2;
896 CHARFORMAT2W tmp;
898 ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
899 if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
901 if (!nOffset)
903 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
904 if (tmp_run->type == diRun) {
905 ME_GetRunCharFormat(editor, tmp_run, pFmt);
906 return;
909 ME_GetRunCharFormat(editor, run, pFmt);
910 return;
913 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
914 nTo--;
915 ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
917 ME_GetRunCharFormat(editor, run, pFmt);
919 if (run == run_end) return;
921 do {
922 /* FIXME add more style feature comparisons */
923 int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
924 int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
926 run = ME_FindItemFwd(run, diRun);
928 ZeroMemory(&tmp, sizeof(tmp));
929 tmp.cbSize = sizeof(tmp);
930 ME_GetRunCharFormat(editor, run, &tmp);
932 assert((tmp.dwMask & nAttribs) == nAttribs);
933 /* reset flags that differ */
935 if (pFmt->yHeight != tmp.yHeight)
936 pFmt->dwMask &= ~CFM_SIZE;
937 if (pFmt->dwMask & CFM_FACE)
939 if (!(tmp.dwMask & CFM_FACE))
940 pFmt->dwMask &= ~CFM_FACE;
941 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
942 pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
943 pFmt->dwMask &= ~CFM_FACE;
945 if (pFmt->yHeight != tmp.yHeight)
946 pFmt->dwMask &= ~CFM_SIZE;
947 if (pFmt->bUnderlineType != tmp.bUnderlineType)
948 pFmt->dwMask &= ~CFM_UNDERLINETYPE;
949 if (pFmt->dwMask & CFM_COLOR)
951 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
953 if (pFmt->crTextColor != tmp.crTextColor)
954 pFmt->dwMask &= ~CFM_COLOR;
958 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
959 pFmt->dwEffects = tmp.dwEffects;
961 } while(run != run_end);