sync with trunk (r46275)
[reactos.git] / dll / win32 / riched20 / run.c
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.
5 *
6 * Copyright 2004 by Krzysztof Foltman
7 * Copyright 2006 by Phil Krylov
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "editor.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
28 WINE_DECLARE_DEBUG_CHANNEL(richedit_lists);
29
30 /******************************************************************************
31 * ME_CanJoinRuns
32 *
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)
36 {
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;
44 }
45
46 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
47 {
48 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
49 assert(p);
50 ME_PropagateCharOffset(p, shift);
51 }
52
53 /******************************************************************************
54 * ME_PropagateCharOffsets
55 *
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)
60 {
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 */
66 {
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);
73 }
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 */
79 {
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);
85 }
86 /* diTextEnd also has character offset in it, which makes finding text length
87 * easier. But it needs to be up to date first.
88 */
89 if (p->type == diTextEnd)
90 {
91 p->member.para.nCharOfs += shift;
92 assert(p->member.para.nCharOfs >= 0);
93 }
94 }
95
96 /******************************************************************************
97 * ME_CheckCharOffsets
98 *
99 * Checks if editor lists' validity and optionally dumps the document structure
100 */
101 void ME_CheckCharOffsets(ME_TextEditor *editor)
102 {
103 ME_DisplayItem *p = editor->pBuffer->pFirst;
104 int ofs = 0, ofsp = 0;
105 if(TRACE_ON(richedit_lists))
106 {
107 TRACE_(richedit_lists)("---\n");
108 ME_DumpDocument(editor->pBuffer);
109 }
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 assert(p->member.run.strText->nLen);
131 ofs += p->member.run.strText->nLen;
132 break;
133 case diCell:
134 TRACE_(richedit_check)("cell\n");
135 break;
136 default:
137 assert(0);
138 }
139 } while(1);
140 }
141
142 /******************************************************************************
143 * ME_CharOfsFromRunOfs
144 *
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.
148 */
149 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara,
150 const ME_DisplayItem *pRun, int nOfs)
151 {
152 assert(pRun && pRun->type == diRun);
153 assert(pPara && pPara->type == diParagraph);
154 return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs + nOfs;
155 }
156
157 /******************************************************************************
158 * ME_CursorFromCharOfs
159 *
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).
163 */
164 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
165 {
166 ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pPara,
167 &pCursor->pRun, &pCursor->nOffset);
168 }
169
170 /******************************************************************************
171 * ME_RunOfsFromCharOfs
172 *
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.
176 */
177 void ME_RunOfsFromCharOfs(ME_TextEditor *editor,
178 int nCharOfs,
179 ME_DisplayItem **ppPara,
180 ME_DisplayItem **ppRun,
181 int *pOfs)
182 {
183 ME_DisplayItem *item, *next_item;
184
185 nCharOfs = max(nCharOfs, 0);
186 nCharOfs = min(nCharOfs, ME_GetTextLength(editor));
187
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;
197
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;
207
208 if (ppRun) *ppRun = item;
209 if (pOfs) *pOfs = nCharOfs;
210 }
211
212 /******************************************************************************
213 * ME_JoinRuns
214 *
215 * Merges two adjacent runs, the one given as a parameter and the next one.
216 */
217 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
218 {
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;
224
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;
230 }
231 }
232
233 ME_AppendString(p->member.run.strText, pNext->member.run.strText);
234 ME_Remove(pNext);
235 ME_DestroyDisplayItem(pNext);
236 ME_UpdateRunFlags(editor, &p->member.run);
237 if(TRACE_ON(richedit))
238 {
239 TRACE("Before check after join\n");
240 ME_CheckCharOffsets(editor);
241 TRACE("After check after join\n");
242 }
243 }
244
245 /******************************************************************************
246 * ME_SplitRun
247 *
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.
250 */
251 ME_DisplayItem *ME_SplitRun(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
252 {
253 ME_TextEditor *editor = wc->context->editor;
254 ME_DisplayItem *item2 = NULL;
255 ME_Run *run, *run2;
256 ME_Paragraph *para = &ME_GetParagraph(item)->member.para;
257
258 assert(item->member.run.nCharOfs != -1);
259 if(TRACE_ON(richedit))
260 {
261 TRACE("Before check before split\n");
262 ME_CheckCharOffsets(editor);
263 TRACE("After check before split\n");
264 }
265
266 run = &item->member.run;
267
268 TRACE("Before split: %s(%d, %d)\n", debugstr_w(run->strText->szData),
269 run->pt.x, run->pt.y);
270
271 item2 = ME_SplitRunSimple(editor, item, nVChar);
272
273 run2 = &item2->member.run;
274
275 ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
276 ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run2);
277
278 run2->pt.x = run->pt.x+run->nWidth;
279 run2->pt.y = run->pt.y;
280
281 if(TRACE_ON(richedit))
282 {
283 TRACE("Before check after split\n");
284 ME_CheckCharOffsets(editor);
285 TRACE("After check after split\n");
286 TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
287 debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
288 debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
289 }
290
291 return item2;
292 }
293
294 /******************************************************************************
295 * ME_SplitRunSimple
296 *
297 * Does the most basic job of splitting a run into two - it does not
298 * update the positions and extents.
299 */
300 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
301 {
302 ME_Run *run = &item->member.run;
303 ME_DisplayItem *item2;
304 ME_Run *run2;
305 int i;
306 assert(nVChar > 0 && nVChar < run->strText->nLen);
307 assert(item->type == diRun);
308 assert(!(item->member.run.nFlags & MERF_NONTEXT));
309 assert(item->member.run.nCharOfs != -1);
310
311 item2 = ME_MakeRun(run->style,
312 ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
313
314 item2->member.run.nCharOfs = item->member.run.nCharOfs + nVChar;
315
316 run2 = &item2->member.run;
317 ME_InsertBefore(item->next, item2);
318
319 ME_UpdateRunFlags(editor, run);
320 ME_UpdateRunFlags(editor, run2);
321 for (i=0; i<editor->nCursors; i++) {
322 if (editor->pCursors[i].pRun == item &&
323 editor->pCursors[i].nOffset >= nVChar) {
324 assert(item2->type == diRun);
325 editor->pCursors[i].pRun = item2;
326 editor->pCursors[i].nOffset -= nVChar;
327 }
328 }
329 ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
330 return item2;
331 }
332
333 /******************************************************************************
334 * ME_MakeRun
335 *
336 * A helper function to create run structures quickly.
337 */
338 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
339 {
340 ME_DisplayItem *item = ME_MakeDI(diRun);
341 item->member.run.style = s;
342 item->member.run.ole_obj = NULL;
343 item->member.run.strText = strData;
344 item->member.run.nFlags = nFlags;
345 item->member.run.nCharOfs = -1;
346 ME_AddRefStyle(s);
347 return item;
348 }
349
350 /******************************************************************************
351 * ME_InsertRun
352 *
353 * Inserts a run at a given character position (offset).
354 */
355 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
356 {
357 ME_Cursor tmp;
358 ME_DisplayItem *pDI;
359
360 assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
361
362 ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
363 pDI = ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
364 pItem->member.run.strText->szData,
365 pItem->member.run.strText->nLen,
366 pItem->member.run.nFlags);
367
368 return pDI;
369 }
370
371 /******************************************************************************
372 * ME_InsertRunAtCursor
373 *
374 * Inserts a new run with given style, flags and content at a given position,
375 * which is passed as a cursor structure (which consists of a run and
376 * a run-relative character offset).
377 */
378 ME_DisplayItem *
379 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
380 const WCHAR *str, int len, int flags)
381 {
382 ME_DisplayItem *pDI;
383 ME_UndoItem *pUI;
384
385 if (cursor->nOffset) {
386 /* We're inserting at the middle of the existing run, which means that
387 * that run must be split. It isn't always necessary, but */
388 cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
389 cursor->nOffset = 0;
390 }
391
392 pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
393 if (pUI) {
394 pUI->nStart = cursor->pPara->member.para.nCharOfs
395 + cursor->pRun->member.run.nCharOfs;
396 pUI->nLen = len;
397 }
398
399 pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
400 pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
401 ME_InsertBefore(cursor->pRun, pDI);
402 TRACE("Shift length:%d\n", len);
403 ME_PropagateCharOffset(cursor->pRun, len);
404 cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
405 return pDI;
406 }
407
408 /******************************************************************************
409 * ME_UpdateRunFlags
410 *
411 * Determine some of run attributes given its content (style, text content).
412 * Some flags cannot be determined by this function (MERF_GRAPHICS,
413 * MERF_ENDPARA)
414 */
415 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
416 {
417 ME_String *strText = run->strText;
418 assert(run->nCharOfs >= 0);
419
420 if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
421 run->nFlags |= MERF_HIDDEN;
422 else
423 run->nFlags &= ~MERF_HIDDEN;
424
425 if (ME_IsSplitable(strText))
426 run->nFlags |= MERF_SPLITTABLE;
427 else
428 run->nFlags &= ~MERF_SPLITTABLE;
429
430 if (!(run->nFlags & MERF_NOTEXT)) {
431 if (ME_IsWhitespaces(strText))
432 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
433 else
434 {
435 run->nFlags &= ~MERF_WHITESPACE;
436
437 if (ME_IsWSpace(strText->szData[0]))
438 run->nFlags |= MERF_STARTWHITE;
439 else
440 run->nFlags &= ~MERF_STARTWHITE;
441
442 if (ME_IsWSpace(strText->szData[strText->nLen - 1]))
443 run->nFlags |= MERF_ENDWHITE;
444 else
445 run->nFlags &= ~MERF_ENDWHITE;
446 }
447 }
448 else
449 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
450 }
451
452 /******************************************************************************
453 * ME_CharFromPoint
454 *
455 * Returns a character position inside the run given a run-relative
456 * pixel horizontal position. This version rounds left (ie. if the second
457 * character is at pixel position 8, then for cx=0..7 it returns 0).
458 */
459 int ME_CharFromPoint(ME_Context *c, int cx, ME_Run *run)
460 {
461 int fit = 0;
462 HGDIOBJ hOldFont;
463 SIZE sz;
464 if (!run->strText->nLen)
465 return 0;
466
467 if (run->nFlags & MERF_TAB ||
468 (run->nFlags & (MERF_ENDCELL|MERF_ENDPARA)) == MERF_ENDCELL)
469 {
470 if (cx < run->nWidth/2)
471 return 0;
472 return 1;
473 }
474 if (run->nFlags & MERF_GRAPHICS)
475 {
476 SIZE sz;
477 ME_GetOLEObjectSize(c, run, &sz);
478 if (cx < sz.cx)
479 return 0;
480 return 1;
481 }
482 hOldFont = ME_SelectStyleFont(c, run->style);
483
484 if (c->editor->cPasswordMask)
485 {
486 ME_String *strMasked = ME_MakeStringR(c->editor->cPasswordMask, run->strText->nLen);
487 GetTextExtentExPointW(c->hDC, strMasked->szData, run->strText->nLen,
488 cx, &fit, NULL, &sz);
489 ME_DestroyString(strMasked);
490 }
491 else
492 {
493 GetTextExtentExPointW(c->hDC, run->strText->szData, run->strText->nLen,
494 cx, &fit, NULL, &sz);
495 }
496
497 ME_UnselectStyleFont(c, run->style, hOldFont);
498
499 return fit;
500 }
501
502 /******************************************************************************
503 * ME_CharFromPointCursor
504 *
505 * Returns a character position inside the run given a run-relative
506 * pixel horizontal position. This version rounds to the nearest character edge
507 * (ie. if the second character is at pixel position 8, then for cx=0..3
508 * it returns 0, and for cx=4..7 it returns 1).
509 *
510 * It is used for mouse click handling, for better usability (and compatibility
511 * with the native control).
512 */
513 int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
514 {
515 ME_String *strRunText;
516 /* This could point to either the run's real text, or it's masked form in a password control */
517
518 int fit = 0;
519 ME_Context c;
520 HGDIOBJ hOldFont;
521 SIZE sz, sz2, sz3;
522 if (!run->strText->nLen)
523 return 0;
524
525 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
526 {
527 if (cx < run->nWidth/2)
528 return 0;
529 return 1;
530 }
531 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
532 if (run->nFlags & MERF_GRAPHICS)
533 {
534 SIZE sz;
535 ME_GetOLEObjectSize(&c, run, &sz);
536 ME_DestroyContext(&c);
537 if (cx < sz.cx/2)
538 return 0;
539 return 1;
540 }
541
542 if (editor->cPasswordMask)
543 strRunText = ME_MakeStringR(editor->cPasswordMask, run->strText->nLen);
544 else
545 strRunText = run->strText;
546
547 hOldFont = ME_SelectStyleFont(&c, run->style);
548 GetTextExtentExPointW(c.hDC, strRunText->szData, strRunText->nLen,
549 cx, &fit, NULL, &sz);
550 if (fit != strRunText->nLen)
551 {
552 GetTextExtentPoint32W(c.hDC, strRunText->szData, fit, &sz2);
553 GetTextExtentPoint32W(c.hDC, strRunText->szData, fit + 1, &sz3);
554 if (cx >= (sz2.cx+sz3.cx)/2)
555 fit = fit + 1;
556 }
557
558 if (editor->cPasswordMask)
559 ME_DestroyString(strRunText);
560
561 ME_UnselectStyleFont(&c, run->style, hOldFont);
562 ME_DestroyContext(&c);
563 return fit;
564 }
565
566 /******************************************************************************
567 * ME_GetTextExtent
568 *
569 * Finds a width and a height of the text using a specified style
570 */
571 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
572 {
573 HGDIOBJ hOldFont;
574 hOldFont = ME_SelectStyleFont(c, s);
575 GetTextExtentPoint32W(c->hDC, szText, nChars, size);
576 ME_UnselectStyleFont(c, s, hOldFont);
577 }
578
579 /******************************************************************************
580 * ME_PointFromChar
581 *
582 * Returns a run-relative pixel position given a run-relative character
583 * position (character offset)
584 */
585 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
586 {
587 SIZE size;
588 ME_Context c;
589 ME_String *strRunText;
590 /* This could point to either the run's real text, or it's masked form in a password control */
591
592 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
593 if (pRun->nFlags & MERF_GRAPHICS)
594 {
595 if (nOffset)
596 ME_GetOLEObjectSize(&c, pRun, &size);
597 ME_DestroyContext(&c);
598 return nOffset != 0;
599 } else if (pRun->nFlags & MERF_ENDPARA) {
600 nOffset = 0;
601 }
602
603 if (editor->cPasswordMask)
604 strRunText = ME_MakeStringR(editor->cPasswordMask, pRun->strText->nLen);
605 else
606 strRunText = pRun->strText;
607
608 ME_GetTextExtent(&c, strRunText->szData, nOffset, pRun->style, &size);
609 ME_DestroyContext(&c);
610 if (editor->cPasswordMask)
611 ME_DestroyString(strRunText);
612 return size.cx;
613 }
614
615 /******************************************************************************
616 * ME_GetRunSizeCommon
617 *
618 * Finds width, height, ascent and descent of a run, up to given character
619 * (nLen).
620 */
621 static SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
622 int startx, int *pAscent, int *pDescent)
623 {
624 SIZE size;
625 int nMaxLen = run->strText->nLen;
626
627 if (nLen>nMaxLen)
628 nLen = nMaxLen;
629
630 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
631 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
632 * in practice
633 */
634
635 if (c->editor->cPasswordMask)
636 {
637 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
638 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
639 ME_DestroyString(szMasked);
640 }
641 else
642 {
643 ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
644 }
645 *pAscent = run->style->tm.tmAscent;
646 *pDescent = run->style->tm.tmDescent;
647 size.cy = *pAscent + *pDescent;
648
649 if (run->nFlags & MERF_TAB)
650 {
651 int pos = 0, i = 0, ppos, shift = 0;
652 PARAFORMAT2 *pFmt = para->pFmt;
653
654 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
655 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
656 /* The horizontal gap shifts the tab positions to leave the gap. */
657 shift = pFmt->dxOffset * 2;
658 do {
659 if (i < pFmt->cTabCount)
660 {
661 /* Only one side of the horizontal gap is needed at the end of
662 * the table row. */
663 if (i == pFmt->cTabCount -1)
664 shift = shift >> 1;
665 pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
666 i++;
667 }
668 else
669 {
670 pos += lDefaultTab - (pos % lDefaultTab);
671 }
672 ppos = ME_twips2pointsX(c, pos);
673 if (ppos > startx + run->pt.x) {
674 size.cx = ppos - startx - run->pt.x;
675 break;
676 }
677 } while(1);
678 size.cy = *pAscent + *pDescent;
679 return size;
680 }
681 if (run->nFlags & MERF_GRAPHICS)
682 {
683 ME_GetOLEObjectSize(c, run, &size);
684 if (size.cy > *pAscent)
685 *pAscent = size.cy;
686 /* descent is unchanged */
687 return size;
688 }
689 return size;
690 }
691
692 /******************************************************************************
693 * ME_GetRunSize
694 *
695 * Finds width and height (but not ascent and descent) of a part of the run
696 * up to given character.
697 */
698 SIZE ME_GetRunSize(ME_Context *c, const ME_Paragraph *para,
699 ME_Run *run, int nLen, int startx)
700 {
701 int asc, desc;
702 return ME_GetRunSizeCommon(c, para, run, nLen, startx, &asc, &desc);
703 }
704
705 /******************************************************************************
706 * ME_CalcRunExtent
707 *
708 * Updates the size of the run (fills width, ascent and descent). The height
709 * is calculated based on whole row's ascent and descent anyway, so no need
710 * to use it here.
711 */
712 void ME_CalcRunExtent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
713 {
714 if (run->nFlags & MERF_HIDDEN)
715 run->nWidth = 0;
716 else
717 {
718 int nEnd = run->strText->nLen;
719 SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, startx,
720 &run->nAscent, &run->nDescent);
721 run->nWidth = size.cx;
722 if (!size.cx)
723 WARN("size.cx == 0\n");
724 }
725 }
726
727 /******************************************************************************
728 * ME_SetSelectionCharFormat
729 *
730 * Applies a style change, either to a current selection, or to insert cursor
731 * (ie. the style next typed characters will use).
732 */
733 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
734 {
735 int nFrom, nTo;
736 ME_GetSelection(editor, &nFrom, &nTo);
737 if (nFrom == nTo)
738 {
739 ME_Style *s;
740 if (!editor->pBuffer->pCharStyle)
741 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
742 s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
743 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
744 editor->pBuffer->pCharStyle = s;
745 }
746 else
747 ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
748 }
749
750 /******************************************************************************
751 * ME_SetCharFormat
752 *
753 * Applies a style change to the specified part of the text
754 */
755 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
756 {
757 ME_Cursor tmp, tmp2;
758 ME_DisplayItem *para;
759
760 ME_CursorFromCharOfs(editor, nOfs, &tmp);
761 if (tmp.nOffset)
762 tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
763
764 ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
765 if (tmp2.nOffset)
766 tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
767
768 para = tmp.pPara;
769 para->member.para.nFlags |= MEPF_REWRAP;
770
771 while(tmp.pRun != tmp2.pRun)
772 {
773 ME_UndoItem *undo = NULL;
774 ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
775 /* ME_DumpStyle(new_style); */
776 undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
777 if (undo) {
778 undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
779 undo->nLen = tmp.pRun->member.run.strText->nLen;
780 undo->di.member.ustyle = tmp.pRun->member.run.style;
781 /* we'd have to addref undo...ustyle and release tmp...style
782 but they'd cancel each other out so we can do nothing instead */
783 }
784 else
785 ME_ReleaseStyle(tmp.pRun->member.run.style);
786 tmp.pRun->member.run.style = new_style;
787 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
788 if (tmp.pRun->type == diParagraph)
789 {
790 para = tmp.pRun;
791 tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
792 if (tmp.pRun != tmp2.pRun)
793 para->member.para.nFlags |= MEPF_REWRAP;
794 }
795 assert(tmp.pRun);
796 }
797 }
798
799 /******************************************************************************
800 * ME_SetDefaultCharFormat
801 *
802 * Applies a style change to the default character style.
803 */
804 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
805 {
806 ME_Style *style;
807
808 assert(mod->cbSize == sizeof(CHARFORMAT2W));
809 style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
810 editor->pBuffer->pDefaultStyle->fmt = style->fmt;
811 editor->pBuffer->pDefaultStyle->tm = style->tm;
812 ME_ReleaseStyle(style);
813 ME_MarkAllForWrapping(editor);
814 /* pcf = editor->pBuffer->pDefaultStyle->fmt; */
815 }
816
817 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
818 {
819 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
820 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_CF1UNDERLINE))
821 {
822 pFmt->dwMask |= CFM_UNDERLINE;
823 pFmt->dwEffects |= CFE_UNDERLINE;
824 }
825 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_UNDERLINENONE))
826 {
827 pFmt->dwMask |= CFM_UNDERLINE;
828 pFmt->dwEffects &= ~CFE_UNDERLINE;
829 }
830 }
831
832 /******************************************************************************
833 * ME_GetDefaultCharFormat
834 *
835 * Retrieves the current default character style (the one applied where no
836 * other style was applied) .
837 */
838 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
839 {
840 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
841 }
842
843 /******************************************************************************
844 * ME_GetSelectionCharFormat
845 *
846 * If selection exists, it returns all style elements that are set consistently
847 * in the whole selection. If not, it just returns the current style.
848 */
849 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
850 {
851 int nFrom, nTo;
852 ME_GetSelection(editor, &nFrom, &nTo);
853 if (nFrom == nTo && editor->pBuffer->pCharStyle)
854 {
855 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
856 return;
857 }
858 ME_GetCharFormat(editor, nFrom, nTo, pFmt);
859 }
860
861 /******************************************************************************
862 * ME_GetCharFormat
863 *
864 * Returns the style consisting of those attributes which are consistently set
865 * in the whole character range.
866 */
867 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
868 {
869 ME_DisplayItem *run, *run_end;
870 int nOffset, nOffset2;
871 CHARFORMAT2W tmp;
872
873 ME_RunOfsFromCharOfs(editor, nFrom, NULL, &run, &nOffset);
874 if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
875 {
876 if (!nOffset)
877 {
878 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
879 if (tmp_run->type == diRun) {
880 ME_GetRunCharFormat(editor, tmp_run, pFmt);
881 return;
882 }
883 }
884 ME_GetRunCharFormat(editor, run, pFmt);
885 return;
886 }
887
888 if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
889 nTo--;
890 ME_RunOfsFromCharOfs(editor, nTo, NULL, &run_end, &nOffset2);
891
892 ME_GetRunCharFormat(editor, run, pFmt);
893
894 if (run == run_end) return;
895
896 do {
897 /* FIXME add more style feature comparisons */
898 int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
899 int nEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
900
901 run = ME_FindItemFwd(run, diRun);
902
903 ZeroMemory(&tmp, sizeof(tmp));
904 tmp.cbSize = sizeof(tmp);
905 ME_GetRunCharFormat(editor, run, &tmp);
906
907 assert((tmp.dwMask & nAttribs) == nAttribs);
908 /* reset flags that differ */
909
910 if (pFmt->yHeight != tmp.yHeight)
911 pFmt->dwMask &= ~CFM_SIZE;
912 if (pFmt->dwMask & CFM_FACE)
913 {
914 if (!(tmp.dwMask & CFM_FACE))
915 pFmt->dwMask &= ~CFM_FACE;
916 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
917 pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
918 pFmt->dwMask &= ~CFM_FACE;
919 }
920 if (pFmt->yHeight != tmp.yHeight)
921 pFmt->dwMask &= ~CFM_SIZE;
922 if (pFmt->bUnderlineType != tmp.bUnderlineType)
923 pFmt->dwMask &= ~CFM_UNDERLINETYPE;
924 if (pFmt->dwMask & CFM_COLOR)
925 {
926 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
927 {
928 if (pFmt->crTextColor != tmp.crTextColor)
929 pFmt->dwMask &= ~CFM_COLOR;
930 }
931 }
932
933 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
934 pFmt->dwEffects = tmp.dwEffects;
935
936 } while(run != run_end);
937 }