gtkstyleproperty.c 39.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

22
#include "gtkstylepropertyprivate.h"
23 24

#include <errno.h>
25
#include <math.h>
26 27 28 29 30 31 32
#include <stdlib.h>
#include <string.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <cairo-gobject.h>

#include "gtkcssprovider.h"
Benjamin Otte's avatar
Benjamin Otte committed
33
#include "gtkcssparserprivate.h"
34
#include "gtkcssshorthandpropertyprivate.h"
35
#include "gtkcssstylefuncsprivate.h"
36
#include "gtkcssstylepropertyprivate.h"
37
#include "gtkcsstypesprivate.h"
38
#include "gtkintl.h"
39
#include "gtkprivatetypebuiltins.h"
40
#include "gtkstylepropertiesprivate.h"
41 42 43

/* the actual parsers we have */
#include "gtkanimationdescription.h"
44
#include "gtkbindings.h"
45
#include "gtkgradient.h"
46
#include "gtkshadowprivate.h"
47
#include "gtkthemingengine.h"
48
#include "gtktypebuiltins.h"
49
#include "gtkwin32themeprivate.h"
50

Chun-wei Fan's avatar
Chun-wei Fan committed
51 52 53 54 55
/* this is in case round() is not provided by the compiler, 
 * such as in the case of C89 compilers, like MSVC
 */
#include "fallback-c89.c"

56 57
enum {
  PROP_0,
58 59
  PROP_NAME,
  PROP_VALUE_TYPE
60 61
};

62
G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
63 64 65 66 67 68

static void
gtk_style_property_finalize (GObject *object)
{
  GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);

69
  g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
70 71 72 73

  G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static void
gtk_style_property_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
  GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);

  switch (prop_id)
    {
    case PROP_NAME:
      property->name = g_value_dup_string (value);
      g_assert (property->name);
      g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
      g_hash_table_insert (klass->properties, property->name, property);
      break;
91 92 93
    case PROP_VALUE_TYPE:
      property->value_type = g_value_get_gtype (value);
      break;
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_style_property_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, property->name);
      break;
113 114 115
    case PROP_VALUE_TYPE:
      g_value_set_gtype (value, property->value_type);
      break;
116 117 118 119 120 121
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

122 123 124 125 126 127
static gboolean
gtk_style_property_real_parse_value (GtkStyleProperty *property,
                                     GValue           *value,
                                     GtkCssParser     *parser,
                                     GFile            *base);

