Menu option to create a new start-up script.
[rox-filer.git] / ROX-Filer / src / menu.c
blob75e7e83d9e493edd67110c793030222faba70a31
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 #ifndef GTK_STOCK_INFO
23 # define GTK_STOCK_INFO "gtk-info"
24 #endif
26 #include "config.h"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/wait.h>
31 #include <sys/param.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <dirent.h>
37 #include <gtk/gtk.h>
39 #include "global.h"
41 #include "menu.h"
42 #include "run.h"
43 #include "action.h"
44 #include "filer.h"
45 #include "pixmaps.h"
46 #include "type.h"
47 #include "support.h"
48 #include "gui_support.h"
49 #include "options.h"
50 #include "choices.h"
51 #include "gtksavebox.h"
52 #include "mount.h"
53 #include "minibuffer.h"
54 #include "i18n.h"
55 #include "main.h"
56 #include "pinboard.h"
57 #include "dir.h"
58 #include "diritem.h"
59 #include "appmenu.h"
60 #include "usericons.h"
61 #include "infobox.h"
62 #include "view_iface.h"
63 #include "display.h"
64 #include "bookmarks.h"
65 #include "panel.h"
66 #include "bulk_rename.h"
67 #include "xtypes.h"
68 #include "log.h"
70 typedef enum {
71 FILE_COPY_ITEM,
72 FILE_RENAME_ITEM,
73 FILE_LINK_ITEM,
74 FILE_OPEN_FILE,
75 FILE_PROPERTIES,
76 FILE_RUN_ACTION,
77 FILE_SET_ICON,
78 FILE_SEND_TO,
79 FILE_DELETE,
80 FILE_USAGE,
81 FILE_CHMOD_ITEMS,
82 FILE_FIND,
83 FILE_SET_TYPE,
84 } FileOp;
86 typedef void (*ActionFn)(GList *paths,
87 const char *dest_dir, const char *leaf, int quiet);
88 typedef void MenuCallback(GtkWidget *widget, gpointer data);
90 typedef gboolean (*SaveCb)(GObject *savebox,
91 const gchar *current, const gchar *new);
93 GtkAccelGroup *filer_keys = NULL;
94 static gboolean filer_keys_need_init = TRUE;
96 static GtkWidget *popup_menu = NULL; /* Currently open menu */
98 static gint updating_menu = 0; /* Non-zero => ignore activations */
99 static GList *send_to_paths = NULL;
101 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
103 /* Static prototypes */
105 static void save_menus(void);
106 static void menu_closed(GtkWidget *widget);
107 static void shade_file_menu_items(gboolean shaded);
108 static void savebox_show(const gchar *action, const gchar *path,
109 MaskedPixmap *image, SaveCb callback,
110 GdkDragAction dnd_action);
111 static gint save_to_file(GObject *savebox,
112 const gchar *pathname, gpointer data);
113 static gboolean action_with_leaf(ActionFn action,
114 const gchar *current, const gchar *new);
115 static gboolean link_cb(GObject *savebox,
116 const gchar *initial, const gchar *path);
117 static void select_nth_item(GtkMenuShell *shell, int n);
118 static void new_file_type(gchar *templ);
119 static void do_send_to(gchar *templ);
120 static void show_send_to_menu(GList *paths, GdkEvent *event);
121 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
123 /* Note that for most of these callbacks none of the arguments are used. */
125 static void view_type(gpointer data, guint action, GtkWidget *widget);
127 /* (action used in these three - DetailsType) */
128 static void change_size(gpointer data, guint action, GtkWidget *widget);
129 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
130 static void set_with(gpointer data, guint action, GtkWidget *widget);
132 static void set_sort(gpointer data, guint action, GtkWidget *widget);
133 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
135 static void filter_directories(gpointer data, guint action, GtkWidget *widget);
136 static void hidden(gpointer data, guint action, GtkWidget *widget);
137 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
138 static void refresh(gpointer data, guint action, GtkWidget *widget);
139 static void save_settings(gpointer data, guint action, GtkWidget *widget);
141 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
143 static void select_all(gpointer data, guint action, GtkWidget *widget);
144 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
145 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
146 static void new_directory(gpointer data, guint action, GtkWidget *widget);
147 static void new_file(gpointer data, guint action, GtkWidget *widget);
148 static void new_script(gpointer data, guint action, GtkWidget *widget);
149 static void customise_new(gpointer data);
150 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
152 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
153 static void open_parent(gpointer data, guint action, GtkWidget *widget);
154 static void home_directory(gpointer data, guint action, GtkWidget *widget);
155 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
156 static void show_log(gpointer data, guint action, GtkWidget *widget);
157 static void new_window(gpointer data, guint action, GtkWidget *widget);
158 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
159 static void close_window(gpointer data, guint action, GtkWidget *widget);
160 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
162 /* (action used in this - MiniType) */
163 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
164 static void resize(gpointer data, guint action, GtkWidget *widget);
166 #define MENUS_NAME "menus2"
168 static GtkWidget *filer_menu; /* The popup filer menu */
169 static GtkWidget *filer_file_item; /* The File '' label */
170 static GtkWidget *filer_file_menu; /* The File '' menu */
171 static GtkWidget *file_shift_item; /* Shift Open label */
172 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
173 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
174 static GtkWidget *filer_filter_dirs_menu;/* The Filter Dirs item */
175 static GtkWidget *filer_reverse_menu; /* The Reversed item */
176 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
177 static GtkWidget *filer_new_window; /* The New Window item */
178 static GtkWidget *filer_new_menu; /* The New submenu */
179 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
180 static GtkWidget *filer_set_type; /* Set type item */
182 #undef N_
183 #define N_(x) x
185 static GtkItemFactoryEntry filer_menu_def[] = {
186 {N_("Display"), NULL, NULL, 0, "<Branch>"},
187 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
188 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
189 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
190 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
191 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
192 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
193 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
194 {">", NULL, NULL, 0, "<Separator>"},
195 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
196 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
197 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
198 {">", NULL, NULL, 0, "<Separator>"},
199 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
200 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
201 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
202 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
203 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
204 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
205 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
206 {">", NULL, NULL, 0, "<Separator>"},
207 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
208 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
209 {">" N_("Filter Directories With Files"), NULL, filter_directories, 0, "<ToggleItem>"},
210 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
211 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
212 {">" N_("Save Current Display Settings..."), NULL, save_settings, 0, NULL},
213 {N_("File"), NULL, NULL, 0, "<Branch>"},
214 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
215 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
216 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
217 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
218 {">", NULL, NULL, 0, "<Separator>"},
219 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
220 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
221 {">", NULL, NULL, 0, "<Separator>"},
222 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
223 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
224 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
225 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
226 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
227 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
228 {">", NULL, NULL, 0, "<Separator>"},
229 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
230 {N_("Select"), NULL, NULL, 0, "<Branch>"},
231 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
232 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
233 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
234 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
235 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
236 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
237 {N_("New"), NULL, NULL, 0, "<Branch>"},
238 {">" N_("Directory"), NULL, new_directory, 0, NULL},
239 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
240 {">" N_("Start script"), NULL, new_script, 0, NULL},
241 {">" N_("Customise Menu..."), NULL, customise_new, 0, NULL},
242 {N_("Window"), NULL, NULL, 0, "<Branch>"},
243 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
244 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
245 {">" N_("New Window"), NULL, new_window, 0, NULL},
246 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
247 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
248 {">" N_("Show Log"), NULL, show_log, 0, "<StockItem>", GTK_STOCK_INFO},
249 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
250 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
251 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
253 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
254 {">", NULL, NULL, 0, "<Separator>"},
255 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
256 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
257 {">" N_("Terminal Here"), "grave", xterm_here, FALSE, NULL},
258 {">" N_("Switch to Terminal"), NULL, xterm_here, TRUE, NULL},
259 {N_("Help"), NULL, NULL, 0, "<Branch>"},
260 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
261 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
262 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
266 #define GET_MENU_ITEM(var, menu) \
267 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
269 #define GET_SMENU_ITEM(var, menu, sub) \
270 do { \
271 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
272 var = gtk_item_factory_get_widget(item_factory, tmp); \
273 g_free(tmp); \
274 } while (0)
276 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
277 do { \
278 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
279 var = gtk_item_factory_get_widget(item_factory, tmp); \
280 g_free(tmp); \
281 } while (0)
283 /* Returns TRUE if the keys were installed (first call only) */
284 gboolean ensure_filer_menu(void)
286 GList *items;
287 guchar *tmp;
288 GtkWidget *item;
289 GtkItemFactory *item_factory;
291 if (!filer_keys_need_init)
292 return FALSE;
293 filer_keys_need_init = FALSE;
295 item_factory = menu_create(filer_menu_def,
296 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
297 "<filer>", filer_keys);
299 GET_MENU_ITEM(filer_menu, "filer");
300 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
301 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
302 GET_SSMENU_ITEM(filer_filter_dirs_menu, "filer", "Display", "Filter Directories With Files");
303 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
304 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
305 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
306 "Show Thumbnails");
307 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
308 filer_set_type = GTK_BIN(item)->child;
310 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
311 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
312 filer_follow_sym = GTK_BIN(item)->child;
314 /* File '' label... */
315 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
316 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
317 g_list_free(items);
319 /* Shift Open... label */
320 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
321 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
322 g_list_free(items);
324 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
325 filer_new_window = GTK_BIN(item)->child;
327 g_signal_connect(filer_menu, "unmap_event",
328 G_CALLBACK(menu_closed), NULL);
329 g_signal_connect(filer_file_menu, "unmap_event",
330 G_CALLBACK(menu_closed), NULL);
332 g_signal_connect(filer_keys, "accel_changed",
333 G_CALLBACK(save_menus), NULL);
335 return TRUE;
338 void menu_init(void)
340 char *menurc;
342 menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
343 if (menurc)
345 gtk_accel_map_load(menurc);
346 g_free(menurc);
349 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
350 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
351 option_add_int(&o_menu_quick, "menu_quick", FALSE);
352 option_add_saver(save_menus);
354 option_register_widget("menu-set-keys", set_keys_button);
356 filer_keys = gtk_accel_group_new();
359 /* Name is in the form "<panel>" */
360 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
361 const gchar *name, GtkAccelGroup *keys)
363 GtkItemFactory *item_factory;
364 GtkItemFactoryEntry *translated;
366 if (!keys)
368 keys = gtk_accel_group_new();
369 gtk_accel_group_lock(keys);
372 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
374 translated = translate_entries(def, n_entries);
375 gtk_item_factory_create_items(item_factory, n_entries,
376 translated, NULL);
377 free_translated_entries(translated, n_entries);
379 return item_factory;
382 /* Prevent the user from setting a short-cut on this item */
383 static void menuitem_no_shortcuts(GtkWidget *item)
385 /* XXX */
386 #if 0
387 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
389 _gtk_widget_set_accel_path(item, NULL, NULL);
390 null_g_free(&menuitem->accel_path);
391 #endif
394 /* Shade items that only work on single files */
395 static void shade_file_menu_items(gboolean shaded)
397 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
398 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
399 menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
400 menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
403 /* 'data' is an array of three ints:
404 * [ pointer_x, pointer_y, item_under_pointer ]
406 void position_menu(GtkMenu *menu, gint *x, gint *y,
407 gboolean *push_in, gpointer data)
409 int *pos = (int *) data;
410 GtkRequisition requisition;
411 GList *items, *next;
412 int y_shift = 0;
413 int item = pos[2];
415 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
417 while (item >= 0 && next)
419 int h = ((GtkWidget *) next->data)->requisition.height;
421 if (item > 0)
422 y_shift += h;
423 else
424 y_shift += h / 2;
426 next = next->next;
427 item--;
430 g_list_free(items);
432 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
434 *x = pos[0] - (requisition.width * 7 / 8);
435 *y = pos[1] - y_shift;
437 *x = CLAMP(*x, 0, screen_width - requisition.width);
438 *y = CLAMP(*y, 0, screen_height - requisition.height);
440 *push_in = FALSE;
443 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
444 MenuIconStyle style)
446 GtkWidget *item;
448 if (style != MIS_NONE && di_image(ditem))
450 GdkPixbuf *pixbuf;
451 MaskedPixmap *image;
453 image = di_image(ditem);
455 switch (style)
457 case MIS_LARGE:
458 pixbuf = image->pixbuf;
459 break;
460 default:
461 if (!image->sm_pixbuf)
462 pixmap_make_small(image);
463 pixbuf = image->sm_pixbuf;
464 break;
467 item = gtk_image_menu_item_new_with_label(label);
468 /* TODO: Find a way to allow short-cuts */
469 menuitem_no_shortcuts(item);
471 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
472 gtk_image_new_from_pixbuf(pixbuf));
473 gtk_widget_show_all(item);
475 else
476 item = gtk_menu_item_new_with_label(label);
478 return item;
481 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
482 MenuIconStyle style, CallbackFn func,
483 gboolean separator, gboolean strip_ext,
484 gboolean recurse)
486 GList *widgets = NULL;
487 DirItem *ditem;
488 int i;
489 GtkWidget *item;
490 char *dname = NULL;
491 GPtrArray *names;
493 dname = pathdup(dir_name);
495 names = list_dir(dname);
496 if (!names)
497 goto out;
499 for (i = 0; i < names->len; i++)
501 char *leaf = names->pdata[i];
502 gchar *fname;
504 if (separator)
506 item = gtk_menu_item_new();
507 widgets = g_list_append(widgets, item);
508 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
509 separator = FALSE;
512 fname = g_strconcat(dname, "/", leaf, NULL);
514 /* Strip off extension, if any */
515 if (strip_ext)
517 char *dot;
518 dot = strchr(leaf, '.');
519 if (dot)
520 *dot = '\0';
523 ditem = diritem_new("");
524 diritem_restat(fname, ditem, NULL);
526 item = make_send_to_item(ditem, leaf, style);
528 g_free(leaf);
530 /* If it is a directory (but NOT an AppDir) and we are
531 * recursing then set up a sub menu.
533 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
534 !(ditem->flags & ITEM_FLAG_APPDIR))
536 GtkWidget *sub;
537 GList *new_widgets;
539 sub = gtk_menu_new();
540 new_widgets = menu_from_dir(sub, fname, style, func,
541 separator, strip_ext, TRUE);
542 g_list_free(new_widgets);
543 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
545 else
546 g_signal_connect_swapped(item, "activate",
547 G_CALLBACK(func), fname);
549 diritem_free(ditem);
551 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
552 g_signal_connect_swapped(item, "destroy",
553 G_CALLBACK(g_free), fname);
555 widgets = g_list_append(widgets, item);
558 g_ptr_array_free(names, TRUE);
559 out:
560 g_free(dname);
562 return widgets;
565 /* Scan the templates dir and create entries for the New menu */
566 static void update_new_files_menu(MenuIconStyle style)
568 static GList *widgets = NULL;
570 gchar *templ_dname = NULL;
572 if (widgets)
574 GList *next;
576 for (next = widgets; next; next = next->next)
577 gtk_widget_destroy((GtkWidget *) next->data);
579 g_list_free(widgets);
580 widgets = NULL;
583 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
584 if (templ_dname)
586 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
587 (CallbackFn) new_file_type, TRUE, TRUE,
588 FALSE);
589 g_free(templ_dname);
591 gtk_widget_show_all(filer_new_menu);
594 /* 'item' is the number of the item to appear under the pointer. */
595 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
597 int pos[3];
598 int button = 0;
599 guint32 time = 0;
601 if (event && (event->type == GDK_BUTTON_PRESS ||
602 event->type == GDK_BUTTON_RELEASE))
604 GdkEventButton *bev = (GdkEventButton *) event;
606 pos[0] = bev->x_root;
607 pos[1] = bev->y_root;
608 button = bev->button;
609 time = bev->time;
611 else if (event && event->type == GDK_KEY_PRESS)
613 GdkEventKey *kev = (GdkEventKey *) event;
615 get_pointer_xy(pos, pos + 1);
616 time = kev->time;
618 else
619 get_pointer_xy(pos, pos + 1);
621 pos[2] = item;
623 gtk_widget_show_all(menu);
624 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
625 position_menu, (gpointer) pos, button, time);
626 select_nth_item(GTK_MENU_SHELL(menu), item);
629 /* Hide the popup menu, if any */
630 void menu_popdown(void)
632 if (popup_menu)
633 gtk_menu_popdown(GTK_MENU(popup_menu));
636 static MenuIconStyle get_menu_icon_style(void)
638 MenuIconStyle mis;
639 int display;
641 mis = o_menu_iconsize.int_value;
643 switch (mis)
645 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
646 return mis;
647 default:
648 break;
651 if (mis == MIS_CURRENT && window_with_focus)
653 switch (window_with_focus->display_style)
655 case HUGE_ICONS:
656 case LARGE_ICONS:
657 return MIS_LARGE;
658 case SMALL_ICONS:
659 return MIS_SMALL;
660 default:
661 break;
665 display = o_display_size.int_value;
666 switch (display)
668 case HUGE_ICONS:
669 case LARGE_ICONS:
670 return MIS_LARGE;
671 case SMALL_ICONS:
672 return MIS_SMALL;
673 default:
674 break;
677 return MIS_SMALL;
680 /* iter->peek() is the clicked item, or NULL if none */
681 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
683 DirItem *file_item = NULL;
684 GdkModifierType state = 0;
685 int n_selected;
686 int n_added = 0;
688 g_return_if_fail(event != NULL);
690 n_selected = view_count_selected(filer_window->view);
692 ensure_filer_menu();
694 updating_menu++;
696 /* Remove previous AppMenu, if any */
697 appmenu_remove();
699 window_with_focus = filer_window;
701 if (event->type == GDK_BUTTON_PRESS)
702 state = ((GdkEventButton *) event)->state;
703 else if (event->type == GDK_KEY_PRESS)
704 state = ((GdkEventKey *) event)->state;
706 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
708 filer_window->temp_item_selected = TRUE;
709 view_set_selected(filer_window->view, iter, TRUE);
710 n_selected = view_count_selected(filer_window->view);
712 else
714 filer_window->temp_item_selected = FALSE;
717 /* Short-cut to the Send To menu */
718 if (state & GDK_SHIFT_MASK)
720 GList *paths;
722 updating_menu--;
724 if (n_selected == 0)
726 report_error(
727 _("You should Shift+Menu click over a file to "
728 "send it somewhere"));
729 return;
732 paths = filer_selected_items(filer_window);
734 show_send_to_menu(paths, event); /* (paths eaten) */
736 return;
740 GtkWidget *file_label, *file_menu;
741 GString *buffer;
742 DirItem *item;
744 file_label = filer_file_item;
745 file_menu = filer_file_menu;
746 gtk_check_menu_item_set_active(
747 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
748 filer_window->show_thumbs);
749 gtk_check_menu_item_set_active(
750 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
751 filer_window->show_hidden);
752 gtk_check_menu_item_set_active(
753 GTK_CHECK_MENU_ITEM(filer_filter_dirs_menu),
754 filer_window->filter_directories);
755 gtk_check_menu_item_set_active(
756 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
757 filer_window->sort_order != GTK_SORT_ASCENDING);
758 gtk_check_menu_item_set_active(
759 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
760 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
761 buffer = g_string_new(NULL);
763 switch (n_selected)
765 case 0:
766 g_string_assign(buffer, _("Next Click"));
767 shade_file_menu_items(FALSE);
768 break;
769 case 1:
770 item = filer_selected_item(filer_window);
771 if (item->base_type == TYPE_UNKNOWN)
772 dir_update_item(filer_window->directory,
773 item->leafname);
774 shade_file_menu_items(FALSE);
775 file_item = filer_selected_item(filer_window);
776 g_string_printf(buffer, _("%s '%s'"),
777 basetype_name(file_item),
778 g_utf8_validate(file_item->leafname,
779 -1, NULL)
780 ? file_item->leafname
781 : _("(bad utf-8)"));
782 if (!can_set_run_action(file_item))
783 menu_set_items_shaded(filer_file_menu,
784 TRUE, 8, 1);
785 break;
786 default:
787 shade_file_menu_items(TRUE);
788 g_string_printf(buffer, _("%d items"),
789 n_selected);
790 break;
792 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
793 g_string_free(buffer, TRUE);
795 menu_show_shift_action(file_shift_item, file_item,
796 n_selected == 0);
797 if (file_item)
798 n_added = appmenu_add(make_path(filer_window->sym_path,
799 file_item->leafname),
800 file_item, filer_file_menu);
803 update_new_files_menu(get_menu_icon_style());
805 gtk_widget_set_sensitive(filer_new_window,
806 !o_unique_filer_windows.int_value);
807 gtk_widget_set_sensitive(filer_follow_sym,
808 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
809 gtk_widget_set_sensitive(filer_set_type,
810 xattr_supported(filer_window->real_path));
812 if (n_selected && o_menu_quick.int_value)
813 popup_menu = (state & GDK_CONTROL_MASK)
814 ? filer_menu
815 : filer_file_menu;
816 else
817 popup_menu = (state & GDK_CONTROL_MASK)
818 ? filer_file_menu
819 : filer_menu;
821 updating_menu--;
823 show_popup_menu(popup_menu, event,
824 popup_menu == filer_file_menu ? n_added : 1);
827 static void menu_closed(GtkWidget *widget)
829 if (window_with_focus == NULL || widget != popup_menu)
830 return; /* Close panel item chosen? */
832 popup_menu = NULL;
834 if (window_with_focus->temp_item_selected)
836 view_clear_selection(window_with_focus->view);
837 window_with_focus->temp_item_selected = FALSE;
840 appmenu_remove();
843 static void target_callback(FilerWindow *filer_window,
844 ViewIter *iter,
845 gpointer action)
847 g_return_if_fail(filer_window != NULL);
849 window_with_focus = filer_window;
851 /* Don't grab the primary selection */
852 filer_window->temp_item_selected = TRUE;
854 view_wink_item(filer_window->view, iter);
855 view_select_only(filer_window->view, iter);
856 file_op(NULL, GPOINTER_TO_INT(action), NULL);
858 view_clear_selection(filer_window->view);
859 filer_window->temp_item_selected = FALSE;
862 /* Set the text of the 'Shift Open...' menu item.
863 * If icon is NULL, reset the text and also shade it, unless 'next'.
865 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
867 guchar *shift_action = NULL;
869 if (item)
871 if (item->flags & ITEM_FLAG_MOUNT_POINT)
873 if (item->flags & ITEM_FLAG_MOUNTED)
874 shift_action = N_("Unmount");
875 else
876 shift_action = N_("Open unmounted");
878 else if (item->flags & ITEM_FLAG_SYMLINK)
879 shift_action = N_("Show Target");
880 else if (item->base_type == TYPE_DIRECTORY)
881 shift_action = N_("Look Inside");
882 else if (item->base_type == TYPE_FILE)
883 shift_action = N_("Open As Text");
885 gtk_label_set_text(GTK_LABEL(menu_item),
886 shift_action ? _(shift_action)
887 : _("Shift Open"));
888 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
891 /* Actions */
893 static void view_type(gpointer data, guint action, GtkWidget *widget)
895 ViewType view_type = (ViewType) action;
897 g_return_if_fail(window_with_focus != NULL);
899 if (view_type == VIEW_TYPE_COLLECTION)
900 display_set_layout(window_with_focus,
901 window_with_focus->display_style_wanted,
902 DETAILS_NONE, FALSE);
904 filer_set_view_type(window_with_focus, (ViewType) action);
907 static void change_size(gpointer data, guint action, GtkWidget *widget)
909 g_return_if_fail(window_with_focus != NULL);
911 display_change_size(window_with_focus, action == 1);
914 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
916 g_return_if_fail(window_with_focus != NULL);
918 if (updating_menu)
919 return;
921 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
922 display_set_layout(window_with_focus,
923 window_with_focus->display_style,
924 window_with_focus->details_type, FALSE);
925 else
926 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
927 window_with_focus->details_type, FALSE);
930 static void set_with(gpointer data, guint action, GtkWidget *widget)
932 DisplayStyle size;
934 g_return_if_fail(window_with_focus != NULL);
936 size = window_with_focus->display_style_wanted;
938 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
939 display_set_layout(window_with_focus, size, action, FALSE);
942 static void set_sort(gpointer data, guint action, GtkWidget *widget)
944 if (updating_menu)
945 return;
947 g_return_if_fail(window_with_focus != NULL);
949 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
952 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
954 GtkSortType order;
956 if (updating_menu)
957 return;
959 g_return_if_fail(window_with_focus != NULL);
961 order = window_with_focus->sort_order;
962 if (order == GTK_SORT_ASCENDING)
963 order = GTK_SORT_DESCENDING;
964 else
965 order = GTK_SORT_ASCENDING;
967 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
968 order);
971 static void hidden(gpointer data, guint action, GtkWidget *widget)
973 if (updating_menu)
974 return;
976 g_return_if_fail(window_with_focus != NULL);
978 display_set_hidden(window_with_focus,
979 !window_with_focus->show_hidden);
982 static void filter_directories(gpointer data, guint action, GtkWidget *widget)
984 if (updating_menu)
985 return;
987 g_return_if_fail(window_with_focus != NULL);
989 display_set_filter_directories(window_with_focus,
990 !window_with_focus->filter_directories);
993 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
995 if (updating_menu)
996 return;
998 g_return_if_fail(window_with_focus != NULL);
1000 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
1003 static void refresh(gpointer data, guint action, GtkWidget *widget)
1005 g_return_if_fail(window_with_focus != NULL);
1007 filer_refresh(window_with_focus);
1010 static void save_settings(gpointer data, guint action, GtkWidget *widget)
1012 g_return_if_fail(window_with_focus != NULL);
1014 filer_save_settings(window_with_focus);
1017 static void delete(FilerWindow *filer_window)
1019 GList *paths;
1020 paths = filer_selected_items(filer_window);
1021 action_delete(paths);
1022 destroy_glist(&paths);
1025 static void usage(FilerWindow *filer_window)
1027 GList *paths;
1028 paths = filer_selected_items(filer_window);
1029 action_usage(paths);
1030 destroy_glist(&paths);
1033 static void chmod_items(FilerWindow *filer_window)
1035 GList *paths;
1036 paths = filer_selected_items(filer_window);
1037 action_chmod(paths, FALSE, NULL);
1038 destroy_glist(&paths);
1041 static void set_type_items(FilerWindow *filer_window)
1043 GList *paths, *p;
1044 int npass=0, nfail=0;
1046 paths = filer_selected_items(filer_window);
1047 for(p=paths; p; p=g_list_next(p)) {
1048 if(xattr_supported((const char *) p->data))
1049 npass++;
1050 else
1051 nfail++;
1053 if(npass==0)
1054 report_error(_("Extended attributes, used to store types, are not supported for this "
1055 "file or files.\n"
1056 "This may be due to lack of support from the filesystem or the C library, "
1057 "or it may simply be that the filesystem needs to be mounted with "
1058 "the right mount option ('user_xattr' on Linux)."));
1059 else if(nfail>0)
1060 report_error(_("Setting type not supported for some of these files"));
1061 if(npass>0)
1062 action_settype(paths, FALSE, NULL);
1063 destroy_glist(&paths);
1066 static void find(FilerWindow *filer_window)
1068 GList *paths;
1069 paths = filer_selected_items(filer_window);
1070 action_find(paths);
1071 destroy_glist(&paths);
1074 static gboolean last_symlink_check_relative = TRUE;
1076 /* This creates a new savebox widget, and allows the user to pick a new path
1077 * for the file.
1078 * Once the new path has been picked, the callback will be called with
1079 * both the current and new paths.
1080 * NOTE: This function unrefs 'image'!
1082 static void savebox_show(const gchar *action, const gchar *path,
1083 MaskedPixmap *image, SaveCb callback,
1084 GdkDragAction dnd_action)
1086 GtkWidget *savebox = NULL;
1087 GtkWidget *check_relative = NULL;
1089 g_return_if_fail(image != NULL);
1091 savebox = gtk_savebox_new(action);
1092 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1094 if (callback == link_cb)
1096 check_relative = gtk_check_button_new_with_mnemonic(
1097 _("_Relative link"));
1098 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1099 last_symlink_check_relative);
1101 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1102 gtk_tooltips_set_tip(tooltips, check_relative,
1103 _("If on, the symlink will store the path from the "
1104 "symlink to the target file. Use this if the symlink "
1105 "and the target will be moved together.\n"
1106 "If off, the path from the root directory is stored - "
1107 "use this if the symlink may move but the target will "
1108 "stay put."), NULL);
1109 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1110 check_relative, FALSE, TRUE, 0);
1111 gtk_widget_show(check_relative);
1114 g_signal_connect(savebox, "save_to_file",
1115 G_CALLBACK(save_to_file), NULL);
1117 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1118 g_strdup(path), g_free);
1119 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1120 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1122 gtk_window_set_title(GTK_WINDOW(savebox), action);
1124 if (g_utf8_validate(path, -1, NULL))
1125 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1126 else
1128 gchar *u8, *dir;
1129 dir = g_path_get_dirname(path);
1130 u8 = to_utf8(g_basename(path));
1131 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1132 make_path(dir, u8));
1133 g_free(u8);
1134 g_free(dir);
1136 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1137 g_object_unref(image);
1139 gtk_widget_show(savebox);
1142 static gint save_to_file(GObject *savebox,
1143 const gchar *pathname, gpointer data)
1145 SaveCb callback;
1146 const gchar *current_path;
1148 callback = g_object_get_data(savebox, "action_callback");
1149 current_path = g_object_get_data(savebox, "current_path");
1151 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1152 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1154 return callback(savebox, current_path, pathname)
1155 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1158 static gboolean copy_cb(GObject *savebox,
1159 const gchar *current, const gchar *new)
1161 return action_with_leaf(action_copy, current, new);
1164 static gboolean action_with_leaf(ActionFn action,
1165 const gchar *current, const gchar *new)
1167 const char *leaf;
1168 char *new_dir;
1169 GList *local_paths;
1171 if (new[0] != '/')
1173 report_error(_("New pathname is not absolute"));
1174 return FALSE;
1177 if (new[strlen(new) - 1] == '/')
1179 new_dir = g_strdup(new);
1180 leaf = NULL;
1182 else
1184 const gchar *slash;
1186 slash = strrchr(new, '/');
1187 new_dir = g_strndup(new, slash - new);
1188 leaf = slash + 1;
1191 local_paths = g_list_append(NULL, (gchar *) current);
1192 action(local_paths, new_dir, leaf, -1);
1193 g_list_free(local_paths);
1195 g_free(new_dir);
1197 return TRUE;
1200 /* Open a savebox to act on the selected file.
1201 * Call 'callback' later to perform the operation.
1203 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1204 const gchar *action, SaveCb callback,
1205 GdkDragAction dnd_action)
1207 g_object_ref(image);
1208 savebox_show(action, path, image, callback, dnd_action);
1211 static gboolean rename_cb(GObject *savebox,
1212 const gchar *current, const gchar *new)
1214 return action_with_leaf(action_move, current, new);
1217 static gboolean link_cb(GObject *savebox,
1218 const gchar *initial, const gchar *path)
1220 GtkToggleButton *check_relative;
1221 struct stat info;
1222 int err;
1223 gchar *link_path;
1225 check_relative = g_object_get_data(savebox, "check_relative");
1227 last_symlink_check_relative = gtk_toggle_button_get_active(check_relative);
1229 if (last_symlink_check_relative)
1230 link_path = get_relative_path(path, initial);
1231 else
1232 link_path = g_strdup(initial);
1234 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1236 GtkWidget *box, *button;
1237 gint ans;
1239 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1240 GTK_BUTTONS_CANCEL,
1241 _("Symlink from '%s' already exists. "
1242 "Replace it with a link to '%s'?"),
1243 path, link_path);
1245 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1247 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1248 gtk_widget_show(button);
1249 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1250 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1251 button, GTK_RESPONSE_OK);
1252 gtk_dialog_set_default_response(GTK_DIALOG(box),
1253 GTK_RESPONSE_OK);
1255 ans = gtk_dialog_run(GTK_DIALOG(box));
1256 gtk_widget_destroy(box);
1258 if (ans != GTK_RESPONSE_OK)
1260 g_free(link_path);
1261 return FALSE;
1264 unlink(path);
1267 err = symlink(link_path, path);
1268 g_free(link_path);
1270 if (err)
1272 report_error("symlink: %s", g_strerror(errno));
1273 return FALSE;
1276 dir_check_this(path);
1278 return TRUE;
1281 static void run_action(DirItem *item)
1283 if (can_set_run_action(item))
1284 type_set_handler_dialog(item->mime_type);
1285 else
1286 report_error(
1287 _("You can only set the run action for a "
1288 "regular file"));
1291 void open_home(gpointer data, guint action, GtkWidget *widget)
1293 filer_opendir(home_dir, NULL, NULL);
1296 static void select_all(gpointer data, guint action, GtkWidget *widget)
1298 g_return_if_fail(window_with_focus != NULL);
1300 window_with_focus->temp_item_selected = FALSE;
1301 view_select_all(window_with_focus->view);
1304 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1306 g_return_if_fail(window_with_focus != NULL);
1308 window_with_focus->temp_item_selected = FALSE;
1309 view_clear_selection(window_with_focus->view);
1312 static gboolean invert_cb(ViewIter *iter, gpointer data)
1314 return !view_get_selected((ViewIface *) data, iter);
1317 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1319 g_return_if_fail(window_with_focus != NULL);
1321 window_with_focus->temp_item_selected = FALSE;
1323 view_select_if(window_with_focus->view, invert_cb,
1324 window_with_focus->view);
1327 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1329 GtkWidget *win;
1331 win = options_show();
1333 if (win)
1335 number_of_windows++;
1336 g_signal_connect(win, "destroy",
1337 G_CALLBACK(one_less_window), NULL);
1341 static gboolean new_directory_cb(GObject *savebox,
1342 const gchar *initial, const gchar *path)
1344 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1346 report_error("mkdir: %s", g_strerror(errno));
1347 return FALSE;
1350 dir_check_this(path);
1352 if (filer_exists(window_with_focus))
1354 guchar *leaf;
1355 leaf = strrchr(path, '/');
1356 if (leaf)
1357 display_set_autoselect(window_with_focus, leaf + 1);
1360 return TRUE;
1363 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1365 g_return_if_fail(window_with_focus != NULL);
1367 savebox_show(_("Create"),
1368 make_path(window_with_focus->sym_path, _("NewDir")),
1369 type_to_icon(inode_directory), new_directory_cb,
1370 GDK_ACTION_COPY);
1373 static gboolean new_file_cb(GObject *savebox,
1374 const gchar *initial, const gchar *path)
1376 int fd;
1378 fd = open(path, O_CREAT | O_EXCL, 0666);
1380 if (fd == -1)
1382 report_error(_("Error creating '%s': %s"),
1383 path, g_strerror(errno));
1384 return FALSE;
1387 if (close(fd))
1388 report_error(_("Error creating '%s': %s"),
1389 path, g_strerror(errno));
1391 dir_check_this(path);
1393 if (filer_exists(window_with_focus))
1395 guchar *leaf;
1396 leaf = strrchr(path, '/');
1397 if (leaf)
1398 display_set_autoselect(window_with_focus, leaf + 1);
1401 return TRUE;
1404 static void new_file(gpointer data, guint action, GtkWidget *widget)
1406 g_return_if_fail(window_with_focus != NULL);
1408 savebox_show(_("Create"),
1409 make_path(window_with_focus->sym_path, _("NewFile")),
1410 type_to_icon(text_plain),
1411 new_file_cb, GDK_ACTION_COPY);
1414 static gboolean new_script_cb(GObject *savebox,
1415 const gchar *initial, const gchar *path)
1417 FILE *fp;
1419 fp = fopen(path, "w");
1421 if (fp == NULL)
1423 report_error(_("Error creating '%s': %s"),
1424 path, g_strerror(errno));
1425 return FALSE;
1428 fprintf(fp, "#!/bin/sh\n");
1429 /* Add 0launch call here, but don't assume it will work */
1430 fprintf(fp, "exec %s/AppRun \"$@\"\n", app_dir);
1432 fclose(fp);
1433 chmod(path, 0755);
1435 dir_check_this(path);
1437 if (filer_exists(window_with_focus))
1439 guchar *leaf;
1440 leaf = strrchr(path, '/');
1441 if (leaf)
1442 display_set_autoselect(window_with_focus, leaf + 1);
1445 return TRUE;
1448 static void new_script(gpointer data, guint action, GtkWidget *widget)
1450 g_return_if_fail(window_with_focus != NULL);
1452 savebox_show(_("Start script"),
1453 make_path(window_with_focus->sym_path, _("rox")),
1454 type_to_icon(application_x_shellscript),
1455 new_script_cb, GDK_ACTION_COPY);
1458 static gboolean new_file_type_cb(GObject *savebox,
1459 const gchar *initial, const gchar *path)
1461 const gchar *oleaf, *leaf;
1462 gchar *templ, *rtempl, *templ_dname, *dest;
1463 GList *paths;
1465 /* We can work out the template path from the initial name */
1466 oleaf = g_basename(initial);
1467 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1468 if (!templ_dname)
1470 report_error(
1471 _("Error creating file: could not find the template for %s"),
1472 oleaf);
1473 return FALSE;
1476 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1477 g_free(templ_dname);
1478 rtempl = pathdup(templ);
1479 g_free(templ);
1481 dest = g_path_get_dirname(path);
1482 leaf = g_basename(path);
1483 paths = g_list_append(NULL, rtempl);
1485 action_copy(paths, dest, leaf, TRUE);
1487 g_list_free(paths);
1488 g_free(dest);
1489 g_free(rtempl);
1491 if (filer_exists(window_with_focus))
1492 display_set_autoselect(window_with_focus, leaf);
1494 return TRUE;
1497 static void do_send_to(gchar *templ)
1499 g_return_if_fail(send_to_paths != NULL);
1501 run_with_files(templ, send_to_paths);
1504 static void new_file_type(gchar *templ)
1506 const gchar *leaf;
1507 MIME_type *type;
1509 g_return_if_fail(window_with_focus != NULL);
1511 leaf = g_basename(templ);
1512 type = type_get_type(templ);
1514 savebox_show(_("Create"),
1515 make_path(window_with_focus->sym_path, leaf),
1516 type_to_icon(type),
1517 new_file_type_cb, GDK_ACTION_COPY);
1520 static void customise_send_to(gpointer data)
1522 GPtrArray *path;
1523 guchar *save;
1524 GString *dirs;
1525 int i;
1527 dirs = g_string_new(NULL);
1529 path = choices_list_xdg_dirs("", SITE);
1530 for (i = 0; i < path->len; i++)
1532 guchar *old = (guchar *) path->pdata[i];
1534 g_string_append(dirs, old);
1535 g_string_append(dirs, "/SendTo\n");
1537 choices_free_list(path);
1539 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1540 if (save)
1541 mkdir(save, 0777);
1543 info_message(
1544 _("The `Send To' menu provides a quick way to send some files "
1545 "to an application. The applications listed are those in "
1546 "the following directories:\n\n%s\n%s\n"
1547 "The `Send To' menu may be opened by Shift+Menu clicking "
1548 "over a file.\n\n"
1549 "Advanced use:\n"
1550 "You can also create subdirectories called "
1551 "`.text_html', `.text', etc which will only be "
1552 "shown for files of that type. `.group' is shown "
1553 "only when multiple files are selected."),
1554 dirs->str,
1555 save ? _("I'll show you your SendTo directory now; you should "
1556 "symlink (Ctrl+Shift drag) any applications you want "
1557 "into it.")
1558 : _("Your CHOICESPATH variable setting prevents "
1559 "customisations - sorry."));
1561 g_string_free(dirs, TRUE);
1563 if (save)
1564 filer_opendir(save, NULL, NULL);
1567 static void customise_new(gpointer data)
1569 GPtrArray *path;
1570 guchar *save;
1571 GString *dirs;
1572 int i;
1574 dirs = g_string_new(NULL);
1576 path = choices_list_xdg_dirs("", SITE);
1577 for (i = 0; i < path->len; i++)
1579 guchar *old = (guchar *) path->pdata[i];
1581 g_string_append(dirs, old);
1582 g_string_append(dirs, "/Templates\n");
1584 choices_free_list(path);
1586 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1587 if (save)
1588 mkdir(save, 0777);
1590 info_message(
1591 _("Any files placed in your Templates directories will "
1592 "appear on the `New' menu. Choosing one of them will make "
1593 "a copy of it as the new file.\n\n"
1594 "The following directories contain templates:\n\n%s\n%s\n"),
1595 dirs->str,
1596 save ? _("I'll show you your Templates directory now; you "
1597 "should place any template files you want inside it.")
1598 : _("Your CHOICESPATH variable setting prevents "
1599 "customisations - sorry."));
1601 g_string_free(dirs, TRUE);
1603 if (save)
1604 filer_opendir(save, NULL, NULL);
1607 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1608 * to the menu.
1610 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1612 gchar *searchdir;
1613 GPtrArray *paths;
1614 int i;
1616 if (subtype)
1617 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1618 else if (type)
1619 searchdir = g_strdup_printf("SendTo/.%s", type);
1620 else
1621 searchdir = g_strdup("SendTo");
1623 paths = choices_list_xdg_dirs(searchdir, SITE);
1624 g_free(searchdir);
1626 for (i = 0; i < paths->len; i++)
1628 GList *widgets = NULL;
1629 guchar *dir = (guchar *) paths->pdata[i];
1631 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1632 (CallbackFn) do_send_to,
1633 FALSE, FALSE, TRUE);
1635 if (widgets)
1636 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1637 gtk_menu_item_new());
1639 g_list_free(widgets); /* TODO: Get rid of this */
1642 choices_free_list(paths);
1645 /* Scan the SendTo dir and create and show the Send To menu.
1646 * The 'paths' list and every path in it is claimed, and will be
1647 * freed later -- don't free it yourself!
1649 static void show_send_to_menu(GList *paths, GdkEvent *event)
1651 GtkWidget *menu, *item;
1653 menu = gtk_menu_new();
1655 if (g_list_length(paths) == 1)
1657 DirItem *item;
1659 item = diritem_new("");
1660 diritem_restat(paths->data, item, NULL);
1662 add_sendto(menu,
1663 item->mime_type->media_type,
1664 item->mime_type->subtype);
1666 add_sendto(menu, item->mime_type->media_type, NULL);
1668 diritem_free(item);
1670 else
1672 GList *rover;
1673 gboolean same=TRUE, same_media=TRUE;
1674 MIME_type *type=NULL;
1675 DirItem *item;
1677 item = diritem_new("");
1678 for(rover=paths; rover; rover=g_list_next(rover))
1680 diritem_restat(rover->data, item, NULL);
1681 if(!type)
1682 type=item->mime_type;
1683 else
1685 if(type!=item->mime_type)
1687 same=FALSE;
1688 if(strcmp(type->media_type,
1689 item->mime_type->media_type)!=0)
1691 same_media=FALSE;
1692 break;
1697 diritem_free(item);
1699 if(type)
1701 if(same)
1702 add_sendto(menu, type->media_type,
1703 type->subtype);
1704 if(same_media)
1705 add_sendto(menu, type->media_type, NULL);
1708 add_sendto(menu, "group", NULL);
1711 add_sendto(menu, NULL, NULL);
1713 item = gtk_menu_item_new_with_label(_("Customise"));
1714 g_signal_connect_swapped(item, "activate",
1715 G_CALLBACK(customise_send_to), NULL);
1716 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1718 if (send_to_paths)
1719 destroy_glist(&send_to_paths);
1721 send_to_paths = paths;
1723 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1725 popup_menu = menu;
1726 show_popup_menu(menu, event, 0);
1729 static void send_to(FilerWindow *filer_window)
1731 GList *paths;
1732 GdkEvent *event;
1734 paths = filer_selected_items(filer_window);
1735 event = gtk_get_current_event();
1737 /* Eats paths */
1738 show_send_to_menu(paths, event);
1740 if (event)
1741 gdk_event_free(event);
1744 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1746 const char *argv[] = {"sh", "-c", NULL, NULL};
1747 gboolean close = action;
1749 argv[2] = o_menu_xterm.value;
1751 g_return_if_fail(window_with_focus != NULL);
1753 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1754 gtk_widget_destroy(window_with_focus->window);
1757 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1759 g_return_if_fail(window_with_focus != NULL);
1761 filer_change_to(window_with_focus, home_dir, NULL);
1764 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1766 g_return_if_fail(window_with_focus != NULL);
1768 bookmarks_show_menu(window_with_focus);
1771 static void show_log(gpointer data, guint action, GtkWidget *widget)
1773 g_return_if_fail(window_with_focus != NULL);
1775 log_show_window();
1778 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1780 g_return_if_fail(window_with_focus != NULL);
1782 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1783 filer_change_to(window_with_focus,
1784 window_with_focus->real_path, NULL);
1785 else
1786 delayed_error(_("This is already the canonical name "
1787 "for this directory."));
1790 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1792 g_return_if_fail(window_with_focus != NULL);
1794 filer_open_parent(window_with_focus);
1797 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1799 g_return_if_fail(window_with_focus != NULL);
1801 change_to_parent(window_with_focus);
1804 static void resize(gpointer data, guint action, GtkWidget *widget)
1806 g_return_if_fail(window_with_focus != NULL);
1808 view_autosize(window_with_focus->view);
1811 static void new_window(gpointer data, guint action, GtkWidget *widget)
1813 g_return_if_fail(window_with_focus != NULL);
1815 if (o_unique_filer_windows.int_value)
1817 report_error(_("You can't open a second view onto "
1818 "this directory because the `Unique Windows' option "
1819 "is turned on in the Options window."));
1821 else
1822 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1825 static void close_window(gpointer data, guint action, GtkWidget *widget)
1827 g_return_if_fail(window_with_focus != NULL);
1829 if (!filer_window_delete(window_with_focus->window, NULL,
1830 window_with_focus))
1831 gtk_widget_destroy(window_with_focus->window);
1834 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1836 MiniType type = (MiniType) action;
1838 g_return_if_fail(window_with_focus != NULL);
1840 /* Item needs to remain selected... */
1841 if (type == MINI_SHELL)
1842 window_with_focus->temp_item_selected = FALSE;
1844 minibuffer_show(window_with_focus, type);
1847 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1849 if (action == HELP_ABOUT)
1850 infobox_new(app_dir);
1851 else if (action == HELP_DIR)
1852 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1853 else if (action == HELP_MANUAL)
1855 gchar *manual = NULL;
1857 if (current_lang)
1859 manual = g_strconcat(app_dir, "/Help/Manual-",
1860 current_lang, ".html", NULL);
1861 if (!file_exists(manual) && strchr(current_lang, '_'))
1863 /* Try again without the territory */
1864 strcpy(strrchr(manual, '_'), ".html");
1866 if (!file_exists(manual))
1867 null_g_free(&manual);
1870 if (!manual)
1871 manual = g_strconcat(app_dir,
1872 "/Help/Manual.html", NULL);
1874 run_by_path(manual);
1876 g_free(manual);
1878 else
1879 g_warning("Unknown help action %d\n", action);
1882 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1883 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1885 GList *items, *item;
1887 items = gtk_container_get_children(GTK_CONTAINER(menu));
1889 item = g_list_nth(items, from);
1890 while (item && n--)
1892 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1893 item = item->next;
1895 g_list_free(items);
1898 static void save_menus(void)
1900 char *menurc;
1902 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1903 if (menurc)
1905 gtk_accel_map_save(menurc);
1906 g_free(menurc);
1910 static void select_nth_item(GtkMenuShell *shell, int n)
1912 GList *items;
1913 GtkWidget *item;
1915 items = gtk_container_get_children(GTK_CONTAINER(shell));
1916 item = g_list_nth_data(items, n);
1918 g_return_if_fail(item != NULL);
1920 g_list_free(items);
1922 gtk_menu_shell_select_item(shell, item);
1925 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1927 DirItem *item;
1928 const guchar *path;
1929 int n_selected;
1930 ViewIter iter;
1932 g_return_if_fail(window_with_focus != NULL);
1934 n_selected = view_count_selected(window_with_focus->view);
1936 if (n_selected < 1)
1938 const char *prompt;
1940 switch (action)
1942 case FILE_COPY_ITEM:
1943 prompt = _("Copy ... ?");
1944 break;
1945 case FILE_RENAME_ITEM:
1946 prompt = _("Rename ... ?");
1947 break;
1948 case FILE_LINK_ITEM:
1949 prompt = _("Symlink ... ?");
1950 break;
1951 case FILE_OPEN_FILE:
1952 prompt = _("Shift Open ... ?");
1953 break;
1954 case FILE_PROPERTIES:
1955 prompt = _("Properties of ... ?");
1956 break;
1957 case FILE_SET_TYPE:
1958 prompt = _("Set type of ... ?");
1959 break;
1960 case FILE_RUN_ACTION:
1961 prompt = _("Set run action for ... ?");
1962 break;
1963 case FILE_SET_ICON:
1964 prompt = _("Set icon for ... ?");
1965 break;
1966 case FILE_SEND_TO:
1967 prompt = _("Send ... to ... ?");
1968 break;
1969 case FILE_DELETE:
1970 prompt = _("DELETE ... ?");
1971 break;
1972 case FILE_USAGE:
1973 prompt = _("Count the size of ... ?");
1974 break;
1975 case FILE_CHMOD_ITEMS:
1976 prompt = _("Set permissions on ... ?");
1977 break;
1978 case FILE_FIND:
1979 prompt = _("Search inside ... ?");
1980 break;
1981 default:
1982 g_warning("Unknown action!");
1983 return;
1985 filer_target_mode(window_with_focus, target_callback,
1986 GINT_TO_POINTER(action), prompt);
1987 return;
1990 switch (action)
1992 case FILE_SEND_TO:
1993 send_to(window_with_focus);
1994 return;
1995 case FILE_DELETE:
1996 delete(window_with_focus);
1997 return;
1998 case FILE_USAGE:
1999 usage(window_with_focus);
2000 return;
2001 case FILE_CHMOD_ITEMS:
2002 chmod_items(window_with_focus);
2003 return;
2004 case FILE_SET_TYPE:
2005 set_type_items(window_with_focus);
2006 return;
2007 case FILE_FIND:
2008 find(window_with_focus);
2009 return;
2010 case FILE_PROPERTIES:
2012 GList *items;
2014 items = filer_selected_items(window_with_focus);
2015 infobox_show_list(items);
2016 destroy_glist(&items);
2017 return;
2019 case FILE_RENAME_ITEM:
2020 if (n_selected > 1)
2022 GList *items = NULL;
2023 ViewIter iter;
2025 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
2026 while ((item = iter.next(&iter)))
2027 items = g_list_prepend(items, item->leafname);
2028 items = g_list_reverse(items);
2030 bulk_rename(window_with_focus->sym_path, items);
2031 g_list_free(items);
2032 return;
2034 break; /* Not a bulk rename... see below */
2035 default:
2036 break;
2039 /* All the following actions require exactly one file selected */
2041 if (n_selected > 1)
2043 report_error(_("You cannot do this to more than "
2044 "one item at a time"));
2045 return;
2048 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
2050 item = iter.next(&iter);
2051 g_return_if_fail(item != NULL);
2052 /* iter may be passed to filer_openitem... */
2054 if (item->base_type == TYPE_UNKNOWN)
2055 item = dir_update_item(window_with_focus->directory,
2056 item->leafname);
2058 if (!item)
2060 report_error(_("Item no longer exists!"));
2061 return;
2064 path = make_path(window_with_focus->sym_path, item->leafname);
2066 switch (action)
2068 case FILE_COPY_ITEM:
2069 src_dest_action_item(path, di_image(item),
2070 _("Copy"), copy_cb,
2071 GDK_ACTION_COPY);
2072 break;
2073 case FILE_RENAME_ITEM:
2074 src_dest_action_item(path, di_image(item),
2075 _("Rename"), rename_cb,
2076 GDK_ACTION_MOVE);
2077 break;
2078 case FILE_LINK_ITEM:
2079 src_dest_action_item(path, di_image(item),
2080 _("Symlink"), link_cb,
2081 GDK_ACTION_LINK);
2082 break;
2083 case FILE_OPEN_FILE:
2084 filer_openitem(window_with_focus, &iter,
2085 OPEN_SAME_WINDOW | OPEN_SHIFT);
2086 break;
2087 case FILE_RUN_ACTION:
2088 run_action(item);
2089 break;
2090 case FILE_SET_ICON:
2091 icon_set_handler_dialog(item, path);
2092 break;
2093 default:
2094 g_warning("Unknown action!");
2095 return;
2099 static void show_key_help(GtkWidget *button, gpointer data)
2101 gboolean can_change_accels;
2103 g_object_get(G_OBJECT(gtk_settings_get_default()),
2104 "gtk-can-change-accels", &can_change_accels,
2105 NULL);
2107 if (!can_change_accels)
2109 info_message(_("User-definable shortcuts are disabled by "
2110 "default in Gtk2, and you have not enabled "
2111 "them. You can turn this feature on by:\n\n"
2112 "1) using an XSettings manager, such as ROX-Session "
2113 "or gnome-settings-daemon, or\n\n"
2114 "2) adding this line to ~/.gtkrc-2.0:\n"
2115 "\tgtk-can-change-accels = 1\n"
2116 "\t(this only works if NOT using XSETTINGS)"));
2117 return;
2120 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2121 "- Open the menu over a filer window,\n"
2122 "- Move the pointer over the item you want to use,\n"
2123 "- Press the key you want attached to it.\n\n"
2124 "The key will appear next to the menu item and you can just press "
2125 "that key without opening the menu in future."));
2128 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2130 GtkWidget *button, *align;
2132 g_return_val_if_fail(option == NULL, NULL);
2134 align = gtk_alignment_new(0, 0.5, 0, 0);
2135 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2136 gtk_container_add(GTK_CONTAINER(align), button);
2137 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2139 return g_list_append(NULL, align);