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