gtkstyleproperty.c 9.08 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* GTK - The GIMP Toolkit
 * Copyright (C) 2010 Carlos Garnacho <carlosg@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19
 */

#include "config.h"

20
#include "gtkstylepropertyprivate.h"
21 22

#include "gtkcssprovider.h"
Benjamin Otte's avatar
Benjamin Otte committed
23
#include "gtkcssparserprivate.h"
24
#include "gtkcssshorthandpropertyprivate.h"
25
#include "gtkcssstylefuncsprivate.h"
26
#include "gtkcssstylepropertyprivate.h"
27
#include "gtkcsstypesprivate.h"
28
#include "gtkintl.h"
29
#include "gtkprivatetypebuiltins.h"
30
#include "gtkstylepropertiesprivate.h"
31

32 33
enum {
  PROP_0,
34 35
  PROP_NAME,
  PROP_VALUE_TYPE
36 37
};

38
G_DEFINE_ABSTRACT_TYPE (GtkStyleProperty, _gtk_style_property, G_TYPE_OBJECT)
39 40 41 42 43 44

static void
gtk_style_property_finalize (GObject *object)
{
  GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);

45
  g_warning ("finalizing %s `%s', how could this happen?", G_OBJECT_TYPE_NAME (object), property->name);
46 47 48 49

  G_OBJECT_CLASS (_gtk_style_property_parent_class)->finalize (object);
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
static void
gtk_style_property_set_property (GObject      *object,
                                 guint         prop_id,
                                 const GValue *value,
                                 GParamSpec   *pspec)
{
  GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);
  GtkStylePropertyClass *klass = GTK_STYLE_PROPERTY_GET_CLASS (property);

  switch (prop_id)
    {
    case PROP_NAME:
      property->name = g_value_dup_string (value);
      g_assert (property->name);
      g_assert (g_hash_table_lookup (klass->properties, property->name) == NULL);
      g_hash_table_insert (klass->properties, property->name, property);
      break;
67 68 69
    case PROP_VALUE_TYPE:
      property->value_type = g_value_get_gtype (value);
      break;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_style_property_get_property (GObject    *object,
                                 guint       prop_id,
                                 GValue     *value,
                                 GParamSpec *pspec)
{
  GtkStyleProperty *property = GTK_STYLE_PROPERTY (object);

  switch (prop_id)
    {
    case PROP_NAME:
      g_value_set_string (value, property->name);
      break;
89 90 91
    case PROP_VALUE_TYPE:
      g_value_set_gtype (value, property->value_type);
      break;
92 93 94 95 96 97
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

98 99 100 101 102 103
static void
_gtk_style_property_class_init (GtkStylePropertyClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_style_property_finalize;
104 105 106 107 108 109 110 111 112 113
  object_class->set_property = gtk_style_property_set_property;
  object_class->get_property = gtk_style_property_get_property;

  g_object_class_install_property (object_class,
                                   PROP_NAME,
                                   g_param_spec_string ("name",
                                                        P_("Property name"),
                                                        P_("The name of the property"),
                                                        NULL,
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
114 115 116 117 118 119 120
  g_object_class_install_property (object_class,
                                   PROP_VALUE_TYPE,
                                   g_param_spec_gtype ("value-type",
                                                       P_("Value type"),
                                                       P_("The value type returned by GtkStyleContext"),
                                                       G_TYPE_NONE,
                                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
121 122

  klass->properties = g_hash_table_new (g_str_hash, g_str_equal);
123 124 125 126 127
}

static void
_gtk_style_property_init (GtkStyleProperty *property)
{
128
  property->value_type = G_TYPE_NONE;
129 130
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/**
 * _gtk_style_property_parse_value:
 * @property: the property
 * @value: an uninitialized value
 * @parser: the parser to parse from
 * @base: the base file for @aprser
 *
 * Tries to parse the given @property from the given @parser into
 * @value. The type that @value will be assigned is dependant on
 * the parser and no assumptions must be made about it. If the
 * parsing fails, %FALSE will be returned and @value will be
 * left uninitialized.
 *
 * Only if @property is a #GtkCssShorthandProperty, the @value will
 * always contain a #GValueArray with the values to be used for
 * the subproperties.
 *
 * Returns: %TRUE on success
 **/
150 151 152 153 154
gboolean
_gtk_style_property_parse_value (GtkStyleProperty *property,
                                 GValue           *value,
                                 GtkCssParser     *parser,
                                 GFile            *base)
155
{
156 157
  GtkStylePropertyClass *klass;

158
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), FALSE);
159 160
  g_return_val_if_fail (value != NULL, FALSE);
  g_return_val_if_fail (parser != NULL, FALSE);
161

162 163 164 165 166
  klass = GTK_STYLE_PROPERTY_GET_CLASS (property);

  return klass->parse_value (property, value, parser, base);
}

167 168 169 170 171 172 173 174 175 176 177 178
/**
 * _gtk_style_property_assign:
 * @property: the property
 * @props: The properties to assign to
 * @state: The state to assign
 * @value: (out): the #GValue with the value to be
 *     assigned
 *
 * This function is called by gtk_style_properties_set() and in
 * turn gtk_style_context_set() and similar functions to set the
 * value from code using old APIs.
 **/
179 180 181 182 183 184
void
_gtk_style_property_assign (GtkStyleProperty   *property,
                            GtkStyleProperties *props,
                            GtkStateFlags       state,
                            const GValue       *value)
{
185 186
  GtkStylePropertyClass *klass;

187 188 189 190
  g_return_if_fail (GTK_IS_STYLE_PROPERTY (property));
  g_return_if_fail (GTK_IS_STYLE_PROPERTIES (props));
  g_return_if_fail (value != NULL);

191
  klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
192

193
  klass->assign (property, props, state, value);
194 195
}

196 197 198 199 200
/**
 * _gtk_style_property_query:
 * @property: the property
 * @value: (out): an uninitialized #GValue to be filled with the
 *   contents of the lookup
201 202
 * @query_func: The function to use to query properties
 * @query_data: The data to pass to @query_func
203 204 205 206 207
 *
 * This function is called by gtk_style_properties_get() and in
 * turn gtk_style_context_get() and similar functions to get the
 * value to return to code using old APIs.
 **/
208
GtkCssValue *
209 210 211
_gtk_style_property_query (GtkStyleProperty  *property,
                           GtkStyleQueryFunc  query_func,
                           gpointer           query_data)
212
{
213
  GtkStylePropertyClass *klass;
214

215 216
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
  g_return_val_if_fail (query_func != NULL, NULL);
217

218
  klass = GTK_STYLE_PROPERTY_GET_CLASS (property);
219

220
  return klass->query (property, query_func, query_data);
221 222
}

223 224
void
_gtk_style_property_init_properties (void)
225
{
226
  static gboolean initialized = FALSE;
227

228
  if (G_LIKELY (initialized))
229 230
    return;

231
  initialized = TRUE;
232

233
  _gtk_css_style_property_init_properties ();
234 235
  /* initialize shorthands last, they depend on the real properties existing */
  _gtk_css_shorthand_property_init_properties ();
236 237
}

238 239 240 241 242 243 244 245 246 247
/**
 * _gtk_style_property_lookup:
 * @name: name of the property to lookup
 *
 * Looks up the CSS property with the given @name. If no such
 * property exists, %NULL is returned.
 *
 * Returns: (transfer none): The property or %NULL if no
 *     property with the given name exists.
 **/
248
GtkStyleProperty *
249 250
_gtk_style_property_lookup (const char *name)
{
251 252 253 254
  GtkStylePropertyClass *klass;

  g_return_val_if_fail (name != NULL, NULL);

255
  _gtk_style_property_init_properties ();
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

  klass = g_type_class_peek (GTK_TYPE_STYLE_PROPERTY);

  return g_hash_table_lookup (klass->properties, name);
}

/**
 * _gtk_style_property_get_name:
 * @property: the property to query
 *
 * Gets the name of the given property.
 *
 * Returns: the name of the property
 **/
const char *
_gtk_style_property_get_name (GtkStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), NULL);
274

275
  return property->name;
276 277
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
/**
 * _gtk_style_property_get_value_type:
 * @property: the property to query
 *
 * Gets the value type of the @property, if the property is usable
 * in public API via _gtk_style_property_assign() and
 * _gtk_style_property_query(). If the @property is not usable in that
 * way, %G_TYPE_NONE is returned.
 *
 * Returns: the value type in use or %G_TYPE_NONE if none.
 **/
GType
_gtk_style_property_get_value_type (GtkStyleProperty *property)
{
  g_return_val_if_fail (GTK_IS_STYLE_PROPERTY (property), G_TYPE_NONE);

  return property->value_type;
}