2 /* GTK - The GIMP Toolkit
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 /* Backing pixmap for drawing area */
25 static GdkPixmap *pixmap = NULL;
27 /* Create a new backing pixmap of the appropriate size */
28 static gboolean configure_event( GtkWidget *widget,
29 GdkEventConfigure *event )
32 g_object_unref (pixmap);
34 pixmap = gdk_pixmap_new (widget->window,
35 widget->allocation.width,
36 widget->allocation.height,
38 gdk_draw_rectangle (pixmap,
39 widget->style->white_gc,
42 widget->allocation.width,
43 widget->allocation.height);
48 /* Redraw the screen from the backing pixmap */
49 static gboolean expose_event( GtkWidget *widget,
50 GdkEventExpose *event )
52 gdk_draw_drawable (widget->window,
53 widget->style->fg_gc[gtk_widget_get_state (widget)],
55 event->area.x, event->area.y,
56 event->area.x, event->area.y,
57 event->area.width, event->area.height);
62 /* Draw a rectangle on the screen */
63 static void draw_brush( GtkWidget *widget,
67 GdkRectangle update_rect;
69 update_rect.x = x - 5;
70 update_rect.y = y - 5;
71 update_rect.width = 10;
72 update_rect.height = 10;
73 gdk_draw_rectangle (pixmap,
74 widget->style->black_gc,
76 update_rect.x, update_rect.y,
77 update_rect.width, update_rect.height);
78 gtk_widget_queue_draw_area (widget,
79 update_rect.x, update_rect.y,
80 update_rect.width, update_rect.height);
83 static gboolean button_press_event( GtkWidget *widget,
84 GdkEventButton *event )
86 if (event->button == 1 && pixmap != NULL)
87 draw_brush (widget, event->x, event->y);
92 static gboolean motion_notify_event( GtkWidget *widget,
93 GdkEventMotion *event )
96 GdkModifierType state;
99 gdk_window_get_pointer (event->window, &x, &y, &state);
104 state = event->state;
107 if (state & GDK_BUTTON1_MASK && pixmap != NULL)
108 draw_brush (widget, x, y);
122 GtkWidget *drawing_area;
127 gtk_init (&argc, &argv);
129 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130 gtk_widget_set_name (window, "Test Input");
132 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
133 gtk_container_add (GTK_CONTAINER (window), vbox);
134 gtk_widget_show (vbox);
136 g_signal_connect (window, "destroy",
137 G_CALLBACK (quit), NULL);
139 /* Create the drawing area */
141 drawing_area = gtk_drawing_area_new ();
142 gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 200, 200);
143 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
145 gtk_widget_show (drawing_area);
147 /* Signals used to handle backing pixmap */
149 g_signal_connect (drawing_area, "expose-event",
150 G_CALLBACK (expose_event), NULL);
151 g_signal_connect (drawing_area,"configure-event",
152 G_CALLBACK (configure_event), NULL);
156 g_signal_connect (drawing_area, "motion-notify-event",
157 G_CALLBACK (motion_notify_event), NULL);
158 g_signal_connect (drawing_area, "button-press-event",
159 G_CALLBACK (button_press_event), NULL);
161 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
162 | GDK_LEAVE_NOTIFY_MASK
163 | GDK_BUTTON_PRESS_MASK
164 | GDK_POINTER_MOTION_MASK
165 | GDK_POINTER_MOTION_HINT_MASK);
167 /* .. And a quit button */
168 button = gtk_button_new_with_label ("Quit");
169 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
171 g_signal_connect_swapped (button, "clicked",
172 G_CALLBACK (gtk_widget_destroy),
174 gtk_widget_show (button);
176 gtk_widget_show (window);