]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
Fixed some bugs with set_default_size.
[~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 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
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include "gtksignal.h"
20 #include "gtkeventbox.h"
21
22
23 static void gtk_event_box_class_init               (GtkEventBoxClass *klass);
24 static void gtk_event_box_init                     (GtkEventBox      *event_box);
25 static void gtk_event_box_realize                  (GtkWidget        *widget);
26 static void gtk_event_box_size_request             (GtkWidget        *widget,
27                                                     GtkRequisition   *requisition);
28 static void gtk_event_box_size_allocate            (GtkWidget        *widget,
29                                                     GtkAllocation    *allocation);
30 static void gtk_event_box_paint                    (GtkWidget         *widget,
31                                                     GdkRectangle      *area);
32 static void gtk_event_box_draw                     (GtkWidget         *widget,
33                                                    GdkRectangle       *area);
34 static gint gtk_event_box_expose                   (GtkWidget         *widget,
35                                                    GdkEventExpose     *event);
36
37
38 GtkType
39 gtk_event_box_get_type (void)
40 {
41   static GtkType event_box_type = 0;
42
43   if (!event_box_type)
44     {
45       static const GtkTypeInfo event_box_info =
46       {
47         "GtkEventBox",
48         sizeof (GtkEventBox),
49         sizeof (GtkEventBoxClass),
50         (GtkClassInitFunc) gtk_event_box_class_init,
51         (GtkObjectInitFunc) gtk_event_box_init,
52         /* reserved_1 */ NULL,
53         /* reserved_2 */ NULL,
54         (GtkClassInitFunc) NULL,
55       };
56
57       event_box_type = gtk_type_unique (gtk_bin_get_type (), &event_box_info);
58     }
59
60   return event_box_type;
61 }
62
63 static void
64 gtk_event_box_class_init (GtkEventBoxClass *class)
65 {
66   GtkWidgetClass *widget_class;
67
68   widget_class = (GtkWidgetClass*) class;
69
70   widget_class->realize = gtk_event_box_realize;
71   widget_class->size_request = gtk_event_box_size_request;
72   widget_class->size_allocate = gtk_event_box_size_allocate;
73   widget_class->draw = gtk_event_box_draw;
74   widget_class->expose_event = gtk_event_box_expose;
75 }
76
77 static void
78 gtk_event_box_init (GtkEventBox *event_box)
79 {
80   GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
81 }
82
83 GtkWidget*
84 gtk_event_box_new (void)
85 {
86   return GTK_WIDGET ( gtk_type_new (gtk_event_box_get_type ()));
87 }
88
89 static void
90 gtk_event_box_realize (GtkWidget *widget)
91 {
92   GdkWindowAttr attributes;
93   gint attributes_mask;
94   gint border_width;
95
96   g_return_if_fail (widget != NULL);
97   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
98
99   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
100
101   border_width = GTK_CONTAINER (widget)->border_width;
102   
103   attributes.x = widget->allocation.x + border_width;
104   attributes.y = widget->allocation.y + border_width;
105   attributes.width = widget->allocation.width - 2*border_width;
106   attributes.height = widget->allocation.height - 2*border_width;
107   attributes.window_type = GDK_WINDOW_CHILD;
108   attributes.wclass = GDK_INPUT_OUTPUT;
109   attributes.visual = gtk_widget_get_visual (widget);
110   attributes.colormap = gtk_widget_get_colormap (widget);
111   attributes.event_mask = gtk_widget_get_events (widget)
112                         | GDK_BUTTON_MOTION_MASK
113                         | GDK_BUTTON_PRESS_MASK
114                         | GDK_BUTTON_RELEASE_MASK
115                         | GDK_EXPOSURE_MASK
116                         | GDK_ENTER_NOTIFY_MASK
117                         | GDK_LEAVE_NOTIFY_MASK;
118
119   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
120
121   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
122   gdk_window_set_user_data (widget->window, widget);
123
124   widget->style = gtk_style_attach (widget->style, widget->window);
125   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
126 }
127
128 static void
129 gtk_event_box_size_request (GtkWidget      *widget,
130                         GtkRequisition *requisition)
131 {
132   GtkBin *bin;
133
134   g_return_if_fail (widget != NULL);
135   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
136   g_return_if_fail (requisition != NULL);
137
138   bin = GTK_BIN (widget);
139
140   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
141   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
142
143   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
144     {
145       GtkRequisition child_requisition;
146       
147       gtk_widget_size_request (bin->child, &child_requisition);
148
149       requisition->width += child_requisition.width;
150       requisition->height += child_requisition.height;
151     }
152 }
153
154 static void
155 gtk_event_box_size_allocate (GtkWidget     *widget,
156                             GtkAllocation *allocation)
157 {
158   GtkBin *bin;
159   GtkAllocation child_allocation;
160
161   g_return_if_fail (widget != NULL);
162   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
163   g_return_if_fail (allocation != NULL);
164
165   widget->allocation = *allocation;
166   bin = GTK_BIN (widget);
167
168   child_allocation.x = 0;
169   child_allocation.y = 0;
170   child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
171   child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
172
173   if (GTK_WIDGET_REALIZED (widget))
174     {
175       gdk_window_move_resize (widget->window,
176                               allocation->x + GTK_CONTAINER (widget)->border_width,
177                               allocation->y + GTK_CONTAINER (widget)->border_width,
178                               child_allocation.width,
179                               child_allocation.height);
180     }
181   
182   if (bin->child)
183     {
184       gtk_widget_size_allocate (bin->child, &child_allocation);
185     }
186 }
187
188 static void
189 gtk_event_box_paint (GtkWidget    *widget,
190                      GdkRectangle *area)
191 {
192   gtk_paint_flat_box (widget->style, widget->window,
193                       widget->state, GTK_SHADOW_NONE,
194                       area, widget, "eventbox",
195                       0, 0, -1, -1);
196 }
197
198 static void
199 gtk_event_box_draw (GtkWidget    *widget,
200                     GdkRectangle *area)
201 {
202   GtkBin *bin;
203   GdkRectangle tmp_area;
204   GdkRectangle child_area;
205
206   g_return_if_fail (widget != NULL);
207   g_return_if_fail (GTK_IS_EVENT_BOX (widget));
208
209   if (GTK_WIDGET_DRAWABLE (widget))
210     {
211       bin = GTK_BIN (widget);
212       tmp_area = *area;
213       tmp_area.x -= GTK_CONTAINER (widget)->border_width;
214       tmp_area.y -= GTK_CONTAINER (widget)->border_width;
215
216       gtk_event_box_paint (widget, &tmp_area);
217       
218       if (bin->child)
219         {
220           if (gtk_widget_intersect (bin->child, &tmp_area, &child_area))
221             gtk_widget_draw (bin->child, &child_area);
222         }
223     }
224 }
225
226 static gint
227 gtk_event_box_expose (GtkWidget      *widget,
228                      GdkEventExpose *event)
229 {
230   GtkBin *bin;
231   GdkEventExpose child_event;
232
233   g_return_val_if_fail (widget != NULL, FALSE);
234   g_return_val_if_fail (GTK_IS_EVENT_BOX (widget), FALSE);
235   g_return_val_if_fail (event != NULL, FALSE);
236
237   if (GTK_WIDGET_DRAWABLE (widget))
238     {
239       bin = GTK_BIN (widget);
240
241       gtk_event_box_paint (widget, &event->area);
242       
243       child_event = *event;
244       if (bin->child &&
245           GTK_WIDGET_NO_WINDOW (bin->child) &&
246           gtk_widget_intersect (bin->child, &event->area, &child_event.area))
247         gtk_widget_event (bin->child, (GdkEvent*) &child_event);
248     }
249
250   return FALSE;
251 }
252