gtkstyleproperty.c 79.5 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 35
#include "gtkcssshorthandpropertyprivate.h"
#include "gtkcssstylepropertyprivate.h"
36
#include "gtkcsstypesprivate.h"
37
#include "gtkintl.h"
38
#include "gtkprivatetypebuiltins.h"
39
#include "gtkstylepropertiesprivate.h"
40 41 42

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

Chun-wei Fan's avatar
Chun-wei Fan committed
50 51 52 53 54
/* 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"

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

61
static GHashTable *parse_funcs = NULL;
62
static GHashTable *print_funcs = NULL;
63

64
G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
65 66 67 68 69 70

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

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

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

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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;
93 94 95
    case PROP_VALUE_TYPE:
      property->value_type = g_value_get_gtype (value);
      break;
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    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;
115 116 117
    case PROP_VALUE_TYPE:
      g_value_set_gtype (value, property->value_type);
      break;
118 119 120 121 122 123
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

124 125 126 127 128 129
static void
_gtk_style_property_class_init (GtkStylePropertyClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_style_property_finalize;
130 131 132 133 134 135 136 137 138 139
  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));
140 141 142 143 144 145 146
  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));
147 148

  klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
149 150 151 152 153
}

static void
_gtk_style_property_init (GtkStyleProperty *property)
{
154
  property->value_type = G_TYPE_NONE;
155 156
}

157
static void
158 159 160
register_conversion_function (GType             type,
                              GtkStyleParseFunc parse,
                              GtkStylePrintFunc print)
161
{
162 163
  if (parse)
    g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
164 165
  if (print)
    g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
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
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, '"');
}

215 216 217
/*** IMPLEMENTATIONS ***/

static gboolean 
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
enum_parse (GtkCssParser *parser,
	    GType         type,
	    int          *res)
{
  char *str;

  if (_gtk_css_parser_try_enum (parser, type, res))
    return TRUE;

  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
    {
      _gtk_css_parser_error (parser, "Expected an identifier");
      return FALSE;
    }

  _gtk_css_parser_error (parser,
			 "Unknown value '%s' for enum type '%s'",
			 str, g_type_name (type));
  g_free (str);

  return FALSE;
}

static void
enum_print (int         value,
	    GType       type,
	    GString    *string)
{
  GEnumClass *enum_class;
  GEnumValue *enum_value;

  enum_class = g_type_class_ref (type);
  enum_value = g_enum_get_value (enum_class, value);

  g_string_append (string, enum_value->value_nick);

  g_type_class_unref (enum_class);
}

static gboolean
259 260 261
rgba_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
262 263 264 265
{
  GtkSymbolicColor *symbolic;
  GdkRGBA rgba;

266
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
267 268 269
  if (symbolic == NULL)
    return FALSE;

270 271 272
  if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
    {
      g_value_set_boxed (value, &rgba);
273
      gtk_symbolic_color_unref (symbolic);
274 275 276 277 278 279 280 281
    }
  else
    {
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
      g_value_take_boxed (value, symbolic);
    }

282 283 284
  return TRUE;
}

285 286 287
static void
rgba_value_print (const GValue *value,
                  GString      *string)
288 289 290 291
{
  const GdkRGBA *rgba = g_value_get_boxed (value);

  if (rgba == NULL)
292 293 294 295 296 297 298
    g_string_append (string, "none");
  else
    {
      char *s = gdk_rgba_to_string (rgba);
      g_string_append (string, s);
      g_free (s);
    }
299 300 301
}

static gboolean 
302 303 304
color_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
305 306
{
  GtkSymbolicColor *symbolic;
307
  GdkRGBA rgba;
308

309
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
310 311 312
  if (symbolic == NULL)
    return FALSE;

313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
    {
      GdkColor color;

      color.red = rgba.red * 65535. + 0.5;
      color.green = rgba.green * 65535. + 0.5;
      color.blue = rgba.blue * 65535. + 0.5;

      g_value_set_boxed (value, &color);
    }
  else
    {
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
      g_value_take_boxed (value, symbolic);
    }

330 331 332
  return TRUE;
}

333 334 335
static void
color_value_print (const GValue *value,
                   GString      *string)
336 337 338 339
{
  const GdkColor *color = g_value_get_boxed (value);

  if (color == NULL)
340 341 342 343 344 345 346
    g_string_append (string, "none");
  else
    {
      char *s = gdk_color_to_string (color);
      g_string_append (string, s);
      g_free (s);
    }
347 348
}

349 350 351 352
static gboolean
symbolic_color_value_parse (GtkCssParser *parser,
                            GFile        *base,
                            GValue       *value)
353 354 355
{
  GtkSymbolicColor *symbolic;

356
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
357 358 359 360 361 362 363
  if (symbolic == NULL)
    return FALSE;

  g_value_take_boxed (value, symbolic);
  return TRUE;
}

364 365 366
static void
symbolic_color_value_print (const GValue *value,
                            GString      *string)
