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 ()
39 static guint 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,
54 event_box_type = gtk_type_unique (gtk_bin_get_type (), &event_box_info);
57 return event_box_type;
61 gtk_event_box_class_init (GtkEventBoxClass *class)
63 GtkWidgetClass *widget_class;
65 widget_class = (GtkWidgetClass*) class;
67 widget_class->realize = gtk_event_box_realize;
68 widget_class->size_request = gtk_event_box_size_request;
69 widget_class->size_allocate = gtk_event_box_size_allocate;
70 widget_class->draw = gtk_event_box_draw;
71 widget_class->expose_event = gtk_event_box_expose;
75 gtk_event_box_init (GtkEventBox *event_box)
77 GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
78 GTK_WIDGET_SET_FLAGS (event_box, GTK_BASIC);
84 return GTK_WIDGET ( gtk_type_new (gtk_event_box_get_type ()));
88 gtk_event_box_realize (GtkWidget *widget)
90 GdkWindowAttr attributes;
94 g_return_if_fail (widget != NULL);
95 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
97 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
99 border_width = GTK_CONTAINER (widget)->border_width;
101 attributes.x = widget->allocation.x + border_width;
102 attributes.y = widget->allocation.y + border_width;
103 attributes.width = widget->allocation.width - 2*border_width;
104 attributes.height = widget->allocation.height - 2*border_width;
105 attributes.window_type = GDK_WINDOW_CHILD;
106 attributes.wclass = GDK_INPUT_OUTPUT;
107 attributes.visual = gtk_widget_get_visual (widget);
108 attributes.colormap = gtk_widget_get_colormap (widget);
109 attributes.event_mask = gtk_widget_get_events (widget)
110 | GDK_BUTTON_MOTION_MASK
111 | GDK_BUTTON_PRESS_MASK
112 | GDK_BUTTON_RELEASE_MASK
114 | GDK_ENTER_NOTIFY_MASK
115 | GDK_LEAVE_NOTIFY_MASK;
117 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
119 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
120 gdk_window_set_user_data (widget->window, widget);
122 widget->style = gtk_style_attach (widget->style, widget->window);
123 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
127 gtk_event_box_size_request (GtkWidget *widget,
128 GtkRequisition *requisition)
132 g_return_if_fail (widget != NULL);
133 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
134 g_return_if_fail (requisition != NULL);
136 bin = GTK_BIN (widget);
138 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
139 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
141 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
143 gtk_widget_size_request (bin->child, &bin->child->requisition);
145 requisition->width += bin->child->requisition.width;
146 requisition->height += bin->child->requisition.height;
151 gtk_event_box_size_allocate (GtkWidget *widget,
152 GtkAllocation *allocation)
155 GtkAllocation child_allocation;
157 g_return_if_fail (widget != NULL);
158 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
159 g_return_if_fail (allocation != NULL);
161 widget->allocation = *allocation;
162 bin = GTK_BIN (widget);
164 child_allocation.x = 0;
165 child_allocation.y = 0;
166 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
167 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
169 if (GTK_WIDGET_REALIZED (widget))
171 gdk_window_move_resize (widget->window,
172 allocation->x + GTK_CONTAINER (widget)->border_width,
173 allocation->y + GTK_CONTAINER (widget)->border_width,
174 child_allocation.width,
175 child_allocation.height);
178 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
180 gtk_widget_size_allocate (bin->child, &child_allocation);
185 gtk_event_box_draw (GtkWidget *widget,
189 GdkRectangle tmp_area;
190 GdkRectangle child_area;
192 g_return_if_fail (widget != NULL);
193 g_return_if_fail (GTK_IS_EVENT_BOX (widget));
195 if (GTK_WIDGET_DRAWABLE (widget))
197 bin = GTK_BIN (widget);
199 tmp_area.x -= GTK_CONTAINER (widget)->border_width;
200 tmp_area.y -= GTK_CONTAINER (widget)->border_width;
204 if (gtk_widget_intersect (bin->child, &tmp_area, &child_area))
205 gtk_widget_draw (bin->child, &child_area);
211 gtk_event_box_expose (GtkWidget *widget,
212 GdkEventExpose *event)
215 GdkEventExpose child_event;
217 g_return_val_if_fail (widget != NULL, FALSE);
218 g_return_val_if_fail (GTK_IS_EVENT_BOX (widget), FALSE);
219 g_return_val_if_fail (event != NULL, FALSE);
221 if (GTK_WIDGET_DRAWABLE (widget))
223 bin = GTK_BIN (widget);
225 child_event = *event;
227 GTK_WIDGET_NO_WINDOW (bin->child) &&
228 gtk_widget_intersect (bin->child, &event->area, &child_event.area))
229 gtk_widget_event (bin->child, (GdkEvent*) &child_event);