GitLab repository storage has been migrated to hashed layout. Please contact Infrastructure team if you notice any issues with repositories or hooks.

gtkstyle.c 153 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4
/* GTK - The GIMP Toolkit
 * 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/. 
 */

Elliot Lee's avatar
Elliot Lee committed
27
#include <math.h>
28
#include <string.h>
Elliot Lee's avatar
Elliot Lee committed
29
#include "gtkgc.h"
30
#include "gtkrc.h"
31
#include "gtkspinbutton.h"
Elliot Lee's avatar
Elliot Lee committed
32
#include "gtkstyle.h"
33
#include "gtkwidget.h"
34
#include "gtkthemes.h"
35
#include "gtkiconfactory.h"
36
#include "gtksettings.h"	/* _gtk_settings_parse_convert() */
Elliot Lee's avatar
Elliot Lee committed
37 38 39 40

#define LIGHTNESS_MULT  1.3
#define DARKNESS_MULT   0.7

41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/* --- typedefs & structures --- */
typedef struct {
  GType       widget_type;
  GParamSpec *pspec;
  GValue      value;
} PropertyValue;


/* --- prototypes --- */
static void	 gtk_style_init			(GtkStyle	*style);
static void	 gtk_style_class_init		(GtkStyleClass	*klass);
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);
66 67 68 69
static GdkPixbuf *gtk_default_render_icon      (GtkStyle            *style,
                                                const GtkIconSource *source,
                                                GtkTextDirection     direction,
                                                GtkStateType         state,
70
                                                GtkIconSize          size,
71 72
                                                GtkWidget           *widget,
                                                const gchar         *detail);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
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_polygon    (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 GdkPoint        *points,
					 gint             npoints,
					 gboolean         fill);
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_string     (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 gint             x,
					 gint             y,
					 const gchar     *string);
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,
					 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);
273 274 275 276 277 278 279 280 281 282 283 284
static void gtk_default_draw_expander   (GtkStyle        *style,
                                         GdkWindow       *window,
                                         GtkStateType     state_type,
                                         GdkRectangle    *area,
                                         GtkWidget       *widget,
                                         const gchar     *detail,
                                         gint             x,
                                         gint             y,
                                         gboolean         is_open);
static void gtk_default_draw_layout     (GtkStyle        *style,
                                         GdkWindow       *window,
                                         GtkStateType     state_type,
285
					 gboolean         use_text,
286 287 288 289 290 291
                                         GdkRectangle    *area,
                                         GtkWidget       *widget,
                                         const gchar     *detail,
                                         gint             x,
                                         gint             y,
                                         PangoLayout     *layout);
Owen Taylor's avatar
Owen Taylor committed
292 293 294 295 296 297 298 299 300 301 302 303
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);

304 305 306 307 308 309 310 311 312 313 314
static void gtk_style_shade		(GdkColor	 *a,
					 GdkColor	 *b,
					 gdouble	  k);
static void rgb_to_hls			(gdouble	 *r,
					 gdouble	 *g,
					 gdouble	 *b);
static void hls_to_rgb			(gdouble	 *h,
					 gdouble	 *l,
					 gdouble	 *s);


315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
/*
 * Data for default check and radio buttons
 */

static GtkRequisition default_option_indicator_size = { 7, 13 };
static GtkBorder default_option_indicator_spacing = { 7, 5, 2, 2 };

#define INDICATOR_PART_SIZE 13

typedef enum {
  CHECK_AA,
  CHECK_BASE,
  CHECK_BLACK,
  CHECK_DARK,
  CHECK_LIGHT,
  CHECK_MID,
  CHECK_TEXT,
  RADIO_BASE,
  RADIO_BLACK,
  RADIO_DARK,
  RADIO_LIGHT,
  RADIO_MID,
  RADIO_TEXT
} IndicatorPart;