367 368 369 370
{
  GtkSymbolicColor *symbolic = g_value_get_boxed (value);

  if (symbolic == NULL)
371 372 373 374 375 376 377
    g_string_append (string, "none");
  else
    {
      char *s = gtk_symbolic_color_to_string (symbolic);
      g_string_append (string, s);
      g_free (s);
    }
378 379
}

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 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
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++;
    }
}

448
static gboolean 
449 450 451
font_description_value_parse (GtkCssParser *parser,
                              GFile        *base,
                              GValue       *value)
452 453
{
  PangoFontDescription *font_desc;
454
  guint mask;
455 456 457 458 459
  char *str;

  str = _gtk_css_parser_read_value (parser);
  if (str == NULL)
    return FALSE;
460 461

  font_desc = pango_font_description_from_string (str);
462 463 464 465 466 467 468
  mask = pango_font_description_get_set_fields (font_desc);
  /* These values are not really correct,
   * but the fields must be set, so we set them to something */
  if ((mask & PANGO_FONT_MASK_FAMILY) == 0)
    pango_font_description_set_family_static (font_desc, "Sans");
  if ((mask & PANGO_FONT_MASK_SIZE) == 0)
    pango_font_description_set_size (font_desc, 10 * PANGO_SCALE);
469
  g_free (str);
470 471 472 473
  g_value_take_boxed (value, font_desc);
  return TRUE;
}

474 475 476
static void
font_description_value_print (const GValue *value,
                              GString      *string)
477 478 479 480
{
  const PangoFontDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
481 482 483 484 485 486 487
    g_string_append (string, "none");
  else
    {
      char *s = pango_font_description_to_string (desc);
      g_string_append (string, s);
      g_free (s);
    }
488 489 490
}

static gboolean 
491 492 493
boolean_value_parse (GtkCssParser *parser,
                     GFile        *base,
                     GValue       *value)
494
{
495 496
  if (_gtk_css_parser_try (parser, "true", TRUE) ||
      _gtk_css_parser_try (parser, "1", TRUE))
497 498 499 500
    {
      g_value_set_boolean (value, TRUE);
      return TRUE;
    }
501 502
  else if (_gtk_css_parser_try (parser, "false", TRUE) ||
           _gtk_css_parser_try (parser, "0", TRUE))
503 504 505 506
    {
      g_value_set_boolean (value, FALSE);
      return TRUE;
    }
507 508 509 510 511
  else
    {
      _gtk_css_parser_error (parser, "Expected a boolean value");
      return FALSE;
    }
512 513
}

514 515 516
static void
boolean_value_print (const GValue *value,
                     GString      *string)
517 518
{
  if (g_value_get_boolean (value))
519
    g_string_append (string, "true");
520
  else
521
    g_string_append (string, "false");
522 523 524
}

static gboolean 
525 526 527
int_value_parse (GtkCssParser *parser,
                 GFile        *base,
                 GValue       *value)
528
{
529
  gint i;
530

531 532 533 534 535 536 537 538 539 540 541
  if (_gtk_css_parser_begins_with (parser, '-'))
    {
      int res = _gtk_win32_theme_int_parse (parser, base, &i);
      if (res >= 0)
	{
	  g_value_set_int (value, i);
	  return res > 0;
	}
      /* < 0 => continue */
    }

542
  if (!_gtk_css_parser_try_int (parser, &i))
543
    {
544
      _gtk_css_parser_error (parser, "Expected a valid integer value");
545 546 547 548 549 550 551
      return FALSE;
    }

  g_value_set_int (value, i);
  return TRUE;
}

552 553 554
static void
int_value_print (const GValue *value,
                 GString      *string)
555
{
556
  g_string_append_printf (string, "%d", g_value_get_int (value));
557 558 559
}

static gboolean 
560 561 562
uint_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
563
{
564
  guint u;
Benjamin Otte's avatar
Benjamin Otte committed
565

566
  if (!_gtk_css_parser_try_uint (parser, &u))
567
    {
568
      _gtk_css_parser_error (parser, "Expected a valid unsigned value");
569 570 571 572 573 574 575
      return FALSE;
    }

  g_value_set_uint (value, u);
  return TRUE;
}

576 577 578
static void
uint_value_print (const GValue *value,
                  GString      *string)
579
{
580
  g_string_append_printf (string, "%u", g_value_get_uint (value));
581 582 583
}

static gboolean 
584 585 586
double_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
587
{
588
  gdouble d;
589

590
  if (!_gtk_css_parser_try_double (parser, &d))
591
    {
592
      _gtk_css_parser_error (parser, "Expected a number");
593 594 595 596 597 598 599
      return FALSE;
    }

  g_value_set_double (value, d);
  return TRUE;
}

600 601 602 603 604 605 606
static void
double_value_print (const GValue *value,
                    GString      *string)
{
  string_append_double (string, g_value_get_double (value));
}

