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