gtkcssstyleproperty.c 14.1 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 25
#include "gtkcssinheritvalueprivate.h"
#include "gtkcssinitialvalueprivate.h"
26 27
#include "gtkcssstylefuncsprivate.h"
#include "gtkcsstypesprivate.h"
28
#include "gtkintl.h"
29
#include "gtkprivatetypebuiltins.h"
30 31
#include "gtkstylepropertiesprivate.h"

32 33 34 35 36
/* 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"

37 38
enum {
  PROP_0,
39
  PROP_ANIMATED,
40
  PROP_ID,
41 42
  PROP_INHERIT,
  PROP_INITIAL
43 44
};

45 46
G_DEFINE_TYPE (GtkCssStyleProperty, _gtk_css_style_property, GTK_TYPE_STYLE_PROPERTY)

47 48 49 50 51 52 53 54 55 56 57 58
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);

  G_OBJECT_CLASS (_gtk_css_style_property_parent_class)->constructed (object);
}

59 60 61 62 63 64 65 66 67 68
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)
    {
69 70 71
    case PROP_ANIMATED:
      property->animated = g_value_get_boolean (value);
      break;
72 73 74 75
    case PROP_INHERIT:
      property->inherit = g_value_get_boolean (value);
      break;
    case PROP_INITIAL:
76 77
      property->initial_value = g_value_dup_boxed (value);
      g_assert (property->initial_value != NULL);
78 79 80 81 82 83 84
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

85 86 87 88 89 90 91 92 93 94
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)
    {
95 96 97
    case PROP_ANIMATED:
      g_value_set_boolean (value, property->animated);
      break;
98 99 100
    case PROP_ID:
      g_value_set_boolean (value, property->id);
      break;
101 102 103 104
    case PROP_INHERIT:
      g_value_set_boolean (value, property->inherit);
      break;
    case PROP_INITIAL:
105
      g_value_set_boxed (value, property->initial_value);
106
      break;
107 108 109 110 111 112
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

113 114 115 116 117 118
static void
_gtk_css_style_property_assign (GtkStyleProperty   *property,
                                GtkStyleProperties *props,
                                GtkStateFlags       state,
                                const GValue       *value)
{
119 120 121 122 123 124
  GtkCssStyleProperty *style;
  GtkCssValue *css_value;
  
  style = GTK_CSS_STYLE_PROPERTY (property);
  css_value = style->assign_value (style, value);

125
  _gtk_style_properties_set_property_by_property (props,
126
                                                  style,
127
                                                  state,
128 129
                                                  css_value);
  _gtk_css_value_unref (css_value);
130 131
}

132
static void
133
_gtk_css_style_property_query (GtkStyleProperty   *property,
134
                               GValue             *value,
135 136
                               GtkStyleQueryFunc   query_func,
                               gpointer            query_data)
137
{
138
  GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
139
  GtkCssValue *css_value;
140
  
141
  css_value = (* query_func) (GTK_CSS_STYLE_PROPERTY (property)->id, query_data);
142 143 144 145
  if (css_value == NULL)
    css_value =_gtk_css_style_property_get_initial_value (style_property);

  style_property->query_value (style_property, css_value, value);
146 147
}

148
static GtkCssValue *
149
gtk_css_style_property_parse_value (GtkStyleProperty *property,
150
                                    GtkCssParser     *parser)
151
{
152
  GtkCssStyleProperty *style_property = GTK_CSS_STYLE_PROPERTY (property);
153

154 155 156 157 158
  if (_gtk_css_parser_try (parser, "initial", TRUE))
    {
      /* the initial value can be explicitly specified with the
       * ‘initial’ keyword which all properties accept.
       */
159
      return _gtk_css_initial_value_new ();
160 161 162 163 164 165 166 167 168
    }
  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.
       */
169
      return _gtk_css_inherit_value_new ();
170
    }
171

172
  return (* style_property->parse_value) (style_property, parser);
173 174
}

175 176 177
static void
_gtk_css_style_property_class_init (GtkCssStylePropertyClass *klass)
{
178
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
179
  GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
180 181

  object_class->constructed = gtk_css_style_property_constructed;
182
  object_class->set_property = gtk_css_style_property_set_property;
183 184
  object_class->get_property = gtk_css_style_property_get_property;

185 186 187 188 189 190 191
  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));
192 193 194 195 196 197 198
  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));
199 200 201 202 203 204 205 206 207 208 209 210
  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"),
211
                                                       GTK_TYPE_CSS_VALUE,
212
                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
213

214 215
  property_class->assign = _gtk_css_style_property_assign;
  property_class->query = _gtk_css_style_property_query;
216
  property_class->parse_value = gtk_css_style_property_parse_value;
217

218
  klass->style_properties = g_ptr_array_new ();
219 220
}

221
static GtkCssValue *
222
gtk_css_style_property_real_parse_value (GtkCssStyleProperty *property,
223
                                         GtkCssParser        *parser)
224
{
225 226
  g_assert_not_reached ();
  return NULL;
227 228 229 230
}

static void
gtk_css_style_property_real_print_value (GtkCssStyleProperty *property,
231
                                         const GtkCssValue   *value,
232 233
                                         GString             *string)
{
234
  _gtk_css_value_print (value, string);
235 236
}

