gtkcssstylefuncs.c 31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25 26 27 28 29
 */

#include "config.h"

#include "gtkcssstylefuncsprivate.h"

#include <errno.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

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

30
#include "gtkcsscolorvalueprivate.h"
31
#include "gtkcssimagegradientprivate.h"
32
#include "gtkcssprovider.h"
33
#include "gtkcssrgbavalueprivate.h"
34
#include "gtkcsstypedvalueprivate.h"
35 36
#include "gtkcsstypesprivate.h"
#include "gtkprivatetypebuiltins.h"
Benjamin Otte's avatar
Benjamin Otte committed
37
#include "gtkstylecontextprivate.h"
38
#include "gtktypebuiltins.h"
39
#include "gtkcsswin32sizevalueprivate.h"
40

41
#include "deprecated/gtkthemingengine.h"
42 43 44
#include "deprecated/gtkgradientprivate.h"
#include "deprecated/gtksymboliccolorprivate.h"

45 46 47 48 49 50 51
/* 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"

static GHashTable *parse_funcs = NULL;
static GHashTable *print_funcs = NULL;
Benjamin Otte's avatar
Benjamin Otte committed
52
static GHashTable *compute_funcs = NULL;
53

54 55 56 57 58
typedef gboolean         (* GtkStyleParseFunc)             (GtkCssParser            *parser,
                                                            GValue                  *value);
typedef void             (* GtkStylePrintFunc)             (const GValue            *value,
                                                            GString                 *string);
typedef GtkCssValue *    (* GtkStyleComputeFunc)           (GtkStyleProviderPrivate *provider,
59 60
                                                            GtkCssStyle             *values,
                                                            GtkCssStyle             *parent_values,
61
                                                            GtkCssValue             *specified);
62 63

static void
Benjamin Otte's avatar
Benjamin Otte committed
64 65 66 67
register_conversion_function (GType               type,
                              GtkStyleParseFunc   parse,
                              GtkStylePrintFunc   print,
                              GtkStyleComputeFunc compute)
68 69 70 71 72
{
  if (parse)
    g_hash_table_insert (parse_funcs, GSIZE_TO_POINTER (type), parse);
  if (print)
    g_hash_table_insert (print_funcs, GSIZE_TO_POINTER (type), print);
Benjamin Otte's avatar
Benjamin Otte committed
73 74
  if (compute)
    g_hash_table_insert (compute_funcs, GSIZE_TO_POINTER (type), compute);
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
}

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)
{
91
  _gtk_css_print_string (str, string);
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
}

/*** IMPLEMENTATIONS ***/

static gboolean 
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
rgba_value_parse (GtkCssParser *parser,
                  GValue       *value)
{
  GtkSymbolicColor *symbolic;
  GdkRGBA rgba;

Benjamin Otte's avatar
Benjamin Otte committed
144 145
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

146
  symbolic = _gtk_css_symbolic_value_new (parser);
147 148
  if (symbolic == NULL)
    return FALSE;
149 150 151 152 153 154 155 156 157 158 159 160 161

  if (gtk_symbolic_color_resolve (symbolic, NULL, &rgba))
    {
      g_value_set_boxed (value, &rgba);
      gtk_symbolic_color_unref (symbolic);
    }
  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
162 163
  G_GNUC_END_IGNORE_DEPRECATIONS;

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  return TRUE;
}

static void
rgba_value_print (const GValue *value,
                  GString      *string)
{
  const GdkRGBA *rgba = g_value_get_boxed (value);

  if (rgba == NULL)
    g_string_append (string, "none");
  else
    {
      char *s = gdk_rgba_to_string (rgba);
      g_string_append (string, s);
      g_free (s);
    }
}

183
static GtkCssValue *
184
rgba_value_compute (GtkStyleProviderPrivate *provider,
185 186
                    GtkCssStyle             *values,
                    GtkCssStyle             *parent_values,
187
                    GtkCssValue             *specified)
