1 /* GtkToolPalette -- A tool palette with categories and DnD support
2 * Copyright (C) 2008 Openismus GmbH
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "gtktoolpaletteprivate.h"
29 #include "gtktypebuiltins.h"
30 #include "gtkprivate.h"
33 #define ANIMATION_TIMEOUT 50
34 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
35 #define DEFAULT_ANIMATION_STATE TRUE
36 #define DEFAULT_EXPANDER_SIZE 16
37 #define DEFAULT_HEADER_SPACING 2
39 #define DEFAULT_LABEL ""
40 #define DEFAULT_COLLAPSED FALSE
41 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
44 * SECTION:gtktoolitemgroup
45 * @Short_description: A sub container used in a tool palette
46 * @Title: GtkToolItemGroup
48 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
49 * #GtkToolItem<!-- -->s to a palette like container with different
50 * categories and drag and drop support.
68 CHILD_PROP_HOMOGENEOUS,
75 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
77 struct _GtkToolItemGroupPrivate
80 GtkWidget *label_widget;
85 gint64 animation_start;
86 GSource *animation_timeout;
89 PangoEllipsizeMode ellipsize;
94 GtkSettings *settings;
95 gulong settings_connection;
100 struct _GtkToolItemGroupChild
104 guint homogeneous : 1;
110 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
112 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
113 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
116 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
118 return gtk_bin_get_child (GTK_BIN (group->priv->header));
121 static GtkOrientation
122 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
124 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
126 if (GTK_IS_TOOL_PALETTE (parent))
127 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
129 return GTK_ORIENTATION_VERTICAL;
132 static GtkToolbarStyle
133 gtk_tool_item_group_get_style (GtkToolShell *shell)
135 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
137 if (GTK_IS_TOOL_PALETTE (parent))
138 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
140 return GTK_TOOLBAR_ICONS;
144 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
146 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
148 if (GTK_IS_TOOL_PALETTE (parent))
149 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
151 return GTK_ICON_SIZE_SMALL_TOOLBAR;
154 static PangoEllipsizeMode
155 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
157 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
161 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
163 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
164 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
170 static GtkOrientation
171 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
173 return GTK_ORIENTATION_HORIZONTAL;
176 static GtkSizeGroup *
177 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
179 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
181 if (GTK_IS_TOOL_PALETTE (parent))
182 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
188 animation_change_notify (GtkToolItemGroup *group)
190 GtkSettings *settings = group->priv->settings;
194 g_object_get (settings,
195 "gtk-enable-animations", &animation,
198 animation = DEFAULT_ANIMATION_STATE;
200 group->priv->animation = animation;
204 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
205 const GParamSpec *pspec,
206 GtkToolItemGroup *group)
208 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
209 animation_change_notify (group);
213 gtk_tool_item_group_screen_changed (GtkWidget *widget,
214 GdkScreen *previous_screen)
216 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
217 GtkToolItemGroupPrivate* priv = group->priv;
218 GtkSettings *old_settings = priv->settings;
219 GtkSettings *settings;
221 if (gtk_widget_has_screen (GTK_WIDGET (group)))
222 settings = gtk_widget_get_settings (GTK_WIDGET (group));
226 if (settings == old_settings)
231 g_signal_handler_disconnect (old_settings, priv->settings_connection);
232 g_object_unref (old_settings);
237 priv->settings_connection =
238 g_signal_connect (settings, "notify",
239 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
241 priv->settings = g_object_ref (settings);
244 priv->settings = NULL;
246 animation_change_notify (group);
250 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
252 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
253 iface->get_orientation = gtk_tool_item_group_get_orientation;
254 iface->get_style = gtk_tool_item_group_get_style;
255 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
256 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
257 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
258 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
262 gtk_tool_item_group_header_draw_cb (GtkWidget *widget,
266 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
267 GtkToolItemGroupPrivate* priv = group->priv;
268 GtkOrientation orientation;
269 gint x, y, width, height;
270 GtkTextDirection direction;
271 GtkStyleContext *context;
272 GtkStateFlags state = 0;
274 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
275 direction = gtk_widget_get_direction (widget);
276 width = gtk_widget_get_allocated_width (widget);
277 height = gtk_widget_get_allocated_height (widget);
278 context = gtk_widget_get_style_context (widget);
280 if (!priv->collapsed)
281 state |= GTK_STATE_FLAG_ACTIVE;
283 gtk_style_context_save (context);
284 gtk_style_context_set_state (context, state);
285 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
287 if (GTK_ORIENTATION_VERTICAL == orientation)
289 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
291 if (GTK_TEXT_DIR_RTL == direction)
296 y = height / 2 - priv->expander_size / 2;
300 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
301 x = width / 2 - priv->expander_size / 2;
305 /* The expander is the only animatable region */
306 gtk_style_context_push_animatable_region (context, GUINT_TO_POINTER (1));
308 gtk_render_expander (context, cr, x, y,
310 priv->expander_size);
312 gtk_style_context_pop_animatable_region (context);
313 gtk_style_context_restore (context);
319 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
322 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
323 GtkToolItemGroupPrivate* priv = group->priv;
324 GtkWidget *parent = gtk_widget_get_parent (data);
326 if (priv->collapsed ||
327 !GTK_IS_TOOL_PALETTE (parent) ||
328 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
329 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
333 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
335 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
336 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
337 GtkWidget *widget = GTK_WIDGET (group);
338 GtkToolItemGroupPrivate* priv = group->priv;
340 GtkTextDirection direction = gtk_widget_get_direction (widget);
342 gtk_widget_style_get (widget,
343 "header-spacing", &(priv->header_spacing),
344 "expander-size", &(priv->expander_size),
347 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
349 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
351 case GTK_ORIENTATION_HORIZONTAL:
352 dy = priv->header_spacing + priv->expander_size;
354 if (GTK_IS_LABEL (label_widget))
356 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
357 if (GTK_TEXT_DIR_RTL == direction)
358 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
360 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
364 case GTK_ORIENTATION_VERTICAL:
365 dx = priv->header_spacing + priv->expander_size;
367 if (GTK_IS_LABEL (label_widget))
369 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
370 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
375 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
379 gtk_tool_item_group_init (GtkToolItemGroup *group)
381 GtkWidget *alignment;
382 GtkToolItemGroupPrivate* priv;
384 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
386 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
387 GTK_TYPE_TOOL_ITEM_GROUP,
388 GtkToolItemGroupPrivate);
390 priv->children = NULL;
391 priv->header_spacing = DEFAULT_HEADER_SPACING;
392 priv->expander_size = DEFAULT_EXPANDER_SIZE;
394 priv->label_widget = gtk_label_new (NULL);
395 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
396 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
397 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
398 gtk_widget_show_all (alignment);
400 gtk_widget_push_composite_child ();
401 priv->header = gtk_button_new ();
402 gtk_widget_set_composite_name (priv->header, "header");
403 gtk_widget_pop_composite_child ();
405 g_object_ref_sink (priv->header);
406 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
407 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
408 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
410 gtk_tool_item_group_header_adjust_style (group);
412 g_signal_connect_after (alignment, "draw",
413 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
416 g_signal_connect (priv->header, "clicked",
417 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
422 gtk_tool_item_group_set_property (GObject *object,
427 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
432 gtk_tool_item_group_set_label (group, g_value_get_string (value));
435 case PROP_LABEL_WIDGET:
436 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
440 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
444 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
448 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gtk_tool_item_group_get_property (GObject *object,
463 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
468 g_value_set_string (value, gtk_tool_item_group_get_label (group));
471 case PROP_LABEL_WIDGET:
472 g_value_set_object (value,
473 gtk_tool_item_group_get_label_widget (group));
477 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
481 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
485 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gtk_tool_item_group_finalize (GObject *object)
497 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
499 if (group->priv->children)
501 g_list_free (group->priv->children);
502 group->priv->children = NULL;
505 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
509 gtk_tool_item_group_dispose (GObject *object)
511 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
512 GtkToolItemGroupPrivate* priv = group->priv;
516 /* disconnect focus tracking handler */
517 g_signal_handler_disconnect (priv->toplevel,
520 priv->focus_set_id = 0;
521 priv->toplevel = NULL;
524 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
528 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
529 GtkRequisition *item_size,
530 gboolean homogeneous_only,
531 gint *requested_rows)
533 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
535 if (GTK_IS_TOOL_PALETTE (parent))
536 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
538 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
542 gtk_tool_item_group_size_request (GtkWidget *widget,
543 GtkRequisition *requisition)
545 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
546 GtkToolItemGroupPrivate* priv = group->priv;
547 GtkOrientation orientation;
548 GtkRequisition item_size;
552 if (priv->children && gtk_tool_item_group_get_label_widget (group))
554 gtk_widget_get_preferred_size (priv->header,
556 gtk_widget_show (priv->header);
560 requisition->width = requisition->height = 0;
561 gtk_widget_hide (priv->header);
564 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
566 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
568 if (GTK_ORIENTATION_VERTICAL == orientation)
569 requisition->width = MAX (requisition->width, item_size.width);
571 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
573 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
574 requisition->width += border_width * 2;
575 requisition->height += border_width * 2;
579 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
583 GtkRequisition requisition;
585 gtk_tool_item_group_size_request (widget, &requisition);
587 *minimum = *natural = requisition.width;
591 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
595 GtkRequisition requisition;
597 gtk_tool_item_group_size_request (widget, &requisition);
599 *minimum = *natural = requisition.height;
604 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
605 GtkToolItemGroupChild *child)
607 GtkToolbarStyle style;
608 GtkOrientation orientation;
610 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
611 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
613 /* horizontal tool palettes with text style support only homogeneous items */
614 if (!child->homogeneous &&
615 GTK_ORIENTATION_HORIZONTAL == orientation &&
616 GTK_TOOLBAR_TEXT == style)
620 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
621 (GTK_ORIENTATION_VERTICAL == orientation ?
622 gtk_tool_item_get_visible_vertical (child->item) :
623 gtk_tool_item_get_visible_horizontal (child->item));
626 static inline unsigned
630 return (x + y - 1) / y;
634 gtk_tool_item_group_real_size_query (GtkWidget *widget,
635 GtkAllocation *allocation,
636 GtkRequisition *inquery)
638 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
639 GtkToolItemGroupPrivate* priv = group->priv;
641 GtkRequisition item_size;
642 GtkAllocation item_area;
644 GtkOrientation orientation;
649 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
650 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
652 /* figure out the size of homogeneous items */
653 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
655 if (GTK_ORIENTATION_VERTICAL == orientation)
656 item_size.width = MIN (item_size.width, allocation->width);
658 item_size.height = MIN (item_size.height, allocation->height);
660 item_size.width = MAX (item_size.width, 1);
661 item_size.height = MAX (item_size.height, 1);
664 item_area.height = 0;
666 /* figure out the required columns (n_columns) and rows (n_rows)
669 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
675 if (GTK_ORIENTATION_VERTICAL == orientation)
677 gboolean new_row = FALSE;
681 item_area.width = allocation->width - 2 * border_width;
682 n_columns = MAX (item_area.width / item_size.width, 1);
684 /* calculate required rows for n_columns columns */
685 for (it = priv->children; it != NULL; it = it->next)
687 GtkToolItemGroupChild *child = it->data;
689 if (!gtk_tool_item_group_is_item_visible (group, child))
692 if (new_row || child->new_row)
702 if (child->homogeneous)
705 if (col >= n_columns)
710 GtkRequisition req = {0, 0};
713 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
716 width = udiv (req.width, item_size.width);
724 if (col >= n_columns)
732 guint *row_min_width;
734 gboolean new_row = TRUE;
735 guint col = 0, min_col, max_col = 0, all_items = 0;
738 item_area.height = allocation->height - 2 * border_width;
739 n_rows = MAX (item_area.height / item_size.height, min_rows);
741 row_min_width = g_new0 (guint, n_rows);
743 /* calculate minimal and maximal required cols and minimal
746 for (it = priv->children; it != NULL; it = it->next)
748 GtkToolItemGroupChild *child = it->data;
750 if (!gtk_tool_item_group_is_item_visible (group, child))
753 if (new_row || child->new_row)
758 row_min_width[row] = 1;
764 if (child->homogeneous)
771 GtkRequisition req = {0, 0};
774 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
777 width = udiv (req.width, item_size.width);
782 row_min_width[row] = MAX (row_min_width[row], width);
785 max_col = MAX (max_col, col);
788 /* calculate minimal required cols */
789 min_col = udiv (all_items, n_rows);
791 for (i = 0; i <= row; i++)
793 min_col = MAX (min_col, row_min_width[i]);
796 /* simple linear search for minimal required columns
797 * for the given maximal number of rows (n_rows)
799 for (n_columns = min_col; n_columns < max_col; n_columns ++)
803 /* calculate required rows for n_columns columns */
804 for (it = priv->children; it != NULL; it = it->next)
806 GtkToolItemGroupChild *child = it->data;
808 if (!gtk_tool_item_group_is_item_visible (group, child))
811 if (new_row || child->new_row)
821 if (child->homogeneous)
824 if (col >= n_columns)
829 GtkRequisition req = {0, 0};
832 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
835 width = udiv (req.width, item_size.width);
843 if (col >= n_columns)
853 item_area.width = item_size.width * n_columns;
854 item_area.height = item_size.height * n_rows;
860 /* figure out header widget size */
861 if (gtk_widget_get_visible (priv->header))
863 GtkRequisition child_requisition;
865 gtk_widget_get_preferred_size (priv->header,
866 &child_requisition, NULL);
868 if (GTK_ORIENTATION_VERTICAL == orientation)
869 inquery->height += child_requisition.height;
871 inquery->width += child_requisition.width;
874 /* report effective widget size */
875 inquery->width += item_area.width + 2 * border_width;
876 inquery->height += item_area.height + 2 * border_width;
880 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
881 GtkAllocation *allocation)
883 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
884 GtkToolItemGroupPrivate* priv = group->priv;
885 GtkRequisition child_requisition;
886 GtkAllocation child_allocation;
888 GtkRequisition item_size;
889 GtkAllocation item_area;
891 GtkOrientation orientation;
895 gint n_columns, n_rows = 1;
898 GtkTextDirection direction;
900 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
902 direction = gtk_widget_get_direction (widget);
904 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
907 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
909 child_allocation.x = border_width;
910 child_allocation.y = border_width;
912 /* place the header widget */
913 if (gtk_widget_get_visible (priv->header))
915 gtk_widget_get_preferred_size (priv->header,
916 &child_requisition, NULL);
918 if (GTK_ORIENTATION_VERTICAL == orientation)
920 child_allocation.width = allocation->width;
921 child_allocation.height = child_requisition.height;
925 child_allocation.width = child_requisition.width;
926 child_allocation.height = allocation->height;
928 if (GTK_TEXT_DIR_RTL == direction)
929 child_allocation.x = allocation->width - border_width - child_allocation.width;
932 gtk_widget_size_allocate (priv->header, &child_allocation);
934 if (GTK_ORIENTATION_VERTICAL == orientation)
935 child_allocation.y += child_allocation.height;
936 else if (GTK_TEXT_DIR_RTL != direction)
937 child_allocation.x += child_allocation.width;
939 child_allocation.x = border_width;
942 child_requisition.width = child_requisition.height = 0;
944 /* figure out the size of homogeneous items */
945 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
947 item_size.width = MAX (item_size.width, 1);
948 item_size.height = MAX (item_size.height, 1);
950 /* figure out the available columns and size of item_area */
951 if (GTK_ORIENTATION_VERTICAL == orientation)
953 item_size.width = MIN (item_size.width, allocation->width);
955 item_area.width = allocation->width - 2 * border_width;
956 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
958 n_columns = MAX (item_area.width / item_size.width, 1);
960 item_size.width = item_area.width / n_columns;
964 item_size.height = MIN (item_size.height, allocation->height);
966 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
967 item_area.height = allocation->height - 2 * border_width;
969 n_columns = MAX (item_area.width / item_size.width, 1);
970 n_rows = MAX (item_area.height / item_size.height, min_rows);
972 item_size.height = item_area.height / n_rows;
975 item_area.x = child_allocation.x;
976 item_area.y = child_allocation.y;
978 /* when expanded or in transition, place the tool items in a grid like layout */
979 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
981 gint col = 0, row = 0;
983 for (it = priv->children; it != NULL; it = it->next)
985 GtkToolItemGroupChild *child = it->data;
988 if (!gtk_tool_item_group_is_item_visible (group, child))
990 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
995 /* for non homogeneous widgets request the required size */
996 child_requisition.width = 0;
998 if (!child->homogeneous)
1000 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
1001 &child_requisition, NULL);
1002 child_requisition.width = MIN (child_requisition.width, item_area.width);
1005 /* select next row if at end of row */
1006 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1010 child_allocation.y += child_allocation.height;
1015 /* calculate the position and size of the item */
1016 if (!child->homogeneous)
1022 col_width = udiv (child_requisition.width, item_size.width);
1024 col_width = n_columns - col;
1026 width = col_width * item_size.width;
1028 if (GTK_TEXT_DIR_RTL == direction)
1029 col_child = (n_columns - col - col_width);
1033 child_allocation.x = item_area.x + col_child * item_size.width;
1034 child_allocation.width = width;
1038 child_allocation.x =
1039 (item_area.x + col_child * item_size.width +
1040 (width - child_requisition.width) / 2);
1041 child_allocation.width = child_requisition.width;
1048 if (GTK_TEXT_DIR_RTL == direction)
1049 col_child = (n_columns - col - 1);
1051 child_allocation.x = item_area.x + col_child * item_size.width;
1052 child_allocation.width = item_size.width;
1057 child_allocation.height = item_size.height;
1059 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1060 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1063 child_allocation.y += item_size.height;
1066 /* or just hide all items, when collapsed */
1070 for (it = priv->children; it != NULL; it = it->next)
1072 GtkToolItemGroupChild *child = it->data;
1074 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1080 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1081 GtkAllocation *allocation)
1083 gtk_tool_item_group_real_size_allocate (widget, allocation);
1085 if (gtk_widget_get_mapped (widget))
1086 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1090 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1094 GtkAdjustment *adjustment;
1095 GtkAllocation allocation, p_allocation;
1098 /* Find this group's parent widget in the focused widget's anchestry. */
1099 for (p = widget; p; p = gtk_widget_get_parent (p))
1102 p = gtk_widget_get_parent (p);
1106 if (GTK_IS_TOOL_PALETTE (p))
1108 /* Check that the focused widgets is fully visible within
1109 * the group's parent widget and make it visible otherwise. */
1111 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1117 gtk_widget_get_allocation (widget, &allocation);
1118 gtk_widget_get_allocation (p, &p_allocation);
1120 /* Handle vertical adjustment. */
1121 if (gtk_widget_translate_coordinates
1122 (widget, p, 0, 0, NULL, &y) && y < 0)
1124 y += gtk_adjustment_get_value (adjustment);
1125 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1127 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1128 y > p_allocation.height)
1130 y += gtk_adjustment_get_value (adjustment);
1131 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1135 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1141 gtk_widget_get_allocation (widget, &allocation);
1142 gtk_widget_get_allocation (p, &p_allocation);
1144 /* Handle horizontal adjustment. */
1145 if (gtk_widget_translate_coordinates
1146 (widget, p, 0, 0, &x, NULL) && x < 0)
1148 x += gtk_adjustment_get_value (adjustment);
1149 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1151 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1152 x > p_allocation.width)
1154 x += gtk_adjustment_get_value (adjustment);
1155 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1164 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1165 GtkWidget *toplevel)
1167 GtkToolItemGroupPrivate* priv = group->priv;
1169 if (toplevel != priv->toplevel)
1173 /* Disconnect focus tracking handler. */
1174 g_signal_handler_disconnect (priv->toplevel,
1175 priv->focus_set_id);
1177 priv->focus_set_id = 0;
1178 priv->toplevel = NULL;
1183 /* Install focus tracking handler. We connect to the window's
1184 * set-focus signal instead of connecting to the focus signal of
1187 * 1) Reduce the number of signal handlers used.
1188 * 2) Avoid special handling for group headers.
1189 * 3) Catch focus grabs not only for direct children,
1190 * but also for nested widgets.
1192 priv->focus_set_id =
1193 g_signal_connect (toplevel, "set-focus",
1194 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1197 priv->toplevel = toplevel;
1203 gtk_tool_item_group_realize (GtkWidget *widget)
1205 GtkAllocation allocation;
1206 GtkWidget *toplevel_window;
1208 GdkWindowAttr attributes;
1209 GdkDisplay *display;
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 display = gdk_window_get_display (window);
1240 if (gdk_display_supports_composite (display))
1241 gdk_window_set_composited (window, TRUE);
1243 gdk_window_set_user_data (window, widget);
1245 gtk_style_context_set_background (context, window);
1247 gtk_container_forall (GTK_CONTAINER (widget),
1248 (GtkCallback) gtk_widget_set_parent_window,
1251 gtk_widget_queue_resize_no_redraw (widget);
1253 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1254 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1259 gtk_tool_item_group_unrealize (GtkWidget *widget)
1261 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1262 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1266 gtk_tool_item_group_style_updated (GtkWidget *widget)
1268 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1269 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
1273 gtk_tool_item_group_add (GtkContainer *container,
1276 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1277 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1279 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1280 GTK_TOOL_ITEM (widget), -1);
1284 gtk_tool_item_group_remove (GtkContainer *container,
1287 GtkToolItemGroup *group;
1288 GtkToolItemGroupPrivate* priv;
1291 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1292 group = GTK_TOOL_ITEM_GROUP (container);
1295 for (it = priv->children; it != NULL; it = it->next)
1297 GtkToolItemGroupChild *child_info = it->data;
1299 if ((GtkWidget *)child_info->item == child)
1301 g_object_unref (child);
1302 gtk_widget_unparent (child);
1304 g_free (child_info);
1305 priv->children = g_list_delete_link (priv->children, it);
1307 gtk_widget_queue_resize (GTK_WIDGET (container));
1314 gtk_tool_item_group_forall (GtkContainer *container,
1316 GtkCallback callback,
1317 gpointer callback_data)
1319 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1320 GtkToolItemGroupPrivate* priv = group->priv;
1323 if (internals && priv->header)
1324 callback (priv->header, callback_data);
1326 children = priv->children;
1329 GtkToolItemGroupChild *child = children->data;
1330 children = children->next; /* store pointer before call to callback
1331 because the child pointer is invalid if the
1332 child->item is removed from the item group
1335 callback (GTK_WIDGET (child->item), callback_data);
1340 gtk_tool_item_group_child_type (GtkContainer *container)
1342 return GTK_TYPE_TOOL_ITEM;
1345 static GtkToolItemGroupChild *
1346 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1354 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1355 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1357 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1359 GtkToolItemGroupChild *child = it->data;
1361 if (child->item == item)
1377 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1379 gboolean *homogeneous,
1384 GtkToolItemGroupChild *child;
1386 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1387 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1389 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1394 *expand = child->expand;
1397 *homogeneous = child->homogeneous;
1400 *fill = child->fill;
1403 *new_row = child->new_row;
1407 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1409 gboolean homogeneous,
1414 GtkToolItemGroupChild *child;
1415 gboolean changed = FALSE;
1417 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1418 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1420 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1424 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1426 if (child->homogeneous != homogeneous)
1428 child->homogeneous = homogeneous;
1430 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1432 if (child->expand != expand)
1434 child->expand = expand;
1436 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1438 if (child->fill != fill)
1442 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1444 if (child->new_row != new_row)
1446 child->new_row = new_row;
1448 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1451 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1454 && gtk_widget_get_visible (GTK_WIDGET (group))
1455 && gtk_widget_get_visible (GTK_WIDGET (item)))
1456 gtk_widget_queue_resize (GTK_WIDGET (group));
1460 gtk_tool_item_group_set_child_property (GtkContainer *container,
1463 const GValue *value,
1466 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1467 GtkToolItem *item = GTK_TOOL_ITEM (child);
1468 gboolean homogeneous, expand, fill, new_row;
1470 if (prop_id != CHILD_PROP_POSITION)
1471 gtk_tool_item_group_get_item_packing (group, item,
1479 case CHILD_PROP_HOMOGENEOUS:
1480 gtk_tool_item_group_set_item_packing (group, item,
1481 g_value_get_boolean (value),
1487 case CHILD_PROP_EXPAND:
1488 gtk_tool_item_group_set_item_packing (group, item,
1490 g_value_get_boolean (value),
1495 case CHILD_PROP_FILL:
1496 gtk_tool_item_group_set_item_packing (group, item,
1499 g_value_get_boolean (value),
1503 case CHILD_PROP_NEW_ROW:
1504 gtk_tool_item_group_set_item_packing (group, item,
1508 g_value_get_boolean (value));
1511 case CHILD_PROP_POSITION:
1512 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1516 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1522 gtk_tool_item_group_get_child_property (GtkContainer *container,
1528 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1529 GtkToolItem *item = GTK_TOOL_ITEM (child);
1530 gboolean homogeneous, expand, fill, new_row;
1532 if (prop_id != CHILD_PROP_POSITION)
1533 gtk_tool_item_group_get_item_packing (group, item,
1541 case CHILD_PROP_HOMOGENEOUS:
1542 g_value_set_boolean (value, homogeneous);
1545 case CHILD_PROP_EXPAND:
1546 g_value_set_boolean (value, expand);
1549 case CHILD_PROP_FILL:
1550 g_value_set_boolean (value, fill);
1553 case CHILD_PROP_NEW_ROW:
1554 g_value_set_boolean (value, new_row);
1557 case CHILD_PROP_POSITION:
1558 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1562 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1568 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1570 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1571 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1572 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1574 oclass->set_property = gtk_tool_item_group_set_property;
1575 oclass->get_property = gtk_tool_item_group_get_property;
1576 oclass->finalize = gtk_tool_item_group_finalize;
1577 oclass->dispose = gtk_tool_item_group_dispose;
1579 wclass->get_preferred_width = gtk_tool_item_group_get_preferred_width;
1580 wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1581 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1582 wclass->realize = gtk_tool_item_group_realize;
1583 wclass->unrealize = gtk_tool_item_group_unrealize;
1584 wclass->style_updated = gtk_tool_item_group_style_updated;
1585 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1587 cclass->add = gtk_tool_item_group_add;
1588 cclass->remove = gtk_tool_item_group_remove;
1589 cclass->forall = gtk_tool_item_group_forall;
1590 cclass->child_type = gtk_tool_item_group_child_type;
1591 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1592 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1594 g_object_class_install_property (oclass, PROP_LABEL,
1595 g_param_spec_string ("label",
1597 P_("The human-readable title of this item group"),
1599 GTK_PARAM_READWRITE));
1601 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1602 g_param_spec_object ("label-widget",
1604 P_("A widget to display in place of the usual label"),
1606 GTK_PARAM_READWRITE));
1608 g_object_class_install_property (oclass, PROP_COLLAPSED,
1609 g_param_spec_boolean ("collapsed",
1611 P_("Whether the group has been collapsed and items are hidden"),
1613 GTK_PARAM_READWRITE));
1615 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1616 g_param_spec_enum ("ellipsize",
1618 P_("Ellipsize for item group headers"),
1619 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1620 GTK_PARAM_READWRITE));
1622 g_object_class_install_property (oclass, PROP_RELIEF,
1623 g_param_spec_enum ("header-relief",
1624 P_("Header Relief"),
1625 P_("Relief of the group header button"),
1626 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1627 GTK_PARAM_READWRITE));
1629 gtk_widget_class_install_style_property (wclass,
1630 g_param_spec_int ("expander-size",
1631 P_("Expander Size"),
1632 P_("Size of the expander arrow"),
1635 DEFAULT_EXPANDER_SIZE,
1636 GTK_PARAM_READABLE));
1638 gtk_widget_class_install_style_property (wclass,
1639 g_param_spec_int ("header-spacing",
1640 P_("Header Spacing"),
1641 P_("Spacing between expander arrow and caption"),
1644 DEFAULT_HEADER_SPACING,
1645 GTK_PARAM_READABLE));
1647 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1648 g_param_spec_boolean ("homogeneous",
1650 P_("Whether the item should be the same size as other homogeneous items"),
1652 GTK_PARAM_READWRITE));
1654 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1655 g_param_spec_boolean ("expand",
1657 P_("Whether the item should receive extra space when the group grows"),
1659 GTK_PARAM_READWRITE));
1661 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1662 g_param_spec_boolean ("fill",
1664 P_("Whether the item should fill the available space"),
1666 GTK_PARAM_READWRITE));
1668 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1669 g_param_spec_boolean ("new-row",
1671 P_("Whether the item should start a new row"),
1673 GTK_PARAM_READWRITE));
1675 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1676 g_param_spec_int ("position",
1678 P_("Position of the item within this group"),
1682 GTK_PARAM_READWRITE));
1684 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1688 * gtk_tool_item_group_new:
1689 * @label: the label of the new group
1691 * Creates a new tool item group with label @label.
1693 * Returns: a new #GtkToolItemGroup.
1698 gtk_tool_item_group_new (const gchar *label)
1700 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1704 * gtk_tool_item_group_set_label:
1705 * @group: a #GtkToolItemGroup
1706 * @label: the new human-readable label of of the group
1708 * Sets the label of the tool item group. The label is displayed in the header
1714 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1717 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1720 gtk_tool_item_group_set_label_widget (group, NULL);
1723 GtkWidget *child = gtk_label_new (label);
1724 gtk_widget_show (child);
1726 gtk_tool_item_group_set_label_widget (group, child);
1729 g_object_notify (G_OBJECT (group), "label");
1733 * gtk_tool_item_group_set_label_widget:
1734 * @group: a #GtkToolItemGroup
1735 * @label_widget: the widget to be displayed in place of the usual label
1737 * Sets the label of the tool item group.
1738 * The label widget is displayed in the header of the group, in place
1739 * of the usual label.
1744 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1745 GtkWidget *label_widget)
1747 GtkToolItemGroupPrivate* priv;
1748 GtkWidget *alignment;
1750 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1751 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1752 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1756 if (priv->label_widget == label_widget)
1759 alignment = gtk_tool_item_group_get_alignment (group);
1761 if (priv->label_widget)
1763 gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1764 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1769 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1771 priv->label_widget = label_widget;
1773 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1774 gtk_widget_queue_resize (GTK_WIDGET (group));
1776 /* Only show the header widget if the group has children: */
1777 if (label_widget && priv->children)
1778 gtk_widget_show (priv->header);
1780 gtk_widget_hide (priv->header);
1782 g_object_freeze_notify (G_OBJECT (group));
1783 g_object_notify (G_OBJECT (group), "label-widget");
1784 g_object_notify (G_OBJECT (group), "label");
1785 g_object_thaw_notify (G_OBJECT (group));
1789 * gtk_tool_item_group_set_header_relief:
1790 * @group: a #GtkToolItemGroup
1791 * @style: the #GtkReliefStyle
1793 * Set the button relief of the group header.
1794 * See gtk_button_set_relief() for details.
1799 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1800 GtkReliefStyle style)
1802 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1804 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1808 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1810 return (g_source_get_time (group->priv->animation_timeout) -
1811 group->priv->animation_start) / 1000;
1815 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1817 GtkToolItemGroupPrivate* priv = group->priv;
1818 GtkWidget *widget = GTK_WIDGET (group);
1820 if (gtk_widget_get_realized (priv->header))
1822 GtkAllocation alignment_allocation;
1823 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1826 /* Find the header button's arrow area... */
1827 gtk_widget_get_allocation (alignment, &alignment_allocation);
1828 area.x = alignment_allocation.x;
1829 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1830 area.height = priv->expander_size;
1831 area.width = priv->expander_size;
1833 /* ... and invalidated it to get it animated. */
1834 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1837 if (gtk_widget_get_realized (widget))
1839 GtkAllocation allocation;
1840 GtkWidget *parent = gtk_widget_get_parent (widget);
1841 int x, y, width, height;
1843 /* Find the tool item area button's arrow area... */
1844 gtk_widget_get_allocation (widget, &allocation);
1845 width = allocation.width;
1846 height = allocation.height;
1848 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1850 if (gtk_widget_get_visible (priv->header))
1852 GtkAllocation header_allocation;
1854 gtk_widget_get_allocation (priv->header, &header_allocation);
1855 height -= header_allocation.height;
1856 y += header_allocation.height;
1859 /* ... and invalidated it to get it animated. */
1860 gtk_widget_queue_draw_area (parent, x, y, width, height);
1865 gtk_tool_item_group_animation_cb (gpointer data)
1867 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1868 GtkToolItemGroupPrivate* priv = group->priv;
1869 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1872 GDK_THREADS_ENTER ();
1874 /* Enque this early to reduce number of expose events. */
1875 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1877 gtk_tool_item_group_force_expose (group);
1879 /* Finish animation when done. */
1880 if (timestamp >= ANIMATION_DURATION)
1881 priv->animation_timeout = NULL;
1883 retval = (priv->animation_timeout != NULL);
1885 GDK_THREADS_LEAVE ();
1891 * gtk_tool_item_group_set_collapsed:
1892 * @group: a #GtkToolItemGroup
1893 * @collapsed: whether the @group should be collapsed or expanded
1895 * Sets whether the @group should be collapsed or expanded.
1900 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1904 GtkToolItemGroupPrivate* priv;
1906 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1910 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1911 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1912 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1913 GTK_WIDGET (group));
1914 if (collapsed != priv->collapsed)
1916 GtkStyleContext *context;
1918 if (priv->animation)
1920 if (priv->animation_timeout)
1921 g_source_destroy (priv->animation_timeout);
1923 priv->animation_start = g_get_monotonic_time ();
1924 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1926 g_source_set_callback (priv->animation_timeout,
1927 gtk_tool_item_group_animation_cb,
1929 g_source_attach (priv->animation_timeout, NULL);
1931 context = gtk_widget_get_style_context (gtk_bin_get_child (GTK_BIN (priv->header)));
1933 gtk_style_context_save (context);
1934 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
1936 gtk_style_context_notify_state_change (context,
1937 gtk_widget_get_window (priv->header),
1938 GUINT_TO_POINTER (1),
1939 GTK_STATE_FLAG_ACTIVE,
1942 gtk_style_context_restore (context);
1945 gtk_tool_item_group_force_expose (group);
1947 priv->collapsed = collapsed;
1948 g_object_notify (G_OBJECT (group), "collapsed");
1953 * gtk_tool_item_group_set_ellipsize:
1954 * @group: a #GtkToolItemGroup
1955 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1957 * Sets the ellipsization mode which should be used by labels in @group.
1962 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1963 PangoEllipsizeMode ellipsize)
1965 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1967 if (ellipsize != group->priv->ellipsize)
1969 group->priv->ellipsize = ellipsize;
1970 gtk_tool_item_group_header_adjust_style (group);
1971 g_object_notify (G_OBJECT (group), "ellipsize");
1972 _gtk_tool_item_group_palette_reconfigured (group);
1977 * gtk_tool_item_group_get_label:
1978 * @group: a #GtkToolItemGroup
1980 * Gets the label of @group.
1982 * Returns: the label of @group. The label is an internal string of @group
1983 * and must not be modified. Note that %NULL is returned if a custom
1984 * label has been set with gtk_tool_item_group_set_label_widget()
1988 G_CONST_RETURN gchar*
1989 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1991 GtkToolItemGroupPrivate *priv;
1993 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1997 if (GTK_IS_LABEL (priv->label_widget))
1998 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2004 * gtk_tool_item_group_get_label_widget:
2005 * @group: a #GtkToolItemGroup
2007 * Gets the label widget of @group.
2008 * See gtk_tool_item_group_set_label_widget().
2010 * Returns: (transfer none): the label widget of @group
2015 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2017 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2019 return gtk_bin_get_child (GTK_BIN (alignment));
2023 * gtk_tool_item_group_get_collapsed:
2024 * @group: a GtkToolItemGroup
2026 * Gets whether @group is collapsed or expanded.
2028 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2033 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2035 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2037 return group->priv->collapsed;
2041 * gtk_tool_item_group_get_ellipsize:
2042 * @group: a #GtkToolItemGroup
2044 * Gets the ellipsization mode of @group.
2046 * Returns: the #PangoEllipsizeMode of @group
2051 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2053 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2055 return group->priv->ellipsize;
2059 * gtk_tool_item_group_get_header_relief:
2060 * @group: a #GtkToolItemGroup
2062 * Gets the relief mode of the header button of @group.
2064 * Returns: the #GtkReliefStyle
2069 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2071 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2073 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2077 * gtk_tool_item_group_insert:
2078 * @group: a #GtkToolItemGroup
2079 * @item: the #GtkToolItem to insert into @group
2080 * @position: the position of @item in @group, starting with 0.
2081 * The position -1 means end of list.
2083 * Inserts @item at @position in the list of children of @group.
2088 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2092 GtkWidget *parent, *child_widget;
2093 GtkToolItemGroupChild *child;
2095 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2096 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2097 g_return_if_fail (position >= -1);
2099 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2101 child = g_new (GtkToolItemGroupChild, 1);
2102 child->item = g_object_ref_sink (item);
2103 child->homogeneous = TRUE;
2104 child->expand = FALSE;
2106 child->new_row = FALSE;
2108 group->priv->children = g_list_insert (group->priv->children, child, position);
2110 if (GTK_IS_TOOL_PALETTE (parent))
2111 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2113 child_widget = gtk_bin_get_child (GTK_BIN (item));
2115 if (GTK_IS_BUTTON (child_widget))
2116 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2118 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2122 * gtk_tool_item_group_set_item_position:
2123 * @group: a #GtkToolItemGroup
2124 * @item: the #GtkToolItem to move to a new position, should
2125 * be a child of @group.
2126 * @position: the new position of @item in @group, starting with 0.
2127 * The position -1 means end of list.
2129 * Sets the position of @item in the list of children of @group.
2134 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2140 GtkToolItemGroupChild *child;
2141 GtkToolItemGroupPrivate* priv;
2143 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2144 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2145 g_return_if_fail (position >= -1);
2147 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2150 g_return_if_fail (child != NULL);
2152 if (position == old_position)
2155 priv->children = g_list_delete_link (priv->children, link);
2156 priv->children = g_list_insert (priv->children, child, position);
2158 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2159 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2160 gtk_widget_get_visible (GTK_WIDGET (item)))
2161 gtk_widget_queue_resize (GTK_WIDGET (group));
2165 * gtk_tool_item_group_get_item_position:
2166 * @group: a #GtkToolItemGroup
2167 * @item: a #GtkToolItem
2169 * Gets the position of @item in @group as index.
2171 * Returns: the index of @item in @group or -1 if @item is no child of @group
2176 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2181 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2182 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2184 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2191 * gtk_tool_item_group_get_n_items:
2192 * @group: a #GtkToolItemGroup
2194 * Gets the number of tool items in @group.
2196 * Returns: the number of tool items in @group
2201 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2203 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2205 return g_list_length (group->priv->children);
2209 * gtk_tool_item_group_get_nth_item:
2210 * @group: a #GtkToolItemGroup
2213 * Gets the tool item at @index in group.
2215 * Returns: (transfer none): the #GtkToolItem at index
2220 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2223 GtkToolItemGroupChild *child;
2225 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2227 child = g_list_nth_data (group->priv->children, index);
2229 return child != NULL ? child->item : NULL;
2233 * gtk_tool_item_group_get_drop_item:
2234 * @group: a #GtkToolItemGroup
2235 * @x: the x position
2236 * @y: the y position
2238 * Gets the tool item at position (x, y).
2240 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2245 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2249 GtkAllocation allocation;
2252 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2254 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2256 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2257 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2259 for (it = group->priv->children; it != NULL; it = it->next)
2261 GtkToolItemGroupChild *child = it->data;
2262 GtkToolItem *item = child->item;
2265 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2268 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2270 x0 = x - allocation.x;
2271 y0 = y - allocation.y;
2273 if (x0 >= 0 && x0 < allocation.width &&
2274 y0 >= 0 && y0 < allocation.height)
2282 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2283 GtkRequisition *item_size,
2284 gboolean homogeneous_only,
2285 gint *requested_rows)
2287 GtkRequisition child_requisition;
2290 gboolean new_row = TRUE;
2292 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2293 g_return_if_fail (NULL != item_size);
2295 item_size->width = item_size->height = 0;
2297 for (it = group->priv->children; it != NULL; it = it->next)
2299 GtkToolItemGroupChild *child = it->data;
2301 if (!gtk_tool_item_group_is_item_visible (group, child))
2304 if (child->new_row || new_row)
2310 if (!child->homogeneous && child->expand)
2313 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2314 &child_requisition, NULL);
2316 if (!homogeneous_only || child->homogeneous)
2317 item_size->width = MAX (item_size->width, child_requisition.width);
2318 item_size->height = MAX (item_size->height, child_requisition.height);
2322 *requested_rows = rows;
2326 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2329 GtkAllocation allocation;
2330 GtkWidget *widget = GTK_WIDGET (group);
2331 GtkToolItemGroupPrivate* priv = group->priv;
2333 gtk_widget_get_allocation (widget, &allocation);
2335 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2339 if (priv->animation_timeout)
2341 GtkAllocation header_allocation;
2342 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2343 cairo_pattern_t *mask;
2346 if (GTK_ORIENTATION_VERTICAL == orientation)
2347 v1 = allocation.height;
2349 v1 = allocation.width;
2353 gtk_widget_get_allocation (priv->header, &header_allocation);
2354 if (!gtk_widget_get_visible (priv->header))
2356 else if (GTK_ORIENTATION_VERTICAL == orientation)
2357 v0 = MAX (v0, header_allocation.height);
2359 v0 = MAX (v0, header_allocation.width);
2361 v1 = MIN (v0 + 256, v1);
2363 if (GTK_ORIENTATION_VERTICAL == orientation)
2368 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2375 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2378 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2379 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2380 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2381 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2382 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2384 cairo_mask (cr, mask);
2385 cairo_pattern_destroy (mask);
2392 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2397 GtkRequisition requisition;
2398 GtkToolItemGroupPrivate* priv = group->priv;
2400 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2401 &requisition, NULL);
2403 if (!priv->collapsed || priv->animation_timeout)
2405 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2406 GtkRequisition inquery;
2409 allocation.width = limit;
2411 allocation.height = limit;
2413 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2414 &allocation, &inquery);
2417 inquery.height -= requisition.height;
2419 inquery.width -= requisition.width;
2421 if (priv->animation_timeout && animation)
2423 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2425 timestamp = MIN (timestamp, ANIMATION_DURATION);
2427 if (priv->collapsed)
2428 timestamp = ANIMATION_DURATION - timestamp;
2432 inquery.height *= timestamp;
2433 inquery.height /= ANIMATION_DURATION;
2437 inquery.width *= timestamp;
2438 inquery.width /= ANIMATION_DURATION;
2443 requisition.height += inquery.height;
2445 requisition.width += inquery.width;
2448 return (vertical ? requisition.height : requisition.width);
2452 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2455 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2459 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2462 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2466 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2469 if (GTK_IS_TOOL_ITEM (child))
2470 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2475 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2477 gtk_container_foreach (GTK_CONTAINER (group),
2478 gtk_tool_palette_reconfigured_foreach_item,
2481 gtk_tool_item_group_header_adjust_style (group);