gtkstyleproperty.c 109 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 "gtkborderimageprivate.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 58 59 60
enum {
  PROP_0,
  PROP_NAME
};

61
static GHashTable *parse_funcs = NULL;
62
static GHashTable *print_funcs = NULL;
63
static GPtrArray *__style_property_array = NULL;
64

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

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

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

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

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

119 120 121 122 123 124
static void
_gtk_style_property_class_init (GtkStylePropertyClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_style_property_finalize;
125 126 127 128 129 130 131 132 133 134 135 136
  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));

  klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
137 138 139 140 141 142 143
}

static void
_gtk_style_property_init (GtkStyleProperty *property)
{
}

144
static void
145 146 147
register_conversion_function (GType             type,
                              GtkStyleParseFunc parse,
                              GtkStylePrintFunc print)
148
{
149 150
  if (parse)
    g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
151 152
  if (print)
    g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
153 154
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
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, '"');
}

202 203 204
/*** IMPLEMENTATIONS ***/

static gboolean 
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
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
246 247 248
rgba_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
249 250 251 252
{
  GtkSymbolicColor *symbolic;
  GdkRGBA rgba;

253
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
254 255 256
  if (symbolic == NULL)
    return FALSE;

257 258 259
  if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
    {
      g_value_set_boxed (value, &rgba);
260
      gtk_symbolic_color_unref (symbolic);
261 262 263 264 265 266 267 268
    }
  else
    {
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
      g_value_take_boxed (value, symbolic);
    }

269 270 271
  return TRUE;
}

272 273 274
static void
rgba_value_print (const GValue *value,
                  GString      *string)
275 276 277 278
{
  const GdkRGBA *rgba = g_value_get_boxed (value);

  if (rgba == NULL)
279 280 281 282 283 284 285
    g_string_append (string, "none");
  else
    {
      char *s = gdk_rgba_to_string (rgba);
      g_string_append (string, s);
      g_free (s);
    }
286 287 288
}

static gboolean 
289 290 291
color_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
292 293
{
  GtkSymbolicColor *symbolic;
294
  GdkRGBA rgba;
295

296
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
297 298 299
  if (symbolic == NULL)
    return FALSE;

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  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);
    }

317 318 319
  return TRUE;
}

320 321 322
static void
color_value_print (const GValue *value,
                   GString      *string)
323 324 325 326
{
  const GdkColor *color = g_value_get_boxed (value);

  if (color == NULL)
327 328 329 330 331 332 333
    g_string_append (string, "none");
  else
    {
      char *s = gdk_color_to_string (color);
      g_string_append (string, s);
      g_free (s);
    }
334 335
}

336 337 338 339
static gboolean
symbolic_color_value_parse (GtkCssParser *parser,
                            GFile        *base,
                            GValue       *value)
340 341 342
{
  GtkSymbolicColor *symbolic;

343
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
344 345 346 347 348 349 350
  if (symbolic == NULL)
    return FALSE;

  g_value_take_boxed (value, symbolic);
  return TRUE;
}

351 352 353
static void
symbolic_color_value_print (const GValue *value,
                            GString      *string)
354 355 356 357
{
  GtkSymbolicColor *symbolic = g_value_get_boxed (value);

  if (symbolic == NULL)
358 359 360 361 362 363 364
    g_string_append (string, "none");
  else
    {
      char *s = gtk_symbolic_color_to_string (symbolic);
      g_string_append (string, s);
      g_free (s);
    }
365 366
}

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 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
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++;
    }
}

435
static gboolean 
436 437 438
font_description_value_parse (GtkCssParser *parser,
                              GFile        *base,
                              GValue       *value)
439 440
{
  PangoFontDescription *font_desc;
441
  guint mask;
442 443 444 445 446
  char *str;

  str = _gtk_css_parser_read_value (parser);
  if (str == NULL)
    return FALSE;
447 448

  font_desc = pango_font_description_from_string (str);
449 450 451 452 453 454 455
  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);
456
  g_free (str);
457 458 459 460
  g_value_take_boxed (value, font_desc);
  return TRUE;
}

461 462 463
static void
font_description_value_print (const GValue *value,
                              GString      *string)
