run.c 28.7 KB
Newer Older
1 2 3 4 5 6
/*
 * RichEdit - operations on runs (diRun, rectangular pieces of paragraphs).
 * Splitting/joining runs. Adjusting offsets after deleting/adding content.
 * Character/pixel conversions.
 *
 * Copyright 2004 by Krzysztof Foltman
7
 * Copyright 2006 by Phil Krylov
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 25 26
 */

#include "editor.h"

WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 28
WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
WINE_DECLARE_DEBUG_CHANNEL(richedit_lists);
29

30 31 32 33 34
/******************************************************************************
 * ME_CanJoinRuns
 *
 * Returns 1 if two runs can be safely merged into one, 0 otherwise.
 */ 
35
int ME_CanJoinRuns(const ME_Run *run1, const ME_Run *run2)
36
{
37
  if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    return 0;
  if (run1->style != run2->style)
    return 0;
  if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
    return 0;
  return 1;
}

void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
{
  p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
  assert(p);
  ME_PropagateCharOffset(p, shift);
}

53 54 55 56 57 58
/******************************************************************************
 * ME_PropagateCharOffsets
 *
 * Shifts (increases or decreases) character offset (relative to beginning of 
 * the document) of the part of the text starting from given place.  
 */ 
59 60
void ME_PropagateCharOffset(ME_DisplayItem *p, int shift)
{
61 62 63 64
	/* Runs in one paragraph contain character offset relative to their owning
	 * paragraph. If we start the shifting from the run, we need to shift
	 * all the relative offsets until the end of the paragraph
	 */	 	    
65 66 67 68 69 70 71 72 73
  if (p->type == diRun) /* propagate in all runs in this para */
  {
    TRACE("PropagateCharOffset(%s, %d)\n", debugstr_w(p->member.run.strText->szData), shift);
    do {
      p->member.run.nCharOfs += shift;
      assert(p->member.run.nCharOfs >= 0);
      p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
    } while(p->type == diRun);
  }
74 75 76 77
	/* Runs in next paragraphs don't need their offsets updated, because they, 
	 * again, those offsets are relative to their respective paragraphs.
	 * Instead of that, we're updating paragraphs' character offsets.	  
	 */	 	    
78 79 80 81 82 83 84 85
  if (p->type == diParagraph) /* propagate in all next paras */
  {
    do {
      p->member.para.nCharOfs += shift;
      assert(p->member.para.nCharOfs >= 0);
      p = p->member.para.next_para;
    } while(p->type == diParagraph);
  }
86 87 88
  /* diTextEnd also has character offset in it, which makes finding text length
   * easier. But it needs to be up to date first.
   */
89 90 91 92 93 94 95
  if (p->type == diTextEnd)
  {
    p->member.para.nCharOfs += shift;
    assert(p->member.para.nCharOfs >= 0);
  }
}

96 97 98 99 100
/******************************************************************************
 * ME_CheckCharOffsets
 * 
 * Checks if editor lists' validity and optionally dumps the document structure
 */      
101 102 103 104
void ME_CheckCharOffsets(ME_TextEditor *editor)
{
  ME_DisplayItem *p = editor->pBuffer->pFirst;
  int ofs = 0, ofsp = 0;
105
  if(TRACE_ON(richedit_lists))
106
  {
107
    TRACE_(richedit_lists)("---\n");
108 109 110 111 112 113
    ME_DumpDocument(editor->pBuffer);
  }
  do {
    p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
    switch(p->type) {
      case diTextEnd:
114
        TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
115 116 117
        assert(ofsp+ofs == p->member.para.nCharOfs);
        return;
      case diParagraph:
118
        TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
119 120 121 122 123
        assert(ofsp+ofs == p->member.para.nCharOfs);
        ofsp = p->member.para.nCharOfs;
        ofs = 0;
        break;
      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 127 128 129
          p->member.run.strText->nLen, debugstr_w(p->member.run.strText->szData),
          p->member.run.nFlags,
          p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
        assert(ofs == p->member.run.nCharOfs);
130 131
        assert(p->member.run.strText->nLen);
        ofs += p->member.run.strText->nLen;
132
        break;
133 134 135
      case diCell:
        TRACE_(richedit_check)("cell\n");
        break;
136 137 138 139 140 141
      default:
        assert(0);
    }
  } while(1);
}

