gtkstyle.c 123 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 22 23 24 25 26

/*
 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
 * 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"
Elliot Lee's avatar
Elliot Lee committed
31
#include "gtkstyle.h"
32
#include "gtkwidget.h"
33
#include "gtkthemes.h"
Elliot Lee's avatar
Elliot Lee committed
34 35 36 37 38


#define LIGHTNESS_MULT  1.3
#define DARKNESS_MULT   0.7

39 40 41 42 43 44 45 46
/* actually glib should do that for us */
#ifndef M_PI
#define M_PI    3.14159265358979323846
#endif /* M_PI */
#ifndef M_PI_4
#define M_PI_4  0.78539816339744830962
#endif /* M_PI_4 */

47 48 49
static void         gtk_style_realize (GtkStyle    *style,
                                       GdkColormap *colormap,
                                       gint         depth);
Elliot Lee's avatar
Elliot Lee committed
50

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
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_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_oval       (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_cross      (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_ramp       (GtkStyle        *style,
					 GdkWindow       *window,
					 GtkStateType     state_type,
					 GtkShadowType    shadow_type,
					 GdkRectangle    *area,
					 GtkWidget       *widget,
					 const gchar     *detail,
					 GtkArrowType     arrow_type,
					 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);
Elliot Lee's avatar
Elliot Lee committed
296 297 298 299 300

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

301
GdkFont *default_font = NULL;
Elliot Lee's avatar
Elliot Lee committed
302 303 304 305 306 307 308 309 310 311 312 313 314

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

315 316 317 318 319 320 321 322
static gpointer parent_class = NULL;

static void gtk_style_init       (GtkStyle      *style);
static void gtk_style_class_init (GtkStyleClass *klass);
static void gtk_style_finalize   (GObject       *object);

GType
gtk_style_get_type (void)
323
{
324
  static GType object_type = 0;
Stuart Parmenter's avatar
Stuart Parmenter committed
325

326
  if (!object_type)
Stuart Parmenter's avatar
Stuart Parmenter committed
327
    {
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
      static const GTypeInfo object_info =
      {
        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,
      };
      
      object_type = g_type_register_static (G_TYPE_OBJECT,
                                            "GtkStyle",
                                            &object_info);
Stuart Parmenter's avatar
Stuart Parmenter committed
344
    }
345
  
346
  return object_type;
347
}
Elliot Lee's avatar
Elliot Lee committed
348

349 350
static void
gtk_style_init (GtkStyle *style)
Elliot Lee's avatar
Elliot Lee committed
351 352
{
  gint i;
353
  
354
  style->font_desc = pango_font_description_from_string ("Sans 10");
Elliot Lee's avatar
Elliot Lee committed
355

Elliot Lee's avatar
Elliot Lee committed
356
  if (!default_font)
357
    {
358 359
      default_font = gdk_font_from_description (style->font_desc);

360 361 362
      if (!default_font)
	g_error ("Unable to load default font.");
    }
363
  
Elliot Lee's avatar
Elliot Lee committed
364 365
  style->font = default_font;
  gdk_font_ref (style->font);
Elliot Lee's avatar
Elliot Lee committed
366

Elliot Lee's avatar
Elliot Lee committed
367 368 369
  style->attach_count = 0;
  style->colormap = NULL;
  style->depth = -1;
370
  
Elliot Lee's avatar
Elliot Lee committed
371 372 373
  style->black.red = 0;
  style->black.green = 0;
  style->black.blue = 0;
374
  
Elliot Lee's avatar
Elliot Lee committed
375 376 377
  style->white.red = 65535;
  style->white.green = 65535;
  style->white.blue = 65535;
378
  
Elliot Lee's avatar
Elliot Lee committed
379 380
  style->black_gc = NULL;
  style->white_gc = NULL;
381
  
Elliot Lee's avatar
Elliot Lee committed
382 383 384 385 386
  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;
387
  
Elliot Lee's avatar
Elliot Lee committed
388 389 390 391 392
  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;
393
  
394
  for (i = 0; i < 4; i++)
Elliot Lee's avatar
Elliot Lee committed
395 396 397 398
    {
      style->text[i] = style->fg[i];
      style->base[i] = style->white;
    }
399
  
Owen Taylor's avatar
Owen Taylor committed
400 401
  style->base[GTK_STATE_INSENSITIVE] = gtk_default_prelight_bg;
  style->text[GTK_STATE_INSENSITIVE] = gtk_default_insensitive_fg;
402
  
Elliot Lee's avatar
Elliot Lee committed
403 404
  for (i = 0; i < 5; i++)
    style->bg_pixmap[i] = NULL;
405
  
406 407
  style->engine = NULL;
  style->engine_data = NULL;
408
  
409
  style->rc_style = NULL;
410
  
Elliot Lee's avatar
Elliot Lee committed
411 412 413 414 415 416 417 418 419 420
  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;
    }
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

  style->xthickness = 2;
  style->ythickness = 2;
}

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;

  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_oval = gtk_default_draw_oval;
  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_cross = gtk_default_draw_cross;
  klass->draw_ramp = gtk_default_draw_ramp;
  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;
}

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

  g_return_if_fail (style->attach_count == 0);
  
  if (style->styles)
    {
      if (style->styles->data != style)
        g_slist_remove (style->styles, style);
      else
        {
          GSList *tmp_list = style->styles->next;
	  
          while (tmp_list)
            {
              ((GtkStyle*) tmp_list->data)->styles = style->styles->next;
              tmp_list = tmp_list->next;
            }
          g_slist_free_1 (style->styles);
        }
    }
  
  if (style->engine)
    {
      style->engine->destroy_style (style);
      gtk_theme_engine_unref (style->engine);
    }
  
  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;
  guint i;
  
  g_return_val_if_fail (style != NULL, NULL);
  
  new_style = gtk_style_new ();
  
  for (i = 0; i < 5; i++)
    {
      new_style->fg[i] = style->fg[i];
      new_style->bg[i] = style->bg[i];
      new_style->text[i] = style->text[i];
      new_style->base[i] = style->base[i];
      
      new_style->bg_pixmap[i] = style->bg_pixmap[i];
    }
  
  gdk_font_unref (new_style->font);
  new_style->font = style->font;
  gdk_font_ref (new_style->font);

  pango_font_description_free (new_style->font_desc);
  new_style->font_desc = pango_font_description_copy (style->font_desc);
  
  if (style->rc_style)
    {
      new_style->rc_style = style->rc_style;
      gtk_rc_style_ref (style->rc_style);
    }
  
  if (style->engine)
    {
      new_style->engine = style->engine;
      gtk_theme_engine_ref (new_style->engine);
      new_style->engine->duplicate_style (new_style, style);
    }

  return new_style;
}

