]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssbgsizevalue.c
notebook: restore previous behaviour wrt. unparenting of tab labels
[~andy/gtk] / gtk / gtkcssbgsizevalue.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 "gtkcssbgsizevalueprivate.h"
21
22 #include "gtkcssnumbervalueprivate.h"
23
24 struct _GtkCssValue {
25   GTK_CSS_VALUE_BASE
26   guint cover :1;
27   guint contain :1;
28   GtkCssValue *x;
29   GtkCssValue *y;
30 };
31
32 static void
33 gtk_css_value_bg_size_free (GtkCssValue *value)
34 {
35   if (value->x)
36     _gtk_css_value_unref (value->x);
37   if (value->y)
38     _gtk_css_value_unref (value->y);
39
40   g_slice_free (GtkCssValue, value);
41 }
42
43 static gboolean
44 gtk_css_value_bg_size_equal (const GtkCssValue *value1,
45                              const GtkCssValue *value2)
46 {
47   return value1->cover == value2->cover &&
48          value2->contain == value2->contain &&
49          (value1->x == value2->x ||
50           (value1->x != NULL && value2->x != NULL &&
51            _gtk_css_value_equal (value1->x, value2->x))) &&
52          (value1->y == value2->y ||
53           (value1->y != NULL && value2->y != NULL &&
54            _gtk_css_value_equal (value1->y, value2->y)));
55 }
56
57 static GtkCssValue *
58 gtk_css_value_bg_size_transition (GtkCssValue *start,
59                                   GtkCssValue *end,
60                                   double       progress)
61 {
62   GtkCssValue *x, *y;
63
64   if (start->cover)
65     return end->cover ? _gtk_css_value_ref (end) : NULL;
66   if (start->contain)
67     return end->contain ? _gtk_css_value_ref (end) : NULL;
68
69   if ((start->x != NULL) ^ (end->x != NULL) ||
70       (start->y != NULL) ^ (end->y != NULL))
71     return NULL;
72
73   if (start->x)
74     {
75       x = _gtk_css_value_transition (start->x, end->x, progress);
76       if (x == NULL)
77         return NULL;
78     }
79   else
80     x = NULL;
81
82   if (start->y)
83     {
84       y = _gtk_css_value_transition (start->y, end->y, progress);
85       if (y == NULL)
86         {
87           _gtk_css_value_unref (x);
88           return NULL;
89         }
90     }
91   else
92     y = NULL;
93
94   return _gtk_css_bg_size_value_new (x, y);
95 }
96
97 static void
98 gtk_css_value_bg_size_print (const GtkCssValue *value,
99                              GString           *string)
100 {
101   if (value->cover)
102     g_string_append (string, "cover");
103   else if (value->contain)
104     g_string_append (string, "contain");
105   else
106     {
107       if (value->x == NULL)
108         g_string_append (string, "auto");
109       else
110         _gtk_css_value_print (value->x, string);
111
112       if (value->y)
113         {
114           g_string_append_c (string, ' ');
115           _gtk_css_value_print (value->y, string);
116         }
117     }
118 }
119
120 static const GtkCssValueClass GTK_CSS_VALUE_BG_SIZE = {
121   gtk_css_value_bg_size_free,
122   gtk_css_value_bg_size_equal,
123   gtk_css_value_bg_size_transition,
124   gtk_css_value_bg_size_print
125 };
126
127 static GtkCssValue auto_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, FALSE, FALSE, NULL, NULL };
128 static GtkCssValue cover_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, TRUE, FALSE, NULL, NULL };
129 static GtkCssValue contain_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, FALSE, TRUE, NULL, NULL };
130
131 GtkCssValue *
132 _gtk_css_bg_size_value_new (GtkCssValue *x,
133                             GtkCssValue *y)
134 {
135   GtkCssValue *result;
136
137   if (x == NULL && y == NULL)
138     return _gtk_css_value_ref (&auto_singleton);
139
140   result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_BG_SIZE);
141   result->x = x;
142   result->y = y;
143
144   return result;
145 }
146
147 GtkCssValue *
148 _gtk_css_bg_size_value_parse (GtkCssParser *parser)
149 {
150   GtkCssValue *x, *y;
151
152   if (_gtk_css_parser_try (parser, "cover", TRUE))
153     return _gtk_css_value_ref (&cover_singleton);
154   else if (_gtk_css_parser_try (parser, "contain", TRUE))
155     return _gtk_css_value_ref (&contain_singleton);
156
157   if (_gtk_css_parser_try (parser, "auto", TRUE))
158     x = NULL;
159   else
160     {
161       x = _gtk_css_number_value_parse (parser,
162                                        GTK_CSS_POSITIVE_ONLY
163                                        | GTK_CSS_PARSE_PERCENT
164                                        | GTK_CSS_PARSE_LENGTH);
165       if (x == NULL)
166         return NULL;
167     }
168
169   if (_gtk_css_parser_try (parser, "auto", TRUE))
170     y = NULL;
171   else if (!_gtk_css_parser_has_number (parser))
172     y = NULL;
173   else
174     {
175       y = _gtk_css_number_value_parse (parser,
176                                        GTK_CSS_POSITIVE_ONLY
177                                        | GTK_CSS_PARSE_PERCENT
178                                        | GTK_CSS_PARSE_LENGTH);
179       if (y == NULL)
180         {
181           _gtk_css_value_unref (x);
182           return NULL;
183         }
184     }
185
186   return _gtk_css_bg_size_value_new (x, y);
187 }
188
189 static void
190 gtk_css_bg_size_compute_size_for_cover_contain (gboolean     cover,
191                                                 GtkCssImage *image,
192                                                 double       width,
193                                                 double       height,
194                                                 double      *concrete_width,
195                                                 double      *concrete_height)
196 {
197   double aspect, image_aspect;
198   
199   image_aspect = _gtk_css_image_get_aspect_ratio (image);
200   if (image_aspect == 0.0)
201     {
202       *concrete_width = width;
203       *concrete_height = height;
204       return;
205     }
206
207   aspect = width / height;
208
209   if ((aspect >= image_aspect && cover) ||
210       (aspect < image_aspect && !cover))
211     {
212       *concrete_width = width;
213       *concrete_height = width / image_aspect;
214     }
215   else
216     {
217       *concrete_height = height;
218       *concrete_width = height * image_aspect;
219     }
220 }
221
222 void
223 _gtk_css_bg_size_value_compute_size (const GtkCssValue *value,
224                                      GtkCssImage       *image,
225                                      double             area_width,
226                                      double             area_height,
227                                      double            *out_width,
228                                      double            *out_height)
229 {
230   g_return_if_fail (value->class == &GTK_CSS_VALUE_BG_SIZE);
231
232   if (value->contain || value->cover)
233     gtk_css_bg_size_compute_size_for_cover_contain (value->cover,
234                                                     image,
235                                                     area_width, area_height,
236                                                     out_width, out_height);
237   else
238     _gtk_css_image_get_concrete_size (image,
239                                       /* note: 0 does the right thing here for 'auto' */
240                                       value->x ? _gtk_css_number_value_get (value->x, area_width) : 0,
241                                       value->y ? _gtk_css_number_value_get (value->y, area_height) : 0,
242                                       area_width, area_height,
243                                       out_width, out_height);
244 }
245
246 GtkCssValue *
247 _gtk_css_bg_size_value_compute (GtkCssValue     *value,
248                                 GtkStyleContext *context)
249 {
250   g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BG_SIZE, NULL);
251
252   if (value->x == NULL && value->y == NULL)
253     return _gtk_css_value_ref (value);
254
255   return _gtk_css_bg_size_value_new (value->x ? _gtk_css_number_value_compute (value->x, context) : NULL,
256                                      value->y ? _gtk_css_number_value_compute (value->y, context) : NULL);
257 }
258