]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimagewin32.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkcssimagewin32.c
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
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_part_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 *cr;
45       int dx2, dy2;
46
47       surface2 = _gtk_win32_theme_part_create_surface (wimage->theme, wimage->part2, wimage->state2, wimage->margins,
48                                                        width, height, &dx2, &dy2);
49
50       cr = cairo_create (surface);
51
52       cairo_set_source_surface (cr, surface2, dx2 - dx, dy2-dy);
53       cairo_paint_with_alpha (cr, wimage->over_alpha);
54       
55       cairo_destroy (cr);
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
69 gtk_css_image_win32_parse (GtkCssImage  *image,
70                            GtkCssParser *parser)
71 {
72   GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (image);
73   char *class;
74
75   if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
76     {
77       _gtk_css_parser_error (parser, "'-gtk-win32-theme-part'");
78       return FALSE;
79     }
80   
81   if (!_gtk_css_parser_try (parser, "(", TRUE))
82     {
83       _gtk_css_parser_error (parser,
84                              "Expected '(' after '-gtk-win32-theme-part'");
85       return FALSE;
86     }
87   
88   class = _gtk_css_parser_try_name (parser, TRUE);
89   if (class == NULL)
90     {
91       _gtk_css_parser_error (parser,
92                              "Expected name as first argument to  '-gtk-win32-theme-part'");
93       return FALSE;
94     }
95   wimage->theme = _gtk_win32_lookup_htheme_by_classname (class);
96   g_free (class);
97
98   if (! _gtk_css_parser_try (parser, ",", TRUE))
99     {
100       _gtk_css_parser_error (parser, "Expected ','");
101       return FALSE;
102     }
103
104   if (!_gtk_css_parser_try_int (parser, &wimage->part))
105     {
106       _gtk_css_parser_error (parser, "Expected a valid integer value");
107       return FALSE;
108     }
109
110   if (!_gtk_css_parser_try_int (parser, &wimage->state))
111     {
112       _gtk_css_parser_error (parser, "Expected a valid integer value");
113       return FALSE;
114     }
115
116   while ( _gtk_css_parser_try (parser, ",", TRUE))
117     {
118       if ( _gtk_css_parser_try (parser, "over", TRUE))
119         {
120           if (!_gtk_css_parser_try (parser, "(", TRUE))
121             {
122               _gtk_css_parser_error (parser,
123                                      "Expected '(' after 'over'");
124               return FALSE;
125             }
126
127           if (!_gtk_css_parser_try_int (parser, &wimage->part2))
128             {
129               _gtk_css_parser_error (parser, "Expected a valid integer value");
130               return FALSE;
131             }
132
133           if (!_gtk_css_parser_try_int (parser, &wimage->state2))
134             {
135               _gtk_css_parser_error (parser, "Expected a valid integer value");
136               return FALSE;
137             }
138
139           if ( _gtk_css_parser_try (parser, ",", TRUE))
140             {
141               if (!_gtk_css_parser_try_double (parser, &wimage->over_alpha))
142                 {
143                   _gtk_css_parser_error (parser, "Expected a valid double value");
144                   return FALSE;
145                 }
146             }
147
148           if (!_gtk_css_parser_try (parser, ")", TRUE))
149             {
150               _gtk_css_parser_error (parser,
151                                      "Expected ')' at end of 'over'");
152               return FALSE;
153             }
154         }
155       else if ( _gtk_css_parser_try (parser, "margins", TRUE))
156         {
157           guint i;
158
159           if (!_gtk_css_parser_try (parser, "(", TRUE))
160             {
161               _gtk_css_parser_error (parser,
162                                      "Expected '(' after 'margins'");
163               return FALSE;
164             }
165
166           for (i = 0; i < 4; i++)
167             {
168               if (!_gtk_css_parser_try_int (parser, &wimage->margins[i]))
169                 break;
170             }
171           
172           if (i == 0)
173             {
174               _gtk_css_parser_error (parser, "Expected valid margins");
175               return FALSE;
176             }
177
178           if (i == 1)
179             wimage->margins[1] = wimage->margins[0];
180           if (i <= 2)
181             wimage->margins[2] = wimage->margins[1];
182           if (i <= 3)
183             wimage->margins[3] = wimage->margins[2];
184           
185           if (!_gtk_css_parser_try (parser, ")", TRUE))
186             {
187               _gtk_css_parser_error (parser,
188                                      "Expected ')' at end of 'margins'");
189               return FALSE;
190             }
191         }
192       else
193         {
194           _gtk_css_parser_error (parser,
195                                  "Expected identifier");
196           return FALSE;
197         }
198     }
199
200   if (!_gtk_css_parser_try (parser, ")", TRUE))
201     {
202       _gtk_css_parser_error (parser,
203                              "Expected ')'");
204       return FALSE;
205     }
206   
207   return TRUE;
208 }
209
210 static void
211 gtk_css_image_win32_print (GtkCssImage *image,
212                            GString     *string)
213 {
214   g_string_append (string, "none /* printing win32 theme components is not implemented */");
215 }
216
217 static void
218 _gtk_css_image_win32_class_init (GtkCssImageWin32Class *klass)
219 {
220   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
221
222   image_class->draw = gtk_css_image_win32_draw;
223   image_class->parse = gtk_css_image_win32_parse;
224   image_class->print = gtk_css_image_win32_print;
225 }
226
227 static void
228 _gtk_css_image_win32_init (GtkCssImageWin32 *wimage)
229 {
230   wimage->over_alpha = 1.0;
231   wimage->part2 = -1;
232   wimage->state2 = -1;
233 }
234