gtkcssshorthandpropertyimpl.c 41.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright © 2011 Red Hat Inc.
 *
 * 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.1 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
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtkcssshorthandpropertyprivate.h"

#include <cairo-gobject.h>
#include <math.h>

27
#include "gtkcssarrayvalueprivate.h"
28
#include "gtkcssbgsizevalueprivate.h"
29
#include "gtkcssbordervalueprivate.h"
30
#include "gtkcsscornervalueprivate.h"
31
#include "gtkcsseasevalueprivate.h"
32
#include "gtkcssenumvalueprivate.h"
33
#include "gtkcssimageprivate.h"
34
#include "gtkcssimagevalueprivate.h"
35
#include "gtkcssnumbervalueprivate.h"
36
#include "gtkcsspositionvalueprivate.h"
37
#include "gtkcssrepeatvalueprivate.h"
38
#include "gtkcssstringvalueprivate.h"
39
#include "gtkcssstylefuncsprivate.h"
40
#include "gtkcssvalueprivate.h"
41
#include "gtkstylepropertiesprivate.h"
42
#include "gtksymboliccolorprivate.h"
43
#include "gtktypebuiltins.h"
44 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"

/*** PARSING ***/

52 53 54 55
static gboolean
value_is_done_parsing (GtkCssParser *parser)
{
  return _gtk_css_parser_is_eof (parser) ||
56
         _gtk_css_parser_begins_with (parser, ',') ||
57 58 59 60
         _gtk_css_parser_begins_with (parser, ';') ||
         _gtk_css_parser_begins_with (parser, '}');
}

61
static gboolean
62 63 64 65
parse_four_numbers (GtkCssShorthandProperty  *shorthand,
                    GtkCssValue             **values,
                    GtkCssParser             *parser,
                    GtkCssNumberParseFlags    flags)
66 67 68 69 70 71 72 73
{
  guint i;

  for (i = 0; i < 4; i++)
    {
      if (!_gtk_css_parser_has_number (parser))
        break;

74 75
      values[i] = _gtk_css_number_value_parse (parser, flags);
      if (values[i] == NULL)
76 77 78 79 80 81 82 83 84 85 86
        return FALSE;
    }

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

  for (; i < 4; i++)
    {
87
      values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
88 89 90 91 92
    }

  return TRUE;
}

93
static gboolean
94 95
parse_margin (GtkCssShorthandProperty  *shorthand,
              GtkCssValue             **values,
96
              GtkCssParser             *parser)
97 98 99 100 101 102 103 104 105
{
  return parse_four_numbers (shorthand,
                             values,
                             parser,
                             GTK_CSS_NUMBER_AS_PIXELS
                             | GTK_CSS_PARSE_LENGTH);
}

static gboolean
106 107
parse_padding (GtkCssShorthandProperty  *shorthand,
               GtkCssValue             **values,
108
               GtkCssParser             *parser)
109 110 111 112 113 114 115 116 117 118
{
  return parse_four_numbers (shorthand,
                             values,
                             parser,
                             GTK_CSS_POSITIVE_ONLY
                             | GTK_CSS_NUMBER_AS_PIXELS
                             | GTK_CSS_PARSE_LENGTH);
}

static gboolean
119 120
parse_border_width (GtkCssShorthandProperty  *shorthand,
                    GtkCssValue             **values,
121
                    GtkCssParser             *parser)
122 123 124 125 126 127 128 129 130
{
  return parse_four_numbers (shorthand,
                             values,
                             parser,
                             GTK_CSS_POSITIVE_ONLY
                             | GTK_CSS_NUMBER_AS_PIXELS
                             | GTK_CSS_PARSE_LENGTH);
}

131
static gboolean 
132 133
parse_border_radius (GtkCssShorthandProperty  *shorthand,
                     GtkCssValue             **values,
134
                     GtkCssParser             *parser)
135
{
136
  GtkCssValue *x[4] = { NULL, }, *y[4] = { NULL, };
137 138
  guint i;

139
  for (i = 0; i < 4; i++)
140
    {
141
      if (!_gtk_css_parser_has_number (parser))
142
        break;
143 144 145 146 147 148 149
      x[i] = _gtk_css_number_value_parse (parser,
                                          GTK_CSS_POSITIVE_ONLY
                                          | GTK_CSS_PARSE_PERCENT
                                          | GTK_CSS_NUMBER_AS_PIXELS
                                          | GTK_CSS_PARSE_LENGTH);
      if (x[i] == NULL)
        goto fail;
150 151 152 153 154
    }

  if (i == 0)
    {
      _gtk_css_parser_error (parser, "Expected a number");
155
      goto fail;
156 157 158 159
    }

  /* The magic (i - 1) >> 1 below makes it take the correct value
   * according to spec. Feel free to check the 4 cases */
160 161
  for (; i < 4; i++)
    x[i] = _gtk_css_value_ref (x[(i - 1) >> 1]);
162 163 164

  if (_gtk_css_parser_try (parser, "/", TRUE))
    {
165
      for (i = 0; i < 4; i++)
166
        {
167
          if (!_gtk_css_parser_has_number (parser))
168
            break;
169 170 171 172 173 174 175
          y[i] = _gtk_css_number_value_parse (parser,
                                              GTK_CSS_POSITIVE_ONLY
                                              | GTK_CSS_PARSE_PERCENT
                                              | GTK_CSS_NUMBER_AS_PIXELS
                                              | GTK_CSS_PARSE_LENGTH);
          if (y[i] == NULL)
            goto fail;
176 177 178 179 180
        }

      if (i == 0)
        {
          _gtk_css_parser_error (parser, "Expected a number");
181
          goto fail;
182 183
        }

184 185
      for (; i < 4; i++)
        y[i] = _gtk_css_value_ref (y[(i - 1) >> 1]);
186 187 188
    }
  else
    {
189 190
      for (i = 0; i < 4; i++)
        y[i] = _gtk_css_value_ref (x[i]);
191 192
    }

193
  for (i = 0; i < 4; i++)
194
    {
195
      values[i] = _gtk_css_corner_value_new (x[i], y[i]);
196 197 198
    }

  return TRUE;
199 200 201 202 203 204 205 206 207 208

fail:
  for (i = 0; i < 4; i++)
    {
      if (x[i])
        _gtk_css_value_unref (x[i]);
      if (y[i])
        _gtk_css_value_unref (y[i]);
    }
  return FALSE;
209 210
}

