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