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