142 143
/******************************************************************************
 * ME_CharOfsFromRunOfs
144
 *
145 146
 * Converts a character position relative to the start of the run, to a
 * character position relative to the start of the document.
147 148 149 150
 * Kind of a "local to global" offset conversion.
 */
int ME_CharOfsFromRunOfs(ME_TextEditor *editor, const ME_DisplayItem *pPara,
                         const ME_DisplayItem *pRun, int nOfs)
151
{
152 153 154
  assert(pRun && pRun->type == diRun);
  assert(pPara && pPara->type == diParagraph);
  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 163
 * run).
 */
164 165
void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
{
166 167
  ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pPara,
                       &pCursor->pRun, &pCursor->nOffset);
168 169
}

170 171
/******************************************************************************
 * ME_RunOfsFromCharOfs
172
 *
173 174
 * Find a run and relative character offset given an absolute character offset
 * (absolute offset being an offset relative to the start of the document).
175 176
 * Kind of a "global to local" offset conversion.
 */
177 178 179 180 181
void ME_RunOfsFromCharOfs(ME_TextEditor *editor,
                          int nCharOfs,
                          ME_DisplayItem **ppPara,
                          ME_DisplayItem **ppRun,
                          int *pOfs)
182
{
183
  ME_DisplayItem *item, *next_item;
184

185 186 187 188 189 190 191 192 193 194 195
  nCharOfs = max(nCharOfs, 0);
  nCharOfs = min(nCharOfs, ME_GetTextLength(editor));

  /* Find the paragraph at the offset. */
  next_item = editor->pBuffer->pFirst->member.para.next_para;
  do {
    item = next_item;
    next_item = item->member.para.next_para;
  } while (next_item->member.para.nCharOfs <= nCharOfs);
  assert(item->type == diParagraph);
  nCharOfs -= item->member.para.nCharOfs;
196
  if (ppPara) *ppPara = item;
197 198 199 200 201 202 203 204 205 206 207

  /* Find the run at the offset. */
  next_item = ME_FindItemFwd(item, diRun);
  do {
    item = next_item;
    next_item = ME_FindItemFwd(item, diRunOrParagraphOrEnd);
  } while (next_item->type == diRun &&
           next_item->member.run.nCharOfs <= nCharOfs);
  assert(item->type == diRun);
  nCharOfs -= item->member.run.nCharOfs;

208 209
  if (ppRun) *ppRun = item;
  if (pOfs) *pOfs = nCharOfs;
210 211
}

212 213 214 215 216
/******************************************************************************
 * ME_JoinRuns
 * 
 * Merges two adjacent runs, the one given as a parameter and the next one.
 */    
217 218 219 220 221 222
void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
{
  ME_DisplayItem *pNext = p->next;
  int i;
  assert(p->type == diRun && pNext->type == diRun);
  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 227 228
  for (i=0; i<editor->nCursors; i++) {
    if (editor->pCursors[i].pRun == pNext) {
      editor->pCursors[i].pRun = p;
229
      editor->pCursors[i].nOffset += p->member.run.strText->nLen;
230 231
    }
  }
232

233 234 235 236 237 238 239 240 241 242 243 244
  ME_AppendString(p->member.run.strText, pNext->member.run.strText);
  ME_Remove(pNext);
  ME_DestroyDisplayItem(pNext);
  ME_UpdateRunFlags(editor, &p->member.run);
  if(TRACE_ON(richedit))
  {
    TRACE("Before check after join\n");
    ME_CheckCharOffsets(editor);
    TRACE("After check after join\n");
  }
}

245 246
/******************************************************************************
 * ME_SplitRun
247
 *
248
 * Splits a run into two in a given place. It also updates the screen position
249 250
 * and size (extent) of the newly generated runs.
 */
