]> Pileus Git - ~andy/gtk/blob - modules/engines/ms-windows/msw_style.c
Check whether the widget actually is a GtkNotebook before treating it as
[~andy/gtk] / modules / engines / ms-windows / msw_style.c
1 /* MS-Windows Engine (aka GTK-Wimp)
2  *
3  * Copyright (C) 2003, 2004 Raymond Penners <raymond@dotsphinx.com>
4  * Includes code adapted from redmond95 by Owen Taylor, and
5  * gtk-nativewin by Evan Martin
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /*
24  * Useful resources:
25  *
26  *  http://lxr.mozilla.org/mozilla/source/gfx/src/windows/nsNativeThemeWin.cpp
27  *  http://lxr.mozilla.org/seamonkey/source/widget/src/windows/nsLookAndFeel.cpp
28  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/userex/functions/drawthemebackground.asp
29  */
30
31 #include "msw_style.h"
32 #include "xp_theme.h"
33
34 #include <windows.h>
35 #include <math.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include "gtk/gtk.h"
40 #include "gtk/gtk.h"
41 /* #include <gdk/gdkwin32.h> */
42 #include "gdk/win32/gdkwin32.h"
43
44
45 /* Default values, not normally used
46  */
47 static const GtkRequisition default_option_indicator_size = { 9, 8 };
48 static const GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };
49
50 static GtkStyleClass *parent_class;
51
52 typedef enum {
53   CHECK_AA,
54   CHECK_BASE,
55   CHECK_BLACK,
56   CHECK_DARK,
57   CHECK_LIGHT,
58   CHECK_MID,
59   CHECK_TEXT,
60   RADIO_BASE,
61   RADIO_BLACK,
62   RADIO_DARK,
63   RADIO_LIGHT,
64   RADIO_MID,
65   RADIO_TEXT
66 } Part;
67
68 #define PART_SIZE 13
69
70 static const guint8 check_aa_bits[] = {
71  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
72  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
73 static const guint8 check_base_bits[] = {
74  0x00,0x00,0x00,0x00,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
75  0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0x00,0x00,0x00,0x00};
76 static const guint8 check_black_bits[] = {
77  0x00,0x00,0xfe,0x0f,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
78  0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00};
79 static const guint8 check_dark_bits[] = {
80  0xff,0x1f,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
81  0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00};
82 static const guint8 check_light_bits[] = {
83  0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
84  0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0xfe,0x1f};
85 static const guint8 check_mid_bits[] = {
86  0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
87  0x08,0x00,0x08,0x00,0x08,0x00,0x08,0xfc,0x0f,0x00,0x00};
88 static const guint8 check_text_bits[] = {
89  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x03,0x88,0x03,0xd8,0x01,0xf8,
90  0x00,0x70,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
91 static const guint8 radio_base_bits[] = {
92  0x00,0x00,0x00,0x00,0xf0,0x01,0xf8,0x03,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
93  0x07,0xfc,0x07,0xf8,0x03,0xf0,0x01,0x00,0x00,0x00,0x00};
94 static const guint8 radio_black_bits[] = {
95  0x00,0x00,0xf0,0x01,0x0c,0x02,0x04,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
96  0x00,0x02,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
97 static const guint8 radio_dark_bits[] = {
98  0xf0,0x01,0x0c,0x06,0x02,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
99  0x00,0x01,0x00,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x00};
100 static const guint8 radio_light_bits[] = {
101  0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
102  0x10,0x00,0x10,0x00,0x08,0x00,0x08,0x0c,0x06,0xf0,0x01};
103 static const guint8 radio_mid_bits[] = {
104  0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x04,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
105  0x08,0x00,0x08,0x00,0x04,0x0c,0x06,0xf0,0x01,0x00,0x00};
106 static const guint8 radio_text_bits[] = {
107  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0xf0,0x01,0xf0,0x01,0xf0,
108  0x01,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
109
110 static struct {
111   const guint8 *bits;
112   GdkBitmap  *bmap;
113 } parts[] = {
114   { check_aa_bits, NULL },
115   { check_base_bits, NULL },
116   { check_black_bits, NULL },
117   { check_dark_bits, NULL },
118   { check_light_bits, NULL },
119   { check_mid_bits, NULL },
120   { check_text_bits, NULL },
121   { radio_base_bits, NULL },
122   { radio_black_bits, NULL },
123   { radio_dark_bits, NULL },
124   { radio_light_bits, NULL },
125   { radio_mid_bits, NULL },
126   { radio_text_bits, NULL }
127 };
128
129 static gboolean
130 get_system_font(XpThemeClass klazz, XpThemeFont type, LOGFONT *out_lf)
131 {
132 #if 0
133   /* TODO: this causes crashes later because the font name is in UCS2, and the pango fns don't deal with that gracefully */
134   if (xp_theme_get_system_font(klazz, type, out_lf))
135     return TRUE;
136   else
137 #endif
138   {
139           NONCLIENTMETRICS ncm;
140           ncm.cbSize = sizeof(NONCLIENTMETRICS);
141
142           if (SystemParametersInfo(SPI_GETNONCLIENTMETRICS,
143                                    sizeof(NONCLIENTMETRICS), &ncm, 0))
144             {
145               if (type == XP_THEME_FONT_CAPTION)
146                 *out_lf = ncm.lfCaptionFont;
147               else if (type == XP_THEME_FONT_MENU)
148                 *out_lf = ncm.lfMenuFont;
149               else if (type == XP_THEME_FONT_STATUS)
150                 *out_lf = ncm.lfStatusFont;
151               else
152                 *out_lf = ncm.lfMessageFont;
153
154               return TRUE;
155             }
156   }
157   return FALSE;
158 }
159
160 /***************************** BEGIN STOLEN FROM PANGO *****************************/
161
162 /*
163         This code is stolen from Pango 1.4. It attempts to address the following problems:
164
165         http://bugzilla.gnome.org/show_bug.cgi?id=135098
166         http://sourceforge.net/tracker/index.php?func=detail&aid=895762&group_id=76416&atid=547655
167
168         As Owen suggested in bug 135098, once Pango 1.6 is released, we need to get rid of this code.
169 */
170
171 #define PING(printlist)
172
173 /* TrueType defines: */
174
175 #define MAKE_TT_TABLE_NAME(c1, c2, c3, c4) \
176    (((guint32)c4) << 24 | ((guint32)c3) << 16 | ((guint32)c2) << 8 | ((guint32)c1))
177
178 #define CMAP (MAKE_TT_TABLE_NAME('c','m','a','p'))
179 #define CMAP_HEADER_SIZE 4
180
181 #define NAME (MAKE_TT_TABLE_NAME('n','a','m','e'))
182 #define NAME_HEADER_SIZE 6
183
184 #define ENCODING_TABLE_SIZE 8
185
186 #define APPLE_UNICODE_PLATFORM_ID 0
187 #define MACINTOSH_PLATFORM_ID 1
188 #define ISO_PLATFORM_ID 2
189 #define MICROSOFT_PLATFORM_ID 3
190
191 #define SYMBOL_ENCODING_ID 0
192 #define UNICODE_ENCODING_ID 1
193 #define UCS4_ENCODING_ID 10
194
195 struct name_header
196 {
197   guint16 format_selector;
198   guint16 num_records;
199   guint16 string_storage_offset;
200 };
201
202 struct name_record
203 {
204   guint16 platform_id;
205   guint16 encoding_id;
206   guint16 language_id;
207   guint16 name_id;
208   guint16 string_length;
209   guint16 string_offset;
210 };
211
212 static gboolean
213 pango_win32_get_name_header (HDC                 hdc,
214                              struct name_header *header)
215 {
216   if (GetFontData (hdc, NAME, 0, header, sizeof (*header)) != sizeof (*header))
217     return FALSE;
218
219   header->num_records = GUINT16_FROM_BE (header->num_records);
220   header->string_storage_offset = GUINT16_FROM_BE (header->string_storage_offset);
221
222   return TRUE;
223 }
224
225 static gboolean
226 pango_win32_get_name_record (HDC                 hdc,
227                              gint                i,
228                              struct name_record *record)
229 {
230   if (GetFontData (hdc, NAME, 6 + i * sizeof (*record),
231                    record, sizeof (*record)) != sizeof (*record))
232     return FALSE;
233
234   record->platform_id = GUINT16_FROM_BE (record->platform_id);
235   record->encoding_id = GUINT16_FROM_BE (record->encoding_id);
236   record->language_id = GUINT16_FROM_BE (record->language_id);
237   record->name_id = GUINT16_FROM_BE (record->name_id);
238   record->string_length = GUINT16_FROM_BE (record->string_length);
239   record->string_offset = GUINT16_FROM_BE (record->string_offset);
240
241   return TRUE;
242 }
243
244 static gchar *
245 get_family_name (LOGFONT *lfp, HDC pango_win32_hdc)
246 {
247   HFONT hfont;
248   HFONT oldhfont;
249
250   struct name_header header;
251   struct name_record record;
252
253   gint unicode_ix = -1, mac_ix = -1, microsoft_ix = -1;
254   gint name_ix;
255   gchar *codeset;
256
257   gchar *string = NULL;
258   gchar *name;
259
260   size_t i, l, nbytes;
261
262   /* If lfFaceName is ASCII, assume it is the common (English) name
263    * for the font. Is this valid? Do some TrueType fonts have
264    * different names in French, German, etc, and does the system
265    * return these if the locale is set to use French, German, etc?
266    */
267   l = strlen (lfp->lfFaceName);
268   for (i = 0; i < l; i++)
269     if (lfp->lfFaceName[i] < ' ' || lfp->lfFaceName[i] > '~')
270       break;
271
272   if (i == l)
273     return g_strdup (lfp->lfFaceName);
274
275   if ((hfont = CreateFontIndirect (lfp)) == NULL)
276     goto fail0;
277
278   if ((oldhfont = (HFONT)SelectObject (pango_win32_hdc, hfont)) == NULL)
279     goto fail1;
280
281   if (!pango_win32_get_name_header (pango_win32_hdc, &header))
282     goto fail2;
283
284   PING (("%d name records", header.num_records));
285
286   for (i = 0; i < header.num_records; i++)
287     {
288       if (!pango_win32_get_name_record (pango_win32_hdc, i, &record))
289         goto fail2;
290
291       if ((record.name_id != 1 && record.name_id != 16) || record.string_length <= 0)
292         continue;
293
294       PING(("platform:%d encoding:%d language:%04x name_id:%d",
295             record.platform_id, record.encoding_id, record.language_id, record.name_id));
296
297       if (record.platform_id == APPLE_UNICODE_PLATFORM_ID ||
298           record.platform_id == ISO_PLATFORM_ID)
299         unicode_ix = i;
300       else if (record.platform_id == MACINTOSH_PLATFORM_ID &&
301                record.encoding_id == 0 && /* Roman */
302                record.language_id == 0) /* English */
303         mac_ix = i;
304       else if (record.platform_id == MICROSOFT_PLATFORM_ID)
305         if ((microsoft_ix == -1 ||
306              PRIMARYLANGID (record.language_id) == LANG_ENGLISH) &&
307             (record.encoding_id == SYMBOL_ENCODING_ID ||
308              record.encoding_id == UNICODE_ENCODING_ID ||
309              record.encoding_id == UCS4_ENCODING_ID))
310           microsoft_ix = i;
311     }
312
313   if (microsoft_ix >= 0)
314     name_ix = microsoft_ix;
315   else if (mac_ix >= 0)
316     name_ix = mac_ix;
317   else if (unicode_ix >= 0)
318     name_ix = unicode_ix;
319   else
320     goto fail2;
321
322   if (!pango_win32_get_name_record (pango_win32_hdc, name_ix, &record))
323     goto fail2;
324
325   string = g_malloc (record.string_length + 1);
326   if (GetFontData (pango_win32_hdc, NAME,
327                    header.string_storage_offset + record.string_offset,
328                    string, record.string_length) != record.string_length)
329     goto fail2;
330
331   string[record.string_length] = '\0';
332
333   if (name_ix == microsoft_ix)
334     if (record.encoding_id == SYMBOL_ENCODING_ID ||
335         record.encoding_id == UNICODE_ENCODING_ID)
336       codeset = "UTF-16BE";
337     else
338       codeset = "UCS-4BE";
339   else if (name_ix == mac_ix)
340     codeset = "MacRoman";
341   else /* name_ix == unicode_ix */
342     codeset = "UCS-4BE";
343
344   name = g_convert (string, record.string_length, "UTF-8", codeset, NULL, &nbytes, NULL);
345   if (name == NULL)
346     goto fail2;
347   g_free (string);
348
349   PING(("%s", name));
350
351   SelectObject (pango_win32_hdc, oldhfont);
352   DeleteObject (hfont);
353
354   return name;
355
356  fail2:
357   g_free (string);
358   SelectObject (pango_win32_hdc, oldhfont);
359
360  fail1:
361   DeleteObject (hfont);
362
363  fail0:
364   return g_locale_to_utf8 (lfp->lfFaceName, -1, NULL, NULL, NULL);
365 }
366
367 /***************************** END STOLEN FROM PANGO *****************************/
368
369 static char *
370 sys_font_to_pango_font (XpThemeClass klazz, XpThemeFont type, char * buf, size_t bufsiz)
371 {
372   HDC hDC;
373   HWND hwnd;
374   LOGFONT lf;
375   int pt_size;
376   const char * weight;
377   const char * style;
378   char * font;
379
380   if (get_system_font(klazz, type, &lf))
381     {
382       switch (lf.lfWeight) {
383       case FW_THIN:
384       case FW_EXTRALIGHT:
385         weight = "Ultra-Light";
386         break;
387
388       case FW_LIGHT:
389         weight = "Light";
390         break;
391
392       case FW_BOLD:
393         weight = "Bold";
394         break;
395
396       case FW_SEMIBOLD:
397         weight = "Semi-Bold";
398         break;
399
400       case FW_ULTRABOLD:
401         weight = "Ultra-Bold";
402         break;
403
404       case FW_HEAVY:
405         weight = "Heavy";
406         break;
407
408       default:
409         weight = "";
410         break;
411       }
412
413       if (lf.lfItalic)
414         style="Italic";
415       else
416         style="";
417
418         hwnd = GetDesktopWindow();
419         hDC = GetDC(hwnd);
420         if (hDC) {
421                 pt_size = -MulDiv(lf.lfHeight, 72,
422                               GetDeviceCaps(hDC,LOGPIXELSY));
423                 ReleaseDC(hwnd, hDC);
424         } else
425                 pt_size = 10;
426
427         font = get_family_name(&lf, hDC);
428     g_snprintf(buf, bufsiz, "%s %s %s %d", font, style, weight, pt_size);
429         g_free(font);
430
431     return buf;
432    }
433
434   return NULL;
435 }
436
437 /* missing from ms's header files */
438 #ifndef SPI_GETMENUSHOWDELAY
439 #define SPI_GETMENUSHOWDELAY 106
440 #endif
441
442 /* I don't know the proper XP theme class for things like
443    HIGHLIGHTTEXT, so we'll just define it to be "BUTTON"
444    for now */
445 #define XP_THEME_CLASS_TEXT XP_THEME_CLASS_BUTTON
446
447 static void
448 setup_menu_settings (GtkSettings * settings)
449 {
450   int menu_delay;
451   gboolean win95 = FALSE;
452   OSVERSIONINFOEX osvi;
453   GObjectClass * klazz = G_OBJECT_GET_CLASS(G_OBJECT(settings));
454
455   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
456   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
457
458   if (!GetVersionEx ( (OSVERSIONINFO *) &osvi))
459     win95 = TRUE; /* assume the worst */
460
461   if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
462     if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
463       win95 = TRUE;
464
465   if (!win95) {
466     if (SystemParametersInfo (SPI_GETMENUSHOWDELAY, 0, &menu_delay, 0)) {
467       if (klazz) {
468         if (g_object_class_find_property (klazz, "gtk-menu-bar-popup-delay")) {
469           g_object_set (settings, "gtk-menu-bar-popup-delay", 0, NULL);
470         }
471         if (g_object_class_find_property (klazz, "gtk-menu-popup-delay")) {
472           g_object_set (settings, "gtk-menu-popup-delay", menu_delay, NULL);
473         }
474         if (g_object_class_find_property (klazz, "gtk-menu-popdown-delay")) {
475           g_object_set (settings, "gtk-menu-popdown-delay", menu_delay, NULL);
476         }
477       }
478     }
479   }
480 }
481
482 void
483 msw_style_setup_system_settings (void)
484 {
485   GtkSettings * settings;
486   int cursor_blink_time;
487
488   settings = gtk_settings_get_default ();
489   if (!settings)
490     return;
491
492   cursor_blink_time = GetCaretBlinkTime ();
493   g_object_set (settings, "gtk-cursor-blink", cursor_blink_time > 0, NULL);
494
495   if (cursor_blink_time > 0)
496         g_object_set (settings, "gtk-cursor-blink-time", 2*cursor_blink_time,
497                       NULL);
498
499   g_object_set (settings, "gtk-double-click-distance", GetSystemMetrics(SM_CXDOUBLECLK), NULL);
500   g_object_set (settings, "gtk-double-click-time", GetDoubleClickTime(), NULL);
501   g_object_set (settings, "gtk-dnd-drag-threshold", GetSystemMetrics(SM_CXDRAG), NULL);
502
503   setup_menu_settings (settings);
504
505   /*
506      http://developer.gnome.org/doc/API/2.0/gtk/GtkSettings.html
507      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/systemparametersinfo.asp
508      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemmetrics.asp
509   */
510 }
511
512 static void
513 setup_system_font(GtkStyle *style)
514 {
515   char buf[256], * font; /* It's okay, lfFaceName is smaller than 32 chars */
516
517   if ((font = sys_font_to_pango_font(XP_THEME_CLASS_TEXT,
518                                      XP_THEME_FONT_MESSAGE,
519                                      buf, sizeof (buf))) != NULL)
520     {
521       if (style->font_desc)
522         pango_font_description_free (style->font_desc);
523
524       style->font_desc = pango_font_description_from_string(font);
525     }
526 }
527
528 static void
529 sys_color_to_gtk_color(XpThemeClass klazz, int id, GdkColor *pcolor)
530 {
531   DWORD color;
532
533   if (!xp_theme_get_system_color (klazz, id, &color))
534         color = GetSysColor(id);
535
536   pcolor->pixel = color;
537   pcolor->red   = (GetRValue(color) << 8) | GetRValue(color);
538   pcolor->green = (GetGValue(color) << 8) | GetGValue(color);
539   pcolor->blue  = (GetBValue(color) << 8) | GetBValue(color);
540 }
541
542 static int
543 get_system_metric(XpThemeClass klazz, int id)
544 {
545   int rval;
546
547   if (!xp_theme_get_system_metric(klazz, id, &rval))
548     rval = GetSystemMetrics (id);
549
550   return rval;
551 }
552
553 static void
554 setup_msw_rc_style(void)
555 {
556   char buf[1024], font_buf[256], *font_ptr;
557
558   GdkColor menu_color;
559   GdkColor menu_text_color;
560   GdkColor tooltip_back;
561   GdkColor tooltip_fore;
562   GdkColor btn_fore;
563   GdkColor btn_face;
564   GdkColor progress_back;
565
566   GdkColor fg_prelight;
567   GdkColor bg_prelight;
568   GdkColor base_prelight;
569   GdkColor text_prelight;
570
571   gboolean xp_theme = xp_theme_is_active();
572
573   /* Prelight */
574   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT, &fg_prelight);
575   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT, &bg_prelight);
576   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT, &base_prelight);
577   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT, &text_prelight);
578
579   sys_color_to_gtk_color(XP_THEME_CLASS_MENU, COLOR_MENUTEXT, &menu_text_color);
580   sys_color_to_gtk_color(XP_THEME_CLASS_MENU, COLOR_MENU, &menu_color);
581
582   /* tooltips */
583   sys_color_to_gtk_color(XP_THEME_CLASS_TOOLTIP, COLOR_INFOTEXT, &tooltip_fore);
584   sys_color_to_gtk_color(XP_THEME_CLASS_TOOLTIP, COLOR_INFOBK, &tooltip_back);
585
586   /* text on push buttons. TODO: button shadows, backgrounds, and highlights */
587   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_BTNTEXT, &btn_fore);
588   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_BTNFACE, &btn_face);
589
590   /* progress bar background color */
591   sys_color_to_gtk_color(XP_THEME_CLASS_PROGRESS, COLOR_HIGHLIGHT, &progress_back);
592
593   /* Enable coloring for menus. */
594   font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_MENU, XP_THEME_FONT_MENU,font_buf, sizeof (font_buf));
595   g_snprintf(buf, sizeof (buf),
596              "style \"msw-menu\" = \"msw-default\"\n"
597              "{\n"
598              "fg[PRELIGHT] = { %d, %d, %d }\n"
599              "bg[PRELIGHT] = { %d, %d, %d }\n"
600              "text[PRELIGHT] = { %d, %d, %d }\n"
601              "base[PRELIGHT] = { %d, %d, %d }\n"
602              "fg[NORMAL] = { %d, %d, %d }\n"
603              "bg[NORMAL] = { %d, %d, %d }\n"
604              "%s = \"%s\"\n"
605              "}widget_class \"*MenuItem*\" style \"msw-menu\"\n"
606              "widget_class \"*GtkMenu\" style \"msw-menu\"\n"
607              "widget_class \"*GtkMenuShell*\" style \"msw-menu\"\n",
608              fg_prelight.red,
609              fg_prelight.green,
610              fg_prelight.blue,
611              bg_prelight.red,
612              bg_prelight.green,
613              bg_prelight.blue,
614              text_prelight.red,
615              text_prelight.green,
616              text_prelight.blue,
617              base_prelight.red,
618              base_prelight.green,
619              base_prelight.blue,
620              menu_text_color.red,
621              menu_text_color.green,
622              menu_text_color.blue,
623              menu_color.red,
624              menu_color.green,
625              menu_color.blue,
626              (font_ptr ? "font_name" : "#"),
627              (font_ptr ? font_ptr : " font name should go here"));
628   gtk_rc_parse_string(buf);
629
630   /* Enable coloring for menu bars. */
631   g_snprintf(buf, sizeof (buf),
632              "style \"msw-menu-bar\" = \"msw-menu\"\n"
633              "{\n"
634              "bg[NORMAL] = { %d, %d, %d }\n"
635              "GtkMenuBar::shadow-type = %s\n"
636              "}widget_class \"*MenuBar*\" style \"msw-menu-bar\"\n",
637              btn_face.red,
638              btn_face.green,
639              btn_face.blue,
640              (xp_theme ? "etched-in" : "out"));
641   gtk_rc_parse_string(buf);
642
643   g_snprintf(buf, sizeof (buf),
644              "style \"msw-toolbar\" = \"msw-default\"\n"
645              "{\n"
646                  "GtkHandleBox::shadow-type = %s\n"
647                  "GtkToolbar::shadow-type = %s\n"
648              "}widget_class \"*HandleBox*\" style \"msw-toolbar\"\n",
649              (xp_theme ? "none" : "out"),
650              (xp_theme ? "none" : "out"));
651   gtk_rc_parse_string(buf);
652
653   /* enable tooltip fonts */
654   font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_STATUS, XP_THEME_FONT_STATUS,font_buf, sizeof (font_buf));
655   g_snprintf(buf, sizeof (buf),
656              "style \"msw-tooltips-caption\" = \"msw-default\"\n"
657              "{fg[NORMAL] = { %d, %d, %d }\n"
658              "%s = \"%s\"\n"
659              "}widget \"gtk-tooltips.GtkLabel\" style \"msw-tooltips-caption\"\n",
660              tooltip_fore.red,
661              tooltip_fore.green,
662              tooltip_fore.blue,
663              (font_ptr ? "font_name" : "#"),
664              (font_ptr ? font_ptr : " font name should go here"));
665   gtk_rc_parse_string(buf);
666
667   g_snprintf(buf, sizeof (buf),
668              "style \"msw-tooltips\" = \"msw-default\"\n"
669              "{bg[NORMAL] = { %d, %d, %d }\n"
670              "}widget \"gtk-tooltips*\" style \"msw-tooltips\"\n",
671              tooltip_back.red,
672              tooltip_back.green,
673              tooltip_back.blue);
674   gtk_rc_parse_string(buf);
675
676   /* enable font theming for status bars */
677   font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_STATUS, XP_THEME_FONT_STATUS,font_buf, sizeof (font_buf));
678   g_snprintf(buf, sizeof (buf),
679              "style \"msw-status\" = \"msw-default\"\n"
680              "{%s = \"%s\"\n"
681              "bg[NORMAL] = { %d, %d, %d }\n"
682              "}widget_class \"*Status*\" style \"msw-status\"\n",
683              (font_ptr ? "font_name" : "#"),
684              (font_ptr ? font_ptr : " font name should go here"),
685              btn_face.red, btn_face.green, btn_face.blue);
686   gtk_rc_parse_string(buf);
687
688   /* enable coloring for text on buttons
689      TODO: use GetThemeMetric for the border and outside border */
690   g_snprintf(buf, sizeof (buf),
691              "style \"msw-button\" = \"msw-default\"\n"
692              "{\n"
693              "bg[NORMAL] = { %d, %d, %d }\n"
694              "bg[PRELIGHT] = { %d, %d, %d }\n"
695              "bg[INSENSITIVE] = { %d, %d, %d }\n"
696              "fg[PRELIGHT] = { %d, %d, %d }\n"
697              "GtkButton::default-border = { 1, 1, 1, 1 }\n"
698              "GtkButton::default-outside-border = { 0, 0, 0, 0 }\n"
699              "GtkButton::child-displacement-x = 1\n"
700              "GtkButton::child-displacement-y = 1\n"
701              "}widget_class \"*Button*\" style \"msw-button\"\n",
702              btn_face.red, btn_face.green, btn_face.blue,
703              btn_face.red, btn_face.green, btn_face.blue,
704              btn_face.red, btn_face.green, btn_face.blue,
705              btn_fore.red, btn_fore.green, btn_fore.blue
706              );
707   gtk_rc_parse_string(buf);
708
709   /* enable coloring for progress bars */
710   g_snprintf(buf, sizeof (buf),
711              "style \"msw-progress\" = \"msw-default\"\n"
712              "{bg[PRELIGHT] = { %d, %d, %d }\n"
713              "bg[NORMAL] = { %d, %d, %d }\n"
714              "}widget_class \"*Progress*\" style \"msw-progress\"\n",
715              progress_back.red,
716              progress_back.green,
717              progress_back.blue,
718              btn_face.red, btn_face.green, btn_face.blue);
719   gtk_rc_parse_string(buf);
720
721   /* scrollbar thumb width and height */
722   g_snprintf(buf, sizeof (buf),
723              "style \"msw-vscrollbar\" = \"msw-default\"\n"
724              "{GtkRange::slider-width = %d\n"
725              "GtkRange::stepper-size = %d\n"
726              "GtkRange::stepper-spacing = 0\n"
727              "GtkRange::trough_border = 0\n"
728              "GtkScale::slider-length = %d\n"
729              "}widget_class \"*VScrollbar*\" style \"msw-vscrollbar\"\n"
730              "widget_class \"*VScale*\" style \"msw-vscrollbar\"\n",
731              GetSystemMetrics(SM_CYVTHUMB),
732              get_system_metric(XP_THEME_CLASS_SCROLLBAR, SM_CXVSCROLL),
733              11);
734   gtk_rc_parse_string(buf);
735
736   g_snprintf(buf, sizeof (buf),
737              "style \"msw-hscrollbar\" = \"msw-default\"\n"
738              "{GtkRange::slider-width = %d\n"
739              "GtkRange::stepper-size = %d\n"
740              "GtkRange::stepper-spacing = 0\n"
741              "GtkRange::trough_border = 0\n"
742              "GtkScale::slider-length = %d\n"
743              "}widget_class \"*HScrollbar*\" style \"msw-hscrollbar\"\n"
744              "widget_class \"*HScale*\" style \"msw-hscrollbar\"\n",
745              GetSystemMetrics(SM_CXHTHUMB),
746              get_system_metric(XP_THEME_CLASS_SCROLLBAR, SM_CYHSCROLL),
747              11);
748   gtk_rc_parse_string(buf);
749
750   /* radio/check button sizes */
751   g_snprintf(buf, sizeof (buf),
752              "style \"msw-checkbutton\" = \"msw-button\"\n"
753              "{GtkCheckButton::indicator-size = 13\n"
754              "}widget_class \"*CheckButton*\" style \"msw-checkbutton\"\n"
755              "widget_class \"*RadioButton*\" style \"msw-checkbutton\"\n");
756   gtk_rc_parse_string(buf);
757 }
758
759 static void
760 setup_system_styles(GtkStyle *style)
761 {
762   int i;
763
764   /* Default background */
765   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_NORMAL]);
766   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHT, &style->bg[GTK_STATE_SELECTED]);
767   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_INSENSITIVE]);
768   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_ACTIVE]);
769   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->bg[GTK_STATE_PRELIGHT]);
770
771   /* Default base */
772   sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOW,    &style->base[GTK_STATE_NORMAL]);
773   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHT, &style->base[GTK_STATE_SELECTED]);
774   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->base[GTK_STATE_INSENSITIVE]);
775   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNFACE,   &style->base[GTK_STATE_ACTIVE]);
776   sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOW,    &style->base[GTK_STATE_PRELIGHT]);
777
778   /* Default text */
779   sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOWTEXT,    &style->text[GTK_STATE_NORMAL]);
780   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHTTEXT, &style->text[GTK_STATE_SELECTED]);
781   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_GRAYTEXT,      &style->text[GTK_STATE_INSENSITIVE]);
782   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNTEXT,       &style->text[GTK_STATE_ACTIVE]);
783   sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOWTEXT,    &style->text[GTK_STATE_PRELIGHT]);
784
785   /* Default foreground */
786   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNTEXT,       &style->fg[GTK_STATE_NORMAL]);
787   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_HIGHLIGHTTEXT, &style->fg[GTK_STATE_SELECTED]);
788   sys_color_to_gtk_color(XP_THEME_CLASS_TEXT,    COLOR_GRAYTEXT,      &style->fg[GTK_STATE_INSENSITIVE]);
789   sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON,  COLOR_BTNTEXT,       &style->fg[GTK_STATE_ACTIVE]);
790   sys_color_to_gtk_color(XP_THEME_CLASS_WINDOW,  COLOR_WINDOWTEXT,    &style->fg[GTK_STATE_PRELIGHT]);
791
792   for (i = 0; i < 5; i++)
793     {
794       sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_3DSHADOW, &style->dark[i]);
795       sys_color_to_gtk_color(XP_THEME_CLASS_BUTTON, COLOR_3DHILIGHT, &style->light[i]);
796
797       style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
798       style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
799       style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
800
801       style->text_aa[i].red = (style->text[i].red + style->base[i].red) / 2;
802       style->text_aa[i].green = (style->text[i].green + style->base[i].green) / 2;
803       style->text_aa[i].blue = (style->text[i].blue + style->base[i].blue) / 2;
804     }
805 }
806
807 static gboolean
808 sanitize_size (GdkWindow *window,
809                gint      *width,
810                gint      *height)
811 {
812   gboolean set_bg = FALSE;
813
814   if ((*width == -1) && (*height == -1))
815     {
816       set_bg = GDK_IS_WINDOW (window);
817       gdk_drawable_get_size (window, width, height);
818     }
819   else if (*width == -1)
820     gdk_drawable_get_size (window, width, NULL);
821   else if (*height == -1)
822     gdk_drawable_get_size (window, NULL, height);
823
824   return set_bg;
825 }
826
827 static XpThemeElement
828 map_gtk_progress_bar_to_xp(GtkProgressBar *progress_bar, gboolean trough)
829 {
830   XpThemeElement ret;
831   switch (progress_bar->orientation)
832     {
833     case GTK_PROGRESS_LEFT_TO_RIGHT:
834     case GTK_PROGRESS_RIGHT_TO_LEFT:
835       ret = trough
836         ? XP_THEME_ELEMENT_PROGRESS_TROUGH_H
837         : XP_THEME_ELEMENT_PROGRESS_BAR_H;
838       break;
839     default:
840       ret = trough
841         ? XP_THEME_ELEMENT_PROGRESS_TROUGH_V
842         : XP_THEME_ELEMENT_PROGRESS_BAR_V;
843       break;
844     }
845   return ret;
846 }
847
848 static void
849 draw_part (GdkDrawable  *drawable,
850            GdkGC        *gc,
851            GdkRectangle *area,
852            gint          x,
853            gint          y,
854            Part          part)
855 {
856   if (area)
857     gdk_gc_set_clip_rectangle (gc, area);
858
859   if (!parts[part].bmap)
860       parts[part].bmap = gdk_bitmap_create_from_data (drawable,
861                                                       parts[part].bits,
862                                                       PART_SIZE, PART_SIZE);
863
864   gdk_gc_set_ts_origin (gc, x, y);
865   gdk_gc_set_stipple (gc, parts[part].bmap);
866   gdk_gc_set_fill (gc, GDK_STIPPLED);
867
868   gdk_draw_rectangle (drawable, gc, TRUE, x, y, PART_SIZE, PART_SIZE);
869
870   gdk_gc_set_fill (gc, GDK_SOLID);
871
872   if (area)
873     gdk_gc_set_clip_rectangle (gc, NULL);
874 }
875
876 static void
877 draw_check(GtkStyle      *style,
878            GdkWindow     *window,
879            GtkStateType   state,
880            GtkShadowType  shadow,
881            GdkRectangle  *area,
882            GtkWidget     *widget,
883            const gchar   *detail,
884            gint           x,
885            gint           y,
886            gint           width,
887            gint           height)
888 {
889   x -= (1 + PART_SIZE - width) / 2;
890   y -= (1 + PART_SIZE - height) / 2;
891
892   if (detail && strcmp (detail, "check") == 0)  /* Menu item */
893     {
894       if (shadow == GTK_SHADOW_IN)
895         {
896           draw_part (window, style->black_gc, area, x, y, CHECK_TEXT);
897           draw_part (window, style->dark_gc[state], area, x, y, CHECK_AA);
898         }
899     }
900   else
901     {
902       if (!xp_theme_draw(window, shadow == GTK_SHADOW_IN
903                          ? XP_THEME_ELEMENT_PRESSED_CHECKBOX
904                          : XP_THEME_ELEMENT_CHECKBOX,
905                          style, x, y, width, height, state, area))
906         {
907           draw_part (window, style->black_gc, area, x, y, CHECK_BLACK);
908           draw_part (window, style->dark_gc[state], area, x, y, CHECK_DARK);
909           draw_part (window, style->mid_gc[state], area, x, y, CHECK_MID);
910           draw_part (window, style->light_gc[state], area, x, y, CHECK_LIGHT);
911           draw_part (window, style->base_gc[state], area, x, y, CHECK_BASE);
912
913           if (shadow == GTK_SHADOW_IN)
914             {
915               draw_part (window, style->text_gc[state], area, x, y, CHECK_TEXT);
916               draw_part (window, style->text_aa_gc[state], area, x, y, CHECK_AA);
917             }
918         }
919     }
920 }
921
922 static void
923 draw_expander(GtkStyle      *style,
924               GdkWindow     *window,
925               GtkStateType   state,
926               GdkRectangle  *area,
927               GtkWidget     *widget,
928               const gchar   *detail,
929               gint           x,
930               gint           y,
931               GtkExpanderStyle expander_style)
932 {
933   gint expander_size;
934   gint expander_semi_size;
935   GdkColor color;
936   GdkGCValues values;
937   gboolean success;
938   XpThemeElement xp_expander;
939
940   gtk_widget_style_get (widget, "expander_size", &expander_size, NULL);
941
942   switch (expander_style)
943     {
944     case GTK_EXPANDER_COLLAPSED:
945     case GTK_EXPANDER_SEMI_COLLAPSED:
946       xp_expander = XP_THEME_ELEMENT_TREEVIEW_EXPANDER_CLOSED;
947       break;
948     default:
949       xp_expander = XP_THEME_ELEMENT_TREEVIEW_EXPANDER_OPENED;
950       break;
951     }
952
953   if ((expander_size % 2) == 0)
954     expander_size--;
955
956   if (expander_size > 2)
957     expander_size -= 2;
958
959   if (area)
960     gdk_gc_set_clip_rectangle (style->fg_gc[state], area);
961
962   expander_semi_size = expander_size / 2;
963   x -= expander_semi_size;
964   y -= expander_semi_size;
965
966   gdk_gc_get_values (style->fg_gc[state], &values);
967
968   if (! xp_theme_draw(window, xp_expander, style,
969                       x, y,
970                       expander_size, expander_size, state, area))
971     {
972       /* RGB values to emulate Windows Classic style */
973       color.red = color.green = color.blue = 128 << 8;
974
975       success = gdk_colormap_alloc_color
976         (gtk_widget_get_default_colormap (), &color, FALSE, TRUE);
977
978       if (success)
979         gdk_gc_set_foreground (style->fg_gc[state], &color);
980
981       gdk_draw_rectangle
982         (window, style->fg_gc[state], FALSE, x, y,
983          expander_size - 1, expander_size - 1);
984
985       gdk_draw_line
986         (window, style->fg_gc[state], x + 2, y + expander_semi_size,
987          x + expander_size - 2, y + expander_semi_size);
988
989       switch (expander_style)
990         {
991         case GTK_EXPANDER_COLLAPSED:
992         case GTK_EXPANDER_SEMI_COLLAPSED:
993           gdk_draw_line
994             (window, style->fg_gc[state], x + expander_semi_size, y + 2,
995              x + expander_semi_size, y + expander_size - 2);
996           break;
997
998         default:
999           break;
1000         }
1001
1002       if (success)
1003         gdk_gc_set_foreground (style->fg_gc[state], &values.foreground);
1004     }
1005
1006   if (area)
1007     gdk_gc_set_clip_rectangle (style->fg_gc[state], NULL);
1008 }
1009
1010 static void
1011 draw_option(GtkStyle      *style,
1012             GdkWindow     *window,
1013             GtkStateType   state,
1014             GtkShadowType  shadow,
1015             GdkRectangle  *area,
1016             GtkWidget     *widget,
1017             const gchar   *detail,
1018             gint           x,
1019             gint           y,
1020             gint           width,
1021             gint           height)
1022 {
1023   x -= (1 + PART_SIZE - width) / 2;
1024   y -= (1 + PART_SIZE - height) / 2;
1025
1026   if (detail && strcmp (detail, "option") == 0) /* Menu item */
1027     {
1028       if (shadow == GTK_SHADOW_IN)
1029         draw_part (window, style->fg_gc[state], area, x, y, RADIO_TEXT);
1030     }
1031   else
1032     {
1033       if (xp_theme_draw (window, shadow == GTK_SHADOW_IN
1034                         ? XP_THEME_ELEMENT_PRESSED_RADIO_BUTTON
1035                         : XP_THEME_ELEMENT_RADIO_BUTTON,
1036                         style, x, y, width, height, state, area))
1037         {
1038         }
1039       else
1040         {
1041           draw_part (window, style->black_gc, area, x, y, RADIO_BLACK);
1042           draw_part (window, style->dark_gc[state], area, x, y, RADIO_DARK);
1043           draw_part (window, style->mid_gc[state], area, x, y, RADIO_MID);
1044           draw_part (window, style->light_gc[state], area, x, y, RADIO_LIGHT);
1045           draw_part (window, style->base_gc[state], area, x, y, RADIO_BASE);
1046
1047           if (shadow == GTK_SHADOW_IN)
1048             draw_part (window, style->text_gc[state], area, x, y, RADIO_TEXT);
1049         }
1050     }
1051 }
1052
1053 static void
1054 draw_varrow (GdkWindow     *window,
1055              GdkGC         *gc,
1056              GtkShadowType  shadow_type,
1057              GdkRectangle  *area,
1058              GtkArrowType   arrow_type,
1059              gint           x,
1060              gint           y,
1061              gint           width,
1062              gint           height)
1063 {
1064   gint steps, extra;
1065   gint y_start, y_increment;
1066   gint i;
1067
1068   if (area)
1069     gdk_gc_set_clip_rectangle (gc, area);
1070
1071   width = width + width % 2 - 1;        /* Force odd */
1072
1073   steps = 1 + width / 2;
1074
1075   extra = height - steps;
1076
1077   if (arrow_type == GTK_ARROW_DOWN)
1078     {
1079       y_start = y;
1080       y_increment = 1;
1081     }
1082   else
1083     {
1084       y_start = y + height - 1;
1085       y_increment = -1;
1086     }
1087
1088   for (i = extra; i < height; i++)
1089     {
1090       gdk_draw_line (window, gc,
1091                      x + (i - extra),              y_start + i * y_increment,
1092                      x + width - (i - extra) - 1,  y_start + i * y_increment);
1093     }
1094
1095   if (area)
1096     gdk_gc_set_clip_rectangle (gc, NULL);
1097 }
1098
1099 static void
1100 draw_harrow (GdkWindow     *window,
1101              GdkGC         *gc,
1102              GtkShadowType  shadow_type,
1103              GdkRectangle  *area,
1104              GtkArrowType   arrow_type,
1105              gint           x,
1106              gint           y,
1107              gint           width,
1108              gint           height)
1109 {
1110   gint steps, extra;
1111   gint x_start, x_increment;
1112   gint i;
1113
1114   if (area)
1115     gdk_gc_set_clip_rectangle (gc, area);
1116
1117   height = height + height % 2 - 1;     /* Force odd */
1118
1119   steps = 1 + height / 2;
1120
1121   extra = width - steps;
1122
1123   if (arrow_type == GTK_ARROW_RIGHT)
1124     {
1125       x_start = x;
1126       x_increment = 1;
1127     }
1128   else
1129     {
1130       x_start = x + width - 1;
1131       x_increment = -1;
1132     }
1133
1134   for (i = extra; i < width; i++)
1135     {
1136       gdk_draw_line (window, gc,
1137                      x_start + i * x_increment, y + (i - extra),
1138                      x_start + i * x_increment, y + height - (i - extra) - 1);
1139     }
1140
1141
1142   if (area)
1143     gdk_gc_set_clip_rectangle (gc, NULL);
1144 }
1145
1146 /* This function makes up for some brokeness in gtkrange.c
1147  * where we never get the full arrow of the stepper button
1148  * and the type of button in a single drawing function.
1149  *
1150  * It doesn't work correctly when the scrollbar is squished
1151  * to the point we don't have room for full-sized steppers.
1152  */
1153 static void
1154 reverse_engineer_stepper_box (GtkWidget    *range,
1155                               GtkArrowType  arrow_type,
1156                               gint         *x,
1157                               gint         *y,
1158                               gint         *width,
1159                               gint         *height)
1160 {
1161   gint slider_width = 14, stepper_size = 14;
1162   gint box_width;
1163   gint box_height;
1164
1165   if (range)
1166     {
1167       gtk_widget_style_get (range,
1168                             "slider_width", &slider_width,
1169                             "stepper_size", &stepper_size,
1170                             NULL);
1171     }
1172
1173   if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
1174     {
1175       box_width = slider_width;
1176       box_height = stepper_size;
1177     }
1178   else
1179     {
1180       box_width = stepper_size;
1181       box_height = slider_width;
1182     }
1183
1184   *x = *x - (box_width - *width) / 2;
1185   *y = *y - (box_height - *height) / 2;
1186   *width = box_width;
1187   *height = box_height;
1188 }
1189
1190 static XpThemeElement to_xp_arrow(GtkArrowType   arrow_type)
1191 {
1192         XpThemeElement xp_arrow;
1193
1194         switch (arrow_type)
1195         {
1196                 case GTK_ARROW_UP:
1197                         xp_arrow = XP_THEME_ELEMENT_ARROW_UP;
1198                         break;
1199                 case GTK_ARROW_DOWN:
1200                         xp_arrow = XP_THEME_ELEMENT_ARROW_DOWN;
1201                         break;
1202                 case GTK_ARROW_LEFT:
1203                         xp_arrow = XP_THEME_ELEMENT_ARROW_LEFT;
1204                         break;
1205                 default:
1206                         xp_arrow = XP_THEME_ELEMENT_ARROW_RIGHT;
1207                         break;
1208         }
1209
1210         return xp_arrow;
1211 }
1212
1213 static void
1214 draw_arrow (GtkStyle      *style,
1215             GdkWindow     *window,
1216             GtkStateType   state,
1217             GtkShadowType  shadow,
1218             GdkRectangle  *area,
1219             GtkWidget     *widget,
1220             const gchar   *detail,
1221             GtkArrowType   arrow_type,
1222             gboolean       fill,
1223             gint           x,
1224             gint           y,
1225             gint           width,
1226             gint           height)
1227 {
1228   const gchar * name;
1229
1230   name = gtk_widget_get_name (widget);
1231
1232   sanitize_size (window, &width, &height);
1233
1234   if (detail && strcmp (detail, "spinbutton") == 0)
1235     {
1236       if (xp_theme_is_drawable(XP_THEME_ELEMENT_SPIN_BUTTON_UP))
1237         {
1238           return;
1239         }
1240       else
1241         {
1242           x += (width - 7) / 2;
1243
1244           if (arrow_type == GTK_ARROW_UP)
1245             y += (height - 4) / 2;
1246           else
1247             y += (1 + height - 4) / 2;
1248           draw_varrow (window, style->fg_gc[state], shadow, area, arrow_type,
1249                        x, y, 7, 4);
1250         }
1251     }
1252   else if (detail && (!strcmp (detail, "vscrollbar")
1253                       || !strcmp (detail, "hscrollbar")))
1254     {
1255       gboolean is_disabled = FALSE;
1256       GtkScrollbar * scrollbar = GTK_SCROLLBAR(widget);
1257       gint box_x = x;
1258       gint box_y = y;
1259       gint box_width = width;
1260       gint box_height = height;
1261
1262       reverse_engineer_stepper_box (widget, arrow_type,
1263                                     &box_x, &box_y, &box_width, &box_height);
1264
1265       if (scrollbar->range.adjustment->page_size >= (scrollbar->range.adjustment->upper-scrollbar->range.adjustment->lower))
1266         is_disabled = TRUE;
1267
1268       if (xp_theme_draw(window, to_xp_arrow(arrow_type), style, box_x, box_y, box_width, box_height, state, area))
1269         {
1270         }
1271       else if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
1272         {
1273           x += (width - 7) / 2;
1274           y += (height - 5) / 2;
1275
1276           draw_varrow (window, is_disabled ? style->text_aa_gc[state] : style->fg_gc[state], shadow, area, arrow_type,
1277                        x, y, 7, 5);
1278         }
1279       else
1280         {
1281           y += (height - 7) / 2;
1282           x += (width - 5) / 2;
1283
1284           draw_harrow (window, is_disabled ? style->text_aa_gc[state] : style->fg_gc[state], shadow, area, arrow_type,
1285                        x, y, 5, 7);
1286         }
1287     }
1288   else
1289     {
1290       /* draw the toolbar chevrons - waiting for GTK 2.4 */
1291           if (name && !strcmp (name, "gtk-toolbar-arrow"))
1292           {
1293                   if (xp_theme_draw(window, XP_THEME_ELEMENT_REBAR_CHEVRON, style, x, y, width, height, state, area))
1294                                 return;
1295           }
1296           /* probably a gtk combo box on a toolbar */
1297           else if (0 /*widget->parent && GTK_IS_BUTTON (widget->parent)*/)
1298                 {
1299                         if (xp_theme_draw(window, XP_THEME_ELEMENT_COMBOBUTTON, style, x-3, widget->allocation.y+1,
1300                         width+5, widget->allocation.height-4, state, area))
1301                                 return;
1302                 }
1303
1304       if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
1305         {
1306           x += (width - 7) / 2;
1307           y += (height - 5) / 2;
1308
1309           draw_varrow (window, style->fg_gc[state], shadow, area, arrow_type,
1310                        x, y, 7, 5);
1311         }
1312       else
1313         {
1314           x += (width - 5) / 2;
1315           y += (height - 7) / 2;
1316
1317           draw_harrow (window, style->fg_gc[state], shadow, area, arrow_type,
1318                        x, y, 5, 7);
1319         }
1320     }
1321 }
1322
1323 static void
1324 option_menu_get_props (GtkWidget      *widget,
1325                        GtkRequisition *indicator_size,
1326                        GtkBorder      *indicator_spacing)
1327 {
1328   GtkRequisition *tmp_size = NULL;
1329   GtkBorder *tmp_spacing = NULL;
1330
1331   if (widget)
1332     gtk_widget_style_get (widget,
1333                           "indicator_size", &tmp_size,
1334                           "indicator_spacing", &tmp_spacing,
1335                           NULL);
1336
1337   if (tmp_size)
1338     {
1339       *indicator_size = *tmp_size;
1340       g_free (tmp_size);
1341     }
1342   else
1343     *indicator_size = default_option_indicator_size;
1344
1345   if (tmp_spacing)
1346     {
1347       *indicator_spacing = *tmp_spacing;
1348       g_free (tmp_spacing);
1349     }
1350   else
1351     *indicator_spacing = default_option_indicator_spacing;
1352 }
1353
1354 static gboolean is_toolbar_child(GtkWidget * wid)
1355 {
1356         while(wid)
1357         {
1358                 if(GTK_IS_TOOLBAR(wid) || GTK_IS_HANDLE_BOX(wid))
1359                         return TRUE;
1360                 else
1361                         wid = wid->parent;
1362         }
1363
1364         return FALSE;
1365 }
1366
1367 static void
1368 draw_box (GtkStyle      *style,
1369           GdkWindow     *window,
1370           GtkStateType   state_type,
1371           GtkShadowType  shadow_type,
1372           GdkRectangle  *area,
1373           GtkWidget     *widget,
1374           const gchar   *detail,
1375           gint           x,
1376           gint           y,
1377           gint           width,
1378           gint           height)
1379 {
1380   if (detail &&
1381       (!strcmp (detail, "button") ||
1382        !strcmp (detail, "buttondefault")))
1383     {
1384       if (GTK_IS_TREE_VIEW (widget->parent) || GTK_IS_CLIST (widget->parent))
1385         {
1386           if (xp_theme_draw(window, XP_THEME_ELEMENT_LIST_HEADER, style, x, y,
1387                             width, height, state_type, area))
1388             return;
1389         }
1390       else if (is_toolbar_child (widget->parent))
1391       {
1392                     if (xp_theme_draw(window, XP_THEME_ELEMENT_TOOLBAR_BUTTON, style, x, y,
1393                                               width, height, state_type, area))
1394                 return;
1395           }
1396       else
1397         {
1398           gboolean is_default = !strcmp (detail, "buttondefault");
1399           if (xp_theme_draw(window, is_default ? XP_THEME_ELEMENT_DEFAULT_BUTTON
1400                             : XP_THEME_ELEMENT_BUTTON, style, x, y,
1401                             width, height, state_type, area))
1402             return;
1403         }
1404     }
1405   else if (detail && !strcmp (detail, "spinbutton"))
1406     {
1407       if (xp_theme_is_drawable(XP_THEME_ELEMENT_SPIN_BUTTON_UP))
1408         {
1409           return;
1410         }
1411     }
1412   else if (detail && (!strcmp (detail, "spinbutton_up")
1413                       || !strcmp (detail, "spinbutton_down")))
1414     {
1415       if (xp_theme_draw(window,
1416                         (! strcmp (detail, "spinbutton_up"))
1417                         ? XP_THEME_ELEMENT_SPIN_BUTTON_UP
1418                         : XP_THEME_ELEMENT_SPIN_BUTTON_DOWN,
1419                         style, x, y, width, height, state_type, area))
1420         {
1421           return;
1422         }
1423     }
1424   else if (detail && !strcmp (detail, "slider"))
1425     {
1426       if (GTK_IS_SCROLLBAR(widget))
1427         {
1428           GtkScrollbar * scrollbar = GTK_SCROLLBAR(widget);
1429           gboolean is_v = GTK_IS_VSCROLLBAR(widget);
1430
1431           if (xp_theme_draw(window,
1432                             is_v
1433                             ? XP_THEME_ELEMENT_SCROLLBAR_V
1434                             : XP_THEME_ELEMENT_SCROLLBAR_H,
1435                             style, x, y, width, height, state_type, area))
1436             {
1437               XpThemeElement gripper = (is_v ? XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_V : XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_H);
1438
1439               /* Do not display grippers on tiny scroll bars, the limit imposed
1440                  is rather arbitrary, perhaps we can fetch the gripper geometry
1441                  from somewhere and use that... */
1442               if ((gripper == XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_H && width < 16)
1443                   || (gripper == XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_V && height < 16))
1444                 {
1445                   return;
1446                 }
1447
1448               xp_theme_draw(window, gripper, style, x, y, width, height, state_type, area);
1449               return;
1450             }
1451           else
1452           {
1453             if (scrollbar->range.adjustment->page_size >= (scrollbar->range.adjustment->upper-scrollbar->range.adjustment->lower))
1454               return;
1455           }
1456         }
1457     }
1458   else if (detail && !strcmp (detail, "bar"))
1459     {
1460       if (widget && GTK_IS_PROGRESS_BAR (widget))
1461         {
1462           GtkProgressBar *progress_bar = GTK_PROGRESS_BAR(widget);
1463           XpThemeElement xp_progress_bar = map_gtk_progress_bar_to_xp (progress_bar, FALSE);
1464           if (xp_theme_draw (window, xp_progress_bar,
1465                              style, x, y, width, height, state_type, area))
1466             {
1467               return;
1468             }
1469         }
1470     }
1471   else if (detail && strcmp (detail, "menuitem") == 0) {
1472     shadow_type = GTK_SHADOW_NONE;
1473       if (xp_theme_draw (window, XP_THEME_ELEMENT_MENU_ITEM, style, x, y, width, height, state_type, area))
1474         {
1475                 return;
1476         }
1477   }
1478   else if (detail && !strcmp (detail, "trough"))
1479     {
1480       if (widget && GTK_IS_PROGRESS_BAR (widget))
1481         {
1482           GtkProgressBar *progress_bar = GTK_PROGRESS_BAR(widget);
1483           XpThemeElement xp_progress_bar = map_gtk_progress_bar_to_xp (progress_bar, TRUE);
1484           if (xp_theme_draw (window, xp_progress_bar,
1485                              style, x, y, width, height, state_type, area))
1486             {
1487               return;
1488             }
1489           else
1490             {
1491               /* Blank in classic Windows */
1492             }
1493         }
1494       else if (widget && GTK_IS_SCROLLBAR(widget))
1495         {
1496           gboolean is_vertical = GTK_IS_VSCROLLBAR(widget);
1497
1498           if (xp_theme_draw(window,
1499                                is_vertical
1500                                ? XP_THEME_ELEMENT_TROUGH_V
1501                                : XP_THEME_ELEMENT_TROUGH_H,
1502                                style,
1503                                x, y, width, height, state_type, area))
1504             {
1505               return;
1506             }
1507           else
1508             {
1509               GdkGCValues gc_values;
1510               GdkGC *gc;
1511               GdkPixmap *pixmap;
1512
1513               sanitize_size (window, &width, &height);
1514
1515               pixmap = gdk_pixmap_new (window, 2, 2, -1);
1516
1517               gdk_draw_point (pixmap, style->bg_gc[GTK_STATE_NORMAL], 0, 0);
1518               gdk_draw_point (pixmap, style->bg_gc[GTK_STATE_NORMAL], 1, 1);
1519               gdk_draw_point (pixmap, style->light_gc[GTK_STATE_NORMAL], 1, 0);
1520               gdk_draw_point (pixmap, style->light_gc[GTK_STATE_NORMAL], 0, 1);
1521
1522               gc_values.fill = GDK_TILED;
1523               gc_values.tile = pixmap;
1524               gc_values.ts_x_origin = x;
1525               gc_values.ts_y_origin = y;
1526               gc = gdk_gc_new_with_values (window, &gc_values,
1527                                            GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN | GDK_GC_FILL | GDK_GC_TILE);
1528
1529               if (area)
1530                 gdk_gc_set_clip_rectangle (gc, area);
1531
1532               gdk_draw_rectangle (window, gc, TRUE, x, y, width, height);
1533
1534               g_object_unref (gc);
1535               g_object_unref (pixmap);
1536
1537               return;
1538             }
1539         }
1540       else if (widget && GTK_IS_SCALE(widget))
1541         {
1542           gboolean is_vertical = GTK_IS_VSCALE(widget);
1543
1544           if(!xp_theme_is_active ()) {
1545                   parent_class->draw_box (style, window, state_type, GTK_SHADOW_NONE, area,
1546                                   widget, detail, x, y, width, height);
1547           }
1548
1549           if(is_vertical) {
1550             if(xp_theme_draw(window, XP_THEME_ELEMENT_SCALE_TROUGH_V, style, (2 * x + width)/2, y, 2, height, state_type, area))
1551                 return;
1552
1553                 parent_class->draw_box(style, window, state_type, GTK_SHADOW_ETCHED_IN, area, NULL, NULL, (2 * x + width)/2, y, 1, height);
1554       }
1555           else {
1556             if(xp_theme_draw(window, XP_THEME_ELEMENT_SCALE_TROUGH_H, style, x, (2 * y + height)/2, width, 2, state_type, area))
1557                 return;
1558
1559                 parent_class->draw_box(style, window, state_type, GTK_SHADOW_ETCHED_IN, area, NULL, NULL, x, (2 * y + height)/2, width, 1);
1560       }
1561           return;
1562         }
1563     }
1564   else if (detail && strcmp (detail, "optionmenu") == 0)
1565     {
1566       if (xp_theme_draw(window, XP_THEME_ELEMENT_EDIT_TEXT,
1567                         style, x, y, width, height, state_type, area))
1568         {
1569           return;
1570         }
1571     }
1572   else if (detail && (strcmp (detail, "vscrollbar") == 0 || strcmp (detail, "hscrollbar") == 0))
1573   {
1574        GtkScrollbar * scrollbar = GTK_SCROLLBAR(widget);
1575           if (shadow_type == GTK_SHADOW_IN)
1576                 shadow_type = GTK_SHADOW_ETCHED_IN;
1577        if (scrollbar->range.adjustment->page_size >= (scrollbar->range.adjustment->upper-scrollbar->range.adjustment->lower))
1578            shadow_type = GTK_SHADOW_OUT;
1579   }
1580   else if (detail && (strcmp (detail, "handlebox_bin") == 0 || strcmp (detail, "toolbar") == 0 ||
1581                         strcmp (detail, "menubar") == 0))
1582   {
1583       if (xp_theme_draw(window, XP_THEME_ELEMENT_REBAR,
1584                         style, x, y, width, height, state_type, area))
1585         {
1586           return;
1587         }
1588   }
1589   else if (detail &&
1590            (!strcmp(detail, "handlebox"))) /* grip */
1591     {
1592     }
1593   else
1594   {
1595          const gchar * name = gtk_widget_get_name (widget);
1596
1597           if (name && !strcmp (name, "gtk-tooltips")) {
1598          if (xp_theme_draw (window, XP_THEME_ELEMENT_TOOLTIP, style, x, y, width, height, state_type, area))
1599            {
1600                         return;
1601             }
1602                 else {
1603                 HBRUSH brush;
1604                         gint xoff, yoff;
1605                         GdkDrawable *drawable;
1606                         RECT rect;
1607                 HDC hdc;
1608
1609                         if (!GDK_IS_WINDOW(window))
1610                 {
1611                         xoff = 0;
1612                         yoff = 0;
1613                         drawable = window;
1614                 }
1615                         else
1616                         {
1617                         gdk_window_get_internal_paint_info(window, &drawable, &xoff, &yoff);
1618                         }
1619
1620                         rect.left = x - xoff;
1621                         rect.top = y - yoff;
1622                         rect.right = rect.left + width;
1623                         rect.bottom = rect.top + height;
1624
1625                         hdc = gdk_win32_hdc_get(window, style->dark_gc[state_type], 0);
1626                 brush = GetSysColorBrush(COLOR_3DDKSHADOW);
1627                         if (brush)
1628                         FrameRect(hdc, &rect, brush);
1629                 InflateRect(&rect, -1, -1);
1630                 FillRect(hdc, &rect, (HBRUSH) (COLOR_INFOBK+1));
1631
1632                 return;
1633                 }
1634
1635                 }
1636   }
1637
1638   parent_class->draw_box (style, window, state_type, shadow_type, area,
1639                           widget, detail, x, y, width, height);
1640
1641   if (detail && strcmp (detail, "optionmenu") == 0)
1642     {
1643       GtkRequisition indicator_size;
1644       GtkBorder indicator_spacing;
1645       gint vline_x;
1646
1647       option_menu_get_props (widget, &indicator_size, &indicator_spacing);
1648
1649       sanitize_size (window, &width, &height);
1650
1651       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
1652         vline_x = x + indicator_size.width + indicator_spacing.left + indicator_spacing.right;
1653       else
1654         vline_x = x + width - (indicator_size.width + indicator_spacing.left + indicator_spacing.right) - style->xthickness;
1655
1656       parent_class->draw_vline (style, window, state_type, area, widget,
1657                                 detail,
1658                                 y + style->ythickness + 1,
1659                                 y + height - style->ythickness - 3,
1660                                 vline_x);
1661     }
1662 }
1663
1664 static void
1665 draw_tab (GtkStyle      *style,
1666           GdkWindow     *window,
1667           GtkStateType   state,
1668           GtkShadowType  shadow,
1669           GdkRectangle  *area,
1670           GtkWidget     *widget,
1671           const gchar   *detail,
1672           gint           x,
1673           gint           y,
1674           gint           width,
1675           gint           height)
1676 {
1677   GtkRequisition indicator_size;
1678   GtkBorder indicator_spacing;
1679
1680   gint arrow_height;
1681
1682   g_return_if_fail (style != NULL);
1683   g_return_if_fail (window != NULL);
1684
1685   if (detail && ! strcmp (detail, "optionmenutab"))
1686     {
1687       if (xp_theme_draw(window, XP_THEME_ELEMENT_COMBOBUTTON,
1688                         style, x-5, widget->allocation.y+1,
1689                         width+10, widget->allocation.height-2, state, area))
1690         {
1691           return;
1692         }
1693     }
1694
1695   if (widget)
1696     gtk_widget_style_get (widget, "indicator_size", &indicator_size, NULL);
1697
1698   option_menu_get_props (widget, &indicator_size, &indicator_spacing);
1699
1700   x += (width - indicator_size.width) / 2;
1701   arrow_height = (indicator_size.width + 1) / 2;
1702
1703   y += (height - arrow_height) / 2;
1704
1705   draw_varrow (window, style->black_gc, shadow, area, GTK_ARROW_DOWN,
1706                x, y, indicator_size.width, arrow_height);
1707 }
1708
1709 /* this is an undefined magic value that, according to the mozilla folks,
1710    worked for all the various themes that they tried */
1711 #define XP_EDGE_SIZE 2
1712
1713 static void
1714 draw_extension(GtkStyle *style,
1715                GdkWindow *window,
1716                GtkStateType state_type,
1717                GtkShadowType shadow_type,
1718                GdkRectangle *area,
1719                GtkWidget *widget,
1720                const gchar *detail,
1721                gint x,
1722                gint y,
1723                gint width,
1724                gint height,
1725                GtkPositionType gap_side)
1726 {
1727   if (GTK_IS_NOTEBOOK(widget) && detail && !strcmp(detail, "tab"))
1728     {
1729       GtkNotebook *notebook = GTK_NOTEBOOK(widget);
1730       gint x2, y2, w2, h2;
1731
1732       x2 = x; y2 = y; w2 = width; h2 = height;
1733       if (gap_side == GTK_POS_TOP && state_type == GTK_STATE_NORMAL) {
1734           /*h2 += XP_EDGE_SIZE;*/
1735           }
1736       else if (gap_side == GTK_POS_BOTTOM && state_type == GTK_STATE_NORMAL) {
1737           /*h2 += XP_EDGE_SIZE;*/
1738           }
1739       else if (gap_side == GTK_POS_LEFT && state_type == GTK_STATE_NORMAL) {
1740           x2 += 1;
1741           w2 -= XP_EDGE_SIZE;
1742           }
1743       else if (gap_side == GTK_POS_RIGHT && state_type == GTK_STATE_NORMAL) {
1744           w2 -= (XP_EDGE_SIZE + 1);
1745           }
1746
1747       if (xp_theme_draw
1748           (window, gtk_notebook_get_current_page(notebook)==0
1749            ? XP_THEME_ELEMENT_TAB_ITEM_LEFT_EDGE
1750            : XP_THEME_ELEMENT_TAB_ITEM,
1751            style, x2, y2, w2, h2, state_type, area))
1752         {
1753           return;
1754         }
1755     }
1756   parent_class->draw_extension
1757     (style, window, state_type, shadow_type, area, widget, detail,
1758          x, y, width, height, gap_side);
1759 }
1760
1761 static void
1762 draw_box_gap (GtkStyle *style, GdkWindow *window, GtkStateType state_type,
1763               GtkShadowType shadow_type, GdkRectangle *area,
1764               GtkWidget *widget, const gchar *detail, gint x,
1765               gint y, gint width, gint height, GtkPositionType gap_side,
1766               gint gap_x, gint gap_width)
1767 {
1768   if (GTK_IS_NOTEBOOK(widget) && detail && !strcmp(detail, "notebook"))
1769     {
1770       /* FIXME: pos != TOP to be implemented */
1771       if (gap_side == GTK_POS_TOP && xp_theme_draw(window, XP_THEME_ELEMENT_TAB_PANE, style,  x, y, width, height,
1772                         state_type, area))
1773         {
1774           return;
1775         }
1776     }
1777   parent_class->draw_box_gap(style, window, state_type, shadow_type,
1778                              area, widget, detail, x, y, width, height,
1779                              gap_side, gap_x, gap_width);
1780 }
1781
1782 static void
1783 draw_flat_box (GtkStyle *style, GdkWindow *window,
1784                GtkStateType state_type, GtkShadowType shadow_type,
1785                GdkRectangle *area, GtkWidget *widget,
1786                const gchar *detail, gint x, gint y,
1787                gint width, gint height)
1788 {
1789   if (detail && ! strcmp (detail, "checkbutton"))
1790     {
1791       if (state_type == GTK_STATE_PRELIGHT)
1792         {
1793           return;
1794         }
1795     }
1796
1797   parent_class->draw_flat_box(style, window, state_type, shadow_type,
1798                               area, widget, detail, x, y, width, height);
1799 }
1800
1801 static void
1802 draw_shadow (GtkStyle      *style,
1803              GdkWindow     *window,
1804              GtkStateType   state_type,
1805              GtkShadowType  shadow_type,
1806              GdkRectangle  *area,
1807              GtkWidget     *widget,
1808              const gchar   *detail,
1809              gint           x,
1810              gint           y,
1811              gint           width,
1812              gint           height)
1813 {
1814   if(detail && ! strcmp(detail, "entry"))
1815     {
1816       if (xp_theme_draw(window, XP_THEME_ELEMENT_EDIT_TEXT, style,
1817                         x, y, width, height, state_type, area))
1818         {
1819           return;
1820         }
1821     }
1822   parent_class->draw_shadow (style, window, state_type, shadow_type, area, widget,
1823                              detail, x, y, width, height);
1824 }
1825
1826 static void
1827 draw_hline (GtkStyle            *style,
1828             GdkWindow           *window,
1829             GtkStateType         state_type,
1830             GdkRectangle        *area,
1831             GtkWidget           *widget,
1832             const gchar         *detail,
1833             gint                 x1,
1834             gint                 x2,
1835             gint                 y)
1836 {
1837 #if 0
1838   if (detail && !strcmp(detail, "menuitem")) {
1839           if (xp_theme_draw(window, XP_THEME_ELEMENT_MENU_SEPARATOR, style,
1840                         x1, y, x2, style->ythickness, state_type, area)) {
1841                         return;
1842           }
1843   } else if (detail && !strcmp(detail, "toolbar")) {
1844           if (xp_theme_draw(window, XP_THEME_ELEMENT_TOOLBAR_SEPARATOR_H, style,
1845                         x1, y, x2, style->ythickness, state_type, area)) {
1846                         return;
1847           }
1848   }
1849
1850   if(xp_theme_draw(window, XP_THEME_ELEMENT_LINE_H, style, x1, y, x2, style->ythickness, state_type, area))
1851         return;
1852 #endif
1853
1854   parent_class->draw_hline (style, window, state_type, area, widget,
1855                             detail, x1, x2, y);
1856 }
1857
1858 static void
1859 draw_vline (GtkStyle            *style,
1860             GdkWindow           *window,
1861             GtkStateType         state_type,
1862             GdkRectangle        *area,
1863             GtkWidget           *widget,
1864             const gchar         *detail,
1865             gint                 y1,
1866             gint                 y2,
1867             gint                 x)
1868 {
1869 #if 0
1870         if (detail && !strcmp(detail, "toolbar")) {
1871           if (xp_theme_draw(window, XP_THEME_ELEMENT_TOOLBAR_SEPARATOR_V, style,
1872                         x, y1, style->xthickness, y2, state_type, area)) {
1873                         return;
1874           }
1875         }
1876
1877   if(xp_theme_draw(window, XP_THEME_ELEMENT_LINE_V, style, x, y1, style->xthickness, y2, state_type, area))
1878         return;
1879 #endif
1880
1881   parent_class->draw_vline (style, window, state_type, area, widget,
1882                             detail, y1, y2, x);
1883 }
1884
1885 static void
1886 draw_slider (GtkStyle *style,
1887              GdkWindow *window,
1888              GtkStateType state_type,
1889              GtkShadowType shadow_type,
1890              GdkRectangle *area,
1891              GtkWidget *widget,
1892              const gchar *detail,
1893              gint x,
1894              gint y,
1895              gint width,
1896              gint height,
1897              GtkOrientation orientation)
1898 {
1899           if(GTK_IS_SCALE(widget) &&
1900              xp_theme_draw(window, ((orientation ==  GTK_ORIENTATION_VERTICAL) ? XP_THEME_ELEMENT_SCALE_SLIDER_V : XP_THEME_ELEMENT_SCALE_SLIDER_H), style, x, y, width, height, state_type, area)) {
1901                         return;
1902                 }
1903
1904           parent_class->draw_slider (style, window, state_type, shadow_type, area, widget,
1905                                                             detail, x, y, width, height, orientation);
1906 }
1907
1908 static void
1909 draw_resize_grip (GtkStyle      *style,
1910                        GdkWindow     *window,
1911                        GtkStateType   state_type,
1912                        GdkRectangle  *area,
1913                        GtkWidget     *widget,
1914                        const gchar   *detail,
1915                        GdkWindowEdge  edge,
1916                        gint           x,
1917                        gint           y,
1918                        gint           width,
1919                        gint           height)
1920 {
1921         if (detail && !strcmp(detail, "statusbar")) {
1922                 if (xp_theme_draw(window, XP_THEME_ELEMENT_STATUS_GRIPPER, style, x, y, width, height,
1923                            state_type, area))
1924                         return;
1925         }
1926
1927         parent_class->draw_resize_grip (style, window, state_type, area,
1928                                                                         widget, detail, edge, x, y, width, height);
1929 }
1930
1931 static void
1932 draw_handle (GtkStyle        *style,
1933              GdkWindow       *window,
1934              GtkStateType     state_type,
1935              GtkShadowType    shadow_type,
1936              GdkRectangle    *area,
1937              GtkWidget       *widget,
1938              const gchar     *detail,
1939              gint             x,
1940              gint             y,
1941              gint             width,
1942              gint             height,
1943              GtkOrientation   orientation)
1944 {
1945   if (is_toolbar_child (widget))
1946     {
1947       XpThemeElement hndl;
1948
1949       sanitize_size (window, &width, &height);
1950
1951       if (orientation == GTK_ORIENTATION_VERTICAL)
1952         hndl = XP_THEME_ELEMENT_REBAR_GRIPPER_V;
1953       else
1954         hndl = XP_THEME_ELEMENT_REBAR_GRIPPER_H;
1955
1956       if (xp_theme_draw (window, hndl, style, x, y, width, height,
1957                          state_type, area))
1958         {
1959           return;
1960         }
1961         }
1962
1963   if (!GTK_IS_PANED(widget)) {
1964   gint                xthick, ythick;
1965   GdkGC              *light_gc, *dark_gc, *shadow_gc;
1966   GdkRectangle        dest;
1967
1968         sanitize_size (window, &width, &height);
1969
1970   gtk_paint_box(style, window, state_type, shadow_type, area, widget,
1971                 detail, x, y, width, height);
1972
1973   light_gc = style->light_gc[state_type];
1974   dark_gc = style->dark_gc[state_type];
1975   shadow_gc = style->mid_gc[state_type];
1976
1977   xthick = style->xthickness;
1978   ythick = style->ythickness;
1979
1980   dest.x = x + xthick;
1981   dest.y = y + ythick;
1982   dest.width = width - (xthick * 2);
1983   dest.height = height - (ythick * 2);
1984
1985   gdk_gc_set_clip_rectangle(light_gc, &dest);
1986   gdk_gc_set_clip_rectangle(dark_gc, &dest);
1987   gdk_gc_set_clip_rectangle(shadow_gc, &dest);
1988
1989   if (dest.width < dest.height)
1990     {
1991           gdk_draw_line(window, light_gc, dest.x, dest.y, dest.x, dest.height);
1992           gdk_draw_line(window, dark_gc, dest.x + (dest.width / 2), dest.y, dest.x + (dest.width / 2), dest.height);
1993           gdk_draw_line(window, shadow_gc, dest.x + dest.width, dest.y, dest.x + dest.width, dest.height);
1994     }
1995   else
1996     {
1997
1998           gdk_draw_line(window, light_gc, dest.x, dest.y, dest.x + dest.width, dest.y);
1999           gdk_draw_line(window, dark_gc, dest.x, dest.y + (dest.height / 2), dest.x + dest.width, dest.y + (dest.height / 2));
2000           gdk_draw_line(window, shadow_gc, dest.x, dest.y + dest.height, dest.x + dest.width, dest.y + dest.height);
2001     }
2002
2003   gdk_gc_set_clip_rectangle(shadow_gc, NULL);
2004   gdk_gc_set_clip_rectangle(light_gc, NULL);
2005   gdk_gc_set_clip_rectangle(dark_gc, NULL);
2006     }
2007 }
2008
2009 static void
2010 msw_style_init_from_rc (GtkStyle * style, GtkRcStyle * rc_style)
2011 {
2012   setup_system_font (style);
2013   setup_menu_settings (gtk_settings_get_default ());
2014   setup_system_styles (style);
2015   parent_class->init_from_rc(style, rc_style);
2016 }
2017
2018 static void
2019 msw_style_class_init (MswStyleClass *klass)
2020 {
2021   GtkStyleClass *style_class = GTK_STYLE_CLASS (klass);
2022
2023   parent_class = g_type_class_peek_parent (klass);
2024
2025   style_class->init_from_rc = msw_style_init_from_rc;
2026   style_class->draw_arrow = draw_arrow;
2027   style_class->draw_box = draw_box;
2028   style_class->draw_check = draw_check;
2029   style_class->draw_option = draw_option;
2030   style_class->draw_tab = draw_tab;
2031   style_class->draw_flat_box = draw_flat_box;
2032   style_class->draw_expander = draw_expander;
2033   style_class->draw_extension = draw_extension;
2034   style_class->draw_box_gap = draw_box_gap;
2035   style_class->draw_shadow = draw_shadow;
2036   style_class->draw_hline = draw_hline;
2037   style_class->draw_vline = draw_vline;
2038   style_class->draw_handle = draw_handle;
2039   style_class->draw_resize_grip = draw_resize_grip;
2040   style_class->draw_slider = draw_slider;
2041 }
2042
2043 GType msw_type_style = 0;
2044
2045 void
2046 msw_style_register_type (GTypeModule *module)
2047 {
2048   static const GTypeInfo object_info =
2049   {
2050     sizeof (MswStyleClass),
2051     (GBaseInitFunc) NULL,
2052     (GBaseFinalizeFunc) NULL,
2053     (GClassInitFunc) msw_style_class_init,
2054     NULL,           /* class_finalize */
2055     NULL,           /* class_data */
2056     sizeof (MswStyle),
2057     0,              /* n_preallocs */
2058     (GInstanceInitFunc) NULL,
2059   };
2060
2061   msw_type_style = g_type_module_register_type (module,
2062                                                    GTK_TYPE_STYLE,
2063                                                    "MswStyle",
2064                                                    &object_info, 0);
2065 }
2066
2067 void
2068 msw_style_init (void)
2069 {
2070   xp_theme_init ();
2071   msw_style_setup_system_settings ();
2072   setup_msw_rc_style ();
2073 }