static GtkStyle*
gtk_style_duplicate (GtkStyle *style)
{
  GtkStyle *new_style;
  
  g_return_val_if_fail (style != NULL, NULL);
  
  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;
  
  style = GTK_STYLE (g_type_create_instance (gtk_style_get_type ()));
562
  
Elliot Lee's avatar
Elliot Lee committed
563 564 565
  return style;
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
/*************************************************************
 * 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.
 *************************************************************/

599
GtkStyle*
Elliot Lee's avatar
Elliot Lee committed
600
gtk_style_attach (GtkStyle  *style,
601
                  GdkWindow *window)
Elliot Lee's avatar
Elliot Lee committed
602
{
603 604
  GSList *styles;
  GtkStyle *new_style = NULL;
Elliot Lee's avatar
Elliot Lee committed
605 606
  GdkColormap *colormap;
  gint depth;
607
  
Elliot Lee's avatar
Elliot Lee committed
608 609
  g_return_val_if_fail (style != NULL, NULL);
  g_return_val_if_fail (window != NULL, NULL);
610
  
Elliot Lee's avatar
Elliot Lee committed
611
  colormap = gdk_window_get_colormap (window);
612
  depth = gdk_window_get_visual (window)->depth;
613
  
614 615
  if (!style->styles)
    style->styles = g_slist_append (NULL, style);
616
  
617 618
  styles = style->styles;
  while (styles)
Elliot Lee's avatar
Elliot Lee committed
619
    {
620
      new_style = styles->data;
621
      
622
      if (new_style->attach_count == 0)
623
        {
624
          gtk_style_realize (new_style, colormap, depth);
625 626 627 628 629
          break;
        }
      else if (new_style->colormap == colormap &&
               new_style->depth == depth)
        break;
630
      
631 632
      new_style = NULL;
      styles = styles->next;
Elliot Lee's avatar
Elliot Lee committed
633
    }
634
  
635 636 637
  if (!new_style)
    {
      new_style = gtk_style_duplicate (style);
638
      gtk_style_realize (new_style, colormap, depth);
639
    }
640 641

  /* A style gets a refcount from being attached */
642 643
  if (new_style->attach_count == 0)
    gtk_style_ref (new_style);
644 645 646 647 648 649 650

  /* Another refcount belongs to the parent */
  if (style != new_style) 
    {
      gtk_style_unref (style);
      gtk_style_ref (new_style);
    }
651
  
652
  new_style->attach_count++;
653
  
654
  return new_style;
Elliot Lee's avatar
Elliot Lee committed
655 656 657 658 659 660
}

void
gtk_style_detach (GtkStyle *style)
{
  gint i;
661
  
Elliot Lee's avatar
Elliot Lee committed
662
  g_return_if_fail (style != NULL);
663
  
Elliot Lee's avatar
Elliot Lee committed
664 665 666
  style->attach_count -= 1;
  if (style->attach_count == 0)
    {
667
      if (style->engine)
668
        style->engine->unrealize_style (style);
669
      
Elliot Lee's avatar
Elliot Lee committed
670 671
      gtk_gc_release (style->black_gc);
      gtk_gc_release (style->white_gc);
672
      
Elliot Lee's avatar
Elliot Lee committed
673
      for (i = 0; i < 5; i++)
674 675 676 677 678 679 680 681 682
        {
          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]);
        }
683
      
684
      gtk_style_unref (style);
Elliot Lee's avatar
Elliot Lee committed
685 686 687 688 689 690
    }
}

