r2274: Minor fixes.
[rox-filer.git] / ROX-Filer / src / menu.c
blob07eb1f9fe3b44bf486ec28b93ecf244ac947891f
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* menu.c - code for handling the popup menus */
24 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <dirent.h>
35 #include <gtk/gtk.h>
37 #include "global.h"
39 #include "menu.h"
40 #include "run.h"
41 #include "action.h"
42 #include "filer.h"
43 #include "pixmaps.h"
44 #include "type.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "options.h"
48 #include "choices.h"
49 #include "gtksavebox.h"
50 #include "mount.h"
51 #include "minibuffer.h"
52 #include "i18n.h"
53 #include "main.h"
54 #include "pinboard.h"
55 #include "dir.h"
56 #include "diritem.h"
57 #include "appmenu.h"
58 #include "usericons.h"
59 #include "infobox.h"
60 #include "view_iface.h"
61 #include "display.h"
62 #include "bookmarks.h"
64 typedef enum {
65 FILE_COPY_ITEM,
66 FILE_RENAME_ITEM,
67 FILE_LINK_ITEM,
68 FILE_OPEN_FILE,
69 FILE_HELP,
70 FILE_SHOW_FILE_INFO,
71 FILE_RUN_ACTION,
72 FILE_SET_ICON,
73 FILE_SEND_TO,
74 FILE_DELETE,
75 FILE_USAGE,
76 FILE_CHMOD_ITEMS,
77 FILE_FIND,
78 FILE_OPEN_VFS_AVFS,
79 } FileOp;
81 typedef enum menu_icon_style {
82 MIS_NONE, MIS_SMALL, MIS_LARGE,
83 MIS_HUGE_UNUSED,
84 MIS_CURRENT, /* As per current filer window */
85 MIS_DEFAULT
86 } MenuIconStyle;
88 typedef void (*ActionFn)(GList *paths,
89 const char *dest_dir, const char *leaf, int quiet);
90 typedef void MenuCallback(GtkWidget *widget, gpointer data);
92 typedef gboolean (*SaveCb)(GObject *savebox,
93 const gchar *current, const gchar *new);
95 GtkAccelGroup *filer_keys = NULL;
96 static gboolean filer_keys_need_init = TRUE;
98 static GtkWidget *popup_menu = NULL; /* Currently open menu */
100 static gint updating_menu = 0; /* Non-zero => ignore activations */
101 static GList *send_to_paths = NULL;
103 static Option o_menu_iconsize, o_menu_xterm;
105 /* Static prototypes */
107 static void save_menus(void);
108 static void menu_closed(GtkWidget *widget);
109 static void shade_file_menu_items(gboolean shaded);
110 static void savebox_show(const gchar *action, const gchar *path,
111 MaskedPixmap *image, SaveCb callback);
112 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_("Type"), 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, NULL},
188 {">", NULL, NULL, 0, "<Separator>"},
189 {">" N_("Bigger Icons"), NULL, change_size, 1, NULL},
190 {">" N_("Smaller Icons"), NULL, change_size, -1, NULL},
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, NULL},
204 {N_("File"), NULL, NULL, 0, "<Branch>"},
205 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
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, NULL},
209 {">", NULL, NULL, 0, "<Separator>"},
210 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
211 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
212 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
213 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
214 {">", NULL, NULL, 0, "<Separator>"},
215 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
216 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
217 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
218 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
219 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
220 {">", NULL, NULL, 0, "<Separator>"},
221 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
222 {N_("Select"), NULL, NULL, 0, "<Branch>"},
223 {">" N_("Select All"), NULL, select_all, 0, NULL},
224 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
225 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
226 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
227 {N_("Options..."), NULL, menu_show_options, 0, NULL},
228 {N_("New"), NULL, NULL, 0, "<Branch>"},
229 {">" N_("Directory"), NULL, new_directory, 0, NULL},
230 {">" N_("Blank file"), NULL, new_file, 0, NULL},
231 {N_("Window"), NULL, NULL, 0, "<Branch>"},
232 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
233 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
234 {">" N_("New Window"), NULL, new_window, 0, NULL},
235 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
236 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, NULL},
237 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
238 {">" N_("Resize Window"), NULL, resize, 0, NULL},
239 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
241 {">" N_("Close Window"), NULL, close_window, 0, NULL},
242 {">", NULL, NULL, 0, "<Separator>"},
243 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
244 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
245 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
246 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
247 {N_("Help"), NULL, NULL, 0, "<Branch>"},
248 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
249 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, NULL},
250 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
254 #define GET_MENU_ITEM(var, menu) \
255 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
257 #define GET_SMENU_ITEM(var, menu, sub) \
258 do { \
259 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
260 var = gtk_item_factory_get_widget(item_factory, tmp); \
261 g_free(tmp); \
262 } while (0)
264 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
265 do { \
266 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
267 var = gtk_item_factory_get_widget(item_factory, tmp); \
268 g_free(tmp); \
269 } while (0)
271 /* Returns TRUE if the keys were installed (first call only) */
272 gboolean ensure_filer_menu(void)
274 GList *items;
275 guchar *tmp;
276 GtkWidget *item;
277 GtkItemFactory *item_factory;
279 if (!filer_keys_need_init)
280 return FALSE;
281 filer_keys_need_init = FALSE;
283 item_factory = menu_create(filer_menu_def,
284 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
285 "<filer>", filer_keys);
287 GET_MENU_ITEM(filer_menu, "filer");
288 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
289 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
290 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
291 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
292 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
293 "Show Thumbnails");
295 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
296 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
297 filer_follow_sym = GTK_BIN(item)->child;
299 /* File '' label... */
300 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
301 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
302 g_list_free(items);
304 /* Shift Open... label */
305 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
306 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
307 g_list_free(items);
309 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
310 filer_new_window = GTK_BIN(item)->child;
312 g_signal_connect(filer_menu, "unmap_event",
313 G_CALLBACK(menu_closed), NULL);
314 g_signal_connect(filer_file_menu, "unmap_event",
315 G_CALLBACK(menu_closed), NULL);
317 g_signal_connect(filer_keys, "accel_changed",
318 G_CALLBACK(save_menus), NULL);
320 return TRUE;
323 void menu_init(void)
325 char *menurc;
327 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
328 if (menurc)
330 gtk_accel_map_load(menurc);
331 g_free(menurc);
334 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
335 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
336 option_add_saver(save_menus);
338 option_register_widget("menu-set-keys", set_keys_button);
340 filer_keys = gtk_accel_group_new();
343 /* Name is in the form "<panel>" */
344 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
345 const gchar *name, GtkAccelGroup *keys)
347 GtkItemFactory *item_factory;
348 GtkItemFactoryEntry *translated;
350 if (!keys)
352 keys = gtk_accel_group_new();
353 gtk_accel_group_lock(keys);
356 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
358 translated = translate_entries(def, n_entries);
359 gtk_item_factory_create_items(item_factory, n_entries,
360 translated, NULL);
361 free_translated_entries(translated, n_entries);
363 return item_factory;
366 /* Prevent the user from setting a short-cut on this item */
367 static void menuitem_no_shortcuts(GtkWidget *item)
369 /* XXX */
370 #if 0
371 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
373 _gtk_widget_set_accel_path(item, NULL, NULL);
374 null_g_free(&menuitem->accel_path);
375 #endif
378 /* Shade items that only work on single files */
379 static void shade_file_menu_items(gboolean shaded)
381 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
382 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
383 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
386 /* 'data' is an array of three ints:
387 * [ pointer_x, pointer_y, item_under_pointer ]
389 void position_menu(GtkMenu *menu, gint *x, gint *y,
390 gboolean *push_in, gpointer data)
392 int *pos = (int *) data;
393 GtkRequisition requisition;
394 GList *items, *next;
395 int y_shift = 0;
396 int item = pos[2];
398 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
400 while (item >= 0 && next)
402 int h = ((GtkWidget *) next->data)->requisition.height;
404 if (item > 0)
405 y_shift += h;
406 else
407 y_shift += h / 2;
409 next = next->next;
410 item--;
413 g_list_free(items);
415 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
417 *x = pos[0] - (requisition.width * 7 / 8);
418 *y = pos[1] - y_shift;
420 *x = CLAMP(*x, 0, screen_width - requisition.width);
421 *y = CLAMP(*y, 0, screen_height - requisition.height);
423 *push_in = FALSE;
426 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
427 MenuIconStyle style, CallbackFn func,
428 gboolean separator, gboolean strip_ext,
429 gboolean recurse)
431 GList *widgets = NULL;
432 DirItem *ditem;
433 DIR *dir;
434 struct dirent *ent;
435 GtkWidget *item;
436 char *dname = NULL;
438 dname = pathdup(dir_name);
440 dir = opendir(dname);
441 if (!dir)
442 goto out;
444 if (separator)
446 item = gtk_menu_item_new();
447 widgets = g_list_append(widgets, item);
448 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
451 while ((ent = readdir(dir)))
453 char *dot, *leaf;
454 GtkWidget *hbox;
455 GtkWidget *img;
456 GtkWidget *label;
457 gchar *fname;
459 /* Ignore hidden files */
460 if (ent->d_name[0] == '.')
461 continue;
463 /* Strip off extension, if any */
464 dot = strchr(ent->d_name, '.');
465 if (strip_ext && dot)
466 leaf = g_strndup(ent->d_name, dot - ent->d_name);
467 else
468 leaf = g_strdup(ent->d_name);
470 fname = g_strconcat(dname, "/", ent->d_name, NULL);
471 ditem = diritem_new("");
472 diritem_restat(fname, ditem, NULL);
474 if (ditem->image && style != MIS_NONE)
476 GdkPixbuf *pixbuf;
478 switch (style) {
479 case MIS_LARGE:
480 pixbuf = ditem->image->pixbuf;
481 break;
482 default:
483 if (!ditem->image->sm_pixbuf)
484 pixmap_make_small(ditem->image);
485 pixbuf = ditem->image->sm_pixbuf;
486 break;
489 item = gtk_menu_item_new();
490 /* TODO: Find a way to allow short-cuts */
491 menuitem_no_shortcuts(item);
493 hbox = gtk_hbox_new(FALSE, 2);
494 gtk_container_add(GTK_CONTAINER(item), hbox);
496 img = gtk_image_new_from_pixbuf(pixbuf);
498 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
500 label = gtk_label_new(leaf);
501 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
502 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
505 else
506 item = gtk_menu_item_new_with_label(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);
528 g_free(leaf);
530 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
531 g_signal_connect_swapped(item, "destroy",
532 G_CALLBACK(g_free), fname);
534 widgets = g_list_append(widgets, item);
537 closedir(dir);
538 out:
539 g_free(dname);
541 return widgets;
544 /* Scan the templates dir and create entries for the New menu */
545 static void update_new_files_menu(MenuIconStyle style)
547 static GList *widgets = NULL;
549 gchar *templ_dname = NULL;
551 if (widgets)
553 GList *next;
555 for (next = widgets; next; next = next->next)
556 gtk_widget_destroy((GtkWidget *) next->data);
558 g_list_free(widgets);
559 widgets = NULL;
562 templ_dname = choices_find_path_load("Templates", "");
563 if (templ_dname)
565 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
566 (CallbackFn) new_file_type, TRUE, TRUE,
567 FALSE);
568 g_free(templ_dname);
570 gtk_widget_show_all(filer_new_menu);
573 /* 'item' is the number of the item to appear under the pointer. */
574 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
576 int pos[3];
577 int button = 0;
578 guint32 time = 0;
580 if (event && (event->type == GDK_BUTTON_PRESS ||
581 event->type == GDK_BUTTON_RELEASE))
583 GdkEventButton *bev = (GdkEventButton *) event;
585 pos[0] = bev->x_root;
586 pos[1] = bev->y_root;
587 button = bev->button;
588 time = bev->time;
590 else if (event && event->type == GDK_KEY_PRESS)
592 GdkEventKey *kev = (GdkEventKey *) event;
594 get_pointer_xy(pos, pos + 1);
595 time = kev->time;
597 else
598 get_pointer_xy(pos, pos + 1);
600 pos[2] = item;
602 gtk_widget_show_all(menu);
603 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
604 position_menu, (gpointer) pos, button, time);
605 select_nth_item(GTK_MENU_SHELL(menu), item);
608 /* Hide the popup menu, if any */
609 void menu_popdown(void)
611 if (popup_menu)
612 gtk_menu_popdown(GTK_MENU(popup_menu));
615 static MenuIconStyle get_menu_icon_style(void)
617 MenuIconStyle mis;
618 int display;
620 mis = o_menu_iconsize.int_value;
622 switch (mis)
624 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
625 return mis;
626 default:
627 break;
630 if (mis == MIS_CURRENT && window_with_focus)
632 switch (window_with_focus->display_style)
634 case HUGE_ICONS:
635 case LARGE_ICONS:
636 return MIS_LARGE;
637 case SMALL_ICONS:
638 return MIS_SMALL;
639 default:
640 break;
644 display = o_display_size.int_value;
645 switch (display)
647 case HUGE_ICONS:
648 case LARGE_ICONS:
649 return MIS_LARGE;
650 case SMALL_ICONS:
651 return MIS_SMALL;
652 default:
653 break;
656 return MIS_SMALL;
659 /* iter->peek() is the clicked item, or NULL if none */
660 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
662 DirItem *file_item = NULL;
663 GdkModifierType state = 0;
664 int n_selected;
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, 6, 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 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 popup_menu = (state & GDK_CONTROL_MASK)
787 ? filer_file_menu
788 : filer_menu;
790 updating_menu--;
792 show_popup_menu(popup_menu, event,
793 popup_menu == filer_file_menu ? 5 : 1);
796 static void menu_closed(GtkWidget *widget)
798 if (window_with_focus == NULL || widget != popup_menu)
799 return; /* Close panel item chosen? */
801 popup_menu = NULL;
803 if (window_with_focus->temp_item_selected)
805 view_clear_selection(window_with_focus->view);
806 window_with_focus->temp_item_selected = FALSE;
810 static void target_callback(FilerWindow *filer_window,
811 ViewIter *iter,
812 gpointer action)
814 g_return_if_fail(filer_window != NULL);
816 window_with_focus = filer_window;
818 /* Don't grab the primary selection */
819 filer_window->temp_item_selected = TRUE;
821 view_wink_item(filer_window->view, iter);
822 view_select_only(filer_window->view, iter);
823 file_op(NULL, GPOINTER_TO_INT(action), NULL);
825 view_clear_selection(filer_window->view);
826 filer_window->temp_item_selected = FALSE;
829 /* Set the text of the 'Shift Open...' menu item.
830 * If icon is NULL, reset the text and also shade it, unless 'next'.
832 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
834 guchar *shift_action = NULL;
836 if (item)
838 if (item->flags & ITEM_FLAG_MOUNT_POINT)
840 if (item->flags & ITEM_FLAG_MOUNTED)
841 shift_action = N_("Unmount");
842 else
843 shift_action = N_("Open unmounted");
845 else if (item->flags & ITEM_FLAG_SYMLINK)
846 shift_action = N_("Show Target");
847 else if (item->base_type == TYPE_DIRECTORY)
848 shift_action = N_("Look Inside");
849 else if (item->base_type == TYPE_FILE)
850 shift_action = N_("Open As Text");
852 gtk_label_set_text(GTK_LABEL(menu_item),
853 shift_action ? _(shift_action)
854 : _("Shift Open"));
855 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
858 /* Actions */
860 static void view_type(gpointer data, guint action, GtkWidget *widget)
862 ViewType view_type = (ViewType) action;
864 g_return_if_fail(window_with_focus != NULL);
866 if (view_type == VIEW_TYPE_COLLECTION)
867 display_set_layout(window_with_focus,
868 window_with_focus->display_style_wanted,
869 DETAILS_NONE, FALSE);
871 filer_set_view_type(window_with_focus, (ViewType) action);
874 static void change_size(gpointer data, guint action, GtkWidget *widget)
876 g_return_if_fail(window_with_focus != NULL);
878 display_change_size(window_with_focus, action == 1);
881 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
883 g_return_if_fail(window_with_focus != NULL);
885 if (updating_menu)
886 return;
888 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
889 display_set_layout(window_with_focus,
890 window_with_focus->display_style,
891 window_with_focus->details_type, FALSE);
892 else
893 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
894 window_with_focus->details_type, FALSE);
897 static void set_with(gpointer data, guint action, GtkWidget *widget)
899 DisplayStyle size;
901 g_return_if_fail(window_with_focus != NULL);
903 size = window_with_focus->display_style_wanted;
905 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
906 display_set_layout(window_with_focus, size, action, FALSE);
909 static void set_sort(gpointer data, guint action, GtkWidget *widget)
911 if (updating_menu)
912 return;
914 g_return_if_fail(window_with_focus != NULL);
916 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
919 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
921 GtkSortType order;
923 if (updating_menu)
924 return;
926 g_return_if_fail(window_with_focus != NULL);
928 order = window_with_focus->sort_order;
929 if (order == GTK_SORT_ASCENDING)
930 order = GTK_SORT_DESCENDING;
931 else
932 order = GTK_SORT_ASCENDING;
934 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
935 order);
938 static void hidden(gpointer data, guint action, GtkWidget *widget)
940 if (updating_menu)
941 return;
943 g_return_if_fail(window_with_focus != NULL);
945 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
948 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
950 if (updating_menu)
951 return;
953 g_return_if_fail(window_with_focus != NULL);
955 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
958 static void refresh(gpointer data, guint action, GtkWidget *widget)
960 g_return_if_fail(window_with_focus != NULL);
962 full_refresh();
963 filer_update_dir(window_with_focus, TRUE);
966 static void delete(FilerWindow *filer_window)
968 GList *paths;
969 paths = filer_selected_items(filer_window);
970 action_delete(paths);
971 destroy_glist(&paths);
974 static void usage(FilerWindow *filer_window)
976 GList *paths;
977 paths = filer_selected_items(filer_window);
978 action_usage(paths);
979 destroy_glist(&paths);
982 static void chmod_items(FilerWindow *filer_window)
984 GList *paths;
985 paths = filer_selected_items(filer_window);
986 action_chmod(paths, FALSE, NULL);
987 destroy_glist(&paths);
990 static void find(FilerWindow *filer_window)
992 GList *paths;
993 paths = filer_selected_items(filer_window);
994 action_find(paths);
995 destroy_glist(&paths);
998 /* This creates a new savebox widget, and allows the user to pick a new path
999 * for the file.
1000 * Once the new path has been picked, the callback will be called with
1001 * both the current and new paths.
1002 * NOTE: This function unrefs 'image'!
1004 static void savebox_show(const gchar *action, const gchar *path,
1005 MaskedPixmap *image, SaveCb callback)
1007 GtkWidget *savebox = NULL;
1008 GtkWidget *check_relative = NULL;
1010 g_return_if_fail(image != NULL);
1012 savebox = gtk_savebox_new(action);
1014 if (callback == link_cb)
1016 check_relative = gtk_check_button_new_with_mnemonic(
1017 _("_Relative link"));
1018 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1019 TRUE);
1021 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1022 gtk_tooltips_set_tip(tooltips, check_relative,
1023 _("If on, the symlink will store the path from the "
1024 "symlink to the target file. Use this if the symlink "
1025 "and the target will be moved together.\n"
1026 "If off, the path from the root directory is stored - "
1027 "use this if the symlink may move but the target will "
1028 "stay put."), NULL);
1029 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1030 check_relative, FALSE, TRUE, 0);
1031 gtk_widget_show(check_relative);
1034 g_signal_connect(savebox, "save_to_file",
1035 G_CALLBACK(save_to_file), NULL);
1037 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1038 g_strdup(path), g_free);
1039 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1040 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1042 gtk_window_set_title(GTK_WINDOW(savebox), action);
1044 if (g_utf8_validate(path, -1, NULL))
1045 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1046 else
1048 gchar *u8;
1049 u8 = to_utf8(path);
1050 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1051 g_free(u8);
1053 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1054 g_object_unref(image);
1056 gtk_widget_show(savebox);
1059 static gint save_to_file(GObject *savebox,
1060 const gchar *pathname, gpointer data)
1062 SaveCb callback;
1063 const gchar *current_path;
1065 callback = g_object_get_data(savebox, "action_callback");
1066 current_path = g_object_get_data(savebox, "current_path");
1068 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1069 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1071 return callback(savebox, current_path, pathname)
1072 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1075 static gboolean copy_cb(GObject *savebox,
1076 const gchar *current, const gchar *new)
1078 return action_with_leaf(action_copy, current, new);
1081 static gboolean action_with_leaf(ActionFn action,
1082 const gchar *current, const gchar *new)
1084 const char *leaf;
1085 char *new_dir;
1086 GList *local_paths;
1088 if (new[0] != '/')
1090 report_error(_("New pathname is not absolute"));
1091 return FALSE;
1094 if (new[strlen(new) - 1] == '/')
1096 new_dir = g_strdup(new);
1097 leaf = NULL;
1099 else
1101 const gchar *slash;
1103 slash = strrchr(new, '/');
1104 new_dir = g_strndup(new, slash - new);
1105 leaf = slash + 1;
1108 local_paths = g_list_append(NULL, (gchar *) current);
1109 action(local_paths, new_dir, leaf, -1);
1110 g_list_free(local_paths);
1112 g_free(new_dir);
1114 return TRUE;
1117 /* Open a savebox to act on the selected file.
1118 * Call 'callback' later to perform the operation.
1120 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1121 const gchar *action, SaveCb callback)
1123 g_object_ref(image);
1124 savebox_show(action, path, image, callback);
1127 static gboolean rename_cb(GObject *savebox,
1128 const gchar *current, const gchar *new)
1130 return action_with_leaf(action_move, current, new);
1133 static gboolean link_cb(GObject *savebox,
1134 const gchar *initial, const gchar *path)
1136 GtkToggleButton *check_relative;
1137 struct stat info;
1138 int err;
1139 gchar *link_path;
1141 check_relative = g_object_get_data(savebox, "check_relative");
1143 if (gtk_toggle_button_get_active(check_relative))
1144 link_path = get_relative_path(path, initial);
1145 else
1146 link_path = g_strdup(initial);
1148 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1150 GtkWidget *box, *button;
1151 gint ans;
1153 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1154 GTK_BUTTONS_CANCEL,
1155 _("Symlink from '%s' already exists. "
1156 "Replace it with a link to '%s'?"),
1157 path, link_path);
1159 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1161 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1162 gtk_widget_show(button);
1163 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1164 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1165 button, GTK_RESPONSE_OK);
1166 gtk_dialog_set_default_response(GTK_DIALOG(box),
1167 GTK_RESPONSE_OK);
1169 ans = gtk_dialog_run(GTK_DIALOG(box));
1170 gtk_widget_destroy(box);
1172 if (ans != GTK_RESPONSE_OK)
1174 g_free(link_path);
1175 return FALSE;
1178 unlink(path);
1181 err = symlink(link_path, path);
1182 g_free(link_path);
1184 if (err)
1186 report_error("symlink: %s", g_strerror(errno));
1187 return FALSE;
1190 dir_check_this(path);
1192 return TRUE;
1195 static void run_action(DirItem *item)
1197 if (can_set_run_action(item))
1198 type_set_handler_dialog(item->mime_type);
1199 else
1200 report_error(
1201 _("You can only set the run action for a "
1202 "regular file"));
1205 void open_home(gpointer data, guint action, GtkWidget *widget)
1207 filer_opendir(home_dir, NULL, NULL);
1210 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1212 gchar *path;
1214 path = g_strconcat(filer_window->sym_path,
1215 "/", item->leafname, "#", NULL);
1217 filer_change_to(filer_window, path, NULL);
1218 g_free(path);
1221 static void select_all(gpointer data, guint action, GtkWidget *widget)
1223 g_return_if_fail(window_with_focus != NULL);
1225 window_with_focus->temp_item_selected = FALSE;
1226 view_select_all(window_with_focus->view);
1229 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1231 g_return_if_fail(window_with_focus != NULL);
1233 window_with_focus->temp_item_selected = FALSE;
1234 view_clear_selection(window_with_focus->view);
1237 static gboolean invert_cb(ViewIter *iter, gpointer data)
1239 return !view_get_selected((ViewIface *) data, iter);
1242 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1244 g_return_if_fail(window_with_focus != NULL);
1246 window_with_focus->temp_item_selected = FALSE;
1248 view_select_if(window_with_focus->view, invert_cb,
1249 window_with_focus->view);
1252 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1254 GtkWidget *win;
1256 win = options_show();
1258 if (win)
1260 number_of_windows++;
1261 g_signal_connect(win, "destroy",
1262 G_CALLBACK(one_less_window), NULL);
1266 static gboolean new_directory_cb(GObject *savebox,
1267 const gchar *initial, const gchar *path)
1269 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1271 report_error("mkdir: %s", g_strerror(errno));
1272 return FALSE;
1275 dir_check_this(path);
1277 if (filer_exists(window_with_focus))
1279 guchar *leaf;
1280 leaf = strrchr(path, '/');
1281 if (leaf)
1282 display_set_autoselect(window_with_focus, leaf + 1);
1285 return TRUE;
1288 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1290 g_return_if_fail(window_with_focus != NULL);
1292 savebox_show(_("Create"),
1293 make_path(window_with_focus->sym_path, _("NewDir")),
1294 type_to_icon(inode_directory), new_directory_cb);
1297 static gboolean new_file_cb(GObject *savebox,
1298 const gchar *initial, const gchar *path)
1300 int fd;
1302 fd = open(path, O_CREAT | O_EXCL, 0666);
1304 if (fd == -1)
1306 report_error(_("Error creating '%s': %s"),
1307 path, g_strerror(errno));
1308 return FALSE;
1311 if (close(fd))
1312 report_error(_("Error creating '%s': %s"),
1313 path, g_strerror(errno));
1315 dir_check_this(path);
1317 if (filer_exists(window_with_focus))
1319 guchar *leaf;
1320 leaf = strrchr(path, '/');
1321 if (leaf)
1322 display_set_autoselect(window_with_focus, leaf + 1);
1325 return TRUE;
1328 static void new_file(gpointer data, guint action, GtkWidget *widget)
1330 g_return_if_fail(window_with_focus != NULL);
1332 savebox_show(_("Create"),
1333 make_path(window_with_focus->sym_path, _("NewFile")),
1334 type_to_icon(text_plain),
1335 new_file_cb);
1338 static gboolean new_file_type_cb(GObject *savebox,
1339 const gchar *initial, const gchar *path)
1341 const gchar *oleaf, *leaf;
1342 gchar *templ, *templ_dname, *dest;
1343 GList *paths;
1345 /* We can work out the template path from the initial name */
1346 oleaf = g_basename(initial);
1347 templ_dname = choices_find_path_load("Templates", "");
1348 if (!templ_dname)
1350 report_error(
1351 _("Error creating file: could not find the template for %s"),
1352 oleaf);
1353 return FALSE;
1356 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1357 g_free(templ_dname);
1359 dest = g_path_get_dirname(path);
1360 leaf = g_basename(path);
1361 paths = g_list_append(NULL, templ);
1363 action_copy(paths, dest, leaf, TRUE);
1365 g_list_free(paths);
1366 g_free(dest);
1367 g_free(templ);
1369 if (filer_exists(window_with_focus))
1370 display_set_autoselect(window_with_focus, leaf);
1372 return TRUE;
1375 static void do_send_to(gchar *templ)
1377 g_return_if_fail(send_to_paths != NULL);
1379 run_with_files(templ, send_to_paths);
1382 static void new_file_type(gchar *templ)
1384 const gchar *leaf;
1385 MIME_type *type;
1387 g_return_if_fail(window_with_focus != NULL);
1389 leaf = g_basename(templ);
1390 type = type_get_type(templ);
1392 savebox_show(_("Create"),
1393 make_path(window_with_focus->sym_path, leaf),
1394 type_to_icon(type),
1395 new_file_type_cb);
1398 static void customise_send_to(gpointer data)
1400 GPtrArray *path;
1401 guchar *save;
1402 GString *dirs;
1403 int i;
1405 dirs = g_string_new(NULL);
1407 path = choices_list_dirs("");
1408 for (i = 0; i < path->len; i++)
1410 guchar *old = (guchar *) path->pdata[i];
1412 g_string_append(dirs, old);
1413 g_string_append(dirs, "SendTo\n");
1415 choices_free_list(path);
1417 save = choices_find_path_save("", "SendTo", TRUE);
1418 if (save)
1419 mkdir(save, 0777);
1421 info_message(
1422 _("The `Send To' menu provides a quick way to send some files "
1423 "to an application. The applications listed are those in "
1424 "the following directories:\n\n%s\n%s\n"
1425 "The `Send To' menu may be opened by Shift+Menu clicking "
1426 "over a file.\n\n"
1427 "Advanced use:\n"
1428 "You can also create subdirectories called "
1429 "`.text_html', `.text', etc which will only be "
1430 "shown for files of that type. `.group' is shown "
1431 "only when multiple files are selected."),
1432 dirs->str,
1433 save ? _("I'll show you your SendTo directory now; you should "
1434 "symlink (Ctrl+Shift drag) any applications you want "
1435 "into it.")
1436 : _("Your CHOICESPATH variable setting prevents "
1437 "customisations - sorry."));
1439 g_string_free(dirs, TRUE);
1441 if (save)
1442 filer_opendir(save, NULL, NULL);
1445 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1446 * to the menu.
1448 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1450 gchar *searchdir;
1451 GPtrArray *paths;
1452 int i;
1454 if (subtype)
1455 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1456 else if (type)
1457 searchdir = g_strdup_printf("SendTo/.%s", type);
1458 else
1459 searchdir = g_strdup("SendTo");
1461 paths = choices_list_dirs(searchdir);
1462 g_free(searchdir);
1464 for (i = 0; i < paths->len; i++)
1466 GList *widgets = NULL;
1467 guchar *dir = (guchar *) paths->pdata[i];
1469 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1470 (CallbackFn) do_send_to,
1471 FALSE, FALSE, TRUE);
1473 if (widgets)
1474 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1475 gtk_menu_item_new());
1477 g_list_free(widgets); /* TODO: Get rid of this */
1480 choices_free_list(paths);
1483 /* Scan the SendTo dir and create and show the Send To menu.
1484 * The 'paths' list and every path in it is claimed, and will be
1485 * freed later -- don't free it yourself!
1487 static void show_send_to_menu(GList *paths, GdkEvent *event)
1489 GtkWidget *menu, *item;
1491 menu = gtk_menu_new();
1493 if (g_list_length(paths) == 1)
1495 DirItem *item;
1497 item = diritem_new("");
1498 diritem_restat(paths->data, item, NULL);
1500 add_sendto(menu,
1501 item->mime_type->media_type,
1502 item->mime_type->subtype);
1504 add_sendto(menu, item->mime_type->media_type, NULL);
1506 diritem_free(item);
1508 else
1509 add_sendto(menu, "group", NULL);
1511 add_sendto(menu, NULL, NULL);
1513 item = gtk_menu_item_new_with_label(_("Customise"));
1514 g_signal_connect_swapped(item, "activate",
1515 G_CALLBACK(customise_send_to), NULL);
1516 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1518 if (send_to_paths)
1519 destroy_glist(&send_to_paths);
1521 send_to_paths = paths;
1523 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1525 popup_menu = menu;
1526 show_popup_menu(menu, event, 0);
1529 static void send_to(FilerWindow *filer_window)
1531 GList *paths;
1532 GdkEvent *event;
1534 paths = filer_selected_items(filer_window);
1535 event = gtk_get_current_event();
1537 /* Eats paths */
1538 show_send_to_menu(paths, event);
1540 gdk_event_free(event);
1543 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1545 const char *argv[] = {"sh", "-c", NULL, NULL};
1546 gboolean close = action;
1548 argv[2] = o_menu_xterm.value;
1550 g_return_if_fail(window_with_focus != NULL);
1552 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1553 gtk_widget_destroy(window_with_focus->window);
1556 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1558 g_return_if_fail(window_with_focus != NULL);
1560 filer_change_to(window_with_focus, home_dir, NULL);
1563 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1565 g_return_if_fail(window_with_focus != NULL);
1567 bookmarks_show_menu(window_with_focus);
1570 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1572 g_return_if_fail(window_with_focus != NULL);
1574 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1575 filer_change_to(window_with_focus,
1576 window_with_focus->real_path, NULL);
1577 else
1578 delayed_error(_("This is already the canonical name "
1579 "for this directory."));
1582 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1584 g_return_if_fail(window_with_focus != NULL);
1586 filer_open_parent(window_with_focus);
1589 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1591 g_return_if_fail(window_with_focus != NULL);
1593 change_to_parent(window_with_focus);
1596 static void resize(gpointer data, guint action, GtkWidget *widget)
1598 g_return_if_fail(window_with_focus != NULL);
1600 view_autosize(window_with_focus->view);
1603 static void new_window(gpointer data, guint action, GtkWidget *widget)
1605 g_return_if_fail(window_with_focus != NULL);
1607 if (o_unique_filer_windows.int_value)
1609 report_error(_("You can't open a second view onto "
1610 "this directory because the `Unique Windows' option "
1611 "is turned on in the Options window."));
1613 else
1614 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1617 static void close_window(gpointer data, guint action, GtkWidget *widget)
1619 g_return_if_fail(window_with_focus != NULL);
1621 gtk_widget_destroy(window_with_focus->window);
1624 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1626 MiniType type = (MiniType) action;
1628 g_return_if_fail(window_with_focus != NULL);
1630 /* Item needs to remain selected... */
1631 if (type == MINI_SHELL)
1632 window_with_focus->temp_item_selected = FALSE;
1634 minibuffer_show(window_with_focus, type);
1637 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1639 if (action == HELP_ABOUT)
1640 infobox_new(app_dir);
1641 else if (action == HELP_DIR)
1642 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1643 else if (action == HELP_MANUAL)
1645 gchar *manual = NULL;
1647 if (current_lang)
1649 manual = g_strconcat(app_dir, "/Help/Manual-",
1650 current_lang, ".html", NULL);
1651 if (access(manual, F_OK))
1652 null_g_free(&manual);
1655 if (!manual)
1656 manual = g_strconcat(app_dir,
1657 "/Help/Manual.html", NULL);
1659 run_by_path(manual);
1661 g_free(manual);
1663 else
1664 g_warning("Unknown help action %d\n", action);
1667 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1668 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1670 GList *items, *item;
1672 items = gtk_container_get_children(GTK_CONTAINER(menu));
1674 item = g_list_nth(items, from);
1675 while (item && n--)
1677 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1678 item = item->next;
1680 g_list_free(items);
1683 static void save_menus(void)
1685 char *menurc;
1687 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1688 if (menurc)
1690 gtk_accel_map_save(menurc);
1691 g_free(menurc);
1695 static void select_nth_item(GtkMenuShell *shell, int n)
1697 GList *items;
1698 GtkWidget *item;
1700 items = gtk_container_get_children(GTK_CONTAINER(shell));
1701 item = g_list_nth_data(items, n);
1703 g_return_if_fail(item != NULL);
1705 g_list_free(items);
1707 gtk_menu_shell_select_item(shell, item);
1710 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1712 DirItem *item;
1713 const guchar *path;
1714 int n_selected;
1715 ViewIter iter;
1717 g_return_if_fail(window_with_focus != NULL);
1719 n_selected = view_count_selected(window_with_focus->view);
1721 if (n_selected < 1)
1723 const char *prompt;
1725 switch (action)
1727 case FILE_COPY_ITEM:
1728 prompt = _("Copy ... ?");
1729 break;
1730 case FILE_RENAME_ITEM:
1731 prompt = _("Rename ... ?");
1732 break;
1733 case FILE_LINK_ITEM:
1734 prompt = _("Symlink ... ?");
1735 break;
1736 case FILE_OPEN_FILE:
1737 prompt = _("Shift Open ... ?");
1738 break;
1739 case FILE_HELP:
1740 prompt = _("Help about ... ?");
1741 break;
1742 case FILE_SHOW_FILE_INFO:
1743 prompt = _("Examine ... ?");
1744 break;
1745 case FILE_RUN_ACTION:
1746 prompt = _("Set run action for ... ?");
1747 break;
1748 case FILE_SET_ICON:
1749 prompt = _("Set icon for ... ?");
1750 break;
1751 case FILE_SEND_TO:
1752 prompt = _("Send ... to ... ?");
1753 break;
1754 case FILE_DELETE:
1755 prompt = _("DELETE ... ?");
1756 break;
1757 case FILE_USAGE:
1758 prompt = _("Count the size of ... ?");
1759 break;
1760 case FILE_CHMOD_ITEMS:
1761 prompt = _("Set permissions on ... ?");
1762 break;
1763 case FILE_FIND:
1764 prompt = _("Search inside ... ?");
1765 break;
1766 case FILE_OPEN_VFS_AVFS:
1767 prompt = _("Look inside ... ?");
1768 break;
1769 default:
1770 g_warning("Unknown action!");
1771 return;
1773 filer_target_mode(window_with_focus, target_callback,
1774 GINT_TO_POINTER(action), prompt);
1775 return;
1778 switch (action)
1780 case FILE_SEND_TO:
1781 send_to(window_with_focus);
1782 return;
1783 case FILE_DELETE:
1784 delete(window_with_focus);
1785 return;
1786 case FILE_USAGE:
1787 usage(window_with_focus);
1788 return;
1789 case FILE_CHMOD_ITEMS:
1790 chmod_items(window_with_focus);
1791 return;
1792 case FILE_FIND:
1793 find(window_with_focus);
1794 return;
1795 case FILE_SHOW_FILE_INFO:
1797 GList *items;
1799 items = filer_selected_items(window_with_focus);
1800 infobox_show_list(items);
1801 destroy_glist(&items);
1802 return;
1804 default:
1805 break;
1808 /* All the following actions require exactly one file selected */
1810 if (n_selected > 1)
1812 report_error(_("You cannot do this to more than "
1813 "one item at a time"));
1814 return;
1817 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1819 item = iter.next(&iter);
1820 g_return_if_fail(item != NULL);
1821 /* iter may be passed to filer_openitem... */
1823 if (!item->image)
1824 item = dir_update_item(window_with_focus->directory,
1825 item->leafname);
1827 if (!item)
1829 report_error(_("Item no longer exists!"));
1830 return;
1833 path = make_path(window_with_focus->sym_path, item->leafname);
1835 switch (action)
1837 case FILE_COPY_ITEM:
1838 src_dest_action_item(path, item->image,
1839 _("Copy"), copy_cb);
1840 break;
1841 case FILE_RENAME_ITEM:
1842 src_dest_action_item(path, item->image,
1843 _("Rename"), rename_cb);
1844 break;
1845 case FILE_LINK_ITEM:
1846 src_dest_action_item(path, item->image,
1847 _("Symlink"), link_cb);
1848 break;
1849 case FILE_OPEN_FILE:
1850 filer_openitem(window_with_focus, &iter,
1851 OPEN_SAME_WINDOW | OPEN_SHIFT);
1852 break;
1853 case FILE_HELP:
1854 show_item_help(path, item);
1855 break;
1856 case FILE_RUN_ACTION:
1857 run_action(item);
1858 break;
1859 case FILE_SET_ICON:
1860 icon_set_handler_dialog(item, path);
1861 break;
1862 case FILE_OPEN_VFS_AVFS:
1863 open_vfs_avfs(window_with_focus, item);
1864 break;
1865 default:
1866 g_warning("Unknown action!");
1867 return;
1871 static void show_key_help(GtkWidget *button, gpointer data)
1873 gboolean can_change_accels;
1875 g_object_get(G_OBJECT(gtk_settings_get_default()),
1876 "gtk-can-change-accels", &can_change_accels,
1877 NULL);
1879 if (!can_change_accels)
1881 info_message(_("User-definable shortcuts are disabled by "
1882 "default in Gtk2, and you have not enabled "
1883 "them. You can turn this feature on by:\n"
1884 "1) using an XSettings manager, such as ROX-Session\n"
1885 "or\n"
1886 "2) adding this line to ~/.gtkrc-2.0:\n"
1887 "\tgtk-can-change-accels = 1"));
1888 return;
1891 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1892 "- Open the menu over a filer window,\n"
1893 "- Move the pointer over the item you want to use,\n"
1894 "- Press the key you want attached to it.\n\n"
1895 "The key will appear next to the menu item and you can just press "
1896 "that key without opening the menu in future."));
1899 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1901 GtkWidget *button, *align;
1903 g_return_val_if_fail(option == NULL, NULL);
1905 align = gtk_alignment_new(0, 0.5, 0, 0);
1906 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1907 gtk_container_add(GTK_CONTAINER(align), button);
1908 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1910 return g_list_append(NULL, align);