msw_style.c 55.1 KB
Newer Older
1
/* MS-Windows Engine (aka GTK-Wimp)
2
 *
3
 * Copyright (C) 2003, 2004 Raymond Penners <raymond@dotsphinx.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Includes code adapted from redmond95 by Owen Taylor, and
 * gtk-nativewin by Evan Martin
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23
#include "msw_style.h"
Raymond Penners's avatar
Raymond Penners committed
24
#include "xp_theme.h"
25 26 27 28 29 30 31 32 33 34 35 36

#include <windows.h>
#include <math.h>
#include <string.h>
#include <gtk/gtk.h>
#include <gtk/gtk.h>
#include <gdk/gdkwin32.h>

#include <stdio.h>

/* Default values, not normally used
 */
cinamod's avatar
cinamod committed
37 38
static const GtkRequisition default_option_indicator_size = { 9, 8 };
static const GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

static GtkStyleClass *parent_class;

typedef enum {
  CHECK_AA,
  CHECK_BASE,
  CHECK_BLACK,
  CHECK_DARK,
  CHECK_LIGHT,
  CHECK_MID,
  CHECK_TEXT,
  RADIO_BASE,
  RADIO_BLACK,
  RADIO_DARK,
  RADIO_LIGHT,
  RADIO_MID,
  RADIO_TEXT
} Part;

#define PART_SIZE 13

