]> Pileus Git - ~andy/gtk/blob - gtk/gtkstyle.c
Merge branch 'master' into broadway
[~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 <gobject/gvaluecollector.h>
32 #include "gtkmarshalers.h"
33 #include "gtkpango.h"
34 #include "gtkrc.h"
35 #include "gtkspinbutton.h"
36 #include "gtkstyle.h"
37 #include "gtkwidget.h"
38 #include "gtkthemes.h"
39 #include "gtkiconfactory.h"
40 #include "gtksettings.h"        /* _gtk_settings_parse_convert() */
41 #include "gtkintl.h"
42 #include "gtkdebug.h"
43 #include "gtkspinner.h"
44 #include "gtkborder.h"
45
46
47 /**
48  * SECTION:gtkstyle
49  * @Short_description: An object that hold style information for widgets
50  * @Title: GtkStyle
51  *
52  * A #GtkStyle object encapsulates the information that provides the look and
53  * feel for a widget. Each #GtkWidget has an associated #GTkStyle object that
54  * is used when rendering that widget. Also, a #GtkStyle holds information for
55  * the five possible widget states though not every widget supports all five
56  * states; see #GtkStateType.
57  *
58  * Usually the #GtkStyle for a widget is the same as the default style that is
59  * set by GTK+ and modified the theme engine.
60  *
61  * Usually applications should not need to use or modify the #GtkStyle of their
62  * widgets.
63  *
64  * <warning>
65  * In GTK+ 3.0, GtkStyle has been deprecated and replaced by #GtkStyleContext.
66  * </warning>
67  */
68
69
70 #define LIGHTNESS_MULT  1.3
71 #define DARKNESS_MULT   0.7
72
73 /* --- typedefs & structures --- */
74 typedef struct {
75   GType       widget_type;
76   GParamSpec *pspec;
77   GValue      value;
78 } PropertyValue;
79
80 #define GTK_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_STYLE, GtkStylePrivate))
81
82 typedef struct _GtkStylePrivate GtkStylePrivate;
83
84 struct _GtkStylePrivate {
85   GSList *color_hashes;
86   GtkStyleContext *context;
87   gulong context_changed_id;
88 };
89
90 enum {
91   PROP_0,
92   PROP_CONTEXT
93 };
94
95 /* --- prototypes --- */
96 static void      gtk_style_finalize             (GObject        *object);
97 static void      gtk_style_constructed          (GObject        *object);
98 static void      gtk_style_set_property         (GObject        *object,
99                                                  guint           prop_id,
100                                                  const GValue   *value,
101                                                  GParamSpec     *pspec);
102 static void      gtk_style_get_property         (GObject        *object,
103                                                  guint           prop_id,
104                                                  GValue         *value,
105                                                  GParamSpec     *pspec);
106
107 static void      gtk_style_realize              (GtkStyle       *style,
108                                                  GdkVisual      *visual);
109 static void      gtk_style_real_realize        (GtkStyle        *style);
110 static void      gtk_style_real_unrealize      (GtkStyle        *style);
111 static void      gtk_style_real_copy           (GtkStyle        *style,
112                                                 GtkStyle        *src);
113 static void      gtk_style_real_set_background (GtkStyle        *style,
114                                                 GdkWindow       *window,
115                                                 GtkStateType     state_type);
116 static GtkStyle *gtk_style_real_clone          (GtkStyle        *style);
117 static void      gtk_style_real_init_from_rc   (GtkStyle        *style,
118                                                 GtkRcStyle      *rc_style);
119 static GdkPixbuf *gtk_default_render_icon      (GtkStyle            *style,
120                                                 const GtkIconSource *source,
121                                                 GtkTextDirection     direction,
122                                                 GtkStateType         state,
123                                                 GtkIconSize          size,
124                                                 GtkWidget           *widget,
125                                                 const gchar         *detail);
126 static void gtk_default_draw_hline      (GtkStyle        *style,
127                                          cairo_t         *cr,
128                                          GtkStateType     state_type,
129                                          GtkWidget       *widget,
130                                          const gchar     *detail,
131                                          gint             x1,
132                                          gint             x2,
133                                          gint             y);
134 static void gtk_default_draw_vline      (GtkStyle        *style,
135                                          cairo_t         *cr,
136                                          GtkStateType     state_type,
137                                          GtkWidget       *widget,
138                                          const gchar     *detail,
139                                          gint             y1,
140                                          gint             y2,
141                                          gint             x);
142 static void gtk_default_draw_shadow     (GtkStyle        *style,
143                                          cairo_t         *cr,
144                                          GtkStateType     state_type,
145                                          GtkShadowType    shadow_type,
146                                          GtkWidget       *widget,
147                                          const gchar     *detail,
148                                          gint             x,
149                                          gint             y,
150                                          gint             width,
151                                          gint             height);
152 static void gtk_default_draw_arrow      (GtkStyle        *style,
153                                          cairo_t         *cr,
154                                          GtkStateType     state_type,
155                                          GtkShadowType    shadow_type,
156                                          GtkWidget       *widget,
157                                          const gchar     *detail,
158                                          GtkArrowType     arrow_type,
159                                          gboolean         fill,
160                                          gint             x,
161                                          gint             y,
162                                          gint             width,
163                                          gint             height);
164 static void gtk_default_draw_diamond    (GtkStyle        *style,
165                                          cairo_t         *cr,
166                                          GtkStateType     state_type,
167                                          GtkShadowType    shadow_type,
168                                          GtkWidget       *widget,
169                                          const gchar     *detail,
170                                          gint             x,
171                                          gint             y,
172                                          gint             width,
173                                          gint             height);
174 static void gtk_default_draw_box        (GtkStyle        *style,
175                                          cairo_t         *cr,
176                                          GtkStateType     state_type,
177                                          GtkShadowType    shadow_type,
178                                          GtkWidget       *widget,
179                                          const gchar     *detail,
180                                          gint             x,
181                                          gint             y,
182                                          gint             width,
183                                          gint             height);
184 static void gtk_default_draw_flat_box   (GtkStyle        *style,
185                                          cairo_t         *cr,
186                                          GtkStateType     state_type,
187                                          GtkShadowType    shadow_type,
188                                          GtkWidget       *widget,
189                                          const gchar     *detail,
190                                          gint             x,
191                                          gint             y,
192                                          gint             width,
193                                          gint             height);
194 static void gtk_default_draw_check      (GtkStyle        *style,
195                                          cairo_t         *cr,
196                                          GtkStateType     state_type,
197                                          GtkShadowType    shadow_type,
198                                          GtkWidget       *widget,
199                                          const gchar     *detail,
200                                          gint             x,
201                                          gint             y,
202                                          gint             width,
203                                          gint             height);
204 static void gtk_default_draw_option     (GtkStyle        *style,
205                                          cairo_t         *cr,
206                                          GtkStateType     state_type,
207                                          GtkShadowType    shadow_type,
208                                          GtkWidget       *widget,
209                                          const gchar     *detail,
210                                          gint             x,
211                                          gint             y,
212                                          gint             width,
213                                          gint             height);
214 static void gtk_default_draw_tab        (GtkStyle        *style,
215                                          cairo_t         *cr,
216                                          GtkStateType     state_type,
217                                          GtkShadowType    shadow_type,
218                                          GtkWidget       *widget,
219                                          const gchar     *detail,
220                                          gint             x,
221                                          gint             y,
222                                          gint             width,
223                                          gint             height);
224 static void gtk_default_draw_shadow_gap (GtkStyle        *style,
225                                          cairo_t         *cr,
226                                          GtkStateType     state_type,
227                                          GtkShadowType    shadow_type,
228                                          GtkWidget       *widget,
229                                          const gchar     *detail,
230                                          gint             x,
231                                          gint             y,
232                                          gint             width,
233                                          gint             height,
234                                          GtkPositionType  gap_side,
235                                          gint             gap_x,
236                                          gint             gap_width);
237 static void gtk_default_draw_box_gap    (GtkStyle        *style,
238                                          cairo_t         *cr,
239                                          GtkStateType     state_type,
240                                          GtkShadowType    shadow_type,
241                                          GtkWidget       *widget,
242                                          const gchar     *detail,
243                                          gint             x,
244                                          gint             y,
245                                          gint             width,
246                                          gint             height,
247                                          GtkPositionType  gap_side,
248                                          gint             gap_x,
249                                          gint             gap_width);
250 static void gtk_default_draw_extension  (GtkStyle        *style,
251                                          cairo_t         *cr,
252                                          GtkStateType     state_type,
253                                          GtkShadowType    shadow_type,
254                                          GtkWidget       *widget,
255                                          const gchar     *detail,
256                                          gint             x,
257                                          gint             y,
258                                          gint             width,
259                                          gint             height,
260                                          GtkPositionType  gap_side);
261 static void gtk_default_draw_focus      (GtkStyle        *style,
262                                          cairo_t         *cr,
263                                          GtkStateType     state_type,
264                                          GtkWidget       *widget,
265                                          const gchar     *detail,
266                                          gint             x,
267                                          gint             y,
268                                          gint             width,
269                                          gint             height);
270 static void gtk_default_draw_slider     (GtkStyle        *style,
271                                          cairo_t         *cr,
272                                          GtkStateType     state_type,
273                                          GtkShadowType    shadow_type,
274                                          GtkWidget       *widget,
275                                          const gchar     *detail,
276                                          gint             x,
277                                          gint             y,
278                                          gint             width,
279                                          gint             height,
280                                          GtkOrientation   orientation);
281 static void gtk_default_draw_handle     (GtkStyle        *style,
282                                          cairo_t         *cr,
283                                          GtkStateType     state_type,
284                                          GtkShadowType    shadow_type,
285                                          GtkWidget       *widget,
286                                          const gchar     *detail,
287                                          gint             x,
288                                          gint             y,
289                                          gint             width,
290                                          gint             height,
291                                          GtkOrientation   orientation);
292 static void gtk_default_draw_expander   (GtkStyle        *style,
293                                          cairo_t         *cr,
294                                          GtkStateType     state_type,
295                                          GtkWidget       *widget,
296                                          const gchar     *detail,
297                                          gint             x,
298                                          gint             y,
299                                          GtkExpanderStyle expander_style);
300 static void gtk_default_draw_layout     (GtkStyle        *style,
301                                          cairo_t         *cr,
302                                          GtkStateType     state_type,
303                                          gboolean         use_text,
304                                          GtkWidget       *widget,
305                                          const gchar     *detail,
306                                          gint             x,
307                                          gint             y,
308                                          PangoLayout     *layout);
309 static void gtk_default_draw_resize_grip (GtkStyle       *style,
310                                           cairo_t        *cr,
311                                           GtkStateType    state_type,
312                                           GtkWidget      *widget,
313                                           const gchar    *detail,
314                                           GdkWindowEdge   edge,
315                                           gint            x,
316                                           gint            y,
317                                           gint            width,
318                                           gint            height);
319 static void gtk_default_draw_spinner     (GtkStyle       *style,
320                                           cairo_t        *cr,
321                                           GtkStateType    state_type,
322                                           GtkWidget      *widget,
323                                           const gchar    *detail,
324                                           guint           step,
325                                           gint            x,
326                                           gint            y,
327                                           gint            width,
328                                           gint            height);
329
330 static void rgb_to_hls                  (gdouble         *r,
331                                          gdouble         *g,
332                                          gdouble         *b);
333 static void hls_to_rgb                  (gdouble         *h,
334                                          gdouble         *l,
335                                          gdouble         *s);
336
337 static void style_unrealize_cursors     (GtkStyle *style);
338
339 static void transform_detail_string (const gchar     *detail,
340                                      GtkStyleContext *context);
341
342 /*
343  * Data for default check and radio buttons
344  */
345
346 static const GtkRequisition default_option_indicator_size = { 7, 13 };
347 static const GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };
348
349 #define GTK_GRAY                0xdcdc, 0xdada, 0xd5d5
350 #define GTK_DARK_GRAY           0xc4c4, 0xc2c2, 0xbdbd
351 #define GTK_LIGHT_GRAY          0xeeee, 0xebeb, 0xe7e7
352 #define GTK_WHITE               0xffff, 0xffff, 0xffff
353 #define GTK_BLUE                0x4b4b, 0x6969, 0x8383
354 #define GTK_VERY_DARK_GRAY      0x9c9c, 0x9a9a, 0x9494
355 #define GTK_BLACK               0x0000, 0x0000, 0x0000
356 #define GTK_WEAK_GRAY           0x7530, 0x7530, 0x7530
357
358 /* --- variables --- */
359 static const GdkColor gtk_default_normal_fg =      { 0, GTK_BLACK };
360 static const GdkColor gtk_default_active_fg =      { 0, GTK_BLACK };
361 static const GdkColor gtk_default_prelight_fg =    { 0, GTK_BLACK };
362 static const GdkColor gtk_default_selected_fg =    { 0, GTK_WHITE };
363 static const GdkColor gtk_default_insensitive_fg = { 0, GTK_WEAK_GRAY };
364
365 static const GdkColor gtk_default_normal_bg =      { 0, GTK_GRAY };
366 static const GdkColor gtk_default_active_bg =      { 0, GTK_DARK_GRAY };
367 static const GdkColor gtk_default_prelight_bg =    { 0, GTK_LIGHT_GRAY };
368 static const GdkColor gtk_default_selected_bg =    { 0, GTK_BLUE };
369 static const GdkColor gtk_default_insensitive_bg = { 0, GTK_GRAY };
370 static const GdkColor gtk_default_selected_base =  { 0, GTK_BLUE };
371 static const GdkColor gtk_default_active_base =    { 0, GTK_VERY_DARK_GRAY };
372
373 /* --- signals --- */
374 static guint realize_signal = 0;
375 static guint unrealize_signal = 0;
376
377 G_DEFINE_TYPE (GtkStyle, gtk_style, G_TYPE_OBJECT)
378
379 /* --- functions --- */
380
381 /**
382  * _gtk_style_init_for_settings:
383  * @style: a #GtkStyle
384  * @settings: a #GtkSettings
385  * 
386  * Initializes the font description in @style according to the default
387  * font name of @settings. This is called for gtk_style_new() with
388  * the settings for the default screen (if any); if we are creating
389  * a style for a particular screen, we then call it again in a
390  * location where we know the correct settings.
391  * The reason for this is that gtk_rc_style_create_style() doesn't
392  * take the screen for an argument.
393  **/
394 void
395 _gtk_style_init_for_settings (GtkStyle    *style,
396                               GtkSettings *settings)
397 {
398   const gchar *font_name = _gtk_rc_context_get_default_font_name (settings);
399
400   if (style->font_desc)
401     pango_font_description_free (style->font_desc);
402   
403   style->font_desc = pango_font_description_from_string (font_name);
404       
405   if (!pango_font_description_get_family (style->font_desc))
406     {
407       g_warning ("Default font does not have a family set");
408       pango_font_description_set_family (style->font_desc, "Sans");
409     }
410   if (pango_font_description_get_size (style->font_desc) <= 0)
411     {
412       g_warning ("Default font does not have a positive size");
413       pango_font_description_set_size (style->font_desc, 10 * PANGO_SCALE);
414     }
415 }
416
417 static void
418 gtk_style_init (GtkStyle *style)
419 {
420   gint i;
421   
422   GtkSettings *settings = gtk_settings_get_default ();
423   
424   if (settings)
425     _gtk_style_init_for_settings (style, settings);
426   else
427     style->font_desc = pango_font_description_from_string ("Sans 10");
428   
429   style->attach_count = 0;
430   
431   style->black.red = 0;
432   style->black.green = 0;
433   style->black.blue = 0;
434   
435   style->white.red = 65535;
436   style->white.green = 65535;
437   style->white.blue = 65535;
438   
439   style->fg[GTK_STATE_NORMAL] = gtk_default_normal_fg;
440   style->fg[GTK_STATE_ACTIVE] = gtk_default_active_fg;
441   style->fg[GTK_STATE_PRELIGHT] = gtk_default_prelight_fg;
442   style->fg[GTK_STATE_SELECTED] = gtk_default_selected_fg;
443   style->fg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
444   
445   style->bg[GTK_STATE_NORMAL] = gtk_default_normal_bg;
446   style->bg[GTK_STATE_ACTIVE] = gtk_default_active_bg;
447   style->bg[GTK_STATE_PRELIGHT] = gtk_default_prelight_bg;
448   style->bg[GTK_STATE_SELECTED] = gtk_default_selected_bg;
449   style->bg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_bg;
450   
451   for (i = 0; i < 4; i++)
452     {
453       style->text[i] = style->fg[i];
454       style->base[i] = style->white;
455     }
456
457   style->base[GTK_STATE_SELECTED] = gtk_default_selected_base;
458   style->text[GTK_STATE_SELECTED] = style->white;
459   style->base[GTK_STATE_ACTIVE] = gtk_default_active_base;
460   style->text[GTK_STATE_ACTIVE] = style->white;
461   style->base[GTK_STATE_INSENSITIVE] = gtk_default_prelight_bg;
462   style->text[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
463   
464   style->rc_style = NULL;
465   
466   style->xthickness = 2;
467   style->ythickness = 2;
468
469   style->property_cache = NULL;
470 }
471
472 static void
473 gtk_style_class_init (GtkStyleClass *klass)
474 {
475   GObjectClass *object_class = G_OBJECT_CLASS (klass);
476   
477   object_class->finalize = gtk_style_finalize;
478   object_class->set_property = gtk_style_set_property;
479   object_class->get_property = gtk_style_get_property;
480   object_class->constructed = gtk_style_constructed;
481
482   klass->clone = gtk_style_real_clone;
483   klass->copy = gtk_style_real_copy;
484   klass->init_from_rc = gtk_style_real_init_from_rc;
485   klass->realize = gtk_style_real_realize;
486   klass->unrealize = gtk_style_real_unrealize;
487   klass->set_background = gtk_style_real_set_background;
488   klass->render_icon = gtk_default_render_icon;
489
490   klass->draw_hline = gtk_default_draw_hline;
491   klass->draw_vline = gtk_default_draw_vline;
492   klass->draw_shadow = gtk_default_draw_shadow;
493   klass->draw_arrow = gtk_default_draw_arrow;
494   klass->draw_diamond = gtk_default_draw_diamond;
495   klass->draw_box = gtk_default_draw_box;
496   klass->draw_flat_box = gtk_default_draw_flat_box;
497   klass->draw_check = gtk_default_draw_check;
498   klass->draw_option = gtk_default_draw_option;
499   klass->draw_tab = gtk_default_draw_tab;
500   klass->draw_shadow_gap = gtk_default_draw_shadow_gap;
501   klass->draw_box_gap = gtk_default_draw_box_gap;
502   klass->draw_extension = gtk_default_draw_extension;
503   klass->draw_focus = gtk_default_draw_focus;
504   klass->draw_slider = gtk_default_draw_slider;
505   klass->draw_handle = gtk_default_draw_handle;
506   klass->draw_expander = gtk_default_draw_expander;
507   klass->draw_layout = gtk_default_draw_layout;
508   klass->draw_resize_grip = gtk_default_draw_resize_grip;
509   klass->draw_spinner = gtk_default_draw_spinner;
510
511   g_type_class_add_private (object_class, sizeof (GtkStylePrivate));
512
513   g_object_class_install_property (object_class,
514                                    PROP_CONTEXT,
515                                    g_param_spec_object ("context",
516                                                         P_("Style context"),
517                                                         P_("GtkStyleContext to get style from"),
518                                                         GTK_TYPE_STYLE_CONTEXT,
519                                                         G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
520
521   /**
522    * GtkStyle::realize:
523    * @style: the object which received the signal
524    *
525    * Emitted when the style has been initialized for a particular
526    * visual. Connecting to this signal is probably seldom
527    * useful since most of the time applications and widgets only
528    * deal with styles that have been already realized.
529    *
530    * Since: 2.4
531    */
532   realize_signal = g_signal_new (I_("realize"),
533                                  G_TYPE_FROM_CLASS (object_class),
534                                  G_SIGNAL_RUN_FIRST,
535                                  G_STRUCT_OFFSET (GtkStyleClass, realize),
536                                  NULL, NULL,
537                                  _gtk_marshal_VOID__VOID,
538                                  G_TYPE_NONE, 0);
539   /**
540    * GtkStyle::unrealize:
541    * @style: the object which received the signal
542    *
543    * Emitted when the aspects of the style specific to a particular visual
544    * is being cleaned up. A connection to this signal can be useful
545    * if a widget wants to cache objects as object data on #GtkStyle.
546    * This signal provides a convenient place to free such cached objects.
547    *
548    * Since: 2.4
549    */
550   unrealize_signal = g_signal_new (I_("unrealize"),
551                                    G_TYPE_FROM_CLASS (object_class),
552                                    G_SIGNAL_RUN_FIRST,
553                                    G_STRUCT_OFFSET (GtkStyleClass, unrealize),
554                                    NULL, NULL,
555                                    _gtk_marshal_VOID__VOID,
556                                    G_TYPE_NONE, 0);
557 }
558
559 static void
560 clear_property_cache (GtkStyle *style)
561 {
562   if (style->property_cache)
563     {
564       guint i;
565
566       for (i = 0; i < style->property_cache->len; i++)
567         {
568           PropertyValue *node = &g_array_index (style->property_cache, PropertyValue, i);
569
570           g_param_spec_unref (node->pspec);
571           g_value_unset (&node->value);
572         }
573       g_array_free (style->property_cache, TRUE);
574       style->property_cache = NULL;
575     }
576 }
577
578 static void
579 gtk_style_finalize (GObject *object)
580 {
581   GtkStyle *style = GTK_STYLE (object);
582   GtkStylePrivate *priv = GTK_STYLE_GET_PRIVATE (style);
583
584   g_return_if_fail (style->attach_count == 0);
585
586   clear_property_cache (style);
587   
588   /* All the styles in the list have the same 
589    * style->styles pointer. If we delete the 
590    * *first* style from the list, we need to update
591    * the style->styles pointers from all the styles.
592    * Otherwise we simply remove the node from
593    * the list.
594    */
595   if (style->styles)
596     {
597       if (style->styles->data != style)
598         style->styles = g_slist_remove (style->styles, style);
599       else
600         {
601           GSList *tmp_list = style->styles->next;
602           
603           while (tmp_list)
604             {
605               GTK_STYLE (tmp_list->data)->styles = style->styles->next;
606               tmp_list = tmp_list->next;
607             }
608           g_slist_free_1 (style->styles);
609         }
610     }
611
612   g_slist_foreach (style->icon_factories, (GFunc) g_object_unref, NULL);
613   g_slist_free (style->icon_factories);
614
615   g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
616   g_slist_free (priv->color_hashes);
617
618   pango_font_description_free (style->font_desc);
619
620   if (style->private_font_desc)
621     pango_font_description_free (style->private_font_desc);
622
623   if (style->rc_style)
624     g_object_unref (style->rc_style);
625
626   if (priv->context)
627     {
628       if (priv->context_changed_id)
629         g_signal_handler_disconnect (priv->context, priv->context_changed_id);
630
631       g_object_unref (priv->context);
632     }
633
634   G_OBJECT_CLASS (gtk_style_parent_class)->finalize (object);
635 }
636
637 static void
638 gtk_style_set_property (GObject      *object,
639                         guint         prop_id,
640                         const GValue *value,
641                         GParamSpec   *pspec)
642 {
643   GtkStylePrivate *priv;
644
645   priv = GTK_STYLE_GET_PRIVATE (object);
646
647   switch (prop_id)
648     {
649     case PROP_CONTEXT:
650       priv->context = g_value_dup_object (value);
651       break;
652     default:
653       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
654       break;
655     }
656 }
657
658 static void
659 gtk_style_get_property (GObject      *object,
660                         guint         prop_id,
661                         GValue       *value,
662                         GParamSpec   *pspec)
663 {
664   GtkStylePrivate *priv;
665
666   priv = GTK_STYLE_GET_PRIVATE (object);
667
668   switch (prop_id)
669     {
670     case PROP_CONTEXT:
671       g_value_set_object (value, priv->context);
672       break;
673     default:
674       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
675       break;
676     }
677 }
678
679 static void
680 set_color (GtkStyle        *style,
681            GtkStyleContext *context,
682            GtkStateType     state,
683            GtkRcFlags       prop)
684 {
685   GtkStateFlags flags;
686   GdkRGBA *color = NULL;
687   GdkColor *dest = { 0 }; /* Shut up gcc */
688
689   switch (state)
690     {
691     case GTK_STATE_ACTIVE:
692       flags = GTK_STATE_FLAG_ACTIVE;
693       break;
694     case GTK_STATE_PRELIGHT:
695       flags = GTK_STATE_FLAG_PRELIGHT;
696       break;
697     case GTK_STATE_SELECTED:
698       flags = GTK_STATE_FLAG_SELECTED;
699       break;
700     case GTK_STATE_INSENSITIVE:
701       flags = GTK_STATE_FLAG_INSENSITIVE;
702       break;
703     default:
704       flags = 0;
705     }
706
707   switch (prop)
708     {
709     case GTK_RC_BG:
710       gtk_style_context_get (context, flags,
711                              "background-color", &color,
712                              NULL);
713       dest = &style->bg[state];
714       break;
715     case GTK_RC_FG:
716       gtk_style_context_get (context, flags,
717                              "color", &color,
718                              NULL);
719       dest = &style->fg[state];
720       break;
721     case GTK_RC_TEXT:
722       gtk_style_context_get (context, flags,
723                              "color", &color,
724                              NULL);
725       dest = &style->text[state];
726       break;
727     case GTK_RC_BASE:
728       gtk_style_context_get (context, flags,
729                              "background-color", &color,
730                              NULL);
731       dest = &style->base[state];
732       break;
733     }
734
735   if (color)
736     {
737       dest->pixel = 0;
738       dest->red = CLAMP ((guint) (color->red * 65535), 0, 65535);
739       dest->green = CLAMP ((guint) (color->green * 65535), 0, 65535);
740       dest->blue = CLAMP ((guint) (color->blue * 65535), 0, 65535);
741       gdk_rgba_free (color);
742     }
743 }
744
745 static void
746 gtk_style_update_from_context (GtkStyle *style)
747 {
748   GtkStylePrivate *priv;
749   GtkStateType state;
750   GtkBorder *padding;
751
752   priv = GTK_STYLE_GET_PRIVATE (style);
753
754   for (state = GTK_STATE_NORMAL; state <= GTK_STATE_INSENSITIVE; state++)
755     {
756       if (gtk_style_context_has_class (priv->context, "entry"))
757         {
758           gtk_style_context_save (priv->context);
759           gtk_style_context_remove_class (priv->context, "entry");
760           set_color (style, priv->context, state, GTK_RC_BG);
761           set_color (style, priv->context, state, GTK_RC_FG);
762           gtk_style_context_restore (priv->context);
763
764           set_color (style, priv->context, state, GTK_RC_BASE);
765           set_color (style, priv->context, state, GTK_RC_TEXT);
766         }
767       else
768         {
769           gtk_style_context_save (priv->context);
770           gtk_style_context_add_class (priv->context, "entry");
771           set_color (style, priv->context, state, GTK_RC_BASE);
772           set_color (style, priv->context, state, GTK_RC_TEXT);
773           gtk_style_context_restore (priv->context);
774
775           set_color (style, priv->context, state, GTK_RC_BG);
776           set_color (style, priv->context, state, GTK_RC_FG);
777         }
778     }
779
780   if (style->font_desc)
781     pango_font_description_free (style->font_desc);
782
783   gtk_style_context_get (priv->context, 0,
784                          "font", &style->font_desc,
785                          "padding", &padding,
786                          NULL);
787
788   if (padding)
789     {
790       style->xthickness = padding->left;
791       style->ythickness = padding->top;
792
793       gtk_border_free (padding);
794     }
795 }
796
797 static void
798 style_context_changed (GtkStyleContext *context,
799                        gpointer         user_data)
800 {
801   gtk_style_update_from_context (GTK_STYLE (user_data));
802 }
803
804 static void
805 gtk_style_constructed (GObject *object)
806 {
807   GtkStylePrivate *priv;
808
809   priv = GTK_STYLE_GET_PRIVATE (object);
810
811   if (priv->context)
812     {
813       gtk_style_update_from_context (GTK_STYLE (object));
814
815       priv->context_changed_id = g_signal_connect (priv->context, "changed",
816                                                    G_CALLBACK (style_context_changed), object);
817     }
818 }
819
820 /**
821  * gtk_style_copy:
822  * @style: a #GtkStyle
823  *
824  * Creates a copy of the passed in #GtkStyle object.
825  *
826  * Returns: (transfer full): a copy of @style
827  *
828  * Deprecated:3.0: Use #GtkStyleContext instead
829  */
830 GtkStyle*
831 gtk_style_copy (GtkStyle *style)
832 {
833   GtkStyle *new_style;
834   
835   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
836   
837   new_style = GTK_STYLE_GET_CLASS (style)->clone (style);
838   GTK_STYLE_GET_CLASS (style)->copy (new_style, style);
839
840   return new_style;
841 }
842
843 static GtkStyle*
844 gtk_style_duplicate (GtkStyle *style)
845 {
846   GtkStyle *new_style;
847   
848   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
849   
850   new_style = gtk_style_copy (style);
851   
852   /* All the styles in the list have the same 
853    * style->styles pointer. When we insert a new 
854    * style, we append it to the list to avoid having 
855    * to update the existing ones. 
856    */
857   style->styles = g_slist_append (style->styles, new_style);
858   new_style->styles = style->styles;  
859   
860   return new_style;
861 }
862
863 /**
864  * gtk_style_new:
865  * @returns: a new #GtkStyle.
866  *
867  * Creates a new #GtkStyle.
868  *
869  * Deprecated: 3.0: Use #GtkStyleContext
870  **/
871 GtkStyle*
872 gtk_style_new (void)
873 {
874   GtkStyle *style;
875   
876   style = g_object_new (GTK_TYPE_STYLE, NULL);
877   
878   return style;
879 }
880
881 gboolean
882 gtk_style_has_context (GtkStyle *style)
883 {
884   GtkStylePrivate *priv;
885
886   priv = GTK_STYLE_GET_PRIVATE (style);
887
888   return priv->context != NULL;
889 }
890
891 /**
892  * gtk_style_attach:
893  * @style: a #GtkStyle.
894  * @window: a #GdkWindow.
895  *
896  * Attaches a style to a window; this process allocates the
897  * colors and creates the GC's for the style - it specializes
898  * it to a particular visual. The process may involve the creation
899  * of a new style if the style has already been attached to a
900  * window with a different style and visual.
901  *
902  * Since this function may return a new object, you have to use it
903  * in the following way:
904  * <literal>style = gtk_style_attach (style, window)</literal>
905  *
906  * Returns: Either @style, or a newly-created #GtkStyle.
907  *   If the style is newly created, the style parameter
908  *   will be unref'ed, and the new style will have
909  *   a reference count belonging to the caller.
910  *
911  * Deprecated:3.0: Use gtk_widget_style_attach() instead
912  */
913 GtkStyle*
914 gtk_style_attach (GtkStyle  *style,
915                   GdkWindow *window)
916 {
917   GSList *styles;
918   GtkStyle *new_style = NULL;
919   GdkVisual *visual;
920   
921   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
922   g_return_val_if_fail (window != NULL, NULL);
923   
924   visual = gdk_window_get_visual (window);
925   
926   if (!style->styles)
927     style->styles = g_slist_append (NULL, style);
928   
929   styles = style->styles;
930   while (styles)
931     {
932       new_style = styles->data;
933       
934       if (new_style->visual == visual)
935         break;
936
937       new_style = NULL;
938       styles = styles->next;
939     }
940
941   if (!new_style)
942     {
943       styles = style->styles;
944       
945       while (styles)
946         {
947           new_style = styles->data;
948           
949           if (new_style->attach_count == 0)
950             {
951               gtk_style_realize (new_style, visual);
952               break;
953             }
954           
955           new_style = NULL;
956           styles = styles->next;
957         }
958     }
959   
960   if (!new_style)
961     {
962       new_style = gtk_style_duplicate (style);
963       gtk_style_realize (new_style, visual);
964     }
965
966   /* A style gets a refcount from being attached */
967   if (new_style->attach_count == 0)
968     g_object_ref (new_style);
969
970   /* Another refcount belongs to the parent */
971   if (style != new_style) 
972     {
973       g_object_unref (style);
974       g_object_ref (new_style);
975     }
976   
977   new_style->attach_count++;
978   
979   return new_style;
980 }
981
982 /**
983  * gtk_style_detach:
984  * @style: a #GtkStyle
985  *
986  * Detaches a style from a window. If the style is not attached
987  * to any windows anymore, it is unrealized. See gtk_style_attach().
988  *
989  * Deprecated:3.0: Use #GtkStyleContext instead
990  */
991 void
992 gtk_style_detach (GtkStyle *style)
993 {
994   g_return_if_fail (GTK_IS_STYLE (style));
995
996   if (style->attach_count == 0)
997     return;
998
999   style->attach_count -= 1;
1000   if (style->attach_count == 0)
1001     {
1002       g_signal_emit (style, unrealize_signal, 0);
1003       
1004       g_object_unref (style->visual);
1005       style->visual = NULL;
1006
1007       if (style->private_font_desc)
1008         {
1009           pango_font_description_free (style->private_font_desc);
1010           style->private_font_desc = NULL;
1011         }
1012
1013       g_object_unref (style);
1014     }
1015 }
1016
1017 static void
1018 gtk_style_realize (GtkStyle  *style,
1019                    GdkVisual *visual)
1020 {
1021   style->visual = g_object_ref (visual);
1022
1023   g_signal_emit (style, realize_signal, 0);
1024 }
1025
1026 /**
1027  * gtk_style_lookup_icon_set:
1028  * @style: a #GtkStyle
1029  * @stock_id: an icon name
1030  *
1031  * Looks up @stock_id in the icon factories associated with @style
1032  * and the default icon factory, returning an icon set if found,
1033  * otherwise %NULL.
1034  *
1035  * Return value: icon set of @stock_id
1036  *
1037  * Deprecated:3.0: Use gtk_style_context_lookup_icon_set() instead
1038  */
1039 GtkIconSet*
1040 gtk_style_lookup_icon_set (GtkStyle   *style,
1041                            const char *stock_id)
1042 {
1043   GtkStylePrivate *priv;
1044
1045   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1046   g_return_val_if_fail (stock_id != NULL, NULL);
1047
1048   priv = GTK_STYLE_GET_PRIVATE (style);
1049
1050   if (priv->context)
1051     return gtk_style_context_lookup_icon_set (priv->context, stock_id);
1052
1053   return gtk_icon_factory_lookup_default (stock_id);
1054 }
1055
1056 /**
1057  * gtk_style_lookup_color:
1058  * @style: a #GtkStyle
1059  * @color_name: the name of the logical color to look up
1060  * @color: the #GdkColor to fill in
1061  *
1062  * Looks up @color_name in the style's logical color mappings,
1063  * filling in @color and returning %TRUE if found, otherwise
1064  * returning %FALSE. Do not cache the found mapping, because
1065  * it depends on the #GtkStyle and might change when a theme
1066  * switch occurs.
1067  *
1068  * Return value: %TRUE if the mapping was found.
1069  *
1070  * Since: 2.10
1071  *
1072  * Deprecated:3.0: Use gtk_style_context_lookup_color() instead
1073  **/
1074 gboolean
1075 gtk_style_lookup_color (GtkStyle   *style,
1076                         const char *color_name,
1077                         GdkColor   *color)
1078 {
1079   GtkStylePrivate *priv;
1080   gboolean result;
1081   GdkRGBA rgba;
1082
1083   g_return_val_if_fail (GTK_IS_STYLE (style), FALSE);
1084   g_return_val_if_fail (color_name != NULL, FALSE);
1085   g_return_val_if_fail (color != NULL, FALSE);
1086
1087   priv = GTK_STYLE_GET_PRIVATE (style);
1088
1089   if (!priv->context)
1090     return FALSE;
1091
1092   result = gtk_style_context_lookup_color (priv->context, color_name, &rgba);
1093
1094   if (color)
1095     {
1096       color->red = (guint16) (rgba.red * 65535);
1097       color->green = (guint16) (rgba.green * 65535);
1098       color->blue = (guint16) (rgba.blue * 65535);
1099       color->pixel = 0;
1100     }
1101
1102   return result;
1103 }
1104
1105 /**
1106  * gtk_style_set_background:
1107  * @style: a #GtkStyle
1108  * @window: a #GdkWindow
1109  * @state_type: a state
1110  * 
1111  * Sets the background of @window to the background color or pixmap
1112  * specified by @style for the given state.
1113  *
1114  * Deprecated:3.0: Use gtk_style_context_set_background() instead
1115  */
1116 void
1117 gtk_style_set_background (GtkStyle    *style,
1118                           GdkWindow   *window,
1119                           GtkStateType state_type)
1120 {
1121   g_return_if_fail (GTK_IS_STYLE (style));
1122   g_return_if_fail (window != NULL);
1123   
1124   GTK_STYLE_GET_CLASS (style)->set_background (style, window, state_type);
1125 }
1126
1127 /* Default functions */
1128 static GtkStyle *
1129 gtk_style_real_clone (GtkStyle *style)
1130 {
1131   GtkStylePrivate *priv;
1132
1133   priv = GTK_STYLE_GET_PRIVATE (style);
1134
1135   return g_object_new (G_OBJECT_TYPE (style),
1136                        "context", priv->context,
1137                        NULL);
1138 }
1139
1140 static void
1141 gtk_style_real_copy (GtkStyle *style,
1142                      GtkStyle *src)
1143 {
1144   GtkStylePrivate *priv = GTK_STYLE_GET_PRIVATE (style);
1145   GtkStylePrivate *src_priv = GTK_STYLE_GET_PRIVATE (src);
1146   gint i;
1147   
1148   for (i = 0; i < 5; i++)
1149     {
1150       style->fg[i] = src->fg[i];
1151       style->bg[i] = src->bg[i];
1152       style->text[i] = src->text[i];
1153       style->base[i] = src->base[i];
1154
1155       if (style->background[i])
1156         cairo_pattern_destroy (style->background[i]),
1157       style->background[i] = src->background[i];
1158       if (style->background[i])
1159         cairo_pattern_reference (style->background[i]);
1160     }
1161
1162   if (style->font_desc)
1163     pango_font_description_free (style->font_desc);
1164   if (src->font_desc)
1165     style->font_desc = pango_font_description_copy (src->font_desc);
1166   else
1167     style->font_desc = NULL;
1168   
1169   style->xthickness = src->xthickness;
1170   style->ythickness = src->ythickness;
1171
1172   if (style->rc_style)
1173     g_object_unref (style->rc_style);
1174   style->rc_style = src->rc_style;
1175   if (src->rc_style)
1176     g_object_ref (src->rc_style);
1177
1178   g_slist_foreach (style->icon_factories, (GFunc) g_object_unref, NULL);
1179   g_slist_free (style->icon_factories);
1180   style->icon_factories = g_slist_copy (src->icon_factories);
1181   g_slist_foreach (style->icon_factories, (GFunc) g_object_ref, NULL);
1182
1183   g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
1184   g_slist_free (priv->color_hashes);
1185   priv->color_hashes = g_slist_copy (src_priv->color_hashes);
1186   g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_ref, NULL);
1187
1188   /* don't copy, just clear cache */
1189   clear_property_cache (style);
1190 }
1191
1192 static void
1193 gtk_style_real_init_from_rc (GtkStyle   *style,
1194                              GtkRcStyle *rc_style)
1195 {
1196   GtkStylePrivate *priv = GTK_STYLE_GET_PRIVATE (style);
1197   gint i;
1198
1199   /* cache _should_ be still empty */
1200   clear_property_cache (style);
1201
1202   if (rc_style->font_desc)
1203     pango_font_description_merge (style->font_desc, rc_style->font_desc, TRUE);
1204     
1205   for (i = 0; i < 5; i++)
1206     {
1207       if (rc_style->color_flags[i] & GTK_RC_FG)
1208         style->fg[i] = rc_style->fg[i];
1209       if (rc_style->color_flags[i] & GTK_RC_BG)
1210         style->bg[i] = rc_style->bg[i];
1211       if (rc_style->color_flags[i] & GTK_RC_TEXT)
1212         style->text[i] = rc_style->text[i];
1213       if (rc_style->color_flags[i] & GTK_RC_BASE)
1214         style->base[i] = rc_style->base[i];
1215     }
1216
1217   if (rc_style->xthickness >= 0)
1218     style->xthickness = rc_style->xthickness;
1219   if (rc_style->ythickness >= 0)
1220     style->ythickness = rc_style->ythickness;
1221
1222   style->icon_factories = g_slist_copy (rc_style->icon_factories);
1223   g_slist_foreach (style->icon_factories, (GFunc) g_object_ref, NULL);
1224
1225   priv->color_hashes = g_slist_copy (_gtk_rc_style_get_color_hashes (rc_style));
1226   g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_ref, NULL);
1227 }
1228
1229 static gint
1230 style_property_values_cmp (gconstpointer bsearch_node1,
1231                            gconstpointer bsearch_node2)
1232 {
1233   const PropertyValue *val1 = bsearch_node1;
1234   const PropertyValue *val2 = bsearch_node2;
1235
1236   if (val1->widget_type == val2->widget_type)
1237     return val1->pspec < val2->pspec ? -1 : val1->pspec == val2->pspec ? 0 : 1;
1238   else
1239     return val1->widget_type < val2->widget_type ? -1 : 1;
1240 }
1241
1242 /**
1243  * gtk_style_get_style_property:
1244  * @style: a #GtkStyle
1245  * @widget_type: the #GType of a descendant of #GtkWidget
1246  * @property_name: the name of the style property to get
1247  * @value: a #GValue where the value of the property being
1248  *     queried will be stored
1249  *
1250  * Queries the value of a style property corresponding to a
1251  * widget class is in the given style.
1252  *
1253  * Since: 2.16
1254  */
1255 void 
1256 gtk_style_get_style_property (GtkStyle     *style,
1257                               GType        widget_type,
1258                               const gchar *property_name,
1259                               GValue      *value)
1260 {
1261   GtkWidgetClass *klass;
1262   GParamSpec *pspec;
1263   GtkRcPropertyParser parser;
1264   const GValue *peek_value;
1265
1266   klass = g_type_class_ref (widget_type);
1267   pspec = gtk_widget_class_find_style_property (klass, property_name);
1268   g_type_class_unref (klass);
1269
1270   if (!pspec)
1271     {
1272       g_warning ("%s: widget class `%s' has no property named `%s'",
1273                  G_STRLOC,
1274                  g_type_name (widget_type),
1275                  property_name);
1276       return;
1277     }
1278
1279   parser = g_param_spec_get_qdata (pspec,
1280                                    g_quark_from_static_string ("gtk-rc-property-parser"));
1281
1282   peek_value = _gtk_style_peek_property_value (style, widget_type, pspec, parser);
1283
1284   if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
1285     g_value_copy (peek_value, value);
1286   else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
1287     g_value_transform (peek_value, value);
1288   else
1289     g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
1290                pspec->name,
1291                g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1292                G_VALUE_TYPE_NAME (value));
1293 }
1294
1295 /**
1296  * gtk_style_get_valist:
1297  * @style: a #GtkStyle
1298  * @widget_type: the #GType of a descendant of #GtkWidget
1299  * @first_property_name: the name of the first style property to get
1300  * @var_args: a <type>va_list</type> of pairs of property names and
1301  *     locations to return the property values, starting with the
1302  *     location for @first_property_name.
1303  *
1304  * Non-vararg variant of gtk_style_get().
1305  * Used primarily by language bindings.
1306  *
1307  * Since: 2.16
1308  */
1309 void 
1310 gtk_style_get_valist (GtkStyle    *style,
1311                       GType        widget_type,
1312                       const gchar *first_property_name,
1313                       va_list      var_args)
1314 {
1315   const char *property_name;
1316   GtkWidgetClass *klass;
1317
1318   g_return_if_fail (GTK_IS_STYLE (style));
1319
1320   klass = g_type_class_ref (widget_type);
1321
1322   property_name = first_property_name;
1323   while (property_name)
1324     {
1325       GParamSpec *pspec;
1326       GtkRcPropertyParser parser;
1327       const GValue *peek_value;
1328       gchar *error;
1329
1330       pspec = gtk_widget_class_find_style_property (klass, property_name);
1331
1332       if (!pspec)
1333         {
1334           g_warning ("%s: widget class `%s' has no property named `%s'",
1335                      G_STRLOC,
1336                      g_type_name (widget_type),
1337                      property_name);
1338           break;
1339         }
1340
1341       parser = g_param_spec_get_qdata (pspec,
1342                                        g_quark_from_static_string ("gtk-rc-property-parser"));
1343
1344       peek_value = _gtk_style_peek_property_value (style, widget_type, pspec, parser);
1345       G_VALUE_LCOPY (peek_value, var_args, 0, &error);
1346       if (error)
1347         {
1348           g_warning ("%s: %s", G_STRLOC, error);
1349           g_free (error);
1350           break;
1351         }
1352
1353       property_name = va_arg (var_args, gchar*);
1354     }
1355
1356   g_type_class_unref (klass);
1357 }
1358
1359 /**
1360  * gtk_style_get:
1361  * @style: a #GtkStyle
1362  * @widget_type: the #GType of a descendant of #GtkWidget
1363  * @first_property_name: the name of the first style property to get
1364  * @Varargs: pairs of property names and locations to
1365  *   return the property values, starting with the location for
1366  *   @first_property_name, terminated by %NULL.
1367  *
1368  * Gets the values of a multiple style properties for @widget_type
1369  * from @style.
1370  *
1371  * Since: 2.16
1372  */
1373 void
1374 gtk_style_get (GtkStyle    *style,
1375                GType        widget_type,
1376                const gchar *first_property_name,
1377                ...)
1378 {
1379   va_list var_args;
1380
1381   va_start (var_args, first_property_name);
1382   gtk_style_get_valist (style, widget_type, first_property_name, var_args);
1383   va_end (var_args);
1384 }
1385
1386 const GValue*
1387 _gtk_style_peek_property_value (GtkStyle           *style,
1388                                 GType               widget_type,
1389                                 GParamSpec         *pspec,
1390                                 GtkRcPropertyParser parser)
1391 {
1392   PropertyValue *pcache, key = { 0, NULL, { 0, } };
1393   const GtkRcProperty *rcprop = NULL;
1394   guint i;
1395
1396   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1397   g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
1398   g_return_val_if_fail (g_type_is_a (pspec->owner_type, GTK_TYPE_WIDGET), NULL);
1399   g_return_val_if_fail (g_type_is_a (widget_type, pspec->owner_type), NULL);
1400
1401   key.widget_type = widget_type;
1402   key.pspec = pspec;
1403
1404   /* need value cache array */
1405   if (!style->property_cache)
1406     style->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
1407   else
1408     {
1409       pcache = bsearch (&key,
1410                         style->property_cache->data, style->property_cache->len,
1411                         sizeof (PropertyValue), style_property_values_cmp);
1412       if (pcache)
1413         return &pcache->value;
1414     }
1415
1416   i = 0;
1417   while (i < style->property_cache->len &&
1418          style_property_values_cmp (&key, &g_array_index (style->property_cache, PropertyValue, i)) >= 0)
1419     i++;
1420
1421   g_array_insert_val (style->property_cache, i, key);
1422   pcache = &g_array_index (style->property_cache, PropertyValue, i);
1423
1424   /* cache miss, initialize value type, then set contents */
1425   g_param_spec_ref (pcache->pspec);
1426   g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));
1427
1428   /* value provided by rc style? */
1429   if (style->rc_style)
1430     {
1431       GQuark prop_quark = g_quark_from_string (pspec->name);
1432
1433       do
1434         {
1435           rcprop = _gtk_rc_style_lookup_rc_property (style->rc_style,
1436                                                      g_type_qname (widget_type),
1437                                                      prop_quark);
1438           if (rcprop)
1439             break;
1440           widget_type = g_type_parent (widget_type);
1441         }
1442       while (g_type_is_a (widget_type, pspec->owner_type));
1443     }
1444
1445   /* when supplied by rc style, we need to convert */
1446   if (rcprop && !_gtk_settings_parse_convert (parser, &rcprop->value,
1447                                               pspec, &pcache->value))
1448     {
1449       gchar *contents = g_strdup_value_contents (&rcprop->value);
1450       
1451       g_message ("%s: failed to retrieve property `%s::%s' of type `%s' from rc file value \"%s\" of type `%s'",
1452                  rcprop->origin ? rcprop->origin : "(for origin information, set GTK_DEBUG)",
1453                  g_type_name (pspec->owner_type), pspec->name,
1454                  g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
1455                  contents,
1456                  G_VALUE_TYPE_NAME (&rcprop->value));
1457       g_free (contents);
1458       rcprop = NULL; /* needs default */
1459     }
1460   
1461   /* not supplied by rc style (or conversion failed), revert to default */
1462   if (!rcprop)
1463     g_param_value_set_default (pspec, &pcache->value);
1464
1465   return &pcache->value;
1466 }
1467
1468 static cairo_pattern_t *
1469 load_background (GdkVisual   *visual,
1470                  GdkColor    *bg_color,
1471                  const gchar *filename)
1472 {
1473   if (filename == NULL)
1474     {
1475       return cairo_pattern_create_rgb (bg_color->red   / 65535.0,
1476                                        bg_color->green / 65535.0,
1477                                        bg_color->blue  / 65535.0);
1478     }
1479   if (strcmp (filename, "<parent>") == 0)
1480     return NULL;
1481   else
1482     {
1483       GdkPixbuf *pixbuf;
1484       cairo_surface_t *surface;
1485       cairo_pattern_t *pattern;
1486       cairo_t *cr;
1487       GdkScreen *screen = gdk_visual_get_screen (visual);
1488   
1489       pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
1490       if (!pixbuf)
1491         return NULL;
1492
1493       surface = gdk_window_create_similar_surface (gdk_screen_get_root_window (screen),
1494                                                    CAIRO_CONTENT_COLOR,
1495                                                    gdk_pixbuf_get_width (pixbuf),
1496                                                    gdk_pixbuf_get_height (pixbuf));
1497   
1498       cr = cairo_create (surface);
1499
1500       gdk_cairo_set_source_color (cr, bg_color);
1501       cairo_paint (cr);
1502
1503       gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
1504       cairo_paint (cr);
1505
1506       cairo_destroy (cr);
1507       g_object_unref (pixbuf);
1508
1509       pattern = cairo_pattern_create_for_surface (surface);
1510
1511       cairo_surface_destroy (surface);
1512
1513       return pattern;
1514     }
1515 }
1516
1517 static void
1518 gtk_style_real_realize (GtkStyle *style)
1519 {
1520   gint i;
1521
1522   for (i = 0; i < 5; i++)
1523     {
1524       _gtk_style_shade (&style->bg[i], &style->light[i], LIGHTNESS_MULT);
1525       _gtk_style_shade (&style->bg[i], &style->dark[i], DARKNESS_MULT);
1526
1527       style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
1528       style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
1529       style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
1530
1531       style->text_aa[i].red = (style->text[i].red + style->base[i].red) / 2;
1532       style->text_aa[i].green = (style->text[i].green + style->base[i].green) / 2;
1533       style->text_aa[i].blue = (style->text[i].blue + style->base[i].blue) / 2;
1534     }
1535
1536   style->black.red = 0x0000;
1537   style->black.green = 0x0000;
1538   style->black.blue = 0x0000;
1539
1540   style->white.red = 0xffff;
1541   style->white.green = 0xffff;
1542   style->white.blue = 0xffff;
1543
1544   for (i = 0; i < 5; i++)
1545     {
1546       const char *image_name;
1547
1548       if (style->rc_style)
1549         image_name = style->rc_style->bg_pixmap_name[i];
1550       else
1551         image_name = NULL;
1552
1553       style->background[i] = load_background (style->visual,
1554                                               &style->bg[i],
1555                                               image_name);
1556     }
1557 }
1558
1559 static void
1560 gtk_style_real_unrealize (GtkStyle *style)
1561 {
1562   int i;
1563
1564   for (i = 0; i < 5; i++)
1565     {
1566       if (style->background[i])
1567         {
1568           cairo_pattern_destroy (style->background[i]);
1569           style->background[i] = NULL;
1570         }
1571       
1572     }
1573   
1574   style_unrealize_cursors (style);
1575 }
1576
1577 static void
1578 gtk_style_real_set_background (GtkStyle    *style,
1579                                GdkWindow   *window,
1580                                GtkStateType state_type)
1581 {
1582   gdk_window_set_background_pattern (window, style->background[state_type]);
1583 }
1584
1585 /**
1586  * gtk_style_render_icon:
1587  * @style: a #GtkStyle
1588  * @source: the #GtkIconSource specifying the icon to render
1589  * @direction: a text direction
1590  * @state: a state
1591  * @size: (type int): the size to render the icon at. A size of
1592  *     (GtkIconSize)-1 means render at the size of the source and
1593  *     don't scale.
1594  * @widget: (allow-none): the widget
1595  * @detail: (allow-none): a style detail
1596  *
1597  * Renders the icon specified by @source at the given @size
1598  * according to the given parameters and returns the result in a
1599  * pixbuf.
1600  *
1601  * Return value: (transfer full): a newly-created #GdkPixbuf
1602  *     containing the rendered icon
1603  *
1604  * Deprecated:3.0: Use gtk_render_icon_pixbuf() instead
1605  */
1606 GdkPixbuf *
1607 gtk_style_render_icon (GtkStyle            *style,
1608                        const GtkIconSource *source,
1609                        GtkTextDirection     direction,
1610                        GtkStateType         state,
1611                        GtkIconSize          size,
1612                        GtkWidget           *widget,
1613                        const gchar         *detail)
1614 {
1615   GdkPixbuf *pixbuf;
1616   
1617   g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
1618   g_return_val_if_fail (GTK_STYLE_GET_CLASS (style)->render_icon != NULL, NULL);
1619   
1620   pixbuf = GTK_STYLE_GET_CLASS (style)->render_icon (style, source, direction, state,
1621                                                      size, widget, detail);
1622
1623   g_return_val_if_fail (pixbuf != NULL, NULL);
1624
1625   return pixbuf;
1626 }
1627
1628 /* Default functions */
1629
1630 /**
1631  * gtk_style_apply_default_background:
1632  * @style:
1633  * @cr: 
1634  * @window:
1635  * @set_bg:
1636  * @state_type:
1637  * @area: (allow-none):
1638  * @x:
1639  * @y:
1640  * @width:
1641  * @height:
1642  *
1643  * Deprecated:3.0: Use #GtkStyleContext instead
1644  */
1645 void
1646 gtk_style_apply_default_background (GtkStyle          *style,
1647                                     cairo_t           *cr,
1648                                     GdkWindow         *window,
1649                                     GtkStateType       state_type,
1650                                     gint               x,
1651                                     gint               y,
1652                                     gint               width,
1653                                     gint               height)
1654 {
1655   cairo_save (cr);
1656
1657   if (style->background[state_type] == NULL)
1658     {
1659       GdkWindow *parent = gdk_window_get_parent (window);
1660       int x_offset, y_offset;
1661
1662       if (parent)
1663         {
1664           gdk_window_get_position (window, &x_offset, &y_offset);
1665           cairo_translate (cr, -x_offset, -y_offset);
1666           gtk_style_apply_default_background (style, cr,
1667                                               parent, state_type,
1668                                               x + x_offset, y + y_offset,
1669                                               width, height);
1670           goto out;
1671         }
1672       else
1673         gdk_cairo_set_source_color (cr, &style->bg[state_type]);
1674     }
1675   else
1676     cairo_set_source (cr, style->background[state_type]);
1677
1678   cairo_rectangle (cr, x, y, width, height);
1679   cairo_fill (cr);
1680
1681 out:
1682   cairo_restore (cr);
1683 }
1684
1685 static GdkPixbuf *
1686 gtk_default_render_icon (GtkStyle            *style,
1687                          const GtkIconSource *source,
1688                          GtkTextDirection     direction,
1689                          GtkStateType         state,
1690                          GtkIconSize          size,
1691                          GtkWidget           *widget,
1692                          const gchar         *detail)
1693 {
1694   GtkStyleContext *context;
1695   GtkStylePrivate *priv;
1696   GtkStateFlags flags = 0;
1697   GdkPixbuf *pixbuf;
1698
1699   if (widget)
1700     context = gtk_widget_get_style_context (widget);
1701   else
1702     {
1703       priv = GTK_STYLE_GET_PRIVATE (style);
1704       context = priv->context;
1705     }
1706
1707   if (!context)
1708     return NULL;
1709
1710   gtk_style_context_save (context);
1711
1712   if (detail)
1713     transform_detail_string (detail, context);
1714
1715   switch (state)
1716     {
1717     case GTK_STATE_PRELIGHT:
1718       flags |= GTK_STATE_FLAG_PRELIGHT;
1719       break;
1720     case GTK_STATE_INSENSITIVE:
1721       flags |= GTK_STATE_FLAG_INSENSITIVE;
1722       break;
1723     default:
1724       break;
1725     }
1726
1727   gtk_style_context_set_state (context, flags);
1728
1729   pixbuf = gtk_render_icon_pixbuf (context, source, size);
1730
1731   gtk_style_context_restore (context);
1732
1733   return pixbuf;
1734 }
1735
1736 static void
1737 _cairo_draw_line (cairo_t  *cr,
1738                   GdkColor *color,
1739                   gint      x1,
1740                   gint      y1,
1741                   gint      x2,
1742                   gint      y2)
1743 {
1744   cairo_save (cr);
1745
1746   gdk_cairo_set_source_color (cr, color);
1747   cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
1748
1749   cairo_move_to (cr, x1 + 0.5, y1 + 0.5);
1750   cairo_line_to (cr, x2 + 0.5, y2 + 0.5);
1751   cairo_stroke (cr);
1752
1753   cairo_restore (cr);
1754 }
1755
1756 static void
1757 _cairo_draw_rectangle (cairo_t *cr,
1758                        GdkColor *color,
1759                        gboolean filled,
1760                        gint x,
1761                        gint y,
1762                        gint width,
1763                        gint height)
1764 {
1765   gdk_cairo_set_source_color (cr, color);
1766
1767   if (filled)
1768     {
1769       cairo_rectangle (cr, x, y, width, height);
1770       cairo_fill (cr);
1771     }
1772   else
1773     {
1774       cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
1775       cairo_stroke (cr);
1776     }
1777 }
1778
1779 static void
1780 _cairo_draw_point (cairo_t *cr,
1781                    GdkColor *color,
1782                    gint x,
1783                    gint y)
1784 {
1785   gdk_cairo_set_source_color (cr, color);
1786   cairo_rectangle (cr, x, y, 1, 1);
1787   cairo_fill (cr);
1788 }
1789
1790 static void
1791 transform_detail_string (const gchar     *detail,
1792                          GtkStyleContext *context)
1793 {
1794   if (!detail)
1795     return;
1796
1797   if (strcmp (detail, "arrow") == 0)
1798     gtk_style_context_add_class (context, "arrow");
1799   else if (strcmp (detail, "button") == 0)
1800     gtk_style_context_add_class (context, "button");
1801   else if (strcmp (detail, "buttondefault") == 0)
1802     {
1803       gtk_style_context_add_class (context, "button");
1804       gtk_style_context_add_class (context, "default");
1805     }
1806   else if (strcmp (detail, "calendar") == 0)
1807     gtk_style_context_add_class (context, "calendar");
1808   else if (strcmp (detail, "cellcheck") == 0)
1809     {
1810       gtk_style_context_add_class (context, "cell");
1811       gtk_style_context_add_class (context, "check");
1812     }
1813   else if (strcmp (detail, "cellradio") == 0)
1814     {
1815       gtk_style_context_add_class (context, "cell");
1816       gtk_style_context_add_class (context, "radio");
1817     }
1818   else if (strcmp (detail, "checkbutton") == 0)
1819     gtk_style_context_add_class (context, "check");
1820   else if (strcmp (detail, "check") == 0)
1821     {
1822       gtk_style_context_add_class (context, "check");
1823       gtk_style_context_add_class (context, "menu");
1824     }
1825   else if (strcmp (detail, "radiobutton") == 0)
1826     {
1827       gtk_style_context_add_class (context, "radio");
1828     }
1829   else if (strcmp (detail, "option") == 0)
1830     {
1831       gtk_style_context_add_class (context, "radio");
1832       gtk_style_context_add_class (context, "menu");
1833     }
1834   else if (strcmp (detail, "entry") == 0 ||
1835            strcmp (detail, "entry_bg") == 0)
1836     gtk_style_context_add_class (context, "entry");
1837   else if (strcmp (detail, "expander") == 0)
1838     gtk_style_context_add_class (context, "expander");
1839   else if (strcmp (detail, "tooltip") == 0)
1840     gtk_style_context_add_class (context, "tooltip");
1841   else if (strcmp (detail, "frame") == 0)
1842     gtk_style_context_add_class (context, "frame");
1843   else if (strcmp (detail, "scrolled_window") == 0)
1844     gtk_style_context_add_class (context, "scrolled-window");
1845   else if (strcmp (detail, "viewport") == 0 ||
1846            strcmp (detail, "viewportbin") == 0)
1847     gtk_style_context_add_class (context, "viewport");
1848   else if (strncmp (detail, "trough", 6) == 0)
1849     gtk_style_context_add_class (context, "trough");
1850   else if (strcmp (detail, "spinbutton") == 0)
1851     gtk_style_context_add_class (context, "spinbutton");
1852   else if (strcmp (detail, "spinbutton_up") == 0)
1853     {
1854       gtk_style_context_add_class (context, "spinbutton");
1855       gtk_style_context_add_class (context, "button");
1856       gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
1857     }
1858   else if (strcmp (detail, "spinbutton_down") == 0)
1859     {
1860       gtk_style_context_add_class (context, "spinbutton");
1861       gtk_style_context_add_class (context, "button");
1862       gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
1863     }
1864   else if ((detail[0] == 'h' || detail[0] == 'v') &&
1865            strncmp (&detail[1], "scrollbar_", 10) == 0)
1866     {
1867       gtk_style_context_add_class (context, "button");
1868       gtk_style_context_add_class (context, "scrollbar");
1869     }
1870   else if (strcmp (detail, "slider") == 0)
1871     {
1872       gtk_style_context_add_class (context, "slider");
1873       gtk_style_context_add_class (context, "scrollbar");
1874     }
1875   else if (strcmp (detail, "vscale") == 0 ||
1876            strcmp (detail, "hscale") == 0)
1877     {
1878       gtk_style_context_add_class (context, "slider");
1879       gtk_style_context_add_class (context, "scale");
1880     }
1881   else if (strcmp (detail, "menuitem") == 0)
1882     {
1883       gtk_style_context_add_class (context, "menuitem");
1884       gtk_style_context_add_class (context, "menu");
1885     }
1886   else if (strcmp (detail, "menu") == 0)
1887     {
1888       gtk_style_context_add_class (context, "popup");
1889       gtk_style_context_add_class (context, "menu");
1890     }
1891   else if (strcmp (detail, "accellabel") == 0)
1892     gtk_style_context_add_class (context, "accelerator");
1893   else if (strcmp (detail, "menubar") == 0)
1894     gtk_style_context_add_class (context, "menubar");
1895   else if (strcmp (detail, "base") == 0)
1896     gtk_style_context_add_class (context, "background");
1897   else if (strcmp (detail, "bar") == 0 ||
1898            strcmp (detail, "progressbar") == 0)
1899     gtk_style_context_add_class (context, "progressbar");
1900   else if (strcmp (detail, "toolbar") == 0)
1901     gtk_style_context_add_class (context, "toolbar");
1902   else if (strcmp (detail, "handlebox_bin") == 0)
1903     gtk_style_context_add_class (context, "dock");
1904   else if (strcmp (detail, "notebook") == 0)
1905     gtk_style_context_add_class (context, "notebook");
1906   else if (strcmp (detail, "tab") == 0)
1907     {
1908       gtk_style_context_add_class (context, "notebook");
1909       gtk_style_context_add_region (context, GTK_STYLE_REGION_TAB, 0);
1910     }
1911   else if (g_str_has_prefix (detail, "cell"))
1912     {
1913       GtkRegionFlags row, col;
1914       gboolean ruled = FALSE;
1915       GStrv tokens;
1916       guint i;
1917
1918       tokens = g_strsplit (detail, "_", -1);
1919       row = col = 0;
1920       i = 0;
1921
1922       while (tokens[i])
1923         {
1924           if (strcmp (tokens[i], "even") == 0)
1925             row |= GTK_REGION_EVEN;
1926           else if (strcmp (tokens[i], "odd") == 0)
1927             row |= GTK_REGION_ODD;
1928           else if (strcmp (tokens[i], "start") == 0)
1929             col |= GTK_REGION_FIRST;
1930           else if (strcmp (tokens[i], "end") == 0)
1931             col |= GTK_REGION_LAST;
1932           else if (strcmp (tokens[i], "ruled") == 0)
1933             ruled = TRUE;
1934           else if (strcmp (tokens[i], "sorted") == 0)
1935             col |= GTK_REGION_SORTED;
1936
1937           i++;
1938         }
1939
1940       if (!ruled)
1941         row &= ~(GTK_REGION_EVEN | GTK_REGION_ODD);
1942
1943       gtk_style_context_add_class (context, "cell");
1944       gtk_style_context_add_region (context, "row", row);
1945       gtk_style_context_add_region (context, "column", col);
1946
1947       g_strfreev (tokens);
1948     }
1949 }
1950
1951 static void
1952 gtk_default_draw_hline (GtkStyle     *style,
1953                         cairo_t       *cr,
1954                         GtkStateType  state_type,
1955                         GtkWidget     *widget,
1956                         const gchar   *detail,
1957                         gint          x1,
1958                         gint          x2,
1959                         gint          y)
1960 {
1961   GtkStyleContext *context;
1962   GtkStylePrivate *priv;
1963
1964   if (widget)
1965     context = gtk_widget_get_style_context (widget);
1966   else
1967     {
1968       priv = GTK_STYLE_GET_PRIVATE (style);
1969       context = priv->context;
1970     }
1971
1972   gtk_style_context_save (context);
1973
1974   if (detail)
1975     transform_detail_string (detail, context);
1976
1977   cairo_save (cr);
1978
1979   gtk_render_line (context, cr,
1980                    x1, y, x2, y);
1981
1982   cairo_restore (cr);
1983
1984   gtk_style_context_restore (context);
1985 }
1986
1987
1988 static void
1989 gtk_default_draw_vline (GtkStyle      *style,
1990                         cairo_t       *cr,
1991                         GtkStateType  state_type,
1992                         GtkWidget     *widget,
1993                         const gchar   *detail,
1994                         gint          y1,
1995                         gint          y2,
1996                         gint          x)
1997 {
1998   GtkStyleContext *context;
1999   GtkStylePrivate *priv;
2000
2001   if (widget)
2002     context = gtk_widget_get_style_context (widget);
2003   else
2004     {
2005       priv = GTK_STYLE_GET_PRIVATE (style);
2006       context = priv->context;
2007     }
2008
2009   gtk_style_context_save (context);
2010
2011   if (detail)
2012     transform_detail_string (detail, context);
2013
2014   cairo_save (cr);
2015
2016   gtk_render_line (context, cr,
2017                    x, y1, x, y2);
2018
2019   cairo_restore (cr);
2020   gtk_style_context_restore (context);
2021 }
2022
2023 static void
2024 draw_thin_shadow (GtkStyle      *style,
2025                   cairo_t       *cr,
2026                   GtkStateType   state,
2027                   gint           x,
2028                   gint           y,
2029                   gint           width,
2030                   gint           height)
2031 {
2032   GdkColor *gc1, *gc2;
2033
2034   gc1 = &style->light[state];
2035   gc2 = &style->dark[state];
2036
2037   _cairo_draw_line (cr, gc1,
2038                     x, y + height - 1, x + width - 1, y + height - 1);
2039   _cairo_draw_line (cr, gc1,
2040                     x + width - 1, y,  x + width - 1, y + height - 1);
2041       
2042   _cairo_draw_line (cr, gc2,
2043                     x, y, x + width - 2, y);
2044   _cairo_draw_line (cr, gc2,
2045                     x, y, x, y + height - 2);
2046 }
2047
2048 static void
2049 draw_spinbutton_shadow (GtkStyle        *style,
2050                         cairo_t         *cr,
2051                         GtkStateType     state,
2052                         GtkTextDirection direction,
2053                         gint             x,
2054                         gint             y,
2055                         gint             width,
2056                         gint             height)
2057 {
2058
2059   if (direction == GTK_TEXT_DIR_LTR)
2060     {
2061       _cairo_draw_line (cr, &style->dark[state],
2062                         x, y, x + width - 1, y);
2063       _cairo_draw_line (cr, &style->black,
2064                         x, y + 1, x + width - 2, y + 1);
2065       _cairo_draw_line (cr, &style->black,
2066                         x + width - 2, y + 2, x + width - 2, y + height - 3);
2067       _cairo_draw_line (cr, &style->light[state],
2068                         x + width - 1, y + 1, x + width - 1, y + height - 2);
2069       _cairo_draw_line (cr, &style->light[state],
2070                         x, y + height - 1, x + width - 1, y + height - 1);
2071       _cairo_draw_line (cr, &style->bg[state],
2072                         x, y + height - 2, x + width - 2, y + height - 2);
2073       _cairo_draw_line (cr, &style->black,
2074                         x, y + 2, x, y + height - 3);
2075     }
2076   else
2077     {
2078       _cairo_draw_line (cr, &style->dark[state],
2079                         x, y, x + width - 1, y);
2080       _cairo_draw_line (cr, &style->dark[state],
2081                         x, y + 1, x, y + height - 1);
2082       _cairo_draw_line (cr, &style->black,
2083                         x + 1, y + 1, x + width - 1, y + 1);
2084       _cairo_draw_line (cr, &style->black,
2085                         x + 1, y + 2, x + 1, y + height - 2);
2086       _cairo_draw_line (cr, &style->black,
2087                         x + width - 1, y + 2, x + width - 1, y + height - 3);
2088       _cairo_draw_line (cr, &style->light[state],
2089                         x + 1, y + height - 1, x + width - 1, y + height - 1);
2090       _cairo_draw_line (cr, &style->bg[state],
2091                         x + 2, y + height - 2, x + width - 1, y + height - 2);
2092     }
2093 }
2094
2095 static void
2096 draw_menu_shadow (GtkStyle        *style,
2097                   cairo_t         *cr,
2098                   GtkStateType     state,
2099                   gint             x,
2100                   gint             y,
2101                   gint             width,
2102                   gint             height)
2103 {
2104   if (style->ythickness > 0)
2105     {
2106       if (style->ythickness > 1)
2107         {
2108           _cairo_draw_line (cr, &style->dark[state],
2109                             x + 1, y + height - 2,
2110                             x + width - 2, y + height - 2);
2111           _cairo_draw_line (cr, &style->black,
2112                             x, y + height - 1, x + width - 1, y + height - 1);
2113         }
2114       else
2115         {
2116           _cairo_draw_line (cr, &style->dark[state],
2117                             x + 1, y + height - 1, x + width - 1, y + height - 1);
2118         }
2119     }
2120   
2121   if (style->xthickness > 0)
2122     {
2123       if (style->xthickness > 1)
2124         {
2125           _cairo_draw_line (cr, &style->dark[state],
2126                             x + width - 2, y + 1,
2127                             x + width - 2, y + height - 2);
2128
2129           _cairo_draw_line (cr, &style->black,
2130                             x + width - 1, y, x + width - 1, y + height - 1);
2131         }
2132       else
2133         {
2134           _cairo_draw_line (cr, &style->dark[state],
2135                             x + width - 1, y + 1, x + width - 1, y + height - 1);
2136         }
2137     }
2138   
2139   /* Light around top and left */
2140   
2141   if (style->ythickness > 0)
2142     _cairo_draw_line (cr, &style->black,
2143                    x, y, x + width - 2, y);
2144   if (style->xthickness > 0)
2145     _cairo_draw_line (cr, &style->black,
2146                       x, y, x, y + height - 2);
2147   
2148   if (style->ythickness > 1)
2149     _cairo_draw_line (cr, &style->light[state],
2150                       x + 1, y + 1, x + width - 3, y + 1);
2151   if (style->xthickness > 1)
2152     _cairo_draw_line (cr, &style->light[state],
2153                       x + 1, y + 1, x + 1, y + height - 3);
2154 }
2155
2156 static GtkTextDirection
2157 get_direction (GtkWidget *widget)
2158 {
2159   GtkTextDirection dir;
2160   
2161   if (widget)
2162     dir = gtk_widget_get_direction (widget);
2163   else
2164     dir = GTK_TEXT_DIR_LTR;
2165   
2166   return dir;
2167 }
2168
2169
2170 static void
2171 gtk_default_draw_shadow (GtkStyle      *style,
2172                          cairo_t       *cr,
2173                          GtkStateType   state_type,
2174                          GtkShadowType  shadow_type,
2175                          GtkWidget     *widget,
2176                          const gchar   *detail,
2177                          gint           x,
2178                          gint           y,
2179                          gint           width,
2180                          gint           height)
2181 {
2182   GtkStyleContext *context;
2183   GtkStylePrivate *priv;
2184
2185   if (widget)
2186     context = gtk_widget_get_style_context (widget);
2187   else
2188     {
2189       priv = GTK_STYLE_GET_PRIVATE (style);
2190       context = priv->context;
2191     }
2192
2193   gtk_style_context_save (context);
2194
2195   if (detail)
2196     transform_detail_string (detail, context);
2197
2198   cairo_save (cr);
2199
2200   gtk_render_frame (context, cr,
2201                     (gdouble) x,
2202                     (gdouble) y,
2203                     (gdouble) width,
2204                     (gdouble) height);
2205
2206   cairo_restore (cr);
2207   gtk_style_context_restore (context);
2208 }
2209
2210 static void
2211 draw_arrow (cairo_t       *cr,
2212             GdkColor      *color,
2213             GtkArrowType   arrow_type,
2214             gint           x,
2215             gint           y,
2216             gint           width,
2217             gint           height)
2218 {
2219   gdk_cairo_set_source_color (cr, color);
2220   cairo_save (cr);
2221     
2222   if (arrow_type == GTK_ARROW_DOWN)
2223     {
2224       cairo_move_to (cr, x,              y);
2225       cairo_line_to (cr, x + width,      y);
2226       cairo_line_to (cr, x + width / 2., y + height);
2227     }
2228   else if (arrow_type == GTK_ARROW_UP)
2229     {
2230       cairo_move_to (cr, x,              y + height);
2231       cairo_line_to (cr, x + width / 2., y);
2232       cairo_line_to (cr, x + width,      y + height);
2233     }
2234   else if (arrow_type == GTK_ARROW_LEFT)
2235     {
2236       cairo_move_to (cr, x + width,      y);
2237       cairo_line_to (cr, x + width,      y + height);
2238       cairo_line_to (cr, x,              y + height / 2.);
2239     }
2240   else if (arrow_type == GTK_ARROW_RIGHT)
2241     {
2242       cairo_move_to (cr, x,              y);
2243       cairo_line_to (cr, x + width,      y + height / 2.);
2244       cairo_line_to (cr, x,              y + height);
2245     }
2246
2247   cairo_close_path (cr);
2248   cairo_fill (cr);
2249
2250   cairo_restore (cr);
2251 }
2252
2253 static void
2254 calculate_arrow_geometry (GtkArrowType  arrow_type,
2255                           gint         *x,
2256                           gint         *y,
2257                           gint         *width,
2258                           gint         *height)
2259 {
2260   gint w = *width;
2261   gint h = *height;
2262   
2263   switch (arrow_type)
2264     {
2265     case GTK_ARROW_UP:
2266     case GTK_ARROW_DOWN:
2267       w += (w % 2) - 1;
2268       h = (w / 2 + 1);
2269       
2270       if (h > *height)
2271         {
2272           h = *height;
2273           w = 2 * h - 1;
2274         }
2275       
2276       if (arrow_type == GTK_ARROW_DOWN)
2277         {
2278           if (*height % 2 == 1 || h % 2 == 0)
2279             *height += 1;
2280         }
2281       else
2282         {
2283           if (*height % 2 == 0 || h % 2 == 0)
2284             *height -= 1;
2285         }
2286       break;
2287
2288     case GTK_ARROW_RIGHT:
2289     case GTK_ARROW_LEFT:
2290       h += (h % 2) - 1;
2291       w = (h / 2 + 1);
2292       
2293       if (w > *width)
2294         {
2295           w = *width;
2296           h = 2 * w - 1;
2297         }
2298       
2299       if (arrow_type == GTK_ARROW_RIGHT)
2300         {
2301           if (*width % 2 == 1 || w % 2 == 0)
2302             *width += 1;
2303         }
2304       else
2305         {
2306           if (*width % 2 == 0 || w % 2 == 0)
2307             *width -= 1;
2308         }
2309       break;
2310       
2311     default:
2312       /* should not be reached */
2313       break;
2314     }
2315
2316   *x += (*width - w) / 2;
2317   *y += (*height - h) / 2;
2318   *height = h;
2319   *width = w;
2320 }
2321
2322 static void
2323 gtk_default_draw_arrow (GtkStyle      *style,
2324                         cairo_t       *cr,
2325                         GtkStateType   state,
2326                         GtkShadowType  shadow,
2327                         GtkWidget     *widget,
2328                         const gchar   *detail,
2329                         GtkArrowType   arrow_type,
2330                         gboolean       fill,
2331                         gint           x,
2332                         gint           y,
2333                         gint           width,
2334                         gint           height)
2335 {
2336   GtkStyleContext *context;
2337   GtkStylePrivate *priv;
2338   GtkStateFlags flags = 0;
2339   gdouble angle, size;
2340
2341   if (arrow_type == GTK_ARROW_NONE)
2342     return;
2343
2344   if (widget)
2345     context = gtk_widget_get_style_context (widget);
2346   else
2347     {
2348       priv = GTK_STYLE_GET_PRIVATE (style);
2349       context = priv->context;
2350     }
2351
2352   gtk_style_context_save (context);
2353
2354   if (detail)
2355     transform_detail_string (detail, context);
2356
2357   switch (arrow_type)
2358     {
2359     case GTK_ARROW_UP:
2360       angle = 0;
2361       size = width;
2362       break;
2363     case GTK_ARROW_RIGHT:
2364       angle = G_PI / 2;
2365       size = height;
2366       break;
2367     case GTK_ARROW_DOWN:
2368       angle = G_PI;
2369       size = width;
2370       break;
2371     case GTK_ARROW_LEFT:
2372       angle = 3 * (G_PI / 2);
2373       size = height;
2374       break;
2375     default:
2376       g_assert_not_reached ();
2377     }
2378
2379   switch (state)
2380     {
2381     case GTK_STATE_PRELIGHT:
2382       flags |= GTK_STATE_FLAG_PRELIGHT;
2383       break;
2384     case GTK_STATE_SELECTED:
2385       flags |= GTK_STATE_FLAG_SELECTED;
2386       break;
2387     case GTK_STATE_INSENSITIVE:
2388       flags |= GTK_STATE_FLAG_INSENSITIVE;
2389       break;
2390     case GTK_STATE_ACTIVE:
2391       flags |= GTK_STATE_FLAG_ACTIVE;
2392       break;
2393     default:
2394       break;
2395     }
2396
2397   gtk_style_context_set_state (context, flags);
2398
2399   cairo_save (cr);
2400
2401   gtk_render_arrow (context,
2402                     cr, angle,
2403                     (gdouble) x,
2404                     (gdouble) y,
2405                     size);
2406
2407   cairo_restore (cr);
2408   gtk_style_context_restore (context);
2409 }
2410
2411 static void
2412 gtk_default_draw_diamond (GtkStyle      *style,
2413                           cairo_t       *cr,
2414                           GtkStateType   state_type,
2415                           GtkShadowType  shadow_type,
2416                           GtkWidget     *widget,
2417                           const gchar   *detail,
2418                           gint           x,
2419                           gint           y,
2420                           gint           width,
2421                           gint           height)
2422 {
2423   gint half_width;
2424   gint half_height;
2425   GdkColor *outer_nw = NULL;
2426   GdkColor *outer_ne = NULL;
2427   GdkColor *outer_sw = NULL;
2428   GdkColor *outer_se = NULL;
2429   GdkColor *middle_nw = NULL;
2430   GdkColor *middle_ne = NULL;
2431   GdkColor *middle_sw = NULL;
2432   GdkColor *middle_se = NULL;
2433   GdkColor *inner_nw = NULL;
2434   GdkColor *inner_ne = NULL;
2435   GdkColor *inner_sw = NULL;
2436   GdkColor *inner_se = NULL;
2437   
2438   half_width = width / 2;
2439   half_height = height / 2;
2440   
2441   switch (shadow_type)
2442     {
2443     case GTK_SHADOW_IN:
2444       inner_sw = inner_se = &style->bg[state_type];
2445       middle_sw = middle_se = &style->light[state_type];
2446       outer_sw = outer_se = &style->light[state_type];
2447       inner_nw = inner_ne = &style->black;
2448       middle_nw = middle_ne = &style->dark[state_type];
2449       outer_nw = outer_ne = &style->dark[state_type];
2450       break;
2451           
2452     case GTK_SHADOW_OUT:
2453       inner_sw = inner_se = &style->dark[state_type];
2454       middle_sw = middle_se = &style->dark[state_type];
2455       outer_sw = outer_se = &style->black;
2456       inner_nw = inner_ne = &style->bg[state_type];
2457       middle_nw = middle_ne = &style->light[state_type];
2458       outer_nw = outer_ne = &style->light[state_type];
2459       break;
2460
2461     case GTK_SHADOW_ETCHED_IN:
2462       inner_sw = inner_se = &style->bg[state_type];
2463       middle_sw = middle_se = &style->dark[state_type];
2464       outer_sw = outer_se = &style->light[state_type];
2465       inner_nw = inner_ne = &style->bg[state_type];
2466       middle_nw = middle_ne = &style->light[state_type];
2467       outer_nw = outer_ne = &style->dark[state_type];
2468       break;
2469
2470     case GTK_SHADOW_ETCHED_OUT:
2471       inner_sw = inner_se = &style->bg[state_type];
2472       middle_sw = middle_se = &style->light[state_type];
2473       outer_sw = outer_se = &style->dark[state_type];
2474       inner_nw = inner_ne = &style->bg[state_type];
2475       middle_nw = middle_ne = &style->dark[state_type];
2476       outer_nw = outer_ne = &style->light[state_type];
2477       break;
2478       
2479     default:
2480
2481       break;
2482     }
2483
2484   if (inner_sw)
2485     {
2486       _cairo_draw_line (cr, inner_sw,
2487                         x + 2, y + half_height,
2488                         x + half_width, y + height - 2);
2489       _cairo_draw_line (cr, inner_se,
2490                         x + half_width, y + height - 2,
2491                         x + width - 2, y + half_height);
2492       _cairo_draw_line (cr, middle_sw,
2493                         x + 1, y + half_height,
2494                         x + half_width, y + height - 1);
2495       _cairo_draw_line (cr, middle_se,
2496                         x + half_width, y + height - 1,
2497                         x + width - 1, y + half_height);
2498       _cairo_draw_line (cr, outer_sw,
2499                         x, y + half_height,
2500                         x + half_width, y + height);
2501       _cairo_draw_line (cr, outer_se,
2502                         x + half_width, y + height,
2503                         x + width, y + half_height);
2504   
2505       _cairo_draw_line (cr, inner_nw,
2506                         x + 2, y + half_height,
2507                         x + half_width, y + 2);
2508       _cairo_draw_line (cr, inner_ne,
2509                         x + half_width, y + 2,
2510                         x + width - 2, y + half_height);
2511       _cairo_draw_line (cr, middle_nw,
2512                         x + 1, y + half_height,
2513                         x + half_width, y + 1);
2514       _cairo_draw_line (cr, middle_ne,
2515                         x + half_width, y + 1,
2516                         x + width - 1, y + half_height);
2517       _cairo_draw_line (cr, outer_nw,
2518                         x, y + half_height,
2519                         x + half_width, y);
2520       _cairo_draw_line (cr, outer_ne,
2521                         x + half_width, y,
2522                         x + width, y + half_height);
2523     }
2524 }
2525
2526 static void
2527 option_menu_get_props (GtkWidget      *widget,
2528                        GtkRequisition *indicator_size,
2529                        GtkBorder      *indicator_spacing)
2530 {
2531   GtkRequisition *tmp_size = NULL;
2532   GtkBorder *tmp_spacing = NULL;
2533
2534   if (tmp_size)
2535     {
2536       *indicator_size = *tmp_size;
2537       gtk_requisition_free (tmp_size);
2538     }
2539   else
2540     *indicator_size = default_option_indicator_size;
2541
2542   if (tmp_spacing)
2543     {
2544       *indicator_spacing = *tmp_spacing;
2545       gtk_border_free (tmp_spacing);
2546     }
2547   else
2548     *indicator_spacing = default_option_indicator_spacing;
2549 }
2550
2551 static gboolean
2552 background_is_solid (GtkStyle     *style,
2553                      GtkStateType  type)
2554 {
2555   if (style->background[type] == NULL)
2556     return FALSE;
2557
2558   return cairo_pattern_get_type (style->background[type]) == CAIRO_PATTERN_TYPE_SOLID;
2559 }
2560
2561 static void 
2562 gtk_default_draw_box (GtkStyle      *style,
2563                       cairo_t       *cr,
2564                       GtkStateType   state_type,
2565                       GtkShadowType  shadow_type,
2566                       GtkWidget     *widget,
2567                       const gchar   *detail,
2568                       gint           x,
2569                       gint           y,
2570                       gint           width,
2571                       gint           height)
2572 {
2573   GtkStyleContext *context;
2574   GtkStylePrivate *priv;
2575   GtkStateFlags flags = 0;
2576
2577   if (widget)
2578     context = gtk_widget_get_style_context (widget);
2579   else
2580     {
2581       priv = GTK_STYLE_GET_PRIVATE (style);
2582       context = priv->context;
2583     }
2584
2585   gtk_style_context_save (context);
2586
2587   if (detail)
2588     transform_detail_string (detail, context);
2589
2590   switch (state_type)
2591     {
2592     case GTK_STATE_ACTIVE:
2593       flags |= GTK_STATE_FLAG_ACTIVE;
2594       break;
2595     case GTK_STATE_PRELIGHT:
2596       flags |= GTK_STATE_FLAG_PRELIGHT;
2597       break;
2598     case GTK_STATE_SELECTED:
2599       flags |= GTK_STATE_FLAG_SELECTED;
2600       break;
2601     case GTK_STATE_INSENSITIVE:
2602       flags |= GTK_STATE_FLAG_INSENSITIVE;
2603       break;
2604     default:
2605       break;
2606     }
2607
2608   if (shadow_type == GTK_SHADOW_IN)
2609     flags |= GTK_STATE_FLAG_ACTIVE;
2610
2611   gtk_style_context_set_state (context, flags);
2612
2613   cairo_save (cr);
2614
2615   if (gtk_style_context_has_class (context, GTK_STYLE_CLASS_PROGRESSBAR))
2616     gtk_render_activity (context, cr, x, y, width, height);
2617   else
2618     {
2619       gtk_render_background (context, cr, x, y, width, height);
2620       gtk_render_frame (context, cr, x, y, width, height);
2621     }
2622
2623   cairo_restore (cr);
2624   gtk_style_context_restore (context);
2625 }
2626
2627 static GdkColor *
2628 get_darkened (const GdkColor *color,
2629                  gint            darken_count)
2630 {
2631   GdkColor src = *color;
2632   GdkColor shaded = *color;
2633   
2634   while (darken_count)
2635     {
2636       _gtk_style_shade (&src, &shaded, 0.93);
2637       src = shaded;
2638       --darken_count;
2639     }
2640    
2641   return gdk_color_copy (&shaded);
2642 }
2643
2644 static void 
2645 gtk_default_draw_flat_box (GtkStyle      *style,
2646                            cairo_t       *cr,
2647                            GtkStateType   state_type,
2648                            GtkShadowType  shadow_type,
2649                            GtkWidget     *widget,
2650                            const gchar   *detail,
2651                            gint           x,
2652                            gint           y,
2653                            gint           width,
2654                            gint           height)
2655 {
2656   GtkStyleContext *context;
2657   GtkStylePrivate *priv;
2658   GtkStateFlags flags = 0;
2659
2660   if (widget)
2661     context = gtk_widget_get_style_context (widget);
2662   else
2663     {
2664       priv = GTK_STYLE_GET_PRIVATE (style);
2665       context = priv->context;
2666     }
2667
2668   gtk_style_context_save (context);
2669
2670   if (detail)
2671     transform_detail_string (detail, context);
2672
2673   switch (state_type)
2674     {
2675     case GTK_STATE_PRELIGHT:
2676       flags |= GTK_STATE_FLAG_PRELIGHT;
2677       break;
2678     case GTK_STATE_SELECTED:
2679       flags |= GTK_STATE_FLAG_SELECTED;
2680       break;
2681     case GTK_STATE_INSENSITIVE:
2682       flags |= GTK_STATE_FLAG_INSENSITIVE;
2683       break;
2684     case GTK_STATE_ACTIVE:
2685       flags |= GTK_STATE_FLAG_ACTIVE;
2686       break;
2687     case GTK_STATE_FOCUSED:
2688       flags |= GTK_STATE_FLAG_FOCUSED;
2689       break;
2690     default:
2691       break;
2692     }
2693
2694   gtk_style_context_set_state (context, flags);
2695
2696   cairo_save (cr);
2697
2698   gtk_render_background (context, cr,
2699                          (gdouble) x,
2700                          (gdouble) y,
2701                          (gdouble) width,
2702                          (gdouble) height);
2703
2704   cairo_restore (cr);
2705   gtk_style_context_restore (context);
2706 }
2707
2708 static void 
2709 gtk_default_draw_check (GtkStyle      *style,
2710                         cairo_t       *cr,
2711                         GtkStateType   state_type,
2712                         GtkShadowType  shadow_type,
2713                         GtkWidget     *widget,
2714                         const gchar   *detail,
2715                         gint           x,
2716                         gint           y,
2717                         gint           width,
2718                         gint           height)
2719 {
2720   GtkStyleContext *context;
2721   GtkStylePrivate *priv;
2722   GtkStateFlags flags = 0;
2723
2724   if (widget)
2725     context = gtk_widget_get_style_context (widget);
2726   else
2727     {
2728       priv = GTK_STYLE_GET_PRIVATE (style);
2729       context = priv->context;
2730     }
2731
2732   gtk_style_context_save (context);
2733
2734   if (detail)
2735     transform_detail_string (detail, context);
2736
2737   switch (state_type)
2738     {
2739     case GTK_STATE_PRELIGHT:
2740       flags |= GTK_STATE_FLAG_PRELIGHT;
2741       break;
2742     case GTK_STATE_SELECTED:
2743       flags |= GTK_STATE_FLAG_SELECTED;
2744       break;
2745     case GTK_STATE_INSENSITIVE:
2746       flags |= GTK_STATE_FLAG_INSENSITIVE;
2747       break;
2748     default:
2749       break;
2750     }
2751
2752   if (shadow_type == GTK_SHADOW_IN)
2753     flags |= GTK_STATE_FLAG_ACTIVE;
2754   else if (shadow_type == GTK_SHADOW_ETCHED_IN)
2755     flags |= GTK_STATE_FLAG_INCONSISTENT;
2756
2757   gtk_style_context_set_state (context, flags);
2758
2759   cairo_save (cr);
2760
2761   gtk_render_check (context,
2762                     cr, x, y,
2763                     width, height);
2764
2765   cairo_restore (cr);
2766   gtk_style_context_restore (context);
2767 }
2768
2769 static void 
2770 gtk_default_draw_option (GtkStyle      *style,
2771                          cairo_t       *cr,
2772                          GtkStateType   state_type,
2773                          GtkShadowType  shadow_type,
2774                          GtkWidget     *widget,
2775                          const gchar   *detail,
2776                          gint           x,
2777                          gint           y,
2778                          gint           width,
2779                          gint           height)
2780 {
2781   GtkStyleContext *context;
2782   GtkStylePrivate *priv;
2783   GtkStateFlags flags = 0;
2784
2785   if (widget)
2786     context = gtk_widget_get_style_context (widget);
2787   else
2788     {
2789       priv = GTK_STYLE_GET_PRIVATE (style);
2790       context = priv->context;
2791     }
2792
2793   gtk_style_context_save (context);
2794
2795   if (detail)
2796     transform_detail_string (detail, context);
2797
2798   switch (state_type)
2799     {
2800     case GTK_STATE_PRELIGHT:
2801       flags |= GTK_STATE_FLAG_PRELIGHT;
2802       break;
2803     case GTK_STATE_SELECTED:
2804       flags |= GTK_STATE_FLAG_SELECTED;
2805       break;
2806     case GTK_STATE_INSENSITIVE:
2807       flags |= GTK_STATE_FLAG_INSENSITIVE;
2808       break;
2809     default:
2810       break;
2811     }
2812
2813   if (shadow_type == GTK_SHADOW_IN)
2814     flags |= GTK_STATE_FLAG_ACTIVE;
2815   else if (shadow_type == GTK_SHADOW_ETCHED_IN)
2816     flags |= GTK_STATE_FLAG_INCONSISTENT;
2817
2818   gtk_style_context_set_state (context, flags);
2819
2820   cairo_save (cr);
2821   gtk_render_option (context, cr,
2822                      (gdouble) x,
2823                      (gdouble) y,
2824                      (gdouble) width,
2825                      (gdouble) height);
2826
2827   cairo_restore (cr);
2828   gtk_style_context_restore (context);
2829 }
2830
2831 static void
2832 gtk_default_draw_tab (GtkStyle      *style,
2833                       cairo_t       *cr,
2834                       GtkStateType   state_type,
2835                       GtkShadowType  shadow_type,
2836                       GtkWidget     *widget,
2837                       const gchar   *detail,
2838                       gint           x,
2839                       gint           y,
2840                       gint           width,
2841                       gint           height)
2842 {
2843 #define ARROW_SPACE 4
2844
2845   GtkRequisition indicator_size;
2846   GtkBorder indicator_spacing;
2847   gint arrow_height;
2848
2849   option_menu_get_props (widget, &indicator_size, &indicator_spacing);
2850
2851   indicator_size.width += (indicator_size.width % 2) - 1;
2852   arrow_height = indicator_size.width / 2 + 1;
2853
2854   x += (width - indicator_size.width) / 2;
2855   y += (height - (2 * arrow_height + ARROW_SPACE)) / 2;
2856
2857   if (state_type == GTK_STATE_INSENSITIVE)
2858     {
2859       draw_arrow (cr, &style->white,
2860                   GTK_ARROW_UP, x + 1, y + 1,
2861                   indicator_size.width, arrow_height);
2862       
2863       draw_arrow (cr, &style->white,
2864                   GTK_ARROW_DOWN, x + 1, y + arrow_height + ARROW_SPACE + 1,
2865                   indicator_size.width, arrow_height);
2866     }
2867   
2868   draw_arrow (cr, &style->fg[state_type],
2869               GTK_ARROW_UP, x, y,
2870               indicator_size.width, arrow_height);
2871   
2872   
2873   draw_arrow (cr, &style->fg[state_type],
2874               GTK_ARROW_DOWN, x, y + arrow_height + ARROW_SPACE,
2875               indicator_size.width, arrow_height);
2876 }
2877
2878 static void 
2879 gtk_default_draw_shadow_gap (GtkStyle       *style,
2880                              cairo_t        *cr,
2881                              GtkStateType    state_type,
2882                              GtkShadowType   shadow_type,
2883                              GtkWidget      *widget,
2884                              const gchar    *detail,
2885                              gint            x,
2886                              gint            y,
2887                              gint            width,
2888                              gint            height,
2889                              GtkPositionType gap_side,
2890                              gint            gap_x,
2891                              gint            gap_width)
2892 {
2893   GtkStyleContext *context;
2894   GtkStylePrivate *priv;
2895   GtkStateFlags flags = 0;
2896
2897   if (widget)
2898     context = gtk_widget_get_style_context (widget);
2899   else
2900     {
2901       priv = GTK_STYLE_GET_PRIVATE (style);
2902       context = priv->context;
2903     }
2904
2905   gtk_style_context_save (context);
2906
2907   if (detail)
2908     transform_detail_string (detail, context);
2909
2910   switch (state_type)
2911     {
2912     case GTK_STATE_ACTIVE:
2913       flags |= GTK_STATE_FLAG_ACTIVE;
2914       break;
2915     case GTK_STATE_PRELIGHT:
2916       flags |= GTK_STATE_FLAG_PRELIGHT;
2917       break;
2918     case GTK_STATE_SELECTED:
2919       flags |= GTK_STATE_FLAG_SELECTED;
2920       break;
2921     case GTK_STATE_INSENSITIVE:
2922       flags |= GTK_STATE_FLAG_INSENSITIVE;
2923       break;
2924     default:
2925       break;
2926     }
2927
2928   gtk_style_context_set_state (context, flags);
2929
2930   cairo_save (cr);
2931   gtk_render_frame_gap (context, cr,
2932                         (gdouble) x,
2933                         (gdouble) y,
2934                         (gdouble) width,
2935                         (gdouble) height,
2936                         gap_side,
2937                         (gdouble) gap_x,
2938                         (gdouble) gap_x + gap_width);
2939
2940   cairo_restore (cr);
2941   gtk_style_context_restore (context);
2942 }
2943
2944 static void 
2945 gtk_default_draw_box_gap (GtkStyle       *style,
2946                           cairo_t        *cr,
2947                           GtkStateType    state_type,
2948                           GtkShadowType   shadow_type,
2949                           GtkWidget      *widget,
2950                           const gchar    *detail,
2951                           gint            x,
2952                           gint            y,
2953                           gint            width,
2954                           gint            height,
2955                           GtkPositionType gap_side,
2956                           gint            gap_x,
2957                           gint            gap_width)
2958 {
2959   GtkStyleContext *context;
2960   GtkStylePrivate *priv;
2961   GtkStateFlags flags = 0;
2962
2963   if (widget)
2964     context = gtk_widget_get_style_context (widget);
2965   else
2966     {
2967       priv = GTK_STYLE_GET_PRIVATE (style);
2968       context = priv->context;
2969     }
2970
2971   gtk_style_context_save (context);
2972
2973   if (detail)
2974     transform_detail_string (detail, context);
2975
2976   switch (state_type)
2977     {
2978     case GTK_STATE_ACTIVE:
2979       flags |= GTK_STATE_FLAG_ACTIVE;
2980       break;
2981     case GTK_STATE_PRELIGHT:
2982       flags |= GTK_STATE_FLAG_PRELIGHT;
2983       break;
2984     case GTK_STATE_SELECTED:
2985       flags |= GTK_STATE_FLAG_SELECTED;
2986       break;
2987     case GTK_STATE_INSENSITIVE:
2988       flags |= GTK_STATE_FLAG_INSENSITIVE;
2989       break;
2990     default:
2991       break;
2992     }
2993
2994   gtk_style_context_set_state (context, flags);
2995
2996   cairo_save (cr);
2997   gtk_render_background (context, cr,
2998                          (gdouble) x,
2999                          (gdouble) y,
3000                          (gdouble) width,
3001                          (gdouble) height);
3002
3003   gtk_render_frame_gap (context, cr,
3004                         (gdouble) x,
3005                         (gdouble) y,
3006                         (gdouble) width,
3007                         (gdouble) height,
3008                         gap_side,
3009                         (gdouble) gap_x,
3010                         (gdouble) gap_x + gap_width);
3011
3012   cairo_restore (cr);
3013   gtk_style_context_restore (context);
3014 }
3015
3016 static void 
3017 gtk_default_draw_extension (GtkStyle       *style,
3018                             cairo_t        *cr,
3019                             GtkStateType    state_type,
3020                             GtkShadowType   shadow_type,
3021                             GtkWidget      *widget,
3022                             const gchar    *detail,
3023                             gint            x,
3024                             gint            y,
3025                             gint            width,
3026                             gint            height,
3027                             GtkPositionType gap_side)
3028 {
3029   GtkStyleContext *context;
3030   GtkStylePrivate *priv;
3031   GtkStateFlags flags = 0;
3032
3033   if (widget)
3034     context = gtk_widget_get_style_context (widget);
3035   else
3036     {
3037       priv = GTK_STYLE_GET_PRIVATE (style);
3038       context = priv->context;
3039     }
3040
3041   gtk_style_context_save (context);
3042
3043   if (detail)
3044     transform_detail_string (detail, context);
3045
3046   switch (state_type)
3047     {
3048     case GTK_STATE_ACTIVE:
3049       flags |= GTK_STATE_FLAG_ACTIVE;
3050       break;
3051     case GTK_STATE_PRELIGHT:
3052       flags |= GTK_STATE_FLAG_PRELIGHT;
3053       break;
3054     case GTK_STATE_SELECTED:
3055       flags |= GTK_STATE_FLAG_SELECTED;
3056       break;
3057     case GTK_STATE_INSENSITIVE:
3058       flags |= GTK_STATE_FLAG_INSENSITIVE;
3059       break;
3060     default:
3061       break;
3062     }
3063
3064   gtk_style_context_set_state (context, flags);
3065
3066   cairo_save (cr);
3067
3068   gtk_render_extension (context, cr,
3069                         (gdouble) x,
3070                         (gdouble) y,
3071                         (gdouble) width,
3072                         (gdouble) height,
3073                         gap_side);
3074
3075   cairo_restore (cr);
3076   gtk_style_context_restore (context);
3077 }
3078
3079 static void 
3080 gtk_default_draw_focus (GtkStyle      *style,
3081                         cairo_t       *cr,
3082                         GtkStateType   state_type,
3083                         GtkWidget     *widget,
3084                         const gchar   *detail,
3085                         gint           x,
3086                         gint           y,
3087                         gint           width,
3088                         gint           height)
3089 {
3090   GtkStyleContext *context;
3091   GtkStylePrivate *priv;
3092
3093   if (widget)
3094     context = gtk_widget_get_style_context (widget);
3095   else
3096     {
3097       priv = GTK_STYLE_GET_PRIVATE (style);
3098       context = priv->context;
3099     }
3100
3101   gtk_style_context_save (context);
3102
3103   if (detail)
3104     transform_detail_string (detail, context);
3105
3106   cairo_save (cr);
3107
3108   gtk_render_focus (context, cr,
3109                     (gdouble) x,
3110                     (gdouble) y,
3111                     (gdouble) width,
3112                     (gdouble) height);
3113
3114   cairo_restore (cr);
3115   gtk_style_context_restore (context);
3116 }
3117
3118 static void 
3119 gtk_default_draw_slider (GtkStyle      *style,
3120                          cairo_t       *cr,
3121                          GtkStateType   state_type,
3122                          GtkShadowType  shadow_type,
3123                          GtkWidget     *widget,
3124                          const gchar   *detail,
3125                          gint           x,
3126                          gint           y,
3127                          gint           width,
3128                          gint           height,
3129                          GtkOrientation orientation)
3130 {
3131   GtkStyleContext *context;
3132   GtkStylePrivate *priv;
3133   GtkStateFlags flags = 0;
3134
3135   if (widget)
3136     context = gtk_widget_get_style_context (widget);
3137   else
3138     {
3139       priv = GTK_STYLE_GET_PRIVATE (style);
3140       context = priv->context;
3141     }
3142
3143   gtk_style_context_save (context);
3144
3145   if (detail)
3146     transform_detail_string (detail, context);
3147
3148   switch (state_type)
3149     {
3150     case GTK_STATE_PRELIGHT:
3151       flags |= GTK_STATE_FLAG_PRELIGHT;
3152       break;
3153     case GTK_STATE_SELECTED:
3154       flags |= GTK_STATE_FLAG_SELECTED;
3155       break;
3156     case GTK_STATE_INSENSITIVE:
3157       flags |= GTK_STATE_FLAG_INSENSITIVE;
3158       break;
3159     default:
3160       break;
3161     }
3162
3163   gtk_style_context_set_state (context, flags);
3164
3165   cairo_save (cr);
3166
3167   gtk_render_slider (context, cr,  x, y, width, height, orientation);
3168
3169   cairo_restore (cr);
3170   gtk_style_context_restore (context);
3171 }
3172
3173 static void
3174 draw_dot (cairo_t    *cr,
3175           GdkColor   *light,
3176           GdkColor   *dark,
3177           gint        x,
3178           gint        y,
3179           gushort     size)
3180 {
3181   size = CLAMP (size, 2, 3);
3182
3183   if (size == 2)
3184     {
3185       _cairo_draw_point (cr, light, x, y);
3186       _cairo_draw_point (cr, light, x+1, y+1);
3187     }
3188   else if (size == 3)
3189     {
3190       _cairo_draw_point (cr, light, x, y);
3191       _cairo_draw_point (cr, light, x+1, y);
3192       _cairo_draw_point (cr, light, x, y+1);
3193       _cairo_draw_point (cr, dark, x+1, y+2);
3194       _cairo_draw_point (cr, dark, x+2, y+1);
3195       _cairo_draw_point (cr, dark, x+2, y+2);
3196     }
3197 }
3198
3199 static void 
3200 gtk_default_draw_handle (GtkStyle      *style,
3201                          cairo_t       *cr,
3202                          GtkStateType   state_type,
3203                          GtkShadowType  shadow_type,
3204                          GtkWidget     *widget,
3205                          const gchar   *detail,
3206                          gint           x,
3207                          gint           y,
3208                          gint           width,
3209                          gint           height,
3210                          GtkOrientation orientation)
3211 {
3212   GtkStyleContext *context;
3213   GtkStylePrivate *priv;
3214   GtkStateFlags flags = 0;
3215
3216   if (widget)
3217     context = gtk_widget_get_style_context (widget);
3218   else
3219     {
3220       priv = GTK_STYLE_GET_PRIVATE (style);
3221       context = priv->context;
3222     }
3223
3224   gtk_style_context_save (context);
3225
3226   if (detail)
3227     transform_detail_string (detail, context);
3228
3229   switch (state_type)
3230     {
3231     case GTK_STATE_PRELIGHT:
3232       flags |= GTK_STATE_FLAG_PRELIGHT;
3233       break;
3234     case GTK_STATE_SELECTED:
3235       flags |= GTK_STATE_FLAG_SELECTED;
3236       break;
3237     case GTK_STATE_INSENSITIVE:
3238       flags |= GTK_STATE_FLAG_INSENSITIVE;
3239       break;
3240     default:
3241       break;
3242     }
3243
3244   gtk_style_context_set_state (context, flags);
3245
3246   cairo_save (cr);
3247
3248   gtk_render_handle (context, cr,
3249                      (gdouble) x,
3250                      (gdouble) y,
3251                      (gdouble) width,
3252                      (gdouble) height);
3253
3254   cairo_restore (cr);
3255   gtk_style_context_restore (context);
3256 }
3257
3258 static void
3259 gtk_default_draw_expander (GtkStyle        *style,
3260                            cairo_t         *cr,
3261                            GtkStateType     state_type,
3262                            GtkWidget       *widget,
3263                            const gchar     *detail,
3264                            gint             x,
3265                            gint             y,
3266                            GtkExpanderStyle expander_style)
3267 {
3268   GtkStyleContext *context;
3269   GtkStylePrivate *priv;
3270   GtkStateFlags flags = 0;
3271   gint size;
3272
3273   if (widget)
3274     context = gtk_widget_get_style_context (widget);
3275   else
3276     {
3277       priv = GTK_STYLE_GET_PRIVATE (style);
3278       context = priv->context;
3279     }
3280
3281   gtk_style_context_save (context);
3282
3283   if (detail)
3284     transform_detail_string (detail, context);
3285
3286   gtk_style_context_add_class (context, "expander");
3287
3288   switch (state_type)
3289     {
3290     case GTK_STATE_PRELIGHT:
3291       flags |= GTK_STATE_FLAG_PRELIGHT;
3292       break;
3293     case GTK_STATE_SELECTED:
3294       flags |= GTK_STATE_FLAG_SELECTED;
3295       break;
3296     case GTK_STATE_INSENSITIVE:
3297       flags |= GTK_STATE_FLAG_INSENSITIVE;
3298       break;
3299     default:
3300       break;
3301     }
3302
3303   if (widget &&
3304       gtk_widget_class_find_style_property (GTK_WIDGET_GET_CLASS (widget),
3305                                             "expander-size"))
3306     gtk_widget_style_get (widget, "expander-size", &size, NULL);
3307   else
3308     size = 12;
3309
3310   if (expander_style == GTK_EXPANDER_EXPANDED)
3311     flags |= GTK_STATE_FLAG_ACTIVE;
3312
3313   gtk_style_context_set_state (context, flags);
3314
3315   cairo_save (cr);
3316
3317   gtk_render_expander (context, cr,
3318                        (gdouble) x - (size / 2),
3319                        (gdouble) y - (size / 2),
3320                        (gdouble) size,
3321                        (gdouble) size);
3322
3323   cairo_restore (cr);
3324   gtk_style_context_restore (context);
3325 }
3326
3327 static void
3328 gtk_default_draw_layout (GtkStyle        *style,
3329                          cairo_t         *cr,
3330                          GtkStateType     state_type,
3331                          gboolean         use_text,
3332                          GtkWidget       *widget,
3333                          const gchar     *detail,
3334                          gint             x,
3335                          gint             y,
3336                          PangoLayout     *layout)
3337 {
3338   GtkStyleContext *context;
3339   GtkStylePrivate *priv;
3340   GtkStateFlags flags = 0;
3341
3342   if (widget)
3343     context = gtk_widget_get_style_context (widget);
3344   else
3345     {
3346       priv = GTK_STYLE_GET_PRIVATE (style);
3347       context = priv->context;
3348     }
3349
3350   gtk_style_context_save (context);
3351
3352   if (detail)
3353     transform_detail_string (detail, context);
3354
3355   switch (state_type)
3356     {
3357     case GTK_STATE_PRELIGHT:
3358       flags |= GTK_STATE_FLAG_PRELIGHT;
3359       break;
3360     case GTK_STATE_SELECTED:
3361       flags |= GTK_STATE_FLAG_SELECTED;
3362       break;
3363     case GTK_STATE_INSENSITIVE:
3364       flags |= GTK_STATE_FLAG_INSENSITIVE;
3365       break;
3366     default:
3367       break;
3368     }
3369
3370   gtk_style_context_set_state (context, flags);
3371
3372   cairo_save (cr);
3373
3374   gtk_render_layout (context, cr,
3375                      (gdouble) x,
3376                      (gdouble) y,
3377                      layout);
3378
3379   cairo_restore (cr);
3380   gtk_style_context_restore (context);
3381 }
3382
3383 static void
3384 gtk_default_draw_resize_grip (GtkStyle       *style,
3385                               cairo_t        *cr,
3386                               GtkStateType    state_type,
3387                               GtkWidget      *widget,
3388                               const gchar    *detail,
3389                               GdkWindowEdge   edge,
3390                               gint            x,
3391                               gint            y,
3392                               gint            width,
3393                               gint            height)
3394 {
3395   GtkStyleContext *context;
3396   GtkStylePrivate *priv;
3397   GtkStateFlags flags = 0;
3398   GtkJunctionSides sides = 0;
3399
3400   if (widget)
3401     context = gtk_widget_get_style_context (widget);
3402   else
3403     {
3404       priv = GTK_STYLE_GET_PRIVATE (style);
3405       context = priv->context;
3406     }
3407
3408   gtk_style_context_save (context);
3409
3410   if (detail)
3411     transform_detail_string (detail, context);
3412
3413   gtk_style_context_add_class (context, "grip");
3414
3415   switch (state_type)
3416     {
3417     case GTK_STATE_PRELIGHT:
3418       flags |= GTK_STATE_FLAG_PRELIGHT;
3419       break;
3420     case GTK_STATE_SELECTED:
3421       flags |= GTK_STATE_FLAG_SELECTED;
3422       break;
3423     case GTK_STATE_INSENSITIVE:
3424       flags |= GTK_STATE_FLAG_INSENSITIVE;
3425       break;
3426     default:
3427       break;
3428     }
3429
3430   gtk_style_context_set_state (context, flags);
3431
3432   switch (edge)
3433     {
3434     case GDK_WINDOW_EDGE_NORTH_WEST:
3435       sides = GTK_JUNCTION_CORNER_TOPLEFT;
3436       break;
3437     case GDK_WINDOW_EDGE_NORTH:
3438       sides = GTK_JUNCTION_TOP;
3439       break;
3440     case GDK_WINDOW_EDGE_NORTH_EAST:
3441       sides = GTK_JUNCTION_CORNER_TOPRIGHT;
3442       break;
3443     case GDK_WINDOW_EDGE_WEST:
3444       sides = GTK_JUNCTION_LEFT;
3445       break;
3446     case GDK_WINDOW_EDGE_EAST:
3447       sides = GTK_JUNCTION_RIGHT;
3448       break;
3449     case GDK_WINDOW_EDGE_SOUTH_WEST:
3450       sides = GTK_JUNCTION_CORNER_BOTTOMLEFT;
3451       break;
3452     case GDK_WINDOW_EDGE_SOUTH:
3453       sides = GTK_JUNCTION_BOTTOM;
3454       break;
3455     case GDK_WINDOW_EDGE_SOUTH_EAST:
3456       sides = GTK_JUNCTION_CORNER_BOTTOMRIGHT;
3457       break;
3458     }
3459
3460   gtk_style_context_set_junction_sides (context, sides);
3461
3462   cairo_save (cr);
3463
3464   gtk_render_handle (context, cr,
3465                      (gdouble) x,
3466                      (gdouble) y,
3467                      (gdouble) width,
3468                      (gdouble) height);
3469
3470   cairo_restore (cr);
3471   gtk_style_context_restore (context);
3472 }
3473
3474 static void
3475 gtk_default_draw_spinner (GtkStyle     *style,
3476                           cairo_t      *cr,
3477                           GtkStateType  state_type,
3478                           GtkWidget    *widget,
3479                           const gchar  *detail,
3480                           guint         step,
3481                           gint          x,
3482                           gint          y,
3483                           gint          width,
3484                           gint          height)
3485 {
3486   GdkColor *color;
3487   guint num_steps;
3488   gdouble dx, dy;
3489   gdouble radius;
3490   gdouble half;
3491   gint i;
3492   guint real_step;
3493
3494   gtk_style_get (style, GTK_TYPE_SPINNER,
3495                  "num-steps", &num_steps,
3496                  NULL);
3497   real_step = step % num_steps;
3498
3499   /* set a clip region for the expose event */
3500   cairo_rectangle (cr, x, y, width, height);
3501   cairo_clip (cr);
3502
3503   cairo_translate (cr, x, y);
3504
3505   /* draw clip region */
3506   cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
3507
3508   color = &style->fg[state_type];
3509   dx = width / 2;
3510   dy = height / 2;
3511   radius = MIN (width / 2, height / 2);
3512   half = num_steps / 2;
3513
3514   for (i = 0; i < num_steps; i++)
3515     {
3516       gint inset = 0.7 * radius;
3517
3518       /* transparency is a function of time and intial value */
3519       gdouble t = (gdouble) ((i + num_steps - real_step)
3520                              % num_steps) / num_steps;
3521
3522       cairo_save (cr);
3523
3524       cairo_set_source_rgba (cr,
3525                              color->red / 65535.,
3526                              color->green / 65535.,
3527                              color->blue / 65535.,
3528                              t);
3529
3530       cairo_set_line_width (cr, 2.0);
3531       cairo_move_to (cr,
3532                      dx + (radius - inset) * cos (i * G_PI / half),
3533                      dy + (radius - inset) * sin (i * G_PI / half));
3534       cairo_line_to (cr,
3535                      dx + radius * cos (i * G_PI / half),
3536                      dy + radius * sin (i * G_PI / half));
3537       cairo_stroke (cr);
3538
3539       cairo_restore (cr);
3540     }
3541 }
3542
3543 void
3544 _gtk_style_shade (const GdkColor *a,
3545                   GdkColor       *b,
3546                   gdouble         k)
3547 {
3548   gdouble red;
3549   gdouble green;
3550   gdouble blue;
3551   
3552   red = (gdouble) a->red / 65535.0;
3553   green = (gdouble) a->green / 65535.0;
3554   blue = (gdouble) a->blue / 65535.0;
3555   
3556   rgb_to_hls (&red, &green, &blue);
3557   
3558   green *= k;
3559   if (green > 1.0)
3560     green = 1.0;
3561   else if (green < 0.0)
3562     green = 0.0;
3563   
3564   blue *= k;
3565   if (blue > 1.0)
3566     blue = 1.0;
3567   else if (blue < 0.0)
3568     blue = 0.0;
3569   
3570   hls_to_rgb (&red, &green, &blue);
3571   
3572   b->red = red * 65535.0;
3573   b->green = green * 65535.0;
3574   b->blue = blue * 65535.0;
3575 }
3576
3577 static void
3578 rgb_to_hls (gdouble *r,
3579             gdouble *g,
3580             gdouble *b)
3581 {
3582   gdouble min;
3583   gdouble max;
3584   gdouble red;
3585   gdouble green;
3586   gdouble blue;
3587   gdouble h, l, s;
3588   gdouble delta;
3589   
3590   red = *r;
3591   green = *g;
3592   blue = *b;
3593   
3594   if (red > green)
3595     {
3596       if (red > blue)
3597         max = red;
3598       else
3599         max = blue;
3600       
3601       if (green < blue)
3602         min = green;
3603       else
3604         min = blue;
3605     }
3606   else
3607     {
3608       if (green > blue)
3609         max = green;
3610       else
3611         max = blue;
3612       
3613       if (red < blue)
3614         min = red;
3615       else
3616         min = blue;
3617     }
3618   
3619   l = (max + min) / 2;
3620   s = 0;
3621   h = 0;
3622   
3623   if (max != min)
3624     {
3625       if (l <= 0.5)
3626         s = (max - min) / (max + min);
3627       else
3628         s = (max - min) / (2 - max - min);
3629       
3630       delta = max -min;
3631       if (red == max)
3632         h = (green - blue) / delta;
3633       else if (green == max)
3634         h = 2 + (blue - red) / delta;
3635       else if (blue == max)
3636         h = 4 + (red - green) / delta;
3637       
3638       h *= 60;
3639       if (h < 0.0)
3640         h += 360;
3641     }
3642   
3643   *r = h;
3644   *g = l;
3645   *b = s;
3646 }
3647
3648 static void
3649 hls_to_rgb (gdouble *h,
3650             gdouble *l,
3651             gdouble *s)
3652 {
3653   gdouble hue;
3654   gdouble lightness;
3655   gdouble saturation;
3656   gdouble m1, m2;
3657   gdouble r, g, b;
3658   
3659   lightness = *l;
3660   saturation = *s;
3661   
3662   if (lightness <= 0.5)
3663     m2 = lightness * (1 + saturation);
3664   else
3665     m2 = lightness + saturation - lightness * saturation;
3666   m1 = 2 * lightness - m2;
3667   
3668   if (saturation == 0)
3669     {
3670       *h = lightness;
3671       *l = lightness;
3672       *s = lightness;
3673     }
3674   else
3675     {
3676       hue = *h + 120;
3677       while (hue > 360)
3678         hue -= 360;
3679       while (hue < 0)
3680         hue += 360;
3681       
3682       if (hue < 60)
3683         r = m1 + (m2 - m1) * hue / 60;
3684       else if (hue < 180)
3685         r = m2;
3686       else if (hue < 240)
3687         r = m1 + (m2 - m1) * (240 - hue) / 60;
3688       else
3689         r = m1;
3690       
3691       hue = *h;
3692       while (hue > 360)
3693         hue -= 360;
3694       while (hue < 0)
3695         hue += 360;
3696       
3697       if (hue < 60)
3698         g = m1 + (m2 - m1) * hue / 60;
3699       else if (hue < 180)
3700         g = m2;
3701       else if (hue < 240)
3702         g = m1 + (m2 - m1) * (240 - hue) / 60;
3703       else
3704         g = m1;
3705       
3706       hue = *h - 120;
3707       while (hue > 360)
3708         hue -= 360;
3709       while (hue < 0)
3710         hue += 360;
3711       
3712       if (hue < 60)
3713         b = m1 + (m2 - m1) * hue / 60;
3714       else if (hue < 180)
3715         b = m2;
3716       else if (hue < 240)
3717         b = m1 + (m2 - m1) * (240 - hue) / 60;
3718       else
3719         b = m1;
3720       
3721       *h = r;
3722       *l = g;
3723       *s = b;
3724     }
3725 }
3726
3727
3728 /**
3729  * gtk_paint_hline:
3730  * @style: a #GtkStyle
3731  * @cr: a #caio_t
3732  * @state_type: a state
3733  * @widget: (allow-none): the widget
3734  * @detail: (allow-none): a style detail
3735  * @x1: the starting x coordinate
3736  * @x2: the ending x coordinate
3737  * @y: the y coordinate
3738  *
3739  * Draws a horizontal line from (@x1, @y) to (@x2, @y) in @cr
3740  * using the given style and state.
3741  *
3742  * Deprecated:3.0: Use gtk_render_line() instead
3743  **/
3744 void
3745 gtk_paint_hline (GtkStyle           *style,
3746                  cairo_t            *cr,
3747                  GtkStateType        state_type,
3748                  GtkWidget          *widget,
3749                  const gchar        *detail,
3750                  gint                x1,
3751                  gint                x2,
3752                  gint                y)
3753 {
3754   g_return_if_fail (GTK_IS_STYLE (style));
3755   g_return_if_fail (cr != NULL);
3756   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_hline != NULL);
3757
3758   cairo_save (cr);
3759
3760   GTK_STYLE_GET_CLASS (style)->draw_hline (style, cr, state_type,
3761                                            widget, detail,
3762                                            x1, x2, y);
3763
3764   cairo_restore (cr);
3765 }
3766
3767 /**
3768  * gtk_paint_vline:
3769  * @style: a #GtkStyle
3770  * @cr: a #cairo_t
3771  * @state_type: a state
3772  * @widget: (allow-none): the widget
3773  * @detail: (allow-none): a style detail
3774  * @y1_: the starting y coordinate
3775  * @y2_: the ending y coordinate
3776  * @x: the x coordinate
3777  *
3778  * Draws a vertical line from (@x, @y1_) to (@x, @y2_) in @cr
3779  * using the given style and state.
3780  *
3781  * Deprecated:3.0: Use gtk_render_line() instead
3782  */
3783 void
3784 gtk_paint_vline (GtkStyle           *style,
3785                  cairo_t            *cr,
3786                  GtkStateType        state_type,
3787                  GtkWidget          *widget,
3788                  const gchar        *detail,
3789                  gint                y1_,
3790                  gint                y2_,
3791                  gint                x)
3792 {
3793   g_return_if_fail (GTK_IS_STYLE (style));
3794   g_return_if_fail (cr != NULL);
3795   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_vline != NULL);
3796
3797   cairo_save (cr);
3798
3799   GTK_STYLE_GET_CLASS (style)->draw_vline (style, cr, state_type,
3800                                            widget, detail,
3801                                            y1_, y2_, x);
3802
3803   cairo_restore (cr);
3804 }
3805
3806 /**
3807  * gtk_paint_shadow:
3808  * @style: a #GtkStyle
3809  * @cr: a #cairo_t
3810  * @state_type: a state
3811  * @shadow_type: type of shadow to draw
3812  * @widget: (allow-none): the widget
3813  * @detail: (allow-none): a style detail
3814  * @x: x origin of the rectangle
3815  * @y: y origin of the rectangle
3816  * @width: width of the rectangle
3817  * @height: width of the rectangle
3818  *
3819  * Draws a shadow around the given rectangle in @cr
3820  * using the given style and state and shadow type.
3821  *
3822  * Deprecated:3.0: Use gtk_render_frame() instead
3823  */
3824 void
3825 gtk_paint_shadow (GtkStyle           *style,
3826                   cairo_t            *cr,
3827                   GtkStateType        state_type,
3828                   GtkShadowType       shadow_type,
3829                   GtkWidget          *widget,
3830                   const gchar        *detail,
3831                   gint                x,
3832                   gint                y,
3833                   gint                width,
3834                   gint                height)
3835 {
3836   g_return_if_fail (GTK_IS_STYLE (style));
3837   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow != NULL);
3838   g_return_if_fail (cr != NULL);
3839   g_return_if_fail (width >= 0);
3840   g_return_if_fail (height >= 0);
3841
3842   cairo_save (cr);
3843
3844   GTK_STYLE_GET_CLASS (style)->draw_shadow (style, cr, state_type, shadow_type,
3845                                             widget, detail,
3846                                             x, y, width, height);
3847
3848   cairo_restore (cr);
3849 }
3850
3851 /**
3852  * gtk_paint_arrow:
3853  * @style: a #GtkStyle
3854  * @cr: a #cairo_t
3855  * @state_type: a state
3856  * @shadow_type: the type of shadow to draw
3857  * @widget: (allow-none): the widget
3858  * @detail: (allow-none): a style detail
3859  * @arrow_type: the type of arrow to draw
3860  * @fill: %TRUE if the arrow tip should be filled
3861  * @x: x origin of the rectangle to draw the arrow in
3862  * @y: y origin of the rectangle to draw the arrow in
3863  * @width: width of the rectangle to draw the arrow in
3864  * @height: height of the rectangle to draw the arrow in
3865  *
3866  * Draws an arrow in the given rectangle on @cr using the given
3867  * parameters. @arrow_type determines the direction of the arrow.
3868  *
3869  * Deprecated:3.0: Use gtk_render_arrow() instead
3870  */
3871 void
3872 gtk_paint_arrow (GtkStyle           *style,
3873                  cairo_t            *cr,
3874                  GtkStateType        state_type,
3875                  GtkShadowType       shadow_type,
3876                  GtkWidget          *widget,
3877                  const gchar        *detail,
3878                  GtkArrowType        arrow_type,
3879                  gboolean            fill,
3880                  gint                x,
3881                  gint                y,
3882                  gint                width,
3883                  gint                height)
3884 {
3885   g_return_if_fail (GTK_IS_STYLE (style));
3886   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_arrow != NULL);
3887   g_return_if_fail (cr != NULL);
3888   g_return_if_fail (width >= 0);
3889   g_return_if_fail (height >= 0);
3890
3891   cairo_save (cr);
3892
3893   GTK_STYLE_GET_CLASS (style)->draw_arrow (style, cr, state_type, shadow_type,
3894                                            widget, detail,
3895                                            arrow_type, fill, x, y, width, height);
3896
3897   cairo_restore (cr);
3898 }
3899
3900 /**
3901  * gtk_paint_diamond:
3902  * @style: a #GtkStyle
3903  * @cr: a #cairo_t
3904  * @state_type: a state
3905  * @shadow_type: the type of shadow to draw
3906  * @widget: (allow-none): the widget
3907  * @detail: (allow-none): a style detail
3908  * @x: x origin of the rectangle to draw the diamond in
3909  * @y: y origin of the rectangle to draw the diamond in
3910  * @width: width of the rectangle to draw the diamond in
3911  * @height: height of the rectangle to draw the diamond in
3912  *
3913  * Draws a diamond in the given rectangle on @window using the given
3914  * parameters.
3915  *
3916  * Deprecated:3.0: Use cairo instead
3917  */
3918 void
3919 gtk_paint_diamond (GtkStyle           *style,
3920                    cairo_t            *cr,
3921                    GtkStateType        state_type,
3922                    GtkShadowType       shadow_type,
3923                    GtkWidget          *widget,
3924                    const gchar        *detail,
3925                    gint                x,
3926                    gint                y,
3927                    gint                width,
3928                    gint                height)
3929 {
3930   g_return_if_fail (GTK_IS_STYLE (style));
3931   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_diamond != NULL);
3932   g_return_if_fail (cr != NULL);
3933   g_return_if_fail (width >= 0);
3934   g_return_if_fail (height >= 0);
3935
3936   cairo_save (cr);
3937
3938   GTK_STYLE_GET_CLASS (style)->draw_diamond (style, cr, state_type, shadow_type,
3939                                              widget, detail,
3940                                              x, y, width, height);
3941
3942   cairo_restore (cr);
3943 }
3944
3945 /**
3946  * gtk_paint_box:
3947  * @style: a #GtkStyle
3948  * @cr: a #cairo_t
3949  * @state_type: a state
3950  * @shadow_type: the type of shadow to draw
3951  * @widget: (allow-none): the widget
3952  * @detail: (allow-none): a style detail
3953  * @x: x origin of the box
3954  * @y: y origin of the box
3955  * @width: the width of the box
3956  * @height: the height of the box
3957  *
3958  * Draws a box on @cr with the given parameters.
3959  *
3960  * Deprecated:3.0: Use gtk_render_frame() and gtk_render_background() instead
3961  */
3962 void
3963 gtk_paint_box (GtkStyle           *style,
3964                cairo_t            *cr,
3965                GtkStateType        state_type,
3966                GtkShadowType       shadow_type,
3967                GtkWidget          *widget,
3968                const gchar        *detail,
3969                gint                x,
3970                gint                y,
3971                gint                width,
3972                gint                height)
3973 {
3974   g_return_if_fail (GTK_IS_STYLE (style));
3975   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box != NULL);
3976   g_return_if_fail (cr != NULL);
3977
3978   cairo_save (cr);
3979
3980   GTK_STYLE_GET_CLASS (style)->draw_box (style, cr, state_type, shadow_type,
3981                                          widget, detail,
3982                                          x, y, width, height);
3983
3984   cairo_restore (cr);
3985 }
3986
3987 /**
3988  * gtk_paint_flat_box:
3989  * @style: a #GtkStyle
3990  * @cr: a #cairo_t
3991  * @state_type: a state
3992  * @shadow_type: the type of shadow to draw
3993  * @area: (allow-none): clip rectangle, or %NULL if the
3994  *        output should not be clipped
3995  * @widget: (allow-none): the widget
3996  * @detail: (allow-none): a style detail
3997  * @x: x origin of the box
3998  * @y: y origin of the box
3999  * @width: the width of the box
4000  * @height: the height of the box
4001  *
4002  * Draws a flat box on @cr with the given parameters.
4003  *
4004  * Deprecated:3.0: Use gtk_render_frame() and gtk_render_background() instead
4005  */
4006 void
4007 gtk_paint_flat_box (GtkStyle           *style,
4008                     cairo_t            *cr,
4009                     GtkStateType        state_type,
4010                     GtkShadowType       shadow_type,
4011                     GtkWidget          *widget,
4012                     const gchar        *detail,
4013                     gint                x,
4014                     gint                y,
4015                     gint                width,
4016                     gint                height)
4017 {
4018   g_return_if_fail (GTK_IS_STYLE (style));
4019   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_flat_box != NULL);
4020   g_return_if_fail (cr != NULL);
4021   g_return_if_fail (width >= 0);
4022   g_return_if_fail (height >= 0);
4023
4024   cairo_save (cr);
4025
4026   GTK_STYLE_GET_CLASS (style)->draw_flat_box (style, cr, state_type, shadow_type,
4027                                               widget, detail,
4028                                               x, y, width, height);
4029
4030   cairo_restore (cr);
4031 }
4032
4033 /**
4034  * gtk_paint_check:
4035  * @style: a #GtkStyle
4036  * @cr: a #cairo_t
4037  * @state_type: a state
4038  * @shadow_type: the type of shadow to draw
4039  * @widget: (allow-none): the widget
4040  * @detail: (allow-none): a style detail
4041  * @x: x origin of the rectangle to draw the check in
4042  * @y: y origin of the rectangle to draw the check in
4043  * @width: the width of the rectangle to draw the check in
4044  * @height: the height of the rectangle to draw the check in
4045  *
4046  * Draws a check button indicator in the given rectangle on @cr with
4047  * the given parameters.
4048  *
4049  * Deprecated:3.0: Use gtk_render_check() instead
4050  */
4051 void
4052 gtk_paint_check (GtkStyle           *style,
4053                  cairo_t            *cr,
4054                  GtkStateType        state_type,
4055                  GtkShadowType       shadow_type,
4056                  GtkWidget          *widget,
4057                  const gchar        *detail,
4058                  gint                x,
4059                  gint                y,
4060                  gint                width,
4061                  gint                height)
4062 {
4063   g_return_if_fail (GTK_IS_STYLE (style));
4064   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_check != NULL);
4065   g_return_if_fail (cr != NULL);
4066
4067   cairo_save (cr);
4068
4069   GTK_STYLE_GET_CLASS (style)->draw_check (style, cr, state_type, shadow_type,
4070                                            widget, detail,
4071                                            x, y, width, height);
4072
4073   cairo_restore (cr);
4074 }
4075
4076 /**
4077  * gtk_paint_option:
4078  * @style: a #GtkStyle
4079  * @cr: a #cairo_t
4080  * @state_type: a state
4081  * @shadow_type: the type of shadow to draw
4082  * @widget: (allow-none): the widget
4083  * @detail: (allow-none): a style detail
4084  * @x: x origin of the rectangle to draw the option in
4085  * @y: y origin of the rectangle to draw the option in
4086  * @width: the width of the rectangle to draw the option in
4087  * @height: the height of the rectangle to draw the option in
4088  *
4089  * Draws a radio button indicator in the given rectangle on @cr with
4090  * the given parameters.
4091  *
4092  * Deprecated:3.0: Use gtk_render_option() instead
4093  */
4094 void
4095 gtk_paint_option (GtkStyle           *style,
4096                   cairo_t            *cr,
4097                   GtkStateType        state_type,
4098                   GtkShadowType       shadow_type,
4099                   GtkWidget          *widget,
4100                   const gchar        *detail,
4101                   gint                x,
4102                   gint                y,
4103                   gint                width,
4104                   gint                height)
4105 {
4106   g_return_if_fail (GTK_IS_STYLE (style));
4107   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_option != NULL);
4108   g_return_if_fail (cr != NULL);
4109
4110   cairo_save (cr);
4111
4112   GTK_STYLE_GET_CLASS (style)->draw_option (style, cr, state_type, shadow_type,
4113                                             widget, detail,
4114                                             x, y, width, height);
4115
4116   cairo_restore (cr);
4117 }
4118
4119 /**
4120  * gtk_paint_tab:
4121  * @style: a #GtkStyle
4122  * @cr: a #cairo_t
4123  * @state_type: a state
4124  * @shadow_type: the type of shadow to draw
4125  * @widget: (allow-none): the widget
4126  * @detail: (allow-none): a style detail
4127  * @x: x origin of the rectangle to draw the tab in
4128  * @y: y origin of the rectangle to draw the tab in
4129  * @width: the width of the rectangle to draw the tab in
4130  * @height: the height of the rectangle to draw the tab in
4131  *
4132  * Draws an option menu tab (i.e. the up and down pointing arrows)
4133  * in the given rectangle on @cr using the given parameters.
4134  *
4135  * Deprecated:3.0: Use cairo instead
4136  */
4137 void
4138 gtk_paint_tab (GtkStyle           *style,
4139                cairo_t            *cr,
4140                GtkStateType        state_type,
4141                GtkShadowType       shadow_type,
4142                GtkWidget          *widget,
4143                const gchar        *detail,
4144                gint                x,
4145                gint                y,
4146                gint                width,
4147                gint                height)
4148 {
4149   g_return_if_fail (GTK_IS_STYLE (style));
4150   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_tab != NULL);
4151   g_return_if_fail (cr != NULL);
4152
4153   cairo_save (cr);
4154
4155   GTK_STYLE_GET_CLASS (style)->draw_tab (style, cr, state_type, shadow_type,
4156                                          widget, detail,
4157                                          x, y, width, height);
4158
4159   cairo_restore (cr);
4160 }
4161
4162 /**
4163  * gtk_paint_shadow_gap:
4164  * @style: a #GtkStyle
4165  * @cr: a #cairo_t
4166  * @state_type: a state
4167  * @shadow_type: type of shadow to draw
4168  * @widget: (allow-none): the widget
4169  * @detail: (allow-none): a style detail
4170  * @x: x origin of the rectangle
4171  * @y: y origin of the rectangle
4172  * @width: width of the rectangle
4173  * @height: width of the rectangle
4174  * @gap_side: side in which to leave the gap
4175  * @gap_x: starting position of the gap
4176  * @gap_width: width of the gap
4177  *
4178  * Draws a shadow around the given rectangle in @cr
4179  * using the given style and state and shadow type, leaving a
4180  * gap in one side.
4181  *
4182  * Deprecated:3.0: Use gtk_render_frame_gap() instead
4183  */
4184 void
4185 gtk_paint_shadow_gap (GtkStyle           *style,
4186                       cairo_t            *cr,
4187                       GtkStateType        state_type,
4188                       GtkShadowType       shadow_type,
4189                       GtkWidget          *widget,
4190                       const gchar        *detail,
4191                       gint                x,
4192                       gint                y,
4193                       gint                width,
4194                       gint                height,
4195                       GtkPositionType     gap_side,
4196                       gint                gap_x,
4197                       gint                gap_width)
4198 {
4199   g_return_if_fail (GTK_IS_STYLE (style));
4200   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow_gap != NULL);
4201   g_return_if_fail (cr != NULL);
4202   g_return_if_fail (width >= 0);
4203   g_return_if_fail (height >= 0);
4204
4205   cairo_save (cr);
4206
4207   GTK_STYLE_GET_CLASS (style)->draw_shadow_gap (style, cr, state_type, shadow_type,
4208                                                 widget, detail,
4209                                                 x, y, width, height, gap_side, gap_x, gap_width);
4210
4211   cairo_restore (cr);
4212 }
4213
4214 /**
4215  * gtk_paint_box_gap:
4216  * @style: a #GtkStyle
4217  * @cr: a #cairo_t
4218  * @state_type: a state
4219  * @shadow_type: type of shadow to draw
4220  * @widget: (allow-none): the widget
4221  * @detail: (allow-none): a style detail
4222  * @x: x origin of the rectangle
4223  * @y: y origin of the rectangle
4224  * @width: width of the rectangle
4225  * @height: width of the rectangle
4226  * @gap_side: side in which to leave the gap
4227  * @gap_x: starting position of the gap
4228  * @gap_width: width of the gap
4229  *
4230  * Draws a box in @cr using the given style and state and shadow type,
4231  * leaving a gap in one side.
4232  *
4233  * Deprecated:3.0: Use gtk_render_frame_gap() instead
4234  */
4235 void
4236 gtk_paint_box_gap (GtkStyle           *style,
4237                    cairo_t            *cr,
4238                    GtkStateType        state_type,
4239                    GtkShadowType       shadow_type,
4240                    GtkWidget          *widget,
4241                    const gchar        *detail,
4242                    gint                x,
4243                    gint                y,
4244                    gint                width,
4245                    gint                height,
4246                    GtkPositionType     gap_side,
4247                    gint                gap_x,
4248                    gint                gap_width)
4249 {
4250   g_return_if_fail (GTK_IS_STYLE (style));
4251   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box_gap != NULL);
4252   g_return_if_fail (cr != NULL);
4253   g_return_if_fail (width >= 0);
4254   g_return_if_fail (height >= 0);
4255
4256   cairo_save (cr);
4257
4258   GTK_STYLE_GET_CLASS (style)->draw_box_gap (style, cr, state_type, shadow_type,
4259                                              widget, detail,
4260                                              x, y, width, height, gap_side, gap_x, gap_width);
4261
4262   cairo_restore (cr);
4263 }
4264
4265 /**
4266  * gtk_paint_extension:
4267  * @style: a #GtkStyle
4268  * @cr: a #cairo_t
4269  * @state_type: a state
4270  * @shadow_type: type of shadow to draw
4271  * @widget: (allow-none): the widget
4272  * @detail: (allow-none): a style detail
4273  * @x: x origin of the extension
4274  * @y: y origin of the extension
4275  * @width: width of the extension
4276  * @height: width of the extension
4277  * @gap_side: the side on to which the extension is attached
4278  *
4279  * Draws an extension, i.e. a notebook tab.
4280  *
4281  * Deprecated:3.0: Use gtk_render_extension() instead
4282  **/
4283 void
4284 gtk_paint_extension (GtkStyle           *style,
4285                      cairo_t            *cr,
4286                      GtkStateType        state_type,
4287                      GtkShadowType       shadow_type,
4288                      GtkWidget          *widget,
4289                      const gchar        *detail,
4290                      gint                x,
4291                      gint                y,
4292                      gint                width,
4293                      gint                height,
4294                      GtkPositionType     gap_side)
4295 {
4296   g_return_if_fail (GTK_IS_STYLE (style));
4297   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_extension != NULL);
4298   g_return_if_fail (cr != NULL);
4299   g_return_if_fail (width >= 0);
4300   g_return_if_fail (height >= 0);
4301
4302   cairo_save (cr);
4303
4304   GTK_STYLE_GET_CLASS (style)->draw_extension (style, cr, state_type, shadow_type,
4305                                                widget, detail,
4306                                                x, y, width, height, gap_side);
4307
4308   cairo_restore (cr);
4309 }
4310
4311 /**
4312  * gtk_paint_focus:
4313  * @style: a #GtkStyle
4314  * @cr: a #cairo_t
4315  * @state_type: a state
4316  * @widget: (allow-none): the widget
4317  * @detail: (allow-none): a style detail
4318  * @x: the x origin of the rectangle around which to draw a focus indicator
4319  * @y: the y origin of the rectangle around which to draw a focus indicator
4320  * @width: the width of the rectangle around which to draw a focus indicator
4321  * @height: the height of the rectangle around which to draw a focus indicator
4322  *
4323  * Draws a focus indicator around the given rectangle on @cr using the
4324  * given style.
4325  *
4326  * Deprecated:3.0: Use gtk_render_focus() instead
4327  */
4328 void
4329 gtk_paint_focus (GtkStyle           *style,
4330                  cairo_t            *cr,
4331                  GtkStateType        state_type,
4332                  GtkWidget          *widget,
4333                  const gchar        *detail,
4334                  gint                x,
4335                  gint                y,
4336                  gint                width,
4337                  gint                height)
4338 {
4339   g_return_if_fail (GTK_IS_STYLE (style));
4340   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_focus != NULL);
4341   g_return_if_fail (cr != NULL);
4342   g_return_if_fail (width >= 0);
4343   g_return_if_fail (height >= 0);
4344
4345   cairo_save (cr);
4346
4347   GTK_STYLE_GET_CLASS (style)->draw_focus (style, cr, state_type,
4348                                            widget, detail,
4349                                            x, y, width, height);
4350
4351   cairo_restore (cr);
4352 }
4353
4354 /**
4355  * gtk_paint_slider:
4356  * @style: a #GtkStyle
4357  * @cr: a #cairo_t
4358  * @state_type: a state
4359  * @shadow_type: a shadow
4360  * @widget: (allow-none): the widget
4361  * @detail: (allow-none): a style detail
4362  * @x: the x origin of the rectangle in which to draw a slider
4363  * @y: the y origin of the rectangle in which to draw a slider
4364  * @width: the width of the rectangle in which to draw a slider
4365  * @height: the height of the rectangle in which to draw a slider
4366  * @orientation: the orientation to be used
4367  *
4368  * Draws a slider in the given rectangle on @cr using the
4369  * given style and orientation.
4370  *
4371  * Deprecated:3.0: Use gtk_render_slider() instead
4372  **/
4373 void
4374 gtk_paint_slider (GtkStyle           *style,
4375                   cairo_t            *cr,
4376                   GtkStateType        state_type,
4377                   GtkShadowType       shadow_type,
4378                   GtkWidget          *widget,
4379                   const gchar        *detail,
4380                   gint                x,
4381                   gint                y,
4382                   gint                width,
4383                   gint                height,
4384                   GtkOrientation      orientation)
4385 {
4386   g_return_if_fail (GTK_IS_STYLE (style));
4387   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_slider != NULL);
4388   g_return_if_fail (cr != NULL);
4389   g_return_if_fail (width >= 0);
4390   g_return_if_fail (height >= 0);
4391
4392   cairo_save (cr);
4393
4394   GTK_STYLE_GET_CLASS (style)->draw_slider (style, cr, state_type, shadow_type,
4395                                             widget, detail,
4396                                             x, y, width, height, orientation);
4397
4398   cairo_restore (cr);
4399 }
4400
4401 /**
4402  * gtk_paint_handle:
4403  * @style: a #GtkStyle
4404  * @cr: a #cairo_t
4405  * @state_type: a state
4406  * @shadow_type: type of shadow to draw
4407  * @widget: (allow-none): the widget
4408  * @detail: (allow-none): a style detail
4409  * @x: x origin of the handle
4410  * @y: y origin of the handle
4411  * @width: with of the handle
4412  * @height: height of the handle
4413  * @orientation: the orientation of the handle
4414  *
4415  * Draws a handle as used in #GtkHandleBox and #GtkPaned.
4416  *
4417  * Deprecated:3.0: Use gtk_render_handle() instead
4418  **/
4419 void
4420 gtk_paint_handle (GtkStyle           *style,
4421                   cairo_t            *cr,
4422                   GtkStateType        state_type,
4423                   GtkShadowType       shadow_type,
4424                   GtkWidget          *widget,
4425                   const gchar        *detail,
4426                   gint                x,
4427                   gint                y,
4428                   gint                width,
4429                   gint                height,
4430                   GtkOrientation      orientation)
4431 {
4432   g_return_if_fail (GTK_IS_STYLE (style));
4433   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_handle != NULL);
4434   g_return_if_fail (cr != NULL);
4435   g_return_if_fail (width >= 0);
4436   g_return_if_fail (height >= 0);
4437
4438   cairo_save (cr);
4439
4440   GTK_STYLE_GET_CLASS (style)->draw_handle (style, cr, state_type, shadow_type,
4441                                             widget, detail,
4442                                             x, y, width, height, orientation);
4443
4444   cairo_restore (cr);
4445 }
4446
4447 /**
4448  * gtk_paint_expander:
4449  * @style: a #GtkStyle
4450  * @cr: a #cairo_t
4451  * @state_type: a state
4452  * @widget: (allow-none): the widget
4453  * @detail: (allow-none): a style detail
4454  * @x: the x position to draw the expander at
4455  * @y: the y position to draw the expander at
4456  * @expander_style: the style to draw the expander in; determines
4457  *   whether the expander is collapsed, expanded, or in an
4458  *   intermediate state.
4459  *
4460  * Draws an expander as used in #GtkTreeView. @x and @y specify the
4461  * center the expander. The size of the expander is determined by the
4462  * "expander-size" style property of @widget.  (If widget is not
4463  * specified or doesn't have an "expander-size" property, an
4464  * unspecified default size will be used, since the caller doesn't
4465  * have sufficient information to position the expander, this is
4466  * likely not useful.) The expander is expander_size pixels tall
4467  * in the collapsed position and expander_size pixels wide in the
4468  * expanded position.
4469  *
4470  * Deprecated:3.0: Use gtk_render_expander() instead
4471  **/
4472 void
4473 gtk_paint_expander (GtkStyle           *style,
4474                     cairo_t            *cr,
4475                     GtkStateType        state_type,
4476                     GtkWidget          *widget,
4477                     const gchar        *detail,
4478                     gint                x,
4479                     gint                y,
4480                     GtkExpanderStyle    expander_style)
4481 {
4482   g_return_if_fail (GTK_IS_STYLE (style));
4483   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_expander != NULL);
4484   g_return_if_fail (cr != NULL);
4485
4486   cairo_save (cr);
4487
4488   GTK_STYLE_GET_CLASS (style)->draw_expander (style, cr, state_type,
4489                                               widget, detail,
4490                                               x, y, expander_style);
4491
4492   cairo_restore (cr);
4493 }
4494
4495 /**
4496  * gtk_paint_layout:
4497  * @style: a #GtkStyle
4498  * @cr: a #cairo_t
4499  * @state_type: a state
4500  * @use_text: whether to use the text or foreground
4501  *            graphics context of @style
4502  * @widget: (allow-none): the widget
4503  * @detail: (allow-none): a style detail
4504  * @x: x origin
4505  * @y: y origin
4506  * @layout: the layout to draw
4507  *
4508  * Draws a layout on @cr using the given parameters.
4509  *
4510  * Deprecated:3.0: Use gtk_render_layout() instead
4511  **/
4512 void
4513 gtk_paint_layout (GtkStyle           *style,
4514                   cairo_t            *cr,
4515                   GtkStateType        state_type,
4516                   gboolean            use_text,
4517                   GtkWidget          *widget,
4518                   const gchar        *detail,
4519                   gint                x,
4520                   gint                y,
4521                   PangoLayout        *layout)
4522 {
4523   g_return_if_fail (GTK_IS_STYLE (style));
4524   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_layout != NULL);
4525   g_return_if_fail (cr != NULL);
4526
4527   cairo_save (cr);
4528
4529   GTK_STYLE_GET_CLASS (style)->draw_layout (style, cr, state_type, use_text,
4530                                             widget, detail,
4531                                             x, y, layout);
4532
4533   cairo_restore (cr);
4534 }
4535
4536 /**
4537  * gtk_paint_resize_grip:
4538  * @style: a #GtkStyle
4539  * @cr: a #cairo_t
4540  * @state_type: a state
4541  * @widget: (allow-none): the widget
4542  * @detail: (allow-none): a style detail
4543  * @edge: the edge in which to draw the resize grip
4544  * @x: the x origin of the rectangle in which to draw the resize grip
4545  * @y: the y origin of the rectangle in which to draw the resize grip
4546  * @width: the width of the rectangle in which to draw the resize grip
4547  * @height: the height of the rectangle in which to draw the resize grip
4548  *
4549  * Draws a resize grip in the given rectangle on @cr using the given
4550  * parameters.
4551  *
4552  * Deprecated:3.0: Use gtk_render_handle() instead
4553  */
4554 void
4555 gtk_paint_resize_grip (GtkStyle           *style,
4556                        cairo_t            *cr,
4557                        GtkStateType        state_type,
4558                        GtkWidget          *widget,
4559                        const gchar        *detail,
4560                        GdkWindowEdge       edge,
4561                        gint                x,
4562                        gint                y,
4563                        gint                width,
4564                        gint                height)
4565 {
4566   g_return_if_fail (GTK_IS_STYLE (style));
4567   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_resize_grip != NULL);
4568   g_return_if_fail (cr != NULL);
4569
4570   cairo_save (cr);
4571
4572   GTK_STYLE_GET_CLASS (style)->draw_resize_grip (style, cr, state_type,
4573                                                  widget, detail,
4574                                                  edge, x, y, width, height);
4575   cairo_restore (cr);
4576 }
4577
4578 /**
4579  * gtk_paint_spinner:
4580  * @style: a #GtkStyle
4581  * @cr: a #cairo_t
4582  * @state_type: a state
4583  * @widget: (allow-none): the widget (may be %NULL)
4584  * @detail: (allow-none): a style detail (may be %NULL)
4585  * @step: the nth step, a value between 0 and #GtkSpinner:num-steps
4586  * @x: the x origin of the rectangle in which to draw the spinner
4587  * @y: the y origin of the rectangle in which to draw the spinner
4588  * @width: the width of the rectangle in which to draw the spinner
4589  * @height: the height of the rectangle in which to draw the spinner
4590  *
4591  * Draws a spinner on @window using the given parameters.
4592  *
4593  * Deprecated:3.0: Use gtk_render_activity() instead
4594  */
4595 void
4596 gtk_paint_spinner (GtkStyle           *style,
4597                    cairo_t            *cr,
4598                    GtkStateType        state_type,
4599                    GtkWidget          *widget,
4600                    const gchar        *detail,
4601                    guint               step,
4602                    gint                x,
4603                    gint                y,
4604                    gint                width,
4605                    gint                height)
4606 {
4607   g_return_if_fail (GTK_IS_STYLE (style));
4608   g_return_if_fail (cr != NULL);
4609   g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_spinner != NULL);
4610
4611   cairo_save (cr);
4612
4613   GTK_STYLE_GET_CLASS (style)->draw_spinner (style, cr, state_type,
4614                                              widget, detail,
4615                                              step, x, y, width, height);
4616
4617   cairo_restore (cr);
4618 }
4619
4620 typedef struct _CursorInfo CursorInfo;
4621
4622 struct _CursorInfo
4623 {
4624   GType for_type;
4625   GdkColor primary;
4626   GdkColor secondary;
4627 };
4628
4629 static void
4630 style_unrealize_cursors (GtkStyle *style)
4631 {
4632   CursorInfo *
4633   
4634   cursor_info = g_object_get_data (G_OBJECT (style), "gtk-style-cursor-info");
4635   if (cursor_info)
4636     {
4637       g_free (cursor_info);
4638       g_object_set_data (G_OBJECT (style), I_("gtk-style-cursor-info"), NULL);
4639     }
4640 }
4641
4642 static const GdkColor *
4643 get_insertion_cursor_color (GtkWidget *widget,
4644                             gboolean   is_primary)
4645 {
4646   CursorInfo *cursor_info;
4647   GtkStyle *style;
4648   GdkColor *cursor_color;
4649
4650   style = gtk_widget_get_style (widget);
4651
4652   cursor_info = g_object_get_data (G_OBJECT (style), "gtk-style-cursor-info");
4653   if (!cursor_info)
4654     {
4655       cursor_info = g_new0 (CursorInfo, 1);
4656       g_object_set_data (G_OBJECT (style), I_("gtk-style-cursor-info"), cursor_info);
4657       cursor_info->for_type = G_TYPE_INVALID;
4658     }
4659
4660   /* We have to keep track of the type because gtk_widget_style_get()
4661    * can return different results when called on the same property and
4662    * same style but for different widgets. :-(. That is,
4663    * GtkEntry::cursor-color = "red" in a style will modify the cursor
4664    * color for entries but not for text view.
4665    */
4666   if (cursor_info->for_type != G_OBJECT_TYPE (widget))
4667     {
4668       cursor_info->for_type = G_OBJECT_TYPE (widget);
4669
4670       /* Cursors in text widgets are drawn only in NORMAL state,
4671        * so we can use text[GTK_STATE_NORMAL] as text color here */
4672       gtk_widget_style_get (widget, "cursor-color", &cursor_color, NULL);
4673       if (cursor_color)
4674         {
4675           cursor_info->primary = *cursor_color;
4676           gdk_color_free (cursor_color);
4677         }
4678       else
4679         {
4680           cursor_info->primary = style->text[GTK_STATE_NORMAL];
4681         }
4682
4683       gtk_widget_style_get (widget, "secondary-cursor-color", &cursor_color, NULL);
4684       if (cursor_color)
4685         {
4686           cursor_info->secondary = *cursor_color;
4687           gdk_color_free (cursor_color);
4688         }
4689       else
4690         {
4691           /* text_aa is the average of text and base colors,
4692            * in usual black-on-white case it's grey. */
4693           cursor_info->secondary = style->text_aa[GTK_STATE_NORMAL];
4694         }
4695     }
4696
4697   if (is_primary)
4698     return &cursor_info->primary;
4699   else
4700     return &cursor_info->secondary;
4701 }
4702
4703 void
4704 _gtk_widget_get_cursor_color (GtkWidget *widget,
4705                               GdkColor  *color)
4706 {
4707   GdkColor *style_color;
4708
4709   g_return_if_fail (GTK_IS_WIDGET (widget));
4710   g_return_if_fail (color != NULL);
4711
4712   gtk_widget_style_get (widget, "cursor-color", &style_color, NULL);
4713
4714   if (style_color)
4715     {
4716       *color = *style_color;
4717       gdk_color_free (style_color);
4718     }
4719   else
4720     *color = gtk_widget_get_style (widget)->text[GTK_STATE_NORMAL];
4721 }
4722
4723 /**
4724  * gtk_draw_insertion_cursor:
4725  * @widget:  a #GtkWidget
4726  * @cr: cairo context to draw to
4727  * @location: location where to draw the cursor (@location->width is ignored)
4728  * @is_primary: if the cursor should be the primary cursor color.
4729  * @direction: whether the cursor is left-to-right or
4730  *             right-to-left. Should never be #GTK_TEXT_DIR_NONE
4731  * @draw_arrow: %TRUE to draw a directional arrow on the
4732  *        cursor. Should be %FALSE unless the cursor is split.
4733  *
4734  * Draws a text caret on @cr at @location. This is not a style function
4735  * but merely a convenience function for drawing the standard cursor shape.
4736  *
4737  * Since: 3.0
4738  **/
4739 void
4740 gtk_draw_insertion_cursor (GtkWidget          *widget,
4741                            cairo_t            *cr,
4742                            const GdkRectangle *location,
4743                            gboolean            is_primary,
4744                            GtkTextDirection    direction,
4745                            gboolean            draw_arrow)
4746 {
4747   gint stem_width;
4748   gint arrow_width;
4749   gint x, y;
4750   gfloat cursor_aspect_ratio;
4751   gint offset;
4752   
4753   g_return_if_fail (GTK_IS_WIDGET (widget));
4754   g_return_if_fail (cr != NULL);
4755   g_return_if_fail (location != NULL);
4756   g_return_if_fail (direction != GTK_TEXT_DIR_NONE);
4757
4758   gdk_cairo_set_source_color (cr, get_insertion_cursor_color (widget, is_primary));
4759
4760   /* When changing the shape or size of the cursor here,
4761    * propagate the changes to gtktextview.c:text_window_invalidate_cursors().
4762    */
4763
4764   gtk_widget_style_get (widget, "cursor-aspect-ratio", &cursor_aspect_ratio, NULL);
4765   
4766   stem_width = location->height * cursor_aspect_ratio + 1;
4767   arrow_width = stem_width + 1;
4768
4769   /* put (stem_width % 2) on the proper side of the cursor */
4770   if (direction == GTK_TEXT_DIR_LTR)
4771     offset = stem_width / 2;
4772   else
4773     offset = stem_width - stem_width / 2;
4774   
4775   cairo_rectangle (cr, 
4776                    location->x - offset, location->y,
4777                    stem_width, location->height);
4778   cairo_fill (cr);
4779
4780   if (draw_arrow)
4781     {
4782       if (direction == GTK_TEXT_DIR_RTL)
4783         {
4784           x = location->x - offset - 1;
4785           y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
4786   
4787           cairo_move_to (cr, x, y + 1);
4788           cairo_line_to (cr, x - arrow_width, y + arrow_width);
4789           cairo_line_to (cr, x, y + 2 * arrow_width);
4790           cairo_fill (cr);
4791         }
4792       else if (direction == GTK_TEXT_DIR_LTR)
4793         {
4794           x = location->x + stem_width - offset;
4795           y = location->y + location->height - arrow_width * 2 - arrow_width + 1;
4796   
4797           cairo_move_to (cr, x, y + 1);
4798           cairo_line_to (cr, x + arrow_width, y + arrow_width);
4799           cairo_line_to (cr, x, y + 2 * arrow_width);
4800           cairo_fill (cr);
4801         }
4802     }
4803 }