]> Pileus Git - ~andy/gtk/blob - tests/testinput.c
Make gdkx.h the only installed header from gdk/x11. All structures in
[~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_pixmap(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     gdk_pixmap_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_pixmap(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_draw (widget, &update_rect);
144 }
145
146 static guint32 motion_time;
147
148 static void
149 print_axes (GdkDevice *device, gdouble *axes)
150 {
151   int i;
152   
153   if (axes)
154     {
155       g_print ("%s ", device->name);
156       
157       for (i=0; i<device->num_axes; i++)
158         g_print ("%g ", axes[i]);
159
160       g_print ("\n");
161     }
162 }
163
164 static gint
165 button_press_event (GtkWidget *widget, GdkEventButton *event)
166 {
167   current_device = event->device;
168   cursor_proximity = TRUE;
169
170   if (event->button == 1 && pixmap != NULL)
171     {
172       gdouble pressure = 0.5;
173
174       print_axes (event->device, event->axes);
175       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
176       draw_brush (widget, event->device->source, event->x, event->y, pressure);
177       
178       motion_time = event->time;
179     }
180
181   update_cursor (widget, event->x, event->y);
182
183   return TRUE;
184 }
185
186 static gint
187 key_press_event (GtkWidget *widget, GdkEventKey *event)
188 {
189   if ((event->keyval >= 0x20) && (event->keyval <= 0xFF))
190     printf("I got a %c\n", event->keyval);
191   else
192     printf("I got some other key\n");
193
194   return TRUE;
195 }
196
197 static gint
198 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
199 {
200   GdkTimeCoord **events;
201   int n_events;
202   int i;
203
204   current_device = event->device;
205   cursor_proximity = TRUE;
206
207   if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
208     {
209       if (gdk_device_get_history (event->device, event->window, 
210                                   motion_time, event->time,
211                                   &events, &n_events))
212         {
213           for (i=0; i<n_events; i++)
214             {
215               double x = 0, y = 0, pressure = 0.5;
216
217               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_X, &x);
218               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_Y, &y);
219               gdk_device_get_axis (event->device, events[i]->axes, GDK_AXIS_PRESSURE, &pressure);
220               draw_brush (widget,  event->device->source, x, y, pressure);
221
222               print_axes (event->device, events[i]->axes);
223             }
224           gdk_device_free_history (events, n_events);
225         }
226       else
227         {
228           double pressure = 0.5;
229
230           gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
231
232           draw_brush (widget,  event->device->source, event->x, event->y, pressure);
233         }
234       motion_time = event->time;
235     }
236
237   if (event->is_hint)
238     gdk_device_get_state (event->device, event->window, NULL, NULL);
239
240   print_axes (event->device, event->axes);
241   update_cursor (widget, event->x, event->y);
242
243   return TRUE;
244 }
245
246 /* We track the next two events to know when we need to draw a
247    cursor */
248
249 static gint
250 proximity_out_event (GtkWidget *widget, GdkEventProximity *event)
251 {
252   cursor_proximity = FALSE;
253   update_cursor (widget, cursor_x, cursor_y);
254   return TRUE;
255 }
256
257 static gint
258 leave_notify_event (GtkWidget *widget, GdkEventCrossing *event)
259 {
260   cursor_proximity = FALSE;
261   update_cursor (widget, cursor_x, cursor_y);
262   return TRUE;
263 }
264
265 void
266 input_dialog_destroy (GtkWidget *w, gpointer data)
267 {
268   *((GtkWidget **)data) = NULL;
269 }
270
271 void
272 create_input_dialog (void)
273 {
274   static GtkWidget *inputd = NULL;
275
276   if (!inputd)
277     {
278       inputd = gtk_input_dialog_new();
279
280       gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
281                           (GtkSignalFunc)input_dialog_destroy, &inputd);
282       gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
283                           "clicked",
284                           (GtkSignalFunc)gtk_widget_hide,
285                           GTK_OBJECT(inputd));
286       gtk_widget_hide (GTK_INPUT_DIALOG(inputd)->save_button);
287
288       gtk_widget_show (inputd);
289     }
290   else
291     {
292       if (!GTK_WIDGET_MAPPED(inputd))
293         gtk_widget_show(inputd);
294       else
295         gdk_window_raise(inputd->window);
296     }
297 }
298
299 void
300 quit (void)
301 {
302   gtk_exit (0);
303 }
304
305 int
306 main (int argc, char *argv[])
307 {
308   GtkWidget *window;
309   GtkWidget *drawing_area;
310   GtkWidget *vbox;
311
312   GtkWidget *button;
313
314   gtk_init (&argc, &argv);
315
316   current_device = gdk_device_get_core_pointer ();
317
318   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
319   gtk_widget_set_name (window, "Test Input");
320
321   vbox = gtk_vbox_new (FALSE, 0);
322   gtk_container_add (GTK_CONTAINER (window), vbox);
323   gtk_widget_show (vbox);
324
325   gtk_signal_connect (GTK_OBJECT (window), "destroy",
326                       GTK_SIGNAL_FUNC (quit), NULL);
327
328   /* Create the drawing area */
329
330   drawing_area = gtk_drawing_area_new ();
331   gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
332   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
333
334   gtk_widget_show (drawing_area);
335
336   /* Signals used to handle backing pixmap */
337
338   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
339                       (GtkSignalFunc) expose_event, NULL);
340   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
341                       (GtkSignalFunc) configure_event, NULL);
342
343   /* Event signals */
344
345   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
346                       (GtkSignalFunc) motion_notify_event, NULL);
347   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
348                       (GtkSignalFunc) button_press_event, NULL);
349   gtk_signal_connect (GTK_OBJECT (drawing_area), "key_press_event",
350                       (GtkSignalFunc) key_press_event, NULL);
351
352   gtk_signal_connect (GTK_OBJECT (drawing_area), "leave_notify_event",
353                       (GtkSignalFunc) leave_notify_event, NULL);
354   gtk_signal_connect (GTK_OBJECT (drawing_area), "proximity_out_event",
355                       (GtkSignalFunc) proximity_out_event, NULL);
356
357   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
358                          | GDK_LEAVE_NOTIFY_MASK
359                          | GDK_BUTTON_PRESS_MASK
360                          | GDK_KEY_PRESS_MASK
361                          | GDK_POINTER_MOTION_MASK
362                          | GDK_POINTER_MOTION_HINT_MASK
363                          | GDK_PROXIMITY_OUT_MASK);
364
365   /* The following call enables tracking and processing of extension
366      events for the drawing area */
367   gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_ALL);
368
369   GTK_WIDGET_SET_FLAGS (drawing_area, GTK_CAN_FOCUS);
370   gtk_widget_grab_focus (drawing_area);
371
372   /* .. And create some buttons */
373   button = gtk_button_new_with_label ("Input Dialog");
374   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
375
376   gtk_signal_connect (GTK_OBJECT (button), "clicked",
377                       GTK_SIGNAL_FUNC (create_input_dialog), NULL);
378   gtk_widget_show (button);
379
380   button = gtk_button_new_with_label ("Quit");
381   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
382
383   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
384                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
385                              GTK_OBJECT (window));
386   gtk_widget_show (button);
387
388   gtk_widget_show (window);
389
390   gtk_main ();
391
392   return 0;
393 }