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