211
static gboolean 
212 213
parse_border_color (GtkCssShorthandProperty  *shorthand,
                    GtkCssValue             **values,
214
                    GtkCssParser             *parser)
215 216 217 218 219
{
  guint i;

  for (i = 0; i < 4; i++)
    {
220 221
      values[i] = _gtk_css_symbolic_value_new (parser);
      if (values[i] == NULL)
222
        return FALSE;
223

224
      if (value_is_done_parsing (parser))
225 226 227 228 229
        break;
    }

  for (i++; i < 4; i++)
    {
230
      values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
231 232 233 234 235
    }

  return TRUE;
}

236
static gboolean
237 238
parse_border_style (GtkCssShorthandProperty  *shorthand,
                    GtkCssValue             **values,
239
                    GtkCssParser             *parser)
240 241 242 243 244
{
  guint i;

  for (i = 0; i < 4; i++)
    {
245 246
      values[i] = _gtk_css_border_style_value_try_parse (parser);
      if (values[i] == NULL)
247 248 249 250 251 252 253 254 255
        break;
    }

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

256 257
  for (; i < 4; i++)
    values[i] = _gtk_css_value_ref (values[(i - 1) >> 1]);
258 259 260 261

  return TRUE;
}

262
static gboolean
263 264
parse_border_image (GtkCssShorthandProperty  *shorthand,
                    GtkCssValue             **values,
265
                    GtkCssParser             *parser)
266
{
267
  do
268
    {
269 270 271 272 273
      if (values[0] == NULL &&
          (_gtk_css_parser_has_prefix (parser, "none") ||
           _gtk_css_image_can_parse (parser)))
        {
          GtkCssImage *image;
274

275 276 277 278
          if (_gtk_css_parser_try (parser, "none", TRUE))
            image = NULL;
          else
            {
279
              image = _gtk_css_image_new_parse (parser);
280 281 282
              if (image == NULL)
                return FALSE;
            }
283

284 285 286 287 288 289 290 291 292
          values[0] = _gtk_css_image_value_new (image);
        }
      else if (values[3] == NULL &&
               (values[3] = _gtk_css_border_repeat_value_try_parse (parser)))
        {
          /* please move along */
        }
      else if (values[1] == NULL)
        {
293 294 295 296 297 298 299
          values[1] = _gtk_css_border_value_parse (parser,
                                                   GTK_CSS_PARSE_PERCENT
                                                   | GTK_CSS_PARSE_NUMBER
                                                   | GTK_CSS_POSITIVE_ONLY,
                                                   FALSE,
                                                   TRUE);
          if (values[1] == NULL)
300
            return FALSE;
301

302 303
          if (_gtk_css_parser_try (parser, "/", TRUE))
            {
304 305 306 307 308 309 310 311
              values[2] = _gtk_css_border_value_parse (parser,
                                                       GTK_CSS_PARSE_PERCENT
                                                       | GTK_CSS_PARSE_LENGTH
                                                       | GTK_CSS_PARSE_NUMBER
                                                       | GTK_CSS_POSITIVE_ONLY,
                                                       TRUE,
                                                       FALSE);
              if (values[2] == NULL)
312 313 314 315 316 317 318 319 320 321 322 323
                return FALSE;
            }
        }
      else
        {
          /* We parsed everything and there's still stuff left?
           * Pretend we didn't notice and let the normal code produce
           * a 'junk at end of value' error */
          break;
        }
    }
  while (!value_is_done_parsing (parser));
324

325
  return TRUE;
326 327
}

328
static gboolean
329 330
parse_border_side (GtkCssShorthandProperty  *shorthand,
                   GtkCssValue             **values,
331
                   GtkCssParser             *parser)
