]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolitemgroup.c
GtkToolItemGroupPrivate: Improve struct packing
[~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   GdkDisplay *display;
1211   gint attributes_mask;
1212   guint border_width;
1213   GtkStyleContext *context;
1214
1215   gtk_widget_set_realized (widget, TRUE);
1216
1217   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
1218   context = gtk_widget_get_style_context (widget);
1219
1220   gtk_widget_get_allocation (widget, &allocation);
1221
1222   attributes.window_type = GDK_WINDOW_CHILD;
1223   attributes.x = allocation.x + border_width;
1224   attributes.y = allocation.y + border_width;
1225   attributes.width = allocation.width - border_width * 2;
1226   attributes.height = allocation.height - border_width * 2;
1227   attributes.wclass = GDK_INPUT_OUTPUT;
1228   attributes.visual = gtk_widget_get_visual (widget);
1229   attributes.event_mask = gtk_widget_get_events (widget)
1230                          | GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1231                          | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1232                          | GDK_BUTTON_MOTION_MASK;
1233   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1234
1235   window = gdk_window_new (gtk_widget_get_parent_window (widget),
1236                            &attributes, attributes_mask);
1237   gtk_widget_set_window (widget, window);
1238
1239   display = gdk_window_get_display (window);
1240
1241   if (gdk_display_supports_composite (display))
1242     gdk_window_set_composited (window, TRUE);
1243
1244   gdk_window_set_user_data (window, widget);
1245
1246   gtk_style_context_set_background (context, window);
1247
1248   gtk_container_forall (GTK_CONTAINER (widget),
1249                         (GtkCallback) gtk_widget_set_parent_window,
1250                         window);
1251
1252   gtk_widget_queue_resize_no_redraw (widget);
1253
1254   toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1255   gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1256                                            toplevel_window);
1257 }
1258
1259 static void
1260 gtk_tool_item_group_unrealize (GtkWidget *widget)
1261 {
1262   gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1263   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1264 }
1265
1266 static void
1267 gtk_tool_item_group_style_updated (GtkWidget *widget)
1268 {
1269   gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1270   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_updated (widget);
1271 }
1272
1273 static void
1274 gtk_tool_item_group_add (GtkContainer *container,
1275                          GtkWidget    *widget)
1276 {
1277   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1278   g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1279
1280   gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1281                               GTK_TOOL_ITEM (widget), -1);
1282 }
1283
1284 static void
1285 gtk_tool_item_group_remove (GtkContainer *container,
1286                             GtkWidget    *child)
1287 {
1288   GtkToolItemGroup *group;
1289   GtkToolItemGroupPrivate* priv;
1290   GList *it;
1291
1292   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1293   group = GTK_TOOL_ITEM_GROUP (container);
1294   priv = group->priv;
1295
1296   for (it = priv->children; it != NULL; it = it->next)
1297     {
1298       GtkToolItemGroupChild *child_info = it->data;
1299
1300       if ((GtkWidget *)child_info->item == child)
1301         {
1302           g_object_unref (child);
1303           gtk_widget_unparent (child);
1304
1305           g_free (child_info);
1306           priv->children = g_list_delete_link (priv->children, it);
1307
1308           gtk_widget_queue_resize (GTK_WIDGET (container));
1309           break;
1310         }
1311     }
1312 }
1313
1314 static void
1315 gtk_tool_item_group_forall (GtkContainer *container,
1316                             gboolean      internals,
1317                             GtkCallback   callback,
1318                             gpointer      callback_data)
1319 {
1320   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1321   GtkToolItemGroupPrivate* priv = group->priv;
1322   GList *children;
1323
1324   if (internals && priv->header)
1325     callback (priv->header, callback_data);
1326
1327   children = priv->children;
1328   while (children)
1329     {
1330       GtkToolItemGroupChild *child = children->data;
1331       children = children->next; /* store pointer before call to callback
1332                                     because the child pointer is invalid if the
1333                                     child->item is removed from the item group
1334                                     in callback */
1335
1336       callback (GTK_WIDGET (child->item), callback_data);
1337     }
1338 }
1339
1340 static GType
1341 gtk_tool_item_group_child_type (GtkContainer *container)
1342 {
1343   return GTK_TYPE_TOOL_ITEM;
1344 }
1345
1346 static GtkToolItemGroupChild *
1347 gtk_tool_item_group_get_child (GtkToolItemGroup  *group,
1348                                GtkToolItem       *item,
1349                                gint              *position,
1350                                GList            **link)
1351 {
1352   guint i;
1353   GList *it;
1354
1355   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1356   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1357
1358   for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1359     {
1360       GtkToolItemGroupChild *child = it->data;
1361
1362       if (child->item == item)
1363         {
1364           if (position)
1365             *position = i;
1366
1367           if (link)
1368             *link = it;
1369
1370           return child;
1371         }
1372     }
1373
1374   return NULL;
1375 }
1376
1377 static void
1378 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1379                                       GtkToolItem      *item,
1380                                       gboolean         *homogeneous,
1381                                       gboolean         *expand,
1382                                       gboolean         *fill,
1383                                       gboolean         *new_row)
1384 {
1385   GtkToolItemGroupChild *child;
1386
1387   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1388   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1389
1390   child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1391   if (!child)
1392     return;
1393
1394   if (expand)
1395     *expand = child->expand;
1396
1397   if (homogeneous)
1398     *homogeneous = child->homogeneous;
1399
1400   if (fill)
1401     *fill = child->fill;
1402
1403   if (new_row)
1404     *new_row = child->new_row;
1405 }
1406
1407 static void
1408 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1409                                       GtkToolItem      *item,
1410                                       gboolean          homogeneous,
1411                                       gboolean          expand,
1412                                       gboolean          fill,
1413                                       gboolean          new_row)
1414 {
1415   GtkToolItemGroupChild *child;
1416   gboolean changed = FALSE;
1417
1418   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1419   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1420
1421   child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1422   if (!child)
1423     return;
1424
1425   gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1426
1427   if (child->homogeneous != homogeneous)
1428     {
1429       child->homogeneous = homogeneous;
1430       changed = TRUE;
1431       gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1432     }
1433   if (child->expand != expand)
1434     {
1435       child->expand = expand;
1436       changed = TRUE;
1437       gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1438     }
1439   if (child->fill != fill)
1440     {
1441       child->fill = fill;
1442       changed = TRUE;
1443       gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1444     }
1445   if (child->new_row != new_row)
1446     {
1447       child->new_row = new_row;
1448       changed = TRUE;
1449       gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1450     }
1451
1452   gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1453
1454   if (changed
1455       && gtk_widget_get_visible (GTK_WIDGET (group))
1456       && gtk_widget_get_visible (GTK_WIDGET (item)))
1457     gtk_widget_queue_resize (GTK_WIDGET (group));
1458 }
1459
1460 static void
1461 gtk_tool_item_group_set_child_property (GtkContainer *container,
1462                                         GtkWidget    *child,
1463                                         guint         prop_id,
1464                                         const GValue *value,
1465                                         GParamSpec   *pspec)
1466 {
1467   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1468   GtkToolItem *item = GTK_TOOL_ITEM (child);
1469   gboolean homogeneous, expand, fill, new_row;
1470
1471   if (prop_id != CHILD_PROP_POSITION)
1472     gtk_tool_item_group_get_item_packing (group, item,
1473                                           &homogeneous,
1474                                           &expand,
1475                                           &fill,
1476                                           &new_row);
1477
1478   switch (prop_id)
1479     {
1480       case CHILD_PROP_HOMOGENEOUS:
1481         gtk_tool_item_group_set_item_packing (group, item,
1482                                               g_value_get_boolean (value),
1483                                               expand,
1484                                               fill,
1485                                               new_row);
1486         break;
1487
1488       case CHILD_PROP_EXPAND:
1489         gtk_tool_item_group_set_item_packing (group, item,
1490                                               homogeneous,
1491                                               g_value_get_boolean (value),
1492                                               fill,
1493                                               new_row);
1494         break;
1495
1496       case CHILD_PROP_FILL:
1497         gtk_tool_item_group_set_item_packing (group, item,
1498                                               homogeneous,
1499                                               expand,
1500                                               g_value_get_boolean (value),
1501                                               new_row);
1502         break;
1503
1504       case CHILD_PROP_NEW_ROW:
1505         gtk_tool_item_group_set_item_packing (group, item,
1506                                               homogeneous,
1507                                               expand,
1508                                               fill,
1509                                               g_value_get_boolean (value));
1510         break;
1511
1512       case CHILD_PROP_POSITION:
1513         gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1514         break;
1515
1516       default:
1517         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1518         break;
1519     }
1520 }
1521
1522 static void
1523 gtk_tool_item_group_get_child_property (GtkContainer *container,
1524                                         GtkWidget    *child,
1525                                         guint         prop_id,
1526                                         GValue       *value,
1527                                         GParamSpec   *pspec)
1528 {
1529   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1530   GtkToolItem *item = GTK_TOOL_ITEM (child);
1531   gboolean homogeneous, expand, fill, new_row;
1532
1533   if (prop_id != CHILD_PROP_POSITION)
1534     gtk_tool_item_group_get_item_packing (group, item,
1535                                           &homogeneous,
1536                                           &expand,
1537                                           &fill,
1538                                           &new_row);
1539
1540   switch (prop_id)
1541     {
1542       case CHILD_PROP_HOMOGENEOUS:
1543         g_value_set_boolean (value, homogeneous);
1544         break;
1545
1546        case CHILD_PROP_EXPAND:
1547         g_value_set_boolean (value, expand);
1548         break;
1549
1550        case CHILD_PROP_FILL:
1551         g_value_set_boolean (value, fill);
1552         break;
1553
1554        case CHILD_PROP_NEW_ROW:
1555         g_value_set_boolean (value, new_row);
1556         break;
1557
1558      case CHILD_PROP_POSITION:
1559         g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1560         break;
1561
1562       default:
1563         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1564         break;
1565     }
1566 }
1567
1568 static void
1569 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1570 {
1571   GObjectClass       *oclass = G_OBJECT_CLASS (cls);
1572   GtkWidgetClass     *wclass = GTK_WIDGET_CLASS (cls);
1573   GtkContainerClass  *cclass = GTK_CONTAINER_CLASS (cls);
1574
1575   oclass->set_property       = gtk_tool_item_group_set_property;
1576   oclass->get_property       = gtk_tool_item_group_get_property;
1577   oclass->finalize           = gtk_tool_item_group_finalize;
1578   oclass->dispose            = gtk_tool_item_group_dispose;
1579
1580   wclass->get_preferred_width  = gtk_tool_item_group_get_preferred_width;
1581   wclass->get_preferred_height = gtk_tool_item_group_get_preferred_height;
1582   wclass->size_allocate        = gtk_tool_item_group_size_allocate;
1583   wclass->realize              = gtk_tool_item_group_realize;
1584   wclass->unrealize            = gtk_tool_item_group_unrealize;
1585   wclass->style_updated        = gtk_tool_item_group_style_updated;
1586   wclass->screen_changed       = gtk_tool_item_group_screen_changed;
1587
1588   cclass->add                = gtk_tool_item_group_add;
1589   cclass->remove             = gtk_tool_item_group_remove;
1590   cclass->forall             = gtk_tool_item_group_forall;
1591   cclass->child_type         = gtk_tool_item_group_child_type;
1592   cclass->set_child_property = gtk_tool_item_group_set_child_property;
1593   cclass->get_child_property = gtk_tool_item_group_get_child_property;
1594
1595   g_object_class_install_property (oclass, PROP_LABEL,
1596                                    g_param_spec_string ("label",
1597                                                         P_("Label"),
1598                                                         P_("The human-readable title of this item group"),
1599                                                         DEFAULT_LABEL,
1600                                                         GTK_PARAM_READWRITE));
1601
1602   g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1603                                    g_param_spec_object  ("label-widget",
1604                                                         P_("Label widget"),
1605                                                         P_("A widget to display in place of the usual label"),
1606                                                         GTK_TYPE_WIDGET,
1607                                                         GTK_PARAM_READWRITE));
1608
1609   g_object_class_install_property (oclass, PROP_COLLAPSED,
1610                                    g_param_spec_boolean ("collapsed",
1611                                                          P_("Collapsed"),
1612                                                          P_("Whether the group has been collapsed and items are hidden"),
1613                                                          DEFAULT_COLLAPSED,
1614                                                          GTK_PARAM_READWRITE));
1615
1616   g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1617                                    g_param_spec_enum ("ellipsize",
1618                                                       P_("ellipsize"),
1619                                                       P_("Ellipsize for item group headers"),
1620                                                       PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1621                                                       GTK_PARAM_READWRITE));
1622
1623   g_object_class_install_property (oclass, PROP_RELIEF,
1624                                    g_param_spec_enum ("header-relief",
1625                                                       P_("Header Relief"),
1626                                                       P_("Relief of the group header button"),
1627                                                       GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1628                                                       GTK_PARAM_READWRITE));
1629
1630   gtk_widget_class_install_style_property (wclass,
1631                                            g_param_spec_int ("expander-size",
1632                                                              P_("Expander Size"),
1633                                                              P_("Size of the expander arrow"),
1634                                                              0,
1635                                                              G_MAXINT,
1636                                                              DEFAULT_EXPANDER_SIZE,
1637                                                              GTK_PARAM_READABLE));
1638
1639   gtk_widget_class_install_style_property (wclass,
1640                                            g_param_spec_int ("header-spacing",
1641                                                              P_("Header Spacing"),
1642                                                              P_("Spacing between expander arrow and caption"),
1643                                                              0,
1644                                                              G_MAXINT,
1645                                                              DEFAULT_HEADER_SPACING,
1646                                                              GTK_PARAM_READABLE));
1647
1648   gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1649                                               g_param_spec_boolean ("homogeneous",
1650                                                                     P_("Homogeneous"),
1651                                                                     P_("Whether the item should be the same size as other homogeneous items"),
1652                                                                     TRUE,
1653                                                                     GTK_PARAM_READWRITE));
1654
1655   gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1656                                               g_param_spec_boolean ("expand",
1657                                                                     P_("Expand"),
1658                                                                     P_("Whether the item should receive extra space when the group grows"),
1659                                                                     FALSE,
1660                                                                     GTK_PARAM_READWRITE)); 
1661
1662   gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1663                                               g_param_spec_boolean ("fill",
1664                                                                     P_("Fill"),
1665                                                                     P_("Whether the item should fill the available space"),
1666                                                                     TRUE,
1667                                                                     GTK_PARAM_READWRITE));
1668
1669   gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1670                                               g_param_spec_boolean ("new-row",
1671                                                                     P_("New Row"),
1672                                                                     P_("Whether the item should start a new row"),
1673                                                                     FALSE,
1674                                                                     GTK_PARAM_READWRITE));
1675
1676   gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1677                                               g_param_spec_int ("position",
1678                                                                 P_("Position"),
1679                                                                 P_("Position of the item within this group"),
1680                                                                 0,
1681                                                                 G_MAXINT,
1682                                                                 0,
1683                                                                 GTK_PARAM_READWRITE));
1684
1685   g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1686 }
1687
1688 /**
1689  * gtk_tool_item_group_new:
1690  * @label: the label of the new group
1691  *
1692  * Creates a new tool item group with label @label.
1693  *
1694  * Returns: a new #GtkToolItemGroup.
1695  *
1696  * Since: 2.20
1697  */
1698 GtkWidget*
1699 gtk_tool_item_group_new (const gchar *label)
1700 {
1701   return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1702 }
1703
1704 /**
1705  * gtk_tool_item_group_set_label:
1706  * @group: a #GtkToolItemGroup
1707  * @label: the new human-readable label of of the group
1708  *
1709  * Sets the label of the tool item group. The label is displayed in the header
1710  * of the group.
1711  *
1712  * Since: 2.20
1713  */
1714 void
1715 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1716                                const gchar      *label)
1717 {
1718   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1719
1720   if (!label)
1721     gtk_tool_item_group_set_label_widget (group, NULL);
1722   else
1723     {
1724       GtkWidget *child = gtk_label_new (label);
1725       gtk_widget_show (child);
1726
1727       gtk_tool_item_group_set_label_widget (group, child);
1728     }
1729
1730   g_object_notify (G_OBJECT (group), "label");
1731 }
1732
1733 /**
1734  * gtk_tool_item_group_set_label_widget:
1735  * @group: a #GtkToolItemGroup
1736  * @label_widget: the widget to be displayed in place of the usual label
1737  *
1738  * Sets the label of the tool item group.
1739  * The label widget is displayed in the header of the group, in place
1740  * of the usual label.
1741  *
1742  * Since: 2.20
1743  */
1744 void
1745 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1746                                       GtkWidget        *label_widget)
1747 {
1748   GtkToolItemGroupPrivate* priv;
1749   GtkWidget *alignment;
1750
1751   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1752   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1753   g_return_if_fail (label_widget == NULL || gtk_widget_get_parent (label_widget) == NULL);
1754
1755   priv = group->priv;
1756
1757   if (priv->label_widget == label_widget)
1758     return;
1759
1760   alignment = gtk_tool_item_group_get_alignment (group);
1761
1762   if (priv->label_widget)
1763     {
1764       gtk_widget_set_state_flags (priv->label_widget, 0, TRUE);
1765       gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1766     }
1767
1768
1769   if (label_widget)
1770       gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1771
1772   priv->label_widget = label_widget;
1773
1774   if (gtk_widget_get_visible (GTK_WIDGET (group)))
1775     gtk_widget_queue_resize (GTK_WIDGET (group));
1776
1777   /* Only show the header widget if the group has children: */
1778   if (label_widget && priv->children)
1779     gtk_widget_show (priv->header);
1780   else
1781     gtk_widget_hide (priv->header);
1782
1783   g_object_freeze_notify (G_OBJECT (group));
1784   g_object_notify (G_OBJECT (group), "label-widget");
1785   g_object_notify (G_OBJECT (group), "label");
1786   g_object_thaw_notify (G_OBJECT (group));
1787 }
1788
1789 /**
1790  * gtk_tool_item_group_set_header_relief:
1791  * @group: a #GtkToolItemGroup
1792  * @style: the #GtkReliefStyle
1793  *
1794  * Set the button relief of the group header.
1795  * See gtk_button_set_relief() for details.
1796  *
1797  * Since: 2.20
1798  */
1799 void
1800 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1801                                        GtkReliefStyle    style)
1802 {
1803   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1804
1805   gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1806 }
1807
1808 static gint64
1809 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1810 {
1811   return (g_source_get_time (group->priv->animation_timeout) -
1812           group->priv->animation_start) / 1000;
1813 }
1814
1815 static void
1816 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1817 {
1818   GtkToolItemGroupPrivate* priv = group->priv;
1819   GtkWidget *widget = GTK_WIDGET (group);
1820
1821   if (gtk_widget_get_realized (priv->header))
1822     {
1823       GtkAllocation alignment_allocation;
1824       GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1825       GdkRectangle area;
1826
1827       /* Find the header button's arrow area... */
1828       gtk_widget_get_allocation (alignment, &alignment_allocation);
1829       area.x = alignment_allocation.x;
1830       area.y = alignment_allocation.y + (alignment_allocation.height - priv->expander_size) / 2;
1831       area.height = priv->expander_size;
1832       area.width = priv->expander_size;
1833
1834       /* ... and invalidated it to get it animated. */
1835       gdk_window_invalidate_rect (gtk_widget_get_window (priv->header), &area, TRUE);
1836     }
1837
1838   if (gtk_widget_get_realized (widget))
1839     {
1840       GtkAllocation allocation;
1841       GtkWidget *parent = gtk_widget_get_parent (widget);
1842       int x, y, width, height;
1843
1844       /* Find the tool item area button's arrow area... */
1845       gtk_widget_get_allocation (widget, &allocation);
1846       width = allocation.width;
1847       height = allocation.height;
1848
1849       gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1850
1851       if (gtk_widget_get_visible (priv->header))
1852         {
1853           GtkAllocation header_allocation;
1854
1855           gtk_widget_get_allocation (priv->header, &header_allocation);
1856           height -= header_allocation.height;
1857           y += header_allocation.height;
1858         }
1859
1860       /* ... and invalidated it to get it animated. */
1861       gtk_widget_queue_draw_area (parent, x, y, width, height);
1862     }
1863 }
1864
1865 static gboolean
1866 gtk_tool_item_group_animation_cb (gpointer data)
1867 {
1868   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1869   GtkToolItemGroupPrivate* priv = group->priv;
1870   gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1871   gboolean retval;
1872
1873   GDK_THREADS_ENTER ();
1874
1875   /* Enque this early to reduce number of expose events. */
1876   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1877
1878   gtk_tool_item_group_force_expose (group);
1879
1880   /* Finish animation when done. */
1881   if (timestamp >= ANIMATION_DURATION)
1882     priv->animation_timeout = NULL;
1883
1884   retval = (priv->animation_timeout != NULL);
1885
1886   GDK_THREADS_LEAVE ();
1887
1888   return retval;
1889 }
1890
1891 /**
1892  * gtk_tool_item_group_set_collapsed:
1893  * @group: a #GtkToolItemGroup
1894  * @collapsed: whether the @group should be collapsed or expanded
1895  *
1896  * Sets whether the @group should be collapsed or expanded.
1897  *
1898  * Since: 2.20
1899  */
1900 void
1901 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1902                                    gboolean          collapsed)
1903 {
1904   GtkWidget *parent;
1905   GtkToolItemGroupPrivate* priv;
1906
1907   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1908
1909   priv = group->priv;
1910
1911   parent = gtk_widget_get_parent (GTK_WIDGET (group));
1912   if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1913     _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1914                                            GTK_WIDGET (group));
1915   if (collapsed != priv->collapsed)
1916     {
1917       GtkStyleContext *context;
1918
1919       if (priv->animation)
1920         {
1921           if (priv->animation_timeout)
1922             g_source_destroy (priv->animation_timeout);
1923
1924           priv->animation_start = g_get_monotonic_time ();
1925           priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1926
1927           g_source_set_callback (priv->animation_timeout,
1928                                  gtk_tool_item_group_animation_cb,
1929                                  group, NULL);
1930           g_source_attach (priv->animation_timeout, NULL);
1931
1932           context = gtk_widget_get_style_context (gtk_bin_get_child (GTK_BIN (priv->header)));
1933
1934           gtk_style_context_save (context);
1935           gtk_style_context_add_class (context, GTK_STYLE_CLASS_EXPANDER);
1936
1937           gtk_style_context_notify_state_change (context,
1938                                                  gtk_widget_get_window (priv->header),
1939                                                  GUINT_TO_POINTER (1),
1940                                                  GTK_STATE_FLAG_ACTIVE,
1941                                                  !collapsed);
1942
1943           gtk_style_context_restore (context);
1944         }
1945       else
1946         gtk_tool_item_group_force_expose (group);
1947
1948       priv->collapsed = collapsed;
1949       g_object_notify (G_OBJECT (group), "collapsed");
1950     }
1951 }
1952
1953 /**
1954  * gtk_tool_item_group_set_ellipsize:
1955  * @group: a #GtkToolItemGroup
1956  * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1957  *
1958  * Sets the ellipsization mode which should be used by labels in @group.
1959  *
1960  * Since: 2.20
1961  */
1962 void
1963 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup   *group,
1964                                    PangoEllipsizeMode  ellipsize)
1965 {
1966   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1967
1968   if (ellipsize != group->priv->ellipsize)
1969     {
1970       group->priv->ellipsize = ellipsize;
1971       gtk_tool_item_group_header_adjust_style (group);
1972       g_object_notify (G_OBJECT (group), "ellipsize");
1973       _gtk_tool_item_group_palette_reconfigured (group);
1974     }
1975 }
1976
1977 /**
1978  * gtk_tool_item_group_get_label:
1979  * @group: a #GtkToolItemGroup
1980  *
1981  * Gets the label of @group.
1982  *
1983  * Returns: the label of @group. The label is an internal string of @group
1984  *     and must not be modified. Note that %NULL is returned if a custom
1985  *     label has been set with gtk_tool_item_group_set_label_widget()
1986  *
1987  * Since: 2.20
1988  */
1989 G_CONST_RETURN gchar*
1990 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1991 {
1992   GtkToolItemGroupPrivate *priv;
1993
1994   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1995
1996   priv = group->priv;
1997
1998   if (GTK_IS_LABEL (priv->label_widget))
1999     return gtk_label_get_label (GTK_LABEL (priv->label_widget));
2000   else
2001     return NULL;
2002 }
2003
2004 /**
2005  * gtk_tool_item_group_get_label_widget:
2006  * @group: a #GtkToolItemGroup
2007  *
2008  * Gets the label widget of @group.
2009  * See gtk_tool_item_group_set_label_widget().
2010  *
2011  * Returns: (transfer none): the label widget of @group
2012  *
2013  * Since: 2.20
2014  */
2015 GtkWidget*
2016 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
2017 {
2018   GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
2019
2020   return gtk_bin_get_child (GTK_BIN (alignment));
2021 }
2022
2023 /**
2024  * gtk_tool_item_group_get_collapsed:
2025  * @group: a GtkToolItemGroup
2026  *
2027  * Gets whether @group is collapsed or expanded.
2028  *
2029  * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
2030  *
2031  * Since: 2.20
2032  */
2033 gboolean
2034 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
2035 {
2036   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
2037
2038   return group->priv->collapsed;
2039 }
2040
2041 /**
2042  * gtk_tool_item_group_get_ellipsize:
2043  * @group: a #GtkToolItemGroup
2044  *
2045  * Gets the ellipsization mode of @group.
2046  *
2047  * Returns: the #PangoEllipsizeMode of @group
2048  *
2049  * Since: 2.20
2050  */
2051 PangoEllipsizeMode
2052 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
2053 {
2054   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
2055
2056   return group->priv->ellipsize;
2057 }
2058
2059 /**
2060  * gtk_tool_item_group_get_header_relief:
2061  * @group: a #GtkToolItemGroup
2062  *
2063  * Gets the relief mode of the header button of @group.
2064  *
2065  * Returns: the #GtkReliefStyle
2066  *
2067  * Since: 2.20
2068  */
2069 GtkReliefStyle
2070 gtk_tool_item_group_get_header_relief (GtkToolItemGroup   *group)
2071 {
2072   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2073
2074   return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2075 }
2076
2077 /**
2078  * gtk_tool_item_group_insert:
2079  * @group: a #GtkToolItemGroup
2080  * @item: the #GtkToolItem to insert into @group
2081  * @position: the position of @item in @group, starting with 0.
2082  *     The position -1 means end of list.
2083  *
2084  * Inserts @item at @position in the list of children of @group.
2085  *
2086  * Since: 2.20
2087  */
2088 void
2089 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2090                             GtkToolItem      *item,
2091                             gint              position)
2092 {
2093   GtkWidget *parent, *child_widget;
2094   GtkToolItemGroupChild *child;
2095
2096   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2097   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2098   g_return_if_fail (position >= -1);
2099
2100   parent = gtk_widget_get_parent (GTK_WIDGET (group));
2101
2102   child = g_new (GtkToolItemGroupChild, 1);
2103   child->item = g_object_ref_sink (item);
2104   child->homogeneous = TRUE;
2105   child->expand = FALSE;
2106   child->fill = TRUE;
2107   child->new_row = FALSE;
2108
2109   group->priv->children = g_list_insert (group->priv->children, child, position);
2110
2111   if (GTK_IS_TOOL_PALETTE (parent))
2112     _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2113
2114   child_widget = gtk_bin_get_child (GTK_BIN (item));
2115
2116   if (GTK_IS_BUTTON (child_widget))
2117     gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2118
2119   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2120 }
2121
2122 /**
2123  * gtk_tool_item_group_set_item_position:
2124  * @group: a #GtkToolItemGroup
2125  * @item: the #GtkToolItem to move to a new position, should
2126  *     be a child of @group.
2127  * @position: the new position of @item in @group, starting with 0.
2128  *     The position -1 means end of list.
2129  *
2130  * Sets the position of @item in the list of children of @group.
2131  *
2132  * Since: 2.20
2133  */
2134 void
2135 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2136                                        GtkToolItem      *item,
2137                                        gint              position)
2138 {
2139   gint old_position;
2140   GList *link;
2141   GtkToolItemGroupChild *child;
2142   GtkToolItemGroupPrivate* priv;
2143
2144   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2145   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2146   g_return_if_fail (position >= -1);
2147
2148   child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2149   priv = group->priv;
2150
2151   g_return_if_fail (child != NULL);
2152
2153   if (position == old_position)
2154     return;
2155
2156   priv->children = g_list_delete_link (priv->children, link);
2157   priv->children = g_list_insert (priv->children, child, position);
2158
2159   gtk_widget_child_notify (GTK_WIDGET (item), "position");
2160   if (gtk_widget_get_visible (GTK_WIDGET (group)) &&
2161       gtk_widget_get_visible (GTK_WIDGET (item)))
2162     gtk_widget_queue_resize (GTK_WIDGET (group));
2163 }
2164
2165 /**
2166  * gtk_tool_item_group_get_item_position:
2167  * @group: a #GtkToolItemGroup
2168  * @item: a #GtkToolItem
2169  *
2170  * Gets the position of @item in @group as index.
2171  *
2172  * Returns: the index of @item in @group or -1 if @item is no child of @group
2173  *
2174  * Since: 2.20
2175  */
2176 gint
2177 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2178                                        GtkToolItem      *item)
2179 {
2180   gint position;
2181
2182   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2183   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2184
2185   if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2186     return position;
2187
2188   return -1;
2189 }
2190
2191 /**
2192  * gtk_tool_item_group_get_n_items:
2193  * @group: a #GtkToolItemGroup
2194  *
2195  * Gets the number of tool items in @group.
2196  *
2197  * Returns: the number of tool items in @group
2198  *
2199  * Since: 2.20
2200  */
2201 guint
2202 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2203 {
2204   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2205
2206   return g_list_length (group->priv->children);
2207 }
2208
2209 /**
2210  * gtk_tool_item_group_get_nth_item:
2211  * @group: a #GtkToolItemGroup
2212  * @index: the index
2213  *
2214  * Gets the tool item at @index in group.
2215  *
2216  * Returns: (transfer none): the #GtkToolItem at index
2217  *
2218  * Since: 2.20
2219  */
2220 GtkToolItem*
2221 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2222                                   guint             index)
2223 {
2224   GtkToolItemGroupChild *child;
2225
2226   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2227
2228   child = g_list_nth_data (group->priv->children, index);
2229
2230   return child != NULL ? child->item : NULL;
2231 }
2232
2233 /**
2234  * gtk_tool_item_group_get_drop_item:
2235  * @group: a #GtkToolItemGroup
2236  * @x: the x position
2237  * @y: the y position
2238  *
2239  * Gets the tool item at position (x, y).
2240  *
2241  * Returns: (transfer none): the #GtkToolItem at position (x, y)
2242  *
2243  * Since: 2.20
2244  */
2245 GtkToolItem*
2246 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2247                                    gint              x,
2248                                    gint              y)
2249 {
2250   GtkAllocation allocation;
2251   GList *it;
2252
2253   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2254
2255   gtk_widget_get_allocation (GTK_WIDGET (group), &allocation);
2256
2257   g_return_val_if_fail (x >= 0 && x < allocation.width, NULL);
2258   g_return_val_if_fail (y >= 0 && y < allocation.height, NULL);
2259
2260   for (it = group->priv->children; it != NULL; it = it->next)
2261     {
2262       GtkToolItemGroupChild *child = it->data;
2263       GtkToolItem *item = child->item;
2264       gint x0, y0;
2265
2266       if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2267         continue;
2268
2269       gtk_widget_get_allocation (GTK_WIDGET (item), &allocation);
2270
2271       x0 = x - allocation.x;
2272       y0 = y - allocation.y;
2273
2274       if (x0 >= 0 && x0 < allocation.width &&
2275           y0 >= 0 && y0 < allocation.height)
2276         return item;
2277     }
2278
2279   return NULL;
2280 }
2281
2282 void
2283 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2284                                         GtkRequisition   *item_size,
2285                                         gboolean          homogeneous_only,
2286                                         gint             *requested_rows)
2287 {
2288   GtkRequisition child_requisition;
2289   GList *it;
2290   gint rows = 0;
2291   gboolean new_row = TRUE;
2292
2293   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2294   g_return_if_fail (NULL != item_size);
2295
2296   item_size->width = item_size->height = 0;
2297
2298   for (it = group->priv->children; it != NULL; it = it->next)
2299     {
2300       GtkToolItemGroupChild *child = it->data;
2301
2302       if (!gtk_tool_item_group_is_item_visible (group, child))
2303         continue;
2304
2305       if (child->new_row || new_row)
2306         {
2307           rows++;
2308           new_row = FALSE;
2309         }
2310
2311       if (!child->homogeneous && child->expand)
2312           new_row = TRUE;
2313
2314       gtk_widget_get_preferred_size (GTK_WIDGET (child->item),
2315                                      &child_requisition, NULL);
2316
2317       if (!homogeneous_only || child->homogeneous)
2318         item_size->width = MAX (item_size->width, child_requisition.width);
2319       item_size->height = MAX (item_size->height, child_requisition.height);
2320     }
2321
2322   if (requested_rows)
2323     *requested_rows = rows;
2324 }
2325
2326 void
2327 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2328                             cairo_t          *cr)
2329 {
2330   GtkAllocation allocation;
2331   GtkWidget *widget = GTK_WIDGET (group);
2332   GtkToolItemGroupPrivate* priv = group->priv;
2333
2334   gtk_widget_get_allocation (widget, &allocation);
2335
2336   gdk_cairo_set_source_window (cr, gtk_widget_get_window (widget),
2337                                allocation.x,
2338                                allocation.y);
2339
2340   if (priv->animation_timeout)
2341     {
2342       GtkAllocation header_allocation;
2343       GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2344       cairo_pattern_t *mask;
2345       gdouble v0, v1;
2346
2347       if (GTK_ORIENTATION_VERTICAL == orientation)
2348         v1 = allocation.height;
2349       else
2350         v1 = allocation.width;
2351
2352       v0 = v1 - 256;
2353
2354       gtk_widget_get_allocation (priv->header, &header_allocation);
2355       if (!gtk_widget_get_visible (priv->header))
2356         v0 = MAX (v0, 0);
2357       else if (GTK_ORIENTATION_VERTICAL == orientation)
2358         v0 = MAX (v0, header_allocation.height);
2359       else
2360         v0 = MAX (v0, header_allocation.width);
2361
2362       v1 = MIN (v0 + 256, v1);
2363
2364       if (GTK_ORIENTATION_VERTICAL == orientation)
2365         {
2366           v0 += allocation.y;
2367           v1 += allocation.y;
2368
2369           mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2370         }
2371       else
2372         {
2373           v0 += allocation.x;
2374           v1 += allocation.x;
2375
2376           mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2377         }
2378
2379       cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2380       cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2381       cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2382       cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2383       cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2384
2385       cairo_mask (cr, mask);
2386       cairo_pattern_destroy (mask);
2387     }
2388   else
2389     cairo_paint (cr);
2390 }
2391
2392 gint
2393 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2394                                          gint              limit,
2395                                          gboolean          vertical,
2396                                          gboolean          animation)
2397 {
2398   GtkRequisition requisition;
2399   GtkToolItemGroupPrivate* priv = group->priv;
2400
2401   gtk_widget_get_preferred_size (GTK_WIDGET (group),
2402                                  &requisition, NULL);
2403
2404   if (!priv->collapsed || priv->animation_timeout)
2405     {
2406       GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2407       GtkRequisition inquery;
2408
2409       if (vertical)
2410         allocation.width = limit;
2411       else
2412         allocation.height = limit;
2413
2414       gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2415                                            &allocation, &inquery);
2416
2417       if (vertical)
2418         inquery.height -= requisition.height;
2419       else
2420         inquery.width -= requisition.width;
2421
2422       if (priv->animation_timeout && animation)
2423         {
2424           gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2425
2426           timestamp = MIN (timestamp, ANIMATION_DURATION);
2427
2428           if (priv->collapsed)
2429             timestamp = ANIMATION_DURATION - timestamp;
2430
2431           if (vertical)
2432             {
2433               inquery.height *= timestamp;
2434               inquery.height /= ANIMATION_DURATION;
2435             }
2436           else
2437             {
2438               inquery.width *= timestamp;
2439               inquery.width /= ANIMATION_DURATION;
2440             }
2441         }
2442
2443       if (vertical)
2444         requisition.height += inquery.height;
2445       else
2446         requisition.width += inquery.width;
2447     }
2448
2449   return (vertical ? requisition.height : requisition.width);
2450 }
2451
2452 gint
2453 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2454                                            gint              width)
2455 {
2456   return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2457 }
2458
2459 gint
2460 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2461                                            gint              height)
2462 {
2463   return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2464 }
2465
2466 static void
2467 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2468                                             gpointer   data)
2469 {
2470   if (GTK_IS_TOOL_ITEM (child))
2471     gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2472 }
2473
2474
2475 void
2476 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2477 {
2478   gtk_container_foreach (GTK_CONTAINER (group),
2479                          gtk_tool_palette_reconfigured_foreach_item,
2480                          NULL);
2481
2482   gtk_tool_item_group_header_adjust_style (group);
2483 }