GtkStyle*
gtk_style_ref (GtkStyle *style)
{
691
  return (GtkStyle *) g_object_ref (G_OBJECT (style));
Elliot Lee's avatar
Elliot Lee committed
692 693 694 695 696
}

void
gtk_style_unref (GtkStyle *style)
{
697
  g_object_unref (G_OBJECT (style));
Elliot Lee's avatar
Elliot Lee committed
698 699
}

700
static void
701 702 703
gtk_style_realize (GtkStyle    *style,
                   GdkColormap *colormap,
                   gint         depth)
704 705 706 707
{
  GdkGCValues gc_values;
  GdkGCValuesMask gc_values_mask;
  gint i;
708
  
709
  g_return_if_fail (style != NULL);
710
  
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
  style->colormap = colormap;
  style->depth = depth;
  
  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);
      
      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;
    }
  
  gdk_color_black (colormap, &style->black);
  gdk_color_white (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)
    {
      gc_values.font = default_font;
    }
  
  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])
746 747 748
        style->bg_pixmap[i] = gtk_rc_load_image (style->colormap,
                                                 &style->bg[i],
                                                 style->rc_style->bg_pixmap_name[i]);
749 750
      
      if (!gdk_color_alloc (colormap, &style->fg[i]))
751 752
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->fg[i].red, style->fg[i].green, style->fg[i].blue);
753
      if (!gdk_color_alloc (colormap, &style->bg[i]))
754 755
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->bg[i].red, style->bg[i].green, style->bg[i].blue);
756
      if (!gdk_color_alloc (colormap, &style->light[i]))
757 758
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->light[i].red, style->light[i].green, style->light[i].blue);
759
      if (!gdk_color_alloc (colormap, &style->dark[i]))
760 761
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->dark[i].red, style->dark[i].green, style->dark[i].blue);
762
      if (!gdk_color_alloc (colormap, &style->mid[i]))
763 764
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->mid[i].red, style->mid[i].green, style->mid[i].blue);
765
      if (!gdk_color_alloc (colormap, &style->text[i]))
766 767
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->text[i].red, style->text[i].green, style->text[i].blue);
768
      if (!gdk_color_alloc (colormap, &style->base[i]))
