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
23 #include "gtktoolpaletteprivate.h"
29 #define P_(msgid) (msgid)
31 #define ANIMATION_TIMEOUT 50
32 #define ANIMATION_DURATION (ANIMATION_TIMEOUT * 4)
33 #define DEFAULT_ANIMATION_STATE TRUE
34 #define DEFAULT_EXPANDER_SIZE 16
35 #define DEFAULT_HEADER_SPACING 2
37 #define DEFAULT_LABEL NULL
38 #define DEFAULT_COLLAPSED FALSE
39 #define DEFAULT_ELLIPSIZE PANGO_ELLIPSIZE_NONE
42 * SECTION:GtkToolItemGroup
43 * @short_description: A sub container used in a tool palette
44 * @include: gtktoolitemgroup.h
46 * An #GtkToolItemGroup is used together with #GtkToolPalette to add #GtkToolItem<!-- -->s to a palette like container
47 * with different categories and drag and drop support.
65 CHILD_PROP_HOMOGENEOUS,
72 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
74 struct _GtkToolItemGroupPrivate
77 GtkWidget *label_widget;
82 gint64 animation_start;
83 GSource *animation_timeout;
84 GtkExpanderStyle expander_style;
87 PangoEllipsizeMode ellipsize;
92 GtkSettings *settings;
93 gulong settings_connection;
98 struct _GtkToolItemGroupChild
102 guint homogeneous : 1;
108 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
110 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
111 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
114 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
116 return gtk_bin_get_child (GTK_BIN (group->priv->header));
119 static GtkOrientation
120 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
122 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
124 if (GTK_IS_TOOL_PALETTE (parent))
125 return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
127 return GTK_ORIENTATION_VERTICAL;
130 static GtkToolbarStyle
131 gtk_tool_item_group_get_style (GtkToolShell *shell)
133 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
135 if (GTK_IS_TOOL_PALETTE (parent))
136 return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
138 return GTK_TOOLBAR_ICONS;
142 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
144 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
146 if (GTK_IS_TOOL_PALETTE (parent))
147 return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
149 return GTK_ICON_SIZE_SMALL_TOOLBAR;
152 static PangoEllipsizeMode
153 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
155 return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
159 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
161 if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
162 GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
168 static GtkOrientation
169 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
171 return GTK_ORIENTATION_HORIZONTAL;
174 static GtkSizeGroup *
175 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
177 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
179 if (GTK_IS_TOOL_PALETTE (parent))
180 return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
186 animation_change_notify (GtkToolItemGroup *group)
188 GtkSettings *settings = group->priv->settings;
192 g_object_get (settings,
193 "gtk-enable-animations", &animation,
196 animation = DEFAULT_ANIMATION_STATE;
198 group->priv->animation = animation;
202 gtk_tool_item_group_settings_change_notify (GtkSettings *settings,
203 const GParamSpec *pspec,
204 GtkToolItemGroup *group)
206 if (! strcmp (pspec->name, "gtk-enable-animations"))
208 animation_change_notify (group);
213 gtk_tool_item_group_screen_changed (GtkWidget *widget,
214 GdkScreen *previous_screen)
216 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
217 GtkToolItemGroupPrivate* priv = group->priv;
218 GtkSettings *old_settings = priv->settings;
219 GtkSettings *settings;
221 if (gtk_widget_has_screen (GTK_WIDGET (group)))
222 settings = gtk_widget_get_settings (GTK_WIDGET (group));
226 if (settings == old_settings)
231 g_signal_handler_disconnect (old_settings, priv->settings_connection);
232 g_object_unref (old_settings);
237 priv->settings_connection =
238 g_signal_connect (settings, "notify",
239 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
241 priv->settings = g_object_ref (settings);
244 priv->settings = NULL;
246 animation_change_notify (group);
250 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
252 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
253 iface->get_orientation = gtk_tool_item_group_get_orientation;
254 iface->get_style = gtk_tool_item_group_get_style;
255 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
256 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
257 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
258 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
262 gtk_tool_item_group_header_expose_event_cb (GtkWidget *widget,
263 GdkEventExpose *event,
266 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
267 GtkToolItemGroupPrivate* priv = group->priv;
268 GtkExpanderStyle expander_style;
269 GtkOrientation orientation;
271 GtkTextDirection direction;
273 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
274 expander_style = priv->expander_style;
275 direction = gtk_widget_get_direction (widget);
277 if (GTK_ORIENTATION_VERTICAL == orientation)
279 if (GTK_TEXT_DIR_RTL == direction)
280 x = widget->allocation.x + widget->allocation.width - priv->expander_size / 2;
282 x = widget->allocation.x + priv->expander_size / 2;
283 y = widget->allocation.y + widget->allocation.height / 2;
287 x = widget->allocation.x + widget->allocation.width / 2;
288 y = widget->allocation.y + priv->expander_size / 2;
290 /* Unfortunatly gtk_paint_expander() doesn't support rotated drawing
291 * modes. Luckily the following shady arithmetics produce the desired
293 expander_style = GTK_EXPANDER_EXPANDED - expander_style;
296 gtk_paint_expander (widget->style, widget->window,
298 &event->area, GTK_WIDGET (group),
299 "tool-palette-header", x, y,
306 gtk_tool_item_group_header_size_request_cb (GtkWidget *widget,
307 GtkRequisition *requisition,
310 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
311 requisition->height = MAX (requisition->height, group->priv->expander_size);
315 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
318 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
319 GtkToolItemGroupPrivate* priv = group->priv;
320 GtkWidget *parent = gtk_widget_get_parent (data);
322 if (priv->collapsed ||
323 !GTK_IS_TOOL_PALETTE (parent) ||
324 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
325 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
329 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
331 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
332 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
333 GtkWidget *widget = GTK_WIDGET (group);
334 GtkToolItemGroupPrivate* priv = group->priv;
336 GtkTextDirection direction = gtk_widget_get_direction (widget);
338 gtk_widget_style_get (widget,
339 "header-spacing", &(priv->header_spacing),
340 "expander-size", &(priv->expander_size),
343 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
345 case GTK_ORIENTATION_HORIZONTAL:
346 dy = priv->header_spacing + priv->expander_size;
348 if (GTK_IS_LABEL (label_widget))
350 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
351 if (GTK_TEXT_DIR_RTL == direction)
352 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
354 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
358 case GTK_ORIENTATION_VERTICAL:
359 dx = priv->header_spacing + priv->expander_size;
361 if (GTK_IS_LABEL (label_widget))
363 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
364 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
369 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
373 gtk_tool_item_group_init (GtkToolItemGroup *group)
375 GtkWidget *alignment;
376 GtkToolItemGroupPrivate* priv;
378 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
380 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
381 GTK_TYPE_TOOL_ITEM_GROUP,
382 GtkToolItemGroupPrivate);
384 priv->children = NULL;
385 priv->header_spacing = DEFAULT_HEADER_SPACING;
386 priv->expander_size = DEFAULT_EXPANDER_SIZE;
387 priv->expander_style = GTK_EXPANDER_EXPANDED;
389 priv->label_widget = gtk_label_new (NULL);
390 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
391 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
392 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
393 gtk_widget_show_all (alignment);
395 gtk_widget_push_composite_child ();
396 priv->header = gtk_button_new ();
397 gtk_widget_set_composite_name (priv->header, "header");
398 gtk_widget_pop_composite_child ();
400 g_object_ref_sink (priv->header);
401 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
402 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
403 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
405 gtk_tool_item_group_header_adjust_style (group);
407 g_signal_connect_after (alignment, "expose-event",
408 G_CALLBACK (gtk_tool_item_group_header_expose_event_cb),
410 g_signal_connect_after (alignment, "size-request",
411 G_CALLBACK (gtk_tool_item_group_header_size_request_cb),
414 g_signal_connect (priv->header, "clicked",
415 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
420 gtk_tool_item_group_set_property (GObject *object,
425 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
430 gtk_tool_item_group_set_label (group, g_value_get_string (value));
433 case PROP_LABEL_WIDGET:
434 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
437 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
441 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
444 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
447 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
453 gtk_tool_item_group_get_property (GObject *object,
458 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
463 g_value_set_string (value, gtk_tool_item_group_get_label (group));
466 case PROP_LABEL_WIDGET:
467 g_value_set_object (value,
468 gtk_tool_item_group_get_label_widget (group));
472 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
476 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
480 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
484 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
490 gtk_tool_item_group_finalize (GObject *object)
492 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
494 if (group->priv->children)
496 g_list_free (group->priv->children);
497 group->priv->children = NULL;
500 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
504 gtk_tool_item_group_dispose (GObject *object)
506 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
507 GtkToolItemGroupPrivate* priv = group->priv;
511 /* disconnect focus tracking handler */
512 g_signal_handler_disconnect (priv->toplevel,
515 priv->focus_set_id = 0;
516 priv->toplevel = NULL;
519 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
523 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
524 GtkRequisition *item_size,
525 gboolean homogeneous_only,
526 gint *requested_rows)
528 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
530 if (GTK_IS_TOOL_PALETTE (parent))
531 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
533 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
537 gtk_tool_item_group_size_request (GtkWidget *widget,
538 GtkRequisition *requisition)
540 const gint border_width = GTK_CONTAINER (widget)->border_width;
541 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
542 GtkToolItemGroupPrivate* priv = group->priv;
543 GtkOrientation orientation;
544 GtkRequisition item_size;
547 if (priv->children && gtk_tool_item_group_get_label_widget (group))
549 gtk_widget_size_request (priv->header, requisition);
550 gtk_widget_show (priv->header);
554 requisition->width = requisition->height = 0;
555 gtk_widget_hide (priv->header);
558 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
560 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
562 if (GTK_ORIENTATION_VERTICAL == orientation)
563 requisition->width = MAX (requisition->width, item_size.width);
565 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
567 requisition->width += border_width * 2;
568 requisition->height += border_width * 2;
572 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
573 GtkToolItemGroupChild *child)
575 GtkToolbarStyle style;
576 GtkOrientation orientation;
578 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
579 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
581 /* horizontal tool palettes with text style support only homogeneous items */
582 if (!child->homogeneous &&
583 GTK_ORIENTATION_HORIZONTAL == orientation &&
584 GTK_TOOLBAR_TEXT == style)
588 (GTK_WIDGET_VISIBLE (child->item)) &&
589 (GTK_ORIENTATION_VERTICAL == orientation ?
590 gtk_tool_item_get_visible_vertical (child->item) :
591 gtk_tool_item_get_visible_horizontal (child->item));
594 static inline unsigned
598 return (x + y - 1) / y;
602 gtk_tool_item_group_real_size_query (GtkWidget *widget,
603 GtkAllocation *allocation,
604 GtkRequisition *inquery)
606 const gint border_width = GTK_CONTAINER (widget)->border_width;
607 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
608 GtkToolItemGroupPrivate* priv = group->priv;
610 GtkRequisition item_size;
611 GtkAllocation item_area;
613 GtkOrientation orientation;
614 GtkToolbarStyle style;
618 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
619 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
621 /* figure out the size of homogeneous items */
622 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
624 if (GTK_ORIENTATION_VERTICAL == orientation)
625 item_size.width = MIN (item_size.width, allocation->width);
627 item_size.height = MIN (item_size.height, allocation->height);
630 item_area.height = 0;
632 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
633 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
639 if (GTK_ORIENTATION_VERTICAL == orientation)
641 gboolean new_row = FALSE;
645 item_area.width = allocation->width - 2 * border_width;
646 n_columns = MAX (item_area.width / item_size.width, 1);
648 /* calculate required rows for n_columns columns */
649 for (it = priv->children; it != NULL; it = it->next)
651 GtkToolItemGroupChild *child = it->data;
653 if (!gtk_tool_item_group_is_item_visible (group, child))
656 if (new_row || child->new_row)
666 if (child->homogeneous)
669 if (col >= n_columns)
674 GtkRequisition req = {0, 0};
677 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
679 width = udiv (req.width, item_size.width);
687 if (col >= n_columns)
695 guint *row_min_width;
697 gboolean new_row = TRUE;
698 guint col = 0, min_col, max_col = 0, all_items = 0;
701 item_area.height = allocation->height - 2 * border_width;
702 n_rows = MAX (item_area.height / item_size.height, min_rows);
704 row_min_width = g_new0 (guint, n_rows);
706 /* calculate minimal and maximal required cols and minimal required rows */
707 for (it = priv->children; it != NULL; it = it->next)
709 GtkToolItemGroupChild *child = it->data;
711 if (!gtk_tool_item_group_is_item_visible (group, child))
714 if (new_row || child->new_row)
719 row_min_width[row] = 1;
725 if (child->homogeneous)
732 GtkRequisition req = {0, 0};
735 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
737 width = udiv (req.width, item_size.width);
742 row_min_width[row] = MAX (row_min_width[row], width);
745 max_col = MAX (max_col, col);
748 /* calculate minimal required cols */
749 min_col = udiv (all_items, n_rows);
751 for (i = 0; i <= row; i++)
753 min_col = MAX (min_col, row_min_width[i]);
756 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
757 for (n_columns = min_col; n_columns < max_col; n_columns ++)
761 /* calculate required rows for n_columns columns */
762 for (it = priv->children; it != NULL; it = it->next)
764 GtkToolItemGroupChild *child = it->data;
766 if (!gtk_tool_item_group_is_item_visible (group, child))
769 if (new_row || child->new_row)
779 if (child->homogeneous)
782 if (col >= n_columns)
787 GtkRequisition req = {0, 0};
790 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
792 width = udiv (req.width, item_size.width);
800 if (col >= n_columns)
810 item_area.width = item_size.width * n_columns;
811 item_area.height = item_size.height * n_rows;
817 /* figure out header widget size */
818 if (GTK_WIDGET_VISIBLE (priv->header))
820 GtkRequisition child_requisition;
822 gtk_widget_size_request (priv->header, &child_requisition);
824 if (GTK_ORIENTATION_VERTICAL == orientation)
825 inquery->height += child_requisition.height;
827 inquery->width += child_requisition.width;
830 /* report effective widget size */
831 inquery->width += item_area.width + 2 * border_width;
832 inquery->height += item_area.height + 2 * border_width;
836 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
837 GtkAllocation *allocation)
839 const gint border_width = GTK_CONTAINER (widget)->border_width;
840 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
841 GtkToolItemGroupPrivate* priv = group->priv;
842 GtkRequisition child_requisition;
843 GtkAllocation child_allocation;
845 GtkRequisition item_size;
846 GtkAllocation item_area;
848 GtkOrientation orientation;
849 GtkToolbarStyle style;
853 gint n_columns, n_rows = 1;
856 GtkTextDirection direction = gtk_widget_get_direction (widget);
858 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
859 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
862 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
864 child_allocation.x = border_width;
865 child_allocation.y = border_width;
867 /* place the header widget */
868 if (GTK_WIDGET_VISIBLE (priv->header))
870 gtk_widget_size_request (priv->header, &child_requisition);
872 if (GTK_ORIENTATION_VERTICAL == orientation)
874 child_allocation.width = allocation->width;
875 child_allocation.height = child_requisition.height;
879 child_allocation.width = child_requisition.width;
880 child_allocation.height = allocation->height;
882 if (GTK_TEXT_DIR_RTL == direction)
883 child_allocation.x = allocation->width - border_width - child_allocation.width;
886 gtk_widget_size_allocate (priv->header, &child_allocation);
888 if (GTK_ORIENTATION_VERTICAL == orientation)
889 child_allocation.y += child_allocation.height;
890 else if (GTK_TEXT_DIR_RTL != direction)
891 child_allocation.x += child_allocation.width;
893 child_allocation.x = border_width;
896 child_requisition.width = child_requisition.height = 0;
898 /* figure out the size of homogeneous items */
899 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
901 /* figure out the available columns and size of item_area */
902 if (GTK_ORIENTATION_VERTICAL == orientation)
904 item_size.width = MIN (item_size.width, allocation->width);
906 item_area.width = allocation->width - 2 * border_width;
907 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
909 n_columns = MAX (item_area.width / item_size.width, 1);
911 item_size.width = item_area.width / n_columns;
915 item_size.height = MIN (item_size.height, allocation->height);
917 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
918 item_area.height = allocation->height - 2 * border_width;
920 n_columns = MAX (item_area.width / item_size.width, 1);
921 n_rows = MAX (item_area.height / item_size.height, min_rows);
923 item_size.height = item_area.height / n_rows;
926 item_area.x = child_allocation.x;
927 item_area.y = child_allocation.y;
929 /* when expanded or in transition, place the tool items in a grid like layout */
930 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
932 gint col = 0, row = 0;
934 for (it = priv->children; it != NULL; it = it->next)
936 GtkToolItemGroupChild *child = it->data;
939 if (!gtk_tool_item_group_is_item_visible (group, child))
941 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
946 /* for non homogeneous widgets request the required size */
947 child_requisition.width = 0;
949 if (!child->homogeneous)
951 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
952 child_requisition.width = MIN (child_requisition.width, item_area.width);
955 /* select next row if at end of row */
956 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
960 child_allocation.y += child_allocation.height;
965 /* calculate the position and size of the item */
966 if (!child->homogeneous)
972 col_width = udiv (child_requisition.width, item_size.width);
974 col_width = n_columns - col;
976 width = col_width * item_size.width;
978 if (GTK_TEXT_DIR_RTL == direction)
979 col_child = (n_columns - col - col_width);
983 child_allocation.x = item_area.x + col_child * item_size.width;
984 child_allocation.width = width;
989 (item_area.x + col_child * item_size.width +
990 (width - child_requisition.width) / 2);
991 child_allocation.width = child_requisition.width;
998 if (GTK_TEXT_DIR_RTL == direction)
999 col_child = (n_columns - col - 1);
1001 child_allocation.x = item_area.x + col_child * item_size.width;
1002 child_allocation.width = item_size.width;
1007 child_allocation.height = item_size.height;
1009 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1010 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1013 child_allocation.y += item_size.height;
1016 /* or just hide all items, when collapsed */
1020 for (it = priv->children; it != NULL; it = it->next)
1022 GtkToolItemGroupChild *child = it->data;
1024 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1030 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1031 GtkAllocation *allocation)
1033 gtk_tool_item_group_real_size_allocate (widget, allocation);
1035 if (GTK_WIDGET_MAPPED (widget))
1036 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
1040 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1044 GtkAdjustment *adjustment;
1047 /* Find this group's parent widget in the focused widget's anchestry. */
1048 for (p = widget; p; p = gtk_widget_get_parent (p))
1051 p = gtk_widget_get_parent (p);
1055 if (GTK_IS_TOOL_PALETTE (p))
1057 /* Check that the focused widgets is fully visible within
1058 * the group's parent widget and make it visible otherwise. */
1060 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1061 adjustment = gtk_tool_palette_get_vadjustment (GTK_TOOL_PALETTE (p));
1067 /* Handle vertical adjustment. */
1068 if (gtk_widget_translate_coordinates
1069 (widget, p, 0, 0, NULL, &y) && y < 0)
1071 y += adjustment->value;
1072 gtk_adjustment_clamp_page (adjustment, y, y + widget->allocation.height);
1074 else if (gtk_widget_translate_coordinates
1075 (widget, p, 0, widget->allocation.height, NULL, &y) &&
1076 y > p->allocation.height)
1078 y += adjustment->value;
1079 gtk_adjustment_clamp_page (adjustment, y - widget->allocation.height, y);
1083 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1089 /* Handle horizontal adjustment. */
1090 if (gtk_widget_translate_coordinates
1091 (widget, p, 0, 0, &x, NULL) && x < 0)
1093 x += adjustment->value;
1094 gtk_adjustment_clamp_page (adjustment, x, x + widget->allocation.width);
1096 else if (gtk_widget_translate_coordinates
1097 (widget, p, widget->allocation.width, 0, &x, NULL) &&
1098 x > p->allocation.width)
1100 x += adjustment->value;
1101 gtk_adjustment_clamp_page (adjustment, x - widget->allocation.width, x);
1110 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1111 GtkWidget *toplevel)
1113 GtkToolItemGroupPrivate* priv = group->priv;
1115 if (toplevel != priv->toplevel)
1119 /* Disconnect focus tracking handler. */
1120 g_signal_handler_disconnect (priv->toplevel,
1121 priv->focus_set_id);
1123 priv->focus_set_id = 0;
1124 priv->toplevel = NULL;
1129 /* Install focus tracking handler. We connect to the window's
1130 * set-focus signal instead of connecting to the focus signal of
1133 * 1) Reduce the number of signal handlers used.
1134 * 2) Avoid special handling for group headers.
1135 * 3) Catch focus grabs not only for direct children,
1136 * but also for nested widgets.
1138 priv->focus_set_id =
1139 g_signal_connect (toplevel, "set-focus",
1140 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1143 priv->toplevel = toplevel;
1149 gtk_tool_item_group_realize (GtkWidget *widget)
1151 GtkWidget *toplevel_window;
1152 const gint border_width = GTK_CONTAINER (widget)->border_width;
1153 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1154 GdkWindowAttr attributes;
1155 GdkDisplay *display;
1157 attributes.window_type = GDK_WINDOW_CHILD;
1158 attributes.x = widget->allocation.x + border_width;
1159 attributes.y = widget->allocation.y + border_width;
1160 attributes.width = widget->allocation.width - border_width * 2;
1161 attributes.height = widget->allocation.height - border_width * 2;
1162 attributes.wclass = GDK_INPUT_OUTPUT;
1163 attributes.visual = gtk_widget_get_visual (widget);
1164 attributes.colormap = gtk_widget_get_colormap (widget);
1165 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1166 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1167 | GDK_BUTTON_MOTION_MASK;
1169 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1170 &attributes, attributes_mask);
1172 display = gdk_drawable_get_display (widget->window);
1174 if (gdk_display_supports_composite (display))
1175 gdk_window_set_composited (widget->window, TRUE);
1177 gdk_window_set_user_data (widget->window, widget);
1178 widget->style = gtk_style_attach (widget->style, widget->window);
1179 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1180 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1182 gtk_container_forall (GTK_CONTAINER (widget),
1183 (GtkCallback) gtk_widget_set_parent_window,
1186 gtk_widget_queue_resize_no_redraw (widget);
1188 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1189 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1194 gtk_tool_item_group_unrealize (GtkWidget *widget)
1196 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1197 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1201 gtk_tool_item_group_style_set (GtkWidget *widget,
1202 GtkStyle *previous_style)
1204 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1205 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1209 gtk_tool_item_group_add (GtkContainer *container,
1212 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1213 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1215 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1216 GTK_TOOL_ITEM (widget), -1);
1220 gtk_tool_item_group_remove (GtkContainer *container,
1223 GtkToolItemGroup *group;
1224 GtkToolItemGroupPrivate* priv;
1227 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1228 group = GTK_TOOL_ITEM_GROUP (container);
1231 for (it = priv->children; it != NULL; it = it->next)
1233 GtkToolItemGroupChild *child_info = it->data;
1235 if ((GtkWidget *)child_info->item == child)
1237 g_object_unref (child);
1238 gtk_widget_unparent (child);
1240 g_free (child_info);
1241 priv->children = g_list_delete_link (priv->children, it);
1243 gtk_widget_queue_resize (GTK_WIDGET (container));
1250 gtk_tool_item_group_forall (GtkContainer *container,
1252 GtkCallback callback,
1253 gpointer callback_data)
1255 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1256 GtkToolItemGroupPrivate* priv = group->priv;
1259 if (internals && priv->header)
1260 callback (priv->header, callback_data);
1262 children = priv->children;
1265 GtkToolItemGroupChild *child = children->data;
1266 children = children->next; /* store pointer before call to callback
1267 because the child pointer is invalid if the
1268 child->item is removed from the item group
1271 callback (GTK_WIDGET (child->item), callback_data);
1276 gtk_tool_item_group_child_type (GtkContainer *container)
1278 return GTK_TYPE_TOOL_ITEM;
1281 static GtkToolItemGroupChild *
1282 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1290 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1291 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1293 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1295 GtkToolItemGroupChild *child = it->data;
1297 if (child->item == item)
1313 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1315 gboolean *homogeneous,
1320 GtkToolItemGroupChild *child;
1322 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1323 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1325 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1330 *expand = child->expand;
1333 *homogeneous = child->homogeneous;
1336 *fill = child->fill;
1339 *new_row = child->new_row;
1343 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1345 gboolean homogeneous,
1350 GtkToolItemGroupChild *child;
1351 gboolean changed = FALSE;
1353 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1354 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1356 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1360 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1362 if (child->homogeneous != homogeneous)
1364 child->homogeneous = homogeneous;
1366 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1368 if (child->expand != expand)
1370 child->expand = expand;
1372 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1374 if (child->fill != fill)
1378 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1380 if (child->new_row != new_row)
1382 child->new_row = new_row;
1384 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1387 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1389 if (changed && GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
1390 gtk_widget_queue_resize (GTK_WIDGET (group));
1394 gtk_tool_item_group_set_child_property (GtkContainer *container,
1397 const GValue *value,
1400 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1401 GtkToolItem *item = GTK_TOOL_ITEM (child);
1402 gboolean homogeneous, expand, fill, new_row;
1404 if (prop_id != CHILD_PROP_POSITION)
1405 gtk_tool_item_group_get_item_packing (group, item,
1413 case CHILD_PROP_HOMOGENEOUS:
1414 gtk_tool_item_group_set_item_packing (group, item,
1415 g_value_get_boolean (value),
1421 case CHILD_PROP_EXPAND:
1422 gtk_tool_item_group_set_item_packing (group, item,
1424 g_value_get_boolean (value),
1429 case CHILD_PROP_FILL:
1430 gtk_tool_item_group_set_item_packing (group, item,
1433 g_value_get_boolean (value),
1437 case CHILD_PROP_NEW_ROW:
1438 gtk_tool_item_group_set_item_packing (group, item,
1442 g_value_get_boolean (value));
1445 case CHILD_PROP_POSITION:
1446 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1450 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1456 gtk_tool_item_group_get_child_property (GtkContainer *container,
1462 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1463 GtkToolItem *item = GTK_TOOL_ITEM (child);
1464 gboolean homogeneous, expand, fill, new_row;
1466 if (prop_id != CHILD_PROP_POSITION)
1467 gtk_tool_item_group_get_item_packing (group, item,
1475 case CHILD_PROP_HOMOGENEOUS:
1476 g_value_set_boolean (value, homogeneous);
1479 case CHILD_PROP_EXPAND:
1480 g_value_set_boolean (value, expand);
1483 case CHILD_PROP_FILL:
1484 g_value_set_boolean (value, fill);
1487 case CHILD_PROP_NEW_ROW:
1488 g_value_set_boolean (value, new_row);
1491 case CHILD_PROP_POSITION:
1492 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1496 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1502 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1504 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1505 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1506 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1508 oclass->set_property = gtk_tool_item_group_set_property;
1509 oclass->get_property = gtk_tool_item_group_get_property;
1510 oclass->finalize = gtk_tool_item_group_finalize;
1511 oclass->dispose = gtk_tool_item_group_dispose;
1513 wclass->size_request = gtk_tool_item_group_size_request;
1514 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1515 wclass->realize = gtk_tool_item_group_realize;
1516 wclass->unrealize = gtk_tool_item_group_unrealize;
1517 wclass->style_set = gtk_tool_item_group_style_set;
1518 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1520 cclass->add = gtk_tool_item_group_add;
1521 cclass->remove = gtk_tool_item_group_remove;
1522 cclass->forall = gtk_tool_item_group_forall;
1523 cclass->child_type = gtk_tool_item_group_child_type;
1524 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1525 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1527 g_object_class_install_property (oclass, PROP_LABEL,
1528 g_param_spec_string ("label",
1530 P_("The human-readable title of this item group"),
1532 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1533 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1535 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1536 g_param_spec_object ("label-widget",
1538 P_("A widget to display in place of the usual label"),
1540 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1541 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1543 g_object_class_install_property (oclass, PROP_COLLAPSED,
1544 g_param_spec_boolean ("collapsed",
1546 P_("Wether the group has been collapsed and items are hidden"),
1548 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1549 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1551 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1552 g_param_spec_enum ("ellipsize",
1554 P_("Ellipsize for item group headers"),
1555 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1556 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1557 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1559 g_object_class_install_property (oclass, PROP_RELIEF,
1560 g_param_spec_enum ("header-relief",
1561 P_("Header Relief"),
1562 P_("Relief of the group header button"),
1563 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1564 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1565 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1567 gtk_widget_class_install_style_property (wclass,
1568 g_param_spec_int ("expander-size",
1569 P_("Expander Size"),
1570 P_("Size of the expander arrow"),
1573 DEFAULT_EXPANDER_SIZE,
1574 G_PARAM_READABLE | G_PARAM_STATIC_NAME |
1575 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1577 gtk_widget_class_install_style_property (wclass,
1578 g_param_spec_int ("header-spacing",
1579 P_("Header Spacing"),
1580 P_("Spacing between expander arrow and caption"),
1583 DEFAULT_HEADER_SPACING,
1584 G_PARAM_READABLE | G_PARAM_STATIC_NAME |
1585 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1587 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1588 g_param_spec_boolean ("homogeneous",
1590 P_("Whether the item should be the same size as other homogeneous items"),
1592 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1593 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1595 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1596 g_param_spec_boolean ("expand",
1598 P_("Whether the item should receive extra space when the toolbar grows"),
1600 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1601 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1603 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1604 g_param_spec_boolean ("fill",
1606 P_("Whether the item should fill the avaiable space"),
1608 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1609 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1611 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1612 g_param_spec_boolean ("new-row",
1614 P_("Whether the item should start a new row"),
1616 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1617 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1619 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1620 g_param_spec_int ("position",
1622 P_("Position of the item within this group"),
1626 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1627 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1629 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1633 * gtk_tool_item_group_new:
1634 * @label: the label of the new group.
1636 * Creates a new tool item group with label @label.
1638 * Returns: a new #GtkToolItemGroup.
1643 gtk_tool_item_group_new (const gchar *label)
1645 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label",
1650 * gtk_tool_item_group_set_label:
1651 * @group: an #GtkToolItemGroup.
1652 * @label: the new human-readable label of of the group.
1654 * Sets the label of the tool item group. The label is displayed in the header
1660 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1663 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1667 gtk_tool_item_group_set_label_widget (group, NULL);
1671 GtkWidget *child = gtk_label_new (label);
1672 gtk_widget_show (child);
1674 gtk_tool_item_group_set_label_widget (group, child);
1677 g_object_notify (G_OBJECT (group), "label");
1681 * gtk_tool_item_group_set_label_widget:
1682 * @group: an #GtkToolItemGroup.
1683 * @widget: the new human-readable label of of the group.
1685 * Sets the label of the tool item group. The label is displayed in the header
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);
1717 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1720 priv->label_widget = label_widget;
1722 if (GTK_WIDGET_VISIBLE (group))
1723 gtk_widget_queue_resize (GTK_WIDGET (group));
1725 /* Only show the header widget if the group has children: */
1726 if (label_widget && priv->children)
1727 gtk_widget_show (priv->header);
1729 gtk_widget_hide (priv->header);
1731 g_object_freeze_notify (G_OBJECT (group));
1732 g_object_notify (G_OBJECT (group), "label-widget");
1733 g_object_notify (G_OBJECT (group), "label");
1734 g_object_thaw_notify (G_OBJECT (group));
1738 * gtk_tool_item_group_header_relief:
1739 * @group: an #GtkToolItemGroup.
1740 * @style: The GtkReliefStyle
1742 * Set the button relief of the group header. See #gtk_button_set_relief for
1748 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1749 GtkReliefStyle style)
1751 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1752 gtk_button_set_relief (GTK_BUTTON(group->priv->header), style);
1756 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;
1768 if (GTK_WIDGET_REALIZED (priv->header))
1770 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1773 /* Find the header button's arrow area... */
1774 area.x = alignment->allocation.x;
1775 area.y = alignment->allocation.y + (alignment->allocation.height - priv->expander_size) / 2;
1776 area.height = priv->expander_size;
1777 area.width = priv->expander_size;
1779 /* ... and invalidated it to get it animated. */
1780 gdk_window_invalidate_rect (priv->header->window, &area, TRUE);
1783 if (GTK_WIDGET_REALIZED (group))
1785 GtkWidget *widget = GTK_WIDGET (group);
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_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 /* Ensure that all composited windows and child windows are repainted, before
1842 * the parent widget gets its expose-event. This is needed to avoid heavy
1843 * rendering artifacts. GTK+ should take care about this issue by itself I
1844 * guess, but currently it doesn't. Also I don't understand the parameters
1845 * of this issue well enough yet, to file a bug report.
1847 gdk_window_process_updates (GTK_WIDGET (group)->window, TRUE);
1849 retval = (priv->animation_timeout != NULL);
1851 GDK_THREADS_LEAVE();
1857 * gtk_tool_item_group_set_collapsed:
1858 * @group: an #GtkToolItemGroup.
1859 * @collapsed: whether the @group should be collapsed or expanded.
1861 * Sets whether the @group should be collapsed or expanded.
1866 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1870 GtkToolItemGroupPrivate* priv;
1872 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1876 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1877 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1878 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1879 GTK_WIDGET (group));
1880 if (collapsed != priv->collapsed)
1882 if (priv->animation)
1886 g_get_current_time (&now);
1888 if (priv->animation_timeout)
1889 g_source_destroy (priv->animation_timeout);
1891 priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1892 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1894 g_source_set_callback (priv->animation_timeout,
1895 gtk_tool_item_group_animation_cb,
1898 g_source_attach (priv->animation_timeout, NULL);
1902 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1903 gtk_tool_item_group_force_expose (group);
1906 priv->collapsed = collapsed;
1907 g_object_notify (G_OBJECT (group), "collapsed");
1912 * gtk_tool_item_group_set_ellipsize:
1913 * @group: an #GtkToolItemGroup.
1914 * @ellipsize: the #PangoEllipsizeMode labels in @group should use.
1916 * Sets the ellipsization mode which should be used by labels in @group.
1921 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1922 PangoEllipsizeMode ellipsize)
1924 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1926 if (ellipsize != group->priv->ellipsize)
1928 group->priv->ellipsize = ellipsize;
1929 gtk_tool_item_group_header_adjust_style (group);
1930 g_object_notify (G_OBJECT (group), "ellipsize");
1931 _gtk_tool_item_group_palette_reconfigured (group);
1936 * gtk_tool_item_group_get_label:
1937 * @group: an #GtkToolItemGroup.
1939 * Gets the label of @group.
1941 * Returns: the label of @group. The label is an internal string of @group and must not be modified.
1942 * Note that NULL is returned if a custom label has been set with gtk_tool_item_group_set_label_widget()
1946 G_CONST_RETURN gchar*
1947 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1949 GtkToolItemGroupPrivate *priv;
1951 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1955 if (GTK_IS_LABEL (priv->label_widget))
1956 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1962 * gtk_tool_item_group_get_label_widget:
1963 * @group: an #GtkToolItemGroup.
1965 * Gets the label widget of @group.
1967 * Returns: the label widget of @group. See gtk_tool_item_group_set_label_widget()
1972 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
1974 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1975 return gtk_bin_get_child (GTK_BIN (alignment));
1979 * gtk_tool_item_group_get_collapsed:
1980 * @group: an GtkToolItemGroup.
1982 * Gets whether @group is collapsed or expanded.
1984 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded.
1989 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
1991 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
1992 return group->priv->collapsed;
1996 * gtk_tool_item_group_get_ellipsize:
1997 * @group: an #GtkToolItemGroup.
1999 * Gets the ellipsization mode of @group.
2001 * Returns: the #PangoEllipsizeMode of @group.
2006 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2008 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2009 return group->priv->ellipsize;
2013 * gtk_tool_item_group_get_header_relief:
2014 * @group: an #GtkToolItemGroup.
2016 * Gets the relief mode of the header button of @group.
2018 * Returns: the #GtkReliefStyle
2023 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2025 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2026 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2030 * gtk_tool_item_group_insert:
2031 * @group: an #GtkToolItemGroup.
2032 * @item: the #GtkToolItem to insert into @group.
2033 * @position: the position of @item in @group, starting with 0. The position -1 means end of list.
2035 * Inserts @item at @position in the list of children of @group.
2040 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2044 GtkWidget *parent, *child_widget;
2045 GtkToolItemGroupChild *child;
2047 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2048 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2049 g_return_if_fail (position >= -1);
2051 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2053 child = g_new (GtkToolItemGroupChild, 1);
2054 child->item = g_object_ref_sink (item);
2055 child->homogeneous = TRUE;
2056 child->expand = FALSE;
2058 child->new_row = FALSE;
2060 group->priv->children = g_list_insert (group->priv->children, child, position);
2062 if (GTK_IS_TOOL_PALETTE (parent))
2063 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2065 child_widget = gtk_bin_get_child (GTK_BIN (item));
2067 if (GTK_IS_BUTTON (child_widget))
2068 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2070 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2074 * gtk_tool_item_group_set_item_position:
2075 * @group: an #GtkToolItemGroup.
2076 * @item: the #GtkToolItem to move to a new position, should be a child of @group.
2077 * @position: the new position of @item in @group, starting with 0. The position -1 means end of list.
2079 * Sets the position of @item in the list of children of @group.
2084 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2090 GtkToolItemGroupChild *child;
2091 GtkToolItemGroupPrivate* priv;
2093 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2094 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_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
2111 gtk_widget_queue_resize (GTK_WIDGET (group));
2115 * gtk_tool_item_group_get_item_position:
2116 * @group: an #GtkToolItemGroup.
2117 * @item: a #GtkToolItem.
2119 * Gets the position of @item in @group as index.
2121 * Returns: the index of @item in @group or -1 if @item is no child of @group.
2126 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2131 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2132 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2134 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2141 * gtk_tool_item_group_get_n_items:
2142 * @group: an #GtkToolItemGroup.
2144 * Gets the number of tool items in group.
2146 * Returns: the number of tool items in group.
2151 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2153 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2155 return g_list_length (group->priv->children);
2159 * gtk_tool_item_group_get_nth_item:
2160 * @group: an #GtkToolItemGroup.
2161 * @index: the index.
2163 * Gets the tool item at index in group.
2165 * Returns: the #GtkToolItem at index.
2170 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2173 GtkToolItemGroupChild *child;
2175 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2177 child = g_list_nth_data (group->priv->children, index);
2179 return child != NULL ? child->item : NULL;
2183 * gtk_tool_item_group_get_drop_item:
2184 * @group: an #GtkToolItemGroup.
2185 * @x: the x position.
2186 * @y: the y position.
2188 * Gets the tool item at position (x, y).
2190 * Returns: the #GtkToolItem at position (x, y).
2195 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2199 GtkAllocation *allocation;
2200 GtkOrientation orientation;
2203 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2205 allocation = >K_WIDGET (group)->allocation;
2206 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2208 g_return_val_if_fail (x >= 0 && x < allocation->width, NULL);
2209 g_return_val_if_fail (y >= 0 && y < allocation->height, NULL);
2211 for (it = group->priv->children; it != NULL; it = it->next)
2213 GtkToolItemGroupChild *child = it->data;
2214 GtkToolItem *item = child->item;
2217 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2220 allocation = >K_WIDGET (item)->allocation;
2222 x0 = x - allocation->x;
2223 y0 = y - allocation->y;
2225 if (x0 >= 0 && x0 < allocation->width &&
2226 y0 >= 0 && y0 < allocation->height)
2234 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2235 GtkRequisition *item_size,
2236 gboolean homogeneous_only,
2237 gint *requested_rows)
2239 GtkRequisition child_requisition;
2242 gboolean new_row = TRUE;
2243 GtkOrientation orientation;
2244 GtkToolbarStyle style;
2246 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2247 g_return_if_fail (NULL != item_size);
2249 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2250 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2252 item_size->width = item_size->height = 0;
2254 for (it = group->priv->children; it != NULL; it = it->next)
2256 GtkToolItemGroupChild *child = it->data;
2258 if (!gtk_tool_item_group_is_item_visible (group, child))
2261 if (child->new_row || new_row)
2267 if (!child->homogeneous && child->expand)
2270 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
2272 if (!homogeneous_only || child->homogeneous)
2273 item_size->width = MAX (item_size->width, child_requisition.width);
2274 item_size->height = MAX (item_size->height, child_requisition.height);
2278 *requested_rows = rows;
2282 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2285 GtkWidget *widget = GTK_WIDGET (group);
2286 GtkToolItemGroupPrivate* priv = group->priv;
2288 gdk_cairo_set_source_pixmap (cr, widget->window,
2289 widget->allocation.x,
2290 widget->allocation.y);
2292 if (priv->animation_timeout)
2294 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2295 cairo_pattern_t *mask;
2298 if (GTK_ORIENTATION_VERTICAL == orientation)
2299 v1 = widget->allocation.height;
2301 v1 = widget->allocation.width;
2305 if (!GTK_WIDGET_VISIBLE (priv->header))
2307 else if (GTK_ORIENTATION_VERTICAL == orientation)
2308 v0 = MAX (v0, priv->header->allocation.height);
2310 v0 = MAX (v0, priv->header->allocation.width);
2312 v1 = MIN (v0 + 256, v1);
2314 if (GTK_ORIENTATION_VERTICAL == orientation)
2316 v0 += widget->allocation.y;
2317 v1 += widget->allocation.y;
2319 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2323 v0 += widget->allocation.x;
2324 v1 += widget->allocation.x;
2326 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2329 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2330 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2331 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2332 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2333 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2335 cairo_mask (cr, mask);
2336 cairo_pattern_destroy (mask);
2343 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2348 GtkRequisition requisition;
2349 GtkToolItemGroupPrivate* priv = group->priv;
2351 gtk_widget_size_request (GTK_WIDGET (group), &requisition);
2353 if (!priv->collapsed || priv->animation_timeout)
2355 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2356 GtkRequisition inquery;
2359 allocation.width = limit;
2361 allocation.height = limit;
2363 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2364 &allocation, &inquery);
2367 inquery.height -= requisition.height;
2369 inquery.width -= requisition.width;
2371 if (priv->animation_timeout && animation)
2373 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2375 timestamp = MIN (timestamp, ANIMATION_DURATION);
2377 if (priv->collapsed)
2378 timestamp = ANIMATION_DURATION - timestamp;
2382 inquery.height *= timestamp;
2383 inquery.height /= ANIMATION_DURATION;
2387 inquery.width *= timestamp;
2388 inquery.width /= ANIMATION_DURATION;
2393 requisition.height += inquery.height;
2395 requisition.width += inquery.width;
2398 return (vertical ? requisition.height : requisition.width);
2402 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2405 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2409 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2412 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2416 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2419 if (GTK_IS_TOOL_ITEM (child))
2420 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2425 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2427 gtk_container_foreach (GTK_CONTAINER (group),
2428 gtk_tool_palette_reconfigured_foreach_item,
2431 gtk_tool_item_group_header_adjust_style (group);