r1658: Added Help menu with both About and Show Files entries (suggested by
[rox-filer.git] / ROX-Filer / src / menu.c
blob1d2d0bac2a6b5c06f923bdf2d5e35614d40c7868
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 "collection.h"
62 #include "display.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;
97 static GtkWidget *popup_menu = NULL; /* Currently open menu */
99 static gint updating_menu = 0; /* Non-zero => ignore activations */
100 static GList *send_to_paths = NULL;
102 static Option o_menu_iconsize, o_menu_xterm;
104 /* Static prototypes */
106 static void save_menus(void);
107 static void menu_closed(GtkWidget *widget);
108 static void shade_file_menu_items(gboolean shaded);
109 static void savebox_show(const gchar *action, const gchar *path,
110 MaskedPixmap *image, SaveCb callback);
111 static gint save_to_file(GObject *savebox,
112 const gchar *pathname, gpointer data);
113 static gboolean action_with_leaf(ActionFn action,
114 const gchar *current, const gchar *new);
115 static gboolean link_cb(GObject *savebox,
116 const gchar *initial, const gchar *path);
117 static void select_nth_item(GtkMenuShell *shell, int n);
118 static void new_file_type(gchar *templ);
119 static void do_send_to(gchar *templ);
120 static void show_send_to_menu(GList *paths, GdkEvent *event);
121 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
123 /* Note that for most of these callbacks none of the arguments are used. */
125 /* (action used in these three - DetailsType) */
126 static void huge_with(gpointer data, guint action, GtkWidget *widget);
127 static void large_with(gpointer data, guint action, GtkWidget *widget);
128 static void small_with(gpointer data, guint action, GtkWidget *widget);
130 static void sort_name(gpointer data, guint action, GtkWidget *widget);
131 static void sort_type(gpointer data, guint action, GtkWidget *widget);
132 static void sort_size(gpointer data, guint action, GtkWidget *widget);
133 static void sort_date(gpointer data, guint action, GtkWidget *widget);
135 static void hidden(gpointer data, guint action, GtkWidget *widget);
136 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
137 static void refresh(gpointer data, guint action, GtkWidget *widget);
139 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
141 static void select_all(gpointer data, guint action, GtkWidget *widget);
142 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
143 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
144 static void new_directory(gpointer data, guint action, GtkWidget *widget);
145 static void new_file(gpointer data, guint action, GtkWidget *widget);
146 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
149 static void open_parent(gpointer data, guint action, GtkWidget *widget);
150 static void home_directory(gpointer data, guint action, GtkWidget *widget);
151 static void new_window(gpointer data, guint action, GtkWidget *widget);
152 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
153 static void close_window(gpointer data, guint action, GtkWidget *widget);
154 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
156 /* (action used in this - MiniType) */
157 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
158 static void resize(gpointer data, guint action, GtkWidget *widget);
160 #define MENUS_NAME "menus2"
162 static GtkWidget *filer_menu; /* The popup filer menu */
163 static GtkWidget *filer_file_item; /* The File '' label */
164 static GtkWidget *filer_file_menu; /* The File '' menu */
165 static GtkWidget *file_shift_item; /* Shift Open label */
166 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
167 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
168 static GtkWidget *filer_new_window; /* The New Window item */
169 static GtkWidget *filer_new_menu; /* The New submenu */
170 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
172 #undef N_
173 #define N_(x) x
175 static GtkItemFactoryEntry filer_menu_def[] = {
176 {N_("Display"), NULL, NULL, 0, "<Branch>"},
177 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
178 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
179 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
180 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
181 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
182 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
183 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
184 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
185 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
186 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
187 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
188 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
189 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
190 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
191 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
192 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
193 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
194 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
195 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
196 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
197 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
198 {">", NULL, NULL, 0, "<Separator>"},
199 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
200 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
201 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
202 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
203 {">", NULL, NULL, 0, "<Separator>"},
204 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
205 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
206 {">" N_("Refresh"), NULL, refresh, 0, NULL},
207 {N_("File"), NULL, NULL, 0, "<Branch>"},
208 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
209 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
210 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
211 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
212 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
213 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
214 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
215 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
216 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
217 {">", NULL, NULL, 0, "<Separator>"},
218 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
219 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
220 {">" N_("Disk Usage"), NULL, file_op, FILE_USAGE, NULL},
221 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
222 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
223 {N_("Select"), NULL, NULL, 0, "<Branch>"},
224 {">" N_("Select All"), NULL, select_all, 0, NULL},
225 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
226 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
227 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
228 {N_("Options..."), NULL, menu_show_options, 0, NULL},
229 {N_("New"), NULL, NULL, 0, "<Branch>"},
230 {">" N_("Directory"), NULL, new_directory, 0, NULL},
231 {">" N_("Blank file"), NULL, new_file, 0, NULL},
232 {N_("Window"), NULL, NULL, 0, "<Branch>"},
233 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
234 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
235 {">" N_("New Window"), NULL, new_window, 0, NULL},
236 {">" N_("Home Directory"), NULL, home_directory, 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},
253 #define GET_MENU_ITEM(var, menu) \
254 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
256 #define GET_SMENU_ITEM(var, menu, sub) \
257 do { \
258 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
259 var = gtk_item_factory_get_widget(item_factory, tmp); \
260 g_free(tmp); \
261 } while (0)
263 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
264 do { \
265 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
266 var = gtk_item_factory_get_widget(item_factory, tmp); \
267 g_free(tmp); \
268 } while (0)
270 void ensure_filer_menu(void)
272 GList *items;
273 guchar *tmp;
274 GtkWidget *item;
275 GtkItemFactory *item_factory;
277 if (filer_keys)
278 return;
280 filer_keys = gtk_accel_group_new();
281 item_factory = menu_create(filer_menu_def,
282 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
283 "<filer>", filer_keys);
285 GET_MENU_ITEM(filer_menu, "filer");
286 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
287 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
288 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
289 "Show Thumbnails");
291 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
292 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
293 filer_follow_sym = GTK_BIN(item)->child;
295 /* File '' label... */
296 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
297 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
298 g_list_free(items);
300 /* Shift Open... label */
301 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
302 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
303 g_list_free(items);
305 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
306 filer_new_window = GTK_BIN(item)->child;
308 g_signal_connect(filer_menu, "unmap_event",
309 G_CALLBACK(menu_closed), NULL);
310 g_signal_connect(filer_file_menu, "unmap_event",
311 G_CALLBACK(menu_closed), NULL);
313 g_signal_connect(filer_keys, "accel_changed",
314 G_CALLBACK(save_menus), NULL);
317 void menu_init(void)
319 char *menurc;
321 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
322 if (menurc)
324 gtk_accel_map_load(menurc);
325 g_free(menurc);
328 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
329 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
330 option_add_saver(save_menus);
332 option_register_widget("menu-set-keys", set_keys_button);
335 /* Name is in the form "<panel>" */
336 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
337 const gchar *name, GtkAccelGroup *keys)
339 GtkItemFactory *item_factory;
340 GtkItemFactoryEntry *translated;
342 if (!keys)
344 keys = gtk_accel_group_new();
345 gtk_accel_group_lock(keys);
348 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
350 translated = translate_entries(def, n_entries);
351 gtk_item_factory_create_items(item_factory, n_entries,
352 translated, NULL);
353 free_translated_entries(translated, n_entries);
355 return item_factory;
358 /* Prevent the user from setting a short-cut on this item */
359 static void menuitem_no_shortcuts(GtkWidget *item)
361 /* XXX */
362 #if 0
363 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
365 _gtk_widget_set_accel_path(item, NULL, NULL);
366 g_free(menuitem->accel_path);
367 menuitem->accel_path = NULL;
368 #endif
371 static void shade_file_menu_items(gboolean shaded)
373 menu_set_items_shaded(filer_file_menu, shaded, 0, 5);
374 menu_set_items_shaded(filer_file_menu, shaded, 6, 3);
377 /* 'data' is an array of three ints:
378 * [ pointer_x, pointer_y, item_under_pointer ]
380 void position_menu(GtkMenu *menu, gint *x, gint *y,
381 gboolean *push_in, gpointer data)
383 int *pos = (int *) data;
384 GtkRequisition requisition;
385 GList *items, *next;
386 int y_shift = 0;
387 int item = pos[2];
389 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
391 while (item >= 0 && next)
393 int h = ((GtkWidget *) next->data)->requisition.height;
395 if (item > 0)
396 y_shift += h;
397 else
398 y_shift += h / 2;
400 next = next->next;
401 item--;
404 g_list_free(items);
406 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
408 *x = pos[0] - (requisition.width * 7 / 8);
409 *y = pos[1] - y_shift;
411 *x = CLAMP(*x, 0, screen_width - requisition.width);
412 *y = CLAMP(*y, 0, screen_height - requisition.height);
414 *push_in = FALSE;
417 #if 0
418 /* Used when you menu-click on the Large or Small toolbar tools */
419 void show_style_menu(FilerWindow *filer_window,
420 GdkEventButton *event,
421 GtkWidget *menu)
423 int pos[3];
425 pos[0] = event->x_root;
426 pos[1] = event->y_root;
427 pos[2] = 0;
429 window_with_focus = filer_window;
431 popup_menu = menu;
433 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
434 (gpointer) pos, event->button, event->time);
436 #endif
438 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
439 MenuIconStyle style, CallbackFn func,
440 gboolean separator, gboolean strip_ext,
441 gboolean recurse)
443 GList *widgets = NULL;
444 DirItem *ditem;
445 DIR *dir;
446 struct dirent *ent;
447 GtkWidget *item;
448 char *dname = NULL;
450 dname = pathdup(dir_name);
452 dir = opendir(dname);
453 if (!dir)
454 goto out;
456 if (separator)
458 item = gtk_menu_item_new();
459 widgets = g_list_append(widgets, item);
460 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
463 while ((ent = readdir(dir)))
465 char *dot, *leaf;
466 GtkWidget *hbox;
467 GtkWidget *img;
468 GtkWidget *label;
469 gchar *fname;
471 /* Ignore hidden files */
472 if (ent->d_name[0] == '.')
473 continue;
475 /* Strip off extension, if any */
476 dot = strchr(ent->d_name, '.');
477 if (strip_ext && dot)
478 leaf = g_strndup(ent->d_name, dot - ent->d_name);
479 else
480 leaf = g_strdup(ent->d_name);
482 fname = g_strconcat(dname, "/", ent->d_name, NULL);
483 ditem = diritem_new("");
484 diritem_restat(fname, ditem, NULL);
486 if (ditem->image && style != MIS_NONE)
488 GdkPixbuf *pixbuf;
490 switch (style) {
491 case MIS_LARGE:
492 pixbuf = ditem->image->pixbuf;
493 break;
494 default:
495 if (!ditem->image->sm_pixbuf)
496 pixmap_make_small(ditem->image);
497 pixbuf = ditem->image->sm_pixbuf;
498 break;
501 item = gtk_menu_item_new();
502 /* TODO: Find a way to allow short-cuts */
503 menuitem_no_shortcuts(item);
505 hbox = gtk_hbox_new(FALSE, 2);
506 gtk_container_add(GTK_CONTAINER(item), hbox);
508 img = gtk_image_new_from_pixbuf(pixbuf);
510 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
512 label = gtk_label_new(leaf);
513 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
514 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
517 else
518 item = gtk_menu_item_new_with_label(leaf);
520 /* If it is a directory (but NOT an AppDir) and we are
521 * recursing then set up a sub menu.
523 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
524 !(ditem->flags & ITEM_FLAG_APPDIR))
526 GtkWidget *sub;
527 GList *new_widgets;
529 sub = gtk_menu_new();
530 new_widgets = menu_from_dir(sub, fname, style, func,
531 separator, strip_ext, FALSE);
532 g_list_free(new_widgets);
533 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
535 else
536 g_signal_connect_swapped(item, "activate",
537 G_CALLBACK(func), fname);
539 diritem_free(ditem);
540 g_free(leaf);
542 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
543 g_signal_connect_swapped(item, "destroy",
544 G_CALLBACK(g_free), fname);
546 widgets = g_list_append(widgets, item);
549 closedir(dir);
550 out:
551 g_free(dname);
553 return widgets;
556 /* Scan the templates dir and create entries for the New menu */
557 static void update_new_files_menu(MenuIconStyle style)
559 static GList *widgets = NULL;
561 gchar *templ_dname = NULL;
563 if (widgets)
565 GList *next;
567 for (next = widgets; next; next = next->next)
568 gtk_widget_destroy((GtkWidget *) next->data);
570 g_list_free(widgets);
571 widgets = NULL;
574 templ_dname = choices_find_path_load("Templates", "");
575 if (templ_dname)
577 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
578 (CallbackFn) new_file_type, TRUE, TRUE,
579 FALSE);
580 g_free(templ_dname);
582 gtk_widget_show_all(filer_new_menu);
585 /* 'item' is the number of the item to appear under the pointer. */
586 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
588 int pos[3];
589 int button = 0;
590 guint32 time = 0;
592 if (event && (event->type == GDK_BUTTON_PRESS ||
593 event->type == GDK_BUTTON_RELEASE))
595 GdkEventButton *bev = (GdkEventButton *) event;
597 pos[0] = bev->x_root;
598 pos[1] = bev->y_root;
599 button = bev->button;
600 time = bev->time;
602 else if (event && event->type == GDK_KEY_PRESS)
604 GdkEventKey *kev = (GdkEventKey *) event;
606 get_pointer_xy(pos, pos + 1);
607 time = kev->time;
609 else
610 get_pointer_xy(pos, pos + 1);
612 pos[2] = item;
614 gtk_widget_show_all(menu);
615 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
616 position_menu, (gpointer) pos, button, time);
617 select_nth_item(GTK_MENU_SHELL(menu), item);
620 /* Hide the popup menu, if any */
621 void menu_popdown(void)
623 if (popup_menu)
624 gtk_menu_popdown(GTK_MENU(popup_menu));
627 static MenuIconStyle get_menu_icon_style(void)
629 MenuIconStyle mis;
630 int display;
632 mis = o_menu_iconsize.int_value;
634 switch (mis)
636 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
637 return mis;
638 default:
639 break;
642 if (mis == MIS_CURRENT && window_with_focus)
644 switch (window_with_focus->display_style)
646 case HUGE_ICONS:
647 case LARGE_ICONS:
648 return MIS_LARGE;
649 case SMALL_ICONS:
650 return MIS_SMALL;
651 default:
652 break;
656 display = o_display_size.int_value;
657 switch (display)
659 case HUGE_ICONS:
660 case LARGE_ICONS:
661 return MIS_LARGE;
662 case SMALL_ICONS:
663 return MIS_SMALL;
664 default:
665 break;
668 return MIS_SMALL;
671 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
673 DirItem *file_item = NULL;
674 GdkModifierType state = 0;
675 int n_selected;
677 n_selected = view_count_selected(filer_window->view);
679 ensure_filer_menu();
681 updating_menu++;
683 /* Remove previous AppMenu, if any */
684 appmenu_remove();
686 window_with_focus = filer_window;
688 if (event->type == GDK_BUTTON_PRESS)
689 state = ((GdkEventButton *) event)->state;
690 else if (event->type == GDK_KEY_PRESS)
691 state = ((GdkEventKey *) event)->state;
693 if (n_selected == 0 && item >= 0)
695 filer_window->temp_item_selected = TRUE;
696 collection_select_item(filer_window->collection, item);
697 n_selected = view_count_selected(filer_window->view);
699 else
701 filer_window->temp_item_selected = FALSE;
704 /* Short-cut to the Send To menu */
705 if (state & GDK_SHIFT_MASK)
707 GList *paths;
709 updating_menu--;
711 if (n_selected == 0)
713 report_error(
714 _("You should Shift+Menu click over a file to "
715 "send it somewhere"));
716 return;
719 paths = filer_selected_items(filer_window);
721 show_send_to_menu(paths, event); /* (paths eaten) */
723 return;
727 GtkWidget *file_label, *file_menu;
728 GString *buffer;
729 DirItem *item;
731 file_label = filer_file_item;
732 file_menu = filer_file_menu;
733 gtk_check_menu_item_set_active(
734 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
735 filer_window->show_thumbs);
736 gtk_check_menu_item_set_active(
737 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
738 filer_window->show_hidden);
739 buffer = g_string_new(NULL);
741 switch (n_selected)
743 case 0:
744 g_string_assign(buffer, _("Next Click"));
745 shade_file_menu_items(FALSE);
746 break;
747 case 1:
748 item = filer_selected_item(filer_window);
749 if (!item->image)
750 dir_update_item(filer_window->directory,
751 item->leafname);
752 shade_file_menu_items(FALSE);
753 file_item = filer_selected_item(filer_window);
754 g_string_sprintf(buffer, "%s '%s'",
755 basetype_name(file_item),
756 file_item->leafname);
757 if (!can_set_run_action(file_item))
758 menu_set_items_shaded(filer_file_menu,
759 TRUE, 6, 1);
760 break;
761 default:
762 shade_file_menu_items(TRUE);
763 g_string_sprintf(buffer, _("%d items"),
764 n_selected);
765 break;
767 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
768 g_string_free(buffer, TRUE);
770 menu_show_shift_action(file_shift_item, file_item,
771 n_selected == 0);
772 if (file_item)
773 appmenu_add(make_path(filer_window->sym_path,
774 file_item->leafname)->str,
775 file_item, filer_file_menu);
778 update_new_files_menu(get_menu_icon_style());
780 gtk_widget_set_sensitive(filer_new_window,
781 !o_unique_filer_windows.int_value);
782 gtk_widget_set_sensitive(filer_follow_sym,
783 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
785 popup_menu = (state & GDK_CONTROL_MASK)
786 ? filer_file_menu
787 : filer_menu;
789 updating_menu--;
791 show_popup_menu(popup_menu, event,
792 popup_menu == filer_file_menu ? 5 : 1);
795 static void menu_closed(GtkWidget *widget)
797 if (window_with_focus == NULL || widget != popup_menu)
798 return; /* Close panel item chosen? */
800 popup_menu = NULL;
802 if (window_with_focus->temp_item_selected)
804 view_clear_selection(window_with_focus->view);
805 window_with_focus->temp_item_selected = FALSE;
809 static void target_callback(FilerWindow *filer_window,
810 gint item,
811 gpointer action)
813 Collection *collection;
815 g_return_if_fail(filer_window != NULL);
817 collection = filer_window->collection;
819 window_with_focus = filer_window;
821 /* Don't grab the primary selection */
822 filer_window->temp_item_selected = TRUE;
824 collection_wink_item(collection, item);
825 collection_clear_except(collection, item);
826 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
828 if (item < collection->number_of_items)
829 collection_unselect_item(collection, item);
830 filer_window->temp_item_selected = FALSE;
833 /* Set the text of the 'Shift Open...' menu item.
834 * If icon is NULL, reset the text and also shade it, unless 'next'.
836 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
838 guchar *shift_action = NULL;
840 if (item)
842 if (item->flags & ITEM_FLAG_MOUNT_POINT)
844 if (item->flags & ITEM_FLAG_MOUNTED)
845 shift_action = N_("Unmount");
846 else
847 shift_action = N_("Mount");
849 else if (item->flags & ITEM_FLAG_SYMLINK)
850 shift_action = N_("Show Target");
851 else if (item->base_type == TYPE_DIRECTORY)
852 shift_action = N_("Look Inside");
853 else if (item->base_type == TYPE_FILE)
854 shift_action = N_("Open As Text");
856 gtk_label_set_text(GTK_LABEL(menu_item),
857 shift_action ? _(shift_action)
858 : _("Shift Open"));
859 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
862 /* Actions */
864 static void huge_with(gpointer data, guint action, GtkWidget *widget)
866 display_set_layout(window_with_focus, HUGE_ICONS, action);
869 static void large_with(gpointer data, guint action, GtkWidget *widget)
871 display_set_layout(window_with_focus, LARGE_ICONS, action);
874 static void small_with(gpointer data, guint action, GtkWidget *widget)
876 display_set_layout(window_with_focus, SMALL_ICONS, action);
879 static void sort_name(gpointer data, guint action, GtkWidget *widget)
881 g_return_if_fail(window_with_focus != NULL);
883 display_set_sort_fn(window_with_focus, sort_by_name);
886 static void sort_type(gpointer data, guint action, GtkWidget *widget)
888 g_return_if_fail(window_with_focus != NULL);
890 display_set_sort_fn(window_with_focus, sort_by_type);
893 static void sort_date(gpointer data, guint action, GtkWidget *widget)
895 g_return_if_fail(window_with_focus != NULL);
897 display_set_sort_fn(window_with_focus, sort_by_date);
900 static void sort_size(gpointer data, guint action, GtkWidget *widget)
902 g_return_if_fail(window_with_focus != NULL);
904 display_set_sort_fn(window_with_focus, sort_by_size);
907 static void hidden(gpointer data, guint action, GtkWidget *widget)
909 if (updating_menu)
910 return;
912 g_return_if_fail(window_with_focus != NULL);
914 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
917 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
919 if (updating_menu)
920 return;
922 g_return_if_fail(window_with_focus != NULL);
924 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
927 static void refresh(gpointer data, guint action, GtkWidget *widget)
929 g_return_if_fail(window_with_focus != NULL);
931 full_refresh();
932 filer_update_dir(window_with_focus, TRUE);
935 static void delete(FilerWindow *filer_window)
937 GList *paths;
938 paths = filer_selected_items(filer_window);
939 action_delete(paths);
940 g_list_foreach(paths, (GFunc) g_free, NULL);
941 g_list_free(paths);
944 static void usage(FilerWindow *filer_window)
946 GList *paths;
947 paths = filer_selected_items(filer_window);
948 action_usage(paths);
949 g_list_foreach(paths, (GFunc) g_free, NULL);
950 g_list_free(paths);
953 static void chmod_items(FilerWindow *filer_window)
955 GList *paths;
956 paths = filer_selected_items(filer_window);
957 action_chmod(paths, FALSE, NULL);
958 g_list_foreach(paths, (GFunc) g_free, NULL);
959 g_list_free(paths);
962 static void find(FilerWindow *filer_window)
964 GList *paths;
965 paths = filer_selected_items(filer_window);
966 action_find(paths);
967 g_list_foreach(paths, (GFunc) g_free, NULL);
968 g_list_free(paths);
971 /* This creates a new savebox widget, and allows the user to pick a new path
972 * for the file.
973 * Once the new path has been picked, the callback will be called with
974 * both the current and new paths.
975 * NOTE: This function unrefs 'image'!
977 static void savebox_show(const gchar *action, const gchar *path,
978 MaskedPixmap *image, SaveCb callback)
980 GtkWidget *savebox = NULL;
981 GtkWidget *check_relative = NULL;
983 g_return_if_fail(image != NULL);
985 savebox = gtk_savebox_new(action);
987 if (callback == link_cb)
989 check_relative = gtk_check_button_new_with_mnemonic(
990 _("_Relative link"));
991 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
992 TRUE);
994 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
995 gtk_tooltips_set_tip(tooltips, check_relative,
996 _("If on, the symlink will store the path from the "
997 "symlink to the target file. Use this if the symlink "
998 "and the target will be moved together.\n"
999 "If off, the path from the root directory is stored - "
1000 "use this if the symlink may move but the target will "
1001 "stay put."), NULL);
1002 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1003 check_relative, FALSE, TRUE, 0);
1004 gtk_widget_show(check_relative);
1007 g_signal_connect(savebox, "save_to_file",
1008 G_CALLBACK(save_to_file), NULL);
1010 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1011 g_strdup(path), g_free);
1012 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1013 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1015 gtk_window_set_title(GTK_WINDOW(savebox), action);
1017 if (g_utf8_validate(path, -1, NULL))
1018 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1019 else
1021 gchar *u8;
1022 u8 = to_utf8(path);
1023 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1024 g_free(u8);
1026 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1027 g_object_unref(image);
1029 gtk_widget_show(savebox);
1032 static gint save_to_file(GObject *savebox,
1033 const gchar *pathname, gpointer data)
1035 SaveCb callback;
1036 const gchar *current_path;
1038 callback = g_object_get_data(savebox, "action_callback");
1039 current_path = g_object_get_data(savebox, "current_path");
1041 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1042 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1044 return callback(savebox, current_path, pathname)
1045 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1048 static gboolean copy_cb(GObject *savebox,
1049 const gchar *current, const gchar *new)
1051 return action_with_leaf(action_copy, current, new);
1054 static gboolean action_with_leaf(ActionFn action,
1055 const gchar *current, const gchar *new)
1057 const char *leaf;
1058 char *new_dir;
1059 GList *local_paths;
1061 if (new[0] != '/')
1063 report_error(_("New pathname is not absolute"));
1064 return FALSE;
1067 if (new[strlen(new) - 1] == '/')
1069 new_dir = g_strdup(new);
1070 leaf = NULL;
1072 else
1074 const gchar *slash;
1076 slash = strrchr(new, '/');
1077 new_dir = g_strndup(new, slash - new);
1078 leaf = slash + 1;
1081 local_paths = g_list_append(NULL, (gchar *) current);
1082 action(local_paths, new_dir, leaf, -1);
1083 g_list_free(local_paths);
1085 g_free(new_dir);
1087 return TRUE;
1090 /* Open a savebox to act on the selected file.
1091 * Call 'callback' later to perform the operation.
1093 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1094 const gchar *action, SaveCb callback)
1096 g_object_ref(image);
1097 savebox_show(action, path, image, callback);
1100 static gboolean rename_cb(GObject *savebox,
1101 const gchar *current, const gchar *new)
1103 return action_with_leaf(action_move, current, new);
1106 static gboolean link_cb(GObject *savebox,
1107 const gchar *initial, const gchar *path)
1109 GtkToggleButton *check_relative;
1110 struct stat info;
1111 int err;
1112 gchar *link_path;
1114 check_relative = g_object_get_data(savebox, "check_relative");
1116 if (gtk_toggle_button_get_active(check_relative))
1117 link_path = get_relative_path(path, initial);
1118 else
1119 link_path = g_strdup(initial);
1121 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1123 GtkWidget *box, *button;
1124 gint ans;
1126 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1127 GTK_BUTTONS_CANCEL,
1128 _("Symlink from '%s' already exists. "
1129 "Replace it with a link to '%s'?"),
1130 path, link_path);
1132 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1134 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1135 gtk_widget_show(button);
1136 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1137 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1138 button, GTK_RESPONSE_OK);
1139 gtk_dialog_set_default_response(GTK_DIALOG(box),
1140 GTK_RESPONSE_OK);
1142 ans = gtk_dialog_run(GTK_DIALOG(box));
1143 gtk_widget_destroy(box);
1145 if (ans != GTK_RESPONSE_OK)
1147 g_free(link_path);
1148 return FALSE;
1151 unlink(path);
1154 err = symlink(link_path, path);
1155 g_free(link_path);
1157 if (err)
1159 report_error("symlink: %s", g_strerror(errno));
1160 return FALSE;
1163 dir_check_this(path);
1165 return TRUE;
1168 static void run_action(DirItem *item)
1170 if (can_set_run_action(item))
1171 type_set_handler_dialog(item->mime_type);
1172 else
1173 report_error(
1174 _("You can only set the run action for a "
1175 "regular file"));
1178 void open_home(gpointer data, guint action, GtkWidget *widget)
1180 filer_opendir(home_dir, NULL);
1183 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1185 gchar *path;
1187 path = g_strconcat(filer_window->sym_path,
1188 "/", item->leafname, "#", NULL);
1190 filer_change_to(filer_window, path, NULL);
1191 g_free(path);
1194 static void select_all(gpointer data, guint action, GtkWidget *widget)
1196 g_return_if_fail(window_with_focus != NULL);
1198 window_with_focus->temp_item_selected = FALSE;
1199 collection_select_all(window_with_focus->collection);
1202 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1204 g_return_if_fail(window_with_focus != NULL);
1206 window_with_focus->temp_item_selected = FALSE;
1207 view_clear_selection(window_with_focus->view);
1210 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1212 g_return_if_fail(window_with_focus != NULL);
1214 window_with_focus->temp_item_selected = FALSE;
1215 collection_invert_selection(window_with_focus->collection);
1218 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1220 GtkWidget *win;
1222 win = options_show();
1224 if (win)
1226 number_of_windows++;
1227 g_signal_connect(win, "destroy",
1228 G_CALLBACK(one_less_window), NULL);
1232 static gboolean new_directory_cb(GObject *savebox,
1233 const gchar *initial, const gchar *path)
1235 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1237 report_error("mkdir: %s", g_strerror(errno));
1238 return FALSE;
1241 dir_check_this(path);
1243 if (filer_exists(window_with_focus))
1245 guchar *leaf;
1246 leaf = strrchr(path, '/');
1247 if (leaf)
1248 display_set_autoselect(window_with_focus, leaf + 1);
1251 return TRUE;
1254 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1256 g_return_if_fail(window_with_focus != NULL);
1258 savebox_show(_("Create"),
1259 make_path(window_with_focus->sym_path, _("NewDir"))->str,
1260 type_to_icon(inode_directory), new_directory_cb);
1263 static gboolean new_file_cb(GObject *savebox,
1264 const gchar *initial, const gchar *path)
1266 int fd;
1268 fd = open(path, O_CREAT | O_EXCL, 0666);
1270 if (fd == -1)
1272 report_error(_("Error creating '%s': %s"),
1273 path, g_strerror(errno));
1274 return FALSE;
1277 if (close(fd))
1278 report_error(_("Error creating '%s': %s"),
1279 path, g_strerror(errno));
1281 dir_check_this(path);
1283 if (filer_exists(window_with_focus))
1285 guchar *leaf;
1286 leaf = strrchr(path, '/');
1287 if (leaf)
1288 display_set_autoselect(window_with_focus, leaf + 1);
1291 return TRUE;
1294 static void new_file(gpointer data, guint action, GtkWidget *widget)
1296 g_return_if_fail(window_with_focus != NULL);
1298 savebox_show(_("Create"),
1299 make_path(window_with_focus->sym_path, _("NewFile"))->str,
1300 type_to_icon(text_plain),
1301 new_file_cb);
1304 static gboolean new_file_type_cb(GObject *savebox,
1305 const gchar *initial, const gchar *path)
1307 const gchar *oleaf, *leaf;
1308 gchar *templ, *templ_dname, *dest;
1309 GList *paths;
1311 /* We can work out the template path from the initial name */
1312 oleaf = g_basename(initial);
1313 templ_dname = choices_find_path_load("Templates", "");
1314 if (!templ_dname)
1316 report_error(
1317 _("Error creating file: could not find the template for %s"),
1318 oleaf);
1319 return FALSE;
1322 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1323 g_free(templ_dname);
1325 dest = g_dirname(path);
1326 leaf = g_basename(path);
1327 paths = g_list_append(NULL, templ);
1329 action_copy(paths, dest, leaf, TRUE);
1331 g_list_free(paths);
1332 g_free(dest);
1333 g_free(templ);
1335 if (filer_exists(window_with_focus))
1336 display_set_autoselect(window_with_focus, leaf);
1338 return TRUE;
1341 static void do_send_to(gchar *templ)
1343 g_return_if_fail(send_to_paths != NULL);
1345 run_with_files(templ, send_to_paths);
1348 static void new_file_type(gchar *templ)
1350 const gchar *leaf;
1351 MIME_type *type;
1353 g_return_if_fail(window_with_focus != NULL);
1355 leaf = g_basename(templ);
1356 type = type_get_type(templ);
1358 savebox_show(_("Create"),
1359 make_path(window_with_focus->sym_path, leaf)->str,
1360 type_to_icon(type),
1361 new_file_type_cb);
1364 static void customise_send_to(gpointer data)
1366 GPtrArray *path;
1367 guchar *save;
1368 GString *dirs;
1369 int i;
1371 dirs = g_string_new(NULL);
1373 path = choices_list_dirs("");
1374 for (i = 0; i < path->len; i++)
1376 guchar *old = (guchar *) path->pdata[i];
1378 g_string_append(dirs, old);
1379 g_string_append(dirs, "SendTo\n");
1381 choices_free_list(path);
1383 save = choices_find_path_save("", "SendTo", TRUE);
1384 if (save)
1385 mkdir(save, 0777);
1387 info_message(
1388 _("The `Send To' menu provides a quick way to send some files "
1389 "to an application. The applications listed are those in "
1390 "the following directories:\n\n%s\n%s\n"
1391 "The `Send To' menu may be opened by Shift+Menu clicking "
1392 "over a file.\n\n"
1393 "Advanced use:\n"
1394 "You can also create subdirectories called "
1395 "`.text_html', `.text', etc which will only be "
1396 "shown for files of that type. `.group' is shown "
1397 "only when multiple files are selected."),
1398 dirs->str,
1399 save ? _("I'll show you your SendTo directory now; you should "
1400 "symlink (Ctrl+Shift drag) any applications you want "
1401 "into it.")
1402 : _("Your CHOICESPATH variable setting prevents "
1403 "customisations - sorry."));
1405 g_string_free(dirs, TRUE);
1407 if (save)
1408 filer_opendir(save, NULL);
1411 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1412 * to the menu.
1414 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1416 gchar *searchdir;
1417 GPtrArray *paths;
1418 int i;
1420 if (subtype)
1421 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1422 else if (type)
1423 searchdir = g_strdup_printf("SendTo/.%s", type);
1424 else
1425 searchdir = g_strdup("SendTo");
1427 paths = choices_list_dirs(searchdir);
1428 g_free(searchdir);
1430 for (i = 0; i < paths->len; i++)
1432 GList *widgets = NULL;
1433 guchar *dir = (guchar *) paths->pdata[i];
1435 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1436 (CallbackFn) do_send_to,
1437 FALSE, FALSE, TRUE);
1439 if (widgets)
1440 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1441 gtk_menu_item_new());
1443 g_list_free(widgets); /* TODO: Get rid of this */
1446 choices_free_list(paths);
1449 /* Scan the SendTo dir and create and show the Send To menu.
1450 * The 'paths' list and every path in it is claimed, and will be
1451 * freed later -- don't free it yourself!
1453 static void show_send_to_menu(GList *paths, GdkEvent *event)
1455 GtkWidget *menu, *item;
1457 menu = gtk_menu_new();
1459 if (g_list_length(paths) == 1)
1461 DirItem *item;
1463 item = diritem_new("");
1464 diritem_restat(paths->data, item, NULL);
1466 add_sendto(menu,
1467 item->mime_type->media_type,
1468 item->mime_type->subtype);
1470 add_sendto(menu, item->mime_type->media_type, NULL);
1472 diritem_free(item);
1474 else
1475 add_sendto(menu, "group", NULL);
1477 add_sendto(menu, NULL, NULL);
1479 item = gtk_menu_item_new_with_label(_("Customise"));
1480 g_signal_connect_swapped(item, "activate",
1481 G_CALLBACK(customise_send_to), NULL);
1482 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1484 if (send_to_paths)
1486 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1487 g_list_free(send_to_paths);
1489 send_to_paths = paths;
1491 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1493 popup_menu = menu;
1494 show_popup_menu(menu, event, 0);
1497 static void send_to(FilerWindow *filer_window)
1499 GList *paths;
1500 GdkEvent *event;
1502 paths = filer_selected_items(filer_window);
1503 event = gtk_get_current_event();
1505 /* Eats paths */
1506 show_send_to_menu(paths, event);
1508 gdk_event_free(event);
1511 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1513 const char *argv[] = {"sh", "-c", NULL, NULL};
1514 gboolean close = action;
1516 argv[2] = o_menu_xterm.value;
1518 g_return_if_fail(window_with_focus != NULL);
1520 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1521 gtk_widget_destroy(window_with_focus->window);
1524 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1526 g_return_if_fail(window_with_focus != NULL);
1528 filer_change_to(window_with_focus, home_dir, NULL);
1531 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1533 g_return_if_fail(window_with_focus != NULL);
1535 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1536 filer_change_to(window_with_focus,
1537 window_with_focus->real_path, NULL);
1538 else
1539 delayed_error(_("This is already the canonical name "
1540 "for this directory."));
1543 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1545 g_return_if_fail(window_with_focus != NULL);
1547 filer_open_parent(window_with_focus);
1550 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1552 g_return_if_fail(window_with_focus != NULL);
1554 change_to_parent(window_with_focus);
1557 static void resize(gpointer data, guint action, GtkWidget *widget)
1559 g_return_if_fail(window_with_focus != NULL);
1561 filer_window_autosize(window_with_focus, TRUE);
1564 static void new_window(gpointer data, guint action, GtkWidget *widget)
1566 g_return_if_fail(window_with_focus != NULL);
1568 if (o_unique_filer_windows.int_value)
1570 report_error(_("You can't open a second view onto "
1571 "this directory because the `Unique Windows' option "
1572 "is turned on in the Options window."));
1574 else
1575 filer_opendir(window_with_focus->sym_path, window_with_focus);
1578 #if 0
1579 static void su_to_user(GtkWidget *dialog)
1581 char *argv[] = {
1582 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1583 GtkEntry *user;
1584 guchar *path;
1586 g_return_if_fail(dialog != NULL);
1588 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1589 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1591 g_return_if_fail(user != NULL && path != NULL);
1593 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1594 argv[3] = gtk_entry_get_text(user);
1595 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1596 argv[5] = path;
1598 if (!spawn(argv))
1599 report_error(_("fork: %s"), g_strerror(errno));
1601 g_free(argv[2]);
1602 g_free(argv[4]);
1604 gtk_widget_destroy(dialog);
1607 static void new_user(gpointer data, guint action, GtkWidget *widget)
1609 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1611 g_return_if_fail(window_with_focus != NULL);
1613 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1614 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1615 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1616 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1617 g_strdup(window_with_focus->path), g_free);
1619 vbox = gtk_vbox_new(FALSE, 4);
1620 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1621 gtk_box_pack_start(GTK_BOX(vbox),
1622 gtk_label_new(_("Browse as which user?")),
1623 TRUE, TRUE, 2);
1625 hbox = gtk_hbox_new(FALSE, 4);
1626 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1628 gtk_box_pack_start(GTK_BOX(hbox),
1629 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1631 entry = gtk_entry_new();
1632 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1633 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1634 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1635 gtk_widget_grab_focus(entry);
1636 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1637 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1638 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1640 hbox = gtk_hbox_new(TRUE, 0);
1641 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1643 button = gtk_button_new_with_label(_("OK"));
1644 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1645 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1646 gtk_window_set_default(GTK_WINDOW(dialog), button);
1647 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1648 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1650 button = gtk_button_new_with_label(_("Cancel"));
1651 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1652 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1653 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1654 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1655 GTK_OBJECT(dialog));
1657 gtk_widget_show_all(dialog);
1659 #endif
1661 static void close_window(gpointer data, guint action, GtkWidget *widget)
1663 g_return_if_fail(window_with_focus != NULL);
1665 gtk_widget_destroy(window_with_focus->window);
1668 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1670 MiniType type = (MiniType) action;
1672 g_return_if_fail(window_with_focus != NULL);
1674 /* Item needs to remain selected... */
1675 if (type == MINI_SHELL)
1676 window_with_focus->temp_item_selected = FALSE;
1678 minibuffer_show(window_with_focus, type);
1681 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1683 if (action == HELP_ABOUT)
1684 infobox_new(app_dir);
1685 else if (action == HELP_DIR)
1686 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1687 else
1688 g_warning("Unknown help action %d\n", action);
1691 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1692 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1694 GList *items, *item;
1696 items = gtk_container_get_children(GTK_CONTAINER(menu));
1698 item = g_list_nth(items, from);
1699 while (item && n--)
1701 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1702 item = item->next;
1704 g_list_free(items);
1707 static void save_menus(void)
1709 char *menurc;
1711 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1712 if (menurc)
1714 gtk_accel_map_save(menurc);
1715 g_free(menurc);
1719 static void select_nth_item(GtkMenuShell *shell, int n)
1721 GList *items;
1722 GtkWidget *item;
1724 items = gtk_container_get_children(GTK_CONTAINER(shell));
1725 item = g_list_nth_data(items, n);
1727 g_return_if_fail(item != NULL);
1729 g_list_free(items);
1731 gtk_menu_shell_select_item(shell, item);
1734 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1736 DirItem *item;
1737 gchar *path;
1738 int n_selected;
1740 g_return_if_fail(window_with_focus != NULL);
1742 n_selected = view_count_selected(window_with_focus->view);
1744 if (n_selected < 1)
1746 const char *prompt;
1748 switch (action)
1750 case FILE_COPY_ITEM:
1751 prompt = _("Copy ... ?");
1752 break;
1753 case FILE_RENAME_ITEM:
1754 prompt = _("Rename ... ?");
1755 break;
1756 case FILE_LINK_ITEM:
1757 prompt = _("Symlink ... ?");
1758 break;
1759 case FILE_OPEN_FILE:
1760 prompt = _("Shift Open ... ?");
1761 break;
1762 case FILE_HELP:
1763 prompt = _("Help about ... ?");
1764 break;
1765 case FILE_SHOW_FILE_INFO:
1766 prompt = _("Examine ... ?");
1767 break;
1768 case FILE_RUN_ACTION:
1769 prompt = _("Set run action for ... ?");
1770 break;
1771 case FILE_SET_ICON:
1772 prompt = _("Set icon for ... ?");
1773 break;
1774 case FILE_SEND_TO:
1775 prompt = _("Send ... to ... ?");
1776 break;
1777 case FILE_DELETE:
1778 prompt = _("DELETE ... ?");
1779 break;
1780 case FILE_USAGE:
1781 prompt = _("Count the size of ... ?");
1782 break;
1783 case FILE_CHMOD_ITEMS:
1784 prompt = _("Set permissions on ... ?");
1785 break;
1786 case FILE_FIND:
1787 prompt = _("Search inside ... ?");
1788 break;
1789 case FILE_OPEN_VFS_AVFS:
1790 prompt = _("Look inside ... ?");
1791 break;
1792 default:
1793 g_warning("Unknown action!");
1794 return;
1796 filer_target_mode(window_with_focus, target_callback,
1797 GINT_TO_POINTER(action), prompt);
1798 return;
1801 switch (action)
1803 case FILE_SEND_TO:
1804 send_to(window_with_focus);
1805 return;
1806 case FILE_DELETE:
1807 delete(window_with_focus);
1808 return;
1809 case FILE_USAGE:
1810 usage(window_with_focus);
1811 return;
1812 case FILE_CHMOD_ITEMS:
1813 chmod_items(window_with_focus);
1814 return;
1815 case FILE_FIND:
1816 find(window_with_focus);
1817 return;
1818 case FILE_SHOW_FILE_INFO:
1820 GList *items;
1822 items = filer_selected_items(window_with_focus);
1823 infobox_show_list(items);
1824 g_list_foreach(items, (GFunc) g_free, NULL);
1825 g_list_free(items);
1826 return;
1828 default:
1829 break;
1832 /* All the following actions require exactly one file selected */
1834 if (n_selected > 1)
1836 report_error(_("You cannot do this to more than "
1837 "one item at a time"));
1838 return;
1841 item = filer_selected_item(window_with_focus);
1842 g_return_if_fail(item != NULL);
1843 if (!item->image)
1844 item = dir_update_item(window_with_focus->directory,
1845 item->leafname);
1847 if (!item)
1849 report_error(_("Item no longer exists!"));
1850 return;
1853 path = make_path(window_with_focus->sym_path, item->leafname)->str;
1855 switch (action)
1857 case FILE_COPY_ITEM:
1858 src_dest_action_item(path, item->image,
1859 _("Copy"), copy_cb);
1860 break;
1861 case FILE_RENAME_ITEM:
1862 src_dest_action_item(path, item->image,
1863 _("Rename"), rename_cb);
1864 break;
1865 case FILE_LINK_ITEM:
1866 src_dest_action_item(path, item->image,
1867 _("Symlink"), link_cb);
1868 break;
1869 case FILE_OPEN_FILE:
1870 filer_openitem(window_with_focus,
1871 collection_selected_item_number(
1872 window_with_focus->collection),
1873 OPEN_SAME_WINDOW | OPEN_SHIFT);
1874 break;
1875 case FILE_HELP:
1876 show_item_help(path, item);
1877 break;
1878 case FILE_RUN_ACTION:
1879 run_action(item);
1880 break;
1881 case FILE_SET_ICON:
1882 icon_set_handler_dialog(item, path);
1883 break;
1884 case FILE_OPEN_VFS_AVFS:
1885 open_vfs_avfs(window_with_focus, item);
1886 break;
1887 default:
1888 g_warning("Unknown action!");
1889 return;
1893 static void show_key_help(GtkWidget *button, gpointer data)
1895 gboolean can_change_accels;
1897 g_object_get(G_OBJECT(gtk_settings_get_default()),
1898 "gtk-can-change-accels", &can_change_accels,
1899 NULL);
1901 if (!can_change_accels)
1903 info_message(_("User-definable shortcuts are disabled by "
1904 "default in Gtk2, and you have not enabled "
1905 "them. You can turn this feature on by:\n"
1906 "1) using an XSettings manager, such as ROX-Session\n"
1907 "or\n"
1908 "2) adding this line to ~/.gtkrc-2.0:\n"
1909 "\tgtk-can-change-accels = 1"));
1910 return;
1913 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1914 "- Open the menu over a filer window,\n"
1915 "- Move the pointer over the item you want to use,\n"
1916 "- Press the key you want attached to it.\n\n"
1917 "The key will appear next to the menu item and you can just press "
1918 "that key without opening the menu in future."));
1921 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1923 GtkWidget *button, *align;
1925 g_return_val_if_fail(option == NULL, NULL);
1927 align = gtk_alignment_new(0.5, 0.5, 0, 0);
1928 button = button_new_mixed(GTK_STOCK_DIALOG_INFO,
1929 _("Set keyboard shortcuts"));
1930 gtk_container_add(GTK_CONTAINER(align), button);
1931 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1933 return g_list_append(NULL, align);