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