gtkstyle.c 176 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10 11
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18
 */
19 20

/*
21
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include "config.h"
Elliot Lee's avatar
Elliot Lee committed
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"
Elliot Lee's avatar
Elliot Lee committed
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 "gtkspinner.h"
Elliot Lee's avatar
Elliot Lee committed
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

/**
 * SECTION:gtkstyle
 * @Short_description: An object that hold style information for widgets
 * @Title: GtkStyle
 *
 * A #GtkStyle object encapsulates the information that provides the look and
 * feel for a widget. Each #GtkWidget has an associated #GTkStyle object that
 * is used when rendering that widget. Also, a #GtkStyle holds information for
 * the five possible widget states though not every widget supports all five
 * states; see #GtkStateType.
 *
 * Usually the #GtkStyle for a widget is the same as the default style that is
 * set by GTK+ and modified the theme engine.
 *
 * Usually applications should not need to use or modify the #GtkStyle of their
 * widgets.
 */


Elliot Lee's avatar
Elliot Lee committed
64 65 66
#define LIGHTNESS_MULT  1.3
#define DARKNESS_MULT   0.7

67 68 69 70 71 72 73
/* --- typedefs & structures --- */
typedef struct {
  GType       widget_type;
  GParamSpec *pspec;
  GValue      value;
} PropertyValue;

74 75 76 77 78 79 80 81
#define GTK_STYLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_STYLE, GtkStylePrivate))

typedef struct _GtkStylePrivate GtkStylePrivate;

struct _GtkStylePrivate {
  GSList *color_hashes;
};

82 83 84 85 86 87 88 89 90 91 92 93 94 95
/* --- prototypes --- */
static void	 gtk_style_finalize		(GObject	*object);
static void	 gtk_style_realize		(GtkStyle	*style,
						 GdkColormap	*colormap);
static void      gtk_style_real_realize        (GtkStyle	*style);
static void      gtk_style_real_unrealize      (GtkStyle	*style);
static void      gtk_style_real_copy           (GtkStyle	*style,
						GtkStyle	*src);
static void      gtk_style_real_set_background (GtkStyle	*style,
						GdkWindow	*window,
						GtkStateType	 state_type);
static GtkStyle *gtk_style_real_clone          (GtkStyle	*style);
static void      gtk_style_real_init_from_rc   (GtkStyle	*style,
                                                GtkRcStyle	*rc_style);
96 97 98 99
static GdkPixbuf *gtk_default_render_icon      (GtkStyle            *style,
                                                const GtkIconSource *source,
                                                GtkTextDirection     direction,
                                                GtkStateType         state,
100
                                                GtkIconSize          size,
101 102
                                                GtkWidget           *widget,
                                                const gchar         *detail);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
static void gtk_default_draw_hline      (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x1,
					 gint             x2,
					 gint             y);
static void gtk_default_draw_vline      (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             y1,
					 gint             y2,
					 gint             x);
