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