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