1 /*
2  * Copyright © 2013 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.1 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  * Authors: Alexander Larsson <alexl@gnome.org>
18  */
19 
20 #include "config.h"
21 
22 #include "gtkcssimagescaledprivate.h"
23 
24 #include "gtkstyleproviderprivate.h"
25 
G_DEFINE_TYPE(GtkCssImageScaled,_gtk_css_image_scaled,GTK_TYPE_CSS_IMAGE)26 G_DEFINE_TYPE (GtkCssImageScaled, _gtk_css_image_scaled, GTK_TYPE_CSS_IMAGE)
27 
28 static int
29 gtk_css_image_scaled_get_width (GtkCssImage *image)
30 {
31   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
32 
33   return _gtk_css_image_get_width (scaled->images[scaled->scale - 1]) / scaled->scale;
34 }
35 
36 static int
gtk_css_image_scaled_get_height(GtkCssImage * image)37 gtk_css_image_scaled_get_height (GtkCssImage *image)
38 {
39   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
40 
41   return _gtk_css_image_get_height (scaled->images[scaled->scale - 1]) / scaled->scale;
42 }
43 
44 static double
gtk_css_image_scaled_get_aspect_ratio(GtkCssImage * image)45 gtk_css_image_scaled_get_aspect_ratio (GtkCssImage *image)
46 {
47   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
48 
49   return _gtk_css_image_get_aspect_ratio (scaled->images[scaled->scale - 1]);
50 }
51 
52 static void
gtk_css_image_scaled_draw(GtkCssImage * image,cairo_t * cr,double width,double height)53 gtk_css_image_scaled_draw (GtkCssImage *image,
54 			   cairo_t     *cr,
55 			   double       width,
56 			   double       height)
57 {
58   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
59 
60   _gtk_css_image_draw (scaled->images[scaled->scale - 1], cr, width, height);
61 }
62 
63 static void
gtk_css_image_scaled_print(GtkCssImage * image,GString * string)64 gtk_css_image_scaled_print (GtkCssImage *image,
65                              GString     *string)
66 {
67   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
68   int i;
69 
70   g_string_append (string, "-gtk-scaled(");
71   for (i = 0; i < scaled->n_images; i++)
72     {
73       _gtk_css_image_print (scaled->images[i], string);
74       if (i != scaled->n_images - 1)
75 	g_string_append (string, ",");
76     }
77   g_string_append (string, ")");
78 }
79 
80 static void
gtk_css_image_scaled_dispose(GObject * object)81 gtk_css_image_scaled_dispose (GObject *object)
82 {
83   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (object);
84   int i;
85 
86   for (i = 0; i < scaled->n_images; i++)
87     g_object_unref (scaled->images[i]);
88   g_free (scaled->images);
89   scaled->images = NULL;
90 
91   G_OBJECT_CLASS (_gtk_css_image_scaled_parent_class)->dispose (object);
92 }
93 
94 
95 static GtkCssImage *
gtk_css_image_scaled_compute(GtkCssImage * image,guint property_id,GtkStyleProviderPrivate * provider,GtkCssStyle * style,GtkCssStyle * parent_style)96 gtk_css_image_scaled_compute (GtkCssImage             *image,
97 			      guint                    property_id,
98 			      GtkStyleProviderPrivate *provider,
99 			      GtkCssStyle             *style,
100 			      GtkCssStyle             *parent_style)
101 {
102   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
103   GtkCssImageScaled *copy;
104   int i, scale;
105 
106   scale = _gtk_style_provider_private_get_scale (provider);
107   scale = MAX(MIN (scale, scaled->n_images), 1);
108 
109   if (scaled->scale == scale)
110     return GTK_CSS_IMAGE (g_object_ref (scaled));
111   else
112     {
113       copy = g_object_new (_gtk_css_image_scaled_get_type (), NULL);
114       copy->scale = scale;
115       copy->n_images = scaled->n_images;
116       copy->images = g_new (GtkCssImage *, scaled->n_images);
117       for (i = 0; i < scaled->n_images; i++)
118         {
119           if (i == scale - 1)
120             copy->images[i] = _gtk_css_image_compute (scaled->images[i],
121                                                       property_id,
122                                                       provider,
123                                                       style,
124                                                       parent_style);
125           else
126             copy->images[i] = g_object_ref (scaled->images[i]);
127         }
128 
129       return GTK_CSS_IMAGE (copy);
130     }
131 }
132 
133 static gboolean
gtk_css_image_scaled_parse(GtkCssImage * image,GtkCssParser * parser)134 gtk_css_image_scaled_parse (GtkCssImage  *image,
135 			    GtkCssParser *parser)
136 {
137   GtkCssImageScaled *scaled = GTK_CSS_IMAGE_SCALED (image);
138   GPtrArray *images;
139   GtkCssImage *child;
140 
141   if (!_gtk_css_parser_try (parser, "-gtk-scaled", TRUE))
142     {
143       _gtk_css_parser_error (parser, "'-gtk-scaled'");
144       return FALSE;
145     }
146 
147   if (!_gtk_css_parser_try (parser, "(", TRUE))
148     {
149       _gtk_css_parser_error (parser,
150                              "Expected '(' after '-gtk-scaled'");
151       return FALSE;
152     }
153 
154   images = g_ptr_array_new_with_free_func (g_object_unref);
155 
156   do
157     {
158       child = _gtk_css_image_new_parse (parser);
159       if (child == NULL)
160 	{
161 	  g_ptr_array_free (images, TRUE);
162 	  return FALSE;
163 	}
164       g_ptr_array_add (images, child);
165 
166     }
167   while ( _gtk_css_parser_try (parser, ",", TRUE));
168 
169   if (!_gtk_css_parser_try (parser, ")", TRUE))
170     {
171       g_ptr_array_free (images, TRUE);
172       _gtk_css_parser_error (parser,
173                              "Expected ')' at end of '-gtk-scaled'");
174       return FALSE;
175     }
176 
177   scaled->n_images = images->len;
178   scaled->images = (GtkCssImage **) g_ptr_array_free (images, FALSE);
179 
180   return TRUE;
181 }
182 
183 static void
_gtk_css_image_scaled_class_init(GtkCssImageScaledClass * klass)184 _gtk_css_image_scaled_class_init (GtkCssImageScaledClass *klass)
185 {
186   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
187   GObjectClass *object_class = G_OBJECT_CLASS (klass);
188 
189   image_class->get_width = gtk_css_image_scaled_get_width;
190   image_class->get_height = gtk_css_image_scaled_get_height;
191   image_class->get_aspect_ratio = gtk_css_image_scaled_get_aspect_ratio;
192   image_class->draw = gtk_css_image_scaled_draw;
193   image_class->parse = gtk_css_image_scaled_parse;
194   image_class->compute = gtk_css_image_scaled_compute;
195   image_class->print = gtk_css_image_scaled_print;
196 
197   object_class->dispose = gtk_css_image_scaled_dispose;
198 }
199 
200 static void
_gtk_css_image_scaled_init(GtkCssImageScaled * image_scaled)201 _gtk_css_image_scaled_init (GtkCssImageScaled *image_scaled)
202 {
203   image_scaled->scale = 1;
204 }
205