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, see <http://www.gnu.org/licenses/>.
24 #include "gtkwin32themeprivate.h"
28 #include <cairo-win32.h>
30 #define UXTHEME_DLL "uxtheme.dll"
32 static HINSTANCE uxtheme_dll = NULL;
33 static gboolean use_xp_theme = FALSE;
34 static HTHEME needs_alpha_fixup = NULL;
36 typedef HRESULT (FAR PASCAL *GetThemeSysFontFunc) (HTHEME hTheme, int iFontID, OUT LOGFONTW *plf);
37 typedef int (FAR PASCAL *GetThemeSysSizeFunc) (HTHEME hTheme, int iSizeId);
38 typedef COLORREF (FAR PASCAL *GetThemeSysColorFunc) (HTHEME hTheme,
40 typedef HTHEME (FAR PASCAL *OpenThemeDataFunc) (HWND hwnd,
41 LPCWSTR pszClassList);
42 typedef HRESULT (FAR PASCAL *CloseThemeDataFunc) (HTHEME theme);
43 typedef HRESULT (FAR PASCAL *DrawThemeBackgroundFunc) (HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
44 const RECT *pRect, const RECT *pClipRect);
45 typedef HRESULT (FAR PASCAL *EnableThemeDialogTextureFunc) (HWND hwnd,
47 typedef BOOL (FAR PASCAL *IsThemeActiveFunc) (VOID);
48 typedef BOOL (FAR PASCAL *IsAppThemedFunc) (VOID);
49 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
52 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
55 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc) (HTHEME hTheme,
63 static GetThemeSysFontFunc get_theme_sys_font = NULL;
64 static GetThemeSysColorFunc get_theme_sys_color = NULL;
65 static GetThemeSysSizeFunc get_theme_sys_metric = NULL;
66 static OpenThemeDataFunc open_theme_data = NULL;
67 static CloseThemeDataFunc close_theme_data = NULL;
68 static DrawThemeBackgroundFunc draw_theme_background = NULL;
69 static EnableThemeDialogTextureFunc enable_theme_dialog_texture = NULL;
70 static IsThemeActiveFunc is_theme_active = NULL;
71 static IsAppThemedFunc is_app_themed = NULL;
72 static IsThemeBackgroundPartiallyTransparentFunc is_theme_partially_transparent = NULL;
73 static DrawThemeParentBackgroundFunc draw_theme_parent_background = NULL;
74 static GetThemePartSizeFunc get_theme_part_size = NULL;
76 static GHashTable *hthemes_by_class = NULL;
79 _gtk_win32_theme_init (void)
81 OSVERSIONINFO version;
89 n = GetSystemDirectory (&dummy, 0);
93 buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
94 k = GetSystemDirectory (buf, n);
101 if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
102 strcat (buf, G_DIR_SEPARATOR_S);
103 strcat (buf, UXTHEME_DLL);
105 uxtheme_dll = LoadLibrary (buf);
111 is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
114 is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
115 open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
116 close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
117 draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
118 enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
119 get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
120 get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
121 get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
122 is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
123 draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
124 get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
127 if (is_app_themed && is_theme_active)
129 use_xp_theme = (is_app_themed () && is_theme_active ());
133 use_xp_theme = FALSE;
136 hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
138 memset (&version, 0, sizeof (version));
139 version.dwOSVersionInfoSize = sizeof (version);
140 if (GetVersionEx (&version) && version.dwMajorVersion == 5)
141 needs_alpha_fixup = _gtk_win32_lookup_htheme_by_classname ("toolbar");
145 _gtk_win32_lookup_htheme_by_classname (const char *class)
151 _gtk_win32_theme_init ();
153 lower = g_ascii_strdown (class, -1);
155 theme = (HTHEME) g_hash_table_lookup (hthemes_by_class, lower);
162 wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
163 theme = open_theme_data (NULL, wclass);
172 /* Takes ownership of lower: */
173 g_hash_table_insert (hthemes_by_class, lower, theme);
181 _gtk_win32_lookup_htheme_by_classname (const char *class)
186 #endif /* G_OS_WIN32 */
189 _gtk_win32_theme_part_create_surface (HTHEME theme,
198 cairo_surface_t *surface;
214 width -= margins[3] + margins[1];
215 height -= margins[0] + margins[2];
221 rect.bottom = height;
224 res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
225 ReleaseDC (NULL, hdc);
229 x_offs += (width - size.cx) / 2;
230 y_offs += (height - size.cy) / 2;
236 rect.bottom = height;
239 has_alpha = is_theme_partially_transparent (theme, xp_part, state);
241 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
243 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
245 hdc = cairo_win32_surface_get_dc (surface);
247 res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
249 /* XP Can't handle rendering some parts on an alpha target */
250 if (theme == needs_alpha_fixup && has_alpha) {
251 cairo_surface_t *img = cairo_win32_surface_get_image (surface);
252 guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
255 for (int i = 0; i < width; i++)
257 for (int j = 0; j < height; j++)
259 if (data[i+j*width] != 0)
260 data[i+j*width] |= 0xff000000;
265 *x_offs_out = x_offs;
266 *y_offs_out = y_offs;
271 #else /* !G_OS_WIN32 */
272 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
273 #endif /* G_OS_WIN32 */
275 cr = cairo_create (surface);
277 /* XXX: Do something better here (like printing the theme parts) */
278 gdk_rgba_parse (&color, "pink");
279 gdk_cairo_set_source_rgba (cr, &color);
284 *x_offs_out = x_offs;
285 *y_offs_out = y_offs;
291 _gtk_win32_theme_int_parse (GtkCssParser *parser,
298 if (_gtk_css_parser_try (parser,
302 if (!_gtk_css_parser_try (parser, "(", TRUE))
304 _gtk_css_parser_error (parser,
305 "Expected '(' after '-gtk-win32-size'");
309 class = _gtk_css_parser_try_name (parser, TRUE);
312 _gtk_css_parser_error (parser,
313 "Expected name as first argument to '-gtk-win32-size'");
317 if (! _gtk_css_parser_try (parser, ",", TRUE))
320 _gtk_css_parser_error (parser,
325 if (!_gtk_css_parser_try_int (parser, &arg))
328 _gtk_css_parser_error (parser, "Expected a valid integer value");
332 if (!_gtk_css_parser_try (parser, ")", TRUE))
334 _gtk_css_parser_error (parser,
340 if (use_xp_theme && get_theme_sys_metric != NULL)
342 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
344 /* If theme is NULL it will just return the GetSystemMetrics value */
345 *value = get_theme_sys_metric (theme, arg);
348 *value = GetSystemMetrics (arg);
362 _gtk_win32_theme_color_parse (GtkCssParser *parser)
364 GtkSymbolicColor *color;
368 class = _gtk_css_parser_try_name (parser, TRUE);
371 _gtk_css_parser_error (parser,
372 "Expected name as first argument to '-gtk-win32-color'");
376 if (! _gtk_css_parser_try (parser, ",", TRUE))
379 _gtk_css_parser_error (parser,
384 if (!_gtk_css_parser_try_int (parser, &id))
387 _gtk_css_parser_error (parser, "Expected a valid integer value");
391 color = gtk_symbolic_color_new_win32 (class, id);
397 _gtk_win32_theme_color_resolve (const char *theme_class,
404 if (use_xp_theme && get_theme_sys_color != NULL)
406 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
408 /* if theme is NULL, it will just return the GetSystemColor()
410 dcolor = get_theme_sys_color (theme, id);
413 dcolor = GetSysColor (id);
416 color->red = GetRValue (dcolor) / 255.0;
417 color->green = GetGValue (dcolor) / 255.0;
418 color->blue = GetBValue (dcolor) / 255.0;
420 gdk_rgba_parse (color, "pink");