332 333 334
{
  do
  {
335
    if (values[0] == NULL &&
336
         _gtk_css_parser_has_number (parser))
337
      {
338 339 340 341 342
        values[0] = _gtk_css_number_value_parse (parser,
                                                 GTK_CSS_POSITIVE_ONLY
                                                 | GTK_CSS_NUMBER_AS_PIXELS
                                                 | GTK_CSS_PARSE_LENGTH);
        if (values[0] == NULL)
343
          return FALSE;
344
      }
345
    else if (values[1] == NULL &&
346
             (values[1] = _gtk_css_border_style_value_try_parse (parser)))
347
      {
348
        /* Nothing to do */
349
      }
350
    else if (values[2] == NULL)
351
      {
352 353
        values[2] = _gtk_css_symbolic_value_new (parser);
        if (values[2] == NULL)
354 355 356 357 358 359 360 361
          return FALSE;
      }
  }
  while (!value_is_done_parsing (parser));

  return TRUE;
}

362
static gboolean
363 364
parse_border (GtkCssShorthandProperty  *shorthand,
              GtkCssValue             **values,
365
              GtkCssParser             *parser)
366 367 368
{
  do
  {
369
    if (values[0] == NULL &&
370
         _gtk_css_parser_has_number (parser))
371
      {
372 373 374 375 376
        values[0] = _gtk_css_number_value_parse (parser,
                                                 GTK_CSS_POSITIVE_ONLY
                                                 | GTK_CSS_NUMBER_AS_PIXELS
                                                 | GTK_CSS_PARSE_LENGTH);
        if (values[0] == NULL)
377
          return FALSE;
378 379 380
        values[1] = _gtk_css_value_ref (values[0]);
        values[2] = _gtk_css_value_ref (values[0]);
        values[3] = _gtk_css_value_ref (values[0]);
381
      }
382
    else if (values[4] == NULL &&
383
             (values[4] = _gtk_css_border_style_value_try_parse (parser)))
384
      {
385 386 387
        values[5] = _gtk_css_value_ref (values[4]);
        values[6] = _gtk_css_value_ref (values[4]);
        values[7] = _gtk_css_value_ref (values[4]);
388 389 390
      }
    else if (!G_IS_VALUE (&values[8]))
      {
391 392
        values[8] = _gtk_css_symbolic_value_new (parser);
        if (values[8] == NULL)
393 394
          return FALSE;

395 396 397
        values[9] = _gtk_css_value_ref (values[8]);
        values[10] = _gtk_css_value_ref (values[8]);
        values[11] = _gtk_css_value_ref (values[8]);
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
      }
    else
      {
        /* We parsed everything and there's still stuff left?
         * Pretend we didn't notice and let the normal code produce
         * a 'junk at end of value' error */
        break;
      }
  }
  while (!value_is_done_parsing (parser));

  /* Note that border-image values are not set: according to the spec
     they just need to be reset when using the border shorthand */

  return TRUE;
}

415
static gboolean
416 417
parse_font (GtkCssShorthandProperty  *shorthand,
            GtkCssValue             **values,
418
            GtkCssParser             *parser)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
{
  PangoFontDescription *desc;
  guint mask;
  char *str;

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

  desc = pango_font_description_from_string (str);
  g_free (str);

  mask = pango_font_description_get_set_fields (desc);

  if (mask & PANGO_FONT_MASK_FAMILY)
    {
435
      values[0] = _gtk_css_array_value_new (_gtk_css_string_value_new (pango_font_description_get_family (desc)));
436 437 438
    }
  if (mask & PANGO_FONT_MASK_STYLE)
    {
439
      values[1] = _gtk_css_font_style_value_new (pango_font_description_get_style (desc));
440 441 442
    }
  if (mask & PANGO_FONT_MASK_VARIANT)
    {
443
      values[2] = _gtk_css_font_variant_value_new (pango_font_description_get_variant (desc));
444 445 446
    }
  if (mask & PANGO_FONT_MASK_WEIGHT)
    {
447
      values[3] = _gtk_css_font_weight_value_new (pango_font_description_get_weight (desc));
448 449 450
    }
  if (mask & PANGO_FONT_MASK_SIZE)
    {
451
      values[4] = _gtk_css_number_value_new ((double) pango_font_description_get_size (desc) / PANGO_SCALE, GTK_CSS_PX);
452 453 454 455 456 457 458
    }

  pango_font_description_free (desc);

  return TRUE;
}

459
static gboolean
460 461 462
parse_one_background (GtkCssShorthandProperty  *shorthand,
                      GtkCssValue             **values,
                      GtkCssParser             *parser)
