st-theme-node.c 121 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 3 4 5 6 7 8 9
/*
 * st-theme-node.c: style information for one node in a tree of themed objects
 *
 * Copyright 2008-2010 Red Hat, Inc.
 * Copyright 2009 Steve Frécinaux
 * Copyright 2009, 2010 Florian Müllner
 * Copyright 2010 Adel Gadllah
 * Copyright 2010 Giovanni Campagna
10
 * Copyright 2011 Quentin "Sardem FF7" Glidic
11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program 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 program is distributed in the hope 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 program. If not, see <http://www.gnu.org/licenses/>.
 */
25 26 27 28

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

29
#include "st-settings.h"
30 31
#include "st-theme-private.h"
#include "st-theme-context.h"
32
#include "st-theme-node-private.h"
33

Colin Walters's avatar
Colin Walters committed
34
static void st_theme_node_dispose           (GObject                 *object);
35
static void st_theme_node_finalize          (GObject                 *object);
36 37 38

static const ClutterColor BLACK_COLOR = { 0, 0, 0, 0xff };
static const ClutterColor TRANSPARENT_COLOR = { 0, 0, 0, 0 };
39 40 41
static const ClutterColor DEFAULT_SUCCESS_COLOR = { 0x4e, 0x9a, 0x06, 0xff };
static const ClutterColor DEFAULT_WARNING_COLOR = { 0xf5, 0x79, 0x3e, 0xff };
static const ClutterColor DEFAULT_ERROR_COLOR = { 0xcc, 0x00, 0x00, 0xff };
42 43 44 45 46 47

G_DEFINE_TYPE (StThemeNode, st_theme_node, G_TYPE_OBJECT)

static void
st_theme_node_init (StThemeNode *node)
{
48
  node->transition_duration = -1;
49 50

  st_theme_node_paint_state_init (&node->cached_state);
51 52 53 54 55 56 57
}

static void
st_theme_node_class_init (StThemeNodeClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

Colin Walters's avatar
Colin Walters committed
58
  object_class->dispose = st_theme_node_dispose;
59 60 61
  object_class->finalize = st_theme_node_finalize;
}

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
static void
maybe_free_properties (StThemeNode *node)
{
  if (node->properties)
    {
      g_free (node->properties);
      node->properties = NULL;
      node->n_properties = 0;
    }

  if (node->inline_properties)
    {
      /* This destroys the list, not just the head of the list */
      cr_declaration_destroy (node->inline_properties);
      node->inline_properties = NULL;
    }
}

80 81
void
_st_theme_node_reset_for_stylesheet_change (StThemeNode *node)
82
{
83
  maybe_free_properties (node);
84 85 86
  node->properties_computed = FALSE;
}

Colin Walters's avatar
Colin Walters committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
static void
st_theme_node_dispose (GObject *gobject)
{
  StThemeNode *node = ST_THEME_NODE (gobject);

  if (node->parent_node)
    {
      g_object_unref (node->parent_node);
      node->parent_node = NULL;
    }

  if (node->border_image)
    {
      g_object_unref (node->border_image);
      node->border_image = NULL;
    }

104 105 106 107 108 109
  if (node->icon_colors)
    {
      st_icon_colors_unref (node->icon_colors);
      node->icon_colors = NULL;
    }

110 111
  st_theme_node_paint_state_free (&node->cached_state);

112 113
  g_clear_object (&node->theme);

Colin Walters's avatar
Colin Walters committed
114 115 116
  G_OBJECT_CLASS (st_theme_node_parent_class)->dispose (gobject);
}

117 118 119 120 121 122
static void
st_theme_node_finalize (GObject *object)
{
  StThemeNode *node = ST_THEME_NODE (object);

  g_free (node->element_id);
123 124
  g_strfreev (node->element_classes);
  g_strfreev (node->pseudo_classes);
Owen W. Taylor's avatar
Owen W. Taylor committed
125
  g_free (node->inline_style);
126

127
  maybe_free_properties (node);
Owen W. Taylor's avatar
Owen W. Taylor committed
128

Niels De Graef's avatar
Niels De Graef committed
129
  g_clear_pointer (&node->font_desc, pango_font_description_free);
130

Niels De Graef's avatar
Niels De Graef committed
131 132 133
  g_clear_pointer (&node->box_shadow, st_shadow_unref);
  g_clear_pointer (&node->background_image_shadow, st_shadow_unref);
  g_clear_pointer (&node->text_shadow, st_shadow_unref);
134

Niels De Graef's avatar
Niels De Graef committed
135
  g_clear_object (&node->background_image);
136

Niels De Graef's avatar
Niels De Graef committed
137 138 139 140 141 142
  cogl_clear_object (&node->background_texture);
  cogl_clear_object (&node->background_pipeline);
  cogl_clear_object (&node->background_shadow_pipeline);
  cogl_clear_object (&node->border_slices_texture);
  cogl_clear_object (&node->border_slices_pipeline);
  cogl_clear_object (&node->color_pipeline);
143

144 145 146
  G_OBJECT_CLASS (st_theme_node_parent_class)->finalize (object);
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static GStrv
split_on_whitespace (const gchar *s)
{
  gchar *cur;
  gchar *l;
  gchar *temp;
  GPtrArray *arr;

  if (s == NULL)
    return NULL;

  arr = g_ptr_array_new ();
  l = g_strdup (s);

  cur = strtok_r (l, " \t\f\r\n", &temp);

  while (cur != NULL)
    {
      g_ptr_array_add (arr, g_strdup (cur));
      cur = strtok_r (NULL, " \t\f\r\n", &temp);
    }

  g_free (l);
  g_ptr_array_add (arr, NULL);
  return (GStrv) g_ptr_array_free (arr, FALSE);
}

174 175 176
/**
 * st_theme_node_new:
 * @context: the context representing global state for this themed tree
177 178
 * @parent_node: (nullable): the parent node of this node
 * @theme: (nullable): a theme (stylesheet set) that overrides the
179 180 181 182 183
 *   theme inherited from the parent node
 * @element_type: the type of the GObject represented by this node
 *  in the tree (corresponding to an element if we were theming an XML
 *  document. %G_TYPE_NONE means this style was created for the stage
 * actor and matches a selector element name of 'stage'.
184 185
 * @element_id: (nullable): the ID to match CSS rules against
 * @element_class: (nullable): a whitespace-separated list of classes
186
 *   to match CSS rules against
187
 * @pseudo_class: (nullable): a whitespace-separated list of pseudo-classes
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
 *   (like 'hover' or 'visited') to match CSS rules against
 *
 * Creates a new #StThemeNode. Once created, a node is immutable. Of any
 * of the attributes of the node (like the @element_class) change the node
 * and its child nodes must be destroyed and recreated.
 *
 * Return value: (transfer full): the theme node
 */
StThemeNode *
st_theme_node_new (StThemeContext    *context,
                   StThemeNode       *parent_node,
                   StTheme           *theme,
                   GType              element_type,
                   const char        *element_id,
                   const char        *element_class,
Owen W. Taylor's avatar
Owen W. Taylor committed
203 204
                   const char        *pseudo_class,
                   const char        *inline_style)
205 206 207 208 209 210 211 212
{
  StThemeNode *node;

  g_return_val_if_fail (ST_IS_THEME_CONTEXT (context), NULL);
  g_return_val_if_fail (parent_node == NULL || ST_IS_THEME_NODE (parent_node), NULL);

  node = g_object_new (ST_TYPE_THEME_NODE, NULL);

213
  node->context = context;
214 215 216 217 218 219 220 221
  if (parent_node != NULL)
    node->parent_node = g_object_ref (parent_node);
  else
    node->parent_node = NULL;

  if (theme == NULL && parent_node != NULL)
    theme = parent_node->theme;

222
  g_set_object (&node->theme, theme);
223 224
  node->element_type = element_type;
  node->element_id = g_strdup (element_id);
225 226
  node->element_classes = split_on_whitespace (element_class);
  node->pseudo_classes = split_on_whitespace (pseudo_class);
Owen W. Taylor's avatar
Owen W. Taylor committed
227
  node->inline_style = g_strdup (inline_style);
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

  return node;
}

/**
 * st_theme_node_get_parent:
 * @node: a #StThemeNode
 *
 * Gets the parent themed element node.
 *
 * Return value: (transfer none): the parent #StThemeNode, or %NULL if this
 *  is the root node of the tree of theme elements.
 */
StThemeNode *
st_theme_node_get_parent (StThemeNode *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), NULL);

  return node->parent_node;
}

