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