gtkstyleproperty.c 62.8 KB
Newer Older
Benjamin Otte's avatar
Benjamin Otte committed
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"
Benjamin Otte's avatar
Benjamin Otte committed
23 24 25 26 27 28 29 30 31

#include <errno.h>
#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
32
#include "gtkcssparserprivate.h"
33
#include "gtkcsstypesprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
34 35 36

/* the actual parsers we have */
#include "gtkanimationdescription.h"
37
#include "gtkbindings.h"
Benjamin Otte's avatar
Benjamin Otte committed
38 39
#include "gtk9slice.h"
#include "gtkgradient.h"
40
#include "gtkshadowprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
41
#include "gtkthemingengine.h"
42
#include "gtktypebuiltins.h"
Benjamin Otte's avatar
Benjamin Otte committed
43

44
static GHashTable *parse_funcs = NULL;
45
static GHashTable *print_funcs = NULL;
46
static GHashTable *properties = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
47 48

static void
49 50 51
register_conversion_function (GType             type,
                              GtkStyleParseFunc parse,
                              GtkStylePrintFunc print)
Benjamin Otte's avatar
Benjamin Otte committed
52
{
53 54
  if (parse)
    g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
55 56
  if (print)
    g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
Benjamin Otte's avatar
Benjamin Otte committed
57 58 59 60 61
}

/*** IMPLEMENTATIONS ***/

static gboolean 
62 63 64
rgba_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
65 66 67 68
{
  GtkSymbolicColor *symbolic;
  GdkRGBA rgba;

69
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
Benjamin Otte's avatar
Benjamin Otte committed
70 71 72
  if (symbolic == NULL)
    return FALSE;

73 74 75
  if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
    {
      g_value_set_boxed (value, &rgba);
76
      gtk_symbolic_color_unref (symbolic);
77 78 79 80 81 82 83 84
    }
  else
    {
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_SYMBOLIC_COLOR);
      g_value_take_boxed (value, symbolic);
    }

Benjamin Otte's avatar
Benjamin Otte committed
85 86 87
  return TRUE;
}

88 89 90
static void
rgba_value_print (const GValue *value,
                  GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
91 92 93 94
{
  const GdkRGBA *rgba = g_value_get_boxed (value);

  if (rgba == NULL)
95 96 97 98 99 100 101
    g_string_append (string, "none");
  else
    {
      char *s = gdk_rgba_to_string (rgba);
      g_string_append (string, s);
      g_free (s);
    }
Benjamin Otte's avatar
Benjamin Otte committed
102 103 104
}

static gboolean 
105 106 107
color_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
108 109
{
  GtkSymbolicColor *symbolic;
110
  GdkRGBA rgba;
Benjamin Otte's avatar
Benjamin Otte committed
111

112
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
Benjamin Otte's avatar
Benjamin Otte committed
113 114 115
  if (symbolic == NULL)
    return FALSE;

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
  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);
    }

Benjamin Otte's avatar
Benjamin Otte committed
133 134 135
  return TRUE;
}

