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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 /* Backing pixmap for drawing area */
23 static GdkPixmap *pixmap = NULL;
25 /* Information about cursor */
27 static gint need_cursor = FALSE;
28 static gint cursor_proximity = TRUE;
29 static gdouble cursor_x;
30 static gdouble cursor_y;
32 /* Unique ID of current device */
33 static guint32 current_device = GDK_CORE_POINTER;
35 /* Check to see if we need to draw a cursor for current device */
41 /* gdk_input_list_devices returns an internal list, so we shouldn't
43 tmp_list = gdk_input_list_devices();
47 GdkDeviceInfo *info = (GdkDeviceInfo *)tmp_list->data;
49 if (info->deviceid == current_device)
51 need_cursor = !info->has_cursor;
55 tmp_list = tmp_list->next;
59 /* Erase the old cursor, and/or draw a new one, if necessary */
61 update_cursor (GtkWidget *widget, gdouble x, gdouble y)
63 static gint cursor_present = 0;
64 gint state = need_cursor && cursor_proximity;
68 if (cursor_present && (cursor_present != state ||
69 x != cursor_x || y != cursor_y))
71 gdk_draw_pixmap(widget->window,
72 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
74 cursor_x - 5, cursor_y - 5,
75 cursor_x - 5, cursor_y - 5,
79 cursor_present = state;
85 gdk_draw_rectangle (widget->window,
86 widget->style->black_gc,
88 cursor_x - 5, cursor_y -5,
94 /* Create a new backing pixmap of the appropriate size */
96 configure_event (GtkWidget *widget, GdkEventConfigure *event)
100 gdk_pixmap_destroy(pixmap);
102 pixmap = gdk_pixmap_new(widget->window,
103 widget->allocation.width,
104 widget->allocation.height,
106 gdk_draw_rectangle (pixmap,
107 widget->style->white_gc,
110 widget->allocation.width,
111 widget->allocation.height);
116 /* Refill the screen from the backing pixmap */
118 expose_event (GtkWidget *widget, GdkEventExpose *event)
120 gdk_draw_pixmap(widget->window,
121 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
123 event->area.x, event->area.y,
124 event->area.x, event->area.y,
125 event->area.width, event->area.height);
130 /* Draw a rectangle on the screen, size depending on pressure,
131 and color on the type of device */
133 draw_brush (GtkWidget *widget, GdkInputSource source,
134 gdouble x, gdouble y, gdouble pressure)
137 GdkRectangle update_rect;
141 case GDK_SOURCE_MOUSE:
142 gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
145 gc = widget->style->black_gc;
147 case GDK_SOURCE_ERASER:
148 gc = widget->style->white_gc;
151 gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
154 update_rect.x = x - 10 * pressure;
155 update_rect.y = y - 10 * pressure;
156 update_rect.width = 20 * pressure;
157 update_rect.height = 20 * pressure;
158 gdk_draw_rectangle (pixmap, gc, TRUE,
159 update_rect.x, update_rect.y,
160 update_rect.width, update_rect.height);
161 gtk_widget_draw (widget, &update_rect);
164 static guint32 motion_time;
167 button_press_event (GtkWidget *widget, GdkEventButton *event)
169 if (event->deviceid != current_device)
171 current_device = event->deviceid;
175 cursor_proximity = TRUE;
177 if (event->button == 1 && pixmap != NULL)
179 draw_brush (widget, event->source, event->x, event->y,
181 motion_time = event->time;
184 update_cursor (widget, event->x, event->y);
190 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
192 GdkTimeCoord *coords;
196 if (event->deviceid != current_device)
198 current_device = event->deviceid;
202 cursor_proximity = TRUE;
204 if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
206 coords = gdk_input_motion_events (event->window, event->deviceid,
207 motion_time, event->time,
209 motion_time = event->time;
212 for (i=0; i<nevents; i++)
213 draw_brush (widget, event->source, coords[i].x, coords[i].y,
220 gdk_input_window_get_pointer (event->window, event->deviceid,
221 NULL, NULL, NULL, NULL, NULL, NULL);
222 draw_brush (widget, event->source, event->x, event->y,
228 gdk_input_window_get_pointer (event->window, event->deviceid,
229 &event->x, &event->y,
230 NULL, NULL, NULL, NULL);
233 update_cursor (widget, event->x, event->y);
238 /* We track the next two events to know when we need to draw a
242 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
244 cursor_proximity = FALSE;
245 update_cursor (widget, cursor_x, cursor_y);
250 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
252 cursor_proximity = FALSE;
253 update_cursor (widget, cursor_x, cursor_y);
258 input_dialog_destroy (GtkWidget *w, gpointer data)
260 *((GtkWidget **)data) = NULL;
264 create_input_dialog ()
266 static GtkWidget *inputd = NULL;
270 inputd = gtk_input_dialog_new();
272 gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
273 (GtkSignalFunc)input_dialog_destroy, &inputd);
274 gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
276 (GtkSignalFunc)gtk_widget_hide,
278 gtk_widget_hide ( GTK_INPUT_DIALOG(inputd)->save_button);
280 gtk_signal_connect (GTK_OBJECT(inputd), "enable_device",
281 (GtkSignalFunc)check_cursor, NULL);
282 gtk_widget_show (inputd);
286 if (!GTK_WIDGET_MAPPED(inputd))
287 gtk_widget_show(inputd);
289 gdk_window_raise(inputd->window);
300 main (int argc, char *argv[])
303 GtkWidget *drawing_area;
308 gtk_init (&argc, &argv);
310 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
311 gtk_widget_set_name (window, "Test Input");
313 vbox = gtk_vbox_new (FALSE, 0);
314 gtk_container_add (GTK_CONTAINER (window), vbox);
315 gtk_widget_show (vbox);
317 gtk_signal_connect (GTK_OBJECT (window), "destroy",
318 GTK_SIGNAL_FUNC (quit), NULL);
320 /* Create the drawing area */
322 drawing_area = gtk_drawing_area_new ();
323 gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
324 gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
326 gtk_widget_show (drawing_area);
328 /* Signals used to handle backing pixmap */
330 gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
331 (GtkSignalFunc) expose_event, NULL);
332 gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
333 (GtkSignalFunc) configure_event, NULL);
337 gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
338 (GtkSignalFunc) motion_notify_event, NULL);
339 gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
340 (GtkSignalFunc) button_press_event, NULL);
342 gtk_signal_connect (GTK_OBJECT (drawing_area), "leave_notify_event",
343 (GtkSignalFunc) leave_notify_event, NULL);
344 gtk_signal_connect (GTK_OBJECT (drawing_area), "proximity_out_event",
345 (GtkSignalFunc) proximity_out_event, NULL);
347 gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
348 | GDK_LEAVE_NOTIFY_MASK
349 | GDK_BUTTON_PRESS_MASK
350 | GDK_POINTER_MOTION_MASK
351 | GDK_POINTER_MOTION_HINT_MASK
352 | GDK_PROXIMITY_OUT_MASK);
354 /* The following call enables tracking and processing of extension
355 events for the drawing area */
356 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
358 /* .. And create some buttons */
359 button = gtk_button_new_with_label ("Input Dialog");
360 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
362 gtk_signal_connect (GTK_OBJECT (button), "clicked",
363 GTK_SIGNAL_FUNC (create_input_dialog), NULL);
364 gtk_widget_show (button);
366 button = gtk_button_new_with_label ("Quit");
367 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
369 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
370 GTK_SIGNAL_FUNC (gtk_widget_destroy),
371 GTK_OBJECT (window));
372 gtk_widget_show (button);
374 gtk_widget_show (window);