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