]> Pileus Git - ~andy/gtk/blob - tests/testinput.c
It's all in the changelog. Well, almost all.
[~andy/gtk] / tests / testinput.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "gtk.h"
20
21 /* Backing pixmap for drawing area */
22
23 static GdkPixmap *pixmap = NULL;
24
25 /* Information about cursor */
26
27 static gint need_cursor = FALSE;
28 static gint cursor_proximity = TRUE;
29 static gdouble cursor_x;
30 static gdouble cursor_y;
31
32 /* Unique ID of current device */
33 static guint32 current_device = GDK_CORE_POINTER;
34
35 /* Check to see if we need to draw a cursor for current device */
36 static void
37 check_cursor ()
38 {
39   GList *tmp_list;
40
41   /* gdk_input_list_devices returns an internal list, so we shouldn't
42      free it afterwards */
43   tmp_list = gdk_input_list_devices();
44
45   while (tmp_list)
46     {
47       GdkDeviceInfo *info = (GdkDeviceInfo *)tmp_list->data;
48
49       if (info->deviceid == current_device)
50         {
51           need_cursor = !info->has_cursor;
52           break;
53         }
54
55       tmp_list = tmp_list->next;
56     }
57 }
58
59 /* Erase the old cursor, and/or draw a new one, if necessary */
60 static void
61 update_cursor (GtkWidget *widget,  gdouble x, gdouble y)
62 {
63   static gint cursor_present = 0;
64   gint state = need_cursor && cursor_proximity;
65
66   if (pixmap != NULL)
67     {
68       if (cursor_present && (cursor_present != state ||
69                              x != cursor_x || y != cursor_y))
70         {
71           gdk_draw_pixmap(widget->window,
72                           widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
73                           pixmap,
74                           cursor_x - 5, cursor_y - 5,
75                           cursor_x - 5, cursor_y - 5,
76                           10, 10);
77         }
78
79       cursor_present = state;
80       cursor_x = x;
81       cursor_y = y;
82
83       if (cursor_present)
84         {
85           gdk_draw_rectangle (widget->window,
86                               widget->style->black_gc,
87                               TRUE,
88                               cursor_x - 5, cursor_y -5,
89                               10, 10);
90         }
91     }
92 }
93
94 /* Create a new backing pixmap of the appropriate size */
95 static gint
96 configure_event (GtkWidget *widget, GdkEventConfigure *event)
97 {
98   if (pixmap)
99     gdk_pixmap_unref (pixmap);
100   pixmap = gdk_pixmap_new(widget->window,
101                           widget->allocation.width,
102                           widget->allocation.height,
103                           -1);
104   gdk_draw_rectangle (pixmap,
105                       widget->style->white_gc,
106                       TRUE,
107                       0, 0,
108                       widget->allocation.width,
109                       widget->allocation.height);
110
111   return TRUE;
112 }
113
114 /* Refill the screen from the backing pixmap */
115 static gint
116 expose_event (GtkWidget *widget, GdkEventExpose *event)
117 {
118   gdk_draw_pixmap(widget->window,
119                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
120                   pixmap,
121                   event->area.x, event->area.y,
122                   event->area.x, event->area.y,
123                   event->area.width, event->area.height);
124
125   return FALSE;
126 }
127
128 /* Draw a rectangle on the screen, size depending on pressure,
129    and color on the type of device */
130 static void
131 draw_brush (GtkWidget *widget, GdkInputSource source,
132             gdouble x, gdouble y, gdouble pressure)
133 {
134   GdkGC *gc;
135   GdkRectangle update_rect;
136
137   switch (source)
138     {
139     case GDK_SOURCE_MOUSE:
140       gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
141       break;
142     case GDK_SOURCE_PEN:
143       gc = widget->style->black_gc;
144       break;
145     case GDK_SOURCE_ERASER:
146       gc = widget->style->white_gc;
147       break;
148     default:
149       gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
150     }
151
152   update_rect.x = x - 10 * pressure;
153   update_rect.y = y - 10 * pressure;
154   update_rect.width = 20 * pressure;
155   update_rect.height = 20 * pressure;
156   gdk_draw_rectangle (pixmap, gc, TRUE,
157                       update_rect.x, update_rect.y,
158                       update_rect.width, update_rect.height);
159   gtk_widget_draw (widget, &update_rect);
160 }
161
162 static guint32 motion_time;
163
164 static gint
165 button_press_event (GtkWidget *widget, GdkEventButton *event)
166 {
167   if (event->deviceid != current_device)
168     {
169       current_device = event->deviceid;
170       check_cursor ();
171     }
172
173   cursor_proximity = TRUE;
174
175   if (event->button == 1 && pixmap != NULL)
176     {
177       draw_brush (widget, event->source, event->x, event->y,
178                   event->pressure);
179       motion_time = event->time;
180     }
181
182   update_cursor (widget, event->x, event->y);
183
184   return TRUE;
185 }
186
187 static gint
188 key_press_event (GtkWidget *widget, GdkEventKey *event)
189 {
190   if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
191     printf("I got a %c\n", event->keyval);
192   else
193     printf("I got some other key\n");
194
195   return TRUE;
196 }
197
198 static gint
199 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
200 {
201   GdkTimeCoord *coords;
202   int nevents;
203   int i;
204
205   if (event->deviceid != current_device)
206     {
207       current_device = event->deviceid;
208       check_cursor ();
209     }
210
211   cursor_proximity = TRUE;
212
213   if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
214     {
215       coords = gdk_input_motion_events (event->window, event->deviceid,
216                                         motion_time, event->time,
217                                         &nevents);
218       motion_time = event->time;
219       if (coords)
220         {
221           for (i=0; i<nevents; i++)
222             draw_brush (widget,  event->source, coords[i].x, coords[i].y,
223                         coords[i].pressure);
224           g_free (coords);
225         }
226       else
227         {
228           if (event->is_hint)
229             gdk_input_window_get_pointer (event->window, event->deviceid,
230                                           NULL, NULL, NULL, NULL, NULL, NULL);
231           draw_brush (widget,  event->source, event->x, event->y,
232                       event->pressure);
233         }
234     }
235   else
236     {
237       gdk_input_window_get_pointer (event->window, event->deviceid,
238                                     &event->x, &event->y,
239                                     NULL, NULL, NULL, NULL);
240     }
241
242   update_cursor (widget, event->x, event->y);
243
244   return TRUE;
245 }
246
247 /* We track the next two events to know when we need to draw a
248    cursor */
249
250 static gint
251 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
252 {
253   cursor_proximity = FALSE;
254   update_cursor (widget, cursor_x, cursor_y);
255   return TRUE;
256 }
257
258 static gint
259 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
260 {
261   cursor_proximity = FALSE;
262   update_cursor (widget, cursor_x, cursor_y);
263   return TRUE;
264 }
265
266 void
267 input_dialog_destroy (GtkWidget *w, gpointer data)
268 {
269   *((GtkWidget **)data) = NULL;
270 }
271
272 void
273 create_input_dialog ()
274 {
275   static GtkWidget *inputd = NULL;
276
277   if (!inputd)
278     {
279       inputd = gtk_input_dialog_new();
280
281       gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
282                           (GtkSignalFunc)input_dialog_destroy, &inputd);
283       gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
284                           "clicked",
285                           (GtkSignalFunc)gtk_widget_hide,
286                           GTK_OBJECT(inputd));
287       gtk_widget_hide ( GTK_INPUT_DIALOG(inputd)->save_button);
288
289       gtk_signal_connect (GTK_OBJECT(inputd), "enable_device",
290                           (GtkSignalFunc)check_cursor, NULL);
291       gtk_widget_show (inputd);
292     }
293   else
294     {
295       if (!GTK_WIDGET_MAPPED(inputd))
296         gtk_widget_show(inputd);
297       else
298         gdk_window_raise(inputd->window);
299     }
300 }
301
302 void
303 quit ()
304 {
305   gtk_exit (0);
306 }
307
308 int
309 main (int argc, char *argv[])
310 {
311   GtkWidget *window;
312   GtkWidget *drawing_area;
313   GtkWidget *vbox;
314
315   GtkWidget *button;
316
317   gtk_init (&argc, &argv);
318
319   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
320   gtk_widget_set_name (window, "Test Input");
321
322   vbox = gtk_vbox_new (FALSE, 0);
323   gtk_container_add (GTK_CONTAINER (window), vbox);
324   gtk_widget_show (vbox);
325
326   gtk_signal_connect (GTK_OBJECT (window), "destroy",
327                       GTK_SIGNAL_FUNC (quit), NULL);
328
329   /* Create the drawing area */
330
331   drawing_area = gtk_drawing_area_new ();
332   gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
333   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
334
335   gtk_widget_show (drawing_area);
336
337   /* Signals used to handle backing pixmap */
338
339   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
340                       (GtkSignalFunc) expose_event, NULL);
341   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
342                       (GtkSignalFunc) configure_event, NULL);
343
344   /* Event signals */
345
346   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
347                       (GtkSignalFunc) motion_notify_event, NULL);
348   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
349                       (GtkSignalFunc) button_press_event, NULL);
350   gtk_signal_connect (GTK_OBJECT (drawing_area), "key_press_event",
351                       (GtkSignalFunc) key_press_event, NULL);
352
353   gtk_signal_connect (GTK_OBJECT (drawing_area), "leave_notify_event",
354                       (GtkSignalFunc) leave_notify_event, NULL);
355   gtk_signal_connect (GTK_OBJECT (drawing_area), "proximity_out_event",
356                       (GtkSignalFunc) proximity_out_event, NULL);
357
358   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
359                          | GDK_LEAVE_NOTIFY_MASK
360                          | GDK_BUTTON_PRESS_MASK
361                          | GDK_KEY_PRESS_MASK
362                          | GDK_POINTER_MOTION_MASK
363                          | GDK_POINTER_MOTION_HINT_MASK
364                          | GDK_PROXIMITY_OUT_MASK);
365
366   /* The following call enables tracking and processing of extension
367      events for the drawing area */
368   gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
369
370   gtk_widget_grab_focus (drawing_area);
371
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);
375
376   gtk_signal_connect (GTK_OBJECT (button), "clicked",
377                       GTK_SIGNAL_FUNC (create_input_dialog), NULL);
378   gtk_widget_show (button);
379
380   button = gtk_button_new_with_label ("Quit");
381   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
382
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);
387
388   gtk_widget_show (window);
389
390   gtk_main ();
391
392   return 0;
393 }