]> Pileus Git - ~andy/gtk/blob - examples/scribble-simple/scribble-simple.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / examples / scribble-simple / scribble-simple.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 <stdlib.h>
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 gboolean configure_event( GtkWidget         *widget,
29                                  GdkEventConfigure *event )
30 {
31   if (pixmap)
32     g_object_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 gboolean expose_event( GtkWidget      *widget,
50                               GdkEventExpose *event )
51 {
52   gdk_draw_drawable (widget->window,
53                      widget->style->fg_gc[gtk_widget_get_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 draw_brush( GtkWidget *widget,
64                         gdouble    x,
65                         gdouble    y)
66 {
67   GdkRectangle update_rect;
68
69   update_rect.x = x - 5;
70   update_rect.y = y - 5;
71   update_rect.width = 10;
72   update_rect.height = 10;
73   gdk_draw_rectangle (pixmap,
74                       widget->style->black_gc,
75                       TRUE,
76                       update_rect.x, update_rect.y,
77                       update_rect.width, update_rect.height);
78   gtk_widget_queue_draw_area (widget, 
79                               update_rect.x, update_rect.y,
80                               update_rect.width, update_rect.height);
81 }
82
83 static gboolean button_press_event( GtkWidget      *widget,
84                                     GdkEventButton *event )
85 {
86   if (event->button == 1 && pixmap != NULL)
87     draw_brush (widget, event->x, event->y);
88
89   return TRUE;
90 }
91
92 static gboolean motion_notify_event( GtkWidget *widget,
93                                      GdkEventMotion *event )
94 {
95   int x, y;
96   GdkModifierType state;
97
98   if (event->is_hint)
99     gdk_window_get_pointer (event->window, &x, &y, &state);
100   else
101     {
102       x = event->x;
103       y = event->y;
104       state = event->state;
105     }
106     
107   if (state & GDK_BUTTON1_MASK && pixmap != NULL)
108     draw_brush (widget, x, y);
109   
110   return TRUE;
111 }
112
113 void quit ()
114 {
115   exit (0);
116 }
117
118 int main( int   argc, 
119           char *argv[] )
120 {
121   GtkWidget *window;
122   GtkWidget *drawing_area;
123   GtkWidget *vbox;
124
125   GtkWidget *button;
126
127   gtk_init (&argc, &argv);
128
129   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130   gtk_widget_set_name (window, "Test Input");
131
132   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
133   gtk_container_add (GTK_CONTAINER (window), vbox);
134   gtk_widget_show (vbox);
135
136   g_signal_connect (window, "destroy",
137                     G_CALLBACK (quit), NULL);
138
139   /* Create the drawing area */
140
141   drawing_area = gtk_drawing_area_new ();
142   gtk_widget_set_size_request (GTK_WIDGET (drawing_area), 200, 200);
143   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
144
145   gtk_widget_show (drawing_area);
146
147   /* Signals used to handle backing pixmap */
148
149   g_signal_connect (drawing_area, "expose-event",
150                     G_CALLBACK (expose_event), NULL);
151   g_signal_connect (drawing_area,"configure-event",
152                     G_CALLBACK (configure_event), NULL);
153
154   /* Event signals */
155
156   g_signal_connect (drawing_area, "motion-notify-event",
157                     G_CALLBACK (motion_notify_event), NULL);
158   g_signal_connect (drawing_area, "button-press-event",
159                     G_CALLBACK (button_press_event), NULL);
160
161   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
162                          | GDK_LEAVE_NOTIFY_MASK
163                          | GDK_BUTTON_PRESS_MASK
164                          | GDK_POINTER_MOTION_MASK
165                          | GDK_POINTER_MOTION_HINT_MASK);
166
167   /* .. And a quit button */
168   button = gtk_button_new_with_label ("Quit");
169   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
170
171   g_signal_connect_swapped (button, "clicked",
172                             G_CALLBACK (gtk_widget_destroy),
173                             window);
174   gtk_widget_show (button);
175
176   gtk_widget_show (window);
177
178   gtk_main ();
179
180   return 0;
181 }