r2943: Allow ejecting mounted media.
[rox-filer.git] / ROX-Filer / src / menu.c
blob960b679dcad0fc34a40353920034c7bf708bdb45
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_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 FILE_EJECT,
80 FILE_FORMAT,
81 FILE_FREE,
82 } FileOp;
84 typedef enum menu_icon_style {
85 MIS_NONE, MIS_SMALL, MIS_LARGE,
86 MIS_HUGE_UNUSED,
87 MIS_CURRENT, /* As per current filer window */
88 MIS_DEFAULT
89 } MenuIconStyle;
91 typedef void (*ActionFn)(GList *paths,
92 const char *dest_dir, const char *leaf, int quiet);
93 typedef void MenuCallback(GtkWidget *widget, gpointer data);
95 typedef gboolean (*SaveCb)(GObject *savebox,
96 const gchar *current, const gchar *new);
98 GtkAccelGroup *filer_keys = NULL;
99 static gboolean filer_keys_need_init = TRUE;
101 static GtkWidget *popup_menu = NULL; /* Currently open menu */
103 static gint updating_menu = 0; /* Non-zero => ignore activations */
104 static GList *send_to_paths = NULL;
106 static Option o_menu_iconsize, o_menu_xterm;
108 /* Static prototypes */
110 static void save_menus(void);
111 static void menu_closed(GtkWidget *widget);
112 static void shade_file_menu_items(gboolean shaded);
113 static void savebox_show(const gchar *action, const gchar *path,
114 MaskedPixmap *image, SaveCb callback,
115 GdkDragAction dnd_action);
116 static gint save_to_file(GObject *savebox,
117 const gchar *pathname, gpointer data);
118 static gboolean action_with_leaf(ActionFn action,
119 const gchar *current, const gchar *new);
120 static gboolean link_cb(GObject *savebox,
121 const gchar *initial, const gchar *path);
122 static void select_nth_item(GtkMenuShell *shell, int n);
123 static void new_file_type(gchar *templ);
124 static void do_send_to(gchar *templ);
125 static void show_send_to_menu(GList *paths, GdkEvent *event);
126 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
128 /* Note that for most of these callbacks none of the arguments are used. */
130 static void view_type(gpointer data, guint action, GtkWidget *widget);
132 /* (action used in these three - DetailsType) */
133 static void change_size(gpointer data, guint action, GtkWidget *widget);
134 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
135 static void set_with(gpointer data, guint action, GtkWidget *widget);
137 static void set_sort(gpointer data, guint action, GtkWidget *widget);
138 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
140 static void hidden(gpointer data, guint action, GtkWidget *widget);
141 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
142 static void refresh(gpointer data, guint action, GtkWidget *widget);
144 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
146 static void select_all(gpointer data, guint action, GtkWidget *widget);
147 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
148 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
149 static void new_directory(gpointer data, guint action, GtkWidget *widget);
150 static void new_file(gpointer data, guint action, GtkWidget *widget);
151 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
153 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
154 static void open_parent(gpointer data, guint action, GtkWidget *widget);
155 static void home_directory(gpointer data, guint action, GtkWidget *widget);
156 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
157 static void new_window(gpointer data, guint action, GtkWidget *widget);
158 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
159 static void close_window(gpointer data, guint action, GtkWidget *widget);
160 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
162 /* (action used in this - MiniType) */
163 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
164 static void resize(gpointer data, guint action, GtkWidget *widget);
166 #define MENUS_NAME "menus2"
168 static GtkWidget *filer_menu; /* The popup filer menu */
169 static GtkWidget *filer_file_item; /* The File '' label */
170 static GtkWidget *filer_file_menu; /* The File '' menu */
171 static GtkWidget *file_shift_item; /* Shift Open label */
172 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
173 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
174 static GtkWidget *filer_reverse_menu; /* The Reversed item */
175 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
176 static GtkWidget *filer_new_window; /* The New Window item */
177 static GtkWidget *filer_new_menu; /* The New submenu */
178 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
179 static GtkWidget *filer_file_eject; /* File => Eject item */
180 static GtkWidget *filer_file_format; /* File => Format item */
181 static GtkWidget *filer_file_free; /* File => Free item */
183 #undef N_
184 #define N_(x) x
186 static GtkItemFactoryEntry filer_menu_def[] = {
187 {N_("Display"), NULL, NULL, 0, "<Branch>"},
188 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
189 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
190 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
191 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
192 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
193 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
194 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
195 {">", NULL, NULL, 0, "<Separator>"},
196 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
197 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
198 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
199 {">", NULL, NULL, 0, "<Separator>"},
200 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
201 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
202 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
203 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
204 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
205 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
206 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
207 {">", NULL, NULL, 0, "<Separator>"},
208 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
209 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
210 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
211 {N_("File"), NULL, NULL, 0, "<Branch>"},
212 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
213 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
214 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
215 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
216 {">", NULL, NULL, 0, "<Separator>"},
217 {">" N_("Help"), NULL, file_op, FILE_HELP, "<StockItem>", GTK_STOCK_HELP},
218 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
219 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
220 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
221 {">", NULL, NULL, 0, "<Separator>"},
222 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
223 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
224 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, "<StockItem>", GTK_STOCK_DIALOG_INFO},
225 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
226 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
227 {">", NULL, NULL, 0, "<Separator>"},
228 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
229 {">", NULL, NULL, 0, "<Separator>"},
230 {">" N_("Eject"), NULL, file_op, FILE_EJECT, NULL},
231 {">" N_("Format"), NULL, file_op, FILE_FORMAT, NULL},
232 {">" N_("Free"), NULL, file_op, FILE_FREE, NULL},
233 {N_("Select"), NULL, NULL, 0, "<Branch>"},
234 {">" N_("Select All"), NULL, select_all, 0, NULL},
235 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
236 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
237 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
238 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
239 {N_("New"), NULL, NULL, 0, "<Branch>"},
240 {">" N_("Directory"), NULL, new_directory, 0, NULL},
241 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
242 {N_("Window"), NULL, NULL, 0, "<Branch>"},
243 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
244 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
245 {">" N_("New Window"), NULL, new_window, 0, NULL},
246 {">" N_("Home Directory"), NULL, home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
247 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
248 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
249 {">" N_("Resize Window"), NULL, resize, 0, NULL},
250 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
252 {">" N_("Close Window"), NULL, close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
253 {">", NULL, NULL, 0, "<Separator>"},
254 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
255 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
256 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
257 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
258 {N_("Help"), NULL, NULL, 0, "<Branch>"},
259 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
260 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
261 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
265 #define GET_MENU_ITEM(var, menu) \
266 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
268 #define GET_SMENU_ITEM(var, menu, sub) \
269 do { \
270 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
271 var = gtk_item_factory_get_widget(item_factory, tmp); \
272 g_free(tmp); \
273 } while (0)
275 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
276 do { \
277 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
278 var = gtk_item_factory_get_widget(item_factory, tmp); \
279 g_free(tmp); \
280 } while (0)
282 /* Returns TRUE if the keys were installed (first call only) */
283 gboolean ensure_filer_menu(void)
285 GList *items;
286 guchar *tmp;
287 GtkWidget *item;
288 GtkItemFactory *item_factory;
290 if (!filer_keys_need_init)
291 return FALSE;
292 filer_keys_need_init = FALSE;
294 item_factory = menu_create(filer_menu_def,
295 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
296 "<filer>", filer_keys);
298 GET_MENU_ITEM(filer_menu, "filer");
299 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
300 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
301 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
302 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
303 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
304 "Show Thumbnails");
306 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
307 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
308 filer_follow_sym = GTK_BIN(item)->child;
310 /* File '' label... */
311 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
312 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
313 g_list_free(items);
315 /* Shift Open... label */
316 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
317 file_shift_item = GTK_BIN(g_list_nth(items, 6)->data)->child;
318 g_list_free(items);
320 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
321 filer_new_window = GTK_BIN(item)->child;
323 GET_SSMENU_ITEM(filer_file_eject, "filer", "File", "Eject");
324 GET_SSMENU_ITEM(filer_file_format, "filer", "File", "Format");
325 GET_SSMENU_ITEM(filer_file_free, "filer", "File", "Free");
327 g_signal_connect(filer_menu, "unmap_event",
328 G_CALLBACK(menu_closed), NULL);
329 g_signal_connect(filer_file_menu, "unmap_event",
330 G_CALLBACK(menu_closed), NULL);
332 g_signal_connect(filer_keys, "accel_changed",
333 G_CALLBACK(save_menus), NULL);
335 return TRUE;
338 void menu_init(void)
340 char *menurc;
342 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
343 if (menurc)
345 gtk_accel_map_load(menurc);
346 g_free(menurc);
349 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
350 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
351 option_add_saver(save_menus);
353 option_register_widget("menu-set-keys", set_keys_button);
355 filer_keys = gtk_accel_group_new();
358 /* Name is in the form "<panel>" */
359 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
360 const gchar *name, GtkAccelGroup *keys)
362 GtkItemFactory *item_factory;
363 GtkItemFactoryEntry *translated;
365 if (!keys)
367 keys = gtk_accel_group_new();
368 gtk_accel_group_lock(keys);
371 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
373 translated = translate_entries(def, n_entries);
374 gtk_item_factory_create_items(item_factory, n_entries,
375 translated, NULL);
376 free_translated_entries(translated, n_entries);
378 return item_factory;
381 /* Prevent the user from setting a short-cut on this item */
382 static void menuitem_no_shortcuts(GtkWidget *item)
384 /* XXX */
385 #if 0
386 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
388 _gtk_widget_set_accel_path(item, NULL, NULL);
389 null_g_free(&menuitem->accel_path);
390 #endif
393 /* Shade items that only work on single files */
394 static void shade_file_menu_items(gboolean shaded)
396 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
397 menu_set_items_shaded(filer_file_menu, shaded, 5, 3);
398 menu_set_items_shaded(filer_file_menu, shaded, 10, 2);
401 /* 'data' is an array of three ints:
402 * [ pointer_x, pointer_y, item_under_pointer ]
404 void position_menu(GtkMenu *menu, gint *x, gint *y,
405 gboolean *push_in, gpointer data)
407 int *pos = (int *) data;
408 GtkRequisition requisition;
409 GList *items, *next;
410 int y_shift = 0;
411 int item = pos[2];
413 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
415 while (item >= 0 && next)
417 int h = ((GtkWidget *) next->data)->requisition.height;
419 if (item > 0)
420 y_shift += h;
421 else
422 y_shift += h / 2;
424 next = next->next;
425 item--;
428 g_list_free(items);
430 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
432 *x = pos[0] - (requisition.width * 7 / 8);
433 *y = pos[1] - y_shift;
435 *x = CLAMP(*x, 0, screen_width - requisition.width);
436 *y = CLAMP(*y, 0, screen_height - requisition.height);
438 *push_in = FALSE;
441 /* Returns an array listing all the names in the directory 'path'.
442 * The array is unsorted.
443 * '.' and '..' are skipped.
444 * On error, the error is reported with g_warning and NULL is returned.
446 static GPtrArray *list_dir(const guchar *path)
448 GDir *dir;
449 GError *error = NULL;
450 GPtrArray *names;
451 const char *leaf;
453 dir = g_dir_open(path, 0, &error);
454 if (error)
456 g_warning("Can't list directory:\n%s", error->message);
457 g_error_free(error);
458 return NULL;
461 names = g_ptr_array_new();
463 while ((leaf = g_dir_read_name(dir))) {
464 if (leaf[0] != '.')
465 g_ptr_array_add(names, g_strdup(leaf));
468 g_dir_close(dir);
470 return names;
473 /* Used as the sort function for sorting GPtrArrays */
474 static gint strcmp2(gconstpointer a, gconstpointer b)
476 const char *aa = *(char **) a;
477 const char *bb = *(char **) b;
479 return g_strcasecmp(aa, bb);
482 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
483 MenuIconStyle style, CallbackFn func,
484 gboolean separator, gboolean strip_ext,
485 gboolean recurse)
487 GList *widgets = NULL;
488 DirItem *ditem;
489 int i;
490 GtkWidget *item;
491 char *dname = NULL;
492 GPtrArray *names;
494 dname = pathdup(dir_name);
496 names = list_dir(dname);
497 if (!names)
498 goto out;
500 if (separator)
502 item = gtk_menu_item_new();
503 widgets = g_list_append(widgets, item);
504 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
507 g_ptr_array_sort(names, strcmp2);
509 for (i = 0; i < names->len; i++)
511 char *leaf = names->pdata[i];
512 gchar *fname;
514 fname = g_strconcat(dname, "/", leaf, NULL);
516 /* Strip off extension, if any */
517 if (strip_ext)
519 char *dot;
520 dot = strchr(leaf, '.');
521 if (dot)
522 *dot = '\0';
525 ditem = diritem_new("");
526 diritem_restat(fname, ditem, NULL);
528 if (ditem->image && style != MIS_NONE)
530 GdkPixbuf *pixbuf;
532 switch (style)
534 case MIS_LARGE:
535 pixbuf = ditem->image->pixbuf;
536 break;
537 default:
538 if (!ditem->image->sm_pixbuf)
539 pixmap_make_small(ditem->image);
540 pixbuf = ditem->image->sm_pixbuf;
541 break;
544 item = gtk_image_menu_item_new_with_label(leaf);
545 /* TODO: Find a way to allow short-cuts */
546 menuitem_no_shortcuts(item);
548 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
549 gtk_image_new_from_pixbuf(pixbuf));
551 else
552 item = gtk_menu_item_new_with_label(leaf);
554 g_free(leaf);
556 /* If it is a directory (but NOT an AppDir) and we are
557 * recursing then set up a sub menu.
559 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
560 !(ditem->flags & ITEM_FLAG_APPDIR))
562 GtkWidget *sub;
563 GList *new_widgets;
565 sub = gtk_menu_new();
566 new_widgets = menu_from_dir(sub, fname, style, func,
567 separator, strip_ext, FALSE);
568 g_list_free(new_widgets);
569 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
571 else
572 g_signal_connect_swapped(item, "activate",
573 G_CALLBACK(func), fname);
575 diritem_free(ditem);
577 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
578 g_signal_connect_swapped(item, "destroy",
579 G_CALLBACK(g_free), fname);
581 widgets = g_list_append(widgets, item);
584 g_ptr_array_free(names, TRUE);
585 out:
586 g_free(dname);
588 return widgets;
591 /* Scan the templates dir and create entries for the New menu */
592 static void update_new_files_menu(MenuIconStyle style)
594 static GList *widgets = NULL;
596 gchar *templ_dname = NULL;
598 if (widgets)
600 GList *next;
602 for (next = widgets; next; next = next->next)
603 gtk_widget_destroy((GtkWidget *) next->data);
605 g_list_free(widgets);
606 widgets = NULL;
609 templ_dname = choices_find_path_load("Templates", "");
610 if (templ_dname)
612 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
613 (CallbackFn) new_file_type, TRUE, TRUE,
614 FALSE);
615 g_free(templ_dname);
617 gtk_widget_show_all(filer_new_menu);
620 /* 'item' is the number of the item to appear under the pointer. */
621 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
623 int pos[3];
624 int button = 0;
625 guint32 time = 0;
627 if (event && (event->type == GDK_BUTTON_PRESS ||
628 event->type == GDK_BUTTON_RELEASE))
630 GdkEventButton *bev = (GdkEventButton *) event;
632 pos[0] = bev->x_root;
633 pos[1] = bev->y_root;
634 button = bev->button;
635 time = bev->time;
637 else if (event && event->type == GDK_KEY_PRESS)
639 GdkEventKey *kev = (GdkEventKey *) event;
641 get_pointer_xy(pos, pos + 1);
642 time = kev->time;
644 else
645 get_pointer_xy(pos, pos + 1);
647 pos[2] = item;
649 gtk_widget_show_all(menu);
650 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
651 position_menu, (gpointer) pos, button, time);
652 select_nth_item(GTK_MENU_SHELL(menu), item);
655 /* Hide the popup menu, if any */
656 void menu_popdown(void)
658 if (popup_menu)
659 gtk_menu_popdown(GTK_MENU(popup_menu));
662 static MenuIconStyle get_menu_icon_style(void)
664 MenuIconStyle mis;
665 int display;
667 mis = o_menu_iconsize.int_value;
669 switch (mis)
671 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
672 return mis;
673 default:
674 break;
677 if (mis == MIS_CURRENT && window_with_focus)
679 switch (window_with_focus->display_style)
681 case HUGE_ICONS:
682 case LARGE_ICONS:
683 return MIS_LARGE;
684 case SMALL_ICONS:
685 return MIS_SMALL;
686 default:
687 break;
691 display = o_display_size.int_value;
692 switch (display)
694 case HUGE_ICONS:
695 case LARGE_ICONS:
696 return MIS_LARGE;
697 case SMALL_ICONS:
698 return MIS_SMALL;
699 default:
700 break;
703 return MIS_SMALL;
706 /* Set availability of the mount point specific menu items
707 (only eject implemented at present) */
708 static void set_mount_point_items(gboolean ismp, gboolean ismounted)
710 gtk_widget_set_sensitive(filer_file_eject, ismp);
711 gtk_widget_set_sensitive(filer_file_format, ismp && !ismounted);
712 gtk_widget_set_sensitive(filer_file_free, ismp && ismounted);
714 gtk_widget_set_sensitive(filer_file_format, FALSE);
715 gtk_widget_set_sensitive(filer_file_free, FALSE);
718 /* Set mount point items based on state of selected item */
719 static void set_mount_point(DirItem *item)
721 set_mount_point_items(item->flags & ITEM_FLAG_MOUNT_POINT,
722 item->flags & ITEM_FLAG_MOUNTED);
725 /* iter->peek() is the clicked item, or NULL if none */
726 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
728 DirItem *file_item = NULL;
729 GdkModifierType state = 0;
730 int n_selected;
732 n_selected = view_count_selected(filer_window->view);
734 ensure_filer_menu();
736 updating_menu++;
738 /* Remove previous AppMenu, if any */
739 appmenu_remove();
741 window_with_focus = filer_window;
743 if (!event)
744 event = gtk_get_current_event();
746 if (event->type == GDK_BUTTON_PRESS)
747 state = ((GdkEventButton *) event)->state;
748 else if (event->type == GDK_KEY_PRESS)
749 state = ((GdkEventKey *) event)->state;
751 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
753 filer_window->temp_item_selected = TRUE;
754 view_set_selected(filer_window->view, iter, TRUE);
755 n_selected = view_count_selected(filer_window->view);
757 else
759 filer_window->temp_item_selected = FALSE;
762 /* Short-cut to the Send To menu */
763 if (state & GDK_SHIFT_MASK)
765 GList *paths;
767 updating_menu--;
769 if (n_selected == 0)
771 report_error(
772 _("You should Shift+Menu click over a file to "
773 "send it somewhere"));
774 return;
777 paths = filer_selected_items(filer_window);
779 show_send_to_menu(paths, event); /* (paths eaten) */
781 return;
785 GtkWidget *file_label, *file_menu;
786 GString *buffer;
787 DirItem *item;
789 file_label = filer_file_item;
790 file_menu = filer_file_menu;
791 gtk_check_menu_item_set_active(
792 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
793 filer_window->show_thumbs);
794 gtk_check_menu_item_set_active(
795 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
796 filer_window->show_hidden);
797 gtk_check_menu_item_set_active(
798 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
799 filer_window->sort_order != GTK_SORT_ASCENDING);
800 gtk_check_menu_item_set_active(
801 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
802 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
803 buffer = g_string_new(NULL);
805 switch (n_selected)
807 case 0:
808 g_string_assign(buffer, _("Next Click"));
809 shade_file_menu_items(FALSE);
810 set_mount_point_items(FALSE, FALSE);
811 break;
812 case 1:
813 item = filer_selected_item(filer_window);
814 if (!item->image)
815 dir_update_item(filer_window->directory,
816 item->leafname);
817 shade_file_menu_items(FALSE);
818 file_item = filer_selected_item(filer_window);
819 g_string_printf(buffer, _("%s '%s'"),
820 basetype_name(file_item),
821 g_utf8_validate(file_item->leafname,
822 -1, NULL)
823 ? file_item->leafname
824 : _("(bad utf-8)"));
825 if (!can_set_run_action(file_item))
826 menu_set_items_shaded(filer_file_menu,
827 TRUE, 6, 1);
828 set_mount_point(file_item);
829 break;
830 default:
831 shade_file_menu_items(TRUE);
832 g_string_printf(buffer, _("%d items"),
833 n_selected);
834 set_mount_point_items(FALSE, FALSE);
835 break;
837 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
838 g_string_free(buffer, TRUE);
840 menu_show_shift_action(file_shift_item, file_item,
841 n_selected == 0);
842 if (file_item)
843 appmenu_add(make_path(filer_window->sym_path,
844 file_item->leafname),
845 file_item, filer_file_menu);
848 update_new_files_menu(get_menu_icon_style());
850 gtk_widget_set_sensitive(filer_new_window,
851 !o_unique_filer_windows.int_value);
852 gtk_widget_set_sensitive(filer_follow_sym,
853 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
855 popup_menu = (state & GDK_CONTROL_MASK)
856 ? filer_file_menu
857 : filer_menu;
859 updating_menu--;
861 show_popup_menu(popup_menu, event,
862 popup_menu == filer_file_menu ? 5 : 1);
865 static void menu_closed(GtkWidget *widget)
867 if (window_with_focus == NULL || widget != popup_menu)
868 return; /* Close panel item chosen? */
870 popup_menu = NULL;
872 if (window_with_focus->temp_item_selected)
874 view_clear_selection(window_with_focus->view);
875 window_with_focus->temp_item_selected = FALSE;
879 static void target_callback(FilerWindow *filer_window,
880 ViewIter *iter,
881 gpointer action)
883 g_return_if_fail(filer_window != NULL);
885 window_with_focus = filer_window;
887 /* Don't grab the primary selection */
888 filer_window->temp_item_selected = TRUE;
890 view_wink_item(filer_window->view, iter);
891 view_select_only(filer_window->view, iter);
892 file_op(NULL, GPOINTER_TO_INT(action), NULL);
894 view_clear_selection(filer_window->view);
895 filer_window->temp_item_selected = FALSE;
898 /* Set the text of the 'Shift Open...' menu item.
899 * If icon is NULL, reset the text and also shade it, unless 'next'.
901 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
903 guchar *shift_action = NULL;
905 if (item)
907 if (item->flags & ITEM_FLAG_MOUNT_POINT)
909 if (item->flags & ITEM_FLAG_MOUNTED)
910 shift_action = N_("Unmount");
911 else
912 shift_action = N_("Open unmounted");
914 else if (item->flags & ITEM_FLAG_SYMLINK)
915 shift_action = N_("Show Target");
916 else if (item->base_type == TYPE_DIRECTORY)
917 shift_action = N_("Look Inside");
918 else if (item->base_type == TYPE_FILE)
919 shift_action = N_("Open As Text");
921 gtk_label_set_text(GTK_LABEL(menu_item),
922 shift_action ? _(shift_action)
923 : _("Shift Open"));
924 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
927 /* Actions */
929 static void view_type(gpointer data, guint action, GtkWidget *widget)
931 ViewType view_type = (ViewType) action;
933 g_return_if_fail(window_with_focus != NULL);
935 if (view_type == VIEW_TYPE_COLLECTION)
936 display_set_layout(window_with_focus,
937 window_with_focus->display_style_wanted,
938 DETAILS_NONE, FALSE);
940 filer_set_view_type(window_with_focus, (ViewType) action);
943 static void change_size(gpointer data, guint action, GtkWidget *widget)
945 g_return_if_fail(window_with_focus != NULL);
947 display_change_size(window_with_focus, action == 1);
950 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
952 g_return_if_fail(window_with_focus != NULL);
954 if (updating_menu)
955 return;
957 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
958 display_set_layout(window_with_focus,
959 window_with_focus->display_style,
960 window_with_focus->details_type, FALSE);
961 else
962 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
963 window_with_focus->details_type, FALSE);
966 static void set_with(gpointer data, guint action, GtkWidget *widget)
968 DisplayStyle size;
970 g_return_if_fail(window_with_focus != NULL);
972 size = window_with_focus->display_style_wanted;
974 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
975 display_set_layout(window_with_focus, size, action, FALSE);
978 static void set_sort(gpointer data, guint action, GtkWidget *widget)
980 if (updating_menu)
981 return;
983 g_return_if_fail(window_with_focus != NULL);
985 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
988 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
990 GtkSortType order;
992 if (updating_menu)
993 return;
995 g_return_if_fail(window_with_focus != NULL);
997 order = window_with_focus->sort_order;
998 if (order == GTK_SORT_ASCENDING)
999 order = GTK_SORT_DESCENDING;
1000 else
1001 order = GTK_SORT_ASCENDING;
1003 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
1004 order);
1007 static void hidden(gpointer data, guint action, GtkWidget *widget)
1009 if (updating_menu)
1010 return;
1012 g_return_if_fail(window_with_focus != NULL);
1014 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
1017 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
1019 if (updating_menu)
1020 return;
1022 g_return_if_fail(window_with_focus != NULL);
1024 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
1027 static void refresh(gpointer data, guint action, GtkWidget *widget)
1029 g_return_if_fail(window_with_focus != NULL);
1031 filer_refresh(window_with_focus);
1034 static void delete(FilerWindow *filer_window)
1036 GList *paths;
1037 paths = filer_selected_items(filer_window);
1038 action_delete(paths);
1039 destroy_glist(&paths);
1042 static void usage(FilerWindow *filer_window)
1044 GList *paths;
1045 paths = filer_selected_items(filer_window);
1046 action_usage(paths);
1047 destroy_glist(&paths);
1050 static void chmod_items(FilerWindow *filer_window)
1052 GList *paths;
1053 paths = filer_selected_items(filer_window);
1054 action_chmod(paths, FALSE, NULL);
1055 destroy_glist(&paths);
1058 static void find(FilerWindow *filer_window)
1060 GList *paths;
1061 paths = filer_selected_items(filer_window);
1062 action_find(paths);
1063 destroy_glist(&paths);
1066 /* This creates a new savebox widget, and allows the user to pick a new path
1067 * for the file.
1068 * Once the new path has been picked, the callback will be called with
1069 * both the current and new paths.
1070 * NOTE: This function unrefs 'image'!
1072 static void savebox_show(const gchar *action, const gchar *path,
1073 MaskedPixmap *image, SaveCb callback,
1074 GdkDragAction dnd_action)
1076 GtkWidget *savebox = NULL;
1077 GtkWidget *check_relative = NULL;
1079 g_return_if_fail(image != NULL);
1081 savebox = gtk_savebox_new(action);
1082 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1084 if (callback == link_cb)
1086 check_relative = gtk_check_button_new_with_mnemonic(
1087 _("_Relative link"));
1088 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1089 TRUE);
1091 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1092 gtk_tooltips_set_tip(tooltips, check_relative,
1093 _("If on, the symlink will store the path from the "
1094 "symlink to the target file. Use this if the symlink "
1095 "and the target will be moved together.\n"
1096 "If off, the path from the root directory is stored - "
1097 "use this if the symlink may move but the target will "
1098 "stay put."), NULL);
1099 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1100 check_relative, FALSE, TRUE, 0);
1101 gtk_widget_show(check_relative);
1104 g_signal_connect(savebox, "save_to_file",
1105 G_CALLBACK(save_to_file), NULL);
1107 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1108 g_strdup(path), g_free);
1109 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1110 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1112 gtk_window_set_title(GTK_WINDOW(savebox), action);
1114 if (g_utf8_validate(path, -1, NULL))
1115 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1116 else
1118 gchar *u8, *dir;
1119 dir = g_path_get_dirname(path);
1120 u8 = to_utf8(g_basename(path));
1121 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1122 make_path(dir, u8));
1123 g_free(u8);
1124 g_free(dir);
1126 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1127 g_object_unref(image);
1129 gtk_widget_show(savebox);
1132 static gint save_to_file(GObject *savebox,
1133 const gchar *pathname, gpointer data)
1135 SaveCb callback;
1136 const gchar *current_path;
1138 callback = g_object_get_data(savebox, "action_callback");
1139 current_path = g_object_get_data(savebox, "current_path");
1141 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1142 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1144 return callback(savebox, current_path, pathname)
1145 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1148 static gboolean copy_cb(GObject *savebox,
1149 const gchar *current, const gchar *new)
1151 return action_with_leaf(action_copy, current, new);
1154 static gboolean action_with_leaf(ActionFn action,
1155 const gchar *current, const gchar *new)
1157 const char *leaf;
1158 char *new_dir;
1159 GList *local_paths;
1161 if (new[0] != '/')
1163 report_error(_("New pathname is not absolute"));
1164 return FALSE;
1167 if (new[strlen(new) - 1] == '/')
1169 new_dir = g_strdup(new);
1170 leaf = NULL;
1172 else
1174 const gchar *slash;
1176 slash = strrchr(new, '/');
1177 new_dir = g_strndup(new, slash - new);
1178 leaf = slash + 1;
1181 local_paths = g_list_append(NULL, (gchar *) current);
1182 action(local_paths, new_dir, leaf, -1);
1183 g_list_free(local_paths);
1185 g_free(new_dir);
1187 return TRUE;
1190 /* Open a savebox to act on the selected file.
1191 * Call 'callback' later to perform the operation.
1193 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1194 const gchar *action, SaveCb callback,
1195 GdkDragAction dnd_action)
1197 g_object_ref(image);
1198 savebox_show(action, path, image, callback, dnd_action);
1201 static gboolean rename_cb(GObject *savebox,
1202 const gchar *current, const gchar *new)
1204 return action_with_leaf(action_move, current, new);
1207 static gboolean link_cb(GObject *savebox,
1208 const gchar *initial, const gchar *path)
1210 GtkToggleButton *check_relative;
1211 struct stat info;
1212 int err;
1213 gchar *link_path;
1215 check_relative = g_object_get_data(savebox, "check_relative");
1217 if (gtk_toggle_button_get_active(check_relative))
1218 link_path = get_relative_path(path, initial);
1219 else
1220 link_path = g_strdup(initial);
1222 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1224 GtkWidget *box, *button;
1225 gint ans;
1227 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1228 GTK_BUTTONS_CANCEL,
1229 _("Symlink from '%s' already exists. "
1230 "Replace it with a link to '%s'?"),
1231 path, link_path);
1233 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1235 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1236 gtk_widget_show(button);
1237 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1238 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1239 button, GTK_RESPONSE_OK);
1240 gtk_dialog_set_default_response(GTK_DIALOG(box),
1241 GTK_RESPONSE_OK);
1243 ans = gtk_dialog_run(GTK_DIALOG(box));
1244 gtk_widget_destroy(box);
1246 if (ans != GTK_RESPONSE_OK)
1248 g_free(link_path);
1249 return FALSE;
1252 unlink(path);
1255 err = symlink(link_path, path);
1256 g_free(link_path);
1258 if (err)
1260 report_error("symlink: %s", g_strerror(errno));
1261 return FALSE;
1264 dir_check_this(path);
1266 return TRUE;
1269 static void run_action(DirItem *item)
1271 if (can_set_run_action(item))
1272 type_set_handler_dialog(item->mime_type);
1273 else
1274 report_error(
1275 _("You can only set the run action for a "
1276 "regular file"));
1279 void open_home(gpointer data, guint action, GtkWidget *widget)
1281 filer_opendir(home_dir, NULL, NULL);
1284 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1286 gchar *path;
1288 path = g_strconcat(filer_window->sym_path,
1289 "/", item->leafname, "#", NULL);
1291 filer_change_to(filer_window, path, NULL);
1292 g_free(path);
1295 static void select_all(gpointer data, guint action, GtkWidget *widget)
1297 g_return_if_fail(window_with_focus != NULL);
1299 window_with_focus->temp_item_selected = FALSE;
1300 view_select_all(window_with_focus->view);
1303 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1305 g_return_if_fail(window_with_focus != NULL);
1307 window_with_focus->temp_item_selected = FALSE;
1308 view_clear_selection(window_with_focus->view);
1311 static gboolean invert_cb(ViewIter *iter, gpointer data)
1313 return !view_get_selected((ViewIface *) data, iter);
1316 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1318 g_return_if_fail(window_with_focus != NULL);
1320 window_with_focus->temp_item_selected = FALSE;
1322 view_select_if(window_with_focus->view, invert_cb,
1323 window_with_focus->view);
1326 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1328 GtkWidget *win;
1330 win = options_show();
1332 if (win)
1334 number_of_windows++;
1335 g_signal_connect(win, "destroy",
1336 G_CALLBACK(one_less_window), NULL);
1340 static gboolean new_directory_cb(GObject *savebox,
1341 const gchar *initial, const gchar *path)
1343 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1345 report_error("mkdir: %s", g_strerror(errno));
1346 return FALSE;
1349 dir_check_this(path);
1351 if (filer_exists(window_with_focus))
1353 guchar *leaf;
1354 leaf = strrchr(path, '/');
1355 if (leaf)
1356 display_set_autoselect(window_with_focus, leaf + 1);
1359 return TRUE;
1362 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1364 g_return_if_fail(window_with_focus != NULL);
1366 savebox_show(_("Create"),
1367 make_path(window_with_focus->sym_path, _("NewDir")),
1368 type_to_icon(inode_directory), new_directory_cb,
1369 GDK_ACTION_COPY);
1372 static gboolean new_file_cb(GObject *savebox,
1373 const gchar *initial, const gchar *path)
1375 int fd;
1377 fd = open(path, O_CREAT | O_EXCL, 0666);
1379 if (fd == -1)
1381 report_error(_("Error creating '%s': %s"),
1382 path, g_strerror(errno));
1383 return FALSE;
1386 if (close(fd))
1387 report_error(_("Error creating '%s': %s"),
1388 path, g_strerror(errno));
1390 dir_check_this(path);
1392 if (filer_exists(window_with_focus))
1394 guchar *leaf;
1395 leaf = strrchr(path, '/');
1396 if (leaf)
1397 display_set_autoselect(window_with_focus, leaf + 1);
1400 return TRUE;
1403 static void new_file(gpointer data, guint action, GtkWidget *widget)
1405 g_return_if_fail(window_with_focus != NULL);
1407 savebox_show(_("Create"),
1408 make_path(window_with_focus->sym_path, _("NewFile")),
1409 type_to_icon(text_plain),
1410 new_file_cb, GDK_ACTION_COPY);
1413 static gboolean new_file_type_cb(GObject *savebox,
1414 const gchar *initial, const gchar *path)
1416 const gchar *oleaf, *leaf;
1417 gchar *templ, *templ_dname, *dest;
1418 GList *paths;
1420 /* We can work out the template path from the initial name */
1421 oleaf = g_basename(initial);
1422 templ_dname = choices_find_path_load("Templates", "");
1423 if (!templ_dname)
1425 report_error(
1426 _("Error creating file: could not find the template for %s"),
1427 oleaf);
1428 return FALSE;
1431 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1432 g_free(templ_dname);
1434 dest = g_path_get_dirname(path);
1435 leaf = g_basename(path);
1436 paths = g_list_append(NULL, templ);
1438 action_copy(paths, dest, leaf, TRUE);
1440 g_list_free(paths);
1441 g_free(dest);
1442 g_free(templ);
1444 if (filer_exists(window_with_focus))
1445 display_set_autoselect(window_with_focus, leaf);
1447 return TRUE;
1450 static void do_send_to(gchar *templ)
1452 g_return_if_fail(send_to_paths != NULL);
1454 run_with_files(templ, send_to_paths);
1457 static void new_file_type(gchar *templ)
1459 const gchar *leaf;
1460 MIME_type *type;
1462 g_return_if_fail(window_with_focus != NULL);
1464 leaf = g_basename(templ);
1465 type = type_get_type(templ);
1467 savebox_show(_("Create"),
1468 make_path(window_with_focus->sym_path, leaf),
1469 type_to_icon(type),
1470 new_file_type_cb, GDK_ACTION_COPY);
1473 static void customise_send_to(gpointer data)
1475 GPtrArray *path;
1476 guchar *save;
1477 GString *dirs;
1478 int i;
1480 dirs = g_string_new(NULL);
1482 path = choices_list_dirs("");
1483 for (i = 0; i < path->len; i++)
1485 guchar *old = (guchar *) path->pdata[i];
1487 g_string_append(dirs, old);
1488 g_string_append(dirs, "SendTo\n");
1490 choices_free_list(path);
1492 save = choices_find_path_save("", "SendTo", TRUE);
1493 if (save)
1494 mkdir(save, 0777);
1496 info_message(
1497 _("The `Send To' menu provides a quick way to send some files "
1498 "to an application. The applications listed are those in "
1499 "the following directories:\n\n%s\n%s\n"
1500 "The `Send To' menu may be opened by Shift+Menu clicking "
1501 "over a file.\n\n"
1502 "Advanced use:\n"
1503 "You can also create subdirectories called "
1504 "`.text_html', `.text', etc which will only be "
1505 "shown for files of that type. `.group' is shown "
1506 "only when multiple files are selected."),
1507 dirs->str,
1508 save ? _("I'll show you your SendTo directory now; you should "
1509 "symlink (Ctrl+Shift drag) any applications you want "
1510 "into it.")
1511 : _("Your CHOICESPATH variable setting prevents "
1512 "customisations - sorry."));
1514 g_string_free(dirs, TRUE);
1516 if (save)
1517 filer_opendir(save, NULL, NULL);
1520 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1521 * to the menu.
1523 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1525 gchar *searchdir;
1526 GPtrArray *paths;
1527 int i;
1529 if (subtype)
1530 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1531 else if (type)
1532 searchdir = g_strdup_printf("SendTo/.%s", type);
1533 else
1534 searchdir = g_strdup("SendTo");
1536 paths = choices_list_dirs(searchdir);
1537 g_free(searchdir);
1539 for (i = 0; i < paths->len; i++)
1541 GList *widgets = NULL;
1542 guchar *dir = (guchar *) paths->pdata[i];
1544 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1545 (CallbackFn) do_send_to,
1546 FALSE, FALSE, TRUE);
1548 if (widgets)
1549 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1550 gtk_menu_item_new());
1552 g_list_free(widgets); /* TODO: Get rid of this */
1555 choices_free_list(paths);
1558 /* Scan the SendTo dir and create and show the Send To menu.
1559 * The 'paths' list and every path in it is claimed, and will be
1560 * freed later -- don't free it yourself!
1562 static void show_send_to_menu(GList *paths, GdkEvent *event)
1564 GtkWidget *menu, *item;
1566 menu = gtk_menu_new();
1568 if (g_list_length(paths) == 1)
1570 DirItem *item;
1572 item = diritem_new("");
1573 diritem_restat(paths->data, item, NULL);
1575 add_sendto(menu,
1576 item->mime_type->media_type,
1577 item->mime_type->subtype);
1579 add_sendto(menu, item->mime_type->media_type, NULL);
1581 diritem_free(item);
1583 else
1584 add_sendto(menu, "group", NULL);
1586 add_sendto(menu, NULL, NULL);
1588 item = gtk_menu_item_new_with_label(_("Customise"));
1589 g_signal_connect_swapped(item, "activate",
1590 G_CALLBACK(customise_send_to), NULL);
1591 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1593 if (send_to_paths)
1594 destroy_glist(&send_to_paths);
1596 send_to_paths = paths;
1598 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1600 popup_menu = menu;
1601 show_popup_menu(menu, event, 0);
1604 static void send_to(FilerWindow *filer_window)
1606 GList *paths;
1607 GdkEvent *event;
1609 paths = filer_selected_items(filer_window);
1610 event = gtk_get_current_event();
1612 /* Eats paths */
1613 show_send_to_menu(paths, event);
1615 gdk_event_free(event);
1618 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1620 const char *argv[] = {"sh", "-c", NULL, NULL};
1621 gboolean close = action;
1623 argv[2] = o_menu_xterm.value;
1625 g_return_if_fail(window_with_focus != NULL);
1627 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1628 gtk_widget_destroy(window_with_focus->window);
1631 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1633 g_return_if_fail(window_with_focus != NULL);
1635 filer_change_to(window_with_focus, home_dir, NULL);
1638 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1640 g_return_if_fail(window_with_focus != NULL);
1642 bookmarks_show_menu(window_with_focus);
1645 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1647 g_return_if_fail(window_with_focus != NULL);
1649 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1650 filer_change_to(window_with_focus,
1651 window_with_focus->real_path, NULL);
1652 else
1653 delayed_error(_("This is already the canonical name "
1654 "for this directory."));
1657 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1659 g_return_if_fail(window_with_focus != NULL);
1661 filer_open_parent(window_with_focus);
1664 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1666 g_return_if_fail(window_with_focus != NULL);
1668 change_to_parent(window_with_focus);
1671 static void resize(gpointer data, guint action, GtkWidget *widget)
1673 g_return_if_fail(window_with_focus != NULL);
1675 view_autosize(window_with_focus->view);
1678 static void new_window(gpointer data, guint action, GtkWidget *widget)
1680 g_return_if_fail(window_with_focus != NULL);
1682 if (o_unique_filer_windows.int_value)
1684 report_error(_("You can't open a second view onto "
1685 "this directory because the `Unique Windows' option "
1686 "is turned on in the Options window."));
1688 else
1689 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1692 static void close_window(gpointer data, guint action, GtkWidget *widget)
1694 g_return_if_fail(window_with_focus != NULL);
1696 if (!filer_window_delete(window_with_focus->window, NULL,
1697 window_with_focus))
1698 gtk_widget_destroy(window_with_focus->window);
1701 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1703 MiniType type = (MiniType) action;
1705 g_return_if_fail(window_with_focus != NULL);
1707 /* Item needs to remain selected... */
1708 if (type == MINI_SHELL)
1709 window_with_focus->temp_item_selected = FALSE;
1711 minibuffer_show(window_with_focus, type);
1714 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1716 if (action == HELP_ABOUT)
1717 infobox_new(app_dir);
1718 else if (action == HELP_DIR)
1719 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1720 else if (action == HELP_MANUAL)
1722 gchar *manual = NULL;
1724 if (current_lang)
1726 manual = g_strconcat(app_dir, "/Help/Manual-",
1727 current_lang, ".html", NULL);
1728 if (access(manual, F_OK))
1729 null_g_free(&manual);
1732 if (!manual)
1733 manual = g_strconcat(app_dir,
1734 "/Help/Manual.html", NULL);
1736 run_by_path(manual);
1738 g_free(manual);
1740 else
1741 g_warning("Unknown help action %d\n", action);
1744 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1745 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1747 GList *items, *item;
1749 items = gtk_container_get_children(GTK_CONTAINER(menu));
1751 item = g_list_nth(items, from);
1752 while (item && n--)
1754 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1755 item = item->next;
1757 g_list_free(items);
1760 static void save_menus(void)
1762 char *menurc;
1764 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1765 if (menurc)
1767 gtk_accel_map_save(menurc);
1768 g_free(menurc);
1772 static void select_nth_item(GtkMenuShell *shell, int n)
1774 GList *items;
1775 GtkWidget *item;
1777 items = gtk_container_get_children(GTK_CONTAINER(shell));
1778 item = g_list_nth_data(items, n);
1780 g_return_if_fail(item != NULL);
1782 g_list_free(items);
1784 gtk_menu_shell_select_item(shell, item);
1787 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1789 DirItem *item;
1790 const guchar *path;
1791 int n_selected;
1792 ViewIter iter;
1794 g_return_if_fail(window_with_focus != NULL);
1796 n_selected = view_count_selected(window_with_focus->view);
1798 if (n_selected < 1)
1800 const char *prompt;
1802 switch (action)
1804 case FILE_COPY_ITEM:
1805 prompt = _("Copy ... ?");
1806 break;
1807 case FILE_RENAME_ITEM:
1808 prompt = _("Rename ... ?");
1809 break;
1810 case FILE_LINK_ITEM:
1811 prompt = _("Symlink ... ?");
1812 break;
1813 case FILE_OPEN_FILE:
1814 prompt = _("Shift Open ... ?");
1815 break;
1816 case FILE_HELP:
1817 prompt = _("Help about ... ?");
1818 break;
1819 case FILE_SHOW_FILE_INFO:
1820 prompt = _("Examine ... ?");
1821 break;
1822 case FILE_RUN_ACTION:
1823 prompt = _("Set run action for ... ?");
1824 break;
1825 case FILE_SET_ICON:
1826 prompt = _("Set icon for ... ?");
1827 break;
1828 case FILE_SEND_TO:
1829 prompt = _("Send ... to ... ?");
1830 break;
1831 case FILE_DELETE:
1832 prompt = _("DELETE ... ?");
1833 break;
1834 case FILE_USAGE:
1835 prompt = _("Count the size of ... ?");
1836 break;
1837 case FILE_CHMOD_ITEMS:
1838 prompt = _("Set permissions on ... ?");
1839 break;
1840 case FILE_FIND:
1841 prompt = _("Search inside ... ?");
1842 break;
1843 case FILE_OPEN_VFS_AVFS:
1844 prompt = _("Look inside ... ?");
1845 break;
1846 default:
1847 g_warning("Unknown action!");
1848 return;
1850 filer_target_mode(window_with_focus, target_callback,
1851 GINT_TO_POINTER(action), prompt);
1852 return;
1855 switch (action)
1857 case FILE_SEND_TO:
1858 send_to(window_with_focus);
1859 return;
1860 case FILE_DELETE:
1861 delete(window_with_focus);
1862 return;
1863 case FILE_USAGE:
1864 usage(window_with_focus);
1865 return;
1866 case FILE_CHMOD_ITEMS:
1867 chmod_items(window_with_focus);
1868 return;
1869 case FILE_FIND:
1870 find(window_with_focus);
1871 return;
1872 case FILE_SHOW_FILE_INFO:
1874 GList *items;
1876 items = filer_selected_items(window_with_focus);
1877 infobox_show_list(items);
1878 destroy_glist(&items);
1879 return;
1881 default:
1882 break;
1885 /* All the following actions require exactly one file selected */
1887 if (n_selected > 1)
1889 report_error(_("You cannot do this to more than "
1890 "one item at a time"));
1891 return;
1894 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1896 item = iter.next(&iter);
1897 g_return_if_fail(item != NULL);
1898 /* iter may be passed to filer_openitem... */
1900 if (!item->image)
1901 item = dir_update_item(window_with_focus->directory,
1902 item->leafname);
1904 if (!item)
1906 report_error(_("Item no longer exists!"));
1907 return;
1910 path = make_path(window_with_focus->sym_path, item->leafname);
1912 switch (action)
1914 case FILE_COPY_ITEM:
1915 src_dest_action_item(path, item->image,
1916 _("Copy"), copy_cb,
1917 GDK_ACTION_COPY);
1918 break;
1919 case FILE_RENAME_ITEM:
1920 src_dest_action_item(path, item->image,
1921 _("Rename"), rename_cb,
1922 GDK_ACTION_MOVE);
1923 break;
1924 case FILE_LINK_ITEM:
1925 src_dest_action_item(path, item->image,
1926 _("Symlink"), link_cb,
1927 GDK_ACTION_LINK);
1928 break;
1929 case FILE_OPEN_FILE:
1930 filer_openitem(window_with_focus, &iter,
1931 OPEN_SAME_WINDOW | OPEN_SHIFT);
1932 break;
1933 case FILE_HELP:
1934 show_item_help(path, item);
1935 break;
1936 case FILE_RUN_ACTION:
1937 run_action(item);
1938 break;
1939 case FILE_SET_ICON:
1940 icon_set_handler_dialog(item, path);
1941 break;
1942 case FILE_OPEN_VFS_AVFS:
1943 open_vfs_avfs(window_with_focus, item);
1944 break;
1945 case FILE_EJECT:
1947 GList *items;
1949 items=filer_selected_items(window_with_focus);
1950 action_eject(items);
1951 destroy_glist(&items);
1952 break;
1954 default:
1955 g_warning("Unknown action!");
1956 return;
1960 static void show_key_help(GtkWidget *button, gpointer data)
1962 gboolean can_change_accels;
1964 g_object_get(G_OBJECT(gtk_settings_get_default()),
1965 "gtk-can-change-accels", &can_change_accels,
1966 NULL);
1968 if (!can_change_accels)
1970 info_message(_("User-definable shortcuts are disabled by "
1971 "default in Gtk2, and you have not enabled "
1972 "them. You can turn this feature on by:\n"
1973 "1) using an XSettings manager, such as ROX-Session\n"
1974 "or\n"
1975 "2) adding this line to ~/.gtkrc-2.0:\n"
1976 "\tgtk-can-change-accels = 1"));
1977 return;
1980 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1981 "- Open the menu over a filer window,\n"
1982 "- Move the pointer over the item you want to use,\n"
1983 "- Press the key you want attached to it.\n\n"
1984 "The key will appear next to the menu item and you can just press "
1985 "that key without opening the menu in future."));
1988 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1990 GtkWidget *button, *align;
1992 g_return_val_if_fail(option == NULL, NULL);
1994 align = gtk_alignment_new(0, 0.5, 0, 0);
1995 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1996 gtk_container_add(GTK_CONTAINER(align), button);
1997 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1999 return g_list_append(NULL, align);