bb2f808954ac0aedf1189809a00b0db591eafbf7
[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(0, 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(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;
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(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
82 if (from->dwMask & CFM_UNDERLINETYPE)
83 {
84 switch (from->bUnderlineType)
85 {
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;
94 }
95 }
96 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
97 return to;
98 }
99 if (to->cbSize == sizeof(CHARFORMATW))
100 {
101 CHARFORMATW *t = (CHARFORMATW *)to;
102 CopyMemory(t, from, sizeof(*t));
103 if (from->dwMask & CFM_UNDERLINETYPE)
104 {
105 switch (from->bUnderlineType)
106 {
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;
115 }
116 }
117 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
118 return to;
119 }
120 if (to->cbSize == sizeof(CHARFORMAT2A))
121 {
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;
131 }
132 assert(to->cbSize >= sizeof(CHARFORMAT2W));
133 return from;
134 }
135
136 void ME_CopyToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
137 {
138 if (ME_ToCFAny(to, from) == from)
139 CopyMemory(to, from, to->cbSize);
140 }
141
142 ME_Style *ME_MakeStyle(CHARFORMAT2W *style) {
143 CHARFORMAT2W styledata;
144 ME_Style *s = ALLOC_OBJ(ME_Style);
145
146 style = ME_ToCF2W(&styledata, style);
147 memset(s, 0, sizeof(ME_Style));
148 if (style->cbSize <= sizeof(CHARFORMAT2W))
149 CopyMemory(&s->fmt, style, style->cbSize);
150 else
151 s->fmt = *style;
152 s->fmt.cbSize = sizeof(CHARFORMAT2W);
153
154 s->nSequence = -2;
155 s->nRefs = 1;
156 s->hFont = NULL;
157 s->tm.tmAscent = -1;
158 all_refs++;
159 TRACE_(richedit_style)("ME_MakeStyle %p, total refs=%d\n", s, all_refs);
160 return s;
161 }
162
163 #define COPY_STYLE_ITEM(mask, member) \
164 if (style->dwMask & mask) { \
165 s->fmt.dwMask |= mask;\
166 s->fmt.member = style->member;\
167 }
168
169 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
170 if (style->dwMask & mask) { \
171 s->fmt.dwMask |= mask;\
172 CopyMemory(s->fmt.member, style->member, sizeof(style->member));\
173 }
174
175 void ME_InitCharFormat2W(CHARFORMAT2W *pFmt)
176 {
177 ZeroMemory(pFmt, sizeof(CHARFORMAT2W));
178 pFmt->cbSize = sizeof(CHARFORMAT2W);
179 }
180
181 ME_Style *ME_ApplyStyle(ME_Style *sSrc, CHARFORMAT2W *style)
182 {
183 CHARFORMAT2W styledata;
184 ME_Style *s = ME_MakeStyle(&sSrc->fmt);
185 style = ME_ToCF2W(&styledata, style);
186 COPY_STYLE_ITEM(CFM_ANIMATION, bAnimation);
187 COPY_STYLE_ITEM(CFM_BACKCOLOR, crBackColor);
188 COPY_STYLE_ITEM(CFM_CHARSET, bCharSet);
189 COPY_STYLE_ITEM(CFM_COLOR, crTextColor);
190 COPY_STYLE_ITEM_MEMCPY(CFM_FACE, szFaceName);
191 COPY_STYLE_ITEM(CFM_KERNING, wKerning);
192 COPY_STYLE_ITEM(CFM_LCID, lcid);
193 COPY_STYLE_ITEM(CFM_OFFSET, yOffset);
194 COPY_STYLE_ITEM(CFM_REVAUTHOR, bRevAuthor);
195 if (style->dwMask & CFM_SIZE) {
196 s->fmt.dwMask |= CFM_SIZE;
197 s->fmt.yHeight = min(style->yHeight, yHeightCharPtsMost * 20);
198 }
199 COPY_STYLE_ITEM(CFM_SPACING, sSpacing);
200 COPY_STYLE_ITEM(CFM_STYLE, sStyle);
201 COPY_STYLE_ITEM(CFM_UNDERLINETYPE, bUnderlineType);
202 COPY_STYLE_ITEM(CFM_WEIGHT, wWeight);
203 /* FIXME: this is not documented this way, but that's the more logical */
204 COPY_STYLE_ITEM(CFM_FACE, bPitchAndFamily);
205
206 s->fmt.dwEffects &= ~(style->dwMask);
207 s->fmt.dwEffects |= style->dwEffects & style->dwMask;
208 s->fmt.dwMask |= style->dwMask;
209 if (style->dwMask & CFM_COLOR)
210 {
211 if (style->dwEffects & CFE_AUTOCOLOR)
212 s->fmt.dwEffects |= CFE_AUTOCOLOR;
213 else
214 s->fmt.dwEffects &= ~CFE_AUTOCOLOR;
215 }
216 if (style->dwMask & CFM_UNDERLINE)
217 {
218 s->fmt.dwMask |= CFM_UNDERLINETYPE;
219 s->fmt.bUnderlineType = (style->dwEffects & CFM_UNDERLINE) ?
220 CFU_CF1UNDERLINE : CFU_UNDERLINENONE;
221 }
222 if (style->dwMask & CFM_BOLD && !(style->dwMask & CFM_WEIGHT))
223 {
224 s->fmt.wWeight = (style->dwEffects & CFE_BOLD) ? FW_BOLD : FW_NORMAL;
225 } else if (style->dwMask & CFM_WEIGHT && !(style->dwMask & CFM_BOLD)) {
226 if (style->wWeight > FW_NORMAL)
227 s->fmt.dwEffects |= CFE_BOLD;
228 else
229 s->fmt.dwEffects &= ~CFE_BOLD;
230 }
231 return s;
232 }
233
234 void ME_CopyCharFormat(CHARFORMAT2W *pDest, const CHARFORMAT2W *pSrc)
235 {
236 /* using this with non-2W structs is forbidden */
237 assert(pSrc->cbSize == sizeof(CHARFORMAT2W));
238 assert(pDest->cbSize == sizeof(CHARFORMAT2W));
239 *pDest = *pSrc;
240 }
241
242 static void ME_DumpStyleEffect(char **p, const char *name, const CHARFORMAT2W *fmt, int mask)
243 {
244 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->dwEffects & mask) ? "YES" : "no") : "N/A");
245 }
246
247 void ME_DumpStyle(ME_Style *s)
248 {
249 char buf[2048];
250 ME_DumpStyleToBuf(&s->fmt, buf);
251 TRACE_(richedit_style)("%s\n", buf);
252 }
253
254 void ME_DumpStyleToBuf(CHARFORMAT2W *pFmt, char buf[2048])
255 {
256 /* FIXME only CHARFORMAT styles implemented */
257 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
258 char *p;
259 p = buf;
260 p += sprintf(p, "Font face: ");
261 if (pFmt->dwMask & CFM_FACE) {
262 WCHAR *q = pFmt->szFaceName;
263 while(*q) {
264 *p++ = (*q > 255) ? '?' : *q;
265 q++;
266 }
267 } else
268 p += sprintf(p, "N/A");
269
270 if (pFmt->dwMask & CFM_SIZE)
271 p += sprintf(p, "\nFont size: %d\n", pFmt->yHeight);
272 else
273 p += sprintf(p, "\nFont size: N/A\n");
274
275 if (pFmt->dwMask & CFM_OFFSET)
276 p += sprintf(p, "Char offset: %d\n", pFmt->yOffset);
277 else
278 p += sprintf(p, "Char offset: N/A\n");
279
280 if (pFmt->dwMask & CFM_CHARSET)
281 p += sprintf(p, "Font charset: %d\n", (int)pFmt->bCharSet);
282 else
283 p += sprintf(p, "Font charset: N/A\n");
284
285 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
286 ME_DumpStyleEffect(&p, "Font bold:", pFmt, CFM_BOLD);
287 ME_DumpStyleEffect(&p, "Font italic:", pFmt, CFM_ITALIC);
288 ME_DumpStyleEffect(&p, "Font underline:", pFmt, CFM_UNDERLINE);
289 ME_DumpStyleEffect(&p, "Font strikeout:", pFmt, CFM_STRIKEOUT);
290 ME_DumpStyleEffect(&p, "Hidden text:", pFmt, CFM_HIDDEN);
291 p += sprintf(p, "Text color: ");
292 if (pFmt->dwMask & CFM_COLOR)
293 {
294 if (pFmt->dwEffects & CFE_AUTOCOLOR)
295 p += sprintf(p, "auto\n");
296 else
297 p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
298 }
299 else
300 p += sprintf(p, "N/A\n");
301 ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
302 }
303
304
305 static void
306 ME_LogFontFromStyle(ME_Context* c, LOGFONTW *lf, const ME_Style *s)
307 {
308 ZeroMemory(lf, sizeof(LOGFONTW));
309 lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
310
311 lf->lfHeight = ME_twips2pointsY(c, -s->fmt.yHeight);
312
313 lf->lfWeight = FW_NORMAL;
314 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_BOLD)
315 lf->lfWeight = FW_BOLD;
316 if (s->fmt.dwMask & CFM_WEIGHT)
317 lf->lfWeight = s->fmt.wWeight;
318 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_ITALIC)
319 lf->lfItalic = 1;
320 if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_UNDERLINE | CFE_LINK))
321 lf->lfUnderline = 1;
322 if (s->fmt.dwMask & CFM_UNDERLINETYPE && s->fmt.bUnderlineType == CFU_CF1UNDERLINE)
323 lf->lfUnderline = 1;
324 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
325 lf->lfStrikeOut = 1;
326 if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_SUBSCRIPT|CFM_SUPERSCRIPT))
327 lf->lfHeight = (lf->lfHeight*2)/3;
328 /*lf.lfQuality = PROOF_QUALITY; */
329 if (s->fmt.dwMask & CFM_FACE)
330 lf->lfPitchAndFamily = s->fmt.bPitchAndFamily;
331 if (s->fmt.dwMask & CFM_CHARSET)
332 lf->lfCharSet = s->fmt.bCharSet;
333 }
334
335 void ME_CharFormatFromLogFont(HDC hDC, const LOGFONTW *lf, CHARFORMAT2W *fmt)
336 {
337 int ry;
338
339 ME_InitCharFormat2W(fmt);
340 ry = GetDeviceCaps(hDC, LOGPIXELSY);
341 lstrcpyW(fmt->szFaceName, lf->lfFaceName);
342 fmt->dwEffects = 0;
343 fmt->dwMask = CFM_WEIGHT|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_STRIKEOUT|CFM_SIZE|CFM_FACE|CFM_CHARSET;
344 fmt->wWeight = lf->lfWeight;
345 fmt->yHeight = -lf->lfHeight*1440/ry;
346 if (lf->lfWeight > FW_NORMAL) fmt->dwEffects |= CFM_BOLD;
347 if (lf->lfItalic) fmt->dwEffects |= CFM_ITALIC;
348 if (lf->lfUnderline) fmt->dwEffects |= CFM_UNDERLINE;
349 /* notice that if a logfont was created with underline due to CFM_LINK, this
350 would add an erroneous CFM_UNDERLINE. This isn't currently ever a problem. */
351 if (lf->lfStrikeOut) fmt->dwEffects |= CFM_STRIKEOUT;
352 fmt->bPitchAndFamily = lf->lfPitchAndFamily;
353 fmt->bCharSet = lf->lfCharSet;
354 }
355
356 static BOOL ME_IsFontEqual(const LOGFONTW *p1, const LOGFONTW *p2)
357 {
358 if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
359 return FALSE;
360 if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
361 return FALSE;
362 return TRUE;
363 }
364
365 HFONT ME_SelectStyleFont(ME_Context *c, ME_Style *s)
366 {
367 HFONT hOldFont;
368 LOGFONTW lf;
369 int i, nEmpty, nAge = 0x7FFFFFFF;
370 ME_FontCacheItem *item;
371 assert(s);
372
373 ME_LogFontFromStyle(c, &lf, s);
374
375 for (i=0; i<HFONT_CACHE_SIZE; i++)
376 c->editor->pFontCache[i].nAge++;
377 for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
378 {
379 item = &c->editor->pFontCache[i];
380 if (!item->nRefs)
381 {
382 if (item->nAge > nAge)
383 nEmpty = i, nAge = item->nAge;
384 }
385 if (item->hFont && ME_IsFontEqual(&item->lfSpecs, &lf))
386 break;
387 }
388 if (i < HFONT_CACHE_SIZE) /* found */
389 {
390 item = &c->editor->pFontCache[i];
391 TRACE_(richedit_style)("font reused %d\n", i);
392
393 s->hFont = item->hFont;
394 item->nRefs++;
395 }
396 else
397 {
398 item = &c->editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
399
400 assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
401 if (item->hFont) {
402 TRACE_(richedit_style)("font deleted %d\n", nEmpty);
403 DeleteObject(item->hFont);
404 item->hFont = NULL;
405 }
406 s->hFont = CreateFontIndirectW(&lf);
407 assert(s->hFont);
408 TRACE_(richedit_style)("font created %d\n", nEmpty);
409 item->hFont = s->hFont;
410 item->nRefs = 1;
411 item->lfSpecs = lf;
412 }
413 hOldFont = SelectObject(c->hDC, s->hFont);
414 /* should be cached too, maybe ? */
415 GetTextMetricsW(c->hDC, &s->tm);
416 return hOldFont;
417 }
418
419 void ME_UnselectStyleFont(ME_Context *c, ME_Style *s, HFONT hOldFont)
420 {
421 int i;
422
423 assert(s);
424 SelectObject(c->hDC, hOldFont);
425 for (i=0; i<HFONT_CACHE_SIZE; i++)
426 {
427 ME_FontCacheItem *pItem = &c->editor->pFontCache[i];
428 if (pItem->hFont == s->hFont && pItem->nRefs > 0)
429 {
430 pItem->nRefs--;
431 pItem->nAge = 0;
432 s->hFont = NULL;
433 return;
434 }
435 }
436 assert(0 == "UnselectStyleFont without SelectStyleFont");
437 }
438
439 static void ME_DestroyStyle(ME_Style *s) {
440 if (s->hFont)
441 {
442 DeleteObject(s->hFont);
443 s->hFont = NULL;
444 }
445 FREE_OBJ(s);
446 }
447
448 void ME_AddRefStyle(ME_Style *s)
449 {
450 assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
451 s->nRefs++;
452 all_refs++;
453 TRACE_(richedit_style)("ME_AddRefStyle %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
454 }
455
456 void ME_ReleaseStyle(ME_Style *s)
457 {
458 s->nRefs--;
459 all_refs--;
460 if (s->nRefs==0)
461 TRACE_(richedit_style)("destroy style %p, total refs=%d\n", s, all_refs);
462 else
463 TRACE_(richedit_style)("release style %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
464 if (!all_refs) TRACE("all style references freed (good!)\n");
465 assert(s->nRefs>=0);
466 if (!s->nRefs)
467 ME_DestroyStyle(s);
468 }
469
470 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor)
471 {
472 if (ME_IsSelection(editor))
473 {
474 ME_Cursor *from, *to;
475
476 ME_GetSelection(editor, &from, &to);
477 ME_AddRefStyle(from->pRun->member.run.style);
478 return from->pRun->member.run.style;
479 }
480 if (editor->pBuffer->pCharStyle) {
481 ME_AddRefStyle(editor->pBuffer->pCharStyle);
482 return editor->pBuffer->pCharStyle;
483 }
484 else
485 {
486 ME_Cursor *pCursor = &editor->pCursors[nCursor];
487 ME_DisplayItem *pRunItem = pCursor->pRun;
488 ME_DisplayItem *pPrevItem = NULL;
489 if (pCursor->nOffset) {
490 ME_Run *pRun = &pRunItem->member.run;
491 ME_AddRefStyle(pRun->style);
492 return pRun->style;
493 }
494 pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
495 if (pPrevItem->type == diRun)
496 {
497 ME_AddRefStyle(pPrevItem->member.run.style);
498 return pPrevItem->member.run.style;
499 }
500 else
501 {
502 ME_AddRefStyle(pRunItem->member.run.style);
503 return pRunItem->member.run.style;
504 }
505 }
506 }
507
508 void ME_SaveTempStyle(ME_TextEditor *editor)
509 {
510 ME_Style *old_style = editor->pBuffer->pCharStyle;
511 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
512 if (old_style)
513 ME_ReleaseStyle(old_style);
514 }
515
516 void ME_ClearTempStyle(ME_TextEditor *editor)
517 {
518 if (!editor->pBuffer->pCharStyle) return;
519 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
520 editor->pBuffer->pCharStyle = NULL;
521 }