251
ME_DisplayItem *ME_SplitRun(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
252
{
253
  ME_TextEditor *editor = wc->context->editor;
254
  ME_Run *run, *run2;
255 256
  ME_Paragraph *para = &wc->pPara->member.para;
  ME_Cursor cursor = {wc->pPara, item, nVChar};
257

258 259 260 261 262 263 264 265 266 267
  assert(item->member.run.nCharOfs != -1);
  if(TRACE_ON(richedit))
  {
    TRACE("Before check before split\n");
    ME_CheckCharOffsets(editor);
    TRACE("After check before split\n");
  }

  run = &item->member.run;

268
  TRACE("Before split: %s(%d, %d)\n", debugstr_w(run->strText->szData),
269 270
        run->pt.x, run->pt.y);

271
  ME_SplitRunSimple(editor, &cursor);
272

273
  run2 = &cursor.pRun->member.run;
274

275
  ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
276

277 278
  run2->pt.x = run->pt.x+run->nWidth;
  run2->pt.y = run->pt.y;
279

280 281 282 283 284
  if(TRACE_ON(richedit))
  {
    TRACE("Before check after split\n");
    ME_CheckCharOffsets(editor);
    TRACE("After check after split\n");
285
    TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
286 287 288 289
      debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
      debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
  }

290
  return cursor.pRun;
291
}
292

293 294
/******************************************************************************
 * ME_SplitRunSimple
295
 *
296
 * Does the most basic job of splitting a run into two - it does not
297 298 299
 * update the positions and extents.
 */
ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_Cursor *cursor)
300
{
301 302
  ME_DisplayItem *run = cursor->pRun;
  ME_DisplayItem *new_run;
303
  int i;
304
  int nOffset = cursor->nOffset;
305

306
  assert(!(run->member.run.nFlags & MERF_NONTEXT));
307

308 309 310
  new_run = ME_MakeRun(run->member.run.style,
                       ME_VSplitString(run->member.run.strText, nOffset),
                       run->member.run.nFlags & MERF_SPLITMASK);
311

312 313 314
  new_run->member.run.nCharOfs = run->member.run.nCharOfs + nOffset;
  cursor->pRun = new_run;
  cursor->nOffset = 0;
315

316 317 318 319 320 321 322 323 324
  ME_InsertBefore(run->next, new_run);

  ME_UpdateRunFlags(editor, &run->member.run);
  ME_UpdateRunFlags(editor, &new_run->member.run);
  for (i = 0; i < editor->nCursors; i++) {
    if (editor->pCursors[i].pRun == run &&
        editor->pCursors[i].nOffset >= nOffset) {
      editor->pCursors[i].pRun = new_run;
      editor->pCursors[i].nOffset -= nOffset;
325 326
    }
  }
327 328
  cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
  return run;
329 330
}

331 332 333 334 335
/******************************************************************************
 * ME_MakeRun
 * 
 * A helper function to create run structures quickly.
 */   
336 337 338 339
ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
{
  ME_DisplayItem *item = ME_MakeDI(diRun);
  item->member.run.style = s;
340
  item->member.run.ole_obj = NULL;
341 342 343 344 345 346 347
  item->member.run.strText = strData;
  item->member.run.nFlags = nFlags;
  item->member.run.nCharOfs = -1;
  ME_AddRefStyle(s);
  return item;
}

348 349
/******************************************************************************
 * ME_InsertRunAtCursor
350
 *
351 352
 * Inserts a new run with given style, flags and content at a given position,
 * which is passed as a cursor structure (which consists of a run and 
353 354
 * a run-relative character offset).
 */
355 356 357 358 359 360
ME_DisplayItem *
ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
                     const WCHAR *str, int len, int flags)
{
  ME_DisplayItem *pDI;
  ME_UndoItem *pUI;
361

362 363
  if (cursor->nOffset)
    ME_SplitRunSimple(editor, cursor);
364

365
  pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
366
  if (pUI) {
367 368
    pUI->nStart = cursor->pPara->member.para.nCharOfs
                  + cursor->pRun->member.run.nCharOfs;
369
    pUI->nLen = len;
370
  }
371

372 373 374 375 376
  pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
  pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
  ME_InsertBefore(cursor->pRun, pDI);
  TRACE("Shift length:%d\n", len);
  ME_PropagateCharOffset(cursor->pRun, len);
377
  cursor->pPara->member.para.nFlags |= MEPF_REWRAP;
378 379 380
  return pDI;
}

