gtkwin32theme.c 12.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * Authors: Carlos Garnacho <carlosg@gnome.org>
 *          Cosimo Cecchi <cosimoc@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25
 */

#include <config.h>

#include "gtkwin32themeprivate.h"

26 27
#include <gtksymboliccolor.h>

28 29 30 31 32 33 34 35
#ifdef G_OS_WIN32

#include <cairo-win32.h>

#define UXTHEME_DLL "uxtheme.dll"

static HINSTANCE uxtheme_dll = NULL;
static gboolean use_xp_theme = FALSE;
36
static OSVERSIONINFO os_version;
37 38 39 40 41
static HTHEME needs_alpha_fixup1 = NULL;
static HTHEME needs_alpha_fixup2 = NULL;
static HTHEME needs_alpha_fixup3 = NULL;
static HTHEME needs_alpha_fixup4 = NULL;
static HTHEME needs_alpha_fixup5 = NULL;
42
static HTHEME needs_alpha_fixup6 = NULL;
43
static HTHEME needs_alpha_fixup7 = NULL;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

typedef HRESULT (FAR PASCAL *GetThemeSysFontFunc)           (HTHEME hTheme, int iFontID, OUT LOGFONTW *plf);
typedef int (FAR PASCAL *GetThemeSysSizeFunc)               (HTHEME hTheme, int iSizeId);
typedef COLORREF (FAR PASCAL *GetThemeSysColorFunc)         (HTHEME hTheme,
							     int iColorID);
typedef HTHEME (FAR PASCAL *OpenThemeDataFunc)              (HWND hwnd,
							     LPCWSTR pszClassList);
typedef HRESULT (FAR PASCAL *CloseThemeDataFunc)            (HTHEME theme);
typedef HRESULT (FAR PASCAL *DrawThemeBackgroundFunc)       (HTHEME hTheme, HDC hdc, int iPartId, int iStateId,
							     const RECT *pRect, const RECT *pClipRect);
typedef HRESULT (FAR PASCAL *EnableThemeDialogTextureFunc)  (HWND hwnd,
							     DWORD dwFlags);
typedef BOOL (FAR PASCAL *IsThemeActiveFunc)                (VOID);
typedef BOOL (FAR PASCAL *IsAppThemedFunc)                  (VOID);
typedef BOOL (FAR PASCAL *IsThemeBackgroundPartiallyTransparentFunc) (HTHEME hTheme,
								      int iPartId,
								      int iStateId);
typedef HRESULT (FAR PASCAL *DrawThemeParentBackgroundFunc) (HWND hwnd,
							     HDC hdc,
							     RECT *prc);
typedef HRESULT (FAR PASCAL *GetThemePartSizeFunc)          (HTHEME hTheme,
							     HDC hdc,
							     int iPartId,
							     int iStateId,
							     RECT *prc,
							     int eSize,
							     SIZE *psz);

static GetThemeSysFontFunc get_theme_sys_font = NULL;
static GetThemeSysColorFunc get_theme_sys_color = NULL;
static GetThemeSysSizeFunc get_theme_sys_metric = NULL;
static OpenThemeDataFunc open_theme_data = NULL;
static CloseThemeDataFunc close_theme_data = NULL;
static DrawThemeBackgroundFunc draw_theme_background = NULL;
static EnableThemeDialogTextureFunc enable_theme_dialog_texture = NULL;
static IsThemeActiveFunc is_theme_active = NULL;
static IsAppThemedFunc is_app_themed = NULL;
static IsThemeBackgroundPartiallyTransparentFunc is_theme_partially_transparent = NULL;
static DrawThemeParentBackgroundFunc draw_theme_parent_background = NULL;
static GetThemePartSizeFunc get_theme_part_size = NULL;

static GHashTable *hthemes_by_class = NULL;

