]> Pileus Git - ~andy/gtk/blob - gtk/gtkwin32theme.c
f76c1983b308f9dfef6f8ab4a99c6e13ef95b591
[~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
200   double over_alpha;
201   int part2;
202   int state2;
203
204   gint ref_count;
205 };
206
207 GtkWin32ThemePart *
208 _gtk_win32_theme_part_new (const char *class, 
209                            int xp_part, int state, 
210                            int xp_part2, int state2, 
211                            double over_alpha)
212 {
213   GtkWin32ThemePart *part;
214
215   part = g_slice_new0 (GtkWin32ThemePart);
216   part->ref_count = 1;
217
218   part->theme = lookup_htheme_by_classname (class);
219   part->part = xp_part;
220   part->state = state;
221   part->part2 = xp_part2;
222   part->state2 = state2;
223   part->over_alpha = over_alpha;
224
225   return part;
226 }
227
228 GtkWin32ThemePart *
229 _gtk_win32_theme_part_ref (GtkWin32ThemePart *part)
230 {
231   g_return_val_if_fail (part != NULL, NULL);
232
233   part->ref_count++;
234
235   return part;
236 }
237
238 void
239 _gtk_win32_theme_part_unref (GtkWin32ThemePart *part)
240 {
241   g_return_if_fail (part != NULL);
242
243   part->ref_count--;
244
245   if (part->ref_count == 0)
246     {
247       g_slice_free (GtkWin32ThemePart, part);
248     }
249 }
250
251 int
252 _gtk_win32_theme_part_parse (GtkCssParser *parser, 
253                              GFile *base, 
254                              GValue *value)
255 {
256   char *class;
257   int xp_part, state, xp_part2, state2;
258   double over_alpha;
259   GtkWin32ThemePart *theme_part;
260
261   if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
262     {
263       return -1;
264     }
265   
266   g_value_unset (value);
267   g_value_init (value, GTK_TYPE_WIN32_THEME_PART);
268
269   if (!_gtk_css_parser_try (parser, "(", TRUE))
270     {
271       _gtk_css_parser_error (parser,
272                              "Expected '(' after '-gtk-win32-theme-part'");
273       return 0;
274     }
275   
276   class = _gtk_css_parser_try_name (parser, TRUE);
277   if (class == NULL)
278     {
279       _gtk_css_parser_error (parser,
280                              "Expected name as first argument to  '-gtk-win32-theme-part'");
281       return 0;
282     }
283
284   if (! _gtk_css_parser_try (parser, ",", TRUE))
285     {
286       g_free (class);
287       _gtk_css_parser_error (parser,
288                              "Expected ','");
289       return 0;
290     }
291
292   if (!_gtk_css_parser_try_int (parser, &xp_part))
293     {
294       g_free (class);
295       _gtk_css_parser_error (parser, "Expected a valid integer value");
296       return 0;
297     }
298
299   if (!_gtk_css_parser_try_int (parser, &state))
300     {
301       g_free (class);
302       _gtk_css_parser_error (parser, "Expected a valid integer value");
303       return 0;
304     }
305
306
307   over_alpha = 1.0;
308   xp_part2 = -1;
309   state2 = -1;
310
311   if ( _gtk_css_parser_try (parser, ",", TRUE))
312     {
313
314       if ( _gtk_css_parser_try (parser, "over", TRUE))
315         {
316           if (!_gtk_css_parser_try (parser, "(", TRUE))
317             {
318               _gtk_css_parser_error (parser,
319                                      "Expected '(' after 'over'");
320               return 0;
321             }
322
323           if (!_gtk_css_parser_try_int (parser, &xp_part2))
324             {
325               g_free (class);
326               _gtk_css_parser_error (parser, "Expected a valid integer value");
327               return 0;
328             }
329
330           if (!_gtk_css_parser_try_int (parser, &state2))
331             {
332               g_free (class);
333               _gtk_css_parser_error (parser, "Expected a valid integer value");
334               return 0;
335             }
336
337           if ( _gtk_css_parser_try (parser, ",", TRUE))
338             {
339               if (!_gtk_css_parser_try_double (parser, &over_alpha))
340                 {
341                   g_free (class);
342                   _gtk_css_parser_error (parser, "Expected a valid double value");
343                   return 0;
344                 }
345             }
346
347           if (!_gtk_css_parser_try (parser, ")", TRUE))
348             {
349               _gtk_css_parser_error (parser,
350                                      "Expected ')' at end of 'over'");
351               return 0;
352             }
353         }
354     }
355
356
357   if (!_gtk_css_parser_try (parser, ")", TRUE))
358     {
359       g_free (class);
360       _gtk_css_parser_error (parser,
361                              "Expected ')'");
362       return 0;
363     }
364   
365   theme_part = _gtk_win32_theme_part_new (class, 
366                                           xp_part, state, 
367                                           xp_part2, state2,
368                                           over_alpha);
369   g_free (class);
370   
371   g_value_take_boxed (value, theme_part);
372   return 1;
373 }
374
375 #ifdef G_OS_WIN32
376 cairo_surface_t *
377 _gtk_win32_theme_part_create_surface  (GtkWin32ThemePart  *part,
378                                        int                 xp_part,
379                                        int                 state,
380                                        int                 width,
381                                        int                 height)
382 {
383   cairo_surface_t *surface;
384   HDC hdc;
385   RECT rect;
386   HRESULT res;
387
388   surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
389   hdc = cairo_win32_surface_get_dc (surface);
390   
391   rect.left = 0;
392   rect.top = 0;
393   rect.right = width;
394   rect.bottom = height;
395
396   res = draw_theme_background (part->theme, hdc, xp_part, state, &rect, &rect);
397   return surface;
398 }
399 #endif
400
401
402 cairo_pattern_t *
403 _gtk_win32_theme_part_render  (GtkWin32ThemePart  *part,
404                                int                 width,
405                                int                 height)
406 {
407 #ifdef G_OS_WIN32
408   cairo_surface_t *surface, *surface2, *image;
409   cairo_pattern_t *pattern;
410   cairo_t *cr;
411   cairo_matrix_t matrix;
412   cairo_user_data_key_t key;
413
414   surface = _gtk_win32_theme_part_create_surface  (part, part->part, part->state, 
415                                                    width, height);
416   
417   if (part->state2 >= 0)
418     {
419       surface2 = _gtk_win32_theme_part_create_surface  (part, part->part2, part->state2, 
420                                                         width, height);
421
422
423       cr = cairo_create (surface);
424
425       pattern = cairo_pattern_create_for_surface (surface2);
426       cairo_set_source (cr, pattern);
427       cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
428       cairo_paint_with_alpha (cr, part->over_alpha);
429       
430       cairo_destroy (cr);
431       cairo_pattern_destroy (pattern);
432
433       cairo_surface_destroy (surface2);
434     }
435
436   /* We need to return an image surface, as that is what the code expects in order
437      to get the size */
438   image = cairo_win32_surface_get_image (surface);
439   pattern = cairo_pattern_create_for_surface (cairo_surface_reference (image));
440
441   cairo_matrix_init_scale (&matrix,
442                            width,
443                            height);
444   cairo_pattern_set_matrix (pattern, &matrix);
445
446   /* We can't immediately destroy the surface, because that would free the data
447      the image surface refers too. Instead we destroy it with the pattern. */
448   cairo_pattern_set_user_data (pattern,
449                                &key,
450                                surface, (cairo_destroy_func_t) cairo_surface_destroy);
451
452   return pattern;
453 #else
454   GdkRGBA color;
455   
456   gdk_rgba_parse (&color, "pink");
457
458   return cairo_pattern_create_rgb (color.red, color.green, color.blue);
459 #endif
460 }
461
462 int
463 _gtk_win32_theme_int_parse (GtkCssParser      *parser,
464                             GFile             *base,
465                             int               *value)
466 {
467   char *class;
468   int arg;
469
470   if (_gtk_css_parser_try (parser,
471                            "-gtk-win32-size",
472                            TRUE))
473     {
474       if (!_gtk_css_parser_try (parser, "(", TRUE))
475         {
476           _gtk_css_parser_error (parser,
477                                  "Expected '(' after '-gtk-win32-size'");
478           return 0;
479         }
480
481       class = _gtk_css_parser_try_name (parser, TRUE);
482       if (class == NULL)
483         {
484           _gtk_css_parser_error (parser,
485                                  "Expected name as first argument to  '-gtk-win32-size'");
486           return 0;
487         }
488
489       if (! _gtk_css_parser_try (parser, ",", TRUE))
490         {
491           g_free (class);
492           _gtk_css_parser_error (parser,
493                                  "Expected ','");
494           return 0;
495         }
496
497       if (!_gtk_css_parser_try_int (parser, &arg))
498         {
499           g_free (class);
500           _gtk_css_parser_error (parser, "Expected a valid integer value");
501           return 0;
502         }
503
504       if (!_gtk_css_parser_try (parser, ")", TRUE))
505         {
506           _gtk_css_parser_error (parser,
507                                  "Expected ')'");
508           return 0;
509         }
510
511 #ifdef G_OS_WIN32
512       if (use_xp_theme && get_theme_sys_metric != NULL)
513         {
514           HTHEME theme = lookup_htheme_by_classname (class);
515
516           /* If theme is NULL it will just return the GetSystemMetrics value */
517           *value = get_theme_sys_metric (theme, arg);
518         }
519       else
520         *value = GetSystemMetrics (arg);
521 #else
522       *value = 1;
523 #endif
524
525       g_free (class);
526
527       return 1;
528     }
529
530   return -1;
531 }
532
533 GtkSymbolicColor *
534 _gtk_win32_theme_color_parse (GtkCssParser *parser)
535 {
536   GtkSymbolicColor *color;
537   char *class;
538   int id;
539
540   class = _gtk_css_parser_try_name (parser, TRUE);
541   if (class == NULL)
542     {
543       _gtk_css_parser_error (parser,
544                              "Expected name as first argument to  '-gtk-win32-color'");
545       return NULL;
546     }
547
548   if (! _gtk_css_parser_try (parser, ",", TRUE))
549     {
550       g_free (class);
551       _gtk_css_parser_error (parser,
552                              "Expected ','");
553       return NULL;
554     }
555
556   if (!_gtk_css_parser_try_int (parser, &id))
557     {
558       g_free (class);
559       _gtk_css_parser_error (parser, "Expected a valid integer value");
560       return NULL;
561     }
562
563   color = gtk_symbolic_color_new_win32 (class, id);
564   g_free (class);
565   return color;
566 }
567
568 gboolean
569 _gtk_win32_theme_color_resolve (const char *theme_class,
570                                 gint id,
571                                 GdkRGBA *color)
572 {
573 #ifdef G_OS_WIN32
574   DWORD dcolor;
575
576   if (use_xp_theme && get_theme_sys_color != NULL)
577     {
578       HTHEME theme = lookup_htheme_by_classname (theme_class);
579
580       /* if theme is NULL, it will just return the GetSystemColor()
581          value */
582       dcolor = get_theme_sys_color (theme, id);
583     }
584   else
585     dcolor = GetSysColor (id);
586
587   color->alpha = 1.0;
588   color->red = GetRValue (dcolor) / 255.0;
589   color->green = GetGValue (dcolor) / 255.0;
590   color->blue = GetBValue (dcolor) / 255.0;
591 #else
592   gdk_rgba_parse (color, "pink");
593 #endif
594   return TRUE;
595 }