/**
 * st_theme_node_get_theme:
 * @node: a #StThemeNode
 *
 * Gets the theme stylesheet set that styles this node
 *
 * Return value: (transfer none): the theme stylesheet set
 */
StTheme *
st_theme_node_get_theme (StThemeNode *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), NULL);

  return node->theme;
}

GType
st_theme_node_get_element_type (StThemeNode *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), G_TYPE_NONE);

  return node->element_type;
}

const char *
st_theme_node_get_element_id (StThemeNode *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), NULL);

  return node->element_id;
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/**
 * st_theme_node_get_element_classes:
 *
 * Returns: (transfer none): the element's classes
 */
GStrv
st_theme_node_get_element_classes (StThemeNode *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), NULL);

  return node->element_classes;
}

/**
 * st_theme_node_get_pseudo_classes:
 *
 * Returns: (transfer none): the element's pseudo-classes
 */
GStrv
st_theme_node_get_pseudo_classes (StThemeNode *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), NULL);

  return node->pseudo_classes;
}

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
/**
 * st_theme_node_equal:
 * @node_a: first #StThemeNode
 * @node_b: second #StThemeNode
 *
 * Compare two #StThemeNodes. Two nodes which compare equal will match
 * the same CSS rules and have the same style properties. However, two
 * nodes that have ended up with identical style properties do not
 * necessarily compare equal.
 * In detail, @node_a and @node_b are considered equal iff
 * <itemizedlist>
 *   <listitem>
 *     <para>they share the same #StTheme and #StThemeContext</para>
 *   </listitem>
 *   <listitem>
 *     <para>they have the same parent</para>
 *   </listitem>
 *   <listitem>
 *     <para>they have the same element type</para>
 *   </listitem>
 *   <listitem>
 *     <para>their id, class, pseudo-class and inline-style match</para>
 *   </listitem>
 * </itemizedlist>
 *
 * Returns: %TRUE if @node_a equals @node_b
 */
gboolean
st_theme_node_equal (StThemeNode *node_a, StThemeNode *node_b)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node_a), FALSE);
338 339 340 341

  if (node_a == node_b)
    return TRUE;

342 343
  g_return_val_if_fail (ST_IS_THEME_NODE (node_b), FALSE);

344 345 346 347 348 349 350 351 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
  if (node_a->parent_node != node_b->parent_node ||
      node_a->context != node_b->context ||
      node_a->theme != node_b->theme ||
      node_a->element_type != node_b->element_type ||
      g_strcmp0 (node_a->element_id, node_b->element_id) ||
      g_strcmp0 (node_a->inline_style, node_b->inline_style))
    return FALSE;

  if ((node_a->element_classes == NULL) != (node_b->element_classes == NULL))
    return FALSE;

  if ((node_a->pseudo_classes == NULL) != (node_b->pseudo_classes == NULL))
    return FALSE;

  if (node_a->element_classes != NULL)
    {
      int i;

      for (i = 0; ; i++)
        {
          if (g_strcmp0 (node_a->element_classes[i],
                         node_b->element_classes[i]))
            return FALSE;

          if (node_a->element_classes[i] == NULL)
            break;
        }
    }

  if (node_a->pseudo_classes != NULL)
    {
      int i;

      for (i = 0; ; i++)
        {
          if (g_strcmp0 (node_a->pseudo_classes[i],
                         node_b->pseudo_classes[i]))
            return FALSE;

          if (node_a->pseudo_classes[i] == NULL)
            break;
        }
    }

  return TRUE;
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
guint
st_theme_node_hash (StThemeNode *node)
{
  guint hash = GPOINTER_TO_UINT (node->parent_node);

  hash = hash * 33 + GPOINTER_TO_UINT (node->context);
  hash = hash * 33 + GPOINTER_TO_UINT (node->theme);
  hash = hash * 33 + ((guint) node->element_type);

  if (node->element_id != NULL)
    hash = hash * 33 + g_str_hash (node->element_id);

  if (node->inline_style != NULL)
    hash = hash * 33 + g_str_hash (node->inline_style);

  if (node->element_classes != NULL)
    {
      gchar **it;

      for (it = node->element_classes; *it != NULL; it++)
        hash = hash * 33 + g_str_hash (*it) + 1;
    }

  if (node->pseudo_classes != NULL)
    {
      gchar **it;

      for (it = node->pseudo_classes; *it != NULL; it++)
        hash = hash * 33 + g_str_hash (*it) + 1;
    }

  return hash;
}

425
static void
426 427 428 429
ensure_properties (StThemeNode *node)
{
  if (!node->properties_computed)
    {
Owen W. Taylor's avatar
Owen W. Taylor committed
430 431
      GPtrArray *properties = NULL;

432 433 434
      node->properties_computed = TRUE;

      if (node->theme)
Owen W. Taylor's avatar
Owen W. Taylor committed
435 436 437 438 439 440 441 442 443
        properties = _st_theme_get_matched_properties (node->theme, node);

      if (node->inline_style)
        {
          CRDeclaration *cur_decl;

          if (!properties)
            properties = g_ptr_array_new ();

444
          node->inline_properties = _st_theme_parse_declaration_list (node->inline_style);
Owen W. Taylor's avatar
Owen W. Taylor committed
445 446 447 448 449 450 451 452 453
          for (cur_decl = node->inline_properties; cur_decl; cur_decl = cur_decl->next)
            g_ptr_array_add (properties, cur_decl);
        }

      if (properties)
        {
          node->n_properties = properties->len;
          node->properties = (CRDeclaration **)g_ptr_array_free (properties, FALSE);
        }
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
    }
}

typedef enum {
  VALUE_FOUND,
  VALUE_NOT_FOUND,
  VALUE_INHERIT
} GetFromTermResult;

static gboolean
term_is_inherit (CRTerm *term)
{
  return (term->type == TERM_IDENT &&
          strcmp (term->content.str->stryng->str, "inherit") == 0);
}

static gboolean
term_is_none (CRTerm *term)
{
  return (term->type == TERM_IDENT &&
          strcmp (term->content.str->stryng->str, "none") == 0);
}

static gboolean
term_is_transparent (CRTerm *term)
{
  return (term->type == TERM_IDENT &&
          strcmp (term->content.str->stryng->str, "transparent") == 0);
}

