]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
stylecontext: Do invalidation on first resize container
[~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, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23  */
24
25 #include "config.h"
26
27 #include "gtkeventbox.h"
28
29 #include "gtksizerequest.h"
30
31 #include "gtkprivate.h"
32 #include "gtkintl.h"
33
34
35 /**
36  * SECTION:gtkeventbox
37  * @Short_description: A widget used to catch events for widgets which
38  *     do not have their own window
39  * @Title: GtkEventBox
40  *
41  * The #GtkEventBox widget is a subclass of #GtkBin which also has its
42  * own window. It is useful since it allows you to catch events for widgets
43  * which do not have their own window.
44  */
45
46 struct _GtkEventBoxPrivate
47 {
48   gboolean above_child;
49   GdkWindow *event_window;
50 };
51
52 enum {
53   PROP_0,
54   PROP_VISIBLE_WINDOW,
55   PROP_ABOVE_CHILD
56 };
57
58 static void     gtk_event_box_realize       (GtkWidget        *widget);
59 static void     gtk_event_box_unrealize     (GtkWidget        *widget);
60 static void     gtk_event_box_map           (GtkWidget        *widget);
61 static void     gtk_event_box_unmap         (GtkWidget        *widget);
62 static void     gtk_event_box_get_preferred_width  (GtkWidget *widget,
63                                                     gint      *minimum,
64                                                     gint      *natural);
65 static void     gtk_event_box_get_preferred_height (GtkWidget *widget,
66                                                     gint      *minimum,
67                                                     gint      *natural);
68 static void     gtk_event_box_size_allocate (GtkWidget        *widget,
69                                              GtkAllocation    *allocation);
70 static gboolean gtk_event_box_draw          (GtkWidget        *widget,
71                                              cairo_t          *cr);
72 static void     gtk_event_box_set_property  (GObject          *object,
73                                              guint             prop_id,
74                                              const GValue     *value,
75                                              GParamSpec       *pspec);
76 static void     gtk_event_box_get_property  (GObject          *object,
77                                              guint             prop_id,
78                                              GValue           *value,
79                                              GParamSpec       *pspec);
80
81 G_DEFINE_TYPE (GtkEventBox, gtk_event_box, GTK_TYPE_BIN)
82
83 static void
84 gtk_event_box_class_init (GtkEventBoxClass *class)
85 {
86   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
87   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
88   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
89
90   gobject_class->set_property = gtk_event_box_set_property;
91   gobject_class->get_property = gtk_event_box_get_property;
92
93   widget_class->realize = gtk_event_box_realize;
94   widget_class->unrealize = gtk_event_box_unrealize;
95   widget_class->map = gtk_event_box_map;
96   widget_class->unmap = gtk_event_box_unmap;
97   widget_class->get_preferred_width = gtk_event_box_get_preferred_width;
98   widget_class->get_preferred_height = gtk_event_box_get_preferred_height;
99   widget_class->size_allocate = gtk_event_box_size_allocate;
100   widget_class->draw = gtk_event_box_draw;
101
102   gtk_container_class_handle_border_width (container_class);
103
104   g_object_class_install_property (gobject_class,
105                                    PROP_VISIBLE_WINDOW,
106                                    g_param_spec_boolean ("visible-window",
107                                                         P_("Visible Window"),
108                                                         P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
109                                                         TRUE,
110                                                         GTK_PARAM_READWRITE));
111   g_object_class_install_property (gobject_class,
112                                    PROP_ABOVE_CHILD,
113                                    g_param_spec_boolean ("above-child",
114                                                         P_("Above child"),
115                                                         P_("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
116                                                         FALSE,
117                                                         GTK_PARAM_READWRITE));
118
119   g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
120 }
121
122 static void
123 gtk_event_box_init (GtkEventBox *event_box)
124 {
125   GtkEventBoxPrivate *priv;
126
127   gtk_widget_set_has_window (GTK_WIDGET (event_box), TRUE);
128
129   priv = G_TYPE_INSTANCE_GET_PRIVATE (event_box,
130                                       GTK_TYPE_EVENT_BOX,
131                                       GtkEventBoxPrivate);
132
133   event_box->priv = priv;
134   priv->above_child = FALSE;
135 }
136
137 /**
138  * gtk_event_box_new:
139  *
140  * Creates a new #GtkEventBox.
141  *
142  * Returns: a new #GtkEventBox
143  */
144 GtkWidget*
145 gtk_event_box_new (void)
146 {
147   return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
148 }
149
150 static void
151 gtk_event_box_set_property (GObject      *object,
152                             guint         prop_id,
153                             const GValue *value,
154                             GParamSpec   *pspec)
155 {
156   GtkEventBox *event_box;
157
158   event_box = GTK_EVENT_BOX (object);
159
160   switch (prop_id)
161     {
162     case PROP_VISIBLE_WINDOW:
163       gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
164       break;
165
166     case PROP_ABOVE_CHILD:
167       gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
168       break;
169
170     default:
171       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172       break;
173     }
174 }
175
176 static void
177 gtk_event_box_get_property (GObject     *object,
178                             guint        prop_id,
179                             GValue      *value,
180                             GParamSpec  *pspec)
181 {
182   GtkEventBox *event_box;
183
184   event_box = GTK_EVENT_BOX (object);
185
186   switch (prop_id)
187     {
188     case PROP_VISIBLE_WINDOW:
189       g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
190       break;
191
192     case PROP_ABOVE_CHILD:
193       g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
194       break;
195
196     default:
197       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
198       break;
199     }
200 }
201
202 /**
203  * gtk_event_box_get_visible_window:
204  * @event_box: a #GtkEventBox
205  *
206  * Returns whether the event box has a visible window.
207  * See gtk_event_box_set_visible_window() for details.
208  *
209  * Return value: %TRUE if the event box window is visible
210  *
211  * Since: 2.4
212  */
213 gboolean
214 gtk_event_box_get_visible_window (GtkEventBox *event_box)
215 {
216   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
217
218   return gtk_widget_get_has_window (GTK_WIDGET (event_box));
219 }
220
221 /**
222  * gtk_event_box_set_visible_window:
223  * @event_box: a #GtkEventBox
224  * @visible_window: %TRUE to make the event box have a visible window
225  *
226  * Set whether the event box uses a visible or invisible child
227  * window. The default is to use visible windows.
228  *
229  * In an invisible window event box, the window that the
230  * event box creates is a %GDK_INPUT_ONLY window, which
231  * means that it is invisible and only serves to receive
232  * events.
233  *
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.
237  *
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.
242  *
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
245  * draw on it.
246  *
247  * <note><para>
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.
256  * </para><para>
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.
260  * </para></note>
261  *
262  * Since: 2.4
263  */
264 void
265 gtk_event_box_set_visible_window (GtkEventBox *event_box,
266                                   gboolean     visible_window)
267 {
268   GtkWidget *widget;
269
270   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
271
272   widget = GTK_WIDGET (event_box);
273
274   visible_window = visible_window != FALSE;
275
276   if (visible_window != gtk_widget_get_has_window (widget))
277     {
278       if (gtk_widget_get_realized (widget))
279         {
280           gboolean visible = gtk_widget_get_visible (widget);
281
282           if (visible)
283             gtk_widget_hide (widget);
284
285           gtk_widget_unrealize (widget);
286
287           gtk_widget_set_has_window (widget, visible_window);
288
289           gtk_widget_realize (widget);
290
291           if (visible)
292             gtk_widget_show (widget);
293         }
294       else
295         {
296           gtk_widget_set_has_window (widget, visible_window);
297         }
298
299       if (gtk_widget_get_visible (widget))
300         gtk_widget_queue_resize (widget);
301
302       g_object_notify (G_OBJECT (event_box), "visible-window");
303     }
304 }
305
306 /**
307  * gtk_event_box_get_above_child:
308  * @event_box: a #GtkEventBox
309  *
310  * Returns whether the event box window is above or below the
311  * windows of its child. See gtk_event_box_set_above_child()
312  * for details.
313  *
314  * Return value: %TRUE if the event box window is above the
315  *     window of its child
316  *
317  * Since: 2.4
318  */
319 gboolean
320 gtk_event_box_get_above_child (GtkEventBox *event_box)
321 {
322   GtkEventBoxPrivate *priv = event_box->priv;
323
324   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
325
326   return priv->above_child;
327 }
328
329 /**
330  * gtk_event_box_set_above_child:
331  * @event_box: a #GtkEventBox
332  * @above_child: %TRUE if the event box window is above its child
333  *
334  * Set whether the event box window is positioned above the windows
335  * of its child, as opposed to below it. If the window is above, all
336  * events inside the event box will go to the event box. If the window
337  * is below, events in windows of child widgets will first got to that
338  * widget, and then to its parents.
339  *
340  * The default is to keep the window below the child.
341  *
342  * Since: 2.4
343  */
344 void
345 gtk_event_box_set_above_child (GtkEventBox *event_box,
346                                gboolean     above_child)
347 {
348   GtkEventBoxPrivate *priv = event_box->priv;
349   GtkWidget *widget;
350
351   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
352
353   widget = GTK_WIDGET (event_box);
354
355   above_child = above_child != FALSE;
356
357   if (priv->above_child != above_child)
358     {
359       priv->above_child = above_child;
360
361       if (gtk_widget_get_realized (widget))
362         {
363           if (!gtk_widget_get_has_window (widget))
364             {
365               if (above_child)
366                 gdk_window_raise (priv->event_window);
367               else
368                 gdk_window_lower (priv->event_window);
369             }
370           else
371             {
372               gboolean visible = gtk_widget_get_visible (widget);
373
374               if (visible)
375                 gtk_widget_hide (widget);
376
377               gtk_widget_unrealize (widget);
378               gtk_widget_realize (widget);
379
380               if (visible)
381                 gtk_widget_show (widget);
382             }
383         }
384
385       if (gtk_widget_get_visible (widget))
386         gtk_widget_queue_resize (widget);
387
388       g_object_notify (G_OBJECT (event_box), "above-child");
389     }
390 }
391
392 static void
393 gtk_event_box_realize (GtkWidget *widget)
394 {
395   GtkEventBoxPrivate *priv;
396   GtkAllocation allocation;
397   GdkWindow *window;
398   GdkWindowAttr attributes;
399   gint attributes_mask;
400   gboolean visible_window;
401
402   gtk_widget_get_allocation (widget, &allocation);
403
404   gtk_widget_set_realized (widget, TRUE);
405
406   attributes.x = allocation.x;
407   attributes.y = allocation.y;
408   attributes.width = allocation.width;
409   attributes.height = allocation.height;
410   attributes.window_type = GDK_WINDOW_CHILD;
411   attributes.event_mask = gtk_widget_get_events (widget)
412                         | GDK_BUTTON_MOTION_MASK
413                         | GDK_BUTTON_PRESS_MASK
414                         | GDK_BUTTON_RELEASE_MASK
415                         | GDK_EXPOSURE_MASK
416                         | GDK_ENTER_NOTIFY_MASK
417                         | GDK_LEAVE_NOTIFY_MASK;
418
419   priv = GTK_EVENT_BOX (widget)->priv;
420
421   visible_window = gtk_widget_get_has_window (widget);
422   if (visible_window)
423     {
424       attributes.visual = gtk_widget_get_visual (widget);
425       attributes.wclass = GDK_INPUT_OUTPUT;
426
427       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
428
429       window = gdk_window_new (gtk_widget_get_parent_window (widget),
430                                &attributes, attributes_mask);
431       gtk_widget_set_window (widget, window);
432       gtk_widget_register_window (widget, window);
433     }
434   else
435     {
436       window = gtk_widget_get_parent_window (widget);
437       gtk_widget_set_window (widget, window);
438       g_object_ref (window);
439     }
440
441   if (!visible_window || priv->above_child)
442     {
443       attributes.wclass = GDK_INPUT_ONLY;
444       if (!visible_window)
445         attributes_mask = GDK_WA_X | GDK_WA_Y;
446       else
447         attributes_mask = 0;
448
449       priv->event_window = gdk_window_new (window,
450                                            &attributes, attributes_mask);
451       gtk_widget_register_window (widget, priv->event_window);
452     }
453
454   if (visible_window)
455     gtk_style_context_set_background (gtk_widget_get_style_context (widget), window);
456 }
457
458 static void
459 gtk_event_box_unrealize (GtkWidget *widget)
460 {
461   GtkEventBoxPrivate *priv = GTK_EVENT_BOX (widget)->priv;
462
463   if (priv->event_window != NULL)
464     {
465       gtk_widget_unregister_window (widget, priv->event_window);
466       gdk_window_destroy (priv->event_window);
467       priv->event_window = NULL;
468     }
469
470   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unrealize (widget);
471 }
472
473 static void
474 gtk_event_box_map (GtkWidget *widget)
475 {
476   GtkEventBoxPrivate *priv = GTK_EVENT_BOX (widget)->priv;
477
478   if (priv->event_window != NULL && !priv->above_child)
479     gdk_window_show (priv->event_window);
480
481   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->map (widget);
482
483   if (priv->event_window != NULL && priv->above_child)
484     gdk_window_show (priv->event_window);
485 }
486
487 static void
488 gtk_event_box_unmap (GtkWidget *widget)
489 {
490   GtkEventBoxPrivate *priv = GTK_EVENT_BOX (widget)->priv;
491
492   if (priv->event_window != NULL)
493     gdk_window_hide (priv->event_window);
494
495   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->unmap (widget);
496 }
497
498 static void
499 gtk_event_box_get_preferred_width (GtkWidget *widget,
500                                    gint      *minimum,
501                                    gint      *natural)
502 {
503   GtkBin *bin = GTK_BIN (widget);
504   GtkWidget *child;
505
506   if (minimum)
507     *minimum = 0;
508
509   if (natural)
510     *natural = 0;
511
512   child = gtk_bin_get_child (bin);
513   if (child && gtk_widget_get_visible (child))
514     gtk_widget_get_preferred_width (child, minimum, natural);
515 }
516
517 static void
518 gtk_event_box_get_preferred_height (GtkWidget *widget,
519                                     gint      *minimum,
520                                     gint      *natural)
521 {
522   GtkBin *bin = GTK_BIN (widget);
523   GtkWidget *child;
524
525   if (minimum)
526     *minimum = 0;
527
528   if (natural)
529     *natural = 0;
530
531   child = gtk_bin_get_child (bin);
532   if (child && gtk_widget_get_visible (child))
533     gtk_widget_get_preferred_height (child, minimum, natural);
534 }
535
536 static void
537 gtk_event_box_size_allocate (GtkWidget     *widget,
538                              GtkAllocation *allocation)
539 {
540   GtkBin *bin;
541   GtkAllocation child_allocation;
542   GtkEventBoxPrivate *priv;
543   GtkWidget *child;
544
545   bin = GTK_BIN (widget);
546
547   gtk_widget_set_allocation (widget, allocation);
548
549   if (!gtk_widget_get_has_window (widget))
550     {
551       child_allocation.x = allocation->x;
552       child_allocation.y = allocation->y;
553     }
554   else
555     {
556       child_allocation.x = 0;
557       child_allocation.y = 0;
558     }
559   child_allocation.width = allocation->width;
560   child_allocation.height = allocation->height;
561
562   if (gtk_widget_get_realized (widget))
563     {
564       priv = GTK_EVENT_BOX (widget)->priv;
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_get_has_window (widget))
574         gdk_window_move_resize (gtk_widget_get_window (widget),
575                                 allocation->x,
576                                 allocation->y,
577                                 child_allocation.width,
578                                 child_allocation.height);
579     }
580
581   child = gtk_bin_get_child (bin);
582   if (child)
583     gtk_widget_size_allocate (child, &child_allocation);
584 }
585
586 static gboolean
587 gtk_event_box_draw (GtkWidget *widget,
588                     cairo_t   *cr)
589 {
590   if (gtk_widget_get_has_window (widget) &&
591       !gtk_widget_get_app_paintable (widget))
592     {
593       GtkStyleContext *context;
594
595       context = gtk_widget_get_style_context (widget);
596
597       gtk_render_background (context, cr, 0, 0,
598                              gtk_widget_get_allocated_width (widget),
599                              gtk_widget_get_allocated_height (widget));
600     }
601
602   GTK_WIDGET_CLASS (gtk_event_box_parent_class)->draw (widget, cr);
603
604   return FALSE;
605 }