Benjamin Otte's avatar
Benjamin Otte committed
188
{
189
  GdkRGBA white = { 1, 1, 1, 1 };
190 191 192
  const GValue *value;

  value = _gtk_css_typed_value_get (specified);
193 194 195

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

196
  if (G_VALUE_HOLDS (value, GTK_TYPE_SYMBOLIC_COLOR))
Benjamin Otte's avatar
Benjamin Otte committed
197
    {
198
      GtkSymbolicColor *symbolic = g_value_get_boxed (value);
199
      GtkCssValue *val;
200
      GValue new_value = G_VALUE_INIT;
201
      GdkRGBA rgba;
202

203 204
      val = _gtk_css_color_value_resolve (_gtk_symbolic_color_get_css_value (symbolic),
                                          provider,
205
                                          gtk_css_style_get_value (values, GTK_CSS_PROPERTY_COLOR),
206
                                          NULL);
207 208 209 210 211 212
      if (val != NULL)
        {
          rgba = *_gtk_css_rgba_value_get_rgba (val);
          _gtk_css_value_unref (val);
        }
      else
213
        rgba = white;
214

215 216 217
      g_value_init (&new_value, GDK_TYPE_RGBA);
      g_value_set_boxed (&new_value, &rgba);
      return _gtk_css_typed_value_new_take (&new_value);
Benjamin Otte's avatar
Benjamin Otte committed
218 219
    }
  else
220
    return _gtk_css_value_ref (specified);
221 222

  G_GNUC_END_IGNORE_DEPRECATIONS;
Benjamin Otte's avatar
Benjamin Otte committed
223 224
}

225
static gboolean
226 227 228 229 230 231
color_value_parse (GtkCssParser *parser,
                   GValue       *value)
{
  GtkSymbolicColor *symbolic;
  GdkRGBA rgba;

Benjamin Otte's avatar
Benjamin Otte committed
232 233
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

234
  symbolic = _gtk_css_symbolic_value_new (parser);
235 236 237 238 239 240 241 242 243 244 245 246
  if (symbolic == NULL)
    return FALSE;

  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);
247
      gtk_symbolic_color_unref (symbolic);
248 249 250 251 252 253 254 255
    }
  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
256 257
  G_GNUC_END_IGNORE_DEPRECATIONS;

258 259 260 261 262 263 264 265 266 267 268 269 270
  return TRUE;
}

static void
color_value_print (const GValue *value,
                   GString      *string)
{
  const GdkColor *color = g_value_get_boxed (value);

  if (color == NULL)
    g_string_append (string, "none");
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
271
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
272
      char *s = gdk_color_to_string (color);
Matthias Clasen's avatar
Matthias Clasen committed
273
G_GNUC_END_IGNORE_DEPRECATIONS
274 275 276 277 278
      g_string_append (string, s);
      g_free (s);
    }
}

279
static GtkCssValue *
280
color_value_compute (GtkStyleProviderPrivate *provider,
281 282
                     GtkCssStyle             *values,
                     GtkCssStyle             *parent_values,
283
                     GtkCssValue             *specified)
