]> Pileus Git - ~andy/gtk/blob - examples/drawing.c
Remove latest bits of GtkItemFactory
[~andy/gtk] / examples / drawing.c
1 #include <gtk/gtk.h>
2
3 /* Surface to store current scribbles */
4 static cairo_surface_t *surface = NULL;
5
6 static void
7 clear_surface (void)
8 {
9   cairo_t *cr;
10
11   cr = cairo_create (surface);
12
13   cairo_set_source_rgb (cr, 1, 1, 1);
14   cairo_paint (cr);
15
16   cairo_destroy (cr);
17 }
18
19 /* Create a new surface of the appropriate size to store our scribbles */
20 static gboolean
21 configure_event_cb (GtkWidget         *widget,
22                     GdkEventConfigure *event,
23                     gpointer           data)
24 {
25   if (surface)
26     cairo_surface_destroy (surface);
27
28   surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
29                                                CAIRO_CONTENT_COLOR,
30                                                gtk_widget_get_allocated_width (widget),
31                                                gtk_widget_get_allocated_height (widget));
32
33   /* Initialize the surface to white */
34   clear_surface ();
35
36   /* We've handled the configure event, no need for further processing. */
37   return TRUE;
38 }
39
40 /* Redraw the screen from the surface. Note that the ::draw
41  * signal receives a ready-to-be-used cairo_t that is already
42  * clipped to only draw the exposed areas of the widget
43  */
44 static gboolean
45 draw_cb (GtkWidget *widget,
46          cairo_t   *cr,
47          gpointer   data)
48 {
49   cairo_set_source_surface (cr, surface, 0, 0);
50   cairo_paint (cr);
51
52   return FALSE;
53 }
54
55 /* Draw a rectangle on the surface at the given position */
56 static void
57 draw_brush (GtkWidget *widget,
58             gdouble    x,
59             gdouble    y)
60 {
61   cairo_t *cr;
62
63   /* Paint to the surface, where we store our state */
64   cr = cairo_create (surface);
65
66   cairo_rectangle (cr, x - 3, y - 3, 6, 6);
67   cairo_fill (cr);
68
69   cairo_destroy (cr);
70
71   /* Now invalidate the affected region of the drawing area. */
72   gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
73 }
74
75 /* Handle button press events by either drawing a rectangle
76  * or clearing the surface, depending on which button was pressed.
77  * The ::button-press signal handler receives a GdkEventButton
78  * struct which contains this information.
79  */
80 static gboolean
81 button_press_event_cb (GtkWidget      *widget,
82                        GdkEventButton *event,
83                        gpointer        data)
84 {
85   /* paranoia check, in case we haven't gotten a configure event */
86   if (surface == NULL)
87     return FALSE;
88
89   if (event->button == 1)
90     {
91       draw_brush (widget, event->x, event->y);
92     }
93   else if (event->button == 3)
94     {
95       clear_surface ();
96       gtk_widget_queue_draw (widget);
97     }
98
99   /* We've handled the event, stop processing */
100   return TRUE;
101 }
102
103 /* Handle motion events by continuing to draw if button 1 is
104  * still held down. The ::motion-notify signal handler receives
105  * a GdkEventMotion struct which contains this information.
106  */
107 static gboolean
108 motion_notify_event_cb (GtkWidget      *widget,
109                         GdkEventMotion *event,
110                         gpointer        data)
111 {
112   int x, y;
113   GdkModifierType state;
114
115   /* paranoia check, in case we haven't gotten a configure event */
116   if (surface == NULL)
117     return FALSE;
118
119   /* This call is very important; it requests the next motion event.
120    * If you don't call gdk_window_get_pointer() you'll only get
121    * a single motion event. The reason is that we specified
122    * GDK_POINTER_MOTION_HINT_MASK to gtk_widget_set_events().
123    * If we hadn't specified that, we could just use event->x, event->y
124    * as the pointer location. But we'd also get deluged in events.
125    * By requesting the next event as we handle the current one,
126    * we avoid getting a huge number of events faster than we
127    * can cope.
128    */
129   gdk_window_get_pointer (event->window, &x, &y, &state);
130
131   if (state & GDK_BUTTON1_MASK)
132     draw_brush (widget, x, y);
133
134   /* We've handled it, stop processing */
135   return TRUE;
136 }
137
138 static void
139 close_window (void)
140 {
141   if (surface)
142     cairo_surface_destroy (surface);
143
144   gtk_main_quit ();
145 }
146
147 int
148 main (int   argc,
149       char *argv[])
150 {
151   GtkWidget *window;
152   GtkWidget *frame;
153   GtkWidget *da;
154
155   gtk_init (&argc, &argv);
156
157   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
158   gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
159
160   g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
161
162   gtk_container_set_border_width (GTK_CONTAINER (window), 8);
163
164   frame = gtk_frame_new (NULL);
165   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
166   gtk_container_add (GTK_CONTAINER (window), frame);
167
168   da = gtk_drawing_area_new ();
169   /* set a minimum size */
170   gtk_widget_set_size_request (da, 100, 100);
171
172   gtk_container_add (GTK_CONTAINER (frame), da);
173
174   /* Signals used to handle the backing surface */
175   g_signal_connect (da, "draw",
176                     G_CALLBACK (draw_cb), NULL);
177   g_signal_connect (da,"configure-event",
178                     G_CALLBACK (configure_event_cb), NULL);
179
180   /* Event signals */
181   g_signal_connect (da, "motion-notify-event",
182                     G_CALLBACK (motion_notify_event_cb), NULL);
183   g_signal_connect (da, "button-press-event",
184                     G_CALLBACK (button_press_event_cb), NULL);
185
186   /* Ask to receive events the drawing area doesn't normally
187    * subscribe to. In particular, we need to ask for the
188    * button press and motion notify events that want to handle.
189    */
190   gtk_widget_set_events (da, gtk_widget_get_events (da)
191                              | GDK_BUTTON_PRESS_MASK
192                              | GDK_POINTER_MOTION_MASK
193                              | GDK_POINTER_MOTION_HINT_MASK);
194
195   gtk_widget_show_all (window);
196
197   gtk_main ();
198
199   return 0;
200 }