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 OSVERSIONINFO os_version;
35 static HTHEME needs_alpha_fixup1 = NULL;
36 static HTHEME needs_alpha_fixup2 = NULL;
37 static HTHEME needs_alpha_fixup3 = NULL;
38 static HTHEME needs_alpha_fixup4 = NULL;
39 static HTHEME needs_alpha_fixup5 = NULL;
40 static HTHEME needs_alpha_fixup6 = NULL;
41 static HTHEME needs_alpha_fixup7 = NULL;
43 typedef HRESULT (FAR PASCAL *GetThemeSysFontFunc) (HTHEME hTheme, int iFontID, OUT LOGFONTW *plf);
44 typedef int (FAR PASCAL *GetThemeSysSizeFunc) (HTHEME hTheme, int iSizeId);
45 typedef COLORREF (FAR PASCAL *GetThemeSysColorFunc) (HTHEME hTheme,
47 typedef HTHEME (FAR PASCAL *OpenThemeDataFunc) (HWND hwnd,
48 LPCWSTR pszClassList);
49 typedef HRESULT (FAR PASCAL *CloseThemeDataFunc) (HTHEME theme);
50 typedef HRESULT (FAR PASCAL *DrawThemeBackgroundFunc) (HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
51 const RECT *pRect, const RECT *pClipRect);
52 typedef HRESULT (FAR PASCAL *EnableThemeDialogTextureFunc) (HWND hwnd,
54 typedef BOOL (FAR PASCAL *IsThemeActiveFunc) (VOID);
55 typedef BOOL (FAR PASCAL *IsAppThemedFunc) (VOID);
56 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
59 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
62 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc) (HTHEME hTheme,
70 static GetThemeSysFontFunc get_theme_sys_font = NULL;
71 static GetThemeSysColorFunc get_theme_sys_color = NULL;
72 static GetThemeSysSizeFunc get_theme_sys_metric = NULL;
73 static OpenThemeDataFunc open_theme_data = NULL;
74 static CloseThemeDataFunc close_theme_data = NULL;
75 static DrawThemeBackgroundFunc draw_theme_background = NULL;
76 static EnableThemeDialogTextureFunc enable_theme_dialog_texture = NULL;
77 static IsThemeActiveFunc is_theme_active = NULL;
78 static IsAppThemedFunc is_app_themed = NULL;
79 static IsThemeBackgroundPartiallyTransparentFunc is_theme_partially_transparent = NULL;
80 static DrawThemeParentBackgroundFunc draw_theme_parent_background = NULL;
81 static GetThemePartSizeFunc get_theme_part_size = NULL;
83 static GHashTable *hthemes_by_class = NULL;
86 _gtk_win32_theme_init (void)
95 n = GetSystemDirectory (&dummy, 0);
99 buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
100 k = GetSystemDirectory (buf, n);
107 if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
108 strcat (buf, G_DIR_SEPARATOR_S);
109 strcat (buf, UXTHEME_DLL);
111 uxtheme_dll = LoadLibrary (buf);
117 is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
120 is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
121 open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
122 close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
123 draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
124 enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
125 get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
126 get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
127 get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
128 is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
129 draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
130 get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
133 if (is_app_themed && is_theme_active)
135 use_xp_theme = (is_app_themed () && is_theme_active ());
139 use_xp_theme = FALSE;
142 hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
144 memset (&os_version, 0, sizeof (os_version));
145 os_version.dwOSVersionInfoSize = sizeof (os_version);
146 GetVersionEx (&os_version);
147 if (os_version.dwMajorVersion == 5)
149 needs_alpha_fixup1 = _gtk_win32_lookup_htheme_by_classname ("scrollbar");
150 needs_alpha_fixup2 = _gtk_win32_lookup_htheme_by_classname ("toolbar");
151 needs_alpha_fixup3 = _gtk_win32_lookup_htheme_by_classname ("button");
152 needs_alpha_fixup4 = _gtk_win32_lookup_htheme_by_classname ("header");
153 needs_alpha_fixup5 = _gtk_win32_lookup_htheme_by_classname ("trackbar");
154 needs_alpha_fixup6 = _gtk_win32_lookup_htheme_by_classname ("status");
155 needs_alpha_fixup7 = _gtk_win32_lookup_htheme_by_classname ("rebar");
160 _gtk_win32_lookup_htheme_by_classname (const char *class)
166 _gtk_win32_theme_init ();
168 lower = g_ascii_strdown (class, -1);
170 theme = (HTHEME) g_hash_table_lookup (hthemes_by_class, lower);
177 wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
178 theme = open_theme_data (NULL, wclass);
187 /* Takes ownership of lower: */
188 g_hash_table_insert (hthemes_by_class, lower, theme);
196 _gtk_win32_lookup_htheme_by_classname (const char *class)
201 #endif /* G_OS_WIN32 */
204 _gtk_win32_theme_part_create_surface (HTHEME theme,
213 cairo_surface_t *surface;
229 width -= margins[3] + margins[1];
230 height -= margins[0] + margins[2];
236 rect.bottom = height;
239 res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
240 ReleaseDC (NULL, hdc);
244 x_offs += (width - size.cx) / 2;
245 y_offs += (height - size.cy) / 2;
251 rect.bottom = height;
254 has_alpha = is_theme_partially_transparent (theme, xp_part, state);
256 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
258 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
260 hdc = cairo_win32_surface_get_dc (surface);
262 res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
264 /* XP Can't handle rendering some parts on an alpha target */
266 (theme == needs_alpha_fixup1 ||
267 theme == needs_alpha_fixup2 ||
268 (theme == needs_alpha_fixup3 && xp_part == 4) ||
269 theme == needs_alpha_fixup4 ||
270 theme == needs_alpha_fixup5 ||
271 theme == needs_alpha_fixup6 ||
272 theme == needs_alpha_fixup7))
274 cairo_surface_t *img = cairo_win32_surface_get_image (surface);
275 guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
279 for (i = 0; i < width; i++)
281 for (j = 0; j < height; j++)
283 if (data[i+j*width] != 0)
284 data[i+j*width] |= 0xff000000;
289 *x_offs_out = x_offs;
290 *y_offs_out = y_offs;
295 #else /* !G_OS_WIN32 */
296 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
297 #endif /* G_OS_WIN32 */
299 cr = cairo_create (surface);
301 /* XXX: Do something better here (like printing the theme parts) */
302 gdk_rgba_parse (&color, "pink");
303 gdk_cairo_set_source_rgba (cr, &color);
308 *x_offs_out = x_offs;
309 *y_offs_out = y_offs;
315 _gtk_win32_theme_int_parse (GtkCssParser *parser,
321 if (_gtk_css_parser_try (parser,
325 if (!_gtk_css_parser_try (parser, "(", TRUE))
327 _gtk_css_parser_error (parser,
328 "Expected '(' after '-gtk-win32-size'");
332 class = _gtk_css_parser_try_name (parser, TRUE);
335 _gtk_css_parser_error (parser,
336 "Expected name as first argument to '-gtk-win32-size'");
340 if (! _gtk_css_parser_try (parser, ",", TRUE))
343 _gtk_css_parser_error (parser,
348 if (!_gtk_css_parser_try_int (parser, &arg))
351 _gtk_css_parser_error (parser, "Expected a valid integer value");
355 if (!_gtk_css_parser_try (parser, ")", TRUE))
357 _gtk_css_parser_error (parser,
363 if (use_xp_theme && get_theme_sys_metric != NULL)
365 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
367 /* If theme is NULL it will just return the GetSystemMetrics value */
368 *value = get_theme_sys_metric (theme, arg);
371 *value = GetSystemMetrics (arg);
385 _gtk_win32_theme_color_resolve (const char *theme_class,
392 if (use_xp_theme && get_theme_sys_color != NULL)
394 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
396 /* if theme is NULL, it will just return the GetSystemColor()
398 dcolor = get_theme_sys_color (theme, id);
401 dcolor = GetSysColor (id);
404 color->red = GetRValue (dcolor) / 255.0;
405 color->green = GetGValue (dcolor) / 255.0;
406 color->blue = GetBValue (dcolor) / 255.0;
408 gdk_rgba_parse (color, "pink");
414 _gtk_win32_theme_get_default (void)
417 _gtk_win32_theme_init ();
420 return (os_version.dwMajorVersion >= 6) ? "gtk-win32" : "gtk-win32-xp";
422 return "gtk-win32-classic";