769 770
        g_warning ("unable to allocate color: ( %d %d %d )",
                   style->base[i].red, style->base[i].green, style->base[i].blue);
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
      
      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);
789
      
790 791 792
      gc_values.foreground = style->base[i];
      style->base_gc[i] = gtk_gc_get (style->depth, style->colormap, &gc_values, gc_values_mask);
    }
793
  
794 795 796 797
  if (style->engine)
    style->engine->realize_style (style);
}

Elliot Lee's avatar
Elliot Lee committed
798 799
void
gtk_draw_hline (GtkStyle     *style,
800 801 802 803 804
                GdkWindow    *window,
                GtkStateType  state_type,
                gint          x1,
                gint          x2,
                gint          y)
Elliot Lee's avatar
Elliot Lee committed
805 806
{
  g_return_if_fail (style != NULL);
807
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_hline != NULL);
808
  
809
  GTK_STYLE_GET_CLASS (style)->draw_hline (style, window, state_type, NULL, NULL, NULL, x1, x2, y);
Elliot Lee's avatar
Elliot Lee committed
810 811 812 813 814
}


void
gtk_draw_vline (GtkStyle     *style,
815 816 817 818 819
                GdkWindow    *window,
                GtkStateType  state_type,
                gint          y1,
                gint          y2,
                gint          x)
Elliot Lee's avatar
Elliot Lee committed
820 821
{
  g_return_if_fail (style != NULL);
822
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_vline != NULL);
823
  
824
  GTK_STYLE_GET_CLASS (style)->draw_vline (style, window, state_type, NULL, NULL, NULL, y1, y2, x);
Elliot Lee's avatar
Elliot Lee committed
825 826 827 828 829
}


void
gtk_draw_shadow (GtkStyle      *style,
830 831 832 833 834 835 836
                 GdkWindow     *window,
                 GtkStateType   state_type,
                 GtkShadowType  shadow_type,
                 gint           x,
                 gint           y,
                 gint           width,
                 gint           height)
Elliot Lee's avatar
Elliot Lee committed
837 838
{
  g_return_if_fail (style != NULL);
839
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow != NULL);
840
  
841
  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
842 843 844 845
}

void
gtk_draw_polygon (GtkStyle      *style,
846 847 848 849 850 851
                  GdkWindow     *window,
                  GtkStateType   state_type,
                  GtkShadowType  shadow_type,
                  GdkPoint      *points,
                  gint           npoints,
                  gboolean       fill)
Elliot Lee's avatar
Elliot Lee committed
852 853
{
  g_return_if_fail (style != NULL);
854
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_polygon != NULL);
855
  
856
  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
857 858 859 860
}

void
gtk_draw_arrow (GtkStyle      *style,
861 862 863 864 865 866 867 868 869
                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
870 871
{
  g_return_if_fail (style != NULL);
872
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_arrow != NULL);
873
  
874
  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
875 876 877 878 879
}


void
gtk_draw_diamond (GtkStyle      *style,
880 881 882 883 884 885 886
                  GdkWindow     *window,
                  GtkStateType   state_type,
                  GtkShadowType  shadow_type,
                  gint           x,
                  gint           y,
                  gint           width,
                  gint           height)
Elliot Lee's avatar
Elliot Lee committed
887 888
{
  g_return_if_fail (style != NULL);
889
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_diamond != NULL);
890
  
891
  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
892 893 894 895 896
}


void
gtk_draw_oval (GtkStyle      *style,
897 898 899 900 901 902 903
               GdkWindow     *window,
               GtkStateType   state_type,
               GtkShadowType  shadow_type,
               gint           x,
               gint           y,
               gint           width,
               gint           height)
Elliot Lee's avatar
Elliot Lee committed
904 905
{
  g_return_if_fail (style != NULL);
906
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_oval != NULL);
907
  
908
  GTK_STYLE_GET_CLASS (style)->draw_oval (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
909 910 911 912
}

void
gtk_draw_string (GtkStyle      *style,
913 914 915 916 917
                 GdkWindow     *window,
                 GtkStateType   state_type,
                 gint           x,
                 gint           y,
                 const gchar   *string)
Elliot Lee's avatar
Elliot Lee committed
918 919
{
  g_return_if_fail (style != NULL);
920
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_string != NULL);
921
  