136 137 138
static void
color_value_print (const GValue *value,
                   GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
139 140 141 142
{
  const GdkColor *color = g_value_get_boxed (value);

  if (color == NULL)
143 144 145 146 147 148 149
    g_string_append (string, "none");
  else
    {
      char *s = gdk_color_to_string (color);
      g_string_append (string, s);
      g_free (s);
    }
Benjamin Otte's avatar
Benjamin Otte committed
150 151
}

152 153 154 155
static gboolean
symbolic_color_value_parse (GtkCssParser *parser,
                            GFile        *base,
                            GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
156 157 158
{
  GtkSymbolicColor *symbolic;

159
  symbolic = _gtk_css_parser_read_symbolic_color (parser);
Benjamin Otte's avatar
Benjamin Otte committed
160 161 162 163 164 165 166
  if (symbolic == NULL)
    return FALSE;

  g_value_take_boxed (value, symbolic);
  return TRUE;
}

167 168 169
static void
symbolic_color_value_print (const GValue *value,
                            GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
170 171 172 173
{
  GtkSymbolicColor *symbolic = g_value_get_boxed (value);

  if (symbolic == NULL)
174 175 176 177 178 179 180
    g_string_append (string, "none");
  else
    {
      char *s = gtk_symbolic_color_to_string (symbolic);
      g_string_append (string, s);
      g_free (s);
    }
Benjamin Otte's avatar
Benjamin Otte committed
181 182 183
}

static gboolean 
184 185 186
font_description_value_parse (GtkCssParser *parser,
                              GFile        *base,
                              GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
187 188
{
  PangoFontDescription *font_desc;
189 190 191 192 193
  char *str;

  str = _gtk_css_parser_read_value (parser);
  if (str == NULL)
    return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
194 195

  font_desc = pango_font_description_from_string (str);
196
  g_free (str);
Benjamin Otte's avatar
Benjamin Otte committed
197 198 199 200
  g_value_take_boxed (value, font_desc);
  return TRUE;
}

201 202 203
static void
font_description_value_print (const GValue *value,
                              GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
204 205 206 207
{
  const PangoFontDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
208 209 210 211 212 213 214
    g_string_append (string, "none");
  else
    {
      char *s = pango_font_description_to_string (desc);
      g_string_append (string, s);
      g_free (s);
    }
Benjamin Otte's avatar
Benjamin Otte committed
215 216 217
}

static gboolean 
218 219 220
boolean_value_parse (GtkCssParser *parser,
                     GFile        *base,
                     GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
221
{
222 223
  if (_gtk_css_parser_try (parser, "true", TRUE) ||
      _gtk_css_parser_try (parser, "1", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
224 225 226 227
    {
      g_value_set_boolean (value, TRUE);
      return TRUE;
    }
228 229
  else if (_gtk_css_parser_try (parser, "false", TRUE) ||
           _gtk_css_parser_try (parser, "0", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
230 231 232 233
    {
      g_value_set_boolean (value, FALSE);
      return TRUE;
    }
234 235 236 237 238
  else
    {
      _gtk_css_parser_error (parser, "Expected a boolean value");
      return FALSE;
    }
Benjamin Otte's avatar
Benjamin Otte committed
239 240
}

241 242 243
static void
boolean_value_print (const GValue *value,
                     GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
244 245
{
  if (g_value_get_boolean (value))
246
    g_string_append (string, "true");
Benjamin Otte's avatar
Benjamin Otte committed
247
  else
248
    g_string_append (string, "false");
Benjamin Otte's avatar
Benjamin Otte committed
249 250 251
}

static gboolean 
252 253 254
int_value_parse (GtkCssParser *parser,
                 GFile        *base,
                 GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
255
{
256
  gint i;
Benjamin Otte's avatar
Benjamin Otte committed
257

258
  if (!_gtk_css_parser_try_int (parser, &i))
Benjamin Otte's avatar
Benjamin Otte committed
259
    {
260
      _gtk_css_parser_error (parser, "Expected a valid integer value");
Benjamin Otte's avatar
Benjamin Otte committed
261 262 263 264 265 266 267
      return FALSE;
    }

  g_value_set_int (value, i);
  return TRUE;
}

268 269 270
static void
int_value_print (const GValue *value,
                 GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
271
{
272
  g_string_append_printf (string, "%d", g_value_get_int (value));
Benjamin Otte's avatar
Benjamin Otte committed
273 274 275
}

static gboolean 
276 277 278
uint_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
279
{
280
  guint u;
Benjamin Otte's avatar
Benjamin Otte committed
281

282
  if (!_gtk_css_parser_try_uint (parser, &u))
Benjamin Otte's avatar
Benjamin Otte committed
283
    {
284
      _gtk_css_parser_error (parser, "Expected a valid unsigned value");
Benjamin Otte's avatar
Benjamin Otte committed
285 286 287 288 289 290 291
      return FALSE;
    }

  g_value_set_uint (value, u);
  return TRUE;
}

292 293 294
static void
uint_value_print (const GValue *value,
                  GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
295
{
296
  g_string_append_printf (string, "%u", g_value_get_uint (value));
Benjamin Otte's avatar
Benjamin Otte committed
297 298 299
}

static gboolean 
300 301 302
double_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
303
{
304
  gdouble d;
Benjamin Otte's avatar
Benjamin Otte committed
305

306
  if (!_gtk_css_parser_try_double (parser, &d))
Benjamin Otte's avatar
Benjamin Otte committed
307
    {
308
      _gtk_css_parser_error (parser, "Expected a number");
Benjamin Otte's avatar
Benjamin Otte committed
309 310 311 312 313 314 315
      return FALSE;
    }

  g_value_set_double (value, d);
  return TRUE;
}

316
static void
317 318
string_append_double (GString *string,
                      double   d)
Benjamin Otte's avatar
Benjamin Otte committed
319 320 321
{
  char buf[G_ASCII_DTOSTR_BUF_SIZE];

322
  g_ascii_dtostr (buf, sizeof (buf), d);
323
  g_string_append (string, buf);
Benjamin Otte's avatar
Benjamin Otte committed
324 325
}

326 327 328 329 330 331 332
static void
double_value_print (const GValue *value,
                    GString      *string)
{
  string_append_double (string, g_value_get_double (value));
}

Benjamin Otte's avatar
Benjamin Otte committed
333
static gboolean 
334 335 336
float_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
337
{
338
  gdouble d;
Benjamin Otte's avatar
Benjamin Otte committed
339

340
  if (!_gtk_css_parser_try_double (parser, &d))
Benjamin Otte's avatar
Benjamin Otte committed
341
    {
342
      _gtk_css_parser_error (parser, "Expected a number");
Benjamin Otte's avatar
Benjamin Otte committed
343 344 345 346 347 348 349
      return FALSE;
    }

  g_value_set_float (value, d);
  return TRUE;
}

350 351 352
static void
float_value_print (const GValue *value,
                   GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
353
{
354
  string_append_double (string, g_value_get_float (value));
Benjamin Otte's avatar
Benjamin Otte committed
355 356
}

357
static gboolean 
358 359 360
string_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
361
{
362
  char *str = _gtk_css_parser_read_string (parser);
363

364
  if (str == NULL)
365 366
    return FALSE;

367
  g_value_take_string (value, str);
368 369 370
  return TRUE;
}

371 372 373
static void
string_value_print (const GValue *value,
                    GString      *str)
374 375 376 377 378
{
  const char *string;
  gsize len;

  string = g_value_get_string (value);
379
  g_string_append_c (str, '"');
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

  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, '"');
}

Benjamin Otte's avatar
Benjamin Otte committed
410
static gboolean 
411 412 413
theming_engine_value_parse (GtkCssParser *parser,
                            GFile        *base,
                            GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
414 415
{
  GtkThemingEngine *engine;
416 417 418 419 420 421 422 423
  char *str;

  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
    {
      _gtk_css_parser_error (parser, "Expected a valid theme name");
      return FALSE;
    }
Benjamin Otte's avatar
Benjamin Otte committed
424 425 426 427

  engine = gtk_theming_engine_load (str);
  if (engine == NULL)
    {
428 429
      _gtk_css_parser_error (parser, "Themeing engine '%s' not found", str);
      g_free (str);
Benjamin Otte's avatar
Benjamin Otte committed
430 431 432 433
      return FALSE;
    }

  g_value_set_object (value, engine);
434
  g_free (str);
Benjamin Otte's avatar
Benjamin Otte committed
435 436 437
  return TRUE;
}

438 439 440
static void
theming_engine_value_print (const GValue *value,
                            GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
441 442 443 444 445 446
{
  GtkThemingEngine *engine;
  char *name;

  engine = g_value_get_object (value);
  if (engine == NULL)
447 448 449 450 451 452 453 454
    g_string_append (string, "none");
  else
    {
      /* XXX: gtk_theming_engine_get_name()? */
      g_object_get (engine, "name", &name, NULL);
      g_string_append (string, name);
      g_free (name);
    }
Benjamin Otte's avatar
Benjamin Otte committed
455 456 457
}

static gboolean 
458 459 460
animation_description_value_parse (GtkCssParser *parser,
                                   GFile        *base,
                                   GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
461 462
{
  GtkAnimationDescription *desc;
463 464 465 466 467
  char *str;

  str = _gtk_css_parser_read_value (parser);
  if (str == NULL)
    return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
468 469

  desc = _gtk_animation_description_from_string (str);
470
  g_free (str);
Benjamin Otte's avatar
Benjamin Otte committed
471 472

  if (desc == NULL)
473 474 475 476
    {
      _gtk_css_parser_error (parser, "Invalid animation description");
      return FALSE;
    }
Benjamin Otte's avatar
Benjamin Otte committed
477 478 479 480 481
  
  g_value_take_boxed (value, desc);
  return TRUE;
}

482 483 484
static void
animation_description_value_print (const GValue *value,
                                   GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
485 486 487 488
{
  GtkAnimationDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
489 490 491
    g_string_append (string, "none");
  else
    _gtk_animation_description_print (desc, string);
Benjamin Otte's avatar
Benjamin Otte committed
492 493
}

494 495 496 497
static gboolean 
border_value_parse (GtkCssParser *parser,
                    GFile        *base,
                    GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
498
{
499 500
  GtkBorder border = { 0, };
  guint i, numbers[4];
Benjamin Otte's avatar
Benjamin Otte committed
501

502
  for (i = 0; i < G_N_ELEMENTS (numbers); i++)
Benjamin Otte's avatar
Benjamin Otte committed
503
    {
504 505
      if (!_gtk_css_parser_try_uint (parser, &numbers[i]))
        break;
Benjamin Otte's avatar
Benjamin Otte committed
506

507 508
      /* XXX: shouldn't allow spaces here? */
      _gtk_css_parser_try (parser, "px", TRUE);
Benjamin Otte's avatar
Benjamin Otte committed
509 510
    }

511
  if (i == 0)
Benjamin Otte's avatar
Benjamin Otte committed
512
    {
513
      _gtk_css_parser_error (parser, "Expected valid border");
Benjamin Otte's avatar
Benjamin Otte committed
514 515 516
      return FALSE;
    }

517 518 519
  border.top = numbers[0];
  if (i > 1)
    border.right = numbers[1];
Benjamin Otte's avatar
Benjamin Otte committed
520
  else
521 522 523
    border.right = border.top;
  if (i > 2)
    border.bottom = numbers[2];
Benjamin Otte's avatar
Benjamin Otte committed
524
  else
525 526 527
    border.bottom = border.top;
  if (i > 3)
    border.left = numbers[3];
Benjamin Otte's avatar
Benjamin Otte committed
528
  else
529
    border.left = border.right;
Benjamin Otte's avatar
Benjamin Otte committed
530

531
  g_value_set_boxed (value, &border);
Benjamin Otte's avatar
Benjamin Otte committed
532 533 534
  return TRUE;
}

535 536
static void
border_value_print (const GValue *value, GString *string)
Benjamin Otte's avatar
Benjamin Otte committed
537 538 539 540
{
  const GtkBorder *border = g_value_get_boxed (value);

  if (border == NULL)
541
    g_string_append (string, "none");
Benjamin Otte's avatar
Benjamin Otte committed
542
  else if (border->left != border->right)
543
    g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
Benjamin Otte's avatar
Benjamin Otte committed
544
  else if (border->top != border->bottom)
545
    g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
Benjamin Otte's avatar
Benjamin Otte committed
546
  else if (border->top != border->left)
547
    g_string_append_printf (string, "%d %d", border->top, border->right);
Benjamin Otte's avatar
Benjamin Otte committed
548
  else
549
    g_string_append_printf (string, "%d", border->top);
Benjamin Otte's avatar
Benjamin Otte committed
550 551
}

552 553 554 555
static gboolean 
gradient_value_parse (GtkCssParser *parser,
                      GFile        *base,
                      GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
556 557 558 559 560 561
{
  GtkGradient *gradient;
  cairo_pattern_type_t type;
  gdouble coords[6];
  guint i;

Benjamin Otte's avatar
Benjamin Otte committed
562
  if (!_gtk_css_parser_try (parser, "-gtk-gradient", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
563
    {
Benjamin Otte's avatar
Benjamin Otte committed
564 565
      _gtk_css_parser_error (parser,
                             "Expected '-gtk-gradient'");
566
      return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
567 568
    }

Benjamin Otte's avatar
Benjamin Otte committed
569
  if (!_gtk_css_parser_try (parser, "(", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
570
    {
Benjamin Otte's avatar
Benjamin Otte committed
571 572
      _gtk_css_parser_error (parser,
                             "Expected '(' after '-gtk-gradient'");
573
      return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
574
    }
Benjamin Otte's avatar
Benjamin Otte committed
575 576 577 578 579 580

  /* 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;
Benjamin Otte's avatar
Benjamin Otte committed
581 582
  else
    {
Benjamin Otte's avatar
Benjamin Otte committed
583 584
      _gtk_css_parser_error (parser,
                             "Gradient type must be 'radial' or 'linear'");
585
      return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
586 587 588 589 590
    }

  /* Parse start/stop position parameters */
  for (i = 0; i < 2; i++)
    {
Benjamin Otte's avatar
Benjamin Otte committed
591
      if (! _gtk_css_parser_try (parser, ",", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
592
        {
Benjamin Otte's avatar
Benjamin Otte committed
593 594
          _gtk_css_parser_error (parser,
                                 "Expected ','");
595
          return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
596 597
        }

Benjamin Otte's avatar
Benjamin Otte committed
598 599 600 601 602 603 604
      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]))
Benjamin Otte's avatar
Benjamin Otte committed
605
        {
Benjamin Otte's avatar
Benjamin Otte committed
606 607
          _gtk_css_parser_error (parser,
                                 "Expected a valid X coordinate");
608
          return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
609 610
        }

Benjamin Otte's avatar
Benjamin Otte committed
611 612 613 614 615 616 617
      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]))
Benjamin Otte's avatar
Benjamin Otte committed
618
        {
Benjamin Otte's avatar
Benjamin Otte committed
619 620
          _gtk_css_parser_error (parser,
                                 "Expected a valid Y coordinate");
621
          return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
622 623 624 625 626
        }

      if (type == CAIRO_PATTERN_TYPE_RADIAL)
        {
          /* Parse radius */
Benjamin Otte's avatar
Benjamin Otte committed
627
          if (! _gtk_css_parser_try (parser, ",", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
628
            {
Benjamin Otte's avatar
Benjamin Otte committed
629 630
              _gtk_css_parser_error (parser,
                                     "Expected ','");
631
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
632 633
            }

Benjamin Otte's avatar
Benjamin Otte committed
634 635 636 637
          if (! _gtk_css_parser_try_double (parser, &coords[(i * 3) + 2]))
            {
              _gtk_css_parser_error (parser,
                                     "Expected a numer for the radius");
638
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
639
            }
Benjamin Otte's avatar
Benjamin Otte committed
640 641 642 643 644 645 646 647 648
        }
    }

  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
649
  while (_gtk_css_parser_try (parser, ",", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
650 651 652 653
    {
      GtkSymbolicColor *color;
      gdouble position;

Benjamin Otte's avatar
Benjamin Otte committed
654
      if (_gtk_css_parser_try (parser, "from", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
655 656 657
        {
          position = 0;

Benjamin Otte's avatar
Benjamin Otte committed
658
          if (!_gtk_css_parser_try (parser, "(", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
659
            {
Benjamin Otte's avatar
Benjamin Otte committed
660 661 662
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
663
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
664
            }
Benjamin Otte's avatar
Benjamin Otte committed
665

Benjamin Otte's avatar
Benjamin Otte committed
666
        }
Benjamin Otte's avatar
Benjamin Otte committed
667
      else if (_gtk_css_parser_try (parser, "to", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
668 669 670
        {
          position = 1;

Benjamin Otte's avatar
Benjamin Otte committed
671
          if (!_gtk_css_parser_try (parser, "(", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
672
            {
Benjamin Otte's avatar
Benjamin Otte committed
673 674 675
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
676
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
677
            }
Benjamin Otte's avatar
Benjamin Otte committed
678

Benjamin Otte's avatar
Benjamin Otte committed
679
        }
Benjamin Otte's avatar
Benjamin Otte committed
680
      else if (_gtk_css_parser_try (parser, "color-stop", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
681
        {
Benjamin Otte's avatar
Benjamin Otte committed
682
          if (!_gtk_css_parser_try (parser, "(", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
683
            {
Benjamin Otte's avatar
Benjamin Otte committed
684 685 686
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected '('");
687
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
688 689
            }

Benjamin Otte's avatar
Benjamin Otte committed
690 691 692 693 694
          if (!_gtk_css_parser_try_double (parser, &position))
            {
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a valid number");
695
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
696
            }
Benjamin Otte's avatar
Benjamin Otte committed
697

Benjamin Otte's avatar
Benjamin Otte committed
698
          if (!_gtk_css_parser_try (parser, ",", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
699
            {
Benjamin Otte's avatar
Benjamin Otte committed
700 701 702
              gtk_gradient_unref (gradient);
              _gtk_css_parser_error (parser,
                                     "Expected a comma");
703
              return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
704 705 706 707
            }
        }
      else
        {
Benjamin Otte's avatar
Benjamin Otte committed
708 709 710
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Not a valid color-stop definition");
711
          return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
712 713
        }

Benjamin Otte's avatar
Benjamin Otte committed
714 715
      color = _gtk_css_parser_read_symbolic_color (parser);
      if (color == NULL)
Benjamin Otte's avatar
Benjamin Otte committed
716
        {
Benjamin Otte's avatar
Benjamin Otte committed
717
          gtk_gradient_unref (gradient);
718
          return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
719 720
        }

Benjamin Otte's avatar
Benjamin Otte committed
721 722
      gtk_gradient_add_color_stop (gradient, position, color);
      gtk_symbolic_color_unref (color);
Benjamin Otte's avatar
Benjamin Otte committed
723

Benjamin Otte's avatar
Benjamin Otte committed
724
      if (!_gtk_css_parser_try (parser, ")", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
725
        {
Benjamin Otte's avatar
Benjamin Otte committed
726 727 728
          gtk_gradient_unref (gradient);
          _gtk_css_parser_error (parser,
                                 "Expected ')'");
729
          return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
730 731 732
        }
    }

Benjamin Otte's avatar
Benjamin Otte committed
733
  if (!_gtk_css_parser_try (parser, ")", TRUE))
Benjamin Otte's avatar
Benjamin Otte committed
734
    {
Benjamin Otte's avatar
Benjamin Otte committed
735 736 737
      gtk_gradient_unref (gradient);
      _gtk_css_parser_error (parser,
                             "Expected ')'");
738
      return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
739 740
    }

741
  g_value_take_boxed (value, gradient);
Benjamin Otte's avatar
Benjamin Otte committed
742 743 744
  return TRUE;
}

745 746 747
static void
gradient_value_print (const GValue *value,
                      GString      *string)
Benjamin Otte's avatar
Benjamin Otte committed
748 749 750 751
{
  GtkGradient *gradient = g_value_get_boxed (value);

  if (gradient == NULL)
752 753 754 755 756 757 758
    g_string_append (string, "none");
  else
    {
      char *s = gtk_gradient_to_string (gradient);
      g_string_append (string, s);
      g_free (s);
    }
Benjamin Otte's avatar
Benjamin Otte committed
759 760
}

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
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,
                                           "Whitespace between 'url' and '(' is not allowed");
                             
              _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;
}

Benjamin Otte's avatar
Benjamin Otte committed
817
static gboolean 
818 819 820
pattern_value_parse (GtkCssParser *parser,
                     GFile        *base,
                     GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
821
{
822
  if (_gtk_css_parser_begins_with (parser, '-'))
Benjamin Otte's avatar
Benjamin Otte committed
823 824 825
    {
      g_value_unset (value);
      g_value_init (value, GTK_TYPE_GRADIENT);
826
      return gradient_value_parse (parser, base, value);
Benjamin Otte's avatar
Benjamin Otte committed
827 828 829
    }
  else
    {
830
      GError *error = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
831 832 833
      gchar *path;
      GdkPixbuf *pixbuf;
      GFile *file;
834 835 836 837
      cairo_surface_t *surface;
      cairo_pattern_t *pattern;
      cairo_t *cr;
      cairo_matrix_t matrix;
Benjamin Otte's avatar
Benjamin Otte committed
838

839
      file = gtk_css_parse_url (parser, base);
Benjamin Otte's avatar
Benjamin Otte committed
840 841 842 843 844 845
      if (file == NULL)
        return FALSE;

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

846
      pixbuf = gdk_pixbuf_new_from_file (path, &error);
Benjamin Otte's avatar
Benjamin Otte committed
847 848
      g_free (path);
      if (pixbuf == NULL)
849 850 851 852
        {
          _gtk_css_parser_take_error (parser, error);
          return FALSE;
        }
Benjamin Otte's avatar
Benjamin Otte committed
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876

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

877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
static gboolean
shadow_value_parse (GtkCssParser *parser,
                    GFile *base,
                    GValue *value)
{
  gboolean inset;
  gdouble hoffset, voffset, blur, spread;
  GtkSymbolicColor *color;
  GtkShadow *shadow;

  shadow = _gtk_shadow_new ();

  do
    {
      inset = _gtk_css_parser_try (parser, "inset", TRUE);

      if (!_gtk_css_parser_try_double (parser, &hoffset) ||
          !_gtk_css_parser_try_double (parser, &voffset))
        {
          _gtk_css_parser_error (parser, "Horizontal and vertical offsets are required");
          _gtk_shadow_unref (shadow);
          return FALSE;
        }

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

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

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

      _gtk_shadow_append (shadow,
                          hoffset, voffset,
                          blur, spread,
                          inset, color);

      gtk_symbolic_color_unref (color);

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

  g_value_take_boxed (value, shadow);
  return TRUE;
}

932 933 934
static void
shadow_value_print (const GValue *value,
                    GString      *string)
935 936 937 938 939 940
{
  GtkShadow *shadow;

  shadow = g_value_get_boxed (value);

  if (shadow == NULL)
941 942 943
    g_string_append (string, "none");
  else
    _gtk_shadow_print (shadow, string);
944 945
}

Benjamin Otte's avatar
Benjamin Otte committed
946
static gboolean 
947 948 949
slice_value_parse (GtkCssParser *parser,
                   GFile        *base,
                   GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
950 951 952 953 954 955 956
{
  gdouble distance_top, distance_bottom;
  gdouble distance_left, distance_right;
  GtkSliceSideModifier mods[2];
  GdkPixbuf *pixbuf;
  Gtk9Slice *slice;
  GFile *file;
957 958
  GError *error = NULL;
  gint i;
Benjamin Otte's avatar
Benjamin Otte committed
959 960 961
  char *path;

  /* Parse image url */
962
  file = gtk_css_parse_url (parser, base);
Benjamin Otte's avatar
Benjamin Otte committed
963 964 965
  if (!file)
      return FALSE;

966 967 968 969
  if (!_gtk_css_parser_try_double (parser, &distance_top) ||
      !_gtk_css_parser_try_double (parser, &distance_right) ||
      !_gtk_css_parser_try_double (parser, &distance_bottom) ||
      !_gtk_css_parser_try_double (parser, &distance_left))
Benjamin Otte's avatar
Benjamin Otte committed
970
    {
971
      _gtk_css_parser_error (parser, "Expected a number");
Benjamin Otte's avatar
Benjamin Otte committed
972
      g_object_unref (file);
973
      return FALSE;
Benjamin Otte's avatar
Benjamin Otte committed
974 975
    }

976
  for (i = 0; i < 2; i++)
Benjamin Otte's avatar
Benjamin Otte committed
977
    {
978 979 980 981 982 983 984 985 986 987 988
      if (_gtk_css_parser_try (parser, "stretch", TRUE))
        mods[i] = GTK_SLICE_STRETCH;
      else if (_gtk_css_parser_try (parser, "repeat", TRUE))
        mods[i] = GTK_SLICE_REPEAT;
      else if (i == 0)
        {
          mods[1] = mods[0] = GTK_SLICE_STRETCH;
          break;
        }
      else
        mods[i] = mods[0];
Benjamin Otte's avatar
Benjamin Otte committed
989 990 991
    }

  path = g_file_get_path (file);
992 993
  g_object_unref (file);
  pixbuf = gdk_pixbuf_new_from_file (path, &error);
Benjamin Otte's avatar
Benjamin Otte committed
994 995
  g_free (path);
  if (!pixbuf)
996 997 998 999
    {
      _gtk_css_parser_take_error (parser, error);
      return FALSE;
    }
Benjamin Otte's avatar
Benjamin Otte committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

  slice = _gtk_9slice_new (pixbuf,
                           distance_top, distance_bottom,
                           distance_left, distance_right,
                           mods[0], mods[1]);
  g_object_unref (pixbuf);

  g_value_take_boxed (value, slice);
  return TRUE;
}

static gboolean 
1012 1013 1014
enum_value_parse (GtkCssParser *parser,
                  GFile        *base,
                  GValue       *value)
Benjamin Otte's avatar
Benjamin Otte committed
1015 1016 1017
{
  GEnumClass *enum_class;
  GEnumValue *enum_value;
1018
  char *str;
Benjamin Otte's avatar
Benjamin Otte committed
1019

1020 1021
  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
Benjamin Otte's avatar
Benjamin Otte committed
1022
    {
1023
      _gtk_css_parser_error (parser, "Expected an identifier");
Benjamin Otte's avatar
Benjamin Otte committed