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"
26 #include <gtksymboliccolor.h>
30 #include <cairo-win32.h>
32 #define UXTHEME_DLL "uxtheme.dll"
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;
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,
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,
56 typedef BOOL (FAR PASCAL *IsThemeActiveFunc) (VOID);
57 typedef BOOL (FAR PASCAL *IsAppThemedFunc) (VOID);
58 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
61 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
64 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc) (HTHEME hTheme,
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;
85 static GHashTable *hthemes_by_class = NULL;
88 _gtk_win32_theme_init (void)
97 n = GetSystemDirectory (&dummy, 0);
101 buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
102 k = GetSystemDirectory (buf, n);
109 if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
110 strcat (buf, G_DIR_SEPARATOR_S);
111 strcat (buf, UXTHEME_DLL);
113 uxtheme_dll = LoadLibrary (buf);
119 is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
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");
135 if (is_app_themed && is_theme_active)
137 use_xp_theme = (is_app_themed () && is_theme_active ());
141 use_xp_theme = FALSE;
144 hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
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)
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");
162 _gtk_win32_lookup_htheme_by_classname (const char *class)
168 _gtk_win32_theme_init ();
170 lower = g_ascii_strdown (class, -1);
172 theme = (HTHEME) g_hash_table_lookup (hthemes_by_class, lower);
179 wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
180 theme = open_theme_data (NULL, wclass);
189 /* Takes ownership of lower: */
190 g_hash_table_insert (hthemes_by_class, lower, theme);
198 _gtk_win32_lookup_htheme_by_classname (const char *class)
203 #endif /* G_OS_WIN32 */
206 _gtk_win32_theme_part_create_surface (HTHEME theme,
215 cairo_surface_t *surface;
231 width -= margins[3] + margins[1];
232 height -= margins[0] + margins[2];
238 rect.bottom = height;
241 res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
242 ReleaseDC (NULL, hdc);
246 x_offs += (width - size.cx) / 2;
247 y_offs += (height - size.cy) / 2;
253 rect.bottom = height;
256 has_alpha = is_theme_partially_transparent (theme, xp_part, state);
258 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
260 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
262 hdc = cairo_win32_surface_get_dc (surface);
264 res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
266 /* XP Can't handle rendering some parts on an alpha target */
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))
276 cairo_surface_t *img = cairo_win32_surface_get_image (surface);
277 guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
281 for (i = 0; i < width; i++)
283 for (j = 0; j < height; j++)
285 if (data[i+j*width] != 0)
286 data[i+j*width] |= 0xff000000;
291 *x_offs_out = x_offs;
292 *y_offs_out = y_offs;
297 #else /* !G_OS_WIN32 */
298 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
299 #endif /* G_OS_WIN32 */
301 cr = cairo_create (surface);
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);
310 *x_offs_out = x_offs;
311 *y_offs_out = y_offs;
317 _gtk_win32_theme_int_parse (GtkCssParser *parser,
323 if (_gtk_css_parser_try (parser,
327 if (!_gtk_css_parser_try (parser, "(", TRUE))
329 _gtk_css_parser_error (parser,
330 "Expected '(' after '-gtk-win32-size'");
334 class = _gtk_css_parser_try_name (parser, TRUE);
337 _gtk_css_parser_error (parser,
338 "Expected name as first argument to '-gtk-win32-size'");
342 if (! _gtk_css_parser_try (parser, ",", TRUE))
345 _gtk_css_parser_error (parser,
350 if (!_gtk_css_parser_try_int (parser, &arg))
353 _gtk_css_parser_error (parser, "Expected a valid integer value");
357 if (!_gtk_css_parser_try (parser, ")", TRUE))
359 _gtk_css_parser_error (parser,
365 if (use_xp_theme && get_theme_sys_metric != NULL)
367 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
369 /* If theme is NULL it will just return the GetSystemMetrics value */
370 *value = get_theme_sys_metric (theme, arg);
373 *value = GetSystemMetrics (arg);
387 _gtk_win32_theme_color_parse (GtkCssParser *parser)
389 GtkSymbolicColor *color;
393 class = _gtk_css_parser_try_name (parser, TRUE);
396 _gtk_css_parser_error (parser,
397 "Expected name as first argument to '-gtk-win32-color'");
401 if (! _gtk_css_parser_try (parser, ",", TRUE))
404 _gtk_css_parser_error (parser,
409 if (!_gtk_css_parser_try_int (parser, &id))
412 _gtk_css_parser_error (parser, "Expected a valid integer value");
416 color = gtk_symbolic_color_new_win32 (class, id);
422 _gtk_win32_theme_color_resolve (const char *theme_class,
429 if (use_xp_theme && get_theme_sys_color != NULL)
431 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
433 /* if theme is NULL, it will just return the GetSystemColor()
435 dcolor = get_theme_sys_color (theme, id);
438 dcolor = GetSysColor (id);
441 color->red = GetRValue (dcolor) / 255.0;
442 color->green = GetGValue (dcolor) / 255.0;
443 color->blue = GetBValue (dcolor) / 255.0;
445 gdk_rgba_parse (color, "pink");
451 _gtk_win32_theme_get_default (void)
454 _gtk_win32_theme_init ();
457 return (os_version.dwMajorVersion >= 6) ? "gtk-win32" : "gtk-win32-xp";
459 return "gtk-win32-classic";