463
{
464
  GtkCssValue *value = NULL;
465

466 467 468
  do
    {
      /* the image part */
469
      if (values[0] == NULL &&
470 471 472 473 474 475 476 477 478
          (_gtk_css_parser_has_prefix (parser, "none") ||
           _gtk_css_image_can_parse (parser)))
        {
          GtkCssImage *image;

          if (_gtk_css_parser_try (parser, "none", TRUE))
            image = NULL;
          else
            {
479
              image = _gtk_css_image_new_parse (parser);
480 481 482 483
              if (image == NULL)
                return FALSE;
            }

484 485 486
          values[0] = _gtk_css_image_value_new (image);
        }
      else if (values[1] == NULL &&
Matthias Clasen's avatar
Matthias Clasen committed
487
               (value = _gtk_css_position_value_try_parse (parser)))
488 489 490 491 492 493 494 495 496 497
        {
          values[1] = value;
          value = NULL;

          if (_gtk_css_parser_try (parser, "/", TRUE) &&
              (value = _gtk_css_bg_size_value_parse (parser)))
            {
              values[2] = value;
              value = NULL;
            }
498
        }
499
      else if (values[3] == NULL &&
500
               (value = _gtk_css_background_repeat_value_try_parse (parser)))
501
        {
502
          values[3] = value;
503
          value = NULL;
504
        }
505
      else if ((values[4] == NULL || values[5] == NULL) &&
506
               (value = _gtk_css_area_value_try_parse (parser)))
507
        {
508
          values[4] = value;
509

510
          if (values[5] == NULL)
511
            {
512 513
              values[5] = values[4];
              values[4] = NULL;
514
            }
515
          value = NULL;
516
        }
517
      else if (values[6] == NULL)
518
        {
519 520
          value = _gtk_css_symbolic_value_new (parser);
          if (value == NULL)
521 522 523 524
            values[6] = _gtk_css_value_ref (_gtk_css_style_property_get_initial_value 
                                            (_gtk_css_shorthand_property_get_subproperty (shorthand, 6)));
          else
            values[6] = value;
525

526 527
          value = NULL;
        }
528 529 530 531 532 533 534 535 536 537
      else
        {
          /* We parsed everything and there's still stuff left?
           * Pretend we didn't notice and let the normal code produce
           * a 'junk at end of value' error */
          break;
        }
    }
  while (!value_is_done_parsing (parser));

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  if (values[5] != NULL && values[4] == NULL)
    values[4] = _gtk_css_value_ref (values[5]);

  return TRUE;
}

static gboolean
parse_background (GtkCssShorthandProperty  *shorthand,
                  GtkCssValue             **values,
                  GtkCssParser             *parser)
{
  GtkCssValue *step_values[7];
  GPtrArray *arrays[6];
  guint i;

  for (i = 0; i < 6; i++)
    {
      arrays[i] = g_ptr_array_new ();
      step_values[i] = NULL;
    }
  
  step_values[6] = NULL;

  do {
    if (!parse_one_background (shorthand, step_values, parser))
      {
        for (i = 0; i < 6; i++)
          {
            g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
            g_ptr_array_unref (arrays[i]);
            return FALSE;
          }
      }

      for (i = 0; i < 6; i++)
        {
          if (step_values[i] == NULL)
            {
              GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
                                         _gtk_css_shorthand_property_get_subproperty (shorthand, i));
              step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
            }

          g_ptr_array_add (arrays[i], step_values[i]);
          step_values[i] = NULL;
        }
  } while (_gtk_css_parser_try (parser, ",", TRUE));

  for (i = 0; i < 6; i++)
    {
      values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
      g_ptr_array_unref (arrays[i]);
    }

  values[6] = step_values[6];

594 595 596
  return TRUE;
}

597 598 599
static gboolean
parse_one_transition (GtkCssShorthandProperty  *shorthand,
                      GtkCssValue             **values,
600
                      GtkCssParser             *parser)
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
{
  do
    {
      /* the image part */
      if (values[2] == NULL &&
          _gtk_css_parser_has_number (parser) && !_gtk_css_parser_begins_with (parser, '-'))
        {
          GtkCssValue *number = _gtk_css_number_value_parse (parser, GTK_CSS_PARSE_TIME);

          if (number == NULL)
            return FALSE;

          if (values[1] == NULL)
            values[1] = number;
          else
            values[2] = number;
        }
      else if (values[3] == NULL &&
               _gtk_css_ease_value_can_parse (parser))
        {
          values[3] = _gtk_css_ease_value_parse (parser);

          if (values[3] == NULL)
            return FALSE;
        }
      else if (values[0] == NULL)
        {
          values[0] = _gtk_css_ident_value_try_parse (parser);
          if (values[0] == NULL)
            {
              _gtk_css_parser_error (parser, "Unknown value for property");
              return FALSE;
            }

        }
      else
        {
          /* We parsed everything and there's still stuff left?
           * Pretend we didn't notice and let the normal code produce
           * a 'junk at end of value' error */
          break;
        }
    }
  while (!value_is_done_parsing (parser));

  return TRUE;
}

static gboolean
parse_transition (GtkCssShorthandProperty  *shorthand,
                  GtkCssValue             **values,
652
                  GtkCssParser             *parser)
