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