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