]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolitemgroup.c
Removed size_request from GtkToolItemGroup
[~andy/gtk] / gtk / gtktoolitemgroup.c
1 /* GtkToolPalette -- A tool palette with categories and DnD support
2  * Copyright (C) 2008  Openismus GmbH
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors:
19  *      Mathias Hasselmann
20  *      Jan Arne Petersen
21  */
22
23 #include "config.h"
24
25 #include "gtktoolpaletteprivate.h"
26
27 #include <gtk/gtk.h>
28 #include <math.h>
29 #include <string.h>
30 #include "gtkprivate.h"
31 #include "gtkintl.h"
32
33 #define ANIMATION_TIMEOUT        50
34 #define ANIMATION_DURATION      (ANIMATION_TIMEOUT * 4)
35 #define DEFAULT_ANIMATION_STATE  TRUE
36 #define DEFAULT_EXPANDER_SIZE    16
37 #define DEFAULT_HEADER_SPACING   2
38
39 #define DEFAULT_LABEL            ""
40 #define DEFAULT_COLLAPSED        FALSE
41 #define DEFAULT_ELLIPSIZE        PANGO_ELLIPSIZE_NONE
42
43 /**
44  * SECTION:gtktoolitemgroup
45  * @Short_description: A sub container used in a tool palette
46  * @Title: GtkToolItemGroup
47  *
48  * A #GtkToolItemGroup is used together with #GtkToolPalette to add
49  * #GtkToolItem<!-- -->s to a palette like container with different
50  * categories and drag and drop support.
51  *
52  * Since: 2.20
53  */
54
55 enum
56 {
57   PROP_NONE,
58   PROP_LABEL,
59   PROP_LABEL_WIDGET,
60   PROP_COLLAPSED,
61   PROP_ELLIPSIZE,
62   PROP_RELIEF
63 };
64
65 enum
66 {
67   CHILD_PROP_NONE,
68   CHILD_PROP_HOMOGENEOUS,
69   CHILD_PROP_EXPAND,
70   CHILD_PROP_FILL,
71   CHILD_PROP_NEW_ROW,
72   CHILD_PROP_POSITION,
73 };
74
75 typedef struct _GtkToolItemGroupChild GtkToolItemGroupChild;
76
77 struct _GtkToolItemGroupPrivate
78 {
79   GtkWidget         *header;
80   GtkWidget         *label_widget;
81
82   GList             *children;
83
84   gboolean           animation;
85   gint64             animation_start;
86   GSource           *animation_timeout;
87   GtkExpanderStyle   expander_style;
88   gint               expander_size;
89   gint               header_spacing;
90   PangoEllipsizeMode ellipsize;
91
92   gulong             focus_set_id;
93   GtkWidget         *toplevel;
94
95   GtkSettings       *settings;
96   gulong             settings_connection;
97
98   guint              collapsed : 1;
99 };
100
101 struct _GtkToolItemGroupChild
102 {
103   GtkToolItem *item;
104
105   guint        homogeneous : 1;
106   guint        expand : 1;
107   guint        fill : 1;
108   guint        new_row : 1;
109 };
110
111 static void gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface);
112
113 G_DEFINE_TYPE_WITH_CODE (GtkToolItemGroup, gtk_tool_item_group, GTK_TYPE_CONTAINER,
114 G_IMPLEMENT_INTERFACE (GTK_TYPE_TOOL_SHELL, gtk_tool_item_group_tool_shell_init));
115
116 static GtkWidget*
117 gtk_tool_item_group_get_alignment (GtkToolItemGroup *group)
118 {
119   return gtk_bin_get_child (GTK_BIN (group->priv->header));
120 }
121
122 static GtkOrientation
123 gtk_tool_item_group_get_orientation (GtkToolShell *shell)
124 {
125   GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
126
127   if (GTK_IS_TOOL_PALETTE (parent))
128     return gtk_orientable_get_orientation (GTK_ORIENTABLE (parent));
129
130   return GTK_ORIENTATION_VERTICAL;
131 }
132
133 static GtkToolbarStyle
134 gtk_tool_item_group_get_style (GtkToolShell *shell)
135 {
136   GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
137
138   if (GTK_IS_TOOL_PALETTE (parent))
139     return gtk_tool_palette_get_style (GTK_TOOL_PALETTE (parent));
140
141   return GTK_TOOLBAR_ICONS;
142 }
143
144 static GtkIconSize
145 gtk_tool_item_group_get_icon_size (GtkToolShell *shell)
146 {
147   GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
148
149   if (GTK_IS_TOOL_PALETTE (parent))
150     return gtk_tool_palette_get_icon_size (GTK_TOOL_PALETTE (parent));
151
152   return GTK_ICON_SIZE_SMALL_TOOLBAR;
153 }
154
155 static PangoEllipsizeMode
156 gtk_tool_item_group_get_ellipsize_mode (GtkToolShell *shell)
157 {
158   return GTK_TOOL_ITEM_GROUP (shell)->priv->ellipsize;
159 }
160
161 static gfloat
162 gtk_tool_item_group_get_text_alignment (GtkToolShell *shell)
163 {
164   if (GTK_TOOLBAR_TEXT == gtk_tool_item_group_get_style (shell) ||
165       GTK_TOOLBAR_BOTH_HORIZ == gtk_tool_item_group_get_style (shell))
166     return 0.0;
167
168   return 0.5;
169 }
170
171 static GtkOrientation
172 gtk_tool_item_group_get_text_orientation (GtkToolShell *shell)
173 {
174   return GTK_ORIENTATION_HORIZONTAL;
175 }
176
177 static GtkSizeGroup *
178 gtk_tool_item_group_get_text_size_group (GtkToolShell *shell)
179 {
180   GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (shell));
181
182   if (GTK_IS_TOOL_PALETTE (parent))
183     return _gtk_tool_palette_get_size_group (GTK_TOOL_PALETTE (parent));
184
185   return NULL;
186 }
187
188 static void
189 animation_change_notify (GtkToolItemGroup *group)
190 {
191   GtkSettings *settings = group->priv->settings;
192   gboolean animation;
193
194   if (settings)
195     g_object_get (settings,
196                   "gtk-enable-animations", &animation,
197                   NULL);
198   else
199     animation = DEFAULT_ANIMATION_STATE;
200
201   group->priv->animation = animation;
202 }
203
204 static void
205 gtk_tool_item_group_settings_change_notify (GtkSettings      *settings,
206                                             const GParamSpec *pspec,
207                                             GtkToolItemGroup *group)
208 {
209   if (strcmp (pspec->name, "gtk-enable-animations") == 0)
210     animation_change_notify (group);
211 }
212
213 static void
214 gtk_tool_item_group_screen_changed (GtkWidget *widget,
215                                     GdkScreen *previous_screen)
216 {
217   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
218   GtkToolItemGroupPrivate* priv = group->priv;
219   GtkSettings *old_settings = priv->settings;
220   GtkSettings *settings;
221
222   if (gtk_widget_has_screen (GTK_WIDGET (group)))
223     settings = gtk_widget_get_settings (GTK_WIDGET (group));
224   else
225     settings = NULL;
226
227   if (settings == old_settings)
228     return;
229
230   if (old_settings)
231   {
232     g_signal_handler_disconnect (old_settings, priv->settings_connection);
233     g_object_unref (old_settings);
234   }
235
236   if (settings)
237   {
238     priv->settings_connection =
239       g_signal_connect (settings, "notify",
240                         G_CALLBACK (gtk_tool_item_group_settings_change_notify),
241                         group);
242     priv->settings = g_object_ref (settings);
243   }
244   else
245     priv->settings = NULL;
246
247   animation_change_notify (group);
248 }
249
250 static void
251 gtk_tool_item_group_tool_shell_init (GtkToolShellIface *iface)
252 {
253   iface->get_icon_size = gtk_tool_item_group_get_icon_size;
254   iface->get_orientation = gtk_tool_item_group_get_orientation;
255   iface->get_style = gtk_tool_item_group_get_style;
256   iface->get_text_alignment = gtk_tool_item_group_get_text_alignment;
257   iface->get_text_orientation = gtk_tool_item_group_get_text_orientation;
258   iface->get_text_size_group = gtk_tool_item_group_get_text_size_group;
259   iface->get_ellipsize_mode = gtk_tool_item_group_get_ellipsize_mode;
260 }
261
262 static gboolean
263 gtk_tool_item_group_header_draw_cb (GtkWidget *widget,
264                                     cairo_t   *cr,
265                                     gpointer   data)
266 {
267   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
268   GtkToolItemGroupPrivate* priv = group->priv;
269   GtkExpanderStyle expander_style;
270   GtkOrientation orientation;
271   gint x, y, width, height;
272   GtkTextDirection direction;
273
274   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
275   expander_style = priv->expander_style;
276   direction = gtk_widget_get_direction (widget);
277   width = gtk_widget_get_allocated_width (widget);
278   height = gtk_widget_get_allocated_height (widget);
279
280   if (GTK_ORIENTATION_VERTICAL == orientation)
281     {
282       if (GTK_TEXT_DIR_RTL == direction)
283         x = width - priv->expander_size / 2;
284       else
285         x = priv->expander_size / 2;
286
287       y = height / 2;
288     }
289   else
290     {
291       x = width / 2;
292       y = priv->expander_size / 2;
293
294       /* Unfortunatly gtk_paint_expander() doesn't support rotated drawing
295        * modes. Luckily the following shady arithmetics produce the desired
296        * result. */
297       expander_style = GTK_EXPANDER_EXPANDED - expander_style;
298     }
299
300   gtk_paint_expander (gtk_widget_get_style (widget),
301                       cr,
302                       gtk_widget_get_state (priv->header),
303                       GTK_WIDGET (group),
304                       "tool-palette-header", x, y,
305                       expander_style);
306
307   return FALSE;
308 }
309
310 static void
311 gtk_tool_item_group_header_clicked_cb (GtkButton *button,
312                                        gpointer   data)
313 {
314   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
315   GtkToolItemGroupPrivate* priv = group->priv;
316   GtkWidget *parent = gtk_widget_get_parent (data);
317
318   if (priv->collapsed ||
319       !GTK_IS_TOOL_PALETTE (parent) ||
320       !gtk_tool_palette_get_exclusive (GTK_TOOL_PALETTE (parent), data))
321     gtk_tool_item_group_set_collapsed (group, !priv->collapsed);
322 }
323
324 static void
325 gtk_tool_item_group_header_adjust_style (GtkToolItemGroup *group)
326 {
327   GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
328   GtkWidget *label_widget = gtk_bin_get_child (GTK_BIN (alignment));
329   GtkWidget *widget = GTK_WIDGET (group);
330   GtkToolItemGroupPrivate* priv = group->priv;
331   gint dx = 0, dy = 0;
332   GtkTextDirection direction = gtk_widget_get_direction (widget);
333
334   gtk_widget_style_get (widget,
335                         "header-spacing", &(priv->header_spacing),
336                         "expander-size", &(priv->expander_size),
337                         NULL);
338   
339   gtk_widget_set_size_request (alignment, -1, priv->expander_size);
340
341   switch (gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group)))
342     {
343       case GTK_ORIENTATION_HORIZONTAL:
344         dy = priv->header_spacing + priv->expander_size;
345
346         if (GTK_IS_LABEL (label_widget))
347           {
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);
351             else
352               gtk_label_set_angle (GTK_LABEL (label_widget), 90);
353           }
354        break;
355
356       case GTK_ORIENTATION_VERTICAL:
357         dx = priv->header_spacing + priv->expander_size;
358
359         if (GTK_IS_LABEL (label_widget))
360           {
361             gtk_label_set_ellipsize (GTK_LABEL (label_widget), priv->ellipsize);
362             gtk_label_set_angle (GTK_LABEL (label_widget), 0);
363           }
364         break;
365     }
366
367   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), dy, 0, dx, 0);
368 }
369
370 static void
371 gtk_tool_item_group_init (GtkToolItemGroup *group)
372 {
373   GtkWidget *alignment;
374   GtkToolItemGroupPrivate* priv;
375
376   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (group), FALSE);
377
378   group->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (group,
379                                              GTK_TYPE_TOOL_ITEM_GROUP,
380                                              GtkToolItemGroupPrivate);
381
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;
386
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);
392
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 ();
397
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));
402
403   gtk_tool_item_group_header_adjust_style (group);
404
405   g_signal_connect_after (alignment, "draw",
406                           G_CALLBACK (gtk_tool_item_group_header_draw_cb),
407                           group);
408
409   g_signal_connect (priv->header, "clicked",
410                     G_CALLBACK (gtk_tool_item_group_header_clicked_cb),
411                     group);
412 }
413
414 static void
415 gtk_tool_item_group_set_property (GObject      *object,
416                                   guint         prop_id,
417                                   const GValue *value,
418                                   GParamSpec   *pspec)
419 {
420   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
421
422   switch (prop_id)
423     {
424       case PROP_LABEL:
425         gtk_tool_item_group_set_label (group, g_value_get_string (value));
426         break;
427
428       case PROP_LABEL_WIDGET:
429         gtk_tool_item_group_set_label_widget (group, g_value_get_object (value));
430         break;
431
432       case PROP_COLLAPSED:
433         gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
434         break;
435
436       case PROP_ELLIPSIZE:
437         gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
438         break;
439
440       case PROP_RELIEF:
441         gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
442         break;
443
444       default:
445         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
446         break;
447     }
448 }
449
450 static void
451 gtk_tool_item_group_get_property (GObject    *object,
452                                   guint       prop_id,
453                                   GValue     *value,
454                                   GParamSpec *pspec)
455 {
456   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
457
458   switch (prop_id)
459     {
460       case PROP_LABEL:
461         g_value_set_string (value, gtk_tool_item_group_get_label (group));
462         break;
463
464       case PROP_LABEL_WIDGET:
465         g_value_set_object (value,
466                             gtk_tool_item_group_get_label_widget (group));
467         break;
468
469       case PROP_COLLAPSED:
470         g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
471         break;
472
473       case PROP_ELLIPSIZE:
474         g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
475         break;
476
477       case PROP_RELIEF:
478         g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
479         break;
480
481       default:
482         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
483         break;
484     }
485 }
486
487 static void
488 gtk_tool_item_group_finalize (GObject *object)
489 {
490   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
491
492   if (group->priv->children)
493     {
494       g_list_free (group->priv->children);
495       group->priv->children = NULL;
496     }
497
498   G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
499 }
500
501 static void
502 gtk_tool_item_group_dispose (GObject *object)
503 {
504   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
505   GtkToolItemGroupPrivate* priv = group->priv;
506
507   if (priv->toplevel)
508     {
509       /* disconnect focus tracking handler */
510       g_signal_handler_disconnect (priv->toplevel,
511                                    priv->focus_set_id);
512
513       priv->focus_set_id = 0;
514       priv->toplevel = NULL;
515     }
516
517   G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
518 }
519
520 static void
521 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
522                                    GtkRequisition   *item_size,
523                                    gboolean          homogeneous_only,
524                                    gint             *requested_rows)
525 {
526   GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
527
528   if (GTK_IS_TOOL_PALETTE (parent))
529     _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
530   else
531     _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
532 }
533
534 static void
535 gtk_tool_item_group_size_request (GtkWidget      *widget,
536                                   GtkRequisition *requisition)
537 {
538   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
539   GtkToolItemGroupPrivate* priv = group->priv;
540   GtkOrientation orientation;
541   GtkRequisition item_size;
542   gint requested_rows;
543   guint border_width;
544
545   if (priv->children && gtk_tool_item_group_get_label_widget (group))
546     {
547       gtk_widget_get_preferred_size (priv->header,
548                                      requisition, NULL);
549       gtk_widget_show (priv->header);
550     }
551   else
552     {
553       requisition->width = requisition->height = 0;
554       gtk_widget_hide (priv->header);
555     }
556
557   gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
558
559   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
560
561   if (GTK_ORIENTATION_VERTICAL == orientation)
562     requisition->width = MAX (requisition->width, item_size.width);
563   else
564     requisition->height = MAX (requisition->height, item_size.height * requested_rows);
565
566   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
567   requisition->width += border_width * 2;
568   requisition->height += border_width * 2;
569 }
570
571 static void
572 gtk_tool_item_group_get_preferred_width (GtkWidget *widget,
573                                          gint      *minimum,
574                                          gint      *natural)
575 {
576   GtkRequisition requisition;
577
578   gtk_tool_item_group_size_request (widget, &requisition);
579
580   *minimum = *natural = requisition.width;
581 }
582
583 static void
584 gtk_tool_item_group_get_preferred_height (GtkWidget *widget,
585                                           gint      *minimum,
586                                           gint      *natural)
587 {
588   GtkRequisition requisition;
589
590   gtk_tool_item_group_size_request (widget, &requisition);
591
592   *minimum = *natural = requisition.height;
593 }
594
595
596 static gboolean
597 gtk_tool_item_group_is_item_visible (GtkToolItemGroup      *group,
598                                      GtkToolItemGroupChild *child)
599 {
600   GtkToolbarStyle style;
601   GtkOrientation orientation;
602
603   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
604   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
605
606   /* horizontal tool palettes with text style support only homogeneous items */
607   if (!child->homogeneous &&
608       GTK_ORIENTATION_HORIZONTAL == orientation &&
609       GTK_TOOLBAR_TEXT == style)
610     return FALSE;
611
612   return
613     (gtk_widget_get_visible (GTK_WIDGET (child->item))) &&
614     (GTK_ORIENTATION_VERTICAL == orientation ?
615      gtk_tool_item_get_visible_vertical (child->item) :
616      gtk_tool_item_get_visible_horizontal (child->item));
617 }
618
619 static inline unsigned
620 udiv (unsigned x,
621       unsigned y)
622 {
623   return (x + y - 1) / y;
624 }
625
626 static void
627 gtk_tool_item_group_real_size_query (GtkWidget      *widget,
628                                      GtkAllocation  *allocation,
629                                      GtkRequisition *inquery)
630 {
631   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
632   GtkToolItemGroupPrivate* priv = group->priv;
633
634   GtkRequisition item_size;
635   GtkAllocation item_area;
636
637   GtkOrientation orientation;
638   GtkToolbarStyle style;
639
640   gint min_rows;
641   guint border_width;
642
643   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
644   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
645   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
646
647   /* figure out the size of homogeneous items */
648   gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
649
650   if (GTK_ORIENTATION_VERTICAL == orientation)
651     item_size.width = MIN (item_size.width, allocation->width);
652   else
653     item_size.height = MIN (item_size.height, allocation->height);
654
655   item_size.width  = MAX (item_size.width, 1);
656   item_size.height = MAX (item_size.height, 1);
657
658   item_area.width = 0;
659   item_area.height = 0;
660
661   /* figure out the required columns (n_columns) and rows (n_rows) to place all items */
662   if (!priv->collapsed || !priv->animation || priv->animation_timeout)
663     {
664       guint n_columns;
665       gint n_rows;
666       GList *it;
667
668       if (GTK_ORIENTATION_VERTICAL == orientation)
669         {
670           gboolean new_row = FALSE;
671           gint row = -1;
672           guint col = 0;
673
674           item_area.width = allocation->width - 2 * border_width;
675           n_columns = MAX (item_area.width / item_size.width, 1);
676
677           /* calculate required rows for n_columns columns */
678           for (it = priv->children; it != NULL; it = it->next)
679             {
680               GtkToolItemGroupChild *child = it->data;
681
682               if (!gtk_tool_item_group_is_item_visible (group, child))
683                 continue;
684
685               if (new_row || child->new_row)
686                 {
687                   new_row = FALSE;
688                   row++;
689                   col = 0;
690                 }
691
692               if (child->expand)
693                 new_row = TRUE;
694
695               if (child->homogeneous)
696                 {
697                   col++;
698                   if (col >= n_columns)
699                     new_row = TRUE;
700                 }
701               else
702                 {
703                   GtkRequisition req = {0, 0};
704                   guint width;
705
706                   gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
707                                                  &req, NULL);
708
709                   width = udiv (req.width, item_size.width);
710                   col += width;
711
712                   if (col > n_columns)
713                     row++;
714
715                   col = width;
716
717                   if (col >= n_columns)
718                     new_row = TRUE;
719                 }
720             }
721           n_rows = row + 2;
722         }
723       else
724         {
725           guint *row_min_width;
726           gint row = -1;
727           gboolean new_row = TRUE;
728           guint col = 0, min_col, max_col = 0, all_items = 0;
729           gint i;
730
731           item_area.height = allocation->height - 2 * border_width;
732           n_rows = MAX (item_area.height / item_size.height, min_rows);
733
734           row_min_width = g_new0 (guint, n_rows);
735
736           /* calculate minimal and maximal required cols and minimal required rows */
737           for (it = priv->children; it != NULL; it = it->next)
738             {
739               GtkToolItemGroupChild *child = it->data;
740
741               if (!gtk_tool_item_group_is_item_visible (group, child))
742                 continue;
743
744               if (new_row || child->new_row)
745                 {
746                   new_row = FALSE;
747                   row++;
748                   col = 0;
749                   row_min_width[row] = 1;
750                 }
751
752               if (child->expand)
753                 new_row = TRUE;
754
755               if (child->homogeneous)
756                 {
757                   col++;
758                   all_items++;
759                 }
760               else
761                 {
762                   GtkRequisition req = {0, 0};
763                   guint width;
764
765                   gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
766                                                  &req, NULL);
767
768                   width = udiv (req.width, item_size.width);
769
770                   col += width;
771                   all_items += width;
772
773                   row_min_width[row] = MAX (row_min_width[row], width);
774                 }
775
776               max_col = MAX (max_col, col);
777             }
778
779           /* calculate minimal required cols */
780           min_col = udiv (all_items, n_rows);
781
782           for (i = 0; i <= row; i++)
783             {
784               min_col = MAX (min_col, row_min_width[i]);
785             }
786
787           /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
788           for (n_columns = min_col; n_columns < max_col; n_columns ++)
789             {
790               new_row = TRUE;
791               row = -1;
792               /* calculate required rows for n_columns columns */
793               for (it = priv->children; it != NULL; it = it->next)
794                 {
795                   GtkToolItemGroupChild *child = it->data;
796
797                   if (!gtk_tool_item_group_is_item_visible (group, child))
798                     continue;
799
800                   if (new_row || child->new_row)
801                     {
802                       new_row = FALSE;
803                       row++;
804                       col = 0;
805                     }
806
807                   if (child->expand)
808                     new_row = TRUE;
809
810                   if (child->homogeneous)
811                     {
812                       col++;
813                       if (col >= n_columns)
814                         new_row = TRUE;
815                     }
816                   else
817                     {
818                       GtkRequisition req = {0, 0};
819                       guint width;
820
821                       gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
822                                                      &req, NULL);
823
824                       width = udiv (req.width, item_size.width);
825                       col += width;
826
827                       if (col > n_columns)
828                         row++;
829
830                       col = width;
831
832                       if (col >= n_columns)
833                         new_row = TRUE;
834                     }
835                 }
836
837               if (row < n_rows)
838                 break;
839             }
840         }
841
842       item_area.width = item_size.width * n_columns;
843       item_area.height = item_size.height * n_rows;
844     }
845
846   inquery->width = 0;
847   inquery->height = 0;
848
849   /* figure out header widget size */
850   if (gtk_widget_get_visible (priv->header))
851     {
852       GtkRequisition child_requisition;
853
854       gtk_widget_get_preferred_size (priv->header,
855                                      &child_requisition, NULL);
856
857       if (GTK_ORIENTATION_VERTICAL == orientation)
858         inquery->height += child_requisition.height;
859       else
860         inquery->width += child_requisition.width;
861     }
862
863   /* report effective widget size */
864   inquery->width += item_area.width + 2 * border_width;
865   inquery->height += item_area.height + 2 * border_width;
866 }
867
868 static void
869 gtk_tool_item_group_real_size_allocate (GtkWidget     *widget,
870                                         GtkAllocation *allocation)
871 {
872   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
873   GtkToolItemGroupPrivate* priv = group->priv;
874   GtkRequisition child_requisition;
875   GtkAllocation child_allocation;
876
877   GtkRequisition item_size;
878   GtkAllocation item_area;
879
880   GtkOrientation orientation;
881   GtkToolbarStyle style;
882
883   GList *it;
884
885   gint n_columns, n_rows = 1;
886   gint min_rows;
887   guint border_width;
888
889   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
890
891   GtkTextDirection direction = gtk_widget_get_direction (widget);
892
893   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
894   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
895
896   /* chain up */
897   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
898
899   child_allocation.x = border_width;
900   child_allocation.y = border_width;
901
902   /* place the header widget */
903   if (gtk_widget_get_visible (priv->header))
904     {
905       gtk_widget_get_preferred_size (priv->header,
906                                      &child_requisition, NULL);
907
908       if (GTK_ORIENTATION_VERTICAL == orientation)
909         {
910           child_allocation.width = allocation->width;
911           child_allocation.height = child_requisition.height;
912         }
913       else
914         {
915           child_allocation.width = child_requisition.width;
916           child_allocation.height = allocation->height;
917
918           if (GTK_TEXT_DIR_RTL == direction)
919             child_allocation.x = allocation->width - border_width - child_allocation.width;
920         }
921
922       gtk_widget_size_allocate (priv->header, &child_allocation);
923
924       if (GTK_ORIENTATION_VERTICAL == orientation)
925         child_allocation.y += child_allocation.height;
926       else if (GTK_TEXT_DIR_RTL != direction)
927         child_allocation.x += child_allocation.width;
928       else
929         child_allocation.x = border_width;
930     }
931   else
932     child_requisition.width = child_requisition.height = 0;
933
934   /* figure out the size of homogeneous items */
935   gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
936
937   item_size.width  = MAX (item_size.width, 1);
938   item_size.height = MAX (item_size.height, 1);
939
940   /* figure out the available columns and size of item_area */
941   if (GTK_ORIENTATION_VERTICAL == orientation)
942     {
943       item_size.width = MIN (item_size.width, allocation->width);
944
945       item_area.width = allocation->width - 2 * border_width;
946       item_area.height = allocation->height - 2 * border_width - child_requisition.height;
947
948       n_columns = MAX (item_area.width / item_size.width, 1);
949
950       item_size.width = item_area.width / n_columns;
951     }
952   else
953     {
954       item_size.height = MIN (item_size.height, allocation->height);
955
956       item_area.width = allocation->width - 2 * border_width - child_requisition.width;
957       item_area.height = allocation->height - 2 * border_width;
958
959       n_columns = MAX (item_area.width / item_size.width, 1);
960       n_rows = MAX (item_area.height / item_size.height, min_rows);
961
962       item_size.height = item_area.height / n_rows;
963     }
964
965   item_area.x = child_allocation.x;
966   item_area.y = child_allocation.y;
967
968   /* when expanded or in transition, place the tool items in a grid like layout */
969   if (!priv->collapsed || !priv->animation || priv->animation_timeout)
970     {
971       gint col = 0, row = 0;
972
973       for (it = priv->children; it != NULL; it = it->next)
974         {
975           GtkToolItemGroupChild *child = it->data;
976           gint col_child;
977
978           if (!gtk_tool_item_group_is_item_visible (group, child))
979             {
980               gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
981
982               continue;
983             }
984
985           /* for non homogeneous widgets request the required size */
986           child_requisition.width = 0;
987
988           if (!child->homogeneous)
989             {
990               gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
991                                              &child_requisition, NULL);
992               child_requisition.width = MIN (child_requisition.width, item_area.width);
993             }
994
995           /* select next row if at end of row */
996           if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
997             {
998               row++;
999               col = 0;
1000               child_allocation.y += child_allocation.height;
1001             }
1002
1003           col_child = col;
1004
1005           /* calculate the position and size of the item */
1006           if (!child->homogeneous)
1007             {
1008               gint col_width;
1009               gint width;
1010
1011               if (!child->expand)
1012                 col_width = udiv (child_requisition.width, item_size.width);
1013               else
1014                 col_width = n_columns - col;
1015
1016               width = col_width * item_size.width;
1017
1018               if (GTK_TEXT_DIR_RTL == direction)
1019                 col_child = (n_columns - col - col_width);
1020
1021               if (child->fill)
1022                 {
1023                   child_allocation.x = item_area.x + col_child * item_size.width;
1024                   child_allocation.width = width;
1025                 }
1026               else
1027                 {
1028                   child_allocation.x =
1029                     (item_area.x + col_child * item_size.width +
1030                     (width - child_requisition.width) / 2);
1031                   child_allocation.width = child_requisition.width;
1032                 }
1033
1034               col += col_width;
1035             }
1036           else
1037             {
1038               if (GTK_TEXT_DIR_RTL == direction)
1039                 col_child = (n_columns - col - 1);
1040
1041               child_allocation.x = item_area.x + col_child * item_size.width;
1042               child_allocation.width = item_size.width;
1043
1044               col++;
1045             }
1046
1047           child_allocation.height = item_size.height;
1048
1049           gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1050           gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1051         }
1052
1053       child_allocation.y += item_size.height;
1054     }
1055
1056   /* or just hide all items, when collapsed */
1057
1058   else
1059     {
1060       for (it = priv->children; it != NULL; it = it->next)
1061         {
1062           GtkToolItemGroupChild *child = it->data;
1063
1064           gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1065         }
1066     }
1067 }
1068
1069 static void
1070 gtk_tool_item_group_size_allocate (GtkWidget     *widget,
1071                                    GtkAllocation *allocation)
1072 {
1073   gtk_tool_item_group_real_size_allocate (widget, allocation);
1074
1075   if (gtk_widget_get_mapped (widget))
1076     gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
1077 }
1078
1079 static void
1080 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1081                                   GtkWidget *widget,
1082                                   gpointer   user_data)
1083 {
1084   GtkAdjustment *adjustment;
1085   GtkAllocation allocation, p_allocation;
1086   GtkWidget *p;
1087
1088   /* Find this group's parent widget in the focused widget's anchestry. */
1089   for (p = widget; p; p = gtk_widget_get_parent (p))
1090     if (p == user_data)
1091       {
1092         p = gtk_widget_get_parent (p);
1093         break;
1094       }
1095
1096   if (GTK_IS_TOOL_PALETTE (p))
1097     {
1098       /* Check that the focused widgets is fully visible within
1099        * the group's parent widget and make it visible otherwise. */
1100
1101       adjustment = gtk_scrollable_get_vadjustment (GTK_SCROLLABLE (p));
1102
1103       if (adjustment)
1104         {
1105           int y;
1106
1107           gtk_widget_get_allocation (widget, &allocation);
1108           gtk_widget_get_allocation (p, &p_allocation);
1109
1110           /* Handle vertical adjustment. */
1111           if (gtk_widget_translate_coordinates
1112                 (widget, p, 0, 0, NULL, &y) && y < 0)
1113             {
1114               y += adjustment->value;
1115               gtk_adjustment_clamp_page (adjustment, y, y + allocation.height);
1116             }
1117           else if (gtk_widget_translate_coordinates (widget, p, 0, allocation.height, NULL, &y) &&
1118                    y > p_allocation.height)
1119             {
1120               y += adjustment->value;
1121               gtk_adjustment_clamp_page (adjustment, y - allocation.height, y);
1122             }
1123         }
1124
1125       adjustment = gtk_scrollable_get_hadjustment (GTK_SCROLLABLE (p));
1126
1127       if (adjustment)
1128         {
1129           int x;
1130
1131           gtk_widget_get_allocation (widget, &allocation);
1132           gtk_widget_get_allocation (p, &p_allocation);
1133
1134           /* Handle horizontal adjustment. */
1135           if (gtk_widget_translate_coordinates
1136                 (widget, p, 0, 0, &x, NULL) && x < 0)
1137             {
1138               x += adjustment->value;
1139               gtk_adjustment_clamp_page (adjustment, x, x + allocation.width);
1140             }
1141           else if (gtk_widget_translate_coordinates (widget, p, allocation.width, 0, &x, NULL) &&
1142                    x > p_allocation.width)
1143             {
1144               x += adjustment->value;
1145               gtk_adjustment_clamp_page (adjustment, x - allocation.width, x);
1146             }
1147
1148           return;
1149         }
1150     }
1151 }
1152
1153 static void
1154 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1155                                          GtkWidget        *toplevel)
1156 {
1157   GtkToolItemGroupPrivate* priv = group->priv;
1158
1159   if (toplevel != priv->toplevel)
1160     {
1161       if (priv->toplevel)
1162         {
1163           /* Disconnect focus tracking handler. */
1164           g_signal_handler_disconnect (priv->toplevel,
1165                                        priv->focus_set_id);
1166
1167           priv->focus_set_id = 0;
1168           priv->toplevel = NULL;
1169         }
1170
1171       if (toplevel)
1172         {
1173           /* Install focus tracking handler. We connect to the window's
1174            * set-focus signal instead of connecting to the focus signal of
1175            * each child to:
1176            *
1177            * 1) Reduce the number of signal handlers used.
1178            * 2) Avoid special handling for group headers.
1179            * 3) Catch focus grabs not only for direct children,
1180            *    but also for nested widgets.
1181            */
1182           priv->focus_set_id =
1183             g_signal_connect (toplevel, "set-focus",
1184                               G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1185                               group);
1186
1187           priv->toplevel = toplevel;
1188         }
1189     }
1190 }
1191
1192 static void
1193 gtk_tool_item_group_realize (GtkWidget *widget)
1194 {
1195   GtkAllocation allocation;
1196   GtkWidget *toplevel_window;
1197   GdkWindow *window;
1198   GdkWindowAttr attributes;
1199   GdkDisplay *display;
1200   gint attributes_mask;
1201   guint border_width;
1202
1203   gtk_widget_set_realized (widget, TRUE);
1204
1205   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1206
1207   gtk_widget_get_allocation (widget, &allocation);
1208
1209   attributes.window_type = GDK_WINDOW_CHILD;
1210   attributes.x = allocation.x + border_width;
1211   attributes.y = allocation.y + border_width;
1212   attributes.width = allocation.width - border_width * 2;
1213   attributes.height = allocation.height - border_width * 2;
1214   attributes.wclass = GDK_INPUT_OUTPUT;
1215   attributes.visual = gtk_widget_get_visual (widget);
1216   attributes.event_mask = gtk_widget_get_events (widget)
1217                          | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1218                          | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1219                          | GDK_BUTTON_MOTION_MASK;
1220   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1221
1222   window = gdk_window_new (gtk_widget_get_parent_window (widget),
1223                            &attributes, attributes_mask);
1224   gtk_widget_set_window (widget, window);
1225
1226   display = gdk_window_get_display (window);
1227
1228   if (gdk_display_supports_composite (display))
1229     gdk_window_set_composited (window, TRUE);
1230
1231   gdk_window_set_user_data (window, widget);
1232
1233   gtk_widget_style_attach (widget);
1234   gtk_style_set_background (gtk_widget_get_style (widget),
1235                             window, GTK_STATE_NORMAL);
1236
1237   gtk_container_forall (GTK_CONTAINER (widget),
1238                         (GtkCallback) gtk_widget_set_parent_window,
1239                         window);
1240
1241   gtk_widget_queue_resize_no_redraw (widget);
1242
1243   toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1244   gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1245                                            toplevel_window);
1246 }
1247
1248 static void
1249 gtk_tool_item_group_unrealize (GtkWidget *widget)
1250 {
1251   gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1252   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1253 }
1254
1255 static void
1256 gtk_tool_item_group_style_set (GtkWidget *widget,
1257                                GtkStyle  *previous_style)
1258 {
1259   gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1260   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1261 }
1262
1263 static void
1264 gtk_tool_item_group_add (GtkContainer *container,
1265                          GtkWidget    *widget)
1266 {
1267   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1268   g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1269
1270   gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1271                               GTK_TOOL_ITEM (widget), -1);
1272 }
1273
1274 static void
1275 gtk_tool_item_group_remove (GtkContainer *container,
1276                             GtkWidget    *child)
1277 {
1278   GtkToolItemGroup *group;
1279   GtkToolItemGroupPrivate* priv;
1280   GList *it;
1281
1282   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1283   group = GTK_TOOL_ITEM_GROUP (container);
1284   priv = group->priv;
1285
1286   for (it = priv->children; it != NULL; it = it->next)
1287     {
1288       GtkToolItemGroupChild *child_info = it->data;
1289
1290       if ((GtkWidget *)child_info->item == child)
1291         {
1292           g_object_unref (child);
1293           gtk_widget_unparent (child);
1294
1295           g_free (child_info);
1296           priv->children = g_list_delete_link (priv->children, it);
1297
1298           gtk_widget_queue_resize (GTK_WIDGET (container));
1299           break;
1300         }
1301     }
1302 }
1303
1304 static void
1305 gtk_tool_item_group_forall (GtkContainer *container,
1306                             gboolean      internals,
1307                             GtkCallback   callback,
1308                             gpointer      callback_data)
1309 {
1310   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1311   GtkToolItemGroupPrivate* priv = group->priv;
1312   GList *children;
1313
1314   if (internals && priv->header)
1315     callback (priv->header, callback_data);
1316
1317   children = priv->children;
1318   while (children)
1319     {
1320       GtkToolItemGroupChild *child = children->data;
1321       children = children->next; /* store pointer before call to callback
1322                                     because the child pointer is invalid if the
1323                                     child->item is removed from the item group
1324                                     in callback */
1325
1326       callback (GTK_WIDGET (child->item), callback_data);
1327     }
1328 }
1329
1330 static GType
1331 gtk_tool_item_group_child_type (GtkContainer *container)
1332 {
1333   return GTK_TYPE_TOOL_ITEM;
1334 }
1335
1336 static GtkToolItemGroupChild *
1337 gtk_tool_item_group_get_child (GtkToolItemGroup  *group,
1338                                GtkToolItem       *item,
1339                                gint              *position,
1340                                GList            **link)
1341 {
1342   guint i;
1343   GList *it;
1344
1345   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1346   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1347
1348   for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1349     {
1350       GtkToolItemGroupChild *child = it->data;
1351
1352       if (child->item == item)
1353         {
1354           if (position)
1355             *position = i;
1356
1357           if (link)
1358             *link = it;
1359
1360           return child;
1361         }
1362     }
1363
1364   return NULL;
1365 }
1366
1367 static void
1368 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1369                                       GtkToolItem      *item,
1370                                       gboolean         *homogeneous,
1371                                       gboolean         *expand,
1372                                       gboolean         *fill,
1373                                       gboolean         *new_row)
1374 {
1375   GtkToolItemGroupChild *child;
1376
1377   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1378   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1379
1380   child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1381   if (!child)
1382     return;
1383
1384   if (expand)
1385     *expand = child->expand;
1386
1387   if (homogeneous)
1388     *homogeneous = child->homogeneous;
1389
1390   if (fill)
1391     *fill = child->fill;
1392
1393   if (new_row)
1394     *new_row = child->new_row;
1395 }
1396
1397 static void
1398 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1399                                       GtkToolItem      *item,
1400                                       gboolean          homogeneous,
1401                                       gboolean          expand,
1402                                       gboolean          fill,
1403                                       gboolean          new_row)
1404 {
1405   GtkToolItemGroupChild *child;
1406   gboolean changed = FALSE;
1407
1408   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1409   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1410
1411   child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1412   if (!child)
1413     return;
1414
1415   gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1416
1417   if (child->homogeneous != homogeneous)
1418     {
1419       child->homogeneous = homogeneous;
1420       changed = TRUE;
1421       gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1422     }
1423   if (child->expand != expand)
1424     {
1425       child->expand = expand;
1426       changed = TRUE;
1427       gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1428     }
1429   if (child->fill != fill)
1430     {
1431       child->fill = fill;
1432       changed = TRUE;
1433       gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1434     }
1435   if (child->new_row != new_row)
1436     {
1437       child->new_row = new_row;
1438       changed = TRUE;
1439       gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1440     }
1441
1442   gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1443
1444   if (changed
1445       && gtk_widget_get_visible (GTK_WIDGET (group))
1446       && gtk_widget_get_visible (GTK_WIDGET (item)))
1447     gtk_widget_queue_resize (GTK_WIDGET (group));
1448 }
1449
1450 static void
1451 gtk_tool_item_group_set_child_property (GtkContainer *container,
1452                                         GtkWidget    *child,
1453                                         guint         prop_id,
1454                                         const GValue *value,
1455                                         GParamSpec   *pspec)
1456 {
1457   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1458   GtkToolItem *item = GTK_TOOL_ITEM (child);
1459   gboolean homogeneous, expand, fill, new_row;
1460
1461   if (prop_id != CHILD_PROP_POSITION)
1462     gtk_tool_item_group_get_item_packing (group, item,
1463                                           &homogeneous,
1464                                           &expand,
1465                                           &fill,
1466                                           &new_row);
1467
1468   switch (prop_id)
1469     {
1470       case CHILD_PROP_HOMOGENEOUS:
1471         gtk_tool_item_group_set_item_packing (group, item,
1472                                               g_value_get_boolean (value),
1473                                               expand,
1474                                               fill,
1475                                               new_row);
1476         break;
1477
1478       case CHILD_PROP_EXPAND:
1479         gtk_tool_item_group_set_item_packing (group, item,
1480                                               homogeneous,
1481                                               g_value_get_boolean (value),
1482                                               fill,
1483                                               new_row);
1484         break;
1485
1486       case CHILD_PROP_FILL:
1487         gtk_tool_item_group_set_item_packing (group, item,
1488                                               homogeneous,
1489                                               expand,
1490                                               g_value_get_boolean (value),
1491                                               new_row);
1492         break;
1493
1494       case CHILD_PROP_NEW_ROW:
1495         gtk_tool_item_group_set_item_packing (group, item,
1496                                               homogeneous,
1497                                               expand,
1498                                               fill,
1499                                               g_value_get_boolean (value));
1500         break;
1501
1502       case CHILD_PROP_POSITION:
1503         gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1504         break;
1505
1506       default:
1507         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1508         break;
1509     }
1510 }
1511
1512 static void
1513 gtk_tool_item_group_get_child_property (GtkContainer *container,
1514                                         GtkWidget    *child,
1515                                         guint         prop_id,
1516                                         GValue       *value,
1517                                         GParamSpec   *pspec)
1518 {
1519   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1520   GtkToolItem *item = GTK_TOOL_ITEM (child);
1521   gboolean homogeneous, expand, fill, new_row;
1522
1523   if (prop_id != CHILD_PROP_POSITION)
1524     gtk_tool_item_group_get_item_packing (group, item,
1525                                           &homogeneous,
1526                                           &expand,
1527                                           &fill,
1528                                           &new_row);
1529
1530   switch (prop_id)
1531     {
1532       case CHILD_PROP_HOMOGENEOUS:
1533         g_value_set_boolean (value, homogeneous);
1534         break;
1535
1536        case CHILD_PROP_EXPAND:
1537         g_value_set_boolean (value, expand);
1538         break;
1539
1540        case CHILD_PROP_FILL:
1541         g_value_set_boolean (value, fill);
1542         break;
1543
1544        case CHILD_PROP_NEW_ROW:
1545         g_value_set_boolean (value, new_row);
1546         break;
1547
1548      case CHILD_PROP_POSITION:
1549         g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1550         break;
1551
1552       default:
1553         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1554         break;
1555     }
1556 }
1557
1558 static void
1559 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1560 {
1561   GObjectClass       *oclass = G_OBJECT_CLASS (cls);
1562   GtkWidgetClass     *wclass = GTK_WIDGET_CLASS (cls);
1563   GtkContainerClass  *cclass = GTK_CONTAINER_CLASS (cls);
1564
1565   oclass->set_property       = gtk_tool_item_group_set_property;
1566   oclass->get_property       = gtk_tool_item_group_get_property;
1567   oclass->finalize           = gtk_tool_item_group_finalize;
1568   oclass->dispose            = gtk_tool_item_group_dispose;
1569
1570   wclass->get_preferred_width  = gtk_tool_item_group_get_preferred_width;
1571   wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1572   wclass->size_allocate        = gtk_tool_item_group_size_allocate;
1573   wclass->realize              = gtk_tool_item_group_realize;
1574   wclass->unrealize            = gtk_tool_item_group_unrealize;
1575   wclass->style_set            = gtk_tool_item_group_style_set;
1576   wclass->screen_changed       = gtk_tool_item_group_screen_changed;
1577
1578   cclass->add                = gtk_tool_item_group_add;
1579   cclass->remove             = gtk_tool_item_group_remove;
1580   cclass->forall             = gtk_tool_item_group_forall;
1581   cclass->child_type         = gtk_tool_item_group_child_type;
1582   cclass->set_child_property = gtk_tool_item_group_set_child_property;
1583   cclass->get_child_property = gtk_tool_item_group_get_child_property;
1584
1585   g_object_class_install_property (oclass, PROP_LABEL,
1586                                    g_param_spec_string ("label",
1587                                                         P_("Label"),
1588                                                         P_("The human-readable title of this item group"),
1589                                                         DEFAULT_LABEL,
1590                                                         GTK_PARAM_READWRITE));
1591
1592   g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1593                                    g_param_spec_object  ("label-widget",
1594                                                         P_("Label widget"),
1595                                                         P_("A widget to display in place of the usual label"),
1596                                                         GTK_TYPE_WIDGET,
1597                                                         GTK_PARAM_READWRITE));
1598
1599   g_object_class_install_property (oclass, PROP_COLLAPSED,
1600                                    g_param_spec_boolean ("collapsed",
1601                                                          P_("Collapsed"),
1602                                                          P_("Whether the group has been collapsed and items are hidden"),
1603                                                          DEFAULT_COLLAPSED,
1604                                                          GTK_PARAM_READWRITE));
1605
1606   g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1607                                    g_param_spec_enum ("ellipsize",
1608                                                       P_("ellipsize"),
1609                                                       P_("Ellipsize for item group headers"),
1610                                                       PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1611                                                       GTK_PARAM_READWRITE));
1612
1613   g_object_class_install_property (oclass, PROP_RELIEF,
1614                                    g_param_spec_enum ("header-relief",
1615                                                       P_("Header Relief"),
1616                                                       P_("Relief of the group header button"),
1617                                                       GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1618                                                       GTK_PARAM_READWRITE));
1619
1620   gtk_widget_class_install_style_property (wclass,
1621                                            g_param_spec_int ("expander-size",
1622                                                              P_("Expander Size"),
1623                                                              P_("Size of the expander arrow"),
1624                                                              0,
1625                                                              G_MAXINT,
1626                                                              DEFAULT_EXPANDER_SIZE,
1627                                                              GTK_PARAM_READABLE));
1628
1629   gtk_widget_class_install_style_property (wclass,
1630                                            g_param_spec_int ("header-spacing",
1631                                                              P_("Header Spacing"),
1632                                                              P_("Spacing between expander arrow and caption"),
1633                                                              0,
1634                                                              G_MAXINT,
1635                                                              DEFAULT_HEADER_SPACING,
1636                                                              GTK_PARAM_READABLE));
1637
1638   gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1639                                               g_param_spec_boolean ("homogeneous",
1640                                                                     P_("Homogeneous"),
1641                                                                     P_("Whether the item should be the same size as other homogeneous items"),
1642                                                                     TRUE,
1643                                                                     GTK_PARAM_READWRITE));
1644
1645   gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1646                                               g_param_spec_boolean ("expand",
1647                                                                     P_("Expand"),
1648                                                                     P_("Whether the item should receive extra space when the group grows"),
1649                                                                     FALSE,
1650                                                                     GTK_PARAM_READWRITE)); 
1651
1652   gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1653                                               g_param_spec_boolean ("fill",
1654                                                                     P_("Fill"),
1655                                                                     P_("Whether the item should fill the available space"),
1656                                                                     TRUE,
1657                                                                     GTK_PARAM_READWRITE));
1658
1659   gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1660                                               g_param_spec_boolean ("new-row",
1661                                                                     P_("New Row"),
1662                                                                     P_("Whether the item should start a new row"),
1663                                                                     FALSE,
1664                                                                     GTK_PARAM_READWRITE));
1665
1666   gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1667                                               g_param_spec_int ("position",
1668                                                                 P_("Position"),
1669                                                                 P_("Position of the item within this group"),
1670                                                                 0,
1671                                                                 G_MAXINT,
1672                                                                 0,
1673                                                                 GTK_PARAM_READWRITE));
1674
1675   g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1676 }
1677
1678 /**
1679  * gtk_tool_item_group_new:
1680  * @label: the label of the new group
1681  *
1682  * Creates a new tool item group with label @label.
1683  *
1684  * Returns: a new #GtkToolItemGroup.
1685  *
1686  * Since: 2.20
1687  */
1688 GtkWidget*
1689 gtk_tool_item_group_new (const gchar *label)
1690 {
1691   return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1692 }
1693
1694 /**
1695  * gtk_tool_item_group_set_label:
1696  * @group: a #GtkToolItemGroup
1697  * @label: the new human-readable label of of the group
1698  *
1699  * Sets the label of the tool item group. The label is displayed in the header
1700  * of the group.
1701  *
1702  * Since: 2.20
1703  */
1704 void
1705 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1706                                const gchar      *label)
1707 {
1708   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1709
1710   if (!label)
1711     gtk_tool_item_group_set_label_widget (group, NULL);
1712   else
1713     {
1714       GtkWidget *child = gtk_label_new (label);
1715       gtk_widget_show (child);
1716
1717       gtk_tool_item_group_set_label_widget (group, child);
1718     }
1719
1720   g_object_notify (G_OBJECT (group), "label");
1721 }
1722
1723 /**
1724  * gtk_tool_item_group_set_label_widget:
1725  * @group: a #GtkToolItemGroup
1726  * @label_widget: the widget to be displayed in place of the usual label
1727  *
1728  * Sets the label of the tool item group.
1729  * The label widget is displayed in the header of the group, in place
1730  * of the usual label.
1731  *
1732  * Since: 2.20
1733  */
1734 void
1735 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1736                                       GtkWidget        *label_widget)
1737 {
1738   GtkToolItemGroupPrivate* priv;
1739   GtkWidget *alignment;
1740
1741   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1742   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1743   g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1744
1745   priv = group->priv;
1746
1747   if (priv->label_widget == label_widget)
1748     return;
1749
1750   alignment = gtk_tool_item_group_get_alignment (group);
1751
1752   if (priv->label_widget)
1753     {
1754       gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1755       gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1756     }
1757
1758
1759   if (label_widget)
1760       gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1761
1762   priv->label_widget = label_widget;
1763
1764   if (gtk_widget_get_visible (GTK_WIDGET (group)))
1765     gtk_widget_queue_resize (GTK_WIDGET (group));
1766
1767   /* Only show the header widget if the group has children: */
1768   if (label_widget && priv->children)
1769     gtk_widget_show (priv->header);
1770   else
1771     gtk_widget_hide (priv->header);
1772
1773   g_object_freeze_notify (G_OBJECT (group));
1774   g_object_notify (G_OBJECT (group), "label-widget");
1775   g_object_notify (G_OBJECT (group), "label");
1776   g_object_thaw_notify (G_OBJECT (group));
1777 }
1778
1779 /**
1780  * gtk_tool_item_group_set_header_relief:
1781  * @group: a #GtkToolItemGroup
1782  * @style: the #GtkReliefStyle
1783  *
1784  * Set the button relief of the group header.
1785  * See gtk_button_set_relief() for details.
1786  *
1787  * Since: 2.20
1788  */
1789 void
1790 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1791                                        GtkReliefStyle    style)
1792 {
1793   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1794
1795   gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1796 }
1797
1798 static gint64
1799 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1800 {
1801   GTimeVal now;
1802
1803   g_source_get_current_time (group->priv->animation_timeout, &now);
1804   return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1805 }
1806
1807 static void
1808 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1809 {
1810   GtkToolItemGroupPrivate* priv = group->priv;
1811   GtkWidget *widget = GTK_WIDGET (group);
1812
1813   if (gtk_widget_get_realized (priv->header))
1814     {
1815       GtkAllocation alignment_allocation;
1816       GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1817       GdkRectangle area;
1818
1819       /* Find the header button's arrow area... */
1820       gtk_widget_get_allocation (alignment, &alignment_allocation);
1821       area.x = alignment_allocation.x;
1822       area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1823       area.height = priv->expander_size;
1824       area.width = priv->expander_size;
1825
1826       /* ... and invalidated it to get it animated. */
1827       gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1828     }
1829
1830   if (gtk_widget_get_realized (widget))
1831     {
1832       GtkAllocation allocation;
1833       GtkWidget *parent = gtk_widget_get_parent (widget);
1834       int x, y, width, height;
1835
1836       /* Find the tool item area button's arrow area... */
1837       gtk_widget_get_allocation (widget, &allocation);
1838       width = allocation.width;
1839       height = allocation.height;
1840
1841       gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1842
1843       if (gtk_widget_get_visible (priv->header))
1844         {
1845           GtkAllocation header_allocation;
1846
1847           gtk_widget_get_allocation (priv->header, &header_allocation);
1848           height -= header_allocation.height;
1849           y += header_allocation.height;
1850         }
1851
1852       /* ... and invalidated it to get it animated. */
1853       gtk_widget_queue_draw_area (parent, x, y, width, height);
1854     }
1855 }
1856
1857 static gboolean
1858 gtk_tool_item_group_animation_cb (gpointer data)
1859 {
1860   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1861   GtkToolItemGroupPrivate* priv = group->priv;
1862   gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1863   gboolean retval;
1864
1865   GDK_THREADS_ENTER ();
1866
1867   /* Enque this early to reduce number of expose events. */
1868   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1869
1870   /* Figure out current style of the expander arrow. */
1871   if (priv->collapsed)
1872     {
1873       if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1874         priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1875       else
1876         priv->expander_style = GTK_EXPANDER_COLLAPSED;
1877     }
1878   else
1879     {
1880       if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1881         priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1882       else
1883         priv->expander_style = GTK_EXPANDER_EXPANDED;
1884     }
1885
1886   gtk_tool_item_group_force_expose (group);
1887
1888   /* Finish animation when done. */
1889   if (timestamp >= ANIMATION_DURATION)
1890     priv->animation_timeout = NULL;
1891
1892   retval = (priv->animation_timeout != NULL);
1893
1894   GDK_THREADS_LEAVE ();
1895
1896   return retval;
1897 }
1898
1899 /**
1900  * gtk_tool_item_group_set_collapsed:
1901  * @group: a #GtkToolItemGroup
1902  * @collapsed: whether the @group should be collapsed or expanded
1903  *
1904  * Sets whether the @group should be collapsed or expanded.
1905  *
1906  * Since: 2.20
1907  */
1908 void
1909 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1910                                    gboolean          collapsed)
1911 {
1912   GtkWidget *parent;
1913   GtkToolItemGroupPrivate* priv;
1914
1915   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1916
1917   priv = group->priv;
1918
1919   parent = gtk_widget_get_parent (GTK_WIDGET (group));
1920   if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1921     _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1922                                            GTK_WIDGET (group));
1923   if (collapsed != priv->collapsed)
1924     {
1925       if (priv->animation)
1926         {
1927           GTimeVal now;
1928
1929           g_get_current_time (&now);
1930
1931           if (priv->animation_timeout)
1932             g_source_destroy (priv->animation_timeout);
1933
1934           priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1935           priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1936
1937           g_source_set_callback (priv->animation_timeout,
1938                                  gtk_tool_item_group_animation_cb,
1939                                  group, NULL);
1940
1941           g_source_attach (priv->animation_timeout, NULL);
1942         }
1943         else
1944         {
1945           priv->expander_style = GTK_EXPANDER_COLLAPSED;
1946           gtk_tool_item_group_force_expose (group);
1947         }
1948
1949       priv->collapsed = collapsed;
1950       g_object_notify (G_OBJECT (group), "collapsed");
1951     }
1952 }
1953
1954 /**
1955  * gtk_tool_item_group_set_ellipsize:
1956  * @group: a #GtkToolItemGroup
1957  * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1958  *
1959  * Sets the ellipsization mode which should be used by labels in @group.
1960  *
1961  * Since: 2.20
1962  */
1963 void
1964 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup   *group,
1965                                    PangoEllipsizeMode  ellipsize)
1966 {
1967   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1968
1969   if (ellipsize != group->priv->ellipsize)
1970     {
1971       group->priv->ellipsize = ellipsize;
1972       gtk_tool_item_group_header_adjust_style (group);
1973       g_object_notify (G_OBJECT (group), "ellipsize");
1974       _gtk_tool_item_group_palette_reconfigured (group);
1975     }
1976 }
1977
1978 /**
1979  * gtk_tool_item_group_get_label:
1980  * @group: a #GtkToolItemGroup
1981  *
1982  * Gets the label of @group.
1983  *
1984  * Returns: the label of @group. The label is an internal string of @group
1985  *     and must not be modified. Note that %NULL is returned if a custom
1986  *     label has been set with gtk_tool_item_group_set_label_widget()
1987  *
1988  * Since: 2.20
1989  */
1990 G_CONST_RETURN gchar*
1991 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1992 {
1993   GtkToolItemGroupPrivate *priv;
1994
1995   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1996
1997   priv = group->priv;
1998
1999   if (GTK_IS_LABEL (priv->label_widget))
2000     return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2001   else
2002     return NULL;
2003 }
2004
2005 /**
2006  * gtk_tool_item_group_get_label_widget:
2007  * @group: a #GtkToolItemGroup
2008  *
2009  * Gets the label widget of @group.
2010  * See gtk_tool_item_group_set_label_widget().
2011  *
2012  * Returns: (transfer none): the label widget of @group
2013  *
2014  * Since: 2.20
2015  */
2016 GtkWidget*
2017 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2018 {
2019   GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2020
2021   return gtk_bin_get_child (GTK_BIN (alignment));
2022 }
2023
2024 /**
2025  * gtk_tool_item_group_get_collapsed:
2026  * @group: a GtkToolItemGroup
2027  *
2028  * Gets whether @group is collapsed or expanded.
2029  *
2030  * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2031  *
2032  * Since: 2.20
2033  */
2034 gboolean
2035 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2036 {
2037   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2038
2039   return group->priv->collapsed;
2040 }
2041
2042 /**
2043  * gtk_tool_item_group_get_ellipsize:
2044  * @group: a #GtkToolItemGroup
2045  *
2046  * Gets the ellipsization mode of @group.
2047  *
2048  * Returns: the #PangoEllipsizeMode of @group
2049  *
2050  * Since: 2.20
2051  */
2052 PangoEllipsizeMode
2053 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2054 {
2055   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2056
2057   return group->priv->ellipsize;
2058 }
2059
2060 /**
2061  * gtk_tool_item_group_get_header_relief:
2062  * @group: a #GtkToolItemGroup
2063  *
2064  * Gets the relief mode of the header button of @group.
2065  *
2066  * Returns: the #GtkReliefStyle
2067  *
2068  * Since: 2.20
2069  */
2070 GtkReliefStyle
2071 gtk_tool_item_group_get_header_relief (GtkToolItemGroup   *group)
2072 {
2073   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2074
2075   return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2076 }
2077
2078 /**
2079  * gtk_tool_item_group_insert:
2080  * @group: a #GtkToolItemGroup
2081  * @item: the #GtkToolItem to insert into @group
2082  * @position: the position of @item in @group, starting with 0.
2083  *     The position -1 means end of list.
2084  *
2085  * Inserts @item at @position in the list of children of @group.
2086  *
2087  * Since: 2.20
2088  */
2089 void
2090 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2091                             GtkToolItem      *item,
2092                             gint              position)
2093 {
2094   GtkWidget *parent, *child_widget;
2095   GtkToolItemGroupChild *child;
2096
2097   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2098   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2099   g_return_if_fail (position >= -1);
2100
2101   parent = gtk_widget_get_parent (GTK_WIDGET (group));
2102
2103   child = g_new (GtkToolItemGroupChild, 1);
2104   child->item = g_object_ref_sink (item);
2105   child->homogeneous = TRUE;
2106   child->expand = FALSE;
2107   child->fill = TRUE;
2108   child->new_row = FALSE;
2109
2110   group->priv->children = g_list_insert (group->priv->children, child, position);
2111
2112   if (GTK_IS_TOOL_PALETTE (parent))
2113     _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2114
2115   child_widget = gtk_bin_get_child (GTK_BIN (item));
2116
2117   if (GTK_IS_BUTTON (child_widget))
2118     gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2119
2120   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2121 }
2122
2123 /**
2124  * gtk_tool_item_group_set_item_position:
2125  * @group: a #GtkToolItemGroup
2126  * @item: the #GtkToolItem to move to a new position, should
2127  *     be a child of @group.
2128  * @position: the new position of @item in @group, starting with 0.
2129  *     The position -1 means end of list.
2130  *
2131  * Sets the position of @item in the list of children of @group.
2132  *
2133  * Since: 2.20
2134  */
2135 void
2136 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2137                                        GtkToolItem      *item,
2138                                        gint              position)
2139 {
2140   gint old_position;
2141   GList *link;
2142   GtkToolItemGroupChild *child;
2143   GtkToolItemGroupPrivate* priv;
2144
2145   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2146   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2147   g_return_if_fail (position >= -1);
2148
2149   child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2150   priv = group->priv;
2151
2152   g_return_if_fail (child != NULL);
2153
2154   if (position == old_position)
2155     return;
2156
2157   priv->children = g_list_delete_link (priv->children, link);
2158   priv->children = g_list_insert (priv->children, child, position);
2159
2160   gtk_widget_child_notify (GTK_WIDGET (item), "position");
2161   if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2162       gtk_widget_get_visible (GTK_WIDGET (item)))
2163     gtk_widget_queue_resize (GTK_WIDGET (group));
2164 }
2165
2166 /**
2167  * gtk_tool_item_group_get_item_position:
2168  * @group: a #GtkToolItemGroup
2169  * @item: a #GtkToolItem
2170  *
2171  * Gets the position of @item in @group as index.
2172  *
2173  * Returns: the index of @item in @group or -1 if @item is no child of @group
2174  *
2175  * Since: 2.20
2176  */
2177 gint
2178 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2179                                        GtkToolItem      *item)
2180 {
2181   gint position;
2182
2183   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2184   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2185
2186   if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2187     return position;
2188
2189   return -1;
2190 }
2191
2192 /**
2193  * gtk_tool_item_group_get_n_items:
2194  * @group: a #GtkToolItemGroup
2195  *
2196  * Gets the number of tool items in @group.
2197  *
2198  * Returns: the number of tool items in @group
2199  *
2200  * Since: 2.20
2201  */
2202 guint
2203 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2204 {
2205   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2206
2207   return g_list_length (group->priv->children);
2208 }
2209
2210 /**
2211  * gtk_tool_item_group_get_nth_item:
2212  * @group: a #GtkToolItemGroup
2213  * @index: the index
2214  *
2215  * Gets the tool item at @index in group.
2216  *
2217  * Returns: (transfer none): the #GtkToolItem at index
2218  *
2219  * Since: 2.20
2220  */
2221 GtkToolItem*
2222 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2223                                   guint             index)
2224 {
2225   GtkToolItemGroupChild *child;
2226
2227   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2228
2229   child = g_list_nth_data (group->priv->children, index);
2230
2231   return child != NULL ? child->item : NULL;
2232 }
2233
2234 /**
2235  * gtk_tool_item_group_get_drop_item:
2236  * @group: a #GtkToolItemGroup
2237  * @x: the x position
2238  * @y: the y position
2239  *
2240  * Gets the tool item at position (x, y).
2241  *
2242  * Returns: (transfer none): the #GtkToolItem at position (x, y)
2243  *
2244  * Since: 2.20
2245  */
2246 GtkToolItem*
2247 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2248                                    gint              x,
2249                                    gint              y)
2250 {
2251   GtkAllocation allocation;
2252   GtkOrientation orientation;
2253   GList *it;
2254
2255   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2256
2257   gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2258   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2259
2260   g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2261   g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2262
2263   for (it = group->priv->children; it != NULL; it = it->next)
2264     {
2265       GtkToolItemGroupChild *child = it->data;
2266       GtkToolItem *item = child->item;
2267       gint x0, y0;
2268
2269       if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2270         continue;
2271
2272       gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2273
2274       x0 = x - allocation.x;
2275       y0 = y - allocation.y;
2276
2277       if (x0 >= 0 && x0 < allocation.width &&
2278           y0 >= 0 && y0 < allocation.height)
2279         return item;
2280     }
2281
2282   return NULL;
2283 }
2284
2285 void
2286 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2287                                         GtkRequisition   *item_size,
2288                                         gboolean          homogeneous_only,
2289                                         gint             *requested_rows)
2290 {
2291   GtkRequisition child_requisition;
2292   GList *it;
2293   gint rows = 0;
2294   gboolean new_row = TRUE;
2295   GtkOrientation orientation;
2296   GtkToolbarStyle style;
2297
2298   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2299   g_return_if_fail (NULL != item_size);
2300
2301   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2302   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2303
2304   item_size->width = item_size->height = 0;
2305
2306   for (it = group->priv->children; it != NULL; it = it->next)
2307     {
2308       GtkToolItemGroupChild *child = it->data;
2309
2310       if (!gtk_tool_item_group_is_item_visible (group, child))
2311         continue;
2312
2313       if (child->new_row || new_row)
2314         {
2315           rows++;
2316           new_row = FALSE;
2317         }
2318
2319       if (!child->homogeneous && child->expand)
2320           new_row = TRUE;
2321
2322       gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2323                                      &child_requisition, NULL);
2324
2325       if (!homogeneous_only || child->homogeneous)
2326         item_size->width = MAX (item_size->width, child_requisition.width);
2327       item_size->height = MAX (item_size->height, child_requisition.height);
2328     }
2329
2330   if (requested_rows)
2331     *requested_rows = rows;
2332 }
2333
2334 void
2335 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2336                             cairo_t          *cr)
2337 {
2338   GtkAllocation allocation;
2339   GtkWidget *widget = GTK_WIDGET (group);
2340   GtkToolItemGroupPrivate* priv = group->priv;
2341
2342   gtk_widget_get_allocation (widget, &allocation);
2343
2344   gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2345                                allocation.x,
2346                                allocation.y);
2347
2348   if (priv->animation_timeout)
2349     {
2350       GtkAllocation header_allocation;
2351       GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2352       cairo_pattern_t *mask;
2353       gdouble v0, v1;
2354
2355       if (GTK_ORIENTATION_VERTICAL == orientation)
2356         v1 = allocation.height;
2357       else
2358         v1 = allocation.width;
2359
2360       v0 = v1 - 256;
2361
2362       gtk_widget_get_allocation (priv->header, &header_allocation);
2363       if (!gtk_widget_get_visible (priv->header))
2364         v0 = MAX (v0, 0);
2365       else if (GTK_ORIENTATION_VERTICAL == orientation)
2366         v0 = MAX (v0, header_allocation.height);
2367       else
2368         v0 = MAX (v0, header_allocation.width);
2369
2370       v1 = MIN (v0 + 256, v1);
2371
2372       if (GTK_ORIENTATION_VERTICAL == orientation)
2373         {
2374           v0 += allocation.y;
2375           v1 += allocation.y;
2376
2377           mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2378         }
2379       else
2380         {
2381           v0 += allocation.x;
2382           v1 += allocation.x;
2383
2384           mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2385         }
2386
2387       cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2388       cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2389       cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2390       cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2391       cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2392
2393       cairo_mask (cr, mask);
2394       cairo_pattern_destroy (mask);
2395     }
2396   else
2397     cairo_paint (cr);
2398 }
2399
2400 gint
2401 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2402                                          gint              limit,
2403                                          gboolean          vertical,
2404                                          gboolean          animation)
2405 {
2406   GtkRequisition requisition;
2407   GtkToolItemGroupPrivate* priv = group->priv;
2408
2409   gtk_widget_get_preferred_size (GTK_WIDGET (group),
2410                                  &requisition, NULL);
2411
2412   if (!priv->collapsed || priv->animation_timeout)
2413     {
2414       GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2415       GtkRequisition inquery;
2416
2417       if (vertical)
2418         allocation.width = limit;
2419       else
2420         allocation.height = limit;
2421
2422       gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2423                                            &allocation, &inquery);
2424
2425       if (vertical)
2426         inquery.height -= requisition.height;
2427       else
2428         inquery.width -= requisition.width;
2429
2430       if (priv->animation_timeout && animation)
2431         {
2432           gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2433
2434           timestamp = MIN (timestamp, ANIMATION_DURATION);
2435
2436           if (priv->collapsed)
2437             timestamp = ANIMATION_DURATION - timestamp;
2438
2439           if (vertical)
2440             {
2441               inquery.height *= timestamp;
2442               inquery.height /= ANIMATION_DURATION;
2443             }
2444           else
2445             {
2446               inquery.width *= timestamp;
2447               inquery.width /= ANIMATION_DURATION;
2448             }
2449         }
2450
2451       if (vertical)
2452         requisition.height += inquery.height;
2453       else
2454         requisition.width += inquery.width;
2455     }
2456
2457   return (vertical ? requisition.height : requisition.width);
2458 }
2459
2460 gint
2461 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2462                                            gint              width)
2463 {
2464   return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2465 }
2466
2467 gint
2468 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2469                                            gint              height)
2470 {
2471   return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2472 }
2473
2474 static void
2475 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2476                                             gpointer   data)
2477 {
2478   if (GTK_IS_TOOL_ITEM (child))
2479     gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2480 }
2481
2482
2483 void
2484 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2485 {
2486   gtk_container_foreach (GTK_CONTAINER (group),
2487                          gtk_tool_palette_reconfigured_foreach_item,
2488                          NULL);
2489
2490   gtk_tool_item_group_header_adjust_style (group);
2491 }