]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssrgbavalue.c
css: Pass property_id to compute function
[~andy/gtk] / gtk / gtkcssrgbavalue.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2011 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "config.h"
19
20 #include "gtkcssrgbavalueprivate.h"
21
22 #include "gtkcssstylepropertyprivate.h"
23 #include "gtkstylecontextprivate.h"
24 #include "gtksymboliccolorprivate.h"
25
26 struct _GtkCssValue {
27   GTK_CSS_VALUE_BASE
28   GdkRGBA rgba;
29 };
30
31 static void
32 gtk_css_value_rgba_free (GtkCssValue *value)
33 {
34   g_slice_free (GtkCssValue, value);
35 }
36
37 static GtkCssValue *
38 gtk_css_value_rgba_compute (GtkCssValue     *value,
39                             guint            property_id,
40                             GtkStyleContext *context)
41 {
42   return _gtk_css_value_ref (value);
43 }
44
45 static gboolean
46 gtk_css_value_rgba_equal (const GtkCssValue *rgba1,
47                           const GtkCssValue *rgba2)
48 {
49   return gdk_rgba_equal (&rgba1->rgba, &rgba2->rgba);
50 }
51
52 static GtkCssValue *
53 gtk_css_value_rgba_transition (GtkCssValue *start,
54                                GtkCssValue *end,
55                                double       progress)
56 {
57   GdkRGBA transition;
58
59   progress = CLAMP (progress, 0, 1);
60   transition.red = start->rgba.red + (end->rgba.red - start->rgba.red) * progress;
61   transition.green = start->rgba.green + (end->rgba.green - start->rgba.green) * progress;
62   transition.blue = start->rgba.blue + (end->rgba.blue - start->rgba.blue) * progress;
63   transition.alpha = start->rgba.alpha + (end->rgba.alpha - start->rgba.alpha) * progress;
64
65   return _gtk_css_rgba_value_new_from_rgba (&transition);
66 }
67
68 static void
69 gtk_css_value_rgba_print (const GtkCssValue *rgba,
70                           GString           *string)
71 {
72   char *s = gdk_rgba_to_string (&rgba->rgba);
73   g_string_append (string, s);
74   g_free (s);
75 }
76
77 static const GtkCssValueClass GTK_CSS_VALUE_RGBA = {
78   gtk_css_value_rgba_free,
79   gtk_css_value_rgba_compute,
80   gtk_css_value_rgba_equal,
81   gtk_css_value_rgba_transition,
82   gtk_css_value_rgba_print
83 };
84
85 GtkCssValue *
86 _gtk_css_rgba_value_new_from_rgba (const GdkRGBA *rgba)
87 {
88   GtkCssValue *value;
89
90   g_return_val_if_fail (rgba != NULL, NULL);
91
92   value = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_RGBA);
93   value->rgba = *rgba;
94
95   return value;
96 }
97
98 const GdkRGBA *
99 _gtk_css_rgba_value_get_rgba (const GtkCssValue *rgba)
100 {
101   g_return_val_if_fail (rgba->class == &GTK_CSS_VALUE_RGBA, NULL);
102
103   return &rgba->rgba;
104 }
105
106 GtkCssValue *
107 _gtk_css_rgba_value_compute_from_symbolic (GtkCssValue     *symbolic,
108                                            GtkCssValue     *fallback,
109                                            GtkStyleContext *context,
110                                            gboolean         for_color_property)
111 {
112   GtkCssValue *resolved, *current;
113
114   g_return_val_if_fail (symbolic != NULL, NULL);
115
116   /* The computed value of the ‘currentColor’ keyword is the computed
117    * value of the ‘color’ property. If the ‘currentColor’ keyword is
118    * set on the ‘color’ property itself, it is treated as ‘color: inherit’. 
119    */
120   if (for_color_property)
121     {
122       GtkStyleContext *parent = gtk_style_context_get_parent (context);
123
124       if (parent)
125         current = _gtk_style_context_peek_property (parent, GTK_CSS_PROPERTY_COLOR);
126       else
127         current = _gtk_css_style_property_get_initial_value (_gtk_css_style_property_lookup_by_id (GTK_CSS_PROPERTY_COLOR));
128     }
129   else
130     {
131       current = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR);
132     }
133   
134   resolved = _gtk_style_context_resolve_color_value (context, current, symbolic);
135
136   if (resolved == NULL)
137     return _gtk_css_rgba_value_compute_from_symbolic (fallback, NULL, context, for_color_property);
138
139   g_assert (resolved->class == &GTK_CSS_VALUE_RGBA);
140   return resolved;
141 }
142