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 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
85 if (to
->cbSize
== sizeof(CHARFORMATW
))
87 CHARFORMATW
*t
= (CHARFORMATW
*)to
;
88 CopyMemory(t
, from
, sizeof(*t
));
89 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
92 if (to
->cbSize
== sizeof(CHARFORMAT2A
))
94 CHARFORMAT2A
*t
= (CHARFORMAT2A
*)to
;
95 /* copy the A structure without face name */
96 CopyMemory(t
, from
, FIELD_OFFSET(CHARFORMATA
, szFaceName
));
97 /* convert face name */
98 WideCharToMultiByte(CP_ACP
, 0, from
->szFaceName
, -1, t
->szFaceName
, sizeof(t
->szFaceName
), NULL
, NULL
);
99 /* copy the rest of the 2A structure to 2W */
100 CopyMemory(&t
->wWeight
, &from
->wWeight
, sizeof(CHARFORMAT2W
)-FIELD_OFFSET(CHARFORMAT2W
,wWeight
));
101 t
->cbSize
= sizeof(*t
); /* it was overwritten by CopyMemory */
104 assert(to
->cbSize
>= sizeof(CHARFORMAT2W
));
108 void ME_CopyToCFAny(CHARFORMAT2W
*to
, CHARFORMAT2W
*from
)
110 if (ME_ToCFAny(to
, from
) == from
)
111 CopyMemory(to
, from
, to
->cbSize
);
114 ME_Style
*ME_MakeStyle(CHARFORMAT2W
*style
)
116 ME_Style
*s
= heap_alloc(sizeof(*s
));
118 assert(style
->cbSize
== sizeof(CHARFORMAT2W
));
121 s
->font_cache
= NULL
;
122 memset(&s
->tm
, 0, sizeof(s
->tm
));
124 s
->script_cache
= NULL
;
125 list_init(&s
->entry
);
127 TRACE_(richedit_style
)("ME_MakeStyle %p, total refs=%d\n", s
, all_refs
);
131 #define COPY_STYLE_ITEM(mask, member) \
132 if (mod->dwMask & mask) { \
134 fmt.member = mod->member;\
137 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
138 if (mod->dwMask & mask) { \
140 CopyMemory(fmt.member, mod->member, sizeof(mod->member));\
143 void ME_InitCharFormat2W(CHARFORMAT2W
*pFmt
)
145 ZeroMemory(pFmt
, sizeof(CHARFORMAT2W
));
146 pFmt
->cbSize
= sizeof(CHARFORMAT2W
);
149 ME_Style
*ME_ApplyStyle(ME_TextEditor
*editor
, ME_Style
*sSrc
, CHARFORMAT2W
*mod
)
151 CHARFORMAT2W fmt
= sSrc
->fmt
;
154 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
155 COPY_STYLE_ITEM(CFM_ANIMATION
, bAnimation
);
156 COPY_STYLE_ITEM(CFM_BACKCOLOR
, crBackColor
);
157 COPY_STYLE_ITEM(CFM_CHARSET
, bCharSet
);
158 COPY_STYLE_ITEM(CFM_COLOR
, crTextColor
);
159 COPY_STYLE_ITEM_MEMCPY(CFM_FACE
, szFaceName
);
160 COPY_STYLE_ITEM(CFM_KERNING
, wKerning
);
161 COPY_STYLE_ITEM(CFM_LCID
, lcid
);
162 COPY_STYLE_ITEM(CFM_OFFSET
, yOffset
);
163 COPY_STYLE_ITEM(CFM_REVAUTHOR
, bRevAuthor
);
164 if (mod
->dwMask
& CFM_SIZE
) {
165 fmt
.dwMask
|= CFM_SIZE
;
166 fmt
.yHeight
= min(mod
->yHeight
, yHeightCharPtsMost
* 20);
168 COPY_STYLE_ITEM(CFM_SPACING
, sSpacing
);
169 COPY_STYLE_ITEM(CFM_STYLE
, sStyle
);
170 COPY_STYLE_ITEM(CFM_WEIGHT
, wWeight
);
171 /* FIXME: this is not documented this way, but that's the more logical */
172 COPY_STYLE_ITEM(CFM_FACE
, bPitchAndFamily
);
174 fmt
.dwEffects
&= ~(mod
->dwMask
);
175 fmt
.dwEffects
|= mod
->dwEffects
& mod
->dwMask
;
176 fmt
.dwMask
|= mod
->dwMask
;
177 if (mod
->dwMask
& CFM_COLOR
)
179 if (mod
->dwEffects
& CFE_AUTOCOLOR
)
180 fmt
.dwEffects
|= CFE_AUTOCOLOR
;
182 fmt
.dwEffects
&= ~CFE_AUTOCOLOR
;
185 COPY_STYLE_ITEM(CFM_UNDERLINETYPE
, bUnderlineType
);
186 /* If the CFM_UNDERLINE effect is not specified, set it appropriately */
187 if ((mod
->dwMask
& CFM_UNDERLINETYPE
) && !(mod
->dwMask
& CFM_UNDERLINE
))
189 fmt
.dwMask
|= CFM_UNDERLINE
;
190 if (mod
->bUnderlineType
== CFU_UNDERLINENONE
)
191 fmt
.dwEffects
&= ~CFE_UNDERLINE
;
193 fmt
.dwEffects
|= CFE_UNDERLINE
;
196 if (mod
->dwMask
& CFM_BOLD
&& !(mod
->dwMask
& CFM_WEIGHT
))
198 fmt
.wWeight
= (mod
->dwEffects
& CFE_BOLD
) ? FW_BOLD
: FW_NORMAL
;
199 } else if (mod
->dwMask
& CFM_WEIGHT
&& !(mod
->dwMask
& CFM_BOLD
)) {
200 if (mod
->wWeight
> FW_NORMAL
)
201 fmt
.dwEffects
|= CFE_BOLD
;
203 fmt
.dwEffects
&= ~CFE_BOLD
;
206 LIST_FOR_EACH_ENTRY(s
, &editor
->style_list
, ME_Style
, entry
)
208 if (!memcmp( &s
->fmt
, &fmt
, sizeof(fmt
) ))
210 TRACE_(richedit_style
)("found existing style %p\n", s
);
216 s
= ME_MakeStyle( &fmt
);
218 list_add_head( &editor
->style_list
, &s
->entry
);
219 TRACE_(richedit_style
)("created new style %p\n", s
);
223 void ME_CopyCharFormat(CHARFORMAT2W
*pDest
, const CHARFORMAT2W
*pSrc
)
225 /* using this with non-2W structs is forbidden */
226 assert(pSrc
->cbSize
== sizeof(CHARFORMAT2W
));
227 assert(pDest
->cbSize
== sizeof(CHARFORMAT2W
));
231 static void ME_DumpStyleEffect(char **p
, const char *name
, const CHARFORMAT2W
*fmt
, int mask
)
233 *p
+= sprintf(*p
, "%-22s%s\n", name
, (fmt
->dwMask
& mask
) ? ((fmt
->dwEffects
& mask
) ? "YES" : "no") : "N/A");
236 void ME_DumpStyle(ME_Style
*s
)
239 ME_DumpStyleToBuf(&s
->fmt
, buf
);
240 TRACE_(richedit_style
)("%s\n", buf
);
243 void ME_DumpStyleToBuf(CHARFORMAT2W
*pFmt
, char buf
[2048])
245 /* FIXME only CHARFORMAT styles implemented */
246 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
249 p
+= sprintf(p
, "Font face: ");
250 if (pFmt
->dwMask
& CFM_FACE
) {
251 WCHAR
*q
= pFmt
->szFaceName
;
253 *p
++ = (*q
> 255) ? '?' : *q
;
257 p
+= sprintf(p
, "N/A");
259 if (pFmt
->dwMask
& CFM_SIZE
)
260 p
+= sprintf(p
, "\nFont size: %d\n", pFmt
->yHeight
);
262 p
+= sprintf(p
, "\nFont size: N/A\n");
264 if (pFmt
->dwMask
& CFM_OFFSET
)
265 p
+= sprintf(p
, "Char offset: %d\n", pFmt
->yOffset
);
267 p
+= sprintf(p
, "Char offset: N/A\n");
269 if (pFmt
->dwMask
& CFM_CHARSET
)
270 p
+= sprintf(p
, "Font charset: %d\n", (int)pFmt
->bCharSet
);
272 p
+= sprintf(p
, "Font charset: N/A\n");
274 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
275 ME_DumpStyleEffect(&p
, "Font bold:", pFmt
, CFM_BOLD
);
276 ME_DumpStyleEffect(&p
, "Font italic:", pFmt
, CFM_ITALIC
);
277 ME_DumpStyleEffect(&p
, "Font underline:", pFmt
, CFM_UNDERLINE
);
278 ME_DumpStyleEffect(&p
, "Font strikeout:", pFmt
, CFM_STRIKEOUT
);
279 ME_DumpStyleEffect(&p
, "Hidden text:", pFmt
, CFM_HIDDEN
);
280 p
+= sprintf(p
, "Text color: ");
281 if (pFmt
->dwMask
& CFM_COLOR
)
283 if (pFmt
->dwEffects
& CFE_AUTOCOLOR
)
284 p
+= sprintf(p
, "auto\n");
286 p
+= sprintf(p
, "%06x\n", (int)pFmt
->crTextColor
);
289 p
+= sprintf(p
, "N/A\n");
290 ME_DumpStyleEffect(&p
, "Text protected:", pFmt
, CFM_PROTECTED
);
295 ME_LogFontFromStyle(ME_Context
* c
, LOGFONTW
*lf
, const ME_Style
*s
)
297 ZeroMemory(lf
, sizeof(LOGFONTW
));
298 lstrcpyW(lf
->lfFaceName
, s
->fmt
.szFaceName
);
300 lf
->lfHeight
= ME_twips2pointsY(c
, -s
->fmt
.yHeight
);
302 lf
->lfWeight
= FW_NORMAL
;
303 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_BOLD
)
304 lf
->lfWeight
= FW_BOLD
;
305 if (s
->fmt
.dwMask
& CFM_WEIGHT
)
306 lf
->lfWeight
= s
->fmt
.wWeight
;
307 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_ITALIC
)
309 if ((s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_UNDERLINE
) &&
310 !(s
->fmt
.dwEffects
& CFE_LINK
) &&
311 s
->fmt
.bUnderlineType
== CFU_CF1UNDERLINE
)
313 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& CFM_STRIKEOUT
)
315 if (s
->fmt
.dwEffects
& s
->fmt
.dwMask
& (CFM_SUBSCRIPT
|CFM_SUPERSCRIPT
))
316 lf
->lfHeight
= (lf
->lfHeight
*2)/3;
317 /*lf.lfQuality = PROOF_QUALITY; */
318 if (s
->fmt
.dwMask
& CFM_FACE
)
319 lf
->lfPitchAndFamily
= s
->fmt
.bPitchAndFamily
;
320 if (s
->fmt
.dwMask
& CFM_CHARSET
)
321 lf
->lfCharSet
= s
->fmt
.bCharSet
;
324 void ME_CharFormatFromLogFont(HDC hDC
, const LOGFONTW
*lf
, CHARFORMAT2W
*fmt
)
328 ME_InitCharFormat2W(fmt
);
329 ry
= GetDeviceCaps(hDC
, LOGPIXELSY
);
330 lstrcpyW(fmt
->szFaceName
, lf
->lfFaceName
);
332 fmt
->dwMask
= CFM_WEIGHT
|CFM_BOLD
|CFM_ITALIC
|CFM_UNDERLINE
|CFM_UNDERLINETYPE
|CFM_STRIKEOUT
|CFM_SIZE
|CFM_FACE
|CFM_CHARSET
;
333 fmt
->wWeight
= lf
->lfWeight
;
334 fmt
->yHeight
= -lf
->lfHeight
*1440/ry
;
335 if (lf
->lfWeight
> FW_NORMAL
) fmt
->dwEffects
|= CFM_BOLD
;
336 if (lf
->lfItalic
) fmt
->dwEffects
|= CFM_ITALIC
;
337 if (lf
->lfUnderline
) fmt
->dwEffects
|= CFM_UNDERLINE
;
338 fmt
->bUnderlineType
= CFU_UNDERLINE
;
339 if (lf
->lfStrikeOut
) fmt
->dwEffects
|= CFM_STRIKEOUT
;
340 fmt
->bPitchAndFamily
= lf
->lfPitchAndFamily
;
341 fmt
->bCharSet
= lf
->lfCharSet
;
344 static BOOL
ME_IsFontEqual(const LOGFONTW
*p1
, const LOGFONTW
*p2
)
346 if (memcmp(p1
, p2
, sizeof(LOGFONTW
)-sizeof(p1
->lfFaceName
)))
348 if (lstrcmpW(p1
->lfFaceName
, p2
->lfFaceName
))
353 HFONT
ME_SelectStyleFont(ME_Context
*c
, ME_Style
*s
)
357 int i
, nEmpty
, nAge
= 0x7FFFFFFF;
358 ME_FontCacheItem
*item
;
361 ME_LogFontFromStyle(c
, &lf
, s
);
363 for (i
=0; i
<HFONT_CACHE_SIZE
; i
++)
364 c
->editor
->pFontCache
[i
].nAge
++;
365 for (i
=0, nEmpty
=-1, nAge
=0; i
<HFONT_CACHE_SIZE
; i
++)
367 item
= &c
->editor
->pFontCache
[i
];
370 if (item
->nAge
> nAge
)
371 nEmpty
= i
, nAge
= item
->nAge
;
373 if (item
->hFont
&& ME_IsFontEqual(&item
->lfSpecs
, &lf
))
376 if (i
< HFONT_CACHE_SIZE
) /* found */
378 item
= &c
->editor
->pFontCache
[i
];
379 TRACE_(richedit_style
)("font reused %d\n", i
);
384 item
= &c
->editor
->pFontCache
[nEmpty
]; /* this legal even when nEmpty == -1, as we don't dereference it */
386 assert(nEmpty
!= -1); /* otherwise we leak cache entries or get too many fonts at once*/
388 TRACE_(richedit_style
)("font deleted %d\n", nEmpty
);
389 DeleteObject(item
->hFont
);
392 item
->hFont
= CreateFontIndirectW(&lf
);
393 TRACE_(richedit_style
)("font created %d\n", nEmpty
);
397 s
->font_cache
= item
;
398 hOldFont
= SelectObject(c
->hDC
, item
->hFont
);
399 /* should be cached too, maybe ? */
400 GetTextMetricsW(c
->hDC
, &s
->tm
);
404 static void release_font_cache(ME_FontCacheItem
*item
)
413 void ME_UnselectStyleFont(ME_Context
*c
, ME_Style
*s
, HFONT hOldFont
)
415 SelectObject(c
->hDC
, hOldFont
);
416 release_font_cache(s
->font_cache
);
417 s
->font_cache
= NULL
;
420 void ME_DestroyStyle(ME_Style
*s
)
422 list_remove( &s
->entry
);
425 release_font_cache( s
->font_cache
);
426 s
->font_cache
= NULL
;
428 ScriptFreeCache( &s
->script_cache
);
432 void ME_AddRefStyle(ME_Style
*s
)
434 assert(s
->nRefs
>0); /* style with 0 references isn't supposed to exist */
437 TRACE_(richedit_style
)("ME_AddRefStyle %p, new refs=%d, total refs=%d\n", s
, s
->nRefs
, all_refs
);
440 void ME_ReleaseStyle(ME_Style
*s
)
445 TRACE_(richedit_style
)("destroy style %p, total refs=%d\n", s
, all_refs
);
447 TRACE_(richedit_style
)("release style %p, new refs=%d, total refs=%d\n", s
, s
->nRefs
, all_refs
);
448 if (!all_refs
) TRACE("all style references freed (good!)\n");
454 ME_Style
*ME_GetInsertStyle(ME_TextEditor
*editor
, int nCursor
)
456 if (ME_IsSelection(editor
))
458 ME_Cursor
*from
, *to
;
460 ME_GetSelection(editor
, &from
, &to
);
461 ME_AddRefStyle(from
->pRun
->member
.run
.style
);
462 return from
->pRun
->member
.run
.style
;
464 if (editor
->pBuffer
->pCharStyle
) {
465 ME_AddRefStyle(editor
->pBuffer
->pCharStyle
);
466 return editor
->pBuffer
->pCharStyle
;
470 ME_Cursor
*pCursor
= &editor
->pCursors
[nCursor
];
471 ME_DisplayItem
*pRunItem
= pCursor
->pRun
;
472 ME_DisplayItem
*pPrevItem
= NULL
;
473 if (pCursor
->nOffset
) {
474 ME_Run
*pRun
= &pRunItem
->member
.run
;
475 ME_AddRefStyle(pRun
->style
);
478 pPrevItem
= ME_FindItemBack(pRunItem
, diRunOrParagraph
);
479 if (pPrevItem
->type
== diRun
)
481 ME_AddRefStyle(pPrevItem
->member
.run
.style
);
482 return pPrevItem
->member
.run
.style
;
486 ME_AddRefStyle(pRunItem
->member
.run
.style
);
487 return pRunItem
->member
.run
.style
;
492 void ME_SaveTempStyle(ME_TextEditor
*editor
, ME_Style
*style
)
494 ME_Style
*old_style
= editor
->pBuffer
->pCharStyle
;
496 if (style
) ME_AddRefStyle( style
);
497 editor
->pBuffer
->pCharStyle
= style
;
498 if (old_style
) ME_ReleaseStyle( old_style
);
501 void ME_ClearTempStyle(ME_TextEditor
*editor
)
503 if (!editor
->pBuffer
->pCharStyle
) return;
504 ME_ReleaseStyle(editor
->pBuffer
->pCharStyle
);
505 editor
->pBuffer
->pCharStyle
= NULL
;
508 /******************************************************************************
509 * ME_SetDefaultCharFormat
511 * Applies a style change to the default character style.
513 * The default style is special in that it is mutable - runs
514 * in the document that have this style should change if the
515 * default style changes. That means we need to fix up this
518 void ME_SetDefaultCharFormat(ME_TextEditor
*editor
, CHARFORMAT2W
*mod
)
520 ME_Style
*style
, *def
= editor
->pBuffer
->pDefaultStyle
;
522 assert(mod
->cbSize
== sizeof(CHARFORMAT2W
));
523 style
= ME_ApplyStyle(editor
, def
, mod
);
524 def
->fmt
= style
->fmt
;
528 release_font_cache( def
->font_cache
);
529 def
->font_cache
= NULL
;
531 ScriptFreeCache( &def
->script_cache
);
532 ME_ReleaseStyle( style
);
533 ME_MarkAllForWrapping( editor
);