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 #include "gtkwin32drawprivate.h"
27 
28 #ifdef G_OS_WIN32
29 
30 #include <windows.h>
31 #include <gdk/win32/gdkwin32.h>
32 #include <cairo-win32.h>
33 
34 typedef HANDLE HTHEME;
35 
36 #define UXTHEME_DLL "uxtheme.dll"
37 
38 static HINSTANCE uxtheme_dll = NULL;
39 static gboolean use_xp_theme = FALSE;
40 
41 typedef HRESULT (FAR PASCAL *GetThemeSysFontFunc)           (HTHEME hTheme, int iFontID, OUT LOGFONTW *plf);
42 typedef int (FAR PASCAL *GetThemeSysSizeFunc)               (HTHEME hTheme, int iSizeId);
43 typedef COLORREF (FAR PASCAL *GetThemeSysColorFunc)         (HTHEME hTheme,
44 							     int iColorID);
45 typedef HTHEME (FAR PASCAL *OpenThemeDataFunc)              (HWND hwnd,
46 							     LPCWSTR pszClassList);
47 typedef HRESULT (FAR PASCAL *CloseThemeDataFunc)            (HTHEME theme);
48 typedef HRESULT (FAR PASCAL *DrawThemeBackgroundFunc)       (HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
49 							     const RECT *pRect, const RECT *pClipRect);
50 typedef HRESULT (FAR PASCAL *EnableThemeDialogTextureFunc)  (HWND hwnd,
51 							     DWORD dwFlags);
52 typedef BOOL (FAR PASCAL *IsThemeActiveFunc)                (VOID);
53 typedef BOOL (FAR PASCAL *IsAppThemedFunc)                  (VOID);
54 typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
55 								      int iPartId,
56 								      int iStateId);
57 typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
58 							     HDC hdc,
59 							     RECT *prc);
60 typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc)          (HTHEME hTheme,
61 							     HDC hdc,
62 							     int iPartId,
63 							     int iStateId,
64 							     RECT *prc,
65 							     int eSize,
66 							     SIZE *psz);
67 typedef HRESULT (FAR PASCAL *GetThemeBackgroundExtentFunc)  (HTHEME hTheme,
68 							     HDC hdc,
69 							     int iPartId,
70 							     int iStateId,
71                                                              const RECT *pContentRect,
72                                                              RECT *pExtentRect);
73 
74 static GetThemeSysFontFunc get_theme_sys_font = NULL;
75 static GetThemeSysColorFunc GetThemeSysColor = NULL;
76 static GetThemeSysSizeFunc GetThemeSysSize = NULL;
77 static OpenThemeDataFunc OpenThemeData = NULL;
78 static CloseThemeDataFunc CloseThemeData = NULL;
79 static DrawThemeBackgroundFunc draw_theme_background = NULL;
80 static EnableThemeDialogTextureFunc enable_theme_dialog_texture = NULL;
81 static IsThemeActiveFunc is_theme_active = NULL;
82 static IsAppThemedFunc is_app_themed = NULL;
83 static IsThemeBackgroundPartiallyTransparentFunc is_theme_partially_transparent = NULL;
84 static DrawThemeParentBackgroundFunc draw_theme_parent_background = NULL;
85 static GetThemePartSizeFunc GetThemePartSize = NULL;
86 static GetThemeBackgroundExtentFunc GetThemeBackgroundExtent = NULL;
87 
88 #endif
89 
90 static GHashTable *themes_by_class = NULL;
91 
92 struct _GtkWin32Theme {
93   char *class_name;
94   gint ref_count;
95 #ifdef G_OS_WIN32
96   HTHEME htheme;
97 #endif
98 };
99 
100 GtkWin32Theme *
gtk_win32_theme_ref(GtkWin32Theme * theme)101 gtk_win32_theme_ref (GtkWin32Theme *theme)
102 {
103   theme->ref_count++;
104 
105   return theme;
106 }
107 
108 static gboolean
gtk_win32_theme_close(GtkWin32Theme * theme)109 gtk_win32_theme_close (GtkWin32Theme *theme)
110 {
111 #ifdef G_OS_WIN32
112   if (theme->htheme)
113     {
114       CloseThemeData (theme->htheme);
115       theme->htheme = NULL;
116       return TRUE;
117     }
118 #endif
119   return FALSE;
120 }
121 
122 void
gtk_win32_theme_unref(GtkWin32Theme * theme)123 gtk_win32_theme_unref (GtkWin32Theme *theme)
124 {
125   theme->ref_count--;
126 
127   if (theme->ref_count > 0)
128     return;
129 
130   g_hash_table_remove (themes_by_class, theme->class_name);
131 
132   gtk_win32_theme_close (theme);
133   g_free (theme->class_name);
134 
135   g_slice_free (GtkWin32Theme, theme);
136 }
137 
138 gboolean
gtk_win32_theme_equal(GtkWin32Theme * theme1,GtkWin32Theme * theme2)139 gtk_win32_theme_equal (GtkWin32Theme *theme1,
140                        GtkWin32Theme *theme2)
141 {
142   /* Themes are cached so they're guaranteed unique. */
143   return theme1 == theme2;
144 }
145 
146 #ifdef G_OS_WIN32
147 
148 static GdkFilterReturn
invalidate_win32_themes(GdkXEvent * xevent,GdkEvent * event,gpointer unused)149 invalidate_win32_themes (GdkXEvent *xevent,
150                          GdkEvent  *event,
151                          gpointer   unused)
152 {
153   GHashTableIter iter;
154   gboolean theme_was_open = FALSE;
155   gpointer theme;
156   MSG *msg;
157 
158   if (!GDK_IS_WIN32_WINDOW (event->any.window))
159     return GDK_FILTER_CONTINUE;
160 
161   msg = (MSG *) xevent;
162   if (msg->message != WM_THEMECHANGED)
163     return GDK_FILTER_CONTINUE;
164 
165   g_hash_table_iter_init (&iter, themes_by_class);
166   while (g_hash_table_iter_next (&iter, NULL, &theme))
167     {
168       theme_was_open |= gtk_win32_theme_close (theme);
169     }
170   if (theme_was_open)
171     gtk_style_context_reset_widgets (gdk_display_get_default_screen (gdk_window_get_display (event->any.window)));
172 
173   return GDK_FILTER_CONTINUE;
174 }
175 
176 static void
gtk_win32_theme_init(void)177 gtk_win32_theme_init (void)
178 {
179   char *buf;
180   char dummy;
181   int n, k;
182 
183   if (uxtheme_dll)
184     return;
185 
186   n = GetSystemDirectory (&dummy, 0);
187   if (n <= 0)
188     return;
189 
190   buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
191   k = GetSystemDirectory (buf, n);
192   if (k == 0 || k > n)
193     {
194       g_free (buf);
195       return;
196     }
197 
198   if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
199     strcat (buf, G_DIR_SEPARATOR_S);
200   strcat (buf, UXTHEME_DLL);
201 
202   uxtheme_dll = LoadLibrary (buf);
203   g_free (buf);
204 
205   if (!uxtheme_dll)
206     return;
207 
208   is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
209   if (is_app_themed)
210     {
211       is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
212       OpenThemeData = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
213       CloseThemeData = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
214       draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
215       enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
216       get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
217       GetThemeSysColor = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
218       GetThemeSysSize = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
219       is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
220       draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
221       GetThemePartSize = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
222       GetThemeBackgroundExtent = (GetThemeBackgroundExtentFunc) GetProcAddress (uxtheme_dll, "GetThemeBackgroundExtent");
223     }
224 
225   if (is_app_themed && is_theme_active)
226     {
227       use_xp_theme = (is_app_themed () && is_theme_active ());
228     }
229   else
230     {
231       use_xp_theme = FALSE;
232     }
233 
234   gdk_window_add_filter (NULL, invalidate_win32_themes, NULL);
235 }
236 
237 static HTHEME
gtk_win32_theme_get_htheme(GtkWin32Theme * theme)238 gtk_win32_theme_get_htheme (GtkWin32Theme *theme)
239 {
240   guint16 *wclass;
241 
242   gtk_win32_theme_init ();
243 
244   if (theme->htheme)
245     return theme->htheme;
246 
247   wclass = g_utf8_to_utf16 (theme->class_name, -1, NULL, NULL, NULL);
248   theme->htheme  = OpenThemeData (NULL, wclass);
249   g_free (wclass);
250 
251   return theme->htheme;
252 }
253 
254 #endif /* G_OS_WIN32 */
255 
256 static char *
canonicalize_class_name(const char * classname)257 canonicalize_class_name (const char *classname)
258 {
259   /* Wine claims class names are case insensitive, so we convert them
260      here to avoid multiple theme objects referencing the same HTHEME. */
261   return g_ascii_strdown (classname, -1);
262 }
263 
264 GtkWin32Theme *
gtk_win32_theme_lookup(const char * classname)265 gtk_win32_theme_lookup (const char *classname)
266 {
267   GtkWin32Theme *theme;
268   char *canonical_classname;
269 
270   if (G_UNLIKELY (themes_by_class == NULL))
271     themes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
272 
273   canonical_classname = canonicalize_class_name (classname);
274   theme = g_hash_table_lookup (themes_by_class, canonical_classname);
275 
276   if (theme != NULL)
277     {
278       g_free (canonical_classname);
279       return gtk_win32_theme_ref (theme);
280     }
281 
282   theme = g_slice_new0 (GtkWin32Theme);
283   theme->ref_count = 1;
284   theme->class_name = canonical_classname;
285 
286   g_hash_table_insert (themes_by_class, theme->class_name, theme);
287 
288   return theme;
289 }
290 
291 GtkWin32Theme *
gtk_win32_theme_parse(GtkCssParser * parser)292 gtk_win32_theme_parse (GtkCssParser *parser)
293 {
294   GtkWin32Theme *theme;
295   char *class_name;
296 
297   class_name = _gtk_css_parser_try_name (parser, TRUE);
298   if (class_name == NULL)
299     {
300       _gtk_css_parser_error (parser, "Expected valid win32 theme name");
301       return NULL;
302     }
303 
304   theme = gtk_win32_theme_lookup (class_name);
305   g_free (class_name);
306 
307   return theme;
308 }
309 
310 cairo_surface_t *
gtk_win32_theme_create_surface(GtkWin32Theme * theme,int xp_part,int state,int margins[4],int width,int height,int * x_offs_out,int * y_offs_out)311 gtk_win32_theme_create_surface (GtkWin32Theme *theme,
312                                 int            xp_part,
313 				int            state,
314 				int            margins[4],
315 				int            width,
316                                 int            height,
317 				int           *x_offs_out,
318 				int           *y_offs_out)
319 {
320   cairo_surface_t *surface;
321   cairo_t *cr;
322   int x_offs;
323   int y_offs;
324 #ifdef G_OS_WIN32
325   gboolean has_alpha;
326   HDC hdc;
327   RECT rect;
328   SIZE size;
329   HRESULT res;
330   HTHEME htheme;
331 #endif
332 
333   x_offs = margins[3];
334   y_offs = margins[0];
335 
336   width -= margins[3] + margins[1];
337   height -= margins[0] + margins[2];
338 
339 #ifdef G_OS_WIN32
340   htheme = gtk_win32_theme_get_htheme (theme);
341   if (htheme)
342     {
343       rect.left = 0;
344       rect.top = 0;
345       rect.right = width;
346       rect.bottom = height;
347 
348       hdc = GetDC (NULL);
349       res = GetThemePartSize (htheme, hdc, xp_part, state, &rect, 2 /*TS_DRAW*/, &size);
350       ReleaseDC (NULL, hdc);
351 
352       if (res == S_OK)
353         {
354           x_offs += (width - size.cx) / 2;
355           y_offs += (height - size.cy) / 2;
356 
357           width = size.cx;
358           height = size.cy;
359 
360           rect.right = width;
361           rect.bottom = height;
362         }
363 
364       has_alpha = is_theme_partially_transparent (htheme, xp_part, state);
365       if (has_alpha)
366         surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
367       else
368         surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);
369 
370       hdc = cairo_win32_surface_get_dc (surface);
371 
372       res = draw_theme_background (htheme, hdc, xp_part, state, &rect, &rect);
373 
374       *x_offs_out = x_offs;
375       *y_offs_out = y_offs;
376 
377       if (res == S_OK)
378         return surface;
379     }
380   else
381 #endif /* G_OS_WIN32 */
382     {
383       surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
384     }
385 
386   cr = cairo_create (surface);
387 
388   gtk_win32_draw_theme_background (cr, theme->class_name, xp_part, state, width, height);
389 
390   cairo_destroy (cr);
391 
392   *x_offs_out = x_offs;
393   *y_offs_out = y_offs;
394 
395   return surface;
396 }
397 
398 void
gtk_win32_theme_get_part_border(GtkWin32Theme * theme,int part,int state,GtkBorder * out_border)399 gtk_win32_theme_get_part_border (GtkWin32Theme  *theme,
400                                  int             part,
401                                  int             state,
402                                  GtkBorder      *out_border)
403 {
404 #ifdef G_OS_WIN32
405   HTHEME htheme = gtk_win32_theme_get_htheme (theme);
406   RECT content, extent;
407   HDC hdc;
408   HRESULT res;
409 
410   if (use_xp_theme && GetThemeBackgroundExtent != NULL && htheme != NULL)
411     {
412       /* According to Wine source code, these values don't matter
413        * because uxtheme.dll deals with margins internally. */
414       content.left = content.top = 0;
415       content.right = content.bottom = 100;
416 
417       hdc = GetDC (NULL);
418       res = GetThemeBackgroundExtent (htheme, hdc, part, state, &content, &extent);
419       ReleaseDC (NULL, hdc);
420 
421       if (SUCCEEDED (res))
422         {
423           out_border->top = content.top - extent.top;
424           out_border->left = content.left - extent.left;
425           out_border->bottom = extent.bottom - content.bottom;
426           out_border->right = extent.right - content.right;
427           return;
428         }
429     }
430 #endif
431 
432   gtk_win32_get_theme_margins (theme->class_name, part, state, out_border);
433 }
434 
435 void
gtk_win32_theme_get_part_size(GtkWin32Theme * theme,int part,int state,int * width,int * height)436 gtk_win32_theme_get_part_size (GtkWin32Theme  *theme,
437                                int             part,
438                                int             state,
439                                int            *width,
440                                int            *height)
441 {
442 #ifdef G_OS_WIN32
443   HTHEME htheme = gtk_win32_theme_get_htheme (theme);
444   SIZE size;
445   HRESULT res;
446 
447   if (use_xp_theme && GetThemePartSize != NULL && htheme != NULL)
448     {
449       res = GetThemePartSize (htheme, NULL, part, state, NULL, 1 /* TS_TRUE */, &size);
450 
451       if (SUCCEEDED (res))
452         {
453           if (width)
454             *width = size.cx;
455           if (height)
456             *height = size.cy;
457           return;
458         }
459     }
460 #endif
461   gtk_win32_get_theme_part_size (theme->class_name, part, state, width, height);
462 }
463 
464 int
gtk_win32_theme_get_size(GtkWin32Theme * theme,int id)465 gtk_win32_theme_get_size (GtkWin32Theme *theme,
466 			  int            id)
467 {
468 #ifdef G_OS_WIN32
469   if (use_xp_theme && GetThemeSysSize != NULL)
470     {
471       HTHEME htheme = gtk_win32_theme_get_htheme (theme);
472       int size;
473 
474       /* If htheme is NULL it will just return the GetSystemMetrics value */
475       size = GetThemeSysSize (htheme, id);
476       /* fall through on invalid parameter */
477       if (GetLastError () == 0)
478         return size;
479     }
480 
481   return GetSystemMetrics (id);
482 #else
483   return gtk_win32_get_sys_metric (id);
484 #endif
485 }
486 
487 void
gtk_win32_theme_get_color(GtkWin32Theme * theme,gint id,GdkRGBA * color)488 gtk_win32_theme_get_color (GtkWin32Theme *theme,
489                            gint           id,
490                            GdkRGBA       *color)
491 {
492 #ifdef G_OS_WIN32
493   HTHEME htheme;
494   DWORD dcolor;
495 
496   if (use_xp_theme && GetThemeSysColor != NULL)
497     {
498       htheme = gtk_win32_theme_get_htheme (theme);
499 
500       /* if htheme is NULL, it will just return the GetSysColor() value */
501       dcolor = GetThemeSysColor (htheme, id);
502     }
503   else
504     dcolor = GetSysColor (id);
505 
506   color->alpha = 1.0;
507   color->red = GetRValue (dcolor) / 255.0;
508   color->green = GetGValue (dcolor) / 255.0;
509   color->blue = GetBValue (dcolor) / 255.0;
510 #else
511   gtk_win32_get_sys_color (id, color);
512 #endif
513 }
514 
515 void
gtk_win32_theme_print(GtkWin32Theme * theme,GString * string)516 gtk_win32_theme_print (GtkWin32Theme *theme,
517                        GString       *string)
518 {
519   g_string_append (string, theme->class_name);
520 }
521