static int
color_component_from_double (double component)
{
  /* We want to spread the range 0-1 equally over 0..255, but
   * 1.0 should map to 255 not 256, so we need to special-case it.
   * See http://people.redhat.com/otaylor/pixel-converting.html
   * for (very) detailed discussion of related issues. */
  if (component >= 1.0)
    return 255;
  else
    return (int)(component * 256);
}

static GetFromTermResult
get_color_from_rgba_term (CRTerm       *term,
                          ClutterColor *color)
{
  CRTerm *arg = term->ext_content.func_param;
  CRNum *num;
  double r = 0, g = 0, b = 0, a = 0;
  int i;

  for (i = 0; i < 4; i++)
    {
      double value;

      if (arg == NULL)
        return VALUE_NOT_FOUND;

      if ((i == 0 && arg->the_operator != NO_OP) ||
          (i > 0 && arg->the_operator != COMMA))
        return VALUE_NOT_FOUND;

      if (arg->type != TERM_NUMBER)
        return VALUE_NOT_FOUND;

      num = arg->content.num;

      /* For simplicity, we convert a,r,g,b to [0,1.0] floats and then
       * convert them back below. Then when we set them on a cairo content
       * we convert them back to floats, and then cairo converts them
       * back to integers to pass them to X, and so forth...
       */
      if (i < 3)
        {
          if (num->type == NUM_PERCENTAGE)
            value = num->val / 100;
          else if (num->type == NUM_GENERIC)
            value = num->val / 255;
          else
            return VALUE_NOT_FOUND;
        }
      else
        {
          if (num->type != NUM_GENERIC)
            return VALUE_NOT_FOUND;

          value = num->val;
        }

      value = CLAMP (value, 0, 1);

      switch (i)
        {
        case 0:
          r = value;
          break;
        case 1:
          g = value;
          break;
        case 2:
          b = value;
          break;
        case 3:
          a = value;
          break;
560 561 562
        default:
          g_assert_not_reached();
          break;
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 594 595 596 597 598 599
        }

      arg = arg->next;
    }

  color->red = color_component_from_double (r);
  color->green = color_component_from_double (g);
  color->blue = color_component_from_double (b);
  color->alpha = color_component_from_double (a);

  return VALUE_FOUND;
}

static GetFromTermResult
get_color_from_term (StThemeNode  *node,
                     CRTerm       *term,
                     ClutterColor *color)
{
  CRRgb rgb;
  enum CRStatus status;

  /* Since libcroco doesn't know about rgba colors, it can't handle
   * the transparent keyword
   */
  if (term_is_transparent (term))
    {
      *color = TRANSPARENT_COLOR;
      return VALUE_FOUND;
    }
  /* rgba () colors - a CSS3 addition, are not supported by libcroco,
   * but they are parsed as a "function", so we can emulate the
   * functionality.
   */
  else if (term->type == TERM_FUNCTION &&
           term->content.str &&
           term->content.str->stryng &&
           term->content.str->stryng->str &&
Dan Winship's avatar
Dan Winship committed
600
           strcmp (term->content.str->stryng->str, "rgba") == 0)
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    {
      return get_color_from_rgba_term (term, color);
    }

  status = cr_rgb_set_from_term (&rgb, term);
  if (status != CR_OK)
    return VALUE_NOT_FOUND;

  if (rgb.inherit)
    return VALUE_INHERIT;

  if (rgb.is_percentage)
    cr_rgb_compute_from_percentage (&rgb);

  color->red = rgb.red;
  color->green = rgb.green;
  color->blue = rgb.blue;
  color->alpha = 0xff;

  return VALUE_FOUND;
}

/**
624
 * st_theme_node_lookup_color:
625 626 627 628 629 630 631
 * @node: a #StThemeNode
 * @property_name: The name of the color property
 * @inherit: if %TRUE, if a value is not found for the property on the
 *   node, then it will be looked up on the parent node, and then on the
 *   parent's parent, and so forth. Note that if the property has a
 *   value of 'inherit' it will be inherited even if %FALSE is passed
 *   in for @inherit; this only affects the default behavior for inheritance.
632 633
 * @color: (out caller-allocates): location to store the color that was
 *   determined. If the property is not found, the value in this location
634 635 636 637 638 639 640
 *   will not be changed.
 *
 * Generically looks up a property containing a single color value. When
 * specific getters (like st_theme_node_get_background_color()) exist, they
 * should be used instead. They are cached, so more efficient, and have
 * handling for shortcut properties and other details of CSS.
 *
641 642
 * See also st_theme_node_get_color(), which provides a simpler API.
 *
643 644 645 646
 * Return value: %TRUE if the property was found in the properties for this
 *  theme node (or in the properties of parent nodes when inheriting.)
 */
gboolean
647 648 649 650
st_theme_node_lookup_color (StThemeNode  *node,
                            const char   *property_name,
                            gboolean      inherit,
                            ClutterColor *color)
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
{

  int i;

  ensure_properties (node);

  for (i = node->n_properties - 1; i >= 0; i--)
    {
      CRDeclaration *decl = node->properties[i];

      if (strcmp (decl->property->stryng->str, property_name) == 0)
        {
          GetFromTermResult result = get_color_from_term (node, decl->value, color);
          if (result == VALUE_FOUND)
            {
              return TRUE;
            }
          else if (result == VALUE_INHERIT)
            {
              if (node->parent_node)
671
                return st_theme_node_lookup_color (node->parent_node, property_name, inherit, color);
672 673 674 675 676 677
              else
                break;
            }
        }
    }

678
  if (inherit && node->parent_node)
679
    return st_theme_node_lookup_color (node->parent_node, property_name, inherit, color);
680

681 682 683 684
  return FALSE;
}

/**
685 686 687
 * st_theme_node_get_color:
 * @node: a #StThemeNode
 * @property_name: The name of the color property
688 689
 * @color: (out caller-allocates): location to store the color that
 *   was determined.
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
 *
 * Generically looks up a property containing a single color value. When
 * specific getters (like st_theme_node_get_background_color()) exist, they
 * should be used instead. They are cached, so more efficient, and have
 * handling for shortcut properties and other details of CSS.
 *
 * If @property_name is not found, a warning will be logged and a
 * default color returned.
 *
 * See also st_theme_node_lookup_color(), which provides more options,
 * and lets you handle the case where the theme does not specify the
 * indicated color.
 */
void
st_theme_node_get_color (StThemeNode  *node,
                         const char   *property_name,
                         ClutterColor *color)
{
  if (!st_theme_node_lookup_color (node, property_name, FALSE, color))
    {
      g_warning ("Did not find color property '%s'", property_name);
      memset (color, 0, sizeof (ClutterColor));
    }
}

/**
 * st_theme_node_lookup_double:
717 718 719 720 721 722 723
 * @node: a #StThemeNode
 * @property_name: The name of the numeric property
 * @inherit: if %TRUE, if a value is not found for the property on the
 *   node, then it will be looked up on the parent node, and then on the
 *   parent's parent, and so forth. Note that if the property has a
 *   value of 'inherit' it will be inherited even if %FALSE is passed
 *   in for @inherit; this only affects the default behavior for inheritance.
724
 * @value: (out): location to store the value that was determined.
725 726 727 728 729 730
 *   If the property is not found, the value in this location
 *   will not be changed.
 *
 * Generically looks up a property containing a single numeric value
 *  without units.
 *
731 732
 * See also st_theme_node_get_double(), which provides a simpler API.
 *
733 734 735 736
 * Return value: %TRUE if the property was found in the properties for this
 *  theme node (or in the properties of parent nodes when inheriting.)
 */