922
  GTK_STYLE_GET_CLASS (style)->draw_string (style, window, state_type, NULL, NULL, NULL, x, y, string);
Elliot Lee's avatar
Elliot Lee committed
923 924
}

925 926
void
gtk_draw_box (GtkStyle      *style,
927 928 929 930 931 932 933
              GdkWindow     *window,
              GtkStateType   state_type,
              GtkShadowType  shadow_type,
              gint           x,
              gint           y,
              gint           width,
              gint           height)
Elliot Lee's avatar
Elliot Lee committed
934
{
935
  g_return_if_fail (style != NULL);
936
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box != NULL);
937
  
938
  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
939 940
}

941 942
void
gtk_draw_flat_box (GtkStyle      *style,
943 944 945 946 947 948 949
                   GdkWindow     *window,
                   GtkStateType   state_type,
                   GtkShadowType  shadow_type,
                   gint           x,
                   gint           y,
                   gint           width,
                   gint           height)
Elliot Lee's avatar
Elliot Lee committed
950
{
951
  g_return_if_fail (style != NULL);
952
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_flat_box != NULL);
953
  
954
  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
955 956
}

957
void
958 959 960 961 962 963 964 965
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
966
{
967
  g_return_if_fail (style != NULL);
968
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_check != NULL);
969
  
970
  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
971 972
}

973
void
974 975 976 977 978 979 980 981
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
982
{
983
  g_return_if_fail (style != NULL);
984
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_option != NULL);
985
  
986
  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
987 988
}

989
void
990 991 992 993 994 995 996 997
gtk_draw_cross (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
998
{
999
  g_return_if_fail (style != NULL);
1000
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_cross != NULL);
1001
  
1002
  GTK_STYLE_GET_CLASS (style)->draw_cross (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1003
}
Elliot Lee's avatar
Elliot Lee committed
1004

1005
void
1006 1007 1008 1009 1010 1011 1012 1013 1014
gtk_draw_ramp (GtkStyle      *style,
	       GdkWindow     *window,
	       GtkStateType   state_type,
	       GtkShadowType  shadow_type,
	       GtkArrowType   arrow_type,
	       gint           x,
	       gint           y,
	       gint           width,
	       gint           height)
1015
{
1016
  g_return_if_fail (style != NULL);
1017
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_ramp != NULL);
1018
  
1019
  GTK_STYLE_GET_CLASS (style)->draw_ramp (style, window, state_type, shadow_type, NULL, NULL, NULL, arrow_type, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
1020 1021
}

1022
void
1023 1024 1025 1026 1027 1028 1029 1030
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
1031
{
1032
  g_return_if_fail (style != NULL);
1033
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_tab != NULL);
1034
  
1035
  GTK_STYLE_GET_CLASS (style)->draw_tab (style, window, state_type, shadow_type, NULL, NULL, NULL, x, y, width, height);
1036
}
Elliot Lee's avatar
Elliot Lee committed
1037

1038 1039
void
gtk_draw_shadow_gap (GtkStyle       *style,
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
                     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)
1050
{
1051
  g_return_if_fail (style != NULL);
1052
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_shadow_gap != NULL);
1053
  
1054
  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
1055 1056
}

1057 1058
void
gtk_draw_box_gap (GtkStyle       *style,
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
                  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
1069
{
1070
  g_return_if_fail (style != NULL);
1071
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_box_gap != NULL);
1072
  
1073
  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);
1074
}
Elliot Lee's avatar
Elliot Lee committed
1075

1076 1077
void
gtk_draw_extension (GtkStyle       *style,
1078 1079 1080 1081 1082 1083 1084 1085
                    GdkWindow      *window,
                    GtkStateType    state_type,
                    GtkShadowType   shadow_type,
                    gint            x,
                    gint            y,
                    gint            width,
                    gint            height,
                    GtkPositionType gap_side)
1086
{
1087
  g_return_if_fail (style != NULL);
1088
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_extension != NULL);
1089
  
1090
  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
1091 1092
}

1093
void
1094 1095 1096 1097 1098 1099
gtk_draw_focus (GtkStyle      *style,
		GdkWindow     *window,
		gint           x,
		gint           y,
		gint           width,
		gint           height)