Benjamin Otte's avatar
Benjamin Otte committed
284 285
{
  GdkColor color = { 0, 65535, 65535, 65535 };
286
  const GValue *value;
Benjamin Otte's avatar
Benjamin Otte committed
287

288
  value = _gtk_css_typed_value_get (specified);
289 290 291

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

292
  if (G_VALUE_HOLDS (value, GTK_TYPE_SYMBOLIC_COLOR))
Benjamin Otte's avatar
Benjamin Otte committed
293
    {
294
      GValue new_value = G_VALUE_INIT;
295 296
      GtkCssValue *val;

297 298
      val = _gtk_css_color_value_resolve (_gtk_symbolic_color_get_css_value (g_value_get_boxed (value)),
                                          provider,
299
                                          gtk_css_style_get_value (values, GTK_CSS_PROPERTY_COLOR),
300
                                          NULL);
301
      if (val != NULL)
Benjamin Otte's avatar
Benjamin Otte committed
302
        {
303 304 305 306 307
          const GdkRGBA *rgba = _gtk_css_rgba_value_get_rgba (val);
          color.red = rgba->red * 65535. + 0.5;
          color.green = rgba->green * 65535. + 0.5;
          color.blue = rgba->blue * 65535. + 0.5;
          _gtk_css_value_unref (val);
Benjamin Otte's avatar
Benjamin Otte committed
308
        }
309

310 311 312
      g_value_init (&new_value, GDK_TYPE_COLOR);
      g_value_set_boxed (&new_value, &color);
      return _gtk_css_typed_value_new_take (&new_value);
Benjamin Otte's avatar
Benjamin Otte committed
313 314
    }
  else
315
    return _gtk_css_value_ref (specified);
316 317

  G_GNUC_END_IGNORE_DEPRECATIONS;
Benjamin Otte's avatar
Benjamin Otte committed
318 319
}

320 321 322 323 324 325
static gboolean
symbolic_color_value_parse (GtkCssParser *parser,
                            GValue       *value)
{
  GtkSymbolicColor *symbolic;

326
  symbolic = _gtk_css_symbolic_value_new (parser);
327 328
  if (symbolic == NULL)
    return FALSE;
329 330 331 332 333 334 335 336 337 338 339

  g_value_take_boxed (value, symbolic);
  return TRUE;
}

static void
symbolic_color_value_print (const GValue *value,
                            GString      *string)
{
  GtkSymbolicColor *symbolic = g_value_get_boxed (value);

Benjamin Otte's avatar
Benjamin Otte committed
340 341
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

342 343 344 345 346 347 348 349
  if (symbolic == NULL)
    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
350 351

  G_GNUC_END_IGNORE_DEPRECATIONS;
352 353 354 355 356 357 358 359 360 361 362 363 364 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
}

static gboolean 
font_description_value_parse (GtkCssParser *parser,
                              GValue       *value)
{
  PangoFontDescription *font_desc;
  guint mask;
  char *str;

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

  font_desc = pango_font_description_from_string (str);
  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);
  g_free (str);
  g_value_take_boxed (value, font_desc);
  return TRUE;
}

static void
font_description_value_print (const GValue *value,
                              GString      *string)
{
  const PangoFontDescription *desc = g_value_get_boxed (value);

  if (desc == NULL)
    g_string_append (string, "none");
  else
    {
      char *s = pango_font_description_to_string (desc);
      g_string_append (string, s);
      g_free (s);
    }
}

static gboolean 
boolean_value_parse (GtkCssParser *parser,
                     GValue       *value)
{
  if (_gtk_css_parser_try (parser, "true", TRUE) ||
      _gtk_css_parser_try (parser, "1", TRUE))
    {
      g_value_set_boolean (value, TRUE);
      return TRUE;
    }
  else if (_gtk_css_parser_try (parser, "false", TRUE) ||
           _gtk_css_parser_try (parser, "0", TRUE))
    {
      g_value_set_boolean (value, FALSE);
      return TRUE;
    }
  else
    {
      _gtk_css_parser_error (parser, "Expected a boolean value");
      return FALSE;
    }
}

static void
boolean_value_print (const GValue *value,
                     GString      *string)
{
  if (g_value_get_boolean (value))
    g_string_append (string, "true");
  else
    g_string_append (string, "false");
}

