]> Pileus Git - ~andy/gtk/blob - gtk/gtktoolpalette.c
GtkToolPalette: Use gtk-toolbar-style and gtk-toolbar-icon-size settings.
[~andy/gtk] / gtk / gtktoolpalette.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  */
21
22 #include "gtktoolpaletteprivate.h"
23 #include "gtkmarshalers.h"
24
25 #include <gtk/gtk.h>
26 #include <string.h>
27
28 #define DEFAULT_ICON_SIZE       GTK_ICON_SIZE_SMALL_TOOLBAR
29 #define DEFAULT_ORIENTATION     GTK_ORIENTATION_VERTICAL
30 #define DEFAULT_TOOLBAR_STYLE   GTK_TOOLBAR_ICONS
31
32 #define DEFAULT_CHILD_EXCLUSIVE FALSE
33 #define DEFAULT_CHILD_EXPAND    FALSE
34
35 #define P_(msgid) (msgid)
36
37 /**
38  * SECTION:GtkToolPalette
39  * @short_description: A tool palette with categories
40  * @include: gtktoolpalette.h
41  *
42  * An #GtkToolPalette allows it to add #GtkToolItem<!-- -->s to a palette like container
43  * with different categories and drag and drop support.
44  *
45  * An #GtkToolPalette is created with a call to gtk_tool_palette_new().
46  *
47  * #GtkToolItem<!-- -->s cannot be added directly to an #GtkToolPalette, instead they
48  * are added to an #GtkToolItemGroup which can than be added to an #GtkToolPalette. To add
49  * an #GtkToolItemGroup to an #GtkToolPalette use gtk_container_add().
50  *
51  * |[
52  * GtkWidget *palette, *group;
53  * GtkToolItem *item;
54  *
55  * palette = gtk_tool_palette_new ();
56  * group = gtk_tool_item_group_new (_("Test Category"));
57  * gtk_container_add (GTK_CONTAINER (palette), group);
58  *
59  * item = gtk_tool_button_new_from_stock (GTK_STOCK_OK);
60  * gtk_tool_item_group_insert (GTK_TOOL_ITEM_GROUP (group), item, -1);
61  * ]|
62  *
63  * The easiest way to use drag and drop with GtkToolPalette is to call gtk_tool_palette_add_drag_dest()
64  * with the desired drag source @palette and the desired drag target @widget. Than gtk_tool_palette_get_drag_item()
65  * can be used to get the dragged item in the #GtkWidget::drag-data-received signal handler of the drag target.
66  *
67  * |[
68  * static void
69  * passive_canvas_drag_data_received (GtkWidget        *widget,
70  *                                    GdkDragContext   *context,
71  *                                    gint              x,
72  *                                    gint              y,
73  *                                    GtkSelectionData *selection,
74  *                                    guint             info,
75  *                                    guint             time,
76  *                                    gpointer          data)
77  * {
78  *   GtkWidget *palette;
79  *   GtkWidget *item;
80  *
81  *   /<!-- -->* Get the dragged item *<!-- -->/
82  *   palette = gtk_widget_get_ancestor (gtk_drag_get_source_widget (context), GTK_TYPE_TOOL_PALETTE);
83  *   if (palette != NULL)
84  *     item = gtk_tool_palette_get_drag_item (GTK_TOOL_PALETTE (palette), selection);
85  *
86  *   /<!-- -->* Do something with item *<!-- -->/
87  * }
88  *
89  * GtkWidget *target, palette;
90  *
91  * palette = gtk_tool_palette_new ();
92  * target = gtk_drawing_area_new ();
93  *
94  * g_signal_connect (G_OBJECT (target), "drag-data-received",
95  *                   G_CALLBACK (passive_canvas_drag_data_received), NULL);   
96  * gtk_tool_palette_add_drag_dest (GTK_TOOL_PALETTE (palette), target,
97  *                                 GTK_DEST_DEFAULT_ALL,
98  *                                 GTK_TOOL_PALETTE_DRAG_ITEMS,
99  *                                 GDK_ACTION_COPY);
100  * ]|
101  *
102  * Since: 2.18
103  */
104
105 typedef struct _GtkToolItemGroupInfo   GtkToolItemGroupInfo;
106 typedef struct _GtkToolPaletteDragData GtkToolPaletteDragData;
107
108 enum
109 {
110   PROP_NONE,
111   PROP_ICON_SIZE,
112   PROP_ICON_SIZE_SET,
113   PROP_ORIENTATION,
114   PROP_TOOLBAR_STYLE,
115 };
116
117 enum
118 {
119   CHILD_PROP_NONE,
120   CHILD_PROP_EXCLUSIVE,
121   CHILD_PROP_EXPAND,
122 };
123
124 struct _GtkToolItemGroupInfo
125 {
126   GtkToolItemGroup *widget;
127
128   guint             notify_collapsed;
129   guint             exclusive : 1;
130   guint             expand : 1;
131 };
132
133 struct _GtkToolPalettePrivate
134 {
135   GtkToolItemGroupInfo *groups;
136   gsize                 groups_size;
137   gsize                 groups_length;
138
139   GtkAdjustment        *hadjustment;
140   GtkAdjustment        *vadjustment;
141
142   GtkIconSize           icon_size;
143   gboolean              icon_size_set;
144   GtkOrientation        orientation;
145   GtkToolbarStyle       style;
146   gboolean              style_set;
147
148   GtkWidget            *expanding_child;
149
150   GtkSizeGroup         *text_size_group;
151   
152   GtkSettings       *settings;
153   gulong             settings_connection;
154
155   guint                 sparse_groups : 1;
156   guint                 drag_source : 2;
157 };
158
159 struct _GtkToolPaletteDragData
160 {
161   GtkToolPalette *palette;
162   GtkWidget      *item;
163 };
164
165 static GdkAtom dnd_target_atom_item = GDK_NONE;
166 static GdkAtom dnd_target_atom_group = GDK_NONE;
167
168 static const GtkTargetEntry dnd_targets[] =
169 {
170   { "application/x-GTK-tool-palette-item", GTK_TARGET_SAME_APP, 0 },
171   { "application/x-GTK-tool-palette-group", GTK_TARGET_SAME_APP, 0 },
172 };
173
174 G_DEFINE_TYPE_WITH_CODE (GtkToolPalette,
175                gtk_tool_palette,
176                GTK_TYPE_CONTAINER,
177                G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL));
178
179 static void
180 gtk_tool_palette_init (GtkToolPalette *palette)
181 {
182   palette->priv = G_TYPE_INSTANCE_GET_PRIVATE (palette,
183                                                GTK_TYPE_TOOL_PALETTE,
184                                                GtkToolPalettePrivate);
185
186   palette->priv->groups_size = 4;
187   palette->priv->groups_length = 0;
188   palette->priv->groups = g_new0 (GtkToolItemGroupInfo, palette->priv->groups_size);
189
190   palette->priv->icon_size = DEFAULT_ICON_SIZE;
191   palette->priv->icon_size_set = FALSE;
192   palette->priv->orientation = DEFAULT_ORIENTATION;
193   palette->priv->style = DEFAULT_TOOLBAR_STYLE;
194   palette->priv->style_set = FALSE;
195
196   palette->priv->text_size_group = gtk_size_group_new (GTK_SIZE_GROUP_BOTH);
197 }
198
199 static void
200 gtk_tool_palette_reconfigured (GtkToolPalette *palette)
201 {
202   guint i;
203
204   for (i = 0; i < palette->priv->groups_length; ++i)
205     {
206       if (palette->priv->groups[i].widget)
207         _gtk_tool_item_group_palette_reconfigured (palette->priv->groups[i].widget);
208     }
209
210   gtk_widget_queue_resize_no_redraw (GTK_WIDGET (palette));
211 }
212
213 static void
214 gtk_tool_palette_set_property (GObject      *object,
215                                guint         prop_id,
216                                const GValue *value,
217                                GParamSpec   *pspec)
218 {
219   GtkToolPalette *palette = GTK_TOOL_PALETTE (object);
220
221   switch (prop_id)
222     {
223       case PROP_ICON_SIZE:
224         if ((guint) g_value_get_enum (value) != palette->priv->icon_size)
225           {
226             palette->priv->icon_size = g_value_get_enum (value);
227             gtk_tool_palette_reconfigured (palette);
228           }
229         break;
230         
231       case PROP_ICON_SIZE_SET:
232         if ((guint) g_value_get_enum (value) != palette->priv->icon_size)
233           {
234             palette->priv->icon_size_set = g_value_get_enum (value);
235             gtk_tool_palette_reconfigured (palette);
236           }
237         break;
238
239       case PROP_ORIENTATION:
240         if ((guint) g_value_get_enum (value) != palette->priv->orientation)
241           {
242             palette->priv->orientation = g_value_get_enum (value);
243             gtk_tool_palette_reconfigured (palette);
244           }
245         break;
246
247       case PROP_TOOLBAR_STYLE:
248         if ((guint) g_value_get_enum (value) != palette->priv->style)
249           {
250             palette->priv->style = g_value_get_enum (value);
251             gtk_tool_palette_reconfigured (palette);
252           }
253         break;
254
255       default:
256         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257         break;
258     }
259 }
260
261 static void
262 gtk_tool_palette_get_property (GObject    *object,
263                                guint       prop_id,
264                                GValue     *value,
265                                GParamSpec *pspec)
266 {
267   GtkToolPalette *palette = GTK_TOOL_PALETTE (object);
268
269   switch (prop_id)
270     {
271       case PROP_ICON_SIZE:
272         g_value_set_enum (value, gtk_tool_palette_get_icon_size (palette));
273         break;
274         
275       case PROP_ICON_SIZE_SET:
276         g_value_set_boolean (value, palette->priv->icon_size_set);
277         break;
278
279       case PROP_ORIENTATION:
280         g_value_set_enum (value, palette->priv->orientation);
281         break;
282
283       case PROP_TOOLBAR_STYLE:
284         g_value_set_enum (value, gtk_tool_palette_get_style (palette));
285         break;
286
287       default:
288         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289         break;
290     }
291 }
292
293 static void
294 gtk_tool_palette_dispose (GObject *object)
295 {
296   GtkToolPalette *palette = GTK_TOOL_PALETTE (object);
297   guint i;
298
299   if (palette->priv->hadjustment)
300     {
301       g_object_unref (palette->priv->hadjustment);
302       palette->priv->hadjustment = NULL;
303     }
304
305   if (palette->priv->vadjustment)
306     {
307       g_object_unref (palette->priv->vadjustment);
308       palette->priv->vadjustment = NULL;
309     }
310
311   for (i = 0; i < palette->priv->groups_size; ++i)
312     {
313       GtkToolItemGroupInfo *group = &palette->priv->groups[i];
314
315       if (group->notify_collapsed)
316         {
317           g_signal_handler_disconnect (group->widget, group->notify_collapsed);
318           group->notify_collapsed = 0;
319         }
320     }
321
322   if (palette->priv->text_size_group)
323     {
324       g_object_unref (palette->priv->text_size_group);
325       palette->priv->text_size_group = NULL;
326     }
327
328   G_OBJECT_CLASS (gtk_tool_palette_parent_class)->dispose (object);
329 }
330
331 static void
332 gtk_tool_palette_finalize (GObject *object)
333 {
334   GtkToolPalette *palette = GTK_TOOL_PALETTE (object);
335
336   if (palette->priv->groups)
337     {
338       palette->priv->groups_length = 0;
339       g_free (palette->priv->groups);
340       palette->priv->groups = NULL;
341     }
342
343   G_OBJECT_CLASS (gtk_tool_palette_parent_class)->finalize (object);
344 }
345
346 static void
347 gtk_tool_palette_size_request (GtkWidget      *widget,
348                                GtkRequisition *requisition)
349 {
350   const gint border_width = GTK_CONTAINER (widget)->border_width;
351   GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
352   GtkRequisition child_requisition;
353   guint i;
354
355   requisition->width = 0;
356   requisition->height = 0;
357
358   for (i = 0; i < palette->priv->groups_length; ++i)
359     {
360       GtkToolItemGroupInfo *group = &palette->priv->groups[i];
361
362       if (!group->widget)
363         continue;
364
365       gtk_widget_size_request (GTK_WIDGET (group->widget), &child_requisition);
366
367       if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
368         {
369           requisition->width = MAX (requisition->width, child_requisition.width);
370           requisition->height += child_requisition.height;
371         }
372       else
373         {
374           requisition->width += child_requisition.width;
375           requisition->height = MAX (requisition->height, child_requisition.height);
376         }
377     }
378
379   requisition->width += border_width * 2;
380   requisition->height += border_width * 2;
381 }
382
383 static void
384 gtk_tool_palette_size_allocate (GtkWidget     *widget,
385                                 GtkAllocation *allocation)
386 {
387   const gint border_width = GTK_CONTAINER (widget)->border_width;
388   GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
389   GtkAdjustment *adjustment = NULL;
390   GtkAllocation child_allocation;
391
392   gint n_expand_groups = 0;
393   gint remaining_space = 0;
394   gint expand_space = 0;
395
396   gint page_start, page_size = 0;
397   gint offset = 0;
398   guint i;
399
400   gint min_offset = -1, max_offset = -1;
401
402   gint x;
403
404   gint *group_sizes = g_newa(gint, palette->priv->groups_length);
405
406   GtkTextDirection direction = gtk_widget_get_direction (widget);
407
408   GTK_WIDGET_CLASS (gtk_tool_palette_parent_class)->size_allocate (widget, allocation);
409
410   if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
411     {
412       adjustment = palette->priv->vadjustment;
413       page_size = allocation->height;
414     }
415   else
416     {
417       adjustment = palette->priv->hadjustment;
418       page_size = allocation->width;
419     }
420
421   if (adjustment)
422     offset = gtk_adjustment_get_value (adjustment);
423   if (GTK_ORIENTATION_HORIZONTAL == palette->priv->orientation &&
424       GTK_TEXT_DIR_RTL == direction)
425     offset = -offset;
426
427   if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
428     child_allocation.width = allocation->width - border_width * 2;
429   else
430     child_allocation.height = allocation->height - border_width * 2;
431
432   if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
433     remaining_space = allocation->height;
434   else
435     remaining_space = allocation->width;
436
437   /* figure out the required size of all groups to be able to distribute the
438    * remaining space on allocation */
439   for (i = 0; i < palette->priv->groups_length; ++i)
440     {
441       GtkToolItemGroupInfo *group = &palette->priv->groups[i];
442       gint size;
443
444       if (!group->widget)
445         continue;
446
447       widget = GTK_WIDGET (group->widget);
448
449       if (gtk_tool_item_group_get_n_items (group->widget))
450         {
451           if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
452             size = _gtk_tool_item_group_get_height_for_width (group->widget, child_allocation.width);
453           else
454             size = _gtk_tool_item_group_get_width_for_height (group->widget, child_allocation.height);
455
456           if (group->expand && !gtk_tool_item_group_get_collapsed (group->widget))
457             n_expand_groups += 1;
458         }
459       else
460         size = 0;
461
462       remaining_space -= size;
463       group_sizes[i] = size;
464
465       /* if the widget is currently expanding an offset which allows to display as much of the
466        * widget as possible is calculated */
467       if (widget == palette->priv->expanding_child)
468         {
469           gint limit =
470             GTK_ORIENTATION_VERTICAL == palette->priv->orientation ?
471             child_allocation.width : child_allocation.height;
472
473           gint real_size;
474           guint j;
475
476           min_offset = 0;
477
478           for (j = 0; j < i; ++j)
479             min_offset += group_sizes[j];
480
481           max_offset = min_offset + group_sizes[i];
482
483           real_size = _gtk_tool_item_group_get_size_for_limit
484             (GTK_TOOL_ITEM_GROUP (widget), limit,
485              GTK_ORIENTATION_VERTICAL == palette->priv->orientation,
486              FALSE);
487
488           if (size == real_size)
489             palette->priv->expanding_child = NULL;
490         }
491     }
492
493   if (n_expand_groups > 0)
494     {
495       remaining_space = MAX (0, remaining_space);
496       expand_space = remaining_space / n_expand_groups;
497     }
498
499   if (max_offset != -1)
500     {
501       gint limit =
502         GTK_ORIENTATION_VERTICAL == palette->priv->orientation ?
503         allocation->height : allocation->width;
504
505       offset = MIN (MAX (offset, max_offset - limit), min_offset);
506     }
507
508   if (remaining_space > 0)
509     offset = 0;
510
511   x = border_width;
512   child_allocation.y = border_width;
513
514   if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
515     child_allocation.y -= offset;
516   else
517     x -= offset;
518
519   /* allocate all groups at the calculated positions */
520   for (i = 0; i < palette->priv->groups_length; ++i)
521     {
522       GtkToolItemGroupInfo *group = &palette->priv->groups[i];
523       GtkWidget *widget;
524
525       if (!group->widget)
526         continue;
527
528       widget = GTK_WIDGET (group->widget);
529
530       if (gtk_tool_item_group_get_n_items (group->widget))
531         {
532           gint size = group_sizes[i];
533
534           if (group->expand && !gtk_tool_item_group_get_collapsed (group->widget))
535             {
536               size += MIN (expand_space, remaining_space);
537               remaining_space -= expand_space;
538             }
539
540           if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
541             child_allocation.height = size;
542           else
543             child_allocation.width = size;
544
545           if (GTK_ORIENTATION_HORIZONTAL == palette->priv->orientation &&
546               GTK_TEXT_DIR_RTL == direction)
547             child_allocation.x = allocation->width - x - child_allocation.width;
548           else
549             child_allocation.x = x;
550
551           gtk_widget_size_allocate (widget, &child_allocation);
552           gtk_widget_show (widget);
553
554           if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
555             child_allocation.y += child_allocation.height;
556           else
557             x += child_allocation.width;
558         }
559       else
560         gtk_widget_hide (widget);
561     }
562
563   if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation)
564     {
565       child_allocation.y += border_width;
566       child_allocation.y += offset;
567
568       page_start = child_allocation.y;
569     }
570   else
571     {
572       x += border_width;
573       x += offset;
574
575       page_start = x;
576     }
577
578   /* update the scrollbar to match the displayed adjustment */
579   if (adjustment)
580     {
581       gdouble value;
582
583       adjustment->page_increment = page_size * 0.9;
584       adjustment->step_increment = page_size * 0.1;
585       adjustment->page_size = page_size;
586
587       if (GTK_ORIENTATION_VERTICAL == palette->priv->orientation ||
588           GTK_TEXT_DIR_LTR == direction)
589         {
590           adjustment->lower = 0;
591           adjustment->upper = MAX (0, page_start);
592
593           value = MIN (offset, adjustment->upper - adjustment->page_size);
594           gtk_adjustment_clamp_page (adjustment, value, offset + page_size);
595         }
596       else
597         {
598           adjustment->lower = page_size - MAX (0, page_start);
599           adjustment->upper = page_size;
600
601           offset = -offset;
602
603           value = MAX (offset, adjustment->lower);
604           gtk_adjustment_clamp_page (adjustment, offset, value + page_size);
605         }
606
607       gtk_adjustment_changed (adjustment);
608     }
609 }
610
611 static gboolean
612 gtk_tool_palette_expose_event (GtkWidget      *widget,
613                                GdkEventExpose *event)
614 {
615   GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
616   GdkDisplay *display;
617   cairo_t *cr;
618   guint i;
619
620   display = gdk_drawable_get_display (widget->window);
621
622   if (!gdk_display_supports_composite (display))
623     return FALSE;
624
625   cr = gdk_cairo_create (widget->window);
626   gdk_cairo_region (cr, event->region);
627   cairo_clip (cr);
628
629   cairo_push_group (cr);
630
631   for (i = 0; i < palette->priv->groups_length; ++i)
632     if (palette->priv->groups[i].widget)
633       _gtk_tool_item_group_paint (palette->priv->groups[i].widget, cr);
634
635   cairo_pop_group_to_source (cr);
636
637   cairo_paint (cr);
638   cairo_destroy (cr);
639
640   return FALSE;
641 }
642
643 static void
644 gtk_tool_palette_realize (GtkWidget *widget)
645 {
646   const gint border_width = GTK_CONTAINER (widget)->border_width;
647   gint attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
648   GdkWindowAttr attributes;
649
650   attributes.window_type = GDK_WINDOW_CHILD;
651   attributes.x = widget->allocation.x + border_width;
652   attributes.y = widget->allocation.y + border_width;
653   attributes.width = widget->allocation.width - border_width * 2;
654   attributes.height = widget->allocation.height - border_width * 2;
655   attributes.wclass = GDK_INPUT_OUTPUT;
656   attributes.visual = gtk_widget_get_visual (widget);
657   attributes.colormap = gtk_widget_get_colormap (widget);
658   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK | GDK_EXPOSURE_MASK
659                         | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
660                         | GDK_BUTTON_MOTION_MASK;
661
662   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
663                                    &attributes, attributes_mask);
664
665   gdk_window_set_user_data (widget->window, widget);
666   widget->style = gtk_style_attach (widget->style, widget->window);
667   gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL);
668   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
669
670   gtk_container_forall (GTK_CONTAINER (widget),
671                         (GtkCallback) gtk_widget_set_parent_window,
672                         widget->window);
673
674   gtk_widget_queue_resize_no_redraw (widget);
675 }
676
677 static void
678 gtk_tool_palette_adjustment_value_changed (GtkAdjustment *adjustment G_GNUC_UNUSED,
679                                            gpointer       data)
680 {
681   GtkWidget *widget = GTK_WIDGET (data);
682   gtk_tool_palette_size_allocate (widget, &widget->allocation);
683 }
684
685 static void
686 gtk_tool_palette_set_scroll_adjustments (GtkWidget     *widget,
687                                          GtkAdjustment *hadjustment,
688                                          GtkAdjustment *vadjustment)
689 {
690   GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
691
692   if (palette->priv->hadjustment)
693     g_object_unref (palette->priv->hadjustment);
694   if (palette->priv->vadjustment)
695     g_object_unref (palette->priv->vadjustment);
696
697   if (hadjustment)
698     g_object_ref_sink (hadjustment);
699   if (vadjustment)
700     g_object_ref_sink (vadjustment);
701
702   palette->priv->hadjustment = hadjustment;
703   palette->priv->vadjustment = vadjustment;
704
705   if (palette->priv->hadjustment)
706     g_signal_connect (palette->priv->hadjustment, "value-changed",
707                       G_CALLBACK (gtk_tool_palette_adjustment_value_changed),
708                       palette);
709   if (palette->priv->vadjustment)
710     g_signal_connect (palette->priv->vadjustment, "value-changed",
711                       G_CALLBACK (gtk_tool_palette_adjustment_value_changed),
712                       palette);
713 }
714
715 static void
716 gtk_tool_palette_repack (GtkToolPalette *palette)
717 {
718   guint si, di;
719
720   for (si = di = 0; di < palette->priv->groups_length; ++si)
721     {
722       if (palette->priv->groups[si].widget)
723         {
724           palette->priv->groups[di] = palette->priv->groups[si];
725           ++di;
726         }
727       else
728         --palette->priv->groups_length;
729     }
730
731   palette->priv->sparse_groups = FALSE;
732 }
733
734 static void
735 gtk_tool_palette_add (GtkContainer *container,
736                       GtkWidget    *child)
737 {
738   GtkToolPalette *palette;
739
740   g_return_if_fail (GTK_IS_TOOL_PALETTE (container));
741   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (child));
742
743   palette = GTK_TOOL_PALETTE (container);
744
745   if (palette->priv->groups_length == palette->priv->groups_size)
746     gtk_tool_palette_repack (palette);
747
748   if (palette->priv->groups_length == palette->priv->groups_size)
749     {
750       gsize old_size = palette->priv->groups_size;
751       gsize new_size = old_size * 2;
752
753       palette->priv->groups = g_renew (GtkToolItemGroupInfo,
754                                        palette->priv->groups,
755                                        new_size);
756
757       memset (palette->priv->groups + old_size, 0,
758               sizeof (GtkToolItemGroupInfo) * old_size);
759
760       palette->priv->groups_size = new_size;
761     }
762
763   palette->priv->groups[palette->priv->groups_length].widget = g_object_ref_sink (child);
764   palette->priv->groups_length += 1;
765
766   gtk_widget_set_parent (child, GTK_WIDGET (palette));
767 }
768
769 static void
770 gtk_tool_palette_remove (GtkContainer *container,
771                          GtkWidget    *child)
772 {
773   GtkToolPalette *palette;
774   guint i;
775
776   g_return_if_fail (GTK_IS_TOOL_PALETTE (container));
777   palette = GTK_TOOL_PALETTE (container);
778
779   for (i = 0; i < palette->priv->groups_length; ++i)
780     if ((GtkWidget*) palette->priv->groups[i].widget == child)
781       {
782         g_object_unref (child);
783         gtk_widget_unparent (child);
784
785         memset (&palette->priv->groups[i], 0, sizeof (GtkToolItemGroupInfo));
786         palette->priv->sparse_groups = TRUE;
787       }
788 }
789
790 static void
791 gtk_tool_palette_forall (GtkContainer *container,
792                          gboolean      internals G_GNUC_UNUSED,
793                          GtkCallback   callback,
794                          gpointer      callback_data)
795 {
796   GtkToolPalette *palette = GTK_TOOL_PALETTE (container);
797   guint i;
798
799   if (palette->priv->groups)
800     {
801       for (i = 0; i < palette->priv->groups_length; ++i)
802         if (palette->priv->groups[i].widget)
803           callback (GTK_WIDGET (palette->priv->groups[i].widget),
804                     callback_data);
805     }
806 }
807
808 static GType
809 gtk_tool_palette_child_type (GtkContainer *container G_GNUC_UNUSED)
810 {
811   return GTK_TYPE_TOOL_ITEM_GROUP;
812 }
813
814 static void
815 gtk_tool_palette_set_child_property (GtkContainer *container,
816                                      GtkWidget    *child,
817                                      guint         prop_id,
818                                      const GValue *value,
819                                      GParamSpec   *pspec)
820 {
821   GtkToolPalette *palette = GTK_TOOL_PALETTE (container);
822
823   switch (prop_id)
824     {
825       case CHILD_PROP_EXCLUSIVE:
826         gtk_tool_palette_set_exclusive (palette, child, g_value_get_boolean (value));
827         break;
828
829       case CHILD_PROP_EXPAND:
830         gtk_tool_palette_set_expand (palette, child, g_value_get_boolean (value));
831         break;
832
833       default:
834         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
835         break;
836     }
837 }
838
839 static void
840 gtk_tool_palette_get_child_property (GtkContainer *container,
841                                      GtkWidget    *child,
842                                      guint         prop_id,
843                                      GValue       *value,
844                                      GParamSpec   *pspec)
845 {
846   GtkToolPalette *palette = GTK_TOOL_PALETTE (container);
847
848   switch (prop_id)
849     {
850       case CHILD_PROP_EXCLUSIVE:
851         g_value_set_boolean (value, gtk_tool_palette_get_exclusive (palette, child));
852         break;
853
854       case CHILD_PROP_EXPAND:
855         g_value_set_boolean (value, gtk_tool_palette_get_expand (palette, child));
856         break;
857
858       default:
859         GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, prop_id, pspec);
860         break;
861     }
862 }
863
864 static void
865 style_change_notify (GtkToolPalette *palette)
866 {
867   GtkToolPalettePrivate* priv = palette->priv;
868   
869   if (!priv->style_set)
870     {
871       /* pretend it was set, then unset, thus reverting to new default */
872       priv->style_set = TRUE;
873       gtk_tool_palette_unset_style (palette);
874     }
875 }
876
877 static void
878 icon_size_change_notify (GtkToolPalette *palette)
879 {
880   GtkToolPalettePrivate* priv = palette->priv;
881   
882   if (!priv->icon_size_set)
883     {
884       /* pretend it was set, then unset, thus reverting to new default */
885       priv->icon_size_set = TRUE;
886       gtk_tool_palette_unset_icon_size (palette);
887     }
888 }
889
890 static void
891 gtk_tool_palette_settings_change_notify (GtkSettings      *settings,
892                                          const GParamSpec *pspec,
893                                          GtkToolPalette   *palette)
894 {
895   if (! strcmp (pspec->name, "gtk-toolbar-style"))
896     style_change_notify (palette);
897   else if (! strcmp (pspec->name, "gtk-toolbar-icon-size"))
898     icon_size_change_notify (palette);
899 }
900
901 static void
902 gtk_tool_palette_screen_changed (GtkWidget *widget,
903                                  GdkScreen *previous_screen)
904 {
905   GtkToolPalette *palette = GTK_TOOL_PALETTE (widget);
906   GtkToolPalettePrivate* priv = palette->priv;
907   GtkSettings *old_settings = priv->settings;
908   GtkSettings *settings;
909   
910   if (gtk_widget_has_screen (GTK_WIDGET (palette)))
911     settings = gtk_widget_get_settings (GTK_WIDGET (palette));
912   else
913     settings = NULL;
914   
915   if (settings == old_settings)
916     return;
917   
918   if (old_settings)
919   {
920     g_signal_handler_disconnect (old_settings, priv->settings_connection);
921     g_object_unref (old_settings);
922   }
923
924   if (settings)
925   {
926     priv->settings_connection =
927       g_signal_connect (settings, "notify",
928                         G_CALLBACK (gtk_tool_palette_settings_change_notify),
929                         palette);
930     priv->settings = g_object_ref (settings);
931   }
932   else
933     priv->settings = NULL;
934
935   gtk_tool_palette_reconfigured (palette);
936 }
937
938
939 static void
940 gtk_tool_palette_class_init (GtkToolPaletteClass *cls)
941 {
942   GObjectClass      *oclass   = G_OBJECT_CLASS (cls);
943   GtkWidgetClass    *wclass   = GTK_WIDGET_CLASS (cls);
944   GtkContainerClass *cclass   = GTK_CONTAINER_CLASS (cls);
945
946   oclass->set_property        = gtk_tool_palette_set_property;
947   oclass->get_property        = gtk_tool_palette_get_property;
948   oclass->dispose             = gtk_tool_palette_dispose;
949   oclass->finalize            = gtk_tool_palette_finalize;
950
951   wclass->size_request        = gtk_tool_palette_size_request;
952   wclass->size_allocate       = gtk_tool_palette_size_allocate;
953   wclass->expose_event        = gtk_tool_palette_expose_event;
954   wclass->realize             = gtk_tool_palette_realize;
955
956   cclass->add                 = gtk_tool_palette_add;
957   cclass->remove              = gtk_tool_palette_remove;
958   cclass->forall              = gtk_tool_palette_forall;
959   cclass->child_type          = gtk_tool_palette_child_type;
960   cclass->set_child_property  = gtk_tool_palette_set_child_property;
961   cclass->get_child_property  = gtk_tool_palette_get_child_property;
962
963   cls->set_scroll_adjustments = gtk_tool_palette_set_scroll_adjustments;
964   
965   /* Handle screen-changed so we can update our GtkSettings.
966    */
967   wclass->screen_changed      = gtk_tool_palette_screen_changed;
968     
969   /**
970    * GtkToolPalette::set-scroll-adjustments:
971    * @widget: the GtkToolPalette that received the signal
972    * @hadjustment: The horizontal adjustment 
973    * @vadjustment: The vertical adjustment
974    *
975    * The ::set-scroll-adjustments when FIXME
976    *
977    * Since: 2.18
978    */
979   wclass->set_scroll_adjustments_signal =
980     g_signal_new ("set-scroll-adjustments",
981                   G_TYPE_FROM_CLASS (oclass),
982                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
983                   G_STRUCT_OFFSET (GtkToolPaletteClass, set_scroll_adjustments),
984                   NULL, NULL,
985                   _gtk_marshal_VOID__OBJECT_OBJECT,
986                   G_TYPE_NONE, 2,
987                   GTK_TYPE_ADJUSTMENT,
988                   GTK_TYPE_ADJUSTMENT);
989
990   g_object_class_override_property (oclass, PROP_ORIENTATION,
991                                     "orientation");
992                                                       
993  /**
994    * GtkToolPalette:icon-size:
995    *
996    * The size of the icons in a tool palette is normally determined by
997    * the toolbar-icon-size setting. When this property is set, it 
998    * overrides the setting. 
999    * 
1000    * This should only be used for special-purpose toolbars, normal
1001    * application toolbars should respect the user preferences for the
1002    * size of icons.
1003    *
1004    * Since: 2.18
1005    */
1006   g_object_class_install_property (oclass,
1007                                    PROP_ICON_SIZE,
1008                                    g_param_spec_enum ("icon-size",
1009                                                       P_("Icon size"),
1010                                                       P_("Size of icons in this tool palette"),
1011                                                       GTK_TYPE_ICON_SIZE,
1012                                                       DEFAULT_ICON_SIZE,
1013                                                       G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1014                                                       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1015
1016   /**
1017    * GtkToolbar:icon-size-set:
1018    *
1019    * Is %TRUE if the icon-size property has been set.
1020    *
1021    * Since: 2.10
1022    */
1023   g_object_class_install_property (oclass,
1024                                    PROP_ICON_SIZE_SET,
1025                                    g_param_spec_boolean ("icon-size-set",
1026                                                       P_("Icon size set"),
1027                                                       P_("Whether the icon-size property has been set"),
1028                                                       FALSE,
1029                                                       G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1030                                                       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1031                                                       
1032   g_object_class_install_property (oclass, PROP_TOOLBAR_STYLE,
1033                                    g_param_spec_enum ("toolbar-style",
1034                                                       P_("Toolbar Style"),
1035                                                       P_("Style of items in the tool palette"),
1036                                                       GTK_TYPE_TOOLBAR_STYLE,
1037                                                       DEFAULT_TOOLBAR_STYLE,
1038                                                       G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1039                                                       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1040                                                          
1041
1042   gtk_container_class_install_child_property (cclass, CHILD_PROP_EXCLUSIVE,
1043                                               g_param_spec_boolean ("exclusive",
1044                                                                     P_("Exclusive"),
1045                                                                     P_("Whether the item group should be the only expanded at a given time"),
1046                                                                     DEFAULT_CHILD_EXCLUSIVE,
1047                                                                     G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1048                                                                     G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1049
1050   gtk_container_class_install_child_property (cclass, CHILD_PROP_EXPAND,
1051                                               g_param_spec_boolean ("expand",
1052                                                                     P_("Expand"),
1053                                                                     P_("Whether the item group should receive extra space when the palette grows"),
1054                                                                     DEFAULT_CHILD_EXPAND,
1055                                                                     G_PARAM_READWRITE | G_PARAM_STATIC_NAME |
1056                                                                     G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1057
1058   g_type_class_add_private (cls, sizeof (GtkToolPalettePrivate));
1059
1060   dnd_target_atom_item = gdk_atom_intern_static_string (dnd_targets[0].target);
1061   dnd_target_atom_group = gdk_atom_intern_static_string (dnd_targets[1].target);
1062 }
1063
1064 /**
1065  * gtk_tool_palette_new:
1066  *
1067  * Creates a new tool palette.
1068  *
1069  * Returns: a new #GtkToolPalette.
1070  *
1071  * Since: 2.18
1072  */
1073 GtkWidget*
1074 gtk_tool_palette_new (void)
1075 {
1076   return g_object_new (GTK_TYPE_TOOL_PALETTE, NULL);
1077 }
1078
1079 /**
1080  * gtk_tool_palette_set_icon_size:
1081  * @palette: an #GtkToolPalette.
1082  * @icon_size: the #GtkIconSize that icons in the tool palette shall have.
1083  *
1084  * Sets the size of icons in the tool palette.
1085  *
1086  * Since: 2.18
1087  */
1088 void
1089 gtk_tool_palette_set_icon_size (GtkToolPalette *palette,
1090                                 GtkIconSize     icon_size)
1091 {
1092   GtkToolPalettePrivate *priv;
1093   
1094   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1095   g_return_if_fail (icon_size != GTK_ICON_SIZE_INVALID);
1096
1097   priv = palette->priv;
1098     
1099   if (!priv->icon_size_set)
1100     {
1101       priv->icon_size_set = TRUE;  
1102       g_object_notify (G_OBJECT (palette), "icon-size-set");
1103     }
1104
1105   if (priv->icon_size == icon_size)
1106     return;
1107   
1108   priv->icon_size = icon_size;
1109   g_object_notify (G_OBJECT (palette), "icon-size");
1110   
1111   gtk_tool_palette_reconfigured (palette);
1112   
1113   gtk_widget_queue_resize (GTK_WIDGET (palette));
1114 }
1115
1116 static GtkSettings *
1117 toolpalette_get_settings (GtkToolPalette *palette)
1118 {
1119   GtkToolPalettePrivate *priv = palette->priv;
1120   return priv->settings;
1121 }
1122
1123 /**
1124  * gtk_tool_palette_unset_icon_size:
1125  * @palette: an #GtkToolPalette.
1126  *
1127  * Unsets the tool palette icon size set with gtk_tool_palette_set_icon_size(), so that
1128  * user preferences will be used to determine the icon size.
1129  *
1130  * Since: 2.18
1131  */
1132 void
1133 gtk_tool_palette_unset_icon_size (GtkToolPalette *palette)
1134 {
1135   GtkToolPalettePrivate* priv = palette->priv;
1136   GtkIconSize size;
1137   
1138   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1139   
1140   if (palette->priv->icon_size_set)
1141     {
1142       GtkSettings *settings = toolpalette_get_settings (palette);
1143       
1144       if (settings)
1145         {
1146           g_object_get (settings,
1147             "gtk-toolbar-icon-size", &size,
1148             NULL);
1149         }
1150       else
1151         size = DEFAULT_ICON_SIZE;
1152       
1153       if (size != palette->priv->icon_size)
1154       {
1155         gtk_tool_palette_set_icon_size (palette, size);
1156         g_object_notify (G_OBJECT (palette), "icon-size");        
1157             }
1158       
1159       priv->icon_size_set = FALSE;
1160       g_object_notify (G_OBJECT (palette), "icon-size-set");      
1161     }
1162 }
1163
1164 /* Set the "toolbar-style" property and do appropriate things.
1165  * GtkToolbar does this by emiting a signal instead of just calling a function, 
1166  * but I don't see how that is useful. murrayc.
1167  */
1168 static void
1169 gtk_tool_palette_change_style (GtkToolPalette     *palette,
1170                                 GtkToolbarStyle style)
1171 {
1172   GtkToolPalettePrivate* priv = palette->priv;
1173   
1174   if (priv->style != style)
1175     {
1176       priv->style = style;
1177       
1178       gtk_tool_palette_reconfigured (palette);
1179       
1180       gtk_widget_queue_resize (GTK_WIDGET (palette));
1181       g_object_notify (G_OBJECT (palette), "toolbar-style");
1182     }
1183 }
1184
1185
1186 /**
1187  * gtk_tool_palette_set_style:
1188  * @palette: a #GtkToolPalette.
1189  * @style: the #GtkToolbarStyle that items in the tool palette shall have.
1190  *
1191  * Sets the style (text, icons or both) of items in the tool palette.
1192  *
1193  * Since: 2.18
1194  */
1195 void
1196 gtk_tool_palette_set_style (GtkToolPalette  *palette,
1197                             GtkToolbarStyle  style)
1198 {
1199   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1200
1201   palette->pr->style_set = TRUE; 
1202   gtk_tool_palette_change_style (palette, style);
1203 }
1204
1205
1206 /**
1207  * gtk_tool_palette_unset_style:
1208  * @palette: a #GtkToolPalette.
1209  * 
1210  * Unsets a toolbar style set with gtk_tool_palette_set_style(), so that
1211  * user preferences will be used to determine the toolbar style.
1212  **/
1213 void
1214 gtk_tool_palette_unset_style (GtkToolPalette *palette)
1215 {
1216   GtkToolPalettePrivate* priv = palette->priv;
1217   GtkToolbarStyle style;
1218   
1219   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1220   
1221   if (priv->style_set)
1222     {
1223       GtkSettings *settings = toolpalette_get_settings (palette);
1224       
1225       if (settings)
1226               g_object_get (settings,
1227                       "gtk-toolbar-style", &style,
1228                       NULL);
1229       else
1230         style = DEFAULT_TOOLBAR_STYLE;
1231       
1232       if (style != priv->style)
1233         gtk_tool_palette_change_style (palette, style);
1234       
1235       priv->style_set = FALSE;
1236     }
1237 }
1238
1239 /**
1240  * gtk_tool_palette_get_icon_size:
1241  * @palette: an #GtkToolPalette.
1242  *
1243  * Gets the size of icons in the tool palette. See gtk_tool_palette_set_icon_size().
1244  * 
1245  * Returns: the #GtkIconSize of icons in the tool palette.
1246  *
1247  * Since: 2.18
1248  */
1249 GtkIconSize
1250 gtk_tool_palette_get_icon_size (GtkToolPalette *palette)
1251 {
1252   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), DEFAULT_ICON_SIZE);
1253   return palette->priv->icon_size;
1254 }
1255
1256 /**
1257  * gtk_tool_palette_get_style:
1258  * @palette: an #GtkToolPalette.
1259  *
1260  * Gets the style (icons, text or both) of items in the tool palette.
1261  *
1262  * Returns: the #GtkToolbarStyle of items in the tool palette.
1263  *
1264  * Since: 2.18
1265  */
1266 GtkToolbarStyle
1267 gtk_tool_palette_get_style (GtkToolPalette *palette)
1268 {
1269   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), DEFAULT_TOOLBAR_STYLE);
1270   return palette->priv->style;
1271 }
1272
1273 /**
1274  * gtk_tool_palette_set_group_position:
1275  * @palette: an #GtkToolPalette.
1276  * @group: an #GtkToolItemGroup which is a child of palette.
1277  * @position: a new index for group.
1278  *
1279  * Sets the position of the group as an index of the tool palette.
1280  * If position is 0 the group will become the first child, if position is
1281  * -1 it will become the last child.
1282  *
1283  * Since: 2.18
1284  */
1285 void
1286 gtk_tool_palette_set_group_position (GtkToolPalette *palette,
1287                                      GtkWidget      *group,
1288                                      gint            position)
1289 {
1290   GtkToolItemGroupInfo group_info;
1291   gint old_position;
1292   gpointer src, dst;
1293   gsize len;
1294
1295   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1296   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1297
1298   gtk_tool_palette_repack (palette);
1299
1300   g_return_if_fail (position >= -1);
1301
1302   if (-1 == position)
1303     position = palette->priv->groups_length - 1;
1304
1305   g_return_if_fail ((guint) position < palette->priv->groups_length);
1306
1307   if (GTK_TOOL_ITEM_GROUP (group) == palette->priv->groups[position].widget)
1308     return;
1309
1310   old_position = gtk_tool_palette_get_group_position (palette, group);
1311   g_return_if_fail (old_position >= 0);
1312
1313   group_info = palette->priv->groups[old_position];
1314
1315   if (position < old_position)
1316     {
1317       dst = palette->priv->groups + position + 1;
1318       src = palette->priv->groups + position;
1319       len = old_position - position;
1320     }
1321   else
1322     {
1323       dst = palette->priv->groups + old_position;
1324       src = palette->priv->groups + old_position + 1;
1325       len = position - old_position;
1326     }
1327
1328   memmove (dst, src, len * sizeof (*palette->priv->groups));
1329   palette->priv->groups[position] = group_info;
1330
1331   gtk_widget_queue_resize (GTK_WIDGET (palette));
1332 }
1333
1334 static void
1335 gtk_tool_palette_group_notify_collapsed (GtkToolItemGroup *group,
1336                                          GParamSpec       *pspec G_GNUC_UNUSED,
1337                                          gpointer          data)
1338 {
1339   GtkToolPalette *palette = GTK_TOOL_PALETTE (data);
1340   guint i;
1341
1342   if (gtk_tool_item_group_get_collapsed (group))
1343     return;
1344
1345   for (i = 0; i < palette->priv->groups_size; ++i)
1346     {
1347       GtkToolItemGroup *current_group = palette->priv->groups[i].widget;
1348
1349       if (current_group && current_group != group)
1350         gtk_tool_item_group_set_collapsed (palette->priv->groups[i].widget, TRUE);
1351     }
1352 }
1353
1354 /**
1355  * gtk_tool_palette_set_exclusive:
1356  * @palette: an #GtkToolPalette.
1357  * @group: an #GtkToolItemGroup which is a child of palette.
1358  * @exclusive: whether the group should be exclusive or not.
1359  *
1360  * Sets whether the group should be exclusive or not. If an exclusive group is expanded
1361  * all other groups are collapsed.
1362  *
1363  * Since: 2.18
1364  */
1365 void
1366 gtk_tool_palette_set_exclusive (GtkToolPalette *palette,
1367                                 GtkWidget      *group,
1368                                 gboolean        exclusive)
1369 {
1370   GtkToolItemGroupInfo *group_info;
1371   gint position;
1372
1373   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1374   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1375
1376   position = gtk_tool_palette_get_group_position (palette, group);
1377   g_return_if_fail (position >= 0);
1378
1379   group_info = &palette->priv->groups[position];
1380
1381   if (exclusive == group_info->exclusive)
1382     return;
1383
1384   group_info->exclusive = exclusive;
1385
1386   if (group_info->exclusive != (0 != group_info->notify_collapsed))
1387     {
1388       if (group_info->exclusive)
1389         {
1390           group_info->notify_collapsed =
1391             g_signal_connect (group, "notify::collapsed",
1392                               G_CALLBACK (gtk_tool_palette_group_notify_collapsed),
1393                               palette);
1394         }
1395       else
1396         {
1397           g_signal_handler_disconnect (group, group_info->notify_collapsed);
1398           group_info->notify_collapsed = 0;
1399         }
1400     }
1401
1402   gtk_tool_palette_group_notify_collapsed (group_info->widget, NULL, palette);
1403   gtk_widget_child_notify (group, "exclusive");
1404 }
1405
1406 /**
1407  * gtk_tool_palette_set_expand:
1408  * @palette: an #GtkToolPalette.
1409  * @group: an #GtkToolItemGroup which is a child of palette.
1410  * @expand: whether the group should be given extra space.
1411  *
1412  * Sets whether the group should be given extra space.
1413  *
1414  * Since: 2.18
1415  */
1416 void
1417 gtk_tool_palette_set_expand (GtkToolPalette *palette,
1418                              GtkWidget      *group,
1419                              gboolean        expand)
1420 {
1421   GtkToolItemGroupInfo *group_info;
1422   gint position;
1423
1424   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1425   g_return_if_fail (GTK_IS_TOOL_ITEM_GROUP (group));
1426
1427   position = gtk_tool_palette_get_group_position (palette, group);
1428   g_return_if_fail (position >= 0);
1429
1430   group_info = &palette->priv->groups[position];
1431
1432   if (expand != group_info->expand)
1433     {
1434       group_info->expand = expand;
1435       gtk_widget_queue_resize (GTK_WIDGET (palette));
1436       gtk_widget_child_notify (group, "expand");
1437     }
1438 }
1439
1440 /**
1441  * gtk_tool_palette_get_group_position:
1442  * @palette: an #GtkToolPalette.
1443  * @group: an #GtkToolItemGroup.
1444  *
1445  * Gets the position of @group in @palette as index. see gtk_tool_palette_set_group_position().
1446  *
1447  * Returns: the index of group or -1 if @group is not a child of @palette.
1448  *
1449  * Since: 2.18
1450  */
1451 gint
1452 gtk_tool_palette_get_group_position (GtkToolPalette *palette,
1453                                      GtkWidget      *group)
1454 {
1455   guint i;
1456
1457   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), -1);
1458   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), -1);
1459
1460   for (i = 0; i < palette->priv->groups_length; ++i)
1461     if ((gpointer) group == palette->priv->groups[i].widget)
1462       return i;
1463
1464   return -1;
1465 }
1466
1467 /**
1468  * gtk_tool_palette_get_exclusive:
1469  * @palette: an #GtkToolPalette.
1470  * @group: an #GtkToolItemGroup which is a child of palette.
1471  *
1472  * Gets whether group is exclusive or not. See gtk_tool_palette_set_exclusive().
1473  *
1474  * Returns: %TRUE if group is exclusive.
1475  *
1476  * Since: 2.18
1477  */
1478 gboolean
1479 gtk_tool_palette_get_exclusive (GtkToolPalette *palette,
1480                                 GtkWidget      *group)
1481 {
1482   gint position;
1483
1484   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), DEFAULT_CHILD_EXCLUSIVE);
1485   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_CHILD_EXCLUSIVE);
1486
1487   position = gtk_tool_palette_get_group_position (palette, group);
1488   g_return_val_if_fail (position >= 0, DEFAULT_CHILD_EXCLUSIVE);
1489
1490   return palette->priv->groups[position].exclusive;
1491 }
1492
1493 /**
1494  * gtk_tool_palette_get_expand:
1495  * @palette: an #GtkToolPalette.
1496  * @group: an #GtkToolItemGroup which is a child of palette.
1497  *
1498  * Gets whether group should be given extra space. See gtk_tool_palette_set_expand().
1499  *
1500  * Returns: %TRUE if group should be given extra space, %FALSE otherwise.
1501  *
1502  * Since: 2.18
1503  */
1504 gboolean
1505 gtk_tool_palette_get_expand (GtkToolPalette *palette,
1506                              GtkWidget      *group)
1507 {
1508   gint position;
1509
1510   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), DEFAULT_CHILD_EXPAND);
1511   g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (group), DEFAULT_CHILD_EXPAND);
1512
1513   position = gtk_tool_palette_get_group_position (palette, group);
1514   g_return_val_if_fail (position >= 0, DEFAULT_CHILD_EXPAND);
1515
1516   return palette->priv->groups[position].expand;
1517 }
1518
1519 /**
1520  * gtk_tool_palette_get_drop_item:
1521  * @palette: an #GtkToolPalette.
1522  * @x: the x position.
1523  * @y: the y position.
1524  *
1525  * Gets the item at position (x, y). See gtk_tool_palette_get_drop_group().
1526  *
1527  * Returns: the #GtkToolItem at position or %NULL if there is no such item.
1528  *
1529  * Since: 2.18
1530  */
1531 GtkToolItem*
1532 gtk_tool_palette_get_drop_item (GtkToolPalette *palette,
1533                                 gint            x,
1534                                 gint            y)
1535 {
1536   GtkWidget *group = gtk_tool_palette_get_drop_group (palette, x, y);
1537
1538   if (group)
1539     return gtk_tool_item_group_get_drop_item (GTK_TOOL_ITEM_GROUP (group),
1540                                               x - group->allocation.x,
1541                                               y - group->allocation.y);
1542
1543   return NULL;
1544 }
1545
1546 /**
1547  * gtk_tool_palette_get_drop_group:
1548  * @palette: an #GtkToolPalette.
1549  * @x: the x position.
1550  * @y: the y position.
1551  *
1552  * Gets the group at position (x, y).
1553  *
1554  * Returns: the #GtkToolItemGroup at position or %NULL if there is no such group.
1555  *
1556  * Since: 2.18
1557  */
1558 GtkWidget*
1559 gtk_tool_palette_get_drop_group (GtkToolPalette *palette,
1560                                  gint            x,
1561                                  gint            y)
1562 {
1563   GtkAllocation *allocation;
1564   guint i;
1565
1566   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), NULL);
1567
1568   allocation = &GTK_WIDGET (palette)->allocation;
1569
1570   g_return_val_if_fail (x >= 0 && x < allocation->width, NULL);
1571   g_return_val_if_fail (y >= 0 && y < allocation->height, NULL);
1572
1573   for (i = 0; i < palette->priv->groups_length; ++i)
1574     {
1575       GtkToolItemGroupInfo *group = &palette->priv->groups[i];
1576       GtkWidget *widget;
1577       gint x0, y0;
1578
1579       if (!group->widget)
1580         continue;
1581
1582       widget = GTK_WIDGET (group->widget);
1583
1584       x0 = x - widget->allocation.x;
1585       y0 = y - widget->allocation.y;
1586
1587       if (x0 >= 0 && x0 < widget->allocation.width &&
1588           y0 >= 0 && y0 < widget->allocation.height)
1589         return widget;
1590     }
1591
1592   return NULL;
1593 }
1594
1595 /**
1596  * gtk_tool_palette_get_drag_item:
1597  * @palette: an #GtkToolPalette.
1598  * @selection: a #GtkSelectionData.
1599  *
1600  * Get the dragged item from the selection. This could be a #GtkToolItem or 
1601  * an #GtkToolItemGroup.
1602  *
1603  * Returns: the dragged item in selection.
1604  *
1605  * Since: 2.18
1606  */
1607 GtkWidget*
1608 gtk_tool_palette_get_drag_item (GtkToolPalette         *palette,
1609                                 const GtkSelectionData *selection)
1610 {
1611   GtkToolPaletteDragData *data;
1612
1613   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), NULL);
1614   g_return_val_if_fail (NULL != selection, NULL);
1615
1616   g_return_val_if_fail (selection->format == 8, NULL);
1617   g_return_val_if_fail (selection->length == sizeof (GtkToolPaletteDragData), NULL);
1618   g_return_val_if_fail (selection->target == dnd_target_atom_item ||
1619                         selection->target == dnd_target_atom_group,
1620                         NULL);
1621
1622   data = (GtkToolPaletteDragData*) selection->data;
1623
1624   g_return_val_if_fail (data->palette == palette, NULL);
1625
1626   if (dnd_target_atom_item == selection->target)
1627     g_return_val_if_fail (GTK_IS_TOOL_ITEM (data->item), NULL);
1628   else if (dnd_target_atom_group == selection->target)
1629     g_return_val_if_fail (GTK_IS_TOOL_ITEM_GROUP (data->item), NULL);
1630
1631   return data->item;
1632 }
1633
1634 /**
1635  * gtk_tool_palette_set_drag_source:
1636  * @palette: an #GtkToolPalette.
1637  * @targets: the #GtkToolPaletteDragTargets which the widget should support.
1638  *
1639  * Sets the tool palette as a drag source. Enables all groups and items in
1640  * the tool palette as drag sources on button 1 and button 3 press with copy
1641  * and move actions.
1642  *
1643  * See gtk_drag_source_set().
1644  *
1645  * Since: 2.18
1646  *
1647  */
1648 void
1649 gtk_tool_palette_set_drag_source (GtkToolPalette            *palette,
1650                                   GtkToolPaletteDragTargets  targets)
1651 {
1652   guint i;
1653
1654   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1655
1656   if ((palette->priv->drag_source & targets) == targets)
1657     return;
1658
1659   palette->priv->drag_source |= targets;
1660
1661   for (i = 0; i < palette->priv->groups_length; ++i)
1662     {
1663       if (palette->priv->groups[i].widget)
1664         gtk_container_forall (GTK_CONTAINER (palette->priv->groups[i].widget),
1665                               _gtk_tool_palette_child_set_drag_source,
1666                               palette);
1667     }
1668 }
1669
1670 /**
1671  * gtk_tool_palette_add_drag_dest:
1672  * @palette: an #GtkToolPalette.
1673  * @widget: a #GtkWidget which should be a drag destination for palette.
1674  * @flags: the flags that specify what actions GTK+ should take for drops on that widget.
1675  * @targets: the #GtkToolPaletteDragTargets which the widget should support.
1676  * @actions: the #GdkDragAction<!-- -->s which the widget should suppport.
1677  *
1678  * Sets the tool palette as drag source (see gtk_tool_palette_set_drag_source) and
1679  * sets widget as a drag destination for drags from palette. With flags the actions
1680  * (like highlighting and target checking) which should be performed by GTK+ for
1681  * drops on widget can be specified. With targets the supported drag targets 
1682  * (groups and/or items) can be specified. With actions the supported drag actions
1683  * (copy and move) can be specified.
1684  *
1685  * See gtk_drag_dest_set().
1686  *
1687  * Since: 2.18
1688  */
1689 void
1690 gtk_tool_palette_add_drag_dest (GtkToolPalette            *palette,
1691                                 GtkWidget                 *widget,
1692                                 GtkDestDefaults            flags,
1693                                 GtkToolPaletteDragTargets  targets,
1694                                 GdkDragAction              actions)
1695 {
1696   GtkTargetEntry entries[G_N_ELEMENTS (dnd_targets)];
1697   gint n_entries = 0;
1698
1699   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1700   g_return_if_fail (GTK_IS_WIDGET (widget));
1701
1702   gtk_tool_palette_set_drag_source (palette,
1703                                     targets);
1704
1705   if (targets & GTK_TOOL_PALETTE_DRAG_ITEMS)
1706     entries[n_entries++] = dnd_targets[0];
1707   if (targets & GTK_TOOL_PALETTE_DRAG_GROUPS)
1708     entries[n_entries++] = dnd_targets[1];
1709
1710   gtk_drag_dest_set (widget, flags, entries, n_entries, actions);
1711 }
1712
1713 void
1714 _gtk_tool_palette_get_item_size (GtkToolPalette *palette,
1715                                  GtkRequisition *item_size,
1716                                  gboolean        homogeneous_only,
1717                                  gint           *requested_rows)
1718 {
1719   GtkRequisition max_requisition;
1720   gint max_rows;
1721   guint i;
1722
1723   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1724   g_return_if_fail (NULL != item_size);
1725
1726   max_requisition.width = 0;
1727   max_requisition.height = 0;
1728   max_rows = 0;
1729
1730   /* iterate over all groups and calculate the max item_size and max row request */
1731   for (i = 0; i < palette->priv->groups_length; ++i)
1732     {
1733       GtkRequisition requisition;
1734       gint rows;
1735       GtkToolItemGroupInfo *group = &palette->priv->groups[i];
1736
1737       if (!group->widget)
1738         continue;
1739
1740       _gtk_tool_item_group_item_size_request (group->widget, &requisition, homogeneous_only, &rows);
1741
1742       max_requisition.width = MAX (max_requisition.width, requisition.width);
1743       max_requisition.height = MAX (max_requisition.height, requisition.height);
1744       max_rows = MAX (max_rows, rows);
1745     }
1746
1747   *item_size = max_requisition;
1748   if (requested_rows)
1749     *requested_rows = max_rows;
1750 }
1751
1752 static void
1753 gtk_tool_palette_item_drag_data_get (GtkWidget        *widget,
1754                                      GdkDragContext   *context G_GNUC_UNUSED,
1755                                      GtkSelectionData *selection,
1756                                      guint             info G_GNUC_UNUSED,
1757                                      guint             time G_GNUC_UNUSED,
1758                                      gpointer          data)
1759 {
1760   GtkToolPaletteDragData drag_data = { GTK_TOOL_PALETTE (data), NULL };
1761
1762   if (selection->target == dnd_target_atom_item)
1763     drag_data.item = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM);
1764
1765   if (drag_data.item)
1766     gtk_selection_data_set (selection, selection->target, 8,
1767                             (guchar*) &drag_data, sizeof (drag_data));
1768 }
1769
1770 static void
1771 gtk_tool_palette_child_drag_data_get (GtkWidget        *widget,
1772                                       GdkDragContext   *context G_GNUC_UNUSED,
1773                                       GtkSelectionData *selection,
1774                                       guint             info G_GNUC_UNUSED,
1775                                       guint             time G_GNUC_UNUSED,
1776                                       gpointer          data)
1777 {
1778   GtkToolPaletteDragData drag_data = { GTK_TOOL_PALETTE (data), NULL };
1779
1780   if (selection->target == dnd_target_atom_group)
1781     drag_data.item = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM_GROUP);
1782
1783   if (drag_data.item)
1784     gtk_selection_data_set (selection, selection->target, 8,
1785                             (guchar*) &drag_data, sizeof (drag_data));
1786 }
1787
1788 void
1789 _gtk_tool_palette_child_set_drag_source (GtkWidget *child,
1790                                          gpointer   data)
1791 {
1792   GtkToolPalette *palette = GTK_TOOL_PALETTE (data);
1793
1794   /* Check drag_source,
1795    * to work properly when called from gtk_tool_item_group_insert().
1796    */
1797   if (!palette->priv->drag_source)
1798     return;
1799
1800   if (GTK_IS_TOOL_ITEM (child) &&
1801       (palette->priv->drag_source & GTK_TOOL_PALETTE_DRAG_ITEMS))
1802     {
1803       /* Connect to child instead of the item itself,
1804        * to work arround bug 510377.
1805        */
1806       if (GTK_IS_TOOL_BUTTON (child))
1807         child = gtk_bin_get_child (GTK_BIN (child));
1808
1809       if (!child)
1810         return;
1811
1812       gtk_drag_source_set (child, GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
1813                            &dnd_targets[0], 1, GDK_ACTION_COPY | GDK_ACTION_MOVE);
1814
1815       g_signal_connect (child, "drag-data-get",
1816                         G_CALLBACK (gtk_tool_palette_item_drag_data_get),
1817                         palette);
1818     }
1819   else if (GTK_IS_BUTTON (child) && 
1820            (palette->priv->drag_source & GTK_TOOL_PALETTE_DRAG_GROUPS))
1821     {
1822       gtk_drag_source_set (child, GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
1823                            &dnd_targets[1], 1, GDK_ACTION_COPY | GDK_ACTION_MOVE);
1824
1825       g_signal_connect (child, "drag-data-get",
1826                         G_CALLBACK (gtk_tool_palette_child_drag_data_get),
1827                         palette);
1828     }
1829 }
1830
1831 /**
1832  * gtk_tool_palette_get_drag_target_item:
1833  *
1834  * Get the target entry for a dragged #GtkToolItem.
1835  *
1836  * Returns: the #GtkTargetEntry for a dragged item.
1837  *
1838  * Since: 2.18
1839  */
1840 G_CONST_RETURN GtkTargetEntry*
1841 gtk_tool_palette_get_drag_target_item (void)
1842 {
1843   return &dnd_targets[0];
1844 }
1845
1846 /**
1847  * gtk_tool_palette_get_drag_target_group:
1848  *
1849  * Get the target entry for a dragged #GtkToolItemGroup.
1850  *
1851  * Returns: the #GtkTargetEntry for a dragged group.
1852  *
1853  * Since: 2.18
1854  */
1855 G_CONST_RETURN GtkTargetEntry*
1856 gtk_tool_palette_get_drag_target_group (void)
1857 {
1858   return &dnd_targets[1];
1859 }
1860
1861 void
1862 _gtk_tool_palette_set_expanding_child (GtkToolPalette *palette,
1863                                        GtkWidget      *widget)
1864 {
1865   g_return_if_fail (GTK_IS_TOOL_PALETTE (palette));
1866   palette->priv->expanding_child = widget;
1867 }
1868
1869 GtkAdjustment*
1870 gtk_tool_palette_get_hadjustment (GtkToolPalette *palette)
1871 {
1872   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), NULL);
1873   return palette->priv->hadjustment;
1874 }
1875
1876 GtkAdjustment*
1877 gtk_tool_palette_get_vadjustment (GtkToolPalette *palette)
1878 {
1879   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), NULL);
1880   return palette->priv->vadjustment;
1881 }
1882
1883 GtkSizeGroup *
1884 _gtk_tool_palette_get_size_group (GtkToolPalette *palette)
1885 {
1886   g_return_val_if_fail (GTK_IS_TOOL_PALETTE (palette), NULL);
1887
1888   return palette->priv->text_size_group;
1889 }