]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
gtk/gtkeventbox.c: use accessor functions to access GtkWidget
[~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 "config.h"
28 #include "gtkeventbox.h"
29 #include "gtkprivate.h"
30 #include "gtkintl.h"
31
32
33 typedef struct
34 {
35   gboolean above_child;
36   GdkWindow *event_window;
37 } GtkEventBoxPrivate;
38
39 enum {
40   PROP_0,
41   PROP_VISIBLE_WINDOW,
42   PROP_ABOVE_CHILD
43 };
44
45
46 #define GTK_EVENT_BOX_GET_PRIVATE(obj)  G_TYPE_INSTANCE_GET_PRIVATE((obj), GTK_TYPE_EVENT_BOX, GtkEventBoxPrivate)
47
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,
57                                              GdkRectangle     *area);
58 static gboolean gtk_event_box_expose        (GtkWidget        *widget,
59                                              GdkEventExpose   *event);
60 static void     gtk_event_box_set_property  (GObject          *object,
61                                              guint             prop_id,
62                                              const GValue     *value,
63                                              GParamSpec       *pspec);
64 static void     gtk_event_box_get_property  (GObject          *object,
65                                              guint             prop_id,
66                                              GValue           *value,
67                                              GParamSpec       *pspec);
68
69 G_DEFINE_TYPE (GtkEventBox, gtk_event_box, GTK_TYPE_BIN)
70
71 static void
72 gtk_event_box_class_init (GtkEventBoxClass *class)
73 {
74   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
75   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
76
77   gobject_class->set_property = gtk_event_box_set_property;
78   gobject_class->get_property = gtk_event_box_get_property;
79   
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;
87
88   g_object_class_install_property (gobject_class,
89                                    PROP_VISIBLE_WINDOW,
90                                    g_param_spec_boolean ("visible-window",
91                                                         P_("Visible Window"),
92                                                         P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
93                                                         TRUE,
94                                                         GTK_PARAM_READWRITE));
95   g_object_class_install_property (gobject_class,
96                                    PROP_ABOVE_CHILD,
97                                    g_param_spec_boolean ("above-child",
98                                                         P_("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."),
100                                                         FALSE,
101                                                         GTK_PARAM_READWRITE));
102   
103   g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
104 }
105
106 static void
107 gtk_event_box_init (GtkEventBox *event_box)
108 {
109   GtkEventBoxPrivate *priv;
110
111   gtk_widget_set_has_window (GTK_WIDGET (event_box), TRUE);
112  
113   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
114   priv->above_child = FALSE;
115 }
116
117 GtkWidget*
118 gtk_event_box_new (void)
119 {
120   return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
121 }
122
123 static void 
124 gtk_event_box_set_property (GObject      *object,
125                             guint         prop_id,
126                             const GValue *value,
127                             GParamSpec   *pspec)
128 {
129   GtkEventBox *event_box;
130   
131   event_box = GTK_EVENT_BOX (object);
132   
133   switch (prop_id)
134     {
135     case PROP_VISIBLE_WINDOW:
136       gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
137       break;      
138     case PROP_ABOVE_CHILD:
139       gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
140       break;      
141     default:
142       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
143       break;
144     }
145 }
146
147 static void 
148 gtk_event_box_get_property (GObject     *object,
149                             guint        prop_id,
150                             GValue      *value,
151                             GParamSpec  *pspec)
152 {
153   GtkEventBox *event_box;
154   
155   event_box = GTK_EVENT_BOX (object);
156   
157   switch (prop_id)
158     {
159     case PROP_VISIBLE_WINDOW:
160       g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
161       break;
162     case PROP_ABOVE_CHILD:
163       g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
164       break;
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167       break;
168     }
169 }
170
171 /**
172  * gtk_event_box_get_visible_window:
173  * @event_box: a #GtkEventBox
174  *
175  * Returns whether the event box has a visible window.
176  * See gtk_event_box_set_visible_window() for details.
177  *
178  * Return value: %TRUE if the event box window is visible
179  *
180  * Since: 2.4
181  **/
182 gboolean
183 gtk_event_box_get_visible_window (GtkEventBox *event_box)
184 {
185   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
186
187   return gtk_widget_get_has_window (GTK_WIDGET (event_box));
188 }
189
190 /**
191  * gtk_event_box_set_visible_window:
192  * @event_box: a #GtkEventBox
193  * @visible_window: boolean value
194  *
195  * Set whether the event box uses a visible or invisible child
196  * window. The default is to use visible windows.
197  *
198  * In an invisible window event box, the window that the
199  * event box creates is a %GDK_INPUT_ONLY window, which 
200  * means that it is invisible and only serves to receive
201  * events.
202  * 
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.
206  * 
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.
211  * 
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
214  * draw on it.
215  *
216  * <note><para>
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. 
225  * </para><para>
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.
229  * </para></note>
230  * 
231  * Since: 2.4
232  **/
233 void
234 gtk_event_box_set_visible_window (GtkEventBox *event_box,
235                                   gboolean visible_window)
236 {
237   GtkWidget *widget;
238
239   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
240
241   widget = GTK_WIDGET (event_box);
242
243   visible_window = visible_window != FALSE;
244
245   if (visible_window != gtk_widget_get_has_window (widget))
246     {
247       if (gtk_widget_get_realized (widget))
248         {
249           gboolean visible = gtk_widget_get_visible (widget);
250
251           if (visible)
252             gtk_widget_hide (widget);
253
254           gtk_widget_unrealize (widget);
255
256           gtk_widget_set_has_window (widget, visible_window);
257
258           gtk_widget_realize (widget);
259
260           if (visible)
261             gtk_widget_show (widget);
262         }
263       else
264         {
265           gtk_widget_set_has_window (widget, visible_window);
266         }
267
268       if (gtk_widget_get_visible (widget))
269         gtk_widget_queue_resize (widget);
270       
271       g_object_notify (G_OBJECT (event_box), "visible-window");
272     }
273 }
274
275 /**
276  * gtk_event_box_get_above_child:
277  * @event_box: a #GtkEventBox
278  *
279  * Returns whether the event box window is above or below the
280  * windows of its child. See gtk_event_box_set_above_child() for
281  * details.
282  *
283  * Return value: %TRUE if the event box window is above the window
284  * of its child.
285  *
286  * Since: 2.4
287  **/
288 gboolean
289 gtk_event_box_get_above_child (GtkEventBox *event_box)
290 {
291   GtkEventBoxPrivate *priv;
292
293   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
294
295   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
296
297   return priv->above_child;
298 }
299
300 /**
301  * gtk_event_box_set_above_child:
302  * @event_box: a #GtkEventBox
303  * @above_child: %TRUE if the event box window is above the windows of its child
304  *
305  * Set whether the event box window is positioned above the windows of its child,
306  * as opposed to below it. If the window is above, all events inside the
307  * event box will go to the event box. If the window is below, events
308  * in windows of child widgets will first got to that widget, and then
309  * to its parents.
310  *
311  * The default is to keep the window below the child.
312  * 
313  * Since: 2.4
314  **/
315 void
316 gtk_event_box_set_above_child (GtkEventBox *event_box,
317                                gboolean above_child)
318 {
319   GtkWidget *widget;
320   GtkEventBoxPrivate *priv;
321
322   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
323
324   widget = GTK_WIDGET (event_box);
325   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
326
327   above_child = above_child != FALSE;
328
329   if (priv->above_child != above_child)
330     {
331       priv->above_child = above_child;
332
333       if (gtk_widget_get_realized (widget))
334         {
335           if (!gtk_widget_get_has_window (widget))
336             {
337               if (above_child)
338                 gdk_window_raise (priv->event_window);
339               else
340                 gdk_window_lower (priv->event_window);
341             }
342           else
343             {
344               gboolean visible = gtk_widget_get_visible (widget);
345
346               if (visible)
347                 gtk_widget_hide (widget);
348               
349               gtk_widget_unrealize (widget);
350               
351               gtk_widget_realize (widget);
352               
353               if (visible)
354                 gtk_widget_show (widget);
355             }
356         }
357
358       if (gtk_widget_get_visible (widget))
359         gtk_widget_queue_resize (widget);
360       
361       g_object_notify (G_OBJECT (event_box), "above-child");
362     }
363 }
364
365
366 static void
367 gtk_event_box_realize (GtkWidget *widget)
368 {
369   GtkAllocation allocation;
370   GdkWindow *window;
371   GdkWindowAttr attributes;
372   gint attributes_mask;
373   gint border_width;
374   GtkEventBoxPrivate *priv;
375   gboolean visible_window;
376
377   gtk_widget_get_allocation (widget, &allocation);
378
379   gtk_widget_set_realized (widget, TRUE);
380
381   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
382
383   attributes.x = allocation.x + border_width;
384   attributes.y = allocation.y + border_width;
385   attributes.width = allocation.width - 2*border_width;
386   attributes.height = allocation.height - 2*border_width;
387   attributes.window_type = GDK_WINDOW_CHILD;
388   attributes.event_mask = gtk_widget_get_events (widget)
389                         | GDK_BUTTON_MOTION_MASK
390                         | GDK_BUTTON_PRESS_MASK
391                         | GDK_BUTTON_RELEASE_MASK
392                         | GDK_EXPOSURE_MASK
393                         | GDK_ENTER_NOTIFY_MASK
394                         | GDK_LEAVE_NOTIFY_MASK;
395
396   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
397
398   visible_window = gtk_widget_get_has_window (widget);
399   if (visible_window)
400     {
401       attributes.visual = gtk_widget_get_visual (widget);
402       attributes.colormap = gtk_widget_get_colormap (widget);
403       attributes.wclass = GDK_INPUT_OUTPUT;
404       
405       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
406
407       window = gdk_window_new (gtk_widget_get_parent_window (widget),
408                                &attributes, attributes_mask);
409       gtk_widget_set_window (widget, window);
410       gdk_window_set_user_data (window, widget);
411     }
412   else
413     {
414       window = gtk_widget_get_parent_window (widget);
415       gtk_widget_set_window (widget, window);
416       g_object_ref (window);
417     }
418
419   if (!visible_window || priv->above_child)
420     {
421       attributes.wclass = GDK_INPUT_ONLY;
422       if (!visible_window)
423         attributes_mask = GDK_WA_X | GDK_WA_Y;
424       else
425         attributes_mask = 0;
426
427       priv->event_window = gdk_window_new (window,
428                                            &attributes, attributes_mask);
429       gdk_window_set_user_data (priv->event_window, widget);
430     }
431
432   gtk_widget_style_attach (widget);
433
434   if (visible_window)
435     gtk_style_set_background (gtk_widget_get_style (widget), window, GTK_STATE_NORMAL);
436 }
437
438 static void
439 gtk_event_box_unrealize (GtkWidget *widget)
440 {
441   GtkEventBoxPrivate *priv;
442   
443   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
444   
445   if (priv->event_window != NULL)
446     {
447       gdk_window_set_user_data (priv->event_window, NULL);
448       gdk_window_destroy (priv->event_window);
449       priv->event_window = NULL;
450     }
451
452   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unrealize (widget);
453 }
454
455 static void
456 gtk_event_box_map (GtkWidget *widget)
457 {
458   GtkEventBoxPrivate *priv;
459
460   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
461
462   if (priv->event_window != NULL && !priv->above_child)
463     gdk_window_show (priv->event_window);
464
465   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->map (widget);
466
467   if (priv->event_window != NULL && priv->above_child)
468     gdk_window_show (priv->event_window);
469 }
470
471 static void
472 gtk_event_box_unmap (GtkWidget *widget)
473 {
474   GtkEventBoxPrivate *priv;
475
476   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
477
478   if (priv->event_window != NULL)
479     gdk_window_hide (priv->event_window);
480
481   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unmap (widget);
482 }
483
484
485
486 static void
487 gtk_event_box_size_request (GtkWidget      *widget,
488                             GtkRequisition *requisition)
489 {
490   GtkBin *bin = GTK_BIN (widget);
491   guint border_width;
492   GtkWidget *child;
493
494   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
495   requisition->width = border_width * 2;
496   requisition->height = border_width * 2;
497
498   child = gtk_bin_get_child (bin);
499   if (child && gtk_widget_get_visible (child))
500     {
501       GtkRequisition child_requisition;
502       
503       gtk_widget_size_request (child, &child_requisition);
504
505       requisition->width += child_requisition.width;
506       requisition->height += child_requisition.height;
507     }
508 }
509
510 static void
511 gtk_event_box_size_allocate (GtkWidget     *widget,
512                              GtkAllocation *allocation)
513 {
514   GtkBin *bin;
515   GtkAllocation child_allocation;
516   GtkEventBoxPrivate *priv;
517   guint border_width;
518   GtkWidget *child;
519
520   bin = GTK_BIN (widget);
521
522   gtk_widget_set_allocation (widget, allocation);
523
524   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
525   
526   if (!gtk_widget_get_has_window (widget))
527     {
528       child_allocation.x = allocation->x + border_width;
529       child_allocation.y = allocation->y + border_width;
530     }
531   else
532     {
533       child_allocation.x = 0;
534       child_allocation.y = 0;
535     }
536   child_allocation.width = MAX (allocation->width - border_width * 2, 0);
537   child_allocation.height = MAX (allocation->height - border_width * 2, 0);
538
539   if (gtk_widget_get_realized (widget))
540     {
541       priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
542
543       if (priv->event_window != NULL)
544         gdk_window_move_resize (priv->event_window,
545                                 child_allocation.x,
546                                 child_allocation.y,
547                                 child_allocation.width,
548                                 child_allocation.height);
549       
550       if (gtk_widget_get_has_window (widget))
551         gdk_window_move_resize (gtk_widget_get_window (widget),
552                                 allocation->x + border_width,
553                                 allocation->y + border_width,
554                                 child_allocation.width,
555                                 child_allocation.height);
556     }
557
558   child = gtk_bin_get_child (bin);
559   if (child)
560     gtk_widget_size_allocate (child, &child_allocation);
561 }
562
563 static void
564 gtk_event_box_paint (GtkWidget    *widget,
565                      GdkRectangle *area)
566 {
567   if (!gtk_widget_get_app_paintable (widget))
568     gtk_paint_flat_box (gtk_widget_get_style (widget),
569                         gtk_widget_get_window (widget),
570                         gtk_widget_get_state (widget),
571                         GTK_SHADOW_NONE,
572                         area, widget, "eventbox",
573                         0, 0, -1, -1);
574 }
575
576 static gboolean
577 gtk_event_box_expose (GtkWidget      *widget,
578                      GdkEventExpose *event)
579 {
580   if (gtk_widget_is_drawable (widget))
581     {
582       if (gtk_widget_get_has_window (widget))
583         gtk_event_box_paint (widget, &event->area);
584
585       GTK_WIDGET_CLASS (gtk_event_box_parent_class)->expose_event (widget, event);
586     }
587
588   return FALSE;
589 }