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 "gtkcssimagevalueprivate.h"
21 
22 #include "gtkcssimagecrossfadeprivate.h"
23 
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   GtkCssImage *image;
27 };
28 
29 static void
gtk_css_value_image_free(GtkCssValue * value)30 gtk_css_value_image_free (GtkCssValue *value)
31 {
32   g_object_unref (value->image);
33   g_slice_free (GtkCssValue, value);
34 }
35 
36 static GtkCssValue *
gtk_css_value_image_compute(GtkCssValue * value,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)37 gtk_css_value_image_compute (GtkCssValue             *value,
38                              guint                    property_id,
39                              GtkStyleProviderPrivate *provider,
40                              GtkCssStyle             *style,
41                              GtkCssStyle             *parent_style)
42 {
43   GtkCssImage *image, *computed;
44 
45   image = _gtk_css_image_value_get_image (value);
46 
47   if (image == NULL)
48     return _gtk_css_value_ref (value);
49 
50   computed = _gtk_css_image_compute (image, property_id, provider, style, parent_style);
51 
52   if (computed == image)
53     {
54       g_object_unref (computed);
55       return _gtk_css_value_ref (value);
56     }
57 
58   return _gtk_css_image_value_new (computed);
59 }
60 
61 static gboolean
gtk_css_value_image_equal(const GtkCssValue * value1,const GtkCssValue * value2)62 gtk_css_value_image_equal (const GtkCssValue *value1,
63                            const GtkCssValue *value2)
64 {
65   return _gtk_css_image_equal (value1->image, value2->image);
66 }
67 
68 static GtkCssValue *
gtk_css_value_image_transition(GtkCssValue * start,GtkCssValue * end,guint property_id,double progress)69 gtk_css_value_image_transition (GtkCssValue *start,
70                                 GtkCssValue *end,
71                                 guint        property_id,
72                                 double       progress)
73 {
74   GtkCssImage *transition;
75 
76   transition = _gtk_css_image_transition (_gtk_css_image_value_get_image (start),
77                                           _gtk_css_image_value_get_image (end),
78                                           property_id,
79                                           progress);
80 
81   return _gtk_css_image_value_new (transition);
82 }
83 
84 static void
gtk_css_value_image_print(const GtkCssValue * value,GString * string)85 gtk_css_value_image_print (const GtkCssValue *value,
86                            GString           *string)
87 {
88   if (value->image)
89     _gtk_css_image_print (value->image, string);
90   else
91     g_string_append (string, "none");
92 }
93 
94 static const GtkCssValueClass GTK_CSS_VALUE_IMAGE = {
95   gtk_css_value_image_free,
96   gtk_css_value_image_compute,
97   gtk_css_value_image_equal,
98   gtk_css_value_image_transition,
99   gtk_css_value_image_print
100 };
101 
102 GtkCssValue *
_gtk_css_image_value_new(GtkCssImage * image)103 _gtk_css_image_value_new (GtkCssImage *image)
104 {
105   static GtkCssValue none_singleton = { &GTK_CSS_VALUE_IMAGE, 1, NULL };
106   GtkCssValue *value;
107 
108   if (image == NULL)
109     return _gtk_css_value_ref (&none_singleton);
110 
111   value = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_IMAGE);
112   value->image = image;
113 
114   return value;
115 }
116 
117 GtkCssImage *
_gtk_css_image_value_get_image(const GtkCssValue * value)118 _gtk_css_image_value_get_image (const GtkCssValue *value)
119 {
120   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_IMAGE, NULL);
121 
122   return value->image;
123 }
124 
125