381 382
/******************************************************************************
 * ME_UpdateRunFlags
383
 *
384
 * Determine some of run attributes given its content (style, text content).
385 386 387
 * Some flags cannot be determined by this function (MERF_GRAPHICS,
 * MERF_ENDPARA)
 */
388 389
void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
{
390 391
  ME_String *strText = run->strText;
  assert(run->nCharOfs >= 0);
392

393
  if (RUN_IS_HIDDEN(run) || run->nFlags & MERF_TABLESTART)
394 395 396 397
    run->nFlags |= MERF_HIDDEN;
  else
    run->nFlags &= ~MERF_HIDDEN;

398
  if (ME_IsSplitable(strText))
399 400 401
    run->nFlags |= MERF_SPLITTABLE;
  else
    run->nFlags &= ~MERF_SPLITTABLE;
402 403

  if (!(run->nFlags & MERF_NOTEXT)) {
404
    if (ME_IsWhitespaces(strText))
405 406 407 408
      run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
    else
    {
      run->nFlags &= ~MERF_WHITESPACE;
409

410
      if (ME_IsWSpace(strText->szData[0]))
411 412 413
        run->nFlags |= MERF_STARTWHITE;
      else
        run->nFlags &= ~MERF_STARTWHITE;
414

415
      if (ME_IsWSpace(strText->szData[strText->nLen - 1]))
416 417 418 419 420 421 422 423 424
        run->nFlags |= MERF_ENDWHITE;
      else
        run->nFlags &= ~MERF_ENDWHITE;
    }
  }
  else
    run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
}

425 426 427 428 429 430 431
/******************************************************************************
 * ME_CharFromPoint
 * 
 * Returns a character position inside the run given a run-relative
 * pixel horizontal position. This version rounds left (ie. if the second
 * character is at pixel position 8, then for cx=0..7 it returns 0).  
 */     
432
int ME_CharFromPoint(ME_Context *c, int cx, ME_Run *run)
433 434 435 436
{
  int fit = 0;
  HGDIOBJ hOldFont;
  SIZE sz;
437
  if (!run->strText->nLen || cx <= 0)
438 439
    return 0;

440 441
  if (run->nFlags & MERF_TAB ||
      (run->nFlags & (MERF_ENDCELL|MERF_ENDPARA)) == MERF_ENDCELL)
442
  {
443
    if (cx < run->nWidth/2) 
444 445 446
      return 0;
    return 1;
  }
447 448 449
  if (run->nFlags & MERF_GRAPHICS)
  {
    SIZE sz;
450
    ME_GetOLEObjectSize(c, run, &sz);
451 452 453 454
    if (cx < sz.cx)
      return 0;
    return 1;
  }
455
  hOldFont = ME_SelectStyleFont(c, run->style);
456
  
457
  if (c->editor->cPasswordMask)
458
  {
459
    ME_String *strMasked = ME_MakeStringR(c->editor->cPasswordMask, run->strText->nLen);
460
    GetTextExtentExPointW(c->hDC, strMasked->szData, run->strText->nLen,
461 462 463 464 465
      cx, &fit, NULL, &sz);
    ME_DestroyString(strMasked);
  }
  else
  {
466
    GetTextExtentExPointW(c->hDC, run->strText->szData, run->strText->nLen,
467 468 469
      cx, &fit, NULL, &sz);
  }
  
470
  ME_UnselectStyleFont(c, run->style, hOldFont);
471

472 473 474
  return fit;
}

475 476
/******************************************************************************
 * ME_CharFromPointCursor
477
 *
478
 * Returns a character position inside the run given a run-relative
479 480
 * pixel horizontal position. This version rounds to the nearest character edge
 * (ie. if the second character is at pixel position 8, then for cx=0..3
481
 * it returns 0, and for cx=4..7 it returns 1).
482
 *
483
 * It is used for mouse click handling, for better usability (and compatibility
484 485
 * with the native control).
 */
