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