607
static gboolean 
608 609 610
float_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
611
{
612
  gdouble d;
613

614
  if (!_gtk_css_parser_try_double (parser, &d))
615
    {
616
      _gtk_css_parser_error (parser, "Expected a number");
617 618 619 620 621 622 623
      return FALSE;
    }

  g_value_set_float (value, d);
  return TRUE;
}

624 625 626
static void
float_value_print (const GValue *value,
                   GString      *string)
627
{
628
  string_append_double (string, g_value_get_float (value));
629 630
}

631
static gboolean 
632 633 634
string_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
635
{
636
  char *str = _gtk_css_parser_read_string (parser);
637

638
  if (str == NULL)
639 640
    return FALSE;

641
  g_value_take_string (value, str);
642 643 644
  return TRUE;
}

645 646 647
static void
string_value_print (const GValue *value,
                    GString      *str)
648
{
649
  string_append_string (str, g_value_get_string (value));
650 651
}

652
static gboolean 
653 654 655
theming_engine_value_parse (GtkCssParser *parser,
                            GFile        *base,
                            GValue       *value)
656 657
{
  GtkThemingEngine *engine;
658 659
  char *str;

660 661 662 663 664 665
  if (_gtk_css_parser_try (parser, "none", TRUE))
    {
      g_value_set_object (value, gtk_theming_engine_load (NULL));
      return TRUE;
    }

666 667 668 669 670 671
  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
    {
      _gtk_css_parser_error (parser, "Expected a valid theme name");
      return FALSE;
    }
672 673

  engine = gtk_theming_engine_load (str);
674

675 676
  if (engine == NULL)
    {
677 678
      _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
      g_free (str);
679 680 681 682
      return FALSE;
    }

  g_value_set_object (value, engine);
683
  g_free (str);
684 685 686
  return TRUE;
}

687 688 689
static void
theming_engine_value_print (const GValue *value,
                            GString      *string)
690 691 692 693 694 695
{
  GtkThemingEngine *engine;
  char *name;

  engine = g_value_get_object (value);
  if (engine == NULL)
696 697 698 699 700
    g_string_append (string, "none");
  else
    {
      /* XXX: gtk_theming_engine_get_name()? */
      g_object_get (engine, "name", &name, NULL);
701
      g_string_append (string, name ? name : "none");
702 703
      g_free (name);
    }
704 705 706
}

static gboolean 
707 708 709
animation_description_value_parse (GtkCssParser *parser,
                                   GFile        *base,
                                   GValue       *value)
710 711
{
  GtkAnimationDescription *desc;
712 713 714 715 716
  char *str;

  str = _gtk_css_parser_read_value (parser);
  if (str == NULL)
    return FALSE;
717 718

  desc = _gtk_animation_description_from_string (str);
719
  g_free (str);
720 721

  if (desc == NULL)
722 723 724 725
    {
      _gtk_css_parser_error (parser, "Invalid animation description");
      return FALSE;
    }
726 727 728 729 730
  
  g_value_take_boxed (value, desc);
  return TRUE;
}

731 732 733
static void
animation_description_value_print (const GValue *value,
                                   GString      *string)
734 735 736 737
{
  GtkAnimationDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
738 739 740
    g_string_append (string, "none");
  else
    _gtk_animation_description_print (desc, string);
741 742
}

743 744 745 746
static gboolean 
border_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
747
{
748 749
  GtkBorder border = { 0, };
  guint i, numbers[4];
750

751
  for (i = 0; i < G_N_ELEMENTS (numbers); i++)
752
    {
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
      if (_gtk_css_parser_begins_with (parser, '-'))
	{
	  /* These are strictly speaking signed, but we want to be able to use them
	     for unsigned types too, as the actual ranges of values make this safe */
	  int res = _gtk_win32_theme_int_parse (parser, base, (int *)&numbers[i]);

	  if (res == 0) /* Parse error, report */
	    return FALSE;

	  if (res < 0) /* Nothing known to expand */
	    break;
	}
      else
	{
	  if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
	    break;

	  /* XXX: shouldn't allow spaces here? */
	  _gtk_css_parser_try (parser, "px", TRUE);
	}
773 774
    }

775
  if (i == 0)
776
    {
777
      _gtk_css_parser_error (parser, "Expected valid border");
778 779 780
      return FALSE;
    }

781 782 783
  border.top = numbers[0];
  if (i > 1)
    border.right = numbers[1];
784
  else
785 786 787
    border.right = border.top;
  if (i > 2)
    border.bottom = numbers[2];
788
  else
789 790 791
    border.bottom = border.top;
  if (i > 3)
    border.left = numbers[3];
792
  else
793
    border.left = border.right;
794

795
  g_value_set_boxed (value, &border);
796 797 798
  return TRUE;
}

799 800
static void
border_value_print (const GValue *value, GString *string)
801 802 803 804
{
  const GtkBorder *border = g_value_get_boxed (value);

  if (border == NULL)
805
    g_string_append (string, "none");
806
  else if (border->left != border->right)
807
    g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
808
  else if (border->top != border->bottom)
809
    g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
810
  else if (border->top != border->left)
811
    g_string_append_printf (string, "%d %d", border->top, border->right);
812
  else
813
    g_string_append_printf (string, "%d", border->top);
814 815
}

