]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32theme.c
fb5bb4790383f97d101989d3f206d8392b9251c0
[~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, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <config.h>
25
26 #include "gtkwin32themeprivate.h"
27
28 #ifdef G_OS_WIN32
29
30 #include <windows.h>
31 #include <cairo-win32.h>
32
33 typedef HANDLE HTHEME;
34
35 #define UXTHEME_DLL "uxtheme.dll"
36
37 static HINSTANCE uxtheme_dll = NULL;
38 static gboolean use_xp_theme = FALSE;
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   char *buf;
86   char dummy;
87   int n, k;
88
89   if (uxtheme_dll)
90     return;
91
92   n = GetSystemDirectory (&dummy, 0);
93   if (n <= 0)
94     return;
95
96   buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
97   k = GetSystemDirectory (buf, n);
98   if (k == 0 || k > n)
99     {
100       g_free (buf);
101       return;
102     }
103
104   if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
105     strcat (buf, G_DIR_SEPARATOR_S);
106   strcat (buf, UXTHEME_DLL);
107
108   uxtheme_dll = LoadLibrary (buf);
109   g_free (buf);
110
111   if (!uxtheme_dll)
112     return;
113
114   is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
115   if (is_app_themed)
116     {
117       is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
118       open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
119       close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
120       draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
121       enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
122       get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
123       get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
124       get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
125       is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
126       draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
127       get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
128     }
129
130   if (is_app_themed && is_theme_active)
131     {
132       use_xp_theme = (is_app_themed () && is_theme_active ());
133     }
134   else
135     {
136       use_xp_theme = FALSE;
137     }
138
139   hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
140 }
141
142 static HTHEME
143 lookup_htheme_by_classname (const char *class)
144 {
145   HTHEME theme;
146   guint16 *wclass;
147   char *lower;
148   
149   lower = g_ascii_strdown (class, -1);
150
151   theme = (HTHEME)  g_hash_table_lookup (hthemes_by_class, lower);
152   if (theme)
153     {
154       g_free (lower);
155       return theme;
156     }
157
158   wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
159   theme  = open_theme_data (NULL, wclass);
160   g_free (wclass);
161
162   if (theme == NULL)
163     {
164       g_free (lower);
165       return NULL;
166     }
167
168   /* Takes ownership of lower: */
169   g_hash_table_insert (hthemes_by_class, lower, theme);
170
171   return theme;
172 }
173
174 #else
175
176 typedef void * HTHEME;
177
178 static void
179 _gtk_win32_theme_init (void)
180 {
181 }
182
183 static HTHEME
184 lookup_htheme_by_classname (const char *class)
185 {
186   return NULL;
187 }
188
189 #endif /* G_OS_WIN32 */
190
191 G_DEFINE_BOXED_TYPE_WITH_CODE (GtkWin32ThemePart, _gtk_win32_theme_part,
192                                _gtk_win32_theme_part_ref, _gtk_win32_theme_part_unref, 
193                                _gtk_win32_theme_init() )
194
195 struct _GtkWin32ThemePart {
196   HTHEME theme;
197   int part;
198   int state;
199   int part2;
200   int state2;
201
202   gint ref_count;
203 };
204
205 GtkWin32ThemePart *
206 _gtk_win32_theme_part_new (const char *class, 
207                            int xp_part, int state, 
208                            int xp_part2, int state2)
209 {
210   GtkWin32ThemePart *part;
211
212   part = g_slice_new0 (GtkWin32ThemePart);
213   part->ref_count = 1;
214
215   part->theme = lookup_htheme_by_classname (class);
216   part->part = xp_part;
217   part->state = state;
218   part->part2 = xp_part2;
219   part->state2 = state2;
220
221   return part;
222 }
223
224 GtkWin32ThemePart *
225 _gtk_win32_theme_part_ref (GtkWin32ThemePart *part)
226 {
227   g_return_val_if_fail (part != NULL, NULL);
228
229   part->ref_count++;
230
231   return part;
232 }
233
234 void
235 _gtk_win32_theme_part_unref (GtkWin32ThemePart *part)
236 {
237   g_return_if_fail (part != NULL);
238
239   part->ref_count--;
240
241   if (part->ref_count == 0)
242     {
243       g_slice_free (GtkWin32ThemePart, part);
244     }
245 }
246
247 int
248 _gtk_win32_theme_part_parse (GtkCssParser *parser, 
249                              GFile *base, 
250                              GValue *value)
251 {
252   char *class;
253   int xp_part, state, xp_part2, state2;
254   GtkWin32ThemePart *theme_part;
255
256   if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
257     {
258       return -1;
259     }
260   
261   g_value_unset (value);
262   g_value_init (value, GTK_TYPE_WIN32_THEME_PART);
263
264   if (!_gtk_css_parser_try (parser, "(", TRUE))
265     {
266       _gtk_css_parser_error (parser,
267                              "Expected '(' after '-gtk-win32-theme-part'");
268       return 0;
269     }
270   
271   class = _gtk_css_parser_try_name (parser, TRUE);
272   if (class == NULL)
273     {
274       _gtk_css_parser_error (parser,
275                              "Expected name as first argument to  '-gtk-win32-theme-part'");
276       return 0;
277     }
278
279   if (! _gtk_css_parser_try (parser, ",", TRUE))
280     {
281       g_free (class);
282       _gtk_css_parser_error (parser,
283                              "Expected ','");
284       return 0;
285     }
286
287   if (!_gtk_css_parser_try_int (parser, &xp_part))
288     {
289       g_free (class);
290       _gtk_css_parser_error (parser, "Expected a valid integer value");
291       return 0;
292     }
293
294   if (!_gtk_css_parser_try_int (parser, &state))
295     {
296       g_free (class);
297       _gtk_css_parser_error (parser, "Expected a valid integer value");
298       return 0;
299     }
300
301   xp_part2 = -1;
302   state2 = -1;
303   if ( _gtk_css_parser_try (parser, ",", TRUE))
304     {
305       if (!_gtk_css_parser_try_int (parser, &xp_part2))
306         {
307           g_free (class);
308           _gtk_css_parser_error (parser, "Expected a valid integer value");
309           return 0;
310         }
311
312       if (!_gtk_css_parser_try_int (parser, &state2))
313         {
314           g_free (class);
315           _gtk_css_parser_error (parser, "Expected a valid integer value");
316           return 0;
317         }
318     }
319
320
321   if (!_gtk_css_parser_try (parser, ")", TRUE))
322     {
323       g_free (class);
324       _gtk_css_parser_error (parser,
325                              "Expected ')'");
326       return 0;
327     }
328   
329   theme_part = _gtk_win32_theme_part_new (class, 
330                                           xp_part, state, 
331                                           xp_part2, state2);
332   g_free (class);
333   
334   g_value_take_boxed (value, theme_part);
335   return 1;
336 }
337
338 #ifdef G_OS_WIN32
339 cairo_surface_t *
340 _gtk_win32_theme_part_create_surface  (GtkWin32ThemePart  *part,
341                                        int                 xp_part,
342                                        int                 state,
343                                        int                 width,
344                                        int                 height)
345 {
346   cairo_surface_t *surface;
347   HDC hdc;
348   RECT rect;
349   HRESULT res;
350
351   surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
352   hdc = cairo_win32_surface_get_dc (surface);
353   
354   rect.left = 0;
355   rect.top = 0;
356   rect.right = width;
357   rect.bottom = height;
358
359   res = draw_theme_background (part->theme, hdc, xp_part, state, &rect, &rect);
360   return surface;
361 }
362 #endif
363
364
365 cairo_pattern_t *
366 _gtk_win32_theme_part_render  (GtkWin32ThemePart  *part,
367                                int                 width,
368                                int                 height)
369 {
370 #ifdef G_OS_WIN32
371   cairo_surface_t *surface, *surface2, *image;
372   cairo_pattern_t *pattern;
373   cairo_t *cr;
374   cairo_matrix_t matrix;
375   cairo_user_data_key_t key;
376
377   surface = _gtk_win32_theme_part_create_surface  (part, part->part, part->state, 
378                                                    width, height);
379   
380   if (part->state2 >= 0)
381     {
382       surface2 = _gtk_win32_theme_part_create_surface  (part, part->part2, part->state2, 
383                                                         width, height);
384
385       cr = cairo_create (surface);
386
387       pattern = cairo_pattern_create_for_surface (surface2);
388       cairo_set_source (cr, pattern);
389       cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
390       cairo_paint (cr);
391       
392       cairo_destroy (cr);
393       cairo_pattern_destroy (pattern);
394       cairo_surface_destroy (surface2);
395     }
396
397   /* We need to return an image surface, as that is what the code expects in order
398      to get the size */
399   image = cairo_win32_surface_get_image (surface);
400   pattern = cairo_pattern_create_for_surface (cairo_surface_reference (image));
401
402   cairo_matrix_init_scale (&matrix,
403                            width,
404                            height);
405   cairo_pattern_set_matrix (pattern, &matrix);
406
407   /* We can't immediately destroy the surface, because that would free the data
408      the image surface refers too. Instead we destroy it with the pattern. */
409   cairo_pattern_set_user_data (pattern,
410                                &key,
411                                surface, (cairo_destroy_func_t) cairo_surface_destroy);
412
413   return pattern;
414 #else
415   GdkRGBA color;
416   
417   gdk_rgba_parse (&color, "pink");
418
419   return cairo_pattern_create_rgb (color.red, color.green, color.blue);
420 #endif
421 }
422
423 int
424 _gtk_win32_theme_int_parse (GtkCssParser      *parser,
425                             GFile             *base,
426                             int               *value)
427 {
428   char *class;
429   int arg;
430
431   if (_gtk_css_parser_try (parser,
432                            "-gtk-win32-size",
433                            TRUE))
434     {
435       if (!_gtk_css_parser_try (parser, "(", TRUE))
436         {
437           _gtk_css_parser_error (parser,
438                                  "Expected '(' after '-gtk-win32-size'");
439           return 0;
440         }
441
442       class = _gtk_css_parser_try_name (parser, TRUE);
443       if (class == NULL)
444         {
445           _gtk_css_parser_error (parser,
446                                  "Expected name as first argument to  '-gtk-win32-size'");
447           return 0;
448         }
449
450       if (! _gtk_css_parser_try (parser, ",", TRUE))
451         {
452           g_free (class);
453           _gtk_css_parser_error (parser,
454                                  "Expected ','");
455           return 0;
456         }
457
458       if (!_gtk_css_parser_try_int (parser, &arg))
459         {
460           g_free (class);
461           _gtk_css_parser_error (parser, "Expected a valid integer value");
462           return 0;
463         }
464
465       if (!_gtk_css_parser_try (parser, ")", TRUE))
466         {
467           _gtk_css_parser_error (parser,
468                                  "Expected ')'");
469           return 0;
470         }
471
472 #ifdef G_OS_WIN32
473       if (use_xp_theme && get_theme_sys_metric != NULL)
474         {
475           HTHEME theme = lookup_htheme_by_classname (class);
476
477           /* If theme is NULL it will just return the GetSystemMetrics value */
478           *value = get_theme_sys_metric (theme, arg);
479         }
480       else
481         *value = GetSystemMetrics (arg);
482 #else
483       *value = 1;
484 #endif
485
486       g_free (class);
487
488       return 1;
489     }
490
491   return -1;
492 }
493
494 GtkSymbolicColor *
495 _gtk_win32_theme_color_parse (GtkCssParser *parser)
496 {
497   GtkSymbolicColor *color;
498   char *class;
499   int id;
500
501   class = _gtk_css_parser_try_name (parser, TRUE);
502   if (class == NULL)
503     {
504       _gtk_css_parser_error (parser,
505                              "Expected name as first argument to  '-gtk-win32-color'");
506       return NULL;
507     }
508
509   if (! _gtk_css_parser_try (parser, ",", TRUE))
510     {
511       g_free (class);
512       _gtk_css_parser_error (parser,
513                              "Expected ','");
514       return NULL;
515     }
516
517   if (!_gtk_css_parser_try_int (parser, &id))
518     {
519       g_free (class);
520       _gtk_css_parser_error (parser, "Expected a valid integer value");
521       return NULL;
522     }
523
524   color = gtk_symbolic_color_new_win32 (class, id);
525   g_free (class);
526   return color;
527 }
528
529 gboolean
530 _gtk_win32_theme_color_resolve (const char *theme_class,
531                                 gint id,
532                                 GdkRGBA *color)
533 {
534 #ifdef G_OS_WIN32
535   DWORD dcolor;
536
537   if (use_xp_theme && get_theme_sys_color != NULL)
538     {
539       HTHEME theme = lookup_htheme_by_classname (theme_class);
540
541       /* if theme is NULL, it will just return the GetSystemColor()
542          value */
543       dcolor = get_theme_sys_color (theme, id);
544     }
545   else
546     dcolor = GetSysColor (id);
547
548   color->alpha = 1.0;
549   color->red = GetRValue (dcolor) / 255.0;
550   color->green = GetGValue (dcolor) / 255.0;
551   color->blue = GetBValue (dcolor) / 255.0;
552 #else
553   gdk_rgba_parse (color, "pink");
554 #endif
555   return TRUE;
556 }