r3263: Make Ctrl-button-3 open the menu slightly higher, so Delete isn't under the
[rox-filer.git] / ROX-Filer / src / menu.c
blob5868bd1c32ef81a2dc633aed9b4bc087466a9b42
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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_PROPERTIES,
70 FILE_RUN_ACTION,
71 FILE_SET_ICON,
72 FILE_SEND_TO,
73 FILE_DELETE,
74 FILE_USAGE,
75 FILE_CHMOD_ITEMS,
76 FILE_FIND,
77 FILE_OPEN_VFS_AVFS,
78 FILE_SET_TYPE,
79 } FileOp;
81 typedef void (*ActionFn)(GList *paths,
82 const char *dest_dir, const char *leaf, int quiet);
83 typedef void MenuCallback(GtkWidget *widget, gpointer data);
85 typedef gboolean (*SaveCb)(GObject *savebox,
86 const gchar *current, const gchar *new);
88 GtkAccelGroup *filer_keys = NULL;
89 static gboolean filer_keys_need_init = TRUE;
91 static GtkWidget *popup_menu = NULL; /* Currently open menu */
93 static gint updating_menu = 0; /* Non-zero => ignore activations */
94 static GList *send_to_paths = NULL;
96 static Option o_menu_iconsize, o_menu_xterm;
98 /* Static prototypes */
100 static void save_menus(void);
101 static void menu_closed(GtkWidget *widget);
102 static void shade_file_menu_items(gboolean shaded);
103 static void savebox_show(const gchar *action, const gchar *path,
104 MaskedPixmap *image, SaveCb callback,
105 GdkDragAction dnd_action);
106 static gint save_to_file(GObject *savebox,
107 const gchar *pathname, gpointer data);
108 static gboolean action_with_leaf(ActionFn action,
109 const gchar *current, const gchar *new);
110 static gboolean link_cb(GObject *savebox,
111 const gchar *initial, const gchar *path);
112 static void select_nth_item(GtkMenuShell *shell, int n);
113 static void new_file_type(gchar *templ);
114 static void do_send_to(gchar *templ);
115 static void show_send_to_menu(GList *paths, GdkEvent *event);
116 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
118 /* Note that for most of these callbacks none of the arguments are used. */
120 static void view_type(gpointer data, guint action, GtkWidget *widget);
122 /* (action used in these three - DetailsType) */
123 static void change_size(gpointer data, guint action, GtkWidget *widget);
124 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
125 static void set_with(gpointer data, guint action, GtkWidget *widget);
127 static void set_sort(gpointer data, guint action, GtkWidget *widget);
128 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
130 static void hidden(gpointer data, guint action, GtkWidget *widget);
131 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
132 static void refresh(gpointer data, guint action, GtkWidget *widget);
134 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
136 static void select_all(gpointer data, guint action, GtkWidget *widget);
137 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
138 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
139 static void new_directory(gpointer data, guint action, GtkWidget *widget);
140 static void new_file(gpointer data, guint action, GtkWidget *widget);
141 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
143 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
144 static void open_parent(gpointer data, guint action, GtkWidget *widget);
145 static void home_directory(gpointer data, guint action, GtkWidget *widget);
146 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
147 static void new_window(gpointer data, guint action, GtkWidget *widget);
148 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
149 static void close_window(gpointer data, guint action, GtkWidget *widget);
150 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
152 /* (action used in this - MiniType) */
153 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
154 static void resize(gpointer data, guint action, GtkWidget *widget);
156 #define MENUS_NAME "menus2"
158 static GtkWidget *filer_menu; /* The popup filer menu */
159 static GtkWidget *filer_file_item; /* The File '' label */
160 static GtkWidget *filer_file_menu; /* The File '' menu */
161 static GtkWidget *file_shift_item; /* Shift Open label */
162 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
163 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
164 static GtkWidget *filer_reverse_menu; /* The Reversed item */
165 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
166 static GtkWidget *filer_new_window; /* The New Window item */
167 static GtkWidget *filer_new_menu; /* The New submenu */
168 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
170 #undef N_
171 #define N_(x) x
173 static GtkItemFactoryEntry filer_menu_def[] = {
174 {N_("Display"), NULL, NULL, 0, "<Branch>"},
175 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
176 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
177 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
178 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
179 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
180 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
181 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
182 {">", NULL, NULL, 0, "<Separator>"},
183 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
184 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
185 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
186 {">", NULL, NULL, 0, "<Separator>"},
187 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
188 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
189 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
190 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
191 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
192 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
193 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
194 {">", NULL, NULL, 0, "<Separator>"},
195 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
196 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
197 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
198 {N_("File"), NULL, NULL, 0, "<Branch>"},
199 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
200 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
201 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
202 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
203 {">", NULL, NULL, 0, "<Separator>"},
204 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
205 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
206 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
207 {">", NULL, NULL, 0, "<Separator>"},
208 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
209 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
210 {">" N_("Properties"), NULL, file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
211 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
212 {">" N_("Set type..."), NULL, file_op, FILE_SET_TYPE, NULL},
213 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
214 {">", NULL, NULL, 0, "<Separator>"},
215 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
216 {N_("Select"), NULL, NULL, 0, "<Branch>"},
217 {">" N_("Select All"), NULL, select_all, 0, NULL},
218 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
219 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
220 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
221 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
222 {N_("New"), NULL, NULL, 0, "<Branch>"},
223 {">" N_("Directory"), NULL, new_directory, 0, NULL},
224 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
225 {N_("Window"), NULL, NULL, 0, "<Branch>"},
226 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
227 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
228 {">" N_("New Window"), NULL, new_window, 0, NULL},
229 {">" N_("Home Directory"), NULL, home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
230 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
231 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
232 {">" N_("Resize Window"), NULL, resize, 0, NULL},
233 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
235 {">" N_("Close Window"), NULL, close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
236 {">", NULL, NULL, 0, "<Separator>"},
237 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
238 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
239 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
240 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
241 {N_("Help"), NULL, NULL, 0, "<Branch>"},
242 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
243 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
244 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
248 #define GET_MENU_ITEM(var, menu) \
249 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
251 #define GET_SMENU_ITEM(var, menu, sub) \
252 do { \
253 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
254 var = gtk_item_factory_get_widget(item_factory, tmp); \
255 g_free(tmp); \
256 } while (0)
258 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
259 do { \
260 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
261 var = gtk_item_factory_get_widget(item_factory, tmp); \
262 g_free(tmp); \
263 } while (0)
265 /* Returns TRUE if the keys were installed (first call only) */
266 gboolean ensure_filer_menu(void)
268 GList *items;
269 guchar *tmp;
270 GtkWidget *item;
271 GtkItemFactory *item_factory;
273 if (!filer_keys_need_init)
274 return FALSE;
275 filer_keys_need_init = FALSE;
277 item_factory = menu_create(filer_menu_def,
278 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
279 "<filer>", filer_keys);
281 GET_MENU_ITEM(filer_menu, "filer");
282 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
283 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
284 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
285 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
286 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
287 "Show Thumbnails");
289 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
290 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
291 filer_follow_sym = GTK_BIN(item)->child;
293 /* File '' label... */
294 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
295 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
296 g_list_free(items);
298 /* Shift Open... label */
299 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
300 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
301 g_list_free(items);
303 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
304 filer_new_window = GTK_BIN(item)->child;
306 g_signal_connect(filer_menu, "unmap_event",
307 G_CALLBACK(menu_closed), NULL);
308 g_signal_connect(filer_file_menu, "unmap_event",
309 G_CALLBACK(menu_closed), NULL);
311 g_signal_connect(filer_keys, "accel_changed",
312 G_CALLBACK(save_menus), NULL);
314 return TRUE;
317 void menu_init(void)
319 char *menurc;
321 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
322 if (menurc)
324 gtk_accel_map_load(menurc);
325 g_free(menurc);
328 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
329 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
330 option_add_saver(save_menus);
332 option_register_widget("menu-set-keys", set_keys_button);
334 filer_keys = gtk_accel_group_new();
337 /* Name is in the form "<panel>" */
338 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
339 const gchar *name, GtkAccelGroup *keys)
341 GtkItemFactory *item_factory;
342 GtkItemFactoryEntry *translated;
344 if (!keys)
346 keys = gtk_accel_group_new();
347 gtk_accel_group_lock(keys);
350 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
352 translated = translate_entries(def, n_entries);
353 gtk_item_factory_create_items(item_factory, n_entries,
354 translated, NULL);
355 free_translated_entries(translated, n_entries);
357 return item_factory;
360 /* Prevent the user from setting a short-cut on this item */
361 static void menuitem_no_shortcuts(GtkWidget *item)
363 /* XXX */
364 #if 0
365 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
367 _gtk_widget_set_accel_path(item, NULL, NULL);
368 null_g_free(&menuitem->accel_path);
369 #endif
372 /* Shade items that only work on single files */
373 static void shade_file_menu_items(gboolean shaded)
375 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
376 menu_set_items_shaded(filer_file_menu, shaded, 5, 2);
377 menu_set_items_shaded(filer_file_menu, shaded, 9, 2);
380 /* 'data' is an array of three ints:
381 * [ pointer_x, pointer_y, item_under_pointer ]
383 void position_menu(GtkMenu *menu, gint *x, gint *y,
384 gboolean *push_in, gpointer data)
386 int *pos = (int *) data;
387 GtkRequisition requisition;
388 GList *items, *next;
389 int y_shift = 0;
390 int item = pos[2];
392 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
394 while (item >= 0 && next)
396 int h = ((GtkWidget *) next->data)->requisition.height;
398 if (item > 0)
399 y_shift += h;
400 else
401 y_shift += h / 2;
403 next = next->next;
404 item--;
407 g_list_free(items);
409 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
411 *x = pos[0] - (requisition.width * 7 / 8);
412 *y = pos[1] - y_shift;
414 *x = CLAMP(*x, 0, screen_width - requisition.width);
415 *y = CLAMP(*y, 0, screen_height - requisition.height);
417 *push_in = FALSE;
420 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
421 MenuIconStyle style)
423 GtkWidget *item;
425 if (ditem->image && style != MIS_NONE)
427 GdkPixbuf *pixbuf;
429 switch (style)
431 case MIS_LARGE:
432 pixbuf = ditem->image->pixbuf;
433 break;
434 default:
435 if (!ditem->image->sm_pixbuf)
436 pixmap_make_small(ditem->image);
437 pixbuf = ditem->image->sm_pixbuf;
438 break;
441 item = gtk_image_menu_item_new_with_label(label);
442 /* TODO: Find a way to allow short-cuts */
443 menuitem_no_shortcuts(item);
445 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
446 gtk_image_new_from_pixbuf(pixbuf));
447 gtk_widget_show_all(item);
449 else
450 item = gtk_menu_item_new_with_label(label);
452 return item;
455 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
456 MenuIconStyle style, CallbackFn func,
457 gboolean separator, gboolean strip_ext,
458 gboolean recurse)
460 GList *widgets = NULL;
461 DirItem *ditem;
462 int i;
463 GtkWidget *item;
464 char *dname = NULL;
465 GPtrArray *names;
467 dname = pathdup(dir_name);
469 names = list_dir(dname);
470 if (!names)
471 goto out;
473 if (separator)
475 item = gtk_menu_item_new();
476 widgets = g_list_append(widgets, item);
477 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
480 for (i = 0; i < names->len; i++)
482 char *leaf = names->pdata[i];
483 gchar *fname;
485 fname = g_strconcat(dname, "/", leaf, NULL);
487 /* Strip off extension, if any */
488 if (strip_ext)
490 char *dot;
491 dot = strchr(leaf, '.');
492 if (dot)
493 *dot = '\0';
496 ditem = diritem_new("");
497 diritem_restat(fname, ditem, NULL);
499 item = make_send_to_item(ditem, leaf, style);
501 g_free(leaf);
503 /* If it is a directory (but NOT an AppDir) and we are
504 * recursing then set up a sub menu.
506 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
507 !(ditem->flags & ITEM_FLAG_APPDIR))
509 GtkWidget *sub;
510 GList *new_widgets;
512 sub = gtk_menu_new();
513 new_widgets = menu_from_dir(sub, fname, style, func,
514 separator, strip_ext, FALSE);
515 g_list_free(new_widgets);
516 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
518 else
519 g_signal_connect_swapped(item, "activate",
520 G_CALLBACK(func), fname);
522 diritem_free(ditem);
524 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
525 g_signal_connect_swapped(item, "destroy",
526 G_CALLBACK(g_free), fname);
528 widgets = g_list_append(widgets, item);
531 g_ptr_array_free(names, TRUE);
532 out:
533 g_free(dname);
535 return widgets;
538 /* Scan the templates dir and create entries for the New menu */
539 static void update_new_files_menu(MenuIconStyle style)
541 static GList *widgets = NULL;
543 gchar *templ_dname = NULL;
545 if (widgets)
547 GList *next;
549 for (next = widgets; next; next = next->next)
550 gtk_widget_destroy((GtkWidget *) next->data);
552 g_list_free(widgets);
553 widgets = NULL;
556 templ_dname = choices_find_path_load("Templates", "");
557 if (templ_dname)
559 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
560 (CallbackFn) new_file_type, TRUE, TRUE,
561 FALSE);
562 g_free(templ_dname);
564 gtk_widget_show_all(filer_new_menu);
567 /* 'item' is the number of the item to appear under the pointer. */
568 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
570 int pos[3];
571 int button = 0;
572 guint32 time = 0;
574 if (event && (event->type == GDK_BUTTON_PRESS ||
575 event->type == GDK_BUTTON_RELEASE))
577 GdkEventButton *bev = (GdkEventButton *) event;
579 pos[0] = bev->x_root;
580 pos[1] = bev->y_root;
581 button = bev->button;
582 time = bev->time;
584 else if (event && event->type == GDK_KEY_PRESS)
586 GdkEventKey *kev = (GdkEventKey *) event;
588 get_pointer_xy(pos, pos + 1);
589 time = kev->time;
591 else
592 get_pointer_xy(pos, pos + 1);
594 pos[2] = item;
596 gtk_widget_show_all(menu);
597 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
598 position_menu, (gpointer) pos, button, time);
599 select_nth_item(GTK_MENU_SHELL(menu), item);
602 /* Hide the popup menu, if any */
603 void menu_popdown(void)
605 if (popup_menu)
606 gtk_menu_popdown(GTK_MENU(popup_menu));
609 static MenuIconStyle get_menu_icon_style(void)
611 MenuIconStyle mis;
612 int display;
614 mis = o_menu_iconsize.int_value;
616 switch (mis)
618 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
619 return mis;
620 default:
621 break;
624 if (mis == MIS_CURRENT && window_with_focus)
626 switch (window_with_focus->display_style)
628 case HUGE_ICONS:
629 case LARGE_ICONS:
630 return MIS_LARGE;
631 case SMALL_ICONS:
632 return MIS_SMALL;
633 default:
634 break;
638 display = o_display_size.int_value;
639 switch (display)
641 case HUGE_ICONS:
642 case LARGE_ICONS:
643 return MIS_LARGE;
644 case SMALL_ICONS:
645 return MIS_SMALL;
646 default:
647 break;
650 return MIS_SMALL;
653 /* iter->peek() is the clicked item, or NULL if none */
654 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
656 DirItem *file_item = NULL;
657 GdkModifierType state = 0;
658 int n_selected;
660 n_selected = view_count_selected(filer_window->view);
662 ensure_filer_menu();
664 updating_menu++;
666 /* Remove previous AppMenu, if any */
667 appmenu_remove();
669 window_with_focus = filer_window;
671 if (!event)
672 event = gtk_get_current_event();
674 if (event->type == GDK_BUTTON_PRESS)
675 state = ((GdkEventButton *) event)->state;
676 else if (event->type == GDK_KEY_PRESS)
677 state = ((GdkEventKey *) event)->state;
679 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
681 filer_window->temp_item_selected = TRUE;
682 view_set_selected(filer_window->view, iter, TRUE);
683 n_selected = view_count_selected(filer_window->view);
685 else
687 filer_window->temp_item_selected = FALSE;
690 /* Short-cut to the Send To menu */
691 if (state & GDK_SHIFT_MASK)
693 GList *paths;
695 updating_menu--;
697 if (n_selected == 0)
699 report_error(
700 _("You should Shift+Menu click over a file to "
701 "send it somewhere"));
702 return;
705 paths = filer_selected_items(filer_window);
707 show_send_to_menu(paths, event); /* (paths eaten) */
709 return;
713 GtkWidget *file_label, *file_menu;
714 GString *buffer;
715 DirItem *item;
717 file_label = filer_file_item;
718 file_menu = filer_file_menu;
719 gtk_check_menu_item_set_active(
720 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
721 filer_window->show_thumbs);
722 gtk_check_menu_item_set_active(
723 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
724 filer_window->show_hidden);
725 gtk_check_menu_item_set_active(
726 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
727 filer_window->sort_order != GTK_SORT_ASCENDING);
728 gtk_check_menu_item_set_active(
729 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
730 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
731 buffer = g_string_new(NULL);
733 switch (n_selected)
735 case 0:
736 g_string_assign(buffer, _("Next Click"));
737 shade_file_menu_items(FALSE);
738 break;
739 case 1:
740 item = filer_selected_item(filer_window);
741 if (!item->image)
742 dir_update_item(filer_window->directory,
743 item->leafname);
744 shade_file_menu_items(FALSE);
745 file_item = filer_selected_item(filer_window);
746 g_string_printf(buffer, _("%s '%s'"),
747 basetype_name(file_item),
748 g_utf8_validate(file_item->leafname,
749 -1, NULL)
750 ? file_item->leafname
751 : _("(bad utf-8)"));
752 if (!can_set_run_action(file_item))
753 menu_set_items_shaded(filer_file_menu,
754 TRUE, 9, 1);
755 break;
756 default:
757 shade_file_menu_items(TRUE);
758 g_string_printf(buffer, _("%d items"),
759 n_selected);
760 break;
762 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
763 g_string_free(buffer, TRUE);
765 menu_show_shift_action(file_shift_item, file_item,
766 n_selected == 0);
767 if (file_item)
768 appmenu_add(make_path(filer_window->sym_path,
769 file_item->leafname),
770 file_item, filer_file_menu);
773 update_new_files_menu(get_menu_icon_style());
775 gtk_widget_set_sensitive(filer_new_window,
776 !o_unique_filer_windows.int_value);
777 gtk_widget_set_sensitive(filer_follow_sym,
778 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
780 popup_menu = (state & GDK_CONTROL_MASK)
781 ? filer_file_menu
782 : filer_menu;
784 updating_menu--;
786 show_popup_menu(popup_menu, event,
787 popup_menu == filer_file_menu ? 9 : 1);
790 static void menu_closed(GtkWidget *widget)
792 if (window_with_focus == NULL || widget != popup_menu)
793 return; /* Close panel item chosen? */
795 popup_menu = NULL;
797 if (window_with_focus->temp_item_selected)
799 view_clear_selection(window_with_focus->view);
800 window_with_focus->temp_item_selected = FALSE;
803 appmenu_remove();
806 static void target_callback(FilerWindow *filer_window,
807 ViewIter *iter,
808 gpointer action)
810 g_return_if_fail(filer_window != NULL);
812 window_with_focus = filer_window;
814 /* Don't grab the primary selection */
815 filer_window->temp_item_selected = TRUE;
817 view_wink_item(filer_window->view, iter);
818 view_select_only(filer_window->view, iter);
819 file_op(NULL, GPOINTER_TO_INT(action), NULL);
821 view_clear_selection(filer_window->view);
822 filer_window->temp_item_selected = FALSE;
825 /* Set the text of the 'Shift Open...' menu item.
826 * If icon is NULL, reset the text and also shade it, unless 'next'.
828 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
830 guchar *shift_action = NULL;
832 if (item)
834 if (item->flags & ITEM_FLAG_MOUNT_POINT)
836 if (item->flags & ITEM_FLAG_MOUNTED)
837 shift_action = N_("Unmount");
838 else
839 shift_action = N_("Open unmounted");
841 else if (item->flags & ITEM_FLAG_SYMLINK)
842 shift_action = N_("Show Target");
843 else if (item->base_type == TYPE_DIRECTORY)
844 shift_action = N_("Look Inside");
845 else if (item->base_type == TYPE_FILE)
846 shift_action = N_("Open As Text");
848 gtk_label_set_text(GTK_LABEL(menu_item),
849 shift_action ? _(shift_action)
850 : _("Shift Open"));
851 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
854 /* Actions */
856 static void view_type(gpointer data, guint action, GtkWidget *widget)
858 ViewType view_type = (ViewType) action;
860 g_return_if_fail(window_with_focus != NULL);
862 if (view_type == VIEW_TYPE_COLLECTION)
863 display_set_layout(window_with_focus,
864 window_with_focus->display_style_wanted,
865 DETAILS_NONE, FALSE);
867 filer_set_view_type(window_with_focus, (ViewType) action);
870 static void change_size(gpointer data, guint action, GtkWidget *widget)
872 g_return_if_fail(window_with_focus != NULL);
874 display_change_size(window_with_focus, action == 1);
877 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
879 g_return_if_fail(window_with_focus != NULL);
881 if (updating_menu)
882 return;
884 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
885 display_set_layout(window_with_focus,
886 window_with_focus->display_style,
887 window_with_focus->details_type, FALSE);
888 else
889 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
890 window_with_focus->details_type, FALSE);
893 static void set_with(gpointer data, guint action, GtkWidget *widget)
895 DisplayStyle size;
897 g_return_if_fail(window_with_focus != NULL);
899 size = window_with_focus->display_style_wanted;
901 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
902 display_set_layout(window_with_focus, size, action, FALSE);
905 static void set_sort(gpointer data, guint action, GtkWidget *widget)
907 if (updating_menu)
908 return;
910 g_return_if_fail(window_with_focus != NULL);
912 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
915 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
917 GtkSortType order;
919 if (updating_menu)
920 return;
922 g_return_if_fail(window_with_focus != NULL);
924 order = window_with_focus->sort_order;
925 if (order == GTK_SORT_ASCENDING)
926 order = GTK_SORT_DESCENDING;
927 else
928 order = GTK_SORT_ASCENDING;
930 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
931 order);
934 static void hidden(gpointer data, guint action, GtkWidget *widget)
936 if (updating_menu)
937 return;
939 g_return_if_fail(window_with_focus != NULL);
941 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
944 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
946 if (updating_menu)
947 return;
949 g_return_if_fail(window_with_focus != NULL);
951 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
954 static void refresh(gpointer data, guint action, GtkWidget *widget)
956 g_return_if_fail(window_with_focus != NULL);
958 filer_refresh(window_with_focus);
961 static void delete(FilerWindow *filer_window)
963 GList *paths;
964 paths = filer_selected_items(filer_window);
965 action_delete(paths);
966 destroy_glist(&paths);
969 static void usage(FilerWindow *filer_window)
971 GList *paths;
972 paths = filer_selected_items(filer_window);
973 action_usage(paths);
974 destroy_glist(&paths);
977 static void chmod_items(FilerWindow *filer_window)
979 GList *paths;
980 paths = filer_selected_items(filer_window);
981 action_chmod(paths, FALSE, NULL);
982 destroy_glist(&paths);
985 static void set_type_items(FilerWindow *filer_window)
987 GList *paths;
988 paths = filer_selected_items(filer_window);
989 action_settype(paths, FALSE, NULL);
990 destroy_glist(&paths);
993 static void find(FilerWindow *filer_window)
995 GList *paths;
996 paths = filer_selected_items(filer_window);
997 action_find(paths);
998 destroy_glist(&paths);
1001 /* This creates a new savebox widget, and allows the user to pick a new path
1002 * for the file.
1003 * Once the new path has been picked, the callback will be called with
1004 * both the current and new paths.
1005 * NOTE: This function unrefs 'image'!
1007 static void savebox_show(const gchar *action, const gchar *path,
1008 MaskedPixmap *image, SaveCb callback,
1009 GdkDragAction dnd_action)
1011 GtkWidget *savebox = NULL;
1012 GtkWidget *check_relative = NULL;
1014 g_return_if_fail(image != NULL);
1016 savebox = gtk_savebox_new(action);
1017 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1019 if (callback == link_cb)
1021 check_relative = gtk_check_button_new_with_mnemonic(
1022 _("_Relative link"));
1023 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1024 TRUE);
1026 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1027 gtk_tooltips_set_tip(tooltips, check_relative,
1028 _("If on, the symlink will store the path from the "
1029 "symlink to the target file. Use this if the symlink "
1030 "and the target will be moved together.\n"
1031 "If off, the path from the root directory is stored - "
1032 "use this if the symlink may move but the target will "
1033 "stay put."), NULL);
1034 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1035 check_relative, FALSE, TRUE, 0);
1036 gtk_widget_show(check_relative);
1039 g_signal_connect(savebox, "save_to_file",
1040 G_CALLBACK(save_to_file), NULL);
1042 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1043 g_strdup(path), g_free);
1044 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1045 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1047 gtk_window_set_title(GTK_WINDOW(savebox), action);
1049 if (g_utf8_validate(path, -1, NULL))
1050 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1051 else
1053 gchar *u8, *dir;
1054 dir = g_path_get_dirname(path);
1055 u8 = to_utf8(g_basename(path));
1056 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1057 make_path(dir, u8));
1058 g_free(u8);
1059 g_free(dir);
1061 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1062 g_object_unref(image);
1064 gtk_widget_show(savebox);
1067 static gint save_to_file(GObject *savebox,
1068 const gchar *pathname, gpointer data)
1070 SaveCb callback;
1071 const gchar *current_path;
1073 callback = g_object_get_data(savebox, "action_callback");
1074 current_path = g_object_get_data(savebox, "current_path");
1076 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1077 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1079 return callback(savebox, current_path, pathname)
1080 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1083 static gboolean copy_cb(GObject *savebox,
1084 const gchar *current, const gchar *new)
1086 return action_with_leaf(action_copy, current, new);
1089 static gboolean action_with_leaf(ActionFn action,
1090 const gchar *current, const gchar *new)
1092 const char *leaf;
1093 char *new_dir;
1094 GList *local_paths;
1096 if (new[0] != '/')
1098 report_error(_("New pathname is not absolute"));
1099 return FALSE;
1102 if (new[strlen(new) - 1] == '/')
1104 new_dir = g_strdup(new);
1105 leaf = NULL;
1107 else
1109 const gchar *slash;
1111 slash = strrchr(new, '/');
1112 new_dir = g_strndup(new, slash - new);
1113 leaf = slash + 1;
1116 local_paths = g_list_append(NULL, (gchar *) current);
1117 action(local_paths, new_dir, leaf, -1);
1118 g_list_free(local_paths);
1120 g_free(new_dir);
1122 return TRUE;
1125 /* Open a savebox to act on the selected file.
1126 * Call 'callback' later to perform the operation.
1128 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1129 const gchar *action, SaveCb callback,
1130 GdkDragAction dnd_action)
1132 g_object_ref(image);
1133 savebox_show(action, path, image, callback, dnd_action);
1136 static gboolean rename_cb(GObject *savebox,
1137 const gchar *current, const gchar *new)
1139 return action_with_leaf(action_move, current, new);
1142 static gboolean link_cb(GObject *savebox,
1143 const gchar *initial, const gchar *path)
1145 GtkToggleButton *check_relative;
1146 struct stat info;
1147 int err;
1148 gchar *link_path;
1150 check_relative = g_object_get_data(savebox, "check_relative");
1152 if (gtk_toggle_button_get_active(check_relative))
1153 link_path = get_relative_path(path, initial);
1154 else
1155 link_path = g_strdup(initial);
1157 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1159 GtkWidget *box, *button;
1160 gint ans;
1162 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1163 GTK_BUTTONS_CANCEL,
1164 _("Symlink from '%s' already exists. "
1165 "Replace it with a link to '%s'?"),
1166 path, link_path);
1168 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1170 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1171 gtk_widget_show(button);
1172 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1173 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1174 button, GTK_RESPONSE_OK);
1175 gtk_dialog_set_default_response(GTK_DIALOG(box),
1176 GTK_RESPONSE_OK);
1178 ans = gtk_dialog_run(GTK_DIALOG(box));
1179 gtk_widget_destroy(box);
1181 if (ans != GTK_RESPONSE_OK)
1183 g_free(link_path);
1184 return FALSE;
1187 unlink(path);
1190 err = symlink(link_path, path);
1191 g_free(link_path);
1193 if (err)
1195 report_error("symlink: %s", g_strerror(errno));
1196 return FALSE;
1199 dir_check_this(path);
1201 return TRUE;
1204 static void run_action(DirItem *item)
1206 if (can_set_run_action(item))
1207 type_set_handler_dialog(item->mime_type);
1208 else
1209 report_error(
1210 _("You can only set the run action for a "
1211 "regular file"));
1214 void open_home(gpointer data, guint action, GtkWidget *widget)
1216 filer_opendir(home_dir, NULL, NULL);
1219 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1221 gchar *path;
1223 path = g_strconcat(filer_window->sym_path,
1224 "/", item->leafname, "#", NULL);
1226 filer_change_to(filer_window, path, NULL);
1227 g_free(path);
1230 static void select_all(gpointer data, guint action, GtkWidget *widget)
1232 g_return_if_fail(window_with_focus != NULL);
1234 window_with_focus->temp_item_selected = FALSE;
1235 view_select_all(window_with_focus->view);
1238 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1240 g_return_if_fail(window_with_focus != NULL);
1242 window_with_focus->temp_item_selected = FALSE;
1243 view_clear_selection(window_with_focus->view);
1246 static gboolean invert_cb(ViewIter *iter, gpointer data)
1248 return !view_get_selected((ViewIface *) data, iter);
1251 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1253 g_return_if_fail(window_with_focus != NULL);
1255 window_with_focus->temp_item_selected = FALSE;
1257 view_select_if(window_with_focus->view, invert_cb,
1258 window_with_focus->view);
1261 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1263 GtkWidget *win;
1265 win = options_show();
1267 if (win)
1269 number_of_windows++;
1270 g_signal_connect(win, "destroy",
1271 G_CALLBACK(one_less_window), NULL);
1275 static gboolean new_directory_cb(GObject *savebox,
1276 const gchar *initial, const gchar *path)
1278 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1280 report_error("mkdir: %s", g_strerror(errno));
1281 return FALSE;
1284 dir_check_this(path);
1286 if (filer_exists(window_with_focus))
1288 guchar *leaf;
1289 leaf = strrchr(path, '/');
1290 if (leaf)
1291 display_set_autoselect(window_with_focus, leaf + 1);
1294 return TRUE;
1297 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1299 g_return_if_fail(window_with_focus != NULL);
1301 savebox_show(_("Create"),
1302 make_path(window_with_focus->sym_path, _("NewDir")),
1303 type_to_icon(inode_directory), new_directory_cb,
1304 GDK_ACTION_COPY);
1307 static gboolean new_file_cb(GObject *savebox,
1308 const gchar *initial, const gchar *path)
1310 int fd;
1312 fd = open(path, O_CREAT | O_EXCL, 0666);
1314 if (fd == -1)
1316 report_error(_("Error creating '%s': %s"),
1317 path, g_strerror(errno));
1318 return FALSE;
1321 if (close(fd))
1322 report_error(_("Error creating '%s': %s"),
1323 path, g_strerror(errno));
1325 dir_check_this(path);
1327 if (filer_exists(window_with_focus))
1329 guchar *leaf;
1330 leaf = strrchr(path, '/');
1331 if (leaf)
1332 display_set_autoselect(window_with_focus, leaf + 1);
1335 return TRUE;
1338 static void new_file(gpointer data, guint action, GtkWidget *widget)
1340 g_return_if_fail(window_with_focus != NULL);
1342 savebox_show(_("Create"),
1343 make_path(window_with_focus->sym_path, _("NewFile")),
1344 type_to_icon(text_plain),
1345 new_file_cb, GDK_ACTION_COPY);
1348 static gboolean new_file_type_cb(GObject *savebox,
1349 const gchar *initial, const gchar *path)
1351 const gchar *oleaf, *leaf;
1352 gchar *templ, *templ_dname, *dest;
1353 GList *paths;
1355 /* We can work out the template path from the initial name */
1356 oleaf = g_basename(initial);
1357 templ_dname = choices_find_path_load("Templates", "");
1358 if (!templ_dname)
1360 report_error(
1361 _("Error creating file: could not find the template for %s"),
1362 oleaf);
1363 return FALSE;
1366 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1367 g_free(templ_dname);
1369 dest = g_path_get_dirname(path);
1370 leaf = g_basename(path);
1371 paths = g_list_append(NULL, templ);
1373 action_copy(paths, dest, leaf, TRUE);
1375 g_list_free(paths);
1376 g_free(dest);
1377 g_free(templ);
1379 if (filer_exists(window_with_focus))
1380 display_set_autoselect(window_with_focus, leaf);
1382 return TRUE;
1385 static void do_send_to(gchar *templ)
1387 g_return_if_fail(send_to_paths != NULL);
1389 run_with_files(templ, send_to_paths);
1392 static void new_file_type(gchar *templ)
1394 const gchar *leaf;
1395 MIME_type *type;
1397 g_return_if_fail(window_with_focus != NULL);
1399 leaf = g_basename(templ);
1400 type = type_get_type(templ);
1402 savebox_show(_("Create"),
1403 make_path(window_with_focus->sym_path, leaf),
1404 type_to_icon(type),
1405 new_file_type_cb, GDK_ACTION_COPY);
1408 static void customise_send_to(gpointer data)
1410 GPtrArray *path;
1411 guchar *save;
1412 GString *dirs;
1413 int i;
1415 dirs = g_string_new(NULL);
1417 path = choices_list_dirs("");
1418 for (i = 0; i < path->len; i++)
1420 guchar *old = (guchar *) path->pdata[i];
1422 g_string_append(dirs, old);
1423 g_string_append(dirs, "SendTo\n");
1425 choices_free_list(path);
1427 save = choices_find_path_save("", "SendTo", TRUE);
1428 if (save)
1429 mkdir(save, 0777);
1431 info_message(
1432 _("The `Send To' menu provides a quick way to send some files "
1433 "to an application. The applications listed are those in "
1434 "the following directories:\n\n%s\n%s\n"
1435 "The `Send To' menu may be opened by Shift+Menu clicking "
1436 "over a file.\n\n"
1437 "Advanced use:\n"
1438 "You can also create subdirectories called "
1439 "`.text_html', `.text', etc which will only be "
1440 "shown for files of that type. `.group' is shown "
1441 "only when multiple files are selected."),
1442 dirs->str,
1443 save ? _("I'll show you your SendTo directory now; you should "
1444 "symlink (Ctrl+Shift drag) any applications you want "
1445 "into it.")
1446 : _("Your CHOICESPATH variable setting prevents "
1447 "customisations - sorry."));
1449 g_string_free(dirs, TRUE);
1451 if (save)
1452 filer_opendir(save, NULL, NULL);
1455 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1456 * to the menu.
1458 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1460 gchar *searchdir;
1461 GPtrArray *paths;
1462 int i;
1464 if (subtype)
1465 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1466 else if (type)
1467 searchdir = g_strdup_printf("SendTo/.%s", type);
1468 else
1469 searchdir = g_strdup("SendTo");
1471 paths = choices_list_dirs(searchdir);
1472 g_free(searchdir);
1474 for (i = 0; i < paths->len; i++)
1476 GList *widgets = NULL;
1477 guchar *dir = (guchar *) paths->pdata[i];
1479 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1480 (CallbackFn) do_send_to,
1481 FALSE, FALSE, TRUE);
1483 if (widgets)
1484 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1485 gtk_menu_item_new());
1487 g_list_free(widgets); /* TODO: Get rid of this */
1490 choices_free_list(paths);
1493 /* Scan the SendTo dir and create and show the Send To menu.
1494 * The 'paths' list and every path in it is claimed, and will be
1495 * freed later -- don't free it yourself!
1497 static void show_send_to_menu(GList *paths, GdkEvent *event)
1499 GtkWidget *menu, *item;
1501 menu = gtk_menu_new();
1503 if (g_list_length(paths) == 1)
1505 DirItem *item;
1507 item = diritem_new("");
1508 diritem_restat(paths->data, item, NULL);
1510 add_sendto(menu,
1511 item->mime_type->media_type,
1512 item->mime_type->subtype);
1514 add_sendto(menu, item->mime_type->media_type, NULL);
1516 diritem_free(item);
1518 else
1519 add_sendto(menu, "group", NULL);
1521 add_sendto(menu, NULL, NULL);
1523 item = gtk_menu_item_new_with_label(_("Customise"));
1524 g_signal_connect_swapped(item, "activate",
1525 G_CALLBACK(customise_send_to), NULL);
1526 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1528 if (send_to_paths)
1529 destroy_glist(&send_to_paths);
1531 send_to_paths = paths;
1533 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1535 popup_menu = menu;
1536 show_popup_menu(menu, event, 0);
1539 static void send_to(FilerWindow *filer_window)
1541 GList *paths;
1542 GdkEvent *event;
1544 paths = filer_selected_items(filer_window);
1545 event = gtk_get_current_event();
1547 /* Eats paths */
1548 show_send_to_menu(paths, event);
1550 gdk_event_free(event);
1553 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1555 const char *argv[] = {"sh", "-c", NULL, NULL};
1556 gboolean close = action;
1558 argv[2] = o_menu_xterm.value;
1560 g_return_if_fail(window_with_focus != NULL);
1562 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1563 gtk_widget_destroy(window_with_focus->window);
1566 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1568 g_return_if_fail(window_with_focus != NULL);
1570 filer_change_to(window_with_focus, home_dir, NULL);
1573 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1575 g_return_if_fail(window_with_focus != NULL);
1577 bookmarks_show_menu(window_with_focus);
1580 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1582 g_return_if_fail(window_with_focus != NULL);
1584 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1585 filer_change_to(window_with_focus,
1586 window_with_focus->real_path, NULL);
1587 else
1588 delayed_error(_("This is already the canonical name "
1589 "for this directory."));
1592 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1594 g_return_if_fail(window_with_focus != NULL);
1596 filer_open_parent(window_with_focus);
1599 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1601 g_return_if_fail(window_with_focus != NULL);
1603 change_to_parent(window_with_focus);
1606 static void resize(gpointer data, guint action, GtkWidget *widget)
1608 g_return_if_fail(window_with_focus != NULL);
1610 view_autosize(window_with_focus->view);
1613 static void new_window(gpointer data, guint action, GtkWidget *widget)
1615 g_return_if_fail(window_with_focus != NULL);
1617 if (o_unique_filer_windows.int_value)
1619 report_error(_("You can't open a second view onto "
1620 "this directory because the `Unique Windows' option "
1621 "is turned on in the Options window."));
1623 else
1624 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1627 static void close_window(gpointer data, guint action, GtkWidget *widget)
1629 g_return_if_fail(window_with_focus != NULL);
1631 if (!filer_window_delete(window_with_focus->window, NULL,
1632 window_with_focus))
1633 gtk_widget_destroy(window_with_focus->window);
1636 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1638 MiniType type = (MiniType) action;
1640 g_return_if_fail(window_with_focus != NULL);
1642 /* Item needs to remain selected... */
1643 if (type == MINI_SHELL)
1644 window_with_focus->temp_item_selected = FALSE;
1646 minibuffer_show(window_with_focus, type);
1649 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1651 if (action == HELP_ABOUT)
1652 infobox_new(app_dir);
1653 else if (action == HELP_DIR)
1654 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1655 else if (action == HELP_MANUAL)
1657 gchar *manual = NULL;
1659 if (current_lang)
1661 manual = g_strconcat(app_dir, "/Help/Manual-",
1662 current_lang, ".html", NULL);
1663 if (access(manual, F_OK))
1664 null_g_free(&manual);
1667 if (!manual)
1668 manual = g_strconcat(app_dir,
1669 "/Help/Manual.html", NULL);
1671 run_by_path(manual);
1673 g_free(manual);
1675 else
1676 g_warning("Unknown help action %d\n", action);
1679 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1680 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1682 GList *items, *item;
1684 items = gtk_container_get_children(GTK_CONTAINER(menu));
1686 item = g_list_nth(items, from);
1687 while (item && n--)
1689 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1690 item = item->next;
1692 g_list_free(items);
1695 static void save_menus(void)
1697 char *menurc;
1699 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1700 if (menurc)
1702 gtk_accel_map_save(menurc);
1703 g_free(menurc);
1707 static void select_nth_item(GtkMenuShell *shell, int n)
1709 GList *items;
1710 GtkWidget *item;
1712 items = gtk_container_get_children(GTK_CONTAINER(shell));
1713 item = g_list_nth_data(items, n);
1715 g_return_if_fail(item != NULL);
1717 g_list_free(items);
1719 gtk_menu_shell_select_item(shell, item);
1722 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1724 DirItem *item;
1725 const guchar *path;
1726 int n_selected;
1727 ViewIter iter;
1729 g_return_if_fail(window_with_focus != NULL);
1731 n_selected = view_count_selected(window_with_focus->view);
1733 if (n_selected < 1)
1735 const char *prompt;
1737 switch (action)
1739 case FILE_COPY_ITEM:
1740 prompt = _("Copy ... ?");
1741 break;
1742 case FILE_RENAME_ITEM:
1743 prompt = _("Rename ... ?");
1744 break;
1745 case FILE_LINK_ITEM:
1746 prompt = _("Symlink ... ?");
1747 break;
1748 case FILE_OPEN_FILE:
1749 prompt = _("Shift Open ... ?");
1750 break;
1751 case FILE_PROPERTIES:
1752 prompt = _("Properties of ... ?");
1753 break;
1754 case FILE_SET_TYPE:
1755 prompt = _("Set type of ... ?");
1756 break;
1757 case FILE_RUN_ACTION:
1758 prompt = _("Set run action for ... ?");
1759 break;
1760 case FILE_SET_ICON:
1761 prompt = _("Set icon for ... ?");
1762 break;
1763 case FILE_SEND_TO:
1764 prompt = _("Send ... to ... ?");
1765 break;
1766 case FILE_DELETE:
1767 prompt = _("DELETE ... ?");
1768 break;
1769 case FILE_USAGE:
1770 prompt = _("Count the size of ... ?");
1771 break;
1772 case FILE_CHMOD_ITEMS:
1773 prompt = _("Set permissions on ... ?");
1774 break;
1775 case FILE_FIND:
1776 prompt = _("Search inside ... ?");
1777 break;
1778 case FILE_OPEN_VFS_AVFS:
1779 prompt = _("Look inside ... ?");
1780 break;
1781 default:
1782 g_warning("Unknown action!");
1783 return;
1785 filer_target_mode(window_with_focus, target_callback,
1786 GINT_TO_POINTER(action), prompt);
1787 return;
1790 switch (action)
1792 case FILE_SEND_TO:
1793 send_to(window_with_focus);
1794 return;
1795 case FILE_DELETE:
1796 delete(window_with_focus);
1797 return;
1798 case FILE_USAGE:
1799 usage(window_with_focus);
1800 return;
1801 case FILE_CHMOD_ITEMS:
1802 chmod_items(window_with_focus);
1803 return;
1804 case FILE_SET_TYPE:
1805 set_type_items(window_with_focus);
1806 return;
1807 case FILE_FIND:
1808 find(window_with_focus);
1809 return;
1810 case FILE_PROPERTIES:
1812 GList *items;
1814 items = filer_selected_items(window_with_focus);
1815 infobox_show_list(items);
1816 destroy_glist(&items);
1817 return;
1819 default:
1820 break;
1823 /* All the following actions require exactly one file selected */
1825 if (n_selected > 1)
1827 report_error(_("You cannot do this to more than "
1828 "one item at a time"));
1829 return;
1832 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1834 item = iter.next(&iter);
1835 g_return_if_fail(item != NULL);
1836 /* iter may be passed to filer_openitem... */
1838 if (!item->image)
1839 item = dir_update_item(window_with_focus->directory,
1840 item->leafname);
1842 if (!item)
1844 report_error(_("Item no longer exists!"));
1845 return;
1848 path = make_path(window_with_focus->sym_path, item->leafname);
1850 switch (action)
1852 case FILE_COPY_ITEM:
1853 src_dest_action_item(path, item->image,
1854 _("Copy"), copy_cb,
1855 GDK_ACTION_COPY);
1856 break;
1857 case FILE_RENAME_ITEM:
1858 src_dest_action_item(path, item->image,
1859 _("Rename"), rename_cb,
1860 GDK_ACTION_MOVE);
1861 break;
1862 case FILE_LINK_ITEM:
1863 src_dest_action_item(path, item->image,
1864 _("Symlink"), link_cb,
1865 GDK_ACTION_LINK);
1866 break;
1867 case FILE_OPEN_FILE:
1868 filer_openitem(window_with_focus, &iter,
1869 OPEN_SAME_WINDOW | OPEN_SHIFT);
1870 break;
1871 case FILE_RUN_ACTION:
1872 run_action(item);
1873 break;
1874 case FILE_SET_ICON:
1875 icon_set_handler_dialog(item, path);
1876 break;
1877 case FILE_OPEN_VFS_AVFS:
1878 open_vfs_avfs(window_with_focus, item);
1879 break;
1880 default:
1881 g_warning("Unknown action!");
1882 return;
1886 static void show_key_help(GtkWidget *button, gpointer data)
1888 gboolean can_change_accels;
1890 g_object_get(G_OBJECT(gtk_settings_get_default()),
1891 "gtk-can-change-accels", &can_change_accels,
1892 NULL);
1894 if (!can_change_accels)
1896 info_message(_("User-definable shortcuts are disabled by "
1897 "default in Gtk2, and you have not enabled "
1898 "them. You can turn this feature on by:\n\n"
1899 "1) using an XSettings manager, such as ROX-Session "
1900 "or gnome-settings-daemon, or\n\n"
1901 "2) adding this line to ~/.gtkrc-2.0:\n"
1902 "\tgtk-can-change-accels = 1\n"
1903 "\t(this only works if NOT using XSETTINGS)"));
1904 return;
1907 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1908 "- Open the menu over a filer window,\n"
1909 "- Move the pointer over the item you want to use,\n"
1910 "- Press the key you want attached to it.\n\n"
1911 "The key will appear next to the menu item and you can just press "
1912 "that key without opening the menu in future."));
1915 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1917 GtkWidget *button, *align;
1919 g_return_val_if_fail(option == NULL, NULL);
1921 align = gtk_alignment_new(0, 0.5, 0, 0);
1922 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1923 gtk_container_add(GTK_CONTAINER(align), button);
1924 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1926 return g_list_append(NULL, align);