816 817 818 819
static gboolean 
gradient_value_parse (GtkCssParser *parser,
                      GFile        *base,
                      GValue       *value)
820 821 822 823 824 825
{
  GtkGradient *gradient;
  cairo_pattern_type_t type;
  gdouble coords[6];
  guint i;

Benjamin Otte's avatar
Benjamin Otte committed
826
  if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
827
    {
Benjamin Otte's avatar
Benjamin Otte committed
828 829
      _gtk_css_parser_error (parser,
                             "Expected '-gtk-gradient'");
830
      return FALSE;
831 832
    }

Benjamin Otte's avatar
Benjamin Otte committed
833
  if (!_gtk_css_parser_try (parser, "(", TRUE))
834
    {
Benjamin Otte's avatar
Benjamin Otte committed
835 836
      _gtk_css_parser_error (parser,
                             "Expected '(' after '-gtk-gradient'");
837
      return FALSE;
838
    }
Benjamin Otte's avatar
Benjamin Otte committed
839 840 841 842 843 844

  /* Parse gradient type */
  if (_gtk_css_parser_try (parser, "linear", TRUE))
    type = CAIRO_PATTERN_TYPE_LINEAR;
  else if (_gtk_css_parser_try (parser, "radial", TRUE))
    type = CAIRO_PATTERN_TYPE_RADIAL;
845 846
  else
    {
Benjamin Otte's avatar
Benjamin Otte committed
847 848
      _gtk_css_parser_error (parser,
                             "Gradient type must be 'radial' or 'linear'");
849
      return FALSE;
850 851 852 853 854
    }

  /* Parse start/stop position parameters */
  for (i = 0; i < 2; i++)
    {
Benjamin Otte's avatar
Benjamin Otte committed
855
      if (! _gtk_css_parser_try (parser, ",", TRUE))
856
        {
Benjamin Otte's avatar
Benjamin Otte committed
857 858
          _gtk_css_parser_error (parser,
                                 "Expected ','");
859
          return FALSE;
860 861
        }

Benjamin Otte's avatar
Benjamin Otte committed
862 863 864 865 866 867 868
      if (_gtk_css_parser_try (parser, "left", TRUE))
        coords[i * 3] = 0;
      else if (_gtk_css_parser_try (parser, "right", TRUE))
        coords[i * 3] = 1;
      else if (_gtk_css_parser_try (parser, "center", TRUE))
        coords[i * 3] = 0.5;
      else if (!_gtk_css_parser_try_double (parser, &coords[i * 3]))
869
        {
Benjamin Otte's avatar
Benjamin Otte committed
870 871
          _gtk_css_parser_error (parser,
                                 "Expected a valid X coordinate");
872
          return FALSE;
873 874
        }

Benjamin Otte's avatar
Benjamin Otte committed
875 876 877 878 879 880 881
      if (_gtk_css_parser_try (parser, "top", TRUE))
        coords[i * 3 + 1] = 0;
      else if (_gtk_css_parser_try (parser, "bottom", TRUE))
        coords[i * 3 + 1] = 1;
      else if (_gtk_css_parser_try (parser, "center", TRUE))
        coords[i * 3 + 1] = 0.5;
      else if (!_gtk_css_parser_try_double (parser, &coords[i * 3 + 1]))
882
        {
Benjamin Otte's avatar
Benjamin Otte committed
883 884
          _gtk_css_parser_error (parser,
                                 "Expected a valid Y coordinate");
885
          return FALSE;
886 887 888 889 890
        }

      if (type == CAIRO_PATTERN_TYPE_RADIAL)
        {
          /* Parse radius */
Benjamin Otte's avatar
Benjamin Otte committed
891
          if (! _gtk_css_parser_try (parser, ",", TRUE))
892
            {
Benjamin Otte's avatar
Benjamin Otte committed
893 894
              _gtk_css_parser_error (parser,
                                     "Expected ','");
895
              return FALSE;
896 897
            }

Benjamin Otte's avatar
Benjamin Otte committed
898 899 900 901
          if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
            {
              _gtk_css_parser_error (parser,
                                     "Expected a numer for the radius");
902
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
903
            }
904 905 906 907 908 909 910 911 912
        }
    }

  if (type == CAIRO_PATTERN_TYPE_LINEAR)
    gradient = gtk_gradient_new_linear (coords[0], coords[1], coords[3], coords[4]);
  else
    gradient = gtk_gradient_new_radial (coords[0], coords[1], coords[2],
                                        coords[3], coords[4], coords[5]);

Benjamin Otte's avatar
Benjamin Otte committed
913
  while (_gtk_css_parser_try (parser, ",", TRUE))
914 915 916 917
    {
      GtkSymbolicColor *color;
      gdouble position;

Benjamin Otte's avatar
Benjamin Otte committed
918
      if (_gtk_css_parser_try (parser, "from", TRUE))
919 920 921
        {
          position = 0;

Benjamin Otte's avatar
Benjamin Otte committed
922
          if (!_gtk_css_parser_try (parser, "(", TRUE))
923
            {
Benjamin Otte's avatar
Benjamin Otte committed
924 925 926
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
927
              return FALSE;
928
            }
Benjamin Otte's avatar
Benjamin Otte committed
929

930
        }
Benjamin Otte's avatar
Benjamin Otte committed
931
      else if (_gtk_css_parser_try (parser, "to", TRUE))
932 933 934
        {
          position = 1;

Benjamin Otte's avatar
Benjamin Otte committed
935
          if (!_gtk_css_parser_try (parser, "(", TRUE))
936
            {
Benjamin Otte's avatar
Benjamin Otte committed
937 938 939
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
940
              return FALSE;
941
            }
Benjamin Otte's avatar
Benjamin Otte committed
942

943
        }
Benjamin Otte's avatar
Benjamin Otte committed
944
      else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
945
        {
Benjamin Otte's avatar
Benjamin Otte committed
946
          if (!_gtk_css_parser_try (parser, "(", TRUE))
947
            {
Benjamin Otte's avatar
Benjamin Otte committed
948 949 950
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
951
              return FALSE;
952 953
            }

Benjamin Otte's avatar
Benjamin Otte committed
954 955 956 957 958
          if (!_gtk_css_parser_try_double (parser, &position))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a valid number");
959
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
960
            }
961

Benjamin Otte's avatar
Benjamin Otte committed
962
          if (!_gtk_css_parser_try (parser, ",", TRUE))
963
            {
Benjamin Otte's avatar
Benjamin Otte committed
964 965 966
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a comma");
967
              return FALSE;
968 969 970 971
            }
        }
      else
        {
Benjamin Otte's avatar
Benjamin Otte committed
972 973 974
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Not a valid color-stop definition");
975
          return FALSE;
976 977
        }

Benjamin Otte's avatar
Benjamin Otte committed
978 979
      color = _gtk_css_parser_read_symbolic_color (parser);
      if (color == NULL)
980
        {
Benjamin Otte's avatar
Benjamin Otte committed
981
          gtk_gradient_unref (gradient);
982
          return FALSE;
983 984
        }

Benjamin Otte's avatar
Benjamin Otte committed
985 986
      gtk_gradient_add_color_stop (gradient, position, color);
      gtk_symbolic_color_unref (color);
987

Benjamin Otte's avatar
Benjamin Otte committed
988
      if (!_gtk_css_parser_try (parser, ")", TRUE))
989
        {
Benjamin Otte's avatar
Benjamin Otte committed
990 991 992
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Expected ')'");
993
          return FALSE;
994 995 996
        }
    }

