]> Pileus Git - ~andy/gtk/blob - examples/scribble-simple/scribble-simple.c
Changed LGPL address for FSF in all .h and .c files
[~andy/gtk] / examples / scribble-simple / scribble-simple.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 <gtk/gtk.h>
21
22 /* Backing pixmap for drawing area */
23 static GdkPixmap *pixmap = NULL;
24
25 /* Create a new backing pixmap of the appropriate size */
26 static gint
27 configure_event (GtkWidget *widget, GdkEventConfigure *event)
28 {
29   if (pixmap)
30     gdk_pixmap_unref(pixmap);
31
32   pixmap = gdk_pixmap_new(widget->window,
33                           widget->allocation.width,
34                           widget->allocation.height,
35                           -1);
36   gdk_draw_rectangle (pixmap,
37                       widget->style->white_gc,
38                       TRUE,
39                       0, 0,
40                       widget->allocation.width,
41                       widget->allocation.height);
42
43   return TRUE;
44 }
45
46 /* Redraw the screen from the backing pixmap */
47 static gint
48 expose_event (GtkWidget *widget, GdkEventExpose *event)
49 {
50   gdk_draw_pixmap(widget->window,
51                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
52                   pixmap,
53                   event->area.x, event->area.y,
54                   event->area.x, event->area.y,
55                   event->area.width, event->area.height);
56
57   return FALSE;
58 }
59
60 /* Draw a rectangle on the screen */
61 static void
62 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
63 {
64   GdkRectangle update_rect;
65
66   update_rect.x = x - 5;
67   update_rect.y = y - 5;
68   update_rect.width = 10;
69   update_rect.height = 10;
70   gdk_draw_rectangle (pixmap,
71                       widget->style->black_gc,
72                       TRUE,
73                       update_rect.x, update_rect.y,
74                       update_rect.width, update_rect.height);
75   gtk_widget_draw (widget, &update_rect);
76 }
77
78 static gint
79 button_press_event (GtkWidget *widget, GdkEventButton *event)
80 {
81   if (event->button == 1 && pixmap != NULL)
82     draw_brush (widget, event->x, event->y);
83
84   return TRUE;
85 }
86
87 static gint
88 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
89 {
90   int x, y;
91   GdkModifierType state;
92
93   if (event->is_hint)
94     gdk_window_get_pointer (event->window, &x, &y, &state);
95   else
96     {
97       x = event->x;
98       y = event->y;
99       state = event->state;
100     }
101     
102   if (state & GDK_BUTTON1_MASK && pixmap != NULL)
103     draw_brush (widget, x, y);
104   
105   return TRUE;
106 }
107
108 void
109 quit ()
110 {
111   gtk_exit (0);
112 }
113
114 int
115 main (int argc, char *argv[])
116 {
117   GtkWidget *window;
118   GtkWidget *drawing_area;
119   GtkWidget *vbox;
120
121   GtkWidget *button;
122
123   gtk_init (&argc, &argv);
124
125   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
126   gtk_widget_set_name (window, "Test Input");
127
128   vbox = gtk_vbox_new (FALSE, 0);
129   gtk_container_add (GTK_CONTAINER (window), vbox);
130   gtk_widget_show (vbox);
131
132   gtk_signal_connect (GTK_OBJECT (window), "destroy",
133                       GTK_SIGNAL_FUNC (quit), NULL);
134
135   /* Create the drawing area */
136
137   drawing_area = gtk_drawing_area_new ();
138   gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
139   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
140
141   gtk_widget_show (drawing_area);
142
143   /* Signals used to handle backing pixmap */
144
145   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
146                       (GtkSignalFunc) expose_event, NULL);
147   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
148                       (GtkSignalFunc) configure_event, NULL);
149
150   /* Event signals */
151
152   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
153                       (GtkSignalFunc) motion_notify_event, NULL);
154   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
155                       (GtkSignalFunc) button_press_event, NULL);
156
157   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
158                          | GDK_LEAVE_NOTIFY_MASK
159                          | GDK_BUTTON_PRESS_MASK
160                          | GDK_POINTER_MOTION_MASK
161                          | GDK_POINTER_MOTION_HINT_MASK);
162
163   /* .. And a quit button */
164   button = gtk_button_new_with_label ("Quit");
165   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
166
167   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
168                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
169                              GTK_OBJECT (window));
170   gtk_widget_show (button);
171
172   gtk_widget_show (window);
173
174   gtk_main ();
175
176   return 0;
177 }