static gboolean 
int_value_parse (GtkCssParser *parser,
                 GValue       *value)
{
  gint i;

434
  if (_gtk_css_parser_has_prefix (parser, "-gtk"))
435
    {
436 437 438 439 440 441 442 443 444 445
      GtkCssValue *cssvalue = gtk_css_win32_size_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_NUMBER_AS_PIXELS);

      if (cssvalue)
        {
          g_value_set_int (value, _gtk_css_number_value_get (cssvalue, 100));
          _gtk_css_value_unref (cssvalue);
          return TRUE;
        }

      return FALSE;
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
    }

  if (!_gtk_css_parser_try_int (parser, &i))
    {
      _gtk_css_parser_error (parser, "Expected a valid integer value");
      return FALSE;
    }

  g_value_set_int (value, i);
  return TRUE;
}

static void
int_value_print (const GValue *value,
                 GString      *string)
{
  g_string_append_printf (string, "%d", g_value_get_int (value));
}

static gboolean 
uint_value_parse (GtkCssParser *parser,
                  GValue       *value)
{
  guint u;

  if (!_gtk_css_parser_try_uint (parser, &u))
    {
      _gtk_css_parser_error (parser, "Expected a valid unsigned value");
      return FALSE;
    }

  g_value_set_uint (value, u);
  return TRUE;
}

static void
uint_value_print (const GValue *value,
                  GString      *string)
{
  g_string_append_printf (string, "%u", g_value_get_uint (value));
}

static gboolean 
double_value_parse (GtkCssParser *parser,
                    GValue       *value)
{
  gdouble d;

  if (!_gtk_css_parser_try_double (parser, &d))
    {
      _gtk_css_parser_error (parser, "Expected a number");
      return FALSE;
    }

  g_value_set_double (value, d);
  return TRUE;
}

static void
double_value_print (const GValue *value,
                    GString      *string)
{
  string_append_double (string, g_value_get_double (value));
}

static gboolean 
float_value_parse (GtkCssParser *parser,
                   GValue       *value)
{
  gdouble d;

  if (!_gtk_css_parser_try_double (parser, &d))
    {
      _gtk_css_parser_error (parser, "Expected a number");
      return FALSE;
    }

  g_value_set_float (value, d);
  return TRUE;
}

static void
float_value_print (const GValue *value,
                   GString      *string)
{
  string_append_double (string, g_value_get_float (value));
}

static gboolean 
string_value_parse (GtkCssParser *parser,
                    GValue       *value)
{
  char *str = _gtk_css_parser_read_string (parser);

  if (str == NULL)
    return FALSE;

  g_value_take_string (value, str);
  return TRUE;
}

static void
string_value_print (const GValue *value,
                    GString      *str)
{
  string_append_string (str, g_value_get_string (value));
}

static gboolean 
theming_engine_value_parse (GtkCssParser *parser,
                            GValue       *value)
{
  GtkThemingEngine *engine;
  char *str;

Benjamin Otte's avatar
Benjamin Otte committed
561 562
G_GNUC_BEGIN_IGNORE_DEPRECATIONS

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
  if (_gtk_css_parser_try (parser, "none", TRUE))
    {
      g_value_set_object (value, gtk_theming_engine_load (NULL));
      return TRUE;
    }

  str = _gtk_css_parser_try_ident (parser, TRUE);
  if (str == NULL)
    {
      _gtk_css_parser_error (parser, "Expected a valid theme name");
      return FALSE;
    }

  engine = gtk_theming_engine_load (str);

  if (engine == NULL)
    {
Matthias Clasen's avatar
Matthias Clasen committed
580
      _gtk_css_parser_error (parser, "Theming engine '%s' not found", str);
581 582 583 584 585 586 587
      g_free (str);
      return FALSE;
    }

  g_value_set_object (value, engine);
  g_free (str);
  return TRUE;
Benjamin Otte's avatar
Benjamin Otte committed
588 589

G_GNUC_END_IGNORE_DEPRECATIONS
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
}

static void
theming_engine_value_print (const GValue *value,
                            GString      *string)
{
  GtkThemingEngine *engine;
  char *name;

  engine = g_value_get_object (value);
  if (engine == NULL)
    g_string_append (string, "none");
  else
    {
      /* XXX: gtk_theming_engine_get_name()? */
      g_object_get (engine, "name", &name, NULL);
      g_string_append (string, name ? name : "none");
      g_free (name);
    }
}

