3 /* Surface to store current scribbles */
4 static cairo_surface_t *surface = NULL;
11 cr = cairo_create (surface);
13 cairo_set_source_rgb (cr, 1, 1, 1);
19 /* Create a new surface of the appropriate size to store our scribbles */
21 configure_event_cb (GtkWidget *widget,
22 GdkEventConfigure *event,
26 cairo_surface_destroy (surface);
28 surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
30 gtk_widget_get_allocated_width (widget),
31 gtk_widget_get_allocated_height (widget));
33 /* Initialize the surface to white */
36 /* We've handled the configure event, no need for further processing. */
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
45 draw_cb (GtkWidget *widget,
49 cairo_set_source_surface (cr, surface, 0, 0);
55 /* Draw a rectangle on the surface at the given position */
57 draw_brush (GtkWidget *widget,
63 /* Paint to the surface, where we store our state */
64 cr = cairo_create (surface);
66 cairo_rectangle (cr, x - 3, y - 3, 6, 6);
71 /* Now invalidate the affected region of the drawing area. */
72 gtk_widget_queue_draw_area (widget, x - 3, y - 3, 6, 6);
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.
81 button_press_event_cb (GtkWidget *widget,
82 GdkEventButton *event,
85 /* paranoia check, in case we haven't gotten a configure event */
89 if (event->button == GDK_BUTTON_PRIMARY)
91 draw_brush (widget, event->x, event->y);
93 else if (event->button == GDK_BUTTON_SECONDARY)
96 gtk_widget_queue_draw (widget);
99 /* We've handled the event, stop processing */
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.
108 motion_notify_event_cb (GtkWidget *widget,
109 GdkEventMotion *event,
112 /* paranoia check, in case we haven't gotten a configure event */
116 if (event->state & GDK_BUTTON1_MASK)
117 draw_brush (widget, event->x, event->y);
119 /* We've handled it, stop processing */
127 cairo_surface_destroy (surface);
140 gtk_init (&argc, &argv);
142 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
143 gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
145 g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
147 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
149 frame = gtk_frame_new (NULL);
150 gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
151 gtk_container_add (GTK_CONTAINER (window), frame);
153 da = gtk_drawing_area_new ();
154 /* set a minimum size */
155 gtk_widget_set_size_request (da, 100, 100);
157 gtk_container_add (GTK_CONTAINER (frame), da);
159 /* Signals used to handle the backing surface */
160 g_signal_connect (da, "draw",
161 G_CALLBACK (draw_cb), NULL);
162 g_signal_connect (da,"configure-event",
163 G_CALLBACK (configure_event_cb), NULL);
166 g_signal_connect (da, "motion-notify-event",
167 G_CALLBACK (motion_notify_event_cb), NULL);
168 g_signal_connect (da, "button-press-event",
169 G_CALLBACK (button_press_event_cb), NULL);
171 /* Ask to receive events the drawing area doesn't normally
172 * subscribe to. In particular, we need to ask for the
173 * button press and motion notify events that want to handle.
175 gtk_widget_set_events (da, gtk_widget_get_events (da)
176 | GDK_BUTTON_PRESS_MASK
177 | GDK_POINTER_MOTION_MASK);
179 gtk_widget_show_all (window);