653 654 655 656 657 658 659 660 661 662 663 664
{
  GtkCssValue *step_values[4];
  GPtrArray *arrays[4];
  guint i;

  for (i = 0; i < 4; i++)
    {
      arrays[i] = g_ptr_array_new ();
      step_values[i] = NULL;
    }

  do {
665
    if (!parse_one_transition (shorthand, step_values, parser))
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
      {
        for (i = 0; i < 4; i++)
          {
            g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
            g_ptr_array_unref (arrays[i]);
            return FALSE;
          }
      }

      for (i = 0; i < 4; i++)
        {
          if (step_values[i] == NULL)
            {
              GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
                                         _gtk_css_shorthand_property_get_subproperty (shorthand, i));
              step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
            }

          g_ptr_array_add (arrays[i], step_values[i]);
          step_values[i] = NULL;
        }
  } while (_gtk_css_parser_try (parser, ",", TRUE));

  for (i = 0; i < 4; i++)
    {
      values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
      g_ptr_array_unref (arrays[i]);
    }

  return TRUE;
}

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 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
static gboolean
parse_one_animation (GtkCssShorthandProperty  *shorthand,
                     GtkCssValue             **values,
                     GtkCssParser             *parser)
{
  do
    {
      if (values[1] == NULL && _gtk_css_parser_try (parser, "infinite", TRUE))
        {
          values[1] = _gtk_css_number_value_new (HUGE_VAL, GTK_CSS_NUMBER);
        }
      else if ((values[1] == NULL || values[3] == NULL) &&
          _gtk_css_parser_has_number (parser))
        {
          GtkCssValue *value;
          
          value = _gtk_css_number_value_parse (parser,
                                               GTK_CSS_POSITIVE_ONLY
                                               | (values[1] == NULL ? GTK_CSS_PARSE_NUMBER : 0)
                                               | (values[3] == NULL ? GTK_CSS_PARSE_TIME : 0));
          if (_gtk_css_number_value_get_unit (value) == GTK_CSS_NUMBER)
            values[1] = value;
          else if (values[2] == NULL)
            values[2] = value;
          else
            values[3] = value;
        }
      else if (values[4] == NULL &&
               _gtk_css_ease_value_can_parse (parser))
        {
          values[4] = _gtk_css_ease_value_parse (parser);

          if (values[4] == NULL)
            return FALSE;
        }
      else if (values[5] == NULL &&
               (values[5] = _gtk_css_direction_value_try_parse (parser)))
        {
          /* nothing to do */
        }
      else if (values[6] == NULL &&
               (values[6] = _gtk_css_fill_mode_value_try_parse (parser)))
        {
          /* nothing to do */
        }
      else if (values[0] == NULL &&
               (values[0] = _gtk_css_ident_value_try_parse (parser)))
        {
          /* nothing to do */
          /* keep in mind though that this needs to come last as fill modes, directions
           * etc are valid idents */
        }
      else
        {
          /* We parsed everything and there's still stuff left?
           * Pretend we didn't notice and let the normal code produce
           * a 'junk at end of value' error */
          break;
        }
    }
  while (!value_is_done_parsing (parser));

  return TRUE;
}

static gboolean
parse_animation (GtkCssShorthandProperty  *shorthand,
                 GtkCssValue             **values,
                 GtkCssParser             *parser)
{
  GtkCssValue *step_values[7];
  GPtrArray *arrays[6];
  guint i;

  for (i = 0; i < 6; i++)
    {
      arrays[i] = g_ptr_array_new ();
      step_values[i] = NULL;
    }
  
  step_values[6] = NULL;

  do {
    if (!parse_one_animation (shorthand, step_values, parser))
      {
        for (i = 0; i < 6; i++)
          {
            g_ptr_array_set_free_func (arrays[i], (GDestroyNotify) _gtk_css_value_unref);
            g_ptr_array_unref (arrays[i]);
            return FALSE;
          }
      }

      for (i = 0; i < 6; i++)
        {
          if (step_values[i] == NULL)
            {
              GtkCssValue *initial = _gtk_css_style_property_get_initial_value (
                                         _gtk_css_shorthand_property_get_subproperty (shorthand, i));
              step_values[i] = _gtk_css_value_ref (_gtk_css_array_value_get_nth (initial, 0));
            }

          g_ptr_array_add (arrays[i], step_values[i]);
          step_values[i] = NULL;
        }
  } while (_gtk_css_parser_try (parser, ",", TRUE));

  for (i = 0; i < 6; i++)
    {
      values[i] = _gtk_css_array_value_new_from_array ((GtkCssValue **) arrays[i]->pdata, arrays[i]->len);
      g_ptr_array_unref (arrays[i]);
    }

  values[6] = step_values[6];

  return TRUE;
}

816 817
/*** PACKING ***/

818
static void
819
unpack_border (GtkCssShorthandProperty *shorthand,
820 821 822
               GtkStyleProperties      *props,
               GtkStateFlags            state,
               const GValue            *value)
823
{
824
  GValue v = G_VALUE_INIT;
825 826
  GtkBorder *border = g_value_get_boxed (value);

827 828 829 830 831 832 833 834 835 836 837 838
  g_value_init (&v, G_TYPE_INT);

  g_value_set_int (&v, border->top);
  _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 0)), props, state, &v);
  g_value_set_int (&v, border->right);
  _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 1)), props, state, &v);
  g_value_set_int (&v, border->bottom);
  _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 2)), props, state, &v);
  g_value_set_int (&v, border->left);
  _gtk_style_property_assign (GTK_STYLE_PROPERTY (_gtk_css_shorthand_property_get_subproperty (shorthand, 3)), props, state, &v);

  g_value_unset (&v);
839 840
}

841
static void
842
pack_border (GtkCssShorthandProperty *shorthand,
843
             GValue                  *value,
844 845
             GtkStyleQueryFunc        query_func,
             gpointer                 query_data)
846
{
847
  GtkCssStyleProperty *prop;
848
  GtkBorder border;
849
  GValue v;
850 851

  prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
852 853 854 855
  _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
  border.top = g_value_get_int (&v);
  g_value_unset (&v);

856
  prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 1);
857 858 859 860
  _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
  border.right = g_value_get_int (&v);
  g_value_unset (&v);