cinamod's avatar
cinamod committed
60
static const char check_aa_bits[] = {
61 62
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
63
static const char check_base_bits[] = {
64 65
 0x00,0x00,0x00,0x00,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
 0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0x00,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
66
static const char check_black_bits[] = {
67 68
 0x00,0x00,0xfe,0x0f,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
 0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
69
static const char check_dark_bits[] = {
70 71
 0xff,0x1f,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
 0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00};
cinamod's avatar
cinamod committed
72
static const char check_light_bits[] = {
73 74
 0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
 0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0xfe,0x1f};
cinamod's avatar
cinamod committed
75
static const char check_mid_bits[] = {
76 77
 0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
 0x08,0x00,0x08,0x00,0x08,0x00,0x08,0xfc,0x0f,0x00,0x00};
cinamod's avatar
cinamod committed
78
static const char check_text_bits[] = {
79 80
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x03,0x88,0x03,0xd8,0x01,0xf8,
 0x00,0x70,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
81
static const char radio_base_bits[] = {
82 83
 0x00,0x00,0x00,0x00,0xf0,0x01,0xf8,0x03,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
 0x07,0xfc,0x07,0xf8,0x03,0xf0,0x01,0x00,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
84
static const char radio_black_bits[] = {
85 86
 0x00,0x00,0xf0,0x01,0x0c,0x02,0x04,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
 0x00,0x02,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
87
static const char radio_dark_bits[] = {
88 89
 0xf0,0x01,0x0c,0x06,0x02,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
 0x00,0x01,0x00,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x00};
cinamod's avatar
cinamod committed
90
static const char radio_light_bits[] = {
91 92
 0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
 0x10,0x00,0x10,0x00,0x08,0x00,0x08,0x0c,0x06,0xf0,0x01};
cinamod's avatar
cinamod committed
93
static const char radio_mid_bits[] = {
94 95
 0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x04,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
 0x08,0x00,0x08,0x00,0x04,0x0c,0x06,0xf0,0x01,0x00,0x00};
cinamod's avatar
cinamod committed
96
static const char radio_text_bits[] = {
97 98 99 100
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0xf0,0x01,0xf0,0x01,0xf0,
 0x01,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

static struct {
cinamod's avatar
cinamod committed
101 102
  const char *bits;
  GdkBitmap  *bmap;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
} parts[] = {
  { check_aa_bits, NULL },
  { check_base_bits, NULL },
  { check_black_bits, NULL },
  { check_dark_bits, NULL },
  { check_light_bits, NULL },
  { check_mid_bits, NULL },
  { check_text_bits, NULL },
  { radio_base_bits, NULL },
  { radio_black_bits, NULL },
  { radio_dark_bits, NULL },
  { radio_light_bits, NULL },
  { radio_mid_bits, NULL },
  { radio_text_bits, NULL }
};

static gboolean
cinamod's avatar
cinamod committed
120
get_system_font(XpThemeClass klazz, XpThemeFont type, LOGFONT *out_lf)
121
{
cinamod's avatar
cinamod committed
122 123
#if 0
  /* TODO: this crashes. need to figure out why and how to fix it */
cinamod's avatar
cinamod committed
124 125 126
  if (xp_theme_get_system_font(klazz, type, out_lf))
    return TRUE;
  else
cinamod's avatar
cinamod committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
#endif
  {
	  NONCLIENTMETRICS ncm;
	  ncm.cbSize = sizeof(NONCLIENTMETRICS);

	  if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
				   sizeof(NONCLIENTMETRICS), &ncm, 0))
	    {
	      if (type == XP_THEME_FONT_CAPTION)
		*out_lf = ncm.lfCaptionFont;
	      else if (type == XP_THEME_FONT_MENU)
		*out_lf = ncm.lfMenuFont;
	      else if (type == XP_THEME_FONT_STATUS)
		*out_lf = ncm.lfStatusFont;
	      else
		*out_lf = ncm.lfMessageFont;

	      return TRUE;
	    }
  }
cinamod's avatar
cinamod committed
147
  return FALSE;
148 149
}

cinamod's avatar
cinamod committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 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
/***************************** STOLEN FROM PANGO *****************************/

/*
	This code is stolen from Pango 1.4. It attempts to address the following problems:

	http://bugzilla.gnome.org/show_bug.cgi?id=135098
	http://sourceforge.net/tracker/index.php?func=detail&aid=895762&group_id=76416&atid=547655

	As Owen suggested in bug 135098, once Pango 1.6 is released, we need to get rid of this code.
*/

#define PING(printlist)

/* TrueType defines: */

#define MAKE_TT_TABLE_NAME(c1, c2, c3, c4) \
   (((guint32)c4) << 24 | ((guint32)c3) << 16 | ((guint32)c2) << 8 | ((guint32)c1))

#define CMAP (MAKE_TT_TABLE_NAME('c','m','a','p'))
#define CMAP_HEADER_SIZE 4

#define NAME (MAKE_TT_TABLE_NAME('n','a','m','e'))
#define NAME_HEADER_SIZE 6

#define ENCODING_TABLE_SIZE 8

#define APPLE_UNICODE_PLATFORM_ID 0
#define MACINTOSH_PLATFORM_ID 1
#define ISO_PLATFORM_ID 2
#define MICROSOFT_PLATFORM_ID 3

#define SYMBOL_ENCODING_ID 0
#define UNICODE_ENCODING_ID 1
#define UCS4_ENCODING_ID 10

struct name_header
{
  guint16 format_selector;
  guint16 num_records;
  guint16 string_storage_offset;
};

struct name_record
{
  guint16 platform_id;
  guint16 encoding_id;
  guint16 language_id;
  guint16 name_id;
  guint16 string_length;
  guint16 string_offset;
};

gboolean
pango_win32_get_name_header (HDC                 hdc,
			     struct name_header *header)
{
  if (GetFontData (hdc, NAME, 0, header, sizeof (*header)) != sizeof (*header))
    return FALSE;

  header->num_records = GUINT16_FROM_BE (header->num_records);
  header->string_storage_offset = GUINT16_FROM_BE (header->string_storage_offset);

  return TRUE;
}

gboolean
pango_win32_get_name_record (HDC                 hdc,
			     gint                i,
			     struct name_record *record)
{
  if (GetFontData (hdc, NAME, 6 + i * sizeof (*record),
		   record, sizeof (*record)) != sizeof (*record))
    return FALSE;

  record->platform_id = GUINT16_FROM_BE (record->platform_id);
  record->encoding_id = GUINT16_FROM_BE (record->encoding_id);
  record->language_id = GUINT16_FROM_BE (record->language_id);
  record->name_id = GUINT16_FROM_BE (record->name_id);
  record->string_length = GUINT16_FROM_BE (record->string_length);
  record->string_offset = GUINT16_FROM_BE (record->string_offset);

  return TRUE;
}

static gchar *
get_family_name (LOGFONT *lfp, HDC pango_win32_hdc)
{
  HFONT hfont;
  HFONT oldhfont;

  struct name_header header;
  struct name_record record;

  gint unicode_ix = -1, mac_ix = -1, microsoft_ix = -1;
  gint name_ix;
  gchar *codeset;

  gchar *string = NULL;
  gchar *name;

  gint i, l;
  gsize nbytes;

  /* If lfFaceName is ASCII, assume it is the common (English) name
   * for the font. Is this valid? Do some TrueType fonts have
   * different names in French, German, etc, and does the system
   * return these if the locale is set to use French, German, etc?
   */
  l = strlen (lfp->lfFaceName);
  for (i = 0; i < l; i++)
    if (lfp->lfFaceName[i] < ' ' || lfp->lfFaceName[i] > '~')
      break;

  if (i == l)
    return g_strdup (lfp->lfFaceName);

  if ((hfont = CreateFontIndirect (lfp)) == NULL)
    goto fail0;

  if ((oldhfont = SelectObject (pango_win32_hdc, hfont)) == NULL)
    goto fail1;

  if (!pango_win32_get_name_header (pango_win32_hdc, &header))
    goto fail2;

  PING (("%d name records", header.num_records));

  for (i = 0; i < header.num_records; i++)
    {
      if (!pango_win32_get_name_record (pango_win32_hdc, i, &record))
	goto fail2;

      if ((record.name_id != 1 && record.name_id != 16) || record.string_length <= 0)
	continue;

      PING(("platform:%d encoding:%d language:%04x name_id:%d",
	    record.platform_id, record.encoding_id, record.language_id, record.name_id));

      if (record.platform_id == APPLE_UNICODE_PLATFORM_ID ||
	  record.platform_id == ISO_PLATFORM_ID)
	unicode_ix = i;
      else if (record.platform_id == MACINTOSH_PLATFORM_ID &&
	       record.encoding_id == 0 && /* Roman */
	       record.language_id == 0)	/* English */
	mac_ix = i;
      else if (record.platform_id == MICROSOFT_PLATFORM_ID)
	if ((microsoft_ix == -1 ||
	     PRIMARYLANGID (record.language_id) == LANG_ENGLISH) &&
	    (record.encoding_id == SYMBOL_ENCODING_ID ||
	     record.encoding_id == UNICODE_ENCODING_ID ||
	     record.encoding_id == UCS4_ENCODING_ID))
	  microsoft_ix = i;
    }

  if (microsoft_ix >= 0)
    name_ix = microsoft_ix;
  else if (mac_ix >= 0)
    name_ix = mac_ix;
  else if (unicode_ix >= 0)
    name_ix = unicode_ix;
  else
    goto fail2;

  if (!pango_win32_get_name_record (pango_win32_hdc, name_ix, &record))
    goto fail2;

  string = g_malloc (record.string_length + 1);
  if (GetFontData (pango_win32_hdc, NAME,
		   header.string_storage_offset + record.string_offset,
		   string, record.string_length) != record.string_length)
    goto fail2;

  string[record.string_length] = '\0';

  if (name_ix == microsoft_ix)
    if (record.encoding_id == SYMBOL_ENCODING_ID ||
	record.encoding_id == UNICODE_ENCODING_ID)
      codeset = "UTF-16BE";
    else
      codeset = "UCS-4BE";
  else if (name_ix == mac_ix)
    codeset = "MacRoman";
  else /* name_ix == unicode_ix */
    codeset = "UCS-4BE";

  name = g_convert (string, record.string_length, "UTF-8", codeset, NULL, &nbytes, NULL);
  if (name == NULL)
    goto fail2;
  g_free (string);

  PING(("%s", name));

  SelectObject (pango_win32_hdc, oldhfont);
  DeleteObject (hfont);

  return name;

 fail2:
  g_free (string);
  SelectObject (pango_win32_hdc, oldhfont);

 fail1:
  DeleteObject (hfont);

 fail0:
  return g_locale_to_utf8 (lfp->lfFaceName, -1, NULL, NULL, NULL);
}

/***************************** STOLEN FROM PANGO *****************************/

cinamod's avatar
cinamod committed
360
static char *
cinamod's avatar
cinamod committed
361
sys_font_to_pango_font (XpThemeClass klazz, XpThemeFont type, char * buf, size_t bufsiz)
362
{
cinamod's avatar
cinamod committed
363 364
  HDC hDC;
  HWND hwnd;
365
  LOGFONT lf;
cinamod's avatar
cinamod committed
366
  int pt_size;
cinamod's avatar
cinamod committed
367 368
  const char * weight;
  const char * style;
cinamod's avatar
cinamod committed
369
  char * font;
cinamod's avatar
cinamod committed
370

cinamod's avatar
cinamod committed
371
  if (get_system_font(klazz, type, &lf))
372
    {
373 374 375
      switch (lf.lfWeight) {
      case FW_THIN:
      case FW_EXTRALIGHT:
cinamod's avatar
cinamod committed
376
	weight = "Ultra-Light";
377 378 379
	break;

      case FW_LIGHT:
cinamod's avatar
cinamod committed
380
	weight = "Light";
381 382 383
	break;

      case FW_BOLD:
cinamod's avatar
cinamod committed
384 385 386 387 388
	weight = "Bold";
	break;

      case FW_SEMIBOLD:
	weight = "Semi-Bold";
389 390 391
	break;

      case FW_ULTRABOLD:
cinamod's avatar
cinamod committed
392
	weight = "Ultra-Bold";
393 394 395
	break;

      case FW_HEAVY:
cinamod's avatar
cinamod committed
396 397
	weight = "Heavy";
	break;
398 399

      default:
cinamod's avatar
cinamod committed
400
	weight = "";
401 402 403 404
	break;
      }

      if (lf.lfItalic)
cinamod's avatar
cinamod committed
405
	style="Italic";
406
      else
cinamod's avatar
cinamod committed
407
	style="";
cinamod's avatar
cinamod committed
408

cinamod's avatar
cinamod committed
409 410 411 412 413 414 415 416
	hwnd = GetDesktopWindow();
	hDC = GetDC(hwnd);
	if (hDC) {
		pt_size = -MulDiv(lf.lfHeight, 72,
	                      GetDeviceCaps(hDC,LOGPIXELSY));
		ReleaseDC(hwnd, hDC);
	} else
		pt_size = 10;
cinamod's avatar
cinamod committed
417

cinamod's avatar
cinamod committed
418 419 420
	font = get_family_name(&lf, hDC);
    g_snprintf(buf, bufsiz, "%s %s %s %d", font, style, weight, pt_size);
	g_free(font);
cinamod's avatar
cinamod committed
421 422 423

    return buf;
   }
cinamod's avatar
cinamod committed
424

425
  return NULL;
cinamod's avatar
cinamod committed
426 427
}

cinamod's avatar
cinamod committed
428 429 430 431 432
/* missing from ms's header files */
#ifndef SPI_GETMENUSHOWDELAY
#define SPI_GETMENUSHOWDELAY 106
#endif

cinamod's avatar
cinamod committed
433 434 435 436
/* I don't know the proper XP theme class for things like
   HIGHLIGHTTEXT, so we'll just define it to be "BUTTON"
   for now */
#define XP_THEME_CLASS_TEXT XP_THEME_CLASS_BUTTON
cinamod's avatar
cinamod committed
437

cinamod's avatar
cinamod committed
438
static void
cinamod's avatar
cinamod committed
439
setup_menu_settings (GtkSettings * settings)
cinamod's avatar
cinamod committed
440
{
441 442 443
  int menu_delay;
  gboolean win95 = FALSE;
  OSVERSIONINFOEX osvi;
cinamod's avatar
cinamod committed
444
  GObjectClass * klazz = G_OBJECT_GET_CLASS(G_OBJECT(settings));
cinamod's avatar
cinamod committed
445

446 447
  ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
cinamod's avatar
cinamod committed
448

449 450
  if (!GetVersionEx ( (OSVERSIONINFO *) &osvi))
    win95 = TRUE; /* assume the worst */
cinamod's avatar
cinamod committed
451

452 453 454
  if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
    if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
      win95 = TRUE;
cinamod's avatar
cinamod committed
455

456 457 458 459 460 461 462 463 464 465 466 467 468 469
  if (!win95) {
    if (SystemParametersInfo (SPI_GETMENUSHOWDELAY, 0, &menu_delay, 0)) {
      if (klazz) {
	if (g_object_class_find_property (klazz, "gtk-menu-bar-popup-delay")) {
	  g_object_set (G_OBJECT (settings), "gtk-menu-bar-popup-delay",
			0, NULL);
	}
	if (g_object_class_find_property (klazz, "gtk-menu-popup-delay")) {
	  g_object_set (G_OBJECT (settings), "gtk-menu-popup-delay",
			menu_delay, NULL);
	}
	if (g_object_class_find_property (klazz, "gtk-menu-popdown-delay")) {
	  g_object_set (G_OBJECT (settings), "gtk-menu-popdown-delay",
			menu_delay, NULL);
cinamod's avatar
cinamod committed
470
	}
471 472 473
      }
    }
  }
cinamod's avatar
cinamod committed
474 475
}

476
void
477
msw_style_setup_system_settings (void)
cinamod's avatar
cinamod committed
478 479
{
  GtkSettings * settings;
cinamod's avatar
cinamod committed
480
  int cursor_blink_time;
cinamod's avatar
cinamod committed
481 482 483 484 485 486 487 488 489 490 491 492

  settings = gtk_settings_get_default ();
  if (!settings)
    return;

  cursor_blink_time = GetCaretBlinkTime ();
  g_object_set (G_OBJECT (settings), "gtk-cursor-blink",
		        cursor_blink_time > 0, NULL);

  if (cursor_blink_time > 0)
  {
  	g_object_set (G_OBJECT (settings), "gtk-cursor-blink-time",
Raymond Penners's avatar
Raymond Penners committed
493
					2*cursor_blink_time, NULL);
cinamod's avatar
cinamod committed
494 495
  }

Raymond Penners's avatar
Raymond Penners committed
496
  g_object_set (G_OBJECT (settings), "gtk-double-click-time",
cinamod's avatar
cinamod committed
497 498
		GetDoubleClickTime(), NULL);
  g_object_set (G_OBJECT (settings), "gtk-dnd-drag-threshold",
cinamod's avatar
cinamod committed
499
		GetSystemMetrics(SM_CXDRAG), NULL);
cinamod's avatar
cinamod committed
500

cinamod's avatar
cinamod committed
501
  setup_menu_settings (settings);
cinamod's avatar
cinamod committed
502

cinamod's avatar
cinamod committed
503 504 505 506 507 508 509
  /*
     http://developer.gnome.org/doc/API/2.0/gtk/GtkSettings.html
     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/systemparametersinfo.asp
     http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemmetrics.asp
  */
}

cinamod's avatar
cinamod committed
510 511 512
static void
setup_system_font(GtkStyle *style)
{
cinamod's avatar
cinamod committed
513
  char buf[256], * font; /* It's okay, lfFaceName is smaller than 32 chars */
cinamod's avatar
cinamod committed
514

Raymond Penners's avatar
Raymond Penners committed
515 516 517 518 519
  if ((font = sys_font_to_pango_font(XP_THEME_CLASS_TEXT,
                                     XP_THEME_FONT_MESSAGE,
                                     buf, sizeof (buf))) != NULL)
    {
      if (style->font_desc)
cinamod's avatar
cinamod committed
520 521
	pango_font_description_free (style->font_desc);

Raymond Penners's avatar
Raymond Penners committed
522 523
      style->font_desc = pango_font_description_from_string(font);
    }
524 525 526
}

static void
cinamod's avatar
cinamod committed
527
sys_color_to_gtk_color(XpThemeClass klazz, int id, GdkColor *pcolor)
528
{
cinamod's avatar
cinamod committed
529 530 531 532 533
  DWORD color;

  if (!xp_theme_get_system_color (klazz, id, &color))
  	color = GetSysColor(id);

Raymond Penners's avatar
Raymond Penners committed
534 535 536 537
  pcolor->pixel = color;
  pcolor->red   = (GetRValue(color) << 8) | GetRValue(color);
  pcolor->green = (GetGValue(color) << 8) | GetGValue(color);
  pcolor->blue  = (GetBValue(color) << 8) | GetBValue(color);
538 539
}

cinamod's avatar
cinamod committed
540 541 542
static int
get_system_metric(XpThemeClass klazz, int id)
{
Raymond Penners's avatar
Raymond Penners committed
543
  int rval;
cinamod's avatar
cinamod committed
544

Raymond Penners's avatar
Raymond Penners committed
545 546
  if (!xp_theme_get_system_metric(klazz, id, &rval))
    rval = GetSystemMetrics (id);
cinamod's avatar
cinamod committed
547

Raymond Penners's avatar
Raymond Penners committed
548
  return rval;
cinamod's avatar
cinamod committed
549 550
}

551
static void
552
setup_msw_rc_style(void)
553
{
cinamod's avatar
cinamod committed
554 555 556
  /* TODO: Owen says:
  	 "If your setup_system_styles() function called gtk_rc_parse_string(), then you are just piling a new set of strings on top each time the theme changes .. the old ones won't be removed" */

cinamod's avatar
cinamod committed
557
  char buf[1024], font_buf[256], *font_ptr;
cinamod's avatar
cinamod committed
558

Raymond Penners's avatar
Raymond Penners committed
559 560
  GdkColor menu_color;
  GdkColor menu_text_color;
Raymond Penners's avatar
Raymond Penners committed
561 562 563
  GdkColor tooltip_back;
  GdkColor tooltip_fore;
  GdkColor btn_fore;
cinamod's avatar
cinamod committed
564
  GdkColor btn_face;
Raymond Penners's avatar
Raymond Penners committed
565 566
  GdkColor progress_back;

cinamod's avatar
cinamod committed
567 568 569 570 571
  GdkColor fg_prelight;
  GdkColor bg_prelight;
  GdkColor base_prelight;
  GdkColor text_prelight;

572 573
  NONCLIENTMETRICS nc;

Raymond Penners's avatar
Raymond Penners committed
574
  /* Prelight */
cinamod's avatar
cinamod committed
575 576 577 578
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT, &fg_prelight);
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT, &bg_prelight);
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT, &base_prelight);
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT, &text_prelight);
579

cinamod's avatar
cinamod committed
580 581
  sys_color_to_gtk_color(XP_THEME_CLASS_MENU, COLOR_MENUTEXT, &menu_text_color);
  sys_color_to_gtk_color(XP_THEME_CLASS_MENU, COLOR_MENU, &menu_color);
582

Raymond Penners's avatar
Raymond Penners committed
583
  /* tooltips */
cinamod's avatar
cinamod committed
584 585
  sys_color_to_gtk_color(XP_THEME_CLASS_TOOLTIP, COLOR_INFOTEXT, &tooltip_fore);
  sys_color_to_gtk_color(XP_THEME_CLASS_TOOLTIP, COLOR_INFOBK, &tooltip_back);
Raymond Penners's avatar
Raymond Penners committed
586 587

  /* text on push buttons. TODO: button shadows, backgrounds, and highlights */
cinamod's avatar
cinamod committed
588
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_BTNTEXT, &btn_fore);
cinamod's avatar
cinamod committed
589
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_BTNFACE, &btn_face);
Raymond Penners's avatar
Raymond Penners committed
590 591

  /* progress bar background color */
