f3b4db6d464d28c81901fd762a2e223c90fa914c
[reactos.git] / reactos / 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 TRUE if two runs can be safely merged into one, FALSE otherwise.
34 */
35 BOOL ME_CanJoinRuns(const ME_Run *run1, const ME_Run *run2)
36 {
37 if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
38 return FALSE;
39 if (run1->style != run2->style)
40 return FALSE;
41 if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
42 return FALSE;
43 return TRUE;
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_run( &p->member.run ), shift);
68 do {
69 p->member.run.nCharOfs += shift;
70 assert(p->member.run.nCharOfs >= 0);
71 p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
72 } while(p->type == diRun);
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.len, debugstr_run( &p->member.run ),
127 p->member.run.nFlags,
128 p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
129 assert(ofs == p->member.run.nCharOfs);
130 assert(p->member.run.len);
131 ofs += p->member.run.len;
132 break;
133 case diCell:
134 TRACE_(richedit_check)("cell\n");
135 break;
136 default:
137 assert(0);
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 int endOfs = nCharOfs, len = ME_GetTextLength(editor);
185
186 nCharOfs = max(nCharOfs, 0);
187 nCharOfs = min(nCharOfs, len);
188
189 /* Find the paragraph at the offset. */
190 next_item = editor->pBuffer->pFirst->member.para.next_para;
191 do {
192 item = next_item;
193 next_item = item->member.para.next_para;
194 } while (next_item->member.para.nCharOfs <= nCharOfs);
195 assert(item->type == diParagraph);
196 nCharOfs -= item->member.para.nCharOfs;
197 if (ppPara) *ppPara = item;
198
199 /* Find the run at the offset. */
200 next_item = ME_FindItemFwd(item, diRun);
201 do {
202 item = next_item;
203 next_item = ME_FindItemFwd(item, diRunOrParagraphOrEnd);
204 } while (next_item->type == diRun &&
205 next_item->member.run.nCharOfs <= nCharOfs);
206 assert(item->type == diRun);
207 nCharOfs -= item->member.run.nCharOfs;
208
209 if (ppRun) *ppRun = item;
210 if (pOfs) {
211 if (((*ppRun)->member.run.nFlags & MERF_ENDPARA) && endOfs > len)
212 *pOfs = (*ppRun)->member.run.len;
213 else *pOfs = nCharOfs;
214 }
215 }
216
217 /******************************************************************************
218 * ME_JoinRuns
219 *
220 * Merges two adjacent runs, the one given as a parameter and the next one.
221 */
222 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
223 {
224 ME_DisplayItem *pNext = p->next;
225 int i;
226 assert(p->type == diRun && pNext->type == diRun);
227 assert(p->member.run.nCharOfs != -1);
228 ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
229
230 /* Update all cursors so that they don't contain the soon deleted run */
231 for (i=0; i<editor->nCursors; i++) {
232 if (editor->pCursors[i].pRun == pNext) {
233 editor->pCursors[i].pRun = p;
234 editor->pCursors[i].nOffset += p->member.run.len;
235 }
236 }
237
238 p->member.run.len += pNext->member.run.len;
239 ME_Remove(pNext);
240 ME_DestroyDisplayItem(pNext);
241 ME_UpdateRunFlags(editor, &p->member.run);
242 if(TRACE_ON(richedit))
243 {
244 TRACE("Before check after join\n");
245 ME_CheckCharOffsets(editor);
246 TRACE("After check after join\n");
247 }
248 }
249
250 /******************************************************************************
251 * ME_SplitRunSimple
252 *
253 * Does the most basic job of splitting a run into two - it does not
254 * update the positions and extents.
255 */
256 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_Cursor *cursor)
257 {
258 ME_DisplayItem *run = cursor->pRun;
259 ME_DisplayItem *new_run;
260 int i;
261 int nOffset = cursor->nOffset;
262
263 assert(!(run->member.run.nFlags & MERF_NONTEXT));
264
265 new_run = ME_MakeRun(run->member.run.style,
266 run->member.run.nFlags & MERF_SPLITMASK);
267 new_run->member.run.nCharOfs = run->member.run.nCharOfs + nOffset;
268 new_run->member.run.len = run->member.run.len - nOffset;
269 new_run->member.run.para = run->member.run.para;
270 run->member.run.len = nOffset;
271 cursor->pRun = new_run;
272 cursor->nOffset = 0;
273
274 ME_InsertBefore(run->next, new_run);
275
276 ME_UpdateRunFlags(editor, &run->member.run);
277 ME_UpdateRunFlags(editor, &new_run->member.run);
278 for (i = 0; i < editor->nCursors; i++) {
279 if (editor->pCursors[i].pRun == run &&
280 editor->pCursors[i].nOffset >= nOffset) {
281 editor->pCursors[i].pRun = new_run;
282 editor->pCursors[i].nOffset -= nOffset;
283 }
284 }
285 cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
286 return run;
287 }
288
289 /******************************************************************************
290 * ME_MakeRun
291 *
292 * A helper function to create run structures quickly.
293 */
294 ME_DisplayItem *ME_MakeRun(ME_Style *s, int nFlags)
295 {
296 ME_DisplayItem *item = ME_MakeDI(diRun);
297 item->member.run.style = s;
298 item->member.run.ole_obj = NULL;
299 item->member.run.nFlags = nFlags;
300 item->member.run.nCharOfs = -1;
301 item->member.run.len = 0;
302 item->member.run.para = NULL;
303 item->member.run.num_glyphs = 0;
304 item->member.run.max_glyphs = 0;
305 item->member.run.glyphs = NULL;
306 item->member.run.vis_attrs = NULL;
307 item->member.run.advances = NULL;
308 item->member.run.offsets = NULL;
309 item->member.run.max_clusters = 0;
310 item->member.run.clusters = NULL;
311 ME_AddRefStyle(s);
312 return item;
313 }
314
315 /******************************************************************************
316 * ME_InsertRunAtCursor
317 *
318 * Inserts a new run with given style, flags and content at a given position,
319 * which is passed as a cursor structure (which consists of a run and
320 * a run-relative character offset).
321 */
322 ME_DisplayItem *
323 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
324 const WCHAR *str, int len, int flags)
325 {
326 ME_DisplayItem *pDI, *insert_before = cursor->pRun, *prev;
327
328 if (cursor->nOffset)
329 {
330 if (cursor->nOffset == cursor->pRun->member.run.len)
331 {
332 insert_before = ME_FindItemFwd( cursor->pRun, diRun );
333 if (!insert_before) insert_before = cursor->pRun; /* Always insert before the final eop run */
334 }
335 else
336 {
337 ME_SplitRunSimple( editor, cursor );
338 insert_before = cursor->pRun;
339 }
340 }
341
342 add_undo_delete_run( editor, insert_before->member.run.para->nCharOfs +
343 insert_before->member.run.nCharOfs, len );
344
345 pDI = ME_MakeRun(style, flags);
346 pDI->member.run.nCharOfs = insert_before->member.run.nCharOfs;
347 pDI->member.run.len = len;
348 pDI->member.run.para = insert_before->member.run.para;
349 ME_InsertString( pDI->member.run.para->text, pDI->member.run.nCharOfs, str, len );
350 ME_InsertBefore( insert_before, pDI );
351 TRACE("Shift length:%d\n", len);
352 ME_PropagateCharOffset( insert_before, len );
353 insert_before->member.run.para->nFlags |= MEPF_REWRAP;
354
355 /* Move any cursors that were at the end of the previous run to the end of the inserted run */
356 prev = ME_FindItemBack( pDI, diRun );
357 if (prev)
358 {
359 int i;
360
361 for (i = 0; i < editor->nCursors; i++)
362 {
363 if (editor->pCursors[i].pRun == prev &&
364 editor->pCursors[i].nOffset == prev->member.run.len)
365 {
366 editor->pCursors[i].pRun = pDI;
367 editor->pCursors[i].nOffset = len;
368 }
369 }
370 }
371
372 return pDI;
373 }
374
375 static BOOL run_is_splittable( const ME_Run *run )
376 {
377 WCHAR *str = get_text( run, 0 ), *p;
378 int i;
379 BOOL found_ink = FALSE;
380
381 for (i = 0, p = str; i < run->len; i++, p++)
382 {
383 if (ME_IsWSpace( *p ))
384 {
385 if (found_ink) return TRUE;
386 }
387 else
388 found_ink = TRUE;
389 }
390 return FALSE;
391 }
392
393 static BOOL run_is_entirely_ws( const ME_Run *run )
394 {
395 WCHAR *str = get_text( run, 0 ), *p;
396 int i;
397
398 for (i = 0, p = str; i < run->len; i++, p++)
399 if (!ME_IsWSpace( *p )) return FALSE;
400
401 return TRUE;
402 }
403
404 /******************************************************************************
405 * ME_UpdateRunFlags
406 *
407 * Determine some of run attributes given its content (style, text content).
408 * Some flags cannot be determined by this function (MERF_GRAPHICS,
409 * MERF_ENDPARA)
410 */
411 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
412 {
413 assert(run->nCharOfs >= 0);
414
415 if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
416 run->nFlags |= MERF_HIDDEN;
417 else
418 run->nFlags &= ~MERF_HIDDEN;
419
420 if (run_is_splittable( run ))
421 run->nFlags |= MERF_SPLITTABLE;
422 else
423 run->nFlags &= ~MERF_SPLITTABLE;
424
425 if (!(run->nFlags & MERF_NOTEXT))
426 {
427 if (run_is_entirely_ws( run ))
428 run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
429 else
430 {
431 run->nFlags &= ~MERF_WHITESPACE;
432
433 if (ME_IsWSpace( *get_text( run, 0 ) ))
434 run->nFlags |= MERF_STARTWHITE;
435 else
436 run->nFlags &= ~MERF_STARTWHITE;
437
438 if (ME_IsWSpace( *get_text( run, run->len - 1 ) ))
439 run->nFlags |= MERF_ENDWHITE;
440 else
441 run->nFlags &= ~MERF_ENDWHITE;
442 }
443 }
444 else
445 run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
446 }
447
448 /******************************************************************************
449 * ME_CharFromPointContext
450 *
451 * Returns a character position inside the run given a run-relative
452 * pixel horizontal position.
453 *
454 * If closest is FALSE return the actual character
455 * If closest is TRUE will round to the closest leading edge.
456 * ie. if the second character is at pixel position 8 and third at 16 then for:
457 * closest = FALSE cx = 0..7 return 0, cx = 8..15 return 1
458 * closest = TRUE cx = 0..3 return 0, cx = 4..11 return 1.
459 */
460 int ME_CharFromPointContext(ME_Context *c, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
461 {
462 ME_String *mask_text = NULL;
463 WCHAR *str;
464 int fit = 0;
465 HGDIOBJ hOldFont;
466 SIZE sz, sz2, sz3;
467 if (!run->len || cx <= 0)
468 return 0;
469
470 if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
471 {
472 if (!closest || cx < run->nWidth / 2) return 0;
473 return 1;
474 }
475
476 if (run->nFlags & MERF_GRAPHICS)
477 {
478 SIZE sz;
479 ME_GetOLEObjectSize(c, run, &sz);
480 if (!closest || cx < sz.cx / 2) return 0;
481 return 1;
482 }
483
484 if (run->para->nFlags & MEPF_COMPLEX)
485 {
486 int cp, trailing;
487 if (visual_order && run->script_analysis.fRTL) cx = run->nWidth - cx - 1;
488
489 ScriptXtoCP( cx, run->len, run->num_glyphs, run->clusters, run->vis_attrs, run->advances, &run->script_analysis,
490 &cp, &trailing );
491 TRACE("x %d cp %d trailing %d (run width %d) rtl %d log order %d\n", cx, cp, trailing, run->nWidth,
492 run->script_analysis.fRTL, run->script_analysis.fLogicalOrder);
493 return closest ? cp + trailing : cp;
494 }
495
496 if (c->editor->cPasswordMask)
497 {
498 mask_text = ME_MakeStringR( c->editor->cPasswordMask, run->len );
499 str = mask_text->szData;
500 }
501 else
502 str = get_text( run, 0 );
503
504 hOldFont = ME_SelectStyleFont(c, run->style);
505 GetTextExtentExPointW(c->hDC, str, run->len,
506 cx, &fit, NULL, &sz);
507 if (closest && fit != run->len)
508 {
509 GetTextExtentPoint32W(c->hDC, str, fit, &sz2);
510 GetTextExtentPoint32W(c->hDC, str, fit + 1, &sz3);
511 if (cx >= (sz2.cx+sz3.cx)/2)
512 fit = fit + 1;
513 }
514
515 ME_DestroyString( mask_text );
516
517 ME_UnselectStyleFont(c, run->style, hOldFont);
518 return fit;
519 }
520
521 int ME_CharFromPoint(ME_TextEditor *editor, int cx, ME_Run *run, BOOL closest, BOOL visual_order)
522 {
523 ME_Context c;
524 int ret;
525
526 ME_InitContext( &c, editor, ITextHost_TxGetDC( editor->texthost ) );
527 ret = ME_CharFromPointContext( &c, cx, run, closest, visual_order );
528 ME_DestroyContext(&c);
529 return ret;
530 }
531
532 /******************************************************************************
533 * ME_GetTextExtent
534 *
535 * Finds a width and a height of the text using a specified style
536 */
537 static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
538 {
539 HGDIOBJ hOldFont;
540 if (c->hDC) {
541 hOldFont = ME_SelectStyleFont(c, s);
542 GetTextExtentPoint32W(c->hDC, szText, nChars, size);
543 ME_UnselectStyleFont(c, s, hOldFont);
544 } else {
545 size->cx = 0;
546 size->cy = 0;
547 }
548 }
549
550 /******************************************************************************
551 * ME_PointFromCharContext
552 *
553 * Returns a run-relative pixel position given a run-relative character
554 * position (character offset)
555 */
556 int ME_PointFromCharContext(ME_Context *c, ME_Run *pRun, int nOffset, BOOL visual_order)
557 {
558 SIZE size;
559 ME_String *mask_text = NULL;
560 WCHAR *str;
561
562 if (pRun->nFlags & MERF_GRAPHICS)
563 {
564 if (nOffset)
565 ME_GetOLEObjectSize(c, pRun, &size);
566 return nOffset != 0;
567 } else if (pRun->nFlags & MERF_ENDPARA) {
568 nOffset = 0;
569 }
570
571 if (pRun->para->nFlags & MEPF_COMPLEX)
572 {
573 int x;
574 ScriptCPtoX( nOffset, FALSE, pRun->len, pRun->num_glyphs, pRun->clusters,
575 pRun->vis_attrs, pRun->advances, &pRun->script_analysis, &x );
576 if (visual_order && pRun->script_analysis.fRTL) x = pRun->nWidth - x - 1;
577 return x;
578 }
579 if (c->editor->cPasswordMask)
580 {
581 mask_text = ME_MakeStringR(c->editor->cPasswordMask, pRun->len);
582 str = mask_text->szData;
583 }
584 else
585 str = get_text( pRun, 0 );
586
587 ME_GetTextExtent(c, str, nOffset, pRun->style, &size);
588 ME_DestroyString( mask_text );
589 return size.cx;
590 }
591
592 /******************************************************************************
593 * ME_PointFromChar
594 *
595 * Calls ME_PointFromCharContext after first creating a context.
596 */
597 int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset, BOOL visual_order)
598 {
599 ME_Context c;
600 int ret;
601
602 ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
603 ret = ME_PointFromCharContext( &c, pRun, nOffset, visual_order );
604 ME_DestroyContext(&c);
605
606 return ret;
607 }
608
609 /******************************************************************************
610 * ME_GetRunSizeCommon
611 *
612 * Finds width, height, ascent and descent of a run, up to given character
613 * (nLen).
614 */
615 SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
616 int startx, int *pAscent, int *pDescent)
617 {
618 SIZE size;
619 int nMaxLen = run->len;
620
621 if (nLen>nMaxLen)
622 nLen = nMaxLen;
623
624 /* FIXME the following call also ensures that TEXTMETRIC structure is filled
625 * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
626 * in practice
627 */
628
629 if (para->nFlags & MEPF_COMPLEX)
630 {
631 size.cx = run->nWidth;
632 }
633 else if (c->editor->cPasswordMask)
634 {
635 ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
636 ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size);
637 ME_DestroyString(szMasked);
638 }
639 else
640 {
641 ME_GetTextExtent(c, get_text( run, 0 ), nLen, run->style, &size);
642 }
643 *pAscent = run->style->tm.tmAscent;
644 *pDescent = run->style->tm.tmDescent;
645 size.cy = *pAscent + *pDescent;
646
647 if (run->nFlags & MERF_TAB)
648 {
649 int pos = 0, i = 0, ppos, shift = 0;
650 PARAFORMAT2 *pFmt = para->pFmt;
651
652 if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
653 pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
654 /* The horizontal gap shifts the tab positions to leave the gap. */
655 shift = pFmt->dxOffset * 2;
656 do {
657 if (i < pFmt->cTabCount)
658 {
659 /* Only one side of the horizontal gap is needed at the end of
660 * the table row. */
661 if (i == pFmt->cTabCount -1)
662 shift = shift >> 1;
663 pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
664 i++;
665 }
666 else
667 {
668 pos += lDefaultTab - (pos % lDefaultTab);
669 }
670 ppos = ME_twips2pointsX(c, pos);
671 if (ppos > startx + run->pt.x) {
672 size.cx = ppos - startx - run->pt.x;
673 break;
674 }
675 } while(1);
676 size.cy = *pAscent + *pDescent;
677 return size;
678 }
679 if (run->nFlags & MERF_GRAPHICS)
680 {
681 ME_GetOLEObjectSize(c, run, &size);
682 if (size.cy > *pAscent)
683 *pAscent = size.cy;
684 /* descent is unchanged */
685 return size;
686 }
687 return size;
688 }
689
690 /******************************************************************************
691 * ME_SetSelectionCharFormat
692 *
693 * Applies a style change, either to a current selection, or to insert cursor
694 * (ie. the style next typed characters will use).
695 */
696 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
697 {
698 if (!ME_IsSelection(editor))
699 {
700 ME_Style *s;
701 if (!editor->pBuffer->pCharStyle)
702 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
703 s = ME_ApplyStyle(editor, editor->pBuffer->pCharStyle, pFmt);
704 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
705 editor->pBuffer->pCharStyle = s;
706 } else {
707 ME_Cursor *from, *to;
708 ME_GetSelection(editor, &from, &to);
709 ME_SetCharFormat(editor, from, to, pFmt);
710 }
711 }
712
713 /******************************************************************************
714 * ME_SetCharFormat
715 *
716 * Applies a style change to the specified part of the text
717 *
718 * The start and end cursors specify the part of the text. These cursors will
719 * be updated to stay valid, but this function may invalidate other
720 * non-selection cursors. The end cursor may be NULL to specify all the text
721 * following the start cursor.
722 *
723 * If no text is selected, then nothing is done.
724 */
725 void ME_SetCharFormat(ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *pFmt)
726 {
727 ME_DisplayItem *run, *start_run = start->pRun, *end_run = NULL;
728
729 if (end && start->pRun == end->pRun && start->nOffset == end->nOffset)
730 return;
731
732 if (start->nOffset == start->pRun->member.run.len)
733 start_run = ME_FindItemFwd( start->pRun, diRun );
734 else if (start->nOffset)
735 {
736 /* SplitRunSimple may or may not update the cursors, depending on whether they
737 * are selection cursors, but we need to make sure they are valid. */
738 int split_offset = start->nOffset;
739 ME_DisplayItem *split_run = ME_SplitRunSimple(editor, start);
740 start_run = start->pRun;
741 if (end && end->pRun == split_run)
742 {
743 end->pRun = start->pRun;
744 end->nOffset -= split_offset;
745 }
746 }
747
748 if (end)
749 {
750 if (end->nOffset == end->pRun->member.run.len)
751 end_run = ME_FindItemFwd( end->pRun, diRun );
752 else
753 {
754 if (end->nOffset) ME_SplitRunSimple(editor, end);
755 end_run = end->pRun;
756 }
757 }
758
759 for (run = start_run; run != end_run; run = ME_FindItemFwd( run, diRun ))
760 {
761 ME_Style *new_style = ME_ApplyStyle(editor, run->member.run.style, pFmt);
762
763 add_undo_set_char_fmt( editor, run->member.run.para->nCharOfs + run->member.run.nCharOfs,
764 run->member.run.len, &run->member.run.style->fmt );
765 ME_ReleaseStyle(run->member.run.style);
766 run->member.run.style = new_style;
767 run->member.run.para->nFlags |= MEPF_REWRAP;
768 }
769 }
770
771 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
772 {
773 ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
774 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_CF1UNDERLINE))
775 {
776 pFmt->dwMask |= CFM_UNDERLINE;
777 pFmt->dwEffects |= CFE_UNDERLINE;
778 }
779 if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_UNDERLINENONE))
780 {
781 pFmt->dwMask |= CFM_UNDERLINE;
782 pFmt->dwEffects &= ~CFE_UNDERLINE;
783 }
784 }
785
786 /******************************************************************************
787 * ME_GetDefaultCharFormat
788 *
789 * Retrieves the current default character style (the one applied where no
790 * other style was applied) .
791 */
792 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
793 {
794 ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
795 }
796
797 /******************************************************************************
798 * ME_GetSelectionCharFormat
799 *
800 * If selection exists, it returns all style elements that are set consistently
801 * in the whole selection. If not, it just returns the current style.
802 */
803 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
804 {
805 ME_Cursor *from, *to;
806 if (!ME_IsSelection(editor) && editor->pBuffer->pCharStyle)
807 {
808 ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
809 return;
810 }
811 ME_GetSelection(editor, &from, &to);
812 ME_GetCharFormat(editor, from, to, pFmt);
813 }
814
815 /******************************************************************************
816 * ME_GetCharFormat
817 *
818 * Returns the style consisting of those attributes which are consistently set
819 * in the whole character range.
820 */
821 void ME_GetCharFormat(ME_TextEditor *editor, const ME_Cursor *from,
822 const ME_Cursor *to, CHARFORMAT2W *pFmt)
823 {
824 ME_DisplayItem *run, *run_end;
825 CHARFORMAT2W tmp;
826
827 run = from->pRun;
828 /* special case - if selection is empty, take previous char's formatting */
829 if (from->pRun == to->pRun && from->nOffset == to->nOffset)
830 {
831 if (!from->nOffset)
832 {
833 ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
834 if (tmp_run->type == diRun) {
835 ME_GetRunCharFormat(editor, tmp_run, pFmt);
836 return;
837 }
838 }
839 ME_GetRunCharFormat(editor, run, pFmt);
840 return;
841 }
842
843 run_end = to->pRun;
844 if (!to->nOffset)
845 run_end = ME_FindItemBack(run_end, diRun);
846
847 ME_GetRunCharFormat(editor, run, pFmt);
848
849 if (run == run_end) return;
850
851 do {
852 /* FIXME add more style feature comparisons */
853 DWORD dwAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
854 DWORD dwEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
855
856 run = ME_FindItemFwd(run, diRun);
857
858 ZeroMemory(&tmp, sizeof(tmp));
859 tmp.cbSize = sizeof(tmp);
860 ME_GetRunCharFormat(editor, run, &tmp);
861
862 assert((tmp.dwMask & dwAttribs) == dwAttribs);
863 /* reset flags that differ */
864
865 if (pFmt->yHeight != tmp.yHeight)
866 pFmt->dwMask &= ~CFM_SIZE;
867 if (pFmt->dwMask & CFM_FACE)
868 {
869 if (!(tmp.dwMask & CFM_FACE))
870 pFmt->dwMask &= ~CFM_FACE;
871 else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
872 pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
873 pFmt->dwMask &= ~CFM_FACE;
874 }
875 if (pFmt->yHeight != tmp.yHeight)
876 pFmt->dwMask &= ~CFM_SIZE;
877 if (pFmt->bUnderlineType != tmp.bUnderlineType)
878 pFmt->dwMask &= ~CFM_UNDERLINETYPE;
879 if (pFmt->dwMask & CFM_COLOR)
880 {
881 if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
882 {
883 if (pFmt->crTextColor != tmp.crTextColor)
884 pFmt->dwMask &= ~CFM_COLOR;
885 }
886 }
887
888 pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & dwEffects);
889 pFmt->dwEffects = tmp.dwEffects;
890
891 } while(run != run_end);
892 }