static char check_aa_bits[] = {
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x58,0x00,0xa0,
 0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static char check_base_bits[] = {
 0x00,0x00,0x00,0x00,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
 0x07,0xfc,0x07,0xfc,0x07,0xfc,0x07,0x00,0x00,0x00,0x00};
static char check_black_bits[] = {
 0x00,0x00,0xfe,0x0f,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
 0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00};
static char check_dark_bits[] = {
 0xff,0x1f,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
 0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00};
static char check_light_bits[] = {
 0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
 0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0xfe,0x1f};
static char check_mid_bits[] = {
 0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
 0x08,0x00,0x08,0x00,0x08,0x00,0x08,0xfc,0x0f,0x00,0x00};
static char check_text_bits[] = {
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x03,0x80,0x01,0x80,0x00,0xd8,
 0x00,0x60,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static char radio_base_bits[] = {
 0x00,0x00,0x00,0x00,0xf0,0x01,0xf8,0x03,0xfc,0x07,0xfc,0x07,0xfc,0x07,0xfc,
 0x07,0xfc,0x07,0xf8,0x03,0xf0,0x01,0x00,0x00,0x00,0x00};
static char radio_black_bits[] = {
 0x00,0x00,0xf0,0x01,0x08,0x02,0x04,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,
 0x00,0x02,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
static char radio_dark_bits[] = {
 0xf0,0x01,0x08,0x02,0x04,0x04,0x02,0x04,0x01,0x00,0x01,0x00,0x01,0x00,0x01,
 0x00,0x01,0x00,0x02,0x00,0x0c,0x00,0x00,0x00,0x00,0x00};
static char radio_light_bits[] = {
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x10,0x00,0x10,0x00,0x10,0x00,
 0x10,0x00,0x10,0x00,0x08,0x00,0x04,0x08,0x02,0xf0,0x01};
static char radio_mid_bits[] = {
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x08,0x00,
 0x08,0x00,0x08,0x00,0x04,0x00,0x02,0xf0,0x01,0x00,0x00};
static char radio_text_bits[] = {
 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xe0,0x00,0xf0,0x01,0xf0,0x01,0xf0,
 0x01,0xe0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

static struct {
  char      *bits;
  GdkBitmap *bmap;
} indicator_parts[] = {
  { check_aa_bits, NULL },
  { check_base_bits, NULL },
  { check_black_bits, NULL },
  { check_dark_bits, NULL },
  { check_light_bits, NULL },
  { check_mid_bits, NULL },
  { check_text_bits, NULL },
  { radio_base_bits, NULL },
  { radio_black_bits, NULL },
  { radio_dark_bits, NULL },
  { radio_light_bits, NULL },
  { radio_mid_bits, NULL },
  { radio_text_bits, NULL }
};

399
/* --- variables --- */
Elliot Lee's avatar
Elliot Lee committed
400 401 402 403 404 405 406 407 408 409 410 411
static GdkColor gtk_default_normal_fg =      { 0,      0,      0,      0 };
static GdkColor gtk_default_active_fg =      { 0,      0,      0,      0 };
static GdkColor gtk_default_prelight_fg =    { 0,      0,      0,      0 };
static GdkColor gtk_default_selected_fg =    { 0, 0xffff, 0xffff, 0xffff };
static GdkColor gtk_default_insensitive_fg = { 0, 0x7530, 0x7530, 0x7530 };

static GdkColor gtk_default_normal_bg =      { 0, 0xd6d6, 0xd6d6, 0xd6d6 };
static GdkColor gtk_default_active_bg =      { 0, 0xc350, 0xc350, 0xc350 };
static GdkColor gtk_default_prelight_bg =    { 0, 0xea60, 0xea60, 0xea60 };
static GdkColor gtk_default_selected_bg =    { 0,      0,      0, 0x9c40 };
static GdkColor gtk_default_insensitive_bg = { 0, 0xd6d6, 0xd6d6, 0xd6d6 };

412
static gpointer parent_class = NULL;
413
static GdkFont *static_default_font = NULL;
414 415


416
/* --- functions --- */
417 418
GType
gtk_style_get_type (void)
419
{
420 421 422
  static GType style_type = 0;
  
  if (!style_type)
Stuart Parmenter's avatar
Stuart Parmenter committed
423
    {
424
      static const GTypeInfo style_info =
425 426 427 428 429 430 431 432 433 434 435 436
      {
        sizeof (GtkStyleClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gtk_style_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkStyle),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_style_init,
      };
      
437 438 439
      style_type = g_type_register_static (G_TYPE_OBJECT,
					   "GtkStyle",
					   &style_info, 0);
Stuart Parmenter's avatar
Stuart Parmenter committed
440
    }
441
  
442
  return style_type;
443
}
Elliot Lee's avatar
Elliot Lee committed
444

445 446
static void
gtk_style_init (GtkStyle *style)
Elliot Lee's avatar
Elliot Lee committed
447 448
{
  gint i;
449
  
450
  style->font_desc = pango_font_description_from_string ("Sans 10");
Elliot Lee's avatar
Elliot Lee committed
451

452
  if (!static_default_font)
453
    {
454
      static_default_font = gdk_font_from_description (style->font_desc);
455

456 457
      if (!static_default_font) 
	static_default_font = gdk_font_load ("fixed");
458

459 460
      if (!static_default_font) 
	g_error ("Unable to load \"fixed\" font");
461
    }
462
  
463
  style->font = static_default_font;
Elliot Lee's avatar
Elliot Lee committed
464
  gdk_font_ref (style->font);
Elliot Lee's avatar
Elliot Lee committed
465

Elliot Lee's avatar
Elliot Lee committed
466 467 468
  style->attach_count = 0;
  style->colormap = NULL;
  style->depth = -1;
469
  
Elliot Lee's avatar
Elliot Lee committed
470 471 472
  style->black.red = 0;
  style->black.green = 0;
  style->black.blue = 0;
473
  
Elliot Lee's avatar
Elliot Lee committed
474 475 476
  style->white.red = 65535;
  style->white.green = 65535;
  style->white.blue = 65535;
477
  
Elliot Lee's avatar
Elliot Lee committed
478 479
  style->black_gc = NULL;
  style->white_gc = NULL;
480
  
Elliot Lee's avatar
Elliot Lee committed
481 482 483 484 485
  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;
486
  
Elliot Lee's avatar
Elliot Lee committed
487 488 489 490 491
  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;
492
  
493
  for (i = 0; i < 4; i++)
Elliot Lee's avatar
Elliot Lee committed
494 495 496 497
    {
      style->text[i] = style->fg[i];
      style->base[i] = style->white;
    }
Havoc Pennington's avatar
Havoc Pennington committed
498 499

  style->base[GTK_STATE_SELECTED] = gtk_default_selected_bg;
Owen Taylor's avatar
Owen Taylor committed
500 501
  style->base[GTK_STATE_INSENSITIVE] = gtk_default_prelight_bg;
  style->text[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
502
  
Elliot Lee's avatar
Elliot Lee committed
503 504
  for (i = 0; i < 5; i++)
    style->bg_pixmap[i] = NULL;
505
  
506
  style->rc_style = NULL;
507
  
Elliot Lee's avatar
Elliot Lee committed
508 509 510 511 512 513 514 515 516
  for (i = 0; i < 5; i++)
    {
      style->fg_gc[i] = NULL;
      style->bg_gc[i] = NULL;
      style->light_gc[i] = NULL;
      style->dark_gc[i] = NULL;
      style->mid_gc[i] = NULL;
      style->text_gc[i] = NULL;
      style->base_gc[i] = NULL;
517
      style->text_aa_gc[i] = NULL;
Elliot Lee's avatar
Elliot Lee committed
518
    }
519 520 521

  style->xthickness = 2;
  style->ythickness = 2;
522 523

  style->property_cache = NULL;
524 525 526 527 528 529 530 531 532 533 534
}

static void
gtk_style_class_init (GtkStyleClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gtk_style_finalize;

535 536 537 538 539 540
  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;
541 542
  klass->render_icon = gtk_default_render_icon;

543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
  klass->draw_hline = gtk_default_draw_hline;
  klass->draw_vline = gtk_default_draw_vline;
  klass->draw_shadow = gtk_default_draw_shadow;
  klass->draw_polygon = gtk_default_draw_polygon;
  klass->draw_arrow = gtk_default_draw_arrow;
  klass->draw_diamond = gtk_default_draw_diamond;
  klass->draw_string = gtk_default_draw_string;
  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;
561 562
  klass->draw_expander = gtk_default_draw_expander;
  klass->draw_layout = gtk_default_draw_layout;
Owen Taylor's avatar
Owen Taylor committed
563
  klass->draw_resize_grip = gtk_default_draw_resize_grip;
564 565 566 567 568 569 570 571
}

static void
gtk_style_finalize (GObject *object)
{
  GtkStyle *style = GTK_STYLE (object);

  g_return_if_fail (style->attach_count == 0);
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586

  if (style->property_cache)
    {
      guint i;

      for (i = 0; i < style->property_cache->n_nodes; i++)
	{
	  PropertyValue *node = g_bsearch_array_get_nth (style->property_cache, i);

	  g_param_spec_unref (node->pspec);
	  g_value_unset (&node->value);
	}
      g_bsearch_array_destroy (style->property_cache);
      style->property_cache = NULL;
    }
587 588 589 590 591 592 593 594 595 596 597
  
  if (style->styles)
    {
      if (style->styles->data != style)
        g_slist_remove (style->styles, style);
      else
        {
          GSList *tmp_list = style->styles->next;
	  
          while (tmp_list)
            {
598
              GTK_STYLE (tmp_list->data)->styles = style->styles->next;
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
              tmp_list = tmp_list->next;
            }
          g_slist_free_1 (style->styles);
        }
    }
  
  gdk_font_unref (style->font);
  pango_font_description_free (style->font_desc);
  
  if (style->rc_style)
    gtk_rc_style_unref (style->rc_style);
  
  G_OBJECT_CLASS (parent_class)->finalize (object);
}


GtkStyle*
gtk_style_copy (GtkStyle *style)
{
  GtkStyle *new_style;
  
620
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
621
  
622 623
  new_style = GTK_STYLE_GET_CLASS (style)->clone (style);
  GTK_STYLE_GET_CLASS (style)->copy (new_style, style);
624 625 626 627 628 629 630 631 632

  return new_style;
}

static GtkStyle*
gtk_style_duplicate (GtkStyle *style)
{
  GtkStyle *new_style;
  
633
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
634 635 636 637 638 639 640 641 642 643 644 645 646 647
  
  new_style = gtk_style_copy (style);
  
  style->styles = g_slist_append (style->styles, new_style);
  new_style->styles = style->styles;  
  
  return new_style;
}

GtkStyle*
gtk_style_new (void)
{
  GtkStyle *style;
  
648
  style = g_object_new (GTK_TYPE_STYLE, NULL);
649
  
Elliot Lee's avatar
Elliot Lee committed
650 651 652
  return style;
}

653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
/*************************************************************
 * gtk_style_attach:
 *     Attach 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.
 *   arguments:
 *     style:
 *     window: 
 *   results:
 *     Either the style parameter, or a newly created style.
 *     If the style is newly created, the style parameter
 *     will be dereferenced, and the new style will have
 *     a reference count belonging to the caller.
 *
 * FIXME: The sequence - 
 *    create a style => s1
 *    attach s1 to v1, c1 => s1
 *    attach s1 to v2, c2 => s2
 *    detach s1 from v1, c1
 *    attach s1 to v2, c2 => s3
 * results in two separate, unlinked styles s2 and s3 which
 * are identical and could be shared. To fix this, we would
 * want to never remove a style from the list of linked
 * styles as long as as it has a reference count. However, the 
 * disadvantage of doing it this way means that we would need two 
 * passes through the linked list when attaching (one to check for 
 * matching styles, one to look for empty unattached styles - but 
 * it will almost never be longer than 2 elements.
 *************************************************************/

686
GtkStyle*
Elliot Lee's avatar
Elliot Lee committed
687
gtk_style_attach (GtkStyle  *style,
688
                  GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
689
{
690 691
  GSList *styles;
  GtkStyle *new_style = NULL;
Elliot Lee's avatar
Elliot Lee committed
692
  GdkColormap *colormap;
693
  
694
  g_return_val_if_fail (GTK_IS_STYLE (style), NULL);
Elliot Lee's avatar
Elliot Lee committed
695
  g_return_val_if_fail (window != NULL, NULL);
696
  
Elliot Lee's avatar
Elliot Lee committed
697
  colormap = gdk_window_get_colormap (window);
698
  
699 700
  if (!style->styles)
    style->styles = g_slist_append (NULL, style);
701
  
702 703
  styles = style->styles;
  while (styles)
Elliot Lee's avatar
Elliot Lee committed
704
    {
705
      new_style = styles->data;
706
      
707
      if (new_style->attach_count == 0)
708
        {
709
          gtk_style_realize (new_style, colormap);
710 711
          break;
        }
712
      else if (new_style->colormap == colormap)
713
        break;
714
      
715 716
      new_style = NULL;
      styles = styles->next;
Elliot Lee's avatar
Elliot Lee committed
717
    }
718
  
719 720 721
  if (!new_style)
    {
      new_style = gtk_style_duplicate (style);
722
      gtk_style_realize (new_style, colormap);
723
    }
724 725

  /* A style gets a refcount from being attached */
726 727
  if (new_style->attach_count == 0)
    gtk_style_ref (new_style);
728 729 730 731 732 733 734

  /* Another refcount belongs to the parent */
  if (style != new_style) 
    {
      gtk_style_unref (style);
      gtk_style_ref (new_style);
    }
735
  
736
  new_style->attach_count++;
737
  
738
  return new_style;
Elliot Lee's avatar
Elliot Lee committed
739 740 741 742 743
}

void
gtk_style_detach (GtkStyle *style)
{
744
  g_return_if_fail (GTK_IS_STYLE (style));
745
  
Elliot Lee's avatar
Elliot Lee committed
746 747 748
  style->attach_count -= 1;
  if (style->attach_count == 0)
    {
749
      GTK_STYLE_GET_CLASS (style)->unrealize (style);
750
      
751 752 753
      gdk_colormap_unref (style->colormap);
      style->colormap = NULL;
      
754
      gtk_style_unref (style);
Elliot Lee's avatar
Elliot Lee committed
755 756 757 758 759 760
    }
}

GtkStyle*
gtk_style_ref (GtkStyle *style)
{
761
  return (GtkStyle *) g_object_ref (G_OBJECT (style));
Elliot Lee's avatar
Elliot Lee committed
762 763 764 765 766
}

void
gtk_style_unref (GtkStyle *style)
{
767
  g_object_unref (G_OBJECT (style));
Elliot Lee's avatar
Elliot Lee committed
768 769
}

770
static void
771
gtk_style_realize (GtkStyle    *style,
772
                   GdkColormap *colormap)
773
{
774
  g_return_if_fail (GTK_IS_STYLE (style));
Hans Breuer's avatar
Hans Breuer committed
775
  g_return_if_fail (GDK_IS_COLORMAP (colormap));
776
  
777
  style->colormap = gdk_colormap_ref (colormap);
778 779 780
  style->depth = gdk_colormap_get_visual (colormap)->depth;

  GTK_STYLE_GET_CLASS (style)->realize (style);
781 782
}

783 784 785 786 787 788 789 790 791 792 793 794
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)
    {
795 796
      GtkIconSet *icon_set = gtk_icon_factory_lookup (GTK_ICON_FACTORY (iter->data),
						      stock_id);
797 798 799 800 801 802 803 804 805
      if (icon_set)
        return icon_set;
      
      iter = g_slist_next (iter);
    }

  return gtk_icon_factory_lookup_default (stock_id);
}

Elliot Lee's avatar
Elliot Lee committed
806 807
void
gtk_draw_hline (GtkStyle     *style,
808 809 810 811 812
                GdkWindow    *window,
                GtkStateType  state_type,
                gint          x1,
                gint          x2,
                gint          y)
Elliot Lee's avatar
Elliot Lee committed
813
{
814
  g_return_if_fail (GTK_IS_STYLE (style));
815
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_hline != NULL);
816
  
817
  GTK_STYLE_GET_CLASS (style)->draw_hline (style, window, state_type, NULL, NULL, NULL, x1, x2, y);
Elliot Lee's avatar
Elliot Lee committed
818 819 820 821 822
}


void
gtk_draw_vline (GtkStyle     *style,
823 824 825 826 827
                GdkWindow    *window,
                GtkStateType  state_type,
                gint          y1,
                gint          y2,
                gint          x)
Elliot Lee's avatar
Elliot Lee committed
828
{
829
  g_return_if_fail (GTK_IS_STYLE (style));
830
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_vline != NULL);
831
  
832
  GTK_STYLE_GET_CLASS (style)->draw_vline (style, window, state_type, NULL, NULL, NULL, y1, y2, x);
Elliot Lee's avatar
Elliot Lee committed
833 834 835 836 837
}


void
gtk_draw_shadow (GtkStyle      *style,
838 839 840 841 842 843 844
                 GdkWindow     *window,
                 GtkStateType   state_type,
                 GtkShadowType  shadow_type,
                 gint           x,
                 gint           y,
                 gint           width,
                 gint           height)
Elliot Lee's avatar
Elliot Lee committed
845
{
846
  g_return_if_fail (GTK_IS_STYLE (style));
847
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow != NULL);
848
  
849
  GTK_STYLE_GET_CLASS (style)->draw_shadow (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
850 851 852 853
}

void
gtk_draw_polygon (GtkStyle      *style,
854 855 856 857 858 859
                  GdkWindow     *window,
                  GtkStateType   state_type,
                  GtkShadowType  shadow_type,
                  GdkPoint      *points,
                  gint           npoints,
                  gboolean       fill)
Elliot Lee's avatar
Elliot Lee committed
860
{
861
  g_return_if_fail (GTK_IS_STYLE (style));
862
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_polygon != NULL);
863
  
864
  GTK_STYLE_GET_CLASS (style)->draw_polygon (style, window, state_type, shadow_type, NULL, NULL, NULL, points, npoints, fill);
Elliot Lee's avatar
Elliot Lee committed
865 866 867 868
}

void
gtk_draw_arrow (GtkStyle      *style,
869 870 871 872 873 874 875 876 877
                GdkWindow     *window,
                GtkStateType   state_type,
                GtkShadowType  shadow_type,
                GtkArrowType   arrow_type,
                gboolean       fill,
                gint           x,
                gint           y,
                gint           width,
                gint           height)
Elliot Lee's avatar
Elliot Lee committed
878
{
879
  g_return_if_fail (GTK_IS_STYLE (style));
880
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_arrow != NULL);
881
  
882
  GTK_STYLE_GET_CLASS (style)->draw_arrow (style, window, state_type, shadow_type, NULL, NULL, NULL, arrow_type, fill, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
883 884 885 886 887
}


void
gtk_draw_diamond (GtkStyle      *style,
888 889 890 891 892 893 894
                  GdkWindow     *window,
                  GtkStateType   state_type,
                  GtkShadowType  shadow_type,
                  gint           x,
                  gint           y,
                  gint           width,
                  gint           height)
Elliot Lee's avatar
Elliot Lee committed
895
{
896
  g_return_if_fail (GTK_IS_STYLE (style));
897
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_diamond != NULL);
898
  
899
  GTK_STYLE_GET_CLASS (style)->draw_diamond (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
900 901 902 903 904
}


void
gtk_draw_string (GtkStyle      *style,
905 906 907 908 909
                 GdkWindow     *window,
                 GtkStateType   state_type,
                 gint           x,
                 gint           y,
                 const gchar   *string)
Elliot Lee's avatar
Elliot Lee committed
910
{
911
  g_return_if_fail (GTK_IS_STYLE (style));
912
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_string != NULL);
913
  
914
  GTK_STYLE_GET_CLASS (style)->draw_string (style, window, state_type, NULL, NULL, NULL, x, y, string);
Elliot Lee's avatar
Elliot Lee committed
915 916
}

