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