gtkcssstyleproperty.c 17.7 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
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gtkcssstylepropertyprivate.h"

24
#include "gtkcssenumvalueprivate.h"
25 26
#include "gtkcssinheritvalueprivate.h"
#include "gtkcssinitialvalueprivate.h"
27 28
#include "gtkcssstylefuncsprivate.h"
#include "gtkcsstypesprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
29
#include "gtkcssunsetvalueprivate.h"
30
#include "gtkintl.h"
31
#include "gtkprivatetypebuiltins.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "deprecated/gtkstylepropertiesprivate.h"
33

34 35 36 37 38
/* 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"

39 40
enum {
  PROP_0,
41
  PROP_ANIMATED,
42
  PROP_AFFECTS_SIZE,
43
  PROP_AFFECTS_FONT,
44
  PROP_ID,
45 46
  PROP_INHERIT,
  PROP_INITIAL
47 48
};

49 50
G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)

51
static GtkBitmask *_properties_affecting_size = NULL;
52
static GtkBitmask *_properties_affecting_font = NULL;
53

54 55
static GtkCssStylePropertyClass *gtk_css_style_property_class = NULL;

56 57 58 59 60 61 62 63 64
static void
gtk_css_style_property_constructed (GObject *object)
{
  GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);
  GtkCssStylePropertyClass *klass = GTK_CSS_STYLE_PROPERTY_GET_CLASS (property);

  property->id = klass->style_properties->len;
  g_ptr_array_add (klass->style_properties, property);

65 66 67
  if (property->affects_size)
    _properties_affecting_size = _gtk_bitmask_set (_properties_affecting_size, property->id, TRUE);

68 69 70
  if (property->affects_font)
    _properties_affecting_font = _gtk_bitmask_set (_properties_affecting_font, property->id, TRUE);

71 72 73
  G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
}

74 75 76 77 78 79 80 81 82 83
static void
gtk_css_style_property_set_property (GObject      *object,
                                     guint         prop_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);

  switch (prop_id)
    {
84 85 86
    case PROP_ANIMATED:
      property->animated = g_value_get_boolean (value);
      break;
87 88 89
    case PROP_AFFECTS_SIZE:
      property->affects_size = g_value_get_boolean (value);
      break;
90 91 92
    case PROP_AFFECTS_FONT:
      property->affects_font = g_value_get_boolean (value);
      break;
93 94 95 96
    case PROP_INHERIT:
      property->inherit = g_value_get_boolean (value);
      break;
    case PROP_INITIAL:
97 98
      property->initial_value = g_value_dup_boxed (value);
      g_assert (property->initial_value != NULL);
99 100 101 102 103 104 105
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

106 107 108 109 110 111 112 113 114 115
static void
gtk_css_style_property_get_property (GObject    *object,
                                     guint       prop_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  GtkCssStyleProperty *property = GTK_CSS_STYLE_PROPERTY (object);

  switch (prop_id)
    {
116 117 118
    case PROP_ANIMATED:
      g_value_set_boolean (value, property->animated);
      break;
119 120 121
    case PROP_AFFECTS_SIZE:
      g_value_set_boolean (value, property->affects_size);
      break;
122 123 124
    case PROP_AFFECTS_FONT:
      g_value_set_boolean (value, property->affects_font);
      break;
125 126 127
    case PROP_ID:
      g_value_set_boolean (value, property->id);
      break;
128 129 130 131
    case PROP_INHERIT:
      g_value_set_boolean (value, property->inherit);
      break;
    case PROP_INITIAL:
132
      g_value_set_boxed (value, property->initial_value);
133
      break;
134 135 136 137 138 139
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

140 141 142 143 144 145
static void
_gtk_css_style_property_assign (GtkStyleProperty   *property,
                                GtkStyleProperties *props,
                                GtkStateFlags       state,
                                const GValue       *value)
{
146 147 148 149 150 151
  GtkCssStyleProperty *style;
  GtkCssValue *css_value;
  
  style = GTK_CSS_STYLE_PROPERTY (property);
  css_value = style->assign_value (style, value);

152
  _gtk_style_properties_set_property_by_property (props,
153
                                                  style,
154
                                                  state,
155 156
                                                  css_value);
  _gtk_css_value_unref (css_value);
157 158
}

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
static gboolean
_gtk_css_style_property_query_special_case (GtkCssStyleProperty *property,
                                            GValue              *value,
                                            GtkStyleQueryFunc    query_func,
                                            gpointer             query_data)
{
  GtkBorderStyle border_style;

  switch (property->id)
    {
      case GTK_CSS_PROPERTY_BORDER_TOP_WIDTH:
        border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_TOP_STYLE, query_data));
        if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
          {
            g_value_init (value, G_TYPE_INT);
            return TRUE;
          }
        break;
      case GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH:
        border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE, query_data));
        if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
          {
            g_value_init (value, G_TYPE_INT);
            return TRUE;
          }
        break;
      case GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH:
        border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE, query_data));
        if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
          {
            g_value_init (value, G_TYPE_INT);
            return TRUE;
          }
        break;
      case GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH:
        border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_BORDER_LEFT_STYLE, query_data));
        if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
          {
            g_value_init (value, G_TYPE_INT);
            return TRUE;
          }
        break;
      case GTK_CSS_PROPERTY_OUTLINE_WIDTH:
        border_style = _gtk_css_border_style_value_get (query_func (GTK_CSS_PROPERTY_OUTLINE_STYLE, query_data));
        if (border_style == GTK_BORDER_STYLE_NONE || border_style == GTK_BORDER_STYLE_HIDDEN)
          {
            g_value_init (value, G_TYPE_INT);
            return TRUE;
          }
        break;
      default:
        break;
    }

  return FALSE;
}

216
static void
217
_gtk_css_style_property_query (GtkStyleProperty   *property,
218
                               GValue             *value,
219 220
                               GtkStyleQueryFunc   query_func,
                               gpointer            query_data)
221
{
222
  GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
223
  GtkCssValue *css_value;
224
  
225 226 227 228
  /* I don't like this special case being here in this generic code path, but no idea where else to put it. */
  if (_gtk_css_style_property_query_special_case (style_property, value, query_func, query_data))
    return;

