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