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"
34 #define ANIMATION_TIMEOUT 50
35 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
36 #define DEFAULT_ANIMATION_STATE TRUE
37 #define DEFAULT_EXPANDER_SIZE 16
38 #define DEFAULT_HEADER_SPACING 2
40 #define DEFAULT_LABEL ""
41 #define DEFAULT_COLLAPSED FALSE
42 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
45 * SECTION:gtktoolitemgroup
46 * @Short_description: A sub container used in a tool palette
47 * @Title: GtkToolItemGroup
49 * A #GtkToolItemGroup is used together with #GtkToolPalette to add
50 * #GtkToolItem<!-- -->s to a palette like container with different
51 * categories and drag and drop support.
69 CHILD_PROP_HOMOGENEOUS,
76 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
78 struct _GtkToolItemGroupPrivate
81 GtkWidget *label_widget;
86 gint64 animation_start;
87 GSource *animation_timeout;
88 GtkExpanderStyle expander_style;
91 PangoEllipsizeMode ellipsize;
96 GtkSettings *settings;
97 gulong settings_connection;
102 struct _GtkToolItemGroupChild
106 guint homogeneous : 1;
112 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
114 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
115 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
118 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
120 return gtk_bin_get_child (GTK_BIN (group->priv->header));
123 static GtkOrientation
124 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
126 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
128 if (GTK_IS_TOOL_PALETTE (parent))
129 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
131 return GTK_ORIENTATION_VERTICAL;
134 static GtkToolbarStyle
135 gtk_tool_item_group_get_style (GtkToolShell *shell)
137 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
139 if (GTK_IS_TOOL_PALETTE (parent))
140 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
142 return GTK_TOOLBAR_ICONS;
146 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
148 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
150 if (GTK_IS_TOOL_PALETTE (parent))
151 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
153 return GTK_ICON_SIZE_SMALL_TOOLBAR;
156 static PangoEllipsizeMode
157 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
159 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
163 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
165 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
166 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
172 static GtkOrientation
173 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
175 return GTK_ORIENTATION_HORIZONTAL;
178 static GtkSizeGroup *
179 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
181 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
183 if (GTK_IS_TOOL_PALETTE (parent))
184 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
190 animation_change_notify (GtkToolItemGroup *group)
192 GtkSettings *settings = group->priv->settings;
196 g_object_get (settings,
197 "gtk-enable-animations", &animation,
200 animation = DEFAULT_ANIMATION_STATE;
202 group->priv->animation = animation;
206 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
207 const GParamSpec *pspec,
208 GtkToolItemGroup *group)
210 if (strcmp (pspec->name, "gtk-enable-animations") == 0)
211 animation_change_notify (group);
215 gtk_tool_item_group_screen_changed (GtkWidget *widget,
216 GdkScreen *previous_screen)
218 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
219 GtkToolItemGroupPrivate* priv = group->priv;
220 GtkSettings *old_settings = priv->settings;
221 GtkSettings *settings;
223 if (gtk_widget_has_screen (GTK_WIDGET (group)))
224 settings = gtk_widget_get_settings (GTK_WIDGET (group));
228 if (settings == old_settings)
233 g_signal_handler_disconnect (old_settings, priv->settings_connection);
234 g_object_unref (old_settings);
239 priv->settings_connection =
240 g_signal_connect (settings, "notify",
241 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
243 priv->settings = g_object_ref (settings);
246 priv->settings = NULL;
248 animation_change_notify (group);
252 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
254 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
255 iface->get_orientation = gtk_tool_item_group_get_orientation;
256 iface->get_style = gtk_tool_item_group_get_style;
257 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
258 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
259 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
260 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
264 gtk_tool_item_group_header_expose_event_cb (GtkWidget *widget,
265 GdkEventExpose *event,
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 if (GTK_ORIENTATION_VERTICAL == orientation)
281 if (GTK_TEXT_DIR_RTL == direction)
282 x = widget->allocation.x + widget->allocation.width - priv->expander_size / 2;
284 x = widget->allocation.x + priv->expander_size / 2;
285 y = widget->allocation.y + widget->allocation.height / 2;
289 x = widget->allocation.x + widget->allocation.width / 2;
290 y = widget->allocation.y + priv->expander_size / 2;
292 /* Unfortunatly gtk_paint_expander() doesn't support rotated drawing
293 * modes. Luckily the following shady arithmetics produce the desired
295 expander_style = GTK_EXPANDER_EXPANDED - expander_style;
298 gtk_paint_expander (widget->style, widget->window,
300 &event->area, GTK_WIDGET (group),
301 "tool-palette-header", x, y,
308 gtk_tool_item_group_header_size_request_cb (GtkWidget *widget,
309 GtkRequisition *requisition,
312 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
313 requisition->height = MAX (requisition->height, group->priv->expander_size);
317 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
320 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
321 GtkToolItemGroupPrivate* priv = group->priv;
322 GtkWidget *parent = gtk_widget_get_parent (data);
324 if (priv->collapsed ||
325 !GTK_IS_TOOL_PALETTE (parent) ||
326 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
327 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
331 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
333 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
334 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
335 GtkWidget *widget = GTK_WIDGET (group);
336 GtkToolItemGroupPrivate* priv = group->priv;
338 GtkTextDirection direction = gtk_widget_get_direction (widget);
340 gtk_widget_style_get (widget,
341 "header-spacing", &(priv->header_spacing),
342 "expander-size", &(priv->expander_size),
345 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
347 case GTK_ORIENTATION_HORIZONTAL:
348 dy = priv->header_spacing + priv->expander_size;
350 if (GTK_IS_LABEL (label_widget))
352 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
353 if (GTK_TEXT_DIR_RTL == direction)
354 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
356 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
360 case GTK_ORIENTATION_VERTICAL:
361 dx = priv->header_spacing + priv->expander_size;
363 if (GTK_IS_LABEL (label_widget))
365 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
366 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
371 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
375 gtk_tool_item_group_init (GtkToolItemGroup *group)
377 GtkWidget *alignment;
378 GtkToolItemGroupPrivate* priv;
380 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
382 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
383 GTK_TYPE_TOOL_ITEM_GROUP,
384 GtkToolItemGroupPrivate);
386 priv->children = NULL;
387 priv->header_spacing = DEFAULT_HEADER_SPACING;
388 priv->expander_size = DEFAULT_EXPANDER_SIZE;
389 priv->expander_style = GTK_EXPANDER_EXPANDED;
391 priv->label_widget = gtk_label_new (NULL);
392 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
393 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
394 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
395 gtk_widget_show_all (alignment);
397 gtk_widget_push_composite_child ();
398 priv->header = gtk_button_new ();
399 gtk_widget_set_composite_name (priv->header, "header");
400 gtk_widget_pop_composite_child ();
402 g_object_ref_sink (priv->header);
403 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
404 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
405 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
407 gtk_tool_item_group_header_adjust_style (group);
409 g_signal_connect_after (alignment, "expose-event",
410 G_CALLBACK (gtk_tool_item_group_header_expose_event_cb),
412 g_signal_connect_after (alignment, "size-request",
413 G_CALLBACK (gtk_tool_item_group_header_size_request_cb),
416 g_signal_connect (priv->header, "clicked",
417 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
422 gtk_tool_item_group_set_property (GObject *object,
427 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
432 gtk_tool_item_group_set_label (group, g_value_get_string (value));
435 case PROP_LABEL_WIDGET:
436 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
440 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
444 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
448 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
452 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458 gtk_tool_item_group_get_property (GObject *object,
463 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
468 g_value_set_string (value, gtk_tool_item_group_get_label (group));
471 case PROP_LABEL_WIDGET:
472 g_value_set_object (value,
473 gtk_tool_item_group_get_label_widget (group));
477 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
481 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
485 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
489 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
495 gtk_tool_item_group_finalize (GObject *object)
497 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
499 if (group->priv->children)
501 g_list_free (group->priv->children);
502 group->priv->children = NULL;
505 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
509 gtk_tool_item_group_dispose (GObject *object)
511 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
512 GtkToolItemGroupPrivate* priv = group->priv;
516 /* disconnect focus tracking handler */
517 g_signal_handler_disconnect (priv->toplevel,
520 priv->focus_set_id = 0;
521 priv->toplevel = NULL;
524 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
528 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
529 GtkRequisition *item_size,
530 gboolean homogeneous_only,
531 gint *requested_rows)
533 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
535 if (GTK_IS_TOOL_PALETTE (parent))
536 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
538 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
542 gtk_tool_item_group_size_request (GtkWidget *widget,
543 GtkRequisition *requisition)
545 const gint border_width = GTK_CONTAINER (widget)->border_width;
546 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
547 GtkToolItemGroupPrivate* priv = group->priv;
548 GtkOrientation orientation;
549 GtkRequisition item_size;
552 if (priv->children && gtk_tool_item_group_get_label_widget (group))
554 gtk_widget_size_request (priv->header, requisition);
555 gtk_widget_show (priv->header);
559 requisition->width = requisition->height = 0;
560 gtk_widget_hide (priv->header);
563 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
565 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
567 if (GTK_ORIENTATION_VERTICAL == orientation)
568 requisition->width = MAX (requisition->width, item_size.width);
570 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
572 requisition->width += border_width * 2;
573 requisition->height += border_width * 2;
577 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
578 GtkToolItemGroupChild *child)
580 GtkToolbarStyle style;
581 GtkOrientation orientation;
583 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
584 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
586 /* horizontal tool palettes with text style support only homogeneous items */
587 if (!child->homogeneous &&
588 GTK_ORIENTATION_HORIZONTAL == orientation &&
589 GTK_TOOLBAR_TEXT == style)
593 (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
594 (GTK_ORIENTATION_VERTICAL == orientation ?
595 gtk_tool_item_get_visible_vertical (child->item) :
596 gtk_tool_item_get_visible_horizontal (child->item));
599 static inline unsigned
603 return (x + y - 1) / y;
607 gtk_tool_item_group_real_size_query (GtkWidget *widget,
608 GtkAllocation *allocation,
609 GtkRequisition *inquery)
611 const gint border_width = GTK_CONTAINER (widget)->border_width;
612 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
613 GtkToolItemGroupPrivate* priv = group->priv;
615 GtkRequisition item_size;
616 GtkAllocation item_area;
618 GtkOrientation orientation;
619 GtkToolbarStyle style;
623 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
624 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
626 /* figure out the size of homogeneous items */
627 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
629 if (GTK_ORIENTATION_VERTICAL == orientation)
630 item_size.width = MIN (item_size.width, allocation->width);
632 item_size.height = MIN (item_size.height, allocation->height);
634 item_size.width = MAX (item_size.width, 1);
635 item_size.height = MAX (item_size.height, 1);
638 item_area.height = 0;
640 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
641 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
647 if (GTK_ORIENTATION_VERTICAL == orientation)
649 gboolean new_row = FALSE;
653 item_area.width = allocation->width - 2 * border_width;
654 n_columns = MAX (item_area.width / item_size.width, 1);
656 /* calculate required rows for n_columns columns */
657 for (it = priv->children; it != NULL; it = it->next)
659 GtkToolItemGroupChild *child = it->data;
661 if (!gtk_tool_item_group_is_item_visible (group, child))
664 if (new_row || child->new_row)
674 if (child->homogeneous)
677 if (col >= n_columns)
682 GtkRequisition req = {0, 0};
685 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
687 width = udiv (req.width, item_size.width);
695 if (col >= n_columns)
703 guint *row_min_width;
705 gboolean new_row = TRUE;
706 guint col = 0, min_col, max_col = 0, all_items = 0;
709 item_area.height = allocation->height - 2 * border_width;
710 n_rows = MAX (item_area.height / item_size.height, min_rows);
712 row_min_width = g_new0 (guint, n_rows);
714 /* calculate minimal and maximal required cols and minimal required rows */
715 for (it = priv->children; it != NULL; it = it->next)
717 GtkToolItemGroupChild *child = it->data;
719 if (!gtk_tool_item_group_is_item_visible (group, child))
722 if (new_row || child->new_row)
727 row_min_width[row] = 1;
733 if (child->homogeneous)
740 GtkRequisition req = {0, 0};
743 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
745 width = udiv (req.width, item_size.width);
750 row_min_width[row] = MAX (row_min_width[row], width);
753 max_col = MAX (max_col, col);
756 /* calculate minimal required cols */
757 min_col = udiv (all_items, n_rows);
759 for (i = 0; i <= row; i++)
761 min_col = MAX (min_col, row_min_width[i]);
764 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
765 for (n_columns = min_col; n_columns < max_col; n_columns ++)
769 /* calculate required rows for n_columns columns */
770 for (it = priv->children; it != NULL; it = it->next)
772 GtkToolItemGroupChild *child = it->data;
774 if (!gtk_tool_item_group_is_item_visible (group, child))
777 if (new_row || child->new_row)
787 if (child->homogeneous)
790 if (col >= n_columns)
795 GtkRequisition req = {0, 0};
798 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
800 width = udiv (req.width, item_size.width);
808 if (col >= n_columns)
818 item_area.width = item_size.width * n_columns;
819 item_area.height = item_size.height * n_rows;
825 /* figure out header widget size */
826 if (gtk_widget_get_visible (priv->header))
828 GtkRequisition child_requisition;
830 gtk_widget_size_request (priv->header, &child_requisition);
832 if (GTK_ORIENTATION_VERTICAL == orientation)
833 inquery->height += child_requisition.height;
835 inquery->width += child_requisition.width;
838 /* report effective widget size */
839 inquery->width += item_area.width + 2 * border_width;
840 inquery->height += item_area.height + 2 * border_width;
844 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
845 GtkAllocation *allocation)
847 const gint border_width = GTK_CONTAINER (widget)->border_width;
848 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
849 GtkToolItemGroupPrivate* priv = group->priv;
850 GtkRequisition child_requisition;
851 GtkAllocation child_allocation;
853 GtkRequisition item_size;
854 GtkAllocation item_area;
856 GtkOrientation orientation;
857 GtkToolbarStyle style;
861 gint n_columns, n_rows = 1;
864 GtkTextDirection direction = gtk_widget_get_direction (widget);
866 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
867 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
870 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
872 child_allocation.x = border_width;
873 child_allocation.y = border_width;
875 /* place the header widget */
876 if (gtk_widget_get_visible (priv->header))
878 gtk_widget_size_request (priv->header, &child_requisition);
880 if (GTK_ORIENTATION_VERTICAL == orientation)
882 child_allocation.width = allocation->width;
883 child_allocation.height = child_requisition.height;
887 child_allocation.width = child_requisition.width;
888 child_allocation.height = allocation->height;
890 if (GTK_TEXT_DIR_RTL == direction)
891 child_allocation.x = allocation->width - border_width - child_allocation.width;
894 gtk_widget_size_allocate (priv->header, &child_allocation);
896 if (GTK_ORIENTATION_VERTICAL == orientation)
897 child_allocation.y += child_allocation.height;
898 else if (GTK_TEXT_DIR_RTL != direction)
899 child_allocation.x += child_allocation.width;
901 child_allocation.x = border_width;
904 child_requisition.width = child_requisition.height = 0;
906 /* figure out the size of homogeneous items */
907 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
909 item_size.width = MAX (item_size.width, 1);
910 item_size.height = MAX (item_size.height, 1);
912 /* figure out the available columns and size of item_area */
913 if (GTK_ORIENTATION_VERTICAL == orientation)
915 item_size.width = MIN (item_size.width, allocation->width);
917 item_area.width = allocation->width - 2 * border_width;
918 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
920 n_columns = MAX (item_area.width / item_size.width, 1);
922 item_size.width = item_area.width / n_columns;
926 item_size.height = MIN (item_size.height, allocation->height);
928 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
929 item_area.height = allocation->height - 2 * border_width;
931 n_columns = MAX (item_area.width / item_size.width, 1);
932 n_rows = MAX (item_area.height / item_size.height, min_rows);
934 item_size.height = item_area.height / n_rows;
937 item_area.x = child_allocation.x;
938 item_area.y = child_allocation.y;
940 /* when expanded or in transition, place the tool items in a grid like layout */
941 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
943 gint col = 0, row = 0;
945 for (it = priv->children; it != NULL; it = it->next)
947 GtkToolItemGroupChild *child = it->data;
950 if (!gtk_tool_item_group_is_item_visible (group, child))
952 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
957 /* for non homogeneous widgets request the required size */
958 child_requisition.width = 0;
960 if (!child->homogeneous)
962 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
963 child_requisition.width = MIN (child_requisition.width, item_area.width);
966 /* select next row if at end of row */
967 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
971 child_allocation.y += child_allocation.height;
976 /* calculate the position and size of the item */
977 if (!child->homogeneous)
983 col_width = udiv (child_requisition.width, item_size.width);
985 col_width = n_columns - col;
987 width = col_width * item_size.width;
989 if (GTK_TEXT_DIR_RTL == direction)
990 col_child = (n_columns - col - col_width);
994 child_allocation.x = item_area.x + col_child * item_size.width;
995 child_allocation.width = width;
1000 (item_area.x + col_child * item_size.width +
1001 (width - child_requisition.width) / 2);
1002 child_allocation.width = child_requisition.width;
1009 if (GTK_TEXT_DIR_RTL == direction)
1010 col_child = (n_columns - col - 1);
1012 child_allocation.x = item_area.x + col_child * item_size.width;
1013 child_allocation.width = item_size.width;
1018 child_allocation.height = item_size.height;
1020 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1021 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1024 child_allocation.y += item_size.height;
1027 /* or just hide all items, when collapsed */
1031 for (it = priv->children; it != NULL; it = it->next)
1033 GtkToolItemGroupChild *child = it->data;
1035 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1041 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1042 GtkAllocation *allocation)
1044 gtk_tool_item_group_real_size_allocate (widget, allocation);
1046 if (gtk_widget_get_mapped (widget))
1047 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
1051 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1055 GtkAdjustment *adjustment;
1058 /* Find this group's parent widget in the focused widget's anchestry. */
1059 for (p = widget; p; p = gtk_widget_get_parent (p))
1062 p = gtk_widget_get_parent (p);
1066 if (GTK_IS_TOOL_PALETTE (p))
1068 /* Check that the focused widgets is fully visible within
1069 * the group's parent widget and make it visible otherwise. */
1071 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1072 adjustment = gtk_tool_palette_get_vadjustment (GTK_TOOL_PALETTE (p));
1078 /* Handle vertical adjustment. */
1079 if (gtk_widget_translate_coordinates
1080 (widget, p, 0, 0, NULL, &y) && y < 0)
1082 y += adjustment->value;
1083 gtk_adjustment_clamp_page (adjustment, y, y + widget->allocation.height);
1085 else if (gtk_widget_translate_coordinates
1086 (widget, p, 0, widget->allocation.height, NULL, &y) &&
1087 y > p->allocation.height)
1089 y += adjustment->value;
1090 gtk_adjustment_clamp_page (adjustment, y - widget->allocation.height, y);
1094 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1100 /* Handle horizontal adjustment. */
1101 if (gtk_widget_translate_coordinates
1102 (widget, p, 0, 0, &x, NULL) && x < 0)
1104 x += adjustment->value;
1105 gtk_adjustment_clamp_page (adjustment, x, x + widget->allocation.width);
1107 else if (gtk_widget_translate_coordinates
1108 (widget, p, widget->allocation.width, 0, &x, NULL) &&
1109 x > p->allocation.width)
1111 x += adjustment->value;
1112 gtk_adjustment_clamp_page (adjustment, x - widget->allocation.width, x);
1121 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1122 GtkWidget *toplevel)
1124 GtkToolItemGroupPrivate* priv = group->priv;
1126 if (toplevel != priv->toplevel)
1130 /* Disconnect focus tracking handler. */
1131 g_signal_handler_disconnect (priv->toplevel,
1132 priv->focus_set_id);
1134 priv->focus_set_id = 0;
1135 priv->toplevel = NULL;
1140 /* Install focus tracking handler. We connect to the window's
1141 * set-focus signal instead of connecting to the focus signal of
1144 * 1) Reduce the number of signal handlers used.
1145 * 2) Avoid special handling for group headers.
1146 * 3) Catch focus grabs not only for direct children,
1147 * but also for nested widgets.
1149 priv->focus_set_id =
1150 g_signal_connect (toplevel, "set-focus",
1151 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1154 priv->toplevel = toplevel;
1160 gtk_tool_item_group_realize (GtkWidget *widget)
1162 GtkWidget *toplevel_window;
1163 const gint border_width = GTK_CONTAINER (widget)->border_width;
1164 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1165 GdkWindowAttr attributes;
1166 GdkDisplay *display;
1168 attributes.window_type = GDK_WINDOW_CHILD;
1169 attributes.x = widget->allocation.x + border_width;
1170 attributes.y = widget->allocation.y + border_width;
1171 attributes.width = widget->allocation.width - border_width * 2;
1172 attributes.height = widget->allocation.height - border_width * 2;
1173 attributes.wclass = GDK_INPUT_OUTPUT;
1174 attributes.visual = gtk_widget_get_visual (widget);
1175 attributes.colormap = gtk_widget_get_colormap (widget);
1176 attributes.event_mask = gtk_widget_get_events (widget)
1177 | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1178 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1179 | GDK_BUTTON_MOTION_MASK;
1181 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1182 &attributes, attributes_mask);
1184 display = gdk_drawable_get_display (widget->window);
1186 if (gdk_display_supports_composite (display))
1187 gdk_window_set_composited (widget->window, TRUE);
1189 gdk_window_set_user_data (widget->window, widget);
1190 widget->style = gtk_style_attach (widget->style, widget->window);
1191 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1192 gtk_widget_set_realized (widget, TRUE);
1194 gtk_container_forall (GTK_CONTAINER (widget),
1195 (GtkCallback) gtk_widget_set_parent_window,
1198 gtk_widget_queue_resize_no_redraw (widget);
1200 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1201 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1206 gtk_tool_item_group_unrealize (GtkWidget *widget)
1208 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1209 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1213 gtk_tool_item_group_style_set (GtkWidget *widget,
1214 GtkStyle *previous_style)
1216 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1217 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1221 gtk_tool_item_group_add (GtkContainer *container,
1224 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1225 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1227 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1228 GTK_TOOL_ITEM (widget), -1);
1232 gtk_tool_item_group_remove (GtkContainer *container,
1235 GtkToolItemGroup *group;
1236 GtkToolItemGroupPrivate* priv;
1239 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1240 group = GTK_TOOL_ITEM_GROUP (container);
1243 for (it = priv->children; it != NULL; it = it->next)
1245 GtkToolItemGroupChild *child_info = it->data;
1247 if ((GtkWidget *)child_info->item == child)
1249 g_object_unref (child);
1250 gtk_widget_unparent (child);
1252 g_free (child_info);
1253 priv->children = g_list_delete_link (priv->children, it);
1255 gtk_widget_queue_resize (GTK_WIDGET (container));
1262 gtk_tool_item_group_forall (GtkContainer *container,
1264 GtkCallback callback,
1265 gpointer callback_data)
1267 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1268 GtkToolItemGroupPrivate* priv = group->priv;
1271 if (internals && priv->header)
1272 callback (priv->header, callback_data);
1274 children = priv->children;
1277 GtkToolItemGroupChild *child = children->data;
1278 children = children->next; /* store pointer before call to callback
1279 because the child pointer is invalid if the
1280 child->item is removed from the item group
1283 callback (GTK_WIDGET (child->item), callback_data);
1288 gtk_tool_item_group_child_type (GtkContainer *container)
1290 return GTK_TYPE_TOOL_ITEM;
1293 static GtkToolItemGroupChild *
1294 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1302 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1303 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1305 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1307 GtkToolItemGroupChild *child = it->data;
1309 if (child->item == item)
1325 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1327 gboolean *homogeneous,
1332 GtkToolItemGroupChild *child;
1334 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1335 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1337 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1342 *expand = child->expand;
1345 *homogeneous = child->homogeneous;
1348 *fill = child->fill;
1351 *new_row = child->new_row;
1355 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1357 gboolean homogeneous,
1362 GtkToolItemGroupChild *child;
1363 gboolean changed = FALSE;
1365 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1366 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1368 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1372 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1374 if (child->homogeneous != homogeneous)
1376 child->homogeneous = homogeneous;
1378 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1380 if (child->expand != expand)
1382 child->expand = expand;
1384 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1386 if (child->fill != fill)
1390 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1392 if (child->new_row != new_row)
1394 child->new_row = new_row;
1396 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1399 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1402 && gtk_widget_get_visible (GTK_WIDGET (group))
1403 && gtk_widget_get_visible (GTK_WIDGET (item)))
1404 gtk_widget_queue_resize (GTK_WIDGET (group));
1408 gtk_tool_item_group_set_child_property (GtkContainer *container,
1411 const GValue *value,
1414 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1415 GtkToolItem *item = GTK_TOOL_ITEM (child);
1416 gboolean homogeneous, expand, fill, new_row;
1418 if (prop_id != CHILD_PROP_POSITION)
1419 gtk_tool_item_group_get_item_packing (group, item,
1427 case CHILD_PROP_HOMOGENEOUS:
1428 gtk_tool_item_group_set_item_packing (group, item,
1429 g_value_get_boolean (value),
1435 case CHILD_PROP_EXPAND:
1436 gtk_tool_item_group_set_item_packing (group, item,
1438 g_value_get_boolean (value),
1443 case CHILD_PROP_FILL:
1444 gtk_tool_item_group_set_item_packing (group, item,
1447 g_value_get_boolean (value),
1451 case CHILD_PROP_NEW_ROW:
1452 gtk_tool_item_group_set_item_packing (group, item,
1456 g_value_get_boolean (value));
1459 case CHILD_PROP_POSITION:
1460 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1464 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1470 gtk_tool_item_group_get_child_property (GtkContainer *container,
1476 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1477 GtkToolItem *item = GTK_TOOL_ITEM (child);
1478 gboolean homogeneous, expand, fill, new_row;
1480 if (prop_id != CHILD_PROP_POSITION)
1481 gtk_tool_item_group_get_item_packing (group, item,
1489 case CHILD_PROP_HOMOGENEOUS:
1490 g_value_set_boolean (value, homogeneous);
1493 case CHILD_PROP_EXPAND:
1494 g_value_set_boolean (value, expand);
1497 case CHILD_PROP_FILL:
1498 g_value_set_boolean (value, fill);
1501 case CHILD_PROP_NEW_ROW:
1502 g_value_set_boolean (value, new_row);
1505 case CHILD_PROP_POSITION:
1506 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1510 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1516 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1518 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1519 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1520 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1522 oclass->set_property = gtk_tool_item_group_set_property;
1523 oclass->get_property = gtk_tool_item_group_get_property;
1524 oclass->finalize = gtk_tool_item_group_finalize;
1525 oclass->dispose = gtk_tool_item_group_dispose;
1527 wclass->size_request = gtk_tool_item_group_size_request;
1528 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1529 wclass->realize = gtk_tool_item_group_realize;
1530 wclass->unrealize = gtk_tool_item_group_unrealize;
1531 wclass->style_set = gtk_tool_item_group_style_set;
1532 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1534 cclass->add = gtk_tool_item_group_add;
1535 cclass->remove = gtk_tool_item_group_remove;
1536 cclass->forall = gtk_tool_item_group_forall;
1537 cclass->child_type = gtk_tool_item_group_child_type;
1538 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1539 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1541 g_object_class_install_property (oclass, PROP_LABEL,
1542 g_param_spec_string ("label",
1544 P_("The human-readable title of this item group"),
1546 GTK_PARAM_READWRITE));
1548 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1549 g_param_spec_object ("label-widget",
1551 P_("A widget to display in place of the usual label"),
1553 GTK_PARAM_READWRITE));
1555 g_object_class_install_property (oclass, PROP_COLLAPSED,
1556 g_param_spec_boolean ("collapsed",
1558 P_("Wether the group has been collapsed and items are hidden"),
1560 GTK_PARAM_READWRITE));
1562 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1563 g_param_spec_enum ("ellipsize",
1565 P_("Ellipsize for item group headers"),
1566 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1567 GTK_PARAM_READWRITE));
1569 g_object_class_install_property (oclass, PROP_RELIEF,
1570 g_param_spec_enum ("header-relief",
1571 P_("Header Relief"),
1572 P_("Relief of the group header button"),
1573 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1574 GTK_PARAM_READWRITE));
1576 gtk_widget_class_install_style_property (wclass,
1577 g_param_spec_int ("expander-size",
1578 P_("Expander Size"),
1579 P_("Size of the expander arrow"),
1582 DEFAULT_EXPANDER_SIZE,
1583 GTK_PARAM_READABLE));
1585 gtk_widget_class_install_style_property (wclass,
1586 g_param_spec_int ("header-spacing",
1587 P_("Header Spacing"),
1588 P_("Spacing between expander arrow and caption"),
1591 DEFAULT_HEADER_SPACING,
1592 GTK_PARAM_READABLE));
1594 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1595 g_param_spec_boolean ("homogeneous",
1597 P_("Whether the item should be the same size as other homogeneous items"),
1599 GTK_PARAM_READWRITE));
1601 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1602 g_param_spec_boolean ("expand",
1604 P_("Whether the item should receive extra space when the group grows"),
1606 GTK_PARAM_READWRITE));
1608 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1609 g_param_spec_boolean ("fill",
1611 P_("Whether the item should fill the available space"),
1613 GTK_PARAM_READWRITE));
1615 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1616 g_param_spec_boolean ("new-row",
1618 P_("Whether the item should start a new row"),
1620 GTK_PARAM_READWRITE));
1622 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1623 g_param_spec_int ("position",
1625 P_("Position of the item within this group"),
1629 GTK_PARAM_READWRITE));
1631 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1635 * gtk_tool_item_group_new:
1636 * @label: the label of the new group
1638 * Creates a new tool item group with label @label.
1640 * Returns: a new #GtkToolItemGroup.
1645 gtk_tool_item_group_new (const gchar *label)
1647 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1651 * gtk_tool_item_group_set_label:
1652 * @group: a #GtkToolItemGroup
1653 * @label: the new human-readable label of of the group
1655 * Sets the label of the tool item group. The label is displayed in the header
1661 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1664 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1667 gtk_tool_item_group_set_label_widget (group, NULL);
1670 GtkWidget *child = gtk_label_new (label);
1671 gtk_widget_show (child);
1673 gtk_tool_item_group_set_label_widget (group, child);
1676 g_object_notify (G_OBJECT (group), "label");
1680 * gtk_tool_item_group_set_label_widget:
1681 * @group: a #GtkToolItemGroup
1682 * @label_widget: the widget to be displayed in place of the usual label
1684 * Sets the label of the tool item group.
1685 * The label widget is displayed in the header of the group, in place
1686 * of the usual label.
1691 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1692 GtkWidget *label_widget)
1694 GtkToolItemGroupPrivate* priv;
1695 GtkWidget *alignment;
1697 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1698 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1699 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1703 if (priv->label_widget == label_widget)
1706 alignment = gtk_tool_item_group_get_alignment (group);
1708 if (priv->label_widget)
1710 gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1711 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1716 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1718 priv->label_widget = label_widget;
1720 if (gtk_widget_get_visible (GTK_WIDGET (group)))
1721 gtk_widget_queue_resize (GTK_WIDGET (group));
1723 /* Only show the header widget if the group has children: */
1724 if (label_widget && priv->children)
1725 gtk_widget_show (priv->header);
1727 gtk_widget_hide (priv->header);
1729 g_object_freeze_notify (G_OBJECT (group));
1730 g_object_notify (G_OBJECT (group), "label-widget");
1731 g_object_notify (G_OBJECT (group), "label");
1732 g_object_thaw_notify (G_OBJECT (group));
1736 * gtk_tool_item_group_set_header_relief:
1737 * @group: a #GtkToolItemGroup
1738 * @style: the #GtkReliefStyle
1740 * Set the button relief of the group header.
1741 * See gtk_button_set_relief() for details.
1746 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1747 GtkReliefStyle style)
1749 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1751 gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1755 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1759 g_source_get_current_time (group->priv->animation_timeout, &now);
1760 return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1764 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1766 GtkToolItemGroupPrivate* priv = group->priv;
1767 GtkWidget *widget = GTK_WIDGET (group);
1769 if (gtk_widget_get_realized (priv->header))
1771 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1774 /* Find the header button's arrow area... */
1775 area.x = alignment->allocation.x;
1776 area.y = alignment->allocation.y + (alignment->allocation.height - priv->expander_size) / 2;
1777 area.height = priv->expander_size;
1778 area.width = priv->expander_size;
1780 /* ... and invalidated it to get it animated. */
1781 gdk_window_invalidate_rect (priv->header->window, &area, TRUE);
1784 if (gtk_widget_get_realized (widget))
1786 GtkWidget *parent = gtk_widget_get_parent (widget);
1787 int x, y, width, height;
1789 /* Find the tool item area button's arrow area... */
1790 width = widget->allocation.width;
1791 height = widget->allocation.height;
1793 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1795 if (gtk_widget_get_visible (priv->header))
1797 height -= priv->header->allocation.height;
1798 y += priv->header->allocation.height;
1801 /* ... and invalidated it to get it animated. */
1802 gtk_widget_queue_draw_area (parent, x, y, width, height);
1807 gtk_tool_item_group_animation_cb (gpointer data)
1809 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1810 GtkToolItemGroupPrivate* priv = group->priv;
1811 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1814 GDK_THREADS_ENTER ();
1816 /* Enque this early to reduce number of expose events. */
1817 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1819 /* Figure out current style of the expander arrow. */
1820 if (priv->collapsed)
1822 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1823 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1825 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1829 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1830 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1832 priv->expander_style = GTK_EXPANDER_EXPANDED;
1835 gtk_tool_item_group_force_expose (group);
1837 /* Finish animation when done. */
1838 if (timestamp >= ANIMATION_DURATION)
1839 priv->animation_timeout = NULL;
1841 retval = (priv->animation_timeout != NULL);
1843 GDK_THREADS_LEAVE ();
1849 * gtk_tool_item_group_set_collapsed:
1850 * @group: a #GtkToolItemGroup
1851 * @collapsed: whether the @group should be collapsed or expanded
1853 * Sets whether the @group should be collapsed or expanded.
1858 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1862 GtkToolItemGroupPrivate* priv;
1864 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1868 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1869 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1870 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1871 GTK_WIDGET (group));
1872 if (collapsed != priv->collapsed)
1874 if (priv->animation)
1878 g_get_current_time (&now);
1880 if (priv->animation_timeout)
1881 g_source_destroy (priv->animation_timeout);
1883 priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1884 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1886 g_source_set_callback (priv->animation_timeout,
1887 gtk_tool_item_group_animation_cb,
1890 g_source_attach (priv->animation_timeout, NULL);
1894 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1895 gtk_tool_item_group_force_expose (group);
1898 priv->collapsed = collapsed;
1899 g_object_notify (G_OBJECT (group), "collapsed");
1904 * gtk_tool_item_group_set_ellipsize:
1905 * @group: a #GtkToolItemGroup
1906 * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1908 * Sets the ellipsization mode which should be used by labels in @group.
1913 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1914 PangoEllipsizeMode ellipsize)
1916 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1918 if (ellipsize != group->priv->ellipsize)
1920 group->priv->ellipsize = ellipsize;
1921 gtk_tool_item_group_header_adjust_style (group);
1922 g_object_notify (G_OBJECT (group), "ellipsize");
1923 _gtk_tool_item_group_palette_reconfigured (group);
1928 * gtk_tool_item_group_get_label:
1929 * @group: a #GtkToolItemGroup
1931 * Gets the label of @group.
1933 * Returns: the label of @group. The label is an internal string of @group
1934 * and must not be modified. Note that %NULL is returned if a custom
1935 * label has been set with gtk_tool_item_group_set_label_widget()
1939 G_CONST_RETURN gchar*
1940 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1942 GtkToolItemGroupPrivate *priv;
1944 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1948 if (GTK_IS_LABEL (priv->label_widget))
1949 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1955 * gtk_tool_item_group_get_label_widget:
1956 * @group: a #GtkToolItemGroup
1958 * Gets the label widget of @group.
1959 * See gtk_tool_item_group_set_label_widget().
1961 * Returns: the label widget of @group
1966 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
1968 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1970 return gtk_bin_get_child (GTK_BIN (alignment));
1974 * gtk_tool_item_group_get_collapsed:
1975 * @group: a GtkToolItemGroup
1977 * Gets whether @group is collapsed or expanded.
1979 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
1984 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
1986 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
1988 return group->priv->collapsed;
1992 * gtk_tool_item_group_get_ellipsize:
1993 * @group: a #GtkToolItemGroup
1995 * Gets the ellipsization mode of @group.
1997 * Returns: the #PangoEllipsizeMode of @group
2002 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2004 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2006 return group->priv->ellipsize;
2010 * gtk_tool_item_group_get_header_relief:
2011 * @group: a #GtkToolItemGroup
2013 * Gets the relief mode of the header button of @group.
2015 * Returns: the #GtkReliefStyle
2020 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2022 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2024 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2028 * gtk_tool_item_group_insert:
2029 * @group: a #GtkToolItemGroup
2030 * @item: the #GtkToolItem to insert into @group
2031 * @position: the position of @item in @group, starting with 0.
2032 * The position -1 means end of list.
2034 * Inserts @item at @position in the list of children of @group.
2039 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2043 GtkWidget *parent, *child_widget;
2044 GtkToolItemGroupChild *child;
2046 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2047 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2048 g_return_if_fail (position >= -1);
2050 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2052 child = g_new (GtkToolItemGroupChild, 1);
2053 child->item = g_object_ref_sink (item);
2054 child->homogeneous = TRUE;
2055 child->expand = FALSE;
2057 child->new_row = FALSE;
2059 group->priv->children = g_list_insert (group->priv->children, child, position);
2061 if (GTK_IS_TOOL_PALETTE (parent))
2062 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2064 child_widget = gtk_bin_get_child (GTK_BIN (item));
2066 if (GTK_IS_BUTTON (child_widget))
2067 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2069 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2073 * gtk_tool_item_group_set_item_position:
2074 * @group: a #GtkToolItemGroup
2075 * @item: the #GtkToolItem to move to a new position, should
2076 * be a child of @group.
2077 * @position: the new position of @item in @group, starting with 0.
2078 * The position -1 means end of list.
2080 * Sets the position of @item in the list of children of @group.
2085 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2091 GtkToolItemGroupChild *child;
2092 GtkToolItemGroupPrivate* priv;
2094 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2095 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2096 g_return_if_fail (position >= -1);
2098 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2101 g_return_if_fail (child != NULL);
2103 if (position == old_position)
2106 priv->children = g_list_delete_link (priv->children, link);
2107 priv->children = g_list_insert (priv->children, child, position);
2109 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2110 if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2111 gtk_widget_get_visible (GTK_WIDGET (item)))
2112 gtk_widget_queue_resize (GTK_WIDGET (group));
2116 * gtk_tool_item_group_get_item_position:
2117 * @group: a #GtkToolItemGroup
2118 * @item: a #GtkToolItem
2120 * Gets the position of @item in @group as index.
2122 * Returns: the index of @item in @group or -1 if @item is no child of @group
2127 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2132 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2133 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2135 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2142 * gtk_tool_item_group_get_n_items:
2143 * @group: a #GtkToolItemGroup
2145 * Gets the number of tool items in @group.
2147 * Returns: the number of tool items in @group
2152 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2154 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2156 return g_list_length (group->priv->children);
2160 * gtk_tool_item_group_get_nth_item:
2161 * @group: a #GtkToolItemGroup
2164 * Gets the tool item at @index in group.
2166 * Returns: the #GtkToolItem at index
2171 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2174 GtkToolItemGroupChild *child;
2176 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2178 child = g_list_nth_data (group->priv->children, index);
2180 return child != NULL ? child->item : NULL;
2184 * gtk_tool_item_group_get_drop_item:
2185 * @group: a #GtkToolItemGroup
2186 * @x: the x position
2187 * @y: the y position
2189 * Gets the tool item at position (x, y).
2191 * Returns: the #GtkToolItem at position (x, y)
2196 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2200 GtkAllocation *allocation;
2201 GtkOrientation orientation;
2204 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2206 allocation = >K_WIDGET (group)->allocation;
2207 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2209 g_return_val_if_fail (x >= 0 && x < allocation->width, NULL);
2210 g_return_val_if_fail (y >= 0 && y < allocation->height, NULL);
2212 for (it = group->priv->children; it != NULL; it = it->next)
2214 GtkToolItemGroupChild *child = it->data;
2215 GtkToolItem *item = child->item;
2218 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2221 allocation = >K_WIDGET (item)->allocation;
2223 x0 = x - allocation->x;
2224 y0 = y - allocation->y;
2226 if (x0 >= 0 && x0 < allocation->width &&
2227 y0 >= 0 && y0 < allocation->height)
2235 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2236 GtkRequisition *item_size,
2237 gboolean homogeneous_only,
2238 gint *requested_rows)
2240 GtkRequisition child_requisition;
2243 gboolean new_row = TRUE;
2244 GtkOrientation orientation;
2245 GtkToolbarStyle style;
2247 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2248 g_return_if_fail (NULL != item_size);
2250 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2251 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2253 item_size->width = item_size->height = 0;
2255 for (it = group->priv->children; it != NULL; it = it->next)
2257 GtkToolItemGroupChild *child = it->data;
2259 if (!gtk_tool_item_group_is_item_visible (group, child))
2262 if (child->new_row || new_row)
2268 if (!child->homogeneous && child->expand)
2271 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
2273 if (!homogeneous_only || child->homogeneous)
2274 item_size->width = MAX (item_size->width, child_requisition.width);
2275 item_size->height = MAX (item_size->height, child_requisition.height);
2279 *requested_rows = rows;
2283 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2286 GtkWidget *widget = GTK_WIDGET (group);
2287 GtkToolItemGroupPrivate* priv = group->priv;
2289 gdk_cairo_set_source_pixmap (cr, widget->window,
2290 widget->allocation.x,
2291 widget->allocation.y);
2293 if (priv->animation_timeout)
2295 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2296 cairo_pattern_t *mask;
2299 if (GTK_ORIENTATION_VERTICAL == orientation)
2300 v1 = widget->allocation.height;
2302 v1 = widget->allocation.width;
2306 if (!gtk_widget_get_visible (priv->header))
2308 else if (GTK_ORIENTATION_VERTICAL == orientation)
2309 v0 = MAX (v0, priv->header->allocation.height);
2311 v0 = MAX (v0, priv->header->allocation.width);
2313 v1 = MIN (v0 + 256, v1);
2315 if (GTK_ORIENTATION_VERTICAL == orientation)
2317 v0 += widget->allocation.y;
2318 v1 += widget->allocation.y;
2320 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2324 v0 += widget->allocation.x;
2325 v1 += widget->allocation.x;
2327 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2330 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2331 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2332 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2333 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2334 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2336 cairo_mask (cr, mask);
2337 cairo_pattern_destroy (mask);
2344 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2349 GtkRequisition requisition;
2350 GtkToolItemGroupPrivate* priv = group->priv;
2352 gtk_widget_size_request (GTK_WIDGET (group), &requisition);
2354 if (!priv->collapsed || priv->animation_timeout)
2356 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2357 GtkRequisition inquery;
2360 allocation.width = limit;
2362 allocation.height = limit;
2364 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2365 &allocation, &inquery);
2368 inquery.height -= requisition.height;
2370 inquery.width -= requisition.width;
2372 if (priv->animation_timeout && animation)
2374 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2376 timestamp = MIN (timestamp, ANIMATION_DURATION);
2378 if (priv->collapsed)
2379 timestamp = ANIMATION_DURATION - timestamp;
2383 inquery.height *= timestamp;
2384 inquery.height /= ANIMATION_DURATION;
2388 inquery.width *= timestamp;
2389 inquery.width /= ANIMATION_DURATION;
2394 requisition.height += inquery.height;
2396 requisition.width += inquery.width;
2399 return (vertical ? requisition.height : requisition.width);
2403 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2406 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2410 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2413 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2417 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2420 if (GTK_IS_TOOL_ITEM (child))
2421 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2426 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2428 gtk_container_foreach (GTK_CONTAINER (group),
2429 gtk_tool_palette_reconfigured_foreach_item,
2432 gtk_tool_item_group_header_adjust_style (group);
2436 #define __GTK_TOOL_ITEM_GROUP_C__
2437 #include "gtkaliasdef.c"