static void
_gtk_win32_theme_init (void)
{
  char *buf;
  char dummy;
  int n, k;

  if (uxtheme_dll)
    return;

  n = GetSystemDirectory (&dummy, 0);
  if (n <= 0)
    return;

  buf = g_malloc (n + 1 + strlen (UXTHEME_DLL));
  k = GetSystemDirectory (buf, n);
  if (k == 0 || k > n)
    {
      g_free (buf);
      return;
    }

  if (!G_IS_DIR_SEPARATOR (buf[strlen (buf) -1]))
    strcat (buf, G_DIR_SEPARATOR_S);
  strcat (buf, UXTHEME_DLL);

  uxtheme_dll = LoadLibrary (buf);
  g_free (buf);

  if (!uxtheme_dll)
    return;

  is_app_themed = (IsAppThemedFunc) GetProcAddress (uxtheme_dll, "IsAppThemed");
  if (is_app_themed)
    {
      is_theme_active = (IsThemeActiveFunc) GetProcAddress (uxtheme_dll, "IsThemeActive");
      open_theme_data = (OpenThemeDataFunc) GetProcAddress (uxtheme_dll, "OpenThemeData");
      close_theme_data = (CloseThemeDataFunc) GetProcAddress (uxtheme_dll, "CloseThemeData");
      draw_theme_background = (DrawThemeBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeBackground");
      enable_theme_dialog_texture = (EnableThemeDialogTextureFunc) GetProcAddress (uxtheme_dll, "EnableThemeDialogTexture");
      get_theme_sys_font = (GetThemeSysFontFunc) GetProcAddress (uxtheme_dll, "GetThemeSysFont");
      get_theme_sys_color = (GetThemeSysColorFunc) GetProcAddress (uxtheme_dll, "GetThemeSysColor");
      get_theme_sys_metric = (GetThemeSysSizeFunc) GetProcAddress (uxtheme_dll, "GetThemeSysSize");
      is_theme_partially_transparent = (IsThemeBackgroundPartiallyTransparentFunc) GetProcAddress (uxtheme_dll, "IsThemeBackgroundPartiallyTransparent");
      draw_theme_parent_background = (DrawThemeParentBackgroundFunc) GetProcAddress (uxtheme_dll, "DrawThemeParentBackground");
      get_theme_part_size = (GetThemePartSizeFunc) GetProcAddress (uxtheme_dll, "GetThemePartSize");
    }

  if (is_app_themed && is_theme_active)
    {
      use_xp_theme = (is_app_themed () && is_theme_active ());
    }
  else
    {
      use_xp_theme = FALSE;
    }

  hthemes_by_class = g_hash_table_new (g_str_hash, g_str_equal);
145

146 147 148 149
  memset (&os_version, 0, sizeof (os_version));
  os_version.dwOSVersionInfoSize = sizeof (os_version);
  GetVersionEx (&os_version);
  if (os_version.dwMajorVersion == 5)
150 151 152 153 154 155
    {
      needs_alpha_fixup1 = _gtk_win32_lookup_htheme_by_classname ("scrollbar");
      needs_alpha_fixup2 = _gtk_win32_lookup_htheme_by_classname ("toolbar");
      needs_alpha_fixup3 = _gtk_win32_lookup_htheme_by_classname ("button");
      needs_alpha_fixup4 = _gtk_win32_lookup_htheme_by_classname ("header");
      needs_alpha_fixup5 = _gtk_win32_lookup_htheme_by_classname ("trackbar");
156
      needs_alpha_fixup6 = _gtk_win32_lookup_htheme_by_classname ("status");
157
      needs_alpha_fixup7 = _gtk_win32_lookup_htheme_by_classname ("rebar");
158
    }
159 160
}

161 162
HTHEME
_gtk_win32_lookup_htheme_by_classname (const char *class)
163 164 165 166 167
{
  HTHEME theme;
  guint16 *wclass;
  char *lower;
  
168 169
  _gtk_win32_theme_init ();

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  lower = g_ascii_strdown (class, -1);

  theme = (HTHEME)  g_hash_table_lookup (hthemes_by_class, lower);
  if (theme)
    {
      g_free (lower);
      return theme;
    }

  wclass = g_utf8_to_utf16 (lower, -1, NULL, NULL, NULL);
  theme  = open_theme_data (NULL, wclass);
  g_free (wclass);

  if (theme == NULL)
    {
      g_free (lower);
      return NULL;
    }

  /* Takes ownership of lower: */
  g_hash_table_insert (hthemes_by_class, lower, theme);

  return theme;
}

#else

197 198
HTHEME
_gtk_win32_lookup_htheme_by_classname (const char *class)
199 200 201 202 203 204
{
  return NULL;
}

#endif /* G_OS_WIN32 */

205
cairo_surface_t *
206 207 208 209 210
_gtk_win32_theme_part_create_surface (HTHEME theme,
                                      int    xp_part,
				      int    state,
				      int    margins[4],
				      int    width,
211 212 213
                                      int    height,
				      int   *x_offs_out,
				      int   *y_offs_out)
214 215
{
  cairo_surface_t *surface;
216 217
  GdkRGBA color;
  cairo_t *cr;
218 219
  int x_offs;
  int y_offs;
220
#ifdef G_OS_WIN32
221
  gboolean has_alpha;
222 223
  HDC hdc;
  RECT rect;
224
  SIZE size;
225
  HRESULT res;
226
#endif
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  x_offs = margins[3];
  y_offs = margins[0];

  width -= margins[3] + margins[1];
  height -= margins[0] + margins[2];

#ifdef G_OS_WIN32
  rect.left = 0;
  rect.top = 0;
  rect.right = width;
  rect.bottom = height;

  hdc = GetDC (NULL);
  res = get_theme_part_size (theme, hdc, xp_part, state, &rect, 2, &size);
  ReleaseDC (NULL, hdc);

  if (res == S_OK)
    {
      x_offs += (width - size.cx) / 2;
      y_offs += (height - size.cy) / 2;
248
  
249 250 251 252 253 254 255
      width = size.cx;
      height = size.cy;

      rect.right = width;
      rect.bottom = height;
    }

256 257
  has_alpha = is_theme_partially_transparent (theme, xp_part, state);
  if (has_alpha)
258 259 260 261 262
    surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_ARGB32, width, height);
  else
    surface = cairo_win32_surface_create_with_dib (CAIRO_FORMAT_RGB24, width, height);

  hdc = cairo_win32_surface_get_dc (surface);
263 264

  res = draw_theme_background (theme, hdc, xp_part, state, &rect, &rect);
265

266
  /* XP Can't handle rendering some parts on an alpha target */
267 268 269 270 271
  if (has_alpha && 
      (theme == needs_alpha_fixup1 ||
       theme == needs_alpha_fixup2 ||
       (theme == needs_alpha_fixup3 && xp_part == 4) ||
       theme == needs_alpha_fixup4 ||
272
       theme == needs_alpha_fixup5 ||
273 274
       theme == needs_alpha_fixup6 ||
       theme == needs_alpha_fixup7))
275 276 277
    {
      cairo_surface_t *img = cairo_win32_surface_get_image (surface);
      guint32 *data = (guint32 *)cairo_image_surface_get_data (img);
278
      int i, j;
279 280
      GdiFlush ();

281
      for (i = 0; i < width; i++)
282
	{
283
	  for (j = 0; j < height; j++)
284 285 286 287 288 289
	    {
	      if (data[i+j*width] != 0)
		data[i+j*width] |= 0xff000000;
	    }
	}
    }
290

291 292 293
  *x_offs_out = x_offs;
  *y_offs_out = y_offs;

294 295
  if (res == S_OK)
    return surface;
296

297 298 299
#else /* !G_OS_WIN32 */
  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
#endif /* G_OS_WIN32 */
300

301 302 303 304 305 306 307 308 309
  cr = cairo_create (surface);
  
  /* XXX: Do something better here (like printing the theme parts) */
  gdk_rgba_parse (&color, "pink");
  gdk_cairo_set_source_rgba (cr, &color);
  cairo_paint (cr);

  cairo_destroy (cr);
  
310 311 312
  *x_offs_out = x_offs;
  *y_offs_out = y_offs;

313 314 315
  return surface;
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
int
_gtk_win32_theme_int_parse (GtkCssParser      *parser,
			    int               *value)
{
  char *class;
  int arg;

  if (_gtk_css_parser_try (parser,
			   "-gtk-win32-size",
			   TRUE))
    {
      if (!_gtk_css_parser_try (parser, "(", TRUE))
	{
	  _gtk_css_parser_error (parser,
				 "Expected '(' after '-gtk-win32-size'");
	  return 0;
	}

      class = _gtk_css_parser_try_name (parser, TRUE);
      if (class == NULL)
	{
	  _gtk_css_parser_error (parser,
				 "Expected name as first argument to  '-gtk-win32-size'");
	  return 0;
	}

      if (! _gtk_css_parser_try (parser, ",", TRUE))
	{
	  g_free (class);
	  _gtk_css_parser_error (parser,
				 "Expected ','");
	  return 0;
	}

      if (!_gtk_css_parser_try_int (parser, &arg))
	{
	  g_free (class);
	  _gtk_css_parser_error (parser, "Expected a valid integer value");
	  return 0;
	}

      if (!_gtk_css_parser_try (parser, ")", TRUE))
	{
	  _gtk_css_parser_error (parser,
				 "Expected ')'");
	  return 0;
	}

#ifdef G_OS_WIN32
      if (use_xp_theme && get_theme_sys_metric != NULL)
	{
367
	  HTHEME theme = _gtk_win32_lookup_htheme_by_classname (class);
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

	  /* If theme is NULL it will just return the GetSystemMetrics value */
	  *value = get_theme_sys_metric (theme, arg);
	}
      else
	*value = GetSystemMetrics (arg);
#else
      *value = 1;
#endif

      g_free (class);

      return 1;
    }

  return -1;
}
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

GtkSymbolicColor *
_gtk_win32_theme_color_parse (GtkCssParser *parser)
{
  GtkSymbolicColor *color;
  char *class;
  int id;

  class = _gtk_css_parser_try_name (parser, TRUE);
  if (class == NULL)
    {
      _gtk_css_parser_error (parser,
			     "Expected name as first argument to  '-gtk-win32-color'");
      return NULL;
    }

  if (! _gtk_css_parser_try (parser, ",", TRUE))
    {
      g_free (class);
      _gtk_css_parser_error (parser,
			     "Expected ','");
      return NULL;
    }

  if (!_gtk_css_parser_try_int (parser, &id))
    {
      g_free (class);
      _gtk_css_parser_error (parser, "Expected a valid integer value");
      return NULL;
    }

  color = gtk_symbolic_color_new_win32 (class, id);
  g_free (class);
  return color;
}

gboolean
_gtk_win32_theme_color_resolve (const char *theme_class,
				gint id,
				GdkRGBA *color)
{
#ifdef G_OS_WIN32
  DWORD dcolor;

  if (use_xp_theme && get_theme_sys_color != NULL)
    {
431
      HTHEME theme = _gtk_win32_lookup_htheme_by_classname (theme_class);
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448

      /* if theme is NULL, it will just return the GetSystemColor()
         value */
      dcolor = get_theme_sys_color (theme, id);
    }
  else
    dcolor = GetSysColor (id);

  color->alpha = 1.0;
  color->red = GetRValue (dcolor) / 255.0;
  color->green = GetGValue (dcolor) / 255.0;
  color->blue = GetBValue (dcolor) / 255.0;
#else
  gdk_rgba_parse (color, "pink");
#endif
  return TRUE;
}
449 450 451 452 453 454 455 456 457 458 459 460 461

const char *
_gtk_win32_theme_get_default (void)
{
#ifdef G_OS_WIN32
  _gtk_win32_theme_init ();
  
  if (use_xp_theme)
    return (os_version.dwMajorVersion >= 6) ? "gtk-win32" : "gtk-win32-xp";
#endif
  return "gtk-win32-classic";
}