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