486 487
int ME_CharFromPointCursor(ME_TextEditor *editor, int cx, ME_Run *run)
{
488 489
  ME_String *strRunText;
  /* This could point to either the run's real text, or it's masked form in a password control */
490 491

  int fit = 0;
492
  ME_Context c;
493 494
  HGDIOBJ hOldFont;
  SIZE sz, sz2, sz3;
495
  if (!run->strText->nLen || cx <= 0)
496 497
    return 0;

498
  if (run->nFlags & (MERF_TAB | MERF_ENDCELL))
499 500 501 502 503
  {
    if (cx < run->nWidth/2)
      return 0;
    return 1;
  }
504
  ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
505 506 507
  if (run->nFlags & MERF_GRAPHICS)
  {
    SIZE sz;
508
    ME_GetOLEObjectSize(&c, run, &sz);
509
    ME_DestroyContext(&c);
510 511 512 513
    if (cx < sz.cx/2)
      return 0;
    return 1;
  }
514

515
  if (editor->cPasswordMask)
516
    strRunText = ME_MakeStringR(editor->cPasswordMask, run->strText->nLen);
517 518
  else
    strRunText = run->strText;
519

520 521 522
  hOldFont = ME_SelectStyleFont(&c, run->style);
  GetTextExtentExPointW(c.hDC, strRunText->szData, strRunText->nLen,
                        cx, &fit, NULL, &sz);
523
  if (fit != strRunText->nLen)
524
  {
525
    GetTextExtentPoint32W(c.hDC, strRunText->szData, fit, &sz2);
526
    GetTextExtentPoint32W(c.hDC, strRunText->szData, fit + 1, &sz3);
527
    if (cx >= (sz2.cx+sz3.cx)/2)
528
      fit = fit + 1;
529
  }
530

531 532
  if (editor->cPasswordMask)
    ME_DestroyString(strRunText);
533

534
  ME_UnselectStyleFont(&c, run->style, hOldFont);
535
  ME_DestroyContext(&c);
536 537 538
  return fit;
}

539 540 541 542 543 544 545 546
/******************************************************************************
 * ME_GetTextExtent
 *
 * Finds a width and a height of the text using a specified style
 */
static void ME_GetTextExtent(ME_Context *c, LPCWSTR szText, int nChars, ME_Style *s, SIZE *size)
{
  HGDIOBJ hOldFont;
547 548 549 550 551 552 553 554
  if (c->hDC) {
    hOldFont = ME_SelectStyleFont(c, s);
    GetTextExtentPoint32W(c->hDC, szText, nChars, size);
    ME_UnselectStyleFont(c, s, hOldFont);
  } else {
    size->cx = 0;
    size->cy = 0;
  }
555 556
}

557 558
/******************************************************************************
 * ME_PointFromChar
559
 *
560 561
 * Returns a run-relative pixel position given a run-relative character
 * position (character offset)
562
 */
563 564 565
int ME_PointFromChar(ME_TextEditor *editor, ME_Run *pRun, int nOffset)
{
  SIZE size;
566
  ME_Context c;
567 568
  ME_String *strRunText;
  /* This could point to either the run's real text, or it's masked form in a password control */
569

570
  ME_InitContext(&c, editor, ITextHost_TxGetDC(editor->texthost));
571 572
  if (pRun->nFlags & MERF_GRAPHICS)
  {
573 574
    if (nOffset)
      ME_GetOLEObjectSize(&c, pRun, &size);
575
    ME_DestroyContext(&c);
576
    return nOffset != 0;
577 578
  } else if (pRun->nFlags & MERF_ENDPARA) {
    nOffset = 0;
579
  }
580 581

  if (editor->cPasswordMask)
582
    strRunText = ME_MakeStringR(editor->cPasswordMask, pRun->strText->nLen);
583 584
  else
    strRunText = pRun->strText;
585

586
  ME_GetTextExtent(&c,  strRunText->szData, nOffset, pRun->style, &size);
587
  ME_DestroyContext(&c);
588 589
  if (editor->cPasswordMask)
    ME_DestroyString(strRunText);
590 591 592
  return size.cx;
}

593 594 595
/******************************************************************************
 * ME_GetRunSizeCommon
 * 
596 597 598
 * Finds width, height, ascent and descent of a run, up to given character
 * (nLen).
 */
599
static SIZE ME_GetRunSizeCommon(ME_Context *c, const ME_Paragraph *para, ME_Run *run, int nLen,
600
                                int startx, int *pAscent, int *pDescent)
