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