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