Initial packaging to sync OBS with git/gerrit
[profile/ivi/gtk3.git] / 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_notify_state_change (context,
155                                          gtk_widget_get_window (widget),
156                                          NULL,
157                                          GTK_STATE_FLAG_ACTIVE,
158                                          TRUE);
159
160   gtk_style_context_save (context);
161
162   path = gtk_widget_path_new ();
163   gtk_widget_path_append_type (path, GTK_TYPE_SPINNER);
164   gtk_widget_path_iter_add_class (path, 0, "spinner");
165   gtk_style_context_set_path (context, path);
166   gtk_widget_path_free (path);
167
168   gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);
169   gtk_render_activity (context, cr, 12, 12, 12, 12);
170
171   gtk_style_context_restore (context);
172
173   return TRUE;
174 }
175
176 static gboolean
177 draw_cb_slider (GtkWidget *widget, cairo_t *cr)
178 {
179   GtkStyleContext *context;
180   GtkWidgetPath *path;
181
182   context = gtk_widget_get_style_context (widget);
183   gtk_style_context_save (context);
184
185   path = gtk_widget_path_new ();
186   gtk_widget_path_append_type (path, GTK_TYPE_SCALE);
187   gtk_widget_path_iter_add_class (path, 0, "slider");
188   gtk_widget_path_iter_add_class (path, 0, "scale");
189   gtk_style_context_set_path (context, path);
190   gtk_widget_path_free (path);
191
192   gtk_render_slider (context, cr, 12, 22, 30, 10, GTK_ORIENTATION_HORIZONTAL);
193   gtk_render_slider (context, cr, 54, 12, 10, 30, GTK_ORIENTATION_VERTICAL);
194
195   gtk_style_context_restore (context);
196
197   return TRUE;
198 }
199
200 static gboolean
201 draw_cb_focus (GtkWidget *widget, cairo_t *cr)
202 {
203   GtkStyleContext *context;
204
205   context = gtk_widget_get_style_context (widget);
206
207   gtk_style_context_save (context);
208
209   gtk_render_focus (context, cr, 12, 12, 50, 50);
210
211   gtk_style_context_restore (context);
212
213   return TRUE;
214 }
215
216 static gboolean
217 draw_cb_extension (GtkWidget *widget, cairo_t *cr)
218 {
219   GtkStyleContext *context;
220
221   context = gtk_widget_get_style_context (widget);
222
223   gtk_style_context_save (context);
224
225   gtk_style_context_add_class (context, "notebook");
226   gtk_style_context_add_region (context, GTK_STYLE_REGION_TAB, 0);
227
228   gtk_style_context_set_state (context, 0);
229   gtk_render_extension (context, cr, 26, 12, 24, 12, GTK_POS_BOTTOM);
230   gtk_render_extension (context, cr, 12, 26, 12, 24, GTK_POS_RIGHT);
231   gtk_render_extension (context, cr, 26, 52, 24, 12, GTK_POS_TOP);
232   gtk_render_extension (context, cr, 52, 26, 12, 24, GTK_POS_LEFT);
233
234   gtk_style_context_restore (context);
235
236   return TRUE;
237 }
238
239 static gboolean
240 draw_cb_frame_gap (GtkWidget *widget, cairo_t *cr)
241 {
242   GtkStyleContext *context;
243
244   context = gtk_widget_get_style_context (widget);
245
246   gtk_style_context_save (context);
247
248   gtk_style_context_add_class (context, "frame");
249   gtk_style_context_set_junction_sides (context, 0);
250   gtk_render_frame_gap (context, cr, 12, 12, 50, 50, GTK_POS_TOP, 15, 35);
251   gtk_style_context_remove_class (context, "frame");
252
253   gtk_style_context_restore (context);
254
255   return TRUE;
256 }
257
258 static gboolean
259 draw_cb_handles (GtkWidget *widget, cairo_t *cr)
260 {
261   GtkStyleContext *context;
262
263   context = gtk_widget_get_style_context (widget);
264   gtk_style_context_save (context);
265
266   gtk_style_context_add_class (context, "paned");
267   gtk_render_handle (context, cr, 12, 22, 20, 10);
268   gtk_render_handle (context, cr, 44, 12, 10, 20);
269   gtk_style_context_remove_class (context, "paned");
270
271   gtk_style_context_add_class (context, "grip");
272   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_CORNER_BOTTOMLEFT);
273   gtk_render_handle (context, cr, 12, 48, 12, 12);
274
275   gtk_style_context_set_junction_sides (context, GTK_JUNCTION_CORNER_BOTTOMRIGHT);
276   gtk_render_handle (context, cr, 40, 48, 12, 12);
277
278   gtk_style_context_restore (context);
279
280   return TRUE;
281 }
282
283 static char *what;
284
285 static gboolean
286 draw_cb (GtkWidget *widget, cairo_t *cr)
287 {
288   if (strcmp (what, "check") == 0)
289     return draw_cb_checks (widget, cr);
290   else if (strcmp (what, "option") == 0)
291     return draw_cb_options (widget, cr);
292   else if (strcmp (what, "arrow") == 0)
293     return draw_cb_arrows (widget, cr);
294   else if (strcmp (what, "expander") == 0)
295     return draw_cb_expanders (widget, cr);
296   else if (strcmp (what, "background") == 0)
297     return draw_cb_background (widget, cr);
298   else if (strcmp (what, "frame") == 0)
299     return draw_cb_frame (widget, cr);
300   else if (strcmp (what, "activity") == 0)
301     return draw_cb_activity (widget, cr);
302   else if (strcmp (what, "slider") == 0)
303     return draw_cb_slider (widget, cr);
304   else if (strcmp (what, "focus") == 0)
305     return draw_cb_focus (widget, cr);
306   else if (strcmp (what, "extension") == 0)
307     return draw_cb_extension (widget, cr);
308   else if (strcmp (what, "frame-gap") == 0)
309     return draw_cb_frame_gap (widget, cr);
310   else if (strcmp (what, "handle") == 0)
311     return draw_cb_handles (widget, cr);
312
313   return FALSE;
314 }
315
316 int main (int argc, char *argv[])
317 {
318   GtkWidget *window;
319   GtkWidget *ebox;
320   GtkStyleContext *context;
321   GtkStyleProvider *provider;
322
323   gtk_init (&argc, &argv);
324
325   if (argc > 1)
326     what = argv[1];
327   else
328     what = "check";
329
330   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
331   gtk_window_set_has_resize_grip (GTK_WINDOW (window), FALSE);
332   ebox = gtk_event_box_new ();
333   gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), TRUE);
334   gtk_container_add (GTK_CONTAINER (window), ebox);
335   gtk_widget_set_name (ebox, "ebox");
336
337   context = gtk_widget_get_style_context (ebox);
338   provider = (GtkStyleProvider *)gtk_css_provider_new ();
339   gtk_css_provider_load_from_data (GTK_CSS_PROVIDER (provider),
340                                    ".frame1 {\n"
341                                    "   border-image: url('gradient1.png') 10 10 10 10 stretch;\n"
342                                    "}\n"
343                                    ".frame2 {\n"
344                                    "   border-style: solid;\n"
345                                    "   border-color: rgb(255,0,0);\n"
346                                    "   border-width: 10;\n"
347                                    "   border-radius: 10;\n"
348                                    "}\n"
349                                    ".frame3 {\n"
350                                    "   border-style: solid;\n"
351                                    "   border-color: rgb(0,0,0);\n"
352                                    "   border-width: 2;\n"
353                                    "   border-radius: 10;\n"
354                                    "}\n"
355                                    ".background {\n"
356                                    "   border-radius: 10;\n"
357                                    "   border-width: 0;\n"
358                                    "   background-image: -gtk-gradient (linear, left top, right bottom, from(#ff00ff), to(#aabbcc));\n"
359                                    "}\n"
360                                    ".frame {\n"
361                                    "   border-style: solid;\n"
362                                    "   border-width: 1;\n"
363                                    "   border-radius: 0;\n"
364                                    "}\n", -1, NULL);
365   gtk_style_context_add_provider (context, provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
366
367   g_signal_connect_after (ebox, "draw", G_CALLBACK (draw_cb), NULL);
368
369   gtk_widget_show_all (window);
370
371   gtk_main ();
372
373   gtk_style_context_remove_provider (context, provider);
374
375   return 0;
376 }
377