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/.
31 /* Backing pixmap for drawing area */
33 static GdkPixmap *pixmap = NULL;
35 /* Information about cursor */
37 static gint cursor_proximity = TRUE;
38 static gdouble cursor_x;
39 static gdouble cursor_y;
41 /* Unique ID of current device */
42 static GdkDevice *current_device;
44 /* Erase the old cursor, and/or draw a new one, if necessary */
46 update_cursor (GtkWidget *widget, gdouble x, gdouble y)
48 static gint cursor_present = 0;
49 gint state = !current_device->has_cursor && cursor_proximity;
53 if (cursor_present && (cursor_present != state ||
54 x != cursor_x || y != cursor_y))
56 gdk_draw_drawable (widget->window,
57 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
59 cursor_x - 5, cursor_y - 5,
60 cursor_x - 5, cursor_y - 5,
64 cursor_present = state;
70 gdk_draw_rectangle (widget->window,
71 widget->style->black_gc,
73 cursor_x - 5, cursor_y -5,
79 /* Create a new backing pixmap of the appropriate size */
81 configure_event (GtkWidget *widget, GdkEventConfigure *event)
84 g_object_unref (pixmap);
85 pixmap = gdk_pixmap_new(widget->window,
86 widget->allocation.width,
87 widget->allocation.height,
89 gdk_draw_rectangle (pixmap,
90 widget->style->white_gc,
93 widget->allocation.width,
94 widget->allocation.height);
99 /* Refill the screen from the backing pixmap */
101 expose_event (GtkWidget *widget, GdkEventExpose *event)
103 gdk_draw_drawable (widget->window,
104 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
106 event->area.x, event->area.y,
107 event->area.x, event->area.y,
108 event->area.width, event->area.height);
113 /* Draw a rectangle on the screen, size depending on pressure,
114 and color on the type of device */
116 draw_brush (GtkWidget *widget, GdkInputSource source,
117 gdouble x, gdouble y, gdouble pressure)
120 GdkRectangle update_rect;
124 case GDK_SOURCE_MOUSE:
125 gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
128 gc = widget->style->black_gc;
130 case GDK_SOURCE_ERASER:
131 gc = widget->style->white_gc;
134 gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
137 update_rect.x = x - 10 * pressure;
138 update_rect.y = y - 10 * pressure;
139 update_rect.width = 20 * pressure;
140 update_rect.height = 20 * pressure;
141 gdk_draw_rectangle (pixmap, gc, TRUE,
142 update_rect.x, update_rect.y,
143 update_rect.width, update_rect.height);
144 gtk_widget_queue_draw_area (widget,
145 update_rect.x, update_rect.y,
146 update_rect.width, update_rect.height);
147 gdk_window_process_updates (widget->window, TRUE);
150 static guint32 motion_time;
153 print_axes (GdkDevice *device, gdouble *axes)
159 g_print ("%s ", device->name);
161 for (i=0; i<device->num_axes; i++)
162 g_print ("%g ", axes[i]);
169 button_press_event (GtkWidget *widget, GdkEventButton *event)
171 current_device = event->device;
172 cursor_proximity = TRUE;
174 if (event->button == 1 && pixmap != NULL)
176 gdouble pressure = 0.5;
178 print_axes (event->device, event->axes);
179 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
180 draw_brush (widget, event->device->source, event->x, event->y, pressure);
182 motion_time = event->time;
185 update_cursor (widget, event->x, event->y);
191 key_press_event (GtkWidget *widget, GdkEventKey *event)
193 if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
194 printf("I got a %c\n", event->keyval);
196 printf("I got some other key\n");
202 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
204 GdkTimeCoord **events;
208 current_device = event->device;
209 cursor_proximity = TRUE;
211 if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
213 if (gdk_device_get_history (event->device, event->window,
214 motion_time, event->time,
217 for (i=0; i<n_events; i++)
219 double x = 0, y = 0, pressure = 0.5;
221 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_X, &x);
222 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_Y, &y);
223 gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_PRESSURE, &pressure);
224 draw_brush (widget, event->device->source, x, y, pressure);
226 print_axes (event->device, events[i]->axes);
228 gdk_device_free_history (events, n_events);
232 double pressure = 0.5;
234 gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
236 draw_brush (widget, event->device->source, event->x, event->y, pressure);
238 motion_time = event->time;
242 gdk_device_get_state (event->device, event->window, NULL, NULL);
244 print_axes (event->device, event->axes);
245 update_cursor (widget, event->x, event->y);
250 /* We track the next two events to know when we need to draw a
254 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
256 cursor_proximity = FALSE;
257 update_cursor (widget, cursor_x, cursor_y);
262 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
264 cursor_proximity = FALSE;
265 update_cursor (widget, cursor_x, cursor_y);
270 input_dialog_destroy (GtkWidget *w, gpointer data)
272 *((GtkWidget **)data) = NULL;
276 create_input_dialog (void)
278 static GtkWidget *inputd = NULL;
282 inputd = gtk_input_dialog_new ();
284 g_signal_connect (inputd, "destroy",
285 G_CALLBACK (input_dialog_destroy), &inputd);
286 g_signal_connect_swapped (GTK_INPUT_DIALOG (inputd)->close_button,
288 G_CALLBACK (gtk_widget_hide),
290 gtk_widget_hide (GTK_INPUT_DIALOG (inputd)->save_button);
292 gtk_widget_show (inputd);
296 if (!GTK_WIDGET_MAPPED(inputd))
297 gtk_widget_show(inputd);
299 gdk_window_raise(inputd->window);
310 main (int argc, char *argv[])
313 GtkWidget *drawing_area;
318 gtk_init (&argc, &argv);
320 current_device = gdk_device_get_core_pointer ();
322 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
323 gtk_widget_set_name (window, "Test Input");
325 vbox = gtk_vbox_new (FALSE, 0);
326 gtk_container_add (GTK_CONTAINER (window), vbox);
327 gtk_widget_show (vbox);
329 g_signal_connect (window, "destroy",
330 G_CALLBACK (quit), NULL);
332 /* Create the drawing area */
334 drawing_area = gtk_drawing_area_new ();
335 gtk_widget_set_size_request (drawing_area, 200, 200);
336 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
338 gtk_widget_show (drawing_area);
340 /* Signals used to handle backing pixmap */
342 g_signal_connect (drawing_area, "expose_event",
343 G_CALLBACK (expose_event), NULL);
344 g_signal_connect (drawing_area, "configure_event",
345 G_CALLBACK (configure_event), NULL);
349 g_signal_connect (drawing_area, "motion_notify_event",
350 G_CALLBACK (motion_notify_event), NULL);
351 g_signal_connect (drawing_area, "button_press_event",
352 G_CALLBACK (button_press_event), NULL);
353 g_signal_connect (drawing_area, "key_press_event",
354 G_CALLBACK (key_press_event), NULL);
356 g_signal_connect (drawing_area, "leave_notify_event",
357 G_CALLBACK (leave_notify_event), NULL);
358 g_signal_connect (drawing_area, "proximity_out_event",
359 G_CALLBACK (proximity_out_event), NULL);
361 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
362 | GDK_LEAVE_NOTIFY_MASK
363 | GDK_BUTTON_PRESS_MASK
365 | GDK_POINTER_MOTION_MASK
366 | GDK_POINTER_MOTION_HINT_MASK
367 | GDK_PROXIMITY_OUT_MASK);
369 /* The following call enables tracking and processing of extension
370 events for the drawing area */
371 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
373 GTK_WIDGET_SET_FLAGS (drawing_area, GTK_CAN_FOCUS);
374 gtk_widget_grab_focus (drawing_area);
376 /* .. And create some buttons */
377 button = gtk_button_new_with_label ("Input Dialog");
378 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
380 g_signal_connect (button, "clicked",
381 G_CALLBACK (create_input_dialog), NULL);
382 gtk_widget_show (button);
384 button = gtk_button_new_with_label ("Quit");
385 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
387 g_signal_connect_swapped (button, "clicked",
388 G_CALLBACK (gtk_widget_destroy),
390 gtk_widget_show (button);
392 gtk_widget_show (window);