static void gtk_default_draw_shadow     (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_arrow      (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 GtkArrowType     arrow_type,
					 gboolean         fill,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_diamond    (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_box        (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_flat_box   (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_check      (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_option     (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_tab        (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_shadow_gap (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height,
					 GtkPositionType  gap_side,
					 gint             gap_x,
					 gint             gap_width);
static void gtk_default_draw_box_gap    (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height,
					 GtkPositionType  gap_side,
					 gint             gap_x,
					 gint             gap_width);
static void gtk_default_draw_extension  (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height,
					 GtkPositionType  gap_side);
static void gtk_default_draw_focus      (GtkStyle        *style,
					 GdkWindow       *window,
253
					 GtkStateType     state_type,
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height);
static void gtk_default_draw_slider     (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height,
					 GtkOrientation   orientation);
static void gtk_default_draw_handle     (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 gint             width,
					 gint             height,
					 GtkOrientation   orientation);
285 286 287 288 289 290 291 292
static void gtk_default_draw_expander   (GtkStyle        *style,
                                         GdkWindow       *window,
                                         GtkStateType     state_type,
                                         GdkRectangle    *area,
                                         GtkWidget       *widget,
                                         const gchar     *detail,
                                         gint             x,
                                         gint             y,
293
					 GtkExpanderStyle expander_style);
294 295 296
static void gtk_default_draw_layout     (GtkStyle        *style,
                                         GdkWindow       *window,
                                         GtkStateType     state_type,
297
					 gboolean         use_text,
298 299 300 301 302 303
                                         GdkRectangle    *area,
                                         GtkWidget       *widget,
                                         const gchar     *detail,
                                         gint             x,
                                         gint             y,
                                         PangoLayout     *layout);
Owen Taylor's avatar
Owen Taylor committed
304 305 306 307 308 309 310 311 312 313 314
static void gtk_default_draw_resize_grip (GtkStyle       *style,
                                          GdkWindow      *window,
                                          GtkStateType    state_type,
                                          GdkRectangle   *area,
                                          GtkWidget      *widget,
                                          const gchar    *detail,
                                          GdkWindowEdge   edge,
                                          gint            x,
                                          gint            y,
                                          gint            width,
                                          gint            height);
315 316 317 318 319 320 321 322 323 324 325
static void gtk_default_draw_spinner     (GtkStyle       *style,
					  GdkWindow      *window,
					  GtkStateType    state_type,
                                          GdkRectangle   *area,
                                          GtkWidget      *widget,
                                          const gchar    *detail,
					  guint           step,
					  gint            x,
					  gint            y,
					  gint            width,
					  gint            height);
Owen Taylor's avatar
Owen Taylor committed
326

327 328 329 330 331 332 333
static void rgb_to_hls			(gdouble	 *r,
					 gdouble	 *g,
					 gdouble	 *b);
static void hls_to_rgb			(gdouble	 *h,
					 gdouble	 *l,
					 gdouble	 *s);

334
static void style_unrealize_cursors     (GtkStyle *style);
335

336 337 338 339
/*
 * Data for default check and radio buttons
 */

340 341
static const GtkRequisition default_option_indicator_size = { 7, 13 };
static const GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };
342

343 344 345 346 347 348 349 350
#define GTK_GRAY		0xdcdc, 0xdada, 0xd5d5
#define GTK_DARK_GRAY		0xc4c4, 0xc2c2, 0xbdbd
#define GTK_LIGHT_GRAY		0xeeee, 0xebeb, 0xe7e7
#define GTK_WHITE		0xffff, 0xffff, 0xffff
#define GTK_BLUE		0x4b4b, 0x6969, 0x8383
#define GTK_VERY_DARK_GRAY	0x9c9c, 0x9a9a, 0x9494
#define GTK_BLACK		0x0000, 0x0000, 0x0000
#define GTK_WEAK_GRAY		0x7530, 0x7530, 0x7530
351

352
/* --- variables --- */
353 354 355 356 357
static const GdkColor gtk_default_normal_fg =      { 0, GTK_BLACK };
static const GdkColor gtk_default_active_fg =      { 0, GTK_BLACK };
static const GdkColor gtk_default_prelight_fg =    { 0, GTK_BLACK };
static const GdkColor gtk_default_selected_fg =    { 0, GTK_WHITE };
static const GdkColor gtk_default_insensitive_fg = { 0, GTK_WEAK_GRAY };
358

359 360 361 362 363 364 365
static const GdkColor gtk_default_normal_bg =      { 0, GTK_GRAY };
static const GdkColor gtk_default_active_bg =      { 0, GTK_DARK_GRAY };
static const GdkColor gtk_default_prelight_bg =    { 0, GTK_LIGHT_GRAY };
static const GdkColor gtk_default_selected_bg =    { 0, GTK_BLUE };
static const GdkColor gtk_default_insensitive_bg = { 0, GTK_GRAY };
static const GdkColor gtk_default_selected_base =  { 0, GTK_BLUE };
static const GdkColor gtk_default_active_base =    { 0, GTK_VERY_DARK_GRAY };
Elliot Lee's avatar
Elliot Lee committed
366

367 368 369
/* --- signals --- */
static guint realize_signal = 0;
static guint unrealize_signal = 0;
370

Matthias Clasen's avatar
Matthias Clasen committed
371
G_DEFINE_TYPE (GtkStyle, gtk_style, G_TYPE_OBJECT)
Matthias Clasen's avatar
Matthias Clasen committed
372

373
/* --- functions --- */
Elliot Lee's avatar
Elliot Lee committed
374

375 376 377 378 379
/**
 * _gtk_style_init_for_settings:
 * @style: a #GtkStyle
 * @settings: a #GtkSettings
 * 
Matthias Clasen's avatar
Matthias Clasen committed
380
 * Initializes the font description in @style according to the default
381 382 383 384 385 386 387 388 389 390
 * font name of @settings. This is called for gtk_style_new() with
 * the settings for the default screen (if any); if we are creating
 * a style for a particular screen, we then call it again in a
 * location where we know the correct settings.
 * The reason for this is that gtk_rc_style_create_style() doesn't
 * take the screen for an argument.
 **/
void
_gtk_style_init_for_settings (GtkStyle    *style,
			      GtkSettings *settings)
Elliot Lee's avatar
Elliot Lee committed
391
{
392
  const gchar *font_name = _gtk_rc_context_get_default_font_name (settings);
393

394 395 396
  if (style->font_desc)
    pango_font_description_free (style->font_desc);
  
397
  style->font_desc = pango_font_description_from_string (font_name);
398
      
399 400 401 402 403 404 405 406 407 408
  if (!pango_font_description_get_family (style->font_desc))
    {
      g_warning ("Default font does not have a family set");
      pango_font_description_set_family (style->font_desc, "Sans");
    }
  if (pango_font_description_get_size (style->font_desc) <= 0)
    {
      g_warning ("Default font does not have a positive size");
      pango_font_description_set_size (style->font_desc, 10 * PANGO_SCALE);
    }
409 410 411 412 413 414 415 416 417 418 419 420 421
}

static void
gtk_style_init (GtkStyle *style)
{
  gint i;
  
  GtkSettings *settings = gtk_settings_get_default ();
  
  if (settings)
    _gtk_style_init_for_settings (style, settings);
  else
    style->font_desc = pango_font_description_from_string ("Sans 10");
422
  
Elliot Lee's avatar
Elliot Lee committed
423 424 425
  style->attach_count = 0;
  style->colormap = NULL;
  style->depth = -1;
426
  
Elliot Lee's avatar
Elliot Lee committed
427 428 429
  style->black.red = 0;
  style->black.green = 0;
  style->black.blue = 0;
430
  
Elliot Lee's avatar
Elliot Lee committed
431 432 433
  style->white.red = 65535;
  style->white.green = 65535;
  style->white.blue = 65535;
434
  
Elliot Lee's avatar
Elliot Lee committed
435 436 437 438 439
  style->fg[GTK_STATE_NORMAL] = gtk_default_normal_fg;
  style->fg[GTK_STATE_ACTIVE] = gtk_default_active_fg;
  style->fg[GTK_STATE_PRELIGHT] = gtk_default_prelight_fg;
  style->fg[GTK_STATE_SELECTED] = gtk_default_selected_fg;
  style->fg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
440
  
Elliot Lee's avatar
Elliot Lee committed
441 442 443 444 445
  style->bg[GTK_STATE_NORMAL] = gtk_default_normal_bg;
  style->bg[GTK_STATE_ACTIVE] = gtk_default_active_bg;
  style->bg[GTK_STATE_PRELIGHT] = gtk_default_prelight_bg;
  style->bg[GTK_STATE_SELECTED] = gtk_default_selected_bg;
  style->bg[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_bg;
446
  
447
  for (i = 0; i < 4; i++)
Elliot Lee's avatar
Elliot Lee committed
448 449 450 451
    {
      style->text[i] = style->fg[i];
      style->base[i] = style->white;
    }
Havoc Pennington's avatar
Havoc Pennington committed
452

453
  style->base[GTK_STATE_SELECTED] = gtk_default_selected_base;
454
  style->text[GTK_STATE_SELECTED] = style->white;
455
  style->base[GTK_STATE_ACTIVE] = gtk_default_active_base;
456
  style->text[GTK_STATE_ACTIVE] = style->white;
Owen Taylor's avatar
Owen Taylor committed
457 458
  style->base[GTK_STATE_INSENSITIVE] = gtk_default_prelight_bg;
  style->text[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
459
  
Elliot Lee's avatar
Elliot Lee committed
460 461
  for (i = 0; i < 5; i++)
    style->bg_pixmap[i] = NULL;
462
  
463
  style->rc_style = NULL;
464
  
465 466
  style->xthickness = 2;
  style->ythickness = 2;
467 468

  style->property_cache = NULL;
469 470 471 472 473 474 475 476 477
}

static void
gtk_style_class_init (GtkStyleClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  object_class->finalize = gtk_style_finalize;

478 479 480 481 482 483
  klass->clone = gtk_style_real_clone;
  klass->copy = gtk_style_real_copy;
  klass->init_from_rc = gtk_style_real_init_from_rc;
  klass->realize = gtk_style_real_realize;
  klass->unrealize = gtk_style_real_unrealize;
  klass->set_background = gtk_style_real_set_background;
484 485
  klass->render_icon = gtk_default_render_icon;

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
  klass->draw_hline = gtk_default_draw_hline;
  klass->draw_vline = gtk_default_draw_vline;
  klass->draw_shadow = gtk_default_draw_shadow;
  klass->draw_arrow = gtk_default_draw_arrow;
  klass->draw_diamond = gtk_default_draw_diamond;
  klass->draw_box = gtk_default_draw_box;
  klass->draw_flat_box = gtk_default_draw_flat_box;
  klass->draw_check = gtk_default_draw_check;
  klass->draw_option = gtk_default_draw_option;
  klass->draw_tab = gtk_default_draw_tab;
  klass->draw_shadow_gap = gtk_default_draw_shadow_gap;
  klass->draw_box_gap = gtk_default_draw_box_gap;
  klass->draw_extension = gtk_default_draw_extension;
  klass->draw_focus = gtk_default_draw_focus;
  klass->draw_slider = gtk_default_draw_slider;
  klass->draw_handle = gtk_default_draw_handle;
502 503
  klass->draw_expander = gtk_default_draw_expander;
  klass->draw_layout = gtk_default_draw_layout;
Owen Taylor's avatar
Owen Taylor committed
504
  klass->draw_resize_grip = gtk_default_draw_resize_grip;
505
  klass->draw_spinner = gtk_default_draw_spinner;
506

507 508
  g_type_class_add_private (object_class, sizeof (GtkStylePrivate));

509 510 511 512 513 514 515 516
  /**
   * GtkStyle::realize:
   * @style: the object which received the signal
   *
   * Emitted when the style has been initialized for a particular
   * colormap and depth. Connecting to this signal is probably seldom
   * useful since most of the time applications and widgets only
   * deal with styles that have been already realized.
517 518
   *
   * Since: 2.4
519
   */
520
  realize_signal = g_signal_new (I_("realize"),
521 522 523 524 525 526 527 528 529 530 531 532
				 G_TYPE_FROM_CLASS (object_class),
				 G_SIGNAL_RUN_FIRST,
				 G_STRUCT_OFFSET (GtkStyleClass, realize),
				 NULL, NULL,
				 _gtk_marshal_VOID__VOID,
				 G_TYPE_NONE, 0);
  /**
   * GtkStyle::unrealize:
   * @style: the object which received the signal
   *
   * Emitted when the aspects of the style specific to a particular colormap
   * and depth are being cleaned up. A connection to this signal can be useful
533
   * if a widget wants to cache objects as object data on #GtkStyle.
534
   * This signal provides a convenient place to free such cached objects.
535 536
   *
   * Since: 2.4
537
   */
538
  unrealize_signal = g_signal_new (I_("unrealize"),
539 540 541 542 543 544
				   G_TYPE_FROM_CLASS (object_class),
				   G_SIGNAL_RUN_FIRST,
				   G_STRUCT_OFFSET (GtkStyleClass, unrealize),
				   NULL, NULL,
				   _gtk_marshal_VOID__VOID,
				   G_TYPE_NONE, 0);
545 546 547
}

static void
548
clear_property_cache (GtkStyle *style)
549
{
550 551 552 553
  if (style->property_cache)
    {
      guint i;

554
      for (i = 0; i < style->property_cache->len; i++)
555
	{
556
	  PropertyValue *node = &g_array_index (style->property_cache, PropertyValue, i);
557 558 559 560

	  g_param_spec_unref (node->pspec);
	  g_value_unset (&node->value);
	}
561
      g_array_free (style->property_cache, TRUE);
562 563
      style->property_cache = NULL;
    }
564 565 566 567 568 569
}

static void
gtk_style_finalize (GObject *object)
{
  GtkStyle *style = GTK_STYLE (object);
570
  GtkStylePrivate *priv = GTK_STYLE_GET_PRIVATE (style);
571 572 573 574

  g_return_if_fail (style->attach_count == 0);

  clear_property_cache (style);
575
  
576 577 578 579 580 581 582
  /* All the styles in the list have the same 
   * style->styles pointer. If we delete the 
   * *first* style from the list, we need to update
   * the style->styles pointers from all the styles.
   * Otherwise we simply remove the node from
   * the list.
   */
583 584 585
  if (style->styles)
    {
      if (style->styles->data != style)
586
        style->styles = g_slist_remove (style->styles, style);
587 588 589 590 591 592
      else
        {
          GSList *tmp_list = style->styles->next;
	  
          while (tmp_list)
            {
593
              GTK_STYLE (tmp_list->data)->styles = style->styles->next;
594 595 596 597 598
              tmp_list = tmp_list->next;
            }
          g_slist_free_1 (style->styles);
        }
    }
599

600 601
  g_slist_foreach (style->icon_factories, (GFunc) g_object_unref, NULL);
  g_slist_free (style->icon_factories);
602

603 604
  g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
  g_slist_free (priv->color_hashes);
605

606
  pango_font_description_free (style->font_desc);
607 608 609

  if (style->private_font_desc)
    pango_font_description_free (style->private_font_desc);
610

611
  if (style->rc_style)
612 613
    g_object_unref (style->rc_style);

Matthias Clasen's avatar
Matthias Clasen committed
614
  G_OBJECT_CLASS (gtk_style_parent_class)->finalize (object);
615 616 617
}


Matthias Clasen's avatar
Matthias Clasen committed
618 619 620 621 622 623 624 625
/**
 * gtk_style_copy:
 * @style: a #GtkStyle
 *
 * Creates a copy of the passed in #GtkStyle object.
 *
 * Returns: a copy of @style
 */
626 627 628 629 630
GtkStyle*
gtk_style_copy (GtkStyle *style)
{
  GtkStyle *new_style;
  
631
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
632
  
633 634
  new_style = GTK_STYLE_GET_CLASS (style)->clone (style);
  GTK_STYLE_GET_CLASS (style)->copy (new_style, style);
635 636 637 638 639 640 641 642 643

  return new_style;
}

static GtkStyle*
gtk_style_duplicate (GtkStyle *style)
{
  GtkStyle *new_style;
  
644
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
645 646 647
  
  new_style = gtk_style_copy (style);
  
648 649 650 651 652
  /* All the styles in the list have the same 
   * style->styles pointer. When we insert a new 
   * style, we append it to the list to avoid having 
   * to update the existing ones. 
   */
653 654 655 656 657 658
  style->styles = g_slist_append (style->styles, new_style);
  new_style->styles = style->styles;  
  
  return new_style;
}

Matthias Clasen's avatar
Matthias Clasen committed
659 660 661 662 663 664
/**
 * gtk_style_new:
 * @returns: a new #GtkStyle.
 *
 * Creates a new #GtkStyle.
 **/
665 666 667 668 669
GtkStyle*
gtk_style_new (void)
{
  GtkStyle *style;
  
670
  style = g_object_new (GTK_TYPE_STYLE, NULL);
671
  
Elliot Lee's avatar
Elliot Lee committed
672 673 674
  return style;
}

Matthias Clasen's avatar
Matthias Clasen committed
675
/**
676
 * gtk_style_attach:
Matthias Clasen's avatar
Matthias Clasen committed
677
 * @style: a #GtkStyle.
678
 * @window: a #GdkWindow.
679
 *
Matthias Clasen's avatar
Matthias Clasen committed
680 681 682 683 684
 * Attaches a style to a window; this process allocates the
 * colors and creates the GC's for the style - it specializes
 * it to a particular visual and colormap. The process may 
 * involve the creation of a new style if the style has already 
 * been attached to a window with a different style and colormap.
685 686 687 688
 *
 * Since this function may return a new object, you have to use it 
 * in the following way: 
 * <literal>style = gtk_style_attach (style, window)</literal>
689 690 691 692 693 694
 *
 * Returns: Either @style, or a newly-created #GtkStyle.
 *   If the style is newly created, the style parameter
 *   will be unref'ed, and the new style will have
 *   a reference count belonging to the caller.
 */
695
GtkStyle*
Elliot Lee's avatar
Elliot Lee committed
696
gtk_style_attach (GtkStyle  *style,
697
                  GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
698
{
699 700
  GSList *styles;
  GtkStyle *new_style = NULL;
Elliot Lee's avatar
Elliot Lee committed
701
  GdkColormap *colormap;
702
  
703
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
Elliot Lee's avatar
Elliot Lee committed
704
  g_return_val_if_fail (window != NULL, NULL);
705
  
Manish Singh's avatar
Manish Singh committed
706
  colormap = gdk_drawable_get_colormap (window);
707
  
708 709
  if (!style->styles)
    style->styles = g_slist_append (NULL, style);
710
  
711 712
  styles = style->styles;
  while (styles)
Elliot Lee's avatar
Elliot Lee committed
713
    {
714
      new_style = styles->data;
715
      
716
      if (new_style->colormap == colormap)
717
        break;
718

719 720
      new_style = NULL;
      styles = styles->next;
Elliot Lee's avatar
Elliot Lee committed
721
    }
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

  if (!new_style)
    {
      styles = style->styles;
      
      while (styles)
	{
	  new_style = styles->data;
	  
	  if (new_style->attach_count == 0)
	    {
	      gtk_style_realize (new_style, colormap);
	      break;
	    }
	  
	  new_style = NULL;
	  styles = styles->next;
	}
    }
741
  
742 743 744
  if (!new_style)
    {
      new_style = gtk_style_duplicate (style);
745
      gtk_style_realize (new_style, colormap);
746
    }
747 748

  /* A style gets a refcount from being attached */
749
  if (new_style->attach_count == 0)
750
    g_object_ref (new_style);
751 752 753 754

  /* Another refcount belongs to the parent */
  if (style != new_style) 
    {
755 756
      g_object_unref (style);
      g_object_ref (new_style);
757
    }
758
  
759
  new_style->attach_count++;
760
  
761
  return new_style;
Elliot Lee's avatar
Elliot Lee committed
762 763
}

764 765 766 767 768 769 770 771
/**
 * gtk_style_detach:
 * @style: a #GtkStyle
 *
 * Detaches a style from a window. If the style is not attached
 * to any windows anymore, it is unrealized. See gtk_style_attach().
 * 
 */
Elliot Lee's avatar
Elliot Lee committed
772 773 774
void
gtk_style_detach (GtkStyle *style)
{
775
  g_return_if_fail (GTK_IS_STYLE (style));
776
  g_return_if_fail (style->attach_count > 0);
777
  
Elliot Lee's avatar
Elliot Lee committed
778 779 780
  style->attach_count -= 1;
  if (style->attach_count == 0)
    {
781
      g_signal_emit (style, unrealize_signal, 0);
782
      
Manish Singh's avatar
Manish Singh committed
783
      g_object_unref (style->colormap);
784
      style->colormap = NULL;
785 786 787 788 789 790 791

      if (style->private_font_desc)
	{
	  pango_font_description_free (style->private_font_desc);
	  style->private_font_desc = NULL;
	}

792
      g_object_unref (style);
Elliot Lee's avatar
Elliot Lee committed
793 794 795
    }
}

796
static void
797
gtk_style_realize (GtkStyle    *style,
798
                   GdkColormap *colormap)
799
{
Manish Singh's avatar
Manish Singh committed
800
  style->colormap = g_object_ref (colormap);
801 802
  style->depth = gdk_colormap_get_visual (colormap)->depth;

803
  g_signal_emit (style, realize_signal, 0);
804 805
}

Matthias Clasen's avatar
Matthias Clasen committed
806 807 808 809 810 811 812 813 814 815 816
/**
 * gtk_style_lookup_icon_set:
 * @style: a #GtkStyle
 * @stock_id: an icon name
 *
 * Looks up @stock_id in the icon factories associated with @style
 * and the default icon factory, returning an icon set if found,
 * otherwise %NULL.
 *
 * Return value: icon set of @stock_id
 */
817 818 819 820 821 822 823 824 825 826 827 828
GtkIconSet*
gtk_style_lookup_icon_set (GtkStyle   *style,
                           const char *stock_id)
{
  GSList *iter;

  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
  g_return_val_if_fail (stock_id != NULL, NULL);
  
  iter = style->icon_factories;
  while (iter != NULL)
    {
829 830
      GtkIconSet *icon_set = gtk_icon_factory_lookup (GTK_ICON_FACTORY (iter->data),
						      stock_id);
831 832 833 834 835 836 837 838 839
      if (icon_set)
        return icon_set;
      
      iter = g_slist_next (iter);
    }

  return gtk_icon_factory_lookup_default (stock_id);
}

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
/**
 * gtk_style_lookup_color:
 * @style: a #GtkStyle
 * @color_name: the name of the logical color to look up
 * @color: the #GdkColor to fill in
 *
 * Looks up @color_name in the style's logical color mappings,
 * filling in @color and returning %TRUE if found, otherwise
 * returning %FALSE. Do not cache the found mapping, because
 * it depends on the #GtkStyle and might change when a theme
 * switch occurs.
 *
 * Return value: %TRUE if the mapping was found.
 *
 * Since: 2.10
 **/
gboolean
gtk_style_lookup_color (GtkStyle   *style,
                        const char *color_name,
                        GdkColor   *color)
{
  GtkStylePrivate *priv;
  GSList *iter;

  g_return_val_if_fail (GTK_IS_STYLE (style), FALSE);
  g_return_val_if_fail (color_name != NULL, FALSE);
  g_return_val_if_fail (color != NULL, FALSE);

  priv = GTK_STYLE_GET_PRIVATE (style);

  for (iter = priv->color_hashes; iter != NULL; iter = iter->next)
    {
      GHashTable *hash    = iter->data;
      GdkColor   *mapping = g_hash_table_lookup (hash, color_name);

      if (mapping)
        {
          color->red = mapping->red;
          color->green = mapping->green;
          color->blue = mapping->blue;
          return TRUE;
        }
    }

  return FALSE;
}

887 888 889 890 891 892 893 894 895
/**
 * gtk_style_set_background:
 * @style: a #GtkStyle
 * @window: a #GdkWindow
 * @state_type: a state
 * 
 * Sets the background of @window to the background color or pixmap
 * specified by @style for the given state.
 */
896
void
897 898 899
gtk_style_set_background (GtkStyle    *style,
                          GdkWindow   *window,
                          GtkStateType state_type)
900
{
901
  g_return_if_fail (GTK_IS_STYLE (style));
902 903
  g_return_if_fail (window != NULL);
  
904 905 906 907 908 909 910
  GTK_STYLE_GET_CLASS (style)->set_background (style, window, state_type);
}

/* Default functions */
static GtkStyle *
gtk_style_real_clone (GtkStyle *style)
{
911
  return g_object_new (G_OBJECT_TYPE (style), NULL);
912 913 914 915 916 917
}

static void
gtk_style_real_copy (GtkStyle *style,
		     GtkStyle *src)
{
918 919
  GtkStylePrivate *priv = GTK_STYLE_GET_PRIVATE (style);
  GtkStylePrivate *src_priv = GTK_STYLE_GET_PRIVATE (src);
920 921 922 923 924 925 926 927
  gint i;
  
  for (i = 0; i < 5; i++)
    {
      style->fg[i] = src->fg[i];
      style->bg[i] = src->bg[i];
      style->text[i] = src->text[i];
      style->base[i] = src->base[i];
928 929 930

      if (style->bg_pixmap[i])
	g_object_unref (style->bg_pixmap[i]),
931
      style->bg_pixmap[i] = src->bg_pixmap[i];
932 933
      if (style->bg_pixmap[i])
	g_object_ref (style->bg_pixmap[i]);
934 935 936 937 938 939 940 941 942 943 944 945 946
    }

  if (style->font_desc)
    pango_font_description_free (style->font_desc);
  if (src->font_desc)
    style->font_desc = pango_font_description_copy (src->font_desc);
  else
    style->font_desc = NULL;
  
  style->xthickness = src->xthickness;
  style->ythickness = src->ythickness;

  if (style->rc_style)
947
    g_object_unref (style->rc_style);
948 949
  style->rc_style = src->rc_style;
  if (src->rc_style)
950
    g_object_ref (src->rc_style);
951

952 953 954 955 956 957 958 959 960 961
  g_slist_foreach (style->icon_factories, (GFunc) g_object_unref, NULL);
  g_slist_free (style->icon_factories);
  style->icon_factories = g_slist_copy (src->icon_factories);
  g_slist_foreach (style->icon_factories, (GFunc) g_object_ref, NULL);

  g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_unref, NULL);
  g_slist_free (priv->color_hashes);
  priv->color_hashes = g_slist_copy (src_priv->color_hashes);
  g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_ref, NULL);

962
  /* don't copy, just clear cache */
963
  clear_property_cache (style);
964 965 966 967 968 969
}

static void
gtk_style_real_init_from_rc (GtkStyle   *style,
			     GtkRcStyle *rc_style)
{
970
  GtkStylePrivate *priv = GTK_STYLE_GET_PRIVATE (style);
971 972
  gint i;

973
  /* cache _should_ be still empty */
974
  clear_property_cache (style);
975

976
  if (rc_style->font_desc)
977
    pango_font_description_merge (style->font_desc, rc_style->font_desc, TRUE);
978 979
    
  for (i = 0; i < 5; i++)
980
    {
981 982 983 984 985 986 987 988 989 990 991 992 993 994
      if (rc_style->color_flags[i] & GTK_RC_FG)
	style->fg[i] = rc_style->fg[i];
      if (rc_style->color_flags[i] & GTK_RC_BG)
	style->bg[i] = rc_style->bg[i];
      if (rc_style->color_flags[i] & GTK_RC_TEXT)
	style->text[i] = rc_style->text[i];
      if (rc_style->color_flags[i] & GTK_RC_BASE)
	style->base[i] = rc_style->base[i];
    }

  if (rc_style->xthickness >= 0)
    style->xthickness = rc_style->xthickness;
  if (rc_style->ythickness >= 0)
    style->ythickness = rc_style->ythickness;
995

996 997
  style->icon_factories = g_slist_copy (rc_style->icon_factories);
  g_slist_foreach (style->icon_factories, (GFunc) g_object_ref, NULL);
998

999 1000
  priv->color_hashes = g_slist_copy (_gtk_rc_style_get_color_hashes (rc_style));
  g_slist_foreach (priv->color_hashes, (GFunc) g_hash_table_ref, NULL);
1001 1002
}

1003 1004 1005 1006 1007 1008 1009
static gint
style_property_values_cmp (gconstpointer bsearch_node1,
			   gconstpointer bsearch_node2)
{
  const PropertyValue *val1 = bsearch_node1;
  const PropertyValue *val2 = bsearch_node2;

1010 1011 1012 1013
  if (val1->widget_type == val2->widget_type)
    return val1->pspec < val2->pspec ? -1 : val1->pspec == val2->pspec ? 0 : 1;
  else
    return val1->widget_type < val2->widget_type ? -1 : 1;
1014 1015
}

1016
/**
1017
 * gtk_style_get_style_property:
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
 * @style: a #GtkStyle
 * @widget_type: the #GType of a descendant of #GtkWidget
 * @property_name: the name of the style property to get
 * @value: a #GValue where the value of the property being
 *     queried will be stored
 *
 * Queries the value of a style property corresponding to a
 * widget class is in the given style.
 *
 * Since: 2.16
 */
void 
1030 1031 1032 1033
gtk_style_get_style_property (GtkStyle     *style,
                              GType        widget_type,
                              const gchar *property_name,
                              GValue      *value)
1034 1035 1036 1037 1038 1039
{
  GtkWidgetClass *klass;
  GParamSpec *pspec;
  GtkRcPropertyParser parser;
  const GValue *peek_value;

1040
  klass = g_type_class_ref (widget_type);
1041
  pspec = gtk_widget_class_find_style_property (klass, property_name);
1042
  g_type_class_unref (klass);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

  if (!pspec)
    {
      g_warning ("%s: widget class `%s' has no property named `%s'",
                 G_STRLOC,
                 g_type_name (widget_type),
                 property_name);
      return;
    }

  parser = g_param_spec_get_qdata (pspec,
                                   g_quark_from_static_string ("gtk-rc-property-parser"));

  peek_value = _gtk_style_peek_property_value (style, widget_type, pspec, parser);

  if (G_VALUE_TYPE (value) == G_PARAM_SPEC_VALUE_TYPE (pspec))
    g_value_copy (peek_value, value);
  else if (g_value_type_transformable (G_PARAM_SPEC_VALUE_TYPE (pspec), G_VALUE_TYPE (value)))
    g_value_transform (peek_value, value);
  else
    g_warning ("can't retrieve style property `%s' of type `%s' as value of type `%s'",
               pspec->name,
               g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
               G_VALUE_TYPE_NAME (value));
}

/**
 * gtk_style_get_valist:
 * @style: a #GtkStyle
 * @widget_type: the #GType of a descendant of #GtkWidget
 * @first_property_name: the name of the first style property to get
Matthias Clasen's avatar
Matthias Clasen committed
1074
 * @var_args: a <type>va_list</type> of pairs of property names and
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
 *     locations to return the property values, starting with the
 *     location for @first_property_name.
 *
 * Non-vararg variant of gtk_style_get().
 * Used primarily by language bindings.
 *
 * Since: 2.16
 */
void 
gtk_style_get_valist (GtkStyle    *style,
                      GType        widget_type,
                      const gchar *first_property_name,
                      va_list      var_args)
{
  const char *property_name;
  GtkWidgetClass *klass;

  g_return_if_fail (GTK_IS_STYLE (style));

  klass = g_type_class_ref (widget_type);

  property_name = first_property_name;
  while (property_name)
    {
      GParamSpec *pspec;
      GtkRcPropertyParser parser;
      const GValue *peek_value;
      gchar *error;

      pspec = gtk_widget_class_find_style_property (klass, property_name);

      if (!pspec)
        {
          g_warning ("%s: widget class `%s' has no property named `%s'",
                     G_STRLOC,
                     g_type_name (widget_type),
                     property_name);
          break;
        }

      parser = g_param_spec_get_qdata (pspec,
                                       g_quark_from_static_string ("gtk-rc-property-parser"));

      peek_value = _gtk_style_peek_property_value (style, widget_type, pspec, parser);
      G_VALUE_LCOPY (peek_value, var_args, 0, &error);
      if (error)
        {
          g_warning ("%s: %s", G_STRLOC, error);
          g_free (error);
          break;
        }

      property_name = va_arg (var_args, gchar*);
    }

  g_type_class_unref (klass);
}

/**
 * gtk_style_get:
 * @style: a #GtkStyle
 * @widget_type: the #GType of a descendant of #GtkWidget
 * @first_property_name: the name of the first style property to get
 * @Varargs: pairs of property names and locations to
 *   return the property values, starting with the location for
 *   @first_property_name, terminated by %NULL.
 *
 * Gets the values of a multiple style properties for @widget_type
 * from @style.
1144 1145
 *
 * Since: 2.16
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
 */
void
gtk_style_get (GtkStyle    *style,
               GType        widget_type,
               const gchar *first_property_name,
               ...)
{
  va_list var_args;

  va_start (var_args, first_property_name);
  gtk_style_get_valist (style, widget_type, first_property_name, var_args);
  va_end (var_args);
}

1160 1161 1162 1163 1164 1165 1166 1167
const GValue*
_gtk_style_peek_property_value (GtkStyle           *style,
				GType               widget_type,
				GParamSpec         *pspec,
				GtkRcPropertyParser parser)
{
  PropertyValue *pcache, key = { 0, NULL, { 0, } };
  const GtkRcProperty *rcprop = NULL;
1168
  guint i;
1169 1170 1171 1172 1173 1174

  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
  g_return_val_if_fail (G_IS_PARAM_SPEC (pspec), NULL);
  g_return_val_if_fail (g_type_is_a (pspec->owner_type, GTK_TYPE_WIDGET), NULL);
  g_return_val_if_fail (g_type_is_a (widget_type, pspec->owner_type), NULL);

1175 1176 1177
  key.widget_type = widget_type;
  key.pspec = pspec;

1178 1179
  /* need value cache array */
  if (!style->property_cache)
1180
    style->property_cache = g_array_new (FALSE, FALSE, sizeof (PropertyValue));
1181 1182 1183 1184 1185 1186 1187 1188
  else
    {
      pcache = bsearch (&key,
			style->property_cache->data, style->property_cache->len,
			sizeof (PropertyValue), style_property_values_cmp);
      if (pcache)
	return &pcache->value;
    }
1189

1190 1191 1192 1193 1194 1195 1196 1197
  i = 0;
  while (i < style->property_cache->len &&
	 style_property_values_cmp (&key, &g_array_index (style->property_cache, PropertyValue, i)) >= 0)
    i++;

  g_array_insert_val (style->property_cache, i, key);
  pcache = &g_array_index (style->property_cache, PropertyValue, i);

1198
  /* cache miss, initialize value type, then set contents */
1199
  g_param_spec_ref (pcache->pspec);
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  g_value_init (&pcache->value, G_PARAM_SPEC_VALUE_TYPE (pspec));

  /* value provided by rc style? */
  if (style->rc_style)
    {
      GQuark prop_quark = g_quark_from_string (pspec->name);

      do
	{
	  rcprop = _gtk_rc_style_lookup_rc_property (style->rc_style,
						     g_type_qname (widget_type),
						     prop_quark);
	  if (rcprop)
	    break;
	  widget_type = g_type_parent (widget_type);
	}
      while (g_type_is_a (widget_type, pspec->owner_type));
    }

  /* when supplied by rc style, we need to convert */
1220 1221
  if (rcprop && !_gtk_settings_parse_convert (parser, &rcprop->value,
					      pspec, &pcache->value))
1222
    {
1223 1224
      gchar *contents = g_strdup_value_contents (&rcprop->value);
      
jacob berkman's avatar
jacob berkman committed
1225
      g_message ("%s: failed to retrieve property `%s::%s' of type `%s' from rc file value \"%s\" of type `%s'",
1226
		 rcprop->origin ? rcprop->origin : "(for origin information, set GTK_DEBUG)",
1227 1228 1229 1230 1231 1232
		 g_type_name (pspec->owner_type), pspec->name,
		 g_type_name (G_PARAM_SPEC_VALUE_TYPE (pspec)),
		 contents,
		 G_VALUE_TYPE_NAME (&rcprop->value));
      g_free (contents);
      rcprop = NULL; /* needs default */
1233 1234 1235 1236 1237 1238 1239 1240 1241
    }
  
  /* not supplied by rc style (or conversion failed), revert to default */
  if (!rcprop)
    g_param_value_set_default (pspec, &pcache->value);

  return &pcache->value;
}

1242
static GdkPixmap *
1243 1244 1245 1246 1247 1248 1249 1250
load_bg_image (GdkColormap *colormap,
	       GdkColor    *bg_color,
	       const gchar *filename)
{
  if (strcmp (filename, "<parent>") == 0)
    return (GdkPixmap*) GDK_PARENT_RELATIVE;
  else
    {
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
      GdkPixmap *pixmap;
      GdkPixbuf *pixbuf;
      cairo_t *cr;
      GdkScreen *screen = gdk_colormap_get_screen (colormap);
  
      pixbuf = gdk_pixbuf_new_from_file (filename, NULL);
      if (!pixbuf)
        return NULL;

      pixmap = gdk_pixmap_new (gdk_screen_get_root_window (screen),
                               gdk_pixbuf_get_width (pixbuf),
                               gdk_pixbuf_get_height (pixbuf),
                               gdk_colormap_get_visual (colormap)->depth);
      gdk_drawable_set_colormap (pixmap, colormap);
  
      cr = gdk_cairo_create (pixmap);

      gdk_cairo_set_source_color (cr, bg_color);
      cairo_paint (cr);

      gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
      cairo_paint (cr);

      cairo_destroy (cr);
      g_object_unref (pixbuf);

      return pixmap;
1278 1279 1280
    }
}

1281 1282 1283 1284 1285 1286 1287
static void
gtk_style_real_realize (GtkStyle *style)
{
  gint i;

  for (i = 0; i < 5; i++)
    {
1288 1289 1290
      _gtk_style_shade (&style->bg[i], &style->light[i], LIGHTNESS_MULT);
      _gtk_style_shade (&style->bg[i], &style->dark[i], DARKNESS_MULT);

1291 1292 1293
      style->mid[i].red = (style->light[i].red + style->dark[i].red) / 2;
      style->mid[i].green = (style->light[i].green + style->dark[i].green) / 2;
      style->mid[i].blue = (style->light[i].blue + style->dark[i].blue) / 2;
1294 1295 1296 1297

      style->text_aa[i].red = (style->text[i].red + style->base[i].red) / 2;
      style->text_aa[i].green = (style->text[i].green + style->base[i].green) / 2;
      style->text_aa[i].blue = (style->text[i].blue + style->base[i].blue) / 2;
1298
    }
Manish Singh's avatar
Manish Singh committed
1299 1300 1301 1302 1303 1304 1305 1306 1307

  style->black.red = 0x0000;
  style->black.green = 0x0000;
  style->black.blue = 0x0000;

  style->white.red = 0xffff;
  style->white.green = 0xffff;
  style->white.blue = 0xffff;

1308 1309 1310
  for (i = 0; i < 5; i++)
    {
      if (style->rc_style && style->rc_style->bg_pixmap_name[i])
1311
	style->bg_pixmap[i] = load_bg_image (style->colormap,
1312 1313
					     &style->bg[i],
					     style->rc_style->bg_pixmap_name[i]);
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
    }
}

static void
gtk_style_real_unrealize (GtkStyle *style)
{
  int i;

  for (i = 0; i < 5; i++)
    {
1324
      if (style->bg_pixmap[i] &&  style->bg_pixmap[i] != (GdkPixmap*) GDK_PARENT_RELATIVE)
1325 1326 1327 1328 1329
	{
	  g_object_unref (style->bg_pixmap[i]);
	  style->bg_pixmap[i] = NULL;
	}
      
1330
    }
1331
  
1332
  style_unrealize_cursors (style);
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
}

static void
gtk_style_real_set_background (GtkStyle    *style,
			       GdkWindow   *window,
			       GtkStateType state_type)
{
  GdkPixmap *pixmap;
  gint parent_relative;
  
1343 1344 1345
  if (style->bg_pixmap[state_type])
    {
      if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
1346 1347 1348 1349
        {
          pixmap = NULL;
          parent_relative = TRUE;
        }
1350
      else
1351 1352 1353 1354
        {
          pixmap = style->bg_pixmap[state_type];
          parent_relative = FALSE;
        }
1355 1356 1357 1358 1359 1360 1361
      
      gdk_window_set_back_pixmap (window, pixmap, parent_relative);
    }
  else
    gdk_window_set_background (window, &style->bg[state_type]);
}

1362 1363 1364 1365 1366 1367
/**
 * gtk_style_render_icon:
 * @style: a #GtkStyle
 * @source: the #GtkIconSource specifying the icon to render
 * @direction: a text direction
 * @state: a state
1368 1369 1370
 * @size: (type int) the size to render the icon at. A size of
 *     (GtkIconSize)-1 means render at the size of the source and
 *     don't scale.
1371 1372
 * @widget: (allow-none): the widget
 * @detail: (allow-none): a style detail
1373 1374
 * @returns: a newly-created #GdkPixbuf containing the rendered icon
 *
1375 1376
 * Renders the icon specified by @source at the given @size
 * according to the given parameters and returns the result in a
1377 1378
 * pixbuf.
 */
1379 1380 1381 1382 1383
GdkPixbuf *
gtk_style_render_icon (GtkStyle            *style,
                       const GtkIconSource *source,
                       GtkTextDirection     direction,
                       GtkStateType         state,
1384
                       GtkIconSize          size,
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
                       GtkWidget           *widget,
                       const gchar         *detail)
{
  GdkPixbuf *pixbuf;
  
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
  g_return_val_if_fail (GTK_STYLE_GET_CLASS (style)->render_icon != NULL, NULL);
  
  pixbuf = GTK_STYLE_GET_CLASS (style)->render_icon (style, source, direction, state,
                                                     size, widget, detail);

  g_return_val_if_fail (pixbuf != NULL, NULL);

  return pixbuf;
}

/* Default functions */
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414

/**
 * gtk_style_apply_default_background:
 * @style:
 * @window:
 * @set_bg:
 * @state_type:
 * @area: (allow-none):
 * @x:
 * @y:
 * @width:
 * @height:
 */
1415
void
1416 1417 1418 1419 1420 1421 1422 1423 1424
gtk_style_apply_default_background (GtkStyle          *style,
                                    GdkWindow         *window,
                                    gboolean           set_bg,
                                    GtkStateType        state_type,
                                    const GdkRectangle *area,
                                    gint                x,
                                    gint                y,
                                    gint                width,
                                    gint                height)
Elliot Lee's avatar
Elliot Lee committed
1425
{
1426
  GdkRectangle new_rect, old_rect;
1427
  
1428
  if (area)
Elliot Lee's avatar
Elliot Lee committed
1429
    {
1430 1431 1432 1433 1434
      old_rect.x = x;
      old_rect.y = y;
      old_rect.width = width;
      old_rect.height = height;
      
1435
      if (!gdk_rectangle_intersect (area, &old_rect, &new_rect))
1436
        return;
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446
    }
  else
    {
      new_rect.x = x;
      new_rect.y = y;
      new_rect.width = width;
      new_rect.height = height;
    }
  
  if (!style->bg_pixmap[state_type] ||
1447
      GDK_IS_PIXMAP (window) ||
1448
      (!set_bg && style->bg_pixmap[state_type] != (GdkPixmap*) GDK_PARENT_RELATIVE))
1449
    {
1450 1451
      cairo_t *cr = gdk_cairo_create (window);

1452
      if (style->bg_pixmap[state_type])
1453
        {
1454 1455
          gdk_cairo_set_source_pixmap (cr, style->bg_pixmap[state_type], 0, 0);
          cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_REPEAT);
1456
        }
1457 1458 1459 1460 1461 1462 1463
      else
        gdk_cairo_set_source_color (cr, &style->bg[state_type]);

      gdk_cairo_rectangle (cr, &new_rect);
      cairo_fill (cr);

      cairo_destroy (cr);
1464
    }
1465
  else
1466
    {
1467
      if (set_bg)
1468 1469 1470 1471 1472 1473
        {
          if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
            gdk_window_set_back_pixmap (window, NULL, TRUE);
          else
            gdk_window_set_back_pixmap (window, style->bg_pixmap[state_type], FALSE);
        }
1474
      
1475
      gdk_window_clear_area (window, 
1476 1477
                             new_rect.x, new_rect.y, 
                             new_rect.width, new_rect.height);
1478
    }
Elliot Lee's avatar
Elliot Lee committed
1479 1480
}

1481
static GdkPixbuf *
1482
scale_or_ref (GdkPixbuf *src,
1483 1484
              gint       width,
              gint       height)
1485 1486 1487 1488
{
  if (width == gdk_pixbuf_get_width (src) &&
      height == gdk_pixbuf_get_height (src))
    {
Manish Singh's avatar
Manish Singh committed
1489
      return g_object_ref (src);
1490 1491 1492 1493 1494 1495 1496 1497 1498
    }
  else
    {
      return gdk_pixbuf_scale_simple (src,
                                      width, height,
                                      GDK_INTERP_BILINEAR);
    }
}

1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
static gboolean
lookup_icon_size (GtkStyle    *style,
		  GtkWidget   *widget,
		  GtkIconSize  size,
		  gint        *width,
		  gint        *height)
{
  GdkScreen *screen;
  GtkSettings *settings;

  if (widget && gtk_widget_has_screen (widget))
    {
      screen = gtk_widget_get_screen (widget);
      settings = gtk_settings_get_for_screen (screen);
    }
  else if (style && style->colormap)
    {
      screen = gdk_colormap_get_screen (style->colormap);
      settings = gtk_settings_get_for_screen (screen);
    }
  else
    {
      settings = gtk_settings_get_default ();
      GTK_NOTE (MULTIHEAD,
		g_warning ("Using the default screen for gtk_default_render_icon()"));
    }

  return gtk_icon_size_lookup_for_settings (settings, size, width, height);
}

1529 1530
static GdkPixbuf *
gtk_default_render_icon (GtkStyle            *style,
1531 1532 1533 1534 1535 1536
                         const GtkIconSource *source,
                         GtkTextDirection     direction,
                         GtkStateType         state,
                         GtkIconSize          size,
                         GtkWidget           *widget,
                         const gchar         *detail)
1537 1538 1539 1540 1541
{
  gint width = 1;
  gint height = 1;
  GdkPixbuf *scaled;
  GdkPixbuf *stated;
1542
  GdkPixbuf *base_pixbuf;
1543 1544 1545 1546 1547

  /* Oddly, style can be NULL in this function, because
   * GtkIconSet can be used without a style and if so
   * it uses this function.
   */
1548 1549 1550 1551

  base_pixbuf = gtk_icon_source_get_pixbuf (source);

  g_return_val_if_fail (base_pixbuf != NULL, NULL);
1552

1553
  if (size != (GtkIconSize) -1 && !lookup_icon_size(style, widget, size, &width, &height))
1554
    {
1555
      g_warning (G_STRLOC ": invalid icon size '%d'", size);
1556 1557 1558
      return NULL;
    }

1559 1560
  /* If the size was wildcarded, and we're allowed to scale, then scale; otherwise,
   * leave it alone.
1561
   */
1562
  if (size != (GtkIconSize)-1 && gtk_icon_source_get_size_wildcarded (source))
1563
    scaled = scale_or_ref (base_pixbuf, width, height);
1564
  else
Manish Singh's avatar
Manish Singh committed
1565
    scaled = g_object_ref (base_pixbuf);
1566 1567

  /* If the state was wildcarded, then generate a state. */
1568
  if (gtk_icon_source_get_state_wildcarded (source))
1569 1570 1571 1572 1573 1574 1575 1576
    {
      if (state == GTK_STATE_INSENSITIVE)
        {
          stated = gdk_pixbuf_copy (scaled);      
          
          gdk_pixbuf_saturate_and_pixelate (scaled, stated,
                                            0.8, TRUE);
          
Manish Singh's avatar
Manish Singh committed
1577
          g_object_unref (scaled);
1578 1579 1580 1581 1582 1583 1584 1585
        }
      else if (state == GTK_STATE_PRELIGHT)
        {
          stated = gdk_pixbuf_copy (scaled);      
          
          gdk_pixbuf_saturate_and_pixelate (scaled, stated,
                                            1.2, FALSE);
          
Manish Singh's avatar
Manish Singh committed
1586
          g_object_unref (scaled);
1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
        }
      else
        {
          stated = scaled;
        }
    }
  else
    stated = scaled;
  
  return stated;
}

1599
static void
1600 1601 1602 1603 1604
sanitize_size (GdkWindow *window,
	       gint      *width,
	       gint      *height)
{
  if ((*width == -1) && (*height == -1))
Manish Singh's avatar
Manish Singh committed
1605
    gdk_drawable_get_size (window, width, height);
1606
  else if (*width == -1)
Manish Singh's avatar
Manish Singh committed
1607
    gdk_drawable_get_size (window, width, NULL);
1608
  else if (*height == -1)
Manish Singh's avatar
Manish Singh committed
1609
    gdk_drawable_get_size (window, NULL, height);
1610 1611
}

1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
static void
_cairo_draw_line (cairo_t  *cr,
                  GdkColor *color,
                  gint      x1,
                  gint      y1,
                  gint      x2,
                  gint      y2)
{
  cairo_save (cr);

  gdk_cairo_set_source_color (cr, color);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);

  cairo_move_to (cr, x1 + 0.5, y1 + 0.5);
  cairo_line_to (cr, x2 + 0.5, y2 + 0.5);
  cairo_stroke (cr);

  cairo_restore (cr);
}

static void
_cairo_draw_rectangle (cairo_t *cr,
                       GdkColor *color,
                       gboolean filled,
                       gint x,
                       gint y,
                       gint width,
                       gint height)
{
  gdk_cairo_set_source_color (cr, color);

  if (filled)
    {
      cairo_rectangle (cr, x, y, width, height);
      cairo_fill (cr);
    }
  else
    {
      cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
      cairo_stroke (cr);
    }
}

static void
_cairo_draw_point (cairo_t *cr,
                   GdkColor *color,
                   gint x,
                   gint y)
{
  gdk_cairo_set_source_color (cr, color);
  cairo_rectangle (cr, x, y, 1, 1);
  cairo_fill (cr);
}

Elliot Lee's avatar
Elliot Lee committed
1666 1667
static void
gtk_default_draw_hline (GtkStyle     *style,
1668 1669 1670 1671
                        GdkWindow    *window,
                        GtkStateType  state_type,
                        GdkRectangle  *area,
                        GtkWidget     *widget,
1672
                        const gchar   *detail,
1673 1674 1675
                        gint          x1,
                        gint          x2,
                        gint          y)
Elliot Lee's avatar
Elliot Lee committed
1676
{
1677
  cairo_t *cr;
1678 1679 1680 1681
  gint thickness_light;
  gint thickness_dark;
  gint i;
  
1682 1683
  thickness_light = style->ythickness / 2;
  thickness_dark = style->ythickness