]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32theme.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkwin32theme.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
3  * Copyright (C) 2011 Red Hat, Inc.
4  *
5  * Authors: Carlos Garnacho <carlosg@gnome.org>
6  *          Cosimo Cecchi <cosimoc@gnome.org>
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include <config.h>
23
24 #include "gtkwin32themeprivate.h"
25
26 #ifdef G_OS_WIN32
27
28 #include <cairo-win32.h>
29
30 #define UXTHEME_DLL "uxtheme.dll"
31
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;
42
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,
46                                                              int iColorID);
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,
53                                                              DWORD dwFlags);
54 typedef BOOL (FAR PASCAL *IsThemeActiveFunc)                (VOID);
55 typedef BOOL (FAR PASCAL *IsAppThemedFunc)                  (VOID);
56 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
57                                                                       int iPartId,
58                                                                       int iStateId);
59 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
60                                                              HDC hdc,
61                                                              RECT *prc);
62 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc)          (HTHEME hTheme,
63                                                              HDC hdc,
64                                                              int iPartId,
65                                                              int iStateId,
66                                                              RECT *prc,
67                                                              int eSize,
68                                                              SIZE *psz);
69
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;
82
83 static GHashTable *hthemes_by_class = NULL;
84
85 static void
86 _gtk_win32_theme_init (void)
87 {
88   char *buf;
89   char dummy;
90   int n, k;
91
92   if (uxtheme_dll)
93     return;
94
95   n = GetSystemDirectory (&dummy, 0);
96   if (n <= 0)
97     return;
98
99   buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
100   k = GetSystemDirectory (buf, n);
101   if (k == 0 || k > n)
102     {
103       g_free (buf);
104       return;
105     }
106
107   if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
108     strcat (buf, G_DIR_SEPARATOR_S);
109   strcat (buf, UXTHEME_DLL);
110
111   uxtheme_dll = LoadLibrary (buf);
112   g_free (buf);
113
114   if (!uxtheme_dll)
115     return;
116
117   is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
118   if (is_app_themed)
119     {
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");
131     }
132
133   if (is_app_themed && is_theme_active)
134     {
135       use_xp_theme = (is_app_themed () && is_theme_active ());
136     }
137   else
138     {
139       use_xp_theme = FALSE;
140     }
141
142   hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
143
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)
148     {
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");
156     }
157 }
158
159 HTHEME
160 _gtk_win32_lookup_htheme_by_classname (const char *class)
161 {
162   HTHEME theme;
163   guint16 *wclass;
164   char *lower;
165   
166   _gtk_win32_theme_init ();
167
168   lower = g_ascii_strdown (class, -1);
169
170   theme = (HTHEME)  g_hash_table_lookup (hthemes_by_class, lower);
171   if (theme)
172     {
173       g_free (lower);
174       return theme;
175     }
176
177   wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
178   theme  = open_theme_data (NULL, wclass);
179   g_free (wclass);
180
181   if (theme == NULL)
182     {
183       g_free (lower);
184       return NULL;
185     }
186
187   /* Takes ownership of lower: */
188   g_hash_table_insert (hthemes_by_class, lower, theme);
189
190   return theme;
191 }
192
193 #else
194
195 HTHEME
196 _gtk_win32_lookup_htheme_by_classname (const char *class)
197 {
198   return NULL;
199 }
200
201 #endif /* G_OS_WIN32 */
202
203 cairo_surface_t *
204 _gtk_win32_theme_part_create_surface (HTHEME theme,
205                                       int    xp_part,
206                                       int    state,
207                                       int    margins[4],
208                                       int    width,
209                                       int    height,
210                                       int   *x_offs_out,
211                                       int   *y_offs_out)
212 {
213   cairo_surface_t *surface;
214   GdkRGBA color;
215   cairo_t *cr;
216   int x_offs;
217   int y_offs;
218 #ifdef G_OS_WIN32
219   gboolean has_alpha;
220   HDC hdc;
221   RECT rect;
222   SIZE size;
223   HRESULT res;
224 #endif
225
226   x_offs = margins[3];
227   y_offs = margins[0];
228
229   width -= margins[3] + margins[1];
230   height -= margins[0] + margins[2];
231
232 #ifdef G_OS_WIN32
233   rect.left = 0;
234   rect.top = 0;
235   rect.right = width;
236   rect.bottom = height;
237
238   hdc = GetDC (NULL);
239   res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
240   ReleaseDC (NULL, hdc);
241
242   if (res == S_OK)
243     {
244       x_offs += (width - size.cx) / 2;
245       y_offs += (height - size.cy) / 2;
246   
247       width = size.cx;
248       height = size.cy;
249
250       rect.right = width;
251       rect.bottom = height;
252     }
253
254   has_alpha = is_theme_partially_transparent (theme, xp_part, state);
255   if (has_alpha)
256     surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
257   else
258     surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
259
260   hdc = cairo_win32_surface_get_dc (surface);
261
262   res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
263
264   /* XP Can't handle rendering some parts on an alpha target */
265   if (has_alpha && 
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))
273     {
274       cairo_surface_t *img = cairo_win32_surface_get_image (surface);
275       guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
276       int i, j;
277       GdiFlush ();
278
279       for (i = 0; i < width; i++)
280         {
281           for (j = 0; j < height; j++)
282             {
283               if (data[i+j*width] != 0)
284                 data[i+j*width] |= 0xff000000;
285             }
286         }
287     }
288
289   *x_offs_out = x_offs;
290   *y_offs_out = y_offs;
291
292   if (res == S_OK)
293     return surface;
294
295 #else /* !G_OS_WIN32 */
296   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
297 #endif /* G_OS_WIN32 */
298
299   cr = cairo_create (surface);
300   
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);
304   cairo_paint (cr);
305
306   cairo_destroy (cr);
307   
308   *x_offs_out = x_offs;
309   *y_offs_out = y_offs;
310
311   return surface;
312 }
313
314 int
315 _gtk_win32_theme_int_parse (GtkCssParser      *parser,
316                             int               *value)
317 {
318   char *class;
319   int arg;
320
321   if (_gtk_css_parser_try (parser,
322                            "-gtk-win32-size",
323                            TRUE))
324     {
325       if (!_gtk_css_parser_try (parser, "(", TRUE))
326         {
327           _gtk_css_parser_error (parser,
328                                  "Expected '(' after '-gtk-win32-size'");
329           return 0;
330         }
331
332       class = _gtk_css_parser_try_name (parser, TRUE);
333       if (class == NULL)
334         {
335           _gtk_css_parser_error (parser,
336                                  "Expected name as first argument to  '-gtk-win32-size'");
337           return 0;
338         }
339
340       if (! _gtk_css_parser_try (parser, ",", TRUE))
341         {
342           g_free (class);
343           _gtk_css_parser_error (parser,
344                                  "Expected ','");
345           return 0;
346         }
347
348       if (!_gtk_css_parser_try_int (parser, &arg))
349         {
350           g_free (class);
351           _gtk_css_parser_error (parser, "Expected a valid integer value");
352           return 0;
353         }
354
355       if (!_gtk_css_parser_try (parser, ")", TRUE))
356         {
357           _gtk_css_parser_error (parser,
358                                  "Expected ')'");
359           return 0;
360         }
361
362 #ifdef G_OS_WIN32
363       if (use_xp_theme && get_theme_sys_metric != NULL)
364         {
365           HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
366
367           /* If theme is NULL it will just return the GetSystemMetrics value */
368           *value = get_theme_sys_metric (theme, arg);
369         }
370       else
371         *value = GetSystemMetrics (arg);
372 #else
373       *value = 1;
374 #endif
375
376       g_free (class);
377
378       return 1;
379     }
380
381   return -1;
382 }
383
384 gboolean
385 _gtk_win32_theme_color_resolve (const char *theme_class,
386                                 gint id,
387                                 GdkRGBA *color)
388 {
389 #ifdef G_OS_WIN32
390   DWORD dcolor;
391
392   if (use_xp_theme && get_theme_sys_color != NULL)
393     {
394       HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
395
396       /* if theme is NULL, it will just return the GetSystemColor()
397          value */
398       dcolor = get_theme_sys_color (theme, id);
399     }
400   else
401     dcolor = GetSysColor (id);
402
403   color->alpha = 1.0;
404   color->red = GetRValue (dcolor) / 255.0;
405   color->green = GetGValue (dcolor) / 255.0;
406   color->blue = GetBValue (dcolor) / 255.0;
407 #else
408   gdk_rgba_parse (color, "pink");
409 #endif
410   return TRUE;
411 }
412
413 const char *
414 _gtk_win32_theme_get_default (void)
415 {
416 #ifdef G_OS_WIN32
417   _gtk_win32_theme_init ();
418   
419   if (use_xp_theme)
420     return (os_version.dwMajorVersion >= 6) ? "gtk-win32" : "gtk-win32-xp";
421 #endif
422   return "gtk-win32-classic";
423 }
424