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