464 465 466 467
{
  const PangoFontDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
468 469 470 471 472 473 474
    g_string_append (string, "none");
  else
    {
      char *s = pango_font_description_to_string (desc);
      g_string_append (string, s);
      g_free (s);
    }
475 476 477
}

static gboolean 
478 479 480
boolean_value_parse (GtkCssParser *parser,
                     GFile        *base,
                     GValue       *value)
481
{
482 483
  if (_gtk_css_parser_try (parser, "true", TRUE) ||
      _gtk_css_parser_try (parser, "1", TRUE))
484 485 486 487
    {
      g_value_set_boolean (value, TRUE);
      return TRUE;
    }
488 489
  else if (_gtk_css_parser_try (parser, "false", TRUE) ||
           _gtk_css_parser_try (parser, "0", TRUE))
490 491 492 493
    {
      g_value_set_boolean (value, FALSE);
      return TRUE;
    }
494 495 496 497 498
  else
    {
      _gtk_css_parser_error (parser, "Expected a boolean value");
      return FALSE;
    }
499 500
}

501 502 503
static void
boolean_value_print (const GValue *value,
                     GString      *string)
504 505
{
  if (g_value_get_boolean (value))
506
    g_string_append (string, "true");
507
  else
508
    g_string_append (string, "false");
509 510 511
}

static gboolean 
512 513 514
int_value_parse (GtkCssParser *parser,
                 GFile        *base,
                 GValue       *value)
515
{
516
  gint i;
517

518 519 520 521 522 523 524 525 526 527 528
  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 */
    }

529
  if (!_gtk_css_parser_try_int (parser, &i))
530
    {
531
      _gtk_css_parser_error (parser, "Expected a valid integer value");
532 533 534 535 536 537 538
      return FALSE;
    }

  g_value_set_int (value, i);
  return TRUE;
}

539 540 541
static void
int_value_print (const GValue *value,
                 GString      *string)
542
{
543
  g_string_append_printf (string, "%d", g_value_get_int (value));
544 545 546
}

static gboolean 
547 548 549
uint_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
550
{
551
  guint u;
Benjamin Otte's avatar
Benjamin Otte committed
552

553
  if (!_gtk_css_parser_try_uint (parser, &u))
554
    {
555
      _gtk_css_parser_error (parser, "Expected a valid unsigned value");
556 557 558 559 560 561 562
      return FALSE;
    }

  g_value_set_uint (value, u);
  return TRUE;
}

563 564 565
static void
uint_value_print (const GValue *value,
                  GString      *string)
566
{
567
  g_string_append_printf (string, "%u", g_value_get_uint (value));
568 569 570
}

static gboolean 
571 572 573
double_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
574
{
575
  gdouble d;
576

577
  if (!_gtk_css_parser_try_double (parser, &d))
578
    {
579
      _gtk_css_parser_error (parser, "Expected a number");
580 581 582 583 584 585 586
      return FALSE;
    }

  g_value_set_double (value, d);
  return TRUE;
}

587 588 589 590 591 592 593
static void
double_value_print (const GValue *value,
                    GString      *string)
{
  string_append_double (string, g_value_get_double (value));
}

594
static gboolean 
595 596 597
float_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
598
{
599
  gdouble d;
600

601
  if (!_gtk_css_parser_try_double (parser, &d))
602
    {
603
      _gtk_css_parser_error (parser, "Expected a number");
604 605 606 607 608 609 610
      return FALSE;
    }

  g_value_set_float (value, d);
  return TRUE;
}

611 612 613
static void
float_value_print (const GValue *value,
                   GString      *string)
614
{
615
  string_append_double (string, g_value_get_float (value));
616 617
}

618
static gboolean 
619 620 621
string_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
622
{
623
  char *str = _gtk_css_parser_read_string (parser);
624

625
  if (str == NULL)
626 627
    return FALSE;

628
  g_value_take_string (value, str);
629 630 631
  return TRUE;
}

632 633 634
static void
string_value_print (const GValue *value,
                    GString      *str)
635
{
636
  string_append_string (str, g_value_get_string (value));
637 638
}

639
static gboolean 
640 641 642
theming_engine_value_parse (GtkCssParser *parser,
                            GFile        *base,
                            GValue       *value)
