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