237
static GtkCssValue *
238 239
gtk_css_style_property_real_compute_value (GtkCssStyleProperty *property,
                                           GtkStyleContext     *context,
240
                                           GtkCssValue         *specified)
241
{
242
  return _gtk_css_value_ref (specified);
243
}
244

245
static void
246
_gtk_css_style_property_init (GtkCssStyleProperty *property)
247
{
248 249
  property->parse_value = gtk_css_style_property_real_parse_value;
  property->print_value = gtk_css_style_property_real_print_value;
250
  property->compute_value = gtk_css_style_property_real_compute_value;
251 252
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * _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)
{
  GtkCssStylePropertyClass *klass;

  klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
267 268 269 270 271 272
  if (G_UNLIKELY (klass == NULL))
    {
      _gtk_style_property_init_properties ();
      klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
      g_assert (klass);
    }
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292

  return klass->style_properties->len;
}

/**
 * _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
 * shorthand properties) are indexable by id so that it's easy to use arrays
 * when doing style lookups.
 *
 * Returns: (transfer none): The style property with the given id
 **/
GtkCssStyleProperty *
_gtk_css_style_property_lookup_by_id (guint id)
{
  GtkCssStylePropertyClass *klass;

  klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
293 294 295 296 297 298 299
  if (G_UNLIKELY (klass == NULL))
    {
      _gtk_style_property_init_properties ();
      klass = g_type_class_peek (GTK_TYPE_CSS_STYLE_PROPERTY);
      g_assert (klass);
    }
  g_return_val_if_fail (id < klass->style_properties->len, NULL);
300 301 302 303

  return g_ptr_array_index (klass->style_properties, id);
}

304 305 306 307 308 309 310 311 312 313 314 315 316
/**
 * _gtk_css_style_property_is_inherit:
 * @property: the property
 *
 * Queries if the given @property is inherited. See
 * <ulink url="http://www.w3.org/TR/css3-cascade/#inheritance>
 * the CSS documentation</ulink> for an explanation of this concept.
 *
 * Returns: %TRUE if the property is inherited by default.
 **/
gboolean
_gtk_css_style_property_is_inherit (GtkCssStyleProperty *property)
{
317
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), FALSE);
318 319 320 321

  return property->inherit;
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
/**
 * _gtk_css_style_property_is_animated:
 * @property: the property
 *
 * Queries if the given @property can be is animated. See
 * <ulink url="http://www.w3.org/TR/css3-transitions/#animatable-css>
 * the CSS documentation</ulink> for animatable properties.
 *
 * 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;
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
/**
 * _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;
}

357 358 359 360 361 362 363 364 365 366
/**
 * _gtk_css_style_property_get_initial_value:
 * @property: the property
 *
 * Queries the initial value of the given @property. See
 * <ulink url="http://www.w3.org/TR/css3-cascade/#intial>
 * the CSS documentation</ulink> for an explanation of this concept.
 *
 * Returns: a reference to the initial value. The value will never change.
 **/
367
GtkCssValue *
368 369
_gtk_css_style_property_get_initial_value (GtkCssStyleProperty *property)
{
370
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
371

372
  return property->initial_value;
373 374
}

Benjamin Otte's avatar
Benjamin Otte committed
375 376 377 378 379 380 381 382 383 384 385 386
/**
 * _gtk_css_style_property_compute_value:
 * @property: the property
 * @computed: (out): an uninitialized value to be filled with the result
 * @context: the context to use for resolving
 * @specified: the value to compute from
 *
 * Converts the @specified value into the @computed value using the
 * information in @context. This step is explained in detail in
 * <ulink url="http://www.w3.org/TR/css3-cascade/#computed>
 * the CSS documentation</ulink>.
 **/
387
GtkCssValue *
Benjamin Otte's avatar
Benjamin Otte committed
388 389
_gtk_css_style_property_compute_value (GtkCssStyleProperty *property,
                                       GtkStyleContext     *context,
390
                                       GtkCssValue         *specified)
Benjamin Otte's avatar
Benjamin Otte committed
391
{
392 393
  g_return_val_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property), NULL);
  g_return_val_if_fail (GTK_IS_STYLE_CONTEXT (context), NULL);
394

395
  return property->compute_value (property, context, specified);
Benjamin Otte's avatar
Benjamin Otte committed
396 397
}

398 399 400 401 402 403 404 405 406 407 408 409
/**
 * _gtk_css_style_property_print_value:
 * @property: the property
 * @value: the value to print
 * @string: the string to print to
 *
 * Prints @value to the given @string in CSS format. The @value must be a
 * valid specified value as parsed using the parse functions or as assigned
 * via _gtk_style_property_assign().
 **/
void
_gtk_css_style_property_print_value (GtkCssStyleProperty    *property,
410
                                     GtkCssValue            *value,
411 412 413
                                     GString                *string)
{
  g_return_if_fail (GTK_IS_CSS_STYLE_PROPERTY (property));
414
  g_return_if_fail (value != NULL);
415 416
  g_return_if_fail (string != NULL);

417 418 419
  if (_gtk_css_value_is_inherit (value) ||
      _gtk_css_value_is_initial (value))
    _gtk_css_value_print (value, string);
420
  else
421
    property->print_value (property, value, string);
422
}