]> Pileus Git - ~andy/gtk/blob - examples/scribble-simple/scribble-simple.c
- re-write the GtkProgressBar section to the 1.1 API. - add an Appendix
[~andy/gtk] / examples / scribble-simple / scribble-simple.c
1 /* example-start scribble-simple scribble-simple.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 */
63 static void
64 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
65 {
66   GdkRectangle update_rect;
67
68   update_rect.x = x - 5;
69   update_rect.y = y - 5;
70   update_rect.width = 10;
71   update_rect.height = 10;
72   gdk_draw_rectangle (pixmap,
73                       widget->style->black_gc,
74                       TRUE,
75                       update_rect.x, update_rect.y,
76                       update_rect.width, update_rect.height);
77   gtk_widget_draw (widget, &update_rect);
78 }
79
80 static gint
81 button_press_event (GtkWidget *widget, GdkEventButton *event)
82 {
83   if (event->button == 1 && pixmap != NULL)
84     draw_brush (widget, event->x, event->y);
85
86   return TRUE;
87 }
88
89 static gint
90 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
91 {
92   int x, y;
93   GdkModifierType state;
94
95   if (event->is_hint)
96     gdk_window_get_pointer (event->window, &x, &y, &state);
97   else
98     {
99       x = event->x;
100       y = event->y;
101       state = event->state;
102     }
103     
104   if (state & GDK_BUTTON1_MASK && pixmap != NULL)
105     draw_brush (widget, x, y);
106   
107   return TRUE;
108 }
109
110 void
111 quit ()
112 {
113   gtk_exit (0);
114 }
115
116 int
117 main (int argc, char *argv[])
118 {
119   GtkWidget *window;
120   GtkWidget *drawing_area;
121   GtkWidget *vbox;
122
123   GtkWidget *button;
124
125   gtk_init (&argc, &argv);
126
127   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
128   gtk_widget_set_name (window, "Test Input");
129
130   vbox = gtk_vbox_new (FALSE, 0);
131   gtk_container_add (GTK_CONTAINER (window), vbox);
132   gtk_widget_show (vbox);
133
134   gtk_signal_connect (GTK_OBJECT (window), "destroy",
135                       GTK_SIGNAL_FUNC (quit), NULL);
136
137   /* Create the drawing area */
138
139   drawing_area = gtk_drawing_area_new ();
140   gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
141   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
142
143   gtk_widget_show (drawing_area);
144
145   /* Signals used to handle backing pixmap */
146
147   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
148                       (GtkSignalFunc) expose_event, NULL);
149   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
150                       (GtkSignalFunc) configure_event, NULL);
151
152   /* Event signals */
153
154   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
155                       (GtkSignalFunc) motion_notify_event, NULL);
156   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
157                       (GtkSignalFunc) button_press_event, NULL);
158
159   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
160                          | GDK_LEAVE_NOTIFY_MASK
161                          | GDK_BUTTON_PRESS_MASK
162                          | GDK_POINTER_MOTION_MASK
163                          | GDK_POINTER_MOTION_HINT_MASK);
164
165   /* .. And a quit button */
166   button = gtk_button_new_with_label ("Quit");
167   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
168
169   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
170                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
171                              GTK_OBJECT (window));
172   gtk_widget_show (button);
173
174   gtk_widget_show (window);
175
176   gtk_main ();
177
178   return 0;
179 }
180 /* example-end */