Benjamin Otte's avatar
Benjamin Otte committed
997
  if (!_gtk_css_parser_try (parser, ")", TRUE))
998
    {
Benjamin Otte's avatar
Benjamin Otte committed
999 1000 1001
      gtk_gradient_unref (gradient);
      _gtk_css_parser_error (parser,
                             "Expected ')'");
1002
      return FALSE;
1003 1004
    }

1005
  g_value_take_boxed (value, gradient);
1006 1007 1008
  return TRUE;
}

1009 1010 1011
static void
gradient_value_print (const GValue *value,
                      GString      *string)
1012 1013 1014 1015
{
  GtkGradient *gradient = g_value_get_boxed (value);

  if (gradient == NULL)
1016 1017 1018 1019 1020 1021 1022
    g_string_append (string, "none");
  else
    {
      char *s = gtk_gradient_to_string (gradient);
      g_string_append (string, s);
      g_free (s);
    }
1023 1024
}

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
static GFile *
gtk_css_parse_url (GtkCssParser *parser,
                   GFile        *base)
{
  gchar *path;
  GFile *file;

  if (_gtk_css_parser_try (parser, "url", FALSE))
    {
      if (!_gtk_css_parser_try (parser, "(", TRUE))
        {
          _gtk_css_parser_skip_whitespace (parser);
          if (_gtk_css_parser_try (parser, "(", TRUE))
            {
              GError *error;
              
              error = g_error_new_literal (GTK_CSS_PROVIDER_ERROR,
                                           GTK_CSS_PROVIDER_ERROR_DEPRECATED,
1043
                                           "Whitespace between 'url' and '(' is deprecated");
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
                             
              _gtk_css_parser_take_error (parser, error);
            }
          else
            {
              _gtk_css_parser_error (parser, "Expected '(' after 'url'");
              return NULL;
            }
        }

      path = _gtk_css_parser_read_string (parser);
      if (path == NULL)
        return NULL;

      if (!_gtk_css_parser_try (parser, ")", TRUE))
        {
          _gtk_css_parser_error (parser, "No closing ')' found for 'url'");
          g_free (path);
          return NULL;
        }
    }
  else
    {
      path = _gtk_css_parser_try_name (parser, TRUE);
      if (path == NULL)
        {
          _gtk_css_parser_error (parser, "Not a valid url");
          return NULL;
        }
    }

  file = g_file_resolve_relative_path (base, path);
  g_free (path);

  return file;
}

