Sync to Wine-20050725:
[reactos.git] / reactos / lib / 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25 static int all_refs = 0;
26
27 CHARFORMAT2W *ME_ToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
28 {
29 if (from->cbSize == sizeof(CHARFORMATA))
30 {
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));
36 }
37 return to;
38 }
39 if (from->cbSize == sizeof(CHARFORMATW))
40 {
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);
46 return to;
47 }
48 if (from->cbSize == sizeof(CHARFORMAT2A))
49 {
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);
59 return to;
60 }
61
62 assert(from->cbSize >= sizeof(CHARFORMAT2W));
63 return from;
64 }
65
66 void ME_CopyToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
67 {
68 if (ME_ToCF2W(to, from) == from)
69 CopyMemory(to, from, sizeof(*from));
70 }
71
72 CHARFORMAT2W *ME_ToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
73 {
74 assert(from->cbSize == sizeof(CHARFORMAT2W));
75 if (to->cbSize == sizeof(CHARFORMATA))
76 {
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 */
81 return to;
82 }
83 if (to->cbSize == sizeof(CHARFORMATW))
84 {
85 CHARFORMATW *t = (CHARFORMATW *)to;
86 CopyMemory(t, from, sizeof(*t));
87 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
88 return to;
89 }
90 if (to->cbSize == sizeof(CHARFORMAT2A))
91 {
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 */
100 return to;
101 }
102 assert(to->cbSize >= sizeof(CHARFORMAT2W));
103 return from;
104 }
105
106 void ME_CopyToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
107 {
108 if (ME_ToCFAny(to, from) == from)
109 CopyMemory(to, from, to->cbSize);
110 }
111
112 ME_Style *ME_MakeStyle(CHARFORMAT2W *style) {
113 CHARFORMAT2W styledata;
114 ME_Style *s = ALLOC_OBJ(ME_Style);
115
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);
120 else
121 CopyMemory(&s->fmt, style, sizeof(CHARFORMAT2W));
122 s->fmt.cbSize = sizeof(CHARFORMAT2W);
123
124 s->nSequence = -2;
125 s->nRefs = 1;
126 s->hFont = NULL;
127 s->tm.tmAscent = -1;
128 all_refs++;
129 return s;
130 }
131
132 #define COPY_STYLE_ITEM(mask, member) \
133 if (style->dwMask & mask) { \
134 s->fmt.dwMask |= mask;\
135 s->fmt.member = style->member;\
136 }
137
138 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
139 if (style->dwMask & mask) { \
140 s->fmt.dwMask |= mask;\
141 CopyMemory(s->fmt.member, style->member, sizeof(style->member));\
142 }
143
144 void ME_InitCharFormat2W(CHARFORMAT2W *pFmt)
145 {
146 ZeroMemory(pFmt, sizeof(CHARFORMAT2W));
147 pFmt->cbSize = sizeof(CHARFORMAT2W);
148 }
149
150 ME_Style *ME_ApplyStyle(ME_Style *sSrc, CHARFORMAT2W *style)
151 {
152 CHARFORMAT2W styledata;
153 ME_Style *s = ME_MakeStyle(&sSrc->fmt);
154 style = ME_ToCF2W(&styledata, style);
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 COPY_STYLE_ITEM(CFM_SIZE, yHeight);
165 COPY_STYLE_ITEM(CFM_SPACING, sSpacing);
166 COPY_STYLE_ITEM(CFM_STYLE, sStyle);
167 COPY_STYLE_ITEM(CFM_UNDERLINETYPE, bUnderlineType);
168 COPY_STYLE_ITEM(CFM_WEIGHT, wWeight);
169
170 s->fmt.dwEffects &= ~(style->dwMask);
171 s->fmt.dwEffects |= style->dwEffects & style->dwMask;
172 s->fmt.dwMask |= style->dwMask;
173 if (style->dwMask & CFM_COLOR)
174 {
175 if (style->dwEffects & CFE_AUTOCOLOR)
176 s->fmt.dwEffects |= CFE_AUTOCOLOR;
177 else
178 s->fmt.dwEffects &= ~CFE_AUTOCOLOR;
179 }
180 return s;
181 }
182
183 void ME_CopyCharFormat(CHARFORMAT2W *pDest, CHARFORMAT2W *pSrc)
184 {
185 /* using this with non-2W structs is forbidden */
186 assert(pSrc->cbSize == sizeof(CHARFORMAT2W));
187 assert(pDest->cbSize == sizeof(CHARFORMAT2W));
188 CopyMemory(pDest, pSrc, sizeof(CHARFORMAT2W));
189 }
190
191 static void ME_DumpStyleEffect(char **p, const char *name, CHARFORMAT2W *fmt, int mask)
192 {
193 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->dwEffects & mask) ? "YES" : "no") : "N/A");
194 }
195
196 void ME_DumpStyle(ME_Style *s)
197 {
198 char buf[2048];
199 ME_DumpStyleToBuf(&s->fmt, buf);
200 TRACE("%s\n", buf);
201 }
202
203 void ME_DumpStyleToBuf(CHARFORMAT2W *pFmt, char buf[2048])
204 {
205 /* FIXME only CHARFORMAT styles implemented */
206 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
207 char *p;
208 p = buf;
209 p += sprintf(p, "Font face: ");
210 if (pFmt->dwMask & CFM_FACE) {
211 WCHAR *q = pFmt->szFaceName;
212 while(*q) {
213 *p++ = (*q > 255) ? '?' : *q;
214 q++;
215 }
216 } else
217 p += sprintf(p, "N/A");
218
219 if (pFmt->dwMask & CFM_SIZE)
220 p += sprintf(p, "\nFont size: %d\n", (int)pFmt->yHeight);
221 else
222 p += sprintf(p, "\nFont size: N/A\n");
223
224 if (pFmt->dwMask & CFM_OFFSET)
225 p += sprintf(p, "Char offset: %d\n", (int)pFmt->yOffset);
226 else
227 p += sprintf(p, "Char offset: N/A\n");
228
229 if (pFmt->dwMask & CFM_CHARSET)
230 p += sprintf(p, "Font charset: %d\n", (int)pFmt->bCharSet);
231 else
232 p += sprintf(p, "Font charset: N/A\n");
233
234 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
235 ME_DumpStyleEffect(&p, "Font bold:", pFmt, CFM_BOLD);
236 ME_DumpStyleEffect(&p, "Font italic:", pFmt, CFM_ITALIC);
237 ME_DumpStyleEffect(&p, "Font underline:", pFmt, CFM_UNDERLINE);
238 ME_DumpStyleEffect(&p, "Font strikeout:", pFmt, CFM_STRIKEOUT);
239 p += sprintf(p, "Text color: ");
240 if (pFmt->dwMask & CFM_COLOR)
241 {
242 if (pFmt->dwEffects & CFE_AUTOCOLOR)
243 p += sprintf(p, "auto\n");
244 else
245 p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
246 }
247 else
248 p += sprintf(p, "N/A\n");
249 ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
250 }
251
252
253 static void
254 ME_LogFontFromStyle(HDC hDC, LOGFONTW *lf, ME_Style *s, int nZoomNumerator, int nZoomDenominator)
255 {
256 int rx, ry;
257 rx = GetDeviceCaps(hDC, LOGPIXELSX);
258 ry = GetDeviceCaps(hDC, LOGPIXELSY);
259 ZeroMemory(lf, sizeof(LOGFONTW));
260 lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
261
262 if (nZoomNumerator == 0)
263 {
264 nZoomNumerator = 1;
265 nZoomDenominator = 1;
266 }
267 lf->lfHeight = -s->fmt.yHeight*ry*nZoomNumerator/nZoomDenominator/1440;
268
269 lf->lfWeight = 400;
270 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_BOLD)
271 lf->lfWeight = 700;
272 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_WEIGHT)
273 lf->lfWeight = s->fmt.wWeight;
274 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_ITALIC)
275 lf->lfItalic = 1;
276 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_UNDERLINE)
277 lf->lfUnderline = 1;
278 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
279 lf->lfStrikeOut = 1;
280 if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_SUBSCRIPT|CFM_SUPERSCRIPT))
281 lf->lfHeight = (lf->lfHeight*2)/3;
282 /*lf.lfQuality = PROOF_QUALITY; */
283 lf->lfPitchAndFamily = s->fmt.bPitchAndFamily;
284 lf->lfCharSet = s->fmt.bCharSet;
285 }
286
287
288 BOOL ME_IsFontEqual(LOGFONTW *p1, LOGFONTW *p2)
289 {
290 if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
291 return FALSE;
292 if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
293 return FALSE;
294 return TRUE;
295 }
296
297 HFONT ME_SelectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s)
298 {
299 HFONT hOldFont;
300 LOGFONTW lf;
301 int i, nEmpty, nAge = 0x7FFFFFFF;
302 ME_FontCacheItem *item;
303 assert(hDC);
304 assert(s);
305
306 ME_LogFontFromStyle(hDC, &lf, s, editor->nZoomNumerator, editor->nZoomDenominator);
307
308 for (i=0; i<HFONT_CACHE_SIZE; i++)
309 editor->pFontCache[i].nAge++;
310 for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
311 {
312 item = &editor->pFontCache[i];
313 if (!item->nRefs)
314 {
315 if (item->nAge > nAge)
316 nEmpty = i, nAge = item->nAge;
317 }
318 if (ME_IsFontEqual(&item->lfSpecs, &lf))
319 break;
320 }
321 if (i < HFONT_CACHE_SIZE) /* found */
322 {
323 item = &editor->pFontCache[i];
324 TRACE("font reused %d\n", i);
325
326 s->hFont = item->hFont;
327 item->nRefs++;
328 }
329 else
330 {
331 item = &editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
332
333 assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
334 if (item->hFont) {
335 TRACE("font deleted %d\n", nEmpty);
336 DeleteObject(item->hFont);
337 item->hFont = NULL;
338 }
339 s->hFont = CreateFontIndirectW(&lf);
340 assert(s->hFont);
341 TRACE("font created %d\n", nEmpty);
342 item->hFont = s->hFont;
343 item->nRefs = 1;
344 memcpy(&item->lfSpecs, &lf, sizeof(LOGFONTW));
345 }
346 hOldFont = SelectObject(hDC, s->hFont);
347 /* should be cached too, maybe ? */
348 GetTextMetricsW(hDC, &s->tm);
349 return hOldFont;
350 }
351
352 void ME_UnselectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s, HFONT hOldFont)
353 {
354 int i;
355
356 assert(hDC);
357 assert(s);
358 SelectObject(hDC, hOldFont);
359 for (i=0; i<HFONT_CACHE_SIZE; i++)
360 {
361 ME_FontCacheItem *pItem = &editor->pFontCache[i];
362 if (pItem->hFont == s->hFont && pItem->nRefs > 0)
363 {
364 pItem->nRefs--;
365 pItem->nAge = 0;
366 s->hFont = NULL;
367 return;
368 }
369 }
370 assert(0 == "UnselectStyleFont without SelectStyleFont");
371 }
372
373 void ME_DestroyStyle(ME_Style *s) {
374 if (s->hFont)
375 {
376 DeleteObject(s->hFont);
377 s->hFont = NULL;
378 }
379 FREE_OBJ(s);
380 }
381
382 void ME_AddRefStyle(ME_Style *s)
383 {
384 assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
385 s->nRefs++;
386 all_refs++;
387 }
388
389 void ME_ReleaseStyle(ME_Style *s)
390 {
391 s->nRefs--;
392 all_refs--;
393 if (s->nRefs==0)
394 TRACE("destroy style %p, total refs=%d\n", s, all_refs);
395 else
396 TRACE("release style %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
397 if (!all_refs) TRACE("all style references freed (good!)\n");
398 assert(s->nRefs>=0);
399 if (!s->nRefs)
400 ME_DestroyStyle(s);
401 }
402
403 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor) {
404 if (ME_IsSelection(editor))
405 {
406 ME_Cursor c;
407 int from, to;
408
409 ME_GetSelection(editor, &from, &to);
410 ME_CursorFromCharOfs(editor, from, &c);
411 ME_AddRefStyle(c.pRun->member.run.style);
412 return c.pRun->member.run.style;
413 }
414 if (editor->pBuffer->pCharStyle) {
415 ME_AddRefStyle(editor->pBuffer->pCharStyle);
416 return editor->pBuffer->pCharStyle;
417 }
418 else
419 {
420 ME_Cursor *pCursor = &editor->pCursors[nCursor];
421 ME_DisplayItem *pRunItem = pCursor->pRun;
422 ME_DisplayItem *pPrevItem = NULL;
423 if (pCursor->nOffset) {
424 ME_Run *pRun = &pRunItem->member.run;
425 ME_AddRefStyle(pRun->style);
426 return pRun->style;
427 }
428 pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
429 if (pPrevItem->type == diRun)
430 {
431 ME_AddRefStyle(pPrevItem->member.run.style);
432 return pPrevItem->member.run.style;
433 }
434 else
435 {
436 ME_AddRefStyle(pRunItem->member.run.style);
437 return pRunItem->member.run.style;
438 }
439 }
440 }
441
442 void ME_SaveTempStyle(ME_TextEditor *editor)
443 {
444 ME_Style *old_style = editor->pBuffer->pCharStyle;
445 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
446 if (old_style)
447 ME_ReleaseStyle(old_style);
448 }
449
450 void ME_ClearTempStyle(ME_TextEditor *editor)
451 {
452 if (!editor->pBuffer->pCharStyle) return;
453 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
454 editor->pBuffer->pCharStyle = NULL;
455 }