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 {
206 _gtk_win32_theme_part_new (const char *class,
207 int xp_part, int state,
208 int xp_part2, int state2)
210 GtkWin32ThemePart *part;
212 part = g_slice_new0 (GtkWin32ThemePart);
215 part->theme = lookup_htheme_by_classname (class);
216 part->part = xp_part;
218 part->part2 = xp_part2;
219 part->state2 = state2;
225 _gtk_win32_theme_part_ref (GtkWin32ThemePart *part)
227 g_return_val_if_fail (part != NULL, NULL);
235 _gtk_win32_theme_part_unref (GtkWin32ThemePart *part)
237 g_return_if_fail (part != NULL);
241 if (part->ref_count == 0)
243 g_slice_free (GtkWin32ThemePart, part);
248 _gtk_win32_theme_part_parse (GtkCssParser *parser,
253 int xp_part, state, xp_part2, state2;
254 GtkWin32ThemePart *theme_part;
256 if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
261 g_value_unset (value);
262 g_value_init (value, GTK_TYPE_WIN32_THEME_PART);
264 if (!_gtk_css_parser_try (parser, "(", TRUE))
266 _gtk_css_parser_error (parser,
267 "Expected '(' after '-gtk-win32-theme-part'");
271 class = _gtk_css_parser_try_name (parser, TRUE);
274 _gtk_css_parser_error (parser,
275 "Expected name as first argument to '-gtk-win32-theme-part'");
279 if (! _gtk_css_parser_try (parser, ",", TRUE))
282 _gtk_css_parser_error (parser,
287 if (!_gtk_css_parser_try_int (parser, &xp_part))
290 _gtk_css_parser_error (parser, "Expected a valid integer value");
294 if (!_gtk_css_parser_try_int (parser, &state))
297 _gtk_css_parser_error (parser, "Expected a valid integer value");
303 if ( _gtk_css_parser_try (parser, ",", TRUE))
305 if (!_gtk_css_parser_try_int (parser, &xp_part2))
308 _gtk_css_parser_error (parser, "Expected a valid integer value");
312 if (!_gtk_css_parser_try_int (parser, &state2))
315 _gtk_css_parser_error (parser, "Expected a valid integer value");
321 if (!_gtk_css_parser_try (parser, ")", TRUE))
324 _gtk_css_parser_error (parser,
329 theme_part = _gtk_win32_theme_part_new (class,
334 g_value_take_boxed (value, theme_part);
340 _gtk_win32_theme_part_create_surface (GtkWin32ThemePart *part,
346 cairo_surface_t *surface;
351 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
352 hdc = cairo_win32_surface_get_dc (surface);
357 rect.bottom = height;
359 res = draw_theme_background (part->theme, hdc, xp_part, state, &rect, &rect);
366 _gtk_win32_theme_part_render (GtkWin32ThemePart *part,
371 cairo_surface_t *surface, *surface2, *image;
372 cairo_pattern_t *pattern;
374 cairo_matrix_t matrix;
375 cairo_user_data_key_t key;
377 surface = _gtk_win32_theme_part_create_surface (part, part->part, part->state,
380 if (part->state2 >= 0)
382 surface2 = _gtk_win32_theme_part_create_surface (part, part->part2, part->state2,
385 cr = cairo_create (surface);
387 pattern = cairo_pattern_create_for_surface (surface2);
388 cairo_set_source (cr, pattern);
389 cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
393 cairo_pattern_destroy (pattern);
394 cairo_surface_destroy (surface2);
397 /* We need to return an image surface, as that is what the code expects in order
399 image = cairo_win32_surface_get_image (surface);
400 pattern = cairo_pattern_create_for_surface (cairo_surface_reference (image));
402 cairo_matrix_init_scale (&matrix,
405 cairo_pattern_set_matrix (pattern, &matrix);
407 /* We can't immediately destroy the surface, because that would free the data
408 the image surface refers too. Instead we destroy it with the pattern. */
409 cairo_pattern_set_user_data (pattern,
411 surface, (cairo_destroy_func_t) cairo_surface_destroy);
417 gdk_rgba_parse (&color, "pink");
419 return cairo_pattern_create_rgb (color.red, color.green, color.blue);
424 _gtk_win32_theme_int_parse (GtkCssParser *parser,
431 if (_gtk_css_parser_try (parser,
435 if (!_gtk_css_parser_try (parser, "(", TRUE))
437 _gtk_css_parser_error (parser,
438 "Expected '(' after '-gtk-win32-size'");
442 class = _gtk_css_parser_try_name (parser, TRUE);
445 _gtk_css_parser_error (parser,
446 "Expected name as first argument to '-gtk-win32-size'");
450 if (! _gtk_css_parser_try (parser, ",", TRUE))
453 _gtk_css_parser_error (parser,
458 if (!_gtk_css_parser_try_int (parser, &arg))
461 _gtk_css_parser_error (parser, "Expected a valid integer value");
465 if (!_gtk_css_parser_try (parser, ")", TRUE))
467 _gtk_css_parser_error (parser,
473 if (use_xp_theme && get_theme_sys_metric != NULL)
475 HTHEME theme = lookup_htheme_by_classname (class);
477 /* If theme is NULL it will just return the GetSystemMetrics value */
478 *value = get_theme_sys_metric (theme, arg);
481 *value = GetSystemMetrics (arg);
495 _gtk_win32_theme_color_parse (GtkCssParser *parser)
497 GtkSymbolicColor *color;
501 class = _gtk_css_parser_try_name (parser, TRUE);
504 _gtk_css_parser_error (parser,
505 "Expected name as first argument to '-gtk-win32-color'");
509 if (! _gtk_css_parser_try (parser, ",", TRUE))
512 _gtk_css_parser_error (parser,
517 if (!_gtk_css_parser_try_int (parser, &id))
520 _gtk_css_parser_error (parser, "Expected a valid integer value");
524 color = gtk_symbolic_color_new_win32 (class, id);
530 _gtk_win32_theme_color_resolve (const char *theme_class,
537 if (use_xp_theme && get_theme_sys_color != NULL)
539 HTHEME theme = lookup_htheme_by_classname (theme_class);
541 /* if theme is NULL, it will just return the GetSystemColor()
543 dcolor = get_theme_sys_color (theme, id);
546 dcolor = GetSysColor (id);
549 color->red = GetRValue (dcolor) / 255.0;
550 color->green = GetGValue (dcolor) / 255.0;
551 color->blue = GetBValue (dcolor) / 255.0;
553 gdk_rgba_parse (color, "pink");