1081
static gboolean 
1082 1083 1084
pattern_value_parse (GtkCssParser *parser,
                     GFile        *base,
                     GValue       *value)
1085
{
1086 1087 1088 1089 1090
  if (_gtk_css_parser_try (parser, "none", TRUE))
    {
      /* nothing to do here */
    }
  else if (_gtk_css_parser_begins_with (parser, '-'))
1091
    {
1092 1093 1094 1095 1096
      int res;
      res = _gtk_win32_theme_part_parse (parser, base, value);
      if (res >= 0)
	return res > 0;
      /* < 0 => continue */
1097 1098
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_GRADIENT);
1099
      return gradient_value_parse (parser, base, value);
1100 1101 1102
    }
  else
    {
1103
      GError *error = NULL;
1104 1105 1106
      gchar *path;
      GdkPixbuf *pixbuf;
      GFile *file;
1107 1108 1109 1110
      cairo_surface_t *surface;
      cairo_pattern_t *pattern;
      cairo_t *cr;
      cairo_matrix_t matrix;
1111

1112
      file = gtk_css_parse_url (parser, base);
1113 1114 1115 1116 1117 1118
      if (file == NULL)
        return FALSE;

      path = g_file_get_path (file);
      g_object_unref (file);

1119
      pixbuf = gdk_pixbuf_new_from_file (path, &error);
1120 1121
      g_free (path);
      if (pixbuf == NULL)
1122 1123 1124 1125
        {
          _gtk_css_parser_take_error (parser, error);
          return FALSE;
        }
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149

      surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                            gdk_pixbuf_get_width (pixbuf),
                                            gdk_pixbuf_get_height (pixbuf));
      cr = cairo_create (surface);
      gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
      cairo_paint (cr);
      pattern = cairo_pattern_create_for_surface (surface);

      cairo_matrix_init_scale (&matrix,
                               gdk_pixbuf_get_width (pixbuf),
                               gdk_pixbuf_get_height (pixbuf));
      cairo_pattern_set_matrix (pattern, &matrix);

      cairo_surface_destroy (surface);
      cairo_destroy (cr);
      g_object_unref (pixbuf);

      g_value_take_boxed (value, pattern);
    }
  
  return TRUE;
}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
static cairo_status_t
surface_write (void                *closure,
               const unsigned char *data,
               unsigned int         length)
{
  g_byte_array_append (closure, data, length);

  return CAIRO_STATUS_SUCCESS;
}

static void
surface_print (cairo_surface_t *surface,
               GString *        string)
{
#if CAIRO_HAS_PNG_FUNCTIONS
  GByteArray *array;
  char *base64;
  
  array = g_byte_array_new ();
  cairo_surface_write_to_png_stream (surface, surface_write, array);
  base64 = g_base64_encode (array->data, array->len);
  g_byte_array_free (array, TRUE);

  g_string_append (string, "url(\"data:image/png;base64,");
  g_string_append (string, base64);
  g_string_append (string, "\")");

  g_free (base64);
#else
  g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
#endif
}

static void
pattern_value_print (const GValue *value,
                     GString      *string)
{
  cairo_pattern_t *pattern;
  cairo_surface_t *surface;

  pattern = g_value_get_boxed (value);

  if (pattern == NULL)
    {
      g_string_append (string, "none");
      return;
    }

  switch (cairo_pattern_get_type (pattern))
    {
    case CAIRO_PATTERN_TYPE_SURFACE:
      if (cairo_pattern_get_surface (pattern, &surface) != CAIRO_STATUS_SUCCESS)
        {
          g_assert_not_reached ();
        }
      surface_print (surface, string);
      break;
    case CAIRO_PATTERN_TYPE_SOLID:
    case CAIRO_PATTERN_TYPE_LINEAR:
    case CAIRO_PATTERN_TYPE_RADIAL:
    default:
      g_assert_not_reached ();
      break;
    }
}

