1 /* GTK - The GIMP Toolkit
3 * Copyright (C) 2003 Sun Microsystems, Inc.
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.
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.
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.
21 * Mark McLoughlin <mark@skynet.ie>
26 #include "gtkexpander.h"
29 #include "gtkcontainer.h"
30 #include "gtkmarshalers.h"
33 #include "gtkprivate.h"
34 #include <gdk/gdkkeysyms.h>
36 #define GTK_EXPANDER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_EXPANDER, GtkExpanderPrivate))
38 #define DEFAULT_EXPANDER_SIZE 10
39 #define DEFAULT_EXPANDER_SPACING 2
52 struct _GtkExpanderPrivate
54 GtkWidget *label_widget;
55 GdkWindow *event_window;
58 GtkExpanderStyle expander_style;
59 guint animation_timeout;
62 guint use_underline : 1;
64 guint button_down : 1;
68 static void gtk_expander_class_init (GtkExpanderClass *klass);
69 static void gtk_expander_init (GtkExpander *expander);
71 static void gtk_expander_set_property (GObject *object,
75 static void gtk_expander_get_property (GObject *object,
80 static void gtk_expander_destroy (GtkObject *object);
82 static void gtk_expander_realize (GtkWidget *widget);
83 static void gtk_expander_unrealize (GtkWidget *widget);
84 static void gtk_expander_size_request (GtkWidget *widget,
85 GtkRequisition *requisition);
86 static void gtk_expander_size_allocate (GtkWidget *widget,
87 GtkAllocation *allocation);
88 static void gtk_expander_map (GtkWidget *widget);
89 static void gtk_expander_unmap (GtkWidget *widget);
90 static gboolean gtk_expander_expose (GtkWidget *widget,
91 GdkEventExpose *event);
92 static gboolean gtk_expander_button_press (GtkWidget *widget,
93 GdkEventButton *event);
94 static gboolean gtk_expander_button_release (GtkWidget *widget,
95 GdkEventButton *event);
96 static gboolean gtk_expander_enter_notify (GtkWidget *widget,
97 GdkEventCrossing *event);
98 static gboolean gtk_expander_leave_notify (GtkWidget *widget,
99 GdkEventCrossing *event);
100 static gboolean gtk_expander_focus (GtkWidget *widget,
101 GtkDirectionType direction);
102 static void gtk_expander_grab_notify (GtkWidget *widget,
103 gboolean was_grabbed);
104 static void gtk_expander_state_changed (GtkWidget *widget,
105 GtkStateType previous_state);
107 static void gtk_expander_add (GtkContainer *container,
109 static void gtk_expander_remove (GtkContainer *container,
111 static void gtk_expander_forall (GtkContainer *container,
112 gboolean include_internals,
113 GtkCallback callback,
114 gpointer callback_data);
116 static void gtk_expander_activate (GtkExpander *expander);
118 static GtkBinClass *parent_class = NULL;
121 gtk_expander_get_type (void)
123 static GType expander_type = 0;
127 static const GTypeInfo expander_info =
129 sizeof (GtkExpanderClass),
130 NULL, /* base_init */
131 NULL, /* base_finalize */
132 (GClassInitFunc) gtk_expander_class_init,
133 NULL, /* class_finalize */
134 NULL, /* class_data */
135 sizeof (GtkExpander),
137 (GInstanceInitFunc) gtk_expander_init,
140 expander_type = g_type_register_static (GTK_TYPE_BIN,
145 return expander_type;
149 gtk_expander_class_init (GtkExpanderClass *klass)
151 GObjectClass *gobject_class;
152 GtkObjectClass *object_class;
153 GtkWidgetClass *widget_class;
154 GtkContainerClass *container_class;
156 parent_class = g_type_class_peek_parent (klass);
158 gobject_class = (GObjectClass *) klass;
159 object_class = (GtkObjectClass *) klass;
160 widget_class = (GtkWidgetClass *) klass;
161 container_class = (GtkContainerClass *) klass;
163 gobject_class->set_property = gtk_expander_set_property;
164 gobject_class->get_property = gtk_expander_get_property;
166 object_class->destroy = gtk_expander_destroy;
168 widget_class->realize = gtk_expander_realize;
169 widget_class->unrealize = gtk_expander_unrealize;
170 widget_class->size_request = gtk_expander_size_request;
171 widget_class->size_allocate = gtk_expander_size_allocate;
172 widget_class->map = gtk_expander_map;
173 widget_class->unmap = gtk_expander_unmap;
174 widget_class->expose_event = gtk_expander_expose;
175 widget_class->button_press_event = gtk_expander_button_press;
176 widget_class->button_release_event = gtk_expander_button_release;
177 widget_class->enter_notify_event = gtk_expander_enter_notify;
178 widget_class->leave_notify_event = gtk_expander_leave_notify;
179 widget_class->focus = gtk_expander_focus;
180 widget_class->grab_notify = gtk_expander_grab_notify;
181 widget_class->state_changed = gtk_expander_state_changed;
183 container_class->add = gtk_expander_add;
184 container_class->remove = gtk_expander_remove;
185 container_class->forall = gtk_expander_forall;
187 klass->activate = gtk_expander_activate;
189 g_type_class_add_private (klass, sizeof (GtkExpanderPrivate));
191 g_object_class_install_property (gobject_class,
193 g_param_spec_boolean ("expanded",
195 P_("Whether the expander has been opened to reveal the child widget"),
197 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
199 g_object_class_install_property (gobject_class,
201 g_param_spec_string ("label",
203 P_("Text of the expander's label"),
205 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
207 g_object_class_install_property (gobject_class,
209 g_param_spec_boolean ("use_underline",
211 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
213 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
215 g_object_class_install_property (gobject_class,
217 g_param_spec_boolean ("use_markup",
219 P_("The text of the label includes XML markup. See pango_parse_markup()"),
221 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
223 g_object_class_install_property (gobject_class,
225 g_param_spec_int ("spacing",
227 P_("Space to put between the label and the child"),
233 g_object_class_install_property (gobject_class,
235 g_param_spec_object ("label_widget",
237 P_("A widget to display in place of the usual expander label"),
241 gtk_widget_class_install_style_property (widget_class,
242 g_param_spec_int ("expander-size",
244 P_("Size of the expander arrow"),
247 DEFAULT_EXPANDER_SIZE,
250 gtk_widget_class_install_style_property (widget_class,
251 g_param_spec_int ("expander-spacing",
252 P_("Indicator Spacing"),
253 P_("Spacing around expander arrow"),
256 DEFAULT_EXPANDER_SPACING,
259 widget_class->activate_signal =
260 g_signal_new ("activate",
261 G_TYPE_FROM_CLASS (gobject_class),
262 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
263 G_STRUCT_OFFSET (GtkExpanderClass, activate),
265 _gtk_marshal_VOID__VOID,
270 gtk_expander_init (GtkExpander *expander)
272 GtkExpanderPrivate *priv;
274 expander->priv = priv = GTK_EXPANDER_GET_PRIVATE (expander);
276 GTK_WIDGET_SET_FLAGS (expander, GTK_CAN_FOCUS);
277 GTK_WIDGET_SET_FLAGS (expander, GTK_NO_WINDOW);
279 priv->label_widget = NULL;
280 priv->event_window = NULL;
283 priv->expander_style = GTK_EXPANDER_COLLAPSED;
284 priv->animation_timeout = 0;
286 priv->expanded = FALSE;
287 priv->use_underline = FALSE;
288 priv->use_markup = FALSE;
289 priv->button_down = FALSE;
290 priv->prelight = FALSE;
294 gtk_expander_set_property (GObject *object,
299 GtkExpander *expander = GTK_EXPANDER (object);
304 gtk_expander_set_expanded (expander, g_value_get_boolean (value));
307 gtk_expander_set_label (expander, g_value_get_string (value));
309 case PROP_USE_UNDERLINE:
310 gtk_expander_set_use_underline (expander, g_value_get_boolean (value));
312 case PROP_USE_MARKUP:
313 gtk_expander_set_use_markup (expander, g_value_get_boolean (value));
316 gtk_expander_set_spacing (expander, g_value_get_int (value));
318 case PROP_LABEL_WIDGET:
319 gtk_expander_set_label_widget (expander, g_value_get_object (value));
322 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328 gtk_expander_get_property (GObject *object,
333 GtkExpander *expander = GTK_EXPANDER (object);
334 GtkExpanderPrivate *priv = expander->priv;
339 g_value_set_boolean (value, priv->expanded);
342 g_value_set_string (value, gtk_expander_get_label (expander));
344 case PROP_USE_UNDERLINE:
345 g_value_set_boolean (value, priv->use_underline);
347 case PROP_USE_MARKUP:
348 g_value_set_boolean (value, priv->use_markup);
351 g_value_set_int (value, priv->spacing);
353 case PROP_LABEL_WIDGET:
354 g_value_set_object (value,
356 G_OBJECT (priv->label_widget) : NULL);
359 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
365 gtk_expander_destroy (GtkObject *object)
367 GtkExpanderPrivate *priv = GTK_EXPANDER (object)->priv;
369 if (priv->animation_timeout)
371 g_source_remove (priv->animation_timeout);
372 priv->animation_timeout = 0;
375 GTK_OBJECT_CLASS (parent_class)->destroy (object);
379 gtk_expander_realize (GtkWidget *widget)
381 GtkExpanderPrivate *priv;
382 GdkWindowAttr attributes;
383 gint attributes_mask;
386 priv = GTK_EXPANDER (widget)->priv;
387 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
389 border_width = GTK_CONTAINER (widget)->border_width;
391 attributes.window_type = GDK_WINDOW_CHILD;
392 attributes.x = widget->allocation.x + border_width;
393 attributes.y = widget->allocation.y + border_width;
394 attributes.width = widget->allocation.width - 2 * border_width;
395 attributes.height = widget->allocation.height - 2 * border_width;
396 attributes.wclass = GDK_INPUT_ONLY;
397 attributes.event_mask = gtk_widget_get_events (widget) |
398 GDK_BUTTON_PRESS_MASK |
399 GDK_BUTTON_RELEASE_MASK |
400 GDK_ENTER_NOTIFY_MASK |
401 GDK_LEAVE_NOTIFY_MASK;
403 attributes_mask = GDK_WA_X | GDK_WA_Y;
405 widget->window = gtk_widget_get_parent_window (widget);
406 g_object_ref (widget->window);
408 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
409 &attributes, attributes_mask);
410 gdk_window_set_user_data (priv->event_window, widget);
412 widget->style = gtk_style_attach (widget->style, widget->window);
413 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
417 gtk_expander_unrealize (GtkWidget *widget)
419 GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
421 if (priv->event_window)
423 gdk_window_set_user_data (priv->event_window, NULL);
424 gdk_window_destroy (priv->event_window);
425 priv->event_window = NULL;
428 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
432 gtk_expander_size_request (GtkWidget *widget,
433 GtkRequisition *requisition)
435 GtkExpander *expander;
437 GtkExpanderPrivate *priv;
440 gint expander_spacing;
441 gboolean interior_focus;
445 bin = GTK_BIN (widget);
446 expander = GTK_EXPANDER (widget);
447 priv = expander->priv;
449 border_width = GTK_CONTAINER (widget)->border_width;
451 gtk_widget_style_get (widget,
452 "interior-focus", &interior_focus,
453 "focus-line-width", &focus_width,
454 "focus-padding", &focus_pad,
455 "expander-size", &expander_size,
456 "expander-spacing", &expander_spacing,
459 requisition->width = expander_size + 2 * expander_spacing +
460 2 * focus_width + 2 * focus_pad;
461 requisition->height = interior_focus ? (2 * focus_width + 2 * focus_pad) : 0;
463 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
465 GtkRequisition label_requisition;
467 gtk_widget_size_request (priv->label_widget, &label_requisition);
469 requisition->width += label_requisition.width;
470 requisition->height += label_requisition.height;
473 requisition->height = MAX (expander_size + 2 * expander_spacing, requisition->height);
476 requisition->height += 2 * focus_width + 2 * focus_pad;
478 if (bin->child && GTK_WIDGET_CHILD_VISIBLE (bin->child))
480 GtkRequisition child_requisition;
482 gtk_widget_size_request (bin->child, &child_requisition);
484 requisition->width = MAX (requisition->width, child_requisition.width);
485 requisition->height += child_requisition.height + priv->spacing;
488 requisition->width += 2 * border_width;
489 requisition->height += 2 * border_width;
493 get_expander_bounds (GtkExpander *expander,
498 GtkExpanderPrivate *priv;
501 gint expander_spacing;
502 gboolean interior_focus;
507 widget = GTK_WIDGET (expander);
508 bin = GTK_BIN (expander);
509 priv = expander->priv;
511 border_width = GTK_CONTAINER (expander)->border_width;
513 gtk_widget_style_get (widget,
514 "interior-focus", &interior_focus,
515 "focus-line-width", &focus_width,
516 "focus-padding", &focus_pad,
517 "expander-size", &expander_size,
518 "expander-spacing", &expander_spacing,
521 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
523 rect->x = widget->allocation.x + border_width;
524 rect->y = widget->allocation.y + border_width;
527 rect->x += expander_spacing;
529 rect->x += widget->allocation.width - 2 * border_width -
530 expander_spacing - expander_size;
532 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
534 GtkAllocation label_allocation;
536 label_allocation = priv->label_widget->allocation;
538 if (expander_size < label_allocation.height)
539 rect->y += focus_width + focus_pad + (label_allocation.height - expander_size) / 2;
541 rect->y += expander_spacing;
545 rect->y += expander_spacing;
551 rect->x += focus_width + focus_pad;
553 rect->x -= focus_width + focus_pad;
554 rect->y += focus_width + focus_pad;
557 rect->width = rect->height = expander_size;
561 gtk_expander_size_allocate (GtkWidget *widget,
562 GtkAllocation *allocation)
564 GtkExpander *expander;
566 GtkExpanderPrivate *priv;
567 GtkRequisition child_requisition;
568 gboolean child_visible = FALSE;
571 gint expander_spacing;
572 gboolean interior_focus;
577 expander = GTK_EXPANDER (widget);
578 bin = GTK_BIN (widget);
579 priv = expander->priv;
581 border_width = GTK_CONTAINER (widget)->border_width;
583 gtk_widget_style_get (widget,
584 "interior-focus", &interior_focus,
585 "focus-line-width", &focus_width,
586 "focus-padding", &focus_pad,
587 "expander-size", &expander_size,
588 "expander-spacing", &expander_spacing,
591 child_requisition.width = 0;
592 child_requisition.height = 0;
593 if (bin->child && GTK_WIDGET_CHILD_VISIBLE (bin->child))
595 child_visible = TRUE;
596 gtk_widget_get_child_requisition (bin->child, &child_requisition);
599 widget->allocation = *allocation;
601 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
603 GtkAllocation label_allocation;
604 GtkRequisition label_requisition;
607 gtk_widget_get_child_requisition (priv->label_widget, &label_requisition);
609 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
611 label_allocation.x = widget->allocation.x + border_width + focus_width + focus_pad;
613 label_allocation.x += expander_size + 2 * expander_spacing;
614 label_allocation.y = widget->allocation.y + border_width + focus_width + focus_pad;
616 label_allocation.width = MIN (label_requisition.width,
617 allocation->width - 2 * border_width -
618 expander_size - 2 * expander_spacing -
619 2 * focus_width - 2 * focus_pad);
620 label_allocation.width = MAX (label_allocation.width, 1);
622 label_allocation.height = MIN (label_requisition.height,
623 allocation->height - 2 * border_width -
624 2 * focus_width - 2 * focus_pad -
625 (child_visible ? priv->spacing : 0));
626 label_allocation.height = MAX (label_allocation.height, 1);
628 gtk_widget_size_allocate (priv->label_widget, &label_allocation);
630 label_height = label_allocation.height;
637 if (GTK_WIDGET_REALIZED (widget))
641 get_expander_bounds (expander, &rect);
643 gdk_window_move_resize (priv->event_window,
644 allocation->x + border_width, rect.y,
645 MAX (allocation->width - 2 * border_width, 1), rect.width);
650 GtkAllocation child_allocation;
653 top_height = MAX (2 * expander_spacing + expander_size,
655 (interior_focus ? 2 * focus_width + 2 * focus_pad : 0));
657 child_allocation.x = widget->allocation.x + border_width;
658 child_allocation.y = widget->allocation.y + border_width + top_height + priv->spacing;
661 child_allocation.y += 2 * focus_width + 2 * focus_pad;
663 child_allocation.width = MAX (allocation->width - 2 * border_width, 1);
665 child_allocation.height = allocation->height - top_height -
666 2 * border_width - priv->spacing -
667 (!interior_focus ? 2 * focus_width + 2 * focus_pad : 0);
668 child_allocation.height = MAX (child_allocation.height, 1);
670 gtk_widget_size_allocate (bin->child, &child_allocation);
675 gtk_expander_map (GtkWidget *widget)
677 GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
679 if (priv->label_widget)
680 gtk_widget_map (priv->label_widget);
682 GTK_WIDGET_CLASS (parent_class)->map (widget);
684 if (priv->event_window)
685 gdk_window_show (priv->event_window);
689 gtk_expander_unmap (GtkWidget *widget)
691 GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
693 if (priv->event_window)
694 gdk_window_hide (priv->event_window);
696 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
698 if (priv->label_widget)
699 gtk_widget_unmap (priv->label_widget);
703 gtk_expander_paint_prelight (GtkExpander *expander)
706 GtkContainer *container;
707 GtkExpanderPrivate *priv;
709 gboolean interior_focus;
713 int expander_spacing;
715 priv = expander->priv;
716 widget = GTK_WIDGET (expander);
717 container = GTK_CONTAINER (expander);
719 gtk_widget_style_get (widget,
720 "interior-focus", &interior_focus,
721 "focus-line-width", &focus_width,
722 "focus-padding", &focus_pad,
723 "expander-size", &expander_size,
724 "expander-spacing", &expander_spacing,
727 area.x = widget->allocation.x + container->border_width;
728 area.y = widget->allocation.y + container->border_width;
729 area.width = widget->allocation.width - (2 * container->border_width);
731 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
732 area.height = priv->label_widget->allocation.height;
736 area.height += interior_focus ? (focus_width + focus_pad) * 2 : 0;
737 area.height = MAX (area.height, expander_size + 2 * expander_spacing);
738 area.height += !interior_focus ? (focus_width + focus_pad) * 2 : 0;
740 gtk_paint_flat_box (widget->style, widget->window,
742 GTK_SHADOW_ETCHED_OUT,
743 &area, widget, "expander",
745 area.width, area.height);
749 gtk_expander_paint (GtkExpander *expander)
755 widget = GTK_WIDGET (expander);
757 get_expander_bounds (expander, &clip);
759 state = widget->state;
760 if (expander->priv->prelight)
762 state = GTK_STATE_PRELIGHT;
764 gtk_expander_paint_prelight (expander);
767 gtk_paint_expander (widget->style,
773 clip.x + clip.width / 2,
774 clip.y + clip.height / 2,
775 expander->priv->expander_style);
779 gtk_expander_paint_focus (GtkExpander *expander,
783 GtkExpanderPrivate *priv;
784 gint x, y, width, height;
785 gboolean interior_focus;
790 gint expander_spacing;
793 widget = GTK_WIDGET (expander);
794 priv = expander->priv;
796 border_width = GTK_CONTAINER (widget)->border_width;
798 gtk_widget_style_get (widget,
799 "interior-focus", &interior_focus,
800 "focus-line-width", &focus_width,
801 "focus-padding", &focus_pad,
802 "expander-size", &expander_size,
803 "expander-spacing", &expander_spacing,
806 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
808 x = widget->allocation.x + border_width;
809 y = widget->allocation.y + border_width;
811 if (ltr && interior_focus)
812 x += expander_spacing * 2 + expander_size;
816 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
818 GtkAllocation label_allocation = priv->label_widget->allocation;
820 width = label_allocation.width;
821 height = label_allocation.height;
826 width += expander_size + 2 * expander_spacing;
827 height = MAX (height, expander_size + 2 * expander_spacing);
830 width += 2 * focus_pad + 2 * focus_width;
831 height += 2 * focus_pad + 2 * focus_width;
833 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
834 area, widget, "expander",
835 x, y, width, height);
839 gtk_expander_expose (GtkWidget *widget,
840 GdkEventExpose *event)
842 if (GTK_WIDGET_DRAWABLE (widget))
844 GtkExpander *expander = GTK_EXPANDER (widget);
846 gtk_expander_paint (expander);
848 if (GTK_WIDGET_HAS_FOCUS (expander))
849 gtk_expander_paint_focus (expander, &event->area);
851 if (expander->priv->label_widget)
852 gtk_container_propagate_expose (GTK_CONTAINER (widget),
853 expander->priv->label_widget,
856 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
863 gtk_expander_button_press (GtkWidget *widget,
864 GdkEventButton *event)
866 GtkExpander *expander = GTK_EXPANDER (widget);
868 if (event->button == 1 && event->window == expander->priv->event_window)
870 expander->priv->button_down = TRUE;
878 gtk_expander_button_release (GtkWidget *widget,
879 GdkEventButton *event)
881 GtkExpander *expander = GTK_EXPANDER (widget);
883 if (event->button == 1 && expander->priv->button_down)
885 gtk_widget_activate (widget);
886 expander->priv->button_down = FALSE;
894 gtk_expander_grab_notify (GtkWidget *widget,
895 gboolean was_grabbed)
898 GTK_EXPANDER (widget)->priv->button_down = FALSE;
902 gtk_expander_state_changed (GtkWidget *widget,
903 GtkStateType previous_state)
905 if (!GTK_WIDGET_IS_SENSITIVE (widget))
906 GTK_EXPANDER (widget)->priv->button_down = FALSE;
910 gtk_expander_redraw_expander (GtkExpander *expander)
914 widget = GTK_WIDGET (expander);
916 if (GTK_WIDGET_REALIZED (widget))
917 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
921 gtk_expander_enter_notify (GtkWidget *widget,
922 GdkEventCrossing *event)
924 GtkExpander *expander = GTK_EXPANDER (widget);
925 GtkWidget *event_widget;
927 event_widget = gtk_get_event_widget ((GdkEvent *) event);
929 if (event_widget == widget &&
930 event->detail != GDK_NOTIFY_INFERIOR)
932 expander->priv->prelight = TRUE;
933 gtk_expander_redraw_expander (expander);
940 gtk_expander_leave_notify (GtkWidget *widget,
941 GdkEventCrossing *event)
943 GtkExpander *expander = GTK_EXPANDER (widget);
944 GtkWidget *event_widget;
946 event_widget = gtk_get_event_widget ((GdkEvent *) event);
948 if (event_widget == widget &&
949 event->detail != GDK_NOTIFY_INFERIOR)
951 expander->priv->prelight = FALSE;
952 gtk_expander_redraw_expander (expander);
967 focus_current_site (GtkExpander *expander,
968 GtkDirectionType direction)
970 GtkWidget *current_focus;
972 current_focus = GTK_CONTAINER (expander)->focus_child;
977 return gtk_widget_child_focus (current_focus, direction);
981 focus_in_site (GtkExpander *expander,
983 GtkDirectionType direction)
988 gtk_widget_grab_focus (GTK_WIDGET (expander));
991 if (expander->priv->label_widget)
992 return gtk_widget_child_focus (expander->priv->label_widget, direction);
997 GtkWidget *child = gtk_bin_get_child (GTK_BIN (expander));
999 if (child && GTK_WIDGET_CHILD_VISIBLE (child))
1000 return gtk_widget_child_focus (child, direction);
1008 g_assert_not_reached ();
1013 get_next_site (GtkExpander *expander,
1015 GtkDirectionType direction)
1019 ltr = gtk_widget_get_direction (GTK_WIDGET (expander)) != GTK_TEXT_DIR_RTL;
1026 case GTK_DIR_TAB_BACKWARD:
1030 case GTK_DIR_TAB_FORWARD:
1033 return FOCUS_WIDGET;
1038 case GTK_DIR_TAB_BACKWARD:
1042 return ltr ? FOCUS_NONE : FOCUS_LABEL;
1043 case GTK_DIR_TAB_FORWARD:
1047 return ltr ? FOCUS_LABEL : FOCUS_NONE;
1053 case GTK_DIR_TAB_BACKWARD:
1055 return FOCUS_WIDGET;
1057 return ltr ? FOCUS_WIDGET : FOCUS_CHILD;
1058 case GTK_DIR_TAB_FORWARD:
1062 return ltr ? FOCUS_CHILD : FOCUS_WIDGET;
1068 case GTK_DIR_TAB_BACKWARD:
1072 case GTK_DIR_TAB_FORWARD:
1079 g_assert_not_reached ();
1084 gtk_expander_focus (GtkWidget *widget,
1085 GtkDirectionType direction)
1087 GtkExpander *expander = GTK_EXPANDER (widget);
1089 if (!focus_current_site (expander, direction))
1091 GtkWidget *old_focus_child;
1092 gboolean widget_is_focus;
1093 FocusSite site = FOCUS_NONE;
1095 widget_is_focus = gtk_widget_is_focus (widget);
1096 old_focus_child = GTK_CONTAINER (widget)->focus_child;
1098 if (old_focus_child && old_focus_child == expander->priv->label_widget)
1100 else if (old_focus_child)
1102 else if (widget_is_focus)
1103 site = FOCUS_WIDGET;
1105 while ((site = get_next_site (expander, site, direction)) != FOCUS_NONE)
1107 if (focus_in_site (expander, site, direction))
1118 gtk_expander_add (GtkContainer *container,
1121 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1123 gtk_widget_set_child_visible (widget, GTK_EXPANDER (container)->priv->expanded);
1124 gtk_widget_queue_resize (GTK_WIDGET (container));
1128 gtk_expander_remove (GtkContainer *container,
1131 GtkExpander *expander = GTK_EXPANDER (container);
1133 if (GTK_EXPANDER (expander)->priv->label_widget == widget)
1134 gtk_expander_set_label_widget (expander, NULL);
1136 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1140 gtk_expander_forall (GtkContainer *container,
1141 gboolean include_internals,
1142 GtkCallback callback,
1143 gpointer callback_data)
1145 GtkBin *bin = GTK_BIN (container);
1146 GtkExpanderPrivate *priv = GTK_EXPANDER (container)->priv;
1149 (* callback) (bin->child, callback_data);
1151 if (priv->label_widget)
1152 (* callback) (priv->label_widget, callback_data);
1156 gtk_expander_activate (GtkExpander *expander)
1158 gtk_expander_set_expanded (expander, !expander->priv->expanded);
1164 * @label: the text of the label
1166 * Creates a new expander using @label as the text of the label.
1168 * Return value: a new #GtkExpander widget.
1173 gtk_expander_new (const gchar *label)
1175 return g_object_new (GTK_TYPE_EXPANDER, "label", label, NULL);
1179 * gtk_expander_new_with_mnemonic:
1180 * @label: the text of the label with an underscore in front of the
1181 * mnemonic character
1183 * Creates a new expander using @label as the text of the label.
1184 * If characters in @label are preceded by an underscore, they are underlined.
1185 * If you need a literal underscore character in a label, use '__' (two
1186 * underscores). The first underlined character represents a keyboard
1187 * accelerator called a mnemonic.
1188 * Pressing Alt and that key activates the button.
1190 * Return value: a new #GtkExpander widget.
1195 gtk_expander_new_with_mnemonic (const gchar *label)
1197 return g_object_new (GTK_TYPE_EXPANDER,
1199 "use_underline", TRUE,
1204 gtk_expander_animation_timeout (GtkExpander *expander)
1206 GtkExpanderPrivate *priv = expander->priv;
1208 gboolean finish = FALSE;
1210 GDK_THREADS_ENTER();
1212 if (GTK_WIDGET_REALIZED (expander))
1214 get_expander_bounds (expander, &area);
1215 gdk_window_invalidate_rect (GTK_WIDGET (expander)->window, &area, TRUE);
1220 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1222 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1226 priv->expander_style = GTK_EXPANDER_EXPANDED;
1232 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1234 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1238 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1245 priv->animation_timeout = 0;
1246 if (GTK_BIN (expander)->child)
1247 gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1248 gtk_widget_queue_resize (GTK_WIDGET (expander));
1251 GDK_THREADS_LEAVE();
1257 gtk_expander_start_animation (GtkExpander *expander)
1259 GtkExpanderPrivate *priv = expander->priv;
1261 if (priv->animation_timeout)
1262 g_source_remove (priv->animation_timeout);
1264 priv->animation_timeout =
1266 (GSourceFunc) gtk_expander_animation_timeout,
1271 * gtk_expander_set_expanded:
1272 * @expander: a #GtkExpander
1273 * @expanded: whether the child widget is revealed
1275 * Sets the state of the expander. Set to %TRUE, if you want
1276 * the child widget to be revealed, and %FALSE if you want the
1277 * child widget to be hidden.
1282 gtk_expander_set_expanded (GtkExpander *expander,
1285 GtkExpanderPrivate *priv;
1287 g_return_if_fail (GTK_IS_EXPANDER (expander));
1289 priv = expander->priv;
1291 expanded = expanded != FALSE;
1293 if (priv->expanded != expanded)
1295 priv->expanded = expanded;
1297 if (GTK_WIDGET_REALIZED (expander))
1299 gtk_expander_start_animation (expander);
1303 priv->expander_style = expanded ? GTK_EXPANDER_EXPANDED :
1304 GTK_EXPANDER_COLLAPSED;
1306 if (GTK_BIN (expander)->child)
1308 gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1309 gtk_widget_queue_resize (GTK_WIDGET (expander));
1313 g_object_notify (G_OBJECT (expander), "expanded");
1318 * gtk_expander_get_expanded:
1319 * @expander:a #GtkExpander
1321 * Queries a #GtkExpander and returns its current state. Returns %TRUE
1322 * if the child widget is revealed.
1324 * See gtk_expander_set_expanded().
1326 * Return value: the current state of the expander.
1331 gtk_expander_get_expanded (GtkExpander *expander)
1333 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1335 return expander->priv->expanded;
1339 * gtk_expander_set_spacing:
1340 * @expander: a #GtkExpander
1341 * @spacing: distance between the expander and child in pixels.
1343 * Sets the spacing field of @expander, which is the number of pixels to
1344 * place between expander and the child.
1349 gtk_expander_set_spacing (GtkExpander *expander,
1352 g_return_if_fail (GTK_IS_EXPANDER (expander));
1353 g_return_if_fail (spacing >= 0);
1355 if (expander->priv->spacing != spacing)
1357 expander->priv->spacing = spacing;
1359 gtk_widget_queue_resize (GTK_WIDGET (expander));
1361 g_object_notify (G_OBJECT (expander), "spacing");
1366 * gtk_expander_get_spacing:
1367 * @expander: a #GtkExpander
1369 * Gets the value set by gtk_expander_set_spacing().
1371 * Return value: spacing between the expander and child.
1376 gtk_expander_get_spacing (GtkExpander *expander)
1378 g_return_val_if_fail (GTK_IS_EXPANDER (expander), 0);
1380 return expander->priv->spacing;
1384 * gtk_expander_set_label:
1385 * @expander: a #GtkExpander
1388 * Sets the text of the label of the expander to @label.
1390 * This will also clear any previously set labels.
1395 gtk_expander_set_label (GtkExpander *expander,
1398 g_return_if_fail (GTK_IS_EXPANDER (expander));
1402 gtk_expander_set_label_widget (expander, NULL);
1408 child = gtk_label_new (label);
1409 gtk_label_set_use_underline (GTK_LABEL (child), expander->priv->use_underline);
1410 gtk_label_set_use_markup (GTK_LABEL (child), expander->priv->use_markup);
1411 gtk_widget_show (child);
1413 gtk_expander_set_label_widget (expander, child);
1416 g_object_notify (G_OBJECT (expander), "label");
1420 * gtk_expander_get_label:
1421 * @expander: a #GtkExpander
1423 * Fetches the text from the label of the expander, as set by
1424 * gtk_expander_set_label(). If the label text has not
1425 * been set the return value will be %NULL. This will be the
1426 * case if you create an empty button with gtk_button_new() to
1427 * use as a container.
1429 * Return value: The text of the label widget. This string is owned
1430 * by the widget and must not be modified or freed.
1434 G_CONST_RETURN char *
1435 gtk_expander_get_label (GtkExpander *expander)
1437 GtkExpanderPrivate *priv;
1439 g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1441 priv = expander->priv;
1443 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1444 return gtk_label_get_text (GTK_LABEL (priv->label_widget));
1450 * gtk_expander_set_use_underline:
1451 * @expander: a #GtkExpander
1452 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1454 * If true, an underline in the text of the expander label indicates
1455 * the next character should be used for the mnemonic accelerator key.
1460 gtk_expander_set_use_underline (GtkExpander *expander,
1461 gboolean use_underline)
1463 GtkExpanderPrivate *priv;
1465 g_return_if_fail (GTK_IS_EXPANDER (expander));
1467 priv = expander->priv;
1469 use_underline = use_underline != FALSE;
1471 if (priv->use_underline != use_underline)
1473 priv->use_underline = use_underline;
1475 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1476 gtk_label_set_use_underline (GTK_LABEL (priv->label_widget), use_underline);
1478 g_object_notify (G_OBJECT (expander), "use_underline");
1483 * gtk_expander_get_use_underline:
1484 * @expander: a #GtkExpander
1486 * Returns whether an embedded underline in the expander label indicates a
1487 * mnemonic. See gtk_expander_set_use_underline().
1489 * Return value: %TRUE if an embedded underline in the expander label
1490 * indicates the mnemonic accelerator keys.
1495 gtk_expander_get_use_underline (GtkExpander *expander)
1497 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1499 return expander->priv->use_underline;
1503 * gtk_expander_set_use_markup:
1504 * @expander: a #GtkExpander
1505 * @use_markup: %TRUE if the label's text should be parsed for markup
1507 * Sets whether the text of the label contains markup in <link
1508 * linkend="PangoMarkupFormat">Pango's text markup
1509 * language</link>. See gtk_label_set_markup().
1514 gtk_expander_set_use_markup (GtkExpander *expander,
1515 gboolean use_markup)
1517 GtkExpanderPrivate *priv;
1519 g_return_if_fail (GTK_IS_EXPANDER (expander));
1521 priv = expander->priv;
1523 use_markup = use_markup != FALSE;
1525 if (priv->use_markup != use_markup)
1527 priv->use_markup = use_markup;
1529 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1530 gtk_label_set_use_markup (GTK_LABEL (priv->label_widget), use_markup);
1532 g_object_notify (G_OBJECT (expander), "use_markup");
1537 * gtk_expander_get_use_markup:
1538 * @expander: a #GtkExpander
1540 * Returns whether the label's text is interpreted as marked up with
1541 * the <link linkend="PangoMarkupFormat">Pango text markup
1542 * language</link>. See gtk_expander_set_use_markup ().
1544 * Return value: %TRUE if the label's text will be parsed for markup
1549 gtk_expander_get_use_markup (GtkExpander *expander)
1551 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1553 return expander->priv->use_markup;
1557 * gtk_expander_set_label_widget:
1558 * @expander: a #GtkExpander
1559 * @label_widget: the new label widget
1561 * Set the label widget for the expander. This is the widget
1562 * that will appear embedded alongside the expander arrow.
1567 gtk_expander_set_label_widget (GtkExpander *expander,
1568 GtkWidget *label_widget)
1570 GtkExpanderPrivate *priv;
1572 g_return_if_fail (GTK_IS_EXPANDER (expander));
1573 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1574 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1576 priv = expander->priv;
1578 if (priv->label_widget == label_widget)
1581 if (priv->label_widget)
1582 gtk_widget_unparent (priv->label_widget);
1584 priv->label_widget = label_widget;
1588 priv->label_widget = label_widget;
1589 gtk_widget_set_parent (label_widget, GTK_WIDGET (expander));
1592 if (GTK_WIDGET_VISIBLE (expander))
1593 gtk_widget_queue_resize (GTK_WIDGET (expander));
1595 g_object_freeze_notify (G_OBJECT (expander));
1596 g_object_notify (G_OBJECT (expander), "label_widget");
1597 g_object_notify (G_OBJECT (expander), "label");
1598 g_object_thaw_notify (G_OBJECT (expander));
1602 * gtk_expander_get_label_widget:
1603 * @expander: a #GtkExpander
1605 * Retrieves the label widget for the frame. See
1606 * gtk_expander_set_label_widget().
1608 * Return value: the label widget, or %NULL if there is none.
1613 gtk_expander_get_label_widget (GtkExpander *expander)
1615 g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1617 return expander->priv->label_widget;