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