1 /* GtkToolPalette -- A tool palette with categories and DnD support
2 * Copyright (C) 2008 Openismus GmbH
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "gtktoolpaletteprivate.h"
29 #include "gtktypebuiltins.h"
30 #include "gtkprivate.h"
33 #define ANIMATION_TIMEOUT 50
34 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
35 #define DEFAULT_ANIMATION_STATE TRUE
36 #define DEFAULT_EXPANDER_SIZE 16
37 #define DEFAULT_HEADER_SPACING 2
39 #define DEFAULT_LABEL ""
40 #define DEFAULT_COLLAPSED FALSE
41 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
44 * SECTION:gtktoolitemgroup
45 * @Short_description: A sub container used in a tool palette
46 * @Title: GtkToolItemGroup
48 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
49 * #GtkToolItem<!-- -->s to a palette like container with different
50 * categories and drag and drop support.
68 CHILD_PROP_HOMOGENEOUS,
75 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
77 struct _GtkToolItemGroupPrivate
80 GtkWidget *label_widget;
84 gint64 animation_start;
85 GSource *animation_timeout;
92 GtkSettings *settings;
93 gulong settings_connection;
95 PangoEllipsizeMode ellipsize;
101 struct _GtkToolItemGroupChild
105 guint homogeneous : 1;
111 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
113 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
117 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
119 return gtk_bin_get_child (GTK_BIN (group->priv->header));
122 static GtkOrientation
123 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
125 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
127 if (GTK_IS_TOOL_PALETTE (parent))
128 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
130 return GTK_ORIENTATION_VERTICAL;
133 static GtkToolbarStyle
134 gtk_tool_item_group_get_style (GtkToolShell *shell)
136 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
138 if (GTK_IS_TOOL_PALETTE (parent))
139 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
141 return GTK_TOOLBAR_ICONS;
145 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
147 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
149 if (GTK_IS_TOOL_PALETTE (parent))
150 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
152 return GTK_ICON_SIZE_SMALL_TOOLBAR;
155 static PangoEllipsizeMode
156 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
158 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
162 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
164 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
165 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
171 static GtkOrientation
172 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
174 return GTK_ORIENTATION_HORIZONTAL;
177 static GtkSizeGroup *
178 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
180 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
182 if (GTK_IS_TOOL_PALETTE (parent))
183 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
189 animation_change_notify (GtkToolItemGroup *group)
191 GtkSettings *settings = group->priv->settings;
195 g_object_get (settings,
196 "gtk-enable-animations", &animation,
199 animation = DEFAULT_ANIMATION_STATE;
201 group->priv->animation = animation;
205 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
206 const GParamSpec *pspec,
207 GtkToolItemGroup *group)
209 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
210 animation_change_notify (group);
214 gtk_tool_item_group_screen_changed (GtkWidget *widget,
215 GdkScreen *previous_screen)
217 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
218 GtkToolItemGroupPrivate* priv = group->priv;
219 GtkSettings *old_settings = priv->settings;
220 GtkSettings *settings;
222 if (gtk_widget_has_screen (GTK_WIDGET (group)))
223 settings = gtk_widget_get_settings (GTK_WIDGET (group));
227 if (settings == old_settings)
232 g_signal_handler_disconnect (old_settings, priv->settings_connection);
233 g_object_unref (old_settings);
238 priv->settings_connection =
239 g_signal_connect (settings, "notify",
240 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
242 priv->settings = g_object_ref (settings);
245 priv->settings = NULL;
247 animation_change_notify (group);
251 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
253 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
254 iface->get_orientation = gtk_tool_item_group_get_orientation;
255 iface->get_style = gtk_tool_item_group_get_style;
256 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
257 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
258 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
259 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
263 gtk_tool_item_group_header_draw_cb (GtkWidget *widget,
267 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
268 GtkToolItemGroupPrivate* priv = group->priv;
269 GtkOrientation orientation;
270 gint x, y, width, height;
271 GtkTextDirection direction;
272 GtkStyleContext *context;
273 GtkStateFlags state = 0;
275 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
276 direction = gtk_widget_get_direction (widget);
277 width = gtk_widget_get_allocated_width (widget);
278 height = gtk_widget_get_allocated_height (widget);
279 context = gtk_widget_get_style_context (widget);
281 if (!priv->collapsed)
282 state |= GTK_STATE_FLAG_ACTIVE;
284 gtk_style_context_save (context);
285 gtk_style_context_set_state (context, state);
286 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
288 if (GTK_ORIENTATION_VERTICAL == orientation)
290 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
292 if (GTK_TEXT_DIR_RTL == direction)
297 y = height / 2 - priv->expander_size / 2;
301 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
302 x = width / 2 - priv->expander_size / 2;
306 /* The expander is the only animatable region */
307 gtk_style_context_push_animatable_region (context, GUINT_TO_POINTER (1));
309 gtk_render_expander (context, cr, x, y,
311 priv->expander_size);
313 gtk_style_context_pop_animatable_region (context);
314 gtk_style_context_restore (context);
320 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
323 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
324 GtkToolItemGroupPrivate* priv = group->priv;
325 GtkWidget *parent = gtk_widget_get_parent (data);
327 if (priv->collapsed ||
328 !GTK_IS_TOOL_PALETTE (parent) ||
329 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
330 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
334 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
336 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
337 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
338 GtkWidget *widget = GTK_WIDGET (group);
339 GtkToolItemGroupPrivate* priv = group->priv;
341 GtkTextDirection direction = gtk_widget_get_direction (widget);
343 gtk_widget_style_get (widget,
344 "header-spacing", &(priv->header_spacing),
345 "expander-size", &(priv->expander_size),
348 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
350 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
352 case GTK_ORIENTATION_HORIZONTAL:
353 dy = priv->header_spacing + priv->expander_size;
355 if (GTK_IS_LABEL (label_widget))
357 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
358 if (GTK_TEXT_DIR_RTL == direction)
359 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
361 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
365 case GTK_ORIENTATION_VERTICAL:
366 dx = priv->header_spacing + priv->expander_size;
368 if (GTK_IS_LABEL (label_widget))
370 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
371 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
376 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
380 gtk_tool_item_group_init (GtkToolItemGroup *group)
382 GtkWidget *alignment;
383 GtkToolItemGroupPrivate* priv;
385 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
387 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
388 GTK_TYPE_TOOL_ITEM_GROUP,
389 GtkToolItemGroupPrivate);
391 priv->children = NULL;
392 priv->header_spacing = DEFAULT_HEADER_SPACING;
393 priv->expander_size = DEFAULT_EXPANDER_SIZE;
395 priv->label_widget = gtk_label_new (NULL);
396 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
397 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
398 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
399 gtk_widget_show_all (alignment);
401 gtk_widget_push_composite_child ();
402 priv->header = gtk_button_new ();
403 gtk_widget_set_composite_name (priv->header, "header");
404 gtk_widget_pop_composite_child ();
406 g_object_ref_sink (priv->header);
407 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
408 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
409 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
411 gtk_tool_item_group_header_adjust_style (group);
413 g_signal_connect_after (alignment, "draw",
414 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
417 g_signal_connect (priv->header, "clicked",
418 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
423 gtk_tool_item_group_set_property (GObject *object,
428 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
433 gtk_tool_item_group_set_label (group, g_value_get_string (value));
436 case PROP_LABEL_WIDGET:
437 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
441 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
445 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
449 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
453 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
459 gtk_tool_item_group_get_property (GObject *object,
464 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
469 g_value_set_string (value, gtk_tool_item_group_get_label (group));
472 case PROP_LABEL_WIDGET:
473 g_value_set_object (value,
474 gtk_tool_item_group_get_label_widget (group));
478 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
482 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
486 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
490 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
496 gtk_tool_item_group_finalize (GObject *object)
498 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
500 if (group->priv->children)
502 g_list_free (group->priv->children);
503 group->priv->children = NULL;
506 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
510 gtk_tool_item_group_dispose (GObject *object)
512 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
513 GtkToolItemGroupPrivate* priv = group->priv;
517 /* disconnect focus tracking handler */
518 g_signal_handler_disconnect (priv->toplevel,
521 priv->focus_set_id = 0;
522 priv->toplevel = NULL;
525 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
529 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
530 GtkRequisition *item_size,
531 gboolean homogeneous_only,
532 gint *requested_rows)
534 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
536 if (GTK_IS_TOOL_PALETTE (parent))
537 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
539 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
543 gtk_tool_item_group_size_request (GtkWidget *widget,
544 GtkRequisition *requisition)
546 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
547 GtkToolItemGroupPrivate* priv = group->priv;
548 GtkOrientation orientation;
549 GtkRequisition item_size;
553 if (priv->children && gtk_tool_item_group_get_label_widget (group))
555 gtk_widget_get_preferred_size (priv->header,
557 gtk_widget_show (priv->header);
561 requisition->width = requisition->height = 0;
562 gtk_widget_hide (priv->header);
565 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
567 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
569 if (GTK_ORIENTATION_VERTICAL == orientation)
570 requisition->width = MAX (requisition->width, item_size.width);
572 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
574 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
575 requisition->width += border_width * 2;
576 requisition->height += border_width * 2;
580 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
584 GtkRequisition requisition;
586 gtk_tool_item_group_size_request (widget, &requisition);
588 *minimum = *natural = requisition.width;
592 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
596 GtkRequisition requisition;
598 gtk_tool_item_group_size_request (widget, &requisition);
600 *minimum = *natural = requisition.height;
605 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
606 GtkToolItemGroupChild *child)
608 GtkToolbarStyle style;
609 GtkOrientation orientation;
611 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
612 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
614 /* horizontal tool palettes with text style support only homogeneous items */
615 if (!child->homogeneous &&
616 GTK_ORIENTATION_HORIZONTAL == orientation &&
617 GTK_TOOLBAR_TEXT == style)
621 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
622 (GTK_ORIENTATION_VERTICAL == orientation ?
623 gtk_tool_item_get_visible_vertical (child->item) :
624 gtk_tool_item_get_visible_horizontal (child->item));
627 static inline unsigned
631 return (x + y - 1) / y;
635 gtk_tool_item_group_real_size_query (GtkWidget *widget,
636 GtkAllocation *allocation,
637 GtkRequisition *inquery)
639 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
640 GtkToolItemGroupPrivate* priv = group->priv;
642 GtkRequisition item_size;
643 GtkAllocation item_area;
645 GtkOrientation orientation;
650 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
651 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
653 /* figure out the size of homogeneous items */
654 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
656 if (GTK_ORIENTATION_VERTICAL == orientation)
657 item_size.width = MIN (item_size.width, allocation->width);
659 item_size.height = MIN (item_size.height, allocation->height);
661 item_size.width = MAX (item_size.width, 1);
662 item_size.height = MAX (item_size.height, 1);
665 item_area.height = 0;
667 /* figure out the required columns (n_columns) and rows (n_rows)
670 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
676 if (GTK_ORIENTATION_VERTICAL == orientation)
678 gboolean new_row = FALSE;
682 item_area.width = allocation->width - 2 * border_width;
683 n_columns = MAX (item_area.width / item_size.width, 1);
685 /* calculate required rows for n_columns columns */
686 for (it = priv->children; it != NULL; it = it->next)
688 GtkToolItemGroupChild *child = it->data;
690 if (!gtk_tool_item_group_is_item_visible (group, child))
693 if (new_row || child->new_row)
703 if (child->homogeneous)
706 if (col >= n_columns)
711 GtkRequisition req = {0, 0};
714 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
717 width = udiv (req.width, item_size.width);
725 if (col >= n_columns)
733 guint *row_min_width;
735 gboolean new_row = TRUE;
736 guint col = 0, min_col, max_col = 0, all_items = 0;
739 item_area.height = allocation->height - 2 * border_width;
740 n_rows = MAX (item_area.height / item_size.height, min_rows);
742 row_min_width = g_new0 (guint, n_rows);
744 /* calculate minimal and maximal required cols and minimal
747 for (it = priv->children; it != NULL; it = it->next)
749 GtkToolItemGroupChild *child = it->data;
751 if (!gtk_tool_item_group_is_item_visible (group, child))
754 if (new_row || child->new_row)
759 row_min_width[row] = 1;
765 if (child->homogeneous)
772 GtkRequisition req = {0, 0};
775 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
778 width = udiv (req.width, item_size.width);
783 row_min_width[row] = MAX (row_min_width[row], width);
786 max_col = MAX (max_col, col);
789 /* calculate minimal required cols */
790 min_col = udiv (all_items, n_rows);
792 for (i = 0; i <= row; i++)
794 min_col = MAX (min_col, row_min_width[i]);
797 /* simple linear search for minimal required columns
798 * for the given maximal number of rows (n_rows)
800 for (n_columns = min_col; n_columns < max_col; n_columns ++)
804 /* calculate required rows for n_columns columns */
805 for (it = priv->children; it != NULL; it = it->next)
807 GtkToolItemGroupChild *child = it->data;
809 if (!gtk_tool_item_group_is_item_visible (group, child))
812 if (new_row || child->new_row)
822 if (child->homogeneous)
825 if (col >= n_columns)
830 GtkRequisition req = {0, 0};
833 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
836 width = udiv (req.width, item_size.width);
844 if (col >= n_columns)
854 item_area.width = item_size.width * n_columns;
855 item_area.height = item_size.height * n_rows;
861 /* figure out header widget size */
862 if (gtk_widget_get_visible (priv->header))
864 GtkRequisition child_requisition;
866 gtk_widget_get_preferred_size (priv->header,
867 &child_requisition, NULL);
869 if (GTK_ORIENTATION_VERTICAL == orientation)
870 inquery->height += child_requisition.height;
872 inquery->width += child_requisition.width;
875 /* report effective widget size */
876 inquery->width += item_area.width + 2 * border_width;
877 inquery->height += item_area.height + 2 * border_width;
881 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
882 GtkAllocation *allocation)
884 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
885 GtkToolItemGroupPrivate* priv = group->priv;
886 GtkRequisition child_requisition;
887 GtkAllocation child_allocation;
889 GtkRequisition item_size;
890 GtkAllocation item_area;
892 GtkOrientation orientation;
896 gint n_columns, n_rows = 1;
899 GtkTextDirection direction;
901 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
903 direction = gtk_widget_get_direction (widget);
905 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
908 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
910 child_allocation.x = border_width;
911 child_allocation.y = border_width;
913 /* place the header widget */
914 if (gtk_widget_get_visible (priv->header))
916 gtk_widget_get_preferred_size (priv->header,
917 &child_requisition, NULL);
919 if (GTK_ORIENTATION_VERTICAL == orientation)
921 child_allocation.width = allocation->width;
922 child_allocation.height = child_requisition.height;
926 child_allocation.width = child_requisition.width;
927 child_allocation.height = allocation->height;
929 if (GTK_TEXT_DIR_RTL == direction)
930 child_allocation.x = allocation->width - border_width - child_allocation.width;
933 gtk_widget_size_allocate (priv->header, &child_allocation);
935 if (GTK_ORIENTATION_VERTICAL == orientation)
936 child_allocation.y += child_allocation.height;
937 else if (GTK_TEXT_DIR_RTL != direction)
938 child_allocation.x += child_allocation.width;
940 child_allocation.x = border_width;
943 child_requisition.width = child_requisition.height = 0;
945 /* figure out the size of homogeneous items */
946 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
948 item_size.width = MAX (item_size.width, 1);
949 item_size.height = MAX (item_size.height, 1);
951 /* figure out the available columns and size of item_area */
952 if (GTK_ORIENTATION_VERTICAL == orientation)
954 item_size.width = MIN (item_size.width, allocation->width);
956 item_area.width = allocation->width - 2 * border_width;
957 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
959 n_columns = MAX (item_area.width / item_size.width, 1);
961 item_size.width = item_area.width / n_columns;
965 item_size.height = MIN (item_size.height, allocation->height);
967 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
968 item_area.height = allocation->height - 2 * border_width;
970 n_columns = MAX (item_area.width / item_size.width, 1);
971 n_rows = MAX (item_area.height / item_size.height, min_rows);
973 item_size.height = item_area.height / n_rows;
976 item_area.x = child_allocation.x;
977 item_area.y = child_allocation.y;
979 /* when expanded or in transition, place the tool items in a grid like layout */
980 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
982 gint col = 0, row = 0;
984 for (it = priv->children; it != NULL; it = it->next)
986 GtkToolItemGroupChild *child = it->data;
989 if (!gtk_tool_item_group_is_item_visible (group, child))
991 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
996 /* for non homogeneous widgets request the required size */
997 child_requisition.width = 0;
999 if (!child->homogeneous)
1001 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
1002 &child_requisition, NULL);
1003 child_requisition.width = MIN (child_requisition.width, item_area.width);
1006 /* select next row if at end of row */
1007 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1011 child_allocation.y += child_allocation.height;
1016 /* calculate the position and size of the item */
1017 if (!child->homogeneous)
1023 col_width = udiv (child_requisition.width, item_size.width);
1025 col_width = n_columns - col;
1027 width = col_width * item_size.width;
1029 if (GTK_TEXT_DIR_RTL == direction)
1030 col_child = (n_columns - col - col_width);
1034 child_allocation.x = item_area.x + col_child * item_size.width;
1035 child_allocation.width = width;
1039 child_allocation.x =
1040 (item_area.x + col_child * item_size.width +
1041 (width - child_requisition.width) / 2);
1042 child_allocation.width = child_requisition.width;
1049 if (GTK_TEXT_DIR_RTL == direction)
1050 col_child = (n_columns - col - 1);
1052 child_allocation.x = item_area.x + col_child * item_size.width;
1053 child_allocation.width = item_size.width;
1058 child_allocation.height = item_size.height;
1060 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1061 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1064 child_allocation.y += item_size.height;
1067 /* or just hide all items, when collapsed */
1071 for (it = priv->children; it != NULL; it = it->next)
1073 GtkToolItemGroupChild *child = it->data;
1075 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1081 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1082 GtkAllocation *allocation)
1084 gtk_tool_item_group_real_size_allocate (widget, allocation);
1086 if (gtk_widget_get_mapped (widget))
1087 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1091 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1095 GtkAdjustment *adjustment;
1096 GtkAllocation allocation, p_allocation;
1099 /* Find this group's parent widget in the focused widget's anchestry. */
1100 for (p = widget; p; p = gtk_widget_get_parent (p))
1103 p = gtk_widget_get_parent (p);
1107 if (GTK_IS_TOOL_PALETTE (p))
1109 /* Check that the focused widgets is fully visible within
1110 * the group's parent widget and make it visible otherwise. */
1112 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1118 gtk_widget_get_allocation (widget, &allocation);
1119 gtk_widget_get_allocation (p, &p_allocation);
1121 /* Handle vertical adjustment. */
1122 if (gtk_widget_translate_coordinates
1123 (widget, p, 0, 0, NULL, &y) && y < 0)
1125 y += gtk_adjustment_get_value (adjustment);
1126 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1128 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1129 y > p_allocation.height)
1131 y += gtk_adjustment_get_value (adjustment);
1132 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1136 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1142 gtk_widget_get_allocation (widget, &allocation);
1143 gtk_widget_get_allocation (p, &p_allocation);
1145 /* Handle horizontal adjustment. */
1146 if (gtk_widget_translate_coordinates
1147 (widget, p, 0, 0, &x, NULL) && x < 0)
1149 x += gtk_adjustment_get_value (adjustment);
1150 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1152 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1153 x > p_allocation.width)
1155 x += gtk_adjustment_get_value (adjustment);
1156 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1165 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1166 GtkWidget *toplevel)
1168 GtkToolItemGroupPrivate* priv = group->priv;
1170 if (toplevel != priv->toplevel)
1174 /* Disconnect focus tracking handler. */
1175 g_signal_handler_disconnect (priv->toplevel,
1176 priv->focus_set_id);
1178 priv->focus_set_id = 0;
1179 priv->toplevel = NULL;
1184 /* Install focus tracking handler. We connect to the window's
1185 * set-focus signal instead of connecting to the focus signal of
1188 * 1) Reduce the number of signal handlers used.
1189 * 2) Avoid special handling for group headers.
1190 * 3) Catch focus grabs not only for direct children,
1191 * but also for nested widgets.
1193 priv->focus_set_id =
1194 g_signal_connect (toplevel, "set-focus",
1195 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1198 priv->toplevel = toplevel;
1204 gtk_tool_item_group_realize (GtkWidget *widget)
1206 GtkAllocation allocation;
1207 GtkWidget *toplevel_window;
1209 GdkWindowAttr attributes;
1210 gint attributes_mask;
1212 GtkStyleContext *context;
1214 gtk_widget_set_realized (widget, TRUE);
1216 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1217 context = gtk_widget_get_style_context (widget);
1219 gtk_widget_get_allocation (widget, &allocation);
1221 attributes.window_type = GDK_WINDOW_CHILD;
1222 attributes.x = allocation.x + border_width;
1223 attributes.y = allocation.y + border_width;
1224 attributes.width = allocation.width - border_width * 2;
1225 attributes.height = allocation.height - border_width * 2;
1226 attributes.wclass = GDK_INPUT_OUTPUT;
1227 attributes.visual = gtk_widget_get_visual (widget);
1228 attributes.event_mask = gtk_widget_get_events (widget)
1229 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1230 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1231 | GDK_BUTTON_MOTION_MASK;
1232 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1234 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1235 &attributes, attributes_mask);
1236 gtk_widget_set_window (widget, window);
1238 gdk_window_set_user_data (window, widget);
1240 gtk_style_context_set_background (context, window);
1242 gtk_container_forall (GTK_CONTAINER (widget),
1243 (GtkCallback) gtk_widget_set_parent_window,
1246 gtk_widget_queue_resize_no_redraw (widget);
1248 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1249 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1254 gtk_tool_item_group_unrealize (GtkWidget *widget)
1256 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1257 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1261 gtk_tool_item_group_style_updated (GtkWidget *widget)
1263 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1264 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
1268 gtk_tool_item_group_add (GtkContainer *container,
1271 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1272 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1274 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1275 GTK_TOOL_ITEM (widget), -1);
1279 gtk_tool_item_group_remove (GtkContainer *container,
1282 GtkToolItemGroup *group;
1283 GtkToolItemGroupPrivate* priv;
1286 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1287 group = GTK_TOOL_ITEM_GROUP (container);
1290 for (it = priv->children; it != NULL; it = it->next)
1292 GtkToolItemGroupChild *child_info = it->data;
1294 if ((GtkWidget *)child_info->item == child)
1296 g_object_unref (child);
1297 gtk_widget_unparent (child);
1299 g_free (child_info);
1300 priv->children = g_list_delete_link (priv->children, it);
1302 gtk_widget_queue_resize (GTK_WIDGET (container));
1309 gtk_tool_item_group_forall (GtkContainer *container,
1311 GtkCallback callback,
1312 gpointer callback_data)
1314 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1315 GtkToolItemGroupPrivate* priv = group->priv;
1318 if (internals && priv->header)
1319 callback (priv->header, callback_data);
1321 children = priv->children;
1324 GtkToolItemGroupChild *child = children->data;
1325 children = children->next; /* store pointer before call to callback
1326 because the child pointer is invalid if the
1327 child->item is removed from the item group
1330 callback (GTK_WIDGET (child->item), callback_data);
1335 gtk_tool_item_group_child_type (GtkContainer *container)
1337 return GTK_TYPE_TOOL_ITEM;
1340 static GtkToolItemGroupChild *
1341 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1349 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1350 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1352 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1354 GtkToolItemGroupChild *child = it->data;
1356 if (child->item == item)
1372 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1374 gboolean *homogeneous,
1379 GtkToolItemGroupChild *child;
1381 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1382 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1384 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1389 *expand = child->expand;
1392 *homogeneous = child->homogeneous;
1395 *fill = child->fill;
1398 *new_row = child->new_row;
1402 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1404 gboolean homogeneous,
1409 GtkToolItemGroupChild *child;
1410 gboolean changed = FALSE;
1412 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1413 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1415 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1419 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1421 if (child->homogeneous != homogeneous)
1423 child->homogeneous = homogeneous;
1425 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1427 if (child->expand != expand)
1429 child->expand = expand;
1431 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1433 if (child->fill != fill)
1437 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1439 if (child->new_row != new_row)
1441 child->new_row = new_row;
1443 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1446 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1449 && gtk_widget_get_visible (GTK_WIDGET (group))
1450 && gtk_widget_get_visible (GTK_WIDGET (item)))
1451 gtk_widget_queue_resize (GTK_WIDGET (group));
1455 gtk_tool_item_group_set_child_property (GtkContainer *container,
1458 const GValue *value,
1461 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1462 GtkToolItem *item = GTK_TOOL_ITEM (child);
1463 gboolean homogeneous, expand, fill, new_row;
1465 if (prop_id != CHILD_PROP_POSITION)
1466 gtk_tool_item_group_get_item_packing (group, item,
1474 case CHILD_PROP_HOMOGENEOUS:
1475 gtk_tool_item_group_set_item_packing (group, item,
1476 g_value_get_boolean (value),
1482 case CHILD_PROP_EXPAND:
1483 gtk_tool_item_group_set_item_packing (group, item,
1485 g_value_get_boolean (value),
1490 case CHILD_PROP_FILL:
1491 gtk_tool_item_group_set_item_packing (group, item,
1494 g_value_get_boolean (value),
1498 case CHILD_PROP_NEW_ROW:
1499 gtk_tool_item_group_set_item_packing (group, item,
1503 g_value_get_boolean (value));
1506 case CHILD_PROP_POSITION:
1507 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1511 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1517 gtk_tool_item_group_get_child_property (GtkContainer *container,
1523 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1524 GtkToolItem *item = GTK_TOOL_ITEM (child);
1525 gboolean homogeneous, expand, fill, new_row;
1527 if (prop_id != CHILD_PROP_POSITION)
1528 gtk_tool_item_group_get_item_packing (group, item,
1536 case CHILD_PROP_HOMOGENEOUS:
1537 g_value_set_boolean (value, homogeneous);
1540 case CHILD_PROP_EXPAND:
1541 g_value_set_boolean (value, expand);
1544 case CHILD_PROP_FILL:
1545 g_value_set_boolean (value, fill);
1548 case CHILD_PROP_NEW_ROW:
1549 g_value_set_boolean (value, new_row);
1552 case CHILD_PROP_POSITION:
1553 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1557 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1563 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1565 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1566 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1567 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1569 oclass->set_property = gtk_tool_item_group_set_property;
1570 oclass->get_property = gtk_tool_item_group_get_property;
1571 oclass->finalize = gtk_tool_item_group_finalize;
1572 oclass->dispose = gtk_tool_item_group_dispose;
1574 wclass->get_preferred_width = gtk_tool_item_group_get_preferred_width;
1575 wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1576 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1577 wclass->realize = gtk_tool_item_group_realize;
1578 wclass->unrealize = gtk_tool_item_group_unrealize;
1579 wclass->style_updated = gtk_tool_item_group_style_updated;
1580 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1582 cclass->add = gtk_tool_item_group_add;
1583 cclass->remove = gtk_tool_item_group_remove;
1584 cclass->forall = gtk_tool_item_group_forall;
1585 cclass->child_type = gtk_tool_item_group_child_type;
1586 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1587 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1589 g_object_class_install_property (oclass, PROP_LABEL,
1590 g_param_spec_string ("label",
1592 P_("The human-readable title of this item group"),
1594 GTK_PARAM_READWRITE));
1596 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1597 g_param_spec_object ("label-widget",
1599 P_("A widget to display in place of the usual label"),
1601 GTK_PARAM_READWRITE));
1603 g_object_class_install_property (oclass, PROP_COLLAPSED,
1604 g_param_spec_boolean ("collapsed",
1606 P_("Whether the group has been collapsed and items are hidden"),
1608 GTK_PARAM_READWRITE));
1610 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1611 g_param_spec_enum ("ellipsize",
1613 P_("Ellipsize for item group headers"),
1614 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1615 GTK_PARAM_READWRITE));
1617 g_object_class_install_property (oclass, PROP_RELIEF,
1618 g_param_spec_enum ("header-relief",
1619 P_("Header Relief"),
1620 P_("Relief of the group header button"),
1621 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1622 GTK_PARAM_READWRITE));
1624 gtk_widget_class_install_style_property (wclass,
1625 g_param_spec_int ("expander-size",
1626 P_("Expander Size"),
1627 P_("Size of the expander arrow"),
1630 DEFAULT_EXPANDER_SIZE,
1631 GTK_PARAM_READABLE));
1633 gtk_widget_class_install_style_property (wclass,
1634 g_param_spec_int ("header-spacing",
1635 P_("Header Spacing"),
1636 P_("Spacing between expander arrow and caption"),
1639 DEFAULT_HEADER_SPACING,
1640 GTK_PARAM_READABLE));
1642 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1643 g_param_spec_boolean ("homogeneous",
1645 P_("Whether the item should be the same size as other homogeneous items"),
1647 GTK_PARAM_READWRITE));
1649 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1650 g_param_spec_boolean ("expand",
1652 P_("Whether the item should receive extra space when the group grows"),
1654 GTK_PARAM_READWRITE));
1656 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1657 g_param_spec_boolean ("fill",
1659 P_("Whether the item should fill the available space"),
1661 GTK_PARAM_READWRITE));
1663 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1664 g_param_spec_boolean ("new-row",
1666 P_("Whether the item should start a new row"),
1668 GTK_PARAM_READWRITE));
1670 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1671 g_param_spec_int ("position",
1673 P_("Position of the item within this group"),
1677 GTK_PARAM_READWRITE));
1679 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1683 * gtk_tool_item_group_new:
1684 * @label: the label of the new group
1686 * Creates a new tool item group with label @label.
1688 * Returns: a new #GtkToolItemGroup.
1693 gtk_tool_item_group_new (const gchar *label)
1695 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1699 * gtk_tool_item_group_set_label:
1700 * @group: a #GtkToolItemGroup
1701 * @label: the new human-readable label of of the group
1703 * Sets the label of the tool item group. The label is displayed in the header
1709 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1712 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1715 gtk_tool_item_group_set_label_widget (group, NULL);
1718 GtkWidget *child = gtk_label_new (label);
1719 gtk_widget_show (child);
1721 gtk_tool_item_group_set_label_widget (group, child);
1724 g_object_notify (G_OBJECT (group), "label");
1728 * gtk_tool_item_group_set_label_widget:
1729 * @group: a #GtkToolItemGroup
1730 * @label_widget: the widget to be displayed in place of the usual label
1732 * Sets the label of the tool item group.
1733 * The label widget is displayed in the header of the group, in place
1734 * of the usual label.
1739 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1740 GtkWidget *label_widget)
1742 GtkToolItemGroupPrivate* priv;
1743 GtkWidget *alignment;
1745 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1746 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1747 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1751 if (priv->label_widget == label_widget)
1754 alignment = gtk_tool_item_group_get_alignment (group);
1756 if (priv->label_widget)
1758 gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1759 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1764 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1766 priv->label_widget = label_widget;
1768 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1769 gtk_widget_queue_resize (GTK_WIDGET (group));
1771 /* Only show the header widget if the group has children: */
1772 if (label_widget && priv->children)
1773 gtk_widget_show (priv->header);
1775 gtk_widget_hide (priv->header);
1777 g_object_freeze_notify (G_OBJECT (group));
1778 g_object_notify (G_OBJECT (group), "label-widget");
1779 g_object_notify (G_OBJECT (group), "label");
1780 g_object_thaw_notify (G_OBJECT (group));
1784 * gtk_tool_item_group_set_header_relief:
1785 * @group: a #GtkToolItemGroup
1786 * @style: the #GtkReliefStyle
1788 * Set the button relief of the group header.
1789 * See gtk_button_set_relief() for details.
1794 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1795 GtkReliefStyle style)
1797 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1799 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1803 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1805 return (g_source_get_time (group->priv->animation_timeout) -
1806 group->priv->animation_start) / 1000;
1810 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1812 GtkToolItemGroupPrivate* priv = group->priv;
1813 GtkWidget *widget = GTK_WIDGET (group);
1815 if (gtk_widget_get_realized (priv->header))
1817 GtkAllocation alignment_allocation;
1818 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1821 /* Find the header button's arrow area... */
1822 gtk_widget_get_allocation (alignment, &alignment_allocation);
1823 area.x = alignment_allocation.x;
1824 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1825 area.height = priv->expander_size;
1826 area.width = priv->expander_size;
1828 /* ... and invalidated it to get it animated. */
1829 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1832 if (gtk_widget_get_realized (widget))
1834 GtkAllocation allocation;
1835 GtkWidget *parent = gtk_widget_get_parent (widget);
1836 int x, y, width, height;
1838 /* Find the tool item area button's arrow area... */
1839 gtk_widget_get_allocation (widget, &allocation);
1840 width = allocation.width;
1841 height = allocation.height;
1843 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1845 if (gtk_widget_get_visible (priv->header))
1847 GtkAllocation header_allocation;
1849 gtk_widget_get_allocation (priv->header, &header_allocation);
1850 height -= header_allocation.height;
1851 y += header_allocation.height;
1854 /* ... and invalidated it to get it animated. */
1855 gtk_widget_queue_draw_area (parent, x, y, width, height);
1860 gtk_tool_item_group_animation_cb (gpointer data)
1862 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1863 GtkToolItemGroupPrivate* priv = group->priv;
1864 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1867 GDK_THREADS_ENTER ();
1869 /* Enque this early to reduce number of expose events. */
1870 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1872 gtk_tool_item_group_force_expose (group);
1874 /* Finish animation when done. */
1875 if (timestamp >= ANIMATION_DURATION)
1876 priv->animation_timeout = NULL;
1878 retval = (priv->animation_timeout != NULL);
1880 GDK_THREADS_LEAVE ();
1886 * gtk_tool_item_group_set_collapsed:
1887 * @group: a #GtkToolItemGroup
1888 * @collapsed: whether the @group should be collapsed or expanded
1890 * Sets whether the @group should be collapsed or expanded.
1895 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1899 GtkToolItemGroupPrivate* priv;
1901 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1905 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1906 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1907 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1908 GTK_WIDGET (group));
1909 if (collapsed != priv->collapsed)
1911 GtkStyleContext *context;
1913 if (priv->animation)
1915 if (priv->animation_timeout)
1916 g_source_destroy (priv->animation_timeout);
1918 priv->animation_start = g_get_monotonic_time ();
1919 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1921 g_source_set_callback (priv->animation_timeout,
1922 gtk_tool_item_group_animation_cb,
1924 g_source_attach (priv->animation_timeout, NULL);
1926 context = gtk_widget_get_style_context (gtk_bin_get_child (GTK_BIN (priv->header)));
1928 gtk_style_context_save (context);
1929 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
1931 gtk_style_context_notify_state_change (context,
1932 gtk_widget_get_window (priv->header),
1933 GUINT_TO_POINTER (1),
1934 GTK_STATE_FLAG_ACTIVE,
1937 gtk_style_context_restore (context);
1940 gtk_tool_item_group_force_expose (group);
1942 priv->collapsed = collapsed;
1943 g_object_notify (G_OBJECT (group), "collapsed");
1948 * gtk_tool_item_group_set_ellipsize:
1949 * @group: a #GtkToolItemGroup
1950 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1952 * Sets the ellipsization mode which should be used by labels in @group.
1957 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1958 PangoEllipsizeMode ellipsize)
1960 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1962 if (ellipsize != group->priv->ellipsize)
1964 group->priv->ellipsize = ellipsize;
1965 gtk_tool_item_group_header_adjust_style (group);
1966 g_object_notify (G_OBJECT (group), "ellipsize");
1967 _gtk_tool_item_group_palette_reconfigured (group);
1972 * gtk_tool_item_group_get_label:
1973 * @group: a #GtkToolItemGroup
1975 * Gets the label of @group.
1977 * Returns: the label of @group. The label is an internal string of @group
1978 * and must not be modified. Note that %NULL is returned if a custom
1979 * label has been set with gtk_tool_item_group_set_label_widget()
1984 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1986 GtkToolItemGroupPrivate *priv;
1988 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1992 if (GTK_IS_LABEL (priv->label_widget))
1993 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1999 * gtk_tool_item_group_get_label_widget:
2000 * @group: a #GtkToolItemGroup
2002 * Gets the label widget of @group.
2003 * See gtk_tool_item_group_set_label_widget().
2005 * Returns: (transfer none): the label widget of @group
2010 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2012 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2014 return gtk_bin_get_child (GTK_BIN (alignment));
2018 * gtk_tool_item_group_get_collapsed:
2019 * @group: a GtkToolItemGroup
2021 * Gets whether @group is collapsed or expanded.
2023 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2028 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2030 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2032 return group->priv->collapsed;
2036 * gtk_tool_item_group_get_ellipsize:
2037 * @group: a #GtkToolItemGroup
2039 * Gets the ellipsization mode of @group.
2041 * Returns: the #PangoEllipsizeMode of @group
2046 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2048 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2050 return group->priv->ellipsize;
2054 * gtk_tool_item_group_get_header_relief:
2055 * @group: a #GtkToolItemGroup
2057 * Gets the relief mode of the header button of @group.
2059 * Returns: the #GtkReliefStyle
2064 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2066 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2068 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2072 * gtk_tool_item_group_insert:
2073 * @group: a #GtkToolItemGroup
2074 * @item: the #GtkToolItem to insert into @group
2075 * @position: the position of @item in @group, starting with 0.
2076 * The position -1 means end of list.
2078 * Inserts @item at @position in the list of children of @group.
2083 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2087 GtkWidget *parent, *child_widget;
2088 GtkToolItemGroupChild *child;
2090 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2091 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2092 g_return_if_fail (position >= -1);
2094 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2096 child = g_new (GtkToolItemGroupChild, 1);
2097 child->item = g_object_ref_sink (item);
2098 child->homogeneous = TRUE;
2099 child->expand = FALSE;
2101 child->new_row = FALSE;
2103 group->priv->children = g_list_insert (group->priv->children, child, position);
2105 if (GTK_IS_TOOL_PALETTE (parent))
2106 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2108 child_widget = gtk_bin_get_child (GTK_BIN (item));
2110 if (GTK_IS_BUTTON (child_widget))
2111 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2113 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2117 * gtk_tool_item_group_set_item_position:
2118 * @group: a #GtkToolItemGroup
2119 * @item: the #GtkToolItem to move to a new position, should
2120 * be a child of @group.
2121 * @position: the new position of @item in @group, starting with 0.
2122 * The position -1 means end of list.
2124 * Sets the position of @item in the list of children of @group.
2129 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2135 GtkToolItemGroupChild *child;
2136 GtkToolItemGroupPrivate* priv;
2138 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2139 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2140 g_return_if_fail (position >= -1);
2142 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2145 g_return_if_fail (child != NULL);
2147 if (position == old_position)
2150 priv->children = g_list_delete_link (priv->children, link);
2151 priv->children = g_list_insert (priv->children, child, position);
2153 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2154 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2155 gtk_widget_get_visible (GTK_WIDGET (item)))
2156 gtk_widget_queue_resize (GTK_WIDGET (group));
2160 * gtk_tool_item_group_get_item_position:
2161 * @group: a #GtkToolItemGroup
2162 * @item: a #GtkToolItem
2164 * Gets the position of @item in @group as index.
2166 * Returns: the index of @item in @group or -1 if @item is no child of @group
2171 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2176 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2177 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2179 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2186 * gtk_tool_item_group_get_n_items:
2187 * @group: a #GtkToolItemGroup
2189 * Gets the number of tool items in @group.
2191 * Returns: the number of tool items in @group
2196 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2198 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2200 return g_list_length (group->priv->children);
2204 * gtk_tool_item_group_get_nth_item:
2205 * @group: a #GtkToolItemGroup
2208 * Gets the tool item at @index in group.
2210 * Returns: (transfer none): the #GtkToolItem at index
2215 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2218 GtkToolItemGroupChild *child;
2220 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2222 child = g_list_nth_data (group->priv->children, index);
2224 return child != NULL ? child->item : NULL;
2228 * gtk_tool_item_group_get_drop_item:
2229 * @group: a #GtkToolItemGroup
2230 * @x: the x position
2231 * @y: the y position
2233 * Gets the tool item at position (x, y).
2235 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2240 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2244 GtkAllocation allocation;
2247 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2249 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2251 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2252 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2254 for (it = group->priv->children; it != NULL; it = it->next)
2256 GtkToolItemGroupChild *child = it->data;
2257 GtkToolItem *item = child->item;
2260 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2263 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2265 x0 = x - allocation.x;
2266 y0 = y - allocation.y;
2268 if (x0 >= 0 && x0 < allocation.width &&
2269 y0 >= 0 && y0 < allocation.height)
2277 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2278 GtkRequisition *item_size,
2279 gboolean homogeneous_only,
2280 gint *requested_rows)
2282 GtkRequisition child_requisition;
2285 gboolean new_row = TRUE;
2287 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2288 g_return_if_fail (NULL != item_size);
2290 item_size->width = item_size->height = 0;
2292 for (it = group->priv->children; it != NULL; it = it->next)
2294 GtkToolItemGroupChild *child = it->data;
2296 if (!gtk_tool_item_group_is_item_visible (group, child))
2299 if (child->new_row || new_row)
2305 if (!child->homogeneous && child->expand)
2308 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2309 &child_requisition, NULL);
2311 if (!homogeneous_only || child->homogeneous)
2312 item_size->width = MAX (item_size->width, child_requisition.width);
2313 item_size->height = MAX (item_size->height, child_requisition.height);
2317 *requested_rows = rows;
2321 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2324 GtkAllocation allocation;
2325 GtkWidget *widget = GTK_WIDGET (group);
2326 GtkToolItemGroupPrivate* priv = group->priv;
2328 gtk_widget_get_allocation (widget, &allocation);
2330 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2334 if (priv->animation_timeout)
2336 GtkAllocation header_allocation;
2337 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2338 cairo_pattern_t *mask;
2341 if (GTK_ORIENTATION_VERTICAL == orientation)
2342 v1 = allocation.height;
2344 v1 = allocation.width;
2348 gtk_widget_get_allocation (priv->header, &header_allocation);
2349 if (!gtk_widget_get_visible (priv->header))
2351 else if (GTK_ORIENTATION_VERTICAL == orientation)
2352 v0 = MAX (v0, header_allocation.height);
2354 v0 = MAX (v0, header_allocation.width);
2356 v1 = MIN (v0 + 256, v1);
2358 if (GTK_ORIENTATION_VERTICAL == orientation)
2363 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2370 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2373 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2374 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2375 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2376 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2377 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2379 cairo_mask (cr, mask);
2380 cairo_pattern_destroy (mask);
2387 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2392 GtkRequisition requisition;
2393 GtkToolItemGroupPrivate* priv = group->priv;
2395 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2396 &requisition, NULL);
2398 if (!priv->collapsed || priv->animation_timeout)
2400 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2401 GtkRequisition inquery;
2404 allocation.width = limit;
2406 allocation.height = limit;
2408 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2409 &allocation, &inquery);
2412 inquery.height -= requisition.height;
2414 inquery.width -= requisition.width;
2416 if (priv->animation_timeout && animation)
2418 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2420 timestamp = MIN (timestamp, ANIMATION_DURATION);
2422 if (priv->collapsed)
2423 timestamp = ANIMATION_DURATION - timestamp;
2427 inquery.height *= timestamp;
2428 inquery.height /= ANIMATION_DURATION;
2432 inquery.width *= timestamp;
2433 inquery.width /= ANIMATION_DURATION;
2438 requisition.height += inquery.height;
2440 requisition.width += inquery.width;
2443 return (vertical ? requisition.height : requisition.width);
2447 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2450 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2454 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2457 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2461 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2464 if (GTK_IS_TOOL_ITEM (child))
2465 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2470 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2472 gtk_container_foreach (GTK_CONTAINER (group),
2473 gtk_tool_palette_reconfigured_foreach_item,
2476 gtk_tool_item_group_header_adjust_style (group);