Bugfix: unselect item when menu is closed
[rox-filer.git] / ROX-Filer / src / menu.c
1 /*
2  * ROX-Filer, filer for the ROX desktop project
3  * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the Free
7  * Software Foundation; either version 2 of the License, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17  * Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 /* menu.c - code for handling the popup menus */
21
22 #ifndef GTK_STOCK_INFO
23 # define GTK_STOCK_INFO "gtk-info"
24 #endif
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/wait.h>
31 #include <sys/param.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <dirent.h>
36
37 #include <gtk/gtk.h>
38
39 #include "global.h"
40
41 #include "menu.h"
42 #include "run.h"
43 #include "action.h"
44 #include "filer.h"
45 #include "pixmaps.h"
46 #include "type.h"
47 #include "support.h"
48 #include "gui_support.h"
49 #include "options.h"
50 #include "choices.h"
51 #include "gtksavebox.h"
52 #include "mount.h"
53 #include "minibuffer.h"
54 #include "i18n.h"
55 #include "main.h"
56 #include "pinboard.h"
57 #include "dir.h"
58 #include "diritem.h"
59 #include "appmenu.h"
60 #include "usericons.h"
61 #include "infobox.h"
62 #include "view_iface.h"
63 #include "display.h"
64 #include "bookmarks.h"
65 #include "panel.h"
66 #include "bulk_rename.h"
67 #include "xtypes.h"
68 #include "log.h"
69
70 typedef enum {
71         FILE_COPY_ITEM,
72         FILE_RENAME_ITEM,
73         FILE_LINK_ITEM,
74         FILE_OPEN_FILE,
75         FILE_PROPERTIES,
76         FILE_RUN_ACTION,
77         FILE_SET_ICON,
78         FILE_SEND_TO,
79         FILE_DELETE,
80         FILE_USAGE,
81         FILE_CHMOD_ITEMS,
82         FILE_FIND,
83         FILE_SET_TYPE,
84 } FileOp;
85
86 typedef void (*ActionFn)(GList *paths,
87                          const char *dest_dir, const char *leaf, int quiet);
88 typedef void MenuCallback(GtkWidget *widget, gpointer data);
89
90 typedef gboolean (*SaveCb)(GObject *savebox,
91                            const gchar *current, const gchar *new);
92
93 GtkAccelGroup   *filer_keys = NULL;
94 static gboolean filer_keys_need_init = TRUE;
95
96 static GtkWidget *popup_menu = NULL;    /* Currently open menu */
97
98 static gint updating_menu = 0;          /* Non-zero => ignore activations */
99 static GList *send_to_paths = NULL;
100
101 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
102
103 /* Static prototypes */
104
105 static void save_menus(void);
106 static void menu_closed(GtkWidget *widget);
107 static void shade_file_menu_items(gboolean shaded);
108 static void savebox_show(const gchar *action, const gchar *path,
109                          MaskedPixmap *image, SaveCb callback,
110                          GdkDragAction dnd_action);
111 static gint save_to_file(GObject *savebox,
112                          const gchar *pathname, gpointer data);
113 static gboolean action_with_leaf(ActionFn action,
114                                  const gchar *current, const gchar *new);
115 static gboolean link_cb(GObject *savebox,
116                         const gchar *initial, const gchar *path);
117 static void select_nth_item(GtkMenuShell *shell, int n);
118 static void new_file_type(gchar *templ);
119 static void do_send_to(gchar *templ);
120 static void show_send_to_menu(GList *paths, GdkEvent *event);
121 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
122
123 /* Note that for most of these callbacks none of the arguments are used. */
124
125 static void view_type(gpointer data, guint action, GtkWidget *widget);
126
127 /* (action used in these three - DetailsType) */
128 static void change_size(gpointer data, guint action, GtkWidget *widget);
129 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
130 static void set_with(gpointer data, guint action, GtkWidget *widget);
131
132 static void set_sort(gpointer data, guint action, GtkWidget *widget);
133 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
134
135 static void filter_directories(gpointer data, guint action, GtkWidget *widget);
136 static void hidden(gpointer data, guint action, GtkWidget *widget);
137 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
138 static void refresh(gpointer data, guint action, GtkWidget *widget);
139 static void save_settings(gpointer data, guint action, GtkWidget *widget);
140
141 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
142
143 static void select_all(gpointer data, guint action, GtkWidget *widget);
144 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
145 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
146 static void new_directory(gpointer data, guint action, GtkWidget *widget);
147 static void new_file(gpointer data, guint action, GtkWidget *widget);
148 static void customise_new(gpointer data);
149 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
150
151 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
152 static void open_parent(gpointer data, guint action, GtkWidget *widget);
153 static void home_directory(gpointer data, guint action, GtkWidget *widget);
154 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
155 static void show_log(gpointer data, guint action, GtkWidget *widget);
156 static void new_window(gpointer data, guint action, GtkWidget *widget);
157 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
158 static void close_window(gpointer data, guint action, GtkWidget *widget);
159 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
160
161 /* (action used in this - MiniType) */
162 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
163 static void resize(gpointer data, guint action, GtkWidget *widget);
164
165 #define MENUS_NAME "menus2"
166
167 static GtkWidget        *filer_menu;            /* The popup filer menu */
168 static GtkWidget        *filer_file_item;       /* The File '' label */
169 static GtkWidget        *filer_file_menu;       /* The File '' menu */
170 static GtkWidget        *file_shift_item;       /* Shift Open label */
171 static GtkWidget        *filer_auto_size_menu;  /* The Automatic item */
172 static GtkWidget        *filer_hidden_menu;     /* The Show Hidden item */
173 static GtkWidget        *filer_filter_dirs_menu;/* The Filter Dirs item */
174 static GtkWidget        *filer_reverse_menu;    /* The Reversed item */
175 static GtkWidget        *filer_thumb_menu;      /* The Show Thumbs item */
176 static GtkWidget        *filer_new_window;      /* The New Window item */
177 static GtkWidget        *filer_new_menu;        /* The New submenu */
178 static GtkWidget        *filer_follow_sym;      /* Follow symbolic links item */
179 static GtkWidget        *filer_set_type;        /* Set type item */
180
181 #undef N_
182 #define N_(x) x
183
184 static GtkItemFactoryEntry filer_menu_def[] = {
185 {N_("Display"),                 NULL, NULL, 0, "<Branch>"},
186 {">" N_("Icons View"),          NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
187 {">" N_("Icons, With..."),      NULL, NULL, 0, "<Branch>"},
188 {">>" N_("Sizes"),              NULL, set_with, DETAILS_SIZE, NULL},
189 {">>" N_("Permissions"),        NULL, set_with, DETAILS_PERMISSIONS, NULL},
190 {">>" N_("Types"),              NULL, set_with, DETAILS_TYPE, NULL},
191 {">>" N_("Times"),              NULL, set_with, DETAILS_TIMES, NULL},
192 {">" N_("List View"),           NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
193 {">",                           NULL, NULL, 0, "<Separator>"},
194 {">" N_("Bigger Icons"),        "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
195 {">" N_("Smaller Icons"),       "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
196 {">" N_("Automatic"),           NULL, change_size_auto, 0, "<ToggleItem>"},
197 {">",                           NULL, NULL, 0, "<Separator>"},
198 {">" N_("Sort by Name"),        NULL, set_sort, SORT_NAME, NULL},
199 {">" N_("Sort by Type"),        NULL, set_sort, SORT_TYPE, NULL},
200 {">" N_("Sort by Date"),        NULL, set_sort, SORT_DATE, NULL},
201 {">" N_("Sort by Size"),        NULL, set_sort, SORT_SIZE, NULL},
202 {">" N_("Sort by Owner"),       NULL, set_sort, SORT_OWNER, NULL},
203 {">" N_("Sort by Group"),       NULL, set_sort, SORT_GROUP, NULL},
204 {">" N_("Reversed"),            NULL, reverse_sort, 0, "<ToggleItem>"},
205 {">",                           NULL, NULL, 0, "<Separator>"},
206 {">" N_("Show Hidden"),         "<Ctrl>H", hidden, 0, "<ToggleItem>"},
207 {">" N_("Filter Files..."),     NULL, mini_buffer, MINI_FILTER, NULL},
208 {">" N_("Filter Directories With Files"),       NULL, filter_directories, 0, "<ToggleItem>"},
209 {">" N_("Show Thumbnails"),     NULL, show_thumbs, 0, "<ToggleItem>"},
210 {">" N_("Refresh"),             NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
211 {">" N_("Save Current Display Settings..."),     NULL, save_settings, 0, NULL},
212 {N_("File"),                    NULL, NULL, 0, "<Branch>"},
213 {">" N_("Copy..."),             "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
214 {">" N_("Rename..."),           NULL, file_op, FILE_RENAME_ITEM, NULL},
215 {">" N_("Link..."),             NULL, file_op, FILE_LINK_ITEM, NULL},
216 {">" N_("Delete"),              "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
217 {">",                           NULL, NULL, 0, "<Separator>"},
218 {">" N_("Shift Open"),          NULL, file_op, FILE_OPEN_FILE},
219 {">" N_("Send To..."),          NULL, file_op, FILE_SEND_TO, NULL},
220 {">",                           NULL, NULL, 0, "<Separator>"},
221 {">" N_("Set Run Action..."),   "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
222 {">" N_("Set Icon..."),         NULL, file_op, FILE_SET_ICON, NULL},
223 {">" N_("Properties"),          "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
224 {">" N_("Count"),               NULL, file_op, FILE_USAGE, NULL},
225 {">" N_("Set Type..."),         NULL, file_op, FILE_SET_TYPE, NULL},
226 {">" N_("Permissions"),         NULL, file_op, FILE_CHMOD_ITEMS, NULL},
227 {">",                           NULL, NULL, 0, "<Separator>"},
228 {">" N_("Find"),                "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
229 {N_("Select"),                  NULL, NULL, 0, "<Branch>"},
230 {">" N_("Select All"),          "<Ctrl>A", select_all, 0, NULL},
231 {">" N_("Clear Selection"),     NULL, clear_selection, 0, NULL},
232 {">" N_("Invert Selection"),    NULL, invert_selection, 0, NULL},
233 {">" N_("Select by Name..."),   "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
234 {">" N_("Select If..."),        "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
235 {N_("Options..."),              NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
236 {N_("New"),                     NULL, NULL, 0, "<Branch>"},
237 {">" N_("Directory"),           NULL, new_directory, 0, NULL},
238 {">" N_("Blank file"),          NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
239 {">" N_("Customise Menu..."),   NULL, customise_new, 0, NULL},
240 {N_("Window"),                  NULL, NULL, 0, "<Branch>"},
241 {">" N_("Parent, New Window"),  NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
242 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
243 {">" N_("New Window"),          NULL, new_window, 0, NULL},
244 {">" N_("Home Directory"),      "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
245 {">" N_("Show Bookmarks"),      "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
246 {">" N_("Show Log"),            NULL, show_log, 0, "<StockItem>", GTK_STOCK_INFO},
247 {">" N_("Follow Symbolic Links"),       NULL, follow_symlinks, 0, NULL},
248 {">" N_("Resize Window"),       "<Ctrl>E", resize, 0, NULL},
249 /* {">" N_("New, As User..."),  NULL, new_user, 0, NULL}, */
250
251 {">" N_("Close Window"),        "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
252 {">",                           NULL, NULL, 0, "<Separator>"},
253 {">" N_("Enter Path..."),       "slash", mini_buffer, MINI_PATH, NULL},
254 {">" N_("Shell Command..."),    "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
255 {">" N_("Terminal Here"),       "grave", xterm_here, FALSE, NULL},
256 {">" N_("Switch to Terminal"),  NULL, xterm_here, TRUE, NULL},
257 {N_("Help"),                    NULL, NULL, 0, "<Branch>"},
258 {">" N_("About ROX-Filer..."),  NULL, menu_rox_help, HELP_ABOUT, NULL},
259 {">" N_("Show Help Files"),     "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
260 {">" N_("Manual"),              NULL, menu_rox_help, HELP_MANUAL, NULL},
261 };
262
263
264 #define GET_MENU_ITEM(var, menu)        \
265                 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
266
267 #define GET_SMENU_ITEM(var, menu, sub)  \
268         do {                            \
269                 tmp = g_strdup_printf("<" menu ">/%s", _(sub));         \
270                 var = gtk_item_factory_get_widget(item_factory, tmp);   \
271                 g_free(tmp);            \
272         } while (0)
273
274 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
275         do {                            \
276                 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
277                 var = gtk_item_factory_get_widget(item_factory, tmp);   \
278                 g_free(tmp);            \
279         } while (0)
280
281 /* Returns TRUE if the keys were installed (first call only) */
282 gboolean ensure_filer_menu(void)
283 {
284         GList                   *items;
285         guchar                  *tmp;
286         GtkWidget               *item;
287         GtkItemFactory          *item_factory;
288
289         if (!filer_keys_need_init)
290                 return FALSE;
291         filer_keys_need_init = FALSE;
292
293         item_factory = menu_create(filer_menu_def,
294                 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
295                 "<filer>", filer_keys);
296
297         GET_MENU_ITEM(filer_menu, "filer");
298         GET_SMENU_ITEM(filer_file_menu, "filer", "File");
299         GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
300         GET_SSMENU_ITEM(filer_filter_dirs_menu, "filer", "Display", "Filter Directories With Files");
301         GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
302         GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
303         GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
304                                                         "Show Thumbnails");
305         GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
306         filer_set_type = GTK_BIN(item)->child;
307
308         GET_SMENU_ITEM(filer_new_menu, "filer", "New");
309         GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
310         filer_follow_sym = GTK_BIN(item)->child;
311
312         /* File '' label... */
313         items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
314         filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
315         g_list_free(items);
316
317         /* Shift Open... label */
318         items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
319         file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
320         g_list_free(items);
321
322         GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
323         filer_new_window = GTK_BIN(item)->child;
324
325         g_signal_connect(filer_menu, "selection-done",
326                         G_CALLBACK(menu_closed), NULL);
327         g_signal_connect(filer_file_menu, "selection-done",
328                         G_CALLBACK(menu_closed), NULL);
329
330         g_signal_connect(filer_keys, "accel_changed",
331                         G_CALLBACK(save_menus), NULL);
332
333         return TRUE;
334 }
335
336 void menu_init(void)
337 {
338         char                    *menurc;
339
340         menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
341         if (menurc)
342         {
343                 gtk_accel_map_load(menurc);
344                 g_free(menurc);
345         }
346
347         option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
348         option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
349         option_add_int(&o_menu_quick, "menu_quick", FALSE);
350         option_add_saver(save_menus);
351
352         option_register_widget("menu-set-keys", set_keys_button);
353
354         filer_keys = gtk_accel_group_new();
355 }
356
357 /* Name is in the form "<panel>" */
358 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
359                             const gchar *name, GtkAccelGroup *keys)
360 {
361         GtkItemFactory          *item_factory;
362         GtkItemFactoryEntry     *translated;
363
364         if (!keys)
365         {
366                 keys = gtk_accel_group_new();
367                 gtk_accel_group_lock(keys);
368         }
369
370         item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
371
372         translated = translate_entries(def, n_entries);
373         gtk_item_factory_create_items(item_factory, n_entries,
374                                         translated, NULL);
375         free_translated_entries(translated, n_entries);
376
377         return item_factory;
378 }
379
380 /* Prevent the user from setting a short-cut on this item */
381 static void menuitem_no_shortcuts(GtkWidget *item)
382 {
383         /* XXX */
384 #if 0
385         GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
386
387         _gtk_widget_set_accel_path(item, NULL, NULL);
388         null_g_free(&menuitem->accel_path);
389 #endif
390 }
391
392 /* Shade items that only work on single files */
393 static void shade_file_menu_items(gboolean shaded)
394 {
395         menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
396         menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
397         menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
398         menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
399 }
400
401 /* 'data' is an array of three ints:
402  * [ pointer_x, pointer_y, item_under_pointer ]
403  */
404 void position_menu(GtkMenu *menu, gint *x, gint *y,
405                    gboolean  *push_in, gpointer data)
406 {
407         int             *pos = (int *) data;
408         GtkRequisition  requisition;
409         GList           *items, *next;
410         int             y_shift = 0;
411         int             item = pos[2];
412
413         next = items = gtk_container_get_children(GTK_CONTAINER(menu));
414
415         while (item >= 0 && next)
416         {
417                 int h = ((GtkWidget *) next->data)->requisition.height;
418
419                 if (item > 0)
420                         y_shift += h;
421                 else
422                         y_shift += h / 2;
423
424                 next = next->next;
425                 item--;
426         }
427         
428         g_list_free(items);
429
430         gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
431
432         *x = pos[0] - (requisition.width * 7 / 8);
433         *y = pos[1] - y_shift;
434
435         *x = CLAMP(*x, 0, screen_width - requisition.width);
436         *y = CLAMP(*y, 0, screen_height - requisition.height);
437
438         *push_in = FALSE;
439 }
440
441 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
442                                 MenuIconStyle style)
443 {
444         GtkWidget *item;
445         
446         if (style != MIS_NONE && di_image(ditem))
447         {
448                 GdkPixbuf *pixbuf;
449                 MaskedPixmap *image;
450
451                 image = di_image(ditem);
452
453                 switch (style)
454                 {
455                         case MIS_LARGE:
456                                 pixbuf = image->pixbuf;
457                                 break;
458                         default:
459                                 if (!image->sm_pixbuf)
460                                         pixmap_make_small(image);
461                                 pixbuf = image->sm_pixbuf;
462                                 break;
463                 }
464
465                 item = gtk_image_menu_item_new_with_label(label);
466                 /* TODO: Find a way to allow short-cuts */
467                 menuitem_no_shortcuts(item);
468
469                 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
470                                 gtk_image_new_from_pixbuf(pixbuf));
471                 gtk_widget_show_all(item);
472         }
473         else
474                 item = gtk_menu_item_new_with_label(label);
475
476         return item;
477 }
478
479 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
480                             MenuIconStyle style, CallbackFn func,
481                             gboolean separator, gboolean strip_ext,
482                             gboolean recurse)
483 {
484         GList *widgets = NULL;
485         DirItem *ditem;
486         int i;
487         GtkWidget *item;
488         char *dname = NULL;
489         GPtrArray *names;
490
491         dname = pathdup(dir_name);
492
493         names = list_dir(dname);
494         if (!names)
495                 goto out;
496
497         for (i = 0; i < names->len; i++)
498         {
499                 char    *leaf = names->pdata[i];
500                 gchar   *fname;
501
502                 if (separator)
503                 {
504                         item = gtk_menu_item_new();
505                         widgets = g_list_append(widgets, item);
506                         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
507                         separator = FALSE;
508                 }
509
510                 fname = g_strconcat(dname, "/", leaf, NULL);
511
512                 /* Strip off extension, if any */
513                 if (strip_ext)
514                 {
515                         char    *dot;
516                         dot = strchr(leaf, '.');
517                         if (dot)
518                                 *dot = '\0';
519                 }
520
521                 ditem = diritem_new("");
522                 diritem_restat(fname, ditem, NULL);
523
524                 item = make_send_to_item(ditem, leaf, style);
525
526                 g_free(leaf);
527
528                 /* If it is a directory (but NOT an AppDir) and we are
529                  * recursing then set up a sub menu.
530                  */
531                 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
532                            !(ditem->flags & ITEM_FLAG_APPDIR))
533                 {
534                         GtkWidget *sub;
535                         GList *new_widgets;
536
537                         sub = gtk_menu_new();
538                         new_widgets = menu_from_dir(sub, fname, style, func,
539                                                 separator, strip_ext, TRUE);
540                         g_list_free(new_widgets);
541                         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
542                 }
543                 else
544                         g_signal_connect_swapped(item, "activate",
545                                         G_CALLBACK(func), fname);
546
547                 diritem_free(ditem);
548
549                 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
550                 g_signal_connect_swapped(item, "destroy",
551                                 G_CALLBACK(g_free), fname);
552
553                 widgets = g_list_append(widgets, item);
554         }
555
556         g_ptr_array_free(names, TRUE);
557 out:
558         g_free(dname);
559
560         return widgets;
561 }
562
563 /* Scan the templates dir and create entries for the New menu */
564 static void update_new_files_menu(MenuIconStyle style)
565 {
566         static GList *widgets = NULL;
567
568         gchar   *templ_dname = NULL;
569
570         if (widgets)
571         {
572                 GList   *next;
573                 
574                 for (next = widgets; next; next = next->next)
575                         gtk_widget_destroy((GtkWidget *) next->data);
576
577                 g_list_free(widgets);
578                 widgets = NULL;
579         }
580
581         templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
582         if (templ_dname)
583         {
584                 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
585                                         (CallbackFn) new_file_type, TRUE, TRUE,
586                                         FALSE);
587                 g_free(templ_dname);
588         }
589         gtk_widget_show_all(filer_new_menu);
590 }
591
592 /* 'item' is the number of the item to appear under the pointer. */
593 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
594 {
595         int             pos[3];
596         int             button = 0;
597         guint32         time = 0;
598
599         if (event && (event->type == GDK_BUTTON_PRESS ||
600                         event->type == GDK_BUTTON_RELEASE))
601         {
602                 GdkEventButton *bev = (GdkEventButton *) event;
603
604                 pos[0] = bev->x_root;
605                 pos[1] = bev->y_root;
606                 button = bev->button;
607                 time = bev->time;
608         }
609         else if (event && event->type == GDK_KEY_PRESS)
610         {
611                 GdkEventKey *kev = (GdkEventKey *) event;
612
613                 get_pointer_xy(pos, pos + 1);
614                 time = kev->time;
615         }
616         else
617                 get_pointer_xy(pos, pos + 1);
618
619         pos[2] = item;
620
621         gtk_widget_show_all(menu);
622         gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
623                         position_menu, (gpointer) pos, button, time);
624         select_nth_item(GTK_MENU_SHELL(menu), item);
625 }
626
627 /* Hide the popup menu, if any */
628 void menu_popdown(void)
629 {
630         if (popup_menu)
631                 gtk_menu_popdown(GTK_MENU(popup_menu));
632 }
633
634 static MenuIconStyle get_menu_icon_style(void)
635 {
636         MenuIconStyle mis;
637         int display;
638
639         mis = o_menu_iconsize.int_value;
640
641         switch (mis)
642         {
643                 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
644                         return mis;
645                 default:
646                         break;
647         }
648
649         if (mis == MIS_CURRENT && window_with_focus)
650         {
651                 switch (window_with_focus->display_style)
652                 {
653                         case HUGE_ICONS:
654                         case LARGE_ICONS:
655                                 return MIS_LARGE;
656                         case SMALL_ICONS:
657                                 return MIS_SMALL;
658                         default:
659                                 break;
660                 }
661         }
662
663         display = o_display_size.int_value;
664         switch (display)
665         {
666                 case HUGE_ICONS:
667                 case LARGE_ICONS:
668                         return MIS_LARGE;
669                 case SMALL_ICONS:
670                         return MIS_SMALL;
671                 default:
672                         break;
673         }
674
675         return MIS_SMALL;
676 }
677
678 /* iter->peek() is the clicked item, or NULL if none */
679 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
680 {
681         DirItem         *file_item = NULL;
682         GdkModifierType state = 0;
683         int             n_selected;
684         int             n_added = 0;
685
686         g_return_if_fail(event != NULL);
687
688         n_selected = view_count_selected(filer_window->view);
689
690         ensure_filer_menu();
691
692         updating_menu++;
693
694         /* Remove previous AppMenu, if any */
695         appmenu_remove();
696
697         window_with_focus = filer_window;
698
699         if (event->type == GDK_BUTTON_PRESS)
700                 state = ((GdkEventButton *) event)->state;
701         else if (event->type == GDK_KEY_PRESS)
702                 state = ((GdkEventKey *) event)->state;
703
704         if (n_selected == 0 && iter && iter->peek(iter) != NULL)
705         {
706                 filer_window->temp_item_selected = TRUE;
707                 view_set_selected(filer_window->view, iter, TRUE);
708                 n_selected = view_count_selected(filer_window->view);
709         }
710         else
711         {
712                 filer_window->temp_item_selected = FALSE;
713         }
714
715         /* Short-cut to the Send To menu */
716         if (state & GDK_SHIFT_MASK)
717         {
718                 GList *paths;
719
720                 updating_menu--;
721
722                 if (n_selected == 0)
723                 {
724                         report_error(
725                                 _("You should Shift+Menu click over a file to "
726                                 "send it somewhere"));
727                         return;
728                 }
729
730                 paths = filer_selected_items(filer_window);
731
732                 show_send_to_menu(paths, event); /* (paths eaten) */
733
734                 return;
735         }
736
737         {
738                 GtkWidget       *file_label, *file_menu;
739                 GString         *buffer;
740                 DirItem         *item;
741
742                 file_label = filer_file_item;
743                 file_menu = filer_file_menu;
744                 gtk_check_menu_item_set_active(
745                                 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
746                                 filer_window->show_thumbs);
747                 gtk_check_menu_item_set_active(
748                                 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
749                                 filer_window->show_hidden);
750                 gtk_check_menu_item_set_active(
751                                 GTK_CHECK_MENU_ITEM(filer_filter_dirs_menu),
752                                 filer_window->filter_directories);
753                 gtk_check_menu_item_set_active(
754                                 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
755                                 filer_window->sort_order != GTK_SORT_ASCENDING);
756                 gtk_check_menu_item_set_active(
757                         GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
758                         filer_window->display_style_wanted == AUTO_SIZE_ICONS);
759                 buffer = g_string_new(NULL);
760
761                 switch (n_selected)
762                 {
763                         case 0:
764                                 g_string_assign(buffer, _("Next Click"));
765                                 shade_file_menu_items(FALSE);
766                                 break;
767                         case 1:
768                                 item = filer_selected_item(filer_window);
769                                 if (item->base_type == TYPE_UNKNOWN)
770                                         dir_update_item(filer_window->directory,
771                                                         item->leafname);
772                                 shade_file_menu_items(FALSE);
773                                 file_item = filer_selected_item(filer_window);
774                                 g_string_printf(buffer, _("%s '%s'"),
775                                         basetype_name(file_item),
776                                         g_utf8_validate(file_item->leafname,
777                                                         -1, NULL)
778                                                 ? file_item->leafname
779                                                 : _("(bad utf-8)"));
780                                 if (!can_set_run_action(file_item))
781                                         menu_set_items_shaded(filer_file_menu,
782                                                         TRUE, 8, 1);
783                                 break;
784                         default:
785                                 shade_file_menu_items(TRUE);
786                                 g_string_printf(buffer, _("%d items"),
787                                                  n_selected);
788                                 break;
789                 }
790                 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
791                 g_string_free(buffer, TRUE);
792
793                 menu_show_shift_action(file_shift_item, file_item,
794                                         n_selected == 0);
795                 if (file_item)
796                         n_added = appmenu_add(make_path(filer_window->sym_path,
797                                                         file_item->leafname),
798                                                 file_item, filer_file_menu);
799         }
800
801         update_new_files_menu(get_menu_icon_style());
802
803         gtk_widget_set_sensitive(filer_new_window,
804                         !o_unique_filer_windows.int_value);
805         gtk_widget_set_sensitive(filer_follow_sym,
806                 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
807         gtk_widget_set_sensitive(filer_set_type,
808                                  xattr_supported(filer_window->real_path));
809
810         if (n_selected && o_menu_quick.int_value) 
811                 popup_menu = (state & GDK_CONTROL_MASK)
812                                         ? filer_menu
813                                         : filer_file_menu;
814         else 
815                 popup_menu = (state & GDK_CONTROL_MASK)
816                                         ? filer_file_menu
817                                         : filer_menu;
818
819         updating_menu--;
820         
821         show_popup_menu(popup_menu, event,
822                         popup_menu == filer_file_menu ? n_added : 1);
823 }
824
825 static void menu_closed(GtkWidget *widget)
826 {
827         if (window_with_focus == NULL || widget != popup_menu)
828                 return;                 /* Close panel item chosen? */
829
830         popup_menu = NULL;
831
832         if (window_with_focus->temp_item_selected)
833         {
834                 view_clear_selection(window_with_focus->view);
835                 window_with_focus->temp_item_selected = FALSE;
836         }
837
838         appmenu_remove();
839 }
840
841 static void target_callback(FilerWindow *filer_window,
842                         ViewIter *iter,
843                         gpointer action)
844 {
845         g_return_if_fail(filer_window != NULL);
846
847         window_with_focus = filer_window;
848         
849         /* Don't grab the primary selection */
850         filer_window->temp_item_selected = TRUE;
851         
852         view_wink_item(filer_window->view, iter);
853         view_select_only(filer_window->view, iter);
854         file_op(NULL, GPOINTER_TO_INT(action), NULL);
855
856         view_clear_selection(filer_window->view);
857         filer_window->temp_item_selected = FALSE;
858 }
859
860 /* Set the text of the 'Shift Open...' menu item.
861  * If icon is NULL, reset the text and also shade it, unless 'next'.
862  */
863 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
864 {
865         guchar          *shift_action = NULL;
866
867         if (item)
868         {
869                 if (item->flags & ITEM_FLAG_MOUNT_POINT)
870                 {
871                         if (item->flags & ITEM_FLAG_MOUNTED)
872                                 shift_action = N_("Unmount");
873                         else
874                                 shift_action = N_("Open unmounted");
875                 }
876                 else if (item->flags & ITEM_FLAG_SYMLINK)
877                         shift_action = N_("Show Target");
878                 else if (item->base_type == TYPE_DIRECTORY)
879                         shift_action = N_("Look Inside");
880                 else if (item->base_type == TYPE_FILE)
881                         shift_action = N_("Open As Text");
882         }
883         gtk_label_set_text(GTK_LABEL(menu_item),
884                         shift_action ? _(shift_action)
885                                      : _("Shift Open"));
886         gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
887 }
888
889 /* Actions */
890
891 static void view_type(gpointer data, guint action, GtkWidget *widget)
892 {
893         ViewType view_type = (ViewType) action;
894
895         g_return_if_fail(window_with_focus != NULL);
896
897         if (view_type == VIEW_TYPE_COLLECTION)
898                 display_set_layout(window_with_focus,
899                                 window_with_focus->display_style_wanted,
900                                 DETAILS_NONE, FALSE);
901
902         filer_set_view_type(window_with_focus, (ViewType) action);
903 }
904
905 static void change_size(gpointer data, guint action, GtkWidget *widget)
906 {
907         g_return_if_fail(window_with_focus != NULL);
908
909         display_change_size(window_with_focus, action == 1);
910 }
911
912 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
913 {
914         g_return_if_fail(window_with_focus != NULL);
915
916         if (updating_menu)
917                 return;
918
919         if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
920                 display_set_layout(window_with_focus,
921                                    window_with_focus->display_style,
922                                    window_with_focus->details_type, FALSE);
923         else
924                 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
925                                    window_with_focus->details_type, FALSE);
926 }
927
928 static void set_with(gpointer data, guint action, GtkWidget *widget)
929 {
930         DisplayStyle size;
931
932         g_return_if_fail(window_with_focus != NULL);
933
934         size = window_with_focus->display_style_wanted;
935
936         filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
937         display_set_layout(window_with_focus, size, action, FALSE);
938 }
939
940 static void set_sort(gpointer data, guint action, GtkWidget *widget)
941 {
942         if (updating_menu)
943                 return;
944
945         g_return_if_fail(window_with_focus != NULL);
946
947         display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
948 }
949
950 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
951 {
952         GtkSortType order;
953
954         if (updating_menu)
955                 return;
956         
957         g_return_if_fail(window_with_focus != NULL);
958
959         order = window_with_focus->sort_order;
960         if (order == GTK_SORT_ASCENDING)
961                 order = GTK_SORT_DESCENDING;
962         else
963                 order = GTK_SORT_ASCENDING;
964
965         display_set_sort_type(window_with_focus, window_with_focus->sort_type,
966                               order);
967 }
968
969 static void hidden(gpointer data, guint action, GtkWidget *widget)
970 {
971         if (updating_menu)
972                 return;
973
974         g_return_if_fail(window_with_focus != NULL);
975
976         display_set_hidden(window_with_focus,
977                            !window_with_focus->show_hidden);
978 }
979
980 static void filter_directories(gpointer data, guint action, GtkWidget *widget)
981 {
982         if (updating_menu)
983                 return;
984         
985         g_return_if_fail(window_with_focus != NULL);
986
987         display_set_filter_directories(window_with_focus,
988                            !window_with_focus->filter_directories);
989 }
990
991 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
992 {
993         if (updating_menu)
994                 return;
995
996         g_return_if_fail(window_with_focus != NULL);
997
998         display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
999 }
1000
1001 static void refresh(gpointer data, guint action, GtkWidget *widget)
1002 {
1003         g_return_if_fail(window_with_focus != NULL);
1004
1005         filer_refresh(window_with_focus);
1006 }
1007
1008 static void save_settings(gpointer data, guint action, GtkWidget *widget)
1009 {
1010         g_return_if_fail(window_with_focus != NULL);
1011
1012         filer_save_settings(window_with_focus);
1013 }
1014
1015 static void delete(FilerWindow *filer_window)
1016 {
1017         GList *paths;
1018         paths = filer_selected_items(filer_window);
1019         action_delete(paths);
1020         destroy_glist(&paths);
1021 }
1022
1023 static void usage(FilerWindow *filer_window)
1024 {
1025         GList *paths;
1026         paths = filer_selected_items(filer_window);
1027         action_usage(paths);
1028         destroy_glist(&paths);
1029 }
1030
1031 static void chmod_items(FilerWindow *filer_window)
1032 {
1033         GList *paths;
1034         paths = filer_selected_items(filer_window);
1035         action_chmod(paths, FALSE, NULL);
1036         destroy_glist(&paths);
1037 }
1038
1039 static void set_type_items(FilerWindow *filer_window)
1040 {
1041         GList *paths, *p;
1042         int npass=0, nfail=0;
1043         
1044         paths = filer_selected_items(filer_window);
1045         for(p=paths; p; p=g_list_next(p)) {
1046                 if(xattr_supported((const char *) p->data))
1047                         npass++;
1048                 else
1049                         nfail++;
1050         }
1051         if(npass==0) 
1052                 report_error(_("Extended attributes, used to store types, are not supported for this "
1053                                 "file or files.\n"
1054                                 "This may be due to lack of support from the filesystem or the C library, "
1055                                 "or it may simply be that the filesystem needs to be mounted with "
1056                                 "the right mount option ('user_xattr' on Linux)."));
1057         else if(nfail>0)
1058                 report_error(_("Setting type not supported for some of these files"));
1059         if(npass>0)
1060                 action_settype(paths, FALSE, NULL);
1061         destroy_glist(&paths);
1062 }
1063
1064 static void find(FilerWindow *filer_window)
1065 {
1066         GList *paths;
1067         paths = filer_selected_items(filer_window);
1068         action_find(paths);
1069         destroy_glist(&paths);
1070 }
1071
1072 static gboolean last_symlink_check_relative = TRUE;
1073
1074 /* This creates a new savebox widget, and allows the user to pick a new path
1075  * for the file.
1076  * Once the new path has been picked, the callback will be called with
1077  * both the current and new paths.
1078  * NOTE: This function unrefs 'image'!
1079  */
1080 static void savebox_show(const gchar *action, const gchar *path,
1081                          MaskedPixmap *image, SaveCb callback,
1082                          GdkDragAction dnd_action)
1083 {
1084         GtkWidget       *savebox = NULL;        
1085         GtkWidget       *check_relative = NULL; 
1086
1087         g_return_if_fail(image != NULL);
1088         
1089         savebox = gtk_savebox_new(action);
1090         gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1091
1092         if (callback == link_cb)
1093         {
1094                 check_relative = gtk_check_button_new_with_mnemonic(
1095                                                         _("_Relative link"));
1096                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1097                                              last_symlink_check_relative);
1098
1099                 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1100                 gtk_tooltips_set_tip(tooltips, check_relative,
1101                         _("If on, the symlink will store the path from the "
1102                         "symlink to the target file. Use this if the symlink "
1103                         "and the target will be moved together.\n"
1104                         "If off, the path from the root directory is stored - "
1105                         "use this if the symlink may move but the target will "
1106                         "stay put."), NULL);
1107                 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1108                                 check_relative, FALSE, TRUE, 0);
1109                 gtk_widget_show(check_relative);
1110         }
1111
1112         g_signal_connect(savebox, "save_to_file",
1113                                 G_CALLBACK(save_to_file), NULL);
1114
1115         g_object_set_data_full(G_OBJECT(savebox), "current_path",
1116                                 g_strdup(path), g_free);
1117         g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1118         g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1119
1120         gtk_window_set_title(GTK_WINDOW(savebox), action);
1121
1122         if (g_utf8_validate(path, -1, NULL))
1123                 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1124         else
1125         {
1126                 gchar *u8, *dir;
1127                 dir = g_path_get_dirname(path);
1128                 u8 = to_utf8(g_basename(path));
1129                 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1130                                 make_path(dir, u8));
1131                 g_free(u8);
1132                 g_free(dir);
1133         }
1134         gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1135         g_object_unref(image);
1136                                 
1137         gtk_widget_show(savebox);
1138 }
1139
1140 static gint save_to_file(GObject *savebox,
1141                          const gchar *pathname, gpointer data)
1142 {
1143         SaveCb          callback;
1144         const gchar     *current_path;
1145         
1146         callback = g_object_get_data(savebox, "action_callback");
1147         current_path = g_object_get_data(savebox, "current_path");
1148
1149         g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1150         g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1151
1152         return callback(savebox, current_path, pathname)
1153                         ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1154 }
1155
1156 static gboolean copy_cb(GObject *savebox,
1157                         const gchar *current, const gchar *new)
1158 {
1159         return action_with_leaf(action_copy, current, new);
1160 }
1161
1162 static gboolean action_with_leaf(ActionFn action,
1163                                  const gchar *current, const gchar *new)
1164 {
1165         const char      *leaf;
1166         char            *new_dir;
1167         GList           *local_paths;
1168
1169         if (new[0] != '/')
1170         {
1171                 report_error(_("New pathname is not absolute"));
1172                 return FALSE;
1173         }
1174
1175         if (new[strlen(new) - 1] == '/')
1176         {
1177                 new_dir = g_strdup(new);
1178                 leaf = NULL;
1179         }
1180         else
1181         {
1182                 const gchar *slash;
1183                 
1184                 slash = strrchr(new, '/');
1185                 new_dir = g_strndup(new, slash - new);
1186                 leaf = slash + 1;
1187         }
1188
1189         local_paths = g_list_append(NULL, (gchar *) current);
1190         action(local_paths, new_dir, leaf, -1);
1191         g_list_free(local_paths);
1192
1193         g_free(new_dir);
1194
1195         return TRUE;
1196 }
1197
1198 /* Open a savebox to act on the selected file.
1199  * Call 'callback' later to perform the operation.
1200  */
1201 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1202                          const gchar *action, SaveCb callback,
1203                          GdkDragAction dnd_action)
1204 {
1205         g_object_ref(image);
1206         savebox_show(action, path, image, callback, dnd_action);
1207 }
1208
1209 static gboolean rename_cb(GObject *savebox,
1210                           const gchar *current, const gchar *new)
1211 {
1212         return action_with_leaf(action_move, current, new);
1213 }
1214
1215 static gboolean link_cb(GObject *savebox,
1216                         const gchar *initial, const gchar *path)
1217 {
1218         GtkToggleButton *check_relative;
1219         struct stat info;
1220         int     err;
1221         gchar   *link_path;
1222
1223         check_relative = g_object_get_data(savebox, "check_relative");
1224
1225         last_symlink_check_relative = gtk_toggle_button_get_active(check_relative);
1226
1227         if (last_symlink_check_relative)
1228                 link_path = get_relative_path(path, initial);
1229         else
1230                 link_path = g_strdup(initial);
1231
1232         if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1233         {
1234                 GtkWidget *box, *button;
1235                 gint ans;
1236
1237                 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1238                                 GTK_BUTTONS_CANCEL,
1239                                 _("Symlink from '%s' already exists. "
1240                                 "Replace it with a link to '%s'?"),
1241                                 path, link_path);
1242
1243                 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1244                 
1245                 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1246                 gtk_widget_show(button);
1247                 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1248                 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1249                                              button, GTK_RESPONSE_OK);
1250                 gtk_dialog_set_default_response(GTK_DIALOG(box),
1251                                                 GTK_RESPONSE_OK);
1252
1253                 ans = gtk_dialog_run(GTK_DIALOG(box));
1254                 gtk_widget_destroy(box);
1255                 
1256                 if (ans != GTK_RESPONSE_OK)
1257                 {
1258                         g_free(link_path);
1259                         return FALSE;
1260                 }
1261
1262                 unlink(path);
1263         }
1264
1265         err = symlink(link_path, path);
1266         g_free(link_path);
1267                         
1268         if (err)
1269         {
1270                 report_error("symlink: %s", g_strerror(errno));
1271                 return FALSE;
1272         }
1273
1274         dir_check_this(path);
1275
1276         return TRUE;
1277 }
1278
1279 static void run_action(DirItem *item)
1280 {
1281         if (can_set_run_action(item))
1282                 type_set_handler_dialog(item->mime_type);
1283         else
1284                 report_error(
1285                         _("You can only set the run action for a "
1286                         "regular file"));
1287 }
1288
1289 void open_home(gpointer data, guint action, GtkWidget *widget)
1290 {
1291         filer_opendir(home_dir, NULL, NULL);
1292 }
1293
1294 static void select_all(gpointer data, guint action, GtkWidget *widget)
1295 {
1296         g_return_if_fail(window_with_focus != NULL);
1297
1298         window_with_focus->temp_item_selected = FALSE;
1299         view_select_all(window_with_focus->view);
1300 }
1301
1302 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1303 {
1304         g_return_if_fail(window_with_focus != NULL);
1305
1306         window_with_focus->temp_item_selected = FALSE;
1307         view_clear_selection(window_with_focus->view);
1308 }
1309
1310 static gboolean invert_cb(ViewIter *iter, gpointer data)
1311 {
1312         return !view_get_selected((ViewIface *) data, iter);
1313 }
1314
1315 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1316 {
1317         g_return_if_fail(window_with_focus != NULL);
1318
1319         window_with_focus->temp_item_selected = FALSE;
1320
1321         view_select_if(window_with_focus->view, invert_cb,
1322                        window_with_focus->view);
1323 }
1324
1325 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1326 {
1327         GtkWidget *win;
1328
1329         win = options_show();
1330
1331         if (win)
1332         {
1333                 number_of_windows++;
1334                 g_signal_connect(win, "destroy",
1335                                 G_CALLBACK(one_less_window), NULL);
1336         }
1337 }
1338
1339 static gboolean new_directory_cb(GObject *savebox,
1340                                  const gchar *initial, const gchar *path)
1341 {
1342         if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1343         {
1344                 report_error("mkdir: %s", g_strerror(errno));
1345                 return FALSE;
1346         }
1347
1348         dir_check_this(path);
1349
1350         if (filer_exists(window_with_focus))
1351         {
1352                 guchar  *leaf;
1353                 leaf = strrchr(path, '/');
1354                 if (leaf)
1355                         display_set_autoselect(window_with_focus, leaf + 1);
1356         }
1357         
1358         return TRUE;
1359 }
1360
1361 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1362 {
1363         g_return_if_fail(window_with_focus != NULL);
1364
1365         savebox_show(_("Create"),
1366                 make_path(window_with_focus->sym_path, _("NewDir")),
1367                 type_to_icon(inode_directory), new_directory_cb,
1368                 GDK_ACTION_COPY);
1369 }
1370
1371 static gboolean new_file_cb(GObject *savebox,
1372                             const gchar *initial, const gchar *path)
1373 {
1374         int fd;
1375
1376         fd = open(path, O_CREAT | O_EXCL, 0666);
1377
1378         if (fd == -1)
1379         {
1380                 report_error(_("Error creating '%s': %s"),
1381                                 path, g_strerror(errno));
1382                 return FALSE;
1383         }
1384
1385         if (close(fd))
1386                 report_error(_("Error creating '%s': %s"),
1387                                 path, g_strerror(errno));
1388
1389         dir_check_this(path);
1390
1391         if (filer_exists(window_with_focus))
1392         {
1393                 guchar  *leaf;
1394                 leaf = strrchr(path, '/');
1395                 if (leaf)
1396                         display_set_autoselect(window_with_focus, leaf + 1);
1397         }
1398
1399         return TRUE;
1400 }
1401
1402 static void new_file(gpointer data, guint action, GtkWidget *widget)
1403 {
1404         g_return_if_fail(window_with_focus != NULL);
1405         
1406         savebox_show(_("Create"),
1407                 make_path(window_with_focus->sym_path, _("NewFile")),
1408                 type_to_icon(text_plain),
1409                 new_file_cb, GDK_ACTION_COPY);
1410 }
1411
1412 static gboolean new_file_type_cb(GObject *savebox,
1413                                  const gchar *initial, const gchar *path)
1414 {
1415         const gchar *oleaf, *leaf;
1416         gchar *templ, *rtempl, *templ_dname, *dest;
1417         GList *paths;
1418
1419         /* We can work out the template path from the initial name */
1420         oleaf = g_basename(initial);
1421         templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1422         if (!templ_dname)
1423         {
1424                 report_error(
1425                 _("Error creating file: could not find the template for %s"),
1426                                 oleaf);
1427                 return FALSE;
1428         }
1429
1430         templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1431         g_free(templ_dname);
1432         rtempl = pathdup(templ);
1433         g_free(templ);
1434
1435         dest = g_path_get_dirname(path);
1436         leaf = g_basename(path);
1437         paths = g_list_append(NULL, rtempl);
1438
1439         action_copy(paths, dest, leaf, TRUE);
1440
1441         g_list_free(paths);
1442         g_free(dest);
1443         g_free(rtempl);
1444
1445         if (filer_exists(window_with_focus))
1446                 display_set_autoselect(window_with_focus, leaf);
1447
1448         return TRUE;
1449 }
1450
1451 static void do_send_to(gchar *templ)
1452 {
1453         g_return_if_fail(send_to_paths != NULL);
1454
1455         run_with_files(templ, send_to_paths);
1456 }
1457
1458 static void new_file_type(gchar *templ)
1459 {
1460         const gchar *leaf;
1461         MIME_type *type;
1462
1463         g_return_if_fail(window_with_focus != NULL);
1464         
1465         leaf = g_basename(templ);
1466         type = type_get_type(templ);
1467
1468         savebox_show(_("Create"),
1469                 make_path(window_with_focus->sym_path, leaf),
1470                 type_to_icon(type),
1471                 new_file_type_cb, GDK_ACTION_COPY);
1472 }
1473
1474 static void customise_send_to(gpointer data)
1475 {
1476         GPtrArray       *path;
1477         guchar          *save;
1478         GString         *dirs;
1479         int             i;
1480
1481         dirs = g_string_new(NULL);
1482
1483         path = choices_list_xdg_dirs("", SITE);
1484         for (i = 0; i < path->len; i++)
1485         {
1486                 guchar *old = (guchar *) path->pdata[i];
1487
1488                 g_string_append(dirs, old);
1489                 g_string_append(dirs, "/SendTo\n");
1490         }
1491         choices_free_list(path);
1492
1493         save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1494         if (save)
1495                 mkdir(save, 0777);
1496
1497         info_message(
1498                 _("The `Send To' menu provides a quick way to send some files "
1499                 "to an application. The applications listed are those in "
1500                 "the following directories:\n\n%s\n%s\n"
1501                 "The `Send To' menu may be opened by Shift+Menu clicking "
1502                 "over a file.\n\n"
1503                 "Advanced use:\n"
1504                 "You can also create subdirectories called "
1505                 "`.text_html', `.text', etc which will only be "
1506                 "shown for files of that type. `.group' is shown "
1507                 "only when multiple files are selected."),
1508                 dirs->str,
1509                 save ? _("I'll show you your SendTo directory now; you should "
1510                         "symlink (Ctrl+Shift drag) any applications you want "
1511                         "into it.")
1512                      : _("Your CHOICESPATH variable setting prevents "
1513                          "customisations - sorry."));
1514
1515         g_string_free(dirs, TRUE);
1516         
1517         if (save)
1518                 filer_opendir(save, NULL, NULL);
1519 }
1520
1521 static void customise_new(gpointer data)
1522 {
1523         GPtrArray       *path;
1524         guchar          *save;
1525         GString         *dirs;
1526         int             i;
1527
1528         dirs = g_string_new(NULL);
1529
1530         path = choices_list_xdg_dirs("", SITE);
1531         for (i = 0; i < path->len; i++)
1532         {
1533                 guchar *old = (guchar *) path->pdata[i];
1534
1535                 g_string_append(dirs, old);
1536                 g_string_append(dirs, "/Templates\n");
1537         }
1538         choices_free_list(path);
1539
1540         save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1541         if (save)
1542                 mkdir(save, 0777);
1543
1544         info_message(
1545                 _("Any files placed in your Templates directories will "
1546                 "appear on the `New' menu. Choosing one of them will make "
1547                 "a copy of it as the new file.\n\n"
1548                 "The following directories contain templates:\n\n%s\n%s\n"),
1549                 dirs->str,
1550                 save ? _("I'll show you your Templates directory now; you "
1551                          "should place any template files you want inside it.")
1552                      : _("Your CHOICESPATH variable setting prevents "
1553                          "customisations - sorry."));
1554
1555         g_string_free(dirs, TRUE);
1556         
1557         if (save)
1558                 filer_opendir(save, NULL, NULL);
1559 }
1560
1561 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]] 
1562  * to the menu.
1563  */
1564 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1565 {
1566         gchar *searchdir;
1567         GPtrArray *paths;
1568         int i;
1569
1570         if (subtype)
1571                 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1572         else if (type)
1573                 searchdir = g_strdup_printf("SendTo/.%s", type);
1574         else
1575                 searchdir = g_strdup("SendTo");
1576
1577         paths = choices_list_xdg_dirs(searchdir, SITE);
1578         g_free(searchdir);      
1579
1580         for (i = 0; i < paths->len; i++)
1581         {
1582                 GList   *widgets = NULL;
1583                 guchar  *dir = (guchar *) paths->pdata[i];
1584
1585                 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1586                                 (CallbackFn) do_send_to,
1587                                 FALSE, FALSE, TRUE);
1588
1589                 if (widgets)
1590                         gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1591                                         gtk_menu_item_new());
1592
1593                 g_list_free(widgets);   /* TODO: Get rid of this */
1594         }
1595
1596         choices_free_list(paths);
1597 }
1598
1599 /* Scan the SendTo dir and create and show the Send To menu.
1600  * The 'paths' list and every path in it is claimed, and will be
1601  * freed later -- don't free it yourself!
1602  */
1603 static void show_send_to_menu(GList *paths, GdkEvent *event)
1604 {
1605         GtkWidget       *menu, *item;
1606
1607         menu = gtk_menu_new();
1608         
1609         if (g_list_length(paths) == 1)
1610         {
1611                 DirItem *item;
1612                 
1613                 item = diritem_new("");
1614                 diritem_restat(paths->data, item, NULL);
1615
1616                 add_sendto(menu,
1617                            item->mime_type->media_type,
1618                            item->mime_type->subtype);
1619
1620                 add_sendto(menu, item->mime_type->media_type, NULL);
1621                 
1622                 diritem_free(item);
1623         }
1624         else
1625         {
1626                 GList *rover;
1627                 gboolean same=TRUE, same_media=TRUE;
1628                 MIME_type *type=NULL;
1629                 DirItem *item;
1630                 
1631                 item = diritem_new("");
1632                 for(rover=paths; rover; rover=g_list_next(rover))
1633                 {
1634                         diritem_restat(rover->data, item, NULL);
1635                         if(!type)
1636                                 type=item->mime_type;
1637                         else
1638                         {
1639                                 if(type!=item->mime_type)
1640                                 {
1641                                         same=FALSE;
1642                                         if(strcmp(type->media_type,
1643                                                   item->mime_type->media_type)!=0)
1644                                         {
1645                                                 same_media=FALSE;
1646                                                 break;
1647                                         }
1648                                 }
1649                         }
1650                 }
1651                 diritem_free(item);
1652
1653                 if(type)
1654                 {
1655                         if(same)
1656                                 add_sendto(menu, type->media_type,
1657                                            type->subtype);
1658                         if(same_media)
1659                                 add_sendto(menu, type->media_type, NULL);
1660                 }
1661                 
1662                 add_sendto(menu, "group", NULL);
1663         }
1664         
1665         add_sendto(menu, NULL, NULL);
1666
1667         item = gtk_menu_item_new_with_label(_("Customise"));
1668         g_signal_connect_swapped(item, "activate",
1669                                 G_CALLBACK(customise_send_to), NULL);
1670         gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1671
1672         if (send_to_paths)
1673                 destroy_glist(&send_to_paths);
1674
1675         send_to_paths = paths;
1676
1677         g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1678
1679         popup_menu = menu;
1680         show_popup_menu(menu, event, 0);
1681 }
1682
1683 static void send_to(FilerWindow *filer_window)
1684 {
1685         GList           *paths;
1686         GdkEvent        *event;
1687
1688         paths = filer_selected_items(filer_window);
1689         event = gtk_get_current_event();
1690
1691         /* Eats paths */
1692         show_send_to_menu(paths, event);
1693
1694         if (event)
1695                 gdk_event_free(event);
1696 }
1697
1698 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1699 {
1700         const char *argv[] = {"sh", "-c", NULL, NULL};
1701         gboolean close = action;
1702
1703         argv[2] = o_menu_xterm.value;
1704
1705         g_return_if_fail(window_with_focus != NULL);
1706
1707         if (rox_spawn(window_with_focus->sym_path, argv) && close)
1708                 gtk_widget_destroy(window_with_focus->window);
1709 }
1710
1711 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1712 {
1713         g_return_if_fail(window_with_focus != NULL);
1714
1715         filer_change_to(window_with_focus, home_dir, NULL);
1716 }
1717
1718 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1719 {
1720         g_return_if_fail(window_with_focus != NULL);
1721
1722         bookmarks_show_menu(window_with_focus);
1723 }
1724
1725 static void show_log(gpointer data, guint action, GtkWidget *widget)
1726 {
1727         g_return_if_fail(window_with_focus != NULL);
1728
1729         log_show_window();
1730 }
1731
1732 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1733 {
1734         g_return_if_fail(window_with_focus != NULL);
1735
1736         if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1737                 filer_change_to(window_with_focus,
1738                                 window_with_focus->real_path, NULL);
1739         else
1740                 delayed_error(_("This is already the canonical name "
1741                                 "for this directory."));
1742 }
1743
1744 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1745 {
1746         g_return_if_fail(window_with_focus != NULL);
1747
1748         filer_open_parent(window_with_focus);
1749 }
1750
1751 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1752 {
1753         g_return_if_fail(window_with_focus != NULL);
1754
1755         change_to_parent(window_with_focus);
1756 }
1757
1758 static void resize(gpointer data, guint action, GtkWidget *widget)
1759 {
1760         g_return_if_fail(window_with_focus != NULL);
1761
1762         view_autosize(window_with_focus->view);
1763 }
1764
1765 static void new_window(gpointer data, guint action, GtkWidget *widget)
1766 {
1767         g_return_if_fail(window_with_focus != NULL);
1768
1769         if (o_unique_filer_windows.int_value)
1770         {
1771                 report_error(_("You can't open a second view onto "
1772                         "this directory because the `Unique Windows' option "
1773                         "is turned on in the Options window."));
1774         }
1775         else
1776                 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1777 }
1778
1779 static void close_window(gpointer data, guint action, GtkWidget *widget)
1780 {
1781         g_return_if_fail(window_with_focus != NULL);
1782
1783         if (!filer_window_delete(window_with_focus->window, NULL,
1784                                  window_with_focus))
1785                 gtk_widget_destroy(window_with_focus->window);
1786 }
1787
1788 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1789 {
1790         MiniType type = (MiniType) action;
1791
1792         g_return_if_fail(window_with_focus != NULL);
1793
1794         /* Item needs to remain selected... */
1795         if (type == MINI_SHELL)
1796                 window_with_focus->temp_item_selected = FALSE;
1797
1798         minibuffer_show(window_with_focus, type);
1799 }
1800
1801 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1802 {
1803         if (action == HELP_ABOUT)
1804                 infobox_new(app_dir);
1805         else if (action == HELP_DIR)
1806                 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1807         else if (action == HELP_MANUAL)
1808         {
1809                 gchar *manual = NULL;
1810
1811                 if (current_lang)
1812                 {
1813                         manual = g_strconcat(app_dir, "/Help/Manual-",
1814                                              current_lang, ".html", NULL);
1815                         if (!file_exists(manual) && strchr(current_lang, '_'))
1816                         {
1817                                 /* Try again without the territory */
1818                                 strcpy(strrchr(manual, '_'), ".html");
1819                         }
1820                         if (!file_exists(manual))
1821                                 null_g_free(&manual);
1822                 }
1823
1824                 if (!manual)
1825                         manual = g_strconcat(app_dir,
1826                                                 "/Help/Manual.html", NULL);
1827                 
1828                 run_by_path(manual);
1829
1830                 g_free(manual);
1831         }
1832         else
1833                 g_warning("Unknown help action %d\n", action);
1834 }
1835
1836 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1837 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1838 {
1839         GList   *items, *item;
1840
1841         items = gtk_container_get_children(GTK_CONTAINER(menu));
1842
1843         item = g_list_nth(items, from);
1844         while (item && n--)
1845         {
1846                 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1847                 item = item->next;
1848         }
1849         g_list_free(items);
1850 }
1851
1852 static void save_menus(void)
1853 {
1854         char    *menurc;
1855
1856         menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1857         if (menurc)
1858         {
1859                 gtk_accel_map_save(menurc);
1860                 g_free(menurc);
1861         }
1862 }
1863
1864 static void select_nth_item(GtkMenuShell *shell, int n)
1865 {
1866         GList     *items;
1867         GtkWidget *item;
1868
1869         items = gtk_container_get_children(GTK_CONTAINER(shell));
1870         item = g_list_nth_data(items, n);
1871
1872         g_return_if_fail(item != NULL);
1873
1874         g_list_free(items);
1875
1876         gtk_menu_shell_select_item(shell, item);
1877 }
1878
1879 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1880 {
1881         DirItem *item;
1882         const guchar *path;
1883         int     n_selected;
1884         ViewIter iter;
1885
1886         g_return_if_fail(window_with_focus != NULL);
1887
1888         n_selected = view_count_selected(window_with_focus->view);
1889
1890         if (n_selected < 1)
1891         {
1892                 const char *prompt;
1893
1894                 switch (action)
1895                 {
1896                         case FILE_COPY_ITEM:
1897                                 prompt = _("Copy ... ?");
1898                                 break;
1899                         case FILE_RENAME_ITEM:
1900                                 prompt = _("Rename ... ?");
1901                                 break;
1902                         case FILE_LINK_ITEM:
1903                                 prompt = _("Symlink ... ?");
1904                                 break;
1905                         case FILE_OPEN_FILE:
1906                                 prompt = _("Shift Open ... ?");
1907                                 break;
1908                         case FILE_PROPERTIES:
1909                                 prompt = _("Properties of ... ?");
1910                                 break;
1911                         case FILE_SET_TYPE:
1912                                 prompt = _("Set type of ... ?");
1913                                 break;
1914                         case FILE_RUN_ACTION:
1915                                 prompt = _("Set run action for ... ?");
1916                                 break;
1917                         case FILE_SET_ICON:
1918                                 prompt = _("Set icon for ... ?");
1919                                 break;
1920                         case FILE_SEND_TO:
1921                                 prompt = _("Send ... to ... ?");
1922                                 break;
1923                         case FILE_DELETE:
1924                                 prompt = _("DELETE ... ?");
1925                                 break;
1926                         case FILE_USAGE:
1927                                 prompt = _("Count the size of ... ?");
1928                                 break;
1929                         case FILE_CHMOD_ITEMS:
1930                                 prompt = _("Set permissions on ... ?");
1931                                 break;
1932                         case FILE_FIND:
1933                                 prompt = _("Search inside ... ?");
1934                                 break;
1935                         default:
1936                                 g_warning("Unknown action!");
1937                                 return;
1938                 }
1939                 filer_target_mode(window_with_focus, target_callback,
1940                                         GINT_TO_POINTER(action), prompt);
1941                 return;
1942         }
1943
1944         switch (action)
1945         {
1946                 case FILE_SEND_TO:
1947                         send_to(window_with_focus);
1948                         return;
1949                 case FILE_DELETE:
1950                         delete(window_with_focus);
1951                         return;
1952                 case FILE_USAGE:
1953                         usage(window_with_focus);
1954                         return;
1955                 case FILE_CHMOD_ITEMS:
1956                         chmod_items(window_with_focus);
1957                         return;
1958                 case FILE_SET_TYPE:
1959                         set_type_items(window_with_focus);
1960                         return;
1961                 case FILE_FIND:
1962                         find(window_with_focus);
1963                         return;
1964                 case FILE_PROPERTIES:
1965                 {
1966                         GList *items;
1967
1968                         items = filer_selected_items(window_with_focus);
1969                         infobox_show_list(items);
1970                         destroy_glist(&items);
1971                         return;
1972                 }
1973                 case FILE_RENAME_ITEM:
1974                         if (n_selected > 1)
1975                         {
1976                                 GList *items = NULL;
1977                                 ViewIter iter;
1978
1979                                 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1980                                 while ((item = iter.next(&iter)))
1981                                         items = g_list_prepend(items, item->leafname);
1982                                 items = g_list_reverse(items);
1983
1984                                 bulk_rename(window_with_focus->sym_path, items);
1985                                 g_list_free(items);
1986                                 return;
1987                         }
1988                         break;  /* Not a bulk rename... see below */
1989                 default:
1990                         break;
1991         }
1992
1993         /* All the following actions require exactly one file selected */
1994
1995         if (n_selected > 1)
1996         {
1997                 report_error(_("You cannot do this to more than "
1998                                 "one item at a time"));
1999                 return;
2000         }
2001
2002         view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
2003                 
2004         item = iter.next(&iter);
2005         g_return_if_fail(item != NULL);
2006         /* iter may be passed to filer_openitem... */
2007
2008         if (item->base_type == TYPE_UNKNOWN)
2009                 item = dir_update_item(window_with_focus->directory,
2010                                         item->leafname);
2011
2012         if (!item)
2013         {
2014                 report_error(_("Item no longer exists!"));
2015                 return;
2016         }
2017
2018         path = make_path(window_with_focus->sym_path, item->leafname);
2019
2020         switch (action)
2021         {
2022                 case FILE_COPY_ITEM:
2023                         src_dest_action_item(path, di_image(item),
2024                                         _("Copy"), copy_cb,
2025                                         GDK_ACTION_COPY);
2026                         break;
2027                 case FILE_RENAME_ITEM:
2028                         src_dest_action_item(path, di_image(item),
2029                                         _("Rename"), rename_cb,
2030                                         GDK_ACTION_MOVE);
2031                         break;
2032                 case FILE_LINK_ITEM:
2033                         src_dest_action_item(path, di_image(item),
2034                                         _("Symlink"), link_cb,
2035                                         GDK_ACTION_LINK);
2036                         break;
2037                 case FILE_OPEN_FILE:
2038                         filer_openitem(window_with_focus, &iter,
2039                                 OPEN_SAME_WINDOW | OPEN_SHIFT);
2040                         break;
2041                 case FILE_RUN_ACTION:
2042                         run_action(item);
2043                         break;
2044                 case FILE_SET_ICON:
2045                         icon_set_handler_dialog(item, path);
2046                         break;
2047                 default:
2048                         g_warning("Unknown action!");
2049                         return;
2050         }
2051 }
2052
2053 static void show_key_help(GtkWidget *button, gpointer data)
2054 {
2055         gboolean can_change_accels;
2056         
2057         g_object_get(G_OBJECT(gtk_settings_get_default()),
2058                      "gtk-can-change-accels", &can_change_accels,
2059                      NULL);
2060
2061         if (!can_change_accels)
2062         {
2063                 info_message(_("User-definable shortcuts are disabled by "
2064                         "default in Gtk2, and you have not enabled "
2065                         "them. You can turn this feature on by:\n\n"
2066                         "1) using an XSettings manager, such as ROX-Session "
2067                         "or gnome-settings-daemon, or\n\n"
2068                         "2) adding this line to ~/.gtkrc-2.0:\n"
2069                         "\tgtk-can-change-accels = 1\n"
2070                         "\t(this only works if NOT using XSETTINGS)"));
2071                 return;
2072         }
2073
2074         info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2075         "- Open the menu over a filer window,\n"
2076         "- Move the pointer over the item you want to use,\n"
2077         "- Press the key you want attached to it.\n\n"
2078         "The key will appear next to the menu item and you can just press "
2079         "that key without opening the menu in future."));
2080 }
2081
2082 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2083 {
2084         GtkWidget *button, *align;
2085
2086         g_return_val_if_fail(option == NULL, NULL);
2087         
2088         align = gtk_alignment_new(0, 0.5, 0, 0);
2089         button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2090         gtk_container_add(GTK_CONTAINER(align), button);
2091         g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2092
2093         return g_list_append(NULL, align);
2094 }