]> Pileus Git - ~andy/gtk/blob - tests/styleexamples.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / tests / styleexamples.c
1 #include <string.h>
2 #include <gtk/gtk.h>
3
4 static gboolean
5 draw_cb_checks (GtkWidget *widget, cairo_t *cr)
6 {
7   GtkStyleContext *context;
8
9   context = gtk_widget_get_style_context (widget);
10
11   gtk_style_context_save (context);
12
13   gtk_style_context_add_class (context, "check");
14   gtk_style_context_set_state (context, 0);
15   gtk_render_check (context, cr, 12, 12, 12, 12);
16   gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
17   gtk_render_check (context, cr, 36, 12, 12, 12);
18   gtk_style_context_set_state (context, GTK_STATE_FLAG_INCONSISTENT);
19   gtk_render_check (context, cr, 60, 12, 12, 12);
20   gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
21   gtk_render_check (context, cr, 84, 12, 12, 12);
22
23   gtk_style_context_restore (context);
24
25   return TRUE;
26 }
27
28
29 static gboolean
30 draw_cb_options (GtkWidget *widget, cairo_t *cr)
31 {
32   GtkStyleContext *context;
33
34   context = gtk_widget_get_style_context (widget);
35
36   gtk_style_context_save (context);
37
38   gtk_style_context_add_class (context, "radio");
39   gtk_style_context_set_state (context, 0);
40   gtk_render_option (context, cr, 12, 12, 12, 12);
41   gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
42   gtk_render_option (context, cr, 36, 12, 12, 12);
43   gtk_style_context_set_state (context, GTK_STATE_FLAG_INCONSISTENT);
44   gtk_render_option (context, cr, 60, 12, 12, 12);
45   gtk_style_context_set_state (context, GTK_STATE_FLAG_INSENSITIVE);
46   gtk_render_option (context, cr, 84, 12, 12, 12);
47
48   gtk_style_context_restore (context);
49
50   return TRUE;
51 }
52
53 static gboolean
54 draw_cb_arrows (GtkWidget *widget, cairo_t *cr)
55 {
56   GtkStyleContext *context;
57
58   context = gtk_widget_get_style_context (widget);
59
60   gtk_style_context_save (context);
61
62   gtk_style_context_set_state (context, 0);
63   gtk_render_arrow (context, cr, 0,        12, 12, 12);
64   gtk_render_arrow (context, cr, G_PI/2,   36, 12, 12);
65   gtk_render_arrow (context, cr, G_PI,     60, 12, 12);
66   gtk_render_arrow (context, cr, G_PI*3/2, 84, 12, 12);
67
68   gtk_style_context_restore (context);
69
70   return TRUE;
71 }
72
73 static gboolean
74 draw_cb_expanders (GtkWidget *widget, cairo_t *cr)
75 {
76   GtkStyleContext *context;
77
78   context = gtk_widget_get_style_context (widget);
79
80   gtk_style_context_save (context);
81
82   gtk_style_context_add_class (context, "expander");
83   gtk_style_context_set_state (context, 0);
84   gtk_render_expander (context, cr, 12, 12, 12, 12);
85   gtk_style_context_set_state (context, GTK_STATE_FLAG_PRELIGHT);
86   gtk_render_expander (context, cr, 36, 12, 12, 12);
87   gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
88   gtk_render_expander (context, cr, 60, 12, 12, 12);
89   gtk_style_context_set_state (context, GTK_STATE_FLAG_PRELIGHT | GTK_STATE_FLAG_ACTIVE);
90   gtk_render_expander (context, cr, 84, 12, 12, 12);
91
92   gtk_style_context_restore (context);
93
94   return TRUE;
95 }
96
97 static gboolean
98 draw_cb_background (GtkWidget *widget, cairo_t *cr)
99 {
100   GtkStyleContext *context;
101
102   context = gtk_widget_get_style_context (widget);
103
104   gtk_style_context_save (context);
105
106   gtk_style_context_add_class (context, "background");
107   gtk_style_context_set_junction_sides (context, 0);
108   gtk_render_background (context, cr, 12, 12, 100, 100);
109   gtk_style_context_remove_class (context, "background");
110
111   gtk_style_context_restore (context);
112
113   return TRUE;
114 }
115
116 static gboolean
117 draw_cb_frame (GtkWidget *widget, cairo_t *cr)
118 {
119   GtkStyleContext *context;
120
121   context = gtk_widget_get_style_context (widget);
122
123   gtk_style_context_save (context);
124
125   gtk_style_context_add_class (context, "frame1");
126   gtk_style_context_set_junction_sides (context, 0);
127   gtk_render_frame (context, cr, 12, 12, 50, 50);
128   gtk_style_context_remove_class (context, "frame1");
129
130   gtk_style_context_add_class (context, "frame2");
131   gtk_render_frame (context, cr, 74, 12, 50, 50);
132   gtk_style_context_remove_class (context, "frame2");
133
134   gtk_style_context_add_class (context, "frame3");
135   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_RIGHT);
136   gtk_render_frame (context, cr, 12, 74, 56, 50);
137   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_LEFT);
138   gtk_render_frame (context, cr, 68, 74, 56, 50);
139   gtk_style_context_remove_class (context, "frame3");
140
141   gtk_style_context_restore (context);
142
143   return TRUE;
144 }
145
146 /* FIXME: this doesn't work */
147 static gboolean
148 draw_cb_activity (GtkWidget *widget, cairo_t *cr)
149 {
150   GtkStyleContext *context;
151   GtkWidgetPath *path;
152
153   context = gtk_widget_get_style_context (widget);
154   gtk_style_context_save (context);
155
156   path = gtk_widget_path_new ();
157   gtk_widget_path_append_type (path, GTK_TYPE_SPINNER);
158   gtk_widget_path_iter_add_class (path, 0, "spinner");
159   gtk_style_context_set_path (context, path);
160   gtk_widget_path_free (path);
161
162   gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
163   gtk_render_activity (context, cr, 12, 12, 12, 12);
164
165   gtk_style_context_restore (context);
166
167   return TRUE;
168 }
169
170 static gboolean
171 draw_cb_slider (GtkWidget *widget, cairo_t *cr)
172 {
173   GtkStyleContext *context;
174   GtkWidgetPath *path;
175
176   context = gtk_widget_get_style_context (widget);
177   gtk_style_context_save (context);
178
179   path = gtk_widget_path_new ();
180   gtk_widget_path_append_type (path, GTK_TYPE_SCALE);
181   gtk_widget_path_iter_add_class (path, 0, "slider");
182   gtk_widget_path_iter_add_class (path, 0, "scale");
183   gtk_style_context_set_path (context, path);
184   gtk_widget_path_free (path);
185
186   gtk_render_slider (context, cr, 12, 22, 30, 10, GTK_ORIENTATION_HORIZONTAL);
187   gtk_render_slider (context, cr, 54, 12, 10, 30, GTK_ORIENTATION_VERTICAL);
188
189   gtk_style_context_restore (context);
190
191   return TRUE;
192 }
193
194 static gboolean
195 draw_cb_focus (GtkWidget *widget, cairo_t *cr)
196 {
197   GtkStyleContext *context;
198
199   context = gtk_widget_get_style_context (widget);
200
201   gtk_style_context_save (context);
202
203   gtk_render_focus (context, cr, 12, 12, 50, 50);
204
205   gtk_style_context_restore (context);
206
207   return TRUE;
208 }
209
210 static gboolean
211 draw_cb_extension (GtkWidget *widget, cairo_t *cr)
212 {
213   GtkStyleContext *context;
214
215   context = gtk_widget_get_style_context (widget);
216
217   gtk_style_context_save (context);
218
219   gtk_style_context_add_class (context, "notebook");
220   gtk_style_context_add_region (context, GTK_STYLE_REGION_TAB, 0);
221
222   gtk_style_context_set_state (context, 0);
223   gtk_render_extension (context, cr, 26, 12, 24, 12, GTK_POS_BOTTOM);
224   gtk_render_extension (context, cr, 12, 26, 12, 24, GTK_POS_RIGHT);
225   gtk_render_extension (context, cr, 26, 52, 24, 12, GTK_POS_TOP);
226   gtk_render_extension (context, cr, 52, 26, 12, 24, GTK_POS_LEFT);
227
228   gtk_style_context_restore (context);
229
230   return TRUE;
231 }
232
233 static gboolean
234 draw_cb_frame_gap (GtkWidget *widget, cairo_t *cr)
235 {
236   GtkStyleContext *context;
237
238   context = gtk_widget_get_style_context (widget);
239
240   gtk_style_context_save (context);
241
242   gtk_style_context_add_class (context, "frame");
243   gtk_style_context_set_junction_sides (context, 0);
244   gtk_render_frame_gap (context, cr, 12, 12, 50, 50, GTK_POS_TOP, 15, 35);
245   gtk_style_context_remove_class (context, "frame");
246
247   gtk_style_context_restore (context);
248
249   return TRUE;
250 }
251
252 static gboolean
253 draw_cb_handles (GtkWidget *widget, cairo_t *cr)
254 {
255   GtkStyleContext *context;
256
257   context = gtk_widget_get_style_context (widget);
258   gtk_style_context_save (context);
259
260   gtk_style_context_add_class (context, "paned");
261   gtk_render_handle (context, cr, 12, 22, 20, 10);
262   gtk_render_handle (context, cr, 44, 12, 10, 20);
263   gtk_style_context_remove_class (context, "paned");
264
265   gtk_style_context_add_class (context, "grip");
266   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_CORNER_BOTTOMLEFT);
267   gtk_render_handle (context, cr, 12, 48, 12, 12);
268
269   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_CORNER_BOTTOMRIGHT);
270   gtk_render_handle (context, cr, 40, 48, 12, 12);
271
272   gtk_style_context_restore (context);
273
274   return TRUE;
275 }
276
277 static char *what;
278
279 static gboolean
280 draw_cb (GtkWidget *widget, cairo_t *cr)
281 {
282   if (strcmp (what, "check") == 0)
283     return draw_cb_checks (widget, cr);
284   else if (strcmp (what, "option") == 0)
285     return draw_cb_options (widget, cr);
286   else if (strcmp (what, "arrow") == 0)
287     return draw_cb_arrows (widget, cr);
288   else if (strcmp (what, "expander") == 0)
289     return draw_cb_expanders (widget, cr);
290   else if (strcmp (what, "background") == 0)
291     return draw_cb_background (widget, cr);
292   else if (strcmp (what, "frame") == 0)
293     return draw_cb_frame (widget, cr);
294   else if (strcmp (what, "activity") == 0)
295     return draw_cb_activity (widget, cr);
296   else if (strcmp (what, "slider") == 0)
297     return draw_cb_slider (widget, cr);
298   else if (strcmp (what, "focus") == 0)
299     return draw_cb_focus (widget, cr);
300   else if (strcmp (what, "extension") == 0)
301     return draw_cb_extension (widget, cr);
302   else if (strcmp (what, "frame-gap") == 0)
303     return draw_cb_frame_gap (widget, cr);
304   else if (strcmp (what, "handle") == 0)
305     return draw_cb_handles (widget, cr);
306
307   return FALSE;
308 }
309
310 int main (int argc, char *argv[])
311 {
312   GtkWidget *window;
313   GtkWidget *ebox;
314   GtkStyleContext *context;
315   GtkStyleProvider *provider;
316
317   gtk_init (&argc, &argv);
318
319   if (argc > 1)
320     what = argv[1];
321   else
322     what = "check";
323
324   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
325   gtk_window_set_has_resize_grip (GTK_WINDOW (window), FALSE);
326   ebox = gtk_event_box_new ();
327   gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), TRUE);
328   gtk_container_add (GTK_CONTAINER (window), ebox);
329   gtk_widget_set_name (ebox, "ebox");
330
331   context = gtk_widget_get_style_context (ebox);
332   provider = (GtkStyleProvider *)gtk_css_provider_new ();
333   gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider),
334                                    ".frame1 {\n"
335                                    "   border-image: url('gradient1.png') 10 10 10 10 stretch;\n"
336                                    "}\n"
337                                    ".frame2 {\n"
338                                    "   border-style: solid;\n"
339                                    "   border-color: rgb(255,0,0);\n"
340                                    "   border-width: 10;\n"
341                                    "   border-radius: 10;\n"
342                                    "}\n"
343                                    ".frame3 {\n"
344                                    "   border-style: solid;\n"
345                                    "   border-color: rgb(0,0,0);\n"
346                                    "   border-width: 2;\n"
347                                    "   border-radius: 10;\n"
348                                    "}\n"
349                                    ".background {\n"
350                                    "   border-radius: 10;\n"
351                                    "   border-width: 0;\n"
352                                    "   background-image: -gtk-gradient (linear, left top, right bottom, from(#ff00ff), to(#aabbcc));\n"
353                                    "}\n"
354                                    ".frame {\n"
355                                    "   border-style: solid;\n"
356                                    "   border-width: 1;\n"
357                                    "   border-radius: 0;\n"
358                                    "}\n", -1, NULL);
359   gtk_style_context_add_provider (context, provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
360
361   g_signal_connect_after (ebox, "draw", G_CALLBACK (draw_cb), NULL);
362
363   gtk_widget_show_all (window);
364
365   gtk_main ();
366
367   gtk_style_context_remove_provider (context, provider);
368
369   return 0;
370 }
371