cinamod's avatar
cinamod committed
592
  sys_color_to_gtk_color(XP_THEME_CLASS_PROGRESS, COLOR_HIGHLIGHT, &progress_back);
Raymond Penners's avatar
Raymond Penners committed
593 594

  /* Enable coloring for menus. */
cinamod's avatar
cinamod committed
595 596
  font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_MENU, XP_THEME_FONT_MENU,font_buf, sizeof (font_buf));
  g_snprintf(buf, sizeof (buf),
597
	     "style \"msw-menu\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
598 599 600 601 602 603 604 605
	     "{\n"
	     "fg[PRELIGHT] = { %d, %d, %d }\n"
	     "bg[PRELIGHT] = { %d, %d, %d }\n"
	     "text[PRELIGHT] = { %d, %d, %d }\n"
	     "base[PRELIGHT] = { %d, %d, %d }\n"
	     "fg[NORMAL] = { %d, %d, %d }\n"
	     "bg[NORMAL] = { %d, %d, %d }\n"
	     "%s = \"%s\"\n"
606 607 608
	     "}widget_class \"*MenuItem*\" style \"msw-menu\"\n"
	     "widget_class \"*GtkMenu\" style \"msw-menu\"\n"
	     "widget_class \"*GtkMenuShell*\" style \"msw-menu\"\n",
cinamod's avatar
cinamod committed
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
	     fg_prelight.red,
	     fg_prelight.green,
	     fg_prelight.blue,
	     bg_prelight.red,
	     bg_prelight.green,
	     bg_prelight.blue,
	     text_prelight.red,
	     text_prelight.green,
	     text_prelight.blue,
	     base_prelight.red,
	     base_prelight.green,
	     base_prelight.blue,
	     menu_text_color.red,
	     menu_text_color.green,
	     menu_text_color.blue,
	     menu_color.red,
	     menu_color.green,
	     menu_color.blue,
	     (font_ptr ? "font_name" : "#"),
	     (font_ptr ? font_ptr : " font name should go here"));
