0b85d69f6f17cef3d29facc7099997f09b482180
[reactos.git] / dll / win32 / riched20 / style.c
1 /*
2 * RichEdit style management functions
3 *
4 * Copyright 2004 by Krzysztof Foltman
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24 WINE_DECLARE_DEBUG_CHANNEL(richedit_style);
25
26 static int all_refs = 0;
27
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])
34 */
35
36 CHARFORMAT2W *ME_ToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
37 {
38 if (from->cbSize == sizeof(CHARFORMATA))
39 {
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));
45 }
46 return to;
47 }
48 if (from->cbSize == sizeof(CHARFORMATW))
49 {
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;
56 }
57 if (from->cbSize == sizeof(CHARFORMAT2A))
58 {
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);
68 return to;
69 }
70
71 return (from->cbSize >= sizeof(CHARFORMAT2W)) ? from : NULL;
72 }
73
74 static CHARFORMAT2W *ME_ToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
75 {
76 assert(from->cbSize == sizeof(CHARFORMAT2W));
77 if (to->cbSize == sizeof(CHARFORMATA))
78 {
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 */
83 return to;
84 }
85 if (to->cbSize == sizeof(CHARFORMATW))
86 {
87 CHARFORMATW *t = (CHARFORMATW *)to;
88 CopyMemory(t, from, sizeof(*t));
89 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
90 return to;
91 }
92 if (to->cbSize == sizeof(CHARFORMAT2A))
93 {
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 */
102 return to;
103 }
104 assert(to->cbSize >= sizeof(CHARFORMAT2W));
105 return from;
106 }
107
108 void ME_CopyToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
109 {
110 if (ME_ToCFAny(to, from) == from)
111 CopyMemory(to, from, to->cbSize);
112 }
113
114 ME_Style *ME_MakeStyle(CHARFORMAT2W *style)
115 {
116 ME_Style *s = heap_alloc(sizeof(*s));
117
118 assert(style->cbSize == sizeof(CHARFORMAT2W));
119 s->fmt = *style;
120 s->nRefs = 1;
121 s->font_cache = NULL;
122 memset(&s->tm, 0, sizeof(s->tm));
123 s->tm.tmAscent = -1;
124 s->script_cache = NULL;
125 list_init(&s->entry);
126 all_refs++;
127 TRACE_(richedit_style)("ME_MakeStyle %p, total refs=%d\n", s, all_refs);
128 return s;
129 }
130
131 #define COPY_STYLE_ITEM(mask, member) \
132 if (mod->dwMask & mask) { \
133 fmt.dwMask |= mask;\
134 fmt.member = mod->member;\
135 }
136
137 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
138 if (mod->dwMask & mask) { \
139 fmt.dwMask |= mask;\
140 CopyMemory(fmt.member, mod->member, sizeof(mod->member));\
141 }
142
143 void ME_InitCharFormat2W(CHARFORMAT2W *pFmt)
144 {
145 ZeroMemory(pFmt, sizeof(CHARFORMAT2W));
146 pFmt->cbSize = sizeof(CHARFORMAT2W);
147 }
148
149 ME_Style *ME_ApplyStyle(ME_TextEditor *editor, ME_Style *sSrc, CHARFORMAT2W *mod)
150 {
151 CHARFORMAT2W fmt = sSrc->fmt;
152 ME_Style *s;
153
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);
167 }
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);
173
174 fmt.dwEffects &= ~(mod->dwMask);
175 fmt.dwEffects |= mod->dwEffects & mod->dwMask;
176 fmt.dwMask |= mod->dwMask;
177 if (mod->dwMask & CFM_COLOR)
178 {
179 if (mod->dwEffects & CFE_AUTOCOLOR)
180 fmt.dwEffects |= CFE_AUTOCOLOR;
181 else
182 fmt.dwEffects &= ~CFE_AUTOCOLOR;
183 }
184
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))
188 {
189 fmt.dwMask |= CFM_UNDERLINE;
190 if (mod->bUnderlineType == CFU_UNDERLINENONE)
191 fmt.dwEffects &= ~CFE_UNDERLINE;
192 else
193 fmt.dwEffects |= CFE_UNDERLINE;
194 }
195
196 if (mod->dwMask & CFM_BOLD && !(mod->dwMask & CFM_WEIGHT))
197 {
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;
202 else
203 fmt.dwEffects &= ~CFE_BOLD;
204 }
205
206 LIST_FOR_EACH_ENTRY(s, &editor->style_list, ME_Style, entry)
207 {
208 if (!memcmp( &s->fmt, &fmt, sizeof(fmt) ))
209 {
210 TRACE_(richedit_style)("found existing style %p\n", s);
211 ME_AddRefStyle( s );
212 return s;
213 }
214 }
215
216 s = ME_MakeStyle( &fmt );
217 if (s)
218 list_add_head( &editor->style_list, &s->entry );
219 TRACE_(richedit_style)("created new style %p\n", s);
220 return s;
221 }
222
223 void ME_CopyCharFormat(CHARFORMAT2W *pDest, const CHARFORMAT2W *pSrc)
224 {
225 /* using this with non-2W structs is forbidden */
226 assert(pSrc->cbSize == sizeof(CHARFORMAT2W));
227 assert(pDest->cbSize == sizeof(CHARFORMAT2W));
228 *pDest = *pSrc;
229 }
230
231 static void ME_DumpStyleEffect(char **p, const char *name, const CHARFORMAT2W *fmt, int mask)
232 {
233 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->dwEffects & mask) ? "YES" : "no") : "N/A");
234 }
235
236 void ME_DumpStyle(ME_Style *s)
237 {
238 char buf[2048];
239 ME_DumpStyleToBuf(&s->fmt, buf);
240 TRACE_(richedit_style)("%s\n", buf);
241 }
242
243 void ME_DumpStyleToBuf(CHARFORMAT2W *pFmt, char buf[2048])
244 {
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 */
247 char *p;
248 p = buf;
249 p += sprintf(p, "Font face: ");
250 if (pFmt->dwMask & CFM_FACE) {
251 WCHAR *q = pFmt->szFaceName;
252 while(*q) {
253 *p++ = (*q > 255) ? '?' : *q;
254 q++;
255 }
256 } else
257 p += sprintf(p, "N/A");
258
259 if (pFmt->dwMask & CFM_SIZE)
260 p += sprintf(p, "\nFont size: %d\n", pFmt->yHeight);
261 else
262 p += sprintf(p, "\nFont size: N/A\n");
263
264 if (pFmt->dwMask & CFM_OFFSET)
265 p += sprintf(p, "Char offset: %d\n", pFmt->yOffset);
266 else
267 p += sprintf(p, "Char offset: N/A\n");
268
269 if (pFmt->dwMask & CFM_CHARSET)
270 p += sprintf(p, "Font charset: %d\n", (int)pFmt->bCharSet);
271 else
272 p += sprintf(p, "Font charset: N/A\n");
273
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)
282 {
283 if (pFmt->dwEffects & CFE_AUTOCOLOR)
284 p += sprintf(p, "auto\n");
285 else
286 p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
287 }
288 else
289 p += sprintf(p, "N/A\n");
290 ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
291 }
292
293
294 static void
295 ME_LogFontFromStyle(ME_Context* c, LOGFONTW *lf, const ME_Style *s)
296 {
297 ZeroMemory(lf, sizeof(LOGFONTW));
298 lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
299
300 lf->lfHeight = ME_twips2pointsY(c, -s->fmt.yHeight);
301
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)
308 lf->lfItalic = 1;
309 if ((s->fmt.dwEffects & s->fmt.dwMask & CFM_UNDERLINE) &&
310 !(s->fmt.dwEffects & CFE_LINK) &&
311 s->fmt.bUnderlineType == CFU_CF1UNDERLINE)
312 lf->lfUnderline = 1;
313 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
314 lf->lfStrikeOut = 1;
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;
322 }
323
324 void ME_CharFormatFromLogFont(HDC hDC, const LOGFONTW *lf, CHARFORMAT2W *fmt)
325 {
326 int ry;
327
328 ME_InitCharFormat2W(fmt);
329 ry = GetDeviceCaps(hDC, LOGPIXELSY);
330 lstrcpyW(fmt->szFaceName, lf->lfFaceName);
331 fmt->dwEffects = 0;
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;
342 }
343
344 static BOOL ME_IsFontEqual(const LOGFONTW *p1, const LOGFONTW *p2)
345 {
346 if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
347 return FALSE;
348 if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
349 return FALSE;
350 return TRUE;
351 }
352
353 HFONT ME_SelectStyleFont(ME_Context *c, ME_Style *s)
354 {
355 HFONT hOldFont;
356 LOGFONTW lf;
357 int i, nEmpty, nAge = 0x7FFFFFFF;
358 ME_FontCacheItem *item;
359 assert(s);
360
361 ME_LogFontFromStyle(c, &lf, s);
362
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++)
366 {
367 item = &c->editor->pFontCache[i];
368 if (!item->nRefs)
369 {
370 if (item->nAge > nAge)
371 nEmpty = i, nAge = item->nAge;
372 }
373 if (item->hFont && ME_IsFontEqual(&item->lfSpecs, &lf))
374 break;
375 }
376 if (i < HFONT_CACHE_SIZE) /* found */
377 {
378 item = &c->editor->pFontCache[i];
379 TRACE_(richedit_style)("font reused %d\n", i);
380 item->nRefs++;
381 }
382 else
383 {
384 item = &c->editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
385
386 assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
387 if (item->hFont) {
388 TRACE_(richedit_style)("font deleted %d\n", nEmpty);
389 DeleteObject(item->hFont);
390 item->hFont = NULL;
391 }
392 item->hFont = CreateFontIndirectW(&lf);
393 TRACE_(richedit_style)("font created %d\n", nEmpty);
394 item->nRefs = 1;
395 item->lfSpecs = lf;
396 }
397 s->font_cache = item;
398 hOldFont = SelectObject(c->hDC, item->hFont);
399 /* should be cached too, maybe ? */
400 GetTextMetricsW(c->hDC, &s->tm);
401 return hOldFont;
402 }
403
404 static void release_font_cache(ME_FontCacheItem *item)
405 {
406 if (item->nRefs > 0)
407 {
408 item->nRefs--;
409 item->nAge = 0;
410 }
411 }
412
413 void ME_UnselectStyleFont(ME_Context *c, ME_Style *s, HFONT hOldFont)
414 {
415 SelectObject(c->hDC, hOldFont);
416 release_font_cache(s->font_cache);
417 s->font_cache = NULL;
418 }
419
420 void ME_DestroyStyle(ME_Style *s)
421 {
422 list_remove( &s->entry );
423 if (s->font_cache)
424 {
425 release_font_cache( s->font_cache );
426 s->font_cache = NULL;
427 }
428 ScriptFreeCache( &s->script_cache );
429 heap_free(s);
430 }
431
432 void ME_AddRefStyle(ME_Style *s)
433 {
434 assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
435 s->nRefs++;
436 all_refs++;
437 TRACE_(richedit_style)("ME_AddRefStyle %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
438 }
439
440 void ME_ReleaseStyle(ME_Style *s)
441 {
442 s->nRefs--;
443 all_refs--;
444 if (s->nRefs==0)
445 TRACE_(richedit_style)("destroy style %p, total refs=%d\n", s, all_refs);
446 else
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");
449 assert(s->nRefs>=0);
450 if (!s->nRefs)
451 ME_DestroyStyle(s);
452 }
453
454 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor)
455 {
456 if (ME_IsSelection(editor))
457 {
458 ME_Cursor *from, *to;
459
460 ME_GetSelection(editor, &from, &to);
461 ME_AddRefStyle(from->pRun->member.run.style);
462 return from->pRun->member.run.style;
463 }
464 if (editor->pBuffer->pCharStyle) {
465 ME_AddRefStyle(editor->pBuffer->pCharStyle);
466 return editor->pBuffer->pCharStyle;
467 }
468 else
469 {
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);
476 return pRun->style;
477 }
478 pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
479 if (pPrevItem->type == diRun)
480 {
481 ME_AddRefStyle(pPrevItem->member.run.style);
482 return pPrevItem->member.run.style;
483 }
484 else
485 {
486 ME_AddRefStyle(pRunItem->member.run.style);
487 return pRunItem->member.run.style;
488 }
489 }
490 }
491
492 void ME_SaveTempStyle(ME_TextEditor *editor, ME_Style *style)
493 {
494 ME_Style *old_style = editor->pBuffer->pCharStyle;
495
496 if (style) ME_AddRefStyle( style );
497 editor->pBuffer->pCharStyle = style;
498 if (old_style) ME_ReleaseStyle( old_style );
499 }
500
501 void ME_ClearTempStyle(ME_TextEditor *editor)
502 {
503 if (!editor->pBuffer->pCharStyle) return;
504 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
505 editor->pBuffer->pCharStyle = NULL;
506 }
507
508 /******************************************************************************
509 * ME_SetDefaultCharFormat
510 *
511 * Applies a style change to the default character style.
512 *
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
516 * style manually.
517 */
518 void ME_SetDefaultCharFormat(ME_TextEditor *editor, CHARFORMAT2W *mod)
519 {
520 ME_Style *style, *def = editor->pBuffer->pDefaultStyle;
521
522 assert(mod->cbSize == sizeof(CHARFORMAT2W));
523 style = ME_ApplyStyle(editor, def, mod);
524 def->fmt = style->fmt;
525 def->tm = style->tm;
526 if (def->font_cache)
527 {
528 release_font_cache( def->font_cache );
529 def->font_cache = NULL;
530 }
531 ScriptFreeCache( &def->script_cache );
532 ME_ReleaseStyle( style );
533 ME_MarkAllForWrapping( editor );
534 }