643 644
{
  GtkThemingEngine *engine;
645 646
  char *str;

647 648 649 650 651 652
  if (_gtk_css_parser_try (parser, "none", TRUE))
    {
      g_value_set_object (value, gtk_theming_engine_load (NULL));
      return TRUE;
    }

653 654 655 656 657 658
  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
    {
      _gtk_css_parser_error (parser, "Expected a valid theme name");
      return FALSE;
    }
659 660

  engine = gtk_theming_engine_load (str);
661

662 663
  if (engine == NULL)
    {
664 665
      _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
      g_free (str);
666 667 668 669
      return FALSE;
    }

  g_value_set_object (value, engine);
670
  g_free (str);
671 672 673
  return TRUE;
}

674 675 676
static void
theming_engine_value_print (const GValue *value,
                            GString      *string)
677 678 679 680 681 682
{
  GtkThemingEngine *engine;
  char *name;

  engine = g_value_get_object (value);
  if (engine == NULL)
683 684 685 686 687
    g_string_append (string, "none");
  else
    {
      /* XXX: gtk_theming_engine_get_name()? */
      g_object_get (engine, "name", &name, NULL);
688
      g_string_append (string, name ? name : "none");
689 690
      g_free (name);
    }
691 692 693
}

static gboolean 
694 695 696
animation_description_value_parse (GtkCssParser *parser,
                                   GFile        *base,
                                   GValue       *value)
697 698
{
  GtkAnimationDescription *desc;
699 700 701 702 703
  char *str;

  str = _gtk_css_parser_read_value (parser);
  if (str == NULL)
    return FALSE;
704 705

  desc = _gtk_animation_description_from_string (str);
706
  g_free (str);
707 708

  if (desc == NULL)
709 710 711 712
    {
      _gtk_css_parser_error (parser, "Invalid animation description");
      return FALSE;
    }
713 714 715 716 717
  
  g_value_take_boxed (value, desc);
  return TRUE;
}

718 719 720
static void
animation_description_value_print (const GValue *value,
                                   GString      *string)
721 722 723 724
{
  GtkAnimationDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
725 726 727
    g_string_append (string, "none");
  else
    _gtk_animation_description_print (desc, string);
728 729
}

730 731 732 733
static gboolean 
border_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
734
{
735 736
  GtkBorder border = { 0, };
  guint i, numbers[4];
737

738
  for (i = 0; i < G_N_ELEMENTS (numbers); i++)
739
    {
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
      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);
	}
760 761
    }

762
  if (i == 0)
763
    {
764
      _gtk_css_parser_error (parser, "Expected valid border");
765 766 767
      return FALSE;
    }

768 769 770
  border.top = numbers[0];
  if (i > 1)
    border.right = numbers[1];
771
  else
772 773 774
    border.right = border.top;
  if (i > 2)
    border.bottom = numbers[2];
775
  else
776 777 778
    border.bottom = border.top;
  if (i > 3)
    border.left = numbers[3];
779
  else
780
    border.left = border.right;
781

782
  g_value_set_boxed (value, &border);
783 784 785
  return TRUE;
}

786 787
static void
border_value_print (const GValue *value, GString *string)
788 789 790 791
{
  const GtkBorder *border = g_value_get_boxed (value);

  if (border == NULL)
792
    g_string_append (string, "none");
793
  else if (border->left != border->right)
794
    g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
795
  else if (border->top != border->bottom)
796
    g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
797
  else if (border->top != border->left)
798
    g_string_append_printf (string, "%d %d", border->top, border->right);
799
  else
800
    g_string_append_printf (string, "%d", border->top);
801 802
}

803 804 805 806
static gboolean 
gradient_value_parse (GtkCssParser *parser,
                      GFile        *base,
                      GValue       *value)