Raymond Penners's avatar
Raymond Penners committed
629
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
630

cinamod's avatar
cinamod committed
631 632
  /* Enable coloring for menu bars. */
  g_snprintf(buf, sizeof (buf),
633
	     "style \"msw-menu-bar\" = \"msw-menu\"\n"
cinamod's avatar
cinamod committed
634 635
	     "{\n"
	     "bg[NORMAL] = { %d, %d, %d }\n"
cinamod's avatar
cinamod committed
636
	     "GtkMenuBar::shadow-type = etched-in\n"
637
	     "}widget_class \"*MenuBar*\" style \"msw-menu-bar\"\n",
cinamod's avatar
cinamod committed
638 639 640 641
	     btn_face.red,
	     btn_face.green,
	     btn_face.blue);
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
642

cinamod's avatar
cinamod committed
643
  /* enable tooltip fonts */
cinamod's avatar
cinamod committed
644 645
  font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_STATUS, XP_THEME_FONT_STATUS,font_buf, sizeof (font_buf));
  g_snprintf(buf, sizeof (buf),
646
	     "style \"msw-tooltips-caption\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
647 648
	     "{fg[NORMAL] = { %d, %d, %d }\n"
	     "%s = \"%s\"\n"
649
	     "}widget \"gtk-tooltips.GtkLabel\" style \"msw-tooltips-caption\"\n",
cinamod's avatar
cinamod committed
650 651 652 653 654
	     tooltip_fore.red,
	     tooltip_fore.green,
	     tooltip_fore.blue,
	     (font_ptr ? "font_name" : "#"),
	     (font_ptr ? font_ptr : " font name should go here"));
cinamod's avatar
cinamod committed
655
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
656

cinamod's avatar
cinamod committed
657
  g_snprintf(buf, sizeof (buf),
658
	     "style \"msw-tooltips\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
659
	     "{bg[NORMAL] = { %d, %d, %d }\n"
660
	     "}widget \"gtk-tooltips*\" style \"msw-tooltips\"\n",
cinamod's avatar
cinamod committed
661 662 663
	     tooltip_back.red,
	     tooltip_back.green,
	     tooltip_back.blue);
Raymond Penners's avatar
Raymond Penners committed
664
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
665

cinamod's avatar
cinamod committed
666
  /* enable font theming for status bars */
cinamod's avatar
cinamod committed
667 668
  font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_STATUS, XP_THEME_FONT_STATUS,font_buf, sizeof (font_buf));
  g_snprintf(buf, sizeof (buf),
669
	     "style \"msw-status\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
670 671
	     "{%s = \"%s\"\n"
	     "bg[NORMAL] = { %d, %d, %d }\n"
672
	     "}widget_class \"*Status*\" style \"msw-status\"\n",
cinamod's avatar
cinamod committed
673 674 675
	     (font_ptr ? "font_name" : "#"),
	     (font_ptr ? font_ptr : " font name should go here"),
	     btn_face.red, btn_face.green, btn_face.blue);
cinamod's avatar
cinamod committed
676
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
677

Raymond Penners's avatar
Raymond Penners committed
678 679
  /* enable coloring for text on buttons
     TODO: use GetThemeMetric for the border and outside border */
cinamod's avatar
cinamod committed
680
  g_snprintf(buf, sizeof (buf),
681
	     "style \"msw-button\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
682 683 684 685 686 687 688 689 690
	     "{\n"
	     "bg[NORMAL] = { %d, %d, %d }\n"
	     "bg[PRELIGHT] = { %d, %d, %d }\n"
	     "bg[INSENSITIVE] = { %d, %d, %d }\n"
	     "fg[PRELIGHT] = { %d, %d, %d }\n"
	     "GtkButton::default-border = { 1, 1, 1, 1 }\n"
	     "GtkButton::default-outside-border = { 0, 0, 0, 0 }\n"
	     "GtkButton::child-displacement-x = 1\n"
	     "GtkButton::child-displacement-y = 1\n"
691
	     "}widget_class \"*Button*\" style \"msw-button\"\n",
cinamod's avatar
cinamod committed
692 693 694 695 696
	     btn_face.red, btn_face.green, btn_face.blue,
	     btn_face.red, btn_face.green, btn_face.blue,
	     btn_face.red, btn_face.green, btn_face.blue,
	     btn_fore.red, btn_fore.green, btn_fore.blue
	     );
Raymond Penners's avatar
Raymond Penners committed
697
  gtk_rc_parse_string(buf);
698

Raymond Penners's avatar
Raymond Penners committed
699
  /* enable coloring for progress bars */
cinamod's avatar
cinamod committed
700
  g_snprintf(buf, sizeof (buf),
701
	     "style \"msw-progress\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
702 703
	     "{bg[PRELIGHT] = { %d, %d, %d }\n"
	     "bg[NORMAL] = { %d, %d, %d }\n"
704
	     "}widget_class \"*Progress*\" style \"msw-progress\"\n",
cinamod's avatar
cinamod committed
705 706 707 708
	     progress_back.red,
	     progress_back.green,
	     progress_back.blue,
	     btn_face.red, btn_face.green, btn_face.blue);