gboolean
737 738 739 740
st_theme_node_lookup_double (StThemeNode *node,
                             const char  *property_name,
                             gboolean     inherit,
                             double      *value)
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
{
  gboolean result = FALSE;
  int i;

  ensure_properties (node);

  for (i = node->n_properties - 1; i >= 0; i--)
    {
      CRDeclaration *decl = node->properties[i];

      if (strcmp (decl->property->stryng->str, property_name) == 0)
        {
          CRTerm *term = decl->value;

          if (term->type != TERM_NUMBER || term->content.num->type != NUM_GENERIC)
            continue;

          *value = term->content.num->val;
          result = TRUE;
          break;
        }
    }

  if (!result && inherit && node->parent_node)
765
    result = st_theme_node_lookup_double (node->parent_node, property_name, inherit, value);
766 767 768 769

  return result;
}

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
/**
 * st_theme_node_lookup_time:
 * @node: a #StThemeNode
 * @property_name: The name of the time property
 * @inherit: if %TRUE, if a value is not found for the property on the
 *   node, then it will be looked up on the parent node, and then on the
 *   parent's parent, and so forth. Note that if the property has a
 *   value of 'inherit' it will be inherited even if %FALSE is passed
 *   in for @inherit; this only affects the default behavior for inheritance.
 * @value: (out): location to store the value that was determined.
 *   If the property is not found, the value in this location
 *   will not be changed.
 *
 * Generically looks up a property containing a single time value,
 *  which is converted to milliseconds.
 *
 * Return value: %TRUE if the property was found in the properties for this
 *  theme node (or in the properties of parent nodes when inheriting.)
 */
gboolean
st_theme_node_lookup_time (StThemeNode *node,
                           const char  *property_name,
                           gboolean     inherit,
                           double      *value)
{
  gboolean result = FALSE;
  int i;

  ensure_properties (node);

  for (i = node->n_properties - 1; i >= 0; i--)
    {
      CRDeclaration *decl = node->properties[i];

      if (strcmp (decl->property->stryng->str, property_name) == 0)
        {
          CRTerm *term = decl->value;
807
          int factor = 1;
808 809 810 811

          if (term->type != TERM_NUMBER)
            continue;

812
          if (term->content.num->type != NUM_TIME_S &&
813 814
              term->content.num->type != NUM_TIME_MS)
            continue;
815

816 817 818 819 820 821
          if (term->content.num->type == NUM_TIME_S)
            factor = 1000;

          *value = factor * term->content.num->val;
          result = TRUE;
          break;
822 823 824 825 826 827 828 829 830
        }
    }

  if (!result && inherit && node->parent_node)
    result = st_theme_node_lookup_time (node->parent_node, property_name, inherit, value);

  return result;
}

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
/**
 * st_theme_node_get_double:
 * @node: a #StThemeNode
 * @property_name: The name of the numeric property
 *
 * Generically looks up a property containing a single numeric value
 *  without units.
 *
 * See also st_theme_node_lookup_double(), which provides more options,
 * and lets you handle the case where the theme does not specify the
 * indicated value.
 *
 * Return value: the value found. If @property_name is not
 *  found, a warning will be logged and 0 will be returned.
 */
gdouble
st_theme_node_get_double (StThemeNode *node,
                          const char  *property_name)
{
  gdouble value;

  if (st_theme_node_lookup_double (node, property_name, FALSE, &value))
    return value;
  else
    {
      g_warning ("Did not find double property '%s'", property_name);
      return 0.0;
    }
}

861 862 863 864 865 866 867 868 869
/**
 * st_theme_node_lookup_url:
 * @node: a #StThemeNode
 * @property_name: The name of the string property
 * @inherit: if %TRUE, if a value is not found for the property on the
 *   node, then it will be looked up on the parent node, and then on the
 *   parent's parent, and so forth. Note that if the property has a
 *   value of 'inherit' it will be inherited even if %FALSE is passed
 *   in for @inherit; this only affects the default behavior for inheritance.
870
 * @file: (out) (transfer full): location to store the newly allocated value that was
871 872 873 874 875 876 877 878 879 880 881 882 883 884
 *   determined. If the property is not found, the value in this location
 *   will not be changed.
 *
 * Looks up a property containing a single URL value.
 *
 * See also st_theme_node_get_url(), which provides a simpler API.
 *
 * Return value: %TRUE if the property was found in the properties for this
 *  theme node (or in the properties of parent nodes when inheriting.)
 */
gboolean
st_theme_node_lookup_url (StThemeNode  *node,
                          const char   *property_name,
                          gboolean      inherit,
885
                          GFile       **file)
886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
{
  gboolean result = FALSE;
  int i;

  ensure_properties (node);

  for (i = node->n_properties - 1; i >= 0; i--)
    {
      CRDeclaration *decl = node->properties[i];

      if (strcmp (decl->property->stryng->str, property_name) == 0)
        {
          CRTerm *term = decl->value;
          CRStyleSheet *base_stylesheet;

          if (term->type != TERM_URI && term->type != TERM_STRING)
            continue;

          if (decl->parent_statement != NULL)
            base_stylesheet = decl->parent_statement->parent_sheet;
          else
            base_stylesheet = NULL;

909 910 911
          *file = _st_theme_resolve_url (node->theme,
                                         base_stylesheet,
                                         decl->value->content.str->stryng->str);
912 913 914 915 916 917
          result = TRUE;
          break;
        }
    }

  if (!result && inherit && node->parent_node)
918
    result = st_theme_node_lookup_url (node->parent_node, property_name, inherit, file);
919 920 921 922

  return result;
}

923
/**
924 925 926 927 928 929 930 931 932 933
 * st_theme_node_get_url:
 * @node: a #StThemeNode
 * @property_name: The name of the string property
 *
 * Looks up a property containing a single URL value.
 *
 * See also st_theme_node_lookup_url(), which provides more options,
 * and lets you handle the case where the theme does not specify the
 * indicated value.
 *
934
 * Returns: (transfer full): the newly allocated value if found.
935 936 937
 *  If @property_name is not found, a warning will be logged and %NULL
 *  will be returned.
 */
938
GFile *
939 940 941
st_theme_node_get_url (StThemeNode *node,
                       const char  *property_name)
{
942
  GFile *file;
943

944 945
  if (st_theme_node_lookup_url (node, property_name, FALSE, &file))
    return file;
946 947 948 949 950 951 952
  else
    {
      g_warning ("Did not find string property '%s'", property_name);
      return NULL;
    }
}

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
static const PangoFontDescription *
get_parent_font (StThemeNode *node)
{
  if (node->parent_node)
    return st_theme_node_get_font (node->parent_node);
  else
    return st_theme_context_get_font (node->context);
}