917 918
void
gtk_draw_box (GtkStyle      *style,
919 920 921 922 923 924 925
              GdkWindow     *window,
              GtkStateType   state_type,
              GtkShadowType  shadow_type,
              gint           x,
              gint           y,
              gint           width,
              gint           height)
Elliot Lee's avatar
Elliot Lee committed
926
{
927
  g_return_if_fail (GTK_IS_STYLE (style));
928
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box != NULL);
929
  
930
  GTK_STYLE_GET_CLASS (style)->draw_box (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
931 932
}

933 934
void
gtk_draw_flat_box (GtkStyle      *style,
935 936 937 938 939 940 941
                   GdkWindow     *window,
                   GtkStateType   state_type,
                   GtkShadowType  shadow_type,
                   gint           x,
                   gint           y,
                   gint           width,
                   gint           height)
Elliot Lee's avatar
Elliot Lee committed
942
{
943
  g_return_if_fail (GTK_IS_STYLE (style));
944
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_flat_box != NULL);
945
  
946
  GTK_STYLE_GET_CLASS (style)->draw_flat_box (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
947 948
}

949
void
950 951 952 953 954 955 956 957
gtk_draw_check (GtkStyle      *style,
                GdkWindow     *window,
                GtkStateType   state_type,
                GtkShadowType  shadow_type,
                gint           x,
                gint           y,
                gint           width,
                gint           height)
