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