]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyle.c
Draw focus indication in the color wheel using standard focus style.
[~andy/gtk] / gtk / gtkstyle.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <config.h>
28 #include <math.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "gtkgc.h"
32 #include "gtkmarshalers.h"
33 #include "gtkrc.h"
34 #include "gtkspinbutton.h"
35 #include "gtkstyle.h"
36 #include "gtkwidget.h"
37 #include "gtkthemes.h"
38 #include "gtkiconfactory.h"
39 #include "gtksettings.h"        /* _gtk_settings_parse_convert() */
40
41 #define LIGHTNESS_MULT  1.3
42 #define DARKNESS_MULT   0.7
43
44 /* --- typedefs & structures --- */
45 typedef struct {
46   GType       widget_type;
47   GParamSpec *pspec;
48   GValue      value;
49 } PropertyValue;
50
51 /* --- prototypes --- */
52 static void      gtk_style_init                 (GtkStyle       *style);
53 static void      gtk_style_class_init           (GtkStyleClass  *klass);
54 static void      gtk_style_finalize             (GObject        *object);
55 static void      gtk_style_realize              (GtkStyle       *style,
56                                                  GdkColormap    *colormap);
57 static void      gtk_style_real_realize        (GtkStyle        *style);
58 static void      gtk_style_real_unrealize      (GtkStyle        *style);
59 static void      gtk_style_real_copy           (GtkStyle        *style,
60                                                 GtkStyle        *src);
61 static void      gtk_style_real_set_background (GtkStyle        *style,
62                                                 GdkWindow       *window,
63                                                 GtkStateType     state_type);
64 static GtkStyle *gtk_style_real_clone          (GtkStyle        *style);
65 static void      gtk_style_real_init_from_rc   (GtkStyle        *style,
66                                                 GtkRcStyle      *rc_style);
67 static GdkPixbuf *gtk_default_render_icon      (GtkStyle            *style,
68                                                 const GtkIconSource *source,
69                                                 GtkTextDirection     direction,
70                                                 GtkStateType         state,
71                                                 GtkIconSize          size,
72                                                 GtkWidget           *widget,
73                                                 const gchar         *detail);
74 static void gtk_default_draw_hline      (GtkStyle        *style,
75                                          GdkWindow       *window,
76                                          GtkStateType     state_type,
77                                          GdkRectangle    *area,
78                                          GtkWidget       *widget,
79                                          const gchar     *detail,
80                                          gint             x1,
81                                          gint             x2,
82                                          gint             y);
83 static void gtk_default_draw_vline      (GtkStyle        *style,
84                                          GdkWindow       *window,
85                                          GtkStateType     state_type,
86                                          GdkRectangle    *area,
87                                          GtkWidget       *widget,
88                                          const gchar     *detail,
89                                          gint             y1,
90                                          gint             y2,
91                                          gint             x);
92 static void gtk_default_draw_shadow     (GtkStyle        *style,
93                                          GdkWindow       *window,
94                                          GtkStateType     state_type,
95                                          GtkShadowType    shadow_type,
96                                          GdkRectangle    *area,
97                                          GtkWidget       *widget,
98                                          const gchar     *detail,
99                                          gint             x,
100                                          gint             y,
101                                          gint             width,
102                                          gint             height);
103 static void gtk_default_draw_polygon    (GtkStyle        *style,
104                                          GdkWindow       *window,
105                                          GtkStateType     state_type,
106                                          GtkShadowType    shadow_type,
107                                          GdkRectangle    *area,
108                                          GtkWidget       *widget,
109                                          const gchar     *detail,
110                                          GdkPoint        *points,
111                                          gint             npoints,
112                                          gboolean         fill);
113 static void gtk_default_draw_arrow      (GtkStyle        *style,
114                                          GdkWindow       *window,
115                                          GtkStateType     state_type,
116                                          GtkShadowType    shadow_type,
117                                          GdkRectangle    *area,
118                                          GtkWidget       *widget,
119                                          const gchar     *detail,
120                                          GtkArrowType     arrow_type,
121                                          gboolean         fill,
122                                          gint             x,
123                                          gint             y,
124                                          gint             width,
125                                          gint             height);
126 static void gtk_default_draw_diamond    (GtkStyle        *style,
127                                          GdkWindow       *window,
128                                          GtkStateType     state_type,
129                                          GtkShadowType    shadow_type,
130                                          GdkRectangle    *area,
131                                          GtkWidget       *widget,
132                                          const gchar     *detail,
133                                          gint             x,
134                                          gint             y,
135                                          gint             width,
136                                          gint             height);
137 static void gtk_default_draw_string     (GtkStyle        *style,
138                                          GdkWindow       *window,
139                                          GtkStateType     state_type,
140                                          GdkRectangle    *area,
141                                          GtkWidget       *widget,
142                                          const gchar     *detail,
143                                          gint             x,
144                                          gint             y,
145                                          const gchar     *string);
146 static void gtk_default_draw_box        (GtkStyle        *style,
147                                          GdkWindow       *window,
148                                          GtkStateType     state_type,
149                                          GtkShadowType    shadow_type,
150                                          GdkRectangle    *area,
151                                          GtkWidget       *widget,
152                                          const gchar     *detail,
153                                          gint             x,
154                                          gint             y,
155                                          gint             width,
156                                          gint             height);
157 static void gtk_default_draw_flat_box   (GtkStyle        *style,
158                                          GdkWindow       *window,
159                                          GtkStateType     state_type,
160                                          GtkShadowType    shadow_type,
161                                          GdkRectangle    *area,
162                                          GtkWidget       *widget,
163                                          const gchar     *detail,
164                                          gint             x,
165                                          gint             y,
166                                          gint             width,
167                                          gint             height);
168 static void gtk_default_draw_check      (GtkStyle        *style,
169                                          GdkWindow       *window,
170                                          GtkStateType     state_type,
171                                          GtkShadowType    shadow_type,
172                                          GdkRectangle    *area,
173                                          GtkWidget       *widget,
174                                          const gchar     *detail,
175                                          gint             x,
176                                          gint             y,
177                                          gint             width,
178                                          gint             height);
179 static void gtk_default_draw_option     (GtkStyle        *style,
180                                          GdkWindow       *window,
181                                          GtkStateType     state_type,
182                                          GtkShadowType    shadow_type,
183                                          GdkRectangle    *area,
184                                          GtkWidget       *widget,
185                                          const gchar     *detail,
186                                          gint             x,
187                                          gint             y,
188                                          gint             width,
189                                          gint             height);
190 static void gtk_default_draw_tab        (GtkStyle        *style,
191                                          GdkWindow       *window,
192                                          GtkStateType     state_type,
193                                          GtkShadowType    shadow_type,
194                                          GdkRectangle    *area,
195                                          GtkWidget       *widget,
196                                          const gchar     *detail,
197                                          gint             x,
198                                          gint             y,
199                                          gint             width,
200                                          gint             height);
201 static void gtk_default_draw_shadow_gap (GtkStyle        *style,
202                                          GdkWindow       *window,
203                                          GtkStateType     state_type,
204                                          GtkShadowType    shadow_type,
205                                          GdkRectangle    *area,
206                                          GtkWidget       *widget,
207                                          const gchar     *detail,
208                                          gint             x,
209                                          gint             y,
210                                          gint             width,
211                                          gint             height,
212                                          GtkPositionType  gap_side,
213                                          gint             gap_x,
214                                          gint             gap_width);
215 static void gtk_default_draw_box_gap    (GtkStyle        *style,
216                                          GdkWindow       *window,
217                                          GtkStateType     state_type,
218                                          GtkShadowType    shadow_type,
219                                          GdkRectangle    *area,
220                                          GtkWidget       *widget,
221                                          const gchar     *detail,
222                                          gint             x,
223                                          gint             y,
224                                          gint             width,
225                                          gint             height,
226                                          GtkPositionType  gap_side,
227                                          gint             gap_x,
228                                          gint             gap_width);
229 static void gtk_default_draw_extension  (GtkStyle        *style,
230                                          GdkWindow       *window,
231                                          GtkStateType     state_type,
232                                          GtkShadowType    shadow_type,
233                                          GdkRectangle    *area,
234                                          GtkWidget       *widget,
235                                          const gchar     *detail,
236                                          gint             x,
237                                          gint             y,
238                                          gint             width,
239                                          gint             height,
240                                          GtkPositionType  gap_side);
241 static void gtk_default_draw_focus      (GtkStyle        *style,
242                                          GdkWindow       *window,
243                                          GtkStateType     state_type,
244                                          GdkRectangle    *area,
245                                          GtkWidget       *widget,
246                                          const gchar     *detail,
247                                          gint             x,
248                                          gint             y,
249                                          gint             width,
250                                          gint             height);
251 static void gtk_default_draw_slider     (GtkStyle        *style,
252                                          GdkWindow       *window,
253                                          GtkStateType     state_type,
254                                          GtkShadowType    shadow_type,
255                                          GdkRectangle    *area,
256                                          GtkWidget       *widget,
257                                          const gchar     *detail,
258                                          gint             x,
259                                          gint             y,
260                                          gint             width,
261                                          gint             height,
262                                          GtkOrientation   orientation);
263 static void gtk_default_draw_handle     (GtkStyle        *style,
264                                          GdkWindow       *window,
265                                          GtkStateType     state_type,
266                                          GtkShadowType    shadow_type,
267                                          GdkRectangle    *area,
268                                          GtkWidget       *widget,
269                                          const gchar     *detail,
270                                          gint             x,
271                                          gint             y,
272                                          gint             width,
273                                          gint             height,
274                                          GtkOrientation   orientation);
275 static void gtk_default_draw_expander   (GtkStyle        *style,
276                                          GdkWindow       *window,
277                                          GtkStateType     state_type,
278                                          GdkRectangle    *area,
279                                          GtkWidget       *widget,
280                                          const gchar     *detail,
281                                          gint             x,
282                                          gint             y,
283                                          GtkExpanderStyle expander_style);
284 static void gtk_default_draw_layout     (GtkStyle        *style,
285                                          GdkWindow       *window,
286                                          GtkStateType     state_type,
287                                          gboolean         use_text,
288                                          GdkRectangle    *area,
289                                          GtkWidget       *widget,
290                                          const gchar     *detail,
291                                          gint             x,
292                                          gint             y,
293                                          PangoLayout     *layout);
294 static void gtk_default_draw_resize_grip (GtkStyle       *style,
295                                           GdkWindow      *window,
296                                           GtkStateType    state_type,
297                                           GdkRectangle   *area,
298                                           GtkWidget      *widget,
299                                           const gchar    *detail,
300                                           GdkWindowEdge   edge,
301                                           gint            x,
302                                           gint            y,
303                                           gint            width,
304                                           gint            height);
305
306 static void gtk_style_shade             (GdkColor        *a,
307                                          GdkColor        *b,
308                                          gdouble          k);
309 static void rgb_to_hls                  (gdouble         *r,
310                                          gdouble         *g,
311                                          gdouble         *b);
312 static void hls_to_rgb                  (gdouble         *h,
313                                          gdouble         *l,
314                                          gdouble         *s);
315
316 static void style_unrealize_cursor_gcs (GtkStyle *style);
317
318 static GdkFont *gtk_style_get_font_internal (GtkStyle *style);
319
320 /*
321  * Data for default check and radio buttons
322  */
323
324 static const GtkRequisition default_option_indicator_size = { 7, 13 };
325 static const GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };
326
327 #define INDICATOR_PART_SIZE 13
328
329 typedef enum {
330   CHECK_AA,
331   CHECK_BASE,
332   CHECK_BLACK,
333   CHECK_DARK,
334   CHECK_LIGHT,
335   CHECK_MID,
336   CHECK_TEXT,
337   CHECK_INCONSISTENT_TEXT,
338   RADIO_BASE,
339   RADIO_BLACK,
340   RADIO_DARK,
341   RADIO_LIGHT,
342   RADIO_MID,
343   RADIO_TEXT,
344   RADIO_INCONSISTENT_AA,
345   RADIO_INCONSISTENT_TEXT
346 } IndicatorPart;
347
348 /*
349  * Extracted from check-13.png, width=13, height=13
350  */
351 static const guchar check_black_bits[] = {
352   0x00,0x00,0xfe,0x0f,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
353   0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x00};
354 static const guchar check_dark_bits[] = {
355   0xff,0x1f,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
356   0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x00,0x00};
357 static const guchar check_mid_bits[] = {
358   0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
359   0x08,0x00,0x08,0x00,0x08,0x00,0x08,0xfc,0x0f,0x00,0x00,0x00,0x00};
360 static const guchar check_light_bits[] = {
361   0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
362   0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0xfe,0x1f,0x00,0x00};
363 static const guchar check_text_bits[] = {
364   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x03,0x80,0x01,0x80,0x00,0x58,
365   0x00,0x60,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
366 static const guchar check_aa_bits[] = {
367   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x58,0x00,0xa0,
368   0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
369 static const guchar check_base_bits[] = {
370   0x00,0x00,0x00,0x00,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
371   0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0x00,0x00,0x00,0x00,0x00,0x00};
372
373 /*
374  * Extracted from check-13-inconsistent.png, width=13, height=13
375  */
376 static const guchar check_inconsistent_text_bits[] = {
377   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x03,0xf8,
378   0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
379 #if 0
380 /*
381  * check_inconsistent_aa_bits is currently not used, since it is all zeros.
382  */
383 static const guchar check_inconsistent_aa_bits[] = {
384   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
385   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
386 #endif
387
388 /*
389  * Extracted from radio-13.png, width=13, height=13
390  */
391 static const guchar radio_black_bits[] = {
392   0x00,0x00,0xf0,0x01,0x0c,0x02,0x04,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
393   0x00,0x02,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0d,0x08};
394 static const guchar radio_dark_bits[] = {
395   0xf0,0x00,0x0c,0x02,0x02,0x04,0x02,0x04,0x01,0x08,0x01,0x08,0x01,0x08,0x01,
396   0x08,0x00,0x08,0x02,0x04,0x0c,0x06,0xf0,0x01,0x00,0x00,0x00,0x00};
397 static const guchar radio_mid_bits[] = {
398   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
399   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
400 static const guchar radio_light_bits[] = {
401   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x10,0x00,
402   0x10,0x00,0x10,0x00,0x08,0x00,0x08,0x00,0x06,0xe0,0x01,0x00,0x00};
403 static const guchar radio_text_bits[] = {
404   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0xf0,0x01,0xf0,0x01,0xf0,
405   0x01,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
406 #if 0
407 /*
408  * radio_aa_bits is currently not used, since it is all zeros.
409  */
410 static const guchar radio_aa_bits[] = {
411   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
412   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
413 #endif
414 static const guchar radio_base_bits[] = {
415   0x00,0x00,0x00,0x00,0xf0,0x01,0xf8,0x03,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
416   0x07,0xfc,0x07,0xf8,0x03,0xf0,0x01,0x00,0x00,0x00,0x00,0x00,0x00};
417
418 /*
419  * Extracted from radio-13.png, width=13, height=13
420  */
421 static const guchar radio_inconsistent_text_bits[] = {
422   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x03,0x00,
423   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
424 static const guchar radio_inconsistent_aa_bits[] = {
425   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0x03,0x00,0x00,0xf8,
426   0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
427
428 static struct {
429   const guchar      *bits;
430   GList     *bmap_list; /* list of GdkBitmap */
431 } indicator_parts[] = {
432   { check_aa_bits, NULL },
433   { check_base_bits, NULL },
434   { check_black_bits, NULL },
435   { check_dark_bits, NULL },
436   { check_light_bits, NULL },
437   { check_mid_bits, NULL },
438   { check_text_bits, NULL },
439   { check_inconsistent_text_bits, NULL },
440   { radio_base_bits, NULL },
441   { radio_black_bits, NULL },
442   { radio_dark_bits, NULL },
443   { radio_light_bits, NULL },
444   { radio_mid_bits, NULL },
445   { radio_text_bits, NULL },
446   { radio_inconsistent_aa_bits, NULL },
447   { radio_inconsistent_text_bits, NULL },
448 };
449
450 /* --- variables --- */
451 static const GdkColor gtk_default_normal_fg =      { 0,      0,      0,      0 };
452 static const GdkColor gtk_default_active_fg =      { 0,      0,      0,      0 };
453 static const GdkColor gtk_default_prelight_fg =    { 0,      0,      0,      0 };
454 static const GdkColor gtk_default_selected_fg =    { 0, 0xffff, 0xffff, 0xffff };
455 static const GdkColor gtk_default_insensitive_fg = { 0, 0x7530, 0x7530, 0x7530 };
456
457 static const GdkColor gtk_default_normal_bg =      { 0, 0xdcdc, 0xdada, 0xd5d5 };
458 static const GdkColor gtk_default_active_bg =      { 0, 0xbaba, 0xb5b5, 0xabab };
459 static const GdkColor gtk_default_prelight_bg =    { 0, 0xeeee, 0xebeb, 0xe7e7 };
460 static const GdkColor gtk_default_selected_bg =    { 0, 0x4b4b, 0x6969, 0x8383 };
461 static const GdkColor gtk_default_insensitive_bg = { 0, 0xdcdc, 0xdada, 0xd5d5 };
462 static const GdkColor gtk_default_selected_base =  { 0, 0x4b4b, 0x6969, 0x8383 };
463 static const GdkColor gtk_default_active_base =    { 0, 0x8080, 0x7d7d, 0x7474 };
464
465 static gpointer parent_class = NULL;
466
467 /* --- signals --- */
468 static guint realize_signal = 0;
469 static guint unrealize_signal = 0;
470
471 /* --- functions --- */
472 GType
473 gtk_style_get_type (void)
474 {
475   static GType style_type = 0;
476   
477   if (!style_type)
478     {
479       static const GTypeInfo style_info =
480       {
481         sizeof (GtkStyleClass),
482         (GBaseInitFunc) NULL,
483         (GBaseFinalizeFunc) NULL,
484         (GClassInitFunc) gtk_style_class_init,
485         NULL,           /* class_finalize */
486         NULL,           /* class_data */
487         sizeof (GtkStyle),
488         0,              /* n_preallocs */
489         (GInstanceInitFunc) gtk_style_init,
490       };
491       
492       style_type = g_type_register_static (G_TYPE_OBJECT, "GtkStyle",
493                                            &style_info, 0);
494     }
495   
496   return style_type;
497 }
498
499 /**
500  * _gtk_style_init_for_settings:
501  * @style: a #GtkStyle
502  * @settings: a #GtkSettings
503  * 
504  * Initializes the font description in @style accoridng to the default
505  * font name of @settings. This is called for gtk_style_new() with
506  * the settings for the default screen (if any); if we are creating
507  * a style for a particular screen, we then call it again in a
508  * location where we know the correct settings.
509  * The reason for this is that gtk_rc_style_create_style() doesn't
510  * take the screen for an argument.
511  **/
512 void
513 _gtk_style_init_for_settings (GtkStyle    *style,
514                               GtkSettings *settings)
515 {
516   const gchar *font_name = _gtk_rc_context_get_default_font_name (settings);
517
518   if (style->font_desc)
519     pango_font_description_free (style->font_desc);
520   
521   style->font_desc = pango_font_description_from_string (font_name);
522       
523   if (!pango_font_description_get_family (style->font_desc))
524     {
525       g_warning ("Default font does not have a family set");
526       pango_font_description_set_family (style->font_desc, "Sans");
527     }
528   if (pango_font_description_get_size (style->font_desc) <= 0)
529     {
530       g_warning ("Default font does not have a positive size");
531       pango_font_description_set_size (style->font_desc, 10 * PANGO_SCALE);
532     }
533 }
534
535 static void
536 gtk_style_init (GtkStyle *style)
537 {
538   gint i;
539   
540   GtkSettings *settings = gtk_settings_get_default ();
541   
542   if (settings)
543     _gtk_style_init_for_settings (style, settings);
544   else
545     style->font_desc = pango_font_description_from_string ("Sans 10");
546   
547   style->attach_count = 0;
548   style->colormap = NULL;
549   style->depth = -1;
550   
551   style->black.red = 0;
552   style->black.green = 0;
553   style->black.blue = 0;
554   
555   style->white.red = 65535;
556   style->white.green = 65535;
557   style->white.blue = 65535;
558   
559   style->black_gc = NULL;
560   style->white_gc = NULL;
561   
562   style->fg[GTK_STATE_NORMAL] = gtk_default_normal_fg;
563   style->fg[GTK_STATE_ACTIVE] = gtk_default_active_fg;
564   style->fg[GTK_STATE_PRELIGHT] = gtk_default_prelight_fg;
565   style->fg[GTK_STATE_SELECTED] = gtk_default_selected_fg;
566   style->fg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
567   
568   style->bg[GTK_STATE_NORMAL] = gtk_default_normal_bg;
569   style->bg[GTK_STATE_ACTIVE] = gtk_default_active_bg;
570   style->bg[GTK_STATE_PRELIGHT] = gtk_default_prelight_bg;
571   style->bg[GTK_STATE_SELECTED] = gtk_default_selected_bg;
572   style->bg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_bg;
573   
574   for (i = 0; i < 4; i++)
575     {
576       style->text[i] = style->fg[i];
577       style->base[i] = style->white;
578     }
579
580   style->base[GTK_STATE_SELECTED] = gtk_default_selected_base;
581   style->text[GTK_STATE_SELECTED] = style->white;
582   style->base[GTK_STATE_ACTIVE] = gtk_default_active_base;
583   style->text[GTK_STATE_ACTIVE] = style->white;
584   style->base[GTK_STATE_INSENSITIVE] = gtk_default_prelight_bg;
585   style->text[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
586   
587   for (i = 0; i < 5; i++)
588     style->bg_pixmap[i] = NULL;
589   
590   style->rc_style = NULL;
591   
592   for (i = 0; i < 5; i++)
593     {
594       style->fg_gc[i] = NULL;
595       style->bg_gc[i] = NULL;
596       style->light_gc[i] = NULL;
597       style->dark_gc[i] = NULL;
598       style->mid_gc[i] = NULL;
599       style->text_gc[i] = NULL;
600       style->base_gc[i] = NULL;
601       style->text_aa_gc[i] = NULL;
602     }
603
604   style->xthickness = 2;
605   style->ythickness = 2;
606
607   style->property_cache = NULL;
608 }
609
610 static void
611 gtk_style_class_init (GtkStyleClass *klass)
612 {
613   GObjectClass *object_class = G_OBJECT_CLASS (klass);
614   
615   parent_class = g_type_class_peek_parent (klass);
616
617   object_class->finalize = gtk_style_finalize;
618
619   klass->clone = gtk_style_real_clone;
620   klass->copy = gtk_style_real_copy;
621   klass->init_from_rc = gtk_style_real_init_from_rc;
622   klass->realize = gtk_style_real_realize;
623   klass->unrealize = gtk_style_real_unrealize;
624   klass->set_background = gtk_style_real_set_background;
625   klass->render_icon = gtk_default_render_icon;
626
627   klass->draw_hline = gtk_default_draw_hline;
628   klass->draw_vline = gtk_default_draw_vline;
629   klass->draw_shadow = gtk_default_draw_shadow;
630   klass->draw_polygon = gtk_default_draw_polygon;
631   klass->draw_arrow = gtk_default_draw_arrow;
632   klass->draw_diamond = gtk_default_draw_diamond;
633   klass->draw_string = gtk_default_draw_string;
634   klass->draw_box = gtk_default_draw_box;
635   klass->draw_flat_box = gtk_default_draw_flat_box;
636   klass->draw_check = gtk_default_draw_check;
637   klass->draw_option = gtk_default_draw_option;
638   klass->draw_tab = gtk_default_draw_tab;
639   klass->draw_shadow_gap = gtk_default_draw_shadow_gap;
640   klass->draw_box_gap = gtk_default_draw_box_gap;
641   klass->draw_extension = gtk_default_draw_extension;
642   klass->draw_focus = gtk_default_draw_focus;
643   klass->draw_slider = gtk_default_draw_slider;
644   klass->draw_handle = gtk_default_draw_handle;
645   klass->draw_expander = gtk_default_draw_expander;
646   klass->draw_layout = gtk_default_draw_layout;
647   klass->draw_resize_grip = gtk_default_draw_resize_grip;
648
649   
650   /**
651    * GtkStyle::realize:
652    * @style: the object which received the signal
653    *
654    * Emitted when the style has been initialized for a particular
655    * colormap and depth. Connecting to this signal is probably seldom
656    * useful since most of the time applications and widgets only
657    * deal with styles that have been already realized.
658    *
659    * Since: 2.4
660    */
661   realize_signal = g_signal_new ("realize",
662                                  G_TYPE_FROM_CLASS (object_class),
663                                  G_SIGNAL_RUN_FIRST,
664                                  G_STRUCT_OFFSET (GtkStyleClass, realize),
665                                  NULL, NULL,
666                                  _gtk_marshal_VOID__VOID,
667                                  G_TYPE_NONE, 0);
668   /**
669    * GtkStyle::unrealize:
670    * @style: the object which received the signal
671    *
672    * Emitted when the aspects of the style specific to a particular colormap
673    * and depth are being cleaned up. A connection to this signal can be useful
674    * if a widget wants to cache objects like a #GdkGC as object data on #GtkStyle.
675    * This signal provides a convenient place to free such cached objects.
676    *
677    * Since: 2.4
678    */
679   unrealize_signal = g_signal_new ("unrealize",
680                                    G_TYPE_FROM_CLASS (object_class),
681                                    G_SIGNAL_RUN_FIRST,
682                                    G_STRUCT_OFFSET (GtkStyleClass, unrealize),
683                                    NULL, NULL,
684                                    _gtk_marshal_VOID__VOID,
685                                    G_TYPE_NONE, 0);
686 }
687
688 static void
689 clear_property_cache (GtkStyle *style)
690 {
691   if (style->property_cache)
692     {
693       guint i;
694
695       for (i = 0; i < style->property_cache->len; i++)
696         {
697           PropertyValue *node = &g_array_index (style->property_cache, PropertyValue, i);
698
699           g_param_spec_unref (node->pspec);
700           g_value_unset (&node->value);
701         }
702       g_array_free (style->property_cache, TRUE);
703       style->property_cache = NULL;
704     }
705 }
706
707 static void
708 gtk_style_finalize (GObject *object)
709 {
710   GtkStyle *style = GTK_STYLE (object);
711
712   g_return_if_fail (style->attach_count == 0);
713
714   clear_property_cache (style);
715   
716   if (style->styles)
717     {
718       if (style->styles->data != style)
719         g_slist_remove (style->styles, style);
720       else
721         {
722           GSList *tmp_list = style->styles->next;
723           
724           while (tmp_list)
725             {
726               GTK_STYLE (tmp_list->data)->styles = style->styles->next;
727               tmp_list = tmp_list->next;
728             }
729           g_slist_free_1 (style->styles);
730         }
731     }
732
733   if (style->icon_factories)
734     {
735       GSList *tmp_list = style->icon_factories;
736
737       while (tmp_list)
738         {
739           g_object_unref (tmp_list->data);
740           tmp_list = tmp_list->next;
741         }
742
743       g_slist_free (style->icon_factories);
744     }
745
746   pango_font_description_free (style->font_desc);
747   
748   if (style->private_font)
749     gdk_font_unref (style->private_font);
750
751   if (style->private_font_desc)
752     pango_font_description_free (style->private_font_desc);
753   
754   if (style->rc_style)
755     gtk_rc_style_unref (style->rc_style);
756   
757   G_OBJECT_CLASS (parent_class)->finalize (object);
758 }
759
760
761 GtkStyle*
762 gtk_style_copy (GtkStyle *style)
763 {
764   GtkStyle *new_style;
765   
766   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
767   
768   new_style = GTK_STYLE_GET_CLASS (style)->clone (style);
769   GTK_STYLE_GET_CLASS (style)->copy (new_style, style);
770
771   return new_style;
772 }
773
774 static GtkStyle*
775 gtk_style_duplicate (GtkStyle *style)
776 {
777   GtkStyle *new_style;
778   
779   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
780   
781   new_style = gtk_style_copy (style);
782   
783   style->styles = g_slist_append (style->styles, new_style);
784   new_style->styles = style->styles;  
785   
786   return new_style;
787 }
788
789 /**
790  * gtk_style_new:
791  * @returns: a new #GtkStyle.
792  *
793  * Creates a new #GtkStyle.
794  **/
795 GtkStyle*
796 gtk_style_new (void)
797 {
798   GtkStyle *style;
799   
800   style = g_object_new (GTK_TYPE_STYLE, NULL);
801   
802   return style;
803 }
804
805 /**
806  * gtk_style_attach:
807  * @style: a #GtkStyle.
808  * @window: a #GdkWindow.
809  * @returns: Either @style, or a newly-created #GtkStyle.
810  *   If the style is newly created, the style parameter
811  *   will be dereferenced, and the new style will have
812  *   a reference count belonging to the caller.
813  *
814  * Attaches a style to a window; this process allocates the
815  * colors and creates the GC's for the style - it specializes
816  * it to a particular visual and colormap. The process may 
817  * involve the creation of a new style if the style has already 
818  * been attached to a window with a different style and colormap.
819  **/
820  /*
821  * FIXME: The sequence - 
822  *    create a style => s1
823  *    attach s1 to v1, c1 => s1
824  *    attach s1 to v2, c2 => s2
825  *    detach s1 from v1, c1
826  *    attach s1 to v2, c2 => s3
827  * results in two separate, unlinked styles s2 and s3 which
828  * are identical and could be shared. To fix this, we would
829  * want to never remove a style from the list of linked
830  * styles as long as as it has a reference count. However, the 
831  * disadvantage of doing it this way means that we would need two 
832  * passes through the linked list when attaching (one to check for 
833  * matching styles, one to look for empty unattached styles - but 
834  * it will almost never be longer than 2 elements.
835  */
836 GtkStyle*
837 gtk_style_attach (GtkStyle  *style,
838                   GdkWindow *window)
839 {
840   GSList *styles;
841   GtkStyle *new_style = NULL;
842   GdkColormap *colormap;
843   
844   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
845   g_return_val_if_fail (window != NULL, NULL);
846   
847   colormap = gdk_drawable_get_colormap (window);
848   
849   if (!style->styles)
850     style->styles = g_slist_append (NULL, style);
851   
852   styles = style->styles;
853   while (styles)
854     {
855       new_style = styles->data;
856       
857       if (new_style->attach_count == 0)
858         {
859           gtk_style_realize (new_style, colormap);
860           break;
861         }
862       else if (new_style->colormap == colormap)
863         break;
864       
865       new_style = NULL;
866       styles = styles->next;
867     }
868   
869   if (!new_style)
870     {
871       new_style = gtk_style_duplicate (style);
872       if (gdk_colormap_get_screen (style->colormap) != gdk_colormap_get_screen (colormap) &&
873           new_style->private_font)
874         {
875           gdk_font_unref (new_style->private_font);
876           new_style->private_font = NULL;
877         }
878       gtk_style_realize (new_style, colormap);
879     }
880
881   /* A style gets a refcount from being attached */
882   if (new_style->attach_count == 0)
883     g_object_ref (new_style);
884
885   /* Another refcount belongs to the parent */
886   if (style != new_style) 
887     {
888       g_object_unref (style);
889       g_object_ref (new_style);
890     }
891   
892   new_style->attach_count++;
893   
894   return new_style;
895 }
896
897 void
898 gtk_style_detach (GtkStyle *style)
899 {
900   g_return_if_fail (GTK_IS_STYLE (style));
901   
902   style->attach_count -= 1;
903   if (style->attach_count == 0)
904     {
905       g_signal_emit (style, unrealize_signal, 0);
906       
907       g_object_unref (style->colormap);
908       style->colormap = NULL;
909
910       if (style->private_font_desc)
911         {
912           if (style->private_font)
913             {
914               gdk_font_unref (style->private_font);
915               style->private_font = NULL;
916             }
917           
918           pango_font_description_free (style->private_font_desc);
919           style->private_font_desc = NULL;
920         }
921
922       g_object_unref (style);
923     }
924 }
925
926 /**
927  * gtk_style_ref:
928  * @style: a #GtkStyle.
929  * @returns: @style.
930  *
931  * Deprecated equivalent of g_object_ref().
932  **/
933 GtkStyle*
934 gtk_style_ref (GtkStyle *style)
935 {
936   return (GtkStyle *) g_object_ref (style);
937 }
938
939 /**
940  * gtk_style_unref:
941  * @style: a #GtkStyle.
942  *
943  * Deprecated equivalent of g_object_unref().
944  **/
945 void
946 gtk_style_unref (GtkStyle *style)
947 {
948   g_object_unref (style);
949 }
950
951 static void
952 gtk_style_realize (GtkStyle    *style,
953                    GdkColormap *colormap)
954 {
955   g_return_if_fail (GTK_IS_STYLE (style));
956   g_return_if_fail (GDK_IS_COLORMAP (colormap));
957   
958   style->colormap = g_object_ref (colormap);
959   style->depth = gdk_colormap_get_visual (colormap)->depth;
960
961   g_signal_emit (style, realize_signal, 0);
962 }
963
964 GtkIconSet*
965 gtk_style_lookup_icon_set (GtkStyle   *style,
966                            const char *stock_id)
967 {
968   GSList *iter;
969
970   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
971   g_return_val_if_fail (stock_id != NULL, NULL);
972   
973   iter = style->icon_factories;
974   while (iter != NULL)
975     {
976       GtkIconSet *icon_set = gtk_icon_factory_lookup (GTK_ICON_FACTORY (iter->data),
977                                                       stock_id);
978       if (icon_set)
979         return icon_set;
980       
981       iter = g_slist_next (iter);
982     }
983
984   return gtk_icon_factory_lookup_default (stock_id);
985 }
986
987 /**
988  * gtk_draw_hline:
989  * @style: a #GtkStyle
990  * @window: a #GdkWindow
991  * @state_type: a state
992  * @x1: the starting x coordinate
993  * @x2: the ending x coordinate
994  * @y: the y coordinate
995  * 
996  * Draws a horizontal line from (@x1, @y) to (@x2, @y) in @window
997  * using the given style and state.
998  * 
999  * Deprecated: Use gtk_paint_hline() instead.
1000  **/
1001 void
1002 gtk_draw_hline (GtkStyle     *style,
1003                 GdkWindow    *window,
1004                 GtkStateType  state_type,
1005                 gint          x1,
1006                 gint          x2,
1007                 gint          y)
1008 {
1009   g_return_if_fail (GTK_IS_STYLE (style));
1010   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_hline != NULL);
1011   
1012   GTK_STYLE_GET_CLASS (style)->draw_hline (style, window, state_type, NULL, NULL, NULL, x1, x2, y);
1013 }
1014
1015
1016 /**
1017  * gtk_draw_vline:
1018  * @style: a #GtkStyle
1019  * @window: a #GdkWindow
1020  * @state_type: a state
1021  * @y1_: the starting y coordinate
1022  * @y2_: the ending y coordinate
1023  * @x: the x coordinate
1024  * 
1025  * Draws a vertical line from (@x, @y1_) to (@x, @y2_) in @window
1026  * using the given style and state.
1027  * 
1028  * Deprecated: Use gtk_paint_vline() instead.
1029  **/
1030 void
1031 gtk_draw_vline (GtkStyle     *style,
1032                 GdkWindow    *window,
1033                 GtkStateType  state_type,
1034                 gint          y1_,
1035                 gint          y2_,
1036                 gint          x)
1037 {
1038   g_return_if_fail (GTK_IS_STYLE (style));
1039   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_vline != NULL);
1040   
1041   GTK_STYLE_GET_CLASS (style)->draw_vline (style, window, state_type, NULL, NULL, NULL, y1_, y2_, x);
1042 }
1043
1044 /**
1045  * gtk_draw_shadow:
1046  * @style: a #GtkStyle
1047  * @window: a #GdkWindow
1048  * @state_type: a state
1049  * @shadow_type: type of shadow to draw
1050  * @x: x origin of the rectangle
1051  * @y: y origin of the rectangle
1052  * @width: width of the rectangle 
1053  * @height: width of the rectangle 
1054  *
1055  * Draws a shadow around the given rectangle in @window 
1056  * using the given style and state and shadow type.
1057  * 
1058  * Deprecated: Use gtk_paint_shadow() instead.
1059  */
1060 void
1061 gtk_draw_shadow (GtkStyle      *style,
1062                  GdkWindow     *window,
1063                  GtkStateType   state_type,
1064                  GtkShadowType  shadow_type,
1065                  gint           x,
1066                  gint           y,
1067                  gint           width,
1068                  gint           height)
1069 {
1070   g_return_if_fail (GTK_IS_STYLE (style));
1071   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow != NULL);
1072   
1073   GTK_STYLE_GET_CLASS (style)->draw_shadow (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1074 }
1075
1076 /**
1077  * gtk_draw_polygon:
1078  * @style: a #GtkStyle
1079  * @window: a #GdkWindow
1080  * @state_type: a state
1081  * @shadow_type: type of shadow to draw
1082  * @points: an array of #GdkPoint<!-- -->s
1083  * @npoints: length of @points
1084  * @fill: %TRUE if the polygon should be filled
1085  * 
1086  * Draws a polygon on @window with the given parameters.
1087  *
1088  * Deprecated: Use gtk_paint_polygon() instead.
1089  */ 
1090 void
1091 gtk_draw_polygon (GtkStyle      *style,
1092                   GdkWindow     *window,
1093                   GtkStateType   state_type,
1094                   GtkShadowType  shadow_type,
1095                   GdkPoint      *points,
1096                   gint           npoints,
1097                   gboolean       fill)
1098 {
1099   g_return_if_fail (GTK_IS_STYLE (style));
1100   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_polygon != NULL);
1101   
1102   GTK_STYLE_GET_CLASS (style)->draw_polygon (style, window, state_type, shadow_type, NULL, NULL, NULL, points, npoints, fill);
1103 }
1104
1105 /**
1106  * gtk_draw_arrow:
1107  * @style: a #GtkStyle
1108  * @window: a #GdkWindow
1109  * @state_type: a state
1110  * @shadow_type: the type of shadow to draw
1111  * @arrow_type: the type of arrow to draw
1112  * @fill: %TRUE if the arrow tip should be filled
1113  * @x: x origin of the rectangle to draw the arrow in
1114  * @y: y origin of the rectangle to draw the arrow in
1115  * @width: width of the rectangle to draw the arrow in
1116  * @height: height of the rectangle to draw the arrow in
1117  * 
1118  * Draws an arrow in the given rectangle on @window using the given 
1119  * parameters. @arrow_type determines the direction of the arrow.
1120  *
1121  * Deprecated: Use gtk_paint_arrow() instead.
1122  */
1123 void
1124 gtk_draw_arrow (GtkStyle      *style,
1125                 GdkWindow     *window,
1126                 GtkStateType   state_type,
1127                 GtkShadowType  shadow_type,
1128                 GtkArrowType   arrow_type,
1129                 gboolean       fill,
1130                 gint           x,
1131                 gint           y,
1132                 gint           width,
1133                 gint           height)
1134 {
1135   g_return_if_fail (GTK_IS_STYLE (style));
1136   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_arrow != NULL);
1137   
1138   GTK_STYLE_GET_CLASS (style)->draw_arrow (style, window, state_type, shadow_type, NULL, NULL, NULL, arrow_type, fill, x, y, width, height);
1139 }
1140
1141 /**
1142  * gtk_draw_diamond:
1143  * @style: a #GtkStyle
1144  * @window: a #GdkWindow
1145  * @state_type: a state
1146  * @shadow_type: the type of shadow to draw
1147  * @x: x origin of the rectangle to draw the diamond in
1148  * @y: y origin of the rectangle to draw the diamond in
1149  * @width: width of the rectangle to draw the diamond in
1150  * @height: height of the rectangle to draw the diamond in
1151  *
1152  * Draws a diamond in the given rectangle on @window using the given
1153  * parameters.
1154  *
1155  * Deprecated: Use gtk_paint_diamond() instead.
1156  */
1157 void
1158 gtk_draw_diamond (GtkStyle      *style,
1159                   GdkWindow     *window,
1160                   GtkStateType   state_type,
1161                   GtkShadowType  shadow_type,
1162                   gint           x,
1163                   gint           y,
1164                   gint           width,
1165                   gint           height)
1166 {
1167   g_return_if_fail (GTK_IS_STYLE (style));
1168   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_diamond != NULL);
1169   
1170   GTK_STYLE_GET_CLASS (style)->draw_diamond (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1171 }
1172
1173 /**
1174  * gtk_draw_string:
1175  * @style: a #GtkStyle
1176  * @window: a #GdkWindow
1177  * @state_type: a state
1178  * @x: x origin
1179  * @y: y origin
1180  * @string: the string to draw
1181  * 
1182  * Draws a text string on @window with the given parameters.
1183  *
1184  * Deprecated: Use gtk_paint_layout() instead.
1185  */
1186 void
1187 gtk_draw_string (GtkStyle      *style,
1188                  GdkWindow     *window,
1189                  GtkStateType   state_type,
1190                  gint           x,
1191                  gint           y,
1192                  const gchar   *string)
1193 {
1194   g_return_if_fail (GTK_IS_STYLE (style));
1195   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_string != NULL);
1196   
1197   GTK_STYLE_GET_CLASS (style)->draw_string (style, window, state_type, NULL, NULL, NULL, x, y, string);
1198 }
1199
1200 /**
1201  * gtk_draw_box:
1202  * @style: a #GtkStyle
1203  * @window: a #GdkWindow
1204  * @state_type: a state
1205  * @shadow_type: the type of shadow to draw
1206  * @x: x origin of the box
1207  * @y: y origin of the box
1208  * @width: the width of the box
1209  * @height: the height of the box
1210  * 
1211  * Draws a box on @window with the given parameters.
1212  *
1213  * Deprecated: Use gtk_paint_box() instead.
1214  */
1215 void
1216 gtk_draw_box (GtkStyle      *style,
1217               GdkWindow     *window,
1218               GtkStateType   state_type,
1219               GtkShadowType  shadow_type,
1220               gint           x,
1221               gint           y,
1222               gint           width,
1223               gint           height)
1224 {
1225   g_return_if_fail (GTK_IS_STYLE (style));
1226   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box != NULL);
1227   
1228   GTK_STYLE_GET_CLASS (style)->draw_box (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1229 }
1230
1231 /**
1232  * gtk_draw_flat_box:
1233  * @style: a #GtkStyle
1234  * @window: a #GdkWindow
1235  * @state_type: a state
1236  * @shadow_type: the type of shadow to draw
1237  * @x: x origin of the box
1238  * @y: y origin of the box
1239  * @width: the width of the box
1240  * @height: the height of the box
1241  * 
1242  * Draws a flat box on @window with the given parameters.
1243  *
1244  * Deprecated: Use gtk_paint_flat_box() instead.
1245  */
1246 void
1247 gtk_draw_flat_box (GtkStyle      *style,
1248                    GdkWindow     *window,
1249                    GtkStateType   state_type,
1250                    GtkShadowType  shadow_type,
1251                    gint           x,
1252                    gint           y,
1253                    gint           width,
1254                    gint           height)
1255 {
1256   g_return_if_fail (GTK_IS_STYLE (style));
1257   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_flat_box != NULL);
1258   
1259   GTK_STYLE_GET_CLASS (style)->draw_flat_box (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1260 }
1261
1262 /**
1263  * gtk_draw_check:
1264  * @style: a #GtkStyle
1265  * @window: a #GdkWindow
1266  * @state_type: a state
1267  * @shadow_type: the type of shadow to draw
1268  * @x: x origin of the rectangle to draw the check in
1269  * @y: y origin of the rectangle to draw the check in
1270  * @width: the width of the rectangle to draw the check in
1271  * @height: the height of the rectangle to draw the check in
1272  * 
1273  * Draws a check button indicator in the given rectangle on @window with 
1274  * the given parameters.
1275  *
1276  * Deprecated: Use gtk_paint_check() instead.
1277  */
1278 void
1279 gtk_draw_check (GtkStyle      *style,
1280                 GdkWindow     *window,
1281                 GtkStateType   state_type,
1282                 GtkShadowType  shadow_type,
1283                 gint           x,
1284                 gint           y,
1285                 gint           width,
1286                 gint           height)
1287 {
1288   g_return_if_fail (GTK_IS_STYLE (style));
1289   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_check != NULL);
1290   
1291   GTK_STYLE_GET_CLASS (style)->draw_check (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1292 }
1293
1294 /**
1295  * gtk_draw_option:
1296  * @style: a #GtkStyle
1297  * @window: a #GdkWindow
1298  * @state_type: a state
1299  * @shadow_type: the type of shadow to draw
1300  * @x: x origin of the rectangle to draw the option in
1301  * @y: y origin of the rectangle to draw the option in
1302  * @width: the width of the rectangle to draw the option in
1303  * @height: the height of the rectangle to draw the option in
1304  *
1305  * Draws a radio button indicator in the given rectangle on @window with 
1306  * the given parameters.
1307  *
1308  * Deprecated: Use gtk_paint_option() instead.
1309  */
1310 void
1311 gtk_draw_option (GtkStyle      *style,
1312                  GdkWindow     *window,
1313                  GtkStateType   state_type,
1314                  GtkShadowType  shadow_type,
1315                  gint           x,
1316                  gint           y,
1317                  gint           width,
1318                  gint           height)
1319 {
1320   g_return_if_fail (GTK_IS_STYLE (style));
1321   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_option != NULL);
1322   
1323   GTK_STYLE_GET_CLASS (style)->draw_option (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1324 }
1325
1326 /**
1327  * gtk_draw_tab:
1328  * @style: a #GtkStyle
1329  * @window: a #GdkWindow
1330  * @state_type: a state
1331  * @shadow_type: the type of shadow to draw
1332  * @x: x origin of the rectangle to draw the tab in
1333  * @y: y origin of the rectangle to draw the tab in
1334  * @width: the width of the rectangle to draw the tab in
1335  * @height: the height of the rectangle to draw the tab in
1336  *
1337  * Draws an option menu tab (i.e. the up and down pointing arrows)
1338  * in the given rectangle on @window using the given parameters.
1339  * 
1340  * Deprecated: Use gtk_paint_tab() instead.
1341  */ 
1342 void
1343 gtk_draw_tab (GtkStyle      *style,
1344               GdkWindow     *window,
1345               GtkStateType   state_type,
1346               GtkShadowType  shadow_type,
1347               gint           x,
1348               gint           y,
1349               gint           width,
1350               gint           height)
1351 {
1352   g_return_if_fail (GTK_IS_STYLE (style));
1353   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_tab != NULL);
1354   
1355   GTK_STYLE_GET_CLASS (style)->draw_tab (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1356 }
1357
1358 /**
1359  * gtk_draw_shadow_gap:
1360  * @style: a #GtkStyle
1361  * @window: a #GdkWindow
1362  * @state_type: a state
1363  * @shadow_type: type of shadow to draw
1364  * @x: x origin of the rectangle
1365  * @y: y origin of the rectangle
1366  * @width: width of the rectangle 
1367  * @height: width of the rectangle 
1368  * @gap_side: side in which to leave the gap
1369  * @gap_x: starting position of the gap
1370  * @gap_width: width of the gap
1371  *
1372  * Draws a shadow around the given rectangle in @window 
1373  * using the given style and state and shadow type, leaving a 
1374  * gap in one side.
1375  * 
1376  * Deprecated: Use gtk_paint_shadow_gap() instead.
1377 */
1378 void
1379 gtk_draw_shadow_gap (GtkStyle       *style,
1380                      GdkWindow      *window,
1381                      GtkStateType    state_type,
1382                      GtkShadowType   shadow_type,
1383                      gint            x,
1384                      gint            y,
1385                      gint            width,
1386                      gint            height,
1387                      GtkPositionType gap_side,
1388                      gint            gap_x,
1389                      gint            gap_width)
1390 {
1391   g_return_if_fail (GTK_IS_STYLE (style));
1392   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow_gap != NULL);
1393   
1394   GTK_STYLE_GET_CLASS (style)->draw_shadow_gap (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, gap_side, gap_x, gap_width);
1395 }
1396
1397 /**
1398  * gtk_draw_box_gap:
1399  * @style: a #GtkStyle
1400  * @window: a #GdkWindow
1401  * @state_type: a state
1402  * @shadow_type: type of shadow to draw
1403  * @x: x origin of the rectangle
1404  * @y: y origin of the rectangle
1405  * @width: width of the rectangle 
1406  * @height: width of the rectangle 
1407  * @gap_side: side in which to leave the gap
1408  * @gap_x: starting position of the gap
1409  * @gap_width: width of the gap
1410  *
1411  * Draws a box in @window using the given style and state and shadow type, 
1412  * leaving a gap in one side.
1413  * 
1414  * Deprecated: Use gtk_paint_box_gap() instead.
1415  */
1416 void
1417 gtk_draw_box_gap (GtkStyle       *style,
1418                   GdkWindow      *window,
1419                   GtkStateType    state_type,
1420                   GtkShadowType   shadow_type,
1421                   gint            x,
1422                   gint            y,
1423                   gint            width,
1424                   gint            height,
1425                   GtkPositionType gap_side,
1426                   gint            gap_x,
1427                   gint            gap_width)
1428 {
1429   g_return_if_fail (GTK_IS_STYLE (style));
1430   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box_gap != NULL);
1431   
1432   GTK_STYLE_GET_CLASS (style)->draw_box_gap (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, gap_side, gap_x, gap_width);
1433 }
1434
1435 /**
1436  * gtk_draw_extension: 
1437  * @style: a #GtkStyle
1438  * @window: a #GdkWindow
1439  * @state_type: a state
1440  * @shadow_type: type of shadow to draw
1441  * @x: x origin of the extension
1442  * @y: y origin of the extension
1443  * @width: width of the extension 
1444  * @height: width of the extension 
1445  * @gap_side: the side on to which the extension is attached
1446  * 
1447  * Draws an extension, i.e. a notebook tab.
1448  *
1449  * Deprecated: Use gtk_paint_extension() instead.
1450  **/
1451 void
1452 gtk_draw_extension (GtkStyle       *style,
1453                     GdkWindow      *window,
1454                     GtkStateType    state_type,
1455                     GtkShadowType   shadow_type,
1456                     gint            x,
1457                     gint            y,
1458                     gint            width,
1459                     gint            height,
1460                     GtkPositionType gap_side)
1461 {
1462   g_return_if_fail (GTK_IS_STYLE (style));
1463   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_extension != NULL);
1464   
1465   GTK_STYLE_GET_CLASS (style)->draw_extension (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, gap_side);
1466 }
1467
1468 /**
1469  * gtk_draw_focus:
1470  * @style: a #GtkStyle
1471  * @window: a #GdkWindow
1472  * @x: the x origin of the rectangle around which to draw a focus indicator
1473  * @y: the y origin of the rectangle around which to draw a focus indicator
1474  * @width: the width of the rectangle around which to draw a focus indicator
1475  * @height: the height of the rectangle around which to draw a focus indicator
1476  *
1477  * Draws a focus indicator around the given rectangle on @window using the
1478  * given style.
1479  * 
1480  * Deprecated: Use gtk_paint_focus() instead.
1481  */
1482 void
1483 gtk_draw_focus (GtkStyle      *style,
1484                 GdkWindow     *window,
1485                 gint           x,
1486                 gint           y,
1487                 gint           width,
1488                 gint           height)
1489 {
1490   g_return_if_fail (GTK_IS_STYLE (style));
1491   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_focus != NULL);
1492   
1493   GTK_STYLE_GET_CLASS (style)->draw_focus (style, window, GTK_STATE_NORMAL, NULL, NULL, NULL, x, y, width, height);
1494 }
1495
1496 void 
1497 gtk_draw_slider (GtkStyle      *style,
1498                  GdkWindow     *window,
1499                  GtkStateType   state_type,
1500                  GtkShadowType  shadow_type,
1501                  gint           x,
1502                  gint           y,
1503                  gint           width,
1504                  gint           height,
1505                  GtkOrientation orientation)
1506 {
1507   g_return_if_fail (GTK_IS_STYLE (style));
1508   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_slider != NULL);
1509   
1510   GTK_STYLE_GET_CLASS (style)->draw_slider (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, orientation);
1511 }
1512
1513 /**
1514  * gtk_draw_handle:
1515  * @style: a #GtkStyle
1516  * @window: a #GdkWindow
1517  * @state_type: a state
1518  * @shadow_type: type of shadow to draw
1519  * @x: x origin of the handle
1520  * @y: y origin of the handle
1521  * @width: with of the handle
1522  * @height: height of the handle
1523  * @orientation: the orientation of the handle
1524  * 
1525  * Draws a handle as used in #GtkHandleBox and #GtkPaned.
1526  * 
1527  * Deprecated: Use gtk_paint_handle() instead.
1528  **/
1529 void 
1530 gtk_draw_handle (GtkStyle      *style,
1531                  GdkWindow     *window,
1532                  GtkStateType   state_type,
1533                  GtkShadowType  shadow_type,
1534                  gint           x,
1535                  gint           y,
1536                  gint           width,
1537                  gint           height,
1538                  GtkOrientation orientation)
1539 {
1540   g_return_if_fail (GTK_IS_STYLE (style));
1541   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_handle != NULL);
1542   
1543   GTK_STYLE_GET_CLASS (style)->draw_handle (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, orientation);
1544 }
1545
1546 /**
1547  * gtk_draw_expander:
1548  * @style: a #GtkStyle
1549  * @window: a #GdkWindow
1550  * @state_type: a state
1551  * @x: the x position to draw the expander at
1552  * @y: the y position to draw the expander at
1553  * @expander_style: the style to draw the expander in
1554  * 
1555  * Draws an expander as used in #GtkTreeView.
1556  * 
1557  * Deprecated: Use gtk_paint_expander() instead.
1558  **/
1559 void
1560 gtk_draw_expander (GtkStyle        *style,
1561                    GdkWindow       *window,
1562                    GtkStateType     state_type,
1563                    gint             x,
1564                    gint             y,
1565                    GtkExpanderStyle expander_style)
1566 {
1567   g_return_if_fail (GTK_IS_STYLE (style));
1568   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_expander != NULL);
1569   
1570   GTK_STYLE_GET_CLASS (style)->draw_expander (style, window, state_type,
1571                                               NULL, NULL, NULL,
1572                                               x, y, expander_style);
1573 }
1574
1575 void
1576 gtk_draw_layout (GtkStyle        *style,
1577                  GdkWindow       *window,
1578                  GtkStateType     state_type,
1579                  gboolean         use_text,
1580                  gint             x,
1581                  gint             y,
1582                  PangoLayout     *layout)
1583 {
1584   g_return_if_fail (GTK_IS_STYLE (style));
1585   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_layout != NULL);
1586   
1587   GTK_STYLE_GET_CLASS (style)->draw_layout (style, window, state_type, use_text,
1588                                             NULL, NULL, NULL,
1589                                             x, y, layout);
1590 }
1591
1592 /**
1593  * gtk_draw_resize_grip:
1594  * @style: a #GtkStyle
1595  * @window: a #GdkWindow
1596  * @state_type: a state
1597  * @edge: the edge in which to draw the resize grip
1598  * @x: the x origin of the rectangle in which to draw the resize grip
1599  * @y: the y origin of the rectangle in which to draw the resize grip
1600  * @width: the width of the rectangle in which to draw the resize grip
1601  * @height: the height of the rectangle in which to draw the resize grip
1602  *
1603  * Draws a resize grip in the given rectangle on @window using the given
1604  * parameters. 
1605  * 
1606  * Deprecated: Use gtk_paint_resize_grip() instead.
1607  */
1608 void
1609 gtk_draw_resize_grip (GtkStyle     *style,
1610                       GdkWindow    *window,
1611                       GtkStateType  state_type,
1612                       GdkWindowEdge edge,
1613                       gint          x,
1614                       gint          y,
1615                       gint          width,
1616                       gint          height)
1617 {
1618   g_return_if_fail (GTK_IS_STYLE (style));
1619   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_resize_grip != NULL);
1620
1621   GTK_STYLE_GET_CLASS (style)->draw_resize_grip (style, window, state_type,
1622                                                  NULL, NULL, NULL,
1623                                                  edge,
1624                                                  x, y, width, height);
1625 }
1626
1627
1628 /**
1629  * gtk_style_set_background:
1630  * @style: a #GtkStyle
1631  * @window: a #GdkWindow
1632  * @state_type: a state
1633  * 
1634  * Sets the background of @window to the background color or pixmap
1635  * specified by @style for the given state.
1636  */
1637 void
1638 gtk_style_set_background (GtkStyle    *style,
1639                           GdkWindow   *window,
1640                           GtkStateType state_type)
1641 {
1642   g_return_if_fail (GTK_IS_STYLE (style));
1643   g_return_if_fail (window != NULL);
1644   
1645   GTK_STYLE_GET_CLASS (style)->set_background (style, window, state_type);
1646 }
1647
1648 /* Default functions */
1649 static GtkStyle *
1650 gtk_style_real_clone (GtkStyle *style)
1651 {
1652   return GTK_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
1653 }
1654
1655 static void
1656 gtk_style_real_copy (GtkStyle *style,
1657                      GtkStyle *src)
1658 {
1659   gint i;
1660   
1661   for (i = 0; i < 5; i++)
1662     {
1663       style->fg[i] = src->fg[i];
1664       style->bg[i] = src->bg[i];
1665       style->text[i] = src->text[i];
1666       style->base[i] = src->base[i];
1667       
1668       style->bg_pixmap[i] = src->bg_pixmap[i];
1669     }
1670
1671   if (style->private_font)
1672     gdk_font_unref (style->private_font);
1673   style->private_font = src->private_font;
1674   if (style->private_font)
1675     gdk_font_ref (style->private_font);
1676
1677   if (style->font_desc)
1678     pango_font_description_free (style->font_desc);
1679   if (src->font_desc)
1680     style->font_desc = pango_font_description_copy (src->font_desc);
1681   else
1682     style->font_desc = NULL;
1683   
1684   style->xthickness = src->xthickness;
1685   style->ythickness = src->ythickness;
1686
1687   if (style->rc_style)
1688     gtk_rc_style_unref (style->rc_style);
1689   style->rc_style = src->rc_style;
1690   if (src->rc_style)
1691     gtk_rc_style_ref (src->rc_style);
1692
1693   /* don't copy, just clear cache */
1694   clear_property_cache (style);
1695 }
1696
1697 static void
1698 gtk_style_real_init_from_rc (GtkStyle   *style,
1699                              GtkRcStyle *rc_style)
1700 {
1701   gint i;
1702
1703   /* cache _should_ be still empty */
1704   clear_property_cache (style);
1705
1706   if (rc_style->font_desc)
1707     pango_font_description_merge (style->font_desc, rc_style->font_desc, TRUE);
1708     
1709   for (i = 0; i < 5; i++)
1710     {
1711       if (rc_style->color_flags[i] & GTK_RC_FG)
1712         style->fg[i] = rc_style->fg[i];
1713       if (rc_style->color_flags[i] & GTK_RC_BG)
1714         style->bg[i] = rc_style->bg[i];
1715       if (rc_style->color_flags[i] & GTK_RC_TEXT)
1716         style->text[i] = rc_style->text[i];
1717       if (rc_style->color_flags[i] & GTK_RC_BASE)
1718         style->base[i] = rc_style->base[i];
1719     }
1720
1721   if (rc_style->xthickness >= 0)
1722     style->xthickness = rc_style->xthickness;
1723   if (rc_style->ythickness >= 0)
1724     style->ythickness = rc_style->ythickness;
1725
1726   if (rc_style->icon_factories)
1727     {
1728       GSList *iter;
1729
1730       style->icon_factories = g_slist_copy (rc_style->icon_factories);
1731       
1732       iter = style->icon_factories;
1733       while (iter != NULL)
1734         {
1735           g_object_ref (iter->data);
1736           iter = g_slist_next (iter);
1737         }
1738     }
1739 }
1740
1741 static gint
1742 style_property_values_cmp (gconstpointer bsearch_node1,
1743                            gconstpointer bsearch_node2)
1744 {
1745   const PropertyValue *val1 = bsearch_node1;
1746   const PropertyValue *val2 = bsearch_node2;
1747
1748   if (val1->widget_type == val2->widget_type)
1749     return val1->pspec < val2->pspec ? -1 : val1->pspec == val2->pspec ? 0 : 1;
1750   else
1751     return val1->widget_type < val2->widget_type ? -1 : 1;
1752 }
1753
1754 const GValue*
1755 _gtk_style_peek_property_value (GtkStyle           *style,
1756                                 GType               widget_type,
1757                                 GParamSpec         *pspec,
1758                                 GtkRcPropertyParser parser)
1759 {
1760   PropertyValue *pcache, key = { 0, NULL, { 0, } };
1761   const GtkRcProperty *rcprop = NULL;
1762   guint i;
1763
1764   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1765   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
1766   g_return_val_if_fail (g_type_is_a (pspec->owner_type, GTK_TYPE_WIDGET), NULL);
1767   g_return_val_if_fail (g_type_is_a (widget_type, pspec->owner_type), NULL);
1768
1769   key.widget_type = widget_type;
1770   key.pspec = pspec;
1771
1772   /* need value cache array */
1773   if (!style->property_cache)
1774     style->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
1775   else
1776     {
1777       pcache = bsearch (&key,
1778                         style->property_cache->data, style->property_cache->len,
1779                         sizeof (PropertyValue), style_property_values_cmp);
1780       if (pcache)
1781         return &pcache->value;
1782     }
1783
1784   i = 0;
1785   while (i < style->property_cache->len &&
1786          style_property_values_cmp (&key, &g_array_index (style->property_cache, PropertyValue, i)) >= 0)
1787     i++;
1788
1789   g_array_insert_val (style->property_cache, i, key);
1790   pcache = &g_array_index (style->property_cache, PropertyValue, i);
1791
1792   /* cache miss, initialize value type, then set contents */
1793   g_param_spec_ref (pcache->pspec);
1794   g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1795
1796   /* value provided by rc style? */
1797   if (style->rc_style)
1798     {
1799       GQuark prop_quark = g_quark_from_string (pspec->name);
1800
1801       do
1802         {
1803           rcprop = _gtk_rc_style_lookup_rc_property (style->rc_style,
1804                                                      g_type_qname (widget_type),
1805                                                      prop_quark);
1806           if (rcprop)
1807             break;
1808           widget_type = g_type_parent (widget_type);
1809         }
1810       while (g_type_is_a (widget_type, pspec->owner_type));
1811     }
1812
1813   /* when supplied by rc style, we need to convert */
1814   if (rcprop && !_gtk_settings_parse_convert (parser, &rcprop->value,
1815                                               pspec, &pcache->value))
1816     {
1817       gchar *contents = g_strdup_value_contents (&rcprop->value);
1818       
1819       g_message ("%s: failed to retrieve property `%s::%s' of type `%s' from rc file value \"%s\" of type `%s'",
1820                  rcprop->origin,
1821                  g_type_name (pspec->owner_type), pspec->name,
1822                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1823                  contents,
1824                  G_VALUE_TYPE_NAME (&rcprop->value));
1825       g_free (contents);
1826       rcprop = NULL; /* needs default */
1827     }
1828   
1829   /* not supplied by rc style (or conversion failed), revert to default */
1830   if (!rcprop)
1831     g_param_value_set_default (pspec, &pcache->value);
1832
1833   return &pcache->value;
1834 }
1835
1836 static GdkPixmap *
1837 load_bg_image (GdkColormap *colormap,
1838                GdkColor    *bg_color,
1839                const gchar *filename)
1840 {
1841   if (strcmp (filename, "<parent>") == 0)
1842     return (GdkPixmap*) GDK_PARENT_RELATIVE;
1843   else
1844     {
1845       return gdk_pixmap_colormap_create_from_xpm (NULL, colormap, NULL,
1846                                                   bg_color,
1847                                                   filename);
1848     }
1849 }
1850
1851 static void
1852 gtk_style_real_realize (GtkStyle *style)
1853 {
1854   GdkGCValues gc_values;
1855   GdkGCValuesMask gc_values_mask;
1856   
1857   gint i;
1858
1859   for (i = 0; i < 5; i++)
1860     {
1861       gtk_style_shade (&style->bg[i], &style->light[i], LIGHTNESS_MULT);
1862       gtk_style_shade (&style->bg[i], &style->dark[i], DARKNESS_MULT);
1863       
1864       style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
1865       style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
1866       style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
1867
1868       style->text_aa[i].red = (style->text[i].red + style->base[i].red) / 2;
1869       style->text_aa[i].green = (style->text[i].green + style->base[i].green) / 2;
1870       style->text_aa[i].blue = (style->text[i].blue + style->base[i].blue) / 2;
1871     }
1872
1873   style->black.red = 0x0000;
1874   style->black.green = 0x0000;
1875   style->black.blue = 0x0000;
1876   gdk_colormap_alloc_color (style->colormap, &style->black, FALSE, TRUE);
1877
1878   style->white.red = 0xffff;
1879   style->white.green = 0xffff;
1880   style->white.blue = 0xffff;
1881   gdk_colormap_alloc_color (style->colormap, &style->white, FALSE, TRUE);
1882
1883   gc_values_mask = GDK_GC_FOREGROUND | GDK_GC_BACKGROUND;
1884   
1885   gc_values.foreground = style->black;
1886   gc_values.background = style->white;
1887   style->black_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1888   
1889   gc_values.foreground = style->white;
1890   gc_values.background = style->black;
1891   style->white_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1892   
1893   gc_values_mask = GDK_GC_FOREGROUND;
1894
1895   for (i = 0; i < 5; i++)
1896     {
1897       if (style->rc_style && style->rc_style->bg_pixmap_name[i])
1898         style->bg_pixmap[i] = load_bg_image (style->colormap,
1899                                              &style->bg[i],
1900                                              style->rc_style->bg_pixmap_name[i]);
1901       
1902       if (!gdk_colormap_alloc_color (style->colormap, &style->fg[i], FALSE, TRUE))
1903         g_warning ("unable to allocate color: ( %d %d %d )",
1904                    style->fg[i].red, style->fg[i].green, style->fg[i].blue);
1905       if (!gdk_colormap_alloc_color (style->colormap, &style->bg[i], FALSE, TRUE))
1906         g_warning ("unable to allocate color: ( %d %d %d )",
1907                    style->bg[i].red, style->bg[i].green, style->bg[i].blue);
1908       if (!gdk_colormap_alloc_color (style->colormap, &style->light[i], FALSE, TRUE))
1909         g_warning ("unable to allocate color: ( %d %d %d )",
1910                    style->light[i].red, style->light[i].green, style->light[i].blue);
1911       if (!gdk_colormap_alloc_color (style->colormap, &style->dark[i], FALSE, TRUE))
1912         g_warning ("unable to allocate color: ( %d %d %d )",
1913                    style->dark[i].red, style->dark[i].green, style->dark[i].blue);
1914       if (!gdk_colormap_alloc_color (style->colormap, &style->mid[i], FALSE, TRUE))
1915         g_warning ("unable to allocate color: ( %d %d %d )",
1916                    style->mid[i].red, style->mid[i].green, style->mid[i].blue);
1917       if (!gdk_colormap_alloc_color (style->colormap, &style->text[i], FALSE, TRUE))
1918         g_warning ("unable to allocate color: ( %d %d %d )",
1919                    style->text[i].red, style->text[i].green, style->text[i].blue);
1920       if (!gdk_colormap_alloc_color (style->colormap, &style->base[i], FALSE, TRUE))
1921         g_warning ("unable to allocate color: ( %d %d %d )",
1922                    style->base[i].red, style->base[i].green, style->base[i].blue);
1923       if (!gdk_colormap_alloc_color (style->colormap, &style->text_aa[i], FALSE, TRUE))
1924         g_warning ("unable to allocate color: ( %d %d %d )",
1925                    style->text_aa[i].red, style->text_aa[i].green, style->text_aa[i].blue);
1926       
1927       gc_values.foreground = style->fg[i];
1928       style->fg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1929       
1930       gc_values.foreground = style->bg[i];
1931       style->bg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1932       
1933       gc_values.foreground = style->light[i];
1934       style->light_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1935       
1936       gc_values.foreground = style->dark[i];
1937       style->dark_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1938       
1939       gc_values.foreground = style->mid[i];
1940       style->mid_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1941       
1942       gc_values.foreground = style->text[i];
1943       style->text_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1944       
1945       gc_values.foreground = style->base[i];
1946       style->base_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1947
1948       gc_values.foreground = style->text_aa[i];
1949       style->text_aa_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1950     }
1951 }
1952
1953 static void
1954 gtk_style_real_unrealize (GtkStyle *style)
1955 {
1956   int i;
1957
1958   gtk_gc_release (style->black_gc);
1959   gtk_gc_release (style->white_gc);
1960       
1961   for (i = 0; i < 5; i++)
1962     {
1963       gtk_gc_release (style->fg_gc[i]);
1964       gtk_gc_release (style->bg_gc[i]);
1965       gtk_gc_release (style->light_gc[i]);
1966       gtk_gc_release (style->dark_gc[i]);
1967       gtk_gc_release (style->mid_gc[i]);
1968       gtk_gc_release (style->text_gc[i]);
1969       gtk_gc_release (style->base_gc[i]);
1970       gtk_gc_release (style->text_aa_gc[i]);
1971
1972       if (style->bg_pixmap[i] &&  style->bg_pixmap[i] != (GdkPixmap*) GDK_PARENT_RELATIVE)
1973         g_object_unref (style->bg_pixmap[i]);
1974     }
1975   
1976   gdk_colormap_free_colors (style->colormap, style->fg, 5);
1977   gdk_colormap_free_colors (style->colormap, style->bg, 5);
1978   gdk_colormap_free_colors (style->colormap, style->light, 5);
1979   gdk_colormap_free_colors (style->colormap, style->dark, 5);
1980   gdk_colormap_free_colors (style->colormap, style->mid, 5);
1981   gdk_colormap_free_colors (style->colormap, style->text, 5);
1982   gdk_colormap_free_colors (style->colormap, style->base, 5);
1983   gdk_colormap_free_colors (style->colormap, style->text_aa, 5);
1984
1985   style_unrealize_cursor_gcs (style);
1986 }
1987
1988 static void
1989 gtk_style_real_set_background (GtkStyle    *style,
1990                                GdkWindow   *window,
1991                                GtkStateType state_type)
1992 {
1993   GdkPixmap *pixmap;
1994   gint parent_relative;
1995   
1996   if (style->bg_pixmap[state_type])
1997     {
1998       if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
1999         {
2000           pixmap = NULL;
2001           parent_relative = TRUE;
2002         }
2003       else
2004         {
2005           pixmap = style->bg_pixmap[state_type];
2006           parent_relative = FALSE;
2007         }
2008       
2009       gdk_window_set_back_pixmap (window, pixmap, parent_relative);
2010     }
2011   else
2012     gdk_window_set_background (window, &style->bg[state_type]);
2013 }
2014
2015 /**
2016  * gtk_style_render_icon:
2017  * @style: a #GtkStyle
2018  * @source: the #GtkIconSource specifying the icon to render
2019  * @direction: a text direction
2020  * @state: a state
2021  * @size: the size to render the icon at. A size of (GtkIconSize)-1
2022  *        means render at the size of the source and don't scale.
2023  * @widget: the widget 
2024  * @detail: a style detail
2025  * @returns: a newly-created #GdkPixbuf containing the rendered icon
2026  *
2027  * Renders the icon specified by @source at the given @size 
2028  * according to the given parameters and returns the result in a 
2029  * pixbuf.
2030  */
2031 GdkPixbuf *
2032 gtk_style_render_icon (GtkStyle            *style,
2033                        const GtkIconSource *source,
2034                        GtkTextDirection     direction,
2035                        GtkStateType         state,
2036                        GtkIconSize          size,
2037                        GtkWidget           *widget,
2038                        const gchar         *detail)
2039 {
2040   GdkPixbuf *pixbuf;
2041   
2042   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
2043   g_return_val_if_fail (GTK_STYLE_GET_CLASS (style)->render_icon != NULL, NULL);
2044   
2045   pixbuf = GTK_STYLE_GET_CLASS (style)->render_icon (style, source, direction, state,
2046                                                      size, widget, detail);
2047
2048   g_return_val_if_fail (pixbuf != NULL, NULL);
2049
2050   return pixbuf;
2051 }
2052
2053 /* Default functions */
2054 void
2055 gtk_style_apply_default_background (GtkStyle     *style,
2056                                     GdkWindow    *window,
2057                                     gboolean      set_bg,
2058                                     GtkStateType  state_type, 
2059                                     GdkRectangle *area, 
2060                                     gint          x, 
2061                                     gint          y, 
2062                                     gint          width, 
2063                                     gint          height)
2064 {
2065   GdkRectangle new_rect, old_rect;
2066   
2067   if (area)
2068     {
2069       old_rect.x = x;
2070       old_rect.y = y;
2071       old_rect.width = width;
2072       old_rect.height = height;
2073       
2074       if (!gdk_rectangle_intersect (area, &old_rect, &new_rect))
2075         return;
2076     }
2077   else
2078     {
2079       new_rect.x = x;
2080       new_rect.y = y;
2081       new_rect.width = width;
2082       new_rect.height = height;
2083     }
2084   
2085   if (!style->bg_pixmap[state_type] ||
2086       GDK_IS_PIXMAP (window) ||
2087       (!set_bg && style->bg_pixmap[state_type] != (GdkPixmap*) GDK_PARENT_RELATIVE))
2088     {
2089       GdkGC *gc = style->bg_gc[state_type];
2090       
2091       if (style->bg_pixmap[state_type])
2092         {
2093           gdk_gc_set_fill (gc, GDK_TILED);
2094           gdk_gc_set_tile (gc, style->bg_pixmap[state_type]);
2095         }
2096       
2097       gdk_draw_rectangle (window, gc, TRUE, 
2098                           new_rect.x, new_rect.y, new_rect.width, new_rect.height);
2099       if (style->bg_pixmap[state_type])
2100         gdk_gc_set_fill (gc, GDK_SOLID);
2101     }
2102   else
2103     {
2104       if (set_bg)
2105         {
2106           if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
2107             gdk_window_set_back_pixmap (window, NULL, TRUE);
2108           else
2109             gdk_window_set_back_pixmap (window, style->bg_pixmap[state_type], FALSE);
2110         }
2111       
2112       gdk_window_clear_area (window, 
2113                              new_rect.x, new_rect.y, 
2114                              new_rect.width, new_rect.height);
2115     }
2116 }
2117
2118 static GdkPixbuf*
2119 scale_or_ref (GdkPixbuf *src,
2120               gint width,
2121               gint height)
2122 {
2123   if (width == gdk_pixbuf_get_width (src) &&
2124       height == gdk_pixbuf_get_height (src))
2125     {
2126       return g_object_ref (src);
2127     }
2128   else
2129     {
2130       return gdk_pixbuf_scale_simple (src,
2131                                       width, height,
2132                                       GDK_INTERP_BILINEAR);
2133     }
2134 }
2135
2136 static GdkPixbuf *
2137 gtk_default_render_icon (GtkStyle            *style,
2138                          const GtkIconSource *source,
2139                          GtkTextDirection     direction,
2140                          GtkStateType         state,
2141                          GtkIconSize          size,
2142                          GtkWidget           *widget,
2143                          const gchar         *detail)
2144 {
2145   gint width = 1;
2146   gint height = 1;
2147   GdkPixbuf *scaled;
2148   GdkPixbuf *stated;
2149   GdkPixbuf *base_pixbuf;
2150   GdkScreen *screen;
2151   GtkSettings *settings;
2152
2153   /* Oddly, style can be NULL in this function, because
2154    * GtkIconSet can be used without a style and if so
2155    * it uses this function.
2156    */
2157
2158   base_pixbuf = gtk_icon_source_get_pixbuf (source);
2159
2160   g_return_val_if_fail (base_pixbuf != NULL, NULL);
2161
2162   if (widget && gtk_widget_has_screen (widget))
2163     {
2164       screen = gtk_widget_get_screen (widget);
2165       settings = gtk_settings_get_for_screen (screen);
2166     }
2167   else if (style->colormap)
2168     {
2169       screen = gdk_colormap_get_screen (style->colormap);
2170       settings = gtk_settings_get_for_screen (screen);
2171     }
2172   else
2173     {
2174       settings = gtk_settings_get_default ();
2175       GTK_NOTE (MULTIHEAD,
2176                 g_warning ("Using the default screen for gtk_default_render_icon()"));
2177     }
2178
2179   
2180   if (size != (GtkIconSize) -1 && !gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
2181     {
2182       g_warning (G_STRLOC ": invalid icon size '%d'", size);
2183       return NULL;
2184     }
2185
2186   /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
2187    * leave it alone.
2188    */
2189   if (size != (GtkIconSize)-1 && gtk_icon_source_get_size_wildcarded (source))
2190     scaled = scale_or_ref (base_pixbuf, width, height);
2191   else
2192     scaled = g_object_ref (base_pixbuf);
2193
2194   /* If the state was wildcarded, then generate a state. */
2195   if (gtk_icon_source_get_state_wildcarded (source))
2196     {
2197       if (state == GTK_STATE_INSENSITIVE)
2198         {
2199           stated = gdk_pixbuf_copy (scaled);      
2200           
2201           gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2202                                             0.8, TRUE);
2203           
2204           g_object_unref (scaled);
2205         }
2206       else if (state == GTK_STATE_PRELIGHT)
2207         {
2208           stated = gdk_pixbuf_copy (scaled);      
2209           
2210           gdk_pixbuf_saturate_and_pixelate (scaled, stated,
2211                                             1.2, FALSE);
2212           
2213           g_object_unref (scaled);
2214         }
2215       else
2216         {
2217           stated = scaled;
2218         }
2219     }
2220   else
2221     stated = scaled;
2222   
2223   return stated;
2224 }
2225
2226 static void
2227 sanitize_size (GdkWindow *window,
2228                gint      *width,
2229                gint      *height)
2230 {
2231   if ((*width == -1) && (*height == -1))
2232     gdk_drawable_get_size (window, width, height);
2233   else if (*width == -1)
2234     gdk_drawable_get_size (window, width, NULL);
2235   else if (*height == -1)
2236     gdk_drawable_get_size (window, NULL, height);
2237 }
2238
2239 static GdkBitmap * 
2240 get_indicator_for_screen (GdkDrawable   *drawable,
2241                           IndicatorPart  part)
2242                           
2243 {
2244   GdkScreen *screen = gdk_drawable_get_screen (drawable);
2245   GdkBitmap *bitmap;
2246   GList *tmp_list;
2247   
2248   tmp_list = indicator_parts[part].bmap_list;
2249   while (tmp_list)
2250     {
2251       bitmap = tmp_list->data;
2252       
2253       if (gdk_drawable_get_screen (bitmap) == screen)
2254         return bitmap;
2255       
2256       tmp_list = tmp_list->next;
2257     }
2258   
2259   bitmap = gdk_bitmap_create_from_data (drawable,
2260                                         (gchar *)indicator_parts[part].bits,
2261                                         INDICATOR_PART_SIZE, INDICATOR_PART_SIZE);
2262   indicator_parts[part].bmap_list = g_list_prepend (indicator_parts[part].bmap_list, bitmap);
2263
2264   return bitmap;
2265 }
2266
2267 static void
2268 draw_part (GdkDrawable  *drawable,
2269            GdkGC        *gc,
2270            GdkRectangle *area,
2271            gint          x,
2272            gint          y,
2273            IndicatorPart part)
2274 {
2275   if (area)
2276     gdk_gc_set_clip_rectangle (gc, area);
2277   
2278   gdk_gc_set_ts_origin (gc, x, y);
2279   gdk_gc_set_stipple (gc, get_indicator_for_screen (drawable, part));
2280   gdk_gc_set_fill (gc, GDK_STIPPLED);
2281
2282   gdk_draw_rectangle (drawable, gc, TRUE, x, y, INDICATOR_PART_SIZE, INDICATOR_PART_SIZE);
2283
2284   gdk_gc_set_fill (gc, GDK_SOLID);
2285
2286   if (area)
2287     gdk_gc_set_clip_rectangle (gc, NULL);
2288 }
2289
2290 static void
2291 gtk_default_draw_hline (GtkStyle     *style,
2292                         GdkWindow    *window,
2293                         GtkStateType  state_type,
2294                         GdkRectangle  *area,
2295                         GtkWidget     *widget,
2296                         const gchar   *detail,
2297                         gint          x1,
2298                         gint          x2,
2299                         gint          y)
2300 {
2301   gint thickness_light;
2302   gint thickness_dark;
2303   gint i;
2304   
2305   g_return_if_fail (GTK_IS_STYLE (style));
2306   g_return_if_fail (window != NULL);
2307   
2308   thickness_light = style->ythickness / 2;
2309   thickness_dark = style->ythickness - thickness_light;
2310   
2311   if (area)
2312     {
2313       gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
2314       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
2315     }
2316   
2317   if (detail && !strcmp (detail, "label"))
2318     {
2319       if (state_type == GTK_STATE_INSENSITIVE)
2320         gdk_draw_line (window, style->white_gc, x1 + 1, y + 1, x2 + 1, y + 1);   
2321       gdk_draw_line (window, style->fg_gc[state_type], x1, y, x2, y);     
2322     }
2323   else
2324     {
2325       for (i = 0; i < thickness_dark; i++)
2326         {
2327           gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x2 - i - 1, y + i);
2328           gdk_draw_line (window, style->light_gc[state_type], x2 - i, y + i, x2, y + i);
2329         }
2330       
2331       y += thickness_dark;
2332       for (i = 0; i < thickness_light; i++)
2333         {
2334           gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x1 + thickness_light - i - 1, y + i);
2335           gdk_draw_line (window, style->light_gc[state_type], x1 + thickness_light - i, y + i, x2, y + i);
2336         }
2337     }
2338   
2339   if (area)
2340     {
2341       gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
2342       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
2343     }
2344 }
2345
2346
2347 static void
2348 gtk_default_draw_vline (GtkStyle     *style,
2349                         GdkWindow    *window,
2350                         GtkStateType  state_type,
2351                         GdkRectangle  *area,
2352                         GtkWidget     *widget,
2353                         const gchar   *detail,
2354                         gint          y1,
2355                         gint          y2,
2356                         gint          x)
2357 {
2358   gint thickness_light;
2359   gint thickness_dark;
2360   gint i;
2361   
2362   g_return_if_fail (GTK_IS_STYLE (style));
2363   g_return_if_fail (window != NULL);
2364   
2365   thickness_light = style->xthickness / 2;
2366   thickness_dark = style->xthickness - thickness_light;
2367   
2368   if (area)
2369     {
2370       gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
2371       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
2372     }
2373   for (i = 0; i < thickness_dark; i++)
2374     { 
2375       gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y2 - i - 1);
2376       gdk_draw_line (window, style->light_gc[state_type], x + i, y2 - i, x + i, y2);
2377     }
2378   
2379   x += thickness_dark;
2380   for (i = 0; i < thickness_light; i++)
2381     {
2382       gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y1 + thickness_light - i - 1);
2383       gdk_draw_line (window, style->light_gc[state_type], x + i, y1 + thickness_light - i, x + i, y2);
2384     }
2385   if (area)
2386     {
2387       gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
2388       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
2389     }
2390 }
2391
2392 static void
2393 draw_thin_shadow (GtkStyle      *style,
2394                   GdkWindow     *window,
2395                   GtkStateType   state,
2396                   GdkRectangle  *area,
2397                   gint           x,
2398                   gint           y,
2399                   gint           width,
2400                   gint           height)
2401 {
2402   GdkGC *gc1, *gc2;
2403
2404   sanitize_size (window, &width, &height);
2405   
2406   gc1 = style->light_gc[state];
2407   gc2 = style->dark_gc[state];
2408   
2409   if (area)
2410     {
2411       gdk_gc_set_clip_rectangle (gc1, area);
2412       gdk_gc_set_clip_rectangle (gc2, area);
2413     }
2414   
2415   gdk_draw_line (window, gc1,
2416                  x, y + height - 1, x + width - 1, y + height - 1);
2417   gdk_draw_line (window, gc1,
2418                  x + width - 1, y,  x + width - 1, y + height - 1);
2419       
2420   gdk_draw_line (window, gc2,
2421                  x, y, x + width - 2, y);
2422   gdk_draw_line (window, gc2,
2423                  x, y, x, y + height - 2);
2424
2425   if (area)
2426     {
2427       gdk_gc_set_clip_rectangle (gc1, NULL);
2428       gdk_gc_set_clip_rectangle (gc2, NULL);
2429     }
2430 }
2431
2432 static void
2433 draw_spinbutton_shadow (GtkStyle        *style,
2434                         GdkWindow       *window,
2435                         GtkStateType     state,
2436                         GtkTextDirection direction,
2437                         GdkRectangle    *area,
2438                         gint             x,
2439                         gint             y,
2440                         gint             width,
2441                         gint             height)
2442 {
2443   sanitize_size (window, &width, &height);
2444
2445   if (area)
2446     {
2447       gdk_gc_set_clip_rectangle (style->black_gc, area);
2448       gdk_gc_set_clip_rectangle (style->bg_gc[state], area);
2449       gdk_gc_set_clip_rectangle (style->dark_gc[state], area);
2450       gdk_gc_set_clip_rectangle (style->light_gc[state], area);
2451     }
2452
2453   if (direction == GTK_TEXT_DIR_LTR)
2454     {
2455       gdk_draw_line (window, style->dark_gc[state],
2456                      x, y, x + width - 1, y);
2457       gdk_draw_line (window, style->black_gc,
2458                      x, y + 1, x + width - 2, y + 1);
2459       gdk_draw_line (window, style->black_gc,
2460                      x + width - 2, y + 2, x + width - 2, y + height - 3);
2461       gdk_draw_line (window, style->light_gc[state],
2462                      x + width - 1, y + 1, x + width - 1, y + height - 2);
2463       gdk_draw_line (window, style->light_gc[state],
2464                      x, y + height - 1, x + width - 1, y + height - 1);
2465       gdk_draw_line (window, style->bg_gc[state],
2466                      x, y + height - 2, x + width - 2, y + height - 2);
2467       gdk_draw_line (window, style->black_gc,
2468                      x, y + 2, x, y + height - 3);
2469     }
2470   else
2471     {
2472       gdk_draw_line (window, style->dark_gc[state],
2473                      x, y, x + width - 1, y);
2474       gdk_draw_line (window, style->dark_gc[state],
2475                      x, y + 1, x, y + height - 1);
2476       gdk_draw_line (window, style->black_gc,
2477                      x + 1, y + 1, x + width - 1, y + 1);
2478       gdk_draw_line (window, style->black_gc,
2479                      x + 1, y + 2, x + 1, y + height - 2);
2480       gdk_draw_line (window, style->black_gc,
2481                      x + width - 1, y + 2, x + width - 1, y + height - 3);
2482       gdk_draw_line (window, style->light_gc[state],
2483                      x + 1, y + height - 1, x + width - 1, y + height - 1);
2484       gdk_draw_line (window, style->bg_gc[state],
2485                      x + 2, y + height - 2, x + width - 1, y + height - 2);
2486     }
2487   
2488   if (area)
2489     {
2490       gdk_gc_set_clip_rectangle (style->black_gc, NULL);
2491       gdk_gc_set_clip_rectangle (style->bg_gc[state], NULL);
2492       gdk_gc_set_clip_rectangle (style->dark_gc[state], NULL);
2493       gdk_gc_set_clip_rectangle (style->light_gc[state], NULL);
2494     }
2495 }
2496
2497 static void
2498 draw_menu_shadow (GtkStyle        *style,
2499                   GdkWindow       *window,
2500                   GtkStateType     state,
2501                   GdkRectangle    *area,
2502                   gint             x,
2503                   gint             y,
2504                   gint             width,
2505                   gint             height)
2506 {
2507   if (style->ythickness > 0)
2508     {
2509       if (style->ythickness > 1)
2510         {
2511           gdk_draw_line (window, style->dark_gc[state],
2512                          x + 1, y + height - 2, x + width - 2, y + height - 2);
2513           gdk_draw_line (window, style->black_gc,
2514                          x, y + height - 1, x + width - 1, y + height - 1);
2515         }
2516       else
2517         {
2518           gdk_draw_line (window, style->dark_gc[state],
2519                          x + 1, y + height - 1, x + width - 1, y + height - 1);
2520         }
2521     }
2522   
2523   if (style->xthickness > 0)
2524     {
2525       if (style->xthickness > 1)
2526         {
2527           gdk_draw_line (window, style->dark_gc[state],
2528                          x + width - 2, y + 1, x + width - 2, y + height - 2);
2529           
2530           gdk_draw_line (window, style->black_gc,
2531                          x + width - 1, y, x + width - 1, y + height - 1);
2532         }
2533       else
2534         {
2535           gdk_draw_line (window, style->dark_gc[state],
2536                          x + width - 1, y + 1, x + width - 1, y + height - 1);
2537         }
2538     }
2539   
2540   /* Light around top and left */
2541   
2542   if (style->ythickness > 0)
2543     gdk_draw_line (window, style->black_gc,
2544                    x, y, x + width - 2, y);
2545   if (style->xthickness > 0)
2546     gdk_draw_line (window, style->black_gc,
2547                    x, y, x, y + height - 2);
2548   
2549   if (style->ythickness > 1)
2550     gdk_draw_line (window, style->light_gc[state],
2551                    x + 1, y + 1, x + width - 3, y + 1);
2552   if (style->xthickness > 1)
2553     gdk_draw_line (window, style->light_gc[state],
2554                    x + 1, y + 1, x + 1, y + height - 3);
2555 }
2556
2557 static void
2558 gtk_default_draw_shadow (GtkStyle      *style,
2559                          GdkWindow     *window,
2560                          GtkStateType   state_type,
2561                          GtkShadowType  shadow_type,
2562                          GdkRectangle  *area,
2563                          GtkWidget     *widget,
2564                          const gchar   *detail,
2565                          gint           x,
2566                          gint           y,
2567                          gint           width,
2568                          gint           height)
2569 {
2570   GdkGC *gc1 = NULL;
2571   GdkGC *gc2 = NULL;
2572   gint thickness_light;
2573   gint thickness_dark;
2574   gint i;
2575   
2576   g_return_if_fail (GTK_IS_STYLE (style));
2577   g_return_if_fail (window != NULL);
2578
2579   if (shadow_type == GTK_SHADOW_IN)
2580     {
2581       if (detail && (strcmp (detail, "buttondefault") == 0))
2582         {
2583           sanitize_size (window, &width, &height);
2584
2585           gdk_draw_rectangle (window, style->black_gc, FALSE,
2586                               x, y, width - 1, height - 1);
2587           
2588           return;
2589         }
2590       if (detail && strcmp (detail, "trough") == 0)
2591         {
2592           draw_thin_shadow (style, window, state_type, area,
2593                             x, y, width, height);
2594           return;
2595         }
2596       if (widget && GTK_IS_SPIN_BUTTON (widget) &&
2597          detail && strcmp (detail, "spinbutton") == 0)
2598         {
2599           draw_spinbutton_shadow (style, window, state_type,
2600                                   gtk_widget_get_direction (widget), area, x, y, width, height);
2601           
2602           return;
2603         }
2604     }
2605
2606   if (shadow_type == GTK_SHADOW_OUT && detail && strcmp (detail, "menu") == 0)
2607     {
2608       draw_menu_shadow (style, window, state_type, area, x, y, width, height);
2609       return;
2610     }
2611   
2612   sanitize_size (window, &width, &height);
2613   
2614   switch (shadow_type)
2615     {
2616     case GTK_SHADOW_NONE:
2617       return;
2618     case GTK_SHADOW_IN:
2619     case GTK_SHADOW_ETCHED_IN:
2620       gc1 = style->light_gc[state_type];
2621       gc2 = style->dark_gc[state_type];
2622       break;
2623     case GTK_SHADOW_OUT:
2624     case GTK_SHADOW_ETCHED_OUT:
2625       gc1 = style->dark_gc[state_type];
2626       gc2 = style->light_gc[state_type];
2627       break;
2628     }
2629   
2630   if (area)
2631     {
2632       gdk_gc_set_clip_rectangle (gc1, area);
2633       gdk_gc_set_clip_rectangle (gc2, area);
2634       if (shadow_type == GTK_SHADOW_IN || 
2635           shadow_type == GTK_SHADOW_OUT)
2636         {
2637           gdk_gc_set_clip_rectangle (style->black_gc, area);
2638           gdk_gc_set_clip_rectangle (style->bg_gc[state_type], area);
2639         }
2640     }
2641   
2642   switch (shadow_type)
2643     {
2644     case GTK_SHADOW_NONE:
2645       break;
2646       
2647     case GTK_SHADOW_IN:
2648       /* Light around right and bottom edge */
2649
2650       if (style->ythickness > 0)
2651         gdk_draw_line (window, gc1,
2652                        x, y + height - 1, x + width - 1, y + height - 1);
2653       if (style->xthickness > 0)
2654         gdk_draw_line (window, gc1,
2655                        x + width - 1, y, x + width - 1, y + height - 1);
2656
2657       if (style->ythickness > 1)
2658         gdk_draw_line (window, style->bg_gc[state_type],
2659                        x + 1, y + height - 2, x + width - 2, y + height - 2);
2660       if (style->xthickness > 1)
2661         gdk_draw_line (window, style->bg_gc[state_type],
2662                        x + width - 2, y + 1, x + width - 2, y + height - 2);
2663
2664       /* Dark around left and top */
2665
2666       if (style->ythickness > 1)
2667         gdk_draw_line (window, style->black_gc,
2668                        x + 1, y + 1, x + width - 2, y + 1);
2669       if (style->xthickness > 1)
2670         gdk_draw_line (window, style->black_gc,
2671                        x + 1, y + 1, x + 1, y + height - 2);
2672
2673       if (style->ythickness > 0)
2674         gdk_draw_line (window, gc2,
2675                        x, y, x + width - 1, y);
2676       if (style->xthickness > 0)
2677         gdk_draw_line (window, gc2,
2678                        x, y, x, y + height - 1);
2679       break;
2680       
2681     case GTK_SHADOW_OUT:
2682       /* Dark around right and bottom edge */
2683
2684       if (style->ythickness > 0)
2685         {
2686           if (style->ythickness > 1)
2687             {
2688               gdk_draw_line (window, gc1,
2689                              x + 1, y + height - 2, x + width - 2, y + height - 2);
2690               gdk_draw_line (window, style->black_gc,
2691                              x, y + height - 1, x + width - 1, y + height - 1);
2692             }
2693           else
2694             {
2695               gdk_draw_line (window, gc1,
2696                              x + 1, y + height - 1, x + width - 1, y + height - 1);
2697             }
2698         }
2699
2700       if (style->xthickness > 0)
2701         {
2702           if (style->xthickness > 1)
2703             {
2704               gdk_draw_line (window, gc1,
2705                              x + width - 2, y + 1, x + width - 2, y + height - 2);
2706               
2707               gdk_draw_line (window, style->black_gc,
2708                              x + width - 1, y, x + width - 1, y + height - 1);
2709             }
2710           else
2711             {
2712               gdk_draw_line (window, gc1,
2713                              x + width - 1, y + 1, x + width - 1, y + height - 1);
2714             }
2715         }
2716       
2717       /* Light around top and left */
2718
2719       if (style->ythickness > 0)
2720         gdk_draw_line (window, gc2,
2721                        x, y, x + width - 2, y);
2722       if (style->xthickness > 0)
2723         gdk_draw_line (window, gc2,
2724                        x, y, x, y + height - 2);
2725
2726       if (style->ythickness > 1)
2727         gdk_draw_line (window, style->bg_gc[state_type],
2728                        x + 1, y + 1, x + width - 3, y + 1);
2729       if (style->xthickness > 1)
2730         gdk_draw_line (window, style->bg_gc[state_type],
2731                        x + 1, y + 1, x + 1, y + height - 3);
2732       break;
2733       
2734     case GTK_SHADOW_ETCHED_IN:
2735     case GTK_SHADOW_ETCHED_OUT:
2736       if (style->xthickness > 0)
2737         {
2738           if (style->xthickness > 1)
2739             {
2740               thickness_light = 1;
2741               thickness_dark = 1;
2742       
2743               for (i = 0; i < thickness_dark; i++)
2744                 {
2745                   gdk_draw_line (window, gc1,
2746                                  x + width - i - 1,
2747                                  y + i,
2748                                  x + width - i - 1,
2749                                  y + height - i - 1);
2750                   gdk_draw_line (window, gc2,
2751                                  x + i,
2752                                  y + i,
2753                                  x + i,
2754                                  y + height - i - 2);
2755                 }
2756       
2757               for (i = 0; i < thickness_light; i++)
2758                 {
2759                   gdk_draw_line (window, gc1,
2760                                  x + thickness_dark + i,
2761                                  y + thickness_dark + i,
2762                                  x + thickness_dark + i,
2763                                  y + height - thickness_dark - i - 1);
2764                   gdk_draw_line (window, gc2,
2765                                  x + width - thickness_light - i - 1,
2766                                  y + thickness_dark + i,
2767                                  x + width - thickness_light - i - 1,
2768                                  y + height - thickness_light - 1);
2769                 }
2770             }
2771           else
2772             {
2773               gdk_draw_line (window, 
2774                              style->dark_gc[state_type],
2775                              x, y, x, y + height);                         
2776               gdk_draw_line (window, 
2777                              style->dark_gc[state_type],
2778                              x + width, y, x + width, y + height);
2779             }
2780         }
2781
2782       if (style->ythickness > 0)
2783         {
2784           if (style->ythickness > 1)
2785             {
2786               thickness_light = 1;
2787               thickness_dark = 1;
2788       
2789               for (i = 0; i < thickness_dark; i++)
2790                 {
2791                   gdk_draw_line (window, gc1,
2792                                  x + i,
2793                                  y + height - i - 1,
2794                                  x + width - i - 1,
2795                                  y + height - i - 1);
2796           
2797                   gdk_draw_line (window, gc2,
2798                                  x + i,
2799                                  y + i,
2800                                  x + width - i - 2,
2801                                  y + i);
2802                 }
2803       
2804               for (i = 0; i < thickness_light; i++)
2805                 {
2806                   gdk_draw_line (window, gc1,
2807                                  x + thickness_dark + i,
2808                                  y + thickness_dark + i,
2809                                  x + width - thickness_dark - i - 2,
2810                                  y + thickness_dark + i);
2811           
2812                   gdk_draw_line (window, gc2,
2813                                  x + thickness_dark + i,
2814                                  y + height - thickness_light - i - 1,
2815                                  x + width - thickness_light - 1,
2816                                  y + height - thickness_light - i - 1);
2817                 }
2818             }
2819           else
2820             {
2821               gdk_draw_line (window, 
2822                              style->dark_gc[state_type],
2823                              x, y, x + width, y);
2824               gdk_draw_line (window, 
2825                              style->dark_gc[state_type],
2826                              x, y + height, x + width, y + height);
2827             }
2828         }
2829       
2830       break;
2831     }
2832
2833   if (shadow_type == GTK_SHADOW_IN &&
2834       widget && GTK_IS_SPIN_BUTTON (widget) &&
2835       detail && strcmp (detail, "entry") == 0)
2836     {
2837       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2838         {
2839           gdk_draw_line (window,
2840                          style->base_gc[state_type],
2841                          x + width - 1, y + 2,
2842                          x + width - 1, y + height - 3);
2843           gdk_draw_line (window,
2844                          style->base_gc[state_type],
2845                          x + width - 2, y + 2,
2846                          x + width - 2, y + height - 3);
2847           gdk_draw_point (window,
2848                           style->black_gc,
2849                           x + width - 1, y + 1);
2850           gdk_draw_point (window,
2851                           style->bg_gc[state_type],
2852                           x + width - 1, y + height - 2);
2853         }
2854       else
2855         {
2856           gdk_draw_line (window,
2857                          style->base_gc[state_type],
2858                          x, y + 2,
2859                          x, y + height - 3);
2860           gdk_draw_line (window,
2861                          style->base_gc[state_type],
2862                          x + 1, y + 2,
2863                          x + 1, y + height - 3);
2864           gdk_draw_point (window,
2865                           style->black_gc,
2866                           x, y + 1);
2867           gdk_draw_line (window,
2868                          style->bg_gc[state_type],
2869                          x, y + height - 2,
2870                          x + 1, y + height - 2);
2871           gdk_draw_point (window,
2872                           style->light_gc[state_type],
2873                           x, y + height - 1);
2874         }
2875     }
2876
2877
2878   if (area)
2879     {
2880       gdk_gc_set_clip_rectangle (gc1, NULL);
2881       gdk_gc_set_clip_rectangle (gc2, NULL);
2882       if (shadow_type == GTK_SHADOW_IN || 
2883           shadow_type == GTK_SHADOW_OUT)
2884         {
2885           gdk_gc_set_clip_rectangle (style->black_gc, NULL);
2886           gdk_gc_set_clip_rectangle (style->bg_gc[state_type], NULL);
2887         }
2888     }
2889 }
2890
2891 static void
2892 gtk_default_draw_polygon (GtkStyle      *style,
2893                           GdkWindow     *window,
2894                           GtkStateType   state_type,
2895                           GtkShadowType  shadow_type,
2896                           GdkRectangle  *area,
2897                           GtkWidget     *widget,
2898                           const gchar   *detail,
2899                           GdkPoint      *points,
2900                           gint           npoints,
2901                           gboolean       fill)
2902 {
2903   static const gdouble pi_over_4 = G_PI_4;
2904   static const gdouble pi_3_over_4 = G_PI_4 * 3;
2905   GdkGC *gc1;
2906   GdkGC *gc2;
2907   GdkGC *gc3;
2908   GdkGC *gc4;
2909   gdouble angle;
2910   gint xadjust;
2911   gint yadjust;
2912   gint i;
2913   
2914   g_return_if_fail (GTK_IS_STYLE (style));
2915   g_return_if_fail (window != NULL);
2916   g_return_if_fail (points != NULL);
2917   
2918   switch (shadow_type)
2919     {
2920     case GTK_SHADOW_IN:
2921       gc1 = style->bg_gc[state_type];
2922       gc2 = style->dark_gc[state_type];
2923       gc3 = style->light_gc[state_type];
2924       gc4 = style->black_gc;
2925       break;
2926     case GTK_SHADOW_ETCHED_IN:
2927       gc1 = style->light_gc[state_type];
2928       gc2 = style->dark_gc[state_type];
2929       gc3 = style->dark_gc[state_type];
2930       gc4 = style->light_gc[state_type];
2931       break;
2932     case GTK_SHADOW_OUT:
2933       gc1 = style->dark_gc[state_type];
2934       gc2 = style->light_gc[state_type];
2935       gc3 = style->black_gc;
2936       gc4 = style->bg_gc[state_type];
2937       break;
2938     case GTK_SHADOW_ETCHED_OUT:
2939       gc1 = style->dark_gc[state_type];
2940       gc2 = style->light_gc[state_type];
2941       gc3 = style->light_gc[state_type];
2942       gc4 = style->dark_gc[state_type];
2943       break;
2944     default:
2945       return;
2946     }
2947   
2948   if (area)
2949     {
2950       gdk_gc_set_clip_rectangle (gc1, area);
2951       gdk_gc_set_clip_rectangle (gc2, area);
2952       gdk_gc_set_clip_rectangle (gc3, area);
2953       gdk_gc_set_clip_rectangle (gc4, area);
2954     }
2955   
2956   if (fill)
2957     gdk_draw_polygon (window, style->bg_gc[state_type], TRUE, points, npoints);
2958   
2959   npoints--;
2960   
2961   for (i = 0; i < npoints; i++)
2962     {
2963       if ((points[i].x == points[i+1].x) &&
2964           (points[i].y == points[i+1].y))
2965         {
2966           angle = 0;
2967         }
2968       else
2969         {
2970           angle = atan2 (points[i+1].y - points[i].y,
2971                          points[i+1].x - points[i].x);
2972         }
2973       
2974       if ((angle > -pi_3_over_4) && (angle < pi_over_4))
2975         {
2976           if (angle > -pi_over_4)
2977             {
2978               xadjust = 0;
2979               yadjust = 1;
2980             }
2981           else
2982             {
2983               xadjust = 1;
2984               yadjust = 0;
2985             }
2986           
2987           gdk_draw_line (window, gc1,
2988                          points[i].x-xadjust, points[i].y-yadjust,
2989                          points[i+1].x-xadjust, points[i+1].y-yadjust);
2990           gdk_draw_line (window, gc3,
2991                          points[i].x, points[i].y,
2992                          points[i+1].x, points[i+1].y);
2993         }
2994       else
2995         {
2996           if ((angle < -pi_3_over_4) || (angle > pi_3_over_4))
2997             {
2998               xadjust = 0;
2999               yadjust = 1;
3000             }
3001           else
3002             {
3003               xadjust = 1;
3004               yadjust = 0;
3005             }
3006           
3007           gdk_draw_line (window, gc4,
3008                          points[i].x+xadjust, points[i].y+yadjust,
3009                          points[i+1].x+xadjust, points[i+1].y+yadjust);
3010           gdk_draw_line (window, gc2,
3011                          points[i].x, points[i].y,
3012                          points[i+1].x, points[i+1].y);
3013         }
3014     }
3015
3016   if (area)
3017     {
3018       gdk_gc_set_clip_rectangle (gc1, NULL);
3019       gdk_gc_set_clip_rectangle (gc2, NULL);
3020       gdk_gc_set_clip_rectangle (gc3, NULL);
3021       gdk_gc_set_clip_rectangle (gc4, NULL);
3022     }
3023 }
3024
3025 static void
3026 draw_arrow (GdkWindow     *window,
3027             GdkGC         *gc,
3028             GdkRectangle  *area,
3029             GtkArrowType   arrow_type,
3030             gint           x,
3031             gint           y,
3032             gint           width,
3033             gint           height)
3034 {
3035   gint i, j;
3036
3037   if (area)
3038     gdk_gc_set_clip_rectangle (gc, area);
3039
3040   if (arrow_type == GTK_ARROW_DOWN)
3041     {
3042       for (i = 0, j = 0; i < height; i++, j++)
3043         gdk_draw_line (window, gc, x + j, y + i, x + width - j - 1, y + i);
3044     }
3045   else if (arrow_type == GTK_ARROW_UP)
3046     {
3047       for (i = height - 1, j = 0; i >= 0; i--, j++)
3048         gdk_draw_line (window, gc, x + j, y + i, x + width - j - 1, y + i);
3049     }
3050   else if (arrow_type == GTK_ARROW_LEFT)
3051     {
3052       for (i = width - 1, j = 0; i >= 0; i--, j++)
3053         gdk_draw_line (window, gc, x + i, y + j, x + i, y + height - j - 1);
3054     }
3055   else if (arrow_type == GTK_ARROW_RIGHT)
3056     {
3057       for (i = 0, j = 0; i < width; i++, j++)
3058         gdk_draw_line (window, gc, x + i, y + j, x + i, y + height - j - 1);
3059     }
3060
3061   if (area)
3062     gdk_gc_set_clip_rectangle (gc, NULL);
3063 }
3064
3065 static void
3066 calculate_arrow_geometry (GtkArrowType  arrow_type,
3067                           gint         *x,
3068                           gint         *y,
3069                           gint         *width,
3070                           gint         *height)
3071 {
3072   gint w = *width;
3073   gint h = *height;
3074   
3075   switch (arrow_type)
3076     {
3077     case GTK_ARROW_UP:
3078     case GTK_ARROW_DOWN:
3079       w += (w % 2) - 1;
3080       h = (w / 2 + 1);
3081       
3082       if (h > *height)
3083         {
3084           h = *height;
3085           w = 2 * h - 1;
3086         }
3087       
3088       if (arrow_type == GTK_ARROW_DOWN)
3089         {
3090           if (*height % 2 == 1 || h % 2 == 0)
3091             *height += 1;
3092         }
3093       else
3094         {
3095           if (*height % 2 == 0 || h % 2 == 0)
3096             *height -= 1;
3097         }
3098       break;
3099
3100     case GTK_ARROW_RIGHT:
3101     case GTK_ARROW_LEFT:
3102       h += (h % 2) - 1;
3103       w = (h / 2 + 1);
3104       
3105       if (w > *width)
3106         {
3107           w = *width;
3108           h = 2 * w - 1;
3109         }
3110       
3111       if (arrow_type == GTK_ARROW_RIGHT)
3112         {
3113           if (*width % 2 == 1 || w % 2 == 0)
3114             *width += 1;
3115         }
3116       else
3117         {
3118           if (*width % 2 == 0 || w % 2 == 0)
3119             *width -= 1;
3120         }
3121       break;
3122       
3123     default:
3124       /* should not be reached */
3125       break;
3126     }
3127
3128   *x += (*width - w) / 2;
3129   *y += (*height - h) / 2;
3130   *height = h;
3131   *width = w;
3132 }
3133
3134 static void
3135 gtk_default_draw_arrow (GtkStyle      *style,
3136                         GdkWindow     *window,
3137                         GtkStateType   state,
3138                         GtkShadowType  shadow,
3139                         GdkRectangle  *area,
3140                         GtkWidget     *widget,
3141                         const gchar   *detail,
3142                         GtkArrowType   arrow_type,
3143                         gboolean       fill,
3144                         gint           x,
3145                         gint           y,
3146                         gint           width,
3147                         gint           height)
3148 {
3149   gint original_width, original_x;
3150   
3151   sanitize_size (window, &width, &height);
3152
3153   original_width = width;
3154   original_x = x;
3155
3156   calculate_arrow_geometry (arrow_type, &x, &y, &width, &height);
3157
3158   if (detail && strcmp (detail, "menu_scroll_arrow_up") == 0)
3159     y++;
3160
3161   if (state == GTK_STATE_INSENSITIVE)
3162     draw_arrow (window, style->white_gc, area, arrow_type,
3163                 x + 1, y + 1, width, height);
3164   draw_arrow (window, style->fg_gc[state], area, arrow_type,
3165               x, y, width, height);
3166 }
3167
3168 static void
3169 gtk_default_draw_diamond (GtkStyle      *style,
3170                           GdkWindow     *window,
3171                           GtkStateType   state_type,
3172                           GtkShadowType  shadow_type,
3173                           GdkRectangle  *area,
3174                           GtkWidget     *widget,
3175                           const gchar   *detail,
3176                           gint           x,
3177                           gint           y,
3178                           gint           width,
3179                           gint           height)
3180 {
3181   gint half_width;
3182   gint half_height;
3183   GdkGC *outer_nw = NULL;
3184   GdkGC *outer_ne = NULL;
3185   GdkGC *outer_sw = NULL;
3186   GdkGC *outer_se = NULL;
3187   GdkGC *middle_nw = NULL;
3188   GdkGC *middle_ne = NULL;
3189   GdkGC *middle_sw = NULL;
3190   GdkGC *middle_se = NULL;
3191   GdkGC *inner_nw = NULL;
3192   GdkGC *inner_ne = NULL;
3193   GdkGC *inner_sw = NULL;
3194   GdkGC *inner_se = NULL;
3195   
3196   g_return_if_fail (GTK_IS_STYLE (style));
3197   g_return_if_fail (window != NULL);
3198   
3199   sanitize_size (window, &width, &height);
3200   
3201   half_width = width / 2;
3202   half_height = height / 2;
3203   
3204   if (area)
3205     {
3206       gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
3207       gdk_gc_set_clip_rectangle (style->bg_gc[state_type], area);
3208       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
3209       gdk_gc_set_clip_rectangle (style->black_gc, area);
3210     }
3211   
3212   switch (shadow_type)
3213     {
3214     case GTK_SHADOW_IN:
3215       inner_sw = inner_se = style->bg_gc[state_type];
3216       middle_sw = middle_se = style->light_gc[state_type];
3217       outer_sw = outer_se = style->light_gc[state_type];
3218       inner_nw = inner_ne = style->black_gc;
3219       middle_nw = middle_ne = style->dark_gc[state_type];
3220       outer_nw = outer_ne = style->dark_gc[state_type];
3221       break;
3222           
3223     case GTK_SHADOW_OUT:
3224       inner_sw = inner_se = style->dark_gc[state_type];
3225       middle_sw = middle_se = style->dark_gc[state_type];
3226       outer_sw = outer_se = style->black_gc;
3227       inner_nw = inner_ne = style->bg_gc[state_type];
3228       middle_nw = middle_ne = style->light_gc[state_type];
3229       outer_nw = outer_ne = style->light_gc[state_type];
3230       break;
3231
3232     case GTK_SHADOW_ETCHED_IN:
3233       inner_sw = inner_se = style->bg_gc[state_type];
3234       middle_sw = middle_se = style->dark_gc[state_type];
3235       outer_sw = outer_se = style->light_gc[state_type];
3236       inner_nw = inner_ne = style->bg_gc[state_type];
3237       middle_nw = middle_ne = style->light_gc[state_type];
3238       outer_nw = outer_ne = style->dark_gc[state_type];
3239       break;
3240
3241     case GTK_SHADOW_ETCHED_OUT:
3242       inner_sw = inner_se = style->bg_gc[state_type];
3243       middle_sw = middle_se = style->light_gc[state_type];
3244       outer_sw = outer_se = style->dark_gc[state_type];
3245       inner_nw = inner_ne = style->bg_gc[state_type];
3246       middle_nw = middle_ne = style->dark_gc[state_type];
3247       outer_nw = outer_ne = style->light_gc[state_type];
3248       break;
3249       
3250     default:
3251
3252       break;
3253     }
3254
3255   if (inner_sw)
3256     {
3257       gdk_draw_line (window, inner_sw,
3258                      x + 2, y + half_height,
3259                      x + half_width, y + height - 2);
3260       gdk_draw_line (window, inner_se,
3261                      x + half_width, y + height - 2,
3262                      x + width - 2, y + half_height);
3263       gdk_draw_line (window, middle_sw,
3264                      x + 1, y + half_height,
3265                      x + half_width, y + height - 1);
3266       gdk_draw_line (window, middle_se,
3267                      x + half_width, y + height - 1,
3268                      x + width - 1, y + half_height);
3269       gdk_draw_line (window, outer_sw,
3270                      x, y + half_height,
3271                      x + half_width, y + height);
3272       gdk_draw_line (window, outer_se,
3273                      x + half_width, y + height,
3274                      x + width, y + half_height);
3275   
3276       gdk_draw_line (window, inner_nw,
3277                      x + 2, y + half_height,
3278                      x + half_width, y + 2);
3279       gdk_draw_line (window, inner_ne,
3280                      x + half_width, y + 2,
3281                      x + width - 2, y + half_height);
3282       gdk_draw_line (window, middle_nw,
3283                      x + 1, y + half_height,
3284                      x + half_width, y + 1);
3285       gdk_draw_line (window, middle_ne,
3286                      x + half_width, y + 1,
3287                      x + width - 1, y + half_height);
3288       gdk_draw_line (window, outer_nw,
3289                      x, y + half_height,
3290                      x + half_width, y);
3291       gdk_draw_line (window, outer_ne,
3292                      x + half_width, y,
3293                      x + width, y + half_height);
3294     }
3295   
3296   if (area)
3297     {
3298       gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
3299       gdk_gc_set_clip_rectangle (style->bg_gc[state_type], NULL);
3300       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
3301       gdk_gc_set_clip_rectangle (style->black_gc, NULL);
3302     }
3303 }
3304
3305 static void
3306 gtk_default_draw_string (GtkStyle      *style,
3307                          GdkWindow     *window,
3308                          GtkStateType   state_type,
3309                          GdkRectangle  *area,
3310                          GtkWidget     *widget,
3311                          const gchar   *detail,
3312                          gint           x,
3313                          gint           y,
3314                          const gchar   *string)
3315 {
3316   GdkDisplay *display;
3317   
3318   g_return_if_fail (GTK_IS_STYLE (style));
3319   g_return_if_fail (window != NULL);
3320   
3321   display = gdk_drawable_get_display (window);
3322   
3323   if (area)
3324     {
3325       gdk_gc_set_clip_rectangle (style->white_gc, area);
3326       gdk_gc_set_clip_rectangle (style->fg_gc[state_type], area);
3327     }
3328
3329   if (state_type == GTK_STATE_INSENSITIVE)
3330     gdk_draw_string (window,
3331                      gtk_style_get_font_internal (style),
3332                      style->white_gc, x + 1, y + 1, string);
3333
3334   gdk_draw_string (window,
3335                    gtk_style_get_font_internal (style),
3336                    style->fg_gc[state_type], x, y, string);
3337
3338   if (area)
3339     {
3340       gdk_gc_set_clip_rectangle (style->white_gc, NULL);
3341       gdk_gc_set_clip_rectangle (style->fg_gc[state_type], NULL);
3342     }
3343 }
3344
3345 static void
3346 option_menu_get_props (GtkWidget      *widget,
3347                        GtkRequisition *indicator_size,
3348                        GtkBorder      *indicator_spacing)
3349 {
3350   GtkRequisition *tmp_size = NULL;
3351   GtkBorder *tmp_spacing = NULL;
3352   
3353   if (widget)
3354     gtk_widget_style_get (widget, 
3355                           "indicator_size", &tmp_size,
3356                           "indicator_spacing", &tmp_spacing,
3357                           NULL);
3358
3359   if (tmp_size)
3360     {
3361       *indicator_size = *tmp_size;
3362       g_free (tmp_size);
3363     }
3364   else
3365     *indicator_size = default_option_indicator_size;
3366
3367   if (tmp_spacing)
3368     {
3369       *indicator_spacing = *tmp_spacing;
3370       g_free (tmp_spacing);
3371     }
3372   else
3373     *indicator_spacing = default_option_indicator_spacing;
3374 }
3375
3376 static void 
3377 gtk_default_draw_box (GtkStyle      *style,
3378                       GdkWindow     *window,
3379                       GtkStateType   state_type,
3380                       GtkShadowType  shadow_type,
3381                       GdkRectangle  *area,
3382                       GtkWidget     *widget,
3383                       const gchar   *detail,
3384                       gint           x,
3385                       gint           y,
3386                       gint           width,
3387                       gint           height)
3388 {
3389   gboolean is_spinbutton_box = FALSE;
3390   
3391   g_return_if_fail (GTK_IS_STYLE (style));
3392   g_return_if_fail (window != NULL);
3393   
3394   sanitize_size (window, &width, &height);
3395
3396   if (widget && GTK_IS_SPIN_BUTTON (widget) && detail)
3397     {
3398       if (strcmp (detail, "spinbutton_up") == 0)
3399         {
3400           y += 2;
3401           width -= 3;
3402           height -= 2;
3403
3404           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3405             x += 2;
3406           else
3407             x += 1;
3408
3409           is_spinbutton_box = TRUE;
3410         }
3411       else if (strcmp (detail, "spinbutton_down") == 0)
3412         {
3413           width -= 3;
3414           height -= 2;
3415
3416           if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3417             x += 2;
3418           else
3419             x += 1;
3420
3421           is_spinbutton_box = TRUE;
3422         }
3423     }
3424   
3425   if (!style->bg_pixmap[state_type] || 
3426       GDK_IS_PIXMAP (window))
3427     {
3428       GdkGC *gc = style->bg_gc[state_type];
3429       
3430       if (state_type == GTK_STATE_SELECTED && strcmp (detail, "paned") == 0)
3431         {
3432           if (!GTK_WIDGET_HAS_FOCUS (widget))
3433             gc = style->base_gc[GTK_STATE_ACTIVE];
3434         }
3435
3436       if (area)
3437         gdk_gc_set_clip_rectangle (gc, area);
3438
3439       gdk_draw_rectangle (window, gc, TRUE,
3440                           x, y, width, height);
3441       if (area)
3442         gdk_gc_set_clip_rectangle (gc, NULL);
3443     }
3444   else
3445     gtk_style_apply_default_background (style, window,
3446                                         widget && !GTK_WIDGET_NO_WINDOW (widget),
3447                                         state_type, area, x, y, width, height);
3448
3449   if (is_spinbutton_box)
3450     {
3451       GdkGC *upper_gc;
3452       GdkGC *lower_gc;
3453       
3454       lower_gc = style->dark_gc[state_type];
3455       if (shadow_type == GTK_SHADOW_OUT)
3456         upper_gc = style->light_gc[state_type];
3457       else
3458         upper_gc = style->dark_gc[state_type];
3459
3460       if (area)
3461         {
3462           gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
3463           gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
3464         }
3465       
3466       gdk_draw_line (window, upper_gc, x, y, x + width - 1, y);
3467       gdk_draw_line (window, lower_gc, x, y + height - 1, x + width - 1, y + height - 1);
3468
3469       if (area)
3470         {
3471           gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
3472           gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
3473         }
3474       return;
3475     }
3476
3477   gtk_paint_shadow (style, window, state_type, shadow_type, area, widget, detail,
3478                     x, y, width, height);
3479
3480   if (detail && strcmp (detail, "optionmenu") == 0)
3481     {
3482       GtkRequisition indicator_size;
3483       GtkBorder indicator_spacing;
3484       gint vline_x;
3485
3486       option_menu_get_props (widget, &indicator_size, &indicator_spacing);
3487
3488       sanitize_size (window, &width, &height);
3489
3490       if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
3491         vline_x = x + indicator_size.width + indicator_spacing.left + indicator_spacing.right;
3492       else 
3493         vline_x = x + width - (indicator_size.width + indicator_spacing.left + indicator_spacing.right) - style->xthickness;
3494
3495       gtk_paint_vline (style, window, state_type, area, widget,
3496                        detail,
3497                        y + style->ythickness + 1,
3498                        y + height - style->ythickness - 3,
3499                        vline_x);
3500     }
3501 }
3502
3503 static GdkGC*
3504 get_darkened_gc (GdkWindow *window,
3505                  GdkColor  *color,
3506                  gint       darken_count)
3507 {
3508   GdkColor src = *color;
3509   GdkColor shaded = *color;
3510   GdkGC *gc;
3511   
3512   gc = gdk_gc_new (window);
3513
3514   while (darken_count)
3515     {
3516       gtk_style_shade (&src, &shaded, 0.93);
3517       src = shaded;
3518       --darken_count;
3519     }
3520    
3521   gdk_gc_set_rgb_fg_color (gc, &shaded);
3522
3523   return gc;
3524 }
3525
3526 static void 
3527 gtk_default_draw_flat_box (GtkStyle      *style,
3528                            GdkWindow     *window,
3529                            GtkStateType   state_type,
3530                            GtkShadowType  shadow_type,
3531                            GdkRectangle  *area,
3532                            GtkWidget     *widget,
3533                            const gchar   *detail,
3534                            gint           x,
3535                            gint           y,
3536                            gint           width,
3537                            gint           height)
3538 {
3539   GdkGC *gc1;
3540   GdkGC *freeme = NULL;
3541   
3542   g_return_if_fail (GTK_IS_STYLE (style));
3543   g_return_if_fail (window != NULL);
3544   
3545   sanitize_size (window, &width, &height);
3546   
3547   if (detail)
3548     {
3549       if (state_type == GTK_STATE_SELECTED)
3550         {
3551           if (!strcmp ("text", detail))
3552             gc1 = style->bg_gc[GTK_STATE_SELECTED];
3553           else if (!strncmp ("cell_even", detail, strlen ("cell_even")) ||
3554                    !strncmp ("cell_odd", detail, strlen ("cell_odd")))
3555             {
3556               /* This has to be really broken; alex made me do it. -jrb */
3557               if (GTK_WIDGET_HAS_FOCUS (widget))
3558                 gc1 = style->base_gc[state_type];
3559               else 
3560                 gc1 = style->base_gc[GTK_STATE_ACTIVE];
3561             }
3562           else
3563             {
3564               gc1 = style->bg_gc[state_type];
3565             }
3566         }
3567       else
3568         {
3569           if (!strcmp ("viewportbin", detail))
3570             gc1 = style->bg_gc[GTK_STATE_NORMAL];
3571           else if (!strcmp ("entry_bg", detail))
3572             gc1 = style->base_gc[state_type];
3573
3574           /* For trees: even rows are base color, odd rows are a shade of
3575            * the base color, the sort column is a shade of the original color
3576            * for that row.
3577            */
3578
3579           else if (!strcmp ("cell_even", detail) ||
3580                    !strcmp ("cell_odd", detail) ||
3581                    !strcmp ("cell_even_ruled", detail))
3582             {
3583               GdkColor *color = NULL;
3584
3585               gtk_widget_style_get (widget,
3586                                     "even_row_color", &color,
3587                                     NULL);
3588
3589               if (color)
3590                 {
3591                   freeme = get_darkened_gc (window, color, 0);
3592                   gc1 = freeme;
3593
3594                   gdk_color_free (color);
3595                 }
3596               else
3597                 gc1 = style->base_gc[state_type];
3598             }
3599           else if (!strcmp ("cell_odd_ruled", detail))
3600             {
3601               GdkColor *color;
3602
3603               gtk_widget_style_get (widget,
3604                                     "odd_row_color", &color,
3605                                     NULL);
3606
3607               if (color)
3608                 {
3609                   freeme = get_darkened_gc (window, color, 0);
3610                   gc1 = freeme;
3611
3612                   gdk_color_free (color);
3613                 }
3614               else
3615                 {
3616                   gtk_widget_style_get (widget,
3617                                         "even_row_color", &color,
3618                                         NULL);
3619
3620                   if (color)
3621                     {
3622                       freeme = get_darkened_gc (window, color, 1);
3623                       gdk_color_free (color);
3624                     }
3625                   else
3626                     freeme = get_darkened_gc (window, &style->base[state_type], 1);
3627                   gc1 = freeme;
3628                 }
3629             }
3630           else if (!strcmp ("cell_even_sorted", detail) ||
3631                    !strcmp ("cell_odd_sorted", detail) ||
3632                    !strcmp ("cell_even_ruled_sorted", detail))
3633             {
3634               GdkColor *color = NULL;
3635
3636               if (!strcmp ("cell_odd_sorted", detail))
3637                 gtk_widget_style_get (widget,
3638                                       "odd_row_color", &color,
3639                                       NULL);
3640               else
3641                 gtk_widget_style_get (widget,
3642                                       "even_row_color", &color,
3643                                       NULL);
3644
3645               if (color)
3646                 {
3647                   freeme = get_darkened_gc (window, color, 1);
3648                   gc1 = freeme;
3649
3650                   gdk_color_free (color);
3651                 }
3652               else
3653                 {
3654                   freeme = get_darkened_gc (window, &style->base[state_type], 1);
3655                   gc1 = freeme;
3656                 }
3657             }
3658           else if (!strcmp ("cell_odd_ruled_sorted", detail))
3659             {
3660               GdkColor *color = NULL;
3661
3662               gtk_widget_style_get (widget,
3663                                     "odd_row_color", &color,
3664                                     NULL);
3665
3666               if (color)
3667                 {
3668                   freeme = get_darkened_gc (window, color, 1);
3669                   gc1 = freeme;
3670
3671                   gdk_color_free (color);
3672                 }
3673               else
3674                 {
3675                   gtk_widget_style_get (widget,
3676                                         "even_row_color", &color,
3677                                         NULL);
3678
3679                   if (color)
3680                     {
3681                       freeme = get_darkened_gc (window, color, 2);
3682                       gdk_color_free (color);
3683                     }
3684                   else
3685                     freeme = get_darkened_gc (window, &style->base[state_type], 2);
3686                   gc1 = freeme;
3687                 }
3688             }
3689           else
3690             gc1 = style->bg_gc[state_type];
3691         }
3692     }
3693   else
3694     gc1 = style->bg_gc[state_type];
3695   
3696   if (!style->bg_pixmap[state_type] || gc1 != style->bg_gc[state_type] ||
3697       GDK_IS_PIXMAP (window))
3698     {
3699       if (area)
3700         gdk_gc_set_clip_rectangle (gc1, area);
3701
3702       gdk_draw_rectangle (window, gc1, TRUE,
3703                           x, y, width, height);
3704
3705       if (detail && !strcmp ("tooltip", detail))
3706         gdk_draw_rectangle (window, style->black_gc, FALSE,
3707                             x, y, width - 1, height - 1);
3708
3709       if (area)
3710         gdk_gc_set_clip_rectangle (gc1, NULL);
3711     }
3712   else
3713     gtk_style_apply_default_background (style, window,
3714                                         widget && !GTK_WIDGET_NO_WINDOW (widget),
3715                                         state_type, area, x, y, width, height);
3716
3717
3718   if (freeme)
3719     g_object_unref (freeme);
3720 }
3721
3722 static GdkGC *
3723 create_aa_gc (GdkWindow *window, GtkStyle *style, GtkStateType state_type)
3724 {
3725   GdkColor aa_color;
3726   GdkGC *gc = gdk_gc_new (window);
3727    
3728   aa_color.red = (style->fg[state_type].red + style->bg[state_type].red) / 2;
3729   aa_color.green = (style->fg[state_type].green + style->bg[state_type].green) / 2;
3730   aa_color.blue = (style->fg[state_type].blue + style->bg[state_type].blue) / 2;
3731   
3732   gdk_gc_set_rgb_fg_color (gc, &aa_color);
3733
3734   return gc;
3735 }
3736
3737 static void 
3738 gtk_default_draw_check (GtkStyle      *style,
3739                         GdkWindow     *window,
3740                         GtkStateType   state_type,
3741                         GtkShadowType  shadow_type,
3742                         GdkRectangle  *area,
3743                         GtkWidget     *widget,
3744                         const gchar   *detail,
3745                         gint           x,
3746                         gint           y,
3747                         gint           width,
3748                         gint           height)
3749 {
3750   if (detail && strcmp (detail, "cellcheck") == 0)
3751     {
3752       if (area)               
3753         gdk_gc_set_clip_rectangle (widget->style->base_gc[state_type], area);
3754       gdk_draw_rectangle (window,
3755                           widget->style->base_gc[state_type],
3756                           TRUE,
3757                           x, y,
3758                           width, height);
3759       if (area)
3760         {
3761           gdk_gc_set_clip_rectangle (widget->style->base_gc[state_type], NULL);
3762           gdk_gc_set_clip_rectangle (widget->style->text_gc[state_type], area);
3763         }
3764       gdk_draw_rectangle (window,
3765                           widget->style->text_gc[state_type],
3766                           FALSE,
3767                           x, y,
3768                           width, height);
3769       if (area)               
3770         gdk_gc_set_clip_rectangle (widget->style->text_gc[state_type], NULL);
3771
3772       x -= (1 + INDICATOR_PART_SIZE - width) / 2;
3773       y -= (((1 + INDICATOR_PART_SIZE - height) / 2) - 1);
3774       if (shadow_type == GTK_SHADOW_IN)
3775         {
3776           draw_part (window, style->text_gc[state_type], area, x, y, CHECK_TEXT);
3777           draw_part (window, style->text_aa_gc[state_type], area, x, y, CHECK_AA);
3778         }
3779       else if (shadow_type == GTK_SHADOW_ETCHED_IN) /* inconsistent */
3780         {
3781           draw_part (window, style->text_gc[state_type], area, x, y, CHECK_INCONSISTENT_TEXT);
3782         }
3783     }
3784   else
3785     {
3786       GdkGC *free_me = NULL;
3787       
3788       GdkGC *base_gc;
3789       GdkGC *text_gc;
3790       GdkGC *aa_gc;
3791
3792       x -= (1 + INDICATOR_PART_SIZE - width) / 2;
3793       y -= (1 + INDICATOR_PART_SIZE - height) / 2;
3794
3795       if (strcmp (detail, "check") == 0)        /* Menu item */
3796         {
3797           text_gc = style->fg_gc[state_type];
3798           base_gc = style->bg_gc[state_type];
3799           aa_gc = free_me = create_aa_gc (window, style, state_type);
3800         }
3801       else
3802         {
3803           if (state_type == GTK_STATE_ACTIVE)
3804             {
3805               text_gc = style->fg_gc[state_type];
3806               base_gc = style->bg_gc[state_type];
3807               aa_gc = free_me = create_aa_gc (window, style, state_type);
3808             }
3809           else
3810             {
3811               text_gc = style->text_gc[state_type];
3812               base_gc = style->base_gc[state_type];
3813               aa_gc = style->text_aa_gc[state_type];
3814             }
3815
3816           draw_part (window, base_gc, area, x, y, CHECK_BASE);
3817           draw_part (window, style->black_gc, area, x, y, CHECK_BLACK);
3818           draw_part (window, style->dark_gc[state_type], area, x, y, CHECK_DARK);
3819           draw_part (window, style->mid_gc[state_type], area, x, y, CHECK_MID);
3820           draw_part (window, style->light_gc[state_type], area, x, y, CHECK_LIGHT);
3821         }
3822
3823       if (shadow_type == GTK_SHADOW_IN)
3824         {
3825           draw_part (window, text_gc, area, x, y, CHECK_TEXT);
3826           draw_part (window, aa_gc, area, x, y, CHECK_AA);
3827         }
3828       else if (shadow_type == GTK_SHADOW_ETCHED_IN) /* inconsistent */
3829         {
3830           draw_part (window, text_gc, area, x, y, CHECK_INCONSISTENT_TEXT);
3831         }
3832
3833       if (free_me)
3834         g_object_unref (free_me);
3835     }
3836 }
3837
3838 static void 
3839 gtk_default_draw_option (GtkStyle      *style,
3840                          GdkWindow     *window,
3841                          GtkStateType   state_type,
3842                          GtkShadowType  shadow_type,
3843                          GdkRectangle  *area,
3844                          GtkWidget     *widget,
3845                          const gchar   *detail,
3846                          gint           x,
3847                          gint           y,
3848                          gint           width,
3849                          gint           height)
3850 {
3851   if (detail && strcmp (detail, "cellradio") == 0)
3852     {
3853       if (area)               
3854         gdk_gc_set_clip_rectangle (widget->style->fg_gc[state_type], area);
3855       gdk_draw_arc (window,
3856                     widget->style->fg_gc[state_type],
3857                     FALSE,
3858                     x, y,
3859                     width,
3860                     height,
3861                     0, 360*64);
3862
3863       if (shadow_type == GTK_SHADOW_IN)
3864         {
3865           gdk_draw_arc (window,
3866                         widget->style->fg_gc[state_type],
3867                         TRUE,
3868                         x + 2,
3869                         y + 2,
3870                         width - 4,
3871                         height - 4,
3872                         0, 360*64);
3873         }
3874       else if (shadow_type == GTK_SHADOW_ETCHED_IN) /* inconsistent */
3875         {
3876           draw_part (window, widget->style->fg_gc[state_type],
3877                      area, x, y, CHECK_INCONSISTENT_TEXT);
3878         }
3879       if (area)               
3880         gdk_gc_set_clip_rectangle (widget->style->fg_gc[state_type], NULL);
3881     }
3882   else
3883     {
3884       GdkGC *free_me = NULL;
3885       
3886       GdkGC *base_gc;
3887       GdkGC *text_gc;
3888       GdkGC *aa_gc;
3889
3890       x -= (1 + INDICATOR_PART_SIZE - width) / 2;
3891       y -= (1 + INDICATOR_PART_SIZE - height) / 2;
3892
3893       if (strcmp (detail, "option") == 0)       /* Menu item */
3894         {
3895           text_gc = style->fg_gc[state_type];
3896           base_gc = style->bg_gc[state_type];
3897           aa_gc = free_me = create_aa_gc (window, style, state_type);
3898         }
3899       else
3900         {
3901           if (state_type == GTK_STATE_ACTIVE)
3902             {
3903               text_gc = style->fg_gc[state_type];
3904               base_gc = style->bg_gc[state_type];
3905               aa_gc = free_me = create_aa_gc (window, style, state_type);
3906             }
3907           else
3908             {
3909               text_gc = style->text_gc[state_type];
3910               base_gc = style->base_gc[state_type];
3911               aa_gc = style->text_aa_gc[state_type];
3912             }
3913
3914           draw_part (window, base_gc, area, x, y, RADIO_BASE);
3915           draw_part (window, style->black_gc, area, x, y, RADIO_BLACK);
3916           draw_part (window, style->dark_gc[state_type], area, x, y, RADIO_DARK);
3917           draw_part (window, style->mid_gc[state_type], area, x, y, RADIO_MID);
3918           draw_part (window, style->light_gc[state_type], area, x, y, RADIO_LIGHT);
3919         }
3920
3921       if (shadow_type == GTK_SHADOW_IN)
3922         {
3923           draw_part (window, text_gc, area, x, y, RADIO_TEXT);
3924         }
3925       else if (shadow_type == GTK_SHADOW_ETCHED_IN) /* inconsistent */
3926         {
3927           if (strcmp (detail, "option") == 0)  /* Menu item */
3928             {
3929               draw_part (window, text_gc, area, x, y, CHECK_INCONSISTENT_TEXT);
3930             }
3931           else
3932             {
3933               draw_part (window, text_gc, area, x, y, RADIO_INCONSISTENT_TEXT);
3934               draw_part (window, aa_gc, area, x, y, RADIO_INCONSISTENT_AA);
3935             }
3936         }
3937
3938       if (free_me)
3939         g_object_unref (free_me);
3940     }
3941 }
3942
3943 static void
3944 gtk_default_draw_tab (GtkStyle      *style,
3945                       GdkWindow     *window,
3946                       GtkStateType   state_type,
3947                       GtkShadowType  shadow_type,
3948                       GdkRectangle  *area,
3949                       GtkWidget     *widget,
3950                       const gchar   *detail,
3951                       gint           x,
3952                       gint           y,
3953                       gint           width,
3954                       gint           height)
3955 {
3956 #define ARROW_SPACE 4
3957
3958   GtkRequisition indicator_size;
3959   GtkBorder indicator_spacing;
3960   gint arrow_height;
3961   
3962   option_menu_get_props (widget, &indicator_size, &indicator_spacing);
3963
3964   indicator_size.width += (indicator_size.width % 2) - 1;
3965   arrow_height = indicator_size.width / 2 + 1;
3966
3967   x += (width - indicator_size.width) / 2;
3968   y += (height - (2 * arrow_height + ARROW_SPACE)) / 2;
3969
3970   if (state_type == GTK_STATE_INSENSITIVE)
3971     {
3972       draw_arrow (window, style->white_gc, area,
3973                   GTK_ARROW_UP, x + 1, y + 1,
3974                   indicator_size.width, arrow_height);
3975       
3976       draw_arrow (window, style->white_gc, area,
3977                   GTK_ARROW_DOWN, x + 1, y + arrow_height + ARROW_SPACE + 1,
3978                   indicator_size.width, arrow_height);
3979     }
3980   
3981   draw_arrow (window, style->fg_gc[state_type], area,
3982               GTK_ARROW_UP, x, y,
3983               indicator_size.width, arrow_height);
3984   
3985   
3986   draw_arrow (window, style->fg_gc[state_type], area,
3987               GTK_ARROW_DOWN, x, y + arrow_height + ARROW_SPACE,
3988               indicator_size.width, arrow_height);
3989 }
3990
3991 static void 
3992 gtk_default_draw_shadow_gap (GtkStyle       *style,
3993                              GdkWindow      *window,
3994                              GtkStateType    state_type,
3995                              GtkShadowType   shadow_type,
3996                              GdkRectangle   *area,
3997                              GtkWidget      *widget,
3998                              const gchar    *detail,
3999                              gint            x,
4000                              gint            y,
4001                              gint            width,
4002                              gint            height,
4003                              GtkPositionType gap_side,
4004                              gint            gap_x,
4005                              gint            gap_width)
4006 {
4007   GdkGC *gc1 = NULL;
4008   GdkGC *gc2 = NULL;
4009   GdkGC *gc3 = NULL;
4010   GdkGC *gc4 = NULL;
4011   
4012   g_return_if_fail (GTK_IS_STYLE (style));
4013   g_return_if_fail (window != NULL);
4014   
4015   sanitize_size (window, &width, &height);
4016   
4017   switch (shadow_type)
4018     {
4019     case GTK_SHADOW_NONE:
4020       return;
4021     case GTK_SHADOW_IN:
4022       gc1 = style->dark_gc[state_type];
4023       gc2 = style->black_gc;
4024       gc3 = style->bg_gc[state_type];
4025       gc4 = style->light_gc[state_type];
4026       break;
4027     case GTK_SHADOW_ETCHED_IN:
4028       gc1 = style->dark_gc[state_type];
4029       gc2 = style->light_gc[state_type];
4030       gc3 = style->dark_gc[state_type];
4031       gc4 = style->light_gc[state_type];
4032       break;
4033     case GTK_SHADOW_OUT:
4034       gc1 = style->light_gc[state_type];
4035       gc2 = style->bg_gc[state_type];
4036       gc3 = style->dark_gc[state_type];
4037       gc4 = style->black_gc;
4038       break;
4039     case GTK_SHADOW_ETCHED_OUT:
4040       gc1 = style->light_gc[state_type];
4041       gc2 = style->dark_gc[state_type];
4042       gc3 = style->light_gc[state_type];
4043       gc4 = style->dark_gc[state_type];
4044       break;
4045     }
4046   if (area)
4047     {
4048       gdk_gc_set_clip_rectangle (gc1, area);
4049       gdk_gc_set_clip_rectangle (gc2, area);
4050       gdk_gc_set_clip_rectangle (gc3, area);
4051       gdk_gc_set_clip_rectangle (gc4, area);
4052     }
4053   
4054   switch (shadow_type)
4055     {
4056     case GTK_SHADOW_NONE:
4057     case GTK_SHADOW_IN:
4058     case GTK_SHADOW_OUT:
4059     case GTK_SHADOW_ETCHED_IN:
4060     case GTK_SHADOW_ETCHED_OUT:
4061       switch (gap_side)
4062         {
4063         case GTK_POS_TOP:
4064           gdk_draw_line (window, gc1,
4065                          x, y, x, y + height - 1);
4066           gdk_draw_line (window, gc2,
4067                          x + 1, y, x + 1, y + height - 2);
4068           
4069           gdk_draw_line (window, gc3,
4070                          x + 1, y + height - 2, x + width - 2, y + height - 2);
4071           gdk_draw_line (window, gc3,
4072                          x + width - 2, y, x + width - 2, y + height - 2);
4073           gdk_draw_line (window, gc4,
4074                          x, y + height - 1, x + width - 1, y + height - 1);
4075           gdk_draw_line (window, gc4,
4076                          x + width - 1, y, x + width - 1, y + height - 1);
4077           if (gap_x > 0)
4078             {
4079               gdk_draw_line (window, gc1,
4080                              x, y, x + gap_x - 1, y);
4081               gdk_draw_line (window, gc2,
4082                              x + 1, y + 1, x + gap_x - 1, y + 1);
4083               gdk_draw_line (window, gc2,
4084                              x + gap_x, y, x + gap_x, y);
4085             }
4086           if ((width - (gap_x + gap_width)) > 0)
4087             {
4088               gdk_draw_line (window, gc1,
4089                              x + gap_x + gap_width, y, x + width - 2, y);
4090               gdk_draw_line (window, gc2,
4091                              x + gap_x + gap_width, y + 1, x + width - 3, y + 1);
4092               gdk_draw_line (window, gc2,
4093                              x + gap_x + gap_width - 1, y, x + gap_x + gap_width - 1, y);
4094             }
4095           break;
4096         case GTK_POS_BOTTOM:
4097           gdk_draw_line (window, gc1,
4098                          x, y, x + width - 1, y);
4099           gdk_draw_line (window, gc1,
4100                          x, y, x, y + height - 1);
4101           gdk_draw_line (window, gc2,
4102                          x + 1, y + 1, x + width - 2, y + 1);
4103           gdk_draw_line (window, gc2,
4104                          x + 1, y + 1, x + 1, y + height - 1);
4105           
4106           gdk_draw_line (window, gc3,
4107                          x + width - 2, y + 1, x + width - 2, y + height - 1);
4108           gdk_draw_line (window, gc4,
4109                          x + width - 1, y, x + width - 1, y + height - 1);
4110           if (gap_x > 0)
4111             {
4112               gdk_draw_line (window, gc4,
4113                              x, y + height - 1, x + gap_x - 1, y + height - 1);
4114               gdk_draw_line (window, gc3,
4115                              x + 1, y + height - 2, x + gap_x - 1, y + height - 2);
4116               gdk_draw_line (window, gc3,
4117                              x + gap_x, y + height - 1, x + gap_x, y + height - 1);
4118             }
4119           if ((width - (gap_x + gap_width)) > 0)
4120             {
4121               gdk_draw_line (window, gc4,
4122                              x + gap_x + gap_width, y + height - 1, x + width - 2, y + height - 1);
4123               gdk_draw_line (window, gc3,
4124                              x + gap_x + gap_width, y + height - 2, x + width - 2, y + height - 2);
4125               gdk_draw_line (window, gc3,
4126                              x + gap_x + gap_width - 1, y + height - 1, x + gap_x + gap_width - 1, y + height - 1);
4127             }
4128           break;
4129         case GTK_POS_LEFT:
4130           gdk_draw_line (window, gc1,
4131                          x, y, x + width - 1, y);
4132           gdk_draw_line (window, gc2,
4133                          x, y + 1, x + width - 2, y + 1);
4134           
4135           gdk_draw_line (window, gc3,
4136                          x, y + height - 2, x + width - 2, y + height - 2);
4137           gdk_draw_line (window, gc3,
4138                          x + width - 2, y + 1, x + width - 2, y + height - 2);
4139           gdk_draw_line (window, gc4,
4140                          x, y + height - 1, x + width - 1, y + height - 1);
4141           gdk_draw_line (window, gc4,
4142                          x + width - 1, y, x + width - 1, y + height - 1);
4143           if (gap_x > 0)
4144             {
4145               gdk_draw_line (window, gc1,
4146                              x, y, x, y + gap_x - 1);
4147               gdk_draw_line (window, gc2,
4148                              x + 1, y + 1, x + 1, y + gap_x - 1);
4149               gdk_draw_line (window, gc2,
4150                              x, y + gap_x, x, y + gap_x);
4151             }
4152           if ((width - (gap_x + gap_width)) > 0)
4153             {
4154               gdk_draw_line (window, gc1,
4155                              x, y + gap_x + gap_width, x, y + height - 2);
4156               gdk_draw_line (window, gc2,
4157                              x + 1, y + gap_x + gap_width, x + 1, y + height - 2);
4158               gdk_draw_line (window, gc2,
4159                              x, y + gap_x + gap_width - 1, x, y + gap_x + gap_width - 1);
4160             }
4161           break;
4162         case GTK_POS_RIGHT:
4163           gdk_draw_line (window, gc1,
4164                          x, y, x + width - 1, y);
4165           gdk_draw_line (window, gc1,
4166                          x, y, x, y + height - 1);
4167           gdk_draw_line (window, gc2,
4168                          x + 1, y + 1, x + width - 1, y + 1);
4169           gdk_draw_line (window, gc2,
4170                          x + 1, y + 1, x + 1, y + height - 2);
4171           
4172           gdk_draw_line (window, gc3,
4173                          x + 1, y + height - 2, x + width - 1, y + height - 2);
4174           gdk_draw_line (window, gc4,
4175                          x, y + height - 1, x + width - 1, y + height - 1);
4176           if (gap_x > 0)
4177             {
4178               gdk_draw_line (window, gc4,
4179                              x + width - 1, y, x + width - 1, y + gap_x - 1);
4180               gdk_draw_line (window, gc3,
4181                              x + width - 2, y + 1, x + width - 2, y + gap_x - 1);
4182               gdk_draw_line (window, gc3,
4183                              x + width - 1, y + gap_x, x + width - 1, y + gap_x);
4184             }
4185           if ((width - (gap_x + gap_width)) > 0)
4186             {
4187               gdk_draw_line (window, gc4,
4188                              x + width - 1, y + gap_x + gap_width, x + width - 1, y + height - 2);
4189               gdk_draw_line (window, gc3,
4190                              x + width - 2, y + gap_x + gap_width, x + width - 2, y + height - 2);
4191               gdk_draw_line (window, gc3,
4192                              x + width - 1, y + gap_x + gap_width - 1, x + width - 1, y + gap_x + gap_width - 1);
4193             }
4194           break;
4195         }
4196     }
4197
4198   if (area)
4199     {
4200       gdk_gc_set_clip_rectangle (gc1, NULL);
4201       gdk_gc_set_clip_rectangle (gc2, NULL);
4202       gdk_gc_set_clip_rectangle (gc3, NULL);
4203       gdk_gc_set_clip_rectangle (gc4, NULL);
4204     }
4205 }
4206
4207 static void 
4208 gtk_default_draw_box_gap (GtkStyle       *style,
4209                           GdkWindow      *window,
4210                           GtkStateType    state_type,
4211                           GtkShadowType   shadow_type,
4212                           GdkRectangle   *area,
4213                           GtkWidget      *widget,
4214                           const gchar    *detail,
4215                           gint            x,
4216                           gint            y,
4217                           gint            width,
4218                           gint            height,
4219                           GtkPositionType gap_side,
4220                           gint            gap_x,
4221                           gint            gap_width)
4222 {
4223   GdkGC *gc1 = NULL;
4224   GdkGC *gc2 = NULL;
4225   GdkGC *gc3 = NULL;
4226   GdkGC *gc4 = NULL;
4227   
4228   g_return_if_fail (GTK_IS_STYLE (style));
4229   g_return_if_fail (window != NULL);
4230   
4231   gtk_style_apply_default_background (style, window,
4232                                       widget && !GTK_WIDGET_NO_WINDOW (widget),
4233                                       state_type, area, x, y, width, height);
4234   
4235   sanitize_size (window, &width, &height);
4236   
4237   switch (shadow_type)
4238     {
4239     case GTK_SHADOW_NONE:
4240       return;
4241     case GTK_SHADOW_IN:
4242       gc1 = style->dark_gc[state_type];
4243       gc2 = style->black_gc;
4244       gc3 = style->bg_gc[state_type];
4245       gc4 = style->light_gc[state_type];
4246       break;
4247     case GTK_SHADOW_ETCHED_IN:
4248       gc1 = style->dark_gc[state_type];
4249       gc2 = style->light_gc[state_type];
4250       gc3 = style->dark_gc[state_type];
4251       gc4 = style->light_gc[state_type];
4252       break;
4253     case GTK_SHADOW_OUT:
4254       gc1 = style->light_gc[state_type];
4255       gc2 = style->bg_gc[state_type];
4256       gc3 = style->dark_gc[state_type];
4257       gc4 = style->black_gc;
4258       break;
4259     case GTK_SHADOW_ETCHED_OUT:
4260       gc1 = style->light_gc[state_type];
4261       gc2 = style->dark_gc[state_type];
4262       gc3 = style->light_gc[state_type];
4263       gc4 = style->dark_gc[state_type];
4264       break;
4265     }
4266
4267   if (area)
4268     {
4269       gdk_gc_set_clip_rectangle (gc1, area);
4270       gdk_gc_set_clip_rectangle (gc2, area);
4271       gdk_gc_set_clip_rectangle (gc3, area);
4272       gdk_gc_set_clip_rectangle (gc4, area);
4273     }
4274   
4275   switch (shadow_type)
4276     {
4277     case GTK_SHADOW_NONE:
4278     case GTK_SHADOW_IN:
4279     case GTK_SHADOW_OUT:
4280     case GTK_SHADOW_ETCHED_IN:
4281     case GTK_SHADOW_ETCHED_OUT:
4282       switch (gap_side)
4283         {
4284         case GTK_POS_TOP:
4285           gdk_draw_line (window, gc1,
4286                          x, y, x, y + height - 1);
4287           gdk_draw_line (window, gc2,
4288                          x + 1, y, x + 1, y + height - 2);
4289           
4290           gdk_draw_line (window, gc3,
4291                          x + 1, y + height - 2, x + width - 2, y + height - 2);
4292           gdk_draw_line (window, gc3,
4293                          x + width - 2, y, x + width - 2, y + height - 2);
4294           gdk_draw_line (window, gc4,
4295                          x, y + height - 1, x + width - 1, y + height - 1);
4296           gdk_draw_line (window, gc4,
4297                          x + width - 1, y, x + width - 1, y + height - 1);
4298           if (gap_x > 0)
4299             {
4300               gdk_draw_line (window, gc1,
4301                              x, y, x + gap_x - 1, y);
4302               gdk_draw_line (window, gc2,
4303                              x + 1, y + 1, x + gap_x - 1, y + 1);
4304               gdk_draw_line (window, gc2,
4305                              x + gap_x, y, x + gap_x, y);
4306             }
4307           if ((width - (gap_x + gap_width)) > 0)
4308             {
4309               gdk_draw_line (window, gc1,
4310                              x + gap_x + gap_width, y, x + width - 2, y);
4311               gdk_draw_line (window, gc2,
4312                              x + gap_x + gap_width, y + 1, x + width - 2, y + 1);
4313               gdk_draw_line (window, gc2,
4314                              x + gap_x + gap_width - 1, y, x + gap_x + gap_width - 1, y);
4315             }
4316           break;
4317         case  GTK_POS_BOTTOM:
4318           gdk_draw_line (window, gc1,
4319                          x, y, x + width - 1, y);
4320           gdk_draw_line (window, gc1,
4321                          x, y, x, y + height - 1);
4322           gdk_draw_line (window, gc2,
4323                          x + 1, y + 1, x + width - 2, y + 1);
4324           gdk_draw_line (window, gc2,
4325                          x + 1, y + 1, x + 1, y + height - 1);
4326           
4327           gdk_draw_line (window, gc3,
4328                          x + width - 2, y + 1, x + width - 2, y + height - 1);
4329           gdk_draw_line (window, gc4,
4330                          x + width - 1, y, x + width - 1, y + height - 1);
4331           if (gap_x > 0)
4332             {
4333               gdk_draw_line (window, gc4,
4334                              x, y + height - 1, x + gap_x - 1, y + height - 1);
4335               gdk_draw_line (window, gc3,
4336                              x + 1, y + height - 2, x + gap_x - 1, y + height - 2);
4337               gdk_draw_line (window, gc3,
4338                              x + gap_x, y + height - 1, x + gap_x, y + height - 1);
4339             }
4340           if ((width - (gap_x + gap_width)) > 0)
4341             {
4342               gdk_draw_line (window, gc4,
4343                              x + gap_x + gap_width, y + height - 1, x + width - 2, y + height - 1);
4344               gdk_draw_line (window, gc3,
4345                              x + gap_x + gap_width, y + height - 2, x + width - 2, y + height - 2);
4346               gdk_draw_line (window, gc3,
4347                              x + gap_x + gap_width - 1, y + height - 1, x + gap_x + gap_width - 1, y + height - 1);
4348             }
4349           break;
4350         case GTK_POS_LEFT:
4351           gdk_draw_line (window, gc1,
4352                          x, y, x + width - 1, y);
4353           gdk_draw_line (window, gc2,
4354                          x, y + 1, x + width - 2, y + 1);
4355           
4356           gdk_draw_line (window, gc3,
4357                          x, y + height - 2, x + width - 2, y + height - 2);
4358           gdk_draw_line (window, gc3,
4359                          x + width - 2, y + 1, x + width - 2, y + height - 2);
4360           gdk_draw_line (window, gc4,
4361                          x, y + height - 1, x + width - 1, y + height - 1);
4362           gdk_draw_line (window, gc4,
4363                          x + width - 1, y, x + width - 1, y + height - 1);
4364           if (gap_x > 0)
4365             {
4366               gdk_draw_line (window, gc1,
4367                              x, y, x, y + gap_x - 1);
4368               gdk_draw_line (window, gc2,
4369                              x + 1, y + 1, x + 1, y + gap_x - 1);
4370               gdk_draw_line (window, gc2,
4371                              x, y + gap_x, x, y + gap_x);
4372             }
4373           if ((width - (gap_x + gap_width)) > 0)
4374             {
4375               gdk_draw_line (window, gc1,
4376                              x, y + gap_x + gap_width, x, y + height - 2);
4377               gdk_draw_line (window, gc2,
4378                              x + 1, y + gap_x + gap_width, x + 1, y + height - 2);
4379               gdk_draw_line (window, gc2,
4380                              x, y + gap_x + gap_width - 1, x, y + gap_x + gap_width - 1);
4381             }
4382           break;
4383         case GTK_POS_RIGHT:
4384           gdk_draw_line (window, gc1,
4385                          x, y, x + width - 1, y);
4386           gdk_draw_line (window, gc1,
4387                          x, y, x, y + height - 1);
4388           gdk_draw_line (window, gc2,
4389                          x + 1, y + 1, x + width - 1, y + 1);
4390           gdk_draw_line (window, gc2,
4391                          x + 1, y + 1, x + 1, y + height - 2);
4392           
4393           gdk_draw_line (window, gc3,
4394                          x + 1, y + height - 2, x + width - 1, y + height - 2);
4395           gdk_draw_line (window, gc4,
4396                          x, y + height - 1, x + width - 1, y + height - 1);
4397           if (gap_x > 0)
4398             {
4399               gdk_draw_line (window, gc4,
4400                              x + width - 1, y, x + width - 1, y + gap_x - 1);
4401               gdk_draw_line (window, gc3,
4402                              x + width - 2, y + 1, x + width - 2, y + gap_x - 1);
4403               gdk_draw_line (window, gc3,
4404                              x + width - 1, y + gap_x, x + width - 1, y + gap_x);
4405             }
4406           if ((width - (gap_x + gap_width)) > 0)
4407             {
4408               gdk_draw_line (window, gc4,
4409                              x + width - 1, y + gap_x + gap_width, x + width - 1, y + height - 2);
4410               gdk_draw_line (window, gc3,
4411                              x + width - 2, y + gap_x + gap_width, x + width - 2, y + height - 2);
4412               gdk_draw_line (window, gc3,
4413                              x + width - 1, y + gap_x + gap_width - 1, x + width - 1, y + gap_x + gap_width - 1);
4414             }
4415           break;
4416         }
4417     }
4418
4419   if (area)
4420     {
4421       gdk_gc_set_clip_rectangle (gc1, NULL);
4422       gdk_gc_set_clip_rectangle (gc2, NULL);
4423       gdk_gc_set_clip_rectangle (gc3, NULL);
4424       gdk_gc_set_clip_rectangle (gc4, NULL);
4425     }
4426 }
4427
4428 static void 
4429 gtk_default_draw_extension (GtkStyle       *style,
4430                             GdkWindow      *window,
4431                             GtkStateType    state_type,
4432                             GtkShadowType   shadow_type,
4433                             GdkRectangle   *area,
4434                             GtkWidget      *widget,
4435                             const gchar    *detail,
4436                             gint            x,
4437                             gint            y,
4438                             gint            width,
4439                             gint            height,
4440                             GtkPositionType gap_side)
4441 {
4442   GdkGC *gc1 = NULL;
4443   GdkGC *gc2 = NULL;
4444   GdkGC *gc3 = NULL;
4445   GdkGC *gc4 = NULL;
4446   
4447   g_return_if_fail (GTK_IS_STYLE (style));
4448   g_return_if_fail (window != NULL);
4449   
4450   gtk_style_apply_default_background (style, window,
4451                                       widget && !GTK_WIDGET_NO_WINDOW (widget),
4452                                       GTK_STATE_NORMAL, area, x, y, width, height);
4453   
4454   sanitize_size (window, &width, &height);
4455   
4456   switch (shadow_type)
4457     {
4458     case GTK_SHADOW_NONE:
4459       return;
4460     case GTK_SHADOW_IN:
4461       gc1 = style->dark_gc[state_type];
4462       gc2 = style->black_gc;
4463       gc3 = style->bg_gc[state_type];
4464       gc4 = style->light_gc[state_type];
4465       break;
4466     case GTK_SHADOW_ETCHED_IN:
4467       gc1 = style->dark_gc[state_type];
4468       gc2 = style->light_gc[state_type];
4469       gc3 = style->dark_gc[state_type];
4470       gc4 = style->light_gc[state_type];
4471       break;
4472     case GTK_SHADOW_OUT:
4473       gc1 = style->light_gc[state_type];
4474       gc2 = style->bg_gc[state_type];
4475       gc3 = style->dark_gc[state_type];
4476       gc4 = style->black_gc;
4477       break;
4478     case GTK_SHADOW_ETCHED_OUT:
4479       gc1 = style->light_gc[state_type];
4480       gc2 = style->dark_gc[state_type];
4481       gc3 = style->light_gc[state_type];
4482       gc4 = style->dark_gc[state_type];
4483       break;
4484     }
4485
4486   if (area)
4487     {
4488       gdk_gc_set_clip_rectangle (gc1, area);
4489       gdk_gc_set_clip_rectangle (gc2, area);
4490       gdk_gc_set_clip_rectangle (gc3, area);
4491       gdk_gc_set_clip_rectangle (gc4, area);
4492     }
4493
4494   switch (shadow_type)
4495     {
4496     case GTK_SHADOW_NONE:
4497     case GTK_SHADOW_IN:
4498     case GTK_SHADOW_OUT:
4499     case GTK_SHADOW_ETCHED_IN:
4500     case GTK_SHADOW_ETCHED_OUT:
4501       switch (gap_side)
4502         {
4503         case GTK_POS_TOP:
4504           gtk_style_apply_default_background (style, window,
4505                                               widget && !GTK_WIDGET_NO_WINDOW (widget),
4506                                               state_type, area,
4507                                               x + style->xthickness, 
4508                                               y, 
4509                                               width - (2 * style->xthickness), 
4510                                               height - (style->ythickness));
4511           gdk_draw_line (window, gc1,
4512                          x, y, x, y + height - 2);
4513           gdk_draw_line (window, gc2,
4514                          x + 1, y, x + 1, y + height - 2);
4515           
4516           gdk_draw_line (window, gc3,
4517                          x + 2, y + height - 2, x + width - 2, y + height - 2);
4518           gdk_draw_line (window, gc3,
4519                          x + width - 2, y, x + width - 2, y + height - 2);
4520           gdk_draw_line (window, gc4,
4521                          x + 1, y + height - 1, x + width - 2, y + height - 1);
4522           gdk_draw_line (window, gc4,
4523                          x + width - 1, y, x + width - 1, y + height - 2);
4524           break;
4525         case GTK_POS_BOTTOM:
4526           gtk_style_apply_default_background (style, window,
4527                                               widget && !GTK_WIDGET_NO_WINDOW (widget),
4528                                               state_type, area,
4529                                               x + style->xthickness, 
4530                                               y + style->ythickness, 
4531                                               width - (2 * style->xthickness), 
4532                                               height - (style->ythickness));
4533           gdk_draw_line (window, gc1,
4534                          x + 1, y, x + width - 2, y);
4535           gdk_draw_line (window, gc1,
4536                          x, y + 1, x, y + height - 1);
4537           gdk_draw_line (window, gc2,
4538                          x + 1, y + 1, x + width - 2, y + 1);
4539           gdk_draw_line (window, gc2,
4540                          x + 1, y + 1, x + 1, y + height - 1);
4541           
4542           gdk_draw_line (window, gc3,
4543                          x + width - 2, y + 2, x + width - 2, y + height - 1);
4544           gdk_draw_line (window, gc4,
4545                          x + width - 1, y + 1, x + width - 1, y + height - 1);
4546           break;
4547         case GTK_POS_LEFT:
4548           gtk_style_apply_default_background (style, window,
4549                                               widget && !GTK_WIDGET_NO_WINDOW (widget),
4550                                               state_type, area,
4551                                               x, 
4552                                               y + style->ythickness, 
4553                                               width - (style->xthickness), 
4554                                               height - (2 * style->ythickness));
4555           gdk_draw_line (window, gc1,
4556                          x, y, x + width - 2, y);
4557           gdk_draw_line (window, gc2,
4558                          x + 1, y + 1, x + width - 2, y + 1);
4559           
4560           gdk_draw_line (window, gc3,
4561                          x, y + height - 2, x + width - 2, y + height - 2);
4562           gdk_draw_line (window, gc3,
4563                          x + width - 2, y + 2, x + width - 2, y + height - 2);
4564           gdk_draw_line (window, gc4,
4565                          x, y + height - 1, x + width - 2, y + height - 1);
4566           gdk_draw_line (window, gc4,
4567                          x + width - 1, y + 1, x + width - 1, y + height - 2);
4568           break;
4569         case GTK_POS_RIGHT:
4570           gtk_style_apply_default_background (style, window,
4571                                               widget && !GTK_WIDGET_NO_WINDOW (widget),
4572                                               state_type, area,
4573                                               x + style->xthickness, 
4574                                               y + style->ythickness, 
4575                                               width - (style->xthickness), 
4576                                               height - (2 * style->ythickness));
4577           gdk_draw_line (window, gc1,
4578                          x + 1, y, x + width - 1, y);
4579           gdk_draw_line (window, gc1,
4580                          x, y + 1, x, y + height - 2);
4581           gdk_draw_line (window, gc2,
4582                          x + 1, y + 1, x + width - 1, y + 1);
4583           gdk_draw_line (window, gc2,
4584                          x + 1, y + 1, x + 1, y + height - 2);
4585           
4586           gdk_draw_line (window, gc3,
4587                          x + 2, y + height - 2, x + width - 1, y + height - 2);
4588           gdk_draw_line (window, gc4,
4589                          x + 1, y + height - 1, x + width - 1, y + height - 1);
4590           break;
4591         }
4592     }
4593
4594   if (area)
4595     {
4596       gdk_gc_set_clip_rectangle (gc1, NULL);
4597       gdk_gc_set_clip_rectangle (gc2, NULL);
4598       gdk_gc_set_clip_rectangle (gc3, NULL);
4599       gdk_gc_set_clip_rectangle (gc4, NULL);
4600     }
4601 }
4602
4603 static void 
4604 gtk_default_draw_focus (GtkStyle      *style,
4605                         GdkWindow     *window,
4606                         GtkStateType   state_type,
4607                         GdkRectangle  *area,
4608                         GtkWidget     *widget,
4609                         const gchar   *detail,
4610                         gint           x,
4611                         gint           y,
4612                         gint           width,
4613                         gint           height)
4614 {
4615   GdkPoint points[5];
4616   GdkGC    *gc;
4617   gboolean free_dash_list = FALSE;
4618   gint line_width = 1;
4619   gint8 *dash_list = "\1\1";
4620   gint dash_len;
4621
4622   if (widget)
4623     {
4624       gtk_widget_style_get (widget,
4625                             "focus-line-width", &line_width,
4626                             "focus-line-pattern", (gchar *)&dash_list,
4627                             NULL);
4628
4629       free_dash_list = TRUE;
4630   }
4631
4632   sanitize_size (window, &width, &height);
4633   
4634   if (detail && !strcmp (detail, "colorwheel_light"))
4635     gc = style->black_gc;
4636   else if (detail && !strcmp (detail, "colorwheel_dark"))
4637     gc = style->white_gc;
4638   else 
4639     gc = style->fg_gc[state_type];
4640
4641   gdk_gc_set_line_attributes (gc, line_width,
4642                               dash_list[0] ? GDK_LINE_ON_OFF_DASH : GDK_LINE_SOLID,
4643                               GDK_CAP_BUTT, GDK_JOIN_MITER);
4644   
4645   if (area)
4646     gdk_gc_set_clip_rectangle (gc, area);
4647
4648   if (detail && !strcmp (detail, "add-mode"))
4649     {
4650       if (free_dash_list)
4651         g_free (dash_list);
4652       
4653       dash_list = "\4\4";
4654       free_dash_list = FALSE;
4655     }
4656
4657   points[0].x = x + line_width / 2;
4658   points[0].y = y + line_width / 2;
4659   points[1].x = x + width - line_width + line_width / 2;
4660   points[1].y = y + line_width / 2;
4661   points[2].x = x + width - line_width + line_width / 2;
4662   points[2].y = y + height - line_width + line_width / 2;
4663   points[3].x = x + line_width / 2;
4664   points[3].y = y + height - line_width + line_width / 2;
4665   points[4] = points[0];
4666
4667   if (!dash_list[0])
4668     {
4669       gdk_draw_lines (window, gc, points, 5);
4670     }
4671   else
4672     {
4673       /* We go through all the pain below because the X rasterization
4674        * rules don't really work right for dashed lines if you
4675        * want continuity in segments that go between top/right
4676        * and left/bottom. For instance, a top left corner
4677        * with a 1-1 dash is drawn as:
4678        *
4679        *  X X X 
4680        *  X
4681        *
4682        *  X
4683        *
4684        * This is because pixels on the top and left boundaries
4685        * of polygons are drawn, but not on the bottom and right.
4686        * So, if you have a line going up that turns the corner
4687        * and goes right, there is a one pixel shift in the pattern.
4688        *
4689        * So, to fix this, we drawn the top and right in one call,
4690        * then the left and bottom in another call, fixing up
4691        * the dash offset for the second call ourselves to get
4692        * continuity at the upper left.
4693        *
4694        * It's not perfect since we really should have a join at
4695        * the upper left and lower right instead of two intersecting
4696        * lines but that's only really apparent for no-dashes,
4697        * which (for this reason) are done as one polygon and
4698        * don't to through this code path.
4699        */
4700       
4701       dash_len = strlen (dash_list);
4702       
4703       if (dash_list[0])
4704         gdk_gc_set_dashes (gc, 0, dash_list, dash_len);
4705       
4706       gdk_draw_lines (window, gc, points, 3);
4707       
4708       /* We draw this line one farther over than it is "supposed" to
4709        * because of another rasterization problem ... if two 1 pixel
4710        * unjoined lines meet at the lower right, there will be a missing
4711        * pixel.
4712        */
4713       points[2].x += 1;
4714       
4715       if (dash_list[0])
4716         {
4717           gint dash_pixels = 0;
4718           gint i;
4719           
4720           /* Adjust the dash offset for the bottom and left so we
4721            * match up at the upper left.
4722            */
4723           for (i = 0; i < dash_len; i++)
4724             dash_pixels += dash_list[i];
4725       
4726           if (dash_len % 2 == 1)
4727             dash_pixels *= 2;
4728           
4729           gdk_gc_set_dashes (gc, dash_pixels - (width + height - 2 * line_width) % dash_pixels, dash_list, dash_len);
4730         }
4731       
4732       gdk_draw_lines (window, gc, points + 2, 3);
4733     }
4734
4735   gdk_gc_set_line_attributes (gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
4736
4737   if (area)
4738     gdk_gc_set_clip_rectangle (gc, NULL);
4739
4740   if (free_dash_list)
4741     g_free (dash_list);
4742 }
4743
4744 static void 
4745 gtk_default_draw_slider (GtkStyle      *style,
4746                          GdkWindow     *window,
4747                          GtkStateType   state_type,
4748                          GtkShadowType  shadow_type,
4749                          GdkRectangle  *area,
4750                          GtkWidget     *widget,
4751                          const gchar   *detail,
4752                          gint           x,
4753                          gint           y,
4754                          gint           width,
4755                          gint           height,
4756                          GtkOrientation orientation)
4757 {
4758   g_return_if_fail (GTK_IS_STYLE (style));
4759   g_return_if_fail (window != NULL);
4760   
4761   sanitize_size (window, &width, &height);
4762   
4763   gtk_paint_box (style, window, state_type, shadow_type,
4764                  area, widget, detail, x, y, width, height);
4765
4766   if (detail &&
4767       (strcmp ("hscale", detail) == 0 ||
4768        strcmp ("vscale", detail) == 0))
4769     {
4770       if (orientation == GTK_ORIENTATION_HORIZONTAL)
4771         gtk_paint_vline (style, window, state_type, area, widget, detail, 
4772                          y + style->ythickness, 
4773                          y + height - style->ythickness - 1, x + width / 2);
4774       else
4775         gtk_paint_hline (style, window, state_type, area, widget, detail, 
4776                          x + style->xthickness, 
4777                          x + width - style->xthickness - 1, y + height / 2);
4778     }
4779 }
4780
4781 static void
4782 draw_dot (GdkWindow    *window,
4783           GdkGC        *light_gc,
4784           GdkGC        *dark_gc,
4785           gint          x,
4786           gint          y,
4787           gushort       size)
4788 {
4789   
4790   size = CLAMP (size, 2, 3);
4791
4792   if (size == 2)
4793     {
4794       gdk_draw_point (window, light_gc, x, y);
4795       gdk_draw_point (window, light_gc, x+1, y+1);
4796     }
4797   else if (size == 3)
4798     {
4799       gdk_draw_point (window, light_gc, x, y);
4800       gdk_draw_point (window, light_gc, x+1, y);
4801       gdk_draw_point (window, light_gc, x, y+1);
4802       gdk_draw_point (window, dark_gc, x+1, y+2);
4803       gdk_draw_point (window, dark_gc, x+2, y+1);
4804       gdk_draw_point (window, dark_gc, x+2, y+2);
4805     }
4806 }
4807
4808 static void 
4809 gtk_default_draw_handle (GtkStyle      *style,
4810                          GdkWindow     *window,
4811                          GtkStateType   state_type,
4812                          GtkShadowType  shadow_type,
4813                          GdkRectangle  *area,
4814                          GtkWidget     *widget,
4815                          const gchar   *detail,
4816                          gint           x,
4817                          gint           y,
4818                          gint           width,
4819                          gint           height,
4820                          GtkOrientation orientation)
4821 {
4822   gint xx, yy;
4823   gint xthick, ythick;
4824   GdkGC *light_gc, *dark_gc;
4825   GdkGC *free_me = NULL;
4826   GdkRectangle rect;
4827   GdkRectangle dest;
4828   gint intersect;
4829   
4830   g_return_if_fail (GTK_IS_STYLE (style));
4831   g_return_if_fail (window != NULL);
4832   
4833   sanitize_size (window, &width, &height);
4834   
4835   gtk_paint_box (style, window, state_type, shadow_type, area, widget, 
4836                  detail, x, y, width, height);
4837   
4838   
4839   if (!strcmp (detail, "paned"))
4840     {
4841       /* we want to ignore the shadow border in paned widgets */
4842       xthick = 0;
4843       ythick = 0;
4844
4845       if (state_type == GTK_STATE_SELECTED && !GTK_WIDGET_HAS_FOCUS (widget))
4846         {
4847           GdkColor unfocused_light;
4848       
4849           gtk_style_shade (&style->base[GTK_STATE_ACTIVE], &unfocused_light,
4850                            LIGHTNESS_MULT);
4851
4852           light_gc = free_me = gdk_gc_new (window);
4853           gdk_gc_set_rgb_fg_color (light_gc, &unfocused_light);
4854         }
4855       else
4856         light_gc = style->light_gc[state_type];
4857
4858       dark_gc = style->black_gc;
4859     }
4860   else
4861     {
4862       xthick = style->xthickness;
4863       ythick = style->ythickness;
4864
4865       light_gc = style->light_gc[state_type];
4866       dark_gc = style->dark_gc[state_type];
4867     }
4868   
4869   rect.x = x + xthick;
4870   rect.y = y + ythick;
4871   rect.width = width - (xthick * 2);
4872   rect.height = height - (ythick * 2);
4873
4874   if (area)
4875     intersect = gdk_rectangle_intersect (area, &rect, &dest);
4876   else
4877     {
4878       intersect = TRUE;
4879       dest = rect;
4880     }
4881
4882   if (!intersect)
4883     goto out;
4884
4885   gdk_gc_set_clip_rectangle (light_gc, &dest);
4886   gdk_gc_set_clip_rectangle (dark_gc, &dest);
4887
4888   if (!strcmp (detail, "paned"))
4889     {
4890       if (orientation == GTK_ORIENTATION_HORIZONTAL)
4891         for (xx = x + width/2 - 15; xx <= x + width/2 + 15; xx += 5)
4892           draw_dot (window, light_gc, dark_gc, xx, y + height/2 - 1, 3);
4893       else
4894         for (yy = y + height/2 - 15; yy <= y + height/2 + 15; yy += 5)
4895           draw_dot (window, light_gc, dark_gc, x + width/2 - 1, yy, 3);
4896     }
4897   else
4898     {
4899       for (yy = y + ythick; yy < (y + height - ythick); yy += 3)
4900         for (xx = x + xthick; xx < (x + width - xthick); xx += 6)
4901           {
4902             draw_dot (window, light_gc, dark_gc, xx, yy, 2);
4903             draw_dot (window, light_gc, dark_gc, xx + 3, yy + 1, 2);
4904           }
4905     }
4906
4907   gdk_gc_set_clip_rectangle (light_gc, NULL);
4908   gdk_gc_set_clip_rectangle (dark_gc, NULL);
4909
4910  out:
4911   if (free_me)
4912     g_object_unref (G_OBJECT (free_me));
4913 }
4914
4915 static void
4916 create_expander_affine (gdouble affine[6],
4917                         gint    degrees,
4918                         gint    expander_size,
4919                         gint    x,
4920                         gint    y)
4921 {
4922   gdouble s, c;
4923   gdouble width;
4924   gdouble height;
4925
4926   width = expander_size / 4.0;
4927   height = expander_size / 2.0;
4928   
4929   s = sin (degrees * G_PI / 180.0);
4930   c = cos (degrees * G_PI / 180.0);
4931   
4932   affine[0] = c;
4933   affine[1] = s;
4934   affine[2] = -s;
4935   affine[3] = c;
4936   affine[4] = -width * c - height * -s + x;
4937   affine[5] = -width * s - height * c + y;
4938 }
4939
4940 static void
4941 apply_affine_on_point (double affine[6], GdkPoint *point)
4942 {
4943   gdouble x, y;
4944
4945   x = point->x * affine[0] + point->y * affine[2] + affine[4];
4946   y = point->x * affine[1] + point->y * affine[3] + affine[5];
4947
4948   point->x = floor (x);
4949   point->y = floor (y);
4950 }
4951
4952 static void
4953 gtk_style_draw_polygon_with_gc (GdkWindow *window, GdkGC *gc, gint line_width,
4954                                 gboolean do_fill, GdkPoint *points, gint n_points)
4955 {
4956   gdk_gc_set_line_attributes (gc, line_width,
4957                               GDK_LINE_SOLID,
4958                               GDK_CAP_BUTT, GDK_JOIN_MITER);
4959
4960   gdk_draw_polygon (window, gc, do_fill, points, n_points);
4961   gdk_gc_set_line_attributes (gc, 0, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
4962 }
4963
4964 static void
4965 gtk_default_draw_expander (GtkStyle        *style,
4966                            GdkWindow       *window,
4967                            GtkStateType     state_type,
4968                            GdkRectangle    *area,
4969                            GtkWidget       *widget,
4970                            const gchar     *detail,
4971                            gint             x,
4972                            gint             y,
4973                            GtkExpanderStyle expander_style)
4974 {
4975   gint expander_size;
4976   GdkPoint points[3];
4977   gint i;
4978   gint line_width;
4979   gdouble affine[6];
4980   gint degrees = 0;
4981
4982   gtk_widget_style_get (widget,
4983                         "expander_size", &expander_size,
4984                         NULL);
4985   line_width = MAX (1, expander_size/7);
4986
4987   if (area)
4988     {
4989       gdk_gc_set_clip_rectangle (style->fg_gc[GTK_STATE_NORMAL], area);
4990       gdk_gc_set_clip_rectangle (style->base_gc[GTK_STATE_NORMAL], area);
4991     }
4992
4993   expander_size -= (line_width * 2 - 2);
4994   points[0].x = line_width / 2;
4995   points[0].y = line_width / 2;
4996   points[1].x = expander_size / 2 + line_width / 2;
4997   points[1].y = expander_size / 2 + line_width / 2;
4998   points[2].x = line_width / 2;
4999   points[2].y = expander_size + line_width / 2;
5000
5001   switch (expander_style)
5002     {
5003     case GTK_EXPANDER_COLLAPSED:
5004       degrees = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ? 180 : 0;
5005       break;
5006     case GTK_EXPANDER_SEMI_COLLAPSED:
5007       degrees = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ? 150 : 30;
5008       break;
5009     case GTK_EXPANDER_SEMI_EXPANDED:
5010       degrees = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ? 120 : 60;
5011       break;
5012     case GTK_EXPANDER_EXPANDED:
5013       degrees = 90;
5014       break;
5015     default:
5016       g_assert_not_reached ();
5017     }
5018
5019   create_expander_affine (affine, degrees, expander_size, x, y);
5020
5021   for (i = 0; i < 3; i++)
5022     apply_affine_on_point (affine, &points[i]);
5023
5024   if (state_type == GTK_STATE_PRELIGHT)
5025     {
5026       gtk_style_draw_polygon_with_gc (window, style->fg_gc[GTK_STATE_PRELIGHT],
5027                                       1, TRUE, points, 3);
5028     }
5029   else if (state_type == GTK_STATE_ACTIVE)
5030     {
5031       gtk_style_draw_polygon_with_gc (window, style->light_gc[GTK_STATE_ACTIVE],
5032                                       1, TRUE, points, 3);
5033       gtk_style_draw_polygon_with_gc (window, style->fg_gc[GTK_STATE_NORMAL],
5034                                       line_width, FALSE, points, 3);
5035     }
5036   else
5037     {
5038       gtk_style_draw_polygon_with_gc (window, style->base_gc[GTK_STATE_NORMAL],
5039                                       1, TRUE, points, 3);
5040       gtk_style_draw_polygon_with_gc (window, style->fg_gc[GTK_STATE_NORMAL],
5041                                       line_width, FALSE, points, 3);
5042     }
5043   if (area)
5044     {
5045       gdk_gc_set_clip_rectangle (style->fg_gc[GTK_STATE_NORMAL], NULL);
5046       gdk_gc_set_clip_rectangle (style->base_gc[GTK_STATE_NORMAL], NULL);
5047     }
5048 }
5049
5050 typedef struct _ByteRange ByteRange;
5051
5052 struct _ByteRange
5053 {
5054   guint start;
5055   guint end;
5056 };
5057
5058 static ByteRange*
5059 range_new (guint start,
5060            guint end)
5061 {
5062   ByteRange *br = g_new (ByteRange, 1);
5063
5064   br->start = start;
5065   br->end = end;
5066   
5067   return br;
5068 }
5069
5070 static PangoLayout*
5071 get_insensitive_layout (GdkDrawable *drawable,
5072                         PangoLayout *layout)
5073 {
5074   GSList *embossed_ranges = NULL;
5075   GSList *stippled_ranges = NULL;
5076   PangoLayoutIter *iter;
5077   GSList *tmp_list = NULL;
5078   PangoLayout *new_layout;
5079   PangoAttrList *attrs;
5080   GdkBitmap *stipple = NULL;
5081   
5082   iter = pango_layout_get_iter (layout);
5083   
5084   do
5085     {
5086       PangoLayoutRun *run;
5087       PangoAttribute *attr;
5088       gboolean need_stipple = FALSE;
5089       ByteRange *br;
5090       
5091       run = pango_layout_iter_get_run (iter);
5092
5093       if (run)
5094         {
5095           tmp_list = run->item->analysis.extra_attrs;
5096
5097           while (tmp_list != NULL)
5098             {
5099               attr = tmp_list->data;
5100               switch (attr->klass->type)
5101                 {
5102                 case PANGO_ATTR_FOREGROUND:
5103                 case PANGO_ATTR_BACKGROUND:
5104                   need_stipple = TRUE;
5105                   break;
5106               
5107                 default:
5108                   break;
5109                 }
5110
5111               if (need_stipple)
5112                 break;
5113           
5114               tmp_list = g_slist_next (tmp_list);
5115             }
5116
5117           br = range_new (run->item->offset, run->item->offset + run->item->length);
5118       
5119           if (need_stipple)
5120             stippled_ranges = g_slist_prepend (stippled_ranges, br);
5121           else
5122             embossed_ranges = g_slist_prepend (embossed_ranges, br);
5123         }
5124     }
5125   while (pango_layout_iter_next_run (iter));
5126
5127   pango_layout_iter_free (iter);
5128
5129   new_layout = pango_layout_copy (layout);
5130
5131   attrs = pango_layout_get_attributes (new_layout);
5132
5133   if (attrs == NULL)
5134     {
5135       /* Create attr list if there wasn't one */
5136       attrs = pango_attr_list_new ();
5137       pango_layout_set_attributes (new_layout, attrs);
5138       pango_attr_list_unref (attrs);
5139     }
5140   
5141   tmp_list = embossed_ranges;
5142   while (tmp_list != NULL)
5143     {
5144       PangoAttribute *attr;
5145       ByteRange *br = tmp_list->data;
5146
5147       attr = gdk_pango_attr_embossed_new (TRUE);
5148
5149       attr->start_index = br->start;
5150       attr->end_index = br->end;
5151       
5152       pango_attr_list_change (attrs, attr);
5153
5154       g_free (br);
5155       
5156       tmp_list = g_slist_next (tmp_list);
5157     }
5158
5159   g_slist_free (embossed_ranges);
5160   
5161   tmp_list = stippled_ranges;
5162   while (tmp_list != NULL)
5163     {
5164       PangoAttribute *attr;
5165       ByteRange *br = tmp_list->data;
5166
5167       if (stipple == NULL)
5168         {
5169 #define gray50_width 2
5170 #define gray50_height 2
5171           static const char gray50_bits[] = {
5172             0x02, 0x01
5173           };
5174
5175           stipple = gdk_bitmap_create_from_data (drawable,
5176                                                  gray50_bits, gray50_width,
5177                                                  gray50_height);
5178         }
5179       
5180       attr = gdk_pango_attr_stipple_new (stipple);
5181
5182       attr->start_index = br->start;
5183       attr->end_index = br->end;
5184       
5185       pango_attr_list_change (attrs, attr);
5186
5187       g_free (br);
5188       
5189       tmp_list = g_slist_next (tmp_list);
5190     }
5191
5192   g_slist_free (stippled_ranges);
5193   
5194   if (stipple)
5195     g_object_unref (stipple);
5196
5197   return new_layout;
5198 }
5199
5200 static void
5201 gtk_default_draw_layout (GtkStyle        *style,
5202                          GdkWindow       *window,
5203                          GtkStateType     state_type,
5204                          gboolean         use_text,
5205                          GdkRectangle    *area,
5206                          GtkWidget       *widget,
5207                          const gchar     *detail,
5208                          gint             x,
5209                          gint             y,
5210                          PangoLayout     *layout)
5211 {
5212   GdkGC *gc;
5213   
5214   g_return_if_fail (GTK_IS_STYLE (style));
5215   g_return_if_fail (window != NULL);
5216
5217   gc = use_text ? style->text_gc[state_type] : style->fg_gc[state_type];
5218   
5219   if (area)
5220     gdk_gc_set_clip_rectangle (gc, area);
5221
5222   if (state_type == GTK_STATE_INSENSITIVE)
5223     {
5224       PangoLayout *ins;
5225
5226       ins = get_insensitive_layout (window, layout);
5227       
5228       gdk_draw_layout (window, gc, x, y, ins);
5229
5230       g_object_unref (ins);
5231     }
5232   else
5233     {
5234       gdk_draw_layout (window, gc, x, y, layout);
5235     }
5236
5237   if (area)
5238     gdk_gc_set_clip_rectangle (gc, NULL);
5239 }
5240
5241 static void
5242 gtk_default_draw_resize_grip (GtkStyle       *style,
5243                               GdkWindow      *window,
5244                               GtkStateType    state_type,
5245                               GdkRectangle   *area,
5246                               GtkWidget      *widget,
5247                               const gchar    *detail,
5248                               GdkWindowEdge   edge,
5249                               gint            x,
5250                               gint            y,
5251                               gint            width,
5252                               gint            height)
5253 {
5254   g_return_if_fail (GTK_IS_STYLE (style));
5255   g_return_if_fail (window != NULL);
5256   
5257   if (area)
5258     {
5259       gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
5260       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
5261       gdk_gc_set_clip_rectangle (style->bg_gc[state_type], area);
5262     }
5263   
5264   switch (edge)
5265     {
5266     case GDK_WINDOW_EDGE_NORTH_WEST:
5267       /* make it square */
5268       if (width < height)
5269         {
5270           height = width;
5271         }
5272       else if (height < width)
5273         {
5274           width = height;
5275         }
5276       break;
5277     case GDK_WINDOW_EDGE_NORTH:
5278       if (width < height)
5279         {
5280           height = width;
5281         }
5282       break;
5283     case GDK_WINDOW_EDGE_NORTH_EAST:
5284       /* make it square, aligning to top right */
5285       if (width < height)
5286         {
5287           height = width;
5288         }
5289       else if (height < width)
5290         {
5291           x += (width - height);
5292           width = height;
5293         }
5294       break;
5295     case GDK_WINDOW_EDGE_WEST:
5296       if (height < width)
5297         {
5298            width = height;
5299         }
5300       break;
5301     case GDK_WINDOW_EDGE_EAST:
5302       /* aligning to right */
5303       if (height < width)
5304         {
5305           x += (width - height);
5306           width = height;
5307         }
5308       break;
5309     case GDK_WINDOW_EDGE_SOUTH_WEST:
5310       /* make it square, aligning to bottom left */
5311       if (width < height)
5312         {
5313           y += (height - width);
5314           height = width;
5315         }
5316       else if (height < width)
5317         {
5318           width = height;
5319         }
5320       break;
5321     case GDK_WINDOW_EDGE_SOUTH:
5322       /* align to bottom */
5323       if (width < height)
5324         {
5325           y += (height - width);
5326           height = width;
5327         }
5328       break;
5329     case GDK_WINDOW_EDGE_SOUTH_EAST:
5330       /* make it square, aligning to bottom right */
5331       if (width < height)
5332         {
5333           y += (height - width);
5334           height = width;
5335         }
5336       else if (height < width)
5337         {
5338           x += (width - height);
5339           width = height;
5340         }
5341       break;
5342     default:
5343       g_assert_not_reached ();
5344     }
5345   /* Clear background */
5346   gtk_style_apply_default_background (style, window, FALSE,
5347                                       state_type, area,
5348                                       x, y, width, height);   
5349
5350   switch (edge)
5351     {
5352     case GDK_WINDOW_EDGE_WEST:
5353     case GDK_WINDOW_EDGE_EAST:
5354       {
5355         gint xi;
5356
5357         xi = x;
5358
5359         while (xi < x + width)
5360           {
5361             gdk_draw_line (window,
5362                            style->light_gc[state_type],
5363                            xi, y,
5364                            xi, y + height);
5365
5366             xi++;
5367             gdk_draw_line (window,
5368                            style->dark_gc[state_type],
5369                            xi, y,
5370                            xi, y + height);
5371
5372             xi += 2;
5373           }
5374       }
5375       break;
5376     case GDK_WINDOW_EDGE_NORTH:
5377     case GDK_WINDOW_EDGE_SOUTH:
5378       {
5379         gint yi;
5380
5381         yi = y;
5382
5383         while (yi < y + height)
5384           {
5385             gdk_draw_line (window,
5386                            style->light_gc[state_type],
5387                            x, yi,
5388                            x + width, yi);
5389
5390             yi++;
5391             gdk_draw_line (window,
5392                            style->dark_gc[state_type],
5393                            x, yi,
5394                            x + width, yi);
5395
5396             yi+= 2;
5397           }
5398       }
5399       break;
5400     case GDK_WINDOW_EDGE_NORTH_WEST:
5401       {
5402         gint xi, yi;
5403
5404         xi = x + width;
5405         yi = y + height;
5406
5407         while (xi > x + 3)
5408           {
5409             gdk_draw_line (window,
5410                            style->dark_gc[state_type],
5411                            xi, y,
5412                            x, yi);
5413
5414             --xi;
5415             --yi;
5416
5417             gdk_draw_line (window,
5418                            style->dark_gc[state_type],
5419                            xi, y,
5420                            x, yi);
5421
5422             --xi;
5423             --yi;
5424
5425             gdk_draw_line (window,
5426                            style->light_gc[state_type],
5427                            xi, y,
5428                            x, yi);
5429
5430             xi -= 3;
5431             yi -= 3;
5432             
5433           }
5434       }
5435       break;
5436     case GDK_WINDOW_EDGE_NORTH_EAST:
5437       {
5438         gint xi, yi;
5439
5440         xi = x;
5441         yi = y + height;
5442
5443         while (xi < (x + width - 3))
5444           {
5445             gdk_draw_line (window,
5446                            style->light_gc[state_type],
5447                            xi, y,
5448                            x + width, yi);                           
5449
5450             ++xi;
5451             --yi;
5452             
5453             gdk_draw_line (window,
5454                            style->dark_gc[state_type],
5455                            xi, y,
5456                            x + width, yi);                           
5457
5458             ++xi;
5459             --yi;
5460             
5461             gdk_draw_line (window,
5462                            style->dark_gc[state_type],
5463                            xi, y,
5464                            x + width, yi);
5465
5466             xi += 3;
5467             yi -= 3;
5468           }
5469       }
5470       break;
5471     case GDK_WINDOW_EDGE_SOUTH_WEST:
5472       {
5473         gint xi, yi;
5474
5475         xi = x + width;
5476         yi = y;
5477
5478         while (xi > x + 3)
5479           {
5480             gdk_draw_line (window,
5481                            style->dark_gc[state_type],
5482                            x, yi,
5483                            xi, y + height);
5484
5485             --xi;
5486             ++yi;
5487
5488             gdk_draw_line (window,
5489                            style->dark_gc[state_type],
5490                            x, yi,
5491                            xi, y + height);
5492
5493             --xi;
5494             ++yi;
5495
5496             gdk_draw_line (window,
5497                            style->light_gc[state_type],
5498                            x, yi,
5499                            xi, y + height);
5500
5501             xi -= 3;
5502             yi += 3;
5503             
5504           }
5505       }
5506       break;
5507     case GDK_WINDOW_EDGE_SOUTH_EAST:
5508       {
5509         gint xi, yi;
5510
5511         xi = x;
5512         yi = y;
5513
5514         while (xi < (x + width - 3))
5515           {
5516             gdk_draw_line (window,
5517                            style->light_gc[state_type],
5518                            xi, y + height,
5519                            x + width, yi);                           
5520
5521             ++xi;
5522             ++yi;
5523             
5524             gdk_draw_line (window,
5525                            style->dark_gc[state_type],
5526                            xi, y + height,
5527                            x + width, yi);                           
5528
5529             ++xi;
5530             ++yi;
5531             
5532             gdk_draw_line (window,
5533                            style->dark_gc[state_type],
5534                            xi, y + height,
5535                            x + width, yi);
5536
5537             xi += 3;
5538             yi += 3;
5539           }
5540       }
5541       break;
5542     default:
5543       g_assert_not_reached ();
5544       break;
5545     }
5546   
5547   if (area)
5548     {
5549       gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
5550       gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
5551       gdk_gc_set_clip_rectangle (style->bg_gc[state_type], NULL);
5552     }
5553 }
5554
5555 static void
5556 gtk_style_shade (GdkColor *a,
5557                  GdkColor *b,
5558                  gdouble   k)
5559 {
5560   gdouble red;
5561   gdouble green;
5562   gdouble blue;
5563   
5564   red = (gdouble) a->red / 65535.0;
5565   green = (gdouble) a->green / 65535.0;
5566   blue = (gdouble) a->blue / 65535.0;
5567   
5568   rgb_to_hls (&red, &green, &blue);
5569   
5570   green *= k;
5571   if (green > 1.0)
5572     green = 1.0;
5573   else if (green < 0.0)
5574     green = 0.0;
5575   
5576   blue *= k;
5577   if (blue > 1.0)
5578     blue = 1.0;
5579   else if (blue < 0.0)
5580     blue = 0.0;
5581   
5582   hls_to_rgb (&red, &green, &blue);
5583   
5584   b->red = red * 65535.0;
5585   b->green = green * 65535.0;
5586   b->blue = blue * 65535.0;
5587 }
5588
5589 static void
5590 rgb_to_hls (gdouble *r,
5591             gdouble *g,
5592             gdouble *b)
5593 {
5594   gdouble min;
5595   gdouble max;
5596   gdouble red;
5597   gdouble green;
5598   gdouble blue;
5599   gdouble h, l, s;
5600   gdouble delta;
5601   
5602   red = *r;
5603   green = *g;
5604   blue = *b;
5605   
5606   if (red > green)
5607     {
5608       if (red > blue)
5609         max = red;
5610       else
5611         max = blue;
5612       
5613       if (green < blue)
5614         min = green;
5615       else
5616         min = blue;
5617     }
5618   else
5619     {
5620       if (green > blue)
5621         max = green;
5622       else
5623         max = blue;
5624       
5625       if (red < blue)
5626         min = red;
5627       else
5628         min = blue;
5629     }
5630   
5631   l = (max + min) / 2;
5632   s = 0;
5633   h = 0;
5634   
5635   if (max != min)
5636     {
5637       if (l <= 0.5)
5638         s = (max - min) / (max + min);
5639       else
5640         s = (max - min) / (2 - max - min);
5641       
5642       delta = max -min;
5643       if (red == max)
5644         h = (green - blue) / delta;
5645       else if (green == max)
5646         h = 2 + (blue - red) / delta;
5647       else if (blue == max)
5648         h = 4 + (red - green) / delta;
5649       
5650       h *= 60;
5651       if (h < 0.0)
5652         h += 360;
5653     }
5654   
5655   *r = h;
5656   *g = l;
5657   *b = s;
5658 }
5659
5660 static void
5661 hls_to_rgb (gdouble *h,
5662             gdouble *l,
5663             gdouble *s)
5664 {
5665   gdouble hue;
5666   gdouble lightness;
5667   gdouble saturation;
5668   gdouble m1, m2;
5669   gdouble r, g, b;
5670   
5671   lightness = *l;
5672   saturation = *s;
5673   
5674   if (lightness <= 0.5)
5675     m2 = lightness * (1 + saturation);
5676   else
5677     m2 = lightness + saturation - lightness * saturation;
5678   m1 = 2 * lightness - m2;
5679   
5680   if (saturation == 0)
5681     {
5682       *h = lightness;
5683       *l = lightness;
5684       *s = lightness;
5685     }
5686   else
5687     {
5688       hue = *h + 120;
5689       while (hue > 360)
5690         hue -= 360;
5691       while (hue < 0)
5692         hue += 360;
5693       
5694       if (hue < 60)
5695         r = m1 + (m2 - m1) * hue / 60;
5696       else if (hue < 180)
5697         r = m2;
5698       else if (hue < 240)
5699         r = m1 + (m2 - m1) * (240 - hue) / 60;
5700       else
5701         r = m1;
5702       
5703       hue = *h;
5704       while (hue > 360)
5705         hue -= 360;
5706       while (hue < 0)
5707         hue += 360;
5708       
5709       if (hue < 60)
5710         g = m1 + (m2 - m1) * hue / 60;
5711       else if (hue < 180)
5712         g = m2;
5713       else if (hue < 240)
5714         g = m1 + (m2 - m1) * (240 - hue) / 60;
5715       else
5716         g = m1;
5717       
5718       hue = *h - 120;
5719       while (hue > 360)
5720         hue -= 360;
5721       while (hue < 0)
5722         hue += 360;
5723       
5724       if (hue < 60)
5725         b = m1 + (m2 - m1) * hue / 60;
5726       else if (hue < 180)
5727         b = m2;
5728       else if (hue < 240)
5729         b = m1 + (m2 - m1) * (240 - hue) / 60;
5730       else
5731         b = m1;
5732       
5733       *h = r;
5734       *l = g;
5735       *s = b;
5736     }
5737 }
5738
5739
5740 /**
5741  * gtk_paint_hline:
5742  * @style: a #GtkStyle
5743  * @window: a #GdkWindow
5744  * @state_type: a state
5745  * @area: rectangle to which the output is clipped
5746  * @widget: the widget
5747  * @detail: a style detail
5748  * @x1: the starting x coordinate
5749  * @x2: the ending x coordinate
5750  * @y: the y coordinate
5751  * 
5752  * Draws a horizontal line from (@x1, @y) to (@x2, @y) in @window
5753  * using the given style and state.
5754  **/ 
5755 void 
5756 gtk_paint_hline (GtkStyle      *style,
5757                  GdkWindow     *window,
5758                  GtkStateType   state_type,
5759                  GdkRectangle  *area,
5760                  GtkWidget     *widget,
5761                  const gchar   *detail,
5762                  gint          x1,
5763                  gint          x2,
5764                  gint          y)
5765 {
5766   g_return_if_fail (GTK_IS_STYLE (style));
5767   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_hline != NULL);
5768   
5769   GTK_STYLE_GET_CLASS (style)->draw_hline (style, window, state_type, area, widget, detail, x1, x2, y);
5770 }
5771
5772 /**
5773  * gtk_paint_vline:
5774  * @style: a #GtkStyle
5775  * @window: a #GdkWindow
5776  * @state_type: a state
5777  * @area: rectangle to which the output is clipped
5778  * @widget: the widget
5779  * @detail: a style detail
5780  * @y1_: the starting y coordinate
5781  * @y2_: the ending y coordinate
5782  * @x: the x coordinate
5783  * 
5784  * Draws a vertical line from (@x, @y1_) to (@x, @y2_) in @window
5785  * using the given style and state.
5786  */
5787 void
5788 gtk_paint_vline (GtkStyle      *style,
5789                  GdkWindow     *window,
5790                  GtkStateType   state_type,
5791                  GdkRectangle  *area,
5792                  GtkWidget     *widget,
5793                  const gchar   *detail,
5794                  gint          y1_,
5795                  gint          y2_,
5796                  gint          x)
5797 {
5798   g_return_if_fail (GTK_IS_STYLE (style));
5799   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_vline != NULL);
5800   
5801   GTK_STYLE_GET_CLASS (style)->draw_vline (style, window, state_type, area, widget, detail, y1_, y2_, x);
5802 }
5803
5804 /**
5805  * gtk_paint_shadow:
5806  * @style: a #GtkStyle
5807  * @window: a #GdkWindow
5808  * @state_type: a state
5809  * @shadow_type: type of shadow to draw
5810  * @area: clip rectangle
5811  * @widget: the widget
5812  * @detail: a style detail
5813  * @x: x origin of the rectangle
5814  * @y: y origin of the rectangle
5815  * @width: width of the rectangle 
5816  * @height: width of the rectangle 
5817  *
5818  * Draws a shadow around the given rectangle in @window 
5819  * using the given style and state and shadow type.
5820  */
5821 void
5822 gtk_paint_shadow (GtkStyle     *style,
5823                   GdkWindow    *window,
5824                   GtkStateType  state_type,
5825                   GtkShadowType shadow_type,
5826                   GdkRectangle *area,
5827                   GtkWidget    *widget,
5828                   const gchar  *detail,
5829                   gint          x,
5830                   gint          y,
5831                   gint          width,
5832                   gint          height)
5833 {
5834   g_return_if_fail (GTK_IS_STYLE (style));
5835   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow != NULL);
5836   
5837   GTK_STYLE_GET_CLASS (style)->draw_shadow (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
5838 }
5839
5840 /**
5841  * gtk_paint_polygon:
5842  * @style: a #GtkStyle
5843  * @window: a #GdkWindow
5844  * @state_type: a state
5845  * @shadow_type: type of shadow to draw
5846  * @area: clip rectangle
5847  * @widget: the widget
5848  * @detail: a style detail
5849  * @points: an array of #GdkPoint<!-- -->s
5850  * @npoints: length of @points
5851  * @fill: %TRUE if the polygon should be filled
5852  * 
5853  * Draws a polygon on @window with the given parameters.
5854  */ 
5855 void
5856 gtk_paint_polygon (GtkStyle      *style,
5857                    GdkWindow     *window,
5858                    GtkStateType   state_type,
5859                    GtkShadowType  shadow_type,
5860                    GdkRectangle  *area,
5861                    GtkWidget     *widget,
5862                    const gchar   *detail,
5863                    GdkPoint      *points,
5864                    gint           npoints,
5865                    gboolean       fill)
5866 {
5867   g_return_if_fail (GTK_IS_STYLE (style));
5868   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_polygon != NULL);
5869   
5870   GTK_STYLE_GET_CLASS (style)->draw_polygon (style, window, state_type, shadow_type, area, widget, detail, points, npoints, fill);
5871 }
5872
5873 /**
5874  * gtk_paint_arrow:
5875  * @style: a #GtkStyle
5876  * @window: a #GdkWindow
5877  * @state_type: a state
5878  * @shadow_type: the type of shadow to draw
5879  * @area: clip rectangle
5880  * @widget: the widget
5881  * @detail: a style detail
5882  * @arrow_type: the type of arrow to draw
5883  * @fill: %TRUE if the arrow tip should be filled
5884  * @x: x origin of the rectangle to draw the arrow in
5885  * @y: y origin of the rectangle to draw the arrow in
5886  * @width: width of the rectangle to draw the arrow in
5887  * @height: height of the rectangle to draw the arrow in
5888  * 
5889  * Draws an arrow in the given rectangle on @window using the given 
5890  * parameters. @arrow_type determines the direction of the arrow.
5891  */
5892 void
5893 gtk_paint_arrow (GtkStyle      *style,
5894                  GdkWindow     *window,
5895                  GtkStateType   state_type,
5896                  GtkShadowType  shadow_type,
5897                  GdkRectangle  *area,
5898                  GtkWidget     *widget,
5899                  const gchar   *detail,
5900                  GtkArrowType   arrow_type,
5901                  gboolean       fill,
5902                  gint           x,
5903                  gint           y,
5904                  gint           width,
5905                  gint           height)
5906 {
5907   g_return_if_fail (GTK_IS_STYLE (style));
5908   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_arrow != NULL);
5909   
5910   GTK_STYLE_GET_CLASS (style)->draw_arrow (style, window, state_type, shadow_type, area, widget, detail, arrow_type, fill, x, y, width, height);
5911 }
5912
5913 /**
5914  * gtk_paint_diamond:
5915  * @style: a #GtkStyle
5916  * @window: a #GdkWindow
5917  * @state_type: a state
5918  * @shadow_type: the type of shadow to draw
5919  * @area: clip rectangle
5920  * @widget: the widget
5921  * @detail: a style detail
5922  * @x: x origin of the rectangle to draw the diamond in
5923  * @y: y origin of the rectangle to draw the diamond in
5924  * @width: width of the rectangle to draw the diamond in
5925  * @height: height of the rectangle to draw the diamond in
5926  *
5927  * Draws a diamond in the given rectangle on @window using the given
5928  * parameters.
5929  */
5930 void
5931 gtk_paint_diamond (GtkStyle      *style,
5932                    GdkWindow     *window,
5933                    GtkStateType   state_type,
5934                    GtkShadowType  shadow_type,
5935                    GdkRectangle  *area,
5936                    GtkWidget     *widget,
5937                    const gchar   *detail,
5938                    gint        x,
5939                    gint        y,
5940                    gint        width,
5941                    gint        height)
5942 {
5943   g_return_if_fail (GTK_IS_STYLE (style));
5944   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_diamond != NULL);
5945   
5946   GTK_STYLE_GET_CLASS (style)->draw_diamond (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
5947 }
5948
5949 /**
5950  * gtk_paint_string:
5951  * @style: a #GtkStyle
5952  * @window: a #GdkWindow
5953  * @state_type: a state
5954  * @area: clip rectangle
5955  * @widget: the widget
5956  * @detail: a style detail
5957  * @x: x origin
5958  * @y: y origin
5959  * @string: the string to draw
5960  * 
5961  * Draws a text string on @window with the given parameters.
5962  *
5963  * Deprecated: Use gtk_paint_layout() instead.
5964  */
5965 void
5966 gtk_paint_string (GtkStyle      *style,
5967                   GdkWindow     *window,
5968                   GtkStateType   state_type,
5969                   GdkRectangle  *area,
5970                   GtkWidget     *widget,
5971                   const gchar   *detail,
5972                   gint           x,
5973                   gint           y,
5974                   const gchar   *string)
5975 {
5976   g_return_if_fail (GTK_IS_STYLE (style));
5977   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_string != NULL);
5978   
5979   GTK_STYLE_GET_CLASS (style)->draw_string (style, window, state_type, area, widget, detail, x, y, string);
5980 }
5981
5982 /**
5983  * gtk_paint_box:
5984  * @style: a #GtkStyle
5985  * @window: a #GdkWindow
5986  * @state_type: a state
5987  * @shadow_type: the type of shadow to draw
5988  * @area: clip rectangle
5989  * @widget: the widget
5990  * @detail: a style detail
5991  * @x: x origin of the box
5992  * @y: y origin of the box
5993  * @width: the width of the box
5994  * @height: the height of the box
5995  * 
5996  * Draws a box on @window with the given parameters.
5997  */
5998 void
5999 gtk_paint_box (GtkStyle      *style,
6000                GdkWindow     *window,
6001                GtkStateType   state_type,
6002                GtkShadowType  shadow_type,
6003                GdkRectangle  *area,
6004                GtkWidget     *widget,
6005                const gchar   *detail,
6006                gint           x,
6007                gint           y,
6008                gint           width,
6009                gint           height)
6010 {
6011   g_return_if_fail (GTK_IS_STYLE (style));
6012   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box != NULL);
6013   
6014   GTK_STYLE_GET_CLASS (style)->draw_box (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
6015 }
6016
6017 /**
6018  * gtk_paint_flat_box:
6019  * @style: a #GtkStyle
6020  * @window: a #GdkWindow
6021  * @state_type: a state
6022  * @shadow_type: the type of shadow to draw
6023  * @area: clip rectangle
6024  * @widget: the widget
6025  * @detail: a style detail
6026  * @x: x origin of the box
6027  * @y: y origin of the box
6028  * @width: the width of the box
6029  * @height: the height of the box
6030  * 
6031  * Draws a flat box on @window with the given parameters.
6032  */
6033 void
6034 gtk_paint_flat_box (GtkStyle      *style,
6035                     GdkWindow     *window,
6036                     GtkStateType   state_type,
6037                     GtkShadowType  shadow_type,
6038                     GdkRectangle  *area,
6039                     GtkWidget     *widget,
6040                     const gchar   *detail,
6041                     gint           x,
6042                     gint           y,
6043                     gint           width,
6044                     gint           height)
6045 {
6046   g_return_if_fail (GTK_IS_STYLE (style));
6047   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_flat_box != NULL);
6048   
6049   GTK_STYLE_GET_CLASS (style)->draw_flat_box (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
6050 }
6051
6052 /**
6053  * gtk_paint_check:
6054  * @style: a #GtkStyle
6055  * @window: a #GdkWindow
6056  * @state_type: a state
6057  * @shadow_type: the type of shadow to draw
6058  * @area: clip rectangle
6059  * @widget: the widget
6060  * @detail: a style detail
6061  * @x: x origin of the rectangle to draw the check in
6062  * @y: y origin of the rectangle to draw the check in
6063  * @width: the width of the rectangle to draw the check in
6064  * @height: the height of the rectangle to draw the check in
6065  * 
6066  * Draws a check button indicator in the given rectangle on @window with 
6067  * the given parameters.
6068  */
6069 void
6070 gtk_paint_check (GtkStyle      *style,
6071                  GdkWindow     *window,
6072                  GtkStateType   state_type,
6073                  GtkShadowType  shadow_type,
6074                  GdkRectangle  *area,
6075                  GtkWidget     *widget,
6076                  const gchar   *detail,
6077                  gint           x,
6078                  gint           y,
6079                  gint           width,
6080                  gint           height)
6081 {
6082   g_return_if_fail (GTK_IS_STYLE (style));
6083   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_check != NULL);
6084   
6085   GTK_STYLE_GET_CLASS (style)->draw_check (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
6086 }
6087
6088 /**
6089  * gtk_paint_option:
6090  * @style: a #GtkStyle
6091  * @window: a #GdkWindow
6092  * @state_type: a state
6093  * @shadow_type: the type of shadow to draw
6094  * @area: clip rectangle
6095  * @widget: the widget
6096  * @detail: a style detail
6097  * @x: x origin of the rectangle to draw the option in
6098  * @y: y origin of the rectangle to draw the option in
6099  * @width: the width of the rectangle to draw the option in
6100  * @height: the height of the rectangle to draw the option in
6101  *
6102  * Draws a radio button indicator in the given rectangle on @window with 
6103  * the given parameters.
6104  */
6105 void
6106 gtk_paint_option (GtkStyle      *style,
6107                   GdkWindow     *window,
6108                   GtkStateType   state_type,
6109                   GtkShadowType  shadow_type,
6110                   GdkRectangle  *area,
6111                   GtkWidget     *widget,
6112                   const gchar   *detail,
6113                   gint           x,
6114                   gint           y,
6115                   gint           width,
6116                   gint           height)
6117 {
6118   g_return_if_fail (GTK_IS_STYLE (style));
6119   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_option != NULL);
6120   
6121   GTK_STYLE_GET_CLASS (style)->draw_option (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
6122 }
6123
6124 /**
6125  * gtk_paint_tab:
6126  * @style: a #GtkStyle
6127  * @window: a #GdkWindow
6128  * @state_type: a state
6129  * @shadow_type: the type of shadow to draw
6130  * @area: clip rectangle
6131  * @widget: the widget
6132  * @detail: a style detail
6133  * @x: x origin of the rectangle to draw the tab in
6134  * @y: y origin of the rectangle to draw the tab in
6135  * @width: the width of the rectangle to draw the tab in
6136  * @height: the height of the rectangle to draw the tab in
6137  *
6138  * Draws an option menu tab (i.e. the up and down pointing arrows)
6139  * in the given rectangle on @window using the given parameters.
6140  */ 
6141 void
6142 gtk_paint_tab (GtkStyle      *style,
6143                GdkWindow     *window,
6144                GtkStateType   state_type,
6145                GtkShadowType  shadow_type,
6146                GdkRectangle  *area,
6147                GtkWidget     *widget,
6148                const gchar   *detail,
6149                gint           x,
6150                gint           y,
6151                gint           width,
6152                gint           height)
6153 {
6154   g_return_if_fail (GTK_IS_STYLE (style));
6155   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_tab != NULL);
6156   
6157   GTK_STYLE_GET_CLASS (style)->draw_tab (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height);
6158 }
6159
6160 /**
6161  * gtk_paint_shadow_gap:
6162  * @style: a #GtkStyle
6163  * @window: a #GdkWindow
6164  * @state_type: a state
6165  * @shadow_type: type of shadow to draw
6166  * @area: clip rectangle
6167  * @widget: the widget
6168  * @detail: a style detail
6169  * @x: x origin of the rectangle
6170  * @y: y origin of the rectangle
6171  * @width: width of the rectangle 
6172  * @height: width of the rectangle 
6173  * @gap_side: side in which to leave the gap
6174  * @gap_x: starting position of the gap
6175  * @gap_width: width of the gap
6176  *
6177  * Draws a shadow around the given rectangle in @window 
6178  * using the given style and state and shadow type, leaving a 
6179  * gap in one side.
6180 */
6181 void
6182 gtk_paint_shadow_gap (GtkStyle       *style,
6183                       GdkWindow      *window,
6184                       GtkStateType    state_type,
6185                       GtkShadowType   shadow_type,
6186                       GdkRectangle   *area,
6187                       GtkWidget      *widget,
6188                       gchar          *detail,
6189                       gint            x,
6190                       gint            y,
6191                       gint            width,
6192                       gint            height,
6193                       GtkPositionType gap_side,
6194                       gint            gap_x,
6195                       gint            gap_width)
6196 {
6197   g_return_if_fail (GTK_IS_STYLE (style));
6198   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow_gap != NULL);
6199   
6200   GTK_STYLE_GET_CLASS (style)->draw_shadow_gap (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height, gap_side, gap_x, gap_width);
6201 }
6202
6203
6204 /**
6205  * gtk_paint_box_gap:
6206  * @style: a #GtkStyle
6207  * @window: a #GdkWindow
6208  * @state_type: a state
6209  * @shadow_type: type of shadow to draw
6210  * @area: clip rectangle
6211  * @widget: the widget
6212  * @detail: a style detail
6213  * @x: x origin of the rectangle
6214  * @y: y origin of the rectangle
6215  * @width: width of the rectangle 
6216  * @height: width of the rectangle 
6217  * @gap_side: side in which to leave the gap
6218  * @gap_x: starting position of the gap
6219  * @gap_width: width of the gap
6220  *
6221  * Draws a box in @window using the given style and state and shadow type, 
6222  * leaving a gap in one side.
6223  */
6224 void
6225 gtk_paint_box_gap (GtkStyle       *style,
6226                    GdkWindow      *window,
6227                    GtkStateType    state_type,
6228                    GtkShadowType   shadow_type,
6229                    GdkRectangle   *area,
6230                    GtkWidget      *widget,
6231                    gchar          *detail,
6232                    gint            x,
6233                    gint            y,
6234                    gint            width,
6235                    gint            height,
6236                    GtkPositionType gap_side,
6237                    gint            gap_x,
6238                    gint            gap_width)
6239 {
6240   g_return_if_fail (GTK_IS_STYLE (style));
6241   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box_gap != NULL);
6242   
6243   GTK_STYLE_GET_CLASS (style)->draw_box_gap (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height, gap_side, gap_x, gap_width);
6244 }
6245
6246 /**
6247  * gtk_paint_extension: 
6248  * @style: a #GtkStyle
6249  * @window: a #GdkWindow
6250  * @state_type: a state
6251  * @shadow_type: type of shadow to draw
6252  * @area: clip rectangle
6253  * @widget: the widget
6254  * @detail: a style detail
6255  * @x: x origin of the extension
6256  * @y: y origin of the extension
6257  * @width: width of the extension 
6258  * @height: width of the extension 
6259  * @gap_side: the side on to which the extension is attached
6260  * 
6261  * Draws an extension, i.e. a notebook tab.
6262  **/
6263 void
6264 gtk_paint_extension (GtkStyle       *style,
6265                      GdkWindow      *window,
6266                      GtkStateType    state_type,
6267                      GtkShadowType   shadow_type,
6268                      GdkRectangle   *area,
6269                      GtkWidget      *widget,
6270                      gchar          *detail,
6271                      gint            x,
6272                      gint            y,
6273                      gint            width,
6274                      gint            height,
6275                      GtkPositionType gap_side)
6276 {
6277   g_return_if_fail (GTK_IS_STYLE (style));
6278   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_extension != NULL);
6279   
6280   GTK_STYLE_GET_CLASS (style)->draw_extension (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height, gap_side);
6281 }
6282
6283 /**
6284  * gtk_paint_focus:
6285  * @style: a #GtkStyle
6286  * @window: a #GdkWindow
6287  * @state_type: a state
6288  * @area: clip rectangle
6289  * @widget: the widget
6290  * @detail: a style detail
6291  * @x: the x origin of the rectangle around which to draw a focus indicator
6292  * @y: the y origin of the rectangle around which to draw a focus indicator
6293  * @width: the width of the rectangle around which to draw a focus indicator
6294  * @height: the height of the rectangle around which to draw a focus indicator
6295  *
6296  * Draws a focus indicator around the given rectangle on @window using the
6297  * given style.
6298  */
6299 void
6300 gtk_paint_focus (GtkStyle      *style,
6301                  GdkWindow     *window,
6302                  GtkStateType   state_type,
6303                  GdkRectangle  *area,
6304                  GtkWidget     *widget,
6305                  const gchar   *detail,
6306                  gint           x,
6307                  gint           y,
6308                  gint           width,
6309                  gint           height)
6310 {
6311   g_return_if_fail (GTK_IS_STYLE (style));
6312   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_focus != NULL);
6313   
6314   GTK_STYLE_GET_CLASS (style)->draw_focus (style, window, state_type, area, widget, detail, x, y, width, height);
6315 }
6316
6317 void
6318 gtk_paint_slider (GtkStyle      *style,
6319                   GdkWindow     *window,
6320                   GtkStateType   state_type,
6321                   GtkShadowType  shadow_type,
6322                   GdkRectangle  *area,
6323                   GtkWidget     *widget,
6324                   const gchar   *detail,
6325                   gint           x,
6326                   gint           y,
6327                   gint           width,
6328                   gint           height,
6329                   GtkOrientation orientation)
6330 {
6331   g_return_if_fail (GTK_IS_STYLE (style));
6332   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_slider != NULL);
6333   
6334   GTK_STYLE_GET_CLASS (style)->draw_slider (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height, orientation);
6335 }
6336
6337 /**
6338  * gtk_paint_handle:
6339  * @style: a #GtkStyle
6340  * @window: a #GdkWindow
6341  * @state_type: a state
6342  * @shadow_type: type of shadow to draw
6343  * @area: clip rectangle
6344  * @widget: the widget
6345  * @detail: a style detail
6346  * @x: x origin of the handle
6347  * @y: y origin of the handle
6348  * @width: with of the handle
6349  * @height: height of the handle
6350  * @orientation: the orientation of the handle
6351  * 
6352  * Draws a handle as used in #GtkHandleBox and #GtkPaned.
6353  **/
6354 void
6355 gtk_paint_handle (GtkStyle      *style,
6356                   GdkWindow     *window,
6357                   GtkStateType   state_type,
6358                   GtkShadowType  shadow_type,
6359                   GdkRectangle  *area,
6360                   GtkWidget     *widget,
6361                   const gchar   *detail,
6362                   gint           x,
6363                   gint           y,
6364                   gint           width,
6365                   gint           height,
6366                   GtkOrientation orientation)
6367 {
6368   g_return_if_fail (GTK_IS_STYLE (style));
6369   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_handle != NULL);
6370   
6371   GTK_STYLE_GET_CLASS (style)->draw_handle (style, window, state_type, shadow_type, area, widget, detail, x, y, width, height, orientation);
6372 }
6373
6374 /**
6375  * gtk_paint_expander:
6376  * @style: a #GtkStyle
6377  * @window: a #GdkWindow
6378  * @state_type: a state
6379  * @area: clip rectangle
6380  * @widget: the widget
6381  * @detail: a style detail
6382  * @x: the x position to draw the expander at
6383  * @y: the y position to draw the expander at
6384  * @expander_style: the style to draw the expander in
6385  * 
6386  * Draws an expander as used in #GtkTreeView.
6387  **/
6388 void
6389 gtk_paint_expander (GtkStyle        *style,
6390                     GdkWindow       *window,
6391                     GtkStateType     state_type,
6392                     GdkRectangle    *area,
6393                     GtkWidget       *widget,
6394                     const gchar     *detail,
6395                     gint             x,
6396                     gint             y,
6397                     GtkExpanderStyle expander_style)
6398 {
6399   g_return_if_fail (GTK_IS_STYLE (style));
6400   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_expander != NULL);
6401   
6402   GTK_STYLE_GET_CLASS (style)->draw_expander (style, window, state_type, area,
6403                                               widget, detail, x, y, expander_style);
6404 }
6405
6406 void
6407 gtk_paint_layout (GtkStyle        *style,
6408                   GdkWindow       *window,
6409                   GtkStateType     state_type,
6410                   gboolean         use_text,
6411                   GdkRectangle    *area,
6412                   GtkWidget       *widget,
6413                   const gchar     *detail,
6414                   gint             x,
6415                   gint             y,
6416                   PangoLayout     *layout)
6417 {
6418   g_return_if_fail (GTK_IS_STYLE (style));
6419   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_layout != NULL);
6420   
6421   GTK_STYLE_GET_CLASS (style)->draw_layout (style, window, state_type, use_text, area,
6422                                             widget, detail, x, y, layout);
6423 }
6424
6425 /**
6426  * gtk_paint_resize_grip:
6427  * @style: a #GtkStyle
6428  * @window: a #GdkWindow
6429  * @state_type: a state
6430  * @area: clip rectangle
6431  * @widget: the widget
6432  * @detail: a style detail
6433  * @edge: the edge in which to draw the resize grip
6434  * @x: the x origin of the rectangle in which to draw the resize grip
6435  * @y: the y origin of the rectangle in which to draw the resize grip
6436  * @width: the width of the rectangle in which to draw the resize grip
6437  * @height: the height of the rectangle in which to draw the resize grip
6438  *
6439  * Draws a resize grip in the given rectangle on @window using the given
6440  * parameters. 
6441  */
6442 void
6443 gtk_paint_resize_grip (GtkStyle      *style,
6444                        GdkWindow     *window,
6445                        GtkStateType   state_type,
6446                        GdkRectangle  *area,
6447                        GtkWidget     *widget,
6448                        const gchar   *detail,
6449                        GdkWindowEdge  edge,
6450                        gint           x,
6451                        gint           y,
6452                        gint           width,
6453                        gint           height)
6454
6455 {
6456   g_return_if_fail (GTK_IS_STYLE (style));
6457   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_resize_grip != NULL);
6458
6459   GTK_STYLE_GET_CLASS (style)->draw_resize_grip (style, window, state_type,
6460                                                  area, widget, detail,
6461                                                  edge, x, y, width, height);
6462 }
6463
6464 /**
6465  * gtk_border_copy:
6466  * @border_: a #GtkBorder.
6467  * @returns: a copy of @border_.
6468  *
6469  * Copies a #GtkBorder structure.
6470  **/
6471 GtkBorder *
6472 gtk_border_copy (const GtkBorder *border)
6473 {
6474   return (GtkBorder *)g_memdup (border, sizeof (GtkBorder));
6475 }
6476
6477 /**
6478  * gtk_border_free:
6479  * @border_: a #GtkBorder.
6480  * 
6481  * Frees a #GtkBorder structure.
6482  **/
6483 void
6484 gtk_border_free (GtkBorder *border)
6485 {
6486   g_free (border);
6487 }
6488
6489 GType
6490 gtk_border_get_type (void)
6491 {
6492   static GType our_type = 0;
6493   
6494   if (our_type == 0)
6495     our_type = g_boxed_type_register_static ("GtkBorder",
6496                                              (GBoxedCopyFunc) gtk_border_copy,
6497                                              (GBoxedFreeFunc) gtk_border_free);
6498
6499   return our_type;
6500 }
6501
6502 static GdkFont *
6503 gtk_style_get_font_internal (GtkStyle *style)
6504 {
6505   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
6506
6507   if (style->private_font && style->private_font_desc)
6508     {
6509       if (!style->font_desc ||
6510           !pango_font_description_equal (style->private_font_desc, style->font_desc))
6511         {
6512           gdk_font_unref (style->private_font);
6513           style->private_font = NULL;
6514           
6515           if (style->private_font_desc)
6516             {
6517               pango_font_description_free (style->private_font_desc);
6518               style->private_font_desc = NULL;
6519             }
6520         }
6521     }
6522   
6523   if (!style->private_font)
6524     {
6525       GdkDisplay *display;
6526
6527       if (style->colormap)
6528         {
6529           display = gdk_screen_get_display (gdk_colormap_get_screen (style->colormap));
6530         }
6531       else
6532         {
6533           display = gdk_display_get_default ();
6534           GTK_NOTE (MULTIHEAD,
6535                     g_warning ("gtk_style_get_font() should not be called on an unattached style"));
6536         }
6537       
6538       if (style->font_desc)
6539         {
6540           style->private_font = gdk_font_from_description_for_display (display, style->font_desc);
6541           style->private_font_desc = pango_font_description_copy (style->font_desc);
6542         }
6543
6544       if (!style->private_font)
6545         style->private_font = gdk_font_load_for_display (display, "fixed");
6546       
6547       if (!style->private_font) 
6548         g_error ("Unable to load \"fixed\" font");
6549     }
6550
6551   return style->private_font;
6552 }
6553
6554 /**
6555  * gtk_style_get_font:
6556  * @style: a #GtkStyle
6557  * 
6558  * Gets the #GdkFont to use for the given style. This is
6559  * meant only as a replacement for direct access to @style->font
6560  * and should not be used in new code. New code should
6561  * use @style->font_desc instead.
6562  * 
6563  * Return value: the #GdkFont for the style. This font is owned
6564  *   by the style; if you want to keep around a copy, you must
6565  *   call gdk_font_ref().
6566  **/
6567 GdkFont *
6568 gtk_style_get_font (GtkStyle *style)
6569 {
6570   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
6571
6572   return gtk_style_get_font_internal (style);
6573 }
6574
6575 /**
6576  * gtk_style_set_font:
6577  * @style: a #GtkStyle.
6578  * @font: a #GdkFont, or %NULL to use the #GdkFont corresponding
6579  *   to style->font_desc.
6580  * 
6581  * Sets the #GdkFont to use for a given style. This is
6582  * meant only as a replacement for direct access to style->font
6583  * and should not be used in new code. New code should
6584  * use style->font_desc instead.
6585  **/
6586 void
6587 gtk_style_set_font (GtkStyle *style,
6588                     GdkFont  *font)
6589 {
6590   GdkFont *old_font;
6591
6592   g_return_if_fail (GTK_IS_STYLE (style));
6593
6594   old_font = style->private_font;
6595
6596   style->private_font = font;
6597   if (font)
6598     gdk_font_ref (font);
6599
6600   if (old_font)
6601     gdk_font_unref (old_font);
6602
6603   if (style->private_font_desc)
6604     {
6605       pango_font_description_free (style->private_font_desc);
6606       style->private_font_desc = NULL;
6607     }
6608 }
6609
6610 typedef struct _CursorInfo CursorInfo;
6611
6612 struct _CursorInfo
6613 {
6614   GType for_type;
6615   GdkGC *primary_gc;
6616   GdkGC *secondary_gc;
6617 };
6618
6619 static void
6620 style_unrealize_cursor_gcs (GtkStyle *style)
6621 {
6622   CursorInfo *
6623   
6624   cursor_info = g_object_get_data (G_OBJECT (style), "gtk-style-cursor-info");
6625   if (cursor_info)
6626     {
6627       if (cursor_info->primary_gc)
6628         gtk_gc_release (cursor_info->primary_gc);
6629
6630       if (cursor_info->secondary_gc)
6631         gtk_gc_release (cursor_info->secondary_gc);
6632       
6633       g_free (cursor_info);
6634       g_object_set_data (G_OBJECT (style), "gtk-style-cursor-info", NULL);
6635     }
6636 }
6637
6638 static GdkGC *
6639 make_cursor_gc (GtkWidget   *widget,
6640                 const gchar *property_name,
6641                 const GdkColor *fallback)
6642 {
6643   GdkGCValues gc_values;
6644   GdkGCValuesMask gc_values_mask;
6645   GdkColor *cursor_color;
6646
6647   gtk_widget_style_get (widget, property_name, &cursor_color, NULL);
6648   
6649   gc_values_mask = GDK_GC_FOREGROUND;
6650   if (cursor_color)
6651     {
6652       gc_values.foreground = *cursor_color;
6653       gdk_color_free (cursor_color);
6654     }
6655   else
6656     gc_values.foreground = *fallback;
6657   
6658   gdk_rgb_find_color (widget->style->colormap, &gc_values.foreground);
6659   return gtk_gc_get (widget->style->depth, widget->style->colormap, &gc_values, gc_values_mask);
6660 }
6661
6662 static GdkGC *
6663 get_insertion_cursor_gc (GtkWidget *widget,
6664                          gboolean   is_primary)
6665 {
6666   CursorInfo *cursor_info;
6667
6668   cursor_info = g_object_get_data (G_OBJECT (widget->style), "gtk-style-cursor-info");
6669   if (!cursor_info)
6670     {
6671       cursor_info = g_new (CursorInfo, 1);
6672       g_object_set_data (G_OBJECT (widget->style), "gtk-style-cursor-info", cursor_info);
6673       cursor_info->primary_gc = NULL;
6674       cursor_info->secondary_gc = NULL;
6675       cursor_info->for_type = G_TYPE_INVALID;
6676     }
6677
6678   /* We have to keep track of the type because gtk_widget_style_get()
6679    * can return different results when called on the same property and
6680    * same style but for different widgets. :-(. That is,
6681    * GtkEntry::cursor-color = "red" in a style will modify the cursor
6682    * color for entries but not for text view.
6683    */
6684   if (cursor_info->for_type != G_OBJECT_TYPE (widget))
6685     {
6686       cursor_info->for_type = G_OBJECT_TYPE (widget);
6687       if (cursor_info->primary_gc)
6688         {
6689           gtk_gc_release (cursor_info->primary_gc);
6690           cursor_info->primary_gc = NULL;
6691         }
6692       if (cursor_info->secondary_gc)
6693         {
6694           gtk_gc_release (cursor_info->secondary_gc);
6695           cursor_info->secondary_gc = NULL;
6696         }
6697     }
6698
6699   if (is_primary)
6700     {
6701       if (!cursor_info->primary_gc)
6702         cursor_info->primary_gc = make_cursor_gc (widget,
6703                                                   "cursor-color",
6704                                                   &widget->style->black);
6705         
6706       return cursor_info->primary_gc;
6707     }
6708   else
6709     {
6710       static const GdkColor gray = { 0, 0x8888, 0x8888, 0x8888 };
6711       
6712       if (!cursor_info->secondary_gc)
6713         cursor_info->secondary_gc = make_cursor_gc (widget,
6714                                                     "secondary-cursor-color",
6715                                                     &gray);
6716         
6717       return cursor_info->secondary_gc;
6718     }
6719 }
6720
6721 static void
6722 draw_insertion_cursor (GtkWidget        *widget,
6723                        GdkDrawable      *drawable,
6724                        GdkGC            *gc,
6725                        GdkRectangle     *location,
6726                        GtkTextDirection  direction,
6727                        gboolean          draw_arrow)
6728 {
6729   gint stem_width;
6730   gint arrow_width;
6731   gint x, y;
6732   gint i;
6733   gfloat cursor_aspect_ratio;
6734   gint offset;
6735   
6736   g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
6737   
6738   gtk_widget_style_get (widget, "cursor-aspect-ratio", &cursor_aspect_ratio, NULL);
6739   
6740   stem_width = location->height * cursor_aspect_ratio + 1;
6741   arrow_width = stem_width + 1;
6742
6743   /* put (stem_width % 2) on the proper side of the cursor */
6744   if (direction == GTK_TEXT_DIR_LTR)
6745     offset = stem_width / 2;
6746   else
6747     offset = stem_width - stem_width / 2;
6748   
6749   for (i = 0; i < stem_width; i++)
6750     gdk_draw_line (drawable, gc,
6751                    location->x + i - offset, location->y,
6752                    location->x + i - offset, location->y + location->height - 1);
6753
6754   if (draw_arrow)
6755     {
6756       if (direction == GTK_TEXT_DIR_RTL)
6757         {
6758           x = location->x - offset - 1;
6759           y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
6760   
6761           for (i = 0; i < arrow_width; i++)
6762             {
6763               gdk_draw_line (drawable, gc,
6764                              x, y + i + 1,
6765                              x, y + 2 * arrow_width - i - 1);
6766               x --;
6767             }
6768         }
6769       else if (direction == GTK_TEXT_DIR_LTR)
6770         {
6771           x = location->x + stem_width - offset;
6772           y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
6773   
6774           for (i = 0; i < arrow_width; i++) 
6775             {
6776               gdk_draw_line (drawable, gc,
6777                              x, y + i + 1,
6778                              x, y + 2 * arrow_width - i - 1);
6779               x++;
6780             }
6781         }
6782     }
6783 }
6784
6785 /**
6786  * gtk_draw_insertion_cursor:
6787  * @widget:  a #GtkWidget
6788  * @drawable: a #GdkDrawable 
6789  * @area: rectangle to which the output is clipped, or %NULL if the
6790  *        output should not be clipped
6791  * @location: location where to draw the cursor (@location->width is ignored)
6792  * @is_primary: if the cursor should be the primary cursor color.
6793  * @direction: whether the cursor is left-to-right or
6794  *             right-to-left. Should never be #GTK_TEXT_DIR_NONE
6795  * @draw_arrow: %TRUE to draw a directional arrow on the
6796  *        cursor. Should be %FALSE unless the cursor is split.
6797  * 
6798  * Draws a text caret on @drawable at @location. This is not a style function
6799  * but merely a convenience function for drawing the standard cursor shape.
6800  *
6801  * Since: 2.4
6802  **/
6803 void
6804 gtk_draw_insertion_cursor (GtkWidget        *widget,
6805                            GdkDrawable      *drawable,
6806                            GdkRectangle     *area,
6807                            GdkRectangle     *location,
6808                            gboolean          is_primary,
6809                            GtkTextDirection  direction,
6810                            gboolean          draw_arrow)
6811 {
6812   GdkGC *gc;
6813
6814   g_return_if_fail (GTK_IS_WIDGET (widget));
6815   g_return_if_fail (GDK_IS_DRAWABLE (drawable));
6816   g_return_if_fail (location != NULL);
6817   g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
6818
6819   gc = get_insertion_cursor_gc (widget, is_primary);
6820   if (area)
6821     gdk_gc_set_clip_rectangle (gc, area);
6822   
6823   draw_insertion_cursor (widget, drawable, gc,
6824                          location, direction, draw_arrow);
6825   
6826   if (area)
6827     gdk_gc_set_clip_rectangle (gc, NULL);
6828 }