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.
23 /* Backing pixmap for drawing area */
24 static GdkPixmap *pixmap = NULL;
26 /* Create a new backing pixmap of the appropriate size */
28 configure_event (GtkWidget *widget, GdkEventConfigure *event)
31 g_object_unref (pixmap);
33 pixmap = gdk_pixmap_new (widget->window,
34 widget->allocation.width,
35 widget->allocation.height,
37 gdk_draw_rectangle (pixmap,
38 widget->style->white_gc,
41 widget->allocation.width,
42 widget->allocation.height);
47 /* Redraw the screen from the backing pixmap */
49 expose_event (GtkWidget *widget, GdkEventExpose *event)
51 gdk_draw_drawable (widget->window,
52 widget->style->fg_gc[gtk_widget_get_state (widget)],
54 event->area.x, event->area.y,
55 event->area.x, event->area.y,
56 event->area.width, event->area.height);
61 /* Draw a rectangle on the screen, size depending on pressure,
62 and color on the type of device */
64 draw_brush (GtkWidget *widget, GdkInputSource source,
65 gdouble x, gdouble y, gdouble pressure)
68 GdkRectangle update_rect;
72 case GDK_SOURCE_MOUSE:
73 gc = widget->style->dark_gc[gtk_widget_get_state (widget)];
76 gc = widget->style->black_gc;
78 case GDK_SOURCE_ERASER:
79 gc = widget->style->white_gc;
82 gc = widget->style->light_gc[gtk_widget_get_state (widget)];
85 update_rect.x = x - 10 * pressure;
86 update_rect.y = y - 10 * pressure;
87 update_rect.width = 20 * pressure;
88 update_rect.height = 20 * pressure;
89 gdk_draw_rectangle (pixmap, gc, TRUE,
90 update_rect.x, update_rect.y,
91 update_rect.width, update_rect.height);
92 gtk_widget_queue_draw_area (widget,
93 update_rect.x, update_rect.y,
94 update_rect.width, update_rect.height);
98 print_button_press (GdkDevice *device)
100 g_print ("Button press on device '%s'\n", device->name);
104 button_press_event (GtkWidget *widget, GdkEventButton *event)
106 print_button_press (event->device);
108 if (event->button == 1 && pixmap != NULL) {
110 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
111 draw_brush (widget, event->device->source, event->x, event->y, pressure);
118 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
122 GdkModifierType state;
126 gdk_device_get_state (event->device, event->window, NULL, &state);
127 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_X, &x);
128 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_Y, &y);
129 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
135 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
136 state = event->state;
139 if (state & GDK_BUTTON1_MASK && pixmap != NULL)
140 draw_brush (widget, event->device->source, x, y, pressure);
146 input_dialog_destroy (GtkWidget *w, gpointer data)
148 *((GtkWidget **)data) = NULL;
152 create_input_dialog ()
154 static GtkWidget *inputd = NULL;
158 inputd = gtk_input_dialog_new();
160 g_signal_connect (inputd, "destroy",
161 G_CALLBACK (input_dialog_destroy), &inputd);
162 g_signal_connect_swapped (GTK_INPUT_DIALOG (inputd)->close_button,
164 G_CALLBACK (gtk_widget_hide),
166 gtk_widget_hide (GTK_INPUT_DIALOG (inputd)->save_button);
168 gtk_widget_show (inputd);
172 if (!gtk_widget_get_mapped (inputd))
173 gtk_widget_show (inputd);
175 gdk_window_raise (inputd->window);
180 main (int argc, char *argv[])
183 GtkWidget *drawing_area;
188 gtk_init (&argc, &argv);
190 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
191 gtk_widget_set_name (window, "Test Input");
193 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
194 gtk_container_add (GTK_CONTAINER (window), vbox);
195 gtk_widget_show (vbox);
197 g_signal_connect (window, "destroy",
198 G_CALLBACK (gtk_main_quit), NULL);
200 /* Create the drawing area */
202 drawing_area = gtk_drawing_area_new ();
203 gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 200, 200);
204 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
206 gtk_widget_show (drawing_area);
208 /* Signals used to handle backing pixmap */
210 g_signal_connect (drawing_area, "expose-event",
211 G_CALLBACK (expose_event), NULL);
212 g_signal_connect (drawing_area,"configure-event",
213 G_CALLBACK (configure_event), NULL);
217 g_signal_connect (drawing_area, "motion-notify-event",
218 G_CALLBACK (motion_notify_event), NULL);
219 g_signal_connect (drawing_area, "button-press-event",
220 G_CALLBACK (button_press_event), NULL);
222 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
223 | GDK_LEAVE_NOTIFY_MASK
224 | GDK_BUTTON_PRESS_MASK
225 | GDK_POINTER_MOTION_MASK
226 | GDK_POINTER_MOTION_HINT_MASK);
228 /* The following call enables tracking and processing of extension
229 events for the drawing area */
230 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_CURSOR);
232 /* .. And some buttons */
233 button = gtk_button_new_with_label ("Input Dialog");
234 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
236 g_signal_connect (button, "clicked",
237 G_CALLBACK (create_input_dialog), NULL);
238 gtk_widget_show (button);
240 button = gtk_button_new_with_label ("Quit");
241 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
243 g_signal_connect_swapped (button, "clicked",
244 G_CALLBACK (gtk_widget_destroy),
246 gtk_widget_show (button);
248 gtk_widget_show (window);