861
  prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 2);
862 863 864 865
  _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
  border.bottom = g_value_get_int (&v);
  g_value_unset (&v);

866
  prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 3);
867 868 869
  _gtk_style_property_query (GTK_STYLE_PROPERTY (prop), &v, query_func, query_data);
  border.left = g_value_get_int (&v);
  g_value_unset (&v);
870

871 872
  g_value_init (value, GTK_TYPE_BORDER);
  g_value_set_boxed (value, &border);
873 874
}

875
static void
876
unpack_border_radius (GtkCssShorthandProperty *shorthand,
877 878 879
                      GtkStyleProperties      *props,
                      GtkStateFlags            state,
                      const GValue            *value)
880
{
881
  GtkCssValue *css_value;
882
  guint i;
883
  
884 885
  css_value = _gtk_css_corner_value_new (_gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX),
                                         _gtk_css_number_value_new (g_value_get_int (value), GTK_CSS_PX));
886

887
  for (i = 0; i < 4; i++)
888 889 890 891
    _gtk_style_properties_set_property_by_property (props,
                                                    _gtk_css_shorthand_property_get_subproperty (shorthand, i),
                                                    state,
                                                    css_value);
892

893
  _gtk_css_value_unref (css_value);
894 895
}

896
static void
897
pack_border_radius (GtkCssShorthandProperty *shorthand,
898
                    GValue                  *value,
899 900
                    GtkStyleQueryFunc        query_func,
                    gpointer                 query_data)
901
{
902
  GtkCssStyleProperty *prop;
903
  GtkCssValue *v;
904
  int i = 0;
905

906 907 908
  prop = GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("border-top-left-radius"));
  v = (* query_func) (_gtk_css_style_property_get_id (prop), query_data);
  if (v)
909
    i = _gtk_css_corner_value_get_x (v, 100);
910

911 912
  g_value_init (value, G_TYPE_INT);
  g_value_set_int (value, i);
913 914
}

915
static void
916
unpack_font_description (GtkCssShorthandProperty *shorthand,
917 918 919
                         GtkStyleProperties      *props,
                         GtkStateFlags            state,
                         const GValue            *value)
920
{
921
  GtkStyleProperty *prop;
922 923
  PangoFontDescription *description;
  PangoFontMask mask;
924
  GValue v = G_VALUE_INIT;
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
  
  /* For backwards compat, we only unpack values that are indeed set.
   * For strict CSS conformance we need to unpack all of them.
   * Note that we do set all of them in the parse function, so it
   * will not have effects when parsing CSS files. It will though
   * for custom style providers.
   */

  description = g_value_get_boxed (value);

  if (description)
    mask = pango_font_description_get_set_fields (description);
  else
    mask = 0;

  if (mask & PANGO_FONT_MASK_FAMILY)
    {
      GPtrArray *strv = g_ptr_array_new ();

      g_ptr_array_add (strv, g_strdup (pango_font_description_get_family (description)));
      g_ptr_array_add (strv, NULL);
946 947 948 949 950 951
      g_value_init (&v, G_TYPE_STRV);
      g_value_take_boxed (&v, g_ptr_array_free (strv, FALSE));

      prop = _gtk_style_property_lookup ("font-family");
      _gtk_style_property_assign (prop, props, state, &v);
      g_value_unset (&v);
952 953 954 955
    }

  if (mask & PANGO_FONT_MASK_STYLE)
    {
956 957 958 959 960 961
      g_value_init (&v, PANGO_TYPE_STYLE);
      g_value_set_enum (&v, pango_font_description_get_style (description));

      prop = _gtk_style_property_lookup ("font-style");
      _gtk_style_property_assign (prop, props, state, &v);
      g_value_unset (&v);
962 963 964 965
    }

  if (mask & PANGO_FONT_MASK_VARIANT)
    {
966 967 968 969 970 971
      g_value_init (&v, PANGO_TYPE_VARIANT);
      g_value_set_enum (&v, pango_font_description_get_variant (description));

      prop = _gtk_style_property_lookup ("font-variant");
      _gtk_style_property_assign (prop, props, state, &v);
      g_value_unset (&v);
972 973 974 975
    }

  if (mask & PANGO_FONT_MASK_WEIGHT)
    {
976 977 978 979 980 981
      g_value_init (&v, PANGO_TYPE_WEIGHT);
      g_value_set_enum (&v, pango_font_description_get_weight (description));

      prop = _gtk_style_property_lookup ("font-weight");
      _gtk_style_property_assign (prop, props, state, &v);
      g_value_unset (&v);
982 983 984 985
    }

  if (mask & PANGO_FONT_MASK_SIZE)
    {
986 987
      g_value_init (&v, G_TYPE_DOUBLE);
      g_value_set_double (&v, (double) pango_font_description_get_size (description) / PANGO_SCALE);
988

989 990 991 992
      prop = _gtk_style_property_lookup ("font-size");
      _gtk_style_property_assign (prop, props, state, &v);
      g_value_unset (&v);
    }
993 994
}

995
static void
996
pack_font_description (GtkCssShorthandProperty *shorthand,
997
                       GValue                  *value,
998 999
                       GtkStyleQueryFunc        query_func,
                       gpointer                 query_data)
