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_class_init (GtkEventBoxClass *klass);
49 static void gtk_event_box_init (GtkEventBox *event_box);
50 static void gtk_event_box_realize (GtkWidget *widget);
51 static void gtk_event_box_unrealize (GtkWidget *widget);
52 static void gtk_event_box_map (GtkWidget *widget);
53 static void gtk_event_box_unmap (GtkWidget *widget);
54 static void gtk_event_box_size_request (GtkWidget *widget,
55 GtkRequisition *requisition);
56 static void gtk_event_box_size_allocate (GtkWidget *widget,
57 GtkAllocation *allocation);
58 static void gtk_event_box_paint (GtkWidget *widget,
60 static gint 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 static GtkBinClass *parent_class = NULL;
74 gtk_event_box_get_type (void)
76 static GType event_box_type = 0;
80 static const GTypeInfo event_box_info =
82 sizeof (GtkEventBoxClass),
84 NULL, /* base_finalize */
85 (GClassInitFunc) gtk_event_box_class_init,
86 NULL, /* class_finalize */
87 NULL, /* class_data */
90 (GInstanceInitFunc) gtk_event_box_init,
93 event_box_type = g_type_register_static (GTK_TYPE_BIN, "GtkEventBox",
97 return event_box_type;
101 gtk_event_box_class_init (GtkEventBoxClass *class)
103 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
104 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
106 parent_class = g_type_class_peek_parent (class);
108 gobject_class->set_property = gtk_event_box_set_property;
109 gobject_class->get_property = gtk_event_box_get_property;
111 widget_class->realize = gtk_event_box_realize;
112 widget_class->unrealize = gtk_event_box_unrealize;
113 widget_class->map = gtk_event_box_map;
114 widget_class->unmap = gtk_event_box_unmap;
115 widget_class->size_request = gtk_event_box_size_request;
116 widget_class->size_allocate = gtk_event_box_size_allocate;
117 widget_class->expose_event = gtk_event_box_expose;
119 g_object_class_install_property (gobject_class,
121 g_param_spec_boolean ("visible-window",
122 P_("Visible Window"),
123 P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
125 GTK_PARAM_READWRITE));
126 g_object_class_install_property (gobject_class,
128 g_param_spec_boolean ("above-child",
130 P_("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
132 GTK_PARAM_READWRITE));
134 g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
138 gtk_event_box_init (GtkEventBox *event_box)
140 GtkEventBoxPrivate *priv;
142 GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
144 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
145 priv->above_child = FALSE;
149 gtk_event_box_new (void)
151 return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
155 gtk_event_box_set_property (GObject *object,
160 GtkEventBox *event_box;
162 event_box = GTK_EVENT_BOX (object);
166 case PROP_VISIBLE_WINDOW:
167 gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
169 case PROP_ABOVE_CHILD:
170 gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
179 gtk_event_box_get_property (GObject *object,
184 GtkEventBox *event_box;
186 event_box = GTK_EVENT_BOX (object);
190 case PROP_VISIBLE_WINDOW:
191 g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
193 case PROP_ABOVE_CHILD:
194 g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 * gtk_event_box_get_visible_window:
204 * @event_box: a #GtkEventBox
206 * Returns whether the event box has a visible window.
207 * See gtk_event_box_set_visible_window() for details.
209 * Return value: %TRUE if the event box window is visible
214 gtk_event_box_get_visible_window (GtkEventBox *event_box)
216 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
218 return !GTK_WIDGET_NO_WINDOW (event_box);
222 * gtk_event_box_set_visible_window:
223 * @event_box: a #GtkEventBox
224 * @visible_window: boolean value
226 * Set whether the event box uses a visible or invisible child
227 * window. The default is to use visible windows.
229 * In an invisible window event box, the window that that the
230 * event box creates is a %GDK_INPUT_ONLY window, which
231 * means that it is invisible and only serves to receive
234 * A visible window event box creates a visible (%GDK_INPUT_OUTPUT)
235 * window that acts as the parent window for all the widgets
236 * contained in the event box.
238 * You should generally make your event box invisible if
239 * you just want to trap events. Creating a visible window
240 * may cause artifacts that are visible to the user, especially
241 * if the user is using a theme with gradients or pixmaps.
243 * The main reason to create a non input-only event box is if
244 * you want to set the background to a different color or
248 * There is one unexpected issue for an invisible event box that has its
249 * window below the child. (See gtk_event_box_set_above_child().)
250 * Since the input-only window is not an ancestor window of any windows
251 * that descendent widgets of the event box create, events on these
252 * windows aren't propagated up by the windowing system, but only by GTK+.
253 * The practical effect of this is if an event isn't in the event
254 * mask for the descendant window (see gtk_widget_add_events()),
255 * it won't be received by the event box.
257 * This problem doesn't occur for visible event boxes, because in
258 * that case, the event box window is actually the ancestor of the
259 * descendant windows, not just at the same place on the screen.
265 gtk_event_box_set_visible_window (GtkEventBox *event_box,
266 gboolean visible_window)
269 GtkEventBoxPrivate *priv;
271 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
273 widget = GTK_WIDGET (event_box);
274 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
276 visible_window = visible_window != FALSE;
278 if (visible_window != !GTK_WIDGET_NO_WINDOW (widget))
280 if (GTK_WIDGET_REALIZED (widget))
282 gboolean visible = GTK_WIDGET_VISIBLE (widget);
285 gtk_widget_hide (widget);
287 gtk_widget_unrealize (widget);
290 GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
292 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
294 gtk_widget_realize (widget);
297 gtk_widget_show (widget);
302 GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
304 GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
307 if (GTK_WIDGET_VISIBLE (widget))
308 gtk_widget_queue_resize (widget);
310 g_object_notify (G_OBJECT (event_box), "visible-window");
315 * gtk_event_box_get_above_child:
316 * @event_box: a #GtkEventBox
318 * Returns whether the event box window is above or below the
319 * windows of its child. See gtk_event_box_set_above_child() for
322 * Return value: %TRUE if the event box window is above the window
328 gtk_event_box_get_above_child (GtkEventBox *event_box)
330 GtkEventBoxPrivate *priv;
332 g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
334 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
336 return priv->above_child;
340 * gtk_event_box_set_above_child:
341 * @event_box: a #GtkEventBox
342 * @above_child: %TRUE if the event box window is above the windows of its child
344 * Set whether the event box window is positioned above the windows of its child,
345 * as opposed to below it. If the window is above, all events inside the
346 * event box will go to the event box. If the window is below, events
347 * in windows of child widgets will first got to that widget, and then
350 * The default is to keep the window below the child.
355 gtk_event_box_set_above_child (GtkEventBox *event_box,
356 gboolean above_child)
359 GtkEventBoxPrivate *priv;
361 g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
363 widget = GTK_WIDGET (event_box);
364 priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
366 above_child = above_child != FALSE;
368 if (priv->above_child != above_child)
370 priv->above_child = above_child;
372 if (GTK_WIDGET_REALIZED (widget))
374 if (GTK_WIDGET_NO_WINDOW (widget))
377 gdk_window_raise (priv->event_window);
379 gdk_window_lower (priv->event_window);
383 gboolean visible = GTK_WIDGET_VISIBLE (widget);
386 gtk_widget_hide (widget);
388 gtk_widget_unrealize (widget);
390 gtk_widget_realize (widget);
393 gtk_widget_show (widget);
397 if (GTK_WIDGET_VISIBLE (widget))
398 gtk_widget_queue_resize (widget);
400 g_object_notify (G_OBJECT (event_box), "above-child");
407 gtk_event_box_realize (GtkWidget *widget)
409 GdkWindowAttr attributes;
410 gint attributes_mask;
412 GtkEventBoxPrivate *priv;
413 gboolean visible_window;
415 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
417 border_width = GTK_CONTAINER (widget)->border_width;
419 attributes.x = widget->allocation.x + border_width;
420 attributes.y = widget->allocation.y + border_width;
421 attributes.width = widget->allocation.width - 2*border_width;
422 attributes.height = widget->allocation.height - 2*border_width;
423 attributes.window_type = GDK_WINDOW_CHILD;
424 attributes.event_mask = gtk_widget_get_events (widget)
425 | GDK_BUTTON_MOTION_MASK
426 | GDK_BUTTON_PRESS_MASK
427 | GDK_BUTTON_RELEASE_MASK
429 | GDK_ENTER_NOTIFY_MASK
430 | GDK_LEAVE_NOTIFY_MASK;
432 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
434 visible_window = !GTK_WIDGET_NO_WINDOW (widget);
437 attributes.visual = gtk_widget_get_visual (widget);
438 attributes.colormap = gtk_widget_get_colormap (widget);
439 attributes.wclass = GDK_INPUT_OUTPUT;
441 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
443 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
444 &attributes, attributes_mask);
445 gdk_window_set_user_data (widget->window, widget);
449 widget->window = gtk_widget_get_parent_window (widget);
450 g_object_ref (widget->window);
453 if (!visible_window || priv->above_child)
455 attributes.wclass = GDK_INPUT_ONLY;
456 attributes_mask = GDK_WA_X | GDK_WA_Y;
458 priv->event_window = gdk_window_new (widget->window,
459 &attributes, attributes_mask);
460 gdk_window_set_user_data (priv->event_window, widget);
464 widget->style = gtk_style_attach (widget->style, widget->window);
467 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
471 gtk_event_box_unrealize (GtkWidget *widget)
473 GtkEventBox *event_box;
474 GtkEventBoxPrivate *priv;
476 event_box = GTK_EVENT_BOX (widget);
477 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
479 if (priv->event_window != NULL)
481 gdk_window_set_user_data (priv->event_window, NULL);
482 gdk_window_destroy (priv->event_window);
483 priv->event_window = NULL;
486 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
487 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
491 gtk_event_box_map (GtkWidget *widget)
493 GtkEventBoxPrivate *priv;
495 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
497 if (priv->event_window != NULL && !priv->above_child)
498 gdk_window_show (priv->event_window);
500 (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
502 if (priv->event_window != NULL && priv->above_child)
503 gdk_window_show (priv->event_window);
507 gtk_event_box_unmap (GtkWidget *widget)
509 GtkEventBoxPrivate *priv;
511 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
513 if (priv->event_window != NULL)
514 gdk_window_hide (priv->event_window);
516 (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
522 gtk_event_box_size_request (GtkWidget *widget,
523 GtkRequisition *requisition)
525 GtkBin *bin = GTK_BIN (widget);
527 requisition->width = GTK_CONTAINER (widget)->border_width * 2;
528 requisition->height = GTK_CONTAINER (widget)->border_width * 2;
530 if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
532 GtkRequisition child_requisition;
534 gtk_widget_size_request (bin->child, &child_requisition);
536 requisition->width += child_requisition.width;
537 requisition->height += child_requisition.height;
542 gtk_event_box_size_allocate (GtkWidget *widget,
543 GtkAllocation *allocation)
546 GtkAllocation child_allocation;
547 GtkEventBoxPrivate *priv;
549 widget->allocation = *allocation;
550 bin = GTK_BIN (widget);
552 if (GTK_WIDGET_NO_WINDOW (widget))
554 child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
555 child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
559 child_allocation.x = 0;
560 child_allocation.y = 0;
562 child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
563 child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
565 if (GTK_WIDGET_REALIZED (widget))
567 priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
569 if (priv->event_window != NULL)
570 gdk_window_move_resize (priv->event_window,
573 child_allocation.width,
574 child_allocation.height);
576 if (!GTK_WIDGET_NO_WINDOW (widget))
577 gdk_window_move_resize (widget->window,
578 allocation->x + GTK_CONTAINER (widget)->border_width,
579 allocation->y + GTK_CONTAINER (widget)->border_width,
580 child_allocation.width,
581 child_allocation.height);
585 gtk_widget_size_allocate (bin->child, &child_allocation);
589 gtk_event_box_paint (GtkWidget *widget,
592 if (!GTK_WIDGET_APP_PAINTABLE (widget))
593 gtk_paint_flat_box (widget->style, widget->window,
594 widget->state, GTK_SHADOW_NONE,
595 area, widget, "eventbox",
600 gtk_event_box_expose (GtkWidget *widget,
601 GdkEventExpose *event)
603 if (GTK_WIDGET_DRAWABLE (widget))
605 if (!GTK_WIDGET_NO_WINDOW (widget))
606 gtk_event_box_paint (widget, &event->area);
608 (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
614 #define __GTK_EVENT_BOX_C__
615 #include "gtkaliasdef.c"