garnacho / gtk-roles-and-siblings forked from aruiz/gtk-roles-and-siblings

Gtk+ branch with support for widget roles and sibling detection

gtk-roles-and-siblings / tests / testtheme.c
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 1 #include <gtk/gtk.h>
2
3 #define BORDER 5
4 #define N_COLS 3
5 #define N_ROWS 3
6
7 static gboolean
8 on_expose_event (GtkWidget *widget,
9 GdkEventExpose *event,
10 gpointer user_data)
11 {
12 GtkStyleContext *context;
13 gint x, y, width, height;
14 gint r, c;
15 cairo_t *cr;
16
17 cr = gdk_cairo_create (widget->window);
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 18
19 /* Ideally the widget will provide a context */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 20 context = g_object_new (GTK_TYPE_STYLE_CONTEXT, NULL);
21
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 22 /* Set radius for box borders */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 23 gtk_style_context_set_param_int (context, "radius", 15);
24
25 width = (widget->allocation.width - 2 * BORDER) / N_COLS;
26 height = (widget->allocation.height - 2 * BORDER) / N_ROWS;
27 x = y = BORDER;
28
29 for (r = 0; r < N_ROWS; r++)
30 {
31 GtkPlacingContext placing = 0;
32
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 33 /* Specify where will the row connect with others */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 34 if (r > 0)
35 placing |= GTK_PLACING_CONNECTS_UP;
36
37 if (r < N_ROWS - 1)
38 placing |= GTK_PLACING_CONNECTS_DOWN;
39
40 gtk_style_context_set_placing_context (context, placing);
41
42 for (c = 0; c < N_COLS; c++)
43 {
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 44 /* Save context so we can restore later the previous placing */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 45 gtk_style_context_save (context);
46
47 placing = gtk_style_context_get_placing_context (context);
48
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 49 /* Specify where will the column connect with others */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 50 if (c > 0)
51 placing |= GTK_PLACING_CONNECTS_LEFT;
52
53 if (c < N_COLS - 1)
54 placing |= GTK_PLACING_CONNECTS_RIGHT;
55
56 gtk_style_context_set_placing_context (context, placing);
57
58 if ((r + c) % 2)
59 {
60 GdkColor color = { 0, 65535, 65535, 65535 };
61 gtk_style_context_set_color (context, &color);
62 }
63
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 64 /* Paint box, GtkStyleContext will use the placing
65 * info to know which are the rounded corners in
66 * the box group.
67 */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 68 gtk_depict_box (context, cr, x, y, width, height);
69
70 x += width;
71
afeb3908 » garnacho 2009-02-04 Add a few comments to the t... 72 /* Restore context to reset placing to
73 * the previous value and unset color
74 */
53088a32 » garnacho 2009-02-04 Add simple testcase for the... 75 gtk_style_context_restore (context);
76 }
77
78 y += height;
79 x = BORDER;
80 }
81
82 g_object_unref (context);
83 cairo_destroy (cr);
84
85 return TRUE;
86 }
87
88 int
89 main (int argc, char *argv[])
90 {
91 GtkWidget *window, *event_box;
92
93 gtk_init (&argc, &argv);
94
95 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
96 event_box = gtk_event_box_new ();
97
98 gtk_container_add (GTK_CONTAINER (window), event_box);
99
100 g_signal_connect (window, "destroy",
101 G_CALLBACK (gtk_main_quit), NULL);
102 g_signal_connect (event_box, "expose-event",
103 G_CALLBACK (on_expose_event), NULL);
104
105 gtk_widget_show_all (window);
106
107 gtk_main ();
108
109 return 0;
110 }