r2993: Help, Format, Eject and Free Space now appear in the AppMenu section of the
[rox-filer.git] / ROX-Filer / src / menu.c
blob307f5bc1d7d2bae40919cc46243666ac3c111fd7
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_SHOW_FILE_INFO,
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 } FileOp;
80 typedef enum menu_icon_style {
81 MIS_NONE, MIS_SMALL, MIS_LARGE,
82 MIS_HUGE_UNUSED,
83 MIS_CURRENT, /* As per current filer window */
84 MIS_DEFAULT
85 } MenuIconStyle;
87 typedef void (*ActionFn)(GList *paths,
88 const char *dest_dir, const char *leaf, int quiet);
89 typedef void MenuCallback(GtkWidget *widget, gpointer data);
91 typedef gboolean (*SaveCb)(GObject *savebox,
92 const gchar *current, const gchar *new);
94 GtkAccelGroup *filer_keys = NULL;
95 static gboolean filer_keys_need_init = TRUE;
97 static GtkWidget *popup_menu = NULL; /* Currently open menu */
99 static gint updating_menu = 0; /* Non-zero => ignore activations */
100 static GList *send_to_paths = NULL;
102 static Option o_menu_iconsize, o_menu_xterm;
104 /* Static prototypes */
106 static void save_menus(void);
107 static void menu_closed(GtkWidget *widget);
108 static void shade_file_menu_items(gboolean shaded);
109 static void savebox_show(const gchar *action, const gchar *path,
110 MaskedPixmap *image, SaveCb callback,
111 GdkDragAction dnd_action);
112 static gint save_to_file(GObject *savebox,
113 const gchar *pathname, gpointer data);
114 static gboolean action_with_leaf(ActionFn action,
115 const gchar *current, const gchar *new);
116 static gboolean link_cb(GObject *savebox,
117 const gchar *initial, const gchar *path);
118 static void select_nth_item(GtkMenuShell *shell, int n);
119 static void new_file_type(gchar *templ);
120 static void do_send_to(gchar *templ);
121 static void show_send_to_menu(GList *paths, GdkEvent *event);
122 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
124 /* Note that for most of these callbacks none of the arguments are used. */
126 static void view_type(gpointer data, guint action, GtkWidget *widget);
128 /* (action used in these three - DetailsType) */
129 static void change_size(gpointer data, guint action, GtkWidget *widget);
130 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
131 static void set_with(gpointer data, guint action, GtkWidget *widget);
133 static void set_sort(gpointer data, guint action, GtkWidget *widget);
134 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
136 static void hidden(gpointer data, guint action, GtkWidget *widget);
137 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
138 static void refresh(gpointer data, guint action, GtkWidget *widget);
140 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
142 static void select_all(gpointer data, guint action, GtkWidget *widget);
143 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
144 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
145 static void new_directory(gpointer data, guint action, GtkWidget *widget);
146 static void new_file(gpointer data, guint action, GtkWidget *widget);
147 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
149 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
150 static void open_parent(gpointer data, guint action, GtkWidget *widget);
151 static void home_directory(gpointer data, guint action, GtkWidget *widget);
152 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
153 static void new_window(gpointer data, guint action, GtkWidget *widget);
154 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
155 static void close_window(gpointer data, guint action, GtkWidget *widget);
156 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
158 /* (action used in this - MiniType) */
159 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
160 static void resize(gpointer data, guint action, GtkWidget *widget);
162 #define MENUS_NAME "menus2"
164 static GtkWidget *filer_menu; /* The popup filer menu */
165 static GtkWidget *filer_file_item; /* The File '' label */
166 static GtkWidget *filer_file_menu; /* The File '' menu */
167 static GtkWidget *file_shift_item; /* Shift Open label */
168 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
169 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
170 static GtkWidget *filer_reverse_menu; /* The Reversed item */
171 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
172 static GtkWidget *filer_new_window; /* The New Window item */
173 static GtkWidget *filer_new_menu; /* The New submenu */
174 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
176 #undef N_
177 #define N_(x) x
179 static GtkItemFactoryEntry filer_menu_def[] = {
180 {N_("Display"), NULL, NULL, 0, "<Branch>"},
181 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
182 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
183 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
184 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
185 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
186 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
187 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
188 {">", NULL, NULL, 0, "<Separator>"},
189 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
190 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
191 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
192 {">", NULL, NULL, 0, "<Separator>"},
193 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
194 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
195 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
196 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
197 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
198 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
199 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
200 {">", NULL, NULL, 0, "<Separator>"},
201 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
202 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
203 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
204 {N_("File"), NULL, NULL, 0, "<Branch>"},
205 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
206 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
207 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
208 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
209 {">", NULL, NULL, 0, "<Separator>"},
210 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
211 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
212 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
213 {">", NULL, NULL, 0, "<Separator>"},
214 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
215 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
216 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, "<StockItem>", GTK_STOCK_DIALOG_INFO},
217 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
218 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
219 {">", NULL, NULL, 0, "<Separator>"},
220 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
221 {N_("Select"), NULL, NULL, 0, "<Branch>"},
222 {">" N_("Select All"), NULL, select_all, 0, NULL},
223 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
224 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
225 {">" N_("Select If..."), NULL, 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"), NULL, 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"), NULL, resize, 0, NULL},
238 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
240 {">" N_("Close Window"), NULL, 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..."), NULL, mini_buffer, MINI_SHELL, NULL},
244 {">" N_("Xterm Here"), NULL, 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, 6)->data)->child;
306 g_list_free(items);
308 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
309 filer_new_window = GTK_BIN(item)->child;
311 g_signal_connect(filer_menu, "unmap_event",
312 G_CALLBACK(menu_closed), NULL);
313 g_signal_connect(filer_file_menu, "unmap_event",
314 G_CALLBACK(menu_closed), NULL);
316 g_signal_connect(filer_keys, "accel_changed",
317 G_CALLBACK(save_menus), NULL);
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_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, 3);
382 menu_set_items_shaded(filer_file_menu, shaded, 10, 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 /* Returns an array listing all the names in the directory 'path'.
426 * The array is unsorted.
427 * '.' and '..' are skipped.
428 * On error, the error is reported with g_warning and NULL is returned.
430 static GPtrArray *list_dir(const guchar *path)
432 GDir *dir;
433 GError *error = NULL;
434 GPtrArray *names;
435 const char *leaf;
437 dir = g_dir_open(path, 0, &error);
438 if (error)
440 g_warning("Can't list directory:\n%s", error->message);
441 g_error_free(error);
442 return NULL;
445 names = g_ptr_array_new();
447 while ((leaf = g_dir_read_name(dir))) {
448 if (leaf[0] != '.')
449 g_ptr_array_add(names, g_strdup(leaf));
452 g_dir_close(dir);
454 return names;
457 /* Used as the sort function for sorting GPtrArrays */
458 static gint strcmp2(gconstpointer a, gconstpointer b)
460 const char *aa = *(char **) a;
461 const char *bb = *(char **) b;
463 return g_strcasecmp(aa, bb);
466 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
467 MenuIconStyle style, CallbackFn func,
468 gboolean separator, gboolean strip_ext,
469 gboolean recurse)
471 GList *widgets = NULL;
472 DirItem *ditem;
473 int i;
474 GtkWidget *item;
475 char *dname = NULL;
476 GPtrArray *names;
478 dname = pathdup(dir_name);
480 names = list_dir(dname);
481 if (!names)
482 goto out;
484 if (separator)
486 item = gtk_menu_item_new();
487 widgets = g_list_append(widgets, item);
488 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
491 g_ptr_array_sort(names, strcmp2);
493 for (i = 0; i < names->len; i++)
495 char *leaf = names->pdata[i];
496 gchar *fname;
498 fname = g_strconcat(dname, "/", leaf, NULL);
500 /* Strip off extension, if any */
501 if (strip_ext)
503 char *dot;
504 dot = strchr(leaf, '.');
505 if (dot)
506 *dot = '\0';
509 ditem = diritem_new("");
510 diritem_restat(fname, ditem, NULL);
512 if (ditem->image && style != MIS_NONE)
514 GdkPixbuf *pixbuf;
516 switch (style)
518 case MIS_LARGE:
519 pixbuf = ditem->image->pixbuf;
520 break;
521 default:
522 if (!ditem->image->sm_pixbuf)
523 pixmap_make_small(ditem->image);
524 pixbuf = ditem->image->sm_pixbuf;
525 break;
528 item = gtk_image_menu_item_new_with_label(leaf);
529 /* TODO: Find a way to allow short-cuts */
530 menuitem_no_shortcuts(item);
532 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
533 gtk_image_new_from_pixbuf(pixbuf));
535 else
536 item = gtk_menu_item_new_with_label(leaf);
538 g_free(leaf);
540 /* If it is a directory (but NOT an AppDir) and we are
541 * recursing then set up a sub menu.
543 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
544 !(ditem->flags & ITEM_FLAG_APPDIR))
546 GtkWidget *sub;
547 GList *new_widgets;
549 sub = gtk_menu_new();
550 new_widgets = menu_from_dir(sub, fname, style, func,
551 separator, strip_ext, FALSE);
552 g_list_free(new_widgets);
553 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
555 else
556 g_signal_connect_swapped(item, "activate",
557 G_CALLBACK(func), fname);
559 diritem_free(ditem);
561 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
562 g_signal_connect_swapped(item, "destroy",
563 G_CALLBACK(g_free), fname);
565 widgets = g_list_append(widgets, item);
568 g_ptr_array_free(names, TRUE);
569 out:
570 g_free(dname);
572 return widgets;
575 /* Scan the templates dir and create entries for the New menu */
576 static void update_new_files_menu(MenuIconStyle style)
578 static GList *widgets = NULL;
580 gchar *templ_dname = NULL;
582 if (widgets)
584 GList *next;
586 for (next = widgets; next; next = next->next)
587 gtk_widget_destroy((GtkWidget *) next->data);
589 g_list_free(widgets);
590 widgets = NULL;
593 templ_dname = choices_find_path_load("Templates", "");
594 if (templ_dname)
596 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
597 (CallbackFn) new_file_type, TRUE, TRUE,
598 FALSE);
599 g_free(templ_dname);
601 gtk_widget_show_all(filer_new_menu);
604 /* 'item' is the number of the item to appear under the pointer. */
605 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
607 int pos[3];
608 int button = 0;
609 guint32 time = 0;
611 if (event && (event->type == GDK_BUTTON_PRESS ||
612 event->type == GDK_BUTTON_RELEASE))
614 GdkEventButton *bev = (GdkEventButton *) event;
616 pos[0] = bev->x_root;
617 pos[1] = bev->y_root;
618 button = bev->button;
619 time = bev->time;
621 else if (event && event->type == GDK_KEY_PRESS)
623 GdkEventKey *kev = (GdkEventKey *) event;
625 get_pointer_xy(pos, pos + 1);
626 time = kev->time;
628 else
629 get_pointer_xy(pos, pos + 1);
631 pos[2] = item;
633 gtk_widget_show_all(menu);
634 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
635 position_menu, (gpointer) pos, button, time);
636 select_nth_item(GTK_MENU_SHELL(menu), item);
639 /* Hide the popup menu, if any */
640 void menu_popdown(void)
642 if (popup_menu)
643 gtk_menu_popdown(GTK_MENU(popup_menu));
646 static MenuIconStyle get_menu_icon_style(void)
648 MenuIconStyle mis;
649 int display;
651 mis = o_menu_iconsize.int_value;
653 switch (mis)
655 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
656 return mis;
657 default:
658 break;
661 if (mis == MIS_CURRENT && window_with_focus)
663 switch (window_with_focus->display_style)
665 case HUGE_ICONS:
666 case LARGE_ICONS:
667 return MIS_LARGE;
668 case SMALL_ICONS:
669 return MIS_SMALL;
670 default:
671 break;
675 display = o_display_size.int_value;
676 switch (display)
678 case HUGE_ICONS:
679 case LARGE_ICONS:
680 return MIS_LARGE;
681 case SMALL_ICONS:
682 return MIS_SMALL;
683 default:
684 break;
687 return MIS_SMALL;
690 /* iter->peek() is the clicked item, or NULL if none */
691 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
693 DirItem *file_item = NULL;
694 GdkModifierType state = 0;
695 int n_selected;
697 n_selected = view_count_selected(filer_window->view);
699 ensure_filer_menu();
701 updating_menu++;
703 /* Remove previous AppMenu, if any */
704 appmenu_remove();
706 window_with_focus = filer_window;
708 if (!event)
709 event = gtk_get_current_event();
711 if (event->type == GDK_BUTTON_PRESS)
712 state = ((GdkEventButton *) event)->state;
713 else if (event->type == GDK_KEY_PRESS)
714 state = ((GdkEventKey *) event)->state;
716 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
718 filer_window->temp_item_selected = TRUE;
719 view_set_selected(filer_window->view, iter, TRUE);
720 n_selected = view_count_selected(filer_window->view);
722 else
724 filer_window->temp_item_selected = FALSE;
727 /* Short-cut to the Send To menu */
728 if (state & GDK_SHIFT_MASK)
730 GList *paths;
732 updating_menu--;
734 if (n_selected == 0)
736 report_error(
737 _("You should Shift+Menu click over a file to "
738 "send it somewhere"));
739 return;
742 paths = filer_selected_items(filer_window);
744 show_send_to_menu(paths, event); /* (paths eaten) */
746 return;
750 GtkWidget *file_label, *file_menu;
751 GString *buffer;
752 DirItem *item;
754 file_label = filer_file_item;
755 file_menu = filer_file_menu;
756 gtk_check_menu_item_set_active(
757 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
758 filer_window->show_thumbs);
759 gtk_check_menu_item_set_active(
760 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
761 filer_window->show_hidden);
762 gtk_check_menu_item_set_active(
763 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
764 filer_window->sort_order != GTK_SORT_ASCENDING);
765 gtk_check_menu_item_set_active(
766 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
767 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
768 buffer = g_string_new(NULL);
770 switch (n_selected)
772 case 0:
773 g_string_assign(buffer, _("Next Click"));
774 shade_file_menu_items(FALSE);
775 break;
776 case 1:
777 item = filer_selected_item(filer_window);
778 if (!item->image)
779 dir_update_item(filer_window->directory,
780 item->leafname);
781 shade_file_menu_items(FALSE);
782 file_item = filer_selected_item(filer_window);
783 g_string_printf(buffer, _("%s '%s'"),
784 basetype_name(file_item),
785 g_utf8_validate(file_item->leafname,
786 -1, NULL)
787 ? file_item->leafname
788 : _("(bad utf-8)"));
789 if (!can_set_run_action(file_item))
790 menu_set_items_shaded(filer_file_menu,
791 TRUE, 6, 1);
792 break;
793 default:
794 shade_file_menu_items(TRUE);
795 g_string_printf(buffer, _("%d items"),
796 n_selected);
797 break;
799 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
800 g_string_free(buffer, TRUE);
802 menu_show_shift_action(file_shift_item, file_item,
803 n_selected == 0);
804 if (file_item)
805 appmenu_add(make_path(filer_window->sym_path,
806 file_item->leafname),
807 file_item, filer_file_menu);
810 update_new_files_menu(get_menu_icon_style());
812 gtk_widget_set_sensitive(filer_new_window,
813 !o_unique_filer_windows.int_value);
814 gtk_widget_set_sensitive(filer_follow_sym,
815 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
817 popup_menu = (state & GDK_CONTROL_MASK)
818 ? filer_file_menu
819 : filer_menu;
821 updating_menu--;
823 show_popup_menu(popup_menu, event,
824 popup_menu == filer_file_menu ? 5 : 1);
827 static void menu_closed(GtkWidget *widget)
829 if (window_with_focus == NULL || widget != popup_menu)
830 return; /* Close panel item chosen? */
832 popup_menu = NULL;
834 if (window_with_focus->temp_item_selected)
836 view_clear_selection(window_with_focus->view);
837 window_with_focus->temp_item_selected = FALSE;
841 static void target_callback(FilerWindow *filer_window,
842 ViewIter *iter,
843 gpointer action)
845 g_return_if_fail(filer_window != NULL);
847 window_with_focus = filer_window;
849 /* Don't grab the primary selection */
850 filer_window->temp_item_selected = TRUE;
852 view_wink_item(filer_window->view, iter);
853 view_select_only(filer_window->view, iter);
854 file_op(NULL, GPOINTER_TO_INT(action), NULL);
856 view_clear_selection(filer_window->view);
857 filer_window->temp_item_selected = FALSE;
860 /* Set the text of the 'Shift Open...' menu item.
861 * If icon is NULL, reset the text and also shade it, unless 'next'.
863 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
865 guchar *shift_action = NULL;
867 if (item)
869 if (item->flags & ITEM_FLAG_MOUNT_POINT)
871 if (item->flags & ITEM_FLAG_MOUNTED)
872 shift_action = N_("Unmount");
873 else
874 shift_action = N_("Open unmounted");
876 else if (item->flags & ITEM_FLAG_SYMLINK)
877 shift_action = N_("Show Target");
878 else if (item->base_type == TYPE_DIRECTORY)
879 shift_action = N_("Look Inside");
880 else if (item->base_type == TYPE_FILE)
881 shift_action = N_("Open As Text");
883 gtk_label_set_text(GTK_LABEL(menu_item),
884 shift_action ? _(shift_action)
885 : _("Shift Open"));
886 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
889 /* Actions */
891 static void view_type(gpointer data, guint action, GtkWidget *widget)
893 ViewType view_type = (ViewType) action;
895 g_return_if_fail(window_with_focus != NULL);
897 if (view_type == VIEW_TYPE_COLLECTION)
898 display_set_layout(window_with_focus,
899 window_with_focus->display_style_wanted,
900 DETAILS_NONE, FALSE);
902 filer_set_view_type(window_with_focus, (ViewType) action);
905 static void change_size(gpointer data, guint action, GtkWidget *widget)
907 g_return_if_fail(window_with_focus != NULL);
909 display_change_size(window_with_focus, action == 1);
912 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
914 g_return_if_fail(window_with_focus != NULL);
916 if (updating_menu)
917 return;
919 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
920 display_set_layout(window_with_focus,
921 window_with_focus->display_style,
922 window_with_focus->details_type, FALSE);
923 else
924 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
925 window_with_focus->details_type, FALSE);
928 static void set_with(gpointer data, guint action, GtkWidget *widget)
930 DisplayStyle size;
932 g_return_if_fail(window_with_focus != NULL);
934 size = window_with_focus->display_style_wanted;
936 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
937 display_set_layout(window_with_focus, size, action, FALSE);
940 static void set_sort(gpointer data, guint action, GtkWidget *widget)
942 if (updating_menu)
943 return;
945 g_return_if_fail(window_with_focus != NULL);
947 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
950 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
952 GtkSortType order;
954 if (updating_menu)
955 return;
957 g_return_if_fail(window_with_focus != NULL);
959 order = window_with_focus->sort_order;
960 if (order == GTK_SORT_ASCENDING)
961 order = GTK_SORT_DESCENDING;
962 else
963 order = GTK_SORT_ASCENDING;
965 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
966 order);
969 static void hidden(gpointer data, guint action, GtkWidget *widget)
971 if (updating_menu)
972 return;
974 g_return_if_fail(window_with_focus != NULL);
976 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
979 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
981 if (updating_menu)
982 return;
984 g_return_if_fail(window_with_focus != NULL);
986 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
989 static void refresh(gpointer data, guint action, GtkWidget *widget)
991 g_return_if_fail(window_with_focus != NULL);
993 filer_refresh(window_with_focus);
996 static void delete(FilerWindow *filer_window)
998 GList *paths;
999 paths = filer_selected_items(filer_window);
1000 action_delete(paths);
1001 destroy_glist(&paths);
1004 static void usage(FilerWindow *filer_window)
1006 GList *paths;
1007 paths = filer_selected_items(filer_window);
1008 action_usage(paths);
1009 destroy_glist(&paths);
1012 static void chmod_items(FilerWindow *filer_window)
1014 GList *paths;
1015 paths = filer_selected_items(filer_window);
1016 action_chmod(paths, FALSE, NULL);
1017 destroy_glist(&paths);
1020 static void find(FilerWindow *filer_window)
1022 GList *paths;
1023 paths = filer_selected_items(filer_window);
1024 action_find(paths);
1025 destroy_glist(&paths);
1028 /* This creates a new savebox widget, and allows the user to pick a new path
1029 * for the file.
1030 * Once the new path has been picked, the callback will be called with
1031 * both the current and new paths.
1032 * NOTE: This function unrefs 'image'!
1034 static void savebox_show(const gchar *action, const gchar *path,
1035 MaskedPixmap *image, SaveCb callback,
1036 GdkDragAction dnd_action)
1038 GtkWidget *savebox = NULL;
1039 GtkWidget *check_relative = NULL;
1041 g_return_if_fail(image != NULL);
1043 savebox = gtk_savebox_new(action);
1044 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1046 if (callback == link_cb)
1048 check_relative = gtk_check_button_new_with_mnemonic(
1049 _("_Relative link"));
1050 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1051 TRUE);
1053 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1054 gtk_tooltips_set_tip(tooltips, check_relative,
1055 _("If on, the symlink will store the path from the "
1056 "symlink to the target file. Use this if the symlink "
1057 "and the target will be moved together.\n"
1058 "If off, the path from the root directory is stored - "
1059 "use this if the symlink may move but the target will "
1060 "stay put."), NULL);
1061 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1062 check_relative, FALSE, TRUE, 0);
1063 gtk_widget_show(check_relative);
1066 g_signal_connect(savebox, "save_to_file",
1067 G_CALLBACK(save_to_file), NULL);
1069 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1070 g_strdup(path), g_free);
1071 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1072 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1074 gtk_window_set_title(GTK_WINDOW(savebox), action);
1076 if (g_utf8_validate(path, -1, NULL))
1077 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1078 else
1080 gchar *u8, *dir;
1081 dir = g_path_get_dirname(path);
1082 u8 = to_utf8(g_basename(path));
1083 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1084 make_path(dir, u8));
1085 g_free(u8);
1086 g_free(dir);
1088 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1089 g_object_unref(image);
1091 gtk_widget_show(savebox);
1094 static gint save_to_file(GObject *savebox,
1095 const gchar *pathname, gpointer data)
1097 SaveCb callback;
1098 const gchar *current_path;
1100 callback = g_object_get_data(savebox, "action_callback");
1101 current_path = g_object_get_data(savebox, "current_path");
1103 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1104 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1106 return callback(savebox, current_path, pathname)
1107 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1110 static gboolean copy_cb(GObject *savebox,
1111 const gchar *current, const gchar *new)
1113 return action_with_leaf(action_copy, current, new);
1116 static gboolean action_with_leaf(ActionFn action,
1117 const gchar *current, const gchar *new)
1119 const char *leaf;
1120 char *new_dir;
1121 GList *local_paths;
1123 if (new[0] != '/')
1125 report_error(_("New pathname is not absolute"));
1126 return FALSE;
1129 if (new[strlen(new) - 1] == '/')
1131 new_dir = g_strdup(new);
1132 leaf = NULL;
1134 else
1136 const gchar *slash;
1138 slash = strrchr(new, '/');
1139 new_dir = g_strndup(new, slash - new);
1140 leaf = slash + 1;
1143 local_paths = g_list_append(NULL, (gchar *) current);
1144 action(local_paths, new_dir, leaf, -1);
1145 g_list_free(local_paths);
1147 g_free(new_dir);
1149 return TRUE;
1152 /* Open a savebox to act on the selected file.
1153 * Call 'callback' later to perform the operation.
1155 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1156 const gchar *action, SaveCb callback,
1157 GdkDragAction dnd_action)
1159 g_object_ref(image);
1160 savebox_show(action, path, image, callback, dnd_action);
1163 static gboolean rename_cb(GObject *savebox,
1164 const gchar *current, const gchar *new)
1166 return action_with_leaf(action_move, current, new);
1169 static gboolean link_cb(GObject *savebox,
1170 const gchar *initial, const gchar *path)
1172 GtkToggleButton *check_relative;
1173 struct stat info;
1174 int err;
1175 gchar *link_path;
1177 check_relative = g_object_get_data(savebox, "check_relative");
1179 if (gtk_toggle_button_get_active(check_relative))
1180 link_path = get_relative_path(path, initial);
1181 else
1182 link_path = g_strdup(initial);
1184 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1186 GtkWidget *box, *button;
1187 gint ans;
1189 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1190 GTK_BUTTONS_CANCEL,
1191 _("Symlink from '%s' already exists. "
1192 "Replace it with a link to '%s'?"),
1193 path, link_path);
1195 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1197 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1198 gtk_widget_show(button);
1199 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1200 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1201 button, GTK_RESPONSE_OK);
1202 gtk_dialog_set_default_response(GTK_DIALOG(box),
1203 GTK_RESPONSE_OK);
1205 ans = gtk_dialog_run(GTK_DIALOG(box));
1206 gtk_widget_destroy(box);
1208 if (ans != GTK_RESPONSE_OK)
1210 g_free(link_path);
1211 return FALSE;
1214 unlink(path);
1217 err = symlink(link_path, path);
1218 g_free(link_path);
1220 if (err)
1222 report_error("symlink: %s", g_strerror(errno));
1223 return FALSE;
1226 dir_check_this(path);
1228 return TRUE;
1231 static void run_action(DirItem *item)
1233 if (can_set_run_action(item))
1234 type_set_handler_dialog(item->mime_type);
1235 else
1236 report_error(
1237 _("You can only set the run action for a "
1238 "regular file"));
1241 void open_home(gpointer data, guint action, GtkWidget *widget)
1243 filer_opendir(home_dir, NULL, NULL);
1246 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1248 gchar *path;
1250 path = g_strconcat(filer_window->sym_path,
1251 "/", item->leafname, "#", NULL);
1253 filer_change_to(filer_window, path, NULL);
1254 g_free(path);
1257 static void select_all(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_select_all(window_with_focus->view);
1265 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1267 g_return_if_fail(window_with_focus != NULL);
1269 window_with_focus->temp_item_selected = FALSE;
1270 view_clear_selection(window_with_focus->view);
1273 static gboolean invert_cb(ViewIter *iter, gpointer data)
1275 return !view_get_selected((ViewIface *) data, iter);
1278 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1280 g_return_if_fail(window_with_focus != NULL);
1282 window_with_focus->temp_item_selected = FALSE;
1284 view_select_if(window_with_focus->view, invert_cb,
1285 window_with_focus->view);
1288 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1290 GtkWidget *win;
1292 win = options_show();
1294 if (win)
1296 number_of_windows++;
1297 g_signal_connect(win, "destroy",
1298 G_CALLBACK(one_less_window), NULL);
1302 static gboolean new_directory_cb(GObject *savebox,
1303 const gchar *initial, const gchar *path)
1305 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1307 report_error("mkdir: %s", g_strerror(errno));
1308 return FALSE;
1311 dir_check_this(path);
1313 if (filer_exists(window_with_focus))
1315 guchar *leaf;
1316 leaf = strrchr(path, '/');
1317 if (leaf)
1318 display_set_autoselect(window_with_focus, leaf + 1);
1321 return TRUE;
1324 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1326 g_return_if_fail(window_with_focus != NULL);
1328 savebox_show(_("Create"),
1329 make_path(window_with_focus->sym_path, _("NewDir")),
1330 type_to_icon(inode_directory), new_directory_cb,
1331 GDK_ACTION_COPY);
1334 static gboolean new_file_cb(GObject *savebox,
1335 const gchar *initial, const gchar *path)
1337 int fd;
1339 fd = open(path, O_CREAT | O_EXCL, 0666);
1341 if (fd == -1)
1343 report_error(_("Error creating '%s': %s"),
1344 path, g_strerror(errno));
1345 return FALSE;
1348 if (close(fd))
1349 report_error(_("Error creating '%s': %s"),
1350 path, g_strerror(errno));
1352 dir_check_this(path);
1354 if (filer_exists(window_with_focus))
1356 guchar *leaf;
1357 leaf = strrchr(path, '/');
1358 if (leaf)
1359 display_set_autoselect(window_with_focus, leaf + 1);
1362 return TRUE;
1365 static void new_file(gpointer data, guint action, GtkWidget *widget)
1367 g_return_if_fail(window_with_focus != NULL);
1369 savebox_show(_("Create"),
1370 make_path(window_with_focus->sym_path, _("NewFile")),
1371 type_to_icon(text_plain),
1372 new_file_cb, GDK_ACTION_COPY);
1375 static gboolean new_file_type_cb(GObject *savebox,
1376 const gchar *initial, const gchar *path)
1378 const gchar *oleaf, *leaf;
1379 gchar *templ, *templ_dname, *dest;
1380 GList *paths;
1382 /* We can work out the template path from the initial name */
1383 oleaf = g_basename(initial);
1384 templ_dname = choices_find_path_load("Templates", "");
1385 if (!templ_dname)
1387 report_error(
1388 _("Error creating file: could not find the template for %s"),
1389 oleaf);
1390 return FALSE;
1393 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1394 g_free(templ_dname);
1396 dest = g_path_get_dirname(path);
1397 leaf = g_basename(path);
1398 paths = g_list_append(NULL, templ);
1400 action_copy(paths, dest, leaf, TRUE);
1402 g_list_free(paths);
1403 g_free(dest);
1404 g_free(templ);
1406 if (filer_exists(window_with_focus))
1407 display_set_autoselect(window_with_focus, leaf);
1409 return TRUE;
1412 static void do_send_to(gchar *templ)
1414 g_return_if_fail(send_to_paths != NULL);
1416 run_with_files(templ, send_to_paths);
1419 static void new_file_type(gchar *templ)
1421 const gchar *leaf;
1422 MIME_type *type;
1424 g_return_if_fail(window_with_focus != NULL);
1426 leaf = g_basename(templ);
1427 type = type_get_type(templ);
1429 savebox_show(_("Create"),
1430 make_path(window_with_focus->sym_path, leaf),
1431 type_to_icon(type),
1432 new_file_type_cb, GDK_ACTION_COPY);
1435 static void customise_send_to(gpointer data)
1437 GPtrArray *path;
1438 guchar *save;
1439 GString *dirs;
1440 int i;
1442 dirs = g_string_new(NULL);
1444 path = choices_list_dirs("");
1445 for (i = 0; i < path->len; i++)
1447 guchar *old = (guchar *) path->pdata[i];
1449 g_string_append(dirs, old);
1450 g_string_append(dirs, "SendTo\n");
1452 choices_free_list(path);
1454 save = choices_find_path_save("", "SendTo", TRUE);
1455 if (save)
1456 mkdir(save, 0777);
1458 info_message(
1459 _("The `Send To' menu provides a quick way to send some files "
1460 "to an application. The applications listed are those in "
1461 "the following directories:\n\n%s\n%s\n"
1462 "The `Send To' menu may be opened by Shift+Menu clicking "
1463 "over a file.\n\n"
1464 "Advanced use:\n"
1465 "You can also create subdirectories called "
1466 "`.text_html', `.text', etc which will only be "
1467 "shown for files of that type. `.group' is shown "
1468 "only when multiple files are selected."),
1469 dirs->str,
1470 save ? _("I'll show you your SendTo directory now; you should "
1471 "symlink (Ctrl+Shift drag) any applications you want "
1472 "into it.")
1473 : _("Your CHOICESPATH variable setting prevents "
1474 "customisations - sorry."));
1476 g_string_free(dirs, TRUE);
1478 if (save)
1479 filer_opendir(save, NULL, NULL);
1482 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1483 * to the menu.
1485 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1487 gchar *searchdir;
1488 GPtrArray *paths;
1489 int i;
1491 if (subtype)
1492 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1493 else if (type)
1494 searchdir = g_strdup_printf("SendTo/.%s", type);
1495 else
1496 searchdir = g_strdup("SendTo");
1498 paths = choices_list_dirs(searchdir);
1499 g_free(searchdir);
1501 for (i = 0; i < paths->len; i++)
1503 GList *widgets = NULL;
1504 guchar *dir = (guchar *) paths->pdata[i];
1506 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1507 (CallbackFn) do_send_to,
1508 FALSE, FALSE, TRUE);
1510 if (widgets)
1511 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1512 gtk_menu_item_new());
1514 g_list_free(widgets); /* TODO: Get rid of this */
1517 choices_free_list(paths);
1520 /* Scan the SendTo dir and create and show the Send To menu.
1521 * The 'paths' list and every path in it is claimed, and will be
1522 * freed later -- don't free it yourself!
1524 static void show_send_to_menu(GList *paths, GdkEvent *event)
1526 GtkWidget *menu, *item;
1528 menu = gtk_menu_new();
1530 if (g_list_length(paths) == 1)
1532 DirItem *item;
1534 item = diritem_new("");
1535 diritem_restat(paths->data, item, NULL);
1537 add_sendto(menu,
1538 item->mime_type->media_type,
1539 item->mime_type->subtype);
1541 add_sendto(menu, item->mime_type->media_type, NULL);
1543 diritem_free(item);
1545 else
1546 add_sendto(menu, "group", NULL);
1548 add_sendto(menu, NULL, NULL);
1550 item = gtk_menu_item_new_with_label(_("Customise"));
1551 g_signal_connect_swapped(item, "activate",
1552 G_CALLBACK(customise_send_to), NULL);
1553 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1555 if (send_to_paths)
1556 destroy_glist(&send_to_paths);
1558 send_to_paths = paths;
1560 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1562 popup_menu = menu;
1563 show_popup_menu(menu, event, 0);
1566 static void send_to(FilerWindow *filer_window)
1568 GList *paths;
1569 GdkEvent *event;
1571 paths = filer_selected_items(filer_window);
1572 event = gtk_get_current_event();
1574 /* Eats paths */
1575 show_send_to_menu(paths, event);
1577 gdk_event_free(event);
1580 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1582 const char *argv[] = {"sh", "-c", NULL, NULL};
1583 gboolean close = action;
1585 argv[2] = o_menu_xterm.value;
1587 g_return_if_fail(window_with_focus != NULL);
1589 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1590 gtk_widget_destroy(window_with_focus->window);
1593 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1595 g_return_if_fail(window_with_focus != NULL);
1597 filer_change_to(window_with_focus, home_dir, NULL);
1600 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1602 g_return_if_fail(window_with_focus != NULL);
1604 bookmarks_show_menu(window_with_focus);
1607 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1609 g_return_if_fail(window_with_focus != NULL);
1611 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1612 filer_change_to(window_with_focus,
1613 window_with_focus->real_path, NULL);
1614 else
1615 delayed_error(_("This is already the canonical name "
1616 "for this directory."));
1619 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1621 g_return_if_fail(window_with_focus != NULL);
1623 filer_open_parent(window_with_focus);
1626 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1628 g_return_if_fail(window_with_focus != NULL);
1630 change_to_parent(window_with_focus);
1633 static void resize(gpointer data, guint action, GtkWidget *widget)
1635 g_return_if_fail(window_with_focus != NULL);
1637 view_autosize(window_with_focus->view);
1640 static void new_window(gpointer data, guint action, GtkWidget *widget)
1642 g_return_if_fail(window_with_focus != NULL);
1644 if (o_unique_filer_windows.int_value)
1646 report_error(_("You can't open a second view onto "
1647 "this directory because the `Unique Windows' option "
1648 "is turned on in the Options window."));
1650 else
1651 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1654 static void close_window(gpointer data, guint action, GtkWidget *widget)
1656 g_return_if_fail(window_with_focus != NULL);
1658 if (!filer_window_delete(window_with_focus->window, NULL,
1659 window_with_focus))
1660 gtk_widget_destroy(window_with_focus->window);
1663 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1665 MiniType type = (MiniType) action;
1667 g_return_if_fail(window_with_focus != NULL);
1669 /* Item needs to remain selected... */
1670 if (type == MINI_SHELL)
1671 window_with_focus->temp_item_selected = FALSE;
1673 minibuffer_show(window_with_focus, type);
1676 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1678 if (action == HELP_ABOUT)
1679 infobox_new(app_dir);
1680 else if (action == HELP_DIR)
1681 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1682 else if (action == HELP_MANUAL)
1684 gchar *manual = NULL;
1686 if (current_lang)
1688 manual = g_strconcat(app_dir, "/Help/Manual-",
1689 current_lang, ".html", NULL);
1690 if (access(manual, F_OK))
1691 null_g_free(&manual);
1694 if (!manual)
1695 manual = g_strconcat(app_dir,
1696 "/Help/Manual.html", NULL);
1698 run_by_path(manual);
1700 g_free(manual);
1702 else
1703 g_warning("Unknown help action %d\n", action);
1706 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1707 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1709 GList *items, *item;
1711 items = gtk_container_get_children(GTK_CONTAINER(menu));
1713 item = g_list_nth(items, from);
1714 while (item && n--)
1716 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1717 item = item->next;
1719 g_list_free(items);
1722 static void save_menus(void)
1724 char *menurc;
1726 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1727 if (menurc)
1729 gtk_accel_map_save(menurc);
1730 g_free(menurc);
1734 static void select_nth_item(GtkMenuShell *shell, int n)
1736 GList *items;
1737 GtkWidget *item;
1739 items = gtk_container_get_children(GTK_CONTAINER(shell));
1740 item = g_list_nth_data(items, n);
1742 g_return_if_fail(item != NULL);
1744 g_list_free(items);
1746 gtk_menu_shell_select_item(shell, item);
1749 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1751 DirItem *item;
1752 const guchar *path;
1753 int n_selected;
1754 ViewIter iter;
1756 g_return_if_fail(window_with_focus != NULL);
1758 n_selected = view_count_selected(window_with_focus->view);
1760 if (n_selected < 1)
1762 const char *prompt;
1764 switch (action)
1766 case FILE_COPY_ITEM:
1767 prompt = _("Copy ... ?");
1768 break;
1769 case FILE_RENAME_ITEM:
1770 prompt = _("Rename ... ?");
1771 break;
1772 case FILE_LINK_ITEM:
1773 prompt = _("Symlink ... ?");
1774 break;
1775 case FILE_OPEN_FILE:
1776 prompt = _("Shift Open ... ?");
1777 break;
1778 case FILE_SHOW_FILE_INFO:
1779 prompt = _("Examine ... ?");
1780 break;
1781 case FILE_RUN_ACTION:
1782 prompt = _("Set run action for ... ?");
1783 break;
1784 case FILE_SET_ICON:
1785 prompt = _("Set icon for ... ?");
1786 break;
1787 case FILE_SEND_TO:
1788 prompt = _("Send ... to ... ?");
1789 break;
1790 case FILE_DELETE:
1791 prompt = _("DELETE ... ?");
1792 break;
1793 case FILE_USAGE:
1794 prompt = _("Count the size of ... ?");
1795 break;
1796 case FILE_CHMOD_ITEMS:
1797 prompt = _("Set permissions on ... ?");
1798 break;
1799 case FILE_FIND:
1800 prompt = _("Search inside ... ?");
1801 break;
1802 case FILE_OPEN_VFS_AVFS:
1803 prompt = _("Look inside ... ?");
1804 break;
1805 default:
1806 g_warning("Unknown action!");
1807 return;
1809 filer_target_mode(window_with_focus, target_callback,
1810 GINT_TO_POINTER(action), prompt);
1811 return;
1814 switch (action)
1816 case FILE_SEND_TO:
1817 send_to(window_with_focus);
1818 return;
1819 case FILE_DELETE:
1820 delete(window_with_focus);
1821 return;
1822 case FILE_USAGE:
1823 usage(window_with_focus);
1824 return;
1825 case FILE_CHMOD_ITEMS:
1826 chmod_items(window_with_focus);
1827 return;
1828 case FILE_FIND:
1829 find(window_with_focus);
1830 return;
1831 case FILE_SHOW_FILE_INFO:
1833 GList *items;
1835 items = filer_selected_items(window_with_focus);
1836 infobox_show_list(items);
1837 destroy_glist(&items);
1838 return;
1840 default:
1841 break;
1844 /* All the following actions require exactly one file selected */
1846 if (n_selected > 1)
1848 report_error(_("You cannot do this to more than "
1849 "one item at a time"));
1850 return;
1853 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1855 item = iter.next(&iter);
1856 g_return_if_fail(item != NULL);
1857 /* iter may be passed to filer_openitem... */
1859 if (!item->image)
1860 item = dir_update_item(window_with_focus->directory,
1861 item->leafname);
1863 if (!item)
1865 report_error(_("Item no longer exists!"));
1866 return;
1869 path = make_path(window_with_focus->sym_path, item->leafname);
1871 switch (action)
1873 case FILE_COPY_ITEM:
1874 src_dest_action_item(path, item->image,
1875 _("Copy"), copy_cb,
1876 GDK_ACTION_COPY);
1877 break;
1878 case FILE_RENAME_ITEM:
1879 src_dest_action_item(path, item->image,
1880 _("Rename"), rename_cb,
1881 GDK_ACTION_MOVE);
1882 break;
1883 case FILE_LINK_ITEM:
1884 src_dest_action_item(path, item->image,
1885 _("Symlink"), link_cb,
1886 GDK_ACTION_LINK);
1887 break;
1888 case FILE_OPEN_FILE:
1889 filer_openitem(window_with_focus, &iter,
1890 OPEN_SAME_WINDOW | OPEN_SHIFT);
1891 break;
1892 case FILE_RUN_ACTION:
1893 run_action(item);
1894 break;
1895 case FILE_SET_ICON:
1896 icon_set_handler_dialog(item, path);
1897 break;
1898 case FILE_OPEN_VFS_AVFS:
1899 open_vfs_avfs(window_with_focus, item);
1900 break;
1901 default:
1902 g_warning("Unknown action!");
1903 return;
1907 static void show_key_help(GtkWidget *button, gpointer data)
1909 gboolean can_change_accels;
1911 g_object_get(G_OBJECT(gtk_settings_get_default()),
1912 "gtk-can-change-accels", &can_change_accels,
1913 NULL);
1915 if (!can_change_accels)
1917 info_message(_("User-definable shortcuts are disabled by "
1918 "default in Gtk2, and you have not enabled "
1919 "them. You can turn this feature on by:\n"
1920 "1) using an XSettings manager, such as ROX-Session\n"
1921 "or\n"
1922 "2) adding this line to ~/.gtkrc-2.0:\n"
1923 "\tgtk-can-change-accels = 1"));
1924 return;
1927 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1928 "- Open the menu over a filer window,\n"
1929 "- Move the pointer over the item you want to use,\n"
1930 "- Press the key you want attached to it.\n\n"
1931 "The key will appear next to the menu item and you can just press "
1932 "that key without opening the menu in future."));
1935 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1937 GtkWidget *button, *align;
1939 g_return_val_if_fail(option == NULL, NULL);
1941 align = gtk_alignment_new(0, 0.5, 0, 0);
1942 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1943 gtk_container_add(GTK_CONTAINER(align), button);
1944 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1946 return g_list_append(NULL, align);