Add powerbox hook
[gtk-with-powerbox.git] / examples / scribble-simple / scribble-simple.c
blob92e54da91eb83e1dde59fa821fa3d6100483a5d8
2 /* GTK - The GIMP Toolkit
3 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
21 #include <stdlib.h>
22 #include <gtk/gtk.h>
24 /* Backing pixmap for drawing area */
25 static GdkPixmap *pixmap = NULL;
27 /* Create a new backing pixmap of the appropriate size */
28 static gboolean configure_event( GtkWidget *widget,
29 GdkEventConfigure *event )
31 if (pixmap)
32 g_object_unref (pixmap);
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);
45 return TRUE;
48 /* Redraw the screen from the backing pixmap */
49 static gboolean expose_event( GtkWidget *widget,
50 GdkEventExpose *event )
52 gdk_draw_drawable (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);
59 return FALSE;
62 /* Draw a rectangle on the screen */
63 static void draw_brush( GtkWidget *widget,
64 gdouble x,
65 gdouble y)
67 GdkRectangle update_rect;
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);
83 static gboolean button_press_event( GtkWidget *widget,
84 GdkEventButton *event )
86 if (event->button == 1 && pixmap != NULL)
87 draw_brush (widget, event->x, event->y);
89 return TRUE;
92 static gboolean motion_notify_event( GtkWidget *widget,
93 GdkEventMotion *event )
95 int x, y;
96 GdkModifierType state;
98 if (event->is_hint)
99 gdk_window_get_pointer (event->window, &x, &y, &state);
100 else
102 x = event->x;
103 y = event->y;
104 state = event->state;
107 if (state & GDK_BUTTON1_MASK && pixmap != NULL)
108 draw_brush (widget, x, y);
110 return TRUE;
113 void quit ()
115 exit (0);
118 int main( int argc,
119 char *argv[] )
121 GtkWidget *window;
122 GtkWidget *drawing_area;
123 GtkWidget *vbox;
125 GtkWidget *button;
127 gtk_init (&argc, &argv);
129 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130 gtk_widget_set_name (window, "Test Input");
132 vbox = gtk_vbox_new (FALSE, 0);
133 gtk_container_add (GTK_CONTAINER (window), vbox);
134 gtk_widget_show (vbox);
136 g_signal_connect (G_OBJECT (window), "destroy",
137 G_CALLBACK (quit), NULL);
139 /* Create the drawing area */
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);
145 gtk_widget_show (drawing_area);
147 /* Signals used to handle backing pixmap */
149 g_signal_connect (G_OBJECT (drawing_area), "expose_event",
150 G_CALLBACK (expose_event), NULL);
151 g_signal_connect (G_OBJECT (drawing_area),"configure_event",
152 G_CALLBACK (configure_event), NULL);
154 /* Event signals */
156 g_signal_connect (G_OBJECT (drawing_area), "motion_notify_event",
157 G_CALLBACK (motion_notify_event), NULL);
158 g_signal_connect (G_OBJECT (drawing_area), "button_press_event",
159 G_CALLBACK (button_press_event), NULL);
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);
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);
171 g_signal_connect_swapped (G_OBJECT (button), "clicked",
172 G_CALLBACK (gtk_widget_destroy),
173 G_OBJECT (window));
174 gtk_widget_show (button);
176 gtk_widget_show (window);
178 gtk_main ();
180 return 0;