]> www.fi.muni.cz Git - evince.git/blob - cut-n-paste/toolbar-editor/egg-editable-toolbar.c
Replace GTK_WIDGET_VISIBLE() with gtk_widget_get_visible()
[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 #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   showing = gtk_widget_get_visible (GTK_WIDGET (etoolbar));
876   
877   n_toolbars = egg_toolbars_model_n_toolbars (priv->model);
878   for (i = 0; i < n_toolbars; i++)
879     {
880       string = g_string_sized_new (0);
881       n_items = egg_toolbars_model_n_items (priv->model, i);
882       for (k = 0, j = 0; j < n_items; j++)
883         {
884           GValue value = { 0, };
885           GtkAction *action;
886           const char *name;
887
888           name = egg_toolbars_model_item_nth (priv->model, i, j);
889           if (name == NULL) continue;
890           action = find_action (etoolbar, name);
891           if (action == NULL) continue;
892
893           g_value_init (&value, G_TYPE_STRING);
894           g_object_get_property (G_OBJECT (action), "label", &value);
895           name = g_value_get_string (&value);
896           if (name == NULL)
897             {
898                 g_value_unset (&value);
899                 continue;
900             }
901           k += g_utf8_strlen (name, -1) + 2;
902           if (j > 0)
903             {
904               g_string_append (string, ", ");
905               if (j > 1 && k > 25)
906                 {
907                   g_value_unset (&value);
908                   break;
909                 }
910             }
911           g_string_append (string, name);
912           g_value_unset (&value);
913         }
914       if (j < n_items)
915         {
916           g_string_append (string, " ...");
917         }
918       
919       tmp = g_string_free (string, FALSE);
920       for (j = 0, k = 0; tmp[j]; j++)
921       {
922         if (tmp[j] == '_') continue;
923         tmp[k] = tmp[j];
924         k++;
925       }
926       tmp[k] = 0;
927       /* Translaters: This string is for a toggle to display a toolbar.
928        * The name of the toolbar is automatically computed from the widgets
929        * on the toolbar, and is placed at the %s. Note the _ before the %s
930        * which is used to add mnemonics. We know that this is likely to
931        * produce duplicates, but don't worry about it. If your language
932        * normally has a mnemonic at the start, please use the _. If not,
933        * please remove. */
934       action_label = g_strdup_printf (_("Show “_%s”"), tmp);
935       g_free (tmp);
936       
937       sprintf(action_name, "ToolbarToggle%d", i);
938       
939       if (i >= priv->visibility_actions->len)
940         {
941           action = gtk_toggle_action_new (action_name, action_label, NULL, NULL);
942           g_ptr_array_add (priv->visibility_actions, action);
943           g_signal_connect_object (action, "toggled",
944                                    G_CALLBACK (toggled_visibility_cb),
945                                    etoolbar, 0);
946           gtk_action_group_add_action (priv->actions, GTK_ACTION (action));
947         }
948       else
949         {
950           action = g_ptr_array_index (priv->visibility_actions, i);
951           g_object_set (action, "label", action_label, NULL);
952         }
953
954       gtk_action_set_visible (GTK_ACTION (action), (egg_toolbars_model_get_flags (priv->model, i) 
955                                                     & EGG_TB_MODEL_NOT_REMOVABLE) == 0);
956       gtk_action_set_sensitive (GTK_ACTION (action), showing);
957       gtk_toggle_action_set_active (action, gtk_widget_get_visible
958                                     (get_dock_nth (etoolbar, i)));
959       
960       for (list = priv->visibility_paths; list != NULL; list = g_list_next (list))
961         {
962           gtk_ui_manager_add_ui (priv->manager, priv->visibility_id,
963                                  (const char *)list->data, action_name, action_name,
964                                  GTK_UI_MANAGER_MENUITEM, FALSE);
965         }
966             
967       g_free (action_label);
968     }
969   
970   gtk_ui_manager_ensure_update (priv->manager);
971   
972   while (i < priv->visibility_actions->len)
973     {
974       action = g_ptr_array_index (priv->visibility_actions, i);
975       g_ptr_array_remove_index_fast (priv->visibility_actions, i);
976       gtk_action_group_remove_action (priv->actions, GTK_ACTION (action));
977       i++;
978     }
979 }
980
981 static GtkWidget *
982 create_dock (EggEditableToolbar *etoolbar)
983 {
984   GtkWidget *toolbar, *hbox;
985
986   hbox = gtk_hbox_new (0, FALSE);
987
988   toolbar = gtk_toolbar_new ();
989   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), TRUE);
990   gtk_widget_show (toolbar);
991   gtk_box_pack_start (GTK_BOX (hbox), toolbar, TRUE, TRUE, 0);
992
993   g_signal_connect (toolbar, "drag_drop",
994                     G_CALLBACK (toolbar_drag_drop_cb), etoolbar); 
995   g_signal_connect (toolbar, "drag_motion",
996                     G_CALLBACK (toolbar_drag_motion_cb), etoolbar);
997   g_signal_connect (toolbar, "drag_leave",
998                     G_CALLBACK (toolbar_drag_leave_cb), etoolbar);
999
1000   g_signal_connect (toolbar, "drag_data_received",
1001                     G_CALLBACK (toolbar_drag_data_received_cb), etoolbar);
1002   g_signal_connect (toolbar, "popup_context_menu",
1003                     G_CALLBACK (popup_context_menu_cb), etoolbar);
1004
1005   configure_drag_dest (etoolbar, GTK_TOOLBAR (toolbar));
1006   
1007   return hbox;
1008 }
1009
1010 static void
1011 set_fixed_style (EggEditableToolbar *t, GtkToolbarStyle style)
1012 {
1013   g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar));
1014   gtk_toolbar_set_style (GTK_TOOLBAR (t->priv->fixed_toolbar),
1015                          style == GTK_TOOLBAR_ICONS ? GTK_TOOLBAR_BOTH_HORIZ : style);
1016 }
1017
1018 static void
1019 unset_fixed_style (EggEditableToolbar *t)
1020 {
1021   g_return_if_fail (GTK_IS_TOOLBAR (t->priv->fixed_toolbar));
1022   gtk_toolbar_unset_style (GTK_TOOLBAR (t->priv->fixed_toolbar));
1023 }
1024
1025 static void
1026 toolbar_changed_cb (EggToolbarsModel   *model,
1027                     int                 position,
1028                     EggEditableToolbar *etoolbar)
1029 {
1030   GtkWidget *toolbar;
1031   EggTbModelFlags flags;
1032   GtkToolbarStyle style;
1033
1034   flags = egg_toolbars_model_get_flags (model, position);
1035   toolbar = get_toolbar_nth (etoolbar, position);
1036
1037   if (flags & EGG_TB_MODEL_ICONS)
1038   {
1039     style = GTK_TOOLBAR_ICONS;
1040   }
1041   else if (flags & EGG_TB_MODEL_TEXT)
1042   {
1043     style = GTK_TOOLBAR_TEXT;
1044   }
1045   else if (flags & EGG_TB_MODEL_BOTH)
1046   {
1047     style = GTK_TOOLBAR_BOTH;
1048   }
1049   else if (flags & EGG_TB_MODEL_BOTH_HORIZ)
1050   {
1051     style = GTK_TOOLBAR_BOTH_HORIZ;
1052   }
1053   else
1054   {
1055     gtk_toolbar_unset_style (GTK_TOOLBAR (toolbar));
1056     if (position == 0 && etoolbar->priv->fixed_toolbar)
1057       {
1058         unset_fixed_style (etoolbar);
1059       }
1060     return;
1061   }
1062
1063   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), style);
1064   if (position == 0 && etoolbar->priv->fixed_toolbar)
1065     {
1066       set_fixed_style (etoolbar, style);
1067     }
1068
1069   toolbar_visibility_refresh (etoolbar);
1070 }
1071
1072 static void
1073 unparent_fixed (EggEditableToolbar *etoolbar)
1074 {
1075   GtkWidget *toolbar, *dock;
1076   g_return_if_fail (GTK_IS_TOOLBAR (etoolbar->priv->fixed_toolbar));
1077
1078   toolbar = etoolbar->priv->fixed_toolbar;
1079   dock = get_dock_nth (etoolbar, 0);
1080
1081   if (dock && toolbar->parent != NULL)
1082     {
1083       gtk_container_remove (GTK_CONTAINER (dock), toolbar);
1084     }
1085 }
1086
1087 static void
1088 update_fixed (EggEditableToolbar *etoolbar)
1089 {
1090   GtkWidget *toolbar, *dock;
1091   if (!etoolbar->priv->fixed_toolbar) return;
1092
1093   toolbar = etoolbar->priv->fixed_toolbar;
1094   dock = get_dock_nth (etoolbar, 0);
1095
1096   if (dock && toolbar && toolbar->parent == NULL)
1097     {
1098       gtk_box_pack_end (GTK_BOX (dock), toolbar, FALSE, TRUE, 0);
1099
1100       gtk_widget_show (toolbar);
1101   
1102       gtk_widget_set_size_request (dock, -1, -1);
1103       gtk_widget_queue_resize_no_redraw (dock);
1104     }
1105 }
1106
1107 static void
1108 toolbar_added_cb (EggToolbarsModel   *model,
1109                   int                 position,
1110                   EggEditableToolbar *etoolbar)
1111 {
1112   GtkWidget *dock;
1113
1114   dock = create_dock (etoolbar);
1115   if ((egg_toolbars_model_get_flags (model, position) & EGG_TB_MODEL_HIDDEN) == 0)
1116     gtk_widget_show (dock);
1117
1118   gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT);
1119
1120   gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0);
1121
1122   gtk_box_reorder_child (GTK_BOX (etoolbar), dock, position);
1123
1124   gtk_widget_show_all (dock);
1125   
1126   update_fixed (etoolbar);
1127
1128   toolbar_visibility_refresh (etoolbar);
1129 }
1130
1131 static void
1132 toolbar_removed_cb (EggToolbarsModel   *model,
1133                     int                 position,
1134                     EggEditableToolbar *etoolbar)
1135 {
1136   GtkWidget *dock;
1137
1138   if (position == 0 && etoolbar->priv->fixed_toolbar != NULL)
1139     {
1140       unparent_fixed (etoolbar);
1141     }
1142
1143   dock = get_dock_nth (etoolbar, position);
1144   gtk_widget_destroy (dock);
1145
1146   update_fixed (etoolbar);
1147   
1148   toolbar_visibility_refresh (etoolbar);
1149 }
1150
1151 static void
1152 item_added_cb (EggToolbarsModel   *model,
1153                int                 tpos,
1154                int                 ipos,
1155                EggEditableToolbar *etoolbar)
1156 {
1157   GtkWidget *dock;
1158   GtkWidget *toolbar;
1159   GtkToolItem *item;
1160
1161   toolbar = get_toolbar_nth (etoolbar, tpos);
1162   item = create_item_from_position (etoolbar, tpos, ipos);
1163   if (item == NULL) return;
1164     
1165   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, ipos);
1166   
1167   connect_widget_signals (GTK_WIDGET (item), etoolbar);
1168   configure_item_tooltip (item);
1169   configure_item_cursor (item, etoolbar);
1170   configure_item_sensitivity (item, etoolbar);
1171   
1172   dock = get_dock_nth (etoolbar, tpos);
1173   gtk_widget_set_size_request (dock, -1, -1);
1174   gtk_widget_queue_resize_no_redraw (dock);
1175
1176   toolbar_visibility_refresh (etoolbar);
1177 }
1178
1179 static void
1180 item_removed_cb (EggToolbarsModel   *model,
1181                  int                 toolbar_position,
1182                  int                 position,
1183                  EggEditableToolbar *etoolbar)
1184 {
1185   EggEditableToolbarPrivate *priv = etoolbar->priv;
1186   
1187   GtkWidget *toolbar;
1188   GtkWidget *item;
1189
1190   toolbar = get_toolbar_nth (etoolbar, toolbar_position);
1191   item = GTK_WIDGET (gtk_toolbar_get_nth_item
1192         (GTK_TOOLBAR (toolbar), position));
1193   g_return_if_fail (item != NULL);
1194
1195   if (item == priv->selected)
1196     {
1197       /* FIXME */
1198     }
1199
1200   gtk_container_remove (GTK_CONTAINER (toolbar), item);
1201
1202   toolbar_visibility_refresh (etoolbar);
1203 }
1204
1205 static void
1206 egg_editable_toolbar_build (EggEditableToolbar *etoolbar)
1207 {
1208   int i, l, n_items, n_toolbars;
1209   EggToolbarsModel *model = etoolbar->priv->model;
1210
1211   g_return_if_fail (model != NULL);
1212   g_return_if_fail (etoolbar->priv->manager != NULL);
1213
1214   n_toolbars = egg_toolbars_model_n_toolbars (model);
1215
1216   for (i = 0; i < n_toolbars; i++)
1217     {
1218       GtkWidget *toolbar, *dock;
1219
1220       dock = create_dock (etoolbar);
1221       if ((egg_toolbars_model_get_flags (model, i) & EGG_TB_MODEL_HIDDEN) == 0)
1222         gtk_widget_show (dock);
1223       gtk_box_pack_start (GTK_BOX (etoolbar), dock, TRUE, TRUE, 0);
1224       toolbar = get_toolbar_nth (etoolbar, i);
1225
1226       n_items = egg_toolbars_model_n_items (model, i);
1227       for (l = 0; l < n_items; l++)
1228         {
1229           GtkToolItem *item;
1230
1231           item = create_item_from_position (etoolbar, i, l);
1232           if (item)
1233             {
1234               gtk_toolbar_insert (GTK_TOOLBAR (toolbar), item, l);
1235               
1236               connect_widget_signals (GTK_WIDGET (item), etoolbar);
1237               configure_item_tooltip (item);
1238               configure_item_sensitivity (item, etoolbar);
1239             }
1240           else
1241             {
1242               egg_toolbars_model_remove_item (model, i, l);
1243               l--;
1244               n_items--;
1245             }
1246         }
1247
1248       if (n_items == 0)
1249         {
1250             gtk_widget_set_size_request (dock, -1, MIN_TOOLBAR_HEIGHT);
1251         }
1252     }
1253
1254   update_fixed (etoolbar);
1255
1256   /* apply styles */
1257   for (i = 0; i < n_toolbars; i ++)
1258     {
1259       toolbar_changed_cb (model, i, etoolbar);
1260     }
1261 }
1262
1263 static void
1264 egg_editable_toolbar_disconnect_model (EggEditableToolbar *toolbar)
1265 {
1266   EggToolbarsModel *model = toolbar->priv->model;
1267
1268   g_signal_handlers_disconnect_by_func
1269     (model, G_CALLBACK (item_added_cb), toolbar);
1270   g_signal_handlers_disconnect_by_func
1271     (model, G_CALLBACK (item_removed_cb), toolbar);
1272   g_signal_handlers_disconnect_by_func
1273     (model, G_CALLBACK (toolbar_added_cb), toolbar);
1274   g_signal_handlers_disconnect_by_func
1275     (model, G_CALLBACK (toolbar_removed_cb), toolbar);
1276   g_signal_handlers_disconnect_by_func
1277     (model, G_CALLBACK (toolbar_changed_cb), toolbar);
1278 }
1279
1280 static void
1281 egg_editable_toolbar_deconstruct (EggEditableToolbar *toolbar)
1282 {
1283   EggToolbarsModel *model = toolbar->priv->model;
1284   GList *children;
1285
1286   g_return_if_fail (model != NULL);
1287
1288   if (toolbar->priv->fixed_toolbar)
1289     {
1290        unset_fixed_style (toolbar);
1291        unparent_fixed (toolbar);
1292     }
1293
1294   children = gtk_container_get_children (GTK_CONTAINER (toolbar));
1295   g_list_foreach (children, (GFunc) gtk_widget_destroy, NULL);
1296   g_list_free (children);
1297 }
1298
1299 void
1300 egg_editable_toolbar_set_model (EggEditableToolbar *etoolbar,
1301                                 EggToolbarsModel   *model)
1302 {
1303   EggEditableToolbarPrivate *priv = etoolbar->priv;
1304
1305   if (priv->model == model) return;
1306
1307   if (priv->model)
1308     {
1309       egg_editable_toolbar_disconnect_model (etoolbar);
1310       egg_editable_toolbar_deconstruct (etoolbar);
1311
1312       g_object_unref (priv->model);
1313     }
1314
1315   priv->model = g_object_ref (model);
1316
1317   egg_editable_toolbar_build (etoolbar);
1318
1319   toolbar_visibility_refresh (etoolbar);
1320
1321   g_signal_connect (model, "item_added",
1322                     G_CALLBACK (item_added_cb), etoolbar);
1323   g_signal_connect (model, "item_removed",
1324                     G_CALLBACK (item_removed_cb), etoolbar);
1325   g_signal_connect (model, "toolbar_added",
1326                     G_CALLBACK (toolbar_added_cb), etoolbar);
1327   g_signal_connect (model, "toolbar_removed",
1328                     G_CALLBACK (toolbar_removed_cb), etoolbar);
1329   g_signal_connect (model, "toolbar_changed",
1330                     G_CALLBACK (toolbar_changed_cb), etoolbar);
1331 }
1332
1333 static void
1334 egg_editable_toolbar_init (EggEditableToolbar *etoolbar)
1335 {
1336   EggEditableToolbarPrivate *priv;
1337
1338   priv = etoolbar->priv = EGG_EDITABLE_TOOLBAR_GET_PRIVATE (etoolbar);
1339
1340   priv->save_hidden = TRUE;
1341   
1342   g_signal_connect (etoolbar, "notify::visible",
1343                     G_CALLBACK (toolbar_visibility_refresh), NULL);
1344 }
1345
1346 static void
1347 egg_editable_toolbar_dispose (GObject *object)
1348 {
1349   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1350   EggEditableToolbarPrivate *priv = etoolbar->priv;
1351   GList *children;
1352
1353   if (priv->fixed_toolbar != NULL)
1354     {
1355       g_object_unref (priv->fixed_toolbar);
1356       priv->fixed_toolbar = NULL;
1357     }
1358
1359   if (priv->visibility_paths)
1360     {
1361       children = priv->visibility_paths;
1362       g_list_foreach (children, (GFunc) g_free, NULL);
1363       g_list_free (children);
1364       priv->visibility_paths = NULL;
1365     }
1366
1367   g_free (priv->popup_path);
1368   priv->popup_path = NULL;
1369
1370   if (priv->manager != NULL)
1371     {
1372       if (priv->visibility_id)
1373         {
1374           gtk_ui_manager_remove_ui (priv->manager, priv->visibility_id);
1375           priv->visibility_id = 0;
1376         }
1377
1378       g_object_unref (priv->manager);
1379       priv->manager = NULL;
1380     }
1381
1382   if (priv->model)
1383     {
1384       egg_editable_toolbar_disconnect_model (etoolbar);
1385       g_object_unref (priv->model);
1386       priv->model = NULL;
1387     }
1388
1389   G_OBJECT_CLASS (egg_editable_toolbar_parent_class)->dispose (object);
1390 }
1391
1392 static void
1393 egg_editable_toolbar_set_ui_manager (EggEditableToolbar *etoolbar,
1394                                      GtkUIManager       *manager)
1395 {
1396   static const GtkActionEntry actions[] = {
1397     { "MoveToolItem", STOCK_DRAG_MODE, N_("_Move on Toolbar"), NULL,
1398       N_("Move the selected item on the toolbar"), G_CALLBACK (move_item_cb) },
1399     { "RemoveToolItem", GTK_STOCK_REMOVE, N_("_Remove from Toolbar"), NULL,
1400       N_("Remove the selected item from the toolbar"), G_CALLBACK (remove_item_cb) },
1401     { "RemoveToolbar", GTK_STOCK_DELETE, N_("_Delete Toolbar"), NULL,
1402       N_("Remove the selected toolbar"), G_CALLBACK (remove_toolbar_cb) },
1403   };
1404   
1405   etoolbar->priv->manager = g_object_ref (manager);
1406
1407   etoolbar->priv->actions = gtk_action_group_new ("ToolbarActions");
1408   gtk_action_group_set_translation_domain (etoolbar->priv->actions, GETTEXT_PACKAGE);
1409   gtk_action_group_add_actions (etoolbar->priv->actions, actions,
1410                                 G_N_ELEMENTS (actions), etoolbar);
1411   gtk_ui_manager_insert_action_group (manager, etoolbar->priv->actions, -1);
1412   g_object_unref (etoolbar->priv->actions);
1413
1414   toolbar_visibility_refresh (etoolbar);
1415 }
1416
1417 GtkWidget * egg_editable_toolbar_get_selected (EggEditableToolbar   *etoolbar)
1418 {
1419   return etoolbar->priv->selected;
1420 }
1421
1422 void
1423 egg_editable_toolbar_set_selected (EggEditableToolbar *etoolbar,
1424                                    GtkWidget          *widget)
1425 {
1426   GtkWidget *toolbar, *toolitem;
1427   gboolean editable;
1428
1429   etoolbar->priv->selected = widget;
1430   
1431   toolbar = (widget != NULL) ? gtk_widget_get_ancestor (widget, GTK_TYPE_TOOLBAR) : NULL;
1432   toolitem = (widget != NULL) ? gtk_widget_get_ancestor (widget, GTK_TYPE_TOOL_ITEM) : NULL;
1433   
1434   if(toolbar != NULL)
1435     {
1436       gint tpos = get_toolbar_position (etoolbar, toolbar);
1437       editable = ((egg_toolbars_model_get_flags (etoolbar->priv->model, tpos) & EGG_TB_MODEL_NOT_EDITABLE) == 0);
1438     }
1439   else
1440     {
1441       editable = FALSE;
1442     }
1443   
1444   gtk_action_set_visible (find_action (etoolbar, "RemoveToolbar"), (toolbar != NULL) && (etoolbar->priv->edit_mode > 0));
1445   gtk_action_set_visible (find_action (etoolbar, "RemoveToolItem"), (toolitem != NULL) && editable);
1446   gtk_action_set_visible (find_action (etoolbar, "MoveToolItem"), (toolitem != NULL) && editable);
1447 }
1448
1449 static void
1450 set_edit_mode (EggEditableToolbar *etoolbar,
1451                gboolean mode)
1452 {
1453   EggEditableToolbarPrivate *priv = etoolbar->priv;
1454   int i, l, n_items;
1455
1456   i = priv->edit_mode;
1457   if (mode)
1458     {
1459       priv->edit_mode++;
1460     }
1461   else
1462     {
1463       g_return_if_fail (priv->edit_mode > 0);
1464       priv->edit_mode--;
1465     }
1466   i *= priv->edit_mode;
1467   
1468   if (i == 0)
1469     {
1470       for (i = get_n_toolbars (etoolbar)-1; i >= 0; i--)
1471         {
1472           GtkWidget *toolbar;
1473           
1474           toolbar = get_toolbar_nth (etoolbar, i);
1475           n_items = gtk_toolbar_get_n_items (GTK_TOOLBAR (toolbar));
1476
1477           if (n_items == 0 && priv->edit_mode == 0)
1478             {
1479               egg_toolbars_model_remove_toolbar (priv->model, i);
1480             }
1481           else
1482             {          
1483               for (l = 0; l < n_items; l++)
1484                 {
1485                   GtkToolItem *item;
1486               
1487                   item = gtk_toolbar_get_nth_item (GTK_TOOLBAR (toolbar), l);
1488                   
1489                   configure_item_cursor (item, etoolbar);
1490                   configure_item_sensitivity (item, etoolbar);
1491                 }
1492             }
1493         }
1494     }
1495 }
1496
1497 static void
1498 egg_editable_toolbar_set_property (GObject      *object,
1499                                    guint         prop_id,
1500                                    const GValue *value,
1501                                    GParamSpec   *pspec)
1502 {
1503   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1504
1505   switch (prop_id)
1506     {
1507     case PROP_UI_MANAGER:
1508       egg_editable_toolbar_set_ui_manager (etoolbar, g_value_get_object (value));
1509       break;
1510     case PROP_TOOLBARS_MODEL:
1511       egg_editable_toolbar_set_model (etoolbar, g_value_get_object (value));
1512       break;
1513     case PROP_SELECTED:
1514       egg_editable_toolbar_set_selected (etoolbar, g_value_get_object (value));
1515       break;
1516     case PROP_POPUP_PATH:
1517       etoolbar->priv->popup_path = g_strdup (g_value_get_string (value));
1518       break;
1519     case PROP_EDIT_MODE:
1520       set_edit_mode (etoolbar, g_value_get_boolean (value));
1521       break;
1522     default:
1523       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1524       break;
1525     }
1526 }
1527
1528 static void
1529 egg_editable_toolbar_get_property (GObject    *object,
1530                                    guint       prop_id,
1531                                    GValue     *value,
1532                                    GParamSpec *pspec)
1533 {
1534   EggEditableToolbar *etoolbar = EGG_EDITABLE_TOOLBAR (object);
1535
1536   switch (prop_id)
1537     {
1538     case PROP_UI_MANAGER:
1539       g_value_set_object (value, etoolbar->priv->manager);
1540       break;
1541     case PROP_TOOLBARS_MODEL:
1542       g_value_set_object (value, etoolbar->priv->model);
1543       break;
1544     case PROP_SELECTED:
1545       g_value_set_object (value, etoolbar->priv->selected);
1546       break;
1547     case PROP_EDIT_MODE:
1548       g_value_set_boolean (value, etoolbar->priv->edit_mode>0);
1549       break;
1550     default:
1551       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1552       break;
1553     }
1554 }
1555
1556 static void
1557 egg_editable_toolbar_class_init (EggEditableToolbarClass *klass)
1558 {
1559   GObjectClass *object_class = G_OBJECT_CLASS (klass);
1560
1561   object_class->dispose = egg_editable_toolbar_dispose;
1562   object_class->set_property = egg_editable_toolbar_set_property;
1563   object_class->get_property = egg_editable_toolbar_get_property;
1564
1565   egg_editable_toolbar_signals[ACTION_REQUEST] =
1566     g_signal_new ("action_request",
1567                   G_OBJECT_CLASS_TYPE (object_class),
1568                   G_SIGNAL_RUN_LAST,
1569                   G_STRUCT_OFFSET (EggEditableToolbarClass, action_request),
1570                   NULL, NULL, g_cclosure_marshal_VOID__STRING,
1571                   G_TYPE_NONE, 1, G_TYPE_STRING);
1572
1573   g_object_class_install_property (object_class,
1574                                    PROP_UI_MANAGER,
1575                                    g_param_spec_object ("ui-manager",
1576                                                         "UI-Mmanager",
1577                                                         "UI Manager",
1578                                                         GTK_TYPE_UI_MANAGER,
1579                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1580   g_object_class_install_property (object_class,
1581                                    PROP_TOOLBARS_MODEL,
1582                                    g_param_spec_object ("model",
1583                                                         "Model",
1584                                                         "Toolbars Model",
1585                                                         EGG_TYPE_TOOLBARS_MODEL,
1586                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1587   g_object_class_install_property (object_class,
1588                                    PROP_SELECTED,
1589                                    g_param_spec_object ("selected",
1590                                                         "Selected",
1591                                                         "Selected toolitem",
1592                                                         GTK_TYPE_TOOL_ITEM,
1593                                                         G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1594
1595   g_object_class_install_property (object_class,
1596                                    PROP_POPUP_PATH,
1597                                    g_param_spec_string ("popup-path",
1598                                                         "popup-path",
1599                                                         "popup-path",
1600                                                         NULL,
1601                                                         G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1602
1603   g_object_class_install_property (object_class,
1604                                    PROP_EDIT_MODE,
1605                                    g_param_spec_boolean ("edit-mode",
1606                                                          "Edit-Mode",
1607                                                          "Edit Mode",
1608                                                          FALSE,
1609                                                          G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
1610
1611   g_type_class_add_private (object_class, sizeof (EggEditableToolbarPrivate));
1612 }
1613
1614 GtkWidget *
1615 egg_editable_toolbar_new (GtkUIManager *manager,
1616                           const char *popup_path)
1617 {
1618     return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
1619                                      "ui-manager", manager,
1620                                      "popup-path", popup_path,
1621                                      NULL));
1622 }
1623
1624 GtkWidget *
1625 egg_editable_toolbar_new_with_model (GtkUIManager *manager,
1626                                      EggToolbarsModel *model,
1627                                      const char *popup_path)
1628 {
1629   return GTK_WIDGET (g_object_new (EGG_TYPE_EDITABLE_TOOLBAR,
1630                                    "ui-manager", manager,
1631                                    "model", model,
1632                                    "popup-path", popup_path,
1633                                    NULL));
1634 }
1635
1636 gboolean
1637 egg_editable_toolbar_get_edit_mode (EggEditableToolbar *etoolbar)
1638 {
1639   EggEditableToolbarPrivate *priv = etoolbar->priv;
1640
1641   return priv->edit_mode > 0;
1642 }
1643
1644 void
1645 egg_editable_toolbar_set_edit_mode (EggEditableToolbar *etoolbar,
1646                                     gboolean mode)
1647 {
1648   set_edit_mode (etoolbar, mode);
1649   g_object_notify (G_OBJECT (etoolbar), "edit-mode");
1650 }
1651
1652 void
1653 egg_editable_toolbar_add_visibility (EggEditableToolbar *etoolbar,
1654                                      const char *path)
1655 {
1656   etoolbar->priv->visibility_paths = g_list_prepend
1657           (etoolbar->priv->visibility_paths, g_strdup (path));
1658 }
1659
1660 void
1661 egg_editable_toolbar_show (EggEditableToolbar *etoolbar,
1662                            const char *name)
1663 {
1664   EggEditableToolbarPrivate *priv = etoolbar->priv;
1665   EggToolbarsModel *model = priv->model;
1666   int i, n_toolbars;
1667
1668   n_toolbars = egg_toolbars_model_n_toolbars (model);
1669   for (i = 0; i < n_toolbars; i++)
1670     {
1671       const char *toolbar_name;
1672
1673       toolbar_name = egg_toolbars_model_toolbar_nth (model, i);
1674       if (strcmp (toolbar_name, name) == 0)
1675         {
1676           gtk_widget_show (get_dock_nth (etoolbar, i));
1677         }
1678     }
1679 }
1680
1681 void
1682 egg_editable_toolbar_hide (EggEditableToolbar *etoolbar,
1683                            const char *name)
1684 {
1685   EggEditableToolbarPrivate *priv = etoolbar->priv;
1686   EggToolbarsModel *model = priv->model;
1687   int i, n_toolbars;
1688
1689   n_toolbars = egg_toolbars_model_n_toolbars (model);
1690   for (i = 0; i < n_toolbars; i++)
1691     {
1692       const char *toolbar_name;
1693
1694       toolbar_name = egg_toolbars_model_toolbar_nth (model, i);
1695       if (strcmp (toolbar_name, name) == 0)
1696       {
1697         gtk_widget_hide (get_dock_nth (etoolbar, i));
1698       }
1699     }
1700 }
1701
1702 void
1703 egg_editable_toolbar_set_fixed (EggEditableToolbar *etoolbar,
1704                                 GtkToolbar *toolbar)
1705 {
1706   EggEditableToolbarPrivate *priv = etoolbar->priv;
1707
1708   g_return_if_fail (!toolbar || GTK_IS_TOOLBAR (toolbar));
1709
1710   if (priv->fixed_toolbar)
1711     {
1712       unparent_fixed (etoolbar);
1713       g_object_unref (priv->fixed_toolbar);
1714       priv->fixed_toolbar = NULL;
1715     }
1716
1717   if (toolbar)
1718     {
1719       priv->fixed_toolbar = GTK_WIDGET (toolbar);
1720       gtk_toolbar_set_show_arrow (toolbar, FALSE);
1721       g_object_ref_sink (toolbar);
1722     }
1723
1724   update_fixed (etoolbar);
1725 }
1726
1727 #define DEFAULT_ICON_HEIGHT 20
1728 #define DEFAULT_ICON_WIDTH 0
1729
1730 static void
1731 fake_expose_widget (GtkWidget *widget,
1732                     GdkPixmap *pixmap)
1733 {
1734   GdkWindow *tmp_window;
1735   GdkEventExpose event;
1736
1737   event.type = GDK_EXPOSE;
1738   event.window = pixmap;
1739   event.send_event = FALSE;
1740   event.area = widget->allocation;
1741   event.region = NULL;
1742   event.count = 0;
1743
1744   tmp_window = widget->window;
1745   widget->window = pixmap;
1746   gtk_widget_send_expose (widget, (GdkEvent *) &event);
1747   widget->window = tmp_window;
1748 }
1749
1750 /* We should probably experiment some more with this.
1751  * Right now the rendered icon is pretty good for most
1752  * themes. However, the icon is slightly large for themes
1753  * with large toolbar icons.
1754  */
1755 static GdkPixbuf *
1756 new_pixbuf_from_widget (GtkWidget *widget)
1757 {
1758   GtkWidget *window;
1759   GdkPixbuf *pixbuf;
1760   GtkRequisition requisition;
1761   GtkAllocation allocation;
1762   GdkPixmap *pixmap;
1763   GdkVisual *visual;
1764   gint icon_width;
1765   gint icon_height;
1766   GdkScreen *screen;
1767
1768   icon_width = DEFAULT_ICON_WIDTH;
1769
1770   screen = gtk_widget_get_screen (widget);
1771
1772   if (!gtk_icon_size_lookup_for_settings (gtk_settings_get_for_screen (screen),
1773                                           GTK_ICON_SIZE_LARGE_TOOLBAR,
1774                                           NULL, 
1775                                           &icon_height))
1776     {
1777       icon_height = DEFAULT_ICON_HEIGHT;
1778     }
1779
1780   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1781   
1782   gtk_container_add (GTK_CONTAINER (window), widget);
1783   gtk_widget_realize (window);
1784   gtk_widget_show (widget);
1785   gtk_widget_realize (widget);
1786   gtk_widget_map (widget);
1787
1788   /* Gtk will never set the width or height of a window to 0. So setting the width to
1789    * 0 and than getting it will provide us with the minimum width needed to render
1790    * the icon correctly, without any additional window background noise.
1791    * This is needed mostly for pixmap based themes.
1792    */
1793   gtk_window_set_default_size (GTK_WINDOW (window), icon_width, icon_height);
1794   gtk_window_get_size (GTK_WINDOW (window),&icon_width, &icon_height);
1795
1796   gtk_widget_size_request (window, &requisition);
1797   allocation.x = 0;
1798   allocation.y = 0;
1799   allocation.width = icon_width;
1800   allocation.height = icon_height;
1801   gtk_widget_size_allocate (window, &allocation);
1802   gtk_widget_size_request (window, &requisition);
1803   
1804   /* Create a pixmap */
1805   visual = gtk_widget_get_visual (window);
1806   pixmap = gdk_pixmap_new (NULL, icon_width, icon_height, visual->depth);
1807   gdk_drawable_set_colormap (GDK_DRAWABLE (pixmap), gtk_widget_get_colormap (window));
1808
1809   /* Draw the window */
1810   gtk_widget_ensure_style (window);
1811   g_assert (window->style);
1812   g_assert (window->style->font_desc);
1813   
1814   fake_expose_widget (window, pixmap);
1815   fake_expose_widget (widget, pixmap);
1816   
1817   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, icon_width, icon_height);
1818   gdk_pixbuf_get_from_drawable (pixbuf, pixmap, NULL, 0, 0, 0, 0, icon_width, icon_height);
1819
1820   gtk_widget_destroy (window);
1821
1822   return pixbuf;
1823 }
1824
1825 static GdkPixbuf *
1826 new_separator_pixbuf (void)
1827 {
1828   GtkWidget *separator;
1829   GdkPixbuf *pixbuf;
1830
1831   separator = gtk_vseparator_new ();
1832   pixbuf = new_pixbuf_from_widget (separator);
1833   return pixbuf;
1834 }
1835
1836 static void
1837 update_separator_image (GtkImage *image)
1838 {
1839   GdkPixbuf *pixbuf = new_separator_pixbuf ();
1840   gtk_image_set_from_pixbuf (GTK_IMAGE (image), pixbuf);
1841   g_object_unref (pixbuf);
1842 }
1843
1844 static gboolean
1845 style_set_cb (GtkWidget *widget,
1846               GtkStyle *previous_style,
1847               GtkImage *image)
1848 {
1849
1850   update_separator_image (image);
1851   return FALSE;
1852 }
1853
1854 GtkWidget *
1855 _egg_editable_toolbar_new_separator_image (void)
1856 {
1857   GtkWidget *image = gtk_image_new ();
1858   update_separator_image (GTK_IMAGE (image));
1859   g_signal_connect (G_OBJECT (image), "style_set",
1860                     G_CALLBACK (style_set_cb), GTK_IMAGE (image));
1861
1862   return image;
1863 }
1864
1865 EggToolbarsModel *
1866 egg_editable_toolbar_get_model (EggEditableToolbar *etoolbar)
1867 {
1868   return etoolbar->priv->model;
1869 }