Elliot Lee's avatar
Elliot Lee committed
1100
{
1101
  g_return_if_fail (style != NULL);
1102
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_focus != NULL);
1103
  
1104
  GTK_STYLE_GET_CLASS (style)->draw_focus (style, window, NULL, NULL, NULL, x, y, width, height);
Elliot Lee's avatar
Elliot Lee committed
1105 1106
}

1107
void 
1108 1109 1110 1111 1112 1113 1114 1115 1116
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
1117
{
1118
  g_return_if_fail (style != NULL);
1119
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_slider != NULL);
1120
  
1121
  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
1122 1123
}

1124
void 
1125 1126 1127 1128 1129 1130 1131 1132 1133
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
1134
{
1135
  g_return_if_fail (style != NULL);
1136
  g_return_if_fail (GTK_STYLE_GET_CLASS (style)->draw_handle != NULL);
1137
  
1138
  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
1139 1140
}

1141
void
1142 1143 1144
gtk_style_set_background (GtkStyle    *style,
                          GdkWindow   *window,
                          GtkStateType state_type)
1145 1146 1147 1148
{
  GdkPixmap *pixmap;
  gint parent_relative;
  
1149 1150 1151
  g_return_if_fail (style != NULL);
  g_return_if_fail (window != NULL);
  
1152 1153
  if (style->engine && style->engine->set_background)
    {
1154 1155
      style->engine->set_background (style, window, state_type);
      
1156 1157 1158 1159 1160 1161
      return;
    }
  
  if (style->bg_pixmap[state_type])
    {
      if (style->bg_pixmap[state_type] == (GdkPixmap*) GDK_PARENT_RELATIVE)
1162 1163 1164 1165
        {
          pixmap = NULL;
          parent_relative = TRUE;
        }
1166
      else
1167 1168 1169 1170
        {
          pixmap = style->bg_pixmap[state_type];
          parent_relative = FALSE;
        }
1171 1172 1173 1174 1175 1176 1177 1178
      
      gdk_window_set_back_pixmap (window, pixmap, parent_relative);
    }
  else
    gdk_window_set_background (window, &style->bg[state_type]);
}


1179 1180
/* Default functions */
void
1181
gtk_style_apply_default_background (GtkStyle     *style,
1182 1183 1184 1185 1186 1187 1188 1189
                                    GdkWindow    *window,
                                    gboolean      set_bg,
                                    GtkStateType  state_type, 
                                    GdkRectangle *area, 
                                    gint          x, 
                                    gint          y, 
                                    gint          width, 
                                    gint          height)
Elliot Lee's avatar
Elliot Lee committed
1190
{
1191
  GdkRectangle new_rect, old_rect;
1192
  
1193
  if (area)
Elliot Lee's avatar
Elliot Lee committed
1194
    {
1195 1196 1197 1198 1199
      old_rect.x = x;
      old_rect.y = y;
      old_rect.width = width;
      old_rect.height = height;
      
1200
      if (!gdk_rectangle_intersect (area, &old_rect, &new_rect))
1201
        return;
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
    }
  else
    {
      new_rect.x = x;
      new_rect.y = y;
      new_rect.width = width;
      new_rect.height = height;
    }
  
  if (!style->bg_pixmap[state_type] ||
1212
      GDK_IS_PIXMAP (window) ||
1213
      (!set_bg && style->bg_pixmap[state_type] != (GdkPixmap*) GDK_PARENT_RELATIVE))
1214 1215
    {
      GdkGC *gc = style->bg_gc[state_type];
1216
      
1217
      if (style->bg_pixmap[state_type])
1218 1219 1220 1221 1222
        {
          gdk_gc_set_fill (gc, GDK_TILED);
          gdk_gc_set_tile (gc, style->bg_pixmap[state_type]);
        }
      
1223
      gdk_draw_rectangle (window, gc, TRUE, 
1224
                          new_rect.x, new_rect.y, new_rect.width, new_rect.height);
1225
      if (style->bg_pixmap[state_type])
1226
        gdk_gc_set_fill (gc, GDK_SOLID);
1227
    }
1228
  else
1229
    {
1230
      if (set_bg)
1231 1232 1233 1234 1235 1236
        {
          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);
        }
1237
      
1238
      gdk_window_clear_area (window, 
1239 1240
                             new_rect.x, new_rect.y, 
                             new_rect.width, new_rect.height);
1241
    }
