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_fixup1 = NULL;
35 static HTHEME needs_alpha_fixup2 = NULL;
36 static HTHEME needs_alpha_fixup3 = NULL;
37 static HTHEME needs_alpha_fixup4 = NULL;
38 static HTHEME needs_alpha_fixup5 = NULL;
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)
85 OSVERSIONINFO version;
93 n = GetSystemDirectory (&dummy, 0);
97 buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
98 k = GetSystemDirectory (buf, n);
105 if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
106 strcat (buf, G_DIR_SEPARATOR_S);
107 strcat (buf, UXTHEME_DLL);
109 uxtheme_dll = LoadLibrary (buf);
115 is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
118 is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
119 open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
120 close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
121 draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
122 enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
123 get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
124 get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
125 get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
126 is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
127 draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
128 get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
131 if (is_app_themed && is_theme_active)
133 use_xp_theme = (is_app_themed () && is_theme_active ());
137 use_xp_theme = FALSE;
140 hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
142 memset (&version, 0, sizeof (version));
143 version.dwOSVersionInfoSize = sizeof (version);
144 if (GetVersionEx (&version) && version.dwMajorVersion == 5)
146 needs_alpha_fixup1 = _gtk_win32_lookup_htheme_by_classname ("scrollbar");
147 needs_alpha_fixup2 = _gtk_win32_lookup_htheme_by_classname ("toolbar");
148 needs_alpha_fixup3 = _gtk_win32_lookup_htheme_by_classname ("button");
149 needs_alpha_fixup4 = _gtk_win32_lookup_htheme_by_classname ("header");
150 needs_alpha_fixup5 = _gtk_win32_lookup_htheme_by_classname ("trackbar");
155 _gtk_win32_lookup_htheme_by_classname (const char *class)
161 _gtk_win32_theme_init ();
163 lower = g_ascii_strdown (class, -1);
165 theme = (HTHEME) g_hash_table_lookup (hthemes_by_class, lower);
172 wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
173 theme = open_theme_data (NULL, wclass);
182 /* Takes ownership of lower: */
183 g_hash_table_insert (hthemes_by_class, lower, theme);
191 _gtk_win32_lookup_htheme_by_classname (const char *class)
196 #endif /* G_OS_WIN32 */
199 _gtk_win32_theme_part_create_surface (HTHEME theme,
208 cairo_surface_t *surface;
224 width -= margins[3] + margins[1];
225 height -= margins[0] + margins[2];
231 rect.bottom = height;
234 res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
235 ReleaseDC (NULL, hdc);
239 x_offs += (width - size.cx) / 2;
240 y_offs += (height - size.cy) / 2;
246 rect.bottom = height;
249 has_alpha = is_theme_partially_transparent (theme, xp_part, state);
251 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
253 surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
255 hdc = cairo_win32_surface_get_dc (surface);
257 res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
259 /* XP Can't handle rendering some parts on an alpha target */
261 (theme == needs_alpha_fixup1 ||
262 theme == needs_alpha_fixup2 ||
263 (theme == needs_alpha_fixup3 && xp_part == 4) ||
264 theme == needs_alpha_fixup4 ||
265 theme == needs_alpha_fixup5))
267 cairo_surface_t *img = cairo_win32_surface_get_image (surface);
268 guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
271 for (int i = 0; i < width; i++)
273 for (int j = 0; j < height; j++)
275 if (data[i+j*width] != 0)
276 data[i+j*width] |= 0xff000000;
281 *x_offs_out = x_offs;
282 *y_offs_out = y_offs;
287 #else /* !G_OS_WIN32 */
288 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
289 #endif /* G_OS_WIN32 */
291 cr = cairo_create (surface);
293 /* XXX: Do something better here (like printing the theme parts) */
294 gdk_rgba_parse (&color, "pink");
295 gdk_cairo_set_source_rgba (cr, &color);
300 *x_offs_out = x_offs;
301 *y_offs_out = y_offs;
307 _gtk_win32_theme_int_parse (GtkCssParser *parser,
314 if (_gtk_css_parser_try (parser,
318 if (!_gtk_css_parser_try (parser, "(", TRUE))
320 _gtk_css_parser_error (parser,
321 "Expected '(' after '-gtk-win32-size'");
325 class = _gtk_css_parser_try_name (parser, TRUE);
328 _gtk_css_parser_error (parser,
329 "Expected name as first argument to '-gtk-win32-size'");
333 if (! _gtk_css_parser_try (parser, ",", TRUE))
336 _gtk_css_parser_error (parser,
341 if (!_gtk_css_parser_try_int (parser, &arg))
344 _gtk_css_parser_error (parser, "Expected a valid integer value");
348 if (!_gtk_css_parser_try (parser, ")", TRUE))
350 _gtk_css_parser_error (parser,
356 if (use_xp_theme && get_theme_sys_metric != NULL)
358 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
360 /* If theme is NULL it will just return the GetSystemMetrics value */
361 *value = get_theme_sys_metric (theme, arg);
364 *value = GetSystemMetrics (arg);
378 _gtk_win32_theme_color_parse (GtkCssParser *parser)
380 GtkSymbolicColor *color;
384 class = _gtk_css_parser_try_name (parser, TRUE);
387 _gtk_css_parser_error (parser,
388 "Expected name as first argument to '-gtk-win32-color'");
392 if (! _gtk_css_parser_try (parser, ",", TRUE))
395 _gtk_css_parser_error (parser,
400 if (!_gtk_css_parser_try_int (parser, &id))
403 _gtk_css_parser_error (parser, "Expected a valid integer value");
407 color = gtk_symbolic_color_new_win32 (class, id);
413 _gtk_win32_theme_color_resolve (const char *theme_class,
420 if (use_xp_theme && get_theme_sys_color != NULL)
422 HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
424 /* if theme is NULL, it will just return the GetSystemColor()
426 dcolor = get_theme_sys_color (theme, id);
429 dcolor = GetSysColor (id);
432 color->red = GetRValue (dcolor) / 255.0;
433 color->green = GetGValue (dcolor) / 255.0;
434 color->blue = GetBValue (dcolor) / 255.0;
436 gdk_rgba_parse (color, "pink");