]> Pileus Git - ~andy/gtk/blob - gtk/gtkexpander.c
c658340b794929d22d1865c73e83d18a9545dec4
[~andy/gtk] / gtk / gtkexpander.c
1 /* GTK - The GIMP Toolkit
2  *
3  * Copyright (C) 2003 Sun Microsystems, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Authors:
21  *      Mark McLoughlin <mark@skynet.ie>
22  */
23
24 #include <config.h>
25
26 #include "gtkexpander.h"
27
28 #include "gtklabel.h"
29 #include "gtkcontainer.h"
30 #include "gtkmarshalers.h"
31 #include "gtkmain.h"
32 #include "gtkintl.h"
33 #include "gtkprivate.h"
34 #include <gdk/gdkkeysyms.h>
35 #include "gtkalias.h"
36 #include "gtkdnd.h"
37
38 #define GTK_EXPANDER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_EXPANDER, GtkExpanderPrivate))
39
40 #define DEFAULT_EXPANDER_SIZE 10
41 #define DEFAULT_EXPANDER_SPACING 2
42
43 enum
44 {
45   PROP_0,
46   PROP_EXPANDED,
47   PROP_LABEL,
48   PROP_USE_UNDERLINE,
49   PROP_USE_MARKUP,
50   PROP_SPACING,
51   PROP_LABEL_WIDGET
52 };
53
54 struct _GtkExpanderPrivate
55 {
56   GtkWidget        *label_widget;
57   GdkWindow        *event_window;
58   gint              spacing;
59
60   GtkExpanderStyle  expander_style;
61   guint             animation_timeout;
62   guint             expand_timer;
63
64   guint             expanded : 1;
65   guint             use_underline : 1;
66   guint             use_markup : 1; 
67   guint             button_down : 1;
68   guint             prelight : 1;
69 };
70
71 static void gtk_expander_class_init (GtkExpanderClass *klass);
72 static void gtk_expander_init       (GtkExpander      *expander);
73
74 static void gtk_expander_set_property (GObject          *object,
75                                        guint             prop_id,
76                                        const GValue     *value,
77                                        GParamSpec       *pspec);
78 static void gtk_expander_get_property (GObject          *object,
79                                        guint             prop_id,
80                                        GValue           *value,
81                                        GParamSpec       *pspec);
82
83 static void gtk_expander_destroy (GtkObject *object);
84
85 static void     gtk_expander_realize        (GtkWidget        *widget);
86 static void     gtk_expander_unrealize      (GtkWidget        *widget);
87 static void     gtk_expander_size_request   (GtkWidget        *widget,
88                                              GtkRequisition   *requisition);
89 static void     gtk_expander_size_allocate  (GtkWidget        *widget,
90                                              GtkAllocation    *allocation);
91 static void     gtk_expander_map            (GtkWidget        *widget);
92 static void     gtk_expander_unmap          (GtkWidget        *widget);
93 static gboolean gtk_expander_expose         (GtkWidget        *widget,
94                                              GdkEventExpose   *event);
95 static gboolean gtk_expander_button_press   (GtkWidget        *widget,
96                                              GdkEventButton   *event);
97 static gboolean gtk_expander_button_release (GtkWidget        *widget,
98                                              GdkEventButton   *event);
99 static gboolean gtk_expander_enter_notify   (GtkWidget        *widget,
100                                              GdkEventCrossing *event);
101 static gboolean gtk_expander_leave_notify   (GtkWidget        *widget,
102                                              GdkEventCrossing *event);
103 static gboolean gtk_expander_focus          (GtkWidget        *widget,
104                                              GtkDirectionType  direction);
105 static void     gtk_expander_grab_notify    (GtkWidget        *widget,
106                                              gboolean          was_grabbed);
107 static void     gtk_expander_state_changed  (GtkWidget        *widget,
108                                              GtkStateType      previous_state);
109 static gboolean gtk_expander_drag_motion    (GtkWidget        *widget,
110                                              GdkDragContext   *context,
111                                              gint              x,
112                                              gint              y,
113                                              guint             time);
114 static void     gtk_expander_drag_leave     (GtkWidget        *widget,
115                                              GdkDragContext   *context,
116                                              guint             time);
117
118 static void gtk_expander_add    (GtkContainer *container,
119                                  GtkWidget    *widget);
120 static void gtk_expander_remove (GtkContainer *container,
121                                  GtkWidget    *widget);
122 static void gtk_expander_forall (GtkContainer *container,
123                                  gboolean        include_internals,
124                                  GtkCallback     callback,
125                                  gpointer        callback_data);
126
127 static void gtk_expander_activate (GtkExpander *expander);
128
129 static void get_expander_bounds (GtkExpander  *expander,
130                                  GdkRectangle *rect);
131
132 static GtkBinClass *parent_class = NULL;
133
134 GType
135 gtk_expander_get_type (void)
136 {
137   static GType expander_type = 0;
138   
139   if (!expander_type)
140     {
141       static const GTypeInfo expander_info =
142       {
143         sizeof (GtkExpanderClass),
144         NULL,           /* base_init */
145         NULL,           /* base_finalize */
146         (GClassInitFunc) gtk_expander_class_init,
147         NULL,           /* class_finalize */
148         NULL,           /* class_data */
149         sizeof (GtkExpander),
150         0,              /* n_preallocs */
151         (GInstanceInitFunc) gtk_expander_init,
152       };
153       
154       expander_type = g_type_register_static (GTK_TYPE_BIN,
155                                               I_("GtkExpander"),
156                                               &expander_info, 0);
157     }
158   
159   return expander_type;
160 }
161
162 static void
163 gtk_expander_class_init (GtkExpanderClass *klass)
164 {
165   GObjectClass *gobject_class;
166   GtkObjectClass *object_class;
167   GtkWidgetClass *widget_class;
168   GtkContainerClass *container_class;
169
170   parent_class = g_type_class_peek_parent (klass);
171
172   gobject_class   = (GObjectClass *) klass;
173   object_class    = (GtkObjectClass *) klass;
174   widget_class    = (GtkWidgetClass *) klass;
175   container_class = (GtkContainerClass *) klass;
176
177   gobject_class->set_property = gtk_expander_set_property;
178   gobject_class->get_property = gtk_expander_get_property;
179
180   object_class->destroy = gtk_expander_destroy;
181
182   widget_class->realize              = gtk_expander_realize;
183   widget_class->unrealize            = gtk_expander_unrealize;
184   widget_class->size_request         = gtk_expander_size_request;
185   widget_class->size_allocate        = gtk_expander_size_allocate;
186   widget_class->map                  = gtk_expander_map;
187   widget_class->unmap                = gtk_expander_unmap;
188   widget_class->expose_event         = gtk_expander_expose;
189   widget_class->button_press_event   = gtk_expander_button_press;
190   widget_class->button_release_event = gtk_expander_button_release;
191   widget_class->enter_notify_event   = gtk_expander_enter_notify;
192   widget_class->leave_notify_event   = gtk_expander_leave_notify;
193   widget_class->focus                = gtk_expander_focus;
194   widget_class->grab_notify          = gtk_expander_grab_notify;
195   widget_class->state_changed        = gtk_expander_state_changed;
196   widget_class->drag_motion          = gtk_expander_drag_motion;
197   widget_class->drag_leave           = gtk_expander_drag_leave;
198
199   container_class->add    = gtk_expander_add;
200   container_class->remove = gtk_expander_remove;
201   container_class->forall = gtk_expander_forall;
202
203   klass->activate = gtk_expander_activate;
204
205   g_type_class_add_private (klass, sizeof (GtkExpanderPrivate));
206
207   g_object_class_install_property (gobject_class,
208                                    PROP_EXPANDED,
209                                    g_param_spec_boolean ("expanded",
210                                                          P_("Expanded"),
211                                                          P_("Whether the expander has been opened to reveal the child widget"),
212                                                          FALSE,
213                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
214
215   g_object_class_install_property (gobject_class,
216                                    PROP_LABEL,
217                                    g_param_spec_string ("label",
218                                                         P_("Label"),
219                                                         P_("Text of the expander's label"),
220                                                         NULL,
221                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
222
223   g_object_class_install_property (gobject_class,
224                                    PROP_USE_UNDERLINE,
225                                    g_param_spec_boolean ("use-underline",
226                                                          P_("Use underline"),
227                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
228                                                          FALSE,
229                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
230
231   g_object_class_install_property (gobject_class,
232                                    PROP_USE_MARKUP,
233                                    g_param_spec_boolean ("use-markup",
234                                                          P_("Use markup"),
235                                                          P_("The text of the label includes XML markup. See pango_parse_markup()"),
236                                                          FALSE,
237                                                          GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
238
239   g_object_class_install_property (gobject_class,
240                                    PROP_SPACING,
241                                    g_param_spec_int ("spacing",
242                                                      P_("Spacing"),
243                                                      P_("Space to put between the label and the child"),
244                                                      0,
245                                                      G_MAXINT,
246                                                      0,
247                                                      GTK_PARAM_READWRITE));
248
249   g_object_class_install_property (gobject_class,
250                                    PROP_LABEL_WIDGET,
251                                    g_param_spec_object ("label-widget",
252                                                         P_("Label widget"),
253                                                         P_("A widget to display in place of the usual expander label"),
254                                                         GTK_TYPE_WIDGET,
255                                                         GTK_PARAM_READWRITE));
256
257   gtk_widget_class_install_style_property (widget_class,
258                                            g_param_spec_int ("expander-size",
259                                                              P_("Expander Size"),
260                                                              P_("Size of the expander arrow"),
261                                                              0,
262                                                              G_MAXINT,
263                                                              DEFAULT_EXPANDER_SIZE,
264                                                              GTK_PARAM_READABLE));
265
266   gtk_widget_class_install_style_property (widget_class,
267                                            g_param_spec_int ("expander-spacing",
268                                                              P_("Indicator Spacing"),
269                                                              P_("Spacing around expander arrow"),
270                                                              0,
271                                                              G_MAXINT,
272                                                              DEFAULT_EXPANDER_SPACING,
273                                                              GTK_PARAM_READABLE));
274
275   widget_class->activate_signal =
276     g_signal_new (I_("activate"),
277                   G_TYPE_FROM_CLASS (gobject_class),
278                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
279                   G_STRUCT_OFFSET (GtkExpanderClass, activate),
280                   NULL, NULL,
281                   _gtk_marshal_VOID__VOID,
282                   G_TYPE_NONE, 0);
283 }
284
285 static void
286 gtk_expander_init (GtkExpander *expander)
287 {
288   GtkExpanderPrivate *priv;
289
290   expander->priv = priv = GTK_EXPANDER_GET_PRIVATE (expander);
291
292   GTK_WIDGET_SET_FLAGS (expander, GTK_CAN_FOCUS);
293   GTK_WIDGET_SET_FLAGS (expander, GTK_NO_WINDOW);
294
295   priv->label_widget = NULL;
296   priv->event_window = NULL;
297   priv->spacing = 0;
298
299   priv->expander_style = GTK_EXPANDER_COLLAPSED;
300   priv->animation_timeout = 0;
301
302   priv->expanded = FALSE;
303   priv->use_underline = FALSE;
304   priv->use_markup = FALSE;
305   priv->button_down = FALSE;
306   priv->prelight = FALSE;
307   priv->expand_timer = 0;
308
309   gtk_drag_dest_set (GTK_WIDGET (expander), 0, NULL, 0, 0);
310   gtk_drag_dest_set_track_motion (GTK_WIDGET (expander), TRUE);
311 }
312
313 static void
314 gtk_expander_set_property (GObject      *object,
315                            guint         prop_id,
316                            const GValue *value,
317                            GParamSpec   *pspec)
318 {
319   GtkExpander *expander = GTK_EXPANDER (object);
320                                                                                                              
321   switch (prop_id)
322     {
323     case PROP_EXPANDED:
324       gtk_expander_set_expanded (expander, g_value_get_boolean (value));
325       break;
326     case PROP_LABEL:
327       gtk_expander_set_label (expander, g_value_get_string (value));
328       break;
329     case PROP_USE_UNDERLINE:
330       gtk_expander_set_use_underline (expander, g_value_get_boolean (value));
331       break;
332     case PROP_USE_MARKUP:
333       gtk_expander_set_use_markup (expander, g_value_get_boolean (value));
334       break;
335     case PROP_SPACING:
336       gtk_expander_set_spacing (expander, g_value_get_int (value));
337       break;
338     case PROP_LABEL_WIDGET:
339       gtk_expander_set_label_widget (expander, g_value_get_object (value));
340       break;
341     default:
342       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
343       break;
344     }
345 }
346
347 static void
348 gtk_expander_get_property (GObject    *object,
349                            guint       prop_id,
350                            GValue     *value,
351                            GParamSpec *pspec)
352 {
353   GtkExpander *expander = GTK_EXPANDER (object);
354   GtkExpanderPrivate *priv = expander->priv;
355
356   switch (prop_id)
357     {
358     case PROP_EXPANDED:
359       g_value_set_boolean (value, priv->expanded);
360       break;
361     case PROP_LABEL:
362       g_value_set_string (value, gtk_expander_get_label (expander));
363       break;
364     case PROP_USE_UNDERLINE:
365       g_value_set_boolean (value, priv->use_underline);
366       break;
367     case PROP_USE_MARKUP:
368       g_value_set_boolean (value, priv->use_markup);
369       break;
370     case PROP_SPACING:
371       g_value_set_int (value, priv->spacing);
372       break;
373     case PROP_LABEL_WIDGET:
374       g_value_set_object (value,
375                           priv->label_widget ?
376                           G_OBJECT (priv->label_widget) : NULL);
377       break;
378     default:
379       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
380       break;
381     }
382 }
383
384 static void
385 gtk_expander_destroy (GtkObject *object)
386 {
387   GtkExpanderPrivate *priv = GTK_EXPANDER (object)->priv;
388   
389   if (priv->animation_timeout)
390     {
391       g_source_remove (priv->animation_timeout);
392       priv->animation_timeout = 0;
393     }
394   
395   GTK_OBJECT_CLASS (parent_class)->destroy (object);
396 }
397
398 static void
399 gtk_expander_realize (GtkWidget *widget)
400 {
401   GtkExpanderPrivate *priv;
402   GdkWindowAttr attributes;
403   gint attributes_mask;
404   gint border_width;
405   GdkRectangle expander_rect;
406
407   priv = GTK_EXPANDER (widget)->priv;
408   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
409
410   border_width = GTK_CONTAINER (widget)->border_width;
411
412   get_expander_bounds (GTK_EXPANDER (widget), &expander_rect);
413   
414   attributes.window_type = GDK_WINDOW_CHILD;
415   attributes.x = widget->allocation.x + border_width;
416   attributes.y = expander_rect.y;
417   attributes.width = MAX (widget->allocation.width - 2 * border_width, 1);
418   attributes.height = expander_rect.width;
419   attributes.wclass = GDK_INPUT_ONLY;
420   attributes.event_mask = gtk_widget_get_events (widget)     |
421                                 GDK_BUTTON_PRESS_MASK        |
422                                 GDK_BUTTON_RELEASE_MASK      |
423                                 GDK_ENTER_NOTIFY_MASK        |
424                                 GDK_LEAVE_NOTIFY_MASK;
425
426   attributes_mask = GDK_WA_X | GDK_WA_Y;
427
428   widget->window = gtk_widget_get_parent_window (widget);
429   g_object_ref (widget->window);
430
431   priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
432                                        &attributes, attributes_mask);
433   gdk_window_set_user_data (priv->event_window, widget);
434
435   widget->style = gtk_style_attach (widget->style, widget->window);
436   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
437 }
438
439 static void
440 gtk_expander_unrealize (GtkWidget *widget)
441 {
442   GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
443
444   if (priv->event_window)
445     {
446       gdk_window_set_user_data (priv->event_window, NULL);
447       gdk_window_destroy (priv->event_window);
448       priv->event_window = NULL;
449     }
450
451   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
452 }
453
454 static void
455 gtk_expander_size_request (GtkWidget      *widget,
456                            GtkRequisition *requisition)
457 {
458   GtkExpander *expander;
459   GtkBin *bin;
460   GtkExpanderPrivate *priv;
461   gint border_width;
462   gint expander_size;
463   gint expander_spacing;
464   gboolean interior_focus;
465   gint focus_width;
466   gint focus_pad;
467
468   bin = GTK_BIN (widget);
469   expander = GTK_EXPANDER (widget);
470   priv = expander->priv;
471
472   border_width = GTK_CONTAINER (widget)->border_width;
473
474   gtk_widget_style_get (widget,
475                         "interior-focus", &interior_focus,
476                         "focus-line-width", &focus_width,
477                         "focus-padding", &focus_pad,
478                         "expander-size", &expander_size,
479                         "expander-spacing", &expander_spacing,
480                         NULL);
481
482   requisition->width = expander_size + 2 * expander_spacing +
483                        2 * focus_width + 2 * focus_pad;
484   requisition->height = interior_focus ? (2 * focus_width + 2 * focus_pad) : 0;
485
486   if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
487     {
488       GtkRequisition label_requisition;
489
490       gtk_widget_size_request (priv->label_widget, &label_requisition);
491
492       requisition->width  += label_requisition.width;
493       requisition->height += label_requisition.height;
494     }
495
496   requisition->height = MAX (expander_size + 2 * expander_spacing, requisition->height);
497
498   if (!interior_focus)
499     requisition->height += 2 * focus_width + 2 * focus_pad;
500
501   if (bin->child && GTK_WIDGET_CHILD_VISIBLE (bin->child))
502     {
503       GtkRequisition child_requisition;
504
505       gtk_widget_size_request (bin->child, &child_requisition);
506
507       requisition->width = MAX (requisition->width, child_requisition.width);
508       requisition->height += child_requisition.height + priv->spacing;
509     }
510
511   requisition->width  += 2 * border_width;
512   requisition->height += 2 * border_width;
513 }
514
515 static void
516 get_expander_bounds (GtkExpander  *expander,
517                      GdkRectangle *rect)
518 {
519   GtkWidget *widget;
520   GtkExpanderPrivate *priv;
521   gint border_width;
522   gint expander_size;
523   gint expander_spacing;
524   gboolean interior_focus;
525   gint focus_width;
526   gint focus_pad;
527   gboolean ltr;
528
529   widget = GTK_WIDGET (expander);
530   priv = expander->priv;
531
532   border_width = GTK_CONTAINER (expander)->border_width;
533
534   gtk_widget_style_get (widget,
535                         "interior-focus", &interior_focus,
536                         "focus-line-width", &focus_width,
537                         "focus-padding", &focus_pad,
538                         "expander-size", &expander_size,
539                         "expander-spacing", &expander_spacing,
540                         NULL);
541
542   ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
543
544   rect->x = widget->allocation.x + border_width;
545   rect->y = widget->allocation.y + border_width;
546
547   if (ltr)
548     rect->x += expander_spacing;
549   else
550     rect->x += widget->allocation.width - 2 * border_width -
551                expander_spacing - expander_size;
552
553   if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
554     {
555       GtkAllocation label_allocation;
556
557       label_allocation = priv->label_widget->allocation;
558
559       if (expander_size < label_allocation.height)
560         rect->y += focus_width + focus_pad + (label_allocation.height - expander_size) / 2;
561       else
562         rect->y += expander_spacing;
563     }
564   else
565     {
566       rect->y += expander_spacing;
567     }
568
569   if (!interior_focus)
570     {
571       if (ltr)
572         rect->x += focus_width + focus_pad;
573       else
574         rect->x -= focus_width + focus_pad;
575       rect->y += focus_width + focus_pad;
576     }
577
578   rect->width = rect->height = expander_size;
579 }
580
581 static void
582 gtk_expander_size_allocate (GtkWidget     *widget,
583                             GtkAllocation *allocation)
584 {
585   GtkExpander *expander;
586   GtkBin *bin;
587   GtkExpanderPrivate *priv;
588   GtkRequisition child_requisition;
589   gboolean child_visible = FALSE;
590   gint border_width;
591   gint expander_size;
592   gint expander_spacing;
593   gboolean interior_focus;
594   gint focus_width;
595   gint focus_pad;
596   gint label_height;
597
598   expander = GTK_EXPANDER (widget);
599   bin = GTK_BIN (widget);
600   priv = expander->priv;
601
602   border_width = GTK_CONTAINER (widget)->border_width;
603
604   gtk_widget_style_get (widget,
605                         "interior-focus", &interior_focus,
606                         "focus-line-width", &focus_width,
607                         "focus-padding", &focus_pad,
608                         "expander-size", &expander_size,
609                         "expander-spacing", &expander_spacing,
610                         NULL);
611
612   child_requisition.width = 0;
613   child_requisition.height = 0;
614   if (bin->child && GTK_WIDGET_CHILD_VISIBLE (bin->child))
615     {
616       child_visible = TRUE;
617       gtk_widget_get_child_requisition (bin->child, &child_requisition);
618     }
619
620   widget->allocation = *allocation;
621
622   if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
623     {
624       GtkAllocation label_allocation;
625       GtkRequisition label_requisition;
626       gboolean ltr;
627
628       gtk_widget_get_child_requisition (priv->label_widget, &label_requisition);
629
630       ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
631
632       if (ltr)
633         label_allocation.x = (widget->allocation.x +
634                               border_width + focus_width + focus_pad +
635                               expander_size + 2 * expander_spacing);
636       else
637         label_allocation.x = (widget->allocation.x + widget->allocation.width -
638                               (label_requisition.width +
639                                border_width + focus_width + focus_pad +
640                                expander_size + 2 * expander_spacing));
641
642       label_allocation.y = widget->allocation.y + border_width + focus_width + focus_pad;
643
644       label_allocation.width = MIN (label_requisition.width,
645                                     allocation->width - 2 * border_width -
646                                     expander_size - 2 * expander_spacing -
647                                     2 * focus_width - 2 * focus_pad);
648       label_allocation.width = MAX (label_allocation.width, 1);
649
650       label_allocation.height = MIN (label_requisition.height,
651                                      allocation->height - 2 * border_width -
652                                      2 * focus_width - 2 * focus_pad -
653                                      (child_visible ? priv->spacing : 0));
654       label_allocation.height = MAX (label_allocation.height, 1);
655
656       gtk_widget_size_allocate (priv->label_widget, &label_allocation);
657
658       label_height = label_allocation.height;
659     }
660   else
661     {
662       label_height = 0;
663     }
664
665   if (GTK_WIDGET_REALIZED (widget))
666     {
667       GdkRectangle rect;
668
669       get_expander_bounds (expander, &rect);
670
671       gdk_window_move_resize (priv->event_window,
672                               allocation->x + border_width, rect.y,
673                               MAX (allocation->width - 2 * border_width, 1), rect.width);
674     }
675
676   if (child_visible)
677     {
678       GtkAllocation child_allocation;
679       gint top_height;
680
681       top_height = MAX (2 * expander_spacing + expander_size,
682                         label_height +
683                         (interior_focus ? 2 * focus_width + 2 * focus_pad : 0));
684
685       child_allocation.x = widget->allocation.x + border_width;
686       child_allocation.y = widget->allocation.y + border_width + top_height + priv->spacing;
687
688       if (!interior_focus)
689         child_allocation.y += 2 * focus_width + 2 * focus_pad;
690
691       child_allocation.width = MAX (allocation->width - 2 * border_width, 1);
692
693       child_allocation.height = allocation->height - top_height -
694                                 2 * border_width - priv->spacing -
695                                 (!interior_focus ? 2 * focus_width + 2 * focus_pad : 0);
696       child_allocation.height = MAX (child_allocation.height, 1);
697
698       gtk_widget_size_allocate (bin->child, &child_allocation);
699     }
700 }
701
702 static void
703 gtk_expander_map (GtkWidget *widget)
704 {
705   GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
706
707   if (priv->label_widget)
708     gtk_widget_map (priv->label_widget);
709
710   GTK_WIDGET_CLASS (parent_class)->map (widget);
711
712   if (priv->event_window)
713     gdk_window_show (priv->event_window);
714 }
715
716 static void
717 gtk_expander_unmap (GtkWidget *widget)
718 {
719   GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
720
721   if (priv->event_window)
722     gdk_window_hide (priv->event_window);
723
724   GTK_WIDGET_CLASS (parent_class)->unmap (widget);
725
726   if (priv->label_widget)
727     gtk_widget_unmap (priv->label_widget);
728 }
729
730 static void
731 gtk_expander_paint_prelight (GtkExpander *expander)
732 {
733   GtkWidget *widget;
734   GtkContainer *container;
735   GtkExpanderPrivate *priv;
736   GdkRectangle area;
737   gboolean interior_focus;
738   int focus_width;
739   int focus_pad;
740   int expander_size;
741   int expander_spacing;
742
743   priv = expander->priv;
744   widget = GTK_WIDGET (expander);
745   container = GTK_CONTAINER (expander);
746
747   gtk_widget_style_get (widget,
748                         "interior-focus", &interior_focus,
749                         "focus-line-width", &focus_width,
750                         "focus-padding", &focus_pad,
751                         "expander-size", &expander_size,
752                         "expander-spacing", &expander_spacing,
753                         NULL);
754
755   area.x = widget->allocation.x + container->border_width;
756   area.y = widget->allocation.y + container->border_width;
757   area.width = widget->allocation.width - (2 * container->border_width);
758
759   if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
760     area.height = priv->label_widget->allocation.height;
761   else
762     area.height = 0;
763
764   area.height += interior_focus ? (focus_width + focus_pad) * 2 : 0;
765   area.height = MAX (area.height, expander_size + 2 * expander_spacing);
766   area.height += !interior_focus ? (focus_width + focus_pad) * 2 : 0;
767
768   gtk_paint_flat_box (widget->style, widget->window,
769                       GTK_STATE_PRELIGHT,
770                       GTK_SHADOW_ETCHED_OUT,
771                       &area, widget, "expander",
772                       area.x, area.y,
773                       area.width, area.height);
774 }
775
776 static void
777 gtk_expander_paint (GtkExpander *expander)
778 {
779   GtkWidget *widget;
780   GdkRectangle clip;
781   GtkStateType state;
782
783   widget = GTK_WIDGET (expander);
784
785   get_expander_bounds (expander, &clip);
786
787   state = widget->state;
788   if (expander->priv->prelight)
789     {
790       state = GTK_STATE_PRELIGHT;
791
792       gtk_expander_paint_prelight (expander);
793     }
794
795   gtk_paint_expander (widget->style,
796                       widget->window,
797                       state,
798                       &clip,
799                       widget,
800                       "expander",
801                       clip.x + clip.width / 2,
802                       clip.y + clip.height / 2,
803                       expander->priv->expander_style);
804 }
805
806 static void
807 gtk_expander_paint_focus (GtkExpander  *expander,
808                           GdkRectangle *area)
809 {
810   GtkWidget *widget;
811   GtkExpanderPrivate *priv;
812   gint x, y, width, height;
813   gboolean interior_focus;
814   gint border_width;
815   gint focus_width;
816   gint focus_pad;
817   gint expander_size;
818   gint expander_spacing;
819   gboolean ltr;
820
821   widget = GTK_WIDGET (expander);
822   priv = expander->priv;
823
824   border_width = GTK_CONTAINER (widget)->border_width;
825
826   gtk_widget_style_get (widget,
827                         "interior-focus", &interior_focus,
828                         "focus-line-width", &focus_width,
829                         "focus-padding", &focus_pad,
830                         "expander-size", &expander_size,
831                         "expander-spacing", &expander_spacing,
832                         NULL);
833
834   ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
835   
836   width = height = 0;
837
838   if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
839     {
840       GtkAllocation label_allocation = priv->label_widget->allocation;
841
842       width  = label_allocation.width;
843       height = label_allocation.height;
844     }
845
846   width  += 2 * focus_pad + 2 * focus_width;
847   height += 2 * focus_pad + 2 * focus_width;
848
849   x = widget->allocation.x + border_width;
850   y = widget->allocation.y + border_width;
851
852   if (ltr)
853     {
854       if (interior_focus)
855         x += expander_spacing * 2 + expander_size;
856     }
857   else
858     {
859       x += widget->allocation.width - 2 * border_width
860         - expander_spacing * 2 - expander_size - width;
861     }
862
863   if (!interior_focus)
864     {
865       width += expander_size + 2 * expander_spacing;
866       height = MAX (height, expander_size + 2 * expander_spacing);
867     }
868       
869   gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
870                    area, widget, "expander",
871                    x, y, width, height);
872 }
873
874 static gboolean
875 gtk_expander_expose (GtkWidget      *widget,
876                      GdkEventExpose *event)
877 {
878   if (GTK_WIDGET_DRAWABLE (widget))
879     {
880       GtkExpander *expander = GTK_EXPANDER (widget);
881
882       gtk_expander_paint (expander);
883
884       if (GTK_WIDGET_HAS_FOCUS (expander))
885         gtk_expander_paint_focus (expander, &event->area);
886
887       GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
888     }
889
890   return FALSE;
891 }
892
893 static gboolean
894 gtk_expander_button_press (GtkWidget      *widget,
895                            GdkEventButton *event)
896 {
897   GtkExpander *expander = GTK_EXPANDER (widget);
898
899   if (event->button == 1 && event->window == expander->priv->event_window)
900     {
901       expander->priv->button_down = TRUE;
902       return TRUE;
903     }
904
905   return FALSE;
906 }
907
908 static gboolean
909 gtk_expander_button_release (GtkWidget      *widget,
910                              GdkEventButton *event)
911 {
912   GtkExpander *expander = GTK_EXPANDER (widget);
913
914   if (event->button == 1 && expander->priv->button_down)
915     {
916       gtk_widget_activate (widget);
917       expander->priv->button_down = FALSE;
918       return TRUE;
919     }
920
921   return FALSE;
922 }
923
924 static void
925 gtk_expander_grab_notify (GtkWidget *widget,
926                           gboolean   was_grabbed)
927 {
928   if (!was_grabbed)
929     GTK_EXPANDER (widget)->priv->button_down = FALSE;
930 }
931
932 static void
933 gtk_expander_state_changed (GtkWidget    *widget,
934                             GtkStateType  previous_state)
935 {
936   if (!GTK_WIDGET_IS_SENSITIVE (widget))
937     GTK_EXPANDER (widget)->priv->button_down = FALSE;
938 }
939
940 static void
941 gtk_expander_redraw_expander (GtkExpander *expander)
942 {
943   GtkWidget *widget;
944
945   widget = GTK_WIDGET (expander);
946
947   if (GTK_WIDGET_REALIZED (widget))
948     gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
949 }
950
951 static gboolean
952 gtk_expander_enter_notify (GtkWidget        *widget,
953                            GdkEventCrossing *event)
954 {
955   GtkExpander *expander = GTK_EXPANDER (widget);
956   GtkWidget *event_widget;
957
958   event_widget = gtk_get_event_widget ((GdkEvent *) event);
959
960   if (event_widget == widget &&
961       event->detail != GDK_NOTIFY_INFERIOR)
962     {
963       expander->priv->prelight = TRUE;
964
965       if (expander->priv->label_widget)
966         gtk_widget_set_state (expander->priv->label_widget, GTK_STATE_PRELIGHT);
967
968       gtk_expander_redraw_expander (expander);
969     }
970
971   return FALSE;
972 }
973
974 static gboolean
975 gtk_expander_leave_notify (GtkWidget        *widget,
976                            GdkEventCrossing *event)
977 {
978   GtkExpander *expander = GTK_EXPANDER (widget);
979   GtkWidget *event_widget;
980
981   event_widget = gtk_get_event_widget ((GdkEvent *) event);
982
983   if (event_widget == widget &&
984       event->detail != GDK_NOTIFY_INFERIOR)
985     {
986       expander->priv->prelight = FALSE;
987
988       if (expander->priv->label_widget)
989         gtk_widget_set_state (expander->priv->label_widget, GTK_STATE_NORMAL);
990
991       gtk_expander_redraw_expander (expander);
992     }
993
994   return FALSE;
995 }
996
997 static gboolean
998 expand_timeout (gpointer data)
999 {
1000   GtkExpander *expander = GTK_EXPANDER (data);
1001   GtkExpanderPrivate *priv = expander->priv;
1002
1003   priv->expand_timer = 0;
1004   gtk_expander_set_expanded (expander, TRUE);
1005
1006   return FALSE;
1007 }
1008
1009 static gboolean
1010 gtk_expander_drag_motion (GtkWidget        *widget,
1011                           GdkDragContext   *context,
1012                           gint              x,
1013                           gint              y,
1014                           guint             time)
1015 {
1016   GtkExpander *expander = GTK_EXPANDER (widget);
1017   GtkExpanderPrivate *priv = expander->priv;
1018
1019   if (!priv->expanded && !priv->expand_timer)
1020     {
1021       GtkSettings *settings;
1022       guint timeout;
1023
1024       settings = gtk_widget_get_settings (widget);
1025       g_object_get (settings, "gtk-timeout-expand", &timeout, NULL);
1026
1027       priv->expand_timer = g_timeout_add (timeout, (GSourceFunc) expand_timeout, expander);
1028     }
1029
1030   return TRUE;
1031 }
1032
1033 static void
1034 gtk_expander_drag_leave (GtkWidget      *widget,
1035                          GdkDragContext *context,
1036                          guint           time)
1037 {
1038   GtkExpander *expander = GTK_EXPANDER (widget);
1039   GtkExpanderPrivate *priv = expander->priv;
1040
1041   if (priv->expand_timer)
1042     {
1043       g_source_remove (priv->expand_timer);
1044       priv->expand_timer = 0;
1045     }
1046 }
1047
1048 typedef enum
1049 {
1050   FOCUS_NONE,
1051   FOCUS_WIDGET,
1052   FOCUS_LABEL,
1053   FOCUS_CHILD
1054 } FocusSite;
1055
1056 static gboolean
1057 focus_current_site (GtkExpander      *expander,
1058                     GtkDirectionType  direction)
1059 {
1060   GtkWidget *current_focus;
1061
1062   current_focus = GTK_CONTAINER (expander)->focus_child;
1063
1064   if (!current_focus)
1065     return FALSE;
1066
1067   return gtk_widget_child_focus (current_focus, direction);
1068 }
1069
1070 static gboolean
1071 focus_in_site (GtkExpander      *expander,
1072                FocusSite         site,
1073                GtkDirectionType  direction)
1074 {
1075   switch (site)
1076     {
1077     case FOCUS_WIDGET:
1078       gtk_widget_grab_focus (GTK_WIDGET (expander));
1079       return TRUE;
1080     case FOCUS_LABEL:
1081       if (expander->priv->label_widget)
1082         return gtk_widget_child_focus (expander->priv->label_widget, direction);
1083       else
1084         return FALSE;
1085     case FOCUS_CHILD:
1086       {
1087         GtkWidget *child = gtk_bin_get_child (GTK_BIN (expander));
1088
1089         if (child && GTK_WIDGET_CHILD_VISIBLE (child))
1090           return gtk_widget_child_focus (child, direction);
1091         else
1092           return FALSE;
1093       }
1094     case FOCUS_NONE:
1095       break;
1096     }
1097
1098   g_assert_not_reached ();
1099   return FALSE;
1100 }
1101
1102 static FocusSite
1103 get_next_site (GtkExpander      *expander,
1104                FocusSite         site,
1105                GtkDirectionType  direction)
1106 {
1107   gboolean ltr;
1108
1109   ltr = gtk_widget_get_direction (GTK_WIDGET (expander)) != GTK_TEXT_DIR_RTL;
1110
1111   switch (site)
1112     {
1113     case FOCUS_NONE:
1114       switch (direction)
1115         {
1116         case GTK_DIR_TAB_BACKWARD:
1117         case GTK_DIR_LEFT:
1118         case GTK_DIR_UP:
1119           return FOCUS_CHILD;
1120         case GTK_DIR_TAB_FORWARD:
1121         case GTK_DIR_DOWN:
1122         case GTK_DIR_RIGHT:
1123           return FOCUS_WIDGET;
1124         }
1125     case FOCUS_WIDGET:
1126       switch (direction)
1127         {
1128         case GTK_DIR_TAB_BACKWARD:
1129         case GTK_DIR_UP:
1130           return FOCUS_NONE;
1131         case GTK_DIR_LEFT:
1132           return ltr ? FOCUS_NONE : FOCUS_LABEL;
1133         case GTK_DIR_TAB_FORWARD:
1134         case GTK_DIR_DOWN:
1135           return FOCUS_LABEL;
1136         case GTK_DIR_RIGHT:
1137           return ltr ? FOCUS_LABEL : FOCUS_NONE;
1138           break;
1139         }
1140     case FOCUS_LABEL:
1141       switch (direction)
1142         {
1143         case GTK_DIR_TAB_BACKWARD:
1144         case GTK_DIR_UP:
1145           return FOCUS_WIDGET;
1146         case GTK_DIR_LEFT:
1147           return ltr ? FOCUS_WIDGET : FOCUS_CHILD;
1148         case GTK_DIR_TAB_FORWARD:
1149         case GTK_DIR_DOWN:
1150           return FOCUS_CHILD;
1151         case GTK_DIR_RIGHT:
1152           return ltr ? FOCUS_CHILD : FOCUS_WIDGET;
1153           break;
1154         }
1155     case FOCUS_CHILD:
1156       switch (direction)
1157         {
1158         case GTK_DIR_TAB_BACKWARD:
1159         case GTK_DIR_LEFT:
1160         case GTK_DIR_UP:
1161           return FOCUS_LABEL;
1162         case GTK_DIR_TAB_FORWARD:
1163         case GTK_DIR_DOWN:
1164         case GTK_DIR_RIGHT:
1165           return FOCUS_NONE;
1166         }
1167     }
1168
1169   g_assert_not_reached ();
1170   return FOCUS_NONE;
1171 }
1172
1173 static gboolean
1174 gtk_expander_focus (GtkWidget        *widget,
1175                     GtkDirectionType  direction)
1176 {
1177   GtkExpander *expander = GTK_EXPANDER (widget);
1178   
1179   if (!focus_current_site (expander, direction))
1180     {
1181       GtkWidget *old_focus_child;
1182       gboolean widget_is_focus;
1183       FocusSite site = FOCUS_NONE;
1184       
1185       widget_is_focus = gtk_widget_is_focus (widget);
1186       old_focus_child = GTK_CONTAINER (widget)->focus_child;
1187       
1188       if (old_focus_child && old_focus_child == expander->priv->label_widget)
1189         site = FOCUS_LABEL;
1190       else if (old_focus_child)
1191         site = FOCUS_CHILD;
1192       else if (widget_is_focus)
1193         site = FOCUS_WIDGET;
1194
1195       while ((site = get_next_site (expander, site, direction)) != FOCUS_NONE)
1196         {
1197           if (focus_in_site (expander, site, direction))
1198             return TRUE;
1199         }
1200
1201       return FALSE;
1202     }
1203
1204   return TRUE;
1205 }
1206
1207 static void
1208 gtk_expander_add (GtkContainer *container,
1209                   GtkWidget    *widget)
1210 {
1211   GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1212
1213   gtk_widget_set_child_visible (widget, GTK_EXPANDER (container)->priv->expanded);
1214   gtk_widget_queue_resize (GTK_WIDGET (container));
1215 }
1216
1217 static void
1218 gtk_expander_remove (GtkContainer *container,
1219                      GtkWidget    *widget)
1220 {
1221   GtkExpander *expander = GTK_EXPANDER (container);
1222
1223   if (GTK_EXPANDER (expander)->priv->label_widget == widget)
1224     gtk_expander_set_label_widget (expander, NULL);
1225   else
1226     GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1227 }
1228
1229 static void
1230 gtk_expander_forall (GtkContainer *container,
1231                      gboolean      include_internals,
1232                      GtkCallback   callback,
1233                      gpointer      callback_data)
1234 {
1235   GtkBin *bin = GTK_BIN (container);
1236   GtkExpanderPrivate *priv = GTK_EXPANDER (container)->priv;
1237
1238   if (bin->child)
1239     (* callback) (bin->child, callback_data);
1240
1241   if (priv->label_widget)
1242     (* callback) (priv->label_widget, callback_data);
1243 }
1244
1245 static void
1246 gtk_expander_activate (GtkExpander *expander)
1247 {
1248   gtk_expander_set_expanded (expander, !expander->priv->expanded);
1249 }
1250
1251 /**
1252  * gtk_expander_new:
1253  * @label: the text of the label
1254  * 
1255  * Creates a new expander using @label as the text of the label.
1256  * 
1257  * Return value: a new #GtkExpander widget.
1258  *
1259  * Since: 2.4
1260  **/
1261 GtkWidget *
1262 gtk_expander_new (const gchar *label)
1263 {
1264   return g_object_new (GTK_TYPE_EXPANDER, "label", label, NULL);
1265 }
1266
1267 /**
1268  * gtk_expander_new_with_mnemonic:
1269  * @label: the text of the label with an underscore in front of the
1270  *         mnemonic character
1271  * 
1272  * Creates a new expander using @label as the text of the label.
1273  * If characters in @label are preceded by an underscore, they are underlined.
1274  * If you need a literal underscore character in a label, use '__' (two 
1275  * underscores). The first underlined character represents a keyboard 
1276  * accelerator called a mnemonic.
1277  * Pressing Alt and that key activates the button.
1278  * 
1279  * Return value: a new #GtkExpander widget.
1280  *
1281  * Since: 2.4
1282  **/
1283 GtkWidget *
1284 gtk_expander_new_with_mnemonic (const gchar *label)
1285 {
1286   return g_object_new (GTK_TYPE_EXPANDER,
1287                        "label", label,
1288                        "use-underline", TRUE,
1289                        NULL);
1290 }
1291
1292 static gboolean
1293 gtk_expander_animation_timeout (GtkExpander *expander)
1294 {
1295   GtkExpanderPrivate *priv = expander->priv;
1296   GdkRectangle area;
1297   gboolean finish = FALSE;
1298
1299   GDK_THREADS_ENTER();
1300
1301   if (GTK_WIDGET_REALIZED (expander))
1302     {
1303       get_expander_bounds (expander, &area);
1304       gdk_window_invalidate_rect (GTK_WIDGET (expander)->window, &area, TRUE);
1305     }
1306
1307   if (priv->expanded)
1308     {
1309       if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1310         {
1311           priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1312         }
1313       else
1314         {
1315           priv->expander_style = GTK_EXPANDER_EXPANDED;
1316           finish = TRUE;
1317         }
1318     }
1319   else
1320     {
1321       if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1322         {
1323           priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1324         }
1325       else
1326         {
1327           priv->expander_style = GTK_EXPANDER_COLLAPSED;
1328           finish = TRUE;
1329         }
1330     }
1331
1332   if (finish)
1333     {
1334       priv->animation_timeout = 0;
1335       if (GTK_BIN (expander)->child)
1336         gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1337       gtk_widget_queue_resize (GTK_WIDGET (expander));
1338     }
1339
1340   GDK_THREADS_LEAVE();
1341
1342   return !finish;
1343 }
1344
1345 static void
1346 gtk_expander_start_animation (GtkExpander *expander)
1347 {
1348   GtkExpanderPrivate *priv = expander->priv;
1349
1350   if (priv->animation_timeout)
1351     g_source_remove (priv->animation_timeout);
1352
1353   priv->animation_timeout =
1354                 g_timeout_add (50,
1355                                (GSourceFunc) gtk_expander_animation_timeout,
1356                                expander);
1357 }
1358
1359 /**
1360  * gtk_expander_set_expanded:
1361  * @expander: a #GtkExpander
1362  * @expanded: whether the child widget is revealed
1363  *
1364  * Sets the state of the expander. Set to %TRUE, if you want
1365  * the child widget to be revealed, and %FALSE if you want the
1366  * child widget to be hidden.
1367  *
1368  * Since: 2.4
1369  **/
1370 void
1371 gtk_expander_set_expanded (GtkExpander *expander,
1372                            gboolean     expanded)
1373 {
1374   GtkExpanderPrivate *priv;
1375
1376   g_return_if_fail (GTK_IS_EXPANDER (expander));
1377
1378   priv = expander->priv;
1379
1380   expanded = expanded != FALSE;
1381
1382   if (priv->expanded != expanded)
1383     {
1384       GtkSettings *settings = gtk_widget_get_settings (GTK_WIDGET (expander));
1385       gboolean     enable_animations;
1386
1387       priv->expanded = expanded;
1388
1389       g_object_get (settings, "gtk-enable-animations", &enable_animations, NULL);
1390
1391       if (enable_animations && GTK_WIDGET_REALIZED (expander))
1392         {
1393           gtk_expander_start_animation (expander);
1394         }
1395       else
1396         {
1397           priv->expander_style = expanded ? GTK_EXPANDER_EXPANDED :
1398                                             GTK_EXPANDER_COLLAPSED;
1399
1400           if (GTK_BIN (expander)->child)
1401             {
1402               gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1403               gtk_widget_queue_resize (GTK_WIDGET (expander));
1404             }
1405         }
1406
1407       g_object_notify (G_OBJECT (expander), "expanded");
1408     }
1409 }
1410
1411 /**
1412  * gtk_expander_get_expanded:
1413  * @expander:a #GtkExpander
1414  *
1415  * Queries a #GtkExpander and returns its current state. Returns %TRUE
1416  * if the child widget is revealed.
1417  *
1418  * See gtk_expander_set_expanded().
1419  *
1420  * Return value: the current state of the expander.
1421  *
1422  * Since: 2.4
1423  **/
1424 gboolean
1425 gtk_expander_get_expanded (GtkExpander *expander)
1426 {
1427   g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1428
1429   return expander->priv->expanded;
1430 }
1431
1432 /**
1433  * gtk_expander_set_spacing:
1434  * @expander: a #GtkExpander
1435  * @spacing: distance between the expander and child in pixels.
1436  *
1437  * Sets the spacing field of @expander, which is the number of pixels to
1438  * place between expander and the child.
1439  *
1440  * Since: 2.4
1441  **/
1442 void
1443 gtk_expander_set_spacing (GtkExpander *expander,
1444                           gint         spacing)
1445 {
1446   g_return_if_fail (GTK_IS_EXPANDER (expander));
1447   g_return_if_fail (spacing >= 0);
1448
1449   if (expander->priv->spacing != spacing)
1450     {
1451       expander->priv->spacing = spacing;
1452
1453       gtk_widget_queue_resize (GTK_WIDGET (expander));
1454
1455       g_object_notify (G_OBJECT (expander), "spacing");
1456     }
1457 }
1458
1459 /**
1460  * gtk_expander_get_spacing:
1461  * @expander: a #GtkExpander
1462  *
1463  * Gets the value set by gtk_expander_set_spacing().
1464  *
1465  * Return value: spacing between the expander and child.
1466  *
1467  * Since: 2.4
1468  **/
1469 gint
1470 gtk_expander_get_spacing (GtkExpander *expander)
1471 {
1472   g_return_val_if_fail (GTK_IS_EXPANDER (expander), 0);
1473
1474   return expander->priv->spacing;
1475 }
1476
1477 /**
1478  * gtk_expander_set_label:
1479  * @expander: a #GtkExpander
1480  * @label: a string
1481  *
1482  * Sets the text of the label of the expander to @label.
1483  *
1484  * This will also clear any previously set labels.
1485  *
1486  * Since: 2.4
1487  **/
1488 void
1489 gtk_expander_set_label (GtkExpander *expander,
1490                         const gchar *label)
1491 {
1492   g_return_if_fail (GTK_IS_EXPANDER (expander));
1493
1494   if (!label)
1495     {
1496       gtk_expander_set_label_widget (expander, NULL);
1497     }
1498   else
1499     {
1500       GtkWidget *child;
1501
1502       child = gtk_label_new (label);
1503       gtk_label_set_use_underline (GTK_LABEL (child), expander->priv->use_underline);
1504       gtk_label_set_use_markup (GTK_LABEL (child), expander->priv->use_markup);
1505       gtk_widget_show (child);
1506
1507       gtk_expander_set_label_widget (expander, child);
1508     }
1509
1510   g_object_notify (G_OBJECT (expander), "label");
1511 }
1512
1513 /**
1514  * gtk_expander_get_label:
1515  * @expander: a #GtkExpander
1516  *
1517  * Fetches the text from the label of the expander, as set by
1518  * gtk_expander_set_label(). If the label text has not
1519  * been set the return value will be %NULL. This will be the
1520  * case if you create an empty button with gtk_button_new() to
1521  * use as a container.
1522  *
1523  * Return value: The text of the label widget. This string is owned
1524  * by the widget and must not be modified or freed.
1525  *
1526  * Since: 2.4
1527  **/
1528 G_CONST_RETURN char *
1529 gtk_expander_get_label (GtkExpander *expander)
1530 {
1531   GtkExpanderPrivate *priv;
1532
1533   g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1534
1535   priv = expander->priv;
1536
1537   if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1538     return gtk_label_get_text (GTK_LABEL (priv->label_widget));
1539   else
1540     return NULL;
1541 }
1542
1543 /**
1544  * gtk_expander_set_use_underline:
1545  * @expander: a #GtkExpander
1546  * @use_underline: %TRUE if underlines in the text indicate mnemonics
1547  *
1548  * If true, an underline in the text of the expander label indicates
1549  * the next character should be used for the mnemonic accelerator key.
1550  *
1551  * Since: 2.4
1552  **/
1553 void
1554 gtk_expander_set_use_underline (GtkExpander *expander,
1555                                 gboolean     use_underline)
1556 {
1557   GtkExpanderPrivate *priv;
1558
1559   g_return_if_fail (GTK_IS_EXPANDER (expander));
1560
1561   priv = expander->priv;
1562
1563   use_underline = use_underline != FALSE;
1564
1565   if (priv->use_underline != use_underline)
1566     {
1567       priv->use_underline = use_underline;
1568
1569       if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1570         gtk_label_set_use_underline (GTK_LABEL (priv->label_widget), use_underline);
1571
1572       g_object_notify (G_OBJECT (expander), "use-underline");
1573     }
1574 }
1575
1576 /**
1577  * gtk_expander_get_use_underline:
1578  * @expander: a #GtkExpander
1579  *
1580  * Returns whether an embedded underline in the expander label indicates a
1581  * mnemonic. See gtk_expander_set_use_underline().
1582  *
1583  * Return value: %TRUE if an embedded underline in the expander label
1584  *               indicates the mnemonic accelerator keys.
1585  *
1586  * Since: 2.4
1587  **/
1588 gboolean
1589 gtk_expander_get_use_underline (GtkExpander *expander)
1590 {
1591   g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1592
1593   return expander->priv->use_underline;
1594 }
1595
1596 /**
1597  * gtk_expander_set_use_markup:
1598  * @expander: a #GtkExpander
1599  * @use_markup: %TRUE if the label's text should be parsed for markup
1600  *
1601  * Sets whether the text of the label contains markup in <link
1602  * linkend="PangoMarkupFormat">Pango's text markup
1603  * language</link>. See gtk_label_set_markup().
1604  *
1605  * Since: 2.4
1606  **/
1607 void
1608 gtk_expander_set_use_markup (GtkExpander *expander,
1609                              gboolean     use_markup)
1610 {
1611   GtkExpanderPrivate *priv;
1612
1613   g_return_if_fail (GTK_IS_EXPANDER (expander));
1614
1615   priv = expander->priv;
1616
1617   use_markup = use_markup != FALSE;
1618
1619   if (priv->use_markup != use_markup)
1620     {
1621       priv->use_markup = use_markup;
1622
1623       if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1624         gtk_label_set_use_markup (GTK_LABEL (priv->label_widget), use_markup);
1625
1626       g_object_notify (G_OBJECT (expander), "use-markup");
1627     }
1628 }
1629
1630 /**
1631  * gtk_expander_get_use_markup:
1632  * @expander: a #GtkExpander
1633  *
1634  * Returns whether the label's text is interpreted as marked up with
1635  * the <link linkend="PangoMarkupFormat">Pango text markup
1636  * language</link>. See gtk_expander_set_use_markup ().
1637  *
1638  * Return value: %TRUE if the label's text will be parsed for markup
1639  *
1640  * Since: 2.4
1641  **/
1642 gboolean
1643 gtk_expander_get_use_markup (GtkExpander *expander)
1644 {
1645   g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1646
1647   return expander->priv->use_markup;
1648 }
1649
1650 /**
1651  * gtk_expander_set_label_widget:
1652  * @expander: a #GtkExpander
1653  * @label_widget: the new label widget
1654  *
1655  * Set the label widget for the expander. This is the widget
1656  * that will appear embedded alongside the expander arrow.
1657  *
1658  * Since: 2.4
1659  **/
1660 void
1661 gtk_expander_set_label_widget (GtkExpander *expander,
1662                                GtkWidget   *label_widget)
1663 {
1664   GtkExpanderPrivate *priv;
1665
1666   g_return_if_fail (GTK_IS_EXPANDER (expander));
1667   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1668   g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1669
1670   priv = expander->priv;
1671
1672   if (priv->label_widget == label_widget)
1673     return;
1674
1675   if (priv->label_widget)
1676     {
1677       gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1678       gtk_widget_unparent (priv->label_widget);
1679     }
1680
1681   priv->label_widget = label_widget;
1682
1683   if (label_widget)
1684     {
1685       priv->label_widget = label_widget;
1686
1687       gtk_widget_set_parent (label_widget, GTK_WIDGET (expander));
1688
1689       if (priv->prelight)
1690         gtk_widget_set_state (label_widget, GTK_STATE_PRELIGHT);
1691     }
1692
1693   if (GTK_WIDGET_VISIBLE (expander))
1694     gtk_widget_queue_resize (GTK_WIDGET (expander));
1695
1696   g_object_freeze_notify (G_OBJECT (expander));
1697   g_object_notify (G_OBJECT (expander), "label-widget");
1698   g_object_notify (G_OBJECT (expander), "label");
1699   g_object_thaw_notify (G_OBJECT (expander));
1700 }
1701
1702 /**
1703  * gtk_expander_get_label_widget:
1704  * @expander: a #GtkExpander
1705  *
1706  * Retrieves the label widget for the frame. See
1707  * gtk_expander_set_label_widget().
1708  *
1709  * Return value: the label widget, or %NULL if there is none.
1710  * 
1711  * Since: 2.4
1712  **/
1713 GtkWidget *
1714 gtk_expander_get_label_widget (GtkExpander *expander)
1715 {
1716   g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1717
1718   return expander->priv->label_widget;
1719 }
1720
1721 #define __GTK_EXPANDER_C__
1722 #include "gtkaliasdef.c"