r3747: Allow renaming multiple files at once. This opens the Bulk Rename window,
[rox-filer.git] / ROX-Filer / src / menu.c
blob30951ae844e5ff7fab29e377ac328dc198eeb319
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"
63 #include "panel.h"
64 #include "bulk_rename.h"
65 #include "xtypes.h"
67 typedef enum {
68 FILE_COPY_ITEM,
69 FILE_RENAME_ITEM,
70 FILE_LINK_ITEM,
71 FILE_OPEN_FILE,
72 FILE_PROPERTIES,
73 FILE_RUN_ACTION,
74 FILE_SET_ICON,
75 FILE_SEND_TO,
76 FILE_DELETE,
77 FILE_USAGE,
78 FILE_CHMOD_ITEMS,
79 FILE_FIND,
80 FILE_OPEN_VFS_AVFS,
81 FILE_SET_TYPE,
82 } FileOp;
84 typedef void (*ActionFn)(GList *paths,
85 const char *dest_dir, const char *leaf, int quiet);
86 typedef void MenuCallback(GtkWidget *widget, gpointer data);
88 typedef gboolean (*SaveCb)(GObject *savebox,
89 const gchar *current, const gchar *new);
91 GtkAccelGroup *filer_keys = NULL;
92 static gboolean filer_keys_need_init = TRUE;
94 static GtkWidget *popup_menu = NULL; /* Currently open menu */
96 static gint updating_menu = 0; /* Non-zero => ignore activations */
97 static GList *send_to_paths = NULL;
99 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
101 /* Static prototypes */
103 static void save_menus(void);
104 static void menu_closed(GtkWidget *widget);
105 static void shade_file_menu_items(gboolean shaded);
106 static void savebox_show(const gchar *action, const gchar *path,
107 MaskedPixmap *image, SaveCb callback,
108 GdkDragAction dnd_action);
109 static gint save_to_file(GObject *savebox,
110 const gchar *pathname, gpointer data);
111 static gboolean action_with_leaf(ActionFn action,
112 const gchar *current, const gchar *new);
113 static gboolean link_cb(GObject *savebox,
114 const gchar *initial, const gchar *path);
115 static void select_nth_item(GtkMenuShell *shell, int n);
116 static void new_file_type(gchar *templ);
117 static void do_send_to(gchar *templ);
118 static void show_send_to_menu(GList *paths, GdkEvent *event);
119 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
121 /* Note that for most of these callbacks none of the arguments are used. */
123 static void view_type(gpointer data, guint action, GtkWidget *widget);
125 /* (action used in these three - DetailsType) */
126 static void change_size(gpointer data, guint action, GtkWidget *widget);
127 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
128 static void set_with(gpointer data, guint action, GtkWidget *widget);
130 static void set_sort(gpointer data, guint action, GtkWidget *widget);
131 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
133 static void hidden(gpointer data, guint action, GtkWidget *widget);
134 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
135 static void refresh(gpointer data, guint action, GtkWidget *widget);
136 static void save_settings(gpointer data, guint action, GtkWidget *widget);
138 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
140 static void select_all(gpointer data, guint action, GtkWidget *widget);
141 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
142 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
143 static void new_directory(gpointer data, guint action, GtkWidget *widget);
144 static void new_file(gpointer data, guint action, GtkWidget *widget);
145 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent(gpointer data, guint action, GtkWidget *widget);
149 static void home_directory(gpointer data, guint action, GtkWidget *widget);
150 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
151 static void new_window(gpointer data, guint action, GtkWidget *widget);
152 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
153 static void close_window(gpointer data, guint action, GtkWidget *widget);
154 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
156 /* (action used in this - MiniType) */
157 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
158 static void resize(gpointer data, guint action, GtkWidget *widget);
160 #define MENUS_NAME "menus2"
162 static GtkWidget *filer_menu; /* The popup filer menu */
163 static GtkWidget *filer_file_item; /* The File '' label */
164 static GtkWidget *filer_file_menu; /* The File '' menu */
165 static GtkWidget *file_shift_item; /* Shift Open label */
166 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
167 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
168 static GtkWidget *filer_reverse_menu; /* The Reversed item */
169 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
170 static GtkWidget *filer_new_window; /* The New Window item */
171 static GtkWidget *filer_new_menu; /* The New submenu */
172 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
173 static GtkWidget *filer_set_type; /* Set type item */
175 #undef N_
176 #define N_(x) x
178 static GtkItemFactoryEntry filer_menu_def[] = {
179 {N_("Display"), NULL, NULL, 0, "<Branch>"},
180 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
181 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
182 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
183 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
184 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
185 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
186 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
187 {">", NULL, NULL, 0, "<Separator>"},
188 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
189 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
190 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
191 {">", NULL, NULL, 0, "<Separator>"},
192 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
193 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
194 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
195 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
196 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
197 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
198 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
199 {">", NULL, NULL, 0, "<Separator>"},
200 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
201 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
202 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
203 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
204 {">" N_("Save Display Settings..."), NULL, save_settings, 0, NULL},
205 {N_("File"), NULL, NULL, 0, "<Branch>"},
206 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
207 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
208 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
209 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
210 {">", NULL, NULL, 0, "<Separator>"},
211 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
212 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
213 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
214 {">", NULL, NULL, 0, "<Separator>"},
215 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
216 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
217 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
218 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
219 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
220 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
221 {">", NULL, NULL, 0, "<Separator>"},
222 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
223 {N_("Select"), NULL, NULL, 0, "<Branch>"},
224 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
225 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
226 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
227 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
228 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
229 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
230 {N_("New"), NULL, NULL, 0, "<Branch>"},
231 {">" N_("Directory"), NULL, new_directory, 0, NULL},
232 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
233 {N_("Window"), NULL, NULL, 0, "<Branch>"},
234 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
235 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
236 {">" N_("New Window"), NULL, new_window, 0, NULL},
237 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
238 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
239 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
240 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
241 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
243 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
244 {">", NULL, NULL, 0, "<Separator>"},
245 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
246 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
247 {">" N_("Xterm Here"), "grave", xterm_here, FALSE, NULL},
248 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
249 {N_("Help"), NULL, NULL, 0, "<Branch>"},
250 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
251 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
252 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
256 #define GET_MENU_ITEM(var, menu) \
257 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
259 #define GET_SMENU_ITEM(var, menu, sub) \
260 do { \
261 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
262 var = gtk_item_factory_get_widget(item_factory, tmp); \
263 g_free(tmp); \
264 } while (0)
266 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
267 do { \
268 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
269 var = gtk_item_factory_get_widget(item_factory, tmp); \
270 g_free(tmp); \
271 } while (0)
273 /* Returns TRUE if the keys were installed (first call only) */
274 gboolean ensure_filer_menu(void)
276 GList *items;
277 guchar *tmp;
278 GtkWidget *item;
279 GtkItemFactory *item_factory;
281 if (!filer_keys_need_init)
282 return FALSE;
283 filer_keys_need_init = FALSE;
285 item_factory = menu_create(filer_menu_def,
286 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
287 "<filer>", filer_keys);
289 GET_MENU_ITEM(filer_menu, "filer");
290 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
291 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
292 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
293 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
294 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
295 "Show Thumbnails");
296 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
297 filer_set_type = GTK_BIN(item)->child;
299 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
300 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
301 filer_follow_sym = GTK_BIN(item)->child;
303 /* File '' label... */
304 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
305 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
306 g_list_free(items);
308 /* Shift Open... label */
309 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
310 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
311 g_list_free(items);
313 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
314 filer_new_window = GTK_BIN(item)->child;
316 g_signal_connect(filer_menu, "unmap_event",
317 G_CALLBACK(menu_closed), NULL);
318 g_signal_connect(filer_file_menu, "unmap_event",
319 G_CALLBACK(menu_closed), NULL);
321 g_signal_connect(filer_keys, "accel_changed",
322 G_CALLBACK(save_menus), NULL);
324 return TRUE;
327 void menu_init(void)
329 char *menurc;
331 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
332 if (menurc)
334 gtk_accel_map_load(menurc);
335 g_free(menurc);
338 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
339 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
340 option_add_int(&o_menu_quick, "menu_quick", FALSE);
341 option_add_saver(save_menus);
343 option_register_widget("menu-set-keys", set_keys_button);
345 filer_keys = gtk_accel_group_new();
348 /* Name is in the form "<panel>" */
349 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
350 const gchar *name, GtkAccelGroup *keys)
352 GtkItemFactory *item_factory;
353 GtkItemFactoryEntry *translated;
355 if (!keys)
357 keys = gtk_accel_group_new();
358 gtk_accel_group_lock(keys);
361 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
363 translated = translate_entries(def, n_entries);
364 gtk_item_factory_create_items(item_factory, n_entries,
365 translated, NULL);
366 free_translated_entries(translated, n_entries);
368 return item_factory;
371 /* Prevent the user from setting a short-cut on this item */
372 static void menuitem_no_shortcuts(GtkWidget *item)
374 /* XXX */
375 #if 0
376 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
378 _gtk_widget_set_accel_path(item, NULL, NULL);
379 null_g_free(&menuitem->accel_path);
380 #endif
383 /* Shade items that only work on single files */
384 static void shade_file_menu_items(gboolean shaded)
386 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
387 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
388 menu_set_items_shaded(filer_file_menu, shaded, 5, 2);
389 menu_set_items_shaded(filer_file_menu, shaded, 9, 2);
392 /* 'data' is an array of three ints:
393 * [ pointer_x, pointer_y, item_under_pointer ]
395 void position_menu(GtkMenu *menu, gint *x, gint *y,
396 gboolean *push_in, gpointer data)
398 int *pos = (int *) data;
399 GtkRequisition requisition;
400 GList *items, *next;
401 int y_shift = 0;
402 int item = pos[2];
404 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
406 while (item >= 0 && next)
408 int h = ((GtkWidget *) next->data)->requisition.height;
410 if (item > 0)
411 y_shift += h;
412 else
413 y_shift += h / 2;
415 next = next->next;
416 item--;
419 g_list_free(items);
421 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
423 *x = pos[0] - (requisition.width * 7 / 8);
424 *y = pos[1] - y_shift;
426 *x = CLAMP(*x, 0, screen_width - requisition.width);
427 *y = CLAMP(*y, 0, screen_height - requisition.height);
429 *push_in = FALSE;
432 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
433 MenuIconStyle style)
435 GtkWidget *item;
437 if (style != MIS_NONE && di_image(ditem))
439 GdkPixbuf *pixbuf;
440 MaskedPixmap *image;
442 image = di_image(ditem);
444 switch (style)
446 case MIS_LARGE:
447 pixbuf = image->pixbuf;
448 break;
449 default:
450 if (!image->sm_pixbuf)
451 pixmap_make_small(image);
452 pixbuf = image->sm_pixbuf;
453 break;
456 item = gtk_image_menu_item_new_with_label(label);
457 /* TODO: Find a way to allow short-cuts */
458 menuitem_no_shortcuts(item);
460 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
461 gtk_image_new_from_pixbuf(pixbuf));
462 gtk_widget_show_all(item);
464 else
465 item = gtk_menu_item_new_with_label(label);
467 return item;
470 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
471 MenuIconStyle style, CallbackFn func,
472 gboolean separator, gboolean strip_ext,
473 gboolean recurse)
475 GList *widgets = NULL;
476 DirItem *ditem;
477 int i;
478 GtkWidget *item;
479 char *dname = NULL;
480 GPtrArray *names;
482 dname = pathdup(dir_name);
484 names = list_dir(dname);
485 if (!names)
486 goto out;
488 if (separator)
490 item = gtk_menu_item_new();
491 widgets = g_list_append(widgets, item);
492 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
495 for (i = 0; i < names->len; i++)
497 char *leaf = names->pdata[i];
498 gchar *fname;
500 fname = g_strconcat(dname, "/", leaf, NULL);
502 /* Strip off extension, if any */
503 if (strip_ext)
505 char *dot;
506 dot = strchr(leaf, '.');
507 if (dot)
508 *dot = '\0';
511 ditem = diritem_new("");
512 diritem_restat(fname, ditem, NULL);
514 item = make_send_to_item(ditem, leaf, style);
516 g_free(leaf);
518 /* If it is a directory (but NOT an AppDir) and we are
519 * recursing then set up a sub menu.
521 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
522 !(ditem->flags & ITEM_FLAG_APPDIR))
524 GtkWidget *sub;
525 GList *new_widgets;
527 sub = gtk_menu_new();
528 new_widgets = menu_from_dir(sub, fname, style, func,
529 separator, strip_ext, FALSE);
530 g_list_free(new_widgets);
531 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
533 else
534 g_signal_connect_swapped(item, "activate",
535 G_CALLBACK(func), fname);
537 diritem_free(ditem);
539 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
540 g_signal_connect_swapped(item, "destroy",
541 G_CALLBACK(g_free), fname);
543 widgets = g_list_append(widgets, item);
546 g_ptr_array_free(names, TRUE);
547 out:
548 g_free(dname);
550 return widgets;
553 /* Scan the templates dir and create entries for the New menu */
554 static void update_new_files_menu(MenuIconStyle style)
556 static GList *widgets = NULL;
558 gchar *templ_dname = NULL;
560 if (widgets)
562 GList *next;
564 for (next = widgets; next; next = next->next)
565 gtk_widget_destroy((GtkWidget *) next->data);
567 g_list_free(widgets);
568 widgets = NULL;
571 templ_dname = choices_find_path_load("Templates", "");
572 if (templ_dname)
574 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
575 (CallbackFn) new_file_type, TRUE, TRUE,
576 FALSE);
577 g_free(templ_dname);
579 gtk_widget_show_all(filer_new_menu);
582 /* 'item' is the number of the item to appear under the pointer. */
583 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
585 int pos[3];
586 int button = 0;
587 guint32 time = 0;
589 if (event && (event->type == GDK_BUTTON_PRESS ||
590 event->type == GDK_BUTTON_RELEASE))
592 GdkEventButton *bev = (GdkEventButton *) event;
594 pos[0] = bev->x_root;
595 pos[1] = bev->y_root;
596 button = bev->button;
597 time = bev->time;
599 else if (event && event->type == GDK_KEY_PRESS)
601 GdkEventKey *kev = (GdkEventKey *) event;
603 get_pointer_xy(pos, pos + 1);
604 time = kev->time;
606 else
607 get_pointer_xy(pos, pos + 1);
609 pos[2] = item;
611 gtk_widget_show_all(menu);
612 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
613 position_menu, (gpointer) pos, button, time);
614 select_nth_item(GTK_MENU_SHELL(menu), item);
617 /* Hide the popup menu, if any */
618 void menu_popdown(void)
620 if (popup_menu)
621 gtk_menu_popdown(GTK_MENU(popup_menu));
624 static MenuIconStyle get_menu_icon_style(void)
626 MenuIconStyle mis;
627 int display;
629 mis = o_menu_iconsize.int_value;
631 switch (mis)
633 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
634 return mis;
635 default:
636 break;
639 if (mis == MIS_CURRENT && window_with_focus)
641 switch (window_with_focus->display_style)
643 case HUGE_ICONS:
644 case LARGE_ICONS:
645 return MIS_LARGE;
646 case SMALL_ICONS:
647 return MIS_SMALL;
648 default:
649 break;
653 display = o_display_size.int_value;
654 switch (display)
656 case HUGE_ICONS:
657 case LARGE_ICONS:
658 return MIS_LARGE;
659 case SMALL_ICONS:
660 return MIS_SMALL;
661 default:
662 break;
665 return MIS_SMALL;
668 /* iter->peek() is the clicked item, or NULL if none */
669 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
671 DirItem *file_item = NULL;
672 GdkModifierType state = 0;
673 int n_selected;
674 int n_added = 0;
676 n_selected = view_count_selected(filer_window->view);
678 ensure_filer_menu();
680 updating_menu++;
682 /* Remove previous AppMenu, if any */
683 appmenu_remove();
685 window_with_focus = filer_window;
687 if (!event)
688 event = gtk_get_current_event();
690 if (event->type == GDK_BUTTON_PRESS)
691 state = ((GdkEventButton *) event)->state;
692 else if (event->type == GDK_KEY_PRESS)
693 state = ((GdkEventKey *) event)->state;
695 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
697 filer_window->temp_item_selected = TRUE;
698 view_set_selected(filer_window->view, iter, TRUE);
699 n_selected = view_count_selected(filer_window->view);
701 else
703 filer_window->temp_item_selected = FALSE;
706 /* Short-cut to the Send To menu */
707 if (state & GDK_SHIFT_MASK)
709 GList *paths;
711 updating_menu--;
713 if (n_selected == 0)
715 report_error(
716 _("You should Shift+Menu click over a file to "
717 "send it somewhere"));
718 return;
721 paths = filer_selected_items(filer_window);
723 show_send_to_menu(paths, event); /* (paths eaten) */
725 return;
729 GtkWidget *file_label, *file_menu;
730 GString *buffer;
731 DirItem *item;
733 file_label = filer_file_item;
734 file_menu = filer_file_menu;
735 gtk_check_menu_item_set_active(
736 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
737 filer_window->show_thumbs);
738 gtk_check_menu_item_set_active(
739 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
740 filer_window->show_hidden);
741 gtk_check_menu_item_set_active(
742 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
743 filer_window->sort_order != GTK_SORT_ASCENDING);
744 gtk_check_menu_item_set_active(
745 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
746 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
747 buffer = g_string_new(NULL);
749 switch (n_selected)
751 case 0:
752 g_string_assign(buffer, _("Next Click"));
753 shade_file_menu_items(FALSE);
754 break;
755 case 1:
756 item = filer_selected_item(filer_window);
757 if (item->base_type == TYPE_UNKNOWN)
758 dir_update_item(filer_window->directory,
759 item->leafname);
760 shade_file_menu_items(FALSE);
761 file_item = filer_selected_item(filer_window);
762 g_string_printf(buffer, _("%s '%s'"),
763 basetype_name(file_item),
764 g_utf8_validate(file_item->leafname,
765 -1, NULL)
766 ? file_item->leafname
767 : _("(bad utf-8)"));
768 if (!can_set_run_action(file_item))
769 menu_set_items_shaded(filer_file_menu,
770 TRUE, 9, 1);
771 break;
772 default:
773 shade_file_menu_items(TRUE);
774 g_string_printf(buffer, _("%d items"),
775 n_selected);
776 break;
778 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
779 g_string_free(buffer, TRUE);
781 menu_show_shift_action(file_shift_item, file_item,
782 n_selected == 0);
783 if (file_item)
784 n_added = appmenu_add(make_path(filer_window->sym_path,
785 file_item->leafname),
786 file_item, filer_file_menu);
789 update_new_files_menu(get_menu_icon_style());
791 gtk_widget_set_sensitive(filer_new_window,
792 !o_unique_filer_windows.int_value);
793 gtk_widget_set_sensitive(filer_follow_sym,
794 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
795 gtk_widget_set_sensitive(filer_set_type,
796 xtype_supported(filer_window->real_path));
798 if (n_selected && o_menu_quick.int_value)
799 popup_menu = (state & GDK_CONTROL_MASK)
800 ? filer_menu
801 : filer_file_menu;
802 else
803 popup_menu = (state & GDK_CONTROL_MASK)
804 ? filer_file_menu
805 : filer_menu;
807 updating_menu--;
809 show_popup_menu(popup_menu, event,
810 popup_menu == filer_file_menu ? n_added : 1);
813 static void menu_closed(GtkWidget *widget)
815 if (window_with_focus == NULL || widget != popup_menu)
816 return; /* Close panel item chosen? */
818 popup_menu = NULL;
820 if (window_with_focus->temp_item_selected)
822 view_clear_selection(window_with_focus->view);
823 window_with_focus->temp_item_selected = FALSE;
826 appmenu_remove();
829 static void target_callback(FilerWindow *filer_window,
830 ViewIter *iter,
831 gpointer action)
833 g_return_if_fail(filer_window != NULL);
835 window_with_focus = filer_window;
837 /* Don't grab the primary selection */
838 filer_window->temp_item_selected = TRUE;
840 view_wink_item(filer_window->view, iter);
841 view_select_only(filer_window->view, iter);
842 file_op(NULL, GPOINTER_TO_INT(action), NULL);
844 view_clear_selection(filer_window->view);
845 filer_window->temp_item_selected = FALSE;
848 /* Set the text of the 'Shift Open...' menu item.
849 * If icon is NULL, reset the text and also shade it, unless 'next'.
851 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
853 guchar *shift_action = NULL;
855 if (item)
857 if (item->flags & ITEM_FLAG_MOUNT_POINT)
859 if (item->flags & ITEM_FLAG_MOUNTED)
860 shift_action = N_("Unmount");
861 else
862 shift_action = N_("Open unmounted");
864 else if (item->flags & ITEM_FLAG_SYMLINK)
865 shift_action = N_("Show Target");
866 else if (item->base_type == TYPE_DIRECTORY)
867 shift_action = N_("Look Inside");
868 else if (item->base_type == TYPE_FILE)
869 shift_action = N_("Open As Text");
871 gtk_label_set_text(GTK_LABEL(menu_item),
872 shift_action ? _(shift_action)
873 : _("Shift Open"));
874 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
877 /* Actions */
879 static void view_type(gpointer data, guint action, GtkWidget *widget)
881 ViewType view_type = (ViewType) action;
883 g_return_if_fail(window_with_focus != NULL);
885 if (view_type == VIEW_TYPE_COLLECTION)
886 display_set_layout(window_with_focus,
887 window_with_focus->display_style_wanted,
888 DETAILS_NONE, FALSE);
890 filer_set_view_type(window_with_focus, (ViewType) action);
893 static void change_size(gpointer data, guint action, GtkWidget *widget)
895 g_return_if_fail(window_with_focus != NULL);
897 display_change_size(window_with_focus, action == 1);
900 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
902 g_return_if_fail(window_with_focus != NULL);
904 if (updating_menu)
905 return;
907 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
908 display_set_layout(window_with_focus,
909 window_with_focus->display_style,
910 window_with_focus->details_type, FALSE);
911 else
912 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
913 window_with_focus->details_type, FALSE);
916 static void set_with(gpointer data, guint action, GtkWidget *widget)
918 DisplayStyle size;
920 g_return_if_fail(window_with_focus != NULL);
922 size = window_with_focus->display_style_wanted;
924 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
925 display_set_layout(window_with_focus, size, action, FALSE);
928 static void set_sort(gpointer data, guint action, GtkWidget *widget)
930 if (updating_menu)
931 return;
933 g_return_if_fail(window_with_focus != NULL);
935 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
938 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
940 GtkSortType order;
942 if (updating_menu)
943 return;
945 g_return_if_fail(window_with_focus != NULL);
947 order = window_with_focus->sort_order;
948 if (order == GTK_SORT_ASCENDING)
949 order = GTK_SORT_DESCENDING;
950 else
951 order = GTK_SORT_ASCENDING;
953 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
954 order);
957 static void hidden(gpointer data, guint action, GtkWidget *widget)
959 if (updating_menu)
960 return;
962 g_return_if_fail(window_with_focus != NULL);
964 display_set_hidden(window_with_focus,
965 !window_with_focus->show_hidden);
968 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
970 if (updating_menu)
971 return;
973 g_return_if_fail(window_with_focus != NULL);
975 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
978 static void refresh(gpointer data, guint action, GtkWidget *widget)
980 g_return_if_fail(window_with_focus != NULL);
982 filer_refresh(window_with_focus);
985 static void save_settings(gpointer data, guint action, GtkWidget *widget)
987 g_return_if_fail(window_with_focus != NULL);
989 filer_save_settings(window_with_focus);
992 static void delete(FilerWindow *filer_window)
994 GList *paths;
995 paths = filer_selected_items(filer_window);
996 action_delete(paths);
997 destroy_glist(&paths);
1000 static void usage(FilerWindow *filer_window)
1002 GList *paths;
1003 paths = filer_selected_items(filer_window);
1004 action_usage(paths);
1005 destroy_glist(&paths);
1008 static void chmod_items(FilerWindow *filer_window)
1010 GList *paths;
1011 paths = filer_selected_items(filer_window);
1012 action_chmod(paths, FALSE, NULL);
1013 destroy_glist(&paths);
1016 static void set_type_items(FilerWindow *filer_window)
1018 GList *paths, *p;
1019 int npass=0, nfail=0;
1021 paths = filer_selected_items(filer_window);
1022 for(p=paths; p; p=g_list_next(p)) {
1023 if(xtype_supported((const char *) p->data))
1024 npass++;
1025 else
1026 nfail++;
1028 if(npass==0)
1029 report_error(_("Extended attributes, used to store types, are not supported for this "
1030 "file or files.\n"
1031 "This may be due to lack of support from the filesystem or the C library, "
1032 "or it may simply be that the filesystem needs to be mounted with "
1033 "the right mount option ('user_xattr' on Linux)."));
1034 else if(nfail>0)
1035 report_error(_("Setting type not supported for some of these files"));
1036 if(npass>0)
1037 action_settype(paths, FALSE, NULL);
1038 destroy_glist(&paths);
1041 static void find(FilerWindow *filer_window)
1043 GList *paths;
1044 paths = filer_selected_items(filer_window);
1045 action_find(paths);
1046 destroy_glist(&paths);
1049 /* This creates a new savebox widget, and allows the user to pick a new path
1050 * for the file.
1051 * Once the new path has been picked, the callback will be called with
1052 * both the current and new paths.
1053 * NOTE: This function unrefs 'image'!
1055 static void savebox_show(const gchar *action, const gchar *path,
1056 MaskedPixmap *image, SaveCb callback,
1057 GdkDragAction dnd_action)
1059 GtkWidget *savebox = NULL;
1060 GtkWidget *check_relative = NULL;
1062 g_return_if_fail(image != NULL);
1064 savebox = gtk_savebox_new(action);
1065 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1067 if (callback == link_cb)
1069 check_relative = gtk_check_button_new_with_mnemonic(
1070 _("_Relative link"));
1071 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1072 TRUE);
1074 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1075 gtk_tooltips_set_tip(tooltips, check_relative,
1076 _("If on, the symlink will store the path from the "
1077 "symlink to the target file. Use this if the symlink "
1078 "and the target will be moved together.\n"
1079 "If off, the path from the root directory is stored - "
1080 "use this if the symlink may move but the target will "
1081 "stay put."), NULL);
1082 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1083 check_relative, FALSE, TRUE, 0);
1084 gtk_widget_show(check_relative);
1087 g_signal_connect(savebox, "save_to_file",
1088 G_CALLBACK(save_to_file), NULL);
1090 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1091 g_strdup(path), g_free);
1092 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1093 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1095 gtk_window_set_title(GTK_WINDOW(savebox), action);
1097 if (g_utf8_validate(path, -1, NULL))
1098 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1099 else
1101 gchar *u8, *dir;
1102 dir = g_path_get_dirname(path);
1103 u8 = to_utf8(g_basename(path));
1104 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1105 make_path(dir, u8));
1106 g_free(u8);
1107 g_free(dir);
1109 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1110 g_object_unref(image);
1112 gtk_widget_show(savebox);
1115 static gint save_to_file(GObject *savebox,
1116 const gchar *pathname, gpointer data)
1118 SaveCb callback;
1119 const gchar *current_path;
1121 callback = g_object_get_data(savebox, "action_callback");
1122 current_path = g_object_get_data(savebox, "current_path");
1124 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1125 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1127 return callback(savebox, current_path, pathname)
1128 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1131 static gboolean copy_cb(GObject *savebox,
1132 const gchar *current, const gchar *new)
1134 return action_with_leaf(action_copy, current, new);
1137 static gboolean action_with_leaf(ActionFn action,
1138 const gchar *current, const gchar *new)
1140 const char *leaf;
1141 char *new_dir;
1142 GList *local_paths;
1144 if (new[0] != '/')
1146 report_error(_("New pathname is not absolute"));
1147 return FALSE;
1150 if (new[strlen(new) - 1] == '/')
1152 new_dir = g_strdup(new);
1153 leaf = NULL;
1155 else
1157 const gchar *slash;
1159 slash = strrchr(new, '/');
1160 new_dir = g_strndup(new, slash - new);
1161 leaf = slash + 1;
1164 local_paths = g_list_append(NULL, (gchar *) current);
1165 action(local_paths, new_dir, leaf, -1);
1166 g_list_free(local_paths);
1168 g_free(new_dir);
1170 return TRUE;
1173 /* Open a savebox to act on the selected file.
1174 * Call 'callback' later to perform the operation.
1176 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1177 const gchar *action, SaveCb callback,
1178 GdkDragAction dnd_action)
1180 g_object_ref(image);
1181 savebox_show(action, path, image, callback, dnd_action);
1184 static gboolean rename_cb(GObject *savebox,
1185 const gchar *current, const gchar *new)
1187 return action_with_leaf(action_move, current, new);
1190 static gboolean link_cb(GObject *savebox,
1191 const gchar *initial, const gchar *path)
1193 GtkToggleButton *check_relative;
1194 struct stat info;
1195 int err;
1196 gchar *link_path;
1198 check_relative = g_object_get_data(savebox, "check_relative");
1200 if (gtk_toggle_button_get_active(check_relative))
1201 link_path = get_relative_path(path, initial);
1202 else
1203 link_path = g_strdup(initial);
1205 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1207 GtkWidget *box, *button;
1208 gint ans;
1210 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1211 GTK_BUTTONS_CANCEL,
1212 _("Symlink from '%s' already exists. "
1213 "Replace it with a link to '%s'?"),
1214 path, link_path);
1216 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1218 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1219 gtk_widget_show(button);
1220 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1221 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1222 button, GTK_RESPONSE_OK);
1223 gtk_dialog_set_default_response(GTK_DIALOG(box),
1224 GTK_RESPONSE_OK);
1226 ans = gtk_dialog_run(GTK_DIALOG(box));
1227 gtk_widget_destroy(box);
1229 if (ans != GTK_RESPONSE_OK)
1231 g_free(link_path);
1232 return FALSE;
1235 unlink(path);
1238 err = symlink(link_path, path);
1239 g_free(link_path);
1241 if (err)
1243 report_error("symlink: %s", g_strerror(errno));
1244 return FALSE;
1247 dir_check_this(path);
1249 return TRUE;
1252 static void run_action(DirItem *item)
1254 if (can_set_run_action(item))
1255 type_set_handler_dialog(item->mime_type);
1256 else
1257 report_error(
1258 _("You can only set the run action for a "
1259 "regular file"));
1262 void open_home(gpointer data, guint action, GtkWidget *widget)
1264 filer_opendir(home_dir, NULL, NULL);
1267 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1269 gchar *path;
1271 path = g_strconcat(filer_window->sym_path,
1272 "/", item->leafname, "#", NULL);
1274 filer_change_to(filer_window, path, NULL);
1275 g_free(path);
1278 static void select_all(gpointer data, guint action, GtkWidget *widget)
1280 g_return_if_fail(window_with_focus != NULL);
1282 window_with_focus->temp_item_selected = FALSE;
1283 view_select_all(window_with_focus->view);
1286 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1288 g_return_if_fail(window_with_focus != NULL);
1290 window_with_focus->temp_item_selected = FALSE;
1291 view_clear_selection(window_with_focus->view);
1294 static gboolean invert_cb(ViewIter *iter, gpointer data)
1296 return !view_get_selected((ViewIface *) data, iter);
1299 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1301 g_return_if_fail(window_with_focus != NULL);
1303 window_with_focus->temp_item_selected = FALSE;
1305 view_select_if(window_with_focus->view, invert_cb,
1306 window_with_focus->view);
1309 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1311 GtkWidget *win;
1313 win = options_show();
1315 if (win)
1317 number_of_windows++;
1318 g_signal_connect(win, "destroy",
1319 G_CALLBACK(one_less_window), NULL);
1323 static gboolean new_directory_cb(GObject *savebox,
1324 const gchar *initial, const gchar *path)
1326 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1328 report_error("mkdir: %s", g_strerror(errno));
1329 return FALSE;
1332 dir_check_this(path);
1334 if (filer_exists(window_with_focus))
1336 guchar *leaf;
1337 leaf = strrchr(path, '/');
1338 if (leaf)
1339 display_set_autoselect(window_with_focus, leaf + 1);
1342 return TRUE;
1345 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1347 g_return_if_fail(window_with_focus != NULL);
1349 savebox_show(_("Create"),
1350 make_path(window_with_focus->sym_path, _("NewDir")),
1351 type_to_icon(inode_directory), new_directory_cb,
1352 GDK_ACTION_COPY);
1355 static gboolean new_file_cb(GObject *savebox,
1356 const gchar *initial, const gchar *path)
1358 int fd;
1360 fd = open(path, O_CREAT | O_EXCL, 0666);
1362 if (fd == -1)
1364 report_error(_("Error creating '%s': %s"),
1365 path, g_strerror(errno));
1366 return FALSE;
1369 if (close(fd))
1370 report_error(_("Error creating '%s': %s"),
1371 path, g_strerror(errno));
1373 dir_check_this(path);
1375 if (filer_exists(window_with_focus))
1377 guchar *leaf;
1378 leaf = strrchr(path, '/');
1379 if (leaf)
1380 display_set_autoselect(window_with_focus, leaf + 1);
1383 return TRUE;
1386 static void new_file(gpointer data, guint action, GtkWidget *widget)
1388 g_return_if_fail(window_with_focus != NULL);
1390 savebox_show(_("Create"),
1391 make_path(window_with_focus->sym_path, _("NewFile")),
1392 type_to_icon(text_plain),
1393 new_file_cb, GDK_ACTION_COPY);
1396 static gboolean new_file_type_cb(GObject *savebox,
1397 const gchar *initial, const gchar *path)
1399 const gchar *oleaf, *leaf;
1400 gchar *templ, *templ_dname, *dest;
1401 GList *paths;
1403 /* We can work out the template path from the initial name */
1404 oleaf = g_basename(initial);
1405 templ_dname = choices_find_path_load("Templates", "");
1406 if (!templ_dname)
1408 report_error(
1409 _("Error creating file: could not find the template for %s"),
1410 oleaf);
1411 return FALSE;
1414 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1415 g_free(templ_dname);
1417 dest = g_path_get_dirname(path);
1418 leaf = g_basename(path);
1419 paths = g_list_append(NULL, templ);
1421 action_copy(paths, dest, leaf, TRUE);
1423 g_list_free(paths);
1424 g_free(dest);
1425 g_free(templ);
1427 if (filer_exists(window_with_focus))
1428 display_set_autoselect(window_with_focus, leaf);
1430 return TRUE;
1433 static void do_send_to(gchar *templ)
1435 g_return_if_fail(send_to_paths != NULL);
1437 run_with_files(templ, send_to_paths);
1440 static void new_file_type(gchar *templ)
1442 const gchar *leaf;
1443 MIME_type *type;
1445 g_return_if_fail(window_with_focus != NULL);
1447 leaf = g_basename(templ);
1448 type = type_get_type(templ);
1450 savebox_show(_("Create"),
1451 make_path(window_with_focus->sym_path, leaf),
1452 type_to_icon(type),
1453 new_file_type_cb, GDK_ACTION_COPY);
1456 static void customise_send_to(gpointer data)
1458 GPtrArray *path;
1459 guchar *save;
1460 GString *dirs;
1461 int i;
1463 dirs = g_string_new(NULL);
1465 path = choices_list_dirs("");
1466 for (i = 0; i < path->len; i++)
1468 guchar *old = (guchar *) path->pdata[i];
1470 g_string_append(dirs, old);
1471 g_string_append(dirs, "SendTo\n");
1473 choices_free_list(path);
1475 save = choices_find_path_save("", "SendTo", TRUE);
1476 if (save)
1477 mkdir(save, 0777);
1479 info_message(
1480 _("The `Send To' menu provides a quick way to send some files "
1481 "to an application. The applications listed are those in "
1482 "the following directories:\n\n%s\n%s\n"
1483 "The `Send To' menu may be opened by Shift+Menu clicking "
1484 "over a file.\n\n"
1485 "Advanced use:\n"
1486 "You can also create subdirectories called "
1487 "`.text_html', `.text', etc which will only be "
1488 "shown for files of that type. `.group' is shown "
1489 "only when multiple files are selected."),
1490 dirs->str,
1491 save ? _("I'll show you your SendTo directory now; you should "
1492 "symlink (Ctrl+Shift drag) any applications you want "
1493 "into it.")
1494 : _("Your CHOICESPATH variable setting prevents "
1495 "customisations - sorry."));
1497 g_string_free(dirs, TRUE);
1499 if (save)
1500 filer_opendir(save, NULL, NULL);
1503 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1504 * to the menu.
1506 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1508 gchar *searchdir;
1509 GPtrArray *paths;
1510 int i;
1512 if (subtype)
1513 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1514 else if (type)
1515 searchdir = g_strdup_printf("SendTo/.%s", type);
1516 else
1517 searchdir = g_strdup("SendTo");
1519 paths = choices_list_dirs(searchdir);
1520 g_free(searchdir);
1522 for (i = 0; i < paths->len; i++)
1524 GList *widgets = NULL;
1525 guchar *dir = (guchar *) paths->pdata[i];
1527 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1528 (CallbackFn) do_send_to,
1529 FALSE, FALSE, TRUE);
1531 if (widgets)
1532 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1533 gtk_menu_item_new());
1535 g_list_free(widgets); /* TODO: Get rid of this */
1538 choices_free_list(paths);
1541 /* Scan the SendTo dir and create and show the Send To menu.
1542 * The 'paths' list and every path in it is claimed, and will be
1543 * freed later -- don't free it yourself!
1545 static void show_send_to_menu(GList *paths, GdkEvent *event)
1547 GtkWidget *menu, *item;
1549 menu = gtk_menu_new();
1551 if (g_list_length(paths) == 1)
1553 DirItem *item;
1555 item = diritem_new("");
1556 diritem_restat(paths->data, item, NULL);
1558 add_sendto(menu,
1559 item->mime_type->media_type,
1560 item->mime_type->subtype);
1562 add_sendto(menu, item->mime_type->media_type, NULL);
1564 diritem_free(item);
1566 else
1568 GList *rover;
1569 gboolean same=TRUE, same_media=TRUE;
1570 MIME_type *type=NULL;
1571 DirItem *item;
1573 item = diritem_new("");
1574 for(rover=paths; rover; rover=g_list_next(rover))
1576 diritem_restat(rover->data, item, NULL);
1577 if(!type)
1578 type=item->mime_type;
1579 else
1581 if(type!=item->mime_type)
1583 same=FALSE;
1584 if(strcmp(type->media_type,
1585 item->mime_type->media_type)!=0)
1587 same_media=FALSE;
1588 break;
1593 diritem_free(item);
1595 if(type)
1597 if(same)
1598 add_sendto(menu, type->media_type,
1599 type->subtype);
1600 if(same_media)
1601 add_sendto(menu, type->media_type, NULL);
1604 add_sendto(menu, "group", NULL);
1607 add_sendto(menu, NULL, NULL);
1609 item = gtk_menu_item_new_with_label(_("Customise"));
1610 g_signal_connect_swapped(item, "activate",
1611 G_CALLBACK(customise_send_to), NULL);
1612 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1614 if (send_to_paths)
1615 destroy_glist(&send_to_paths);
1617 send_to_paths = paths;
1619 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1621 popup_menu = menu;
1622 show_popup_menu(menu, event, 0);
1625 static void send_to(FilerWindow *filer_window)
1627 GList *paths;
1628 GdkEvent *event;
1630 paths = filer_selected_items(filer_window);
1631 event = gtk_get_current_event();
1633 /* Eats paths */
1634 show_send_to_menu(paths, event);
1636 gdk_event_free(event);
1639 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1641 const char *argv[] = {"sh", "-c", NULL, NULL};
1642 gboolean close = action;
1644 argv[2] = o_menu_xterm.value;
1646 g_return_if_fail(window_with_focus != NULL);
1648 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1649 gtk_widget_destroy(window_with_focus->window);
1652 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1654 g_return_if_fail(window_with_focus != NULL);
1656 filer_change_to(window_with_focus, home_dir, NULL);
1659 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1661 g_return_if_fail(window_with_focus != NULL);
1663 bookmarks_show_menu(window_with_focus);
1666 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1668 g_return_if_fail(window_with_focus != NULL);
1670 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1671 filer_change_to(window_with_focus,
1672 window_with_focus->real_path, NULL);
1673 else
1674 delayed_error(_("This is already the canonical name "
1675 "for this directory."));
1678 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1680 g_return_if_fail(window_with_focus != NULL);
1682 filer_open_parent(window_with_focus);
1685 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1687 g_return_if_fail(window_with_focus != NULL);
1689 change_to_parent(window_with_focus);
1692 static void resize(gpointer data, guint action, GtkWidget *widget)
1694 g_return_if_fail(window_with_focus != NULL);
1696 view_autosize(window_with_focus->view);
1699 static void new_window(gpointer data, guint action, GtkWidget *widget)
1701 g_return_if_fail(window_with_focus != NULL);
1703 if (o_unique_filer_windows.int_value)
1705 report_error(_("You can't open a second view onto "
1706 "this directory because the `Unique Windows' option "
1707 "is turned on in the Options window."));
1709 else
1710 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1713 static void close_window(gpointer data, guint action, GtkWidget *widget)
1715 g_return_if_fail(window_with_focus != NULL);
1717 if (!filer_window_delete(window_with_focus->window, NULL,
1718 window_with_focus))
1719 gtk_widget_destroy(window_with_focus->window);
1722 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1724 MiniType type = (MiniType) action;
1726 g_return_if_fail(window_with_focus != NULL);
1728 /* Item needs to remain selected... */
1729 if (type == MINI_SHELL)
1730 window_with_focus->temp_item_selected = FALSE;
1732 minibuffer_show(window_with_focus, type);
1735 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1737 if (action == HELP_ABOUT)
1738 infobox_new(app_dir);
1739 else if (action == HELP_DIR)
1740 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1741 else if (action == HELP_MANUAL)
1743 gchar *manual = NULL;
1745 if (current_lang)
1747 manual = g_strconcat(app_dir, "/Help/Manual-",
1748 current_lang, ".html", NULL);
1749 if (access(manual, F_OK))
1750 null_g_free(&manual);
1753 if (!manual)
1754 manual = g_strconcat(app_dir,
1755 "/Help/Manual.html", NULL);
1757 run_by_path(manual);
1759 g_free(manual);
1761 else
1762 g_warning("Unknown help action %d\n", action);
1765 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1766 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1768 GList *items, *item;
1770 items = gtk_container_get_children(GTK_CONTAINER(menu));
1772 item = g_list_nth(items, from);
1773 while (item && n--)
1775 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1776 item = item->next;
1778 g_list_free(items);
1781 static void save_menus(void)
1783 char *menurc;
1785 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1786 if (menurc)
1788 gtk_accel_map_save(menurc);
1789 g_free(menurc);
1793 static void select_nth_item(GtkMenuShell *shell, int n)
1795 GList *items;
1796 GtkWidget *item;
1798 items = gtk_container_get_children(GTK_CONTAINER(shell));
1799 item = g_list_nth_data(items, n);
1801 g_return_if_fail(item != NULL);
1803 g_list_free(items);
1805 gtk_menu_shell_select_item(shell, item);
1808 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1810 DirItem *item;
1811 const guchar *path;
1812 int n_selected;
1813 ViewIter iter;
1815 g_return_if_fail(window_with_focus != NULL);
1817 n_selected = view_count_selected(window_with_focus->view);
1819 if (n_selected < 1)
1821 const char *prompt;
1823 switch (action)
1825 case FILE_COPY_ITEM:
1826 prompt = _("Copy ... ?");
1827 break;
1828 case FILE_RENAME_ITEM:
1829 prompt = _("Rename ... ?");
1830 break;
1831 case FILE_LINK_ITEM:
1832 prompt = _("Symlink ... ?");
1833 break;
1834 case FILE_OPEN_FILE:
1835 prompt = _("Shift Open ... ?");
1836 break;
1837 case FILE_PROPERTIES:
1838 prompt = _("Properties of ... ?");
1839 break;
1840 case FILE_SET_TYPE:
1841 prompt = _("Set type of ... ?");
1842 break;
1843 case FILE_RUN_ACTION:
1844 prompt = _("Set run action for ... ?");
1845 break;
1846 case FILE_SET_ICON:
1847 prompt = _("Set icon for ... ?");
1848 break;
1849 case FILE_SEND_TO:
1850 prompt = _("Send ... to ... ?");
1851 break;
1852 case FILE_DELETE:
1853 prompt = _("DELETE ... ?");
1854 break;
1855 case FILE_USAGE:
1856 prompt = _("Count the size of ... ?");
1857 break;
1858 case FILE_CHMOD_ITEMS:
1859 prompt = _("Set permissions on ... ?");
1860 break;
1861 case FILE_FIND:
1862 prompt = _("Search inside ... ?");
1863 break;
1864 case FILE_OPEN_VFS_AVFS:
1865 prompt = _("Look inside ... ?");
1866 break;
1867 default:
1868 g_warning("Unknown action!");
1869 return;
1871 filer_target_mode(window_with_focus, target_callback,
1872 GINT_TO_POINTER(action), prompt);
1873 return;
1876 switch (action)
1878 case FILE_SEND_TO:
1879 send_to(window_with_focus);
1880 return;
1881 case FILE_DELETE:
1882 delete(window_with_focus);
1883 return;
1884 case FILE_USAGE:
1885 usage(window_with_focus);
1886 return;
1887 case FILE_CHMOD_ITEMS:
1888 chmod_items(window_with_focus);
1889 return;
1890 case FILE_SET_TYPE:
1891 set_type_items(window_with_focus);
1892 return;
1893 case FILE_FIND:
1894 find(window_with_focus);
1895 return;
1896 case FILE_PROPERTIES:
1898 GList *items;
1900 items = filer_selected_items(window_with_focus);
1901 infobox_show_list(items);
1902 destroy_glist(&items);
1903 return;
1905 case FILE_RENAME_ITEM:
1906 if (n_selected > 1)
1908 GList *items = NULL;
1909 ViewIter iter;
1911 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1912 while ((item = iter.next(&iter)))
1913 items = g_list_prepend(items, item->leafname);
1914 items = g_list_reverse(items);
1916 bulk_rename(window_with_focus->sym_path, items);
1917 g_list_free(items);
1918 return;
1920 break; /* Not a bulk rename... see below */
1921 default:
1922 break;
1925 /* All the following actions require exactly one file selected */
1927 if (n_selected > 1)
1929 report_error(_("You cannot do this to more than "
1930 "one item at a time"));
1931 return;
1934 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1936 item = iter.next(&iter);
1937 g_return_if_fail(item != NULL);
1938 /* iter may be passed to filer_openitem... */
1940 if (item->base_type == TYPE_UNKNOWN)
1941 item = dir_update_item(window_with_focus->directory,
1942 item->leafname);
1944 if (!item)
1946 report_error(_("Item no longer exists!"));
1947 return;
1950 path = make_path(window_with_focus->sym_path, item->leafname);
1952 switch (action)
1954 case FILE_COPY_ITEM:
1955 src_dest_action_item(path, di_image(item),
1956 _("Copy"), copy_cb,
1957 GDK_ACTION_COPY);
1958 break;
1959 case FILE_RENAME_ITEM:
1960 src_dest_action_item(path, di_image(item),
1961 _("Rename"), rename_cb,
1962 GDK_ACTION_MOVE);
1963 break;
1964 case FILE_LINK_ITEM:
1965 src_dest_action_item(path, di_image(item),
1966 _("Symlink"), link_cb,
1967 GDK_ACTION_LINK);
1968 break;
1969 case FILE_OPEN_FILE:
1970 filer_openitem(window_with_focus, &iter,
1971 OPEN_SAME_WINDOW | OPEN_SHIFT);
1972 break;
1973 case FILE_RUN_ACTION:
1974 run_action(item);
1975 break;
1976 case FILE_SET_ICON:
1977 icon_set_handler_dialog(item, path);
1978 break;
1979 case FILE_OPEN_VFS_AVFS:
1980 open_vfs_avfs(window_with_focus, item);
1981 break;
1982 default:
1983 g_warning("Unknown action!");
1984 return;
1988 static void show_key_help(GtkWidget *button, gpointer data)
1990 gboolean can_change_accels;
1992 g_object_get(G_OBJECT(gtk_settings_get_default()),
1993 "gtk-can-change-accels", &can_change_accels,
1994 NULL);
1996 if (!can_change_accels)
1998 info_message(_("User-definable shortcuts are disabled by "
1999 "default in Gtk2, and you have not enabled "
2000 "them. You can turn this feature on by:\n\n"
2001 "1) using an XSettings manager, such as ROX-Session "
2002 "or gnome-settings-daemon, or\n\n"
2003 "2) adding this line to ~/.gtkrc-2.0:\n"
2004 "\tgtk-can-change-accels = 1\n"
2005 "\t(this only works if NOT using XSETTINGS)"));
2006 return;
2009 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2010 "- Open the menu over a filer window,\n"
2011 "- Move the pointer over the item you want to use,\n"
2012 "- Press the key you want attached to it.\n\n"
2013 "The key will appear next to the menu item and you can just press "
2014 "that key without opening the menu in future."));
2017 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2019 GtkWidget *button, *align;
2021 g_return_val_if_fail(option == NULL, NULL);
2023 align = gtk_alignment_new(0, 0.5, 0, 0);
2024 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2025 gtk_container_add(GTK_CONTAINER(align), button);
2026 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2028 return g_list_append(NULL, align);