Raymond Penners's avatar
Raymond Penners committed
709
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
710

711
  /* scrollbar thumb width and height */
cinamod's avatar
cinamod committed
712
  g_snprintf(buf, sizeof (buf),
713
	     "style \"msw-vscrollbar\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
714 715 716 717
	     "{GtkRange::slider-width = %d\n"
	     "GtkRange::stepper-size = %d\n"
	     "GtkRange::stepper-spacing = 0\n"
	     "GtkRange::trough_border = 0\n"
718
	     "}widget_class \"*VScrollbar*\" style \"msw-vscrollbar\"\n",
cinamod's avatar
cinamod committed
719 720
	     GetSystemMetrics(SM_CYVTHUMB),
	     get_system_metric(XP_THEME_CLASS_SCROLLBAR, SM_CXVSCROLL));
721
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
722

cinamod's avatar
cinamod committed
723
  g_snprintf(buf, sizeof (buf),
724
	     "style \"msw-hscrollbar\" = \"msw-default\"\n"
cinamod's avatar
cinamod committed
725 726 727 728
	     "{GtkRange::slider-width = %d\n"
	     "GtkRange::stepper-size = %d\n"
	     "GtkRange::stepper-spacing = 0\n"
	     "GtkRange::trough_border = 0\n"
729
	     "}widget_class \"*HScrollbar*\" style \"msw-hscrollbar\"\n",
cinamod's avatar
cinamod committed
730 731
	     GetSystemMetrics(SM_CXHTHUMB),
	     get_system_metric(XP_THEME_CLASS_SCROLLBAR, SM_CYHSCROLL));
732
  gtk_rc_parse_string(buf);
cinamod's avatar
cinamod committed
733

734
  /* radio/check button sizes */
cinamod's avatar
cinamod committed
735
  g_snprintf(buf, sizeof (buf),
736
	     "style \"msw-checkbutton\" = \"msw-button\"\n"
cinamod's avatar
cinamod committed
737
	     "{GtkCheckButton::indicator-size = 13\n"
738 739
	     "}widget_class \"*CheckButton*\" style \"msw-checkbutton\"\n"
	     "widget_class \"*RadioButton*\" style \"msw-checkbutton\"\n");
740
  gtk_rc_parse_string(buf);
Raymond Penners's avatar
Raymond Penners committed
741
}
742

cinamod's avatar
cinamod committed
743 744 745 746 747 748
static void
setup_system_styles(GtkStyle *style)
{
  int i;

  /* Default background */
cinamod's avatar
cinamod committed
749
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_NORMAL]);
cinamod's avatar
cinamod committed
750
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHT, &style->bg[GTK_STATE_SELECTED]);
cinamod's avatar
cinamod committed
751 752 753
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_INSENSITIVE]);
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_ACTIVE]);
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_PRELIGHT]);
cinamod's avatar
cinamod committed
754 755

  /* Default base */
cinamod's avatar
cinamod committed
756
  sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOW,    &style->base[GTK_STATE_NORMAL]);
cinamod's avatar
cinamod committed
757
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHT, &style->base[GTK_STATE_SELECTED]);
cinamod's avatar
cinamod committed
758 759 760
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->base[GTK_STATE_INSENSITIVE]);
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->base[GTK_STATE_ACTIVE]);
  sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOW,    &style->base[GTK_STATE_PRELIGHT]);
cinamod's avatar
cinamod committed
761 762

  /* Default text */
cinamod's avatar
cinamod committed
763
  sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOWTEXT,    &style->text[GTK_STATE_NORMAL]);
cinamod's avatar
cinamod committed
764
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHTTEXT, &style->text[GTK_STATE_SELECTED]);
cinamod's avatar
cinamod committed
765 766 767 768 769 770
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_GRAYTEXT,      &style->text[GTK_STATE_INSENSITIVE]);
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNTEXT,       &style->text[GTK_STATE_ACTIVE]);
  sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOWTEXT,    &style->text[GTK_STATE_PRELIGHT]);

  /* Default forgeground */
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNTEXT,       &style->fg[GTK_STATE_NORMAL]);
cinamod's avatar
cinamod committed
771 772
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHTTEXT, &style->fg[GTK_STATE_SELECTED]);
  sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_GRAYTEXT,      &style->fg[GTK_STATE_INSENSITIVE]);
cinamod's avatar
cinamod committed
773
  sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNTEXT,       &style->fg[GTK_STATE_ACTIVE]);
cinamod's avatar
cinamod committed
774
  sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOWTEXT,    &style->fg[GTK_STATE_PRELIGHT]);
cinamod's avatar
cinamod committed
775 776 777

  for (i = 0; i < 5; i++)
    {
cinamod's avatar
cinamod committed
778 779
      sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_3DSHADOW, &style->dark[i]);
      sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_3DHILIGHT, &style->light[i]);
cinamod's avatar
cinamod committed
780 781 782 783 784 785 786 787 788 789 790

      style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
      style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
      style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;

      style->text_aa[i].red = (style->text[i].red + style->base[i].red) / 2;
      style->text_aa[i].green = (style->text[i].green + style->base[i].green) / 2;
      style->text_aa[i].blue = (style->text[i].blue + style->base[i].blue) / 2;
    }
}

Raymond Penners's avatar
Raymond Penners committed
791
static gboolean
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
sanitize_size (GdkWindow *window,
	       gint      *width,
	       gint      *height)
{
  gboolean set_bg = FALSE;

  if ((*width == -1) && (*height == -1))
    {
      set_bg = GDK_IS_WINDOW (window);
      gdk_window_get_size (window, width, height);
    }
  else if (*width == -1)
    gdk_window_get_size (window, width, NULL);
  else if (*height == -1)
    gdk_window_get_size (window, NULL, height);

  return set_bg;
}

Raymond Penners's avatar
Raymond Penners committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831
static XpThemeElement
map_gtk_progress_bar_to_xp(GtkProgressBar *progress_bar, gboolean trough)
{
  XpThemeElement ret;
  switch (progress_bar->orientation)
    {
    case GTK_PROGRESS_LEFT_TO_RIGHT:
    case GTK_PROGRESS_RIGHT_TO_LEFT:
      ret = trough
        ? XP_THEME_ELEMENT_PROGRESS_TROUGH_H
        : XP_THEME_ELEMENT_PROGRESS_BAR_H;
      break;
    default:
      ret = trough
        ? XP_THEME_ELEMENT_PROGRESS_TROUGH_V
        : XP_THEME_ELEMENT_PROGRESS_BAR_V;
      break;
    }
  return ret;
}

832 833 834 835 836 837 838 839 840 841
static void
draw_part (GdkDrawable  *drawable,
	   GdkGC        *gc,
	   GdkRectangle *area,
	   gint          x,
	   gint          y,
	   Part          part)
{
  if (area)
    gdk_gc_set_clip_rectangle (gc, area);
Raymond Penners's avatar
Raymond Penners committed
842

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
  if (!parts[part].bmap)
      parts[part].bmap = gdk_bitmap_create_from_data (drawable,
						      parts[part].bits,
						      PART_SIZE, PART_SIZE);

  gdk_gc_set_ts_origin (gc, x, y);
  gdk_gc_set_stipple (gc, parts[part].bmap);
  gdk_gc_set_fill (gc, GDK_STIPPLED);

  gdk_draw_rectangle (drawable, gc, TRUE, x, y, PART_SIZE, PART_SIZE);

  gdk_gc_set_fill (gc, GDK_SOLID);

  if (area)
    gdk_gc_set_clip_rectangle (gc, NULL);
}