601 602
{
  SIZE size;
603
  int nMaxLen = run->strText->nLen;
604 605 606

  if (nLen>nMaxLen)
    nLen = nMaxLen;
607 608

  /* FIXME the following call also ensures that TEXTMETRIC structure is filled
609
   * this is wasteful for MERF_NONTEXT runs, but that shouldn't matter
610 611
   * in practice
   */
612 613 614 615 616 617 618 619 620 621 622
  
  if (c->editor->cPasswordMask)
  {
    ME_String *szMasked = ME_MakeStringR(c->editor->cPasswordMask,nLen);
    ME_GetTextExtent(c, szMasked->szData, nLen,run->style, &size); 
    ME_DestroyString(szMasked);
  }
  else
  {
    ME_GetTextExtent(c, run->strText->szData, nLen, run->style, &size);
  }
623 624 625 626 627 628
  *pAscent = run->style->tm.tmAscent;
  *pDescent = run->style->tm.tmDescent;
  size.cy = *pAscent + *pDescent;

  if (run->nFlags & MERF_TAB)
  {
629
    int pos = 0, i = 0, ppos, shift = 0;
630
    PARAFORMAT2 *pFmt = para->pFmt;
631

632 633 634 635
    if (c->editor->bEmulateVersion10 && /* v1.0 - 3.0 */
        pFmt->dwMask & PFM_TABLE && pFmt->wEffects & PFE_TABLE)
      /* The horizontal gap shifts the tab positions to leave the gap. */
      shift = pFmt->dxOffset * 2;
636 637 638
    do {
      if (i < pFmt->cTabCount)
      {
639 640 641 642 643
        /* Only one side of the horizontal gap is needed at the end of
         * the table row. */
        if (i == pFmt->cTabCount -1)
          shift = shift >> 1;
        pos = shift + (pFmt->rgxTabs[i]&0x00FFFFFF);
644 645 646 647
        i++;
      }
      else
      {
648
        pos += lDefaultTab - (pos % lDefaultTab);
649
      }
650
      ppos = ME_twips2pointsX(c, pos);
651 652
      if (ppos > startx + run->pt.x) {
        size.cx = ppos - startx - run->pt.x;
653 654 655 656 657 658
        break;
      }
    } while(1);
    size.cy = *pAscent + *pDescent;
    return size;
  }
659 660
  if (run->nFlags & MERF_GRAPHICS)
  {
661
    ME_GetOLEObjectSize(c, run, &size);
662 663 664
    if (size.cy > *pAscent)
      *pAscent = size.cy;
    /* descent is unchanged */
665 666 667 668 669
    return size;
  }
  return size;
}

670 671 672 673 674 675
/******************************************************************************
 * ME_GetRunSize
 * 
 * Finds width and height (but not ascent and descent) of a part of the run
 * up to given character.    
 */     
676 677
SIZE ME_GetRunSize(ME_Context *c, const ME_Paragraph *para,
                   ME_Run *run, int nLen, int startx)
678 679
{
  int asc, desc;
680
  return ME_GetRunSizeCommon(c, para, run, nLen, startx, &asc, &desc);
681 682
}

683 684 685 686 687 688 689
/******************************************************************************
 * ME_CalcRunExtent
 * 
 * Updates the size of the run (fills width, ascent and descent). The height
 * is calculated based on whole row's ascent and descent anyway, so no need
 * to use it here.        
 */     
690
void ME_CalcRunExtent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
691
{
692 693 694 695
  if (run->nFlags & MERF_HIDDEN)
    run->nWidth = 0;
  else
  {
696
    int nEnd = run->strText->nLen;
697 698
    SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, startx,
                                    &run->nAscent, &run->nDescent);
699 700 701 702
    run->nWidth = size.cx;
    if (!size.cx)
      WARN("size.cx == 0\n");
  }
703 704
}

705 706
/******************************************************************************
 * ME_SetSelectionCharFormat
707
 *
708 709
 * Applies a style change, either to a current selection, or to insert cursor
 * (ie. the style next typed characters will use).
710
 */
