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