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