807 808 809 810 811 812
{
  GtkGradient *gradient;
  cairo_pattern_type_t type;
  gdouble coords[6];
  guint i;

Benjamin Otte's avatar
Benjamin Otte committed
813
  if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
814
    {
Benjamin Otte's avatar
Benjamin Otte committed
815 816
      _gtk_css_parser_error (parser,
                             "Expected '-gtk-gradient'");
817
      return FALSE;
818 819
    }

Benjamin Otte's avatar
Benjamin Otte committed
820
  if (!_gtk_css_parser_try (parser, "(", TRUE))
821
    {
Benjamin Otte's avatar
Benjamin Otte committed
822 823
      _gtk_css_parser_error (parser,
                             "Expected '(' after '-gtk-gradient'");
824
      return FALSE;
825
    }
Benjamin Otte's avatar
Benjamin Otte committed
826 827 828 829 830 831

  /* 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;
832 833
  else
    {
Benjamin Otte's avatar
Benjamin Otte committed
834 835
      _gtk_css_parser_error (parser,
                             "Gradient type must be 'radial' or 'linear'");
836
      return FALSE;
837 838 839 840 841
    }

  /* Parse start/stop position parameters */
  for (i = 0; i < 2; i++)
    {
Benjamin Otte's avatar
Benjamin Otte committed
842
      if (! _gtk_css_parser_try (parser, ",", TRUE))
843
        {
Benjamin Otte's avatar
Benjamin Otte committed
844 845
          _gtk_css_parser_error (parser,
                                 "Expected ','");
846
          return FALSE;
847 848
        }

Benjamin Otte's avatar
Benjamin Otte committed
849 850 851 852 853 854 855
      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]))
856
        {
Benjamin Otte's avatar
Benjamin Otte committed
857 858
          _gtk_css_parser_error (parser,
                                 "Expected a valid X coordinate");
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, "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]))
869
        {
Benjamin Otte's avatar
Benjamin Otte committed
870 871
          _gtk_css_parser_error (parser,
                                 "Expected a valid Y coordinate");
872
          return FALSE;
873 874 875 876 877
        }

      if (type == CAIRO_PATTERN_TYPE_RADIAL)
        {
          /* Parse radius */
Benjamin Otte's avatar
Benjamin Otte committed
878
          if (! _gtk_css_parser_try (parser, ",", TRUE))
879
            {
Benjamin Otte's avatar
Benjamin Otte committed
880 881
              _gtk_css_parser_error (parser,
                                     "Expected ','");
882
              return FALSE;
883 884
            }

Benjamin Otte's avatar
Benjamin Otte committed
885 886 887 888
          if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
            {
              _gtk_css_parser_error (parser,
                                     "Expected a numer for the radius");
889
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
890
            }
891 892 893 894 895 896 897 898 899
        }
    }

  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
900
  while (_gtk_css_parser_try (parser, ",", TRUE))
901 902 903 904
    {
      GtkSymbolicColor *color;
      gdouble position;

Benjamin Otte's avatar
Benjamin Otte committed
905
      if (_gtk_css_parser_try (parser, "from", TRUE))
906 907 908
        {
          position = 0;

Benjamin Otte's avatar
Benjamin Otte committed
909
          if (!_gtk_css_parser_try (parser, "(", TRUE))
910
            {
Benjamin Otte's avatar
Benjamin Otte committed
911 912 913
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
914
              return FALSE;
915
            }
Benjamin Otte's avatar
Benjamin Otte committed
916

917
        }
Benjamin Otte's avatar
Benjamin Otte committed
918
      else if (_gtk_css_parser_try (parser, "to", TRUE))
919 920 921
        {
          position = 1;

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, "color-stop", TRUE))
932
        {
Benjamin Otte's avatar
Benjamin Otte committed
933
          if (!_gtk_css_parser_try (parser, "(", TRUE))
934
            {
Benjamin Otte's avatar
Benjamin Otte committed
935 936 937
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
938
              return FALSE;
939 940
            }

Benjamin Otte's avatar
Benjamin Otte committed
941 942 943 944 945
          if (!_gtk_css_parser_try_double (parser, &position))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a valid number");
946
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
947
            }
948

Benjamin Otte's avatar
Benjamin Otte committed
949
          if (!_gtk_css_parser_try (parser, ",", TRUE))
950
            {
Benjamin Otte's avatar
Benjamin Otte committed
951 952 953
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a comma");
954
              return FALSE;
955 956 957 958
            }
        }
      else
        {
Benjamin Otte's avatar
Benjamin Otte committed
959 960 961
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Not a valid color-stop definition");
962
          return FALSE;
963 964
        }

Benjamin Otte's avatar
Benjamin Otte committed
965 966
      color = _gtk_css_parser_read_symbolic_color (parser);
      if (color == NULL)
