r1717: Rearraged File menu (suggested by Tomas Berg).
[rox-filer.git] / ROX-Filer / src / menu.c
blob1b20c91e79ba82ee6343d188fefeba40bdafc22e
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_("Delete"), NULL, file_op, FILE_DELETE, NULL},
212 {">", NULL, NULL, 0, "<Separator>"},
213 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
214 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
215 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
216 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
217 {">", NULL, NULL, 0, "<Separator>"},
218 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
219 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
220 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
221 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
222 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
223 {">", NULL, NULL, 0, "<Separator>"},
224 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
225 {N_("Select"), NULL, NULL, 0, "<Branch>"},
226 {">" N_("Select All"), NULL, 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 If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
230 {N_("Options..."), NULL, menu_show_options, 0, NULL},
231 {N_("New"), NULL, NULL, 0, "<Branch>"},
232 {">" N_("Directory"), NULL, new_directory, 0, NULL},
233 {">" N_("Blank file"), NULL, new_file, 0, NULL},
234 {N_("Window"), NULL, NULL, 0, "<Branch>"},
235 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
236 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
237 {">" N_("New Window"), NULL, new_window, 0, NULL},
238 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
239 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
240 {">" N_("Resize Window"), NULL, resize, 0, NULL},
241 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
243 {">" N_("Close Window"), NULL, close_window, 0, NULL},
244 {">", NULL, NULL, 0, "<Separator>"},
245 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
246 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
247 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
248 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
249 {N_("Help"), NULL, NULL, 0, "<Branch>"},
250 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
251 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, NULL},
252 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
256 #define GET_MENU_ITEM(var, menu) \
257 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
259 #define GET_SMENU_ITEM(var, menu, sub) \
260 do { \
261 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
262 var = gtk_item_factory_get_widget(item_factory, tmp); \
263 g_free(tmp); \
264 } while (0)
266 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
267 do { \
268 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
269 var = gtk_item_factory_get_widget(item_factory, tmp); \
270 g_free(tmp); \
271 } while (0)
273 void ensure_filer_menu(void)
275 GList *items;
276 guchar *tmp;
277 GtkWidget *item;
278 GtkItemFactory *item_factory;
280 if (filer_keys)
281 return;
283 filer_keys = gtk_accel_group_new();
284 item_factory = menu_create(filer_menu_def,
285 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
286 "<filer>", filer_keys);
288 GET_MENU_ITEM(filer_menu, "filer");
289 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
290 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
291 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
292 "Show Thumbnails");
294 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
295 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
296 filer_follow_sym = GTK_BIN(item)->child;
298 /* File '' label... */
299 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
300 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
301 g_list_free(items);
303 /* Shift Open... label */
304 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
305 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
306 g_list_free(items);
308 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
309 filer_new_window = GTK_BIN(item)->child;
311 g_signal_connect(filer_menu, "unmap_event",
312 G_CALLBACK(menu_closed), NULL);
313 g_signal_connect(filer_file_menu, "unmap_event",
314 G_CALLBACK(menu_closed), NULL);
316 g_signal_connect(filer_keys, "accel_changed",
317 G_CALLBACK(save_menus), NULL);
320 void menu_init(void)
322 char *menurc;
324 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
325 if (menurc)
327 gtk_accel_map_load(menurc);
328 g_free(menurc);
331 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
332 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
333 option_add_saver(save_menus);
335 option_register_widget("menu-set-keys", set_keys_button);
338 /* Name is in the form "<panel>" */
339 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
340 const gchar *name, GtkAccelGroup *keys)
342 GtkItemFactory *item_factory;
343 GtkItemFactoryEntry *translated;
345 if (!keys)
347 keys = gtk_accel_group_new();
348 gtk_accel_group_lock(keys);
351 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
353 translated = translate_entries(def, n_entries);
354 gtk_item_factory_create_items(item_factory, n_entries,
355 translated, NULL);
356 free_translated_entries(translated, n_entries);
358 return item_factory;
361 /* Prevent the user from setting a short-cut on this item */
362 static void menuitem_no_shortcuts(GtkWidget *item)
364 /* XXX */
365 #if 0
366 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
368 _gtk_widget_set_accel_path(item, NULL, NULL);
369 g_free(menuitem->accel_path);
370 menuitem->accel_path = NULL;
371 #endif
374 /* Shade items that only work on single files */
375 static void shade_file_menu_items(gboolean shaded)
377 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
378 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
379 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
382 /* 'data' is an array of three ints:
383 * [ pointer_x, pointer_y, item_under_pointer ]
385 void position_menu(GtkMenu *menu, gint *x, gint *y,
386 gboolean *push_in, gpointer data)
388 int *pos = (int *) data;
389 GtkRequisition requisition;
390 GList *items, *next;
391 int y_shift = 0;
392 int item = pos[2];
394 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
396 while (item >= 0 && next)
398 int h = ((GtkWidget *) next->data)->requisition.height;
400 if (item > 0)
401 y_shift += h;
402 else
403 y_shift += h / 2;
405 next = next->next;
406 item--;
409 g_list_free(items);
411 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
413 *x = pos[0] - (requisition.width * 7 / 8);
414 *y = pos[1] - y_shift;
416 *x = CLAMP(*x, 0, screen_width - requisition.width);
417 *y = CLAMP(*y, 0, screen_height - requisition.height);
419 *push_in = FALSE;
422 #if 0
423 /* Used when you menu-click on the Large or Small toolbar tools */
424 void show_style_menu(FilerWindow *filer_window,
425 GdkEventButton *event,
426 GtkWidget *menu)
428 int pos[3];
430 pos[0] = event->x_root;
431 pos[1] = event->y_root;
432 pos[2] = 0;
434 window_with_focus = filer_window;
436 popup_menu = menu;
438 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
439 (gpointer) pos, event->button, event->time);
441 #endif
443 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
444 MenuIconStyle style, CallbackFn func,
445 gboolean separator, gboolean strip_ext,
446 gboolean recurse)
448 GList *widgets = NULL;
449 DirItem *ditem;
450 DIR *dir;
451 struct dirent *ent;
452 GtkWidget *item;
453 char *dname = NULL;
455 dname = pathdup(dir_name);
457 dir = opendir(dname);
458 if (!dir)
459 goto out;
461 if (separator)
463 item = gtk_menu_item_new();
464 widgets = g_list_append(widgets, item);
465 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
468 while ((ent = readdir(dir)))
470 char *dot, *leaf;
471 GtkWidget *hbox;
472 GtkWidget *img;
473 GtkWidget *label;
474 gchar *fname;
476 /* Ignore hidden files */
477 if (ent->d_name[0] == '.')
478 continue;
480 /* Strip off extension, if any */
481 dot = strchr(ent->d_name, '.');
482 if (strip_ext && dot)
483 leaf = g_strndup(ent->d_name, dot - ent->d_name);
484 else
485 leaf = g_strdup(ent->d_name);
487 fname = g_strconcat(dname, "/", ent->d_name, NULL);
488 ditem = diritem_new("");
489 diritem_restat(fname, ditem, NULL);
491 if (ditem->image && style != MIS_NONE)
493 GdkPixbuf *pixbuf;
495 switch (style) {
496 case MIS_LARGE:
497 pixbuf = ditem->image->pixbuf;
498 break;
499 default:
500 if (!ditem->image->sm_pixbuf)
501 pixmap_make_small(ditem->image);
502 pixbuf = ditem->image->sm_pixbuf;
503 break;
506 item = gtk_menu_item_new();
507 /* TODO: Find a way to allow short-cuts */
508 menuitem_no_shortcuts(item);
510 hbox = gtk_hbox_new(FALSE, 2);
511 gtk_container_add(GTK_CONTAINER(item), hbox);
513 img = gtk_image_new_from_pixbuf(pixbuf);
515 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
517 label = gtk_label_new(leaf);
518 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
519 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
522 else
523 item = gtk_menu_item_new_with_label(leaf);
525 /* If it is a directory (but NOT an AppDir) and we are
526 * recursing then set up a sub menu.
528 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
529 !(ditem->flags & ITEM_FLAG_APPDIR))
531 GtkWidget *sub;
532 GList *new_widgets;
534 sub = gtk_menu_new();
535 new_widgets = menu_from_dir(sub, fname, style, func,
536 separator, strip_ext, FALSE);
537 g_list_free(new_widgets);
538 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
540 else
541 g_signal_connect_swapped(item, "activate",
542 G_CALLBACK(func), fname);
544 diritem_free(ditem);
545 g_free(leaf);
547 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
548 g_signal_connect_swapped(item, "destroy",
549 G_CALLBACK(g_free), fname);
551 widgets = g_list_append(widgets, item);
554 closedir(dir);
555 out:
556 g_free(dname);
558 return widgets;
561 /* Scan the templates dir and create entries for the New menu */
562 static void update_new_files_menu(MenuIconStyle style)
564 static GList *widgets = NULL;
566 gchar *templ_dname = NULL;
568 if (widgets)
570 GList *next;
572 for (next = widgets; next; next = next->next)
573 gtk_widget_destroy((GtkWidget *) next->data);
575 g_list_free(widgets);
576 widgets = NULL;
579 templ_dname = choices_find_path_load("Templates", "");
580 if (templ_dname)
582 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
583 (CallbackFn) new_file_type, TRUE, TRUE,
584 FALSE);
585 g_free(templ_dname);
587 gtk_widget_show_all(filer_new_menu);
590 /* 'item' is the number of the item to appear under the pointer. */
591 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
593 int pos[3];
594 int button = 0;
595 guint32 time = 0;
597 if (event && (event->type == GDK_BUTTON_PRESS ||
598 event->type == GDK_BUTTON_RELEASE))
600 GdkEventButton *bev = (GdkEventButton *) event;
602 pos[0] = bev->x_root;
603 pos[1] = bev->y_root;
604 button = bev->button;
605 time = bev->time;
607 else if (event && event->type == GDK_KEY_PRESS)
609 GdkEventKey *kev = (GdkEventKey *) event;
611 get_pointer_xy(pos, pos + 1);
612 time = kev->time;
614 else
615 get_pointer_xy(pos, pos + 1);
617 pos[2] = item;
619 gtk_widget_show_all(menu);
620 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
621 position_menu, (gpointer) pos, button, time);
622 select_nth_item(GTK_MENU_SHELL(menu), item);
625 /* Hide the popup menu, if any */
626 void menu_popdown(void)
628 if (popup_menu)
629 gtk_menu_popdown(GTK_MENU(popup_menu));
632 static MenuIconStyle get_menu_icon_style(void)
634 MenuIconStyle mis;
635 int display;
637 mis = o_menu_iconsize.int_value;
639 switch (mis)
641 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
642 return mis;
643 default:
644 break;
647 if (mis == MIS_CURRENT && window_with_focus)
649 switch (window_with_focus->display_style)
651 case HUGE_ICONS:
652 case LARGE_ICONS:
653 return MIS_LARGE;
654 case SMALL_ICONS:
655 return MIS_SMALL;
656 default:
657 break;
661 display = o_display_size.int_value;
662 switch (display)
664 case HUGE_ICONS:
665 case LARGE_ICONS:
666 return MIS_LARGE;
667 case SMALL_ICONS:
668 return MIS_SMALL;
669 default:
670 break;
673 return MIS_SMALL;
676 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
678 DirItem *file_item = NULL;
679 GdkModifierType state = 0;
680 int n_selected;
682 n_selected = view_count_selected(filer_window->view);
684 ensure_filer_menu();
686 updating_menu++;
688 /* Remove previous AppMenu, if any */
689 appmenu_remove();
691 window_with_focus = filer_window;
693 if (event->type == GDK_BUTTON_PRESS)
694 state = ((GdkEventButton *) event)->state;
695 else if (event->type == GDK_KEY_PRESS)
696 state = ((GdkEventKey *) event)->state;
698 if (n_selected == 0 && item >= 0)
700 filer_window->temp_item_selected = TRUE;
701 collection_select_item(filer_window->collection, item);
702 n_selected = view_count_selected(filer_window->view);
704 else
706 filer_window->temp_item_selected = FALSE;
709 /* Short-cut to the Send To menu */
710 if (state & GDK_SHIFT_MASK)
712 GList *paths;
714 updating_menu--;
716 if (n_selected == 0)
718 report_error(
719 _("You should Shift+Menu click over a file to "
720 "send it somewhere"));
721 return;
724 paths = filer_selected_items(filer_window);
726 show_send_to_menu(paths, event); /* (paths eaten) */
728 return;
732 GtkWidget *file_label, *file_menu;
733 GString *buffer;
734 DirItem *item;
736 file_label = filer_file_item;
737 file_menu = filer_file_menu;
738 gtk_check_menu_item_set_active(
739 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
740 filer_window->show_thumbs);
741 gtk_check_menu_item_set_active(
742 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
743 filer_window->show_hidden);
744 buffer = g_string_new(NULL);
746 switch (n_selected)
748 case 0:
749 g_string_assign(buffer, _("Next Click"));
750 shade_file_menu_items(FALSE);
751 break;
752 case 1:
753 item = filer_selected_item(filer_window);
754 if (!item->image)
755 dir_update_item(filer_window->directory,
756 item->leafname);
757 shade_file_menu_items(FALSE);
758 file_item = filer_selected_item(filer_window);
759 g_string_sprintf(buffer, "%s '%s'",
760 basetype_name(file_item),
761 file_item->leafname);
762 if (!can_set_run_action(file_item))
763 menu_set_items_shaded(filer_file_menu,
764 TRUE, 6, 1);
765 break;
766 default:
767 shade_file_menu_items(TRUE);
768 g_string_sprintf(buffer, _("%d items"),
769 n_selected);
770 break;
772 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
773 g_string_free(buffer, TRUE);
775 menu_show_shift_action(file_shift_item, file_item,
776 n_selected == 0);
777 if (file_item)
778 appmenu_add(make_path(filer_window->sym_path,
779 file_item->leafname)->str,
780 file_item, filer_file_menu);
783 update_new_files_menu(get_menu_icon_style());
785 gtk_widget_set_sensitive(filer_new_window,
786 !o_unique_filer_windows.int_value);
787 gtk_widget_set_sensitive(filer_follow_sym,
788 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
790 popup_menu = (state & GDK_CONTROL_MASK)
791 ? filer_file_menu
792 : filer_menu;
794 updating_menu--;
796 show_popup_menu(popup_menu, event,
797 popup_menu == filer_file_menu ? 5 : 1);
800 static void menu_closed(GtkWidget *widget)
802 if (window_with_focus == NULL || widget != popup_menu)
803 return; /* Close panel item chosen? */
805 popup_menu = NULL;
807 if (window_with_focus->temp_item_selected)
809 view_clear_selection(window_with_focus->view);
810 window_with_focus->temp_item_selected = FALSE;
814 static void target_callback(FilerWindow *filer_window,
815 gint item,
816 gpointer action)
818 Collection *collection;
820 g_return_if_fail(filer_window != NULL);
822 collection = filer_window->collection;
824 window_with_focus = filer_window;
826 /* Don't grab the primary selection */
827 filer_window->temp_item_selected = TRUE;
829 collection_wink_item(collection, item);
830 collection_clear_except(collection, item);
831 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
833 if (item < collection->number_of_items)
834 collection_unselect_item(collection, item);
835 filer_window->temp_item_selected = FALSE;
838 /* Set the text of the 'Shift Open...' menu item.
839 * If icon is NULL, reset the text and also shade it, unless 'next'.
841 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
843 guchar *shift_action = NULL;
845 if (item)
847 if (item->flags & ITEM_FLAG_MOUNT_POINT)
849 if (item->flags & ITEM_FLAG_MOUNTED)
850 shift_action = N_("Unmount");
851 else
852 shift_action = N_("Mount");
854 else if (item->flags & ITEM_FLAG_SYMLINK)
855 shift_action = N_("Show Target");
856 else if (item->base_type == TYPE_DIRECTORY)
857 shift_action = N_("Look Inside");
858 else if (item->base_type == TYPE_FILE)
859 shift_action = N_("Open As Text");
861 gtk_label_set_text(GTK_LABEL(menu_item),
862 shift_action ? _(shift_action)
863 : _("Shift Open"));
864 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
867 /* Actions */
869 static void huge_with(gpointer data, guint action, GtkWidget *widget)
871 display_set_layout(window_with_focus, HUGE_ICONS, action);
874 static void large_with(gpointer data, guint action, GtkWidget *widget)
876 display_set_layout(window_with_focus, LARGE_ICONS, action);
879 static void small_with(gpointer data, guint action, GtkWidget *widget)
881 display_set_layout(window_with_focus, SMALL_ICONS, action);
884 static void sort_name(gpointer data, guint action, GtkWidget *widget)
886 g_return_if_fail(window_with_focus != NULL);
888 display_set_sort_fn(window_with_focus, sort_by_name);
891 static void sort_type(gpointer data, guint action, GtkWidget *widget)
893 g_return_if_fail(window_with_focus != NULL);
895 display_set_sort_fn(window_with_focus, sort_by_type);
898 static void sort_date(gpointer data, guint action, GtkWidget *widget)
900 g_return_if_fail(window_with_focus != NULL);
902 display_set_sort_fn(window_with_focus, sort_by_date);
905 static void sort_size(gpointer data, guint action, GtkWidget *widget)
907 g_return_if_fail(window_with_focus != NULL);
909 display_set_sort_fn(window_with_focus, sort_by_size);
912 static void hidden(gpointer data, guint action, GtkWidget *widget)
914 if (updating_menu)
915 return;
917 g_return_if_fail(window_with_focus != NULL);
919 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
922 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
924 if (updating_menu)
925 return;
927 g_return_if_fail(window_with_focus != NULL);
929 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
932 static void refresh(gpointer data, guint action, GtkWidget *widget)
934 g_return_if_fail(window_with_focus != NULL);
936 full_refresh();
937 filer_update_dir(window_with_focus, TRUE);
940 static void delete(FilerWindow *filer_window)
942 GList *paths;
943 paths = filer_selected_items(filer_window);
944 action_delete(paths);
945 g_list_foreach(paths, (GFunc) g_free, NULL);
946 g_list_free(paths);
949 static void usage(FilerWindow *filer_window)
951 GList *paths;
952 paths = filer_selected_items(filer_window);
953 action_usage(paths);
954 g_list_foreach(paths, (GFunc) g_free, NULL);
955 g_list_free(paths);
958 static void chmod_items(FilerWindow *filer_window)
960 GList *paths;
961 paths = filer_selected_items(filer_window);
962 action_chmod(paths, FALSE, NULL);
963 g_list_foreach(paths, (GFunc) g_free, NULL);
964 g_list_free(paths);
967 static void find(FilerWindow *filer_window)
969 GList *paths;
970 paths = filer_selected_items(filer_window);
971 action_find(paths);
972 g_list_foreach(paths, (GFunc) g_free, NULL);
973 g_list_free(paths);
976 /* This creates a new savebox widget, and allows the user to pick a new path
977 * for the file.
978 * Once the new path has been picked, the callback will be called with
979 * both the current and new paths.
980 * NOTE: This function unrefs 'image'!
982 static void savebox_show(const gchar *action, const gchar *path,
983 MaskedPixmap *image, SaveCb callback)
985 GtkWidget *savebox = NULL;
986 GtkWidget *check_relative = NULL;
988 g_return_if_fail(image != NULL);
990 savebox = gtk_savebox_new(action);
992 if (callback == link_cb)
994 check_relative = gtk_check_button_new_with_mnemonic(
995 _("_Relative link"));
996 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
997 TRUE);
999 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1000 gtk_tooltips_set_tip(tooltips, check_relative,
1001 _("If on, the symlink will store the path from the "
1002 "symlink to the target file. Use this if the symlink "
1003 "and the target will be moved together.\n"
1004 "If off, the path from the root directory is stored - "
1005 "use this if the symlink may move but the target will "
1006 "stay put."), NULL);
1007 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1008 check_relative, FALSE, TRUE, 0);
1009 gtk_widget_show(check_relative);
1012 g_signal_connect(savebox, "save_to_file",
1013 G_CALLBACK(save_to_file), NULL);
1015 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1016 g_strdup(path), g_free);
1017 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1018 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1020 gtk_window_set_title(GTK_WINDOW(savebox), action);
1022 if (g_utf8_validate(path, -1, NULL))
1023 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1024 else
1026 gchar *u8;
1027 u8 = to_utf8(path);
1028 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1029 g_free(u8);
1031 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1032 g_object_unref(image);
1034 gtk_widget_show(savebox);
1037 static gint save_to_file(GObject *savebox,
1038 const gchar *pathname, gpointer data)
1040 SaveCb callback;
1041 const gchar *current_path;
1043 callback = g_object_get_data(savebox, "action_callback");
1044 current_path = g_object_get_data(savebox, "current_path");
1046 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1047 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1049 return callback(savebox, current_path, pathname)
1050 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1053 static gboolean copy_cb(GObject *savebox,
1054 const gchar *current, const gchar *new)
1056 return action_with_leaf(action_copy, current, new);
1059 static gboolean action_with_leaf(ActionFn action,
1060 const gchar *current, const gchar *new)
1062 const char *leaf;
1063 char *new_dir;
1064 GList *local_paths;
1066 if (new[0] != '/')
1068 report_error(_("New pathname is not absolute"));
1069 return FALSE;
1072 if (new[strlen(new) - 1] == '/')
1074 new_dir = g_strdup(new);
1075 leaf = NULL;
1077 else
1079 const gchar *slash;
1081 slash = strrchr(new, '/');
1082 new_dir = g_strndup(new, slash - new);
1083 leaf = slash + 1;
1086 local_paths = g_list_append(NULL, (gchar *) current);
1087 action(local_paths, new_dir, leaf, -1);
1088 g_list_free(local_paths);
1090 g_free(new_dir);
1092 return TRUE;
1095 /* Open a savebox to act on the selected file.
1096 * Call 'callback' later to perform the operation.
1098 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1099 const gchar *action, SaveCb callback)
1101 g_object_ref(image);
1102 savebox_show(action, path, image, callback);
1105 static gboolean rename_cb(GObject *savebox,
1106 const gchar *current, const gchar *new)
1108 return action_with_leaf(action_move, current, new);
1111 static gboolean link_cb(GObject *savebox,
1112 const gchar *initial, const gchar *path)
1114 GtkToggleButton *check_relative;
1115 struct stat info;
1116 int err;
1117 gchar *link_path;
1119 check_relative = g_object_get_data(savebox, "check_relative");
1121 if (gtk_toggle_button_get_active(check_relative))
1122 link_path = get_relative_path(path, initial);
1123 else
1124 link_path = g_strdup(initial);
1126 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1128 GtkWidget *box, *button;
1129 gint ans;
1131 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1132 GTK_BUTTONS_CANCEL,
1133 _("Symlink from '%s' already exists. "
1134 "Replace it with a link to '%s'?"),
1135 path, link_path);
1137 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1139 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1140 gtk_widget_show(button);
1141 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1142 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1143 button, GTK_RESPONSE_OK);
1144 gtk_dialog_set_default_response(GTK_DIALOG(box),
1145 GTK_RESPONSE_OK);
1147 ans = gtk_dialog_run(GTK_DIALOG(box));
1148 gtk_widget_destroy(box);
1150 if (ans != GTK_RESPONSE_OK)
1152 g_free(link_path);
1153 return FALSE;
1156 unlink(path);
1159 err = symlink(link_path, path);
1160 g_free(link_path);
1162 if (err)
1164 report_error("symlink: %s", g_strerror(errno));
1165 return FALSE;
1168 dir_check_this(path);
1170 return TRUE;
1173 static void run_action(DirItem *item)
1175 if (can_set_run_action(item))
1176 type_set_handler_dialog(item->mime_type);
1177 else
1178 report_error(
1179 _("You can only set the run action for a "
1180 "regular file"));
1183 void open_home(gpointer data, guint action, GtkWidget *widget)
1185 filer_opendir(home_dir, NULL);
1188 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1190 gchar *path;
1192 path = g_strconcat(filer_window->sym_path,
1193 "/", item->leafname, "#", NULL);
1195 filer_change_to(filer_window, path, NULL);
1196 g_free(path);
1199 static void select_all(gpointer data, guint action, GtkWidget *widget)
1201 g_return_if_fail(window_with_focus != NULL);
1203 window_with_focus->temp_item_selected = FALSE;
1204 collection_select_all(window_with_focus->collection);
1207 static void clear_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 view_clear_selection(window_with_focus->view);
1215 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1217 g_return_if_fail(window_with_focus != NULL);
1219 window_with_focus->temp_item_selected = FALSE;
1220 collection_invert_selection(window_with_focus->collection);
1223 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1225 GtkWidget *win;
1227 win = options_show();
1229 if (win)
1231 number_of_windows++;
1232 g_signal_connect(win, "destroy",
1233 G_CALLBACK(one_less_window), NULL);
1237 static gboolean new_directory_cb(GObject *savebox,
1238 const gchar *initial, const gchar *path)
1240 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1242 report_error("mkdir: %s", g_strerror(errno));
1243 return FALSE;
1246 dir_check_this(path);
1248 if (filer_exists(window_with_focus))
1250 guchar *leaf;
1251 leaf = strrchr(path, '/');
1252 if (leaf)
1253 display_set_autoselect(window_with_focus, leaf + 1);
1256 return TRUE;
1259 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1261 g_return_if_fail(window_with_focus != NULL);
1263 savebox_show(_("Create"),
1264 make_path(window_with_focus->sym_path, _("NewDir"))->str,
1265 type_to_icon(inode_directory), new_directory_cb);
1268 static gboolean new_file_cb(GObject *savebox,
1269 const gchar *initial, const gchar *path)
1271 int fd;
1273 fd = open(path, O_CREAT | O_EXCL, 0666);
1275 if (fd == -1)
1277 report_error(_("Error creating '%s': %s"),
1278 path, g_strerror(errno));
1279 return FALSE;
1282 if (close(fd))
1283 report_error(_("Error creating '%s': %s"),
1284 path, g_strerror(errno));
1286 dir_check_this(path);
1288 if (filer_exists(window_with_focus))
1290 guchar *leaf;
1291 leaf = strrchr(path, '/');
1292 if (leaf)
1293 display_set_autoselect(window_with_focus, leaf + 1);
1296 return TRUE;
1299 static void new_file(gpointer data, guint action, GtkWidget *widget)
1301 g_return_if_fail(window_with_focus != NULL);
1303 savebox_show(_("Create"),
1304 make_path(window_with_focus->sym_path, _("NewFile"))->str,
1305 type_to_icon(text_plain),
1306 new_file_cb);
1309 static gboolean new_file_type_cb(GObject *savebox,
1310 const gchar *initial, const gchar *path)
1312 const gchar *oleaf, *leaf;
1313 gchar *templ, *templ_dname, *dest;
1314 GList *paths;
1316 /* We can work out the template path from the initial name */
1317 oleaf = g_basename(initial);
1318 templ_dname = choices_find_path_load("Templates", "");
1319 if (!templ_dname)
1321 report_error(
1322 _("Error creating file: could not find the template for %s"),
1323 oleaf);
1324 return FALSE;
1327 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1328 g_free(templ_dname);
1330 dest = g_dirname(path);
1331 leaf = g_basename(path);
1332 paths = g_list_append(NULL, templ);
1334 action_copy(paths, dest, leaf, TRUE);
1336 g_list_free(paths);
1337 g_free(dest);
1338 g_free(templ);
1340 if (filer_exists(window_with_focus))
1341 display_set_autoselect(window_with_focus, leaf);
1343 return TRUE;
1346 static void do_send_to(gchar *templ)
1348 g_return_if_fail(send_to_paths != NULL);
1350 run_with_files(templ, send_to_paths);
1353 static void new_file_type(gchar *templ)
1355 const gchar *leaf;
1356 MIME_type *type;
1358 g_return_if_fail(window_with_focus != NULL);
1360 leaf = g_basename(templ);
1361 type = type_get_type(templ);
1363 savebox_show(_("Create"),
1364 make_path(window_with_focus->sym_path, leaf)->str,
1365 type_to_icon(type),
1366 new_file_type_cb);
1369 static void customise_send_to(gpointer data)
1371 GPtrArray *path;
1372 guchar *save;
1373 GString *dirs;
1374 int i;
1376 dirs = g_string_new(NULL);
1378 path = choices_list_dirs("");
1379 for (i = 0; i < path->len; i++)
1381 guchar *old = (guchar *) path->pdata[i];
1383 g_string_append(dirs, old);
1384 g_string_append(dirs, "SendTo\n");
1386 choices_free_list(path);
1388 save = choices_find_path_save("", "SendTo", TRUE);
1389 if (save)
1390 mkdir(save, 0777);
1392 info_message(
1393 _("The `Send To' menu provides a quick way to send some files "
1394 "to an application. The applications listed are those in "
1395 "the following directories:\n\n%s\n%s\n"
1396 "The `Send To' menu may be opened by Shift+Menu clicking "
1397 "over a file.\n\n"
1398 "Advanced use:\n"
1399 "You can also create subdirectories called "
1400 "`.text_html', `.text', etc which will only be "
1401 "shown for files of that type. `.group' is shown "
1402 "only when multiple files are selected."),
1403 dirs->str,
1404 save ? _("I'll show you your SendTo directory now; you should "
1405 "symlink (Ctrl+Shift drag) any applications you want "
1406 "into it.")
1407 : _("Your CHOICESPATH variable setting prevents "
1408 "customisations - sorry."));
1410 g_string_free(dirs, TRUE);
1412 if (save)
1413 filer_opendir(save, NULL);
1416 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1417 * to the menu.
1419 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1421 gchar *searchdir;
1422 GPtrArray *paths;
1423 int i;
1425 if (subtype)
1426 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1427 else if (type)
1428 searchdir = g_strdup_printf("SendTo/.%s", type);
1429 else
1430 searchdir = g_strdup("SendTo");
1432 paths = choices_list_dirs(searchdir);
1433 g_free(searchdir);
1435 for (i = 0; i < paths->len; i++)
1437 GList *widgets = NULL;
1438 guchar *dir = (guchar *) paths->pdata[i];
1440 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1441 (CallbackFn) do_send_to,
1442 FALSE, FALSE, TRUE);
1444 if (widgets)
1445 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1446 gtk_menu_item_new());
1448 g_list_free(widgets); /* TODO: Get rid of this */
1451 choices_free_list(paths);
1454 /* Scan the SendTo dir and create and show the Send To menu.
1455 * The 'paths' list and every path in it is claimed, and will be
1456 * freed later -- don't free it yourself!
1458 static void show_send_to_menu(GList *paths, GdkEvent *event)
1460 GtkWidget *menu, *item;
1462 menu = gtk_menu_new();
1464 if (g_list_length(paths) == 1)
1466 DirItem *item;
1468 item = diritem_new("");
1469 diritem_restat(paths->data, item, NULL);
1471 add_sendto(menu,
1472 item->mime_type->media_type,
1473 item->mime_type->subtype);
1475 add_sendto(menu, item->mime_type->media_type, NULL);
1477 diritem_free(item);
1479 else
1480 add_sendto(menu, "group", NULL);
1482 add_sendto(menu, NULL, NULL);
1484 item = gtk_menu_item_new_with_label(_("Customise"));
1485 g_signal_connect_swapped(item, "activate",
1486 G_CALLBACK(customise_send_to), NULL);
1487 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1489 if (send_to_paths)
1491 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1492 g_list_free(send_to_paths);
1494 send_to_paths = paths;
1496 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1498 popup_menu = menu;
1499 show_popup_menu(menu, event, 0);
1502 static void send_to(FilerWindow *filer_window)
1504 GList *paths;
1505 GdkEvent *event;
1507 paths = filer_selected_items(filer_window);
1508 event = gtk_get_current_event();
1510 /* Eats paths */
1511 show_send_to_menu(paths, event);
1513 gdk_event_free(event);
1516 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1518 const char *argv[] = {"sh", "-c", NULL, NULL};
1519 gboolean close = action;
1521 argv[2] = o_menu_xterm.value;
1523 g_return_if_fail(window_with_focus != NULL);
1525 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1526 gtk_widget_destroy(window_with_focus->window);
1529 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1531 g_return_if_fail(window_with_focus != NULL);
1533 filer_change_to(window_with_focus, home_dir, NULL);
1536 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1538 g_return_if_fail(window_with_focus != NULL);
1540 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1541 filer_change_to(window_with_focus,
1542 window_with_focus->real_path, NULL);
1543 else
1544 delayed_error(_("This is already the canonical name "
1545 "for this directory."));
1548 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1550 g_return_if_fail(window_with_focus != NULL);
1552 filer_open_parent(window_with_focus);
1555 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1557 g_return_if_fail(window_with_focus != NULL);
1559 change_to_parent(window_with_focus);
1562 static void resize(gpointer data, guint action, GtkWidget *widget)
1564 g_return_if_fail(window_with_focus != NULL);
1566 filer_window_autosize(window_with_focus, TRUE);
1569 static void new_window(gpointer data, guint action, GtkWidget *widget)
1571 g_return_if_fail(window_with_focus != NULL);
1573 if (o_unique_filer_windows.int_value)
1575 report_error(_("You can't open a second view onto "
1576 "this directory because the `Unique Windows' option "
1577 "is turned on in the Options window."));
1579 else
1580 filer_opendir(window_with_focus->sym_path, window_with_focus);
1583 #if 0
1584 static void su_to_user(GtkWidget *dialog)
1586 char *argv[] = {
1587 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1588 GtkEntry *user;
1589 guchar *path;
1591 g_return_if_fail(dialog != NULL);
1593 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1594 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1596 g_return_if_fail(user != NULL && path != NULL);
1598 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1599 argv[3] = gtk_entry_get_text(user);
1600 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1601 argv[5] = path;
1603 if (!spawn(argv))
1604 report_error(_("fork: %s"), g_strerror(errno));
1606 g_free(argv[2]);
1607 g_free(argv[4]);
1609 gtk_widget_destroy(dialog);
1612 static void new_user(gpointer data, guint action, GtkWidget *widget)
1614 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1616 g_return_if_fail(window_with_focus != NULL);
1618 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1619 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1620 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1621 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1622 g_strdup(window_with_focus->path), g_free);
1624 vbox = gtk_vbox_new(FALSE, 4);
1625 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1626 gtk_box_pack_start(GTK_BOX(vbox),
1627 gtk_label_new(_("Browse as which user?")),
1628 TRUE, TRUE, 2);
1630 hbox = gtk_hbox_new(FALSE, 4);
1631 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1633 gtk_box_pack_start(GTK_BOX(hbox),
1634 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1636 entry = gtk_entry_new();
1637 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1638 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1639 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1640 gtk_widget_grab_focus(entry);
1641 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1642 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1643 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1645 hbox = gtk_hbox_new(TRUE, 0);
1646 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1648 button = gtk_button_new_with_label(_("OK"));
1649 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1650 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1651 gtk_window_set_default(GTK_WINDOW(dialog), button);
1652 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1653 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1655 button = gtk_button_new_with_label(_("Cancel"));
1656 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1657 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1658 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1659 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1660 GTK_OBJECT(dialog));
1662 gtk_widget_show_all(dialog);
1664 #endif
1666 static void close_window(gpointer data, guint action, GtkWidget *widget)
1668 g_return_if_fail(window_with_focus != NULL);
1670 gtk_widget_destroy(window_with_focus->window);
1673 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1675 MiniType type = (MiniType) action;
1677 g_return_if_fail(window_with_focus != NULL);
1679 /* Item needs to remain selected... */
1680 if (type == MINI_SHELL)
1681 window_with_focus->temp_item_selected = FALSE;
1683 minibuffer_show(window_with_focus, type);
1686 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1688 if (action == HELP_ABOUT)
1689 infobox_new(app_dir);
1690 else if (action == HELP_DIR)
1691 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1692 else if (action == HELP_MANUAL)
1694 gchar *manual = NULL;
1696 if (current_lang)
1698 manual = g_strconcat(app_dir, "/Help/Manual-",
1699 current_lang, ".html", NULL);
1700 if (access(manual, F_OK))
1702 g_free(manual);
1703 manual = NULL;
1707 if (!manual)
1708 manual = g_strconcat(app_dir,
1709 "/Help/Manual.html", NULL);
1711 run_by_path(manual);
1713 g_free(manual);
1715 else
1716 g_warning("Unknown help action %d\n", action);
1719 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1720 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1722 GList *items, *item;
1724 items = gtk_container_get_children(GTK_CONTAINER(menu));
1726 item = g_list_nth(items, from);
1727 while (item && n--)
1729 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1730 item = item->next;
1732 g_list_free(items);
1735 static void save_menus(void)
1737 char *menurc;
1739 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1740 if (menurc)
1742 gtk_accel_map_save(menurc);
1743 g_free(menurc);
1747 static void select_nth_item(GtkMenuShell *shell, int n)
1749 GList *items;
1750 GtkWidget *item;
1752 items = gtk_container_get_children(GTK_CONTAINER(shell));
1753 item = g_list_nth_data(items, n);
1755 g_return_if_fail(item != NULL);
1757 g_list_free(items);
1759 gtk_menu_shell_select_item(shell, item);
1762 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1764 DirItem *item;
1765 gchar *path;
1766 int n_selected;
1768 g_return_if_fail(window_with_focus != NULL);
1770 n_selected = view_count_selected(window_with_focus->view);
1772 if (n_selected < 1)
1774 const char *prompt;
1776 switch (action)
1778 case FILE_COPY_ITEM:
1779 prompt = _("Copy ... ?");
1780 break;
1781 case FILE_RENAME_ITEM:
1782 prompt = _("Rename ... ?");
1783 break;
1784 case FILE_LINK_ITEM:
1785 prompt = _("Symlink ... ?");
1786 break;
1787 case FILE_OPEN_FILE:
1788 prompt = _("Shift Open ... ?");
1789 break;
1790 case FILE_HELP:
1791 prompt = _("Help about ... ?");
1792 break;
1793 case FILE_SHOW_FILE_INFO:
1794 prompt = _("Examine ... ?");
1795 break;
1796 case FILE_RUN_ACTION:
1797 prompt = _("Set run action for ... ?");
1798 break;
1799 case FILE_SET_ICON:
1800 prompt = _("Set icon for ... ?");
1801 break;
1802 case FILE_SEND_TO:
1803 prompt = _("Send ... to ... ?");
1804 break;
1805 case FILE_DELETE:
1806 prompt = _("DELETE ... ?");
1807 break;
1808 case FILE_USAGE:
1809 prompt = _("Count the size of ... ?");
1810 break;
1811 case FILE_CHMOD_ITEMS:
1812 prompt = _("Set permissions on ... ?");
1813 break;
1814 case FILE_FIND:
1815 prompt = _("Search inside ... ?");
1816 break;
1817 case FILE_OPEN_VFS_AVFS:
1818 prompt = _("Look inside ... ?");
1819 break;
1820 default:
1821 g_warning("Unknown action!");
1822 return;
1824 filer_target_mode(window_with_focus, target_callback,
1825 GINT_TO_POINTER(action), prompt);
1826 return;
1829 switch (action)
1831 case FILE_SEND_TO:
1832 send_to(window_with_focus);
1833 return;
1834 case FILE_DELETE:
1835 delete(window_with_focus);
1836 return;
1837 case FILE_USAGE:
1838 usage(window_with_focus);
1839 return;
1840 case FILE_CHMOD_ITEMS:
1841 chmod_items(window_with_focus);
1842 return;
1843 case FILE_FIND:
1844 find(window_with_focus);
1845 return;
1846 case FILE_SHOW_FILE_INFO:
1848 GList *items;
1850 items = filer_selected_items(window_with_focus);
1851 infobox_show_list(items);
1852 g_list_foreach(items, (GFunc) g_free, NULL);
1853 g_list_free(items);
1854 return;
1856 default:
1857 break;
1860 /* All the following actions require exactly one file selected */
1862 if (n_selected > 1)
1864 report_error(_("You cannot do this to more than "
1865 "one item at a time"));
1866 return;
1869 item = filer_selected_item(window_with_focus);
1870 g_return_if_fail(item != NULL);
1871 if (!item->image)
1872 item = dir_update_item(window_with_focus->directory,
1873 item->leafname);
1875 if (!item)
1877 report_error(_("Item no longer exists!"));
1878 return;
1881 path = make_path(window_with_focus->sym_path, item->leafname)->str;
1883 switch (action)
1885 case FILE_COPY_ITEM:
1886 src_dest_action_item(path, item->image,
1887 _("Copy"), copy_cb);
1888 break;
1889 case FILE_RENAME_ITEM:
1890 src_dest_action_item(path, item->image,
1891 _("Rename"), rename_cb);
1892 break;
1893 case FILE_LINK_ITEM:
1894 src_dest_action_item(path, item->image,
1895 _("Symlink"), link_cb);
1896 break;
1897 case FILE_OPEN_FILE:
1898 filer_openitem(window_with_focus,
1899 collection_selected_item_number(
1900 window_with_focus->collection),
1901 OPEN_SAME_WINDOW | OPEN_SHIFT);
1902 break;
1903 case FILE_HELP:
1904 show_item_help(path, item);
1905 break;
1906 case FILE_RUN_ACTION:
1907 run_action(item);
1908 break;
1909 case FILE_SET_ICON:
1910 icon_set_handler_dialog(item, path);
1911 break;
1912 case FILE_OPEN_VFS_AVFS:
1913 open_vfs_avfs(window_with_focus, item);
1914 break;
1915 default:
1916 g_warning("Unknown action!");
1917 return;
1921 static void show_key_help(GtkWidget *button, gpointer data)
1923 gboolean can_change_accels;
1925 g_object_get(G_OBJECT(gtk_settings_get_default()),
1926 "gtk-can-change-accels", &can_change_accels,
1927 NULL);
1929 if (!can_change_accels)
1931 info_message(_("User-definable shortcuts are disabled by "
1932 "default in Gtk2, and you have not enabled "
1933 "them. You can turn this feature on by:\n"
1934 "1) using an XSettings manager, such as ROX-Session\n"
1935 "or\n"
1936 "2) adding this line to ~/.gtkrc-2.0:\n"
1937 "\tgtk-can-change-accels = 1"));
1938 return;
1941 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1942 "- Open the menu over a filer window,\n"
1943 "- Move the pointer over the item you want to use,\n"
1944 "- Press the key you want attached to it.\n\n"
1945 "The key will appear next to the menu item and you can just press "
1946 "that key without opening the menu in future."));
1949 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1951 GtkWidget *button, *align;
1953 g_return_val_if_fail(option == NULL, NULL);
1955 align = gtk_alignment_new(0.5, 0.5, 0, 0);
1956 button = button_new_mixed(GTK_STOCK_DIALOG_INFO,
1957 _("Set keyboard shortcuts"));
1958 gtk_container_add(GTK_CONTAINER(align), button);
1959 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1961 return g_list_append(NULL, align);