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