static gboolean 
border_value_parse (GtkCssParser *parser,
                    GValue       *value)
{
  GtkBorder border = { 0, };
616 617
  guint i;
  int numbers[4];
618 619 620

  for (i = 0; i < G_N_ELEMENTS (numbers); i++)
    {
621 622 623 624 625 626 627 628 629 630 631 632 633
      if (_gtk_css_parser_has_prefix (parser, "-gtk"))
        {
          GtkCssValue *cssvalue = gtk_css_win32_size_value_parse (parser, GTK_CSS_PARSE_NUMBER | GTK_CSS_NUMBER_AS_PIXELS);

          if (cssvalue)
            {
              numbers[i] = _gtk_css_number_value_get (cssvalue, 100);
              _gtk_css_value_unref (cssvalue);
              return TRUE;
            }

          return FALSE;
        }
634
      else
635 636 637 638
        {
          if (!_gtk_css_parser_try_length (parser, &numbers[i]))
            break;
        }
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
    }

  if (i == 0)
    {
      _gtk_css_parser_error (parser, "Expected valid border");
      return FALSE;
    }

  border.top = numbers[0];
  if (i > 1)
    border.right = numbers[1];
  else
    border.right = border.top;
  if (i > 2)
    border.bottom = numbers[2];
  else
    border.bottom = border.top;
  if (i > 3)
    border.left = numbers[3];
  else
    border.left = border.right;

  g_value_set_boxed (value, &border);
  return TRUE;
}

static void
border_value_print (const GValue *value, GString *string)
{
  const GtkBorder *border = g_value_get_boxed (value);

  if (border == NULL)
    g_string_append (string, "none");
  else if (border->left != border->right)
    g_string_append_printf (string, "%d %d %d %d", border->top, border->right, border->bottom, border->left);
  else if (border->top != border->bottom)
    g_string_append_printf (string, "%d %d %d", border->top, border->right, border->bottom);
  else if (border->top != border->left)
    g_string_append_printf (string, "%d %d", border->top, border->right);
  else
    g_string_append_printf (string, "%d", border->top);
}

static gboolean 
gradient_value_parse (GtkCssParser *parser,
                      GValue       *value)
{
  GtkGradient *gradient;

688 689 690
  gradient = _gtk_gradient_parse (parser);
  if (gradient == NULL)
    return FALSE;
691 692 693 694 695 696 697 698 699 700 701

  g_value_take_boxed (value, gradient);
  return TRUE;
}

static void
gradient_value_print (const GValue *value,
                      GString      *string)
{
  GtkGradient *gradient = g_value_get_boxed (value);

Benjamin Otte's avatar
Benjamin Otte committed
702 703
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

704 705 706 707 708 709 710 711
  if (gradient == NULL)
    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
712 713

  G_GNUC_END_IGNORE_DEPRECATIONS;
714 715 716 717 718 719 720 721 722 723 724 725 726
}

