]> Pileus Git - ~andy/gtk/blob - modules/engines/ms-windows/msw_style.c
Porting the MS Windows theme engine to cairo - Part 2 rectangle and line drawing
[~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  * Copyright (C) 2006 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
5  * Includes code adapted from redmond95 by Owen Taylor, and
6  * gtk-nativewin by Evan Martin
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Useful resources:
26  *
27  *  http://lxr.mozilla.org/seamonkey/source/widget/src/windows/nsNativeThemeWin.cpp
28  *  http://lxr.mozilla.org/seamonkey/source/widget/src/windows/nsLookAndFeel.cpp
29  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/userex/functions/drawthemebackground.asp
30  *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pantdraw_4b3g.asp
31  */
32
33 /* Include first, else we get redefinition warnings about STRICT */
34 #include "pango/pangowin32.h"
35
36 #include "msw_style.h"
37 #include "xp_theme.h"
38
39 #include <windows.h>
40 #include <math.h>
41 #include <string.h>
42 #include <stdio.h>
43
44 #include "gtk/gtk.h"
45 #include "gtk/gtk.h"
46
47 #ifdef BUILDING_STANDALONE
48 #include "gdk/gdkwin32.h"
49 #else
50 #include "gdk/win32/gdkwin32.h"
51 #endif
52
53
54 /* Default values, not normally used
55  */
56 static const GtkRequisition default_option_indicator_size = { 9, 8 };
57 static const GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };
58
59 static GtkStyleClass *parent_class;
60 static HBRUSH g_dither_brush = NULL;
61
62 static HPEN g_light_pen = NULL;
63 static HPEN g_dark_pen = NULL;
64
65 typedef enum
66 {
67   CHECK_AA,
68   CHECK_BASE,
69   CHECK_BLACK,
70   CHECK_DARK,
71   CHECK_LIGHT,
72   CHECK_MID,
73   CHECK_TEXT,
74   CHECK_INCONSISTENT,
75   RADIO_BASE,
76   RADIO_BLACK,
77   RADIO_DARK,
78   RADIO_LIGHT,
79   RADIO_MID,
80   RADIO_TEXT
81 } Part;
82
83 #define PART_SIZE 13
84
85 static const gchar check_aa_bits[] = {
86   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87   0x00, 0x00, 0x00,
88   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
89 };
90 static const gchar check_base_bits[] = {
91   0x00, 0x00, 0x00, 0x00, 0xfc, 0x07, 0xfc, 0x07, 0xfc, 0x07, 0xfc, 0x07,
92   0xfc, 0x07, 0xfc,
93   0x07, 0xfc, 0x07, 0xfc, 0x07, 0xfc, 0x07, 0x00, 0x00, 0x00, 0x00
94 };
95 static const gchar check_black_bits[] = {
96   0x00, 0x00, 0xfe, 0x0f, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00,
97   0x02, 0x00, 0x02,
98   0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00
99 };
100 static const gchar check_dark_bits[] = {
101   0xff, 0x1f, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
102   0x01, 0x00, 0x01,
103   0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00
104 };
105 static const gchar check_light_bits[] = {
106   0x00, 0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
107   0x00, 0x10, 0x00,
108   0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0xfe, 0x1f
109 };
110 static const gchar check_mid_bits[] = {
111   0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08,
112   0x00, 0x08, 0x00,
113   0x08, 0x00, 0x08, 0x00, 0x08, 0x00, 0x08, 0xfc, 0x0f, 0x00, 0x00
114 };
115 static const gchar check_text_bits[] = {
116   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x88, 0x03,
117   0xd8, 0x01, 0xf8,
118   0x00, 0x70, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
119 };
120 static const gchar check_inconsistent_bits[] = {
121   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
122     0xf0, 0x03, 0xf0,
123   0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
124 };
125 static const gchar radio_base_bits[] = {
126   0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0xf8, 0x03, 0xfc, 0x07, 0xfc, 0x07,
127   0xfc, 0x07, 0xfc,
128   0x07, 0xfc, 0x07, 0xf8, 0x03, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00
129 };
130 static const gchar radio_black_bits[] = {
131   0x00, 0x00, 0xf0, 0x01, 0x0c, 0x02, 0x04, 0x00, 0x02, 0x00, 0x02, 0x00,
132   0x02, 0x00, 0x02,
133   0x00, 0x02, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
134 };
135 static const gchar radio_dark_bits[] = {
136   0xf0, 0x01, 0x0c, 0x06, 0x02, 0x00, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00,
137   0x01, 0x00, 0x01,
138   0x00, 0x01, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00
139 };
140 static const gchar radio_light_bits[] = {
141   0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x00, 0x10, 0x00, 0x10,
142   0x00, 0x10, 0x00,
143   0x10, 0x00, 0x10, 0x00, 0x08, 0x00, 0x08, 0x0c, 0x06, 0xf0, 0x01
144 };
145 static const gchar radio_mid_bits[] = {
146   0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x08, 0x00, 0x08,
147   0x00, 0x08, 0x00,
148   0x08, 0x00, 0x08, 0x00, 0x04, 0x0c, 0x06, 0xf0, 0x01, 0x00, 0x00
149 };
150 static const gchar radio_text_bits[] = {
151   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0xf0, 0x01,
152   0xf0, 0x01, 0xf0,
153   0x01, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
154 };
155
156 static struct
157 {
158   const gchar *bits;
159   GdkBitmap *bmap;
160 } parts[] = {
161   { check_aa_bits, NULL           },
162   { check_base_bits, NULL         },
163   { check_black_bits, NULL        },
164   { check_dark_bits, NULL         },
165   { check_light_bits, NULL        },
166   { check_mid_bits, NULL          },
167   { check_text_bits, NULL         },
168   { check_inconsistent_bits, NULL },
169   { radio_base_bits, NULL         },
170   { radio_black_bits, NULL        },
171   { radio_dark_bits, NULL         },
172   { radio_light_bits, NULL        },
173   { radio_mid_bits, NULL          },
174   { radio_text_bits, NULL         }
175 };
176
177 static void
178 _cairo_draw_line (cairo_t  *cr,
179                   GdkColor *color,
180                   gint      x1,
181                   gint      y1,
182                   gint      x2,
183                   gint      y2)
184 {
185   cairo_save (cr);
186
187   gdk_cairo_set_source_color (cr, color);
188   cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
189   cairo_set_line_width (cr, 1.0);
190
191   cairo_move_to (cr, x1 + 0.5, y1 + 0.5);
192   cairo_line_to (cr, x2 + 0.5, y2 + 0.5);
193   cairo_stroke (cr);
194
195   cairo_restore (cr);
196 }
197
198 static void
199 _cairo_draw_rectangle (cairo_t *cr,
200                        GdkColor *color,
201                        gboolean filled,
202                        gint x,
203                        gint y,
204                        gint width,
205                        gint height)
206 {
207   gdk_cairo_set_source_color (cr, color);
208
209   if (filled)
210     {
211       cairo_rectangle (cr, x, y, width, height);
212       cairo_fill (cr);
213     }
214   else
215     {
216       cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
217       cairo_stroke (cr);
218     }
219 }
220
221 static gboolean
222 get_system_font (XpThemeClass klazz, XpThemeFont type, LOGFONTW *out_lf)
223 {
224   if (xp_theme_get_system_font (klazz, type, out_lf))
225     {
226       return TRUE;
227     }
228   else
229     {
230       /* Use wide char versions here, as the theming functions only support
231        * wide chars versions of the structures. */
232       NONCLIENTMETRICSW ncm;
233
234       ncm.cbSize = sizeof (NONCLIENTMETRICSW);
235
236       if (SystemParametersInfoW (SPI_GETNONCLIENTMETRICS,
237                                 sizeof (NONCLIENTMETRICSW), &ncm, 0))
238         {
239           if (type == XP_THEME_FONT_CAPTION)
240             *out_lf = ncm.lfCaptionFont;
241           else if (type == XP_THEME_FONT_MENU)
242             *out_lf = ncm.lfMenuFont;
243           else if (type == XP_THEME_FONT_STATUS)
244             *out_lf = ncm.lfStatusFont;
245           else
246             *out_lf = ncm.lfMessageFont;
247
248           return TRUE;
249         }
250     }
251
252   return FALSE;
253 }
254
255 static char *
256 sys_font_to_pango_font (XpThemeClass klazz, XpThemeFont type, char *buf,
257                         size_t bufsiz)
258 {
259   LOGFONTW lf;
260
261   if (get_system_font (klazz, type, &lf))
262     {
263       PangoFontDescription *desc = NULL;
264       int pt_size;
265       const char *font;
266
267       desc = pango_win32_font_description_from_logfontw (&lf);
268       if (!desc)
269         return NULL;
270
271       font = pango_font_description_to_string (desc);
272       pt_size = pango_font_description_get_size (desc);
273
274       if (!(font && *font))
275         {
276           pango_font_description_free (desc);
277           return NULL;
278         }
279
280       if (pt_size == 0)
281         {
282           HDC hDC;
283           HWND hwnd;
284
285           hwnd = GetDesktopWindow ();
286           hDC = GetDC (hwnd);
287
288           if (hDC)
289             pt_size = -MulDiv (lf.lfHeight, 72, GetDeviceCaps (hDC, LOGPIXELSY));
290           else
291             pt_size = 10;
292
293           if (hDC)
294             ReleaseDC (hwnd, hDC);
295
296           g_snprintf (buf, bufsiz, "%s %d", font, pt_size);
297         }
298       else
299         {
300           g_snprintf (buf, bufsiz, "%s", font);
301         }
302
303       if (desc)
304         pango_font_description_free (desc);
305
306       return buf;
307     }
308
309   return NULL;
310 }
311
312 /* missing from ms's header files */
313 #ifndef SPI_GETMENUSHOWDELAY
314 #define SPI_GETMENUSHOWDELAY 106
315 #endif
316
317 /* I don't know the proper XP theme class for things like
318    HIGHLIGHTTEXT, so we'll just define it to be "BUTTON"
319    for now */
320 #define XP_THEME_CLASS_TEXT XP_THEME_CLASS_BUTTON
321
322 #define WIN95_VERSION   0x400
323 #define WIN2K_VERSION   0x500
324 #define WINXP_VERSION   0x501
325 #define WIN2K3_VERSION  0x502
326 #define VISTA_VERSION   0x600
327
328 static gint32
329 get_windows_version ()
330 {
331   static gint32 version = 0;
332   static gboolean have_version = FALSE;
333
334   if (!have_version)
335     {
336       OSVERSIONINFOEX osvi;
337       have_version = TRUE;
338
339       ZeroMemory (&osvi, sizeof (OSVERSIONINFOEX));
340       osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFOEX);
341
342       GetVersionEx((OSVERSIONINFO*) &osvi);
343
344       version = (osvi.dwMajorVersion & 0xff) << 8 | (osvi.dwMinorVersion & 0xff);
345     }
346
347   return version;
348 }
349
350 static void
351 setup_menu_settings (GtkSettings *settings)
352 {
353   int menu_delay;
354   GObjectClass *klazz = G_OBJECT_GET_CLASS (G_OBJECT (settings));
355
356   if (get_windows_version () > WIN95_VERSION)
357     {
358       if (SystemParametersInfo (SPI_GETMENUSHOWDELAY, 0, &menu_delay, 0))
359         {
360           if (klazz)
361             {
362               if (g_object_class_find_property
363                   (klazz, "gtk-menu-bar-popup-delay"))
364                 {
365                   g_object_set (settings,
366                                 "gtk-menu-bar-popup-delay", 0, NULL);
367                 }
368               if (g_object_class_find_property
369                   (klazz, "gtk-menu-popup-delay"))
370                 {
371                   g_object_set (settings,
372                                 "gtk-menu-popup-delay", menu_delay, NULL);
373                 }
374               if (g_object_class_find_property
375                   (klazz, "gtk-menu-popdown-delay"))
376                 {
377                   g_object_set (settings,
378                                 "gtk-menu-popdown-delay", menu_delay, NULL);
379                 }
380             }
381         }
382     }
383 }
384
385 void
386 msw_style_setup_system_settings (void)
387 {
388   GtkSettings *settings;
389   int cursor_blink_time;
390
391   settings = gtk_settings_get_default ();
392   if (!settings)
393     return;
394
395   cursor_blink_time = GetCaretBlinkTime ();
396   g_object_set (settings, "gtk-cursor-blink", cursor_blink_time > 0, NULL);
397
398   if (cursor_blink_time > 0)
399     {
400       g_object_set (settings, "gtk-cursor-blink-time",
401                     2 * cursor_blink_time, NULL);
402     }
403
404   g_object_set (settings, "gtk-double-click-distance",
405                 GetSystemMetrics (SM_CXDOUBLECLK), NULL);
406   g_object_set (settings, "gtk-double-click-time", GetDoubleClickTime (),
407                 NULL);
408   g_object_set (settings, "gtk-dnd-drag-threshold",
409                 GetSystemMetrics (SM_CXDRAG), NULL);
410
411   setup_menu_settings (settings);
412
413   /*
414      http://developer.gnome.org/doc/API/2.0/gtk/GtkSettings.html
415      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/systemparametersinfo.asp
416      http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemmetrics.asp */
417 }
418
419 static void
420 setup_system_font (GtkStyle *style)
421 {
422   char buf[256], *font;         /* It's okay, lfFaceName is smaller than 32
423                                    chars */
424
425   if ((font = sys_font_to_pango_font (XP_THEME_CLASS_TEXT,
426                                       XP_THEME_FONT_MESSAGE,
427                                       buf, sizeof (buf))) != NULL)
428     {
429       if (style->font_desc)
430         {
431           pango_font_description_free (style->font_desc);
432         }
433
434       style->font_desc = pango_font_description_from_string (font);
435     }
436 }
437
438 static void
439 sys_color_to_gtk_color (XpThemeClass klazz, int id, GdkColor * pcolor)
440 {
441   DWORD color;
442
443   if (!xp_theme_get_system_color (klazz, id, &color))
444     color = GetSysColor (id);
445
446   pcolor->pixel = color;
447   pcolor->red = (GetRValue (color) << 8) | GetRValue (color);
448   pcolor->green = (GetGValue (color) << 8) | GetGValue (color);
449   pcolor->blue = (GetBValue (color) << 8) | GetBValue (color);
450 }
451
452 static int
453 get_system_metric (XpThemeClass klazz, int id)
454 {
455   int rval;
456
457   if (!xp_theme_get_system_metric (klazz, id, &rval))
458     rval = GetSystemMetrics (id);
459
460   return rval;
461 }
462
463 static void
464 setup_msw_rc_style (void)
465 {
466   char buf[1024], font_buf[256], *font_ptr;
467   char menu_bar_prelight_str[128];
468
469   GdkColor menu_color;
470   GdkColor menu_text_color;
471   GdkColor tooltip_back;
472   GdkColor tooltip_fore;
473   GdkColor btn_fore;
474   GdkColor btn_face;
475   GdkColor progress_back;
476
477   GdkColor fg_prelight;
478   GdkColor bg_prelight;
479   GdkColor base_prelight;
480   GdkColor text_prelight;
481
482   /* Prelight */
483   sys_color_to_gtk_color (get_windows_version () >= VISTA_VERSION ? XP_THEME_CLASS_MENU : XP_THEME_CLASS_TEXT,
484                           get_windows_version () >= VISTA_VERSION ? COLOR_MENUTEXT : COLOR_HIGHLIGHTTEXT,
485                           &fg_prelight);
486   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT, &bg_prelight);
487   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT,
488                           &base_prelight);
489   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT,
490                           &text_prelight);
491
492   sys_color_to_gtk_color (XP_THEME_CLASS_MENU, COLOR_MENUTEXT,
493                           &menu_text_color);
494   sys_color_to_gtk_color (XP_THEME_CLASS_MENU, COLOR_MENU, &menu_color);
495
496   /* tooltips */
497   sys_color_to_gtk_color (XP_THEME_CLASS_TOOLTIP, COLOR_INFOTEXT,
498                           &tooltip_fore);
499   sys_color_to_gtk_color (XP_THEME_CLASS_TOOLTIP, COLOR_INFOBK,
500                           &tooltip_back);
501
502   /* text on push buttons. TODO: button shadows, backgrounds, and
503      highlights */
504   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNTEXT, &btn_fore);
505   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE, &btn_face);
506
507   /* progress bar background color */
508   sys_color_to_gtk_color (XP_THEME_CLASS_PROGRESS, COLOR_HIGHLIGHT,
509                           &progress_back);
510
511   /* Enable coloring for menus. */
512   font_ptr =
513     sys_font_to_pango_font (XP_THEME_CLASS_MENU, XP_THEME_FONT_MENU,
514                             font_buf, sizeof (font_buf));
515   g_snprintf (buf, sizeof (buf),
516               "style \"msw-menu\" = \"msw-default\"\n" "{\n"
517               "GtkMenuItem::toggle-spacing = 8\n"
518               "fg[PRELIGHT] = { %d, %d, %d }\n"
519               "bg[PRELIGHT] = { %d, %d, %d }\n"
520               "text[PRELIGHT] = { %d, %d, %d }\n"
521               "base[PRELIGHT] = { %d, %d, %d }\n"
522               "fg[NORMAL] = { %d, %d, %d }\n"
523               "bg[NORMAL] = { %d, %d, %d }\n" "%s = \"%s\"\n"
524               "}widget_class \"*MenuItem*\" style \"msw-menu\"\n"
525               "widget_class \"*GtkMenu\" style \"msw-menu\"\n"
526               "widget_class \"*GtkMenuShell*\" style \"msw-menu\"\n",
527               fg_prelight.red, fg_prelight.green, fg_prelight.blue,
528               bg_prelight.red, bg_prelight.green, bg_prelight.blue,
529               text_prelight.red, text_prelight.green, text_prelight.blue,
530               base_prelight.red, base_prelight.green, base_prelight.blue,
531               menu_text_color.red, menu_text_color.green,
532               menu_text_color.blue, menu_color.red, menu_color.green,
533               menu_color.blue, (font_ptr ? "font_name" : "#"),
534               (font_ptr ? font_ptr : " font name should go here"));
535   gtk_rc_parse_string (buf);
536
537   if (xp_theme_is_active ())
538     {
539       *menu_bar_prelight_str = '\0';
540     }
541   else
542     {
543       g_snprintf (menu_bar_prelight_str, sizeof (menu_bar_prelight_str),
544                   "fg[PRELIGHT] = { %d, %d, %d }\n",
545                   menu_text_color.red, menu_text_color.green,
546                   menu_text_color.blue);
547     }
548
549   /* Enable coloring for menu bars. */
550   g_snprintf (buf, sizeof (buf),
551               "style \"msw-menu-bar\" = \"msw-menu\"\n"
552               "{\n"
553               "bg[NORMAL] = { %d, %d, %d }\n"
554               "%s" "GtkMenuBar::shadow-type = %d\n"
555               /*
556                  FIXME: This should be enabled once gtk+ support
557                  GtkMenuBar::prelight-item style property.
558                */
559               /* "GtkMenuBar::prelight-item = 1\n" */
560               "}widget_class \"*MenuBar*\" style \"msw-menu-bar\"\n",
561               btn_face.red, btn_face.green, btn_face.blue,
562               menu_bar_prelight_str, xp_theme_is_active ()? 0 : 2);
563   gtk_rc_parse_string (buf);
564
565   g_snprintf (buf, sizeof (buf),
566               "style \"msw-toolbar\" = \"msw-default\"\n"
567               "{\n"
568               "GtkHandleBox::shadow-type = %s\n"
569               "GtkToolbar::shadow-type = %s\n"
570               "}widget_class \"*HandleBox*\" style \"msw-toolbar\"\n",
571               "etched-in", "etched-in");
572   gtk_rc_parse_string (buf);
573
574   /* enable tooltip fonts */
575   font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_STATUS, XP_THEME_FONT_STATUS,
576                                      font_buf, sizeof (font_buf));
577   g_snprintf (buf, sizeof (buf),
578               "style \"msw-tooltips-caption\" = \"msw-default\"\n"
579               "{fg[NORMAL] = { %d, %d, %d }\n" "%s = \"%s\"\n"
580               "}widget \"gtk-tooltips.GtkLabel\" style \"msw-tooltips-caption\"\n"
581               "widget \"gtk-tooltip.GtkLabel\" style \"msw-tooltips-caption\"\n",
582               tooltip_fore.red, tooltip_fore.green, tooltip_fore.blue,
583               (font_ptr ? "font_name" : "#"),
584               (font_ptr ? font_ptr : " font name should go here"));
585   gtk_rc_parse_string (buf);
586
587   g_snprintf (buf, sizeof (buf),
588               "style \"msw-tooltips\" = \"msw-default\"\n"
589               "{bg[NORMAL] = { %d, %d, %d }\n"
590               "}widget \"gtk-tooltips*\" style \"msw-tooltips\"\n"
591               "widget \"gtk-tooltip*\" style \"msw-tooltips\"\n",
592               tooltip_back.red, tooltip_back.green, tooltip_back.blue);
593   gtk_rc_parse_string (buf);
594
595   /* enable font theming for status bars */
596   font_ptr = sys_font_to_pango_font (XP_THEME_CLASS_STATUS, XP_THEME_FONT_STATUS,
597                                      font_buf, sizeof (font_buf));
598   g_snprintf (buf, sizeof (buf),
599               "style \"msw-status\" = \"msw-default\"\n" "{%s = \"%s\"\n"
600               "bg[NORMAL] = { %d, %d, %d }\n"
601               "}widget_class \"*Status*\" style \"msw-status\"\n",
602               (font_ptr ? "font_name" : "#"),
603               (font_ptr ? font_ptr : " font name should go here"),
604               btn_face.red, btn_face.green, btn_face.blue);
605   gtk_rc_parse_string (buf);
606
607   /* enable coloring for text on buttons TODO: use GetThemeMetric for the
608      border and outside border */
609   g_snprintf (buf, sizeof (buf),
610               "style \"msw-button\" = \"msw-default\"\n"
611               "{\n"
612               "bg[NORMAL] = { %d, %d, %d }\n"
613               "bg[PRELIGHT] = { %d, %d, %d }\n"
614               "bg[INSENSITIVE] = { %d, %d, %d }\n"
615               "fg[PRELIGHT] = { %d, %d, %d }\n"
616               "GtkButton::default-border = { 0, 0, 0, 0 }\n"
617               "GtkButton::default-outside-border = { 0, 0, 0, 0 }\n"
618               "GtkButton::child-displacement-x = 1\n"
619               "GtkButton::child-displacement-y = 1\n"
620               "GtkButton::focus-padding = %d\n"
621               "}widget_class \"*Button*\" style \"msw-button\"\n",
622               btn_face.red, btn_face.green, btn_face.blue,
623               btn_face.red, btn_face.green, btn_face.blue,
624               btn_face.red, btn_face.green, btn_face.blue,
625               btn_fore.red, btn_fore.green, btn_fore.blue,
626               xp_theme_is_active ()? 1 : 2);
627   gtk_rc_parse_string (buf);
628
629   /* enable coloring for progress bars */
630   g_snprintf (buf, sizeof (buf),
631               "style \"msw-progress\" = \"msw-default\"\n"
632               "{bg[PRELIGHT] = { %d, %d, %d }\n"
633               "bg[NORMAL] = { %d, %d, %d }\n"
634               "}widget_class \"*Progress*\" style \"msw-progress\"\n",
635               progress_back.red,
636               progress_back.green,
637               progress_back.blue,
638               btn_face.red, btn_face.green, btn_face.blue);
639   gtk_rc_parse_string (buf);
640
641   /* scrollbar thumb width and height */
642   g_snprintf (buf, sizeof (buf),
643               "style \"msw-vscrollbar\" = \"msw-default\"\n"
644               "{GtkRange::slider-width = %d\n"
645               "GtkRange::stepper-size = %d\n"
646               "GtkRange::stepper-spacing = 0\n"
647               "GtkRange::trough_border = 0\n"
648               "GtkScale::slider-length = %d\n"
649               "GtkScrollbar::min-slider-length = 8\n"
650               "}widget_class \"*VScrollbar*\" style \"msw-vscrollbar\"\n"
651               "widget_class \"*VScale*\" style \"msw-vscrollbar\"\n",
652               GetSystemMetrics (SM_CYVTHUMB),
653               get_system_metric (XP_THEME_CLASS_SCROLLBAR, SM_CXVSCROLL), 11);
654   gtk_rc_parse_string (buf);
655
656   g_snprintf (buf, sizeof (buf),
657               "style \"msw-hscrollbar\" = \"msw-default\"\n"
658               "{GtkRange::slider-width = %d\n"
659               "GtkRange::stepper-size = %d\n"
660               "GtkRange::stepper-spacing = 0\n"
661               "GtkRange::trough_border = 0\n"
662               "GtkScale::slider-length = %d\n"
663               "GtkScrollbar::min-slider-length = 8\n"
664               "}widget_class \"*HScrollbar*\" style \"msw-hscrollbar\"\n"
665               "widget_class \"*HScale*\" style \"msw-hscrollbar\"\n",
666               GetSystemMetrics (SM_CXHTHUMB),
667               get_system_metric (XP_THEME_CLASS_SCROLLBAR, SM_CYHSCROLL), 11);
668   gtk_rc_parse_string (buf);
669
670   gtk_rc_parse_string ("style \"msw-scrolled-window\" = \"msw-default\"\n"
671                        "{GtkScrolledWindow::scrollbars-within-bevel = 1}\n"
672                        "class \"GtkScrolledWindow\" style \"msw-scrolled-window\"\n");
673
674   /* radio/check button sizes */
675   g_snprintf (buf, sizeof (buf),
676               "style \"msw-checkbutton\" = \"msw-button\"\n"
677               "{GtkCheckButton::indicator-size = 13\n"
678               "}widget_class \"*CheckButton*\" style \"msw-checkbutton\"\n"
679               "widget_class \"*RadioButton*\" style \"msw-checkbutton\"\n");
680   gtk_rc_parse_string (buf);
681
682   /* size of combo box toggle button */
683   g_snprintf (buf, sizeof (buf),
684               "style \"msw-combobox-button\" = \"msw-default\"\n"
685               "{\n"
686               "xthickness = 0\n"
687               "ythickness = 0\n"
688               "GtkButton::default-border = { 0, 0, 0, 0 }\n"
689               "GtkButton::default-outside-border = { 0, 0, 0, 0 }\n"
690               "GtkButton::child-displacement-x = 0\n"
691               "GtkButton::child-displacement-y = 0\n"
692               "GtkWidget::focus-padding = 0\n"
693               "GtkWidget::focus-line-width = 0\n"
694               "}\n"
695               "widget_class \"*ComboBox*ToggleButton*\" style \"msw-combobox-button\"\n");
696   gtk_rc_parse_string (buf);
697
698   g_snprintf (buf, sizeof (buf),
699               "style \"msw-combobox\" = \"msw-default\"\n"
700               "{\n"
701               "GtkComboBox::shadow-type = in\n"
702               "xthickness = %d\n"
703               "ythickness = %d\n"
704               "}\n"
705               "class \"GtkComboBox\" style \"msw-combobox\"\n",
706         xp_theme_is_active()? 1 : GetSystemMetrics (SM_CXEDGE),
707         xp_theme_is_active()? 1 : GetSystemMetrics (SM_CYEDGE));
708   gtk_rc_parse_string (buf);
709
710   /* size of tree view header */
711   g_snprintf (buf, sizeof (buf),
712               "style \"msw-header-button\" = \"msw-default\"\n"
713               "{\n"
714               "xthickness = 0\n"
715               "ythickness = 0\n"
716               "GtkWidget::draw-border = {0, 0, 0, 0}\n"
717         "GtkButton::default-border = { 0, 0, 0, 0 }\n"
718               "GtkButton::default-outside-border = { 0, 0, 0, 0 }\n"
719               "GtkButton::child-displacement-x = 0\n"
720               "GtkButton::child-displacement-y = 0\n"
721               "GtkWidget::focus-padding = 0\n"
722               "GtkWidget::focus-line-width = 0\n"
723               "}\n"
724               "widget_class \"*TreeView*Button*\" style \"msw-header-button\"\n");
725   gtk_rc_parse_string (buf);
726
727   /* FIXME: This should be enabled once gtk+ support GtkNotebok::prelight-tab */
728   /* enable prelight tab of GtkNotebook */
729   /*
730      g_snprintf (buf, sizeof (buf),
731      "style \"msw-notebook\" = \"msw-default\"\n"
732      "{GtkNotebook::prelight-tab=1\n"
733      "}widget_class \"*Notebook*\" style \"msw-notebook\"\n");
734      gtk_rc_parse_string (buf);
735    */
736
737   /* FIXME: This should be enabled once gtk+ support GtkTreeView::full-row-focus */
738   /*
739      g_snprintf (buf, sizeof (buf),
740      "style \"msw-treeview\" = \"msw-default\"\n"
741      "{GtkTreeView::full-row-focus=0\n"
742      "}widget_class \"*TreeView*\" style \"msw-treeview\"\n");
743      gtk_rc_parse_string (buf);
744    */
745 }
746
747 static void
748 setup_system_styles (GtkStyle *style)
749 {
750   int i;
751
752   /* Default background */
753   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE,
754                           &style->bg[GTK_STATE_NORMAL]);
755   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT,
756                           &style->bg[GTK_STATE_SELECTED]);
757   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE,
758                           &style->bg[GTK_STATE_INSENSITIVE]);
759   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE,
760                           &style->bg[GTK_STATE_ACTIVE]);
761   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE,
762                           &style->bg[GTK_STATE_PRELIGHT]);
763
764   /* Default base */
765   sys_color_to_gtk_color (XP_THEME_CLASS_WINDOW, COLOR_WINDOW,
766                           &style->base[GTK_STATE_NORMAL]);
767   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHT,
768                           &style->base[GTK_STATE_SELECTED]);
769   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE,
770                           &style->base[GTK_STATE_INSENSITIVE]);
771   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNFACE,
772                           &style->base[GTK_STATE_ACTIVE]);
773   sys_color_to_gtk_color (XP_THEME_CLASS_WINDOW, COLOR_WINDOW,
774                           &style->base[GTK_STATE_PRELIGHT]);
775
776   /* Default text */
777   sys_color_to_gtk_color (XP_THEME_CLASS_WINDOW, COLOR_WINDOWTEXT,
778                           &style->text[GTK_STATE_NORMAL]);
779   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT,
780                           &style->text[GTK_STATE_SELECTED]);
781   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_GRAYTEXT,
782                           &style->text[GTK_STATE_INSENSITIVE]);
783   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNTEXT,
784                           &style->text[GTK_STATE_ACTIVE]);
785   sys_color_to_gtk_color (XP_THEME_CLASS_WINDOW, COLOR_WINDOWTEXT,
786                           &style->text[GTK_STATE_PRELIGHT]);
787
788   /* Default foreground */
789   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNTEXT,
790                           &style->fg[GTK_STATE_NORMAL]);
791   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_HIGHLIGHTTEXT,
792                           &style->fg[GTK_STATE_SELECTED]);
793   sys_color_to_gtk_color (XP_THEME_CLASS_TEXT, COLOR_GRAYTEXT,
794                           &style->fg[GTK_STATE_INSENSITIVE]);
795   sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_BTNTEXT,
796                           &style->bg[GTK_STATE_ACTIVE]);
797   sys_color_to_gtk_color (XP_THEME_CLASS_WINDOW, COLOR_WINDOWTEXT,
798                           &style->fg[GTK_STATE_PRELIGHT]);
799
800   for (i = 0; i < 5; i++)
801     {
802       sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_3DSHADOW,
803                               &style->dark[i]);
804       sys_color_to_gtk_color (XP_THEME_CLASS_BUTTON, COLOR_3DHILIGHT,
805                               &style->light[i]);
806
807       style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
808       style->mid[i].green =
809         (style->light[i].green + style->dark[i].green) / 2;
810       style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
811
812       style->text_aa[i].red = (style->text[i].red + style->base[i].red) / 2;
813       style->text_aa[i].green =
814         (style->text[i].green + style->base[i].green) / 2;
815       style->text_aa[i].blue =
816         (style->text[i].blue + style->base[i].blue) / 2;
817     }
818 }
819
820 static gboolean
821 sanitize_size (GdkWindow *window, gint *width, gint *height)
822 {
823   gboolean set_bg = FALSE;
824
825   if ((*width == -1) && (*height == -1))
826     {
827       set_bg = GDK_IS_WINDOW (window);
828       gdk_drawable_get_size (window, width, height);
829     }
830   else if (*width == -1)
831     {
832       gdk_drawable_get_size (window, width, NULL);
833     }
834   else if (*height == -1)
835     {
836       gdk_drawable_get_size (window, NULL, height);
837     }
838
839   return set_bg;
840 }
841
842 static XpThemeElement
843 map_gtk_progress_bar_to_xp (GtkProgressBar *progress_bar, gboolean trough)
844 {
845   XpThemeElement ret;
846
847   switch (gtk_progress_bar_get_orientation (progress_bar))
848     {
849     case GTK_PROGRESS_LEFT_TO_RIGHT:
850     case GTK_PROGRESS_RIGHT_TO_LEFT:
851       ret = trough
852         ? XP_THEME_ELEMENT_PROGRESS_TROUGH_H
853         : XP_THEME_ELEMENT_PROGRESS_BAR_H;
854       break;
855
856     default:
857       ret = trough
858         ? XP_THEME_ELEMENT_PROGRESS_TROUGH_V
859         : XP_THEME_ELEMENT_PROGRESS_BAR_V;
860       break;
861     }
862
863   return ret;
864 }
865
866 static gboolean
867 is_combo_box_child (GtkWidget *w)
868 {
869   GtkWidget *tmp;
870
871   if (w == NULL)
872     return FALSE;
873
874   for (tmp = w->parent; tmp; tmp = tmp->parent)
875     {
876       if (GTK_IS_COMBO_BOX (tmp))
877         return TRUE;
878     }
879
880   return FALSE;
881 }
882
883 /* This function is not needed anymore */
884 /* static gboolean
885 combo_box_draw_arrow (GtkStyle *style,
886                       GdkWindow *window,
887                       GtkStateType state,
888                       GdkRectangle *area, GtkWidget *widget)
889 {
890   if (xp_theme_is_active ())
891     return TRUE;
892
893   if (widget && GTK_IS_TOGGLE_BUTTON (widget->parent))
894     {
895       DWORD border;
896       RECT rect;
897       HDC dc;
898       XpDCInfo dc_info;
899
900       dc = get_window_dc (style, window, state, &dc_info, area->x, area->y, area->width,
901                           area->height, &rect);
902       border = (GTK_TOGGLE_BUTTON (widget->parent)->
903                 active ? DFCS_PUSHED | DFCS_FLAT : 0);
904
905       InflateRect (&rect, 1, 1);
906       DrawFrameControl (dc, &rect, DFC_SCROLL, DFCS_SCROLLDOWN | border);
907
908       release_window_dc (&dc_info);
909
910       return TRUE;
911     }
912
913   return FALSE;
914 }*/
915
916 static void
917 draw_part (GdkDrawable *drawable,
918            GdkGC *gc, GdkRectangle *area, gint x, gint y, Part part)
919 {
920   if (area)
921     gdk_gc_set_clip_rectangle (gc, area);
922
923   if (!parts[part].bmap)
924     {
925       parts[part].bmap = gdk_bitmap_create_from_data (drawable,
926                                                       parts[part].bits,
927                                                       PART_SIZE, PART_SIZE);
928     }
929
930   gdk_gc_set_ts_origin (gc, x, y);
931   gdk_gc_set_stipple (gc, parts[part].bmap);
932   gdk_gc_set_fill (gc, GDK_STIPPLED);
933
934   gdk_draw_rectangle (drawable, gc, TRUE, x, y, PART_SIZE, PART_SIZE);
935
936   gdk_gc_set_fill (gc, GDK_SOLID);
937
938   if (area)
939     gdk_gc_set_clip_rectangle (gc, NULL);
940 }
941
942 static void
943 draw_check (GtkStyle *style,
944             GdkWindow *window,
945             GtkStateType state,
946             GtkShadowType shadow,
947             GdkRectangle *area,
948             GtkWidget *widget,
949             const gchar *detail, gint x, gint y, gint width, gint height)
950 {
951   x -= (1 + PART_SIZE - width) / 2;
952   y -= (1 + PART_SIZE - height) / 2;
953
954   if (detail && strcmp (detail, "check") == 0)  /* Menu item */
955     {
956       if (shadow == GTK_SHADOW_IN)
957         {
958           draw_part (window, &style->black, area, x, y, CHECK_TEXT);
959           draw_part (window, &style->dark[state], area, x, y, CHECK_AA);
960         }
961     }
962   else
963     {
964       XpThemeElement theme_elt = XP_THEME_ELEMENT_CHECKBOX;
965       switch (shadow)
966         {
967         case GTK_SHADOW_ETCHED_IN:
968           theme_elt = XP_THEME_ELEMENT_INCONSISTENT_CHECKBOX;
969           break;
970
971         case GTK_SHADOW_IN:
972           theme_elt = XP_THEME_ELEMENT_PRESSED_CHECKBOX;
973           break;
974
975         default:
976           break;
977         }
978
979       if (!xp_theme_draw (window, theme_elt,
980                           style, x, y, width, height, state, area))
981         {
982           if (detail && !strcmp (detail, "cellcheck"))
983             state = GTK_STATE_NORMAL;
984
985           draw_part (window, &style->black, area, x, y, CHECK_BLACK);
986           draw_part (window, &style->dark[state], area, x, y, CHECK_DARK);
987           draw_part (window, &style->mid[state], area, x, y, CHECK_MID);
988           draw_part (window, &style->light[state], area, x, y, CHECK_LIGHT);
989           draw_part (window, &style->base[state], area, x, y, CHECK_BASE);
990
991           if (shadow == GTK_SHADOW_IN)
992             {
993               draw_part (window, &style->text[state], area, x,
994                          y, CHECK_TEXT);
995               draw_part (window, &style->text_aa[state], area,
996                          x, y, CHECK_AA);
997             }
998           else if (shadow == GTK_SHADOW_ETCHED_IN)
999             {
1000               draw_part (window, &style->text[state], area, x, y,
1001                          CHECK_INCONSISTENT);
1002               draw_part (window, &style->text_aa[state], area, x, y,
1003                          CHECK_AA);
1004             }
1005         }
1006     }
1007 }
1008
1009 static void
1010 draw_expander (GtkStyle *style,
1011                GdkWindow *window,
1012                GtkStateType state,
1013                GdkRectangle *area,
1014                GtkWidget *widget,
1015                const gchar *detail,
1016                gint x, gint y, GtkExpanderStyle expander_style)
1017 {
1018   cairo_t *cr = gdk_cairo_create (window);
1019
1020   gint expander_size;
1021   gint expander_semi_size;
1022   XpThemeElement xp_expander;
1023
1024   gtk_widget_style_get (widget, "expander_size", &expander_size, NULL);
1025
1026   switch (expander_style)
1027     {
1028     case GTK_EXPANDER_COLLAPSED:
1029     case GTK_EXPANDER_SEMI_COLLAPSED:
1030       xp_expander = XP_THEME_ELEMENT_TREEVIEW_EXPANDER_CLOSED;
1031       break;
1032
1033     default:
1034       xp_expander = XP_THEME_ELEMENT_TREEVIEW_EXPANDER_OPENED;
1035       break;
1036     }
1037
1038   if ((expander_size % 2) == 0)
1039     expander_size--;
1040
1041   if (expander_size > 2)
1042     expander_size -= 2;
1043
1044   if (area)
1045     {
1046       gdk_cairo_rectangle (cr, area);
1047       cairo_clip (cr);
1048       gdk_cairo_set_source_color (cr, &style->fg[state]);
1049     }
1050
1051   expander_semi_size = expander_size / 2;
1052   x -= expander_semi_size;
1053   y -= expander_semi_size;
1054
1055   if (!xp_theme_draw (window, xp_expander, style,
1056                       x, y, expander_size, expander_size, state, area))
1057     {
1058       HDC dc;
1059       RECT rect;
1060       HPEN pen;
1061       HGDIOBJ old_pen;
1062       XpDCInfo dc_info;
1063
1064       dc = get_window_dc (style, window, state, &dc_info, x, y, expander_size,
1065                           expander_size, &rect);
1066       FrameRect (dc, &rect, GetSysColorBrush (COLOR_GRAYTEXT));
1067       InflateRect (&rect, -1, -1);
1068       FillRect (dc, &rect,
1069                 GetSysColorBrush (state ==
1070                                   GTK_STATE_INSENSITIVE ? COLOR_BTNFACE :
1071                                   COLOR_WINDOW));
1072
1073       InflateRect (&rect, -1, -1);
1074
1075       pen = CreatePen (PS_SOLID, 1, GetSysColor (COLOR_WINDOWTEXT));
1076       old_pen = SelectObject (dc, pen);
1077
1078       MoveToEx (dc, rect.left, rect.top - 2 + expander_semi_size, NULL);
1079       LineTo (dc, rect.right, rect.top - 2 + expander_semi_size);
1080
1081       if (expander_style == GTK_EXPANDER_COLLAPSED ||
1082           expander_style == GTK_EXPANDER_SEMI_COLLAPSED)
1083         {
1084           MoveToEx (dc, rect.left - 2 + expander_semi_size, rect.top, NULL);
1085           LineTo (dc, rect.left - 2 + expander_semi_size, rect.bottom);
1086         }
1087
1088       SelectObject (dc, old_pen);
1089       DeleteObject (pen);
1090       release_window_dc (&dc_info);
1091     }
1092
1093   cairo_destroy(cr);
1094 }
1095
1096 static void
1097 draw_option (GtkStyle *style,
1098              GdkWindow *window,
1099              GtkStateType state,
1100              GtkShadowType shadow,
1101              GdkRectangle *area,
1102              GtkWidget *widget,
1103              const gchar *detail, gint x, gint y, gint width, gint height)
1104 {
1105   x -= (1 + PART_SIZE - width) / 2;
1106   y -= (1 + PART_SIZE - height) / 2;
1107
1108   if (detail && strcmp (detail, "option") == 0) /* Menu item */
1109     {
1110       if (shadow == GTK_SHADOW_IN)
1111         {
1112           draw_part (window, &style->fg[state], area, x, y, RADIO_TEXT);
1113         }
1114     }
1115   else
1116     {
1117       if (xp_theme_draw (window, shadow == GTK_SHADOW_IN
1118                          ? XP_THEME_ELEMENT_PRESSED_RADIO_BUTTON
1119                          : XP_THEME_ELEMENT_RADIO_BUTTON,
1120                          style, x, y, width, height, state, area))
1121         {
1122         }
1123       else
1124         {
1125           if (detail && !strcmp (detail, "cellradio"))
1126             state = GTK_STATE_NORMAL;
1127
1128           draw_part (window, &style->black, area, x, y, RADIO_BLACK);
1129           draw_part (window, &style->dark[state], area, x, y, RADIO_DARK);
1130           draw_part (window, &style->mid[state], area, x, y, RADIO_MID);
1131           draw_part (window, &style->light[state], area, x, y, RADIO_LIGHT);
1132           draw_part (window, &style->base[state], area, x, y, RADIO_BASE);
1133
1134           if (shadow == GTK_SHADOW_IN)
1135             draw_part (window, &style->text[state], area, x, y, RADIO_TEXT);
1136         }
1137     }
1138 }
1139
1140 static void
1141 draw_varrow (GdkWindow *window,
1142              GdkColor *gc,
1143              GtkShadowType shadow_type,
1144              GdkRectangle *area,
1145              GtkArrowType arrow_type, gint x, gint y, gint width, gint height)
1146 {
1147   gint steps, extra;
1148   gint y_start, y_increment;
1149   gint i;
1150   cairo_t *cr;
1151   
1152   cr = gdk_cairo_create (window);
1153
1154   if (area)
1155     {
1156        gdk_cairo_rectangle (cr, area);
1157        cairo_clip (cr);
1158     }
1159
1160   width = width + width % 2 - 1;        /* Force odd */
1161   steps = 1 + width / 2;
1162   extra = height - steps;
1163
1164   if (arrow_type == GTK_ARROW_DOWN)
1165     {
1166       y_start = y;
1167       y_increment = 1;
1168     }
1169   else
1170     {
1171       y_start = y + height - 1;
1172       y_increment = -1;
1173     }
1174
1175   for (i = extra; i < height; i++)
1176     {
1177       _cairo_draw_line (cr, gc,
1178                      x + (i - extra), y_start + i * y_increment,
1179                      x + width - (i - extra) - 1, y_start + i * y_increment);
1180     }
1181
1182   cairo_destroy(cr);
1183 }
1184
1185 static void
1186 draw_harrow (GdkWindow *window,
1187              GdkColor *gc,
1188              GtkShadowType shadow_type,
1189              GdkRectangle *area,
1190              GtkArrowType arrow_type, gint x, gint y, gint width, gint height)
1191 {
1192   gint steps, extra;
1193   gint x_start, x_increment;
1194   gint i;
1195   cairo_t *cr;
1196   
1197   cr = gdk_cairo_create (window);
1198
1199   if (area)
1200     {
1201        gdk_cairo_rectangle (cr, area);
1202        cairo_clip (cr);
1203     }
1204
1205   height = height + height % 2 - 1;     /* Force odd */
1206   steps = 1 + height / 2;
1207   extra = width - steps;
1208
1209   if (arrow_type == GTK_ARROW_RIGHT)
1210     {
1211       x_start = x;
1212       x_increment = 1;
1213     }
1214   else
1215     {
1216       x_start = x + width - 1;
1217       x_increment = -1;
1218     }
1219
1220   for (i = extra; i < width; i++)
1221     {
1222       _cairo_draw_line (cr, gc,
1223                      x_start + i * x_increment, y + (i - extra),
1224                      x_start + i * x_increment, y + height - (i - extra) - 1);
1225     }
1226
1227   cairo_destroy(cr);
1228 }
1229
1230 /* This function makes up for some brokeness in gtkrange.c
1231  * where we never get the full arrow of the stepper button
1232  * and the type of button in a single drawing function.
1233  *
1234  * It doesn't work correctly when the scrollbar is squished
1235  * to the point we don't have room for full-sized steppers.
1236  */
1237 static void
1238 reverse_engineer_stepper_box (GtkWidget *range,
1239                               GtkArrowType arrow_type,
1240                               gint *x, gint *y, gint *width, gint *height)
1241 {
1242   gint slider_width = 14, stepper_size = 14;
1243   gint box_width;
1244   gint box_height;
1245
1246   if (range)
1247     {
1248       gtk_widget_style_get (range,
1249                             "slider_width", &slider_width,
1250                             "stepper_size", &stepper_size, NULL);
1251     }
1252
1253   if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
1254     {
1255       box_width = slider_width;
1256       box_height = stepper_size;
1257     }
1258   else
1259     {
1260       box_width = stepper_size;
1261       box_height = slider_width;
1262     }
1263
1264   *x = *x - (box_width - *width) / 2;
1265   *y = *y - (box_height - *height) / 2;
1266   *width = box_width;
1267   *height = box_height;
1268 }
1269
1270 static XpThemeElement
1271 to_xp_arrow (GtkArrowType arrow_type)
1272 {
1273   XpThemeElement xp_arrow;
1274
1275   switch (arrow_type)
1276     {
1277     case GTK_ARROW_UP:
1278       xp_arrow = XP_THEME_ELEMENT_ARROW_UP;
1279       break;
1280
1281     case GTK_ARROW_DOWN:
1282       xp_arrow = XP_THEME_ELEMENT_ARROW_DOWN;
1283       break;
1284
1285     case GTK_ARROW_LEFT:
1286       xp_arrow = XP_THEME_ELEMENT_ARROW_LEFT;
1287       break;
1288
1289     default:
1290       xp_arrow = XP_THEME_ELEMENT_ARROW_RIGHT;
1291       break;
1292     }
1293
1294   return xp_arrow;
1295 }
1296
1297 static void
1298 draw_arrow (GtkStyle *style,
1299             GdkWindow *window,
1300             GtkStateType state,
1301             GtkShadowType shadow,
1302             GdkRectangle *area,
1303             GtkWidget *widget,
1304             const gchar *detail,
1305             GtkArrowType arrow_type,
1306             gboolean fill, gint x, gint y, gint width, gint height)
1307 {
1308   const gchar *name;
1309   HDC dc;
1310   RECT rect;
1311   XpDCInfo dc_info;
1312
1313   name = gtk_widget_get_name (widget);
1314
1315   sanitize_size (window, &width, &height);
1316
1317   if (GTK_IS_ARROW (widget) && is_combo_box_child (widget) && xp_theme_is_active ())
1318     return;
1319
1320   if (detail && strcmp (detail, "spinbutton") == 0)
1321     {
1322       if (xp_theme_is_drawable (XP_THEME_ELEMENT_SPIN_BUTTON_UP))
1323         {
1324           return;
1325         }
1326
1327       width -= 2;
1328       --height;
1329       if (arrow_type == GTK_ARROW_DOWN)
1330         ++y;
1331       ++x;
1332
1333       if (state == GTK_STATE_ACTIVE)
1334         {
1335           ++x;
1336           ++y;
1337         }
1338
1339       draw_varrow (window, &style->fg[state], shadow, area,
1340                    arrow_type, x, y, width, height);
1341
1342       return;
1343     }
1344   else if (detail && (!strcmp (detail, "vscrollbar")
1345                       || !strcmp (detail, "hscrollbar")))
1346     {
1347       gboolean is_disabled = FALSE;
1348       UINT btn_type = 0;
1349       GtkScrollbar *scrollbar = GTK_SCROLLBAR (widget);
1350
1351       gint box_x = x;
1352       gint box_y = y;
1353       gint box_width = width;
1354       gint box_height = height;
1355
1356       reverse_engineer_stepper_box (widget, arrow_type,
1357                                     &box_x, &box_y, &box_width, &box_height);
1358
1359       if (gtk_range_get_adjustment(&scrollbar->range)->page_size >=
1360           (gtk_range_get_adjustment(&scrollbar->range)->upper -
1361            gtk_range_get_adjustment(&scrollbar->range)->lower))
1362         {
1363           is_disabled = TRUE;
1364         }
1365
1366       if (xp_theme_draw (window, to_xp_arrow (arrow_type), style, box_x, box_y,
1367                          box_width, box_height, state, area))
1368         {
1369         }
1370       else
1371         {
1372           switch (arrow_type)
1373             {
1374             case GTK_ARROW_UP:
1375               btn_type = DFCS_SCROLLUP;
1376               break;
1377
1378             case GTK_ARROW_DOWN:
1379               btn_type = DFCS_SCROLLDOWN;
1380               break;
1381
1382             case GTK_ARROW_LEFT:
1383               btn_type = DFCS_SCROLLLEFT;
1384               break;
1385
1386             case GTK_ARROW_RIGHT:
1387               btn_type = DFCS_SCROLLRIGHT;
1388               break;
1389
1390             case GTK_ARROW_NONE:
1391               break;
1392             }
1393
1394           if (state == GTK_STATE_INSENSITIVE)
1395             {
1396               btn_type |= DFCS_INACTIVE;
1397             }
1398
1399           if (widget)
1400             {
1401               sanitize_size (window, &width, &height);
1402
1403               dc = get_window_dc (style, window, state, &dc_info,
1404                                   box_x, box_y, box_width, box_height, &rect);
1405               DrawFrameControl (dc, &rect, DFC_SCROLL,
1406                                 btn_type | (shadow ==
1407                                             GTK_SHADOW_IN ? (DFCS_PUSHED |
1408                                                              DFCS_FLAT) : 0));
1409               release_window_dc (&dc_info);
1410             }
1411         }
1412     }
1413   else
1414     {
1415       /* draw the toolbar chevrons - waiting for GTK 2.4 */
1416       if (name && !strcmp (name, "gtk-toolbar-arrow"))
1417         {
1418           if (xp_theme_draw
1419               (window, XP_THEME_ELEMENT_REBAR_CHEVRON, style, x, y,
1420                width, height, state, area))
1421             {
1422               return;
1423             }
1424         }
1425       /* probably a gtk combo box on a toolbar */
1426       else if (0                /* widget->parent && GTK_IS_BUTTON
1427                                    (widget->parent) */ )
1428         {
1429           if (xp_theme_draw
1430               (window, XP_THEME_ELEMENT_COMBOBUTTON, style, x - 3,
1431                widget->allocation.y + 1, width + 5,
1432                widget->allocation.height - 4, state, area))
1433             {
1434               return;
1435             }
1436         }
1437
1438       if (arrow_type == GTK_ARROW_UP || arrow_type == GTK_ARROW_DOWN)
1439         {
1440           x += (width - 7) / 2;
1441           y += (height - 5) / 2;
1442
1443           draw_varrow (window, &style->fg[state], shadow, area,
1444                        arrow_type, x, y, 7, 5);
1445         }
1446       else
1447         {
1448           x += (width - 5) / 2;
1449           y += (height - 7) / 2;
1450
1451           draw_harrow (window, &style->fg[state], shadow, area,
1452                        arrow_type, x, y, 5, 7);
1453         }
1454     }
1455 }
1456
1457 static void
1458 option_menu_get_props (GtkWidget *widget,
1459                        GtkRequisition *indicator_size,
1460                        GtkBorder *indicator_spacing)
1461 {
1462   GtkRequisition *tmp_size = NULL;
1463   GtkBorder *tmp_spacing = NULL;
1464
1465   if (widget)
1466     gtk_widget_style_get (widget,
1467                           "indicator_size", &tmp_size,
1468                           "indicator_spacing", &tmp_spacing, NULL);
1469
1470   if (tmp_size)
1471     {
1472       *indicator_size = *tmp_size;
1473       gtk_requisition_free (tmp_size);
1474     }
1475   else
1476     {
1477       *indicator_size = default_option_indicator_size;
1478     }
1479
1480   if (tmp_spacing)
1481     {
1482       *indicator_spacing = *tmp_spacing;
1483       gtk_border_free (tmp_spacing);
1484     }
1485   else
1486     {
1487       *indicator_spacing = default_option_indicator_spacing;
1488     }
1489 }
1490
1491 static gboolean
1492 is_toolbar_child (GtkWidget *wid)
1493 {
1494   while (wid)
1495     {
1496       if (GTK_IS_TOOLBAR (wid) || GTK_IS_HANDLE_BOX (wid))
1497         return TRUE;
1498       else
1499         wid = wid->parent;
1500     }
1501
1502   return FALSE;
1503 }
1504
1505 static gboolean
1506 is_menu_tool_button_child (GtkWidget *wid)
1507 {
1508   while (wid)
1509     {
1510       if (GTK_IS_MENU_TOOL_BUTTON (wid))
1511         return TRUE;
1512       else
1513         wid = wid->parent;
1514     }
1515   return FALSE;
1516 }
1517
1518 static HPEN
1519 get_light_pen ()
1520 {
1521   if (!g_light_pen)
1522     {
1523       g_light_pen = CreatePen (PS_SOLID | PS_INSIDEFRAME, 1,
1524                                GetSysColor (COLOR_BTNHIGHLIGHT));
1525     }
1526
1527   return g_light_pen;
1528 }
1529
1530 static HPEN
1531 get_dark_pen ()
1532 {
1533   if (!g_dark_pen)
1534     {
1535       g_dark_pen = CreatePen (PS_SOLID | PS_INSIDEFRAME, 1,
1536                               GetSysColor (COLOR_BTNSHADOW));
1537     }
1538
1539   return g_dark_pen;
1540 }
1541
1542 static void
1543 draw_3d_border (HDC hdc, RECT *rc, gboolean sunken)
1544 {
1545   HPEN pen1, pen2;
1546   HGDIOBJ old_pen;
1547
1548   if (sunken)
1549     {
1550       pen1 = get_dark_pen ();
1551       pen2 = get_light_pen ();
1552     }
1553   else
1554     {
1555       pen1 = get_light_pen ();
1556       pen2 = get_dark_pen ();
1557     }
1558
1559   MoveToEx (hdc, rc->left, rc->bottom - 1, NULL);
1560
1561   old_pen = SelectObject (hdc, pen1);
1562   LineTo (hdc, rc->left, rc->top);
1563   LineTo (hdc, rc->right - 1, rc->top);
1564   SelectObject (hdc, old_pen);
1565
1566   old_pen = SelectObject (hdc, pen2);
1567   LineTo (hdc, rc->right - 1, rc->bottom - 1);
1568   LineTo (hdc, rc->left, rc->bottom - 1);
1569   SelectObject (hdc, old_pen);
1570 }
1571
1572 static gboolean
1573 draw_menu_item (GdkWindow *window, GtkWidget *widget, GtkStyle *style,
1574                 gint x, gint y, gint width, gint height,
1575                 GtkStateType state_type, GdkRectangle *area)
1576 {
1577   GtkWidget *parent;
1578   GtkMenuShell *bar;
1579   HDC dc;
1580   RECT rect;
1581   XpDCInfo dc_info;
1582
1583   if (xp_theme_is_active ())
1584     {
1585       return (xp_theme_draw (window, XP_THEME_ELEMENT_MENU_ITEM, style,
1586                              x, y, width, height, state_type, area));
1587     }
1588
1589   if ((parent = gtk_widget_get_parent (widget))
1590       && GTK_IS_MENU_BAR (parent) && !xp_theme_is_active ())
1591     {
1592       bar = GTK_MENU_SHELL (parent);
1593
1594       dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
1595
1596       if (state_type == GTK_STATE_PRELIGHT)
1597         {
1598           draw_3d_border (dc, &rect, bar->active);
1599         }
1600
1601       release_window_dc (&dc_info);
1602
1603       return TRUE;
1604     }
1605
1606   return FALSE;
1607 }
1608
1609 static HBRUSH
1610 get_dither_brush (void)
1611 {
1612   WORD pattern[8];
1613   HBITMAP pattern_bmp;
1614   int i;
1615
1616   if (g_dither_brush)
1617     return g_dither_brush;
1618
1619   for (i = 0; i < 8; i++)
1620     {
1621       pattern[i] = (WORD) (0x5555 << (i & 1));
1622     }
1623
1624   pattern_bmp = CreateBitmap (8, 8, 1, 1, &pattern);
1625
1626   if (pattern_bmp)
1627     {
1628       g_dither_brush = CreatePatternBrush (pattern_bmp);
1629       DeleteObject (pattern_bmp);
1630     }
1631
1632   return g_dither_brush;
1633 }
1634
1635 static gboolean
1636 draw_tool_button (GdkWindow *window, GtkWidget *widget, GtkStyle *style,
1637                   gint x, gint y, gint width, gint height,
1638                   GtkStateType state_type, GdkRectangle *area)
1639 {
1640   HDC dc;
1641   RECT rect;
1642   XpDCInfo dc_info;
1643   gboolean is_toggled = FALSE;
1644
1645   if (xp_theme_is_active ())
1646     {
1647       return (xp_theme_draw (window, XP_THEME_ELEMENT_TOOLBAR_BUTTON, style,
1648                              x, y, width, height, state_type, area));
1649     }
1650
1651   if (GTK_IS_TOGGLE_BUTTON (widget))
1652     {
1653       if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1654         {
1655           is_toggled = TRUE;
1656         }
1657     }
1658
1659   if (state_type != GTK_STATE_PRELIGHT
1660       && state_type != GTK_STATE_ACTIVE && !is_toggled)
1661     {
1662       return FALSE;
1663     }
1664
1665   dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
1666   if (state_type == GTK_STATE_PRELIGHT)
1667     {
1668       if (is_toggled)
1669         {
1670           FillRect (dc, &rect, GetSysColorBrush (COLOR_BTNFACE));
1671         }
1672
1673       draw_3d_border (dc, &rect, is_toggled);
1674     }
1675   else if (state_type == GTK_STATE_ACTIVE)
1676     {
1677       if (is_toggled && !is_menu_tool_button_child (widget->parent))
1678         {
1679           SetTextColor (dc, GetSysColor (COLOR_3DHILIGHT));
1680           SetBkColor (dc, GetSysColor (COLOR_BTNFACE));
1681           FillRect (dc, &rect, get_dither_brush ());
1682         }
1683
1684       draw_3d_border (dc, &rect, TRUE);
1685     }
1686
1687   release_window_dc (&dc_info);
1688
1689   return TRUE;
1690 }
1691
1692 static void
1693 draw_push_button (GdkWindow *window, GtkWidget *widget, GtkStyle *style,
1694                   gint x, gint y, gint width, gint height,
1695                   GtkStateType state_type, gboolean is_default)
1696 {
1697   HDC dc;
1698   RECT rect;
1699   XpDCInfo dc_info;
1700
1701   dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
1702
1703   if (GTK_IS_TOGGLE_BUTTON (widget))
1704     {
1705       if (state_type == GTK_STATE_PRELIGHT &&
1706           gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)))
1707         {
1708           state_type = GTK_STATE_ACTIVE;
1709         }
1710     }
1711
1712   if (state_type == GTK_STATE_ACTIVE)
1713     {
1714       if (GTK_IS_TOGGLE_BUTTON (widget))
1715         {
1716           DrawEdge (dc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
1717           SetTextColor (dc, GetSysColor (COLOR_3DHILIGHT));
1718           SetBkColor (dc, GetSysColor (COLOR_BTNFACE));
1719           FillRect (dc, &rect, get_dither_brush ());
1720         }
1721       else
1722         {
1723           FrameRect (dc, &rect, GetSysColorBrush (COLOR_WINDOWFRAME));
1724           InflateRect (&rect, -1, -1);
1725           FrameRect (dc, &rect, GetSysColorBrush (COLOR_BTNSHADOW));
1726           InflateRect (&rect, -1, -1);
1727           FillRect (dc, &rect, GetSysColorBrush (COLOR_BTNFACE));
1728         }
1729     }
1730   else
1731     {
1732       if (is_default || gtk_widget_has_focus (widget))
1733         {
1734           FrameRect (dc, &rect, GetSysColorBrush (COLOR_WINDOWFRAME));
1735           InflateRect (&rect, -1, -1);
1736         }
1737
1738       DrawFrameControl (dc, &rect, DFC_BUTTON, DFCS_BUTTONPUSH);
1739     }
1740
1741   release_window_dc (&dc_info);
1742 }
1743
1744 static void
1745 draw_box (GtkStyle *style,
1746           GdkWindow *window,
1747           GtkStateType state_type,
1748           GtkShadowType shadow_type,
1749           GdkRectangle *area,
1750           GtkWidget *widget,
1751           const gchar *detail, gint x, gint y, gint width, gint height)
1752 {
1753   if (is_combo_box_child (widget) && detail && !strcmp (detail, "button"))
1754     {
1755       RECT rect;
1756       XpDCInfo dc_info;
1757       DWORD border;
1758       HDC dc;
1759       int cx;
1760
1761       border = (GTK_TOGGLE_BUTTON (widget)->active ? DFCS_PUSHED | DFCS_FLAT : 0);
1762
1763       dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
1764       DrawFrameControl (dc, &rect, DFC_SCROLL, DFCS_SCROLLDOWN | border);
1765       release_window_dc (&dc_info);
1766
1767       if (xp_theme_is_active ()
1768           && xp_theme_draw (window, XP_THEME_ELEMENT_COMBOBUTTON, style, x, y,
1769                             width, height, state_type, area))
1770         {
1771       cx = GetSystemMetrics(SM_CXVSCROLL);
1772       x += width - cx;
1773       width = cx;
1774
1775
1776       dc = get_window_dc (style, window, state_type, &dc_info, x, y, width - cx, height, &rect);
1777       FillRect (dc, &rect, GetSysColorBrush (COLOR_WINDOW));
1778       release_window_dc (&dc_info);
1779       return;
1780         }
1781     }
1782
1783   if (detail &&
1784       (!strcmp (detail, "button") || !strcmp (detail, "buttondefault")))
1785     {
1786       if (GTK_IS_TREE_VIEW (widget->parent))
1787       {
1788         if (xp_theme_draw
1789               (window, XP_THEME_ELEMENT_LIST_HEADER, style, x, y,
1790                width, height, state_type, area))
1791             {
1792               return;
1793             }
1794           else
1795             {
1796               HDC dc;
1797               RECT rect;
1798               XpDCInfo dc_info;
1799               dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
1800
1801               DrawFrameControl (dc, &rect, DFC_BUTTON, DFCS_BUTTONPUSH |
1802                                 (state_type ==
1803                                  GTK_STATE_ACTIVE ? (DFCS_PUSHED | DFCS_FLAT)
1804                                  : 0));
1805               release_window_dc (&dc_info);
1806             }
1807         }
1808       else if (is_toolbar_child (widget->parent)
1809                || (!GTK_IS_BUTTON (widget) ||
1810                    (GTK_RELIEF_NONE == gtk_button_get_relief (GTK_BUTTON (widget)))))
1811         {
1812           if (draw_tool_button (window, widget, style, x, y,
1813                                 width, height, state_type, area))
1814             {
1815               return;
1816             }
1817         }
1818       else
1819         {
1820           gboolean is_default = gtk_widget_has_default (widget);
1821           if (xp_theme_draw
1822               (window,
1823                is_default ? XP_THEME_ELEMENT_DEFAULT_BUTTON :
1824                XP_THEME_ELEMENT_BUTTON, style, x, y, width, height,
1825                state_type, area))
1826             {
1827               return;
1828             }
1829
1830           draw_push_button (window, widget, style,
1831                             x, y, width, height, state_type, is_default);
1832
1833           return;
1834         }
1835
1836       return;
1837     }
1838   else if (detail && !strcmp (detail, "spinbutton"))
1839     {
1840       if (xp_theme_is_drawable (XP_THEME_ELEMENT_SPIN_BUTTON_UP))
1841         {
1842           return;
1843         }
1844     }
1845   else if (detail && (!strcmp (detail, "spinbutton_up")
1846                       || !strcmp (detail, "spinbutton_down")))
1847     {
1848       if (!xp_theme_draw (window,
1849                           (!strcmp (detail, "spinbutton_up"))
1850                           ? XP_THEME_ELEMENT_SPIN_BUTTON_UP
1851                           : XP_THEME_ELEMENT_SPIN_BUTTON_DOWN,
1852                           style, x, y, width, height, state_type, area))
1853         {
1854           RECT rect;
1855           XpDCInfo dc_info;
1856           HDC dc;
1857
1858           dc = get_window_dc (style, window, state_type, &dc_info,
1859                               x, y, width, height, &rect);
1860           DrawEdge (dc, &rect,
1861                     state_type ==
1862                     GTK_STATE_ACTIVE ? EDGE_SUNKEN : EDGE_RAISED, BF_RECT);
1863           release_window_dc (&dc_info);
1864         }
1865       return;
1866     }
1867   else if (detail && !strcmp (detail, "slider"))
1868     {
1869       if (GTK_IS_SCROLLBAR (widget))
1870         {
1871           GtkScrollbar *scrollbar = GTK_SCROLLBAR (widget);
1872           gboolean is_v = GTK_IS_VSCROLLBAR (widget);
1873
1874           if (xp_theme_draw (window,
1875                              is_v
1876                              ? XP_THEME_ELEMENT_SCROLLBAR_V
1877                              : XP_THEME_ELEMENT_SCROLLBAR_H,
1878                              style, x, y, width, height, state_type, area))
1879             {
1880               XpThemeElement gripper =
1881                 (is_v ? XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_V :
1882                  XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_H);
1883
1884               /* Do not display grippers on tiny scroll bars,
1885                  the limit imposed is rather arbitrary, perhaps
1886                  we can fetch the gripper geometry from
1887                  somewhere and use that... */
1888               if ((gripper ==
1889                    XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_H
1890                    && width < 16)
1891                   || (gripper ==
1892                       XP_THEME_ELEMENT_SCROLLBAR_GRIPPER_V && height < 16))
1893                 {
1894                   return;
1895                 }
1896
1897               xp_theme_draw (window, gripper, style, x, y,
1898                              width, height, state_type, area);
1899               return;
1900             }
1901           else
1902             {
1903               if (gtk_range_get_adjustment(&scrollbar->range)->page_size >=
1904                   (gtk_range_get_adjustment(&scrollbar->range)->upper -
1905                    gtk_range_get_adjustment(&scrollbar->range)->lower))
1906                 {
1907                   return;
1908                 }
1909             }
1910         }
1911     }
1912   else if (detail && !strcmp (detail, "bar"))
1913     {
1914       if (widget && GTK_IS_PROGRESS_BAR (widget))
1915         {
1916           GtkProgressBar *progress_bar = GTK_PROGRESS_BAR (widget);
1917           XpThemeElement xp_progress_bar =
1918             map_gtk_progress_bar_to_xp (progress_bar, FALSE);
1919
1920           if (xp_theme_draw (window, xp_progress_bar, style, x, y,
1921                              width, height, state_type, area))
1922             {
1923               return;
1924             }
1925
1926           shadow_type = GTK_SHADOW_NONE;
1927         }
1928     }
1929   else if (detail && strcmp (detail, "menuitem") == 0)
1930     {
1931       shadow_type = GTK_SHADOW_NONE;
1932       if (draw_menu_item (window, widget, style,
1933                           x, y, width, height, state_type, area))
1934         {
1935           return;
1936         }
1937     }
1938   else if (detail && !strcmp (detail, "trough"))
1939     {
1940       if (widget && GTK_IS_PROGRESS_BAR (widget))
1941         {
1942           GtkProgressBar *progress_bar = GTK_PROGRESS_BAR (widget);
1943           XpThemeElement xp_progress_bar =
1944             map_gtk_progress_bar_to_xp (progress_bar, TRUE);
1945           if (xp_theme_draw
1946               (window, xp_progress_bar, style, x, y, width, height,
1947                state_type, area))
1948             {
1949               return;
1950             }
1951           else
1952             {
1953               /* Blank in classic Windows */
1954             }
1955         }
1956       else if (widget && GTK_IS_SCROLLBAR (widget))
1957         {
1958           gboolean is_vertical = GTK_IS_VSCROLLBAR (widget);
1959
1960           if (xp_theme_draw (window,
1961                              is_vertical
1962                              ? XP_THEME_ELEMENT_TROUGH_V
1963                              : XP_THEME_ELEMENT_TROUGH_H,
1964                              style, x, y, width, height, state_type, area))
1965             {
1966               return;
1967             }
1968           else
1969             {
1970               HDC dc;
1971               RECT rect;
1972               XpDCInfo dc_info;
1973
1974               sanitize_size (window, &width, &height);
1975               dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
1976
1977               SetTextColor (dc, GetSysColor (COLOR_3DHILIGHT));
1978               SetBkColor (dc, GetSysColor (COLOR_BTNFACE));
1979               FillRect (dc, &rect, get_dither_brush ());
1980
1981               release_window_dc (&dc_info);
1982
1983               return;
1984             }
1985         }
1986       else if (widget && GTK_IS_SCALE (widget))
1987         {
1988           gboolean is_vertical = GTK_IS_VSCALE (widget);
1989
1990           if (!xp_theme_is_active ())
1991             {
1992               parent_class->draw_box (style, window, state_type,
1993                                       GTK_SHADOW_NONE, area,
1994                                       widget, detail, x, y, width, height);
1995             }
1996
1997           if (is_vertical)
1998             {
1999               if (xp_theme_draw
2000                   (window, XP_THEME_ELEMENT_SCALE_TROUGH_V,
2001                    style, (2 * x + width) / 2, y, 2, height,
2002                    state_type, area))
2003                 {
2004                   return;
2005                 }
2006
2007               parent_class->draw_box (style, window, state_type,
2008                                       GTK_SHADOW_ETCHED_IN,
2009                                       area, NULL, NULL,
2010                                       (2 * x + width) / 2, y, 1, height);
2011             }
2012           else
2013             {
2014               if (xp_theme_draw
2015                   (window, XP_THEME_ELEMENT_SCALE_TROUGH_H,
2016                    style, x, (2 * y + height) / 2, width, 2,
2017                    state_type, area))
2018                 {
2019                   return;
2020                 }
2021
2022               parent_class->draw_box (style, window, state_type,
2023                                       GTK_SHADOW_ETCHED_IN,
2024                                       area, NULL, NULL, x,
2025                                       (2 * y + height) / 2, width, 1);
2026             }
2027
2028           return;
2029         }
2030     }
2031   else if (detail && strcmp (detail, "optionmenu") == 0)
2032     {
2033       if (xp_theme_draw (window, XP_THEME_ELEMENT_EDIT_TEXT,
2034                          style, x, y, width, height, state_type, area))
2035         {
2036           return;
2037         }
2038     }
2039   else if (detail
2040            && (strcmp (detail, "vscrollbar") == 0
2041                || strcmp (detail, "hscrollbar") == 0))
2042     {
2043       return;
2044     }
2045   else if (detail
2046            && (strcmp (detail, "handlebox_bin") == 0
2047                || strcmp (detail, "toolbar") == 0
2048                || strcmp (detail, "menubar") == 0))
2049     {
2050       sanitize_size (window, &width, &height);
2051       if (xp_theme_draw (window, XP_THEME_ELEMENT_REBAR,
2052                          style, x, y, width, height, state_type, area))
2053         {
2054           return;
2055         }
2056     }
2057   else if (detail && (!strcmp (detail, "handlebox")))   /* grip */
2058     {
2059       if (!xp_theme_is_active ())
2060         {
2061           return;
2062         }
2063     }
2064   else if (detail && !strcmp (detail, "notebook") && GTK_IS_NOTEBOOK (widget))
2065     {
2066       if (xp_theme_draw (window, XP_THEME_ELEMENT_TAB_PANE, style,
2067                          x, y, width, height, state_type, area))
2068         {
2069           return;
2070         }
2071     }
2072
2073   else
2074     {
2075       const gchar *name = gtk_widget_get_name (widget);
2076
2077       if (name && !strcmp (name, "gtk-tooltips"))
2078         {
2079           if (xp_theme_draw
2080               (window, XP_THEME_ELEMENT_TOOLTIP, style, x, y, width,
2081                height, state_type, area))
2082             {
2083               return;
2084             }
2085           else
2086             {
2087               HBRUSH brush;
2088               RECT rect;
2089               XpDCInfo dc_info;
2090               HDC hdc;
2091
2092               hdc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
2093
2094               brush = GetSysColorBrush (COLOR_3DDKSHADOW);
2095
2096               if (brush)
2097                 {
2098                   FrameRect (hdc, &rect, brush);
2099                 }
2100
2101               InflateRect (&rect, -1, -1);
2102               FillRect (hdc, &rect, (HBRUSH) (COLOR_INFOBK + 1));
2103
2104               release_window_dc (&dc_info);
2105
2106               return;
2107             }
2108         }
2109     }
2110
2111   parent_class->draw_box (style, window, state_type, shadow_type, area,
2112                           widget, detail, x, y, width, height);
2113
2114   if (detail && strcmp (detail, "optionmenu") == 0)
2115     {
2116       GtkRequisition indicator_size;
2117       GtkBorder indicator_spacing;
2118       gint vline_x;
2119
2120       option_menu_get_props (widget, &indicator_size, &indicator_spacing);
2121
2122       sanitize_size (window, &width, &height);
2123
2124       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
2125         {
2126           vline_x =
2127             x + indicator_size.width + indicator_spacing.left +
2128             indicator_spacing.right;
2129         }
2130       else
2131         {
2132           vline_x = x + width - (indicator_size.width +
2133                                  indicator_spacing.left +
2134                                  indicator_spacing.right) - style->xthickness;
2135
2136           parent_class->draw_vline (style, window, state_type, area, widget,
2137                                     detail,
2138                                     y + style->ythickness + 1,
2139                                     y + height - style->ythickness - 3, vline_x);
2140         }
2141     }
2142 }
2143
2144 static void
2145 draw_tab (GtkStyle *style,
2146           GdkWindow *window,
2147           GtkStateType state,
2148           GtkShadowType shadow,
2149           GdkRectangle *area,
2150           GtkWidget *widget,
2151           const gchar *detail, gint x, gint y, gint width, gint height)
2152 {
2153   GtkRequisition indicator_size;
2154   GtkBorder indicator_spacing;
2155
2156   gint arrow_height;
2157
2158   g_return_if_fail (style != NULL);
2159   g_return_if_fail (window != NULL);
2160
2161   if (detail && !strcmp (detail, "optionmenutab"))
2162     {
2163       if (xp_theme_draw (window, XP_THEME_ELEMENT_COMBOBUTTON,
2164                          style, x - 5, widget->allocation.y + 1,
2165                          width + 10, widget->allocation.height - 2,
2166                          state, area))
2167         {
2168           return;
2169         }
2170     }
2171
2172   option_menu_get_props (widget, &indicator_size, &indicator_spacing);
2173
2174   x += (width - indicator_size.width) / 2;
2175   arrow_height = (indicator_size.width + 1) / 2;
2176
2177   y += (height - arrow_height) / 2;
2178
2179   draw_varrow (window, &style->black, shadow, area, GTK_ARROW_DOWN,
2180                x, y, indicator_size.width, arrow_height);
2181 }
2182
2183 /* Draw classic Windows tab - thanks Mozilla!
2184   (no system API for this, but DrawEdge can draw all the parts of a tab) */
2185 static void
2186 DrawTab (HDC hdc, const RECT R, gint32 aPosition, gboolean aSelected,
2187          gboolean aDrawLeft, gboolean aDrawRight)
2188 {
2189   gint32 leftFlag, topFlag, rightFlag, lightFlag, shadeFlag;
2190   RECT topRect, sideRect, bottomRect, lightRect, shadeRect;
2191   gint32 selectedOffset, lOffset, rOffset;
2192
2193   selectedOffset = aSelected ? 1 : 0;
2194   lOffset = aDrawLeft ? 2 : 0;
2195   rOffset = aDrawRight ? 2 : 0;
2196
2197   /* Get info for tab orientation/position (Left, Top, Right, Bottom) */
2198   switch (aPosition)
2199     {
2200     case BF_LEFT:
2201       leftFlag = BF_TOP;
2202       topFlag = BF_LEFT;
2203       rightFlag = BF_BOTTOM;
2204       lightFlag = BF_DIAGONAL_ENDTOPRIGHT;
2205       shadeFlag = BF_DIAGONAL_ENDBOTTOMRIGHT;
2206
2207       SetRect (&topRect, R.left, R.top + lOffset, R.right,
2208                R.bottom - rOffset);
2209       SetRect (&sideRect, R.left + 2, R.top, R.right - 2 + selectedOffset,
2210                R.bottom);
2211       SetRect (&bottomRect, R.right - 2, R.top, R.right, R.bottom);
2212       SetRect (&lightRect, R.left, R.top, R.left + 3, R.top + 3);
2213       SetRect (&shadeRect, R.left + 1, R.bottom - 2, R.left + 2,
2214                R.bottom - 1);
2215       break;
2216
2217     case BF_TOP:
2218       leftFlag = BF_LEFT;
2219       topFlag = BF_TOP;
2220       rightFlag = BF_RIGHT;
2221       lightFlag = BF_DIAGONAL_ENDTOPRIGHT;
2222       shadeFlag = BF_DIAGONAL_ENDBOTTOMRIGHT;
2223
2224       SetRect (&topRect, R.left + lOffset, R.top, R.right - rOffset,
2225                R.bottom);
2226       SetRect (&sideRect, R.left, R.top + 2, R.right,
2227                R.bottom - 1 + selectedOffset);
2228       SetRect (&bottomRect, R.left, R.bottom - 1, R.right, R.bottom);
2229       SetRect (&lightRect, R.left, R.top, R.left + 3, R.top + 3);
2230       SetRect (&shadeRect, R.right - 2, R.top + 1, R.right - 1, R.top + 2);
2231       break;
2232
2233     case BF_RIGHT:
2234       leftFlag = BF_TOP;
2235       topFlag = BF_RIGHT;
2236       rightFlag = BF_BOTTOM;
2237       lightFlag = BF_DIAGONAL_ENDTOPLEFT;
2238       shadeFlag = BF_DIAGONAL_ENDBOTTOMLEFT;
2239
2240       SetRect (&topRect, R.left, R.top + lOffset, R.right,
2241                R.bottom - rOffset);
2242       SetRect (&sideRect, R.left + 2 - selectedOffset, R.top, R.right - 2,
2243                R.bottom);
2244       SetRect (&bottomRect, R.left, R.top, R.left + 2, R.bottom);
2245       SetRect (&lightRect, R.right - 3, R.top, R.right - 1, R.top + 2);
2246       SetRect (&shadeRect, R.right - 2, R.bottom - 3, R.right, R.bottom - 1);
2247       break;
2248
2249     case BF_BOTTOM:
2250       leftFlag = BF_LEFT;
2251       topFlag = BF_BOTTOM;
2252       rightFlag = BF_RIGHT;
2253       lightFlag = BF_DIAGONAL_ENDTOPLEFT;
2254       shadeFlag = BF_DIAGONAL_ENDBOTTOMLEFT;
2255
2256       SetRect (&topRect, R.left + lOffset, R.top, R.right - rOffset,
2257                R.bottom);
2258       SetRect (&sideRect, R.left, R.top + 2 - selectedOffset, R.right,
2259                R.bottom - 2);
2260       SetRect (&bottomRect, R.left, R.top, R.right, R.top + 2);
2261       SetRect (&lightRect, R.left, R.bottom - 3, R.left + 2, R.bottom - 1);
2262       SetRect (&shadeRect, R.right - 2, R.bottom - 3, R.right, R.bottom - 1);
2263       break;
2264
2265     default:
2266       g_return_if_reached ();
2267     }
2268
2269   /* Background */
2270   FillRect (hdc, &R, (HBRUSH) (COLOR_3DFACE + 1));
2271
2272   /* Tab "Top" */
2273   DrawEdge (hdc, &topRect, EDGE_RAISED, BF_SOFT | topFlag);
2274
2275   /* Tab "Bottom" */
2276   if (!aSelected)
2277     DrawEdge (hdc, &bottomRect, EDGE_RAISED, BF_SOFT | topFlag);
2278
2279   /* Tab "Sides" */
2280   if (!aDrawLeft)
2281     leftFlag = 0;
2282   if (!aDrawRight)
2283     rightFlag = 0;
2284
2285   DrawEdge (hdc, &sideRect, EDGE_RAISED, BF_SOFT | leftFlag | rightFlag);
2286
2287   /* Tab Diagonal Corners */
2288   if (aDrawLeft)
2289     DrawEdge (hdc, &lightRect, EDGE_RAISED, BF_SOFT | lightFlag);
2290
2291   if (aDrawRight)
2292     DrawEdge (hdc, &shadeRect, EDGE_RAISED, BF_SOFT | shadeFlag);
2293 }
2294
2295 static gboolean
2296 draw_themed_tab_button (GtkStyle *style,
2297                         GdkWindow *window,
2298                         GtkStateType state_type,
2299                         GtkNotebook *notebook,
2300                         gint x, gint y,
2301                         gint width, gint height, gint gap_side)
2302 {
2303   GdkPixmap *pixmap = NULL;
2304   gint border_width =
2305     gtk_container_get_border_width (GTK_CONTAINER (notebook));
2306   GtkWidget *widget = GTK_WIDGET (notebook);
2307   GdkRectangle draw_rect, clip_rect;
2308   GdkPixbufRotation rotation = GDK_PIXBUF_ROTATE_NONE;
2309   cairo_t *cr;
2310
2311   if (gap_side == GTK_POS_TOP)
2312     {
2313       int widget_right;
2314
2315       if (state_type == GTK_STATE_NORMAL)
2316         {
2317           draw_rect.x = x;
2318           draw_rect.y = y;
2319           draw_rect.width = width + 2;
2320           draw_rect.height = height;
2321
2322           clip_rect = draw_rect;
2323           clip_rect.height--;
2324         }
2325       else
2326         {
2327           draw_rect.x = x + 2;
2328           draw_rect.y = y;
2329           draw_rect.width = width - 2;
2330           draw_rect.height = height - 2;
2331           clip_rect = draw_rect;
2332         }
2333
2334       /* If we are currently drawing the right-most tab, and if that tab is the selected tab... */
2335       widget_right = widget->allocation.x + widget->allocation.width - border_width - 2;
2336
2337       if (draw_rect.x + draw_rect.width >= widget_right)
2338         {
2339           draw_rect.width = clip_rect.width = widget_right - draw_rect.x;
2340         }
2341     }
2342   if (gap_side == GTK_POS_BOTTOM)
2343     {
2344       int widget_right;
2345
2346       if (state_type == GTK_STATE_NORMAL)
2347         {
2348           draw_rect.x = x;
2349           draw_rect.y = y;
2350           draw_rect.width = width + 2;
2351           draw_rect.height = height;
2352
2353           clip_rect = draw_rect;
2354         }
2355       else
2356         {
2357           draw_rect.x = x + 2;
2358           draw_rect.y = y + 2;
2359           draw_rect.width = width - 2;
2360           draw_rect.height = height - 2;
2361           clip_rect = draw_rect;
2362         }
2363
2364       /* If we are currently drawing the right-most tab, and if that tab is the selected tab... */
2365       widget_right = widget->allocation.x + widget->allocation.width - border_width - 2;
2366
2367       if (draw_rect.x + draw_rect.width >= widget_right)
2368         {
2369           draw_rect.width = clip_rect.width = widget_right - draw_rect.x;
2370         }
2371
2372       rotation = GDK_PIXBUF_ROTATE_UPSIDEDOWN;
2373     }
2374   else if (gap_side == GTK_POS_LEFT)
2375     {
2376       int widget_bottom;
2377
2378       if (state_type == GTK_STATE_NORMAL)
2379         {
2380           draw_rect.x = x;
2381           draw_rect.y = y;
2382           draw_rect.width = width;
2383           draw_rect.height = height + 2;
2384
2385           clip_rect = draw_rect;
2386           clip_rect.width--;
2387         }
2388       else
2389         {
2390           draw_rect.x = x;
2391           draw_rect.y = y + 2;
2392           draw_rect.width = width - 2;
2393           draw_rect.height = height - 2;
2394           clip_rect = draw_rect;
2395         }
2396
2397       /* If we are currently drawing the bottom-most tab, and if that tab is the selected tab... */
2398       widget_bottom = widget->allocation.x + widget->allocation.height - border_width - 2;
2399
2400       if (draw_rect.y + draw_rect.height >= widget_bottom)
2401         {
2402           draw_rect.height = clip_rect.height = widget_bottom - draw_rect.y;
2403         }
2404
2405       rotation = GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE;
2406     }
2407   else if (gap_side == GTK_POS_RIGHT)
2408     {
2409       int widget_bottom;
2410
2411       if (state_type == GTK_STATE_NORMAL)
2412         {
2413           draw_rect.x = x + 1;
2414           draw_rect.y = y;
2415           draw_rect.width = width;
2416           draw_rect.height = height + 2;
2417
2418           clip_rect = draw_rect;
2419           clip_rect.width--;
2420         }
2421       else
2422         {
2423           draw_rect.x = x + 2;
2424           draw_rect.y = y + 2;
2425           draw_rect.width = width - 2;
2426           draw_rect.height = height - 2;
2427           clip_rect = draw_rect;
2428         }
2429
2430       /* If we are currently drawing the bottom-most tab, and if that tab is the selected tab... */
2431       widget_bottom = widget->allocation.x + widget->allocation.height - border_width - 2;
2432
2433       if (draw_rect.y + draw_rect.height >= widget_bottom)
2434         {
2435           draw_rect.height = clip_rect.height = widget_bottom - draw_rect.y;
2436         }
2437
2438       rotation = GDK_PIXBUF_ROTATE_CLOCKWISE;
2439     }
2440
2441   if (gap_side == GTK_POS_TOP)
2442     {
2443       if (!xp_theme_draw (window, XP_THEME_ELEMENT_TAB_ITEM, style,
2444                           draw_rect.x, draw_rect.y,
2445                           draw_rect.width, draw_rect.height,
2446                           state_type, &clip_rect))
2447         {
2448           return FALSE;
2449         }
2450     }
2451   else
2452     {
2453       GdkPixbuf *pixbuf;
2454       GdkPixbuf *rotated;
2455
2456       if (gap_side == GTK_POS_LEFT || gap_side == GTK_POS_RIGHT)
2457         {
2458           pixmap = gdk_pixmap_new (window, clip_rect.height, clip_rect.width, -1);
2459
2460           if (!xp_theme_draw (pixmap, XP_THEME_ELEMENT_TAB_ITEM, style,
2461                               draw_rect.y - clip_rect.y, draw_rect.x - clip_rect.x,
2462                               draw_rect.height, draw_rect.width, state_type, 0))
2463             {
2464               g_object_unref (pixmap);
2465               return FALSE;
2466             }
2467
2468           pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, NULL, 0, 0, 0, 0,
2469                                                  clip_rect.height, clip_rect.width);
2470           g_object_unref (pixmap);
2471         }
2472       else
2473         {
2474           pixmap = gdk_pixmap_new (window, clip_rect.width, clip_rect.height, -1);
2475
2476           if (!xp_theme_draw (pixmap, XP_THEME_ELEMENT_TAB_ITEM, style,
2477                               draw_rect.x - clip_rect.x, draw_rect.y - clip_rect.y,
2478                               draw_rect.width, draw_rect.height, state_type, 0))
2479             {
2480               g_object_unref (pixmap);
2481               return FALSE;
2482             }
2483
2484           pixbuf = gdk_pixbuf_get_from_drawable (NULL, pixmap, NULL, 0, 0, 0, 0,
2485                                                  clip_rect.width, clip_rect.height);
2486           g_object_unref (pixmap);
2487         }
2488
2489       rotated = gdk_pixbuf_rotate_simple (pixbuf, rotation);
2490       g_object_unref (pixbuf);
2491       pixbuf = rotated;
2492
2493       // XXX - This is really hacky and evil.  When we're drawing the left-most tab
2494       //       while it is active on a bottom-oriented notebook, there is one white
2495       //       pixel at the top.  There may be a better solution than this if someone
2496       //       has time to discover it.
2497       if (gap_side == GTK_POS_BOTTOM && state_type == GTK_STATE_NORMAL
2498           && x == widget->allocation.x)
2499         {
2500           int rowstride = gdk_pixbuf_get_rowstride (pixbuf);
2501           int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
2502           int psub = 0;
2503
2504           guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);
2505           guchar *p = pixels + rowstride;
2506
2507           for (psub = 0; psub < n_channels; psub++)
2508             {
2509               pixels[psub] = p[psub];
2510             }
2511         }
2512
2513       cr = gdk_cairo_create (window);
2514       gdk_cairo_set_source_pixbuf (cr, pixbuf, clip_rect.x, clip_rect.y);
2515       cairo_paint (cr);
2516       cairo_destroy (cr);
2517       g_object_unref (pixbuf);
2518     }
2519
2520   return TRUE;
2521 }
2522
2523 static gboolean
2524 draw_tab_button (GtkStyle *style,
2525                  GdkWindow *window,
2526                  GtkStateType state_type,
2527                  GtkShadowType shadow_type,
2528                  GdkRectangle *area,
2529                  GtkWidget *widget,
2530                  const gchar *detail,
2531                  gint x, gint y, gint width, gint height, gint gap_side)
2532 {
2533   if (gap_side == GTK_POS_TOP || gap_side == GTK_POS_BOTTOM)
2534     {
2535       /* experimental tab-drawing code from mozilla */
2536       RECT rect;
2537       XpDCInfo dc_info;
2538       HDC dc;
2539       gint32 aPosition;
2540           cairo_t *cr;
2541
2542       dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
2543           cr = gdk_cairo_create (window);
2544
2545       if (gap_side == GTK_POS_TOP)
2546         aPosition = BF_TOP;
2547       else if (gap_side == GTK_POS_BOTTOM)
2548         aPosition = BF_BOTTOM;
2549       else if (gap_side == GTK_POS_LEFT)
2550         aPosition = BF_LEFT;
2551       else
2552         aPosition = BF_RIGHT;
2553
2554       if (state_type == GTK_STATE_PRELIGHT)
2555         state_type = GTK_STATE_NORMAL;
2556       if (area)
2557         {
2558            gdk_cairo_rectangle (cr, area);
2559            cairo_clip (cr);
2560            gdk_cairo_set_source_color (cr, &style->dark[state_type]);
2561         }
2562
2563       DrawTab (dc, rect, aPosition,
2564                state_type != GTK_STATE_PRELIGHT,
2565                (gap_side != GTK_POS_LEFT), (gap_side != GTK_POS_RIGHT));
2566
2567       cairo_destroy (cr);
2568
2569       release_window_dc (&dc_info);
2570       return TRUE;
2571     }
2572
2573   return FALSE;
2574 }
2575
2576 static void
2577 draw_extension (GtkStyle *style,
2578                 GdkWindow *window,
2579                 GtkStateType state_type,
2580                 GtkShadowType shadow_type,
2581                 GdkRectangle *area,
2582                 GtkWidget *widget,
2583                 const gchar *detail,
2584                 gint x, gint y,
2585                 gint width, gint height, GtkPositionType gap_side)
2586 {
2587   if (widget && GTK_IS_NOTEBOOK (widget) && detail && !strcmp (detail, "tab"))
2588     {
2589       GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2590
2591       /* Why this differs from gap_side, I have no idea.. */
2592       int real_gap_side = gtk_notebook_get_tab_pos (notebook);
2593
2594       if (!draw_themed_tab_button (style, window, state_type,
2595                                    GTK_NOTEBOOK (widget), x, y,
2596                                    width, height, real_gap_side))
2597         {
2598           if (!draw_tab_button (style, window, state_type,
2599                                 shadow_type, area, widget,
2600                                 detail, x, y, width, height, real_gap_side))
2601             {
2602               parent_class->draw_extension (style, window, state_type,
2603                                             shadow_type, area, widget, detail,
2604                                             x, y, width, height,
2605                                             real_gap_side);
2606             }
2607         }
2608     }
2609 }
2610
2611 static void
2612 draw_box_gap (GtkStyle *style, GdkWindow *window, GtkStateType state_type,
2613               GtkShadowType shadow_type, GdkRectangle *area,
2614               GtkWidget *widget, const gchar *detail, gint x,
2615               gint y, gint width, gint height, GtkPositionType gap_side,
2616               gint gap_x, gint gap_width)
2617 {
2618   if (GTK_IS_NOTEBOOK (widget) && detail && !strcmp (detail, "notebook"))
2619     {
2620       GtkNotebook *notebook = GTK_NOTEBOOK (widget);
2621       int side = gtk_notebook_get_tab_pos (notebook);
2622       int x2 = x, y2 = y, w2 = width, h2 = height;
2623
2624       if (side == GTK_POS_TOP)
2625         {
2626           x2 = x;
2627           y2 = y - gtk_notebook_get_tab_vborder (notebook);
2628           w2 = width;
2629           h2 = height + gtk_notebook_get_tab_vborder (notebook) * 2;
2630         }
2631       else if (side == GTK_POS_BOTTOM)
2632         {
2633           x2 = x;
2634           y2 = y;
2635           w2 = width;
2636           h2 = height + gtk_notebook_get_tab_vborder (notebook) * 2;
2637         }
2638       else if (side == GTK_POS_LEFT)
2639         {
2640           x2 = x - gtk_notebook_get_tab_hborder (notebook);
2641           y2 = y;
2642           w2 = width + gtk_notebook_get_tab_hborder (notebook);
2643           h2 = height;
2644         }
2645       else if (side == GTK_POS_RIGHT)
2646         {
2647           x2 = x;
2648           y2 = y;
2649           w2 = width + gtk_notebook_get_tab_hborder (notebook) * 2;
2650           h2 = height;
2651         }
2652
2653       if (xp_theme_draw (window, XP_THEME_ELEMENT_TAB_PANE, style,
2654                          x2, y2, w2, h2, state_type, area))
2655         {
2656           return;
2657         }
2658     }
2659
2660   parent_class->draw_box_gap (style, window, state_type, shadow_type,
2661                               area, widget, detail, x, y, width, height,
2662                               gap_side, gap_x, gap_width);
2663 }
2664
2665 static gboolean
2666 is_popup_window_child (GtkWidget *widget)
2667 {
2668   GtkWidget *top;
2669   GtkWindowType type = -1;
2670
2671   top = gtk_widget_get_toplevel (widget);
2672
2673   if (top && GTK_IS_WINDOW (top))
2674     {
2675       g_object_get (top, "type", &type, NULL);
2676
2677       if (type == GTK_WINDOW_POPUP)
2678         {                       /* Hack for combo boxes */
2679           return TRUE;
2680         }
2681     }
2682
2683   return FALSE;
2684 }
2685
2686 static void
2687 draw_flat_box (GtkStyle *style, GdkWindow *window,
2688                GtkStateType state_type, GtkShadowType shadow_type,
2689                GdkRectangle *area, GtkWidget *widget,
2690                const gchar *detail, gint x, gint y, gint width, gint height)
2691 {
2692   if (detail)
2693     {
2694       if (state_type == GTK_STATE_SELECTED &&
2695           (!strncmp ("cell_even", detail, 9) || !strncmp ("cell_odd", detail, 8)))
2696         {
2697           GdkColor *gc = gtk_widget_has_focus (widget) ? &style->base[state_type] : &style->base[GTK_STATE_ACTIVE];
2698           cairo_t *cr = gdk_cairo_create (window);
2699
2700           _cairo_draw_rectangle (cr, gc, TRUE, x, y, width, height);
2701
2702                   cairo_destroy (cr);
2703
2704           return;
2705         }
2706       else if (!strcmp (detail, "checkbutton"))
2707         {
2708           if (state_type == GTK_STATE_PRELIGHT)
2709             {
2710               return;
2711             }
2712         }
2713     }
2714
2715   parent_class->draw_flat_box (style, window, state_type, shadow_type,
2716                                area, widget, detail, x, y, width, height);
2717 }
2718
2719 static gboolean
2720 draw_menu_border (GdkWindow *win, GtkStyle *style,
2721                   gint x, gint y, gint width, gint height)
2722 {
2723   RECT rect;
2724   XpDCInfo dc_info;
2725   HDC dc;
2726
2727   dc = get_window_dc (style, win, GTK_STATE_NORMAL, &dc_info, x, y, width, height, &rect);
2728
2729   if (!dc)
2730     return FALSE;
2731
2732   if (xp_theme_is_active ())
2733     {
2734       FrameRect (dc, &rect, GetSysColorBrush (COLOR_3DSHADOW));
2735     }
2736   else
2737     {
2738       DrawEdge (dc, &rect, EDGE_RAISED, BF_RECT);
2739     }
2740
2741   release_window_dc (&dc_info);
2742
2743   return TRUE;
2744 }
2745
2746 static void
2747 draw_shadow (GtkStyle *style,
2748              GdkWindow *window,
2749              GtkStateType state_type,
2750              GtkShadowType shadow_type,
2751              GdkRectangle *area,
2752              GtkWidget *widget,
2753              const gchar *detail, gint x, gint y, gint width, gint height)
2754 {
2755   gboolean is_handlebox;
2756   gboolean is_toolbar;
2757
2758   if (detail && !strcmp (detail, "frame"))
2759     {
2760
2761       HDC dc;
2762       RECT rect;
2763       XpDCInfo dc_info;
2764
2765
2766
2767       dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
2768       if (is_combo_box_child (widget))
2769         {
2770           FillRect (dc, &rect, GetSysColorBrush (COLOR_WINDOW));
2771         }
2772       else if (is_popup_window_child (widget))
2773         {
2774           FrameRect (dc, &rect, GetSysColorBrush (COLOR_WINDOWFRAME));
2775         }
2776       else
2777         {
2778           switch (shadow_type)
2779             {
2780             case GTK_SHADOW_IN:
2781               draw_3d_border (dc, &rect, TRUE);
2782               break;
2783
2784             case GTK_SHADOW_OUT:
2785               draw_3d_border (dc, &rect, FALSE);
2786               break;
2787
2788             case GTK_SHADOW_ETCHED_IN:
2789               draw_3d_border (dc, &rect, TRUE);
2790               InflateRect (&rect, -1, -1);
2791               draw_3d_border (dc, &rect, FALSE);
2792               break;
2793
2794             case GTK_SHADOW_ETCHED_OUT:
2795               draw_3d_border (dc, &rect, FALSE);
2796               InflateRect (&rect, -1, -1);
2797               draw_3d_border (dc, &rect, TRUE);
2798               break;
2799
2800             case GTK_SHADOW_NONE:
2801               break;
2802             }
2803         }
2804
2805       release_window_dc (&dc_info);
2806
2807       return;
2808     }
2809   if (detail && (!strcmp (detail, "entry") || !strcmp (detail, "combobox")))
2810     {
2811       if (shadow_type != GTK_SHADOW_IN)
2812         return;
2813
2814       if (!xp_theme_draw (window, XP_THEME_ELEMENT_EDIT_TEXT, style,
2815                           x, y, width, height, state_type, area))
2816         {
2817           HDC dc;
2818           RECT rect;
2819           XpDCInfo dc_info;
2820
2821           dc = get_window_dc (style, window, state_type, &dc_info,
2822                               x, y, width, height, &rect);
2823
2824           DrawEdge (dc, &rect, EDGE_SUNKEN, BF_RECT);
2825           release_window_dc (&dc_info);
2826         }
2827
2828       return;
2829     }
2830
2831   if (detail && !strcmp (detail, "scrolled_window") &&
2832       xp_theme_draw (window, XP_THEME_ELEMENT_EDIT_TEXT, style,
2833                      x, y, width, height, state_type, area))
2834     {
2835       return;
2836     }
2837
2838   if (detail && !strcmp (detail, "spinbutton"))
2839     return;
2840
2841   if (detail && !strcmp (detail, "menu"))
2842     {
2843       if (draw_menu_border (window, style, x, y, width, height))
2844         {
2845           return;
2846         }
2847     }
2848
2849   if (detail && !strcmp (detail, "handlebox"))
2850     return;
2851
2852   is_handlebox = (detail && !strcmp (detail, "handlebox_bin"));
2853   is_toolbar = (detail
2854                 && (!strcmp (detail, "toolbar")
2855                     || !strcmp (detail, "menubar")));
2856
2857   if (is_toolbar || is_handlebox)
2858     {
2859       if (shadow_type == GTK_SHADOW_NONE)
2860         {
2861           return;
2862         }
2863
2864       if (widget)
2865         {
2866           HDC dc;
2867           RECT rect;
2868           XpDCInfo dc_info;
2869           HGDIOBJ old_pen = NULL;
2870           GtkPositionType pos;
2871
2872           sanitize_size (window, &width, &height);
2873
2874           if (is_handlebox)
2875             {
2876               pos = gtk_handle_box_get_handle_position (GTK_HANDLE_BOX (widget));
2877               /*
2878                  If the handle box is at left side,
2879                  we shouldn't draw its right border.
2880                  The same holds true for top, right, and bottom.
2881                */
2882               switch (pos)
2883                 {
2884                 case GTK_POS_LEFT:
2885                   pos = GTK_POS_RIGHT;
2886                   break;
2887
2888                 case GTK_POS_RIGHT:
2889                   pos = GTK_POS_LEFT;
2890                   break;
2891
2892                 case GTK_POS_TOP:
2893                   pos = GTK_POS_BOTTOM;
2894                   break;
2895
2896                 case GTK_POS_BOTTOM:
2897                   pos = GTK_POS_TOP;
2898                   break;
2899                 }
2900             }
2901           else
2902             {
2903               GtkWidget *parent = gtk_widget_get_parent (widget);
2904
2905               /* Dirty hack for toolbars contained in handle boxes */
2906               if (GTK_IS_HANDLE_BOX (parent))
2907                 {
2908                   pos = gtk_handle_box_get_handle_position (GTK_HANDLE_BOX (parent));
2909                 }
2910               else
2911                 {
2912                   /*
2913                      Dirty hack:
2914                      Make pos != all legal enum vaules of GtkPositionType.
2915                      So every border will be draw.
2916                    */
2917                   pos = (GtkPositionType) - 1;
2918                 }
2919             }
2920
2921           dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
2922
2923           if (pos != GTK_POS_LEFT)
2924             {
2925               old_pen = SelectObject (dc, get_light_pen ());
2926               MoveToEx (dc, rect.left, rect.top, NULL);
2927               LineTo (dc, rect.left, rect.bottom);
2928             }
2929           if (pos != GTK_POS_TOP)
2930             {
2931               old_pen = SelectObject (dc, get_light_pen ());
2932               MoveToEx (dc, rect.left, rect.top, NULL);
2933               LineTo (dc, rect.right, rect.top);
2934             }
2935           if (pos != GTK_POS_RIGHT)
2936             {
2937               old_pen = SelectObject (dc, get_dark_pen ());
2938               MoveToEx (dc, rect.right - 1, rect.top, NULL);
2939               LineTo (dc, rect.right - 1, rect.bottom);
2940             }
2941           if (pos != GTK_POS_BOTTOM)
2942             {
2943               old_pen = SelectObject (dc, get_dark_pen ());
2944               MoveToEx (dc, rect.left, rect.bottom - 1, NULL);
2945               LineTo (dc, rect.right, rect.bottom - 1);
2946             }
2947           if (old_pen)
2948             SelectObject (dc, old_pen);
2949           release_window_dc (&dc_info);
2950         }
2951
2952       return;
2953     }
2954
2955   if (detail && !strcmp (detail, "statusbar"))
2956     {
2957       return;
2958     }
2959
2960   parent_class->draw_shadow (style, window, state_type, shadow_type, area,
2961                              widget, detail, x, y, width, height);
2962 }
2963
2964 static void
2965 draw_hline (GtkStyle *style,
2966             GdkWindow *window,
2967             GtkStateType state_type,
2968             GdkRectangle *area,
2969             GtkWidget *widget,
2970             const gchar *detail, gint x1, gint x2, gint y)
2971 {
2972   cairo_t *cr;
2973   
2974   cr = gdk_cairo_create (window);
2975
2976   if (xp_theme_is_active () && detail && !strcmp (detail, "menuitem"))
2977     {
2978       gint cx, cy;
2979       gint new_y, new_height;
2980       gint y_offset;
2981
2982       xp_theme_get_element_dimensions (XP_THEME_ELEMENT_MENU_SEPARATOR,
2983                                        state_type,
2984                                        &cx, &cy);
2985
2986       /* Center the separator */
2987       y_offset = (area->height / 2) - (cy / 2);
2988       new_y = y_offset >= 0 ? area->y + y_offset : area->y;
2989       new_height = cy;
2990
2991       if (xp_theme_draw
2992           (window, XP_THEME_ELEMENT_MENU_SEPARATOR, style, x1, new_y, x2, new_height,
2993            state_type, area))
2994         {
2995           return;
2996         }
2997       else
2998         {
2999           if (area)
3000             {
3001               gdk_cairo_rectangle (cr, area);
3002               cairo_clip (cr);
3003             }
3004
3005           _cairo_draw_line (cr, &style->dark[state_type], x1, y, x2, y);
3006
3007         }
3008     }
3009   else
3010     {
3011       if (style->ythickness == 2)
3012         {
3013           if (area)
3014             {
3015               gdk_cairo_rectangle (cr, area);
3016               cairo_clip (cr);
3017             }
3018
3019           _cairo_draw_line (cr, &style->dark[state_type], x1, y, x2, y);
3020           ++y;
3021           _cairo_draw_line (cr, &style->light[state_type], x1, y, x2, y);
3022
3023         }
3024       else
3025         {
3026           parent_class->draw_hline (style, window, state_type, area, widget,
3027                                     detail, x1, x2, y);
3028         }
3029     }
3030   cairo_destroy (cr);
3031 }
3032
3033 static void
3034 draw_vline (GtkStyle *style,
3035             GdkWindow *window,
3036             GtkStateType state_type,
3037             GdkRectangle *area,
3038             GtkWidget *widget,
3039             const gchar *detail, gint y1, gint y2, gint x)
3040 {
3041   cairo_t *cr;
3042   
3043   cr = gdk_cairo_create (window);
3044
3045   if (style->xthickness == 2)
3046     {
3047       if (area)
3048         {
3049               gdk_cairo_rectangle (cr, area);
3050               cairo_clip (cr);
3051         }
3052
3053       _cairo_draw_line (cr, &style->dark[state_type], x, y1, x, y2);
3054       ++x;
3055       _cairo_draw_line (cr, &style->light[state_type], x, y1, x, y2);
3056
3057     }
3058   else
3059     {
3060       parent_class->draw_vline (style, window, state_type, area, widget,
3061                                 detail, y1, y2, x);
3062     }
3063
3064   cairo_destroy (cr);
3065 }
3066
3067 static void
3068 draw_slider (GtkStyle *style,
3069              GdkWindow *window,
3070              GtkStateType state_type,
3071              GtkShadowType shadow_type,
3072              GdkRectangle *area,
3073              GtkWidget *widget,
3074              const gchar *detail,
3075              gint x,
3076              gint y, gint width, gint height, GtkOrientation orientation)
3077 {
3078   if (GTK_IS_SCALE (widget) &&
3079       xp_theme_draw (window, ((orientation == GTK_ORIENTATION_VERTICAL) ?
3080                               XP_THEME_ELEMENT_SCALE_SLIDER_V :
3081                               XP_THEME_ELEMENT_SCALE_SLIDER_H), style, x, y, width,
3082                      height, state_type, area))
3083     {
3084       return;
3085     }
3086
3087   parent_class->draw_slider (style, window, state_type, shadow_type, area,
3088                              widget, detail, x, y, width, height,
3089                              orientation);
3090 }
3091
3092 static void
3093 draw_resize_grip (GtkStyle *style,
3094                   GdkWindow *window,
3095                   GtkStateType state_type,
3096                   GdkRectangle *area,
3097                   GtkWidget *widget,
3098                   const gchar *detail,
3099                   GdkWindowEdge edge, gint x, gint y, gint width, gint height)
3100 {
3101   cairo_t *cr;
3102   
3103   cr = gdk_cairo_create (window);
3104   
3105   if (detail && !strcmp (detail, "statusbar"))
3106     {
3107       if (xp_theme_draw
3108           (window, XP_THEME_ELEMENT_STATUS_GRIPPER, style, x, y, width,
3109            height, state_type, area))
3110         {
3111           cairo_destroy (cr);
3112           return;
3113         }
3114       else
3115         {
3116           RECT rect;
3117           XpDCInfo dc_info;
3118           HDC dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
3119
3120           if (area)
3121             {
3122               gdk_cairo_rectangle (cr, area);
3123               cairo_clip (cr);
3124               gdk_cairo_set_source_color (cr, &style->dark[state_type]);
3125             }
3126
3127           DrawFrameControl (dc, &rect, DFC_SCROLL, DFCS_SCROLLSIZEGRIP);
3128           release_window_dc (&dc_info);
3129
3130           cairo_destroy (cr);
3131           return;
3132         }
3133     }
3134
3135   parent_class->draw_resize_grip (style, window, state_type, area,
3136                                   widget, detail, edge, x, y, width, height);
3137 }
3138
3139 static void
3140 draw_handle (GtkStyle *style,
3141              GdkWindow *window,
3142              GtkStateType state_type,
3143              GtkShadowType shadow_type,
3144              GdkRectangle *area,
3145              GtkWidget *widget,
3146              const gchar *detail,
3147              gint x,
3148              gint y, gint width, gint height, GtkOrientation orientation)
3149 {
3150   HDC dc;
3151   RECT rect;
3152   XpDCInfo dc_info;
3153   cairo_t *cr;
3154   
3155   cr = gdk_cairo_create (window);
3156
3157   if (is_toolbar_child (widget))
3158     {
3159       XpThemeElement hndl;
3160
3161       sanitize_size (window, &width, &height);
3162
3163       if (GTK_IS_HANDLE_BOX (widget))
3164         {
3165           GtkPositionType pos;
3166           pos = gtk_handle_box_get_handle_position (GTK_HANDLE_BOX (widget));
3167
3168           if (pos == GTK_POS_TOP || pos == GTK_POS_BOTTOM)
3169             {
3170               orientation = GTK_ORIENTATION_HORIZONTAL;
3171             }
3172           else
3173             {
3174               orientation = GTK_ORIENTATION_VERTICAL;
3175             }
3176         }
3177
3178       if (orientation == GTK_ORIENTATION_VERTICAL)
3179         hndl = XP_THEME_ELEMENT_REBAR_GRIPPER_V;
3180       else
3181         hndl = XP_THEME_ELEMENT_REBAR_GRIPPER_H;
3182
3183       if (xp_theme_draw (window, hndl, style, x, y, width, height,
3184                          state_type, area))
3185         {
3186           return;
3187         }
3188
3189       dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
3190
3191       if (orientation == GTK_ORIENTATION_VERTICAL)
3192         {
3193           rect.left += 3;
3194           rect.right = rect.left + 3;
3195           rect.bottom -= 3;
3196           rect.top += 3;
3197         }
3198       else
3199         {
3200           rect.top += 3;
3201           rect.bottom = rect.top + 3;
3202           rect.right -= 3;
3203           rect.left += 3;
3204         }
3205
3206       draw_3d_border (dc, &rect, FALSE);
3207       release_window_dc (&dc_info);
3208       return;
3209     }
3210
3211   if (!GTK_IS_PANED (widget))
3212     {
3213       gint xthick, ythick;
3214       GdkColor *light, *dark, *shadow;
3215       GdkRectangle dest;
3216
3217       sanitize_size (window, &width, &height);
3218
3219       gtk_paint_box (style, window, state_type, shadow_type, area,
3220                      widget, detail, x, y, width, height);
3221
3222       light = &style->light[state_type];
3223       dark = &style->dark[state_type];
3224       shadow = &style->mid[state_type];
3225
3226       xthick = style->xthickness;
3227       ythick = style->ythickness;
3228
3229       dest.x = x + xthick;
3230       dest.y = y + ythick;
3231       dest.width = width - (xthick * 2);
3232       dest.height = height - (ythick * 2);
3233
3234       if (dest.width < dest.height)
3235         dest.x += 2;
3236       else
3237         dest.y += 2;
3238
3239       gdk_cairo_rectangle (cr, &dest);
3240       cairo_clip (cr);
3241
3242       if (dest.width < dest.height)
3243         {
3244           _cairo_draw_line (cr, light, dest.x, dest.y, dest.x,
3245                          dest.height);
3246           _cairo_draw_line (cr, dark, dest.x + (dest.width / 2),
3247                          dest.y, dest.x + (dest.width / 2), dest.height);
3248           _cairo_draw_line (cr, shadow, dest.x + dest.width,
3249                          dest.y, dest.x + dest.width, dest.height);
3250         }
3251       else
3252         {
3253           _cairo_draw_line (cr, light, dest.x, dest.y,
3254                          dest.x + dest.width, dest.y);
3255           _cairo_draw_line (cr, dark, dest.x,
3256                          dest.y + (dest.height / 2),
3257                          dest.x + dest.width, dest.y + (dest.height / 2));
3258           _cairo_draw_line (cr, shadow, dest.x,
3259                          dest.y + dest.height, dest.x + dest.width,
3260                          dest.y + dest.height);
3261         }
3262
3263       cairo_destroy (cr);
3264     }
3265 }
3266
3267 static void
3268 draw_focus (GtkStyle *style,
3269             GdkWindow *window,
3270             GtkStateType state_type,
3271             GdkRectangle *area,
3272             GtkWidget *widget,
3273             const gchar *detail, gint x, gint y, gint width, gint height)
3274 {
3275   HDC dc;
3276   RECT rect;
3277   XpDCInfo dc_info;
3278
3279   if (!gtk_widget_get_can_focus (widget))
3280     {
3281       return;
3282     }
3283
3284   if (is_combo_box_child (widget)
3285       && (GTK_IS_ARROW (widget) || GTK_IS_BUTTON (widget)))
3286     {
3287       return;
3288     }
3289   if (GTK_IS_TREE_VIEW (widget->parent) /* list view bheader */)
3290     {
3291       return;
3292     }
3293
3294   dc = get_window_dc (style, window, state_type, &dc_info, x, y, width, height, &rect);
3295   DrawFocusRect (dc, &rect);
3296   release_window_dc (&dc_info);
3297 /*
3298     parent_class->draw_focus (style, window, state_type,
3299                                                      area, widget, detail, x, y, width, height);
3300 */
3301 }
3302
3303 static void
3304 draw_layout (GtkStyle *style,
3305              GdkWindow *window,
3306              GtkStateType state_type,
3307              gboolean use_text,
3308              GdkRectangle *area,
3309              GtkWidget *widget,
3310              const gchar *detail,
3311              gint old_x, gint old_y, PangoLayout *layout)
3312 {
3313   GtkNotebook *notebook = NULL;
3314   gint x = old_x;
3315   gint y = old_y;
3316
3317   /* In the XP theme, labels don't appear correctly centered inside
3318    * notebook tabs, so we give them a gentle nudge two pixels to the
3319    * right.  A little hackish, but what are 'ya gonna do?  -- Cody
3320    */
3321   if (xp_theme_is_active () && detail && !strcmp (detail, "label"))
3322     {
3323       if (widget->parent != NULL)
3324         {
3325           if (GTK_IS_NOTEBOOK (widget->parent))
3326             {
3327               int side;
3328               notebook = GTK_NOTEBOOK (widget->parent);
3329               side = gtk_notebook_get_tab_pos (notebook);
3330
3331               if (side == GTK_POS_TOP || side == GTK_POS_BOTTOM)
3332                 {
3333                   x += 2;
3334                 }
3335             }
3336         }
3337     }
3338
3339   parent_class->draw_layout (style, window, state_type,
3340                              use_text, area, widget, detail, x, y, layout);
3341 }
3342
3343 static void
3344 msw_style_init_from_rc (GtkStyle *style, GtkRcStyle *rc_style)
3345 {
3346   setup_system_font (style);
3347   setup_menu_settings (gtk_settings_get_default ());
3348   setup_system_styles (style);
3349   parent_class->init_from_rc (style, rc_style);
3350 }
3351
3352 static void
3353 msw_style_class_init (MswStyleClass *klass)
3354 {
3355   GtkStyleClass *style_class = GTK_STYLE_CLASS (klass);
3356
3357   parent_class = g_type_class_peek_parent (klass);
3358
3359   style_class->init_from_rc = msw_style_init_from_rc;
3360   style_class->draw_arrow = draw_arrow;
3361   style_class->draw_box = draw_box;
3362   style_class->draw_check = draw_check;
3363   style_class->draw_option = draw_option;
3364   style_class->draw_tab = draw_tab;
3365   style_class->draw_flat_box = draw_flat_box;
3366   style_class->draw_expander = draw_expander;
3367   style_class->draw_extension = draw_extension;
3368   style_class->draw_box_gap = draw_box_gap;
3369   style_class->draw_shadow = draw_shadow;
3370   style_class->draw_hline = draw_hline;
3371   style_class->draw_vline = draw_vline;
3372   style_class->draw_handle = draw_handle;
3373   style_class->draw_resize_grip = draw_resize_grip;
3374   style_class->draw_slider = draw_slider;
3375   style_class->draw_focus = draw_focus;
3376   style_class->draw_layout = draw_layout;
3377 }
3378
3379 GType msw_type_style = 0;
3380
3381 void
3382 msw_style_register_type (GTypeModule *module)
3383 {
3384   const GTypeInfo object_info = {
3385     sizeof (MswStyleClass),
3386     (GBaseInitFunc) NULL,
3387     (GBaseFinalizeFunc) NULL,
3388     (GClassInitFunc) msw_style_class_init,
3389     NULL,                       /* class_finalize */
3390     NULL,                       /* class_data */
3391     sizeof (MswStyle),
3392     0,                          /* n_preallocs */
3393     (GInstanceInitFunc) NULL,
3394   };
3395
3396   msw_type_style = g_type_module_register_type (module,
3397                                                 GTK_TYPE_STYLE,
3398                                                 "MswStyle", &object_info, 0);
3399 }
3400
3401 void
3402 msw_style_init (void)
3403 {
3404   xp_theme_init ();
3405   msw_style_setup_system_settings ();
3406   setup_msw_rc_style ();
3407
3408   if (g_light_pen)
3409     {
3410       DeleteObject (g_light_pen);
3411       g_light_pen = NULL;
3412     }
3413
3414   if (g_dark_pen)
3415     {
3416       DeleteObject (g_dark_pen);
3417       g_dark_pen = NULL;
3418     }
3419 }
3420
3421 void
3422 msw_style_finalize (void)
3423 {
3424   if (g_dither_brush)
3425     {
3426       DeleteObject (g_dither_brush);
3427     }
3428
3429   if (g_light_pen)
3430     {
3431       DeleteObject (g_light_pen);
3432     }
3433
3434   if (g_dark_pen)
3435     {
3436       DeleteObject (g_dark_pen);
3437     }
3438 }