Show check sign in Display menu for sort type
[rox-filer/bju.git] / ROX-Filer / src / menu.c
blobcdaee790dba3b92606f62326028c7503aa8e8a1b
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 customise_new(gpointer data);
149 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
151 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
152 static void open_parent(gpointer data, guint action, GtkWidget *widget);
153 static void home_directory(gpointer data, guint action, GtkWidget *widget);
154 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
155 static void show_log(gpointer data, guint action, GtkWidget *widget);
156 static void new_window(gpointer data, guint action, GtkWidget *widget);
157 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
158 static void close_window(gpointer data, guint action, GtkWidget *widget);
159 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
161 /* (action used in this - MiniType) */
162 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
163 static void resize(gpointer data, guint action, GtkWidget *widget);
165 #define MENUS_NAME "menus2"
167 static GtkWidget *filer_menu; /* The popup filer menu */
168 static GtkWidget *filer_file_item; /* The File '' label */
169 static GtkWidget *filer_file_menu; /* The File '' menu */
170 static GtkWidget *file_shift_item; /* Shift Open label */
171 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
172 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
173 static GtkWidget *filer_filter_dirs_menu; /* The Filter Dirs item */
174 static GtkWidget *filer_sort_by_name_menu; /* The Sort by Name item */
175 static GtkWidget *filer_sort_by_type_menu; /* The Sort by Type item */
176 static GtkWidget *filer_sort_by_date_menu; /* The Sort by Date item */
177 static GtkWidget *filer_sort_by_size_menu; /* The Sort by Size item */
178 static GtkWidget *filer_sort_by_owner_menu; /* The Sort by Owner item */
179 static GtkWidget *filer_sort_by_group_menu; /* The Sort by Group item */
180 static GtkWidget *filer_reverse_menu; /* The Reversed item */
181 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
182 static GtkWidget *filer_new_window; /* The New Window item */
183 static GtkWidget *filer_new_menu; /* The New submenu */
184 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
185 static GtkWidget *filer_set_type; /* Set type item */
187 #undef N_
188 #define N_(x) x
190 static GtkItemFactoryEntry filer_menu_def[] = {
191 {N_("Display"), NULL, NULL, 0, "<Branch>"},
192 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
193 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
194 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
195 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
196 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
197 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
198 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
199 {">", NULL, NULL, 0, "<Separator>"},
200 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
201 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
202 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
203 {">", NULL, NULL, 0, "<Separator>"},
204 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, "<ToggleItem>"},
205 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, "<ToggleItem>"},
206 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, "<ToggleItem>"},
207 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, "<ToggleItem>"},
208 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, "<ToggleItem>"},
209 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, "<ToggleItem>"},
210 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
211 {">", NULL, NULL, 0, "<Separator>"},
212 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
213 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
214 {">" N_("Filter Directories With Files"), NULL, filter_directories, 0, "<ToggleItem>"},
215 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
216 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
217 {">" N_("Save Current Display Settings..."), NULL, save_settings, 0, NULL},
218 {N_("File"), NULL, NULL, 0, "<Branch>"},
219 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
220 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
221 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
222 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
223 {">", NULL, NULL, 0, "<Separator>"},
224 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
225 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
226 {">", NULL, NULL, 0, "<Separator>"},
227 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
228 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
229 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
230 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
231 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
232 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
233 {">", NULL, NULL, 0, "<Separator>"},
234 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
235 {N_("Select"), NULL, NULL, 0, "<Branch>"},
236 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
237 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
238 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
239 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
240 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
241 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
242 {N_("New"), NULL, NULL, 0, "<Branch>"},
243 {">" N_("Directory"), NULL, new_directory, 0, NULL},
244 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
245 {">" N_("Customise Menu..."), NULL, customise_new, 0, NULL},
246 {N_("Window"), NULL, NULL, 0, "<Branch>"},
247 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
248 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
249 {">" N_("New Window"), NULL, new_window, 0, NULL},
250 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
251 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
252 {">" N_("Show Log"), NULL, show_log, 0, "<StockItem>", GTK_STOCK_INFO},
253 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
254 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
255 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
257 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
258 {">", NULL, NULL, 0, "<Separator>"},
259 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
260 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
261 {">" N_("Terminal Here"), "grave", xterm_here, FALSE, NULL},
262 {">" N_("Switch to Terminal"), NULL, xterm_here, TRUE, NULL},
263 {N_("Help"), NULL, NULL, 0, "<Branch>"},
264 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
265 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
266 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
270 #define GET_MENU_ITEM(var, menu) \
271 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
273 #define GET_SMENU_ITEM(var, menu, sub) \
274 do { \
275 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
276 var = gtk_item_factory_get_widget(item_factory, tmp); \
277 g_free(tmp); \
278 } while (0)
280 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
281 do { \
282 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
283 var = gtk_item_factory_get_widget(item_factory, tmp); \
284 g_free(tmp); \
285 } while (0)
287 /* Returns TRUE if the keys were installed (first call only) */
288 gboolean ensure_filer_menu(void)
290 GList *items;
291 guchar *tmp;
292 GtkWidget *item;
293 GtkItemFactory *item_factory;
295 if (!filer_keys_need_init)
296 return FALSE;
297 filer_keys_need_init = FALSE;
299 item_factory = menu_create(filer_menu_def,
300 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
301 "<filer>", filer_keys);
303 GET_MENU_ITEM(filer_menu, "filer");
304 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
305 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
306 GET_SSMENU_ITEM(filer_sort_by_name_menu, "filer", "Display", "Sort by Name");
307 GET_SSMENU_ITEM(filer_sort_by_type_menu, "filer", "Display", "Sort by Type");
308 GET_SSMENU_ITEM(filer_sort_by_date_menu, "filer", "Display", "Sort by Date");
309 GET_SSMENU_ITEM(filer_sort_by_size_menu, "filer", "Display", "Sort by Size");
310 GET_SSMENU_ITEM(filer_sort_by_owner_menu, "filer", "Display", "Sort by Owner");
311 GET_SSMENU_ITEM(filer_sort_by_group_menu, "filer", "Display", "Sort by Group");
312 GET_SSMENU_ITEM(filer_filter_dirs_menu, "filer", "Display", "Filter Directories With Files");
313 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
314 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
315 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
316 "Show Thumbnails");
317 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
318 filer_set_type = GTK_BIN(item)->child;
320 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
321 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
322 filer_follow_sym = GTK_BIN(item)->child;
324 /* File '' label... */
325 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
326 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
327 g_list_free(items);
329 /* Shift Open... label */
330 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
331 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
332 g_list_free(items);
334 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
335 filer_new_window = GTK_BIN(item)->child;
337 g_signal_connect(filer_menu, "unmap_event",
338 G_CALLBACK(menu_closed), NULL);
339 g_signal_connect(filer_file_menu, "unmap_event",
340 G_CALLBACK(menu_closed), NULL);
342 g_signal_connect(filer_keys, "accel_changed",
343 G_CALLBACK(save_menus), NULL);
345 return TRUE;
348 void menu_init(void)
350 char *menurc;
352 menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
353 if (menurc)
355 gtk_accel_map_load(menurc);
356 g_free(menurc);
359 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
360 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
361 option_add_int(&o_menu_quick, "menu_quick", FALSE);
362 option_add_saver(save_menus);
364 option_register_widget("menu-set-keys", set_keys_button);
366 filer_keys = gtk_accel_group_new();
369 /* Name is in the form "<panel>" */
370 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
371 const gchar *name, GtkAccelGroup *keys)
373 GtkItemFactory *item_factory;
374 GtkItemFactoryEntry *translated;
376 if (!keys)
378 keys = gtk_accel_group_new();
379 gtk_accel_group_lock(keys);
382 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
384 translated = translate_entries(def, n_entries);
385 gtk_item_factory_create_items(item_factory, n_entries,
386 translated, NULL);
387 free_translated_entries(translated, n_entries);
389 return item_factory;
392 /* Prevent the user from setting a short-cut on this item */
393 static void menuitem_no_shortcuts(GtkWidget *item)
395 /* XXX */
396 #if 0
397 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
399 _gtk_widget_set_accel_path(item, NULL, NULL);
400 null_g_free(&menuitem->accel_path);
401 #endif
404 /* Shade items that only work on single files */
405 static void shade_file_menu_items(gboolean shaded)
407 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
408 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
409 menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
410 menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
413 /* 'data' is an array of three ints:
414 * [ pointer_x, pointer_y, item_under_pointer ]
416 void position_menu(GtkMenu *menu, gint *x, gint *y,
417 gboolean *push_in, gpointer data)
419 int *pos = (int *) data;
420 GtkRequisition requisition;
421 GList *items, *next;
422 int y_shift = 0;
423 int item = pos[2];
425 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
427 while (item >= 0 && next)
429 int h = ((GtkWidget *) next->data)->requisition.height;
431 if (item > 0)
432 y_shift += h;
433 else
434 y_shift += h / 2;
436 next = next->next;
437 item--;
440 g_list_free(items);
442 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
444 *x = pos[0] - (requisition.width * 7 / 8);
445 *y = pos[1] - y_shift;
447 *x = CLAMP(*x, 0, screen_width - requisition.width);
448 *y = CLAMP(*y, 0, screen_height - requisition.height);
450 *push_in = FALSE;
453 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
454 MenuIconStyle style)
456 GtkWidget *item;
458 if (style != MIS_NONE && di_image(ditem))
460 GdkPixbuf *pixbuf;
461 MaskedPixmap *image;
463 image = di_image(ditem);
465 switch (style)
467 case MIS_LARGE:
468 pixbuf = image->pixbuf;
469 break;
470 default:
471 if (!image->sm_pixbuf)
472 pixmap_make_small(image);
473 pixbuf = image->sm_pixbuf;
474 break;
477 item = gtk_image_menu_item_new_with_label(label);
478 /* TODO: Find a way to allow short-cuts */
479 menuitem_no_shortcuts(item);
481 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
482 gtk_image_new_from_pixbuf(pixbuf));
483 gtk_widget_show_all(item);
485 else
486 item = gtk_menu_item_new_with_label(label);
488 return item;
491 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
492 MenuIconStyle style, CallbackFn func,
493 gboolean separator, gboolean strip_ext,
494 gboolean recurse)
496 GList *widgets = NULL;
497 DirItem *ditem;
498 int i;
499 GtkWidget *item;
500 char *dname = NULL;
501 GPtrArray *names;
503 dname = pathdup(dir_name);
505 names = list_dir(dname);
506 if (!names)
507 goto out;
509 ditem = diritem_new("");
511 for (i = 0; i < names->len; i++)
513 char *leaf = names->pdata[i];
514 gchar *fname;
516 if (separator)
518 item = gtk_menu_item_new();
519 widgets = g_list_append(widgets, item);
520 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
521 separator = FALSE;
524 fname = g_build_filename(dname, leaf, NULL);
526 /* Strip off extension, if any */
527 if (strip_ext)
529 char *dot;
530 dot = strchr(leaf, '.');
531 if (dot)
532 *dot = '\0';
535 diritem_restat(fname, ditem, NULL);
537 if (ditem->base_type != TYPE_FILE
538 && (ditem->base_type != TYPE_DIRECTORY
539 || (!recurse && !(ditem->flags & ITEM_FLAG_APPDIR))))
541 g_free(fname);
542 goto end_names_loop;
545 item = make_send_to_item(ditem, leaf, style);
547 /* If it is a directory (but NOT an AppDir) and we are
548 * recursing then set up a sub menu.
550 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
551 !(ditem->flags & ITEM_FLAG_APPDIR))
553 GtkWidget *sub;
554 GList *new_widgets;
556 sub = gtk_menu_new();
557 new_widgets = menu_from_dir(sub, fname, style, func,
558 separator, strip_ext, TRUE);
559 g_list_free(new_widgets);
560 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
562 else
563 g_signal_connect_swapped(item, "activate",
564 G_CALLBACK(func), fname);
566 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
567 g_signal_connect_swapped(item, "destroy",
568 G_CALLBACK(g_free), fname);
570 widgets = g_list_append(widgets, item);
572 end_names_loop:
573 g_free(leaf);
576 diritem_free(ditem);
577 g_ptr_array_free(names, TRUE);
578 out:
579 g_free(dname);
581 return widgets;
584 /* Scan the templates dir and create entries for the New menu */
585 static void update_new_files_menu(MenuIconStyle style)
587 static GList *widgets = NULL;
589 gchar *templ_dname = NULL;
591 if (widgets)
593 GList *next;
595 for (next = widgets; next; next = next->next)
596 gtk_widget_destroy((GtkWidget *) next->data);
598 g_list_free(widgets);
599 widgets = NULL;
602 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
603 if (templ_dname)
605 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
606 (CallbackFn) new_file_type, TRUE, TRUE,
607 FALSE);
608 g_free(templ_dname);
610 gtk_widget_show_all(filer_new_menu);
613 /* 'item' is the number of the item to appear under the pointer. */
614 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
616 int pos[3];
617 int button = 0;
618 guint32 time = 0;
620 if (event && (event->type == GDK_BUTTON_PRESS ||
621 event->type == GDK_BUTTON_RELEASE))
623 GdkEventButton *bev = (GdkEventButton *) event;
625 pos[0] = bev->x_root;
626 pos[1] = bev->y_root;
627 button = bev->button;
628 time = bev->time;
630 else if (event && event->type == GDK_KEY_PRESS)
632 GdkEventKey *kev = (GdkEventKey *) event;
634 get_pointer_xy(pos, pos + 1);
635 time = kev->time;
637 else
638 get_pointer_xy(pos, pos + 1);
640 pos[2] = item;
642 gtk_widget_show_all(menu);
643 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
644 position_menu, (gpointer) pos, button, time);
645 select_nth_item(GTK_MENU_SHELL(menu), item);
648 /* Hide the popup menu, if any */
649 void menu_popdown(void)
651 if (popup_menu)
652 gtk_menu_popdown(GTK_MENU(popup_menu));
655 static MenuIconStyle get_menu_icon_style(void)
657 MenuIconStyle mis;
658 int display;
660 mis = o_menu_iconsize.int_value;
662 switch (mis)
664 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
665 return mis;
666 default:
667 break;
670 if (mis == MIS_CURRENT && window_with_focus)
672 switch (window_with_focus->display_style)
674 case HUGE_ICONS:
675 case LARGE_ICONS:
676 return MIS_LARGE;
677 case SMALL_ICONS:
678 return MIS_SMALL;
679 default:
680 break;
684 display = o_display_size.int_value;
685 switch (display)
687 case HUGE_ICONS:
688 case LARGE_ICONS:
689 return MIS_LARGE;
690 case SMALL_ICONS:
691 return MIS_SMALL;
692 default:
693 break;
696 return MIS_SMALL;
699 /* iter->peek() is the clicked item, or NULL if none */
700 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
702 DirItem *file_item = NULL;
703 GdkModifierType state = 0;
704 int n_selected;
705 int n_added = 0;
707 g_return_if_fail(event != NULL);
709 n_selected = view_count_selected(filer_window->view);
711 ensure_filer_menu();
713 updating_menu++;
715 /* Remove previous AppMenu, if any */
716 appmenu_remove();
718 window_with_focus = filer_window;
720 if (event->type == GDK_BUTTON_PRESS)
721 state = ((GdkEventButton *) event)->state;
722 else if (event->type == GDK_KEY_PRESS)
723 state = ((GdkEventKey *) event)->state;
725 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
727 filer_window->temp_item_selected = TRUE;
728 view_set_selected(filer_window->view, iter, TRUE);
729 n_selected = view_count_selected(filer_window->view);
731 else
733 filer_window->temp_item_selected = FALSE;
736 /* Short-cut to the Send To menu */
737 if (state & GDK_SHIFT_MASK)
739 GList *paths;
741 updating_menu--;
743 if (n_selected == 0)
745 report_error(
746 _("You should Shift+Menu click over a file to "
747 "send it somewhere"));
748 return;
751 paths = filer_selected_items(filer_window);
753 show_send_to_menu(paths, event); /* (paths eaten) */
755 return;
759 GtkWidget *file_label, *file_menu;
760 GString *buffer;
761 DirItem *item;
763 file_label = filer_file_item;
764 file_menu = filer_file_menu;
765 gtk_check_menu_item_set_active(
766 GTK_CHECK_MENU_ITEM(filer_sort_by_name_menu),
767 filer_window->sort_type == SORT_NAME);
768 gtk_check_menu_item_set_active(
769 GTK_CHECK_MENU_ITEM(filer_sort_by_type_menu),
770 filer_window->sort_type == SORT_TYPE);
771 gtk_check_menu_item_set_active(
772 GTK_CHECK_MENU_ITEM(filer_sort_by_date_menu),
773 filer_window->sort_type == SORT_DATE);
774 gtk_check_menu_item_set_active(
775 GTK_CHECK_MENU_ITEM(filer_sort_by_size_menu),
776 filer_window->sort_type == SORT_SIZE);
777 gtk_check_menu_item_set_active(
778 GTK_CHECK_MENU_ITEM(filer_sort_by_owner_menu),
779 filer_window->sort_type == SORT_OWNER);
780 gtk_check_menu_item_set_active(
781 GTK_CHECK_MENU_ITEM(filer_sort_by_group_menu),
782 filer_window->sort_type == SORT_GROUP);
783 gtk_check_menu_item_set_active(
784 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
785 filer_window->show_thumbs);
786 gtk_check_menu_item_set_active(
787 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
788 filer_window->show_hidden);
789 gtk_check_menu_item_set_active(
790 GTK_CHECK_MENU_ITEM(filer_filter_dirs_menu),
791 filer_window->filter_directories);
792 gtk_check_menu_item_set_active(
793 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
794 filer_window->sort_order != GTK_SORT_ASCENDING);
795 gtk_check_menu_item_set_active(
796 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
797 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
798 buffer = g_string_new(NULL);
800 switch (n_selected)
802 case 0:
803 g_string_assign(buffer, _("Next Click"));
804 shade_file_menu_items(FALSE);
805 break;
806 case 1:
807 item = filer_selected_item(filer_window);
808 if (item->base_type == TYPE_UNKNOWN)
809 dir_update_item(filer_window->directory,
810 item->leafname);
811 shade_file_menu_items(FALSE);
812 file_item = filer_selected_item(filer_window);
813 g_string_printf(buffer, _("%s '%s'"),
814 basetype_name(file_item),
815 g_utf8_validate(file_item->leafname,
816 -1, NULL)
817 ? file_item->leafname
818 : _("(bad utf-8)"));
819 if (!can_set_run_action(file_item))
820 menu_set_items_shaded(filer_file_menu,
821 TRUE, 8, 1);
822 break;
823 default:
824 shade_file_menu_items(TRUE);
825 g_string_printf(buffer, _("%d items"),
826 n_selected);
827 break;
829 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
830 g_string_free(buffer, TRUE);
832 menu_show_shift_action(file_shift_item, file_item,
833 n_selected == 0);
834 if (file_item)
835 n_added = appmenu_add(make_path(filer_window->sym_path,
836 file_item->leafname),
837 file_item, filer_file_menu);
840 update_new_files_menu(get_menu_icon_style());
842 gtk_widget_set_sensitive(filer_new_window,
843 !o_unique_filer_windows.int_value);
844 gtk_widget_set_sensitive(filer_follow_sym,
845 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
846 gtk_widget_set_sensitive(filer_set_type,
847 xattr_supported(filer_window->real_path));
849 if (n_selected && o_menu_quick.int_value)
850 popup_menu = (state & GDK_CONTROL_MASK)
851 ? filer_menu
852 : filer_file_menu;
853 else
854 popup_menu = (state & GDK_CONTROL_MASK)
855 ? filer_file_menu
856 : filer_menu;
858 updating_menu--;
860 show_popup_menu(popup_menu, event,
861 popup_menu == filer_file_menu ? n_added : 1);
864 static void menu_closed(GtkWidget *widget)
866 if (window_with_focus == NULL || widget != popup_menu)
867 return; /* Close panel item chosen? */
869 popup_menu = NULL;
871 if (window_with_focus->temp_item_selected)
873 view_clear_selection(window_with_focus->view);
874 window_with_focus->temp_item_selected = FALSE;
877 appmenu_remove();
880 static void target_callback(FilerWindow *filer_window,
881 ViewIter *iter,
882 gpointer action)
884 g_return_if_fail(filer_window != NULL);
886 window_with_focus = filer_window;
888 /* Don't grab the primary selection */
889 filer_window->temp_item_selected = TRUE;
891 view_wink_item(filer_window->view, iter);
892 view_select_only(filer_window->view, iter);
893 file_op(NULL, GPOINTER_TO_INT(action), NULL);
895 view_clear_selection(filer_window->view);
896 filer_window->temp_item_selected = FALSE;
899 /* Set the text of the 'Shift Open...' menu item.
900 * If icon is NULL, reset the text and also shade it, unless 'next'.
902 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
904 guchar *shift_action = NULL;
906 if (item)
908 if (item->flags & ITEM_FLAG_MOUNT_POINT)
910 if (item->flags & ITEM_FLAG_MOUNTED)
911 shift_action = N_("Unmount");
912 else
913 shift_action = N_("Open unmounted");
915 else if (item->flags & ITEM_FLAG_SYMLINK)
916 shift_action = N_("Show Target");
917 else if (item->base_type == TYPE_DIRECTORY)
918 shift_action = N_("Look Inside");
919 else if (item->base_type == TYPE_FILE)
920 shift_action = N_("Open As Text");
922 gtk_label_set_text(GTK_LABEL(menu_item),
923 shift_action ? _(shift_action)
924 : _("Shift Open"));
925 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
928 /* Actions */
930 static void view_type(gpointer data, guint action, GtkWidget *widget)
932 ViewType view_type = (ViewType) action;
934 g_return_if_fail(window_with_focus != NULL);
936 if (view_type == VIEW_TYPE_COLLECTION)
937 display_set_layout(window_with_focus,
938 window_with_focus->display_style_wanted,
939 DETAILS_NONE, FALSE);
941 filer_set_view_type(window_with_focus, (ViewType) action);
944 static void change_size(gpointer data, guint action, GtkWidget *widget)
946 g_return_if_fail(window_with_focus != NULL);
948 display_change_size(window_with_focus, action == 1);
951 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
953 g_return_if_fail(window_with_focus != NULL);
955 if (updating_menu)
956 return;
958 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
959 display_set_layout(window_with_focus,
960 window_with_focus->display_style,
961 window_with_focus->details_type, FALSE);
962 else
963 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
964 window_with_focus->details_type, FALSE);
967 static void set_with(gpointer data, guint action, GtkWidget *widget)
969 DisplayStyle size;
971 g_return_if_fail(window_with_focus != NULL);
973 size = window_with_focus->display_style_wanted;
975 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
976 display_set_layout(window_with_focus, size, action, FALSE);
979 static void set_sort(gpointer data, guint action, GtkWidget *widget)
981 if (updating_menu)
982 return;
984 g_return_if_fail(window_with_focus != NULL);
986 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
989 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
991 GtkSortType order;
993 if (updating_menu)
994 return;
996 g_return_if_fail(window_with_focus != NULL);
998 order = window_with_focus->sort_order;
999 if (order == GTK_SORT_ASCENDING)
1000 order = GTK_SORT_DESCENDING;
1001 else
1002 order = GTK_SORT_ASCENDING;
1004 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
1005 order);
1008 static void hidden(gpointer data, guint action, GtkWidget *widget)
1010 if (updating_menu)
1011 return;
1013 g_return_if_fail(window_with_focus != NULL);
1015 display_set_hidden(window_with_focus,
1016 !window_with_focus->show_hidden);
1019 static void filter_directories(gpointer data, guint action, GtkWidget *widget)
1021 if (updating_menu)
1022 return;
1024 g_return_if_fail(window_with_focus != NULL);
1026 display_set_filter_directories(window_with_focus,
1027 !window_with_focus->filter_directories);
1030 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
1032 if (updating_menu)
1033 return;
1035 g_return_if_fail(window_with_focus != NULL);
1037 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
1040 static void refresh(gpointer data, guint action, GtkWidget *widget)
1042 g_return_if_fail(window_with_focus != NULL);
1044 filer_refresh(window_with_focus);
1047 static void save_settings(gpointer data, guint action, GtkWidget *widget)
1049 g_return_if_fail(window_with_focus != NULL);
1051 filer_save_settings(window_with_focus);
1054 static void delete(FilerWindow *filer_window)
1056 GList *paths;
1057 paths = filer_selected_items(filer_window);
1058 action_delete(paths);
1059 destroy_glist(&paths);
1062 static void usage(FilerWindow *filer_window)
1064 GList *paths;
1065 paths = filer_selected_items(filer_window);
1066 action_usage(paths);
1067 destroy_glist(&paths);
1070 static void chmod_items(FilerWindow *filer_window)
1072 GList *paths;
1073 paths = filer_selected_items(filer_window);
1074 action_chmod(paths, FALSE, NULL);
1075 destroy_glist(&paths);
1078 static void set_type_items(FilerWindow *filer_window)
1080 GList *paths, *p;
1081 int npass=0, nfail=0;
1083 paths = filer_selected_items(filer_window);
1084 for(p=paths; p; p=g_list_next(p)) {
1085 if(xattr_supported((const char *) p->data))
1086 npass++;
1087 else
1088 nfail++;
1090 if(npass==0)
1091 report_error(_("Extended attributes, used to store types, are not supported for this "
1092 "file or files.\n"
1093 "This may be due to lack of support from the filesystem or the C library, "
1094 "or it may simply be that the filesystem needs to be mounted with "
1095 "the right mount option ('user_xattr' on Linux)."));
1096 else if(nfail>0)
1097 report_error(_("Setting type not supported for some of these files"));
1098 if(npass>0)
1099 action_settype(paths, FALSE, NULL);
1100 destroy_glist(&paths);
1103 static void find(FilerWindow *filer_window)
1105 GList *paths;
1106 paths = filer_selected_items(filer_window);
1107 action_find(paths);
1108 destroy_glist(&paths);
1111 static gboolean last_symlink_check_relative = TRUE;
1113 /* This creates a new savebox widget, and allows the user to pick a new path
1114 * for the file.
1115 * Once the new path has been picked, the callback will be called with
1116 * both the current and new paths.
1117 * NOTE: This function unrefs 'image'!
1119 static void savebox_show(const gchar *action, const gchar *path,
1120 MaskedPixmap *image, SaveCb callback,
1121 GdkDragAction dnd_action)
1123 GtkWidget *savebox = NULL;
1124 GtkWidget *check_relative = NULL;
1126 g_return_if_fail(image != NULL);
1128 savebox = gtk_savebox_new(action);
1129 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1131 if (callback == link_cb)
1133 check_relative = gtk_check_button_new_with_mnemonic(
1134 _("_Relative link"));
1135 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1136 last_symlink_check_relative);
1138 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1139 gtk_tooltips_set_tip(tooltips, check_relative,
1140 _("If on, the symlink will store the path from the "
1141 "symlink to the target file. Use this if the symlink "
1142 "and the target will be moved together.\n"
1143 "If off, the path from the root directory is stored - "
1144 "use this if the symlink may move but the target will "
1145 "stay put."), NULL);
1146 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1147 check_relative, FALSE, TRUE, 0);
1148 gtk_widget_show(check_relative);
1151 g_signal_connect(savebox, "save_to_file",
1152 G_CALLBACK(save_to_file), NULL);
1154 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1155 g_strdup(path), g_free);
1156 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1157 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1159 gtk_window_set_title(GTK_WINDOW(savebox), action);
1161 if (g_utf8_validate(path, -1, NULL))
1162 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1163 else
1165 gchar *u8, *dir;
1166 dir = g_path_get_dirname(path);
1167 u8 = to_utf8(g_basename(path));
1168 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1169 make_path(dir, u8));
1170 g_free(u8);
1171 g_free(dir);
1173 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1174 g_object_unref(image);
1176 gtk_widget_show(savebox);
1179 static gint save_to_file(GObject *savebox,
1180 const gchar *pathname, gpointer data)
1182 SaveCb callback;
1183 const gchar *current_path;
1185 callback = g_object_get_data(savebox, "action_callback");
1186 current_path = g_object_get_data(savebox, "current_path");
1188 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1189 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1191 return callback(savebox, current_path, pathname)
1192 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1195 static gboolean copy_cb(GObject *savebox,
1196 const gchar *current, const gchar *new)
1198 return action_with_leaf(action_copy, current, new);
1201 static gboolean action_with_leaf(ActionFn action,
1202 const gchar *current, const gchar *new)
1204 const char *leaf;
1205 char *new_dir;
1206 GList *local_paths;
1208 if (new[0] != '/')
1210 report_error(_("New pathname is not absolute"));
1211 return FALSE;
1214 if (new[strlen(new) - 1] == '/')
1216 new_dir = g_strdup(new);
1217 leaf = NULL;
1219 else
1221 const gchar *slash;
1223 slash = strrchr(new, '/');
1224 new_dir = g_strndup(new, slash - new);
1225 leaf = slash + 1;
1228 local_paths = g_list_append(NULL, (gchar *) current);
1229 action(local_paths, new_dir, leaf, -1);
1230 g_list_free(local_paths);
1232 g_free(new_dir);
1234 return TRUE;
1237 /* Open a savebox to act on the selected file.
1238 * Call 'callback' later to perform the operation.
1240 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1241 const gchar *action, SaveCb callback,
1242 GdkDragAction dnd_action)
1244 g_object_ref(image);
1245 savebox_show(action, path, image, callback, dnd_action);
1248 static gboolean rename_cb(GObject *savebox,
1249 const gchar *current, const gchar *new)
1251 return action_with_leaf(action_move, current, new);
1254 static gboolean link_cb(GObject *savebox,
1255 const gchar *initial, const gchar *path)
1257 GtkToggleButton *check_relative;
1258 struct stat info;
1259 int err;
1260 gchar *link_path;
1262 check_relative = g_object_get_data(savebox, "check_relative");
1264 last_symlink_check_relative = gtk_toggle_button_get_active(check_relative);
1266 if (last_symlink_check_relative)
1267 link_path = get_relative_path(path, initial);
1268 else
1269 link_path = g_strdup(initial);
1271 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1273 GtkWidget *box, *button;
1274 gint ans;
1276 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1277 GTK_BUTTONS_CANCEL,
1278 _("Symlink from '%s' already exists. "
1279 "Replace it with a link to '%s'?"),
1280 path, link_path);
1282 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1284 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1285 gtk_widget_show(button);
1286 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1287 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1288 button, GTK_RESPONSE_OK);
1289 gtk_dialog_set_default_response(GTK_DIALOG(box),
1290 GTK_RESPONSE_OK);
1292 ans = gtk_dialog_run(GTK_DIALOG(box));
1293 gtk_widget_destroy(box);
1295 if (ans != GTK_RESPONSE_OK)
1297 g_free(link_path);
1298 return FALSE;
1301 unlink(path);
1304 err = symlink(link_path, path);
1305 g_free(link_path);
1307 if (err)
1309 report_error("symlink: %s", g_strerror(errno));
1310 return FALSE;
1313 dir_check_this(path);
1315 return TRUE;
1318 static void run_action(DirItem *item)
1320 if (can_set_run_action(item))
1321 type_set_handler_dialog(item->mime_type);
1322 else
1323 report_error(
1324 _("You can only set the run action for a "
1325 "regular file"));
1328 void open_home(gpointer data, guint action, GtkWidget *widget)
1330 filer_opendir(home_dir, NULL, NULL);
1333 static void select_all(gpointer data, guint action, GtkWidget *widget)
1335 g_return_if_fail(window_with_focus != NULL);
1337 window_with_focus->temp_item_selected = FALSE;
1338 view_select_all(window_with_focus->view);
1341 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1343 g_return_if_fail(window_with_focus != NULL);
1345 window_with_focus->temp_item_selected = FALSE;
1346 view_clear_selection(window_with_focus->view);
1349 static gboolean invert_cb(ViewIter *iter, gpointer data)
1351 return !view_get_selected((ViewIface *) data, iter);
1354 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1356 g_return_if_fail(window_with_focus != NULL);
1358 window_with_focus->temp_item_selected = FALSE;
1360 view_select_if(window_with_focus->view, invert_cb,
1361 window_with_focus->view);
1364 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1366 GtkWidget *win;
1368 win = options_show();
1370 if (win)
1372 number_of_windows++;
1373 g_signal_connect(win, "destroy",
1374 G_CALLBACK(one_less_window), NULL);
1378 static gboolean new_directory_cb(GObject *savebox,
1379 const gchar *initial, const gchar *path)
1381 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1383 report_error("mkdir: %s", g_strerror(errno));
1384 return FALSE;
1387 dir_check_this(path);
1389 if (filer_exists(window_with_focus))
1391 guchar *leaf;
1392 leaf = strrchr(path, '/');
1393 if (leaf)
1394 display_set_autoselect(window_with_focus, leaf + 1);
1397 return TRUE;
1400 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1402 g_return_if_fail(window_with_focus != NULL);
1404 savebox_show(_("Create"),
1405 make_path(window_with_focus->sym_path, _("NewDir")),
1406 type_to_icon(inode_directory), new_directory_cb,
1407 GDK_ACTION_COPY);
1410 static gboolean new_file_cb(GObject *savebox,
1411 const gchar *initial, const gchar *path)
1413 int fd;
1415 fd = open(path, O_CREAT | O_EXCL, 0666);
1417 if (fd == -1)
1419 report_error(_("Error creating '%s': %s"),
1420 path, g_strerror(errno));
1421 return FALSE;
1424 if (close(fd))
1425 report_error(_("Error creating '%s': %s"),
1426 path, g_strerror(errno));
1428 dir_check_this(path);
1430 if (filer_exists(window_with_focus))
1432 guchar *leaf;
1433 leaf = strrchr(path, '/');
1434 if (leaf)
1435 display_set_autoselect(window_with_focus, leaf + 1);
1438 return TRUE;
1441 static void new_file(gpointer data, guint action, GtkWidget *widget)
1443 g_return_if_fail(window_with_focus != NULL);
1445 savebox_show(_("Create"),
1446 make_path(window_with_focus->sym_path, _("NewFile")),
1447 type_to_icon(text_plain),
1448 new_file_cb, GDK_ACTION_COPY);
1451 static gboolean new_file_type_cb(GObject *savebox,
1452 const gchar *initial, const gchar *path)
1454 const gchar *oleaf, *leaf;
1455 gchar *templ, *rtempl, *templ_dname, *dest;
1456 GList *paths;
1458 /* We can work out the template path from the initial name */
1459 oleaf = g_basename(initial);
1460 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1461 if (!templ_dname)
1463 report_error(
1464 _("Error creating file: could not find the template for %s"),
1465 oleaf);
1466 return FALSE;
1469 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1470 g_free(templ_dname);
1471 rtempl = pathdup(templ);
1472 g_free(templ);
1474 dest = g_path_get_dirname(path);
1475 leaf = g_basename(path);
1476 paths = g_list_append(NULL, rtempl);
1478 action_copy(paths, dest, leaf, TRUE);
1480 g_list_free(paths);
1481 g_free(dest);
1482 g_free(rtempl);
1484 if (filer_exists(window_with_focus))
1485 display_set_autoselect(window_with_focus, leaf);
1487 return TRUE;
1490 static void do_send_to(gchar *templ)
1492 g_return_if_fail(send_to_paths != NULL);
1494 run_with_files(templ, send_to_paths);
1497 static void new_file_type(gchar *templ)
1499 const gchar *leaf;
1500 MIME_type *type;
1502 g_return_if_fail(window_with_focus != NULL);
1504 leaf = g_basename(templ);
1505 type = type_get_type(templ);
1507 savebox_show(_("Create"),
1508 make_path(window_with_focus->sym_path, leaf),
1509 type_to_icon(type),
1510 new_file_type_cb, GDK_ACTION_COPY);
1513 static void customise_send_to(gpointer data)
1515 GPtrArray *path;
1516 guchar *save;
1517 GString *dirs;
1518 int i;
1520 dirs = g_string_new(NULL);
1522 path = choices_list_xdg_dirs("", SITE);
1523 for (i = 0; i < path->len; i++)
1525 guchar *old = (guchar *) path->pdata[i];
1527 g_string_append(dirs, old);
1528 g_string_append(dirs, "/SendTo\n");
1530 choices_free_list(path);
1532 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1533 if (save)
1534 mkdir(save, 0777);
1536 info_message(
1537 _("The `Send To' menu provides a quick way to send some files "
1538 "to an application. The applications listed are those in "
1539 "the following directories:\n\n%s\n%s\n"
1540 "The `Send To' menu may be opened by Shift+Menu clicking "
1541 "over a file.\n\n"
1542 "Advanced use:\n"
1543 "You can also create subdirectories called "
1544 "`.text_html', `.text', etc which will only be "
1545 "shown for files of that type. `.group' is shown "
1546 "only when multiple files are selected. `.regular' "
1547 "is shown only for regular files."),
1548 dirs->str,
1549 save ? _("I'll show you your SendTo directory now; you should "
1550 "symlink (Ctrl+Shift drag) any applications you want "
1551 "into it.")
1552 : _("Your CHOICESPATH variable setting prevents "
1553 "customisations - sorry."));
1555 g_string_free(dirs, TRUE);
1557 if (save)
1558 filer_opendir(save, NULL, NULL);
1561 static void customise_new(gpointer data)
1563 GPtrArray *path;
1564 guchar *save;
1565 GString *dirs;
1566 int i;
1568 dirs = g_string_new(NULL);
1570 path = choices_list_xdg_dirs("", SITE);
1571 for (i = 0; i < path->len; i++)
1573 guchar *old = (guchar *) path->pdata[i];
1575 g_string_append(dirs, old);
1576 g_string_append(dirs, "/Templates\n");
1578 choices_free_list(path);
1580 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1581 if (save)
1582 mkdir(save, 0777);
1584 info_message(
1585 _("Any files placed in your Templates directories will "
1586 "appear on the `New' menu. Choosing one of them will make "
1587 "a copy of it as the new file.\n\n"
1588 "The following directories contain templates:\n\n%s\n%s\n"),
1589 dirs->str,
1590 save ? _("I'll show you your Templates directory now; you "
1591 "should place any template files you want inside it.")
1592 : _("Your CHOICESPATH variable setting prevents "
1593 "customisations - sorry."));
1595 g_string_free(dirs, TRUE);
1597 if (save)
1598 filer_opendir(save, NULL, NULL);
1601 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1602 * to the menu.
1604 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1606 gchar *searchdir;
1607 GPtrArray *paths;
1608 int i;
1610 if (subtype)
1611 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1612 else if (type)
1613 searchdir = g_strdup_printf("SendTo/.%s", type);
1614 else
1615 searchdir = g_strdup("SendTo");
1617 paths = choices_list_xdg_dirs(searchdir, SITE);
1618 g_free(searchdir);
1620 for (i = 0; i < paths->len; i++)
1622 GList *widgets = NULL;
1623 guchar *dir = (guchar *) paths->pdata[i];
1625 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1626 (CallbackFn) do_send_to,
1627 FALSE, FALSE, TRUE);
1629 if (widgets)
1630 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1631 gtk_menu_item_new());
1633 g_list_free(widgets); /* TODO: Get rid of this */
1636 choices_free_list(paths);
1639 /* Scan the SendTo dir and create and show the Send To menu.
1640 * The 'paths' list and every path in it is claimed, and will be
1641 * freed later -- don't free it yourself!
1643 static void show_send_to_menu(GList *paths, GdkEvent *event)
1645 GtkWidget *menu, *item;
1647 menu = gtk_menu_new();
1649 if (g_list_length(paths) == 1)
1651 DirItem *item;
1653 item = diritem_new("");
1654 diritem_restat(paths->data, item, NULL);
1656 add_sendto(menu,
1657 item->mime_type->media_type,
1658 item->mime_type->subtype);
1660 add_sendto(menu, item->mime_type->media_type, NULL);
1662 if (item->base_type == TYPE_FILE)
1663 add_sendto(menu, "regular", NULL);
1665 diritem_free(item);
1667 else
1669 GList *rover;
1670 gboolean same=TRUE, same_media=TRUE;
1671 MIME_type *type=NULL;
1672 DirItem *item;
1674 item = diritem_new("");
1675 for(rover=paths; rover; rover=g_list_next(rover))
1677 diritem_restat(rover->data, item, NULL);
1678 if(!type)
1679 type=item->mime_type;
1680 else
1682 if(type!=item->mime_type)
1684 same=FALSE;
1685 if(strcmp(type->media_type,
1686 item->mime_type->media_type)!=0)
1688 same_media=FALSE;
1689 break;
1694 diritem_free(item);
1696 if(type)
1698 if(same)
1699 add_sendto(menu, type->media_type,
1700 type->subtype);
1701 if(same_media)
1702 add_sendto(menu, type->media_type, NULL);
1705 add_sendto(menu, "group", NULL);
1708 add_sendto(menu, NULL, NULL);
1710 item = gtk_menu_item_new_with_label(_("Customise"));
1711 g_signal_connect_swapped(item, "activate",
1712 G_CALLBACK(customise_send_to), NULL);
1713 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1715 if (send_to_paths)
1716 destroy_glist(&send_to_paths);
1718 send_to_paths = paths;
1720 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1722 popup_menu = menu;
1723 show_popup_menu(menu, event, 0);
1726 static void send_to(FilerWindow *filer_window)
1728 GList *paths;
1729 GdkEvent *event;
1731 paths = filer_selected_items(filer_window);
1732 event = gtk_get_current_event();
1734 /* Eats paths */
1735 show_send_to_menu(paths, event);
1737 if (event)
1738 gdk_event_free(event);
1741 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1743 const char *argv[] = {"sh", "-c", NULL, NULL};
1744 gboolean close = action;
1746 argv[2] = o_menu_xterm.value;
1748 g_return_if_fail(window_with_focus != NULL);
1750 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1751 gtk_widget_destroy(window_with_focus->window);
1754 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1756 g_return_if_fail(window_with_focus != NULL);
1758 filer_change_to(window_with_focus, home_dir, NULL);
1761 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1763 g_return_if_fail(window_with_focus != NULL);
1765 bookmarks_show_menu(window_with_focus);
1768 static void show_log(gpointer data, guint action, GtkWidget *widget)
1770 g_return_if_fail(window_with_focus != NULL);
1772 log_show_window();
1775 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1777 g_return_if_fail(window_with_focus != NULL);
1779 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1780 filer_change_to(window_with_focus,
1781 window_with_focus->real_path, NULL);
1782 else
1783 delayed_error(_("This is already the canonical name "
1784 "for this directory."));
1787 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1789 g_return_if_fail(window_with_focus != NULL);
1791 filer_open_parent(window_with_focus);
1794 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1796 g_return_if_fail(window_with_focus != NULL);
1798 change_to_parent(window_with_focus);
1801 static void resize(gpointer data, guint action, GtkWidget *widget)
1803 g_return_if_fail(window_with_focus != NULL);
1805 view_autosize(window_with_focus->view);
1808 static void new_window(gpointer data, guint action, GtkWidget *widget)
1810 g_return_if_fail(window_with_focus != NULL);
1812 if (o_unique_filer_windows.int_value)
1814 report_error(_("You can't open a second view onto "
1815 "this directory because the `Unique Windows' option "
1816 "is turned on in the Options window."));
1818 else
1819 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1822 static void close_window(gpointer data, guint action, GtkWidget *widget)
1824 g_return_if_fail(window_with_focus != NULL);
1826 if (!filer_window_delete(window_with_focus->window, NULL,
1827 window_with_focus))
1828 gtk_widget_destroy(window_with_focus->window);
1831 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1833 MiniType type = (MiniType) action;
1835 g_return_if_fail(window_with_focus != NULL);
1837 /* Item needs to remain selected... */
1838 if (type == MINI_SHELL)
1839 window_with_focus->temp_item_selected = FALSE;
1841 minibuffer_show(window_with_focus, type);
1844 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1846 if (action == HELP_ABOUT)
1847 infobox_new(app_dir);
1848 else if (action == HELP_DIR)
1849 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1850 else if (action == HELP_MANUAL)
1852 gchar *manual = NULL;
1854 if (current_lang)
1856 manual = g_strconcat(app_dir, "/Help/Manual-",
1857 current_lang, ".html", NULL);
1858 if (!file_exists(manual) && strchr(current_lang, '_'))
1860 /* Try again without the territory */
1861 strcpy(strrchr(manual, '_'), ".html");
1863 if (!file_exists(manual))
1864 null_g_free(&manual);
1867 if (!manual)
1868 manual = g_strconcat(app_dir,
1869 "/Help/Manual.html", NULL);
1871 run_by_path(manual);
1873 g_free(manual);
1875 else
1876 g_warning("Unknown help action %d\n", action);
1879 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1880 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1882 GList *items, *item;
1884 items = gtk_container_get_children(GTK_CONTAINER(menu));
1886 item = g_list_nth(items, from);
1887 while (item && n--)
1889 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1890 item = item->next;
1892 g_list_free(items);
1895 static void save_menus(void)
1897 char *menurc;
1899 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1900 if (menurc)
1902 gtk_accel_map_save(menurc);
1903 g_free(menurc);
1907 static void select_nth_item(GtkMenuShell *shell, int n)
1909 GList *items;
1910 GtkWidget *item;
1912 items = gtk_container_get_children(GTK_CONTAINER(shell));
1913 item = g_list_nth_data(items, n);
1915 g_return_if_fail(item != NULL);
1917 g_list_free(items);
1919 gtk_menu_shell_select_item(shell, item);
1922 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1924 DirItem *item;
1925 const guchar *path;
1926 int n_selected;
1927 ViewIter iter;
1929 g_return_if_fail(window_with_focus != NULL);
1931 n_selected = view_count_selected(window_with_focus->view);
1933 if (n_selected < 1)
1935 const char *prompt;
1937 switch (action)
1939 case FILE_COPY_ITEM:
1940 prompt = _("Copy ... ?");
1941 break;
1942 case FILE_RENAME_ITEM:
1943 prompt = _("Rename ... ?");
1944 break;
1945 case FILE_LINK_ITEM:
1946 prompt = _("Symlink ... ?");
1947 break;
1948 case FILE_OPEN_FILE:
1949 prompt = _("Shift Open ... ?");
1950 break;
1951 case FILE_PROPERTIES:
1952 prompt = _("Properties of ... ?");
1953 break;
1954 case FILE_SET_TYPE:
1955 prompt = _("Set type of ... ?");
1956 break;
1957 case FILE_RUN_ACTION:
1958 prompt = _("Set run action for ... ?");
1959 break;
1960 case FILE_SET_ICON:
1961 prompt = _("Set icon for ... ?");
1962 break;
1963 case FILE_SEND_TO:
1964 prompt = _("Send ... to ... ?");
1965 break;
1966 case FILE_DELETE:
1967 prompt = _("DELETE ... ?");
1968 break;
1969 case FILE_USAGE:
1970 prompt = _("Count the size of ... ?");
1971 break;
1972 case FILE_CHMOD_ITEMS:
1973 prompt = _("Set permissions on ... ?");
1974 break;
1975 case FILE_FIND:
1976 prompt = _("Search inside ... ?");
1977 break;
1978 default:
1979 g_warning("Unknown action!");
1980 return;
1982 filer_target_mode(window_with_focus, target_callback,
1983 GINT_TO_POINTER(action), prompt);
1984 return;
1987 switch (action)
1989 case FILE_SEND_TO:
1990 send_to(window_with_focus);
1991 return;
1992 case FILE_DELETE:
1993 delete(window_with_focus);
1994 return;
1995 case FILE_USAGE:
1996 usage(window_with_focus);
1997 return;
1998 case FILE_CHMOD_ITEMS:
1999 chmod_items(window_with_focus);
2000 return;
2001 case FILE_SET_TYPE:
2002 set_type_items(window_with_focus);
2003 return;
2004 case FILE_FIND:
2005 find(window_with_focus);
2006 return;
2007 case FILE_PROPERTIES:
2009 GList *items;
2011 items = filer_selected_items(window_with_focus);
2012 infobox_show_list(items);
2013 destroy_glist(&items);
2014 return;
2016 case FILE_RENAME_ITEM:
2017 if (n_selected > 1)
2019 GList *items = NULL;
2020 ViewIter iter;
2022 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
2023 while ((item = iter.next(&iter)))
2024 items = g_list_prepend(items, item->leafname);
2025 items = g_list_reverse(items);
2027 bulk_rename(window_with_focus->sym_path, items);
2028 g_list_free(items);
2029 return;
2031 break; /* Not a bulk rename... see below */
2032 default:
2033 break;
2036 /* All the following actions require exactly one file selected */
2038 if (n_selected > 1)
2040 report_error(_("You cannot do this to more than "
2041 "one item at a time"));
2042 return;
2045 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
2047 item = iter.next(&iter);
2048 g_return_if_fail(item != NULL);
2049 /* iter may be passed to filer_openitem... */
2051 if (item->base_type == TYPE_UNKNOWN)
2052 item = dir_update_item(window_with_focus->directory,
2053 item->leafname);
2055 if (!item)
2057 report_error(_("Item no longer exists!"));
2058 return;
2061 path = make_path(window_with_focus->sym_path, item->leafname);
2063 switch (action)
2065 case FILE_COPY_ITEM:
2066 src_dest_action_item(path, di_image(item),
2067 _("Copy"), copy_cb,
2068 GDK_ACTION_COPY);
2069 break;
2070 case FILE_RENAME_ITEM:
2071 src_dest_action_item(path, di_image(item),
2072 _("Rename"), rename_cb,
2073 GDK_ACTION_MOVE);
2074 break;
2075 case FILE_LINK_ITEM:
2076 src_dest_action_item(path, di_image(item),
2077 _("Symlink"), link_cb,
2078 GDK_ACTION_LINK);
2079 break;
2080 case FILE_OPEN_FILE:
2081 filer_openitem(window_with_focus, &iter,
2082 OPEN_SAME_WINDOW | OPEN_SHIFT);
2083 break;
2084 case FILE_RUN_ACTION:
2085 run_action(item);
2086 break;
2087 case FILE_SET_ICON:
2088 icon_set_handler_dialog(item, path);
2089 break;
2090 default:
2091 g_warning("Unknown action!");
2092 return;
2096 static void show_key_help(GtkWidget *button, gpointer data)
2098 gboolean can_change_accels;
2100 g_object_get(G_OBJECT(gtk_settings_get_default()),
2101 "gtk-can-change-accels", &can_change_accels,
2102 NULL);
2104 if (!can_change_accels)
2106 info_message(_("User-definable shortcuts are disabled by "
2107 "default in Gtk2, and you have not enabled "
2108 "them. You can turn this feature on by:\n\n"
2109 "1) using an XSettings manager, such as ROX-Session "
2110 "or gnome-settings-daemon, or\n\n"
2111 "2) adding this line to ~/.gtkrc-2.0:\n"
2112 "\tgtk-can-change-accels = 1\n"
2113 "\t(this only works if NOT using XSETTINGS)"));
2114 return;
2117 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2118 "- Open the menu over a filer window,\n"
2119 "- Move the pointer over the item you want to use,\n"
2120 "- Press the key you want attached to it.\n\n"
2121 "The key will appear next to the menu item and you can just press "
2122 "that key without opening the menu in future."));
2125 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2127 GtkWidget *button, *align;
2129 g_return_val_if_fail(option == NULL, NULL);
2131 align = gtk_alignment_new(0, 0.5, 0, 0);
2132 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2133 gtk_container_add(GTK_CONTAINER(align), button);
2134 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2136 return g_list_append(NULL, align);