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