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