r2177: Style improvements to options window.
[rox-filer.git] / ROX-Filer / src / menu.c
blobbfaee1f77eba1806fe5cfe53f283dc381ac40c21
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* menu.c - code for handling the popup menus */
24 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <dirent.h>
35 #include <gtk/gtk.h>
37 #include "global.h"
39 #include "menu.h"
40 #include "run.h"
41 #include "action.h"
42 #include "filer.h"
43 #include "pixmaps.h"
44 #include "type.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "options.h"
48 #include "choices.h"
49 #include "gtksavebox.h"
50 #include "mount.h"
51 #include "minibuffer.h"
52 #include "i18n.h"
53 #include "main.h"
54 #include "pinboard.h"
55 #include "dir.h"
56 #include "diritem.h"
57 #include "appmenu.h"
58 #include "usericons.h"
59 #include "infobox.h"
60 #include "view_iface.h"
61 #include "display.h"
62 #include "bookmarks.h"
64 typedef enum {
65 FILE_COPY_ITEM,
66 FILE_RENAME_ITEM,
67 FILE_LINK_ITEM,
68 FILE_OPEN_FILE,
69 FILE_HELP,
70 FILE_SHOW_FILE_INFO,
71 FILE_RUN_ACTION,
72 FILE_SET_ICON,
73 FILE_SEND_TO,
74 FILE_DELETE,
75 FILE_USAGE,
76 FILE_CHMOD_ITEMS,
77 FILE_FIND,
78 FILE_OPEN_VFS_AVFS,
79 } FileOp;
81 typedef enum menu_icon_style {
82 MIS_NONE, MIS_SMALL, MIS_LARGE,
83 MIS_HUGE_UNUSED,
84 MIS_CURRENT, /* As per current filer window */
85 MIS_DEFAULT
86 } MenuIconStyle;
88 typedef void (*ActionFn)(GList *paths,
89 const char *dest_dir, const char *leaf, int quiet);
90 typedef void MenuCallback(GtkWidget *widget, gpointer data);
92 typedef gboolean (*SaveCb)(GObject *savebox,
93 const gchar *current, const gchar *new);
95 GtkAccelGroup *filer_keys = NULL;
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 show_bookmarks(gpointer data, guint action, GtkWidget *widget);
152 static void new_window(gpointer data, guint action, GtkWidget *widget);
153 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
154 static void close_window(gpointer data, guint action, GtkWidget *widget);
155 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
157 /* (action used in this - MiniType) */
158 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
159 static void resize(gpointer data, guint action, GtkWidget *widget);
161 #define MENUS_NAME "menus2"
163 static GtkWidget *filer_menu; /* The popup filer menu */
164 static GtkWidget *filer_file_item; /* The File '' label */
165 static GtkWidget *filer_file_menu; /* The File '' menu */
166 static GtkWidget *file_shift_item; /* Shift Open label */
167 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
168 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
169 static GtkWidget *filer_new_window; /* The New Window item */
170 static GtkWidget *filer_new_menu; /* The New submenu */
171 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
173 #undef N_
174 #define N_(x) x
176 static GtkItemFactoryEntry filer_menu_def[] = {
177 {N_("Display"), NULL, NULL, 0, "<Branch>"},
178 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
179 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
180 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
181 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
182 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
183 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
184 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
185 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
186 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
187 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
188 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
189 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
190 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
191 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
192 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
193 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
194 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
195 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
196 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
197 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
198 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
199 {">", NULL, NULL, 0, "<Separator>"},
200 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
201 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
202 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
203 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
204 {">", NULL, NULL, 0, "<Separator>"},
205 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
206 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
207 {">" N_("Refresh"), NULL, refresh, 0, NULL},
208 {N_("File"), NULL, NULL, 0, "<Branch>"},
209 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
210 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
211 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
212 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
213 {">", NULL, NULL, 0, "<Separator>"},
214 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
215 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
216 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
217 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
218 {">", NULL, NULL, 0, "<Separator>"},
219 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
220 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
221 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
222 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
223 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
224 {">", NULL, NULL, 0, "<Separator>"},
225 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
226 {N_("Select"), NULL, NULL, 0, "<Branch>"},
227 {">" N_("Select All"), NULL, select_all, 0, NULL},
228 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
229 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
230 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
231 {N_("Options..."), NULL, menu_show_options, 0, NULL},
232 {N_("New"), NULL, NULL, 0, "<Branch>"},
233 {">" N_("Directory"), NULL, new_directory, 0, NULL},
234 {">" N_("Blank file"), NULL, new_file, 0, NULL},
235 {N_("Window"), NULL, NULL, 0, "<Branch>"},
236 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
237 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
238 {">" N_("New Window"), NULL, new_window, 0, NULL},
239 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
240 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, NULL},
241 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
242 {">" N_("Resize Window"), NULL, resize, 0, NULL},
243 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
245 {">" N_("Close Window"), NULL, close_window, 0, NULL},
246 {">", NULL, NULL, 0, "<Separator>"},
247 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
248 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
249 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
250 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
251 {N_("Help"), NULL, NULL, 0, "<Branch>"},
252 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
253 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, NULL},
254 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
258 #define GET_MENU_ITEM(var, menu) \
259 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
261 #define GET_SMENU_ITEM(var, menu, sub) \
262 do { \
263 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
264 var = gtk_item_factory_get_widget(item_factory, tmp); \
265 g_free(tmp); \
266 } while (0)
268 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
269 do { \
270 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
271 var = gtk_item_factory_get_widget(item_factory, tmp); \
272 g_free(tmp); \
273 } while (0)
275 void ensure_filer_menu(void)
277 GList *items;
278 guchar *tmp;
279 GtkWidget *item;
280 GtkItemFactory *item_factory;
282 if (filer_keys)
283 return;
285 filer_keys = gtk_accel_group_new();
286 item_factory = menu_create(filer_menu_def,
287 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
288 "<filer>", filer_keys);
290 GET_MENU_ITEM(filer_menu, "filer");
291 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
292 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
293 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
294 "Show Thumbnails");
296 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
297 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
298 filer_follow_sym = GTK_BIN(item)->child;
300 /* File '' label... */
301 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
302 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
303 g_list_free(items);
305 /* Shift Open... label */
306 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
307 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
308 g_list_free(items);
310 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
311 filer_new_window = GTK_BIN(item)->child;
313 g_signal_connect(filer_menu, "unmap_event",
314 G_CALLBACK(menu_closed), NULL);
315 g_signal_connect(filer_file_menu, "unmap_event",
316 G_CALLBACK(menu_closed), NULL);
318 g_signal_connect(filer_keys, "accel_changed",
319 G_CALLBACK(save_menus), NULL);
322 void menu_init(void)
324 char *menurc;
326 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
327 if (menurc)
329 gtk_accel_map_load(menurc);
330 g_free(menurc);
333 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
334 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
335 option_add_saver(save_menus);
337 option_register_widget("menu-set-keys", set_keys_button);
340 /* Name is in the form "<panel>" */
341 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
342 const gchar *name, GtkAccelGroup *keys)
344 GtkItemFactory *item_factory;
345 GtkItemFactoryEntry *translated;
347 if (!keys)
349 keys = gtk_accel_group_new();
350 gtk_accel_group_lock(keys);
353 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
355 translated = translate_entries(def, n_entries);
356 gtk_item_factory_create_items(item_factory, n_entries,
357 translated, NULL);
358 free_translated_entries(translated, n_entries);
360 return item_factory;
363 /* Prevent the user from setting a short-cut on this item */
364 static void menuitem_no_shortcuts(GtkWidget *item)
366 /* XXX */
367 #if 0
368 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
370 _gtk_widget_set_accel_path(item, NULL, NULL);
371 null_g_free(&menuitem->accel_path);
372 #endif
375 /* Shade items that only work on single files */
376 static void shade_file_menu_items(gboolean shaded)
378 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
379 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
380 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
383 /* 'data' is an array of three ints:
384 * [ pointer_x, pointer_y, item_under_pointer ]
386 void position_menu(GtkMenu *menu, gint *x, gint *y,
387 gboolean *push_in, gpointer data)
389 int *pos = (int *) data;
390 GtkRequisition requisition;
391 GList *items, *next;
392 int y_shift = 0;
393 int item = pos[2];
395 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
397 while (item >= 0 && next)
399 int h = ((GtkWidget *) next->data)->requisition.height;
401 if (item > 0)
402 y_shift += h;
403 else
404 y_shift += h / 2;
406 next = next->next;
407 item--;
410 g_list_free(items);
412 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
414 *x = pos[0] - (requisition.width * 7 / 8);
415 *y = pos[1] - y_shift;
417 *x = CLAMP(*x, 0, screen_width - requisition.width);
418 *y = CLAMP(*y, 0, screen_height - requisition.height);
420 *push_in = FALSE;
423 #if 0
424 /* Used when you menu-click on the Large or Small toolbar tools */
425 void show_style_menu(FilerWindow *filer_window,
426 GdkEventButton *event,
427 GtkWidget *menu)
429 int pos[3];
431 pos[0] = event->x_root;
432 pos[1] = event->y_root;
433 pos[2] = 0;
435 window_with_focus = filer_window;
437 popup_menu = menu;
439 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
440 (gpointer) pos, event->button, event->time);
442 #endif
444 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
445 MenuIconStyle style, CallbackFn func,
446 gboolean separator, gboolean strip_ext,
447 gboolean recurse)
449 GList *widgets = NULL;
450 DirItem *ditem;
451 DIR *dir;
452 struct dirent *ent;
453 GtkWidget *item;
454 char *dname = NULL;
456 dname = pathdup(dir_name);
458 dir = opendir(dname);
459 if (!dir)
460 goto out;
462 if (separator)
464 item = gtk_menu_item_new();
465 widgets = g_list_append(widgets, item);
466 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
469 while ((ent = readdir(dir)))
471 char *dot, *leaf;
472 GtkWidget *hbox;
473 GtkWidget *img;
474 GtkWidget *label;
475 gchar *fname;
477 /* Ignore hidden files */
478 if (ent->d_name[0] == '.')
479 continue;
481 /* Strip off extension, if any */
482 dot = strchr(ent->d_name, '.');
483 if (strip_ext && dot)
484 leaf = g_strndup(ent->d_name, dot - ent->d_name);
485 else
486 leaf = g_strdup(ent->d_name);
488 fname = g_strconcat(dname, "/", ent->d_name, NULL);
489 ditem = diritem_new("");
490 diritem_restat(fname, ditem, NULL);
492 if (ditem->image && style != MIS_NONE)
494 GdkPixbuf *pixbuf;
496 switch (style) {
497 case MIS_LARGE:
498 pixbuf = ditem->image->pixbuf;
499 break;
500 default:
501 if (!ditem->image->sm_pixbuf)
502 pixmap_make_small(ditem->image);
503 pixbuf = ditem->image->sm_pixbuf;
504 break;
507 item = gtk_menu_item_new();
508 /* TODO: Find a way to allow short-cuts */
509 menuitem_no_shortcuts(item);
511 hbox = gtk_hbox_new(FALSE, 2);
512 gtk_container_add(GTK_CONTAINER(item), hbox);
514 img = gtk_image_new_from_pixbuf(pixbuf);
516 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
518 label = gtk_label_new(leaf);
519 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
520 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
523 else
524 item = gtk_menu_item_new_with_label(leaf);
526 /* If it is a directory (but NOT an AppDir) and we are
527 * recursing then set up a sub menu.
529 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
530 !(ditem->flags & ITEM_FLAG_APPDIR))
532 GtkWidget *sub;
533 GList *new_widgets;
535 sub = gtk_menu_new();
536 new_widgets = menu_from_dir(sub, fname, style, func,
537 separator, strip_ext, FALSE);
538 g_list_free(new_widgets);
539 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
541 else
542 g_signal_connect_swapped(item, "activate",
543 G_CALLBACK(func), fname);
545 diritem_free(ditem);
546 g_free(leaf);
548 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
549 g_signal_connect_swapped(item, "destroy",
550 G_CALLBACK(g_free), fname);
552 widgets = g_list_append(widgets, item);
555 closedir(dir);
556 out:
557 g_free(dname);
559 return widgets;
562 /* Scan the templates dir and create entries for the New menu */
563 static void update_new_files_menu(MenuIconStyle style)
565 static GList *widgets = NULL;
567 gchar *templ_dname = NULL;
569 if (widgets)
571 GList *next;
573 for (next = widgets; next; next = next->next)
574 gtk_widget_destroy((GtkWidget *) next->data);
576 g_list_free(widgets);
577 widgets = NULL;
580 templ_dname = choices_find_path_load("Templates", "");
581 if (templ_dname)
583 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
584 (CallbackFn) new_file_type, TRUE, TRUE,
585 FALSE);
586 g_free(templ_dname);
588 gtk_widget_show_all(filer_new_menu);
591 /* 'item' is the number of the item to appear under the pointer. */
592 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
594 int pos[3];
595 int button = 0;
596 guint32 time = 0;
598 if (event && (event->type == GDK_BUTTON_PRESS ||
599 event->type == GDK_BUTTON_RELEASE))
601 GdkEventButton *bev = (GdkEventButton *) event;
603 pos[0] = bev->x_root;
604 pos[1] = bev->y_root;
605 button = bev->button;
606 time = bev->time;
608 else if (event && event->type == GDK_KEY_PRESS)
610 GdkEventKey *kev = (GdkEventKey *) event;
612 get_pointer_xy(pos, pos + 1);
613 time = kev->time;
615 else
616 get_pointer_xy(pos, pos + 1);
618 pos[2] = item;
620 gtk_widget_show_all(menu);
621 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
622 position_menu, (gpointer) pos, button, time);
623 select_nth_item(GTK_MENU_SHELL(menu), item);
626 /* Hide the popup menu, if any */
627 void menu_popdown(void)
629 if (popup_menu)
630 gtk_menu_popdown(GTK_MENU(popup_menu));
633 static MenuIconStyle get_menu_icon_style(void)
635 MenuIconStyle mis;
636 int display;
638 mis = o_menu_iconsize.int_value;
640 switch (mis)
642 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
643 return mis;
644 default:
645 break;
648 if (mis == MIS_CURRENT && window_with_focus)
650 switch (window_with_focus->display_style)
652 case HUGE_ICONS:
653 case LARGE_ICONS:
654 return MIS_LARGE;
655 case SMALL_ICONS:
656 return MIS_SMALL;
657 default:
658 break;
662 display = o_display_size.int_value;
663 switch (display)
665 case HUGE_ICONS:
666 case LARGE_ICONS:
667 return MIS_LARGE;
668 case SMALL_ICONS:
669 return MIS_SMALL;
670 default:
671 break;
674 return MIS_SMALL;
677 /* iter->peek() is the clicked item, or NULL if none */
678 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
680 DirItem *file_item = NULL;
681 GdkModifierType state = 0;
682 int n_selected;
684 n_selected = view_count_selected(filer_window->view);
686 ensure_filer_menu();
688 updating_menu++;
690 /* Remove previous AppMenu, if any */
691 appmenu_remove();
693 window_with_focus = filer_window;
695 if (!event)
696 event = gtk_get_current_event();
698 if (event->type == GDK_BUTTON_PRESS)
699 state = ((GdkEventButton *) event)->state;
700 else if (event->type == GDK_KEY_PRESS)
701 state = ((GdkEventKey *) event)->state;
703 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
705 filer_window->temp_item_selected = TRUE;
706 view_set_selected(filer_window->view, iter, TRUE);
707 n_selected = view_count_selected(filer_window->view);
709 else
711 filer_window->temp_item_selected = FALSE;
714 /* Short-cut to the Send To menu */
715 if (state & GDK_SHIFT_MASK)
717 GList *paths;
719 updating_menu--;
721 if (n_selected == 0)
723 report_error(
724 _("You should Shift+Menu click over a file to "
725 "send it somewhere"));
726 return;
729 paths = filer_selected_items(filer_window);
731 show_send_to_menu(paths, event); /* (paths eaten) */
733 return;
737 GtkWidget *file_label, *file_menu;
738 GString *buffer;
739 DirItem *item;
741 file_label = filer_file_item;
742 file_menu = filer_file_menu;
743 gtk_check_menu_item_set_active(
744 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
745 filer_window->show_thumbs);
746 gtk_check_menu_item_set_active(
747 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
748 filer_window->show_hidden);
749 buffer = g_string_new(NULL);
751 switch (n_selected)
753 case 0:
754 g_string_assign(buffer, _("Next Click"));
755 shade_file_menu_items(FALSE);
756 break;
757 case 1:
758 item = filer_selected_item(filer_window);
759 if (!item->image)
760 dir_update_item(filer_window->directory,
761 item->leafname);
762 shade_file_menu_items(FALSE);
763 file_item = filer_selected_item(filer_window);
764 g_string_printf(buffer, "%s '%s'",
765 basetype_name(file_item),
766 g_utf8_validate(file_item->leafname,
767 -1, NULL)
768 ? file_item->leafname
769 : _("(bad utf-8)"));
770 if (!can_set_run_action(file_item))
771 menu_set_items_shaded(filer_file_menu,
772 TRUE, 6, 1);
773 break;
774 default:
775 shade_file_menu_items(TRUE);
776 g_string_printf(buffer, _("%d items"),
777 n_selected);
778 break;
780 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
781 g_string_free(buffer, TRUE);
783 menu_show_shift_action(file_shift_item, file_item,
784 n_selected == 0);
785 if (file_item)
786 appmenu_add(make_path(filer_window->sym_path,
787 file_item->leafname)->str,
788 file_item, filer_file_menu);
791 update_new_files_menu(get_menu_icon_style());
793 gtk_widget_set_sensitive(filer_new_window,
794 !o_unique_filer_windows.int_value);
795 gtk_widget_set_sensitive(filer_follow_sym,
796 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
798 popup_menu = (state & GDK_CONTROL_MASK)
799 ? filer_file_menu
800 : filer_menu;
802 updating_menu--;
804 show_popup_menu(popup_menu, event,
805 popup_menu == filer_file_menu ? 5 : 1);
808 static void menu_closed(GtkWidget *widget)
810 if (window_with_focus == NULL || widget != popup_menu)
811 return; /* Close panel item chosen? */
813 popup_menu = NULL;
815 if (window_with_focus->temp_item_selected)
817 view_clear_selection(window_with_focus->view);
818 window_with_focus->temp_item_selected = FALSE;
822 static void target_callback(FilerWindow *filer_window,
823 ViewIter *iter,
824 gpointer action)
826 g_return_if_fail(filer_window != NULL);
828 window_with_focus = filer_window;
830 /* Don't grab the primary selection */
831 filer_window->temp_item_selected = TRUE;
833 view_wink_item(filer_window->view, iter);
834 view_select_only(filer_window->view, iter);
835 file_op(NULL, GPOINTER_TO_INT(action), NULL);
837 view_clear_selection(filer_window->view);
838 filer_window->temp_item_selected = FALSE;
841 /* Set the text of the 'Shift Open...' menu item.
842 * If icon is NULL, reset the text and also shade it, unless 'next'.
844 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
846 guchar *shift_action = NULL;
848 if (item)
850 if (item->flags & ITEM_FLAG_MOUNT_POINT)
852 if (item->flags & ITEM_FLAG_MOUNTED)
853 shift_action = N_("Unmount");
854 else
855 shift_action = N_("Open unmounted");
857 else if (item->flags & ITEM_FLAG_SYMLINK)
858 shift_action = N_("Show Target");
859 else if (item->base_type == TYPE_DIRECTORY)
860 shift_action = N_("Look Inside");
861 else if (item->base_type == TYPE_FILE)
862 shift_action = N_("Open As Text");
864 gtk_label_set_text(GTK_LABEL(menu_item),
865 shift_action ? _(shift_action)
866 : _("Shift Open"));
867 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
870 /* Actions */
872 static void huge_with(gpointer data, guint action, GtkWidget *widget)
874 display_set_layout(window_with_focus, HUGE_ICONS, action);
877 static void large_with(gpointer data, guint action, GtkWidget *widget)
879 display_set_layout(window_with_focus, LARGE_ICONS, action);
882 static void small_with(gpointer data, guint action, GtkWidget *widget)
884 display_set_layout(window_with_focus, SMALL_ICONS, action);
887 static void sort_name(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_name);
894 static void sort_type(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_type);
901 static void sort_date(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_date);
908 static void sort_size(gpointer data, guint action, GtkWidget *widget)
910 g_return_if_fail(window_with_focus != NULL);
912 display_set_sort_fn(window_with_focus, sort_by_size);
915 static void hidden(gpointer data, guint action, GtkWidget *widget)
917 if (updating_menu)
918 return;
920 g_return_if_fail(window_with_focus != NULL);
922 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
925 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
927 if (updating_menu)
928 return;
930 g_return_if_fail(window_with_focus != NULL);
932 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
935 static void refresh(gpointer data, guint action, GtkWidget *widget)
937 g_return_if_fail(window_with_focus != NULL);
939 full_refresh();
940 filer_update_dir(window_with_focus, TRUE);
943 static void delete(FilerWindow *filer_window)
945 GList *paths;
946 paths = filer_selected_items(filer_window);
947 action_delete(paths);
948 destroy_glist(&paths);
951 static void usage(FilerWindow *filer_window)
953 GList *paths;
954 paths = filer_selected_items(filer_window);
955 action_usage(paths);
956 destroy_glist(&paths);
959 static void chmod_items(FilerWindow *filer_window)
961 GList *paths;
962 paths = filer_selected_items(filer_window);
963 action_chmod(paths, FALSE, NULL);
964 destroy_glist(&paths);
967 static void find(FilerWindow *filer_window)
969 GList *paths;
970 paths = filer_selected_items(filer_window);
971 action_find(paths);
972 destroy_glist(&paths);
975 /* This creates a new savebox widget, and allows the user to pick a new path
976 * for the file.
977 * Once the new path has been picked, the callback will be called with
978 * both the current and new paths.
979 * NOTE: This function unrefs 'image'!
981 static void savebox_show(const gchar *action, const gchar *path,
982 MaskedPixmap *image, SaveCb callback)
984 GtkWidget *savebox = NULL;
985 GtkWidget *check_relative = NULL;
987 g_return_if_fail(image != NULL);
989 savebox = gtk_savebox_new(action);
991 if (callback == link_cb)
993 check_relative = gtk_check_button_new_with_mnemonic(
994 _("_Relative link"));
995 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
996 TRUE);
998 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
999 gtk_tooltips_set_tip(tooltips, check_relative,
1000 _("If on, the symlink will store the path from the "
1001 "symlink to the target file. Use this if the symlink "
1002 "and the target will be moved together.\n"
1003 "If off, the path from the root directory is stored - "
1004 "use this if the symlink may move but the target will "
1005 "stay put."), NULL);
1006 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1007 check_relative, FALSE, TRUE, 0);
1008 gtk_widget_show(check_relative);
1011 g_signal_connect(savebox, "save_to_file",
1012 G_CALLBACK(save_to_file), NULL);
1014 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1015 g_strdup(path), g_free);
1016 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1017 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1019 gtk_window_set_title(GTK_WINDOW(savebox), action);
1021 if (g_utf8_validate(path, -1, NULL))
1022 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1023 else
1025 gchar *u8;
1026 u8 = to_utf8(path);
1027 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1028 g_free(u8);
1030 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1031 g_object_unref(image);
1033 gtk_widget_show(savebox);
1036 static gint save_to_file(GObject *savebox,
1037 const gchar *pathname, gpointer data)
1039 SaveCb callback;
1040 const gchar *current_path;
1042 callback = g_object_get_data(savebox, "action_callback");
1043 current_path = g_object_get_data(savebox, "current_path");
1045 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1046 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1048 return callback(savebox, current_path, pathname)
1049 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1052 static gboolean copy_cb(GObject *savebox,
1053 const gchar *current, const gchar *new)
1055 return action_with_leaf(action_copy, current, new);
1058 static gboolean action_with_leaf(ActionFn action,
1059 const gchar *current, const gchar *new)
1061 const char *leaf;
1062 char *new_dir;
1063 GList *local_paths;
1065 if (new[0] != '/')
1067 report_error(_("New pathname is not absolute"));
1068 return FALSE;
1071 if (new[strlen(new) - 1] == '/')
1073 new_dir = g_strdup(new);
1074 leaf = NULL;
1076 else
1078 const gchar *slash;
1080 slash = strrchr(new, '/');
1081 new_dir = g_strndup(new, slash - new);
1082 leaf = slash + 1;
1085 local_paths = g_list_append(NULL, (gchar *) current);
1086 action(local_paths, new_dir, leaf, -1);
1087 g_list_free(local_paths);
1089 g_free(new_dir);
1091 return TRUE;
1094 /* Open a savebox to act on the selected file.
1095 * Call 'callback' later to perform the operation.
1097 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1098 const gchar *action, SaveCb callback)
1100 g_object_ref(image);
1101 savebox_show(action, path, image, callback);
1104 static gboolean rename_cb(GObject *savebox,
1105 const gchar *current, const gchar *new)
1107 return action_with_leaf(action_move, current, new);
1110 static gboolean link_cb(GObject *savebox,
1111 const gchar *initial, const gchar *path)
1113 GtkToggleButton *check_relative;
1114 struct stat info;
1115 int err;
1116 gchar *link_path;
1118 check_relative = g_object_get_data(savebox, "check_relative");
1120 if (gtk_toggle_button_get_active(check_relative))
1121 link_path = get_relative_path(path, initial);
1122 else
1123 link_path = g_strdup(initial);
1125 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1127 GtkWidget *box, *button;
1128 gint ans;
1130 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1131 GTK_BUTTONS_CANCEL,
1132 _("Symlink from '%s' already exists. "
1133 "Replace it with a link to '%s'?"),
1134 path, link_path);
1136 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1138 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1139 gtk_widget_show(button);
1140 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1141 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1142 button, GTK_RESPONSE_OK);
1143 gtk_dialog_set_default_response(GTK_DIALOG(box),
1144 GTK_RESPONSE_OK);
1146 ans = gtk_dialog_run(GTK_DIALOG(box));
1147 gtk_widget_destroy(box);
1149 if (ans != GTK_RESPONSE_OK)
1151 g_free(link_path);
1152 return FALSE;
1155 unlink(path);
1158 err = symlink(link_path, path);
1159 g_free(link_path);
1161 if (err)
1163 report_error("symlink: %s", g_strerror(errno));
1164 return FALSE;
1167 dir_check_this(path);
1169 return TRUE;
1172 static void run_action(DirItem *item)
1174 if (can_set_run_action(item))
1175 type_set_handler_dialog(item->mime_type);
1176 else
1177 report_error(
1178 _("You can only set the run action for a "
1179 "regular file"));
1182 void open_home(gpointer data, guint action, GtkWidget *widget)
1184 filer_opendir(home_dir, NULL, NULL);
1187 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1189 gchar *path;
1191 path = g_strconcat(filer_window->sym_path,
1192 "/", item->leafname, "#", NULL);
1194 filer_change_to(filer_window, path, NULL);
1195 g_free(path);
1198 static void select_all(gpointer data, guint action, GtkWidget *widget)
1200 g_return_if_fail(window_with_focus != NULL);
1202 window_with_focus->temp_item_selected = FALSE;
1203 view_select_all(window_with_focus->view);
1206 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1208 g_return_if_fail(window_with_focus != NULL);
1210 window_with_focus->temp_item_selected = FALSE;
1211 view_clear_selection(window_with_focus->view);
1214 static gboolean invert_cb(ViewIter *iter, gpointer data)
1216 return !view_get_selected((ViewIface *) data, iter);
1219 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1221 g_return_if_fail(window_with_focus != NULL);
1223 window_with_focus->temp_item_selected = FALSE;
1225 view_select_if(window_with_focus->view, invert_cb,
1226 window_with_focus->view);
1229 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1231 GtkWidget *win;
1233 win = options_show();
1235 if (win)
1237 number_of_windows++;
1238 g_signal_connect(win, "destroy",
1239 G_CALLBACK(one_less_window), NULL);
1243 static gboolean new_directory_cb(GObject *savebox,
1244 const gchar *initial, const gchar *path)
1246 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1248 report_error("mkdir: %s", g_strerror(errno));
1249 return FALSE;
1252 dir_check_this(path);
1254 if (filer_exists(window_with_focus))
1256 guchar *leaf;
1257 leaf = strrchr(path, '/');
1258 if (leaf)
1259 display_set_autoselect(window_with_focus, leaf + 1);
1262 return TRUE;
1265 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1267 g_return_if_fail(window_with_focus != NULL);
1269 savebox_show(_("Create"),
1270 make_path(window_with_focus->sym_path, _("NewDir"))->str,
1271 type_to_icon(inode_directory), new_directory_cb);
1274 static gboolean new_file_cb(GObject *savebox,
1275 const gchar *initial, const gchar *path)
1277 int fd;
1279 fd = open(path, O_CREAT | O_EXCL, 0666);
1281 if (fd == -1)
1283 report_error(_("Error creating '%s': %s"),
1284 path, g_strerror(errno));
1285 return FALSE;
1288 if (close(fd))
1289 report_error(_("Error creating '%s': %s"),
1290 path, g_strerror(errno));
1292 dir_check_this(path);
1294 if (filer_exists(window_with_focus))
1296 guchar *leaf;
1297 leaf = strrchr(path, '/');
1298 if (leaf)
1299 display_set_autoselect(window_with_focus, leaf + 1);
1302 return TRUE;
1305 static void new_file(gpointer data, guint action, GtkWidget *widget)
1307 g_return_if_fail(window_with_focus != NULL);
1309 savebox_show(_("Create"),
1310 make_path(window_with_focus->sym_path, _("NewFile"))->str,
1311 type_to_icon(text_plain),
1312 new_file_cb);
1315 static gboolean new_file_type_cb(GObject *savebox,
1316 const gchar *initial, const gchar *path)
1318 const gchar *oleaf, *leaf;
1319 gchar *templ, *templ_dname, *dest;
1320 GList *paths;
1322 /* We can work out the template path from the initial name */
1323 oleaf = g_basename(initial);
1324 templ_dname = choices_find_path_load("Templates", "");
1325 if (!templ_dname)
1327 report_error(
1328 _("Error creating file: could not find the template for %s"),
1329 oleaf);
1330 return FALSE;
1333 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1334 g_free(templ_dname);
1336 dest = g_path_get_dirname(path);
1337 leaf = g_basename(path);
1338 paths = g_list_append(NULL, templ);
1340 action_copy(paths, dest, leaf, TRUE);
1342 g_list_free(paths);
1343 g_free(dest);
1344 g_free(templ);
1346 if (filer_exists(window_with_focus))
1347 display_set_autoselect(window_with_focus, leaf);
1349 return TRUE;
1352 static void do_send_to(gchar *templ)
1354 g_return_if_fail(send_to_paths != NULL);
1356 run_with_files(templ, send_to_paths);
1359 static void new_file_type(gchar *templ)
1361 const gchar *leaf;
1362 MIME_type *type;
1364 g_return_if_fail(window_with_focus != NULL);
1366 leaf = g_basename(templ);
1367 type = type_get_type(templ);
1369 savebox_show(_("Create"),
1370 make_path(window_with_focus->sym_path, leaf)->str,
1371 type_to_icon(type),
1372 new_file_type_cb);
1375 static void customise_send_to(gpointer data)
1377 GPtrArray *path;
1378 guchar *save;
1379 GString *dirs;
1380 int i;
1382 dirs = g_string_new(NULL);
1384 path = choices_list_dirs("");
1385 for (i = 0; i < path->len; i++)
1387 guchar *old = (guchar *) path->pdata[i];
1389 g_string_append(dirs, old);
1390 g_string_append(dirs, "SendTo\n");
1392 choices_free_list(path);
1394 save = choices_find_path_save("", "SendTo", TRUE);
1395 if (save)
1396 mkdir(save, 0777);
1398 info_message(
1399 _("The `Send To' menu provides a quick way to send some files "
1400 "to an application. The applications listed are those in "
1401 "the following directories:\n\n%s\n%s\n"
1402 "The `Send To' menu may be opened by Shift+Menu clicking "
1403 "over a file.\n\n"
1404 "Advanced use:\n"
1405 "You can also create subdirectories called "
1406 "`.text_html', `.text', etc which will only be "
1407 "shown for files of that type. `.group' is shown "
1408 "only when multiple files are selected."),
1409 dirs->str,
1410 save ? _("I'll show you your SendTo directory now; you should "
1411 "symlink (Ctrl+Shift drag) any applications you want "
1412 "into it.")
1413 : _("Your CHOICESPATH variable setting prevents "
1414 "customisations - sorry."));
1416 g_string_free(dirs, TRUE);
1418 if (save)
1419 filer_opendir(save, NULL, NULL);
1422 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1423 * to the menu.
1425 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1427 gchar *searchdir;
1428 GPtrArray *paths;
1429 int i;
1431 if (subtype)
1432 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1433 else if (type)
1434 searchdir = g_strdup_printf("SendTo/.%s", type);
1435 else
1436 searchdir = g_strdup("SendTo");
1438 paths = choices_list_dirs(searchdir);
1439 g_free(searchdir);
1441 for (i = 0; i < paths->len; i++)
1443 GList *widgets = NULL;
1444 guchar *dir = (guchar *) paths->pdata[i];
1446 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1447 (CallbackFn) do_send_to,
1448 FALSE, FALSE, TRUE);
1450 if (widgets)
1451 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1452 gtk_menu_item_new());
1454 g_list_free(widgets); /* TODO: Get rid of this */
1457 choices_free_list(paths);
1460 /* Scan the SendTo dir and create and show the Send To menu.
1461 * The 'paths' list and every path in it is claimed, and will be
1462 * freed later -- don't free it yourself!
1464 static void show_send_to_menu(GList *paths, GdkEvent *event)
1466 GtkWidget *menu, *item;
1468 menu = gtk_menu_new();
1470 if (g_list_length(paths) == 1)
1472 DirItem *item;
1474 item = diritem_new("");
1475 diritem_restat(paths->data, item, NULL);
1477 add_sendto(menu,
1478 item->mime_type->media_type,
1479 item->mime_type->subtype);
1481 add_sendto(menu, item->mime_type->media_type, NULL);
1483 diritem_free(item);
1485 else
1486 add_sendto(menu, "group", NULL);
1488 add_sendto(menu, NULL, NULL);
1490 item = gtk_menu_item_new_with_label(_("Customise"));
1491 g_signal_connect_swapped(item, "activate",
1492 G_CALLBACK(customise_send_to), NULL);
1493 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1495 if (send_to_paths)
1496 destroy_glist(&send_to_paths);
1498 send_to_paths = paths;
1500 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1502 popup_menu = menu;
1503 show_popup_menu(menu, event, 0);
1506 static void send_to(FilerWindow *filer_window)
1508 GList *paths;
1509 GdkEvent *event;
1511 paths = filer_selected_items(filer_window);
1512 event = gtk_get_current_event();
1514 /* Eats paths */
1515 show_send_to_menu(paths, event);
1517 gdk_event_free(event);
1520 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1522 const char *argv[] = {"sh", "-c", NULL, NULL};
1523 gboolean close = action;
1525 argv[2] = o_menu_xterm.value;
1527 g_return_if_fail(window_with_focus != NULL);
1529 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1530 gtk_widget_destroy(window_with_focus->window);
1533 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1535 g_return_if_fail(window_with_focus != NULL);
1537 filer_change_to(window_with_focus, home_dir, NULL);
1540 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1542 g_return_if_fail(window_with_focus != NULL);
1544 bookmarks_show_menu(window_with_focus);
1547 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1549 g_return_if_fail(window_with_focus != NULL);
1551 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1552 filer_change_to(window_with_focus,
1553 window_with_focus->real_path, NULL);
1554 else
1555 delayed_error(_("This is already the canonical name "
1556 "for this directory."));
1559 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1561 g_return_if_fail(window_with_focus != NULL);
1563 filer_open_parent(window_with_focus);
1566 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1568 g_return_if_fail(window_with_focus != NULL);
1570 change_to_parent(window_with_focus);
1573 static void resize(gpointer data, guint action, GtkWidget *widget)
1575 g_return_if_fail(window_with_focus != NULL);
1577 filer_window_autosize(window_with_focus);
1580 static void new_window(gpointer data, guint action, GtkWidget *widget)
1582 g_return_if_fail(window_with_focus != NULL);
1584 if (o_unique_filer_windows.int_value)
1586 report_error(_("You can't open a second view onto "
1587 "this directory because the `Unique Windows' option "
1588 "is turned on in the Options window."));
1590 else
1591 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1594 #if 0
1595 static void su_to_user(GtkWidget *dialog)
1597 char *argv[] = {
1598 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1599 GtkEntry *user;
1600 guchar *path;
1602 g_return_if_fail(dialog != NULL);
1604 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1605 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1607 g_return_if_fail(user != NULL && path != NULL);
1609 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1610 argv[3] = gtk_entry_get_text(user);
1611 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1612 argv[5] = path;
1614 if (!spawn(argv))
1615 report_error(_("fork: %s"), g_strerror(errno));
1617 g_free(argv[2]);
1618 g_free(argv[4]);
1620 gtk_widget_destroy(dialog);
1623 static void new_user(gpointer data, guint action, GtkWidget *widget)
1625 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1627 g_return_if_fail(window_with_focus != NULL);
1629 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1630 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1631 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1632 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1633 g_strdup(window_with_focus->path), g_free);
1635 vbox = gtk_vbox_new(FALSE, 4);
1636 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1637 gtk_box_pack_start(GTK_BOX(vbox),
1638 gtk_label_new(_("Browse as which user?")),
1639 TRUE, TRUE, 2);
1641 hbox = gtk_hbox_new(FALSE, 4);
1642 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1644 gtk_box_pack_start(GTK_BOX(hbox),
1645 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1647 entry = gtk_entry_new();
1648 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1649 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1650 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1651 gtk_widget_grab_focus(entry);
1652 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1653 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1654 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1656 hbox = gtk_hbox_new(TRUE, 0);
1657 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1659 button = gtk_button_new_with_label(_("OK"));
1660 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1661 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1662 gtk_window_set_default(GTK_WINDOW(dialog), button);
1663 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1664 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1666 button = gtk_button_new_with_label(_("Cancel"));
1667 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1668 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1669 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1670 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1671 GTK_OBJECT(dialog));
1673 gtk_widget_show_all(dialog);
1675 #endif
1677 static void close_window(gpointer data, guint action, GtkWidget *widget)
1679 g_return_if_fail(window_with_focus != NULL);
1681 gtk_widget_destroy(window_with_focus->window);
1684 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1686 MiniType type = (MiniType) action;
1688 g_return_if_fail(window_with_focus != NULL);
1690 /* Item needs to remain selected... */
1691 if (type == MINI_SHELL)
1692 window_with_focus->temp_item_selected = FALSE;
1694 minibuffer_show(window_with_focus, type);
1697 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1699 if (action == HELP_ABOUT)
1700 infobox_new(app_dir);
1701 else if (action == HELP_DIR)
1702 filer_opendir(make_path(app_dir, "Help")->str, NULL, NULL);
1703 else if (action == HELP_MANUAL)
1705 gchar *manual = NULL;
1707 if (current_lang)
1709 manual = g_strconcat(app_dir, "/Help/Manual-",
1710 current_lang, ".html", NULL);
1711 if (access(manual, F_OK))
1712 null_g_free(&manual);
1715 if (!manual)
1716 manual = g_strconcat(app_dir,
1717 "/Help/Manual.html", NULL);
1719 run_by_path(manual);
1721 g_free(manual);
1723 else
1724 g_warning("Unknown help action %d\n", action);
1727 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1728 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1730 GList *items, *item;
1732 items = gtk_container_get_children(GTK_CONTAINER(menu));
1734 item = g_list_nth(items, from);
1735 while (item && n--)
1737 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1738 item = item->next;
1740 g_list_free(items);
1743 static void save_menus(void)
1745 char *menurc;
1747 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1748 if (menurc)
1750 gtk_accel_map_save(menurc);
1751 g_free(menurc);
1755 static void select_nth_item(GtkMenuShell *shell, int n)
1757 GList *items;
1758 GtkWidget *item;
1760 items = gtk_container_get_children(GTK_CONTAINER(shell));
1761 item = g_list_nth_data(items, n);
1763 g_return_if_fail(item != NULL);
1765 g_list_free(items);
1767 gtk_menu_shell_select_item(shell, item);
1770 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1772 DirItem *item;
1773 gchar *path;
1774 int n_selected;
1775 ViewIter iter;
1777 g_return_if_fail(window_with_focus != NULL);
1779 n_selected = view_count_selected(window_with_focus->view);
1781 if (n_selected < 1)
1783 const char *prompt;
1785 switch (action)
1787 case FILE_COPY_ITEM:
1788 prompt = _("Copy ... ?");
1789 break;
1790 case FILE_RENAME_ITEM:
1791 prompt = _("Rename ... ?");
1792 break;
1793 case FILE_LINK_ITEM:
1794 prompt = _("Symlink ... ?");
1795 break;
1796 case FILE_OPEN_FILE:
1797 prompt = _("Shift Open ... ?");
1798 break;
1799 case FILE_HELP:
1800 prompt = _("Help about ... ?");
1801 break;
1802 case FILE_SHOW_FILE_INFO:
1803 prompt = _("Examine ... ?");
1804 break;
1805 case FILE_RUN_ACTION:
1806 prompt = _("Set run action for ... ?");
1807 break;
1808 case FILE_SET_ICON:
1809 prompt = _("Set icon for ... ?");
1810 break;
1811 case FILE_SEND_TO:
1812 prompt = _("Send ... to ... ?");
1813 break;
1814 case FILE_DELETE:
1815 prompt = _("DELETE ... ?");
1816 break;
1817 case FILE_USAGE:
1818 prompt = _("Count the size of ... ?");
1819 break;
1820 case FILE_CHMOD_ITEMS:
1821 prompt = _("Set permissions on ... ?");
1822 break;
1823 case FILE_FIND:
1824 prompt = _("Search inside ... ?");
1825 break;
1826 case FILE_OPEN_VFS_AVFS:
1827 prompt = _("Look inside ... ?");
1828 break;
1829 default:
1830 g_warning("Unknown action!");
1831 return;
1833 filer_target_mode(window_with_focus, target_callback,
1834 GINT_TO_POINTER(action), prompt);
1835 return;
1838 switch (action)
1840 case FILE_SEND_TO:
1841 send_to(window_with_focus);
1842 return;
1843 case FILE_DELETE:
1844 delete(window_with_focus);
1845 return;
1846 case FILE_USAGE:
1847 usage(window_with_focus);
1848 return;
1849 case FILE_CHMOD_ITEMS:
1850 chmod_items(window_with_focus);
1851 return;
1852 case FILE_FIND:
1853 find(window_with_focus);
1854 return;
1855 case FILE_SHOW_FILE_INFO:
1857 GList *items;
1859 items = filer_selected_items(window_with_focus);
1860 infobox_show_list(items);
1861 destroy_glist(&items);
1862 return;
1864 default:
1865 break;
1868 /* All the following actions require exactly one file selected */
1870 if (n_selected > 1)
1872 report_error(_("You cannot do this to more than "
1873 "one item at a time"));
1874 return;
1877 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1879 item = iter.next(&iter);
1880 g_return_if_fail(item != NULL);
1881 /* iter may be passed to filer_openitem... */
1883 if (!item->image)
1884 item = dir_update_item(window_with_focus->directory,
1885 item->leafname);
1887 if (!item)
1889 report_error(_("Item no longer exists!"));
1890 return;
1893 path = make_path(window_with_focus->sym_path, item->leafname)->str;
1895 switch (action)
1897 case FILE_COPY_ITEM:
1898 src_dest_action_item(path, item->image,
1899 _("Copy"), copy_cb);
1900 break;
1901 case FILE_RENAME_ITEM:
1902 src_dest_action_item(path, item->image,
1903 _("Rename"), rename_cb);
1904 break;
1905 case FILE_LINK_ITEM:
1906 src_dest_action_item(path, item->image,
1907 _("Symlink"), link_cb);
1908 break;
1909 case FILE_OPEN_FILE:
1910 filer_openitem(window_with_focus, &iter,
1911 OPEN_SAME_WINDOW | OPEN_SHIFT);
1912 break;
1913 case FILE_HELP:
1914 show_item_help(path, item);
1915 break;
1916 case FILE_RUN_ACTION:
1917 run_action(item);
1918 break;
1919 case FILE_SET_ICON:
1920 icon_set_handler_dialog(item, path);
1921 break;
1922 case FILE_OPEN_VFS_AVFS:
1923 open_vfs_avfs(window_with_focus, item);
1924 break;
1925 default:
1926 g_warning("Unknown action!");
1927 return;
1931 static void show_key_help(GtkWidget *button, gpointer data)
1933 gboolean can_change_accels;
1935 g_object_get(G_OBJECT(gtk_settings_get_default()),
1936 "gtk-can-change-accels", &can_change_accels,
1937 NULL);
1939 if (!can_change_accels)
1941 info_message(_("User-definable shortcuts are disabled by "
1942 "default in Gtk2, and you have not enabled "
1943 "them. You can turn this feature on by:\n"
1944 "1) using an XSettings manager, such as ROX-Session\n"
1945 "or\n"
1946 "2) adding this line to ~/.gtkrc-2.0:\n"
1947 "\tgtk-can-change-accels = 1"));
1948 return;
1951 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1952 "- Open the menu over a filer window,\n"
1953 "- Move the pointer over the item you want to use,\n"
1954 "- Press the key you want attached to it.\n\n"
1955 "The key will appear next to the menu item and you can just press "
1956 "that key without opening the menu in future."));
1959 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1961 GtkWidget *button, *align;
1963 g_return_val_if_fail(option == NULL, NULL);
1965 align = gtk_alignment_new(0, 0.5, 0, 0);
1966 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1967 gtk_container_add(GTK_CONTAINER(align), button);
1968 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1970 return g_list_append(NULL, align);