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