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") == 0)
207 animation_change_notify (group);
211 gtk_tool_item_group_screen_changed (GtkWidget *widget,
212 GdkScreen *previous_screen)
214 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
215 GtkToolItemGroupPrivate* priv = group->priv;
216 GtkSettings *old_settings = priv->settings;
217 GtkSettings *settings;
219 if (gtk_widget_has_screen (GTK_WIDGET (group)))
220 settings = gtk_widget_get_settings (GTK_WIDGET (group));
224 if (settings == old_settings)
229 g_signal_handler_disconnect (old_settings, priv->settings_connection);
230 g_object_unref (old_settings);
235 priv->settings_connection =
236 g_signal_connect (settings, "notify",
237 G_CALLBACK (gtk_tool_item_group_settings_change_notify),
239 priv->settings = g_object_ref (settings);
242 priv->settings = NULL;
244 animation_change_notify (group);
248 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
250 iface->get_icon_size = gtk_tool_item_group_get_icon_size;
251 iface->get_orientation = gtk_tool_item_group_get_orientation;
252 iface->get_style = gtk_tool_item_group_get_style;
253 iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
254 iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
255 iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
256 iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
260 gtk_tool_item_group_header_expose_event_cb (GtkWidget *widget,
261 GdkEventExpose *event,
264 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
265 GtkToolItemGroupPrivate* priv = group->priv;
266 GtkExpanderStyle expander_style;
267 GtkOrientation orientation;
269 GtkTextDirection direction;
271 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
272 expander_style = priv->expander_style;
273 direction = gtk_widget_get_direction (widget);
275 if (GTK_ORIENTATION_VERTICAL == orientation)
277 if (GTK_TEXT_DIR_RTL == direction)
278 x = widget->allocation.x + widget->allocation.width - priv->expander_size / 2;
280 x = widget->allocation.x + priv->expander_size / 2;
281 y = widget->allocation.y + widget->allocation.height / 2;
285 x = widget->allocation.x + widget->allocation.width / 2;
286 y = widget->allocation.y + priv->expander_size / 2;
288 /* Unfortunatly gtk_paint_expander() doesn't support rotated drawing
289 * modes. Luckily the following shady arithmetics produce the desired
291 expander_style = GTK_EXPANDER_EXPANDED - expander_style;
294 gtk_paint_expander (widget->style, widget->window,
296 &event->area, GTK_WIDGET (group),
297 "tool-palette-header", x, y,
304 gtk_tool_item_group_header_size_request_cb (GtkWidget *widget,
305 GtkRequisition *requisition,
308 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
309 requisition->height = MAX (requisition->height, group->priv->expander_size);
313 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
316 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
317 GtkToolItemGroupPrivate* priv = group->priv;
318 GtkWidget *parent = gtk_widget_get_parent (data);
320 if (priv->collapsed ||
321 !GTK_IS_TOOL_PALETTE (parent) ||
322 !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
323 gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
327 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
329 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
330 GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
331 GtkWidget *widget = GTK_WIDGET (group);
332 GtkToolItemGroupPrivate* priv = group->priv;
334 GtkTextDirection direction = gtk_widget_get_direction (widget);
336 gtk_widget_style_get (widget,
337 "header-spacing", &(priv->header_spacing),
338 "expander-size", &(priv->expander_size),
341 switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
343 case GTK_ORIENTATION_HORIZONTAL:
344 dy = priv->header_spacing + priv->expander_size;
346 if (GTK_IS_LABEL (label_widget))
348 gtk_label_set_ellipsize (GTK_LABEL (label_widget), PANGO_ELLIPSIZE_NONE);
349 if (GTK_TEXT_DIR_RTL == direction)
350 gtk_label_set_angle (GTK_LABEL (label_widget), -90);
352 gtk_label_set_angle (GTK_LABEL (label_widget), 90);
356 case GTK_ORIENTATION_VERTICAL:
357 dx = priv->header_spacing + priv->expander_size;
359 if (GTK_IS_LABEL (label_widget))
361 gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
362 gtk_label_set_angle (GTK_LABEL (label_widget), 0);
367 gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
371 gtk_tool_item_group_init (GtkToolItemGroup *group)
373 GtkWidget *alignment;
374 GtkToolItemGroupPrivate* priv;
376 gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
378 group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
379 GTK_TYPE_TOOL_ITEM_GROUP,
380 GtkToolItemGroupPrivate);
382 priv->children = NULL;
383 priv->header_spacing = DEFAULT_HEADER_SPACING;
384 priv->expander_size = DEFAULT_EXPANDER_SIZE;
385 priv->expander_style = GTK_EXPANDER_EXPANDED;
387 priv->label_widget = gtk_label_new (NULL);
388 gtk_misc_set_alignment (GTK_MISC (priv->label_widget), 0.0, 0.5);
389 alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
390 gtk_container_add (GTK_CONTAINER (alignment), priv->label_widget);
391 gtk_widget_show_all (alignment);
393 gtk_widget_push_composite_child ();
394 priv->header = gtk_button_new ();
395 gtk_widget_set_composite_name (priv->header, "header");
396 gtk_widget_pop_composite_child ();
398 g_object_ref_sink (priv->header);
399 gtk_button_set_focus_on_click (GTK_BUTTON (priv->header), FALSE);
400 gtk_container_add (GTK_CONTAINER (priv->header), alignment);
401 gtk_widget_set_parent (priv->header, GTK_WIDGET (group));
403 gtk_tool_item_group_header_adjust_style (group);
405 g_signal_connect_after (alignment, "expose-event",
406 G_CALLBACK (gtk_tool_item_group_header_expose_event_cb),
408 g_signal_connect_after (alignment, "size-request",
409 G_CALLBACK (gtk_tool_item_group_header_size_request_cb),
412 g_signal_connect (priv->header, "clicked",
413 G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
418 gtk_tool_item_group_set_property (GObject *object,
423 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
428 gtk_tool_item_group_set_label (group, g_value_get_string (value));
430 case PROP_LABEL_WIDGET:
431 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
433 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
436 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
439 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 gtk_tool_item_group_get_property (GObject *object,
453 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
458 g_value_set_string (value, gtk_tool_item_group_get_label (group));
461 case PROP_LABEL_WIDGET:
462 g_value_set_object (value,
463 gtk_tool_item_group_get_label_widget (group));
467 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
471 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
475 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
479 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
485 gtk_tool_item_group_finalize (GObject *object)
487 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
489 if (group->priv->children)
491 g_list_free (group->priv->children);
492 group->priv->children = NULL;
495 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
499 gtk_tool_item_group_dispose (GObject *object)
501 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
502 GtkToolItemGroupPrivate* priv = group->priv;
506 /* disconnect focus tracking handler */
507 g_signal_handler_disconnect (priv->toplevel,
510 priv->focus_set_id = 0;
511 priv->toplevel = NULL;
514 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
518 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
519 GtkRequisition *item_size,
520 gboolean homogeneous_only,
521 gint *requested_rows)
523 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
525 if (GTK_IS_TOOL_PALETTE (parent))
526 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
528 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
532 gtk_tool_item_group_size_request (GtkWidget *widget,
533 GtkRequisition *requisition)
535 const gint border_width = GTK_CONTAINER (widget)->border_width;
536 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
537 GtkToolItemGroupPrivate* priv = group->priv;
538 GtkOrientation orientation;
539 GtkRequisition item_size;
542 if (priv->children && gtk_tool_item_group_get_label_widget (group))
544 gtk_widget_size_request (priv->header, requisition);
545 gtk_widget_show (priv->header);
549 requisition->width = requisition->height = 0;
550 gtk_widget_hide (priv->header);
553 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
555 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
557 if (GTK_ORIENTATION_VERTICAL == orientation)
558 requisition->width = MAX (requisition->width, item_size.width);
560 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
562 requisition->width += border_width * 2;
563 requisition->height += border_width * 2;
567 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
568 GtkToolItemGroupChild *child)
570 GtkToolbarStyle style;
571 GtkOrientation orientation;
573 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
574 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
576 /* horizontal tool palettes with text style support only homogeneous items */
577 if (!child->homogeneous &&
578 GTK_ORIENTATION_HORIZONTAL == orientation &&
579 GTK_TOOLBAR_TEXT == style)
583 (GTK_WIDGET_VISIBLE (child->item)) &&
584 (GTK_ORIENTATION_VERTICAL == orientation ?
585 gtk_tool_item_get_visible_vertical (child->item) :
586 gtk_tool_item_get_visible_horizontal (child->item));
589 static inline unsigned
593 return (x + y - 1) / y;
597 gtk_tool_item_group_real_size_query (GtkWidget *widget,
598 GtkAllocation *allocation,
599 GtkRequisition *inquery)
601 const gint border_width = GTK_CONTAINER (widget)->border_width;
602 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
603 GtkToolItemGroupPrivate* priv = group->priv;
605 GtkRequisition item_size;
606 GtkAllocation item_area;
608 GtkOrientation orientation;
609 GtkToolbarStyle style;
613 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
614 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
616 /* figure out the size of homogeneous items */
617 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
619 if (GTK_ORIENTATION_VERTICAL == orientation)
620 item_size.width = MIN (item_size.width, allocation->width);
622 item_size.height = MIN (item_size.height, allocation->height);
625 item_area.height = 0;
627 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
628 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
634 if (GTK_ORIENTATION_VERTICAL == orientation)
636 gboolean new_row = FALSE;
640 item_area.width = allocation->width - 2 * border_width;
641 n_columns = MAX (item_area.width / item_size.width, 1);
643 /* calculate required rows for n_columns columns */
644 for (it = priv->children; it != NULL; it = it->next)
646 GtkToolItemGroupChild *child = it->data;
648 if (!gtk_tool_item_group_is_item_visible (group, child))
651 if (new_row || child->new_row)
661 if (child->homogeneous)
664 if (col >= n_columns)
669 GtkRequisition req = {0, 0};
672 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
674 width = udiv (req.width, item_size.width);
682 if (col >= n_columns)
690 guint *row_min_width;
692 gboolean new_row = TRUE;
693 guint col = 0, min_col, max_col = 0, all_items = 0;
696 item_area.height = allocation->height - 2 * border_width;
697 n_rows = MAX (item_area.height / item_size.height, min_rows);
699 row_min_width = g_new0 (guint, n_rows);
701 /* calculate minimal and maximal required cols and minimal required rows */
702 for (it = priv->children; it != NULL; it = it->next)
704 GtkToolItemGroupChild *child = it->data;
706 if (!gtk_tool_item_group_is_item_visible (group, child))
709 if (new_row || child->new_row)
714 row_min_width[row] = 1;
720 if (child->homogeneous)
727 GtkRequisition req = {0, 0};
730 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
732 width = udiv (req.width, item_size.width);
737 row_min_width[row] = MAX (row_min_width[row], width);
740 max_col = MAX (max_col, col);
743 /* calculate minimal required cols */
744 min_col = udiv (all_items, n_rows);
746 for (i = 0; i <= row; i++)
748 min_col = MAX (min_col, row_min_width[i]);
751 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
752 for (n_columns = min_col; n_columns < max_col; n_columns ++)
756 /* calculate required rows for n_columns columns */
757 for (it = priv->children; it != NULL; it = it->next)
759 GtkToolItemGroupChild *child = it->data;
761 if (!gtk_tool_item_group_is_item_visible (group, child))
764 if (new_row || child->new_row)
774 if (child->homogeneous)
777 if (col >= n_columns)
782 GtkRequisition req = {0, 0};
785 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
787 width = udiv (req.width, item_size.width);
795 if (col >= n_columns)
805 item_area.width = item_size.width * n_columns;
806 item_area.height = item_size.height * n_rows;
812 /* figure out header widget size */
813 if (GTK_WIDGET_VISIBLE (priv->header))
815 GtkRequisition child_requisition;
817 gtk_widget_size_request (priv->header, &child_requisition);
819 if (GTK_ORIENTATION_VERTICAL == orientation)
820 inquery->height += child_requisition.height;
822 inquery->width += child_requisition.width;
825 /* report effective widget size */
826 inquery->width += item_area.width + 2 * border_width;
827 inquery->height += item_area.height + 2 * border_width;
831 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
832 GtkAllocation *allocation)
834 const gint border_width = GTK_CONTAINER (widget)->border_width;
835 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
836 GtkToolItemGroupPrivate* priv = group->priv;
837 GtkRequisition child_requisition;
838 GtkAllocation child_allocation;
840 GtkRequisition item_size;
841 GtkAllocation item_area;
843 GtkOrientation orientation;
844 GtkToolbarStyle style;
848 gint n_columns, n_rows = 1;
851 GtkTextDirection direction = gtk_widget_get_direction (widget);
853 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
854 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
857 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
859 child_allocation.x = border_width;
860 child_allocation.y = border_width;
862 /* place the header widget */
863 if (GTK_WIDGET_VISIBLE (priv->header))
865 gtk_widget_size_request (priv->header, &child_requisition);
867 if (GTK_ORIENTATION_VERTICAL == orientation)
869 child_allocation.width = allocation->width;
870 child_allocation.height = child_requisition.height;
874 child_allocation.width = child_requisition.width;
875 child_allocation.height = allocation->height;
877 if (GTK_TEXT_DIR_RTL == direction)
878 child_allocation.x = allocation->width - border_width - child_allocation.width;
881 gtk_widget_size_allocate (priv->header, &child_allocation);
883 if (GTK_ORIENTATION_VERTICAL == orientation)
884 child_allocation.y += child_allocation.height;
885 else if (GTK_TEXT_DIR_RTL != direction)
886 child_allocation.x += child_allocation.width;
888 child_allocation.x = border_width;
891 child_requisition.width = child_requisition.height = 0;
893 /* figure out the size of homogeneous items */
894 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
896 /* figure out the available columns and size of item_area */
897 if (GTK_ORIENTATION_VERTICAL == orientation)
899 item_size.width = MIN (item_size.width, allocation->width);
901 item_area.width = allocation->width - 2 * border_width;
902 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
904 n_columns = MAX (item_area.width / item_size.width, 1);
906 item_size.width = item_area.width / n_columns;
910 item_size.height = MIN (item_size.height, allocation->height);
912 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
913 item_area.height = allocation->height - 2 * border_width;
915 n_columns = MAX (item_area.width / item_size.width, 1);
916 n_rows = MAX (item_area.height / item_size.height, min_rows);
918 item_size.height = item_area.height / n_rows;
921 item_area.x = child_allocation.x;
922 item_area.y = child_allocation.y;
924 /* when expanded or in transition, place the tool items in a grid like layout */
925 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
927 gint col = 0, row = 0;
929 for (it = priv->children; it != NULL; it = it->next)
931 GtkToolItemGroupChild *child = it->data;
934 if (!gtk_tool_item_group_is_item_visible (group, child))
936 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
941 /* for non homogeneous widgets request the required size */
942 child_requisition.width = 0;
944 if (!child->homogeneous)
946 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
947 child_requisition.width = MIN (child_requisition.width, item_area.width);
950 /* select next row if at end of row */
951 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
955 child_allocation.y += child_allocation.height;
960 /* calculate the position and size of the item */
961 if (!child->homogeneous)
967 col_width = udiv (child_requisition.width, item_size.width);
969 col_width = n_columns - col;
971 width = col_width * item_size.width;
973 if (GTK_TEXT_DIR_RTL == direction)
974 col_child = (n_columns - col - col_width);
978 child_allocation.x = item_area.x + col_child * item_size.width;
979 child_allocation.width = width;
984 (item_area.x + col_child * item_size.width +
985 (width - child_requisition.width) / 2);
986 child_allocation.width = child_requisition.width;
993 if (GTK_TEXT_DIR_RTL == direction)
994 col_child = (n_columns - col - 1);
996 child_allocation.x = item_area.x + col_child * item_size.width;
997 child_allocation.width = item_size.width;
1002 child_allocation.height = item_size.height;
1004 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1005 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1008 child_allocation.y += item_size.height;
1011 /* or just hide all items, when collapsed */
1015 for (it = priv->children; it != NULL; it = it->next)
1017 GtkToolItemGroupChild *child = it->data;
1019 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1025 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1026 GtkAllocation *allocation)
1028 gtk_tool_item_group_real_size_allocate (widget, allocation);
1030 if (GTK_WIDGET_MAPPED (widget))
1031 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
1035 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1039 GtkAdjustment *adjustment;
1042 /* Find this group's parent widget in the focused widget's anchestry. */
1043 for (p = widget; p; p = gtk_widget_get_parent (p))
1046 p = gtk_widget_get_parent (p);
1050 if (GTK_IS_TOOL_PALETTE (p))
1052 /* Check that the focused widgets is fully visible within
1053 * the group's parent widget and make it visible otherwise. */
1055 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1056 adjustment = gtk_tool_palette_get_vadjustment (GTK_TOOL_PALETTE (p));
1062 /* Handle vertical adjustment. */
1063 if (gtk_widget_translate_coordinates
1064 (widget, p, 0, 0, NULL, &y) && y < 0)
1066 y += adjustment->value;
1067 gtk_adjustment_clamp_page (adjustment, y, y + widget->allocation.height);
1069 else if (gtk_widget_translate_coordinates
1070 (widget, p, 0, widget->allocation.height, NULL, &y) &&
1071 y > p->allocation.height)
1073 y += adjustment->value;
1074 gtk_adjustment_clamp_page (adjustment, y - widget->allocation.height, y);
1078 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1084 /* Handle horizontal adjustment. */
1085 if (gtk_widget_translate_coordinates
1086 (widget, p, 0, 0, &x, NULL) && x < 0)
1088 x += adjustment->value;
1089 gtk_adjustment_clamp_page (adjustment, x, x + widget->allocation.width);
1091 else if (gtk_widget_translate_coordinates
1092 (widget, p, widget->allocation.width, 0, &x, NULL) &&
1093 x > p->allocation.width)
1095 x += adjustment->value;
1096 gtk_adjustment_clamp_page (adjustment, x - widget->allocation.width, x);
1105 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1106 GtkWidget *toplevel)
1108 GtkToolItemGroupPrivate* priv = group->priv;
1110 if (toplevel != priv->toplevel)
1114 /* Disconnect focus tracking handler. */
1115 g_signal_handler_disconnect (priv->toplevel,
1116 priv->focus_set_id);
1118 priv->focus_set_id = 0;
1119 priv->toplevel = NULL;
1124 /* Install focus tracking handler. We connect to the window's
1125 * set-focus signal instead of connecting to the focus signal of
1128 * 1) Reduce the number of signal handlers used.
1129 * 2) Avoid special handling for group headers.
1130 * 3) Catch focus grabs not only for direct children,
1131 * but also for nested widgets.
1133 priv->focus_set_id =
1134 g_signal_connect (toplevel, "set-focus",
1135 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1138 priv->toplevel = toplevel;
1144 gtk_tool_item_group_realize (GtkWidget *widget)
1146 GtkWidget *toplevel_window;
1147 const gint border_width = GTK_CONTAINER (widget)->border_width;
1148 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1149 GdkWindowAttr attributes;
1150 GdkDisplay *display;
1152 attributes.window_type = GDK_WINDOW_CHILD;
1153 attributes.x = widget->allocation.x + border_width;
1154 attributes.y = widget->allocation.y + border_width;
1155 attributes.width = widget->allocation.width - border_width * 2;
1156 attributes.height = widget->allocation.height - border_width * 2;
1157 attributes.wclass = GDK_INPUT_OUTPUT;
1158 attributes.visual = gtk_widget_get_visual (widget);
1159 attributes.colormap = gtk_widget_get_colormap (widget);
1160 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1161 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1162 | GDK_BUTTON_MOTION_MASK;
1164 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1165 &attributes, attributes_mask);
1167 display = gdk_drawable_get_display (widget->window);
1169 if (gdk_display_supports_composite (display))
1170 gdk_window_set_composited (widget->window, TRUE);
1172 gdk_window_set_user_data (widget->window, widget);
1173 widget->style = gtk_style_attach (widget->style, widget->window);
1174 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1175 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1177 gtk_container_forall (GTK_CONTAINER (widget),
1178 (GtkCallback) gtk_widget_set_parent_window,
1181 gtk_widget_queue_resize_no_redraw (widget);
1183 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1184 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1189 gtk_tool_item_group_unrealize (GtkWidget *widget)
1191 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1192 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1196 gtk_tool_item_group_style_set (GtkWidget *widget,
1197 GtkStyle *previous_style)
1199 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1200 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1204 gtk_tool_item_group_add (GtkContainer *container,
1207 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1208 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1210 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1211 GTK_TOOL_ITEM (widget), -1);
1215 gtk_tool_item_group_remove (GtkContainer *container,
1218 GtkToolItemGroup *group;
1219 GtkToolItemGroupPrivate* priv;
1222 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1223 group = GTK_TOOL_ITEM_GROUP (container);
1226 for (it = priv->children; it != NULL; it = it->next)
1228 GtkToolItemGroupChild *child_info = it->data;
1230 if ((GtkWidget *)child_info->item == child)
1232 g_object_unref (child);
1233 gtk_widget_unparent (child);
1235 g_free (child_info);
1236 priv->children = g_list_delete_link (priv->children, it);
1238 gtk_widget_queue_resize (GTK_WIDGET (container));
1245 gtk_tool_item_group_forall (GtkContainer *container,
1247 GtkCallback callback,
1248 gpointer callback_data)
1250 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1251 GtkToolItemGroupPrivate* priv = group->priv;
1254 if (internals && priv->header)
1255 callback (priv->header, callback_data);
1257 children = priv->children;
1260 GtkToolItemGroupChild *child = children->data;
1261 children = children->next; /* store pointer before call to callback
1262 because the child pointer is invalid if the
1263 child->item is removed from the item group
1266 callback (GTK_WIDGET (child->item), callback_data);
1271 gtk_tool_item_group_child_type (GtkContainer *container)
1273 return GTK_TYPE_TOOL_ITEM;
1276 static GtkToolItemGroupChild *
1277 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1285 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1286 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1288 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1290 GtkToolItemGroupChild *child = it->data;
1292 if (child->item == item)
1308 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1310 gboolean *homogeneous,
1315 GtkToolItemGroupChild *child;
1317 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1318 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1320 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1325 *expand = child->expand;
1328 *homogeneous = child->homogeneous;
1331 *fill = child->fill;
1334 *new_row = child->new_row;
1338 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1340 gboolean homogeneous,
1345 GtkToolItemGroupChild *child;
1346 gboolean changed = FALSE;
1348 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1349 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1351 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1355 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1357 if (child->homogeneous != homogeneous)
1359 child->homogeneous = homogeneous;
1361 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1363 if (child->expand != expand)
1365 child->expand = expand;
1367 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1369 if (child->fill != fill)
1373 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1375 if (child->new_row != new_row)
1377 child->new_row = new_row;
1379 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1382 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1384 if (changed && GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
1385 gtk_widget_queue_resize (GTK_WIDGET (group));
1389 gtk_tool_item_group_set_child_property (GtkContainer *container,
1392 const GValue *value,
1395 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1396 GtkToolItem *item = GTK_TOOL_ITEM (child);
1397 gboolean homogeneous, expand, fill, new_row;
1399 if (prop_id != CHILD_PROP_POSITION)
1400 gtk_tool_item_group_get_item_packing (group, item,
1408 case CHILD_PROP_HOMOGENEOUS:
1409 gtk_tool_item_group_set_item_packing (group, item,
1410 g_value_get_boolean (value),
1416 case CHILD_PROP_EXPAND:
1417 gtk_tool_item_group_set_item_packing (group, item,
1419 g_value_get_boolean (value),
1424 case CHILD_PROP_FILL:
1425 gtk_tool_item_group_set_item_packing (group, item,
1428 g_value_get_boolean (value),
1432 case CHILD_PROP_NEW_ROW:
1433 gtk_tool_item_group_set_item_packing (group, item,
1437 g_value_get_boolean (value));
1440 case CHILD_PROP_POSITION:
1441 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1445 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1451 gtk_tool_item_group_get_child_property (GtkContainer *container,
1457 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1458 GtkToolItem *item = GTK_TOOL_ITEM (child);
1459 gboolean homogeneous, expand, fill, new_row;
1461 if (prop_id != CHILD_PROP_POSITION)
1462 gtk_tool_item_group_get_item_packing (group, item,
1470 case CHILD_PROP_HOMOGENEOUS:
1471 g_value_set_boolean (value, homogeneous);
1474 case CHILD_PROP_EXPAND:
1475 g_value_set_boolean (value, expand);
1478 case CHILD_PROP_FILL:
1479 g_value_set_boolean (value, fill);
1482 case CHILD_PROP_NEW_ROW:
1483 g_value_set_boolean (value, new_row);
1486 case CHILD_PROP_POSITION:
1487 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1491 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1497 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1499 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1500 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1501 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1503 oclass->set_property = gtk_tool_item_group_set_property;
1504 oclass->get_property = gtk_tool_item_group_get_property;
1505 oclass->finalize = gtk_tool_item_group_finalize;
1506 oclass->dispose = gtk_tool_item_group_dispose;
1508 wclass->size_request = gtk_tool_item_group_size_request;
1509 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1510 wclass->realize = gtk_tool_item_group_realize;
1511 wclass->unrealize = gtk_tool_item_group_unrealize;
1512 wclass->style_set = gtk_tool_item_group_style_set;
1513 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1515 cclass->add = gtk_tool_item_group_add;
1516 cclass->remove = gtk_tool_item_group_remove;
1517 cclass->forall = gtk_tool_item_group_forall;
1518 cclass->child_type = gtk_tool_item_group_child_type;
1519 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1520 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1522 g_object_class_install_property (oclass, PROP_LABEL,
1523 g_param_spec_string ("label",
1525 P_("The human-readable title of this item group"),
1527 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1528 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1530 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1531 g_param_spec_object ("label-widget",
1533 P_("A widget to display in place of the usual label"),
1535 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1536 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1538 g_object_class_install_property (oclass, PROP_COLLAPSED,
1539 g_param_spec_boolean ("collapsed",
1541 P_("Wether the group has been collapsed and items are hidden"),
1543 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1544 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1546 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1547 g_param_spec_enum ("ellipsize",
1549 P_("Ellipsize for item group headers"),
1550 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1551 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1552 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1554 g_object_class_install_property (oclass, PROP_RELIEF,
1555 g_param_spec_enum ("header-relief",
1556 P_("Header Relief"),
1557 P_("Relief of the group header button"),
1558 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1559 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1560 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1562 gtk_widget_class_install_style_property (wclass,
1563 g_param_spec_int ("expander-size",
1564 P_("Expander Size"),
1565 P_("Size of the expander arrow"),
1568 DEFAULT_EXPANDER_SIZE,
1569 G_PARAM_READABLE | G_PARAM_STATIC_NAME |
1570 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1572 gtk_widget_class_install_style_property (wclass,
1573 g_param_spec_int ("header-spacing",
1574 P_("Header Spacing"),
1575 P_("Spacing between expander arrow and caption"),
1578 DEFAULT_HEADER_SPACING,
1579 G_PARAM_READABLE | G_PARAM_STATIC_NAME |
1580 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1582 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1583 g_param_spec_boolean ("homogeneous",
1585 P_("Whether the item should be the same size as other homogeneous items"),
1587 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1588 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1590 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1591 g_param_spec_boolean ("expand",
1593 P_("Whether the item should receive extra space when the toolbar grows"),
1595 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1596 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1598 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1599 g_param_spec_boolean ("fill",
1601 P_("Whether the item should fill the avaiable space"),
1603 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1604 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1606 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1607 g_param_spec_boolean ("new-row",
1609 P_("Whether the item should start a new row"),
1611 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1612 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1614 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1615 g_param_spec_int ("position",
1617 P_("Position of the item within this group"),
1621 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1622 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1624 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1628 * gtk_tool_item_group_new:
1629 * @label: the label of the new group.
1631 * Creates a new tool item group with label @label.
1633 * Returns: a new #GtkToolItemGroup.
1638 gtk_tool_item_group_new (const gchar *label)
1640 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label",
1645 * gtk_tool_item_group_set_label:
1646 * @group: an #GtkToolItemGroup.
1647 * @label: the new human-readable label of of the group.
1649 * Sets the label of the tool item group. The label is displayed in the header
1655 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1658 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1661 gtk_tool_item_group_set_label_widget (group, NULL);
1664 GtkWidget *child = gtk_label_new (label);
1665 gtk_widget_show (child);
1667 gtk_tool_item_group_set_label_widget (group, child);
1670 g_object_notify (G_OBJECT (group), "label");
1674 * gtk_tool_item_group_set_label_widget:
1675 * @group: an #GtkToolItemGroup.
1676 * @widget: the new human-readable label of of the group.
1678 * Sets the label of the tool item group. The label is displayed in the header
1684 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1685 GtkWidget *label_widget)
1687 GtkToolItemGroupPrivate* priv;
1688 GtkWidget *alignment;
1690 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1691 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1692 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1696 if (priv->label_widget == label_widget)
1699 alignment = gtk_tool_item_group_get_alignment (group);
1701 if (priv->label_widget)
1703 gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1704 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1709 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1711 priv->label_widget = label_widget;
1713 if (GTK_WIDGET_VISIBLE (group))
1714 gtk_widget_queue_resize (GTK_WIDGET (group));
1716 /* Only show the header widget if the group has children: */
1717 if (label_widget && priv->children)
1718 gtk_widget_show (priv->header);
1720 gtk_widget_hide (priv->header);
1722 g_object_freeze_notify (G_OBJECT (group));
1723 g_object_notify (G_OBJECT (group), "label-widget");
1724 g_object_notify (G_OBJECT (group), "label");
1725 g_object_thaw_notify (G_OBJECT (group));
1729 * gtk_tool_item_group_header_relief:
1730 * @group: an #GtkToolItemGroup.
1731 * @style: The GtkReliefStyle
1733 * Set the button relief of the group header. See #gtk_button_set_relief for
1739 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1740 GtkReliefStyle style)
1742 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1743 gtk_button_set_relief (GTK_BUTTON(group->priv->header), style);
1747 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1750 g_source_get_current_time (group->priv->animation_timeout, &now);
1751 return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1755 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1757 GtkToolItemGroupPrivate* priv = group->priv;
1759 if (GTK_WIDGET_REALIZED (priv->header))
1761 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1764 /* Find the header button's arrow area... */
1765 area.x = alignment->allocation.x;
1766 area.y = alignment->allocation.y + (alignment->allocation.height - priv->expander_size) / 2;
1767 area.height = priv->expander_size;
1768 area.width = priv->expander_size;
1770 /* ... and invalidated it to get it animated. */
1771 gdk_window_invalidate_rect (priv->header->window, &area, TRUE);
1774 if (GTK_WIDGET_REALIZED (group))
1776 GtkWidget *widget = GTK_WIDGET (group);
1777 GtkWidget *parent = gtk_widget_get_parent (widget);
1778 int x, y, width, height;
1780 /* Find the tool item area button's arrow area... */
1781 width = widget->allocation.width;
1782 height = widget->allocation.height;
1784 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1786 if (GTK_WIDGET_VISIBLE (priv->header))
1788 height -= priv->header->allocation.height;
1789 y += priv->header->allocation.height;
1792 /* ... and invalidated it to get it animated. */
1793 gtk_widget_queue_draw_area (parent, x, y, width, height);
1798 gtk_tool_item_group_animation_cb (gpointer data)
1800 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1801 GtkToolItemGroupPrivate* priv = group->priv;
1802 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1805 GDK_THREADS_ENTER();
1807 /* Enque this early to reduce number of expose events. */
1808 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1810 /* Figure out current style of the expander arrow. */
1811 if (priv->collapsed)
1813 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1814 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1816 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1820 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1821 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1823 priv->expander_style = GTK_EXPANDER_EXPANDED;
1826 gtk_tool_item_group_force_expose (group);
1828 /* Finish animation when done. */
1829 if (timestamp >= ANIMATION_DURATION)
1830 priv->animation_timeout = NULL;
1832 retval = (priv->animation_timeout != NULL);
1834 GDK_THREADS_LEAVE();
1840 * gtk_tool_item_group_set_collapsed:
1841 * @group: an #GtkToolItemGroup.
1842 * @collapsed: whether the @group should be collapsed or expanded.
1844 * Sets whether the @group should be collapsed or expanded.
1849 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1853 GtkToolItemGroupPrivate* priv;
1855 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1859 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1860 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1861 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1862 GTK_WIDGET (group));
1863 if (collapsed != priv->collapsed)
1865 if (priv->animation)
1869 g_get_current_time (&now);
1871 if (priv->animation_timeout)
1872 g_source_destroy (priv->animation_timeout);
1874 priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1875 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1877 g_source_set_callback (priv->animation_timeout,
1878 gtk_tool_item_group_animation_cb,
1881 g_source_attach (priv->animation_timeout, NULL);
1885 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1886 gtk_tool_item_group_force_expose (group);
1889 priv->collapsed = collapsed;
1890 g_object_notify (G_OBJECT (group), "collapsed");
1895 * gtk_tool_item_group_set_ellipsize:
1896 * @group: an #GtkToolItemGroup.
1897 * @ellipsize: the #PangoEllipsizeMode labels in @group should use.
1899 * Sets the ellipsization mode which should be used by labels in @group.
1904 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1905 PangoEllipsizeMode ellipsize)
1907 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1909 if (ellipsize != group->priv->ellipsize)
1911 group->priv->ellipsize = ellipsize;
1912 gtk_tool_item_group_header_adjust_style (group);
1913 g_object_notify (G_OBJECT (group), "ellipsize");
1914 _gtk_tool_item_group_palette_reconfigured (group);
1919 * gtk_tool_item_group_get_label:
1920 * @group: an #GtkToolItemGroup.
1922 * Gets the label of @group.
1924 * Returns: the label of @group. The label is an internal string of @group and must not be modified.
1925 * Note that NULL is returned if a custom label has been set with gtk_tool_item_group_set_label_widget()
1929 G_CONST_RETURN gchar*
1930 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1932 GtkToolItemGroupPrivate *priv;
1934 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1938 if (GTK_IS_LABEL (priv->label_widget))
1939 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1945 * gtk_tool_item_group_get_label_widget:
1946 * @group: an #GtkToolItemGroup.
1948 * Gets the label widget of @group.
1950 * Returns: the label widget of @group. See gtk_tool_item_group_set_label_widget()
1955 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
1957 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1958 return gtk_bin_get_child (GTK_BIN (alignment));
1962 * gtk_tool_item_group_get_collapsed:
1963 * @group: an GtkToolItemGroup.
1965 * Gets whether @group is collapsed or expanded.
1967 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded.
1972 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
1974 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
1975 return group->priv->collapsed;
1979 * gtk_tool_item_group_get_ellipsize:
1980 * @group: an #GtkToolItemGroup.
1982 * Gets the ellipsization mode of @group.
1984 * Returns: the #PangoEllipsizeMode of @group.
1989 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
1991 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
1992 return group->priv->ellipsize;
1996 * gtk_tool_item_group_get_header_relief:
1997 * @group: an #GtkToolItemGroup.
1999 * Gets the relief mode of the header button of @group.
2001 * Returns: the #GtkReliefStyle
2006 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2008 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2009 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2013 * gtk_tool_item_group_insert:
2014 * @group: an #GtkToolItemGroup.
2015 * @item: the #GtkToolItem to insert into @group.
2016 * @position: the position of @item in @group, starting with 0. The position -1 means end of list.
2018 * Inserts @item at @position in the list of children of @group.
2023 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2027 GtkWidget *parent, *child_widget;
2028 GtkToolItemGroupChild *child;
2030 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2031 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2032 g_return_if_fail (position >= -1);
2034 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2036 child = g_new (GtkToolItemGroupChild, 1);
2037 child->item = g_object_ref_sink (item);
2038 child->homogeneous = TRUE;
2039 child->expand = FALSE;
2041 child->new_row = FALSE;
2043 group->priv->children = g_list_insert (group->priv->children, child, position);
2045 if (GTK_IS_TOOL_PALETTE (parent))
2046 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2048 child_widget = gtk_bin_get_child (GTK_BIN (item));
2050 if (GTK_IS_BUTTON (child_widget))
2051 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2053 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2057 * gtk_tool_item_group_set_item_position:
2058 * @group: an #GtkToolItemGroup.
2059 * @item: the #GtkToolItem to move to a new position, should be a child of @group.
2060 * @position: the new position of @item in @group, starting with 0. The position -1 means end of list.
2062 * Sets the position of @item in the list of children of @group.
2067 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2073 GtkToolItemGroupChild *child;
2074 GtkToolItemGroupPrivate* priv;
2076 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2077 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2079 g_return_if_fail (position >= -1);
2081 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2084 g_return_if_fail (child != NULL);
2086 if (position == old_position)
2089 priv->children = g_list_delete_link (priv->children, link);
2090 priv->children = g_list_insert (priv->children, child, position);
2092 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2093 if (GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
2094 gtk_widget_queue_resize (GTK_WIDGET (group));
2098 * gtk_tool_item_group_get_item_position:
2099 * @group: an #GtkToolItemGroup.
2100 * @item: a #GtkToolItem.
2102 * Gets the position of @item in @group as index.
2104 * Returns: the index of @item in @group or -1 if @item is no child of @group.
2109 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2114 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2115 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2117 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2124 * gtk_tool_item_group_get_n_items:
2125 * @group: an #GtkToolItemGroup.
2127 * Gets the number of tool items in group.
2129 * Returns: the number of tool items in group.
2134 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2136 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2138 return g_list_length (group->priv->children);
2142 * gtk_tool_item_group_get_nth_item:
2143 * @group: an #GtkToolItemGroup.
2144 * @index: the index.
2146 * Gets the tool item at index in group.
2148 * Returns: the #GtkToolItem at index.
2153 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2156 GtkToolItemGroupChild *child;
2158 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2160 child = g_list_nth_data (group->priv->children, index);
2162 return child != NULL ? child->item : NULL;
2166 * gtk_tool_item_group_get_drop_item:
2167 * @group: an #GtkToolItemGroup.
2168 * @x: the x position.
2169 * @y: the y position.
2171 * Gets the tool item at position (x, y).
2173 * Returns: the #GtkToolItem at position (x, y).
2178 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2182 GtkAllocation *allocation;
2183 GtkOrientation orientation;
2186 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2188 allocation = >K_WIDGET (group)->allocation;
2189 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2191 g_return_val_if_fail (x >= 0 && x < allocation->width, NULL);
2192 g_return_val_if_fail (y >= 0 && y < allocation->height, NULL);
2194 for (it = group->priv->children; it != NULL; it = it->next)
2196 GtkToolItemGroupChild *child = it->data;
2197 GtkToolItem *item = child->item;
2200 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2203 allocation = >K_WIDGET (item)->allocation;
2205 x0 = x - allocation->x;
2206 y0 = y - allocation->y;
2208 if (x0 >= 0 && x0 < allocation->width &&
2209 y0 >= 0 && y0 < allocation->height)
2217 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2218 GtkRequisition *item_size,
2219 gboolean homogeneous_only,
2220 gint *requested_rows)
2222 GtkRequisition child_requisition;
2225 gboolean new_row = TRUE;
2226 GtkOrientation orientation;
2227 GtkToolbarStyle style;
2229 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2230 g_return_if_fail (NULL != item_size);
2232 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2233 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2235 item_size->width = item_size->height = 0;
2237 for (it = group->priv->children; it != NULL; it = it->next)
2239 GtkToolItemGroupChild *child = it->data;
2241 if (!gtk_tool_item_group_is_item_visible (group, child))
2244 if (child->new_row || new_row)
2250 if (!child->homogeneous && child->expand)
2253 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
2255 if (!homogeneous_only || child->homogeneous)
2256 item_size->width = MAX (item_size->width, child_requisition.width);
2257 item_size->height = MAX (item_size->height, child_requisition.height);
2261 *requested_rows = rows;
2265 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2268 GtkWidget *widget = GTK_WIDGET (group);
2269 GtkToolItemGroupPrivate* priv = group->priv;
2271 gdk_cairo_set_source_pixmap (cr, widget->window,
2272 widget->allocation.x,
2273 widget->allocation.y);
2275 if (priv->animation_timeout)
2277 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2278 cairo_pattern_t *mask;
2281 if (GTK_ORIENTATION_VERTICAL == orientation)
2282 v1 = widget->allocation.height;
2284 v1 = widget->allocation.width;
2288 if (!GTK_WIDGET_VISIBLE (priv->header))
2290 else if (GTK_ORIENTATION_VERTICAL == orientation)
2291 v0 = MAX (v0, priv->header->allocation.height);
2293 v0 = MAX (v0, priv->header->allocation.width);
2295 v1 = MIN (v0 + 256, v1);
2297 if (GTK_ORIENTATION_VERTICAL == orientation)
2299 v0 += widget->allocation.y;
2300 v1 += widget->allocation.y;
2302 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2306 v0 += widget->allocation.x;
2307 v1 += widget->allocation.x;
2309 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2312 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2313 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2314 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2315 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2316 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2318 cairo_mask (cr, mask);
2319 cairo_pattern_destroy (mask);
2326 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2331 GtkRequisition requisition;
2332 GtkToolItemGroupPrivate* priv = group->priv;
2334 gtk_widget_size_request (GTK_WIDGET (group), &requisition);
2336 if (!priv->collapsed || priv->animation_timeout)
2338 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2339 GtkRequisition inquery;
2342 allocation.width = limit;
2344 allocation.height = limit;
2346 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2347 &allocation, &inquery);
2350 inquery.height -= requisition.height;
2352 inquery.width -= requisition.width;
2354 if (priv->animation_timeout && animation)
2356 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2358 timestamp = MIN (timestamp, ANIMATION_DURATION);
2360 if (priv->collapsed)
2361 timestamp = ANIMATION_DURATION - timestamp;
2365 inquery.height *= timestamp;
2366 inquery.height /= ANIMATION_DURATION;
2370 inquery.width *= timestamp;
2371 inquery.width /= ANIMATION_DURATION;
2376 requisition.height += inquery.height;
2378 requisition.width += inquery.width;
2381 return (vertical ? requisition.height : requisition.width);
2385 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2388 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2392 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2395 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2399 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2402 if (GTK_IS_TOOL_ITEM (child))
2403 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2408 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2410 gtk_container_foreach (GTK_CONTAINER (group),
2411 gtk_tool_palette_reconfigured_foreach_item,
2414 gtk_tool_item_group_header_adjust_style (group);