static void
draw_check(GtkStyle      *style,
	   GdkWindow     *window,
	   GtkStateType   state,
	   GtkShadowType  shadow,
	   GdkRectangle  *area,
	   GtkWidget     *widget,
	   const gchar   *detail,
	   gint           x,
	   gint           y,
	   gint           width,
	   gint           height)
{
  x -= (1 + PART_SIZE - width) / 2;
  y -= (1 + PART_SIZE - height) / 2;
Raymond Penners's avatar
Raymond Penners committed
875

876 877 878 879 880 881 882 883 884 885
  if (detail && strcmp (detail, "check") == 0)	/* Menu item */
    {
      if (shadow == GTK_SHADOW_IN)
	{
	  draw_part (window, style->black_gc, area, x, y, CHECK_TEXT);
	  draw_part (window, style->dark_gc[state], area, x, y, CHECK_AA);
	}
    }
  else
    {
cinamod's avatar
cinamod committed
886 887 888 889
      if (!xp_theme_draw(window, shadow == GTK_SHADOW_IN
			 ? XP_THEME_ELEMENT_PRESSED_CHECKBOX
			 : XP_THEME_ELEMENT_CHECKBOX,
			 style, x, y, width, height, state, area))
890 891 892 893 894 895
        {
          draw_part (window, style->black_gc, area, x, y, CHECK_BLACK);
          draw_part (window, style->dark_gc[state], area, x, y, CHECK_DARK);
          draw_part (window, style->mid_gc[state], area, x, y, CHECK_MID);
          draw_part (window, style->light_gc[state], area, x, y, CHECK_LIGHT);
          draw_part (window, style->base_gc[state], area, x, y, CHECK_BASE);
Raymond Penners's avatar
Raymond Penners committed
896

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
          if (shadow == GTK_SHADOW_IN)
            {
              draw_part (window, style->text_gc[state], area, x, y, CHECK_TEXT);
              draw_part (window, style->text_aa_gc[state], area, x, y, CHECK_AA);
            }
        }
    }
}

static void
draw_expander(GtkStyle      *style,
	      GdkWindow     *window,
	      GtkStateType   state,
	      GdkRectangle  *area,
	      GtkWidget     *widget,
	      const gchar   *detail,
	      gint           x,
	      gint           y,
	      GtkExpanderStyle expander_style)
{
  gint expander_size;
  gint expander_semi_size;
  GdkColor color;
  GdkGCValues values;
  gboolean success;
Raymond Penners's avatar
Raymond Penners committed
922
  XpThemeElement xp_expander;
923 924 925

  gtk_widget_style_get (widget, "expander_size", &expander_size, NULL);

Raymond Penners's avatar
Raymond Penners committed
926 927 928 929 930 931 932 933 934 935
  switch (expander_style)
    {
    case GTK_EXPANDER_COLLAPSED:
    case GTK_EXPANDER_SEMI_COLLAPSED:
      xp_expander = XP_THEME_ELEMENT_TREEVIEW_EXPANDER_CLOSED;
      break;
    default:
      xp_expander = XP_THEME_ELEMENT_TREEVIEW_EXPANDER_OPENED;
      break;
    }
Raymond Penners's avatar
Raymond Penners committed
936

Raymond Penners's avatar
Raymond Penners committed
937 938
  if ((expander_size % 2) == 0)
    expander_size--;
939 940 941

  if (expander_size > 2)
    expander_size -= 2;
cinamod's avatar
cinamod committed
942

943 944 945 946 947 948 949 950 951
  if (area)
    gdk_gc_set_clip_rectangle (style->fg_gc[state], area);

  expander_semi_size = expander_size / 2;
  x -= expander_semi_size;
  y -= expander_semi_size;

  gdk_gc_get_values (style->fg_gc[state], &values);

Raymond Penners's avatar
0.4.3  
Raymond Penners committed
952 953 954
  if (! xp_theme_draw(window, xp_expander, style,
                      x, y,
                      expander_size, expander_size, state, area))
Raymond Penners's avatar
Raymond Penners committed
955
    {
Raymond Penners's avatar
0.4.3  
Raymond Penners committed
956 957
      /* RGB values to emulate Windows Classic style */
      color.red = color.green = color.blue = 128 << 8;
958

Raymond Penners's avatar
0.4.3  
Raymond Penners committed
959 960
      success = gdk_colormap_alloc_color
        (gtk_widget_get_default_colormap (), &color, FALSE, TRUE);
961

Raymond Penners's avatar
0.4.3  
Raymond Penners committed
962 963
      if (success)
        gdk_gc_set_foreground (style->fg_gc[state], &color);
964

Raymond Penners's avatar
0.4.3  
Raymond Penners committed
965 966 967
      gdk_draw_rectangle
        (window, style->fg_gc[state], FALSE, x, y,
         expander_size - 1, expander_size - 1);
Raymond Penners's avatar
Raymond Penners committed
968

969
      gdk_draw_line
Raymond Penners's avatar
0.4.3  
Raymond Penners committed
970 971 972 973 974 975 976 977 978 979 980 981
        (window, style->fg_gc[state], x + 2, y + expander_semi_size,
         x + expander_size - 2, y + expander_semi_size);

      switch (expander_style)
        {
        case GTK_EXPANDER_COLLAPSED:
        case GTK_EXPANDER_SEMI_COLLAPSED:
          gdk_draw_line
            (window, style->fg_gc[state], x + expander_semi_size, y + 2,
             x + expander_semi_size, y + expander_size - 2);
          break;
        }
cinamod's avatar
cinamod committed
982 983 984

      if (success)
        gdk_gc_set_foreground (style->fg_gc[state], &values.foreground);
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
    }

  if (area)
    gdk_gc_set_clip_rectangle (style->fg_gc[state], NULL);
}

static void
draw_option(GtkStyle      *style,
	    GdkWindow     *window,
	    GtkStateType   state,
	    GtkShadowType  shadow,
	    GdkRectangle  *area,
	    GtkWidget     *widget,
	    const gchar   *detail,
	    gint           x,
	    gint           y,
	    gint           width,
	    gint           height)
{
  x -= (1 + PART_SIZE - width) / 2;
  y -= (1 + PART_SIZE - height) / 2;
Raymond Penners's avatar
Raymond Penners committed
1006

1007 1008 1009 1010 1011 1012 1013
  if (detail && strcmp (detail, "option") == 0)	/* Menu item */
    {
      if (shadow == GTK_SHADOW_IN)
	draw_part (window, style->fg_gc[state], area, x, y, RADIO_TEXT);
    }
  else
    {
1014 1015 1016 1017
      if (xp_theme_draw (window, shadow == GTK_SHADOW_IN
                        ? XP_THEME_ELEMENT_PRESSED_RADIO_BUTTON
                        : XP_THEME_ELEMENT_RADIO_BUTTON,
                        style, x, y, width, height, state, area))
1018 1019 1020 1021 1022 1023 1024 1025 1026
        {
        }
      else
	{
	  draw_part (window, style->black_gc, area, x, y, RADIO_BLACK);
	  draw_part (window, style->dark_gc[state], area, x, y, RADIO_DARK);
	  draw_part (window, style->mid_gc[state], area, x, y, RADIO_MID);
	  draw_part (window, style->light_gc[state], area, x, y, RADIO_LIGHT);
	  draw_part (window, style->base_gc[state], area, x, y, RADIO_BASE);
Raymond Penners's avatar
Raymond Penners committed
1027

1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	  if (shadow == GTK_SHADOW_IN)
	    draw_part (window, style->text_gc[state], area, x, y, RADIO_TEXT);
	}
    }
}

