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