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