static void
draw_varrow (GdkWindow     *window,
	     GdkGC         *gc,
	     GtkShadowType  shadow_type,
	     GdkRectangle  *area,
	     GtkArrowType   arrow_type,
	     gint           x,
	     gint           y,
	     gint           width,
	     gint           height)
{
  gint steps, extra;
  gint y_start, y_increment;
  gint i;

  if (area)
    gdk_gc_set_clip_rectangle (gc, area);
Raymond Penners's avatar
Raymond Penners committed
1051

1052
  width = width + width % 2 - 1;	/* Force odd */
Raymond Penners's avatar
Raymond Penners committed
1053

1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  steps = 1 + width / 2;

  extra = height - steps;

  if (arrow_type == GTK_ARROW_DOWN)
    {
      y_start = y;
      y_increment = 1;
    }
  else
    {
      y_start = y + height - 1;
      y_increment = -1;
    }

  for (i = extra; i < height; i++)
    {
      gdk_draw_line (window, gc,
		     x + (i - extra),              y_start + i * y_increment,
		     x + width - (i - extra) - 1,  y_start + i * y_increment);
    }
Raymond Penners's avatar
Raymond Penners committed
1075

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
  if (area)
    gdk_gc_set_clip_rectangle (gc, NULL);
}

static void
draw_harrow (GdkWindow     *window,
	     GdkGC         *gc,
	     GtkShadowType  shadow_type,
	     GdkRectangle  *area,
	     GtkArrowType   arrow_type,
	     gint           x,
	     gint           y,
	     gint           width,
	     gint           height)
{
  gint steps, extra;
  gint x_start, x_increment;
  gint i;

  if (area)
    gdk_gc_set_clip_rectangle (gc, area);
Raymond Penners's avatar
Raymond Penners committed
1097

1098
  height = height + height % 2 - 1;	/* Force odd */
Raymond Penners's avatar
Raymond Penners committed
1099

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
  steps = 1 + height / 2;

  extra = width - steps;

  if (arrow_type == GTK_ARROW_RIGHT)
    {
      x_start = x;
      x_increment = 1;
    }
  else
    {
      x_start = x + width - 1;
      x_increment = -1;
    }

  for (i = extra; i < width; i++)
    {
      gdk_draw_line (window, gc,
		     x_start + i * x_increment, y + (i - extra),
		     x_start + i * x_increment, y + height - (i - extra) - 1);
    }
Raymond Penners's avatar
Raymond Penners committed
1121

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

  if (area)
    gdk_gc_set_clip_rectangle (gc, NULL);
}

/* This function makes up for some brokeness in gtkrange.c
 * where we never get the full arrow of the stepper button
 * and the type of button in a single drawing function.
 *
 * It doesn't work correctly when the scrollbar is squished
 * to the point we don't have room for full-sized steppers.
 */
static void
reverse_engineer_stepper_box (GtkWidget    *range,
			      GtkArrowType  arrow_type,
			      gint         *x,
			      gint         *y,
			      gint         *width,
			      gint         *height)
{
  gint slider_width = 14, stepper_size = 14;
  gint box_width;
  gint box_height;
Raymond Penners's avatar
Raymond Penners committed
1145

1146 1147 1148 1149 1150 1151 1152
  if (range)
    {
      gtk_widget_style_get (range,
			    "slider_width", &slider_width,
			    "stepper_size", &stepper_size,
			    NULL);
    }
Raymond Penners's avatar
Raymond Penners committed
1153

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
  if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
    {
      box_width = slider_width;
      box_height = stepper_size;
    }
  else
    {
      box_width = stepper_size;
      box_height = slider_width;
    }

  *x = *x - (box_width - *width) / 2;
  *y = *y - (box_height - *height) / 2;
  *width = box_width;
  *height = box_height;
}

static void
draw_arrow (GtkStyle      *style,
	    GdkWindow     *window,
	    GtkStateType   state,
	    GtkShadowType  shadow,
	    GdkRectangle  *area,
	    GtkWidget     *widget,
	    const gchar   *detail,
	    GtkArrowType   arrow_type,
	    gboolean       fill,
	    gint           x,
	    gint           y,
	    gint           width,
	    gint           height)
{
cinamod's avatar
cinamod committed
1186 1187 1188 1189
  const gchar * name;

  name = gtk_widget_get_name (widget);

1190
  sanitize_size (window, &width, &height);
Raymond Penners's avatar
Raymond Penners committed
1191

1192 1193
  if (detail && strcmp (detail, "spinbutton") == 0)
    {
Raymond Penners's avatar
Raymond Penners committed
1194
      if (xp_theme_is_drawable(XP_THEME_ELEMENT_SPIN_BUTTON_UP))
1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
        {
          return;
        }
      else
        {
          x += (width - 7) / 2;

          if (arrow_type == GTK_ARROW_UP)
            y += (height - 4) / 2;
          else
            y += (1 + height - 4) / 2;
          draw_varrow (window, style->fg_gc[state], shadow, area, arrow_type,
                       x, y, 7, 4);
        }
    }
  else if (detail && (!strcmp (detail, "vscrollbar")
                      || !strcmp (detail, "hscrollbar")))
    {
      gint box_x = x;
      gint box_y = y;
      gint box_width = width;
      gint box_height = height;
Raymond Penners's avatar
Raymond Penners committed
1217
      XpThemeElement xp_arrow;
1218 1219 1220
      reverse_engineer_stepper_box (widget, arrow_type,
				    &box_x, &box_y, &box_width, &box_height);

Raymond Penners's avatar
Raymond Penners committed
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
      switch (arrow_type)
        {
        case GTK_ARROW_UP:
          xp_arrow = XP_THEME_ELEMENT_ARROW_UP;
          break;
        case GTK_ARROW_DOWN:
          xp_arrow = XP_THEME_ELEMENT_ARROW_DOWN;
          break;
        case GTK_ARROW_LEFT:
          xp_arrow = XP_THEME_ELEMENT_ARROW_LEFT;
          break;
        default:
          xp_arrow = XP_THEME_ELEMENT_ARROW_RIGHT;
          break;
        }
cinamod's avatar
cinamod committed
1236
      if (xp_theme_draw(window, xp_arrow, style, box_x, box_y, box_width, box_height, state, area))
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
        {
        }
      else if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
        {
          x += (width - 7) / 2;
          y += (height - 5) / 2;

          draw_varrow (window, style->fg_gc[state], shadow, area, arrow_type,
                       x, y, 7, 5);
        }
      else
        {
          y += (height - 7) / 2;
          x += (width - 5) / 2;

          draw_harrow (window, style->fg_gc[state], shadow, area, arrow_type,
                       x, y, 5, 7);
        }
    }
  else
    {
1258
      /* draw the toolbar chevrons - waiting for GTK 2.4 */
cinamod's avatar
cinamod committed
1259 1260
	  if (name && !strcmp (name, "gtk-toolbar-arrow"))
	  {
Raymond Penners's avatar
Raymond Penners committed
1261
		  if (xp_theme_draw(window, XP_THEME_ELEMENT_REBAR_CHEVRON, style, x, y, width, height, state, area))
cinamod's avatar
cinamod committed
1262 1263 1264
				return;
	  }

1265 1266 1267 1268
      if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
	{
	  x += (width - 7) / 2;
	  y += (height - 5) / 2;
Raymond Penners's avatar
Raymond Penners committed
1269

1270 1271 1272 1273 1274 1275 1276
	  draw_varrow (window, style->fg_gc[state], shadow, area, arrow_type,
		       x, y, 7, 5);
	}
      else
	{
	  x += (width - 5) / 2;
	  y += (height - 7) / 2;
Raymond Penners's avatar
Raymond Penners committed
1277

1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	  draw_harrow (window, style->fg_gc[state], shadow, area, arrow_type,
		       x, y, 5, 7);
	}
    }
}

