r1824: Internal changes: moved menu and toolbar over to View interface.
[rox-filer.git] / ROX-Filer / src / menu.c
blobe6fce78662544de3707a965e95f27fd8bb01800f
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"
63 typedef enum {
64 FILE_COPY_ITEM,
65 FILE_RENAME_ITEM,
66 FILE_LINK_ITEM,
67 FILE_OPEN_FILE,
68 FILE_HELP,
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;
96 static GtkWidget *popup_menu = NULL; /* Currently open menu */
98 static gint updating_menu = 0; /* Non-zero => ignore activations */
99 static GList *send_to_paths = NULL;
101 static Option o_menu_iconsize, o_menu_xterm;
103 /* Static prototypes */
105 static void save_menus(void);
106 static void menu_closed(GtkWidget *widget);
107 static void shade_file_menu_items(gboolean shaded);
108 static void savebox_show(const gchar *action, const gchar *path,
109 MaskedPixmap *image, SaveCb callback);
110 static gint save_to_file(GObject *savebox,
111 const gchar *pathname, gpointer data);
112 static gboolean action_with_leaf(ActionFn action,
113 const gchar *current, const gchar *new);
114 static gboolean link_cb(GObject *savebox,
115 const gchar *initial, const gchar *path);
116 static void select_nth_item(GtkMenuShell *shell, int n);
117 static void new_file_type(gchar *templ);
118 static void do_send_to(gchar *templ);
119 static void show_send_to_menu(GList *paths, GdkEvent *event);
120 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
122 /* Note that for most of these callbacks none of the arguments are used. */
124 /* (action used in these three - DetailsType) */
125 static void huge_with(gpointer data, guint action, GtkWidget *widget);
126 static void large_with(gpointer data, guint action, GtkWidget *widget);
127 static void small_with(gpointer data, guint action, GtkWidget *widget);
129 static void sort_name(gpointer data, guint action, GtkWidget *widget);
130 static void sort_type(gpointer data, guint action, GtkWidget *widget);
131 static void sort_size(gpointer data, guint action, GtkWidget *widget);
132 static void sort_date(gpointer data, guint action, GtkWidget *widget);
134 static void hidden(gpointer data, guint action, GtkWidget *widget);
135 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
136 static void refresh(gpointer data, guint action, GtkWidget *widget);
138 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
140 static void select_all(gpointer data, guint action, GtkWidget *widget);
141 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
142 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
143 static void new_directory(gpointer data, guint action, GtkWidget *widget);
144 static void new_file(gpointer data, guint action, GtkWidget *widget);
145 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent(gpointer data, guint action, GtkWidget *widget);
149 static void home_directory(gpointer data, guint action, GtkWidget *widget);
150 static void new_window(gpointer data, guint action, GtkWidget *widget);
151 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
152 static void close_window(gpointer data, guint action, GtkWidget *widget);
153 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
155 /* (action used in this - MiniType) */
156 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
157 static void resize(gpointer data, guint action, GtkWidget *widget);
159 #define MENUS_NAME "menus2"
161 static GtkWidget *filer_menu; /* The popup filer menu */
162 static GtkWidget *filer_file_item; /* The File '' label */
163 static GtkWidget *filer_file_menu; /* The File '' menu */
164 static GtkWidget *file_shift_item; /* Shift Open label */
165 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
166 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
167 static GtkWidget *filer_new_window; /* The New Window item */
168 static GtkWidget *filer_new_menu; /* The New submenu */
169 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
171 #undef N_
172 #define N_(x) x
174 static GtkItemFactoryEntry filer_menu_def[] = {
175 {N_("Display"), NULL, NULL, 0, "<Branch>"},
176 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
177 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
178 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
179 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
180 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
181 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
182 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
183 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
184 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
185 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
186 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
187 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
188 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
189 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
190 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
191 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
192 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
193 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
194 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
195 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
196 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
197 {">", NULL, NULL, 0, "<Separator>"},
198 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
199 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
200 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
201 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
202 {">", NULL, NULL, 0, "<Separator>"},
203 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
204 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
205 {">" N_("Refresh"), NULL, refresh, 0, NULL},
206 {N_("File"), NULL, NULL, 0, "<Branch>"},
207 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
208 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
209 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
210 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
211 {">", NULL, NULL, 0, "<Separator>"},
212 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
213 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
214 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
215 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
216 {">", NULL, NULL, 0, "<Separator>"},
217 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
218 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
219 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
220 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
221 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
222 {">", NULL, NULL, 0, "<Separator>"},
223 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
224 {N_("Select"), NULL, NULL, 0, "<Branch>"},
225 {">" N_("Select All"), NULL, select_all, 0, NULL},
226 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
227 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
228 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
229 {N_("Options..."), NULL, menu_show_options, 0, NULL},
230 {N_("New"), NULL, NULL, 0, "<Branch>"},
231 {">" N_("Directory"), NULL, new_directory, 0, NULL},
232 {">" N_("Blank file"), NULL, new_file, 0, NULL},
233 {N_("Window"), NULL, NULL, 0, "<Branch>"},
234 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
235 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
236 {">" N_("New Window"), NULL, new_window, 0, NULL},
237 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
238 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
239 {">" N_("Resize Window"), NULL, resize, 0, NULL},
240 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
242 {">" N_("Close Window"), NULL, close_window, 0, NULL},
243 {">", NULL, NULL, 0, "<Separator>"},
244 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
245 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
246 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
247 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
248 {N_("Help"), NULL, NULL, 0, "<Branch>"},
249 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
250 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, NULL},
251 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
255 #define GET_MENU_ITEM(var, menu) \
256 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
258 #define GET_SMENU_ITEM(var, menu, sub) \
259 do { \
260 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
261 var = gtk_item_factory_get_widget(item_factory, tmp); \
262 g_free(tmp); \
263 } while (0)
265 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
266 do { \
267 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
268 var = gtk_item_factory_get_widget(item_factory, tmp); \
269 g_free(tmp); \
270 } while (0)
272 void ensure_filer_menu(void)
274 GList *items;
275 guchar *tmp;
276 GtkWidget *item;
277 GtkItemFactory *item_factory;
279 if (filer_keys)
280 return;
282 filer_keys = gtk_accel_group_new();
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_thumb_menu, "filer", "Display",
291 "Show Thumbnails");
293 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
294 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
295 filer_follow_sym = GTK_BIN(item)->child;
297 /* File '' label... */
298 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
299 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
300 g_list_free(items);
302 /* Shift Open... label */
303 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
304 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
305 g_list_free(items);
307 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
308 filer_new_window = GTK_BIN(item)->child;
310 g_signal_connect(filer_menu, "unmap_event",
311 G_CALLBACK(menu_closed), NULL);
312 g_signal_connect(filer_file_menu, "unmap_event",
313 G_CALLBACK(menu_closed), NULL);
315 g_signal_connect(filer_keys, "accel_changed",
316 G_CALLBACK(save_menus), NULL);
319 void menu_init(void)
321 char *menurc;
323 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
324 if (menurc)
326 gtk_accel_map_load(menurc);
327 g_free(menurc);
330 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
331 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
332 option_add_saver(save_menus);
334 option_register_widget("menu-set-keys", set_keys_button);
337 /* Name is in the form "<panel>" */
338 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
339 const gchar *name, GtkAccelGroup *keys)
341 GtkItemFactory *item_factory;
342 GtkItemFactoryEntry *translated;
344 if (!keys)
346 keys = gtk_accel_group_new();
347 gtk_accel_group_lock(keys);
350 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
352 translated = translate_entries(def, n_entries);
353 gtk_item_factory_create_items(item_factory, n_entries,
354 translated, NULL);
355 free_translated_entries(translated, n_entries);
357 return item_factory;
360 /* Prevent the user from setting a short-cut on this item */
361 static void menuitem_no_shortcuts(GtkWidget *item)
363 /* XXX */
364 #if 0
365 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
367 _gtk_widget_set_accel_path(item, NULL, NULL);
368 g_free(menuitem->accel_path);
369 menuitem->accel_path = NULL;
370 #endif
373 /* Shade items that only work on single files */
374 static void shade_file_menu_items(gboolean shaded)
376 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
377 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
378 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
381 /* 'data' is an array of three ints:
382 * [ pointer_x, pointer_y, item_under_pointer ]
384 void position_menu(GtkMenu *menu, gint *x, gint *y,
385 gboolean *push_in, gpointer data)
387 int *pos = (int *) data;
388 GtkRequisition requisition;
389 GList *items, *next;
390 int y_shift = 0;
391 int item = pos[2];
393 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
395 while (item >= 0 && next)
397 int h = ((GtkWidget *) next->data)->requisition.height;
399 if (item > 0)
400 y_shift += h;
401 else
402 y_shift += h / 2;
404 next = next->next;
405 item--;
408 g_list_free(items);
410 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
412 *x = pos[0] - (requisition.width * 7 / 8);
413 *y = pos[1] - y_shift;
415 *x = CLAMP(*x, 0, screen_width - requisition.width);
416 *y = CLAMP(*y, 0, screen_height - requisition.height);
418 *push_in = FALSE;
421 #if 0
422 /* Used when you menu-click on the Large or Small toolbar tools */
423 void show_style_menu(FilerWindow *filer_window,
424 GdkEventButton *event,
425 GtkWidget *menu)
427 int pos[3];
429 pos[0] = event->x_root;
430 pos[1] = event->y_root;
431 pos[2] = 0;
433 window_with_focus = filer_window;
435 popup_menu = menu;
437 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
438 (gpointer) pos, event->button, event->time);
440 #endif
442 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
443 MenuIconStyle style, CallbackFn func,
444 gboolean separator, gboolean strip_ext,
445 gboolean recurse)
447 GList *widgets = NULL;
448 DirItem *ditem;
449 DIR *dir;
450 struct dirent *ent;
451 GtkWidget *item;
452 char *dname = NULL;
454 dname = pathdup(dir_name);
456 dir = opendir(dname);
457 if (!dir)
458 goto out;
460 if (separator)
462 item = gtk_menu_item_new();
463 widgets = g_list_append(widgets, item);
464 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
467 while ((ent = readdir(dir)))
469 char *dot, *leaf;
470 GtkWidget *hbox;
471 GtkWidget *img;
472 GtkWidget *label;
473 gchar *fname;
475 /* Ignore hidden files */
476 if (ent->d_name[0] == '.')
477 continue;
479 /* Strip off extension, if any */
480 dot = strchr(ent->d_name, '.');
481 if (strip_ext && dot)
482 leaf = g_strndup(ent->d_name, dot - ent->d_name);
483 else
484 leaf = g_strdup(ent->d_name);
486 fname = g_strconcat(dname, "/", ent->d_name, NULL);
487 ditem = diritem_new("");
488 diritem_restat(fname, ditem, NULL);
490 if (ditem->image && style != MIS_NONE)
492 GdkPixbuf *pixbuf;
494 switch (style) {
495 case MIS_LARGE:
496 pixbuf = ditem->image->pixbuf;
497 break;
498 default:
499 if (!ditem->image->sm_pixbuf)
500 pixmap_make_small(ditem->image);
501 pixbuf = ditem->image->sm_pixbuf;
502 break;
505 item = gtk_menu_item_new();
506 /* TODO: Find a way to allow short-cuts */
507 menuitem_no_shortcuts(item);
509 hbox = gtk_hbox_new(FALSE, 2);
510 gtk_container_add(GTK_CONTAINER(item), hbox);
512 img = gtk_image_new_from_pixbuf(pixbuf);
514 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
516 label = gtk_label_new(leaf);
517 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
518 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
521 else
522 item = gtk_menu_item_new_with_label(leaf);
524 /* If it is a directory (but NOT an AppDir) and we are
525 * recursing then set up a sub menu.
527 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
528 !(ditem->flags & ITEM_FLAG_APPDIR))
530 GtkWidget *sub;
531 GList *new_widgets;
533 sub = gtk_menu_new();
534 new_widgets = menu_from_dir(sub, fname, style, func,
535 separator, strip_ext, FALSE);
536 g_list_free(new_widgets);
537 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
539 else
540 g_signal_connect_swapped(item, "activate",
541 G_CALLBACK(func), fname);
543 diritem_free(ditem);
544 g_free(leaf);
546 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
547 g_signal_connect_swapped(item, "destroy",
548 G_CALLBACK(g_free), fname);
550 widgets = g_list_append(widgets, item);
553 closedir(dir);
554 out:
555 g_free(dname);
557 return widgets;
560 /* Scan the templates dir and create entries for the New menu */
561 static void update_new_files_menu(MenuIconStyle style)
563 static GList *widgets = NULL;
565 gchar *templ_dname = NULL;
567 if (widgets)
569 GList *next;
571 for (next = widgets; next; next = next->next)
572 gtk_widget_destroy((GtkWidget *) next->data);
574 g_list_free(widgets);
575 widgets = NULL;
578 templ_dname = choices_find_path_load("Templates", "");
579 if (templ_dname)
581 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
582 (CallbackFn) new_file_type, TRUE, TRUE,
583 FALSE);
584 g_free(templ_dname);
586 gtk_widget_show_all(filer_new_menu);
589 /* 'item' is the number of the item to appear under the pointer. */
590 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
592 int pos[3];
593 int button = 0;
594 guint32 time = 0;
596 if (event && (event->type == GDK_BUTTON_PRESS ||
597 event->type == GDK_BUTTON_RELEASE))
599 GdkEventButton *bev = (GdkEventButton *) event;
601 pos[0] = bev->x_root;
602 pos[1] = bev->y_root;
603 button = bev->button;
604 time = bev->time;
606 else if (event && event->type == GDK_KEY_PRESS)
608 GdkEventKey *kev = (GdkEventKey *) event;
610 get_pointer_xy(pos, pos + 1);
611 time = kev->time;
613 else
614 get_pointer_xy(pos, pos + 1);
616 pos[2] = item;
618 gtk_widget_show_all(menu);
619 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
620 position_menu, (gpointer) pos, button, time);
621 select_nth_item(GTK_MENU_SHELL(menu), item);
624 /* Hide the popup menu, if any */
625 void menu_popdown(void)
627 if (popup_menu)
628 gtk_menu_popdown(GTK_MENU(popup_menu));
631 static MenuIconStyle get_menu_icon_style(void)
633 MenuIconStyle mis;
634 int display;
636 mis = o_menu_iconsize.int_value;
638 switch (mis)
640 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
641 return mis;
642 default:
643 break;
646 if (mis == MIS_CURRENT && window_with_focus)
648 switch (window_with_focus->display_style)
650 case HUGE_ICONS:
651 case LARGE_ICONS:
652 return MIS_LARGE;
653 case SMALL_ICONS:
654 return MIS_SMALL;
655 default:
656 break;
660 display = o_display_size.int_value;
661 switch (display)
663 case HUGE_ICONS:
664 case LARGE_ICONS:
665 return MIS_LARGE;
666 case SMALL_ICONS:
667 return MIS_SMALL;
668 default:
669 break;
672 return MIS_SMALL;
675 /* iter->peek() is the clicked item, or NULL if none */
676 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
678 DirItem *file_item = NULL;
679 GdkModifierType state = 0;
680 int n_selected;
682 n_selected = view_count_selected(filer_window->view);
684 ensure_filer_menu();
686 updating_menu++;
688 /* Remove previous AppMenu, if any */
689 appmenu_remove();
691 window_with_focus = filer_window;
693 if (!event)
694 event = gtk_get_current_event();
696 if (event->type == GDK_BUTTON_PRESS)
697 state = ((GdkEventButton *) event)->state;
698 else if (event->type == GDK_KEY_PRESS)
699 state = ((GdkEventKey *) event)->state;
701 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
703 filer_window->temp_item_selected = TRUE;
704 view_set_selected(filer_window->view, iter, TRUE);
705 n_selected = view_count_selected(filer_window->view);
707 else
709 filer_window->temp_item_selected = FALSE;
712 /* Short-cut to the Send To menu */
713 if (state & GDK_SHIFT_MASK)
715 GList *paths;
717 updating_menu--;
719 if (n_selected == 0)
721 report_error(
722 _("You should Shift+Menu click over a file to "
723 "send it somewhere"));
724 return;
727 paths = filer_selected_items(filer_window);
729 show_send_to_menu(paths, event); /* (paths eaten) */
731 return;
735 GtkWidget *file_label, *file_menu;
736 GString *buffer;
737 DirItem *item;
739 file_label = filer_file_item;
740 file_menu = filer_file_menu;
741 gtk_check_menu_item_set_active(
742 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
743 filer_window->show_thumbs);
744 gtk_check_menu_item_set_active(
745 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
746 filer_window->show_hidden);
747 buffer = g_string_new(NULL);
749 switch (n_selected)
751 case 0:
752 g_string_assign(buffer, _("Next Click"));
753 shade_file_menu_items(FALSE);
754 break;
755 case 1:
756 item = filer_selected_item(filer_window);
757 if (!item->image)
758 dir_update_item(filer_window->directory,
759 item->leafname);
760 shade_file_menu_items(FALSE);
761 file_item = filer_selected_item(filer_window);
762 g_string_sprintf(buffer, "%s '%s'",
763 basetype_name(file_item),
764 file_item->leafname);
765 if (!can_set_run_action(file_item))
766 menu_set_items_shaded(filer_file_menu,
767 TRUE, 6, 1);
768 break;
769 default:
770 shade_file_menu_items(TRUE);
771 g_string_sprintf(buffer, _("%d items"),
772 n_selected);
773 break;
775 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
776 g_string_free(buffer, TRUE);
778 menu_show_shift_action(file_shift_item, file_item,
779 n_selected == 0);
780 if (file_item)
781 appmenu_add(make_path(filer_window->sym_path,
782 file_item->leafname)->str,
783 file_item, filer_file_menu);
786 update_new_files_menu(get_menu_icon_style());
788 gtk_widget_set_sensitive(filer_new_window,
789 !o_unique_filer_windows.int_value);
790 gtk_widget_set_sensitive(filer_follow_sym,
791 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
793 popup_menu = (state & GDK_CONTROL_MASK)
794 ? filer_file_menu
795 : filer_menu;
797 updating_menu--;
799 show_popup_menu(popup_menu, event,
800 popup_menu == filer_file_menu ? 5 : 1);
803 static void menu_closed(GtkWidget *widget)
805 if (window_with_focus == NULL || widget != popup_menu)
806 return; /* Close panel item chosen? */
808 popup_menu = NULL;
810 if (window_with_focus->temp_item_selected)
812 view_clear_selection(window_with_focus->view);
813 window_with_focus->temp_item_selected = FALSE;
817 static void target_callback(FilerWindow *filer_window,
818 ViewIter *iter,
819 gpointer action)
821 g_return_if_fail(filer_window != NULL);
823 window_with_focus = filer_window;
825 /* Don't grab the primary selection */
826 filer_window->temp_item_selected = TRUE;
828 view_wink_item(filer_window->view, iter);
829 view_select_only(filer_window->view, iter);
830 file_op(NULL, GPOINTER_TO_INT(action), NULL);
832 view_clear_selection(filer_window->view);
833 filer_window->temp_item_selected = FALSE;
836 /* Set the text of the 'Shift Open...' menu item.
837 * If icon is NULL, reset the text and also shade it, unless 'next'.
839 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
841 guchar *shift_action = NULL;
843 if (item)
845 if (item->flags & ITEM_FLAG_MOUNT_POINT)
847 if (item->flags & ITEM_FLAG_MOUNTED)
848 shift_action = N_("Unmount");
849 else
850 shift_action = N_("Mount");
852 else if (item->flags & ITEM_FLAG_SYMLINK)
853 shift_action = N_("Show Target");
854 else if (item->base_type == TYPE_DIRECTORY)
855 shift_action = N_("Look Inside");
856 else if (item->base_type == TYPE_FILE)
857 shift_action = N_("Open As Text");
859 gtk_label_set_text(GTK_LABEL(menu_item),
860 shift_action ? _(shift_action)
861 : _("Shift Open"));
862 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
865 /* Actions */
867 static void huge_with(gpointer data, guint action, GtkWidget *widget)
869 display_set_layout(window_with_focus, HUGE_ICONS, action);
872 static void large_with(gpointer data, guint action, GtkWidget *widget)
874 display_set_layout(window_with_focus, LARGE_ICONS, action);
877 static void small_with(gpointer data, guint action, GtkWidget *widget)
879 display_set_layout(window_with_focus, SMALL_ICONS, action);
882 static void sort_name(gpointer data, guint action, GtkWidget *widget)
884 g_return_if_fail(window_with_focus != NULL);
886 display_set_sort_fn(window_with_focus, sort_by_name);
889 static void sort_type(gpointer data, guint action, GtkWidget *widget)
891 g_return_if_fail(window_with_focus != NULL);
893 display_set_sort_fn(window_with_focus, sort_by_type);
896 static void sort_date(gpointer data, guint action, GtkWidget *widget)
898 g_return_if_fail(window_with_focus != NULL);
900 display_set_sort_fn(window_with_focus, sort_by_date);
903 static void sort_size(gpointer data, guint action, GtkWidget *widget)
905 g_return_if_fail(window_with_focus != NULL);
907 display_set_sort_fn(window_with_focus, sort_by_size);
910 static void hidden(gpointer data, guint action, GtkWidget *widget)
912 if (updating_menu)
913 return;
915 g_return_if_fail(window_with_focus != NULL);
917 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
920 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
922 if (updating_menu)
923 return;
925 g_return_if_fail(window_with_focus != NULL);
927 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
930 static void refresh(gpointer data, guint action, GtkWidget *widget)
932 g_return_if_fail(window_with_focus != NULL);
934 full_refresh();
935 filer_update_dir(window_with_focus, TRUE);
938 static void delete(FilerWindow *filer_window)
940 GList *paths;
941 paths = filer_selected_items(filer_window);
942 action_delete(paths);
943 g_list_foreach(paths, (GFunc) g_free, NULL);
944 g_list_free(paths);
947 static void usage(FilerWindow *filer_window)
949 GList *paths;
950 paths = filer_selected_items(filer_window);
951 action_usage(paths);
952 g_list_foreach(paths, (GFunc) g_free, NULL);
953 g_list_free(paths);
956 static void chmod_items(FilerWindow *filer_window)
958 GList *paths;
959 paths = filer_selected_items(filer_window);
960 action_chmod(paths, FALSE, NULL);
961 g_list_foreach(paths, (GFunc) g_free, NULL);
962 g_list_free(paths);
965 static void find(FilerWindow *filer_window)
967 GList *paths;
968 paths = filer_selected_items(filer_window);
969 action_find(paths);
970 g_list_foreach(paths, (GFunc) g_free, NULL);
971 g_list_free(paths);
974 /* This creates a new savebox widget, and allows the user to pick a new path
975 * for the file.
976 * Once the new path has been picked, the callback will be called with
977 * both the current and new paths.
978 * NOTE: This function unrefs 'image'!
980 static void savebox_show(const gchar *action, const gchar *path,
981 MaskedPixmap *image, SaveCb callback)
983 GtkWidget *savebox = NULL;
984 GtkWidget *check_relative = NULL;
986 g_return_if_fail(image != NULL);
988 savebox = gtk_savebox_new(action);
990 if (callback == link_cb)
992 check_relative = gtk_check_button_new_with_mnemonic(
993 _("_Relative link"));
994 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
995 TRUE);
997 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
998 gtk_tooltips_set_tip(tooltips, check_relative,
999 _("If on, the symlink will store the path from the "
1000 "symlink to the target file. Use this if the symlink "
1001 "and the target will be moved together.\n"
1002 "If off, the path from the root directory is stored - "
1003 "use this if the symlink may move but the target will "
1004 "stay put."), NULL);
1005 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1006 check_relative, FALSE, TRUE, 0);
1007 gtk_widget_show(check_relative);
1010 g_signal_connect(savebox, "save_to_file",
1011 G_CALLBACK(save_to_file), NULL);
1013 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1014 g_strdup(path), g_free);
1015 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1016 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1018 gtk_window_set_title(GTK_WINDOW(savebox), action);
1020 if (g_utf8_validate(path, -1, NULL))
1021 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1022 else
1024 gchar *u8;
1025 u8 = to_utf8(path);
1026 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1027 g_free(u8);
1029 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1030 g_object_unref(image);
1032 gtk_widget_show(savebox);
1035 static gint save_to_file(GObject *savebox,
1036 const gchar *pathname, gpointer data)
1038 SaveCb callback;
1039 const gchar *current_path;
1041 callback = g_object_get_data(savebox, "action_callback");
1042 current_path = g_object_get_data(savebox, "current_path");
1044 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1045 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1047 return callback(savebox, current_path, pathname)
1048 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1051 static gboolean copy_cb(GObject *savebox,
1052 const gchar *current, const gchar *new)
1054 return action_with_leaf(action_copy, current, new);
1057 static gboolean action_with_leaf(ActionFn action,
1058 const gchar *current, const gchar *new)
1060 const char *leaf;
1061 char *new_dir;
1062 GList *local_paths;
1064 if (new[0] != '/')
1066 report_error(_("New pathname is not absolute"));
1067 return FALSE;
1070 if (new[strlen(new) - 1] == '/')
1072 new_dir = g_strdup(new);
1073 leaf = NULL;
1075 else
1077 const gchar *slash;
1079 slash = strrchr(new, '/');
1080 new_dir = g_strndup(new, slash - new);
1081 leaf = slash + 1;
1084 local_paths = g_list_append(NULL, (gchar *) current);
1085 action(local_paths, new_dir, leaf, -1);
1086 g_list_free(local_paths);
1088 g_free(new_dir);
1090 return TRUE;
1093 /* Open a savebox to act on the selected file.
1094 * Call 'callback' later to perform the operation.
1096 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1097 const gchar *action, SaveCb callback)
1099 g_object_ref(image);
1100 savebox_show(action, path, image, callback);
1103 static gboolean rename_cb(GObject *savebox,
1104 const gchar *current, const gchar *new)
1106 return action_with_leaf(action_move, current, new);
1109 static gboolean link_cb(GObject *savebox,
1110 const gchar *initial, const gchar *path)
1112 GtkToggleButton *check_relative;
1113 struct stat info;
1114 int err;
1115 gchar *link_path;
1117 check_relative = g_object_get_data(savebox, "check_relative");
1119 if (gtk_toggle_button_get_active(check_relative))
1120 link_path = get_relative_path(path, initial);
1121 else
1122 link_path = g_strdup(initial);
1124 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1126 GtkWidget *box, *button;
1127 gint ans;
1129 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1130 GTK_BUTTONS_CANCEL,
1131 _("Symlink from '%s' already exists. "
1132 "Replace it with a link to '%s'?"),
1133 path, link_path);
1135 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1137 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1138 gtk_widget_show(button);
1139 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1140 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1141 button, GTK_RESPONSE_OK);
1142 gtk_dialog_set_default_response(GTK_DIALOG(box),
1143 GTK_RESPONSE_OK);
1145 ans = gtk_dialog_run(GTK_DIALOG(box));
1146 gtk_widget_destroy(box);
1148 if (ans != GTK_RESPONSE_OK)
1150 g_free(link_path);
1151 return FALSE;
1154 unlink(path);
1157 err = symlink(link_path, path);
1158 g_free(link_path);
1160 if (err)
1162 report_error("symlink: %s", g_strerror(errno));
1163 return FALSE;
1166 dir_check_this(path);
1168 return TRUE;
1171 static void run_action(DirItem *item)
1173 if (can_set_run_action(item))
1174 type_set_handler_dialog(item->mime_type);
1175 else
1176 report_error(
1177 _("You can only set the run action for a "
1178 "regular file"));
1181 void open_home(gpointer data, guint action, GtkWidget *widget)
1183 filer_opendir(home_dir, NULL);
1186 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1188 gchar *path;
1190 path = g_strconcat(filer_window->sym_path,
1191 "/", item->leafname, "#", NULL);
1193 filer_change_to(filer_window, path, NULL);
1194 g_free(path);
1197 static void select_all(gpointer data, guint action, GtkWidget *widget)
1199 g_return_if_fail(window_with_focus != NULL);
1201 window_with_focus->temp_item_selected = FALSE;
1202 view_select_all(window_with_focus->view);
1205 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1207 g_return_if_fail(window_with_focus != NULL);
1209 window_with_focus->temp_item_selected = FALSE;
1210 view_clear_selection(window_with_focus->view);
1213 static gboolean invert_cb(ViewIter *iter, gpointer data)
1215 return !view_get_selected((ViewIface *) data, iter);
1218 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1220 g_return_if_fail(window_with_focus != NULL);
1222 window_with_focus->temp_item_selected = FALSE;
1224 view_select_if(window_with_focus->view, invert_cb,
1225 window_with_focus->view);
1228 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1230 GtkWidget *win;
1232 win = options_show();
1234 if (win)
1236 number_of_windows++;
1237 g_signal_connect(win, "destroy",
1238 G_CALLBACK(one_less_window), NULL);
1242 static gboolean new_directory_cb(GObject *savebox,
1243 const gchar *initial, const gchar *path)
1245 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1247 report_error("mkdir: %s", g_strerror(errno));
1248 return FALSE;
1251 dir_check_this(path);
1253 if (filer_exists(window_with_focus))
1255 guchar *leaf;
1256 leaf = strrchr(path, '/');
1257 if (leaf)
1258 display_set_autoselect(window_with_focus, leaf + 1);
1261 return TRUE;
1264 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1266 g_return_if_fail(window_with_focus != NULL);
1268 savebox_show(_("Create"),
1269 make_path(window_with_focus->sym_path, _("NewDir"))->str,
1270 type_to_icon(inode_directory), new_directory_cb);
1273 static gboolean new_file_cb(GObject *savebox,
1274 const gchar *initial, const gchar *path)
1276 int fd;
1278 fd = open(path, O_CREAT | O_EXCL, 0666);
1280 if (fd == -1)
1282 report_error(_("Error creating '%s': %s"),
1283 path, g_strerror(errno));
1284 return FALSE;
1287 if (close(fd))
1288 report_error(_("Error creating '%s': %s"),
1289 path, g_strerror(errno));
1291 dir_check_this(path);
1293 if (filer_exists(window_with_focus))
1295 guchar *leaf;
1296 leaf = strrchr(path, '/');
1297 if (leaf)
1298 display_set_autoselect(window_with_focus, leaf + 1);
1301 return TRUE;
1304 static void new_file(gpointer data, guint action, GtkWidget *widget)
1306 g_return_if_fail(window_with_focus != NULL);
1308 savebox_show(_("Create"),
1309 make_path(window_with_focus->sym_path, _("NewFile"))->str,
1310 type_to_icon(text_plain),
1311 new_file_cb);
1314 static gboolean new_file_type_cb(GObject *savebox,
1315 const gchar *initial, const gchar *path)
1317 const gchar *oleaf, *leaf;
1318 gchar *templ, *templ_dname, *dest;
1319 GList *paths;
1321 /* We can work out the template path from the initial name */
1322 oleaf = g_basename(initial);
1323 templ_dname = choices_find_path_load("Templates", "");
1324 if (!templ_dname)
1326 report_error(
1327 _("Error creating file: could not find the template for %s"),
1328 oleaf);
1329 return FALSE;
1332 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1333 g_free(templ_dname);
1335 dest = g_dirname(path);
1336 leaf = g_basename(path);
1337 paths = g_list_append(NULL, templ);
1339 action_copy(paths, dest, leaf, TRUE);
1341 g_list_free(paths);
1342 g_free(dest);
1343 g_free(templ);
1345 if (filer_exists(window_with_focus))
1346 display_set_autoselect(window_with_focus, leaf);
1348 return TRUE;
1351 static void do_send_to(gchar *templ)
1353 g_return_if_fail(send_to_paths != NULL);
1355 run_with_files(templ, send_to_paths);
1358 static void new_file_type(gchar *templ)
1360 const gchar *leaf;
1361 MIME_type *type;
1363 g_return_if_fail(window_with_focus != NULL);
1365 leaf = g_basename(templ);
1366 type = type_get_type(templ);
1368 savebox_show(_("Create"),
1369 make_path(window_with_focus->sym_path, leaf)->str,
1370 type_to_icon(type),
1371 new_file_type_cb);
1374 static void customise_send_to(gpointer data)
1376 GPtrArray *path;
1377 guchar *save;
1378 GString *dirs;
1379 int i;
1381 dirs = g_string_new(NULL);
1383 path = choices_list_dirs("");
1384 for (i = 0; i < path->len; i++)
1386 guchar *old = (guchar *) path->pdata[i];
1388 g_string_append(dirs, old);
1389 g_string_append(dirs, "SendTo\n");
1391 choices_free_list(path);
1393 save = choices_find_path_save("", "SendTo", TRUE);
1394 if (save)
1395 mkdir(save, 0777);
1397 info_message(
1398 _("The `Send To' menu provides a quick way to send some files "
1399 "to an application. The applications listed are those in "
1400 "the following directories:\n\n%s\n%s\n"
1401 "The `Send To' menu may be opened by Shift+Menu clicking "
1402 "over a file.\n\n"
1403 "Advanced use:\n"
1404 "You can also create subdirectories called "
1405 "`.text_html', `.text', etc which will only be "
1406 "shown for files of that type. `.group' is shown "
1407 "only when multiple files are selected."),
1408 dirs->str,
1409 save ? _("I'll show you your SendTo directory now; you should "
1410 "symlink (Ctrl+Shift drag) any applications you want "
1411 "into it.")
1412 : _("Your CHOICESPATH variable setting prevents "
1413 "customisations - sorry."));
1415 g_string_free(dirs, TRUE);
1417 if (save)
1418 filer_opendir(save, NULL);
1421 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1422 * to the menu.
1424 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1426 gchar *searchdir;
1427 GPtrArray *paths;
1428 int i;
1430 if (subtype)
1431 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1432 else if (type)
1433 searchdir = g_strdup_printf("SendTo/.%s", type);
1434 else
1435 searchdir = g_strdup("SendTo");
1437 paths = choices_list_dirs(searchdir);
1438 g_free(searchdir);
1440 for (i = 0; i < paths->len; i++)
1442 GList *widgets = NULL;
1443 guchar *dir = (guchar *) paths->pdata[i];
1445 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1446 (CallbackFn) do_send_to,
1447 FALSE, FALSE, TRUE);
1449 if (widgets)
1450 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1451 gtk_menu_item_new());
1453 g_list_free(widgets); /* TODO: Get rid of this */
1456 choices_free_list(paths);
1459 /* Scan the SendTo dir and create and show the Send To menu.
1460 * The 'paths' list and every path in it is claimed, and will be
1461 * freed later -- don't free it yourself!
1463 static void show_send_to_menu(GList *paths, GdkEvent *event)
1465 GtkWidget *menu, *item;
1467 menu = gtk_menu_new();
1469 if (g_list_length(paths) == 1)
1471 DirItem *item;
1473 item = diritem_new("");
1474 diritem_restat(paths->data, item, NULL);
1476 add_sendto(menu,
1477 item->mime_type->media_type,
1478 item->mime_type->subtype);
1480 add_sendto(menu, item->mime_type->media_type, NULL);
1482 diritem_free(item);
1484 else
1485 add_sendto(menu, "group", NULL);
1487 add_sendto(menu, NULL, NULL);
1489 item = gtk_menu_item_new_with_label(_("Customise"));
1490 g_signal_connect_swapped(item, "activate",
1491 G_CALLBACK(customise_send_to), NULL);
1492 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1494 if (send_to_paths)
1496 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1497 g_list_free(send_to_paths);
1499 send_to_paths = paths;
1501 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1503 popup_menu = menu;
1504 show_popup_menu(menu, event, 0);
1507 static void send_to(FilerWindow *filer_window)
1509 GList *paths;
1510 GdkEvent *event;
1512 paths = filer_selected_items(filer_window);
1513 event = gtk_get_current_event();
1515 /* Eats paths */
1516 show_send_to_menu(paths, event);
1518 gdk_event_free(event);
1521 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1523 const char *argv[] = {"sh", "-c", NULL, NULL};
1524 gboolean close = action;
1526 argv[2] = o_menu_xterm.value;
1528 g_return_if_fail(window_with_focus != NULL);
1530 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1531 gtk_widget_destroy(window_with_focus->window);
1534 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1536 g_return_if_fail(window_with_focus != NULL);
1538 filer_change_to(window_with_focus, home_dir, NULL);
1541 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1543 g_return_if_fail(window_with_focus != NULL);
1545 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1546 filer_change_to(window_with_focus,
1547 window_with_focus->real_path, NULL);
1548 else
1549 delayed_error(_("This is already the canonical name "
1550 "for this directory."));
1553 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1555 g_return_if_fail(window_with_focus != NULL);
1557 filer_open_parent(window_with_focus);
1560 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1562 g_return_if_fail(window_with_focus != NULL);
1564 change_to_parent(window_with_focus);
1567 static void resize(gpointer data, guint action, GtkWidget *widget)
1569 g_return_if_fail(window_with_focus != NULL);
1571 filer_window_autosize(window_with_focus, TRUE);
1574 static void new_window(gpointer data, guint action, GtkWidget *widget)
1576 g_return_if_fail(window_with_focus != NULL);
1578 if (o_unique_filer_windows.int_value)
1580 report_error(_("You can't open a second view onto "
1581 "this directory because the `Unique Windows' option "
1582 "is turned on in the Options window."));
1584 else
1585 filer_opendir(window_with_focus->sym_path, window_with_focus);
1588 #if 0
1589 static void su_to_user(GtkWidget *dialog)
1591 char *argv[] = {
1592 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1593 GtkEntry *user;
1594 guchar *path;
1596 g_return_if_fail(dialog != NULL);
1598 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1599 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1601 g_return_if_fail(user != NULL && path != NULL);
1603 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1604 argv[3] = gtk_entry_get_text(user);
1605 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1606 argv[5] = path;
1608 if (!spawn(argv))
1609 report_error(_("fork: %s"), g_strerror(errno));
1611 g_free(argv[2]);
1612 g_free(argv[4]);
1614 gtk_widget_destroy(dialog);
1617 static void new_user(gpointer data, guint action, GtkWidget *widget)
1619 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1621 g_return_if_fail(window_with_focus != NULL);
1623 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1624 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1625 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1626 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1627 g_strdup(window_with_focus->path), g_free);
1629 vbox = gtk_vbox_new(FALSE, 4);
1630 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1631 gtk_box_pack_start(GTK_BOX(vbox),
1632 gtk_label_new(_("Browse as which user?")),
1633 TRUE, TRUE, 2);
1635 hbox = gtk_hbox_new(FALSE, 4);
1636 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1638 gtk_box_pack_start(GTK_BOX(hbox),
1639 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1641 entry = gtk_entry_new();
1642 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1643 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1644 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1645 gtk_widget_grab_focus(entry);
1646 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1647 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1648 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1650 hbox = gtk_hbox_new(TRUE, 0);
1651 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1653 button = gtk_button_new_with_label(_("OK"));
1654 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1655 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1656 gtk_window_set_default(GTK_WINDOW(dialog), button);
1657 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1658 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1660 button = gtk_button_new_with_label(_("Cancel"));
1661 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1662 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1663 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1664 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1665 GTK_OBJECT(dialog));
1667 gtk_widget_show_all(dialog);
1669 #endif
1671 static void close_window(gpointer data, guint action, GtkWidget *widget)
1673 g_return_if_fail(window_with_focus != NULL);
1675 gtk_widget_destroy(window_with_focus->window);
1678 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1680 MiniType type = (MiniType) action;
1682 g_return_if_fail(window_with_focus != NULL);
1684 /* Item needs to remain selected... */
1685 if (type == MINI_SHELL)
1686 window_with_focus->temp_item_selected = FALSE;
1688 minibuffer_show(window_with_focus, type);
1691 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1693 if (action == HELP_ABOUT)
1694 infobox_new(app_dir);
1695 else if (action == HELP_DIR)
1696 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1697 else if (action == HELP_MANUAL)
1699 gchar *manual = NULL;
1701 if (current_lang)
1703 manual = g_strconcat(app_dir, "/Help/Manual-",
1704 current_lang, ".html", NULL);
1705 if (access(manual, F_OK))
1707 g_free(manual);
1708 manual = NULL;
1712 if (!manual)
1713 manual = g_strconcat(app_dir,
1714 "/Help/Manual.html", NULL);
1716 run_by_path(manual);
1718 g_free(manual);
1720 else
1721 g_warning("Unknown help action %d\n", action);
1724 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1725 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1727 GList *items, *item;
1729 items = gtk_container_get_children(GTK_CONTAINER(menu));
1731 item = g_list_nth(items, from);
1732 while (item && n--)
1734 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1735 item = item->next;
1737 g_list_free(items);
1740 static void save_menus(void)
1742 char *menurc;
1744 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1745 if (menurc)
1747 gtk_accel_map_save(menurc);
1748 g_free(menurc);
1752 static void select_nth_item(GtkMenuShell *shell, int n)
1754 GList *items;
1755 GtkWidget *item;
1757 items = gtk_container_get_children(GTK_CONTAINER(shell));
1758 item = g_list_nth_data(items, n);
1760 g_return_if_fail(item != NULL);
1762 g_list_free(items);
1764 gtk_menu_shell_select_item(shell, item);
1767 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1769 DirItem *item;
1770 gchar *path;
1771 int n_selected;
1772 ViewIter iter;
1774 g_return_if_fail(window_with_focus != NULL);
1776 n_selected = view_count_selected(window_with_focus->view);
1778 if (n_selected < 1)
1780 const char *prompt;
1782 switch (action)
1784 case FILE_COPY_ITEM:
1785 prompt = _("Copy ... ?");
1786 break;
1787 case FILE_RENAME_ITEM:
1788 prompt = _("Rename ... ?");
1789 break;
1790 case FILE_LINK_ITEM:
1791 prompt = _("Symlink ... ?");
1792 break;
1793 case FILE_OPEN_FILE:
1794 prompt = _("Shift Open ... ?");
1795 break;
1796 case FILE_HELP:
1797 prompt = _("Help about ... ?");
1798 break;
1799 case FILE_SHOW_FILE_INFO:
1800 prompt = _("Examine ... ?");
1801 break;
1802 case FILE_RUN_ACTION:
1803 prompt = _("Set run action for ... ?");
1804 break;
1805 case FILE_SET_ICON:
1806 prompt = _("Set icon for ... ?");
1807 break;
1808 case FILE_SEND_TO:
1809 prompt = _("Send ... to ... ?");
1810 break;
1811 case FILE_DELETE:
1812 prompt = _("DELETE ... ?");
1813 break;
1814 case FILE_USAGE:
1815 prompt = _("Count the size of ... ?");
1816 break;
1817 case FILE_CHMOD_ITEMS:
1818 prompt = _("Set permissions on ... ?");
1819 break;
1820 case FILE_FIND:
1821 prompt = _("Search inside ... ?");
1822 break;
1823 case FILE_OPEN_VFS_AVFS:
1824 prompt = _("Look inside ... ?");
1825 break;
1826 default:
1827 g_warning("Unknown action!");
1828 return;
1830 filer_target_mode(window_with_focus, target_callback,
1831 GINT_TO_POINTER(action), prompt);
1832 return;
1835 switch (action)
1837 case FILE_SEND_TO:
1838 send_to(window_with_focus);
1839 return;
1840 case FILE_DELETE:
1841 delete(window_with_focus);
1842 return;
1843 case FILE_USAGE:
1844 usage(window_with_focus);
1845 return;
1846 case FILE_CHMOD_ITEMS:
1847 chmod_items(window_with_focus);
1848 return;
1849 case FILE_FIND:
1850 find(window_with_focus);
1851 return;
1852 case FILE_SHOW_FILE_INFO:
1854 GList *items;
1856 items = filer_selected_items(window_with_focus);
1857 infobox_show_list(items);
1858 g_list_foreach(items, (GFunc) g_free, NULL);
1859 g_list_free(items);
1860 return;
1862 default:
1863 break;
1866 /* All the following actions require exactly one file selected */
1868 if (n_selected > 1)
1870 report_error(_("You cannot do this to more than "
1871 "one item at a time"));
1872 return;
1875 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1877 item = iter.next(&iter);
1878 g_return_if_fail(item != NULL);
1879 g_return_if_fail(iter.next(&iter) == NULL);
1881 if (!item->image)
1882 item = dir_update_item(window_with_focus->directory,
1883 item->leafname);
1885 if (!item)
1887 report_error(_("Item no longer exists!"));
1888 return;
1891 path = make_path(window_with_focus->sym_path, item->leafname)->str;
1893 switch (action)
1895 case FILE_COPY_ITEM:
1896 src_dest_action_item(path, item->image,
1897 _("Copy"), copy_cb);
1898 break;
1899 case FILE_RENAME_ITEM:
1900 src_dest_action_item(path, item->image,
1901 _("Rename"), rename_cb);
1902 break;
1903 case FILE_LINK_ITEM:
1904 src_dest_action_item(path, item->image,
1905 _("Symlink"), link_cb);
1906 break;
1907 case FILE_OPEN_FILE:
1908 filer_openitem(window_with_focus, &iter,
1909 OPEN_SAME_WINDOW | OPEN_SHIFT);
1910 break;
1911 case FILE_HELP:
1912 show_item_help(path, item);
1913 break;
1914 case FILE_RUN_ACTION:
1915 run_action(item);
1916 break;
1917 case FILE_SET_ICON:
1918 icon_set_handler_dialog(item, path);
1919 break;
1920 case FILE_OPEN_VFS_AVFS:
1921 open_vfs_avfs(window_with_focus, item);
1922 break;
1923 default:
1924 g_warning("Unknown action!");
1925 return;
1929 static void show_key_help(GtkWidget *button, gpointer data)
1931 gboolean can_change_accels;
1933 g_object_get(G_OBJECT(gtk_settings_get_default()),
1934 "gtk-can-change-accels", &can_change_accels,
1935 NULL);
1937 if (!can_change_accels)
1939 info_message(_("User-definable shortcuts are disabled by "
1940 "default in Gtk2, and you have not enabled "
1941 "them. You can turn this feature on by:\n"
1942 "1) using an XSettings manager, such as ROX-Session\n"
1943 "or\n"
1944 "2) adding this line to ~/.gtkrc-2.0:\n"
1945 "\tgtk-can-change-accels = 1"));
1946 return;
1949 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1950 "- Open the menu over a filer window,\n"
1951 "- Move the pointer over the item you want to use,\n"
1952 "- Press the key you want attached to it.\n\n"
1953 "The key will appear next to the menu item and you can just press "
1954 "that key without opening the menu in future."));
1957 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1959 GtkWidget *button, *align;
1961 g_return_val_if_fail(option == NULL, NULL);
1963 align = gtk_alignment_new(0.5, 0.5, 0, 0);
1964 button = button_new_mixed(GTK_STOCK_DIALOG_INFO,
1965 _("Set keyboard shortcuts"));
1966 gtk_container_add(GTK_CONTAINER(align), button);
1967 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1969 return g_list_append(NULL, align);