r1659: New menu entry to load the corrent manual for the current locale (suggested by
[rox-filer.git] / ROX-Filer / src / menu.c
blob509b9d1085d71f25748cf112ba6199d4e61a9830
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},
250 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
254 #define GET_MENU_ITEM(var, menu) \
255 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
257 #define GET_SMENU_ITEM(var, menu, sub) \
258 do { \
259 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
260 var = gtk_item_factory_get_widget(item_factory, tmp); \
261 g_free(tmp); \
262 } while (0)
264 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
265 do { \
266 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
267 var = gtk_item_factory_get_widget(item_factory, tmp); \
268 g_free(tmp); \
269 } while (0)
271 void ensure_filer_menu(void)
273 GList *items;
274 guchar *tmp;
275 GtkWidget *item;
276 GtkItemFactory *item_factory;
278 if (filer_keys)
279 return;
281 filer_keys = gtk_accel_group_new();
282 item_factory = menu_create(filer_menu_def,
283 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
284 "<filer>", filer_keys);
286 GET_MENU_ITEM(filer_menu, "filer");
287 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
288 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
289 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
290 "Show Thumbnails");
292 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
293 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
294 filer_follow_sym = GTK_BIN(item)->child;
296 /* File '' label... */
297 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
298 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
299 g_list_free(items);
301 /* Shift Open... label */
302 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
303 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
304 g_list_free(items);
306 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
307 filer_new_window = GTK_BIN(item)->child;
309 g_signal_connect(filer_menu, "unmap_event",
310 G_CALLBACK(menu_closed), NULL);
311 g_signal_connect(filer_file_menu, "unmap_event",
312 G_CALLBACK(menu_closed), NULL);
314 g_signal_connect(filer_keys, "accel_changed",
315 G_CALLBACK(save_menus), NULL);
318 void menu_init(void)
320 char *menurc;
322 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
323 if (menurc)
325 gtk_accel_map_load(menurc);
326 g_free(menurc);
329 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
330 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
331 option_add_saver(save_menus);
333 option_register_widget("menu-set-keys", set_keys_button);
336 /* Name is in the form "<panel>" */
337 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
338 const gchar *name, GtkAccelGroup *keys)
340 GtkItemFactory *item_factory;
341 GtkItemFactoryEntry *translated;
343 if (!keys)
345 keys = gtk_accel_group_new();
346 gtk_accel_group_lock(keys);
349 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
351 translated = translate_entries(def, n_entries);
352 gtk_item_factory_create_items(item_factory, n_entries,
353 translated, NULL);
354 free_translated_entries(translated, n_entries);
356 return item_factory;
359 /* Prevent the user from setting a short-cut on this item */
360 static void menuitem_no_shortcuts(GtkWidget *item)
362 /* XXX */
363 #if 0
364 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
366 _gtk_widget_set_accel_path(item, NULL, NULL);
367 g_free(menuitem->accel_path);
368 menuitem->accel_path = NULL;
369 #endif
372 static void shade_file_menu_items(gboolean shaded)
374 menu_set_items_shaded(filer_file_menu, shaded, 0, 5);
375 menu_set_items_shaded(filer_file_menu, shaded, 6, 3);
378 /* 'data' is an array of three ints:
379 * [ pointer_x, pointer_y, item_under_pointer ]
381 void position_menu(GtkMenu *menu, gint *x, gint *y,
382 gboolean *push_in, gpointer data)
384 int *pos = (int *) data;
385 GtkRequisition requisition;
386 GList *items, *next;
387 int y_shift = 0;
388 int item = pos[2];
390 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
392 while (item >= 0 && next)
394 int h = ((GtkWidget *) next->data)->requisition.height;
396 if (item > 0)
397 y_shift += h;
398 else
399 y_shift += h / 2;
401 next = next->next;
402 item--;
405 g_list_free(items);
407 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
409 *x = pos[0] - (requisition.width * 7 / 8);
410 *y = pos[1] - y_shift;
412 *x = CLAMP(*x, 0, screen_width - requisition.width);
413 *y = CLAMP(*y, 0, screen_height - requisition.height);
415 *push_in = FALSE;
418 #if 0
419 /* Used when you menu-click on the Large or Small toolbar tools */
420 void show_style_menu(FilerWindow *filer_window,
421 GdkEventButton *event,
422 GtkWidget *menu)
424 int pos[3];
426 pos[0] = event->x_root;
427 pos[1] = event->y_root;
428 pos[2] = 0;
430 window_with_focus = filer_window;
432 popup_menu = menu;
434 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
435 (gpointer) pos, event->button, event->time);
437 #endif
439 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
440 MenuIconStyle style, CallbackFn func,
441 gboolean separator, gboolean strip_ext,
442 gboolean recurse)
444 GList *widgets = NULL;
445 DirItem *ditem;
446 DIR *dir;
447 struct dirent *ent;
448 GtkWidget *item;
449 char *dname = NULL;
451 dname = pathdup(dir_name);
453 dir = opendir(dname);
454 if (!dir)
455 goto out;
457 if (separator)
459 item = gtk_menu_item_new();
460 widgets = g_list_append(widgets, item);
461 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
464 while ((ent = readdir(dir)))
466 char *dot, *leaf;
467 GtkWidget *hbox;
468 GtkWidget *img;
469 GtkWidget *label;
470 gchar *fname;
472 /* Ignore hidden files */
473 if (ent->d_name[0] == '.')
474 continue;
476 /* Strip off extension, if any */
477 dot = strchr(ent->d_name, '.');
478 if (strip_ext && dot)
479 leaf = g_strndup(ent->d_name, dot - ent->d_name);
480 else
481 leaf = g_strdup(ent->d_name);
483 fname = g_strconcat(dname, "/", ent->d_name, NULL);
484 ditem = diritem_new("");
485 diritem_restat(fname, ditem, NULL);
487 if (ditem->image && style != MIS_NONE)
489 GdkPixbuf *pixbuf;
491 switch (style) {
492 case MIS_LARGE:
493 pixbuf = ditem->image->pixbuf;
494 break;
495 default:
496 if (!ditem->image->sm_pixbuf)
497 pixmap_make_small(ditem->image);
498 pixbuf = ditem->image->sm_pixbuf;
499 break;
502 item = gtk_menu_item_new();
503 /* TODO: Find a way to allow short-cuts */
504 menuitem_no_shortcuts(item);
506 hbox = gtk_hbox_new(FALSE, 2);
507 gtk_container_add(GTK_CONTAINER(item), hbox);
509 img = gtk_image_new_from_pixbuf(pixbuf);
511 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
513 label = gtk_label_new(leaf);
514 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
515 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
518 else
519 item = gtk_menu_item_new_with_label(leaf);
521 /* If it is a directory (but NOT an AppDir) and we are
522 * recursing then set up a sub menu.
524 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
525 !(ditem->flags & ITEM_FLAG_APPDIR))
527 GtkWidget *sub;
528 GList *new_widgets;
530 sub = gtk_menu_new();
531 new_widgets = menu_from_dir(sub, fname, style, func,
532 separator, strip_ext, FALSE);
533 g_list_free(new_widgets);
534 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
536 else
537 g_signal_connect_swapped(item, "activate",
538 G_CALLBACK(func), fname);
540 diritem_free(ditem);
541 g_free(leaf);
543 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
544 g_signal_connect_swapped(item, "destroy",
545 G_CALLBACK(g_free), fname);
547 widgets = g_list_append(widgets, item);
550 closedir(dir);
551 out:
552 g_free(dname);
554 return widgets;
557 /* Scan the templates dir and create entries for the New menu */
558 static void update_new_files_menu(MenuIconStyle style)
560 static GList *widgets = NULL;
562 gchar *templ_dname = NULL;
564 if (widgets)
566 GList *next;
568 for (next = widgets; next; next = next->next)
569 gtk_widget_destroy((GtkWidget *) next->data);
571 g_list_free(widgets);
572 widgets = NULL;
575 templ_dname = choices_find_path_load("Templates", "");
576 if (templ_dname)
578 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
579 (CallbackFn) new_file_type, TRUE, TRUE,
580 FALSE);
581 g_free(templ_dname);
583 gtk_widget_show_all(filer_new_menu);
586 /* 'item' is the number of the item to appear under the pointer. */
587 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
589 int pos[3];
590 int button = 0;
591 guint32 time = 0;
593 if (event && (event->type == GDK_BUTTON_PRESS ||
594 event->type == GDK_BUTTON_RELEASE))
596 GdkEventButton *bev = (GdkEventButton *) event;
598 pos[0] = bev->x_root;
599 pos[1] = bev->y_root;
600 button = bev->button;
601 time = bev->time;
603 else if (event && event->type == GDK_KEY_PRESS)
605 GdkEventKey *kev = (GdkEventKey *) event;
607 get_pointer_xy(pos, pos + 1);
608 time = kev->time;
610 else
611 get_pointer_xy(pos, pos + 1);
613 pos[2] = item;
615 gtk_widget_show_all(menu);
616 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
617 position_menu, (gpointer) pos, button, time);
618 select_nth_item(GTK_MENU_SHELL(menu), item);
621 /* Hide the popup menu, if any */
622 void menu_popdown(void)
624 if (popup_menu)
625 gtk_menu_popdown(GTK_MENU(popup_menu));
628 static MenuIconStyle get_menu_icon_style(void)
630 MenuIconStyle mis;
631 int display;
633 mis = o_menu_iconsize.int_value;
635 switch (mis)
637 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
638 return mis;
639 default:
640 break;
643 if (mis == MIS_CURRENT && window_with_focus)
645 switch (window_with_focus->display_style)
647 case HUGE_ICONS:
648 case LARGE_ICONS:
649 return MIS_LARGE;
650 case SMALL_ICONS:
651 return MIS_SMALL;
652 default:
653 break;
657 display = o_display_size.int_value;
658 switch (display)
660 case HUGE_ICONS:
661 case LARGE_ICONS:
662 return MIS_LARGE;
663 case SMALL_ICONS:
664 return MIS_SMALL;
665 default:
666 break;
669 return MIS_SMALL;
672 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
674 DirItem *file_item = NULL;
675 GdkModifierType state = 0;
676 int n_selected;
678 n_selected = view_count_selected(filer_window->view);
680 ensure_filer_menu();
682 updating_menu++;
684 /* Remove previous AppMenu, if any */
685 appmenu_remove();
687 window_with_focus = filer_window;
689 if (event->type == GDK_BUTTON_PRESS)
690 state = ((GdkEventButton *) event)->state;
691 else if (event->type == GDK_KEY_PRESS)
692 state = ((GdkEventKey *) event)->state;
694 if (n_selected == 0 && item >= 0)
696 filer_window->temp_item_selected = TRUE;
697 collection_select_item(filer_window->collection, item);
698 n_selected = view_count_selected(filer_window->view);
700 else
702 filer_window->temp_item_selected = FALSE;
705 /* Short-cut to the Send To menu */
706 if (state & GDK_SHIFT_MASK)
708 GList *paths;
710 updating_menu--;
712 if (n_selected == 0)
714 report_error(
715 _("You should Shift+Menu click over a file to "
716 "send it somewhere"));
717 return;
720 paths = filer_selected_items(filer_window);
722 show_send_to_menu(paths, event); /* (paths eaten) */
724 return;
728 GtkWidget *file_label, *file_menu;
729 GString *buffer;
730 DirItem *item;
732 file_label = filer_file_item;
733 file_menu = filer_file_menu;
734 gtk_check_menu_item_set_active(
735 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
736 filer_window->show_thumbs);
737 gtk_check_menu_item_set_active(
738 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
739 filer_window->show_hidden);
740 buffer = g_string_new(NULL);
742 switch (n_selected)
744 case 0:
745 g_string_assign(buffer, _("Next Click"));
746 shade_file_menu_items(FALSE);
747 break;
748 case 1:
749 item = filer_selected_item(filer_window);
750 if (!item->image)
751 dir_update_item(filer_window->directory,
752 item->leafname);
753 shade_file_menu_items(FALSE);
754 file_item = filer_selected_item(filer_window);
755 g_string_sprintf(buffer, "%s '%s'",
756 basetype_name(file_item),
757 file_item->leafname);
758 if (!can_set_run_action(file_item))
759 menu_set_items_shaded(filer_file_menu,
760 TRUE, 6, 1);
761 break;
762 default:
763 shade_file_menu_items(TRUE);
764 g_string_sprintf(buffer, _("%d items"),
765 n_selected);
766 break;
768 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
769 g_string_free(buffer, TRUE);
771 menu_show_shift_action(file_shift_item, file_item,
772 n_selected == 0);
773 if (file_item)
774 appmenu_add(make_path(filer_window->sym_path,
775 file_item->leafname)->str,
776 file_item, filer_file_menu);
779 update_new_files_menu(get_menu_icon_style());
781 gtk_widget_set_sensitive(filer_new_window,
782 !o_unique_filer_windows.int_value);
783 gtk_widget_set_sensitive(filer_follow_sym,
784 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
786 popup_menu = (state & GDK_CONTROL_MASK)
787 ? filer_file_menu
788 : filer_menu;
790 updating_menu--;
792 show_popup_menu(popup_menu, event,
793 popup_menu == filer_file_menu ? 5 : 1);
796 static void menu_closed(GtkWidget *widget)
798 if (window_with_focus == NULL || widget != popup_menu)
799 return; /* Close panel item chosen? */
801 popup_menu = NULL;
803 if (window_with_focus->temp_item_selected)
805 view_clear_selection(window_with_focus->view);
806 window_with_focus->temp_item_selected = FALSE;
810 static void target_callback(FilerWindow *filer_window,
811 gint item,
812 gpointer action)
814 Collection *collection;
816 g_return_if_fail(filer_window != NULL);
818 collection = filer_window->collection;
820 window_with_focus = filer_window;
822 /* Don't grab the primary selection */
823 filer_window->temp_item_selected = TRUE;
825 collection_wink_item(collection, item);
826 collection_clear_except(collection, item);
827 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
829 if (item < collection->number_of_items)
830 collection_unselect_item(collection, item);
831 filer_window->temp_item_selected = FALSE;
834 /* Set the text of the 'Shift Open...' menu item.
835 * If icon is NULL, reset the text and also shade it, unless 'next'.
837 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
839 guchar *shift_action = NULL;
841 if (item)
843 if (item->flags & ITEM_FLAG_MOUNT_POINT)
845 if (item->flags & ITEM_FLAG_MOUNTED)
846 shift_action = N_("Unmount");
847 else
848 shift_action = N_("Mount");
850 else if (item->flags & ITEM_FLAG_SYMLINK)
851 shift_action = N_("Show Target");
852 else if (item->base_type == TYPE_DIRECTORY)
853 shift_action = N_("Look Inside");
854 else if (item->base_type == TYPE_FILE)
855 shift_action = N_("Open As Text");
857 gtk_label_set_text(GTK_LABEL(menu_item),
858 shift_action ? _(shift_action)
859 : _("Shift Open"));
860 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
863 /* Actions */
865 static void huge_with(gpointer data, guint action, GtkWidget *widget)
867 display_set_layout(window_with_focus, HUGE_ICONS, action);
870 static void large_with(gpointer data, guint action, GtkWidget *widget)
872 display_set_layout(window_with_focus, LARGE_ICONS, action);
875 static void small_with(gpointer data, guint action, GtkWidget *widget)
877 display_set_layout(window_with_focus, SMALL_ICONS, action);
880 static void sort_name(gpointer data, guint action, GtkWidget *widget)
882 g_return_if_fail(window_with_focus != NULL);
884 display_set_sort_fn(window_with_focus, sort_by_name);
887 static void sort_type(gpointer data, guint action, GtkWidget *widget)
889 g_return_if_fail(window_with_focus != NULL);
891 display_set_sort_fn(window_with_focus, sort_by_type);
894 static void sort_date(gpointer data, guint action, GtkWidget *widget)
896 g_return_if_fail(window_with_focus != NULL);
898 display_set_sort_fn(window_with_focus, sort_by_date);
901 static void sort_size(gpointer data, guint action, GtkWidget *widget)
903 g_return_if_fail(window_with_focus != NULL);
905 display_set_sort_fn(window_with_focus, sort_by_size);
908 static void hidden(gpointer data, guint action, GtkWidget *widget)
910 if (updating_menu)
911 return;
913 g_return_if_fail(window_with_focus != NULL);
915 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
918 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
920 if (updating_menu)
921 return;
923 g_return_if_fail(window_with_focus != NULL);
925 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
928 static void refresh(gpointer data, guint action, GtkWidget *widget)
930 g_return_if_fail(window_with_focus != NULL);
932 full_refresh();
933 filer_update_dir(window_with_focus, TRUE);
936 static void delete(FilerWindow *filer_window)
938 GList *paths;
939 paths = filer_selected_items(filer_window);
940 action_delete(paths);
941 g_list_foreach(paths, (GFunc) g_free, NULL);
942 g_list_free(paths);
945 static void usage(FilerWindow *filer_window)
947 GList *paths;
948 paths = filer_selected_items(filer_window);
949 action_usage(paths);
950 g_list_foreach(paths, (GFunc) g_free, NULL);
951 g_list_free(paths);
954 static void chmod_items(FilerWindow *filer_window)
956 GList *paths;
957 paths = filer_selected_items(filer_window);
958 action_chmod(paths, FALSE, NULL);
959 g_list_foreach(paths, (GFunc) g_free, NULL);
960 g_list_free(paths);
963 static void find(FilerWindow *filer_window)
965 GList *paths;
966 paths = filer_selected_items(filer_window);
967 action_find(paths);
968 g_list_foreach(paths, (GFunc) g_free, NULL);
969 g_list_free(paths);
972 /* This creates a new savebox widget, and allows the user to pick a new path
973 * for the file.
974 * Once the new path has been picked, the callback will be called with
975 * both the current and new paths.
976 * NOTE: This function unrefs 'image'!
978 static void savebox_show(const gchar *action, const gchar *path,
979 MaskedPixmap *image, SaveCb callback)
981 GtkWidget *savebox = NULL;
982 GtkWidget *check_relative = NULL;
984 g_return_if_fail(image != NULL);
986 savebox = gtk_savebox_new(action);
988 if (callback == link_cb)
990 check_relative = gtk_check_button_new_with_mnemonic(
991 _("_Relative link"));
992 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
993 TRUE);
995 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
996 gtk_tooltips_set_tip(tooltips, check_relative,
997 _("If on, the symlink will store the path from the "
998 "symlink to the target file. Use this if the symlink "
999 "and the target will be moved together.\n"
1000 "If off, the path from the root directory is stored - "
1001 "use this if the symlink may move but the target will "
1002 "stay put."), NULL);
1003 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1004 check_relative, FALSE, TRUE, 0);
1005 gtk_widget_show(check_relative);
1008 g_signal_connect(savebox, "save_to_file",
1009 G_CALLBACK(save_to_file), NULL);
1011 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1012 g_strdup(path), g_free);
1013 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1014 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1016 gtk_window_set_title(GTK_WINDOW(savebox), action);
1018 if (g_utf8_validate(path, -1, NULL))
1019 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1020 else
1022 gchar *u8;
1023 u8 = to_utf8(path);
1024 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1025 g_free(u8);
1027 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1028 g_object_unref(image);
1030 gtk_widget_show(savebox);
1033 static gint save_to_file(GObject *savebox,
1034 const gchar *pathname, gpointer data)
1036 SaveCb callback;
1037 const gchar *current_path;
1039 callback = g_object_get_data(savebox, "action_callback");
1040 current_path = g_object_get_data(savebox, "current_path");
1042 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1043 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1045 return callback(savebox, current_path, pathname)
1046 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1049 static gboolean copy_cb(GObject *savebox,
1050 const gchar *current, const gchar *new)
1052 return action_with_leaf(action_copy, current, new);
1055 static gboolean action_with_leaf(ActionFn action,
1056 const gchar *current, const gchar *new)
1058 const char *leaf;
1059 char *new_dir;
1060 GList *local_paths;
1062 if (new[0] != '/')
1064 report_error(_("New pathname is not absolute"));
1065 return FALSE;
1068 if (new[strlen(new) - 1] == '/')
1070 new_dir = g_strdup(new);
1071 leaf = NULL;
1073 else
1075 const gchar *slash;
1077 slash = strrchr(new, '/');
1078 new_dir = g_strndup(new, slash - new);
1079 leaf = slash + 1;
1082 local_paths = g_list_append(NULL, (gchar *) current);
1083 action(local_paths, new_dir, leaf, -1);
1084 g_list_free(local_paths);
1086 g_free(new_dir);
1088 return TRUE;
1091 /* Open a savebox to act on the selected file.
1092 * Call 'callback' later to perform the operation.
1094 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1095 const gchar *action, SaveCb callback)
1097 g_object_ref(image);
1098 savebox_show(action, path, image, callback);
1101 static gboolean rename_cb(GObject *savebox,
1102 const gchar *current, const gchar *new)
1104 return action_with_leaf(action_move, current, new);
1107 static gboolean link_cb(GObject *savebox,
1108 const gchar *initial, const gchar *path)
1110 GtkToggleButton *check_relative;
1111 struct stat info;
1112 int err;
1113 gchar *link_path;
1115 check_relative = g_object_get_data(savebox, "check_relative");
1117 if (gtk_toggle_button_get_active(check_relative))
1118 link_path = get_relative_path(path, initial);
1119 else
1120 link_path = g_strdup(initial);
1122 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1124 GtkWidget *box, *button;
1125 gint ans;
1127 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1128 GTK_BUTTONS_CANCEL,
1129 _("Symlink from '%s' already exists. "
1130 "Replace it with a link to '%s'?"),
1131 path, link_path);
1133 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1135 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1136 gtk_widget_show(button);
1137 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1138 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1139 button, GTK_RESPONSE_OK);
1140 gtk_dialog_set_default_response(GTK_DIALOG(box),
1141 GTK_RESPONSE_OK);
1143 ans = gtk_dialog_run(GTK_DIALOG(box));
1144 gtk_widget_destroy(box);
1146 if (ans != GTK_RESPONSE_OK)
1148 g_free(link_path);
1149 return FALSE;
1152 unlink(path);
1155 err = symlink(link_path, path);
1156 g_free(link_path);
1158 if (err)
1160 report_error("symlink: %s", g_strerror(errno));
1161 return FALSE;
1164 dir_check_this(path);
1166 return TRUE;
1169 static void run_action(DirItem *item)
1171 if (can_set_run_action(item))
1172 type_set_handler_dialog(item->mime_type);
1173 else
1174 report_error(
1175 _("You can only set the run action for a "
1176 "regular file"));
1179 void open_home(gpointer data, guint action, GtkWidget *widget)
1181 filer_opendir(home_dir, NULL);
1184 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1186 gchar *path;
1188 path = g_strconcat(filer_window->sym_path,
1189 "/", item->leafname, "#", NULL);
1191 filer_change_to(filer_window, path, NULL);
1192 g_free(path);
1195 static void select_all(gpointer data, guint action, GtkWidget *widget)
1197 g_return_if_fail(window_with_focus != NULL);
1199 window_with_focus->temp_item_selected = FALSE;
1200 collection_select_all(window_with_focus->collection);
1203 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1205 g_return_if_fail(window_with_focus != NULL);
1207 window_with_focus->temp_item_selected = FALSE;
1208 view_clear_selection(window_with_focus->view);
1211 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1213 g_return_if_fail(window_with_focus != NULL);
1215 window_with_focus->temp_item_selected = FALSE;
1216 collection_invert_selection(window_with_focus->collection);
1219 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1221 GtkWidget *win;
1223 win = options_show();
1225 if (win)
1227 number_of_windows++;
1228 g_signal_connect(win, "destroy",
1229 G_CALLBACK(one_less_window), NULL);
1233 static gboolean new_directory_cb(GObject *savebox,
1234 const gchar *initial, const gchar *path)
1236 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1238 report_error("mkdir: %s", g_strerror(errno));
1239 return FALSE;
1242 dir_check_this(path);
1244 if (filer_exists(window_with_focus))
1246 guchar *leaf;
1247 leaf = strrchr(path, '/');
1248 if (leaf)
1249 display_set_autoselect(window_with_focus, leaf + 1);
1252 return TRUE;
1255 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1257 g_return_if_fail(window_with_focus != NULL);
1259 savebox_show(_("Create"),
1260 make_path(window_with_focus->sym_path, _("NewDir"))->str,
1261 type_to_icon(inode_directory), new_directory_cb);
1264 static gboolean new_file_cb(GObject *savebox,
1265 const gchar *initial, const gchar *path)
1267 int fd;
1269 fd = open(path, O_CREAT | O_EXCL, 0666);
1271 if (fd == -1)
1273 report_error(_("Error creating '%s': %s"),
1274 path, g_strerror(errno));
1275 return FALSE;
1278 if (close(fd))
1279 report_error(_("Error creating '%s': %s"),
1280 path, g_strerror(errno));
1282 dir_check_this(path);
1284 if (filer_exists(window_with_focus))
1286 guchar *leaf;
1287 leaf = strrchr(path, '/');
1288 if (leaf)
1289 display_set_autoselect(window_with_focus, leaf + 1);
1292 return TRUE;
1295 static void new_file(gpointer data, guint action, GtkWidget *widget)
1297 g_return_if_fail(window_with_focus != NULL);
1299 savebox_show(_("Create"),
1300 make_path(window_with_focus->sym_path, _("NewFile"))->str,
1301 type_to_icon(text_plain),
1302 new_file_cb);
1305 static gboolean new_file_type_cb(GObject *savebox,
1306 const gchar *initial, const gchar *path)
1308 const gchar *oleaf, *leaf;
1309 gchar *templ, *templ_dname, *dest;
1310 GList *paths;
1312 /* We can work out the template path from the initial name */
1313 oleaf = g_basename(initial);
1314 templ_dname = choices_find_path_load("Templates", "");
1315 if (!templ_dname)
1317 report_error(
1318 _("Error creating file: could not find the template for %s"),
1319 oleaf);
1320 return FALSE;
1323 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1324 g_free(templ_dname);
1326 dest = g_dirname(path);
1327 leaf = g_basename(path);
1328 paths = g_list_append(NULL, templ);
1330 action_copy(paths, dest, leaf, TRUE);
1332 g_list_free(paths);
1333 g_free(dest);
1334 g_free(templ);
1336 if (filer_exists(window_with_focus))
1337 display_set_autoselect(window_with_focus, leaf);
1339 return TRUE;
1342 static void do_send_to(gchar *templ)
1344 g_return_if_fail(send_to_paths != NULL);
1346 run_with_files(templ, send_to_paths);
1349 static void new_file_type(gchar *templ)
1351 const gchar *leaf;
1352 MIME_type *type;
1354 g_return_if_fail(window_with_focus != NULL);
1356 leaf = g_basename(templ);
1357 type = type_get_type(templ);
1359 savebox_show(_("Create"),
1360 make_path(window_with_focus->sym_path, leaf)->str,
1361 type_to_icon(type),
1362 new_file_type_cb);
1365 static void customise_send_to(gpointer data)
1367 GPtrArray *path;
1368 guchar *save;
1369 GString *dirs;
1370 int i;
1372 dirs = g_string_new(NULL);
1374 path = choices_list_dirs("");
1375 for (i = 0; i < path->len; i++)
1377 guchar *old = (guchar *) path->pdata[i];
1379 g_string_append(dirs, old);
1380 g_string_append(dirs, "SendTo\n");
1382 choices_free_list(path);
1384 save = choices_find_path_save("", "SendTo", TRUE);
1385 if (save)
1386 mkdir(save, 0777);
1388 info_message(
1389 _("The `Send To' menu provides a quick way to send some files "
1390 "to an application. The applications listed are those in "
1391 "the following directories:\n\n%s\n%s\n"
1392 "The `Send To' menu may be opened by Shift+Menu clicking "
1393 "over a file.\n\n"
1394 "Advanced use:\n"
1395 "You can also create subdirectories called "
1396 "`.text_html', `.text', etc which will only be "
1397 "shown for files of that type. `.group' is shown "
1398 "only when multiple files are selected."),
1399 dirs->str,
1400 save ? _("I'll show you your SendTo directory now; you should "
1401 "symlink (Ctrl+Shift drag) any applications you want "
1402 "into it.")
1403 : _("Your CHOICESPATH variable setting prevents "
1404 "customisations - sorry."));
1406 g_string_free(dirs, TRUE);
1408 if (save)
1409 filer_opendir(save, NULL);
1412 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1413 * to the menu.
1415 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1417 gchar *searchdir;
1418 GPtrArray *paths;
1419 int i;
1421 if (subtype)
1422 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1423 else if (type)
1424 searchdir = g_strdup_printf("SendTo/.%s", type);
1425 else
1426 searchdir = g_strdup("SendTo");
1428 paths = choices_list_dirs(searchdir);
1429 g_free(searchdir);
1431 for (i = 0; i < paths->len; i++)
1433 GList *widgets = NULL;
1434 guchar *dir = (guchar *) paths->pdata[i];
1436 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1437 (CallbackFn) do_send_to,
1438 FALSE, FALSE, TRUE);
1440 if (widgets)
1441 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1442 gtk_menu_item_new());
1444 g_list_free(widgets); /* TODO: Get rid of this */
1447 choices_free_list(paths);
1450 /* Scan the SendTo dir and create and show the Send To menu.
1451 * The 'paths' list and every path in it is claimed, and will be
1452 * freed later -- don't free it yourself!
1454 static void show_send_to_menu(GList *paths, GdkEvent *event)
1456 GtkWidget *menu, *item;
1458 menu = gtk_menu_new();
1460 if (g_list_length(paths) == 1)
1462 DirItem *item;
1464 item = diritem_new("");
1465 diritem_restat(paths->data, item, NULL);
1467 add_sendto(menu,
1468 item->mime_type->media_type,
1469 item->mime_type->subtype);
1471 add_sendto(menu, item->mime_type->media_type, NULL);
1473 diritem_free(item);
1475 else
1476 add_sendto(menu, "group", NULL);
1478 add_sendto(menu, NULL, NULL);
1480 item = gtk_menu_item_new_with_label(_("Customise"));
1481 g_signal_connect_swapped(item, "activate",
1482 G_CALLBACK(customise_send_to), NULL);
1483 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1485 if (send_to_paths)
1487 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1488 g_list_free(send_to_paths);
1490 send_to_paths = paths;
1492 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1494 popup_menu = menu;
1495 show_popup_menu(menu, event, 0);
1498 static void send_to(FilerWindow *filer_window)
1500 GList *paths;
1501 GdkEvent *event;
1503 paths = filer_selected_items(filer_window);
1504 event = gtk_get_current_event();
1506 /* Eats paths */
1507 show_send_to_menu(paths, event);
1509 gdk_event_free(event);
1512 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1514 const char *argv[] = {"sh", "-c", NULL, NULL};
1515 gboolean close = action;
1517 argv[2] = o_menu_xterm.value;
1519 g_return_if_fail(window_with_focus != NULL);
1521 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1522 gtk_widget_destroy(window_with_focus->window);
1525 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1527 g_return_if_fail(window_with_focus != NULL);
1529 filer_change_to(window_with_focus, home_dir, NULL);
1532 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1534 g_return_if_fail(window_with_focus != NULL);
1536 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1537 filer_change_to(window_with_focus,
1538 window_with_focus->real_path, NULL);
1539 else
1540 delayed_error(_("This is already the canonical name "
1541 "for this directory."));
1544 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1546 g_return_if_fail(window_with_focus != NULL);
1548 filer_open_parent(window_with_focus);
1551 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1553 g_return_if_fail(window_with_focus != NULL);
1555 change_to_parent(window_with_focus);
1558 static void resize(gpointer data, guint action, GtkWidget *widget)
1560 g_return_if_fail(window_with_focus != NULL);
1562 filer_window_autosize(window_with_focus, TRUE);
1565 static void new_window(gpointer data, guint action, GtkWidget *widget)
1567 g_return_if_fail(window_with_focus != NULL);
1569 if (o_unique_filer_windows.int_value)
1571 report_error(_("You can't open a second view onto "
1572 "this directory because the `Unique Windows' option "
1573 "is turned on in the Options window."));
1575 else
1576 filer_opendir(window_with_focus->sym_path, window_with_focus);
1579 #if 0
1580 static void su_to_user(GtkWidget *dialog)
1582 char *argv[] = {
1583 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1584 GtkEntry *user;
1585 guchar *path;
1587 g_return_if_fail(dialog != NULL);
1589 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1590 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1592 g_return_if_fail(user != NULL && path != NULL);
1594 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1595 argv[3] = gtk_entry_get_text(user);
1596 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1597 argv[5] = path;
1599 if (!spawn(argv))
1600 report_error(_("fork: %s"), g_strerror(errno));
1602 g_free(argv[2]);
1603 g_free(argv[4]);
1605 gtk_widget_destroy(dialog);
1608 static void new_user(gpointer data, guint action, GtkWidget *widget)
1610 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1612 g_return_if_fail(window_with_focus != NULL);
1614 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1615 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1616 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1617 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1618 g_strdup(window_with_focus->path), g_free);
1620 vbox = gtk_vbox_new(FALSE, 4);
1621 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1622 gtk_box_pack_start(GTK_BOX(vbox),
1623 gtk_label_new(_("Browse as which user?")),
1624 TRUE, TRUE, 2);
1626 hbox = gtk_hbox_new(FALSE, 4);
1627 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1629 gtk_box_pack_start(GTK_BOX(hbox),
1630 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1632 entry = gtk_entry_new();
1633 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1634 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1635 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1636 gtk_widget_grab_focus(entry);
1637 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1638 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1639 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1641 hbox = gtk_hbox_new(TRUE, 0);
1642 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1644 button = gtk_button_new_with_label(_("OK"));
1645 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1646 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1647 gtk_window_set_default(GTK_WINDOW(dialog), button);
1648 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1649 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1651 button = gtk_button_new_with_label(_("Cancel"));
1652 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1653 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1654 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1655 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1656 GTK_OBJECT(dialog));
1658 gtk_widget_show_all(dialog);
1660 #endif
1662 static void close_window(gpointer data, guint action, GtkWidget *widget)
1664 g_return_if_fail(window_with_focus != NULL);
1666 gtk_widget_destroy(window_with_focus->window);
1669 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1671 MiniType type = (MiniType) action;
1673 g_return_if_fail(window_with_focus != NULL);
1675 /* Item needs to remain selected... */
1676 if (type == MINI_SHELL)
1677 window_with_focus->temp_item_selected = FALSE;
1679 minibuffer_show(window_with_focus, type);
1682 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1684 if (action == HELP_ABOUT)
1685 infobox_new(app_dir);
1686 else if (action == HELP_DIR)
1687 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1688 else if (action == HELP_MANUAL)
1690 gchar *manual;
1692 manual = g_strconcat(app_dir, "/Help/Manual-",
1693 current_lang, ".html", NULL);
1695 if (access(manual, F_OK))
1697 g_free(manual);
1698 manual = g_strconcat(app_dir,
1699 "/Help/Manual.html", NULL);
1702 run_by_path(manual);
1704 g_free(manual);
1706 else
1707 g_warning("Unknown help action %d\n", action);
1710 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1711 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1713 GList *items, *item;
1715 items = gtk_container_get_children(GTK_CONTAINER(menu));
1717 item = g_list_nth(items, from);
1718 while (item && n--)
1720 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1721 item = item->next;
1723 g_list_free(items);
1726 static void save_menus(void)
1728 char *menurc;
1730 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1731 if (menurc)
1733 gtk_accel_map_save(menurc);
1734 g_free(menurc);
1738 static void select_nth_item(GtkMenuShell *shell, int n)
1740 GList *items;
1741 GtkWidget *item;
1743 items = gtk_container_get_children(GTK_CONTAINER(shell));
1744 item = g_list_nth_data(items, n);
1746 g_return_if_fail(item != NULL);
1748 g_list_free(items);
1750 gtk_menu_shell_select_item(shell, item);
1753 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1755 DirItem *item;
1756 gchar *path;
1757 int n_selected;
1759 g_return_if_fail(window_with_focus != NULL);
1761 n_selected = view_count_selected(window_with_focus->view);
1763 if (n_selected < 1)
1765 const char *prompt;
1767 switch (action)
1769 case FILE_COPY_ITEM:
1770 prompt = _("Copy ... ?");
1771 break;
1772 case FILE_RENAME_ITEM:
1773 prompt = _("Rename ... ?");
1774 break;
1775 case FILE_LINK_ITEM:
1776 prompt = _("Symlink ... ?");
1777 break;
1778 case FILE_OPEN_FILE:
1779 prompt = _("Shift Open ... ?");
1780 break;
1781 case FILE_HELP:
1782 prompt = _("Help about ... ?");
1783 break;
1784 case FILE_SHOW_FILE_INFO:
1785 prompt = _("Examine ... ?");
1786 break;
1787 case FILE_RUN_ACTION:
1788 prompt = _("Set run action for ... ?");
1789 break;
1790 case FILE_SET_ICON:
1791 prompt = _("Set icon for ... ?");
1792 break;
1793 case FILE_SEND_TO:
1794 prompt = _("Send ... to ... ?");
1795 break;
1796 case FILE_DELETE:
1797 prompt = _("DELETE ... ?");
1798 break;
1799 case FILE_USAGE:
1800 prompt = _("Count the size of ... ?");
1801 break;
1802 case FILE_CHMOD_ITEMS:
1803 prompt = _("Set permissions on ... ?");
1804 break;
1805 case FILE_FIND:
1806 prompt = _("Search inside ... ?");
1807 break;
1808 case FILE_OPEN_VFS_AVFS:
1809 prompt = _("Look inside ... ?");
1810 break;
1811 default:
1812 g_warning("Unknown action!");
1813 return;
1815 filer_target_mode(window_with_focus, target_callback,
1816 GINT_TO_POINTER(action), prompt);
1817 return;
1820 switch (action)
1822 case FILE_SEND_TO:
1823 send_to(window_with_focus);
1824 return;
1825 case FILE_DELETE:
1826 delete(window_with_focus);
1827 return;
1828 case FILE_USAGE:
1829 usage(window_with_focus);
1830 return;
1831 case FILE_CHMOD_ITEMS:
1832 chmod_items(window_with_focus);
1833 return;
1834 case FILE_FIND:
1835 find(window_with_focus);
1836 return;
1837 case FILE_SHOW_FILE_INFO:
1839 GList *items;
1841 items = filer_selected_items(window_with_focus);
1842 infobox_show_list(items);
1843 g_list_foreach(items, (GFunc) g_free, NULL);
1844 g_list_free(items);
1845 return;
1847 default:
1848 break;
1851 /* All the following actions require exactly one file selected */
1853 if (n_selected > 1)
1855 report_error(_("You cannot do this to more than "
1856 "one item at a time"));
1857 return;
1860 item = filer_selected_item(window_with_focus);
1861 g_return_if_fail(item != NULL);
1862 if (!item->image)
1863 item = dir_update_item(window_with_focus->directory,
1864 item->leafname);
1866 if (!item)
1868 report_error(_("Item no longer exists!"));
1869 return;
1872 path = make_path(window_with_focus->sym_path, item->leafname)->str;
1874 switch (action)
1876 case FILE_COPY_ITEM:
1877 src_dest_action_item(path, item->image,
1878 _("Copy"), copy_cb);
1879 break;
1880 case FILE_RENAME_ITEM:
1881 src_dest_action_item(path, item->image,
1882 _("Rename"), rename_cb);
1883 break;
1884 case FILE_LINK_ITEM:
1885 src_dest_action_item(path, item->image,
1886 _("Symlink"), link_cb);
1887 break;
1888 case FILE_OPEN_FILE:
1889 filer_openitem(window_with_focus,
1890 collection_selected_item_number(
1891 window_with_focus->collection),
1892 OPEN_SAME_WINDOW | OPEN_SHIFT);
1893 break;
1894 case FILE_HELP:
1895 show_item_help(path, item);
1896 break;
1897 case FILE_RUN_ACTION:
1898 run_action(item);
1899 break;
1900 case FILE_SET_ICON:
1901 icon_set_handler_dialog(item, path);
1902 break;
1903 case FILE_OPEN_VFS_AVFS:
1904 open_vfs_avfs(window_with_focus, item);
1905 break;
1906 default:
1907 g_warning("Unknown action!");
1908 return;
1912 static void show_key_help(GtkWidget *button, gpointer data)
1914 gboolean can_change_accels;
1916 g_object_get(G_OBJECT(gtk_settings_get_default()),
1917 "gtk-can-change-accels", &can_change_accels,
1918 NULL);
1920 if (!can_change_accels)
1922 info_message(_("User-definable shortcuts are disabled by "
1923 "default in Gtk2, and you have not enabled "
1924 "them. You can turn this feature on by:\n"
1925 "1) using an XSettings manager, such as ROX-Session\n"
1926 "or\n"
1927 "2) adding this line to ~/.gtkrc-2.0:\n"
1928 "\tgtk-can-change-accels = 1"));
1929 return;
1932 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1933 "- Open the menu over a filer window,\n"
1934 "- Move the pointer over the item you want to use,\n"
1935 "- Press the key you want attached to it.\n\n"
1936 "The key will appear next to the menu item and you can just press "
1937 "that key without opening the menu in future."));
1940 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1942 GtkWidget *button, *align;
1944 g_return_val_if_fail(option == NULL, NULL);
1946 align = gtk_alignment_new(0.5, 0.5, 0, 0);
1947 button = button_new_mixed(GTK_STOCK_DIALOG_INFO,
1948 _("Set keyboard shortcuts"));
1949 gtk_container_add(GTK_CONTAINER(align), button);
1950 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1952 return g_list_append(NULL, align);