1 /* GtkToolPalette -- A tool palette with categories and DnD support
2 * Copyright (C) 2008 Openismus GmbH
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
27 #include "gtktoolpaletteprivate.h"
28 #include "gtktypebuiltins.h"
29 #include "gtkprivate.h"
32 #define ANIMATION_TIMEOUT 50
33 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
34 #define DEFAULT_ANIMATION_STATE TRUE
35 #define DEFAULT_EXPANDER_SIZE 16
36 #define DEFAULT_HEADER_SPACING 2
38 #define DEFAULT_LABEL ""
39 #define DEFAULT_COLLAPSED FALSE
40 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
43 * SECTION:gtktoolitemgroup
44 * @Short_description: A sub container used in a tool palette
45 * @Title: GtkToolItemGroup
47 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
48 * #GtkToolItem<!-- -->s to a palette like container with different
49 * categories and drag and drop support.
67 CHILD_PROP_HOMOGENEOUS,
74 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
76 struct _GtkToolItemGroupPrivate
79 GtkWidget *label_widget;
83 gint64 animation_start;
84 GSource *animation_timeout;
91 GtkSettings *settings;
92 gulong settings_connection;
94 PangoEllipsizeMode ellipsize;
100 struct _GtkToolItemGroupChild
104 guint homogeneous : 1;
110 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
112 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
113 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
116 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
118 return gtk_bin_get_child (GTK_BIN (group->priv->header));
121 static GtkOrientation
122 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
124 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
126 if (GTK_IS_TOOL_PALETTE (parent))
127 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
129 return GTK_ORIENTATION_VERTICAL;
132 static GtkToolbarStyle
133 gtk_tool_item_group_get_style (GtkToolShell *shell)
135 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
137 if (GTK_IS_TOOL_PALETTE (parent))
138 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
140 return GTK_TOOLBAR_ICONS;
144 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
146 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
148 if (GTK_IS_TOOL_PALETTE (parent))
149 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
151 return GTK_ICON_SIZE_SMALL_TOOLBAR;
154 static PangoEllipsizeMode
155 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
157 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
161 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
163 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
164 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
170 static GtkOrientation
171 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
173 return GTK_ORIENTATION_HORIZONTAL;
176 static GtkSizeGroup *
177 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
179 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
181 if (GTK_IS_TOOL_PALETTE (parent))
182 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
188 animation_change_notify (GtkToolItemGroup *group)
190 GtkSettings *settings = group->priv->settings;
194 g_object_get (settings,
195 "gtk-enable-animations", &animation,
198 animation = DEFAULT_ANIMATION_STATE;
200 group->priv->animation = animation;
204 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
205 const GParamSpec *pspec,
206 GtkToolItemGroup *group)
208 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
209 animation_change_notify (group);
213 gtk_tool_item_group_screen_changed (GtkWidget *widget,
214 GdkScreen *previous_screen)
216 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
217 GtkToolItemGroupPrivate* priv = group->priv;
218 GtkSettings *old_settings = priv->settings;
219 GtkSettings *settings;
221 if (gtk_widget_has_screen (GTK_WIDGET (group)))
222 settings = gtk_widget_get_settings (GTK_WIDGET (group));
226 if (settings == old_settings)
231 g_signal_handler_disconnect (old_settings, priv->settings_connection);
232 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);
279 state = gtk_widget_get_state_flags (widget);
281 if (!priv->collapsed)
282 state |= GTK_STATE_FLAG_ACTIVE;
284 gtk_style_context_save (context);
285 gtk_style_context_set_state (context, state);
286 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
288 if (GTK_ORIENTATION_VERTICAL == orientation)
290 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VERTICAL);
292 if (GTK_TEXT_DIR_RTL == direction)
297 y = height / 2 - priv->expander_size / 2;
301 gtk_style_context_add_class (context, GTK_STYLE_CLASS_HORIZONTAL);
302 x = width / 2 - priv->expander_size / 2;
306 /* The expander is the only animatable region */
307 gtk_style_context_push_animatable_region (context, GUINT_TO_POINTER (1));
309 gtk_render_expander (context, cr, x, y,
311 priv->expander_size);
313 gtk_style_context_pop_animatable_region (context);
314 gtk_style_context_restore (context);
320 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
323 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
324 GtkToolItemGroupPrivate* priv = group->priv;
325 GtkWidget *parent = gtk_widget_get_parent (data);
327 if (priv->collapsed ||
328 !GTK_IS_TOOL_PALETTE (parent) ||
329 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
330 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
334 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
336 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
337 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
338 GtkWidget *widget = GTK_WIDGET (group);
339 GtkToolItemGroupPrivate* priv = group->priv;
341 GtkTextDirection direction = gtk_widget_get_direction (widget);
343 gtk_widget_style_get (widget,
344 "header-spacing", &(priv->header_spacing),
345 "expander-size", &(priv->expander_size),
348 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
350 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
352 case GTK_ORIENTATION_HORIZONTAL:
353 dy = priv->header_spacing + priv->expander_size;
355 if (GTK_IS_LABEL (label_widget))
357 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
358 if (GTK_TEXT_DIR_RTL == direction)
359 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
361 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
365 case GTK_ORIENTATION_VERTICAL:
366 dx = priv->header_spacing + priv->expander_size;
368 if (GTK_IS_LABEL (label_widget))
370 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
371 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
376 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
380 gtk_tool_item_group_init (GtkToolItemGroup *group)
382 GtkWidget *alignment;
383 GtkToolItemGroupPrivate* priv;
385 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
387 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
388 GTK_TYPE_TOOL_ITEM_GROUP,
389 GtkToolItemGroupPrivate);
391 priv->children = NULL;
392 priv->header_spacing = DEFAULT_HEADER_SPACING;
393 priv->expander_size = DEFAULT_EXPANDER_SIZE;
395 priv->label_widget = gtk_label_new (NULL);
396 gtk_widget_set_halign (priv->label_widget, GTK_ALIGN_START);
397 gtk_widget_set_valign (priv->label_widget, GTK_ALIGN_CENTER);
398 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
399 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
400 gtk_widget_show_all (alignment);
402 gtk_widget_push_composite_child ();
403 priv->header = gtk_button_new ();
404 gtk_widget_set_composite_name (priv->header, "header");
405 gtk_widget_pop_composite_child ();
407 g_object_ref_sink (priv->header);
408 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
409 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
410 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
412 gtk_tool_item_group_header_adjust_style (group);
414 g_signal_connect_after (alignment, "draw",
415 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
418 g_signal_connect (priv->header, "clicked",
419 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
424 gtk_tool_item_group_set_property (GObject *object,
429 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
434 gtk_tool_item_group_set_label (group, g_value_get_string (value));
437 case PROP_LABEL_WIDGET:
438 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
442 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
446 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
450 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
454 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
460 gtk_tool_item_group_get_property (GObject *object,
465 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
470 g_value_set_string (value, gtk_tool_item_group_get_label (group));
473 case PROP_LABEL_WIDGET:
474 g_value_set_object (value,
475 gtk_tool_item_group_get_label_widget (group));
479 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
483 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
487 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
491 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
497 gtk_tool_item_group_finalize (GObject *object)
499 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
501 if (group->priv->children)
503 g_list_free (group->priv->children);
504 group->priv->children = NULL;
507 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
511 gtk_tool_item_group_dispose (GObject *object)
513 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
514 GtkToolItemGroupPrivate* priv = group->priv;
518 /* disconnect focus tracking handler */
519 g_signal_handler_disconnect (priv->toplevel,
522 priv->focus_set_id = 0;
523 priv->toplevel = NULL;
526 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
530 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
531 GtkRequisition *item_size,
532 gboolean homogeneous_only,
533 gint *requested_rows)
535 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
537 if (GTK_IS_TOOL_PALETTE (parent))
538 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
540 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
544 gtk_tool_item_group_size_request (GtkWidget *widget,
545 GtkRequisition *requisition)
547 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
548 GtkToolItemGroupPrivate* priv = group->priv;
549 GtkOrientation orientation;
550 GtkRequisition item_size;
554 if (priv->children && gtk_tool_item_group_get_label_widget (group))
556 gtk_widget_get_preferred_size (priv->header,
558 gtk_widget_show (priv->header);
562 requisition->width = requisition->height = 0;
563 gtk_widget_hide (priv->header);
566 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
568 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
570 if (GTK_ORIENTATION_VERTICAL == orientation)
571 requisition->width = MAX (requisition->width, item_size.width);
573 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
575 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
576 requisition->width += border_width * 2;
577 requisition->height += border_width * 2;
581 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
585 GtkRequisition requisition;
587 gtk_tool_item_group_size_request (widget, &requisition);
589 *minimum = *natural = requisition.width;
593 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
597 GtkRequisition requisition;
599 gtk_tool_item_group_size_request (widget, &requisition);
601 *minimum = *natural = requisition.height;
606 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
607 GtkToolItemGroupChild *child)
609 GtkToolbarStyle style;
610 GtkOrientation orientation;
612 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
613 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
615 /* horizontal tool palettes with text style support only homogeneous items */
616 if (!child->homogeneous &&
617 GTK_ORIENTATION_HORIZONTAL == orientation &&
618 GTK_TOOLBAR_TEXT == style)
622 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
623 (GTK_ORIENTATION_VERTICAL == orientation ?
624 gtk_tool_item_get_visible_vertical (child->item) :
625 gtk_tool_item_get_visible_horizontal (child->item));
628 static inline unsigned
632 return (x + y - 1) / y;
636 gtk_tool_item_group_real_size_query (GtkWidget *widget,
637 GtkAllocation *allocation,
638 GtkRequisition *inquery)
640 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
641 GtkToolItemGroupPrivate* priv = group->priv;
643 GtkRequisition item_size;
644 GtkAllocation item_area;
646 GtkOrientation orientation;
651 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
652 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
654 /* figure out the size of homogeneous items */
655 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
657 if (GTK_ORIENTATION_VERTICAL == orientation)
658 item_size.width = MIN (item_size.width, allocation->width);
660 item_size.height = MIN (item_size.height, allocation->height);
662 item_size.width = MAX (item_size.width, 1);
663 item_size.height = MAX (item_size.height, 1);
666 item_area.height = 0;
668 /* figure out the required columns (n_columns) and rows (n_rows)
671 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
677 if (GTK_ORIENTATION_VERTICAL == orientation)
679 gboolean new_row = FALSE;
683 item_area.width = allocation->width - 2 * border_width;
684 n_columns = MAX (item_area.width / item_size.width, 1);
686 /* calculate required rows for n_columns columns */
687 for (it = priv->children; it != NULL; it = it->next)
689 GtkToolItemGroupChild *child = it->data;
691 if (!gtk_tool_item_group_is_item_visible (group, child))
694 if (new_row || child->new_row)
704 if (child->homogeneous)
707 if (col >= n_columns)
712 GtkRequisition req = {0, 0};
715 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
718 width = udiv (req.width, item_size.width);
726 if (col >= n_columns)
734 guint *row_min_width;
736 gboolean new_row = TRUE;
737 guint col = 0, min_col, max_col = 0, all_items = 0;
740 item_area.height = allocation->height - 2 * border_width;
741 n_rows = MAX (item_area.height / item_size.height, min_rows);
743 row_min_width = g_new0 (guint, n_rows);
745 /* calculate minimal and maximal required cols and minimal
748 for (it = priv->children; it != NULL; it = it->next)
750 GtkToolItemGroupChild *child = it->data;
752 if (!gtk_tool_item_group_is_item_visible (group, child))
755 if (new_row || child->new_row)
760 row_min_width[row] = 1;
766 if (child->homogeneous)
773 GtkRequisition req = {0, 0};
776 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
779 width = udiv (req.width, item_size.width);
784 row_min_width[row] = MAX (row_min_width[row], width);
787 max_col = MAX (max_col, col);
790 /* calculate minimal required cols */
791 min_col = udiv (all_items, n_rows);
793 for (i = 0; i <= row; i++)
795 min_col = MAX (min_col, row_min_width[i]);
798 /* simple linear search for minimal required columns
799 * for the given maximal number of rows (n_rows)
801 for (n_columns = min_col; n_columns < max_col; n_columns ++)
805 /* calculate required rows for n_columns columns */
806 for (it = priv->children; it != NULL; it = it->next)
808 GtkToolItemGroupChild *child = it->data;
810 if (!gtk_tool_item_group_is_item_visible (group, child))
813 if (new_row || child->new_row)
823 if (child->homogeneous)
826 if (col >= n_columns)
831 GtkRequisition req = {0, 0};
834 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
837 width = udiv (req.width, item_size.width);
845 if (col >= n_columns)
855 item_area.width = item_size.width * n_columns;
856 item_area.height = item_size.height * n_rows;
862 /* figure out header widget size */
863 if (gtk_widget_get_visible (priv->header))
865 GtkRequisition child_requisition;
867 gtk_widget_get_preferred_size (priv->header,
868 &child_requisition, NULL);
870 if (GTK_ORIENTATION_VERTICAL == orientation)
871 inquery->height += child_requisition.height;
873 inquery->width += child_requisition.width;
876 /* report effective widget size */
877 inquery->width += item_area.width + 2 * border_width;
878 inquery->height += item_area.height + 2 * border_width;
882 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
883 GtkAllocation *allocation)
885 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
886 GtkToolItemGroupPrivate* priv = group->priv;
887 GtkRequisition child_requisition;
888 GtkAllocation child_allocation;
890 GtkRequisition item_size;
891 GtkAllocation item_area;
893 GtkOrientation orientation;
897 gint n_columns, n_rows = 1;
900 GtkTextDirection direction;
902 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
904 direction = gtk_widget_get_direction (widget);
906 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
909 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
911 child_allocation.x = border_width;
912 child_allocation.y = border_width;
914 /* place the header widget */
915 if (gtk_widget_get_visible (priv->header))
917 gtk_widget_get_preferred_size (priv->header,
918 &child_requisition, NULL);
920 if (GTK_ORIENTATION_VERTICAL == orientation)
922 child_allocation.width = allocation->width;
923 child_allocation.height = child_requisition.height;
927 child_allocation.width = child_requisition.width;
928 child_allocation.height = allocation->height;
930 if (GTK_TEXT_DIR_RTL == direction)
931 child_allocation.x = allocation->width - border_width - child_allocation.width;
934 gtk_widget_size_allocate (priv->header, &child_allocation);
936 if (GTK_ORIENTATION_VERTICAL == orientation)
937 child_allocation.y += child_allocation.height;
938 else if (GTK_TEXT_DIR_RTL != direction)
939 child_allocation.x += child_allocation.width;
941 child_allocation.x = border_width;
944 child_requisition.width = child_requisition.height = 0;
946 /* figure out the size of homogeneous items */
947 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
949 item_size.width = MAX (item_size.width, 1);
950 item_size.height = MAX (item_size.height, 1);
952 /* figure out the available columns and size of item_area */
953 if (GTK_ORIENTATION_VERTICAL == orientation)
955 item_size.width = MIN (item_size.width, allocation->width);
957 item_area.width = allocation->width - 2 * border_width;
958 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
960 n_columns = MAX (item_area.width / item_size.width, 1);
962 item_size.width = item_area.width / n_columns;
966 item_size.height = MIN (item_size.height, allocation->height);
968 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
969 item_area.height = allocation->height - 2 * border_width;
971 n_columns = MAX (item_area.width / item_size.width, 1);
972 n_rows = MAX (item_area.height / item_size.height, min_rows);
974 item_size.height = item_area.height / n_rows;
977 item_area.x = child_allocation.x;
978 item_area.y = child_allocation.y;
980 /* when expanded or in transition, place the tool items in a grid like layout */
981 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
983 gint col = 0, row = 0;
985 for (it = priv->children; it != NULL; it = it->next)
987 GtkToolItemGroupChild *child = it->data;
990 if (!gtk_tool_item_group_is_item_visible (group, child))
992 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
997 /* for non homogeneous widgets request the required size */
998 child_requisition.width = 0;
1000 if (!child->homogeneous)
1002 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
1003 &child_requisition, NULL);
1004 child_requisition.width = MIN (child_requisition.width, item_area.width);
1007 /* select next row if at end of row */
1008 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1012 child_allocation.y += child_allocation.height;
1017 /* calculate the position and size of the item */
1018 if (!child->homogeneous)
1024 col_width = udiv (child_requisition.width, item_size.width);
1026 col_width = n_columns - col;
1028 width = col_width * item_size.width;
1030 if (GTK_TEXT_DIR_RTL == direction)
1031 col_child = (n_columns - col - col_width);
1035 child_allocation.x = item_area.x + col_child * item_size.width;
1036 child_allocation.width = width;
1040 child_allocation.x =
1041 (item_area.x + col_child * item_size.width +
1042 (width - child_requisition.width) / 2);
1043 child_allocation.width = child_requisition.width;
1050 if (GTK_TEXT_DIR_RTL == direction)
1051 col_child = (n_columns - col - 1);
1053 child_allocation.x = item_area.x + col_child * item_size.width;
1054 child_allocation.width = item_size.width;
1059 child_allocation.height = item_size.height;
1061 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1062 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1065 child_allocation.y += item_size.height;
1068 /* or just hide all items, when collapsed */
1072 for (it = priv->children; it != NULL; it = it->next)
1074 GtkToolItemGroupChild *child = it->data;
1076 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1082 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1083 GtkAllocation *allocation)
1085 gtk_tool_item_group_real_size_allocate (widget, allocation);
1087 if (gtk_widget_get_mapped (widget))
1088 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1092 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1096 GtkAdjustment *adjustment;
1097 GtkAllocation allocation, p_allocation;
1100 /* Find this group's parent widget in the focused widget's anchestry. */
1101 for (p = widget; p; p = gtk_widget_get_parent (p))
1104 p = gtk_widget_get_parent (p);
1108 if (GTK_IS_TOOL_PALETTE (p))
1110 /* Check that the focused widgets is fully visible within
1111 * the group's parent widget and make it visible otherwise. */
1113 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1119 gtk_widget_get_allocation (widget, &allocation);
1120 gtk_widget_get_allocation (p, &p_allocation);
1122 /* Handle vertical adjustment. */
1123 if (gtk_widget_translate_coordinates
1124 (widget, p, 0, 0, NULL, &y) && y < 0)
1126 y += gtk_adjustment_get_value (adjustment);
1127 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1129 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1130 y > p_allocation.height)
1132 y += gtk_adjustment_get_value (adjustment);
1133 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1137 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1143 gtk_widget_get_allocation (widget, &allocation);
1144 gtk_widget_get_allocation (p, &p_allocation);
1146 /* Handle horizontal adjustment. */
1147 if (gtk_widget_translate_coordinates
1148 (widget, p, 0, 0, &x, NULL) && x < 0)
1150 x += gtk_adjustment_get_value (adjustment);
1151 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1153 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1154 x > p_allocation.width)
1156 x += gtk_adjustment_get_value (adjustment);
1157 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1166 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1167 GtkWidget *toplevel)
1169 GtkToolItemGroupPrivate* priv = group->priv;
1171 if (toplevel != priv->toplevel)
1175 /* Disconnect focus tracking handler. */
1176 g_signal_handler_disconnect (priv->toplevel,
1177 priv->focus_set_id);
1179 priv->focus_set_id = 0;
1180 priv->toplevel = NULL;
1185 /* Install focus tracking handler. We connect to the window's
1186 * set-focus signal instead of connecting to the focus signal of
1189 * 1) Reduce the number of signal handlers used.
1190 * 2) Avoid special handling for group headers.
1191 * 3) Catch focus grabs not only for direct children,
1192 * but also for nested widgets.
1194 priv->focus_set_id =
1195 g_signal_connect (toplevel, "set-focus",
1196 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1199 priv->toplevel = toplevel;
1205 gtk_tool_item_group_realize (GtkWidget *widget)
1207 GtkAllocation allocation;
1208 GtkWidget *toplevel_window;
1210 GdkWindowAttr attributes;
1211 gint attributes_mask;
1213 GtkStyleContext *context;
1215 gtk_widget_set_realized (widget, TRUE);
1217 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1218 context = gtk_widget_get_style_context (widget);
1220 gtk_widget_get_allocation (widget, &allocation);
1222 attributes.window_type = GDK_WINDOW_CHILD;
1223 attributes.x = allocation.x + border_width;
1224 attributes.y = allocation.y + border_width;
1225 attributes.width = allocation.width - border_width * 2;
1226 attributes.height = allocation.height - border_width * 2;
1227 attributes.wclass = GDK_INPUT_OUTPUT;
1228 attributes.visual = gtk_widget_get_visual (widget);
1229 attributes.event_mask = gtk_widget_get_events (widget)
1230 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1231 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1232 | GDK_BUTTON_MOTION_MASK;
1233 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1235 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1236 &attributes, attributes_mask);
1237 gtk_widget_set_window (widget, window);
1239 gdk_window_set_user_data (window, widget);
1241 gtk_style_context_set_background (context, window);
1243 gtk_container_forall (GTK_CONTAINER (widget),
1244 (GtkCallback) gtk_widget_set_parent_window,
1247 gtk_widget_queue_resize_no_redraw (widget);
1249 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1250 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1255 gtk_tool_item_group_unrealize (GtkWidget *widget)
1257 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1258 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1262 gtk_tool_item_group_style_updated (GtkWidget *widget)
1264 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1265 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
1269 gtk_tool_item_group_add (GtkContainer *container,
1272 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1273 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1275 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1276 GTK_TOOL_ITEM (widget), -1);
1280 gtk_tool_item_group_remove (GtkContainer *container,
1283 GtkToolItemGroup *group;
1284 GtkToolItemGroupPrivate* priv;
1287 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1288 group = GTK_TOOL_ITEM_GROUP (container);
1291 for (it = priv->children; it != NULL; it = it->next)
1293 GtkToolItemGroupChild *child_info = it->data;
1295 if ((GtkWidget *)child_info->item == child)
1297 g_object_unref (child);
1298 gtk_widget_unparent (child);
1300 g_free (child_info);
1301 priv->children = g_list_delete_link (priv->children, it);
1303 gtk_widget_queue_resize (GTK_WIDGET (container));
1310 gtk_tool_item_group_forall (GtkContainer *container,
1312 GtkCallback callback,
1313 gpointer callback_data)
1315 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1316 GtkToolItemGroupPrivate* priv = group->priv;
1319 if (internals && priv->header)
1320 callback (priv->header, callback_data);
1322 children = priv->children;
1325 GtkToolItemGroupChild *child = children->data;
1326 children = children->next; /* store pointer before call to callback
1327 because the child pointer is invalid if the
1328 child->item is removed from the item group
1331 callback (GTK_WIDGET (child->item), callback_data);
1336 gtk_tool_item_group_child_type (GtkContainer *container)
1338 return GTK_TYPE_TOOL_ITEM;
1341 static GtkToolItemGroupChild *
1342 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1350 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1351 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1353 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1355 GtkToolItemGroupChild *child = it->data;
1357 if (child->item == item)
1373 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1375 gboolean *homogeneous,
1380 GtkToolItemGroupChild *child;
1382 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1383 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1385 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1390 *expand = child->expand;
1393 *homogeneous = child->homogeneous;
1396 *fill = child->fill;
1399 *new_row = child->new_row;
1403 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1405 gboolean homogeneous,
1410 GtkToolItemGroupChild *child;
1411 gboolean changed = FALSE;
1413 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1414 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1416 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1420 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1422 if (child->homogeneous != homogeneous)
1424 child->homogeneous = homogeneous;
1426 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1428 if (child->expand != expand)
1430 child->expand = expand;
1432 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1434 if (child->fill != fill)
1438 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1440 if (child->new_row != new_row)
1442 child->new_row = new_row;
1444 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1447 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1450 && gtk_widget_get_visible (GTK_WIDGET (group))
1451 && gtk_widget_get_visible (GTK_WIDGET (item)))
1452 gtk_widget_queue_resize (GTK_WIDGET (group));
1456 gtk_tool_item_group_set_child_property (GtkContainer *container,
1459 const GValue *value,
1462 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1463 GtkToolItem *item = GTK_TOOL_ITEM (child);
1464 gboolean homogeneous, expand, fill, new_row;
1466 if (prop_id != CHILD_PROP_POSITION)
1467 gtk_tool_item_group_get_item_packing (group, item,
1475 case CHILD_PROP_HOMOGENEOUS:
1476 gtk_tool_item_group_set_item_packing (group, item,
1477 g_value_get_boolean (value),
1483 case CHILD_PROP_EXPAND:
1484 gtk_tool_item_group_set_item_packing (group, item,
1486 g_value_get_boolean (value),
1491 case CHILD_PROP_FILL:
1492 gtk_tool_item_group_set_item_packing (group, item,
1495 g_value_get_boolean (value),
1499 case CHILD_PROP_NEW_ROW:
1500 gtk_tool_item_group_set_item_packing (group, item,
1504 g_value_get_boolean (value));
1507 case CHILD_PROP_POSITION:
1508 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1512 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1518 gtk_tool_item_group_get_child_property (GtkContainer *container,
1524 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1525 GtkToolItem *item = GTK_TOOL_ITEM (child);
1526 gboolean homogeneous, expand, fill, new_row;
1528 if (prop_id != CHILD_PROP_POSITION)
1529 gtk_tool_item_group_get_item_packing (group, item,
1537 case CHILD_PROP_HOMOGENEOUS:
1538 g_value_set_boolean (value, homogeneous);
1541 case CHILD_PROP_EXPAND:
1542 g_value_set_boolean (value, expand);
1545 case CHILD_PROP_FILL:
1546 g_value_set_boolean (value, fill);
1549 case CHILD_PROP_NEW_ROW:
1550 g_value_set_boolean (value, new_row);
1553 case CHILD_PROP_POSITION:
1554 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1558 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1564 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1566 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1567 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1568 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1570 oclass->set_property = gtk_tool_item_group_set_property;
1571 oclass->get_property = gtk_tool_item_group_get_property;
1572 oclass->finalize = gtk_tool_item_group_finalize;
1573 oclass->dispose = gtk_tool_item_group_dispose;
1575 wclass->get_preferred_width = gtk_tool_item_group_get_preferred_width;
1576 wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1577 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1578 wclass->realize = gtk_tool_item_group_realize;
1579 wclass->unrealize = gtk_tool_item_group_unrealize;
1580 wclass->style_updated = gtk_tool_item_group_style_updated;
1581 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1583 cclass->add = gtk_tool_item_group_add;
1584 cclass->remove = gtk_tool_item_group_remove;
1585 cclass->forall = gtk_tool_item_group_forall;
1586 cclass->child_type = gtk_tool_item_group_child_type;
1587 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1588 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1590 g_object_class_install_property (oclass, PROP_LABEL,
1591 g_param_spec_string ("label",
1593 P_("The human-readable title of this item group"),
1595 GTK_PARAM_READWRITE));
1597 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1598 g_param_spec_object ("label-widget",
1600 P_("A widget to display in place of the usual label"),
1602 GTK_PARAM_READWRITE));
1604 g_object_class_install_property (oclass, PROP_COLLAPSED,
1605 g_param_spec_boolean ("collapsed",
1607 P_("Whether the group has been collapsed and items are hidden"),
1609 GTK_PARAM_READWRITE));
1611 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1612 g_param_spec_enum ("ellipsize",
1614 P_("Ellipsize for item group headers"),
1615 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1616 GTK_PARAM_READWRITE));
1618 g_object_class_install_property (oclass, PROP_RELIEF,
1619 g_param_spec_enum ("header-relief",
1620 P_("Header Relief"),
1621 P_("Relief of the group header button"),
1622 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1623 GTK_PARAM_READWRITE));
1625 gtk_widget_class_install_style_property (wclass,
1626 g_param_spec_int ("expander-size",
1627 P_("Expander Size"),
1628 P_("Size of the expander arrow"),
1631 DEFAULT_EXPANDER_SIZE,
1632 GTK_PARAM_READABLE));
1634 gtk_widget_class_install_style_property (wclass,
1635 g_param_spec_int ("header-spacing",
1636 P_("Header Spacing"),
1637 P_("Spacing between expander arrow and caption"),
1640 DEFAULT_HEADER_SPACING,
1641 GTK_PARAM_READABLE));
1643 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1644 g_param_spec_boolean ("homogeneous",
1646 P_("Whether the item should be the same size as other homogeneous items"),
1648 GTK_PARAM_READWRITE));
1650 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1651 g_param_spec_boolean ("expand",
1653 P_("Whether the item should receive extra space when the group grows"),
1655 GTK_PARAM_READWRITE));
1657 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1658 g_param_spec_boolean ("fill",
1660 P_("Whether the item should fill the available space"),
1662 GTK_PARAM_READWRITE));
1664 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1665 g_param_spec_boolean ("new-row",
1667 P_("Whether the item should start a new row"),
1669 GTK_PARAM_READWRITE));
1671 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1672 g_param_spec_int ("position",
1674 P_("Position of the item within this group"),
1678 GTK_PARAM_READWRITE));
1680 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1684 * gtk_tool_item_group_new:
1685 * @label: the label of the new group
1687 * Creates a new tool item group with label @label.
1689 * Returns: a new #GtkToolItemGroup.
1694 gtk_tool_item_group_new (const gchar *label)
1696 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1700 * gtk_tool_item_group_set_label:
1701 * @group: a #GtkToolItemGroup
1702 * @label: the new human-readable label of of the group
1704 * Sets the label of the tool item group. The label is displayed in the header
1710 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1713 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1716 gtk_tool_item_group_set_label_widget (group, NULL);
1719 GtkWidget *child = gtk_label_new (label);
1720 gtk_widget_show (child);
1722 gtk_tool_item_group_set_label_widget (group, child);
1725 g_object_notify (G_OBJECT (group), "label");
1729 * gtk_tool_item_group_set_label_widget:
1730 * @group: a #GtkToolItemGroup
1731 * @label_widget: the widget to be displayed in place of the usual label
1733 * Sets the label of the tool item group.
1734 * The label widget is displayed in the header of the group, in place
1735 * of the usual label.
1740 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1741 GtkWidget *label_widget)
1743 GtkToolItemGroupPrivate* priv;
1744 GtkWidget *alignment;
1746 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1747 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1748 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1752 if (priv->label_widget == label_widget)
1755 alignment = gtk_tool_item_group_get_alignment (group);
1757 if (priv->label_widget)
1759 gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1760 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1765 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1767 priv->label_widget = label_widget;
1769 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1770 gtk_widget_queue_resize (GTK_WIDGET (group));
1772 /* Only show the header widget if the group has children: */
1773 if (label_widget && priv->children)
1774 gtk_widget_show (priv->header);
1776 gtk_widget_hide (priv->header);
1778 g_object_freeze_notify (G_OBJECT (group));
1779 g_object_notify (G_OBJECT (group), "label-widget");
1780 g_object_notify (G_OBJECT (group), "label");
1781 g_object_thaw_notify (G_OBJECT (group));
1785 * gtk_tool_item_group_set_header_relief:
1786 * @group: a #GtkToolItemGroup
1787 * @style: the #GtkReliefStyle
1789 * Set the button relief of the group header.
1790 * See gtk_button_set_relief() for details.
1795 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1796 GtkReliefStyle style)
1798 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1800 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1804 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1806 return (g_source_get_time (group->priv->animation_timeout) -
1807 group->priv->animation_start) / 1000;
1811 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1813 GtkToolItemGroupPrivate* priv = group->priv;
1814 GtkWidget *widget = GTK_WIDGET (group);
1816 if (gtk_widget_get_realized (priv->header))
1818 GtkAllocation alignment_allocation;
1819 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1822 /* Find the header button's arrow area... */
1823 gtk_widget_get_allocation (alignment, &alignment_allocation);
1824 area.x = alignment_allocation.x;
1825 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1826 area.height = priv->expander_size;
1827 area.width = priv->expander_size;
1829 /* ... and invalidated it to get it animated. */
1830 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1833 if (gtk_widget_get_realized (widget))
1835 GtkAllocation allocation;
1836 GtkWidget *parent = gtk_widget_get_parent (widget);
1837 int x, y, width, height;
1839 /* Find the tool item area button's arrow area... */
1840 gtk_widget_get_allocation (widget, &allocation);
1841 width = allocation.width;
1842 height = allocation.height;
1844 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1846 if (gtk_widget_get_visible (priv->header))
1848 GtkAllocation header_allocation;
1850 gtk_widget_get_allocation (priv->header, &header_allocation);
1851 height -= header_allocation.height;
1852 y += header_allocation.height;
1855 /* ... and invalidated it to get it animated. */
1856 gtk_widget_queue_draw_area (parent, x, y, width, height);
1861 gtk_tool_item_group_animation_cb (gpointer data)
1863 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1864 GtkToolItemGroupPrivate* priv = group->priv;
1865 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1868 GDK_THREADS_ENTER ();
1870 /* Enque this early to reduce number of expose events. */
1871 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1873 gtk_tool_item_group_force_expose (group);
1875 /* Finish animation when done. */
1876 if (timestamp >= ANIMATION_DURATION)
1877 priv->animation_timeout = NULL;
1879 retval = (priv->animation_timeout != NULL);
1881 GDK_THREADS_LEAVE ();
1887 * gtk_tool_item_group_set_collapsed:
1888 * @group: a #GtkToolItemGroup
1889 * @collapsed: whether the @group should be collapsed or expanded
1891 * Sets whether the @group should be collapsed or expanded.
1896 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1900 GtkToolItemGroupPrivate* priv;
1902 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1906 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1907 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1908 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1909 GTK_WIDGET (group));
1910 if (collapsed != priv->collapsed)
1912 GtkStyleContext *context;
1914 if (priv->animation)
1916 if (priv->animation_timeout)
1917 g_source_destroy (priv->animation_timeout);
1919 priv->animation_start = g_get_monotonic_time ();
1920 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1922 g_source_set_callback (priv->animation_timeout,
1923 gtk_tool_item_group_animation_cb,
1925 g_source_attach (priv->animation_timeout, NULL);
1927 context = gtk_widget_get_style_context (gtk_bin_get_child (GTK_BIN (priv->header)));
1929 gtk_style_context_save (context);
1930 gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
1932 gtk_style_context_notify_state_change (context,
1933 gtk_widget_get_window (priv->header),
1934 GUINT_TO_POINTER (1),
1935 GTK_STATE_FLAG_ACTIVE,
1938 gtk_style_context_restore (context);
1941 gtk_tool_item_group_force_expose (group);
1943 priv->collapsed = collapsed;
1944 g_object_notify (G_OBJECT (group), "collapsed");
1949 * gtk_tool_item_group_set_ellipsize:
1950 * @group: a #GtkToolItemGroup
1951 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1953 * Sets the ellipsization mode which should be used by labels in @group.
1958 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1959 PangoEllipsizeMode ellipsize)
1961 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1963 if (ellipsize != group->priv->ellipsize)
1965 group->priv->ellipsize = ellipsize;
1966 gtk_tool_item_group_header_adjust_style (group);
1967 g_object_notify (G_OBJECT (group), "ellipsize");
1968 _gtk_tool_item_group_palette_reconfigured (group);
1973 * gtk_tool_item_group_get_label:
1974 * @group: a #GtkToolItemGroup
1976 * Gets the label of @group.
1978 * Returns: the label of @group. The label is an internal string of @group
1979 * and must not be modified. Note that %NULL is returned if a custom
1980 * label has been set with gtk_tool_item_group_set_label_widget()
1985 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1987 GtkToolItemGroupPrivate *priv;
1989 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1993 if (GTK_IS_LABEL (priv->label_widget))
1994 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2000 * gtk_tool_item_group_get_label_widget:
2001 * @group: a #GtkToolItemGroup
2003 * Gets the label widget of @group.
2004 * See gtk_tool_item_group_set_label_widget().
2006 * Returns: (transfer none): the label widget of @group
2011 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2013 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2015 return gtk_bin_get_child (GTK_BIN (alignment));
2019 * gtk_tool_item_group_get_collapsed:
2020 * @group: a GtkToolItemGroup
2022 * Gets whether @group is collapsed or expanded.
2024 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2029 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2031 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2033 return group->priv->collapsed;
2037 * gtk_tool_item_group_get_ellipsize:
2038 * @group: a #GtkToolItemGroup
2040 * Gets the ellipsization mode of @group.
2042 * Returns: the #PangoEllipsizeMode of @group
2047 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2049 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2051 return group->priv->ellipsize;
2055 * gtk_tool_item_group_get_header_relief:
2056 * @group: a #GtkToolItemGroup
2058 * Gets the relief mode of the header button of @group.
2060 * Returns: the #GtkReliefStyle
2065 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2067 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2069 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2073 * gtk_tool_item_group_insert:
2074 * @group: a #GtkToolItemGroup
2075 * @item: the #GtkToolItem to insert into @group
2076 * @position: the position of @item in @group, starting with 0.
2077 * The position -1 means end of list.
2079 * Inserts @item at @position in the list of children of @group.
2084 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2088 GtkWidget *parent, *child_widget;
2089 GtkToolItemGroupChild *child;
2091 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2092 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2093 g_return_if_fail (position >= -1);
2095 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2097 child = g_new (GtkToolItemGroupChild, 1);
2098 child->item = g_object_ref_sink (item);
2099 child->homogeneous = TRUE;
2100 child->expand = FALSE;
2102 child->new_row = FALSE;
2104 group->priv->children = g_list_insert (group->priv->children, child, position);
2106 if (GTK_IS_TOOL_PALETTE (parent))
2107 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2109 child_widget = gtk_bin_get_child (GTK_BIN (item));
2111 if (GTK_IS_BUTTON (child_widget))
2112 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2114 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2118 * gtk_tool_item_group_set_item_position:
2119 * @group: a #GtkToolItemGroup
2120 * @item: the #GtkToolItem to move to a new position, should
2121 * be a child of @group.
2122 * @position: the new position of @item in @group, starting with 0.
2123 * The position -1 means end of list.
2125 * Sets the position of @item in the list of children of @group.
2130 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2136 GtkToolItemGroupChild *child;
2137 GtkToolItemGroupPrivate* priv;
2139 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2140 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2141 g_return_if_fail (position >= -1);
2143 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2146 g_return_if_fail (child != NULL);
2148 if (position == old_position)
2151 priv->children = g_list_delete_link (priv->children, link);
2152 priv->children = g_list_insert (priv->children, child, position);
2154 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2155 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2156 gtk_widget_get_visible (GTK_WIDGET (item)))
2157 gtk_widget_queue_resize (GTK_WIDGET (group));
2161 * gtk_tool_item_group_get_item_position:
2162 * @group: a #GtkToolItemGroup
2163 * @item: a #GtkToolItem
2165 * Gets the position of @item in @group as index.
2167 * Returns: the index of @item in @group or -1 if @item is no child of @group
2172 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2177 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2178 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2180 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2187 * gtk_tool_item_group_get_n_items:
2188 * @group: a #GtkToolItemGroup
2190 * Gets the number of tool items in @group.
2192 * Returns: the number of tool items in @group
2197 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2199 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2201 return g_list_length (group->priv->children);
2205 * gtk_tool_item_group_get_nth_item:
2206 * @group: a #GtkToolItemGroup
2209 * Gets the tool item at @index in group.
2211 * Returns: (transfer none): the #GtkToolItem at index
2216 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2219 GtkToolItemGroupChild *child;
2221 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2223 child = g_list_nth_data (group->priv->children, index);
2225 return child != NULL ? child->item : NULL;
2229 * gtk_tool_item_group_get_drop_item:
2230 * @group: a #GtkToolItemGroup
2231 * @x: the x position
2232 * @y: the y position
2234 * Gets the tool item at position (x, y).
2236 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2241 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2245 GtkAllocation allocation;
2248 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2250 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2252 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2253 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2255 for (it = group->priv->children; it != NULL; it = it->next)
2257 GtkToolItemGroupChild *child = it->data;
2258 GtkToolItem *item = child->item;
2261 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2264 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2266 x0 = x - allocation.x;
2267 y0 = y - allocation.y;
2269 if (x0 >= 0 && x0 < allocation.width &&
2270 y0 >= 0 && y0 < allocation.height)
2278 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2279 GtkRequisition *item_size,
2280 gboolean homogeneous_only,
2281 gint *requested_rows)
2283 GtkRequisition child_requisition;
2286 gboolean new_row = TRUE;
2288 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2289 g_return_if_fail (NULL != item_size);
2291 item_size->width = item_size->height = 0;
2293 for (it = group->priv->children; it != NULL; it = it->next)
2295 GtkToolItemGroupChild *child = it->data;
2297 if (!gtk_tool_item_group_is_item_visible (group, child))
2300 if (child->new_row || new_row)
2306 if (!child->homogeneous && child->expand)
2309 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2310 &child_requisition, NULL);
2312 if (!homogeneous_only || child->homogeneous)
2313 item_size->width = MAX (item_size->width, child_requisition.width);
2314 item_size->height = MAX (item_size->height, child_requisition.height);
2318 *requested_rows = rows;
2322 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2325 GtkAllocation allocation;
2326 GtkWidget *widget = GTK_WIDGET (group);
2327 GtkToolItemGroupPrivate* priv = group->priv;
2329 gtk_widget_get_allocation (widget, &allocation);
2331 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2335 if (priv->animation_timeout)
2337 GtkAllocation header_allocation;
2338 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2339 cairo_pattern_t *mask;
2342 if (GTK_ORIENTATION_VERTICAL == orientation)
2343 v1 = allocation.height;
2345 v1 = allocation.width;
2349 gtk_widget_get_allocation (priv->header, &header_allocation);
2350 if (!gtk_widget_get_visible (priv->header))
2352 else if (GTK_ORIENTATION_VERTICAL == orientation)
2353 v0 = MAX (v0, header_allocation.height);
2355 v0 = MAX (v0, header_allocation.width);
2357 v1 = MIN (v0 + 256, v1);
2359 if (GTK_ORIENTATION_VERTICAL == orientation)
2364 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2371 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2374 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2375 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2376 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2377 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2378 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2380 cairo_mask (cr, mask);
2381 cairo_pattern_destroy (mask);
2388 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2393 GtkRequisition requisition;
2394 GtkToolItemGroupPrivate* priv = group->priv;
2396 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2397 &requisition, NULL);
2399 if (!priv->collapsed || priv->animation_timeout)
2401 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2402 GtkRequisition inquery;
2405 allocation.width = limit;
2407 allocation.height = limit;
2409 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2410 &allocation, &inquery);
2413 inquery.height -= requisition.height;
2415 inquery.width -= requisition.width;
2417 if (priv->animation_timeout && animation)
2419 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2421 timestamp = MIN (timestamp, ANIMATION_DURATION);
2423 if (priv->collapsed)
2424 timestamp = ANIMATION_DURATION - timestamp;
2428 inquery.height *= timestamp;
2429 inquery.height /= ANIMATION_DURATION;
2433 inquery.width *= timestamp;
2434 inquery.width /= ANIMATION_DURATION;
2439 requisition.height += inquery.height;
2441 requisition.width += inquery.width;
2444 return (vertical ? requisition.height : requisition.width);
2448 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2451 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2455 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2458 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2462 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2465 if (GTK_IS_TOOL_ITEM (child))
2466 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2471 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2473 gtk_container_foreach (GTK_CONTAINER (group),
2474 gtk_tool_palette_reconfigured_foreach_item,
2477 gtk_tool_item_group_header_adjust_style (group);