Elliot Lee's avatar
Elliot Lee committed
958
{
959
  g_return_if_fail (GTK_IS_STYLE (style));
960
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_check != NULL);
961
  
962
  GTK_STYLE_GET_CLASS (style)->draw_check (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
963 964
}

965
void
966 967 968 969 970 971 972 973
gtk_draw_option (GtkStyle      *style,
		 GdkWindow     *window,
		 GtkStateType   state_type,
		 GtkShadowType  shadow_type,
		 gint           x,
		 gint           y,
		 gint           width,
		 gint           height)
Elliot Lee's avatar
Elliot Lee committed
974
{
975
  g_return_if_fail (GTK_IS_STYLE (style));
976
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_option != NULL);
977
  
978
  GTK_STYLE_GET_CLASS (style)->draw_option (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
979 980
}

981
void
982 983 984 985 986 987 988 989
gtk_draw_tab (GtkStyle      *style,
	      GdkWindow     *window,
	      GtkStateType   state_type,
	      GtkShadowType  shadow_type,
	      gint           x,
	      gint           y,
	      gint           width,
	      gint           height)
Elliot Lee's avatar
Elliot Lee committed
990
{
991
  g_return_if_fail (GTK_IS_STYLE (style));
992
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_tab != NULL);
993
  
994
  GTK_STYLE_GET_CLASS (style)->draw_tab (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
995
}
Elliot Lee's avatar
Elliot Lee committed
996

