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