kernel32: FindExSearchLimitToDirectories has no effect on FindFirstFileEx.
[wine] / dlls / 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 CHARFORMAT2W *ME_ToCF2W(CHARFORMAT2W *to, CHARFORMAT2W *from)
29 {
30   if (from->cbSize == sizeof(CHARFORMATA))
31   {
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));
37     }
38     return to;
39   }
40   if (from->cbSize == sizeof(CHARFORMATW))
41   {
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;
48   }
49   if (from->cbSize == sizeof(CHARFORMAT2A))
50   {
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;
61   }
62
63   return (from->cbSize >= sizeof(CHARFORMAT2W)) ? from : NULL;
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, const 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, const 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_(richedit_style)("%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   ME_DumpStyleEffect(&p, "Hidden text:", pFmt, CFM_HIDDEN);
240   p += sprintf(p, "Text color:           ");
241   if (pFmt->dwMask & CFM_COLOR)
242   {
243     if (pFmt->dwEffects & CFE_AUTOCOLOR)
244       p += sprintf(p, "auto\n");
245     else
246       p += sprintf(p, "%06x\n", (int)pFmt->crTextColor);
247   }
248   else
249     p += sprintf(p, "N/A\n");
250   ME_DumpStyleEffect(&p, "Text protected:", pFmt, CFM_PROTECTED);
251 }
252
253
254 static void
255 ME_LogFontFromStyle(HDC hDC, LOGFONTW *lf, const ME_Style *s, int nZoomNumerator, int nZoomDenominator)
256 {
257   int rx, ry;
258   rx = GetDeviceCaps(hDC, LOGPIXELSX);
259   ry = GetDeviceCaps(hDC, LOGPIXELSY);
260   ZeroMemory(lf, sizeof(LOGFONTW));
261   lstrcpyW(lf->lfFaceName, s->fmt.szFaceName);
262
263   if (nZoomNumerator == 0)
264   {
265     nZoomNumerator = 1;
266     nZoomDenominator = 1;
267   }
268   lf->lfHeight = -s->fmt.yHeight*ry*nZoomNumerator/nZoomDenominator/1440;
269   
270   lf->lfWeight = 400;
271   if (s->fmt.dwEffects & s->fmt.dwMask & CFM_BOLD)
272     lf->lfWeight = 700;
273   if (s->fmt.dwEffects & s->fmt.dwMask & CFM_WEIGHT)
274     lf->lfWeight = s->fmt.wWeight;
275   if (s->fmt.dwEffects & s->fmt.dwMask & CFM_ITALIC)
276     lf->lfItalic = 1;
277   if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_UNDERLINE | CFE_LINK))
278     lf->lfUnderline = 1;
279   if (s->fmt.dwEffects & s->fmt.dwMask & CFM_STRIKEOUT)
280     lf->lfStrikeOut = 1;
281   if (s->fmt.dwEffects & s->fmt.dwMask & (CFM_SUBSCRIPT|CFM_SUPERSCRIPT))
282     lf->lfHeight = (lf->lfHeight*2)/3;
283 /*lf.lfQuality = PROOF_QUALITY; */
284   lf->lfPitchAndFamily = s->fmt.bPitchAndFamily;
285   lf->lfCharSet = s->fmt.bCharSet;
286 }
287
288 void ME_CharFormatFromLogFont(HDC hDC, const LOGFONTW *lf, CHARFORMAT2W *fmt)
289 {
290   int rx, ry;
291
292   ME_InitCharFormat2W(fmt);
293   rx = GetDeviceCaps(hDC, LOGPIXELSX);
294   ry = GetDeviceCaps(hDC, LOGPIXELSY);
295   lstrcpyW(fmt->szFaceName, lf->lfFaceName);
296   fmt->dwEffects = 0;
297   fmt->dwMask = CFM_WEIGHT|CFM_BOLD|CFM_ITALIC|CFM_UNDERLINE|CFM_STRIKEOUT|CFM_SIZE|CFM_FACE|CFM_CHARSET;
298   fmt->wWeight = lf->lfWeight;
299   fmt->yHeight = -lf->lfHeight*1440/ry;
300   if (lf->lfWeight>400) fmt->dwEffects |= CFM_BOLD;
301   if (lf->lfItalic) fmt->dwEffects |= CFM_ITALIC;
302   if (lf->lfUnderline) fmt->dwEffects |= CFM_UNDERLINE;
303   /* notice that if a logfont was created with underline due to CFM_LINK, this
304       would add an erronious CFM_UNDERLINE. This isn't currently ever a problem */
305   if (lf->lfStrikeOut) fmt->dwEffects |= CFM_STRIKEOUT;
306   fmt->bPitchAndFamily = lf->lfPitchAndFamily;
307   fmt->bCharSet = lf->lfCharSet;
308 }
309
310 static BOOL ME_IsFontEqual(const LOGFONTW *p1, const LOGFONTW *p2)
311 {
312   if (memcmp(p1, p2, sizeof(LOGFONTW)-sizeof(p1->lfFaceName)))
313     return FALSE;
314   if (lstrcmpW(p1->lfFaceName, p2->lfFaceName))
315     return FALSE;
316   return TRUE;
317 }
318
319 HFONT ME_SelectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s)
320 {
321   HFONT hOldFont;
322   LOGFONTW lf;
323   int i, nEmpty, nAge = 0x7FFFFFFF;
324   ME_FontCacheItem *item;
325   assert(hDC);
326   assert(s);
327   
328   ME_LogFontFromStyle(hDC, &lf, s, editor->nZoomNumerator, editor->nZoomDenominator);
329   
330   for (i=0; i<HFONT_CACHE_SIZE; i++)
331     editor->pFontCache[i].nAge++;
332   for (i=0, nEmpty=-1, nAge=0; i<HFONT_CACHE_SIZE; i++)
333   {
334     item = &editor->pFontCache[i];
335     if (!item->nRefs)
336     {
337       if (item->nAge > nAge)
338         nEmpty = i, nAge = item->nAge;
339     }
340     if (item->hFont && ME_IsFontEqual(&item->lfSpecs, &lf))
341       break;
342   }
343   if (i < HFONT_CACHE_SIZE) /* found */
344   {
345     item = &editor->pFontCache[i];
346     TRACE_(richedit_style)("font reused %d\n", i);
347
348     s->hFont = item->hFont;
349     item->nRefs++;
350   }
351   else
352   {
353     item = &editor->pFontCache[nEmpty]; /* this legal even when nEmpty == -1, as we don't dereference it */
354
355     assert(nEmpty != -1); /* otherwise we leak cache entries or get too many fonts at once*/
356     if (item->hFont) {
357       TRACE_(richedit_style)("font deleted %d\n", nEmpty);
358       DeleteObject(item->hFont);
359       item->hFont = NULL;
360     }
361     s->hFont = CreateFontIndirectW(&lf);
362     assert(s->hFont);
363     TRACE_(richedit_style)("font created %d\n", nEmpty);
364     item->hFont = s->hFont;
365     item->nRefs = 1;
366     memcpy(&item->lfSpecs, &lf, sizeof(LOGFONTW));
367   }
368   hOldFont = SelectObject(hDC, s->hFont);
369   /* should be cached too, maybe ? */
370   GetTextMetricsW(hDC, &s->tm);
371   return hOldFont;
372 }
373
374 void ME_UnselectStyleFont(ME_TextEditor *editor, HDC hDC, ME_Style *s, HFONT hOldFont)
375 {
376   int i;
377   
378   assert(hDC);
379   assert(s);
380   SelectObject(hDC, hOldFont);
381   for (i=0; i<HFONT_CACHE_SIZE; i++)
382   {
383     ME_FontCacheItem *pItem = &editor->pFontCache[i];
384     if (pItem->hFont == s->hFont && pItem->nRefs > 0)
385     {
386       pItem->nRefs--;
387       pItem->nAge = 0;
388       s->hFont = NULL;
389       return;
390     }
391   }
392   assert(0 == "UnselectStyleFont without SelectStyleFont");
393 }
394
395 static void ME_DestroyStyle(ME_Style *s) {
396   if (s->hFont)
397   {
398     DeleteObject(s->hFont);
399     s->hFont = NULL;
400   }
401   FREE_OBJ(s);
402 }
403
404 void ME_AddRefStyle(ME_Style *s)
405 {
406   assert(s->nRefs>0); /* style with 0 references isn't supposed to exist */
407   s->nRefs++;
408   all_refs++;
409 }
410
411 void ME_ReleaseStyle(ME_Style *s)
412 {
413   s->nRefs--;
414   all_refs--;
415   if (s->nRefs==0)
416     TRACE_(richedit_style)("destroy style %p, total refs=%d\n", s, all_refs);
417   else
418     TRACE_(richedit_style)("release style %p, new refs=%d, total refs=%d\n", s, s->nRefs, all_refs);
419   if (!all_refs) TRACE("all style references freed (good!)\n");
420   assert(s->nRefs>=0);
421   if (!s->nRefs)
422     ME_DestroyStyle(s);
423 }
424
425 ME_Style *ME_GetInsertStyle(ME_TextEditor *editor, int nCursor) {
426   if (ME_IsSelection(editor))
427   {
428     ME_Cursor c;
429     int from, to;
430     
431     ME_GetSelection(editor, &from, &to);
432     ME_CursorFromCharOfs(editor, from, &c);
433     ME_AddRefStyle(c.pRun->member.run.style);
434     return c.pRun->member.run.style;
435   }
436   if (editor->pBuffer->pCharStyle) {
437     ME_AddRefStyle(editor->pBuffer->pCharStyle);
438     return editor->pBuffer->pCharStyle;
439   }
440   else
441   {
442     ME_Cursor *pCursor = &editor->pCursors[nCursor];
443     ME_DisplayItem *pRunItem = pCursor->pRun;
444     ME_DisplayItem *pPrevItem = NULL;
445     if (pCursor->nOffset) {
446       ME_Run *pRun = &pRunItem->member.run;
447       ME_AddRefStyle(pRun->style);
448       return pRun->style;
449     }
450     pPrevItem = ME_FindItemBack(pRunItem, diRunOrParagraph);
451     if (pPrevItem->type == diRun)
452     {
453       ME_AddRefStyle(pPrevItem->member.run.style);
454       return pPrevItem->member.run.style;
455     }
456     else
457     {
458       ME_AddRefStyle(pRunItem->member.run.style);
459       return pRunItem->member.run.style;
460     }
461   }
462 }
463
464 void ME_SaveTempStyle(ME_TextEditor *editor)
465 {
466   ME_Style *old_style = editor->pBuffer->pCharStyle;
467   editor->pBuffer->pCharStyle = ME_GetInsertStyle(editor, 0);
468   if (old_style)
469     ME_ReleaseStyle(old_style);
470 }
471
472 void ME_ClearTempStyle(ME_TextEditor *editor)
473 {
474   if (!editor->pBuffer->pCharStyle) return;
475   ME_ReleaseStyle(editor->pBuffer->pCharStyle);
476   editor->pBuffer->pCharStyle = NULL;
477 }