997 998
void
gtk_draw_shadow_gap (GtkStyle       *style,
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
                     GdkWindow      *window,
                     GtkStateType    state_type,
                     GtkShadowType   shadow_type,
                     gint            x,
                     gint            y,
                     gint            width,
                     gint            height,
                     GtkPositionType gap_side,
                     gint            gap_x,
                     gint            gap_width)
1009
{
1010
  g_return_if_fail (GTK_IS_STYLE (style));
1011
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow_gap != NULL);
1012
  
1013
  GTK_STYLE_GET_CLASS (style)->draw_shadow_gap (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, gap_side, gap_x, gap_width);
Elliot Lee's avatar
Elliot Lee committed
1014 1015
}

1016 1017
void
gtk_draw_box_gap (GtkStyle       *style,
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                  GdkWindow      *window,
                  GtkStateType    state_type,
                  GtkShadowType   shadow_type,
                  gint            x,
                  gint            y,
                  gint            width,
                  gint            height,
                  GtkPositionType gap_side,
                  gint            gap_x,
                  gint            gap_width)
Elliot Lee's avatar
Elliot Lee committed
1028
{
1029
  g_return_if_fail (GTK_IS_STYLE (style));
1030
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box_gap != NULL);
1031
  
1032
  GTK_STYLE_GET_CLASS (style)->draw_box_gap (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, gap_side, gap_x, gap_width);
