2 * RichEdit style management functions
4 * Copyright 2004 by Krzysztof Foltman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit
);
24 WINE_DECLARE_DEBUG_CHANNEL(richedit_style
);
26 static int all_refs
= 0;
28 /* the following routines assume that:
29 * - char2[AW] extends char[AW] by adding fields at the end of the charA form)
30 * - szFaceName is the last field of char[AW] form, and wWeight the first of 2[AW]
31 * - the difference between A and W form is the szFaceName as Ansi vs Unicode string
32 * - because of alignment, offset of wWeight field in 2[AW] structure *IS NOT*
36 CHARFORMAT2W
*ME_ToCF2W(CHARFORMAT2W
*to
, CHARFORMAT2W
*from
)
38 if (from
->cbSize
== sizeof(CHARFORMATA
))
40 CHARFORMATA
*f
= (CHARFORMATA
*)from
;
41 CopyMemory(to
, f
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
42 to
->cbSize
= sizeof(CHARFORMAT2W
);
43 if (f
->dwMask
& CFM_FACE
) {
44 MultiByteToWideChar(CP_ACP
, 0, f
->szFaceName
, -1, to
->szFaceName
, sizeof(to
->szFaceName
)/sizeof(WCHAR
));
48 if (from
->cbSize
== sizeof(CHARFORMATW
))
50 CHARFORMATW
*f
= (CHARFORMATW
*)from
;
51 CopyMemory(to
, f
, sizeof(*f
));
52 /* theoretically, we don't need to zero the remaining memory */
53 ZeroMemory(&to
->wWeight
, sizeof(CHARFORMAT2W
)-FIELD_OFFSET(CHARFORMAT2W
, wWeight
));
54 to
->cbSize
= sizeof(CHARFORMAT2W
);
57 if (from
->cbSize
== sizeof(CHARFORMAT2A
))
59 CHARFORMAT2A
*f
= (CHARFORMAT2A
*)from
;
60 /* copy the A structure without face name */
61 CopyMemory(to
, f
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
62 /* convert face name */
63 if (f
->dwMask
& CFM_FACE
)
64 MultiByteToWideChar(CP_ACP
, 0, f
->szFaceName
, -1, to
->szFaceName
, sizeof(to
->szFaceName
)/sizeof(WCHAR
));
65 /* copy the rest of the 2A structure to 2W */
66 CopyMemory(&to
->wWeight
, &f
->wWeight
, sizeof(CHARFORMAT2A
)-FIELD_OFFSET(CHARFORMAT2A
, wWeight
));
67 to
->cbSize
= sizeof(CHARFORMAT2W
);
71 return (from
->cbSize
>= sizeof(CHARFORMAT2W
)) ? from
: NULL
;
74 static CHARFORMAT2W
*ME_ToCFAny(CHARFORMAT2W
*to
, CHARFORMAT2W
*from
)
76 assert(from
->cbSize
== sizeof(CHARFORMAT2W
));
77 if (to
->cbSize
== sizeof(CHARFORMATA
))
79 CHARFORMATA
*t
= (CHARFORMATA
*)to
;
80 CopyMemory(t
, from
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
81 WideCharToMultiByte(CP_ACP
, 0, from
->szFaceName
, -1, t
->szFaceName
, sizeof(t
->szFaceName
), NULL
, NULL
);
82 if (from
->dwMask
& CFM_UNDERLINETYPE
)
84 switch (from
->bUnderlineType
)
86 case CFU_CF1UNDERLINE
:
87 to
->dwMask
|= CFM_UNDERLINE
;
88 to
->dwEffects
|= CFE_UNDERLINE
;
90 case CFU_UNDERLINENONE
:
91 to
->dwMask
|= CFM_UNDERLINE
;
92 to
->dwEffects
&= ~CFE_UNDERLINE
;
96 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
99 if (to
->cbSize
== sizeof(CHARFORMATW
))
101 CHARFORMATW
*t
= (CHARFORMATW
*)to
;
102 CopyMemory(t
, from
, sizeof(*t
));
103 if (from
->dwMask
& CFM_UNDERLINETYPE
)
105 switch (from
->bUnderlineType
)
107 case CFU_CF1UNDERLINE
:
108 to
->dwMask
|= CFM_UNDERLINE
;
109 to
->dwEffects
|= CFE_UNDERLINE
;
111 case CFU_UNDERLINENONE
:
112 to
->dwMask
|= CFM_UNDERLINE
;
113 to
->dwEffects
&= ~CFE_UNDERLINE
;
117 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
120 if (to
->cbSize
== sizeof(CHARFORMAT2A
))
122 CHARFORMAT2A
*t
= (CHARFORMAT2A
*)to
;
123 /* copy the A structure without face name */
124 CopyMemory(t
, from
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
125 /* convert face name */
126 WideCharToMultiByte(CP_ACP
, 0, from
->szFaceName
, -1, t
->szFaceName
, sizeof(t
->szFaceName
), NULL
, NULL
);
127 /* copy the rest of the 2A structure to 2W */
128 CopyMemory(&t
->wWeight
, &from
->wWeight
, sizeof(CHARFORMAT2W
)-FIELD_OFFSET(CHARFORMAT2W
,wWeight
));
129 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
132 assert(to
->cbSize
>= sizeof(CHARFORMAT2W
));
136 void ME_CopyToCFAny(CHARFORMAT2W
*to
, CHARFORMAT2W
*from
)
138 if (ME_ToCFAny(to
, from
) == from
)
139 CopyMemory(to
, from
, to
->cbSize
);
142 ME_Style
*ME_MakeStyle(CHARFORMAT2W
*style
)
144 ME_Style
*s
= ALLOC_OBJ(ME_Style
);
146 assert(style
->cbSize
== sizeof(CHARFORMAT2W
));
149 s
->font_cache
= NULL
;
150 memset(&s
->tm
, 0, sizeof(s
->tm
));
152 s
->script_cache
= NULL
;
153 list_init(&s
->entry
);
155 TRACE_(richedit_style
)("ME_MakeStyle %p, total refs=%d\n", s
, all_refs
);
159 #define COPY_STYLE_ITEM(mask, member) \
160 if (mod->dwMask & mask) { \
162 fmt.member = mod->member;\
165 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
166 if (mod->dwMask & mask) { \
168 CopyMemory(fmt.member, mod->member, sizeof(mod->member));\
171 void ME_InitCharFormat2W(CHARFORMAT2W
*pFmt
)
173 ZeroMemory(pFmt
, sizeof(CHARFORMAT2W
));
174 pFmt
->cbSize
= sizeof(CHARFORMAT2W
);
177 ME_Style
*ME_ApplyStyle(ME_TextEditor
*editor
, ME_Style
*sSrc
, CHARFORMAT2W
*mod
)
179 CHARFORMAT2W fmt
= sSrc
->fmt
;
182 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
183 COPY_STYLE_ITEM(CFM_ANIMATION
, bAnimation
);
184 COPY_STYLE_ITEM(CFM_BACKCOLOR
, crBackColor
);
185 COPY_STYLE_ITEM(CFM_CHARSET
, bCharSet
);
186 COPY_STYLE_ITEM(CFM_COLOR
, crTextColor
);
187 COPY_STYLE_ITEM_MEMCPY(CFM_FACE
, szFaceName
);
188 COPY_STYLE_ITEM(CFM_KERNING
, wKerning
);
189 COPY_STYLE_ITEM(CFM_LCID
, lcid
);
190 COPY_STYLE_ITEM(CFM_OFFSET
, yOffset
);
191 COPY_STYLE_ITEM(CFM_REVAUTHOR
, bRevAuthor
);
192 if (mod
->dwMask
& CFM_SIZE
) {
193 fmt
.dwMask
|= CFM_SIZE
;
194 fmt
.yHeight
= min(mod
->yHeight
, yHeightCharPtsMost
* 20);
196 COPY_STYLE_ITEM(CFM_SPACING
, sSpacing
);
197 COPY_STYLE_ITEM(CFM_STYLE
, sStyle
);
198 COPY_STYLE_ITEM(CFM_UNDERLINETYPE
, bUnderlineType
);
199 COPY_STYLE_ITEM(CFM_WEIGHT
, wWeight
);
200 /* FIXME: this is not documented this way, but that's the more logical */
201 COPY_STYLE_ITEM(CFM_FACE
, bPitchAndFamily
);
203 fmt
.dwEffects
&= ~(mod
->dwMask
);
204 fmt
.dwEffects
|= mod
->dwEffects
& mod
->dwMask
;
205 fmt
.dwMask
|= mod
->dwMask
;
206 if (mod
->dwMask
& CFM_COLOR
)
208 if (mod
->dwEffects
& CFE_AUTOCOLOR
)
209 fmt
.dwEffects
|= CFE_AUTOCOLOR
;
211 fmt
.dwEffects
&= ~CFE_AUTOCOLOR
;
213 if (mod
->dwMask
& CFM_UNDERLINE
)
215 fmt
.dwMask
|= CFM_UNDERLINETYPE
;
216 fmt
.bUnderlineType
= (mod
->dwEffects
& CFM_UNDERLINE
) ?
217 CFU_CF1UNDERLINE
: CFU_UNDERLINENONE
;
219 if (mod
->dwMask
& CFM_BOLD
&& !(mod
->dwMask
& CFM_WEIGHT
))
221 fmt
.wWeight
= (mod
->dwEffects
& CFE_BOLD
) ? FW_BOLD
: FW_NORMAL
;
222 } else if (mod
->dwMask
& CFM_WEIGHT
&& !(mod
->dwMask
& CFM_BOLD
)) {
223 if (mod
->wWeight
> FW_NORMAL
)
224 fmt
.dwEffects
|= CFE_BOLD
;
226 fmt
.dwEffects
&= ~CFE_BOLD
;
229 LIST_FOR_EACH_ENTRY(s
, &editor
->style_list
, ME_Style
, entry
)
231 if (!memcmp( &s
->fmt
, &fmt
, sizeof(fmt
) ))
233 TRACE_(richedit_style
)("found existing style %p\n", s
);
239 s
= ME_MakeStyle( &fmt
);
241 list_add_head( &editor
->style_list
, &s
->entry
);
242 TRACE_(richedit_style
)("created new style %p\n", s
);
246 void ME_CopyCharFormat(CHARFORMAT2W
*pDest
, const CHARFORMAT2W
*pSrc
)
248 /* using this with non-2W structs is forbidden */
249 assert(pSrc
->cbSize
== sizeof(CHARFORMAT2W
));
250 assert(pDest
->cbSize
== sizeof(CHARFORMAT2W
));
254 static void ME_DumpStyleEffect(char **p
, const char *name
, const CHARFORMAT2W
*fmt
, int mask
)
256 *p
+= sprintf(*p
, "%-22s%s\n", name
, (fmt
->dwMask
& mask
) ? ((fmt
->dwEffects
& mask
) ? "YES" : "no") : "N/A");
259 void ME_DumpStyle(ME_Style
*s
)
262 ME_DumpStyleToBuf(&s
->fmt
, buf
);
263 TRACE_(richedit_style
)("%s\n", buf
);
266 void ME_DumpStyleToBuf(CHARFORMAT2W
*pFmt
, char buf
[2048])
268 /* FIXME only CHARFORMAT styles implemented */
269 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
272 p
+= sprintf(p
, "Font face: ");
273 if (pFmt
->dwMask
& CFM_FACE
) {
274 WCHAR
*q
= pFmt
->szFaceName
;
276 *p
++ = (*q
> 255) ? '?' : *q
;
280 p
+= sprintf(p
, "N/A");
282 if (pFmt
->dwMask
& CFM_SIZE
)
283 p
+= sprintf(p
, "\nFont size: %d\n", pFmt
->yHeight
);
285 p
+= sprintf(p
, "\nFont size: N/A\n");
287 if (pFmt
->dwMask
& CFM_OFFSET
)
288 p
+= sprintf(p
, "Char offset: %d\n", pFmt
->yOffset
);
290 p
+= sprintf(p
, "Char offset: N/A\n");
292 if (pFmt
->dwMask
& CFM_CHARSET
)
293 p
+= sprintf(p
, "Font charset: %d\n", (int)pFmt
->bCharSet
);
295 p
+= sprintf(p
, "Font charset: N/A\n");
297 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
298 ME_DumpStyleEffect(&p
, "Font bold:", pFmt
, CFM_BOLD
);
299 ME_DumpStyleEffect(&p
, "Font italic:", pFmt
, CFM_ITALIC
);
300 ME_DumpStyleEffect(&p
, "Font underline:", pFmt
, CFM_UNDERLINE
);
301 ME_DumpStyleEffect(&p
, "Font strikeout:", pFmt
, CFM_STRIKEOUT
);
302 ME_DumpStyleEffect(&p
, "Hidden text:", pFmt
, CFM_HIDDEN
);
303 p
+= sprintf(p
, "Text color: ");
304 if (pFmt
->dwMask
& CFM_COLOR
)
306 if (pFmt
->dwEffects
& CFE_AUTOCOLOR
)
307 p
+= sprintf(p
, "auto\n");
309 p
+= sprintf(p
, "%06x\n", (int)pFmt
->crTextColor
);
312 p
+= sprintf(p
, "N/A\n");
313 ME_DumpStyleEffect(&p
, "Text protected:", pFmt
, CFM_PROTECTED
);
318 ME_LogFontFromStyle(ME_Context
* c
, LOGFONTW
*lf
, const ME_Style
*s
)
320 ZeroMemory(lf
, sizeof(LOGFONTW
));
321 lstrcpyW(lf
->lfFaceName
, s
->fmt
.szFaceName
);
323 lf
->lfHeight
= ME_twips2pointsY(c
, -s
->fmt
.yHeight
);
325 lf
->lfWeight
= FW_NORMAL
;
326 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_BOLD
)
327 lf
->lfWeight
= FW_BOLD
;
328 if (s
->fmt
.dwMask
& CFM_WEIGHT
)
329 lf
->lfWeight
= s
->fmt
.wWeight
;
330 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_ITALIC
)
332 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& (CFM_UNDERLINE
| CFE_LINK
))
334 if (s
->fmt
.dwMask
& CFM_UNDERLINETYPE
&& s
->fmt
.bUnderlineType
== CFU_CF1UNDERLINE
)
336 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_STRIKEOUT
)
338 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& (CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
))
339 lf
->lfHeight
= (lf
->lfHeight
*2)/3;
340 /*lf.lfQuality = PROOF_QUALITY; */
341 if (s
->fmt
.dwMask
& CFM_FACE
)
342 lf
->lfPitchAndFamily
= s
->fmt
.bPitchAndFamily
;
343 if (s
->fmt
.dwMask
& CFM_CHARSET
)
344 lf
->lfCharSet
= s
->fmt
.bCharSet
;
347 void ME_CharFormatFromLogFont(HDC hDC
, const LOGFONTW
*lf
, CHARFORMAT2W
*fmt
)
351 ME_InitCharFormat2W(fmt
);
352 ry
= GetDeviceCaps(hDC
, LOGPIXELSY
);
353 lstrcpyW(fmt
->szFaceName
, lf
->lfFaceName
);
355 fmt
->dwMask
= CFM_WEIGHT
|CFM_BOLD
|CFM_ITALIC
|CFM_UNDERLINE
|CFM_STRIKEOUT
|CFM_SIZE
|CFM_FACE
|CFM_CHARSET
;
356 fmt
->wWeight
= lf
->lfWeight
;
357 fmt
->yHeight
= -lf
->lfHeight
*1440/ry
;
358 if (lf
->lfWeight
> FW_NORMAL
) fmt
->dwEffects
|= CFM_BOLD
;
359 if (lf
->lfItalic
) fmt
->dwEffects
|= CFM_ITALIC
;
360 if (lf
->lfUnderline
) fmt
->dwEffects
|= CFM_UNDERLINE
;
361 /* notice that if a logfont was created with underline due to CFM_LINK, this
362 would add an erroneous CFM_UNDERLINE. This isn't currently ever a problem. */
363 if (lf
->lfStrikeOut
) fmt
->dwEffects
|= CFM_STRIKEOUT
;
364 fmt
->bPitchAndFamily
= lf
->lfPitchAndFamily
;
365 fmt
->bCharSet
= lf
->lfCharSet
;
368 static BOOL
ME_IsFontEqual(const LOGFONTW
*p1
, const LOGFONTW
*p2
)
370 if (memcmp(p1
, p2
, sizeof(LOGFONTW
)-sizeof(p1
->lfFaceName
)))
372 if (lstrcmpW(p1
->lfFaceName
, p2
->lfFaceName
))
377 HFONT
ME_SelectStyleFont(ME_Context
*c
, ME_Style
*s
)
381 int i
, nEmpty
, nAge
= 0x7FFFFFFF;
382 ME_FontCacheItem
*item
;
385 ME_LogFontFromStyle(c
, &lf
, s
);
387 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
388 c
->editor
->pFontCache
[i
].nAge
++;
389 for (i
=0, nEmpty
=-1, nAge
=0; i
<HFONT_CACHE_SIZE
; i
++)
391 item
= &c
->editor
->pFontCache
[i
];
394 if (item
->nAge
> nAge
)
395 nEmpty
= i
, nAge
= item
->nAge
;
397 if (item
->hFont
&& ME_IsFontEqual(&item
->lfSpecs
, &lf
))
400 if (i
< HFONT_CACHE_SIZE
) /* found */
402 item
= &c
->editor
->pFontCache
[i
];
403 TRACE_(richedit_style
)("font reused %d\n", i
);
408 item
= &c
->editor
->pFontCache
[nEmpty
]; /* this legal even when nEmpty == -1, as we don't dereference it */
410 assert(nEmpty
!= -1); /* otherwise we leak cache entries or get too many fonts at once*/
412 TRACE_(richedit_style
)("font deleted %d\n", nEmpty
);
413 DeleteObject(item
->hFont
);
416 item
->hFont
= CreateFontIndirectW(&lf
);
417 TRACE_(richedit_style
)("font created %d\n", nEmpty
);
421 s
->font_cache
= item
;
422 hOldFont
= SelectObject(c
->hDC
, item
->hFont
);
423 /* should be cached too, maybe ? */
424 GetTextMetricsW(c
->hDC
, &s
->tm
);
428 static void release_font_cache(ME_FontCacheItem
*item
)
437 void ME_UnselectStyleFont(ME_Context
*c
, ME_Style
*s
, HFONT hOldFont
)
439 SelectObject(c
->hDC
, hOldFont
);
440 release_font_cache(s
->font_cache
);
441 s
->font_cache
= NULL
;
444 void ME_DestroyStyle(ME_Style
*s
)
446 list_remove( &s
->entry
);
449 release_font_cache( s
->font_cache
);
450 s
->font_cache
= NULL
;
452 ScriptFreeCache( &s
->script_cache
);
456 void ME_AddRefStyle(ME_Style
*s
)
458 assert(s
->nRefs
>0); /* style with 0 references isn't supposed to exist */
461 TRACE_(richedit_style
)("ME_AddRefStyle %p, new refs=%d, total refs=%d\n", s
, s
->nRefs
, all_refs
);
464 void ME_ReleaseStyle(ME_Style
*s
)
469 TRACE_(richedit_style
)("destroy style %p, total refs=%d\n", s
, all_refs
);
471 TRACE_(richedit_style
)("release style %p, new refs=%d, total refs=%d\n", s
, s
->nRefs
, all_refs
);
472 if (!all_refs
) TRACE("all style references freed (good!)\n");
478 ME_Style
*ME_GetInsertStyle(ME_TextEditor
*editor
, int nCursor
)
480 if (ME_IsSelection(editor
))
482 ME_Cursor
*from
, *to
;
484 ME_GetSelection(editor
, &from
, &to
);
485 ME_AddRefStyle(from
->pRun
->member
.run
.style
);
486 return from
->pRun
->member
.run
.style
;
488 if (editor
->pBuffer
->pCharStyle
) {
489 ME_AddRefStyle(editor
->pBuffer
->pCharStyle
);
490 return editor
->pBuffer
->pCharStyle
;
494 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
495 ME_DisplayItem
*pRunItem
= pCursor
->pRun
;
496 ME_DisplayItem
*pPrevItem
= NULL
;
497 if (pCursor
->nOffset
) {
498 ME_Run
*pRun
= &pRunItem
->member
.run
;
499 ME_AddRefStyle(pRun
->style
);
502 pPrevItem
= ME_FindItemBack(pRunItem
, diRunOrParagraph
);
503 if (pPrevItem
->type
== diRun
)
505 ME_AddRefStyle(pPrevItem
->member
.run
.style
);
506 return pPrevItem
->member
.run
.style
;
510 ME_AddRefStyle(pRunItem
->member
.run
.style
);
511 return pRunItem
->member
.run
.style
;
516 void ME_SaveTempStyle(ME_TextEditor
*editor
)
518 ME_Style
*old_style
= editor
->pBuffer
->pCharStyle
;
519 editor
->pBuffer
->pCharStyle
= ME_GetInsertStyle(editor
, 0);
521 ME_ReleaseStyle(old_style
);
524 void ME_ClearTempStyle(ME_TextEditor
*editor
)
526 if (!editor
->pBuffer
->pCharStyle
) return;
527 ME_ReleaseStyle(editor
->pBuffer
->pCharStyle
);
528 editor
->pBuffer
->pCharStyle
= NULL
;
531 /******************************************************************************
532 * ME_SetDefaultCharFormat
534 * Applies a style change to the default character style.
536 * The default style is special in that it is mutable - runs
537 * in the document that have this style should change if the
538 * default style changes. That means we need to fix up this
541 void ME_SetDefaultCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*mod
)
543 ME_Style
*style
, *def
= editor
->pBuffer
->pDefaultStyle
;
545 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
546 style
= ME_ApplyStyle(editor
, def
, mod
);
547 def
->fmt
= style
->fmt
;
551 release_font_cache( def
->font_cache
);
552 def
->font_cache
= NULL
;
554 ScriptFreeCache( &def
->script_cache
);
555 ME_ReleaseStyle( style
);
556 ME_MarkAllForWrapping( editor
);