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