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 _("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 _("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 _("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 _("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 _("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 _("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 _("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 _("Indicator Spacing"),
253 _("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 (GtkExpander *expander)
709 widget = GTK_WIDGET (expander);
711 get_expander_bounds (expander, &clip);
713 state = widget->state;
714 if (expander->priv->prelight)
715 state = GTK_STATE_PRELIGHT;
717 gtk_paint_expander (widget->style,
723 clip.x + clip.width / 2,
724 clip.y + clip.height / 2,
725 expander->priv->expander_style);
729 gtk_expander_paint_focus (GtkExpander *expander,
733 GtkExpanderPrivate *priv;
734 gint x, y, width, height;
735 gboolean interior_focus;
740 gint expander_spacing;
743 widget = GTK_WIDGET (expander);
744 priv = expander->priv;
746 border_width = GTK_CONTAINER (widget)->border_width;
748 gtk_widget_style_get (widget,
749 "interior-focus", &interior_focus,
750 "focus-line-width", &focus_width,
751 "focus-padding", &focus_pad,
752 "expander-size", &expander_size,
753 "expander-spacing", &expander_spacing,
756 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
758 x = widget->allocation.x + border_width;
759 y = widget->allocation.y + border_width;
761 if (ltr && interior_focus)
762 x += expander_spacing * 2 + expander_size;
766 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
768 GtkAllocation label_allocation = priv->label_widget->allocation;
770 width = label_allocation.width;
771 height = label_allocation.height;
776 width += expander_size + 2 * expander_spacing;
777 height = MAX (height, expander_size + 2 * expander_spacing);
780 width += 2 * focus_pad + 2 * focus_width;
781 height += 2 * focus_pad + 2 * focus_width;
783 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
784 area, widget, "expander",
785 x, y, width, height);
789 gtk_expander_expose (GtkWidget *widget,
790 GdkEventExpose *event)
792 if (GTK_WIDGET_DRAWABLE (widget))
794 GtkExpander *expander = GTK_EXPANDER (widget);
796 gtk_expander_paint (expander);
798 if (GTK_WIDGET_HAS_FOCUS (expander))
799 gtk_expander_paint_focus (expander, &event->area);
801 if (expander->priv->label_widget)
802 gtk_container_propagate_expose (GTK_CONTAINER (widget),
803 expander->priv->label_widget,
806 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
813 gtk_expander_button_press (GtkWidget *widget,
814 GdkEventButton *event)
816 GtkExpander *expander = GTK_EXPANDER (widget);
818 if (event->button == 1 && event->window == expander->priv->event_window)
820 expander->priv->button_down = TRUE;
828 gtk_expander_button_release (GtkWidget *widget,
829 GdkEventButton *event)
831 GtkExpander *expander = GTK_EXPANDER (widget);
833 if (event->button == 1 && expander->priv->button_down)
835 gtk_widget_activate (widget);
836 expander->priv->button_down = FALSE;
844 gtk_expander_grab_notify (GtkWidget *widget,
845 gboolean was_grabbed)
848 GTK_EXPANDER (widget)->priv->button_down = FALSE;
852 gtk_expander_state_changed (GtkWidget *widget,
853 GtkStateType previous_state)
855 if (!GTK_WIDGET_IS_SENSITIVE (widget))
856 GTK_EXPANDER (widget)->priv->button_down = FALSE;
860 gtk_expander_redraw_expander (GtkExpander *expander)
864 get_expander_bounds (expander, &bounds);
866 gtk_widget_queue_draw_area (GTK_WIDGET (expander),
874 gtk_expander_enter_notify (GtkWidget *widget,
875 GdkEventCrossing *event)
877 GtkExpander *expander = GTK_EXPANDER (widget);
878 GtkWidget *event_widget;
880 event_widget = gtk_get_event_widget ((GdkEvent *) event);
882 if (event_widget == widget &&
883 event->detail != GDK_NOTIFY_INFERIOR)
885 expander->priv->prelight = TRUE;
886 gtk_expander_redraw_expander (expander);
893 gtk_expander_leave_notify (GtkWidget *widget,
894 GdkEventCrossing *event)
896 GtkExpander *expander = GTK_EXPANDER (widget);
897 GtkWidget *event_widget;
899 event_widget = gtk_get_event_widget ((GdkEvent *) event);
901 if (event_widget == widget &&
902 event->detail != GDK_NOTIFY_INFERIOR)
904 expander->priv->prelight = FALSE;
905 gtk_expander_redraw_expander (expander);
920 focus_current_site (GtkExpander *expander,
921 GtkDirectionType direction)
923 GtkWidget *current_focus;
925 current_focus = GTK_CONTAINER (expander)->focus_child;
930 return gtk_widget_child_focus (current_focus, direction);
934 focus_in_site (GtkExpander *expander,
936 GtkDirectionType direction)
941 gtk_widget_grab_focus (GTK_WIDGET (expander));
944 if (expander->priv->label_widget)
945 return gtk_widget_child_focus (expander->priv->label_widget, direction);
950 GtkWidget *child = gtk_bin_get_child (GTK_BIN (expander));
952 if (child && GTK_WIDGET_CHILD_VISIBLE (child))
953 return gtk_widget_child_focus (child, direction);
961 g_assert_not_reached ();
966 get_next_site (GtkExpander *expander,
968 GtkDirectionType direction)
972 ltr = gtk_widget_get_direction (GTK_WIDGET (expander)) != GTK_TEXT_DIR_RTL;
979 case GTK_DIR_TAB_BACKWARD:
983 case GTK_DIR_TAB_FORWARD:
991 case GTK_DIR_TAB_BACKWARD:
995 return ltr ? FOCUS_NONE : FOCUS_LABEL;
996 case GTK_DIR_TAB_FORWARD:
1000 return ltr ? FOCUS_LABEL : FOCUS_NONE;
1006 case GTK_DIR_TAB_BACKWARD:
1008 return FOCUS_WIDGET;
1010 return ltr ? FOCUS_WIDGET : FOCUS_CHILD;
1011 case GTK_DIR_TAB_FORWARD:
1015 return ltr ? FOCUS_CHILD : FOCUS_WIDGET;
1021 case GTK_DIR_TAB_BACKWARD:
1025 case GTK_DIR_TAB_FORWARD:
1032 g_assert_not_reached ();
1037 gtk_expander_focus (GtkWidget *widget,
1038 GtkDirectionType direction)
1040 GtkExpander *expander = GTK_EXPANDER (widget);
1042 if (!focus_current_site (expander, direction))
1044 GtkWidget *old_focus_child;
1045 gboolean widget_is_focus;
1046 FocusSite site = FOCUS_NONE;
1048 widget_is_focus = gtk_widget_is_focus (widget);
1049 old_focus_child = GTK_CONTAINER (widget)->focus_child;
1051 if (old_focus_child && old_focus_child == expander->priv->label_widget)
1053 else if (old_focus_child)
1055 else if (widget_is_focus)
1056 site = FOCUS_WIDGET;
1058 while ((site = get_next_site (expander, site, direction)) != FOCUS_NONE)
1060 if (focus_in_site (expander, site, direction))
1071 gtk_expander_add (GtkContainer *container,
1074 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1076 gtk_widget_set_child_visible (widget, GTK_EXPANDER (container)->priv->expanded);
1077 gtk_widget_queue_resize (GTK_WIDGET (container));
1081 gtk_expander_remove (GtkContainer *container,
1084 GtkExpander *expander = GTK_EXPANDER (container);
1086 if (GTK_EXPANDER (expander)->priv->label_widget == widget)
1087 gtk_expander_set_label_widget (expander, NULL);
1089 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1093 gtk_expander_forall (GtkContainer *container,
1094 gboolean include_internals,
1095 GtkCallback callback,
1096 gpointer callback_data)
1098 GtkBin *bin = GTK_BIN (container);
1099 GtkExpanderPrivate *priv = GTK_EXPANDER (container)->priv;
1102 (* callback) (bin->child, callback_data);
1104 if (priv->label_widget)
1105 (* callback) (priv->label_widget, callback_data);
1109 gtk_expander_activate (GtkExpander *expander)
1111 gtk_expander_set_expanded (expander, !expander->priv->expanded);
1117 * @label: the text of the label
1119 * Creates a new expander using @label as the text of the label.
1121 * Return value: a new #GtkExpander widget.
1126 gtk_expander_new (const gchar *label)
1128 return g_object_new (GTK_TYPE_EXPANDER, "label", label, NULL);
1132 * gtk_expander_new_with_mnemonic:
1133 * @label: the text of the label with an underscore in front of the
1134 * mnemonic character
1136 * Creates a new expander using @label as the text of the label.
1137 * If characters in @label are preceded by an underscore, they are underlined.
1138 * If you need a literal underscore character in a label, use '__' (two
1139 * underscores). The first underlined character represents a keyboard
1140 * accelerator called a mnemonic.
1141 * Pressing Alt and that key activates the button.
1143 * Return value: a new #GtkExpander widget.
1148 gtk_expander_new_with_mnemonic (const gchar *label)
1150 return g_object_new (GTK_TYPE_EXPANDER,
1152 "use_underline", TRUE,
1157 gtk_expander_animation_timeout (GtkExpander *expander)
1159 GtkExpanderPrivate *priv = expander->priv;
1161 gboolean finish = FALSE;
1163 if (GTK_WIDGET_REALIZED (expander))
1165 get_expander_bounds (expander, &area);
1166 gdk_window_invalidate_rect (GTK_WIDGET (expander)->window, &area, TRUE);
1171 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1173 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1177 priv->expander_style = GTK_EXPANDER_EXPANDED;
1183 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1185 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1189 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1196 priv->animation_timeout = 0;
1197 if (GTK_BIN (expander)->child)
1198 gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1199 gtk_widget_queue_resize (GTK_WIDGET (expander));
1206 gtk_expander_start_animation (GtkExpander *expander)
1208 GtkExpanderPrivate *priv = expander->priv;
1210 if (priv->animation_timeout)
1211 g_source_remove (priv->animation_timeout);
1213 priv->animation_timeout =
1215 (GSourceFunc) gtk_expander_animation_timeout,
1220 * gtk_expander_set_expanded:
1221 * @expander: a #GtkExpander
1222 * @expanded: whether the child widget is revealed
1224 * Sets the state of the expander. Set to %TRUE, if you want
1225 * the child widget to be revealed, and %FALSE if you want the
1226 * child widget to be hidden.
1231 gtk_expander_set_expanded (GtkExpander *expander,
1234 GtkExpanderPrivate *priv;
1236 g_return_if_fail (GTK_IS_EXPANDER (expander));
1238 priv = expander->priv;
1240 expanded = expanded != FALSE;
1242 if (priv->expanded != expanded)
1244 priv->expanded = expanded;
1246 if (GTK_WIDGET_REALIZED (expander))
1248 gtk_expander_start_animation (expander);
1252 priv->expander_style = expanded ? GTK_EXPANDER_EXPANDED :
1253 GTK_EXPANDER_COLLAPSED;
1255 if (GTK_BIN (expander)->child)
1257 gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1258 gtk_widget_queue_resize (GTK_WIDGET (expander));
1262 g_object_notify (G_OBJECT (expander), "expanded");
1267 * gtk_expander_get_expanded:
1268 * @expander:a #GtkExpander
1270 * Queries a #GtkExpander and returns its current state. Returns %TRUE
1271 * if the child widget is revealed.
1273 * See gtk_expander_set_expanded().
1275 * Return value: the current state of the expander.
1280 gtk_expander_get_expanded (GtkExpander *expander)
1282 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1284 return expander->priv->expanded;
1288 * gtk_expander_set_spacing:
1289 * @expander: a #GtkExpander
1290 * @spacing: distance between the expander and child in pixels.
1292 * Sets the spacing field of @expander, which is the number of pixels to
1293 * place between expander and the child.
1298 gtk_expander_set_spacing (GtkExpander *expander,
1301 g_return_if_fail (GTK_IS_EXPANDER (expander));
1302 g_return_if_fail (spacing >= 0);
1304 if (expander->priv->spacing != spacing)
1306 expander->priv->spacing = spacing;
1308 gtk_widget_queue_resize (GTK_WIDGET (expander));
1310 g_object_notify (G_OBJECT (expander), "spacing");
1315 * gtk_expander_get_spacing:
1316 * @expander: a #GtkExpander
1318 * Gets the value set by gtk_expander_set_spacing().
1320 * Return value: spacing between the expander and child.
1325 gtk_expander_get_spacing (GtkExpander *expander)
1327 g_return_val_if_fail (GTK_IS_EXPANDER (expander), 0);
1329 return expander->priv->spacing;
1333 * gtk_expander_set_label:
1334 * @expander: a #GtkExpander
1337 * Sets the text of the label of the expander to @label.
1339 * This will also clear any previously set labels.
1344 gtk_expander_set_label (GtkExpander *expander,
1347 g_return_if_fail (GTK_IS_EXPANDER (expander));
1351 gtk_expander_set_label_widget (expander, NULL);
1357 child = gtk_label_new (label);
1358 gtk_label_set_use_underline (GTK_LABEL (child), expander->priv->use_underline);
1359 gtk_label_set_use_markup (GTK_LABEL (child), expander->priv->use_markup);
1360 gtk_widget_show (child);
1362 gtk_expander_set_label_widget (expander, child);
1365 g_object_notify (G_OBJECT (expander), "label");
1369 * gtk_expander_get_label:
1370 * @expander: a #GtkExpander
1372 * Fetches the text from the label of the expander, as set by
1373 * gtk_expander_set_label(). If the label text has not
1374 * been set the return value will be %NULL. This will be the
1375 * case if you create an empty button with gtk_button_new() to
1376 * use as a container.
1378 * Return value: The text of the label widget. This string is owned
1379 * by the widget and must not be modified or freed.
1383 G_CONST_RETURN char *
1384 gtk_expander_get_label (GtkExpander *expander)
1386 GtkExpanderPrivate *priv;
1388 g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1390 priv = expander->priv;
1392 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1393 return gtk_label_get_text (GTK_LABEL (priv->label_widget));
1399 * gtk_expander_set_use_underline:
1400 * @expander: a #GtkExpander
1401 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1403 * If true, an underline in the text of the expander label indicates
1404 * the next character should be used for the mnemonic accelerator key.
1409 gtk_expander_set_use_underline (GtkExpander *expander,
1410 gboolean use_underline)
1412 GtkExpanderPrivate *priv;
1414 g_return_if_fail (GTK_IS_EXPANDER (expander));
1416 priv = expander->priv;
1418 use_underline = use_underline != FALSE;
1420 if (priv->use_underline != use_underline)
1422 priv->use_underline = use_underline;
1424 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1425 gtk_label_set_use_underline (GTK_LABEL (priv->label_widget), use_underline);
1427 g_object_notify (G_OBJECT (expander), "use_underline");
1432 * gtk_expander_get_use_underline:
1433 * @expander: a #GtkExpander
1435 * Returns whether an embedded underline in the expander label indicates a
1436 * mnemonic. See gtk_expander_set_use_underline().
1438 * Return value: %TRUE if an embedded underline in the expander label
1439 * indicates the mnemonic accelerator keys.
1444 gtk_expander_get_use_underline (GtkExpander *expander)
1446 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1448 return expander->priv->use_underline;
1452 * gtk_expander_set_use_markup:
1453 * @expander: a #GtkExpander
1454 * @use_markup: %TRUE if the label's text should be parsed for markup
1456 * Sets whether the text of the label contains markup in <link
1457 * linkend="PangoMarkupFormat">Pango's text markup
1458 * language</link>. See gtk_label_set_markup().
1463 gtk_expander_set_use_markup (GtkExpander *expander,
1464 gboolean use_markup)
1466 GtkExpanderPrivate *priv;
1468 g_return_if_fail (GTK_IS_EXPANDER (expander));
1470 priv = expander->priv;
1472 use_markup = use_markup != FALSE;
1474 if (priv->use_markup != use_markup)
1476 priv->use_markup = use_markup;
1478 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1479 gtk_label_set_use_markup (GTK_LABEL (priv->label_widget), use_markup);
1481 g_object_notify (G_OBJECT (expander), "use_markup");
1486 * gtk_expander_get_use_markup:
1487 * @expander: a #GtkExpander
1489 * Returns whether the label's text is interpreted as marked up with
1490 * the <link linkend="PangoMarkupFormat">Pango text markup
1491 * language</link>. See gtk_expander_set_use_markup ().
1493 * Return value: %TRUE if the label's text will be parsed for markup
1498 gtk_expander_get_use_markup (GtkExpander *expander)
1500 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1502 return expander->priv->use_markup;
1506 * gtk_expander_set_label_widget:
1507 * @expander: a #GtkExpander
1508 * @label_widget: the new label widget
1510 * Set the label widget for the expander. This is the widget
1511 * that will appear embedded alongside the expander arrow.
1516 gtk_expander_set_label_widget (GtkExpander *expander,
1517 GtkWidget *label_widget)
1519 GtkExpanderPrivate *priv;
1521 g_return_if_fail (GTK_IS_EXPANDER (expander));
1522 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1523 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1525 priv = expander->priv;
1527 if (priv->label_widget == label_widget)
1530 if (priv->label_widget)
1531 gtk_widget_unparent (priv->label_widget);
1533 priv->label_widget = label_widget;
1537 priv->label_widget = label_widget;
1538 gtk_widget_set_parent (label_widget, GTK_WIDGET (expander));
1541 if (GTK_WIDGET_VISIBLE (expander))
1542 gtk_widget_queue_resize (GTK_WIDGET (expander));
1544 g_object_freeze_notify (G_OBJECT (expander));
1545 g_object_notify (G_OBJECT (expander), "label_widget");
1546 g_object_notify (G_OBJECT (expander), "label");
1547 g_object_thaw_notify (G_OBJECT (expander));
1551 * gtk_expander_get_label_widget:
1552 * @expander: a #GtkExpander
1554 * Retrieves the label widget for the frame. See
1555 * gtk_expander_set_label_widget().
1557 * Return value: the label widget, or %NULL if there is none.
1562 gtk_expander_get_label_widget (GtkExpander *expander)
1564 g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1566 return expander->priv->label_widget;