r2786: Bugfix: Closing a window using the menu didn't offer to unmount (James Bursa).
[rox-filer.git] / ROX-Filer / src / menu.c
blobb89bda0e5c48b510f283cfcc7947427fc81333c7
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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;
96 static gboolean filer_keys_need_init = TRUE;
98 static GtkWidget *popup_menu = NULL; /* Currently open menu */
100 static gint updating_menu = 0; /* Non-zero => ignore activations */
101 static GList *send_to_paths = NULL;
103 static Option o_menu_iconsize, o_menu_xterm;
105 /* Static prototypes */
107 static void save_menus(void);
108 static void menu_closed(GtkWidget *widget);
109 static void shade_file_menu_items(gboolean shaded);
110 static void savebox_show(const gchar *action, const gchar *path,
111 MaskedPixmap *image, SaveCb callback,
112 GdkDragAction dnd_action);
113 static gint save_to_file(GObject *savebox,
114 const gchar *pathname, gpointer data);
115 static gboolean action_with_leaf(ActionFn action,
116 const gchar *current, const gchar *new);
117 static gboolean link_cb(GObject *savebox,
118 const gchar *initial, const gchar *path);
119 static void select_nth_item(GtkMenuShell *shell, int n);
120 static void new_file_type(gchar *templ);
121 static void do_send_to(gchar *templ);
122 static void show_send_to_menu(GList *paths, GdkEvent *event);
123 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
125 /* Note that for most of these callbacks none of the arguments are used. */
127 static void view_type(gpointer data, guint action, GtkWidget *widget);
129 /* (action used in these three - DetailsType) */
130 static void change_size(gpointer data, guint action, GtkWidget *widget);
131 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
132 static void set_with(gpointer data, guint action, GtkWidget *widget);
134 static void set_sort(gpointer data, guint action, GtkWidget *widget);
135 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
137 static void hidden(gpointer data, guint action, GtkWidget *widget);
138 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
139 static void refresh(gpointer data, guint action, GtkWidget *widget);
141 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
143 static void select_all(gpointer data, guint action, GtkWidget *widget);
144 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
145 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
146 static void new_directory(gpointer data, guint action, GtkWidget *widget);
147 static void new_file(gpointer data, guint action, GtkWidget *widget);
148 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
150 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
151 static void open_parent(gpointer data, guint action, GtkWidget *widget);
152 static void home_directory(gpointer data, guint action, GtkWidget *widget);
153 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
154 static void new_window(gpointer data, guint action, GtkWidget *widget);
155 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
156 static void close_window(gpointer data, guint action, GtkWidget *widget);
157 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
159 /* (action used in this - MiniType) */
160 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
161 static void resize(gpointer data, guint action, GtkWidget *widget);
163 #define MENUS_NAME "menus2"
165 static GtkWidget *filer_menu; /* The popup filer menu */
166 static GtkWidget *filer_file_item; /* The File '' label */
167 static GtkWidget *filer_file_menu; /* The File '' menu */
168 static GtkWidget *file_shift_item; /* Shift Open label */
169 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
170 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
171 static GtkWidget *filer_reverse_menu; /* The Reversed item */
172 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
173 static GtkWidget *filer_new_window; /* The New Window item */
174 static GtkWidget *filer_new_menu; /* The New submenu */
175 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
177 #undef N_
178 #define N_(x) x
180 static GtkItemFactoryEntry filer_menu_def[] = {
181 {N_("Display"), NULL, NULL, 0, "<Branch>"},
182 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
183 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
184 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
185 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
186 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
187 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
188 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
189 {">", NULL, NULL, 0, "<Separator>"},
190 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
191 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
192 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
193 {">", NULL, NULL, 0, "<Separator>"},
194 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
195 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
196 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
197 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
198 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
199 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
200 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
201 {">", NULL, NULL, 0, "<Separator>"},
202 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
203 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
204 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
205 {N_("File"), NULL, NULL, 0, "<Branch>"},
206 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
207 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
208 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
209 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
210 {">", NULL, NULL, 0, "<Separator>"},
211 {">" N_("Help"), NULL, file_op, FILE_HELP, "<StockItem>", GTK_STOCK_HELP},
212 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
213 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
214 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
215 {">", NULL, NULL, 0, "<Separator>"},
216 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
217 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
218 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, "<StockItem>", GTK_STOCK_DIALOG_INFO},
219 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
220 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
221 {">", NULL, NULL, 0, "<Separator>"},
222 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
223 {N_("Select"), NULL, NULL, 0, "<Branch>"},
224 {">" N_("Select All"), NULL, select_all, 0, NULL},
225 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
226 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
227 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
228 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
229 {N_("New"), NULL, NULL, 0, "<Branch>"},
230 {">" N_("Directory"), NULL, new_directory, 0, NULL},
231 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
232 {N_("Window"), NULL, NULL, 0, "<Branch>"},
233 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
234 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
235 {">" N_("New Window"), NULL, new_window, 0, NULL},
236 {">" N_("Home Directory"), NULL, home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
237 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
238 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
239 {">" N_("Resize Window"), NULL, resize, 0, NULL},
240 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
242 {">" N_("Close Window"), NULL, close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
243 {">", NULL, NULL, 0, "<Separator>"},
244 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
245 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
246 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
247 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
248 {N_("Help"), NULL, NULL, 0, "<Branch>"},
249 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
250 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
251 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
255 #define GET_MENU_ITEM(var, menu) \
256 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
258 #define GET_SMENU_ITEM(var, menu, sub) \
259 do { \
260 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
261 var = gtk_item_factory_get_widget(item_factory, tmp); \
262 g_free(tmp); \
263 } while (0)
265 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
266 do { \
267 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
268 var = gtk_item_factory_get_widget(item_factory, tmp); \
269 g_free(tmp); \
270 } while (0)
272 /* Returns TRUE if the keys were installed (first call only) */
273 gboolean ensure_filer_menu(void)
275 GList *items;
276 guchar *tmp;
277 GtkWidget *item;
278 GtkItemFactory *item_factory;
280 if (!filer_keys_need_init)
281 return FALSE;
282 filer_keys_need_init = FALSE;
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_reverse_menu, "filer", "Display", "Reversed");
292 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
293 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
294 "Show Thumbnails");
296 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
297 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
298 filer_follow_sym = GTK_BIN(item)->child;
300 /* File '' label... */
301 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
302 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
303 g_list_free(items);
305 /* Shift Open... label */
306 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
307 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
308 g_list_free(items);
310 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
311 filer_new_window = GTK_BIN(item)->child;
313 g_signal_connect(filer_menu, "unmap_event",
314 G_CALLBACK(menu_closed), NULL);
315 g_signal_connect(filer_file_menu, "unmap_event",
316 G_CALLBACK(menu_closed), NULL);
318 g_signal_connect(filer_keys, "accel_changed",
319 G_CALLBACK(save_menus), NULL);
321 return TRUE;
324 void menu_init(void)
326 char *menurc;
328 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
329 if (menurc)
331 gtk_accel_map_load(menurc);
332 g_free(menurc);
335 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
336 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
337 option_add_saver(save_menus);
339 option_register_widget("menu-set-keys", set_keys_button);
341 filer_keys = gtk_accel_group_new();
344 /* Name is in the form "<panel>" */
345 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
346 const gchar *name, GtkAccelGroup *keys)
348 GtkItemFactory *item_factory;
349 GtkItemFactoryEntry *translated;
351 if (!keys)
353 keys = gtk_accel_group_new();
354 gtk_accel_group_lock(keys);
357 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
359 translated = translate_entries(def, n_entries);
360 gtk_item_factory_create_items(item_factory, n_entries,
361 translated, NULL);
362 free_translated_entries(translated, n_entries);
364 return item_factory;
367 /* Prevent the user from setting a short-cut on this item */
368 static void menuitem_no_shortcuts(GtkWidget *item)
370 /* XXX */
371 #if 0
372 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
374 _gtk_widget_set_accel_path(item, NULL, NULL);
375 null_g_free(&menuitem->accel_path);
376 #endif
379 /* Shade items that only work on single files */
380 static void shade_file_menu_items(gboolean shaded)
382 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
383 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
384 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
387 /* 'data' is an array of three ints:
388 * [ pointer_x, pointer_y, item_under_pointer ]
390 void position_menu(GtkMenu *menu, gint *x, gint *y,
391 gboolean *push_in, gpointer data)
393 int *pos = (int *) data;
394 GtkRequisition requisition;
395 GList *items, *next;
396 int y_shift = 0;
397 int item = pos[2];
399 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
401 while (item >= 0 && next)
403 int h = ((GtkWidget *) next->data)->requisition.height;
405 if (item > 0)
406 y_shift += h;
407 else
408 y_shift += h / 2;
410 next = next->next;
411 item--;
414 g_list_free(items);
416 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
418 *x = pos[0] - (requisition.width * 7 / 8);
419 *y = pos[1] - y_shift;
421 *x = CLAMP(*x, 0, screen_width - requisition.width);
422 *y = CLAMP(*y, 0, screen_height - requisition.height);
424 *push_in = FALSE;
427 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
428 MenuIconStyle style, CallbackFn func,
429 gboolean separator, gboolean strip_ext,
430 gboolean recurse)
432 GList *widgets = NULL;
433 DirItem *ditem;
434 DIR *dir;
435 struct dirent *ent;
436 GtkWidget *item;
437 char *dname = NULL;
439 dname = pathdup(dir_name);
441 dir = opendir(dname);
442 if (!dir)
443 goto out;
445 if (separator)
447 item = gtk_menu_item_new();
448 widgets = g_list_append(widgets, item);
449 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
452 while ((ent = readdir(dir)))
454 char *dot, *leaf;
455 gchar *fname;
457 /* Ignore hidden files */
458 if (ent->d_name[0] == '.')
459 continue;
461 /* Strip off extension, if any */
462 dot = strchr(ent->d_name, '.');
463 if (strip_ext && dot)
464 leaf = g_strndup(ent->d_name, dot - ent->d_name);
465 else
466 leaf = g_strdup(ent->d_name);
468 fname = g_strconcat(dname, "/", ent->d_name, NULL);
469 ditem = diritem_new("");
470 diritem_restat(fname, ditem, NULL);
472 if (ditem->image && style != MIS_NONE)
474 GdkPixbuf *pixbuf;
476 switch (style)
478 case MIS_LARGE:
479 pixbuf = ditem->image->pixbuf;
480 break;
481 default:
482 if (!ditem->image->sm_pixbuf)
483 pixmap_make_small(ditem->image);
484 pixbuf = ditem->image->sm_pixbuf;
485 break;
488 item = gtk_image_menu_item_new_with_label(leaf);
489 /* TODO: Find a way to allow short-cuts */
490 menuitem_no_shortcuts(item);
492 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
493 gtk_image_new_from_pixbuf(pixbuf));
495 else
496 item = gtk_menu_item_new_with_label(leaf);
498 /* If it is a directory (but NOT an AppDir) and we are
499 * recursing then set up a sub menu.
501 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
502 !(ditem->flags & ITEM_FLAG_APPDIR))
504 GtkWidget *sub;
505 GList *new_widgets;
507 sub = gtk_menu_new();
508 new_widgets = menu_from_dir(sub, fname, style, func,
509 separator, strip_ext, FALSE);
510 g_list_free(new_widgets);
511 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
513 else
514 g_signal_connect_swapped(item, "activate",
515 G_CALLBACK(func), fname);
517 diritem_free(ditem);
518 g_free(leaf);
520 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
521 g_signal_connect_swapped(item, "destroy",
522 G_CALLBACK(g_free), fname);
524 widgets = g_list_append(widgets, item);
527 closedir(dir);
528 out:
529 g_free(dname);
531 return widgets;
534 /* Scan the templates dir and create entries for the New menu */
535 static void update_new_files_menu(MenuIconStyle style)
537 static GList *widgets = NULL;
539 gchar *templ_dname = NULL;
541 if (widgets)
543 GList *next;
545 for (next = widgets; next; next = next->next)
546 gtk_widget_destroy((GtkWidget *) next->data);
548 g_list_free(widgets);
549 widgets = NULL;
552 templ_dname = choices_find_path_load("Templates", "");
553 if (templ_dname)
555 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
556 (CallbackFn) new_file_type, TRUE, TRUE,
557 FALSE);
558 g_free(templ_dname);
560 gtk_widget_show_all(filer_new_menu);
563 /* 'item' is the number of the item to appear under the pointer. */
564 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
566 int pos[3];
567 int button = 0;
568 guint32 time = 0;
570 if (event && (event->type == GDK_BUTTON_PRESS ||
571 event->type == GDK_BUTTON_RELEASE))
573 GdkEventButton *bev = (GdkEventButton *) event;
575 pos[0] = bev->x_root;
576 pos[1] = bev->y_root;
577 button = bev->button;
578 time = bev->time;
580 else if (event && event->type == GDK_KEY_PRESS)
582 GdkEventKey *kev = (GdkEventKey *) event;
584 get_pointer_xy(pos, pos + 1);
585 time = kev->time;
587 else
588 get_pointer_xy(pos, pos + 1);
590 pos[2] = item;
592 gtk_widget_show_all(menu);
593 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
594 position_menu, (gpointer) pos, button, time);
595 select_nth_item(GTK_MENU_SHELL(menu), item);
598 /* Hide the popup menu, if any */
599 void menu_popdown(void)
601 if (popup_menu)
602 gtk_menu_popdown(GTK_MENU(popup_menu));
605 static MenuIconStyle get_menu_icon_style(void)
607 MenuIconStyle mis;
608 int display;
610 mis = o_menu_iconsize.int_value;
612 switch (mis)
614 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
615 return mis;
616 default:
617 break;
620 if (mis == MIS_CURRENT && window_with_focus)
622 switch (window_with_focus->display_style)
624 case HUGE_ICONS:
625 case LARGE_ICONS:
626 return MIS_LARGE;
627 case SMALL_ICONS:
628 return MIS_SMALL;
629 default:
630 break;
634 display = o_display_size.int_value;
635 switch (display)
637 case HUGE_ICONS:
638 case LARGE_ICONS:
639 return MIS_LARGE;
640 case SMALL_ICONS:
641 return MIS_SMALL;
642 default:
643 break;
646 return MIS_SMALL;
649 /* iter->peek() is the clicked item, or NULL if none */
650 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
652 DirItem *file_item = NULL;
653 GdkModifierType state = 0;
654 int n_selected;
656 n_selected = view_count_selected(filer_window->view);
658 ensure_filer_menu();
660 updating_menu++;
662 /* Remove previous AppMenu, if any */
663 appmenu_remove();
665 window_with_focus = filer_window;
667 if (!event)
668 event = gtk_get_current_event();
670 if (event->type == GDK_BUTTON_PRESS)
671 state = ((GdkEventButton *) event)->state;
672 else if (event->type == GDK_KEY_PRESS)
673 state = ((GdkEventKey *) event)->state;
675 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
677 filer_window->temp_item_selected = TRUE;
678 view_set_selected(filer_window->view, iter, TRUE);
679 n_selected = view_count_selected(filer_window->view);
681 else
683 filer_window->temp_item_selected = FALSE;
686 /* Short-cut to the Send To menu */
687 if (state & GDK_SHIFT_MASK)
689 GList *paths;
691 updating_menu--;
693 if (n_selected == 0)
695 report_error(
696 _("You should Shift+Menu click over a file to "
697 "send it somewhere"));
698 return;
701 paths = filer_selected_items(filer_window);
703 show_send_to_menu(paths, event); /* (paths eaten) */
705 return;
709 GtkWidget *file_label, *file_menu;
710 GString *buffer;
711 DirItem *item;
713 file_label = filer_file_item;
714 file_menu = filer_file_menu;
715 gtk_check_menu_item_set_active(
716 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
717 filer_window->show_thumbs);
718 gtk_check_menu_item_set_active(
719 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
720 filer_window->show_hidden);
721 gtk_check_menu_item_set_active(
722 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
723 filer_window->sort_order != GTK_SORT_ASCENDING);
724 gtk_check_menu_item_set_active(
725 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
726 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
727 buffer = g_string_new(NULL);
729 switch (n_selected)
731 case 0:
732 g_string_assign(buffer, _("Next Click"));
733 shade_file_menu_items(FALSE);
734 break;
735 case 1:
736 item = filer_selected_item(filer_window);
737 if (!item->image)
738 dir_update_item(filer_window->directory,
739 item->leafname);
740 shade_file_menu_items(FALSE);
741 file_item = filer_selected_item(filer_window);
742 g_string_printf(buffer, "%s '%s'",
743 basetype_name(file_item),
744 g_utf8_validate(file_item->leafname,
745 -1, NULL)
746 ? file_item->leafname
747 : _("(bad utf-8)"));
748 if (!can_set_run_action(file_item))
749 menu_set_items_shaded(filer_file_menu,
750 TRUE, 6, 1);
751 break;
752 default:
753 shade_file_menu_items(TRUE);
754 g_string_printf(buffer, _("%d items"),
755 n_selected);
756 break;
758 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
759 g_string_free(buffer, TRUE);
761 menu_show_shift_action(file_shift_item, file_item,
762 n_selected == 0);
763 if (file_item)
764 appmenu_add(make_path(filer_window->sym_path,
765 file_item->leafname),
766 file_item, filer_file_menu);
769 update_new_files_menu(get_menu_icon_style());
771 gtk_widget_set_sensitive(filer_new_window,
772 !o_unique_filer_windows.int_value);
773 gtk_widget_set_sensitive(filer_follow_sym,
774 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
776 popup_menu = (state & GDK_CONTROL_MASK)
777 ? filer_file_menu
778 : filer_menu;
780 updating_menu--;
782 show_popup_menu(popup_menu, event,
783 popup_menu == filer_file_menu ? 5 : 1);
786 static void menu_closed(GtkWidget *widget)
788 if (window_with_focus == NULL || widget != popup_menu)
789 return; /* Close panel item chosen? */
791 popup_menu = NULL;
793 if (window_with_focus->temp_item_selected)
795 view_clear_selection(window_with_focus->view);
796 window_with_focus->temp_item_selected = FALSE;
800 static void target_callback(FilerWindow *filer_window,
801 ViewIter *iter,
802 gpointer action)
804 g_return_if_fail(filer_window != NULL);
806 window_with_focus = filer_window;
808 /* Don't grab the primary selection */
809 filer_window->temp_item_selected = TRUE;
811 view_wink_item(filer_window->view, iter);
812 view_select_only(filer_window->view, iter);
813 file_op(NULL, GPOINTER_TO_INT(action), NULL);
815 view_clear_selection(filer_window->view);
816 filer_window->temp_item_selected = FALSE;
819 /* Set the text of the 'Shift Open...' menu item.
820 * If icon is NULL, reset the text and also shade it, unless 'next'.
822 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
824 guchar *shift_action = NULL;
826 if (item)
828 if (item->flags & ITEM_FLAG_MOUNT_POINT)
830 if (item->flags & ITEM_FLAG_MOUNTED)
831 shift_action = N_("Unmount");
832 else
833 shift_action = N_("Open unmounted");
835 else if (item->flags & ITEM_FLAG_SYMLINK)
836 shift_action = N_("Show Target");
837 else if (item->base_type == TYPE_DIRECTORY)
838 shift_action = N_("Look Inside");
839 else if (item->base_type == TYPE_FILE)
840 shift_action = N_("Open As Text");
842 gtk_label_set_text(GTK_LABEL(menu_item),
843 shift_action ? _(shift_action)
844 : _("Shift Open"));
845 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
848 /* Actions */
850 static void view_type(gpointer data, guint action, GtkWidget *widget)
852 ViewType view_type = (ViewType) action;
854 g_return_if_fail(window_with_focus != NULL);
856 if (view_type == VIEW_TYPE_COLLECTION)
857 display_set_layout(window_with_focus,
858 window_with_focus->display_style_wanted,
859 DETAILS_NONE, FALSE);
861 filer_set_view_type(window_with_focus, (ViewType) action);
864 static void change_size(gpointer data, guint action, GtkWidget *widget)
866 g_return_if_fail(window_with_focus != NULL);
868 display_change_size(window_with_focus, action == 1);
871 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
873 g_return_if_fail(window_with_focus != NULL);
875 if (updating_menu)
876 return;
878 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
879 display_set_layout(window_with_focus,
880 window_with_focus->display_style,
881 window_with_focus->details_type, FALSE);
882 else
883 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
884 window_with_focus->details_type, FALSE);
887 static void set_with(gpointer data, guint action, GtkWidget *widget)
889 DisplayStyle size;
891 g_return_if_fail(window_with_focus != NULL);
893 size = window_with_focus->display_style_wanted;
895 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
896 display_set_layout(window_with_focus, size, action, FALSE);
899 static void set_sort(gpointer data, guint action, GtkWidget *widget)
901 if (updating_menu)
902 return;
904 g_return_if_fail(window_with_focus != NULL);
906 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
909 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
911 GtkSortType order;
913 if (updating_menu)
914 return;
916 g_return_if_fail(window_with_focus != NULL);
918 order = window_with_focus->sort_order;
919 if (order == GTK_SORT_ASCENDING)
920 order = GTK_SORT_DESCENDING;
921 else
922 order = GTK_SORT_ASCENDING;
924 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
925 order);
928 static void hidden(gpointer data, guint action, GtkWidget *widget)
930 if (updating_menu)
931 return;
933 g_return_if_fail(window_with_focus != NULL);
935 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
938 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
940 if (updating_menu)
941 return;
943 g_return_if_fail(window_with_focus != NULL);
945 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
948 static void refresh(gpointer data, guint action, GtkWidget *widget)
950 g_return_if_fail(window_with_focus != NULL);
952 full_refresh();
955 static void delete(FilerWindow *filer_window)
957 GList *paths;
958 paths = filer_selected_items(filer_window);
959 action_delete(paths);
960 destroy_glist(&paths);
963 static void usage(FilerWindow *filer_window)
965 GList *paths;
966 paths = filer_selected_items(filer_window);
967 action_usage(paths);
968 destroy_glist(&paths);
971 static void chmod_items(FilerWindow *filer_window)
973 GList *paths;
974 paths = filer_selected_items(filer_window);
975 action_chmod(paths, FALSE, NULL);
976 destroy_glist(&paths);
979 static void find(FilerWindow *filer_window)
981 GList *paths;
982 paths = filer_selected_items(filer_window);
983 action_find(paths);
984 destroy_glist(&paths);
987 /* This creates a new savebox widget, and allows the user to pick a new path
988 * for the file.
989 * Once the new path has been picked, the callback will be called with
990 * both the current and new paths.
991 * NOTE: This function unrefs 'image'!
993 static void savebox_show(const gchar *action, const gchar *path,
994 MaskedPixmap *image, SaveCb callback,
995 GdkDragAction dnd_action)
997 GtkWidget *savebox = NULL;
998 GtkWidget *check_relative = NULL;
1000 g_return_if_fail(image != NULL);
1002 savebox = gtk_savebox_new(action);
1003 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1005 if (callback == link_cb)
1007 check_relative = gtk_check_button_new_with_mnemonic(
1008 _("_Relative link"));
1009 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1010 TRUE);
1012 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1013 gtk_tooltips_set_tip(tooltips, check_relative,
1014 _("If on, the symlink will store the path from the "
1015 "symlink to the target file. Use this if the symlink "
1016 "and the target will be moved together.\n"
1017 "If off, the path from the root directory is stored - "
1018 "use this if the symlink may move but the target will "
1019 "stay put."), NULL);
1020 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1021 check_relative, FALSE, TRUE, 0);
1022 gtk_widget_show(check_relative);
1025 g_signal_connect(savebox, "save_to_file",
1026 G_CALLBACK(save_to_file), NULL);
1028 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1029 g_strdup(path), g_free);
1030 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1031 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1033 gtk_window_set_title(GTK_WINDOW(savebox), action);
1035 if (g_utf8_validate(path, -1, NULL))
1036 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1037 else
1039 gchar *u8;
1040 u8 = to_utf8(path);
1041 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1042 g_free(u8);
1044 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1045 g_object_unref(image);
1047 gtk_widget_show(savebox);
1050 static gint save_to_file(GObject *savebox,
1051 const gchar *pathname, gpointer data)
1053 SaveCb callback;
1054 const gchar *current_path;
1056 callback = g_object_get_data(savebox, "action_callback");
1057 current_path = g_object_get_data(savebox, "current_path");
1059 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1060 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1062 return callback(savebox, current_path, pathname)
1063 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1066 static gboolean copy_cb(GObject *savebox,
1067 const gchar *current, const gchar *new)
1069 return action_with_leaf(action_copy, current, new);
1072 static gboolean action_with_leaf(ActionFn action,
1073 const gchar *current, const gchar *new)
1075 const char *leaf;
1076 char *new_dir;
1077 GList *local_paths;
1079 if (new[0] != '/')
1081 report_error(_("New pathname is not absolute"));
1082 return FALSE;
1085 if (new[strlen(new) - 1] == '/')
1087 new_dir = g_strdup(new);
1088 leaf = NULL;
1090 else
1092 const gchar *slash;
1094 slash = strrchr(new, '/');
1095 new_dir = g_strndup(new, slash - new);
1096 leaf = slash + 1;
1099 local_paths = g_list_append(NULL, (gchar *) current);
1100 action(local_paths, new_dir, leaf, -1);
1101 g_list_free(local_paths);
1103 g_free(new_dir);
1105 return TRUE;
1108 /* Open a savebox to act on the selected file.
1109 * Call 'callback' later to perform the operation.
1111 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1112 const gchar *action, SaveCb callback,
1113 GdkDragAction dnd_action)
1115 g_object_ref(image);
1116 savebox_show(action, path, image, callback, dnd_action);
1119 static gboolean rename_cb(GObject *savebox,
1120 const gchar *current, const gchar *new)
1122 return action_with_leaf(action_move, current, new);
1125 static gboolean link_cb(GObject *savebox,
1126 const gchar *initial, const gchar *path)
1128 GtkToggleButton *check_relative;
1129 struct stat info;
1130 int err;
1131 gchar *link_path;
1133 check_relative = g_object_get_data(savebox, "check_relative");
1135 if (gtk_toggle_button_get_active(check_relative))
1136 link_path = get_relative_path(path, initial);
1137 else
1138 link_path = g_strdup(initial);
1140 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1142 GtkWidget *box, *button;
1143 gint ans;
1145 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1146 GTK_BUTTONS_CANCEL,
1147 _("Symlink from '%s' already exists. "
1148 "Replace it with a link to '%s'?"),
1149 path, link_path);
1151 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1153 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1154 gtk_widget_show(button);
1155 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1156 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1157 button, GTK_RESPONSE_OK);
1158 gtk_dialog_set_default_response(GTK_DIALOG(box),
1159 GTK_RESPONSE_OK);
1161 ans = gtk_dialog_run(GTK_DIALOG(box));
1162 gtk_widget_destroy(box);
1164 if (ans != GTK_RESPONSE_OK)
1166 g_free(link_path);
1167 return FALSE;
1170 unlink(path);
1173 err = symlink(link_path, path);
1174 g_free(link_path);
1176 if (err)
1178 report_error("symlink: %s", g_strerror(errno));
1179 return FALSE;
1182 dir_check_this(path);
1184 return TRUE;
1187 static void run_action(DirItem *item)
1189 if (can_set_run_action(item))
1190 type_set_handler_dialog(item->mime_type);
1191 else
1192 report_error(
1193 _("You can only set the run action for a "
1194 "regular file"));
1197 void open_home(gpointer data, guint action, GtkWidget *widget)
1199 filer_opendir(home_dir, NULL, NULL);
1202 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1204 gchar *path;
1206 path = g_strconcat(filer_window->sym_path,
1207 "/", item->leafname, "#", NULL);
1209 filer_change_to(filer_window, path, NULL);
1210 g_free(path);
1213 static void select_all(gpointer data, guint action, GtkWidget *widget)
1215 g_return_if_fail(window_with_focus != NULL);
1217 window_with_focus->temp_item_selected = FALSE;
1218 view_select_all(window_with_focus->view);
1221 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1223 g_return_if_fail(window_with_focus != NULL);
1225 window_with_focus->temp_item_selected = FALSE;
1226 view_clear_selection(window_with_focus->view);
1229 static gboolean invert_cb(ViewIter *iter, gpointer data)
1231 return !view_get_selected((ViewIface *) data, iter);
1234 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1236 g_return_if_fail(window_with_focus != NULL);
1238 window_with_focus->temp_item_selected = FALSE;
1240 view_select_if(window_with_focus->view, invert_cb,
1241 window_with_focus->view);
1244 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1246 GtkWidget *win;
1248 win = options_show();
1250 if (win)
1252 number_of_windows++;
1253 g_signal_connect(win, "destroy",
1254 G_CALLBACK(one_less_window), NULL);
1258 static gboolean new_directory_cb(GObject *savebox,
1259 const gchar *initial, const gchar *path)
1261 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1263 report_error("mkdir: %s", g_strerror(errno));
1264 return FALSE;
1267 dir_check_this(path);
1269 if (filer_exists(window_with_focus))
1271 guchar *leaf;
1272 leaf = strrchr(path, '/');
1273 if (leaf)
1274 display_set_autoselect(window_with_focus, leaf + 1);
1277 return TRUE;
1280 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1282 g_return_if_fail(window_with_focus != NULL);
1284 savebox_show(_("Create"),
1285 make_path(window_with_focus->sym_path, _("NewDir")),
1286 type_to_icon(inode_directory), new_directory_cb,
1287 GDK_ACTION_COPY);
1290 static gboolean new_file_cb(GObject *savebox,
1291 const gchar *initial, const gchar *path)
1293 int fd;
1295 fd = open(path, O_CREAT | O_EXCL, 0666);
1297 if (fd == -1)
1299 report_error(_("Error creating '%s': %s"),
1300 path, g_strerror(errno));
1301 return FALSE;
1304 if (close(fd))
1305 report_error(_("Error creating '%s': %s"),
1306 path, g_strerror(errno));
1308 dir_check_this(path);
1310 if (filer_exists(window_with_focus))
1312 guchar *leaf;
1313 leaf = strrchr(path, '/');
1314 if (leaf)
1315 display_set_autoselect(window_with_focus, leaf + 1);
1318 return TRUE;
1321 static void new_file(gpointer data, guint action, GtkWidget *widget)
1323 g_return_if_fail(window_with_focus != NULL);
1325 savebox_show(_("Create"),
1326 make_path(window_with_focus->sym_path, _("NewFile")),
1327 type_to_icon(text_plain),
1328 new_file_cb, GDK_ACTION_COPY);
1331 static gboolean new_file_type_cb(GObject *savebox,
1332 const gchar *initial, const gchar *path)
1334 const gchar *oleaf, *leaf;
1335 gchar *templ, *templ_dname, *dest;
1336 GList *paths;
1338 /* We can work out the template path from the initial name */
1339 oleaf = g_basename(initial);
1340 templ_dname = choices_find_path_load("Templates", "");
1341 if (!templ_dname)
1343 report_error(
1344 _("Error creating file: could not find the template for %s"),
1345 oleaf);
1346 return FALSE;
1349 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1350 g_free(templ_dname);
1352 dest = g_path_get_dirname(path);
1353 leaf = g_basename(path);
1354 paths = g_list_append(NULL, templ);
1356 action_copy(paths, dest, leaf, TRUE);
1358 g_list_free(paths);
1359 g_free(dest);
1360 g_free(templ);
1362 if (filer_exists(window_with_focus))
1363 display_set_autoselect(window_with_focus, leaf);
1365 return TRUE;
1368 static void do_send_to(gchar *templ)
1370 g_return_if_fail(send_to_paths != NULL);
1372 run_with_files(templ, send_to_paths);
1375 static void new_file_type(gchar *templ)
1377 const gchar *leaf;
1378 MIME_type *type;
1380 g_return_if_fail(window_with_focus != NULL);
1382 leaf = g_basename(templ);
1383 type = type_get_type(templ);
1385 savebox_show(_("Create"),
1386 make_path(window_with_focus->sym_path, leaf),
1387 type_to_icon(type),
1388 new_file_type_cb, GDK_ACTION_COPY);
1391 static void customise_send_to(gpointer data)
1393 GPtrArray *path;
1394 guchar *save;
1395 GString *dirs;
1396 int i;
1398 dirs = g_string_new(NULL);
1400 path = choices_list_dirs("");
1401 for (i = 0; i < path->len; i++)
1403 guchar *old = (guchar *) path->pdata[i];
1405 g_string_append(dirs, old);
1406 g_string_append(dirs, "SendTo\n");
1408 choices_free_list(path);
1410 save = choices_find_path_save("", "SendTo", TRUE);
1411 if (save)
1412 mkdir(save, 0777);
1414 info_message(
1415 _("The `Send To' menu provides a quick way to send some files "
1416 "to an application. The applications listed are those in "
1417 "the following directories:\n\n%s\n%s\n"
1418 "The `Send To' menu may be opened by Shift+Menu clicking "
1419 "over a file.\n\n"
1420 "Advanced use:\n"
1421 "You can also create subdirectories called "
1422 "`.text_html', `.text', etc which will only be "
1423 "shown for files of that type. `.group' is shown "
1424 "only when multiple files are selected."),
1425 dirs->str,
1426 save ? _("I'll show you your SendTo directory now; you should "
1427 "symlink (Ctrl+Shift drag) any applications you want "
1428 "into it.")
1429 : _("Your CHOICESPATH variable setting prevents "
1430 "customisations - sorry."));
1432 g_string_free(dirs, TRUE);
1434 if (save)
1435 filer_opendir(save, NULL, NULL);
1438 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1439 * to the menu.
1441 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1443 gchar *searchdir;
1444 GPtrArray *paths;
1445 int i;
1447 if (subtype)
1448 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1449 else if (type)
1450 searchdir = g_strdup_printf("SendTo/.%s", type);
1451 else
1452 searchdir = g_strdup("SendTo");
1454 paths = choices_list_dirs(searchdir);
1455 g_free(searchdir);
1457 for (i = 0; i < paths->len; i++)
1459 GList *widgets = NULL;
1460 guchar *dir = (guchar *) paths->pdata[i];
1462 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1463 (CallbackFn) do_send_to,
1464 FALSE, FALSE, TRUE);
1466 if (widgets)
1467 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1468 gtk_menu_item_new());
1470 g_list_free(widgets); /* TODO: Get rid of this */
1473 choices_free_list(paths);
1476 /* Scan the SendTo dir and create and show the Send To menu.
1477 * The 'paths' list and every path in it is claimed, and will be
1478 * freed later -- don't free it yourself!
1480 static void show_send_to_menu(GList *paths, GdkEvent *event)
1482 GtkWidget *menu, *item;
1484 menu = gtk_menu_new();
1486 if (g_list_length(paths) == 1)
1488 DirItem *item;
1490 item = diritem_new("");
1491 diritem_restat(paths->data, item, NULL);
1493 add_sendto(menu,
1494 item->mime_type->media_type,
1495 item->mime_type->subtype);
1497 add_sendto(menu, item->mime_type->media_type, NULL);
1499 diritem_free(item);
1501 else
1502 add_sendto(menu, "group", NULL);
1504 add_sendto(menu, NULL, NULL);
1506 item = gtk_menu_item_new_with_label(_("Customise"));
1507 g_signal_connect_swapped(item, "activate",
1508 G_CALLBACK(customise_send_to), NULL);
1509 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1511 if (send_to_paths)
1512 destroy_glist(&send_to_paths);
1514 send_to_paths = paths;
1516 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1518 popup_menu = menu;
1519 show_popup_menu(menu, event, 0);
1522 static void send_to(FilerWindow *filer_window)
1524 GList *paths;
1525 GdkEvent *event;
1527 paths = filer_selected_items(filer_window);
1528 event = gtk_get_current_event();
1530 /* Eats paths */
1531 show_send_to_menu(paths, event);
1533 gdk_event_free(event);
1536 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1538 const char *argv[] = {"sh", "-c", NULL, NULL};
1539 gboolean close = action;
1541 argv[2] = o_menu_xterm.value;
1543 g_return_if_fail(window_with_focus != NULL);
1545 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1546 gtk_widget_destroy(window_with_focus->window);
1549 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1551 g_return_if_fail(window_with_focus != NULL);
1553 filer_change_to(window_with_focus, home_dir, NULL);
1556 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1558 g_return_if_fail(window_with_focus != NULL);
1560 bookmarks_show_menu(window_with_focus);
1563 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1565 g_return_if_fail(window_with_focus != NULL);
1567 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1568 filer_change_to(window_with_focus,
1569 window_with_focus->real_path, NULL);
1570 else
1571 delayed_error(_("This is already the canonical name "
1572 "for this directory."));
1575 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1577 g_return_if_fail(window_with_focus != NULL);
1579 filer_open_parent(window_with_focus);
1582 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1584 g_return_if_fail(window_with_focus != NULL);
1586 change_to_parent(window_with_focus);
1589 static void resize(gpointer data, guint action, GtkWidget *widget)
1591 g_return_if_fail(window_with_focus != NULL);
1593 view_autosize(window_with_focus->view);
1596 static void new_window(gpointer data, guint action, GtkWidget *widget)
1598 g_return_if_fail(window_with_focus != NULL);
1600 if (o_unique_filer_windows.int_value)
1602 report_error(_("You can't open a second view onto "
1603 "this directory because the `Unique Windows' option "
1604 "is turned on in the Options window."));
1606 else
1607 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1610 static void close_window(gpointer data, guint action, GtkWidget *widget)
1612 g_return_if_fail(window_with_focus != NULL);
1614 if (!filer_window_delete(window_with_focus->window, NULL,
1615 window_with_focus))
1616 gtk_widget_destroy(window_with_focus->window);
1619 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1621 MiniType type = (MiniType) action;
1623 g_return_if_fail(window_with_focus != NULL);
1625 /* Item needs to remain selected... */
1626 if (type == MINI_SHELL)
1627 window_with_focus->temp_item_selected = FALSE;
1629 minibuffer_show(window_with_focus, type);
1632 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1634 if (action == HELP_ABOUT)
1635 infobox_new(app_dir);
1636 else if (action == HELP_DIR)
1637 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1638 else if (action == HELP_MANUAL)
1640 gchar *manual = NULL;
1642 if (current_lang)
1644 manual = g_strconcat(app_dir, "/Help/Manual-",
1645 current_lang, ".html", NULL);
1646 if (access(manual, F_OK))
1647 null_g_free(&manual);
1650 if (!manual)
1651 manual = g_strconcat(app_dir,
1652 "/Help/Manual.html", NULL);
1654 run_by_path(manual);
1656 g_free(manual);
1658 else
1659 g_warning("Unknown help action %d\n", action);
1662 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1663 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1665 GList *items, *item;
1667 items = gtk_container_get_children(GTK_CONTAINER(menu));
1669 item = g_list_nth(items, from);
1670 while (item && n--)
1672 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1673 item = item->next;
1675 g_list_free(items);
1678 static void save_menus(void)
1680 char *menurc;
1682 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1683 if (menurc)
1685 gtk_accel_map_save(menurc);
1686 g_free(menurc);
1690 static void select_nth_item(GtkMenuShell *shell, int n)
1692 GList *items;
1693 GtkWidget *item;
1695 items = gtk_container_get_children(GTK_CONTAINER(shell));
1696 item = g_list_nth_data(items, n);
1698 g_return_if_fail(item != NULL);
1700 g_list_free(items);
1702 gtk_menu_shell_select_item(shell, item);
1705 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1707 DirItem *item;
1708 const guchar *path;
1709 int n_selected;
1710 ViewIter iter;
1712 g_return_if_fail(window_with_focus != NULL);
1714 n_selected = view_count_selected(window_with_focus->view);
1716 if (n_selected < 1)
1718 const char *prompt;
1720 switch (action)
1722 case FILE_COPY_ITEM:
1723 prompt = _("Copy ... ?");
1724 break;
1725 case FILE_RENAME_ITEM:
1726 prompt = _("Rename ... ?");
1727 break;
1728 case FILE_LINK_ITEM:
1729 prompt = _("Symlink ... ?");
1730 break;
1731 case FILE_OPEN_FILE:
1732 prompt = _("Shift Open ... ?");
1733 break;
1734 case FILE_HELP:
1735 prompt = _("Help about ... ?");
1736 break;
1737 case FILE_SHOW_FILE_INFO:
1738 prompt = _("Examine ... ?");
1739 break;
1740 case FILE_RUN_ACTION:
1741 prompt = _("Set run action for ... ?");
1742 break;
1743 case FILE_SET_ICON:
1744 prompt = _("Set icon for ... ?");
1745 break;
1746 case FILE_SEND_TO:
1747 prompt = _("Send ... to ... ?");
1748 break;
1749 case FILE_DELETE:
1750 prompt = _("DELETE ... ?");
1751 break;
1752 case FILE_USAGE:
1753 prompt = _("Count the size of ... ?");
1754 break;
1755 case FILE_CHMOD_ITEMS:
1756 prompt = _("Set permissions on ... ?");
1757 break;
1758 case FILE_FIND:
1759 prompt = _("Search inside ... ?");
1760 break;
1761 case FILE_OPEN_VFS_AVFS:
1762 prompt = _("Look inside ... ?");
1763 break;
1764 default:
1765 g_warning("Unknown action!");
1766 return;
1768 filer_target_mode(window_with_focus, target_callback,
1769 GINT_TO_POINTER(action), prompt);
1770 return;
1773 switch (action)
1775 case FILE_SEND_TO:
1776 send_to(window_with_focus);
1777 return;
1778 case FILE_DELETE:
1779 delete(window_with_focus);
1780 return;
1781 case FILE_USAGE:
1782 usage(window_with_focus);
1783 return;
1784 case FILE_CHMOD_ITEMS:
1785 chmod_items(window_with_focus);
1786 return;
1787 case FILE_FIND:
1788 find(window_with_focus);
1789 return;
1790 case FILE_SHOW_FILE_INFO:
1792 GList *items;
1794 items = filer_selected_items(window_with_focus);
1795 infobox_show_list(items);
1796 destroy_glist(&items);
1797 return;
1799 default:
1800 break;
1803 /* All the following actions require exactly one file selected */
1805 if (n_selected > 1)
1807 report_error(_("You cannot do this to more than "
1808 "one item at a time"));
1809 return;
1812 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1814 item = iter.next(&iter);
1815 g_return_if_fail(item != NULL);
1816 /* iter may be passed to filer_openitem... */
1818 if (!item->image)
1819 item = dir_update_item(window_with_focus->directory,
1820 item->leafname);
1822 if (!item)
1824 report_error(_("Item no longer exists!"));
1825 return;
1828 path = make_path(window_with_focus->sym_path, item->leafname);
1830 switch (action)
1832 case FILE_COPY_ITEM:
1833 src_dest_action_item(path, item->image,
1834 _("Copy"), copy_cb,
1835 GDK_ACTION_COPY);
1836 break;
1837 case FILE_RENAME_ITEM:
1838 src_dest_action_item(path, item->image,
1839 _("Rename"), rename_cb,
1840 GDK_ACTION_MOVE);
1841 break;
1842 case FILE_LINK_ITEM:
1843 src_dest_action_item(path, item->image,
1844 _("Symlink"), link_cb,
1845 GDK_ACTION_LINK);
1846 break;
1847 case FILE_OPEN_FILE:
1848 filer_openitem(window_with_focus, &iter,
1849 OPEN_SAME_WINDOW | OPEN_SHIFT);
1850 break;
1851 case FILE_HELP:
1852 show_item_help(path, item);
1853 break;
1854 case FILE_RUN_ACTION:
1855 run_action(item);
1856 break;
1857 case FILE_SET_ICON:
1858 icon_set_handler_dialog(item, path);
1859 break;
1860 case FILE_OPEN_VFS_AVFS:
1861 open_vfs_avfs(window_with_focus, item);
1862 break;
1863 default:
1864 g_warning("Unknown action!");
1865 return;
1869 static void show_key_help(GtkWidget *button, gpointer data)
1871 gboolean can_change_accels;
1873 g_object_get(G_OBJECT(gtk_settings_get_default()),
1874 "gtk-can-change-accels", &can_change_accels,
1875 NULL);
1877 if (!can_change_accels)
1879 info_message(_("User-definable shortcuts are disabled by "
1880 "default in Gtk2, and you have not enabled "
1881 "them. You can turn this feature on by:\n"
1882 "1) using an XSettings manager, such as ROX-Session\n"
1883 "or\n"
1884 "2) adding this line to ~/.gtkrc-2.0:\n"
1885 "\tgtk-can-change-accels = 1"));
1886 return;
1889 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1890 "- Open the menu over a filer window,\n"
1891 "- Move the pointer over the item you want to use,\n"
1892 "- Press the key you want attached to it.\n\n"
1893 "The key will appear next to the menu item and you can just press "
1894 "that key without opening the menu in future."));
1897 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1899 GtkWidget *button, *align;
1901 g_return_val_if_fail(option == NULL, NULL);
1903 align = gtk_alignment_new(0, 0.5, 0, 0);
1904 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1905 gtk_container_add(GTK_CONTAINER(align), button);
1906 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1908 return g_list_append(NULL, align);