Add log viewer
[rox-filer.git] / ROX-Filer / src / menu.c
blobf6f0adb6433a934f8bd25a6738396b3f16704f2d
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* menu.c - code for handling the popup menus */
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/wait.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <dirent.h>
33 #include <gtk/gtk.h>
35 #include "global.h"
37 #include "menu.h"
38 #include "run.h"
39 #include "action.h"
40 #include "filer.h"
41 #include "pixmaps.h"
42 #include "type.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "options.h"
46 #include "choices.h"
47 #include "gtksavebox.h"
48 #include "mount.h"
49 #include "minibuffer.h"
50 #include "i18n.h"
51 #include "main.h"
52 #include "pinboard.h"
53 #include "dir.h"
54 #include "diritem.h"
55 #include "appmenu.h"
56 #include "usericons.h"
57 #include "infobox.h"
58 #include "view_iface.h"
59 #include "display.h"
60 #include "bookmarks.h"
61 #include "panel.h"
62 #include "bulk_rename.h"
63 #include "xtypes.h"
65 typedef enum {
66 FILE_COPY_ITEM,
67 FILE_RENAME_ITEM,
68 FILE_LINK_ITEM,
69 FILE_OPEN_FILE,
70 FILE_PROPERTIES,
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_SET_TYPE,
79 } FileOp;
81 typedef void (*ActionFn)(GList *paths,
82 const char *dest_dir, const char *leaf, int quiet);
83 typedef void MenuCallback(GtkWidget *widget, gpointer data);
85 typedef gboolean (*SaveCb)(GObject *savebox,
86 const gchar *current, const gchar *new);
88 GtkAccelGroup *filer_keys = NULL;
89 static gboolean filer_keys_need_init = TRUE;
91 static GtkWidget *popup_menu = NULL; /* Currently open menu */
93 static gint updating_menu = 0; /* Non-zero => ignore activations */
94 static GList *send_to_paths = NULL;
96 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
98 /* Static prototypes */
100 static void save_menus(void);
101 static void menu_closed(GtkWidget *widget);
102 static void shade_file_menu_items(gboolean shaded);
103 static void savebox_show(const gchar *action, const gchar *path,
104 MaskedPixmap *image, SaveCb callback,
105 GdkDragAction dnd_action);
106 static gint save_to_file(GObject *savebox,
107 const gchar *pathname, gpointer data);
108 static gboolean action_with_leaf(ActionFn action,
109 const gchar *current, const gchar *new);
110 static gboolean link_cb(GObject *savebox,
111 const gchar *initial, const gchar *path);
112 static void select_nth_item(GtkMenuShell *shell, int n);
113 static void new_file_type(gchar *templ);
114 static void do_send_to(gchar *templ);
115 static void show_send_to_menu(GList *paths, GdkEvent *event);
116 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
118 /* Note that for most of these callbacks none of the arguments are used. */
120 static void view_type(gpointer data, guint action, GtkWidget *widget);
122 /* (action used in these three - DetailsType) */
123 static void change_size(gpointer data, guint action, GtkWidget *widget);
124 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
125 static void set_with(gpointer data, guint action, GtkWidget *widget);
127 static void set_sort(gpointer data, guint action, GtkWidget *widget);
128 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
130 static void filter_directories(gpointer data, guint action, GtkWidget *widget);
131 static void hidden(gpointer data, guint action, GtkWidget *widget);
132 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
133 static void refresh(gpointer data, guint action, GtkWidget *widget);
134 static void save_settings(gpointer data, guint action, GtkWidget *widget);
136 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
138 static void select_all(gpointer data, guint action, GtkWidget *widget);
139 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
140 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
141 static void new_directory(gpointer data, guint action, GtkWidget *widget);
142 static void new_file(gpointer data, guint action, GtkWidget *widget);
143 static void customise_new(gpointer data);
144 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
146 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent(gpointer data, guint action, GtkWidget *widget);
148 static void home_directory(gpointer data, guint action, GtkWidget *widget);
149 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
150 static void show_log(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_filter_dirs_menu;/* The Filter Dirs item */
169 static GtkWidget *filer_reverse_menu; /* The Reversed item */
170 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
171 static GtkWidget *filer_new_window; /* The New Window item */
172 static GtkWidget *filer_new_menu; /* The New submenu */
173 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
174 static GtkWidget *filer_set_type; /* Set type item */
176 #undef N_
177 #define N_(x) x
179 static GtkItemFactoryEntry filer_menu_def[] = {
180 {N_("Display"), NULL, NULL, 0, "<Branch>"},
181 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
182 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
183 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
184 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
185 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
186 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
187 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
188 {">", NULL, NULL, 0, "<Separator>"},
189 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
190 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
191 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
192 {">", NULL, NULL, 0, "<Separator>"},
193 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
194 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
195 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
196 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
197 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
198 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
199 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
200 {">", NULL, NULL, 0, "<Separator>"},
201 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
202 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
203 {">" N_("Filter Directories With Files"), NULL, filter_directories, 0, "<ToggleItem>"},
204 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
205 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
206 {">" N_("Save Current Display Settings..."), NULL, save_settings, 0, NULL},
207 {N_("File"), NULL, NULL, 0, "<Branch>"},
208 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
209 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
210 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
211 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
212 {">", NULL, NULL, 0, "<Separator>"},
213 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
214 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
215 {">", NULL, NULL, 0, "<Separator>"},
216 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
217 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
218 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
219 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
220 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
221 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
222 {">", NULL, NULL, 0, "<Separator>"},
223 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
224 {N_("Select"), NULL, NULL, 0, "<Branch>"},
225 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
226 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
227 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
228 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
229 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
230 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
231 {N_("New"), NULL, NULL, 0, "<Branch>"},
232 {">" N_("Directory"), NULL, new_directory, 0, NULL},
233 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
234 {">" N_("Customise Menu..."), NULL, customise_new, 0, NULL},
235 {N_("Window"), NULL, NULL, 0, "<Branch>"},
236 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
237 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
238 {">" N_("New Window"), NULL, new_window, 0, NULL},
239 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
240 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
241 {">" N_("Show Log"), NULL, show_log, 0, "<StockItem>", GTK_STOCK_INFO},
242 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
243 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
244 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
246 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
247 {">", NULL, NULL, 0, "<Separator>"},
248 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
249 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
250 {">" N_("Terminal Here"), "grave", xterm_here, FALSE, NULL},
251 {">" N_("Switch to Terminal"), NULL, xterm_here, TRUE, NULL},
252 {N_("Help"), NULL, NULL, 0, "<Branch>"},
253 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
254 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
255 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
259 #define GET_MENU_ITEM(var, menu) \
260 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
262 #define GET_SMENU_ITEM(var, menu, sub) \
263 do { \
264 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
265 var = gtk_item_factory_get_widget(item_factory, tmp); \
266 g_free(tmp); \
267 } while (0)
269 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
270 do { \
271 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
272 var = gtk_item_factory_get_widget(item_factory, tmp); \
273 g_free(tmp); \
274 } while (0)
276 /* Returns TRUE if the keys were installed (first call only) */
277 gboolean ensure_filer_menu(void)
279 GList *items;
280 guchar *tmp;
281 GtkWidget *item;
282 GtkItemFactory *item_factory;
284 if (!filer_keys_need_init)
285 return FALSE;
286 filer_keys_need_init = FALSE;
288 item_factory = menu_create(filer_menu_def,
289 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
290 "<filer>", filer_keys);
292 GET_MENU_ITEM(filer_menu, "filer");
293 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
294 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
295 GET_SSMENU_ITEM(filer_filter_dirs_menu, "filer", "Display", "Filter Directories With Files");
296 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
297 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
298 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
299 "Show Thumbnails");
300 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
301 filer_set_type = GTK_BIN(item)->child;
303 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
304 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
305 filer_follow_sym = GTK_BIN(item)->child;
307 /* File '' label... */
308 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
309 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
310 g_list_free(items);
312 /* Shift Open... label */
313 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
314 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
315 g_list_free(items);
317 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
318 filer_new_window = GTK_BIN(item)->child;
320 g_signal_connect(filer_menu, "unmap_event",
321 G_CALLBACK(menu_closed), NULL);
322 g_signal_connect(filer_file_menu, "unmap_event",
323 G_CALLBACK(menu_closed), NULL);
325 g_signal_connect(filer_keys, "accel_changed",
326 G_CALLBACK(save_menus), NULL);
328 return TRUE;
331 void menu_init(void)
333 char *menurc;
335 menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
336 if (menurc)
338 gtk_accel_map_load(menurc);
339 g_free(menurc);
342 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
343 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
344 option_add_int(&o_menu_quick, "menu_quick", FALSE);
345 option_add_saver(save_menus);
347 option_register_widget("menu-set-keys", set_keys_button);
349 filer_keys = gtk_accel_group_new();
352 /* Name is in the form "<panel>" */
353 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
354 const gchar *name, GtkAccelGroup *keys)
356 GtkItemFactory *item_factory;
357 GtkItemFactoryEntry *translated;
359 if (!keys)
361 keys = gtk_accel_group_new();
362 gtk_accel_group_lock(keys);
365 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
367 translated = translate_entries(def, n_entries);
368 gtk_item_factory_create_items(item_factory, n_entries,
369 translated, NULL);
370 free_translated_entries(translated, n_entries);
372 return item_factory;
375 /* Prevent the user from setting a short-cut on this item */
376 static void menuitem_no_shortcuts(GtkWidget *item)
378 /* XXX */
379 #if 0
380 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
382 _gtk_widget_set_accel_path(item, NULL, NULL);
383 null_g_free(&menuitem->accel_path);
384 #endif
387 /* Shade items that only work on single files */
388 static void shade_file_menu_items(gboolean shaded)
390 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
391 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
392 menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
393 menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
396 /* 'data' is an array of three ints:
397 * [ pointer_x, pointer_y, item_under_pointer ]
399 void position_menu(GtkMenu *menu, gint *x, gint *y,
400 gboolean *push_in, gpointer data)
402 int *pos = (int *) data;
403 GtkRequisition requisition;
404 GList *items, *next;
405 int y_shift = 0;
406 int item = pos[2];
408 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
410 while (item >= 0 && next)
412 int h = ((GtkWidget *) next->data)->requisition.height;
414 if (item > 0)
415 y_shift += h;
416 else
417 y_shift += h / 2;
419 next = next->next;
420 item--;
423 g_list_free(items);
425 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
427 *x = pos[0] - (requisition.width * 7 / 8);
428 *y = pos[1] - y_shift;
430 *x = CLAMP(*x, 0, screen_width - requisition.width);
431 *y = CLAMP(*y, 0, screen_height - requisition.height);
433 *push_in = FALSE;
436 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
437 MenuIconStyle style)
439 GtkWidget *item;
441 if (style != MIS_NONE && di_image(ditem))
443 GdkPixbuf *pixbuf;
444 MaskedPixmap *image;
446 image = di_image(ditem);
448 switch (style)
450 case MIS_LARGE:
451 pixbuf = image->pixbuf;
452 break;
453 default:
454 if (!image->sm_pixbuf)
455 pixmap_make_small(image);
456 pixbuf = image->sm_pixbuf;
457 break;
460 item = gtk_image_menu_item_new_with_label(label);
461 /* TODO: Find a way to allow short-cuts */
462 menuitem_no_shortcuts(item);
464 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
465 gtk_image_new_from_pixbuf(pixbuf));
466 gtk_widget_show_all(item);
468 else
469 item = gtk_menu_item_new_with_label(label);
471 return item;
474 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
475 MenuIconStyle style, CallbackFn func,
476 gboolean separator, gboolean strip_ext,
477 gboolean recurse)
479 GList *widgets = NULL;
480 DirItem *ditem;
481 int i;
482 GtkWidget *item;
483 char *dname = NULL;
484 GPtrArray *names;
486 dname = pathdup(dir_name);
488 names = list_dir(dname);
489 if (!names)
490 goto out;
492 for (i = 0; i < names->len; i++)
494 char *leaf = names->pdata[i];
495 gchar *fname;
497 if (separator)
499 item = gtk_menu_item_new();
500 widgets = g_list_append(widgets, item);
501 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
502 separator = FALSE;
505 fname = g_strconcat(dname, "/", leaf, NULL);
507 /* Strip off extension, if any */
508 if (strip_ext)
510 char *dot;
511 dot = strchr(leaf, '.');
512 if (dot)
513 *dot = '\0';
516 ditem = diritem_new("");
517 diritem_restat(fname, ditem, NULL);
519 item = make_send_to_item(ditem, leaf, style);
521 g_free(leaf);
523 /* If it is a directory (but NOT an AppDir) and we are
524 * recursing then set up a sub menu.
526 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
527 !(ditem->flags & ITEM_FLAG_APPDIR))
529 GtkWidget *sub;
530 GList *new_widgets;
532 sub = gtk_menu_new();
533 new_widgets = menu_from_dir(sub, fname, style, func,
534 separator, strip_ext, TRUE);
535 g_list_free(new_widgets);
536 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
538 else
539 g_signal_connect_swapped(item, "activate",
540 G_CALLBACK(func), fname);
542 diritem_free(ditem);
544 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
545 g_signal_connect_swapped(item, "destroy",
546 G_CALLBACK(g_free), fname);
548 widgets = g_list_append(widgets, item);
551 g_ptr_array_free(names, TRUE);
552 out:
553 g_free(dname);
555 return widgets;
558 /* Scan the templates dir and create entries for the New menu */
559 static void update_new_files_menu(MenuIconStyle style)
561 static GList *widgets = NULL;
563 gchar *templ_dname = NULL;
565 if (widgets)
567 GList *next;
569 for (next = widgets; next; next = next->next)
570 gtk_widget_destroy((GtkWidget *) next->data);
572 g_list_free(widgets);
573 widgets = NULL;
576 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
577 if (templ_dname)
579 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
580 (CallbackFn) new_file_type, TRUE, TRUE,
581 FALSE);
582 g_free(templ_dname);
584 gtk_widget_show_all(filer_new_menu);
587 /* 'item' is the number of the item to appear under the pointer. */
588 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
590 int pos[3];
591 int button = 0;
592 guint32 time = 0;
594 if (event && (event->type == GDK_BUTTON_PRESS ||
595 event->type == GDK_BUTTON_RELEASE))
597 GdkEventButton *bev = (GdkEventButton *) event;
599 pos[0] = bev->x_root;
600 pos[1] = bev->y_root;
601 button = bev->button;
602 time = bev->time;
604 else if (event && event->type == GDK_KEY_PRESS)
606 GdkEventKey *kev = (GdkEventKey *) event;
608 get_pointer_xy(pos, pos + 1);
609 time = kev->time;
611 else
612 get_pointer_xy(pos, pos + 1);
614 pos[2] = item;
616 gtk_widget_show_all(menu);
617 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
618 position_menu, (gpointer) pos, button, time);
619 select_nth_item(GTK_MENU_SHELL(menu), item);
622 /* Hide the popup menu, if any */
623 void menu_popdown(void)
625 if (popup_menu)
626 gtk_menu_popdown(GTK_MENU(popup_menu));
629 static MenuIconStyle get_menu_icon_style(void)
631 MenuIconStyle mis;
632 int display;
634 mis = o_menu_iconsize.int_value;
636 switch (mis)
638 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
639 return mis;
640 default:
641 break;
644 if (mis == MIS_CURRENT && window_with_focus)
646 switch (window_with_focus->display_style)
648 case HUGE_ICONS:
649 case LARGE_ICONS:
650 return MIS_LARGE;
651 case SMALL_ICONS:
652 return MIS_SMALL;
653 default:
654 break;
658 display = o_display_size.int_value;
659 switch (display)
661 case HUGE_ICONS:
662 case LARGE_ICONS:
663 return MIS_LARGE;
664 case SMALL_ICONS:
665 return MIS_SMALL;
666 default:
667 break;
670 return MIS_SMALL;
673 /* iter->peek() is the clicked item, or NULL if none */
674 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
676 DirItem *file_item = NULL;
677 GdkModifierType state = 0;
678 int n_selected;
679 int n_added = 0;
681 g_return_if_fail(event != NULL);
683 n_selected = view_count_selected(filer_window->view);
685 ensure_filer_menu();
687 updating_menu++;
689 /* Remove previous AppMenu, if any */
690 appmenu_remove();
692 window_with_focus = filer_window;
694 if (event->type == GDK_BUTTON_PRESS)
695 state = ((GdkEventButton *) event)->state;
696 else if (event->type == GDK_KEY_PRESS)
697 state = ((GdkEventKey *) event)->state;
699 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
701 filer_window->temp_item_selected = TRUE;
702 view_set_selected(filer_window->view, iter, TRUE);
703 n_selected = view_count_selected(filer_window->view);
705 else
707 filer_window->temp_item_selected = FALSE;
710 /* Short-cut to the Send To menu */
711 if (state & GDK_SHIFT_MASK)
713 GList *paths;
715 updating_menu--;
717 if (n_selected == 0)
719 report_error(
720 _("You should Shift+Menu click over a file to "
721 "send it somewhere"));
722 return;
725 paths = filer_selected_items(filer_window);
727 show_send_to_menu(paths, event); /* (paths eaten) */
729 return;
733 GtkWidget *file_label, *file_menu;
734 GString *buffer;
735 DirItem *item;
737 file_label = filer_file_item;
738 file_menu = filer_file_menu;
739 gtk_check_menu_item_set_active(
740 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
741 filer_window->show_thumbs);
742 gtk_check_menu_item_set_active(
743 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
744 filer_window->show_hidden);
745 gtk_check_menu_item_set_active(
746 GTK_CHECK_MENU_ITEM(filer_filter_dirs_menu),
747 filer_window->filter_directories);
748 gtk_check_menu_item_set_active(
749 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
750 filer_window->sort_order != GTK_SORT_ASCENDING);
751 gtk_check_menu_item_set_active(
752 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
753 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
754 buffer = g_string_new(NULL);
756 switch (n_selected)
758 case 0:
759 g_string_assign(buffer, _("Next Click"));
760 shade_file_menu_items(FALSE);
761 break;
762 case 1:
763 item = filer_selected_item(filer_window);
764 if (item->base_type == TYPE_UNKNOWN)
765 dir_update_item(filer_window->directory,
766 item->leafname);
767 shade_file_menu_items(FALSE);
768 file_item = filer_selected_item(filer_window);
769 g_string_printf(buffer, _("%s '%s'"),
770 basetype_name(file_item),
771 g_utf8_validate(file_item->leafname,
772 -1, NULL)
773 ? file_item->leafname
774 : _("(bad utf-8)"));
775 if (!can_set_run_action(file_item))
776 menu_set_items_shaded(filer_file_menu,
777 TRUE, 8, 1);
778 break;
779 default:
780 shade_file_menu_items(TRUE);
781 g_string_printf(buffer, _("%d items"),
782 n_selected);
783 break;
785 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
786 g_string_free(buffer, TRUE);
788 menu_show_shift_action(file_shift_item, file_item,
789 n_selected == 0);
790 if (file_item)
791 n_added = appmenu_add(make_path(filer_window->sym_path,
792 file_item->leafname),
793 file_item, filer_file_menu);
796 update_new_files_menu(get_menu_icon_style());
798 gtk_widget_set_sensitive(filer_new_window,
799 !o_unique_filer_windows.int_value);
800 gtk_widget_set_sensitive(filer_follow_sym,
801 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
802 gtk_widget_set_sensitive(filer_set_type,
803 xattr_supported(filer_window->real_path));
805 if (n_selected && o_menu_quick.int_value)
806 popup_menu = (state & GDK_CONTROL_MASK)
807 ? filer_menu
808 : filer_file_menu;
809 else
810 popup_menu = (state & GDK_CONTROL_MASK)
811 ? filer_file_menu
812 : filer_menu;
814 updating_menu--;
816 show_popup_menu(popup_menu, event,
817 popup_menu == filer_file_menu ? n_added : 1);
820 static void menu_closed(GtkWidget *widget)
822 if (window_with_focus == NULL || widget != popup_menu)
823 return; /* Close panel item chosen? */
825 popup_menu = NULL;
827 if (window_with_focus->temp_item_selected)
829 view_clear_selection(window_with_focus->view);
830 window_with_focus->temp_item_selected = FALSE;
833 appmenu_remove();
836 static void target_callback(FilerWindow *filer_window,
837 ViewIter *iter,
838 gpointer action)
840 g_return_if_fail(filer_window != NULL);
842 window_with_focus = filer_window;
844 /* Don't grab the primary selection */
845 filer_window->temp_item_selected = TRUE;
847 view_wink_item(filer_window->view, iter);
848 view_select_only(filer_window->view, iter);
849 file_op(NULL, GPOINTER_TO_INT(action), NULL);
851 view_clear_selection(filer_window->view);
852 filer_window->temp_item_selected = FALSE;
855 /* Set the text of the 'Shift Open...' menu item.
856 * If icon is NULL, reset the text and also shade it, unless 'next'.
858 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
860 guchar *shift_action = NULL;
862 if (item)
864 if (item->flags & ITEM_FLAG_MOUNT_POINT)
866 if (item->flags & ITEM_FLAG_MOUNTED)
867 shift_action = N_("Unmount");
868 else
869 shift_action = N_("Open unmounted");
871 else if (item->flags & ITEM_FLAG_SYMLINK)
872 shift_action = N_("Show Target");
873 else if (item->base_type == TYPE_DIRECTORY)
874 shift_action = N_("Look Inside");
875 else if (item->base_type == TYPE_FILE)
876 shift_action = N_("Open As Text");
878 gtk_label_set_text(GTK_LABEL(menu_item),
879 shift_action ? _(shift_action)
880 : _("Shift Open"));
881 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
884 /* Actions */
886 static void view_type(gpointer data, guint action, GtkWidget *widget)
888 ViewType view_type = (ViewType) action;
890 g_return_if_fail(window_with_focus != NULL);
892 if (view_type == VIEW_TYPE_COLLECTION)
893 display_set_layout(window_with_focus,
894 window_with_focus->display_style_wanted,
895 DETAILS_NONE, FALSE);
897 filer_set_view_type(window_with_focus, (ViewType) action);
900 static void change_size(gpointer data, guint action, GtkWidget *widget)
902 g_return_if_fail(window_with_focus != NULL);
904 display_change_size(window_with_focus, action == 1);
907 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
909 g_return_if_fail(window_with_focus != NULL);
911 if (updating_menu)
912 return;
914 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
915 display_set_layout(window_with_focus,
916 window_with_focus->display_style,
917 window_with_focus->details_type, FALSE);
918 else
919 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
920 window_with_focus->details_type, FALSE);
923 static void set_with(gpointer data, guint action, GtkWidget *widget)
925 DisplayStyle size;
927 g_return_if_fail(window_with_focus != NULL);
929 size = window_with_focus->display_style_wanted;
931 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
932 display_set_layout(window_with_focus, size, action, FALSE);
935 static void set_sort(gpointer data, guint action, GtkWidget *widget)
937 if (updating_menu)
938 return;
940 g_return_if_fail(window_with_focus != NULL);
942 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
945 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
947 GtkSortType order;
949 if (updating_menu)
950 return;
952 g_return_if_fail(window_with_focus != NULL);
954 order = window_with_focus->sort_order;
955 if (order == GTK_SORT_ASCENDING)
956 order = GTK_SORT_DESCENDING;
957 else
958 order = GTK_SORT_ASCENDING;
960 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
961 order);
964 static void hidden(gpointer data, guint action, GtkWidget *widget)
966 if (updating_menu)
967 return;
969 g_return_if_fail(window_with_focus != NULL);
971 display_set_hidden(window_with_focus,
972 !window_with_focus->show_hidden);
975 static void filter_directories(gpointer data, guint action, GtkWidget *widget)
977 if (updating_menu)
978 return;
980 g_return_if_fail(window_with_focus != NULL);
982 display_set_filter_directories(window_with_focus,
983 !window_with_focus->filter_directories);
986 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
988 if (updating_menu)
989 return;
991 g_return_if_fail(window_with_focus != NULL);
993 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
996 static void refresh(gpointer data, guint action, GtkWidget *widget)
998 g_return_if_fail(window_with_focus != NULL);
1000 filer_refresh(window_with_focus);
1003 static void save_settings(gpointer data, guint action, GtkWidget *widget)
1005 g_return_if_fail(window_with_focus != NULL);
1007 filer_save_settings(window_with_focus);
1010 static void delete(FilerWindow *filer_window)
1012 GList *paths;
1013 paths = filer_selected_items(filer_window);
1014 action_delete(paths);
1015 destroy_glist(&paths);
1018 static void usage(FilerWindow *filer_window)
1020 GList *paths;
1021 paths = filer_selected_items(filer_window);
1022 action_usage(paths);
1023 destroy_glist(&paths);
1026 static void chmod_items(FilerWindow *filer_window)
1028 GList *paths;
1029 paths = filer_selected_items(filer_window);
1030 action_chmod(paths, FALSE, NULL);
1031 destroy_glist(&paths);
1034 static void set_type_items(FilerWindow *filer_window)
1036 GList *paths, *p;
1037 int npass=0, nfail=0;
1039 paths = filer_selected_items(filer_window);
1040 for(p=paths; p; p=g_list_next(p)) {
1041 if(xattr_supported((const char *) p->data))
1042 npass++;
1043 else
1044 nfail++;
1046 if(npass==0)
1047 report_error(_("Extended attributes, used to store types, are not supported for this "
1048 "file or files.\n"
1049 "This may be due to lack of support from the filesystem or the C library, "
1050 "or it may simply be that the filesystem needs to be mounted with "
1051 "the right mount option ('user_xattr' on Linux)."));
1052 else if(nfail>0)
1053 report_error(_("Setting type not supported for some of these files"));
1054 if(npass>0)
1055 action_settype(paths, FALSE, NULL);
1056 destroy_glist(&paths);
1059 static void find(FilerWindow *filer_window)
1061 GList *paths;
1062 paths = filer_selected_items(filer_window);
1063 action_find(paths);
1064 destroy_glist(&paths);
1067 /* This creates a new savebox widget, and allows the user to pick a new path
1068 * for the file.
1069 * Once the new path has been picked, the callback will be called with
1070 * both the current and new paths.
1071 * NOTE: This function unrefs 'image'!
1073 static void savebox_show(const gchar *action, const gchar *path,
1074 MaskedPixmap *image, SaveCb callback,
1075 GdkDragAction dnd_action)
1077 GtkWidget *savebox = NULL;
1078 GtkWidget *check_relative = NULL;
1080 g_return_if_fail(image != NULL);
1082 savebox = gtk_savebox_new(action);
1083 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1085 if (callback == link_cb)
1087 check_relative = gtk_check_button_new_with_mnemonic(
1088 _("_Relative link"));
1089 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1090 TRUE);
1092 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1093 gtk_tooltips_set_tip(tooltips, check_relative,
1094 _("If on, the symlink will store the path from the "
1095 "symlink to the target file. Use this if the symlink "
1096 "and the target will be moved together.\n"
1097 "If off, the path from the root directory is stored - "
1098 "use this if the symlink may move but the target will "
1099 "stay put."), NULL);
1100 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1101 check_relative, FALSE, TRUE, 0);
1102 gtk_widget_show(check_relative);
1105 g_signal_connect(savebox, "save_to_file",
1106 G_CALLBACK(save_to_file), NULL);
1108 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1109 g_strdup(path), g_free);
1110 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1111 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1113 gtk_window_set_title(GTK_WINDOW(savebox), action);
1115 if (g_utf8_validate(path, -1, NULL))
1116 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1117 else
1119 gchar *u8, *dir;
1120 dir = g_path_get_dirname(path);
1121 u8 = to_utf8(g_basename(path));
1122 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1123 make_path(dir, u8));
1124 g_free(u8);
1125 g_free(dir);
1127 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1128 g_object_unref(image);
1130 gtk_widget_show(savebox);
1133 static gint save_to_file(GObject *savebox,
1134 const gchar *pathname, gpointer data)
1136 SaveCb callback;
1137 const gchar *current_path;
1139 callback = g_object_get_data(savebox, "action_callback");
1140 current_path = g_object_get_data(savebox, "current_path");
1142 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1143 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1145 return callback(savebox, current_path, pathname)
1146 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1149 static gboolean copy_cb(GObject *savebox,
1150 const gchar *current, const gchar *new)
1152 return action_with_leaf(action_copy, current, new);
1155 static gboolean action_with_leaf(ActionFn action,
1156 const gchar *current, const gchar *new)
1158 const char *leaf;
1159 char *new_dir;
1160 GList *local_paths;
1162 if (new[0] != '/')
1164 report_error(_("New pathname is not absolute"));
1165 return FALSE;
1168 if (new[strlen(new) - 1] == '/')
1170 new_dir = g_strdup(new);
1171 leaf = NULL;
1173 else
1175 const gchar *slash;
1177 slash = strrchr(new, '/');
1178 new_dir = g_strndup(new, slash - new);
1179 leaf = slash + 1;
1182 local_paths = g_list_append(NULL, (gchar *) current);
1183 action(local_paths, new_dir, leaf, -1);
1184 g_list_free(local_paths);
1186 g_free(new_dir);
1188 return TRUE;
1191 /* Open a savebox to act on the selected file.
1192 * Call 'callback' later to perform the operation.
1194 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1195 const gchar *action, SaveCb callback,
1196 GdkDragAction dnd_action)
1198 g_object_ref(image);
1199 savebox_show(action, path, image, callback, dnd_action);
1202 static gboolean rename_cb(GObject *savebox,
1203 const gchar *current, const gchar *new)
1205 return action_with_leaf(action_move, current, new);
1208 static gboolean link_cb(GObject *savebox,
1209 const gchar *initial, const gchar *path)
1211 GtkToggleButton *check_relative;
1212 struct stat info;
1213 int err;
1214 gchar *link_path;
1216 check_relative = g_object_get_data(savebox, "check_relative");
1218 if (gtk_toggle_button_get_active(check_relative))
1219 link_path = get_relative_path(path, initial);
1220 else
1221 link_path = g_strdup(initial);
1223 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1225 GtkWidget *box, *button;
1226 gint ans;
1228 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1229 GTK_BUTTONS_CANCEL,
1230 _("Symlink from '%s' already exists. "
1231 "Replace it with a link to '%s'?"),
1232 path, link_path);
1234 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1236 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1237 gtk_widget_show(button);
1238 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1239 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1240 button, GTK_RESPONSE_OK);
1241 gtk_dialog_set_default_response(GTK_DIALOG(box),
1242 GTK_RESPONSE_OK);
1244 ans = gtk_dialog_run(GTK_DIALOG(box));
1245 gtk_widget_destroy(box);
1247 if (ans != GTK_RESPONSE_OK)
1249 g_free(link_path);
1250 return FALSE;
1253 unlink(path);
1256 err = symlink(link_path, path);
1257 g_free(link_path);
1259 if (err)
1261 report_error("symlink: %s", g_strerror(errno));
1262 return FALSE;
1265 dir_check_this(path);
1267 return TRUE;
1270 static void run_action(DirItem *item)
1272 if (can_set_run_action(item))
1273 type_set_handler_dialog(item->mime_type);
1274 else
1275 report_error(
1276 _("You can only set the run action for a "
1277 "regular file"));
1280 void open_home(gpointer data, guint action, GtkWidget *widget)
1282 filer_opendir(home_dir, NULL, NULL);
1285 static void select_all(gpointer data, guint action, GtkWidget *widget)
1287 g_return_if_fail(window_with_focus != NULL);
1289 window_with_focus->temp_item_selected = FALSE;
1290 view_select_all(window_with_focus->view);
1293 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1295 g_return_if_fail(window_with_focus != NULL);
1297 window_with_focus->temp_item_selected = FALSE;
1298 view_clear_selection(window_with_focus->view);
1301 static gboolean invert_cb(ViewIter *iter, gpointer data)
1303 return !view_get_selected((ViewIface *) data, iter);
1306 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1308 g_return_if_fail(window_with_focus != NULL);
1310 window_with_focus->temp_item_selected = FALSE;
1312 view_select_if(window_with_focus->view, invert_cb,
1313 window_with_focus->view);
1316 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1318 GtkWidget *win;
1320 win = options_show();
1322 if (win)
1324 number_of_windows++;
1325 g_signal_connect(win, "destroy",
1326 G_CALLBACK(one_less_window), NULL);
1330 static gboolean new_directory_cb(GObject *savebox,
1331 const gchar *initial, const gchar *path)
1333 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1335 report_error("mkdir: %s", g_strerror(errno));
1336 return FALSE;
1339 dir_check_this(path);
1341 if (filer_exists(window_with_focus))
1343 guchar *leaf;
1344 leaf = strrchr(path, '/');
1345 if (leaf)
1346 display_set_autoselect(window_with_focus, leaf + 1);
1349 return TRUE;
1352 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1354 g_return_if_fail(window_with_focus != NULL);
1356 savebox_show(_("Create"),
1357 make_path(window_with_focus->sym_path, _("NewDir")),
1358 type_to_icon(inode_directory), new_directory_cb,
1359 GDK_ACTION_COPY);
1362 static gboolean new_file_cb(GObject *savebox,
1363 const gchar *initial, const gchar *path)
1365 int fd;
1367 fd = open(path, O_CREAT | O_EXCL, 0666);
1369 if (fd == -1)
1371 report_error(_("Error creating '%s': %s"),
1372 path, g_strerror(errno));
1373 return FALSE;
1376 if (close(fd))
1377 report_error(_("Error creating '%s': %s"),
1378 path, g_strerror(errno));
1380 dir_check_this(path);
1382 if (filer_exists(window_with_focus))
1384 guchar *leaf;
1385 leaf = strrchr(path, '/');
1386 if (leaf)
1387 display_set_autoselect(window_with_focus, leaf + 1);
1390 return TRUE;
1393 static void new_file(gpointer data, guint action, GtkWidget *widget)
1395 g_return_if_fail(window_with_focus != NULL);
1397 savebox_show(_("Create"),
1398 make_path(window_with_focus->sym_path, _("NewFile")),
1399 type_to_icon(text_plain),
1400 new_file_cb, GDK_ACTION_COPY);
1403 static gboolean new_file_type_cb(GObject *savebox,
1404 const gchar *initial, const gchar *path)
1406 const gchar *oleaf, *leaf;
1407 gchar *templ, *templ_dname, *dest;
1408 GList *paths;
1410 /* We can work out the template path from the initial name */
1411 oleaf = g_basename(initial);
1412 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1413 if (!templ_dname)
1415 report_error(
1416 _("Error creating file: could not find the template for %s"),
1417 oleaf);
1418 return FALSE;
1421 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1422 g_free(templ_dname);
1424 dest = g_path_get_dirname(path);
1425 leaf = g_basename(path);
1426 paths = g_list_append(NULL, templ);
1428 action_copy(paths, dest, leaf, TRUE);
1430 g_list_free(paths);
1431 g_free(dest);
1432 g_free(templ);
1434 if (filer_exists(window_with_focus))
1435 display_set_autoselect(window_with_focus, leaf);
1437 return TRUE;
1440 static void do_send_to(gchar *templ)
1442 g_return_if_fail(send_to_paths != NULL);
1444 run_with_files(templ, send_to_paths);
1447 static void new_file_type(gchar *templ)
1449 const gchar *leaf;
1450 MIME_type *type;
1452 g_return_if_fail(window_with_focus != NULL);
1454 leaf = g_basename(templ);
1455 type = type_get_type(templ);
1457 savebox_show(_("Create"),
1458 make_path(window_with_focus->sym_path, leaf),
1459 type_to_icon(type),
1460 new_file_type_cb, GDK_ACTION_COPY);
1463 static void customise_send_to(gpointer data)
1465 GPtrArray *path;
1466 guchar *save;
1467 GString *dirs;
1468 int i;
1470 dirs = g_string_new(NULL);
1472 path = choices_list_xdg_dirs("", SITE);
1473 for (i = 0; i < path->len; i++)
1475 guchar *old = (guchar *) path->pdata[i];
1477 g_string_append(dirs, old);
1478 g_string_append(dirs, "/SendTo\n");
1480 choices_free_list(path);
1482 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1483 if (save)
1484 mkdir(save, 0777);
1486 info_message(
1487 _("The `Send To' menu provides a quick way to send some files "
1488 "to an application. The applications listed are those in "
1489 "the following directories:\n\n%s\n%s\n"
1490 "The `Send To' menu may be opened by Shift+Menu clicking "
1491 "over a file.\n\n"
1492 "Advanced use:\n"
1493 "You can also create subdirectories called "
1494 "`.text_html', `.text', etc which will only be "
1495 "shown for files of that type. `.group' is shown "
1496 "only when multiple files are selected."),
1497 dirs->str,
1498 save ? _("I'll show you your SendTo directory now; you should "
1499 "symlink (Ctrl+Shift drag) any applications you want "
1500 "into it.")
1501 : _("Your CHOICESPATH variable setting prevents "
1502 "customisations - sorry."));
1504 g_string_free(dirs, TRUE);
1506 if (save)
1507 filer_opendir(save, NULL, NULL);
1510 static void customise_new(gpointer data)
1512 GPtrArray *path;
1513 guchar *save;
1514 GString *dirs;
1515 int i;
1517 dirs = g_string_new(NULL);
1519 path = choices_list_xdg_dirs("", SITE);
1520 for (i = 0; i < path->len; i++)
1522 guchar *old = (guchar *) path->pdata[i];
1524 g_string_append(dirs, old);
1525 g_string_append(dirs, "/Templates\n");
1527 choices_free_list(path);
1529 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1530 if (save)
1531 mkdir(save, 0777);
1533 info_message(
1534 _("Any files placed in your Templates directories will "
1535 "appear on the `New' menu. Choosing one of them will make "
1536 "a copy of it as the new file.\n\n"
1537 "The following directories contain templates:\n\n%s\n%s\n"),
1538 dirs->str,
1539 save ? _("I'll show you your Templates directory now; you "
1540 "should place any template files you want inside it.")
1541 : _("Your CHOICESPATH variable setting prevents "
1542 "customisations - sorry."));
1544 g_string_free(dirs, TRUE);
1546 if (save)
1547 filer_opendir(save, NULL, NULL);
1550 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1551 * to the menu.
1553 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1555 gchar *searchdir;
1556 GPtrArray *paths;
1557 int i;
1559 if (subtype)
1560 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1561 else if (type)
1562 searchdir = g_strdup_printf("SendTo/.%s", type);
1563 else
1564 searchdir = g_strdup("SendTo");
1566 paths = choices_list_xdg_dirs(searchdir, SITE);
1567 g_free(searchdir);
1569 for (i = 0; i < paths->len; i++)
1571 GList *widgets = NULL;
1572 guchar *dir = (guchar *) paths->pdata[i];
1574 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1575 (CallbackFn) do_send_to,
1576 FALSE, FALSE, TRUE);
1578 if (widgets)
1579 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1580 gtk_menu_item_new());
1582 g_list_free(widgets); /* TODO: Get rid of this */
1585 choices_free_list(paths);
1588 /* Scan the SendTo dir and create and show the Send To menu.
1589 * The 'paths' list and every path in it is claimed, and will be
1590 * freed later -- don't free it yourself!
1592 static void show_send_to_menu(GList *paths, GdkEvent *event)
1594 GtkWidget *menu, *item;
1596 menu = gtk_menu_new();
1598 if (g_list_length(paths) == 1)
1600 DirItem *item;
1602 item = diritem_new("");
1603 diritem_restat(paths->data, item, NULL);
1605 add_sendto(menu,
1606 item->mime_type->media_type,
1607 item->mime_type->subtype);
1609 add_sendto(menu, item->mime_type->media_type, NULL);
1611 diritem_free(item);
1613 else
1615 GList *rover;
1616 gboolean same=TRUE, same_media=TRUE;
1617 MIME_type *type=NULL;
1618 DirItem *item;
1620 item = diritem_new("");
1621 for(rover=paths; rover; rover=g_list_next(rover))
1623 diritem_restat(rover->data, item, NULL);
1624 if(!type)
1625 type=item->mime_type;
1626 else
1628 if(type!=item->mime_type)
1630 same=FALSE;
1631 if(strcmp(type->media_type,
1632 item->mime_type->media_type)!=0)
1634 same_media=FALSE;
1635 break;
1640 diritem_free(item);
1642 if(type)
1644 if(same)
1645 add_sendto(menu, type->media_type,
1646 type->subtype);
1647 if(same_media)
1648 add_sendto(menu, type->media_type, NULL);
1651 add_sendto(menu, "group", NULL);
1654 add_sendto(menu, NULL, NULL);
1656 item = gtk_menu_item_new_with_label(_("Customise"));
1657 g_signal_connect_swapped(item, "activate",
1658 G_CALLBACK(customise_send_to), NULL);
1659 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1661 if (send_to_paths)
1662 destroy_glist(&send_to_paths);
1664 send_to_paths = paths;
1666 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1668 popup_menu = menu;
1669 show_popup_menu(menu, event, 0);
1672 static void send_to(FilerWindow *filer_window)
1674 GList *paths;
1675 GdkEvent *event;
1677 paths = filer_selected_items(filer_window);
1678 event = gtk_get_current_event();
1680 /* Eats paths */
1681 show_send_to_menu(paths, event);
1683 if (event)
1684 gdk_event_free(event);
1687 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1689 const char *argv[] = {"sh", "-c", NULL, NULL};
1690 gboolean close = action;
1692 argv[2] = o_menu_xterm.value;
1694 g_return_if_fail(window_with_focus != NULL);
1696 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1697 gtk_widget_destroy(window_with_focus->window);
1700 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1702 g_return_if_fail(window_with_focus != NULL);
1704 filer_change_to(window_with_focus, home_dir, NULL);
1707 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1709 g_return_if_fail(window_with_focus != NULL);
1711 bookmarks_show_menu(window_with_focus);
1714 static void show_log(gpointer data, guint action, GtkWidget *widget)
1716 g_return_if_fail(window_with_focus != NULL);
1718 log_show_window();
1721 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1723 g_return_if_fail(window_with_focus != NULL);
1725 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1726 filer_change_to(window_with_focus,
1727 window_with_focus->real_path, NULL);
1728 else
1729 delayed_error(_("This is already the canonical name "
1730 "for this directory."));
1733 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1735 g_return_if_fail(window_with_focus != NULL);
1737 filer_open_parent(window_with_focus);
1740 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1742 g_return_if_fail(window_with_focus != NULL);
1744 change_to_parent(window_with_focus);
1747 static void resize(gpointer data, guint action, GtkWidget *widget)
1749 g_return_if_fail(window_with_focus != NULL);
1751 view_autosize(window_with_focus->view);
1754 static void new_window(gpointer data, guint action, GtkWidget *widget)
1756 g_return_if_fail(window_with_focus != NULL);
1758 if (o_unique_filer_windows.int_value)
1760 report_error(_("You can't open a second view onto "
1761 "this directory because the `Unique Windows' option "
1762 "is turned on in the Options window."));
1764 else
1765 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1768 static void close_window(gpointer data, guint action, GtkWidget *widget)
1770 g_return_if_fail(window_with_focus != NULL);
1772 if (!filer_window_delete(window_with_focus->window, NULL,
1773 window_with_focus))
1774 gtk_widget_destroy(window_with_focus->window);
1777 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1779 MiniType type = (MiniType) action;
1781 g_return_if_fail(window_with_focus != NULL);
1783 /* Item needs to remain selected... */
1784 if (type == MINI_SHELL)
1785 window_with_focus->temp_item_selected = FALSE;
1787 minibuffer_show(window_with_focus, type);
1790 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1792 if (action == HELP_ABOUT)
1793 infobox_new(app_dir);
1794 else if (action == HELP_DIR)
1795 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1796 else if (action == HELP_MANUAL)
1798 gchar *manual = NULL;
1800 if (current_lang)
1802 manual = g_strconcat(app_dir, "/Help/Manual-",
1803 current_lang, ".html", NULL);
1804 if (!file_exists(manual) && strchr(current_lang, '_'))
1806 /* Try again without the territory */
1807 strcpy(strrchr(manual, '_'), ".html");
1809 if (!file_exists(manual))
1810 null_g_free(&manual);
1813 if (!manual)
1814 manual = g_strconcat(app_dir,
1815 "/Help/Manual.html", NULL);
1817 run_by_path(manual);
1819 g_free(manual);
1821 else
1822 g_warning("Unknown help action %d\n", action);
1825 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1826 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1828 GList *items, *item;
1830 items = gtk_container_get_children(GTK_CONTAINER(menu));
1832 item = g_list_nth(items, from);
1833 while (item && n--)
1835 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1836 item = item->next;
1838 g_list_free(items);
1841 static void save_menus(void)
1843 char *menurc;
1845 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1846 if (menurc)
1848 gtk_accel_map_save(menurc);
1849 g_free(menurc);
1853 static void select_nth_item(GtkMenuShell *shell, int n)
1855 GList *items;
1856 GtkWidget *item;
1858 items = gtk_container_get_children(GTK_CONTAINER(shell));
1859 item = g_list_nth_data(items, n);
1861 g_return_if_fail(item != NULL);
1863 g_list_free(items);
1865 gtk_menu_shell_select_item(shell, item);
1868 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1870 DirItem *item;
1871 const guchar *path;
1872 int n_selected;
1873 ViewIter iter;
1875 g_return_if_fail(window_with_focus != NULL);
1877 n_selected = view_count_selected(window_with_focus->view);
1879 if (n_selected < 1)
1881 const char *prompt;
1883 switch (action)
1885 case FILE_COPY_ITEM:
1886 prompt = _("Copy ... ?");
1887 break;
1888 case FILE_RENAME_ITEM:
1889 prompt = _("Rename ... ?");
1890 break;
1891 case FILE_LINK_ITEM:
1892 prompt = _("Symlink ... ?");
1893 break;
1894 case FILE_OPEN_FILE:
1895 prompt = _("Shift Open ... ?");
1896 break;
1897 case FILE_PROPERTIES:
1898 prompt = _("Properties of ... ?");
1899 break;
1900 case FILE_SET_TYPE:
1901 prompt = _("Set type of ... ?");
1902 break;
1903 case FILE_RUN_ACTION:
1904 prompt = _("Set run action for ... ?");
1905 break;
1906 case FILE_SET_ICON:
1907 prompt = _("Set icon for ... ?");
1908 break;
1909 case FILE_SEND_TO:
1910 prompt = _("Send ... to ... ?");
1911 break;
1912 case FILE_DELETE:
1913 prompt = _("DELETE ... ?");
1914 break;
1915 case FILE_USAGE:
1916 prompt = _("Count the size of ... ?");
1917 break;
1918 case FILE_CHMOD_ITEMS:
1919 prompt = _("Set permissions on ... ?");
1920 break;
1921 case FILE_FIND:
1922 prompt = _("Search inside ... ?");
1923 break;
1924 default:
1925 g_warning("Unknown action!");
1926 return;
1928 filer_target_mode(window_with_focus, target_callback,
1929 GINT_TO_POINTER(action), prompt);
1930 return;
1933 switch (action)
1935 case FILE_SEND_TO:
1936 send_to(window_with_focus);
1937 return;
1938 case FILE_DELETE:
1939 delete(window_with_focus);
1940 return;
1941 case FILE_USAGE:
1942 usage(window_with_focus);
1943 return;
1944 case FILE_CHMOD_ITEMS:
1945 chmod_items(window_with_focus);
1946 return;
1947 case FILE_SET_TYPE:
1948 set_type_items(window_with_focus);
1949 return;
1950 case FILE_FIND:
1951 find(window_with_focus);
1952 return;
1953 case FILE_PROPERTIES:
1955 GList *items;
1957 items = filer_selected_items(window_with_focus);
1958 infobox_show_list(items);
1959 destroy_glist(&items);
1960 return;
1962 case FILE_RENAME_ITEM:
1963 if (n_selected > 1)
1965 GList *items = NULL;
1966 ViewIter iter;
1968 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1969 while ((item = iter.next(&iter)))
1970 items = g_list_prepend(items, item->leafname);
1971 items = g_list_reverse(items);
1973 bulk_rename(window_with_focus->sym_path, items);
1974 g_list_free(items);
1975 return;
1977 break; /* Not a bulk rename... see below */
1978 default:
1979 break;
1982 /* All the following actions require exactly one file selected */
1984 if (n_selected > 1)
1986 report_error(_("You cannot do this to more than "
1987 "one item at a time"));
1988 return;
1991 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1993 item = iter.next(&iter);
1994 g_return_if_fail(item != NULL);
1995 /* iter may be passed to filer_openitem... */
1997 if (item->base_type == TYPE_UNKNOWN)
1998 item = dir_update_item(window_with_focus->directory,
1999 item->leafname);
2001 if (!item)
2003 report_error(_("Item no longer exists!"));
2004 return;
2007 path = make_path(window_with_focus->sym_path, item->leafname);
2009 switch (action)
2011 case FILE_COPY_ITEM:
2012 src_dest_action_item(path, di_image(item),
2013 _("Copy"), copy_cb,
2014 GDK_ACTION_COPY);
2015 break;
2016 case FILE_RENAME_ITEM:
2017 src_dest_action_item(path, di_image(item),
2018 _("Rename"), rename_cb,
2019 GDK_ACTION_MOVE);
2020 break;
2021 case FILE_LINK_ITEM:
2022 src_dest_action_item(path, di_image(item),
2023 _("Symlink"), link_cb,
2024 GDK_ACTION_LINK);
2025 break;
2026 case FILE_OPEN_FILE:
2027 filer_openitem(window_with_focus, &iter,
2028 OPEN_SAME_WINDOW | OPEN_SHIFT);
2029 break;
2030 case FILE_RUN_ACTION:
2031 run_action(item);
2032 break;
2033 case FILE_SET_ICON:
2034 icon_set_handler_dialog(item, path);
2035 break;
2036 default:
2037 g_warning("Unknown action!");
2038 return;
2042 static void show_key_help(GtkWidget *button, gpointer data)
2044 gboolean can_change_accels;
2046 g_object_get(G_OBJECT(gtk_settings_get_default()),
2047 "gtk-can-change-accels", &can_change_accels,
2048 NULL);
2050 if (!can_change_accels)
2052 info_message(_("User-definable shortcuts are disabled by "
2053 "default in Gtk2, and you have not enabled "
2054 "them. You can turn this feature on by:\n\n"
2055 "1) using an XSettings manager, such as ROX-Session "
2056 "or gnome-settings-daemon, or\n\n"
2057 "2) adding this line to ~/.gtkrc-2.0:\n"
2058 "\tgtk-can-change-accels = 1\n"
2059 "\t(this only works if NOT using XSETTINGS)"));
2060 return;
2063 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2064 "- Open the menu over a filer window,\n"
2065 "- Move the pointer over the item you want to use,\n"
2066 "- Press the key you want attached to it.\n\n"
2067 "The key will appear next to the menu item and you can just press "
2068 "that key without opening the menu in future."));
2071 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2073 GtkWidget *button, *align;
2075 g_return_val_if_fail(option == NULL, NULL);
2077 align = gtk_alignment_new(0, 0.5, 0, 0);
2078 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2079 gtk_container_add(GTK_CONTAINER(align), button);
2080 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2082 return g_list_append(NULL, align);