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_draw (GtkWidget *widget,
32 static gint gtk_event_box_expose (GtkWidget *widget,
33 GdkEventExpose *event);
37 gtk_event_box_get_type (void)
39 static GtkType event_box_type = 0;
43 GtkTypeInfo event_box_info =
47 sizeof (GtkEventBoxClass),
48 (GtkClassInitFunc) gtk_event_box_class_init,
49 (GtkObjectInitFunc) gtk_event_box_init,
50 /* reserved_1 */ NULL,
51 /* reserved_2 */ NULL,
52 (GtkClassInitFunc) NULL,
55 event_box_type = gtk_type_unique (gtk_bin_get_type (), &event_box_info);
58 return event_box_type;
62 gtk_event_box_class_init (GtkEventBoxClass *class)
64 GtkWidgetClass *widget_class;
66 widget_class = (GtkWidgetClass*) class;
68 widget_class->realize = gtk_event_box_realize;
69 widget_class->size_request = gtk_event_box_size_request;
70 widget_class->size_allocate = gtk_event_box_size_allocate;
71 widget_class->draw = gtk_event_box_draw;
72 widget_class->expose_event = gtk_event_box_expose;
76 gtk_event_box_init (GtkEventBox *event_box)
78 GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
79 GTK_WIDGET_SET_FLAGS (event_box, GTK_BASIC);
83 gtk_event_box_new (void)
85 return GTK_WIDGET ( gtk_type_new (gtk_event_box_get_type ()));
89 gtk_event_box_realize (GtkWidget *widget)
91 GdkWindowAttr attributes;
95 g_return_if_fail (widget != NULL);
96 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
98 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
100 border_width = GTK_CONTAINER (widget)->border_width;
102 attributes.x = widget->allocation.x + border_width;
103 attributes.y = widget->allocation.y + border_width;
104 attributes.width = widget->allocation.width - 2*border_width;
105 attributes.height = widget->allocation.height - 2*border_width;
106 attributes.window_type = GDK_WINDOW_CHILD;
107 attributes.wclass = GDK_INPUT_OUTPUT;
108 attributes.visual = gtk_widget_get_visual (widget);
109 attributes.colormap = gtk_widget_get_colormap (widget);
110 attributes.event_mask = gtk_widget_get_events (widget)
111 | GDK_BUTTON_MOTION_MASK
112 | GDK_BUTTON_PRESS_MASK
113 | GDK_BUTTON_RELEASE_MASK
115 | GDK_ENTER_NOTIFY_MASK
116 | GDK_LEAVE_NOTIFY_MASK;
118 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
120 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
121 gdk_window_set_user_data (widget->window, widget);
123 widget->style = gtk_style_attach (widget->style, widget->window);
124 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
128 gtk_event_box_size_request (GtkWidget *widget,
129 GtkRequisition *requisition)
133 g_return_if_fail (widget != NULL);
134 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
135 g_return_if_fail (requisition != NULL);
137 bin = GTK_BIN (widget);
139 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
140 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
142 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
144 gtk_widget_size_request (bin->child, &bin->child->requisition);
146 requisition->width += bin->child->requisition.width;
147 requisition->height += bin->child->requisition.height;
152 gtk_event_box_size_allocate (GtkWidget *widget,
153 GtkAllocation *allocation)
156 GtkAllocation child_allocation;
158 g_return_if_fail (widget != NULL);
159 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
160 g_return_if_fail (allocation != NULL);
162 widget->allocation = *allocation;
163 bin = GTK_BIN (widget);
165 child_allocation.x = 0;
166 child_allocation.y = 0;
167 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
168 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
170 if (GTK_WIDGET_REALIZED (widget))
172 gdk_window_move_resize (widget->window,
173 allocation->x + GTK_CONTAINER (widget)->border_width,
174 allocation->y + GTK_CONTAINER (widget)->border_width,
175 child_allocation.width,
176 child_allocation.height);
181 gtk_widget_size_allocate (bin->child, &child_allocation);
186 gtk_event_box_draw (GtkWidget *widget,
190 GdkRectangle tmp_area;
191 GdkRectangle child_area;
193 g_return_if_fail (widget != NULL);
194 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
196 if (GTK_WIDGET_DRAWABLE (widget))
198 bin = GTK_BIN (widget);
200 tmp_area.x -= GTK_CONTAINER (widget)->border_width;
201 tmp_area.y -= GTK_CONTAINER (widget)->border_width;
205 if (gtk_widget_intersect (bin->child, &tmp_area, &child_area))
206 gtk_widget_draw (bin->child, &child_area);
212 gtk_event_box_expose (GtkWidget *widget,
213 GdkEventExpose *event)
216 GdkEventExpose child_event;
218 g_return_val_if_fail (widget != NULL, FALSE);
219 g_return_val_if_fail (GTK_IS_EVENT_BOX (widget), FALSE);
220 g_return_val_if_fail (event != NULL, FALSE);
222 if (GTK_WIDGET_DRAWABLE (widget))
224 bin = GTK_BIN (widget);
226 child_event = *event;
228 GTK_WIDGET_NO_WINDOW (bin->child) &&
229 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
230 gtk_widget_event (bin->child, (GdkEvent*) &child_event);