1216 1217 1218 1219 1220
static gboolean
shadow_value_parse (GtkCssParser *parser,
                    GFile *base,
                    GValue *value)
{
1221
  gboolean have_inset, have_color, have_lengths;
1222 1223 1224
  gdouble hoffset, voffset, blur, spread;
  GtkSymbolicColor *color;
  GtkShadow *shadow;
1225
  guint i;
1226 1227 1228

  shadow = _gtk_shadow_new ();

1229 1230 1231
  if (_gtk_css_parser_try (parser, "none", TRUE))
    return TRUE;

1232 1233
  do
    {
1234
      have_inset = have_lengths = have_color = FALSE;
1235

1236
      for (i = 0; i < 3; i++)
1237
        {
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
          if (!have_inset && 
              _gtk_css_parser_try (parser, "inset", TRUE))
            {
              have_inset = TRUE;
              continue;
            }
            
          if (!have_lengths &&
              _gtk_css_parser_try_double (parser, &hoffset))
            {
              have_lengths = TRUE;
1249

1250 1251 1252 1253 1254 1255
              if (!_gtk_css_parser_try_double (parser, &voffset))
                {
                  _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
                  _gtk_shadow_unref (shadow);
                  return FALSE;
                }
1256

1257 1258
              if (!_gtk_css_parser_try_double (parser, &blur))
                blur = 0;
1259

1260 1261
              if (!_gtk_css_parser_try_double (parser, &spread))
                spread = 0;
1262

1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
              continue;
            }

          if (!have_color)
            {
              have_color = TRUE;

              /* XXX: the color is optional and UA-defined if it's missing,
               * but it doesn't really make sense for us...
               */
              color = _gtk_css_parser_read_symbolic_color (parser);

              if (color == NULL)
                {
                  _gtk_shadow_unref (shadow);
                  return FALSE;
                }
            }
        }

      if (!have_color || !have_lengths)
1284
        {
1285
          _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1286 1287 1288 1289 1290 1291 1292
          _gtk_shadow_unref (shadow);
          return FALSE;
        }

      _gtk_shadow_append (shadow,
                          hoffset, voffset,
                          blur, spread,
1293
                          have_inset, color);
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303

      gtk_symbolic_color_unref (color);

    }
  while (_gtk_css_parser_try (parser, ",", TRUE));

  g_value_take_boxed (value, shadow);
  return TRUE;
}

1304 1305 1306
static void
shadow_value_print (const GValue *value,
                    GString      *string)
1307 1308 1309 1310 1311 1312
{
  GtkShadow *shadow;

  shadow = g_value_get_boxed (value);

  if (shadow == NULL)
1313 1314 1315
    g_string_append (string, "none");
  else
    _gtk_shadow_print (shadow, string);
1316 1317
}

1318 1319 1320 1321 1322 1323
static gboolean
background_repeat_value_parse (GtkCssParser *parser,
                               GFile *file,
                               GValue *value)
{
  GtkCssBackgroundRepeat repeat;
1324
  int style;
1325

1326 1327
  if (!enum_parse (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, &style))
    return FALSE;
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343

  repeat.repeat = style;

  g_value_set_boxed (value, &repeat);

  return TRUE;
}

static void
background_repeat_value_print (const GValue *value,
                               GString      *string)
{
  GtkCssBackgroundRepeat *repeat;

  repeat = g_value_get_boxed (value);

1344
  enum_print (repeat->repeat, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, string);
1345 1346
}

1347 1348 1349 1350
static gboolean
border_image_repeat_value_parse (GtkCssParser *parser,
                                 GFile *file,
                                 GValue *value)
1351
{
1352
  GtkCssBorderImageRepeat image_repeat;
1353
  GtkCssBorderRepeatStyle styles[2];
1354
  gint i, v;
1355

1356
  for (i = 0; i < 2; i++)
1357
    {
1358 1359
      if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
        styles[i] = v;
1360 1361
      else if (i == 0)
        {
1362
          styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1363 1364 1365
          break;
        }
      else
1366
        styles[i] = styles[0];
1367 1368
    }

1369 1370
  image_repeat.hrepeat = styles[0];
  image_repeat.vrepeat = styles[1];
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380

  g_value_set_boxed (value, &image_repeat);

  return TRUE;
}

static void
border_image_repeat_value_print (const GValue *value,
                                 GString      *string)
{
1381
  GtkCssBorderImageRepeat *image_repeat;
1382

1383 1384
  image_repeat = g_value_get_boxed (value);

1385
  enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1386 1387 1388
  if (image_repeat->hrepeat != image_repeat->vrepeat)
    {
      g_string_append (string, " ");
1389
      enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1390
    }
1391 1392
}

1393
static gboolean 
1394 1395 1396
enum_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
1397
{
1398 1399
  int v;

1400
  if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1401 1402 1403 1404
    {
      g_value_set_enum (value, v);
      return TRUE;
    }
1405

1406
  return FALSE;
1407 1408
}

1409 1410 1411
static void
enum_value_print (const GValue *value,
                  GString      *string)
1412
{
1413
  enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1414 1415 1416
}

static gboolean 
1417 1418 1419
flags_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
1420 1421 1422 1423
{
  GFlagsClass *flags_class;
  GFlagsValue *flag_value;
  guint flags = 0;
1424
  char *str;
1425 1426 1427

  flags_class = g_type_class_ref (G_VALUE_TYPE (value));

1428 1429 1430 1431 1432 1433 1434 1435
  do {
    str = _gtk_css_parser_try_ident (parser, TRUE);
    if (str == NULL)
      {
        _gtk_css_parser_error (parser, "Expected an identifier");
        g_type_class_unref (flags_class);
        return FALSE;
      }
1436

1437
      flag_value = g_flags_get_value_by_nick (flags_class, str);
1438 1439
      if (!flag_value)
        {
1440 1441 1442 1443 1444 1445 1446
          _gtk_css_parser_error (parser,
                                 "Unknown flag value '%s' for type '%s'",
                                 str, g_type_name (G_VALUE_TYPE (value)));
          /* XXX Do we want to return FALSE here? We can get
           * forward-compatibility for new values this way
           */
          g_free (str);
1447 1448 1449
          g_type_class_unref (flags_class);
          return FALSE;
        }
1450 1451

      g_free (str);
1452
    }
1453
  while (_gtk_css_parser_try (parser, ",", FALSE));
1454 1455 1456 1457 1458 1459 1460 1461

  g_type_class_unref (flags_class);

  g_value_set_enum (value, flags);

  return TRUE;
}