1033
}
Elliot Lee's avatar
Elliot Lee committed
1034

1035 1036
void
gtk_draw_extension (GtkStyle       *style,
1037 1038 1039 1040 1041 1042 1043 1044
                    GdkWindow      *window,
                    GtkStateType    state_type,
                    GtkShadowType   shadow_type,
                    gint            x,
                    gint            y,
                    gint            width,
                    gint            height,
                    GtkPositionType gap_side)
1045
{
1046
  g_return_if_fail (GTK_IS_STYLE (style));
1047
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_extension != NULL);
1048
  
1049
  GTK_STYLE_GET_CLASS (style)->draw_extension (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, gap_side);
Elliot Lee's avatar
Elliot Lee committed
1050 1051
}

1052
void
1053 1054 1055 1056 1057 1058
gtk_draw_focus (GtkStyle      *style,
		GdkWindow     *window,
		gint           x,
		gint           y,
		gint           width,
		gint           height)
Elliot Lee's avatar
Elliot Lee committed
1059
{
1060
  g_return_if_fail (GTK_IS_STYLE (style));
1061
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_focus != NULL);
1062
  
1063
  GTK_STYLE_GET_CLASS (style)->draw_focus (style, window, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
1064 1065
}

1066
void 
1067 1068 1069 1070 1071 1072 1073 1074 1075
gtk_draw_slider (GtkStyle      *style,
		 GdkWindow     *window,
		 GtkStateType   state_type,
		 GtkShadowType  shadow_type,
		 gint           x,
		 gint           y,
		 gint           width,
		 gint           height,
		 GtkOrientation orientation)
Elliot Lee's avatar
Elliot Lee committed
1076
{
1077
  g_return_if_fail (GTK_IS_STYLE (style));
1078
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_slider != NULL);
1079
  
1080
  GTK_STYLE_GET_CLASS (style)->draw_slider (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, orientation);
Elliot Lee's avatar
Elliot Lee committed
1081 1082
}

1083
void 
1084 1085 1086 1087 1088 1089 1090 1091 1092
gtk_draw_handle (GtkStyle      *style,
		 GdkWindow     *window,
		 GtkStateType   state_type,
		 GtkShadowType  shadow_type,
		 gint           x,
		 gint           y,
		 gint           width,
		 gint           height,
		 GtkOrientation orientation)
Elliot Lee's avatar
Elliot Lee committed
1093
{
1094
  g_return_if_fail (GTK_IS_STYLE (style));
1095
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_handle != NULL);
1096
  
1097
  GTK_STYLE_GET_CLASS (style)->draw_handle (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height, orientation);
Elliot Lee's avatar
Elliot Lee committed
1098 1099
}

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
void
gtk_draw_expander (GtkStyle        *style,
                   GdkWindow       *window,
                   GtkStateType     state_type,
                   gint             x,
                   gint             y,
                   gboolean         is_open)
{
  g_return_if_fail (GTK_IS_STYLE (style));
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_expander != NULL);
  
  GTK_STYLE_GET_CLASS (style)->draw_expander (style, window, state_type,
                                              NULL, NULL, NULL,
                                              x, y, is_open);
}

