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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
30 /* Backing pixmap for drawing area */
32 static GdkPixmap *pixmap = NULL;
34 /* Information about cursor */
36 static gint cursor_proximity = TRUE;
37 static gdouble cursor_x;
38 static gdouble cursor_y;
40 /* Unique ID of current device */
41 static GdkDevice *current_device;
43 /* Erase the old cursor, and/or draw a new one, if necessary */
45 update_cursor (GtkWidget *widget, gdouble x, gdouble y)
47 static gint cursor_present = 0;
48 gint state = !current_device->has_cursor && cursor_proximity;
52 if (cursor_present && (cursor_present != state ||
53 x != cursor_x || y != cursor_y))
55 gdk_draw_pixmap(widget->window,
56 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
58 cursor_x - 5, cursor_y - 5,
59 cursor_x - 5, cursor_y - 5,
63 cursor_present = state;
69 gdk_draw_rectangle (widget->window,
70 widget->style->black_gc,
72 cursor_x - 5, cursor_y -5,
78 /* Create a new backing pixmap of the appropriate size */
80 configure_event (GtkWidget *widget, GdkEventConfigure *event)
83 gdk_pixmap_unref (pixmap);
84 pixmap = gdk_pixmap_new(widget->window,
85 widget->allocation.width,
86 widget->allocation.height,
88 gdk_draw_rectangle (pixmap,
89 widget->style->white_gc,
92 widget->allocation.width,
93 widget->allocation.height);
98 /* Refill the screen from the backing pixmap */
100 expose_event (GtkWidget *widget, GdkEventExpose *event)
102 gdk_draw_pixmap(widget->window,
103 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
105 event->area.x, event->area.y,
106 event->area.x, event->area.y,
107 event->area.width, event->area.height);
112 /* Draw a rectangle on the screen, size depending on pressure,
113 and color on the type of device */
115 draw_brush (GtkWidget *widget, GdkInputSource source,
116 gdouble x, gdouble y, gdouble pressure)
119 GdkRectangle update_rect;
123 case GDK_SOURCE_MOUSE:
124 gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
127 gc = widget->style->black_gc;
129 case GDK_SOURCE_ERASER:
130 gc = widget->style->white_gc;
133 gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
136 update_rect.x = x - 10 * pressure;
137 update_rect.y = y - 10 * pressure;
138 update_rect.width = 20 * pressure;
139 update_rect.height = 20 * pressure;
140 gdk_draw_rectangle (pixmap, gc, TRUE,
141 update_rect.x, update_rect.y,
142 update_rect.width, update_rect.height);
143 gtk_widget_draw (widget, &update_rect);
146 static guint32 motion_time;
149 print_axes (GdkDevice *device, gdouble *axes)
155 g_print ("%s ", device->name);
157 for (i=0; i<device->num_axes; i++)
158 g_print ("%g ", axes[i]);
165 button_press_event (GtkWidget *widget, GdkEventButton *event)
167 current_device = event->device;
168 cursor_proximity = TRUE;
170 if (event->button == 1 && pixmap != NULL)
172 gdouble pressure = 0.5;
174 print_axes (event->device, event->axes);
175 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
176 draw_brush (widget, event->device->source, event->x, event->y, pressure);
178 motion_time = event->time;
181 update_cursor (widget, event->x, event->y);
187 key_press_event (GtkWidget *widget, GdkEventKey *event)
189 if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
190 printf("I got a %c\n", event->keyval);
192 printf("I got some other key\n");
198 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
200 GdkTimeCoord **events;
204 current_device = event->device;
205 cursor_proximity = TRUE;
207 if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
209 if (gdk_device_get_history (event->device, event->window,
210 motion_time, event->time,
213 for (i=0; i<n_events; i++)
215 double x = 0, y = 0, pressure = 0.5;
217 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_X, &x);
218 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_Y, &y);
219 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_PRESSURE, &pressure);
220 draw_brush (widget, event->device->source, x, y, pressure);
222 print_axes (event->device, events[i]->axes);
224 gdk_device_free_history (events, n_events);
228 double pressure = 0.5;
230 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
232 draw_brush (widget, event->device->source, event->x, event->y, pressure);
234 motion_time = event->time;
238 gdk_device_get_state (event->device, event->window, NULL, NULL);
240 print_axes (event->device, event->axes);
241 update_cursor (widget, event->x, event->y);
246 /* We track the next two events to know when we need to draw a
250 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
252 cursor_proximity = FALSE;
253 update_cursor (widget, cursor_x, cursor_y);
258 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
260 cursor_proximity = FALSE;
261 update_cursor (widget, cursor_x, cursor_y);
266 input_dialog_destroy (GtkWidget *w, gpointer data)
268 *((GtkWidget **)data) = NULL;
272 create_input_dialog (void)
274 static GtkWidget *inputd = NULL;
278 inputd = gtk_input_dialog_new();
280 gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
281 (GtkSignalFunc)input_dialog_destroy, &inputd);
282 gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
284 (GtkSignalFunc)gtk_widget_hide,
286 gtk_widget_hide (GTK_INPUT_DIALOG(inputd)->save_button);
288 gtk_widget_show (inputd);
292 if (!GTK_WIDGET_MAPPED(inputd))
293 gtk_widget_show(inputd);
295 gdk_window_raise(inputd->window);
306 main (int argc, char *argv[])
309 GtkWidget *drawing_area;
314 gtk_init (&argc, &argv);
316 current_device = gdk_core_pointer;
318 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
319 gtk_widget_set_name (window, "Test Input");
321 vbox = gtk_vbox_new (FALSE, 0);
322 gtk_container_add (GTK_CONTAINER (window), vbox);
323 gtk_widget_show (vbox);
325 gtk_signal_connect (GTK_OBJECT (window), "destroy",
326 GTK_SIGNAL_FUNC (quit), NULL);
328 /* Create the drawing area */
330 drawing_area = gtk_drawing_area_new ();
331 gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
332 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
334 gtk_widget_show (drawing_area);
336 /* Signals used to handle backing pixmap */
338 gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
339 (GtkSignalFunc) expose_event, NULL);
340 gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
341 (GtkSignalFunc) configure_event, NULL);
345 gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
346 (GtkSignalFunc) motion_notify_event, NULL);
347 gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
348 (GtkSignalFunc) button_press_event, NULL);
349 gtk_signal_connect (GTK_OBJECT (drawing_area), "key_press_event",
350 (GtkSignalFunc) key_press_event, NULL);
352 gtk_signal_connect (GTK_OBJECT (drawing_area), "leave_notify_event",
353 (GtkSignalFunc) leave_notify_event, NULL);
354 gtk_signal_connect (GTK_OBJECT (drawing_area), "proximity_out_event",
355 (GtkSignalFunc) proximity_out_event, NULL);
357 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
358 | GDK_LEAVE_NOTIFY_MASK
359 | GDK_BUTTON_PRESS_MASK
361 | GDK_POINTER_MOTION_MASK
362 | GDK_POINTER_MOTION_HINT_MASK
363 | GDK_PROXIMITY_OUT_MASK);
365 /* The following call enables tracking and processing of extension
366 events for the drawing area */
367 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
369 GTK_WIDGET_SET_FLAGS (drawing_area, GTK_CAN_FOCUS);
370 gtk_widget_grab_focus (drawing_area);
372 /* .. And create some buttons */
373 button = gtk_button_new_with_label ("Input Dialog");
374 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
376 gtk_signal_connect (GTK_OBJECT (button), "clicked",
377 GTK_SIGNAL_FUNC (create_input_dialog), NULL);
378 gtk_widget_show (button);
380 button = gtk_button_new_with_label ("Quit");
381 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
383 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
384 GTK_SIGNAL_FUNC (gtk_widget_destroy),
385 GTK_OBJECT (window));
386 gtk_widget_show (button);
388 gtk_widget_show (window);