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