1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
#include "config.h"
#include <stdio.h>
#include "gtk/gtk.h"
#include <math.h>
/* Backing surface for drawing area */
static cairo_surface_t *surface = NULL;
/* Create a new backing surface of the appropriate size */
static void
size_allocate (GtkWidget *widget,
GtkAllocation *allocation,
int baseline,
GdkRectangle *clip,
gpointer data)
{
if (surface)
{
cairo_surface_destroy (surface);
surface = NULL;
}
if (gtk_widget_get_window (widget))
{
cairo_t *cr;
surface = gdk_surface_create_similar_surface (gtk_widget_get_window (widget),
CAIRO_CONTENT_COLOR,
gtk_widget_get_width (widget),
gtk_widget_get_height (widget));
cr = cairo_create (surface);
cairo_set_source_rgb (cr, 1, 1, 1);
cairo_paint (cr);
cairo_destroy (cr);
}
}
/* Refill the screen from the backing surface */
static void
draw (GtkDrawingArea *drawing_area,
cairo_t *cr,
int width,
int height,
gpointer data)
{
cairo_set_source_surface (cr, surface, 0, 0);
cairo_paint (cr);
}
/* Draw a rectangle on the screen, size depending on pressure,
and color on the type of device */
static void
draw_brush (GtkWidget *widget, GdkInputSource source,
gdouble x, gdouble y, gdouble pressure)
{
GdkRGBA color;
GdkRectangle update_rect;
cairo_t *cr;
color.alpha = 1.0;
switch (source)
{
case GDK_SOURCE_MOUSE:
color.red = color.green = 0.0;
color.blue = 1.0;
break;
case GDK_SOURCE_PEN:
color.red = color.green = color.blue = 0.0;
break;
case GDK_SOURCE_ERASER:
color.red = color.green = color.blue = 1.0;
break;
default:
color.red = color.blue = 0.0;
color.green = 1.0;
}
update_rect.x = x - 10 * pressure;
update_rect.y = y - 10 * pressure;
update_rect.width = 20 * pressure;
update_rect.height = 20 * pressure;
cr = cairo_create (surface);
gdk_cairo_set_source_rgba (cr, &color);
gdk_cairo_rectangle (cr, &update_rect);
cairo_fill (cr);
cairo_destroy (cr);
gtk_widget_queue_draw_area (widget,
update_rect.x, update_rect.y,
update_rect.width, update_rect.height);
}
static guint32 motion_time;
static void
print_axes (GdkDevice *device, gdouble *axes)
{
int i;
if (axes)
{
g_print ("%s ", gdk_device_get_name (device));
for (i = 0; i < gdk_device_get_n_axes (device); i++)
g_print ("%g ", axes[i]);
g_print ("\n");
}
}
static void
drag_begin (GtkGesture *gesture,
double x,
double y,
GtkWidget *widget)
{
if (surface != NULL)
{
gdouble pressure = 0.5;
GdkDevice *device;
gdouble *axes;
guint n_axes;
gdouble x, y;
GdkEvent *event;
event = gtk_get_current_event ();
device = gdk_event_get_device (event);
gdk_event_get_axes (event, &axes, &n_axes);
gdk_event_get_coords (event, &x, &y);
print_axes (device, axes);
gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &pressure);
draw_brush (widget, gdk_device_get_source (device), x, y, pressure);
motion_time = gdk_event_get_time ((GdkEvent *)event);
}
}
static gint
key_press_event (GtkWidget *widget, GdkEventKey *event)
{
guint keyval;
gdk_event_get_keyval ((GdkEvent*)event, &keyval);
if ((keyval >= 0x20) && (keyval <= 0xFF))
printf("I got a %c\n", keyval);
else
printf("I got some other key\n");
return TRUE;
}
static void
drag_update (GtkGesture *gesture,
double x,
double y,
GtkWidget *widget)
{
GdkModifierType state;
GdkDevice *device;
gdouble *axes;
GdkEvent *event;
guint n_axes;
double start_x, start_y;
event = gtk_get_current_event ();
gdk_event_get_state (event, &state);
device = gdk_event_get_device (event);
gtk_gesture_drag_get_start_point (GTK_GESTURE_DRAG (gesture), &start_x, &start_y);
if (state & GDK_BUTTON1_MASK && surface != NULL)
{
double pressure = 0.5;
gdk_event_get_axis (event, GDK_AXIS_PRESSURE, &pressure);
draw_brush (widget, gdk_device_get_source (device), start_x + x, start_y + y, pressure);
motion_time = gdk_event_get_time (event);
}
gdk_event_get_axes (event, &axes, &n_axes);
print_axes (device, axes);
}
void
quit (void)
{
gtk_main_quit ();
}
int
main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *drawing_area;
GtkWidget *vbox;
GtkWidget *button;
GtkGesture *gesture;
gtk_init ();
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_set_name (window, "Test Input");
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_widget_show (vbox);
g_signal_connect (window, "destroy",
G_CALLBACK (quit), NULL);
/* Create the drawing area */
drawing_area = gtk_drawing_area_new ();
gtk_widget_set_size_request (drawing_area, 200, 200);
gtk_widget_set_vexpand (drawing_area, TRUE);
gtk_box_pack_start (GTK_BOX (vbox), drawing_area);
gtk_widget_show (drawing_area);
/* Signals used to handle backing surface */
gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (drawing_area), draw, NULL, NULL);
g_signal_connect (drawing_area, "size-allocate", G_CALLBACK (size_allocate), NULL);
gesture = gtk_gesture_drag_new (drawing_area);
g_object_set_data_full (G_OBJECT (drawing_area), "gesture",
gesture, g_object_unref);
g_signal_connect (gesture, "drag-begin",
G_CALLBACK (drag_begin), drawing_area);
g_signal_connect (gesture, "drag-update",
G_CALLBACK (drag_update), drawing_area);
g_signal_connect (drawing_area, "key_press_event",
G_CALLBACK (key_press_event), NULL);
gtk_widget_set_can_focus (drawing_area, TRUE);
gtk_widget_grab_focus (drawing_area);
/* .. And create some buttons */
button = gtk_button_new_with_label ("Quit");
gtk_box_pack_start (GTK_BOX (vbox), button);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy),
window);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
|