]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
The first part of the fix for #114351 (see also gdk-pixbuf/ChangeLog and
[~andy/gtk] / gtk / gtkeventbox.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "gtkeventbox.h"
28 #include "gtkintl.h"
29
30 typedef struct 
31 {
32   gboolean above_child;
33   GdkWindow *event_window;
34 } GtkEventBoxPrivate;
35
36 enum {
37   PROP_0,
38   PROP_VISIBLE_WINDOW,
39   PROP_ABOVE_CHILD
40 };
41
42
43 #define GTK_EVENT_BOX_GET_PRIVATE(obj)  G_TYPE_INSTANCE_GET_PRIVATE((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxPrivate)
44
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,
56                                          GdkRectangle     *area);
57 static gint gtk_event_box_expose        (GtkWidget        *widget,
58                                          GdkEventExpose   *event);
59 static void gtk_event_box_set_property  (GObject          *object,
60                                          guint             prop_id,
61                                          const GValue     *value,
62                                          GParamSpec       *pspec);
63 static void gtk_event_box_get_property  (GObject          *object,
64                                          guint             prop_id,
65                                          GValue           *value,
66                                          GParamSpec       *pspec);
67
68 static GtkBinClass *parent_class = NULL;
69
70 GType
71 gtk_event_box_get_type (void)
72 {
73   static GType event_box_type = 0;
74
75   if (!event_box_type)
76     {
77       static const GTypeInfo event_box_info =
78       {
79         sizeof (GtkEventBoxClass),
80         NULL,           /* base_init */
81         NULL,           /* base_finalize */
82         (GClassInitFunc) gtk_event_box_class_init,
83         NULL,           /* class_finalize */
84         NULL,           /* class_data */
85         sizeof (GtkEventBox),
86         0,              /* n_preallocs */
87         (GInstanceInitFunc) gtk_event_box_init,
88       };
89
90       event_box_type = g_type_register_static (GTK_TYPE_BIN, "GtkEventBox",
91                                                &event_box_info, 0);
92     }
93
94   return event_box_type;
95 }
96
97 static void
98 gtk_event_box_class_init (GtkEventBoxClass *class)
99 {
100   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
101   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
102
103   parent_class = g_type_class_peek_parent (class);
104
105   gobject_class->set_property = gtk_event_box_set_property;
106   gobject_class->get_property = gtk_event_box_get_property;
107   
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;
115
116   g_object_class_install_property (gobject_class,
117                                    PROP_VISIBLE_WINDOW,
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."),
121                                                         TRUE,
122                                                         G_PARAM_READWRITE));
123   g_object_class_install_property (gobject_class,
124                                    PROP_ABOVE_CHILD,
125                                    g_param_spec_boolean ("above-child",
126                                                         P_("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."),
128                                                         FALSE,
129                                                         G_PARAM_READWRITE));
130   
131   g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
132 }
133
134 static void
135 gtk_event_box_init (GtkEventBox *event_box)
136 {
137   GtkEventBoxPrivate *priv;
138
139   GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
140  
141   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
142   priv->above_child = FALSE;
143 }
144
145 GtkWidget*
146 gtk_event_box_new (void)
147 {
148   return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
149 }
150
151 static void 
152 gtk_event_box_set_property (GObject      *object,
153                             guint         prop_id,
154                             const GValue *value,
155                             GParamSpec   *pspec)
156 {
157   GtkEventBox *event_box;
158   
159   event_box = GTK_EVENT_BOX (object);
160   
161   switch (prop_id)
162     {
163     case PROP_VISIBLE_WINDOW:
164       gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
165       break;      
166     case PROP_ABOVE_CHILD:
167       gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
168       break;      
169     default:
170       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171       break;
172     }
173 }
174
175 static void 
176 gtk_event_box_get_property (GObject     *object,
177                             guint        prop_id,
178                             GValue      *value,
179                             GParamSpec  *pspec)
180 {
181   GtkEventBox *event_box;
182   
183   event_box = GTK_EVENT_BOX (object);
184   
185   switch (prop_id)
186     {
187     case PROP_VISIBLE_WINDOW:
188       g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
189       break;
190     case PROP_ABOVE_CHILD:
191       g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
192       break;
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195       break;
196     }
197 }
198
199 /**
200  * gtk_event_box_get_visible_window:
201  * @event_box: a #GtkEventBox
202  *
203  * Returns whether the event box has a visible window.
204  * See gtk_event_box_set_visible_window() for details.
205  *
206  * Return value: %TRUE if the event box window is visible
207  *
208  * Since: 2.4
209  **/
210 gboolean
211 gtk_event_box_get_visible_window (GtkEventBox *event_box)
212 {
213   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
214
215   return !GTK_WIDGET_NO_WINDOW (event_box);
216 }
217
218 /**
219  * gtk_event_box_set_visible_window:
220  * @event_box: a #GtkEventBox
221  * @visible_window: boolean value
222  *
223  * Set whether the event box uses a visible or invisible child
224  * window. The default is to use visible windows.
225  *
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
229  * events.
230  * 
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.
234  * 
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.
239  * 
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
242  * draw on it.
243  *
244  * <note><para>
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. 
253  * </para><para>
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.
257  * </para></note>
258  * 
259  * Since: 2.4
260  **/
261 void
262 gtk_event_box_set_visible_window (GtkEventBox *event_box,
263                                   gboolean visible_window)
264 {
265   GtkWidget *widget;
266   GtkEventBoxPrivate *priv;
267
268   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
269
270   widget = GTK_WIDGET (event_box);
271   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
272
273   visible_window = visible_window != FALSE;
274
275   if (visible_window != !GTK_WIDGET_NO_WINDOW (widget))
276     {
277       if (GTK_WIDGET_REALIZED (widget))
278         {
279           gboolean visible = GTK_WIDGET_VISIBLE (widget);
280
281           if (visible)
282             gtk_widget_hide (widget);
283
284           gtk_widget_unrealize (widget);
285
286           if (visible_window)
287             GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
288           else
289             GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
290           
291           gtk_widget_realize (widget);
292
293           if (visible)
294             gtk_widget_show (widget);
295         }
296       else
297         {
298           if (visible_window)
299             GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
300           else
301             GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
302         }
303
304       if (GTK_WIDGET_VISIBLE (widget))
305         gtk_widget_queue_resize (widget);
306       
307       g_object_notify (G_OBJECT (event_box), "visible-window");
308     }
309 }
310
311 /**
312  * gtk_event_box_get_above_child:
313  * @event_box: a #GtkEventBox
314  *
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
317  * details.
318  *
319  * Return value: %TRUE if the event box window is above the window
320  * of its child.
321  *
322  * Since: 2.4
323  **/
324 gboolean
325 gtk_event_box_get_above_child (GtkEventBox *event_box)
326 {
327   GtkEventBoxPrivate *priv;
328
329   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
330
331   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
332
333   return priv->above_child;
334 }
335
336 /**
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
340  *
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
345  * to its parents.
346  *
347  * The default is to keep the window below the child.
348  * 
349  * Since: 2.4
350  **/
351 void
352 gtk_event_box_set_above_child (GtkEventBox *event_box,
353                                gboolean above_child)
354 {
355   GtkWidget *widget;
356   GtkEventBoxPrivate *priv;
357
358   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
359
360   widget = GTK_WIDGET (event_box);
361   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
362
363   above_child = above_child != FALSE;
364
365   if (priv->above_child != above_child)
366     {
367       priv->above_child = above_child;
368
369       if (GTK_WIDGET_REALIZED (widget))
370         {
371           if (GTK_WIDGET_NO_WINDOW (widget))
372             {
373               if (above_child)
374                 gdk_window_raise (priv->event_window);
375               else
376                 gdk_window_lower (priv->event_window);
377             }
378           else
379             {
380               gboolean visible = GTK_WIDGET_VISIBLE (widget);
381
382               if (visible)
383                 gtk_widget_hide (widget);
384               
385               gtk_widget_unrealize (widget);
386               
387               gtk_widget_realize (widget);
388               
389               if (visible)
390                 gtk_widget_show (widget);
391             }
392         }
393
394       if (GTK_WIDGET_VISIBLE (widget))
395         gtk_widget_queue_resize (widget);
396       
397       g_object_notify (G_OBJECT (event_box), "above-child");
398     }
399 }
400
401
402
403 static void
404 gtk_event_box_realize (GtkWidget *widget)
405 {
406   GdkWindowAttr attributes;
407   gint attributes_mask;
408   gint border_width;
409   GtkEventBoxPrivate *priv;
410   gboolean visible_window;
411
412   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
413
414   border_width = GTK_CONTAINER (widget)->border_width;
415   
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
425                         | GDK_EXPOSURE_MASK
426                         | GDK_ENTER_NOTIFY_MASK
427                         | GDK_LEAVE_NOTIFY_MASK;
428
429   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
430
431   visible_window = !GTK_WIDGET_NO_WINDOW (widget);
432   if (visible_window)
433     {
434       attributes.visual = gtk_widget_get_visual (widget);
435       attributes.colormap = gtk_widget_get_colormap (widget);
436       attributes.wclass = GDK_INPUT_OUTPUT;
437       
438       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
439       
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);
443     }
444   else
445     {
446       widget->window = gtk_widget_get_parent_window (widget);
447       g_object_ref (widget->window);
448     }
449
450   if (!visible_window || priv->above_child)
451     {
452       attributes.wclass = GDK_INPUT_ONLY;
453       attributes_mask = GDK_WA_X | GDK_WA_Y;
454
455       priv->event_window = gdk_window_new (widget->window,
456                                            &attributes, attributes_mask);
457       gdk_window_set_user_data (priv->event_window, widget);
458     }
459
460
461   widget->style = gtk_style_attach (widget->style, widget->window);
462   
463   if (visible_window)
464     gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
465 }
466
467 static void
468 gtk_event_box_unrealize (GtkWidget *widget)
469 {
470   GtkEventBox *event_box;
471   GtkEventBoxPrivate *priv;
472   
473   event_box = GTK_EVENT_BOX (widget);
474   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
475   
476   if (priv->event_window != NULL)
477     {
478       gdk_window_set_user_data (priv->event_window, NULL);
479       gdk_window_destroy (priv->event_window);
480       priv->event_window = NULL;
481     }
482
483   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
484     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
485 }
486
487 static void
488 gtk_event_box_map (GtkWidget *widget)
489 {
490   GtkEventBoxPrivate *priv;
491
492   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
493
494   if (priv->event_window != NULL && !priv->above_child)
495     gdk_window_show (priv->event_window);
496   
497   (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
498
499   if (priv->event_window != NULL && priv->above_child)
500     gdk_window_show (priv->event_window);
501 }
502
503 static void
504 gtk_event_box_unmap (GtkWidget *widget)
505 {
506   GtkEventBoxPrivate *priv;
507
508   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
509
510   if (priv->event_window != NULL)
511     gdk_window_hide (priv->event_window);
512   
513   (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
514 }
515
516
517
518 static void
519 gtk_event_box_size_request (GtkWidget      *widget,
520                             GtkRequisition *requisition)
521 {
522   GtkBin *bin = GTK_BIN (widget);
523
524   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
525   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
526
527   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
528     {
529       GtkRequisition child_requisition;
530       
531       gtk_widget_size_request (bin->child, &child_requisition);
532
533       requisition->width += child_requisition.width;
534       requisition->height += child_requisition.height;
535     }
536 }
537
538 static void
539 gtk_event_box_size_allocate (GtkWidget     *widget,
540                              GtkAllocation *allocation)
541 {
542   GtkBin *bin;
543   GtkAllocation child_allocation;
544   GtkEventBoxPrivate *priv;
545   
546   widget->allocation = *allocation;
547   bin = GTK_BIN (widget);
548   
549   if (GTK_WIDGET_NO_WINDOW (widget))
550     {
551       child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
552       child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
553     }
554   else
555     {
556       child_allocation.x = 0;
557       child_allocation.y = 0;
558     }
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);
561
562   if (GTK_WIDGET_REALIZED (widget))
563     {
564       priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
565
566       if (priv->event_window != NULL)
567         gdk_window_move_resize (priv->event_window,
568                                 child_allocation.x,
569                                 child_allocation.y,
570                                 child_allocation.width,
571                                 child_allocation.height);
572       
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);
579     }
580   
581   if (bin->child)
582     gtk_widget_size_allocate (bin->child, &child_allocation);
583 }
584
585 static void
586 gtk_event_box_paint (GtkWidget    *widget,
587                      GdkRectangle *area)
588 {
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",
593                         0, 0, -1, -1);
594 }
595
596 static gint
597 gtk_event_box_expose (GtkWidget      *widget,
598                      GdkEventExpose *event)
599 {
600   if (GTK_WIDGET_DRAWABLE (widget))
601     {
602       if (!GTK_WIDGET_NO_WINDOW (widget))
603         gtk_event_box_paint (widget, &event->area);
604       
605       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
606     }
607
608   return FALSE;
609 }
610