711 712
void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
{
713
  if (!ME_IsSelection(editor))
714 715 716 717 718 719 720
  {
    ME_Style *s;
    if (!editor->pBuffer->pCharStyle)
      editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
    s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
    ME_ReleaseStyle(editor->pBuffer->pCharStyle);
    editor->pBuffer->pCharStyle = s;
721 722 723 724
  } else {
    ME_Cursor *from, *to;
    ME_GetSelection(editor, &from, &to);
    ME_SetCharFormat(editor, from, to, pFmt);
725 726 727
  }
}

728 729
/******************************************************************************
 * ME_SetCharFormat
730
 *
731
 * Applies a style change to the specified part of the text
732 733 734 735 736 737 738 739 740
 *
 * The start and end cursors specify the part of the text.  These cursors will
 * be updated to stay valid, but this function may invalidate other
 * non-selection cursors. The end cursor may be NULL to specify all the text
 * following the start cursor.
 *
 * If no text is selected, then nothing is done.
 */
void ME_SetCharFormat(ME_TextEditor *editor, ME_Cursor *start, ME_Cursor *end, CHARFORMAT2W *pFmt)
741 742
{
  ME_DisplayItem *para;
743 744
  ME_DisplayItem *run;
  ME_DisplayItem *end_run = NULL;
745

746 747
  if (end && start->pRun == end->pRun && start->nOffset == end->nOffset)
    return;
748

749 750 751 752 753
  if (start->nOffset)
  {
    /* SplitRunSimple may or may not update the cursors, depending on whether they
     * are selection cursors, but we need to make sure they are valid. */
    int split_offset = start->nOffset;
754
    ME_DisplayItem *split_run = ME_SplitRunSimple(editor, start);
755 756 757 758 759 760 761 762
    if (end && end->pRun == split_run)
    {
      end->pRun = start->pRun;
      end->nOffset -= split_offset;
    }
  }

  if (end && end->nOffset)
763 764
    ME_SplitRunSimple(editor, end);
  end_run = end ? end->pRun : NULL;
765

766 767
  run = start->pRun;
  para = start->pPara;
768
  para->member.para.nFlags |= MEPF_REWRAP;
769

770
  while(run != end_run)
771 772
  {
    ME_UndoItem *undo = NULL;
773
    ME_Style *new_style = ME_ApplyStyle(run->member.run.style, pFmt);
774 775 776
    /* ME_DumpStyle(new_style); */
    undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
    if (undo) {
777 778 779
      undo->nStart = run->member.run.nCharOfs+para->member.para.nCharOfs;
      undo->nLen = run->member.run.strText->nLen;
      undo->di.member.ustyle = run->member.run.style;
780
      /* we'd have to addref undo...ustyle and release tmp...style
781 782 783
         but they'd cancel each other out so we can do nothing instead */
    }
    else
784 785 786 787
      ME_ReleaseStyle(run->member.run.style);
    run->member.run.style = new_style;
    run = ME_FindItemFwd(run, diRunOrParagraph);
    if (run && run->type == diParagraph)
788
    {
789 790 791
      para = run;
      run = ME_FindItemFwd(run, diRun);
      if (run != end_run)
792
        para->member.para.nFlags |= MEPF_REWRAP;
793 794 795 796
    }
  }
}

797 798 799 800 801
/******************************************************************************
 * ME_SetDefaultCharFormat
 * 
 * Applies a style change to the default character style.
 */     
802 803
void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
{
804 805
  ME_Style *style;

806 807 808 809 810 811 812 813 814
  assert(mod->cbSize == sizeof(CHARFORMAT2W));
  style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
  editor->pBuffer->pDefaultStyle->fmt = style->fmt;
  editor->pBuffer->pDefaultStyle->tm = style->tm;
  ME_ReleaseStyle(style);
  ME_MarkAllForWrapping(editor);
  /*  pcf = editor->pBuffer->pDefaultStyle->fmt; */
}

815
static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
816 817
{
  ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
818 819 820 821 822 823 824 825 826 827
  if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_CF1UNDERLINE))
  {
    pFmt->dwMask |= CFM_UNDERLINE;
    pFmt->dwEffects |= CFE_UNDERLINE;
  }
  if ((pFmt->dwMask & CFM_UNDERLINETYPE) && (pFmt->bUnderlineType == CFU_UNDERLINENONE))
  {
    pFmt->dwMask |= CFM_UNDERLINE;
    pFmt->dwEffects &= ~CFE_UNDERLINE;
  }
