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