1000 1001
{
  PangoFontDescription *description;
1002
  GtkCssValue *v;
1003 1004 1005

  description = pango_font_description_new ();

1006 1007 1008 1009
  v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-family"))), query_data);
  if (v)
    {
      /* xxx: Can we set all the families here somehow? */
1010
      pango_font_description_set_family (description, _gtk_css_string_value_get (_gtk_css_array_value_get_nth (v, 0)));
1011 1012 1013 1014
    }

  v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-size"))), query_data);
  if (v)
1015
    pango_font_description_set_size (description, round (_gtk_css_number_value_get (v, 100) * PANGO_SCALE));
1016 1017 1018

  v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-style"))), query_data);
  if (v)
1019
    pango_font_description_set_style (description, _gtk_css_font_style_value_get (v));
1020 1021 1022

  v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-variant"))), query_data);
  if (v)
1023
    pango_font_description_set_variant (description, _gtk_css_font_variant_value_get (v));
1024 1025 1026

  v = (* query_func) (_gtk_css_style_property_get_id (GTK_CSS_STYLE_PROPERTY (_gtk_style_property_lookup ("font-weight"))), query_data);
  if (v)
1027
    pango_font_description_set_weight (description, _gtk_css_font_weight_value_get (v));
1028

1029 1030
  g_value_init (value, PANGO_TYPE_FONT_DESCRIPTION);
  g_value_take_boxed (value, description);
1031 1032
}

1033
static void
1034
unpack_to_everything (GtkCssShorthandProperty *shorthand,
1035 1036 1037
                      GtkStyleProperties      *props,
                      GtkStateFlags            state,
                      const GValue            *value)
1038
{
1039 1040
  GtkCssStyleProperty *prop;
  guint i, n;
1041
  
1042
  n = _gtk_css_shorthand_property_get_n_subproperties (shorthand);
1043

1044 1045 1046
  for (i = 0; i < n; i++)
    {
      prop = _gtk_css_shorthand_property_get_subproperty (shorthand, i);
1047
      _gtk_style_property_assign (GTK_STYLE_PROPERTY (prop), props, state, value);
1048
    }
1049 1050
}

1051
static void
1052
pack_first_element (GtkCssShorthandProperty *shorthand,
1053
                    GValue                  *value,
1054 1055
                    GtkStyleQueryFunc        query_func,
                    gpointer                 query_data)
1056
{
1057 1058 1059 1060 1061 1062
  GtkCssStyleProperty *prop;

  /* NB: This is a fallback for properties that originally were
   * not used as shorthand. We just pick the first subproperty
   * as a representative.
   * Lesson learned: Don't query the shorthand, query the 
1063
   * real properties instead. */
1064 1065 1066 1067 1068
  prop = _gtk_css_shorthand_property_get_subproperty (shorthand, 0);
  _gtk_style_property_query (GTK_STYLE_PROPERTY (prop),
                             value,
                             query_func,
                             query_data);
1069 1070
}

1071
static void
1072 1073 1074 1075
_gtk_css_shorthand_property_register (const char                        *name,
                                      GType                              value_type,
                                      const char                       **subproperties,
                                      GtkCssShorthandPropertyParseFunc   parse_func,
1076 1077
                                      GtkCssShorthandPropertyAssignFunc  assign_func,
                                      GtkCssShorthandPropertyQueryFunc   query_func)
1078
{
1079
  GtkCssShorthandProperty *node;
1080 1081

  node = g_object_new (GTK_TYPE_CSS_SHORTHAND_PROPERTY,
1082 1083
                       "name", name,
                       "value-type", value_type,
1084
                       "subproperties", subproperties,
1085 1086
                       NULL);

1087 1088 1089
  node->parse = parse_func;
  node->assign = assign_func;
  node->query = query_func;
1090 1091
}

