1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Backing pixmap for drawing area */
22 static GdkPixmap *pixmap = NULL;
24 /* Create a new backing pixmap of the appropriate size */
26 configure_event (GtkWidget *widget, GdkEventConfigure *event)
29 gdk_pixmap_unref(pixmap);
31 pixmap = gdk_pixmap_new(widget->window,
32 widget->allocation.width,
33 widget->allocation.height,
35 gdk_draw_rectangle (pixmap,
36 widget->style->white_gc,
39 widget->allocation.width,
40 widget->allocation.height);
45 /* Redraw the screen from the backing pixmap */
47 expose_event (GtkWidget *widget, GdkEventExpose *event)
49 gdk_draw_pixmap(widget->window,
50 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
52 event->area.x, event->area.y,
53 event->area.x, event->area.y,
54 event->area.width, event->area.height);
59 /* Draw a rectangle on the screen */
61 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
63 GdkRectangle update_rect;
65 update_rect.x = x - 5;
66 update_rect.y = y - 5;
67 update_rect.width = 10;
68 update_rect.height = 10;
69 gdk_draw_rectangle (pixmap,
70 widget->style->black_gc,
72 update_rect.x, update_rect.y,
73 update_rect.width, update_rect.height);
74 gtk_widget_draw (widget, &update_rect);
78 button_press_event (GtkWidget *widget, GdkEventButton *event)
80 if (event->button == 1 && pixmap != NULL)
81 draw_brush (widget, event->x, event->y);
87 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
90 GdkModifierType state;
93 gdk_window_get_pointer (event->window, &x, &y, &state);
101 if (state & GDK_BUTTON1_MASK && pixmap != NULL)
102 draw_brush (widget, x, y);
114 main (int argc, char *argv[])
117 GtkWidget *drawing_area;
122 gtk_init (&argc, &argv);
124 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
125 gtk_widget_set_name (window, "Test Input");
127 vbox = gtk_vbox_new (FALSE, 0);
128 gtk_container_add (GTK_CONTAINER (window), vbox);
129 gtk_widget_show (vbox);
131 gtk_signal_connect (GTK_OBJECT (window), "destroy",
132 GTK_SIGNAL_FUNC (quit), NULL);
134 /* Create the drawing area */
136 drawing_area = gtk_drawing_area_new ();
137 gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
138 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
140 gtk_widget_show (drawing_area);
142 /* Signals used to handle backing pixmap */
144 gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
145 (GtkSignalFunc) expose_event, NULL);
146 gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
147 (GtkSignalFunc) configure_event, NULL);
151 gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
152 (GtkSignalFunc) motion_notify_event, NULL);
153 gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
154 (GtkSignalFunc) button_press_event, NULL);
156 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
157 | GDK_LEAVE_NOTIFY_MASK
158 | GDK_BUTTON_PRESS_MASK
159 | GDK_POINTER_MOTION_MASK
160 | GDK_POINTER_MOTION_HINT_MASK);
162 /* .. And a quit button */
163 button = gtk_button_new_with_label ("Quit");
164 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
166 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
167 GTK_SIGNAL_FUNC (gtk_widget_destroy),
168 GTK_OBJECT (window));
169 gtk_widget_show (button);
171 gtk_widget_show (window);