static gboolean 
pattern_value_parse (GtkCssParser *parser,
                     GValue       *value)
{
  if (_gtk_css_parser_try (parser, "none", TRUE))
    {
      /* nothing to do here */
    }
  else if (_gtk_css_parser_begins_with (parser, '-'))
    {
      g_value_unset (value);
727 728 729

      G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

730
      g_value_init (value, GTK_TYPE_GRADIENT);
731
      return gradient_value_parse (parser, value);
732 733

      G_GNUC_END_IGNORE_DEPRECATIONS;
734 735 736 737 738 739 740 741 742 743 744
    }
  else
    {
      GError *error = NULL;
      gchar *path;
      GdkPixbuf *pixbuf;
      GFile *file;
      cairo_surface_t *surface;
      cairo_pattern_t *pattern;
      cairo_matrix_t matrix;

745
      file = _gtk_css_parser_read_url (parser);
746 747 748 749 750 751 752 753 754 755 756 757 758 759
      if (file == NULL)
        return FALSE;

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

      pixbuf = gdk_pixbuf_new_from_file (path, &error);
      g_free (path);
      if (pixbuf == NULL)
        {
          _gtk_css_parser_take_error (parser, error);
          return FALSE;
        }

760
      surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
761
      pattern = cairo_pattern_create_for_surface (surface);
762
      cairo_surface_destroy (surface);
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 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

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

      g_object_unref (pixbuf);

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

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_LINEAR:
    case CAIRO_PATTERN_TYPE_RADIAL:
836 837 838
      g_string_append (string, "none /* FIXME: add support for printing gradients */");
      break;
    case CAIRO_PATTERN_TYPE_SOLID:
839 840 841 842 843 844
    default:
      g_assert_not_reached ();
      break;
    }
}

845
static GtkCssValue *
846
pattern_value_compute (GtkStyleProviderPrivate *provider,
847 848
                       GtkCssStyle             *values,
                       GtkCssStyle             *parent_values,
849
                       GtkCssValue             *specified)
Benjamin Otte's avatar
Benjamin Otte committed
850
{
851 852
  const GValue *value = _gtk_css_typed_value_get (specified);

853 854
  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

855
  if (G_VALUE_HOLDS (value, GTK_TYPE_GRADIENT))
Benjamin Otte's avatar
Benjamin Otte committed
856
    {
857
      GValue new_value = G_VALUE_INIT;
Benjamin Otte's avatar
Benjamin Otte committed
858
      cairo_pattern_t *gradient;
859

860
      gradient = _gtk_gradient_resolve_full (g_value_get_boxed (value), provider, values, parent_values);
Benjamin Otte's avatar
Benjamin Otte committed
861

862 863 864
      g_value_init (&new_value, CAIRO_GOBJECT_TYPE_PATTERN);
      g_value_take_boxed (&new_value, gradient);
      return _gtk_css_typed_value_new_take (&new_value);
Benjamin Otte's avatar
Benjamin Otte committed
865 866
    }
  else
867
    return _gtk_css_value_ref (specified);
868 869

  G_GNUC_END_IGNORE_DEPRECATIONS;
Benjamin Otte's avatar
Benjamin Otte committed
870 871
}

872 873 874 875 876 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 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
static gboolean 
enum_value_parse (GtkCssParser *parser,
                  GValue       *value)
{
  int v;

  if (enum_parse (parser, G_VALUE_TYPE (value), &v))
    {
      g_value_set_enum (value, v);
      return TRUE;
    }

  return FALSE;
}

static void
enum_value_print (const GValue *value,
                  GString      *string)
{
  enum_print (g_value_get_enum (value), G_VALUE_TYPE (value), string);
}

static gboolean 
flags_value_parse (GtkCssParser *parser,
                   GValue       *value)
{
  GFlagsClass *flags_class;
  GFlagsValue *flag_value;
  guint flags = 0;
  char *str;

  flags_class = g_type_class_ref (G_VALUE_TYPE (value));

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

      flag_value = g_flags_get_value_by_nick (flags_class, str);
      if (!flag_value)
        {
          _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);
          g_type_class_unref (flags_class);
          return FALSE;
        }

      g_free (str);
    }
  while (_gtk_css_parser_try (parser, ",", FALSE));

  g_type_class_unref (flags_class);

  g_value_set_enum (value, flags);

  return TRUE;
}

static void
flags_value_print (const GValue *value,
                   GString      *string)
{
  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);
}

/*** API ***/

