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