static void
option_menu_get_props (GtkWidget      *widget,
		       GtkRequisition *indicator_size,
		       GtkBorder      *indicator_spacing)
{
  GtkRequisition *tmp_size = NULL;
  GtkBorder *tmp_spacing = NULL;
Raymond Penners's avatar
Raymond Penners committed
1291

1292
  if (widget)
Raymond Penners's avatar
Raymond Penners committed
1293
    gtk_widget_style_get (widget,
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
			  "indicator_size", &tmp_size,
			  "indicator_spacing", &tmp_spacing,
			  NULL);

  if (tmp_size)
    {
      *indicator_size = *tmp_size;
      g_free (tmp_size);
    }
  else
    *indicator_size = default_option_indicator_size;

  if (tmp_spacing)
    {
      *indicator_spacing = *tmp_spacing;
      g_free (tmp_spacing);
    }
  else
    *indicator_spacing = default_option_indicator_spacing;
}

1315 1316 1317 1318
static gboolean is_toolbar_child(GtkWidget * wid)
{
	while(wid)
	{
1319
		if(GTK_IS_TOOLBAR(wid) || GTK_IS_HANDLE_BOX(wid))
1320 1321 1322 1323 1324 1325 1326 1327
			return TRUE;
		else
			wid = wid->parent;
	}

	return FALSE;
}

Raymond Penners's avatar
Raymond Penners committed
1328
static void
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
draw_box (GtkStyle      *style,
	  GdkWindow     *window,
	  GtkStateType   state_type,
	  GtkShadowType  shadow_type,
	  GdkRectangle  *area,
	  GtkWidget     *widget,
	  const gchar   *detail,
	  gint           x,
	  gint           y,
	  gint           width,
	  gint           height)
{
  if (detail &&
      (!strcmp (detail, "button") ||
       !strcmp (detail, "buttondefault")))
    {
Raymond Penners's avatar
Raymond Penners committed
1345
      if (GTK_IS_TREE_VIEW (widget->parent) || GTK_IS_CLIST (widget->parent))
1346
        {
Raymond Penners's avatar
Raymond Penners committed
1347
          if (xp_theme_draw(window, XP_THEME_ELEMENT_LIST_HEADER, style, x, y,
cinamod's avatar
cinamod committed
1348
                            width, height, state_type, area))
Raymond Penners's avatar
Raymond Penners committed
1349 1350
            return;
        }
1351
      else if (is_toolbar_child (widget->parent))
cinamod's avatar
cinamod committed
1352 1353 1354 1355 1356
      {
		    if (xp_theme_draw(window, XP_THEME_ELEMENT_TOOLBAR_BUTTON, style, x, y,
		                              width, height, state_type, area))
            	return;
	  }
Raymond Penners's avatar
Raymond Penners committed
1357 1358 1359 1360 1361
      else
        {
          gboolean is_default = !strcmp (detail, "buttondefault");
          if (xp_theme_draw(window, is_default ? XP_THEME_ELEMENT_DEFAULT_BUTTON
                            : XP_THEME_ELEMENT_BUTTON, style, x, y,
cinamod's avatar
cinamod committed
1362
                            width, height, state_type, area))
Raymond Penners's avatar
Raymond Penners committed
1363
            return;
1364 1365 1366 1367
        }
    }
  else if (detail && !strcmp (detail, "spinbutton"))
    {
Raymond Penners's avatar
Raymond Penners committed
1368
      if (xp_theme_is_drawable(XP_THEME_ELEMENT_SPIN_BUTTON_UP))
1369 1370 1371 1372 1373 1374 1375
        {
          return;
        }
    }
  else if (detail && (!strcmp (detail, "spinbutton_up")
                      || !strcmp (detail, "spinbutton_down")))
    {
Raymond Penners's avatar
Raymond Penners committed
1376
      if (xp_theme_draw(window,
Raymond Penners's avatar
Raymond Penners committed
1377 1378 1379
                        (! strcmp (detail, "spinbutton_up"))
                        ? XP_THEME_ELEMENT_SPIN_BUTTON_UP
                        : XP_THEME_ELEMENT_SPIN_BUTTON_DOWN,
cinamod's avatar
cinamod committed
1380
                        style, x, y, width, height, state_type, area))
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
        {
          return;
        }
    }

  else if (detail && !strcmp (detail, "slider"))
    {
      if (GTK_IS_SCROLLBAR(widget))
        {
          GtkScrollbar * scrollbar = GTK_SCROLLBAR(widget);
Raymond Penners's avatar
Raymond Penners committed
1391
          gboolean is_v = GTK_IS_VSCROLLBAR(widget);
Raymond Penners's avatar
Raymond Penners committed
1392
          if (xp_theme_draw(window,
Raymond Penners's avatar
Raymond Penners committed
1393
                            is_v
Raymond Penners's avatar
Raymond Penners committed
1394 1395
                            ? XP_THEME_ELEMENT_SCROLLBAR_V
                            : XP_THEME_ELEMENT_SCROLLBAR_H,
cinamod's avatar
cinamod committed
1396
                            style, x, y, width, height, state_type, area))
1397
            {
cinamod's avatar
cinamod committed
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
	      XpThemeElement gripper = (is_v ? XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_V : XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_H);

	      /* Do not display grippers on tiny scroll bars, the limit imposed
		 is rather arbitrary, perhaps we can fetch the gripper geometry
		 from somewhere and use that... */
	      if ((gripper == XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_H && width < 16)
		  || (gripper == XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_V && height < 16))
		{
		  return;
		}

              xp_theme_draw(window, gripper, style, x, y, width, height, state_type, area);
1410 1411 1412 1413
              return;
            }
        }
    }
Raymond Penners's avatar
Raymond Penners committed
1414 1415 1416 1417 1418 1419 1420
  else if (detail && !strcmp (detail, "bar"))
    {
      if (widget && GTK_IS_PROGRESS_BAR (widget))
        {
          GtkProgressBar *progress_bar = GTK_PROGRESS_BAR(widget);
          XpThemeElement xp_progress_bar = map_gtk_progress_bar_to_xp (progress_bar, FALSE);
          if (xp_theme_draw (window, xp_progress_bar,
cinamod's avatar
cinamod committed
1421
                             style, x, y, width, height, state_type, area))
Raymond Penners's avatar
Raymond Penners committed
1422 1423 1424 1425 1426
            {
              return;
            }
        }
    }
cinamod's avatar
cinamod committed
1427 1428
  else if (detail && strcmp (detail, "menuitem") == 0) {
    shadow_type = GTK_SHADOW_NONE;
Raymond Penners's avatar
Raymond Penners committed
1429
      if (xp_theme_draw (window, XP_THEME_ELEMENT_MENU_ITEM, style, x, y, width, height, state_type, area))
1430 1431 1432 1433
        {
  		return;
        }
  }
cinamod's avatar
cinamod committed
1434
  else if (detail && !strcmp (detail, "trough"))
1435 1436 1437
    {
      if (widget && GTK_IS_PROGRESS_BAR (widget))
        {
Raymond Penners's avatar
Raymond Penners committed
1438 1439 1440
          GtkProgressBar *progress_bar = GTK_PROGRESS_BAR(widget);
          XpThemeElement xp_progress_bar = map_gtk_progress_bar_to_xp (progress_bar, TRUE);
          if (xp_theme_draw (window, xp_progress_bar,
cinamod's avatar
cinamod committed
1441
                             style, x, y, width, height, state_type, area))
Raymond Penners's avatar
Raymond Penners committed
1442 1443 1444 1445 1446
            {
              return;
            }
          else
            {
Raymond Penners's avatar
Raymond Penners committed
1447
              /* Blank in classic Windows */
Raymond Penners's avatar
Raymond Penners committed
1448
            }
1449
        }