Elliot Lee's avatar
Elliot Lee committed
1242 1243 1244 1245
}

static void
gtk_default_draw_hline (GtkStyle     *style,
1246 1247 1248 1249
                        GdkWindow    *window,
                        GtkStateType  state_type,
                        GdkRectangle  *area,
                        GtkWidget     *widget,
1250
                        const gchar   *detail,
1251 1252 1253
                        gint          x1,
                        gint          x2,
                        gint          y)
Elliot Lee's avatar
Elliot Lee committed
1254
{
1255 1256 1257 1258 1259 1260 1261
  gint thickness_light;
  gint thickness_dark;
  gint i;
  
  g_return_if_fail (style != NULL);
  g_return_if_fail (window != NULL);
  
1262 1263
  thickness_light = style->ythickness / 2;
  thickness_dark = style->ythickness - thickness_light;
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
  
  if (area)
    {
      gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
      gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
    }
  
  if (detail && !strcmp (detail, "label"))
    {
      if (state_type == GTK_STATE_INSENSITIVE)
1274
        gdk_draw_line (window, style->white_gc, x1 + 1, y + 1, x2 + 1, y + 1);   
1275 1276 1277 1278 1279
      gdk_draw_line (window, style->fg_gc[state_type], x1, y, x2, y);     
    }
  else
    {
      for (i = 0; i < thickness_dark; i++)
1280 1281 1282 1283
        {
          gdk_draw_line (window, style->light_gc[state_type], x2 - i - 1, y + i, x2, y + i);
          gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x2 - i - 1, y + i);
        }
1284 1285 1286
      
      y += thickness_dark;
      for (i = 0; i < thickness_light; i++)
1287 1288 1289 1290
        {
          gdk_draw_line (window, style->dark_gc[state_type], x1, y + i, x1 + thickness_light - i - 1, y + i);
          gdk_draw_line (window, style->light_gc[state_type], x1 + thickness_light - i - 1, y + i, x2, y + i);
        }
1291 1292 1293 1294 1295 1296 1297
    }
  
  if (area)
    {
      gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
      gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
    }
Elliot Lee's avatar
Elliot Lee committed
1298 1299 1300 1301 1302
}


static void
gtk_default_draw_vline (GtkStyle     *style,
1303 1304 1305 1306
                        GdkWindow    *window,
                        GtkStateType  state_type,
                        GdkRectangle  *area,
                        GtkWidget     *widget,
1307
                        const gchar   *detail,
1308 1309 1310
                        gint          y1,
                        gint          y2,
                        gint          x)
Elliot Lee's avatar
Elliot Lee committed
1311
{
1312 1313 1314 1315 1316 1317 1318
  gint thickness_light;
  gint thickness_dark;
  gint i;
  
  g_return_if_fail (style != NULL);
  g_return_if_fail (window != NULL);
  
1319 1320
  thickness_light = style->xthickness / 2;
  thickness_dark = style->xthickness - thickness_light;
1321 1322 1323 1324 1325 1326 1327 1328 1329
  
  if (area)
    {
      gdk_gc_set_clip_rectangle (style->light_gc[state_type], area);
      gdk_gc_set_clip_rectangle (style->dark_gc[state_type], area);
    }
  for (i = 0; i < thickness_dark; i++)
    {
      gdk_draw_line (window, style->light_gc[state_type], x + i, y2 - i - 1, x + i, y2);
Elliot Lee's avatar
Elliot Lee committed
1330
      gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y2 - i - 1);
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343
    }
  
  x += thickness_dark;
  for (i = 0; i < thickness_light; i++)
    {
      gdk_draw_line (window, style->dark_gc[state_type], x + i, y1, x + i, y1 + thickness_light - i);
      gdk_draw_line (window, style->light_gc[state_type], x + i, y1 + thickness_light - i, x + i, y2);
    }
  if (area)
    {
      gdk_gc_set_clip_rectangle (style->light_gc[state_type], NULL);
      gdk_gc_set_clip_rectangle (style->dark_gc[state_type], NULL);
    }
