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