]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
Document expose event->region change and that gtk_widget_event doesn't
[~andy/gtk] / gtk / gtkeventbox.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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtksignal.h"
28 #include "gtkeventbox.h"
29
30
31 static void gtk_event_box_class_init               (GtkEventBoxClass *klass);
32 static void gtk_event_box_init                     (GtkEventBox      *event_box);
33 static void gtk_event_box_realize                  (GtkWidget        *widget);
34 static void gtk_event_box_size_request             (GtkWidget        *widget,
35                                                     GtkRequisition   *requisition);
36 static void gtk_event_box_size_allocate            (GtkWidget        *widget,
37                                                     GtkAllocation    *allocation);
38 static void gtk_event_box_paint                    (GtkWidget         *widget,
39                                                     GdkRectangle      *area);
40 static gint gtk_event_box_expose                   (GtkWidget         *widget,
41                                                    GdkEventExpose     *event);
42
43
44 static GtkBinClass *parent_class = NULL;
45
46 GtkType
47 gtk_event_box_get_type (void)
48 {
49   static GtkType event_box_type = 0;
50
51   if (!event_box_type)
52     {
53       static const GtkTypeInfo event_box_info =
54       {
55         "GtkEventBox",
56         sizeof (GtkEventBox),
57         sizeof (GtkEventBoxClass),
58         (GtkClassInitFunc) gtk_event_box_class_init,
59         (GtkObjectInitFunc) gtk_event_box_init,
60         /* reserved_1 */ NULL,
61         /* reserved_2 */ NULL,
62         (GtkClassInitFunc) NULL,
63       };
64
65       event_box_type = gtk_type_unique (gtk_bin_get_type (), &event_box_info);
66     }
67
68   return event_box_type;
69 }
70
71 static void
72 gtk_event_box_class_init (GtkEventBoxClass *class)
73 {
74   GtkWidgetClass *widget_class;
75
76   parent_class = g_type_class_peek_parent (class);
77   
78   widget_class = (GtkWidgetClass*) class;
79
80   widget_class->realize = gtk_event_box_realize;
81   widget_class->size_request = gtk_event_box_size_request;
82   widget_class->size_allocate = gtk_event_box_size_allocate;
83   widget_class->expose_event = gtk_event_box_expose;
84 }
85
86 static void
87 gtk_event_box_init (GtkEventBox *event_box)
88 {
89   GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
90 }
91
92 GtkWidget*
93 gtk_event_box_new (void)
94 {
95   return GTK_WIDGET ( gtk_type_new (gtk_event_box_get_type ()));
96 }
97
98 static void
99 gtk_event_box_realize (GtkWidget *widget)
100 {
101   GdkWindowAttr attributes;
102   gint attributes_mask;
103   gint border_width;
104
105   g_return_if_fail (widget != NULL);
106   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
107
108   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
109
110   border_width = GTK_CONTAINER (widget)->border_width;
111   
112   attributes.x = widget->allocation.x + border_width;
113   attributes.y = widget->allocation.y + border_width;
114   attributes.width = widget->allocation.width - 2*border_width;
115   attributes.height = widget->allocation.height - 2*border_width;
116   attributes.window_type = GDK_WINDOW_CHILD;
117   attributes.wclass = GDK_INPUT_OUTPUT;
118   attributes.visual = gtk_widget_get_visual (widget);
119   attributes.colormap = gtk_widget_get_colormap (widget);
120   attributes.event_mask = gtk_widget_get_events (widget)
121                         | GDK_BUTTON_MOTION_MASK
122                         | GDK_BUTTON_PRESS_MASK
123                         | GDK_BUTTON_RELEASE_MASK
124                         | GDK_EXPOSURE_MASK
125                         | GDK_ENTER_NOTIFY_MASK
126                         | GDK_LEAVE_NOTIFY_MASK;
127
128   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
129
130   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
131   gdk_window_set_user_data (widget->window, widget);
132
133   widget->style = gtk_style_attach (widget->style, widget->window);
134   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
135 }
136
137 static void
138 gtk_event_box_size_request (GtkWidget      *widget,
139                         GtkRequisition *requisition)
140 {
141   GtkBin *bin;
142
143   g_return_if_fail (widget != NULL);
144   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
145   g_return_if_fail (requisition != NULL);
146
147   bin = GTK_BIN (widget);
148
149   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
150   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
151
152   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
153     {
154       GtkRequisition child_requisition;
155       
156       gtk_widget_size_request (bin->child, &child_requisition);
157
158       requisition->width += child_requisition.width;
159       requisition->height += child_requisition.height;
160     }
161 }
162
163 static void
164 gtk_event_box_size_allocate (GtkWidget     *widget,
165                             GtkAllocation *allocation)
166 {
167   GtkBin *bin;
168   GtkAllocation child_allocation;
169
170   g_return_if_fail (widget != NULL);
171   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
172   g_return_if_fail (allocation != NULL);
173
174   widget->allocation = *allocation;
175   bin = GTK_BIN (widget);
176
177   child_allocation.x = 0;
178   child_allocation.y = 0;
179   child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
180   child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
181
182   if (GTK_WIDGET_REALIZED (widget))
183     {
184       gdk_window_move_resize (widget->window,
185                               allocation->x + GTK_CONTAINER (widget)->border_width,
186                               allocation->y + GTK_CONTAINER (widget)->border_width,
187                               child_allocation.width,
188                               child_allocation.height);
189     }
190   
191   if (bin->child)
192     {
193       gtk_widget_size_allocate (bin->child, &child_allocation);
194     }
195 }
196
197 static void
198 gtk_event_box_paint (GtkWidget    *widget,
199                      GdkRectangle *area)
200 {
201   gtk_paint_flat_box (widget->style, widget->window,
202                       widget->state, GTK_SHADOW_NONE,
203                       area, widget, "eventbox",
204                       0, 0, -1, -1);
205 }
206
207 static gint
208 gtk_event_box_expose (GtkWidget      *widget,
209                      GdkEventExpose *event)
210 {
211   g_return_val_if_fail (widget != NULL, FALSE);
212   g_return_val_if_fail (GTK_IS_EVENT_BOX (widget), FALSE);
213   g_return_val_if_fail (event != NULL, FALSE);
214
215   if (GTK_WIDGET_DRAWABLE (widget))
216     {
217       gtk_event_box_paint (widget, &event->area);
218       
219       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
220     }
221
222   return FALSE;
223 }
224