828 829
}

830 831 832 833 834 835
/******************************************************************************
 * ME_GetDefaultCharFormat
 * 
 * Retrieves the current default character style (the one applied where no
 * other style was applied) .
 */     
836 837 838 839 840
void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
{
  ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
}

841 842
/******************************************************************************
 * ME_GetSelectionCharFormat
843
 *
844
 * If selection exists, it returns all style elements that are set consistently
845 846
 * in the whole selection. If not, it just returns the current style.
 */
847 848
void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
{
849 850
  ME_Cursor *from, *to;
  if (!ME_IsSelection(editor) && editor->pBuffer->pCharStyle)
851 852 853 854
  {
    ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
    return;
  }
855 856
  ME_GetSelection(editor, &from, &to);
  ME_GetCharFormat(editor, from, to, pFmt);
857 858
}

859 860
/******************************************************************************
 * ME_GetCharFormat
861
 *
862
 * Returns the style consisting of those attributes which are consistently set
863 864
 * in the whole character range.
 */
865 866
void ME_GetCharFormat(ME_TextEditor *editor, const ME_Cursor *from,
                      const ME_Cursor *to, CHARFORMAT2W *pFmt)
867 868 869
{
  ME_DisplayItem *run, *run_end;
  CHARFORMAT2W tmp;
870

871 872 873
  run = from->pRun;
  /* special case - if selection is empty, take previous char's formatting */
  if (from->pRun == to->pRun && from->nOffset == to->nOffset)
874
  {
875
    if (!from->nOffset)
876 877 878 879 880 881 882 883 884 885
    {
      ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
      if (tmp_run->type == diRun) {
        ME_GetRunCharFormat(editor, tmp_run, pFmt);
        return;
      }
    }
    ME_GetRunCharFormat(editor, run, pFmt);
    return;
  }
886 887 888 889

  run_end = to->pRun;
  if (!to->nOffset)
    run_end = ME_FindItemBack(run_end, diRun);
890

891
  ME_GetRunCharFormat(editor, run, pFmt);
892

893
  if (run == run_end) return;
894

895 896
  do {
    /* FIXME add more style feature comparisons */
897 898
    DWORD dwAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
    DWORD dwEffects = CFM_BOLD | CFM_ITALIC | CFM_UNDERLINE | CFM_STRIKEOUT | CFM_PROTECTED | CFM_LINK | CFM_SUPERSCRIPT;
899 900

    run = ME_FindItemFwd(run, diRun);
901

902 903 904
    ZeroMemory(&tmp, sizeof(tmp));
    tmp.cbSize = sizeof(tmp);
    ME_GetRunCharFormat(editor, run, &tmp);
905

906
    assert((tmp.dwMask & dwAttribs) == dwAttribs);
907 908 909 910 911 912 913 914
    /* reset flags that differ */

    if (pFmt->yHeight != tmp.yHeight)
      pFmt->dwMask &= ~CFM_SIZE;
    if (pFmt->dwMask & CFM_FACE)
    {
      if (!(tmp.dwMask & CFM_FACE))
        pFmt->dwMask &= ~CFM_FACE;
915 916
      else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
          pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
917 918 919 920
        pFmt->dwMask &= ~CFM_FACE;
    }
    if (pFmt->yHeight != tmp.yHeight)
      pFmt->dwMask &= ~CFM_SIZE;
921 922
    if (pFmt->bUnderlineType != tmp.bUnderlineType)
      pFmt->dwMask &= ~CFM_UNDERLINETYPE;
923 924 925 926 927 928 929 930
    if (pFmt->dwMask & CFM_COLOR)
    {
      if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
      {
        if (pFmt->crTextColor != tmp.crTextColor)
          pFmt->dwMask &= ~CFM_COLOR;
      }
    }
931

932
    pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & dwEffects);
933
    pFmt->dwEffects = tmp.dwEffects;
934

935 936
  } while(run != run_end);
}