r1615: Fix massive security hole (alexander).
[rox-filer.git] / ROX-Filer / src / menu.c
blob50a4f6d1624ce40f7382c9ef0e4a23ea5f5eb12a
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* menu.c - code for handling the popup menus */
24 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <dirent.h>
35 #include <gtk/gtk.h>
37 #include "global.h"
39 #include "menu.h"
40 #include "run.h"
41 #include "action.h"
42 #include "filer.h"
43 #include "pixmaps.h"
44 #include "type.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "options.h"
48 #include "choices.h"
49 #include "gtksavebox.h"
50 #include "mount.h"
51 #include "minibuffer.h"
52 #include "i18n.h"
53 #include "main.h"
54 #include "pinboard.h"
55 #include "dir.h"
56 #include "diritem.h"
57 #include "appmenu.h"
58 #include "usericons.h"
59 #include "infobox.h"
60 #include "view_iface.h"
61 #include "collection.h"
62 #include "display.h"
64 typedef enum {
65 FILE_COPY_ITEM,
66 FILE_RENAME_ITEM,
67 FILE_LINK_ITEM,
68 FILE_OPEN_FILE,
69 FILE_HELP,
70 FILE_SHOW_FILE_INFO,
71 FILE_RUN_ACTION,
72 FILE_SET_ICON,
73 FILE_SEND_TO,
74 FILE_DELETE,
75 FILE_USAGE,
76 FILE_CHMOD_ITEMS,
77 FILE_FIND,
78 FILE_OPEN_VFS_AVFS,
79 } FileOp;
81 typedef enum menu_icon_style {
82 MIS_NONE, MIS_SMALL, MIS_LARGE,
83 MIS_HUGE_UNUSED,
84 MIS_CURRENT, /* As per current filer window */
85 MIS_DEFAULT
86 } MenuIconStyle;
88 typedef void (*ActionFn)(GList *paths,
89 const char *dest_dir, const char *leaf, int quiet);
90 typedef void MenuCallback(GtkWidget *widget, gpointer data);
92 typedef gboolean (*SaveCb)(GObject *savebox,
93 const gchar *current, const gchar *new);
95 GtkAccelGroup *filer_keys = NULL;
97 static GtkWidget *popup_menu = NULL; /* Currently open menu */
99 static gint updating_menu = 0; /* Non-zero => ignore activations */
100 static GList *send_to_paths = NULL;
102 static Option o_menu_iconsize, o_menu_xterm;
104 /* Static prototypes */
106 static void save_menus(void);
107 static void menu_closed(GtkWidget *widget);
108 static void shade_file_menu_items(gboolean shaded);
109 static void savebox_show(const gchar *action, const gchar *path,
110 MaskedPixmap *image, SaveCb callback);
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);
123 /* Note that for most of these callbacks none of the arguments are used. */
125 /* (action used in these three - DetailsType) */
126 static void huge_with(gpointer data, guint action, GtkWidget *widget);
127 static void large_with(gpointer data, guint action, GtkWidget *widget);
128 static void small_with(gpointer data, guint action, GtkWidget *widget);
130 static void sort_name(gpointer data, guint action, GtkWidget *widget);
131 static void sort_type(gpointer data, guint action, GtkWidget *widget);
132 static void sort_size(gpointer data, guint action, GtkWidget *widget);
133 static void sort_date(gpointer data, guint action, GtkWidget *widget);
135 static void hidden(gpointer data, guint action, GtkWidget *widget);
136 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
137 static void refresh(gpointer data, guint action, GtkWidget *widget);
139 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
141 static void select_all(gpointer data, guint action, GtkWidget *widget);
142 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
143 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
144 static void new_directory(gpointer data, guint action, GtkWidget *widget);
145 static void new_file(gpointer data, guint action, GtkWidget *widget);
146 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
149 static void open_parent(gpointer data, guint action, GtkWidget *widget);
150 static void home_directory(gpointer data, guint action, GtkWidget *widget);
151 static void new_window(gpointer data, guint action, GtkWidget *widget);
152 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
153 static void close_window(gpointer data, guint action, GtkWidget *widget);
154 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
156 /* (action used in this - MiniType) */
157 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
158 static void resize(gpointer data, guint action, GtkWidget *widget);
160 #define MENUS_NAME "menus2"
162 static GtkWidget *filer_menu; /* The popup filer menu */
163 static GtkWidget *filer_file_item; /* The File '' label */
164 static GtkWidget *filer_file_menu; /* The File '' menu */
165 static GtkWidget *file_shift_item; /* Shift Open label */
166 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
167 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
168 static GtkWidget *filer_new_window; /* The New Window item */
169 static GtkWidget *filer_new_menu; /* The New submenu */
170 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
172 #undef N_
173 #define N_(x) x
175 static GtkItemFactoryEntry filer_menu_def[] = {
176 {N_("Display"), NULL, NULL, 0, "<Branch>"},
177 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
178 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
179 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
180 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
181 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
182 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
183 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
184 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
185 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
186 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
187 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
188 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
189 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
190 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
191 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
192 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
193 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
194 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
195 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
196 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
197 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
198 {">", NULL, NULL, 0, "<Separator>"},
199 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
200 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
201 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
202 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
203 {">", NULL, NULL, 0, "<Separator>"},
204 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
205 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
206 {">" N_("Refresh"), NULL, refresh, 0, NULL},
207 {N_("File"), NULL, NULL, 0, "<Branch>"},
208 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
209 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
210 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
211 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
212 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
213 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
214 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
215 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
216 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
217 {">", NULL, NULL, 0, "<Separator>"},
218 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
219 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
220 {">" N_("Disk Usage"), NULL, file_op, FILE_USAGE, NULL},
221 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
222 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
223 {N_("Select"), NULL, NULL, 0, "<Branch>"},
224 {">" N_("Select All"), NULL, select_all, 0, NULL},
225 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
226 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
227 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
228 {N_("Options..."), NULL, menu_show_options, 0, NULL},
229 {N_("New"), NULL, NULL, 0, "<Branch>"},
230 {">" N_("Directory"), NULL, new_directory, 0, NULL},
231 {">" N_("Blank file"), NULL, new_file, 0, NULL},
232 {N_("Window"), NULL, NULL, 0, "<Branch>"},
233 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
234 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
235 {">" N_("New Window"), NULL, new_window, 0, NULL},
236 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
237 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
238 {">" N_("Resize Window"), NULL, resize, 0, NULL},
239 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
241 {">" N_("Close Window"), NULL, close_window, 0, NULL},
242 {">", NULL, NULL, 0, "<Separator>"},
243 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
244 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
245 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
246 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
247 {">", NULL, NULL, 0, "<Separator>"},
248 {">" N_("Show ROX-Filer Help"), "F1", menu_rox_help, 0, NULL},
252 #define GET_MENU_ITEM(var, menu) \
253 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
255 #define GET_SMENU_ITEM(var, menu, sub) \
256 do { \
257 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
258 var = gtk_item_factory_get_widget(item_factory, tmp); \
259 g_free(tmp); \
260 } while (0)
262 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
263 do { \
264 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
265 var = gtk_item_factory_get_widget(item_factory, tmp); \
266 g_free(tmp); \
267 } while (0)
269 void ensure_filer_menu(void)
271 GList *items;
272 guchar *tmp;
273 GtkWidget *item;
274 GtkItemFactory *item_factory;
276 if (filer_keys)
277 return;
279 filer_keys = gtk_accel_group_new();
280 item_factory = menu_create(filer_menu_def,
281 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
282 "<filer>", filer_keys);
284 GET_MENU_ITEM(filer_menu, "filer");
285 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
286 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
287 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
288 "Show Thumbnails");
290 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
291 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
292 filer_follow_sym = GTK_BIN(item)->child;
294 /* File '' label... */
295 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
296 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
297 g_list_free(items);
299 /* Shift Open... label */
300 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
301 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
302 g_list_free(items);
304 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
305 filer_new_window = GTK_BIN(item)->child;
307 g_signal_connect(filer_menu, "unmap_event",
308 G_CALLBACK(menu_closed), NULL);
309 g_signal_connect(filer_file_menu, "unmap_event",
310 G_CALLBACK(menu_closed), NULL);
312 g_signal_connect(filer_keys, "accel_changed",
313 G_CALLBACK(save_menus), NULL);
316 void menu_init(void)
318 char *menurc;
320 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
321 if (menurc)
323 gtk_accel_map_load(menurc);
324 g_free(menurc);
327 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
328 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
329 option_add_saver(save_menus);
331 option_register_widget("menu-set-keys", set_keys_button);
334 /* Name is in the form "<panel>" */
335 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
336 const gchar *name, GtkAccelGroup *keys)
338 GtkItemFactory *item_factory;
339 GtkItemFactoryEntry *translated;
341 if (!keys)
343 keys = gtk_accel_group_new();
344 gtk_accel_group_lock(keys);
347 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
349 translated = translate_entries(def, n_entries);
350 gtk_item_factory_create_items(item_factory, n_entries,
351 translated, NULL);
352 free_translated_entries(translated, n_entries);
354 return item_factory;
357 /* Prevent the user from setting a short-cut on this item */
358 static void menuitem_no_shortcuts(GtkWidget *item)
360 /* XXX */
361 #if 0
362 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
364 _gtk_widget_set_accel_path(item, NULL, NULL);
365 g_free(menuitem->accel_path);
366 menuitem->accel_path = NULL;
367 #endif
370 static void shade_file_menu_items(gboolean shaded)
372 menu_set_items_shaded(filer_file_menu, shaded, 0, 5);
373 menu_set_items_shaded(filer_file_menu, shaded, 6, 3);
376 /* 'data' is an array of three ints:
377 * [ pointer_x, pointer_y, item_under_pointer ]
379 void position_menu(GtkMenu *menu, gint *x, gint *y,
380 gboolean *push_in, gpointer data)
382 int *pos = (int *) data;
383 GtkRequisition requisition;
384 GList *items, *next;
385 int y_shift = 0;
386 int item = pos[2];
388 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
390 while (item >= 0 && next)
392 int h = ((GtkWidget *) next->data)->requisition.height;
394 if (item > 0)
395 y_shift += h;
396 else
397 y_shift += h / 2;
399 next = next->next;
400 item--;
403 g_list_free(items);
405 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
407 *x = pos[0] - (requisition.width * 7 / 8);
408 *y = pos[1] - y_shift;
410 *x = CLAMP(*x, 0, screen_width - requisition.width);
411 *y = CLAMP(*y, 0, screen_height - requisition.height);
413 *push_in = FALSE;
416 #if 0
417 /* Used when you menu-click on the Large or Small toolbar tools */
418 void show_style_menu(FilerWindow *filer_window,
419 GdkEventButton *event,
420 GtkWidget *menu)
422 int pos[3];
424 pos[0] = event->x_root;
425 pos[1] = event->y_root;
426 pos[2] = 0;
428 window_with_focus = filer_window;
430 popup_menu = menu;
432 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
433 (gpointer) pos, event->button, event->time);
435 #endif
437 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
438 MenuIconStyle style, CallbackFn func,
439 gboolean separator, gboolean strip_ext,
440 gboolean recurse)
442 GList *widgets = NULL;
443 DirItem *ditem;
444 DIR *dir;
445 struct dirent *ent;
446 GtkWidget *item;
447 char *dname = NULL;
449 dname = pathdup(dir_name);
451 dir = opendir(dname);
452 if (!dir)
453 goto out;
455 if (separator)
457 item = gtk_menu_item_new();
458 widgets = g_list_append(widgets, item);
459 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
462 while ((ent = readdir(dir)))
464 char *dot, *leaf;
465 GtkWidget *hbox;
466 GtkWidget *img;
467 GtkWidget *label;
468 gchar *fname;
470 /* Ignore hidden files */
471 if (ent->d_name[0] == '.')
472 continue;
474 /* Strip off extension, if any */
475 dot = strchr(ent->d_name, '.');
476 if (strip_ext && dot)
477 leaf = g_strndup(ent->d_name, dot - ent->d_name);
478 else
479 leaf = g_strdup(ent->d_name);
481 fname = g_strconcat(dname, "/", ent->d_name, NULL);
482 ditem = diritem_new("");
483 diritem_restat(fname, ditem, NULL);
485 if (ditem->image && style != MIS_NONE)
487 GdkPixbuf *pixbuf;
489 switch (style) {
490 case MIS_LARGE:
491 pixbuf = ditem->image->pixbuf;
492 break;
493 default:
494 if (!ditem->image->sm_pixbuf)
495 pixmap_make_small(ditem->image);
496 pixbuf = ditem->image->sm_pixbuf;
497 break;
500 item = gtk_menu_item_new();
501 /* TODO: Find a way to allow short-cuts */
502 menuitem_no_shortcuts(item);
504 hbox = gtk_hbox_new(FALSE, 2);
505 gtk_container_add(GTK_CONTAINER(item), hbox);
507 img = gtk_image_new_from_pixbuf(pixbuf);
509 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
511 label = gtk_label_new(leaf);
512 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
513 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
516 else
517 item = gtk_menu_item_new_with_label(leaf);
519 /* If it is a directory (but NOT an AppDir) and we are
520 * recursing then set up a sub menu.
522 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
523 !(ditem->flags & ITEM_FLAG_APPDIR))
525 GtkWidget *sub;
526 GList *new_widgets;
528 sub = gtk_menu_new();
529 new_widgets = menu_from_dir(sub, fname, style, func,
530 separator, strip_ext, FALSE);
531 g_list_free(new_widgets);
532 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
534 else
535 g_signal_connect_swapped(item, "activate",
536 G_CALLBACK(func), fname);
538 diritem_free(ditem);
539 g_free(leaf);
541 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
542 g_signal_connect_swapped(item, "destroy",
543 G_CALLBACK(g_free), fname);
545 widgets = g_list_append(widgets, item);
548 closedir(dir);
549 out:
550 g_free(dname);
552 return widgets;
555 /* Scan the templates dir and create entries for the New menu */
556 static void update_new_files_menu(MenuIconStyle style)
558 static GList *widgets = NULL;
560 gchar *templ_dname = NULL;
562 if (widgets)
564 GList *next;
566 for (next = widgets; next; next = next->next)
567 gtk_widget_destroy((GtkWidget *) next->data);
569 g_list_free(widgets);
570 widgets = NULL;
573 templ_dname = choices_find_path_load("Templates", "");
574 if (templ_dname)
576 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
577 (CallbackFn) new_file_type, TRUE, TRUE,
578 FALSE);
579 g_free(templ_dname);
581 gtk_widget_show_all(filer_new_menu);
584 /* 'item' is the number of the item to appear under the pointer. */
585 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
587 int pos[3];
588 int button = 0;
589 guint32 time = 0;
591 if (event && (event->type == GDK_BUTTON_PRESS ||
592 event->type == GDK_BUTTON_RELEASE))
594 GdkEventButton *bev = (GdkEventButton *) event;
596 pos[0] = bev->x_root;
597 pos[1] = bev->y_root;
598 button = bev->button;
599 time = bev->time;
601 else if (event && event->type == GDK_KEY_PRESS)
603 GdkEventKey *kev = (GdkEventKey *) event;
605 get_pointer_xy(pos, pos + 1);
606 time = kev->time;
608 else
609 get_pointer_xy(pos, pos + 1);
611 pos[2] = item;
613 gtk_widget_show_all(menu);
614 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
615 position_menu, (gpointer) pos, button, time);
616 select_nth_item(GTK_MENU_SHELL(menu), item);
619 /* Hide the popup menu, if any */
620 void menu_popdown(void)
622 if (popup_menu)
623 gtk_menu_popdown(GTK_MENU(popup_menu));
626 static MenuIconStyle get_menu_icon_style(void)
628 MenuIconStyle mis;
629 int display;
631 mis = o_menu_iconsize.int_value;
633 switch (mis)
635 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
636 return mis;
637 default:
638 break;
641 if (mis == MIS_CURRENT && window_with_focus)
643 switch (window_with_focus->display_style)
645 case HUGE_ICONS:
646 case LARGE_ICONS:
647 return MIS_LARGE;
648 case SMALL_ICONS:
649 return MIS_SMALL;
650 default:
651 break;
655 display = o_display_size.int_value;
656 switch (display)
658 case HUGE_ICONS:
659 case LARGE_ICONS:
660 return MIS_LARGE;
661 case SMALL_ICONS:
662 return MIS_SMALL;
663 default:
664 break;
667 return MIS_SMALL;
670 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
672 DirItem *file_item = NULL;
673 GdkModifierType state = 0;
674 int n_selected;
676 n_selected = view_count_selected(filer_window->view);
678 ensure_filer_menu();
680 updating_menu++;
682 /* Remove previous AppMenu, if any */
683 appmenu_remove();
685 window_with_focus = filer_window;
687 if (event->type == GDK_BUTTON_PRESS)
688 state = ((GdkEventButton *) event)->state;
689 else if (event->type == GDK_KEY_PRESS)
690 state = ((GdkEventKey *) event)->state;
692 if (n_selected == 0 && item >= 0)
694 filer_window->temp_item_selected = TRUE;
695 collection_select_item(filer_window->collection, item);
696 n_selected = view_count_selected(filer_window->view);
698 else
700 filer_window->temp_item_selected = FALSE;
703 /* Short-cut to the Send To menu */
704 if (state & GDK_SHIFT_MASK)
706 GList *paths;
708 updating_menu--;
710 if (n_selected == 0)
712 report_error(
713 _("You should Shift+Menu click over a file to "
714 "send it somewhere"));
715 return;
718 paths = filer_selected_items(filer_window);
720 show_send_to_menu(paths, event); /* (paths eaten) */
722 return;
726 GtkWidget *file_label, *file_menu;
727 GString *buffer;
728 DirItem *item;
730 file_label = filer_file_item;
731 file_menu = filer_file_menu;
732 gtk_check_menu_item_set_active(
733 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
734 filer_window->show_thumbs);
735 gtk_check_menu_item_set_active(
736 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
737 filer_window->show_hidden);
738 buffer = g_string_new(NULL);
740 switch (n_selected)
742 case 0:
743 g_string_assign(buffer, _("Next Click"));
744 shade_file_menu_items(FALSE);
745 break;
746 case 1:
747 item = filer_selected_item(filer_window);
748 if (!item->image)
749 dir_update_item(filer_window->directory,
750 item->leafname);
751 shade_file_menu_items(FALSE);
752 file_item = filer_selected_item(filer_window);
753 g_string_sprintf(buffer, "%s '%s'",
754 basetype_name(file_item),
755 file_item->leafname);
756 if (!can_set_run_action(file_item))
757 menu_set_items_shaded(filer_file_menu,
758 TRUE, 6, 1);
759 break;
760 default:
761 shade_file_menu_items(TRUE);
762 g_string_sprintf(buffer, _("%d items"),
763 n_selected);
764 break;
766 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
767 g_string_free(buffer, TRUE);
769 menu_show_shift_action(file_shift_item, file_item,
770 n_selected == 0);
771 if (file_item)
772 appmenu_add(make_path(filer_window->sym_path,
773 file_item->leafname)->str,
774 file_item, filer_file_menu);
777 update_new_files_menu(get_menu_icon_style());
779 gtk_widget_set_sensitive(filer_new_window,
780 !o_unique_filer_windows.int_value);
781 gtk_widget_set_sensitive(filer_follow_sym,
782 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
784 popup_menu = (state & GDK_CONTROL_MASK)
785 ? filer_file_menu
786 : filer_menu;
788 updating_menu--;
790 show_popup_menu(popup_menu, event,
791 popup_menu == filer_file_menu ? 5 : 1);
794 static void menu_closed(GtkWidget *widget)
796 if (window_with_focus == NULL || widget != popup_menu)
797 return; /* Close panel item chosen? */
799 popup_menu = NULL;
801 if (window_with_focus->temp_item_selected)
803 view_clear_selection(window_with_focus->view);
804 window_with_focus->temp_item_selected = FALSE;
808 static void target_callback(FilerWindow *filer_window,
809 gint item,
810 gpointer action)
812 Collection *collection = filer_window->collection;
814 g_return_if_fail(filer_window != NULL);
816 window_with_focus = filer_window;
818 /* Don't grab the primary selection */
819 filer_window->temp_item_selected = TRUE;
821 collection_wink_item(collection, item);
822 collection_clear_except(collection, item);
823 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
825 if (item < collection->number_of_items)
826 collection_unselect_item(collection, item);
827 filer_window->temp_item_selected = FALSE;
830 /* Set the text of the 'Shift Open...' menu item.
831 * If icon is NULL, reset the text and also shade it, unless 'next'.
833 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
835 guchar *shift_action = NULL;
837 if (item)
839 if (item->flags & ITEM_FLAG_MOUNT_POINT)
841 if (item->flags & ITEM_FLAG_MOUNTED)
842 shift_action = N_("Unmount");
843 else
844 shift_action = N_("Mount");
846 else if (item->flags & ITEM_FLAG_SYMLINK)
847 shift_action = N_("Show Target");
848 else if (item->base_type == TYPE_DIRECTORY)
849 shift_action = N_("Look Inside");
850 else if (item->base_type == TYPE_FILE)
851 shift_action = N_("Open As Text");
853 gtk_label_set_text(GTK_LABEL(menu_item),
854 shift_action ? _(shift_action)
855 : _("Shift Open"));
856 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
859 /* Actions */
861 static void huge_with(gpointer data, guint action, GtkWidget *widget)
863 display_set_layout(window_with_focus, HUGE_ICONS, action);
866 static void large_with(gpointer data, guint action, GtkWidget *widget)
868 display_set_layout(window_with_focus, LARGE_ICONS, action);
871 static void small_with(gpointer data, guint action, GtkWidget *widget)
873 display_set_layout(window_with_focus, SMALL_ICONS, action);
876 static void sort_name(gpointer data, guint action, GtkWidget *widget)
878 g_return_if_fail(window_with_focus != NULL);
880 display_set_sort_fn(window_with_focus, sort_by_name);
883 static void sort_type(gpointer data, guint action, GtkWidget *widget)
885 g_return_if_fail(window_with_focus != NULL);
887 display_set_sort_fn(window_with_focus, sort_by_type);
890 static void sort_date(gpointer data, guint action, GtkWidget *widget)
892 g_return_if_fail(window_with_focus != NULL);
894 display_set_sort_fn(window_with_focus, sort_by_date);
897 static void sort_size(gpointer data, guint action, GtkWidget *widget)
899 g_return_if_fail(window_with_focus != NULL);
901 display_set_sort_fn(window_with_focus, sort_by_size);
904 static void hidden(gpointer data, guint action, GtkWidget *widget)
906 if (updating_menu)
907 return;
909 g_return_if_fail(window_with_focus != NULL);
911 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
914 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
916 if (updating_menu)
917 return;
919 g_return_if_fail(window_with_focus != NULL);
921 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
924 static void refresh(gpointer data, guint action, GtkWidget *widget)
926 g_return_if_fail(window_with_focus != NULL);
928 full_refresh();
929 filer_update_dir(window_with_focus, TRUE);
932 static void delete(FilerWindow *filer_window)
934 GList *paths;
935 paths = filer_selected_items(filer_window);
936 action_delete(paths);
937 g_list_foreach(paths, (GFunc) g_free, NULL);
938 g_list_free(paths);
941 static void usage(FilerWindow *filer_window)
943 GList *paths;
944 paths = filer_selected_items(filer_window);
945 action_usage(paths);
946 g_list_foreach(paths, (GFunc) g_free, NULL);
947 g_list_free(paths);
950 static void chmod_items(FilerWindow *filer_window)
952 GList *paths;
953 paths = filer_selected_items(filer_window);
954 action_chmod(paths);
955 g_list_foreach(paths, (GFunc) g_free, NULL);
956 g_list_free(paths);
959 static void find(FilerWindow *filer_window)
961 GList *paths;
962 paths = filer_selected_items(filer_window);
963 action_find(paths);
964 g_list_foreach(paths, (GFunc) g_free, NULL);
965 g_list_free(paths);
968 /* This creates a new savebox widget, and allows the user to pick a new path
969 * for the file.
970 * Once the new path has been picked, the callback will be called with
971 * both the current and new paths.
972 * NOTE: This function unrefs 'image'!
974 static void savebox_show(const gchar *action, const gchar *path,
975 MaskedPixmap *image, SaveCb callback)
977 GtkWidget *savebox = NULL;
978 GtkWidget *check_relative = NULL;
980 g_return_if_fail(image != NULL);
982 savebox = gtk_savebox_new(action);
984 if (callback == link_cb)
986 check_relative = gtk_check_button_new_with_mnemonic(
987 _("_Relative link"));
988 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
989 TRUE);
991 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
992 gtk_tooltips_set_tip(tooltips, check_relative,
993 _("If on, the symlink will store the path from the "
994 "symlink to the target file. Use this if the symlink "
995 "and the target will be moved together.\n"
996 "If off, the path from the root directory is stored - "
997 "use this if the symlink may move but the target will "
998 "stay put."), NULL);
999 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1000 check_relative, FALSE, TRUE, 0);
1001 gtk_widget_show(check_relative);
1004 g_signal_connect(savebox, "save_to_file",
1005 G_CALLBACK(save_to_file), NULL);
1007 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1008 g_strdup(path), g_free);
1009 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1010 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1012 gtk_window_set_title(GTK_WINDOW(savebox), action);
1014 if (g_utf8_validate(path, -1, NULL))
1015 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1016 else
1018 gchar *u8;
1019 u8 = to_utf8(path);
1020 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1021 g_free(u8);
1023 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1024 g_object_unref(image);
1026 gtk_widget_show(savebox);
1029 static gint save_to_file(GObject *savebox,
1030 const gchar *pathname, gpointer data)
1032 SaveCb callback;
1033 const gchar *current_path;
1035 callback = g_object_get_data(savebox, "action_callback");
1036 current_path = g_object_get_data(savebox, "current_path");
1038 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1039 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1041 return callback(savebox, current_path, pathname)
1042 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1045 static gboolean copy_cb(GObject *savebox,
1046 const gchar *current, const gchar *new)
1048 return action_with_leaf(action_copy, current, new);
1051 static gboolean action_with_leaf(ActionFn action,
1052 const gchar *current, const gchar *new)
1054 const char *leaf;
1055 char *new_dir;
1056 GList *local_paths;
1058 if (new[0] != '/')
1060 report_error(_("New pathname is not absolute"));
1061 return FALSE;
1064 if (new[strlen(new) - 1] == '/')
1066 new_dir = g_strdup(new);
1067 leaf = NULL;
1069 else
1071 const gchar *slash;
1073 slash = strrchr(new, '/');
1074 new_dir = g_strndup(new, slash - new);
1075 leaf = slash + 1;
1078 local_paths = g_list_append(NULL, (gchar *) current);
1079 action(local_paths, new_dir, leaf, -1);
1080 g_list_free(local_paths);
1082 g_free(new_dir);
1084 return TRUE;
1087 /* Open a savebox to act on the selected file.
1088 * Call 'callback' later to perform the operation.
1090 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1091 const gchar *action, SaveCb callback)
1093 g_object_ref(image);
1094 savebox_show(action, path, image, callback);
1097 static gboolean rename_cb(GObject *savebox,
1098 const gchar *current, const gchar *new)
1100 return action_with_leaf(action_move, current, new);
1103 static gboolean link_cb(GObject *savebox,
1104 const gchar *initial, const gchar *path)
1106 GtkToggleButton *check_relative;
1107 struct stat info;
1108 int err;
1109 gchar *link_path;
1111 check_relative = g_object_get_data(savebox, "check_relative");
1113 if (gtk_toggle_button_get_active(check_relative))
1114 link_path = get_relative_path(path, initial);
1115 else
1116 link_path = g_strdup(initial);
1118 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1120 GtkWidget *box, *button;
1121 gint ans;
1123 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1124 GTK_BUTTONS_CANCEL,
1125 _("Symlink from '%s' already exists. "
1126 "Replace it with a link to '%s'?"),
1127 path, link_path);
1129 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1131 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1132 gtk_widget_show(button);
1133 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1134 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1135 button, GTK_RESPONSE_OK);
1136 gtk_dialog_set_default_response(GTK_DIALOG(box),
1137 GTK_RESPONSE_OK);
1139 ans = gtk_dialog_run(GTK_DIALOG(box));
1140 gtk_widget_destroy(box);
1142 if (ans != GTK_RESPONSE_OK)
1144 g_free(link_path);
1145 return FALSE;
1148 unlink(path);
1151 err = symlink(link_path, path);
1152 g_free(link_path);
1154 if (err)
1156 report_error("symlink: %s", g_strerror(errno));
1157 return FALSE;
1160 dir_check_this(path);
1162 return TRUE;
1165 static void run_action(DirItem *item)
1167 if (can_set_run_action(item))
1168 type_set_handler_dialog(item->mime_type);
1169 else
1170 report_error(
1171 _("You can only set the run action for a "
1172 "regular file"));
1175 void open_home(gpointer data, guint action, GtkWidget *widget)
1177 filer_opendir(home_dir, NULL);
1180 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1182 gchar *path;
1184 path = g_strconcat(filer_window->sym_path,
1185 "/", item->leafname, "#", NULL);
1187 filer_change_to(filer_window, path, NULL);
1188 g_free(path);
1191 static void select_all(gpointer data, guint action, GtkWidget *widget)
1193 g_return_if_fail(window_with_focus != NULL);
1195 window_with_focus->temp_item_selected = FALSE;
1196 collection_select_all(window_with_focus->collection);
1199 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1201 g_return_if_fail(window_with_focus != NULL);
1203 window_with_focus->temp_item_selected = FALSE;
1204 view_clear_selection(window_with_focus->view);
1207 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1209 g_return_if_fail(window_with_focus != NULL);
1211 window_with_focus->temp_item_selected = FALSE;
1212 collection_invert_selection(window_with_focus->collection);
1215 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1217 GtkWidget *win;
1219 win = options_show();
1221 if (win)
1223 number_of_windows++;
1224 g_signal_connect(win, "destroy",
1225 G_CALLBACK(one_less_window), NULL);
1229 static gboolean new_directory_cb(GObject *savebox,
1230 const gchar *initial, const gchar *path)
1232 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1234 report_error("mkdir: %s", g_strerror(errno));
1235 return FALSE;
1238 dir_check_this(path);
1240 if (filer_exists(window_with_focus))
1242 guchar *leaf;
1243 leaf = strrchr(path, '/');
1244 if (leaf)
1245 display_set_autoselect(window_with_focus, leaf + 1);
1248 return TRUE;
1251 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1253 g_return_if_fail(window_with_focus != NULL);
1255 savebox_show(_("Create"),
1256 make_path(window_with_focus->sym_path, _("NewDir"))->str,
1257 type_to_icon(inode_directory), new_directory_cb);
1260 static gboolean new_file_cb(GObject *savebox,
1261 const gchar *initial, const gchar *path)
1263 int fd;
1265 fd = open(path, O_CREAT | O_EXCL, 0666);
1267 if (fd == -1)
1269 report_error(_("Error creating '%s': %s"),
1270 path, g_strerror(errno));
1271 return FALSE;
1274 if (close(fd))
1275 report_error(_("Error creating '%s': %s"),
1276 path, g_strerror(errno));
1278 dir_check_this(path);
1280 if (filer_exists(window_with_focus))
1282 guchar *leaf;
1283 leaf = strrchr(path, '/');
1284 if (leaf)
1285 display_set_autoselect(window_with_focus, leaf + 1);
1288 return TRUE;
1291 static void new_file(gpointer data, guint action, GtkWidget *widget)
1293 g_return_if_fail(window_with_focus != NULL);
1295 savebox_show(_("Create"),
1296 make_path(window_with_focus->sym_path, _("NewFile"))->str,
1297 type_to_icon(text_plain),
1298 new_file_cb);
1301 static gboolean new_file_type_cb(GObject *savebox,
1302 const gchar *initial, const gchar *path)
1304 const gchar *oleaf, *leaf;
1305 gchar *templ, *templ_dname, *dest;
1306 GList *paths;
1308 /* We can work out the template path from the initial name */
1309 oleaf = g_basename(initial);
1310 templ_dname = choices_find_path_load("Templates", "");
1311 if (!templ_dname)
1313 report_error(
1314 _("Error creating file: could not find the template for %s"),
1315 oleaf);
1316 return FALSE;
1319 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1320 g_free(templ_dname);
1322 dest = g_dirname(path);
1323 leaf = g_basename(path);
1324 paths = g_list_append(NULL, templ);
1326 action_copy(paths, dest, leaf, TRUE);
1328 g_list_free(paths);
1329 g_free(dest);
1330 g_free(templ);
1332 if (filer_exists(window_with_focus))
1333 display_set_autoselect(window_with_focus, leaf);
1335 return TRUE;
1338 static void do_send_to(gchar *templ)
1340 g_return_if_fail(send_to_paths != NULL);
1342 run_with_files(templ, send_to_paths);
1345 static void new_file_type(gchar *templ)
1347 const gchar *leaf;
1348 MIME_type *type;
1350 g_return_if_fail(window_with_focus != NULL);
1352 leaf = g_basename(templ);
1353 type = type_get_type(templ);
1355 savebox_show(_("Create"),
1356 make_path(window_with_focus->sym_path, leaf)->str,
1357 type_to_icon(type),
1358 new_file_type_cb);
1361 static void customise_send_to(gpointer data)
1363 GPtrArray *path;
1364 guchar *save;
1365 GString *dirs;
1366 int i;
1368 dirs = g_string_new(NULL);
1370 path = choices_list_dirs("");
1371 for (i = 0; i < path->len; i++)
1373 guchar *old = (guchar *) path->pdata[i];
1375 g_string_append(dirs, old);
1376 g_string_append(dirs, "SendTo\n");
1378 choices_free_list(path);
1380 save = choices_find_path_save("", "SendTo", TRUE);
1381 if (save)
1382 mkdir(save, 0777);
1384 info_message(
1385 _("The `Send To' menu provides a quick way to send some files "
1386 "to an application. The applications listed are those in "
1387 "the following directories:\n\n%s\n%s\n"
1388 "The `Send To' menu may be opened by Shift+Menu clicking "
1389 "over a file.\n\n"
1390 "Advanced use:\n"
1391 "You can also create subdirectories called "
1392 "`.text_html', `.text', etc which will only be "
1393 "shown for files of that type. `.group' is shown "
1394 "only when multiple files are selected."),
1395 dirs->str,
1396 save ? _("I'll show you your SendTo directory now; you should "
1397 "symlink (Ctrl+Shift drag) any applications you want "
1398 "into it.")
1399 : _("Your CHOICESPATH variable setting prevents "
1400 "customisations - sorry."));
1402 g_string_free(dirs, TRUE);
1404 if (save)
1405 filer_opendir(save, NULL);
1408 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1409 * to the menu.
1411 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1413 gchar *searchdir;
1414 GPtrArray *paths;
1415 int i;
1417 if (subtype)
1418 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1419 else if (type)
1420 searchdir = g_strdup_printf("SendTo/.%s", type);
1421 else
1422 searchdir = g_strdup("SendTo");
1424 paths = choices_list_dirs(searchdir);
1425 g_free(searchdir);
1427 for (i = 0; i < paths->len; i++)
1429 GList *widgets = NULL;
1430 guchar *dir = (guchar *) paths->pdata[i];
1432 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1433 (CallbackFn) do_send_to,
1434 FALSE, FALSE, TRUE);
1436 if (widgets)
1437 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1438 gtk_menu_item_new());
1440 g_list_free(widgets); /* TODO: Get rid of this */
1443 choices_free_list(paths);
1446 /* Scan the SendTo dir and create and show the Send To menu.
1447 * The 'paths' list and every path in it is claimed, and will be
1448 * freed later -- don't free it yourself!
1450 static void show_send_to_menu(GList *paths, GdkEvent *event)
1452 GtkWidget *menu, *item;
1454 menu = gtk_menu_new();
1456 if (g_list_length(paths) == 1)
1458 DirItem *item;
1460 item = diritem_new("");
1461 diritem_restat(paths->data, item, NULL);
1463 add_sendto(menu,
1464 item->mime_type->media_type,
1465 item->mime_type->subtype);
1467 add_sendto(menu, item->mime_type->media_type, NULL);
1469 diritem_free(item);
1471 else
1472 add_sendto(menu, "group", NULL);
1474 add_sendto(menu, NULL, NULL);
1476 item = gtk_menu_item_new_with_label(_("Customise"));
1477 g_signal_connect_swapped(item, "activate",
1478 G_CALLBACK(customise_send_to), NULL);
1479 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1481 if (send_to_paths)
1483 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1484 g_list_free(send_to_paths);
1486 send_to_paths = paths;
1488 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1490 popup_menu = menu;
1491 show_popup_menu(menu, event, 0);
1494 static void send_to(FilerWindow *filer_window)
1496 GList *paths;
1497 GdkEvent *event;
1499 paths = filer_selected_items(filer_window);
1500 event = gtk_get_current_event();
1502 /* Eats paths */
1503 show_send_to_menu(paths, event);
1505 gdk_event_free(event);
1508 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1510 const char *argv[] = {"sh", "-c", NULL, NULL};
1511 gboolean close = action;
1513 argv[2] = o_menu_xterm.value;
1515 g_return_if_fail(window_with_focus != NULL);
1517 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1518 gtk_widget_destroy(window_with_focus->window);
1521 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1523 g_return_if_fail(window_with_focus != NULL);
1525 filer_change_to(window_with_focus, home_dir, NULL);
1528 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1530 g_return_if_fail(window_with_focus != NULL);
1532 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1533 filer_change_to(window_with_focus,
1534 window_with_focus->real_path, NULL);
1535 else
1536 delayed_error(_("This is already the canonical name "
1537 "for this directory."));
1540 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1542 g_return_if_fail(window_with_focus != NULL);
1544 filer_open_parent(window_with_focus);
1547 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1549 g_return_if_fail(window_with_focus != NULL);
1551 change_to_parent(window_with_focus);
1554 static void resize(gpointer data, guint action, GtkWidget *widget)
1556 g_return_if_fail(window_with_focus != NULL);
1558 filer_window_autosize(window_with_focus, TRUE);
1561 static void new_window(gpointer data, guint action, GtkWidget *widget)
1563 g_return_if_fail(window_with_focus != NULL);
1565 if (o_unique_filer_windows.int_value)
1567 report_error(_("You can't open a second view onto "
1568 "this directory because the `Unique Windows' option "
1569 "is turned on in the Options window."));
1571 else
1572 filer_opendir(window_with_focus->sym_path, window_with_focus);
1575 #if 0
1576 static void su_to_user(GtkWidget *dialog)
1578 char *argv[] = {
1579 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1580 GtkEntry *user;
1581 guchar *path;
1583 g_return_if_fail(dialog != NULL);
1585 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1586 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1588 g_return_if_fail(user != NULL && path != NULL);
1590 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1591 argv[3] = gtk_entry_get_text(user);
1592 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1593 argv[5] = path;
1595 if (!spawn(argv))
1596 report_error(_("fork: %s"), g_strerror(errno));
1598 g_free(argv[2]);
1599 g_free(argv[4]);
1601 gtk_widget_destroy(dialog);
1604 static void new_user(gpointer data, guint action, GtkWidget *widget)
1606 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1608 g_return_if_fail(window_with_focus != NULL);
1610 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1611 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1612 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1613 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1614 g_strdup(window_with_focus->path), g_free);
1616 vbox = gtk_vbox_new(FALSE, 4);
1617 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1618 gtk_box_pack_start(GTK_BOX(vbox),
1619 gtk_label_new(_("Browse as which user?")),
1620 TRUE, TRUE, 2);
1622 hbox = gtk_hbox_new(FALSE, 4);
1623 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1625 gtk_box_pack_start(GTK_BOX(hbox),
1626 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1628 entry = gtk_entry_new();
1629 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1630 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1631 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1632 gtk_widget_grab_focus(entry);
1633 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1634 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1635 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1637 hbox = gtk_hbox_new(TRUE, 0);
1638 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1640 button = gtk_button_new_with_label(_("OK"));
1641 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1642 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1643 gtk_window_set_default(GTK_WINDOW(dialog), button);
1644 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1645 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1647 button = gtk_button_new_with_label(_("Cancel"));
1648 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1649 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1650 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1651 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1652 GTK_OBJECT(dialog));
1654 gtk_widget_show_all(dialog);
1656 #endif
1658 static void close_window(gpointer data, guint action, GtkWidget *widget)
1660 g_return_if_fail(window_with_focus != NULL);
1662 gtk_widget_destroy(window_with_focus->window);
1665 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1667 MiniType type = (MiniType) action;
1669 g_return_if_fail(window_with_focus != NULL);
1671 /* Item needs to remain selected... */
1672 if (type == MINI_SHELL)
1673 window_with_focus->temp_item_selected = FALSE;
1675 minibuffer_show(window_with_focus, type);
1678 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1680 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1683 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1684 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1686 GList *items, *item;
1688 items = gtk_container_get_children(GTK_CONTAINER(menu));
1690 item = g_list_nth(items, from);
1691 while (item && n--)
1693 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1694 item = item->next;
1696 g_list_free(items);
1699 static void save_menus(void)
1701 char *menurc;
1703 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1704 if (menurc)
1706 gtk_accel_map_save(menurc);
1707 g_free(menurc);
1711 static void select_nth_item(GtkMenuShell *shell, int n)
1713 GList *items;
1714 GtkWidget *item;
1716 items = gtk_container_get_children(GTK_CONTAINER(shell));
1717 item = g_list_nth_data(items, n);
1719 g_return_if_fail(item != NULL);
1721 g_list_free(items);
1723 gtk_menu_shell_select_item(shell, item);
1726 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1728 DirItem *item;
1729 gchar *path;
1730 int n_selected;
1732 g_return_if_fail(window_with_focus != NULL);
1734 n_selected = view_count_selected(window_with_focus->view);
1736 if (n_selected < 1)
1738 const char *prompt;
1740 switch (action)
1742 case FILE_COPY_ITEM:
1743 prompt = _("Copy ... ?");
1744 break;
1745 case FILE_RENAME_ITEM:
1746 prompt = _("Rename ... ?");
1747 break;
1748 case FILE_LINK_ITEM:
1749 prompt = _("Symlink ... ?");
1750 break;
1751 case FILE_OPEN_FILE:
1752 prompt = _("Shift Open ... ?");
1753 break;
1754 case FILE_HELP:
1755 prompt = _("Help about ... ?");
1756 break;
1757 case FILE_SHOW_FILE_INFO:
1758 prompt = _("Examine ... ?");
1759 break;
1760 case FILE_RUN_ACTION:
1761 prompt = _("Set run action for ... ?");
1762 break;
1763 case FILE_SET_ICON:
1764 prompt = _("Set icon for ... ?");
1765 break;
1766 case FILE_SEND_TO:
1767 prompt = _("Send ... to ... ?");
1768 break;
1769 case FILE_DELETE:
1770 prompt = _("DELETE ... ?");
1771 break;
1772 case FILE_USAGE:
1773 prompt = _("Count the size of ... ?");
1774 break;
1775 case FILE_CHMOD_ITEMS:
1776 prompt = _("Set permissions on ... ?");
1777 break;
1778 case FILE_FIND:
1779 prompt = _("Search inside ... ?");
1780 break;
1781 case FILE_OPEN_VFS_AVFS:
1782 prompt = _("Look inside ... ?");
1783 break;
1784 default:
1785 g_warning("Unknown action!");
1786 return;
1788 filer_target_mode(window_with_focus, target_callback,
1789 GINT_TO_POINTER(action), prompt);
1790 return;
1793 switch (action)
1795 case FILE_SEND_TO:
1796 send_to(window_with_focus);
1797 return;
1798 case FILE_DELETE:
1799 delete(window_with_focus);
1800 return;
1801 case FILE_USAGE:
1802 usage(window_with_focus);
1803 return;
1804 case FILE_CHMOD_ITEMS:
1805 chmod_items(window_with_focus);
1806 return;
1807 case FILE_FIND:
1808 find(window_with_focus);
1809 return;
1810 case FILE_SHOW_FILE_INFO:
1812 GList *items;
1814 items = filer_selected_items(window_with_focus);
1815 infobox_show_list(items);
1816 g_list_foreach(items, (GFunc) g_free, NULL);
1817 g_list_free(items);
1818 return;
1820 default:
1821 break;
1824 /* All the following actions require exactly one file selected */
1826 if (n_selected > 1)
1828 report_error(_("You cannot do this to more than "
1829 "one item at a time"));
1830 return;
1833 item = filer_selected_item(window_with_focus);
1834 g_return_if_fail(item != NULL);
1835 if (!item->image)
1836 item = dir_update_item(window_with_focus->directory,
1837 item->leafname);
1839 if (!item)
1841 report_error(_("Item no longer exists!"));
1842 return;
1845 path = make_path(window_with_focus->sym_path, item->leafname)->str;
1847 switch (action)
1849 case FILE_COPY_ITEM:
1850 src_dest_action_item(path, item->image,
1851 _("Copy"), copy_cb);
1852 break;
1853 case FILE_RENAME_ITEM:
1854 src_dest_action_item(path, item->image,
1855 _("Rename"), rename_cb);
1856 break;
1857 case FILE_LINK_ITEM:
1858 src_dest_action_item(path, item->image,
1859 _("Symlink"), link_cb);
1860 break;
1861 case FILE_OPEN_FILE:
1862 filer_openitem(window_with_focus,
1863 collection_selected_item_number(
1864 window_with_focus->collection),
1865 OPEN_SAME_WINDOW | OPEN_SHIFT);
1866 break;
1867 case FILE_HELP:
1868 show_item_help(path, item);
1869 break;
1870 case FILE_RUN_ACTION:
1871 run_action(item);
1872 break;
1873 case FILE_SET_ICON:
1874 icon_set_handler_dialog(item, path);
1875 break;
1876 case FILE_OPEN_VFS_AVFS:
1877 open_vfs_avfs(window_with_focus, item);
1878 break;
1879 default:
1880 g_warning("Unknown action!");
1881 return;
1885 static void show_key_help(GtkWidget *button, gpointer data)
1887 gboolean can_change_accels;
1889 g_object_get(G_OBJECT(gtk_settings_get_default()),
1890 "gtk-can-change-accels", &can_change_accels,
1891 NULL);
1893 if (!can_change_accels)
1895 info_message(_("User-definable shortcuts are disabled by "
1896 "default in Gtk2, and you have not enabled "
1897 "them. You can turn this feature on by:\n"
1898 "1) using an XSettings manager, such as ROX-Session\n"
1899 "or\n"
1900 "2) adding this line to ~/.gtkrc-2.0:\n"
1901 "\tgtk-can-change-accels = 1"));
1902 return;
1905 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1906 "- Open the menu over a filer window,\n"
1907 "- Move the pointer over the item you want to use,\n"
1908 "- Press the key you want attached to it.\n\n"
1909 "The key will appear next to the menu item and you can just press "
1910 "that key without opening the menu in future."));
1913 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1915 GtkWidget *button, *align;
1917 g_return_val_if_fail(option == NULL, NULL);
1919 align = gtk_alignment_new(0.5, 0.5, 0, 0);
1920 button = button_new_mixed(GTK_STOCK_DIALOG_INFO,
1921 _("Set keyboard shortcuts"));
1922 gtk_container_add(GTK_CONTAINER(align), button);
1923 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1925 return g_list_append(NULL, align);