Elliot Lee's avatar
Elliot Lee committed
1344 1345 1346 1347 1348
}


static void
gtk_default_draw_shadow (GtkStyle      *style,
1349 1350 1351 1352 1353
                         GdkWindow     *window,
                         GtkStateType   state_type,
                         GtkShadowType  shadow_type,
                         GdkRectangle  *area,
                         GtkWidget     *widget,
1354
                         const gchar   *detail,
1355 1356 1357 1358
                         gint           x,
                         gint           y,
                         gint           width,
                         gint           height)
Elliot Lee's avatar
Elliot Lee committed
1359
{
1360 1361
  GdkGC *gc1 = NULL;
  GdkGC *gc2 = NULL;
Elliot Lee's avatar
Elliot Lee committed
1362 1363 1364
  gint thickness_light;
  gint thickness_dark;
  gint i;
1365
  
Elliot Lee's avatar
Elliot Lee committed
1366 1367
  g_return_if_fail (style != NULL);
  g_return_if_fail (window != NULL);
1368
  
Elliot Lee's avatar
Elliot Lee committed
1369 1370 1371 1372 1373 1374
  if ((width == -1) && (height == -1))
    gdk_window_get_size (window, &width, &height);
  else if (width == -1)
    gdk_window_get_size (window, &width, NULL);
  else if (height == -1)
    gdk_window_get_size (window, NULL, &height);
1375
  
Elliot Lee's avatar
Elliot Lee committed
1376 1377 1378
  switch (shadow_type)
    {
    case GTK_SHADOW_NONE:
1379
      return;
Elliot Lee's avatar
Elliot Lee committed
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
    case GTK_SHADOW_IN:
    case GTK_SHADOW_ETCHED_IN:
      gc1 = style->light_gc[state_type];
      gc2 = style->dark_gc[state_type];
      break;
    case GTK_SHADOW_OUT:
    case GTK_SHADOW_ETCHED_OUT:
      gc1 = style->dark_gc[state_type];
      gc2 = style->light_gc[state_type];
      break;
    }
1391 1392 1393 1394 1395 1396
  
  if (area)
    {
      gdk_gc_set_clip_rectangle (gc1, area);
      gdk_gc_set_clip_rectangle (gc2, area);
      if (shadow_type == GTK_SHADOW_IN || 
1397 1398 1399 1400 1401
          shadow_type == GTK_SHADOW_OUT)
        {
          gdk_gc_set_clip_rectangle (style->black_gc, area);
          gdk_gc_set_clip_rectangle (style->bg_gc[state_type], area);
        }
1402 1403 1404 1405
    }
  
  switch (shadow_type)
    {
Elliot Lee's avatar
Elliot Lee committed
1406 1407
    case GTK_SHADOW_NONE:
      break;
1408
      
Elliot Lee's avatar
Elliot Lee committed
1409 1410 1411 1412 1413
    case GTK_SHADOW_IN:
      gdk_draw_line (window, gc1,
                     x, y + height - 1, x + width - 1, y + height - 1);
      gdk_draw_line (window, gc1,
                     x + width - 1, y, x + width - 1, y + height - 1);
1414
      
Elliot Lee's avatar
Elliot Lee committed
1415 1416 1417 1418
      gdk_draw_line (window, style->bg_gc[state_type],
                     x + 1, y + height - 2, x + width - 2, y + height - 2);
      gdk_draw_line (window, style->bg_gc[state_type],
                     x + width - 2, y + 1, x + width - 2, y + height - 2);
1419
      
Elliot Lee's avatar
Elliot Lee committed
1420 1421 1422 1423
      gdk_draw_line (window, style->black_gc,
                     x + 1, y + 1, x + width - 2, y + 1);
      gdk_draw_line (window, style->black_gc,
                     x + 1, y + 1, x + 1, y + height - 2);
1424
      
Elliot Lee's avatar
Elliot Lee committed
1425 1426 1427 1428 1429
      gdk_draw_line (window, gc2,
                     x, y, x + width - 1, y);
      gdk_draw_line (