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