]> Pileus Git - ~andy/gtk/blob - tests/testinput.c
Deprecation cleanup
[~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 <stdio.h>
28 #include "gtk/gtk.h"
29
30 /* Backing pixmap for drawing area */
31
32 static GdkPixmap *pixmap = 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 = !current_device->has_cursor && cursor_proximity;
49
50   if (pixmap != NULL)
51     {
52       if (cursor_present && (cursor_present != state ||
53                              x != cursor_x || y != cursor_y))
54         {
55           gdk_draw_drawable (widget->window,
56                              widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
57                              pixmap,
58                              cursor_x - 5, cursor_y - 5,
59                              cursor_x - 5, cursor_y - 5,
60                              10, 10);
61         }
62
63       cursor_present = state;
64       cursor_x = x;
65       cursor_y = y;
66
67       if (cursor_present)
68         {
69           gdk_draw_rectangle (widget->window,
70                               widget->style->black_gc,
71                               TRUE,
72                               cursor_x - 5, cursor_y -5,
73                               10, 10);
74         }
75     }
76 }
77
78 /* Create a new backing pixmap of the appropriate size */
79 static gint
80 configure_event (GtkWidget *widget, GdkEventConfigure *event)
81 {
82   if (pixmap)
83     g_object_unref (pixmap);
84   pixmap = gdk_pixmap_new(widget->window,
85                           widget->allocation.width,
86                           widget->allocation.height,
87                           -1);
88   gdk_draw_rectangle (pixmap,
89                       widget->style->white_gc,
90                       TRUE,
91                       0, 0,
92                       widget->allocation.width,
93                       widget->allocation.height);
94
95   return TRUE;
96 }
97
98 /* Refill the screen from the backing pixmap */
99 static gint
100 expose_event (GtkWidget *widget, GdkEventExpose *event)
101 {
102   gdk_draw_drawable (widget->window,
103                      widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
104                      pixmap,
105                      event->area.x, event->area.y,
106                      event->area.x, event->area.y,
107                      event->area.width, event->area.height);
108
109   return FALSE;
110 }
111
112 /* Draw a rectangle on the screen, size depending on pressure,
113    and color on the type of device */
114 static void
115 draw_brush (GtkWidget *widget, GdkInputSource source,
116             gdouble x, gdouble y, gdouble pressure)
117 {
118   GdkGC *gc;
119   GdkRectangle update_rect;
120
121   switch (source)
122     {
123     case GDK_SOURCE_MOUSE:
124       gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
125       break;
126     case GDK_SOURCE_PEN:
127       gc = widget->style->black_gc;
128       break;
129     case GDK_SOURCE_ERASER:
130       gc = widget->style->white_gc;
131       break;
132     default:
133       gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
134     }
135
136   update_rect.x = x - 10 * pressure;
137   update_rect.y = y - 10 * pressure;
138   update_rect.width = 20 * pressure;
139   update_rect.height = 20 * pressure;
140   gdk_draw_rectangle (pixmap, gc, TRUE,
141                       update_rect.x, update_rect.y,
142                       update_rect.width, update_rect.height);
143   gtk_widget_queue_draw_area (widget,
144                               update_rect.x, update_rect.y,
145                               update_rect.width, update_rect.height);
146   gdk_window_process_updates (widget->window, TRUE);
147 }
148
149 static guint32 motion_time;
150
151 static void
152 print_axes (GdkDevice *device, gdouble *axes)
153 {
154   int i;
155   
156   if (axes)
157     {
158       g_print ("%s ", device->name);
159       
160       for (i=0; i<device->num_axes; i++)
161         g_print ("%g ", axes[i]);
162
163       g_print ("\n");
164     }
165 }
166
167 static gint
168 button_press_event (GtkWidget *widget, GdkEventButton *event)
169 {
170   current_device = event->device;
171   cursor_proximity = TRUE;
172
173   if (event->button == 1 && pixmap != NULL)
174     {
175       gdouble pressure = 0.5;
176
177       print_axes (event->device, event->axes);
178       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
179       draw_brush (widget, event->device->source, event->x, event->y, pressure);
180       
181       motion_time = event->time;
182     }
183
184   update_cursor (widget, event->x, event->y);
185
186   return TRUE;
187 }
188
189 static gint
190 key_press_event (GtkWidget *widget, GdkEventKey *event)
191 {
192   if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
193     printf("I got a %c\n", event->keyval);
194   else
195     printf("I got some other key\n");
196
197   return TRUE;
198 }
199
200 static gint
201 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
202 {
203   GdkTimeCoord **events;
204   int n_events;
205   int i;
206
207   current_device = event->device;
208   cursor_proximity = TRUE;
209
210   if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
211     {
212       if (gdk_device_get_history (event->device, event->window, 
213                                   motion_time, event->time,
214                                   &events, &n_events))
215         {
216           for (i=0; i<n_events; i++)
217             {
218               double x = 0, y = 0, pressure = 0.5;
219
220               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_X, &x);
221               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_Y, &y);
222               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_PRESSURE, &pressure);
223               draw_brush (widget,  event->device->source, x, y, pressure);
224
225               print_axes (event->device, events[i]->axes);
226             }
227           gdk_device_free_history (events, n_events);
228         }
229       else
230         {
231           double pressure = 0.5;
232
233           gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
234
235           draw_brush (widget,  event->device->source, event->x, event->y, pressure);
236         }
237       motion_time = event->time;
238     }
239
240   if (event->is_hint)
241     gdk_device_get_state (event->device, event->window, NULL, NULL);
242
243   print_axes (event->device, event->axes);
244   update_cursor (widget, event->x, event->y);
245
246   return TRUE;
247 }
248
249 /* We track the next two events to know when we need to draw a
250    cursor */
251
252 static gint
253 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
254 {
255   cursor_proximity = FALSE;
256   update_cursor (widget, cursor_x, cursor_y);
257   return TRUE;
258 }
259
260 static gint
261 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
262 {
263   cursor_proximity = FALSE;
264   update_cursor (widget, cursor_x, cursor_y);
265   return TRUE;
266 }
267
268 void
269 input_dialog_destroy (GtkWidget *w, gpointer data)
270 {
271   *((GtkWidget **)data) = NULL;
272 }
273
274 void
275 create_input_dialog (void)
276 {
277   static GtkWidget *inputd = NULL;
278
279   if (!inputd)
280     {
281       inputd = gtk_input_dialog_new ();
282
283       g_signal_connect (inputd, "destroy",
284                         G_CALLBACK (input_dialog_destroy), &inputd);
285       g_signal_connect_swapped (GTK_INPUT_DIALOG (inputd)->close_button,
286                                 "clicked",
287                                 G_CALLBACK (gtk_widget_hide),
288                                 inputd);
289       gtk_widget_hide (GTK_INPUT_DIALOG (inputd)->save_button);
290
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 (void)
304 {
305   gtk_main_quit ();
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   current_device = gdk_device_get_core_pointer ();
320
321   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
322   gtk_widget_set_name (window, "Test Input");
323
324   vbox = gtk_vbox_new (FALSE, 0);
325   gtk_container_add (GTK_CONTAINER (window), vbox);
326   gtk_widget_show (vbox);
327
328   g_signal_connect (window, "destroy",
329                     G_CALLBACK (quit), NULL);
330
331   /* Create the drawing area */
332
333   drawing_area = gtk_drawing_area_new ();
334   gtk_widget_set_size_request (drawing_area, 200, 200);
335   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
336
337   gtk_widget_show (drawing_area);
338
339   /* Signals used to handle backing pixmap */
340
341   g_signal_connect (drawing_area, "expose_event",
342                     G_CALLBACK (expose_event), NULL);
343   g_signal_connect (drawing_area, "configure_event",
344                     G_CALLBACK (configure_event), NULL);
345
346   /* Event signals */
347
348   g_signal_connect (drawing_area, "motion_notify_event",
349                     G_CALLBACK (motion_notify_event), NULL);
350   g_signal_connect (drawing_area, "button_press_event",
351                     G_CALLBACK (button_press_event), NULL);
352   g_signal_connect (drawing_area, "key_press_event",
353                     G_CALLBACK (key_press_event), NULL);
354
355   g_signal_connect (drawing_area, "leave_notify_event",
356                     G_CALLBACK (leave_notify_event), NULL);
357   g_signal_connect (drawing_area, "proximity_out_event",
358                     G_CALLBACK (proximity_out_event), NULL);
359
360   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
361                          | GDK_LEAVE_NOTIFY_MASK
362                          | GDK_BUTTON_PRESS_MASK
363                          | GDK_KEY_PRESS_MASK
364                          | GDK_POINTER_MOTION_MASK
365                          | GDK_POINTER_MOTION_HINT_MASK
366                          | GDK_PROXIMITY_OUT_MASK);
367
368   /* The following call enables tracking and processing of extension
369      events for the drawing area */
370   gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
371
372   GTK_WIDGET_SET_FLAGS (drawing_area, GTK_CAN_FOCUS);
373   gtk_widget_grab_focus (drawing_area);
374
375   /* .. And create some buttons */
376   button = gtk_button_new_with_label ("Input Dialog");
377   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
378
379   g_signal_connect (button, "clicked",
380                     G_CALLBACK (create_input_dialog), NULL);
381   gtk_widget_show (button);
382
383   button = gtk_button_new_with_label ("Quit");
384   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
385
386   g_signal_connect_swapped (button, "clicked",
387                             G_CALLBACK (gtk_widget_destroy),
388                             window);
389   gtk_widget_show (button);
390
391   gtk_widget_show (window);
392
393   gtk_main ();
394
395   return 0;
396 }