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 void gtk_event_box_paint (GtkWidget *widget,
62 static gboolean gtk_event_box_expose (GtkWidget *widget,
63 GdkEventExpose *event);
64 static void gtk_event_box_set_property (GObject *object,
68 static void gtk_event_box_get_property (GObject *object,
73 G_DEFINE_TYPE (GtkEventBox, gtk_event_box, GTK_TYPE_BIN)
76 gtk_event_box_class_init (GtkEventBoxClass *class)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
79 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
81 gobject_class->set_property = gtk_event_box_set_property;
82 gobject_class->get_property = gtk_event_box_get_property;
84 widget_class->realize = gtk_event_box_realize;
85 widget_class->unrealize = gtk_event_box_unrealize;
86 widget_class->map = gtk_event_box_map;
87 widget_class->unmap = gtk_event_box_unmap;
88 widget_class->size_request = gtk_event_box_size_request;
89 widget_class->size_allocate = gtk_event_box_size_allocate;
90 widget_class->expose_event = gtk_event_box_expose;
92 g_object_class_install_property (gobject_class,
94 g_param_spec_boolean ("visible-window",
96 P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
98 GTK_PARAM_READWRITE));
99 g_object_class_install_property (gobject_class,
101 g_param_spec_boolean ("above-child",
103 P_("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
105 GTK_PARAM_READWRITE));
107 g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
111 gtk_event_box_init (GtkEventBox *event_box)
113 GtkEventBoxPrivate *priv;
115 gtk_widget_set_has_window (GTK_WIDGET (event_box), TRUE);
117 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
118 priv->above_child = FALSE;
122 gtk_event_box_new (void)
124 return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
128 gtk_event_box_set_property (GObject *object,
133 GtkEventBox *event_box;
135 event_box = GTK_EVENT_BOX (object);
139 case PROP_VISIBLE_WINDOW:
140 gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
142 case PROP_ABOVE_CHILD:
143 gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
152 gtk_event_box_get_property (GObject *object,
157 GtkEventBox *event_box;
159 event_box = GTK_EVENT_BOX (object);
163 case PROP_VISIBLE_WINDOW:
164 g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
166 case PROP_ABOVE_CHILD:
167 g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
170 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
176 * gtk_event_box_get_visible_window:
177 * @event_box: a #GtkEventBox
179 * Returns whether the event box has a visible window.
180 * See gtk_event_box_set_visible_window() for details.
182 * Return value: %TRUE if the event box window is visible
187 gtk_event_box_get_visible_window (GtkEventBox *event_box)
189 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
191 return gtk_widget_get_has_window (GTK_WIDGET (event_box));
195 * gtk_event_box_set_visible_window:
196 * @event_box: a #GtkEventBox
197 * @visible_window: boolean value
199 * Set whether the event box uses a visible or invisible child
200 * window. The default is to use visible windows.
202 * In an invisible window event box, the window that the
203 * event box creates is a %GDK_INPUT_ONLY window, which
204 * means that it is invisible and only serves to receive
207 * A visible window event box creates a visible (%GDK_INPUT_OUTPUT)
208 * window that acts as the parent window for all the widgets
209 * contained in the event box.
211 * You should generally make your event box invisible if
212 * you just want to trap events. Creating a visible window
213 * may cause artifacts that are visible to the user, especially
214 * if the user is using a theme with gradients or pixmaps.
216 * The main reason to create a non input-only event box is if
217 * you want to set the background to a different color or
221 * There is one unexpected issue for an invisible event box that has its
222 * window below the child. (See gtk_event_box_set_above_child().)
223 * Since the input-only window is not an ancestor window of any windows
224 * that descendent widgets of the event box create, events on these
225 * windows aren't propagated up by the windowing system, but only by GTK+.
226 * The practical effect of this is if an event isn't in the event
227 * mask for the descendant window (see gtk_widget_add_events()),
228 * it won't be received by the event box.
230 * This problem doesn't occur for visible event boxes, because in
231 * that case, the event box window is actually the ancestor of the
232 * descendant windows, not just at the same place on the screen.
238 gtk_event_box_set_visible_window (GtkEventBox *event_box,
239 gboolean visible_window)
243 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
245 widget = GTK_WIDGET (event_box);
247 visible_window = visible_window != FALSE;
249 if (visible_window != gtk_widget_get_has_window (widget))
251 if (gtk_widget_get_realized (widget))
253 gboolean visible = gtk_widget_get_visible (widget);
256 gtk_widget_hide (widget);
258 gtk_widget_unrealize (widget);
260 gtk_widget_set_has_window (widget, visible_window);
262 gtk_widget_realize (widget);
265 gtk_widget_show (widget);
269 gtk_widget_set_has_window (widget, visible_window);
272 if (gtk_widget_get_visible (widget))
273 gtk_widget_queue_resize (widget);
275 g_object_notify (G_OBJECT (event_box), "visible-window");
280 * gtk_event_box_get_above_child:
281 * @event_box: a #GtkEventBox
283 * Returns whether the event box window is above or below the
284 * windows of its child. See gtk_event_box_set_above_child() for
287 * Return value: %TRUE if the event box window is above the window
293 gtk_event_box_get_above_child (GtkEventBox *event_box)
295 GtkEventBoxPrivate *priv;
297 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
299 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
301 return priv->above_child;
305 * gtk_event_box_set_above_child:
306 * @event_box: a #GtkEventBox
307 * @above_child: %TRUE if the event box window is above the windows of its child
309 * Set whether the event box window is positioned above the windows of its child,
310 * as opposed to below it. If the window is above, all events inside the
311 * event box will go to the event box. If the window is below, events
312 * in windows of child widgets will first got to that widget, and then
315 * The default is to keep the window below the child.
320 gtk_event_box_set_above_child (GtkEventBox *event_box,
321 gboolean above_child)
324 GtkEventBoxPrivate *priv;
326 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
328 widget = GTK_WIDGET (event_box);
329 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
331 above_child = above_child != FALSE;
333 if (priv->above_child != above_child)
335 priv->above_child = above_child;
337 if (gtk_widget_get_realized (widget))
339 if (!gtk_widget_get_has_window (widget))
342 gdk_window_raise (priv->event_window);
344 gdk_window_lower (priv->event_window);
348 gboolean visible = gtk_widget_get_visible (widget);
351 gtk_widget_hide (widget);
353 gtk_widget_unrealize (widget);
355 gtk_widget_realize (widget);
358 gtk_widget_show (widget);
362 if (gtk_widget_get_visible (widget))
363 gtk_widget_queue_resize (widget);
365 g_object_notify (G_OBJECT (event_box), "above-child");
371 gtk_event_box_realize (GtkWidget *widget)
373 GtkAllocation allocation;
375 GdkWindowAttr attributes;
376 gint attributes_mask;
378 GtkEventBoxPrivate *priv;
379 gboolean visible_window;
381 gtk_widget_get_allocation (widget, &allocation);
383 gtk_widget_set_realized (widget, TRUE);
385 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
387 attributes.x = allocation.x + border_width;
388 attributes.y = allocation.y + border_width;
389 attributes.width = allocation.width - 2*border_width;
390 attributes.height = allocation.height - 2*border_width;
391 attributes.window_type = GDK_WINDOW_CHILD;
392 attributes.event_mask = gtk_widget_get_events (widget)
393 | GDK_BUTTON_MOTION_MASK
394 | GDK_BUTTON_PRESS_MASK
395 | GDK_BUTTON_RELEASE_MASK
397 | GDK_ENTER_NOTIFY_MASK
398 | GDK_LEAVE_NOTIFY_MASK;
400 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
402 visible_window = gtk_widget_get_has_window (widget);
405 attributes.visual = gtk_widget_get_visual (widget);
406 attributes.wclass = GDK_INPUT_OUTPUT;
408 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
410 window = gdk_window_new (gtk_widget_get_parent_window (widget),
411 &attributes, attributes_mask);
412 gtk_widget_set_window (widget, window);
413 gdk_window_set_user_data (window, widget);
417 window = gtk_widget_get_parent_window (widget);
418 gtk_widget_set_window (widget, window);
419 g_object_ref (window);
422 if (!visible_window || priv->above_child)
424 attributes.wclass = GDK_INPUT_ONLY;
426 attributes_mask = GDK_WA_X | GDK_WA_Y;
430 priv->event_window = gdk_window_new (window,
431 &attributes, attributes_mask);
432 gdk_window_set_user_data (priv->event_window, widget);
435 gtk_widget_style_attach (widget);
438 gtk_style_set_background (gtk_widget_get_style (widget), window, GTK_STATE_NORMAL);
442 gtk_event_box_unrealize (GtkWidget *widget)
444 GtkEventBoxPrivate *priv;
446 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
448 if (priv->event_window != NULL)
450 gdk_window_set_user_data (priv->event_window, NULL);
451 gdk_window_destroy (priv->event_window);
452 priv->event_window = NULL;
455 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unrealize (widget);
459 gtk_event_box_map (GtkWidget *widget)
461 GtkEventBoxPrivate *priv;
463 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
465 if (priv->event_window != NULL && !priv->above_child)
466 gdk_window_show (priv->event_window);
468 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->map (widget);
470 if (priv->event_window != NULL && priv->above_child)
471 gdk_window_show (priv->event_window);
475 gtk_event_box_unmap (GtkWidget *widget)
477 GtkEventBoxPrivate *priv;
479 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
481 if (priv->event_window != NULL)
482 gdk_window_hide (priv->event_window);
484 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unmap (widget);
490 gtk_event_box_size_request (GtkWidget *widget,
491 GtkRequisition *requisition)
493 GtkBin *bin = GTK_BIN (widget);
497 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
498 requisition->width = border_width * 2;
499 requisition->height = border_width * 2;
501 child = gtk_bin_get_child (bin);
502 if (child && gtk_widget_get_visible (child))
504 GtkRequisition child_requisition;
506 gtk_size_request_get_size (GTK_SIZE_REQUEST (child),
507 &child_requisition, NULL);
509 requisition->width += child_requisition.width;
510 requisition->height += child_requisition.height;
515 gtk_event_box_size_allocate (GtkWidget *widget,
516 GtkAllocation *allocation)
519 GtkAllocation child_allocation;
520 GtkEventBoxPrivate *priv;
524 bin = GTK_BIN (widget);
526 gtk_widget_set_allocation (widget, allocation);
528 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
530 if (!gtk_widget_get_has_window (widget))
532 child_allocation.x = allocation->x + border_width;
533 child_allocation.y = allocation->y + border_width;
537 child_allocation.x = 0;
538 child_allocation.y = 0;
540 child_allocation.width = MAX (allocation->width - border_width * 2, 0);
541 child_allocation.height = MAX (allocation->height - border_width * 2, 0);
543 if (gtk_widget_get_realized (widget))
545 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
547 if (priv->event_window != NULL)
548 gdk_window_move_resize (priv->event_window,
551 child_allocation.width,
552 child_allocation.height);
554 if (gtk_widget_get_has_window (widget))
555 gdk_window_move_resize (gtk_widget_get_window (widget),
556 allocation->x + border_width,
557 allocation->y + border_width,
558 child_allocation.width,
559 child_allocation.height);
562 child = gtk_bin_get_child (bin);
564 gtk_widget_size_allocate (child, &child_allocation);
568 gtk_event_box_paint (GtkWidget *widget,
571 if (!gtk_widget_get_app_paintable (widget))
572 gtk_paint_flat_box (gtk_widget_get_style (widget),
573 gtk_widget_get_window (widget),
574 gtk_widget_get_state (widget),
576 area, widget, "eventbox",
581 gtk_event_box_expose (GtkWidget *widget,
582 GdkEventExpose *event)
584 if (gtk_widget_is_drawable (widget))
586 if (gtk_widget_get_has_window (widget))
587 gtk_event_box_paint (widget, &event->area);
589 GTK_WIDGET_CLASS (gtk_event_box_parent_class)->expose_event (widget, event);