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