static GetFromTermResult
get_length_from_term (StThemeNode *node,
                      CRTerm      *term,
                      gboolean     use_parent_font,
                      gdouble     *length)
{
  CRNum *num;

  enum {
    ABSOLUTE,
    POINTS,
    FONT_RELATIVE,
  } type = ABSOLUTE;

  double multiplier = 1.0;
Adel Gadllah's avatar
Adel Gadllah committed
977 978 979
  int scale_factor;

  g_object_get (node->context, "scale-factor", &scale_factor, NULL);
980 981 982

  if (term->type != TERM_NUMBER)
    {
983 984
      g_warning ("Ignoring length property that isn't a number at line %d, col %d",
                 term->location.line, term->location.column);
985
      return VALUE_NOT_FOUND;
986 987 988 989 990 991 992 993
    }

  num = term->content.num;

  switch (num->type)
    {
    case NUM_LENGTH_PX:
      type = ABSOLUTE;
Adel Gadllah's avatar
Adel Gadllah committed
994
      multiplier = 1 * scale_factor;
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
      break;
    case NUM_LENGTH_PT:
      type = POINTS;
      multiplier = 1;
      break;
    case NUM_LENGTH_IN:
      type = POINTS;
      multiplier = 72;
      break;
    case NUM_LENGTH_CM:
      type = POINTS;
      multiplier = 72. / 2.54;
      break;
    case NUM_LENGTH_MM:
      type = POINTS;
      multiplier = 72. / 25.4;
      break;
    case NUM_LENGTH_PC:
      type = POINTS;
      multiplier = 12. / 25.4;
      break;
    case NUM_LENGTH_EM:
      {
        type = FONT_RELATIVE;
        multiplier = 1;
        break;
      }
    case NUM_LENGTH_EX:
      {
        /* Doing better would require actually resolving the font description
         * to a specific font, and Pango doesn't have an ex metric anyways,
         * so we'd have to try and synthesize it by complicated means.
         *
         * The 0.5em is the CSS spec suggested thing to use when nothing
         * better is available.
         */
        type = FONT_RELATIVE;
        multiplier = 0.5;
        break;
      }

    case NUM_INHERIT:
      return VALUE_INHERIT;

    case NUM_AUTO:
      g_warning ("'auto' not supported for lengths");
      return VALUE_NOT_FOUND;

    case NUM_GENERIC:
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
      {
        if (num->val != 0)
          {
            g_warning ("length values must specify a unit");
            return VALUE_NOT_FOUND;
          }
        else
          {
            type = ABSOLUTE;
            multiplier = 0;
          }
        break;
      }
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070

    case NUM_PERCENTAGE:
      g_warning ("percentage lengths not currently supported");
      return VALUE_NOT_FOUND;

    case NUM_ANGLE_DEG:
    case NUM_ANGLE_RAD:
    case NUM_ANGLE_GRAD:
    case NUM_TIME_MS:
    case NUM_TIME_S:
    case NUM_FREQ_HZ:
    case NUM_FREQ_KHZ:
    case NUM_UNKNOWN_TYPE:
    case NB_NUM_TYPE:
1071
    default:
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
      g_warning ("Ignoring invalid type of number of length property");
      return VALUE_NOT_FOUND;
    }

  switch (type)
    {
    case ABSOLUTE:
      *length = num->val * multiplier;
      break;
    case POINTS:
      {
1083
        double resolution = clutter_backend_get_resolution (clutter_get_default_backend ());
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
        *length = num->val * multiplier * (resolution / 72.);
      }
      break;
    case FONT_RELATIVE:
      {
        const PangoFontDescription *desc;
        double font_size;

        if (use_parent_font)
          desc = get_parent_font (node);
        else
          desc = st_theme_node_get_font (node);

        font_size = (double)pango_font_description_get_size (desc) / PANGO_SCALE;

        if (pango_font_description_get_size_is_absolute (desc))
          {
            *length = num->val * multiplier * font_size;
          }
        else
          {
1105
            double resolution = clutter_backend_get_resolution (clutter_get_default_backend ());
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
            *length = num->val * multiplier * (resolution / 72.) * font_size;
          }
      }
      break;
    default:
      g_assert_not_reached ();
    }

  return VALUE_FOUND;
}

1117 1118 1119 1120 1121 1122 1123 1124
static GetFromTermResult
get_length_from_term_int (StThemeNode *node,
                          CRTerm      *term,
                          gboolean     use_parent_font,
                          gint        *length)
{
  double value;
  GetFromTermResult result;
1125
  int scale_factor;
1126 1127

  result = get_length_from_term (node, term, use_parent_font, &value);
1128
  if (result == VALUE_FOUND)
1129 1130 1131 1132
    {
      g_object_get (node->context, "scale-factor", &scale_factor, NULL);
      *length = (int) ((value / scale_factor) + 0.5) * scale_factor;
    }
1133 1134 1135
  return result;
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
static GetFromTermResult
get_length_internal (StThemeNode *node,
                     const char  *property_name,
                     gdouble     *length)
{
  int i;

  ensure_properties (node);

  for (i = node->n_properties - 1; i >= 0; i--)
    {
      CRDeclaration *decl = node->properties[i];

1149
      if (strcmp (decl->property->stryng->str, property_name) == 0)
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
        {
          GetFromTermResult result = get_length_from_term (node, decl->value, FALSE, length);
          if (result != VALUE_NOT_FOUND)
            return result;
        }
    }

  return VALUE_NOT_FOUND;
}

/**
1161
 * st_theme_node_lookup_length:
1162 1163 1164 1165 1166 1167 1168
 * @node: a #StThemeNode
 * @property_name: The name of the length property
 * @inherit: if %TRUE, if a value is not found for the property on the
 *   node, then it will be looked up on the parent node, and then on the
 *   parent's parent, and so forth. Note that if the property has a
 *   value of 'inherit' it will be inherited even if %FALSE is passed
 *   in for @inherit; this only affects the default behavior for inheritance.
1169
 * @length: (out): location to store the length that was determined.
1170 1171 1172 1173 1174 1175 1176 1177 1178
 *   If the property is not found, the value in this location
 *   will not be changed. The returned length is resolved
 *   to pixels.
 *
 * Generically looks up a property containing a single length value. When
 * specific getters (like st_theme_node_get_border_width()) exist, they
 * should be used instead. They are cached, so more efficient, and have
 * handling for shortcut properties and other details of CSS.
 *
1179 1180
 * See also st_theme_node_get_length(), which provides a simpler API.
 *
1181 1182 1183 1184
 * Return value: %TRUE if the property was found in the properties for this
 *  theme node (or in the properties of parent nodes when inheriting.)
 */
gboolean
1185 1186 1187 1188
st_theme_node_lookup_length (StThemeNode *node,
                             const char  *property_name,
                             gboolean     inherit,
                             gdouble     *length)
1189
{
1190
  GetFromTermResult result = get_length_internal (node, property_name, length);
1191 1192 1193 1194 1195
  if (result == VALUE_FOUND)
    return TRUE;
  else if (result == VALUE_INHERIT)
    inherit = TRUE;

Federico Mena Quintero's avatar
Federico Mena Quintero committed
1196 1197 1198 1199
  if (inherit && node->parent_node)
    return st_theme_node_lookup_length (node->parent_node, property_name, inherit, length);

  return FALSE;
1200 1201
}

1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
/**
 * st_theme_node_get_length:
 * @node: a #StThemeNode
 * @property_name: The name of the length property
 *
 * Generically looks up a property containing a single length value. When
 * specific getters (like st_theme_node_get_border_width()) exist, they
 * should be used instead. They are cached, so more efficient, and have
 * handling for shortcut properties and other details of CSS.
 *
 * Unlike st_theme_node_get_color() and st_theme_node_get_double(),
 * this does not print a warning if the property is not found; it just
 * returns 0.
 *
 * See also st_theme_node_lookup_length(), which provides more options.
 *
 * Return value: the length, in pixels, or 0 if the property was not found.
 */
gdouble
st_theme_node_get_length (StThemeNode *node,
                          const char  *property_name)
{
  gdouble length;

  if (st_theme_node_lookup_length (node, property_name, FALSE, &length))
    return length;
  else
    return 0.0;
}

1232 1233 1234 1235 1236 1237 1238 1239
static void
do_border_radius_term (StThemeNode *node,
                       CRTerm      *term,
                       gboolean     topleft,
                       gboolean     topright,
                       gboolean     bottomright,
                       gboolean     bottomleft)
{
1240
  int value;
1241

1242
  if (get_length_from_term_int (node, term, FALSE, &value) != VALUE_FOUND)
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
    return;

  if (topleft)
    node->border_radius[ST_CORNER_TOPLEFT] = value;
  if (topright)
    node->border_radius[ST_CORNER_TOPRIGHT] = value;
  if (bottomright)
    node->border_radius[ST_CORNER_BOTTOMRIGHT] = value;
  if (bottomleft)
    node->border_radius[ST_CORNER_BOTTOMLEFT] = value;
}

static void
do_border_radius (StThemeNode   *node,
                  CRDeclaration *decl)
{
  const char *property_name = decl->property->stryng->str + 13; /* Skip 'border-radius' */

  if (strcmp (property_name, "") == 0)
    {
      /* Slight deviation ... if we don't understand some of the terms and understand others,
       * then we set the ones we understand and ignore the others instead of ignoring the
       * whole thing
       */
      if (decl->value == NULL) /* 0 values */
        return;
      else if (decl->value->next == NULL) /* 1 value */
        {
          do_border_radius_term (node, decl->value,       TRUE, TRUE, TRUE, TRUE); /* all corners */
          return;
        }
      else if (decl->value->next->next == NULL) /* 2 values */
        {
          do_border_radius_term (node, decl->value,       TRUE,  FALSE,  TRUE,  FALSE);  /* topleft/bottomright */
          do_border_radius_term (node, decl->value->next, FALSE,  TRUE,   FALSE, TRUE);  /* topright/bottomleft */
        }
      else if (decl->value->next->next->next == NULL) /* 3 values */
        {
          do_border_radius_term (node, decl->value,             TRUE,  FALSE, FALSE, FALSE); /* topleft */
          do_border_radius_term (node, decl->value->next,       FALSE, TRUE,  FALSE, TRUE);  /* topright/bottomleft */
          do_border_radius_term (node, decl->value->next->next, FALSE, FALSE, TRUE,  FALSE);  /* bottomright */
        }
      else if (decl->value->next->next->next->next == NULL) /* 4 values */
        {
          do_border_radius_term (node, decl->value,                   TRUE,  FALSE, FALSE, FALSE); /* topleft */
          do_border_radius_term (node, decl->value->next,             FALSE, TRUE,  FALSE, FALSE); /* topright */
          do_border_radius_term (node, decl->value->next->next,       FALSE, FALSE, TRUE,  FALSE); /* bottomright */
          do_border_radius_term (node, decl->value->next->next->next, FALSE, FALSE, FALSE, TRUE);  /* bottomleft */
        }
      else
        {
          g_warning ("Too many values for border-radius property");
          return;
        }
    }
  else
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

      if (strcmp (property_name, "-topleft") == 0)
        do_border_radius_term (node, decl->value, TRUE,  FALSE, FALSE, FALSE);
      else if (strcmp (property_name, "-topright") == 0)
        do_border_radius_term (node, decl->value, FALSE, TRUE,  FALSE, FALSE);
      else if (strcmp (property_name, "-bottomright") == 0)
        do_border_radius_term (node, decl->value, FALSE, FALSE, TRUE,  FALSE);
      else if (strcmp (property_name, "-bottomleft") == 0)
        do_border_radius_term (node, decl->value, FALSE, FALSE, FALSE, TRUE);
    }
}

