1 /* GtkToolPalette -- A tool palette with categories and DnD support
2 * Copyright (C) 2008 Openismus GmbH
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "gtktoolpaletteprivate.h"
29 #include "gtktypebuiltins.h"
30 #include "gtkprivate.h"
33 #define ANIMATION_TIMEOUT 50
34 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
35 #define DEFAULT_ANIMATION_STATE TRUE
36 #define DEFAULT_EXPANDER_SIZE 16
37 #define DEFAULT_HEADER_SPACING 2
39 #define DEFAULT_LABEL ""
40 #define DEFAULT_COLLAPSED FALSE
41 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
44 * SECTION:gtktoolitemgroup
45 * @Short_description: A sub container used in a tool palette
46 * @Title: GtkToolItemGroup
48 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
49 * #GtkToolItem<!-- -->s to a palette like container with different
50 * categories and drag and drop support.
68 CHILD_PROP_HOMOGENEOUS,
75 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
77 struct _GtkToolItemGroupPrivate
80 GtkWidget *label_widget;
84 gint64 animation_start;
85 GSource *animation_timeout;
92 GtkSettings *settings;
93 gulong settings_connection;
95 PangoEllipsizeMode ellipsize;
101 struct _GtkToolItemGroupChild
105 guint homogeneous : 1;
111 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
113 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
117 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
119 return gtk_bin_get_child (GTK_BIN (group->priv->header));
122 static GtkOrientation
123 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
125 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
127 if (GTK_IS_TOOL_PALETTE (parent))
128 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
130 return GTK_ORIENTATION_VERTICAL;
133 static GtkToolbarStyle
134 gtk_tool_item_group_get_style (GtkToolShell *shell)
136 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
138 if (GTK_IS_TOOL_PALETTE (parent))
139 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
141 return GTK_TOOLBAR_ICONS;
145 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
147 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
149 if (GTK_IS_TOOL_PALETTE (parent))
150 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
152 return GTK_ICON_SIZE_SMALL_TOOLBAR;
155 static PangoEllipsizeMode
156 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
158 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
162 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
164 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
165 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
171 static GtkOrientation
172 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
174 return GTK_ORIENTATION_HORIZONTAL;
177 static GtkSizeGroup *
178 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
180 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
182 if (GTK_IS_TOOL_PALETTE (parent))
183 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
189 animation_change_notify (GtkToolItemGroup *group)
191 GtkSettings *settings = group->priv->settings;
195 g_object_get (settings,
196 "gtk-enable-animations", &animation,
199 animation = DEFAULT_ANIMATION_STATE;
201 group->priv->animation = animation;
205 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
206 const GParamSpec *pspec,
207 GtkToolItemGroup *group)
209 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
210 animation_change_notify (group);
214 gtk_tool_item_group_screen_changed (GtkWidget *widget,
215 GdkScreen *previous_screen)
217 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
218 GtkToolItemGroupPrivate* priv = group->priv;
219 GtkSettings *old_settings = priv->settings;
220 GtkSettings *settings;
222 if (gtk_widget_has_screen (GTK_WIDGET (group)))
223 settings = gtk_widget_get_settings (GTK_WIDGET (group));
227 if (settings == old_settings)
232 g_signal_handler_disconnect (old_settings, priv->settings_connection);
233 g_object_unref (old_settings);
238 priv->settings_connection =
239 g_signal_connect (settings, "notify",
240 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
242 priv->settings = g_object_ref (settings);
245 priv->settings = NULL;
247 animation_change_notify (group);
251 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
253 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
254 iface->get_orientation = gtk_tool_item_group_get_orientation;
255 iface->get_style = gtk_tool_item_group_get_style;
256 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
257 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
258 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
259 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
263 gtk_tool_item_group_header_draw_cb (GtkWidget *widget,
267 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
268 GtkToolItemGroupPrivate* priv = group->priv;
269 GtkOrientation orientation;
270 gint x, y, width, height;
271 GtkTextDirection direction;
272 GtkStyleContext *context;
273 GtkStateFlags state = 0;
275 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
276 direction = gtk_widget_get_direction (widget);
277 width = gtk_widget_get_allocated_width (widget);
278 height = gtk_widget_get_allocated_height (widget);
279 context = gtk_widget_get_style_context (widget);
281 if (!priv->collapsed)
282 state |= GTK_STATE_FLAG_ACTIVE;
284 gtk_style_context_save (context);
285 gtk_style_context_set_state (context, state);
286 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
288 if (GTK_ORIENTATION_VERTICAL == orientation)
290 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
292 if (GTK_TEXT_DIR_RTL == direction)
297 y = height / 2 - priv->expander_size / 2;
301 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
302 x = width / 2 - priv->expander_size / 2;
306 /* The expander is the only animatable region */
307 gtk_style_context_push_animatable_region (context, GUINT_TO_POINTER (1));
309 gtk_render_expander (context, cr, x, y,
311 priv->expander_size);
313 gtk_style_context_pop_animatable_region (context);
314 gtk_style_context_restore (context);
320 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
323 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
324 GtkToolItemGroupPrivate* priv = group->priv;
325 GtkWidget *parent = gtk_widget_get_parent (data);
327 if (priv->collapsed ||
328 !GTK_IS_TOOL_PALETTE (parent) ||
329 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
330 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
334 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
336 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
337 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
338 GtkWidget *widget = GTK_WIDGET (group);
339 GtkToolItemGroupPrivate* priv = group->priv;
341 GtkTextDirection direction = gtk_widget_get_direction (widget);
343 gtk_widget_style_get (widget,
344 "header-spacing", &(priv->header_spacing),
345 "expander-size", &(priv->expander_size),
348 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
350 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
352 case GTK_ORIENTATION_HORIZONTAL:
353 dy = priv->header_spacing + priv->expander_size;
355 if (GTK_IS_LABEL (label_widget))
357 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
358 if (GTK_TEXT_DIR_RTL == direction)
359 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
361 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
365 case GTK_ORIENTATION_VERTICAL:
366 dx = priv->header_spacing + priv->expander_size;
368 if (GTK_IS_LABEL (label_widget))
370 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
371 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
376 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
380 gtk_tool_item_group_init (GtkToolItemGroup *group)
382 GtkWidget *alignment;
383 GtkToolItemGroupPrivate* priv;
385 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
387 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
388 GTK_TYPE_TOOL_ITEM_GROUP,
389 GtkToolItemGroupPrivate);
391 priv->children = NULL;
392 priv->header_spacing = DEFAULT_HEADER_SPACING;
393 priv->expander_size = DEFAULT_EXPANDER_SIZE;
395 priv->label_widget = gtk_label_new (NULL);
396 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
397 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
398 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
399 gtk_widget_show_all (alignment);
401 gtk_widget_push_composite_child ();
402 priv->header = gtk_button_new ();
403 gtk_widget_set_composite_name (priv->header, "header");
404 gtk_widget_pop_composite_child ();
406 g_object_ref_sink (priv->header);
407 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
408 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
409 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
411 gtk_tool_item_group_header_adjust_style (group);
413 g_signal_connect_after (alignment, "draw",
414 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
417 g_signal_connect (priv->header, "clicked",
418 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
423 gtk_tool_item_group_set_property (GObject *object,
428 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
433 gtk_tool_item_group_set_label (group, g_value_get_string (value));
436 case PROP_LABEL_WIDGET:
437 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
441 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
445 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
449 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459 gtk_tool_item_group_get_property (GObject *object,
464 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
469 g_value_set_string (value, gtk_tool_item_group_get_label (group));
472 case PROP_LABEL_WIDGET:
473 g_value_set_object (value,
474 gtk_tool_item_group_get_label_widget (group));
478 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
482 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
486 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 gtk_tool_item_group_finalize (GObject *object)
498 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
500 if (group->priv->children)
502 g_list_free (group->priv->children);
503 group->priv->children = NULL;
506 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
510 gtk_tool_item_group_dispose (GObject *object)
512 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
513 GtkToolItemGroupPrivate* priv = group->priv;
517 /* disconnect focus tracking handler */
518 g_signal_handler_disconnect (priv->toplevel,
521 priv->focus_set_id = 0;
522 priv->toplevel = NULL;
525 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
529 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
530 GtkRequisition *item_size,
531 gboolean homogeneous_only,
532 gint *requested_rows)
534 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
536 if (GTK_IS_TOOL_PALETTE (parent))
537 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
539 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
543 gtk_tool_item_group_size_request (GtkWidget *widget,
544 GtkRequisition *requisition)
546 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
547 GtkToolItemGroupPrivate* priv = group->priv;
548 GtkOrientation orientation;
549 GtkRequisition item_size;
553 if (priv->children && gtk_tool_item_group_get_label_widget (group))
555 gtk_widget_get_preferred_size (priv->header,
557 gtk_widget_show (priv->header);
561 requisition->width = requisition->height = 0;
562 gtk_widget_hide (priv->header);
565 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
567 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
569 if (GTK_ORIENTATION_VERTICAL == orientation)
570 requisition->width = MAX (requisition->width, item_size.width);
572 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
574 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
575 requisition->width += border_width * 2;
576 requisition->height += border_width * 2;
580 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
584 GtkRequisition requisition;
586 gtk_tool_item_group_size_request (widget, &requisition);
588 *minimum = *natural = requisition.width;
592 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
596 GtkRequisition requisition;
598 gtk_tool_item_group_size_request (widget, &requisition);
600 *minimum = *natural = requisition.height;
605 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
606 GtkToolItemGroupChild *child)
608 GtkToolbarStyle style;
609 GtkOrientation orientation;
611 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
612 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
614 /* horizontal tool palettes with text style support only homogeneous items */
615 if (!child->homogeneous &&
616 GTK_ORIENTATION_HORIZONTAL == orientation &&
617 GTK_TOOLBAR_TEXT == style)
621 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
622 (GTK_ORIENTATION_VERTICAL == orientation ?
623 gtk_tool_item_get_visible_vertical (child->item) :
624 gtk_tool_item_get_visible_horizontal (child->item));
627 static inline unsigned
631 return (x + y - 1) / y;
635 gtk_tool_item_group_real_size_query (GtkWidget *widget,
636 GtkAllocation *allocation,
637 GtkRequisition *inquery)
639 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
640 GtkToolItemGroupPrivate* priv = group->priv;
642 GtkRequisition item_size;
643 GtkAllocation item_area;
645 GtkOrientation orientation;
650 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
651 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
653 /* figure out the size of homogeneous items */
654 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
656 if (GTK_ORIENTATION_VERTICAL == orientation)
657 item_size.width = MIN (item_size.width, allocation->width);
659 item_size.height = MIN (item_size.height, allocation->height);
661 item_size.width = MAX (item_size.width, 1);
662 item_size.height = MAX (item_size.height, 1);
665 item_area.height = 0;
667 /* figure out the required columns (n_columns) and rows (n_rows)
670 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
676 if (GTK_ORIENTATION_VERTICAL == orientation)
678 gboolean new_row = FALSE;
682 item_area.width = allocation->width - 2 * border_width;
683 n_columns = MAX (item_area.width / item_size.width, 1);
685 /* calculate required rows for n_columns columns */
686 for (it = priv->children; it != NULL; it = it->next)
688 GtkToolItemGroupChild *child = it->data;
690 if (!gtk_tool_item_group_is_item_visible (group, child))
693 if (new_row || child->new_row)
703 if (child->homogeneous)
706 if (col >= n_columns)
711 GtkRequisition req = {0, 0};
714 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
717 width = udiv (req.width, item_size.width);
725 if (col >= n_columns)
733 guint *row_min_width;
735 gboolean new_row = TRUE;
736 guint col = 0, min_col, max_col = 0, all_items = 0;
739 item_area.height = allocation->height - 2 * border_width;
740 n_rows = MAX (item_area.height / item_size.height, min_rows);
742 row_min_width = g_new0 (guint, n_rows);
744 /* calculate minimal and maximal required cols and minimal
747 for (it = priv->children; it != NULL; it = it->next)
749 GtkToolItemGroupChild *child = it->data;
751 if (!gtk_tool_item_group_is_item_visible (group, child))
754 if (new_row || child->new_row)
759 row_min_width[row] = 1;
765 if (child->homogeneous)
772 GtkRequisition req = {0, 0};
775 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
778 width = udiv (req.width, item_size.width);
783 row_min_width[row] = MAX (row_min_width[row], width);
786 max_col = MAX (max_col, col);
789 /* calculate minimal required cols */
790 min_col = udiv (all_items, n_rows);
792 for (i = 0; i <= row; i++)
794 min_col = MAX (min_col, row_min_width[i]);
797 /* simple linear search for minimal required columns
798 * for the given maximal number of rows (n_rows)
800 for (n_columns = min_col; n_columns < max_col; n_columns ++)
804 /* calculate required rows for n_columns columns */
805 for (it = priv->children; it != NULL; it = it->next)
807 GtkToolItemGroupChild *child = it->data;
809 if (!gtk_tool_item_group_is_item_visible (group, child))
812 if (new_row || child->new_row)
822 if (child->homogeneous)
825 if (col >= n_columns)
830 GtkRequisition req = {0, 0};
833 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
836 width = udiv (req.width, item_size.width);
844 if (col >= n_columns)
854 item_area.width = item_size.width * n_columns;
855 item_area.height = item_size.height * n_rows;
861 /* figure out header widget size */
862 if (gtk_widget_get_visible (priv->header))
864 GtkRequisition child_requisition;
866 gtk_widget_get_preferred_size (priv->header,
867 &child_requisition, NULL);
869 if (GTK_ORIENTATION_VERTICAL == orientation)
870 inquery->height += child_requisition.height;
872 inquery->width += child_requisition.width;
875 /* report effective widget size */
876 inquery->width += item_area.width + 2 * border_width;
877 inquery->height += item_area.height + 2 * border_width;
881 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
882 GtkAllocation *allocation)
884 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
885 GtkToolItemGroupPrivate* priv = group->priv;
886 GtkRequisition child_requisition;
887 GtkAllocation child_allocation;
889 GtkRequisition item_size;
890 GtkAllocation item_area;
892 GtkOrientation orientation;
896 gint n_columns, n_rows = 1;
899 GtkTextDirection direction;
901 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
903 direction = gtk_widget_get_direction (widget);
905 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
908 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
910 child_allocation.x = border_width;
911 child_allocation.y = border_width;
913 /* place the header widget */
914 if (gtk_widget_get_visible (priv->header))
916 gtk_widget_get_preferred_size (priv->header,
917 &child_requisition, NULL);
919 if (GTK_ORIENTATION_VERTICAL == orientation)
921 child_allocation.width = allocation->width;
922 child_allocation.height = child_requisition.height;
926 child_allocation.width = child_requisition.width;
927 child_allocation.height = allocation->height;
929 if (GTK_TEXT_DIR_RTL == direction)
930 child_allocation.x = allocation->width - border_width - child_allocation.width;
933 gtk_widget_size_allocate (priv->header, &child_allocation);
935 if (GTK_ORIENTATION_VERTICAL == orientation)
936 child_allocation.y += child_allocation.height;
937 else if (GTK_TEXT_DIR_RTL != direction)
938 child_allocation.x += child_allocation.width;
940 child_allocation.x = border_width;
943 child_requisition.width = child_requisition.height = 0;
945 /* figure out the size of homogeneous items */
946 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
948 item_size.width = MAX (item_size.width, 1);
949 item_size.height = MAX (item_size.height, 1);
951 /* figure out the available columns and size of item_area */
952 if (GTK_ORIENTATION_VERTICAL == orientation)
954 item_size.width = MIN (item_size.width, allocation->width);
956 item_area.width = allocation->width - 2 * border_width;
957 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
959 n_columns = MAX (item_area.width / item_size.width, 1);
961 item_size.width = item_area.width / n_columns;
965 item_size.height = MIN (item_size.height, allocation->height);
967 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
968 item_area.height = allocation->height - 2 * border_width;
970 n_columns = MAX (item_area.width / item_size.width, 1);
971 n_rows = MAX (item_area.height / item_size.height, min_rows);
973 item_size.height = item_area.height / n_rows;
976 item_area.x = child_allocation.x;
977 item_area.y = child_allocation.y;
979 /* when expanded or in transition, place the tool items in a grid like layout */
980 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
982 gint col = 0, row = 0;
984 for (it = priv->children; it != NULL; it = it->next)
986 GtkToolItemGroupChild *child = it->data;
989 if (!gtk_tool_item_group_is_item_visible (group, child))
991 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
996 /* for non homogeneous widgets request the required size */
997 child_requisition.width = 0;
999 if (!child->homogeneous)
1001 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
1002 &child_requisition, NULL);
1003 child_requisition.width = MIN (child_requisition.width, item_area.width);
1006 /* select next row if at end of row */
1007 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1011 child_allocation.y += child_allocation.height;
1016 /* calculate the position and size of the item */
1017 if (!child->homogeneous)
1023 col_width = udiv (child_requisition.width, item_size.width);
1025 col_width = n_columns - col;
1027 width = col_width * item_size.width;
1029 if (GTK_TEXT_DIR_RTL == direction)
1030 col_child = (n_columns - col - col_width);
1034 child_allocation.x = item_area.x + col_child * item_size.width;
1035 child_allocation.width = width;
1039 child_allocation.x =
1040 (item_area.x + col_child * item_size.width +
1041 (width - child_requisition.width) / 2);
1042 child_allocation.width = child_requisition.width;
1049 if (GTK_TEXT_DIR_RTL == direction)
1050 col_child = (n_columns - col - 1);
1052 child_allocation.x = item_area.x + col_child * item_size.width;
1053 child_allocation.width = item_size.width;
1058 child_allocation.height = item_size.height;
1060 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1061 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1064 child_allocation.y += item_size.height;
1067 /* or just hide all items, when collapsed */
1071 for (it = priv->children; it != NULL; it = it->next)
1073 GtkToolItemGroupChild *child = it->data;
1075 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1081 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1082 GtkAllocation *allocation)
1084 gtk_tool_item_group_real_size_allocate (widget, allocation);
1086 if (gtk_widget_get_mapped (widget))
1087 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1091 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1095 GtkAdjustment *adjustment;
1096 GtkAllocation allocation, p_allocation;
1099 /* Find this group's parent widget in the focused widget's anchestry. */
1100 for (p = widget; p; p = gtk_widget_get_parent (p))
1103 p = gtk_widget_get_parent (p);
1107 if (GTK_IS_TOOL_PALETTE (p))
1109 /* Check that the focused widgets is fully visible within
1110 * the group's parent widget and make it visible otherwise. */
1112 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1118 gtk_widget_get_allocation (widget, &allocation);
1119 gtk_widget_get_allocation (p, &p_allocation);
1121 /* Handle vertical adjustment. */
1122 if (gtk_widget_translate_coordinates
1123 (widget, p, 0, 0, NULL, &y) && y < 0)
1125 y += gtk_adjustment_get_value (adjustment);
1126 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1128 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1129 y > p_allocation.height)
1131 y += gtk_adjustment_get_value (adjustment);
1132 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1136 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1142 gtk_widget_get_allocation (widget, &allocation);
1143 gtk_widget_get_allocation (p, &p_allocation);
1145 /* Handle horizontal adjustment. */
1146 if (gtk_widget_translate_coordinates
1147 (widget, p, 0, 0, &x, NULL) && x < 0)
1149 x += gtk_adjustment_get_value (adjustment);
1150 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1152 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1153 x > p_allocation.width)
1155 x += gtk_adjustment_get_value (adjustment);
1156 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1165 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1166 GtkWidget *toplevel)
1168 GtkToolItemGroupPrivate* priv = group->priv;
1170 if (toplevel != priv->toplevel)
1174 /* Disconnect focus tracking handler. */
1175 g_signal_handler_disconnect (priv->toplevel,
1176 priv->focus_set_id);
1178 priv->focus_set_id = 0;
1179 priv->toplevel = NULL;
1184 /* Install focus tracking handler. We connect to the window's
1185 * set-focus signal instead of connecting to the focus signal of
1188 * 1) Reduce the number of signal handlers used.
1189 * 2) Avoid special handling for group headers.
1190 * 3) Catch focus grabs not only for direct children,
1191 * but also for nested widgets.
1193 priv->focus_set_id =
1194 g_signal_connect (toplevel, "set-focus",
1195 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1198 priv->toplevel = toplevel;
1204 gtk_tool_item_group_realize (GtkWidget *widget)
1206 GtkAllocation allocation;
1207 GtkWidget *toplevel_window;
1209 GdkWindowAttr attributes;
1210 GdkDisplay *display;
1211 gint attributes_mask;
1213 GtkStyleContext *context;
1215 gtk_widget_set_realized (widget, TRUE);
1217 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1218 context = gtk_widget_get_style_context (widget);
1220 gtk_widget_get_allocation (widget, &allocation);
1222 attributes.window_type = GDK_WINDOW_CHILD;
1223 attributes.x = allocation.x + border_width;
1224 attributes.y = allocation.y + border_width;
1225 attributes.width = allocation.width - border_width * 2;
1226 attributes.height = allocation.height - border_width * 2;
1227 attributes.wclass = GDK_INPUT_OUTPUT;
1228 attributes.visual = gtk_widget_get_visual (widget);
1229 attributes.event_mask = gtk_widget_get_events (widget)
1230 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1231 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1232 | GDK_BUTTON_MOTION_MASK;
1233 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1235 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1236 &attributes, attributes_mask);
1237 gtk_widget_set_window (widget, window);
1239 display = gdk_window_get_display (window);
1241 if (gdk_display_supports_composite (display))
1242 gdk_window_set_composited (window, TRUE);
1244 gdk_window_set_user_data (window, widget);
1246 gtk_style_context_set_background (context, window);
1248 gtk_container_forall (GTK_CONTAINER (widget),
1249 (GtkCallback) gtk_widget_set_parent_window,
1252 gtk_widget_queue_resize_no_redraw (widget);
1254 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1255 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1260 gtk_tool_item_group_unrealize (GtkWidget *widget)
1262 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1263 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1267 gtk_tool_item_group_style_updated (GtkWidget *widget)
1269 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1270 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
1274 gtk_tool_item_group_add (GtkContainer *container,
1277 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1278 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1280 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1281 GTK_TOOL_ITEM (widget), -1);
1285 gtk_tool_item_group_remove (GtkContainer *container,
1288 GtkToolItemGroup *group;
1289 GtkToolItemGroupPrivate* priv;
1292 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1293 group = GTK_TOOL_ITEM_GROUP (container);
1296 for (it = priv->children; it != NULL; it = it->next)
1298 GtkToolItemGroupChild *child_info = it->data;
1300 if ((GtkWidget *)child_info->item == child)
1302 g_object_unref (child);
1303 gtk_widget_unparent (child);
1305 g_free (child_info);
1306 priv->children = g_list_delete_link (priv->children, it);
1308 gtk_widget_queue_resize (GTK_WIDGET (container));
1315 gtk_tool_item_group_forall (GtkContainer *container,
1317 GtkCallback callback,
1318 gpointer callback_data)
1320 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1321 GtkToolItemGroupPrivate* priv = group->priv;
1324 if (internals && priv->header)
1325 callback (priv->header, callback_data);
1327 children = priv->children;
1330 GtkToolItemGroupChild *child = children->data;
1331 children = children->next; /* store pointer before call to callback
1332 because the child pointer is invalid if the
1333 child->item is removed from the item group
1336 callback (GTK_WIDGET (child->item), callback_data);
1341 gtk_tool_item_group_child_type (GtkContainer *container)
1343 return GTK_TYPE_TOOL_ITEM;
1346 static GtkToolItemGroupChild *
1347 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1355 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1356 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1358 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1360 GtkToolItemGroupChild *child = it->data;
1362 if (child->item == item)
1378 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1380 gboolean *homogeneous,
1385 GtkToolItemGroupChild *child;
1387 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1388 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1390 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1395 *expand = child->expand;
1398 *homogeneous = child->homogeneous;
1401 *fill = child->fill;
1404 *new_row = child->new_row;
1408 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1410 gboolean homogeneous,
1415 GtkToolItemGroupChild *child;
1416 gboolean changed = FALSE;
1418 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1419 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1421 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1425 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1427 if (child->homogeneous != homogeneous)
1429 child->homogeneous = homogeneous;
1431 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1433 if (child->expand != expand)
1435 child->expand = expand;
1437 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1439 if (child->fill != fill)
1443 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1445 if (child->new_row != new_row)
1447 child->new_row = new_row;
1449 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1452 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1455 && gtk_widget_get_visible (GTK_WIDGET (group))
1456 && gtk_widget_get_visible (GTK_WIDGET (item)))
1457 gtk_widget_queue_resize (GTK_WIDGET (group));
1461 gtk_tool_item_group_set_child_property (GtkContainer *container,
1464 const GValue *value,
1467 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1468 GtkToolItem *item = GTK_TOOL_ITEM (child);
1469 gboolean homogeneous, expand, fill, new_row;
1471 if (prop_id != CHILD_PROP_POSITION)
1472 gtk_tool_item_group_get_item_packing (group, item,
1480 case CHILD_PROP_HOMOGENEOUS:
1481 gtk_tool_item_group_set_item_packing (group, item,
1482 g_value_get_boolean (value),
1488 case CHILD_PROP_EXPAND:
1489 gtk_tool_item_group_set_item_packing (group, item,
1491 g_value_get_boolean (value),
1496 case CHILD_PROP_FILL:
1497 gtk_tool_item_group_set_item_packing (group, item,
1500 g_value_get_boolean (value),
1504 case CHILD_PROP_NEW_ROW:
1505 gtk_tool_item_group_set_item_packing (group, item,
1509 g_value_get_boolean (value));
1512 case CHILD_PROP_POSITION:
1513 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1517 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1523 gtk_tool_item_group_get_child_property (GtkContainer *container,
1529 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1530 GtkToolItem *item = GTK_TOOL_ITEM (child);
1531 gboolean homogeneous, expand, fill, new_row;
1533 if (prop_id != CHILD_PROP_POSITION)
1534 gtk_tool_item_group_get_item_packing (group, item,
1542 case CHILD_PROP_HOMOGENEOUS:
1543 g_value_set_boolean (value, homogeneous);
1546 case CHILD_PROP_EXPAND:
1547 g_value_set_boolean (value, expand);
1550 case CHILD_PROP_FILL:
1551 g_value_set_boolean (value, fill);
1554 case CHILD_PROP_NEW_ROW:
1555 g_value_set_boolean (value, new_row);
1558 case CHILD_PROP_POSITION:
1559 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1563 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1569 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1571 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1572 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1573 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1575 oclass->set_property = gtk_tool_item_group_set_property;
1576 oclass->get_property = gtk_tool_item_group_get_property;
1577 oclass->finalize = gtk_tool_item_group_finalize;
1578 oclass->dispose = gtk_tool_item_group_dispose;
1580 wclass->get_preferred_width = gtk_tool_item_group_get_preferred_width;
1581 wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1582 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1583 wclass->realize = gtk_tool_item_group_realize;
1584 wclass->unrealize = gtk_tool_item_group_unrealize;
1585 wclass->style_updated = gtk_tool_item_group_style_updated;
1586 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1588 cclass->add = gtk_tool_item_group_add;
1589 cclass->remove = gtk_tool_item_group_remove;
1590 cclass->forall = gtk_tool_item_group_forall;
1591 cclass->child_type = gtk_tool_item_group_child_type;
1592 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1593 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1595 g_object_class_install_property (oclass, PROP_LABEL,
1596 g_param_spec_string ("label",
1598 P_("The human-readable title of this item group"),
1600 GTK_PARAM_READWRITE));
1602 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1603 g_param_spec_object ("label-widget",
1605 P_("A widget to display in place of the usual label"),
1607 GTK_PARAM_READWRITE));
1609 g_object_class_install_property (oclass, PROP_COLLAPSED,
1610 g_param_spec_boolean ("collapsed",
1612 P_("Whether the group has been collapsed and items are hidden"),
1614 GTK_PARAM_READWRITE));
1616 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1617 g_param_spec_enum ("ellipsize",
1619 P_("Ellipsize for item group headers"),
1620 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1621 GTK_PARAM_READWRITE));
1623 g_object_class_install_property (oclass, PROP_RELIEF,
1624 g_param_spec_enum ("header-relief",
1625 P_("Header Relief"),
1626 P_("Relief of the group header button"),
1627 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1628 GTK_PARAM_READWRITE));
1630 gtk_widget_class_install_style_property (wclass,
1631 g_param_spec_int ("expander-size",
1632 P_("Expander Size"),
1633 P_("Size of the expander arrow"),
1636 DEFAULT_EXPANDER_SIZE,
1637 GTK_PARAM_READABLE));
1639 gtk_widget_class_install_style_property (wclass,
1640 g_param_spec_int ("header-spacing",
1641 P_("Header Spacing"),
1642 P_("Spacing between expander arrow and caption"),
1645 DEFAULT_HEADER_SPACING,
1646 GTK_PARAM_READABLE));
1648 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1649 g_param_spec_boolean ("homogeneous",
1651 P_("Whether the item should be the same size as other homogeneous items"),
1653 GTK_PARAM_READWRITE));
1655 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1656 g_param_spec_boolean ("expand",
1658 P_("Whether the item should receive extra space when the group grows"),
1660 GTK_PARAM_READWRITE));
1662 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1663 g_param_spec_boolean ("fill",
1665 P_("Whether the item should fill the available space"),
1667 GTK_PARAM_READWRITE));
1669 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1670 g_param_spec_boolean ("new-row",
1672 P_("Whether the item should start a new row"),
1674 GTK_PARAM_READWRITE));
1676 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1677 g_param_spec_int ("position",
1679 P_("Position of the item within this group"),
1683 GTK_PARAM_READWRITE));
1685 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1689 * gtk_tool_item_group_new:
1690 * @label: the label of the new group
1692 * Creates a new tool item group with label @label.
1694 * Returns: a new #GtkToolItemGroup.
1699 gtk_tool_item_group_new (const gchar *label)
1701 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1705 * gtk_tool_item_group_set_label:
1706 * @group: a #GtkToolItemGroup
1707 * @label: the new human-readable label of of the group
1709 * Sets the label of the tool item group. The label is displayed in the header
1715 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1718 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1721 gtk_tool_item_group_set_label_widget (group, NULL);
1724 GtkWidget *child = gtk_label_new (label);
1725 gtk_widget_show (child);
1727 gtk_tool_item_group_set_label_widget (group, child);
1730 g_object_notify (G_OBJECT (group), "label");
1734 * gtk_tool_item_group_set_label_widget:
1735 * @group: a #GtkToolItemGroup
1736 * @label_widget: the widget to be displayed in place of the usual label
1738 * Sets the label of the tool item group.
1739 * The label widget is displayed in the header of the group, in place
1740 * of the usual label.
1745 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1746 GtkWidget *label_widget)
1748 GtkToolItemGroupPrivate* priv;
1749 GtkWidget *alignment;
1751 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1752 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1753 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1757 if (priv->label_widget == label_widget)
1760 alignment = gtk_tool_item_group_get_alignment (group);
1762 if (priv->label_widget)
1764 gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1765 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1770 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1772 priv->label_widget = label_widget;
1774 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1775 gtk_widget_queue_resize (GTK_WIDGET (group));
1777 /* Only show the header widget if the group has children: */
1778 if (label_widget && priv->children)
1779 gtk_widget_show (priv->header);
1781 gtk_widget_hide (priv->header);
1783 g_object_freeze_notify (G_OBJECT (group));
1784 g_object_notify (G_OBJECT (group), "label-widget");
1785 g_object_notify (G_OBJECT (group), "label");
1786 g_object_thaw_notify (G_OBJECT (group));
1790 * gtk_tool_item_group_set_header_relief:
1791 * @group: a #GtkToolItemGroup
1792 * @style: the #GtkReliefStyle
1794 * Set the button relief of the group header.
1795 * See gtk_button_set_relief() for details.
1800 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1801 GtkReliefStyle style)
1803 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1805 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1809 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1811 return (g_source_get_time (group->priv->animation_timeout) -
1812 group->priv->animation_start) / 1000;
1816 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1818 GtkToolItemGroupPrivate* priv = group->priv;
1819 GtkWidget *widget = GTK_WIDGET (group);
1821 if (gtk_widget_get_realized (priv->header))
1823 GtkAllocation alignment_allocation;
1824 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1827 /* Find the header button's arrow area... */
1828 gtk_widget_get_allocation (alignment, &alignment_allocation);
1829 area.x = alignment_allocation.x;
1830 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1831 area.height = priv->expander_size;
1832 area.width = priv->expander_size;
1834 /* ... and invalidated it to get it animated. */
1835 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1838 if (gtk_widget_get_realized (widget))
1840 GtkAllocation allocation;
1841 GtkWidget *parent = gtk_widget_get_parent (widget);
1842 int x, y, width, height;
1844 /* Find the tool item area button's arrow area... */
1845 gtk_widget_get_allocation (widget, &allocation);
1846 width = allocation.width;
1847 height = allocation.height;
1849 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1851 if (gtk_widget_get_visible (priv->header))
1853 GtkAllocation header_allocation;
1855 gtk_widget_get_allocation (priv->header, &header_allocation);
1856 height -= header_allocation.height;
1857 y += header_allocation.height;
1860 /* ... and invalidated it to get it animated. */
1861 gtk_widget_queue_draw_area (parent, x, y, width, height);
1866 gtk_tool_item_group_animation_cb (gpointer data)
1868 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1869 GtkToolItemGroupPrivate* priv = group->priv;
1870 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1873 GDK_THREADS_ENTER ();
1875 /* Enque this early to reduce number of expose events. */
1876 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1878 gtk_tool_item_group_force_expose (group);
1880 /* Finish animation when done. */
1881 if (timestamp >= ANIMATION_DURATION)
1882 priv->animation_timeout = NULL;
1884 retval = (priv->animation_timeout != NULL);
1886 GDK_THREADS_LEAVE ();
1892 * gtk_tool_item_group_set_collapsed:
1893 * @group: a #GtkToolItemGroup
1894 * @collapsed: whether the @group should be collapsed or expanded
1896 * Sets whether the @group should be collapsed or expanded.
1901 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1905 GtkToolItemGroupPrivate* priv;
1907 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1911 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1912 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1913 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1914 GTK_WIDGET (group));
1915 if (collapsed != priv->collapsed)
1917 GtkStyleContext *context;
1919 if (priv->animation)
1921 if (priv->animation_timeout)
1922 g_source_destroy (priv->animation_timeout);
1924 priv->animation_start = g_get_monotonic_time ();
1925 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1927 g_source_set_callback (priv->animation_timeout,
1928 gtk_tool_item_group_animation_cb,
1930 g_source_attach (priv->animation_timeout, NULL);
1932 context = gtk_widget_get_style_context (gtk_bin_get_child (GTK_BIN (priv->header)));
1934 gtk_style_context_save (context);
1935 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
1937 gtk_style_context_notify_state_change (context,
1938 gtk_widget_get_window (priv->header),
1939 GUINT_TO_POINTER (1),
1940 GTK_STATE_FLAG_ACTIVE,
1943 gtk_style_context_restore (context);
1946 gtk_tool_item_group_force_expose (group);
1948 priv->collapsed = collapsed;
1949 g_object_notify (G_OBJECT (group), "collapsed");
1954 * gtk_tool_item_group_set_ellipsize:
1955 * @group: a #GtkToolItemGroup
1956 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1958 * Sets the ellipsization mode which should be used by labels in @group.
1963 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1964 PangoEllipsizeMode ellipsize)
1966 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1968 if (ellipsize != group->priv->ellipsize)
1970 group->priv->ellipsize = ellipsize;
1971 gtk_tool_item_group_header_adjust_style (group);
1972 g_object_notify (G_OBJECT (group), "ellipsize");
1973 _gtk_tool_item_group_palette_reconfigured (group);
1978 * gtk_tool_item_group_get_label:
1979 * @group: a #GtkToolItemGroup
1981 * Gets the label of @group.
1983 * Returns: the label of @group. The label is an internal string of @group
1984 * and must not be modified. Note that %NULL is returned if a custom
1985 * label has been set with gtk_tool_item_group_set_label_widget()
1989 G_CONST_RETURN gchar*
1990 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1992 GtkToolItemGroupPrivate *priv;
1994 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1998 if (GTK_IS_LABEL (priv->label_widget))
1999 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2005 * gtk_tool_item_group_get_label_widget:
2006 * @group: a #GtkToolItemGroup
2008 * Gets the label widget of @group.
2009 * See gtk_tool_item_group_set_label_widget().
2011 * Returns: (transfer none): the label widget of @group
2016 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2018 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2020 return gtk_bin_get_child (GTK_BIN (alignment));
2024 * gtk_tool_item_group_get_collapsed:
2025 * @group: a GtkToolItemGroup
2027 * Gets whether @group is collapsed or expanded.
2029 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2034 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2036 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2038 return group->priv->collapsed;
2042 * gtk_tool_item_group_get_ellipsize:
2043 * @group: a #GtkToolItemGroup
2045 * Gets the ellipsization mode of @group.
2047 * Returns: the #PangoEllipsizeMode of @group
2052 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2054 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2056 return group->priv->ellipsize;
2060 * gtk_tool_item_group_get_header_relief:
2061 * @group: a #GtkToolItemGroup
2063 * Gets the relief mode of the header button of @group.
2065 * Returns: the #GtkReliefStyle
2070 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2072 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2074 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2078 * gtk_tool_item_group_insert:
2079 * @group: a #GtkToolItemGroup
2080 * @item: the #GtkToolItem to insert into @group
2081 * @position: the position of @item in @group, starting with 0.
2082 * The position -1 means end of list.
2084 * Inserts @item at @position in the list of children of @group.
2089 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2093 GtkWidget *parent, *child_widget;
2094 GtkToolItemGroupChild *child;
2096 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2097 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2098 g_return_if_fail (position >= -1);
2100 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2102 child = g_new (GtkToolItemGroupChild, 1);
2103 child->item = g_object_ref_sink (item);
2104 child->homogeneous = TRUE;
2105 child->expand = FALSE;
2107 child->new_row = FALSE;
2109 group->priv->children = g_list_insert (group->priv->children, child, position);
2111 if (GTK_IS_TOOL_PALETTE (parent))
2112 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2114 child_widget = gtk_bin_get_child (GTK_BIN (item));
2116 if (GTK_IS_BUTTON (child_widget))
2117 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2119 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2123 * gtk_tool_item_group_set_item_position:
2124 * @group: a #GtkToolItemGroup
2125 * @item: the #GtkToolItem to move to a new position, should
2126 * be a child of @group.
2127 * @position: the new position of @item in @group, starting with 0.
2128 * The position -1 means end of list.
2130 * Sets the position of @item in the list of children of @group.
2135 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2141 GtkToolItemGroupChild *child;
2142 GtkToolItemGroupPrivate* priv;
2144 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2145 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2146 g_return_if_fail (position >= -1);
2148 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2151 g_return_if_fail (child != NULL);
2153 if (position == old_position)
2156 priv->children = g_list_delete_link (priv->children, link);
2157 priv->children = g_list_insert (priv->children, child, position);
2159 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2160 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2161 gtk_widget_get_visible (GTK_WIDGET (item)))
2162 gtk_widget_queue_resize (GTK_WIDGET (group));
2166 * gtk_tool_item_group_get_item_position:
2167 * @group: a #GtkToolItemGroup
2168 * @item: a #GtkToolItem
2170 * Gets the position of @item in @group as index.
2172 * Returns: the index of @item in @group or -1 if @item is no child of @group
2177 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2182 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2183 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2185 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2192 * gtk_tool_item_group_get_n_items:
2193 * @group: a #GtkToolItemGroup
2195 * Gets the number of tool items in @group.
2197 * Returns: the number of tool items in @group
2202 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2204 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2206 return g_list_length (group->priv->children);
2210 * gtk_tool_item_group_get_nth_item:
2211 * @group: a #GtkToolItemGroup
2214 * Gets the tool item at @index in group.
2216 * Returns: (transfer none): the #GtkToolItem at index
2221 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2224 GtkToolItemGroupChild *child;
2226 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2228 child = g_list_nth_data (group->priv->children, index);
2230 return child != NULL ? child->item : NULL;
2234 * gtk_tool_item_group_get_drop_item:
2235 * @group: a #GtkToolItemGroup
2236 * @x: the x position
2237 * @y: the y position
2239 * Gets the tool item at position (x, y).
2241 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2246 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2250 GtkAllocation allocation;
2253 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2255 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2257 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2258 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2260 for (it = group->priv->children; it != NULL; it = it->next)
2262 GtkToolItemGroupChild *child = it->data;
2263 GtkToolItem *item = child->item;
2266 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2269 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2271 x0 = x - allocation.x;
2272 y0 = y - allocation.y;
2274 if (x0 >= 0 && x0 < allocation.width &&
2275 y0 >= 0 && y0 < allocation.height)
2283 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2284 GtkRequisition *item_size,
2285 gboolean homogeneous_only,
2286 gint *requested_rows)
2288 GtkRequisition child_requisition;
2291 gboolean new_row = TRUE;
2293 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2294 g_return_if_fail (NULL != item_size);
2296 item_size->width = item_size->height = 0;
2298 for (it = group->priv->children; it != NULL; it = it->next)
2300 GtkToolItemGroupChild *child = it->data;
2302 if (!gtk_tool_item_group_is_item_visible (group, child))
2305 if (child->new_row || new_row)
2311 if (!child->homogeneous && child->expand)
2314 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2315 &child_requisition, NULL);
2317 if (!homogeneous_only || child->homogeneous)
2318 item_size->width = MAX (item_size->width, child_requisition.width);
2319 item_size->height = MAX (item_size->height, child_requisition.height);
2323 *requested_rows = rows;
2327 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2330 GtkAllocation allocation;
2331 GtkWidget *widget = GTK_WIDGET (group);
2332 GtkToolItemGroupPrivate* priv = group->priv;
2334 gtk_widget_get_allocation (widget, &allocation);
2336 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2340 if (priv->animation_timeout)
2342 GtkAllocation header_allocation;
2343 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2344 cairo_pattern_t *mask;
2347 if (GTK_ORIENTATION_VERTICAL == orientation)
2348 v1 = allocation.height;
2350 v1 = allocation.width;
2354 gtk_widget_get_allocation (priv->header, &header_allocation);
2355 if (!gtk_widget_get_visible (priv->header))
2357 else if (GTK_ORIENTATION_VERTICAL == orientation)
2358 v0 = MAX (v0, header_allocation.height);
2360 v0 = MAX (v0, header_allocation.width);
2362 v1 = MIN (v0 + 256, v1);
2364 if (GTK_ORIENTATION_VERTICAL == orientation)
2369 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2376 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2379 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2380 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2381 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2382 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2383 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2385 cairo_mask (cr, mask);
2386 cairo_pattern_destroy (mask);
2393 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2398 GtkRequisition requisition;
2399 GtkToolItemGroupPrivate* priv = group->priv;
2401 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2402 &requisition, NULL);
2404 if (!priv->collapsed || priv->animation_timeout)
2406 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2407 GtkRequisition inquery;
2410 allocation.width = limit;
2412 allocation.height = limit;
2414 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2415 &allocation, &inquery);
2418 inquery.height -= requisition.height;
2420 inquery.width -= requisition.width;
2422 if (priv->animation_timeout && animation)
2424 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2426 timestamp = MIN (timestamp, ANIMATION_DURATION);
2428 if (priv->collapsed)
2429 timestamp = ANIMATION_DURATION - timestamp;
2433 inquery.height *= timestamp;
2434 inquery.height /= ANIMATION_DURATION;
2438 inquery.width *= timestamp;
2439 inquery.width /= ANIMATION_DURATION;
2444 requisition.height += inquery.height;
2446 requisition.width += inquery.width;
2449 return (vertical ? requisition.height : requisition.width);
2453 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2456 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2460 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2463 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2467 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2470 if (GTK_IS_TOOL_ITEM (child))
2471 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2476 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2478 gtk_container_foreach (GTK_CONTAINER (group),
2479 gtk_tool_palette_reconfigured_foreach_item,
2482 gtk_tool_item_group_header_adjust_style (group);