967
        {
Benjamin Otte's avatar
Benjamin Otte committed
968
          gtk_gradient_unref (gradient);
969
          return FALSE;
970 971
        }

Benjamin Otte's avatar
Benjamin Otte committed
972 973
      gtk_gradient_add_color_stop (gradient, position, color);
      gtk_symbolic_color_unref (color);
974

Benjamin Otte's avatar
Benjamin Otte committed
975
      if (!_gtk_css_parser_try (parser, ")", TRUE))
976
        {
Benjamin Otte's avatar
Benjamin Otte committed
977 978 979
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Expected ')'");
980
          return FALSE;
981 982 983
        }
    }

Benjamin Otte's avatar
Benjamin Otte committed
984
  if (!_gtk_css_parser_try (parser, ")", TRUE))
985
    {
Benjamin Otte's avatar
Benjamin Otte committed
986 987 988
      gtk_gradient_unref (gradient);
      _gtk_css_parser_error (parser,
                             "Expected ')'");
989
      return FALSE;
990 991
    }

992
  g_value_take_boxed (value, gradient);
993 994 995
  return TRUE;
}

996 997 998
static void
gradient_value_print (const GValue *value,
                      GString      *string)
999 1000 1001 1002
{
  GtkGradient *gradient = g_value_get_boxed (value);

  if (gradient == NULL)
1003 1004 1005 1006 1007 1008 1009
    g_string_append (string, "none");
  else
    {
      char *s = gtk_gradient_to_string (gradient);
      g_string_append (string, s);
      g_free (s);
    }
1010 1011
}

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
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,
1030
                                           "Whitespace between 'url' and '(' is deprecated");
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
                             
              _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;
}

1068
static gboolean 
1069 1070 1071
pattern_value_parse (GtkCssParser *parser,
                     GFile        *base,
                     GValue       *value)
1072
{
1073 1074 1075 1076 1077
  if (_gtk_css_parser_try (parser, "none", TRUE))
    {
      /* nothing to do here */
    }
  else if (_gtk_css_parser_begins_with (parser, '-'))
1078
    {
1079 1080 1081 1082 1083
      int res;
      res = _gtk_win32_theme_part_parse (parser, base, value);
      if (res >= 0)
	return res > 0;
      /* < 0 => continue */
1084 1085
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_GRADIENT);
1086
      return gradient_value_parse (parser, base, value);
1087 1088 1089
    }
  else
    {
1090
      GError *error = NULL;
1091 1092 1093
      gchar *path;
      GdkPixbuf *pixbuf;
      GFile *file;
1094 1095 1096 1097
      cairo_surface_t *surface;
      cairo_pattern_t *pattern;
      cairo_t *cr;
      cairo_matrix_t matrix;
1098

1099
      file = gtk_css_parse_url (parser, base);
1100 1101 1102 1103 1104 1105
      if (file == NULL)
        return FALSE;

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

1106
      pixbuf = gdk_pixbuf_new_from_file (path, &error);
1107 1108
      g_free (path);
      if (pixbuf == NULL)
1109 1110 1111 1112
        {
          _gtk_css_parser_take_error (parser, error);
          return FALSE;
        }
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

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

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 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
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;
    }
}

1203 1204 1205 1206 1207
static gboolean
shadow_value_parse (GtkCssParser *parser,
                    GFile *base,
                    GValue *value)
{
1208
  gboolean have_inset, have_color, have_lengths;
1209 1210 1211
  gdouble hoffset, voffset, blur, spread;
  GtkSymbolicColor *color;
  GtkShadow *shadow;
1212
  guint i;
1213 1214 1215

  shadow = _gtk_shadow_new ();

1216 1217 1218
  if (_gtk_css_parser_try (parser, "none", TRUE))
    return TRUE;

1219 1220
  do
    {
1221
      have_inset = have_lengths = have_color = FALSE;
1222

1223
      for (i = 0; i < 3; i++)
1224
        {
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
          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;
1236

1237 1238 1239 1240 1241 1242
              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;
                }
1243

1244 1245
              if (!_gtk_css_parser_try_double (parser, &blur))
                blur = 0;
1246

1247 1248
              if (!_gtk_css_parser_try_double (parser, &spread))
                spread = 0;
1249

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
              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)
1271
        {
1272
          _gtk_css_parser_error (parser, "Must specify at least color and offsets");
1273 1274 1275 1276 1277 1278 1279
          _gtk_shadow_unref (shadow);
          return FALSE;
        }

      _gtk_shadow_append (shadow,
                          hoffset, voffset,
                          blur, spread,
1280
                          have_inset, color);
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290

      gtk_symbolic_color_unref (color);

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

  g_value_take_boxed (value, shadow);
  return TRUE;
}

