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 gtk_render_expander (context, cr, x, y,
308 priv->expander_size);
310 gtk_style_context_restore (context);
316 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
319 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
320 GtkToolItemGroupPrivate* priv = group->priv;
321 GtkWidget *parent = gtk_widget_get_parent (data);
323 if (priv->collapsed ||
324 !GTK_IS_TOOL_PALETTE (parent) ||
325 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
326 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
330 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
332 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
333 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
334 GtkWidget *widget = GTK_WIDGET (group);
335 GtkToolItemGroupPrivate* priv = group->priv;
337 GtkTextDirection direction = gtk_widget_get_direction (widget);
339 gtk_widget_style_get (widget,
340 "header-spacing", &(priv->header_spacing),
341 "expander-size", &(priv->expander_size),
344 gtk_widget_set_size_request (alignment, -1, priv->expander_size);
346 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
348 case GTK_ORIENTATION_HORIZONTAL:
349 dy = priv->header_spacing + priv->expander_size;
351 if (GTK_IS_LABEL (label_widget))
353 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
354 if (GTK_TEXT_DIR_RTL == direction)
355 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
357 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
361 case GTK_ORIENTATION_VERTICAL:
362 dx = priv->header_spacing + priv->expander_size;
364 if (GTK_IS_LABEL (label_widget))
366 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
367 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
372 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
376 gtk_tool_item_group_init (GtkToolItemGroup *group)
378 GtkWidget *alignment;
379 GtkToolItemGroupPrivate* priv;
381 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
383 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
384 GTK_TYPE_TOOL_ITEM_GROUP,
385 GtkToolItemGroupPrivate);
387 priv->children = NULL;
388 priv->header_spacing = DEFAULT_HEADER_SPACING;
389 priv->expander_size = DEFAULT_EXPANDER_SIZE;
391 priv->label_widget = gtk_label_new (NULL);
392 gtk_widget_set_halign (priv->label_widget, GTK_ALIGN_START);
393 gtk_widget_set_valign (priv->label_widget, GTK_ALIGN_CENTER);
394 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
395 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
396 gtk_widget_show_all (alignment);
398 gtk_widget_push_composite_child ();
399 priv->header = gtk_button_new ();
400 gtk_widget_set_composite_name (priv->header, "header");
401 gtk_widget_pop_composite_child ();
403 g_object_ref_sink (priv->header);
404 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
405 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
406 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
408 gtk_tool_item_group_header_adjust_style (group);
410 g_signal_connect_after (alignment, "draw",
411 G_CALLBACK (gtk_tool_item_group_header_draw_cb),
414 g_signal_connect (priv->header, "clicked",
415 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
420 gtk_tool_item_group_set_property (GObject *object,
425 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
430 gtk_tool_item_group_set_label (group, g_value_get_string (value));
433 case PROP_LABEL_WIDGET:
434 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
438 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
442 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
446 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
450 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
456 gtk_tool_item_group_get_property (GObject *object,
461 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
466 g_value_set_string (value, gtk_tool_item_group_get_label (group));
469 case PROP_LABEL_WIDGET:
470 g_value_set_object (value,
471 gtk_tool_item_group_get_label_widget (group));
475 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
479 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
483 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
487 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
493 gtk_tool_item_group_finalize (GObject *object)
495 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
497 if (group->priv->children)
499 g_list_free (group->priv->children);
500 group->priv->children = NULL;
503 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
507 gtk_tool_item_group_dispose (GObject *object)
509 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
510 GtkToolItemGroupPrivate* priv = group->priv;
514 /* disconnect focus tracking handler */
515 g_signal_handler_disconnect (priv->toplevel,
518 priv->focus_set_id = 0;
519 priv->toplevel = NULL;
522 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
526 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
527 GtkRequisition *item_size,
528 gboolean homogeneous_only,
529 gint *requested_rows)
531 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
533 if (GTK_IS_TOOL_PALETTE (parent))
534 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
536 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
540 gtk_tool_item_group_size_request (GtkWidget *widget,
541 GtkRequisition *requisition)
543 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
544 GtkToolItemGroupPrivate* priv = group->priv;
545 GtkOrientation orientation;
546 GtkRequisition item_size;
550 if (priv->children && gtk_tool_item_group_get_label_widget (group))
552 gtk_widget_get_preferred_size (priv->header,
554 gtk_widget_show (priv->header);
558 requisition->width = requisition->height = 0;
559 gtk_widget_hide (priv->header);
562 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
564 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
566 if (GTK_ORIENTATION_VERTICAL == orientation)
567 requisition->width = MAX (requisition->width, item_size.width);
569 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
571 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
572 requisition->width += border_width * 2;
573 requisition->height += border_width * 2;
577 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
581 GtkRequisition requisition;
583 gtk_tool_item_group_size_request (widget, &requisition);
585 *minimum = *natural = requisition.width;
589 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
593 GtkRequisition requisition;
595 gtk_tool_item_group_size_request (widget, &requisition);
597 *minimum = *natural = requisition.height;
602 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
603 GtkToolItemGroupChild *child)
605 GtkToolbarStyle style;
606 GtkOrientation orientation;
608 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
609 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
611 /* horizontal tool palettes with text style support only homogeneous items */
612 if (!child->homogeneous &&
613 GTK_ORIENTATION_HORIZONTAL == orientation &&
614 GTK_TOOLBAR_TEXT == style)
618 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
619 (GTK_ORIENTATION_VERTICAL == orientation ?
620 gtk_tool_item_get_visible_vertical (child->item) :
621 gtk_tool_item_get_visible_horizontal (child->item));
624 static inline unsigned
628 return (x + y - 1) / y;
632 gtk_tool_item_group_real_size_query (GtkWidget *widget,
633 GtkAllocation *allocation,
634 GtkRequisition *inquery)
636 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
637 GtkToolItemGroupPrivate* priv = group->priv;
639 GtkRequisition item_size;
640 GtkAllocation item_area;
642 GtkOrientation orientation;
647 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
648 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
650 /* figure out the size of homogeneous items */
651 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
653 if (GTK_ORIENTATION_VERTICAL == orientation)
654 item_size.width = MIN (item_size.width, allocation->width);
656 item_size.height = MIN (item_size.height, allocation->height);
658 item_size.width = MAX (item_size.width, 1);
659 item_size.height = MAX (item_size.height, 1);
662 item_area.height = 0;
664 /* figure out the required columns (n_columns) and rows (n_rows)
667 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
673 if (GTK_ORIENTATION_VERTICAL == orientation)
675 gboolean new_row = FALSE;
679 item_area.width = allocation->width - 2 * border_width;
680 n_columns = MAX (item_area.width / item_size.width, 1);
682 /* calculate required rows for n_columns columns */
683 for (it = priv->children; it != NULL; it = it->next)
685 GtkToolItemGroupChild *child = it->data;
687 if (!gtk_tool_item_group_is_item_visible (group, child))
690 if (new_row || child->new_row)
700 if (child->homogeneous)
703 if (col >= n_columns)
708 GtkRequisition req = {0, 0};
711 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
714 width = udiv (req.width, item_size.width);
722 if (col >= n_columns)
730 guint *row_min_width;
732 gboolean new_row = TRUE;
733 guint col = 0, min_col, max_col = 0, all_items = 0;
736 item_area.height = allocation->height - 2 * border_width;
737 n_rows = MAX (item_area.height / item_size.height, min_rows);
739 row_min_width = g_new0 (guint, n_rows);
741 /* calculate minimal and maximal required cols and minimal
744 for (it = priv->children; it != NULL; it = it->next)
746 GtkToolItemGroupChild *child = it->data;
748 if (!gtk_tool_item_group_is_item_visible (group, child))
751 if (new_row || child->new_row)
756 row_min_width[row] = 1;
762 if (child->homogeneous)
769 GtkRequisition req = {0, 0};
772 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
775 width = udiv (req.width, item_size.width);
780 row_min_width[row] = MAX (row_min_width[row], width);
783 max_col = MAX (max_col, col);
786 /* calculate minimal required cols */
787 min_col = udiv (all_items, n_rows);
789 for (i = 0; i <= row; i++)
791 min_col = MAX (min_col, row_min_width[i]);
794 /* simple linear search for minimal required columns
795 * for the given maximal number of rows (n_rows)
797 for (n_columns = min_col; n_columns < max_col; n_columns ++)
801 /* calculate required rows for n_columns columns */
802 for (it = priv->children; it != NULL; it = it->next)
804 GtkToolItemGroupChild *child = it->data;
806 if (!gtk_tool_item_group_is_item_visible (group, child))
809 if (new_row || child->new_row)
819 if (child->homogeneous)
822 if (col >= n_columns)
827 GtkRequisition req = {0, 0};
830 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
833 width = udiv (req.width, item_size.width);
841 if (col >= n_columns)
851 item_area.width = item_size.width * n_columns;
852 item_area.height = item_size.height * n_rows;
858 /* figure out header widget size */
859 if (gtk_widget_get_visible (priv->header))
861 GtkRequisition child_requisition;
863 gtk_widget_get_preferred_size (priv->header,
864 &child_requisition, NULL);
866 if (GTK_ORIENTATION_VERTICAL == orientation)
867 inquery->height += child_requisition.height;
869 inquery->width += child_requisition.width;
872 /* report effective widget size */
873 inquery->width += item_area.width + 2 * border_width;
874 inquery->height += item_area.height + 2 * border_width;
878 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
879 GtkAllocation *allocation)
881 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
882 GtkToolItemGroupPrivate* priv = group->priv;
883 GtkRequisition child_requisition;
884 GtkAllocation child_allocation;
886 GtkRequisition item_size;
887 GtkAllocation item_area;
889 GtkOrientation orientation;
893 gint n_columns, n_rows = 1;
896 GtkTextDirection direction;
898 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
900 direction = gtk_widget_get_direction (widget);
902 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
905 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
907 child_allocation.x = border_width;
908 child_allocation.y = border_width;
910 /* place the header widget */
911 if (gtk_widget_get_visible (priv->header))
913 gtk_widget_get_preferred_size (priv->header,
914 &child_requisition, NULL);
916 if (GTK_ORIENTATION_VERTICAL == orientation)
918 child_allocation.width = allocation->width;
919 child_allocation.height = child_requisition.height;
923 child_allocation.width = child_requisition.width;
924 child_allocation.height = allocation->height;
926 if (GTK_TEXT_DIR_RTL == direction)
927 child_allocation.x = allocation->width - border_width - child_allocation.width;
930 gtk_widget_size_allocate (priv->header, &child_allocation);
932 if (GTK_ORIENTATION_VERTICAL == orientation)
933 child_allocation.y += child_allocation.height;
934 else if (GTK_TEXT_DIR_RTL != direction)
935 child_allocation.x += child_allocation.width;
937 child_allocation.x = border_width;
940 child_requisition.width = child_requisition.height = 0;
942 /* figure out the size of homogeneous items */
943 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
945 item_size.width = MAX (item_size.width, 1);
946 item_size.height = MAX (item_size.height, 1);
948 /* figure out the available columns and size of item_area */
949 if (GTK_ORIENTATION_VERTICAL == orientation)
951 item_size.width = MIN (item_size.width, allocation->width);
953 item_area.width = allocation->width - 2 * border_width;
954 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
956 n_columns = MAX (item_area.width / item_size.width, 1);
958 item_size.width = item_area.width / n_columns;
962 item_size.height = MIN (item_size.height, allocation->height);
964 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
965 item_area.height = allocation->height - 2 * border_width;
967 n_columns = MAX (item_area.width / item_size.width, 1);
968 n_rows = MAX (item_area.height / item_size.height, min_rows);
970 item_size.height = item_area.height / n_rows;
973 item_area.x = child_allocation.x;
974 item_area.y = child_allocation.y;
976 /* when expanded or in transition, place the tool items in a grid like layout */
977 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
979 gint col = 0, row = 0;
981 for (it = priv->children; it != NULL; it = it->next)
983 GtkToolItemGroupChild *child = it->data;
986 if (!gtk_tool_item_group_is_item_visible (group, child))
988 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
993 /* for non homogeneous widgets request the required size */
994 child_requisition.width = 0;
996 if (!child->homogeneous)
998 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
999 &child_requisition, NULL);
1000 child_requisition.width = MIN (child_requisition.width, item_area.width);
1003 /* select next row if at end of row */
1004 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
1008 child_allocation.y += child_allocation.height;
1013 /* calculate the position and size of the item */
1014 if (!child->homogeneous)
1020 col_width = udiv (child_requisition.width, item_size.width);
1022 col_width = n_columns - col;
1024 width = col_width * item_size.width;
1026 if (GTK_TEXT_DIR_RTL == direction)
1027 col_child = (n_columns - col - col_width);
1031 child_allocation.x = item_area.x + col_child * item_size.width;
1032 child_allocation.width = width;
1036 child_allocation.x =
1037 (item_area.x + col_child * item_size.width +
1038 (width - child_requisition.width) / 2);
1039 child_allocation.width = child_requisition.width;
1046 if (GTK_TEXT_DIR_RTL == direction)
1047 col_child = (n_columns - col - 1);
1049 child_allocation.x = item_area.x + col_child * item_size.width;
1050 child_allocation.width = item_size.width;
1055 child_allocation.height = item_size.height;
1057 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1058 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1061 child_allocation.y += item_size.height;
1064 /* or just hide all items, when collapsed */
1068 for (it = priv->children; it != NULL; it = it->next)
1070 GtkToolItemGroupChild *child = it->data;
1072 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1078 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1079 GtkAllocation *allocation)
1081 gtk_tool_item_group_real_size_allocate (widget, allocation);
1083 if (gtk_widget_get_mapped (widget))
1084 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1088 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1092 GtkAdjustment *adjustment;
1093 GtkAllocation allocation, p_allocation;
1096 /* Find this group's parent widget in the focused widget's anchestry. */
1097 for (p = widget; p; p = gtk_widget_get_parent (p))
1100 p = gtk_widget_get_parent (p);
1104 if (GTK_IS_TOOL_PALETTE (p))
1106 /* Check that the focused widgets is fully visible within
1107 * the group's parent widget and make it visible otherwise. */
1109 adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1115 gtk_widget_get_allocation (widget, &allocation);
1116 gtk_widget_get_allocation (p, &p_allocation);
1118 /* Handle vertical adjustment. */
1119 if (gtk_widget_translate_coordinates
1120 (widget, p, 0, 0, NULL, &y) && y < 0)
1122 y += gtk_adjustment_get_value (adjustment);
1123 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1125 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1126 y > p_allocation.height)
1128 y += gtk_adjustment_get_value (adjustment);
1129 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1133 adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1139 gtk_widget_get_allocation (widget, &allocation);
1140 gtk_widget_get_allocation (p, &p_allocation);
1142 /* Handle horizontal adjustment. */
1143 if (gtk_widget_translate_coordinates
1144 (widget, p, 0, 0, &x, NULL) && x < 0)
1146 x += gtk_adjustment_get_value (adjustment);
1147 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1149 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1150 x > p_allocation.width)
1152 x += gtk_adjustment_get_value (adjustment);
1153 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1162 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1163 GtkWidget *toplevel)
1165 GtkToolItemGroupPrivate* priv = group->priv;
1167 if (toplevel != priv->toplevel)
1171 /* Disconnect focus tracking handler. */
1172 g_signal_handler_disconnect (priv->toplevel,
1173 priv->focus_set_id);
1175 priv->focus_set_id = 0;
1176 priv->toplevel = NULL;
1181 /* Install focus tracking handler. We connect to the window's
1182 * set-focus signal instead of connecting to the focus signal of
1185 * 1) Reduce the number of signal handlers used.
1186 * 2) Avoid special handling for group headers.
1187 * 3) Catch focus grabs not only for direct children,
1188 * but also for nested widgets.
1190 priv->focus_set_id =
1191 g_signal_connect (toplevel, "set-focus",
1192 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1195 priv->toplevel = toplevel;
1201 gtk_tool_item_group_realize (GtkWidget *widget)
1203 GtkAllocation allocation;
1204 GtkWidget *toplevel_window;
1206 GdkWindowAttr attributes;
1207 gint attributes_mask;
1209 GtkStyleContext *context;
1211 gtk_widget_set_realized (widget, TRUE);
1213 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1214 context = gtk_widget_get_style_context (widget);
1216 gtk_widget_get_allocation (widget, &allocation);
1218 attributes.window_type = GDK_WINDOW_CHILD;
1219 attributes.x = allocation.x + border_width;
1220 attributes.y = allocation.y + border_width;
1221 attributes.width = allocation.width - border_width * 2;
1222 attributes.height = allocation.height - border_width * 2;
1223 attributes.wclass = GDK_INPUT_OUTPUT;
1224 attributes.visual = gtk_widget_get_visual (widget);
1225 attributes.event_mask = gtk_widget_get_events (widget)
1226 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1227 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1228 | GDK_BUTTON_MOTION_MASK;
1229 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1231 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1232 &attributes, attributes_mask);
1233 gtk_widget_set_window (widget, window);
1235 gdk_window_set_user_data (window, widget);
1237 gtk_style_context_set_background (context, window);
1239 gtk_container_forall (GTK_CONTAINER (widget),
1240 (GtkCallback) gtk_widget_set_parent_window,
1243 gtk_widget_queue_resize_no_redraw (widget);
1245 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1246 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1251 gtk_tool_item_group_unrealize (GtkWidget *widget)
1253 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1254 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1258 gtk_tool_item_group_style_updated (GtkWidget *widget)
1260 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1261 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
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_updated = gtk_tool_item_group_style_updated;
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 gtk_tool_item_group_force_expose (group);
1871 /* Finish animation when done. */
1872 if (timestamp >= ANIMATION_DURATION)
1873 priv->animation_timeout = NULL;
1875 retval = (priv->animation_timeout != NULL);
1877 gdk_threads_leave ();
1883 * gtk_tool_item_group_set_collapsed:
1884 * @group: a #GtkToolItemGroup
1885 * @collapsed: whether the @group should be collapsed or expanded
1887 * Sets whether the @group should be collapsed or expanded.
1892 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1896 GtkToolItemGroupPrivate* priv;
1898 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1902 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1903 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1904 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1905 GTK_WIDGET (group));
1906 if (collapsed != priv->collapsed)
1908 if (priv->animation)
1910 if (priv->animation_timeout)
1911 g_source_destroy (priv->animation_timeout);
1913 priv->animation_start = g_get_monotonic_time ();
1914 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1916 g_source_set_callback (priv->animation_timeout,
1917 gtk_tool_item_group_animation_cb,
1919 g_source_attach (priv->animation_timeout, NULL);
1922 gtk_tool_item_group_force_expose (group);
1924 priv->collapsed = collapsed;
1925 g_object_notify (G_OBJECT (group), "collapsed");
1930 * gtk_tool_item_group_set_ellipsize:
1931 * @group: a #GtkToolItemGroup
1932 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1934 * Sets the ellipsization mode which should be used by labels in @group.
1939 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1940 PangoEllipsizeMode ellipsize)
1942 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1944 if (ellipsize != group->priv->ellipsize)
1946 group->priv->ellipsize = ellipsize;
1947 gtk_tool_item_group_header_adjust_style (group);
1948 g_object_notify (G_OBJECT (group), "ellipsize");
1949 _gtk_tool_item_group_palette_reconfigured (group);
1954 * gtk_tool_item_group_get_label:
1955 * @group: a #GtkToolItemGroup
1957 * Gets the label of @group.
1959 * Returns: the label of @group. The label is an internal string of @group
1960 * and must not be modified. Note that %NULL is returned if a custom
1961 * label has been set with gtk_tool_item_group_set_label_widget()
1966 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1968 GtkToolItemGroupPrivate *priv;
1970 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1974 if (GTK_IS_LABEL (priv->label_widget))
1975 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1981 * gtk_tool_item_group_get_label_widget:
1982 * @group: a #GtkToolItemGroup
1984 * Gets the label widget of @group.
1985 * See gtk_tool_item_group_set_label_widget().
1987 * Returns: (transfer none): the label widget of @group
1992 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
1994 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1996 return gtk_bin_get_child (GTK_BIN (alignment));
2000 * gtk_tool_item_group_get_collapsed:
2001 * @group: a GtkToolItemGroup
2003 * Gets whether @group is collapsed or expanded.
2005 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2010 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2012 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2014 return group->priv->collapsed;
2018 * gtk_tool_item_group_get_ellipsize:
2019 * @group: a #GtkToolItemGroup
2021 * Gets the ellipsization mode of @group.
2023 * Returns: the #PangoEllipsizeMode of @group
2028 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2030 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2032 return group->priv->ellipsize;
2036 * gtk_tool_item_group_get_header_relief:
2037 * @group: a #GtkToolItemGroup
2039 * Gets the relief mode of the header button of @group.
2041 * Returns: the #GtkReliefStyle
2046 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2048 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2050 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2054 * gtk_tool_item_group_insert:
2055 * @group: a #GtkToolItemGroup
2056 * @item: the #GtkToolItem to insert into @group
2057 * @position: the position of @item in @group, starting with 0.
2058 * The position -1 means end of list.
2060 * Inserts @item at @position in the list of children of @group.
2065 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2069 GtkWidget *parent, *child_widget;
2070 GtkToolItemGroupChild *child;
2072 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2073 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2074 g_return_if_fail (position >= -1);
2076 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2078 child = g_new (GtkToolItemGroupChild, 1);
2079 child->item = g_object_ref_sink (item);
2080 child->homogeneous = TRUE;
2081 child->expand = FALSE;
2083 child->new_row = FALSE;
2085 group->priv->children = g_list_insert (group->priv->children, child, position);
2087 if (GTK_IS_TOOL_PALETTE (parent))
2088 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2090 child_widget = gtk_bin_get_child (GTK_BIN (item));
2092 if (GTK_IS_BUTTON (child_widget))
2093 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2095 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2099 * gtk_tool_item_group_set_item_position:
2100 * @group: a #GtkToolItemGroup
2101 * @item: the #GtkToolItem to move to a new position, should
2102 * be a child of @group.
2103 * @position: the new position of @item in @group, starting with 0.
2104 * The position -1 means end of list.
2106 * Sets the position of @item in the list of children of @group.
2111 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2117 GtkToolItemGroupChild *child;
2118 GtkToolItemGroupPrivate* priv;
2120 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2121 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2122 g_return_if_fail (position >= -1);
2124 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2127 g_return_if_fail (child != NULL);
2129 if (position == old_position)
2132 priv->children = g_list_delete_link (priv->children, link);
2133 priv->children = g_list_insert (priv->children, child, position);
2135 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2136 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2137 gtk_widget_get_visible (GTK_WIDGET (item)))
2138 gtk_widget_queue_resize (GTK_WIDGET (group));
2142 * gtk_tool_item_group_get_item_position:
2143 * @group: a #GtkToolItemGroup
2144 * @item: a #GtkToolItem
2146 * Gets the position of @item in @group as index.
2148 * Returns: the index of @item in @group or -1 if @item is no child of @group
2153 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2158 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2159 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2161 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2168 * gtk_tool_item_group_get_n_items:
2169 * @group: a #GtkToolItemGroup
2171 * Gets the number of tool items in @group.
2173 * Returns: the number of tool items in @group
2178 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2180 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2182 return g_list_length (group->priv->children);
2186 * gtk_tool_item_group_get_nth_item:
2187 * @group: a #GtkToolItemGroup
2190 * Gets the tool item at @index in group.
2192 * Returns: (transfer none): the #GtkToolItem at index
2197 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2200 GtkToolItemGroupChild *child;
2202 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2204 child = g_list_nth_data (group->priv->children, index);
2206 return child != NULL ? child->item : NULL;
2210 * gtk_tool_item_group_get_drop_item:
2211 * @group: a #GtkToolItemGroup
2212 * @x: the x position
2213 * @y: the y position
2215 * Gets the tool item at position (x, y).
2217 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2222 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2226 GtkAllocation allocation;
2229 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2231 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2233 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2234 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2236 for (it = group->priv->children; it != NULL; it = it->next)
2238 GtkToolItemGroupChild *child = it->data;
2239 GtkToolItem *item = child->item;
2242 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2245 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2247 x0 = x - allocation.x;
2248 y0 = y - allocation.y;
2250 if (x0 >= 0 && x0 < allocation.width &&
2251 y0 >= 0 && y0 < allocation.height)
2259 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2260 GtkRequisition *item_size,
2261 gboolean homogeneous_only,
2262 gint *requested_rows)
2264 GtkRequisition child_requisition;
2267 gboolean new_row = TRUE;
2269 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2270 g_return_if_fail (NULL != item_size);
2272 item_size->width = item_size->height = 0;
2274 for (it = group->priv->children; it != NULL; it = it->next)
2276 GtkToolItemGroupChild *child = it->data;
2278 if (!gtk_tool_item_group_is_item_visible (group, child))
2281 if (child->new_row || new_row)
2287 if (!child->homogeneous && child->expand)
2290 gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2291 &child_requisition, NULL);
2293 if (!homogeneous_only || child->homogeneous)
2294 item_size->width = MAX (item_size->width, child_requisition.width);
2295 item_size->height = MAX (item_size->height, child_requisition.height);
2299 *requested_rows = rows;
2303 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2306 GtkAllocation allocation;
2307 GtkWidget *widget = GTK_WIDGET (group);
2308 GtkToolItemGroupPrivate* priv = group->priv;
2310 gtk_widget_get_allocation (widget, &allocation);
2312 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2316 if (priv->animation_timeout)
2318 GtkAllocation header_allocation;
2319 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2320 cairo_pattern_t *mask;
2323 if (GTK_ORIENTATION_VERTICAL == orientation)
2324 v1 = allocation.height;
2326 v1 = allocation.width;
2330 gtk_widget_get_allocation (priv->header, &header_allocation);
2331 if (!gtk_widget_get_visible (priv->header))
2333 else if (GTK_ORIENTATION_VERTICAL == orientation)
2334 v0 = MAX (v0, header_allocation.height);
2336 v0 = MAX (v0, header_allocation.width);
2338 v1 = MIN (v0 + 256, v1);
2340 if (GTK_ORIENTATION_VERTICAL == orientation)
2345 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2352 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2355 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2356 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2357 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2358 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2359 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2361 cairo_mask (cr, mask);
2362 cairo_pattern_destroy (mask);
2369 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2374 GtkRequisition requisition;
2375 GtkToolItemGroupPrivate* priv = group->priv;
2377 gtk_widget_get_preferred_size (GTK_WIDGET (group),
2378 &requisition, NULL);
2380 if (!priv->collapsed || priv->animation_timeout)
2382 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2383 GtkRequisition inquery;
2386 allocation.width = limit;
2388 allocation.height = limit;
2390 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2391 &allocation, &inquery);
2394 inquery.height -= requisition.height;
2396 inquery.width -= requisition.width;
2398 if (priv->animation_timeout && animation)
2400 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2402 timestamp = MIN (timestamp, ANIMATION_DURATION);
2404 if (priv->collapsed)
2405 timestamp = ANIMATION_DURATION - timestamp;
2409 inquery.height *= timestamp;
2410 inquery.height /= ANIMATION_DURATION;
2414 inquery.width *= timestamp;
2415 inquery.width /= ANIMATION_DURATION;
2420 requisition.height += inquery.height;
2422 requisition.width += inquery.width;
2425 return (vertical ? requisition.height : requisition.width);
2429 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2432 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2436 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2439 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2443 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2446 if (GTK_IS_TOOL_ITEM (child))
2447 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2452 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2454 gtk_container_foreach (GTK_CONTAINER (group),
2455 gtk_tool_palette_reconfigured_foreach_item,
2458 gtk_tool_item_group_header_adjust_style (group);