1 /*
2 * Copyright © 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.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: Benjamin Otte <otte@gnome.org>
18 */
19
20 #include "config.h"
21
22 #include "gtkcssimagewin32private.h"
23
24 #include "gtkcssprovider.h"
25
G_DEFINE_TYPE(GtkCssImageWin32,_gtk_css_image_win32,GTK_TYPE_CSS_IMAGE)26 G_DEFINE_TYPE (GtkCssImageWin32, _gtk_css_image_win32, GTK_TYPE_CSS_IMAGE)
27
28 static void
29 gtk_css_image_win32_draw (GtkCssImage *image,
30 cairo_t *cr,
31 double width,
32 double height)
33 {
34 GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (image);
35 cairo_surface_t *surface;
36 int dx, dy;
37
38 surface = gtk_win32_theme_create_surface (wimage->theme, wimage->part, wimage->state, wimage->margins,
39 width, height, &dx, &dy);
40
41 if (wimage->state2 >= 0)
42 {
43 cairo_surface_t *surface2;
44 cairo_t *cr2;
45 int dx2, dy2;
46
47 surface2 = gtk_win32_theme_create_surface (wimage->theme, wimage->part2, wimage->state2, wimage->margins,
48 width, height, &dx2, &dy2);
49
50 cr2 = cairo_create (surface);
51
52 cairo_set_source_surface (cr2, surface2, dx2 - dx, dy2-dy);
53 cairo_paint_with_alpha (cr2, wimage->over_alpha);
54
55 cairo_destroy (cr2);
56
57 cairo_surface_destroy (surface2);
58 }
59
60 cairo_set_source_surface (cr, surface, dx, dy);
61 cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
62 cairo_rectangle (cr, 0, 0, width, height);
63 cairo_fill (cr);
64
65 cairo_surface_destroy (surface);
66 }
67
68 static gboolean
gtk_css_image_win32_parse(GtkCssImage * image,GtkCssParser * parser)69 gtk_css_image_win32_parse (GtkCssImage *image,
70 GtkCssParser *parser)
71 {
72 GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (image);
73
74 if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
75 {
76 _gtk_css_parser_error (parser, "'-gtk-win32-theme-part'");
77 return FALSE;
78 }
79
80 if (!_gtk_css_parser_try (parser, "(", TRUE))
81 {
82 _gtk_css_parser_error (parser,
83 "Expected '(' after '-gtk-win32-theme-part'");
84 return FALSE;
85 }
86
87 wimage->theme = gtk_win32_theme_parse (parser);
88 if (wimage->theme == NULL)
89 return FALSE;
90
91 if (! _gtk_css_parser_try (parser, ",", TRUE))
92 {
93 _gtk_css_parser_error (parser, "Expected ','");
94 return FALSE;
95 }
96
97 if (!_gtk_css_parser_try_int (parser, &wimage->part))
98 {
99 _gtk_css_parser_error (parser, "Expected a valid integer value");
100 return FALSE;
101 }
102
103 if (! _gtk_css_parser_try (parser, ",", TRUE))
104 {
105 _gtk_css_parser_error (parser, "Expected ','");
106 return FALSE;
107 }
108
109 if (!_gtk_css_parser_try_int (parser, &wimage->state))
110 {
111 _gtk_css_parser_error (parser, "Expected a valid integer value");
112 return FALSE;
113 }
114
115 while ( _gtk_css_parser_try (parser, ",", TRUE))
116 {
117 if ( _gtk_css_parser_try (parser, "over", TRUE))
118 {
119 if (!_gtk_css_parser_try (parser, "(", TRUE))
120 {
121 _gtk_css_parser_error (parser,
122 "Expected '(' after 'over'");
123 return FALSE;
124 }
125
126 if (!_gtk_css_parser_try_int (parser, &wimage->part2))
127 {
128 _gtk_css_parser_error (parser, "Expected a valid integer value");
129 return FALSE;
130 }
131
132 if (! _gtk_css_parser_try (parser, ",", TRUE))
133 {
134 _gtk_css_parser_error (parser, "Expected ','");
135 return FALSE;
136 }
137
138 if (!_gtk_css_parser_try_int (parser, &wimage->state2))
139 {
140 _gtk_css_parser_error (parser, "Expected a valid integer value");
141 return FALSE;
142 }
143
144 if ( _gtk_css_parser_try (parser, ",", TRUE))
145 {
146 if (!_gtk_css_parser_try_double (parser, &wimage->over_alpha))
147 {
148 _gtk_css_parser_error (parser, "Expected a valid double value");
149 return FALSE;
150 }
151 }
152
153 if (!_gtk_css_parser_try (parser, ")", TRUE))
154 {
155 _gtk_css_parser_error (parser,
156 "Expected ')' at end of 'over'");
157 return FALSE;
158 }
159 }
160 else if ( _gtk_css_parser_try (parser, "margins", TRUE))
161 {
162 guint i;
163
164 if (!_gtk_css_parser_try (parser, "(", TRUE))
165 {
166 _gtk_css_parser_error (parser,
167 "Expected '(' after 'margins'");
168 return FALSE;
169 }
170
171 for (i = 0; i < 4; i++)
172 {
173 if (!_gtk_css_parser_try_int (parser, &wimage->margins[i]))
174 break;
175 }
176
177 if (i == 0)
178 {
179 _gtk_css_parser_error (parser, "Expected valid margins");
180 return FALSE;
181 }
182
183 if (i == 1)
184 wimage->margins[1] = wimage->margins[0];
185 if (i <= 2)
186 wimage->margins[2] = wimage->margins[1];
187 if (i <= 3)
188 wimage->margins[3] = wimage->margins[2];
189
190 if (!_gtk_css_parser_try (parser, ")", TRUE))
191 {
192 _gtk_css_parser_error (parser,
193 "Expected ')' at end of 'margins'");
194 return FALSE;
195 }
196 }
197 else
198 {
199 _gtk_css_parser_error (parser,
200 "Expected identifier");
201 return FALSE;
202 }
203 }
204
205 if (!_gtk_css_parser_try (parser, ")", TRUE))
206 {
207 _gtk_css_parser_error (parser,
208 "Expected ')'");
209 return FALSE;
210 }
211
212 return TRUE;
213 }
214
215 static void
gtk_css_image_win32_print(GtkCssImage * image,GString * string)216 gtk_css_image_win32_print (GtkCssImage *image,
217 GString *string)
218 {
219 GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (image);
220
221 g_string_append (string, "-gtk-win32-theme-part(");
222 gtk_win32_theme_print (wimage->theme, string);
223 g_string_append_printf (string, ", %d, %d)", wimage->part, wimage->state);
224 }
225
226 static void
gtk_css_image_win32_finalize(GObject * object)227 gtk_css_image_win32_finalize (GObject *object)
228 {
229 GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (object);
230
231 if (wimage->theme)
232 gtk_win32_theme_unref (wimage->theme);
233
234 G_OBJECT_CLASS (_gtk_css_image_win32_parent_class)->finalize (object);
235 }
236
237 static void
_gtk_css_image_win32_class_init(GtkCssImageWin32Class * klass)238 _gtk_css_image_win32_class_init (GtkCssImageWin32Class *klass)
239 {
240 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
241 GObjectClass *object_class = G_OBJECT_CLASS (klass);
242
243 object_class->finalize = gtk_css_image_win32_finalize;
244
245 image_class->draw = gtk_css_image_win32_draw;
246 image_class->parse = gtk_css_image_win32_parse;
247 image_class->print = gtk_css_image_win32_print;
248 }
249
250 static void
_gtk_css_image_win32_init(GtkCssImageWin32 * wimage)251 _gtk_css_image_win32_init (GtkCssImageWin32 *wimage)
252 {
253 wimage->over_alpha = 1.0;
254 wimage->part2 = -1;
255 wimage->state2 = -1;
256 }
257
258