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",
119 P_("Visible Window"),
120 P_("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 P_("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 priv->above_child = above_child;
369 if (GTK_WIDGET_REALIZED (widget))
371 if (GTK_WIDGET_NO_WINDOW (widget))
374 gdk_window_raise (priv->event_window);
376 gdk_window_lower (priv->event_window);
380 gboolean visible = GTK_WIDGET_VISIBLE (widget);
383 gtk_widget_hide (widget);
385 gtk_widget_unrealize (widget);
387 gtk_widget_realize (widget);
390 gtk_widget_show (widget);
394 if (GTK_WIDGET_VISIBLE (widget))
395 gtk_widget_queue_resize (widget);
397 g_object_notify (G_OBJECT (event_box), "above-child");
404 gtk_event_box_realize (GtkWidget *widget)
406 GdkWindowAttr attributes;
407 gint attributes_mask;
409 GtkEventBoxPrivate *priv;
410 gboolean visible_window;
412 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
414 border_width = GTK_CONTAINER (widget)->border_width;
416 attributes.x = widget->allocation.x + border_width;
417 attributes.y = widget->allocation.y + border_width;
418 attributes.width = widget->allocation.width - 2*border_width;
419 attributes.height = widget->allocation.height - 2*border_width;
420 attributes.window_type = GDK_WINDOW_CHILD;
421 attributes.event_mask = gtk_widget_get_events (widget)
422 | GDK_BUTTON_MOTION_MASK
423 | GDK_BUTTON_PRESS_MASK
424 | GDK_BUTTON_RELEASE_MASK
426 | GDK_ENTER_NOTIFY_MASK
427 | GDK_LEAVE_NOTIFY_MASK;
429 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
431 visible_window = !GTK_WIDGET_NO_WINDOW (widget);
434 attributes.visual = gtk_widget_get_visual (widget);
435 attributes.colormap = gtk_widget_get_colormap (widget);
436 attributes.wclass = GDK_INPUT_OUTPUT;
438 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
440 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
441 &attributes, attributes_mask);
442 gdk_window_set_user_data (widget->window, widget);
446 widget->window = gtk_widget_get_parent_window (widget);
447 g_object_ref (widget->window);
450 if (!visible_window || priv->above_child)
452 attributes.wclass = GDK_INPUT_ONLY;
453 attributes_mask = GDK_WA_X | GDK_WA_Y;
455 priv->event_window = gdk_window_new (widget->window,
456 &attributes, attributes_mask);
457 gdk_window_set_user_data (priv->event_window, widget);
461 widget->style = gtk_style_attach (widget->style, widget->window);
464 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
468 gtk_event_box_unrealize (GtkWidget *widget)
470 GtkEventBox *event_box;
471 GtkEventBoxPrivate *priv;
473 event_box = GTK_EVENT_BOX (widget);
474 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
476 if (priv->event_window != NULL)
478 gdk_window_set_user_data (priv->event_window, NULL);
479 gdk_window_destroy (priv->event_window);
480 priv->event_window = NULL;
483 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
484 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
488 gtk_event_box_map (GtkWidget *widget)
490 GtkEventBoxPrivate *priv;
492 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
494 if (priv->event_window != NULL && !priv->above_child)
495 gdk_window_show (priv->event_window);
497 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
499 if (priv->event_window != NULL && priv->above_child)
500 gdk_window_show (priv->event_window);
504 gtk_event_box_unmap (GtkWidget *widget)
506 GtkEventBoxPrivate *priv;
508 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
510 if (priv->event_window != NULL)
511 gdk_window_hide (priv->event_window);
513 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
519 gtk_event_box_size_request (GtkWidget *widget,
520 GtkRequisition *requisition)
522 GtkBin *bin = GTK_BIN (widget);
524 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
525 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
527 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
529 GtkRequisition child_requisition;
531 gtk_widget_size_request (bin->child, &child_requisition);
533 requisition->width += child_requisition.width;
534 requisition->height += child_requisition.height;
539 gtk_event_box_size_allocate (GtkWidget *widget,
540 GtkAllocation *allocation)
543 GtkAllocation child_allocation;
544 GtkEventBoxPrivate *priv;
546 widget->allocation = *allocation;
547 bin = GTK_BIN (widget);
549 if (GTK_WIDGET_NO_WINDOW (widget))
551 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
552 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
556 child_allocation.x = 0;
557 child_allocation.y = 0;
559 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
560 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
562 if (GTK_WIDGET_REALIZED (widget))
564 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
566 if (priv->event_window != NULL)
567 gdk_window_move_resize (priv->event_window,
570 child_allocation.width,
571 child_allocation.height);
573 if (!GTK_WIDGET_NO_WINDOW (widget))
574 gdk_window_move_resize (widget->window,
575 allocation->x + GTK_CONTAINER (widget)->border_width,
576 allocation->y + GTK_CONTAINER (widget)->border_width,
577 child_allocation.width,
578 child_allocation.height);
582 gtk_widget_size_allocate (bin->child, &child_allocation);
586 gtk_event_box_paint (GtkWidget *widget,
589 if (!GTK_WIDGET_APP_PAINTABLE (widget))
590 gtk_paint_flat_box (widget->style, widget->window,
591 widget->state, GTK_SHADOW_NONE,
592 area, widget, "eventbox",
597 gtk_event_box_expose (GtkWidget *widget,
598 GdkEventExpose *event)
600 if (GTK_WIDGET_DRAWABLE (widget))
602 if (!GTK_WIDGET_NO_WINDOW (widget))
603 gtk_event_box_paint (widget, &event->area);
605 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);