1 /* example-start scribble-simple scribble-simple.c */
3 /* GTK - The GIMP Toolkit
4 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
24 /* Backing pixmap for drawing area */
25 static GdkPixmap *pixmap = NULL;
27 /* Create a new backing pixmap of the appropriate size */
29 configure_event (GtkWidget *widget, GdkEventConfigure *event)
32 gdk_pixmap_unref(pixmap);
34 pixmap = gdk_pixmap_new(widget->window,
35 widget->allocation.width,
36 widget->allocation.height,
38 gdk_draw_rectangle (pixmap,
39 widget->style->white_gc,
42 widget->allocation.width,
43 widget->allocation.height);
48 /* Redraw the screen from the backing pixmap */
50 expose_event (GtkWidget *widget, GdkEventExpose *event)
52 gdk_draw_pixmap(widget->window,
53 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
55 event->area.x, event->area.y,
56 event->area.x, event->area.y,
57 event->area.width, event->area.height);
62 /* Draw a rectangle on the screen */
64 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
66 GdkRectangle update_rect;
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,
75 update_rect.x, update_rect.y,
76 update_rect.width, update_rect.height);
77 gtk_widget_draw (widget, &update_rect);
81 button_press_event (GtkWidget *widget, GdkEventButton *event)
83 if (event->button == 1 && pixmap != NULL)
84 draw_brush (widget, event->x, event->y);
90 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
93 GdkModifierType state;
96 gdk_window_get_pointer (event->window, &x, &y, &state);
101 state = event->state;
104 if (state & GDK_BUTTON1_MASK && pixmap != NULL)
105 draw_brush (widget, x, y);
117 main (int argc, char *argv[])
120 GtkWidget *drawing_area;
125 gtk_init (&argc, &argv);
127 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
128 gtk_widget_set_name (window, "Test Input");
130 vbox = gtk_vbox_new (FALSE, 0);
131 gtk_container_add (GTK_CONTAINER (window), vbox);
132 gtk_widget_show (vbox);
134 gtk_signal_connect (GTK_OBJECT (window), "destroy",
135 GTK_SIGNAL_FUNC (quit), NULL);
137 /* Create the drawing area */
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);
143 gtk_widget_show (drawing_area);
145 /* Signals used to handle backing pixmap */
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);
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);
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);
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);
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);
174 gtk_widget_show (window);