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/.
28 #include "gtkeventbox.h"
29 #include "gtkprivate.h"
36 GdkWindow *event_window;
46 #define GTK_EVENT_BOX_GET_PRIVATE(obj) G_TYPE_INSTANCE_GET_PRIVATE((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxPrivate)
48 static void gtk_event_box_realize (GtkWidget *widget);
49 static void gtk_event_box_unrealize (GtkWidget *widget);
50 static void gtk_event_box_map (GtkWidget *widget);
51 static void gtk_event_box_unmap (GtkWidget *widget);
52 static void gtk_event_box_size_request (GtkWidget *widget,
53 GtkRequisition *requisition);
54 static void gtk_event_box_size_allocate (GtkWidget *widget,
55 GtkAllocation *allocation);
56 static void gtk_event_box_paint (GtkWidget *widget,
58 static gint gtk_event_box_expose (GtkWidget *widget,
59 GdkEventExpose *event);
60 static void gtk_event_box_set_property (GObject *object,
64 static void gtk_event_box_get_property (GObject *object,
69 G_DEFINE_TYPE (GtkEventBox, gtk_event_box, GTK_TYPE_BIN);
72 gtk_event_box_class_init (GtkEventBoxClass *class)
74 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
75 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
77 gobject_class->set_property = gtk_event_box_set_property;
78 gobject_class->get_property = gtk_event_box_get_property;
80 widget_class->realize = gtk_event_box_realize;
81 widget_class->unrealize = gtk_event_box_unrealize;
82 widget_class->map = gtk_event_box_map;
83 widget_class->unmap = gtk_event_box_unmap;
84 widget_class->size_request = gtk_event_box_size_request;
85 widget_class->size_allocate = gtk_event_box_size_allocate;
86 widget_class->expose_event = gtk_event_box_expose;
88 g_object_class_install_property (gobject_class,
90 g_param_spec_boolean ("visible-window",
92 P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
94 GTK_PARAM_READWRITE));
95 g_object_class_install_property (gobject_class,
97 g_param_spec_boolean ("above-child",
99 P_("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
101 GTK_PARAM_READWRITE));
103 g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
107 gtk_event_box_init (GtkEventBox *event_box)
109 GtkEventBoxPrivate *priv;
111 GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
113 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
114 priv->above_child = FALSE;
118 gtk_event_box_new (void)
120 return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
124 gtk_event_box_set_property (GObject *object,
129 GtkEventBox *event_box;
131 event_box = GTK_EVENT_BOX (object);
135 case PROP_VISIBLE_WINDOW:
136 gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
138 case PROP_ABOVE_CHILD:
139 gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
142 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
148 gtk_event_box_get_property (GObject *object,
153 GtkEventBox *event_box;
155 event_box = GTK_EVENT_BOX (object);
159 case PROP_VISIBLE_WINDOW:
160 g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
162 case PROP_ABOVE_CHILD:
163 g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 * gtk_event_box_get_visible_window:
173 * @event_box: a #GtkEventBox
175 * Returns whether the event box has a visible window.
176 * See gtk_event_box_set_visible_window() for details.
178 * Return value: %TRUE if the event box window is visible
183 gtk_event_box_get_visible_window (GtkEventBox *event_box)
185 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
187 return !GTK_WIDGET_NO_WINDOW (event_box);
191 * gtk_event_box_set_visible_window:
192 * @event_box: a #GtkEventBox
193 * @visible_window: boolean value
195 * Set whether the event box uses a visible or invisible child
196 * window. The default is to use visible windows.
198 * In an invisible window event box, the window that that the
199 * event box creates is a %GDK_INPUT_ONLY window, which
200 * means that it is invisible and only serves to receive
203 * A visible window event box creates a visible (%GDK_INPUT_OUTPUT)
204 * window that acts as the parent window for all the widgets
205 * contained in the event box.
207 * You should generally make your event box invisible if
208 * you just want to trap events. Creating a visible window
209 * may cause artifacts that are visible to the user, especially
210 * if the user is using a theme with gradients or pixmaps.
212 * The main reason to create a non input-only event box is if
213 * you want to set the background to a different color or
217 * There is one unexpected issue for an invisible event box that has its
218 * window below the child. (See gtk_event_box_set_above_child().)
219 * Since the input-only window is not an ancestor window of any windows
220 * that descendent widgets of the event box create, events on these
221 * windows aren't propagated up by the windowing system, but only by GTK+.
222 * The practical effect of this is if an event isn't in the event
223 * mask for the descendant window (see gtk_widget_add_events()),
224 * it won't be received by the event box.
226 * This problem doesn't occur for visible event boxes, because in
227 * that case, the event box window is actually the ancestor of the
228 * descendant windows, not just at the same place on the screen.
234 gtk_event_box_set_visible_window (GtkEventBox *event_box,
235 gboolean visible_window)
239 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
241 widget = GTK_WIDGET (event_box);
243 visible_window = visible_window != FALSE;
245 if (visible_window != !GTK_WIDGET_NO_WINDOW (widget))
247 if (GTK_WIDGET_REALIZED (widget))
249 gboolean visible = GTK_WIDGET_VISIBLE (widget);
252 gtk_widget_hide (widget);
254 gtk_widget_unrealize (widget);
257 GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
259 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
261 gtk_widget_realize (widget);
264 gtk_widget_show (widget);
269 GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
271 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
274 if (GTK_WIDGET_VISIBLE (widget))
275 gtk_widget_queue_resize (widget);
277 g_object_notify (G_OBJECT (event_box), "visible-window");
282 * gtk_event_box_get_above_child:
283 * @event_box: a #GtkEventBox
285 * Returns whether the event box window is above or below the
286 * windows of its child. See gtk_event_box_set_above_child() for
289 * Return value: %TRUE if the event box window is above the window
295 gtk_event_box_get_above_child (GtkEventBox *event_box)
297 GtkEventBoxPrivate *priv;
299 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
301 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
303 return priv->above_child;
307 * gtk_event_box_set_above_child:
308 * @event_box: a #GtkEventBox
309 * @above_child: %TRUE if the event box window is above the windows of its child
311 * Set whether the event box window is positioned above the windows of its child,
312 * as opposed to below it. If the window is above, all events inside the
313 * event box will go to the event box. If the window is below, events
314 * in windows of child widgets will first got to that widget, and then
317 * The default is to keep the window below the child.
322 gtk_event_box_set_above_child (GtkEventBox *event_box,
323 gboolean above_child)
326 GtkEventBoxPrivate *priv;
328 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
330 widget = GTK_WIDGET (event_box);
331 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
333 above_child = above_child != FALSE;
335 if (priv->above_child != above_child)
337 priv->above_child = above_child;
339 if (GTK_WIDGET_REALIZED (widget))
341 if (GTK_WIDGET_NO_WINDOW (widget))
344 gdk_window_raise (priv->event_window);
346 gdk_window_lower (priv->event_window);
350 gboolean visible = GTK_WIDGET_VISIBLE (widget);
353 gtk_widget_hide (widget);
355 gtk_widget_unrealize (widget);
357 gtk_widget_realize (widget);
360 gtk_widget_show (widget);
364 if (GTK_WIDGET_VISIBLE (widget))
365 gtk_widget_queue_resize (widget);
367 g_object_notify (G_OBJECT (event_box), "above-child");
374 gtk_event_box_realize (GtkWidget *widget)
376 GdkWindowAttr attributes;
377 gint attributes_mask;
379 GtkEventBoxPrivate *priv;
380 gboolean visible_window;
382 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
384 border_width = GTK_CONTAINER (widget)->border_width;
386 attributes.x = widget->allocation.x + border_width;
387 attributes.y = widget->allocation.y + border_width;
388 attributes.width = widget->allocation.width - 2*border_width;
389 attributes.height = widget->allocation.height - 2*border_width;
390 attributes.window_type = GDK_WINDOW_CHILD;
391 attributes.event_mask = gtk_widget_get_events (widget)
392 | GDK_BUTTON_MOTION_MASK
393 | GDK_BUTTON_PRESS_MASK
394 | GDK_BUTTON_RELEASE_MASK
396 | GDK_ENTER_NOTIFY_MASK
397 | GDK_LEAVE_NOTIFY_MASK;
399 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
401 visible_window = !GTK_WIDGET_NO_WINDOW (widget);
404 attributes.visual = gtk_widget_get_visual (widget);
405 attributes.colormap = gtk_widget_get_colormap (widget);
406 attributes.wclass = GDK_INPUT_OUTPUT;
408 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
410 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
411 &attributes, attributes_mask);
412 gdk_window_set_user_data (widget->window, widget);
416 widget->window = gtk_widget_get_parent_window (widget);
417 g_object_ref (widget->window);
420 if (!visible_window || priv->above_child)
422 attributes.wclass = GDK_INPUT_ONLY;
423 attributes_mask = GDK_WA_X | GDK_WA_Y;
425 priv->event_window = gdk_window_new (widget->window,
426 &attributes, attributes_mask);
427 gdk_window_set_user_data (priv->event_window, widget);
431 widget->style = gtk_style_attach (widget->style, widget->window);
434 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
438 gtk_event_box_unrealize (GtkWidget *widget)
440 GtkEventBoxPrivate *priv;
442 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
444 if (priv->event_window != NULL)
446 gdk_window_set_user_data (priv->event_window, NULL);
447 gdk_window_destroy (priv->event_window);
448 priv->event_window = NULL;
451 if (GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unrealize)
452 (* GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unrealize) (widget);
456 gtk_event_box_map (GtkWidget *widget)
458 GtkEventBoxPrivate *priv;
460 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
462 if (priv->event_window != NULL && !priv->above_child)
463 gdk_window_show (priv->event_window);
465 (* GTK_WIDGET_CLASS (gtk_event_box_parent_class)->map) (widget);
467 if (priv->event_window != NULL && priv->above_child)
468 gdk_window_show (priv->event_window);
472 gtk_event_box_unmap (GtkWidget *widget)
474 GtkEventBoxPrivate *priv;
476 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
478 if (priv->event_window != NULL)
479 gdk_window_hide (priv->event_window);
481 (* GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unmap) (widget);
487 gtk_event_box_size_request (GtkWidget *widget,
488 GtkRequisition *requisition)
490 GtkBin *bin = GTK_BIN (widget);
492 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
493 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
495 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
497 GtkRequisition child_requisition;
499 gtk_widget_size_request (bin->child, &child_requisition);
501 requisition->width += child_requisition.width;
502 requisition->height += child_requisition.height;
507 gtk_event_box_size_allocate (GtkWidget *widget,
508 GtkAllocation *allocation)
511 GtkAllocation child_allocation;
512 GtkEventBoxPrivate *priv;
514 widget->allocation = *allocation;
515 bin = GTK_BIN (widget);
517 if (GTK_WIDGET_NO_WINDOW (widget))
519 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
520 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
524 child_allocation.x = 0;
525 child_allocation.y = 0;
527 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
528 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
530 if (GTK_WIDGET_REALIZED (widget))
532 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
534 if (priv->event_window != NULL)
535 gdk_window_move_resize (priv->event_window,
538 child_allocation.width,
539 child_allocation.height);
541 if (!GTK_WIDGET_NO_WINDOW (widget))
542 gdk_window_move_resize (widget->window,
543 allocation->x + GTK_CONTAINER (widget)->border_width,
544 allocation->y + GTK_CONTAINER (widget)->border_width,
545 child_allocation.width,
546 child_allocation.height);
550 gtk_widget_size_allocate (bin->child, &child_allocation);
554 gtk_event_box_paint (GtkWidget *widget,
557 if (!GTK_WIDGET_APP_PAINTABLE (widget))
558 gtk_paint_flat_box (widget->style, widget->window,
559 widget->state, GTK_SHADOW_NONE,
560 area, widget, "eventbox",
565 gtk_event_box_expose (GtkWidget *widget,
566 GdkEventExpose *event)
568 if (GTK_WIDGET_DRAWABLE (widget))
570 if (!GTK_WIDGET_NO_WINDOW (widget))
571 gtk_event_box_paint (widget, &event->area);
573 (* GTK_WIDGET_CLASS (gtk_event_box_parent_class)->expose_event) (widget, event);
579 #define __GTK_EVENT_BOX_C__
580 #include "gtkaliasdef.c"