1314 1315 1316 1317 1318 1319 1320 1321
static void
do_border_property (StThemeNode   *node,
                    CRDeclaration *decl)
{
  const char *property_name = decl->property->stryng->str + 6; /* Skip 'border' */
  StSide side = (StSide)-1;
  ClutterColor color;
  gboolean color_set = FALSE;
1322
  int width = 0; /* suppress warning */
1323 1324 1325
  gboolean width_set = FALSE;
  int j;

1326 1327 1328 1329 1330 1331
  if (g_str_has_prefix (property_name, "-radius"))
    {
      do_border_radius (node, decl);
      return;
    }

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
  if (g_str_has_prefix (property_name, "-left"))
    {
      side = ST_SIDE_LEFT;
      property_name += 5;
    }
  else if (g_str_has_prefix (property_name, "-right"))
    {
      side = ST_SIDE_RIGHT;
      property_name += 6;
    }
  else if (g_str_has_prefix (property_name, "-top"))
    {
      side = ST_SIDE_TOP;
      property_name += 4;
    }
  else if (g_str_has_prefix (property_name, "-bottom"))
    {
      side = ST_SIDE_BOTTOM;
      property_name += 7;
    }

  if (strcmp (property_name, "") == 0)
    {
1355
      /* Set value for width/color/style in any order */
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
      CRTerm *term;

      for (term = decl->value; term; term = term->next)
        {
          GetFromTermResult result;

          if (term->type == TERM_IDENT)
            {
              const char *ident = term->content.str->stryng->str;
              if (strcmp (ident, "none") == 0 || strcmp (ident, "hidden") == 0)
                {
1367 1368
                  width = 0;
                  width_set = TRUE;
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
                  continue;
                }
              else if (strcmp (ident, "solid") == 0)
                {
                  /* The only thing we support */
                  continue;
                }
              else if (strcmp (ident, "dotted") == 0 ||
                       strcmp (ident, "dashed") == 0 ||
                       strcmp (ident, "double") == 0 ||
                       strcmp (ident, "groove") == 0 ||
                       strcmp (ident, "ridge") == 0 ||
                       strcmp (ident, "inset") == 0 ||
                       strcmp (ident, "outset") == 0)
                {
                  /* Treat the same as solid */
                  continue;
                }

              /* Presumably a color, fall through */
            }

          if (term->type == TERM_NUMBER)
            {
1393
              result = get_length_from_term_int (node, term, FALSE, &width);
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
              if (result != VALUE_NOT_FOUND)
                {
                  width_set = result == VALUE_FOUND;
                  continue;
                }
            }

          result = get_color_from_term (node, term, &color);
          if (result != VALUE_NOT_FOUND)
            {
              color_set = result == VALUE_FOUND;
              continue;
            }
        }

    }
  else if (strcmp (property_name, "-color") == 0)
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

      if (get_color_from_term (node, decl->value, &color) == VALUE_FOUND)
        /* Ignore inherit */
        color_set = TRUE;
    }
  else if (strcmp (property_name, "-width") == 0)
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

1424
      if (get_length_from_term_int (node, decl->value, FALSE, &width) == VALUE_FOUND)
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
        /* Ignore inherit */
        width_set = TRUE;
    }

  if (side == (StSide)-1)
    {
      for (j = 0; j < 4; j++)
        {
          if (color_set)
            node->border_color[j] = color;
          if (width_set)
            node->border_width[j] = width;
        }
    }
  else
    {
      if (color_set)
        node->border_color[side] = color;
      if (width_set)
        node->border_width[side] = width;
    }
}