1291 1292 1293
static void
shadow_value_print (const GValue *value,
                    GString      *string)
1294 1295 1296 1297 1298 1299
{
  GtkShadow *shadow;

  shadow = g_value_get_boxed (value);

  if (shadow == NULL)
1300 1301 1302
    g_string_append (string, "none");
  else
    _gtk_shadow_print (shadow, string);
1303 1304
}

1305 1306 1307 1308 1309 1310
static gboolean
background_repeat_value_parse (GtkCssParser *parser,
                               GFile *file,
                               GValue *value)
{
  GtkCssBackgroundRepeat repeat;
1311
  int style;
1312

1313 1314
  if (!enum_parse (parser, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, &style))
    return FALSE;
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330

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

1331
  enum_print (repeat->repeat, GTK_TYPE_CSS_BACKGROUND_REPEAT_STYLE, string);
1332 1333
}

1334 1335 1336 1337
static gboolean
border_image_repeat_value_parse (GtkCssParser *parser,
                                 GFile *file,
                                 GValue *value)
1338
{
1339
  GtkCssBorderImageRepeat image_repeat;
1340
  GtkCssBorderRepeatStyle styles[2];
1341
  gint i, v;
1342

1343
  for (i = 0; i < 2; i++)
1344
    {
1345 1346
      if (_gtk_css_parser_try_enum (parser, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, &v))
        styles[i] = v;
1347 1348
      else if (i == 0)
        {
1349
          styles[1] = styles[0] = GTK_CSS_REPEAT_STYLE_STRETCH;
1350 1351 1352
          break;
        }
      else
1353
        styles[i] = styles[0];
1354 1355
    }

1356 1357
  image_repeat.hrepeat = styles[0];
  image_repeat.vrepeat = styles[1];
1358 1359 1360 1361 1362 1363 1364 1365 1366 1367

  g_value_set_boxed (value, &image_repeat);

  return TRUE;
}

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

1370 1371
  image_repeat = g_value_get_boxed (value);

1372
  enum_print (image_repeat->hrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1373 1374 1375
  if (image_repeat->hrepeat != image_repeat->vrepeat)
    {
      g_string_append (string, " ");
1376
      enum_print (image_repeat->vrepeat, GTK_TYPE_CSS_BORDER_REPEAT_STYLE, string);
1377
    }
1378 1379 1380 1381 1382 1383 1384
}

