r4713: Menu option to control whether directories are filtered by name along
[rox-filer.git] / ROX-Filer / src / menu.c
blob2d9fb22a83c4df4d2074daca123db132bdc8bb69
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* menu.c - code for handling the popup menus */
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/wait.h>
27 #include <sys/param.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <dirent.h>
33 #include <gtk/gtk.h>
35 #include "global.h"
37 #include "menu.h"
38 #include "run.h"
39 #include "action.h"
40 #include "filer.h"
41 #include "pixmaps.h"
42 #include "type.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "options.h"
46 #include "choices.h"
47 #include "gtksavebox.h"
48 #include "mount.h"
49 #include "minibuffer.h"
50 #include "i18n.h"
51 #include "main.h"
52 #include "pinboard.h"
53 #include "dir.h"
54 #include "diritem.h"
55 #include "appmenu.h"
56 #include "usericons.h"
57 #include "infobox.h"
58 #include "view_iface.h"
59 #include "display.h"
60 #include "bookmarks.h"
61 #include "panel.h"
62 #include "bulk_rename.h"
63 #include "xtypes.h"
65 typedef enum {
66 FILE_COPY_ITEM,
67 FILE_RENAME_ITEM,
68 FILE_LINK_ITEM,
69 FILE_OPEN_FILE,
70 FILE_PROPERTIES,
71 FILE_RUN_ACTION,
72 FILE_SET_ICON,
73 FILE_SEND_TO,
74 FILE_DELETE,
75 FILE_USAGE,
76 FILE_CHMOD_ITEMS,
77 FILE_FIND,
78 FILE_SET_TYPE,
79 } FileOp;
81 typedef void (*ActionFn)(GList *paths,
82 const char *dest_dir, const char *leaf, int quiet);
83 typedef void MenuCallback(GtkWidget *widget, gpointer data);
85 typedef gboolean (*SaveCb)(GObject *savebox,
86 const gchar *current, const gchar *new);
88 GtkAccelGroup *filer_keys = NULL;
89 static gboolean filer_keys_need_init = TRUE;
91 static GtkWidget *popup_menu = NULL; /* Currently open menu */
93 static gint updating_menu = 0; /* Non-zero => ignore activations */
94 static GList *send_to_paths = NULL;
96 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
98 /* Static prototypes */
100 static void save_menus(void);
101 static void menu_closed(GtkWidget *widget);
102 static void shade_file_menu_items(gboolean shaded);
103 static void savebox_show(const gchar *action, const gchar *path,
104 MaskedPixmap *image, SaveCb callback,
105 GdkDragAction dnd_action);
106 static gint save_to_file(GObject *savebox,
107 const gchar *pathname, gpointer data);
108 static gboolean action_with_leaf(ActionFn action,
109 const gchar *current, const gchar *new);
110 static gboolean link_cb(GObject *savebox,
111 const gchar *initial, const gchar *path);
112 static void select_nth_item(GtkMenuShell *shell, int n);
113 static void new_file_type(gchar *templ);
114 static void do_send_to(gchar *templ);
115 static void show_send_to_menu(GList *paths, GdkEvent *event);
116 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
118 /* Note that for most of these callbacks none of the arguments are used. */
120 static void view_type(gpointer data, guint action, GtkWidget *widget);
122 /* (action used in these three - DetailsType) */
123 static void change_size(gpointer data, guint action, GtkWidget *widget);
124 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
125 static void set_with(gpointer data, guint action, GtkWidget *widget);
127 static void set_sort(gpointer data, guint action, GtkWidget *widget);
128 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
130 static void filter_directories(gpointer data, guint action, GtkWidget *widget);
131 static void hidden(gpointer data, guint action, GtkWidget *widget);
132 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
133 static void refresh(gpointer data, guint action, GtkWidget *widget);
134 static void save_settings(gpointer data, guint action, GtkWidget *widget);
136 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
138 static void select_all(gpointer data, guint action, GtkWidget *widget);
139 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
140 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
141 static void new_directory(gpointer data, guint action, GtkWidget *widget);
142 static void new_file(gpointer data, guint action, GtkWidget *widget);
143 static void customise_new(gpointer data);
144 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
146 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent(gpointer data, guint action, GtkWidget *widget);
148 static void home_directory(gpointer data, guint action, GtkWidget *widget);
149 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
150 static void new_window(gpointer data, guint action, GtkWidget *widget);
151 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
152 static void close_window(gpointer data, guint action, GtkWidget *widget);
153 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
155 /* (action used in this - MiniType) */
156 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
157 static void resize(gpointer data, guint action, GtkWidget *widget);
159 #define MENUS_NAME "menus2"
161 static GtkWidget *filer_menu; /* The popup filer menu */
162 static GtkWidget *filer_file_item; /* The File '' label */
163 static GtkWidget *filer_file_menu; /* The File '' menu */
164 static GtkWidget *file_shift_item; /* Shift Open label */
165 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
166 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
167 static GtkWidget *filer_reverse_menu; /* The Reversed item */
168 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
169 static GtkWidget *filer_new_window; /* The New Window item */
170 static GtkWidget *filer_new_menu; /* The New submenu */
171 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
172 static GtkWidget *filer_set_type; /* Set type item */
174 #undef N_
175 #define N_(x) x
177 static GtkItemFactoryEntry filer_menu_def[] = {
178 {N_("Display"), NULL, NULL, 0, "<Branch>"},
179 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
180 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
181 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
182 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
183 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
184 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
185 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
186 {">", NULL, NULL, 0, "<Separator>"},
187 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
188 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
189 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
190 {">", NULL, NULL, 0, "<Separator>"},
191 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
192 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
193 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
194 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
195 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
196 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
197 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
198 {">", NULL, NULL, 0, "<Separator>"},
199 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
200 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
201 {">" N_("Filter Directories With Files"), NULL, filter_directories, 0, "<ToggleItem>"},
202 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
203 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
204 {">" N_("Save Current Display Settings..."), NULL, save_settings, 0, NULL},
205 {N_("File"), NULL, NULL, 0, "<Branch>"},
206 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
207 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
208 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
209 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
210 {">", NULL, NULL, 0, "<Separator>"},
211 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
212 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
213 {">", NULL, NULL, 0, "<Separator>"},
214 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
215 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
216 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
217 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
218 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
219 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
220 {">", NULL, NULL, 0, "<Separator>"},
221 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
222 {N_("Select"), NULL, NULL, 0, "<Branch>"},
223 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
224 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
225 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
226 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
227 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
228 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
229 {N_("New"), NULL, NULL, 0, "<Branch>"},
230 {">" N_("Directory"), NULL, new_directory, 0, NULL},
231 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
232 {">" N_("Customise Menu..."), NULL, customise_new, 0, NULL},
233 {N_("Window"), NULL, NULL, 0, "<Branch>"},
234 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
235 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
236 {">" N_("New Window"), NULL, new_window, 0, NULL},
237 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
238 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
239 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
240 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
241 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
243 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
244 {">", NULL, NULL, 0, "<Separator>"},
245 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
246 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
247 {">" N_("Terminal Here"), "grave", xterm_here, FALSE, NULL},
248 {">" N_("Switch to Terminal"), NULL, xterm_here, TRUE, NULL},
249 {N_("Help"), NULL, NULL, 0, "<Branch>"},
250 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
251 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
252 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
256 #define GET_MENU_ITEM(var, menu) \
257 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
259 #define GET_SMENU_ITEM(var, menu, sub) \
260 do { \
261 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
262 var = gtk_item_factory_get_widget(item_factory, tmp); \
263 g_free(tmp); \
264 } while (0)
266 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
267 do { \
268 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
269 var = gtk_item_factory_get_widget(item_factory, tmp); \
270 g_free(tmp); \
271 } while (0)
273 /* Returns TRUE if the keys were installed (first call only) */
274 gboolean ensure_filer_menu(void)
276 GList *items;
277 guchar *tmp;
278 GtkWidget *item;
279 GtkItemFactory *item_factory;
281 if (!filer_keys_need_init)
282 return FALSE;
283 filer_keys_need_init = FALSE;
285 item_factory = menu_create(filer_menu_def,
286 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
287 "<filer>", filer_keys);
289 GET_MENU_ITEM(filer_menu, "filer");
290 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
291 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
292 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
293 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
294 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
295 "Show Thumbnails");
296 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
297 filer_set_type = GTK_BIN(item)->child;
299 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
300 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
301 filer_follow_sym = GTK_BIN(item)->child;
303 /* File '' label... */
304 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
305 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
306 g_list_free(items);
308 /* Shift Open... label */
309 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
310 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
311 g_list_free(items);
313 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
314 filer_new_window = GTK_BIN(item)->child;
316 g_signal_connect(filer_menu, "unmap_event",
317 G_CALLBACK(menu_closed), NULL);
318 g_signal_connect(filer_file_menu, "unmap_event",
319 G_CALLBACK(menu_closed), NULL);
321 g_signal_connect(filer_keys, "accel_changed",
322 G_CALLBACK(save_menus), NULL);
324 return TRUE;
327 void menu_init(void)
329 char *menurc;
331 menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
332 if (menurc)
334 gtk_accel_map_load(menurc);
335 g_free(menurc);
338 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
339 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
340 option_add_int(&o_menu_quick, "menu_quick", FALSE);
341 option_add_saver(save_menus);
343 option_register_widget("menu-set-keys", set_keys_button);
345 filer_keys = gtk_accel_group_new();
348 /* Name is in the form "<panel>" */
349 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
350 const gchar *name, GtkAccelGroup *keys)
352 GtkItemFactory *item_factory;
353 GtkItemFactoryEntry *translated;
355 if (!keys)
357 keys = gtk_accel_group_new();
358 gtk_accel_group_lock(keys);
361 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
363 translated = translate_entries(def, n_entries);
364 gtk_item_factory_create_items(item_factory, n_entries,
365 translated, NULL);
366 free_translated_entries(translated, n_entries);
368 return item_factory;
371 /* Prevent the user from setting a short-cut on this item */
372 static void menuitem_no_shortcuts(GtkWidget *item)
374 /* XXX */
375 #if 0
376 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
378 _gtk_widget_set_accel_path(item, NULL, NULL);
379 null_g_free(&menuitem->accel_path);
380 #endif
383 /* Shade items that only work on single files */
384 static void shade_file_menu_items(gboolean shaded)
386 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
387 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
388 menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
389 menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
392 /* 'data' is an array of three ints:
393 * [ pointer_x, pointer_y, item_under_pointer ]
395 void position_menu(GtkMenu *menu, gint *x, gint *y,
396 gboolean *push_in, gpointer data)
398 int *pos = (int *) data;
399 GtkRequisition requisition;
400 GList *items, *next;
401 int y_shift = 0;
402 int item = pos[2];
404 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
406 while (item >= 0 && next)
408 int h = ((GtkWidget *) next->data)->requisition.height;
410 if (item > 0)
411 y_shift += h;
412 else
413 y_shift += h / 2;
415 next = next->next;
416 item--;
419 g_list_free(items);
421 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
423 *x = pos[0] - (requisition.width * 7 / 8);
424 *y = pos[1] - y_shift;
426 *x = CLAMP(*x, 0, screen_width - requisition.width);
427 *y = CLAMP(*y, 0, screen_height - requisition.height);
429 *push_in = FALSE;
432 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
433 MenuIconStyle style)
435 GtkWidget *item;
437 if (style != MIS_NONE && di_image(ditem))
439 GdkPixbuf *pixbuf;
440 MaskedPixmap *image;
442 image = di_image(ditem);
444 switch (style)
446 case MIS_LARGE:
447 pixbuf = image->pixbuf;
448 break;
449 default:
450 if (!image->sm_pixbuf)
451 pixmap_make_small(image);
452 pixbuf = image->sm_pixbuf;
453 break;
456 item = gtk_image_menu_item_new_with_label(label);
457 /* TODO: Find a way to allow short-cuts */
458 menuitem_no_shortcuts(item);
460 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
461 gtk_image_new_from_pixbuf(pixbuf));
462 gtk_widget_show_all(item);
464 else
465 item = gtk_menu_item_new_with_label(label);
467 return item;
470 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
471 MenuIconStyle style, CallbackFn func,
472 gboolean separator, gboolean strip_ext,
473 gboolean recurse)
475 GList *widgets = NULL;
476 DirItem *ditem;
477 int i;
478 GtkWidget *item;
479 char *dname = NULL;
480 GPtrArray *names;
482 dname = pathdup(dir_name);
484 names = list_dir(dname);
485 if (!names)
486 goto out;
488 for (i = 0; i < names->len; i++)
490 char *leaf = names->pdata[i];
491 gchar *fname;
493 if (separator)
495 item = gtk_menu_item_new();
496 widgets = g_list_append(widgets, item);
497 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
498 separator = FALSE;
501 fname = g_strconcat(dname, "/", leaf, NULL);
503 /* Strip off extension, if any */
504 if (strip_ext)
506 char *dot;
507 dot = strchr(leaf, '.');
508 if (dot)
509 *dot = '\0';
512 ditem = diritem_new("");
513 diritem_restat(fname, ditem, NULL);
515 item = make_send_to_item(ditem, leaf, style);
517 g_free(leaf);
519 /* If it is a directory (but NOT an AppDir) and we are
520 * recursing then set up a sub menu.
522 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
523 !(ditem->flags & ITEM_FLAG_APPDIR))
525 GtkWidget *sub;
526 GList *new_widgets;
528 sub = gtk_menu_new();
529 new_widgets = menu_from_dir(sub, fname, style, func,
530 separator, strip_ext, TRUE);
531 g_list_free(new_widgets);
532 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
534 else
535 g_signal_connect_swapped(item, "activate",
536 G_CALLBACK(func), fname);
538 diritem_free(ditem);
540 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
541 g_signal_connect_swapped(item, "destroy",
542 G_CALLBACK(g_free), fname);
544 widgets = g_list_append(widgets, item);
547 g_ptr_array_free(names, TRUE);
548 out:
549 g_free(dname);
551 return widgets;
554 /* Scan the templates dir and create entries for the New menu */
555 static void update_new_files_menu(MenuIconStyle style)
557 static GList *widgets = NULL;
559 gchar *templ_dname = NULL;
561 if (widgets)
563 GList *next;
565 for (next = widgets; next; next = next->next)
566 gtk_widget_destroy((GtkWidget *) next->data);
568 g_list_free(widgets);
569 widgets = NULL;
572 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
573 if (templ_dname)
575 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
576 (CallbackFn) new_file_type, TRUE, TRUE,
577 FALSE);
578 g_free(templ_dname);
580 gtk_widget_show_all(filer_new_menu);
583 /* 'item' is the number of the item to appear under the pointer. */
584 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
586 int pos[3];
587 int button = 0;
588 guint32 time = 0;
590 if (event && (event->type == GDK_BUTTON_PRESS ||
591 event->type == GDK_BUTTON_RELEASE))
593 GdkEventButton *bev = (GdkEventButton *) event;
595 pos[0] = bev->x_root;
596 pos[1] = bev->y_root;
597 button = bev->button;
598 time = bev->time;
600 else if (event && event->type == GDK_KEY_PRESS)
602 GdkEventKey *kev = (GdkEventKey *) event;
604 get_pointer_xy(pos, pos + 1);
605 time = kev->time;
607 else
608 get_pointer_xy(pos, pos + 1);
610 pos[2] = item;
612 gtk_widget_show_all(menu);
613 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
614 position_menu, (gpointer) pos, button, time);
615 select_nth_item(GTK_MENU_SHELL(menu), item);
618 /* Hide the popup menu, if any */
619 void menu_popdown(void)
621 if (popup_menu)
622 gtk_menu_popdown(GTK_MENU(popup_menu));
625 static MenuIconStyle get_menu_icon_style(void)
627 MenuIconStyle mis;
628 int display;
630 mis = o_menu_iconsize.int_value;
632 switch (mis)
634 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
635 return mis;
636 default:
637 break;
640 if (mis == MIS_CURRENT && window_with_focus)
642 switch (window_with_focus->display_style)
644 case HUGE_ICONS:
645 case LARGE_ICONS:
646 return MIS_LARGE;
647 case SMALL_ICONS:
648 return MIS_SMALL;
649 default:
650 break;
654 display = o_display_size.int_value;
655 switch (display)
657 case HUGE_ICONS:
658 case LARGE_ICONS:
659 return MIS_LARGE;
660 case SMALL_ICONS:
661 return MIS_SMALL;
662 default:
663 break;
666 return MIS_SMALL;
669 /* iter->peek() is the clicked item, or NULL if none */
670 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
672 DirItem *file_item = NULL;
673 GdkModifierType state = 0;
674 int n_selected;
675 int n_added = 0;
677 g_return_if_fail(event != NULL);
679 n_selected = view_count_selected(filer_window->view);
681 ensure_filer_menu();
683 updating_menu++;
685 /* Remove previous AppMenu, if any */
686 appmenu_remove();
688 window_with_focus = filer_window;
690 if (event->type == GDK_BUTTON_PRESS)
691 state = ((GdkEventButton *) event)->state;
692 else if (event->type == GDK_KEY_PRESS)
693 state = ((GdkEventKey *) event)->state;
695 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
697 filer_window->temp_item_selected = TRUE;
698 view_set_selected(filer_window->view, iter, TRUE);
699 n_selected = view_count_selected(filer_window->view);
701 else
703 filer_window->temp_item_selected = FALSE;
706 /* Short-cut to the Send To menu */
707 if (state & GDK_SHIFT_MASK)
709 GList *paths;
711 updating_menu--;
713 if (n_selected == 0)
715 report_error(
716 _("You should Shift+Menu click over a file to "
717 "send it somewhere"));
718 return;
721 paths = filer_selected_items(filer_window);
723 show_send_to_menu(paths, event); /* (paths eaten) */
725 return;
729 GtkWidget *file_label, *file_menu;
730 GString *buffer;
731 DirItem *item;
733 file_label = filer_file_item;
734 file_menu = filer_file_menu;
735 gtk_check_menu_item_set_active(
736 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
737 filer_window->show_thumbs);
738 gtk_check_menu_item_set_active(
739 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
740 filer_window->show_hidden);
741 gtk_check_menu_item_set_active(
742 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
743 filer_window->sort_order != GTK_SORT_ASCENDING);
744 gtk_check_menu_item_set_active(
745 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
746 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
747 buffer = g_string_new(NULL);
749 switch (n_selected)
751 case 0:
752 g_string_assign(buffer, _("Next Click"));
753 shade_file_menu_items(FALSE);
754 break;
755 case 1:
756 item = filer_selected_item(filer_window);
757 if (item->base_type == TYPE_UNKNOWN)
758 dir_update_item(filer_window->directory,
759 item->leafname);
760 shade_file_menu_items(FALSE);
761 file_item = filer_selected_item(filer_window);
762 g_string_printf(buffer, _("%s '%s'"),
763 basetype_name(file_item),
764 g_utf8_validate(file_item->leafname,
765 -1, NULL)
766 ? file_item->leafname
767 : _("(bad utf-8)"));
768 if (!can_set_run_action(file_item))
769 menu_set_items_shaded(filer_file_menu,
770 TRUE, 8, 1);
771 break;
772 default:
773 shade_file_menu_items(TRUE);
774 g_string_printf(buffer, _("%d items"),
775 n_selected);
776 break;
778 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
779 g_string_free(buffer, TRUE);
781 menu_show_shift_action(file_shift_item, file_item,
782 n_selected == 0);
783 if (file_item)
784 n_added = appmenu_add(make_path(filer_window->sym_path,
785 file_item->leafname),
786 file_item, filer_file_menu);
789 update_new_files_menu(get_menu_icon_style());
791 gtk_widget_set_sensitive(filer_new_window,
792 !o_unique_filer_windows.int_value);
793 gtk_widget_set_sensitive(filer_follow_sym,
794 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
795 gtk_widget_set_sensitive(filer_set_type,
796 xattr_supported(filer_window->real_path));
798 if (n_selected && o_menu_quick.int_value)
799 popup_menu = (state & GDK_CONTROL_MASK)
800 ? filer_menu
801 : filer_file_menu;
802 else
803 popup_menu = (state & GDK_CONTROL_MASK)
804 ? filer_file_menu
805 : filer_menu;
807 updating_menu--;
809 show_popup_menu(popup_menu, event,
810 popup_menu == filer_file_menu ? n_added : 1);
813 static void menu_closed(GtkWidget *widget)
815 if (window_with_focus == NULL || widget != popup_menu)
816 return; /* Close panel item chosen? */
818 popup_menu = NULL;
820 if (window_with_focus->temp_item_selected)
822 view_clear_selection(window_with_focus->view);
823 window_with_focus->temp_item_selected = FALSE;
826 appmenu_remove();
829 static void target_callback(FilerWindow *filer_window,
830 ViewIter *iter,
831 gpointer action)
833 g_return_if_fail(filer_window != NULL);
835 window_with_focus = filer_window;
837 /* Don't grab the primary selection */
838 filer_window->temp_item_selected = TRUE;
840 view_wink_item(filer_window->view, iter);
841 view_select_only(filer_window->view, iter);
842 file_op(NULL, GPOINTER_TO_INT(action), NULL);
844 view_clear_selection(filer_window->view);
845 filer_window->temp_item_selected = FALSE;
848 /* Set the text of the 'Shift Open...' menu item.
849 * If icon is NULL, reset the text and also shade it, unless 'next'.
851 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
853 guchar *shift_action = NULL;
855 if (item)
857 if (item->flags & ITEM_FLAG_MOUNT_POINT)
859 if (item->flags & ITEM_FLAG_MOUNTED)
860 shift_action = N_("Unmount");
861 else
862 shift_action = N_("Open unmounted");
864 else if (item->flags & ITEM_FLAG_SYMLINK)
865 shift_action = N_("Show Target");
866 else if (item->base_type == TYPE_DIRECTORY)
867 shift_action = N_("Look Inside");
868 else if (item->base_type == TYPE_FILE)
869 shift_action = N_("Open As Text");
871 gtk_label_set_text(GTK_LABEL(menu_item),
872 shift_action ? _(shift_action)
873 : _("Shift Open"));
874 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
877 /* Actions */
879 static void view_type(gpointer data, guint action, GtkWidget *widget)
881 ViewType view_type = (ViewType) action;
883 g_return_if_fail(window_with_focus != NULL);
885 if (view_type == VIEW_TYPE_COLLECTION)
886 display_set_layout(window_with_focus,
887 window_with_focus->display_style_wanted,
888 DETAILS_NONE, FALSE);
890 filer_set_view_type(window_with_focus, (ViewType) action);
893 static void change_size(gpointer data, guint action, GtkWidget *widget)
895 g_return_if_fail(window_with_focus != NULL);
897 display_change_size(window_with_focus, action == 1);
900 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
902 g_return_if_fail(window_with_focus != NULL);
904 if (updating_menu)
905 return;
907 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
908 display_set_layout(window_with_focus,
909 window_with_focus->display_style,
910 window_with_focus->details_type, FALSE);
911 else
912 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
913 window_with_focus->details_type, FALSE);
916 static void set_with(gpointer data, guint action, GtkWidget *widget)
918 DisplayStyle size;
920 g_return_if_fail(window_with_focus != NULL);
922 size = window_with_focus->display_style_wanted;
924 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
925 display_set_layout(window_with_focus, size, action, FALSE);
928 static void set_sort(gpointer data, guint action, GtkWidget *widget)
930 if (updating_menu)
931 return;
933 g_return_if_fail(window_with_focus != NULL);
935 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
938 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
940 GtkSortType order;
942 if (updating_menu)
943 return;
945 g_return_if_fail(window_with_focus != NULL);
947 order = window_with_focus->sort_order;
948 if (order == GTK_SORT_ASCENDING)
949 order = GTK_SORT_DESCENDING;
950 else
951 order = GTK_SORT_ASCENDING;
953 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
954 order);
957 static void hidden(gpointer data, guint action, GtkWidget *widget)
959 if (updating_menu)
960 return;
962 g_return_if_fail(window_with_focus != NULL);
964 display_set_hidden(window_with_focus,
965 !window_with_focus->show_hidden);
968 static void filter_directories(gpointer data, guint action, GtkWidget *widget)
970 if (updating_menu)
971 return;
973 g_return_if_fail(window_with_focus != NULL);
975 display_set_filter_directories(window_with_focus,
976 !window_with_focus->filter_directories);
979 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
981 if (updating_menu)
982 return;
984 g_return_if_fail(window_with_focus != NULL);
986 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
989 static void refresh(gpointer data, guint action, GtkWidget *widget)
991 g_return_if_fail(window_with_focus != NULL);
993 filer_refresh(window_with_focus);
996 static void save_settings(gpointer data, guint action, GtkWidget *widget)
998 g_return_if_fail(window_with_focus != NULL);
1000 filer_save_settings(window_with_focus);
1003 static void delete(FilerWindow *filer_window)
1005 GList *paths;
1006 paths = filer_selected_items(filer_window);
1007 action_delete(paths);
1008 destroy_glist(&paths);
1011 static void usage(FilerWindow *filer_window)
1013 GList *paths;
1014 paths = filer_selected_items(filer_window);
1015 action_usage(paths);
1016 destroy_glist(&paths);
1019 static void chmod_items(FilerWindow *filer_window)
1021 GList *paths;
1022 paths = filer_selected_items(filer_window);
1023 action_chmod(paths, FALSE, NULL);
1024 destroy_glist(&paths);
1027 static void set_type_items(FilerWindow *filer_window)
1029 GList *paths, *p;
1030 int npass=0, nfail=0;
1032 paths = filer_selected_items(filer_window);
1033 for(p=paths; p; p=g_list_next(p)) {
1034 if(xattr_supported((const char *) p->data))
1035 npass++;
1036 else
1037 nfail++;
1039 if(npass==0)
1040 report_error(_("Extended attributes, used to store types, are not supported for this "
1041 "file or files.\n"
1042 "This may be due to lack of support from the filesystem or the C library, "
1043 "or it may simply be that the filesystem needs to be mounted with "
1044 "the right mount option ('user_xattr' on Linux)."));
1045 else if(nfail>0)
1046 report_error(_("Setting type not supported for some of these files"));
1047 if(npass>0)
1048 action_settype(paths, FALSE, NULL);
1049 destroy_glist(&paths);
1052 static void find(FilerWindow *filer_window)
1054 GList *paths;
1055 paths = filer_selected_items(filer_window);
1056 action_find(paths);
1057 destroy_glist(&paths);
1060 /* This creates a new savebox widget, and allows the user to pick a new path
1061 * for the file.
1062 * Once the new path has been picked, the callback will be called with
1063 * both the current and new paths.
1064 * NOTE: This function unrefs 'image'!
1066 static void savebox_show(const gchar *action, const gchar *path,
1067 MaskedPixmap *image, SaveCb callback,
1068 GdkDragAction dnd_action)
1070 GtkWidget *savebox = NULL;
1071 GtkWidget *check_relative = NULL;
1073 g_return_if_fail(image != NULL);
1075 savebox = gtk_savebox_new(action);
1076 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1078 if (callback == link_cb)
1080 check_relative = gtk_check_button_new_with_mnemonic(
1081 _("_Relative link"));
1082 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1083 TRUE);
1085 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1086 gtk_tooltips_set_tip(tooltips, check_relative,
1087 _("If on, the symlink will store the path from the "
1088 "symlink to the target file. Use this if the symlink "
1089 "and the target will be moved together.\n"
1090 "If off, the path from the root directory is stored - "
1091 "use this if the symlink may move but the target will "
1092 "stay put."), NULL);
1093 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1094 check_relative, FALSE, TRUE, 0);
1095 gtk_widget_show(check_relative);
1098 g_signal_connect(savebox, "save_to_file",
1099 G_CALLBACK(save_to_file), NULL);
1101 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1102 g_strdup(path), g_free);
1103 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1104 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1106 gtk_window_set_title(GTK_WINDOW(savebox), action);
1108 if (g_utf8_validate(path, -1, NULL))
1109 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1110 else
1112 gchar *u8, *dir;
1113 dir = g_path_get_dirname(path);
1114 u8 = to_utf8(g_basename(path));
1115 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1116 make_path(dir, u8));
1117 g_free(u8);
1118 g_free(dir);
1120 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1121 g_object_unref(image);
1123 gtk_widget_show(savebox);
1126 static gint save_to_file(GObject *savebox,
1127 const gchar *pathname, gpointer data)
1129 SaveCb callback;
1130 const gchar *current_path;
1132 callback = g_object_get_data(savebox, "action_callback");
1133 current_path = g_object_get_data(savebox, "current_path");
1135 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1136 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1138 return callback(savebox, current_path, pathname)
1139 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1142 static gboolean copy_cb(GObject *savebox,
1143 const gchar *current, const gchar *new)
1145 return action_with_leaf(action_copy, current, new);
1148 static gboolean action_with_leaf(ActionFn action,
1149 const gchar *current, const gchar *new)
1151 const char *leaf;
1152 char *new_dir;
1153 GList *local_paths;
1155 if (new[0] != '/')
1157 report_error(_("New pathname is not absolute"));
1158 return FALSE;
1161 if (new[strlen(new) - 1] == '/')
1163 new_dir = g_strdup(new);
1164 leaf = NULL;
1166 else
1168 const gchar *slash;
1170 slash = strrchr(new, '/');
1171 new_dir = g_strndup(new, slash - new);
1172 leaf = slash + 1;
1175 local_paths = g_list_append(NULL, (gchar *) current);
1176 action(local_paths, new_dir, leaf, -1);
1177 g_list_free(local_paths);
1179 g_free(new_dir);
1181 return TRUE;
1184 /* Open a savebox to act on the selected file.
1185 * Call 'callback' later to perform the operation.
1187 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1188 const gchar *action, SaveCb callback,
1189 GdkDragAction dnd_action)
1191 g_object_ref(image);
1192 savebox_show(action, path, image, callback, dnd_action);
1195 static gboolean rename_cb(GObject *savebox,
1196 const gchar *current, const gchar *new)
1198 return action_with_leaf(action_move, current, new);
1201 static gboolean link_cb(GObject *savebox,
1202 const gchar *initial, const gchar *path)
1204 GtkToggleButton *check_relative;
1205 struct stat info;
1206 int err;
1207 gchar *link_path;
1209 check_relative = g_object_get_data(savebox, "check_relative");
1211 if (gtk_toggle_button_get_active(check_relative))
1212 link_path = get_relative_path(path, initial);
1213 else
1214 link_path = g_strdup(initial);
1216 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1218 GtkWidget *box, *button;
1219 gint ans;
1221 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1222 GTK_BUTTONS_CANCEL,
1223 _("Symlink from '%s' already exists. "
1224 "Replace it with a link to '%s'?"),
1225 path, link_path);
1227 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1229 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1230 gtk_widget_show(button);
1231 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1232 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1233 button, GTK_RESPONSE_OK);
1234 gtk_dialog_set_default_response(GTK_DIALOG(box),
1235 GTK_RESPONSE_OK);
1237 ans = gtk_dialog_run(GTK_DIALOG(box));
1238 gtk_widget_destroy(box);
1240 if (ans != GTK_RESPONSE_OK)
1242 g_free(link_path);
1243 return FALSE;
1246 unlink(path);
1249 err = symlink(link_path, path);
1250 g_free(link_path);
1252 if (err)
1254 report_error("symlink: %s", g_strerror(errno));
1255 return FALSE;
1258 dir_check_this(path);
1260 return TRUE;
1263 static void run_action(DirItem *item)
1265 if (can_set_run_action(item))
1266 type_set_handler_dialog(item->mime_type);
1267 else
1268 report_error(
1269 _("You can only set the run action for a "
1270 "regular file"));
1273 void open_home(gpointer data, guint action, GtkWidget *widget)
1275 filer_opendir(home_dir, NULL, NULL);
1278 static void select_all(gpointer data, guint action, GtkWidget *widget)
1280 g_return_if_fail(window_with_focus != NULL);
1282 window_with_focus->temp_item_selected = FALSE;
1283 view_select_all(window_with_focus->view);
1286 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1288 g_return_if_fail(window_with_focus != NULL);
1290 window_with_focus->temp_item_selected = FALSE;
1291 view_clear_selection(window_with_focus->view);
1294 static gboolean invert_cb(ViewIter *iter, gpointer data)
1296 return !view_get_selected((ViewIface *) data, iter);
1299 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1301 g_return_if_fail(window_with_focus != NULL);
1303 window_with_focus->temp_item_selected = FALSE;
1305 view_select_if(window_with_focus->view, invert_cb,
1306 window_with_focus->view);
1309 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1311 GtkWidget *win;
1313 win = options_show();
1315 if (win)
1317 number_of_windows++;
1318 g_signal_connect(win, "destroy",
1319 G_CALLBACK(one_less_window), NULL);
1323 static gboolean new_directory_cb(GObject *savebox,
1324 const gchar *initial, const gchar *path)
1326 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1328 report_error("mkdir: %s", g_strerror(errno));
1329 return FALSE;
1332 dir_check_this(path);
1334 if (filer_exists(window_with_focus))
1336 guchar *leaf;
1337 leaf = strrchr(path, '/');
1338 if (leaf)
1339 display_set_autoselect(window_with_focus, leaf + 1);
1342 return TRUE;
1345 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1347 g_return_if_fail(window_with_focus != NULL);
1349 savebox_show(_("Create"),
1350 make_path(window_with_focus->sym_path, _("NewDir")),
1351 type_to_icon(inode_directory), new_directory_cb,
1352 GDK_ACTION_COPY);
1355 static gboolean new_file_cb(GObject *savebox,
1356 const gchar *initial, const gchar *path)
1358 int fd;
1360 fd = open(path, O_CREAT | O_EXCL, 0666);
1362 if (fd == -1)
1364 report_error(_("Error creating '%s': %s"),
1365 path, g_strerror(errno));
1366 return FALSE;
1369 if (close(fd))
1370 report_error(_("Error creating '%s': %s"),
1371 path, g_strerror(errno));
1373 dir_check_this(path);
1375 if (filer_exists(window_with_focus))
1377 guchar *leaf;
1378 leaf = strrchr(path, '/');
1379 if (leaf)
1380 display_set_autoselect(window_with_focus, leaf + 1);
1383 return TRUE;
1386 static void new_file(gpointer data, guint action, GtkWidget *widget)
1388 g_return_if_fail(window_with_focus != NULL);
1390 savebox_show(_("Create"),
1391 make_path(window_with_focus->sym_path, _("NewFile")),
1392 type_to_icon(text_plain),
1393 new_file_cb, GDK_ACTION_COPY);
1396 static gboolean new_file_type_cb(GObject *savebox,
1397 const gchar *initial, const gchar *path)
1399 const gchar *oleaf, *leaf;
1400 gchar *templ, *templ_dname, *dest;
1401 GList *paths;
1403 /* We can work out the template path from the initial name */
1404 oleaf = g_basename(initial);
1405 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1406 if (!templ_dname)
1408 report_error(
1409 _("Error creating file: could not find the template for %s"),
1410 oleaf);
1411 return FALSE;
1414 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1415 g_free(templ_dname);
1417 dest = g_path_get_dirname(path);
1418 leaf = g_basename(path);
1419 paths = g_list_append(NULL, templ);
1421 action_copy(paths, dest, leaf, TRUE);
1423 g_list_free(paths);
1424 g_free(dest);
1425 g_free(templ);
1427 if (filer_exists(window_with_focus))
1428 display_set_autoselect(window_with_focus, leaf);
1430 return TRUE;
1433 static void do_send_to(gchar *templ)
1435 g_return_if_fail(send_to_paths != NULL);
1437 run_with_files(templ, send_to_paths);
1440 static void new_file_type(gchar *templ)
1442 const gchar *leaf;
1443 MIME_type *type;
1445 g_return_if_fail(window_with_focus != NULL);
1447 leaf = g_basename(templ);
1448 type = type_get_type(templ);
1450 savebox_show(_("Create"),
1451 make_path(window_with_focus->sym_path, leaf),
1452 type_to_icon(type),
1453 new_file_type_cb, GDK_ACTION_COPY);
1456 static void customise_send_to(gpointer data)
1458 GPtrArray *path;
1459 guchar *save;
1460 GString *dirs;
1461 int i;
1463 dirs = g_string_new(NULL);
1465 path = choices_list_xdg_dirs("", SITE);
1466 for (i = 0; i < path->len; i++)
1468 guchar *old = (guchar *) path->pdata[i];
1470 g_string_append(dirs, old);
1471 g_string_append(dirs, "/SendTo\n");
1473 choices_free_list(path);
1475 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1476 if (save)
1477 mkdir(save, 0777);
1479 info_message(
1480 _("The `Send To' menu provides a quick way to send some files "
1481 "to an application. The applications listed are those in "
1482 "the following directories:\n\n%s\n%s\n"
1483 "The `Send To' menu may be opened by Shift+Menu clicking "
1484 "over a file.\n\n"
1485 "Advanced use:\n"
1486 "You can also create subdirectories called "
1487 "`.text_html', `.text', etc which will only be "
1488 "shown for files of that type. `.group' is shown "
1489 "only when multiple files are selected."),
1490 dirs->str,
1491 save ? _("I'll show you your SendTo directory now; you should "
1492 "symlink (Ctrl+Shift drag) any applications you want "
1493 "into it.")
1494 : _("Your CHOICESPATH variable setting prevents "
1495 "customisations - sorry."));
1497 g_string_free(dirs, TRUE);
1499 if (save)
1500 filer_opendir(save, NULL, NULL);
1503 static void customise_new(gpointer data)
1505 GPtrArray *path;
1506 guchar *save;
1507 GString *dirs;
1508 int i;
1510 dirs = g_string_new(NULL);
1512 path = choices_list_xdg_dirs("", SITE);
1513 for (i = 0; i < path->len; i++)
1515 guchar *old = (guchar *) path->pdata[i];
1517 g_string_append(dirs, old);
1518 g_string_append(dirs, "/Templates\n");
1520 choices_free_list(path);
1522 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1523 if (save)
1524 mkdir(save, 0777);
1526 info_message(
1527 _("Any files placed in your Templates directories will "
1528 "appear on the `New' menu. Choosing one of them will make "
1529 "a copy of it as the new file.\n\n"
1530 "The following directories contain templates:\n\n%s\n%s\n"),
1531 dirs->str,
1532 save ? _("I'll show you your Templates directory now; you "
1533 "should place any template files you want inside it.")
1534 : _("Your CHOICESPATH variable setting prevents "
1535 "customisations - sorry."));
1537 g_string_free(dirs, TRUE);
1539 if (save)
1540 filer_opendir(save, NULL, NULL);
1543 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1544 * to the menu.
1546 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1548 gchar *searchdir;
1549 GPtrArray *paths;
1550 int i;
1552 if (subtype)
1553 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1554 else if (type)
1555 searchdir = g_strdup_printf("SendTo/.%s", type);
1556 else
1557 searchdir = g_strdup("SendTo");
1559 paths = choices_list_xdg_dirs(searchdir, SITE);
1560 g_free(searchdir);
1562 for (i = 0; i < paths->len; i++)
1564 GList *widgets = NULL;
1565 guchar *dir = (guchar *) paths->pdata[i];
1567 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1568 (CallbackFn) do_send_to,
1569 FALSE, FALSE, TRUE);
1571 if (widgets)
1572 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1573 gtk_menu_item_new());
1575 g_list_free(widgets); /* TODO: Get rid of this */
1578 choices_free_list(paths);
1581 /* Scan the SendTo dir and create and show the Send To menu.
1582 * The 'paths' list and every path in it is claimed, and will be
1583 * freed later -- don't free it yourself!
1585 static void show_send_to_menu(GList *paths, GdkEvent *event)
1587 GtkWidget *menu, *item;
1589 menu = gtk_menu_new();
1591 if (g_list_length(paths) == 1)
1593 DirItem *item;
1595 item = diritem_new("");
1596 diritem_restat(paths->data, item, NULL);
1598 add_sendto(menu,
1599 item->mime_type->media_type,
1600 item->mime_type->subtype);
1602 add_sendto(menu, item->mime_type->media_type, NULL);
1604 diritem_free(item);
1606 else
1608 GList *rover;
1609 gboolean same=TRUE, same_media=TRUE;
1610 MIME_type *type=NULL;
1611 DirItem *item;
1613 item = diritem_new("");
1614 for(rover=paths; rover; rover=g_list_next(rover))
1616 diritem_restat(rover->data, item, NULL);
1617 if(!type)
1618 type=item->mime_type;
1619 else
1621 if(type!=item->mime_type)
1623 same=FALSE;
1624 if(strcmp(type->media_type,
1625 item->mime_type->media_type)!=0)
1627 same_media=FALSE;
1628 break;
1633 diritem_free(item);
1635 if(type)
1637 if(same)
1638 add_sendto(menu, type->media_type,
1639 type->subtype);
1640 if(same_media)
1641 add_sendto(menu, type->media_type, NULL);
1644 add_sendto(menu, "group", NULL);
1647 add_sendto(menu, NULL, NULL);
1649 item = gtk_menu_item_new_with_label(_("Customise"));
1650 g_signal_connect_swapped(item, "activate",
1651 G_CALLBACK(customise_send_to), NULL);
1652 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1654 if (send_to_paths)
1655 destroy_glist(&send_to_paths);
1657 send_to_paths = paths;
1659 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1661 popup_menu = menu;
1662 show_popup_menu(menu, event, 0);
1665 static void send_to(FilerWindow *filer_window)
1667 GList *paths;
1668 GdkEvent *event;
1670 paths = filer_selected_items(filer_window);
1671 event = gtk_get_current_event();
1673 /* Eats paths */
1674 show_send_to_menu(paths, event);
1676 if (event)
1677 gdk_event_free(event);
1680 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1682 const char *argv[] = {"sh", "-c", NULL, NULL};
1683 gboolean close = action;
1685 argv[2] = o_menu_xterm.value;
1687 g_return_if_fail(window_with_focus != NULL);
1689 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1690 gtk_widget_destroy(window_with_focus->window);
1693 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1695 g_return_if_fail(window_with_focus != NULL);
1697 filer_change_to(window_with_focus, home_dir, NULL);
1700 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1702 g_return_if_fail(window_with_focus != NULL);
1704 bookmarks_show_menu(window_with_focus);
1707 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1709 g_return_if_fail(window_with_focus != NULL);
1711 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1712 filer_change_to(window_with_focus,
1713 window_with_focus->real_path, NULL);
1714 else
1715 delayed_error(_("This is already the canonical name "
1716 "for this directory."));
1719 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1721 g_return_if_fail(window_with_focus != NULL);
1723 filer_open_parent(window_with_focus);
1726 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1728 g_return_if_fail(window_with_focus != NULL);
1730 change_to_parent(window_with_focus);
1733 static void resize(gpointer data, guint action, GtkWidget *widget)
1735 g_return_if_fail(window_with_focus != NULL);
1737 view_autosize(window_with_focus->view);
1740 static void new_window(gpointer data, guint action, GtkWidget *widget)
1742 g_return_if_fail(window_with_focus != NULL);
1744 if (o_unique_filer_windows.int_value)
1746 report_error(_("You can't open a second view onto "
1747 "this directory because the `Unique Windows' option "
1748 "is turned on in the Options window."));
1750 else
1751 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1754 static void close_window(gpointer data, guint action, GtkWidget *widget)
1756 g_return_if_fail(window_with_focus != NULL);
1758 if (!filer_window_delete(window_with_focus->window, NULL,
1759 window_with_focus))
1760 gtk_widget_destroy(window_with_focus->window);
1763 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1765 MiniType type = (MiniType) action;
1767 g_return_if_fail(window_with_focus != NULL);
1769 /* Item needs to remain selected... */
1770 if (type == MINI_SHELL)
1771 window_with_focus->temp_item_selected = FALSE;
1773 minibuffer_show(window_with_focus, type);
1776 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1778 if (action == HELP_ABOUT)
1779 infobox_new(app_dir);
1780 else if (action == HELP_DIR)
1781 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1782 else if (action == HELP_MANUAL)
1784 gchar *manual = NULL;
1786 if (current_lang)
1788 manual = g_strconcat(app_dir, "/Help/Manual-",
1789 current_lang, ".html", NULL);
1790 if (!file_exists(manual) && strchr(current_lang, '_'))
1792 /* Try again without the territory */
1793 strcpy(strrchr(manual, '_'), ".html");
1795 if (!file_exists(manual))
1796 null_g_free(&manual);
1799 if (!manual)
1800 manual = g_strconcat(app_dir,
1801 "/Help/Manual.html", NULL);
1803 run_by_path(manual);
1805 g_free(manual);
1807 else
1808 g_warning("Unknown help action %d\n", action);
1811 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1812 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1814 GList *items, *item;
1816 items = gtk_container_get_children(GTK_CONTAINER(menu));
1818 item = g_list_nth(items, from);
1819 while (item && n--)
1821 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1822 item = item->next;
1824 g_list_free(items);
1827 static void save_menus(void)
1829 char *menurc;
1831 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1832 if (menurc)
1834 gtk_accel_map_save(menurc);
1835 g_free(menurc);
1839 static void select_nth_item(GtkMenuShell *shell, int n)
1841 GList *items;
1842 GtkWidget *item;
1844 items = gtk_container_get_children(GTK_CONTAINER(shell));
1845 item = g_list_nth_data(items, n);
1847 g_return_if_fail(item != NULL);
1849 g_list_free(items);
1851 gtk_menu_shell_select_item(shell, item);
1854 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1856 DirItem *item;
1857 const guchar *path;
1858 int n_selected;
1859 ViewIter iter;
1861 g_return_if_fail(window_with_focus != NULL);
1863 n_selected = view_count_selected(window_with_focus->view);
1865 if (n_selected < 1)
1867 const char *prompt;
1869 switch (action)
1871 case FILE_COPY_ITEM:
1872 prompt = _("Copy ... ?");
1873 break;
1874 case FILE_RENAME_ITEM:
1875 prompt = _("Rename ... ?");
1876 break;
1877 case FILE_LINK_ITEM:
1878 prompt = _("Symlink ... ?");
1879 break;
1880 case FILE_OPEN_FILE:
1881 prompt = _("Shift Open ... ?");
1882 break;
1883 case FILE_PROPERTIES:
1884 prompt = _("Properties of ... ?");
1885 break;
1886 case FILE_SET_TYPE:
1887 prompt = _("Set type of ... ?");
1888 break;
1889 case FILE_RUN_ACTION:
1890 prompt = _("Set run action for ... ?");
1891 break;
1892 case FILE_SET_ICON:
1893 prompt = _("Set icon for ... ?");
1894 break;
1895 case FILE_SEND_TO:
1896 prompt = _("Send ... to ... ?");
1897 break;
1898 case FILE_DELETE:
1899 prompt = _("DELETE ... ?");
1900 break;
1901 case FILE_USAGE:
1902 prompt = _("Count the size of ... ?");
1903 break;
1904 case FILE_CHMOD_ITEMS:
1905 prompt = _("Set permissions on ... ?");
1906 break;
1907 case FILE_FIND:
1908 prompt = _("Search inside ... ?");
1909 break;
1910 default:
1911 g_warning("Unknown action!");
1912 return;
1914 filer_target_mode(window_with_focus, target_callback,
1915 GINT_TO_POINTER(action), prompt);
1916 return;
1919 switch (action)
1921 case FILE_SEND_TO:
1922 send_to(window_with_focus);
1923 return;
1924 case FILE_DELETE:
1925 delete(window_with_focus);
1926 return;
1927 case FILE_USAGE:
1928 usage(window_with_focus);
1929 return;
1930 case FILE_CHMOD_ITEMS:
1931 chmod_items(window_with_focus);
1932 return;
1933 case FILE_SET_TYPE:
1934 set_type_items(window_with_focus);
1935 return;
1936 case FILE_FIND:
1937 find(window_with_focus);
1938 return;
1939 case FILE_PROPERTIES:
1941 GList *items;
1943 items = filer_selected_items(window_with_focus);
1944 infobox_show_list(items);
1945 destroy_glist(&items);
1946 return;
1948 case FILE_RENAME_ITEM:
1949 if (n_selected > 1)
1951 GList *items = NULL;
1952 ViewIter iter;
1954 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1955 while ((item = iter.next(&iter)))
1956 items = g_list_prepend(items, item->leafname);
1957 items = g_list_reverse(items);
1959 bulk_rename(window_with_focus->sym_path, items);
1960 g_list_free(items);
1961 return;
1963 break; /* Not a bulk rename... see below */
1964 default:
1965 break;
1968 /* All the following actions require exactly one file selected */
1970 if (n_selected > 1)
1972 report_error(_("You cannot do this to more than "
1973 "one item at a time"));
1974 return;
1977 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1979 item = iter.next(&iter);
1980 g_return_if_fail(item != NULL);
1981 /* iter may be passed to filer_openitem... */
1983 if (item->base_type == TYPE_UNKNOWN)
1984 item = dir_update_item(window_with_focus->directory,
1985 item->leafname);
1987 if (!item)
1989 report_error(_("Item no longer exists!"));
1990 return;
1993 path = make_path(window_with_focus->sym_path, item->leafname);
1995 switch (action)
1997 case FILE_COPY_ITEM:
1998 src_dest_action_item(path, di_image(item),
1999 _("Copy"), copy_cb,
2000 GDK_ACTION_COPY);
2001 break;
2002 case FILE_RENAME_ITEM:
2003 src_dest_action_item(path, di_image(item),
2004 _("Rename"), rename_cb,
2005 GDK_ACTION_MOVE);
2006 break;
2007 case FILE_LINK_ITEM:
2008 src_dest_action_item(path, di_image(item),
2009 _("Symlink"), link_cb,
2010 GDK_ACTION_LINK);
2011 break;
2012 case FILE_OPEN_FILE:
2013 filer_openitem(window_with_focus, &iter,
2014 OPEN_SAME_WINDOW | OPEN_SHIFT);
2015 break;
2016 case FILE_RUN_ACTION:
2017 run_action(item);
2018 break;
2019 case FILE_SET_ICON:
2020 icon_set_handler_dialog(item, path);
2021 break;
2022 default:
2023 g_warning("Unknown action!");
2024 return;
2028 static void show_key_help(GtkWidget *button, gpointer data)
2030 gboolean can_change_accels;
2032 g_object_get(G_OBJECT(gtk_settings_get_default()),
2033 "gtk-can-change-accels", &can_change_accels,
2034 NULL);
2036 if (!can_change_accels)
2038 info_message(_("User-definable shortcuts are disabled by "
2039 "default in Gtk2, and you have not enabled "
2040 "them. You can turn this feature on by:\n\n"
2041 "1) using an XSettings manager, such as ROX-Session "
2042 "or gnome-settings-daemon, or\n\n"
2043 "2) adding this line to ~/.gtkrc-2.0:\n"
2044 "\tgtk-can-change-accels = 1\n"
2045 "\t(this only works if NOT using XSETTINGS)"));
2046 return;
2049 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2050 "- Open the menu over a filer window,\n"
2051 "- Move the pointer over the item you want to use,\n"
2052 "- Press the key you want attached to it.\n\n"
2053 "The key will appear next to the menu item and you can just press "
2054 "that key without opening the menu in future."));
2057 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2059 GtkWidget *button, *align;
2061 g_return_val_if_fail(option == NULL, NULL);
2063 align = gtk_alignment_new(0, 0.5, 0, 0);
2064 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2065 gtk_container_add(GTK_CONTAINER(align), button);
2066 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2068 return g_list_append(NULL, align);