]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32theme.c
cssstylefuncs: Remove base argument
[~andy/gtk] / gtk / gtkwin32theme.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  * Copyright (C) 2011 Red Hat, Inc.
4  *
5  * Authors: Carlos Garnacho <carlosg@gnome.org>
6  *          Cosimo Cecchi <cosimoc@gnome.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <config.h>
23
24 #include "gtkwin32themeprivate.h"
25
26 #include <gtksymboliccolor.h>
27
28 #ifdef G_OS_WIN32
29
30 #include <cairo-win32.h>
31
32 #define UXTHEME_DLL "uxtheme.dll"
33
34 static HINSTANCE uxtheme_dll = NULL;
35 static gboolean use_xp_theme = FALSE;
36 static OSVERSIONINFO os_version;
37 static HTHEME needs_alpha_fixup1 = NULL;
38 static HTHEME needs_alpha_fixup2 = NULL;
39 static HTHEME needs_alpha_fixup3 = NULL;
40 static HTHEME needs_alpha_fixup4 = NULL;
41 static HTHEME needs_alpha_fixup5 = NULL;
42 static HTHEME needs_alpha_fixup6 = NULL;
43 static HTHEME needs_alpha_fixup7 = NULL;
44
45 typedef HRESULT (FAR PASCAL *GetThemeSysFontFunc)           (HTHEME hTheme, int iFontID, OUT LOGFONTW *plf);
46 typedef int (FAR PASCAL *GetThemeSysSizeFunc)               (HTHEME hTheme, int iSizeId);
47 typedef COLORREF (FAR PASCAL *GetThemeSysColorFunc)         (HTHEME hTheme,
48                                                              int iColorID);
49 typedef HTHEME (FAR PASCAL *OpenThemeDataFunc)              (HWND hwnd,
50                                                              LPCWSTR pszClassList);
51 typedef HRESULT (FAR PASCAL *CloseThemeDataFunc)            (HTHEME theme);
52 typedef HRESULT (FAR PASCAL *DrawThemeBackgroundFunc)       (HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
53                                                              const RECT *pRect, const RECT *pClipRect);
54 typedef HRESULT (FAR PASCAL *EnableThemeDialogTextureFunc)  (HWND hwnd,
55                                                              DWORD dwFlags);
56 typedef BOOL (FAR PASCAL *IsThemeActiveFunc)                (VOID);
57 typedef BOOL (FAR PASCAL *IsAppThemedFunc)                  (VOID);
58 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
59                                                                       int iPartId,
60                                                                       int iStateId);
61 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
62                                                              HDC hdc,
63                                                              RECT *prc);
64 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc)          (HTHEME hTheme,
65                                                              HDC hdc,
66                                                              int iPartId,
67                                                              int iStateId,
68                                                              RECT *prc,
69                                                              int eSize,
70                                                              SIZE *psz);
71
72 static GetThemeSysFontFunc get_theme_sys_font = NULL;
73 static GetThemeSysColorFunc get_theme_sys_color = NULL;
74 static GetThemeSysSizeFunc get_theme_sys_metric = NULL;
75 static OpenThemeDataFunc open_theme_data = NULL;
76 static CloseThemeDataFunc close_theme_data = NULL;
77 static DrawThemeBackgroundFunc draw_theme_background = NULL;
78 static EnableThemeDialogTextureFunc enable_theme_dialog_texture = NULL;
79 static IsThemeActiveFunc is_theme_active = NULL;
80 static IsAppThemedFunc is_app_themed = NULL;
81 static IsThemeBackgroundPartiallyTransparentFunc is_theme_partially_transparent = NULL;
82 static DrawThemeParentBackgroundFunc draw_theme_parent_background = NULL;
83 static GetThemePartSizeFunc get_theme_part_size = NULL;
84
85 static GHashTable *hthemes_by_class = NULL;
86
87 static void
88 _gtk_win32_theme_init (void)
89 {
90   char *buf;
91   char dummy;
92   int n, k;
93
94   if (uxtheme_dll)
95     return;
96
97   n = GetSystemDirectory (&dummy, 0);
98   if (n <= 0)
99     return;
100
101   buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
102   k = GetSystemDirectory (buf, n);
103   if (k == 0 || k > n)
104     {
105       g_free (buf);
106       return;
107     }
108
109   if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
110     strcat (buf, G_DIR_SEPARATOR_S);
111   strcat (buf, UXTHEME_DLL);
112
113   uxtheme_dll = LoadLibrary (buf);
114   g_free (buf);
115
116   if (!uxtheme_dll)
117     return;
118
119   is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
120   if (is_app_themed)
121     {
122       is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
123       open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
124       close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
125       draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
126       enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
127       get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
128       get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
129       get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
130       is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
131       draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
132       get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
133     }
134
135   if (is_app_themed && is_theme_active)
136     {
137       use_xp_theme = (is_app_themed () && is_theme_active ());
138     }
139   else
140     {
141       use_xp_theme = FALSE;
142     }
143
144   hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
145
146   memset (&os_version, 0, sizeof (os_version));
147   os_version.dwOSVersionInfoSize = sizeof (os_version);
148   GetVersionEx (&os_version);
149   if (os_version.dwMajorVersion == 5)
150     {
151       needs_alpha_fixup1 = _gtk_win32_lookup_htheme_by_classname ("scrollbar");
152       needs_alpha_fixup2 = _gtk_win32_lookup_htheme_by_classname ("toolbar");
153       needs_alpha_fixup3 = _gtk_win32_lookup_htheme_by_classname ("button");
154       needs_alpha_fixup4 = _gtk_win32_lookup_htheme_by_classname ("header");
155       needs_alpha_fixup5 = _gtk_win32_lookup_htheme_by_classname ("trackbar");
156       needs_alpha_fixup6 = _gtk_win32_lookup_htheme_by_classname ("status");
157       needs_alpha_fixup7 = _gtk_win32_lookup_htheme_by_classname ("rebar");
158     }
159 }
160
161 HTHEME
162 _gtk_win32_lookup_htheme_by_classname (const char *class)
163 {
164   HTHEME theme;
165   guint16 *wclass;
166   char *lower;
167   
168   _gtk_win32_theme_init ();
169
170   lower = g_ascii_strdown (class, -1);
171
172   theme = (HTHEME)  g_hash_table_lookup (hthemes_by_class, lower);
173   if (theme)
174     {
175       g_free (lower);
176       return theme;
177     }
178
179   wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
180   theme  = open_theme_data (NULL, wclass);
181   g_free (wclass);
182
183   if (theme == NULL)
184     {
185       g_free (lower);
186       return NULL;
187     }
188
189   /* Takes ownership of lower: */
190   g_hash_table_insert (hthemes_by_class, lower, theme);
191
192   return theme;
193 }
194
195 #else
196
197 HTHEME
198 _gtk_win32_lookup_htheme_by_classname (const char *class)
199 {
200   return NULL;
201 }
202
203 #endif /* G_OS_WIN32 */
204
205 cairo_surface_t *
206 _gtk_win32_theme_part_create_surface (HTHEME theme,
207                                       int    xp_part,
208                                       int    state,
209                                       int    margins[4],
210                                       int    width,
211                                       int    height,
212                                       int   *x_offs_out,
213                                       int   *y_offs_out)
214 {
215   cairo_surface_t *surface;
216   GdkRGBA color;
217   cairo_t *cr;
218   int x_offs;
219   int y_offs;
220 #ifdef G_OS_WIN32
221   gboolean has_alpha;
222   HDC hdc;
223   RECT rect;
224   SIZE size;
225   HRESULT res;
226 #endif
227
228   x_offs = margins[3];
229   y_offs = margins[0];
230
231   width -= margins[3] + margins[1];
232   height -= margins[0] + margins[2];
233
234 #ifdef G_OS_WIN32
235   rect.left = 0;
236   rect.top = 0;
237   rect.right = width;
238   rect.bottom = height;
239
240   hdc = GetDC (NULL);
241   res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
242   ReleaseDC (NULL, hdc);
243
244   if (res == S_OK)
245     {
246       x_offs += (width - size.cx) / 2;
247       y_offs += (height - size.cy) / 2;
248   
249       width = size.cx;
250       height = size.cy;
251
252       rect.right = width;
253       rect.bottom = height;
254     }
255
256   has_alpha = is_theme_partially_transparent (theme, xp_part, state);
257   if (has_alpha)
258     surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
259   else
260     surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
261
262   hdc = cairo_win32_surface_get_dc (surface);
263
264   res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
265
266   /* XP Can't handle rendering some parts on an alpha target */
267   if (has_alpha && 
268       (theme == needs_alpha_fixup1 ||
269        theme == needs_alpha_fixup2 ||
270        (theme == needs_alpha_fixup3 && xp_part == 4) ||
271        theme == needs_alpha_fixup4 ||
272        theme == needs_alpha_fixup5 ||
273        theme == needs_alpha_fixup6 ||
274        theme == needs_alpha_fixup7))
275     {
276       cairo_surface_t *img = cairo_win32_surface_get_image (surface);
277       guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
278       int i, j;
279       GdiFlush ();
280
281       for (i = 0; i < width; i++)
282         {
283           for (j = 0; j < height; j++)
284             {
285               if (data[i+j*width] != 0)
286                 data[i+j*width] |= 0xff000000;
287             }
288         }
289     }
290
291   *x_offs_out = x_offs;
292   *y_offs_out = y_offs;
293
294   if (res == S_OK)
295     return surface;
296
297 #else /* !G_OS_WIN32 */
298   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
299 #endif /* G_OS_WIN32 */
300
301   cr = cairo_create (surface);
302   
303   /* XXX: Do something better here (like printing the theme parts) */
304   gdk_rgba_parse (&color, "pink");
305   gdk_cairo_set_source_rgba (cr, &color);
306   cairo_paint (cr);
307
308   cairo_destroy (cr);
309   
310   *x_offs_out = x_offs;
311   *y_offs_out = y_offs;
312
313   return surface;
314 }
315
316 int
317 _gtk_win32_theme_int_parse (GtkCssParser      *parser,
318                             int               *value)
319 {
320   char *class;
321   int arg;
322
323   if (_gtk_css_parser_try (parser,
324                            "-gtk-win32-size",
325                            TRUE))
326     {
327       if (!_gtk_css_parser_try (parser, "(", TRUE))
328         {
329           _gtk_css_parser_error (parser,
330                                  "Expected '(' after '-gtk-win32-size'");
331           return 0;
332         }
333
334       class = _gtk_css_parser_try_name (parser, TRUE);
335       if (class == NULL)
336         {
337           _gtk_css_parser_error (parser,
338                                  "Expected name as first argument to  '-gtk-win32-size'");
339           return 0;
340         }
341
342       if (! _gtk_css_parser_try (parser, ",", TRUE))
343         {
344           g_free (class);
345           _gtk_css_parser_error (parser,
346                                  "Expected ','");
347           return 0;
348         }
349
350       if (!_gtk_css_parser_try_int (parser, &arg))
351         {
352           g_free (class);
353           _gtk_css_parser_error (parser, "Expected a valid integer value");
354           return 0;
355         }
356
357       if (!_gtk_css_parser_try (parser, ")", TRUE))
358         {
359           _gtk_css_parser_error (parser,
360                                  "Expected ')'");
361           return 0;
362         }
363
364 #ifdef G_OS_WIN32
365       if (use_xp_theme && get_theme_sys_metric != NULL)
366         {
367           HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
368
369           /* If theme is NULL it will just return the GetSystemMetrics value */
370           *value = get_theme_sys_metric (theme, arg);
371         }
372       else
373         *value = GetSystemMetrics (arg);
374 #else
375       *value = 1;
376 #endif
377
378       g_free (class);
379
380       return 1;
381     }
382
383   return -1;
384 }
385
386 GtkSymbolicColor *
387 _gtk_win32_theme_color_parse (GtkCssParser *parser)
388 {
389   GtkSymbolicColor *color;
390   char *class;
391   int id;
392
393   class = _gtk_css_parser_try_name (parser, TRUE);
394   if (class == NULL)
395     {
396       _gtk_css_parser_error (parser,
397                              "Expected name as first argument to  '-gtk-win32-color'");
398       return NULL;
399     }
400
401   if (! _gtk_css_parser_try (parser, ",", TRUE))
402     {
403       g_free (class);
404       _gtk_css_parser_error (parser,
405                              "Expected ','");
406       return NULL;
407     }
408
409   if (!_gtk_css_parser_try_int (parser, &id))
410     {
411       g_free (class);
412       _gtk_css_parser_error (parser, "Expected a valid integer value");
413       return NULL;
414     }
415
416   color = gtk_symbolic_color_new_win32 (class, id);
417   g_free (class);
418   return color;
419 }
420
421 gboolean
422 _gtk_win32_theme_color_resolve (const char *theme_class,
423                                 gint id,
424                                 GdkRGBA *color)
425 {
426 #ifdef G_OS_WIN32
427   DWORD dcolor;
428
429   if (use_xp_theme && get_theme_sys_color != NULL)
430     {
431       HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
432
433       /* if theme is NULL, it will just return the GetSystemColor()
434          value */
435       dcolor = get_theme_sys_color (theme, id);
436     }
437   else
438     dcolor = GetSysColor (id);
439
440   color->alpha = 1.0;
441   color->red = GetRValue (dcolor) / 255.0;
442   color->green = GetGValue (dcolor) / 255.0;
443   color->blue = GetBValue (dcolor) / 255.0;
444 #else
445   gdk_rgba_parse (color, "pink");
446 #endif
447   return TRUE;
448 }
449
450 const char *
451 _gtk_win32_theme_get_default (void)
452 {
453 #ifdef G_OS_WIN32
454   _gtk_win32_theme_init ();
455   
456   if (use_xp_theme)
457     return (os_version.dwMajorVersion >= 6) ? "gtk-win32" : "gtk-win32-xp";
458 #endif
459   return "gtk-win32-classic";
460 }
461