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