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_drawable (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 g_object_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_drawable (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_queue_draw_area (widget,
144 update_rect.x, update_rect.y,
145 update_rect.width, update_rect.height);
146 gdk_window_process_updates (widget->window, TRUE);
149 static guint32 motion_time;
152 print_axes (GdkDevice *device, gdouble *axes)
158 g_print ("%s ", device->name);
160 for (i=0; i<device->num_axes; i++)
161 g_print ("%g ", axes[i]);
168 button_press_event (GtkWidget *widget, GdkEventButton *event)
170 current_device = event->device;
171 cursor_proximity = TRUE;
173 if (event->button == 1 && pixmap != NULL)
175 gdouble pressure = 0.5;
177 print_axes (event->device, event->axes);
178 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
179 draw_brush (widget, event->device->source, event->x, event->y, pressure);
181 motion_time = event->time;
184 update_cursor (widget, event->x, event->y);
190 key_press_event (GtkWidget *widget, GdkEventKey *event)
192 if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
193 printf("I got a %c\n", event->keyval);
195 printf("I got some other key\n");
201 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
203 GdkTimeCoord **events;
207 current_device = event->device;
208 cursor_proximity = TRUE;
210 if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
212 if (gdk_device_get_history (event->device, event->window,
213 motion_time, event->time,
216 for (i=0; i<n_events; i++)
218 double x = 0, y = 0, pressure = 0.5;
220 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_X, &x);
221 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_Y, &y);
222 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_PRESSURE, &pressure);
223 draw_brush (widget, event->device->source, x, y, pressure);
225 print_axes (event->device, events[i]->axes);
227 gdk_device_free_history (events, n_events);
231 double pressure = 0.5;
233 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
235 draw_brush (widget, event->device->source, event->x, event->y, pressure);
237 motion_time = event->time;
241 gdk_device_get_state (event->device, event->window, NULL, NULL);
243 print_axes (event->device, event->axes);
244 update_cursor (widget, event->x, event->y);
249 /* We track the next two events to know when we need to draw a
253 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
255 cursor_proximity = FALSE;
256 update_cursor (widget, cursor_x, cursor_y);
261 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
263 cursor_proximity = FALSE;
264 update_cursor (widget, cursor_x, cursor_y);
269 input_dialog_destroy (GtkWidget *w, gpointer data)
271 *((GtkWidget **)data) = NULL;
275 create_input_dialog (void)
277 static GtkWidget *inputd = NULL;
281 inputd = gtk_input_dialog_new ();
283 g_signal_connect (inputd, "destroy",
284 G_CALLBACK (input_dialog_destroy), &inputd);
285 g_signal_connect_swapped (GTK_INPUT_DIALOG (inputd)->close_button,
287 G_CALLBACK (gtk_widget_hide),
289 gtk_widget_hide (GTK_INPUT_DIALOG (inputd)->save_button);
291 gtk_widget_show (inputd);
295 if (!GTK_WIDGET_MAPPED(inputd))
296 gtk_widget_show(inputd);
298 gdk_window_raise(inputd->window);
309 main (int argc, char *argv[])
312 GtkWidget *drawing_area;
317 gtk_init (&argc, &argv);
319 current_device = gdk_device_get_core_pointer ();
321 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
322 gtk_widget_set_name (window, "Test Input");
324 vbox = gtk_vbox_new (FALSE, 0);
325 gtk_container_add (GTK_CONTAINER (window), vbox);
326 gtk_widget_show (vbox);
328 g_signal_connect (window, "destroy",
329 G_CALLBACK (quit), NULL);
331 /* Create the drawing area */
333 drawing_area = gtk_drawing_area_new ();
334 gtk_widget_set_size_request (drawing_area, 200, 200);
335 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
337 gtk_widget_show (drawing_area);
339 /* Signals used to handle backing pixmap */
341 g_signal_connect (drawing_area, "expose_event",
342 G_CALLBACK (expose_event), NULL);
343 g_signal_connect (drawing_area, "configure_event",
344 G_CALLBACK (configure_event), NULL);
348 g_signal_connect (drawing_area, "motion_notify_event",
349 G_CALLBACK (motion_notify_event), NULL);
350 g_signal_connect (drawing_area, "button_press_event",
351 G_CALLBACK (button_press_event), NULL);
352 g_signal_connect (drawing_area, "key_press_event",
353 G_CALLBACK (key_press_event), NULL);
355 g_signal_connect (drawing_area, "leave_notify_event",
356 G_CALLBACK (leave_notify_event), NULL);
357 g_signal_connect (drawing_area, "proximity_out_event",
358 G_CALLBACK (proximity_out_event), NULL);
360 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
361 | GDK_LEAVE_NOTIFY_MASK
362 | GDK_BUTTON_PRESS_MASK
364 | GDK_POINTER_MOTION_MASK
365 | GDK_POINTER_MOTION_HINT_MASK
366 | GDK_PROXIMITY_OUT_MASK);
368 /* The following call enables tracking and processing of extension
369 events for the drawing area */
370 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
372 GTK_WIDGET_SET_FLAGS (drawing_area, GTK_CAN_FOCUS);
373 gtk_widget_grab_focus (drawing_area);
375 /* .. And create some buttons */
376 button = gtk_button_new_with_label ("Input Dialog");
377 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
379 g_signal_connect (button, "clicked",
380 G_CALLBACK (create_input_dialog), NULL);
381 gtk_widget_show (button);
383 button = gtk_button_new_with_label ("Quit");
384 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
386 g_signal_connect_swapped (button, "clicked",
387 G_CALLBACK (gtk_widget_destroy),
389 gtk_widget_show (button);
391 gtk_widget_show (window);