3 * GtkDrawingArea is a blank area where you can draw custom displays
6 * This demo has two drawing areas. The checkerboard area shows
7 * how you can just draw something; all you have to do is write
8 * a signal handler for expose_event, as shown here.
10 * The "scribble" area is a bit more advanced, and shows how to handle
11 * events such as button presses and mouse motion. Click the mouse
12 * and drag in the scribble area to draw squiggles. Resize the window
18 static GtkWidget *window = NULL;
19 /* Pixmap for scribble area, to store current scribbles */
20 static GdkPixmap *pixmap = NULL;
22 /* Create a new pixmap of the appropriate size to store our scribbles */
24 scribble_configure_event (GtkWidget *widget,
25 GdkEventConfigure *event,
31 g_object_unref (pixmap);
33 pixmap = gdk_pixmap_new (widget->window,
34 widget->allocation.width,
35 widget->allocation.height,
38 /* Initialize the pixmap to white */
39 cr = gdk_cairo_create (pixmap);
41 cairo_set_source_rgb (cr, 1, 1, 1);
46 /* We've handled the configure event, no need for further processing. */
50 /* Redraw the screen from the pixmap */
52 scribble_expose_event (GtkWidget *widget,
53 GdkEventExpose *event,
58 cr = gdk_cairo_create (widget->window);
60 gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
61 gdk_cairo_rectangle (cr, &event->area);
69 /* Draw a rectangle on the screen */
71 draw_brush (GtkWidget *widget,
75 GdkRectangle update_rect;
78 update_rect.x = x - 3;
79 update_rect.y = y - 3;
80 update_rect.width = 6;
81 update_rect.height = 6;
83 /* Paint to the pixmap, where we store our state */
84 cr = gdk_cairo_create (pixmap);
86 gdk_cairo_rectangle (cr, &update_rect);
89 /* Now invalidate the affected region of the drawing area. */
90 gdk_window_invalidate_rect (widget->window,
96 scribble_button_press_event (GtkWidget *widget,
97 GdkEventButton *event,
101 return FALSE; /* paranoia check, in case we haven't gotten a configure event */
103 if (event->button == 1)
104 draw_brush (widget, event->x, event->y);
106 /* We've handled the event, stop processing */
111 scribble_motion_notify_event (GtkWidget *widget,
112 GdkEventMotion *event,
116 GdkModifierType state;
119 return FALSE; /* paranoia check, in case we haven't gotten a configure event */
121 /* This call is very important; it requests the next motion event.
122 * If you don't call gdk_window_get_pointer() you'll only get
123 * a single motion event. The reason is that we specified
124 * GDK_POINTER_MOTION_HINT_MASK to gtk_widget_set_events().
125 * If we hadn't specified that, we could just use event->x, event->y
126 * as the pointer location. But we'd also get deluged in events.
127 * By requesting the next event as we handle the current one,
128 * we avoid getting a huge number of events faster than we
132 gdk_window_get_pointer (event->window, &x, &y, &state);
134 if (state & GDK_BUTTON1_MASK)
135 draw_brush (widget, x, y);
137 /* We've handled it, stop processing */
143 checkerboard_expose (GtkWidget *da,
144 GdkEventExpose *event,
147 gint i, j, xcount, ycount;
150 #define CHECK_SIZE 10
153 /* At the start of an expose handler, a clip region of event->area
154 * is set on the window, and event->area has been cleared to the
155 * widget's background color. The docs for
156 * gdk_window_begin_paint_region() give more details on how this
160 cr = gdk_cairo_create (da->window);
161 gdk_cairo_rectangle (cr, &event->area);
166 while (i < da->allocation.width)
169 ycount = xcount % 2; /* start with even/odd depending on row */
170 while (j < da->allocation.height)
173 cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
175 cairo_set_source_rgb (cr, 1, 1, 1);
177 /* If we're outside event->area, this will do nothing.
178 * It might be mildly more efficient if we handled
179 * the clipping ourselves, but again we're feeling lazy.
181 cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
184 j += CHECK_SIZE + SPACING;
188 i += CHECK_SIZE + SPACING;
194 /* return TRUE because we've handled this event, so no
195 * further processing is required.
206 g_object_unref (pixmap);
211 do_drawingarea (GtkWidget *do_widget)
220 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
221 gtk_window_set_screen (GTK_WINDOW (window),
222 gtk_widget_get_screen (do_widget));
223 gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
225 g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
227 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
229 vbox = gtk_vbox_new (FALSE, 8);
230 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
231 gtk_container_add (GTK_CONTAINER (window), vbox);
234 * Create the checkerboard area
237 label = gtk_label_new (NULL);
238 gtk_label_set_markup (GTK_LABEL (label),
239 "<u>Checkerboard pattern</u>");
240 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
242 frame = gtk_frame_new (NULL);
243 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
244 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
246 da = gtk_drawing_area_new ();
247 /* set a minimum size */
248 gtk_widget_set_size_request (da, 100, 100);
250 gtk_container_add (GTK_CONTAINER (frame), da);
252 g_signal_connect (da, "expose-event",
253 G_CALLBACK (checkerboard_expose), NULL);
256 * Create the scribble area
259 label = gtk_label_new (NULL);
260 gtk_label_set_markup (GTK_LABEL (label),
261 "<u>Scribble area</u>");
262 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
264 frame = gtk_frame_new (NULL);
265 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
266 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
268 da = gtk_drawing_area_new ();
269 /* set a minimum size */
270 gtk_widget_set_size_request (da, 100, 100);
272 gtk_container_add (GTK_CONTAINER (frame), da);
274 /* Signals used to handle backing pixmap */
276 g_signal_connect (da, "expose-event",
277 G_CALLBACK (scribble_expose_event), NULL);
278 g_signal_connect (da,"configure-event",
279 G_CALLBACK (scribble_configure_event), NULL);
283 g_signal_connect (da, "motion-notify-event",
284 G_CALLBACK (scribble_motion_notify_event), NULL);
285 g_signal_connect (da, "button-press-event",
286 G_CALLBACK (scribble_button_press_event), NULL);
289 /* Ask to receive events the drawing area doesn't normally
292 gtk_widget_set_events (da, gtk_widget_get_events (da)
293 | GDK_LEAVE_NOTIFY_MASK
294 | GDK_BUTTON_PRESS_MASK
295 | GDK_POINTER_MOTION_MASK
296 | GDK_POINTER_MOTION_HINT_MASK);
300 if (!gtk_widget_get_visible (window))
301 gtk_widget_show_all (window);
303 gtk_widget_destroy (window);