static void
gtk_css_style_funcs_init (void)
{
  if (G_LIKELY (parse_funcs != NULL))
    return;

  parse_funcs = g_hash_table_new (NULL, NULL);
  print_funcs = g_hash_table_new (NULL, NULL);
Benjamin Otte's avatar
Benjamin Otte committed
975
  compute_funcs = g_hash_table_new (NULL, NULL);
976 977 978

  register_conversion_function (GDK_TYPE_RGBA,
                                rgba_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
979 980
                                rgba_value_print,
                                rgba_value_compute);
Matthias Clasen's avatar
Matthias Clasen committed
981 982 983

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

984 985
  register_conversion_function (GDK_TYPE_COLOR,
                                color_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
986 987
                                color_value_print,
                                color_value_compute);
988

989 990
  register_conversion_function (GTK_TYPE_SYMBOLIC_COLOR,
                                symbolic_color_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
991 992
                                symbolic_color_value_print,
                                NULL);
993 994 995

  G_GNUC_END_IGNORE_DEPRECATIONS;

996 997
  register_conversion_function (PANGO_TYPE_FONT_DESCRIPTION,
                                font_description_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
998 999
                                font_description_value_print,
                                NULL);
1000 1001
  register_conversion_function (G_TYPE_BOOLEAN,
                                boolean_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1002 1003
                                boolean_value_print,
                                NULL);
1004 1005
  register_conversion_function (G_TYPE_INT,
                                int_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1006 1007
                                int_value_print,
                                NULL);
1008 1009
  register_conversion_function (G_TYPE_UINT,
                                uint_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1010 1011
                                uint_value_print,
                                NULL);
1012 1013
  register_conversion_function (G_TYPE_DOUBLE,
                                double_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1014 1015
                                double_value_print,
                                NULL);
1016 1017
  register_conversion_function (G_TYPE_FLOAT,
                                float_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1018 1019
                                float_value_print,
                                NULL);
1020 1021
  register_conversion_function (G_TYPE_STRING,
                                string_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1022 1023
                                string_value_print,
                                NULL);
Benjamin Otte's avatar
Benjamin Otte committed
1024 1025 1026

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS

1027 1028
  register_conversion_function (GTK_TYPE_THEMING_ENGINE,
                                theming_engine_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1029 1030
                                theming_engine_value_print,
                                NULL);
Benjamin Otte's avatar
Benjamin Otte committed
1031 1032 1033

  G_GNUC_END_IGNORE_DEPRECATIONS

1034 1035
  register_conversion_function (GTK_TYPE_BORDER,
                                border_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1036 1037
                                border_value_print,
                                NULL);
1038 1039 1040

  G_GNUC_BEGIN_IGNORE_DEPRECATIONS;

1041 1042
  register_conversion_function (GTK_TYPE_GRADIENT,
                                gradient_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1043 1044
                                gradient_value_print,
                                NULL);
1045 1046 1047

  G_GNUC_END_IGNORE_DEPRECATIONS;

1048 1049
  register_conversion_function (CAIRO_GOBJECT_TYPE_PATTERN,
                                pattern_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1050 1051
                                pattern_value_print,
                                pattern_value_compute);
1052 1053
  register_conversion_function (G_TYPE_ENUM,
                                enum_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1054 1055
                                enum_value_print,
                                NULL);
1056 1057
  register_conversion_function (G_TYPE_FLAGS,
                                flags_value_parse,
Benjamin Otte's avatar
Benjamin Otte committed
1058 1059
                                flags_value_print,
                                NULL);
1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
}

/**
 * _gtk_css_style_parse_value:
 * @value: the value to parse into. Must be a valid initialized #GValue
 * @parser: the parser to parse from
 *
 * This is the generic parsing function used for CSS values. If the
 * function fails to parse a value, it will emit an error on @parser,
 * return %FALSE and not touch @value.
 *
 * Returns: %TRUE if parsing succeeded.
 **/
