Escape special characters when opening the shell minibuffer.
[rox-filer.git] / ROX-Filer / src / menu.c
blob01d87685cf1f85ca45991a8c4f792c5ca1c25628
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
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.
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.
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
20 /* menu.c - code for handling the popup menus */
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/wait.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <dirent.h>
33 #include <gtk/gtk.h>
35 #include "global.h"
37 #include "menu.h"
38 #include "run.h"
39 #include "action.h"
40 #include "filer.h"
41 #include "pixmaps.h"
42 #include "type.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "options.h"
46 #include "choices.h"
47 #include "gtksavebox.h"
48 #include "mount.h"
49 #include "minibuffer.h"
50 #include "i18n.h"
51 #include "main.h"
52 #include "pinboard.h"
53 #include "dir.h"
54 #include "diritem.h"
55 #include "appmenu.h"
56 #include "usericons.h"
57 #include "infobox.h"
58 #include "view_iface.h"
59 #include "display.h"
60 #include "bookmarks.h"
61 #include "panel.h"
62 #include "bulk_rename.h"
63 #include "xtypes.h"
64 #include "log.h"
66 typedef enum {
67 FILE_COPY_ITEM,
68 FILE_RENAME_ITEM,
69 FILE_LINK_ITEM,
70 FILE_OPEN_FILE,
71 FILE_PROPERTIES,
72 FILE_RUN_ACTION,
73 FILE_SET_ICON,
74 FILE_SEND_TO,
75 FILE_DELETE,
76 FILE_USAGE,
77 FILE_CHMOD_ITEMS,
78 FILE_FIND,
79 FILE_SET_TYPE,
80 } FileOp;
82 typedef void (*ActionFn)(GList *paths,
83 const char *dest_dir, const char *leaf, int quiet);
84 typedef void MenuCallback(GtkWidget *widget, gpointer data);
86 typedef gboolean (*SaveCb)(GObject *savebox,
87 const gchar *current, const gchar *new);
89 GtkAccelGroup *filer_keys = NULL;
90 static gboolean filer_keys_need_init = TRUE;
92 static GtkWidget *popup_menu = NULL; /* Currently open menu */
94 static gint updating_menu = 0; /* Non-zero => ignore activations */
95 static GList *send_to_paths = NULL;
97 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
99 /* Static prototypes */
101 static void save_menus(void);
102 static void menu_closed(GtkWidget *widget);
103 static void shade_file_menu_items(gboolean shaded);
104 static void savebox_show(const gchar *action, const gchar *path,
105 MaskedPixmap *image, SaveCb callback,
106 GdkDragAction dnd_action);
107 static gint save_to_file(GObject *savebox,
108 const gchar *pathname, gpointer data);
109 static gboolean action_with_leaf(ActionFn action,
110 const gchar *current, const gchar *new);
111 static gboolean link_cb(GObject *savebox,
112 const gchar *initial, const gchar *path);
113 static void select_nth_item(GtkMenuShell *shell, int n);
114 static void new_file_type(gchar *templ);
115 static void do_send_to(gchar *templ);
116 static void show_send_to_menu(GList *paths, GdkEvent *event);
117 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
119 /* Note that for most of these callbacks none of the arguments are used. */
121 static void view_type(gpointer data, guint action, GtkWidget *widget);
123 /* (action used in these three - DetailsType) */
124 static void change_size(gpointer data, guint action, GtkWidget *widget);
125 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
126 static void set_with(gpointer data, guint action, GtkWidget *widget);
128 static void set_sort(gpointer data, guint action, GtkWidget *widget);
129 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
131 static void filter_directories(gpointer data, guint action, GtkWidget *widget);
132 static void hidden(gpointer data, guint action, GtkWidget *widget);
133 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
134 static void refresh(gpointer data, guint action, GtkWidget *widget);
135 static void save_settings(gpointer data, guint action, GtkWidget *widget);
137 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
139 static void select_all(gpointer data, guint action, GtkWidget *widget);
140 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
141 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
142 static void new_directory(gpointer data, guint action, GtkWidget *widget);
143 static void new_file(gpointer data, guint action, GtkWidget *widget);
144 static void customise_new(gpointer data);
145 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent(gpointer data, guint action, GtkWidget *widget);
149 static void home_directory(gpointer data, guint action, GtkWidget *widget);
150 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
151 static void show_log(gpointer data, guint action, GtkWidget *widget);
152 static void new_window(gpointer data, guint action, GtkWidget *widget);
153 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
154 static void close_window(gpointer data, guint action, GtkWidget *widget);
155 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
157 /* (action used in this - MiniType) */
158 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
159 static void resize(gpointer data, guint action, GtkWidget *widget);
161 #define MENUS_NAME "menus2"
163 static GtkWidget *filer_menu; /* The popup filer menu */
164 static GtkWidget *filer_file_item; /* The File '' label */
165 static GtkWidget *filer_file_menu; /* The File '' menu */
166 static GtkWidget *file_shift_item; /* Shift Open label */
167 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
168 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
169 static GtkWidget *filer_filter_dirs_menu;/* The Filter Dirs item */
170 static GtkWidget *filer_reverse_menu; /* The Reversed item */
171 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
172 static GtkWidget *filer_new_window; /* The New Window item */
173 static GtkWidget *filer_new_menu; /* The New submenu */
174 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
175 static GtkWidget *filer_set_type; /* Set type item */
177 #undef N_
178 #define N_(x) x
180 static GtkItemFactoryEntry filer_menu_def[] = {
181 {N_("Display"), NULL, NULL, 0, "<Branch>"},
182 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
183 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
184 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
185 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
186 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
187 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
188 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
189 {">", NULL, NULL, 0, "<Separator>"},
190 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
191 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
192 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
193 {">", NULL, NULL, 0, "<Separator>"},
194 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
195 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
196 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
197 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
198 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
199 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
200 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
201 {">", NULL, NULL, 0, "<Separator>"},
202 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
203 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
204 {">" N_("Filter Directories With Files"), NULL, filter_directories, 0, "<ToggleItem>"},
205 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
206 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
207 {">" N_("Save Current Display Settings..."), NULL, save_settings, 0, NULL},
208 {N_("File"), NULL, NULL, 0, "<Branch>"},
209 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
210 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
211 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
212 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
213 {">", NULL, NULL, 0, "<Separator>"},
214 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
215 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
216 {">", NULL, NULL, 0, "<Separator>"},
217 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
218 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
219 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
220 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
221 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
222 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
223 {">", NULL, NULL, 0, "<Separator>"},
224 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
225 {N_("Select"), NULL, NULL, 0, "<Branch>"},
226 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
227 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
228 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
229 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
230 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
231 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
232 {N_("New"), NULL, NULL, 0, "<Branch>"},
233 {">" N_("Directory"), NULL, new_directory, 0, NULL},
234 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
235 {">" N_("Customise Menu..."), NULL, customise_new, 0, NULL},
236 {N_("Window"), NULL, NULL, 0, "<Branch>"},
237 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
238 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
239 {">" N_("New Window"), NULL, new_window, 0, NULL},
240 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
241 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
242 {">" N_("Show Log"), NULL, show_log, 0, "<StockItem>", GTK_STOCK_INFO},
243 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
244 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
245 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
247 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
248 {">", NULL, NULL, 0, "<Separator>"},
249 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
250 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
251 {">" N_("Terminal Here"), "grave", xterm_here, FALSE, NULL},
252 {">" N_("Switch to Terminal"), NULL, xterm_here, TRUE, NULL},
253 {N_("Help"), NULL, NULL, 0, "<Branch>"},
254 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
255 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
256 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
260 #define GET_MENU_ITEM(var, menu) \
261 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
263 #define GET_SMENU_ITEM(var, menu, sub) \
264 do { \
265 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
266 var = gtk_item_factory_get_widget(item_factory, tmp); \
267 g_free(tmp); \
268 } while (0)
270 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
271 do { \
272 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
273 var = gtk_item_factory_get_widget(item_factory, tmp); \
274 g_free(tmp); \
275 } while (0)
277 /* Returns TRUE if the keys were installed (first call only) */
278 gboolean ensure_filer_menu(void)
280 GList *items;
281 guchar *tmp;
282 GtkWidget *item;
283 GtkItemFactory *item_factory;
285 if (!filer_keys_need_init)
286 return FALSE;
287 filer_keys_need_init = FALSE;
289 item_factory = menu_create(filer_menu_def,
290 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
291 "<filer>", filer_keys);
293 GET_MENU_ITEM(filer_menu, "filer");
294 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
295 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
296 GET_SSMENU_ITEM(filer_filter_dirs_menu, "filer", "Display", "Filter Directories With Files");
297 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
298 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
299 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
300 "Show Thumbnails");
301 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
302 filer_set_type = GTK_BIN(item)->child;
304 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
305 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
306 filer_follow_sym = GTK_BIN(item)->child;
308 /* File '' label... */
309 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
310 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
311 g_list_free(items);
313 /* Shift Open... label */
314 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
315 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
316 g_list_free(items);
318 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
319 filer_new_window = GTK_BIN(item)->child;
321 g_signal_connect(filer_menu, "unmap_event",
322 G_CALLBACK(menu_closed), NULL);
323 g_signal_connect(filer_file_menu, "unmap_event",
324 G_CALLBACK(menu_closed), NULL);
326 g_signal_connect(filer_keys, "accel_changed",
327 G_CALLBACK(save_menus), NULL);
329 return TRUE;
332 void menu_init(void)
334 char *menurc;
336 menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
337 if (menurc)
339 gtk_accel_map_load(menurc);
340 g_free(menurc);
343 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
344 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
345 option_add_int(&o_menu_quick, "menu_quick", FALSE);
346 option_add_saver(save_menus);
348 option_register_widget("menu-set-keys", set_keys_button);
350 filer_keys = gtk_accel_group_new();
353 /* Name is in the form "<panel>" */
354 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
355 const gchar *name, GtkAccelGroup *keys)
357 GtkItemFactory *item_factory;
358 GtkItemFactoryEntry *translated;
360 if (!keys)
362 keys = gtk_accel_group_new();
363 gtk_accel_group_lock(keys);
366 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
368 translated = translate_entries(def, n_entries);
369 gtk_item_factory_create_items(item_factory, n_entries,
370 translated, NULL);
371 free_translated_entries(translated, n_entries);
373 return item_factory;
376 /* Prevent the user from setting a short-cut on this item */
377 static void menuitem_no_shortcuts(GtkWidget *item)
379 /* XXX */
380 #if 0
381 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
383 _gtk_widget_set_accel_path(item, NULL, NULL);
384 null_g_free(&menuitem->accel_path);
385 #endif
388 /* Shade items that only work on single files */
389 static void shade_file_menu_items(gboolean shaded)
391 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
392 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
393 menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
394 menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
397 /* 'data' is an array of three ints:
398 * [ pointer_x, pointer_y, item_under_pointer ]
400 void position_menu(GtkMenu *menu, gint *x, gint *y,
401 gboolean *push_in, gpointer data)
403 int *pos = (int *) data;
404 GtkRequisition requisition;
405 GList *items, *next;
406 int y_shift = 0;
407 int item = pos[2];
409 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
411 while (item >= 0 && next)
413 int h = ((GtkWidget *) next->data)->requisition.height;
415 if (item > 0)
416 y_shift += h;
417 else
418 y_shift += h / 2;
420 next = next->next;
421 item--;
424 g_list_free(items);
426 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
428 *x = pos[0] - (requisition.width * 7 / 8);
429 *y = pos[1] - y_shift;
431 *x = CLAMP(*x, 0, screen_width - requisition.width);
432 *y = CLAMP(*y, 0, screen_height - requisition.height);
434 *push_in = FALSE;
437 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
438 MenuIconStyle style)
440 GtkWidget *item;
442 if (style != MIS_NONE && di_image(ditem))
444 GdkPixbuf *pixbuf;
445 MaskedPixmap *image;
447 image = di_image(ditem);
449 switch (style)
451 case MIS_LARGE:
452 pixbuf = image->pixbuf;
453 break;
454 default:
455 if (!image->sm_pixbuf)
456 pixmap_make_small(image);
457 pixbuf = image->sm_pixbuf;
458 break;
461 item = gtk_image_menu_item_new_with_label(label);
462 /* TODO: Find a way to allow short-cuts */
463 menuitem_no_shortcuts(item);
465 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
466 gtk_image_new_from_pixbuf(pixbuf));
467 gtk_widget_show_all(item);
469 else
470 item = gtk_menu_item_new_with_label(label);
472 return item;
475 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
476 MenuIconStyle style, CallbackFn func,
477 gboolean separator, gboolean strip_ext,
478 gboolean recurse)
480 GList *widgets = NULL;
481 DirItem *ditem;
482 int i;
483 GtkWidget *item;
484 char *dname = NULL;
485 GPtrArray *names;
487 dname = pathdup(dir_name);
489 names = list_dir(dname);
490 if (!names)
491 goto out;
493 for (i = 0; i < names->len; i++)
495 char *leaf = names->pdata[i];
496 gchar *fname;
498 if (separator)
500 item = gtk_menu_item_new();
501 widgets = g_list_append(widgets, item);
502 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
503 separator = FALSE;
506 fname = g_strconcat(dname, "/", leaf, NULL);
508 /* Strip off extension, if any */
509 if (strip_ext)
511 char *dot;
512 dot = strchr(leaf, '.');
513 if (dot)
514 *dot = '\0';
517 ditem = diritem_new("");
518 diritem_restat(fname, ditem, NULL);
520 item = make_send_to_item(ditem, leaf, style);
522 g_free(leaf);
524 /* If it is a directory (but NOT an AppDir) and we are
525 * recursing then set up a sub menu.
527 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
528 !(ditem->flags & ITEM_FLAG_APPDIR))
530 GtkWidget *sub;
531 GList *new_widgets;
533 sub = gtk_menu_new();
534 new_widgets = menu_from_dir(sub, fname, style, func,
535 separator, strip_ext, TRUE);
536 g_list_free(new_widgets);
537 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
539 else
540 g_signal_connect_swapped(item, "activate",
541 G_CALLBACK(func), fname);
543 diritem_free(ditem);
545 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
546 g_signal_connect_swapped(item, "destroy",
547 G_CALLBACK(g_free), fname);
549 widgets = g_list_append(widgets, item);
552 g_ptr_array_free(names, TRUE);
553 out:
554 g_free(dname);
556 return widgets;
559 /* Scan the templates dir and create entries for the New menu */
560 static void update_new_files_menu(MenuIconStyle style)
562 static GList *widgets = NULL;
564 gchar *templ_dname = NULL;
566 if (widgets)
568 GList *next;
570 for (next = widgets; next; next = next->next)
571 gtk_widget_destroy((GtkWidget *) next->data);
573 g_list_free(widgets);
574 widgets = NULL;
577 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
578 if (templ_dname)
580 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
581 (CallbackFn) new_file_type, TRUE, TRUE,
582 FALSE);
583 g_free(templ_dname);
585 gtk_widget_show_all(filer_new_menu);
588 /* 'item' is the number of the item to appear under the pointer. */
589 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
591 int pos[3];
592 int button = 0;
593 guint32 time = 0;
595 if (event && (event->type == GDK_BUTTON_PRESS ||
596 event->type == GDK_BUTTON_RELEASE))
598 GdkEventButton *bev = (GdkEventButton *) event;
600 pos[0] = bev->x_root;
601 pos[1] = bev->y_root;
602 button = bev->button;
603 time = bev->time;
605 else if (event && event->type == GDK_KEY_PRESS)
607 GdkEventKey *kev = (GdkEventKey *) event;
609 get_pointer_xy(pos, pos + 1);
610 time = kev->time;
612 else
613 get_pointer_xy(pos, pos + 1);
615 pos[2] = item;
617 gtk_widget_show_all(menu);
618 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
619 position_menu, (gpointer) pos, button, time);
620 select_nth_item(GTK_MENU_SHELL(menu), item);
623 /* Hide the popup menu, if any */
624 void menu_popdown(void)
626 if (popup_menu)
627 gtk_menu_popdown(GTK_MENU(popup_menu));
630 static MenuIconStyle get_menu_icon_style(void)
632 MenuIconStyle mis;
633 int display;
635 mis = o_menu_iconsize.int_value;
637 switch (mis)
639 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
640 return mis;
641 default:
642 break;
645 if (mis == MIS_CURRENT && window_with_focus)
647 switch (window_with_focus->display_style)
649 case HUGE_ICONS:
650 case LARGE_ICONS:
651 return MIS_LARGE;
652 case SMALL_ICONS:
653 return MIS_SMALL;
654 default:
655 break;
659 display = o_display_size.int_value;
660 switch (display)
662 case HUGE_ICONS:
663 case LARGE_ICONS:
664 return MIS_LARGE;
665 case SMALL_ICONS:
666 return MIS_SMALL;
667 default:
668 break;
671 return MIS_SMALL;
674 /* iter->peek() is the clicked item, or NULL if none */
675 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
677 DirItem *file_item = NULL;
678 GdkModifierType state = 0;
679 int n_selected;
680 int n_added = 0;
682 g_return_if_fail(event != NULL);
684 n_selected = view_count_selected(filer_window->view);
686 ensure_filer_menu();
688 updating_menu++;
690 /* Remove previous AppMenu, if any */
691 appmenu_remove();
693 window_with_focus = filer_window;
695 if (event->type == GDK_BUTTON_PRESS)
696 state = ((GdkEventButton *) event)->state;
697 else if (event->type == GDK_KEY_PRESS)
698 state = ((GdkEventKey *) event)->state;
700 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
702 filer_window->temp_item_selected = TRUE;
703 view_set_selected(filer_window->view, iter, TRUE);
704 n_selected = view_count_selected(filer_window->view);
706 else
708 filer_window->temp_item_selected = FALSE;
711 /* Short-cut to the Send To menu */
712 if (state & GDK_SHIFT_MASK)
714 GList *paths;
716 updating_menu--;
718 if (n_selected == 0)
720 report_error(
721 _("You should Shift+Menu click over a file to "
722 "send it somewhere"));
723 return;
726 paths = filer_selected_items(filer_window);
728 show_send_to_menu(paths, event); /* (paths eaten) */
730 return;
734 GtkWidget *file_label, *file_menu;
735 GString *buffer;
736 DirItem *item;
738 file_label = filer_file_item;
739 file_menu = filer_file_menu;
740 gtk_check_menu_item_set_active(
741 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
742 filer_window->show_thumbs);
743 gtk_check_menu_item_set_active(
744 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
745 filer_window->show_hidden);
746 gtk_check_menu_item_set_active(
747 GTK_CHECK_MENU_ITEM(filer_filter_dirs_menu),
748 filer_window->filter_directories);
749 gtk_check_menu_item_set_active(
750 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
751 filer_window->sort_order != GTK_SORT_ASCENDING);
752 gtk_check_menu_item_set_active(
753 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
754 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
755 buffer = g_string_new(NULL);
757 switch (n_selected)
759 case 0:
760 g_string_assign(buffer, _("Next Click"));
761 shade_file_menu_items(FALSE);
762 break;
763 case 1:
764 item = filer_selected_item(filer_window);
765 if (item->base_type == TYPE_UNKNOWN)
766 dir_update_item(filer_window->directory,
767 item->leafname);
768 shade_file_menu_items(FALSE);
769 file_item = filer_selected_item(filer_window);
770 g_string_printf(buffer, _("%s '%s'"),
771 basetype_name(file_item),
772 g_utf8_validate(file_item->leafname,
773 -1, NULL)
774 ? file_item->leafname
775 : _("(bad utf-8)"));
776 if (!can_set_run_action(file_item))
777 menu_set_items_shaded(filer_file_menu,
778 TRUE, 8, 1);
779 break;
780 default:
781 shade_file_menu_items(TRUE);
782 g_string_printf(buffer, _("%d items"),
783 n_selected);
784 break;
786 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
787 g_string_free(buffer, TRUE);
789 menu_show_shift_action(file_shift_item, file_item,
790 n_selected == 0);
791 if (file_item)
792 n_added = appmenu_add(make_path(filer_window->sym_path,
793 file_item->leafname),
794 file_item, filer_file_menu);
797 update_new_files_menu(get_menu_icon_style());
799 gtk_widget_set_sensitive(filer_new_window,
800 !o_unique_filer_windows.int_value);
801 gtk_widget_set_sensitive(filer_follow_sym,
802 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
803 gtk_widget_set_sensitive(filer_set_type,
804 xattr_supported(filer_window->real_path));
806 if (n_selected && o_menu_quick.int_value)
807 popup_menu = (state & GDK_CONTROL_MASK)
808 ? filer_menu
809 : filer_file_menu;
810 else
811 popup_menu = (state & GDK_CONTROL_MASK)
812 ? filer_file_menu
813 : filer_menu;
815 updating_menu--;
817 show_popup_menu(popup_menu, event,
818 popup_menu == filer_file_menu ? n_added : 1);
821 static void menu_closed(GtkWidget *widget)
823 if (window_with_focus == NULL || widget != popup_menu)
824 return; /* Close panel item chosen? */
826 popup_menu = NULL;
828 if (window_with_focus->temp_item_selected)
830 view_clear_selection(window_with_focus->view);
831 window_with_focus->temp_item_selected = FALSE;
834 appmenu_remove();
837 static void target_callback(FilerWindow *filer_window,
838 ViewIter *iter,
839 gpointer action)
841 g_return_if_fail(filer_window != NULL);
843 window_with_focus = filer_window;
845 /* Don't grab the primary selection */
846 filer_window->temp_item_selected = TRUE;
848 view_wink_item(filer_window->view, iter);
849 view_select_only(filer_window->view, iter);
850 file_op(NULL, GPOINTER_TO_INT(action), NULL);
852 view_clear_selection(filer_window->view);
853 filer_window->temp_item_selected = FALSE;
856 /* Set the text of the 'Shift Open...' menu item.
857 * If icon is NULL, reset the text and also shade it, unless 'next'.
859 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
861 guchar *shift_action = NULL;
863 if (item)
865 if (item->flags & ITEM_FLAG_MOUNT_POINT)
867 if (item->flags & ITEM_FLAG_MOUNTED)
868 shift_action = N_("Unmount");
869 else
870 shift_action = N_("Open unmounted");
872 else if (item->flags & ITEM_FLAG_SYMLINK)
873 shift_action = N_("Show Target");
874 else if (item->base_type == TYPE_DIRECTORY)
875 shift_action = N_("Look Inside");
876 else if (item->base_type == TYPE_FILE)
877 shift_action = N_("Open As Text");
879 gtk_label_set_text(GTK_LABEL(menu_item),
880 shift_action ? _(shift_action)
881 : _("Shift Open"));
882 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
885 /* Actions */
887 static void view_type(gpointer data, guint action, GtkWidget *widget)
889 ViewType view_type = (ViewType) action;
891 g_return_if_fail(window_with_focus != NULL);
893 if (view_type == VIEW_TYPE_COLLECTION)
894 display_set_layout(window_with_focus,
895 window_with_focus->display_style_wanted,
896 DETAILS_NONE, FALSE);
898 filer_set_view_type(window_with_focus, (ViewType) action);
901 static void change_size(gpointer data, guint action, GtkWidget *widget)
903 g_return_if_fail(window_with_focus != NULL);
905 display_change_size(window_with_focus, action == 1);
908 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
910 g_return_if_fail(window_with_focus != NULL);
912 if (updating_menu)
913 return;
915 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
916 display_set_layout(window_with_focus,
917 window_with_focus->display_style,
918 window_with_focus->details_type, FALSE);
919 else
920 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
921 window_with_focus->details_type, FALSE);
924 static void set_with(gpointer data, guint action, GtkWidget *widget)
926 DisplayStyle size;
928 g_return_if_fail(window_with_focus != NULL);
930 size = window_with_focus->display_style_wanted;
932 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
933 display_set_layout(window_with_focus, size, action, FALSE);
936 static void set_sort(gpointer data, guint action, GtkWidget *widget)
938 if (updating_menu)
939 return;
941 g_return_if_fail(window_with_focus != NULL);
943 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
946 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
948 GtkSortType order;
950 if (updating_menu)
951 return;
953 g_return_if_fail(window_with_focus != NULL);
955 order = window_with_focus->sort_order;
956 if (order == GTK_SORT_ASCENDING)
957 order = GTK_SORT_DESCENDING;
958 else
959 order = GTK_SORT_ASCENDING;
961 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
962 order);
965 static void hidden(gpointer data, guint action, GtkWidget *widget)
967 if (updating_menu)
968 return;
970 g_return_if_fail(window_with_focus != NULL);
972 display_set_hidden(window_with_focus,
973 !window_with_focus->show_hidden);
976 static void filter_directories(gpointer data, guint action, GtkWidget *widget)
978 if (updating_menu)
979 return;
981 g_return_if_fail(window_with_focus != NULL);
983 display_set_filter_directories(window_with_focus,
984 !window_with_focus->filter_directories);
987 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
989 if (updating_menu)
990 return;
992 g_return_if_fail(window_with_focus != NULL);
994 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
997 static void refresh(gpointer data, guint action, GtkWidget *widget)
999 g_return_if_fail(window_with_focus != NULL);
1001 filer_refresh(window_with_focus);
1004 static void save_settings(gpointer data, guint action, GtkWidget *widget)
1006 g_return_if_fail(window_with_focus != NULL);
1008 filer_save_settings(window_with_focus);
1011 static void delete(FilerWindow *filer_window)
1013 GList *paths;
1014 paths = filer_selected_items(filer_window);
1015 action_delete(paths);
1016 destroy_glist(&paths);
1019 static void usage(FilerWindow *filer_window)
1021 GList *paths;
1022 paths = filer_selected_items(filer_window);
1023 action_usage(paths);
1024 destroy_glist(&paths);
1027 static void chmod_items(FilerWindow *filer_window)
1029 GList *paths;
1030 paths = filer_selected_items(filer_window);
1031 action_chmod(paths, FALSE, NULL);
1032 destroy_glist(&paths);
1035 static void set_type_items(FilerWindow *filer_window)
1037 GList *paths, *p;
1038 int npass=0, nfail=0;
1040 paths = filer_selected_items(filer_window);
1041 for(p=paths; p; p=g_list_next(p)) {
1042 if(xattr_supported((const char *) p->data))
1043 npass++;
1044 else
1045 nfail++;
1047 if(npass==0)
1048 report_error(_("Extended attributes, used to store types, are not supported for this "
1049 "file or files.\n"
1050 "This may be due to lack of support from the filesystem or the C library, "
1051 "or it may simply be that the filesystem needs to be mounted with "
1052 "the right mount option ('user_xattr' on Linux)."));
1053 else if(nfail>0)
1054 report_error(_("Setting type not supported for some of these files"));
1055 if(npass>0)
1056 action_settype(paths, FALSE, NULL);
1057 destroy_glist(&paths);
1060 static void find(FilerWindow *filer_window)
1062 GList *paths;
1063 paths = filer_selected_items(filer_window);
1064 action_find(paths);
1065 destroy_glist(&paths);
1068 /* This creates a new savebox widget, and allows the user to pick a new path
1069 * for the file.
1070 * Once the new path has been picked, the callback will be called with
1071 * both the current and new paths.
1072 * NOTE: This function unrefs 'image'!
1074 static void savebox_show(const gchar *action, const gchar *path,
1075 MaskedPixmap *image, SaveCb callback,
1076 GdkDragAction dnd_action)
1078 GtkWidget *savebox = NULL;
1079 GtkWidget *check_relative = NULL;
1081 g_return_if_fail(image != NULL);
1083 savebox = gtk_savebox_new(action);
1084 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1086 if (callback == link_cb)
1088 check_relative = gtk_check_button_new_with_mnemonic(
1089 _("_Relative link"));
1090 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1091 TRUE);
1093 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1094 gtk_tooltips_set_tip(tooltips, check_relative,
1095 _("If on, the symlink will store the path from the "
1096 "symlink to the target file. Use this if the symlink "
1097 "and the target will be moved together.\n"
1098 "If off, the path from the root directory is stored - "
1099 "use this if the symlink may move but the target will "
1100 "stay put."), NULL);
1101 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1102 check_relative, FALSE, TRUE, 0);
1103 gtk_widget_show(check_relative);
1106 g_signal_connect(savebox, "save_to_file",
1107 G_CALLBACK(save_to_file), NULL);
1109 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1110 g_strdup(path), g_free);
1111 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1112 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1114 gtk_window_set_title(GTK_WINDOW(savebox), action);
1116 if (g_utf8_validate(path, -1, NULL))
1117 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1118 else
1120 gchar *u8, *dir;
1121 dir = g_path_get_dirname(path);
1122 u8 = to_utf8(g_basename(path));
1123 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1124 make_path(dir, u8));
1125 g_free(u8);
1126 g_free(dir);
1128 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1129 g_object_unref(image);
1131 gtk_widget_show(savebox);
1134 static gint save_to_file(GObject *savebox,
1135 const gchar *pathname, gpointer data)
1137 SaveCb callback;
1138 const gchar *current_path;
1140 callback = g_object_get_data(savebox, "action_callback");
1141 current_path = g_object_get_data(savebox, "current_path");
1143 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1144 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1146 return callback(savebox, current_path, pathname)
1147 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1150 static gboolean copy_cb(GObject *savebox,
1151 const gchar *current, const gchar *new)
1153 return action_with_leaf(action_copy, current, new);
1156 static gboolean action_with_leaf(ActionFn action,
1157 const gchar *current, const gchar *new)
1159 const char *leaf;
1160 char *new_dir;
1161 GList *local_paths;
1163 if (new[0] != '/')
1165 report_error(_("New pathname is not absolute"));
1166 return FALSE;
1169 if (new[strlen(new) - 1] == '/')
1171 new_dir = g_strdup(new);
1172 leaf = NULL;
1174 else
1176 const gchar *slash;
1178 slash = strrchr(new, '/');
1179 new_dir = g_strndup(new, slash - new);
1180 leaf = slash + 1;
1183 local_paths = g_list_append(NULL, (gchar *) current);
1184 action(local_paths, new_dir, leaf, -1);
1185 g_list_free(local_paths);
1187 g_free(new_dir);
1189 return TRUE;
1192 /* Open a savebox to act on the selected file.
1193 * Call 'callback' later to perform the operation.
1195 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1196 const gchar *action, SaveCb callback,
1197 GdkDragAction dnd_action)
1199 g_object_ref(image);
1200 savebox_show(action, path, image, callback, dnd_action);
1203 static gboolean rename_cb(GObject *savebox,
1204 const gchar *current, const gchar *new)
1206 return action_with_leaf(action_move, current, new);
1209 static gboolean link_cb(GObject *savebox,
1210 const gchar *initial, const gchar *path)
1212 GtkToggleButton *check_relative;
1213 struct stat info;
1214 int err;
1215 gchar *link_path;
1217 check_relative = g_object_get_data(savebox, "check_relative");
1219 if (gtk_toggle_button_get_active(check_relative))
1220 link_path = get_relative_path(path, initial);
1221 else
1222 link_path = g_strdup(initial);
1224 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1226 GtkWidget *box, *button;
1227 gint ans;
1229 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1230 GTK_BUTTONS_CANCEL,
1231 _("Symlink from '%s' already exists. "
1232 "Replace it with a link to '%s'?"),
1233 path, link_path);
1235 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1237 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1238 gtk_widget_show(button);
1239 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1240 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1241 button, GTK_RESPONSE_OK);
1242 gtk_dialog_set_default_response(GTK_DIALOG(box),
1243 GTK_RESPONSE_OK);
1245 ans = gtk_dialog_run(GTK_DIALOG(box));
1246 gtk_widget_destroy(box);
1248 if (ans != GTK_RESPONSE_OK)
1250 g_free(link_path);
1251 return FALSE;
1254 unlink(path);
1257 err = symlink(link_path, path);
1258 g_free(link_path);
1260 if (err)
1262 report_error("symlink: %s", g_strerror(errno));
1263 return FALSE;
1266 dir_check_this(path);
1268 return TRUE;
1271 static void run_action(DirItem *item)
1273 if (can_set_run_action(item))
1274 type_set_handler_dialog(item->mime_type);
1275 else
1276 report_error(
1277 _("You can only set the run action for a "
1278 "regular file"));
1281 void open_home(gpointer data, guint action, GtkWidget *widget)
1283 filer_opendir(home_dir, NULL, NULL);
1286 static void select_all(gpointer data, guint action, GtkWidget *widget)
1288 g_return_if_fail(window_with_focus != NULL);
1290 window_with_focus->temp_item_selected = FALSE;
1291 view_select_all(window_with_focus->view);
1294 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1296 g_return_if_fail(window_with_focus != NULL);
1298 window_with_focus->temp_item_selected = FALSE;
1299 view_clear_selection(window_with_focus->view);
1302 static gboolean invert_cb(ViewIter *iter, gpointer data)
1304 return !view_get_selected((ViewIface *) data, iter);
1307 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1309 g_return_if_fail(window_with_focus != NULL);
1311 window_with_focus->temp_item_selected = FALSE;
1313 view_select_if(window_with_focus->view, invert_cb,
1314 window_with_focus->view);
1317 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1319 GtkWidget *win;
1321 win = options_show();
1323 if (win)
1325 number_of_windows++;
1326 g_signal_connect(win, "destroy",
1327 G_CALLBACK(one_less_window), NULL);
1331 static gboolean new_directory_cb(GObject *savebox,
1332 const gchar *initial, const gchar *path)
1334 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1336 report_error("mkdir: %s", g_strerror(errno));
1337 return FALSE;
1340 dir_check_this(path);
1342 if (filer_exists(window_with_focus))
1344 guchar *leaf;
1345 leaf = strrchr(path, '/');
1346 if (leaf)
1347 display_set_autoselect(window_with_focus, leaf + 1);
1350 return TRUE;
1353 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1355 g_return_if_fail(window_with_focus != NULL);
1357 savebox_show(_("Create"),
1358 make_path(window_with_focus->sym_path, _("NewDir")),
1359 type_to_icon(inode_directory), new_directory_cb,
1360 GDK_ACTION_COPY);
1363 static gboolean new_file_cb(GObject *savebox,
1364 const gchar *initial, const gchar *path)
1366 int fd;
1368 fd = open(path, O_CREAT | O_EXCL, 0666);
1370 if (fd == -1)
1372 report_error(_("Error creating '%s': %s"),
1373 path, g_strerror(errno));
1374 return FALSE;
1377 if (close(fd))
1378 report_error(_("Error creating '%s': %s"),
1379 path, g_strerror(errno));
1381 dir_check_this(path);
1383 if (filer_exists(window_with_focus))
1385 guchar *leaf;
1386 leaf = strrchr(path, '/');
1387 if (leaf)
1388 display_set_autoselect(window_with_focus, leaf + 1);
1391 return TRUE;
1394 static void new_file(gpointer data, guint action, GtkWidget *widget)
1396 g_return_if_fail(window_with_focus != NULL);
1398 savebox_show(_("Create"),
1399 make_path(window_with_focus->sym_path, _("NewFile")),
1400 type_to_icon(text_plain),
1401 new_file_cb, GDK_ACTION_COPY);
1404 static gboolean new_file_type_cb(GObject *savebox,
1405 const gchar *initial, const gchar *path)
1407 const gchar *oleaf, *leaf;
1408 gchar *templ, *templ_dname, *dest;
1409 GList *paths;
1411 /* We can work out the template path from the initial name */
1412 oleaf = g_basename(initial);
1413 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1414 if (!templ_dname)
1416 report_error(
1417 _("Error creating file: could not find the template for %s"),
1418 oleaf);
1419 return FALSE;
1422 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1423 g_free(templ_dname);
1425 dest = g_path_get_dirname(path);
1426 leaf = g_basename(path);
1427 paths = g_list_append(NULL, templ);
1429 action_copy(paths, dest, leaf, TRUE);
1431 g_list_free(paths);
1432 g_free(dest);
1433 g_free(templ);
1435 if (filer_exists(window_with_focus))
1436 display_set_autoselect(window_with_focus, leaf);
1438 return TRUE;
1441 static void do_send_to(gchar *templ)
1443 g_return_if_fail(send_to_paths != NULL);
1445 run_with_files(templ, send_to_paths);
1448 static void new_file_type(gchar *templ)
1450 const gchar *leaf;
1451 MIME_type *type;
1453 g_return_if_fail(window_with_focus != NULL);
1455 leaf = g_basename(templ);
1456 type = type_get_type(templ);
1458 savebox_show(_("Create"),
1459 make_path(window_with_focus->sym_path, leaf),
1460 type_to_icon(type),
1461 new_file_type_cb, GDK_ACTION_COPY);
1464 static void customise_send_to(gpointer data)
1466 GPtrArray *path;
1467 guchar *save;
1468 GString *dirs;
1469 int i;
1471 dirs = g_string_new(NULL);
1473 path = choices_list_xdg_dirs("", SITE);
1474 for (i = 0; i < path->len; i++)
1476 guchar *old = (guchar *) path->pdata[i];
1478 g_string_append(dirs, old);
1479 g_string_append(dirs, "/SendTo\n");
1481 choices_free_list(path);
1483 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1484 if (save)
1485 mkdir(save, 0777);
1487 info_message(
1488 _("The `Send To' menu provides a quick way to send some files "
1489 "to an application. The applications listed are those in "
1490 "the following directories:\n\n%s\n%s\n"
1491 "The `Send To' menu may be opened by Shift+Menu clicking "
1492 "over a file.\n\n"
1493 "Advanced use:\n"
1494 "You can also create subdirectories called "
1495 "`.text_html', `.text', etc which will only be "
1496 "shown for files of that type. `.group' is shown "
1497 "only when multiple files are selected."),
1498 dirs->str,
1499 save ? _("I'll show you your SendTo directory now; you should "
1500 "symlink (Ctrl+Shift drag) any applications you want "
1501 "into it.")
1502 : _("Your CHOICESPATH variable setting prevents "
1503 "customisations - sorry."));
1505 g_string_free(dirs, TRUE);
1507 if (save)
1508 filer_opendir(save, NULL, NULL);
1511 static void customise_new(gpointer data)
1513 GPtrArray *path;
1514 guchar *save;
1515 GString *dirs;
1516 int i;
1518 dirs = g_string_new(NULL);
1520 path = choices_list_xdg_dirs("", SITE);
1521 for (i = 0; i < path->len; i++)
1523 guchar *old = (guchar *) path->pdata[i];
1525 g_string_append(dirs, old);
1526 g_string_append(dirs, "/Templates\n");
1528 choices_free_list(path);
1530 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1531 if (save)
1532 mkdir(save, 0777);
1534 info_message(
1535 _("Any files placed in your Templates directories will "
1536 "appear on the `New' menu. Choosing one of them will make "
1537 "a copy of it as the new file.\n\n"
1538 "The following directories contain templates:\n\n%s\n%s\n"),
1539 dirs->str,
1540 save ? _("I'll show you your Templates directory now; you "
1541 "should place any template files you want inside it.")
1542 : _("Your CHOICESPATH variable setting prevents "
1543 "customisations - sorry."));
1545 g_string_free(dirs, TRUE);
1547 if (save)
1548 filer_opendir(save, NULL, NULL);
1551 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1552 * to the menu.
1554 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1556 gchar *searchdir;
1557 GPtrArray *paths;
1558 int i;
1560 if (subtype)
1561 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1562 else if (type)
1563 searchdir = g_strdup_printf("SendTo/.%s", type);
1564 else
1565 searchdir = g_strdup("SendTo");
1567 paths = choices_list_xdg_dirs(searchdir, SITE);
1568 g_free(searchdir);
1570 for (i = 0; i < paths->len; i++)
1572 GList *widgets = NULL;
1573 guchar *dir = (guchar *) paths->pdata[i];
1575 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1576 (CallbackFn) do_send_to,
1577 FALSE, FALSE, TRUE);
1579 if (widgets)
1580 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1581 gtk_menu_item_new());
1583 g_list_free(widgets); /* TODO: Get rid of this */
1586 choices_free_list(paths);
1589 /* Scan the SendTo dir and create and show the Send To menu.
1590 * The 'paths' list and every path in it is claimed, and will be
1591 * freed later -- don't free it yourself!
1593 static void show_send_to_menu(GList *paths, GdkEvent *event)
1595 GtkWidget *menu, *item;
1597 menu = gtk_menu_new();
1599 if (g_list_length(paths) == 1)
1601 DirItem *item;
1603 item = diritem_new("");
1604 diritem_restat(paths->data, item, NULL);
1606 add_sendto(menu,
1607 item->mime_type->media_type,
1608 item->mime_type->subtype);
1610 add_sendto(menu, item->mime_type->media_type, NULL);
1612 diritem_free(item);
1614 else
1616 GList *rover;
1617 gboolean same=TRUE, same_media=TRUE;
1618 MIME_type *type=NULL;
1619 DirItem *item;
1621 item = diritem_new("");
1622 for(rover=paths; rover; rover=g_list_next(rover))
1624 diritem_restat(rover->data, item, NULL);
1625 if(!type)
1626 type=item->mime_type;
1627 else
1629 if(type!=item->mime_type)
1631 same=FALSE;
1632 if(strcmp(type->media_type,
1633 item->mime_type->media_type)!=0)
1635 same_media=FALSE;
1636 break;
1641 diritem_free(item);
1643 if(type)
1645 if(same)
1646 add_sendto(menu, type->media_type,
1647 type->subtype);
1648 if(same_media)
1649 add_sendto(menu, type->media_type, NULL);
1652 add_sendto(menu, "group", NULL);
1655 add_sendto(menu, NULL, NULL);
1657 item = gtk_menu_item_new_with_label(_("Customise"));
1658 g_signal_connect_swapped(item, "activate",
1659 G_CALLBACK(customise_send_to), NULL);
1660 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1662 if (send_to_paths)
1663 destroy_glist(&send_to_paths);
1665 send_to_paths = paths;
1667 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1669 popup_menu = menu;
1670 show_popup_menu(menu, event, 0);
1673 static void send_to(FilerWindow *filer_window)
1675 GList *paths;
1676 GdkEvent *event;
1678 paths = filer_selected_items(filer_window);
1679 event = gtk_get_current_event();
1681 /* Eats paths */
1682 show_send_to_menu(paths, event);
1684 if (event)
1685 gdk_event_free(event);
1688 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1690 const char *argv[] = {"sh", "-c", NULL, NULL};
1691 gboolean close = action;
1693 argv[2] = o_menu_xterm.value;
1695 g_return_if_fail(window_with_focus != NULL);
1697 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1698 gtk_widget_destroy(window_with_focus->window);
1701 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1703 g_return_if_fail(window_with_focus != NULL);
1705 filer_change_to(window_with_focus, home_dir, NULL);
1708 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1710 g_return_if_fail(window_with_focus != NULL);
1712 bookmarks_show_menu(window_with_focus);
1715 static void show_log(gpointer data, guint action, GtkWidget *widget)
1717 g_return_if_fail(window_with_focus != NULL);
1719 log_show_window();
1722 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1724 g_return_if_fail(window_with_focus != NULL);
1726 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1727 filer_change_to(window_with_focus,
1728 window_with_focus->real_path, NULL);
1729 else
1730 delayed_error(_("This is already the canonical name "
1731 "for this directory."));
1734 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1736 g_return_if_fail(window_with_focus != NULL);
1738 filer_open_parent(window_with_focus);
1741 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1743 g_return_if_fail(window_with_focus != NULL);
1745 change_to_parent(window_with_focus);
1748 static void resize(gpointer data, guint action, GtkWidget *widget)
1750 g_return_if_fail(window_with_focus != NULL);
1752 view_autosize(window_with_focus->view);
1755 static void new_window(gpointer data, guint action, GtkWidget *widget)
1757 g_return_if_fail(window_with_focus != NULL);
1759 if (o_unique_filer_windows.int_value)
1761 report_error(_("You can't open a second view onto "
1762 "this directory because the `Unique Windows' option "
1763 "is turned on in the Options window."));
1765 else
1766 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1769 static void close_window(gpointer data, guint action, GtkWidget *widget)
1771 g_return_if_fail(window_with_focus != NULL);
1773 if (!filer_window_delete(window_with_focus->window, NULL,
1774 window_with_focus))
1775 gtk_widget_destroy(window_with_focus->window);
1778 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1780 MiniType type = (MiniType) action;
1782 g_return_if_fail(window_with_focus != NULL);
1784 /* Item needs to remain selected... */
1785 if (type == MINI_SHELL)
1786 window_with_focus->temp_item_selected = FALSE;
1788 minibuffer_show(window_with_focus, type);
1791 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1793 if (action == HELP_ABOUT)
1794 infobox_new(app_dir);
1795 else if (action == HELP_DIR)
1796 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1797 else if (action == HELP_MANUAL)
1799 gchar *manual = NULL;
1801 if (current_lang)
1803 manual = g_strconcat(app_dir, "/Help/Manual-",
1804 current_lang, ".html", NULL);
1805 if (!file_exists(manual) && strchr(current_lang, '_'))
1807 /* Try again without the territory */
1808 strcpy(strrchr(manual, '_'), ".html");
1810 if (!file_exists(manual))
1811 null_g_free(&manual);
1814 if (!manual)
1815 manual = g_strconcat(app_dir,
1816 "/Help/Manual.html", NULL);
1818 run_by_path(manual);
1820 g_free(manual);
1822 else
1823 g_warning("Unknown help action %d\n", action);
1826 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1827 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1829 GList *items, *item;
1831 items = gtk_container_get_children(GTK_CONTAINER(menu));
1833 item = g_list_nth(items, from);
1834 while (item && n--)
1836 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1837 item = item->next;
1839 g_list_free(items);
1842 static void save_menus(void)
1844 char *menurc;
1846 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1847 if (menurc)
1849 gtk_accel_map_save(menurc);
1850 g_free(menurc);
1854 static void select_nth_item(GtkMenuShell *shell, int n)
1856 GList *items;
1857 GtkWidget *item;
1859 items = gtk_container_get_children(GTK_CONTAINER(shell));
1860 item = g_list_nth_data(items, n);
1862 g_return_if_fail(item != NULL);
1864 g_list_free(items);
1866 gtk_menu_shell_select_item(shell, item);
1869 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1871 DirItem *item;
1872 const guchar *path;
1873 int n_selected;
1874 ViewIter iter;
1876 g_return_if_fail(window_with_focus != NULL);
1878 n_selected = view_count_selected(window_with_focus->view);
1880 if (n_selected < 1)
1882 const char *prompt;
1884 switch (action)
1886 case FILE_COPY_ITEM:
1887 prompt = _("Copy ... ?");
1888 break;
1889 case FILE_RENAME_ITEM:
1890 prompt = _("Rename ... ?");
1891 break;
1892 case FILE_LINK_ITEM:
1893 prompt = _("Symlink ... ?");
1894 break;
1895 case FILE_OPEN_FILE:
1896 prompt = _("Shift Open ... ?");
1897 break;
1898 case FILE_PROPERTIES:
1899 prompt = _("Properties of ... ?");
1900 break;
1901 case FILE_SET_TYPE:
1902 prompt = _("Set type of ... ?");
1903 break;
1904 case FILE_RUN_ACTION:
1905 prompt = _("Set run action for ... ?");
1906 break;
1907 case FILE_SET_ICON:
1908 prompt = _("Set icon for ... ?");
1909 break;
1910 case FILE_SEND_TO:
1911 prompt = _("Send ... to ... ?");
1912 break;
1913 case FILE_DELETE:
1914 prompt = _("DELETE ... ?");
1915 break;
1916 case FILE_USAGE:
1917 prompt = _("Count the size of ... ?");
1918 break;
1919 case FILE_CHMOD_ITEMS:
1920 prompt = _("Set permissions on ... ?");
1921 break;
1922 case FILE_FIND:
1923 prompt = _("Search inside ... ?");
1924 break;
1925 default:
1926 g_warning("Unknown action!");
1927 return;
1929 filer_target_mode(window_with_focus, target_callback,
1930 GINT_TO_POINTER(action), prompt);
1931 return;
1934 switch (action)
1936 case FILE_SEND_TO:
1937 send_to(window_with_focus);
1938 return;
1939 case FILE_DELETE:
1940 delete(window_with_focus);
1941 return;
1942 case FILE_USAGE:
1943 usage(window_with_focus);
1944 return;
1945 case FILE_CHMOD_ITEMS:
1946 chmod_items(window_with_focus);
1947 return;
1948 case FILE_SET_TYPE:
1949 set_type_items(window_with_focus);
1950 return;
1951 case FILE_FIND:
1952 find(window_with_focus);
1953 return;
1954 case FILE_PROPERTIES:
1956 GList *items;
1958 items = filer_selected_items(window_with_focus);
1959 infobox_show_list(items);
1960 destroy_glist(&items);
1961 return;
1963 case FILE_RENAME_ITEM:
1964 if (n_selected > 1)
1966 GList *items = NULL;
1967 ViewIter iter;
1969 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1970 while ((item = iter.next(&iter)))
1971 items = g_list_prepend(items, item->leafname);
1972 items = g_list_reverse(items);
1974 bulk_rename(window_with_focus->sym_path, items);
1975 g_list_free(items);
1976 return;
1978 break; /* Not a bulk rename... see below */
1979 default:
1980 break;
1983 /* All the following actions require exactly one file selected */
1985 if (n_selected > 1)
1987 report_error(_("You cannot do this to more than "
1988 "one item at a time"));
1989 return;
1992 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1994 item = iter.next(&iter);
1995 g_return_if_fail(item != NULL);
1996 /* iter may be passed to filer_openitem... */
1998 if (item->base_type == TYPE_UNKNOWN)
1999 item = dir_update_item(window_with_focus->directory,
2000 item->leafname);
2002 if (!item)
2004 report_error(_("Item no longer exists!"));
2005 return;
2008 path = make_path(window_with_focus->sym_path, item->leafname);
2010 switch (action)
2012 case FILE_COPY_ITEM:
2013 src_dest_action_item(path, di_image(item),
2014 _("Copy"), copy_cb,
2015 GDK_ACTION_COPY);
2016 break;
2017 case FILE_RENAME_ITEM:
2018 src_dest_action_item(path, di_image(item),
2019 _("Rename"), rename_cb,
2020 GDK_ACTION_MOVE);
2021 break;
2022 case FILE_LINK_ITEM:
2023 src_dest_action_item(path, di_image(item),
2024 _("Symlink"), link_cb,
2025 GDK_ACTION_LINK);
2026 break;
2027 case FILE_OPEN_FILE:
2028 filer_openitem(window_with_focus, &iter,
2029 OPEN_SAME_WINDOW | OPEN_SHIFT);
2030 break;
2031 case FILE_RUN_ACTION:
2032 run_action(item);
2033 break;
2034 case FILE_SET_ICON:
2035 icon_set_handler_dialog(item, path);
2036 break;
2037 default:
2038 g_warning("Unknown action!");
2039 return;
2043 static void show_key_help(GtkWidget *button, gpointer data)
2045 gboolean can_change_accels;
2047 g_object_get(G_OBJECT(gtk_settings_get_default()),
2048 "gtk-can-change-accels", &can_change_accels,
2049 NULL);
2051 if (!can_change_accels)
2053 info_message(_("User-definable shortcuts are disabled by "
2054 "default in Gtk2, and you have not enabled "
2055 "them. You can turn this feature on by:\n\n"
2056 "1) using an XSettings manager, such as ROX-Session "
2057 "or gnome-settings-daemon, or\n\n"
2058 "2) adding this line to ~/.gtkrc-2.0:\n"
2059 "\tgtk-can-change-accels = 1\n"
2060 "\t(this only works if NOT using XSETTINGS)"));
2061 return;
2064 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2065 "- Open the menu over a filer window,\n"
2066 "- Move the pointer over the item you want to use,\n"
2067 "- Press the key you want attached to it.\n\n"
2068 "The key will appear next to the menu item and you can just press "
2069 "that key without opening the menu in future."));
2072 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2074 GtkWidget *button, *align;
2076 g_return_val_if_fail(option == NULL, NULL);
2078 align = gtk_alignment_new(0, 0.5, 0, 0);
2079 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2080 gtk_container_add(GTK_CONTAINER(align), button);
2081 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2083 return g_list_append(NULL, align);