229
  css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
230 231 232 233
  if (css_value == NULL)
    css_value =_gtk_css_style_property_get_initial_value (style_property);

  style_property->query_value (style_property, css_value, value);
234 235
}

236
static GtkCssValue *
237
gtk_css_style_property_parse_value (GtkStyleProperty *property,
238
                                    GtkCssParser     *parser)
239
{
240
  GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
241

242 243 244 245 246
  if (_gtk_css_parser_try (parser, "initial", TRUE))
    {
      /* the initial value can be explicitly specified with the
       * ‘initial’ keyword which all properties accept.
       */
247
      return _gtk_css_initial_value_new ();
248 249 250 251 252 253 254 255 256
    }
  else if (_gtk_css_parser_try (parser, "inherit", TRUE))
    {
      /* All properties accept the ‘inherit’ value which
       * explicitly specifies that the value will be determined
       * by inheritance. The ‘inherit’ value can be used to
       * strengthen inherited values in the cascade, and it can
       * also be used on properties that are not normally inherited.
       */
257
      return _gtk_css_inherit_value_new ();
258
    }
Benjamin Otte's avatar
Benjamin Otte committed
259 260 261 262 263 264 265 266
  else if (_gtk_css_parser_try (parser, "unset", TRUE))
    {
      /* If the cascaded value of a property is the unset keyword,
       * then if it is an inherited property, this is treated as
       * inherit, and if it is not, this is treated as initial.
       */
      return _gtk_css_unset_value_new ();
    }
267

268
  return (* style_property->parse_value) (style_property, parser);
269 270
}

