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

#include "config.h"

#include "gtkcsscustompropertyprivate.h"

#include <string.h>

26
#include "gtkcssstylefuncsprivate.h"
27 28 29 30
#include "gtkthemingengine.h"

G_DEFINE_TYPE (GtkCssCustomProperty, _gtk_css_custom_property, GTK_TYPE_CSS_STYLE_PROPERTY)

31 32 33 34 35 36 37 38 39 40
static GType
gtk_css_custom_property_get_specified_type (GParamSpec *pspec)
{
  if (pspec->value_type == GDK_TYPE_RGBA ||
      pspec->value_type == GDK_TYPE_COLOR)
    return GTK_TYPE_SYMBOLIC_COLOR;
  else
    return pspec->value_type;
}

41
static GtkCssValue *
42 43 44 45
gtk_css_custom_property_parse_value (GtkStyleProperty *property,
                                     GtkCssParser     *parser,
                                     GFile            *base)
{
46
  GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);
47 48
  GValue value = G_VALUE_INIT;
  GtkCssValue *result;
49 50
  gboolean success;

51
  if (custom->property_parse_func)
52 53 54 55
    {
      GError *error = NULL;
      char *value_str;
      
56
      g_value_init (&value, _gtk_style_property_get_value_type (property));
57

58 59 60
      value_str = _gtk_css_parser_read_value (parser);
      if (value_str != NULL)
        {
61
          success = (* custom->property_parse_func) (value_str, &value, &error);
62 63 64 65 66 67
          g_free (value_str);
        }
      else
        success = FALSE;
    }
  else
68
    {
69
      g_value_init (&value, gtk_css_custom_property_get_specified_type (custom->pspec));
70

71
      success = _gtk_css_style_parse_value (&value, parser, base);
72
    }
73 74

  if (!success)
75 76 77 78 79 80 81
    {
      g_value_unset (&value);
      return NULL;
    }

  result = _gtk_css_value_new_from_gvalue (&value);
  g_value_unset (&value);
82

83
  return result;
84 85
}

86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
static void
gtk_css_custom_property_query (GtkStyleProperty   *property,
                               GValue             *value,
                               GtkStyleQueryFunc   query_func,
                               gpointer            query_data)
{
  GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (property);
  GtkCssValue *css_value;
  
  css_value = (* query_func) (_gtk_css_style_property_get_id (style), query_data);
  if (css_value == NULL)
    css_value =_gtk_css_style_property_get_initial_value (style);

  _gtk_css_value_init_gvalue (css_value, value);
  g_assert (GTK_CSS_CUSTOM_PROPERTY (property)->pspec->value_type == G_VALUE_TYPE (value));
}

103 104 105
static void
_gtk_css_custom_property_class_init (GtkCssCustomPropertyClass *klass)
{
106 107 108
  GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);

  property_class->parse_value = gtk_css_custom_property_parse_value;
109
  property_class->query = gtk_css_custom_property_query;
110 111
}

112 113 114 115 116 117 118 119 120
static GtkCssValue *
gtk_css_custom_property_compute_value (GtkCssStyleProperty *property,
                                       GtkStyleContext     *context,
                                       GtkCssValue         *specified)
{
  GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (property);

  return _gtk_css_style_compute_value (context, custom->pspec->value_type, specified);
}
121 122

static void
123
_gtk_css_custom_property_init (GtkCssCustomProperty *custom)
124
{
125 126 127
  GtkCssStyleProperty *style = GTK_CSS_STYLE_PROPERTY (custom);

  style->compute_value = gtk_css_custom_property_compute_value;
128 129
}

130 131
static GtkCssValue *
gtk_css_custom_property_create_initial_value (GParamSpec *pspec)
132
{
133
  GValue value = G_VALUE_INIT;
134
  GtkCssValue *result;
135

136
  g_value_init (&value, pspec->value_type);
137 138

  if (pspec->value_type == GTK_TYPE_THEMING_ENGINE)
139
    g_value_set_object (&value, gtk_theming_engine_load (NULL));
140
  else if (pspec->value_type == PANGO_TYPE_FONT_DESCRIPTION)
141
    g_value_take_boxed (&value, pango_font_description_from_string ("Sans 10"));
142
  else if (pspec->value_type == GDK_TYPE_RGBA)
143 144 145
    {
      GdkRGBA color;
      gdk_rgba_parse (&color, "pink");
146
      g_value_set_boxed (&value, &color);
147 148 149 150 151
    }
  else if (pspec->value_type == GDK_TYPE_COLOR)
    {
      GdkColor color;
      gdk_color_parse ("pink", &color);
152
      g_value_set_boxed (&value, &color);
153 154 155
    }
  else if (pspec->value_type == GTK_TYPE_BORDER)
    {
156
      g_value_take_boxed (&value, gtk_border_new ());
157 158
    }
  else
159 160
    g_param_value_set_default (pspec, &value);

161 162 163 164
  result = _gtk_css_value_new_from_gvalue (&value);
  g_value_unset (&value);

  return result;
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
}

/* Property registration functions */