128 129 130 131 132 133
static void
_gtk_style_property_class_init (GtkStylePropertyClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_style_property_finalize;
134 135 136 137 138 139 140 141 142 143
  object_class->set_property = gtk_style_property_set_property;
  object_class->get_property = gtk_style_property_get_property;

  g_object_class_install_property (object_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        P_("Property name"),
                                                        P_("The name of the property"),
                                                        NULL,
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
144 145 146 147 148 149 150
  g_object_class_install_property (object_class,
                                   PROP_VALUE_TYPE,
                                   g_param_spec_gtype ("value-type",
                                                       P_("Value type"),
                                                       P_("The value type returned by GtkStyleContext"),
                                                       G_TYPE_NONE,
                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
151

152 153
  klass->parse_value = gtk_style_property_real_parse_value;

154
  klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
155 156 157 158 159
}

static void
_gtk_style_property_init (GtkStyleProperty *property)
{
160
  property->value_type = G_TYPE_NONE;
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
static void
string_append_double (GString *string,
                      double   d)
{
  char buf[G_ASCII_DTOSTR_BUF_SIZE];

  g_ascii_dtostr (buf, sizeof (buf), d);
  g_string_append (string, buf);
}

static void
string_append_string (GString    *str,
                      const char *string)
{
  gsize len;

  g_string_append_c (str, '"');

  do {
    len = strcspn (string, "\"\n\r\f");
    g_string_append (str, string);
    string += len;
    switch (*string)
      {
      case '\0':
        break;
      case '\n':
        g_string_append (str, "\\A ");
        break;
      case '\r':
        g_string_append (str, "\\D ");
        break;
      case '\f':
        g_string_append (str, "\\C ");
        break;
      case '\"':
        g_string_append (str, "\\\"");
        break;
      default:
        g_assert_not_reached ();
        break;
      }
  } while (*string);

  g_string_append_c (str, '"');
}

210 211
/*** IMPLEMENTATIONS ***/

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
static gboolean
font_family_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
{
  GPtrArray *names;
  char *name;

  /* We don't special case generic families. Pango should do
   * that for us */

  names = g_ptr_array_new ();

  do {
    name = _gtk_css_parser_try_ident (parser, TRUE);
    if (name)
      {
        GString *string = g_string_new (name);
        g_free (name);
        while ((name = _gtk_css_parser_try_ident (parser, TRUE)))
          {
            g_string_append_c (string, ' ');
            g_string_append (string, name);
            g_free (name);
          }
        name = g_string_free (string, FALSE);
      }
    else 
      {
        name = _gtk_css_parser_read_string (parser);
        if (name == NULL)
          {
            g_ptr_array_free (names, TRUE);
            return FALSE;
          }
      }

    g_ptr_array_add (names, name);
  } while (_gtk_css_parser_try (parser, ",", TRUE));

  /* NULL-terminate array */
  g_ptr_array_add (names, NULL);
  g_value_set_boxed (value, g_ptr_array_free (names, FALSE));
  return TRUE;
}

static void
font_family_value_print (const GValue *value,
                         GString      *string)
{
  const char **names = g_value_get_boxed (value);

  if (names == NULL || *names == NULL)
    {
      g_string_append (string, "none");
      return;
    }

  string_append_string (string, *names);
  names++;
  while (*names)
    {
      g_string_append (string, ", ");
      string_append_string (string, *names);
      names++;
    }
}

280
static gboolean 
281 282 283
bindings_value_parse (GtkCssParser *parser,
                      GFile        *base,
                      GValue       *value)
284
{
285 286 287
  GPtrArray *array;
  GtkBindingSet *binding_set;
  char *name;
288

289
  array = g_ptr_array_new ();
290

291 292 293 294 295 296 297 298
  do {
      name = _gtk_css_parser_try_ident (parser, TRUE);
      if (name == NULL)
        {
          _gtk_css_parser_error (parser, "Not a valid binding name");
          g_ptr_array_free (array, TRUE);
          return FALSE;
        }
299

300
      binding_set = gtk_binding_set_find (name);
301

302 303 304 305 306 307
      if (!binding_set)
        {
          _gtk_css_parser_error (parser, "No binding set named '%s'", name);
          g_free (name);
          continue;
        }
308

309 310
      g_ptr_array_add (array, binding_set);
      g_free (name);
311
    }
312
  while (_gtk_css_parser_try (parser, ",", TRUE));
313

314
  g_value_take_boxed (value, array);
315 316 317 318

  return TRUE;
}

319
static void
320 321
bindings_value_print (const GValue *value,
                      GString      *string)
322
{
323 324
  GPtrArray *array;
  guint i;
325

326
  array = g_value_get_boxed (value);
Benjamin Otte's avatar
Benjamin Otte committed
327

328
  for (i = 0; i < array->len; i++)
329
    {
330
      GtkBindingSet *binding_set = g_ptr_array_index (array, i);
331

332 333 334
      if (i > 0)
        g_string_append (string, ", ");
      g_string_append (string, binding_set->set_name);
335
    }
336 337
}

338
static gboolean 
339 340 341
border_corner_radius_value_parse (GtkCssParser *parser,
                                  GFile        *base,
                                  GValue       *value)
342
{
343
  GtkCssBorderCornerRadius corner;
344

345
  if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
346
    {
347
      _gtk_css_parser_error (parser, "Expected a number");
348 349
      return FALSE;
    }
350 351
  else if (corner.horizontal < 0)
    goto negative;
352

353 354 355 356
  if (!_gtk_css_parser_try_double (parser, &corner.vertical))
    corner.vertical = corner.horizontal;
  else if (corner.vertical < 0)
    goto negative;
357

358
  g_value_set_boxed (value, &corner);
359 360
  return TRUE;

361 362 363
negative:
  _gtk_css_parser_error (parser, "Border radius values cannot be negative");
  return FALSE;
364 365
}

366 367 368
static void
border_corner_radius_value_print (const GValue *value,
                                  GString      *string)
369
{
370
  GtkCssBorderCornerRadius *corner;
371

372
  corner = g_value_get_boxed (value);
373

374
  if (corner == NULL)
375
    {
376 377
      g_string_append (string, "none");
      return;
378
    }
379

380 381
  string_append_double (string, corner->horizontal);
  if (corner->horizontal != corner->vertical)
382
    {
383 384
      g_string_append_c (string, ' ');
      string_append_double (string, corner->vertical);
385 386 387
    }
}

388
/*** API ***/
389

390 391 392 393 394
gboolean
_gtk_style_property_parse_value (GtkStyleProperty *property,
                                 GValue           *value,
                                 GtkCssParser     *parser,
                                 GFile            *base)
395
{
396 397
  GtkStylePropertyClass *klass;

398
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
399 400
  g_return_val_if_fail (value != NULL, FALSE);
  g_return_val_if_fail (parser != NULL, FALSE);
401

402 403 404 405 406 407 408 409 410 411 412
  klass = GTK_STYLE_PROPERTY_GET_CLASS (property);

  return klass->parse_value (property, value, parser, base);
}

static gboolean
gtk_style_property_real_parse_value (GtkStyleProperty *property,
                                     GValue           *value,
                                     GtkCssParser     *parser,
                                     GFile            *base)
{
413
  if (_gtk_css_parser_try (parser, "initial", TRUE))
414
    {
415 416 417 418 419 420 421
      /* the initial value can be explicitly specified with the
       * ‘initial’ keyword which all properties accept.
       */
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
      g_value_set_enum (value, GTK_CSS_INITIAL);
      return TRUE;
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
  else if (_gtk_css_parser_try (parser, "inherit", TRUE))
    {
      /* All properties accept the ‘inherit’ value which
       * explicitly specifies that the value will be determined
       * by inheritance. The ‘inherit’ value can be used to
       * strengthen inherited values in the cascade, and it can
       * also be used on properties that are not normally inherited.
       */
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_CSS_SPECIAL_VALUE);
      g_value_set_enum (value, GTK_CSS_INHERIT);
      return TRUE;
    }
  else if (property->property_parse_func)
    {
      GError *error = NULL;
      char *value_str;
      gboolean success;
      
      value_str = _gtk_css_parser_read_value (parser);
      if (value_str == NULL)
        return FALSE;
      
      success = (*property->property_parse_func) (value_str, value, &error);

      g_free (value_str);

      return success;
    }
  else if (property->parse_func)
    return (* property->parse_func) (parser, base, value);
  else
    return _gtk_css_style_parse_value (value, parser, base);
456 457
}

458
GParameter *
459 460 461
_gtk_style_property_unpack (GtkStyleProperty *property,
                            const GValue     *value,
                            guint            *n_params)
462 463 464 465 466 467 468 469 470
{
  g_return_val_if_fail (property != NULL, NULL);
  g_return_val_if_fail (property->unpack_func != NULL, NULL);
  g_return_val_if_fail (value != NULL, NULL);
  g_return_val_if_fail (n_params != NULL, NULL);

  return property->unpack_func (value, n_params);
}

471 472 473 474 475 476 477 478 479 480 481 482
/**
 * _gtk_style_property_assign:
 * @property: the property
 * @props: The properties to assign to
 * @state: The state to assign
 * @value: (out): the #GValue with the value to be
 *     assigned
 *
 * This function is called by gtk_style_properties_set() and in
 * turn gtk_style_context_set() and similar functions to set the
 * value from code using old APIs.
 **/
483 484 485 486 487 488
void
_gtk_style_property_assign (GtkStyleProperty   *property,
                            GtkStyleProperties *props,
                            GtkStateFlags       state,
                            const GValue       *value)
{
489 490
  GtkStylePropertyClass *klass;

491 492 493 494
  g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
  g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
  g_return_if_fail (value != NULL);

495
  klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
496

497
  klass->assign (property, props, state, value);
498 499
}

500 501 502 503 504 505 506 507 508 509 510 511
/**
 * _gtk_style_property_query:
 * @property: the property
 * @props: The properties to query
 * @state: The state to query
 * @value: (out): an uninitialized #GValue to be filled with the
 *   contents of the lookup
 *
 * This function is called by gtk_style_properties_get() and in
 * turn gtk_style_context_get() and similar functions to get the
 * value to return to code using old APIs.
 **/
512
void
513
_gtk_style_property_query (GtkStyleProperty        *property,
514 515 516 517 518
                           GtkStyleProperties      *props,
                           GtkStateFlags            state,
			   GtkStylePropertyContext *context,
                           GValue                  *value)
{
519
  GtkStylePropertyClass *klass;
520 521 522 523 524 525

  g_return_if_fail (property != NULL);
  g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
  g_return_if_fail (context != NULL);
  g_return_if_fail (value != NULL);

526
  klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
527

528 529 530
  g_value_init (value, property->value_type);

  klass->query (property, props, state, context, value);
531 532
}

533 534 535 536 537 538
#define rgba_init(rgba, r, g, b, a) G_STMT_START{ \
  (rgba)->red = (r); \
  (rgba)->green = (g); \
  (rgba)->blue = (b); \
  (rgba)->alpha = (a); \
}G_STMT_END
539
static void
540
gtk_style_property_init_properties (void)
541
{
542
  static gboolean initialized = FALSE;
543 544 545 546
  GValue value = { 0, };
  char *default_font_family[] = { "Sans", NULL };
  GdkRGBA rgba;

547
  if (G_LIKELY (initialized))
548 549
    return;

550
  initialized = TRUE;
551

552 553 554
  g_value_init (&value, GDK_TYPE_RGBA);
  rgba_init (&rgba, 1, 1, 1, 1);
  g_value_set_boxed (&value, &rgba);
555 556 557 558 559 560 561 562
  _gtk_style_property_register           (g_param_spec_boxed ("color",
                                          "Foreground color",
                                          "Foreground color",
                                          GDK_TYPE_RGBA, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
563
                                          &value);
564 565
  rgba_init (&rgba, 0, 0, 0, 0);
  g_value_set_boxed (&value, &rgba);
566 567 568 569 570 571
  _gtk_style_property_register           (g_param_spec_boxed ("background-color",
                                          "Background color",
                                          "Background color",
                                          GDK_TYPE_RGBA, 0),
                                          0,
                                          NULL,
572
                                          NULL,
573
                                          NULL,
574
                                          &value);
575
  g_value_unset (&value);
576

577 578
  g_value_init (&value, G_TYPE_STRV);
  g_value_set_boxed (&value, default_font_family);
579 580 581 582 583 584 585 586
  _gtk_style_property_register           (g_param_spec_boxed ("font-family",
                                                              "Font family",
                                                              "Font family",
                                                              G_TYPE_STRV, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          font_family_parse,
                                          font_family_value_print,
587
                                          &value);
588
  g_value_unset (&value);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
  _gtk_style_property_register           (g_param_spec_enum ("font-style",
                                                             "Font style",
                                                             "Font style",
                                                             PANGO_TYPE_STYLE,
                                                             PANGO_STYLE_NORMAL, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL);
  _gtk_style_property_register           (g_param_spec_enum ("font-variant",
                                                             "Font variant",
                                                             "Font variant",
                                                             PANGO_TYPE_VARIANT,
                                                             PANGO_VARIANT_NORMAL, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL);
  /* xxx: need to parse this properly, ie parse the numbers */
  _gtk_style_property_register           (g_param_spec_enum ("font-weight",
                                                             "Font weight",
                                                             "Font weight",
                                                             PANGO_TYPE_WEIGHT,
                                                             PANGO_WEIGHT_NORMAL, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL);
620 621
  g_value_init (&value, G_TYPE_DOUBLE);
  g_value_set_double (&value, 10);
622 623 624 625 626 627 628 629
  _gtk_style_property_register           (g_param_spec_double ("font-size",
                                                               "Font size",
                                                               "Font size",
                                                               0, G_MAXDOUBLE, 0, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
630
                                          &value);
631
  g_value_unset (&value);
632

633 634 635 636 637 638 639 640 641
  _gtk_style_property_register           (g_param_spec_boxed ("text-shadow",
                                                              "Text shadow",
                                                              "Text shadow",
                                                              GTK_TYPE_SHADOW, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL);
642 643 644 645 646 647 648 649 650 651

  _gtk_style_property_register           (g_param_spec_boxed ("icon-shadow",
                                                              "Icon shadow",
                                                              "Icon shadow",
                                                              GTK_TYPE_SHADOW, 0),
                                          GTK_STYLE_PROPERTY_INHERIT,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL);
652

653 654 655 656 657
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_boxed ("box-shadow",
                                                              "Box shadow",
                                                              "Box shadow",
                                                              GTK_TYPE_SHADOW, 0));
658
  gtk_style_properties_register_property (NULL,
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
                                          g_param_spec_int ("margin-top",
                                                            "margin top",
                                                            "Margin at top",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("margin-left",
                                                            "margin left",
                                                            "Margin at left",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("margin-bottom",
                                                            "margin bottom",
                                                            "Margin at bottom",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("margin-right",
                                                            "margin right",
                                                            "Margin at right",
                                                            0, G_MAXINT, 0, 0));
678
  gtk_style_properties_register_property (NULL,
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
                                          g_param_spec_int ("padding-top",
                                                            "padding top",
                                                            "Padding at top",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("padding-left",
                                                            "padding left",
                                                            "Padding at left",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("padding-bottom",
                                                            "padding bottom",
                                                            "Padding at bottom",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("padding-right",
                                                            "padding right",
                                                            "Padding at right",
                                                            0, G_MAXINT, 0, 0));
698
  gtk_style_properties_register_property (NULL,
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
                                          g_param_spec_int ("border-top-width",
                                                            "border top width",
                                                            "Border width at top",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("border-left-width",
                                                            "border left width",
                                                            "Border width at left",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("border-bottom-width",
                                                            "border bottom width",
                                                            "Border width at bottom",
                                                            0, G_MAXINT, 0, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_int ("border-right-width",
                                                            "border right width",
                                                            "Border width at right",
                                                            0, G_MAXINT, 0, 0));
718 719 720 721 722

  _gtk_style_property_register           (g_param_spec_boxed ("border-top-left-radius",
                                                              "Border top left radius",
                                                              "Border radius of top left corner, in pixels",
                                                              GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
Benjamin Otte's avatar
Benjamin Otte committed
723
                                          0,
724 725
                                          NULL,
                                          border_corner_radius_value_parse,
726 727
                                          border_corner_radius_value_print,
                                          NULL);
728 729 730 731
  _gtk_style_property_register           (g_param_spec_boxed ("border-top-right-radius",
                                                              "Border top right radius",
                                                              "Border radius of top right corner, in pixels",
                                                              GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
Benjamin Otte's avatar
Benjamin Otte committed
732
                                          0,
733 734
                                          NULL,
                                          border_corner_radius_value_parse,
735 736
                                          border_corner_radius_value_print,
                                          NULL);
737 738 739 740
  _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-right-radius",
                                                              "Border bottom right radius",
                                                              "Border radius of bottom right corner, in pixels",
                                                              GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
Benjamin Otte's avatar
Benjamin Otte committed
741
                                          0,
742 743
                                          NULL,
                                          border_corner_radius_value_parse,
744 745
                                          border_corner_radius_value_print,
                                          NULL);
746 747 748 749
  _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-left-radius",
                                                              "Border bottom left radius",
                                                              "Border radius of bottom left corner, in pixels",
                                                              GTK_TYPE_CSS_BORDER_CORNER_RADIUS, 0),
Benjamin Otte's avatar
Benjamin Otte committed
750
                                          0,
751 752
                                          NULL,
                                          border_corner_radius_value_parse,
753 754
                                          border_corner_radius_value_print,
                                          NULL);
755

756 757 758 759 760 761
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_enum ("border-style",
                                                             "Border style",
                                                             "Border style",
                                                             GTK_TYPE_BORDER_STYLE,
                                                             GTK_BORDER_STYLE_NONE, 0));
762 763 764 765 766 767 768 769 770 771 772 773
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_enum ("background-clip",
                                                             "Background clip",
                                                             "Background clip",
                                                             GTK_TYPE_CSS_AREA,
                                                             GTK_CSS_AREA_BORDER_BOX, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_enum ("background-origin",
                                                             "Background origin",
                                                             "Background origin",
                                                             GTK_TYPE_CSS_AREA,
                                                             GTK_CSS_AREA_PADDING_BOX, 0));
774 775
  g_value_init (&value, GTK_TYPE_CSS_SPECIAL_VALUE);
  g_value_set_enum (&value, GTK_CSS_CURRENT_COLOR);
776 777 778 779 780 781
  _gtk_style_property_register           (g_param_spec_boxed ("border-top-color",
                                                              "Border top color",
                                                              "Border top color",
                                                              GDK_TYPE_RGBA, 0),
                                          0,
                                          NULL,
782
                                          NULL,
783
                                          NULL,
784
                                          &value);
785 786 787 788 789 790
  _gtk_style_property_register           (g_param_spec_boxed ("border-right-color",
                                                              "Border right color",
                                                              "Border right color",
                                                              GDK_TYPE_RGBA, 0),
                                          0,
                                          NULL,
791
                                          NULL,
792
                                          NULL,
793
                                          &value);
794 795 796
  _gtk_style_property_register           (g_param_spec_boxed ("border-bottom-color",
                                                              "Border bottom color",
                                                              "Border bottom color",
797 798 799
                                                              GDK_TYPE_RGBA, 0),
                                          0,
                                          NULL,
800
                                          NULL,
801
                                          NULL,
802
                                          &value);
803 804 805 806 807 808
  _gtk_style_property_register           (g_param_spec_boxed ("border-left-color",
                                                              "Border left color",
                                                              "Border left color",
                                                              GDK_TYPE_RGBA, 0),
                                          0,
                                          NULL,
809
                                          NULL,
810
                                          NULL,
811
                                          &value);
812
  g_value_unset (&value);
813

814 815 816 817 818
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_boxed ("background-image",
                                                              "Background Image",
                                                              "Background Image",
                                                              CAIRO_GOBJECT_TYPE_PATTERN, 0));
819 820 821 822 823 824
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_boxed ("background-repeat",
                                                              "Background repeat",
                                                              "Background repeat",
                                                              GTK_TYPE_CSS_BACKGROUND_REPEAT, 0));

825
  gtk_style_properties_register_property (NULL,
826 827 828 829 830 831 832 833
                                          g_param_spec_boxed ("border-image-source",
                                                              "Border image source",
                                                              "Border image source",
                                                              CAIRO_GOBJECT_TYPE_PATTERN, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_boxed ("border-image-repeat",
                                                              "Border image repeat",
                                                              "Border image repeat",
834
                                                              GTK_TYPE_CSS_BORDER_IMAGE_REPEAT, 0));
835 836 837 838 839
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_boxed ("border-image-slice",
                                                              "Border image slice",
                                                              "Border image slice",
                                                              GTK_TYPE_BORDER, 0));
840
  g_value_init (&value, GTK_TYPE_BORDER);
841
  _gtk_style_property_register           (g_param_spec_boxed ("border-image-width",
842 843
                                                              "Border image width",
                                                              "Border image width",
844 845 846 847 848
                                                              GTK_TYPE_BORDER, 0),
                                          0,
                                          NULL,
                                          NULL,
                                          NULL,
849
                                          &value);
850
  g_value_unset (&value);
851 852 853 854 855 856 857 858 859 860 861 862
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_object ("engine",
                                                               "Theming Engine",
                                                               "Theming Engine",
                                                               GTK_TYPE_THEMING_ENGINE, 0));
  gtk_style_properties_register_property (NULL,
                                          g_param_spec_boxed ("transition",
                                                              "Transition animation description",
                                                              "Transition animation description",
                                                              GTK_TYPE_ANIMATION_DESCRIPTION, 0));

  /* Private property holding the binding sets */
863
  _gtk_style_property_register           (g_param_spec_boxed ("gtk-key-bindings",
864 865
                                                              "Key bindings",
                                                              "Key bindings",
866
                                                              G_TYPE_PTR_ARRAY, 0),
Benjamin Otte's avatar
Benjamin Otte committed
867
                                          0,
868 869
                                          NULL,
                                          bindings_value_parse,
870 871
                                          bindings_value_print,
                                          NULL);
872 873 874

  /* initialize shorthands last, they depend on the real properties existing */
  _gtk_css_shorthand_property_init_properties ();
875 876
}

877 878 879 880 881 882 883 884 885 886
/**
 * _gtk_style_property_lookup:
 * @name: name of the property to lookup
 *
 * Looks up the CSS property with the given @name. If no such
 * property exists, %NULL is returned.
 *
 * Returns: (transfer none): The property or %NULL if no
 *     property with the given name exists.
 **/
887
GtkStyleProperty *
888 889
_gtk_style_property_lookup (const char *name)
{
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
  GtkStylePropertyClass *klass;

  g_return_val_if_fail (name != NULL, NULL);

  gtk_style_property_init_properties ();

  klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);

  return g_hash_table_lookup (klass->properties, name);
}

/**
 * _gtk_style_property_get_name:
 * @property: the property to query
 *
 * Gets the name of the given property.
 *
 * Returns: the name of the property
 **/
const char *
_gtk_style_property_get_name (GtkStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
913

914
  return property->name;
915 916
}

917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
/**
 * _gtk_style_property_get_value_type:
 * @property: the property to query
 *
 * Gets the value type of the @property, if the property is usable
 * in public API via _gtk_style_property_assign() and
 * _gtk_style_property_query(). If the @property is not usable in that
 * way, %G_TYPE_NONE is returned.
 *
 * Returns: the value type in use or %G_TYPE_NONE if none.
 **/
GType
_gtk_style_property_get_value_type (GtkStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);

  return property->value_type;
}

936

937
void
938
_gtk_style_property_register (GParamSpec               *pspec,
Benjamin Otte's avatar
Benjamin Otte committed
939
                              GtkStylePropertyFlags     flags,
940 941 942
                              GtkStylePropertyParser    property_parse_func,
                              GtkStyleParseFunc         parse_func,
                              GtkStylePrintFunc         print_func,
943
                              const GValue *            initial_value)
944 945
{
  GtkStyleProperty *node;
946
  GValue initial_fallback = { 0, };
947

948
  if (initial_value == NULL)
949
    {
950
      g_value_init (&initial_fallback, pspec->value_type);
951
      if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
952
        g_value_set_object (&initial_fallback, gtk_theming_engine_load (NULL));
953
      else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
954
        g_value_take_boxed (&initial_fallback, pango_font_description_from_string ("Sans 10"));
955
      else if (pspec->value_type == GDK_TYPE_RGBA)
956
        {
957 958
          GdkRGBA color;
          gdk_rgba_parse (&color, "pink");
959
          g_value_set_boxed (&initial_fallback, &color);
960
        }
961
      else if (pspec->value_type == GTK_TYPE_BORDER)
962
        {
963
          g_value_take_boxed (&initial_fallback, gtk_border_new ());
964
        }
965
      else
966 967 968
        g_param_value_set_default (pspec, &initial_fallback);

      initial_value = &initial_fallback;
969
    }
970 971 972 973 974 975 976

  node = g_object_new (GTK_TYPE_CSS_STYLE_PROPERTY,
                       "inherit", (flags & GTK_STYLE_PROPERTY_INHERIT) ? TRUE : FALSE,
                       "initial-value", initial_value,
                       "name", pspec->name,
                       "value-type", pspec->value_type,
                       NULL);
977
  g_assert (node->value_type == pspec->value_type);
978
  GTK_CSS_STYLE_PROPERTY (node)->pspec = pspec;
979 980 981 982 983 984
  node->property_parse_func = property_parse_func;
  node->parse_func = parse_func;
  node->print_func = print_func;

  if (G_IS_VALUE (&initial_fallback))
    g_value_unset (&initial_fallback);
985
}