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;
85 gint64 animation_start;
86 GSource *animation_timeout;
89 PangoEllipsizeMode ellipsize;
94 GtkSettings *settings;
95 gulong settings_connection;
100 struct _GtkToolItemGroupChild
104 guint homogeneous : 1;
110 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
112 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
113 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
116 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
118 return gtk_bin_get_child (GTK_BIN (group->priv->header));
121 static GtkOrientation
122 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
124 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
126 if (GTK_IS_TOOL_PALETTE (parent))
127 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
129 return GTK_ORIENTATION_VERTICAL;
132 static GtkToolbarStyle
133 gtk_tool_item_group_get_style (GtkToolShell *shell)
135 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
137 if (GTK_IS_TOOL_PALETTE (parent))
138 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
140 return GTK_TOOLBAR_ICONS;
144 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
146 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
148 if (GTK_IS_TOOL_PALETTE (parent))
149 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
151 return GTK_ICON_SIZE_SMALL_TOOLBAR;
154 static PangoEllipsizeMode
155 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
157 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
161 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
163 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
164 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
170 static GtkOrientation
171 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
173 return GTK_ORIENTATION_HORIZONTAL;
176 static GtkSizeGroup *
177 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
179 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
181 if (GTK_IS_TOOL_PALETTE (parent))
182 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
188 animation_change_notify (GtkToolItemGroup *group)
190 GtkSettings *settings = group->priv->settings;
194 g_object_get (settings,
195 "gtk-enable-animations", &animation,
198 animation = DEFAULT_ANIMATION_STATE;
200 group->priv->animation = animation;
204 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
205 const GParamSpec *pspec,
206 GtkToolItemGroup *group)
208 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
209 animation_change_notify (group);
213 gtk_tool_item_group_screen_changed (GtkWidget *widget,
214 GdkScreen *previous_screen)
216 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
217 GtkToolItemGroupPrivate* priv = group->priv;
218 GtkSettings *old_settings = priv->settings;
219 GtkSettings *settings;
221 if (gtk_widget_has_screen (GTK_WIDGET (group)))
222 settings = gtk_widget_get_settings (GTK_WIDGET (group));
226 if (settings == old_settings)
231 g_signal_handler_disconnect (old_settings, priv->settings_connection);
232 g_object_unref (old_settings);
237 priv->settings_connection =
238 g_signal_connect (settings, "notify",
239 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
241 priv->settings = g_object_ref (settings);
244 priv->settings = NULL;
246 animation_change_notify (group);
250 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
252 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
253 iface->get_orientation = gtk_tool_item_group_get_orientation;
254 iface->get_style = gtk_tool_item_group_get_style;
255 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
256 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
257 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
258 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
262 gtk_tool_item_group_header_draw_cb (GtkWidget *widget,
266 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
267 GtkToolItemGroupPrivate* priv = group->priv;
268 GtkOrientation orientation;
269 gint x, y, width, height;
270 GtkTextDirection direction;
271 GtkStyleContext *context;
272 GtkStateFlags state = 0;
274 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
275 direction = gtk_widget_get_direction (widget);
276 width = gtk_widget_get_allocated_width (widget);
277 height = gtk_widget_get_allocated_height (widget);
278 context = gtk_widget_get_style_context (widget);
280 if (!priv->collapsed)
281 state |= GTK_STATE_FLAG_ACTIVE;
283 gtk_style_context_save (context);
284 gtk_style_context_set_state (context, state);
285 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
287 if (GTK_ORIENTATION_VERTICAL == orientation)
289 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
291 if (GTK_TEXT_DIR_RTL == direction)
296 y = height / 2 - priv->expander_size / 2;
300 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
301 x = width / 2 - priv->expander_size / 2;
305 /* The expander is the only animatable region */
306 gtk_style_context_push_animatable_region (context, GUINT_TO_POINTER (1));
308 gtk_render_expander (context, cr, x, y,
310 priv->expander_size);
312 gtk_style_context_pop_animatable_region (context);
313 gtk_style_context_restore (context);
319 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
322 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
323 GtkToolItemGroupPrivate* priv = group->priv;
324 GtkWidget *parent = gtk_widget_get_parent (data);
326 if (priv->collapsed ||
327 !GTK_IS_TOOL_PALETTE (parent) ||
328 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
329 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
333 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
335 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
336 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
337 GtkWidget *widget = GTK_WIDGET (group);
338 GtkToolItemGroupPrivate* priv = group->priv;
340 GtkTextDirection direction = gtk_widget_get_direction (widget);
342 gtk_widget_style_get (widget,
343 "header-spacing", &(priv->header_spacing),
344 "expander-size", &(priv->expander_size),
347 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
349 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
351 case GTK_ORIENTATION_HORIZONTAL:
352 dy = priv->header_spacing + priv->expander_size;
354 if (GTK_IS_LABEL (label_widget))
356 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
357 if (GTK_TEXT_DIR_RTL == direction)
358 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
360 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
364 case GTK_ORIENTATION_VERTICAL:
365 dx = priv->header_spacing + priv->expander_size;
367 if (GTK_IS_LABEL (label_widget))
369 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
370 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
375 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
379 gtk_tool_item_group_init (GtkToolItemGroup *group)
381 GtkWidget *alignment;
382 GtkToolItemGroupPrivate* priv;
384 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
386 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
387 GTK_TYPE_TOOL_ITEM_GROUP,
388 GtkToolItemGroupPrivate);
390 priv->children = NULL;
391 priv->header_spacing = DEFAULT_HEADER_SPACING;
392 priv->expander_size = DEFAULT_EXPANDER_SIZE;
394 priv->label_widget = gtk_label_new (NULL);
395 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
396 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
397 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
398 gtk_widget_show_all (alignment);
400 gtk_widget_push_composite_child ();
401 priv->header = gtk_button_new ();
402 gtk_widget_set_composite_name (priv->header, "header");
403 gtk_widget_pop_composite_child ();
405 g_object_ref_sink (priv->header);
406 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
407 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
408 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
410 gtk_tool_item_group_header_adjust_style (group);
412 g_signal_connect_after (alignment, "draw",
413 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
416 g_signal_connect (priv->header, "clicked",
417 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
422 gtk_tool_item_group_set_property (GObject *object,
427 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
432 gtk_tool_item_group_set_label (group, g_value_get_string (value));
435 case PROP_LABEL_WIDGET:
436 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
440 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
444 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
448 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gtk_tool_item_group_get_property (GObject *object,
463 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
468 g_value_set_string (value, gtk_tool_item_group_get_label (group));
471 case PROP_LABEL_WIDGET:
472 g_value_set_object (value,
473 gtk_tool_item_group_get_label_widget (group));
477 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
481 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
485 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gtk_tool_item_group_finalize (GObject *object)
497 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
499 if (group->priv->children)
501 g_list_free (group->priv->children);
502 group->priv->children = NULL;
505 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
509 gtk_tool_item_group_dispose (GObject *object)
511 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
512 GtkToolItemGroupPrivate* priv = group->priv;
516 /* disconnect focus tracking handler */
517 g_signal_handler_disconnect (priv->toplevel,
520 priv->focus_set_id = 0;
521 priv->toplevel = NULL;
524 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
528 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
529 GtkRequisition *item_size,
530 gboolean homogeneous_only,
531 gint *requested_rows)
533 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
535 if (GTK_IS_TOOL_PALETTE (parent))
536 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
538 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
542 gtk_tool_item_group_size_request (GtkWidget *widget,
543 GtkRequisition *requisition)
545 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
546 GtkToolItemGroupPrivate* priv = group->priv;
547 GtkOrientation orientation;
548 GtkRequisition item_size;
552 if (priv->children && gtk_tool_item_group_get_label_widget (group))
554 gtk_widget_get_preferred_size (priv->header,
556 gtk_widget_show (priv->header);
560 requisition->width = requisition->height = 0;
561 gtk_widget_hide (priv->header);
564 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
566 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
568 if (GTK_ORIENTATION_VERTICAL == orientation)
569 requisition->width = MAX (requisition->width, item_size.width);
571 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
573 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
574 requisition->width += border_width * 2;
575 requisition->height += border_width * 2;
579 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
583 GtkRequisition requisition;
585 gtk_tool_item_group_size_request (widget, &requisition);
587 *minimum = *natural = requisition.width;
591 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
595 GtkRequisition requisition;
597 gtk_tool_item_group_size_request (widget, &requisition);
599 *minimum = *natural = requisition.height;
604 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
605 GtkToolItemGroupChild *child)
607 GtkToolbarStyle style;
608 GtkOrientation orientation;
610 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
611 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
613 /* horizontal tool palettes with text style support only homogeneous items */
614 if (!child->homogeneous &&
615 GTK_ORIENTATION_HORIZONTAL == orientation &&
616 GTK_TOOLBAR_TEXT == style)
620 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
621 (GTK_ORIENTATION_VERTICAL == orientation ?
622 gtk_tool_item_get_visible_vertical (child->item) :
623 gtk_tool_item_get_visible_horizontal (child->item));
626 static inline unsigned
630 return (x + y - 1) / y;
634 gtk_tool_item_group_real_size_query (GtkWidget *widget,
635 GtkAllocation *allocation,
636 GtkRequisition *inquery)
638 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
639 GtkToolItemGroupPrivate* priv = group->priv;
641 GtkRequisition item_size;
642 GtkAllocation item_area;
644 GtkOrientation orientation;
645 GtkToolbarStyle style;
650 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
651 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
652 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
654 /* figure out the size of homogeneous items */
655 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
657 if (GTK_ORIENTATION_VERTICAL == orientation)
658 item_size.width = MIN (item_size.width, allocation->width);
660 item_size.height = MIN (item_size.height, allocation->height);
662 item_size.width = MAX (item_size.width, 1);
663 item_size.height = MAX (item_size.height, 1);
666 item_area.height = 0;
668 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
669 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
675 if (GTK_ORIENTATION_VERTICAL == orientation)
677 gboolean new_row = FALSE;
681 item_area.width = allocation->width - 2 * border_width;
682 n_columns = MAX (item_area.width / item_size.width, 1);
684 /* calculate required rows for n_columns columns */
685 for (it = priv->children; it != NULL; it = it->next)
687 GtkToolItemGroupChild *child = it->data;
689 if (!gtk_tool_item_group_is_item_visible (group, child))
692 if (new_row || child->new_row)
702 if (child->homogeneous)
705 if (col >= n_columns)
710 GtkRequisition req = {0, 0};
713 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
716 width = udiv (req.width, item_size.width);
724 if (col >= n_columns)
732 guint *row_min_width;
734 gboolean new_row = TRUE;
735 guint col = 0, min_col, max_col = 0, all_items = 0;
738 item_area.height = allocation->height - 2 * border_width;
739 n_rows = MAX (item_area.height / item_size.height, min_rows);
741 row_min_width = g_new0 (guint, n_rows);
743 /* calculate minimal and maximal required cols and minimal required rows */
744 for (it = priv->children; it != NULL; it = it->next)
746 GtkToolItemGroupChild *child = it->data;
748 if (!gtk_tool_item_group_is_item_visible (group, child))
751 if (new_row || child->new_row)
756 row_min_width[row] = 1;
762 if (child->homogeneous)
769 GtkRequisition req = {0, 0};
772 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
775 width = udiv (req.width, item_size.width);
780 row_min_width[row] = MAX (row_min_width[row], width);
783 max_col = MAX (max_col, col);
786 /* calculate minimal required cols */
787 min_col = udiv (all_items, n_rows);
789 for (i = 0; i <= row; i++)
791 min_col = MAX (min_col, row_min_width[i]);
794 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
795 for (n_columns = min_col; n_columns < max_col; n_columns ++)
799 /* calculate required rows for n_columns columns */
800 for (it = priv->children; it != NULL; it = it->next)
802 GtkToolItemGroupChild *child = it->data;
804 if (!gtk_tool_item_group_is_item_visible (group, child))
807 if (new_row || child->new_row)
817 if (child->homogeneous)
820 if (col >= n_columns)
825 GtkRequisition req = {0, 0};
828 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
831 width = udiv (req.width, item_size.width);
839 if (col >= n_columns)
849 item_area.width = item_size.width * n_columns;
850 item_area.height = item_size.height * n_rows;
856 /* figure out header widget size */
857 if (gtk_widget_get_visible (priv->header))
859 GtkRequisition child_requisition;
861 gtk_widget_get_preferred_size (priv->header,
862 &child_requisition, NULL);
864 if (GTK_ORIENTATION_VERTICAL == orientation)
865 inquery->height += child_requisition.height;
867 inquery->width += child_requisition.width;
870 /* report effective widget size */
871 inquery->width += item_area.width + 2 * border_width;
872 inquery->height += item_area.height + 2 * border_width;
876 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
877 GtkAllocation *allocation)
879 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
880 GtkToolItemGroupPrivate* priv = group->priv;
881 GtkRequisition child_requisition;
882 GtkAllocation child_allocation;
884 GtkRequisition item_size;
885 GtkAllocation item_area;
887 GtkOrientation orientation;
888 GtkToolbarStyle style;
892 gint n_columns, n_rows = 1;
895 GtkTextDirection direction;
897 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
899 direction = gtk_widget_get_direction (widget);
901 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
902 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
905 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
907 child_allocation.x = border_width;
908 child_allocation.y = border_width;
910 /* place the header widget */
911 if (gtk_widget_get_visible (priv->header))
913 gtk_widget_get_preferred_size (priv->header,
914 &child_requisition, NULL);
916 if (GTK_ORIENTATION_VERTICAL == orientation)
918 child_allocation.width = allocation->width;
919 child_allocation.height = child_requisition.height;
923 child_allocation.width = child_requisition.width;
924 child_allocation.height = allocation->height;
926 if (GTK_TEXT_DIR_RTL == direction)
927 child_allocation.x = allocation->width - border_width - child_allocation.width;
930 gtk_widget_size_allocate (priv->header, &child_allocation);
932 if (GTK_ORIENTATION_VERTICAL == orientation)
933 child_allocation.y += child_allocation.height;
934 else if (GTK_TEXT_DIR_RTL != direction)
935 child_allocation.x += child_allocation.width;
937 child_allocation.x = border_width;
940 child_requisition.width = child_requisition.height = 0;
942 /* figure out the size of homogeneous items */
943 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
945 item_size.width = MAX (item_size.width, 1);
946 item_size.height = MAX (item_size.height, 1);
948 /* figure out the available columns and size of item_area */
949 if (GTK_ORIENTATION_VERTICAL == orientation)
951 item_size.width = MIN (item_size.width, allocation->width);
953 item_area.width = allocation->width - 2 * border_width;
954 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
956 n_columns = MAX (item_area.width / item_size.width, 1);
958 item_size.width = item_area.width / n_columns;
962 item_size.height = MIN (item_size.height, allocation->height);
964 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
965 item_area.height = allocation->height - 2 * border_width;
967 n_columns = MAX (item_area.width / item_size.width, 1);
968 n_rows = MAX (item_area.height / item_size.height, min_rows);
970 item_size.height = item_area.height / n_rows;
973 item_area.x = child_allocation.x;
974 item_area.y = child_allocation.y;
976 /* when expanded or in transition, place the tool items in a grid like layout */
977 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
979 gint col = 0, row = 0;
981 for (it = priv->children; it != NULL; it = it->next)
983 GtkToolItemGroupChild *child = it->data;
986 if (!gtk_tool_item_group_is_item_visible (group, child))
988 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
993 /* for non homogeneous widgets request the required size */
994 child_requisition.width = 0;
996 if (!child->homogeneous)
998 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
999 &child_requisition, NULL);
1000 child_requisition.width = MIN (child_requisition.width, item_area.width);
1003 /* select next row if at end of row */
1004 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1008 child_allocation.y += child_allocation.height;
1013 /* calculate the position and size of the item */
1014 if (!child->homogeneous)
1020 col_width = udiv (child_requisition.width, item_size.width);
1022 col_width = n_columns - col;
1024 width = col_width * item_size.width;
1026 if (GTK_TEXT_DIR_RTL == direction)
1027 col_child = (n_columns - col - col_width);
1031 child_allocation.x = item_area.x + col_child * item_size.width;
1032 child_allocation.width = width;
1036 child_allocation.x =
1037 (item_area.x + col_child * item_size.width +
1038 (width - child_requisition.width) / 2);
1039 child_allocation.width = child_requisition.width;
1046 if (GTK_TEXT_DIR_RTL == direction)
1047 col_child = (n_columns - col - 1);
1049 child_allocation.x = item_area.x + col_child * item_size.width;
1050 child_allocation.width = item_size.width;
1055 child_allocation.height = item_size.height;
1057 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1058 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1061 child_allocation.y += item_size.height;
1064 /* or just hide all items, when collapsed */
1068 for (it = priv->children; it != NULL; it = it->next)
1070 GtkToolItemGroupChild *child = it->data;
1072 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1078 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1079 GtkAllocation *allocation)
1081 gtk_tool_item_group_real_size_allocate (widget, allocation);
1083 if (gtk_widget_get_mapped (widget))
1084 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1088 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1092 GtkAdjustment *adjustment;
1093 GtkAllocation allocation, p_allocation;
1096 /* Find this group's parent widget in the focused widget's anchestry. */
1097 for (p = widget; p; p = gtk_widget_get_parent (p))
1100 p = gtk_widget_get_parent (p);
1104 if (GTK_IS_TOOL_PALETTE (p))
1106 /* Check that the focused widgets is fully visible within
1107 * the group's parent widget and make it visible otherwise. */
1109 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1115 gtk_widget_get_allocation (widget, &allocation);
1116 gtk_widget_get_allocation (p, &p_allocation);
1118 /* Handle vertical adjustment. */
1119 if (gtk_widget_translate_coordinates
1120 (widget, p, 0, 0, NULL, &y) && y < 0)
1122 y += gtk_adjustment_get_value (adjustment);
1123 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1125 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1126 y > p_allocation.height)
1128 y += gtk_adjustment_get_value (adjustment);
1129 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1133 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1139 gtk_widget_get_allocation (widget, &allocation);
1140 gtk_widget_get_allocation (p, &p_allocation);
1142 /* Handle horizontal adjustment. */
1143 if (gtk_widget_translate_coordinates
1144 (widget, p, 0, 0, &x, NULL) && x < 0)
1146 x += gtk_adjustment_get_value (adjustment);
1147 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1149 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1150 x > p_allocation.width)
1152 x += gtk_adjustment_get_value (adjustment);
1153 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1162 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1163 GtkWidget *toplevel)
1165 GtkToolItemGroupPrivate* priv = group->priv;
1167 if (toplevel != priv->toplevel)
1171 /* Disconnect focus tracking handler. */
1172 g_signal_handler_disconnect (priv->toplevel,
1173 priv->focus_set_id);
1175 priv->focus_set_id = 0;
1176 priv->toplevel = NULL;
1181 /* Install focus tracking handler. We connect to the window's
1182 * set-focus signal instead of connecting to the focus signal of
1185 * 1) Reduce the number of signal handlers used.
1186 * 2) Avoid special handling for group headers.
1187 * 3) Catch focus grabs not only for direct children,
1188 * but also for nested widgets.
1190 priv->focus_set_id =
1191 g_signal_connect (toplevel, "set-focus",
1192 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1195 priv->toplevel = toplevel;
1201 gtk_tool_item_group_realize (GtkWidget *widget)
1203 GtkAllocation allocation;
1204 GtkWidget *toplevel_window;
1206 GdkWindowAttr attributes;
1207 GdkDisplay *display;
1208 gint attributes_mask;
1210 GtkStyleContext *context;
1212 gtk_widget_set_realized (widget, TRUE);
1214 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1215 context = gtk_widget_get_style_context (widget);
1217 gtk_widget_get_allocation (widget, &allocation);
1219 attributes.window_type = GDK_WINDOW_CHILD;
1220 attributes.x = allocation.x + border_width;
1221 attributes.y = allocation.y + border_width;
1222 attributes.width = allocation.width - border_width * 2;
1223 attributes.height = allocation.height - border_width * 2;
1224 attributes.wclass = GDK_INPUT_OUTPUT;
1225 attributes.visual = gtk_widget_get_visual (widget);
1226 attributes.event_mask = gtk_widget_get_events (widget)
1227 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1228 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1229 | GDK_BUTTON_MOTION_MASK;
1230 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1232 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1233 &attributes, attributes_mask);
1234 gtk_widget_set_window (widget, window);
1236 display = gdk_window_get_display (window);
1238 if (gdk_display_supports_composite (display))
1239 gdk_window_set_composited (window, TRUE);
1241 gdk_window_set_user_data (window, widget);
1243 gtk_style_context_set_background (context, window);
1245 gtk_container_forall (GTK_CONTAINER (widget),
1246 (GtkCallback) gtk_widget_set_parent_window,
1249 gtk_widget_queue_resize_no_redraw (widget);
1251 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1252 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1257 gtk_tool_item_group_unrealize (GtkWidget *widget)
1259 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1260 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1264 gtk_tool_item_group_style_updated (GtkWidget *widget)
1266 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1267 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
1271 gtk_tool_item_group_add (GtkContainer *container,
1274 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1275 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1277 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1278 GTK_TOOL_ITEM (widget), -1);
1282 gtk_tool_item_group_remove (GtkContainer *container,
1285 GtkToolItemGroup *group;
1286 GtkToolItemGroupPrivate* priv;
1289 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1290 group = GTK_TOOL_ITEM_GROUP (container);
1293 for (it = priv->children; it != NULL; it = it->next)
1295 GtkToolItemGroupChild *child_info = it->data;
1297 if ((GtkWidget *)child_info->item == child)
1299 g_object_unref (child);
1300 gtk_widget_unparent (child);
1302 g_free (child_info);
1303 priv->children = g_list_delete_link (priv->children, it);
1305 gtk_widget_queue_resize (GTK_WIDGET (container));
1312 gtk_tool_item_group_forall (GtkContainer *container,
1314 GtkCallback callback,
1315 gpointer callback_data)
1317 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1318 GtkToolItemGroupPrivate* priv = group->priv;
1321 if (internals && priv->header)
1322 callback (priv->header, callback_data);
1324 children = priv->children;
1327 GtkToolItemGroupChild *child = children->data;
1328 children = children->next; /* store pointer before call to callback
1329 because the child pointer is invalid if the
1330 child->item is removed from the item group
1333 callback (GTK_WIDGET (child->item), callback_data);
1338 gtk_tool_item_group_child_type (GtkContainer *container)
1340 return GTK_TYPE_TOOL_ITEM;
1343 static GtkToolItemGroupChild *
1344 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1352 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1353 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1355 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1357 GtkToolItemGroupChild *child = it->data;
1359 if (child->item == item)
1375 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1377 gboolean *homogeneous,
1382 GtkToolItemGroupChild *child;
1384 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1385 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1387 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1392 *expand = child->expand;
1395 *homogeneous = child->homogeneous;
1398 *fill = child->fill;
1401 *new_row = child->new_row;
1405 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1407 gboolean homogeneous,
1412 GtkToolItemGroupChild *child;
1413 gboolean changed = FALSE;
1415 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1416 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1418 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1422 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1424 if (child->homogeneous != homogeneous)
1426 child->homogeneous = homogeneous;
1428 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1430 if (child->expand != expand)
1432 child->expand = expand;
1434 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1436 if (child->fill != fill)
1440 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1442 if (child->new_row != new_row)
1444 child->new_row = new_row;
1446 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1449 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1452 && gtk_widget_get_visible (GTK_WIDGET (group))
1453 && gtk_widget_get_visible (GTK_WIDGET (item)))
1454 gtk_widget_queue_resize (GTK_WIDGET (group));
1458 gtk_tool_item_group_set_child_property (GtkContainer *container,
1461 const GValue *value,
1464 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1465 GtkToolItem *item = GTK_TOOL_ITEM (child);
1466 gboolean homogeneous, expand, fill, new_row;
1468 if (prop_id != CHILD_PROP_POSITION)
1469 gtk_tool_item_group_get_item_packing (group, item,
1477 case CHILD_PROP_HOMOGENEOUS:
1478 gtk_tool_item_group_set_item_packing (group, item,
1479 g_value_get_boolean (value),
1485 case CHILD_PROP_EXPAND:
1486 gtk_tool_item_group_set_item_packing (group, item,
1488 g_value_get_boolean (value),
1493 case CHILD_PROP_FILL:
1494 gtk_tool_item_group_set_item_packing (group, item,
1497 g_value_get_boolean (value),
1501 case CHILD_PROP_NEW_ROW:
1502 gtk_tool_item_group_set_item_packing (group, item,
1506 g_value_get_boolean (value));
1509 case CHILD_PROP_POSITION:
1510 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1514 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1520 gtk_tool_item_group_get_child_property (GtkContainer *container,
1526 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1527 GtkToolItem *item = GTK_TOOL_ITEM (child);
1528 gboolean homogeneous, expand, fill, new_row;
1530 if (prop_id != CHILD_PROP_POSITION)
1531 gtk_tool_item_group_get_item_packing (group, item,
1539 case CHILD_PROP_HOMOGENEOUS:
1540 g_value_set_boolean (value, homogeneous);
1543 case CHILD_PROP_EXPAND:
1544 g_value_set_boolean (value, expand);
1547 case CHILD_PROP_FILL:
1548 g_value_set_boolean (value, fill);
1551 case CHILD_PROP_NEW_ROW:
1552 g_value_set_boolean (value, new_row);
1555 case CHILD_PROP_POSITION:
1556 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1560 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1566 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1568 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1569 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1570 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1572 oclass->set_property = gtk_tool_item_group_set_property;
1573 oclass->get_property = gtk_tool_item_group_get_property;
1574 oclass->finalize = gtk_tool_item_group_finalize;
1575 oclass->dispose = gtk_tool_item_group_dispose;
1577 wclass->get_preferred_width = gtk_tool_item_group_get_preferred_width;
1578 wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1579 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1580 wclass->realize = gtk_tool_item_group_realize;
1581 wclass->unrealize = gtk_tool_item_group_unrealize;
1582 wclass->style_updated = gtk_tool_item_group_style_updated;
1583 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1585 cclass->add = gtk_tool_item_group_add;
1586 cclass->remove = gtk_tool_item_group_remove;
1587 cclass->forall = gtk_tool_item_group_forall;
1588 cclass->child_type = gtk_tool_item_group_child_type;
1589 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1590 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1592 g_object_class_install_property (oclass, PROP_LABEL,
1593 g_param_spec_string ("label",
1595 P_("The human-readable title of this item group"),
1597 GTK_PARAM_READWRITE));
1599 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1600 g_param_spec_object ("label-widget",
1602 P_("A widget to display in place of the usual label"),
1604 GTK_PARAM_READWRITE));
1606 g_object_class_install_property (oclass, PROP_COLLAPSED,
1607 g_param_spec_boolean ("collapsed",
1609 P_("Whether the group has been collapsed and items are hidden"),
1611 GTK_PARAM_READWRITE));
1613 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1614 g_param_spec_enum ("ellipsize",
1616 P_("Ellipsize for item group headers"),
1617 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1618 GTK_PARAM_READWRITE));
1620 g_object_class_install_property (oclass, PROP_RELIEF,
1621 g_param_spec_enum ("header-relief",
1622 P_("Header Relief"),
1623 P_("Relief of the group header button"),
1624 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1625 GTK_PARAM_READWRITE));
1627 gtk_widget_class_install_style_property (wclass,
1628 g_param_spec_int ("expander-size",
1629 P_("Expander Size"),
1630 P_("Size of the expander arrow"),
1633 DEFAULT_EXPANDER_SIZE,
1634 GTK_PARAM_READABLE));
1636 gtk_widget_class_install_style_property (wclass,
1637 g_param_spec_int ("header-spacing",
1638 P_("Header Spacing"),
1639 P_("Spacing between expander arrow and caption"),
1642 DEFAULT_HEADER_SPACING,
1643 GTK_PARAM_READABLE));
1645 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1646 g_param_spec_boolean ("homogeneous",
1648 P_("Whether the item should be the same size as other homogeneous items"),
1650 GTK_PARAM_READWRITE));
1652 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1653 g_param_spec_boolean ("expand",
1655 P_("Whether the item should receive extra space when the group grows"),
1657 GTK_PARAM_READWRITE));
1659 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1660 g_param_spec_boolean ("fill",
1662 P_("Whether the item should fill the available space"),
1664 GTK_PARAM_READWRITE));
1666 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1667 g_param_spec_boolean ("new-row",
1669 P_("Whether the item should start a new row"),
1671 GTK_PARAM_READWRITE));
1673 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1674 g_param_spec_int ("position",
1676 P_("Position of the item within this group"),
1680 GTK_PARAM_READWRITE));
1682 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1686 * gtk_tool_item_group_new:
1687 * @label: the label of the new group
1689 * Creates a new tool item group with label @label.
1691 * Returns: a new #GtkToolItemGroup.
1696 gtk_tool_item_group_new (const gchar *label)
1698 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1702 * gtk_tool_item_group_set_label:
1703 * @group: a #GtkToolItemGroup
1704 * @label: the new human-readable label of of the group
1706 * Sets the label of the tool item group. The label is displayed in the header
1712 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1715 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1718 gtk_tool_item_group_set_label_widget (group, NULL);
1721 GtkWidget *child = gtk_label_new (label);
1722 gtk_widget_show (child);
1724 gtk_tool_item_group_set_label_widget (group, child);
1727 g_object_notify (G_OBJECT (group), "label");
1731 * gtk_tool_item_group_set_label_widget:
1732 * @group: a #GtkToolItemGroup
1733 * @label_widget: the widget to be displayed in place of the usual label
1735 * Sets the label of the tool item group.
1736 * The label widget is displayed in the header of the group, in place
1737 * of the usual label.
1742 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1743 GtkWidget *label_widget)
1745 GtkToolItemGroupPrivate* priv;
1746 GtkWidget *alignment;
1748 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1749 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1750 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1754 if (priv->label_widget == label_widget)
1757 alignment = gtk_tool_item_group_get_alignment (group);
1759 if (priv->label_widget)
1761 gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1762 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1767 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1769 priv->label_widget = label_widget;
1771 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1772 gtk_widget_queue_resize (GTK_WIDGET (group));
1774 /* Only show the header widget if the group has children: */
1775 if (label_widget && priv->children)
1776 gtk_widget_show (priv->header);
1778 gtk_widget_hide (priv->header);
1780 g_object_freeze_notify (G_OBJECT (group));
1781 g_object_notify (G_OBJECT (group), "label-widget");
1782 g_object_notify (G_OBJECT (group), "label");
1783 g_object_thaw_notify (G_OBJECT (group));
1787 * gtk_tool_item_group_set_header_relief:
1788 * @group: a #GtkToolItemGroup
1789 * @style: the #GtkReliefStyle
1791 * Set the button relief of the group header.
1792 * See gtk_button_set_relief() for details.
1797 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1798 GtkReliefStyle style)
1800 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1802 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1806 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1808 return (g_source_get_time (group->priv->animation_timeout) -
1809 group->priv->animation_start) / 1000;
1813 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1815 GtkToolItemGroupPrivate* priv = group->priv;
1816 GtkWidget *widget = GTK_WIDGET (group);
1818 if (gtk_widget_get_realized (priv->header))
1820 GtkAllocation alignment_allocation;
1821 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1824 /* Find the header button's arrow area... */
1825 gtk_widget_get_allocation (alignment, &alignment_allocation);
1826 area.x = alignment_allocation.x;
1827 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1828 area.height = priv->expander_size;
1829 area.width = priv->expander_size;
1831 /* ... and invalidated it to get it animated. */
1832 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1835 if (gtk_widget_get_realized (widget))
1837 GtkAllocation allocation;
1838 GtkWidget *parent = gtk_widget_get_parent (widget);
1839 int x, y, width, height;
1841 /* Find the tool item area button's arrow area... */
1842 gtk_widget_get_allocation (widget, &allocation);
1843 width = allocation.width;
1844 height = allocation.height;
1846 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1848 if (gtk_widget_get_visible (priv->header))
1850 GtkAllocation header_allocation;
1852 gtk_widget_get_allocation (priv->header, &header_allocation);
1853 height -= header_allocation.height;
1854 y += header_allocation.height;
1857 /* ... and invalidated it to get it animated. */
1858 gtk_widget_queue_draw_area (parent, x, y, width, height);
1863 gtk_tool_item_group_animation_cb (gpointer data)
1865 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1866 GtkToolItemGroupPrivate* priv = group->priv;
1867 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1870 GDK_THREADS_ENTER ();
1872 /* Enque this early to reduce number of expose events. */
1873 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1875 gtk_tool_item_group_force_expose (group);
1877 /* Finish animation when done. */
1878 if (timestamp >= ANIMATION_DURATION)
1879 priv->animation_timeout = NULL;
1881 retval = (priv->animation_timeout != NULL);
1883 GDK_THREADS_LEAVE ();
1889 * gtk_tool_item_group_set_collapsed:
1890 * @group: a #GtkToolItemGroup
1891 * @collapsed: whether the @group should be collapsed or expanded
1893 * Sets whether the @group should be collapsed or expanded.
1898 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1902 GtkToolItemGroupPrivate* priv;
1904 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1908 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1909 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1910 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1911 GTK_WIDGET (group));
1912 if (collapsed != priv->collapsed)
1914 GtkStyleContext *context;
1916 if (priv->animation)
1918 if (priv->animation_timeout)
1919 g_source_destroy (priv->animation_timeout);
1921 priv->animation_start = g_get_monotonic_time ();
1922 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1924 g_source_set_callback (priv->animation_timeout,
1925 gtk_tool_item_group_animation_cb,
1927 g_source_attach (priv->animation_timeout, NULL);
1929 context = gtk_widget_get_style_context (gtk_bin_get_child (GTK_BIN (priv->header)));
1931 gtk_style_context_save (context);
1932 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
1934 gtk_style_context_notify_state_change (context,
1935 gtk_widget_get_window (priv->header),
1936 GUINT_TO_POINTER (1),
1937 GTK_STATE_FLAG_ACTIVE,
1940 gtk_style_context_restore (context);
1943 gtk_tool_item_group_force_expose (group);
1945 priv->collapsed = collapsed;
1946 g_object_notify (G_OBJECT (group), "collapsed");
1951 * gtk_tool_item_group_set_ellipsize:
1952 * @group: a #GtkToolItemGroup
1953 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1955 * Sets the ellipsization mode which should be used by labels in @group.
1960 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1961 PangoEllipsizeMode ellipsize)
1963 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1965 if (ellipsize != group->priv->ellipsize)
1967 group->priv->ellipsize = ellipsize;
1968 gtk_tool_item_group_header_adjust_style (group);
1969 g_object_notify (G_OBJECT (group), "ellipsize");
1970 _gtk_tool_item_group_palette_reconfigured (group);
1975 * gtk_tool_item_group_get_label:
1976 * @group: a #GtkToolItemGroup
1978 * Gets the label of @group.
1980 * Returns: the label of @group. The label is an internal string of @group
1981 * and must not be modified. Note that %NULL is returned if a custom
1982 * label has been set with gtk_tool_item_group_set_label_widget()
1986 G_CONST_RETURN gchar*
1987 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1989 GtkToolItemGroupPrivate *priv;
1991 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1995 if (GTK_IS_LABEL (priv->label_widget))
1996 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2002 * gtk_tool_item_group_get_label_widget:
2003 * @group: a #GtkToolItemGroup
2005 * Gets the label widget of @group.
2006 * See gtk_tool_item_group_set_label_widget().
2008 * Returns: (transfer none): the label widget of @group
2013 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2015 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2017 return gtk_bin_get_child (GTK_BIN (alignment));
2021 * gtk_tool_item_group_get_collapsed:
2022 * @group: a GtkToolItemGroup
2024 * Gets whether @group is collapsed or expanded.
2026 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2031 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2033 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2035 return group->priv->collapsed;
2039 * gtk_tool_item_group_get_ellipsize:
2040 * @group: a #GtkToolItemGroup
2042 * Gets the ellipsization mode of @group.
2044 * Returns: the #PangoEllipsizeMode of @group
2049 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2051 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2053 return group->priv->ellipsize;
2057 * gtk_tool_item_group_get_header_relief:
2058 * @group: a #GtkToolItemGroup
2060 * Gets the relief mode of the header button of @group.
2062 * Returns: the #GtkReliefStyle
2067 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2069 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2071 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2075 * gtk_tool_item_group_insert:
2076 * @group: a #GtkToolItemGroup
2077 * @item: the #GtkToolItem to insert into @group
2078 * @position: the position of @item in @group, starting with 0.
2079 * The position -1 means end of list.
2081 * Inserts @item at @position in the list of children of @group.
2086 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2090 GtkWidget *parent, *child_widget;
2091 GtkToolItemGroupChild *child;
2093 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2094 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2095 g_return_if_fail (position >= -1);
2097 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2099 child = g_new (GtkToolItemGroupChild, 1);
2100 child->item = g_object_ref_sink (item);
2101 child->homogeneous = TRUE;
2102 child->expand = FALSE;
2104 child->new_row = FALSE;
2106 group->priv->children = g_list_insert (group->priv->children, child, position);
2108 if (GTK_IS_TOOL_PALETTE (parent))
2109 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2111 child_widget = gtk_bin_get_child (GTK_BIN (item));
2113 if (GTK_IS_BUTTON (child_widget))
2114 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2116 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2120 * gtk_tool_item_group_set_item_position:
2121 * @group: a #GtkToolItemGroup
2122 * @item: the #GtkToolItem to move to a new position, should
2123 * be a child of @group.
2124 * @position: the new position of @item in @group, starting with 0.
2125 * The position -1 means end of list.
2127 * Sets the position of @item in the list of children of @group.
2132 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2138 GtkToolItemGroupChild *child;
2139 GtkToolItemGroupPrivate* priv;
2141 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2142 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2143 g_return_if_fail (position >= -1);
2145 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2148 g_return_if_fail (child != NULL);
2150 if (position == old_position)
2153 priv->children = g_list_delete_link (priv->children, link);
2154 priv->children = g_list_insert (priv->children, child, position);
2156 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2157 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2158 gtk_widget_get_visible (GTK_WIDGET (item)))
2159 gtk_widget_queue_resize (GTK_WIDGET (group));
2163 * gtk_tool_item_group_get_item_position:
2164 * @group: a #GtkToolItemGroup
2165 * @item: a #GtkToolItem
2167 * Gets the position of @item in @group as index.
2169 * Returns: the index of @item in @group or -1 if @item is no child of @group
2174 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2179 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2180 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2182 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2189 * gtk_tool_item_group_get_n_items:
2190 * @group: a #GtkToolItemGroup
2192 * Gets the number of tool items in @group.
2194 * Returns: the number of tool items in @group
2199 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2201 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2203 return g_list_length (group->priv->children);
2207 * gtk_tool_item_group_get_nth_item:
2208 * @group: a #GtkToolItemGroup
2211 * Gets the tool item at @index in group.
2213 * Returns: (transfer none): the #GtkToolItem at index
2218 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2221 GtkToolItemGroupChild *child;
2223 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2225 child = g_list_nth_data (group->priv->children, index);
2227 return child != NULL ? child->item : NULL;
2231 * gtk_tool_item_group_get_drop_item:
2232 * @group: a #GtkToolItemGroup
2233 * @x: the x position
2234 * @y: the y position
2236 * Gets the tool item at position (x, y).
2238 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2243 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2247 GtkAllocation allocation;
2248 GtkOrientation orientation;
2251 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2253 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2254 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2256 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2257 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2259 for (it = group->priv->children; it != NULL; it = it->next)
2261 GtkToolItemGroupChild *child = it->data;
2262 GtkToolItem *item = child->item;
2265 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2268 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2270 x0 = x - allocation.x;
2271 y0 = y - allocation.y;
2273 if (x0 >= 0 && x0 < allocation.width &&
2274 y0 >= 0 && y0 < allocation.height)
2282 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2283 GtkRequisition *item_size,
2284 gboolean homogeneous_only,
2285 gint *requested_rows)
2287 GtkRequisition child_requisition;
2290 gboolean new_row = TRUE;
2291 GtkOrientation orientation;
2292 GtkToolbarStyle style;
2294 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2295 g_return_if_fail (NULL != item_size);
2297 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2298 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2300 item_size->width = item_size->height = 0;
2302 for (it = group->priv->children; it != NULL; it = it->next)
2304 GtkToolItemGroupChild *child = it->data;
2306 if (!gtk_tool_item_group_is_item_visible (group, child))
2309 if (child->new_row || new_row)
2315 if (!child->homogeneous && child->expand)
2318 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2319 &child_requisition, NULL);
2321 if (!homogeneous_only || child->homogeneous)
2322 item_size->width = MAX (item_size->width, child_requisition.width);
2323 item_size->height = MAX (item_size->height, child_requisition.height);
2327 *requested_rows = rows;
2331 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2334 GtkAllocation allocation;
2335 GtkWidget *widget = GTK_WIDGET (group);
2336 GtkToolItemGroupPrivate* priv = group->priv;
2338 gtk_widget_get_allocation (widget, &allocation);
2340 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2344 if (priv->animation_timeout)
2346 GtkAllocation header_allocation;
2347 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2348 cairo_pattern_t *mask;
2351 if (GTK_ORIENTATION_VERTICAL == orientation)
2352 v1 = allocation.height;
2354 v1 = allocation.width;
2358 gtk_widget_get_allocation (priv->header, &header_allocation);
2359 if (!gtk_widget_get_visible (priv->header))
2361 else if (GTK_ORIENTATION_VERTICAL == orientation)
2362 v0 = MAX (v0, header_allocation.height);
2364 v0 = MAX (v0, header_allocation.width);
2366 v1 = MIN (v0 + 256, v1);
2368 if (GTK_ORIENTATION_VERTICAL == orientation)
2373 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2380 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2383 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2384 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2385 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2386 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2387 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2389 cairo_mask (cr, mask);
2390 cairo_pattern_destroy (mask);
2397 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2402 GtkRequisition requisition;
2403 GtkToolItemGroupPrivate* priv = group->priv;
2405 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2406 &requisition, NULL);
2408 if (!priv->collapsed || priv->animation_timeout)
2410 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2411 GtkRequisition inquery;
2414 allocation.width = limit;
2416 allocation.height = limit;
2418 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2419 &allocation, &inquery);
2422 inquery.height -= requisition.height;
2424 inquery.width -= requisition.width;
2426 if (priv->animation_timeout && animation)
2428 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2430 timestamp = MIN (timestamp, ANIMATION_DURATION);
2432 if (priv->collapsed)
2433 timestamp = ANIMATION_DURATION - timestamp;
2437 inquery.height *= timestamp;
2438 inquery.height /= ANIMATION_DURATION;
2442 inquery.width *= timestamp;
2443 inquery.width /= ANIMATION_DURATION;
2448 requisition.height += inquery.height;
2450 requisition.width += inquery.width;
2453 return (vertical ? requisition.height : requisition.width);
2457 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2460 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2464 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2467 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2471 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2474 if (GTK_IS_TOOL_ITEM (child))
2475 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2480 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2482 gtk_container_foreach (GTK_CONTAINER (group),
2483 gtk_tool_palette_reconfigured_foreach_item,
2486 gtk_tool_item_group_header_adjust_style (group);