]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolitemgroup.c
d8910089086ba74899cc79412fd54a034bc70e4c
[~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
438       case PROP_COLLAPSED:
439         gtk_tool_item_group_set_collapsed (group, g_value_get_boolean (value));
440         break;
441
442       case PROP_ELLIPSIZE:
443         gtk_tool_item_group_set_ellipsize (group, g_value_get_enum (value));
444         break;
445
446       case PROP_RELIEF:
447         gtk_tool_item_group_set_header_relief (group, g_value_get_enum(value));
448         break;
449
450       default:
451         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
452         break;
453     }
454 }
455
456 static void
457 gtk_tool_item_group_get_property (GObject    *object,
458                                   guint       prop_id,
459                                   GValue     *value,
460                                   GParamSpec *pspec)
461 {
462   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
463
464   switch (prop_id)
465     {
466       case PROP_LABEL:
467         g_value_set_string (value, gtk_tool_item_group_get_label (group));
468         break;
469
470       case PROP_LABEL_WIDGET:
471         g_value_set_object (value,
472                             gtk_tool_item_group_get_label_widget (group));
473         break;
474
475       case PROP_COLLAPSED:
476         g_value_set_boolean (value, gtk_tool_item_group_get_collapsed (group));
477         break;
478
479       case PROP_ELLIPSIZE:
480         g_value_set_enum (value, gtk_tool_item_group_get_ellipsize (group));
481         break;
482
483       case PROP_RELIEF:
484         g_value_set_enum (value, gtk_tool_item_group_get_header_relief (group));
485         break;
486
487       default:
488         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
489         break;
490     }
491 }
492
493 static void
494 gtk_tool_item_group_finalize (GObject *object)
495 {
496   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
497
498   if (group->priv->children)
499     {
500       g_list_free (group->priv->children);
501       group->priv->children = NULL;
502     }
503
504   G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->finalize (object);
505 }
506
507 static void
508 gtk_tool_item_group_dispose (GObject *object)
509 {
510   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (object);
511   GtkToolItemGroupPrivate* priv = group->priv;
512
513   if (priv->toplevel)
514     {
515       /* disconnect focus tracking handler */
516       g_signal_handler_disconnect (priv->toplevel,
517                                    priv->focus_set_id);
518
519       priv->focus_set_id = 0;
520       priv->toplevel = NULL;
521     }
522
523   G_OBJECT_CLASS (gtk_tool_item_group_parent_class)->dispose (object);
524 }
525
526 static void
527 gtk_tool_item_group_get_item_size (GtkToolItemGroup *group,
528                                    GtkRequisition   *item_size,
529                                    gboolean          homogeneous_only,
530                                    gint             *requested_rows)
531 {
532   GtkWidget *parent = gtk_widget_get_parent (GTK_WIDGET (group));
533
534   if (GTK_IS_TOOL_PALETTE (parent))
535     _gtk_tool_palette_get_item_size (GTK_TOOL_PALETTE (parent), item_size, homogeneous_only, requested_rows);
536   else
537     _gtk_tool_item_group_item_size_request (group, item_size, homogeneous_only, requested_rows);
538 }
539
540 static void
541 gtk_tool_item_group_size_request (GtkWidget      *widget,
542                                   GtkRequisition *requisition)
543 {
544   const gint border_width = GTK_CONTAINER (widget)->border_width;
545   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
546   GtkToolItemGroupPrivate* priv = group->priv;
547   GtkOrientation orientation;
548   GtkRequisition item_size;
549   gint requested_rows;
550
551   if (priv->children && gtk_tool_item_group_get_label_widget (group))
552     {
553       gtk_widget_size_request (priv->header, requisition);
554       gtk_widget_show (priv->header);
555     }
556   else
557     {
558       requisition->width = requisition->height = 0;
559       gtk_widget_hide (priv->header);
560     }
561
562   gtk_tool_item_group_get_item_size (group, &item_size, FALSE, &requested_rows);
563
564   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
565
566   if (GTK_ORIENTATION_VERTICAL == orientation)
567     requisition->width = MAX (requisition->width, item_size.width);
568   else
569     requisition->height = MAX (requisition->height, item_size.height * requested_rows);
570
571   requisition->width += border_width * 2;
572   requisition->height += border_width * 2;
573 }
574
575 static gboolean
576 gtk_tool_item_group_is_item_visible (GtkToolItemGroup      *group,
577                                      GtkToolItemGroupChild *child)
578 {
579   GtkToolbarStyle style;
580   GtkOrientation orientation;
581
582   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
583   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
584
585   /* horizontal tool palettes with text style support only homogeneous items */
586   if (!child->homogeneous &&
587       GTK_ORIENTATION_HORIZONTAL == orientation &&
588       GTK_TOOLBAR_TEXT == style)
589     return FALSE;
590
591   return
592     (GTK_WIDGET_VISIBLE (child->item)) &&
593     (GTK_ORIENTATION_VERTICAL == orientation ?
594      gtk_tool_item_get_visible_vertical (child->item) :
595      gtk_tool_item_get_visible_horizontal (child->item));
596 }
597
598 static inline unsigned
599 udiv (unsigned x,
600       unsigned y)
601 {
602   return (x + y - 1) / y;
603 }
604
605 static void
606 gtk_tool_item_group_real_size_query (GtkWidget      *widget,
607                                      GtkAllocation  *allocation,
608                                      GtkRequisition *inquery)
609 {
610   const gint border_width = GTK_CONTAINER (widget)->border_width;
611   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
612   GtkToolItemGroupPrivate* priv = group->priv;
613
614   GtkRequisition item_size;
615   GtkAllocation item_area;
616
617   GtkOrientation orientation;
618   GtkToolbarStyle style;
619
620   gint min_rows;
621
622   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
623   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
624
625   /* figure out the size of homogeneous items */
626   gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
627
628   if (GTK_ORIENTATION_VERTICAL == orientation)
629     item_size.width = MIN (item_size.width, allocation->width);
630   else
631     item_size.height = MIN (item_size.height, allocation->height);
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_size_request (GTK_WIDGET (child->item), &req);
682
683                   width = udiv (req.width, item_size.width);
684                   col += width;
685
686                   if (col > n_columns)
687                     row++;
688
689                   col = width;
690
691                   if (col >= n_columns)
692                     new_row = TRUE;
693                 }
694             }
695           n_rows = row + 2;
696         }
697       else
698         {
699           guint *row_min_width;
700           gint row = -1;
701           gboolean new_row = TRUE;
702           guint col = 0, min_col, max_col = 0, all_items = 0;
703           gint i;
704
705           item_area.height = allocation->height - 2 * border_width;
706           n_rows = MAX (item_area.height / item_size.height, min_rows);
707
708           row_min_width = g_new0 (guint, n_rows);
709
710           /* calculate minimal and maximal required cols and minimal required rows */
711           for (it = priv->children; it != NULL; it = it->next)
712             {
713               GtkToolItemGroupChild *child = it->data;
714
715               if (!gtk_tool_item_group_is_item_visible (group, child))
716                 continue;
717
718               if (new_row || child->new_row)
719                 {
720                   new_row = FALSE;
721                   row++;
722                   col = 0;
723                   row_min_width[row] = 1;
724                 }
725
726               if (child->expand)
727                 new_row = TRUE;
728
729               if (child->homogeneous)
730                 {
731                   col++;
732                   all_items++;
733                 }
734               else
735                 {
736                   GtkRequisition req = {0, 0};
737                   guint width;
738
739                   gtk_widget_size_request (GTK_WIDGET (child->item), &req);
740
741                   width = udiv (req.width, item_size.width);
742
743                   col += width;
744                   all_items += width;
745
746                   row_min_width[row] = MAX (row_min_width[row], width);
747                 }
748
749               max_col = MAX (max_col, col);
750             }
751
752           /* calculate minimal required cols */
753           min_col = udiv (all_items, n_rows);
754
755           for (i = 0; i <= row; i++)
756             {
757               min_col = MAX (min_col, row_min_width[i]);
758             }
759
760           /* simple linear search for minimal required columns for the given maximal number of rows (n_rows) */
761           for (n_columns = min_col; n_columns < max_col; n_columns ++)
762             {
763               new_row = TRUE;
764               row = -1;
765               /* calculate required rows for n_columns columns */
766               for (it = priv->children; it != NULL; it = it->next)
767                 {
768                   GtkToolItemGroupChild *child = it->data;
769
770                   if (!gtk_tool_item_group_is_item_visible (group, child))
771                     continue;
772
773                   if (new_row || child->new_row)
774                     {
775                       new_row = FALSE;
776                       row++;
777                       col = 0;
778                     }
779
780                   if (child->expand)
781                     new_row = TRUE;
782
783                   if (child->homogeneous)
784                     {
785                       col++;
786                       if (col >= n_columns)
787                         new_row = TRUE;
788                     }
789                   else
790                     {
791                       GtkRequisition req = {0, 0};
792                       guint width;
793
794                       gtk_widget_size_request (GTK_WIDGET (child->item), &req);
795
796                       width = udiv (req.width, item_size.width);
797                       col += width;
798
799                       if (col > n_columns)
800                         row++;
801
802                       col = width;
803
804                       if (col >= n_columns)
805                         new_row = TRUE;
806                     }
807                 }
808
809               if (row < n_rows)
810                 break;
811             }
812         }
813
814       item_area.width = item_size.width * n_columns;
815       item_area.height = item_size.height * n_rows;
816     }
817
818   inquery->width = 0;
819   inquery->height = 0;
820
821   /* figure out header widget size */
822   if (GTK_WIDGET_VISIBLE (priv->header))
823     {
824       GtkRequisition child_requisition;
825
826       gtk_widget_size_request (priv->header, &child_requisition);
827
828       if (GTK_ORIENTATION_VERTICAL == orientation)
829         inquery->height += child_requisition.height;
830       else
831         inquery->width += child_requisition.width;
832     }
833
834   /* report effective widget size */
835   inquery->width += item_area.width + 2 * border_width;
836   inquery->height += item_area.height + 2 * border_width;
837 }
838
839 static void
840 gtk_tool_item_group_real_size_allocate (GtkWidget     *widget,
841                                         GtkAllocation *allocation)
842 {
843   const gint border_width = GTK_CONTAINER (widget)->border_width;
844   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (widget);
845   GtkToolItemGroupPrivate* priv = group->priv;
846   GtkRequisition child_requisition;
847   GtkAllocation child_allocation;
848
849   GtkRequisition item_size;
850   GtkAllocation item_area;
851
852   GtkOrientation orientation;
853   GtkToolbarStyle style;
854
855   GList *it;
856
857   gint n_columns, n_rows = 1;
858   gint min_rows;
859
860   GtkTextDirection direction = gtk_widget_get_direction (widget);
861
862   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
863   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
864
865   /* chain up */
866   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->size_allocate (widget, allocation);
867
868   child_allocation.x = border_width;
869   child_allocation.y = border_width;
870
871   /* place the header widget */
872   if (GTK_WIDGET_VISIBLE (priv->header))
873     {
874       gtk_widget_size_request (priv->header, &child_requisition);
875
876       if (GTK_ORIENTATION_VERTICAL == orientation)
877         {
878           child_allocation.width = allocation->width;
879           child_allocation.height = child_requisition.height;
880         }
881       else
882         {
883           child_allocation.width = child_requisition.width;
884           child_allocation.height = allocation->height;
885
886           if (GTK_TEXT_DIR_RTL == direction)
887             child_allocation.x = allocation->width - border_width - child_allocation.width;
888         }
889
890       gtk_widget_size_allocate (priv->header, &child_allocation);
891
892       if (GTK_ORIENTATION_VERTICAL == orientation)
893         child_allocation.y += child_allocation.height;
894       else if (GTK_TEXT_DIR_RTL != direction)
895         child_allocation.x += child_allocation.width;
896       else
897         child_allocation.x = border_width;
898     }
899   else
900     child_requisition.width = child_requisition.height = 0;
901
902   /* figure out the size of homogeneous items */
903   gtk_tool_item_group_get_item_size (group, &item_size, TRUE, &min_rows);
904
905   /* figure out the available columns and size of item_area */
906   if (GTK_ORIENTATION_VERTICAL == orientation)
907     {
908       item_size.width = MIN (item_size.width, allocation->width);
909
910       item_area.width = allocation->width - 2 * border_width;
911       item_area.height = allocation->height - 2 * border_width - child_requisition.height;
912
913       n_columns = MAX (item_area.width / item_size.width, 1);
914
915       item_size.width = item_area.width / n_columns;
916     }
917   else
918     {
919       item_size.height = MIN (item_size.height, allocation->height);
920
921       item_area.width = allocation->width - 2 * border_width - child_requisition.width;
922       item_area.height = allocation->height - 2 * border_width;
923
924       n_columns = MAX (item_area.width / item_size.width, 1);
925       n_rows = MAX (item_area.height / item_size.height, min_rows);
926
927       item_size.height = item_area.height / n_rows;
928     }
929
930   item_area.x = child_allocation.x;
931   item_area.y = child_allocation.y;
932
933   /* when expanded or in transition, place the tool items in a grid like layout */
934   if (!priv->collapsed || !priv->animation || priv->animation_timeout)
935     {
936       gint col = 0, row = 0;
937
938       for (it = priv->children; it != NULL; it = it->next)
939         {
940           GtkToolItemGroupChild *child = it->data;
941           gint col_child;
942
943           if (!gtk_tool_item_group_is_item_visible (group, child))
944             {
945               gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
946
947               continue;
948             }
949
950           /* for non homogeneous widgets request the required size */
951           child_requisition.width = 0;
952
953           if (!child->homogeneous)
954             {
955               gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
956               child_requisition.width = MIN (child_requisition.width, item_area.width);
957             }
958
959           /* select next row if at end of row */
960           if (col > 0 && (child->new_row || (col * item_size.width) + MAX (child_requisition.width, item_size.width) > item_area.width))
961             {
962               row++;
963               col = 0;
964               child_allocation.y += child_allocation.height;
965             }
966
967           col_child = col;
968
969           /* calculate the position and size of the item */
970           if (!child->homogeneous)
971             {
972               gint col_width;
973               gint width;
974
975               if (!child->expand)
976                 col_width = udiv (child_requisition.width, item_size.width);
977               else
978                 col_width = n_columns - col;
979
980               width = col_width * item_size.width;
981
982               if (GTK_TEXT_DIR_RTL == direction)
983                 col_child = (n_columns - col - col_width);
984
985               if (child->fill)
986                 {
987                   child_allocation.x = item_area.x + col_child * item_size.width;
988                   child_allocation.width = width;
989                 }
990               else
991                 {
992                   child_allocation.x =
993                     (item_area.x + col_child * item_size.width +
994                     (width - child_requisition.width) / 2);
995                   child_allocation.width = child_requisition.width;
996                 }
997
998               col += col_width;
999             }
1000           else
1001             {
1002               if (GTK_TEXT_DIR_RTL == direction)
1003                 col_child = (n_columns - col - 1);
1004
1005               child_allocation.x = item_area.x + col_child * item_size.width;
1006               child_allocation.width = item_size.width;
1007
1008               col++;
1009             }
1010
1011           child_allocation.height = item_size.height;
1012
1013           gtk_widget_size_allocate (GTK_WIDGET (child->item), &child_allocation);
1014           gtk_widget_set_child_visible (GTK_WIDGET (child->item), TRUE);
1015         }
1016
1017       child_allocation.y += item_size.height;
1018     }
1019
1020   /* or just hide all items, when collapsed */
1021
1022   else
1023     {
1024       for (it = priv->children; it != NULL; it = it->next)
1025         {
1026           GtkToolItemGroupChild *child = it->data;
1027
1028           gtk_widget_set_child_visible (GTK_WIDGET (child->item), FALSE);
1029         }
1030     }
1031 }
1032
1033 static void
1034 gtk_tool_item_group_size_allocate (GtkWidget     *widget,
1035                                    GtkAllocation *allocation)
1036 {
1037   gtk_tool_item_group_real_size_allocate (widget, allocation);
1038
1039   if (GTK_WIDGET_MAPPED (widget))
1040     gdk_window_invalidate_rect (widget->window, NULL, FALSE);
1041 }
1042
1043 static void
1044 gtk_tool_item_group_set_focus_cb (GtkWidget *window,
1045                                   GtkWidget *widget,
1046                                   gpointer   user_data)
1047 {
1048   GtkAdjustment *adjustment;
1049   GtkWidget *p;
1050
1051   /* Find this group's parent widget in the focused widget's anchestry. */
1052   for (p = widget; p; p = gtk_widget_get_parent (p))
1053     if (p == user_data)
1054       {
1055         p = gtk_widget_get_parent (p);
1056         break;
1057       }
1058
1059   if (GTK_IS_TOOL_PALETTE (p))
1060     {
1061       /* Check that the focused widgets is fully visible within
1062        * the group's parent widget and make it visible otherwise. */
1063
1064       adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1065       adjustment = gtk_tool_palette_get_vadjustment (GTK_TOOL_PALETTE (p));
1066
1067       if (adjustment)
1068         {
1069           int y;
1070
1071           /* Handle vertical adjustment. */
1072           if (gtk_widget_translate_coordinates
1073                 (widget, p, 0, 0, NULL, &y) && y < 0)
1074             {
1075               y += adjustment->value;
1076               gtk_adjustment_clamp_page (adjustment, y, y + widget->allocation.height);
1077             }
1078           else if (gtk_widget_translate_coordinates
1079                       (widget, p, 0, widget->allocation.height, NULL, &y) &&
1080                    y > p->allocation.height)
1081             {
1082               y += adjustment->value;
1083               gtk_adjustment_clamp_page (adjustment, y - widget->allocation.height, y);
1084             }
1085         }
1086
1087       adjustment = gtk_tool_palette_get_hadjustment (GTK_TOOL_PALETTE (p));
1088
1089       if (adjustment)
1090         {
1091           int x;
1092
1093           /* Handle horizontal adjustment. */
1094           if (gtk_widget_translate_coordinates
1095                 (widget, p, 0, 0, &x, NULL) && x < 0)
1096             {
1097               x += adjustment->value;
1098               gtk_adjustment_clamp_page (adjustment, x, x + widget->allocation.width);
1099             }
1100           else if (gtk_widget_translate_coordinates
1101                       (widget, p, widget->allocation.width, 0, &x, NULL) &&
1102                    x > p->allocation.width)
1103             {
1104               x += adjustment->value;
1105               gtk_adjustment_clamp_page (adjustment, x - widget->allocation.width, x);
1106             }
1107
1108           return;
1109         }
1110     }
1111 }
1112
1113 static void
1114 gtk_tool_item_group_set_toplevel_window (GtkToolItemGroup *group,
1115                                          GtkWidget        *toplevel)
1116 {
1117   GtkToolItemGroupPrivate* priv = group->priv;
1118
1119   if (toplevel != priv->toplevel)
1120     {
1121       if (priv->toplevel)
1122         {
1123           /* Disconnect focus tracking handler. */
1124           g_signal_handler_disconnect (priv->toplevel,
1125                                        priv->focus_set_id);
1126
1127           priv->focus_set_id = 0;
1128           priv->toplevel = NULL;
1129         }
1130
1131       if (toplevel)
1132         {
1133           /* Install focus tracking handler. We connect to the window's
1134            * set-focus signal instead of connecting to the focus signal of
1135            * each child to:
1136            *
1137            * 1) Reduce the number of signal handlers used.
1138            * 2) Avoid special handling for group headers.
1139            * 3) Catch focus grabs not only for direct children,
1140            *    but also for nested widgets.
1141            */
1142           priv->focus_set_id =
1143             g_signal_connect (toplevel, "set-focus",
1144                               G_CALLBACK (gtk_tool_item_group_set_focus_cb),
1145                               group);
1146
1147           priv->toplevel = toplevel;
1148         }
1149     }
1150 }
1151
1152 static void
1153 gtk_tool_item_group_realize (GtkWidget *widget)
1154 {
1155   GtkWidget *toplevel_window;
1156   const gint border_width = GTK_CONTAINER (widget)->border_width;
1157   gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1158   GdkWindowAttr attributes;
1159   GdkDisplay *display;
1160
1161   attributes.window_type = GDK_WINDOW_CHILD;
1162   attributes.x = widget->allocation.x + border_width;
1163   attributes.y = widget->allocation.y + border_width;
1164   attributes.width = widget->allocation.width - border_width * 2;
1165   attributes.height = widget->allocation.height - border_width * 2;
1166   attributes.wclass = GDK_INPUT_OUTPUT;
1167   attributes.visual = gtk_widget_get_visual (widget);
1168   attributes.colormap = gtk_widget_get_colormap (widget);
1169   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
1170                         | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
1171                         | GDK_BUTTON_MOTION_MASK;
1172
1173   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1174                                    &attributes, attributes_mask);
1175
1176   display = gdk_drawable_get_display (widget->window);
1177
1178   if (gdk_display_supports_composite (display))
1179     gdk_window_set_composited (widget->window, TRUE);
1180
1181   gdk_window_set_user_data (widget->window, widget);
1182   widget->style = gtk_style_attach (widget->style, widget->window);
1183   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
1184   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1185
1186   gtk_container_forall (GTK_CONTAINER (widget),
1187                         (GtkCallback) gtk_widget_set_parent_window,
1188                         widget->window);
1189
1190   gtk_widget_queue_resize_no_redraw (widget);
1191
1192   toplevel_window = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);
1193   gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget),
1194                                            toplevel_window);
1195 }
1196
1197 static void
1198 gtk_tool_item_group_unrealize (GtkWidget *widget)
1199 {
1200   gtk_tool_item_group_set_toplevel_window (GTK_TOOL_ITEM_GROUP (widget), NULL);
1201   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->unrealize (widget);
1202 }
1203
1204 static void
1205 gtk_tool_item_group_style_set (GtkWidget *widget,
1206                                GtkStyle  *previous_style)
1207 {
1208   gtk_tool_item_group_header_adjust_style (GTK_TOOL_ITEM_GROUP (widget));
1209   GTK_WIDGET_CLASS (gtk_tool_item_group_parent_class)->style_set (widget, previous_style);
1210 }
1211
1212 static void
1213 gtk_tool_item_group_add (GtkContainer *container,
1214                          GtkWidget    *widget)
1215 {
1216   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1217   g_return_if_fail (GTK_IS_TOOL_ITEM (widget));
1218
1219   gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (container),
1220                               GTK_TOOL_ITEM (widget), -1);
1221 }
1222
1223 static void
1224 gtk_tool_item_group_remove (GtkContainer *container,
1225                             GtkWidget    *child)
1226 {
1227   GtkToolItemGroup *group;
1228   GtkToolItemGroupPrivate* priv;
1229   GList *it;
1230
1231   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (container));
1232   group = GTK_TOOL_ITEM_GROUP (container);
1233   priv = group->priv;
1234
1235   for (it = priv->children; it != NULL; it = it->next)
1236     {
1237       GtkToolItemGroupChild *child_info = it->data;
1238
1239       if ((GtkWidget *)child_info->item == child)
1240         {
1241           g_object_unref (child);
1242           gtk_widget_unparent (child);
1243
1244           g_free (child_info);
1245           priv->children = g_list_delete_link (priv->children, it);
1246
1247           gtk_widget_queue_resize (GTK_WIDGET (container));
1248           break;
1249         }
1250     }
1251 }
1252
1253 static void
1254 gtk_tool_item_group_forall (GtkContainer *container,
1255                             gboolean      internals,
1256                             GtkCallback   callback,
1257                             gpointer      callback_data)
1258 {
1259   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1260   GtkToolItemGroupPrivate* priv = group->priv;
1261   GList *children;
1262
1263   if (internals && priv->header)
1264     callback (priv->header, callback_data);
1265
1266   children = priv->children;
1267   while (children)
1268     {
1269       GtkToolItemGroupChild *child = children->data;
1270       children = children->next; /* store pointer before call to callback
1271                                     because the child pointer is invalid if the
1272                                     child->item is removed from the item group
1273                                     in callback */
1274
1275       callback (GTK_WIDGET (child->item), callback_data);
1276     }
1277 }
1278
1279 static GType
1280 gtk_tool_item_group_child_type (GtkContainer *container)
1281 {
1282   return GTK_TYPE_TOOL_ITEM;
1283 }
1284
1285 static GtkToolItemGroupChild *
1286 gtk_tool_item_group_get_child (GtkToolItemGroup  *group,
1287                                GtkToolItem       *item,
1288                                gint              *position,
1289                                GList            **link)
1290 {
1291   guint i;
1292   GList *it;
1293
1294   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1295   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), NULL);
1296
1297   for (it = group->priv->children, i = 0; it != NULL; it = it->next, ++i)
1298     {
1299       GtkToolItemGroupChild *child = it->data;
1300
1301       if (child->item == item)
1302         {
1303           if (position)
1304             *position = i;
1305
1306           if (link)
1307             *link = it;
1308
1309           return child;
1310         }
1311     }
1312
1313   return NULL;
1314 }
1315
1316 static void
1317 gtk_tool_item_group_get_item_packing (GtkToolItemGroup *group,
1318                                       GtkToolItem      *item,
1319                                       gboolean         *homogeneous,
1320                                       gboolean         *expand,
1321                                       gboolean         *fill,
1322                                       gboolean         *new_row)
1323 {
1324   GtkToolItemGroupChild *child;
1325
1326   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1327   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1328
1329   child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1330   if (!child)
1331     return;
1332
1333   if (expand)
1334     *expand = child->expand;
1335
1336   if (homogeneous)
1337     *homogeneous = child->homogeneous;
1338
1339   if (fill)
1340     *fill = child->fill;
1341
1342   if (new_row)
1343     *new_row = child->new_row;
1344 }
1345
1346 static void
1347 gtk_tool_item_group_set_item_packing (GtkToolItemGroup *group,
1348                                       GtkToolItem      *item,
1349                                       gboolean          homogeneous,
1350                                       gboolean          expand,
1351                                       gboolean          fill,
1352                                       gboolean          new_row)
1353 {
1354   GtkToolItemGroupChild *child;
1355   gboolean changed = FALSE;
1356
1357   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1358   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
1359
1360   child = gtk_tool_item_group_get_child (group, item, NULL, NULL);
1361   if (!child)
1362     return;
1363
1364   gtk_widget_freeze_child_notify (GTK_WIDGET (item));
1365
1366   if (child->homogeneous != homogeneous)
1367     {
1368       child->homogeneous = homogeneous;
1369       changed = TRUE;
1370       gtk_widget_child_notify (GTK_WIDGET (item), "homogeneous");
1371     }
1372   if (child->expand != expand)
1373     {
1374       child->expand = expand;
1375       changed = TRUE;
1376       gtk_widget_child_notify (GTK_WIDGET (item), "expand");
1377     }
1378   if (child->fill != fill)
1379     {
1380       child->fill = fill;
1381       changed = TRUE;
1382       gtk_widget_child_notify (GTK_WIDGET (item), "fill");
1383     }
1384   if (child->new_row != new_row)
1385     {
1386       child->new_row = new_row;
1387       changed = TRUE;
1388       gtk_widget_child_notify (GTK_WIDGET (item), "new-row");
1389     }
1390
1391   gtk_widget_thaw_child_notify (GTK_WIDGET (item));
1392
1393   if (changed && GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
1394     gtk_widget_queue_resize (GTK_WIDGET (group));
1395 }
1396
1397 static void
1398 gtk_tool_item_group_set_child_property (GtkContainer *container,
1399                                         GtkWidget    *child,
1400                                         guint         prop_id,
1401                                         const GValue *value,
1402                                         GParamSpec   *pspec)
1403 {
1404   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1405   GtkToolItem *item = GTK_TOOL_ITEM (child);
1406   gboolean homogeneous, expand, fill, new_row;
1407
1408   if (prop_id != CHILD_PROP_POSITION)
1409     gtk_tool_item_group_get_item_packing (group, item,
1410                                           &homogeneous,
1411                                           &expand,
1412                                           &fill,
1413                                           &new_row);
1414
1415   switch (prop_id)
1416     {
1417       case CHILD_PROP_HOMOGENEOUS:
1418         gtk_tool_item_group_set_item_packing (group, item,
1419                                               g_value_get_boolean (value),
1420                                               expand,
1421                                               fill,
1422                                               new_row);
1423         break;
1424
1425       case CHILD_PROP_EXPAND:
1426         gtk_tool_item_group_set_item_packing (group, item,
1427                                               homogeneous,
1428                                               g_value_get_boolean (value),
1429                                               fill,
1430                                               new_row);
1431         break;
1432
1433       case CHILD_PROP_FILL:
1434         gtk_tool_item_group_set_item_packing (group, item,
1435                                               homogeneous,
1436                                               expand,
1437                                               g_value_get_boolean (value),
1438                                               new_row);
1439         break;
1440
1441       case CHILD_PROP_NEW_ROW:
1442         gtk_tool_item_group_set_item_packing (group, item,
1443                                               homogeneous,
1444                                               expand,
1445                                               fill,
1446                                               g_value_get_boolean (value));
1447         break;
1448
1449       case CHILD_PROP_POSITION:
1450         gtk_tool_item_group_set_item_position (group, item, g_value_get_int (value));
1451         break;
1452
1453       default:
1454         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1455         break;
1456     }
1457 }
1458
1459 static void
1460 gtk_tool_item_group_get_child_property (GtkContainer *container,
1461                                         GtkWidget    *child,
1462                                         guint         prop_id,
1463                                         GValue       *value,
1464                                         GParamSpec   *pspec)
1465 {
1466   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (container);
1467   GtkToolItem *item = GTK_TOOL_ITEM (child);
1468   gboolean homogeneous, expand, fill, new_row;
1469
1470   if (prop_id != CHILD_PROP_POSITION)
1471     gtk_tool_item_group_get_item_packing (group, item,
1472                                           &homogeneous,
1473                                           &expand,
1474                                           &fill,
1475                                           &new_row);
1476
1477   switch (prop_id)
1478     {
1479       case CHILD_PROP_HOMOGENEOUS:
1480         g_value_set_boolean (value, homogeneous);
1481         break;
1482
1483        case CHILD_PROP_EXPAND:
1484         g_value_set_boolean (value, expand);
1485         break;
1486
1487        case CHILD_PROP_FILL:
1488         g_value_set_boolean (value, fill);
1489         break;
1490
1491        case CHILD_PROP_NEW_ROW:
1492         g_value_set_boolean (value, new_row);
1493         break;
1494
1495      case CHILD_PROP_POSITION:
1496         g_value_set_int (value, gtk_tool_item_group_get_item_position (group, item));
1497         break;
1498
1499       default:
1500         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
1501         break;
1502     }
1503 }
1504
1505 static void
1506 gtk_tool_item_group_class_init (GtkToolItemGroupClass *cls)
1507 {
1508   GObjectClass       *oclass = G_OBJECT_CLASS (cls);
1509   GtkWidgetClass     *wclass = GTK_WIDGET_CLASS (cls);
1510   GtkContainerClass  *cclass = GTK_CONTAINER_CLASS (cls);
1511
1512   oclass->set_property       = gtk_tool_item_group_set_property;
1513   oclass->get_property       = gtk_tool_item_group_get_property;
1514   oclass->finalize           = gtk_tool_item_group_finalize;
1515   oclass->dispose            = gtk_tool_item_group_dispose;
1516
1517   wclass->size_request       = gtk_tool_item_group_size_request;
1518   wclass->size_allocate      = gtk_tool_item_group_size_allocate;
1519   wclass->realize            = gtk_tool_item_group_realize;
1520   wclass->unrealize          = gtk_tool_item_group_unrealize;
1521   wclass->style_set          = gtk_tool_item_group_style_set;
1522   wclass->screen_changed     = gtk_tool_item_group_screen_changed;
1523
1524   cclass->add                = gtk_tool_item_group_add;
1525   cclass->remove             = gtk_tool_item_group_remove;
1526   cclass->forall             = gtk_tool_item_group_forall;
1527   cclass->child_type         = gtk_tool_item_group_child_type;
1528   cclass->set_child_property = gtk_tool_item_group_set_child_property;
1529   cclass->get_child_property = gtk_tool_item_group_get_child_property;
1530
1531   g_object_class_install_property (oclass, PROP_LABEL,
1532                                    g_param_spec_string ("label",
1533                                                         P_("Label"),
1534                                                         P_("The human-readable title of this item group"),
1535                                                         DEFAULT_LABEL,
1536                                                         GTK_PARAM_READWRITE));
1537
1538   g_object_class_install_property (oclass, PROP_LABEL_WIDGET,
1539                                    g_param_spec_object  ("label-widget",
1540                                                         P_("Label widget"),
1541                                                         P_("A widget to display in place of the usual label"),
1542                                                         GTK_TYPE_WIDGET,
1543                                                         GTK_PARAM_READWRITE));
1544
1545   g_object_class_install_property (oclass, PROP_COLLAPSED,
1546                                    g_param_spec_boolean ("collapsed",
1547                                                          P_("Collapsed"),
1548                                                          P_("Wether the group has been collapsed and items are hidden"),
1549                                                          DEFAULT_COLLAPSED,
1550                                                          GTK_PARAM_READWRITE));
1551
1552   g_object_class_install_property (oclass, PROP_ELLIPSIZE,
1553                                    g_param_spec_enum ("ellipsize",
1554                                                       P_("ellipsize"),
1555                                                       P_("Ellipsize for item group headers"),
1556                                                       PANGO_TYPE_ELLIPSIZE_MODE, DEFAULT_ELLIPSIZE,
1557                                                       GTK_PARAM_READWRITE));
1558
1559   g_object_class_install_property (oclass, PROP_RELIEF,
1560                                    g_param_spec_enum ("header-relief",
1561                                                       P_("Header Relief"),
1562                                                       P_("Relief of the group header button"),
1563                                                       GTK_TYPE_RELIEF_STYLE, GTK_RELIEF_NORMAL,
1564                                                       GTK_PARAM_READWRITE));
1565
1566   gtk_widget_class_install_style_property (wclass,
1567                                            g_param_spec_int ("expander-size",
1568                                                              P_("Expander Size"),
1569                                                              P_("Size of the expander arrow"),
1570                                                              0,
1571                                                              G_MAXINT,
1572                                                              DEFAULT_EXPANDER_SIZE,
1573                                                              GTK_PARAM_READABLE));
1574
1575   gtk_widget_class_install_style_property (wclass,
1576                                            g_param_spec_int ("header-spacing",
1577                                                              P_("Header Spacing"),
1578                                                              P_("Spacing between expander arrow and caption"),
1579                                                              0,
1580                                                              G_MAXINT,
1581                                                              DEFAULT_HEADER_SPACING,
1582                                                              GTK_PARAM_READABLE));
1583
1584   gtk_container_class_install_child_property (cclass, CHILD_PROP_HOMOGENEOUS,
1585                                               g_param_spec_boolean ("homogeneous",
1586                                                                     P_("Homogeneous"),
1587                                                                     P_("Whether the item should be the same size as other homogeneous items"),
1588                                                                     TRUE,
1589                                                                     GTK_PARAM_READWRITE));
1590
1591   gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1592                                               g_param_spec_boolean ("expand",
1593                                                                     P_("Expand"),
1594                                                                     P_("Whether the item should receive extra space when the group grows"),
1595                                                                     FALSE,
1596                                                                     GTK_PARAM_READWRITE)); 
1597
1598   gtk_container_class_install_child_property (cclass, CHILD_PROP_FILL,
1599                                               g_param_spec_boolean ("fill",
1600                                                                     P_("Fill"),
1601                                                                     P_("Whether the item should fill the available space"),
1602                                                                     TRUE,
1603                                                                     GTK_PARAM_READWRITE));
1604
1605   gtk_container_class_install_child_property (cclass, CHILD_PROP_NEW_ROW,
1606                                               g_param_spec_boolean ("new-row",
1607                                                                     P_("New Row"),
1608                                                                     P_("Whether the item should start a new row"),
1609                                                                     FALSE,
1610                                                                     GTK_PARAM_READWRITE));
1611
1612   gtk_container_class_install_child_property (cclass, CHILD_PROP_POSITION,
1613                                               g_param_spec_int ("position",
1614                                                                 P_("Position"),
1615                                                                 P_("Position of the item within this group"),
1616                                                                 0,
1617                                                                 G_MAXINT,
1618                                                                 0,
1619                                                                 GTK_PARAM_READWRITE));
1620
1621   g_type_class_add_private (cls, sizeof (GtkToolItemGroupPrivate));
1622 }
1623
1624 /**
1625  * gtk_tool_item_group_new:
1626  * @label: the label of the new group
1627  *
1628  * Creates a new tool item group with label @label.
1629  *
1630  * Returns: a new #GtkToolItemGroup.
1631  *
1632  * Since: 2.20
1633  */
1634 GtkWidget*
1635 gtk_tool_item_group_new (const gchar *label)
1636 {
1637   return g_object_new (GTK_TYPE_TOOL_ITEM_GROUP, "label", label, NULL);
1638 }
1639
1640 /**
1641  * gtk_tool_item_group_set_label:
1642  * @group: a #GtkToolItemGroup
1643  * @label: the new human-readable label of of the group
1644  *
1645  * Sets the label of the tool item group. The label is displayed in the header
1646  * of the group.
1647  *
1648  * Since: 2.20
1649  */
1650 void
1651 gtk_tool_item_group_set_label (GtkToolItemGroup *group,
1652                                const gchar      *label)
1653 {
1654   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1655
1656   if (!label)
1657     gtk_tool_item_group_set_label_widget (group, NULL);
1658   else
1659     {
1660       GtkWidget *child = gtk_label_new (label);
1661       gtk_widget_show (child);
1662
1663       gtk_tool_item_group_set_label_widget (group, child);
1664     }
1665
1666   g_object_notify (G_OBJECT (group), "label");
1667 }
1668
1669 /**
1670  * gtk_tool_item_group_set_label_widget:
1671  * @group: a #GtkToolItemGroup
1672  * @label_widget: the widget to be displayed in place of the usual label
1673  *
1674  * Sets the label of the tool item group.
1675  * The label widget is displayed in the header of the group, in place
1676  * of the usual label.
1677  *
1678  * Since: 2.20
1679  */
1680 void
1681 gtk_tool_item_group_set_label_widget (GtkToolItemGroup *group,
1682                                       GtkWidget        *label_widget)
1683 {
1684   GtkToolItemGroupPrivate* priv;
1685   GtkWidget *alignment;
1686
1687   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1688   g_return_if_fail (label_widget == NULL || GTK_IS_WIDGET (label_widget));
1689   g_return_if_fail (label_widget == NULL || label_widget->parent == NULL);
1690
1691   priv = group->priv;
1692
1693   if (priv->label_widget == label_widget)
1694     return;
1695
1696   alignment = gtk_tool_item_group_get_alignment (group);
1697
1698   if (priv->label_widget)
1699     {
1700       gtk_widget_set_state (priv->label_widget, GTK_STATE_NORMAL);
1701       gtk_container_remove (GTK_CONTAINER (alignment), priv->label_widget);
1702     }
1703
1704
1705   if (label_widget)
1706       gtk_container_add (GTK_CONTAINER (alignment), label_widget);
1707
1708   priv->label_widget = label_widget;
1709
1710   if (GTK_WIDGET_VISIBLE (group))
1711     gtk_widget_queue_resize (GTK_WIDGET (group));
1712
1713   /* Only show the header widget if the group has children: */
1714   if (label_widget && priv->children)
1715     gtk_widget_show (priv->header);
1716   else
1717     gtk_widget_hide (priv->header);
1718
1719   g_object_freeze_notify (G_OBJECT (group));
1720   g_object_notify (G_OBJECT (group), "label-widget");
1721   g_object_notify (G_OBJECT (group), "label");
1722   g_object_thaw_notify (G_OBJECT (group));
1723 }
1724
1725 /**
1726  * gtk_tool_item_group_set_header_relief:
1727  * @group: a #GtkToolItemGroup
1728  * @style: the #GtkReliefStyle
1729  *
1730  * Set the button relief of the group header.
1731  * See gtk_button_set_relief() for details.
1732  *
1733  * Since: 2.20
1734  */
1735 void
1736 gtk_tool_item_group_set_header_relief (GtkToolItemGroup *group,
1737                                        GtkReliefStyle    style)
1738 {
1739   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1740
1741   gtk_button_set_relief (GTK_BUTTON (group->priv->header), style);
1742 }
1743
1744 static gint64
1745 gtk_tool_item_group_get_animation_timestamp (GtkToolItemGroup *group)
1746 {
1747   GTimeVal now;
1748
1749   g_source_get_current_time (group->priv->animation_timeout, &now);
1750   return (now.tv_sec * G_USEC_PER_SEC + now.tv_usec - group->priv->animation_start) / 1000;
1751 }
1752
1753 static void
1754 gtk_tool_item_group_force_expose (GtkToolItemGroup *group)
1755 {
1756   GtkToolItemGroupPrivate* priv = group->priv;
1757
1758   if (GTK_WIDGET_REALIZED (priv->header))
1759     {
1760       GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1761       GdkRectangle area;
1762
1763       /* Find the header button's arrow area... */
1764       area.x = alignment->allocation.x;
1765       area.y = alignment->allocation.y + (alignment->allocation.height - priv->expander_size) / 2;
1766       area.height = priv->expander_size;
1767       area.width = priv->expander_size;
1768
1769       /* ... and invalidated it to get it animated. */
1770       gdk_window_invalidate_rect (priv->header->window, &area, TRUE);
1771     }
1772
1773   if (GTK_WIDGET_REALIZED (group))
1774     {
1775       GtkWidget *widget = GTK_WIDGET (group);
1776       GtkWidget *parent = gtk_widget_get_parent (widget);
1777       int x, y, width, height;
1778
1779       /* Find the tool item area button's arrow area... */
1780       width = widget->allocation.width;
1781       height = widget->allocation.height;
1782
1783       gtk_widget_translate_coordinates (widget, parent, 0, 0, &x, &y);
1784
1785       if (GTK_WIDGET_VISIBLE (priv->header))
1786         {
1787           height -= priv->header->allocation.height;
1788           y += priv->header->allocation.height;
1789         }
1790
1791       /* ... and invalidated it to get it animated. */
1792       gtk_widget_queue_draw_area (parent, x, y, width, height);
1793     }
1794 }
1795
1796 static gboolean
1797 gtk_tool_item_group_animation_cb (gpointer data)
1798 {
1799   GtkToolItemGroup *group = GTK_TOOL_ITEM_GROUP (data);
1800   GtkToolItemGroupPrivate* priv = group->priv;
1801   gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
1802   gboolean retval;
1803
1804   GDK_THREADS_ENTER ();
1805
1806   /* Enque this early to reduce number of expose events. */
1807   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (group));
1808
1809   /* Figure out current style of the expander arrow. */
1810   if (priv->collapsed)
1811     {
1812       if (priv->expander_style == GTK_EXPANDER_EXPANDED)
1813         priv->expander_style = GTK_EXPANDER_SEMI_COLLAPSED;
1814       else
1815         priv->expander_style = GTK_EXPANDER_COLLAPSED;
1816     }
1817   else
1818     {
1819       if (priv->expander_style == GTK_EXPANDER_COLLAPSED)
1820         priv->expander_style = GTK_EXPANDER_SEMI_EXPANDED;
1821       else
1822         priv->expander_style = GTK_EXPANDER_EXPANDED;
1823     }
1824
1825   gtk_tool_item_group_force_expose (group);
1826
1827   /* Finish animation when done. */
1828   if (timestamp >= ANIMATION_DURATION)
1829     priv->animation_timeout = NULL;
1830
1831   retval = (priv->animation_timeout != NULL);
1832
1833   GDK_THREADS_LEAVE ();
1834
1835   return retval;
1836 }
1837
1838 /**
1839  * gtk_tool_item_group_set_collapsed:
1840  * @group: a #GtkToolItemGroup
1841  * @collapsed: whether the @group should be collapsed or expanded
1842  *
1843  * Sets whether the @group should be collapsed or expanded.
1844  *
1845  * Since: 2.20
1846  */
1847 void
1848 gtk_tool_item_group_set_collapsed (GtkToolItemGroup *group,
1849                                    gboolean          collapsed)
1850 {
1851   GtkWidget *parent;
1852   GtkToolItemGroupPrivate* priv;
1853
1854   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1855
1856   priv = group->priv;
1857
1858   parent = gtk_widget_get_parent (GTK_WIDGET (group));
1859   if (GTK_IS_TOOL_PALETTE (parent) && !collapsed)
1860     _gtk_tool_palette_set_expanding_child (GTK_TOOL_PALETTE (parent),
1861                                            GTK_WIDGET (group));
1862   if (collapsed != priv->collapsed)
1863     {
1864       if (priv->animation)
1865         {
1866           GTimeVal now;
1867
1868           g_get_current_time (&now);
1869
1870           if (priv->animation_timeout)
1871             g_source_destroy (priv->animation_timeout);
1872
1873           priv->animation_start = (now.tv_sec * G_USEC_PER_SEC + now.tv_usec);
1874           priv->animation_timeout = g_timeout_source_new (ANIMATION_TIMEOUT);
1875
1876           g_source_set_callback (priv->animation_timeout,
1877                                  gtk_tool_item_group_animation_cb,
1878                                  group, NULL);
1879
1880           g_source_attach (priv->animation_timeout, NULL);
1881         }
1882         else
1883         {
1884           priv->expander_style = GTK_EXPANDER_COLLAPSED;
1885           gtk_tool_item_group_force_expose (group);
1886         }
1887
1888       priv->collapsed = collapsed;
1889       g_object_notify (G_OBJECT (group), "collapsed");
1890     }
1891 }
1892
1893 /**
1894  * gtk_tool_item_group_set_ellipsize:
1895  * @group: a #GtkToolItemGroup
1896  * @ellipsize: the #PangoEllipsizeMode labels in @group should use
1897  *
1898  * Sets the ellipsization mode which should be used by labels in @group.
1899  *
1900  * Since: 2.20
1901  */
1902 void
1903 gtk_tool_item_group_set_ellipsize (GtkToolItemGroup   *group,
1904                                    PangoEllipsizeMode  ellipsize)
1905 {
1906   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1907
1908   if (ellipsize != group->priv->ellipsize)
1909     {
1910       group->priv->ellipsize = ellipsize;
1911       gtk_tool_item_group_header_adjust_style (group);
1912       g_object_notify (G_OBJECT (group), "ellipsize");
1913       _gtk_tool_item_group_palette_reconfigured (group);
1914     }
1915 }
1916
1917 /**
1918  * gtk_tool_item_group_get_label:
1919  * @group: a #GtkToolItemGroup
1920  *
1921  * Gets the label of @group.
1922  *
1923  * Returns: the label of @group. The label is an internal string of @group
1924  *     and must not be modified. Note that %NULL is returned if a custom
1925  *     label has been set with gtk_tool_item_group_set_label_widget()
1926  *
1927  * Since: 2.20
1928  */
1929 G_CONST_RETURN gchar*
1930 gtk_tool_item_group_get_label (GtkToolItemGroup *group)
1931 {
1932   GtkToolItemGroupPrivate *priv;
1933
1934   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
1935
1936   priv = group->priv;
1937
1938   if (GTK_IS_LABEL (priv->label_widget))
1939     return gtk_label_get_label (GTK_LABEL (priv->label_widget));
1940   else
1941     return NULL;
1942 }
1943
1944 /**
1945  * gtk_tool_item_group_get_label_widget:
1946  * @group: a #GtkToolItemGroup
1947  *
1948  * Gets the label widget of @group.
1949  * See gtk_tool_item_group_set_label_widget().
1950  *
1951  * Returns: the label widget of @group
1952  *
1953  * Since: 2.20
1954  */
1955 GtkWidget*
1956 gtk_tool_item_group_get_label_widget (GtkToolItemGroup *group)
1957 {
1958   GtkWidget *alignment = gtk_tool_item_group_get_alignment (group);
1959
1960   return gtk_bin_get_child (GTK_BIN (alignment));
1961 }
1962
1963 /**
1964  * gtk_tool_item_group_get_collapsed:
1965  * @group: a GtkToolItemGroup
1966  *
1967  * Gets whether @group is collapsed or expanded.
1968  *
1969  * Returns: %TRUE if @group is collapsed, %FALSE if it is expanded
1970  *
1971  * Since: 2.20
1972  */
1973 gboolean
1974 gtk_tool_item_group_get_collapsed (GtkToolItemGroup *group)
1975 {
1976   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_COLLAPSED);
1977
1978   return group->priv->collapsed;
1979 }
1980
1981 /**
1982  * gtk_tool_item_group_get_ellipsize:
1983  * @group: a #GtkToolItemGroup
1984  *
1985  * Gets the ellipsization mode of @group.
1986  *
1987  * Returns: the #PangoEllipsizeMode of @group
1988  *
1989  * Since: 2.20
1990  */
1991 PangoEllipsizeMode
1992 gtk_tool_item_group_get_ellipsize (GtkToolItemGroup *group)
1993 {
1994   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_ELLIPSIZE);
1995
1996   return group->priv->ellipsize;
1997 }
1998
1999 /**
2000  * gtk_tool_item_group_get_header_relief:
2001  * @group: a #GtkToolItemGroup
2002  *
2003  * Gets the relief mode of the header button of @group.
2004  *
2005  * Returns: the #GtkReliefStyle
2006  *
2007  * Since: 2.20
2008  */
2009 GtkReliefStyle
2010 gtk_tool_item_group_get_header_relief (GtkToolItemGroup   *group)
2011 {
2012   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), GTK_RELIEF_NORMAL);
2013
2014   return gtk_button_get_relief (GTK_BUTTON (group->priv->header));
2015 }
2016
2017 /**
2018  * gtk_tool_item_group_insert:
2019  * @group: a #GtkToolItemGroup
2020  * @item: the #GtkToolItem to insert into @group
2021  * @position: the position of @item in @group, starting with 0.
2022  *     The position -1 means end of list.
2023  *
2024  * Inserts @item at @position in the list of children of @group.
2025  *
2026  * Since: 2.20
2027  */
2028 void
2029 gtk_tool_item_group_insert (GtkToolItemGroup *group,
2030                             GtkToolItem      *item,
2031                             gint              position)
2032 {
2033   GtkWidget *parent, *child_widget;
2034   GtkToolItemGroupChild *child;
2035
2036   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2037   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2038   g_return_if_fail (position >= -1);
2039
2040   parent = gtk_widget_get_parent (GTK_WIDGET (group));
2041
2042   child = g_new (GtkToolItemGroupChild, 1);
2043   child->item = g_object_ref_sink (item);
2044   child->homogeneous = TRUE;
2045   child->expand = FALSE;
2046   child->fill = TRUE;
2047   child->new_row = FALSE;
2048
2049   group->priv->children = g_list_insert (group->priv->children, child, position);
2050
2051   if (GTK_IS_TOOL_PALETTE (parent))
2052     _gtk_tool_palette_child_set_drag_source (GTK_WIDGET (item), parent);
2053
2054   child_widget = gtk_bin_get_child (GTK_BIN (item));
2055
2056   if (GTK_IS_BUTTON (child_widget))
2057     gtk_button_set_focus_on_click (GTK_BUTTON (child_widget), TRUE);
2058
2059   gtk_widget_set_parent (GTK_WIDGET (item), GTK_WIDGET (group));
2060 }
2061
2062 /**
2063  * gtk_tool_item_group_set_item_position:
2064  * @group: a #GtkToolItemGroup
2065  * @item: the #GtkToolItem to move to a new position, should
2066  *     be a child of @group.
2067  * @position: the new position of @item in @group, starting with 0.
2068  *     The position -1 means end of list.
2069  *
2070  * Sets the position of @item in the list of children of @group.
2071  *
2072  * Since: 2.20
2073  */
2074 void
2075 gtk_tool_item_group_set_item_position (GtkToolItemGroup *group,
2076                                        GtkToolItem      *item,
2077                                        gint              position)
2078 {
2079   gint old_position;
2080   GList *link;
2081   GtkToolItemGroupChild *child;
2082   GtkToolItemGroupPrivate* priv;
2083
2084   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2085   g_return_if_fail (GTK_IS_TOOL_ITEM (item));
2086   g_return_if_fail (position >= -1);
2087
2088   child = gtk_tool_item_group_get_child (group, item, &old_position, &link);
2089   priv = group->priv;
2090
2091   g_return_if_fail (child != NULL);
2092
2093   if (position == old_position)
2094     return;
2095
2096   priv->children = g_list_delete_link (priv->children, link);
2097   priv->children = g_list_insert (priv->children, child, position);
2098
2099   gtk_widget_child_notify (GTK_WIDGET (item), "position");
2100   if (GTK_WIDGET_VISIBLE (group) && GTK_WIDGET_VISIBLE (item))
2101     gtk_widget_queue_resize (GTK_WIDGET (group));
2102 }
2103
2104 /**
2105  * gtk_tool_item_group_get_item_position:
2106  * @group: a #GtkToolItemGroup
2107  * @item: a #GtkToolItem
2108  *
2109  * Gets the position of @item in @group as index.
2110  *
2111  * Returns: the index of @item in @group or -1 if @item is no child of @group
2112  *
2113  * Since: 2.20
2114  */
2115 gint
2116 gtk_tool_item_group_get_item_position (GtkToolItemGroup *group,
2117                                        GtkToolItem      *item)
2118 {
2119   gint position;
2120
2121   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
2122   g_return_val_if_fail (GTK_IS_TOOL_ITEM (item), -1);
2123
2124   if (gtk_tool_item_group_get_child (group, item, &position, NULL))
2125     return position;
2126
2127   return -1;
2128 }
2129
2130 /**
2131  * gtk_tool_item_group_get_n_items:
2132  * @group: a #GtkToolItemGroup
2133  *
2134  * Gets the number of tool items in @group.
2135  *
2136  * Returns: the number of tool items in @group
2137  *
2138  * Since: 2.20
2139  */
2140 guint
2141 gtk_tool_item_group_get_n_items (GtkToolItemGroup *group)
2142 {
2143   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), 0);
2144
2145   return g_list_length (group->priv->children);
2146 }
2147
2148 /**
2149  * gtk_tool_item_group_get_nth_item:
2150  * @group: a #GtkToolItemGroup
2151  * @index: the index
2152  *
2153  * Gets the tool item at @index in group.
2154  *
2155  * Returns: the #GtkToolItem at index
2156  *
2157  * Since: 2.20
2158  */
2159 GtkToolItem*
2160 gtk_tool_item_group_get_nth_item (GtkToolItemGroup *group,
2161                                   guint             index)
2162 {
2163   GtkToolItemGroupChild *child;
2164
2165   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2166
2167   child = g_list_nth_data (group->priv->children, index);
2168
2169   return child != NULL ? child->item : NULL;
2170 }
2171
2172 /**
2173  * gtk_tool_item_group_get_drop_item:
2174  * @group: a #GtkToolItemGroup
2175  * @x: the x position
2176  * @y: the y position
2177  *
2178  * Gets the tool item at position (x, y).
2179  *
2180  * Returns: the #GtkToolItem at position (x, y)
2181  *
2182  * Since: 2.20
2183  */
2184 GtkToolItem*
2185 gtk_tool_item_group_get_drop_item (GtkToolItemGroup *group,
2186                                    gint              x,
2187                                    gint              y)
2188 {
2189   GtkAllocation *allocation;
2190   GtkOrientation orientation;
2191   GList *it;
2192
2193   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), NULL);
2194
2195   allocation = &GTK_WIDGET (group)->allocation;
2196   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2197
2198   g_return_val_if_fail (x >= 0 && x < allocation->width, NULL);
2199   g_return_val_if_fail (y >= 0 && y < allocation->height, NULL);
2200
2201   for (it = group->priv->children; it != NULL; it = it->next)
2202     {
2203       GtkToolItemGroupChild *child = it->data;
2204       GtkToolItem *item = child->item;
2205       gint x0, y0;
2206
2207       if (!item || !gtk_tool_item_group_is_item_visible (group, child))
2208         continue;
2209
2210       allocation = &GTK_WIDGET (item)->allocation;
2211
2212       x0 = x - allocation->x;
2213       y0 = y - allocation->y;
2214
2215       if (x0 >= 0 && x0 < allocation->width &&
2216           y0 >= 0 && y0 < allocation->height)
2217         return item;
2218     }
2219
2220   return NULL;
2221 }
2222
2223 void
2224 _gtk_tool_item_group_item_size_request (GtkToolItemGroup *group,
2225                                         GtkRequisition   *item_size,
2226                                         gboolean          homogeneous_only,
2227                                         gint             *requested_rows)
2228 {
2229   GtkRequisition child_requisition;
2230   GList *it;
2231   gint rows = 0;
2232   gboolean new_row = TRUE;
2233   GtkOrientation orientation;
2234   GtkToolbarStyle style;
2235
2236   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
2237   g_return_if_fail (NULL != item_size);
2238
2239   orientation = gtk_tool_shell_get_orientation (GTK_TOOL_SHELL (group));
2240   style = gtk_tool_shell_get_style (GTK_TOOL_SHELL (group));
2241
2242   item_size->width = item_size->height = 0;
2243
2244   for (it = group->priv->children; it != NULL; it = it->next)
2245     {
2246       GtkToolItemGroupChild *child = it->data;
2247
2248       if (!gtk_tool_item_group_is_item_visible (group, child))
2249         continue;
2250
2251       if (child->new_row || new_row)
2252         {
2253           rows++;
2254           new_row = FALSE;
2255         }
2256
2257       if (!child->homogeneous && child->expand)
2258           new_row = TRUE;
2259
2260       gtk_widget_size_request (GTK_WIDGET (child->item), &child_requisition);
2261
2262       if (!homogeneous_only || child->homogeneous)
2263         item_size->width = MAX (item_size->width, child_requisition.width);
2264       item_size->height = MAX (item_size->height, child_requisition.height);
2265     }
2266
2267   if (requested_rows)
2268     *requested_rows = rows;
2269 }
2270
2271 void
2272 _gtk_tool_item_group_paint (GtkToolItemGroup *group,
2273                             cairo_t          *cr)
2274 {
2275   GtkWidget *widget = GTK_WIDGET (group);
2276   GtkToolItemGroupPrivate* priv = group->priv;
2277
2278   gdk_cairo_set_source_pixmap (cr, widget->window,
2279                                widget->allocation.x,
2280                                widget->allocation.y);
2281
2282   if (priv->animation_timeout)
2283     {
2284       GtkOrientation orientation = gtk_tool_item_group_get_orientation (GTK_TOOL_SHELL (group));
2285       cairo_pattern_t *mask;
2286       gdouble v0, v1;
2287
2288       if (GTK_ORIENTATION_VERTICAL == orientation)
2289         v1 = widget->allocation.height;
2290       else
2291         v1 = widget->allocation.width;
2292
2293       v0 = v1 - 256;
2294
2295       if (!GTK_WIDGET_VISIBLE (priv->header))
2296         v0 = MAX (v0, 0);
2297       else if (GTK_ORIENTATION_VERTICAL == orientation)
2298         v0 = MAX (v0, priv->header->allocation.height);
2299       else
2300         v0 = MAX (v0, priv->header->allocation.width);
2301
2302       v1 = MIN (v0 + 256, v1);
2303
2304       if (GTK_ORIENTATION_VERTICAL == orientation)
2305         {
2306           v0 += widget->allocation.y;
2307           v1 += widget->allocation.y;
2308
2309           mask = cairo_pattern_create_linear (0.0, v0, 0.0, v1);
2310         }
2311       else
2312         {
2313           v0 += widget->allocation.x;
2314           v1 += widget->allocation.x;
2315
2316           mask = cairo_pattern_create_linear (v0, 0.0, v1, 0.0);
2317         }
2318
2319       cairo_pattern_add_color_stop_rgba (mask, 0.00, 0.0, 0.0, 0.0, 1.00);
2320       cairo_pattern_add_color_stop_rgba (mask, 0.25, 0.0, 0.0, 0.0, 0.25);
2321       cairo_pattern_add_color_stop_rgba (mask, 0.50, 0.0, 0.0, 0.0, 0.10);
2322       cairo_pattern_add_color_stop_rgba (mask, 0.75, 0.0, 0.0, 0.0, 0.01);
2323       cairo_pattern_add_color_stop_rgba (mask, 1.00, 0.0, 0.0, 0.0, 0.00);
2324
2325       cairo_mask (cr, mask);
2326       cairo_pattern_destroy (mask);
2327     }
2328   else
2329     cairo_paint (cr);
2330 }
2331
2332 gint
2333 _gtk_tool_item_group_get_size_for_limit (GtkToolItemGroup *group,
2334                                          gint              limit,
2335                                          gboolean          vertical,
2336                                          gboolean          animation)
2337 {
2338   GtkRequisition requisition;
2339   GtkToolItemGroupPrivate* priv = group->priv;
2340
2341   gtk_widget_size_request (GTK_WIDGET (group), &requisition);
2342
2343   if (!priv->collapsed || priv->animation_timeout)
2344     {
2345       GtkAllocation allocation = { 0, 0, requisition.width, requisition.height };
2346       GtkRequisition inquery;
2347
2348       if (vertical)
2349         allocation.width = limit;
2350       else
2351         allocation.height = limit;
2352
2353       gtk_tool_item_group_real_size_query (GTK_WIDGET (group),
2354                                            &allocation, &inquery);
2355
2356       if (vertical)
2357         inquery.height -= requisition.height;
2358       else
2359         inquery.width -= requisition.width;
2360
2361       if (priv->animation_timeout && animation)
2362         {
2363           gint64 timestamp = gtk_tool_item_group_get_animation_timestamp (group);
2364
2365           timestamp = MIN (timestamp, ANIMATION_DURATION);
2366
2367           if (priv->collapsed)
2368             timestamp = ANIMATION_DURATION - timestamp;
2369
2370           if (vertical)
2371             {
2372               inquery.height *= timestamp;
2373               inquery.height /= ANIMATION_DURATION;
2374             }
2375           else
2376             {
2377               inquery.width *= timestamp;
2378               inquery.width /= ANIMATION_DURATION;
2379             }
2380         }
2381
2382       if (vertical)
2383         requisition.height += inquery.height;
2384       else
2385         requisition.width += inquery.width;
2386     }
2387
2388   return (vertical ? requisition.height : requisition.width);
2389 }
2390
2391 gint
2392 _gtk_tool_item_group_get_height_for_width (GtkToolItemGroup *group,
2393                                            gint              width)
2394 {
2395   return _gtk_tool_item_group_get_size_for_limit (group, width, TRUE, group->priv->animation);
2396 }
2397
2398 gint
2399 _gtk_tool_item_group_get_width_for_height (GtkToolItemGroup *group,
2400                                            gint              height)
2401 {
2402   return _gtk_tool_item_group_get_size_for_limit (group, height, FALSE, TRUE);
2403 }
2404
2405 static void
2406 gtk_tool_palette_reconfigured_foreach_item (GtkWidget *child,
2407                                             gpointer   data)
2408 {
2409   if (GTK_IS_TOOL_ITEM (child))
2410     gtk_tool_item_toolbar_reconfigured (GTK_TOOL_ITEM (child));
2411 }
2412
2413
2414 void
2415 _gtk_tool_item_group_palette_reconfigured (GtkToolItemGroup *group)
2416 {
2417   gtk_container_foreach (GTK_CONTAINER (group),
2418                          gtk_tool_palette_reconfigured_foreach_item,
2419                          NULL);
2420
2421   gtk_tool_item_group_header_adjust_style (group);
2422 }
2423
2424
2425 #define __GTK_TOOL_ITEM_GROUP_C__
2426 #include "gtkaliasdef.c"