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
25 #include "gtktoolpaletteprivate.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;
87 GtkExpanderStyle expander_style;
90 PangoEllipsizeMode ellipsize;
95 GtkSettings *settings;
96 gulong settings_connection;
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 GtkExpanderStyle expander_style;
270 GtkOrientation orientation;
271 gint x, y, width, height;
272 GtkTextDirection direction;
274 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
275 expander_style = priv->expander_style;
276 direction = gtk_widget_get_direction (widget);
277 width = gtk_widget_get_allocated_width (widget);
278 height = gtk_widget_get_allocated_height (widget);
280 if (GTK_ORIENTATION_VERTICAL == orientation)
282 if (GTK_TEXT_DIR_RTL == direction)
283 x = width - priv->expander_size / 2;
285 x = priv->expander_size / 2;
292 y = priv->expander_size / 2;
294 /* Unfortunatly gtk_paint_expander() doesn't support rotated drawing
295 * modes. Luckily the following shady arithmetics produce the desired
297 expander_style = GTK_EXPANDER_EXPANDED - expander_style;
300 gtk_paint_expander (gtk_widget_get_style (widget),
302 gtk_widget_get_state (priv->header),
304 "tool-palette-header", x, y,
311 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
314 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
315 GtkToolItemGroupPrivate* priv = group->priv;
316 GtkWidget *parent = gtk_widget_get_parent (data);
318 if (priv->collapsed ||
319 !GTK_IS_TOOL_PALETTE (parent) ||
320 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
321 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
325 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
327 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
328 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
329 GtkWidget *widget = GTK_WIDGET (group);
330 GtkToolItemGroupPrivate* priv = group->priv;
332 GtkTextDirection direction = gtk_widget_get_direction (widget);
334 gtk_widget_style_get (widget,
335 "header-spacing", &(priv->header_spacing),
336 "expander-size", &(priv->expander_size),
339 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
341 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
343 case GTK_ORIENTATION_HORIZONTAL:
344 dy = priv->header_spacing + priv->expander_size;
346 if (GTK_IS_LABEL (label_widget))
348 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
349 if (GTK_TEXT_DIR_RTL == direction)
350 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
352 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
356 case GTK_ORIENTATION_VERTICAL:
357 dx = priv->header_spacing + priv->expander_size;
359 if (GTK_IS_LABEL (label_widget))
361 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
362 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
367 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
371 gtk_tool_item_group_init (GtkToolItemGroup *group)
373 GtkWidget *alignment;
374 GtkToolItemGroupPrivate* priv;
376 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
378 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
379 GTK_TYPE_TOOL_ITEM_GROUP,
380 GtkToolItemGroupPrivate);
382 priv->children = NULL;
383 priv->header_spacing = DEFAULT_HEADER_SPACING;
384 priv->expander_size = DEFAULT_EXPANDER_SIZE;
385 priv->expander_style = GTK_EXPANDER_EXPANDED;
387 priv->label_widget = gtk_label_new (NULL);
388 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
389 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
390 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
391 gtk_widget_show_all (alignment);
393 gtk_widget_push_composite_child ();
394 priv->header = gtk_button_new ();
395 gtk_widget_set_composite_name (priv->header, "header");
396 gtk_widget_pop_composite_child ();
398 g_object_ref_sink (priv->header);
399 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
400 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
401 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
403 gtk_tool_item_group_header_adjust_style (group);
405 g_signal_connect_after (alignment, "draw",
406 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
409 g_signal_connect (priv->header, "clicked",
410 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
415 gtk_tool_item_group_set_property (GObject *object,
420 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
425 gtk_tool_item_group_set_label (group, g_value_get_string (value));
428 case PROP_LABEL_WIDGET:
429 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
433 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
437 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
441 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 gtk_tool_item_group_get_property (GObject *object,
456 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
461 g_value_set_string (value, gtk_tool_item_group_get_label (group));
464 case PROP_LABEL_WIDGET:
465 g_value_set_object (value,
466 gtk_tool_item_group_get_label_widget (group));
470 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
474 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
478 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gtk_tool_item_group_finalize (GObject *object)
490 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
492 if (group->priv->children)
494 g_list_free (group->priv->children);
495 group->priv->children = NULL;
498 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
502 gtk_tool_item_group_dispose (GObject *object)
504 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
505 GtkToolItemGroupPrivate* priv = group->priv;
509 /* disconnect focus tracking handler */
510 g_signal_handler_disconnect (priv->toplevel,
513 priv->focus_set_id = 0;
514 priv->toplevel = NULL;
517 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
521 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
522 GtkRequisition *item_size,
523 gboolean homogeneous_only,
524 gint *requested_rows)
526 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
528 if (GTK_IS_TOOL_PALETTE (parent))
529 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
531 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
535 gtk_tool_item_group_size_request (GtkWidget *widget,
536 GtkRequisition *requisition)
538 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
539 GtkToolItemGroupPrivate* priv = group->priv;
540 GtkOrientation orientation;
541 GtkRequisition item_size;
545 if (priv->children && gtk_tool_item_group_get_label_widget (group))
547 gtk_widget_get_preferred_size (priv->header,
549 gtk_widget_show (priv->header);
553 requisition->width = requisition->height = 0;
554 gtk_widget_hide (priv->header);
557 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
559 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
561 if (GTK_ORIENTATION_VERTICAL == orientation)
562 requisition->width = MAX (requisition->width, item_size.width);
564 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
566 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
567 requisition->width += border_width * 2;
568 requisition->height += border_width * 2;
572 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
576 GtkRequisition requisition;
578 gtk_tool_item_group_size_request (widget, &requisition);
580 *minimum = *natural = requisition.width;
584 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
588 GtkRequisition requisition;
590 gtk_tool_item_group_size_request (widget, &requisition);
592 *minimum = *natural = requisition.height;
597 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
598 GtkToolItemGroupChild *child)
600 GtkToolbarStyle style;
601 GtkOrientation orientation;
603 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
604 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
606 /* horizontal tool palettes with text style support only homogeneous items */
607 if (!child->homogeneous &&
608 GTK_ORIENTATION_HORIZONTAL == orientation &&
609 GTK_TOOLBAR_TEXT == style)
613 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
614 (GTK_ORIENTATION_VERTICAL == orientation ?
615 gtk_tool_item_get_visible_vertical (child->item) :
616 gtk_tool_item_get_visible_horizontal (child->item));
619 static inline unsigned
623 return (x + y - 1) / y;
627 gtk_tool_item_group_real_size_query (GtkWidget *widget,
628 GtkAllocation *allocation,
629 GtkRequisition *inquery)
631 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
632 GtkToolItemGroupPrivate* priv = group->priv;
634 GtkRequisition item_size;
635 GtkAllocation item_area;
637 GtkOrientation orientation;
638 GtkToolbarStyle style;
643 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
644 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
645 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
647 /* figure out the size of homogeneous items */
648 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
650 if (GTK_ORIENTATION_VERTICAL == orientation)
651 item_size.width = MIN (item_size.width, allocation->width);
653 item_size.height = MIN (item_size.height, allocation->height);
655 item_size.width = MAX (item_size.width, 1);
656 item_size.height = MAX (item_size.height, 1);
659 item_area.height = 0;
661 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
662 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
668 if (GTK_ORIENTATION_VERTICAL == orientation)
670 gboolean new_row = FALSE;
674 item_area.width = allocation->width - 2 * border_width;
675 n_columns = MAX (item_area.width / item_size.width, 1);
677 /* calculate required rows for n_columns columns */
678 for (it = priv->children; it != NULL; it = it->next)
680 GtkToolItemGroupChild *child = it->data;
682 if (!gtk_tool_item_group_is_item_visible (group, child))
685 if (new_row || child->new_row)
695 if (child->homogeneous)
698 if (col >= n_columns)
703 GtkRequisition req = {0, 0};
706 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
709 width = udiv (req.width, item_size.width);
717 if (col >= n_columns)
725 guint *row_min_width;
727 gboolean new_row = TRUE;
728 guint col = 0, min_col, max_col = 0, all_items = 0;
731 item_area.height = allocation->height - 2 * border_width;
732 n_rows = MAX (item_area.height / item_size.height, min_rows);
734 row_min_width = g_new0 (guint, n_rows);
736 /* calculate minimal and maximal required cols and minimal required rows */
737 for (it = priv->children; it != NULL; it = it->next)
739 GtkToolItemGroupChild *child = it->data;
741 if (!gtk_tool_item_group_is_item_visible (group, child))
744 if (new_row || child->new_row)
749 row_min_width[row] = 1;
755 if (child->homogeneous)
762 GtkRequisition req = {0, 0};
765 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
768 width = udiv (req.width, item_size.width);
773 row_min_width[row] = MAX (row_min_width[row], width);
776 max_col = MAX (max_col, col);
779 /* calculate minimal required cols */
780 min_col = udiv (all_items, n_rows);
782 for (i = 0; i <= row; i++)
784 min_col = MAX (min_col, row_min_width[i]);
787 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
788 for (n_columns = min_col; n_columns < max_col; n_columns ++)
792 /* calculate required rows for n_columns columns */
793 for (it = priv->children; it != NULL; it = it->next)
795 GtkToolItemGroupChild *child = it->data;
797 if (!gtk_tool_item_group_is_item_visible (group, child))
800 if (new_row || child->new_row)
810 if (child->homogeneous)
813 if (col >= n_columns)
818 GtkRequisition req = {0, 0};
821 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
824 width = udiv (req.width, item_size.width);
832 if (col >= n_columns)
842 item_area.width = item_size.width * n_columns;
843 item_area.height = item_size.height * n_rows;
849 /* figure out header widget size */
850 if (gtk_widget_get_visible (priv->header))
852 GtkRequisition child_requisition;
854 gtk_widget_get_preferred_size (priv->header,
855 &child_requisition, NULL);
857 if (GTK_ORIENTATION_VERTICAL == orientation)
858 inquery->height += child_requisition.height;
860 inquery->width += child_requisition.width;
863 /* report effective widget size */
864 inquery->width += item_area.width + 2 * border_width;
865 inquery->height += item_area.height + 2 * border_width;
869 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
870 GtkAllocation *allocation)
872 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
873 GtkToolItemGroupPrivate* priv = group->priv;
874 GtkRequisition child_requisition;
875 GtkAllocation child_allocation;
877 GtkRequisition item_size;
878 GtkAllocation item_area;
880 GtkOrientation orientation;
881 GtkToolbarStyle style;
885 gint n_columns, n_rows = 1;
888 GtkTextDirection direction;
890 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
892 direction = gtk_widget_get_direction (widget);
894 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
895 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
898 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
900 child_allocation.x = border_width;
901 child_allocation.y = border_width;
903 /* place the header widget */
904 if (gtk_widget_get_visible (priv->header))
906 gtk_widget_get_preferred_size (priv->header,
907 &child_requisition, NULL);
909 if (GTK_ORIENTATION_VERTICAL == orientation)
911 child_allocation.width = allocation->width;
912 child_allocation.height = child_requisition.height;
916 child_allocation.width = child_requisition.width;
917 child_allocation.height = allocation->height;
919 if (GTK_TEXT_DIR_RTL == direction)
920 child_allocation.x = allocation->width - border_width - child_allocation.width;
923 gtk_widget_size_allocate (priv->header, &child_allocation);
925 if (GTK_ORIENTATION_VERTICAL == orientation)
926 child_allocation.y += child_allocation.height;
927 else if (GTK_TEXT_DIR_RTL != direction)
928 child_allocation.x += child_allocation.width;
930 child_allocation.x = border_width;
933 child_requisition.width = child_requisition.height = 0;
935 /* figure out the size of homogeneous items */
936 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
938 item_size.width = MAX (item_size.width, 1);
939 item_size.height = MAX (item_size.height, 1);
941 /* figure out the available columns and size of item_area */
942 if (GTK_ORIENTATION_VERTICAL == orientation)
944 item_size.width = MIN (item_size.width, allocation->width);
946 item_area.width = allocation->width - 2 * border_width;
947 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
949 n_columns = MAX (item_area.width / item_size.width, 1);
951 item_size.width = item_area.width / n_columns;
955 item_size.height = MIN (item_size.height, allocation->height);
957 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
958 item_area.height = allocation->height - 2 * border_width;
960 n_columns = MAX (item_area.width / item_size.width, 1);
961 n_rows = MAX (item_area.height / item_size.height, min_rows);
963 item_size.height = item_area.height / n_rows;
966 item_area.x = child_allocation.x;
967 item_area.y = child_allocation.y;
969 /* when expanded or in transition, place the tool items in a grid like layout */
970 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
972 gint col = 0, row = 0;
974 for (it = priv->children; it != NULL; it = it->next)
976 GtkToolItemGroupChild *child = it->data;
979 if (!gtk_tool_item_group_is_item_visible (group, child))
981 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
986 /* for non homogeneous widgets request the required size */
987 child_requisition.width = 0;
989 if (!child->homogeneous)
991 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
992 &child_requisition, NULL);
993 child_requisition.width = MIN (child_requisition.width, item_area.width);
996 /* select next row if at end of row */
997 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1001 child_allocation.y += child_allocation.height;
1006 /* calculate the position and size of the item */
1007 if (!child->homogeneous)
1013 col_width = udiv (child_requisition.width, item_size.width);
1015 col_width = n_columns - col;
1017 width = col_width * item_size.width;
1019 if (GTK_TEXT_DIR_RTL == direction)
1020 col_child = (n_columns - col - col_width);
1024 child_allocation.x = item_area.x + col_child * item_size.width;
1025 child_allocation.width = width;
1029 child_allocation.x =
1030 (item_area.x + col_child * item_size.width +
1031 (width - child_requisition.width) / 2);
1032 child_allocation.width = child_requisition.width;
1039 if (GTK_TEXT_DIR_RTL == direction)
1040 col_child = (n_columns - col - 1);
1042 child_allocation.x = item_area.x + col_child * item_size.width;
1043 child_allocation.width = item_size.width;
1048 child_allocation.height = item_size.height;
1050 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1051 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1054 child_allocation.y += item_size.height;
1057 /* or just hide all items, when collapsed */
1061 for (it = priv->children; it != NULL; it = it->next)
1063 GtkToolItemGroupChild *child = it->data;
1065 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1071 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1072 GtkAllocation *allocation)
1074 gtk_tool_item_group_real_size_allocate (widget, allocation);
1076 if (gtk_widget_get_mapped (widget))
1077 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1081 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1085 GtkAdjustment *adjustment;
1086 GtkAllocation allocation, p_allocation;
1089 /* Find this group's parent widget in the focused widget's anchestry. */
1090 for (p = widget; p; p = gtk_widget_get_parent (p))
1093 p = gtk_widget_get_parent (p);
1097 if (GTK_IS_TOOL_PALETTE (p))
1099 /* Check that the focused widgets is fully visible within
1100 * the group's parent widget and make it visible otherwise. */
1102 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1108 gtk_widget_get_allocation (widget, &allocation);
1109 gtk_widget_get_allocation (p, &p_allocation);
1111 /* Handle vertical adjustment. */
1112 if (gtk_widget_translate_coordinates
1113 (widget, p, 0, 0, NULL, &y) && y < 0)
1115 y += adjustment->value;
1116 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1118 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1119 y > p_allocation.height)
1121 y += adjustment->value;
1122 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1126 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1132 gtk_widget_get_allocation (widget, &allocation);
1133 gtk_widget_get_allocation (p, &p_allocation);
1135 /* Handle horizontal adjustment. */
1136 if (gtk_widget_translate_coordinates
1137 (widget, p, 0, 0, &x, NULL) && x < 0)
1139 x += adjustment->value;
1140 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1142 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1143 x > p_allocation.width)
1145 x += adjustment->value;
1146 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1155 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1156 GtkWidget *toplevel)
1158 GtkToolItemGroupPrivate* priv = group->priv;
1160 if (toplevel != priv->toplevel)
1164 /* Disconnect focus tracking handler. */
1165 g_signal_handler_disconnect (priv->toplevel,
1166 priv->focus_set_id);
1168 priv->focus_set_id = 0;
1169 priv->toplevel = NULL;
1174 /* Install focus tracking handler. We connect to the window's
1175 * set-focus signal instead of connecting to the focus signal of
1178 * 1) Reduce the number of signal handlers used.
1179 * 2) Avoid special handling for group headers.
1180 * 3) Catch focus grabs not only for direct children,
1181 * but also for nested widgets.
1183 priv->focus_set_id =
1184 g_signal_connect (toplevel, "set-focus",
1185 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1188 priv->toplevel = toplevel;
1194 gtk_tool_item_group_realize (GtkWidget *widget)
1196 GtkAllocation allocation;
1197 GtkWidget *toplevel_window;
1199 GdkWindowAttr attributes;
1200 GdkDisplay *display;
1201 gint attributes_mask;
1204 gtk_widget_set_realized (widget, TRUE);
1206 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1208 gtk_widget_get_allocation (widget, &allocation);
1210 attributes.window_type = GDK_WINDOW_CHILD;
1211 attributes.x = allocation.x + border_width;
1212 attributes.y = allocation.y + border_width;
1213 attributes.width = allocation.width - border_width * 2;
1214 attributes.height = allocation.height - border_width * 2;
1215 attributes.wclass = GDK_INPUT_OUTPUT;
1216 attributes.visual = gtk_widget_get_visual (widget);
1217 attributes.event_mask = gtk_widget_get_events (widget)
1218 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1219 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1220 | GDK_BUTTON_MOTION_MASK;
1221 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1223 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1224 &attributes, attributes_mask);
1225 gtk_widget_set_window (widget, window);
1227 display = gdk_window_get_display (window);
1229 if (gdk_display_supports_composite (display))
1230 gdk_window_set_composited (window, TRUE);
1232 gdk_window_set_user_data (window, widget);
1234 gtk_widget_style_attach (widget);
1235 gtk_style_set_background (gtk_widget_get_style (widget),
1236 window, GTK_STATE_NORMAL);
1238 gtk_container_forall (GTK_CONTAINER (widget),
1239 (GtkCallback) gtk_widget_set_parent_window,
1242 gtk_widget_queue_resize_no_redraw (widget);
1244 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1245 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1250 gtk_tool_item_group_unrealize (GtkWidget *widget)
1252 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1253 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1257 gtk_tool_item_group_style_set (GtkWidget *widget,
1258 GtkStyle *previous_style)
1260 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1261 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1265 gtk_tool_item_group_add (GtkContainer *container,
1268 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1269 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1271 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1272 GTK_TOOL_ITEM (widget), -1);
1276 gtk_tool_item_group_remove (GtkContainer *container,
1279 GtkToolItemGroup *group;
1280 GtkToolItemGroupPrivate* priv;
1283 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1284 group = GTK_TOOL_ITEM_GROUP (container);
1287 for (it = priv->children; it != NULL; it = it->next)
1289 GtkToolItemGroupChild *child_info = it->data;
1291 if ((GtkWidget *)child_info->item == child)
1293 g_object_unref (child);
1294 gtk_widget_unparent (child);
1296 g_free (child_info);
1297 priv->children = g_list_delete_link (priv->children, it);
1299 gtk_widget_queue_resize (GTK_WIDGET (container));
1306 gtk_tool_item_group_forall (GtkContainer *container,
1308 GtkCallback callback,
1309 gpointer callback_data)
1311 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1312 GtkToolItemGroupPrivate* priv = group->priv;
1315 if (internals && priv->header)
1316 callback (priv->header, callback_data);
1318 children = priv->children;
1321 GtkToolItemGroupChild *child = children->data;
1322 children = children->next; /* store pointer before call to callback
1323 because the child pointer is invalid if the
1324 child->item is removed from the item group
1327 callback (GTK_WIDGET (child->item), callback_data);
1332 gtk_tool_item_group_child_type (GtkContainer *container)
1334 return GTK_TYPE_TOOL_ITEM;
1337 static GtkToolItemGroupChild *
1338 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1346 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1347 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1349 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1351 GtkToolItemGroupChild *child = it->data;
1353 if (child->item == item)
1369 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1371 gboolean *homogeneous,
1376 GtkToolItemGroupChild *child;
1378 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1379 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1381 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1386 *expand = child->expand;
1389 *homogeneous = child->homogeneous;
1392 *fill = child->fill;
1395 *new_row = child->new_row;
1399 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1401 gboolean homogeneous,
1406 GtkToolItemGroupChild *child;
1407 gboolean changed = FALSE;
1409 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1410 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1412 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1416 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1418 if (child->homogeneous != homogeneous)
1420 child->homogeneous = homogeneous;
1422 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1424 if (child->expand != expand)
1426 child->expand = expand;
1428 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1430 if (child->fill != fill)
1434 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1436 if (child->new_row != new_row)
1438 child->new_row = new_row;
1440 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1443 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1446 && gtk_widget_get_visible (GTK_WIDGET (group))
1447 && gtk_widget_get_visible (GTK_WIDGET (item)))
1448 gtk_widget_queue_resize (GTK_WIDGET (group));
1452 gtk_tool_item_group_set_child_property (GtkContainer *container,
1455 const GValue *value,
1458 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1459 GtkToolItem *item = GTK_TOOL_ITEM (child);
1460 gboolean homogeneous, expand, fill, new_row;
1462 if (prop_id != CHILD_PROP_POSITION)
1463 gtk_tool_item_group_get_item_packing (group, item,
1471 case CHILD_PROP_HOMOGENEOUS:
1472 gtk_tool_item_group_set_item_packing (group, item,
1473 g_value_get_boolean (value),
1479 case CHILD_PROP_EXPAND:
1480 gtk_tool_item_group_set_item_packing (group, item,
1482 g_value_get_boolean (value),
1487 case CHILD_PROP_FILL:
1488 gtk_tool_item_group_set_item_packing (group, item,
1491 g_value_get_boolean (value),
1495 case CHILD_PROP_NEW_ROW:
1496 gtk_tool_item_group_set_item_packing (group, item,
1500 g_value_get_boolean (value));
1503 case CHILD_PROP_POSITION:
1504 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1508 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1514 gtk_tool_item_group_get_child_property (GtkContainer *container,
1520 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1521 GtkToolItem *item = GTK_TOOL_ITEM (child);
1522 gboolean homogeneous, expand, fill, new_row;
1524 if (prop_id != CHILD_PROP_POSITION)
1525 gtk_tool_item_group_get_item_packing (group, item,
1533 case CHILD_PROP_HOMOGENEOUS:
1534 g_value_set_boolean (value, homogeneous);
1537 case CHILD_PROP_EXPAND:
1538 g_value_set_boolean (value, expand);
1541 case CHILD_PROP_FILL:
1542 g_value_set_boolean (value, fill);
1545 case CHILD_PROP_NEW_ROW:
1546 g_value_set_boolean (value, new_row);
1549 case CHILD_PROP_POSITION:
1550 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1554 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1560 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1562 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1563 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1564 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1566 oclass->set_property = gtk_tool_item_group_set_property;
1567 oclass->get_property = gtk_tool_item_group_get_property;
1568 oclass->finalize = gtk_tool_item_group_finalize;
1569 oclass->dispose = gtk_tool_item_group_dispose;
1571 wclass->get_preferred_width = gtk_tool_item_group_get_preferred_width;
1572 wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1573 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1574 wclass->realize = gtk_tool_item_group_realize;
1575 wclass->unrealize = gtk_tool_item_group_unrealize;
1576 wclass->style_set = gtk_tool_item_group_style_set;
1577 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1579 cclass->add = gtk_tool_item_group_add;
1580 cclass->remove = gtk_tool_item_group_remove;
1581 cclass->forall = gtk_tool_item_group_forall;
1582 cclass->child_type = gtk_tool_item_group_child_type;
1583 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1584 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1586 g_object_class_install_property (oclass, PROP_LABEL,
1587 g_param_spec_string ("label",
1589 P_("The human-readable title of this item group"),
1591 GTK_PARAM_READWRITE));
1593 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1594 g_param_spec_object ("label-widget",
1596 P_("A widget to display in place of the usual label"),
1598 GTK_PARAM_READWRITE));
1600 g_object_class_install_property (oclass, PROP_COLLAPSED,
1601 g_param_spec_boolean ("collapsed",
1603 P_("Whether the group has been collapsed and items are hidden"),
1605 GTK_PARAM_READWRITE));
1607 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1608 g_param_spec_enum ("ellipsize",
1610 P_("Ellipsize for item group headers"),
1611 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1612 GTK_PARAM_READWRITE));
1614 g_object_class_install_property (oclass, PROP_RELIEF,
1615 g_param_spec_enum ("header-relief",
1616 P_("Header Relief"),
1617 P_("Relief of the group header button"),
1618 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1619 GTK_PARAM_READWRITE));
1621 gtk_widget_class_install_style_property (wclass,
1622 g_param_spec_int ("expander-size",
1623 P_("Expander Size"),
1624 P_("Size of the expander arrow"),
1627 DEFAULT_EXPANDER_SIZE,
1628 GTK_PARAM_READABLE));
1630 gtk_widget_class_install_style_property (wclass,
1631 g_param_spec_int ("header-spacing",
1632 P_("Header Spacing"),
1633 P_("Spacing between expander arrow and caption"),
1636 DEFAULT_HEADER_SPACING,
1637 GTK_PARAM_READABLE));
1639 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1640 g_param_spec_boolean ("homogeneous",
1642 P_("Whether the item should be the same size as other homogeneous items"),
1644 GTK_PARAM_READWRITE));
1646 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1647 g_param_spec_boolean ("expand",
1649 P_("Whether the item should receive extra space when the group grows"),
1651 GTK_PARAM_READWRITE));
1653 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1654 g_param_spec_boolean ("fill",
1656 P_("Whether the item should fill the available space"),
1658 GTK_PARAM_READWRITE));
1660 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1661 g_param_spec_boolean ("new-row",
1663 P_("Whether the item should start a new row"),
1665 GTK_PARAM_READWRITE));
1667 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1668 g_param_spec_int ("position",
1670 P_("Position of the item within this group"),
1674 GTK_PARAM_READWRITE));
1676 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1680 * gtk_tool_item_group_new:
1681 * @label: the label of the new group
1683 * Creates a new tool item group with label @label.
1685 * Returns: a new #GtkToolItemGroup.
1690 gtk_tool_item_group_new (const gchar *label)
1692 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1696 * gtk_tool_item_group_set_label:
1697 * @group: a #GtkToolItemGroup
1698 * @label: the new human-readable label of of the group
1700 * Sets the label of the tool item group. The label is displayed in the header
1706 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1709 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1712 gtk_tool_item_group_set_label_widget (group, NULL);
1715 GtkWidget *child = gtk_label_new (label);
1716 gtk_widget_show (child);
1718 gtk_tool_item_group_set_label_widget (group, child);
1721 g_object_notify (G_OBJECT (group), "label");
1725 * gtk_tool_item_group_set_label_widget:
1726 * @group: a #GtkToolItemGroup
1727 * @label_widget: the widget to be displayed in place of the usual label
1729 * Sets the label of the tool item group.
1730 * The label widget is displayed in the header of the group, in place
1731 * of the usual label.
1736 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1737 GtkWidget *label_widget)
1739 GtkToolItemGroupPrivate* priv;
1740 GtkWidget *alignment;
1742 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1743 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1744 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1748 if (priv->label_widget == label_widget)
1751 alignment = gtk_tool_item_group_get_alignment (group);
1753 if (priv->label_widget)
1755 gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1756 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1761 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1763 priv->label_widget = label_widget;
1765 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1766 gtk_widget_queue_resize (GTK_WIDGET (group));
1768 /* Only show the header widget if the group has children: */
1769 if (label_widget && priv->children)
1770 gtk_widget_show (priv->header);
1772 gtk_widget_hide (priv->header);
1774 g_object_freeze_notify (G_OBJECT (group));
1775 g_object_notify (G_OBJECT (group), "label-widget");
1776 g_object_notify (G_OBJECT (group), "label");
1777 g_object_thaw_notify (G_OBJECT (group));
1781 * gtk_tool_item_group_set_header_relief:
1782 * @group: a #GtkToolItemGroup
1783 * @style: the #GtkReliefStyle
1785 * Set the button relief of the group header.
1786 * See gtk_button_set_relief() for details.
1791 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1792 GtkReliefStyle style)
1794 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1796 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1800 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1802 return (g_source_get_time (group->priv->animation_timeout) -
1803 group->priv->animation_start) / 1000;
1807 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1809 GtkToolItemGroupPrivate* priv = group->priv;
1810 GtkWidget *widget = GTK_WIDGET (group);
1812 if (gtk_widget_get_realized (priv->header))
1814 GtkAllocation alignment_allocation;
1815 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1818 /* Find the header button's arrow area... */
1819 gtk_widget_get_allocation (alignment, &alignment_allocation);
1820 area.x = alignment_allocation.x;
1821 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1822 area.height = priv->expander_size;
1823 area.width = priv->expander_size;
1825 /* ... and invalidated it to get it animated. */
1826 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1829 if (gtk_widget_get_realized (widget))
1831 GtkAllocation allocation;
1832 GtkWidget *parent = gtk_widget_get_parent (widget);
1833 int x, y, width, height;
1835 /* Find the tool item area button's arrow area... */
1836 gtk_widget_get_allocation (widget, &allocation);
1837 width = allocation.width;
1838 height = allocation.height;
1840 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1842 if (gtk_widget_get_visible (priv->header))
1844 GtkAllocation header_allocation;
1846 gtk_widget_get_allocation (priv->header, &header_allocation);
1847 height -= header_allocation.height;
1848 y += header_allocation.height;
1851 /* ... and invalidated it to get it animated. */
1852 gtk_widget_queue_draw_area (parent, x, y, width, height);
1857 gtk_tool_item_group_animation_cb (gpointer data)
1859 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1860 GtkToolItemGroupPrivate* priv = group->priv;
1861 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1864 GDK_THREADS_ENTER ();
1866 /* Enque this early to reduce number of expose events. */
1867 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1869 /* Figure out current style of the expander arrow. */
1870 if (priv->collapsed)
1872 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1873 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1875 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1879 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1880 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1882 priv->expander_style = GTK_EXPANDER_EXPANDED;
1885 gtk_tool_item_group_force_expose (group);
1887 /* Finish animation when done. */
1888 if (timestamp >= ANIMATION_DURATION)
1889 priv->animation_timeout = NULL;
1891 retval = (priv->animation_timeout != NULL);
1893 GDK_THREADS_LEAVE ();
1899 * gtk_tool_item_group_set_collapsed:
1900 * @group: a #GtkToolItemGroup
1901 * @collapsed: whether the @group should be collapsed or expanded
1903 * Sets whether the @group should be collapsed or expanded.
1908 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1912 GtkToolItemGroupPrivate* priv;
1914 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1918 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1919 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1920 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1921 GTK_WIDGET (group));
1922 if (collapsed != priv->collapsed)
1924 if (priv->animation)
1926 if (priv->animation_timeout)
1927 g_source_destroy (priv->animation_timeout);
1929 priv->animation_start = g_get_monotonic_time ();
1930 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1932 g_source_set_callback (priv->animation_timeout,
1933 gtk_tool_item_group_animation_cb,
1936 g_source_attach (priv->animation_timeout, NULL);
1940 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1941 gtk_tool_item_group_force_expose (group);
1944 priv->collapsed = collapsed;
1945 g_object_notify (G_OBJECT (group), "collapsed");
1950 * gtk_tool_item_group_set_ellipsize:
1951 * @group: a #GtkToolItemGroup
1952 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1954 * Sets the ellipsization mode which should be used by labels in @group.
1959 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1960 PangoEllipsizeMode ellipsize)
1962 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1964 if (ellipsize != group->priv->ellipsize)
1966 group->priv->ellipsize = ellipsize;
1967 gtk_tool_item_group_header_adjust_style (group);
1968 g_object_notify (G_OBJECT (group), "ellipsize");
1969 _gtk_tool_item_group_palette_reconfigured (group);
1974 * gtk_tool_item_group_get_label:
1975 * @group: a #GtkToolItemGroup
1977 * Gets the label of @group.
1979 * Returns: the label of @group. The label is an internal string of @group
1980 * and must not be modified. Note that %NULL is returned if a custom
1981 * label has been set with gtk_tool_item_group_set_label_widget()
1985 G_CONST_RETURN gchar*
1986 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1988 GtkToolItemGroupPrivate *priv;
1990 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1994 if (GTK_IS_LABEL (priv->label_widget))
1995 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2001 * gtk_tool_item_group_get_label_widget:
2002 * @group: a #GtkToolItemGroup
2004 * Gets the label widget of @group.
2005 * See gtk_tool_item_group_set_label_widget().
2007 * Returns: (transfer none): the label widget of @group
2012 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2014 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2016 return gtk_bin_get_child (GTK_BIN (alignment));
2020 * gtk_tool_item_group_get_collapsed:
2021 * @group: a GtkToolItemGroup
2023 * Gets whether @group is collapsed or expanded.
2025 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2030 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2032 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2034 return group->priv->collapsed;
2038 * gtk_tool_item_group_get_ellipsize:
2039 * @group: a #GtkToolItemGroup
2041 * Gets the ellipsization mode of @group.
2043 * Returns: the #PangoEllipsizeMode of @group
2048 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2050 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2052 return group->priv->ellipsize;
2056 * gtk_tool_item_group_get_header_relief:
2057 * @group: a #GtkToolItemGroup
2059 * Gets the relief mode of the header button of @group.
2061 * Returns: the #GtkReliefStyle
2066 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2068 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2070 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2074 * gtk_tool_item_group_insert:
2075 * @group: a #GtkToolItemGroup
2076 * @item: the #GtkToolItem to insert into @group
2077 * @position: the position of @item in @group, starting with 0.
2078 * The position -1 means end of list.
2080 * Inserts @item at @position in the list of children of @group.
2085 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2089 GtkWidget *parent, *child_widget;
2090 GtkToolItemGroupChild *child;
2092 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2093 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2094 g_return_if_fail (position >= -1);
2096 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2098 child = g_new (GtkToolItemGroupChild, 1);
2099 child->item = g_object_ref_sink (item);
2100 child->homogeneous = TRUE;
2101 child->expand = FALSE;
2103 child->new_row = FALSE;
2105 group->priv->children = g_list_insert (group->priv->children, child, position);
2107 if (GTK_IS_TOOL_PALETTE (parent))
2108 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2110 child_widget = gtk_bin_get_child (GTK_BIN (item));
2112 if (GTK_IS_BUTTON (child_widget))
2113 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2115 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2119 * gtk_tool_item_group_set_item_position:
2120 * @group: a #GtkToolItemGroup
2121 * @item: the #GtkToolItem to move to a new position, should
2122 * be a child of @group.
2123 * @position: the new position of @item in @group, starting with 0.
2124 * The position -1 means end of list.
2126 * Sets the position of @item in the list of children of @group.
2131 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2137 GtkToolItemGroupChild *child;
2138 GtkToolItemGroupPrivate* priv;
2140 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2141 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2142 g_return_if_fail (position >= -1);
2144 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2147 g_return_if_fail (child != NULL);
2149 if (position == old_position)
2152 priv->children = g_list_delete_link (priv->children, link);
2153 priv->children = g_list_insert (priv->children, child, position);
2155 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2156 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2157 gtk_widget_get_visible (GTK_WIDGET (item)))
2158 gtk_widget_queue_resize (GTK_WIDGET (group));
2162 * gtk_tool_item_group_get_item_position:
2163 * @group: a #GtkToolItemGroup
2164 * @item: a #GtkToolItem
2166 * Gets the position of @item in @group as index.
2168 * Returns: the index of @item in @group or -1 if @item is no child of @group
2173 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2178 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2179 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2181 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2188 * gtk_tool_item_group_get_n_items:
2189 * @group: a #GtkToolItemGroup
2191 * Gets the number of tool items in @group.
2193 * Returns: the number of tool items in @group
2198 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2200 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2202 return g_list_length (group->priv->children);
2206 * gtk_tool_item_group_get_nth_item:
2207 * @group: a #GtkToolItemGroup
2210 * Gets the tool item at @index in group.
2212 * Returns: (transfer none): the #GtkToolItem at index
2217 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2220 GtkToolItemGroupChild *child;
2222 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2224 child = g_list_nth_data (group->priv->children, index);
2226 return child != NULL ? child->item : NULL;
2230 * gtk_tool_item_group_get_drop_item:
2231 * @group: a #GtkToolItemGroup
2232 * @x: the x position
2233 * @y: the y position
2235 * Gets the tool item at position (x, y).
2237 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2242 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2246 GtkAllocation allocation;
2247 GtkOrientation orientation;
2250 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2252 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2253 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2255 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2256 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2258 for (it = group->priv->children; it != NULL; it = it->next)
2260 GtkToolItemGroupChild *child = it->data;
2261 GtkToolItem *item = child->item;
2264 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2267 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2269 x0 = x - allocation.x;
2270 y0 = y - allocation.y;
2272 if (x0 >= 0 && x0 < allocation.width &&
2273 y0 >= 0 && y0 < allocation.height)
2281 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2282 GtkRequisition *item_size,
2283 gboolean homogeneous_only,
2284 gint *requested_rows)
2286 GtkRequisition child_requisition;
2289 gboolean new_row = TRUE;
2290 GtkOrientation orientation;
2291 GtkToolbarStyle style;
2293 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2294 g_return_if_fail (NULL != item_size);
2296 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2297 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2299 item_size->width = item_size->height = 0;
2301 for (it = group->priv->children; it != NULL; it = it->next)
2303 GtkToolItemGroupChild *child = it->data;
2305 if (!gtk_tool_item_group_is_item_visible (group, child))
2308 if (child->new_row || new_row)
2314 if (!child->homogeneous && child->expand)
2317 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2318 &child_requisition, NULL);
2320 if (!homogeneous_only || child->homogeneous)
2321 item_size->width = MAX (item_size->width, child_requisition.width);
2322 item_size->height = MAX (item_size->height, child_requisition.height);
2326 *requested_rows = rows;
2330 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2333 GtkAllocation allocation;
2334 GtkWidget *widget = GTK_WIDGET (group);
2335 GtkToolItemGroupPrivate* priv = group->priv;
2337 gtk_widget_get_allocation (widget, &allocation);
2339 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2343 if (priv->animation_timeout)
2345 GtkAllocation header_allocation;
2346 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2347 cairo_pattern_t *mask;
2350 if (GTK_ORIENTATION_VERTICAL == orientation)
2351 v1 = allocation.height;
2353 v1 = allocation.width;
2357 gtk_widget_get_allocation (priv->header, &header_allocation);
2358 if (!gtk_widget_get_visible (priv->header))
2360 else if (GTK_ORIENTATION_VERTICAL == orientation)
2361 v0 = MAX (v0, header_allocation.height);
2363 v0 = MAX (v0, header_allocation.width);
2365 v1 = MIN (v0 + 256, v1);
2367 if (GTK_ORIENTATION_VERTICAL == orientation)
2372 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2379 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2382 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2383 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2384 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2385 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2386 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2388 cairo_mask (cr, mask);
2389 cairo_pattern_destroy (mask);
2396 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2401 GtkRequisition requisition;
2402 GtkToolItemGroupPrivate* priv = group->priv;
2404 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2405 &requisition, NULL);
2407 if (!priv->collapsed || priv->animation_timeout)
2409 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2410 GtkRequisition inquery;
2413 allocation.width = limit;
2415 allocation.height = limit;
2417 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2418 &allocation, &inquery);
2421 inquery.height -= requisition.height;
2423 inquery.width -= requisition.width;
2425 if (priv->animation_timeout && animation)
2427 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2429 timestamp = MIN (timestamp, ANIMATION_DURATION);
2431 if (priv->collapsed)
2432 timestamp = ANIMATION_DURATION - timestamp;
2436 inquery.height *= timestamp;
2437 inquery.height /= ANIMATION_DURATION;
2441 inquery.width *= timestamp;
2442 inquery.width /= ANIMATION_DURATION;
2447 requisition.height += inquery.height;
2449 requisition.width += inquery.width;
2452 return (vertical ? requisition.height : requisition.width);
2456 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2459 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2463 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2466 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2470 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2473 if (GTK_IS_TOOL_ITEM (child))
2474 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2479 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2481 gtk_container_foreach (GTK_CONTAINER (group),
2482 gtk_tool_palette_reconfigured_foreach_item,
2485 gtk_tool_item_group_header_adjust_style (group);