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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 /* Backing pixmap for drawing area */
23 static GdkPixmap *pixmap = NULL;
25 /* Create a new backing pixmap of the appropriate size */
27 configure_event (GtkWidget *widget, GdkEventConfigure *event)
30 gdk_pixmap_unref(pixmap);
32 pixmap = gdk_pixmap_new(widget->window,
33 widget->allocation.width,
34 widget->allocation.height,
36 gdk_draw_rectangle (pixmap,
37 widget->style->white_gc,
40 widget->allocation.width,
41 widget->allocation.height);
46 /* Redraw the screen from the backing pixmap */
48 expose_event (GtkWidget *widget, GdkEventExpose *event)
50 gdk_draw_pixmap(widget->window,
51 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
53 event->area.x, event->area.y,
54 event->area.x, event->area.y,
55 event->area.width, event->area.height);
60 /* Draw a rectangle on the screen */
62 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
64 GdkRectangle update_rect;
66 update_rect.x = x - 5;
67 update_rect.y = y - 5;
68 update_rect.width = 10;
69 update_rect.height = 10;
70 gdk_draw_rectangle (pixmap,
71 widget->style->black_gc,
73 update_rect.x, update_rect.y,
74 update_rect.width, update_rect.height);
75 gtk_widget_draw (widget, &update_rect);
79 button_press_event (GtkWidget *widget, GdkEventButton *event)
81 if (event->button == 1 && pixmap != NULL)
82 draw_brush (widget, event->x, event->y);
88 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
91 GdkModifierType state;
94 gdk_window_get_pointer (event->window, &x, &y, &state);
102 if (state & GDK_BUTTON1_MASK && pixmap != NULL)
103 draw_brush (widget, x, y);
115 main (int argc, char *argv[])
118 GtkWidget *drawing_area;
123 gtk_init (&argc, &argv);
125 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
126 gtk_widget_set_name (window, "Test Input");
128 vbox = gtk_vbox_new (FALSE, 0);
129 gtk_container_add (GTK_CONTAINER (window), vbox);
130 gtk_widget_show (vbox);
132 gtk_signal_connect (GTK_OBJECT (window), "destroy",
133 GTK_SIGNAL_FUNC (quit), NULL);
135 /* Create the drawing area */
137 drawing_area = gtk_drawing_area_new ();
138 gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
139 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
141 gtk_widget_show (drawing_area);
143 /* Signals used to handle backing pixmap */
145 gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
146 (GtkSignalFunc) expose_event, NULL);
147 gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
148 (GtkSignalFunc) configure_event, NULL);
152 gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
153 (GtkSignalFunc) motion_notify_event, NULL);
154 gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
155 (GtkSignalFunc) button_press_event, NULL);
157 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
158 | GDK_LEAVE_NOTIFY_MASK
159 | GDK_BUTTON_PRESS_MASK
160 | GDK_POINTER_MOTION_MASK
161 | GDK_POINTER_MOTION_HINT_MASK);
163 /* .. And a quit button */
164 button = gtk_button_new_with_label ("Quit");
165 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
167 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
168 GTK_SIGNAL_FUNC (gtk_widget_destroy),
169 GTK_OBJECT (window));
170 gtk_widget_show (button);
172 gtk_widget_show (window);