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, see <http://www.gnu.org/licenses/>.
27 #include "gtktoolpaletteprivate.h"
28 #include "gtktypebuiltins.h"
29 #include "gtkprivate.h"
32 #define ANIMATION_TIMEOUT 50
33 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
34 #define DEFAULT_ANIMATION_STATE TRUE
35 #define DEFAULT_EXPANDER_SIZE 16
36 #define DEFAULT_HEADER_SPACING 2
38 #define DEFAULT_LABEL ""
39 #define DEFAULT_COLLAPSED FALSE
40 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
43 * SECTION:gtktoolitemgroup
44 * @Short_description: A sub container used in a tool palette
45 * @Title: GtkToolItemGroup
47 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
48 * #GtkToolItem<!-- -->s to a palette like container with different
49 * categories and drag and drop support.
67 CHILD_PROP_HOMOGENEOUS,
74 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
76 struct _GtkToolItemGroupPrivate
79 GtkWidget *label_widget;
83 gint64 animation_start;
84 GSource *animation_timeout;
91 GtkSettings *settings;
92 gulong settings_connection;
94 PangoEllipsizeMode ellipsize;
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 priv->settings_connection = 0;
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);
280 state = gtk_widget_get_state_flags (widget);
282 if (!priv->collapsed)
283 state |= GTK_STATE_FLAG_ACTIVE;
285 gtk_style_context_save (context);
286 gtk_style_context_set_state (context, state);
287 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
289 if (GTK_ORIENTATION_VERTICAL == orientation)
291 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
293 if (GTK_TEXT_DIR_RTL == direction)
298 y = height / 2 - priv->expander_size / 2;
302 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
303 x = width / 2 - priv->expander_size / 2;
307 gtk_render_expander (context, cr, x, y,
309 priv->expander_size);
311 gtk_style_context_restore (context);
317 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
320 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
321 GtkToolItemGroupPrivate* priv = group->priv;
322 GtkWidget *parent = gtk_widget_get_parent (data);
324 if (priv->collapsed ||
325 !GTK_IS_TOOL_PALETTE (parent) ||
326 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
327 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
331 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
333 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
334 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
335 GtkWidget *widget = GTK_WIDGET (group);
336 GtkToolItemGroupPrivate* priv = group->priv;
338 GtkTextDirection direction = gtk_widget_get_direction (widget);
340 gtk_widget_style_get (widget,
341 "header-spacing", &(priv->header_spacing),
342 "expander-size", &(priv->expander_size),
345 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
347 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
349 case GTK_ORIENTATION_HORIZONTAL:
350 dy = priv->header_spacing + priv->expander_size;
352 if (GTK_IS_LABEL (label_widget))
354 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
355 if (GTK_TEXT_DIR_RTL == direction)
356 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
358 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
362 case GTK_ORIENTATION_VERTICAL:
363 dx = priv->header_spacing + priv->expander_size;
365 if (GTK_IS_LABEL (label_widget))
367 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
368 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
373 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
377 gtk_tool_item_group_init (GtkToolItemGroup *group)
379 GtkWidget *alignment;
380 GtkToolItemGroupPrivate* priv;
382 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
384 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
385 GTK_TYPE_TOOL_ITEM_GROUP,
386 GtkToolItemGroupPrivate);
388 priv->children = NULL;
389 priv->header_spacing = DEFAULT_HEADER_SPACING;
390 priv->expander_size = DEFAULT_EXPANDER_SIZE;
392 priv->label_widget = gtk_label_new (NULL);
393 gtk_widget_set_halign (priv->label_widget, GTK_ALIGN_START);
394 gtk_widget_set_valign (priv->label_widget, GTK_ALIGN_CENTER);
395 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
396 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
397 gtk_widget_show_all (alignment);
399 gtk_widget_push_composite_child ();
400 priv->header = gtk_button_new ();
401 gtk_widget_set_composite_name (priv->header, "header");
402 gtk_widget_pop_composite_child ();
404 g_object_ref_sink (priv->header);
405 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
406 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
407 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
409 gtk_tool_item_group_header_adjust_style (group);
411 g_signal_connect_after (alignment, "draw",
412 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
415 g_signal_connect (priv->header, "clicked",
416 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
421 gtk_tool_item_group_set_property (GObject *object,
426 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
431 gtk_tool_item_group_set_label (group, g_value_get_string (value));
434 case PROP_LABEL_WIDGET:
435 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
439 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
443 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
447 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
451 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
457 gtk_tool_item_group_get_property (GObject *object,
462 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
467 g_value_set_string (value, gtk_tool_item_group_get_label (group));
470 case PROP_LABEL_WIDGET:
471 g_value_set_object (value,
472 gtk_tool_item_group_get_label_widget (group));
476 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
480 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
484 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
488 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
494 gtk_tool_item_group_finalize (GObject *object)
496 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
498 if (group->priv->children)
500 g_list_free (group->priv->children);
501 group->priv->children = NULL;
504 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
508 gtk_tool_item_group_dispose (GObject *object)
510 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
511 GtkToolItemGroupPrivate* priv = group->priv;
515 /* disconnect focus tracking handler */
516 g_signal_handler_disconnect (priv->toplevel,
519 priv->focus_set_id = 0;
520 priv->toplevel = NULL;
523 if (priv->settings_connection > 0)
525 g_signal_handler_disconnect (priv->settings, priv->settings_connection);
526 priv->settings_connection = 0;
529 g_clear_object (&priv->settings);
531 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
535 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
536 GtkRequisition *item_size,
537 gboolean homogeneous_only,
538 gint *requested_rows)
540 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
542 if (GTK_IS_TOOL_PALETTE (parent))
543 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
545 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
549 gtk_tool_item_group_size_request (GtkWidget *widget,
550 GtkRequisition *requisition)
552 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
553 GtkToolItemGroupPrivate* priv = group->priv;
554 GtkOrientation orientation;
555 GtkRequisition item_size;
559 if (priv->children && gtk_tool_item_group_get_label_widget (group))
561 gtk_widget_get_preferred_size (priv->header,
563 gtk_widget_show (priv->header);
567 requisition->width = requisition->height = 0;
568 gtk_widget_hide (priv->header);
571 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
573 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
575 if (GTK_ORIENTATION_VERTICAL == orientation)
576 requisition->width = MAX (requisition->width, item_size.width);
578 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
580 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
581 requisition->width += border_width * 2;
582 requisition->height += border_width * 2;
586 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
590 GtkRequisition requisition;
592 gtk_tool_item_group_size_request (widget, &requisition);
594 *minimum = *natural = requisition.width;
598 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
602 GtkRequisition requisition;
604 gtk_tool_item_group_size_request (widget, &requisition);
606 *minimum = *natural = requisition.height;
611 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
612 GtkToolItemGroupChild *child)
614 GtkToolbarStyle style;
615 GtkOrientation orientation;
617 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
618 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
620 /* horizontal tool palettes with text style support only homogeneous items */
621 if (!child->homogeneous &&
622 GTK_ORIENTATION_HORIZONTAL == orientation &&
623 GTK_TOOLBAR_TEXT == style)
627 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
628 (GTK_ORIENTATION_VERTICAL == orientation ?
629 gtk_tool_item_get_visible_vertical (child->item) :
630 gtk_tool_item_get_visible_horizontal (child->item));
633 static inline unsigned
637 return (x + y - 1) / y;
641 gtk_tool_item_group_real_size_query (GtkWidget *widget,
642 GtkAllocation *allocation,
643 GtkRequisition *inquery)
645 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
646 GtkToolItemGroupPrivate* priv = group->priv;
648 GtkRequisition item_size;
649 GtkAllocation item_area;
651 GtkOrientation orientation;
656 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
657 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
659 /* figure out the size of homogeneous items */
660 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
662 if (GTK_ORIENTATION_VERTICAL == orientation)
663 item_size.width = MIN (item_size.width, allocation->width);
665 item_size.height = MIN (item_size.height, allocation->height);
667 item_size.width = MAX (item_size.width, 1);
668 item_size.height = MAX (item_size.height, 1);
671 item_area.height = 0;
673 /* figure out the required columns (n_columns) and rows (n_rows)
676 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
682 if (GTK_ORIENTATION_VERTICAL == orientation)
684 gboolean new_row = FALSE;
688 item_area.width = allocation->width - 2 * border_width;
689 n_columns = MAX (item_area.width / item_size.width, 1);
691 /* calculate required rows for n_columns columns */
692 for (it = priv->children; it != NULL; it = it->next)
694 GtkToolItemGroupChild *child = it->data;
696 if (!gtk_tool_item_group_is_item_visible (group, child))
699 if (new_row || child->new_row)
709 if (child->homogeneous)
712 if (col >= n_columns)
717 GtkRequisition req = {0, 0};
720 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
723 width = udiv (req.width, item_size.width);
731 if (col >= n_columns)
739 guint *row_min_width;
741 gboolean new_row = TRUE;
742 guint col = 0, min_col, max_col = 0, all_items = 0;
745 item_area.height = allocation->height - 2 * border_width;
746 n_rows = MAX (item_area.height / item_size.height, min_rows);
748 row_min_width = g_new0 (guint, n_rows);
750 /* calculate minimal and maximal required cols and minimal
753 for (it = priv->children; it != NULL; it = it->next)
755 GtkToolItemGroupChild *child = it->data;
757 if (!gtk_tool_item_group_is_item_visible (group, child))
760 if (new_row || child->new_row)
765 row_min_width[row] = 1;
771 if (child->homogeneous)
778 GtkRequisition req = {0, 0};
781 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
784 width = udiv (req.width, item_size.width);
789 row_min_width[row] = MAX (row_min_width[row], width);
792 max_col = MAX (max_col, col);
795 /* calculate minimal required cols */
796 min_col = udiv (all_items, n_rows);
798 for (i = 0; i <= row; i++)
800 min_col = MAX (min_col, row_min_width[i]);
803 /* simple linear search for minimal required columns
804 * for the given maximal number of rows (n_rows)
806 for (n_columns = min_col; n_columns < max_col; n_columns ++)
810 /* calculate required rows for n_columns columns */
811 for (it = priv->children; it != NULL; it = it->next)
813 GtkToolItemGroupChild *child = it->data;
815 if (!gtk_tool_item_group_is_item_visible (group, child))
818 if (new_row || child->new_row)
828 if (child->homogeneous)
831 if (col >= n_columns)
836 GtkRequisition req = {0, 0};
839 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
842 width = udiv (req.width, item_size.width);
850 if (col >= n_columns)
860 item_area.width = item_size.width * n_columns;
861 item_area.height = item_size.height * n_rows;
867 /* figure out header widget size */
868 if (gtk_widget_get_visible (priv->header))
870 GtkRequisition child_requisition;
872 gtk_widget_get_preferred_size (priv->header,
873 &child_requisition, NULL);
875 if (GTK_ORIENTATION_VERTICAL == orientation)
876 inquery->height += child_requisition.height;
878 inquery->width += child_requisition.width;
881 /* report effective widget size */
882 inquery->width += item_area.width + 2 * border_width;
883 inquery->height += item_area.height + 2 * border_width;
887 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
888 GtkAllocation *allocation)
890 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
891 GtkToolItemGroupPrivate* priv = group->priv;
892 GtkRequisition child_requisition;
893 GtkAllocation child_allocation;
895 GtkRequisition item_size;
896 GtkAllocation item_area;
898 GtkOrientation orientation;
902 gint n_columns, n_rows = 1;
905 GtkTextDirection direction;
907 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
909 direction = gtk_widget_get_direction (widget);
911 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
914 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
916 child_allocation.x = border_width;
917 child_allocation.y = border_width;
919 /* place the header widget */
920 if (gtk_widget_get_visible (priv->header))
922 gtk_widget_get_preferred_size (priv->header,
923 &child_requisition, NULL);
925 if (GTK_ORIENTATION_VERTICAL == orientation)
927 child_allocation.width = allocation->width;
928 child_allocation.height = child_requisition.height;
932 child_allocation.width = child_requisition.width;
933 child_allocation.height = allocation->height;
935 if (GTK_TEXT_DIR_RTL == direction)
936 child_allocation.x = allocation->width - border_width - child_allocation.width;
939 gtk_widget_size_allocate (priv->header, &child_allocation);
941 if (GTK_ORIENTATION_VERTICAL == orientation)
942 child_allocation.y += child_allocation.height;
943 else if (GTK_TEXT_DIR_RTL != direction)
944 child_allocation.x += child_allocation.width;
946 child_allocation.x = border_width;
949 child_requisition.width = child_requisition.height = 0;
951 /* figure out the size of homogeneous items */
952 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
954 item_size.width = MAX (item_size.width, 1);
955 item_size.height = MAX (item_size.height, 1);
957 /* figure out the available columns and size of item_area */
958 if (GTK_ORIENTATION_VERTICAL == orientation)
960 item_size.width = MIN (item_size.width, allocation->width);
962 item_area.width = allocation->width - 2 * border_width;
963 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
965 n_columns = MAX (item_area.width / item_size.width, 1);
967 item_size.width = item_area.width / n_columns;
971 item_size.height = MIN (item_size.height, allocation->height);
973 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
974 item_area.height = allocation->height - 2 * border_width;
976 n_columns = MAX (item_area.width / item_size.width, 1);
977 n_rows = MAX (item_area.height / item_size.height, min_rows);
979 item_size.height = item_area.height / n_rows;
982 item_area.x = child_allocation.x;
983 item_area.y = child_allocation.y;
985 /* when expanded or in transition, place the tool items in a grid like layout */
986 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
988 gint col = 0, row = 0;
990 for (it = priv->children; it != NULL; it = it->next)
992 GtkToolItemGroupChild *child = it->data;
995 if (!gtk_tool_item_group_is_item_visible (group, child))
997 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1002 /* for non homogeneous widgets request the required size */
1003 child_requisition.width = 0;
1005 if (!child->homogeneous)
1007 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
1008 &child_requisition, NULL);
1009 child_requisition.width = MIN (child_requisition.width, item_area.width);
1012 /* select next row if at end of row */
1013 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1017 child_allocation.y += child_allocation.height;
1022 /* calculate the position and size of the item */
1023 if (!child->homogeneous)
1029 col_width = udiv (child_requisition.width, item_size.width);
1031 col_width = n_columns - col;
1033 width = col_width * item_size.width;
1035 if (GTK_TEXT_DIR_RTL == direction)
1036 col_child = (n_columns - col - col_width);
1040 child_allocation.x = item_area.x + col_child * item_size.width;
1041 child_allocation.width = width;
1045 child_allocation.x =
1046 (item_area.x + col_child * item_size.width +
1047 (width - child_requisition.width) / 2);
1048 child_allocation.width = child_requisition.width;
1055 if (GTK_TEXT_DIR_RTL == direction)
1056 col_child = (n_columns - col - 1);
1058 child_allocation.x = item_area.x + col_child * item_size.width;
1059 child_allocation.width = item_size.width;
1064 child_allocation.height = item_size.height;
1066 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1067 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1070 child_allocation.y += item_size.height;
1073 /* or just hide all items, when collapsed */
1077 for (it = priv->children; it != NULL; it = it->next)
1079 GtkToolItemGroupChild *child = it->data;
1081 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1087 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1088 GtkAllocation *allocation)
1090 gtk_tool_item_group_real_size_allocate (widget, allocation);
1092 if (gtk_widget_get_mapped (widget))
1093 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1097 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1101 GtkAdjustment *adjustment;
1102 GtkAllocation allocation, p_allocation;
1105 /* Find this group's parent widget in the focused widget's anchestry. */
1106 for (p = widget; p; p = gtk_widget_get_parent (p))
1109 p = gtk_widget_get_parent (p);
1113 if (GTK_IS_TOOL_PALETTE (p))
1115 /* Check that the focused widgets is fully visible within
1116 * the group's parent widget and make it visible otherwise. */
1118 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1124 gtk_widget_get_allocation (widget, &allocation);
1125 gtk_widget_get_allocation (p, &p_allocation);
1127 /* Handle vertical adjustment. */
1128 if (gtk_widget_translate_coordinates
1129 (widget, p, 0, 0, NULL, &y) && y < 0)
1131 y += gtk_adjustment_get_value (adjustment);
1132 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1134 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1135 y > p_allocation.height)
1137 y += gtk_adjustment_get_value (adjustment);
1138 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1142 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1148 gtk_widget_get_allocation (widget, &allocation);
1149 gtk_widget_get_allocation (p, &p_allocation);
1151 /* Handle horizontal adjustment. */
1152 if (gtk_widget_translate_coordinates
1153 (widget, p, 0, 0, &x, NULL) && x < 0)
1155 x += gtk_adjustment_get_value (adjustment);
1156 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1158 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1159 x > p_allocation.width)
1161 x += gtk_adjustment_get_value (adjustment);
1162 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1171 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1172 GtkWidget *toplevel)
1174 GtkToolItemGroupPrivate* priv = group->priv;
1176 if (toplevel != priv->toplevel)
1180 /* Disconnect focus tracking handler. */
1181 g_signal_handler_disconnect (priv->toplevel,
1182 priv->focus_set_id);
1184 priv->focus_set_id = 0;
1185 priv->toplevel = NULL;
1190 /* Install focus tracking handler. We connect to the window's
1191 * set-focus signal instead of connecting to the focus signal of
1194 * 1) Reduce the number of signal handlers used.
1195 * 2) Avoid special handling for group headers.
1196 * 3) Catch focus grabs not only for direct children,
1197 * but also for nested widgets.
1199 priv->focus_set_id =
1200 g_signal_connect (toplevel, "set-focus",
1201 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1204 priv->toplevel = toplevel;
1210 gtk_tool_item_group_realize (GtkWidget *widget)
1212 GtkAllocation allocation;
1213 GtkWidget *toplevel_window;
1215 GdkWindowAttr attributes;
1216 gint attributes_mask;
1218 GtkStyleContext *context;
1220 gtk_widget_set_realized (widget, TRUE);
1222 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1223 context = gtk_widget_get_style_context (widget);
1225 gtk_widget_get_allocation (widget, &allocation);
1227 attributes.window_type = GDK_WINDOW_CHILD;
1228 attributes.x = allocation.x + border_width;
1229 attributes.y = allocation.y + border_width;
1230 attributes.width = allocation.width - border_width * 2;
1231 attributes.height = allocation.height - border_width * 2;
1232 attributes.wclass = GDK_INPUT_OUTPUT;
1233 attributes.visual = gtk_widget_get_visual (widget);
1234 attributes.event_mask = gtk_widget_get_events (widget)
1235 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1236 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1237 | GDK_BUTTON_MOTION_MASK;
1238 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1240 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1241 &attributes, attributes_mask);
1242 gtk_widget_set_window (widget, window);
1244 gtk_widget_register_window (widget, window);
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 if (priv->animation)
1919 if (priv->animation_timeout)
1920 g_source_destroy (priv->animation_timeout);
1922 priv->animation_start = g_get_monotonic_time ();
1923 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1925 g_source_set_callback (priv->animation_timeout,
1926 gtk_tool_item_group_animation_cb,
1928 g_source_attach (priv->animation_timeout, NULL);
1931 gtk_tool_item_group_force_expose (group);
1933 priv->collapsed = collapsed;
1934 g_object_notify (G_OBJECT (group), "collapsed");
1939 * gtk_tool_item_group_set_ellipsize:
1940 * @group: a #GtkToolItemGroup
1941 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1943 * Sets the ellipsization mode which should be used by labels in @group.
1948 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1949 PangoEllipsizeMode ellipsize)
1951 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1953 if (ellipsize != group->priv->ellipsize)
1955 group->priv->ellipsize = ellipsize;
1956 gtk_tool_item_group_header_adjust_style (group);
1957 g_object_notify (G_OBJECT (group), "ellipsize");
1958 _gtk_tool_item_group_palette_reconfigured (group);
1963 * gtk_tool_item_group_get_label:
1964 * @group: a #GtkToolItemGroup
1966 * Gets the label of @group.
1968 * Returns: the label of @group. The label is an internal string of @group
1969 * and must not be modified. Note that %NULL is returned if a custom
1970 * label has been set with gtk_tool_item_group_set_label_widget()
1975 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1977 GtkToolItemGroupPrivate *priv;
1979 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1983 if (GTK_IS_LABEL (priv->label_widget))
1984 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1990 * gtk_tool_item_group_get_label_widget:
1991 * @group: a #GtkToolItemGroup
1993 * Gets the label widget of @group.
1994 * See gtk_tool_item_group_set_label_widget().
1996 * Returns: (transfer none): the label widget of @group
2001 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2003 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2005 return gtk_bin_get_child (GTK_BIN (alignment));
2009 * gtk_tool_item_group_get_collapsed:
2010 * @group: a GtkToolItemGroup
2012 * Gets whether @group is collapsed or expanded.
2014 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2019 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2021 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2023 return group->priv->collapsed;
2027 * gtk_tool_item_group_get_ellipsize:
2028 * @group: a #GtkToolItemGroup
2030 * Gets the ellipsization mode of @group.
2032 * Returns: the #PangoEllipsizeMode of @group
2037 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2039 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2041 return group->priv->ellipsize;
2045 * gtk_tool_item_group_get_header_relief:
2046 * @group: a #GtkToolItemGroup
2048 * Gets the relief mode of the header button of @group.
2050 * Returns: the #GtkReliefStyle
2055 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2057 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2059 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2063 * gtk_tool_item_group_insert:
2064 * @group: a #GtkToolItemGroup
2065 * @item: the #GtkToolItem to insert into @group
2066 * @position: the position of @item in @group, starting with 0.
2067 * The position -1 means end of list.
2069 * Inserts @item at @position in the list of children of @group.
2074 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2078 GtkWidget *parent, *child_widget;
2079 GtkToolItemGroupChild *child;
2081 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2082 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2083 g_return_if_fail (position >= -1);
2085 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2087 child = g_new (GtkToolItemGroupChild, 1);
2088 child->item = g_object_ref_sink (item);
2089 child->homogeneous = TRUE;
2090 child->expand = FALSE;
2092 child->new_row = FALSE;
2094 group->priv->children = g_list_insert (group->priv->children, child, position);
2096 if (GTK_IS_TOOL_PALETTE (parent))
2097 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2099 child_widget = gtk_bin_get_child (GTK_BIN (item));
2101 if (GTK_IS_BUTTON (child_widget))
2102 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2104 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2108 * gtk_tool_item_group_set_item_position:
2109 * @group: a #GtkToolItemGroup
2110 * @item: the #GtkToolItem to move to a new position, should
2111 * be a child of @group.
2112 * @position: the new position of @item in @group, starting with 0.
2113 * The position -1 means end of list.
2115 * Sets the position of @item in the list of children of @group.
2120 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2126 GtkToolItemGroupChild *child;
2127 GtkToolItemGroupPrivate* priv;
2129 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2130 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2131 g_return_if_fail (position >= -1);
2133 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2136 g_return_if_fail (child != NULL);
2138 if (position == old_position)
2141 priv->children = g_list_delete_link (priv->children, link);
2142 priv->children = g_list_insert (priv->children, child, position);
2144 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2145 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2146 gtk_widget_get_visible (GTK_WIDGET (item)))
2147 gtk_widget_queue_resize (GTK_WIDGET (group));
2151 * gtk_tool_item_group_get_item_position:
2152 * @group: a #GtkToolItemGroup
2153 * @item: a #GtkToolItem
2155 * Gets the position of @item in @group as index.
2157 * Returns: the index of @item in @group or -1 if @item is no child of @group
2162 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2167 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2168 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2170 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2177 * gtk_tool_item_group_get_n_items:
2178 * @group: a #GtkToolItemGroup
2180 * Gets the number of tool items in @group.
2182 * Returns: the number of tool items in @group
2187 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2189 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2191 return g_list_length (group->priv->children);
2195 * gtk_tool_item_group_get_nth_item:
2196 * @group: a #GtkToolItemGroup
2199 * Gets the tool item at @index in group.
2201 * Returns: (transfer none): the #GtkToolItem at index
2206 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2209 GtkToolItemGroupChild *child;
2211 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2213 child = g_list_nth_data (group->priv->children, index);
2215 return child != NULL ? child->item : NULL;
2219 * gtk_tool_item_group_get_drop_item:
2220 * @group: a #GtkToolItemGroup
2221 * @x: the x position
2222 * @y: the y position
2224 * Gets the tool item at position (x, y).
2226 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2231 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2235 GtkAllocation allocation;
2238 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2240 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2242 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2243 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2245 for (it = group->priv->children; it != NULL; it = it->next)
2247 GtkToolItemGroupChild *child = it->data;
2248 GtkToolItem *item = child->item;
2251 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2254 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2256 x0 = x - allocation.x;
2257 y0 = y - allocation.y;
2259 if (x0 >= 0 && x0 < allocation.width &&
2260 y0 >= 0 && y0 < allocation.height)
2268 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2269 GtkRequisition *item_size,
2270 gboolean homogeneous_only,
2271 gint *requested_rows)
2273 GtkRequisition child_requisition;
2276 gboolean new_row = TRUE;
2278 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2279 g_return_if_fail (NULL != item_size);
2281 item_size->width = item_size->height = 0;
2283 for (it = group->priv->children; it != NULL; it = it->next)
2285 GtkToolItemGroupChild *child = it->data;
2287 if (!gtk_tool_item_group_is_item_visible (group, child))
2290 if (child->new_row || new_row)
2296 if (!child->homogeneous && child->expand)
2299 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2300 &child_requisition, NULL);
2302 if (!homogeneous_only || child->homogeneous)
2303 item_size->width = MAX (item_size->width, child_requisition.width);
2304 item_size->height = MAX (item_size->height, child_requisition.height);
2308 *requested_rows = rows;
2312 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2315 GtkAllocation allocation;
2316 GtkWidget *widget = GTK_WIDGET (group);
2317 GtkToolItemGroupPrivate* priv = group->priv;
2319 gtk_widget_get_allocation (widget, &allocation);
2321 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2325 if (priv->animation_timeout)
2327 GtkAllocation header_allocation;
2328 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2329 cairo_pattern_t *mask;
2332 if (GTK_ORIENTATION_VERTICAL == orientation)
2333 v1 = allocation.height;
2335 v1 = allocation.width;
2339 gtk_widget_get_allocation (priv->header, &header_allocation);
2340 if (!gtk_widget_get_visible (priv->header))
2342 else if (GTK_ORIENTATION_VERTICAL == orientation)
2343 v0 = MAX (v0, header_allocation.height);
2345 v0 = MAX (v0, header_allocation.width);
2347 v1 = MIN (v0 + 256, v1);
2349 if (GTK_ORIENTATION_VERTICAL == orientation)
2354 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2361 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2364 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2365 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2366 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2367 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2368 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2370 cairo_mask (cr, mask);
2371 cairo_pattern_destroy (mask);
2378 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2383 GtkRequisition requisition;
2384 GtkToolItemGroupPrivate* priv = group->priv;
2386 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2387 &requisition, NULL);
2389 if (!priv->collapsed || priv->animation_timeout)
2391 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2392 GtkRequisition inquery;
2395 allocation.width = limit;
2397 allocation.height = limit;
2399 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2400 &allocation, &inquery);
2403 inquery.height -= requisition.height;
2405 inquery.width -= requisition.width;
2407 if (priv->animation_timeout && animation)
2409 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2411 timestamp = MIN (timestamp, ANIMATION_DURATION);
2413 if (priv->collapsed)
2414 timestamp = ANIMATION_DURATION - timestamp;
2418 inquery.height *= timestamp;
2419 inquery.height /= ANIMATION_DURATION;
2423 inquery.width *= timestamp;
2424 inquery.width /= ANIMATION_DURATION;
2429 requisition.height += inquery.height;
2431 requisition.width += inquery.width;
2434 return (vertical ? requisition.height : requisition.width);
2438 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2441 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2445 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2448 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2452 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2455 if (GTK_IS_TOOL_ITEM (child))
2456 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2461 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2463 gtk_container_foreach (GTK_CONTAINER (group),
2464 gtk_tool_palette_reconfigured_foreach_item,
2467 gtk_tool_item_group_header_adjust_style (group);