]> Pileus Git - ~andy/gtk/blob - tests/testinput.c
Use GDK symbolic names for button numbers
[~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 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.
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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26 #include <stdio.h>
27 #include "gtk/gtk.h"
28
29 /* Backing surface for drawing area */
30
31 static cairo_surface_t *surface = NULL;
32
33 /* Information about cursor */
34
35 static gint cursor_proximity = TRUE;
36 static gdouble cursor_x;
37 static gdouble cursor_y;
38
39 /* Unique ID of current device */
40 static GdkDevice *current_device;
41
42 /* Erase the old cursor, and/or draw a new one, if necessary */
43 static void
44 update_cursor (GtkWidget *widget,  gdouble x, gdouble y)
45 {
46   static gint cursor_present = 0;
47   gint state = !gdk_device_get_has_cursor (current_device) && cursor_proximity;
48
49   if (surface != NULL)
50     {
51       cairo_t *cr = gdk_cairo_create (gtk_widget_get_window (widget));
52
53       if (cursor_present && (cursor_present != state ||
54                              x != cursor_x || y != cursor_y))
55         {
56           cairo_set_source_surface (cr, surface, 0, 0);
57           cairo_rectangle (cr, cursor_x - 5, cursor_y - 5, 10, 10);
58           cairo_fill (cr);
59         }
60
61       cursor_present = state;
62       cursor_x = x;
63       cursor_y = y;
64
65       if (cursor_present)
66         {
67           cairo_set_source_rgb (cr, 0, 0, 0);
68           cairo_rectangle (cr, 
69                            cursor_x - 5, cursor_y -5,
70                            10, 10);
71           cairo_fill (cr);
72         }
73
74       cairo_destroy (cr);
75     }
76 }
77
78 /* Create a new backing surface of the appropriate size */
79 static gint
80 configure_event (GtkWidget *widget, GdkEventConfigure *event)
81 {
82   GtkAllocation allocation;
83   cairo_t *cr;
84
85   if (surface)
86     cairo_surface_destroy (surface);
87
88   gtk_widget_get_allocation (widget, &allocation);
89
90   surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
91                                                CAIRO_CONTENT_COLOR,
92                                                allocation.width,
93                                                allocation.height);
94   cr = cairo_create (surface);
95
96   cairo_set_source_rgb (cr, 1, 1, 1);
97   cairo_paint (cr);
98
99   cairo_destroy (cr);
100
101   return TRUE;
102 }
103
104 /* Refill the screen from the backing surface */
105 static gboolean
106 draw (GtkWidget *widget, cairo_t *cr)
107 {
108   cairo_set_source_surface (cr, surface, 0, 0);
109   cairo_paint (cr);
110
111   return FALSE;
112 }
113
114 /* Draw a rectangle on the screen, size depending on pressure,
115    and color on the type of device */
116 static void
117 draw_brush (GtkWidget *widget, GdkInputSource source,
118             gdouble x, gdouble y, gdouble pressure)
119 {
120   GdkRGBA color;
121   GdkRectangle update_rect;
122   cairo_t *cr;
123
124   color.alpha = 1.0;
125
126   switch (source)
127     {
128     case GDK_SOURCE_MOUSE:
129       color.red = color.green = 0.0;
130       color.blue = 1.0;
131       break;
132     case GDK_SOURCE_PEN:
133       color.red = color.green = color.blue = 0.0;
134       break;
135     case GDK_SOURCE_ERASER:
136       color.red = color.green = color.blue = 1.0;
137       break;
138     default:
139       color.red = color.blue = 0.0;
140       color.green = 1.0;
141     }
142
143   update_rect.x = x - 10 * pressure;
144   update_rect.y = y - 10 * pressure;
145   update_rect.width = 20 * pressure;
146   update_rect.height = 20 * pressure;
147
148   cr = cairo_create (surface);
149   gdk_cairo_set_source_rgba (cr, &color);
150   gdk_cairo_rectangle (cr, &update_rect);
151   cairo_fill (cr);
152   cairo_destroy (cr);
153
154   gtk_widget_queue_draw_area (widget,
155                               update_rect.x, update_rect.y,
156                               update_rect.width, update_rect.height);
157   gdk_window_process_updates (gtk_widget_get_window (widget), TRUE);
158 }
159
160 static guint32 motion_time;
161
162 static void
163 print_axes (GdkDevice *device, gdouble *axes)
164 {
165   int i;
166   
167   if (axes)
168     {
169       g_print ("%s ", gdk_device_get_name (device));
170
171       for (i = 0; i < gdk_device_get_n_axes (device); i++)
172         g_print ("%g ", axes[i]);
173
174       g_print ("\n");
175     }
176 }
177
178 static gint
179 button_press_event (GtkWidget *widget, GdkEventButton *event)
180 {
181   current_device = event->device;
182   cursor_proximity = TRUE;
183
184   if (event->button == GDK_BUTTON_PRIMARY &&
185       surface != NULL)
186     {
187       gdouble pressure = 0.5;
188
189       print_axes (event->device, event->axes);
190       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
191       draw_brush (widget, gdk_device_get_source (event->device),
192                   event->x, event->y, pressure);
193
194       motion_time = event->time;
195     }
196
197   update_cursor (widget, event->x, event->y);
198
199   return TRUE;
200 }
201
202 static gint
203 key_press_event (GtkWidget *widget, GdkEventKey *event)
204 {
205   if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
206     printf("I got a %c\n", event->keyval);
207   else
208     printf("I got some other key\n");
209
210   return TRUE;
211 }
212
213 static gint
214 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
215 {
216   GdkTimeCoord **events;
217   gint n_events;
218   int i;
219
220   current_device = event->device;
221   cursor_proximity = TRUE;
222
223   if (event->state & GDK_BUTTON1_MASK && surface != NULL)
224     {
225       if (gdk_device_get_history (event->device, event->window, 
226                                   motion_time, event->time,
227                                   &events, &n_events))
228         {
229           for (i=0; i<n_events; i++)
230             {
231               double x = 0, y = 0, pressure = 0.5;
232
233               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_X, &x);
234               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_Y, &y);
235               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_PRESSURE, &pressure);
236               draw_brush (widget, gdk_device_get_source (event->device),
237                           x, y, pressure);
238
239               print_axes (event->device, events[i]->axes);
240             }
241           gdk_device_free_history (events, n_events);
242         }
243       else
244         {
245           double pressure = 0.5;
246
247           gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
248
249           draw_brush (widget, gdk_device_get_source (event->device),
250                       event->x, event->y, pressure);
251         }
252       motion_time = event->time;
253     }
254
255   if (event->is_hint)
256     gdk_device_get_state (event->device, event->window, NULL, NULL);
257
258   print_axes (event->device, event->axes);
259   update_cursor (widget, event->x, event->y);
260
261   return TRUE;
262 }
263
264 /* We track the next two events to know when we need to draw a
265    cursor */
266
267 static gint
268 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
269 {
270   cursor_proximity = FALSE;
271   update_cursor (widget, cursor_x, cursor_y);
272   return TRUE;
273 }
274
275 static gint
276 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
277 {
278   cursor_proximity = FALSE;
279   update_cursor (widget, cursor_x, cursor_y);
280   return TRUE;
281 }
282
283 void
284 quit (void)
285 {
286   gtk_main_quit ();
287 }
288
289 int
290 main (int argc, char *argv[])
291 {
292   GdkDeviceManager *device_manager;
293   GList *devices, *d;
294   GdkEventMask event_mask;
295   GtkWidget *window;
296   GtkWidget *drawing_area;
297   GtkWidget *vbox;
298
299   GtkWidget *button;
300
301   gtk_init (&argc, &argv);
302
303   device_manager = gdk_display_get_device_manager (gdk_display_get_default ());
304   current_device = gdk_device_manager_get_client_pointer (device_manager);
305
306   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
307   gtk_widget_set_name (window, "Test Input");
308
309   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
310   gtk_container_add (GTK_CONTAINER (window), vbox);
311   gtk_widget_show (vbox);
312
313   g_signal_connect (window, "destroy",
314                     G_CALLBACK (quit), NULL);
315
316   /* Create the drawing area */
317
318   drawing_area = gtk_drawing_area_new ();
319   gtk_widget_set_size_request (drawing_area, 200, 200);
320   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
321
322   gtk_widget_show (drawing_area);
323
324   /* Signals used to handle backing surface */
325
326   g_signal_connect (drawing_area, "draw",
327                     G_CALLBACK (draw), NULL);
328   g_signal_connect (drawing_area, "configure_event",
329                     G_CALLBACK (configure_event), NULL);
330
331   /* Event signals */
332
333   g_signal_connect (drawing_area, "motion_notify_event",
334                     G_CALLBACK (motion_notify_event), NULL);
335   g_signal_connect (drawing_area, "button_press_event",
336                     G_CALLBACK (button_press_event), NULL);
337   g_signal_connect (drawing_area, "key_press_event",
338                     G_CALLBACK (key_press_event), NULL);
339
340   g_signal_connect (drawing_area, "leave_notify_event",
341                     G_CALLBACK (leave_notify_event), NULL);
342   g_signal_connect (drawing_area, "proximity_out_event",
343                     G_CALLBACK (proximity_out_event), NULL);
344
345   event_mask = GDK_EXPOSURE_MASK |
346     GDK_LEAVE_NOTIFY_MASK |
347     GDK_BUTTON_PRESS_MASK |
348     GDK_KEY_PRESS_MASK |
349     GDK_POINTER_MOTION_MASK |
350     GDK_POINTER_MOTION_HINT_MASK |
351     GDK_PROXIMITY_OUT_MASK;
352
353   gtk_widget_set_events (drawing_area, event_mask);
354
355   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_FLOATING);
356
357   for (d = devices; d; d = d->next)
358     {
359       GdkDevice *device;
360
361       device = d->data;
362       gtk_widget_set_device_events (drawing_area, device, event_mask);
363       gdk_device_set_mode (device, GDK_MODE_SCREEN);
364     }
365
366   g_list_free (devices);
367
368   gtk_widget_set_can_focus (drawing_area, TRUE);
369   gtk_widget_grab_focus (drawing_area);
370
371   /* .. And create some buttons */
372   button = gtk_button_new_with_label ("Quit");
373   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
374
375   g_signal_connect_swapped (button, "clicked",
376                             G_CALLBACK (gtk_widget_destroy),
377                             window);
378   gtk_widget_show (button);
379
380   gtk_widget_show (window);
381
382   gtk_main ();
383
384   return 0;
385 }