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