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 cairo_surface_t *surface = NULL;
22 /* Create a new surface of the appropriate size to store our scribbles */
24 scribble_configure_event (GtkWidget *widget,
25 GdkEventConfigure *event,
28 GtkAllocation allocation;
32 cairo_surface_destroy (surface);
34 gtk_widget_get_allocation (widget, &allocation);
35 surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
40 /* Initialize the surface to white */
41 cr = cairo_create (surface);
43 cairo_set_source_rgb (cr, 1, 1, 1);
48 /* We've handled the configure event, no need for further processing. */
52 /* Redraw the screen from the surface */
54 scribble_draw (GtkWidget *widget,
58 cairo_set_source_surface (cr, surface, 0, 0);
64 /* Draw a rectangle on the screen */
66 draw_brush (GtkWidget *widget,
70 GdkRectangle update_rect;
73 update_rect.x = x - 3;
74 update_rect.y = y - 3;
75 update_rect.width = 6;
76 update_rect.height = 6;
78 /* Paint to the surface, where we store our state */
79 cr = cairo_create (surface);
81 gdk_cairo_rectangle (cr, &update_rect);
86 /* Now invalidate the affected region of the drawing area. */
87 gdk_window_invalidate_rect (gtk_widget_get_window (widget),
93 scribble_button_press_event (GtkWidget *widget,
94 GdkEventButton *event,
98 return FALSE; /* paranoia check, in case we haven't gotten a configure event */
100 if (event->button == 1)
101 draw_brush (widget, event->x, event->y);
103 /* We've handled the event, stop processing */
108 scribble_motion_notify_event (GtkWidget *widget,
109 GdkEventMotion *event,
113 GdkModifierType state;
116 return FALSE; /* paranoia check, in case we haven't gotten a configure event */
118 /* This call is very important; it requests the next motion event.
119 * If you don't call gdk_window_get_pointer() you'll only get
120 * a single motion event. The reason is that we specified
121 * GDK_POINTER_MOTION_HINT_MASK to gtk_widget_set_events().
122 * If we hadn't specified that, we could just use event->x, event->y
123 * as the pointer location. But we'd also get deluged in events.
124 * By requesting the next event as we handle the current one,
125 * we avoid getting a huge number of events faster than we
129 gdk_window_get_pointer (event->window, &x, &y, &state);
131 if (state & GDK_BUTTON1_MASK)
132 draw_brush (widget, x, y);
134 /* We've handled it, stop processing */
140 checkerboard_draw (GtkWidget *da,
144 gint i, j, xcount, ycount, width, height;
146 #define CHECK_SIZE 10
149 /* At the start of a draw handler, a clip region has been set on
150 * the Cairo context, and the contents have been cleared to the
151 * widget's background color. The docs for
152 * gdk_window_begin_paint_region() give more details on how this
157 width = gtk_widget_get_allocated_width (da);
158 height = gtk_widget_get_allocated_height (da);
163 ycount = xcount % 2; /* start with even/odd depending on row */
167 cairo_set_source_rgb (cr, 0.45777, 0, 0.45777);
169 cairo_set_source_rgb (cr, 1, 1, 1);
171 /* If we're outside the clip, this will do nothing.
173 cairo_rectangle (cr, i, j, CHECK_SIZE, CHECK_SIZE);
176 j += CHECK_SIZE + SPACING;
180 i += CHECK_SIZE + SPACING;
184 /* return TRUE because we've handled this event, so no
185 * further processing is required.
196 cairo_surface_destroy (surface);
201 do_drawingarea (GtkWidget *do_widget)
210 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
211 gtk_window_set_screen (GTK_WINDOW (window),
212 gtk_widget_get_screen (do_widget));
213 gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
215 g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
217 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
219 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 8);
220 gtk_container_set_border_width (GTK_CONTAINER (vbox), 8);
221 gtk_container_add (GTK_CONTAINER (window), vbox);
224 * Create the checkerboard area
227 label = gtk_label_new (NULL);
228 gtk_label_set_markup (GTK_LABEL (label),
229 "<u>Checkerboard pattern</u>");
230 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
232 frame = gtk_frame_new (NULL);
233 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
234 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
236 da = gtk_drawing_area_new ();
237 /* set a minimum size */
238 gtk_widget_set_size_request (da, 100, 100);
240 gtk_container_add (GTK_CONTAINER (frame), da);
242 g_signal_connect (da, "draw",
243 G_CALLBACK (checkerboard_draw), NULL);
246 * Create the scribble area
249 label = gtk_label_new (NULL);
250 gtk_label_set_markup (GTK_LABEL (label),
251 "<u>Scribble area</u>");
252 gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
254 frame = gtk_frame_new (NULL);
255 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
256 gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
258 da = gtk_drawing_area_new ();
259 /* set a minimum size */
260 gtk_widget_set_size_request (da, 100, 100);
262 gtk_container_add (GTK_CONTAINER (frame), da);
264 /* Signals used to handle backing surface */
266 g_signal_connect (da, "draw",
267 G_CALLBACK (scribble_draw), NULL);
268 g_signal_connect (da,"configure-event",
269 G_CALLBACK (scribble_configure_event), NULL);
273 g_signal_connect (da, "motion-notify-event",
274 G_CALLBACK (scribble_motion_notify_event), NULL);
275 g_signal_connect (da, "button-press-event",
276 G_CALLBACK (scribble_button_press_event), NULL);
279 /* Ask to receive events the drawing area doesn't normally
282 gtk_widget_set_events (da, gtk_widget_get_events (da)
283 | GDK_LEAVE_NOTIFY_MASK
284 | GDK_BUTTON_PRESS_MASK
285 | GDK_POINTER_MOTION_MASK
286 | GDK_POINTER_MOTION_HINT_MASK);
290 if (!gtk_widget_get_visible (window))
291 gtk_widget_show_all (window);
293 gtk_widget_destroy (window);