1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
19 #include "gtksignal.h"
20 #include "gtkeventbox.h"
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,
32 static void gtk_event_box_draw (GtkWidget *widget,
34 static gint gtk_event_box_expose (GtkWidget *widget,
35 GdkEventExpose *event);
39 gtk_event_box_get_type (void)
41 static GtkType event_box_type = 0;
45 static const GtkTypeInfo event_box_info =
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,
57 event_box_type = gtk_type_unique (gtk_bin_get_type (), &event_box_info);
60 return event_box_type;
64 gtk_event_box_class_init (GtkEventBoxClass *class)
66 GtkWidgetClass *widget_class;
68 widget_class = (GtkWidgetClass*) class;
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;
78 gtk_event_box_init (GtkEventBox *event_box)
80 GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
84 gtk_event_box_new (void)
86 return GTK_WIDGET ( gtk_type_new (gtk_event_box_get_type ()));
90 gtk_event_box_realize (GtkWidget *widget)
92 GdkWindowAttr attributes;
96 g_return_if_fail (widget != NULL);
97 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
99 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
101 border_width = GTK_CONTAINER (widget)->border_width;
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
116 | GDK_ENTER_NOTIFY_MASK
117 | GDK_LEAVE_NOTIFY_MASK;
119 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
121 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
122 gdk_window_set_user_data (widget->window, widget);
124 widget->style = gtk_style_attach (widget->style, widget->window);
125 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
129 gtk_event_box_size_request (GtkWidget *widget,
130 GtkRequisition *requisition)
134 g_return_if_fail (widget != NULL);
135 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
136 g_return_if_fail (requisition != NULL);
138 bin = GTK_BIN (widget);
140 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
141 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
143 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
145 GtkRequisition child_requisition;
147 gtk_widget_size_request (bin->child, &child_requisition);
149 requisition->width += child_requisition.width;
150 requisition->height += child_requisition.height;
155 gtk_event_box_size_allocate (GtkWidget *widget,
156 GtkAllocation *allocation)
159 GtkAllocation child_allocation;
161 g_return_if_fail (widget != NULL);
162 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
163 g_return_if_fail (allocation != NULL);
165 widget->allocation = *allocation;
166 bin = GTK_BIN (widget);
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);
173 if (GTK_WIDGET_REALIZED (widget))
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);
184 gtk_widget_size_allocate (bin->child, &child_allocation);
189 gtk_event_box_paint (GtkWidget *widget,
192 gtk_paint_flat_box (widget->style, widget->window,
193 widget->state, GTK_SHADOW_NONE,
194 area, widget, "eventbox",
199 gtk_event_box_draw (GtkWidget *widget,
203 GdkRectangle tmp_area;
204 GdkRectangle child_area;
206 g_return_if_fail (widget != NULL);
207 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
209 if (GTK_WIDGET_DRAWABLE (widget))
211 bin = GTK_BIN (widget);
213 tmp_area.x -= GTK_CONTAINER (widget)->border_width;
214 tmp_area.y -= GTK_CONTAINER (widget)->border_width;
216 gtk_event_box_paint (widget, &tmp_area);
220 if (gtk_widget_intersect (bin->child, &tmp_area, &child_area))
221 gtk_widget_draw (bin->child, &child_area);
227 gtk_event_box_expose (GtkWidget *widget,
228 GdkEventExpose *event)
231 GdkEventExpose child_event;
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);
237 if (GTK_WIDGET_DRAWABLE (widget))
239 bin = GTK_BIN (widget);
241 gtk_event_box_paint (widget, &event->area);
243 child_event = *event;
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);