]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
Implement gtk_event_box_get/set_input_only()
[~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                                                         _("Visible Window"),
120                                                         _(" Whether the event box is visible, as opposed to invisible and only used to trap events."),
121                                                         FALSE,
122                                                         G_PARAM_READWRITE));
123   g_object_class_install_property (gobject_class,
124                                    PROP_ABOVE_CHILD,
125                                    g_param_spec_boolean ("above-child",
126                                                         _("Above child"),
127                                                         _("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>
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  *
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  * </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       if (GTK_WIDGET_REALIZED (widget))
368         {
369           if (GTK_WIDGET_NO_WINDOW (widget))
370             {
371               if (above_child)
372                 gdk_window_raise (priv->event_window);
373               else
374                 gdk_window_lower (priv->event_window);
375             }
376           else
377             {
378               gboolean visible = GTK_WIDGET_VISIBLE (widget);
379
380               if (visible)
381                 gtk_widget_hide (widget);
382               
383               gtk_widget_unrealize (widget);
384               priv->above_child = above_child;
385               
386               gtk_widget_realize (widget);
387               
388               if (visible)
389                 gtk_widget_show (widget);
390             }
391         }
392       else
393         priv->above_child = above_child;
394
395       if (GTK_WIDGET_VISIBLE (widget))
396         gtk_widget_queue_resize (widget);
397       
398       g_object_notify (G_OBJECT (event_box), "above-child");
399     }
400 }
401
402
403
404 static void
405 gtk_event_box_realize (GtkWidget *widget)
406 {
407   GdkWindowAttr attributes;
408   gint attributes_mask;
409   gint border_width;
410   GtkEventBoxPrivate *priv;
411   gboolean visible_window;
412
413   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
414
415   border_width = GTK_CONTAINER (widget)->border_width;
416   
417   attributes.x = widget->allocation.x + border_width;
418   attributes.y = widget->allocation.y + border_width;
419   attributes.width = widget->allocation.width - 2*border_width;
420   attributes.height = widget->allocation.height - 2*border_width;
421   attributes.window_type = GDK_WINDOW_CHILD;
422   attributes.event_mask = gtk_widget_get_events (widget)
423                         | GDK_BUTTON_MOTION_MASK
424                         | GDK_BUTTON_PRESS_MASK
425                         | GDK_BUTTON_RELEASE_MASK
426                         | GDK_EXPOSURE_MASK
427                         | GDK_ENTER_NOTIFY_MASK
428                         | GDK_LEAVE_NOTIFY_MASK;
429
430   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
431
432   visible_window = !GTK_WIDGET_NO_WINDOW (widget);
433   if (visible_window)
434     {
435       attributes.visual = gtk_widget_get_visual (widget);
436       attributes.colormap = gtk_widget_get_colormap (widget);
437       attributes.wclass = GDK_INPUT_OUTPUT;
438       
439       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
440       
441       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
442                                        &attributes, attributes_mask);
443       gdk_window_set_user_data (widget->window, widget);
444     }
445   else
446     {
447       widget->window = gtk_widget_get_parent_window (widget);
448       g_object_ref (widget->window);
449     }
450
451   if (!visible_window || priv->above_child)
452     {
453       attributes.wclass = GDK_INPUT_ONLY;
454       attributes_mask = GDK_WA_X | GDK_WA_Y;
455
456       priv->event_window = gdk_window_new (widget->window,
457                                            &attributes, attributes_mask);
458       gdk_window_set_user_data (priv->event_window, widget);
459     }
460
461
462   widget->style = gtk_style_attach (widget->style, widget->window);
463   
464   if (visible_window)
465     gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
466 }
467
468 static void
469 gtk_event_box_unrealize (GtkWidget *widget)
470 {
471   GtkEventBox *event_box;
472   GtkEventBoxPrivate *priv;
473   
474   event_box = GTK_EVENT_BOX (widget);
475   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
476   
477   if (priv->event_window != NULL)
478     {
479       gdk_window_set_user_data (priv->event_window, NULL);
480       gdk_window_destroy (priv->event_window);
481       priv->event_window = NULL;
482     }
483
484   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
485     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
486 }
487
488 static void
489 gtk_event_box_map (GtkWidget *widget)
490 {
491   GtkEventBoxPrivate *priv;
492
493   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
494
495   if (priv->event_window != NULL && !priv->above_child)
496     gdk_window_show (priv->event_window);
497   
498   (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
499
500   if (priv->event_window != NULL && priv->above_child)
501     gdk_window_show (priv->event_window);
502 }
503
504 static void
505 gtk_event_box_unmap (GtkWidget *widget)
506 {
507   GtkEventBoxPrivate *priv;
508
509   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
510
511   if (priv->event_window != NULL)
512     gdk_window_hide (priv->event_window);
513   
514   (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
515 }
516
517
518
519 static void
520 gtk_event_box_size_request (GtkWidget      *widget,
521                             GtkRequisition *requisition)
522 {
523   GtkBin *bin = GTK_BIN (widget);
524
525   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
526   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
527
528   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
529     {
530       GtkRequisition child_requisition;
531       
532       gtk_widget_size_request (bin->child, &child_requisition);
533
534       requisition->width += child_requisition.width;
535       requisition->height += child_requisition.height;
536     }
537 }
538
539 static void
540 gtk_event_box_size_allocate (GtkWidget     *widget,
541                              GtkAllocation *allocation)
542 {
543   GtkBin *bin;
544   GtkAllocation child_allocation;
545   GtkEventBoxPrivate *priv;
546   
547   widget->allocation = *allocation;
548   bin = GTK_BIN (widget);
549   
550   if (GTK_WIDGET_NO_WINDOW (widget))
551     {
552       child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
553       child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
554     }
555   else
556     {
557       child_allocation.x = 0;
558       child_allocation.y = 0;
559     }
560   child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
561   child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
562
563   if (GTK_WIDGET_REALIZED (widget))
564     {
565       priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
566
567       if (priv->event_window != NULL)
568         gdk_window_move_resize (priv->event_window,
569                                 child_allocation.x,
570                                 child_allocation.y,
571                                 child_allocation.width,
572                                 child_allocation.height);
573       
574       if (!GTK_WIDGET_NO_WINDOW (widget))
575         gdk_window_move_resize (widget->window,
576                                 allocation->x + GTK_CONTAINER (widget)->border_width,
577                                 allocation->y + GTK_CONTAINER (widget)->border_width,
578                                 child_allocation.width,
579                                 child_allocation.height);
580     }
581   
582   if (bin->child)
583     gtk_widget_size_allocate (bin->child, &child_allocation);
584 }
585
586 static void
587 gtk_event_box_paint (GtkWidget    *widget,
588                      GdkRectangle *area)
589 {
590   if (!GTK_WIDGET_APP_PAINTABLE (widget))
591     gtk_paint_flat_box (widget->style, widget->window,
592                         widget->state, GTK_SHADOW_NONE,
593                         area, widget, "eventbox",
594                         0, 0, -1, -1);
595 }
596
597 static gint
598 gtk_event_box_expose (GtkWidget      *widget,
599                      GdkEventExpose *event)
600 {
601   if (GTK_WIDGET_DRAWABLE (widget))
602     {
603       if (!GTK_WIDGET_NO_WINDOW (widget))
604         gtk_event_box_paint (widget, &event->area);
605       
606       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
607     }
608
609   return FALSE;
610 }
611