static gboolean
border_image_value_parse (GtkCssParser *parser,
                          GFile *base,
                          GValue *value)
{
Javier Jardón's avatar
Javier Jardón committed
1385
  GValue temp = G_VALUE_INIT;
1386
  cairo_pattern_t *pattern = NULL;
1387 1388
  gconstpointer *boxed = NULL;
  GType boxed_type;
1389
  GtkBorder slice, *width = NULL, *parsed_slice;
1390
  GtkCssBorderImageRepeat repeat, *parsed_repeat;
1391 1392
  gboolean retval = FALSE;
  GtkBorderImage *image = NULL;
1393 1394 1395

  if (_gtk_css_parser_try (parser, "none", TRUE))
    return TRUE;
1396 1397 1398 1399 1400 1401

  g_value_init (&temp, CAIRO_GOBJECT_TYPE_PATTERN);

  if (!pattern_value_parse (parser, base, &temp))
    return FALSE;

1402 1403 1404
  boxed_type = G_VALUE_TYPE (&temp);
  if (boxed_type != CAIRO_GOBJECT_TYPE_PATTERN)
    boxed = g_value_dup_boxed (&temp);
1405 1406 1407 1408 1409 1410 1411 1412 1413
  else
    pattern = g_value_dup_boxed (&temp);

  g_value_unset (&temp);
  g_value_init (&temp, GTK_TYPE_BORDER);

  if (!border_value_parse (parser, base, &temp))
    goto out;

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
  parsed_slice = g_value_get_boxed (&temp);
  slice = *parsed_slice;

  if (_gtk_css_parser_try (parser, "/", TRUE))
    {
      g_value_unset (&temp);
      g_value_init (&temp, GTK_TYPE_BORDER);

      if (!border_value_parse (parser, base, &temp))
        goto out;

      width = g_value_dup_boxed (&temp);
    }
1427 1428

  g_value_unset (&temp);
1429
  g_value_init (&temp, GTK_TYPE_CSS_BORDER_IMAGE_REPEAT);
1430 1431 1432 1433 1434 1435 1436 1437 1438

  if (!border_image_repeat_value_parse (parser, base, &temp))
    goto out;

  parsed_repeat = g_value_get_boxed (&temp);
  repeat = *parsed_repeat;

  g_value_unset (&temp);

1439 1440
  if (boxed != NULL)
    image = _gtk_border_image_new_for_boxed (boxed_type, boxed, &slice, width, &repeat);
1441
  else if (pattern != NULL)
1442
    image = _gtk_border_image_new (pattern, &slice, width, &repeat);
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453

  if (image != NULL)
    {
      retval = TRUE;
      g_value_take_boxed (value, image);
    }

 out:
  if (pattern != NULL)
    cairo_pattern_destroy (pattern);

1454 1455
  if (boxed != NULL)
    g_boxed_free (boxed_type, boxed);
1456

1457 1458 1459
  if (width != NULL)
    gtk_border_free (width);

1460
  return retval;
1461 1462 1463
}

static gboolean 
1464 1465 1466
enum_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
1467
{
1468 1469
  int v;

1470
  if (enum_parse (parser, G_VALUE_TYPE (value), &v))
1471 1472 1473 1474
    {
      g_value_set_enum (value, v);
      return TRUE;
    }
1475

1476
  return FALSE;
1477 1478
}

1479 1480 1481
static void
enum_value_print (const GValue *value,
                  GString      *string)
1482
{
1483
  enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
1484 1485 1486
}

static gboolean 
1487 1488 1489
flags_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
1490 1491 1492 1493
{
  GFlagsClass *flags_class;
  GFlagsValue *flag_value;
  guint flags = 0;
1494
  char *str;
1495 1496 1497

  flags_class = g_type_class_ref (G_VALUE_TYPE (value));

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

1507
      flag_value = g_flags_get_value_by_nick (flags_class, str);
1508 1509
      if (!flag_value)
        {
1510 1511 1512 1513 1514 1515 1516
          _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);
1517 1518 1519
          g_type_class_unref (flags_class);
          return FALSE;
        }
1520 1521

      g_free (str);
1522
    }
1523
  while (_gtk_css_parser_try (parser, ",", FALSE));
1524 1525 1526 1527 1528 1529 1530 1531

  g_type_class_unref (flags_class);

  g_value_set_enum (value, flags);

  return TRUE;
}

1532 1533 1534
static void
flags_value_print (const GValue *value,
                   GString      *string)
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557
{
  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);
}

1558
static gboolean 
1559 1560 1561
bindings_value_parse (GtkCssParser *parser,
                      GFile        *base,
                      GValue       *value)
1562 1563
{
  GPtrArray *array;
1564 1565
  GtkBindingSet *binding_set;
  char *name;
1566 1567 1568

  array = g_ptr_array_new ();

1569 1570 1571 1572 1573 1574 1575 1576
  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;
        }
1577

1578
      binding_set = gtk_binding_set_find (name);
1579 1580

      if (!binding_set)
1581 1582 1583 1584 1585
        {
          _gtk_css_parser_error (parser, "No binding set named '%s'", name);
          g_free (name);
          continue;
        }
1586 1587

      g_ptr_array_add (array, binding_set);
1588
      g_free (name);
1589
    }
1590
  while (_gtk_css_parser_try (parser, ",", TRUE));
1591 1592 1593 1594 1595 1596

  g_value_take_boxed (value, array);

  return TRUE;
}

1597 1598 1599
static void
bindings_value_print (const GValue *value,
                      GString      *string)
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
{
  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)
1611 1612
        g_string_append (string, ", ");
      g_string_append (string, binding_set->set_name);
1613 1614 1615
    }
}

1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639