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 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.
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.
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.
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/.
27 #include "gtkeventbox.h"
33 GdkWindow *event_window;
43 #define GTK_EVENT_BOX_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxPrivate)
45 static void gtk_event_box_class_init (GtkEventBoxClass *klass);
46 static void gtk_event_box_init (GtkEventBox *event_box);
47 static void gtk_event_box_realize (GtkWidget *widget);
48 static void gtk_event_box_unrealize (GtkWidget *widget);
49 static void gtk_event_box_map (GtkWidget *widget);
50 static void gtk_event_box_unmap (GtkWidget *widget);
51 static void gtk_event_box_size_request (GtkWidget *widget,
52 GtkRequisition *requisition);
53 static void gtk_event_box_size_allocate (GtkWidget *widget,
54 GtkAllocation *allocation);
55 static void gtk_event_box_paint (GtkWidget *widget,
57 static gint gtk_event_box_expose (GtkWidget *widget,
58 GdkEventExpose *event);
59 static void gtk_event_box_set_property (GObject *object,
63 static void gtk_event_box_get_property (GObject *object,
68 static GtkBinClass *parent_class = NULL;
71 gtk_event_box_get_type (void)
73 static GType event_box_type = 0;
77 static const GTypeInfo event_box_info =
79 sizeof (GtkEventBoxClass),
81 NULL, /* base_finalize */
82 (GClassInitFunc) gtk_event_box_class_init,
83 NULL, /* class_finalize */
84 NULL, /* class_data */
87 (GInstanceInitFunc) gtk_event_box_init,
90 event_box_type = g_type_register_static (GTK_TYPE_BIN, "GtkEventBox",
94 return event_box_type;
98 gtk_event_box_class_init (GtkEventBoxClass *class)
100 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
101 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
103 parent_class = g_type_class_peek_parent (class);
105 gobject_class->set_property = gtk_event_box_set_property;
106 gobject_class->get_property = gtk_event_box_get_property;
108 widget_class->realize = gtk_event_box_realize;
109 widget_class->unrealize = gtk_event_box_unrealize;
110 widget_class->map = gtk_event_box_map;
111 widget_class->unmap = gtk_event_box_unmap;
112 widget_class->size_request = gtk_event_box_size_request;
113 widget_class->size_allocate = gtk_event_box_size_allocate;
114 widget_class->expose_event = gtk_event_box_expose;
116 g_object_class_install_property (gobject_class,
118 g_param_spec_boolean ("visible-window",
120 _(" Whether the event box is visible, as opposed to invisible and only used to trap events."),
123 g_object_class_install_property (gobject_class,
125 g_param_spec_boolean ("above-child",
127 _("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
131 g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
135 gtk_event_box_init (GtkEventBox *event_box)
137 GtkEventBoxPrivate *priv;
139 GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
141 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
142 priv->above_child = FALSE;
146 gtk_event_box_new (void)
148 return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
152 gtk_event_box_set_property (GObject *object,
157 GtkEventBox *event_box;
159 event_box = GTK_EVENT_BOX (object);
163 case PROP_VISIBLE_WINDOW:
164 gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
166 case PROP_ABOVE_CHILD:
167 gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 gtk_event_box_get_property (GObject *object,
181 GtkEventBox *event_box;
183 event_box = GTK_EVENT_BOX (object);
187 case PROP_VISIBLE_WINDOW:
188 g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
190 case PROP_ABOVE_CHILD:
191 g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 * gtk_event_box_get_visible_window:
201 * @event_box: a #GtkEventBox
203 * Returns whether the event box has a visible window.
204 * See gtk_event_box_set_visible_window() for details.
206 * Return value: %TRUE if the event box window is visible
211 gtk_event_box_get_visible_window (GtkEventBox *event_box)
213 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
215 return !GTK_WIDGET_NO_WINDOW (event_box);
219 * gtk_event_box_set_visible_window:
220 * @event_box: a #GtkEventBox
221 * @visible_window: boolean value
223 * Set whether the event box uses a visible or invisible child
224 * window. The default is to use visible windows.
226 * In an invisible window event box, the window that that the
227 * event box creates is a %GDK_INPUT_ONLY window, which
228 * means that it is invisible and only serves to receive
231 * A visible window event box creates a visible (%GDK_INPUT_OUTPUT)
232 * window that acts as the parent window for all the widgets
233 * contained in the event box.
235 * You should generally make your event box invisible if
236 * you just want to trap events. Creating a visible window
237 * may cause artifacts that are visible to the user, especially
238 * if the user is using a theme with gradients or pixmaps.
240 * The main reason to create a non input-only event box is if
241 * you want to set the background to a different color or
245 * There is one unexpected issue for an invisible event box that has its
246 * window below the child. (See gtk_event_box_set_above_child().)
247 * Since the input-only window is not an ancestor window of any windows
248 * that descendent widgets of the event box create, events on these
249 * windows aren't propagated up by the windowing system, but only by GTK+.
250 * The practical effect of this is if an event isn't in the event
251 * mask for the descendant window (see gtk_widget_add_events()),
252 * it won't be received by the event box.
254 * This problem doesn't occur for visible event boxes, because in
255 * that case, the event box window is actually the ancestor of the
256 * descendant windows, not just at the same place on the screen.
262 gtk_event_box_set_visible_window (GtkEventBox *event_box,
263 gboolean visible_window)
266 GtkEventBoxPrivate *priv;
268 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
270 widget = GTK_WIDGET (event_box);
271 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
273 visible_window = visible_window != FALSE;
275 if (visible_window != !GTK_WIDGET_NO_WINDOW (widget))
277 if (GTK_WIDGET_REALIZED (widget))
279 gboolean visible = GTK_WIDGET_VISIBLE (widget);
282 gtk_widget_hide (widget);
284 gtk_widget_unrealize (widget);
287 GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
289 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
291 gtk_widget_realize (widget);
294 gtk_widget_show (widget);
299 GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
301 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
304 if (GTK_WIDGET_VISIBLE (widget))
305 gtk_widget_queue_resize (widget);
307 g_object_notify (G_OBJECT (event_box), "visible-window");
312 * gtk_event_box_get_above_child:
313 * @event_box: a #GtkEventBox
315 * Returns whether the event box window is above or below the
316 * windows of its child. See gtk_event_box_set_above_child() for
319 * Return value: %TRUE if the event box window is above the window
325 gtk_event_box_get_above_child (GtkEventBox *event_box)
327 GtkEventBoxPrivate *priv;
329 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
331 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
333 return priv->above_child;
337 * gtk_event_box_set_above_child:
338 * @event_box: a #GtkEventBox
339 * @above_child: %TRUE if the event box window is above the windows of its child
341 * Set whether the event box window is positioned above the windows of its child,
342 * as opposed to below it. If the window is above, all events inside the
343 * event box will go to the event box. If the window is below, events
344 * in windows of child widgets will first got to that widget, and then
347 * The default is to keep the window below the child.
352 gtk_event_box_set_above_child (GtkEventBox *event_box,
353 gboolean above_child)
356 GtkEventBoxPrivate *priv;
358 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
360 widget = GTK_WIDGET (event_box);
361 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
363 above_child = above_child != FALSE;
365 if (priv->above_child != above_child)
367 if (GTK_WIDGET_REALIZED (widget))
369 if (GTK_WIDGET_NO_WINDOW (widget))
372 gdk_window_raise (priv->event_window);
374 gdk_window_lower (priv->event_window);
378 gboolean visible = GTK_WIDGET_VISIBLE (widget);
381 gtk_widget_hide (widget);
383 gtk_widget_unrealize (widget);
384 priv->above_child = above_child;
386 gtk_widget_realize (widget);
389 gtk_widget_show (widget);
393 priv->above_child = above_child;
395 if (GTK_WIDGET_VISIBLE (widget))
396 gtk_widget_queue_resize (widget);
398 g_object_notify (G_OBJECT (event_box), "above-child");
405 gtk_event_box_realize (GtkWidget *widget)
407 GdkWindowAttr attributes;
408 gint attributes_mask;
410 GtkEventBoxPrivate *priv;
411 gboolean visible_window;
413 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
415 border_width = GTK_CONTAINER (widget)->border_width;
417 attributes.x = widget->allocation.x + border_width;
418 attributes.y = widget->allocation.y + border_width;
419 attributes.width = widget->allocation.width - 2*border_width;
420 attributes.height = widget->allocation.height - 2*border_width;
421 attributes.window_type = GDK_WINDOW_CHILD;
422 attributes.event_mask = gtk_widget_get_events (widget)
423 | GDK_BUTTON_MOTION_MASK
424 | GDK_BUTTON_PRESS_MASK
425 | GDK_BUTTON_RELEASE_MASK
427 | GDK_ENTER_NOTIFY_MASK
428 | GDK_LEAVE_NOTIFY_MASK;
430 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
432 visible_window = !GTK_WIDGET_NO_WINDOW (widget);
435 attributes.visual = gtk_widget_get_visual (widget);
436 attributes.colormap = gtk_widget_get_colormap (widget);
437 attributes.wclass = GDK_INPUT_OUTPUT;
439 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
441 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
442 &attributes, attributes_mask);
443 gdk_window_set_user_data (widget->window, widget);
447 widget->window = gtk_widget_get_parent_window (widget);
448 g_object_ref (widget->window);
451 if (!visible_window || priv->above_child)
453 attributes.wclass = GDK_INPUT_ONLY;
454 attributes_mask = GDK_WA_X | GDK_WA_Y;
456 priv->event_window = gdk_window_new (widget->window,
457 &attributes, attributes_mask);
458 gdk_window_set_user_data (priv->event_window, widget);
462 widget->style = gtk_style_attach (widget->style, widget->window);
465 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
469 gtk_event_box_unrealize (GtkWidget *widget)
471 GtkEventBox *event_box;
472 GtkEventBoxPrivate *priv;
474 event_box = GTK_EVENT_BOX (widget);
475 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
477 if (priv->event_window != NULL)
479 gdk_window_set_user_data (priv->event_window, NULL);
480 gdk_window_destroy (priv->event_window);
481 priv->event_window = NULL;
484 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
485 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
489 gtk_event_box_map (GtkWidget *widget)
491 GtkEventBoxPrivate *priv;
493 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
495 if (priv->event_window != NULL && !priv->above_child)
496 gdk_window_show (priv->event_window);
498 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
500 if (priv->event_window != NULL && priv->above_child)
501 gdk_window_show (priv->event_window);
505 gtk_event_box_unmap (GtkWidget *widget)
507 GtkEventBoxPrivate *priv;
509 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
511 if (priv->event_window != NULL)
512 gdk_window_hide (priv->event_window);
514 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
520 gtk_event_box_size_request (GtkWidget *widget,
521 GtkRequisition *requisition)
523 GtkBin *bin = GTK_BIN (widget);
525 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
526 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
528 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
530 GtkRequisition child_requisition;
532 gtk_widget_size_request (bin->child, &child_requisition);
534 requisition->width += child_requisition.width;
535 requisition->height += child_requisition.height;
540 gtk_event_box_size_allocate (GtkWidget *widget,
541 GtkAllocation *allocation)
544 GtkAllocation child_allocation;
545 GtkEventBoxPrivate *priv;
547 widget->allocation = *allocation;
548 bin = GTK_BIN (widget);
550 if (GTK_WIDGET_NO_WINDOW (widget))
552 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
553 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
557 child_allocation.x = 0;
558 child_allocation.y = 0;
560 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
561 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
563 if (GTK_WIDGET_REALIZED (widget))
565 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
567 if (priv->event_window != NULL)
568 gdk_window_move_resize (priv->event_window,
571 child_allocation.width,
572 child_allocation.height);
574 if (!GTK_WIDGET_NO_WINDOW (widget))
575 gdk_window_move_resize (widget->window,
576 allocation->x + GTK_CONTAINER (widget)->border_width,
577 allocation->y + GTK_CONTAINER (widget)->border_width,
578 child_allocation.width,
579 child_allocation.height);
583 gtk_widget_size_allocate (bin->child, &child_allocation);
587 gtk_event_box_paint (GtkWidget *widget,
590 if (!GTK_WIDGET_APP_PAINTABLE (widget))
591 gtk_paint_flat_box (widget->style, widget->window,
592 widget->state, GTK_SHADOW_NONE,
593 area, widget, "eventbox",
598 gtk_event_box_expose (GtkWidget *widget,
599 GdkEventExpose *event)
601 if (GTK_WIDGET_DRAWABLE (widget))
603 if (!GTK_WIDGET_NO_WINDOW (widget))
604 gtk_event_box_paint (widget, &event->area);
606 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);