shell32: Fix the Ukrainian translation.
[wine] / dlls / riched20 / run.c
1 /*
2  * RichEdit - operations on runs (diRun, rectangular pieces of paragraphs).
3  * Splitting/joining runs. Adjusting offsets after deleting/adding content.
4  * Character/pixel conversions.
5  *
6  * Copyright 2004 by Krzysztof Foltman
7  * Copyright 2006 by Phil Krylov
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "editor.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
27 WINE_DECLARE_DEBUG_CHANNEL(richedit_check);
28 WINE_DECLARE_DEBUG_CHANNEL(richedit_lists);
29
30 /******************************************************************************
31  * ME_CanJoinRuns
32  *
33  * Returns 1 if two runs can be safely merged into one, 0 otherwise.
34  */ 
35 int ME_CanJoinRuns(const ME_Run *run1, const ME_Run *run2)
36 {
37   if ((run1->nFlags | run2->nFlags) & MERF_NOJOIN)
38     return 0;
39   if (run1->style != run2->style)
40     return 0;
41   if ((run1->nFlags & MERF_STYLEFLAGS) != (run2->nFlags & MERF_STYLEFLAGS))
42     return 0;
43   return 1;
44 }
45
46 void ME_SkipAndPropagateCharOffset(ME_DisplayItem *p, int shift)
47 {
48   p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
49   assert(p);
50   ME_PropagateCharOffset(p, shift);
51 }
52
53 /******************************************************************************
54  * ME_PropagateCharOffsets
55  *
56  * Shifts (increases or decreases) character offset (relative to beginning of 
57  * the document) of the part of the text starting from given place.  
58  */ 
59 void ME_PropagateCharOffset(ME_DisplayItem *p, int shift)
60 {
61         /* Runs in one paragraph contain character offset relative to their owning
62          * paragraph. If we start the shifting from the run, we need to shift
63          * all the relative offsets until the end of the paragraph
64          */                 
65   if (p->type == diRun) /* propagate in all runs in this para */
66   {
67     TRACE("PropagateCharOffset(%s, %d)\n", debugstr_w(p->member.run.strText->szData), shift);
68     do {
69       p->member.run.nCharOfs += shift;
70       assert(p->member.run.nCharOfs >= 0);
71       p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
72     } while(p->type == diRun);
73   }
74         /* Runs in next paragraphs don't need their offsets updated, because they, 
75          * again, those offsets are relative to their respective paragraphs.
76          * Instead of that, we're updating paragraphs' character offsets.         
77          */                 
78   if (p->type == diParagraph) /* propagate in all next paras */
79   {
80     do {
81       p->member.para.nCharOfs += shift;
82       assert(p->member.para.nCharOfs >= 0);
83       p = p->member.para.next_para;
84     } while(p->type == diParagraph);
85   }
86   /* diTextEnd also has character offset in it, which makes finding text length
87    * easier. But it needs to be up to date first.
88    */
89   if (p->type == diTextEnd)
90   {
91     p->member.para.nCharOfs += shift;
92     assert(p->member.para.nCharOfs >= 0);
93   }
94 }
95
96 /******************************************************************************
97  * ME_CheckCharOffsets
98  * 
99  * Checks if editor lists' validity and optionally dumps the document structure
100  */      
101 void ME_CheckCharOffsets(ME_TextEditor *editor)
102 {
103   ME_DisplayItem *p = editor->pBuffer->pFirst;
104   int ofs = 0, ofsp = 0;
105   if(TRACE_ON(richedit_lists))
106   {
107     TRACE_(richedit_lists)("---\n");
108     ME_DumpDocument(editor->pBuffer);
109   }
110   do {
111     p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
112     switch(p->type) {
113       case diTextEnd:
114         TRACE_(richedit_check)("tend, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
115         assert(ofsp+ofs == p->member.para.nCharOfs);
116         return;
117       case diParagraph:
118         TRACE_(richedit_check)("para, real ofsp = %d, counted = %d\n", p->member.para.nCharOfs, ofsp+ofs);
119         assert(ofsp+ofs == p->member.para.nCharOfs);
120         ofsp = p->member.para.nCharOfs;
121         ofs = 0;
122         break;
123       case diRun:
124         TRACE_(richedit_check)("run, real ofs = %d (+ofsp = %d), counted = %d, len = %d, txt = \"%s\", flags=%08x, fx&mask = %08x\n",
125           p->member.run.nCharOfs, p->member.run.nCharOfs+ofsp, ofsp+ofs,
126           p->member.run.strText->nLen, debugstr_w(p->member.run.strText->szData),
127           p->member.run.nFlags,
128           p->member.run.style->fmt.dwMask & p->member.run.style->fmt.dwEffects);
129         assert(ofs == p->member.run.nCharOfs);
130         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       default:
138         assert(0);
139     }
140   } while(1);
141 }
142
143 /******************************************************************************
144  * ME_CharOfsFromRunOfs
145  * 
146  * Converts a character position relative to the start of the run, to a
147  * character position relative to the start of the document.
148  * Kind of a "local to global" offset conversion.   
149  */      
150 int ME_CharOfsFromRunOfs(ME_TextEditor *editor, ME_DisplayItem *pRun, int nOfs)
151 {
152   ME_DisplayItem *pPara;
153
154   assert(pRun->type == diRun);
155   assert(pRun->member.run.nCharOfs != -1);
156
157   pPara = ME_FindItemBack(pRun, diParagraph);
158   assert(pPara);
159   assert(pPara->type==diParagraph);
160   return pPara->member.para.nCharOfs + pRun->member.run.nCharOfs
161     + ME_VPosToPos(pRun->member.run.strText, nOfs);
162 }
163
164 /******************************************************************************
165  * ME_CursorFromCharOfs
166  * 
167  * Converts a character offset (relative to the start of the document) to
168  * a cursor structure (which contains a run and a position relative to that 
169  * run).   
170  */      
171 void ME_CursorFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_Cursor *pCursor)
172 {
173   ME_RunOfsFromCharOfs(editor, nCharOfs, &pCursor->pRun, &pCursor->nOffset);
174 }
175
176 /******************************************************************************
177  * ME_RunOfsFromCharOfs
178  * 
179  * Find a run and relative character offset given an absolute character offset
180  * (absolute offset being an offset relative to the start of the document).
181  * Kind of a "global to local" offset conversion.    
182  */      
183 void ME_RunOfsFromCharOfs(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem **ppRun, int *pOfs)
184 {
185   ME_DisplayItem *pPara;
186   int nParaOfs;
187
188   pPara = editor->pBuffer->pFirst->member.para.next_para;
189   assert(pPara);
190   assert(ppRun);
191   assert(pOfs);
192   while (pPara->type == diParagraph)
193   {
194     nParaOfs = pPara->member.para.nCharOfs;
195     assert(nCharOfs >= nParaOfs);
196
197     if (nCharOfs < pPara->member.para.next_para->member.para.nCharOfs)
198     {
199       int eollen = 1;
200       *ppRun = ME_FindItemFwd(pPara, diRun);
201       assert(*ppRun);
202       while (!((*ppRun)->member.run.nFlags & MERF_ENDPARA))
203       {
204         ME_DisplayItem *pNext = ME_FindItemFwd(*ppRun, diRun);
205         assert(pNext);
206         assert(pNext->type == diRun);
207         if (nCharOfs < nParaOfs + pNext->member.run.nCharOfs) {
208           *pOfs = ME_PosToVPos((*ppRun)->member.run.strText,
209             nCharOfs - nParaOfs - (*ppRun)->member.run.nCharOfs);
210           return;
211         }
212         *ppRun = pNext;
213       }
214       /* Recover proper character length of this line break */
215       eollen = (*ppRun)->member.run.nCR + (*ppRun)->member.run.nLF;
216       if (nCharOfs >= nParaOfs + (*ppRun)->member.run.nCharOfs &&
217         nCharOfs < nParaOfs + (*ppRun)->member.run.nCharOfs + eollen) {
218         /* FIXME: Might cause problems when actually requiring an offset in the
219            middle of a run that is considered a single line break */
220         *pOfs = 0;
221         return;
222       }
223     }
224     pPara = pPara->member.para.next_para;
225   }
226   *ppRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
227   *pOfs = 0;
228   assert((*ppRun)->member.run.nFlags & MERF_ENDPARA);
229 }
230
231 /******************************************************************************
232  * ME_JoinRuns
233  * 
234  * Merges two adjacent runs, the one given as a parameter and the next one.
235  */    
236 void ME_JoinRuns(ME_TextEditor *editor, ME_DisplayItem *p)
237 {
238   ME_DisplayItem *pNext = p->next;
239   int i;
240   assert(p->type == diRun && pNext->type == diRun);
241   assert(p->member.run.nCharOfs != -1);
242   ME_GetParagraph(p)->member.para.nFlags |= MEPF_REWRAP;
243
244   /* if we were at the end of screen line, and the next run is in the new
245          * line, then it's not the end of the line anymore */  
246         if (editor->bCaretAtEnd && editor->pCursors[0].pRun == pNext)
247     editor->bCaretAtEnd = FALSE;
248         /* Update all cursors so that they don't contain the soon deleted run */
249   for (i=0; i<editor->nCursors; i++) {
250     if (editor->pCursors[i].pRun == pNext) {
251       editor->pCursors[i].pRun = p;
252       editor->pCursors[i].nOffset += ME_StrVLen(p->member.run.strText);
253     }
254   }
255
256   ME_AppendString(p->member.run.strText, pNext->member.run.strText);
257   ME_Remove(pNext);
258   ME_DestroyDisplayItem(pNext);
259   ME_UpdateRunFlags(editor, &p->member.run);
260   if(TRACE_ON(richedit))
261   {
262     TRACE("Before check after join\n");
263     ME_CheckCharOffsets(editor);
264     TRACE("After check after join\n");
265   }
266 }
267
268 /******************************************************************************
269  * ME_SplitRun
270  * 
271  * Splits a run into two in a given place. It also updates the screen position
272  * and size (extent) of the newly generated runs.  
273  */    
274 ME_DisplayItem *ME_SplitRun(ME_WrapContext *wc, ME_DisplayItem *item, int nVChar)
275 {
276   ME_TextEditor *editor = wc->context->editor;
277   ME_DisplayItem *item2 = NULL;
278   ME_Run *run, *run2;
279   ME_Paragraph *para = &ME_GetParagraph(item)->member.para;
280
281   assert(item->member.run.nCharOfs != -1);
282   if(TRACE_ON(richedit))
283   {
284     TRACE("Before check before split\n");
285     ME_CheckCharOffsets(editor);
286     TRACE("After check before split\n");
287   }
288
289   run = &item->member.run;
290
291   TRACE("Before split: %s(%d, %d)\n", debugstr_w(run->strText->szData),
292         run->pt.x, run->pt.y);
293
294   item2 = ME_SplitRunSimple(editor, item, nVChar);
295
296   run2 = &item2->member.run;
297
298   ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run);
299   ME_CalcRunExtent(wc->context, para, wc->nRow ? wc->nLeftMargin : wc->nFirstMargin, run2);
300
301   run2->pt.x = run->pt.x+run->nWidth;
302   run2->pt.y = run->pt.y;
303
304   if(TRACE_ON(richedit))
305   {
306     TRACE("Before check after split\n");
307     ME_CheckCharOffsets(editor);
308     TRACE("After check after split\n");
309     TRACE("After split: %s(%d, %d), %s(%d, %d)\n",
310       debugstr_w(run->strText->szData), run->pt.x, run->pt.y,
311       debugstr_w(run2->strText->szData), run2->pt.x, run2->pt.y);
312   }
313
314   return item2;
315 }
316
317 /******************************************************************************
318  * ME_SplitRunSimple
319  * 
320  * Does the most basic job of splitting a run into two - it does not
321  * update the positions and extents.    
322  */    
323 ME_DisplayItem *ME_SplitRunSimple(ME_TextEditor *editor, ME_DisplayItem *item, int nVChar)
324 {
325   ME_Run *run = &item->member.run;
326   ME_DisplayItem *item2;
327   ME_Run *run2;
328   int i;
329   assert(nVChar > 0 && nVChar < ME_StrVLen(run->strText));
330   assert(item->type == diRun);
331   assert(!(item->member.run.nFlags & MERF_NONTEXT));
332   assert(item->member.run.nCharOfs != -1);
333
334   item2 = ME_MakeRun(run->style,
335       ME_VSplitString(run->strText, nVChar), run->nFlags&MERF_SPLITMASK);
336
337   item2->member.run.nCharOfs = item->member.run.nCharOfs+
338     ME_VPosToPos(item->member.run.strText, nVChar);
339
340   run2 = &item2->member.run;
341   ME_InsertBefore(item->next, item2);
342
343   ME_UpdateRunFlags(editor, run);
344   ME_UpdateRunFlags(editor, run2);
345   for (i=0; i<editor->nCursors; i++) {
346     if (editor->pCursors[i].pRun == item &&
347         editor->pCursors[i].nOffset >= nVChar) {
348       assert(item2->type == diRun);
349       editor->pCursors[i].pRun = item2;
350       editor->pCursors[i].nOffset -= nVChar;
351     }
352   }
353   ME_GetParagraph(item)->member.para.nFlags |= MEPF_REWRAP;
354   return item2;
355 }
356
357 /******************************************************************************
358  * ME_MakeRun
359  * 
360  * A helper function to create run structures quickly.
361  */   
362 ME_DisplayItem *ME_MakeRun(ME_Style *s, ME_String *strData, int nFlags)
363 {
364   ME_DisplayItem *item = ME_MakeDI(diRun);
365   item->member.run.style = s;
366   item->member.run.ole_obj = NULL;
367   item->member.run.strText = strData;
368   item->member.run.nFlags = nFlags;
369   item->member.run.nCharOfs = -1;
370   ME_AddRefStyle(s);
371   return item;
372 }
373
374 /******************************************************************************
375  * ME_InsertRun
376  * 
377  * Inserts a run at a given character position (offset).
378  */   
379 ME_DisplayItem *ME_InsertRun(ME_TextEditor *editor, int nCharOfs, ME_DisplayItem *pItem)
380 {
381   ME_Cursor tmp;
382   ME_DisplayItem *pDI;
383
384   assert(pItem->type == diRun || pItem->type == diUndoInsertRun);
385
386   ME_CursorFromCharOfs(editor, nCharOfs, &tmp);
387   pDI = ME_InsertRunAtCursor(editor, &tmp, pItem->member.run.style,
388                              pItem->member.run.strText->szData,
389                              pItem->member.run.strText->nLen,
390                              pItem->member.run.nFlags);
391   
392   return pDI;
393 }
394
395 /******************************************************************************
396  * ME_InsertRunAtCursor
397  * 
398  * Inserts a new run with given style, flags and content at a given position,
399  * which is passed as a cursor structure (which consists of a run and 
400  * a run-relative character offset). 
401  */   
402 ME_DisplayItem *
403 ME_InsertRunAtCursor(ME_TextEditor *editor, ME_Cursor *cursor, ME_Style *style,
404                      const WCHAR *str, int len, int flags)
405 {
406   ME_DisplayItem *pDI;
407   ME_UndoItem *pUI;
408   
409   if (cursor->nOffset) {
410         /* We're inserting at the middle of the existing run, which means that
411                  * that run must be split. It isn't always necessary, but */
412     cursor->pRun = ME_SplitRunSimple(editor, cursor->pRun, cursor->nOffset);
413     cursor->nOffset = 0;
414   }
415   
416   pUI = ME_AddUndoItem(editor, diUndoDeleteRun, NULL);
417   if (pUI) {
418     pUI->nStart = (ME_GetParagraph(cursor->pRun)->member.para.nCharOfs
419                    + cursor->pRun->member.run.nCharOfs);
420     pUI->nLen = len;
421   }
422   
423   pDI = ME_MakeRun(style, ME_MakeStringN(str, len), flags);
424   pDI->member.run.nCharOfs = cursor->pRun->member.run.nCharOfs;
425   ME_InsertBefore(cursor->pRun, pDI);
426   TRACE("Shift length:%d\n", len);
427   ME_PropagateCharOffset(cursor->pRun, len);
428   ME_GetParagraph(cursor->pRun)->member.para.nFlags |= MEPF_REWRAP;
429   return pDI;
430 }
431
432 /******************************************************************************
433  * ME_UpdateRunFlags
434  * 
435  * Determine some of run attributes given its content (style, text content).
436  * Some flags cannot be determined by this function (MERF_GRAPHICS, 
437  * MERF_ENDPARA)     
438  */ 
439 void ME_UpdateRunFlags(ME_TextEditor *editor, ME_Run *run)
440 {
441   assert(run->nCharOfs != -1);
442
443   if (RUN_IS_HIDDEN(run))
444     run->nFlags |= MERF_HIDDEN;
445   else
446     run->nFlags &= ~MERF_HIDDEN;
447
448   if (ME_IsSplitable(run->strText))
449     run->nFlags |= MERF_SPLITTABLE;
450   else
451     run->nFlags &= ~MERF_SPLITTABLE;
452
453   if (!(run->nFlags & MERF_NOTEXT)) {
454     if (ME_IsWhitespaces(run->strText))
455       run->nFlags |= MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE;
456     else
457     {
458       run->nFlags &= ~MERF_WHITESPACE;
459
460       if (ME_IsWSpace(ME_GetCharFwd(run->strText,0)))
461         run->nFlags |= MERF_STARTWHITE;
462       else
463         run->nFlags &= ~MERF_STARTWHITE;
464
465       if (ME_IsWSpace(ME_GetCharBack(run->strText,0)))
466         run->nFlags |= MERF_ENDWHITE;
467       else
468         run->nFlags &= ~MERF_ENDWHITE;
469     }
470   }
471   else
472     run->nFlags &= ~(MERF_WHITESPACE | MERF_STARTWHITE | MERF_ENDWHITE);
473 }
474
475 /******************************************************************************
476  * ME_CharFromPoint
477  * 
478  * Returns a character position inside the run given a run-relative
479  * pixel horizontal position. This version rounds left (ie. if the second
480  * character is at pixel position 8, then for cx=0..7 it returns 0).  
481  */     
482 int ME_CharFromPoint(ME_Context *c, int cx, ME_Run *run)
483 {
484   int fit = 0;
485   HGDIOBJ hOldFont;
486   SIZE sz;
487   if (!run->strText->nLen)
488     return 0;
489
490   if (run->nFlags & (MERF_TAB | MERF_CELL))
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_CELL))
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;
675     PARAFORMAT2 *pFmt = para->pFmt;
676
677     do {
678       if (i < pFmt->cTabCount)
679       {
680         pos = pFmt->rgxTabs[i]&0x00FFFFFF;
681         i++;
682       }
683       else
684       {
685         pos += 720-(pos%720);
686       }
687       ppos = ME_twips2pointsX(c, pos);
688       if (ppos > startx + run->pt.x) {
689         size.cx = ppos - startx - run->pt.x;
690         break;
691       }
692     } while(1);
693     size.cy = *pAscent + *pDescent;
694     return size;
695   }
696   if (run->nFlags & MERF_GRAPHICS)
697   {
698     ME_GetOLEObjectSize(c, run, &size);
699     if (size.cy > *pAscent)
700       *pAscent = size.cy;
701     /* descent is unchanged */
702     return size;
703   }
704   if (run->nFlags & MERF_CELL)
705   {
706     size.cx = ME_twips2pointsX(c, run->pCell->nRightBoundary) - run->pt.x;
707     return size;
708   }
709   return size;
710 }
711
712 /******************************************************************************
713  * ME_GetRunSize
714  * 
715  * Finds width and height (but not ascent and descent) of a part of the run
716  * up to given character.    
717  */     
718 SIZE ME_GetRunSize(ME_Context *c, const ME_Paragraph *para,
719                    ME_Run *run, int nLen, int startx)
720 {
721   int asc, desc;
722   return ME_GetRunSizeCommon(c, para, run, nLen, startx, &asc, &desc);
723 }
724
725 /******************************************************************************
726  * ME_CalcRunExtent
727  * 
728  * Updates the size of the run (fills width, ascent and descent). The height
729  * is calculated based on whole row's ascent and descent anyway, so no need
730  * to use it here.        
731  */     
732 void ME_CalcRunExtent(ME_Context *c, const ME_Paragraph *para, int startx, ME_Run *run)
733 {
734   if (run->nFlags & MERF_HIDDEN)
735     run->nWidth = 0;
736   else
737   {
738     int nEnd = ME_StrVLen(run->strText);
739     SIZE size = ME_GetRunSizeCommon(c, para, run, nEnd, startx,
740                                     &run->nAscent, &run->nDescent);
741     run->nWidth = size.cx;
742     if (!size.cx)
743       WARN("size.cx == 0\n");
744   }
745 }
746
747 /******************************************************************************
748  * ME_MustBeWrapped
749  * 
750  * This should ensure that the given paragraph is wrapped so that its screen
751  * row structure may be used. But it doesn't, yet. 
752  */     
753 void ME_MustBeWrapped(ME_Context *c, ME_DisplayItem *para)
754 {
755   assert(para->type == diParagraph);
756   /* FIXME */
757 }
758
759 /******************************************************************************
760  * ME_SetSelectionCharFormat
761  * 
762  * Applies a style change, either to a current selection, or to insert cursor
763  * (ie. the style next typed characters will use).
764  */     
765 void ME_SetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
766 {
767   int nFrom, nTo;
768   ME_GetSelection(editor, &nFrom, &nTo);
769   if (nFrom == nTo)
770   {
771     ME_Style *s;
772     if (!editor->pBuffer->pCharStyle)
773       editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
774     s = ME_ApplyStyle(editor->pBuffer->pCharStyle, pFmt);
775     ME_ReleaseStyle(editor->pBuffer->pCharStyle);
776     editor->pBuffer->pCharStyle = s;
777   }
778   else
779     ME_SetCharFormat(editor, nFrom, nTo-nFrom, pFmt);
780 }
781
782 /******************************************************************************
783  * ME_SetCharFormat
784  * 
785  * Applies a style change to the specified part of the text
786  */     
787 void ME_SetCharFormat(ME_TextEditor *editor, int nOfs, int nChars, CHARFORMAT2W *pFmt)
788 {
789   ME_Cursor tmp, tmp2;
790   ME_DisplayItem *para;
791
792   ME_CursorFromCharOfs(editor, nOfs, &tmp);
793   if (tmp.nOffset)
794     tmp.pRun = ME_SplitRunSimple(editor, tmp.pRun, tmp.nOffset);
795
796   ME_CursorFromCharOfs(editor, nOfs+nChars, &tmp2);
797   if (tmp2.nOffset)
798     tmp2.pRun = ME_SplitRunSimple(editor, tmp2.pRun, tmp2.nOffset);
799
800   para = ME_GetParagraph(tmp.pRun);
801   para->member.para.nFlags |= MEPF_REWRAP;
802
803   while(tmp.pRun != tmp2.pRun)
804   {
805     ME_UndoItem *undo = NULL;
806     ME_Style *new_style = ME_ApplyStyle(tmp.pRun->member.run.style, pFmt);
807     /* ME_DumpStyle(new_style); */
808     undo = ME_AddUndoItem(editor, diUndoSetCharFormat, NULL);
809     if (undo) {
810       undo->nStart = tmp.pRun->member.run.nCharOfs+para->member.para.nCharOfs;
811       undo->nLen = tmp.pRun->member.run.strText->nLen;
812       undo->di.member.ustyle = tmp.pRun->member.run.style;
813       /* we'd have to addref undo..ustyle and release tmp...style
814          but they'd cancel each other out so we can do nothing instead */
815     }
816     else
817       ME_ReleaseStyle(tmp.pRun->member.run.style);
818     tmp.pRun->member.run.style = new_style;
819     tmp.pRun = ME_FindItemFwd(tmp.pRun, diRunOrParagraph);
820     if (tmp.pRun->type == diParagraph)
821     {
822       para = tmp.pRun;
823       tmp.pRun = ME_FindItemFwd(tmp.pRun, diRun);
824       if (tmp.pRun != tmp2.pRun)
825         para->member.para.nFlags |= MEPF_REWRAP;
826     }
827     assert(tmp.pRun);
828   }
829 }
830
831 /******************************************************************************
832  * ME_SetDefaultCharFormat
833  * 
834  * Applies a style change to the default character style.
835  */     
836 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
837 {
838   ME_Style *style;
839   ME_UndoItem *undo;
840
841   assert(mod->cbSize == sizeof(CHARFORMAT2W));
842   undo = ME_AddUndoItem(editor, diUndoSetDefaultCharFormat, NULL);
843   if (undo) {
844     undo->nStart = -1;
845     undo->nLen = -1;
846     undo->di.member.ustyle = editor->pBuffer->pDefaultStyle;
847     ME_AddRefStyle(undo->di.member.ustyle);
848   }
849   style = ME_ApplyStyle(editor->pBuffer->pDefaultStyle, mod);
850   editor->pBuffer->pDefaultStyle->fmt = style->fmt;
851   editor->pBuffer->pDefaultStyle->tm = style->tm;
852   ME_ReleaseStyle(style);
853   ME_MarkAllForWrapping(editor);
854   /*  pcf = editor->pBuffer->pDefaultStyle->fmt; */
855 }
856
857 static void ME_GetRunCharFormat(ME_TextEditor *editor, ME_DisplayItem *run, CHARFORMAT2W *pFmt)
858 {
859   ME_CopyCharFormat(pFmt, &run->member.run.style->fmt);
860 }
861
862 /******************************************************************************
863  * ME_GetDefaultCharFormat
864  * 
865  * Retrieves the current default character style (the one applied where no
866  * other style was applied) .
867  */     
868 void ME_GetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
869 {
870   int nFrom, nTo;
871   ME_GetSelection(editor, &nFrom, &nTo);
872   ME_CopyCharFormat(pFmt, &editor->pBuffer->pDefaultStyle->fmt);
873 }
874
875 /******************************************************************************
876  * ME_GetSelectionCharFormat
877  * 
878  * If selection exists, it returns all style elements that are set consistently
879  * in the whole selection. If not, it just returns the current style.  
880  */     
881 void ME_GetSelectionCharFormat(ME_TextEditor *editor, CHARFORMAT2W *pFmt)
882 {
883   int nFrom, nTo;
884   ME_GetSelection(editor, &nFrom, &nTo);
885   if (nFrom == nTo && editor->pBuffer->pCharStyle)
886   {
887     ME_CopyCharFormat(pFmt, &editor->pBuffer->pCharStyle->fmt);
888     return;
889   }
890   ME_GetCharFormat(editor, nFrom, nTo, pFmt);
891 }
892
893 /******************************************************************************
894  * ME_GetCharFormat
895  * 
896  * Returns the style consisting of those attributes which are consistently set
897  * in the whole character range.    
898  */     
899 void ME_GetCharFormat(ME_TextEditor *editor, int nFrom, int nTo, CHARFORMAT2W *pFmt)
900 {
901   ME_DisplayItem *run, *run_end;
902   int nOffset, nOffset2;
903   CHARFORMAT2W tmp;
904
905   ME_RunOfsFromCharOfs(editor, nFrom, &run, &nOffset);
906   if (nFrom == nTo) /* special case - if selection is empty, take previous char's formatting */
907   {
908     if (!nOffset)
909     {
910       ME_DisplayItem *tmp_run = ME_FindItemBack(run, diRunOrParagraph);
911       if (tmp_run->type == diRun) {
912         ME_GetRunCharFormat(editor, tmp_run, pFmt);
913         return;
914       }
915     }
916     ME_GetRunCharFormat(editor, run, pFmt);
917     return;
918   }
919   
920   if (nTo>nFrom) /* selection consists of chars from nFrom up to nTo-1 */
921     nTo--;
922   ME_RunOfsFromCharOfs(editor, nTo, &run_end, &nOffset2);
923
924   ME_GetRunCharFormat(editor, run, pFmt);
925
926   if (run == run_end) return;
927
928   do {
929     /* FIXME add more style feature comparisons */
930     int nAttribs = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_UNDERLINETYPE;
931     int nEffects = CFM_BOLD | CFM_ITALIC;
932
933     run = ME_FindItemFwd(run, diRun);
934
935     ZeroMemory(&tmp, sizeof(tmp));
936     tmp.cbSize = sizeof(tmp);
937     ME_GetRunCharFormat(editor, run, &tmp);
938
939     assert((tmp.dwMask & nAttribs) == nAttribs);
940     assert((tmp.dwMask & nEffects) == nEffects);
941     /* reset flags that differ */
942
943     if (pFmt->yHeight != tmp.yHeight)
944       pFmt->dwMask &= ~CFM_SIZE;
945     if (pFmt->dwMask & CFM_FACE)
946     {
947       if (!(tmp.dwMask & CFM_FACE))
948         pFmt->dwMask &= ~CFM_FACE;
949       else if (lstrcmpW(pFmt->szFaceName, tmp.szFaceName) ||
950           pFmt->bPitchAndFamily != tmp.bPitchAndFamily)
951         pFmt->dwMask &= ~CFM_FACE;
952     }
953     if (pFmt->yHeight != tmp.yHeight)
954       pFmt->dwMask &= ~CFM_SIZE;
955     if (pFmt->bUnderlineType != tmp.bUnderlineType)
956       pFmt->dwMask &= ~CFM_UNDERLINETYPE;
957     if (pFmt->dwMask & CFM_COLOR)
958     {
959       if (!((pFmt->dwEffects&CFE_AUTOCOLOR) & (tmp.dwEffects&CFE_AUTOCOLOR)))
960       {
961         if (pFmt->crTextColor != tmp.crTextColor)
962           pFmt->dwMask &= ~CFM_COLOR;
963       }
964     }
965
966     pFmt->dwMask &= ~((pFmt->dwEffects ^ tmp.dwEffects) & nEffects);
967
968   } while(run != run_end);
969 }