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