]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/toolbar-editor/egg-editable-toolbar.c
toolbar-editor: Use gtk_widget_render_icon_pixbuf()
[evince.git] / cut-n-paste / toolbar-editor / egg-editable-toolbar.c
1 /*
2  *  Copyright (C) 2003, 2004  Marco Pesenti Gritti
3  *  Copyright (C) 2003, 2004, 2005  Christian Persch
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  *  $Id$
20  */
21
22 #include "config.h"
23
24 #include "egg-editable-toolbar.h"
25 #include "egg-toolbars-model.h"
26 #include "egg-toolbar-editor.h"
27
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 #include <string.h>
31
32 static GdkPixbuf * new_separator_pixbuf         (void);
33
34 #define MIN_TOOLBAR_HEIGHT 20
35 #define EGG_ITEM_NAME      "egg-item-name"
36 #define STOCK_DRAG_MODE    "stock_drag-mode"
37
38 static const GtkTargetEntry dest_drag_types[] = {
39   {EGG_TOOLBAR_ITEM_TYPE, GTK_TARGET_SAME_APP, 0},
40 };
41
42 enum
43 {
44   PROP_0,
45   PROP_TOOLBARS_MODEL,
46   PROP_UI_MANAGER,
47   PROP_POPUP_PATH,
48   PROP_SELECTED,
49   PROP_EDIT_MODE
50 };
51
52 enum
53 {
54   ACTION_REQUEST,
55   LAST_SIGNAL
56 };
57
58 static guint egg_editable_toolbar_signals[LAST_SIGNAL] = { 0 };
59
60 #define EGG_EDITABLE_TOOLBAR_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), EGG_TYPE_EDITABLE_TOOLBAR, EggEditableToolbarPrivate))
61
62 struct _EggEditableToolbarPrivate
63 {
64   GtkUIManager *manager;
65   EggToolbarsModel *model;
66   guint edit_mode;
67   gboolean save_hidden;
68   GtkWidget *fixed_toolbar;
69
70   GtkWidget *selected;
71   GtkActionGroup *actions;
72
73   guint visibility_id;
74   GList *visibility_paths;
75   GPtrArray *visibility_actions;
76
77   char *popup_path;
78
79   guint        dnd_pending;
80   GtkToolbar  *dnd_toolbar;
81   GtkToolItem *dnd_toolitem;
82 };
83
84 G_DEFINE_TYPE (EggEditableToolbar, egg_editable_toolbar, GTK_TYPE_VBOX);
85
86 static int
87 get_dock_position (EggEditableToolbar *etoolbar,
88                    GtkWidget *dock)
89 {
90   GList *l;
91   int result;
92
93   l = gtk_container_get_children (GTK_CONTAINER (etoolbar));
94   result = g_list_index (l, dock);
95   g_list_free (l);
96
97   return result;
98 }
99
100 static int
101 get_toolbar_position (EggEditableToolbar *etoolbar, GtkWidget *toolbar)
102 {
103   return get_dock_position (etoolbar, gtk_widget_get_parent (toolbar));
104 }
105
106 static int
107 get_n_toolbars (EggEditableToolbar *etoolbar)
108 {
109   GList *l;
110   int result;
111
112   l = gtk_container_get_children (GTK_CONTAINER (etoolbar));
113   result = g_list_length (l);
114   g_list_free (l);
115
116   return result;
117 }
118
119 static GtkWidget *
120 get_dock_nth (EggEditableToolbar *etoolbar,
121               int                 position)
122 {
123   GList *l;
124   GtkWidget *result;
125
126   l = gtk_container_get_children (GTK_CONTAINER (etoolbar));
127   result = g_list_nth_data (l, position);
128   g_list_free (l);
129
130   return result;
131 }
132
133 static GtkWidget *
134 get_toolbar_nth (EggEditableToolbar *etoolbar,
135                  int                 position)
136 {
137   GList *l;
138   GtkWidget *dock;
139   GtkWidget *result;
140
141   dock = get_dock_nth (etoolbar, position);
142   g_return_val_if_fail (dock != NULL, NULL);
143
144   l = gtk_container_get_children (GTK_CONTAINER (dock));
145   result = GTK_WIDGET (l->data);
146   g_list_free (l);
147
148   return result;
149 }
150
151 static GtkAction *
152 find_action (EggEditableToolbar *etoolbar,
153              const char         *name)
154 {
155   GList *l;
156   GtkAction *action = NULL;
157
158   l = gtk_ui_manager_get_action_groups (etoolbar->priv->manager);
159
160   g_return_val_if_fail (name != NULL, NULL);
161
162   for (; l != NULL; l = l->next)
163     {
164       GtkAction *tmp;
165
166       tmp = gtk_action_group_get_action (GTK_ACTION_GROUP (l->data), name);
167       if (tmp)
168         action = tmp;
169     }
170
171   return action;
172 }
173
174 static void
175 drag_data_delete_cb (GtkWidget          *widget,
176                      GdkDragContext     *context,
177                      EggEditableToolbar *etoolbar)
178 {
179   int pos, toolbar_pos;
180   GtkWidget *parent;
181
182   widget = gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM);
183   g_return_if_fail (widget != NULL);
184   g_return_if_fail (EGG_IS_EDITABLE_TOOLBAR (etoolbar));
185
186   parent = gtk_widget_get_parent (widget);
187   pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (parent),
188                                     GTK_TOOL_ITEM (widget));
189   toolbar_pos = get_toolbar_position (etoolbar, parent);
190
191   egg_toolbars_model_remove_item (etoolbar->priv->model,
192                                   toolbar_pos, pos);
193 }
194
195 static void
196 drag_begin_cb (GtkWidget          *widget,
197                GdkDragContext     *context,
198                EggEditableToolbar *etoolbar)
199 {
200   GtkAction *action;
201   gint flags;
202
203   gtk_widget_hide (widget);
204
205 #if GTK_CHECK_VERSION (2, 16, 0)
206   action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (widget));
207 #else
208   action = gtk_widget_get_action (widget);
209 #endif
210
211   if (action == NULL) return;
212
213   flags = egg_toolbars_model_get_name_flags (etoolbar->priv->model,
214                                              gtk_action_get_name (action));
215   if (!(flags & EGG_TB_MODEL_NAME_INFINITE))
216     {
217       flags &= ~EGG_TB_MODEL_NAME_USED;
218       egg_toolbars_model_set_name_flags (etoolbar->priv->model,
219                                          gtk_action_get_name (action),
220                                          flags);
221     }
222 }
223
224 static void
225 drag_end_cb (GtkWidget          *widget,
226              GdkDragContext     *context,
227              EggEditableToolbar *etoolbar)
228 {
229   GtkAction *action;
230   gint flags;
231
232   if (gtk_widget_get_parent (widget) != NULL)
233     {
234       gtk_widget_show (widget);
235
236 #if GTK_CHECK_VERSION (2, 16, 0)
237       action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (widget));
238 #else
239       action = gtk_widget_get_action (widget);
240 #endif
241
242       if (action == NULL) return;
243
244       flags = egg_toolbars_model_get_name_flags (etoolbar->priv->model,
245                                                  gtk_action_get_name (action));
246       if (!(flags & EGG_TB_MODEL_NAME_INFINITE))
247         {
248           flags |= EGG_TB_MODEL_NAME_USED;
249           egg_toolbars_model_set_name_flags (etoolbar->priv->model,
250                                              gtk_action_get_name (action),
251                                              flags);
252         }
253     }
254 }
255
256 static void
257 drag_data_get_cb (GtkWidget          *widget,
258                   GdkDragContext     *context,
259                   GtkSelectionData   *selection_data,
260                   guint               info,
261                   guint32             time,
262                   EggEditableToolbar *etoolbar)
263 {
264   EggToolbarsModel *model;
265   const char *name;
266   char *data;
267   GdkAtom target;
268
269   g_return_if_fail (EGG_IS_EDITABLE_TOOLBAR (etoolbar));
270   model = egg_editable_toolbar_get_model (etoolbar);
271
272   name = g_object_get_data (G_OBJECT (widget), EGG_ITEM_NAME);
273   if (name == NULL)
274     {
275       name = g_object_get_data (G_OBJECT (gtk_widget_get_parent (widget)), EGG_ITEM_NAME);
276       g_return_if_fail (name != NULL);
277     }
278
279   target = gtk_selection_data_get_target (selection_data);
280   data = egg_toolbars_model_get_data (model, target, name);
281   if (data != NULL)
282     {
283       gtk_selection_data_set (selection_data, target, 8, (unsigned char *)data, strlen (data));
284       g_free (data);
285     }
286 }
287
288 static void
289 move_item_cb (GtkAction          *action,
290               EggEditableToolbar *etoolbar)
291 {
292   GtkWidget *toolitem = gtk_widget_get_ancestor (egg_editable_toolbar_get_selected (etoolbar), GTK_TYPE_TOOL_ITEM);
293   GtkTargetList *list = gtk_target_list_new (dest_drag_types, G_N_ELEMENTS (dest_drag_types));
294
295   GdkEvent *realevent = gtk_get_current_event();
296   GdkEventMotion event;
297   event.type = GDK_MOTION_NOTIFY;
298   event.window = realevent->any.window;
299   event.send_event = FALSE;
300   event.axes = NULL;
301   event.time = gdk_event_get_time (realevent);
302   gdk_event_get_state (realevent, &event.state);
303   gdk_event_get_coords (realevent, &event.x, &event.y);
304   gdk_event_get_root_coords (realevent, &event.x_root, &event.y_root);
305
306   gtk_drag_begin (toolitem, list, GDK_ACTION_MOVE, 1, (GdkEvent *)&event);
307   gtk_target_list_unref (list);
308 }
309
310 static void
311 remove_item_cb (GtkAction          *action,
312                 EggEditableToolbar *etoolbar)
313 {
314   GtkWidget *toolitem = gtk_widget_get_ancestor (egg_editable_toolbar_get_selected (etoolbar), GTK_TYPE_TOOL_ITEM);
315   GtkWidget *parent = gtk_widget_get_parent (toolitem);
316   int pos, toolbar_pos;
317
318   toolbar_pos = get_toolbar_position (etoolbar, parent);
319   pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (parent),
320                                     GTK_TOOL_ITEM (toolitem));
321
322   egg_toolbars_model_remove_item (etoolbar->priv->model,
323                                   toolbar_pos, pos);
324
325   if (egg_toolbars_model_n_items (etoolbar->priv->model, toolbar_pos) == 0)
326     {
327       egg_toolbars_model_remove_toolbar (etoolbar->priv->model, toolbar_pos);
328     }
329 }
330
331 static void
332 remove_toolbar_cb (GtkAction          *action,
333                    EggEditableToolbar *etoolbar)
334 {
335   GtkWidget *selected = egg_editable_toolbar_get_selected (etoolbar);
336   GtkWidget *toolbar = gtk_widget_get_ancestor (selected, GTK_TYPE_TOOLBAR);
337   int toolbar_pos;
338
339   toolbar_pos = get_toolbar_position (etoolbar, toolbar);
340   egg_toolbars_model_remove_toolbar (etoolbar->priv->model, toolbar_pos);
341 }
342
343 static void
344 popup_context_deactivate (GtkMenuShell *menu,
345                           EggEditableToolbar *etoolbar)
346 {
347   egg_editable_toolbar_set_selected (etoolbar, NULL);
348   g_object_notify (G_OBJECT (etoolbar), "selected");
349 }
350
351 static void
352 popup_context_menu_cb (GtkWidget          *toolbar,
353                        gint                x,
354                        gint                y,
355                        gint                button_number,
356                        EggEditableToolbar *etoolbar)
357 {
358   if (etoolbar->priv->popup_path != NULL)
359     {
360       GtkMenu *menu;
361
362       egg_editable_toolbar_set_selected (etoolbar, toolbar);
363       g_object_notify (G_OBJECT (etoolbar), "selected");
364
365       menu = GTK_MENU (gtk_ui_manager_get_widget (etoolbar->priv->manager,
366                                                   etoolbar->priv->popup_path));
367       g_return_if_fail (menu != NULL);
368       gtk_menu_popup (menu, NULL, NULL, NULL, NULL, button_number, gtk_get_current_event_time ());
369       g_signal_connect_object (menu, "selection-done",
370                                G_CALLBACK (popup_context_deactivate),
371                                etoolbar, 0);
372     }
373 }
374
375 static gboolean
376 button_press_event_cb (GtkWidget *widget,
377                        GdkEventButton *event,
378                        EggEditableToolbar *etoolbar)
379 {
380   if (event->button == 3 && etoolbar->priv->popup_path != NULL)
381     {
382       GtkMenu *menu;
383
384       egg_editable_toolbar_set_selected (etoolbar, widget);
385       g_object_notify (G_OBJECT (etoolbar), "selected");
386
387       menu = GTK_MENU (gtk_ui_manager_get_widget (etoolbar->priv->manager,
388                                                   etoolbar->priv->popup_path));
389       g_return_val_if_fail (menu != NULL, FALSE);
390       gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button, event->time);
391       g_signal_connect_object (menu, "selection-done",
392                                G_CALLBACK (popup_context_deactivate),
393                                etoolbar, 0);
394
395       return TRUE;
396     }
397
398   return FALSE;
399 }
400
401 static void
402 configure_item_sensitivity (GtkToolItem *item, EggEditableToolbar *etoolbar)
403 {
404   GtkAction *action;
405   char *name;
406
407   name = g_object_get_data (G_OBJECT (item), EGG_ITEM_NAME);
408   action = name ? find_action (etoolbar, name) : NULL;
409
410   if (action)
411     {
412       g_object_notify (G_OBJECT (action), "sensitive");
413     }
414
415   gtk_tool_item_set_use_drag_window (item,
416                                      (etoolbar->priv->edit_mode > 0) ||
417                                      GTK_IS_SEPARATOR_TOOL_ITEM (item));
418
419 }
420
421 static void
422 configure_item_cursor (GtkToolItem *item,
423                        EggEditableToolbar *etoolbar)
424 {
425   EggEditableToolbarPrivate *priv = etoolbar->priv;
426   GtkWidget *widget = GTK_WIDGET (item);
427   GdkWindow *window = gtk_widget_get_window (widget);
428
429   if (window != NULL)
430     {
431       if (priv->edit_mode > 0)
432         {
433           GdkCursor *cursor;
434           GdkScreen *screen;
435           GdkPixbuf *pixbuf = NULL;
436
437           screen = gtk_widget_get_screen (GTK_WIDGET (etoolbar));
438
439           cursor = gdk_cursor_new_for_display (gdk_screen_get_display (screen),
440                                                GDK_HAND2);
441           gdk_window_set_cursor (window, cursor);
442           gdk_cursor_unref (cursor);
443
444           gtk_drag_source_set (widget, GDK_BUTTON1_MASK, dest_drag_types,
445                                G_N_ELEMENTS (dest_drag_types), GDK_ACTION_MOVE);
446           if (GTK_IS_SEPARATOR_TOOL_ITEM (item))
447             {
448               pixbuf = new_separator_pixbuf ();
449             }
450           else
451             {
452               char *icon_name=NULL;
453               char *stock_id=NULL;
454               GtkAction *action;
455               char *name;
456
457               name = g_object_get_data (G_OBJECT (widget), EGG_ITEM_NAME);
458               action = name ? find_action (etoolbar, name) : NULL;
459
460               if (action)
461                 {
462                    g_object_get (action,
463                                  "icon-name", &icon_name,
464                                  "stock-id", &stock_id,
465                                  NULL);
466                 }
467               if (icon_name)
468                 {
469                   GdkScreen *screen;
470                   GtkIconTheme *icon_theme;
471                   GtkSettings *settings;
472                   gint width, height;
473
474                   screen = gtk_widget_get_screen (widget);
475                   icon_theme = gtk_icon_theme_get_for_screen (screen);
476                   settings = gtk_settings_get_for_screen (screen);
477
478                   if (!gtk_icon_size_lookup_for_settings (settings,
479                                                           GTK_ICON_SIZE_LARGE_TOOLBAR,
480                                                           &width, &height))
481                     {
482                       width = height = 24;
483                     }
484
485                   pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name,
486                                                      MIN (width, height), 0, NULL);
487                 }
488               else if (stock_id)
489                 {
490                   pixbuf = gtk_widget_render_icon_pixbuf (widget, stock_id,
491                                                           GTK_ICON_SIZE_LARGE_TOOLBAR);
492                 }
493               g_free (icon_name);
494               g_free (stock_id);
495             }
496
497           if (G_UNLIKELY (!pixbuf))
498             {
499               return;
500             }
501           gtk_drag_source_set_icon_pixbuf (widget, pixbuf);
502           g_object_unref (pixbuf);
503
504         }
505       else
506         {
507           gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET(item)), NULL);
508         }
509     }
510 }
511
512
513 static void
514 configure_item_tooltip (GtkToolItem *item)
515 {
516   GtkAction *action;
517
518 #if GTK_CHECK_VERSION (2, 16, 0)
519   action = gtk_activatable_get_related_action (GTK_ACTIVATABLE (item));
520 #else
521   action = gtk_widget_get_action (GTK_WIDGET (item));
522 #endif
523
524   if (action != NULL)
525     {
526       g_object_notify (G_OBJECT (action), "tooltip");
527     }
528 }
529
530
531 static void
532 connect_widget_signals (GtkWidget *proxy, EggEditableToolbar *etoolbar)
533 {
534   if (GTK_IS_CONTAINER (proxy))
535     {
536        gtk_container_forall (GTK_CONTAINER (proxy),
537                              (GtkCallback) connect_widget_signals,
538                              (gpointer) etoolbar);
539     }
540
541   if (GTK_IS_TOOL_ITEM (proxy))
542     {
543       g_signal_connect_object (proxy, "drag_begin",
544                                G_CALLBACK (drag_begin_cb),
545                                etoolbar, 0);
546       g_signal_connect_object (proxy, "drag_end",
547                                G_CALLBACK (drag_end_cb),
548                                etoolbar, 0);
549       g_signal_connect_object (proxy, "drag_data_get",
550                                G_CALLBACK (drag_data_get_cb),
551                                etoolbar, 0);
552       g_signal_connect_object (proxy, "drag_data_delete",
553                                G_CALLBACK (drag_data_delete_cb),
554                                etoolbar, 0);
555     }
556
557   if (GTK_IS_BUTTON (proxy) || GTK_IS_TOOL_ITEM (proxy))
558     {
559       g_signal_connect_object (proxy, "button-press-event",
560                                G_CALLBACK (button_press_event_cb),
561                                etoolbar, 0);
562     }
563 }
564
565 static void
566 action_sensitive_cb (GtkAction   *action,
567                      GParamSpec  *pspec,
568                      GtkToolItem *item)
569 {
570   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR
571     (gtk_widget_get_ancestor (GTK_WIDGET (item), EGG_TYPE_EDITABLE_TOOLBAR));
572
573   if (etoolbar->priv->edit_mode > 0)
574     {
575       gtk_widget_set_sensitive (GTK_WIDGET (item), TRUE);
576     }
577 }
578
579 static GtkToolItem *
580 create_item_from_action (EggEditableToolbar *etoolbar,
581                          const char *name)
582 {
583   GtkToolItem *item;
584
585   g_return_val_if_fail (name != NULL, NULL);
586
587   if (strcmp (name, "_separator") == 0)
588     {
589       item = gtk_separator_tool_item_new ();
590     }
591   else
592     {
593       GtkAction *action = find_action (etoolbar, name);
594       if (action == NULL) return NULL;
595
596       item = GTK_TOOL_ITEM (gtk_action_create_tool_item (action));
597
598       /* Normally done on-demand by the GtkUIManager, but no
599        * such demand may have been made yet, so do it ourselves.
600        */
601       gtk_action_set_accel_group
602         (action, gtk_ui_manager_get_accel_group(etoolbar->priv->manager));
603
604       g_signal_connect_object (action, "notify::sensitive",
605                                G_CALLBACK (action_sensitive_cb), item, 0);
606     }
607
608   gtk_widget_show (GTK_WIDGET (item));
609
610   g_object_set_data_full (G_OBJECT (item), EGG_ITEM_NAME,
611                           g_strdup (name), g_free);
612
613   return item;
614 }
615
616 static GtkToolItem *
617 create_item_from_position (EggEditableToolbar *etoolbar,
618                            int                 toolbar_position,
619                            int                 position)
620 {
621   GtkToolItem *item;
622   const char *name;
623
624   name = egg_toolbars_model_item_nth (etoolbar->priv->model, toolbar_position, position);
625   item = create_item_from_action (etoolbar, name);
626
627   return item;
628 }
629
630 static void
631 toolbar_drag_data_received_cb (GtkToolbar         *toolbar,
632                                GdkDragContext     *context,
633                                gint                x,
634                                gint                y,
635                                GtkSelectionData   *selection_data,
636                                guint               info,
637                                guint               time,
638                                EggEditableToolbar *etoolbar)
639 {
640   /* This function can be called for two reasons
641    *
642    *  (1) drag_motion() needs an item to pass to
643    *      gtk_toolbar_set_drop_highlight_item(). We can
644    *      recognize this case by etoolbar->priv->pending being TRUE
645    *      We should just create an item and return.
646    *
647    *  (2) The drag has finished, and drag_drop() wants us to
648    *      actually add a new item to the toolbar.
649    */
650
651   GdkAtom type = gtk_selection_data_get_data_type (selection_data);
652   const char *data = (char *)gtk_selection_data_get_data (selection_data);
653
654   int ipos = -1;
655   char *name = NULL;
656   gboolean used = FALSE;
657
658   /* Find out where the drop is occuring, and the name of what is being dropped. */
659   if (gtk_selection_data_get_length (selection_data) >= 0)
660     {
661       ipos = gtk_toolbar_get_drop_index (toolbar, x, y);
662       name = egg_toolbars_model_get_name (etoolbar->priv->model, type, data, FALSE);
663       if (name != NULL)
664         {
665           used = ((egg_toolbars_model_get_name_flags (etoolbar->priv->model, name) & EGG_TB_MODEL_NAME_USED) != 0);
666         }
667     }
668
669   /* If we just want a highlight item, then . */
670   if (etoolbar->priv->dnd_pending > 0)
671     {
672       etoolbar->priv->dnd_pending--;
673
674       if (name != NULL && etoolbar->priv->dnd_toolbar == toolbar && !used)
675         {
676           etoolbar->priv->dnd_toolitem = create_item_from_action (etoolbar, name);
677           gtk_toolbar_set_drop_highlight_item (etoolbar->priv->dnd_toolbar,
678                                                etoolbar->priv->dnd_toolitem, ipos);
679         }
680     }
681   else
682     {
683       gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0);
684       etoolbar->priv->dnd_toolbar = NULL;
685       etoolbar->priv->dnd_toolitem = NULL;
686
687       /* If we don't have a name to use yet, try to create one. */
688       if (name == NULL && gtk_selection_data_get_length (selection_data) >= 0)
689         {
690           name = egg_toolbars_model_get_name (etoolbar->priv->model, type, data, TRUE);
691         }
692
693       if (name != NULL && !used)
694         {
695           gint tpos = get_toolbar_position (etoolbar, GTK_WIDGET (toolbar));
696           egg_toolbars_model_add_item (etoolbar->priv->model, tpos, ipos, name);
697           gtk_drag_finish (context, TRUE,
698                            gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE, time);
699         }
700       else
701         {
702           gtk_drag_finish (context, FALSE,
703                            gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE, time);
704         }
705     }
706
707   g_free (name);
708 }
709
710 static gboolean
711 toolbar_drag_drop_cb (GtkToolbar         *toolbar,
712                       GdkDragContext     *context,
713                       gint                x,
714                       gint                y,
715                       guint               time,
716                       EggEditableToolbar *etoolbar)
717 {
718   GdkAtom target;
719
720   target = gtk_drag_dest_find_target (GTK_WIDGET (toolbar), context, NULL);
721   if (target != GDK_NONE)
722     {
723       gtk_drag_get_data (GTK_WIDGET (toolbar), context, target, time);
724       return TRUE;
725     }
726
727   return FALSE;
728 }
729
730 static gboolean
731 toolbar_drag_motion_cb (GtkToolbar         *toolbar,
732                         GdkDragContext     *context,
733                         gint                x,
734                         gint                y,
735                         guint               time,
736                         EggEditableToolbar *etoolbar)
737 {
738   GdkAtom target = gtk_drag_dest_find_target (GTK_WIDGET (toolbar), context, NULL);
739   if (target == GDK_NONE)
740     {
741       gdk_drag_status (context, 0, time);
742       return FALSE;
743     }
744
745   /* Make ourselves the current dnd toolbar, and request a highlight item. */
746   if (etoolbar->priv->dnd_toolbar != toolbar)
747     {
748       etoolbar->priv->dnd_toolbar = toolbar;
749       etoolbar->priv->dnd_toolitem = NULL;
750       etoolbar->priv->dnd_pending++;
751       gtk_drag_get_data (GTK_WIDGET (toolbar), context, target, time);
752     }
753
754   /* If a highlight item is available, use it. */
755   else if (etoolbar->priv->dnd_toolitem)
756     {
757       gint ipos = gtk_toolbar_get_drop_index (etoolbar->priv->dnd_toolbar, x, y);
758       gtk_toolbar_set_drop_highlight_item (etoolbar->priv->dnd_toolbar,
759                                            etoolbar->priv->dnd_toolitem, ipos);
760     }
761
762   gdk_drag_status (context, gdk_drag_context_get_suggested_action (context), time);
763
764   return TRUE;
765 }
766
767 static void
768 toolbar_drag_leave_cb (GtkToolbar         *toolbar,
769                        GdkDragContext     *context,
770                        guint               time,
771                        EggEditableToolbar *etoolbar)
772 {
773   gtk_toolbar_set_drop_highlight_item (toolbar, NULL, 0);
774
775   /* If we were the current dnd toolbar target, remove the item. */
776   if (etoolbar->priv->dnd_toolbar == toolbar)
777     {
778       etoolbar->priv->dnd_toolbar = NULL;
779       etoolbar->priv->dnd_toolitem = NULL;
780     }
781 }
782
783 static void
784 configure_drag_dest (EggEditableToolbar *etoolbar,
785                      GtkToolbar         *toolbar)
786 {
787   EggToolbarsItemType *type;
788   GtkTargetList *targets;
789   GList *list;
790
791   /* Make every toolbar able to receive drag-drops. */
792   gtk_drag_dest_set (GTK_WIDGET (toolbar), 0,
793                      dest_drag_types, G_N_ELEMENTS (dest_drag_types),
794                      GDK_ACTION_MOVE | GDK_ACTION_COPY);
795
796   /* Add any specialist drag-drop abilities. */
797   targets = gtk_drag_dest_get_target_list (GTK_WIDGET (toolbar));
798   list = egg_toolbars_model_get_types (etoolbar->priv->model);
799   while (list)
800   {
801     type = list->data;
802     if (type->new_name != NULL || type->get_name != NULL)
803       gtk_target_list_add (targets, type->type, 0, 0);
804     list = list->next;
805   }
806 }
807
808 static void
809 toggled_visibility_cb (GtkToggleAction *action,
810                        EggEditableToolbar *etoolbar)
811 {
812   EggEditableToolbarPrivate *priv = etoolbar->priv;
813   GtkWidget *dock;
814   EggTbModelFlags flags;
815   gboolean visible;
816   gint i;
817
818   visible = gtk_toggle_action_get_active (action);
819   for (i = 0; i < priv->visibility_actions->len; i++)
820     if (g_ptr_array_index (priv->visibility_actions, i) == action)
821       break;
822
823   g_return_if_fail (i < priv->visibility_actions->len);
824
825   dock = get_dock_nth (etoolbar, i);
826   if (visible)
827     {
828       gtk_widget_show (dock);
829     }
830   else
831     {
832       gtk_widget_hide (dock);
833     }
834
835   if (priv->save_hidden)
836     {
837       flags = egg_toolbars_model_get_flags (priv->model, i);
838
839       if (visible)
840         {
841           flags &= ~(EGG_TB_MODEL_HIDDEN);
842         }
843       else
844         {
845           flags |=  (EGG_TB_MODEL_HIDDEN);
846         }
847
848       egg_toolbars_model_set_flags (priv->model, i, flags);
849     }
850 }
851
852 static void
853 toolbar_visibility_refresh (EggEditableToolbar *etoolbar)
854 {
855   EggEditableToolbarPrivate *priv = etoolbar->priv;
856   gint n_toolbars, n_items, i, j, k;
857   GtkToggleAction *action;
858   GList *list;
859   GString *string;
860   gboolean showing;
861   char action_name[40];
862   char *action_label;
863   char *tmp;
864
865   if (priv == NULL || priv->model == NULL || priv->manager == NULL ||
866       priv->visibility_paths == NULL || priv->actions == NULL)
867     {
868       return;
869     }
870
871   if (priv->visibility_actions == NULL)
872     {
873       priv->visibility_actions = g_ptr_array_new ();
874     }
875
876   if (priv->visibility_id != 0)
877     {
878       gtk_ui_manager_remove_ui (priv->manager, priv->visibility_id);
879     }
880
881   priv->visibility_id = gtk_ui_manager_new_merge_id (priv->manager);
882
883 #if GTK_CHECK_VERSION(2,20,0)
884   showing = gtk_widget_get_visible (GTK_WIDGET (etoolbar));
885 #else
886   showing = GTK_WIDGET_VISIBLE (etoolbar);
887 #endif
888
889   n_toolbars = egg_toolbars_model_n_toolbars (priv->model);
890   for (i = 0; i < n_toolbars; i++)
891     {
892       string = g_string_sized_new (0);
893       n_items = egg_toolbars_model_n_items (priv->model, i);
894       for (k = 0, j = 0; j < n_items; j++)
895         {
896           GValue value = { 0, };
897           GtkAction *action;
898           const char *name;
899
900           name = egg_toolbars_model_item_nth (priv->model, i, j);
901           if (name == NULL) continue;
902           action = find_action (etoolbar, name);
903           if (action == NULL) continue;
904
905           g_value_init (&value, G_TYPE_STRING);
906           g_object_get_property (G_OBJECT (action), "label", &value);
907           name = g_value_get_string (&value);
908           if (name == NULL)
909             {
910                 g_value_unset (&value);
911                 continue;
912             }
913           k += g_utf8_strlen (name, -1) + 2;
914           if (j > 0)
915             {
916               g_string_append (string, ", ");
917               if (j > 1 && k > 25)
918                 {
919                   g_value_unset (&value);
920                   break;
921                 }
922             }
923           g_string_append (string, name);
924           g_value_unset (&value);
925         }
926       if (j < n_items)
927         {
928           g_string_append (string, " ...");
929         }
930
931       tmp = g_string_free (string, FALSE);
932       for (j = 0, k = 0; tmp[j]; j++)
933       {
934         if (tmp[j] == '_') continue;
935         tmp[k] = tmp[j];
936         k++;
937       }
938       tmp[k] = 0;
939       /* Translaters: This string is for a toggle to display a toolbar.
940        * The name of the toolbar is automatically computed from the widgets
941        * on the toolbar, and is placed at the %s. Note the _ before the %s
942        * which is used to add mnemonics. We know that this is likely to
943        * produce duplicates, but don't worry about it. If your language
944        * normally has a mnemonic at the start, please use the _. If not,
945        * please remove. */
946       action_label = g_strdup_printf (_("Show “_%s”"), tmp);
947       g_free (tmp);
948
949       sprintf(action_name, "ToolbarToggle%d", i);
950
951       if (i >= priv->visibility_actions->len)
952         {
953           action = gtk_toggle_action_new (action_name, action_label, NULL, NULL);
954           g_ptr_array_add (priv->visibility_actions, action);
955           g_signal_connect_object (action, "toggled",
956                                    G_CALLBACK (toggled_visibility_cb),
957                                    etoolbar, 0);
958           gtk_action_group_add_action (priv->actions, GTK_ACTION (action));
959         }
960       else
961         {
962           action = g_ptr_array_index (priv->visibility_actions, i);
963           g_object_set (action, "label", action_label, NULL);
964         }
965
966       gtk_action_set_visible (GTK_ACTION (action), (egg_toolbars_model_get_flags (priv->model, i)
967                                                     & EGG_TB_MODEL_NOT_REMOVABLE) == 0);
968       gtk_action_set_sensitive (GTK_ACTION (action), showing);
969 #if GTK_CHECK_VERSION(2,20,0)
970       gtk_toggle_action_set_active (action, gtk_widget_get_visible
971                                     (get_dock_nth (etoolbar, i)));
972 #else
973       gtk_toggle_action_set_active (action, GTK_WIDGET_VISIBLE
974                                     (get_dock_nth (etoolbar, i)));
975 #endif
976
977       for (list = priv->visibility_paths; list != NULL; list = g_list_next (list))
978         {
979           gtk_ui_manager_add_ui (priv->manager, priv->visibility_id,
980                                  (const char *)list->data, action_name, action_name,
981                                  GTK_UI_MANAGER_MENUITEM, FALSE);
982         }
983
984       g_free (action_label);
985     }
986
987   gtk_ui_manager_ensure_update (priv->manager);
988
989   while (i < priv->visibility_actions->len)
990     {
991       action = g_ptr_array_index (priv->visibility_actions, i);
992       g_ptr_array_remove_index_fast (priv->visibility_actions, i);
993       gtk_action_group_remove_action (priv->actions, GTK_ACTION (action));
994       i++;
995     }
996 }
997
998 static GtkWidget *
999 create_dock (EggEditableToolbar *etoolbar)
1000 {
1001   GtkWidget *toolbar, *hbox;
1002
1003   hbox = gtk_hbox_new (0, FALSE);
1004
1005   toolbar = gtk_toolbar_new ();
1006   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
1007   gtk_widget_show (toolbar);
1008   gtk_box_pack_start (GTK_BOX (hbox), toolbar, TRUE, TRUE, 0);
1009
1010   g_signal_connect (toolbar, "drag_drop",
1011                     G_CALLBACK (toolbar_drag_drop_cb), etoolbar);
1012   g_signal_connect (toolbar, "drag_motion",
1013                     G_CALLBACK (toolbar_drag_motion_cb), etoolbar);
1014   g_signal_connect (toolbar, "drag_leave",
1015                     G_CALLBACK (toolbar_drag_leave_cb), etoolbar);
1016
1017   g_signal_connect (toolbar, "drag_data_received",
1018                     G_CALLBACK (toolbar_drag_data_received_cb), etoolbar);
1019   g_signal_connect (toolbar, "popup_context_menu",
1020                     G_CALLBACK (popup_context_menu_cb), etoolbar);
1021
1022   configure_drag_dest (etoolbar, GTK_TOOLBAR (toolbar));
1023
1024   return hbox;
1025 }
1026
1027 static void
1028 set_fixed_style (EggEditableToolbar *t, GtkToolbarStyle style)
1029 {
1030   g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar));
1031   gtk_toolbar_set_style (GTK_TOOLBAR (t->priv->fixed_toolbar),
1032                          style == GTK_TOOLBAR_ICONS ? GTK_TOOLBAR_BOTH_HORIZ : style);
1033 }
1034
1035 static void
1036 unset_fixed_style (EggEditableToolbar *t)
1037 {
1038   g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar));
1039   gtk_toolbar_unset_style (GTK_TOOLBAR (t->priv->fixed_toolbar));
1040 }
1041
1042 static void
1043 toolbar_changed_cb (EggToolbarsModel   *model,
1044                     int                 position,
1045                     EggEditableToolbar *etoolbar)
1046 {
1047   GtkWidget *toolbar;
1048   EggTbModelFlags flags;
1049   GtkToolbarStyle style;
1050
1051   flags = egg_toolbars_model_get_flags (model, position);
1052   toolbar = get_toolbar_nth (etoolbar, position);
1053
1054   if (flags & EGG_TB_MODEL_ICONS)
1055   {
1056     style = GTK_TOOLBAR_ICONS;
1057   }
1058   else if (flags & EGG_TB_MODEL_TEXT)
1059   {
1060     style = GTK_TOOLBAR_TEXT;
1061   }
1062   else if (flags & EGG_TB_MODEL_BOTH)
1063   {
1064     style = GTK_TOOLBAR_BOTH;
1065   }
1066   else if (flags & EGG_TB_MODEL_BOTH_HORIZ)
1067   {
1068     style = GTK_TOOLBAR_BOTH_HORIZ;
1069   }
1070   else
1071   {
1072     gtk_toolbar_unset_style (GTK_TOOLBAR (toolbar));
1073     if (position == 0 && etoolbar->priv->fixed_toolbar)
1074       {
1075         unset_fixed_style (etoolbar);
1076       }
1077     return;
1078   }
1079
1080   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), style);
1081   if (position == 0 && etoolbar->priv->fixed_toolbar)
1082     {
1083       set_fixed_style (etoolbar, style);
1084     }
1085
1086   toolbar_visibility_refresh (etoolbar);
1087 }
1088
1089 static void
1090 unparent_fixed (EggEditableToolbar *etoolbar)
1091 {
1092   GtkWidget *toolbar, *dock;
1093   g_return_if_fail (GTK_IS_TOOLBAR (etoolbar->priv->fixed_toolbar));
1094
1095   toolbar = etoolbar->priv->fixed_toolbar;
1096   dock = get_dock_nth (etoolbar, 0);
1097
1098   if (dock && gtk_widget_get_parent (toolbar) != NULL)
1099     {
1100       gtk_container_remove (GTK_CONTAINER (dock), toolbar);
1101     }
1102 }
1103
1104 static void
1105 update_fixed (EggEditableToolbar *etoolbar)
1106 {
1107   GtkWidget *toolbar, *dock;
1108   if (!etoolbar->priv->fixed_toolbar) return;
1109
1110   toolbar = etoolbar->priv->fixed_toolbar;
1111   dock = get_dock_nth (etoolbar, 0);
1112
1113   if (dock && toolbar && gtk_widget_get_parent (toolbar) == NULL)
1114     {
1115       gtk_box_pack_end (GTK_BOX (dock), toolbar, FALSE, TRUE, 0);
1116
1117       gtk_widget_show (toolbar);
1118
1119       gtk_widget_set_size_request (dock, -1, -1);
1120       gtk_widget_queue_resize_no_redraw (dock);
1121     }
1122 }
1123
1124 static void
1125 toolbar_added_cb (EggToolbarsModel   *model,
1126                   int                 position,
1127                   EggEditableToolbar *etoolbar)
1128 {
1129   GtkWidget *dock;
1130
1131   dock = create_dock (etoolbar);
1132   if ((egg_toolbars_model_get_flags (model, position) & EGG_TB_MODEL_HIDDEN) == 0)
1133     gtk_widget_show (dock);
1134
1135   gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT);
1136
1137   gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0);
1138
1139   gtk_box_reorder_child (GTK_BOX (etoolbar), dock, position);
1140
1141   gtk_widget_show_all (dock);
1142
1143   update_fixed (etoolbar);
1144
1145   toolbar_visibility_refresh (etoolbar);
1146 }
1147
1148 static void
1149 toolbar_removed_cb (EggToolbarsModel   *model,
1150                     int                 position,
1151                     EggEditableToolbar *etoolbar)
1152 {
1153   GtkWidget *dock;
1154
1155   if (position == 0 && etoolbar->priv->fixed_toolbar != NULL)
1156     {
1157       unparent_fixed (etoolbar);
1158     }
1159
1160   dock = get_dock_nth (etoolbar, position);
1161   gtk_widget_destroy (dock);
1162
1163   update_fixed (etoolbar);
1164
1165   toolbar_visibility_refresh (etoolbar);
1166 }
1167
1168 static void
1169 item_added_cb (EggToolbarsModel   *model,
1170                int                 tpos,
1171                int                 ipos,
1172                EggEditableToolbar *etoolbar)
1173 {
1174   GtkWidget *dock;
1175   GtkWidget *toolbar;
1176   GtkToolItem *item;
1177
1178   toolbar = get_toolbar_nth (etoolbar, tpos);
1179   item = create_item_from_position (etoolbar, tpos, ipos);
1180   if (item == NULL) return;
1181
1182   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, ipos);
1183
1184   connect_widget_signals (GTK_WIDGET (item), etoolbar);
1185   configure_item_tooltip (item);
1186   configure_item_cursor (item, etoolbar);
1187   configure_item_sensitivity (item, etoolbar);
1188
1189   dock = get_dock_nth (etoolbar, tpos);
1190   gtk_widget_set_size_request (dock, -1, -1);
1191   gtk_widget_queue_resize_no_redraw (dock);
1192
1193   toolbar_visibility_refresh (etoolbar);
1194 }
1195
1196 static void
1197 item_removed_cb (EggToolbarsModel   *model,
1198                  int                 toolbar_position,
1199                  int                 position,
1200                  EggEditableToolbar *etoolbar)
1201 {
1202   EggEditableToolbarPrivate *priv = etoolbar->priv;
1203
1204   GtkWidget *toolbar;
1205   GtkWidget *item;
1206
1207   toolbar = get_toolbar_nth (etoolbar, toolbar_position);
1208   item = GTK_WIDGET (gtk_toolbar_get_nth_item
1209         (GTK_TOOLBAR (toolbar), position));
1210   g_return_if_fail (item != NULL);
1211
1212   if (item == priv->selected)
1213     {
1214       /* FIXME */
1215     }
1216
1217   gtk_container_remove (GTK_CONTAINER (toolbar), item);
1218
1219   toolbar_visibility_refresh (etoolbar);
1220 }
1221
1222 static void
1223 egg_editable_toolbar_build (EggEditableToolbar *etoolbar)
1224 {
1225   int i, l, n_items, n_toolbars;
1226   EggToolbarsModel *model = etoolbar->priv->model;
1227
1228   g_return_if_fail (model != NULL);
1229   g_return_if_fail (etoolbar->priv->manager != NULL);
1230
1231   n_toolbars = egg_toolbars_model_n_toolbars (model);
1232
1233   for (i = 0; i < n_toolbars; i++)
1234     {
1235       GtkWidget *toolbar, *dock;
1236
1237       dock = create_dock (etoolbar);
1238       if ((egg_toolbars_model_get_flags (model, i) & EGG_TB_MODEL_HIDDEN) == 0)
1239         gtk_widget_show (dock);
1240       gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0);
1241       toolbar = get_toolbar_nth (etoolbar, i);
1242
1243       n_items = egg_toolbars_model_n_items (model, i);
1244       for (l = 0; l < n_items; l++)
1245         {
1246           GtkToolItem *item;
1247
1248           item = create_item_from_position (etoolbar, i, l);
1249           if (item)
1250             {
1251               gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, l);
1252
1253               connect_widget_signals (GTK_WIDGET (item), etoolbar);
1254               configure_item_tooltip (item);
1255               configure_item_sensitivity (item, etoolbar);
1256             }
1257           else
1258             {
1259               egg_toolbars_model_remove_item (model, i, l);
1260               l--;
1261               n_items--;
1262             }
1263         }
1264
1265       if (n_items == 0)
1266         {
1267             gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT);
1268         }
1269     }
1270
1271   update_fixed (etoolbar);
1272
1273   /* apply styles */
1274   for (i = 0; i < n_toolbars; i ++)
1275     {
1276       toolbar_changed_cb (model, i, etoolbar);
1277     }
1278 }
1279
1280 static void
1281 egg_editable_toolbar_disconnect_model (EggEditableToolbar *toolbar)
1282 {
1283   EggToolbarsModel *model = toolbar->priv->model;
1284
1285   g_signal_handlers_disconnect_by_func
1286     (model, G_CALLBACK (item_added_cb), toolbar);
1287   g_signal_handlers_disconnect_by_func
1288     (model, G_CALLBACK (item_removed_cb), toolbar);
1289   g_signal_handlers_disconnect_by_func
1290     (model, G_CALLBACK (toolbar_added_cb), toolbar);
1291   g_signal_handlers_disconnect_by_func
1292     (model, G_CALLBACK (toolbar_removed_cb), toolbar);
1293   g_signal_handlers_disconnect_by_func
1294     (model, G_CALLBACK (toolbar_changed_cb), toolbar);
1295 }
1296
1297 static void
1298 egg_editable_toolbar_deconstruct (EggEditableToolbar *toolbar)
1299 {
1300   EggToolbarsModel *model = toolbar->priv->model;
1301   GList *children;
1302
1303   g_return_if_fail (model != NULL);
1304
1305   if (toolbar->priv->fixed_toolbar)
1306     {
1307        unset_fixed_style (toolbar);
1308        unparent_fixed (toolbar);
1309     }
1310
1311   children = gtk_container_get_children (GTK_CONTAINER (toolbar));
1312   g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL);
1313   g_list_free (children);
1314 }
1315
1316 void
1317 egg_editable_toolbar_set_model (EggEditableToolbar *etoolbar,
1318                                 EggToolbarsModel   *model)
1319 {
1320   EggEditableToolbarPrivate *priv = etoolbar->priv;
1321
1322   if (priv->model == model) return;
1323
1324   if (priv->model)
1325     {
1326       egg_editable_toolbar_disconnect_model (etoolbar);
1327       egg_editable_toolbar_deconstruct (etoolbar);
1328
1329       g_object_unref (priv->model);
1330     }
1331
1332   priv->model = g_object_ref (model);
1333
1334   egg_editable_toolbar_build (etoolbar);
1335
1336   toolbar_visibility_refresh (etoolbar);
1337
1338   g_signal_connect (model, "item_added",
1339                     G_CALLBACK (item_added_cb), etoolbar);
1340   g_signal_connect (model, "item_removed",
1341                     G_CALLBACK (item_removed_cb), etoolbar);
1342   g_signal_connect (model, "toolbar_added",
1343                     G_CALLBACK (toolbar_added_cb), etoolbar);
1344   g_signal_connect (model, "toolbar_removed",
1345                     G_CALLBACK (toolbar_removed_cb), etoolbar);
1346   g_signal_connect (model, "toolbar_changed",
1347                     G_CALLBACK (toolbar_changed_cb), etoolbar);
1348 }
1349
1350 static void
1351 egg_editable_toolbar_init (EggEditableToolbar *etoolbar)
1352 {
1353   EggEditableToolbarPrivate *priv;
1354
1355   priv = etoolbar->priv = EGG_EDITABLE_TOOLBAR_GET_PRIVATE (etoolbar);
1356
1357   priv->save_hidden = TRUE;
1358
1359   g_signal_connect (etoolbar, "notify::visible",
1360                     G_CALLBACK (toolbar_visibility_refresh), NULL);
1361 }
1362
1363 static void
1364 egg_editable_toolbar_dispose (GObject *object)
1365 {
1366   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1367   EggEditableToolbarPrivate *priv = etoolbar->priv;
1368   GList *children;
1369
1370   if (priv->fixed_toolbar != NULL)
1371     {
1372       g_object_unref (priv->fixed_toolbar);
1373       priv->fixed_toolbar = NULL;
1374     }
1375
1376   if (priv->visibility_paths)
1377     {
1378       children = priv->visibility_paths;
1379       g_list_foreach (children, (GFunc) g_free, NULL);
1380       g_list_free (children);
1381       priv->visibility_paths = NULL;
1382     }
1383
1384   g_free (priv->popup_path);
1385   priv->popup_path = NULL;
1386
1387   if (priv->manager != NULL)
1388     {
1389       if (priv->visibility_id)
1390         {
1391           gtk_ui_manager_remove_ui (priv->manager, priv->visibility_id);
1392           priv->visibility_id = 0;
1393         }
1394
1395       g_object_unref (priv->manager);
1396       priv->manager = NULL;
1397     }
1398
1399   if (priv->model)
1400     {
1401       egg_editable_toolbar_disconnect_model (etoolbar);
1402       g_object_unref (priv->model);
1403       priv->model = NULL;
1404     }
1405
1406   G_OBJECT_CLASS (egg_editable_toolbar_parent_class)->dispose (object);
1407 }
1408
1409 static void
1410 egg_editable_toolbar_set_ui_manager (EggEditableToolbar *etoolbar,
1411                                      GtkUIManager       *manager)
1412 {
1413   static const GtkActionEntry actions[] = {
1414     { "MoveToolItem", STOCK_DRAG_MODE, N_("_Move on Toolbar"), NULL,
1415       N_("Move the selected item on the toolbar"), G_CALLBACK (move_item_cb) },
1416     { "RemoveToolItem", GTK_STOCK_REMOVE, N_("_Remove from Toolbar"), NULL,
1417       N_("Remove the selected item from the toolbar"), G_CALLBACK (remove_item_cb) },
1418     { "RemoveToolbar", GTK_STOCK_DELETE, N_("_Delete Toolbar"), NULL,
1419       N_("Remove the selected toolbar"), G_CALLBACK (remove_toolbar_cb) },
1420   };
1421
1422   etoolbar->priv->manager = g_object_ref (manager);
1423
1424   etoolbar->priv->actions = gtk_action_group_new ("ToolbarActions");
1425   gtk_action_group_set_translation_domain (etoolbar->priv->actions, GETTEXT_PACKAGE);
1426   gtk_action_group_add_actions (etoolbar->priv->actions, actions,
1427                                 G_N_ELEMENTS (actions), etoolbar);
1428   gtk_ui_manager_insert_action_group (manager, etoolbar->priv->actions, -1);
1429   g_object_unref (etoolbar->priv->actions);
1430
1431   toolbar_visibility_refresh (etoolbar);
1432 }
1433
1434 GtkWidget * egg_editable_toolbar_get_selected (EggEditableToolbar   *etoolbar)
1435 {
1436   return etoolbar->priv->selected;
1437 }
1438
1439 void
1440 egg_editable_toolbar_set_selected (EggEditableToolbar *etoolbar,
1441                                    GtkWidget          *widget)
1442 {
1443   GtkWidget *toolbar, *toolitem;
1444   gboolean editable;
1445
1446   etoolbar->priv->selected = widget;
1447
1448   toolbar = (widget != NULL) ? gtk_widget_get_ancestor (widget, GTK_TYPE_TOOLBAR) : NULL;
1449   toolitem = (widget != NULL) ? gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM) : NULL;
1450
1451   if(toolbar != NULL)
1452     {
1453       gint tpos = get_toolbar_position (etoolbar, toolbar);
1454       editable = ((egg_toolbars_model_get_flags (etoolbar->priv->model, tpos) & EGG_TB_MODEL_NOT_EDITABLE) == 0);
1455     }
1456   else
1457     {
1458       editable = FALSE;
1459     }
1460
1461   gtk_action_set_visible (find_action (etoolbar, "RemoveToolbar"), (toolbar != NULL) && (etoolbar->priv->edit_mode > 0));
1462   gtk_action_set_visible (find_action (etoolbar, "RemoveToolItem"), (toolitem != NULL) && editable);
1463   gtk_action_set_visible (find_action (etoolbar, "MoveToolItem"), (toolitem != NULL) && editable);
1464 }
1465
1466 static void
1467 set_edit_mode (EggEditableToolbar *etoolbar,
1468                gboolean mode)
1469 {
1470   EggEditableToolbarPrivate *priv = etoolbar->priv;
1471   int i, l, n_items;
1472
1473   i = priv->edit_mode;
1474   if (mode)
1475     {
1476       priv->edit_mode++;
1477     }
1478   else
1479     {
1480       g_return_if_fail (priv->edit_mode > 0);
1481       priv->edit_mode--;
1482     }
1483   i *= priv->edit_mode;
1484
1485   if (i == 0)
1486     {
1487       for (i = get_n_toolbars (etoolbar)-1; i >= 0; i--)
1488         {
1489           GtkWidget *toolbar;
1490
1491           toolbar = get_toolbar_nth (etoolbar, i);
1492           n_items = gtk_toolbar_get_n_items (GTK_TOOLBAR (toolbar));
1493
1494           if (n_items == 0 && priv->edit_mode == 0)
1495             {
1496               egg_toolbars_model_remove_toolbar (priv->model, i);
1497             }
1498           else
1499             {
1500               for (l = 0; l < n_items; l++)
1501                 {
1502                   GtkToolItem *item;
1503
1504                   item = gtk_toolbar_get_nth_item (GTK_TOOLBAR (toolbar), l);
1505
1506                   configure_item_cursor (item, etoolbar);
1507                   configure_item_sensitivity (item, etoolbar);
1508                 }
1509             }
1510         }
1511     }
1512 }
1513
1514 static void
1515 egg_editable_toolbar_set_property (GObject      *object,
1516                                    guint         prop_id,
1517                                    const GValue *value,
1518                                    GParamSpec   *pspec)
1519 {
1520   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1521
1522   switch (prop_id)
1523     {
1524     case PROP_UI_MANAGER:
1525       egg_editable_toolbar_set_ui_manager (etoolbar, g_value_get_object (value));
1526       break;
1527     case PROP_TOOLBARS_MODEL:
1528       egg_editable_toolbar_set_model (etoolbar, g_value_get_object (value));
1529       break;
1530     case PROP_SELECTED:
1531       egg_editable_toolbar_set_selected (etoolbar, g_value_get_object (value));
1532       break;
1533     case PROP_POPUP_PATH:
1534       etoolbar->priv->popup_path = g_strdup (g_value_get_string (value));
1535       break;
1536     case PROP_EDIT_MODE:
1537       set_edit_mode (etoolbar, g_value_get_boolean (value));
1538       break;
1539     default:
1540       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1541       break;
1542     }
1543 }
1544
1545 static void
1546 egg_editable_toolbar_get_property (GObject    *object,
1547                                    guint       prop_id,
1548                                    GValue     *value,
1549                                    GParamSpec *pspec)
1550 {
1551   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1552
1553   switch (prop_id)
1554     {
1555     case PROP_UI_MANAGER:
1556       g_value_set_object (value, etoolbar->priv->manager);
1557       break;
1558     case PROP_TOOLBARS_MODEL:
1559       g_value_set_object (value, etoolbar->priv->model);
1560       break;
1561     case PROP_SELECTED:
1562       g_value_set_object (value, etoolbar->priv->selected);
1563       break;
1564     case PROP_EDIT_MODE:
1565       g_value_set_boolean (value, etoolbar->priv->edit_mode>0);
1566       break;
1567     default:
1568       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1569       break;
1570     }
1571 }
1572
1573 static void
1574 egg_editable_toolbar_class_init (EggEditableToolbarClass *klass)
1575 {
1576   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1577
1578   object_class->dispose = egg_editable_toolbar_dispose;
1579   object_class->set_property = egg_editable_toolbar_set_property;
1580   object_class->get_property = egg_editable_toolbar_get_property;
1581
1582   egg_editable_toolbar_signals[ACTION_REQUEST] =
1583     g_signal_new ("action_request",
1584                   G_OBJECT_CLASS_TYPE (object_class),
1585                   G_SIGNAL_RUN_LAST,
1586                   G_STRUCT_OFFSET (EggEditableToolbarClass, action_request),
1587                   NULL, NULL, g_cclosure_marshal_VOID__STRING,
1588                   G_TYPE_NONE, 1, G_TYPE_STRING);
1589
1590   g_object_class_install_property (object_class,
1591                                    PROP_UI_MANAGER,
1592                                    g_param_spec_object ("ui-manager",
1593                                                         "UI-Mmanager",
1594                                                         "UI Manager",
1595                                                         GTK_TYPE_UI_MANAGER,
1596                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1597   g_object_class_install_property (object_class,
1598                                    PROP_TOOLBARS_MODEL,
1599                                    g_param_spec_object ("model",
1600                                                         "Model",
1601                                                         "Toolbars Model",
1602                                                         EGG_TYPE_TOOLBARS_MODEL,
1603                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1604   g_object_class_install_property (object_class,
1605                                    PROP_SELECTED,
1606                                    g_param_spec_object ("selected",
1607                                                         "Selected",
1608                                                         "Selected toolitem",
1609                                                         GTK_TYPE_TOOL_ITEM,
1610                                                         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1611
1612   g_object_class_install_property (object_class,
1613                                    PROP_POPUP_PATH,
1614                                    g_param_spec_string ("popup-path",
1615                                                         "popup-path",
1616                                                         "popup-path",
1617                                                         NULL,
1618                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1619
1620   g_object_class_install_property (object_class,
1621                                    PROP_EDIT_MODE,
1622                                    g_param_spec_boolean ("edit-mode",
1623                                                          "Edit-Mode",
1624                                                          "Edit Mode",
1625                                                          FALSE,
1626                                                          G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1627
1628   g_type_class_add_private (object_class, sizeof (EggEditableToolbarPrivate));
1629 }
1630
1631 GtkWidget *
1632 egg_editable_toolbar_new (GtkUIManager *manager,
1633                           const char *popup_path)
1634 {
1635     return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
1636                                      "ui-manager", manager,
1637                                      "popup-path", popup_path,
1638                                      NULL));
1639 }
1640
1641 GtkWidget *
1642 egg_editable_toolbar_new_with_model (GtkUIManager *manager,
1643                                      EggToolbarsModel *model,
1644                                      const char *popup_path)
1645 {
1646   return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
1647                                    "ui-manager", manager,
1648                                    "model", model,
1649                                    "popup-path", popup_path,
1650                                    NULL));
1651 }
1652
1653 gboolean
1654 egg_editable_toolbar_get_edit_mode (EggEditableToolbar *etoolbar)
1655 {
1656   EggEditableToolbarPrivate *priv = etoolbar->priv;
1657
1658   return priv->edit_mode > 0;
1659 }
1660
1661 void
1662 egg_editable_toolbar_set_edit_mode (EggEditableToolbar *etoolbar,
1663                                     gboolean mode)
1664 {
1665   set_edit_mode (etoolbar, mode);
1666   g_object_notify (G_OBJECT (etoolbar), "edit-mode");
1667 }
1668
1669 void
1670 egg_editable_toolbar_add_visibility (EggEditableToolbar *etoolbar,
1671                                      const char *path)
1672 {
1673   etoolbar->priv->visibility_paths = g_list_prepend
1674           (etoolbar->priv->visibility_paths, g_strdup (path));
1675 }
1676
1677 void
1678 egg_editable_toolbar_show (EggEditableToolbar *etoolbar,
1679                            const char *name)
1680 {
1681   EggEditableToolbarPrivate *priv = etoolbar->priv;
1682   EggToolbarsModel *model = priv->model;
1683   int i, n_toolbars;
1684
1685   n_toolbars = egg_toolbars_model_n_toolbars (model);
1686   for (i = 0; i < n_toolbars; i++)
1687     {
1688       const char *toolbar_name;
1689
1690       toolbar_name = egg_toolbars_model_toolbar_nth (model, i);
1691       if (strcmp (toolbar_name, name) == 0)
1692         {
1693           gtk_widget_show (get_dock_nth (etoolbar, i));
1694         }
1695     }
1696 }
1697
1698 void
1699 egg_editable_toolbar_hide (EggEditableToolbar *etoolbar,
1700                            const char *name)
1701 {
1702   EggEditableToolbarPrivate *priv = etoolbar->priv;
1703   EggToolbarsModel *model = priv->model;
1704   int i, n_toolbars;
1705
1706   n_toolbars = egg_toolbars_model_n_toolbars (model);
1707   for (i = 0; i < n_toolbars; i++)
1708     {
1709       const char *toolbar_name;
1710
1711       toolbar_name = egg_toolbars_model_toolbar_nth (model, i);
1712       if (strcmp (toolbar_name, name) == 0)
1713       {
1714         gtk_widget_hide (get_dock_nth (etoolbar, i));
1715       }
1716     }
1717 }
1718
1719 void
1720 egg_editable_toolbar_set_fixed (EggEditableToolbar *etoolbar,
1721                                 GtkToolbar *toolbar)
1722 {
1723   EggEditableToolbarPrivate *priv = etoolbar->priv;
1724
1725   g_return_if_fail (!toolbar || GTK_IS_TOOLBAR (toolbar));
1726
1727   if (priv->fixed_toolbar)
1728     {
1729       unparent_fixed (etoolbar);
1730       g_object_unref (priv->fixed_toolbar);
1731       priv->fixed_toolbar = NULL;
1732     }
1733
1734   if (toolbar)
1735     {
1736       priv->fixed_toolbar = GTK_WIDGET (toolbar);
1737       gtk_toolbar_set_show_arrow (toolbar, FALSE);
1738       g_object_ref_sink (toolbar);
1739     }
1740
1741   update_fixed (etoolbar);
1742 }
1743
1744 #define DEFAULT_ICON_HEIGHT 20
1745
1746 /* We should probably experiment some more with this.
1747  * Right now the rendered icon is pretty good for most
1748  * themes. However, the icon is slightly large for themes
1749  * with large toolbar icons.
1750  */
1751 static GdkPixbuf *
1752 new_pixbuf_from_widget (GtkWidget *widget)
1753 {
1754   GtkWidget *window;
1755   GdkPixbuf *pixbuf;
1756   gint icon_height;
1757   GdkScreen *screen;
1758
1759   screen = gtk_widget_get_screen (widget);
1760
1761   if (!gtk_icon_size_lookup_for_settings (gtk_settings_get_for_screen (screen),
1762                                           GTK_ICON_SIZE_LARGE_TOOLBAR,
1763                                           NULL,
1764                                           &icon_height))
1765     {
1766       icon_height = DEFAULT_ICON_HEIGHT;
1767     }
1768
1769   window = gtk_offscreen_window_new ();
1770   /* Set the width to -1 as we want the separator to be as thin as possible. */
1771   gtk_widget_set_size_request (widget, -1, icon_height);
1772
1773   gtk_container_add (GTK_CONTAINER (window), widget);
1774   gtk_widget_show_all (window);
1775
1776   /* Process the waiting events to have the widget actually drawn */
1777   gdk_window_process_updates (gtk_widget_get_window (window), TRUE);
1778   pixbuf = gtk_offscreen_window_get_pixbuf (GTK_OFFSCREEN_WINDOW (window));
1779   gtk_widget_destroy (window);
1780
1781   return pixbuf;
1782 }
1783
1784 static GdkPixbuf *
1785 new_separator_pixbuf (void)
1786 {
1787   GtkWidget *separator;
1788   GdkPixbuf *pixbuf;
1789
1790   separator = gtk_vseparator_new ();
1791   pixbuf = new_pixbuf_from_widget (separator);
1792   return pixbuf;
1793 }
1794
1795 static void
1796 update_separator_image (GtkImage *image)
1797 {
1798   GdkPixbuf *pixbuf = new_separator_pixbuf ();
1799   gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
1800   g_object_unref (pixbuf);
1801 }
1802
1803 static gboolean
1804 style_set_cb (GtkWidget *widget,
1805               GtkStyle *previous_style,
1806               GtkImage *image)
1807 {
1808
1809   update_separator_image (image);
1810   return FALSE;
1811 }
1812
1813 GtkWidget *
1814 _egg_editable_toolbar_new_separator_image (void)
1815 {
1816   GtkWidget *image = gtk_image_new ();
1817   update_separator_image (GTK_IMAGE (image));
1818   g_signal_connect (G_OBJECT (image), "style_set",
1819                     G_CALLBACK (style_set_cb), GTK_IMAGE (image));
1820
1821   return image;
1822 }
1823
1824 EggToolbarsModel *
1825 egg_editable_toolbar_get_model (EggEditableToolbar *etoolbar)
1826 {
1827   return etoolbar->priv->model;
1828 }