1 #include <gtk/gtk.h>
2 
3 /* Surface to store current scribbles */
4 static cairo_surface_t *surface = NULL;
5 
6 static void
clear_surface(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
configure_event_cb(GtkWidget * widget,GdkEventConfigure * event,gpointer data)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
draw_cb(GtkWidget * widget,cairo_t * cr,gpointer data)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
draw_brush(GtkWidget * widget,gdouble x,gdouble y)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
button_press_event_cb(GtkWidget * widget,GdkEventButton * event,gpointer data)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 == GDK_BUTTON_PRIMARY)
90     {
91       draw_brush (widget, event->x, event->y);
92     }
93   else if (event->button == GDK_BUTTON_SECONDARY)
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
motion_notify_event_cb(GtkWidget * widget,GdkEventMotion * event,gpointer data)108 motion_notify_event_cb (GtkWidget      *widget,
109                         GdkEventMotion *event,
110                         gpointer        data)
111 {
112   /* paranoia check, in case we haven't gotten a configure event */
113   if (surface == NULL)
114     return FALSE;
115 
116   if (event->state & GDK_BUTTON1_MASK)
117     draw_brush (widget, event->x, event->y);
118 
119   /* We've handled it, stop processing */
120   return TRUE;
121 }
122 
123 static void
close_window(void)124 close_window (void)
125 {
126   if (surface)
127     cairo_surface_destroy (surface);
128 }
129 
130 static void
activate(GtkApplication * app,gpointer user_data)131 activate (GtkApplication *app,
132           gpointer        user_data)
133 {
134   GtkWidget *window;
135   GtkWidget *frame;
136   GtkWidget *drawing_area;
137 
138   window = gtk_application_window_new (app);
139   gtk_window_set_title (GTK_WINDOW (window), "Drawing Area");
140 
141   g_signal_connect (window, "destroy", G_CALLBACK (close_window), NULL);
142 
143   gtk_container_set_border_width (GTK_CONTAINER (window), 8);
144 
145   frame = gtk_frame_new (NULL);
146   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
147   gtk_container_add (GTK_CONTAINER (window), frame);
148 
149   drawing_area = gtk_drawing_area_new ();
150   /* set a minimum size */
151   gtk_widget_set_size_request (drawing_area, 100, 100);
152 
153   gtk_container_add (GTK_CONTAINER (frame), drawing_area);
154 
155   /* Signals used to handle the backing surface */
156   g_signal_connect (drawing_area, "draw",
157                     G_CALLBACK (draw_cb), NULL);
158   g_signal_connect (drawing_area,"configure-event",
159                     G_CALLBACK (configure_event_cb), NULL);
160 
161   /* Event signals */
162   g_signal_connect (drawing_area, "motion-notify-event",
163                     G_CALLBACK (motion_notify_event_cb), NULL);
164   g_signal_connect (drawing_area, "button-press-event",
165                     G_CALLBACK (button_press_event_cb), NULL);
166 
167   /* Ask to receive events the drawing area doesn't normally
168    * subscribe to. In particular, we need to ask for the
169    * button press and motion notify events that want to handle.
170    */
171   gtk_widget_set_events (drawing_area, gtk_widget_get_events (drawing_area)
172                                      | GDK_BUTTON_PRESS_MASK
173                                      | GDK_POINTER_MOTION_MASK);
174 
175   gtk_widget_show_all (window);
176 }
177 
178 int
main(int argc,char ** argv)179 main (int    argc,
180       char **argv)
181 {
182   GtkApplication *app;
183   int status;
184 
185   app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
186   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
187   status = g_application_run (G_APPLICATION (app), argc, argv);
188   g_object_unref (app);
189 
190   return status;
191 }
192