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.colormap = gtk_widget_get_colormap (widget);
1202 attributes.event_mask = gtk_widget_get_events (widget)
1203 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1204 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1205 | GDK_BUTTON_MOTION_MASK;
1206 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1208 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1209 &attributes, attributes_mask);
1210 gtk_widget_set_window (widget, window);
1212 display = gdk_drawable_get_display (window);
1214 if (gdk_display_supports_composite (display))
1215 gdk_window_set_composited (window, TRUE);
1217 gdk_window_set_user_data (window, widget);
1219 gtk_widget_style_attach (widget);
1220 gtk_style_set_background (gtk_widget_get_style (widget),
1221 window, GTK_STATE_NORMAL);
1223 gtk_container_forall (GTK_CONTAINER (widget),
1224 (GtkCallback) gtk_widget_set_parent_window,
1227 gtk_widget_queue_resize_no_redraw (widget);
1229 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1230 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1235 gtk_tool_item_group_unrealize (GtkWidget *widget)
1237 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1238 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1242 gtk_tool_item_group_style_set (GtkWidget *widget,
1243 GtkStyle *previous_style)
1245 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1246 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1250 gtk_tool_item_group_add (GtkContainer *container,
1253 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1254 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1256 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1257 GTK_TOOL_ITEM (widget), -1);
1261 gtk_tool_item_group_remove (GtkContainer *container,
1264 GtkToolItemGroup *group;
1265 GtkToolItemGroupPrivate* priv;
1268 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1269 group = GTK_TOOL_ITEM_GROUP (container);
1272 for (it = priv->children; it != NULL; it = it->next)
1274 GtkToolItemGroupChild *child_info = it->data;
1276 if ((GtkWidget *)child_info->item == child)
1278 g_object_unref (child);
1279 gtk_widget_unparent (child);
1281 g_free (child_info);
1282 priv->children = g_list_delete_link (priv->children, it);
1284 gtk_widget_queue_resize (GTK_WIDGET (container));
1291 gtk_tool_item_group_forall (GtkContainer *container,
1293 GtkCallback callback,
1294 gpointer callback_data)
1296 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1297 GtkToolItemGroupPrivate* priv = group->priv;
1300 if (internals && priv->header)
1301 callback (priv->header, callback_data);
1303 children = priv->children;
1306 GtkToolItemGroupChild *child = children->data;
1307 children = children->next; /* store pointer before call to callback
1308 because the child pointer is invalid if the
1309 child->item is removed from the item group
1312 callback (GTK_WIDGET (child->item), callback_data);
1317 gtk_tool_item_group_child_type (GtkContainer *container)
1319 return GTK_TYPE_TOOL_ITEM;
1322 static GtkToolItemGroupChild *
1323 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1331 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1332 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1334 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1336 GtkToolItemGroupChild *child = it->data;
1338 if (child->item == item)
1354 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1356 gboolean *homogeneous,
1361 GtkToolItemGroupChild *child;
1363 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1364 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1366 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1371 *expand = child->expand;
1374 *homogeneous = child->homogeneous;
1377 *fill = child->fill;
1380 *new_row = child->new_row;
1384 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1386 gboolean homogeneous,
1391 GtkToolItemGroupChild *child;
1392 gboolean changed = FALSE;
1394 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1395 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1397 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1401 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1403 if (child->homogeneous != homogeneous)
1405 child->homogeneous = homogeneous;
1407 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1409 if (child->expand != expand)
1411 child->expand = expand;
1413 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1415 if (child->fill != fill)
1419 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1421 if (child->new_row != new_row)
1423 child->new_row = new_row;
1425 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1428 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1431 && gtk_widget_get_visible (GTK_WIDGET (group))
1432 && gtk_widget_get_visible (GTK_WIDGET (item)))
1433 gtk_widget_queue_resize (GTK_WIDGET (group));
1437 gtk_tool_item_group_set_child_property (GtkContainer *container,
1440 const GValue *value,
1443 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1444 GtkToolItem *item = GTK_TOOL_ITEM (child);
1445 gboolean homogeneous, expand, fill, new_row;
1447 if (prop_id != CHILD_PROP_POSITION)
1448 gtk_tool_item_group_get_item_packing (group, item,
1456 case CHILD_PROP_HOMOGENEOUS:
1457 gtk_tool_item_group_set_item_packing (group, item,
1458 g_value_get_boolean (value),
1464 case CHILD_PROP_EXPAND:
1465 gtk_tool_item_group_set_item_packing (group, item,
1467 g_value_get_boolean (value),
1472 case CHILD_PROP_FILL:
1473 gtk_tool_item_group_set_item_packing (group, item,
1476 g_value_get_boolean (value),
1480 case CHILD_PROP_NEW_ROW:
1481 gtk_tool_item_group_set_item_packing (group, item,
1485 g_value_get_boolean (value));
1488 case CHILD_PROP_POSITION:
1489 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1493 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1499 gtk_tool_item_group_get_child_property (GtkContainer *container,
1505 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1506 GtkToolItem *item = GTK_TOOL_ITEM (child);
1507 gboolean homogeneous, expand, fill, new_row;
1509 if (prop_id != CHILD_PROP_POSITION)
1510 gtk_tool_item_group_get_item_packing (group, item,
1518 case CHILD_PROP_HOMOGENEOUS:
1519 g_value_set_boolean (value, homogeneous);
1522 case CHILD_PROP_EXPAND:
1523 g_value_set_boolean (value, expand);
1526 case CHILD_PROP_FILL:
1527 g_value_set_boolean (value, fill);
1530 case CHILD_PROP_NEW_ROW:
1531 g_value_set_boolean (value, new_row);
1534 case CHILD_PROP_POSITION:
1535 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1539 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1545 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1547 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1548 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1549 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1551 oclass->set_property = gtk_tool_item_group_set_property;
1552 oclass->get_property = gtk_tool_item_group_get_property;
1553 oclass->finalize = gtk_tool_item_group_finalize;
1554 oclass->dispose = gtk_tool_item_group_dispose;
1556 wclass->size_request = gtk_tool_item_group_size_request;
1557 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1558 wclass->realize = gtk_tool_item_group_realize;
1559 wclass->unrealize = gtk_tool_item_group_unrealize;
1560 wclass->style_set = gtk_tool_item_group_style_set;
1561 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1563 cclass->add = gtk_tool_item_group_add;
1564 cclass->remove = gtk_tool_item_group_remove;
1565 cclass->forall = gtk_tool_item_group_forall;
1566 cclass->child_type = gtk_tool_item_group_child_type;
1567 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1568 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1570 g_object_class_install_property (oclass, PROP_LABEL,
1571 g_param_spec_string ("label",
1573 P_("The human-readable title of this item group"),
1575 GTK_PARAM_READWRITE));
1577 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1578 g_param_spec_object ("label-widget",
1580 P_("A widget to display in place of the usual label"),
1582 GTK_PARAM_READWRITE));
1584 g_object_class_install_property (oclass, PROP_COLLAPSED,
1585 g_param_spec_boolean ("collapsed",
1587 P_("Whether the group has been collapsed and items are hidden"),
1589 GTK_PARAM_READWRITE));
1591 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1592 g_param_spec_enum ("ellipsize",
1594 P_("Ellipsize for item group headers"),
1595 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1596 GTK_PARAM_READWRITE));
1598 g_object_class_install_property (oclass, PROP_RELIEF,
1599 g_param_spec_enum ("header-relief",
1600 P_("Header Relief"),
1601 P_("Relief of the group header button"),
1602 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1603 GTK_PARAM_READWRITE));
1605 gtk_widget_class_install_style_property (wclass,
1606 g_param_spec_int ("expander-size",
1607 P_("Expander Size"),
1608 P_("Size of the expander arrow"),
1611 DEFAULT_EXPANDER_SIZE,
1612 GTK_PARAM_READABLE));
1614 gtk_widget_class_install_style_property (wclass,
1615 g_param_spec_int ("header-spacing",
1616 P_("Header Spacing"),
1617 P_("Spacing between expander arrow and caption"),
1620 DEFAULT_HEADER_SPACING,
1621 GTK_PARAM_READABLE));
1623 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1624 g_param_spec_boolean ("homogeneous",
1626 P_("Whether the item should be the same size as other homogeneous items"),
1628 GTK_PARAM_READWRITE));
1630 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1631 g_param_spec_boolean ("expand",
1633 P_("Whether the item should receive extra space when the group grows"),
1635 GTK_PARAM_READWRITE));
1637 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1638 g_param_spec_boolean ("fill",
1640 P_("Whether the item should fill the available space"),
1642 GTK_PARAM_READWRITE));
1644 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1645 g_param_spec_boolean ("new-row",
1647 P_("Whether the item should start a new row"),
1649 GTK_PARAM_READWRITE));
1651 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1652 g_param_spec_int ("position",
1654 P_("Position of the item within this group"),
1658 GTK_PARAM_READWRITE));
1660 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1664 * gtk_tool_item_group_new:
1665 * @label: the label of the new group
1667 * Creates a new tool item group with label @label.
1669 * Returns: a new #GtkToolItemGroup.
1674 gtk_tool_item_group_new (const gchar *label)
1676 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1680 * gtk_tool_item_group_set_label:
1681 * @group: a #GtkToolItemGroup
1682 * @label: the new human-readable label of of the group
1684 * Sets the label of the tool item group. The label is displayed in the header
1690 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1693 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1696 gtk_tool_item_group_set_label_widget (group, NULL);
1699 GtkWidget *child = gtk_label_new (label);
1700 gtk_widget_show (child);
1702 gtk_tool_item_group_set_label_widget (group, child);
1705 g_object_notify (G_OBJECT (group), "label");
1709 * gtk_tool_item_group_set_label_widget:
1710 * @group: a #GtkToolItemGroup
1711 * @label_widget: the widget to be displayed in place of the usual label
1713 * Sets the label of the tool item group.
1714 * The label widget is displayed in the header of the group, in place
1715 * of the usual label.
1720 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1721 GtkWidget *label_widget)
1723 GtkToolItemGroupPrivate* priv;
1724 GtkWidget *alignment;
1726 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1727 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1728 g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1732 if (priv->label_widget == label_widget)
1735 alignment = gtk_tool_item_group_get_alignment (group);
1737 if (priv->label_widget)
1739 gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1740 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1745 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1747 priv->label_widget = label_widget;
1749 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1750 gtk_widget_queue_resize (GTK_WIDGET (group));
1752 /* Only show the header widget if the group has children: */
1753 if (label_widget && priv->children)
1754 gtk_widget_show (priv->header);
1756 gtk_widget_hide (priv->header);
1758 g_object_freeze_notify (G_OBJECT (group));
1759 g_object_notify (G_OBJECT (group), "label-widget");
1760 g_object_notify (G_OBJECT (group), "label");
1761 g_object_thaw_notify (G_OBJECT (group));
1765 * gtk_tool_item_group_set_header_relief:
1766 * @group: a #GtkToolItemGroup
1767 * @style: the #GtkReliefStyle
1769 * Set the button relief of the group header.
1770 * See gtk_button_set_relief() for details.
1775 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1776 GtkReliefStyle style)
1778 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1780 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1784 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1788 g_source_get_current_time (group->priv->animation_timeout, &now);
1789 return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1793 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1795 GtkToolItemGroupPrivate* priv = group->priv;
1796 GtkWidget *widget = GTK_WIDGET (group);
1798 if (gtk_widget_get_realized (priv->header))
1800 GtkAllocation alignment_allocation;
1801 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1804 /* Find the header button's arrow area... */
1805 gtk_widget_get_allocation (alignment, &alignment_allocation);
1806 area.x = alignment_allocation.x;
1807 area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1808 area.height = priv->expander_size;
1809 area.width = priv->expander_size;
1811 /* ... and invalidated it to get it animated. */
1812 gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1815 if (gtk_widget_get_realized (widget))
1817 GtkAllocation allocation;
1818 GtkWidget *parent = gtk_widget_get_parent (widget);
1819 int x, y, width, height;
1821 /* Find the tool item area button's arrow area... */
1822 gtk_widget_get_allocation (widget, &allocation);
1823 width = allocation.width;
1824 height = allocation.height;
1826 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1828 if (gtk_widget_get_visible (priv->header))
1830 GtkAllocation header_allocation;
1832 gtk_widget_get_allocation (priv->header, &header_allocation);
1833 height -= header_allocation.height;
1834 y += header_allocation.height;
1837 /* ... and invalidated it to get it animated. */
1838 gtk_widget_queue_draw_area (parent, x, y, width, height);
1843 gtk_tool_item_group_animation_cb (gpointer data)
1845 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1846 GtkToolItemGroupPrivate* priv = group->priv;
1847 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1850 GDK_THREADS_ENTER ();
1852 /* Enque this early to reduce number of expose events. */
1853 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1855 /* Figure out current style of the expander arrow. */
1856 if (priv->collapsed)
1858 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1859 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1861 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1865 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1866 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1868 priv->expander_style = GTK_EXPANDER_EXPANDED;
1871 gtk_tool_item_group_force_expose (group);
1873 /* Finish animation when done. */
1874 if (timestamp >= ANIMATION_DURATION)
1875 priv->animation_timeout = NULL;
1877 retval = (priv->animation_timeout != NULL);
1879 GDK_THREADS_LEAVE ();
1885 * gtk_tool_item_group_set_collapsed:
1886 * @group: a #GtkToolItemGroup
1887 * @collapsed: whether the @group should be collapsed or expanded
1889 * Sets whether the @group should be collapsed or expanded.
1894 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1898 GtkToolItemGroupPrivate* priv;
1900 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1904 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1905 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1906 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1907 GTK_WIDGET (group));
1908 if (collapsed != priv->collapsed)
1910 if (priv->animation)
1914 g_get_current_time (&now);
1916 if (priv->animation_timeout)
1917 g_source_destroy (priv->animation_timeout);
1919 priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1920 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1922 g_source_set_callback (priv->animation_timeout,
1923 gtk_tool_item_group_animation_cb,
1926 g_source_attach (priv->animation_timeout, NULL);
1930 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1931 gtk_tool_item_group_force_expose (group);
1934 priv->collapsed = collapsed;
1935 g_object_notify (G_OBJECT (group), "collapsed");
1940 * gtk_tool_item_group_set_ellipsize:
1941 * @group: a #GtkToolItemGroup
1942 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1944 * Sets the ellipsization mode which should be used by labels in @group.
1949 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1950 PangoEllipsizeMode ellipsize)
1952 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1954 if (ellipsize != group->priv->ellipsize)
1956 group->priv->ellipsize = ellipsize;
1957 gtk_tool_item_group_header_adjust_style (group);
1958 g_object_notify (G_OBJECT (group), "ellipsize");
1959 _gtk_tool_item_group_palette_reconfigured (group);
1964 * gtk_tool_item_group_get_label:
1965 * @group: a #GtkToolItemGroup
1967 * Gets the label of @group.
1969 * Returns: the label of @group. The label is an internal string of @group
1970 * and must not be modified. Note that %NULL is returned if a custom
1971 * label has been set with gtk_tool_item_group_set_label_widget()
1975 G_CONST_RETURN gchar*
1976 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1978 GtkToolItemGroupPrivate *priv;
1980 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1984 if (GTK_IS_LABEL (priv->label_widget))
1985 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1991 * gtk_tool_item_group_get_label_widget:
1992 * @group: a #GtkToolItemGroup
1994 * Gets the label widget of @group.
1995 * See gtk_tool_item_group_set_label_widget().
1997 * Returns: the label widget of @group
2002 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2004 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2006 return gtk_bin_get_child (GTK_BIN (alignment));
2010 * gtk_tool_item_group_get_collapsed:
2011 * @group: a GtkToolItemGroup
2013 * Gets whether @group is collapsed or expanded.
2015 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2020 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2022 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2024 return group->priv->collapsed;
2028 * gtk_tool_item_group_get_ellipsize:
2029 * @group: a #GtkToolItemGroup
2031 * Gets the ellipsization mode of @group.
2033 * Returns: the #PangoEllipsizeMode of @group
2038 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2040 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2042 return group->priv->ellipsize;
2046 * gtk_tool_item_group_get_header_relief:
2047 * @group: a #GtkToolItemGroup
2049 * Gets the relief mode of the header button of @group.
2051 * Returns: the #GtkReliefStyle
2056 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2058 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2060 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2064 * gtk_tool_item_group_insert:
2065 * @group: a #GtkToolItemGroup
2066 * @item: the #GtkToolItem to insert into @group
2067 * @position: the position of @item in @group, starting with 0.
2068 * The position -1 means end of list.
2070 * Inserts @item at @position in the list of children of @group.
2075 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2079 GtkWidget *parent, *child_widget;
2080 GtkToolItemGroupChild *child;
2082 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2083 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2084 g_return_if_fail (position >= -1);
2086 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2088 child = g_new (GtkToolItemGroupChild, 1);
2089 child->item = g_object_ref_sink (item);
2090 child->homogeneous = TRUE;
2091 child->expand = FALSE;
2093 child->new_row = FALSE;
2095 group->priv->children = g_list_insert (group->priv->children, child, position);
2097 if (GTK_IS_TOOL_PALETTE (parent))
2098 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2100 child_widget = gtk_bin_get_child (GTK_BIN (item));
2102 if (GTK_IS_BUTTON (child_widget))
2103 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2105 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2109 * gtk_tool_item_group_set_item_position:
2110 * @group: a #GtkToolItemGroup
2111 * @item: the #GtkToolItem to move to a new position, should
2112 * be a child of @group.
2113 * @position: the new position of @item in @group, starting with 0.
2114 * The position -1 means end of list.
2116 * Sets the position of @item in the list of children of @group.
2121 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2127 GtkToolItemGroupChild *child;
2128 GtkToolItemGroupPrivate* priv;
2130 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2131 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2132 g_return_if_fail (position >= -1);
2134 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2137 g_return_if_fail (child != NULL);
2139 if (position == old_position)
2142 priv->children = g_list_delete_link (priv->children, link);
2143 priv->children = g_list_insert (priv->children, child, position);
2145 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2146 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2147 gtk_widget_get_visible (GTK_WIDGET (item)))
2148 gtk_widget_queue_resize (GTK_WIDGET (group));
2152 * gtk_tool_item_group_get_item_position:
2153 * @group: a #GtkToolItemGroup
2154 * @item: a #GtkToolItem
2156 * Gets the position of @item in @group as index.
2158 * Returns: the index of @item in @group or -1 if @item is no child of @group
2163 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2168 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2169 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2171 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2178 * gtk_tool_item_group_get_n_items:
2179 * @group: a #GtkToolItemGroup
2181 * Gets the number of tool items in @group.
2183 * Returns: the number of tool items in @group
2188 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2190 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2192 return g_list_length (group->priv->children);
2196 * gtk_tool_item_group_get_nth_item:
2197 * @group: a #GtkToolItemGroup
2200 * Gets the tool item at @index in group.
2202 * Returns: the #GtkToolItem at index
2207 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2210 GtkToolItemGroupChild *child;
2212 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2214 child = g_list_nth_data (group->priv->children, index);
2216 return child != NULL ? child->item : NULL;
2220 * gtk_tool_item_group_get_drop_item:
2221 * @group: a #GtkToolItemGroup
2222 * @x: the x position
2223 * @y: the y position
2225 * Gets the tool item at position (x, y).
2227 * Returns: the #GtkToolItem at position (x, y)
2232 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2236 GtkAllocation allocation;
2237 GtkOrientation orientation;
2240 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2242 gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2243 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2245 g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2246 g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2248 for (it = group->priv->children; it != NULL; it = it->next)
2250 GtkToolItemGroupChild *child = it->data;
2251 GtkToolItem *item = child->item;
2254 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2257 gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2259 x0 = x - allocation.x;
2260 y0 = y - allocation.y;
2262 if (x0 >= 0 && x0 < allocation.width &&
2263 y0 >= 0 && y0 < allocation.height)
2271 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2272 GtkRequisition *item_size,
2273 gboolean homogeneous_only,
2274 gint *requested_rows)
2276 GtkRequisition child_requisition;
2279 gboolean new_row = TRUE;
2280 GtkOrientation orientation;
2281 GtkToolbarStyle style;
2283 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2284 g_return_if_fail (NULL != item_size);
2286 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2287 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2289 item_size->width = item_size->height = 0;
2291 for (it = group->priv->children; it != NULL; it = it->next)
2293 GtkToolItemGroupChild *child = it->data;
2295 if (!gtk_tool_item_group_is_item_visible (group, child))
2298 if (child->new_row || new_row)
2304 if (!child->homogeneous && child->expand)
2307 gtk_size_request_get_size (GTK_SIZE_REQUEST (child->item),
2308 &child_requisition, NULL);
2310 if (!homogeneous_only || child->homogeneous)
2311 item_size->width = MAX (item_size->width, child_requisition.width);
2312 item_size->height = MAX (item_size->height, child_requisition.height);
2316 *requested_rows = rows;
2320 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2323 GtkAllocation allocation;
2324 GtkWidget *widget = GTK_WIDGET (group);
2325 GtkToolItemGroupPrivate* priv = group->priv;
2327 gtk_widget_get_allocation (widget, &allocation);
2329 gdk_cairo_set_source_pixmap (cr, gtk_widget_get_window (widget),
2333 if (priv->animation_timeout)
2335 GtkAllocation header_allocation;
2336 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2337 cairo_pattern_t *mask;
2340 if (GTK_ORIENTATION_VERTICAL == orientation)
2341 v1 = allocation.height;
2343 v1 = allocation.width;
2347 gtk_widget_get_allocation (priv->header, &header_allocation);
2348 if (!gtk_widget_get_visible (priv->header))
2350 else if (GTK_ORIENTATION_VERTICAL == orientation)
2351 v0 = MAX (v0, header_allocation.height);
2353 v0 = MAX (v0, header_allocation.width);
2355 v1 = MIN (v0 + 256, v1);
2357 if (GTK_ORIENTATION_VERTICAL == orientation)
2362 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2369 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2372 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2373 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2374 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2375 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2376 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2378 cairo_mask (cr, mask);
2379 cairo_pattern_destroy (mask);
2386 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2391 GtkRequisition requisition;
2392 GtkToolItemGroupPrivate* priv = group->priv;
2394 gtk_size_request_get_size (GTK_SIZE_REQUEST (group),
2395 &requisition, NULL);
2397 if (!priv->collapsed || priv->animation_timeout)
2399 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2400 GtkRequisition inquery;
2403 allocation.width = limit;
2405 allocation.height = limit;
2407 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2408 &allocation, &inquery);
2411 inquery.height -= requisition.height;
2413 inquery.width -= requisition.width;
2415 if (priv->animation_timeout && animation)
2417 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2419 timestamp = MIN (timestamp, ANIMATION_DURATION);
2421 if (priv->collapsed)
2422 timestamp = ANIMATION_DURATION - timestamp;
2426 inquery.height *= timestamp;
2427 inquery.height /= ANIMATION_DURATION;
2431 inquery.width *= timestamp;
2432 inquery.width /= ANIMATION_DURATION;
2437 requisition.height += inquery.height;
2439 requisition.width += inquery.width;
2442 return (vertical ? requisition.height : requisition.width);
2446 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2449 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2453 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2456 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2460 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2463 if (GTK_IS_TOOL_ITEM (child))
2464 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2469 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2471 gtk_container_foreach (GTK_CONTAINER (group),
2472 gtk_tool_palette_reconfigured_foreach_item,
2475 gtk_tool_item_group_header_adjust_style (group);