1448 1449 1450 1451 1452 1453 1454
static void
do_outline_property (StThemeNode   *node,
                     CRDeclaration *decl)
{
  const char *property_name = decl->property->stryng->str + 7; /* Skip 'outline' */
  ClutterColor color;
  gboolean color_set = FALSE;
1455
  int width = 0; /* suppress warning */
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
  gboolean width_set = FALSE;

  if (strcmp (property_name, "") == 0)
    {
      /* Set value for width/color/style in any order */
      CRTerm *term;

      for (term = decl->value; term; term = term->next)
        {
          GetFromTermResult result;

          if (term->type == TERM_IDENT)
            {
              const char *ident = term->content.str->stryng->str;
              if (strcmp (ident, "none") == 0 || strcmp (ident, "hidden") == 0)
                {
1472 1473
                  width = 0;
                  width_set = TRUE;
1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
                  continue;
                }
              else if (strcmp (ident, "solid") == 0)
                {
                  /* The only thing we support */
                  continue;
                }
              else if (strcmp (ident, "dotted") == 0 ||
                       strcmp (ident, "dashed") == 0 ||
                       strcmp (ident, "double") == 0 ||
                       strcmp (ident, "groove") == 0 ||
                       strcmp (ident, "ridge") == 0 ||
                       strcmp (ident, "inset") == 0 ||
                       strcmp (ident, "outset") == 0)
                {
                  /* Treat the same as solid */
                  continue;
                }

              /* Presumably a color, fall through */
            }

          if (term->type == TERM_NUMBER)
            {
              result = get_length_from_term_int (node, term, FALSE, &width);
              if (result != VALUE_NOT_FOUND)
                {
                  width_set = result == VALUE_FOUND;
                  continue;
                }
            }

          result = get_color_from_term (node, term, &color);
          if (result != VALUE_NOT_FOUND)
            {
              color_set = result == VALUE_FOUND;
              continue;
            }
        }

    }
  else if (strcmp (property_name, "-color") == 0)
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

      if (get_color_from_term (node, decl->value, &color) == VALUE_FOUND)
        /* Ignore inherit */
        color_set = TRUE;
    }
  else if (strcmp (property_name, "-width") == 0)
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

      if (get_length_from_term_int (node, decl->value, FALSE, &width) == VALUE_FOUND)
        /* Ignore inherit */
        width_set = TRUE;
    }

  if (color_set)
    node->outline_color = color;
  if (width_set)
    node->outline_width = width;
}

1540 1541 1542 1543 1544 1545 1546 1547
static void
do_padding_property_term (StThemeNode *node,
                          CRTerm      *term,
                          gboolean     left,
                          gboolean     right,
                          gboolean     top,
                          gboolean     bottom)
{
1548
  int value;
1549

1550
  if (get_length_from_term_int (node, term, FALSE, &value) != VALUE_FOUND)
1551 1552 1553
    return;

  if (left)
1554
    node->padding[ST_SIDE_LEFT] = value;
1555
  if (right)
1556
    node->padding[ST_SIDE_RIGHT] = value;
1557
  if (top)
1558
    node->padding[ST_SIDE_TOP] = value;
1559
  if (bottom)
1560
    node->padding[ST_SIDE_BOTTOM] = value;
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
}

static void
do_padding_property (StThemeNode   *node,
                     CRDeclaration *decl)
{
  const char *property_name = decl->property->stryng->str + 7; /* Skip 'padding' */

  if (strcmp (property_name, "") == 0)
    {
      /* Slight deviation ... if we don't understand some of the terms and understand others,
       * then we set the ones we understand and ignore the others instead of ignoring the
       * whole thing
       */
      if (decl->value == NULL) /* 0 values */
        return;
1577 1578
      else if (decl->value->next == NULL) /* 1 value */
        {
1579 1580 1581
          do_padding_property_term (node, decl->value, TRUE, TRUE, TRUE, TRUE); /* left/right/top/bottom */
          return;
        }
1582 1583
      else if (decl->value->next->next == NULL) /* 2 values */
        {
1584 1585 1586
          do_padding_property_term (node, decl->value,       FALSE, FALSE, TRUE,  TRUE);  /* top/bottom */
          do_padding_property_term (node, decl->value->next, TRUE, TRUE,   FALSE, FALSE); /* left/right */
        }
1587 1588
      else if (decl->value->next->next->next == NULL) /* 3 values */
        {
1589 1590 1591 1592
          do_padding_property_term (node, decl->value,             FALSE, FALSE, TRUE,  FALSE); /* top */
          do_padding_property_term (node, decl->value->next,       TRUE,  TRUE,  FALSE, FALSE); /* left/right */
          do_padding_property_term (node, decl->value->next->next, FALSE, FALSE, FALSE, TRUE);  /* bottom */
        }
1593 1594 1595 1596 1597 1598
      else if (decl->value->next->next->next->next == NULL) /* 4 values */
        {
          do_padding_property_term (node, decl->value,                   FALSE, FALSE, TRUE,  FALSE); /* top */
          do_padding_property_term (node, decl->value->next,             FALSE, TRUE,  FALSE, FALSE); /* right */
          do_padding_property_term (node, decl->value->next->next,       FALSE, FALSE, FALSE, TRUE);  /* bottom */
          do_padding_property_term (node, decl->value->next->next->next, TRUE,  FALSE, FALSE, FALSE); /* left */
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
        }
      else
        {
          g_warning ("Too many values for padding property");
          return;
        }
    }
  else
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

      if (strcmp (property_name, "-left") == 0)
        do_padding_property_term (node, decl->value, TRUE,  FALSE, FALSE, FALSE);
      else if (strcmp (property_name, "-right") == 0)
        do_padding_property_term (node, decl->value, FALSE, TRUE,  FALSE, FALSE);
      else if (strcmp (property_name, "-top") == 0)
        do_padding_property_term (node, decl->value, FALSE, FALSE, TRUE,  FALSE);
      else if (strcmp (property_name, "-bottom") == 0)
        do_padding_property_term (node, decl->value, FALSE, FALSE, FALSE, TRUE);
    }
}

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
static void
do_margin_property_term (StThemeNode *node,
                         CRTerm      *term,
                         gboolean     left,
                         gboolean     right,
                         gboolean     top,
                         gboolean     bottom)
{
  int value;

  if (get_length_from_term_int (node, term, FALSE, &value) != VALUE_FOUND)
    return;

  if (left)
1636
    node->margin[ST_SIDE_LEFT] = value;
1637
  if (right)
1638
    node->margin[ST_SIDE_RIGHT] = value;
1639
  if (top)
1640
    node->margin[ST_SIDE_TOP] = value;
1641
  if (bottom)
1642
    node->margin[ST_SIDE_BOTTOM] = value;
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
}