/**
 * gtk_theming_engine_register_property: (skip)
 * @name_space: namespace for the property name
 * @parse_func: parsing function to use, or %NULL
 * @pspec: the #GParamSpec for the new property
 *
 * Registers a property so it can be used in the CSS file format,
 * on the CSS file the property will look like
 * "-${@name_space}-${property_name}". being
 * ${property_name} the given to @pspec. @name_space will usually
 * be the theme engine name.
 *
 * For any type a @parse_func may be provided, being this function
 * used for turning any property value (between ':' and ';') in
 * CSS to the #GValue needed. For basic types there is already
 * builtin parsing support, so %NULL may be provided for these
 * cases.
 *
 * <note>
 * Engines must ensure property registration happens exactly once,
 * usually GTK+ deals with theming engines as singletons, so this
 * should be guaranteed to happen once, but bear this in mind
 * when creating #GtkThemeEngine<!-- -->s yourself.
 * </note>
 *
 * <note>
 * In order to make use of the custom registered properties in
 * the CSS file, make sure the engine is loaded first by specifying
 * the engine property, either in a previous rule or within the same
 * one.
 * <programlisting>
 * &ast; {
 *     engine: someengine;
 *     -SomeEngine-custom-property: 2;
 * }
 * </programlisting>
 * </note>
 *
 * Since: 3.0
 **/
void
gtk_theming_engine_register_property (const gchar            *name_space,
                                      GtkStylePropertyParser  parse_func,
                                      GParamSpec             *pspec)
{
214
  GtkCssCustomProperty *node;
215
  GtkCssValue *initial;
216 217 218 219 220 221 222
  gchar *name;

  g_return_if_fail (name_space != NULL);
  g_return_if_fail (strchr (name_space, ' ') == NULL);
  g_return_if_fail (G_IS_PARAM_SPEC (pspec));

  name = g_strdup_printf ("-%s-%s", name_space, pspec->name);
223 224 225 226 227 228 229 230 231

  /* This also initializes the default properties */
  if (_gtk_style_property_lookup (pspec->name))
    {
      g_warning ("a property with name '%s' already exists", name);
      g_free (name);
      return;
    }
  
232
  initial = gtk_css_custom_property_create_initial_value (pspec);
233 234

  node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
235
                       "initial-value", initial,
236 237 238
                       "name", name,
                       "value-type", pspec->value_type,
                       NULL);
239
  node->pspec = pspec;
240 241
  node->property_parse_func = parse_func;

242
  _gtk_css_value_unref (initial);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
  g_free (name);
}

/**
 * gtk_style_properties_register_property: (skip)
 * @parse_func: parsing function to use, or %NULL
 * @pspec: the #GParamSpec for the new property
 *
 * Registers a property so it can be used in the CSS file format.
 * This function is the low-level equivalent of
 * gtk_theming_engine_register_property(), if you are implementing
 * a theming engine, you want to use that function instead.
 *
 * Since: 3.0
 **/
void
gtk_style_properties_register_property (GtkStylePropertyParser  parse_func,
                                        GParamSpec             *pspec)
{
262
  GtkCssCustomProperty *node;
263
  GtkCssValue *initial;
264 265 266

  g_return_if_fail (G_IS_PARAM_SPEC (pspec));

267 268 269 270 271 272 273
  /* This also initializes the default properties */
  if (_gtk_style_property_lookup (pspec->name))
    {
      g_warning ("a property with name '%s' already exists", pspec->name);
      return;
    }
  
274
  initial = gtk_css_custom_property_create_initial_value (pspec);
275 276

  node = g_object_new (GTK_TYPE_CSS_CUSTOM_PROPERTY,
277
                       "initial-value", initial,
278 279 280
                       "name", pspec->name,
                       "value-type", pspec->value_type,
                       NULL);
281
  node->pspec = pspec;
282 283
  node->property_parse_func = parse_func;

284
  _gtk_css_value_unref (initial);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}

/**
 * gtk_style_properties_lookup_property: (skip)
 * @property_name: property name to look up
 * @parse_func: (out): return location for the parse function
 * @pspec: (out) (transfer none): return location for the #GParamSpec
 *
 * Returns %TRUE if a property has been registered, if @pspec or
 * @parse_func are not %NULL, the #GParamSpec and parsing function
 * will be respectively returned.
 *
 * Returns: %TRUE if the property is registered, %FALSE otherwise
 *
 * Since: 3.0
 **/
gboolean
gtk_style_properties_lookup_property (const gchar             *property_name,
                                      GtkStylePropertyParser  *parse_func,
                                      GParamSpec             **pspec)
{
  GtkStyleProperty *node;
  gboolean found = FALSE;

  g_return_val_if_fail (property_name != NULL, FALSE);

  node = _gtk_style_property_lookup (property_name);

313
  if (GTK_IS_CSS_CUSTOM_PROPERTY (node))
314
    {
315 316
      GtkCssCustomProperty *custom = GTK_CSS_CUSTOM_PROPERTY (node);

317
      if (pspec)
318
        *pspec = custom->pspec;
319 320

      if (parse_func)
321
        *parse_func = custom->property_parse_func;
322 323 324 325 326 327 328

      found = TRUE;
    }

  return found;
}