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 BOOL
cfany_to_cf2w(CHARFORMAT2W
*to
, const 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
, ARRAY_SIZE(to
->szFaceName
));
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
, ARRAY_SIZE(to
->szFaceName
));
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
);
70 if (from
->cbSize
== sizeof(CHARFORMAT2W
))
72 CopyMemory(to
, from
, sizeof(CHARFORMAT2W
));
79 BOOL
cf2w_to_cfany(CHARFORMAT2W
*to
, const CHARFORMAT2W
*from
)
81 assert(from
->cbSize
== sizeof(CHARFORMAT2W
));
82 if (to
->cbSize
== sizeof(CHARFORMATA
))
84 CHARFORMATA
*t
= (CHARFORMATA
*)to
;
85 CopyMemory(t
, from
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
86 WideCharToMultiByte(CP_ACP
, 0, from
->szFaceName
, -1, t
->szFaceName
, sizeof(t
->szFaceName
), NULL
, NULL
);
87 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
89 t
->dwEffects
&= CFM_EFFECTS
;
92 if (to
->cbSize
== sizeof(CHARFORMATW
))
94 CHARFORMATW
*t
= (CHARFORMATW
*)to
;
95 CopyMemory(t
, from
, sizeof(*t
));
96 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
98 t
->dwEffects
&= CFM_EFFECTS
;
101 if (to
->cbSize
== sizeof(CHARFORMAT2A
))
103 CHARFORMAT2A
*t
= (CHARFORMAT2A
*)to
;
104 /* copy the A structure without face name */
105 CopyMemory(t
, from
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
106 /* convert face name */
107 WideCharToMultiByte(CP_ACP
, 0, from
->szFaceName
, -1, t
->szFaceName
, sizeof(t
->szFaceName
), NULL
, NULL
);
108 /* copy the rest of the 2A structure to 2W */
109 CopyMemory(&t
->wWeight
, &from
->wWeight
, sizeof(CHARFORMAT2W
)-FIELD_OFFSET(CHARFORMAT2W
,wWeight
));
110 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
113 if (to
->cbSize
== sizeof(CHARFORMAT2W
))
115 CopyMemory(to
, from
, sizeof(CHARFORMAT2W
));
121 ME_Style
*ME_MakeStyle(CHARFORMAT2W
*style
)
123 ME_Style
*s
= heap_alloc(sizeof(*s
));
125 assert(style
->cbSize
== sizeof(CHARFORMAT2W
));
128 s
->font_cache
= NULL
;
129 memset(&s
->tm
, 0, sizeof(s
->tm
));
131 s
->script_cache
= NULL
;
132 list_init(&s
->entry
);
134 TRACE_(richedit_style
)("ME_MakeStyle %p, total refs=%d\n", s
, all_refs
);
138 #define COPY_STYLE_ITEM(mask, member) \
139 if (mod->dwMask & mask) { \
141 fmt.member = mod->member;\
144 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
145 if (mod->dwMask & mask) { \
147 CopyMemory(fmt.member, mod->member, sizeof(mod->member));\
150 void ME_InitCharFormat2W(CHARFORMAT2W
*pFmt
)
152 ZeroMemory(pFmt
, sizeof(CHARFORMAT2W
));
153 pFmt
->cbSize
= sizeof(CHARFORMAT2W
);
156 ME_Style
*ME_ApplyStyle(ME_TextEditor
*editor
, ME_Style
*sSrc
, CHARFORMAT2W
*mod
)
158 CHARFORMAT2W fmt
= sSrc
->fmt
;
161 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
162 COPY_STYLE_ITEM(CFM_ANIMATION
, bAnimation
);
163 COPY_STYLE_ITEM(CFM_BACKCOLOR
, crBackColor
);
164 COPY_STYLE_ITEM(CFM_CHARSET
, bCharSet
);
165 COPY_STYLE_ITEM(CFM_COLOR
, crTextColor
);
166 COPY_STYLE_ITEM_MEMCPY(CFM_FACE
, szFaceName
);
167 COPY_STYLE_ITEM(CFM_KERNING
, wKerning
);
168 COPY_STYLE_ITEM(CFM_LCID
, lcid
);
169 COPY_STYLE_ITEM(CFM_OFFSET
, yOffset
);
170 COPY_STYLE_ITEM(CFM_REVAUTHOR
, bRevAuthor
);
171 if (mod
->dwMask
& CFM_SIZE
) {
172 fmt
.dwMask
|= CFM_SIZE
;
173 fmt
.yHeight
= min(mod
->yHeight
, yHeightCharPtsMost
* 20);
175 COPY_STYLE_ITEM(CFM_SPACING
, sSpacing
);
176 COPY_STYLE_ITEM(CFM_STYLE
, sStyle
);
177 COPY_STYLE_ITEM(CFM_WEIGHT
, wWeight
);
178 /* FIXME: this is not documented this way, but that's the more logical */
179 COPY_STYLE_ITEM(CFM_FACE
, bPitchAndFamily
);
181 fmt
.dwEffects
&= ~(mod
->dwMask
);
182 fmt
.dwEffects
|= mod
->dwEffects
& mod
->dwMask
;
183 fmt
.dwMask
|= mod
->dwMask
;
184 if (mod
->dwMask
& CFM_COLOR
)
186 if (mod
->dwEffects
& CFE_AUTOCOLOR
)
187 fmt
.dwEffects
|= CFE_AUTOCOLOR
;
189 fmt
.dwEffects
&= ~CFE_AUTOCOLOR
;
192 COPY_STYLE_ITEM(CFM_UNDERLINETYPE
, bUnderlineType
);
193 /* If the CFM_UNDERLINE effect is not specified, set it appropriately */
194 if ((mod
->dwMask
& CFM_UNDERLINETYPE
) && !(mod
->dwMask
& CFM_UNDERLINE
))
196 fmt
.dwMask
|= CFM_UNDERLINE
;
197 if (mod
->bUnderlineType
== CFU_UNDERLINENONE
)
198 fmt
.dwEffects
&= ~CFE_UNDERLINE
;
200 fmt
.dwEffects
|= CFE_UNDERLINE
;
203 if (mod
->dwMask
& CFM_BOLD
&& !(mod
->dwMask
& CFM_WEIGHT
))
205 fmt
.wWeight
= (mod
->dwEffects
& CFE_BOLD
) ? FW_BOLD
: FW_NORMAL
;
206 } else if (mod
->dwMask
& CFM_WEIGHT
&& !(mod
->dwMask
& CFM_BOLD
)) {
207 if (mod
->wWeight
> FW_NORMAL
)
208 fmt
.dwEffects
|= CFE_BOLD
;
210 fmt
.dwEffects
&= ~CFE_BOLD
;
213 LIST_FOR_EACH_ENTRY(s
, &editor
->style_list
, ME_Style
, entry
)
215 if (!memcmp( &s
->fmt
, &fmt
, sizeof(fmt
) ))
217 TRACE_(richedit_style
)("found existing style %p\n", s
);
223 s
= ME_MakeStyle( &fmt
);
225 list_add_head( &editor
->style_list
, &s
->entry
);
226 TRACE_(richedit_style
)("created new style %p\n", s
);
230 void ME_CopyCharFormat(CHARFORMAT2W
*pDest
, const CHARFORMAT2W
*pSrc
)
232 /* using this with non-2W structs is forbidden */
233 assert(pSrc
->cbSize
== sizeof(CHARFORMAT2W
));
234 assert(pDest
->cbSize
== sizeof(CHARFORMAT2W
));
238 static void ME_DumpStyleEffect(char **p
, const char *name
, const CHARFORMAT2W
*fmt
, int mask
)
240 *p
+= sprintf(*p
, "%-22s%s\n", name
, (fmt
->dwMask
& mask
) ? ((fmt
->dwEffects
& mask
) ? "YES" : "no") : "N/A");
243 void ME_DumpStyle(ME_Style
*s
)
246 ME_DumpStyleToBuf(&s
->fmt
, buf
);
247 TRACE_(richedit_style
)("%s\n", buf
);
250 void ME_DumpStyleToBuf(CHARFORMAT2W
*pFmt
, char buf
[2048])
252 /* FIXME only CHARFORMAT styles implemented */
253 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
256 p
+= sprintf(p
, "Font face: ");
257 if (pFmt
->dwMask
& CFM_FACE
) {
258 WCHAR
*q
= pFmt
->szFaceName
;
260 *p
++ = (*q
> 255) ? '?' : *q
;
264 p
+= sprintf(p
, "N/A");
266 if (pFmt
->dwMask
& CFM_SIZE
)
267 p
+= sprintf(p
, "\nFont size: %d\n", pFmt
->yHeight
);
269 p
+= sprintf(p
, "\nFont size: N/A\n");
271 if (pFmt
->dwMask
& CFM_OFFSET
)
272 p
+= sprintf(p
, "Char offset: %d\n", pFmt
->yOffset
);
274 p
+= sprintf(p
, "Char offset: N/A\n");
276 if (pFmt
->dwMask
& CFM_CHARSET
)
277 p
+= sprintf(p
, "Font charset: %d\n", (int)pFmt
->bCharSet
);
279 p
+= sprintf(p
, "Font charset: N/A\n");
281 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
282 ME_DumpStyleEffect(&p
, "Font bold:", pFmt
, CFM_BOLD
);
283 ME_DumpStyleEffect(&p
, "Font italic:", pFmt
, CFM_ITALIC
);
284 ME_DumpStyleEffect(&p
, "Font underline:", pFmt
, CFM_UNDERLINE
);
285 ME_DumpStyleEffect(&p
, "Font strikeout:", pFmt
, CFM_STRIKEOUT
);
286 ME_DumpStyleEffect(&p
, "Hidden text:", pFmt
, CFM_HIDDEN
);
287 p
+= sprintf(p
, "Text color: ");
288 if (pFmt
->dwMask
& CFM_COLOR
)
290 if (pFmt
->dwEffects
& CFE_AUTOCOLOR
)
291 p
+= sprintf(p
, "auto\n");
293 p
+= sprintf(p
, "%06x\n", (int)pFmt
->crTextColor
);
296 p
+= sprintf(p
, "N/A\n");
297 ME_DumpStyleEffect(&p
, "Text protected:", pFmt
, CFM_PROTECTED
);
302 ME_LogFontFromStyle(ME_Context
* c
, LOGFONTW
*lf
, const ME_Style
*s
)
304 ZeroMemory(lf
, sizeof(LOGFONTW
));
305 lstrcpyW(lf
->lfFaceName
, s
->fmt
.szFaceName
);
307 lf
->lfHeight
= ME_twips2pointsY(c
, -s
->fmt
.yHeight
);
309 lf
->lfWeight
= FW_NORMAL
;
310 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_BOLD
)
311 lf
->lfWeight
= FW_BOLD
;
312 if (s
->fmt
.dwMask
& CFM_WEIGHT
)
313 lf
->lfWeight
= s
->fmt
.wWeight
;
314 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_ITALIC
)
316 if ((s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_UNDERLINE
) &&
317 !(s
->fmt
.dwEffects
& CFE_LINK
) &&
318 s
->fmt
.bUnderlineType
== CFU_CF1UNDERLINE
)
320 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_STRIKEOUT
)
322 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& (CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
))
323 lf
->lfHeight
= (lf
->lfHeight
*2)/3;
324 /*lf.lfQuality = PROOF_QUALITY; */
325 if (s
->fmt
.dwMask
& CFM_FACE
)
326 lf
->lfPitchAndFamily
= s
->fmt
.bPitchAndFamily
;
327 if (s
->fmt
.dwMask
& CFM_CHARSET
)
328 lf
->lfCharSet
= s
->fmt
.bCharSet
;
331 void ME_CharFormatFromLogFont(HDC hDC
, const LOGFONTW
*lf
, CHARFORMAT2W
*fmt
)
335 ME_InitCharFormat2W(fmt
);
336 ry
= GetDeviceCaps(hDC
, LOGPIXELSY
);
337 lstrcpyW(fmt
->szFaceName
, lf
->lfFaceName
);
339 fmt
->dwMask
= CFM_WEIGHT
|CFM_BOLD
|CFM_ITALIC
|CFM_UNDERLINE
|CFM_UNDERLINETYPE
|CFM_STRIKEOUT
|CFM_SIZE
|CFM_FACE
|CFM_CHARSET
;
340 fmt
->wWeight
= lf
->lfWeight
;
341 fmt
->yHeight
= -lf
->lfHeight
*1440/ry
;
342 if (lf
->lfWeight
> FW_NORMAL
) fmt
->dwEffects
|= CFM_BOLD
;
343 if (lf
->lfItalic
) fmt
->dwEffects
|= CFM_ITALIC
;
344 if (lf
->lfUnderline
) fmt
->dwEffects
|= CFM_UNDERLINE
;
345 fmt
->bUnderlineType
= CFU_UNDERLINE
;
346 if (lf
->lfStrikeOut
) fmt
->dwEffects
|= CFM_STRIKEOUT
;
347 fmt
->bPitchAndFamily
= lf
->lfPitchAndFamily
;
348 fmt
->bCharSet
= lf
->lfCharSet
;
351 static BOOL
ME_IsFontEqual(const LOGFONTW
*p1
, const LOGFONTW
*p2
)
353 if (memcmp(p1
, p2
, sizeof(LOGFONTW
)-sizeof(p1
->lfFaceName
)))
355 if (wcscmp(p1
->lfFaceName
, p2
->lfFaceName
))
360 static void release_font_cache(ME_FontCacheItem
*item
)
369 void select_style( ME_Context
*c
, ME_Style
*s
)
373 int i
, empty
, age
= 0x7FFFFFFF;
374 ME_FontCacheItem
*item
;
376 if (c
->current_style
== s
) return;
380 ME_LogFontFromStyle( c
, &lf
, s
);
382 for (i
= 0; i
< HFONT_CACHE_SIZE
; i
++)
383 c
->editor
->pFontCache
[i
].nAge
++;
384 for (i
= 0, empty
= -1, age
= 0; i
< HFONT_CACHE_SIZE
; i
++)
386 item
= &c
->editor
->pFontCache
[i
];
389 if (item
->nAge
> age
)
396 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
);
409 item
= &c
->editor
->pFontCache
[empty
];
412 TRACE_(richedit_style
)( "font deleted %d\n", empty
);
413 DeleteObject(item
->hFont
);
416 item
->hFont
= CreateFontIndirectW( &lf
);
417 TRACE_(richedit_style
)( "font created %d\n", empty
);
421 s
->font_cache
= item
;
422 old_font
= SelectObject( c
->hDC
, item
->hFont
);
423 GetTextMetricsW( c
->hDC
, &s
->tm
);
424 if (!c
->orig_font
) c
->orig_font
= old_font
;
428 SelectObject( c
->hDC
, c
->orig_font
);
432 if (c
->current_style
)
434 release_font_cache( c
->current_style
->font_cache
);
435 c
->current_style
->font_cache
= NULL
;
437 c
->current_style
= s
;
442 void ME_DestroyStyle(ME_Style
*s
)
444 list_remove( &s
->entry
);
447 release_font_cache( s
->font_cache
);
448 s
->font_cache
= NULL
;
450 ScriptFreeCache( &s
->script_cache
);
454 void ME_AddRefStyle(ME_Style
*s
)
456 assert(s
->nRefs
>0); /* style with 0 references isn't supposed to exist */
459 TRACE_(richedit_style
)("ME_AddRefStyle %p, new refs=%d, total refs=%d\n", s
, s
->nRefs
, all_refs
);
462 void ME_ReleaseStyle(ME_Style
*s
)
467 TRACE_(richedit_style
)("destroy style %p, total refs=%d\n", s
, all_refs
);
469 TRACE_(richedit_style
)("release style %p, new refs=%d, total refs=%d\n", s
, s
->nRefs
, all_refs
);
470 if (!all_refs
) TRACE("all style references freed (good!)\n");
476 ME_Style
*ME_GetInsertStyle(ME_TextEditor
*editor
, int nCursor
)
478 if (ME_IsSelection(editor
))
480 ME_Cursor
*from
, *to
;
482 ME_GetSelection(editor
, &from
, &to
);
483 ME_AddRefStyle(from
->pRun
->member
.run
.style
);
484 return from
->pRun
->member
.run
.style
;
486 if (editor
->pBuffer
->pCharStyle
) {
487 ME_AddRefStyle(editor
->pBuffer
->pCharStyle
);
488 return editor
->pBuffer
->pCharStyle
;
492 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
493 ME_DisplayItem
*pRunItem
= pCursor
->pRun
;
494 ME_DisplayItem
*pPrevItem
= NULL
;
495 if (pCursor
->nOffset
) {
496 ME_Run
*pRun
= &pRunItem
->member
.run
;
497 ME_AddRefStyle(pRun
->style
);
500 pPrevItem
= ME_FindItemBack(pRunItem
, diRunOrParagraph
);
501 if (pPrevItem
->type
== diRun
)
503 ME_AddRefStyle(pPrevItem
->member
.run
.style
);
504 return pPrevItem
->member
.run
.style
;
508 ME_AddRefStyle(pRunItem
->member
.run
.style
);
509 return pRunItem
->member
.run
.style
;
514 void ME_SaveTempStyle(ME_TextEditor
*editor
, ME_Style
*style
)
516 ME_Style
*old_style
= editor
->pBuffer
->pCharStyle
;
518 if (style
) ME_AddRefStyle( style
);
519 editor
->pBuffer
->pCharStyle
= style
;
520 if (old_style
) ME_ReleaseStyle( old_style
);
523 void ME_ClearTempStyle(ME_TextEditor
*editor
)
525 if (!editor
->pBuffer
->pCharStyle
) return;
526 ME_ReleaseStyle(editor
->pBuffer
->pCharStyle
);
527 editor
->pBuffer
->pCharStyle
= NULL
;
530 /******************************************************************************
531 * ME_SetDefaultCharFormat
533 * Applies a style change to the default character style.
535 * The default style is special in that it is mutable - runs
536 * in the document that have this style should change if the
537 * default style changes. That means we need to fix up this
540 void ME_SetDefaultCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*mod
)
542 ME_Style
*style
, *def
= editor
->pBuffer
->pDefaultStyle
;
544 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
545 style
= ME_ApplyStyle(editor
, def
, mod
);
546 def
->fmt
= style
->fmt
;
550 release_font_cache( def
->font_cache
);
551 def
->font_cache
= NULL
;
553 ScriptFreeCache( &def
->script_cache
);
554 ME_ReleaseStyle( style
);
555 ME_MarkAllForWrapping( editor
);