1 /* GtkToolPalette -- A tool palette with categories and DnD support
2 * Copyright (C) 2008 Openismus GmbH
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "gtktoolpaletteprivate.h"
30 #include "gtkprivate.h"
33 #define ANIMATION_TIMEOUT 50
34 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
35 #define DEFAULT_ANIMATION_STATE TRUE
36 #define DEFAULT_EXPANDER_SIZE 16
37 #define DEFAULT_HEADER_SPACING 2
39 #define DEFAULT_LABEL ""
40 #define DEFAULT_COLLAPSED FALSE
41 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
44 * SECTION:gtktoolitemgroup
45 * @Short_description: A sub container used in a tool palette
46 * @Title: GtkToolItemGroup
48 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
49 * #GtkToolItem<!-- -->s to a palette like container with different
50 * categories and drag and drop support.
68 CHILD_PROP_HOMOGENEOUS,
75 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
77 struct _GtkToolItemGroupPrivate
80 GtkWidget *label_widget;
85 gint64 animation_start;
86 GSource *animation_timeout;
87 GtkExpanderStyle expander_style;
90 PangoEllipsizeMode ellipsize;
95 GtkSettings *settings;
96 gulong settings_connection;
101 struct _GtkToolItemGroupChild
105 guint homogeneous : 1;
111 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
113 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
117 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
119 return gtk_bin_get_child (GTK_BIN (group->priv->header));
122 static GtkOrientation
123 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
125 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
127 if (GTK_IS_TOOL_PALETTE (parent))
128 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
130 return GTK_ORIENTATION_VERTICAL;
133 static GtkToolbarStyle
134 gtk_tool_item_group_get_style (GtkToolShell *shell)
136 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
138 if (GTK_IS_TOOL_PALETTE (parent))
139 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
141 return GTK_TOOLBAR_ICONS;
145 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
147 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
149 if (GTK_IS_TOOL_PALETTE (parent))
150 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
152 return GTK_ICON_SIZE_SMALL_TOOLBAR;
155 static PangoEllipsizeMode
156 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
158 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
162 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
164 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
165 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
171 static GtkOrientation
172 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
174 return GTK_ORIENTATION_HORIZONTAL;
177 static GtkSizeGroup *
178 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
180 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
182 if (GTK_IS_TOOL_PALETTE (parent))
183 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
189 animation_change_notify (GtkToolItemGroup *group)
191 GtkSettings *settings = group->priv->settings;
195 g_object_get (settings,
196 "gtk-enable-animations", &animation,
199 animation = DEFAULT_ANIMATION_STATE;
201 group->priv->animation = animation;
205 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
206 const GParamSpec *pspec,
207 GtkToolItemGroup *group)
209 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
210 animation_change_notify (group);
214 gtk_tool_item_group_screen_changed (GtkWidget *widget,
215 GdkScreen *previous_screen)
217 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
218 GtkToolItemGroupPrivate* priv = group->priv;
219 GtkSettings *old_settings = priv->settings;
220 GtkSettings *settings;
222 if (gtk_widget_has_screen (GTK_WIDGET (group)))
223 settings = gtk_widget_get_settings (GTK_WIDGET (group));
227 if (settings == old_settings)
232 g_signal_handler_disconnect (old_settings, priv->settings_connection);
233 g_object_unref (old_settings);
238 priv->settings_connection =
239 g_signal_connect (settings, "notify",
240 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
242 priv->settings = g_object_ref (settings);
245 priv->settings = NULL;
247 animation_change_notify (group);
251 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
253 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
254 iface->get_orientation = gtk_tool_item_group_get_orientation;
255 iface->get_style = gtk_tool_item_group_get_style;
256 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
257 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
258 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
259 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
263 gtk_tool_item_group_header_expose_event_cb (GtkWidget *widget,
264 GdkEventExpose *event,
267 GtkAllocation allocation;
268 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
269 GtkToolItemGroupPrivate* priv = group->priv;
270 GtkExpanderStyle expander_style;
271 GtkOrientation orientation;
273 GtkTextDirection direction;
275 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
276 expander_style = priv->expander_style;
277 direction = gtk_widget_get_direction (widget);
279 gtk_widget_get_allocation (widget, &allocation);
280 if (GTK_ORIENTATION_VERTICAL == orientation)
282 if (GTK_TEXT_DIR_RTL == direction)
283 x = allocation.x + allocation.width - priv->expander_size / 2;
285 x = allocation.x + priv->expander_size / 2;
287 y = allocation.y + allocation.height / 2;
291 x = allocation.x + allocation.width / 2;
292 y = allocation.y + priv->expander_size / 2;
294 /* Unfortunatly gtk_paint_expander() doesn't support rotated drawing
295 * modes. Luckily the following shady arithmetics produce the desired
297 expander_style = GTK_EXPANDER_EXPANDED - expander_style;
300 gtk_paint_expander (gtk_widget_get_style (widget),
301 gtk_widget_get_window (widget),
302 gtk_widget_get_state (priv->header),
303 &event->area, GTK_WIDGET (group),
304 "tool-palette-header", x, y,
311 gtk_tool_item_group_header_size_request_cb (GtkWidget *widget,
312 GtkRequisition *requisition,
315 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
316 requisition->height = MAX (requisition->height, group->priv->expander_size);
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 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
350 case GTK_ORIENTATION_HORIZONTAL:
351 dy = priv->header_spacing + priv->expander_size;
353 if (GTK_IS_LABEL (label_widget))
355 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
356 if (GTK_TEXT_DIR_RTL == direction)
357 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
359 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
363 case GTK_ORIENTATION_VERTICAL:
364 dx = priv->header_spacing + priv->expander_size;
366 if (GTK_IS_LABEL (label_widget))
368 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
369 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
374 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
378 gtk_tool_item_group_init (GtkToolItemGroup *group)
380 GtkWidget *alignment;
381 GtkToolItemGroupPrivate* priv;
383 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
385 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
386 GTK_TYPE_TOOL_ITEM_GROUP,
387 GtkToolItemGroupPrivate);
389 priv->children = NULL;
390 priv->header_spacing = DEFAULT_HEADER_SPACING;
391 priv->expander_size = DEFAULT_EXPANDER_SIZE;
392 priv->expander_style = GTK_EXPANDER_EXPANDED;
394 priv->label_widget = gtk_label_new (NULL);
395 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
396 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
397 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
398 gtk_widget_show_all (alignment);
400 gtk_widget_push_composite_child ();
401 priv->header = gtk_button_new ();
402 gtk_widget_set_composite_name (priv->header, "header");
403 gtk_widget_pop_composite_child ();
405 g_object_ref_sink (priv->header);
406 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
407 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
408 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
410 gtk_tool_item_group_header_adjust_style (group);
412 g_signal_connect_after (alignment, "expose-event",
413 G_CALLBACK (gtk_tool_item_group_header_expose_event_cb),
415 g_signal_connect_after (alignment, "size-request",
416 G_CALLBACK (gtk_tool_item_group_header_size_request_cb),
419 g_signal_connect (priv->header, "clicked",
420 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
425 gtk_tool_item_group_set_property (GObject *object,
430 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
435 gtk_tool_item_group_set_label (group, g_value_get_string (value));
438 case PROP_LABEL_WIDGET:
439 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
443 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
447 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
451 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
455 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
461 gtk_tool_item_group_get_property (GObject *object,
466 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
471 g_value_set_string (value, gtk_tool_item_group_get_label (group));
474 case PROP_LABEL_WIDGET:
475 g_value_set_object (value,
476 gtk_tool_item_group_get_label_widget (group));
480 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
484 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
488 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
492 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
498 gtk_tool_item_group_finalize (GObject *object)
500 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
502 if (group->priv->children)
504 g_list_free (group->priv->children);
505 group->priv->children = NULL;
508 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
512 gtk_tool_item_group_dispose (GObject *object)
514 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
515 GtkToolItemGroupPrivate* priv = group->priv;
519 /* disconnect focus tracking handler */
520 g_signal_handler_disconnect (priv->toplevel,
523 priv->focus_set_id = 0;
524 priv->toplevel = NULL;
527 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
531 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
532 GtkRequisition *item_size,
533 gboolean homogeneous_only,
534 gint *requested_rows)
536 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
538 if (GTK_IS_TOOL_PALETTE (parent))
539 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
541 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
545 gtk_tool_item_group_size_request (GtkWidget *widget,
546 GtkRequisition *requisition)
548 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
549 GtkToolItemGroupPrivate* priv = group->priv;
550 GtkOrientation orientation;
551 GtkRequisition item_size;
555 if (priv->children && gtk_tool_item_group_get_label_widget (group))
557 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->header),
559 gtk_widget_show (priv->header);
563 requisition->width = requisition->height = 0;
564 gtk_widget_hide (priv->header);
567 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
569 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
571 if (GTK_ORIENTATION_VERTICAL == orientation)
572 requisition->width = MAX (requisition->width, item_size.width);
574 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
576 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
577 requisition->width += border_width * 2;
578 requisition->height += border_width * 2;
582 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
583 GtkToolItemGroupChild *child)
585 GtkToolbarStyle style;
586 GtkOrientation orientation;
588 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
589 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
591 /* horizontal tool palettes with text style support only homogeneous items */
592 if (!child->homogeneous &&
593 GTK_ORIENTATION_HORIZONTAL == orientation &&
594 GTK_TOOLBAR_TEXT == style)
598 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
599 (GTK_ORIENTATION_VERTICAL == orientation ?
600 gtk_tool_item_get_visible_vertical (child->item) :
601 gtk_tool_item_get_visible_horizontal (child->item));
604 static inline unsigned
608 return (x + y - 1) / y;
612 gtk_tool_item_group_real_size_query (GtkWidget *widget,
613 GtkAllocation *allocation,
614 GtkRequisition *inquery)
616 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
617 GtkToolItemGroupPrivate* priv = group->priv;
619 GtkRequisition item_size;
620 GtkAllocation item_area;
622 GtkOrientation orientation;
623 GtkToolbarStyle style;
628 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
629 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
630 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
632 /* figure out the size of homogeneous items */
633 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
635 if (GTK_ORIENTATION_VERTICAL == orientation)
636 item_size.width = MIN (item_size.width, allocation->width);
638 item_size.height = MIN (item_size.height, allocation->height);
640 item_size.width = MAX (item_size.width, 1);
641 item_size.height = MAX (item_size.height, 1);
644 item_area.height = 0;
646 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
647 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
653 if (GTK_ORIENTATION_VERTICAL == orientation)
655 gboolean new_row = FALSE;
659 item_area.width = allocation->width - 2 * border_width;
660 n_columns = MAX (item_area.width / item_size.width, 1);
662 /* calculate required rows for n_columns columns */
663 for (it = priv->children; it != NULL; it = it->next)
665 GtkToolItemGroupChild *child = it->data;
667 if (!gtk_tool_item_group_is_item_visible (group, child))
670 if (new_row || child->new_row)
680 if (child->homogeneous)
683 if (col >= n_columns)
688 GtkRequisition req = {0, 0};
691 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->item),
694 width = udiv (req.width, item_size.width);
702 if (col >= n_columns)
710 guint *row_min_width;
712 gboolean new_row = TRUE;
713 guint col = 0, min_col, max_col = 0, all_items = 0;
716 item_area.height = allocation->height - 2 * border_width;
717 n_rows = MAX (item_area.height / item_size.height, min_rows);
719 row_min_width = g_new0 (guint, n_rows);
721 /* calculate minimal and maximal required cols and minimal required rows */
722 for (it = priv->children; it != NULL; it = it->next)
724 GtkToolItemGroupChild *child = it->data;
726 if (!gtk_tool_item_group_is_item_visible (group, child))
729 if (new_row || child->new_row)
734 row_min_width[row] = 1;
740 if (child->homogeneous)
747 GtkRequisition req = {0, 0};
750 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->item),
753 width = udiv (req.width, item_size.width);
758 row_min_width[row] = MAX (row_min_width[row], width);
761 max_col = MAX (max_col, col);
764 /* calculate minimal required cols */
765 min_col = udiv (all_items, n_rows);
767 for (i = 0; i <= row; i++)
769 min_col = MAX (min_col, row_min_width[i]);
772 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
773 for (n_columns = min_col; n_columns < max_col; n_columns ++)
777 /* calculate required rows for n_columns columns */
778 for (it = priv->children; it != NULL; it = it->next)
780 GtkToolItemGroupChild *child = it->data;
782 if (!gtk_tool_item_group_is_item_visible (group, child))
785 if (new_row || child->new_row)
795 if (child->homogeneous)
798 if (col >= n_columns)
803 GtkRequisition req = {0, 0};
806 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->item),
809 width = udiv (req.width, item_size.width);
817 if (col >= n_columns)
827 item_area.width = item_size.width * n_columns;
828 item_area.height = item_size.height * n_rows;
834 /* figure out header widget size */
835 if (gtk_widget_get_visible (priv->header))
837 GtkRequisition child_requisition;
839 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->header),
840 &child_requisition, NULL);
842 if (GTK_ORIENTATION_VERTICAL == orientation)
843 inquery->height += child_requisition.height;
845 inquery->width += child_requisition.width;
848 /* report effective widget size */
849 inquery->width += item_area.width + 2 * border_width;
850 inquery->height += item_area.height + 2 * border_width;
854 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
855 GtkAllocation *allocation)
857 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
858 GtkToolItemGroupPrivate* priv = group->priv;
859 GtkRequisition child_requisition;
860 GtkAllocation child_allocation;
862 GtkRequisition item_size;
863 GtkAllocation item_area;
865 GtkOrientation orientation;
866 GtkToolbarStyle style;
870 gint n_columns, n_rows = 1;
874 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
876 GtkTextDirection direction = gtk_widget_get_direction (widget);
878 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
879 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
882 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
884 child_allocation.x = border_width;
885 child_allocation.y = border_width;
887 /* place the header widget */
888 if (gtk_widget_get_visible (priv->header))
890 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->header),
891 &child_requisition, NULL);
893 if (GTK_ORIENTATION_VERTICAL == orientation)
895 child_allocation.width = allocation->width;
896 child_allocation.height = child_requisition.height;
900 child_allocation.width = child_requisition.width;
901 child_allocation.height = allocation->height;
903 if (GTK_TEXT_DIR_RTL == direction)
904 child_allocation.x = allocation->width - border_width - child_allocation.width;
907 gtk_widget_size_allocate (priv->header, &child_allocation);
909 if (GTK_ORIENTATION_VERTICAL == orientation)
910 child_allocation.y += child_allocation.height;
911 else if (GTK_TEXT_DIR_RTL != direction)
912 child_allocation.x += child_allocation.width;
914 child_allocation.x = border_width;
917 child_requisition.width = child_requisition.height = 0;
919 /* figure out the size of homogeneous items */
920 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
922 item_size.width = MAX (item_size.width, 1);
923 item_size.height = MAX (item_size.height, 1);
925 /* figure out the available columns and size of item_area */
926 if (GTK_ORIENTATION_VERTICAL == orientation)
928 item_size.width = MIN (item_size.width, allocation->width);
930 item_area.width = allocation->width - 2 * border_width;
931 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
933 n_columns = MAX (item_area.width / item_size.width, 1);
935 item_size.width = item_area.width / n_columns;
939 item_size.height = MIN (item_size.height, allocation->height);
941 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
942 item_area.height = allocation->height - 2 * border_width;
944 n_columns = MAX (item_area.width / item_size.width, 1);
945 n_rows = MAX (item_area.height / item_size.height, min_rows);
947 item_size.height = item_area.height / n_rows;
950 item_area.x = child_allocation.x;
951 item_area.y = child_allocation.y;
953 /* when expanded or in transition, place the tool items in a grid like layout */
954 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
956 gint col = 0, row = 0;
958 for (it = priv->children; it != NULL; it = it->next)
960 GtkToolItemGroupChild *child = it->data;
963 if (!gtk_tool_item_group_is_item_visible (group, child))
965 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
970 /* for non homogeneous widgets request the required size */
971 child_requisition.width = 0;
973 if (!child->homogeneous)
975 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->item),
976 &child_requisition, NULL);
977 child_requisition.width = MIN (child_requisition.width, item_area.width);
980 /* select next row if at end of row */
981 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
985 child_allocation.y += child_allocation.height;
990 /* calculate the position and size of the item */
991 if (!child->homogeneous)
997 col_width = udiv (child_requisition.width, item_size.width);
999 col_width = n_columns - col;
1001 width = col_width * item_size.width;
1003 if (GTK_TEXT_DIR_RTL == direction)
1004 col_child = (n_columns - col - col_width);
1008 child_allocation.x = item_area.x + col_child * item_size.width;
1009 child_allocation.width = width;
1013 child_allocation.x =
1014 (item_area.x + col_child * item_size.width +
1015 (width - child_requisition.width) / 2);
1016 child_allocation.width = child_requisition.width;
1023 if (GTK_TEXT_DIR_RTL == direction)
1024 col_child = (n_columns - col - 1);
1026 child_allocation.x = item_area.x + col_child * item_size.width;
1027 child_allocation.width = item_size.width;
1032 child_allocation.height = item_size.height;
1034 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1035 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1038 child_allocation.y += item_size.height;
1041 /* or just hide all items, when collapsed */
1045 for (it = priv->children; it != NULL; it = it->next)
1047 GtkToolItemGroupChild *child = it->data;
1049 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1055 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1056 GtkAllocation *allocation)
1058 gtk_tool_item_group_real_size_allocate (widget, allocation);
1060 if (gtk_widget_get_mapped (widget))
1061 gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1065 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1069 GtkAdjustment *adjustment;
1070 GtkAllocation allocation, p_allocation;
1073 /* Find this group's parent widget in the focused widget's anchestry. */
1074 for (p = widget; p; p = gtk_widget_get_parent (p))
1077 p = gtk_widget_get_parent (p);
1081 if (GTK_IS_TOOL_PALETTE (p))
1083 /* Check that the focused widgets is fully visible within
1084 * the group's parent widget and make it visible otherwise. */
1086 adjustment = gtk_tool_palette_get_vadjustment (GTK_TOOL_PALETTE (p));
1092 gtk_widget_get_allocation (widget, &allocation);
1093 gtk_widget_get_allocation (p, &p_allocation);
1095 /* Handle vertical adjustment. */
1096 if (gtk_widget_translate_coordinates
1097 (widget, p, 0, 0, NULL, &y) && y < 0)
1099 y += adjustment->value;
1100 gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1102 else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1103 y > p_allocation.height)
1105 y += adjustment->value;
1106 gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1110 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1116 gtk_widget_get_allocation (widget, &allocation);
1117 gtk_widget_get_allocation (p, &p_allocation);
1119 /* Handle horizontal adjustment. */
1120 if (gtk_widget_translate_coordinates
1121 (widget, p, 0, 0, &x, NULL) && x < 0)
1123 x += adjustment->value;
1124 gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1126 else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1127 x > p_allocation.width)
1129 x += adjustment->value;
1130 gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1139 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1140 GtkWidget *toplevel)
1142 GtkToolItemGroupPrivate* priv = group->priv;
1144 if (toplevel != priv->toplevel)
1148 /* Disconnect focus tracking handler. */
1149 g_signal_handler_disconnect (priv->toplevel,
1150 priv->focus_set_id);
1152 priv->focus_set_id = 0;
1153 priv->toplevel = NULL;
1158 /* Install focus tracking handler. We connect to the window's
1159 * set-focus signal instead of connecting to the focus signal of
1162 * 1) Reduce the number of signal handlers used.
1163 * 2) Avoid special handling for group headers.
1164 * 3) Catch focus grabs not only for direct children,
1165 * but also for nested widgets.
1167 priv->focus_set_id =
1168 g_signal_connect (toplevel, "set-focus",
1169 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1172 priv->toplevel = toplevel;
1178 gtk_tool_item_group_realize (GtkWidget *widget)
1180 GtkAllocation allocation;
1181 GtkWidget *toplevel_window;
1183 GdkWindowAttr attributes;
1184 GdkDisplay *display;
1185 gint attributes_mask;
1188 gtk_widget_set_realized (widget, TRUE);
1190 border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1192 gtk_widget_get_allocation (widget, &allocation);
1194 attributes.window_type = GDK_WINDOW_CHILD;
1195 attributes.x = allocation.x + border_width;
1196 attributes.y = allocation.y + border_width;
1197 attributes.width = allocation.width - border_width * 2;
1198 attributes.height = allocation.height - border_width * 2;
1199 attributes.wclass = GDK_INPUT_OUTPUT;
1200 attributes.visual = gtk_widget_get_visual (widget);
1201 attributes.event_mask = gtk_widget_get_events (widget)
1202 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1203 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1204 | GDK_BUTTON_MOTION_MASK;
1205 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1207 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1208 &attributes, attributes_mask);
1209 gtk_widget_set_window (widget, window);
1211 display = gdk_window_get_display (window);
1213 if (gdk_display_supports_composite (display))
1214 gdk_window_set_composited (window, TRUE);
1216 gdk_window_set_user_data (window, widget);
1218 gtk_widget_style_attach (widget);
1219 gtk_style_set_background (gtk_widget_get_style (widget),
1220 window, GTK_STATE_NORMAL);
1222 gtk_container_forall (GTK_CONTAINER (widget),
1223 (GtkCallback) gtk_widget_set_parent_window,
1226 gtk_widget_queue_resize_no_redraw (widget);
1228 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1229 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1234 gtk_tool_item_group_unrealize (GtkWidget *widget)
1236 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1237 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1241 gtk_tool_item_group_style_set (GtkWidget *widget,
1242 GtkStyle *previous_style)
1244 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1245 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1249 gtk_tool_item_group_add (GtkContainer *container,
1252 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1253 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1255 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1256 GTK_TOOL_ITEM (widget), -1);
1260 gtk_tool_item_group_remove (GtkContainer *container,
1263 GtkToolItemGroup *group;
1264 GtkToolItemGroupPrivate* priv;
1267 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1268 group = GTK_TOOL_ITEM_GROUP (container);
1271 for (it = priv->children; it != NULL; it = it->next)
1273 GtkToolItemGroupChild *child_info = it->data;
1275 if ((GtkWidget *)child_info->item == child)
1277 g_object_unref (child);
1278 gtk_widget_unparent (child);
1280 g_free (child_info);
1281 priv->children = g_list_delete_link (priv->children, it);
1283 gtk_widget_queue_resize (GTK_WIDGET (container));
1290 gtk_tool_item_group_forall (GtkContainer *container,
1292 GtkCallback callback,
1293 gpointer callback_data)
1295 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1296 GtkToolItemGroupPrivate* priv = group->priv;
1299 if (internals && priv->header)
1300 callback (priv->header, callback_data);
1302 children = priv->children;
1305 GtkToolItemGroupChild *child = children->data;
1306 children = children->next; /* store pointer before call to callback
1307 because the child pointer is invalid if the
1308 child->item is removed from the item group
1311 callback (GTK_WIDGET (child->item), callback_data);
1316 gtk_tool_item_group_child_type (GtkContainer *container)
1318 return GTK_TYPE_TOOL_ITEM;
1321 static GtkToolItemGroupChild *
1322 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1330 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1331 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1333 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1335 GtkToolItemGroupChild *child = it->data;
1337 if (child->item == item)
1353 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1355 gboolean *homogeneous,
1360 GtkToolItemGroupChild *child;
1362 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1363 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1365 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1370 *expand = child->expand;
1373 *homogeneous = child->homogeneous;
1376 *fill = child->fill;
1379 *new_row = child->new_row;
1383 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1385 gboolean homogeneous,
1390 GtkToolItemGroupChild *child;
1391 gboolean changed = FALSE;
1393 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1394 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1396 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1400 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1402 if (child->homogeneous != homogeneous)
1404 child->homogeneous = homogeneous;
1406 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1408 if (child->expand != expand)
1410 child->expand = expand;
1412 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1414 if (child->fill != fill)
1418 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1420 if (child->new_row != new_row)
1422 child->new_row = new_row;
1424 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1427 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1430 && gtk_widget_get_visible (GTK_WIDGET (group))
1431 && gtk_widget_get_visible (GTK_WIDGET (item)))
1432 gtk_widget_queue_resize (GTK_WIDGET (group));
1436 gtk_tool_item_group_set_child_property (GtkContainer *container,
1439 const GValue *value,
1442 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1443 GtkToolItem *item = GTK_TOOL_ITEM (child);
1444 gboolean homogeneous, expand, fill, new_row;
1446 if (prop_id != CHILD_PROP_POSITION)
1447 gtk_tool_item_group_get_item_packing (group, item,
1455 case CHILD_PROP_HOMOGENEOUS:
1456 gtk_tool_item_group_set_item_packing (group, item,
1457 g_value_get_boolean (value),
1463 case CHILD_PROP_EXPAND:
1464 gtk_tool_item_group_set_item_packing (group, item,
1466 g_value_get_boolean (value),
1471 case CHILD_PROP_FILL:
1472 gtk_tool_item_group_set_item_packing (group, item,
1475 g_value_get_boolean (value),
1479 case CHILD_PROP_NEW_ROW:
1480 gtk_tool_item_group_set_item_packing (group, item,
1484 g_value_get_boolean (value));
1487 case CHILD_PROP_POSITION:
1488 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1492 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1498 gtk_tool_item_group_get_child_property (GtkContainer *container,
1504 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1505 GtkToolItem *item = GTK_TOOL_ITEM (child);
1506 gboolean homogeneous, expand, fill, new_row;
1508 if (prop_id != CHILD_PROP_POSITION)
1509 gtk_tool_item_group_get_item_packing (group, item,
1517 case CHILD_PROP_HOMOGENEOUS:
1518 g_value_set_boolean (value, homogeneous);
1521 case CHILD_PROP_EXPAND:
1522 g_value_set_boolean (value, expand);
1525 case CHILD_PROP_FILL:
1526 g_value_set_boolean (value, fill);
1529 case CHILD_PROP_NEW_ROW:
1530 g_value_set_boolean (value, new_row);
1533 case CHILD_PROP_POSITION:
1534 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1538 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1544 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1546 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1547 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1548 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1550 oclass->set_property = gtk_tool_item_group_set_property;
1551 oclass->get_property = gtk_tool_item_group_get_property;
1552 oclass->finalize = gtk_tool_item_group_finalize;
1553 oclass->dispose = gtk_tool_item_group_dispose;
1555 wclass->size_request = gtk_tool_item_group_size_request;
1556 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1557 wclass->realize = gtk_tool_item_group_realize;
1558 wclass->unrealize = gtk_tool_item_group_unrealize;
1559 wclass->style_set = gtk_tool_item_group_style_set;
1560 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1562 cclass->add = gtk_tool_item_group_add;
1563 cclass->remove = gtk_tool_item_group_remove;
1564 cclass->forall = gtk_tool_item_group_forall;
1565 cclass->child_type = gtk_tool_item_group_child_type;
1566 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1567 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1569 g_object_class_install_property (oclass, PROP_LABEL,
1570 g_param_spec_string ("label",
1572 P_("The human-readable title of this item group"),
1574 GTK_PARAM_READWRITE));
1576 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1577 g_param_spec_object ("label-widget",
1579 P_("A widget to display in place of the usual label"),
1581 GTK_PARAM_READWRITE));
1583 g_object_class_install_property (oclass, PROP_COLLAPSED,
1584 g_param_spec_boolean ("collapsed",
1586 P_("Whether the group has been collapsed and items are hidden"),
1588 GTK_PARAM_READWRITE));
1590 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1591 g_param_spec_enum ("ellipsize",
1593 P_("Ellipsize for item group headers"),
1594 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1595 GTK_PARAM_READWRITE));
1597 g_object_class_install_property (oclass, PROP_RELIEF,
1598 g_param_spec_enum ("header-relief",
1599 P_("Header Relief"),
1600 P_("Relief of the group header button"),
1601 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1602 GTK_PARAM_READWRITE));
1604 gtk_widget_class_install_style_property (wclass,
1605 g_param_spec_int ("expander-size",
1606 P_("Expander Size"),
1607 P_("Size of the expander arrow"),
1610 DEFAULT_EXPANDER_SIZE,
1611 GTK_PARAM_READABLE));
1613 gtk_widget_class_install_style_property (wclass,
1614 g_param_spec_int ("header-spacing",
1615 P_("Header Spacing"),
1616 P_("Spacing between expander arrow and caption"),
1619 DEFAULT_HEADER_SPACING,
1620 GTK_PARAM_READABLE));
1622 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1623 g_param_spec_boolean ("homogeneous",
1625 P_("Whether the item should be the same size as other homogeneous items"),
1627 GTK_PARAM_READWRITE));
1629 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1630 g_param_spec_boolean ("expand",
1632 P_("Whether the item should receive extra space when the group grows"),
1634 GTK_PARAM_READWRITE));
1636 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1637 g_param_spec_boolean ("fill",
1639 P_("Whether the item should fill the available space"),
1641 GTK_PARAM_READWRITE));
1643 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1644 g_param_spec_boolean ("new-row",
1646 P_("Whether the item should start a new row"),
1648 GTK_PARAM_READWRITE));
1650 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1651 g_param_spec_int ("position",
1653 P_("Position of the item within this group"),
1657 GTK_PARAM_READWRITE));
1659 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1663 * gtk_tool_item_group_new:
1664 * @label: the label of the new group
1666 * Creates a new tool item group with label @label.
1668 * Returns: a new #GtkToolItemGroup.
1673 gtk_tool_item_group_new (const gchar *label)
1675 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1679 * gtk_tool_item_group_set_label:
1680 * @group: a #GtkToolItemGroup
1681 * @label: the new human-readable label of of the group
1683 * Sets the label of the tool item group. The label is displayed in the header
1689 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1692 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1695 gtk_tool_item_group_set_label_widget (group, NULL);
1698 GtkWidget *child = gtk_label_new (label);
1699 gtk_widget_show (child);
1701 gtk_tool_item_group_set_label_widget (group, child);
1704 g_object_notify (G_OBJECT (group), "label");
1708 * gtk_tool_item_group_set_label_widget:
1709 * @group: a #GtkToolItemGroup
1710 * @label_widget: the widget to be displayed in place of the usual label
1712 * Sets the label of the tool item group.
1713 * The label widget is displayed in the header of the group, in place
1714 * of the usual label.
1719 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1720 GtkWidget *label_widget)
1722 GtkToolItemGroupPrivate* priv;
1723 GtkWidget *alignment;
1725 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1726 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1727 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1731 if (priv->label_widget == label_widget)
1734 alignment = gtk_tool_item_group_get_alignment (group);
1736 if (priv->label_widget)
1738 gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1739 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1744 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1746 priv->label_widget = label_widget;
1748 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1749 gtk_widget_queue_resize (GTK_WIDGET (group));
1751 /* Only show the header widget if the group has children: */
1752 if (label_widget && priv->children)
1753 gtk_widget_show (priv->header);
1755 gtk_widget_hide (priv->header);
1757 g_object_freeze_notify (G_OBJECT (group));
1758 g_object_notify (G_OBJECT (group), "label-widget");
1759 g_object_notify (G_OBJECT (group), "label");
1760 g_object_thaw_notify (G_OBJECT (group));
1764 * gtk_tool_item_group_set_header_relief:
1765 * @group: a #GtkToolItemGroup
1766 * @style: the #GtkReliefStyle
1768 * Set the button relief of the group header.
1769 * See gtk_button_set_relief() for details.
1774 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1775 GtkReliefStyle style)
1777 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1779 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1783 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1787 g_source_get_current_time (group->priv->animation_timeout, &now);
1788 return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1792 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1794 GtkToolItemGroupPrivate* priv = group->priv;
1795 GtkWidget *widget = GTK_WIDGET (group);
1797 if (gtk_widget_get_realized (priv->header))
1799 GtkAllocation alignment_allocation;
1800 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1803 /* Find the header button's arrow area... */
1804 gtk_widget_get_allocation (alignment, &alignment_allocation);
1805 area.x = alignment_allocation.x;
1806 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1807 area.height = priv->expander_size;
1808 area.width = priv->expander_size;
1810 /* ... and invalidated it to get it animated. */
1811 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1814 if (gtk_widget_get_realized (widget))
1816 GtkAllocation allocation;
1817 GtkWidget *parent = gtk_widget_get_parent (widget);
1818 int x, y, width, height;
1820 /* Find the tool item area button's arrow area... */
1821 gtk_widget_get_allocation (widget, &allocation);
1822 width = allocation.width;
1823 height = allocation.height;
1825 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1827 if (gtk_widget_get_visible (priv->header))
1829 GtkAllocation header_allocation;
1831 gtk_widget_get_allocation (priv->header, &header_allocation);
1832 height -= header_allocation.height;
1833 y += header_allocation.height;
1836 /* ... and invalidated it to get it animated. */
1837 gtk_widget_queue_draw_area (parent, x, y, width, height);
1842 gtk_tool_item_group_animation_cb (gpointer data)
1844 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1845 GtkToolItemGroupPrivate* priv = group->priv;
1846 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1849 GDK_THREADS_ENTER ();
1851 /* Enque this early to reduce number of expose events. */
1852 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1854 /* Figure out current style of the expander arrow. */
1855 if (priv->collapsed)
1857 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1858 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1860 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1864 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1865 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1867 priv->expander_style = GTK_EXPANDER_EXPANDED;
1870 gtk_tool_item_group_force_expose (group);
1872 /* Finish animation when done. */
1873 if (timestamp >= ANIMATION_DURATION)
1874 priv->animation_timeout = NULL;
1876 retval = (priv->animation_timeout != NULL);
1878 GDK_THREADS_LEAVE ();
1884 * gtk_tool_item_group_set_collapsed:
1885 * @group: a #GtkToolItemGroup
1886 * @collapsed: whether the @group should be collapsed or expanded
1888 * Sets whether the @group should be collapsed or expanded.
1893 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1897 GtkToolItemGroupPrivate* priv;
1899 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1903 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1904 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1905 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1906 GTK_WIDGET (group));
1907 if (collapsed != priv->collapsed)
1909 if (priv->animation)
1913 g_get_current_time (&now);
1915 if (priv->animation_timeout)
1916 g_source_destroy (priv->animation_timeout);
1918 priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1919 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1921 g_source_set_callback (priv->animation_timeout,
1922 gtk_tool_item_group_animation_cb,
1925 g_source_attach (priv->animation_timeout, NULL);
1929 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1930 gtk_tool_item_group_force_expose (group);
1933 priv->collapsed = collapsed;
1934 g_object_notify (G_OBJECT (group), "collapsed");
1939 * gtk_tool_item_group_set_ellipsize:
1940 * @group: a #GtkToolItemGroup
1941 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1943 * Sets the ellipsization mode which should be used by labels in @group.
1948 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1949 PangoEllipsizeMode ellipsize)
1951 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1953 if (ellipsize != group->priv->ellipsize)
1955 group->priv->ellipsize = ellipsize;
1956 gtk_tool_item_group_header_adjust_style (group);
1957 g_object_notify (G_OBJECT (group), "ellipsize");
1958 _gtk_tool_item_group_palette_reconfigured (group);
1963 * gtk_tool_item_group_get_label:
1964 * @group: a #GtkToolItemGroup
1966 * Gets the label of @group.
1968 * Returns: the label of @group. The label is an internal string of @group
1969 * and must not be modified. Note that %NULL is returned if a custom
1970 * label has been set with gtk_tool_item_group_set_label_widget()
1974 G_CONST_RETURN gchar*
1975 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1977 GtkToolItemGroupPrivate *priv;
1979 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1983 if (GTK_IS_LABEL (priv->label_widget))
1984 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1990 * gtk_tool_item_group_get_label_widget:
1991 * @group: a #GtkToolItemGroup
1993 * Gets the label widget of @group.
1994 * See gtk_tool_item_group_set_label_widget().
1996 * Returns: (transfer none): the label widget of @group
2001 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2003 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2005 return gtk_bin_get_child (GTK_BIN (alignment));
2009 * gtk_tool_item_group_get_collapsed:
2010 * @group: a GtkToolItemGroup
2012 * Gets whether @group is collapsed or expanded.
2014 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2019 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2021 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2023 return group->priv->collapsed;
2027 * gtk_tool_item_group_get_ellipsize:
2028 * @group: a #GtkToolItemGroup
2030 * Gets the ellipsization mode of @group.
2032 * Returns: the #PangoEllipsizeMode of @group
2037 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2039 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2041 return group->priv->ellipsize;
2045 * gtk_tool_item_group_get_header_relief:
2046 * @group: a #GtkToolItemGroup
2048 * Gets the relief mode of the header button of @group.
2050 * Returns: the #GtkReliefStyle
2055 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2057 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2059 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2063 * gtk_tool_item_group_insert:
2064 * @group: a #GtkToolItemGroup
2065 * @item: the #GtkToolItem to insert into @group
2066 * @position: the position of @item in @group, starting with 0.
2067 * The position -1 means end of list.
2069 * Inserts @item at @position in the list of children of @group.
2074 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2078 GtkWidget *parent, *child_widget;
2079 GtkToolItemGroupChild *child;
2081 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2082 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2083 g_return_if_fail (position >= -1);
2085 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2087 child = g_new (GtkToolItemGroupChild, 1);
2088 child->item = g_object_ref_sink (item);
2089 child->homogeneous = TRUE;
2090 child->expand = FALSE;
2092 child->new_row = FALSE;
2094 group->priv->children = g_list_insert (group->priv->children, child, position);
2096 if (GTK_IS_TOOL_PALETTE (parent))
2097 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2099 child_widget = gtk_bin_get_child (GTK_BIN (item));
2101 if (GTK_IS_BUTTON (child_widget))
2102 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2104 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2108 * gtk_tool_item_group_set_item_position:
2109 * @group: a #GtkToolItemGroup
2110 * @item: the #GtkToolItem to move to a new position, should
2111 * be a child of @group.
2112 * @position: the new position of @item in @group, starting with 0.
2113 * The position -1 means end of list.
2115 * Sets the position of @item in the list of children of @group.
2120 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2126 GtkToolItemGroupChild *child;
2127 GtkToolItemGroupPrivate* priv;
2129 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2130 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2131 g_return_if_fail (position >= -1);
2133 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2136 g_return_if_fail (child != NULL);
2138 if (position == old_position)
2141 priv->children = g_list_delete_link (priv->children, link);
2142 priv->children = g_list_insert (priv->children, child, position);
2144 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2145 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2146 gtk_widget_get_visible (GTK_WIDGET (item)))
2147 gtk_widget_queue_resize (GTK_WIDGET (group));
2151 * gtk_tool_item_group_get_item_position:
2152 * @group: a #GtkToolItemGroup
2153 * @item: a #GtkToolItem
2155 * Gets the position of @item in @group as index.
2157 * Returns: the index of @item in @group or -1 if @item is no child of @group
2162 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2167 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2168 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2170 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2177 * gtk_tool_item_group_get_n_items:
2178 * @group: a #GtkToolItemGroup
2180 * Gets the number of tool items in @group.
2182 * Returns: the number of tool items in @group
2187 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2189 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2191 return g_list_length (group->priv->children);
2195 * gtk_tool_item_group_get_nth_item:
2196 * @group: a #GtkToolItemGroup
2199 * Gets the tool item at @index in group.
2201 * Returns: (transfer none): the #GtkToolItem at index
2206 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2209 GtkToolItemGroupChild *child;
2211 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2213 child = g_list_nth_data (group->priv->children, index);
2215 return child != NULL ? child->item : NULL;
2219 * gtk_tool_item_group_get_drop_item:
2220 * @group: a #GtkToolItemGroup
2221 * @x: the x position
2222 * @y: the y position
2224 * Gets the tool item at position (x, y).
2226 * Returns: (transfer none): the #GtkToolItem at position (x, y)
2231 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2235 GtkAllocation allocation;
2236 GtkOrientation orientation;
2239 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2241 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2242 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2244 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2245 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2247 for (it = group->priv->children; it != NULL; it = it->next)
2249 GtkToolItemGroupChild *child = it->data;
2250 GtkToolItem *item = child->item;
2253 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2256 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2258 x0 = x - allocation.x;
2259 y0 = y - allocation.y;
2261 if (x0 >= 0 && x0 < allocation.width &&
2262 y0 >= 0 && y0 < allocation.height)
2270 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2271 GtkRequisition *item_size,
2272 gboolean homogeneous_only,
2273 gint *requested_rows)
2275 GtkRequisition child_requisition;
2278 gboolean new_row = TRUE;
2279 GtkOrientation orientation;
2280 GtkToolbarStyle style;
2282 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2283 g_return_if_fail (NULL != item_size);
2285 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2286 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2288 item_size->width = item_size->height = 0;
2290 for (it = group->priv->children; it != NULL; it = it->next)
2292 GtkToolItemGroupChild *child = it->data;
2294 if (!gtk_tool_item_group_is_item_visible (group, child))
2297 if (child->new_row || new_row)
2303 if (!child->homogeneous && child->expand)
2306 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->item),
2307 &child_requisition, NULL);
2309 if (!homogeneous_only || child->homogeneous)
2310 item_size->width = MAX (item_size->width, child_requisition.width);
2311 item_size->height = MAX (item_size->height, child_requisition.height);
2315 *requested_rows = rows;
2319 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2322 GtkAllocation allocation;
2323 GtkWidget *widget = GTK_WIDGET (group);
2324 GtkToolItemGroupPrivate* priv = group->priv;
2326 gtk_widget_get_allocation (widget, &allocation);
2328 gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2332 if (priv->animation_timeout)
2334 GtkAllocation header_allocation;
2335 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2336 cairo_pattern_t *mask;
2339 if (GTK_ORIENTATION_VERTICAL == orientation)
2340 v1 = allocation.height;
2342 v1 = allocation.width;
2346 gtk_widget_get_allocation (priv->header, &header_allocation);
2347 if (!gtk_widget_get_visible (priv->header))
2349 else if (GTK_ORIENTATION_VERTICAL == orientation)
2350 v0 = MAX (v0, header_allocation.height);
2352 v0 = MAX (v0, header_allocation.width);
2354 v1 = MIN (v0 + 256, v1);
2356 if (GTK_ORIENTATION_VERTICAL == orientation)
2361 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2368 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2371 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2372 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2373 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2374 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2375 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2377 cairo_mask (cr, mask);
2378 cairo_pattern_destroy (mask);
2385 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2390 GtkRequisition requisition;
2391 GtkToolItemGroupPrivate* priv = group->priv;
2393 gtk_size_request_get_size (GTK_SIZE_REQUEST (group),
2394 &requisition, NULL);
2396 if (!priv->collapsed || priv->animation_timeout)
2398 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2399 GtkRequisition inquery;
2402 allocation.width = limit;
2404 allocation.height = limit;
2406 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2407 &allocation, &inquery);
2410 inquery.height -= requisition.height;
2412 inquery.width -= requisition.width;
2414 if (priv->animation_timeout && animation)
2416 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2418 timestamp = MIN (timestamp, ANIMATION_DURATION);
2420 if (priv->collapsed)
2421 timestamp = ANIMATION_DURATION - timestamp;
2425 inquery.height *= timestamp;
2426 inquery.height /= ANIMATION_DURATION;
2430 inquery.width *= timestamp;
2431 inquery.width /= ANIMATION_DURATION;
2436 requisition.height += inquery.height;
2438 requisition.width += inquery.width;
2441 return (vertical ? requisition.height : requisition.width);
2445 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2448 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2452 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2455 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2459 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2462 if (GTK_IS_TOOL_ITEM (child))
2463 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2468 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2470 gtk_container_foreach (GTK_CONTAINER (group),
2471 gtk_tool_palette_reconfigured_foreach_item,
2474 gtk_tool_item_group_header_adjust_style (group);