gtkcssshorthandproperty.c 6.84 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 "gtkcssshorthandpropertyprivate.h"

24 25 26
#include "gtkcssarrayvalueprivate.h"
#include "gtkcssinheritvalueprivate.h"
#include "gtkcssinitialvalueprivate.h"
27
#include "gtkcssstylefuncsprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
28
#include "gtkcssunsetvalueprivate.h"
29 30 31 32 33 34 35
#include "gtkintl.h"

enum {
  PROP_0,
  PROP_SUBPROPERTIES,
};

36 37
G_DEFINE_TYPE (GtkCssShorthandProperty, _gtk_css_shorthand_property, GTK_TYPE_STYLE_PROPERTY)

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static void
gtk_css_shorthand_property_set_property (GObject      *object,
                                         guint         prop_id,
                                         const GValue *value,
                                         GParamSpec   *pspec)
{
  GtkCssShorthandProperty *property = GTK_CSS_SHORTHAND_PROPERTY (object);
  const char **subproperties;
  guint i;

  switch (prop_id)
    {
    case PROP_SUBPROPERTIES:
      subproperties = g_value_get_boxed (value);
      g_assert (subproperties);
      for (i = 0; subproperties[i] != NULL; i++)
        {
          GtkStyleProperty *subproperty = _gtk_style_property_lookup (subproperties[i]);
          g_assert (GTK_IS_CSS_STYLE_PROPERTY (subproperty));
          g_ptr_array_add (property->subproperties, subproperty);
        }
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

66 67 68 69 70 71
static void
_gtk_css_shorthand_property_assign (GtkStyleProperty   *property,
                                    GtkStyleProperties *props,
                                    GtkStateFlags       state,
                                    const GValue       *value)
{
72
  GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
73

74
  shorthand->assign (shorthand, props, state, value);
75 76
}

77
static void
78
_gtk_css_shorthand_property_query (GtkStyleProperty   *property,
79
                                   GValue             *value,
80 81
                                   GtkStyleQueryFunc   query_func,
                                   gpointer            query_data)
82
{
83 84
  GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);

85
  shorthand->query (shorthand, value, query_func, query_data);
86 87
}

88
static GtkCssValue *
89
gtk_css_shorthand_property_parse_value (GtkStyleProperty *property,
90
                                        GtkCssParser     *parser)
91 92
{
  GtkCssShorthandProperty *shorthand = GTK_CSS_SHORTHAND_PROPERTY (property);
93 94
  GtkCssValue **data;
  GtkCssValue *result;
95 96
  guint i;

97
  data = g_new0 (GtkCssValue *, shorthand->subproperties->len);
98 99 100 101 102 103 104 105

  if (_gtk_css_parser_try (parser, "initial", TRUE))
    {
      /* the initial value can be explicitly specified with the
       * ‘initial’ keyword which all properties accept.
       */
      for (i = 0; i < shorthand->subproperties->len; i++)
        {
106
          data[i] = _gtk_css_initial_value_new ();
107 108 109 110 111 112 113 114 115 116 117 118
        }
    }
  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.
       */
      for (i = 0; i < shorthand->subproperties->len; i++)
        {
119
          data[i] = _gtk_css_inherit_value_new ();
120 121
        }
    }
Benjamin Otte's avatar
Benjamin Otte committed
122 123 124 125 126 127 128 129 130 131 132
  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.
       */
      for (i = 0; i < shorthand->subproperties->len; i++)
        {
          data[i] = _gtk_css_unset_value_new ();
        }
    }
133
  else if (!shorthand->parse (shorthand, data, parser))
134
    {
135 136 137 138 139 140 141
      for (i = 0; i < shorthand->subproperties->len; i++)
        {
          if (data[i] != NULL)
            _gtk_css_value_unref (data[i]);
        }
      g_free (data);
      return NULL;
142 143
    }

144 145 146 147 148
  /* All values that aren't set by the parse func are set to their
   * default values here.
   * XXX: Is the default always initial or can it be inherit? */
  for (i = 0; i < shorthand->subproperties->len; i++)
    {
149 150
      if (data[i] == NULL)
        data[i] = _gtk_css_initial_value_new ();
151 152
    }

153
  result = _gtk_css_array_value_new_from_array (data, shorthand->subproperties->len);
154 155 156
  g_free (data);
  
  return result;
157 158
}

159 160 161
static void
_gtk_css_shorthand_property_class_init (GtkCssShorthandPropertyClass *klass)
{
162
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
163
  GtkStylePropertyClass *property_class = GTK_STYLE_PROPERTY_CLASS (klass);
164 165 166 167 168 169 170 171 172 173

  object_class->set_property = gtk_css_shorthand_property_set_property;

  g_object_class_install_property (object_class,
                                   PROP_SUBPROPERTIES,
                                   g_param_spec_boxed ("subproperties",
                                                       P_("Subproperties"),
                                                       P_("The list of subproperties"),
                                                       G_TYPE_STRV,
                                                       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
174 175 176

  property_class->assign = _gtk_css_shorthand_property_assign;
  property_class->query = _gtk_css_shorthand_property_query;
177 178 179
  property_class->parse_value = gtk_css_shorthand_property_parse_value;
}

180 181 182
static void
_gtk_css_shorthand_property_init (GtkCssShorthandProperty *shorthand)
{
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  shorthand->subproperties = g_ptr_array_new_with_free_func (g_object_unref);
}

GtkCssStyleProperty *
_gtk_css_shorthand_property_get_subproperty (GtkCssShorthandProperty *shorthand,
                                             guint                    property)
{
  g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), NULL);
  g_return_val_if_fail (property < shorthand->subproperties->len, NULL);

  return g_ptr_array_index (shorthand->subproperties, property);
}

guint
_gtk_css_shorthand_property_get_n_subproperties (GtkCssShorthandProperty *shorthand)
{
  g_return_val_if_fail (GTK_IS_CSS_SHORTHAND_PROPERTY (shorthand), 0);
  
  return shorthand->subproperties->len;
202 203
}