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/.
29 #include "gtkeventbox.h"
31 #include "gtksizerequest.h"
33 #include "gtkprivate.h"
40 GdkWindow *event_window;
50 #define GTK_EVENT_BOX_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxPrivate)
52 static void gtk_event_box_realize (GtkWidget *widget);
53 static void gtk_event_box_unrealize (GtkWidget *widget);
54 static void gtk_event_box_map (GtkWidget *widget);
55 static void gtk_event_box_unmap (GtkWidget *widget);
56 static void gtk_event_box_size_request (GtkWidget *widget,
57 GtkRequisition *requisition);
58 static void gtk_event_box_size_allocate (GtkWidget *widget,
59 GtkAllocation *allocation);
60 static gboolean gtk_event_box_expose (GtkWidget *widget,
61 GdkEventExpose *event);
62 static void gtk_event_box_set_property (GObject *object,
66 static void gtk_event_box_get_property (GObject *object,
71 G_DEFINE_TYPE (GtkEventBox, gtk_event_box, GTK_TYPE_BIN)
74 gtk_event_box_class_init (GtkEventBoxClass *class)
76 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
77 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
79 gobject_class->set_property = gtk_event_box_set_property;
80 gobject_class->get_property = gtk_event_box_get_property;
82 widget_class->realize = gtk_event_box_realize;
83 widget_class->unrealize = gtk_event_box_unrealize;
84 widget_class->map = gtk_event_box_map;
85 widget_class->unmap = gtk_event_box_unmap;
86 widget_class->size_request = gtk_event_box_size_request;
87 widget_class->size_allocate = gtk_event_box_size_allocate;
88 widget_class->expose_event = gtk_event_box_expose;
90 g_object_class_install_property (gobject_class,
92 g_param_spec_boolean ("visible-window",
94 P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
96 GTK_PARAM_READWRITE));
97 g_object_class_install_property (gobject_class,
99 g_param_spec_boolean ("above-child",
101 P_("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
103 GTK_PARAM_READWRITE));
105 g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
109 gtk_event_box_init (GtkEventBox *event_box)
111 GtkEventBoxPrivate *priv;
113 gtk_widget_set_has_window (GTK_WIDGET (event_box), TRUE);
115 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
116 priv->above_child = FALSE;
120 gtk_event_box_new (void)
122 return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
126 gtk_event_box_set_property (GObject *object,
131 GtkEventBox *event_box;
133 event_box = GTK_EVENT_BOX (object);
137 case PROP_VISIBLE_WINDOW:
138 gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
140 case PROP_ABOVE_CHILD:
141 gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
150 gtk_event_box_get_property (GObject *object,
155 GtkEventBox *event_box;
157 event_box = GTK_EVENT_BOX (object);
161 case PROP_VISIBLE_WINDOW:
162 g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
164 case PROP_ABOVE_CHILD:
165 g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
168 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174 * gtk_event_box_get_visible_window:
175 * @event_box: a #GtkEventBox
177 * Returns whether the event box has a visible window.
178 * See gtk_event_box_set_visible_window() for details.
180 * Return value: %TRUE if the event box window is visible
185 gtk_event_box_get_visible_window (GtkEventBox *event_box)
187 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
189 return gtk_widget_get_has_window (GTK_WIDGET (event_box));
193 * gtk_event_box_set_visible_window:
194 * @event_box: a #GtkEventBox
195 * @visible_window: boolean value
197 * Set whether the event box uses a visible or invisible child
198 * window. The default is to use visible windows.
200 * In an invisible window event box, the window that the
201 * event box creates is a %GDK_INPUT_ONLY window, which
202 * means that it is invisible and only serves to receive
205 * A visible window event box creates a visible (%GDK_INPUT_OUTPUT)
206 * window that acts as the parent window for all the widgets
207 * contained in the event box.
209 * You should generally make your event box invisible if
210 * you just want to trap events. Creating a visible window
211 * may cause artifacts that are visible to the user, especially
212 * if the user is using a theme with gradients or pixmaps.
214 * The main reason to create a non input-only event box is if
215 * you want to set the background to a different color or
219 * There is one unexpected issue for an invisible event box that has its
220 * window below the child. (See gtk_event_box_set_above_child().)
221 * Since the input-only window is not an ancestor window of any windows
222 * that descendent widgets of the event box create, events on these
223 * windows aren't propagated up by the windowing system, but only by GTK+.
224 * The practical effect of this is if an event isn't in the event
225 * mask for the descendant window (see gtk_widget_add_events()),
226 * it won't be received by the event box.
228 * This problem doesn't occur for visible event boxes, because in
229 * that case, the event box window is actually the ancestor of the
230 * descendant windows, not just at the same place on the screen.
236 gtk_event_box_set_visible_window (GtkEventBox *event_box,
237 gboolean visible_window)
241 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
243 widget = GTK_WIDGET (event_box);
245 visible_window = visible_window != FALSE;
247 if (visible_window != gtk_widget_get_has_window (widget))
249 if (gtk_widget_get_realized (widget))
251 gboolean visible = gtk_widget_get_visible (widget);
254 gtk_widget_hide (widget);
256 gtk_widget_unrealize (widget);
258 gtk_widget_set_has_window (widget, visible_window);
260 gtk_widget_realize (widget);
263 gtk_widget_show (widget);
267 gtk_widget_set_has_window (widget, visible_window);
270 if (gtk_widget_get_visible (widget))
271 gtk_widget_queue_resize (widget);
273 g_object_notify (G_OBJECT (event_box), "visible-window");
278 * gtk_event_box_get_above_child:
279 * @event_box: a #GtkEventBox
281 * Returns whether the event box window is above or below the
282 * windows of its child. See gtk_event_box_set_above_child() for
285 * Return value: %TRUE if the event box window is above the window
291 gtk_event_box_get_above_child (GtkEventBox *event_box)
293 GtkEventBoxPrivate *priv;
295 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
297 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
299 return priv->above_child;
303 * gtk_event_box_set_above_child:
304 * @event_box: a #GtkEventBox
305 * @above_child: %TRUE if the event box window is above the windows of its child
307 * Set whether the event box window is positioned above the windows of its child,
308 * as opposed to below it. If the window is above, all events inside the
309 * event box will go to the event box. If the window is below, events
310 * in windows of child widgets will first got to that widget, and then
313 * The default is to keep the window below the child.
318 gtk_event_box_set_above_child (GtkEventBox *event_box,
319 gboolean above_child)
322 GtkEventBoxPrivate *priv;
324 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
326 widget = GTK_WIDGET (event_box);
327 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
329 above_child = above_child != FALSE;
331 if (priv->above_child != above_child)
333 priv->above_child = above_child;
335 if (gtk_widget_get_realized (widget))
337 if (!gtk_widget_get_has_window (widget))
340 gdk_window_raise (priv->event_window);
342 gdk_window_lower (priv->event_window);
346 gboolean visible = gtk_widget_get_visible (widget);
349 gtk_widget_hide (widget);
351 gtk_widget_unrealize (widget);
353 gtk_widget_realize (widget);
356 gtk_widget_show (widget);
360 if (gtk_widget_get_visible (widget))
361 gtk_widget_queue_resize (widget);
363 g_object_notify (G_OBJECT (event_box), "above-child");
369 gtk_event_box_realize (GtkWidget *widget)
371 GtkAllocation allocation;
373 GdkWindowAttr attributes;
374 gint attributes_mask;
376 GtkEventBoxPrivate *priv;
377 gboolean visible_window;
379 gtk_widget_get_allocation (widget, &allocation);
381 gtk_widget_set_realized (widget, TRUE);
383 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
385 attributes.x = allocation.x + border_width;
386 attributes.y = allocation.y + border_width;
387 attributes.width = allocation.width - 2*border_width;
388 attributes.height = allocation.height - 2*border_width;
389 attributes.window_type = GDK_WINDOW_CHILD;
390 attributes.event_mask = gtk_widget_get_events (widget)
391 | GDK_BUTTON_MOTION_MASK
392 | GDK_BUTTON_PRESS_MASK
393 | GDK_BUTTON_RELEASE_MASK
395 | GDK_ENTER_NOTIFY_MASK
396 | GDK_LEAVE_NOTIFY_MASK;
398 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
400 visible_window = gtk_widget_get_has_window (widget);
403 attributes.visual = gtk_widget_get_visual (widget);
404 attributes.wclass = GDK_INPUT_OUTPUT;
406 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
408 window = gdk_window_new (gtk_widget_get_parent_window (widget),
409 &attributes, attributes_mask);
410 gtk_widget_set_window (widget, window);
411 gdk_window_set_user_data (window, widget);
415 window = gtk_widget_get_parent_window (widget);
416 gtk_widget_set_window (widget, window);
417 g_object_ref (window);
420 if (!visible_window || priv->above_child)
422 attributes.wclass = GDK_INPUT_ONLY;
424 attributes_mask = GDK_WA_X | GDK_WA_Y;
428 priv->event_window = gdk_window_new (window,
429 &attributes, attributes_mask);
430 gdk_window_set_user_data (priv->event_window, widget);
433 gtk_widget_style_attach (widget);
436 gtk_style_set_background (gtk_widget_get_style (widget), window, GTK_STATE_NORMAL);
440 gtk_event_box_unrealize (GtkWidget *widget)
442 GtkEventBoxPrivate *priv;
444 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
446 if (priv->event_window != NULL)
448 gdk_window_set_user_data (priv->event_window, NULL);
449 gdk_window_destroy (priv->event_window);
450 priv->event_window = NULL;
453 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unrealize (widget);
457 gtk_event_box_map (GtkWidget *widget)
459 GtkEventBoxPrivate *priv;
461 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
463 if (priv->event_window != NULL && !priv->above_child)
464 gdk_window_show (priv->event_window);
466 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->map (widget);
468 if (priv->event_window != NULL && priv->above_child)
469 gdk_window_show (priv->event_window);
473 gtk_event_box_unmap (GtkWidget *widget)
475 GtkEventBoxPrivate *priv;
477 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
479 if (priv->event_window != NULL)
480 gdk_window_hide (priv->event_window);
482 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unmap (widget);
488 gtk_event_box_size_request (GtkWidget *widget,
489 GtkRequisition *requisition)
491 GtkBin *bin = GTK_BIN (widget);
495 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
496 requisition->width = border_width * 2;
497 requisition->height = border_width * 2;
499 child = gtk_bin_get_child (bin);
500 if (child && gtk_widget_get_visible (child))
502 GtkRequisition child_requisition;
504 gtk_size_request_get_size (GTK_SIZE_REQUEST (child),
505 &child_requisition, NULL);
507 requisition->width += child_requisition.width;
508 requisition->height += child_requisition.height;
513 gtk_event_box_size_allocate (GtkWidget *widget,
514 GtkAllocation *allocation)
517 GtkAllocation child_allocation;
518 GtkEventBoxPrivate *priv;
522 bin = GTK_BIN (widget);
524 gtk_widget_set_allocation (widget, allocation);
526 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
528 if (!gtk_widget_get_has_window (widget))
530 child_allocation.x = allocation->x + border_width;
531 child_allocation.y = allocation->y + border_width;
535 child_allocation.x = 0;
536 child_allocation.y = 0;
538 child_allocation.width = MAX (allocation->width - border_width * 2, 0);
539 child_allocation.height = MAX (allocation->height - border_width * 2, 0);
541 if (gtk_widget_get_realized (widget))
543 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
545 if (priv->event_window != NULL)
546 gdk_window_move_resize (priv->event_window,
549 child_allocation.width,
550 child_allocation.height);
552 if (gtk_widget_get_has_window (widget))
553 gdk_window_move_resize (gtk_widget_get_window (widget),
554 allocation->x + border_width,
555 allocation->y + border_width,
556 child_allocation.width,
557 child_allocation.height);
560 child = gtk_bin_get_child (bin);
562 gtk_widget_size_allocate (child, &child_allocation);
566 gtk_event_box_expose (GtkWidget *widget,
567 GdkEventExpose *event)
569 if (gtk_widget_get_has_window (widget) &&
570 !gtk_widget_get_app_paintable (widget))
571 gtk_paint_flat_box (gtk_widget_get_style (widget),
572 gtk_widget_get_window (widget),
573 gtk_widget_get_state (widget),
575 &event->area, widget, "eventbox",
578 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->expose_event (widget, event);