gtkcssvalue.c 4.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* GTK - The GIMP Toolkit
 * Copyright (C) 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 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
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

20
#include "gtkprivate.h"
21 22
#include "gtkcssvalueprivate.h"

23
#include "gtkcssstyleprivate.h"
24 25
#include "gtkstyleproviderprivate.h"

26
struct _GtkCssValue {
27 28 29
  GTK_CSS_VALUE_BASE
};

30 31
G_DEFINE_BOXED_TYPE (GtkCssValue, _gtk_css_value, _gtk_css_value_ref, _gtk_css_value_unref)

32 33 34 35 36 37 38 39 40 41 42 43 44 45
GtkCssValue *
_gtk_css_value_alloc (const GtkCssValueClass *klass,
                      gsize                   size)
{
  GtkCssValue *value;

  value = g_slice_alloc0 (size);

  value->class = klass;
  value->ref_count = 1;

  return value;
}

46 47 48
GtkCssValue *
_gtk_css_value_ref (GtkCssValue *value)
{
49
  gtk_internal_return_val_if_fail (value != NULL, NULL);
50

51
  value->ref_count += 1;
52 53 54 55 56 57 58 59 60 61

  return value;
}

void
_gtk_css_value_unref (GtkCssValue *value)
{
  if (value == NULL)
    return;

62 63
  value->ref_count -= 1;
  if (value->ref_count > 0)
64 65
    return;

66
  value->class->free (value);
67 68
}

69 70 71 72
/**
 * _gtk_css_value_compute:
 * @value: the value to compute from
 * @property_id: the ID of the property to compute
73
 * @provider: Style provider for looking up extra information
Matthias Clasen's avatar
Matthias Clasen committed
74 75
 * @style: Style to compute for
 * @parent_style: parent style to use for inherited values
76 77 78
 *
 * Converts the specified @value into the computed value for the CSS
 * property given by @property_id using the information in @context.
79 80
 * This step is explained in detail in the
 * [CSS Documentation](http://www.w3.org/TR/css3-cascade/#computed).
81
 *
82
 * Returns: the computed value
83
 **/
84
GtkCssValue *
85 86 87
_gtk_css_value_compute (GtkCssValue             *value,
                        guint                    property_id,
                        GtkStyleProviderPrivate *provider,
88
                        GtkCssStyle             *style,
89
                        GtkCssStyle             *parent_style)
90
{
91

92 93
  gtk_internal_return_val_if_fail (value != NULL, NULL);
  gtk_internal_return_val_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider), NULL);
94 95
  gtk_internal_return_val_if_fail (GTK_IS_CSS_STYLE (style), NULL);
  gtk_internal_return_val_if_fail (parent_style == NULL || GTK_IS_CSS_STYLE (parent_style), NULL);
96

97
  return value->class->compute (value, property_id, provider, style, parent_style);
98 99
}

100 101 102 103
gboolean
_gtk_css_value_equal (const GtkCssValue *value1,
                      const GtkCssValue *value2)
{
104 105
  gtk_internal_return_val_if_fail (value1 != NULL, FALSE);
  gtk_internal_return_val_if_fail (value2 != NULL, FALSE);
106

107 108 109
  if (value1 == value2)
    return TRUE;

110 111 112 113 114 115
  if (value1->class != value2->class)
    return FALSE;

  return value1->class->equal (value1, value2);
}

116 117 118 119
gboolean
_gtk_css_value_equal0 (const GtkCssValue *value1,
                       const GtkCssValue *value2)
{
120 121
  /* Inclues both values being NULL */
  if (value1 == value2)
122 123 124 125 126 127 128 129
    return TRUE;

  if (value1 == NULL || value2 == NULL)
    return FALSE;

  return _gtk_css_value_equal (value1, value2);
}

130 131 132
GtkCssValue *
_gtk_css_value_transition (GtkCssValue *start,
                           GtkCssValue *end,
133
                           guint        property_id,
134 135
                           double       progress)
{
136 137
  gtk_internal_return_val_if_fail (start != NULL, FALSE);
  gtk_internal_return_val_if_fail (end != NULL, FALSE);
138

139 140 141
  /* We compare functions here instead of classes so that number
   * values can all transition to each other */
  if (start->class->transition != end->class->transition)
142 143
    return NULL;

144
  return start->class->transition (start, end, property_id, progress);
145 146
}

147 148 149 150 151
char *
_gtk_css_value_to_string (const GtkCssValue *value)
{
  GString *string;

152
  gtk_internal_return_val_if_fail (value != NULL, NULL);
153 154 155 156 157 158

  string = g_string_new (NULL);
  _gtk_css_value_print (value, string);
  return g_string_free (string, FALSE);
}

159 160 161 162 163 164 165 166 167
/**
 * _gtk_css_value_print:
 * @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().
 **/
168 169 170 171
void
_gtk_css_value_print (const GtkCssValue *value,
                      GString           *string)
{
172 173
  gtk_internal_return_if_fail (value != NULL);
  gtk_internal_return_if_fail (string != NULL);
174 175 176 177

  value->class->print (value, string);
}