static void
do_margin_property (StThemeNode   *node,
                    CRDeclaration *decl)
{
  const char *property_name = decl->property->stryng->str + 6; /* Skip 'margin' */

  if (strcmp (property_name, "") == 0)
    {
      /* Slight deviation ... if we don't understand some of the terms and understand others,
       * then we set the ones we understand and ignore the others instead of ignoring the
       * whole thing
       */
      if (decl->value == NULL) /* 0 values */
        return;
      else if (decl->value->next == NULL) /* 1 value */
        {
          do_margin_property_term (node, decl->value, TRUE, TRUE, TRUE, TRUE); /* left/right/top/bottom */
          return;
        }
      else if (decl->value->next->next == NULL) /* 2 values */
        {
          do_margin_property_term (node, decl->value,       FALSE, FALSE, TRUE,  TRUE);  /* top/bottom */
          do_margin_property_term (node, decl->value->next, TRUE, TRUE,   FALSE, FALSE); /* left/right */
        }
      else if (decl->value->next->next->next == NULL) /* 3 values */
        {
          do_margin_property_term (node, decl->value,             FALSE, FALSE, TRUE,  FALSE); /* top */
          do_margin_property_term (node, decl->value->next,       TRUE,  TRUE,  FALSE, FALSE); /* left/right */
          do_margin_property_term (node, decl->value->next->next, FALSE, FALSE, FALSE, TRUE);  /* bottom */
        }
      else if (decl->value->next->next->next->next == NULL) /* 4 values */
        {
          do_margin_property_term (node, decl->value,                   FALSE, FALSE, TRUE,  FALSE); /* top */
          do_margin_property_term (node, decl->value->next,             FALSE, TRUE,  FALSE, FALSE); /* right */
          do_margin_property_term (node, decl->value->next->next,       FALSE, FALSE, FALSE, TRUE);  /* bottom */
          do_margin_property_term (node, decl->value->next->next->next, TRUE,  FALSE, FALSE, FALSE); /* left */
        }
      else
        {
          g_warning ("Too many values for margin property");
          return;
        }
    }
  else
    {
      if (decl->value == NULL || decl->value->next != NULL)
        return;

      if (strcmp (property_name, "-left") == 0)
        do_margin_property_term (node, decl->value, TRUE,  FALSE, FALSE, FALSE);
      else if (strcmp (property_name, "-right") == 0)
        do_margin_property_term (node, decl->value, FALSE, TRUE,  FALSE, FALSE);
      else if (strcmp (property_name, "-top") == 0)
        do_margin_property_term (node, decl->value, FALSE, FALSE, TRUE,  FALSE);
      else if (strcmp (property_name, "-bottom") == 0)
        do_margin_property_term (node, decl->value, FALSE, FALSE, FALSE, TRUE);
    }
}

1704
static void
1705 1706
do_size_property (StThemeNode   *node,
                  CRDeclaration *decl,
1707
                  int           *node_value)
1708
{
1709
  get_length_from_term_int (node, decl->value, FALSE, node_value);
1710 1711
}

1712 1713
void
_st_theme_node_ensure_geometry (StThemeNode *node)
1714 1715
{
  int i, j;
1716
  int width, height;
1717

1718
  if (node->geometry_computed)
1719 1720
    return;

1721
  node->geometry_computed = TRUE;
1722 1723 1724 1725 1726 1727 1728 1729 1730

  ensure_properties (node);

  for (j = 0; j < 4; j++)
    {
      node->border_width[j] = 0;
      node->border_color[j] = TRANSPARENT_COLOR;
    }

1731 1732 1733
  node->outline_width = 0;
  node->outline_color = TRANSPARENT_COLOR;

1734 1735
  width = -1;
  height = -1;
1736 1737 1738 1739
  node->width = -1;
  node->height = -1;
  node->min_width = -1;
  node->min_height = -1;
1740 1741
  node->max_width = -1;
  node->max_height = -1;
1742

1743 1744 1745 1746 1747 1748 1749
  for (i = 0; i < node->n_properties; i++)
    {
      CRDeclaration *decl = node->properties[i];
      const char *property_name = decl->property->stryng->str;

      if (g_str_has_prefix (property_name, "border"))
        do_border_property (node, decl);
1750 1751
      else if (g_str_has_prefix (property_name, "outline"))
        do_outline_property (node, decl);
1752 1753
      else if (g_str_has_prefix (property_name, "padding"))
        do_padding_property (node, decl);
1754 1755
      else if (g_str_has_prefix (property_name, "margin"))
        do_margin_property (node, decl);
1756
      else if (strcmp (property_name, "width") == 0)
1757
        do_size_property (node, decl, &width);
1758
      else if (strcmp (property_name, "height") == 0)
1759 1760 1761 1762
        do_size_property (node, decl, &height);
      else if (strcmp (property_name, "-st-natural-width") == 0)
        do_size_property (node, decl, &node->width);
      else if (strcmp (property_name, "-st-natural-height") == 0)
1763 1764 1765 1766 1767
        do_size_property (node, decl, &node->height);
      else if (strcmp (property_name, "min-width") == 0)
        do_size_property (node, decl, &node->min_width);
      else if (strcmp (property_name, "min-height") == 0)
        do_size_property (node, decl, &node->min_height);
1768 1769 1770 1771
      else if (strcmp (property_name, "max-width") == 0)
        do_size_property (node, decl, &node->max_width);
      else if (strcmp (property_name, "max-height") == 0)
        do_size_property (node, decl, &node->max_height);
1772 1773
    }

1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786
  /*
   * Setting width sets max-width, min-width and -st-natural-width,
   * unless one of them is set individually.
   * Setting min-width sets natural width too, so that the minimum
   * width reported by get_preferred_width() is always not greater
   * than the natural width.
   * The natural width in node->width is actually a lower bound, the
   * actor is allowed to request something greater than that, but
   * not greater than max-width.
   * We don't need to clamp node->width to be less than max_width,
   * that's done by adjust_preferred_width.
   */
  if (width != -1)
1787
    {
1788 1789
      if (node->width == -1)
        node->width = width;
1790
      if (node->min_width == -1)
1791
        node->min_width = width;
1792
      if (node->max_width == -1)
1793
        node->max_width = width;
1794
    }
1795

1796 1797 1798 1799
  if (node->width < node->min_width)
    node->width = node->min_width;

  if (height != -1)
1800
    {
1801 1802
      if (node->height == -1)
        node->height = height;
1803
      if (node->min_height == -1)
1804
        node->min_height = height;
1805
      if (node->max_height == -1)
1806
        node->max_height = height;
1807
    }
1808 1809 1810

  if (node->height < node->min_height)
    node->height = node->min_height;
1811 1812
}

1813
int
1814 1815 1816 1817
st_theme_node_get_border_width (StThemeNode *node,
                                StSide       side)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), 0.);
1818
  g_return_val_if_fail (side >= ST_SIDE_TOP && side <= ST_SIDE_LEFT, 0.);
1819

1820
  _st_theme_node_ensure_geometry (node);
1821 1822 1823 1824

  return node->border_width[side];
}

1825
int
1826 1827 1828 1829 1830 1831
st_theme_node_get_border_radius (StThemeNode *node,
                                 StCorner     corner)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), 0.);
  g_return_val_if_fail (corner >= ST_CORNER_TOPLEFT && corner <= ST_CORNER_BOTTOMLEFT, 0.);

1832
  _st_theme_node_ensure_geometry (node);
1833 1834 1835 1836

  return node->border_radius[corner];
}

1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
int
st_theme_node_get_outline_width (StThemeNode  *node)
{
  g_return_val_if_fail (ST_IS_THEME_NODE (node), 0);

  _st_theme_node_ensure_geometry (node);

  return node->outline_width;
}

1847 1848 1849 1850 1851
/**
 * st_theme_node_get_outline_color:
 * @node: a #StThemeNode
 * @color: (out caller-allocates): location to store the color
 *
Jasper St. Pierre's avatar
Jasper St. Pierre committed
1852
 * Gets the color of @node's outline.
1853
 */
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
void
st_theme_node_get_outline_color (StThemeNode  *node,
                                 ClutterColor *color)
{
  g_return_if_fail (ST_IS_THEME_NODE (