1092 1093 1094
void
_gtk_css_shorthand_property_init_properties (void)
{
1095
  /* The order is important here, be careful when changing it */
1096 1097 1098 1099 1100 1101 1102
  const char *font_subproperties[] = { "font-family", "font-style", "font-variant", "font-weight", "font-size", NULL };
  const char *margin_subproperties[] = { "margin-top", "margin-right", "margin-bottom", "margin-left", NULL };
  const char *padding_subproperties[] = { "padding-top", "padding-right", "padding-bottom", "padding-left", NULL };
  const char *border_width_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width", NULL };
  const char *border_radius_subproperties[] = { "border-top-left-radius", "border-top-right-radius",
                                                "border-bottom-right-radius", "border-bottom-left-radius", NULL };
  const char *border_color_subproperties[] = { "border-top-color", "border-right-color", "border-bottom-color", "border-left-color", NULL };
1103
  const char *border_style_subproperties[] = { "border-top-style", "border-right-style", "border-bottom-style", "border-left-style", NULL };
1104
  const char *border_image_subproperties[] = { "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
1105 1106 1107 1108
  const char *border_top_subproperties[] = { "border-top-width", "border-top-style", "border-top-color", NULL };
  const char *border_right_subproperties[] = { "border-right-width", "border-right-style", "border-right-color", NULL };
  const char *border_bottom_subproperties[] = { "border-bottom-width", "border-bottom-style", "border-bottom-color", NULL };
  const char *border_left_subproperties[] = { "border-left-width", "border-left-style", "border-left-color", NULL };
1109 1110 1111 1112
  const char *border_subproperties[] = { "border-top-width", "border-right-width", "border-bottom-width", "border-left-width",
                                         "border-top-style", "border-right-style", "border-bottom-style", "border-left-style",
                                         "border-top-color", "border-right-color", "border-bottom-color", "border-left-color",
                                         "border-image-source", "border-image-slice", "border-image-width", "border-image-repeat", NULL };
1113
  const char *outline_subproperties[] = { "outline-width", "outline-style", "outline-color", NULL };
1114
  const char *background_subproperties[] = { "background-image", "background-position", "background-size", "background-repeat", "background-clip", "background-origin",
1115
                                             "background-color", NULL };
1116
  const char *transition_subproperties[] = { "transition-property", "transition-duration", "transition-delay", "transition-timing-function", NULL };
1117 1118
  const char *animation_subproperties[] = { "animation-name", "animation-iteration-count", "animation-duration", "animation-delay", 
                                            "animation-timing-function", "animation-direction", "animation-fill-mode", NULL };
1119

1120 1121
  _gtk_css_shorthand_property_register   ("font",
                                          PANGO_TYPE_FONT_DESCRIPTION,
1122
                                          font_subproperties,
1123
                                          parse_font,
1124
                                          unpack_font_description,
1125
                                          pack_font_description);
1126 1127
  _gtk_css_shorthand_property_register   ("margin",
                                          GTK_TYPE_BORDER,
1128
                                          margin_subproperties,
1129
                                          parse_margin,
1130
                                          unpack_border,
1131
                                          pack_border);
1132 1133
  _gtk_css_shorthand_property_register   ("padding",
                                          GTK_TYPE_BORDER,
1134
                                          padding_subproperties,
1135
                                          parse_padding,
1136
                                          unpack_border,
1137
                                          pack_border);
1138 1139
  _gtk_css_shorthand_property_register   ("border-width",
                                          GTK_TYPE_BORDER,
1140
                                          border_width_subproperties,
1141
                                          parse_border_width,
1142
                                          unpack_border,
1143
                                          pack_border);
1144 1145
  _gtk_css_shorthand_property_register   ("border-radius",
                                          G_TYPE_INT,
1146
                                          border_radius_subproperties,
1147
                                          parse_border_radius,
1148
                                          unpack_border_radius,
1149
                                          pack_border_radius);
1150 1151
  _gtk_css_shorthand_property_register   ("border-color",
                                          GDK_TYPE_RGBA,
1152
                                          border_color_subproperties,
1153
                                          parse_border_color,
1154
                                          unpack_to_everything,
1155
                                          pack_first_element);
1156 1157 1158 1159
  _gtk_css_shorthand_property_register   ("border-style",
                                          GTK_TYPE_BORDER_STYLE,
                                          border_style_subproperties,
                                          parse_border_style,
1160
                                          unpack_to_everything,
1161
                                          pack_first_element);
1162
  _gtk_css_shorthand_property_register   ("border-image",
1163
                                          G_TYPE_NONE,
1164
                                          border_image_subproperties,
1165
                                          parse_border_image,
1166 1167
                                          NULL,
                                          NULL);
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
  _gtk_css_shorthand_property_register   ("border-top",
                                          G_TYPE_NONE,
                                          border_top_subproperties,
                                          parse_border_side,
                                          NULL,
                                          NULL);
  _gtk_css_shorthand_property_register   ("border-right",
                                          G_TYPE_NONE,
                                          border_right_subproperties,
                                          parse_border_side,
                                          NULL,
                                          NULL);
  _gtk_css_shorthand_property_register   ("border-bottom",
                                          G_TYPE_NONE,
                                          border_bottom_subproperties,
                                          parse_border_side,
                                          NULL,
                                          NULL);
  _gtk_css_shorthand_property_register   ("border-left",
                                          G_TYPE_NONE,
                                          border_left_subproperties,
                                          parse_border_side,
                                          NULL,
                                          NULL);
1192 1193 1194 1195 1196 1197
  _gtk_css_shorthand_property_register   ("border",
                                          G_TYPE_NONE,
                                          border_subproperties,
                                          parse_border,
                                          NULL,
                                          NULL);
1198 1199 1200 1201 1202 1203
  _gtk_css_shorthand_property_register   ("outline",
                                          G_TYPE_NONE,
                                          outline_subproperties,
                                          parse_border_side,
                                          NULL,
                                          NULL);
1204 1205 1206 1207 1208 1209
  _gtk_css_shorthand_property_register   ("background",
                                          G_TYPE_NONE,
                                          background_subproperties,
                                          parse_background,
                                          NULL,
                                          NULL);
1210 1211 1212 1213 1214 1215
  _gtk_css_shorthand_property_register   ("transition",
                                          G_TYPE_NONE,
                                          transition_subproperties,
                                          parse_transition,
                                          NULL,
                                          NULL);
1216 1217 1218 1219 1220 1221
  _gtk_css_shorthand_property_register   ("animation",
                                          G_TYPE_NONE,
                                          animation_subproperties,
                                          parse_animation,
                                          NULL,
                                          NULL);
1222
}