]> Pileus Git - ~andy/gtk/blob - tests/testinput.c
Warning cleanups.. cvsignore stuff
[~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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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 #include <stdio.h>
21 #include "gtk.h"
22
23 /* Backing pixmap for drawing area */
24
25 static GdkPixmap *pixmap = NULL;
26
27 /* Information about cursor */
28
29 static gint need_cursor = FALSE;
30 static gint cursor_proximity = TRUE;
31 static gdouble cursor_x;
32 static gdouble cursor_y;
33
34 /* Unique ID of current device */
35 static guint32 current_device = GDK_CORE_POINTER;
36
37 /* Check to see if we need to draw a cursor for current device */
38 static void
39 check_cursor (void)
40 {
41   GList *tmp_list;
42
43   /* gdk_input_list_devices returns an internal list, so we shouldn't
44      free it afterwards */
45   tmp_list = gdk_input_list_devices();
46
47   while (tmp_list)
48     {
49       GdkDeviceInfo *info = (GdkDeviceInfo *)tmp_list->data;
50
51       if (info->deviceid == current_device)
52         {
53           need_cursor = !info->has_cursor;
54           break;
55         }
56
57       tmp_list = tmp_list->next;
58     }
59 }
60
61 /* Erase the old cursor, and/or draw a new one, if necessary */
62 static void
63 update_cursor (GtkWidget *widget,  gdouble x, gdouble y)
64 {
65   static gint cursor_present = 0;
66   gint state = need_cursor && cursor_proximity;
67
68   if (pixmap != NULL)
69     {
70       if (cursor_present && (cursor_present != state ||
71                              x != cursor_x || y != cursor_y))
72         {
73           gdk_draw_pixmap(widget->window,
74                           widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
75                           pixmap,
76                           cursor_x - 5, cursor_y - 5,
77                           cursor_x - 5, cursor_y - 5,
78                           10, 10);
79         }
80
81       cursor_present = state;
82       cursor_x = x;
83       cursor_y = y;
84
85       if (cursor_present)
86         {
87           gdk_draw_rectangle (widget->window,
88                               widget->style->black_gc,
89                               TRUE,
90                               cursor_x - 5, cursor_y -5,
91                               10, 10);
92         }
93     }
94 }
95
96 /* Create a new backing pixmap of the appropriate size */
97 static gint
98 configure_event (GtkWidget *widget, GdkEventConfigure *event)
99 {
100   if (pixmap)
101     gdk_pixmap_unref (pixmap);
102   pixmap = gdk_pixmap_new(widget->window,
103                           widget->allocation.width,
104                           widget->allocation.height,
105                           -1);
106   gdk_draw_rectangle (pixmap,
107                       widget->style->white_gc,
108                       TRUE,
109                       0, 0,
110                       widget->allocation.width,
111                       widget->allocation.height);
112
113   return TRUE;
114 }
115
116 /* Refill the screen from the backing pixmap */
117 static gint
118 expose_event (GtkWidget *widget, GdkEventExpose *event)
119 {
120   gdk_draw_pixmap(widget->window,
121                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
122                   pixmap,
123                   event->area.x, event->area.y,
124                   event->area.x, event->area.y,
125                   event->area.width, event->area.height);
126
127   return FALSE;
128 }
129
130 /* Draw a rectangle on the screen, size depending on pressure,
131    and color on the type of device */
132 static void
133 draw_brush (GtkWidget *widget, GdkInputSource source,
134             gdouble x, gdouble y, gdouble pressure)
135 {
136   GdkGC *gc;
137   GdkRectangle update_rect;
138
139   switch (source)
140     {
141     case GDK_SOURCE_MOUSE:
142       gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
143       break;
144     case GDK_SOURCE_PEN:
145       gc = widget->style->black_gc;
146       break;
147     case GDK_SOURCE_ERASER:
148       gc = widget->style->white_gc;
149       break;
150     default:
151       gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
152     }
153
154   update_rect.x = x - 10 * pressure;
155   update_rect.y = y - 10 * pressure;
156   update_rect.width = 20 * pressure;
157   update_rect.height = 20 * pressure;
158   gdk_draw_rectangle (pixmap, gc, TRUE,
159                       update_rect.x, update_rect.y,
160                       update_rect.width, update_rect.height);
161   gtk_widget_draw (widget, &update_rect);
162 }
163
164 static guint32 motion_time;
165
166 static gint
167 button_press_event (GtkWidget *widget, GdkEventButton *event)
168 {
169   if (event->deviceid != current_device)
170     {
171       current_device = event->deviceid;
172       check_cursor ();
173     }
174
175   cursor_proximity = TRUE;
176
177   if (event->button == 1 && pixmap != NULL)
178     {
179       draw_brush (widget, event->source, event->x, event->y,
180                   event->pressure);
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 *coords;
204   int nevents;
205   int i;
206
207   if (event->deviceid != current_device)
208     {
209       current_device = event->deviceid;
210       check_cursor ();
211     }
212
213   cursor_proximity = TRUE;
214
215   if (event->state & GDK_BUTTON1_MASK && pixmap != NULL)
216     {
217       coords = gdk_input_motion_events (event->window, event->deviceid,
218                                         motion_time, event->time,
219                                         &nevents);
220       motion_time = event->time;
221       if (coords)
222         {
223           for (i=0; i<nevents; i++)
224             draw_brush (widget,  event->source, coords[i].x, coords[i].y,
225                         coords[i].pressure);
226           g_free (coords);
227         }
228       else
229         {
230           if (event->is_hint)
231             gdk_input_window_get_pointer (event->window, event->deviceid,
232                                           NULL, NULL, NULL, NULL, NULL, NULL);
233           draw_brush (widget,  event->source, event->x, event->y,
234                       event->pressure);
235         }
236     }
237   else
238     {
239       gdk_input_window_get_pointer (event->window, event->deviceid,
240                                     &event->x, &event->y,
241                                     NULL, NULL, NULL, NULL);
242     }
243
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       gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
284                           (GtkSignalFunc)input_dialog_destroy, &inputd);
285       gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
286                           "clicked",
287                           (GtkSignalFunc)gtk_widget_hide,
288                           GTK_OBJECT(inputd));
289       gtk_widget_hide ( GTK_INPUT_DIALOG(inputd)->save_button);
290
291       gtk_signal_connect (GTK_OBJECT(inputd), "enable_device",
292                           (GtkSignalFunc)check_cursor, NULL);
293       gtk_widget_show (inputd);
294     }
295   else
296     {
297       if (!GTK_WIDGET_MAPPED(inputd))
298         gtk_widget_show(inputd);
299       else
300         gdk_window_raise(inputd->window);
301     }
302 }
303
304 void
305 quit (void)
306 {
307   gtk_exit (0);
308 }
309
310 int
311 main (int argc, char *argv[])
312 {
313   GtkWidget *window;
314   GtkWidget *drawing_area;
315   GtkWidget *vbox;
316
317   GtkWidget *button;
318
319   gtk_init (&argc, &argv);
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   gtk_signal_connect (GTK_OBJECT (window), "destroy",
329                       GTK_SIGNAL_FUNC (quit), NULL);
330
331   /* Create the drawing area */
332
333   drawing_area = gtk_drawing_area_new ();
334   gtk_drawing_area_size (GTK_DRAWING_AREA (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   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
342                       (GtkSignalFunc) expose_event, NULL);
343   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
344                       (GtkSignalFunc) configure_event, NULL);
345
346   /* Event signals */
347
348   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
349                       (GtkSignalFunc) motion_notify_event, NULL);
350   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
351                       (GtkSignalFunc) button_press_event, NULL);
352   gtk_signal_connect (GTK_OBJECT (drawing_area), "key_press_event",
353                       (GtkSignalFunc) key_press_event, NULL);
354
355   gtk_signal_connect (GTK_OBJECT (drawing_area), "leave_notify_event",
356                       (GtkSignalFunc) leave_notify_event, NULL);
357   gtk_signal_connect (GTK_OBJECT (drawing_area), "proximity_out_event",
358                       (GtkSignalFunc) 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_grab_focus (drawing_area);
373
374   /* .. And create some buttons */
375   button = gtk_button_new_with_label ("Input Dialog");
376   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
377
378   gtk_signal_connect (GTK_OBJECT (button), "clicked",
379                       GTK_SIGNAL_FUNC (create_input_dialog), NULL);
380   gtk_widget_show (button);
381
382   button = gtk_button_new_with_label ("Quit");
383   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
384
385   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
386                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
387                              GTK_OBJECT (window));
388   gtk_widget_show (button);
389
390   gtk_widget_show (window);
391
392   gtk_main ();
393
394   return 0;
395 }