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>
37 #define GTK_EXPANDER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_EXPANDER, GtkExpanderPrivate))
39 #define DEFAULT_EXPANDER_SIZE 10
40 #define DEFAULT_EXPANDER_SPACING 2
53 struct _GtkExpanderPrivate
55 GtkWidget *label_widget;
56 GdkWindow *event_window;
59 GtkExpanderStyle expander_style;
60 guint animation_timeout;
63 guint use_underline : 1;
65 guint button_down : 1;
69 static void gtk_expander_class_init (GtkExpanderClass *klass);
70 static void gtk_expander_init (GtkExpander *expander);
72 static void gtk_expander_set_property (GObject *object,
76 static void gtk_expander_get_property (GObject *object,
81 static void gtk_expander_destroy (GtkObject *object);
83 static void gtk_expander_realize (GtkWidget *widget);
84 static void gtk_expander_unrealize (GtkWidget *widget);
85 static void gtk_expander_size_request (GtkWidget *widget,
86 GtkRequisition *requisition);
87 static void gtk_expander_size_allocate (GtkWidget *widget,
88 GtkAllocation *allocation);
89 static void gtk_expander_map (GtkWidget *widget);
90 static void gtk_expander_unmap (GtkWidget *widget);
91 static gboolean gtk_expander_expose (GtkWidget *widget,
92 GdkEventExpose *event);
93 static gboolean gtk_expander_button_press (GtkWidget *widget,
94 GdkEventButton *event);
95 static gboolean gtk_expander_button_release (GtkWidget *widget,
96 GdkEventButton *event);
97 static gboolean gtk_expander_enter_notify (GtkWidget *widget,
98 GdkEventCrossing *event);
99 static gboolean gtk_expander_leave_notify (GtkWidget *widget,
100 GdkEventCrossing *event);
101 static gboolean gtk_expander_focus (GtkWidget *widget,
102 GtkDirectionType direction);
103 static void gtk_expander_grab_notify (GtkWidget *widget,
104 gboolean was_grabbed);
105 static void gtk_expander_state_changed (GtkWidget *widget,
106 GtkStateType previous_state);
108 static void gtk_expander_add (GtkContainer *container,
110 static void gtk_expander_remove (GtkContainer *container,
112 static void gtk_expander_forall (GtkContainer *container,
113 gboolean include_internals,
114 GtkCallback callback,
115 gpointer callback_data);
117 static void gtk_expander_activate (GtkExpander *expander);
119 static void get_expander_bounds (GtkExpander *expander,
122 static GtkBinClass *parent_class = NULL;
125 gtk_expander_get_type (void)
127 static GType expander_type = 0;
131 static const GTypeInfo expander_info =
133 sizeof (GtkExpanderClass),
134 NULL, /* base_init */
135 NULL, /* base_finalize */
136 (GClassInitFunc) gtk_expander_class_init,
137 NULL, /* class_finalize */
138 NULL, /* class_data */
139 sizeof (GtkExpander),
141 (GInstanceInitFunc) gtk_expander_init,
144 expander_type = g_type_register_static (GTK_TYPE_BIN,
149 return expander_type;
153 gtk_expander_class_init (GtkExpanderClass *klass)
155 GObjectClass *gobject_class;
156 GtkObjectClass *object_class;
157 GtkWidgetClass *widget_class;
158 GtkContainerClass *container_class;
160 parent_class = g_type_class_peek_parent (klass);
162 gobject_class = (GObjectClass *) klass;
163 object_class = (GtkObjectClass *) klass;
164 widget_class = (GtkWidgetClass *) klass;
165 container_class = (GtkContainerClass *) klass;
167 gobject_class->set_property = gtk_expander_set_property;
168 gobject_class->get_property = gtk_expander_get_property;
170 object_class->destroy = gtk_expander_destroy;
172 widget_class->realize = gtk_expander_realize;
173 widget_class->unrealize = gtk_expander_unrealize;
174 widget_class->size_request = gtk_expander_size_request;
175 widget_class->size_allocate = gtk_expander_size_allocate;
176 widget_class->map = gtk_expander_map;
177 widget_class->unmap = gtk_expander_unmap;
178 widget_class->expose_event = gtk_expander_expose;
179 widget_class->button_press_event = gtk_expander_button_press;
180 widget_class->button_release_event = gtk_expander_button_release;
181 widget_class->enter_notify_event = gtk_expander_enter_notify;
182 widget_class->leave_notify_event = gtk_expander_leave_notify;
183 widget_class->focus = gtk_expander_focus;
184 widget_class->grab_notify = gtk_expander_grab_notify;
185 widget_class->state_changed = gtk_expander_state_changed;
187 container_class->add = gtk_expander_add;
188 container_class->remove = gtk_expander_remove;
189 container_class->forall = gtk_expander_forall;
191 klass->activate = gtk_expander_activate;
193 g_type_class_add_private (klass, sizeof (GtkExpanderPrivate));
195 g_object_class_install_property (gobject_class,
197 g_param_spec_boolean ("expanded",
199 P_("Whether the expander has been opened to reveal the child widget"),
201 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
203 g_object_class_install_property (gobject_class,
205 g_param_spec_string ("label",
207 P_("Text of the expander's label"),
209 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
211 g_object_class_install_property (gobject_class,
213 g_param_spec_boolean ("use-underline",
215 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
217 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
219 g_object_class_install_property (gobject_class,
221 g_param_spec_boolean ("use-markup",
223 P_("The text of the label includes XML markup. See pango_parse_markup()"),
225 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT));
227 g_object_class_install_property (gobject_class,
229 g_param_spec_int ("spacing",
231 P_("Space to put between the label and the child"),
235 GTK_PARAM_READWRITE));
237 g_object_class_install_property (gobject_class,
239 g_param_spec_object ("label-widget",
241 P_("A widget to display in place of the usual expander label"),
243 GTK_PARAM_READWRITE));
245 gtk_widget_class_install_style_property (widget_class,
246 g_param_spec_int ("expander-size",
248 P_("Size of the expander arrow"),
251 DEFAULT_EXPANDER_SIZE,
252 GTK_PARAM_READABLE));
254 gtk_widget_class_install_style_property (widget_class,
255 g_param_spec_int ("expander-spacing",
256 P_("Indicator Spacing"),
257 P_("Spacing around expander arrow"),
260 DEFAULT_EXPANDER_SPACING,
261 GTK_PARAM_READABLE));
263 widget_class->activate_signal =
264 g_signal_new ("activate",
265 G_TYPE_FROM_CLASS (gobject_class),
266 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
267 G_STRUCT_OFFSET (GtkExpanderClass, activate),
269 _gtk_marshal_VOID__VOID,
274 gtk_expander_init (GtkExpander *expander)
276 GtkExpanderPrivate *priv;
278 expander->priv = priv = GTK_EXPANDER_GET_PRIVATE (expander);
280 GTK_WIDGET_SET_FLAGS (expander, GTK_CAN_FOCUS);
281 GTK_WIDGET_SET_FLAGS (expander, GTK_NO_WINDOW);
283 priv->label_widget = NULL;
284 priv->event_window = NULL;
287 priv->expander_style = GTK_EXPANDER_COLLAPSED;
288 priv->animation_timeout = 0;
290 priv->expanded = FALSE;
291 priv->use_underline = FALSE;
292 priv->use_markup = FALSE;
293 priv->button_down = FALSE;
294 priv->prelight = FALSE;
298 gtk_expander_set_property (GObject *object,
303 GtkExpander *expander = GTK_EXPANDER (object);
308 gtk_expander_set_expanded (expander, g_value_get_boolean (value));
311 gtk_expander_set_label (expander, g_value_get_string (value));
313 case PROP_USE_UNDERLINE:
314 gtk_expander_set_use_underline (expander, g_value_get_boolean (value));
316 case PROP_USE_MARKUP:
317 gtk_expander_set_use_markup (expander, g_value_get_boolean (value));
320 gtk_expander_set_spacing (expander, g_value_get_int (value));
322 case PROP_LABEL_WIDGET:
323 gtk_expander_set_label_widget (expander, g_value_get_object (value));
326 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
332 gtk_expander_get_property (GObject *object,
337 GtkExpander *expander = GTK_EXPANDER (object);
338 GtkExpanderPrivate *priv = expander->priv;
343 g_value_set_boolean (value, priv->expanded);
346 g_value_set_string (value, gtk_expander_get_label (expander));
348 case PROP_USE_UNDERLINE:
349 g_value_set_boolean (value, priv->use_underline);
351 case PROP_USE_MARKUP:
352 g_value_set_boolean (value, priv->use_markup);
355 g_value_set_int (value, priv->spacing);
357 case PROP_LABEL_WIDGET:
358 g_value_set_object (value,
360 G_OBJECT (priv->label_widget) : NULL);
363 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
369 gtk_expander_destroy (GtkObject *object)
371 GtkExpanderPrivate *priv = GTK_EXPANDER (object)->priv;
373 if (priv->animation_timeout)
375 g_source_remove (priv->animation_timeout);
376 priv->animation_timeout = 0;
379 GTK_OBJECT_CLASS (parent_class)->destroy (object);
383 gtk_expander_realize (GtkWidget *widget)
385 GtkExpanderPrivate *priv;
386 GdkWindowAttr attributes;
387 gint attributes_mask;
389 GdkRectangle expander_rect;
391 priv = GTK_EXPANDER (widget)->priv;
392 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
394 border_width = GTK_CONTAINER (widget)->border_width;
396 get_expander_bounds (GTK_EXPANDER (widget), &expander_rect);
398 attributes.window_type = GDK_WINDOW_CHILD;
399 attributes.x = widget->allocation.x + border_width;
400 attributes.y = expander_rect.y;
401 attributes.width = MAX (widget->allocation.width - 2 * border_width, 1);
402 attributes.height = expander_rect.width;
403 attributes.wclass = GDK_INPUT_ONLY;
404 attributes.event_mask = gtk_widget_get_events (widget) |
405 GDK_BUTTON_PRESS_MASK |
406 GDK_BUTTON_RELEASE_MASK |
407 GDK_ENTER_NOTIFY_MASK |
408 GDK_LEAVE_NOTIFY_MASK;
410 attributes_mask = GDK_WA_X | GDK_WA_Y;
412 widget->window = gtk_widget_get_parent_window (widget);
413 g_object_ref (widget->window);
415 priv->event_window = gdk_window_new (gtk_widget_get_parent_window (widget),
416 &attributes, attributes_mask);
417 gdk_window_set_user_data (priv->event_window, widget);
419 widget->style = gtk_style_attach (widget->style, widget->window);
420 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
424 gtk_expander_unrealize (GtkWidget *widget)
426 GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
428 if (priv->event_window)
430 gdk_window_set_user_data (priv->event_window, NULL);
431 gdk_window_destroy (priv->event_window);
432 priv->event_window = NULL;
435 GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
439 gtk_expander_size_request (GtkWidget *widget,
440 GtkRequisition *requisition)
442 GtkExpander *expander;
444 GtkExpanderPrivate *priv;
447 gint expander_spacing;
448 gboolean interior_focus;
452 bin = GTK_BIN (widget);
453 expander = GTK_EXPANDER (widget);
454 priv = expander->priv;
456 border_width = GTK_CONTAINER (widget)->border_width;
458 gtk_widget_style_get (widget,
459 "interior-focus", &interior_focus,
460 "focus-line-width", &focus_width,
461 "focus-padding", &focus_pad,
462 "expander-size", &expander_size,
463 "expander-spacing", &expander_spacing,
466 requisition->width = expander_size + 2 * expander_spacing +
467 2 * focus_width + 2 * focus_pad;
468 requisition->height = interior_focus ? (2 * focus_width + 2 * focus_pad) : 0;
470 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
472 GtkRequisition label_requisition;
474 gtk_widget_size_request (priv->label_widget, &label_requisition);
476 requisition->width += label_requisition.width;
477 requisition->height += label_requisition.height;
480 requisition->height = MAX (expander_size + 2 * expander_spacing, requisition->height);
483 requisition->height += 2 * focus_width + 2 * focus_pad;
485 if (bin->child && GTK_WIDGET_CHILD_VISIBLE (bin->child))
487 GtkRequisition child_requisition;
489 gtk_widget_size_request (bin->child, &child_requisition);
491 requisition->width = MAX (requisition->width, child_requisition.width);
492 requisition->height += child_requisition.height + priv->spacing;
495 requisition->width += 2 * border_width;
496 requisition->height += 2 * border_width;
500 get_expander_bounds (GtkExpander *expander,
505 GtkExpanderPrivate *priv;
508 gint expander_spacing;
509 gboolean interior_focus;
514 widget = GTK_WIDGET (expander);
515 bin = GTK_BIN (expander);
516 priv = expander->priv;
518 border_width = GTK_CONTAINER (expander)->border_width;
520 gtk_widget_style_get (widget,
521 "interior-focus", &interior_focus,
522 "focus-line-width", &focus_width,
523 "focus-padding", &focus_pad,
524 "expander-size", &expander_size,
525 "expander-spacing", &expander_spacing,
528 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
530 rect->x = widget->allocation.x + border_width;
531 rect->y = widget->allocation.y + border_width;
534 rect->x += expander_spacing;
536 rect->x += widget->allocation.width - 2 * border_width -
537 expander_spacing - expander_size;
539 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
541 GtkAllocation label_allocation;
543 label_allocation = priv->label_widget->allocation;
545 if (expander_size < label_allocation.height)
546 rect->y += focus_width + focus_pad + (label_allocation.height - expander_size) / 2;
548 rect->y += expander_spacing;
552 rect->y += expander_spacing;
558 rect->x += focus_width + focus_pad;
560 rect->x -= focus_width + focus_pad;
561 rect->y += focus_width + focus_pad;
564 rect->width = rect->height = expander_size;
568 gtk_expander_size_allocate (GtkWidget *widget,
569 GtkAllocation *allocation)
571 GtkExpander *expander;
573 GtkExpanderPrivate *priv;
574 GtkRequisition child_requisition;
575 gboolean child_visible = FALSE;
578 gint expander_spacing;
579 gboolean interior_focus;
584 expander = GTK_EXPANDER (widget);
585 bin = GTK_BIN (widget);
586 priv = expander->priv;
588 border_width = GTK_CONTAINER (widget)->border_width;
590 gtk_widget_style_get (widget,
591 "interior-focus", &interior_focus,
592 "focus-line-width", &focus_width,
593 "focus-padding", &focus_pad,
594 "expander-size", &expander_size,
595 "expander-spacing", &expander_spacing,
598 child_requisition.width = 0;
599 child_requisition.height = 0;
600 if (bin->child && GTK_WIDGET_CHILD_VISIBLE (bin->child))
602 child_visible = TRUE;
603 gtk_widget_get_child_requisition (bin->child, &child_requisition);
606 widget->allocation = *allocation;
608 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
610 GtkAllocation label_allocation;
611 GtkRequisition label_requisition;
614 gtk_widget_get_child_requisition (priv->label_widget, &label_requisition);
616 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
619 label_allocation.x = (widget->allocation.x +
620 border_width + focus_width + focus_pad +
621 expander_size + 2 * expander_spacing);
623 label_allocation.x = (widget->allocation.x + widget->allocation.width -
624 (label_requisition.width +
625 border_width + focus_width + focus_pad +
626 expander_size + 2 * expander_spacing));
628 label_allocation.y = widget->allocation.y + border_width + focus_width + focus_pad;
630 label_allocation.width = MIN (label_requisition.width,
631 allocation->width - 2 * border_width -
632 expander_size - 2 * expander_spacing -
633 2 * focus_width - 2 * focus_pad);
634 label_allocation.width = MAX (label_allocation.width, 1);
636 label_allocation.height = MIN (label_requisition.height,
637 allocation->height - 2 * border_width -
638 2 * focus_width - 2 * focus_pad -
639 (child_visible ? priv->spacing : 0));
640 label_allocation.height = MAX (label_allocation.height, 1);
642 gtk_widget_size_allocate (priv->label_widget, &label_allocation);
644 label_height = label_allocation.height;
651 if (GTK_WIDGET_REALIZED (widget))
655 get_expander_bounds (expander, &rect);
657 gdk_window_move_resize (priv->event_window,
658 allocation->x + border_width, rect.y,
659 MAX (allocation->width - 2 * border_width, 1), rect.width);
664 GtkAllocation child_allocation;
667 top_height = MAX (2 * expander_spacing + expander_size,
669 (interior_focus ? 2 * focus_width + 2 * focus_pad : 0));
671 child_allocation.x = widget->allocation.x + border_width;
672 child_allocation.y = widget->allocation.y + border_width + top_height + priv->spacing;
675 child_allocation.y += 2 * focus_width + 2 * focus_pad;
677 child_allocation.width = MAX (allocation->width - 2 * border_width, 1);
679 child_allocation.height = allocation->height - top_height -
680 2 * border_width - priv->spacing -
681 (!interior_focus ? 2 * focus_width + 2 * focus_pad : 0);
682 child_allocation.height = MAX (child_allocation.height, 1);
684 gtk_widget_size_allocate (bin->child, &child_allocation);
689 gtk_expander_map (GtkWidget *widget)
691 GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
693 if (priv->label_widget)
694 gtk_widget_map (priv->label_widget);
696 GTK_WIDGET_CLASS (parent_class)->map (widget);
698 if (priv->event_window)
699 gdk_window_show (priv->event_window);
703 gtk_expander_unmap (GtkWidget *widget)
705 GtkExpanderPrivate *priv = GTK_EXPANDER (widget)->priv;
707 if (priv->event_window)
708 gdk_window_hide (priv->event_window);
710 GTK_WIDGET_CLASS (parent_class)->unmap (widget);
712 if (priv->label_widget)
713 gtk_widget_unmap (priv->label_widget);
717 gtk_expander_paint_prelight (GtkExpander *expander)
720 GtkContainer *container;
721 GtkExpanderPrivate *priv;
723 gboolean interior_focus;
727 int expander_spacing;
729 priv = expander->priv;
730 widget = GTK_WIDGET (expander);
731 container = GTK_CONTAINER (expander);
733 gtk_widget_style_get (widget,
734 "interior-focus", &interior_focus,
735 "focus-line-width", &focus_width,
736 "focus-padding", &focus_pad,
737 "expander-size", &expander_size,
738 "expander-spacing", &expander_spacing,
741 area.x = widget->allocation.x + container->border_width;
742 area.y = widget->allocation.y + container->border_width;
743 area.width = widget->allocation.width - (2 * container->border_width);
745 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
746 area.height = priv->label_widget->allocation.height;
750 area.height += interior_focus ? (focus_width + focus_pad) * 2 : 0;
751 area.height = MAX (area.height, expander_size + 2 * expander_spacing);
752 area.height += !interior_focus ? (focus_width + focus_pad) * 2 : 0;
754 gtk_paint_flat_box (widget->style, widget->window,
756 GTK_SHADOW_ETCHED_OUT,
757 &area, widget, "expander",
759 area.width, area.height);
763 gtk_expander_paint (GtkExpander *expander)
769 widget = GTK_WIDGET (expander);
771 get_expander_bounds (expander, &clip);
773 state = widget->state;
774 if (expander->priv->prelight)
776 state = GTK_STATE_PRELIGHT;
778 gtk_expander_paint_prelight (expander);
781 gtk_paint_expander (widget->style,
787 clip.x + clip.width / 2,
788 clip.y + clip.height / 2,
789 expander->priv->expander_style);
793 gtk_expander_paint_focus (GtkExpander *expander,
797 GtkExpanderPrivate *priv;
798 gint x, y, width, height;
799 gboolean interior_focus;
804 gint expander_spacing;
807 widget = GTK_WIDGET (expander);
808 priv = expander->priv;
810 border_width = GTK_CONTAINER (widget)->border_width;
812 gtk_widget_style_get (widget,
813 "interior-focus", &interior_focus,
814 "focus-line-width", &focus_width,
815 "focus-padding", &focus_pad,
816 "expander-size", &expander_size,
817 "expander-spacing", &expander_spacing,
820 ltr = gtk_widget_get_direction (widget) != GTK_TEXT_DIR_RTL;
822 x = widget->allocation.x + border_width;
823 y = widget->allocation.y + border_width;
825 if (ltr && interior_focus)
826 x += expander_spacing * 2 + expander_size;
830 if (priv->label_widget && GTK_WIDGET_VISIBLE (priv->label_widget))
832 GtkAllocation label_allocation = priv->label_widget->allocation;
834 width = label_allocation.width;
835 height = label_allocation.height;
840 width += expander_size + 2 * expander_spacing;
841 height = MAX (height, expander_size + 2 * expander_spacing);
844 width += 2 * focus_pad + 2 * focus_width;
845 height += 2 * focus_pad + 2 * focus_width;
847 gtk_paint_focus (widget->style, widget->window, GTK_WIDGET_STATE (widget),
848 area, widget, "expander",
849 x, y, width, height);
853 gtk_expander_expose (GtkWidget *widget,
854 GdkEventExpose *event)
856 if (GTK_WIDGET_DRAWABLE (widget))
858 GtkExpander *expander = GTK_EXPANDER (widget);
860 gtk_expander_paint (expander);
862 if (GTK_WIDGET_HAS_FOCUS (expander))
863 gtk_expander_paint_focus (expander, &event->area);
865 GTK_WIDGET_CLASS (parent_class)->expose_event (widget, event);
872 gtk_expander_button_press (GtkWidget *widget,
873 GdkEventButton *event)
875 GtkExpander *expander = GTK_EXPANDER (widget);
877 if (event->button == 1 && event->window == expander->priv->event_window)
879 expander->priv->button_down = TRUE;
887 gtk_expander_button_release (GtkWidget *widget,
888 GdkEventButton *event)
890 GtkExpander *expander = GTK_EXPANDER (widget);
892 if (event->button == 1 && expander->priv->button_down)
894 gtk_widget_activate (widget);
895 expander->priv->button_down = FALSE;
903 gtk_expander_grab_notify (GtkWidget *widget,
904 gboolean was_grabbed)
907 GTK_EXPANDER (widget)->priv->button_down = FALSE;
911 gtk_expander_state_changed (GtkWidget *widget,
912 GtkStateType previous_state)
914 if (!GTK_WIDGET_IS_SENSITIVE (widget))
915 GTK_EXPANDER (widget)->priv->button_down = FALSE;
919 gtk_expander_redraw_expander (GtkExpander *expander)
923 widget = GTK_WIDGET (expander);
925 if (GTK_WIDGET_REALIZED (widget))
926 gdk_window_invalidate_rect (widget->window, &widget->allocation, FALSE);
930 gtk_expander_enter_notify (GtkWidget *widget,
931 GdkEventCrossing *event)
933 GtkExpander *expander = GTK_EXPANDER (widget);
934 GtkWidget *event_widget;
936 event_widget = gtk_get_event_widget ((GdkEvent *) event);
938 if (event_widget == widget &&
939 event->detail != GDK_NOTIFY_INFERIOR)
941 expander->priv->prelight = TRUE;
943 if (expander->priv->label_widget)
944 gtk_widget_set_state (expander->priv->label_widget, GTK_STATE_PRELIGHT);
946 gtk_expander_redraw_expander (expander);
953 gtk_expander_leave_notify (GtkWidget *widget,
954 GdkEventCrossing *event)
956 GtkExpander *expander = GTK_EXPANDER (widget);
957 GtkWidget *event_widget;
959 event_widget = gtk_get_event_widget ((GdkEvent *) event);
961 if (event_widget == widget &&
962 event->detail != GDK_NOTIFY_INFERIOR)
964 expander->priv->prelight = FALSE;
966 if (expander->priv->label_widget)
967 gtk_widget_set_state (expander->priv->label_widget, GTK_STATE_NORMAL);
969 gtk_expander_redraw_expander (expander);
984 focus_current_site (GtkExpander *expander,
985 GtkDirectionType direction)
987 GtkWidget *current_focus;
989 current_focus = GTK_CONTAINER (expander)->focus_child;
994 return gtk_widget_child_focus (current_focus, direction);
998 focus_in_site (GtkExpander *expander,
1000 GtkDirectionType direction)
1005 gtk_widget_grab_focus (GTK_WIDGET (expander));
1008 if (expander->priv->label_widget)
1009 return gtk_widget_child_focus (expander->priv->label_widget, direction);
1014 GtkWidget *child = gtk_bin_get_child (GTK_BIN (expander));
1016 if (child && GTK_WIDGET_CHILD_VISIBLE (child))
1017 return gtk_widget_child_focus (child, direction);
1025 g_assert_not_reached ();
1030 get_next_site (GtkExpander *expander,
1032 GtkDirectionType direction)
1036 ltr = gtk_widget_get_direction (GTK_WIDGET (expander)) != GTK_TEXT_DIR_RTL;
1043 case GTK_DIR_TAB_BACKWARD:
1047 case GTK_DIR_TAB_FORWARD:
1050 return FOCUS_WIDGET;
1055 case GTK_DIR_TAB_BACKWARD:
1059 return ltr ? FOCUS_NONE : FOCUS_LABEL;
1060 case GTK_DIR_TAB_FORWARD:
1064 return ltr ? FOCUS_LABEL : FOCUS_NONE;
1070 case GTK_DIR_TAB_BACKWARD:
1072 return FOCUS_WIDGET;
1074 return ltr ? FOCUS_WIDGET : FOCUS_CHILD;
1075 case GTK_DIR_TAB_FORWARD:
1079 return ltr ? FOCUS_CHILD : FOCUS_WIDGET;
1085 case GTK_DIR_TAB_BACKWARD:
1089 case GTK_DIR_TAB_FORWARD:
1096 g_assert_not_reached ();
1101 gtk_expander_focus (GtkWidget *widget,
1102 GtkDirectionType direction)
1104 GtkExpander *expander = GTK_EXPANDER (widget);
1106 if (!focus_current_site (expander, direction))
1108 GtkWidget *old_focus_child;
1109 gboolean widget_is_focus;
1110 FocusSite site = FOCUS_NONE;
1112 widget_is_focus = gtk_widget_is_focus (widget);
1113 old_focus_child = GTK_CONTAINER (widget)->focus_child;
1115 if (old_focus_child && old_focus_child == expander->priv->label_widget)
1117 else if (old_focus_child)
1119 else if (widget_is_focus)
1120 site = FOCUS_WIDGET;
1122 while ((site = get_next_site (expander, site, direction)) != FOCUS_NONE)
1124 if (focus_in_site (expander, site, direction))
1135 gtk_expander_add (GtkContainer *container,
1138 GTK_CONTAINER_CLASS (parent_class)->add (container, widget);
1140 gtk_widget_set_child_visible (widget, GTK_EXPANDER (container)->priv->expanded);
1141 gtk_widget_queue_resize (GTK_WIDGET (container));
1145 gtk_expander_remove (GtkContainer *container,
1148 GtkExpander *expander = GTK_EXPANDER (container);
1150 if (GTK_EXPANDER (expander)->priv->label_widget == widget)
1151 gtk_expander_set_label_widget (expander, NULL);
1153 GTK_CONTAINER_CLASS (parent_class)->remove (container, widget);
1157 gtk_expander_forall (GtkContainer *container,
1158 gboolean include_internals,
1159 GtkCallback callback,
1160 gpointer callback_data)
1162 GtkBin *bin = GTK_BIN (container);
1163 GtkExpanderPrivate *priv = GTK_EXPANDER (container)->priv;
1166 (* callback) (bin->child, callback_data);
1168 if (priv->label_widget)
1169 (* callback) (priv->label_widget, callback_data);
1173 gtk_expander_activate (GtkExpander *expander)
1175 gtk_expander_set_expanded (expander, !expander->priv->expanded);
1181 * @label: the text of the label
1183 * Creates a new expander using @label as the text of the label.
1185 * Return value: a new #GtkExpander widget.
1190 gtk_expander_new (const gchar *label)
1192 return g_object_new (GTK_TYPE_EXPANDER, "label", label, NULL);
1196 * gtk_expander_new_with_mnemonic:
1197 * @label: the text of the label with an underscore in front of the
1198 * mnemonic character
1200 * Creates a new expander using @label as the text of the label.
1201 * If characters in @label are preceded by an underscore, they are underlined.
1202 * If you need a literal underscore character in a label, use '__' (two
1203 * underscores). The first underlined character represents a keyboard
1204 * accelerator called a mnemonic.
1205 * Pressing Alt and that key activates the button.
1207 * Return value: a new #GtkExpander widget.
1212 gtk_expander_new_with_mnemonic (const gchar *label)
1214 return g_object_new (GTK_TYPE_EXPANDER,
1216 "use-underline", TRUE,
1221 gtk_expander_animation_timeout (GtkExpander *expander)
1223 GtkExpanderPrivate *priv = expander->priv;
1225 gboolean finish = FALSE;
1227 GDK_THREADS_ENTER();
1229 if (GTK_WIDGET_REALIZED (expander))
1231 get_expander_bounds (expander, &area);
1232 gdk_window_invalidate_rect (GTK_WIDGET (expander)->window, &area, TRUE);
1237 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1239 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1243 priv->expander_style = GTK_EXPANDER_EXPANDED;
1249 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1251 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1255 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1262 priv->animation_timeout = 0;
1263 if (GTK_BIN (expander)->child)
1264 gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1265 gtk_widget_queue_resize (GTK_WIDGET (expander));
1268 GDK_THREADS_LEAVE();
1274 gtk_expander_start_animation (GtkExpander *expander)
1276 GtkExpanderPrivate *priv = expander->priv;
1278 if (priv->animation_timeout)
1279 g_source_remove (priv->animation_timeout);
1281 priv->animation_timeout =
1283 (GSourceFunc) gtk_expander_animation_timeout,
1288 * gtk_expander_set_expanded:
1289 * @expander: a #GtkExpander
1290 * @expanded: whether the child widget is revealed
1292 * Sets the state of the expander. Set to %TRUE, if you want
1293 * the child widget to be revealed, and %FALSE if you want the
1294 * child widget to be hidden.
1299 gtk_expander_set_expanded (GtkExpander *expander,
1302 GtkExpanderPrivate *priv;
1304 g_return_if_fail (GTK_IS_EXPANDER (expander));
1306 priv = expander->priv;
1308 expanded = expanded != FALSE;
1310 if (priv->expanded != expanded)
1312 priv->expanded = expanded;
1314 if (GTK_WIDGET_REALIZED (expander))
1316 gtk_expander_start_animation (expander);
1320 priv->expander_style = expanded ? GTK_EXPANDER_EXPANDED :
1321 GTK_EXPANDER_COLLAPSED;
1323 if (GTK_BIN (expander)->child)
1325 gtk_widget_set_child_visible (GTK_BIN (expander)->child, priv->expanded);
1326 gtk_widget_queue_resize (GTK_WIDGET (expander));
1330 g_object_notify (G_OBJECT (expander), "expanded");
1335 * gtk_expander_get_expanded:
1336 * @expander:a #GtkExpander
1338 * Queries a #GtkExpander and returns its current state. Returns %TRUE
1339 * if the child widget is revealed.
1341 * See gtk_expander_set_expanded().
1343 * Return value: the current state of the expander.
1348 gtk_expander_get_expanded (GtkExpander *expander)
1350 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1352 return expander->priv->expanded;
1356 * gtk_expander_set_spacing:
1357 * @expander: a #GtkExpander
1358 * @spacing: distance between the expander and child in pixels.
1360 * Sets the spacing field of @expander, which is the number of pixels to
1361 * place between expander and the child.
1366 gtk_expander_set_spacing (GtkExpander *expander,
1369 g_return_if_fail (GTK_IS_EXPANDER (expander));
1370 g_return_if_fail (spacing >= 0);
1372 if (expander->priv->spacing != spacing)
1374 expander->priv->spacing = spacing;
1376 gtk_widget_queue_resize (GTK_WIDGET (expander));
1378 g_object_notify (G_OBJECT (expander), "spacing");
1383 * gtk_expander_get_spacing:
1384 * @expander: a #GtkExpander
1386 * Gets the value set by gtk_expander_set_spacing().
1388 * Return value: spacing between the expander and child.
1393 gtk_expander_get_spacing (GtkExpander *expander)
1395 g_return_val_if_fail (GTK_IS_EXPANDER (expander), 0);
1397 return expander->priv->spacing;
1401 * gtk_expander_set_label:
1402 * @expander: a #GtkExpander
1405 * Sets the text of the label of the expander to @label.
1407 * This will also clear any previously set labels.
1412 gtk_expander_set_label (GtkExpander *expander,
1415 g_return_if_fail (GTK_IS_EXPANDER (expander));
1419 gtk_expander_set_label_widget (expander, NULL);
1425 child = gtk_label_new (label);
1426 gtk_label_set_use_underline (GTK_LABEL (child), expander->priv->use_underline);
1427 gtk_label_set_use_markup (GTK_LABEL (child), expander->priv->use_markup);
1428 gtk_widget_show (child);
1430 gtk_expander_set_label_widget (expander, child);
1433 g_object_notify (G_OBJECT (expander), "label");
1437 * gtk_expander_get_label:
1438 * @expander: a #GtkExpander
1440 * Fetches the text from the label of the expander, as set by
1441 * gtk_expander_set_label(). If the label text has not
1442 * been set the return value will be %NULL. This will be the
1443 * case if you create an empty button with gtk_button_new() to
1444 * use as a container.
1446 * Return value: The text of the label widget. This string is owned
1447 * by the widget and must not be modified or freed.
1451 G_CONST_RETURN char *
1452 gtk_expander_get_label (GtkExpander *expander)
1454 GtkExpanderPrivate *priv;
1456 g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1458 priv = expander->priv;
1460 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1461 return gtk_label_get_text (GTK_LABEL (priv->label_widget));
1467 * gtk_expander_set_use_underline:
1468 * @expander: a #GtkExpander
1469 * @use_underline: %TRUE if underlines in the text indicate mnemonics
1471 * If true, an underline in the text of the expander label indicates
1472 * the next character should be used for the mnemonic accelerator key.
1477 gtk_expander_set_use_underline (GtkExpander *expander,
1478 gboolean use_underline)
1480 GtkExpanderPrivate *priv;
1482 g_return_if_fail (GTK_IS_EXPANDER (expander));
1484 priv = expander->priv;
1486 use_underline = use_underline != FALSE;
1488 if (priv->use_underline != use_underline)
1490 priv->use_underline = use_underline;
1492 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1493 gtk_label_set_use_underline (GTK_LABEL (priv->label_widget), use_underline);
1495 g_object_notify (G_OBJECT (expander), "use-underline");
1500 * gtk_expander_get_use_underline:
1501 * @expander: a #GtkExpander
1503 * Returns whether an embedded underline in the expander label indicates a
1504 * mnemonic. See gtk_expander_set_use_underline().
1506 * Return value: %TRUE if an embedded underline in the expander label
1507 * indicates the mnemonic accelerator keys.
1512 gtk_expander_get_use_underline (GtkExpander *expander)
1514 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1516 return expander->priv->use_underline;
1520 * gtk_expander_set_use_markup:
1521 * @expander: a #GtkExpander
1522 * @use_markup: %TRUE if the label's text should be parsed for markup
1524 * Sets whether the text of the label contains markup in <link
1525 * linkend="PangoMarkupFormat">Pango's text markup
1526 * language</link>. See gtk_label_set_markup().
1531 gtk_expander_set_use_markup (GtkExpander *expander,
1532 gboolean use_markup)
1534 GtkExpanderPrivate *priv;
1536 g_return_if_fail (GTK_IS_EXPANDER (expander));
1538 priv = expander->priv;
1540 use_markup = use_markup != FALSE;
1542 if (priv->use_markup != use_markup)
1544 priv->use_markup = use_markup;
1546 if (priv->label_widget && GTK_IS_LABEL (priv->label_widget))
1547 gtk_label_set_use_markup (GTK_LABEL (priv->label_widget), use_markup);
1549 g_object_notify (G_OBJECT (expander), "use-markup");
1554 * gtk_expander_get_use_markup:
1555 * @expander: a #GtkExpander
1557 * Returns whether the label's text is interpreted as marked up with
1558 * the <link linkend="PangoMarkupFormat">Pango text markup
1559 * language</link>. See gtk_expander_set_use_markup ().
1561 * Return value: %TRUE if the label's text will be parsed for markup
1566 gtk_expander_get_use_markup (GtkExpander *expander)
1568 g_return_val_if_fail (GTK_IS_EXPANDER (expander), FALSE);
1570 return expander->priv->use_markup;
1574 * gtk_expander_set_label_widget:
1575 * @expander: a #GtkExpander
1576 * @label_widget: the new label widget
1578 * Set the label widget for the expander. This is the widget
1579 * that will appear embedded alongside the expander arrow.
1584 gtk_expander_set_label_widget (GtkExpander *expander,
1585 GtkWidget *label_widget)
1587 GtkExpanderPrivate *priv;
1589 g_return_if_fail (GTK_IS_EXPANDER (expander));
1590 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1591 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1593 priv = expander->priv;
1595 if (priv->label_widget == label_widget)
1598 if (priv->label_widget)
1600 gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1601 gtk_widget_unparent (priv->label_widget);
1604 priv->label_widget = label_widget;
1608 priv->label_widget = label_widget;
1610 gtk_widget_set_parent (label_widget, GTK_WIDGET (expander));
1613 gtk_widget_set_state (label_widget, GTK_STATE_PRELIGHT);
1616 if (GTK_WIDGET_VISIBLE (expander))
1617 gtk_widget_queue_resize (GTK_WIDGET (expander));
1619 g_object_freeze_notify (G_OBJECT (expander));
1620 g_object_notify (G_OBJECT (expander), "label-widget");
1621 g_object_notify (G_OBJECT (expander), "label");
1622 g_object_thaw_notify (G_OBJECT (expander));
1626 * gtk_expander_get_label_widget:
1627 * @expander: a #GtkExpander
1629 * Retrieves the label widget for the frame. See
1630 * gtk_expander_set_label_widget().
1632 * Return value: the label widget, or %NULL if there is none.
1637 gtk_expander_get_label_widget (GtkExpander *expander)
1639 g_return_val_if_fail (GTK_IS_EXPANDER (expander), NULL);
1641 return expander->priv->label_widget;
1644 #define __GTK_EXPANDER_C__
1645 #include "gtkaliasdef.c"