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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
25 static int all_refs = 0;
27 CHARFORMAT2W *ME_ToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
29 if (from->cbSize == sizeof(CHARFORMATA))
31 CHARFORMATA *f = (CHARFORMATA *)from;
32 CopyMemory(to, f, sizeof(*f)-sizeof(f->szFaceName));
33 to->cbSize = sizeof(CHARFORMAT2W);
34 if (f->dwMask & CFM_FACE) {
35 MultiByteToWideChar(0, 0, f->szFaceName, -1, to->szFaceName, sizeof(to->szFaceName)/sizeof(WCHAR));
39 if (from->cbSize == sizeof(CHARFORMATW))
41 CHARFORMATW *f = (CHARFORMATW *)from;
42 CopyMemory(to, f, sizeof(*f));
43 /* theoretically, we don't need to zero the remaining memory */
44 ZeroMemory(((CHARFORMATW *)to)+1, sizeof(CHARFORMAT2W)-sizeof(CHARFORMATW));
45 to->cbSize = sizeof(CHARFORMAT2W);
48 if (from->cbSize == sizeof(CHARFORMAT2A))
50 CHARFORMATA *f = (CHARFORMATA *)from;
51 /* copy the A structure without face name */
52 CopyMemory(to, f, sizeof(CHARFORMATA)-sizeof(f->szFaceName));
53 /* convert face name */
54 if (f->dwMask & CFM_FACE)
55 MultiByteToWideChar(0, 0, f->szFaceName, -1, to->szFaceName, sizeof(to->szFaceName));
56 /* copy the rest of the 2A structure to 2W */
57 CopyMemory(1+((CHARFORMATW *)to), f+1, sizeof(CHARFORMAT2A)-sizeof(CHARFORMATA));
58 to->cbSize = sizeof(CHARFORMAT2W);
62 assert(from->cbSize >= sizeof(CHARFORMAT2W));
66 void ME_CopyToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
68 if (ME_ToCF2W(to, from) == from)
69 CopyMemory(to, from, sizeof(*from));
72 CHARFORMAT2W *ME_ToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
74 assert(from->cbSize == sizeof(CHARFORMAT2W));
75 if (to->cbSize == sizeof(CHARFORMATA))
77 CHARFORMATA *t = (CHARFORMATA *)to;
78 CopyMemory(t, from, sizeof(*t)-sizeof(t->szFaceName));
79 WideCharToMultiByte(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
80 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
83 if (to->cbSize == sizeof(CHARFORMATW))
85 CHARFORMATW *t = (CHARFORMATW *)to;
86 CopyMemory(t, from, sizeof(*t));
87 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
90 if (to->cbSize == sizeof(CHARFORMAT2A))
92 CHARFORMAT2A *t = (CHARFORMAT2A *)to;
93 /* copy the A structure without face name */
94 CopyMemory(t, from, sizeof(CHARFORMATA)-sizeof(t->szFaceName));
95 /* convert face name */
96 WideCharToMultiByte(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
97 /* copy the rest of the 2A structure to 2W */
98 CopyMemory(&t->wWeight, &from->wWeight, sizeof(CHARFORMAT2A)-sizeof(CHARFORMATA));
99 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
102 assert(to->cbSize >= sizeof(CHARFORMAT2W));
106 void ME_CopyToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
108 if (ME_ToCFAny(to, from) == from)
109 CopyMemory(to, from, to->cbSize);
112 ME_Style *ME_MakeStyle(CHARFORMAT2W *style) {
113 CHARFORMAT2W styledata;
114 ME_Style *s = ALLOC_OBJ(ME_Style);
116 style = ME_ToCF2W(&styledata, style);
117 memset(s, 0, sizeof(ME_Style));
118 if (style->cbSize <= sizeof(CHARFORMAT2W))
119 CopyMemory(&s->fmt, style, style->cbSize);
121 CopyMemory(&s->fmt, style, sizeof(CHARFORMAT2W));
122 s->fmt.cbSize = sizeof(CHARFORMAT2W);
131 #define COPY_STYLE_ITEM(mask, member) \
132 if (style->dwMask & mask) { \
133 s->fmt.dwMask |= mask;\
134 s->fmt.member = style->member;\
137 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
138 if (style->dwMask & mask) { \
139 s->fmt.dwMask |= mask;\
140 CopyMemory(s->fmt.member, style->member, sizeof(style->member));\
143 void ME_InitCharFormat2W(CHARFORMAT2W *pFmt)
145 ZeroMemory(pFmt, sizeof(CHARFORMAT2W));
146 pFmt->cbSize = sizeof(CHARFORMAT2W);
149 ME_Style *ME_ApplyStyle(ME_Style *sSrc, CHARFORMAT2W *style)
151 CHARFORMAT2W styledata;
152 ME_Style *s = ME_MakeStyle(&sSrc->fmt);
153 style = ME_ToCF2W(&styledata, style);
154 COPY_STYLE_ITEM(CFM_ANIMATION, bAnimation);
155 COPY_STYLE_ITEM(CFM_BACKCOLOR, crBackColor);
156 COPY_STYLE_ITEM(CFM_CHARSET, bCharSet);
157 COPY_STYLE_ITEM(CFM_COLOR, crTextColor);
158 COPY_STYLE_ITEM_MEMCPY(CFM_FACE, szFaceName);
159 COPY_STYLE_ITEM(CFM_KERNING, wKerning);
160 COPY_STYLE_ITEM(CFM_LCID, lcid);
161 COPY_STYLE_ITEM(CFM_OFFSET, yOffset);
162 COPY_STYLE_ITEM(CFM_REVAUTHOR, bRevAuthor);
163 COPY_STYLE_ITEM(CFM_SIZE, yHeight);
164 COPY_STYLE_ITEM(CFM_SPACING, sSpacing);
165 COPY_STYLE_ITEM(CFM_STYLE, sStyle);
166 COPY_STYLE_ITEM(CFM_UNDERLINETYPE, bUnderlineType);
167 COPY_STYLE_ITEM(CFM_WEIGHT, wWeight);
169 s->fmt.dwEffects &= ~(style->dwMask);
170 s->fmt.dwEffects |= style->dwEffects & style->dwMask;
171 s->fmt.dwMask |= style->dwMask;
172 if (style->dwMask & CFM_COLOR)
174 if (style->dwEffects & CFE_AUTOCOLOR)
175 s->fmt.dwEffects |= CFE_AUTOCOLOR;
177 s->fmt.dwEffects &= ~CFE_AUTOCOLOR;
182 void ME_CopyCharFormat(CHARFORMAT2W *pDest, CHARFORMAT2W *pSrc)
184 /* using this with non-2W structs is forbidden */
185 assert(pSrc->cbSize == sizeof(CHARFORMAT2W));
186 assert(pDest->cbSize == sizeof(CHARFORMAT2W));
187 CopyMemory(pDest, pSrc, sizeof(CHARFORMAT2W));
190 static void ME_DumpStyleEffect(char **p, const char *name, CHARFORMAT2W *fmt, int mask)
192 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->dwEffects & mask) ? "YES" : "no") : "N/A");
195 void ME_DumpStyle(ME_Style *s)
198 ME_DumpStyleToBuf(&s->fmt, buf);
202 void ME_DumpStyleToBuf(CHARFORMAT2W *pFmt, char buf[2048])
204 /* FIXME only CHARFORMAT styles implemented */
205 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
208 p += sprintf(p, "Font face: ");
209 if (pFmt->dwMask & CFM_FACE) {
210 WCHAR *q = pFmt->szFaceName;
212 *p++ = (*q > 255) ? '?' : *q;
216 p += sprintf(p, "N/A");
218 if (pFmt->dwMask & CFM_SIZE)
219 p += sprintf(p, "\nFont size: %d\n", (int)pFmt->yHeight);
221 p += sprintf(p, "\nFont size: N/A\n");
223 if (pFmt->dwMask & CFM_OFFSET)
224 p += sprintf(p, "Char offset: %d\n", (int)pFmt->yOffset);
226 p += sprintf(p, "Char offset: N/A\n");
228 if (pFmt->dwMask & CFM_CHARSET)
229 p += sprintf(p, "Font charset: %d\n", (int)pFmt->bCharSet);
231 p += sprintf(p, "Font charset: N/A\n");
233 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
234 ME_DumpStyleEffect(&p, "Font bold:", pFmt, CFM_BOLD);
235 ME_DumpStyleEffect(&p, "Font italic:", pFmt, CFM_ITALIC);
236 ME_DumpStyleEffect(&p, "Font underline:", pFmt, CFM_UNDERLINE);
237 ME_DumpStyleEffect(&p, "Font strikeout:", pFmt, CFM_STRIKEOUT);
238 p += sprintf(p, "Text color: ");
239 if (pFmt->dwMask & CFM_COLOR)
241 if (pFmt->dwEffects & CFE_AUTOCOLOR)
242 p += sprintf(p, "auto\n");
244 p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
247 p += sprintf(p, "N/A\n");
248 ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
251 void ME_LogFontFromStyle(HDC hDC, LOGFONTW *lf, ME_Style *s)
254 rx = GetDeviceCaps(hDC, LOGPIXELSX);
255 ry = GetDeviceCaps(hDC, LOGPIXELSY);
256 ZeroMemory(lf, sizeof(LOGFONTW));
257 lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
258 lf->lfHeight = -s->fmt.yHeight*ry/1440;
260 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_BOLD)
262 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_WEIGHT)
263 lf->lfWeight = s->fmt.wWeight;
264 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_ITALIC)
266 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_UNDERLINE)
268 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
270 /*lf.lfQuality = PROOF_QUALITY; */
271 lf->lfPitchAndFamily = s->fmt.bPitchAndFamily;
272 lf->lfCharSet = s->fmt.bCharSet;
276 BOOL ME_IsFontEqual(LOGFONTW *p1, LOGFONTW *p2)
278 if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
280 if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
285 HFONT ME_SelectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s)
289 int i, nEmpty, nAge = 0x7FFFFFFF;
290 ME_FontCacheItem *item;
294 ME_LogFontFromStyle(hDC, &lf, s);
296 for (i=0; i<HFONT_CACHE_SIZE; i++)
297 editor->pFontCache[i].nAge++;
298 for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
300 item = &editor->pFontCache[i];
303 if (item->nAge > nAge)
304 nEmpty = i, nAge = item->nAge;
306 if (ME_IsFontEqual(&item->lfSpecs, &lf))
309 if (i < HFONT_CACHE_SIZE) /* found */
311 item = &editor->pFontCache[i];
312 TRACE("font reused %d\n", i);
314 s->hFont = item->hFont;
319 item = &editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
321 assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
323 TRACE("font deleted %d\n", nEmpty);
324 DeleteObject(item->hFont);
327 s->hFont = CreateFontIndirectW(&lf);
329 TRACE("font created %d\n", nEmpty);
330 item->hFont = s->hFont;
332 memcpy(&item->lfSpecs, &lf, sizeof(LOGFONTW));
334 hOldFont = SelectObject(hDC, s->hFont);
335 /* should be cached too, maybe ? */
336 GetTextMetricsW(hDC, &s->tm);
340 void ME_UnselectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s, HFONT hOldFont)
346 SelectObject(hDC, hOldFont);
347 for (i=0; i<HFONT_CACHE_SIZE; i++)
349 ME_FontCacheItem *pItem = &editor->pFontCache[i];
350 if (pItem->hFont == s->hFont && pItem->nRefs > 0)
358 assert(0 == "UnselectStyleFont without SelectStyleFont");
361 void ME_DestroyStyle(ME_Style *s) {
364 DeleteObject(s->hFont);
370 void ME_AddRefStyle(ME_Style *s)
372 assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
377 void ME_ReleaseStyle(ME_Style *s)
382 TRACE("destroy style %p, total refs=%d\n", s, all_refs);
384 TRACE("release style %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
385 if (!all_refs) FIXME("all style references freed (good!)\n");
391 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor) {
392 if (ME_IsSelection(editor))
397 ME_GetSelection(editor, &from, &to);
398 ME_CursorFromCharOfs(editor, from, &c);
399 ME_AddRefStyle(c.pRun->member.run.style);
400 return c.pRun->member.run.style;
402 if (editor->pBuffer->pCharStyle) {
403 ME_AddRefStyle(editor->pBuffer->pCharStyle);
404 return editor->pBuffer->pCharStyle;
408 ME_Cursor *pCursor = &editor->pCursors[nCursor];
409 ME_DisplayItem *pRunItem = pCursor->pRun;
410 ME_DisplayItem *pPrevItem = NULL;
411 if (pCursor->nOffset) {
412 ME_Run *pRun = &pRunItem->member.run;
413 ME_AddRefStyle(pRun->style);
416 pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
417 if (pPrevItem->type == diRun)
419 ME_AddRefStyle(pPrevItem->member.run.style);
420 return pPrevItem->member.run.style;
424 ME_AddRefStyle(pRunItem->member.run.style);
425 return pRunItem->member.run.style;
430 void ME_SaveTempStyle(ME_TextEditor *editor)
432 ME_Style *old_style = editor->pBuffer->pCharStyle;
433 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
435 ME_ReleaseStyle(old_style);
438 void ME_ClearTempStyle(ME_TextEditor *editor)
440 if (!editor->pBuffer->pCharStyle) return;
441 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
442 editor->pBuffer->pCharStyle = NULL;