1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3 * Copyright (C) 2011 Red Hat, Inc.
5 * Authors: Carlos Garnacho <carlosg@gnome.org>
6 * Cosimo Cecchi <cosimoc@gnome.org>
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.
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.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "gtkwin32themeprivate.h"
31 #include <cairo-win32.h>
33 typedef HANDLE HTHEME;
35 #define UXTHEME_DLL "uxtheme.dll"
37 static HINSTANCE uxtheme_dll = NULL;
38 static gboolean use_xp_theme = FALSE;
40 typedef HRESULT (FAR PASCAL *GetThemeSysFontFunc) (HTHEME hTheme, int iFontID, OUT LOGFONTW *plf);
41 typedef int (FAR PASCAL *GetThemeSysSizeFunc) (HTHEME hTheme, int iSizeId);
42 typedef COLORREF (FAR PASCAL *GetThemeSysColorFunc) (HTHEME hTheme,
44 typedef HTHEME (FAR PASCAL *OpenThemeDataFunc) (HWND hwnd,
45 LPCWSTR pszClassList);
46 typedef HRESULT (FAR PASCAL *CloseThemeDataFunc) (HTHEME theme);
47 typedef HRESULT (FAR PASCAL *DrawThemeBackgroundFunc) (HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
48 const RECT *pRect, const RECT *pClipRect);
49 typedef HRESULT (FAR PASCAL *EnableThemeDialogTextureFunc) (HWND hwnd,
51 typedef BOOL (FAR PASCAL *IsThemeActiveFunc) (VOID);
52 typedef BOOL (FAR PASCAL *IsAppThemedFunc) (VOID);
53 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
56 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
59 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc) (HTHEME hTheme,
67 static GetThemeSysFontFunc get_theme_sys_font = NULL;
68 static GetThemeSysColorFunc get_theme_sys_color = NULL;
69 static GetThemeSysSizeFunc get_theme_sys_metric = NULL;
70 static OpenThemeDataFunc open_theme_data = NULL;
71 static CloseThemeDataFunc close_theme_data = NULL;
72 static DrawThemeBackgroundFunc draw_theme_background = NULL;
73 static EnableThemeDialogTextureFunc enable_theme_dialog_texture = NULL;
74 static IsThemeActiveFunc is_theme_active = NULL;
75 static IsAppThemedFunc is_app_themed = NULL;
76 static IsThemeBackgroundPartiallyTransparentFunc is_theme_partially_transparent = NULL;
77 static DrawThemeParentBackgroundFunc draw_theme_parent_background = NULL;
78 static GetThemePartSizeFunc get_theme_part_size = NULL;
80 static GHashTable *hthemes_by_class = NULL;
83 _gtk_win32_theme_init (void)
92 n = GetSystemDirectory (&dummy, 0);
96 buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
97 k = GetSystemDirectory (buf, n);
104 if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
105 strcat (buf, G_DIR_SEPARATOR_S);
106 strcat (buf, UXTHEME_DLL);
108 uxtheme_dll = LoadLibrary (buf);
114 is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
117 is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
118 open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
119 close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
120 draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
121 enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
122 get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
123 get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
124 get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
125 is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
126 draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
127 get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
130 if (is_app_themed && is_theme_active)
132 use_xp_theme = (is_app_themed () && is_theme_active ());
136 use_xp_theme = FALSE;
139 hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
143 lookup_htheme_by_classname (const char *class)
149 lower = g_ascii_strdown (class, -1);
151 theme = (HTHEME) g_hash_table_lookup (hthemes_by_class, lower);
158 wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
159 theme = open_theme_data (NULL, wclass);
168 /* Takes ownership of lower: */
169 g_hash_table_insert (hthemes_by_class, lower, theme);
176 typedef void * HTHEME;
179 _gtk_win32_theme_init (void)
184 lookup_htheme_by_classname (const char *class)
189 #endif /* G_OS_WIN32 */
191 G_DEFINE_BOXED_TYPE_WITH_CODE (GtkWin32ThemePart, _gtk_win32_theme_part,
192 _gtk_win32_theme_part_ref, _gtk_win32_theme_part_unref,
193 _gtk_win32_theme_init() )
195 struct _GtkWin32ThemePart {
208 _gtk_win32_theme_part_new (const char *class,
209 int xp_part, int state,
210 int xp_part2, int state2,
213 GtkWin32ThemePart *part;
215 part = g_slice_new0 (GtkWin32ThemePart);
218 part->theme = lookup_htheme_by_classname (class);
219 part->part = xp_part;
221 part->part2 = xp_part2;
222 part->state2 = state2;
223 part->over_alpha = over_alpha;
229 _gtk_win32_theme_part_ref (GtkWin32ThemePart *part)
231 g_return_val_if_fail (part != NULL, NULL);
239 _gtk_win32_theme_part_unref (GtkWin32ThemePart *part)
241 g_return_if_fail (part != NULL);
245 if (part->ref_count == 0)
247 g_slice_free (GtkWin32ThemePart, part);
252 _gtk_win32_theme_part_parse (GtkCssParser *parser,
257 int xp_part, state, xp_part2, state2;
259 GtkWin32ThemePart *theme_part;
261 if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
266 g_value_unset (value);
267 g_value_init (value, GTK_TYPE_WIN32_THEME_PART);
269 if (!_gtk_css_parser_try (parser, "(", TRUE))
271 _gtk_css_parser_error (parser,
272 "Expected '(' after '-gtk-win32-theme-part'");
276 class = _gtk_css_parser_try_name (parser, TRUE);
279 _gtk_css_parser_error (parser,
280 "Expected name as first argument to '-gtk-win32-theme-part'");
284 if (! _gtk_css_parser_try (parser, ",", TRUE))
287 _gtk_css_parser_error (parser,
292 if (!_gtk_css_parser_try_int (parser, &xp_part))
295 _gtk_css_parser_error (parser, "Expected a valid integer value");
299 if (!_gtk_css_parser_try_int (parser, &state))
302 _gtk_css_parser_error (parser, "Expected a valid integer value");
311 if ( _gtk_css_parser_try (parser, ",", TRUE))
314 if ( _gtk_css_parser_try (parser, "over", TRUE))
316 if (!_gtk_css_parser_try (parser, "(", TRUE))
318 _gtk_css_parser_error (parser,
319 "Expected '(' after 'over'");
323 if (!_gtk_css_parser_try_int (parser, &xp_part2))
326 _gtk_css_parser_error (parser, "Expected a valid integer value");
330 if (!_gtk_css_parser_try_int (parser, &state2))
333 _gtk_css_parser_error (parser, "Expected a valid integer value");
337 if ( _gtk_css_parser_try (parser, ",", TRUE))
339 if (!_gtk_css_parser_try_double (parser, &over_alpha))
342 _gtk_css_parser_error (parser, "Expected a valid double value");
347 if (!_gtk_css_parser_try (parser, ")", TRUE))
349 _gtk_css_parser_error (parser,
350 "Expected ')' at end of 'over'");
357 if (!_gtk_css_parser_try (parser, ")", TRUE))
360 _gtk_css_parser_error (parser,
365 theme_part = _gtk_win32_theme_part_new (class,
371 g_value_take_boxed (value, theme_part);
377 _gtk_win32_theme_part_create_surface (GtkWin32ThemePart *part,
383 cairo_surface_t *surface;
388 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
389 hdc = cairo_win32_surface_get_dc (surface);
394 rect.bottom = height;
396 res = draw_theme_background (part->theme, hdc, xp_part, state, &rect, &rect);
403 _gtk_win32_theme_part_render (GtkWin32ThemePart *part,
408 cairo_surface_t *surface, *surface2, *image;
409 cairo_pattern_t *pattern;
411 cairo_matrix_t matrix;
412 cairo_user_data_key_t key;
414 surface = _gtk_win32_theme_part_create_surface (part, part->part, part->state,
417 if (part->state2 >= 0)
419 surface2 = _gtk_win32_theme_part_create_surface (part, part->part2, part->state2,
423 cr = cairo_create (surface);
425 pattern = cairo_pattern_create_for_surface (surface2);
426 cairo_set_source (cr, pattern);
427 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
428 cairo_paint_with_alpha (cr, part->over_alpha);
431 cairo_pattern_destroy (pattern);
433 cairo_surface_destroy (surface2);
436 /* We need to return an image surface, as that is what the code expects in order
438 image = cairo_win32_surface_get_image (surface);
439 pattern = cairo_pattern_create_for_surface (cairo_surface_reference (image));
441 cairo_matrix_init_scale (&matrix,
444 cairo_pattern_set_matrix (pattern, &matrix);
446 /* We can't immediately destroy the surface, because that would free the data
447 the image surface refers too. Instead we destroy it with the pattern. */
448 cairo_pattern_set_user_data (pattern,
450 surface, (cairo_destroy_func_t) cairo_surface_destroy);
456 gdk_rgba_parse (&color, "pink");
458 return cairo_pattern_create_rgb (color.red, color.green, color.blue);
463 _gtk_win32_theme_int_parse (GtkCssParser *parser,
470 if (_gtk_css_parser_try (parser,
474 if (!_gtk_css_parser_try (parser, "(", TRUE))
476 _gtk_css_parser_error (parser,
477 "Expected '(' after '-gtk-win32-size'");
481 class = _gtk_css_parser_try_name (parser, TRUE);
484 _gtk_css_parser_error (parser,
485 "Expected name as first argument to '-gtk-win32-size'");
489 if (! _gtk_css_parser_try (parser, ",", TRUE))
492 _gtk_css_parser_error (parser,
497 if (!_gtk_css_parser_try_int (parser, &arg))
500 _gtk_css_parser_error (parser, "Expected a valid integer value");
504 if (!_gtk_css_parser_try (parser, ")", TRUE))
506 _gtk_css_parser_error (parser,
512 if (use_xp_theme && get_theme_sys_metric != NULL)
514 HTHEME theme = lookup_htheme_by_classname (class);
516 /* If theme is NULL it will just return the GetSystemMetrics value */
517 *value = get_theme_sys_metric (theme, arg);
520 *value = GetSystemMetrics (arg);
534 _gtk_win32_theme_color_parse (GtkCssParser *parser)
536 GtkSymbolicColor *color;
540 class = _gtk_css_parser_try_name (parser, TRUE);
543 _gtk_css_parser_error (parser,
544 "Expected name as first argument to '-gtk-win32-color'");
548 if (! _gtk_css_parser_try (parser, ",", TRUE))
551 _gtk_css_parser_error (parser,
556 if (!_gtk_css_parser_try_int (parser, &id))
559 _gtk_css_parser_error (parser, "Expected a valid integer value");
563 color = gtk_symbolic_color_new_win32 (class, id);
569 _gtk_win32_theme_color_resolve (const char *theme_class,
576 if (use_xp_theme && get_theme_sys_color != NULL)
578 HTHEME theme = lookup_htheme_by_classname (theme_class);
580 /* if theme is NULL, it will just return the GetSystemColor()
582 dcolor = get_theme_sys_color (theme, id);
585 dcolor = GetSysColor (id);
588 color->red = GetRValue (dcolor) / 255.0;
589 color->green = GetGValue (dcolor) / 255.0;
590 color->blue = GetBValue (dcolor) / 255.0;
592 gdk_rgba_parse (color, "pink");