]> Pileus Git - ~andy/gtk/blob - examples/scribble-xinput/scribble-xinput.c
update packaging script for current tutorial
[~andy/gtk] / examples / scribble-xinput / scribble-xinput.c
1
2 /* GTK - The GIMP Toolkit
3  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gtk/gtk.h>
22
23 /* Backing pixmap for drawing area */
24 static GdkPixmap *pixmap = NULL;
25
26 /* Create a new backing pixmap of the appropriate size */
27 static gint
28 configure_event (GtkWidget *widget, GdkEventConfigure *event)
29 {
30   if (pixmap)
31      g_object_unref (pixmap);
32
33   pixmap = gdk_pixmap_new (widget->window,
34                            widget->allocation.width,
35                            widget->allocation.height,
36                            -1);
37   gdk_draw_rectangle (pixmap,
38                       widget->style->white_gc,
39                       TRUE,
40                       0, 0,
41                       widget->allocation.width,
42                       widget->allocation.height);
43
44   return TRUE;
45 }
46
47 /* Redraw the screen from the backing pixmap */
48 static gint
49 expose_event (GtkWidget *widget, GdkEventExpose *event)
50 {
51   gdk_draw_drawable (widget->window,
52                      widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
53                      pixmap,
54                      event->area.x, event->area.y,
55                      event->area.x, event->area.y,
56                      event->area.width, event->area.height);
57
58   return FALSE;
59 }
60
61 /* Draw a rectangle on the screen, size depending on pressure,
62    and color on the type of device */
63 static void
64 draw_brush (GtkWidget *widget, GdkInputSource source,
65             gdouble x, gdouble y, gdouble pressure)
66 {
67   GdkGC *gc;
68   GdkRectangle update_rect;
69
70   switch (source)
71     {
72     case GDK_SOURCE_MOUSE:
73       gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
74       break;
75     case GDK_SOURCE_PEN:
76       gc = widget->style->black_gc;
77       break;
78     case GDK_SOURCE_ERASER:
79       gc = widget->style->white_gc;
80       break;
81     default:
82       gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
83     }
84
85   update_rect.x = x - 10 * pressure;
86   update_rect.y = y - 10 * pressure;
87   update_rect.width = 20 * pressure;
88   update_rect.height = 20 * pressure;
89   gdk_draw_rectangle (pixmap, gc, TRUE,
90                       update_rect.x, update_rect.y,
91                       update_rect.width, update_rect.height);
92   gtk_widget_queue_draw_area (widget, 
93                       update_rect.x, update_rect.y,
94                       update_rect.width, update_rect.height);
95 }
96
97 static void
98 print_button_press (GdkDevice *device)
99 {
100   g_print ("Button press on device '%s'\n", device->name);
101 }
102
103 static gint
104 button_press_event (GtkWidget *widget, GdkEventButton *event)
105 {
106   print_button_press (event->device);
107   
108   if (event->button == 1 && pixmap != NULL) {
109     gdouble pressure;
110     gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
111     draw_brush (widget, event->device->source, event->x, event->y, pressure);
112   }
113
114   return TRUE;
115 }
116
117 static gint
118 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
119 {
120   gdouble x, y;
121   gdouble pressure;
122   GdkModifierType state;
123
124   if (event->is_hint) 
125     {
126       gdk_device_get_state (event->device, event->window, NULL, &state);
127       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_X, &x);
128       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_Y, &y);
129       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
130     }
131   else
132     {
133       x = event->x;
134       y = event->y;
135       gdk_event_get_axis ((GdkEvent *)event, GDK_AXIS_PRESSURE, &pressure);
136       state = event->state;
137     }
138     
139   if (state & GDK_BUTTON1_MASK && pixmap != NULL)
140     draw_brush (widget, event->device->source, x, y, pressure);
141   
142   return TRUE;
143 }
144
145 void
146 input_dialog_destroy (GtkWidget *w, gpointer data)
147 {
148   *((GtkWidget **)data) = NULL;
149 }
150
151 void
152 create_input_dialog ()
153 {
154   static GtkWidget *inputd = NULL;
155
156   if (!inputd)
157     {
158       inputd = gtk_input_dialog_new();
159
160       g_signal_connect (G_OBJECT (inputd), "destroy",
161                         G_CALLBACK (input_dialog_destroy), (gpointer) &inputd);
162       g_signal_connect_swapped (G_OBJECT (GTK_INPUT_DIALOG (inputd)->close_button),
163                                 "clicked",
164                                 G_CALLBACK (gtk_widget_hide),
165                                 G_OBJECT (inputd));
166       gtk_widget_hide (GTK_INPUT_DIALOG (inputd)->save_button);
167
168       gtk_widget_show (inputd);
169     }
170   else
171     {
172       if (!GTK_WIDGET_MAPPED (inputd))
173         gtk_widget_show (inputd);
174       else
175         gdk_window_raise (inputd->window);
176     }
177 }
178
179 void
180 quit ()
181 {
182   exit (0);
183 }
184
185 int
186 main (int argc, char *argv[])
187 {
188   GtkWidget *window;
189   GtkWidget *drawing_area;
190   GtkWidget *vbox;
191
192   GtkWidget *button;
193
194   gtk_init (&argc, &argv);
195
196   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
197   gtk_widget_set_name (window, "Test Input");
198
199   vbox = gtk_vbox_new (FALSE, 0);
200   gtk_container_add (GTK_CONTAINER (window), vbox);
201   gtk_widget_show (vbox);
202
203   g_signal_connect (G_OBJECT (window), "destroy",
204                     G_CALLBACK (quit), NULL);
205
206   /* Create the drawing area */
207
208   drawing_area = gtk_drawing_area_new ();
209   gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 200, 200);
210   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
211
212   gtk_widget_show (drawing_area);
213
214   /* Signals used to handle backing pixmap */
215
216   g_signal_connect (G_OBJECT (drawing_area), "expose_event",
217                     G_CALLBACK (expose_event), NULL);
218   g_signal_connect (G_OBJECT(drawing_area),"configure_event",
219                     G_CALLBACK (configure_event), NULL);
220
221   /* Event signals */
222
223   g_signal_connect (G_OBJECT (drawing_area), "motion_notify_event",
224                     G_CALLBACK (motion_notify_event), NULL);
225   g_signal_connect (G_OBJECT (drawing_area), "button_press_event",
226                     G_CALLBACK (button_press_event), NULL);
227
228   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
229                          | GDK_LEAVE_NOTIFY_MASK
230                          | GDK_BUTTON_PRESS_MASK
231                          | GDK_POINTER_MOTION_MASK
232                          | GDK_POINTER_MOTION_HINT_MASK);
233
234   /* The following call enables tracking and processing of extension
235      events for the drawing area */
236   gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_CURSOR);
237
238   /* .. And some buttons */
239   button = gtk_button_new_with_label ("Input Dialog");
240   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
241
242   g_signal_connect (G_OBJECT (button), "clicked",
243                     G_CALLBACK (create_input_dialog), NULL);
244   gtk_widget_show (button);
245
246   button = gtk_button_new_with_label ("Quit");
247   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
248
249   g_signal_connect_swapped (G_OBJECT (button), "clicked",
250                             G_CALLBACK (gtk_widget_destroy),
251                             G_OBJECT (window));
252   gtk_widget_show (button);
253
254   gtk_widget_show (window);
255
256   gtk_main ();
257
258   return 0;
259 }