]> Pileus Git - ~andy/gtk/blob - gtk/gtkeventbox.c
Intern some more strings.
[~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 #include "gtkalias.h"
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_class_init    (GtkEventBoxClass *klass);
49 static void gtk_event_box_init          (GtkEventBox      *event_box);
50 static void gtk_event_box_realize       (GtkWidget        *widget);
51 static void gtk_event_box_unrealize     (GtkWidget        *widget);
52 static void gtk_event_box_map           (GtkWidget        *widget);
53 static void gtk_event_box_unmap         (GtkWidget        *widget);
54 static void gtk_event_box_size_request  (GtkWidget        *widget,
55                                          GtkRequisition   *requisition);
56 static void gtk_event_box_size_allocate (GtkWidget        *widget,
57                                          GtkAllocation    *allocation);
58 static void gtk_event_box_paint         (GtkWidget        *widget,
59                                          GdkRectangle     *area);
60 static gint 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 static GtkBinClass *parent_class = NULL;
72
73 GType
74 gtk_event_box_get_type (void)
75 {
76   static GType event_box_type = 0;
77
78   if (!event_box_type)
79     {
80       static const GTypeInfo event_box_info =
81       {
82         sizeof (GtkEventBoxClass),
83         NULL,           /* base_init */
84         NULL,           /* base_finalize */
85         (GClassInitFunc) gtk_event_box_class_init,
86         NULL,           /* class_finalize */
87         NULL,           /* class_data */
88         sizeof (GtkEventBox),
89         0,              /* n_preallocs */
90         (GInstanceInitFunc) gtk_event_box_init,
91       };
92
93       event_box_type = g_type_register_static (GTK_TYPE_BIN, I_("GtkEventBox"),
94                                                &event_box_info, 0);
95     }
96
97   return event_box_type;
98 }
99
100 static void
101 gtk_event_box_class_init (GtkEventBoxClass *class)
102 {
103   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
104   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
105
106   parent_class = g_type_class_peek_parent (class);
107
108   gobject_class->set_property = gtk_event_box_set_property;
109   gobject_class->get_property = gtk_event_box_get_property;
110   
111   widget_class->realize = gtk_event_box_realize;
112   widget_class->unrealize = gtk_event_box_unrealize;
113   widget_class->map = gtk_event_box_map;
114   widget_class->unmap = gtk_event_box_unmap;
115   widget_class->size_request = gtk_event_box_size_request;
116   widget_class->size_allocate = gtk_event_box_size_allocate;
117   widget_class->expose_event = gtk_event_box_expose;
118
119   g_object_class_install_property (gobject_class,
120                                    PROP_VISIBLE_WINDOW,
121                                    g_param_spec_boolean ("visible-window",
122                                                         P_("Visible Window"),
123                                                         P_("Whether the event box is visible, as opposed to invisible and only used to trap events."),
124                                                         TRUE,
125                                                         GTK_PARAM_READWRITE));
126   g_object_class_install_property (gobject_class,
127                                    PROP_ABOVE_CHILD,
128                                    g_param_spec_boolean ("above-child",
129                                                         P_("Above child"),
130                                                         P_("Whether the event-trapping window of the eventbox is above the window of the child widget as opposed to below it."),
131                                                         FALSE,
132                                                         GTK_PARAM_READWRITE));
133   
134   g_type_class_add_private (class, sizeof (GtkEventBoxPrivate));
135 }
136
137 static void
138 gtk_event_box_init (GtkEventBox *event_box)
139 {
140   GtkEventBoxPrivate *priv;
141
142   GTK_WIDGET_UNSET_FLAGS (event_box, GTK_NO_WINDOW);
143  
144   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
145   priv->above_child = FALSE;
146 }
147
148 GtkWidget*
149 gtk_event_box_new (void)
150 {
151   return g_object_new (GTK_TYPE_EVENT_BOX, NULL);
152 }
153
154 static void 
155 gtk_event_box_set_property (GObject      *object,
156                             guint         prop_id,
157                             const GValue *value,
158                             GParamSpec   *pspec)
159 {
160   GtkEventBox *event_box;
161   
162   event_box = GTK_EVENT_BOX (object);
163   
164   switch (prop_id)
165     {
166     case PROP_VISIBLE_WINDOW:
167       gtk_event_box_set_visible_window (event_box, g_value_get_boolean (value));
168       break;      
169     case PROP_ABOVE_CHILD:
170       gtk_event_box_set_above_child (event_box, g_value_get_boolean (value));
171       break;      
172     default:
173       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
174       break;
175     }
176 }
177
178 static void 
179 gtk_event_box_get_property (GObject     *object,
180                             guint        prop_id,
181                             GValue      *value,
182                             GParamSpec  *pspec)
183 {
184   GtkEventBox *event_box;
185   
186   event_box = GTK_EVENT_BOX (object);
187   
188   switch (prop_id)
189     {
190     case PROP_VISIBLE_WINDOW:
191       g_value_set_boolean (value, gtk_event_box_get_visible_window (event_box));
192       break;
193     case PROP_ABOVE_CHILD:
194       g_value_set_boolean (value, gtk_event_box_get_above_child (event_box));
195       break;
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_NO_WINDOW (event_box);
219 }
220
221 /**
222  * gtk_event_box_set_visible_window:
223  * @event_box: a #GtkEventBox
224  * @visible_window: boolean value
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 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   GtkEventBoxPrivate *priv;
270
271   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
272
273   widget = GTK_WIDGET (event_box);
274   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
275
276   visible_window = visible_window != FALSE;
277
278   if (visible_window != !GTK_WIDGET_NO_WINDOW (widget))
279     {
280       if (GTK_WIDGET_REALIZED (widget))
281         {
282           gboolean visible = GTK_WIDGET_VISIBLE (widget);
283
284           if (visible)
285             gtk_widget_hide (widget);
286
287           gtk_widget_unrealize (widget);
288
289           if (visible_window)
290             GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
291           else
292             GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
293           
294           gtk_widget_realize (widget);
295
296           if (visible)
297             gtk_widget_show (widget);
298         }
299       else
300         {
301           if (visible_window)
302             GTK_WIDGET_UNSET_FLAGS (widget, GTK_NO_WINDOW);
303           else
304             GTK_WIDGET_SET_FLAGS (widget, GTK_NO_WINDOW);
305         }
306
307       if (GTK_WIDGET_VISIBLE (widget))
308         gtk_widget_queue_resize (widget);
309       
310       g_object_notify (G_OBJECT (event_box), "visible-window");
311     }
312 }
313
314 /**
315  * gtk_event_box_get_above_child:
316  * @event_box: a #GtkEventBox
317  *
318  * Returns whether the event box window is above or below the
319  * windows of its child. See gtk_event_box_set_above_child() for
320  * details.
321  *
322  * Return value: %TRUE if the event box window is above the window
323  * of its child.
324  *
325  * Since: 2.4
326  **/
327 gboolean
328 gtk_event_box_get_above_child (GtkEventBox *event_box)
329 {
330   GtkEventBoxPrivate *priv;
331
332   g_return_val_if_fail (GTK_IS_EVENT_BOX (event_box), FALSE);
333
334   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
335
336   return priv->above_child;
337 }
338
339 /**
340  * gtk_event_box_set_above_child:
341  * @event_box: a #GtkEventBox
342  * @above_child: %TRUE if the event box window is above the windows of its child
343  *
344  * Set whether the event box window is positioned above the windows of its child,
345  * as opposed to below it. If the window is above, all events inside the
346  * event box will go to the event box. If the window is below, events
347  * in windows of child widgets will first got to that widget, and then
348  * to its parents.
349  *
350  * The default is to keep the window below the child.
351  * 
352  * Since: 2.4
353  **/
354 void
355 gtk_event_box_set_above_child (GtkEventBox *event_box,
356                                gboolean above_child)
357 {
358   GtkWidget *widget;
359   GtkEventBoxPrivate *priv;
360
361   g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
362
363   widget = GTK_WIDGET (event_box);
364   priv = GTK_EVENT_BOX_GET_PRIVATE (event_box);
365
366   above_child = above_child != FALSE;
367
368   if (priv->above_child != above_child)
369     {
370       priv->above_child = above_child;
371
372       if (GTK_WIDGET_REALIZED (widget))
373         {
374           if (GTK_WIDGET_NO_WINDOW (widget))
375             {
376               if (above_child)
377                 gdk_window_raise (priv->event_window);
378               else
379                 gdk_window_lower (priv->event_window);
380             }
381           else
382             {
383               gboolean visible = GTK_WIDGET_VISIBLE (widget);
384
385               if (visible)
386                 gtk_widget_hide (widget);
387               
388               gtk_widget_unrealize (widget);
389               
390               gtk_widget_realize (widget);
391               
392               if (visible)
393                 gtk_widget_show (widget);
394             }
395         }
396
397       if (GTK_WIDGET_VISIBLE (widget))
398         gtk_widget_queue_resize (widget);
399       
400       g_object_notify (G_OBJECT (event_box), "above-child");
401     }
402 }
403
404
405
406 static void
407 gtk_event_box_realize (GtkWidget *widget)
408 {
409   GdkWindowAttr attributes;
410   gint attributes_mask;
411   gint border_width;
412   GtkEventBoxPrivate *priv;
413   gboolean visible_window;
414
415   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
416
417   border_width = GTK_CONTAINER (widget)->border_width;
418   
419   attributes.x = widget->allocation.x + border_width;
420   attributes.y = widget->allocation.y + border_width;
421   attributes.width = widget->allocation.width - 2*border_width;
422   attributes.height = widget->allocation.height - 2*border_width;
423   attributes.window_type = GDK_WINDOW_CHILD;
424   attributes.event_mask = gtk_widget_get_events (widget)
425                         | GDK_BUTTON_MOTION_MASK
426                         | GDK_BUTTON_PRESS_MASK
427                         | GDK_BUTTON_RELEASE_MASK
428                         | GDK_EXPOSURE_MASK
429                         | GDK_ENTER_NOTIFY_MASK
430                         | GDK_LEAVE_NOTIFY_MASK;
431
432   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
433
434   visible_window = !GTK_WIDGET_NO_WINDOW (widget);
435   if (visible_window)
436     {
437       attributes.visual = gtk_widget_get_visual (widget);
438       attributes.colormap = gtk_widget_get_colormap (widget);
439       attributes.wclass = GDK_INPUT_OUTPUT;
440       
441       attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
442       
443       widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
444                                        &attributes, attributes_mask);
445       gdk_window_set_user_data (widget->window, widget);
446     }
447   else
448     {
449       widget->window = gtk_widget_get_parent_window (widget);
450       g_object_ref (widget->window);
451     }
452
453   if (!visible_window || priv->above_child)
454     {
455       attributes.wclass = GDK_INPUT_ONLY;
456       attributes_mask = GDK_WA_X | GDK_WA_Y;
457
458       priv->event_window = gdk_window_new (widget->window,
459                                            &attributes, attributes_mask);
460       gdk_window_set_user_data (priv->event_window, widget);
461     }
462
463
464   widget->style = gtk_style_attach (widget->style, widget->window);
465   
466   if (visible_window)
467     gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
468 }
469
470 static void
471 gtk_event_box_unrealize (GtkWidget *widget)
472 {
473   GtkEventBox *event_box;
474   GtkEventBoxPrivate *priv;
475   
476   event_box = GTK_EVENT_BOX (widget);
477   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
478   
479   if (priv->event_window != NULL)
480     {
481       gdk_window_set_user_data (priv->event_window, NULL);
482       gdk_window_destroy (priv->event_window);
483       priv->event_window = NULL;
484     }
485
486   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
487     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
488 }
489
490 static void
491 gtk_event_box_map (GtkWidget *widget)
492 {
493   GtkEventBoxPrivate *priv;
494
495   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
496
497   if (priv->event_window != NULL && !priv->above_child)
498     gdk_window_show (priv->event_window);
499   
500   (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
501
502   if (priv->event_window != NULL && priv->above_child)
503     gdk_window_show (priv->event_window);
504 }
505
506 static void
507 gtk_event_box_unmap (GtkWidget *widget)
508 {
509   GtkEventBoxPrivate *priv;
510
511   priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
512
513   if (priv->event_window != NULL)
514     gdk_window_hide (priv->event_window);
515   
516   (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
517 }
518
519
520
521 static void
522 gtk_event_box_size_request (GtkWidget      *widget,
523                             GtkRequisition *requisition)
524 {
525   GtkBin *bin = GTK_BIN (widget);
526
527   requisition->width = GTK_CONTAINER (widget)->border_width * 2;
528   requisition->height = GTK_CONTAINER (widget)->border_width * 2;
529
530   if (bin->child && GTK_WIDGET_VISIBLE (bin->child))
531     {
532       GtkRequisition child_requisition;
533       
534       gtk_widget_size_request (bin->child, &child_requisition);
535
536       requisition->width += child_requisition.width;
537       requisition->height += child_requisition.height;
538     }
539 }
540
541 static void
542 gtk_event_box_size_allocate (GtkWidget     *widget,
543                              GtkAllocation *allocation)
544 {
545   GtkBin *bin;
546   GtkAllocation child_allocation;
547   GtkEventBoxPrivate *priv;
548   
549   widget->allocation = *allocation;
550   bin = GTK_BIN (widget);
551   
552   if (GTK_WIDGET_NO_WINDOW (widget))
553     {
554       child_allocation.x = allocation->x + GTK_CONTAINER (widget)->border_width;
555       child_allocation.y = allocation->y + GTK_CONTAINER (widget)->border_width;
556     }
557   else
558     {
559       child_allocation.x = 0;
560       child_allocation.y = 0;
561     }
562   child_allocation.width = MAX (allocation->width - GTK_CONTAINER (widget)->border_width * 2, 0);
563   child_allocation.height = MAX (allocation->height - GTK_CONTAINER (widget)->border_width * 2, 0);
564
565   if (GTK_WIDGET_REALIZED (widget))
566     {
567       priv = GTK_EVENT_BOX_GET_PRIVATE (widget);
568
569       if (priv->event_window != NULL)
570         gdk_window_move_resize (priv->event_window,
571                                 child_allocation.x,
572                                 child_allocation.y,
573                                 child_allocation.width,
574                                 child_allocation.height);
575       
576       if (!GTK_WIDGET_NO_WINDOW (widget))
577         gdk_window_move_resize (widget->window,
578                                 allocation->x + GTK_CONTAINER (widget)->border_width,
579                                 allocation->y + GTK_CONTAINER (widget)->border_width,
580                                 child_allocation.width,
581                                 child_allocation.height);
582     }
583   
584   if (bin->child)
585     gtk_widget_size_allocate (bin->child, &child_allocation);
586 }
587
588 static void
589 gtk_event_box_paint (GtkWidget    *widget,
590                      GdkRectangle *area)
591 {
592   if (!GTK_WIDGET_APP_PAINTABLE (widget))
593     gtk_paint_flat_box (widget->style, widget->window,
594                         widget->state, GTK_SHADOW_NONE,
595                         area, widget, "eventbox",
596                         0, 0, -1, -1);
597 }
598
599 static gint
600 gtk_event_box_expose (GtkWidget      *widget,
601                      GdkEventExpose *event)
602 {
603   if (GTK_WIDGET_DRAWABLE (widget))
604     {
605       if (!GTK_WIDGET_NO_WINDOW (widget))
606         gtk_event_box_paint (widget, &event->area);
607       
608       (* GTK_WIDGET_CLASS (parent_class)->expose_event) (widget, event);
609     }
610
611   return FALSE;
612 }
613
614 #define __GTK_EVENT_BOX_C__
615 #include "gtkaliasdef.c"