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));
431 case PROP_LABEL_WIDGET:
432 gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
435 gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
439 gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
442 gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 gtk_tool_item_group_get_property (GObject *object,
456 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
461 g_value_set_string (value, gtk_tool_item_group_get_label (group));
464 case PROP_LABEL_WIDGET:
465 g_value_set_object (value,
466 gtk_tool_item_group_get_label_widget (group));
470 g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
474 g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
478 g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
482 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
488 gtk_tool_item_group_finalize (GObject *object)
490 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
492 if (group->priv->children)
494 g_list_free (group->priv->children);
495 group->priv->children = NULL;
498 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
502 gtk_tool_item_group_dispose (GObject *object)
504 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
505 GtkToolItemGroupPrivate* priv = group->priv;
509 /* disconnect focus tracking handler */
510 g_signal_handler_disconnect (priv->toplevel,
513 priv->focus_set_id = 0;
514 priv->toplevel = NULL;
517 G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
521 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
522 GtkRequisition *item_size,
523 gboolean homogeneous_only,
524 gint *requested_rows)
526 GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
528 if (GTK_IS_TOOL_PALETTE (parent))
529 _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
531 _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
535 gtk_tool_item_group_size_request (GtkWidget *widget,
536 GtkRequisition *requisition)
538 const gint border_width = GTK_CONTAINER (widget)->border_width;
539 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
540 GtkToolItemGroupPrivate* priv = group->priv;
541 GtkOrientation orientation;
542 GtkRequisition item_size;
545 if (priv->children && gtk_tool_item_group_get_label_widget (group))
547 gtk_widget_size_request (priv->header, requisition);
548 gtk_widget_show (priv->header);
552 requisition->width = requisition->height = 0;
553 gtk_widget_hide (priv->header);
556 gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
558 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
560 if (GTK_ORIENTATION_VERTICAL == orientation)
561 requisition->width = MAX (requisition->width, item_size.width);
563 requisition->height = MAX (requisition->height, item_size.height * requested_rows);
565 requisition->width += border_width * 2;
566 requisition->height += border_width * 2;
570 gtk_tool_item_group_is_item_visible (GtkToolItemGroup *group,
571 GtkToolItemGroupChild *child)
573 GtkToolbarStyle style;
574 GtkOrientation orientation;
576 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
577 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
579 /* horizontal tool palettes with text style support only homogeneous items */
580 if (!child->homogeneous &&
581 GTK_ORIENTATION_HORIZONTAL == orientation &&
582 GTK_TOOLBAR_TEXT == style)
586 (GTK_WIDGET_VISIBLE (child->item)) &&
587 (GTK_ORIENTATION_VERTICAL == orientation ?
588 gtk_tool_item_get_visible_vertical (child->item) :
589 gtk_tool_item_get_visible_horizontal (child->item));
592 static inline unsigned
596 return (x + y - 1) / y;
600 gtk_tool_item_group_real_size_query (GtkWidget *widget,
601 GtkAllocation *allocation,
602 GtkRequisition *inquery)
604 const gint border_width = GTK_CONTAINER (widget)->border_width;
605 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
606 GtkToolItemGroupPrivate* priv = group->priv;
608 GtkRequisition item_size;
609 GtkAllocation item_area;
611 GtkOrientation orientation;
612 GtkToolbarStyle style;
616 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
617 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
619 /* figure out the size of homogeneous items */
620 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
622 if (GTK_ORIENTATION_VERTICAL == orientation)
623 item_size.width = MIN (item_size.width, allocation->width);
625 item_size.height = MIN (item_size.height, allocation->height);
628 item_area.height = 0;
630 /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
631 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
637 if (GTK_ORIENTATION_VERTICAL == orientation)
639 gboolean new_row = FALSE;
643 item_area.width = allocation->width - 2 * border_width;
644 n_columns = MAX (item_area.width / item_size.width, 1);
646 /* calculate required rows for n_columns columns */
647 for (it = priv->children; it != NULL; it = it->next)
649 GtkToolItemGroupChild *child = it->data;
651 if (!gtk_tool_item_group_is_item_visible (group, child))
654 if (new_row || child->new_row)
664 if (child->homogeneous)
667 if (col >= n_columns)
672 GtkRequisition req = {0, 0};
675 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
677 width = udiv (req.width, item_size.width);
685 if (col >= n_columns)
693 guint *row_min_width;
695 gboolean new_row = TRUE;
696 guint col = 0, min_col, max_col = 0, all_items = 0;
699 item_area.height = allocation->height - 2 * border_width;
700 n_rows = MAX (item_area.height / item_size.height, min_rows);
702 row_min_width = g_new0 (guint, n_rows);
704 /* calculate minimal and maximal required cols and minimal required rows */
705 for (it = priv->children; it != NULL; it = it->next)
707 GtkToolItemGroupChild *child = it->data;
709 if (!gtk_tool_item_group_is_item_visible (group, child))
712 if (new_row || child->new_row)
717 row_min_width[row] = 1;
723 if (child->homogeneous)
730 GtkRequisition req = {0, 0};
733 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
735 width = udiv (req.width, item_size.width);
740 row_min_width[row] = MAX (row_min_width[row], width);
743 max_col = MAX (max_col, col);
746 /* calculate minimal required cols */
747 min_col = udiv (all_items, n_rows);
749 for (i = 0; i <= row; i++)
751 min_col = MAX (min_col, row_min_width[i]);
754 /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
755 for (n_columns = min_col; n_columns < max_col; n_columns ++)
759 /* calculate required rows for n_columns columns */
760 for (it = priv->children; it != NULL; it = it->next)
762 GtkToolItemGroupChild *child = it->data;
764 if (!gtk_tool_item_group_is_item_visible (group, child))
767 if (new_row || child->new_row)
777 if (child->homogeneous)
780 if (col >= n_columns)
785 GtkRequisition req = {0, 0};
788 gtk_widget_size_request (GTK_WIDGET (child->item), &req);
790 width = udiv (req.width, item_size.width);
798 if (col >= n_columns)
808 item_area.width = item_size.width * n_columns;
809 item_area.height = item_size.height * n_rows;
815 /* figure out header widget size */
816 if (GTK_WIDGET_VISIBLE (priv->header))
818 GtkRequisition child_requisition;
820 gtk_widget_size_request (priv->header, &child_requisition);
822 if (GTK_ORIENTATION_VERTICAL == orientation)
823 inquery->height += child_requisition.height;
825 inquery->width += child_requisition.width;
828 /* report effective widget size */
829 inquery->width += item_area.width + 2 * border_width;
830 inquery->height += item_area.height + 2 * border_width;
834 gtk_tool_item_group_real_size_allocate (GtkWidget *widget,
835 GtkAllocation *allocation)
837 const gint border_width = GTK_CONTAINER (widget)->border_width;
838 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
839 GtkToolItemGroupPrivate* priv = group->priv;
840 GtkRequisition child_requisition;
841 GtkAllocation child_allocation;
843 GtkRequisition item_size;
844 GtkAllocation item_area;
846 GtkOrientation orientation;
847 GtkToolbarStyle style;
851 gint n_columns, n_rows = 1;
854 GtkTextDirection direction = gtk_widget_get_direction (widget);
856 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
857 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
860 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
862 child_allocation.x = border_width;
863 child_allocation.y = border_width;
865 /* place the header widget */
866 if (GTK_WIDGET_VISIBLE (priv->header))
868 gtk_widget_size_request (priv->header, &child_requisition);
870 if (GTK_ORIENTATION_VERTICAL == orientation)
872 child_allocation.width = allocation->width;
873 child_allocation.height = child_requisition.height;
877 child_allocation.width = child_requisition.width;
878 child_allocation.height = allocation->height;
880 if (GTK_TEXT_DIR_RTL == direction)
881 child_allocation.x = allocation->width - border_width - child_allocation.width;
884 gtk_widget_size_allocate (priv->header, &child_allocation);
886 if (GTK_ORIENTATION_VERTICAL == orientation)
887 child_allocation.y += child_allocation.height;
888 else if (GTK_TEXT_DIR_RTL != direction)
889 child_allocation.x += child_allocation.width;
891 child_allocation.x = border_width;
894 child_requisition.width = child_requisition.height = 0;
896 /* figure out the size of homogeneous items */
897 gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
899 /* figure out the available columns and size of item_area */
900 if (GTK_ORIENTATION_VERTICAL == orientation)
902 item_size.width = MIN (item_size.width, allocation->width);
904 item_area.width = allocation->width - 2 * border_width;
905 item_area.height = allocation->height - 2 * border_width - child_requisition.height;
907 n_columns = MAX (item_area.width / item_size.width, 1);
909 item_size.width = item_area.width / n_columns;
913 item_size.height = MIN (item_size.height, allocation->height);
915 item_area.width = allocation->width - 2 * border_width - child_requisition.width;
916 item_area.height = allocation->height - 2 * border_width;
918 n_columns = MAX (item_area.width / item_size.width, 1);
919 n_rows = MAX (item_area.height / item_size.height, min_rows);
921 item_size.height = item_area.height / n_rows;
924 item_area.x = child_allocation.x;
925 item_area.y = child_allocation.y;
927 /* when expanded or in transition, place the tool items in a grid like layout */
928 if (!priv->collapsed || !priv->animation || priv->animation_timeout)
930 gint col = 0, row = 0;
932 for (it = priv->children; it != NULL; it = it->next)
934 GtkToolItemGroupChild *child = it->data;
937 if (!gtk_tool_item_group_is_item_visible (group, child))
939 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
944 /* for non homogeneous widgets request the required size */
945 child_requisition.width = 0;
947 if (!child->homogeneous)
949 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
950 child_requisition.width = MIN (child_requisition.width, item_area.width);
953 /* select next row if at end of row */
954 if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
958 child_allocation.y += child_allocation.height;
963 /* calculate the position and size of the item */
964 if (!child->homogeneous)
970 col_width = udiv (child_requisition.width, item_size.width);
972 col_width = n_columns - col;
974 width = col_width * item_size.width;
976 if (GTK_TEXT_DIR_RTL == direction)
977 col_child = (n_columns - col - col_width);
981 child_allocation.x = item_area.x + col_child * item_size.width;
982 child_allocation.width = width;
987 (item_area.x + col_child * item_size.width +
988 (width - child_requisition.width) / 2);
989 child_allocation.width = child_requisition.width;
996 if (GTK_TEXT_DIR_RTL == direction)
997 col_child = (n_columns - col - 1);
999 child_allocation.x = item_area.x + col_child * item_size.width;
1000 child_allocation.width = item_size.width;
1005 child_allocation.height = item_size.height;
1007 gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1008 gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1011 child_allocation.y += item_size.height;
1014 /* or just hide all items, when collapsed */
1018 for (it = priv->children; it != NULL; it = it->next)
1020 GtkToolItemGroupChild *child = it->data;
1022 gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1028 gtk_tool_item_group_size_allocate (GtkWidget *widget,
1029 GtkAllocation *allocation)
1031 gtk_tool_item_group_real_size_allocate (widget, allocation);
1033 if (GTK_WIDGET_MAPPED (widget))
1034 gdk_window_invalidate_rect (widget->window, NULL, FALSE);
1038 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1042 GtkAdjustment *adjustment;
1045 /* Find this group's parent widget in the focused widget's anchestry. */
1046 for (p = widget; p; p = gtk_widget_get_parent (p))
1049 p = gtk_widget_get_parent (p);
1053 if (GTK_IS_TOOL_PALETTE (p))
1055 /* Check that the focused widgets is fully visible within
1056 * the group's parent widget and make it visible otherwise. */
1058 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1059 adjustment = gtk_tool_palette_get_vadjustment (GTK_TOOL_PALETTE (p));
1065 /* Handle vertical adjustment. */
1066 if (gtk_widget_translate_coordinates
1067 (widget, p, 0, 0, NULL, &y) && y < 0)
1069 y += adjustment->value;
1070 gtk_adjustment_clamp_page (adjustment, y, y + widget->allocation.height);
1072 else if (gtk_widget_translate_coordinates
1073 (widget, p, 0, widget->allocation.height, NULL, &y) &&
1074 y > p->allocation.height)
1076 y += adjustment->value;
1077 gtk_adjustment_clamp_page (adjustment, y - widget->allocation.height, y);
1081 adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1087 /* Handle horizontal adjustment. */
1088 if (gtk_widget_translate_coordinates
1089 (widget, p, 0, 0, &x, NULL) && x < 0)
1091 x += adjustment->value;
1092 gtk_adjustment_clamp_page (adjustment, x, x + widget->allocation.width);
1094 else if (gtk_widget_translate_coordinates
1095 (widget, p, widget->allocation.width, 0, &x, NULL) &&
1096 x > p->allocation.width)
1098 x += adjustment->value;
1099 gtk_adjustment_clamp_page (adjustment, x - widget->allocation.width, x);
1108 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1109 GtkWidget *toplevel)
1111 GtkToolItemGroupPrivate* priv = group->priv;
1113 if (toplevel != priv->toplevel)
1117 /* Disconnect focus tracking handler. */
1118 g_signal_handler_disconnect (priv->toplevel,
1119 priv->focus_set_id);
1121 priv->focus_set_id = 0;
1122 priv->toplevel = NULL;
1127 /* Install focus tracking handler. We connect to the window's
1128 * set-focus signal instead of connecting to the focus signal of
1131 * 1) Reduce the number of signal handlers used.
1132 * 2) Avoid special handling for group headers.
1133 * 3) Catch focus grabs not only for direct children,
1134 * but also for nested widgets.
1136 priv->focus_set_id =
1137 g_signal_connect (toplevel, "set-focus",
1138 G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1141 priv->toplevel = toplevel;
1147 gtk_tool_item_group_realize (GtkWidget *widget)
1149 GtkWidget *toplevel_window;
1150 const gint border_width = GTK_CONTAINER (widget)->border_width;
1151 gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1152 GdkWindowAttr attributes;
1153 GdkDisplay *display;
1155 attributes.window_type = GDK_WINDOW_CHILD;
1156 attributes.x = widget->allocation.x + border_width;
1157 attributes.y = widget->allocation.y + border_width;
1158 attributes.width = widget->allocation.width - border_width * 2;
1159 attributes.height = widget->allocation.height - border_width * 2;
1160 attributes.wclass = GDK_INPUT_OUTPUT;
1161 attributes.visual = gtk_widget_get_visual (widget);
1162 attributes.colormap = gtk_widget_get_colormap (widget);
1163 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1164 | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1165 | GDK_BUTTON_MOTION_MASK;
1167 widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1168 &attributes, attributes_mask);
1170 display = gdk_drawable_get_display (widget->window);
1172 if (gdk_display_supports_composite (display))
1173 gdk_window_set_composited (widget->window, TRUE);
1175 gdk_window_set_user_data (widget->window, widget);
1176 widget->style = gtk_style_attach (widget->style, widget->window);
1177 gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1178 GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1180 gtk_container_forall (GTK_CONTAINER (widget),
1181 (GtkCallback) gtk_widget_set_parent_window,
1184 gtk_widget_queue_resize_no_redraw (widget);
1186 toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1187 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1192 gtk_tool_item_group_unrealize (GtkWidget *widget)
1194 gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1195 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1199 gtk_tool_item_group_style_set (GtkWidget *widget,
1200 GtkStyle *previous_style)
1202 gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1203 GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1207 gtk_tool_item_group_add (GtkContainer *container,
1210 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1211 g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1213 gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1214 GTK_TOOL_ITEM (widget), -1);
1218 gtk_tool_item_group_remove (GtkContainer *container,
1221 GtkToolItemGroup *group;
1222 GtkToolItemGroupPrivate* priv;
1225 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1226 group = GTK_TOOL_ITEM_GROUP (container);
1229 for (it = priv->children; it != NULL; it = it->next)
1231 GtkToolItemGroupChild *child_info = it->data;
1233 if ((GtkWidget *)child_info->item == child)
1235 g_object_unref (child);
1236 gtk_widget_unparent (child);
1238 g_free (child_info);
1239 priv->children = g_list_delete_link (priv->children, it);
1241 gtk_widget_queue_resize (GTK_WIDGET (container));
1248 gtk_tool_item_group_forall (GtkContainer *container,
1250 GtkCallback callback,
1251 gpointer callback_data)
1253 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1254 GtkToolItemGroupPrivate* priv = group->priv;
1257 if (internals && priv->header)
1258 callback (priv->header, callback_data);
1260 children = priv->children;
1263 GtkToolItemGroupChild *child = children->data;
1264 children = children->next; /* store pointer before call to callback
1265 because the child pointer is invalid if the
1266 child->item is removed from the item group
1269 callback (GTK_WIDGET (child->item), callback_data);
1274 gtk_tool_item_group_child_type (GtkContainer *container)
1276 return GTK_TYPE_TOOL_ITEM;
1279 static GtkToolItemGroupChild *
1280 gtk_tool_item_group_get_child (GtkToolItemGroup *group,
1288 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1289 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1291 for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1293 GtkToolItemGroupChild *child = it->data;
1295 if (child->item == item)
1311 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1313 gboolean *homogeneous,
1318 GtkToolItemGroupChild *child;
1320 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1321 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1323 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1328 *expand = child->expand;
1331 *homogeneous = child->homogeneous;
1334 *fill = child->fill;
1337 *new_row = child->new_row;
1341 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1343 gboolean homogeneous,
1348 GtkToolItemGroupChild *child;
1349 gboolean changed = FALSE;
1351 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1352 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1354 child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1358 gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1360 if (child->homogeneous != homogeneous)
1362 child->homogeneous = homogeneous;
1364 gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1366 if (child->expand != expand)
1368 child->expand = expand;
1370 gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1372 if (child->fill != fill)
1376 gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1378 if (child->new_row != new_row)
1380 child->new_row = new_row;
1382 gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1385 gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1387 if (changed && GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
1388 gtk_widget_queue_resize (GTK_WIDGET (group));
1392 gtk_tool_item_group_set_child_property (GtkContainer *container,
1395 const GValue *value,
1398 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1399 GtkToolItem *item = GTK_TOOL_ITEM (child);
1400 gboolean homogeneous, expand, fill, new_row;
1402 if (prop_id != CHILD_PROP_POSITION)
1403 gtk_tool_item_group_get_item_packing (group, item,
1411 case CHILD_PROP_HOMOGENEOUS:
1412 gtk_tool_item_group_set_item_packing (group, item,
1413 g_value_get_boolean (value),
1419 case CHILD_PROP_EXPAND:
1420 gtk_tool_item_group_set_item_packing (group, item,
1422 g_value_get_boolean (value),
1427 case CHILD_PROP_FILL:
1428 gtk_tool_item_group_set_item_packing (group, item,
1431 g_value_get_boolean (value),
1435 case CHILD_PROP_NEW_ROW:
1436 gtk_tool_item_group_set_item_packing (group, item,
1440 g_value_get_boolean (value));
1443 case CHILD_PROP_POSITION:
1444 gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1448 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1454 gtk_tool_item_group_get_child_property (GtkContainer *container,
1460 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1461 GtkToolItem *item = GTK_TOOL_ITEM (child);
1462 gboolean homogeneous, expand, fill, new_row;
1464 if (prop_id != CHILD_PROP_POSITION)
1465 gtk_tool_item_group_get_item_packing (group, item,
1473 case CHILD_PROP_HOMOGENEOUS:
1474 g_value_set_boolean (value, homogeneous);
1477 case CHILD_PROP_EXPAND:
1478 g_value_set_boolean (value, expand);
1481 case CHILD_PROP_FILL:
1482 g_value_set_boolean (value, fill);
1485 case CHILD_PROP_NEW_ROW:
1486 g_value_set_boolean (value, new_row);
1489 case CHILD_PROP_POSITION:
1490 g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1494 GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1500 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1502 GObjectClass *oclass = G_OBJECT_CLASS (cls);
1503 GtkWidgetClass *wclass = GTK_WIDGET_CLASS (cls);
1504 GtkContainerClass *cclass = GTK_CONTAINER_CLASS (cls);
1506 oclass->set_property = gtk_tool_item_group_set_property;
1507 oclass->get_property = gtk_tool_item_group_get_property;
1508 oclass->finalize = gtk_tool_item_group_finalize;
1509 oclass->dispose = gtk_tool_item_group_dispose;
1511 wclass->size_request = gtk_tool_item_group_size_request;
1512 wclass->size_allocate = gtk_tool_item_group_size_allocate;
1513 wclass->realize = gtk_tool_item_group_realize;
1514 wclass->unrealize = gtk_tool_item_group_unrealize;
1515 wclass->style_set = gtk_tool_item_group_style_set;
1516 wclass->screen_changed = gtk_tool_item_group_screen_changed;
1518 cclass->add = gtk_tool_item_group_add;
1519 cclass->remove = gtk_tool_item_group_remove;
1520 cclass->forall = gtk_tool_item_group_forall;
1521 cclass->child_type = gtk_tool_item_group_child_type;
1522 cclass->set_child_property = gtk_tool_item_group_set_child_property;
1523 cclass->get_child_property = gtk_tool_item_group_get_child_property;
1525 g_object_class_install_property (oclass, PROP_LABEL,
1526 g_param_spec_string ("label",
1528 P_("The human-readable title of this item group"),
1530 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1531 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1533 g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1534 g_param_spec_object ("label-widget",
1536 P_("A widget to display in place of the usual label"),
1538 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1539 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1541 g_object_class_install_property (oclass, PROP_COLLAPSED,
1542 g_param_spec_boolean ("collapsed",
1544 P_("Wether the group has been collapsed and items are hidden"),
1546 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1547 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1549 g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1550 g_param_spec_enum ("ellipsize",
1552 P_("Ellipsize for item group headers"),
1553 PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1554 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1555 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1557 g_object_class_install_property (oclass, PROP_RELIEF,
1558 g_param_spec_enum ("header-relief",
1559 P_("Header Relief"),
1560 P_("Relief of the group header button"),
1561 GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1562 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1563 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1565 gtk_widget_class_install_style_property (wclass,
1566 g_param_spec_int ("expander-size",
1567 P_("Expander Size"),
1568 P_("Size of the expander arrow"),
1571 DEFAULT_EXPANDER_SIZE,
1572 G_PARAM_READABLE | G_PARAM_STATIC_NAME |
1573 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1575 gtk_widget_class_install_style_property (wclass,
1576 g_param_spec_int ("header-spacing",
1577 P_("Header Spacing"),
1578 P_("Spacing between expander arrow and caption"),
1581 DEFAULT_HEADER_SPACING,
1582 G_PARAM_READABLE | G_PARAM_STATIC_NAME |
1583 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1585 gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1586 g_param_spec_boolean ("homogeneous",
1588 P_("Whether the item should be the same size as other homogeneous items"),
1590 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1591 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1593 gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1594 g_param_spec_boolean ("expand",
1596 P_("Whether the item should receive extra space when the toolbar grows"),
1598 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1599 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1601 gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1602 g_param_spec_boolean ("fill",
1604 P_("Whether the item should fill the avaiable space"),
1606 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1607 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1609 gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1610 g_param_spec_boolean ("new-row",
1612 P_("Whether the item should start a new row"),
1614 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1615 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1617 gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1618 g_param_spec_int ("position",
1620 P_("Position of the item within this group"),
1624 G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1625 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1627 g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1631 * gtk_tool_item_group_new:
1632 * @label: the label of the new group.
1634 * Creates a new tool item group with label @label.
1636 * Returns: a new #GtkToolItemGroup.
1641 gtk_tool_item_group_new (const gchar *label)
1643 return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label",
1648 * gtk_tool_item_group_set_label:
1649 * @group: an #GtkToolItemGroup.
1650 * @label: the new human-readable label of of the group.
1652 * Sets the label of the tool item group. The label is displayed in the header
1658 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1661 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1664 gtk_tool_item_group_set_label_widget (group, NULL);
1667 GtkWidget *child = gtk_label_new (label);
1668 gtk_widget_show (child);
1670 gtk_tool_item_group_set_label_widget (group, child);
1673 g_object_notify (G_OBJECT (group), "label");
1677 * gtk_tool_item_group_set_label_widget:
1678 * @group: an #GtkToolItemGroup.
1679 * @widget: the new human-readable label of of the group.
1681 * Sets the label of the tool item group. The label is displayed in the header
1687 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1688 GtkWidget *label_widget)
1690 GtkToolItemGroupPrivate* priv;
1691 GtkWidget *alignment;
1693 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1694 g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1695 g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1699 if (priv->label_widget == label_widget)
1702 alignment = gtk_tool_item_group_get_alignment (group);
1704 if (priv->label_widget)
1706 gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1707 gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1712 gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1714 priv->label_widget = label_widget;
1716 if (GTK_WIDGET_VISIBLE (group))
1717 gtk_widget_queue_resize (GTK_WIDGET (group));
1719 /* Only show the header widget if the group has children: */
1720 if (label_widget && priv->children)
1721 gtk_widget_show (priv->header);
1723 gtk_widget_hide (priv->header);
1725 g_object_freeze_notify (G_OBJECT (group));
1726 g_object_notify (G_OBJECT (group), "label-widget");
1727 g_object_notify (G_OBJECT (group), "label");
1728 g_object_thaw_notify (G_OBJECT (group));
1732 * gtk_tool_item_group_header_relief:
1733 * @group: an #GtkToolItemGroup.
1734 * @style: The GtkReliefStyle
1736 * Set the button relief of the group header. See #gtk_button_set_relief for
1742 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1743 GtkReliefStyle style)
1745 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1746 gtk_button_set_relief (GTK_BUTTON(group->priv->header), style);
1750 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1753 g_source_get_current_time (group->priv->animation_timeout, &now);
1754 return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1758 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1760 GtkToolItemGroupPrivate* priv = group->priv;
1762 if (GTK_WIDGET_REALIZED (priv->header))
1764 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1767 /* Find the header button's arrow area... */
1768 area.x = alignment->allocation.x;
1769 area.y = alignment->allocation.y + (alignment->allocation.height - priv->expander_size) / 2;
1770 area.height = priv->expander_size;
1771 area.width = priv->expander_size;
1773 /* ... and invalidated it to get it animated. */
1774 gdk_window_invalidate_rect (priv->header->window, &area, TRUE);
1777 if (GTK_WIDGET_REALIZED (group))
1779 GtkWidget *widget = GTK_WIDGET (group);
1780 GtkWidget *parent = gtk_widget_get_parent (widget);
1781 int x, y, width, height;
1783 /* Find the tool item area button's arrow area... */
1784 width = widget->allocation.width;
1785 height = widget->allocation.height;
1787 gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1789 if (GTK_WIDGET_VISIBLE (priv->header))
1791 height -= priv->header->allocation.height;
1792 y += priv->header->allocation.height;
1795 /* ... and invalidated it to get it animated. */
1796 gtk_widget_queue_draw_area (parent, x, y, width, height);
1801 gtk_tool_item_group_animation_cb (gpointer data)
1803 GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1804 GtkToolItemGroupPrivate* priv = group->priv;
1805 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1808 GDK_THREADS_ENTER();
1810 /* Enque this early to reduce number of expose events. */
1811 gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1813 /* Figure out current style of the expander arrow. */
1814 if (priv->collapsed)
1816 if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1817 priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1819 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1823 if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1824 priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1826 priv->expander_style = GTK_EXPANDER_EXPANDED;
1829 gtk_tool_item_group_force_expose (group);
1831 /* Finish animation when done. */
1832 if (timestamp >= ANIMATION_DURATION)
1833 priv->animation_timeout = NULL;
1835 /* Ensure that all composited windows and child windows are repainted, before
1836 * the parent widget gets its expose-event. This is needed to avoid heavy
1837 * rendering artifacts. GTK+ should take care about this issue by itself I
1838 * guess, but currently it doesn't. Also I don't understand the parameters
1839 * of this issue well enough yet, to file a bug report.
1841 gdk_window_process_updates (GTK_WIDGET (group)->window, TRUE);
1843 retval = (priv->animation_timeout != NULL);
1845 GDK_THREADS_LEAVE();
1851 * gtk_tool_item_group_set_collapsed:
1852 * @group: an #GtkToolItemGroup.
1853 * @collapsed: whether the @group should be collapsed or expanded.
1855 * Sets whether the @group should be collapsed or expanded.
1860 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1864 GtkToolItemGroupPrivate* priv;
1866 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1870 parent = gtk_widget_get_parent (GTK_WIDGET (group));
1871 if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1872 _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1873 GTK_WIDGET (group));
1874 if (collapsed != priv->collapsed)
1876 if (priv->animation)
1880 g_get_current_time (&now);
1882 if (priv->animation_timeout)
1883 g_source_destroy (priv->animation_timeout);
1885 priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1886 priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1888 g_source_set_callback (priv->animation_timeout,
1889 gtk_tool_item_group_animation_cb,
1892 g_source_attach (priv->animation_timeout, NULL);
1896 priv->expander_style = GTK_EXPANDER_COLLAPSED;
1897 gtk_tool_item_group_force_expose (group);
1900 priv->collapsed = collapsed;
1901 g_object_notify (G_OBJECT (group), "collapsed");
1906 * gtk_tool_item_group_set_ellipsize:
1907 * @group: an #GtkToolItemGroup.
1908 * @ellipsize: the #PangoEllipsizeMode labels in @group should use.
1910 * Sets the ellipsization mode which should be used by labels in @group.
1915 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup *group,
1916 PangoEllipsizeMode ellipsize)
1918 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1920 if (ellipsize != group->priv->ellipsize)
1922 group->priv->ellipsize = ellipsize;
1923 gtk_tool_item_group_header_adjust_style (group);
1924 g_object_notify (G_OBJECT (group), "ellipsize");
1925 _gtk_tool_item_group_palette_reconfigured (group);
1930 * gtk_tool_item_group_get_label:
1931 * @group: an #GtkToolItemGroup.
1933 * Gets the label of @group.
1935 * Returns: the label of @group. The label is an internal string of @group and must not be modified.
1936 * Note that NULL is returned if a custom label has been set with gtk_tool_item_group_set_label_widget()
1940 G_CONST_RETURN gchar*
1941 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1943 GtkToolItemGroupPrivate *priv;
1945 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1949 if (GTK_IS_LABEL (priv->label_widget))
1950 return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1956 * gtk_tool_item_group_get_label_widget:
1957 * @group: an #GtkToolItemGroup.
1959 * Gets the label widget of @group.
1961 * Returns: the label widget of @group. See gtk_tool_item_group_set_label_widget()
1966 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
1968 GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1969 return gtk_bin_get_child (GTK_BIN (alignment));
1973 * gtk_tool_item_group_get_collapsed:
1974 * @group: an GtkToolItemGroup.
1976 * Gets whether @group is collapsed or expanded.
1978 * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded.
1983 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
1985 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
1986 return group->priv->collapsed;
1990 * gtk_tool_item_group_get_ellipsize:
1991 * @group: an #GtkToolItemGroup.
1993 * Gets the ellipsization mode of @group.
1995 * Returns: the #PangoEllipsizeMode of @group.
2000 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2002 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2003 return group->priv->ellipsize;
2007 * gtk_tool_item_group_get_header_relief:
2008 * @group: an #GtkToolItemGroup.
2010 * Gets the relief mode of the header button of @group.
2012 * Returns: the #GtkReliefStyle
2017 gtk_tool_item_group_get_header_relief (GtkToolItemGroup *group)
2019 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2020 return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2024 * gtk_tool_item_group_insert:
2025 * @group: an #GtkToolItemGroup.
2026 * @item: the #GtkToolItem to insert into @group.
2027 * @position: the position of @item in @group, starting with 0. The position -1 means end of list.
2029 * Inserts @item at @position in the list of children of @group.
2034 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2038 GtkWidget *parent, *child_widget;
2039 GtkToolItemGroupChild *child;
2041 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2042 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2043 g_return_if_fail (position >= -1);
2045 parent = gtk_widget_get_parent (GTK_WIDGET (group));
2047 child = g_new (GtkToolItemGroupChild, 1);
2048 child->item = g_object_ref_sink (item);
2049 child->homogeneous = TRUE;
2050 child->expand = FALSE;
2052 child->new_row = FALSE;
2054 group->priv->children = g_list_insert (group->priv->children, child, position);
2056 if (GTK_IS_TOOL_PALETTE (parent))
2057 _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2059 child_widget = gtk_bin_get_child (GTK_BIN (item));
2061 if (GTK_IS_BUTTON (child_widget))
2062 gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2064 gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2068 * gtk_tool_item_group_set_item_position:
2069 * @group: an #GtkToolItemGroup.
2070 * @item: the #GtkToolItem to move to a new position, should be a child of @group.
2071 * @position: the new position of @item in @group, starting with 0. The position -1 means end of list.
2073 * Sets the position of @item in the list of children of @group.
2078 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2084 GtkToolItemGroupChild *child;
2085 GtkToolItemGroupPrivate* priv;
2087 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2088 g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2090 g_return_if_fail (position >= -1);
2092 child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2095 g_return_if_fail (child != NULL);
2097 if (position == old_position)
2100 priv->children = g_list_delete_link (priv->children, link);
2101 priv->children = g_list_insert (priv->children, child, position);
2103 gtk_widget_child_notify (GTK_WIDGET (item), "position");
2104 if (GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
2105 gtk_widget_queue_resize (GTK_WIDGET (group));
2109 * gtk_tool_item_group_get_item_position:
2110 * @group: an #GtkToolItemGroup.
2111 * @item: a #GtkToolItem.
2113 * Gets the position of @item in @group as index.
2115 * Returns: the index of @item in @group or -1 if @item is no child of @group.
2120 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2125 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2126 g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2128 if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2135 * gtk_tool_item_group_get_n_items:
2136 * @group: an #GtkToolItemGroup.
2138 * Gets the number of tool items in group.
2140 * Returns: the number of tool items in group.
2145 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2147 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2149 return g_list_length (group->priv->children);
2153 * gtk_tool_item_group_get_nth_item:
2154 * @group: an #GtkToolItemGroup.
2155 * @index: the index.
2157 * Gets the tool item at index in group.
2159 * Returns: the #GtkToolItem at index.
2164 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2167 GtkToolItemGroupChild *child;
2169 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2171 child = g_list_nth_data (group->priv->children, index);
2173 return child != NULL ? child->item : NULL;
2177 * gtk_tool_item_group_get_drop_item:
2178 * @group: an #GtkToolItemGroup.
2179 * @x: the x position.
2180 * @y: the y position.
2182 * Gets the tool item at position (x, y).
2184 * Returns: the #GtkToolItem at position (x, y).
2189 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2193 GtkAllocation *allocation;
2194 GtkOrientation orientation;
2197 g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2199 allocation = >K_WIDGET (group)->allocation;
2200 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2202 g_return_val_if_fail (x >= 0 && x < allocation->width, NULL);
2203 g_return_val_if_fail (y >= 0 && y < allocation->height, NULL);
2205 for (it = group->priv->children; it != NULL; it = it->next)
2207 GtkToolItemGroupChild *child = it->data;
2208 GtkToolItem *item = child->item;
2211 if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2214 allocation = >K_WIDGET (item)->allocation;
2216 x0 = x - allocation->x;
2217 y0 = y - allocation->y;
2219 if (x0 >= 0 && x0 < allocation->width &&
2220 y0 >= 0 && y0 < allocation->height)
2228 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2229 GtkRequisition *item_size,
2230 gboolean homogeneous_only,
2231 gint *requested_rows)
2233 GtkRequisition child_requisition;
2236 gboolean new_row = TRUE;
2237 GtkOrientation orientation;
2238 GtkToolbarStyle style;
2240 g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2241 g_return_if_fail (NULL != item_size);
2243 orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2244 style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2246 item_size->width = item_size->height = 0;
2248 for (it = group->priv->children; it != NULL; it = it->next)
2250 GtkToolItemGroupChild *child = it->data;
2252 if (!gtk_tool_item_group_is_item_visible (group, child))
2255 if (child->new_row || new_row)
2261 if (!child->homogeneous && child->expand)
2264 gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
2266 if (!homogeneous_only || child->homogeneous)
2267 item_size->width = MAX (item_size->width, child_requisition.width);
2268 item_size->height = MAX (item_size->height, child_requisition.height);
2272 *requested_rows = rows;
2276 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2279 GtkWidget *widget = GTK_WIDGET (group);
2280 GtkToolItemGroupPrivate* priv = group->priv;
2282 gdk_cairo_set_source_pixmap (cr, widget->window,
2283 widget->allocation.x,
2284 widget->allocation.y);
2286 if (priv->animation_timeout)
2288 GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2289 cairo_pattern_t *mask;
2292 if (GTK_ORIENTATION_VERTICAL == orientation)
2293 v1 = widget->allocation.height;
2295 v1 = widget->allocation.width;
2299 if (!GTK_WIDGET_VISIBLE (priv->header))
2301 else if (GTK_ORIENTATION_VERTICAL == orientation)
2302 v0 = MAX (v0, priv->header->allocation.height);
2304 v0 = MAX (v0, priv->header->allocation.width);
2306 v1 = MIN (v0 + 256, v1);
2308 if (GTK_ORIENTATION_VERTICAL == orientation)
2310 v0 += widget->allocation.y;
2311 v1 += widget->allocation.y;
2313 mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2317 v0 += widget->allocation.x;
2318 v1 += widget->allocation.x;
2320 mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2323 cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2324 cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2325 cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2326 cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2327 cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2329 cairo_mask (cr, mask);
2330 cairo_pattern_destroy (mask);
2337 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2342 GtkRequisition requisition;
2343 GtkToolItemGroupPrivate* priv = group->priv;
2345 gtk_widget_size_request (GTK_WIDGET (group), &requisition);
2347 if (!priv->collapsed || priv->animation_timeout)
2349 GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2350 GtkRequisition inquery;
2353 allocation.width = limit;
2355 allocation.height = limit;
2357 gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2358 &allocation, &inquery);
2361 inquery.height -= requisition.height;
2363 inquery.width -= requisition.width;
2365 if (priv->animation_timeout && animation)
2367 gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2369 timestamp = MIN (timestamp, ANIMATION_DURATION);
2371 if (priv->collapsed)
2372 timestamp = ANIMATION_DURATION - timestamp;
2376 inquery.height *= timestamp;
2377 inquery.height /= ANIMATION_DURATION;
2381 inquery.width *= timestamp;
2382 inquery.width /= ANIMATION_DURATION;
2387 requisition.height += inquery.height;
2389 requisition.width += inquery.width;
2392 return (vertical ? requisition.height : requisition.width);
2396 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2399 return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2403 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2406 return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2410 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2413 if (GTK_IS_TOOL_ITEM (child))
2414 gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2419 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2421 gtk_container_foreach (GTK_CONTAINER (group),
2422 gtk_tool_palette_reconfigured_foreach_item,
2425 gtk_tool_item_group_header_adjust_style (group);