void
gtk_draw_layout (GtkStyle        *style,
                 GdkWindow       *window,
                 GtkStateType     state_type,
1120
		 gboolean         use_text,
1121 1122 1123 1124 1125 1126 1127
                 gint             x,
                 gint             y,
                 PangoLayout     *layout)
{
  g_return_if_fail (GTK_IS_STYLE (style));
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_layout != NULL);
  
1128
  GTK_STYLE_GET_CLASS (style)->draw_layout (style, window, state_type, use_text,
1129 1130 1131 1132
                                            NULL, NULL, NULL,
                                            x, y, layout);
}

Owen Taylor's avatar
Owen Taylor committed
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
void
gtk_draw_resize_grip (GtkStyle     *style,
                      GdkWindow    *window,
                      GtkStateType  state_type,
                      GdkWindowEdge edge,
                      gint          x,
                      gint          y,
                      gint          width,
                      gint          height)
{
  g_return_if_fail (GTK_IS_STYLE (style));
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_resize_grip != NULL);

  GTK_STYLE_GET_CLASS (style)->draw_resize_grip (style, window, state_type,
                                                 NULL, NULL, NULL,
                                                 edge,
                                                 x, y, width, height);
}


1153
void
1154 1155 1156
gtk_style_set_background (GtkStyle    *style,
                          GdkWindow   *window,
                          GtkStateType state_type)
1157
{
1158
  g_return_if_fail (GTK_IS_STYLE (style));
1159 1160
  g_return_if_fail (window != NULL);
  
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
  GTK_STYLE_GET_CLASS (style)->set_background (style, window, state_type);
}

/* Default functions */
static GtkStyle *
gtk_style_real_clone (GtkStyle *style)
{
  return GTK_STYLE (g_object_new (G_OBJECT_TYPE (style), NULL));
}

static void
gtk_style_real_copy (GtkStyle *style,
		     GtkStyle *src)
{
  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];
      
      style->bg_pixmap[i] = src->bg_pixmap[i];
    }

  if (style->font)
    gdk_font_unref (style->font);
  style->font = src->font;
  if (style->font)
    gdk_font_ref (style->font);

  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)
    gtk_rc_style_unref (style->rc_style);
  style->rc_style = src->rc_style;
  if (src->rc_style)
    gtk_rc_style_ref (src->rc_style);
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

  /* don't copy, just clear cache */
  if (style->property_cache)
    {
      guint i;

      for (i = 0; i < style->property_cache->n_nodes; i++)
	{
	  PropertyValue *node = g_bsearch_array_get_nth (style->property_cache, i);

	  g_param_spec_unref (node->pspec);
	  g_value_unset (&node->value);
	}
      g_bsearch_array_destroy (style->property_cache);
      style->property_cache = NULL;
    }
1224 1225 1226 1227 1228 1229 1230 1231 1232
}

static void
gtk_style_real_init_from_rc (GtkStyle   *style,
			     GtkRcStyle *rc_style)
{
  GdkFont *old_font;
  gint i;

1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
  /* cache _should_ be still empty */
  if (style->property_cache)
    {
      guint i;

      for (i = 0; i < style->property_cache->n_nodes; i++)
	{
	  PropertyValue *node = g_bsearch_array_get_nth (style->property_cache, i);

	  g_param_spec_unref (node->pspec);
	  g_value_unset (&node->value);
	}
      g_bsearch_array_destroy (style->property_cache);
      style->property_cache = NULL;
    }

1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
  if (rc_style->font_desc)
    {
      pango_font_description_free (style->font_desc);
      style->font_desc = pango_font_description_copy (rc_style->font_desc);

      old_font = style->font;
      style->font = gdk_font_from_description (style->font_desc);
      if (style->font)
	gdk_font_unref (old_font);
      else
	style->font = old_font;
    }
    
  for (i = 0; i < 5; i++)
1263
    {
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
      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;
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

  if (rc_style->icon_factories)
    {
      GSList *iter;

      style->icon_factories = g_slist_copy (rc_style->icon_factories);
      
      iter = style->icon_factories;
      while (iter != NULL)
        {
          g_object_ref (G_OBJECT (iter->data));
          
          iter = g_slist_next (iter);
        }
    }
1293 1294
}

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
static gint
style_property_values_cmp (gconstpointer bsearch_node1,
			   gconstpointer bsearch_node2)
{
  const PropertyValue *val1 = bsearch_node1;
  const PropertyValue *val2 = bsearch_node2;
  gint cmp;

  cmp = G_BSEARCH_ARRAY_CMP (val1->widget_type, val2->widget_type);
  if (cmp == 0)
    cmp = G_BSEARCH_ARRAY_CMP (val1->pspec, val2->pspec);

  return cmp;
}

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;

  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);

  /* need value cache array */
  if (!style->property_cache)
    style->property_cache = g_bsearch_array_new (sizeof (PropertyValue),
						 style_property_values_cmp,
						 0);
  /* lookup, or insert value if not yet present */
  key.widget_type = widget_type;
  key.pspec = pspec;
  pcache = g_bsearch_array_insert (style->property_cache, &key, FALSE);
  if (G_VALUE_TYPE (&pcache->value))
    return &pcache->value;

  /* cache miss, initialize value type, then set contents */
