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