1462 1463 1464
static void
flags_value_print (const GValue *value,
                   GString      *string)
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
{
  GFlagsClass *flags_class;
  guint i, flags;

  flags_class = g_type_class_ref (G_VALUE_TYPE (value));
  flags = g_value_get_flags (value);

  for (i = 0; i < flags_class->n_values; i++)
    {
      GFlagsValue *flags_value = &flags_class->values[i];

      if (flags & flags_value->value)
        {
          if (string->len != 0)
            g_string_append (string, ", ");

          g_string_append (string, flags_value->value_nick);
        }
    }

  g_type_class_unref (flags_class);
}

1488
static gboolean 
1489 1490 1491
bindings_value_parse (GtkCssParser *parser,
                      GFile        *base,
                      GValue       *value)
1492 1493
{
  GPtrArray *array;
1494 1495
  GtkBindingSet *binding_set;
  char *name;
1496 1497 1498

  array = g_ptr_array_new ();

1499 1500 1501 1502 1503 1504 1505 1506
  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;
        }
1507

1508
      binding_set = gtk_binding_set_find (name);
1509 1510

      if (!binding_set)
1511 1512 1513 1514 1515
        {
          _gtk_css_parser_error (parser, "No binding set named '%s'", name);
          g_free (name);
          continue;
        }
1516 1517

      g_ptr_array_add (array, binding_set);
1518
      g_free (name);
1519
    }
1520
  while (_gtk_css_parser_try (parser, ",", TRUE));
1521 1522 1523 1524 1525 1526

  g_value_take_boxed (value, array);

  return TRUE;
}

1527 1528 1529
static void
bindings_value_print (const GValue *value,
                      GString      *string)
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540
{
  GPtrArray *array;
  guint i;

  array = g_value_get_boxed (value);

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

      if (i > 0)
1541 1542
        g_string_append (string, ", ");
      g_string_append (string, binding_set->set_name);
1543 1544 1545
    }
}

1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595
static gboolean 
border_corner_radius_value_parse (GtkCssParser *parser,
                                  GFile        *base,
                                  GValue       *value)
{
  GtkCssBorderCornerRadius corner;

  if (!_gtk_css_parser_try_double (parser, &corner.horizontal))
    {
      _gtk_css_parser_error (parser, "Expected a number");
      return FALSE;
    }
  else if (corner.horizontal < 0)
    goto negative;

  if (!_gtk_css_parser_try_double (parser, &corner.vertical))
    corner.vertical = corner.horizontal;
  else if (corner.vertical < 0)
    goto negative;

  g_value_set_boxed (value, &corner);
  return TRUE;

negative:
  _gtk_css_parser_error (parser, "Border radius values cannot be negative");
  return FALSE;
}

static void
border_corner_radius_value_print (const GValue *value,
                                  GString      *string)
{
  GtkCssBorderCornerRadius *corner;

  corner = g_value_get_boxed (value);

  if (corner == NULL)
    {
      g_string_append (string, "none");
      return;
    }

  string_append_double (string, corner->horizontal);
  if (corner->horizontal != corner->vertical)
    {
      g_string_append_c (string, ' ');
      string_append_double (string, corner->vertical);
    }
}

1596
static gboolean 
1597 1598 1599
transparent_color_value_parse (GtkCssParser *parser,
                               GFile        *base,
                               GValue       *value)
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
{
  if (_gtk_css_parser_try (parser, "transparent", TRUE))
    {
      GdkRGBA transparent = { 0, 0, 0, 0 };
          
      g_value_set_boxed (value, &transparent);

      return TRUE;
    }

  return rgba_value_parse (parser, base, value);
}

1613 1614 1615 1616 1617
/*** API ***/

static void
css_string_funcs_init (void)
{
1618
  if (G_LIKELY (parse_funcs != NULL))
1619 1620
    return;

1621
  parse_funcs = g_hash_table_new (NULL, NULL);
1622
  print_funcs = g_hash_table_new (NULL, NULL);
1623 1624

  register_conversion_function (GDK_TYPE_RGBA,
1625
                                rgba_value_parse,
1626
                                rgba_value_print);
1627
  register_conversion_function (GDK_TYPE_COLOR,
1628
                                color_value_parse,
1629
                                color_value_print);
1630
  register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
1631
                                symbolic_color_value_parse,
1632
                                symbolic_color_value_print);
Benjamin Otte's avatar