1337
  g_param_spec_ref (pcache->pspec);
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
  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 */
1358 1359
  if (rcprop && !_gtk_settings_parse_convert (parser, &rcprop->value,
					      pspec, &pcache->value))
1360
    {
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
      gchar *contents = g_strdup_value_contents (&rcprop->value);
      
      g_message ("%s: failed to retrive property `%s::%s' of type `%s' from rc file value \"%s\" of type `%s'",
		 rcprop->origin,
		 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 */
1371 1372 1373 1374 1375 1376 1377 1378 1379
    }
  
  /* not supplied by rc style (or conversion failed), revert to default */
  if (!rcprop)
    g_param_value_set_default (pspec, &pcache->value);

  return &pcache->value;
}

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
GdkPixmap *
load_bg_image (GdkColormap *colormap,
	       GdkColor    *bg_color,
	       const gchar *filename)
{
  if (strcmp (filename, "<parent>") == 0)
    return (GdkPixmap*) GDK_PARENT_RELATIVE;
  else
    {
      return gdk_pixmap_colormap_create_from_xpm (NULL, colormap, NULL,
						  bg_color,
						  filename);
    }
}

1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
static void
gtk_style_real_realize (GtkStyle *style)
{
  GdkGCValues gc_values;
  GdkGCValuesMask gc_values_mask;
  
  gint i;

  for (i = 0; i < 5; i++)
    {
      gtk_style_shade (&style->bg[i], &style->light[i], LIGHTNESS_MULT);
      gtk_style_shade (&style->bg[i], &style->dark[i], DARKNESS_MULT);
1407
      
1408 1409 1410
      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;
1411 1412 1413 1414

      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;
1415 1416
    }
  
1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  gdk_color_black (style->colormap, &style->black);
  gdk_color_white (style->colormap, &style->white);
  
  gc_values_mask = GDK_GC_FOREGROUND | GDK_GC_FONT;
  if (style->font->type == GDK_FONT_FONT)
    {
      gc_values.font = style->font;
    }
  else if (style->font->type == GDK_FONT_FONTSET)
    {
1427
      gc_values.font = static_default_font;
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
    }
  
  gc_values.foreground = style->black;
  style->black_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
  
  gc_values.foreground = style->white;
  style->white_gc = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
  
  for (i = 0; i < 5; i++)
    {
      if (style->rc_style && style->rc_style->bg_pixmap_name[i])
1439 1440 1441
        style->bg_pixmap[i] = load_bg_image (style->colormap,
					     &style->bg[i],
					     style->rc_style->bg_pixmap_name[i]);
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
      
      if (!gdk_color_alloc (style->colormap, &style->fg[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->fg[i].red, style->fg[i].green, style->fg[i].blue);
      if (!gdk_color_alloc (style->colormap, &style->bg[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->bg[i].red, style->bg[i].green, style->bg[i].blue);
      if (!gdk_color_alloc (style->colormap, &style->light[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->light[i].red, style->light[i].green, style->light[i].blue);
      if (!gdk_color_alloc (style->colormap, &style->dark[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->dark[i].red, style->dark[i].green, style->dark[i].blue);
      if (!gdk_color_alloc (style->colormap, &style->mid[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->mid[i].red, style->mid[i].green, style->mid[i].blue);
      if (!gdk_color_alloc (style->colormap, &style->text[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->text[i].red, style->text[i].green, style->text[i].blue);
      if (!gdk_color_alloc (style->colormap, &style->base[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->base[i].red, style->base[i].green, style->base[i].blue);
1464 1465 1466
      if (!gdk_color_alloc (style->colormap, &style->text_aa[i]))
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->text_aa[i].red, style->text_aa[i].green, style->text_aa[i].blue);
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
      
      gc_values.foreground = style->fg[i];
      style->fg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
      
      gc_values.foreground = style->bg[i];
      style->bg_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
      
      gc_values.foreground = style->light[i];
      style->light_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
      
      gc_values.foreground = style->dark[i];
      style->dark_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
      
      gc_values.foreground = style->mid[i];
      style->mid_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
      
      gc_values.foreground = style->text[i];
      style->text_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
      
      gc_values.foreground = style->base[i];
      style->base_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1488 1489 1490

      gc_values.foreground = style->text_aa[i];
      style->text_aa_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
    }
}

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

  gtk_gc_release (style->black_gc);
  gtk_gc_release (style->white_gc);
      
  for (i = 0; i < 5; i++)
    {
      gtk_gc_release (style->fg_gc[i]);
      gtk_gc_release (style->bg_gc[i]);
      gtk_gc_release (style->light_gc[i]);
      gtk_gc_release (style->dark_gc[i]);
      gtk_gc_release (style->mid_gc[i]);
      gtk_gc_release (style->text_gc[i]);
      gtk_gc_release (style->base_gc[i]);
1511 1512 1513

      if (style->bg_pixmap[i] &&  style->bg_pixmap[i] != (GdkPixmap*) GDK_PARENT_RELATIVE)
	gdk_pixmap_unref (style->bg_pixmap[i]);
1514
    }