r2227: Changes menus for new details stuff.
[rox-filer.git] / ROX-Filer / src / menu.c
blob6f36a54340e88740529b22d796a5aa81c2a457fb
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 "display.h"
62 #include "bookmarks.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 static void view_type(gpointer data, guint action, GtkWidget *widget);
127 /* (action used in these three - DetailsType) */
128 static void change_size(gpointer data, guint action, GtkWidget *widget);
129 static void set_with(gpointer data, guint action, GtkWidget *widget);
131 static void sort_name(gpointer data, guint action, GtkWidget *widget);
132 static void sort_type(gpointer data, guint action, GtkWidget *widget);
133 static void sort_size(gpointer data, guint action, GtkWidget *widget);
134 static void sort_date(gpointer data, guint action, GtkWidget *widget);
136 static void hidden(gpointer data, guint action, GtkWidget *widget);
137 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
138 static void refresh(gpointer data, guint action, GtkWidget *widget);
140 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
142 static void select_all(gpointer data, guint action, GtkWidget *widget);
143 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
144 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
145 static void new_directory(gpointer data, guint action, GtkWidget *widget);
146 static void new_file(gpointer data, guint action, GtkWidget *widget);
147 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
149 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
150 static void open_parent(gpointer data, guint action, GtkWidget *widget);
151 static void home_directory(gpointer data, guint action, GtkWidget *widget);
152 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
153 static void new_window(gpointer data, guint action, GtkWidget *widget);
154 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
155 static void close_window(gpointer data, guint action, GtkWidget *widget);
156 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
158 /* (action used in this - MiniType) */
159 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
160 static void resize(gpointer data, guint action, GtkWidget *widget);
162 #define MENUS_NAME "menus2"
164 static GtkWidget *filer_menu; /* The popup filer menu */
165 static GtkWidget *filer_file_item; /* The File '' label */
166 static GtkWidget *filer_file_menu; /* The File '' menu */
167 static GtkWidget *file_shift_item; /* Shift Open label */
168 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
169 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
170 static GtkWidget *filer_new_window; /* The New Window item */
171 static GtkWidget *filer_new_menu; /* The New submenu */
172 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
174 #undef N_
175 #define N_(x) x
177 static GtkItemFactoryEntry filer_menu_def[] = {
178 {N_("Display"), NULL, NULL, 0, "<Branch>"},
179 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
180 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
181 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
182 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
183 {">>" N_("Type"), NULL, set_with, DETAILS_TYPE, NULL},
184 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
185 {">" N_("Details View"), NULL, view_type, VIEW_TYPE_DETAILS, NULL},
186 {">", NULL, NULL, 0, "<Separator>"},
187 {">" N_("Bigger Icons"), NULL, change_size, 1, NULL},
188 {">" N_("Smaller Icons"), NULL, change_size, -1, NULL},
189 {">", NULL, NULL, 0, "<Separator>"},
190 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
191 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
192 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
193 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
194 {">", NULL, NULL, 0, "<Separator>"},
195 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
196 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
197 {">" N_("Refresh"), NULL, refresh, 0, NULL},
198 {N_("File"), NULL, NULL, 0, "<Branch>"},
199 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
200 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
201 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
202 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
203 {">", NULL, NULL, 0, "<Separator>"},
204 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
205 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
206 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
207 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
208 {">", NULL, NULL, 0, "<Separator>"},
209 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
210 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
211 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
212 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
213 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
214 {">", NULL, NULL, 0, "<Separator>"},
215 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
216 {N_("Select"), NULL, NULL, 0, "<Branch>"},
217 {">" N_("Select All"), NULL, select_all, 0, NULL},
218 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
219 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
220 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
221 {N_("Options..."), NULL, menu_show_options, 0, NULL},
222 {N_("New"), NULL, NULL, 0, "<Branch>"},
223 {">" N_("Directory"), NULL, new_directory, 0, NULL},
224 {">" N_("Blank file"), NULL, new_file, 0, NULL},
225 {N_("Window"), NULL, NULL, 0, "<Branch>"},
226 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
227 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
228 {">" N_("New Window"), NULL, new_window, 0, NULL},
229 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
230 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, NULL},
231 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
232 {">" N_("Resize Window"), NULL, resize, 0, NULL},
233 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
235 {">" N_("Close Window"), NULL, close_window, 0, NULL},
236 {">", NULL, NULL, 0, "<Separator>"},
237 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
238 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
239 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
240 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
241 {N_("Help"), NULL, NULL, 0, "<Branch>"},
242 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
243 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, NULL},
244 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
248 #define GET_MENU_ITEM(var, menu) \
249 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
251 #define GET_SMENU_ITEM(var, menu, sub) \
252 do { \
253 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
254 var = gtk_item_factory_get_widget(item_factory, tmp); \
255 g_free(tmp); \
256 } while (0)
258 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
259 do { \
260 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
261 var = gtk_item_factory_get_widget(item_factory, tmp); \
262 g_free(tmp); \
263 } while (0)
265 void ensure_filer_menu(void)
267 GList *items;
268 guchar *tmp;
269 GtkWidget *item;
270 GtkItemFactory *item_factory;
272 if (filer_keys)
273 return;
275 filer_keys = gtk_accel_group_new();
276 item_factory = menu_create(filer_menu_def,
277 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
278 "<filer>", filer_keys);
280 GET_MENU_ITEM(filer_menu, "filer");
281 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
282 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
283 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
284 "Show Thumbnails");
286 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
287 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
288 filer_follow_sym = GTK_BIN(item)->child;
290 /* File '' label... */
291 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
292 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
293 g_list_free(items);
295 /* Shift Open... label */
296 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
297 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
298 g_list_free(items);
300 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
301 filer_new_window = GTK_BIN(item)->child;
303 g_signal_connect(filer_menu, "unmap_event",
304 G_CALLBACK(menu_closed), NULL);
305 g_signal_connect(filer_file_menu, "unmap_event",
306 G_CALLBACK(menu_closed), NULL);
308 g_signal_connect(filer_keys, "accel_changed",
309 G_CALLBACK(save_menus), NULL);
312 void menu_init(void)
314 char *menurc;
316 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
317 if (menurc)
319 gtk_accel_map_load(menurc);
320 g_free(menurc);
323 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
324 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
325 option_add_saver(save_menus);
327 option_register_widget("menu-set-keys", set_keys_button);
330 /* Name is in the form "<panel>" */
331 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
332 const gchar *name, GtkAccelGroup *keys)
334 GtkItemFactory *item_factory;
335 GtkItemFactoryEntry *translated;
337 if (!keys)
339 keys = gtk_accel_group_new();
340 gtk_accel_group_lock(keys);
343 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
345 translated = translate_entries(def, n_entries);
346 gtk_item_factory_create_items(item_factory, n_entries,
347 translated, NULL);
348 free_translated_entries(translated, n_entries);
350 return item_factory;
353 /* Prevent the user from setting a short-cut on this item */
354 static void menuitem_no_shortcuts(GtkWidget *item)
356 /* XXX */
357 #if 0
358 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
360 _gtk_widget_set_accel_path(item, NULL, NULL);
361 null_g_free(&menuitem->accel_path);
362 #endif
365 /* Shade items that only work on single files */
366 static void shade_file_menu_items(gboolean shaded)
368 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
369 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
370 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
373 /* 'data' is an array of three ints:
374 * [ pointer_x, pointer_y, item_under_pointer ]
376 void position_menu(GtkMenu *menu, gint *x, gint *y,
377 gboolean *push_in, gpointer data)
379 int *pos = (int *) data;
380 GtkRequisition requisition;
381 GList *items, *next;
382 int y_shift = 0;
383 int item = pos[2];
385 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
387 while (item >= 0 && next)
389 int h = ((GtkWidget *) next->data)->requisition.height;
391 if (item > 0)
392 y_shift += h;
393 else
394 y_shift += h / 2;
396 next = next->next;
397 item--;
400 g_list_free(items);
402 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
404 *x = pos[0] - (requisition.width * 7 / 8);
405 *y = pos[1] - y_shift;
407 *x = CLAMP(*x, 0, screen_width - requisition.width);
408 *y = CLAMP(*y, 0, screen_height - requisition.height);
410 *push_in = FALSE;
413 #if 0
414 /* Used when you menu-click on the Large or Small toolbar tools */
415 void show_style_menu(FilerWindow *filer_window,
416 GdkEventButton *event,
417 GtkWidget *menu)
419 int pos[3];
421 pos[0] = event->x_root;
422 pos[1] = event->y_root;
423 pos[2] = 0;
425 window_with_focus = filer_window;
427 popup_menu = menu;
429 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
430 (gpointer) pos, event->button, event->time);
432 #endif
434 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
435 MenuIconStyle style, CallbackFn func,
436 gboolean separator, gboolean strip_ext,
437 gboolean recurse)
439 GList *widgets = NULL;
440 DirItem *ditem;
441 DIR *dir;
442 struct dirent *ent;
443 GtkWidget *item;
444 char *dname = NULL;
446 dname = pathdup(dir_name);
448 dir = opendir(dname);
449 if (!dir)
450 goto out;
452 if (separator)
454 item = gtk_menu_item_new();
455 widgets = g_list_append(widgets, item);
456 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
459 while ((ent = readdir(dir)))
461 char *dot, *leaf;
462 GtkWidget *hbox;
463 GtkWidget *img;
464 GtkWidget *label;
465 gchar *fname;
467 /* Ignore hidden files */
468 if (ent->d_name[0] == '.')
469 continue;
471 /* Strip off extension, if any */
472 dot = strchr(ent->d_name, '.');
473 if (strip_ext && dot)
474 leaf = g_strndup(ent->d_name, dot - ent->d_name);
475 else
476 leaf = g_strdup(ent->d_name);
478 fname = g_strconcat(dname, "/", ent->d_name, NULL);
479 ditem = diritem_new("");
480 diritem_restat(fname, ditem, NULL);
482 if (ditem->image && style != MIS_NONE)
484 GdkPixbuf *pixbuf;
486 switch (style) {
487 case MIS_LARGE:
488 pixbuf = ditem->image->pixbuf;
489 break;
490 default:
491 if (!ditem->image->sm_pixbuf)
492 pixmap_make_small(ditem->image);
493 pixbuf = ditem->image->sm_pixbuf;
494 break;
497 item = gtk_menu_item_new();
498 /* TODO: Find a way to allow short-cuts */
499 menuitem_no_shortcuts(item);
501 hbox = gtk_hbox_new(FALSE, 2);
502 gtk_container_add(GTK_CONTAINER(item), hbox);
504 img = gtk_image_new_from_pixbuf(pixbuf);
506 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
508 label = gtk_label_new(leaf);
509 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
510 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
513 else
514 item = gtk_menu_item_new_with_label(leaf);
516 /* If it is a directory (but NOT an AppDir) and we are
517 * recursing then set up a sub menu.
519 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
520 !(ditem->flags & ITEM_FLAG_APPDIR))
522 GtkWidget *sub;
523 GList *new_widgets;
525 sub = gtk_menu_new();
526 new_widgets = menu_from_dir(sub, fname, style, func,
527 separator, strip_ext, FALSE);
528 g_list_free(new_widgets);
529 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
531 else
532 g_signal_connect_swapped(item, "activate",
533 G_CALLBACK(func), fname);
535 diritem_free(ditem);
536 g_free(leaf);
538 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
539 g_signal_connect_swapped(item, "destroy",
540 G_CALLBACK(g_free), fname);
542 widgets = g_list_append(widgets, item);
545 closedir(dir);
546 out:
547 g_free(dname);
549 return widgets;
552 /* Scan the templates dir and create entries for the New menu */
553 static void update_new_files_menu(MenuIconStyle style)
555 static GList *widgets = NULL;
557 gchar *templ_dname = NULL;
559 if (widgets)
561 GList *next;
563 for (next = widgets; next; next = next->next)
564 gtk_widget_destroy((GtkWidget *) next->data);
566 g_list_free(widgets);
567 widgets = NULL;
570 templ_dname = choices_find_path_load("Templates", "");
571 if (templ_dname)
573 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
574 (CallbackFn) new_file_type, TRUE, TRUE,
575 FALSE);
576 g_free(templ_dname);
578 gtk_widget_show_all(filer_new_menu);
581 /* 'item' is the number of the item to appear under the pointer. */
582 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
584 int pos[3];
585 int button = 0;
586 guint32 time = 0;
588 if (event && (event->type == GDK_BUTTON_PRESS ||
589 event->type == GDK_BUTTON_RELEASE))
591 GdkEventButton *bev = (GdkEventButton *) event;
593 pos[0] = bev->x_root;
594 pos[1] = bev->y_root;
595 button = bev->button;
596 time = bev->time;
598 else if (event && event->type == GDK_KEY_PRESS)
600 GdkEventKey *kev = (GdkEventKey *) event;
602 get_pointer_xy(pos, pos + 1);
603 time = kev->time;
605 else
606 get_pointer_xy(pos, pos + 1);
608 pos[2] = item;
610 gtk_widget_show_all(menu);
611 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
612 position_menu, (gpointer) pos, button, time);
613 select_nth_item(GTK_MENU_SHELL(menu), item);
616 /* Hide the popup menu, if any */
617 void menu_popdown(void)
619 if (popup_menu)
620 gtk_menu_popdown(GTK_MENU(popup_menu));
623 static MenuIconStyle get_menu_icon_style(void)
625 MenuIconStyle mis;
626 int display;
628 mis = o_menu_iconsize.int_value;
630 switch (mis)
632 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
633 return mis;
634 default:
635 break;
638 if (mis == MIS_CURRENT && window_with_focus)
640 switch (window_with_focus->display_style)
642 case HUGE_ICONS:
643 case LARGE_ICONS:
644 return MIS_LARGE;
645 case SMALL_ICONS:
646 return MIS_SMALL;
647 default:
648 break;
652 display = o_display_size.int_value;
653 switch (display)
655 case HUGE_ICONS:
656 case LARGE_ICONS:
657 return MIS_LARGE;
658 case SMALL_ICONS:
659 return MIS_SMALL;
660 default:
661 break;
664 return MIS_SMALL;
667 /* iter->peek() is the clicked item, or NULL if none */
668 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
670 DirItem *file_item = NULL;
671 GdkModifierType state = 0;
672 int n_selected;
674 n_selected = view_count_selected(filer_window->view);
676 ensure_filer_menu();
678 updating_menu++;
680 /* Remove previous AppMenu, if any */
681 appmenu_remove();
683 window_with_focus = filer_window;
685 if (!event)
686 event = gtk_get_current_event();
688 if (event->type == GDK_BUTTON_PRESS)
689 state = ((GdkEventButton *) event)->state;
690 else if (event->type == GDK_KEY_PRESS)
691 state = ((GdkEventKey *) event)->state;
693 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
695 filer_window->temp_item_selected = TRUE;
696 view_set_selected(filer_window->view, iter, TRUE);
697 n_selected = view_count_selected(filer_window->view);
699 else
701 filer_window->temp_item_selected = FALSE;
704 /* Short-cut to the Send To menu */
705 if (state & GDK_SHIFT_MASK)
707 GList *paths;
709 updating_menu--;
711 if (n_selected == 0)
713 report_error(
714 _("You should Shift+Menu click over a file to "
715 "send it somewhere"));
716 return;
719 paths = filer_selected_items(filer_window);
721 show_send_to_menu(paths, event); /* (paths eaten) */
723 return;
727 GtkWidget *file_label, *file_menu;
728 GString *buffer;
729 DirItem *item;
731 file_label = filer_file_item;
732 file_menu = filer_file_menu;
733 gtk_check_menu_item_set_active(
734 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
735 filer_window->show_thumbs);
736 gtk_check_menu_item_set_active(
737 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
738 filer_window->show_hidden);
739 buffer = g_string_new(NULL);
741 switch (n_selected)
743 case 0:
744 g_string_assign(buffer, _("Next Click"));
745 shade_file_menu_items(FALSE);
746 break;
747 case 1:
748 item = filer_selected_item(filer_window);
749 if (!item->image)
750 dir_update_item(filer_window->directory,
751 item->leafname);
752 shade_file_menu_items(FALSE);
753 file_item = filer_selected_item(filer_window);
754 g_string_printf(buffer, "%s '%s'",
755 basetype_name(file_item),
756 g_utf8_validate(file_item->leafname,
757 -1, NULL)
758 ? file_item->leafname
759 : _("(bad utf-8)"));
760 if (!can_set_run_action(file_item))
761 menu_set_items_shaded(filer_file_menu,
762 TRUE, 6, 1);
763 break;
764 default:
765 shade_file_menu_items(TRUE);
766 g_string_printf(buffer, _("%d items"),
767 n_selected);
768 break;
770 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
771 g_string_free(buffer, TRUE);
773 menu_show_shift_action(file_shift_item, file_item,
774 n_selected == 0);
775 if (file_item)
776 appmenu_add(make_path(filer_window->sym_path,
777 file_item->leafname),
778 file_item, filer_file_menu);
781 update_new_files_menu(get_menu_icon_style());
783 gtk_widget_set_sensitive(filer_new_window,
784 !o_unique_filer_windows.int_value);
785 gtk_widget_set_sensitive(filer_follow_sym,
786 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
788 popup_menu = (state & GDK_CONTROL_MASK)
789 ? filer_file_menu
790 : filer_menu;
792 updating_menu--;
794 show_popup_menu(popup_menu, event,
795 popup_menu == filer_file_menu ? 5 : 1);
798 static void menu_closed(GtkWidget *widget)
800 if (window_with_focus == NULL || widget != popup_menu)
801 return; /* Close panel item chosen? */
803 popup_menu = NULL;
805 if (window_with_focus->temp_item_selected)
807 view_clear_selection(window_with_focus->view);
808 window_with_focus->temp_item_selected = FALSE;
812 static void target_callback(FilerWindow *filer_window,
813 ViewIter *iter,
814 gpointer action)
816 g_return_if_fail(filer_window != NULL);
818 window_with_focus = filer_window;
820 /* Don't grab the primary selection */
821 filer_window->temp_item_selected = TRUE;
823 view_wink_item(filer_window->view, iter);
824 view_select_only(filer_window->view, iter);
825 file_op(NULL, GPOINTER_TO_INT(action), NULL);
827 view_clear_selection(filer_window->view);
828 filer_window->temp_item_selected = FALSE;
831 /* Set the text of the 'Shift Open...' menu item.
832 * If icon is NULL, reset the text and also shade it, unless 'next'.
834 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
836 guchar *shift_action = NULL;
838 if (item)
840 if (item->flags & ITEM_FLAG_MOUNT_POINT)
842 if (item->flags & ITEM_FLAG_MOUNTED)
843 shift_action = N_("Unmount");
844 else
845 shift_action = N_("Open unmounted");
847 else if (item->flags & ITEM_FLAG_SYMLINK)
848 shift_action = N_("Show Target");
849 else if (item->base_type == TYPE_DIRECTORY)
850 shift_action = N_("Look Inside");
851 else if (item->base_type == TYPE_FILE)
852 shift_action = N_("Open As Text");
854 gtk_label_set_text(GTK_LABEL(menu_item),
855 shift_action ? _(shift_action)
856 : _("Shift Open"));
857 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
860 /* Actions */
862 static void view_type(gpointer data, guint action, GtkWidget *widget)
864 ViewType view_type = (ViewType) action;
866 g_return_if_fail(window_with_focus != NULL);
868 if (view_type == VIEW_TYPE_COLLECTION)
869 display_set_layout(window_with_focus,
870 window_with_focus->display_style, DETAILS_NONE);
872 filer_set_view_type(window_with_focus, (ViewType) action);
875 static void change_size(gpointer data, guint action, GtkWidget *widget)
877 DisplayStyle size;
878 DetailsType type;
880 g_return_if_fail(window_with_focus != NULL);
882 type = window_with_focus->details_type;
883 size = window_with_focus->display_style;
885 if (action == 1)
887 if (size == SMALL_ICONS)
888 size = LARGE_ICONS;
889 else if (size == LARGE_ICONS)
890 size = HUGE_ICONS;
891 else
892 return;
894 else if (action == -1)
896 if (size == LARGE_ICONS)
897 size = SMALL_ICONS;
898 else if (size == HUGE_ICONS)
899 size = LARGE_ICONS;
900 else
901 return;
904 display_set_layout(window_with_focus, size, type);
907 static void set_with(gpointer data, guint action, GtkWidget *widget)
909 DisplayStyle size;
911 g_return_if_fail(window_with_focus != NULL);
913 size = window_with_focus->display_style;
915 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
916 display_set_layout(window_with_focus, size, action);
919 static void sort_name(gpointer data, guint action, GtkWidget *widget)
921 g_return_if_fail(window_with_focus != NULL);
923 display_set_sort_fn(window_with_focus, sort_by_name);
926 static void sort_type(gpointer data, guint action, GtkWidget *widget)
928 g_return_if_fail(window_with_focus != NULL);
930 display_set_sort_fn(window_with_focus, sort_by_type);
933 static void sort_date(gpointer data, guint action, GtkWidget *widget)
935 g_return_if_fail(window_with_focus != NULL);
937 display_set_sort_fn(window_with_focus, sort_by_date);
940 static void sort_size(gpointer data, guint action, GtkWidget *widget)
942 g_return_if_fail(window_with_focus != NULL);
944 display_set_sort_fn(window_with_focus, sort_by_size);
947 static void hidden(gpointer data, guint action, GtkWidget *widget)
949 if (updating_menu)
950 return;
952 g_return_if_fail(window_with_focus != NULL);
954 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
957 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
959 if (updating_menu)
960 return;
962 g_return_if_fail(window_with_focus != NULL);
964 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
967 static void refresh(gpointer data, guint action, GtkWidget *widget)
969 g_return_if_fail(window_with_focus != NULL);
971 full_refresh();
972 filer_update_dir(window_with_focus, TRUE);
975 static void delete(FilerWindow *filer_window)
977 GList *paths;
978 paths = filer_selected_items(filer_window);
979 action_delete(paths);
980 destroy_glist(&paths);
983 static void usage(FilerWindow *filer_window)
985 GList *paths;
986 paths = filer_selected_items(filer_window);
987 action_usage(paths);
988 destroy_glist(&paths);
991 static void chmod_items(FilerWindow *filer_window)
993 GList *paths;
994 paths = filer_selected_items(filer_window);
995 action_chmod(paths, FALSE, NULL);
996 destroy_glist(&paths);
999 static void find(FilerWindow *filer_window)
1001 GList *paths;
1002 paths = filer_selected_items(filer_window);
1003 action_find(paths);
1004 destroy_glist(&paths);
1007 /* This creates a new savebox widget, and allows the user to pick a new path
1008 * for the file.
1009 * Once the new path has been picked, the callback will be called with
1010 * both the current and new paths.
1011 * NOTE: This function unrefs 'image'!
1013 static void savebox_show(const gchar *action, const gchar *path,
1014 MaskedPixmap *image, SaveCb callback)
1016 GtkWidget *savebox = NULL;
1017 GtkWidget *check_relative = NULL;
1019 g_return_if_fail(image != NULL);
1021 savebox = gtk_savebox_new(action);
1023 if (callback == link_cb)
1025 check_relative = gtk_check_button_new_with_mnemonic(
1026 _("_Relative link"));
1027 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1028 TRUE);
1030 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1031 gtk_tooltips_set_tip(tooltips, check_relative,
1032 _("If on, the symlink will store the path from the "
1033 "symlink to the target file. Use this if the symlink "
1034 "and the target will be moved together.\n"
1035 "If off, the path from the root directory is stored - "
1036 "use this if the symlink may move but the target will "
1037 "stay put."), NULL);
1038 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1039 check_relative, FALSE, TRUE, 0);
1040 gtk_widget_show(check_relative);
1043 g_signal_connect(savebox, "save_to_file",
1044 G_CALLBACK(save_to_file), NULL);
1046 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1047 g_strdup(path), g_free);
1048 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1049 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1051 gtk_window_set_title(GTK_WINDOW(savebox), action);
1053 if (g_utf8_validate(path, -1, NULL))
1054 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1055 else
1057 gchar *u8;
1058 u8 = to_utf8(path);
1059 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1060 g_free(u8);
1062 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1063 g_object_unref(image);
1065 gtk_widget_show(savebox);
1068 static gint save_to_file(GObject *savebox,
1069 const gchar *pathname, gpointer data)
1071 SaveCb callback;
1072 const gchar *current_path;
1074 callback = g_object_get_data(savebox, "action_callback");
1075 current_path = g_object_get_data(savebox, "current_path");
1077 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1078 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1080 return callback(savebox, current_path, pathname)
1081 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1084 static gboolean copy_cb(GObject *savebox,
1085 const gchar *current, const gchar *new)
1087 return action_with_leaf(action_copy, current, new);
1090 static gboolean action_with_leaf(ActionFn action,
1091 const gchar *current, const gchar *new)
1093 const char *leaf;
1094 char *new_dir;
1095 GList *local_paths;
1097 if (new[0] != '/')
1099 report_error(_("New pathname is not absolute"));
1100 return FALSE;
1103 if (new[strlen(new) - 1] == '/')
1105 new_dir = g_strdup(new);
1106 leaf = NULL;
1108 else
1110 const gchar *slash;
1112 slash = strrchr(new, '/');
1113 new_dir = g_strndup(new, slash - new);
1114 leaf = slash + 1;
1117 local_paths = g_list_append(NULL, (gchar *) current);
1118 action(local_paths, new_dir, leaf, -1);
1119 g_list_free(local_paths);
1121 g_free(new_dir);
1123 return TRUE;
1126 /* Open a savebox to act on the selected file.
1127 * Call 'callback' later to perform the operation.
1129 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1130 const gchar *action, SaveCb callback)
1132 g_object_ref(image);
1133 savebox_show(action, path, image, callback);
1136 static gboolean rename_cb(GObject *savebox,
1137 const gchar *current, const gchar *new)
1139 return action_with_leaf(action_move, current, new);
1142 static gboolean link_cb(GObject *savebox,
1143 const gchar *initial, const gchar *path)
1145 GtkToggleButton *check_relative;
1146 struct stat info;
1147 int err;
1148 gchar *link_path;
1150 check_relative = g_object_get_data(savebox, "check_relative");
1152 if (gtk_toggle_button_get_active(check_relative))
1153 link_path = get_relative_path(path, initial);
1154 else
1155 link_path = g_strdup(initial);
1157 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1159 GtkWidget *box, *button;
1160 gint ans;
1162 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1163 GTK_BUTTONS_CANCEL,
1164 _("Symlink from '%s' already exists. "
1165 "Replace it with a link to '%s'?"),
1166 path, link_path);
1168 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1170 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1171 gtk_widget_show(button);
1172 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1173 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1174 button, GTK_RESPONSE_OK);
1175 gtk_dialog_set_default_response(GTK_DIALOG(box),
1176 GTK_RESPONSE_OK);
1178 ans = gtk_dialog_run(GTK_DIALOG(box));
1179 gtk_widget_destroy(box);
1181 if (ans != GTK_RESPONSE_OK)
1183 g_free(link_path);
1184 return FALSE;
1187 unlink(path);
1190 err = symlink(link_path, path);
1191 g_free(link_path);
1193 if (err)
1195 report_error("symlink: %s", g_strerror(errno));
1196 return FALSE;
1199 dir_check_this(path);
1201 return TRUE;
1204 static void run_action(DirItem *item)
1206 if (can_set_run_action(item))
1207 type_set_handler_dialog(item->mime_type);
1208 else
1209 report_error(
1210 _("You can only set the run action for a "
1211 "regular file"));
1214 void open_home(gpointer data, guint action, GtkWidget *widget)
1216 filer_opendir(home_dir, NULL, NULL);
1219 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1221 gchar *path;
1223 path = g_strconcat(filer_window->sym_path,
1224 "/", item->leafname, "#", NULL);
1226 filer_change_to(filer_window, path, NULL);
1227 g_free(path);
1230 static void select_all(gpointer data, guint action, GtkWidget *widget)
1232 g_return_if_fail(window_with_focus != NULL);
1234 window_with_focus->temp_item_selected = FALSE;
1235 view_select_all(window_with_focus->view);
1238 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1240 g_return_if_fail(window_with_focus != NULL);
1242 window_with_focus->temp_item_selected = FALSE;
1243 view_clear_selection(window_with_focus->view);
1246 static gboolean invert_cb(ViewIter *iter, gpointer data)
1248 return !view_get_selected((ViewIface *) data, iter);
1251 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1253 g_return_if_fail(window_with_focus != NULL);
1255 window_with_focus->temp_item_selected = FALSE;
1257 view_select_if(window_with_focus->view, invert_cb,
1258 window_with_focus->view);
1261 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1263 GtkWidget *win;
1265 win = options_show();
1267 if (win)
1269 number_of_windows++;
1270 g_signal_connect(win, "destroy",
1271 G_CALLBACK(one_less_window), NULL);
1275 static gboolean new_directory_cb(GObject *savebox,
1276 const gchar *initial, const gchar *path)
1278 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1280 report_error("mkdir: %s", g_strerror(errno));
1281 return FALSE;
1284 dir_check_this(path);
1286 if (filer_exists(window_with_focus))
1288 guchar *leaf;
1289 leaf = strrchr(path, '/');
1290 if (leaf)
1291 display_set_autoselect(window_with_focus, leaf + 1);
1294 return TRUE;
1297 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1299 g_return_if_fail(window_with_focus != NULL);
1301 savebox_show(_("Create"),
1302 make_path(window_with_focus->sym_path, _("NewDir")),
1303 type_to_icon(inode_directory), new_directory_cb);
1306 static gboolean new_file_cb(GObject *savebox,
1307 const gchar *initial, const gchar *path)
1309 int fd;
1311 fd = open(path, O_CREAT | O_EXCL, 0666);
1313 if (fd == -1)
1315 report_error(_("Error creating '%s': %s"),
1316 path, g_strerror(errno));
1317 return FALSE;
1320 if (close(fd))
1321 report_error(_("Error creating '%s': %s"),
1322 path, g_strerror(errno));
1324 dir_check_this(path);
1326 if (filer_exists(window_with_focus))
1328 guchar *leaf;
1329 leaf = strrchr(path, '/');
1330 if (leaf)
1331 display_set_autoselect(window_with_focus, leaf + 1);
1334 return TRUE;
1337 static void new_file(gpointer data, guint action, GtkWidget *widget)
1339 g_return_if_fail(window_with_focus != NULL);
1341 savebox_show(_("Create"),
1342 make_path(window_with_focus->sym_path, _("NewFile")),
1343 type_to_icon(text_plain),
1344 new_file_cb);
1347 static gboolean new_file_type_cb(GObject *savebox,
1348 const gchar *initial, const gchar *path)
1350 const gchar *oleaf, *leaf;
1351 gchar *templ, *templ_dname, *dest;
1352 GList *paths;
1354 /* We can work out the template path from the initial name */
1355 oleaf = g_basename(initial);
1356 templ_dname = choices_find_path_load("Templates", "");
1357 if (!templ_dname)
1359 report_error(
1360 _("Error creating file: could not find the template for %s"),
1361 oleaf);
1362 return FALSE;
1365 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1366 g_free(templ_dname);
1368 dest = g_path_get_dirname(path);
1369 leaf = g_basename(path);
1370 paths = g_list_append(NULL, templ);
1372 action_copy(paths, dest, leaf, TRUE);
1374 g_list_free(paths);
1375 g_free(dest);
1376 g_free(templ);
1378 if (filer_exists(window_with_focus))
1379 display_set_autoselect(window_with_focus, leaf);
1381 return TRUE;
1384 static void do_send_to(gchar *templ)
1386 g_return_if_fail(send_to_paths != NULL);
1388 run_with_files(templ, send_to_paths);
1391 static void new_file_type(gchar *templ)
1393 const gchar *leaf;
1394 MIME_type *type;
1396 g_return_if_fail(window_with_focus != NULL);
1398 leaf = g_basename(templ);
1399 type = type_get_type(templ);
1401 savebox_show(_("Create"),
1402 make_path(window_with_focus->sym_path, leaf),
1403 type_to_icon(type),
1404 new_file_type_cb);
1407 static void customise_send_to(gpointer data)
1409 GPtrArray *path;
1410 guchar *save;
1411 GString *dirs;
1412 int i;
1414 dirs = g_string_new(NULL);
1416 path = choices_list_dirs("");
1417 for (i = 0; i < path->len; i++)
1419 guchar *old = (guchar *) path->pdata[i];
1421 g_string_append(dirs, old);
1422 g_string_append(dirs, "SendTo\n");
1424 choices_free_list(path);
1426 save = choices_find_path_save("", "SendTo", TRUE);
1427 if (save)
1428 mkdir(save, 0777);
1430 info_message(
1431 _("The `Send To' menu provides a quick way to send some files "
1432 "to an application. The applications listed are those in "
1433 "the following directories:\n\n%s\n%s\n"
1434 "The `Send To' menu may be opened by Shift+Menu clicking "
1435 "over a file.\n\n"
1436 "Advanced use:\n"
1437 "You can also create subdirectories called "
1438 "`.text_html', `.text', etc which will only be "
1439 "shown for files of that type. `.group' is shown "
1440 "only when multiple files are selected."),
1441 dirs->str,
1442 save ? _("I'll show you your SendTo directory now; you should "
1443 "symlink (Ctrl+Shift drag) any applications you want "
1444 "into it.")
1445 : _("Your CHOICESPATH variable setting prevents "
1446 "customisations - sorry."));
1448 g_string_free(dirs, TRUE);
1450 if (save)
1451 filer_opendir(save, NULL, NULL);
1454 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1455 * to the menu.
1457 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1459 gchar *searchdir;
1460 GPtrArray *paths;
1461 int i;
1463 if (subtype)
1464 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1465 else if (type)
1466 searchdir = g_strdup_printf("SendTo/.%s", type);
1467 else
1468 searchdir = g_strdup("SendTo");
1470 paths = choices_list_dirs(searchdir);
1471 g_free(searchdir);
1473 for (i = 0; i < paths->len; i++)
1475 GList *widgets = NULL;
1476 guchar *dir = (guchar *) paths->pdata[i];
1478 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1479 (CallbackFn) do_send_to,
1480 FALSE, FALSE, TRUE);
1482 if (widgets)
1483 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1484 gtk_menu_item_new());
1486 g_list_free(widgets); /* TODO: Get rid of this */
1489 choices_free_list(paths);
1492 /* Scan the SendTo dir and create and show the Send To menu.
1493 * The 'paths' list and every path in it is claimed, and will be
1494 * freed later -- don't free it yourself!
1496 static void show_send_to_menu(GList *paths, GdkEvent *event)
1498 GtkWidget *menu, *item;
1500 menu = gtk_menu_new();
1502 if (g_list_length(paths) == 1)
1504 DirItem *item;
1506 item = diritem_new("");
1507 diritem_restat(paths->data, item, NULL);
1509 add_sendto(menu,
1510 item->mime_type->media_type,
1511 item->mime_type->subtype);
1513 add_sendto(menu, item->mime_type->media_type, NULL);
1515 diritem_free(item);
1517 else
1518 add_sendto(menu, "group", NULL);
1520 add_sendto(menu, NULL, NULL);
1522 item = gtk_menu_item_new_with_label(_("Customise"));
1523 g_signal_connect_swapped(item, "activate",
1524 G_CALLBACK(customise_send_to), NULL);
1525 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1527 if (send_to_paths)
1528 destroy_glist(&send_to_paths);
1530 send_to_paths = paths;
1532 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1534 popup_menu = menu;
1535 show_popup_menu(menu, event, 0);
1538 static void send_to(FilerWindow *filer_window)
1540 GList *paths;
1541 GdkEvent *event;
1543 paths = filer_selected_items(filer_window);
1544 event = gtk_get_current_event();
1546 /* Eats paths */
1547 show_send_to_menu(paths, event);
1549 gdk_event_free(event);
1552 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1554 const char *argv[] = {"sh", "-c", NULL, NULL};
1555 gboolean close = action;
1557 argv[2] = o_menu_xterm.value;
1559 g_return_if_fail(window_with_focus != NULL);
1561 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1562 gtk_widget_destroy(window_with_focus->window);
1565 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1567 g_return_if_fail(window_with_focus != NULL);
1569 filer_change_to(window_with_focus, home_dir, NULL);
1572 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1574 g_return_if_fail(window_with_focus != NULL);
1576 bookmarks_show_menu(window_with_focus);
1579 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1581 g_return_if_fail(window_with_focus != NULL);
1583 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1584 filer_change_to(window_with_focus,
1585 window_with_focus->real_path, NULL);
1586 else
1587 delayed_error(_("This is already the canonical name "
1588 "for this directory."));
1591 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1593 g_return_if_fail(window_with_focus != NULL);
1595 filer_open_parent(window_with_focus);
1598 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1600 g_return_if_fail(window_with_focus != NULL);
1602 change_to_parent(window_with_focus);
1605 static void resize(gpointer data, guint action, GtkWidget *widget)
1607 g_return_if_fail(window_with_focus != NULL);
1609 filer_window_autosize(window_with_focus);
1612 static void new_window(gpointer data, guint action, GtkWidget *widget)
1614 g_return_if_fail(window_with_focus != NULL);
1616 if (o_unique_filer_windows.int_value)
1618 report_error(_("You can't open a second view onto "
1619 "this directory because the `Unique Windows' option "
1620 "is turned on in the Options window."));
1622 else
1623 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1626 static void close_window(gpointer data, guint action, GtkWidget *widget)
1628 g_return_if_fail(window_with_focus != NULL);
1630 gtk_widget_destroy(window_with_focus->window);
1633 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1635 MiniType type = (MiniType) action;
1637 g_return_if_fail(window_with_focus != NULL);
1639 /* Item needs to remain selected... */
1640 if (type == MINI_SHELL)
1641 window_with_focus->temp_item_selected = FALSE;
1643 minibuffer_show(window_with_focus, type);
1646 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1648 if (action == HELP_ABOUT)
1649 infobox_new(app_dir);
1650 else if (action == HELP_DIR)
1651 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1652 else if (action == HELP_MANUAL)
1654 gchar *manual = NULL;
1656 if (current_lang)
1658 manual = g_strconcat(app_dir, "/Help/Manual-",
1659 current_lang, ".html", NULL);
1660 if (access(manual, F_OK))
1661 null_g_free(&manual);
1664 if (!manual)
1665 manual = g_strconcat(app_dir,
1666 "/Help/Manual.html", NULL);
1668 run_by_path(manual);
1670 g_free(manual);
1672 else
1673 g_warning("Unknown help action %d\n", action);
1676 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1677 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1679 GList *items, *item;
1681 items = gtk_container_get_children(GTK_CONTAINER(menu));
1683 item = g_list_nth(items, from);
1684 while (item && n--)
1686 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1687 item = item->next;
1689 g_list_free(items);
1692 static void save_menus(void)
1694 char *menurc;
1696 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1697 if (menurc)
1699 gtk_accel_map_save(menurc);
1700 g_free(menurc);
1704 static void select_nth_item(GtkMenuShell *shell, int n)
1706 GList *items;
1707 GtkWidget *item;
1709 items = gtk_container_get_children(GTK_CONTAINER(shell));
1710 item = g_list_nth_data(items, n);
1712 g_return_if_fail(item != NULL);
1714 g_list_free(items);
1716 gtk_menu_shell_select_item(shell, item);
1719 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1721 DirItem *item;
1722 const guchar *path;
1723 int n_selected;
1724 ViewIter iter;
1726 g_return_if_fail(window_with_focus != NULL);
1728 n_selected = view_count_selected(window_with_focus->view);
1730 if (n_selected < 1)
1732 const char *prompt;
1734 switch (action)
1736 case FILE_COPY_ITEM:
1737 prompt = _("Copy ... ?");
1738 break;
1739 case FILE_RENAME_ITEM:
1740 prompt = _("Rename ... ?");
1741 break;
1742 case FILE_LINK_ITEM:
1743 prompt = _("Symlink ... ?");
1744 break;
1745 case FILE_OPEN_FILE:
1746 prompt = _("Shift Open ... ?");
1747 break;
1748 case FILE_HELP:
1749 prompt = _("Help about ... ?");
1750 break;
1751 case FILE_SHOW_FILE_INFO:
1752 prompt = _("Examine ... ?");
1753 break;
1754 case FILE_RUN_ACTION:
1755 prompt = _("Set run action for ... ?");
1756 break;
1757 case FILE_SET_ICON:
1758 prompt = _("Set icon for ... ?");
1759 break;
1760 case FILE_SEND_TO:
1761 prompt = _("Send ... to ... ?");
1762 break;
1763 case FILE_DELETE:
1764 prompt = _("DELETE ... ?");
1765 break;
1766 case FILE_USAGE:
1767 prompt = _("Count the size of ... ?");
1768 break;
1769 case FILE_CHMOD_ITEMS:
1770 prompt = _("Set permissions on ... ?");
1771 break;
1772 case FILE_FIND:
1773 prompt = _("Search inside ... ?");
1774 break;
1775 case FILE_OPEN_VFS_AVFS:
1776 prompt = _("Look inside ... ?");
1777 break;
1778 default:
1779 g_warning("Unknown action!");
1780 return;
1782 filer_target_mode(window_with_focus, target_callback,
1783 GINT_TO_POINTER(action), prompt);
1784 return;
1787 switch (action)
1789 case FILE_SEND_TO:
1790 send_to(window_with_focus);
1791 return;
1792 case FILE_DELETE:
1793 delete(window_with_focus);
1794 return;
1795 case FILE_USAGE:
1796 usage(window_with_focus);
1797 return;
1798 case FILE_CHMOD_ITEMS:
1799 chmod_items(window_with_focus);
1800 return;
1801 case FILE_FIND:
1802 find(window_with_focus);
1803 return;
1804 case FILE_SHOW_FILE_INFO:
1806 GList *items;
1808 items = filer_selected_items(window_with_focus);
1809 infobox_show_list(items);
1810 destroy_glist(&items);
1811 return;
1813 default:
1814 break;
1817 /* All the following actions require exactly one file selected */
1819 if (n_selected > 1)
1821 report_error(_("You cannot do this to more than "
1822 "one item at a time"));
1823 return;
1826 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1828 item = iter.next(&iter);
1829 g_return_if_fail(item != NULL);
1830 /* iter may be passed to filer_openitem... */
1832 if (!item->image)
1833 item = dir_update_item(window_with_focus->directory,
1834 item->leafname);
1836 if (!item)
1838 report_error(_("Item no longer exists!"));
1839 return;
1842 path = make_path(window_with_focus->sym_path, item->leafname);
1844 switch (action)
1846 case FILE_COPY_ITEM:
1847 src_dest_action_item(path, item->image,
1848 _("Copy"), copy_cb);
1849 break;
1850 case FILE_RENAME_ITEM:
1851 src_dest_action_item(path, item->image,
1852 _("Rename"), rename_cb);
1853 break;
1854 case FILE_LINK_ITEM:
1855 src_dest_action_item(path, item->image,
1856 _("Symlink"), link_cb);
1857 break;
1858 case FILE_OPEN_FILE:
1859 filer_openitem(window_with_focus, &iter,
1860 OPEN_SAME_WINDOW | OPEN_SHIFT);
1861 break;
1862 case FILE_HELP:
1863 show_item_help(path, item);
1864 break;
1865 case FILE_RUN_ACTION:
1866 run_action(item);
1867 break;
1868 case FILE_SET_ICON:
1869 icon_set_handler_dialog(item, path);
1870 break;
1871 case FILE_OPEN_VFS_AVFS:
1872 open_vfs_avfs(window_with_focus, item);
1873 break;
1874 default:
1875 g_warning("Unknown action!");
1876 return;
1880 static void show_key_help(GtkWidget *button, gpointer data)
1882 gboolean can_change_accels;
1884 g_object_get(G_OBJECT(gtk_settings_get_default()),
1885 "gtk-can-change-accels", &can_change_accels,
1886 NULL);
1888 if (!can_change_accels)
1890 info_message(_("User-definable shortcuts are disabled by "
1891 "default in Gtk2, and you have not enabled "
1892 "them. You can turn this feature on by:\n"
1893 "1) using an XSettings manager, such as ROX-Session\n"
1894 "or\n"
1895 "2) adding this line to ~/.gtkrc-2.0:\n"
1896 "\tgtk-can-change-accels = 1"));
1897 return;
1900 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1901 "- Open the menu over a filer window,\n"
1902 "- Move the pointer over the item you want to use,\n"
1903 "- Press the key you want attached to it.\n\n"
1904 "The key will appear next to the menu item and you can just press "
1905 "that key without opening the menu in future."));
1908 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1910 GtkWidget *button, *align;
1912 g_return_val_if_fail(option == NULL, NULL);
1914 align = gtk_alignment_new(0, 0.5, 0, 0);
1915 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1916 gtk_container_add(GTK_CONTAINER(align), button);
1917 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1919 return g_list_append(NULL, align);