r3665: Only examine items that are actually displayed, not all items in a directory.
[rox-filer.git] / ROX-Filer / src / menu.c
blobc8302ee21a176ee3c9e44ac20741369a857703de
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"
63 #include "panel.h"
65 typedef enum {
66 FILE_COPY_ITEM,
67 FILE_RENAME_ITEM,
68 FILE_LINK_ITEM,
69 FILE_OPEN_FILE,
70 FILE_PROPERTIES,
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 FILE_SET_TYPE,
80 } FileOp;
82 typedef void (*ActionFn)(GList *paths,
83 const char *dest_dir, const char *leaf, int quiet);
84 typedef void MenuCallback(GtkWidget *widget, gpointer data);
86 typedef gboolean (*SaveCb)(GObject *savebox,
87 const gchar *current, const gchar *new);
89 GtkAccelGroup *filer_keys = NULL;
90 static gboolean filer_keys_need_init = TRUE;
92 static GtkWidget *popup_menu = NULL; /* Currently open menu */
94 static gint updating_menu = 0; /* Non-zero => ignore activations */
95 static GList *send_to_paths = NULL;
97 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
99 /* Static prototypes */
101 static void save_menus(void);
102 static void menu_closed(GtkWidget *widget);
103 static void shade_file_menu_items(gboolean shaded);
104 static void savebox_show(const gchar *action, const gchar *path,
105 MaskedPixmap *image, SaveCb callback,
106 GdkDragAction dnd_action);
107 static gint save_to_file(GObject *savebox,
108 const gchar *pathname, gpointer data);
109 static gboolean action_with_leaf(ActionFn action,
110 const gchar *current, const gchar *new);
111 static gboolean link_cb(GObject *savebox,
112 const gchar *initial, const gchar *path);
113 static void select_nth_item(GtkMenuShell *shell, int n);
114 static void new_file_type(gchar *templ);
115 static void do_send_to(gchar *templ);
116 static void show_send_to_menu(GList *paths, GdkEvent *event);
117 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
119 /* Note that for most of these callbacks none of the arguments are used. */
121 static void view_type(gpointer data, guint action, GtkWidget *widget);
123 /* (action used in these three - DetailsType) */
124 static void change_size(gpointer data, guint action, GtkWidget *widget);
125 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
126 static void set_with(gpointer data, guint action, GtkWidget *widget);
128 static void set_sort(gpointer data, guint action, GtkWidget *widget);
129 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
131 static void hidden(gpointer data, guint action, GtkWidget *widget);
132 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
133 static void refresh(gpointer data, guint action, GtkWidget *widget);
134 static void save_settings(gpointer data, guint action, GtkWidget *widget);
136 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
138 static void select_all(gpointer data, guint action, GtkWidget *widget);
139 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
140 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
141 static void new_directory(gpointer data, guint action, GtkWidget *widget);
142 static void new_file(gpointer data, guint action, GtkWidget *widget);
143 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
145 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
146 static void open_parent(gpointer data, guint action, GtkWidget *widget);
147 static void home_directory(gpointer data, guint action, GtkWidget *widget);
148 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
149 static void new_window(gpointer data, guint action, GtkWidget *widget);
150 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
151 static void close_window(gpointer data, guint action, GtkWidget *widget);
152 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
154 /* (action used in this - MiniType) */
155 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
156 static void resize(gpointer data, guint action, GtkWidget *widget);
158 #define MENUS_NAME "menus2"
160 static GtkWidget *filer_menu; /* The popup filer menu */
161 static GtkWidget *filer_file_item; /* The File '' label */
162 static GtkWidget *filer_file_menu; /* The File '' menu */
163 static GtkWidget *file_shift_item; /* Shift Open label */
164 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
165 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
166 static GtkWidget *filer_reverse_menu; /* The Reversed item */
167 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
168 static GtkWidget *filer_new_window; /* The New Window item */
169 static GtkWidget *filer_new_menu; /* The New submenu */
170 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
172 #undef N_
173 #define N_(x) x
175 static GtkItemFactoryEntry filer_menu_def[] = {
176 {N_("Display"), NULL, NULL, 0, "<Branch>"},
177 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
178 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
179 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
180 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
181 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
182 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
183 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
184 {">", NULL, NULL, 0, "<Separator>"},
185 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
186 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
187 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
188 {">", NULL, NULL, 0, "<Separator>"},
189 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
190 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
191 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
192 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
193 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
194 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
195 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
196 {">", NULL, NULL, 0, "<Separator>"},
197 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
198 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
199 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
200 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
201 {">" N_("Save Display Settings..."), NULL, save_settings, 0, NULL},
202 {N_("File"), NULL, NULL, 0, "<Branch>"},
203 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
204 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
205 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
206 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
207 {">", NULL, NULL, 0, "<Separator>"},
208 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
209 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
210 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
211 {">", NULL, NULL, 0, "<Separator>"},
212 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
213 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
214 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
215 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
216 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
217 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
218 {">", NULL, NULL, 0, "<Separator>"},
219 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
220 {N_("Select"), NULL, NULL, 0, "<Branch>"},
221 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
222 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
223 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
224 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
225 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
226 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
227 {N_("New"), NULL, NULL, 0, "<Branch>"},
228 {">" N_("Directory"), NULL, new_directory, 0, NULL},
229 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
230 {N_("Window"), NULL, NULL, 0, "<Branch>"},
231 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
232 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
233 {">" N_("New Window"), NULL, new_window, 0, NULL},
234 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
235 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
236 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
237 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
238 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
240 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
241 {">", NULL, NULL, 0, "<Separator>"},
242 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
243 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
244 {">" N_("Xterm Here"), "grave", xterm_here, FALSE, NULL},
245 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
246 {N_("Help"), NULL, NULL, 0, "<Branch>"},
247 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
248 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
249 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
253 #define GET_MENU_ITEM(var, menu) \
254 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
256 #define GET_SMENU_ITEM(var, menu, sub) \
257 do { \
258 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
259 var = gtk_item_factory_get_widget(item_factory, tmp); \
260 g_free(tmp); \
261 } while (0)
263 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
264 do { \
265 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
266 var = gtk_item_factory_get_widget(item_factory, tmp); \
267 g_free(tmp); \
268 } while (0)
270 /* Returns TRUE if the keys were installed (first call only) */
271 gboolean ensure_filer_menu(void)
273 GList *items;
274 guchar *tmp;
275 GtkWidget *item;
276 GtkItemFactory *item_factory;
278 if (!filer_keys_need_init)
279 return FALSE;
280 filer_keys_need_init = FALSE;
282 item_factory = menu_create(filer_menu_def,
283 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
284 "<filer>", filer_keys);
286 GET_MENU_ITEM(filer_menu, "filer");
287 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
288 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
289 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
290 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
291 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
292 "Show Thumbnails");
294 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
295 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
296 filer_follow_sym = GTK_BIN(item)->child;
298 /* File '' label... */
299 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
300 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
301 g_list_free(items);
303 /* Shift Open... label */
304 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
305 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
306 g_list_free(items);
308 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
309 filer_new_window = GTK_BIN(item)->child;
311 g_signal_connect(filer_menu, "unmap_event",
312 G_CALLBACK(menu_closed), NULL);
313 g_signal_connect(filer_file_menu, "unmap_event",
314 G_CALLBACK(menu_closed), NULL);
316 g_signal_connect(filer_keys, "accel_changed",
317 G_CALLBACK(save_menus), NULL);
319 return TRUE;
322 void menu_init(void)
324 char *menurc;
326 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
327 if (menurc)
329 gtk_accel_map_load(menurc);
330 g_free(menurc);
333 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
334 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
335 option_add_int(&o_menu_quick, "menu_quick", FALSE);
336 option_add_saver(save_menus);
338 option_register_widget("menu-set-keys", set_keys_button);
340 filer_keys = gtk_accel_group_new();
343 /* Name is in the form "<panel>" */
344 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
345 const gchar *name, GtkAccelGroup *keys)
347 GtkItemFactory *item_factory;
348 GtkItemFactoryEntry *translated;
350 if (!keys)
352 keys = gtk_accel_group_new();
353 gtk_accel_group_lock(keys);
356 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
358 translated = translate_entries(def, n_entries);
359 gtk_item_factory_create_items(item_factory, n_entries,
360 translated, NULL);
361 free_translated_entries(translated, n_entries);
363 return item_factory;
366 /* Prevent the user from setting a short-cut on this item */
367 static void menuitem_no_shortcuts(GtkWidget *item)
369 /* XXX */
370 #if 0
371 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
373 _gtk_widget_set_accel_path(item, NULL, NULL);
374 null_g_free(&menuitem->accel_path);
375 #endif
378 /* Shade items that only work on single files */
379 static void shade_file_menu_items(gboolean shaded)
381 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
382 menu_set_items_shaded(filer_file_menu, shaded, 5, 2);
383 menu_set_items_shaded(filer_file_menu, shaded, 9, 2);
386 /* 'data' is an array of three ints:
387 * [ pointer_x, pointer_y, item_under_pointer ]
389 void position_menu(GtkMenu *menu, gint *x, gint *y,
390 gboolean *push_in, gpointer data)
392 int *pos = (int *) data;
393 GtkRequisition requisition;
394 GList *items, *next;
395 int y_shift = 0;
396 int item = pos[2];
398 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
400 while (item >= 0 && next)
402 int h = ((GtkWidget *) next->data)->requisition.height;
404 if (item > 0)
405 y_shift += h;
406 else
407 y_shift += h / 2;
409 next = next->next;
410 item--;
413 g_list_free(items);
415 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
417 *x = pos[0] - (requisition.width * 7 / 8);
418 *y = pos[1] - y_shift;
420 *x = CLAMP(*x, 0, screen_width - requisition.width);
421 *y = CLAMP(*y, 0, screen_height - requisition.height);
423 *push_in = FALSE;
426 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
427 MenuIconStyle style)
429 GtkWidget *item;
431 if (style != MIS_NONE && di_image(ditem))
433 GdkPixbuf *pixbuf;
434 MaskedPixmap *image;
436 image = di_image(ditem);
438 switch (style)
440 case MIS_LARGE:
441 pixbuf = image->pixbuf;
442 break;
443 default:
444 if (!image->sm_pixbuf)
445 pixmap_make_small(image);
446 pixbuf = image->sm_pixbuf;
447 break;
450 item = gtk_image_menu_item_new_with_label(label);
451 /* TODO: Find a way to allow short-cuts */
452 menuitem_no_shortcuts(item);
454 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
455 gtk_image_new_from_pixbuf(pixbuf));
456 gtk_widget_show_all(item);
458 else
459 item = gtk_menu_item_new_with_label(label);
461 return item;
464 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
465 MenuIconStyle style, CallbackFn func,
466 gboolean separator, gboolean strip_ext,
467 gboolean recurse)
469 GList *widgets = NULL;
470 DirItem *ditem;
471 int i;
472 GtkWidget *item;
473 char *dname = NULL;
474 GPtrArray *names;
476 dname = pathdup(dir_name);
478 names = list_dir(dname);
479 if (!names)
480 goto out;
482 if (separator)
484 item = gtk_menu_item_new();
485 widgets = g_list_append(widgets, item);
486 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
489 for (i = 0; i < names->len; i++)
491 char *leaf = names->pdata[i];
492 gchar *fname;
494 fname = g_strconcat(dname, "/", leaf, NULL);
496 /* Strip off extension, if any */
497 if (strip_ext)
499 char *dot;
500 dot = strchr(leaf, '.');
501 if (dot)
502 *dot = '\0';
505 ditem = diritem_new("");
506 diritem_restat(fname, ditem, NULL);
508 item = make_send_to_item(ditem, leaf, style);
510 g_free(leaf);
512 /* If it is a directory (but NOT an AppDir) and we are
513 * recursing then set up a sub menu.
515 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
516 !(ditem->flags & ITEM_FLAG_APPDIR))
518 GtkWidget *sub;
519 GList *new_widgets;
521 sub = gtk_menu_new();
522 new_widgets = menu_from_dir(sub, fname, style, func,
523 separator, strip_ext, FALSE);
524 g_list_free(new_widgets);
525 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
527 else
528 g_signal_connect_swapped(item, "activate",
529 G_CALLBACK(func), fname);
531 diritem_free(ditem);
533 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
534 g_signal_connect_swapped(item, "destroy",
535 G_CALLBACK(g_free), fname);
537 widgets = g_list_append(widgets, item);
540 g_ptr_array_free(names, TRUE);
541 out:
542 g_free(dname);
544 return widgets;
547 /* Scan the templates dir and create entries for the New menu */
548 static void update_new_files_menu(MenuIconStyle style)
550 static GList *widgets = NULL;
552 gchar *templ_dname = NULL;
554 if (widgets)
556 GList *next;
558 for (next = widgets; next; next = next->next)
559 gtk_widget_destroy((GtkWidget *) next->data);
561 g_list_free(widgets);
562 widgets = NULL;
565 templ_dname = choices_find_path_load("Templates", "");
566 if (templ_dname)
568 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
569 (CallbackFn) new_file_type, TRUE, TRUE,
570 FALSE);
571 g_free(templ_dname);
573 gtk_widget_show_all(filer_new_menu);
576 /* 'item' is the number of the item to appear under the pointer. */
577 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
579 int pos[3];
580 int button = 0;
581 guint32 time = 0;
583 if (event && (event->type == GDK_BUTTON_PRESS ||
584 event->type == GDK_BUTTON_RELEASE))
586 GdkEventButton *bev = (GdkEventButton *) event;
588 pos[0] = bev->x_root;
589 pos[1] = bev->y_root;
590 button = bev->button;
591 time = bev->time;
593 else if (event && event->type == GDK_KEY_PRESS)
595 GdkEventKey *kev = (GdkEventKey *) event;
597 get_pointer_xy(pos, pos + 1);
598 time = kev->time;
600 else
601 get_pointer_xy(pos, pos + 1);
603 pos[2] = item;
605 gtk_widget_show_all(menu);
606 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
607 position_menu, (gpointer) pos, button, time);
608 select_nth_item(GTK_MENU_SHELL(menu), item);
611 /* Hide the popup menu, if any */
612 void menu_popdown(void)
614 if (popup_menu)
615 gtk_menu_popdown(GTK_MENU(popup_menu));
618 static MenuIconStyle get_menu_icon_style(void)
620 MenuIconStyle mis;
621 int display;
623 mis = o_menu_iconsize.int_value;
625 switch (mis)
627 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
628 return mis;
629 default:
630 break;
633 if (mis == MIS_CURRENT && window_with_focus)
635 switch (window_with_focus->display_style)
637 case HUGE_ICONS:
638 case LARGE_ICONS:
639 return MIS_LARGE;
640 case SMALL_ICONS:
641 return MIS_SMALL;
642 default:
643 break;
647 display = o_display_size.int_value;
648 switch (display)
650 case HUGE_ICONS:
651 case LARGE_ICONS:
652 return MIS_LARGE;
653 case SMALL_ICONS:
654 return MIS_SMALL;
655 default:
656 break;
659 return MIS_SMALL;
662 /* iter->peek() is the clicked item, or NULL if none */
663 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
665 DirItem *file_item = NULL;
666 GdkModifierType state = 0;
667 int n_selected;
668 int n_added = 0;
670 n_selected = view_count_selected(filer_window->view);
672 ensure_filer_menu();
674 updating_menu++;
676 /* Remove previous AppMenu, if any */
677 appmenu_remove();
679 window_with_focus = filer_window;
681 if (!event)
682 event = gtk_get_current_event();
684 if (event->type == GDK_BUTTON_PRESS)
685 state = ((GdkEventButton *) event)->state;
686 else if (event->type == GDK_KEY_PRESS)
687 state = ((GdkEventKey *) event)->state;
689 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
691 filer_window->temp_item_selected = TRUE;
692 view_set_selected(filer_window->view, iter, TRUE);
693 n_selected = view_count_selected(filer_window->view);
695 else
697 filer_window->temp_item_selected = FALSE;
700 /* Short-cut to the Send To menu */
701 if (state & GDK_SHIFT_MASK)
703 GList *paths;
705 updating_menu--;
707 if (n_selected == 0)
709 report_error(
710 _("You should Shift+Menu click over a file to "
711 "send it somewhere"));
712 return;
715 paths = filer_selected_items(filer_window);
717 show_send_to_menu(paths, event); /* (paths eaten) */
719 return;
723 GtkWidget *file_label, *file_menu;
724 GString *buffer;
725 DirItem *item;
727 file_label = filer_file_item;
728 file_menu = filer_file_menu;
729 gtk_check_menu_item_set_active(
730 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
731 filer_window->show_thumbs);
732 gtk_check_menu_item_set_active(
733 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
734 filer_window->show_hidden);
735 gtk_check_menu_item_set_active(
736 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
737 filer_window->sort_order != GTK_SORT_ASCENDING);
738 gtk_check_menu_item_set_active(
739 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
740 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
741 buffer = g_string_new(NULL);
743 switch (n_selected)
745 case 0:
746 g_string_assign(buffer, _("Next Click"));
747 shade_file_menu_items(FALSE);
748 break;
749 case 1:
750 item = filer_selected_item(filer_window);
751 if (item->base_type == TYPE_UNKNOWN)
752 dir_update_item(filer_window->directory,
753 item->leafname);
754 shade_file_menu_items(FALSE);
755 file_item = filer_selected_item(filer_window);
756 g_string_printf(buffer, _("%s '%s'"),
757 basetype_name(file_item),
758 g_utf8_validate(file_item->leafname,
759 -1, NULL)
760 ? file_item->leafname
761 : _("(bad utf-8)"));
762 if (!can_set_run_action(file_item))
763 menu_set_items_shaded(filer_file_menu,
764 TRUE, 9, 1);
765 break;
766 default:
767 shade_file_menu_items(TRUE);
768 g_string_printf(buffer, _("%d items"),
769 n_selected);
770 break;
772 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
773 g_string_free(buffer, TRUE);
775 menu_show_shift_action(file_shift_item, file_item,
776 n_selected == 0);
777 if (file_item)
778 n_added = appmenu_add(make_path(filer_window->sym_path,
779 file_item->leafname),
780 file_item, filer_file_menu);
783 update_new_files_menu(get_menu_icon_style());
785 gtk_widget_set_sensitive(filer_new_window,
786 !o_unique_filer_windows.int_value);
787 gtk_widget_set_sensitive(filer_follow_sym,
788 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
790 if (n_selected && o_menu_quick.int_value)
791 popup_menu = (state & GDK_CONTROL_MASK)
792 ? filer_menu
793 : filer_file_menu;
794 else
795 popup_menu = (state & GDK_CONTROL_MASK)
796 ? filer_file_menu
797 : filer_menu;
799 updating_menu--;
801 show_popup_menu(popup_menu, event,
802 popup_menu == filer_file_menu ? n_added : 1);
805 static void menu_closed(GtkWidget *widget)
807 if (window_with_focus == NULL || widget != popup_menu)
808 return; /* Close panel item chosen? */
810 popup_menu = NULL;
812 if (window_with_focus->temp_item_selected)
814 view_clear_selection(window_with_focus->view);
815 window_with_focus->temp_item_selected = FALSE;
818 appmenu_remove();
821 static void target_callback(FilerWindow *filer_window,
822 ViewIter *iter,
823 gpointer action)
825 g_return_if_fail(filer_window != NULL);
827 window_with_focus = filer_window;
829 /* Don't grab the primary selection */
830 filer_window->temp_item_selected = TRUE;
832 view_wink_item(filer_window->view, iter);
833 view_select_only(filer_window->view, iter);
834 file_op(NULL, GPOINTER_TO_INT(action), NULL);
836 view_clear_selection(filer_window->view);
837 filer_window->temp_item_selected = FALSE;
840 /* Set the text of the 'Shift Open...' menu item.
841 * If icon is NULL, reset the text and also shade it, unless 'next'.
843 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
845 guchar *shift_action = NULL;
847 if (item)
849 if (item->flags & ITEM_FLAG_MOUNT_POINT)
851 if (item->flags & ITEM_FLAG_MOUNTED)
852 shift_action = N_("Unmount");
853 else
854 shift_action = N_("Open unmounted");
856 else if (item->flags & ITEM_FLAG_SYMLINK)
857 shift_action = N_("Show Target");
858 else if (item->base_type == TYPE_DIRECTORY)
859 shift_action = N_("Look Inside");
860 else if (item->base_type == TYPE_FILE)
861 shift_action = N_("Open As Text");
863 gtk_label_set_text(GTK_LABEL(menu_item),
864 shift_action ? _(shift_action)
865 : _("Shift Open"));
866 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
869 /* Actions */
871 static void view_type(gpointer data, guint action, GtkWidget *widget)
873 ViewType view_type = (ViewType) action;
875 g_return_if_fail(window_with_focus != NULL);
877 if (view_type == VIEW_TYPE_COLLECTION)
878 display_set_layout(window_with_focus,
879 window_with_focus->display_style_wanted,
880 DETAILS_NONE, FALSE);
882 filer_set_view_type(window_with_focus, (ViewType) action);
885 static void change_size(gpointer data, guint action, GtkWidget *widget)
887 g_return_if_fail(window_with_focus != NULL);
889 display_change_size(window_with_focus, action == 1);
892 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
894 g_return_if_fail(window_with_focus != NULL);
896 if (updating_menu)
897 return;
899 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
900 display_set_layout(window_with_focus,
901 window_with_focus->display_style,
902 window_with_focus->details_type, FALSE);
903 else
904 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
905 window_with_focus->details_type, FALSE);
908 static void set_with(gpointer data, guint action, GtkWidget *widget)
910 DisplayStyle size;
912 g_return_if_fail(window_with_focus != NULL);
914 size = window_with_focus->display_style_wanted;
916 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
917 display_set_layout(window_with_focus, size, action, FALSE);
920 static void set_sort(gpointer data, guint action, GtkWidget *widget)
922 if (updating_menu)
923 return;
925 g_return_if_fail(window_with_focus != NULL);
927 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
930 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
932 GtkSortType order;
934 if (updating_menu)
935 return;
937 g_return_if_fail(window_with_focus != NULL);
939 order = window_with_focus->sort_order;
940 if (order == GTK_SORT_ASCENDING)
941 order = GTK_SORT_DESCENDING;
942 else
943 order = GTK_SORT_ASCENDING;
945 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
946 order);
949 static void hidden(gpointer data, guint action, GtkWidget *widget)
951 if (updating_menu)
952 return;
954 g_return_if_fail(window_with_focus != NULL);
956 display_set_hidden(window_with_focus,
957 !window_with_focus->show_hidden);
960 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
962 if (updating_menu)
963 return;
965 g_return_if_fail(window_with_focus != NULL);
967 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
970 static void refresh(gpointer data, guint action, GtkWidget *widget)
972 g_return_if_fail(window_with_focus != NULL);
974 filer_refresh(window_with_focus);
977 static void save_settings(gpointer data, guint action, GtkWidget *widget)
979 g_return_if_fail(window_with_focus != NULL);
981 filer_save_settings(window_with_focus);
984 static void delete(FilerWindow *filer_window)
986 GList *paths;
987 paths = filer_selected_items(filer_window);
988 action_delete(paths);
989 destroy_glist(&paths);
992 static void usage(FilerWindow *filer_window)
994 GList *paths;
995 paths = filer_selected_items(filer_window);
996 action_usage(paths);
997 destroy_glist(&paths);
1000 static void chmod_items(FilerWindow *filer_window)
1002 GList *paths;
1003 paths = filer_selected_items(filer_window);
1004 action_chmod(paths, FALSE, NULL);
1005 destroy_glist(&paths);
1008 static void set_type_items(FilerWindow *filer_window)
1010 GList *paths;
1011 paths = filer_selected_items(filer_window);
1012 action_settype(paths, FALSE, NULL);
1013 destroy_glist(&paths);
1016 static void find(FilerWindow *filer_window)
1018 GList *paths;
1019 paths = filer_selected_items(filer_window);
1020 action_find(paths);
1021 destroy_glist(&paths);
1024 /* This creates a new savebox widget, and allows the user to pick a new path
1025 * for the file.
1026 * Once the new path has been picked, the callback will be called with
1027 * both the current and new paths.
1028 * NOTE: This function unrefs 'image'!
1030 static void savebox_show(const gchar *action, const gchar *path,
1031 MaskedPixmap *image, SaveCb callback,
1032 GdkDragAction dnd_action)
1034 GtkWidget *savebox = NULL;
1035 GtkWidget *check_relative = NULL;
1037 g_return_if_fail(image != NULL);
1039 savebox = gtk_savebox_new(action);
1040 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1042 if (callback == link_cb)
1044 check_relative = gtk_check_button_new_with_mnemonic(
1045 _("_Relative link"));
1046 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1047 TRUE);
1049 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1050 gtk_tooltips_set_tip(tooltips, check_relative,
1051 _("If on, the symlink will store the path from the "
1052 "symlink to the target file. Use this if the symlink "
1053 "and the target will be moved together.\n"
1054 "If off, the path from the root directory is stored - "
1055 "use this if the symlink may move but the target will "
1056 "stay put."), NULL);
1057 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1058 check_relative, FALSE, TRUE, 0);
1059 gtk_widget_show(check_relative);
1062 g_signal_connect(savebox, "save_to_file",
1063 G_CALLBACK(save_to_file), NULL);
1065 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1066 g_strdup(path), g_free);
1067 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1068 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1070 gtk_window_set_title(GTK_WINDOW(savebox), action);
1072 if (g_utf8_validate(path, -1, NULL))
1073 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1074 else
1076 gchar *u8, *dir;
1077 dir = g_path_get_dirname(path);
1078 u8 = to_utf8(g_basename(path));
1079 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1080 make_path(dir, u8));
1081 g_free(u8);
1082 g_free(dir);
1084 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1085 g_object_unref(image);
1087 gtk_widget_show(savebox);
1090 static gint save_to_file(GObject *savebox,
1091 const gchar *pathname, gpointer data)
1093 SaveCb callback;
1094 const gchar *current_path;
1096 callback = g_object_get_data(savebox, "action_callback");
1097 current_path = g_object_get_data(savebox, "current_path");
1099 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1100 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1102 return callback(savebox, current_path, pathname)
1103 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1106 static gboolean copy_cb(GObject *savebox,
1107 const gchar *current, const gchar *new)
1109 return action_with_leaf(action_copy, current, new);
1112 static gboolean action_with_leaf(ActionFn action,
1113 const gchar *current, const gchar *new)
1115 const char *leaf;
1116 char *new_dir;
1117 GList *local_paths;
1119 if (new[0] != '/')
1121 report_error(_("New pathname is not absolute"));
1122 return FALSE;
1125 if (new[strlen(new) - 1] == '/')
1127 new_dir = g_strdup(new);
1128 leaf = NULL;
1130 else
1132 const gchar *slash;
1134 slash = strrchr(new, '/');
1135 new_dir = g_strndup(new, slash - new);
1136 leaf = slash + 1;
1139 local_paths = g_list_append(NULL, (gchar *) current);
1140 action(local_paths, new_dir, leaf, -1);
1141 g_list_free(local_paths);
1143 g_free(new_dir);
1145 return TRUE;
1148 /* Open a savebox to act on the selected file.
1149 * Call 'callback' later to perform the operation.
1151 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1152 const gchar *action, SaveCb callback,
1153 GdkDragAction dnd_action)
1155 g_object_ref(image);
1156 savebox_show(action, path, image, callback, dnd_action);
1159 static gboolean rename_cb(GObject *savebox,
1160 const gchar *current, const gchar *new)
1162 return action_with_leaf(action_move, current, new);
1165 static gboolean link_cb(GObject *savebox,
1166 const gchar *initial, const gchar *path)
1168 GtkToggleButton *check_relative;
1169 struct stat info;
1170 int err;
1171 gchar *link_path;
1173 check_relative = g_object_get_data(savebox, "check_relative");
1175 if (gtk_toggle_button_get_active(check_relative))
1176 link_path = get_relative_path(path, initial);
1177 else
1178 link_path = g_strdup(initial);
1180 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1182 GtkWidget *box, *button;
1183 gint ans;
1185 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1186 GTK_BUTTONS_CANCEL,
1187 _("Symlink from '%s' already exists. "
1188 "Replace it with a link to '%s'?"),
1189 path, link_path);
1191 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1193 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1194 gtk_widget_show(button);
1195 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1196 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1197 button, GTK_RESPONSE_OK);
1198 gtk_dialog_set_default_response(GTK_DIALOG(box),
1199 GTK_RESPONSE_OK);
1201 ans = gtk_dialog_run(GTK_DIALOG(box));
1202 gtk_widget_destroy(box);
1204 if (ans != GTK_RESPONSE_OK)
1206 g_free(link_path);
1207 return FALSE;
1210 unlink(path);
1213 err = symlink(link_path, path);
1214 g_free(link_path);
1216 if (err)
1218 report_error("symlink: %s", g_strerror(errno));
1219 return FALSE;
1222 dir_check_this(path);
1224 return TRUE;
1227 static void run_action(DirItem *item)
1229 if (can_set_run_action(item))
1230 type_set_handler_dialog(item->mime_type);
1231 else
1232 report_error(
1233 _("You can only set the run action for a "
1234 "regular file"));
1237 void open_home(gpointer data, guint action, GtkWidget *widget)
1239 filer_opendir(home_dir, NULL, NULL);
1242 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1244 gchar *path;
1246 path = g_strconcat(filer_window->sym_path,
1247 "/", item->leafname, "#", NULL);
1249 filer_change_to(filer_window, path, NULL);
1250 g_free(path);
1253 static void select_all(gpointer data, guint action, GtkWidget *widget)
1255 g_return_if_fail(window_with_focus != NULL);
1257 window_with_focus->temp_item_selected = FALSE;
1258 view_select_all(window_with_focus->view);
1261 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1263 g_return_if_fail(window_with_focus != NULL);
1265 window_with_focus->temp_item_selected = FALSE;
1266 view_clear_selection(window_with_focus->view);
1269 static gboolean invert_cb(ViewIter *iter, gpointer data)
1271 return !view_get_selected((ViewIface *) data, iter);
1274 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1276 g_return_if_fail(window_with_focus != NULL);
1278 window_with_focus->temp_item_selected = FALSE;
1280 view_select_if(window_with_focus->view, invert_cb,
1281 window_with_focus->view);
1284 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1286 GtkWidget *win;
1288 win = options_show();
1290 if (win)
1292 number_of_windows++;
1293 g_signal_connect(win, "destroy",
1294 G_CALLBACK(one_less_window), NULL);
1298 static gboolean new_directory_cb(GObject *savebox,
1299 const gchar *initial, const gchar *path)
1301 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1303 report_error("mkdir: %s", g_strerror(errno));
1304 return FALSE;
1307 dir_check_this(path);
1309 if (filer_exists(window_with_focus))
1311 guchar *leaf;
1312 leaf = strrchr(path, '/');
1313 if (leaf)
1314 display_set_autoselect(window_with_focus, leaf + 1);
1317 return TRUE;
1320 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1322 g_return_if_fail(window_with_focus != NULL);
1324 savebox_show(_("Create"),
1325 make_path(window_with_focus->sym_path, _("NewDir")),
1326 type_to_icon(inode_directory), new_directory_cb,
1327 GDK_ACTION_COPY);
1330 static gboolean new_file_cb(GObject *savebox,
1331 const gchar *initial, const gchar *path)
1333 int fd;
1335 fd = open(path, O_CREAT | O_EXCL, 0666);
1337 if (fd == -1)
1339 report_error(_("Error creating '%s': %s"),
1340 path, g_strerror(errno));
1341 return FALSE;
1344 if (close(fd))
1345 report_error(_("Error creating '%s': %s"),
1346 path, g_strerror(errno));
1348 dir_check_this(path);
1350 if (filer_exists(window_with_focus))
1352 guchar *leaf;
1353 leaf = strrchr(path, '/');
1354 if (leaf)
1355 display_set_autoselect(window_with_focus, leaf + 1);
1358 return TRUE;
1361 static void new_file(gpointer data, guint action, GtkWidget *widget)
1363 g_return_if_fail(window_with_focus != NULL);
1365 savebox_show(_("Create"),
1366 make_path(window_with_focus->sym_path, _("NewFile")),
1367 type_to_icon(text_plain),
1368 new_file_cb, GDK_ACTION_COPY);
1371 static gboolean new_file_type_cb(GObject *savebox,
1372 const gchar *initial, const gchar *path)
1374 const gchar *oleaf, *leaf;
1375 gchar *templ, *templ_dname, *dest;
1376 GList *paths;
1378 /* We can work out the template path from the initial name */
1379 oleaf = g_basename(initial);
1380 templ_dname = choices_find_path_load("Templates", "");
1381 if (!templ_dname)
1383 report_error(
1384 _("Error creating file: could not find the template for %s"),
1385 oleaf);
1386 return FALSE;
1389 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1390 g_free(templ_dname);
1392 dest = g_path_get_dirname(path);
1393 leaf = g_basename(path);
1394 paths = g_list_append(NULL, templ);
1396 action_copy(paths, dest, leaf, TRUE);
1398 g_list_free(paths);
1399 g_free(dest);
1400 g_free(templ);
1402 if (filer_exists(window_with_focus))
1403 display_set_autoselect(window_with_focus, leaf);
1405 return TRUE;
1408 static void do_send_to(gchar *templ)
1410 g_return_if_fail(send_to_paths != NULL);
1412 run_with_files(templ, send_to_paths);
1415 static void new_file_type(gchar *templ)
1417 const gchar *leaf;
1418 MIME_type *type;
1420 g_return_if_fail(window_with_focus != NULL);
1422 leaf = g_basename(templ);
1423 type = type_get_type(templ);
1425 savebox_show(_("Create"),
1426 make_path(window_with_focus->sym_path, leaf),
1427 type_to_icon(type),
1428 new_file_type_cb, GDK_ACTION_COPY);
1431 static void customise_send_to(gpointer data)
1433 GPtrArray *path;
1434 guchar *save;
1435 GString *dirs;
1436 int i;
1438 dirs = g_string_new(NULL);
1440 path = choices_list_dirs("");
1441 for (i = 0; i < path->len; i++)
1443 guchar *old = (guchar *) path->pdata[i];
1445 g_string_append(dirs, old);
1446 g_string_append(dirs, "SendTo\n");
1448 choices_free_list(path);
1450 save = choices_find_path_save("", "SendTo", TRUE);
1451 if (save)
1452 mkdir(save, 0777);
1454 info_message(
1455 _("The `Send To' menu provides a quick way to send some files "
1456 "to an application. The applications listed are those in "
1457 "the following directories:\n\n%s\n%s\n"
1458 "The `Send To' menu may be opened by Shift+Menu clicking "
1459 "over a file.\n\n"
1460 "Advanced use:\n"
1461 "You can also create subdirectories called "
1462 "`.text_html', `.text', etc which will only be "
1463 "shown for files of that type. `.group' is shown "
1464 "only when multiple files are selected."),
1465 dirs->str,
1466 save ? _("I'll show you your SendTo directory now; you should "
1467 "symlink (Ctrl+Shift drag) any applications you want "
1468 "into it.")
1469 : _("Your CHOICESPATH variable setting prevents "
1470 "customisations - sorry."));
1472 g_string_free(dirs, TRUE);
1474 if (save)
1475 filer_opendir(save, NULL, NULL);
1478 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1479 * to the menu.
1481 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1483 gchar *searchdir;
1484 GPtrArray *paths;
1485 int i;
1487 if (subtype)
1488 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1489 else if (type)
1490 searchdir = g_strdup_printf("SendTo/.%s", type);
1491 else
1492 searchdir = g_strdup("SendTo");
1494 paths = choices_list_dirs(searchdir);
1495 g_free(searchdir);
1497 for (i = 0; i < paths->len; i++)
1499 GList *widgets = NULL;
1500 guchar *dir = (guchar *) paths->pdata[i];
1502 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1503 (CallbackFn) do_send_to,
1504 FALSE, FALSE, TRUE);
1506 if (widgets)
1507 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1508 gtk_menu_item_new());
1510 g_list_free(widgets); /* TODO: Get rid of this */
1513 choices_free_list(paths);
1516 /* Scan the SendTo dir and create and show the Send To menu.
1517 * The 'paths' list and every path in it is claimed, and will be
1518 * freed later -- don't free it yourself!
1520 static void show_send_to_menu(GList *paths, GdkEvent *event)
1522 GtkWidget *menu, *item;
1524 menu = gtk_menu_new();
1526 if (g_list_length(paths) == 1)
1528 DirItem *item;
1530 item = diritem_new("");
1531 diritem_restat(paths->data, item, NULL);
1533 add_sendto(menu,
1534 item->mime_type->media_type,
1535 item->mime_type->subtype);
1537 add_sendto(menu, item->mime_type->media_type, NULL);
1539 diritem_free(item);
1541 else
1543 GList *rover;
1544 gboolean same=TRUE, same_media=TRUE;
1545 MIME_type *type=NULL;
1546 DirItem *item;
1548 item = diritem_new("");
1549 for(rover=paths; rover; rover=g_list_next(rover))
1551 diritem_restat(rover->data, item, NULL);
1552 if(!type)
1553 type=item->mime_type;
1554 else
1556 if(type!=item->mime_type)
1558 same=FALSE;
1559 if(strcmp(type->media_type,
1560 item->mime_type->media_type)!=0)
1562 same_media=FALSE;
1563 break;
1568 diritem_free(item);
1570 if(type)
1572 if(same)
1573 add_sendto(menu, type->media_type,
1574 type->subtype);
1575 if(same_media)
1576 add_sendto(menu, type->media_type, NULL);
1579 add_sendto(menu, "group", NULL);
1582 add_sendto(menu, NULL, NULL);
1584 item = gtk_menu_item_new_with_label(_("Customise"));
1585 g_signal_connect_swapped(item, "activate",
1586 G_CALLBACK(customise_send_to), NULL);
1587 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1589 if (send_to_paths)
1590 destroy_glist(&send_to_paths);
1592 send_to_paths = paths;
1594 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1596 popup_menu = menu;
1597 show_popup_menu(menu, event, 0);
1600 static void send_to(FilerWindow *filer_window)
1602 GList *paths;
1603 GdkEvent *event;
1605 paths = filer_selected_items(filer_window);
1606 event = gtk_get_current_event();
1608 /* Eats paths */
1609 show_send_to_menu(paths, event);
1611 gdk_event_free(event);
1614 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1616 const char *argv[] = {"sh", "-c", NULL, NULL};
1617 gboolean close = action;
1619 argv[2] = o_menu_xterm.value;
1621 g_return_if_fail(window_with_focus != NULL);
1623 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1624 gtk_widget_destroy(window_with_focus->window);
1627 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1629 g_return_if_fail(window_with_focus != NULL);
1631 filer_change_to(window_with_focus, home_dir, NULL);
1634 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1636 g_return_if_fail(window_with_focus != NULL);
1638 bookmarks_show_menu(window_with_focus);
1641 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1643 g_return_if_fail(window_with_focus != NULL);
1645 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1646 filer_change_to(window_with_focus,
1647 window_with_focus->real_path, NULL);
1648 else
1649 delayed_error(_("This is already the canonical name "
1650 "for this directory."));
1653 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1655 g_return_if_fail(window_with_focus != NULL);
1657 filer_open_parent(window_with_focus);
1660 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1662 g_return_if_fail(window_with_focus != NULL);
1664 change_to_parent(window_with_focus);
1667 static void resize(gpointer data, guint action, GtkWidget *widget)
1669 g_return_if_fail(window_with_focus != NULL);
1671 view_autosize(window_with_focus->view);
1674 static void new_window(gpointer data, guint action, GtkWidget *widget)
1676 g_return_if_fail(window_with_focus != NULL);
1678 if (o_unique_filer_windows.int_value)
1680 report_error(_("You can't open a second view onto "
1681 "this directory because the `Unique Windows' option "
1682 "is turned on in the Options window."));
1684 else
1685 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1688 static void close_window(gpointer data, guint action, GtkWidget *widget)
1690 g_return_if_fail(window_with_focus != NULL);
1692 if (!filer_window_delete(window_with_focus->window, NULL,
1693 window_with_focus))
1694 gtk_widget_destroy(window_with_focus->window);
1697 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1699 MiniType type = (MiniType) action;
1701 g_return_if_fail(window_with_focus != NULL);
1703 /* Item needs to remain selected... */
1704 if (type == MINI_SHELL)
1705 window_with_focus->temp_item_selected = FALSE;
1707 minibuffer_show(window_with_focus, type);
1710 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1712 if (action == HELP_ABOUT)
1713 infobox_new(app_dir);
1714 else if (action == HELP_DIR)
1715 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1716 else if (action == HELP_MANUAL)
1718 gchar *manual = NULL;
1720 if (current_lang)
1722 manual = g_strconcat(app_dir, "/Help/Manual-",
1723 current_lang, ".html", NULL);
1724 if (access(manual, F_OK))
1725 null_g_free(&manual);
1728 if (!manual)
1729 manual = g_strconcat(app_dir,
1730 "/Help/Manual.html", NULL);
1732 run_by_path(manual);
1734 g_free(manual);
1736 else
1737 g_warning("Unknown help action %d\n", action);
1740 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1741 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1743 GList *items, *item;
1745 items = gtk_container_get_children(GTK_CONTAINER(menu));
1747 item = g_list_nth(items, from);
1748 while (item && n--)
1750 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1751 item = item->next;
1753 g_list_free(items);
1756 static void save_menus(void)
1758 char *menurc;
1760 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1761 if (menurc)
1763 gtk_accel_map_save(menurc);
1764 g_free(menurc);
1768 static void select_nth_item(GtkMenuShell *shell, int n)
1770 GList *items;
1771 GtkWidget *item;
1773 items = gtk_container_get_children(GTK_CONTAINER(shell));
1774 item = g_list_nth_data(items, n);
1776 g_return_if_fail(item != NULL);
1778 g_list_free(items);
1780 gtk_menu_shell_select_item(shell, item);
1783 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1785 DirItem *item;
1786 const guchar *path;
1787 int n_selected;
1788 ViewIter iter;
1790 g_return_if_fail(window_with_focus != NULL);
1792 n_selected = view_count_selected(window_with_focus->view);
1794 if (n_selected < 1)
1796 const char *prompt;
1798 switch (action)
1800 case FILE_COPY_ITEM:
1801 prompt = _("Copy ... ?");
1802 break;
1803 case FILE_RENAME_ITEM:
1804 prompt = _("Rename ... ?");
1805 break;
1806 case FILE_LINK_ITEM:
1807 prompt = _("Symlink ... ?");
1808 break;
1809 case FILE_OPEN_FILE:
1810 prompt = _("Shift Open ... ?");
1811 break;
1812 case FILE_PROPERTIES:
1813 prompt = _("Properties of ... ?");
1814 break;
1815 case FILE_SET_TYPE:
1816 prompt = _("Set type of ... ?");
1817 break;
1818 case FILE_RUN_ACTION:
1819 prompt = _("Set run action for ... ?");
1820 break;
1821 case FILE_SET_ICON:
1822 prompt = _("Set icon for ... ?");
1823 break;
1824 case FILE_SEND_TO:
1825 prompt = _("Send ... to ... ?");
1826 break;
1827 case FILE_DELETE:
1828 prompt = _("DELETE ... ?");
1829 break;
1830 case FILE_USAGE:
1831 prompt = _("Count the size of ... ?");
1832 break;
1833 case FILE_CHMOD_ITEMS:
1834 prompt = _("Set permissions on ... ?");
1835 break;
1836 case FILE_FIND:
1837 prompt = _("Search inside ... ?");
1838 break;
1839 case FILE_OPEN_VFS_AVFS:
1840 prompt = _("Look inside ... ?");
1841 break;
1842 default:
1843 g_warning("Unknown action!");
1844 return;
1846 filer_target_mode(window_with_focus, target_callback,
1847 GINT_TO_POINTER(action), prompt);
1848 return;
1851 switch (action)
1853 case FILE_SEND_TO:
1854 send_to(window_with_focus);
1855 return;
1856 case FILE_DELETE:
1857 delete(window_with_focus);
1858 return;
1859 case FILE_USAGE:
1860 usage(window_with_focus);
1861 return;
1862 case FILE_CHMOD_ITEMS:
1863 chmod_items(window_with_focus);
1864 return;
1865 case FILE_SET_TYPE:
1866 set_type_items(window_with_focus);
1867 return;
1868 case FILE_FIND:
1869 find(window_with_focus);
1870 return;
1871 case FILE_PROPERTIES:
1873 GList *items;
1875 items = filer_selected_items(window_with_focus);
1876 infobox_show_list(items);
1877 destroy_glist(&items);
1878 return;
1880 default:
1881 break;
1884 /* All the following actions require exactly one file selected */
1886 if (n_selected > 1)
1888 report_error(_("You cannot do this to more than "
1889 "one item at a time"));
1890 return;
1893 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1895 item = iter.next(&iter);
1896 g_return_if_fail(item != NULL);
1897 /* iter may be passed to filer_openitem... */
1899 if (item->base_type == TYPE_UNKNOWN)
1900 item = dir_update_item(window_with_focus->directory,
1901 item->leafname);
1903 if (!item)
1905 report_error(_("Item no longer exists!"));
1906 return;
1909 path = make_path(window_with_focus->sym_path, item->leafname);
1911 switch (action)
1913 case FILE_COPY_ITEM:
1914 src_dest_action_item(path, di_image(item),
1915 _("Copy"), copy_cb,
1916 GDK_ACTION_COPY);
1917 break;
1918 case FILE_RENAME_ITEM:
1919 src_dest_action_item(path, di_image(item),
1920 _("Rename"), rename_cb,
1921 GDK_ACTION_MOVE);
1922 break;
1923 case FILE_LINK_ITEM:
1924 src_dest_action_item(path, di_image(item),
1925 _("Symlink"), link_cb,
1926 GDK_ACTION_LINK);
1927 break;
1928 case FILE_OPEN_FILE:
1929 filer_openitem(window_with_focus, &iter,
1930 OPEN_SAME_WINDOW | OPEN_SHIFT);
1931 break;
1932 case FILE_RUN_ACTION:
1933 run_action(item);
1934 break;
1935 case FILE_SET_ICON:
1936 icon_set_handler_dialog(item, path);
1937 break;
1938 case FILE_OPEN_VFS_AVFS:
1939 open_vfs_avfs(window_with_focus, item);
1940 break;
1941 default:
1942 g_warning("Unknown action!");
1943 return;
1947 static void show_key_help(GtkWidget *button, gpointer data)
1949 gboolean can_change_accels;
1951 g_object_get(G_OBJECT(gtk_settings_get_default()),
1952 "gtk-can-change-accels", &can_change_accels,
1953 NULL);
1955 if (!can_change_accels)
1957 info_message(_("User-definable shortcuts are disabled by "
1958 "default in Gtk2, and you have not enabled "
1959 "them. You can turn this feature on by:\n\n"
1960 "1) using an XSettings manager, such as ROX-Session "
1961 "or gnome-settings-daemon, or\n\n"
1962 "2) adding this line to ~/.gtkrc-2.0:\n"
1963 "\tgtk-can-change-accels = 1\n"
1964 "\t(this only works if NOT using XSETTINGS)"));
1965 return;
1968 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1969 "- Open the menu over a filer window,\n"
1970 "- Move the pointer over the item you want to use,\n"
1971 "- Press the key you want attached to it.\n\n"
1972 "The key will appear next to the menu item and you can just press "
1973 "that key without opening the menu in future."));
1976 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1978 GtkWidget *button, *align;
1980 g_return_val_if_fail(option == NULL, NULL);
1982 align = gtk_alignment_new(0, 0.5, 0, 0);
1983 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1984 gtk_container_add(GTK_CONTAINER(align), button);
1985 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1987 return g_list_append(NULL, align);