rpcrt4: Use the thread pool in the RPC server for processing packets.
[wine/multimedia.git] / dlls / riched20 / style.c
blobf420f091a2195efbcec943e01cbf9127edf0ffe2
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 CHARFORMAT2W *ME_ToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
30 if (from->cbSize == sizeof(CHARFORMATA))
32 CHARFORMATA *f = (CHARFORMATA *)from;
33 CopyMemory(to, f, sizeof(*f)-sizeof(f->szFaceName));
34 to->cbSize = sizeof(CHARFORMAT2W);
35 if (f->dwMask & CFM_FACE) {
36 MultiByteToWideChar(0, 0, f->szFaceName, -1, to->szFaceName, sizeof(to->szFaceName)/sizeof(WCHAR));
38 return to;
40 if (from->cbSize == sizeof(CHARFORMATW))
42 CHARFORMATW *f = (CHARFORMATW *)from;
43 CopyMemory(to, f, sizeof(*f));
44 /* theoretically, we don't need to zero the remaining memory */
45 ZeroMemory(((CHARFORMATW *)to)+1, sizeof(CHARFORMAT2W)-sizeof(CHARFORMATW));
46 to->cbSize = sizeof(CHARFORMAT2W);
47 return to;
49 if (from->cbSize == sizeof(CHARFORMAT2A))
51 CHARFORMATA *f = (CHARFORMATA *)from;
52 /* copy the A structure without face name */
53 CopyMemory(to, f, sizeof(CHARFORMATA)-sizeof(f->szFaceName));
54 /* convert face name */
55 if (f->dwMask & CFM_FACE)
56 MultiByteToWideChar(0, 0, f->szFaceName, -1, to->szFaceName, sizeof(to->szFaceName)/sizeof(WCHAR));
57 /* copy the rest of the 2A structure to 2W */
58 CopyMemory(1+((CHARFORMATW *)to), f+1, sizeof(CHARFORMAT2A)-sizeof(CHARFORMATA));
59 to->cbSize = sizeof(CHARFORMAT2W);
60 return to;
63 assert(from->cbSize >= sizeof(CHARFORMAT2W));
64 return from;
67 void ME_CopyToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
69 if (ME_ToCF2W(to, from) == from)
70 CopyMemory(to, from, sizeof(*from));
73 CHARFORMAT2W *ME_ToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
75 assert(from->cbSize == sizeof(CHARFORMAT2W));
76 if (to->cbSize == sizeof(CHARFORMATA))
78 CHARFORMATA *t = (CHARFORMATA *)to;
79 CopyMemory(t, from, sizeof(*t)-sizeof(t->szFaceName));
80 WideCharToMultiByte(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
81 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
82 return to;
84 if (to->cbSize == sizeof(CHARFORMATW))
86 CHARFORMATW *t = (CHARFORMATW *)to;
87 CopyMemory(t, from, sizeof(*t));
88 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
89 return to;
91 if (to->cbSize == sizeof(CHARFORMAT2A))
93 CHARFORMAT2A *t = (CHARFORMAT2A *)to;
94 /* copy the A structure without face name */
95 CopyMemory(t, from, sizeof(CHARFORMATA)-sizeof(t->szFaceName));
96 /* convert face name */
97 WideCharToMultiByte(0, 0, from->szFaceName, -1, t->szFaceName, sizeof(t->szFaceName), 0, 0);
98 /* copy the rest of the 2A structure to 2W */
99 CopyMemory(&t->wWeight, &from->wWeight, sizeof(CHARFORMAT2A)-sizeof(CHARFORMATA));
100 t->cbSize = sizeof(*t); /* it was overwritten by CopyMemory */
101 return to;
103 assert(to->cbSize >= sizeof(CHARFORMAT2W));
104 return from;
107 void ME_CopyToCFAny(CHARFORMAT2W *to, CHARFORMAT2W *from)
109 if (ME_ToCFAny(to, from) == from)
110 CopyMemory(to, from, to->cbSize);
113 ME_Style *ME_MakeStyle(CHARFORMAT2W *style) {
114 CHARFORMAT2W styledata;
115 ME_Style *s = ALLOC_OBJ(ME_Style);
117 style = ME_ToCF2W(&styledata, style);
118 memset(s, 0, sizeof(ME_Style));
119 if (style->cbSize <= sizeof(CHARFORMAT2W))
120 CopyMemory(&s->fmt, style, style->cbSize);
121 else
122 CopyMemory(&s->fmt, style, sizeof(CHARFORMAT2W));
123 s->fmt.cbSize = sizeof(CHARFORMAT2W);
125 s->nSequence = -2;
126 s->nRefs = 1;
127 s->hFont = NULL;
128 s->tm.tmAscent = -1;
129 all_refs++;
130 return s;
133 #define COPY_STYLE_ITEM(mask, member) \
134 if (style->dwMask & mask) { \
135 s->fmt.dwMask |= mask;\
136 s->fmt.member = style->member;\
139 #define COPY_STYLE_ITEM_MEMCPY(mask, member) \
140 if (style->dwMask & mask) { \
141 s->fmt.dwMask |= mask;\
142 CopyMemory(s->fmt.member, style->member, sizeof(style->member));\
145 void ME_InitCharFormat2W(CHARFORMAT2W *pFmt)
147 ZeroMemory(pFmt, sizeof(CHARFORMAT2W));
148 pFmt->cbSize = sizeof(CHARFORMAT2W);
151 ME_Style *ME_ApplyStyle(ME_Style *sSrc, CHARFORMAT2W *style)
153 CHARFORMAT2W styledata;
154 ME_Style *s = ME_MakeStyle(&sSrc->fmt);
155 style = ME_ToCF2W(&styledata, style);
156 COPY_STYLE_ITEM(CFM_ANIMATION, bAnimation);
157 COPY_STYLE_ITEM(CFM_BACKCOLOR, crBackColor);
158 COPY_STYLE_ITEM(CFM_CHARSET, bCharSet);
159 COPY_STYLE_ITEM(CFM_COLOR, crTextColor);
160 COPY_STYLE_ITEM_MEMCPY(CFM_FACE, szFaceName);
161 COPY_STYLE_ITEM(CFM_KERNING, wKerning);
162 COPY_STYLE_ITEM(CFM_LCID, lcid);
163 COPY_STYLE_ITEM(CFM_OFFSET, yOffset);
164 COPY_STYLE_ITEM(CFM_REVAUTHOR, bRevAuthor);
165 COPY_STYLE_ITEM(CFM_SIZE, yHeight);
166 COPY_STYLE_ITEM(CFM_SPACING, sSpacing);
167 COPY_STYLE_ITEM(CFM_STYLE, sStyle);
168 COPY_STYLE_ITEM(CFM_UNDERLINETYPE, bUnderlineType);
169 COPY_STYLE_ITEM(CFM_WEIGHT, wWeight);
171 s->fmt.dwEffects &= ~(style->dwMask);
172 s->fmt.dwEffects |= style->dwEffects & style->dwMask;
173 s->fmt.dwMask |= style->dwMask;
174 if (style->dwMask & CFM_COLOR)
176 if (style->dwEffects & CFE_AUTOCOLOR)
177 s->fmt.dwEffects |= CFE_AUTOCOLOR;
178 else
179 s->fmt.dwEffects &= ~CFE_AUTOCOLOR;
181 return s;
184 void ME_CopyCharFormat(CHARFORMAT2W *pDest, CHARFORMAT2W *pSrc)
186 /* using this with non-2W structs is forbidden */
187 assert(pSrc->cbSize == sizeof(CHARFORMAT2W));
188 assert(pDest->cbSize == sizeof(CHARFORMAT2W));
189 CopyMemory(pDest, pSrc, sizeof(CHARFORMAT2W));
192 static void ME_DumpStyleEffect(char **p, const char *name, CHARFORMAT2W *fmt, int mask)
194 *p += sprintf(*p, "%-22s%s\n", name, (fmt->dwMask & mask) ? ((fmt->dwEffects & mask) ? "YES" : "no") : "N/A");
197 void ME_DumpStyle(ME_Style *s)
199 char buf[2048];
200 ME_DumpStyleToBuf(&s->fmt, buf);
201 TRACE_(richedit_style)("%s\n", buf);
204 void ME_DumpStyleToBuf(CHARFORMAT2W *pFmt, char buf[2048])
206 /* FIXME only CHARFORMAT styles implemented */
207 /* this function sucks, doesn't check for buffer overruns but it's "good enough" as for debug code */
208 char *p;
209 p = buf;
210 p += sprintf(p, "Font face: ");
211 if (pFmt->dwMask & CFM_FACE) {
212 WCHAR *q = pFmt->szFaceName;
213 while(*q) {
214 *p++ = (*q > 255) ? '?' : *q;
215 q++;
217 } else
218 p += sprintf(p, "N/A");
220 if (pFmt->dwMask & CFM_SIZE)
221 p += sprintf(p, "\nFont size: %d\n", (int)pFmt->yHeight);
222 else
223 p += sprintf(p, "\nFont size: N/A\n");
225 if (pFmt->dwMask & CFM_OFFSET)
226 p += sprintf(p, "Char offset: %d\n", (int)pFmt->yOffset);
227 else
228 p += sprintf(p, "Char offset: N/A\n");
230 if (pFmt->dwMask & CFM_CHARSET)
231 p += sprintf(p, "Font charset: %d\n", (int)pFmt->bCharSet);
232 else
233 p += sprintf(p, "Font charset: N/A\n");
235 /* I'm assuming CFM_xxx and CFE_xxx are the same values, fortunately it IS true wrt used flags*/
236 ME_DumpStyleEffect(&p, "Font bold:", pFmt, CFM_BOLD);
237 ME_DumpStyleEffect(&p, "Font italic:", pFmt, CFM_ITALIC);
238 ME_DumpStyleEffect(&p, "Font underline:", pFmt, CFM_UNDERLINE);
239 ME_DumpStyleEffect(&p, "Font strikeout:", pFmt, CFM_STRIKEOUT);
240 ME_DumpStyleEffect(&p, "Hidden text:", pFmt, CFM_HIDDEN);
241 p += sprintf(p, "Text color: ");
242 if (pFmt->dwMask & CFM_COLOR)
244 if (pFmt->dwEffects & CFE_AUTOCOLOR)
245 p += sprintf(p, "auto\n");
246 else
247 p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
249 else
250 p += sprintf(p, "N/A\n");
251 ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
255 static void
256 ME_LogFontFromStyle(HDC hDC, LOGFONTW *lf, ME_Style *s, int nZoomNumerator, int nZoomDenominator)
258 int rx, ry;
259 rx = GetDeviceCaps(hDC, LOGPIXELSX);
260 ry = GetDeviceCaps(hDC, LOGPIXELSY);
261 ZeroMemory(lf, sizeof(LOGFONTW));
262 lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
264 if (nZoomNumerator == 0)
266 nZoomNumerator = 1;
267 nZoomDenominator = 1;
269 lf->lfHeight = -s->fmt.yHeight*ry*nZoomNumerator/nZoomDenominator/1440;
271 lf->lfWeight = 400;
272 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_BOLD)
273 lf->lfWeight = 700;
274 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_WEIGHT)
275 lf->lfWeight = s->fmt.wWeight;
276 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_ITALIC)
277 lf->lfItalic = 1;
278 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_UNDERLINE)
279 lf->lfUnderline = 1;
280 if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
281 lf->lfStrikeOut = 1;
282 if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_SUBSCRIPT|CFM_SUPERSCRIPT))
283 lf->lfHeight = (lf->lfHeight*2)/3;
284 /*lf.lfQuality = PROOF_QUALITY; */
285 lf->lfPitchAndFamily = s->fmt.bPitchAndFamily;
286 lf->lfCharSet = s->fmt.bCharSet;
290 BOOL ME_IsFontEqual(LOGFONTW *p1, LOGFONTW *p2)
292 if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
293 return FALSE;
294 if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
295 return FALSE;
296 return TRUE;
299 HFONT ME_SelectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s)
301 HFONT hOldFont;
302 LOGFONTW lf;
303 int i, nEmpty, nAge = 0x7FFFFFFF;
304 ME_FontCacheItem *item;
305 assert(hDC);
306 assert(s);
308 ME_LogFontFromStyle(hDC, &lf, s, editor->nZoomNumerator, editor->nZoomDenominator);
310 for (i=0; i<HFONT_CACHE_SIZE; i++)
311 editor->pFontCache[i].nAge++;
312 for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
314 item = &editor->pFontCache[i];
315 if (!item->nRefs)
317 if (item->nAge > nAge)
318 nEmpty = i, nAge = item->nAge;
320 if (item->hFont && ME_IsFontEqual(&item->lfSpecs, &lf))
321 break;
323 if (i < HFONT_CACHE_SIZE) /* found */
325 item = &editor->pFontCache[i];
326 TRACE_(richedit_style)("font reused %d\n", i);
328 s->hFont = item->hFont;
329 item->nRefs++;
331 else
333 item = &editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
335 assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
336 if (item->hFont) {
337 TRACE_(richedit_style)("font deleted %d\n", nEmpty);
338 DeleteObject(item->hFont);
339 item->hFont = NULL;
341 s->hFont = CreateFontIndirectW(&lf);
342 assert(s->hFont);
343 TRACE_(richedit_style)("font created %d\n", nEmpty);
344 item->hFont = s->hFont;
345 item->nRefs = 1;
346 memcpy(&item->lfSpecs, &lf, sizeof(LOGFONTW));
348 hOldFont = SelectObject(hDC, s->hFont);
349 /* should be cached too, maybe ? */
350 GetTextMetricsW(hDC, &s->tm);
351 return hOldFont;
354 void ME_UnselectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s, HFONT hOldFont)
356 int i;
358 assert(hDC);
359 assert(s);
360 SelectObject(hDC, hOldFont);
361 for (i=0; i<HFONT_CACHE_SIZE; i++)
363 ME_FontCacheItem *pItem = &editor->pFontCache[i];
364 if (pItem->hFont == s->hFont && pItem->nRefs > 0)
366 pItem->nRefs--;
367 pItem->nAge = 0;
368 s->hFont = NULL;
369 return;
372 assert(0 == "UnselectStyleFont without SelectStyleFont");
375 static void ME_DestroyStyle(ME_Style *s) {
376 if (s->hFont)
378 DeleteObject(s->hFont);
379 s->hFont = NULL;
381 FREE_OBJ(s);
384 void ME_AddRefStyle(ME_Style *s)
386 assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
387 s->nRefs++;
388 all_refs++;
391 void ME_ReleaseStyle(ME_Style *s)
393 s->nRefs--;
394 all_refs--;
395 if (s->nRefs==0)
396 TRACE_(richedit_style)("destroy style %p, total refs=%d\n", s, all_refs);
397 else
398 TRACE_(richedit_style)("release style %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
399 if (!all_refs) TRACE("all style references freed (good!)\n");
400 assert(s->nRefs>=0);
401 if (!s->nRefs)
402 ME_DestroyStyle(s);
405 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor) {
406 if (ME_IsSelection(editor))
408 ME_Cursor c;
409 int from, to;
411 ME_GetSelection(editor, &from, &to);
412 ME_CursorFromCharOfs(editor, from, &c);
413 ME_AddRefStyle(c.pRun->member.run.style);
414 return c.pRun->member.run.style;
416 if (editor->pBuffer->pCharStyle) {
417 ME_AddRefStyle(editor->pBuffer->pCharStyle);
418 return editor->pBuffer->pCharStyle;
420 else
422 ME_Cursor *pCursor = &editor->pCursors[nCursor];
423 ME_DisplayItem *pRunItem = pCursor->pRun;
424 ME_DisplayItem *pPrevItem = NULL;
425 if (pCursor->nOffset) {
426 ME_Run *pRun = &pRunItem->member.run;
427 ME_AddRefStyle(pRun->style);
428 return pRun->style;
430 pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
431 if (pPrevItem->type == diRun)
433 ME_AddRefStyle(pPrevItem->member.run.style);
434 return pPrevItem->member.run.style;
436 else
438 ME_AddRefStyle(pRunItem->member.run.style);
439 return pRunItem->member.run.style;
444 void ME_SaveTempStyle(ME_TextEditor *editor)
446 ME_Style *old_style = editor->pBuffer->pCharStyle;
447 editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
448 if (old_style)
449 ME_ReleaseStyle(old_style);
452 void ME_ClearTempStyle(ME_TextEditor *editor)
454 if (!editor->pBuffer->pCharStyle) return;
455 ME_ReleaseStyle(editor->pBuffer->pCharStyle);
456 editor->pBuffer->pCharStyle = NULL;