271 272 273
static void
_gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
{
274
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
275
  GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
276 277

  object_class->constructed = gtk_css_style_property_constructed;
278
  object_class->set_property = gtk_css_style_property_set_property;
279 280
  object_class->get_property = gtk_css_style_property_get_property;

281 282 283 284 285 286 287
  g_object_class_install_property (object_class,
                                   PROP_ANIMATED,
                                   g_param_spec_boolean ("animated",
                                                         P_("Animated"),
                                                         P_("Set if the value can be animated"),
                                                         FALSE,
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
288 289 290 291 292 293 294
  g_object_class_install_property (object_class,
                                   PROP_AFFECTS_SIZE,
                                   g_param_spec_boolean ("affects-size",
                                                         P_("Affects size"),
                                                         P_("Set if the value affects the sizing of elements"),
                                                         TRUE,
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
295 296 297 298 299 300 301
  g_object_class_install_property (object_class,
                                   PROP_AFFECTS_FONT,
                                   g_param_spec_boolean ("affects-font",
                                                         P_("Affects font"),
                                                         P_("Set if the value affects the font"),
                                                         FALSE,
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
302 303 304 305 306 307 308
  g_object_class_install_property (object_class,
                                   PROP_ID,
                                   g_param_spec_uint ("id",
                                                      P_("ID"),
                                                      P_("The numeric id for quick access"),
                                                      0, G_MAXUINT, 0,
                                                      G_PARAM_READABLE));
309 310 311 312 313 314 315 316 317 318 319 320
  g_object_class_install_property (object_class,
                                   PROP_INHERIT,
                                   g_param_spec_boolean ("inherit",
                                                         P_("Inherit"),
                                                         P_("Set if the value is inherited by default"),
                                                         FALSE,
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (object_class,
                                   PROP_INITIAL,
                                   g_param_spec_boxed ("initial-value",
                                                       P_("Initial value"),
                                                       P_("The initial specified value used for this property"),
321
                                                       GTK_TYPE_CSS_VALUE,
322
                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
323

324 325
  property_class->assign = _gtk_css_style_property_assign;
  property_class->query = _gtk_css_style_property_query;
326
  property_class->parse_value = gtk_css_style_property_parse_value;
327

328
  klass->style_properties = g_ptr_array_new ();
329 330

  _properties_affecting_size = _gtk_bitmask_new ();
331
  _properties_affecting_font = _gtk_bitmask_new ();
332 333

  gtk_css_style_property_class = klass;
334 335
}

336
static GtkCssValue *
337
gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
338
                                         GtkCssParser        *parser)
339
{
340 341
  g_assert_not_reached ();
  return NULL;
342 343
}

344
static void
345
_gtk_css_style_property_init (GtkCssStyleProperty *property)
346
{
347
  property->parse_value = gtk_css_style_property_real_parse_value;
348 349
}

350 351 352 353 354 355 356 357 358 359 360
/**
 * _gtk_css_style_property_get_n_properties:
 *
 * Gets the number of style properties. This number can increase when new
 * theme engines are loaded. Shorthand properties are not included here.
 *
 * Returns: The number of style properties.
 **/
guint
_gtk_css_style_property_get_n_properties (void)
{
361
  if (G_UNLIKELY (gtk_css_style_property_class == NULL))
362 363
    {
      _gtk_style_property_init_properties ();
364
      g_assert (gtk_css_style_property_class);
365
    }
366

367
  return gtk_css_style_property_class->style_properties->len;
368 369 370 371 372 373 374
}

/**
 * _gtk_css_style_property_lookup_by_id:
 * @id: the id of the property
 *
 * Gets the style property with the given id. All style properties (but not
375
 * shorthand properties) are indexable by id so that it’s easy to use arrays
376 377 378 379 380 381 382 383
 * when doing style lookups.
 *
 * Returns: (transfer none): The style property with the given id
 **/
GtkCssStyleProperty *
_gtk_css_style_property_lookup_by_id (guint id)
{

384
  if (G_UNLIKELY (gtk_css_style_property_class == NULL))
385 386
    {
      _gtk_style_property_init_properties ();
387
      g_assert (gtk_css_style_property_class);
388
    }
389

390 391 392
  g_return_val_if_fail (id < gtk_css_style_property_class->style_properties->len, NULL);

  return g_ptr_array_index (gtk_css_style_property_class->style_properties, id);
393 394
}

395 396 397 398
/**
 * _gtk_css_style_property_is_inherit:
 * @property: the property
 *
399 400 401
 * Queries if the given @property is inherited. See the
 * [CSS Documentation](http://www.w3.org/TR/css3-cascade/#inheritance)
 * for an explanation of this concept.
402 403 404 405 406 407
 *
 * Returns: %TRUE if the property is inherited by default.
 **/
gboolean
_gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
{
408
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
409 410 411 412

  return property->inherit;
}

413 414 415 416
/**
 * _gtk_css_style_property_is_animated:
 * @property: the property
 *
417 418 419
 * Queries if the given @property can be is animated. See the
 * [CSS Documentation](http://www.w3.org/TR/css3-transitions/#animatable-css)
 * for animatable properties.
420 421 422 423 424 425 426 427 428 429 430
 *
 * Returns: %TRUE if the property can be animated.
 **/
gboolean
_gtk_css_style_property_is_animated (GtkCssStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);

  return property->animated;
}

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
/**
 * _gtk_css_style_property_affects_size:
 * @property: the property
 *
 * Queries if the given @property affects the size of elements. This is
 * used for optimizations inside GTK, where a gtk_widget_queue_resize()
 * can be avoided if the property does not affect size.
 *
 * Returns: %TRUE if the property affects sizing of elements.
 **/
gboolean
_gtk_css_style_property_affects_size (GtkCssStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);

  return property->affects_size;
}
448 449 450 451 452 453 454

/**
 * _gtk_css_style_property_affects_font:
 * @property: the property
 *
 * Queries if the given @property affects the default font. This is
 * used for optimizations inside GTK, where clearing pango
455
 * layouts can be avoided if the font doesn’t change.
456 457 458 459 460 461 462 463 464 465 466
 *
 * Returns: %TRUE if the property affects the font.
 **/
gboolean
_gtk_css_style_property_affects_font (GtkCssStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);

  return property->affects_font;
}

467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
/**
 * _gtk_css_style_property_get_id:
 * @property: the property
 *
 * Gets the id for the given property. IDs are used to allow using arrays
 * for style lookups.
 *
 * Returns: The id of the property
 **/
guint
_gtk_css_style_property_get_id (GtkCssStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), 0);

  return property->id;
}

484 485 486 487
/**
 * _gtk_css_style_property_get_initial_value:
 * @property: the property
 *
488 489 490
 * Queries the initial value of the given @property. See the
 * [CSS Documentation](http://www.w3.org/TR/css3-cascade/#intial)
 * for an explanation of this concept.
491 492 493
 *
 * Returns: a reference to the initial value. The value will never change.
 **/
494
GtkCssValue *
495 496
_gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
{
497
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
498

499
  return property->initial_value;
500
}
501 502 503 504 505 506

gboolean
_gtk_css_style_property_changes_affect_size (const GtkBitmask *changes)
{
  return _gtk_bitmask_intersects (changes, _properties_affecting_size);
}
507 508 509 510 511 512

gboolean
_gtk_css_style_property_changes_affect_font (const GtkBitmask *changes)
{
  return _gtk_bitmask_intersects (changes, _properties_affecting_font);
}