gboolean
1074 1075
_gtk_css_style_funcs_parse_value (GValue       *value,
                                  GtkCssParser *parser)
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
{
  GtkStyleParseFunc func;

  g_return_val_if_fail (value != NULL, FALSE);
  g_return_val_if_fail (parser != NULL, FALSE);

  gtk_css_style_funcs_init ();

  func = g_hash_table_lookup (parse_funcs,
                              GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
  if (func == NULL)
    func = g_hash_table_lookup (parse_funcs,
                                GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));

  if (func == NULL)
    {
      _gtk_css_parser_error (parser,
                             "Cannot convert to type '%s'",
                             g_type_name (G_VALUE_TYPE (value)));
      return FALSE;
    }

1098
  return (*func) (parser, value);
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
}

/**
 * _gtk_css_style_print_value:
 * @value: an initialized GValue returned from _gtk_css_style_parse()
 * @string: the string to print into
 *
 * Prints @value into @string as a CSS value. If @value is not a
 * valid value, a random string will be printed instead.
 **/
void
1110 1111
_gtk_css_style_funcs_print_value (const GValue *value,
                                  GString      *string)
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
{
  GtkStylePrintFunc func;

  gtk_css_style_funcs_init ();

  func = g_hash_table_lookup (print_funcs,
                              GSIZE_TO_POINTER (G_VALUE_TYPE (value)));
  if (func == NULL)
    func = g_hash_table_lookup (print_funcs,
                                GSIZE_TO_POINTER (g_type_fundamental (G_VALUE_TYPE (value))));

  if (func == NULL)
    {
      char *s = g_strdup_value_contents (value);
      g_string_append (string, s);
      g_free (s);
      return;
    }
  
  func (value, string);
}

Benjamin Otte's avatar
Benjamin Otte committed
1134 1135
/**
 * _gtk_css_style_compute_value:
1136 1137 1138
 * @provider: Style provider to look up information from
 * @values: The values to compute for
 * @parent_values: Values to look up inherited values from
1139
 * @target_type: Type the resulting value should have
Benjamin Otte's avatar
Benjamin Otte committed
1140 1141 1142 1143 1144 1145
 * @specified: the value to use for the computation
 *
 * Converts the @specified value into the @computed value using the
 * information in @context. The values must have matching types, ie
 * @specified must be a result of a call to
 * _gtk_css_style_parse_value() with the same type as @computed.
1146 1147
 *
 * Returns: the resulting value
Benjamin Otte's avatar
Benjamin Otte committed
1148
 **/
1149
GtkCssValue *
1150
_gtk_css_style_funcs_compute_value (GtkStyleProviderPrivate *provider,
1151 1152
                                    GtkCssStyle             *style,
                                    GtkCssStyle             *parent_style,
1153
                                    GType                    target_type,
1154
                                    GtkCssValue             *specified)
Benjamin Otte's avatar
Benjamin Otte committed
1155 1156 1157
{
  GtkStyleComputeFunc func;

1158
  g_return_val_if_fail (GTK_IS_STYLE_PROVIDER (provider), NULL);
1159 1160
  g_return_val_if_fail (GTK_IS_CSS_STYLE (style), NULL);
  g_return_val_if_fail (parent_style == NULL || GTK_IS_CSS_STYLE (parent_style), NULL);
Benjamin Otte's avatar
Benjamin Otte committed
1161 1162 1163 1164

  gtk_css_style_funcs_init ();

  func = g_hash_table_lookup (compute_funcs,
1165
                              GSIZE_TO_POINTER (target_type));
Benjamin Otte's avatar
Benjamin Otte committed
1166 1167
  if (func == NULL)
    func = g_hash_table_lookup (compute_funcs,
1168
                                GSIZE_TO_POINTER (g_type_fundamental (target_type)));
Benjamin Otte's avatar
Benjamin Otte committed
1169 1170

  if (func)
1171
    return func (provider, style, parent_style, specified);
Benjamin Otte's avatar
Benjamin Otte committed
1172
  else
1173
    return _gtk_css_value_ref (specified);
Benjamin Otte's avatar
Benjamin Otte committed
1174 1175
}