r3403: Save display settings for specific paths
[rox-filer.git] / ROX-Filer / src / menu.c
blob87eba3b630a11a5825e1ac69af267ede5dc30182
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_PROPERTIES,
70 FILE_RUN_ACTION,
71 FILE_SET_ICON,
72 FILE_SEND_TO,
73 FILE_DELETE,
74 FILE_USAGE,
75 FILE_CHMOD_ITEMS,
76 FILE_FIND,
77 FILE_OPEN_VFS_AVFS,
78 FILE_SET_TYPE,
79 } FileOp;
81 typedef void (*ActionFn)(GList *paths,
82 const char *dest_dir, const char *leaf, int quiet);
83 typedef void MenuCallback(GtkWidget *widget, gpointer data);
85 typedef gboolean (*SaveCb)(GObject *savebox,
86 const gchar *current, const gchar *new);
88 GtkAccelGroup *filer_keys = NULL;
89 static gboolean filer_keys_need_init = TRUE;
91 static GtkWidget *popup_menu = NULL; /* Currently open menu */
93 static gint updating_menu = 0; /* Non-zero => ignore activations */
94 static GList *send_to_paths = NULL;
96 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
98 /* Static prototypes */
100 static void save_menus(void);
101 static void menu_closed(GtkWidget *widget);
102 static void shade_file_menu_items(gboolean shaded);
103 static void savebox_show(const gchar *action, const gchar *path,
104 MaskedPixmap *image, SaveCb callback,
105 GdkDragAction dnd_action);
106 static gint save_to_file(GObject *savebox,
107 const gchar *pathname, gpointer data);
108 static gboolean action_with_leaf(ActionFn action,
109 const gchar *current, const gchar *new);
110 static gboolean link_cb(GObject *savebox,
111 const gchar *initial, const gchar *path);
112 static void select_nth_item(GtkMenuShell *shell, int n);
113 static void new_file_type(gchar *templ);
114 static void do_send_to(gchar *templ);
115 static void show_send_to_menu(GList *paths, GdkEvent *event);
116 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
118 /* Note that for most of these callbacks none of the arguments are used. */
120 static void view_type(gpointer data, guint action, GtkWidget *widget);
122 /* (action used in these three - DetailsType) */
123 static void change_size(gpointer data, guint action, GtkWidget *widget);
124 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
125 static void set_with(gpointer data, guint action, GtkWidget *widget);
127 static void set_sort(gpointer data, guint action, GtkWidget *widget);
128 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
130 static void hidden(gpointer data, guint action, GtkWidget *widget);
131 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
132 static void refresh(gpointer data, guint action, GtkWidget *widget);
133 static void save_settings(gpointer data, guint action, GtkWidget *widget);
135 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
137 static void select_all(gpointer data, guint action, GtkWidget *widget);
138 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
139 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
140 static void new_directory(gpointer data, guint action, GtkWidget *widget);
141 static void new_file(gpointer data, guint action, GtkWidget *widget);
142 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
144 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
145 static void open_parent(gpointer data, guint action, GtkWidget *widget);
146 static void home_directory(gpointer data, guint action, GtkWidget *widget);
147 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
148 static void new_window(gpointer data, guint action, GtkWidget *widget);
149 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
150 static void close_window(gpointer data, guint action, GtkWidget *widget);
151 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
153 /* (action used in this - MiniType) */
154 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
155 static void resize(gpointer data, guint action, GtkWidget *widget);
157 #define MENUS_NAME "menus2"
159 static GtkWidget *filer_menu; /* The popup filer menu */
160 static GtkWidget *filer_file_item; /* The File '' label */
161 static GtkWidget *filer_file_menu; /* The File '' menu */
162 static GtkWidget *file_shift_item; /* Shift Open label */
163 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
164 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
165 static GtkWidget *filer_reverse_menu; /* The Reversed item */
166 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
167 static GtkWidget *filer_new_window; /* The New Window item */
168 static GtkWidget *filer_new_menu; /* The New submenu */
169 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
171 #undef N_
172 #define N_(x) x
174 static GtkItemFactoryEntry filer_menu_def[] = {
175 {N_("Display"), NULL, NULL, 0, "<Branch>"},
176 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
177 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
178 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
179 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
180 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
181 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
182 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
183 {">", NULL, NULL, 0, "<Separator>"},
184 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
185 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
186 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
187 {">", NULL, NULL, 0, "<Separator>"},
188 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
189 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
190 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
191 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
192 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
193 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
194 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
195 {">", NULL, NULL, 0, "<Separator>"},
196 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
197 {">" N_("Filter files"), NULL, mini_buffer, MINI_FILTER, NULL},
198 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
199 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
200 {">" N_("Save display settings"), NULL, save_settings, 0, NULL},
201 {N_("File"), NULL, NULL, 0, "<Branch>"},
202 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
203 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
204 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
205 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
206 {">", NULL, NULL, 0, "<Separator>"},
207 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
208 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
209 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
210 {">", NULL, NULL, 0, "<Separator>"},
211 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
212 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
213 {">" N_("Properties"), NULL, file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
214 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
215 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
216 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
217 {">", NULL, NULL, 0, "<Separator>"},
218 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
219 {N_("Select"), NULL, NULL, 0, "<Branch>"},
220 {">" N_("Select All"), NULL, select_all, 0, NULL},
221 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
222 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
223 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
224 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
225 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
226 {N_("New"), NULL, NULL, 0, "<Branch>"},
227 {">" N_("Directory"), NULL, new_directory, 0, NULL},
228 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
229 {N_("Window"), NULL, NULL, 0, "<Branch>"},
230 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
231 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
232 {">" N_("New Window"), NULL, new_window, 0, NULL},
233 {">" N_("Home Directory"), NULL, home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
234 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
235 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
236 {">" N_("Resize Window"), NULL, resize, 0, NULL},
237 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
239 {">" N_("Close Window"), NULL, close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
240 {">", NULL, NULL, 0, "<Separator>"},
241 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
242 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
243 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
244 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
245 {N_("Help"), NULL, NULL, 0, "<Branch>"},
246 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
247 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
248 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
252 #define GET_MENU_ITEM(var, menu) \
253 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
255 #define GET_SMENU_ITEM(var, menu, sub) \
256 do { \
257 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
258 var = gtk_item_factory_get_widget(item_factory, tmp); \
259 g_free(tmp); \
260 } while (0)
262 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
263 do { \
264 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
265 var = gtk_item_factory_get_widget(item_factory, tmp); \
266 g_free(tmp); \
267 } while (0)
269 /* Returns TRUE if the keys were installed (first call only) */
270 gboolean ensure_filer_menu(void)
272 GList *items;
273 guchar *tmp;
274 GtkWidget *item;
275 GtkItemFactory *item_factory;
277 if (!filer_keys_need_init)
278 return FALSE;
279 filer_keys_need_init = FALSE;
281 item_factory = menu_create(filer_menu_def,
282 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
283 "<filer>", filer_keys);
285 GET_MENU_ITEM(filer_menu, "filer");
286 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
287 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
288 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
289 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
290 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
291 "Show Thumbnails");
293 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
294 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
295 filer_follow_sym = GTK_BIN(item)->child;
297 /* File '' label... */
298 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
299 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
300 g_list_free(items);
302 /* Shift Open... label */
303 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
304 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
305 g_list_free(items);
307 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
308 filer_new_window = GTK_BIN(item)->child;
310 g_signal_connect(filer_menu, "unmap_event",
311 G_CALLBACK(menu_closed), NULL);
312 g_signal_connect(filer_file_menu, "unmap_event",
313 G_CALLBACK(menu_closed), NULL);
315 g_signal_connect(filer_keys, "accel_changed",
316 G_CALLBACK(save_menus), NULL);
318 return TRUE;
321 void menu_init(void)
323 char *menurc;
325 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
326 if (menurc)
328 gtk_accel_map_load(menurc);
329 g_free(menurc);
332 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
333 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
334 option_add_int(&o_menu_quick, "menu_quick", FALSE);
335 option_add_saver(save_menus);
337 option_register_widget("menu-set-keys", set_keys_button);
339 filer_keys = gtk_accel_group_new();
342 /* Name is in the form "<panel>" */
343 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
344 const gchar *name, GtkAccelGroup *keys)
346 GtkItemFactory *item_factory;
347 GtkItemFactoryEntry *translated;
349 if (!keys)
351 keys = gtk_accel_group_new();
352 gtk_accel_group_lock(keys);
355 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
357 translated = translate_entries(def, n_entries);
358 gtk_item_factory_create_items(item_factory, n_entries,
359 translated, NULL);
360 free_translated_entries(translated, n_entries);
362 return item_factory;
365 /* Prevent the user from setting a short-cut on this item */
366 static void menuitem_no_shortcuts(GtkWidget *item)
368 /* XXX */
369 #if 0
370 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
372 _gtk_widget_set_accel_path(item, NULL, NULL);
373 null_g_free(&menuitem->accel_path);
374 #endif
377 /* Shade items that only work on single files */
378 static void shade_file_menu_items(gboolean shaded)
380 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
381 menu_set_items_shaded(filer_file_menu, shaded, 5, 2);
382 menu_set_items_shaded(filer_file_menu, shaded, 9, 2);
385 /* 'data' is an array of three ints:
386 * [ pointer_x, pointer_y, item_under_pointer ]
388 void position_menu(GtkMenu *menu, gint *x, gint *y,
389 gboolean *push_in, gpointer data)
391 int *pos = (int *) data;
392 GtkRequisition requisition;
393 GList *items, *next;
394 int y_shift = 0;
395 int item = pos[2];
397 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
399 while (item >= 0 && next)
401 int h = ((GtkWidget *) next->data)->requisition.height;
403 if (item > 0)
404 y_shift += h;
405 else
406 y_shift += h / 2;
408 next = next->next;
409 item--;
412 g_list_free(items);
414 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
416 *x = pos[0] - (requisition.width * 7 / 8);
417 *y = pos[1] - y_shift;
419 *x = CLAMP(*x, 0, screen_width - requisition.width);
420 *y = CLAMP(*y, 0, screen_height - requisition.height);
422 *push_in = FALSE;
425 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
426 MenuIconStyle style)
428 GtkWidget *item;
430 if (ditem->image && style != MIS_NONE)
432 GdkPixbuf *pixbuf;
434 switch (style)
436 case MIS_LARGE:
437 pixbuf = ditem->image->pixbuf;
438 break;
439 default:
440 if (!ditem->image->sm_pixbuf)
441 pixmap_make_small(ditem->image);
442 pixbuf = ditem->image->sm_pixbuf;
443 break;
446 item = gtk_image_menu_item_new_with_label(label);
447 /* TODO: Find a way to allow short-cuts */
448 menuitem_no_shortcuts(item);
450 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
451 gtk_image_new_from_pixbuf(pixbuf));
452 gtk_widget_show_all(item);
454 else
455 item = gtk_menu_item_new_with_label(label);
457 return item;
460 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
461 MenuIconStyle style, CallbackFn func,
462 gboolean separator, gboolean strip_ext,
463 gboolean recurse)
465 GList *widgets = NULL;
466 DirItem *ditem;
467 int i;
468 GtkWidget *item;
469 char *dname = NULL;
470 GPtrArray *names;
472 dname = pathdup(dir_name);
474 names = list_dir(dname);
475 if (!names)
476 goto out;
478 if (separator)
480 item = gtk_menu_item_new();
481 widgets = g_list_append(widgets, item);
482 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
485 for (i = 0; i < names->len; i++)
487 char *leaf = names->pdata[i];
488 gchar *fname;
490 fname = g_strconcat(dname, "/", leaf, NULL);
492 /* Strip off extension, if any */
493 if (strip_ext)
495 char *dot;
496 dot = strchr(leaf, '.');
497 if (dot)
498 *dot = '\0';
501 ditem = diritem_new("");
502 diritem_restat(fname, ditem, NULL);
504 item = make_send_to_item(ditem, leaf, style);
506 g_free(leaf);
508 /* If it is a directory (but NOT an AppDir) and we are
509 * recursing then set up a sub menu.
511 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
512 !(ditem->flags & ITEM_FLAG_APPDIR))
514 GtkWidget *sub;
515 GList *new_widgets;
517 sub = gtk_menu_new();
518 new_widgets = menu_from_dir(sub, fname, style, func,
519 separator, strip_ext, FALSE);
520 g_list_free(new_widgets);
521 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
523 else
524 g_signal_connect_swapped(item, "activate",
525 G_CALLBACK(func), fname);
527 diritem_free(ditem);
529 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
530 g_signal_connect_swapped(item, "destroy",
531 G_CALLBACK(g_free), fname);
533 widgets = g_list_append(widgets, item);
536 g_ptr_array_free(names, TRUE);
537 out:
538 g_free(dname);
540 return widgets;
543 /* Scan the templates dir and create entries for the New menu */
544 static void update_new_files_menu(MenuIconStyle style)
546 static GList *widgets = NULL;
548 gchar *templ_dname = NULL;
550 if (widgets)
552 GList *next;
554 for (next = widgets; next; next = next->next)
555 gtk_widget_destroy((GtkWidget *) next->data);
557 g_list_free(widgets);
558 widgets = NULL;
561 templ_dname = choices_find_path_load("Templates", "");
562 if (templ_dname)
564 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
565 (CallbackFn) new_file_type, TRUE, TRUE,
566 FALSE);
567 g_free(templ_dname);
569 gtk_widget_show_all(filer_new_menu);
572 /* 'item' is the number of the item to appear under the pointer. */
573 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
575 int pos[3];
576 int button = 0;
577 guint32 time = 0;
579 if (event && (event->type == GDK_BUTTON_PRESS ||
580 event->type == GDK_BUTTON_RELEASE))
582 GdkEventButton *bev = (GdkEventButton *) event;
584 pos[0] = bev->x_root;
585 pos[1] = bev->y_root;
586 button = bev->button;
587 time = bev->time;
589 else if (event && event->type == GDK_KEY_PRESS)
591 GdkEventKey *kev = (GdkEventKey *) event;
593 get_pointer_xy(pos, pos + 1);
594 time = kev->time;
596 else
597 get_pointer_xy(pos, pos + 1);
599 pos[2] = item;
601 gtk_widget_show_all(menu);
602 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
603 position_menu, (gpointer) pos, button, time);
604 select_nth_item(GTK_MENU_SHELL(menu), item);
607 /* Hide the popup menu, if any */
608 void menu_popdown(void)
610 if (popup_menu)
611 gtk_menu_popdown(GTK_MENU(popup_menu));
614 static MenuIconStyle get_menu_icon_style(void)
616 MenuIconStyle mis;
617 int display;
619 mis = o_menu_iconsize.int_value;
621 switch (mis)
623 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
624 return mis;
625 default:
626 break;
629 if (mis == MIS_CURRENT && window_with_focus)
631 switch (window_with_focus->display_style)
633 case HUGE_ICONS:
634 case LARGE_ICONS:
635 return MIS_LARGE;
636 case SMALL_ICONS:
637 return MIS_SMALL;
638 default:
639 break;
643 display = o_display_size.int_value;
644 switch (display)
646 case HUGE_ICONS:
647 case LARGE_ICONS:
648 return MIS_LARGE;
649 case SMALL_ICONS:
650 return MIS_SMALL;
651 default:
652 break;
655 return MIS_SMALL;
658 /* iter->peek() is the clicked item, or NULL if none */
659 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
661 DirItem *file_item = NULL;
662 GdkModifierType state = 0;
663 int n_selected;
664 int n_added = 0;
666 n_selected = view_count_selected(filer_window->view);
668 ensure_filer_menu();
670 updating_menu++;
672 /* Remove previous AppMenu, if any */
673 appmenu_remove();
675 window_with_focus = filer_window;
677 if (!event)
678 event = gtk_get_current_event();
680 if (event->type == GDK_BUTTON_PRESS)
681 state = ((GdkEventButton *) event)->state;
682 else if (event->type == GDK_KEY_PRESS)
683 state = ((GdkEventKey *) event)->state;
685 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
687 filer_window->temp_item_selected = TRUE;
688 view_set_selected(filer_window->view, iter, TRUE);
689 n_selected = view_count_selected(filer_window->view);
691 else
693 filer_window->temp_item_selected = FALSE;
696 /* Short-cut to the Send To menu */
697 if (state & GDK_SHIFT_MASK)
699 GList *paths;
701 updating_menu--;
703 if (n_selected == 0)
705 report_error(
706 _("You should Shift+Menu click over a file to "
707 "send it somewhere"));
708 return;
711 paths = filer_selected_items(filer_window);
713 show_send_to_menu(paths, event); /* (paths eaten) */
715 return;
719 GtkWidget *file_label, *file_menu;
720 GString *buffer;
721 DirItem *item;
723 file_label = filer_file_item;
724 file_menu = filer_file_menu;
725 gtk_check_menu_item_set_active(
726 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
727 filer_window->show_thumbs);
728 gtk_check_menu_item_set_active(
729 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
730 filer_window->show_hidden);
731 gtk_check_menu_item_set_active(
732 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
733 filer_window->sort_order != GTK_SORT_ASCENDING);
734 gtk_check_menu_item_set_active(
735 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
736 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
737 buffer = g_string_new(NULL);
739 switch (n_selected)
741 case 0:
742 g_string_assign(buffer, _("Next Click"));
743 shade_file_menu_items(FALSE);
744 break;
745 case 1:
746 item = filer_selected_item(filer_window);
747 if (!item->image)
748 dir_update_item(filer_window->directory,
749 item->leafname);
750 shade_file_menu_items(FALSE);
751 file_item = filer_selected_item(filer_window);
752 g_string_printf(buffer, _("%s '%s'"),
753 basetype_name(file_item),
754 g_utf8_validate(file_item->leafname,
755 -1, NULL)
756 ? file_item->leafname
757 : _("(bad utf-8)"));
758 if (!can_set_run_action(file_item))
759 menu_set_items_shaded(filer_file_menu,
760 TRUE, 9, 1);
761 break;
762 default:
763 shade_file_menu_items(TRUE);
764 g_string_printf(buffer, _("%d items"),
765 n_selected);
766 break;
768 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
769 g_string_free(buffer, TRUE);
771 menu_show_shift_action(file_shift_item, file_item,
772 n_selected == 0);
773 if (file_item)
774 n_added = appmenu_add(make_path(filer_window->sym_path,
775 file_item->leafname),
776 file_item, filer_file_menu);
779 update_new_files_menu(get_menu_icon_style());
781 gtk_widget_set_sensitive(filer_new_window,
782 !o_unique_filer_windows.int_value);
783 gtk_widget_set_sensitive(filer_follow_sym,
784 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
786 if (n_selected && o_menu_quick.int_value)
787 popup_menu = (state & GDK_CONTROL_MASK)
788 ? filer_menu
789 : filer_file_menu;
790 else
791 popup_menu = (state & GDK_CONTROL_MASK)
792 ? filer_file_menu
793 : filer_menu;
795 updating_menu--;
797 show_popup_menu(popup_menu, event,
798 popup_menu == filer_file_menu ? n_added : 1);
801 static void menu_closed(GtkWidget *widget)
803 if (window_with_focus == NULL || widget != popup_menu)
804 return; /* Close panel item chosen? */
806 popup_menu = NULL;
808 if (window_with_focus->temp_item_selected)
810 view_clear_selection(window_with_focus->view);
811 window_with_focus->temp_item_selected = FALSE;
814 appmenu_remove();
817 static void target_callback(FilerWindow *filer_window,
818 ViewIter *iter,
819 gpointer action)
821 g_return_if_fail(filer_window != NULL);
823 window_with_focus = filer_window;
825 /* Don't grab the primary selection */
826 filer_window->temp_item_selected = TRUE;
828 view_wink_item(filer_window->view, iter);
829 view_select_only(filer_window->view, iter);
830 file_op(NULL, GPOINTER_TO_INT(action), NULL);
832 view_clear_selection(filer_window->view);
833 filer_window->temp_item_selected = FALSE;
836 /* Set the text of the 'Shift Open...' menu item.
837 * If icon is NULL, reset the text and also shade it, unless 'next'.
839 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
841 guchar *shift_action = NULL;
843 if (item)
845 if (item->flags & ITEM_FLAG_MOUNT_POINT)
847 if (item->flags & ITEM_FLAG_MOUNTED)
848 shift_action = N_("Unmount");
849 else
850 shift_action = N_("Open unmounted");
852 else if (item->flags & ITEM_FLAG_SYMLINK)
853 shift_action = N_("Show Target");
854 else if (item->base_type == TYPE_DIRECTORY)
855 shift_action = N_("Look Inside");
856 else if (item->base_type == TYPE_FILE)
857 shift_action = N_("Open As Text");
859 gtk_label_set_text(GTK_LABEL(menu_item),
860 shift_action ? _(shift_action)
861 : _("Shift Open"));
862 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
865 /* Actions */
867 static void view_type(gpointer data, guint action, GtkWidget *widget)
869 ViewType view_type = (ViewType) action;
871 g_return_if_fail(window_with_focus != NULL);
873 if (view_type == VIEW_TYPE_COLLECTION)
874 display_set_layout(window_with_focus,
875 window_with_focus->display_style_wanted,
876 DETAILS_NONE, FALSE);
878 filer_set_view_type(window_with_focus, (ViewType) action);
881 static void change_size(gpointer data, guint action, GtkWidget *widget)
883 g_return_if_fail(window_with_focus != NULL);
885 display_change_size(window_with_focus, action == 1);
888 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
890 g_return_if_fail(window_with_focus != NULL);
892 if (updating_menu)
893 return;
895 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
896 display_set_layout(window_with_focus,
897 window_with_focus->display_style,
898 window_with_focus->details_type, FALSE);
899 else
900 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
901 window_with_focus->details_type, FALSE);
904 static void set_with(gpointer data, guint action, GtkWidget *widget)
906 DisplayStyle size;
908 g_return_if_fail(window_with_focus != NULL);
910 size = window_with_focus->display_style_wanted;
912 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
913 display_set_layout(window_with_focus, size, action, FALSE);
916 static void set_sort(gpointer data, guint action, GtkWidget *widget)
918 if (updating_menu)
919 return;
921 g_return_if_fail(window_with_focus != NULL);
923 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
926 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
928 GtkSortType order;
930 if (updating_menu)
931 return;
933 g_return_if_fail(window_with_focus != NULL);
935 order = window_with_focus->sort_order;
936 if (order == GTK_SORT_ASCENDING)
937 order = GTK_SORT_DESCENDING;
938 else
939 order = GTK_SORT_ASCENDING;
941 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
942 order);
945 static void hidden(gpointer data, guint action, GtkWidget *widget)
947 if (updating_menu)
948 return;
950 g_return_if_fail(window_with_focus != NULL);
952 display_set_hidden(window_with_focus,
953 !window_with_focus->show_hidden);
956 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
958 if (updating_menu)
959 return;
961 g_return_if_fail(window_with_focus != NULL);
963 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
966 static void refresh(gpointer data, guint action, GtkWidget *widget)
968 g_return_if_fail(window_with_focus != NULL);
970 filer_refresh(window_with_focus);
973 static void save_settings(gpointer data, guint action, GtkWidget *widget)
975 g_return_if_fail(window_with_focus != NULL);
977 filer_save_settings(window_with_focus);
980 static void delete(FilerWindow *filer_window)
982 GList *paths;
983 paths = filer_selected_items(filer_window);
984 action_delete(paths);
985 destroy_glist(&paths);
988 static void usage(FilerWindow *filer_window)
990 GList *paths;
991 paths = filer_selected_items(filer_window);
992 action_usage(paths);
993 destroy_glist(&paths);
996 static void chmod_items(FilerWindow *filer_window)
998 GList *paths;
999 paths = filer_selected_items(filer_window);
1000 action_chmod(paths, FALSE, NULL);
1001 destroy_glist(&paths);
1004 static void set_type_items(FilerWindow *filer_window)
1006 GList *paths;
1007 paths = filer_selected_items(filer_window);
1008 action_settype(paths, FALSE, NULL);
1009 destroy_glist(&paths);
1012 static void find(FilerWindow *filer_window)
1014 GList *paths;
1015 paths = filer_selected_items(filer_window);
1016 action_find(paths);
1017 destroy_glist(&paths);
1020 /* This creates a new savebox widget, and allows the user to pick a new path
1021 * for the file.
1022 * Once the new path has been picked, the callback will be called with
1023 * both the current and new paths.
1024 * NOTE: This function unrefs 'image'!
1026 static void savebox_show(const gchar *action, const gchar *path,
1027 MaskedPixmap *image, SaveCb callback,
1028 GdkDragAction dnd_action)
1030 GtkWidget *savebox = NULL;
1031 GtkWidget *check_relative = NULL;
1033 g_return_if_fail(image != NULL);
1035 savebox = gtk_savebox_new(action);
1036 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1038 if (callback == link_cb)
1040 check_relative = gtk_check_button_new_with_mnemonic(
1041 _("_Relative link"));
1042 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1043 TRUE);
1045 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1046 gtk_tooltips_set_tip(tooltips, check_relative,
1047 _("If on, the symlink will store the path from the "
1048 "symlink to the target file. Use this if the symlink "
1049 "and the target will be moved together.\n"
1050 "If off, the path from the root directory is stored - "
1051 "use this if the symlink may move but the target will "
1052 "stay put."), NULL);
1053 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1054 check_relative, FALSE, TRUE, 0);
1055 gtk_widget_show(check_relative);
1058 g_signal_connect(savebox, "save_to_file",
1059 G_CALLBACK(save_to_file), NULL);
1061 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1062 g_strdup(path), g_free);
1063 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1064 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1066 gtk_window_set_title(GTK_WINDOW(savebox), action);
1068 if (g_utf8_validate(path, -1, NULL))
1069 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1070 else
1072 gchar *u8, *dir;
1073 dir = g_path_get_dirname(path);
1074 u8 = to_utf8(g_basename(path));
1075 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1076 make_path(dir, u8));
1077 g_free(u8);
1078 g_free(dir);
1080 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1081 g_object_unref(image);
1083 gtk_widget_show(savebox);
1086 static gint save_to_file(GObject *savebox,
1087 const gchar *pathname, gpointer data)
1089 SaveCb callback;
1090 const gchar *current_path;
1092 callback = g_object_get_data(savebox, "action_callback");
1093 current_path = g_object_get_data(savebox, "current_path");
1095 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1096 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1098 return callback(savebox, current_path, pathname)
1099 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1102 static gboolean copy_cb(GObject *savebox,
1103 const gchar *current, const gchar *new)
1105 return action_with_leaf(action_copy, current, new);
1108 static gboolean action_with_leaf(ActionFn action,
1109 const gchar *current, const gchar *new)
1111 const char *leaf;
1112 char *new_dir;
1113 GList *local_paths;
1115 if (new[0] != '/')
1117 report_error(_("New pathname is not absolute"));
1118 return FALSE;
1121 if (new[strlen(new) - 1] == '/')
1123 new_dir = g_strdup(new);
1124 leaf = NULL;
1126 else
1128 const gchar *slash;
1130 slash = strrchr(new, '/');
1131 new_dir = g_strndup(new, slash - new);
1132 leaf = slash + 1;
1135 local_paths = g_list_append(NULL, (gchar *) current);
1136 action(local_paths, new_dir, leaf, -1);
1137 g_list_free(local_paths);
1139 g_free(new_dir);
1141 return TRUE;
1144 /* Open a savebox to act on the selected file.
1145 * Call 'callback' later to perform the operation.
1147 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1148 const gchar *action, SaveCb callback,
1149 GdkDragAction dnd_action)
1151 g_object_ref(image);
1152 savebox_show(action, path, image, callback, dnd_action);
1155 static gboolean rename_cb(GObject *savebox,
1156 const gchar *current, const gchar *new)
1158 return action_with_leaf(action_move, current, new);
1161 static gboolean link_cb(GObject *savebox,
1162 const gchar *initial, const gchar *path)
1164 GtkToggleButton *check_relative;
1165 struct stat info;
1166 int err;
1167 gchar *link_path;
1169 check_relative = g_object_get_data(savebox, "check_relative");
1171 if (gtk_toggle_button_get_active(check_relative))
1172 link_path = get_relative_path(path, initial);
1173 else
1174 link_path = g_strdup(initial);
1176 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1178 GtkWidget *box, *button;
1179 gint ans;
1181 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1182 GTK_BUTTONS_CANCEL,
1183 _("Symlink from '%s' already exists. "
1184 "Replace it with a link to '%s'?"),
1185 path, link_path);
1187 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1189 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1190 gtk_widget_show(button);
1191 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1192 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1193 button, GTK_RESPONSE_OK);
1194 gtk_dialog_set_default_response(GTK_DIALOG(box),
1195 GTK_RESPONSE_OK);
1197 ans = gtk_dialog_run(GTK_DIALOG(box));
1198 gtk_widget_destroy(box);
1200 if (ans != GTK_RESPONSE_OK)
1202 g_free(link_path);
1203 return FALSE;
1206 unlink(path);
1209 err = symlink(link_path, path);
1210 g_free(link_path);
1212 if (err)
1214 report_error("symlink: %s", g_strerror(errno));
1215 return FALSE;
1218 dir_check_this(path);
1220 return TRUE;
1223 static void run_action(DirItem *item)
1225 if (can_set_run_action(item))
1226 type_set_handler_dialog(item->mime_type);
1227 else
1228 report_error(
1229 _("You can only set the run action for a "
1230 "regular file"));
1233 void open_home(gpointer data, guint action, GtkWidget *widget)
1235 filer_opendir(home_dir, NULL, NULL);
1238 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1240 gchar *path;
1242 path = g_strconcat(filer_window->sym_path,
1243 "/", item->leafname, "#", NULL);
1245 filer_change_to(filer_window, path, NULL);
1246 g_free(path);
1249 static void select_all(gpointer data, guint action, GtkWidget *widget)
1251 g_return_if_fail(window_with_focus != NULL);
1253 window_with_focus->temp_item_selected = FALSE;
1254 view_select_all(window_with_focus->view);
1257 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1259 g_return_if_fail(window_with_focus != NULL);
1261 window_with_focus->temp_item_selected = FALSE;
1262 view_clear_selection(window_with_focus->view);
1265 static gboolean invert_cb(ViewIter *iter, gpointer data)
1267 return !view_get_selected((ViewIface *) data, iter);
1270 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1272 g_return_if_fail(window_with_focus != NULL);
1274 window_with_focus->temp_item_selected = FALSE;
1276 view_select_if(window_with_focus->view, invert_cb,
1277 window_with_focus->view);
1280 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1282 GtkWidget *win;
1284 win = options_show();
1286 if (win)
1288 number_of_windows++;
1289 g_signal_connect(win, "destroy",
1290 G_CALLBACK(one_less_window), NULL);
1294 static gboolean new_directory_cb(GObject *savebox,
1295 const gchar *initial, const gchar *path)
1297 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1299 report_error("mkdir: %s", g_strerror(errno));
1300 return FALSE;
1303 dir_check_this(path);
1305 if (filer_exists(window_with_focus))
1307 guchar *leaf;
1308 leaf = strrchr(path, '/');
1309 if (leaf)
1310 display_set_autoselect(window_with_focus, leaf + 1);
1313 return TRUE;
1316 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1318 g_return_if_fail(window_with_focus != NULL);
1320 savebox_show(_("Create"),
1321 make_path(window_with_focus->sym_path, _("NewDir")),
1322 type_to_icon(inode_directory), new_directory_cb,
1323 GDK_ACTION_COPY);
1326 static gboolean new_file_cb(GObject *savebox,
1327 const gchar *initial, const gchar *path)
1329 int fd;
1331 fd = open(path, O_CREAT | O_EXCL, 0666);
1333 if (fd == -1)
1335 report_error(_("Error creating '%s': %s"),
1336 path, g_strerror(errno));
1337 return FALSE;
1340 if (close(fd))
1341 report_error(_("Error creating '%s': %s"),
1342 path, g_strerror(errno));
1344 dir_check_this(path);
1346 if (filer_exists(window_with_focus))
1348 guchar *leaf;
1349 leaf = strrchr(path, '/');
1350 if (leaf)
1351 display_set_autoselect(window_with_focus, leaf + 1);
1354 return TRUE;
1357 static void new_file(gpointer data, guint action, GtkWidget *widget)
1359 g_return_if_fail(window_with_focus != NULL);
1361 savebox_show(_("Create"),
1362 make_path(window_with_focus->sym_path, _("NewFile")),
1363 type_to_icon(text_plain),
1364 new_file_cb, GDK_ACTION_COPY);
1367 static gboolean new_file_type_cb(GObject *savebox,
1368 const gchar *initial, const gchar *path)
1370 const gchar *oleaf, *leaf;
1371 gchar *templ, *templ_dname, *dest;
1372 GList *paths;
1374 /* We can work out the template path from the initial name */
1375 oleaf = g_basename(initial);
1376 templ_dname = choices_find_path_load("Templates", "");
1377 if (!templ_dname)
1379 report_error(
1380 _("Error creating file: could not find the template for %s"),
1381 oleaf);
1382 return FALSE;
1385 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1386 g_free(templ_dname);
1388 dest = g_path_get_dirname(path);
1389 leaf = g_basename(path);
1390 paths = g_list_append(NULL, templ);
1392 action_copy(paths, dest, leaf, TRUE);
1394 g_list_free(paths);
1395 g_free(dest);
1396 g_free(templ);
1398 if (filer_exists(window_with_focus))
1399 display_set_autoselect(window_with_focus, leaf);
1401 return TRUE;
1404 static void do_send_to(gchar *templ)
1406 g_return_if_fail(send_to_paths != NULL);
1408 run_with_files(templ, send_to_paths);
1411 static void new_file_type(gchar *templ)
1413 const gchar *leaf;
1414 MIME_type *type;
1416 g_return_if_fail(window_with_focus != NULL);
1418 leaf = g_basename(templ);
1419 type = type_get_type(templ);
1421 savebox_show(_("Create"),
1422 make_path(window_with_focus->sym_path, leaf),
1423 type_to_icon(type),
1424 new_file_type_cb, GDK_ACTION_COPY);
1427 static void customise_send_to(gpointer data)
1429 GPtrArray *path;
1430 guchar *save;
1431 GString *dirs;
1432 int i;
1434 dirs = g_string_new(NULL);
1436 path = choices_list_dirs("");
1437 for (i = 0; i < path->len; i++)
1439 guchar *old = (guchar *) path->pdata[i];
1441 g_string_append(dirs, old);
1442 g_string_append(dirs, "SendTo\n");
1444 choices_free_list(path);
1446 save = choices_find_path_save("", "SendTo", TRUE);
1447 if (save)
1448 mkdir(save, 0777);
1450 info_message(
1451 _("The `Send To' menu provides a quick way to send some files "
1452 "to an application. The applications listed are those in "
1453 "the following directories:\n\n%s\n%s\n"
1454 "The `Send To' menu may be opened by Shift+Menu clicking "
1455 "over a file.\n\n"
1456 "Advanced use:\n"
1457 "You can also create subdirectories called "
1458 "`.text_html', `.text', etc which will only be "
1459 "shown for files of that type. `.group' is shown "
1460 "only when multiple files are selected."),
1461 dirs->str,
1462 save ? _("I'll show you your SendTo directory now; you should "
1463 "symlink (Ctrl+Shift drag) any applications you want "
1464 "into it.")
1465 : _("Your CHOICESPATH variable setting prevents "
1466 "customisations - sorry."));
1468 g_string_free(dirs, TRUE);
1470 if (save)
1471 filer_opendir(save, NULL, NULL);
1474 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1475 * to the menu.
1477 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1479 gchar *searchdir;
1480 GPtrArray *paths;
1481 int i;
1483 if (subtype)
1484 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1485 else if (type)
1486 searchdir = g_strdup_printf("SendTo/.%s", type);
1487 else
1488 searchdir = g_strdup("SendTo");
1490 paths = choices_list_dirs(searchdir);
1491 g_free(searchdir);
1493 for (i = 0; i < paths->len; i++)
1495 GList *widgets = NULL;
1496 guchar *dir = (guchar *) paths->pdata[i];
1498 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1499 (CallbackFn) do_send_to,
1500 FALSE, FALSE, TRUE);
1502 if (widgets)
1503 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1504 gtk_menu_item_new());
1506 g_list_free(widgets); /* TODO: Get rid of this */
1509 choices_free_list(paths);
1512 /* Scan the SendTo dir and create and show the Send To menu.
1513 * The 'paths' list and every path in it is claimed, and will be
1514 * freed later -- don't free it yourself!
1516 static void show_send_to_menu(GList *paths, GdkEvent *event)
1518 GtkWidget *menu, *item;
1520 menu = gtk_menu_new();
1522 if (g_list_length(paths) == 1)
1524 DirItem *item;
1526 item = diritem_new("");
1527 diritem_restat(paths->data, item, NULL);
1529 add_sendto(menu,
1530 item->mime_type->media_type,
1531 item->mime_type->subtype);
1533 add_sendto(menu, item->mime_type->media_type, NULL);
1535 diritem_free(item);
1537 else
1538 add_sendto(menu, "group", NULL);
1540 add_sendto(menu, NULL, NULL);
1542 item = gtk_menu_item_new_with_label(_("Customise"));
1543 g_signal_connect_swapped(item, "activate",
1544 G_CALLBACK(customise_send_to), NULL);
1545 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1547 if (send_to_paths)
1548 destroy_glist(&send_to_paths);
1550 send_to_paths = paths;
1552 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1554 popup_menu = menu;
1555 show_popup_menu(menu, event, 0);
1558 static void send_to(FilerWindow *filer_window)
1560 GList *paths;
1561 GdkEvent *event;
1563 paths = filer_selected_items(filer_window);
1564 event = gtk_get_current_event();
1566 /* Eats paths */
1567 show_send_to_menu(paths, event);
1569 gdk_event_free(event);
1572 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1574 const char *argv[] = {"sh", "-c", NULL, NULL};
1575 gboolean close = action;
1577 argv[2] = o_menu_xterm.value;
1579 g_return_if_fail(window_with_focus != NULL);
1581 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1582 gtk_widget_destroy(window_with_focus->window);
1585 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1587 g_return_if_fail(window_with_focus != NULL);
1589 filer_change_to(window_with_focus, home_dir, NULL);
1592 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1594 g_return_if_fail(window_with_focus != NULL);
1596 bookmarks_show_menu(window_with_focus);
1599 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1601 g_return_if_fail(window_with_focus != NULL);
1603 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1604 filer_change_to(window_with_focus,
1605 window_with_focus->real_path, NULL);
1606 else
1607 delayed_error(_("This is already the canonical name "
1608 "for this directory."));
1611 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1613 g_return_if_fail(window_with_focus != NULL);
1615 filer_open_parent(window_with_focus);
1618 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1620 g_return_if_fail(window_with_focus != NULL);
1622 change_to_parent(window_with_focus);
1625 static void resize(gpointer data, guint action, GtkWidget *widget)
1627 g_return_if_fail(window_with_focus != NULL);
1629 view_autosize(window_with_focus->view);
1632 static void new_window(gpointer data, guint action, GtkWidget *widget)
1634 g_return_if_fail(window_with_focus != NULL);
1636 if (o_unique_filer_windows.int_value)
1638 report_error(_("You can't open a second view onto "
1639 "this directory because the `Unique Windows' option "
1640 "is turned on in the Options window."));
1642 else
1643 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1646 static void close_window(gpointer data, guint action, GtkWidget *widget)
1648 g_return_if_fail(window_with_focus != NULL);
1650 if (!filer_window_delete(window_with_focus->window, NULL,
1651 window_with_focus))
1652 gtk_widget_destroy(window_with_focus->window);
1655 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1657 MiniType type = (MiniType) action;
1659 g_return_if_fail(window_with_focus != NULL);
1661 /* Item needs to remain selected... */
1662 if (type == MINI_SHELL)
1663 window_with_focus->temp_item_selected = FALSE;
1665 minibuffer_show(window_with_focus, type);
1668 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1670 if (action == HELP_ABOUT)
1671 infobox_new(app_dir);
1672 else if (action == HELP_DIR)
1673 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1674 else if (action == HELP_MANUAL)
1676 gchar *manual = NULL;
1678 if (current_lang)
1680 manual = g_strconcat(app_dir, "/Help/Manual-",
1681 current_lang, ".html", NULL);
1682 if (access(manual, F_OK))
1683 null_g_free(&manual);
1686 if (!manual)
1687 manual = g_strconcat(app_dir,
1688 "/Help/Manual.html", NULL);
1690 run_by_path(manual);
1692 g_free(manual);
1694 else
1695 g_warning("Unknown help action %d\n", action);
1698 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1699 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1701 GList *items, *item;
1703 items = gtk_container_get_children(GTK_CONTAINER(menu));
1705 item = g_list_nth(items, from);
1706 while (item && n--)
1708 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1709 item = item->next;
1711 g_list_free(items);
1714 static void save_menus(void)
1716 char *menurc;
1718 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1719 if (menurc)
1721 gtk_accel_map_save(menurc);
1722 g_free(menurc);
1726 static void select_nth_item(GtkMenuShell *shell, int n)
1728 GList *items;
1729 GtkWidget *item;
1731 items = gtk_container_get_children(GTK_CONTAINER(shell));
1732 item = g_list_nth_data(items, n);
1734 g_return_if_fail(item != NULL);
1736 g_list_free(items);
1738 gtk_menu_shell_select_item(shell, item);
1741 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1743 DirItem *item;
1744 const guchar *path;
1745 int n_selected;
1746 ViewIter iter;
1748 g_return_if_fail(window_with_focus != NULL);
1750 n_selected = view_count_selected(window_with_focus->view);
1752 if (n_selected < 1)
1754 const char *prompt;
1756 switch (action)
1758 case FILE_COPY_ITEM:
1759 prompt = _("Copy ... ?");
1760 break;
1761 case FILE_RENAME_ITEM:
1762 prompt = _("Rename ... ?");
1763 break;
1764 case FILE_LINK_ITEM:
1765 prompt = _("Symlink ... ?");
1766 break;
1767 case FILE_OPEN_FILE:
1768 prompt = _("Shift Open ... ?");
1769 break;
1770 case FILE_PROPERTIES:
1771 prompt = _("Properties of ... ?");
1772 break;
1773 case FILE_SET_TYPE:
1774 prompt = _("Set type of ... ?");
1775 break;
1776 case FILE_RUN_ACTION:
1777 prompt = _("Set run action for ... ?");
1778 break;
1779 case FILE_SET_ICON:
1780 prompt = _("Set icon for ... ?");
1781 break;
1782 case FILE_SEND_TO:
1783 prompt = _("Send ... to ... ?");
1784 break;
1785 case FILE_DELETE:
1786 prompt = _("DELETE ... ?");
1787 break;
1788 case FILE_USAGE:
1789 prompt = _("Count the size of ... ?");
1790 break;
1791 case FILE_CHMOD_ITEMS:
1792 prompt = _("Set permissions on ... ?");
1793 break;
1794 case FILE_FIND:
1795 prompt = _("Search inside ... ?");
1796 break;
1797 case FILE_OPEN_VFS_AVFS:
1798 prompt = _("Look inside ... ?");
1799 break;
1800 default:
1801 g_warning("Unknown action!");
1802 return;
1804 filer_target_mode(window_with_focus, target_callback,
1805 GINT_TO_POINTER(action), prompt);
1806 return;
1809 switch (action)
1811 case FILE_SEND_TO:
1812 send_to(window_with_focus);
1813 return;
1814 case FILE_DELETE:
1815 delete(window_with_focus);
1816 return;
1817 case FILE_USAGE:
1818 usage(window_with_focus);
1819 return;
1820 case FILE_CHMOD_ITEMS:
1821 chmod_items(window_with_focus);
1822 return;
1823 case FILE_SET_TYPE:
1824 set_type_items(window_with_focus);
1825 return;
1826 case FILE_FIND:
1827 find(window_with_focus);
1828 return;
1829 case FILE_PROPERTIES:
1831 GList *items;
1833 items = filer_selected_items(window_with_focus);
1834 infobox_show_list(items);
1835 destroy_glist(&items);
1836 return;
1838 default:
1839 break;
1842 /* All the following actions require exactly one file selected */
1844 if (n_selected > 1)
1846 report_error(_("You cannot do this to more than "
1847 "one item at a time"));
1848 return;
1851 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1853 item = iter.next(&iter);
1854 g_return_if_fail(item != NULL);
1855 /* iter may be passed to filer_openitem... */
1857 if (!item->image)
1858 item = dir_update_item(window_with_focus->directory,
1859 item->leafname);
1861 if (!item)
1863 report_error(_("Item no longer exists!"));
1864 return;
1867 path = make_path(window_with_focus->sym_path, item->leafname);
1869 switch (action)
1871 case FILE_COPY_ITEM:
1872 src_dest_action_item(path, item->image,
1873 _("Copy"), copy_cb,
1874 GDK_ACTION_COPY);
1875 break;
1876 case FILE_RENAME_ITEM:
1877 src_dest_action_item(path, item->image,
1878 _("Rename"), rename_cb,
1879 GDK_ACTION_MOVE);
1880 break;
1881 case FILE_LINK_ITEM:
1882 src_dest_action_item(path, item->image,
1883 _("Symlink"), link_cb,
1884 GDK_ACTION_LINK);
1885 break;
1886 case FILE_OPEN_FILE:
1887 filer_openitem(window_with_focus, &iter,
1888 OPEN_SAME_WINDOW | OPEN_SHIFT);
1889 break;
1890 case FILE_RUN_ACTION:
1891 run_action(item);
1892 break;
1893 case FILE_SET_ICON:
1894 icon_set_handler_dialog(item, path);
1895 break;
1896 case FILE_OPEN_VFS_AVFS:
1897 open_vfs_avfs(window_with_focus, item);
1898 break;
1899 default:
1900 g_warning("Unknown action!");
1901 return;
1905 static void show_key_help(GtkWidget *button, gpointer data)
1907 gboolean can_change_accels;
1909 g_object_get(G_OBJECT(gtk_settings_get_default()),
1910 "gtk-can-change-accels", &can_change_accels,
1911 NULL);
1913 if (!can_change_accels)
1915 info_message(_("User-definable shortcuts are disabled by "
1916 "default in Gtk2, and you have not enabled "
1917 "them. You can turn this feature on by:\n\n"
1918 "1) using an XSettings manager, such as ROX-Session "
1919 "or gnome-settings-daemon, or\n\n"
1920 "2) adding this line to ~/.gtkrc-2.0:\n"
1921 "\tgtk-can-change-accels = 1\n"
1922 "\t(this only works if NOT using XSETTINGS)"));
1923 return;
1926 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1927 "- Open the menu over a filer window,\n"
1928 "- Move the pointer over the item you want to use,\n"
1929 "- Press the key you want attached to it.\n\n"
1930 "The key will appear next to the menu item and you can just press "
1931 "that key without opening the menu in future."));
1934 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1936 GtkWidget *button, *align;
1938 g_return_val_if_fail(option == NULL, NULL);
1940 align = gtk_alignment_new(0, 0.5, 0, 0);
1941 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1942 gtk_container_add(GTK_CONTAINER(align), button);
1943 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1945 return g_list_append(NULL, align);