jscript: Removed unused do_*_tag_format arguments.
[wine/multimedia.git] / dlls / riched20 / style.c
blob605faeb92f03c2e551d964acd9e7e8c9b17300af
1 /*
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
21 #include "editor.h"
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*
33 * sizeof(char[AW])
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(0, 0, f->szFaceName, -1, to->szFaceName, sizeof(to->szFaceName)/sizeof(WCHAR));
46 return to;
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);
55 return to;
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(0, 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);
68 return to;
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(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
82 if (from->dwMask & CFM_UNDERLINETYPE)
84 switch (from->bUnderlineType)
86 case CFU_CF1UNDERLINE:
87 to->dwMask |= CFM_UNDERLINE;
88 to->dwEffects |= CFE_UNDERLINE;
89 break;
90 case CFU_UNDERLINENONE:
91 to->dwMask |= CFM_UNDERLINE;
92 to->dwEffects &= ~CFE_UNDERLINE;
93 break;
96 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
97 return to;
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;
110 break;
111 case CFU_UNDERLINENONE:
112 to->dwMask |= CFM_UNDERLINE;
113 to->dwEffects &= ~CFE_UNDERLINE;
114 break;
117 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
118 return to;
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(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
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 */
130 return to;
132 assert(to->cbSize >= sizeof(CHARFORMAT2W));
133 return from;
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));
147 s->fmt = *style;
148 s->nSequence = -2;
149 s->nRefs = 1;
150 s->hFont = NULL;
151 memset(&s->tm, 0, sizeof(s->tm));
152 s->tm.tmAscent = -1;
153 all_refs++;
154 TRACE_(richedit_style)("ME_MakeStyle %p, total refs=%d\n", s, all_refs);
155 return s;
158 #define COPY_STYLE_ITEM(mask, member) \
159 if (style->dwMask & mask) { \
160 s->fmt.dwMask |= mask;\
161 s->fmt.member = style->member;\
164 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
165 if (style->dwMask & mask) { \
166 s->fmt.dwMask |= mask;\
167 CopyMemory(s->fmt.member, style->member, sizeof(style->member));\
170 void ME_InitCharFormat2W(CHARFORMAT2W *pFmt)
172 ZeroMemory(pFmt, sizeof(CHARFORMAT2W));
173 pFmt->cbSize = sizeof(CHARFORMAT2W);
176 ME_Style *ME_ApplyStyle(ME_Style *sSrc, CHARFORMAT2W *style)
178 ME_Style *s = ME_MakeStyle(&sSrc->fmt);
179 assert(style->cbSize == sizeof(CHARFORMAT2W));
180 COPY_STYLE_ITEM(CFM_ANIMATION, bAnimation);
181 COPY_STYLE_ITEM(CFM_BACKCOLOR, crBackColor);
182 COPY_STYLE_ITEM(CFM_CHARSET, bCharSet);
183 COPY_STYLE_ITEM(CFM_COLOR, crTextColor);
184 COPY_STYLE_ITEM_MEMCPY(CFM_FACE, szFaceName);
185 COPY_STYLE_ITEM(CFM_KERNING, wKerning);
186 COPY_STYLE_ITEM(CFM_LCID, lcid);
187 COPY_STYLE_ITEM(CFM_OFFSET, yOffset);
188 COPY_STYLE_ITEM(CFM_REVAUTHOR, bRevAuthor);
189 if (style->dwMask & CFM_SIZE) {
190 s->fmt.dwMask |= CFM_SIZE;
191 s->fmt.yHeight = min(style->yHeight, yHeightCharPtsMost * 20);
193 COPY_STYLE_ITEM(CFM_SPACING, sSpacing);
194 COPY_STYLE_ITEM(CFM_STYLE, sStyle);
195 COPY_STYLE_ITEM(CFM_UNDERLINETYPE, bUnderlineType);
196 COPY_STYLE_ITEM(CFM_WEIGHT, wWeight);
197 /* FIXME: this is not documented this way, but that's the more logical */
198 COPY_STYLE_ITEM(CFM_FACE, bPitchAndFamily);
200 s->fmt.dwEffects &= ~(style->dwMask);
201 s->fmt.dwEffects |= style->dwEffects & style->dwMask;
202 s->fmt.dwMask |= style->dwMask;
203 if (style->dwMask & CFM_COLOR)
205 if (style->dwEffects & CFE_AUTOCOLOR)
206 s->fmt.dwEffects |= CFE_AUTOCOLOR;
207 else
208 s->fmt.dwEffects &= ~CFE_AUTOCOLOR;
210 if (style->dwMask & CFM_UNDERLINE)
212 s->fmt.dwMask |= CFM_UNDERLINETYPE;
213 s->fmt.bUnderlineType = (style->dwEffects & CFM_UNDERLINE) ?
214 CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
216 if (style->dwMask & CFM_BOLD && !(style->dwMask & CFM_WEIGHT))
218 s->fmt.wWeight = (style->dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
219 } else if (style->dwMask & CFM_WEIGHT && !(style->dwMask & CFM_BOLD)) {
220 if (style->wWeight > FW_NORMAL)
221 s->fmt.dwEffects |= CFE_BOLD;
222 else
223 s->fmt.dwEffects &= ~CFE_BOLD;
225 return s;
228 void ME_CopyCharFormat(CHARFORMAT2W *pDest, const CHARFORMAT2W *pSrc)
230 /* using this with non-2W structs is forbidden */
231 assert(pSrc->cbSize == sizeof(CHARFORMAT2W));
232 assert(pDest->cbSize == sizeof(CHARFORMAT2W));
233 *pDest = *pSrc;
236 static void ME_DumpStyleEffect(char **p, const char *name, const CHARFORMAT2W *fmt, int mask)
238 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->dwEffects & mask) ? "YES" : "no") : "N/A");
241 void ME_DumpStyle(ME_Style *s)
243 char buf[2048];
244 ME_DumpStyleToBuf(&s->fmt, buf);
245 TRACE_(richedit_style)("%s\n", buf);
248 void ME_DumpStyleToBuf(CHARFORMAT2W *pFmt, char buf[2048])
250 /* FIXME only CHARFORMAT styles implemented */
251 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
252 char *p;
253 p = buf;
254 p += sprintf(p, "Font face: ");
255 if (pFmt->dwMask & CFM_FACE) {
256 WCHAR *q = pFmt->szFaceName;
257 while(*q) {
258 *p++ = (*q > 255) ? '?' : *q;
259 q++;
261 } else
262 p += sprintf(p, "N/A");
264 if (pFmt->dwMask & CFM_SIZE)
265 p += sprintf(p, "\nFont size: %d\n", pFmt->yHeight);
266 else
267 p += sprintf(p, "\nFont size: N/A\n");
269 if (pFmt->dwMask & CFM_OFFSET)
270 p += sprintf(p, "Char offset: %d\n", pFmt->yOffset);
271 else
272 p += sprintf(p, "Char offset: N/A\n");
274 if (pFmt->dwMask & CFM_CHARSET)
275 p += sprintf(p, "Font charset: %d\n", (int)pFmt->bCharSet);
276 else
277 p += sprintf(p, "Font charset: N/A\n");
279 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
280 ME_DumpStyleEffect(&p, "Font bold:", pFmt, CFM_BOLD);
281 ME_DumpStyleEffect(&p, "Font italic:", pFmt, CFM_ITALIC);
282 ME_DumpStyleEffect(&p, "Font underline:", pFmt, CFM_UNDERLINE);
283 ME_DumpStyleEffect(&p, "Font strikeout:", pFmt, CFM_STRIKEOUT);
284 ME_DumpStyleEffect(&p, "Hidden text:", pFmt, CFM_HIDDEN);
285 p += sprintf(p, "Text color: ");
286 if (pFmt->dwMask & CFM_COLOR)
288 if (pFmt->dwEffects & CFE_AUTOCOLOR)
289 p += sprintf(p, "auto\n");
290 else
291 p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
293 else
294 p += sprintf(p, "N/A\n");
295 ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
299 static void
300 ME_LogFontFromStyle(ME_Context* c, LOGFONTW *lf, const ME_Style *s)
302 ZeroMemory(lf, sizeof(LOGFONTW));
303 lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
305 lf->lfHeight = ME_twips2pointsY(c, -s->fmt.yHeight);
307 lf->lfWeight = FW_NORMAL;
308 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_BOLD)
309 lf->lfWeight = FW_BOLD;
310 if (s->fmt.dwMask & CFM_WEIGHT)
311 lf->lfWeight = s->fmt.wWeight;
312 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_ITALIC)
313 lf->lfItalic = 1;
314 if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_UNDERLINE | CFE_LINK))
315 lf->lfUnderline = 1;
316 if (s->fmt.dwMask & CFM_UNDERLINETYPE && s->fmt.bUnderlineType == CFU_CF1UNDERLINE)
317 lf->lfUnderline = 1;
318 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
319 lf->lfStrikeOut = 1;
320 if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_SUBSCRIPT|CFM_SUPERSCRIPT))
321 lf->lfHeight = (lf->lfHeight*2)/3;
322 /*lf.lfQuality = PROOF_QUALITY; */
323 if (s->fmt.dwMask & CFM_FACE)
324 lf->lfPitchAndFamily = s->fmt.bPitchAndFamily;
325 if (s->fmt.dwMask & CFM_CHARSET)
326 lf->lfCharSet = s->fmt.bCharSet;
329 void ME_CharFormatFromLogFont(HDC hDC, const LOGFONTW *lf, CHARFORMAT2W *fmt)
331 int ry;
333 ME_InitCharFormat2W(fmt);
334 ry = GetDeviceCaps(hDC, LOGPIXELSY);
335 lstrcpyW(fmt->szFaceName, lf->lfFaceName);
336 fmt->dwEffects = 0;
337 fmt->dwMask = CFM_WEIGHT|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_STRIKEOUT|CFM_SIZE|CFM_FACE|CFM_CHARSET;
338 fmt->wWeight = lf->lfWeight;
339 fmt->yHeight = -lf->lfHeight*1440/ry;
340 if (lf->lfWeight > FW_NORMAL) fmt->dwEffects |= CFM_BOLD;
341 if (lf->lfItalic) fmt->dwEffects |= CFM_ITALIC;
342 if (lf->lfUnderline) fmt->dwEffects |= CFM_UNDERLINE;
343 /* notice that if a logfont was created with underline due to CFM_LINK, this
344 would add an erroneous CFM_UNDERLINE. This isn't currently ever a problem. */
345 if (lf->lfStrikeOut) fmt->dwEffects |= CFM_STRIKEOUT;
346 fmt->bPitchAndFamily = lf->lfPitchAndFamily;
347 fmt->bCharSet = lf->lfCharSet;
350 static BOOL ME_IsFontEqual(const LOGFONTW *p1, const LOGFONTW *p2)
352 if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
353 return FALSE;
354 if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
355 return FALSE;
356 return TRUE;
359 HFONT ME_SelectStyleFont(ME_Context *c, ME_Style *s)
361 HFONT hOldFont;
362 LOGFONTW lf;
363 int i, nEmpty, nAge = 0x7FFFFFFF;
364 ME_FontCacheItem *item;
365 assert(s);
367 ME_LogFontFromStyle(c, &lf, s);
369 for (i=0; i<HFONT_CACHE_SIZE; i++)
370 c->editor->pFontCache[i].nAge++;
371 for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
373 item = &c->editor->pFontCache[i];
374 if (!item->nRefs)
376 if (item->nAge > nAge)
377 nEmpty = i, nAge = item->nAge;
379 if (item->hFont && ME_IsFontEqual(&item->lfSpecs, &lf))
380 break;
382 if (i < HFONT_CACHE_SIZE) /* found */
384 item = &c->editor->pFontCache[i];
385 TRACE_(richedit_style)("font reused %d\n", i);
387 s->hFont = item->hFont;
388 item->nRefs++;
390 else
392 item = &c->editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
394 assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
395 if (item->hFont) {
396 TRACE_(richedit_style)("font deleted %d\n", nEmpty);
397 DeleteObject(item->hFont);
398 item->hFont = NULL;
400 s->hFont = CreateFontIndirectW(&lf);
401 assert(s->hFont);
402 TRACE_(richedit_style)("font created %d\n", nEmpty);
403 item->hFont = s->hFont;
404 item->nRefs = 1;
405 item->lfSpecs = lf;
407 hOldFont = SelectObject(c->hDC, s->hFont);
408 /* should be cached too, maybe ? */
409 GetTextMetricsW(c->hDC, &s->tm);
410 return hOldFont;
413 void ME_UnselectStyleFont(ME_Context *c, ME_Style *s, HFONT hOldFont)
415 int i;
417 assert(s);
418 SelectObject(c->hDC, hOldFont);
419 for (i=0; i<HFONT_CACHE_SIZE; i++)
421 ME_FontCacheItem *pItem = &c->editor->pFontCache[i];
422 if (pItem->hFont == s->hFont && pItem->nRefs > 0)
424 pItem->nRefs--;
425 pItem->nAge = 0;
426 s->hFont = NULL;
427 return;
430 assert(0 == "UnselectStyleFont without SelectStyleFont");
433 static void ME_DestroyStyle(ME_Style *s) {
434 if (s->hFont)
436 DeleteObject(s->hFont);
437 s->hFont = NULL;
439 FREE_OBJ(s);
442 void ME_AddRefStyle(ME_Style *s)
444 assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
445 s->nRefs++;
446 all_refs++;
447 TRACE_(richedit_style)("ME_AddRefStyle %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
450 void ME_ReleaseStyle(ME_Style *s)
452 s->nRefs--;
453 all_refs--;
454 if (s->nRefs==0)
455 TRACE_(richedit_style)("destroy style %p, total refs=%d\n", s, all_refs);
456 else
457 TRACE_(richedit_style)("release style %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
458 if (!all_refs) TRACE("all style references freed (good!)\n");
459 assert(s->nRefs>=0);
460 if (!s->nRefs)
461 ME_DestroyStyle(s);
464 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor)
466 if (ME_IsSelection(editor))
468 ME_Cursor *from, *to;
470 ME_GetSelection(editor, &from, &to);
471 ME_AddRefStyle(from->pRun->member.run.style);
472 return from->pRun->member.run.style;
474 if (editor->pBuffer->pCharStyle) {
475 ME_AddRefStyle(editor->pBuffer->pCharStyle);
476 return editor->pBuffer->pCharStyle;
478 else
480 ME_Cursor *pCursor = &editor->pCursors[nCursor];
481 ME_DisplayItem *pRunItem = pCursor->pRun;
482 ME_DisplayItem *pPrevItem = NULL;
483 if (pCursor->nOffset) {
484 ME_Run *pRun = &pRunItem->member.run;
485 ME_AddRefStyle(pRun->style);
486 return pRun->style;
488 pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
489 if (pPrevItem->type == diRun)
491 ME_AddRefStyle(pPrevItem->member.run.style);
492 return pPrevItem->member.run.style;
494 else
496 ME_AddRefStyle(pRunItem->member.run.style);
497 return pRunItem->member.run.style;
502 void ME_SaveTempStyle(ME_TextEditor *editor)
504 ME_Style *old_style = editor->pBuffer->pCharStyle;
505 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
506 if (old_style)
507 ME_ReleaseStyle(old_style);
510 void ME_ClearTempStyle(ME_TextEditor *editor)
512 if (!editor->pBuffer->pCharStyle) return;
513 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
514 editor->pBuffer->pCharStyle = NULL;