r4523: Updated headers:
[rox-filer.git] / ROX-Filer / src / menu.c
blob3e3205635cf1e9da32b296db40043f328efcdcf9
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 hidden(gpointer data, guint action, GtkWidget *widget);
131 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
132 static void refresh(gpointer data, guint action, GtkWidget *widget);
133 static void save_settings(gpointer data, guint action, GtkWidget *widget);
135 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
137 static void select_all(gpointer data, guint action, GtkWidget *widget);
138 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
139 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
140 static void new_directory(gpointer data, guint action, GtkWidget *widget);
141 static void new_file(gpointer data, guint action, GtkWidget *widget);
142 static void customise_new(gpointer data);
143 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
145 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
146 static void open_parent(gpointer data, guint action, GtkWidget *widget);
147 static void home_directory(gpointer data, guint action, GtkWidget *widget);
148 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
149 static void new_window(gpointer data, guint action, GtkWidget *widget);
150 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
151 static void close_window(gpointer data, guint action, GtkWidget *widget);
152 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
154 /* (action used in this - MiniType) */
155 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
156 static void resize(gpointer data, guint action, GtkWidget *widget);
158 #define MENUS_NAME "menus2"
160 static GtkWidget *filer_menu; /* The popup filer menu */
161 static GtkWidget *filer_file_item; /* The File '' label */
162 static GtkWidget *filer_file_menu; /* The File '' menu */
163 static GtkWidget *file_shift_item; /* Shift Open label */
164 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
165 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
166 static GtkWidget *filer_reverse_menu; /* The Reversed item */
167 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
168 static GtkWidget *filer_new_window; /* The New Window item */
169 static GtkWidget *filer_new_menu; /* The New submenu */
170 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
171 static GtkWidget *filer_set_type; /* Set type item */
173 #undef N_
174 #define N_(x) x
176 static GtkItemFactoryEntry filer_menu_def[] = {
177 {N_("Display"), NULL, NULL, 0, "<Branch>"},
178 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
179 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
180 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
181 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
182 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
183 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
184 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
185 {">", NULL, NULL, 0, "<Separator>"},
186 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
187 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
188 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
189 {">", NULL, NULL, 0, "<Separator>"},
190 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
191 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
192 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
193 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
194 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
195 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
196 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
197 {">", NULL, NULL, 0, "<Separator>"},
198 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
199 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
200 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
201 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
202 {">" N_("Save Display Settings..."), NULL, save_settings, 0, NULL},
203 {N_("File"), NULL, NULL, 0, "<Branch>"},
204 {">" N_("Copy..."), "<Ctrl>C", file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
205 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
206 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
207 {">" N_("Delete"), "<Ctrl>X", file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
208 {">", NULL, NULL, 0, "<Separator>"},
209 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
210 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
211 {">", NULL, NULL, 0, "<Separator>"},
212 {">" N_("Set Run Action..."), "asterisk", file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
213 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
214 {">" N_("Properties"), "<Ctrl>P", file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
215 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
216 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
217 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
218 {">", NULL, NULL, 0, "<Separator>"},
219 {">" N_("Find"), "<Ctrl>F", file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
220 {N_("Select"), NULL, NULL, 0, "<Branch>"},
221 {">" N_("Select All"), "<Ctrl>A", select_all, 0, NULL},
222 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
223 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
224 {">" N_("Select by Name..."), "period", mini_buffer, MINI_SELECT_BY_NAME, NULL},
225 {">" N_("Select If..."), "<Shift>question", mini_buffer, MINI_SELECT_IF, NULL},
226 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
227 {N_("New"), NULL, NULL, 0, "<Branch>"},
228 {">" N_("Directory"), NULL, new_directory, 0, NULL},
229 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
230 {">" N_("Customise Menu..."), NULL, customise_new, 0, NULL},
231 {N_("Window"), NULL, NULL, 0, "<Branch>"},
232 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
233 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
234 {">" N_("New Window"), NULL, new_window, 0, NULL},
235 {">" N_("Home Directory"), "<Ctrl>Home", home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
236 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
237 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
238 {">" N_("Resize Window"), "<Ctrl>E", resize, 0, NULL},
239 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
241 {">" N_("Close Window"), "<Ctrl>Q", close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
242 {">", NULL, NULL, 0, "<Separator>"},
243 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
244 {">" N_("Shell Command..."), "<Shift>exclam", mini_buffer, MINI_SHELL, NULL},
245 {">" N_("Terminal Here"), "grave", xterm_here, FALSE, NULL},
246 {">" N_("Switch to Terminal"), NULL, xterm_here, TRUE, NULL},
247 {N_("Help"), NULL, NULL, 0, "<Branch>"},
248 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
249 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
250 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
254 #define GET_MENU_ITEM(var, menu) \
255 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
257 #define GET_SMENU_ITEM(var, menu, sub) \
258 do { \
259 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
260 var = gtk_item_factory_get_widget(item_factory, tmp); \
261 g_free(tmp); \
262 } while (0)
264 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
265 do { \
266 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
267 var = gtk_item_factory_get_widget(item_factory, tmp); \
268 g_free(tmp); \
269 } while (0)
271 /* Returns TRUE if the keys were installed (first call only) */
272 gboolean ensure_filer_menu(void)
274 GList *items;
275 guchar *tmp;
276 GtkWidget *item;
277 GtkItemFactory *item_factory;
279 if (!filer_keys_need_init)
280 return FALSE;
281 filer_keys_need_init = FALSE;
283 item_factory = menu_create(filer_menu_def,
284 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
285 "<filer>", filer_keys);
287 GET_MENU_ITEM(filer_menu, "filer");
288 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
289 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
290 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
291 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
292 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
293 "Show Thumbnails");
294 GET_SSMENU_ITEM(item, "filer", "File", "Set Type...");
295 filer_set_type = GTK_BIN(item)->child;
297 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
298 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
299 filer_follow_sym = GTK_BIN(item)->child;
301 /* File '' label... */
302 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
303 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
304 g_list_free(items);
306 /* Shift Open... label */
307 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
308 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
309 g_list_free(items);
311 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
312 filer_new_window = GTK_BIN(item)->child;
314 g_signal_connect(filer_menu, "unmap_event",
315 G_CALLBACK(menu_closed), NULL);
316 g_signal_connect(filer_file_menu, "unmap_event",
317 G_CALLBACK(menu_closed), NULL);
319 g_signal_connect(filer_keys, "accel_changed",
320 G_CALLBACK(save_menus), NULL);
322 return TRUE;
325 void menu_init(void)
327 char *menurc;
329 menurc = choices_find_xdg_path_load(MENUS_NAME, PROJECT, SITE);
330 if (menurc)
332 gtk_accel_map_load(menurc);
333 g_free(menurc);
336 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
337 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
338 option_add_int(&o_menu_quick, "menu_quick", FALSE);
339 option_add_saver(save_menus);
341 option_register_widget("menu-set-keys", set_keys_button);
343 filer_keys = gtk_accel_group_new();
346 /* Name is in the form "<panel>" */
347 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
348 const gchar *name, GtkAccelGroup *keys)
350 GtkItemFactory *item_factory;
351 GtkItemFactoryEntry *translated;
353 if (!keys)
355 keys = gtk_accel_group_new();
356 gtk_accel_group_lock(keys);
359 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
361 translated = translate_entries(def, n_entries);
362 gtk_item_factory_create_items(item_factory, n_entries,
363 translated, NULL);
364 free_translated_entries(translated, n_entries);
366 return item_factory;
369 /* Prevent the user from setting a short-cut on this item */
370 static void menuitem_no_shortcuts(GtkWidget *item)
372 /* XXX */
373 #if 0
374 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
376 _gtk_widget_set_accel_path(item, NULL, NULL);
377 null_g_free(&menuitem->accel_path);
378 #endif
381 /* Shade items that only work on single files */
382 static void shade_file_menu_items(gboolean shaded)
384 menu_set_items_shaded(filer_file_menu, shaded, 0, 1);
385 menu_set_items_shaded(filer_file_menu, shaded, 2, 1);
386 menu_set_items_shaded(filer_file_menu, shaded, 5, 1);
387 menu_set_items_shaded(filer_file_menu, shaded, 8, 2);
390 /* 'data' is an array of three ints:
391 * [ pointer_x, pointer_y, item_under_pointer ]
393 void position_menu(GtkMenu *menu, gint *x, gint *y,
394 gboolean *push_in, gpointer data)
396 int *pos = (int *) data;
397 GtkRequisition requisition;
398 GList *items, *next;
399 int y_shift = 0;
400 int item = pos[2];
402 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
404 while (item >= 0 && next)
406 int h = ((GtkWidget *) next->data)->requisition.height;
408 if (item > 0)
409 y_shift += h;
410 else
411 y_shift += h / 2;
413 next = next->next;
414 item--;
417 g_list_free(items);
419 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
421 *x = pos[0] - (requisition.width * 7 / 8);
422 *y = pos[1] - y_shift;
424 *x = CLAMP(*x, 0, screen_width - requisition.width);
425 *y = CLAMP(*y, 0, screen_height - requisition.height);
427 *push_in = FALSE;
430 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
431 MenuIconStyle style)
433 GtkWidget *item;
435 if (style != MIS_NONE && di_image(ditem))
437 GdkPixbuf *pixbuf;
438 MaskedPixmap *image;
440 image = di_image(ditem);
442 switch (style)
444 case MIS_LARGE:
445 pixbuf = image->pixbuf;
446 break;
447 default:
448 if (!image->sm_pixbuf)
449 pixmap_make_small(image);
450 pixbuf = image->sm_pixbuf;
451 break;
454 item = gtk_image_menu_item_new_with_label(label);
455 /* TODO: Find a way to allow short-cuts */
456 menuitem_no_shortcuts(item);
458 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
459 gtk_image_new_from_pixbuf(pixbuf));
460 gtk_widget_show_all(item);
462 else
463 item = gtk_menu_item_new_with_label(label);
465 return item;
468 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
469 MenuIconStyle style, CallbackFn func,
470 gboolean separator, gboolean strip_ext,
471 gboolean recurse)
473 GList *widgets = NULL;
474 DirItem *ditem;
475 int i;
476 GtkWidget *item;
477 char *dname = NULL;
478 GPtrArray *names;
480 dname = pathdup(dir_name);
482 names = list_dir(dname);
483 if (!names)
484 goto out;
486 for (i = 0; i < names->len; i++)
488 char *leaf = names->pdata[i];
489 gchar *fname;
491 if (separator)
493 item = gtk_menu_item_new();
494 widgets = g_list_append(widgets, item);
495 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
496 separator = FALSE;
499 fname = g_strconcat(dname, "/", leaf, NULL);
501 /* Strip off extension, if any */
502 if (strip_ext)
504 char *dot;
505 dot = strchr(leaf, '.');
506 if (dot)
507 *dot = '\0';
510 ditem = diritem_new("");
511 diritem_restat(fname, ditem, NULL);
513 item = make_send_to_item(ditem, leaf, style);
515 g_free(leaf);
517 /* If it is a directory (but NOT an AppDir) and we are
518 * recursing then set up a sub menu.
520 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
521 !(ditem->flags & ITEM_FLAG_APPDIR))
523 GtkWidget *sub;
524 GList *new_widgets;
526 sub = gtk_menu_new();
527 new_widgets = menu_from_dir(sub, fname, style, func,
528 separator, strip_ext, TRUE);
529 g_list_free(new_widgets);
530 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
532 else
533 g_signal_connect_swapped(item, "activate",
534 G_CALLBACK(func), fname);
536 diritem_free(ditem);
538 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
539 g_signal_connect_swapped(item, "destroy",
540 G_CALLBACK(g_free), fname);
542 widgets = g_list_append(widgets, item);
545 g_ptr_array_free(names, TRUE);
546 out:
547 g_free(dname);
549 return widgets;
552 /* Scan the templates dir and create entries for the New menu */
553 static void update_new_files_menu(MenuIconStyle style)
555 static GList *widgets = NULL;
557 gchar *templ_dname = NULL;
559 if (widgets)
561 GList *next;
563 for (next = widgets; next; next = next->next)
564 gtk_widget_destroy((GtkWidget *) next->data);
566 g_list_free(widgets);
567 widgets = NULL;
570 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
571 if (templ_dname)
573 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
574 (CallbackFn) new_file_type, TRUE, TRUE,
575 FALSE);
576 g_free(templ_dname);
578 gtk_widget_show_all(filer_new_menu);
581 /* 'item' is the number of the item to appear under the pointer. */
582 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
584 int pos[3];
585 int button = 0;
586 guint32 time = 0;
588 if (event && (event->type == GDK_BUTTON_PRESS ||
589 event->type == GDK_BUTTON_RELEASE))
591 GdkEventButton *bev = (GdkEventButton *) event;
593 pos[0] = bev->x_root;
594 pos[1] = bev->y_root;
595 button = bev->button;
596 time = bev->time;
598 else if (event && event->type == GDK_KEY_PRESS)
600 GdkEventKey *kev = (GdkEventKey *) event;
602 get_pointer_xy(pos, pos + 1);
603 time = kev->time;
605 else
606 get_pointer_xy(pos, pos + 1);
608 pos[2] = item;
610 gtk_widget_show_all(menu);
611 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
612 position_menu, (gpointer) pos, button, time);
613 select_nth_item(GTK_MENU_SHELL(menu), item);
616 /* Hide the popup menu, if any */
617 void menu_popdown(void)
619 if (popup_menu)
620 gtk_menu_popdown(GTK_MENU(popup_menu));
623 static MenuIconStyle get_menu_icon_style(void)
625 MenuIconStyle mis;
626 int display;
628 mis = o_menu_iconsize.int_value;
630 switch (mis)
632 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
633 return mis;
634 default:
635 break;
638 if (mis == MIS_CURRENT && window_with_focus)
640 switch (window_with_focus->display_style)
642 case HUGE_ICONS:
643 case LARGE_ICONS:
644 return MIS_LARGE;
645 case SMALL_ICONS:
646 return MIS_SMALL;
647 default:
648 break;
652 display = o_display_size.int_value;
653 switch (display)
655 case HUGE_ICONS:
656 case LARGE_ICONS:
657 return MIS_LARGE;
658 case SMALL_ICONS:
659 return MIS_SMALL;
660 default:
661 break;
664 return MIS_SMALL;
667 /* iter->peek() is the clicked item, or NULL if none */
668 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
670 DirItem *file_item = NULL;
671 GdkModifierType state = 0;
672 int n_selected;
673 int n_added = 0;
675 g_return_if_fail(event != NULL);
677 n_selected = view_count_selected(filer_window->view);
679 ensure_filer_menu();
681 updating_menu++;
683 /* Remove previous AppMenu, if any */
684 appmenu_remove();
686 window_with_focus = filer_window;
688 if (event->type == GDK_BUTTON_PRESS)
689 state = ((GdkEventButton *) event)->state;
690 else if (event->type == GDK_KEY_PRESS)
691 state = ((GdkEventKey *) event)->state;
693 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
695 filer_window->temp_item_selected = TRUE;
696 view_set_selected(filer_window->view, iter, TRUE);
697 n_selected = view_count_selected(filer_window->view);
699 else
701 filer_window->temp_item_selected = FALSE;
704 /* Short-cut to the Send To menu */
705 if (state & GDK_SHIFT_MASK)
707 GList *paths;
709 updating_menu--;
711 if (n_selected == 0)
713 report_error(
714 _("You should Shift+Menu click over a file to "
715 "send it somewhere"));
716 return;
719 paths = filer_selected_items(filer_window);
721 show_send_to_menu(paths, event); /* (paths eaten) */
723 return;
727 GtkWidget *file_label, *file_menu;
728 GString *buffer;
729 DirItem *item;
731 file_label = filer_file_item;
732 file_menu = filer_file_menu;
733 gtk_check_menu_item_set_active(
734 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
735 filer_window->show_thumbs);
736 gtk_check_menu_item_set_active(
737 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
738 filer_window->show_hidden);
739 gtk_check_menu_item_set_active(
740 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
741 filer_window->sort_order != GTK_SORT_ASCENDING);
742 gtk_check_menu_item_set_active(
743 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
744 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
745 buffer = g_string_new(NULL);
747 switch (n_selected)
749 case 0:
750 g_string_assign(buffer, _("Next Click"));
751 shade_file_menu_items(FALSE);
752 break;
753 case 1:
754 item = filer_selected_item(filer_window);
755 if (item->base_type == TYPE_UNKNOWN)
756 dir_update_item(filer_window->directory,
757 item->leafname);
758 shade_file_menu_items(FALSE);
759 file_item = filer_selected_item(filer_window);
760 g_string_printf(buffer, _("%s '%s'"),
761 basetype_name(file_item),
762 g_utf8_validate(file_item->leafname,
763 -1, NULL)
764 ? file_item->leafname
765 : _("(bad utf-8)"));
766 if (!can_set_run_action(file_item))
767 menu_set_items_shaded(filer_file_menu,
768 TRUE, 8, 1);
769 break;
770 default:
771 shade_file_menu_items(TRUE);
772 g_string_printf(buffer, _("%d items"),
773 n_selected);
774 break;
776 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
777 g_string_free(buffer, TRUE);
779 menu_show_shift_action(file_shift_item, file_item,
780 n_selected == 0);
781 if (file_item)
782 n_added = appmenu_add(make_path(filer_window->sym_path,
783 file_item->leafname),
784 file_item, filer_file_menu);
787 update_new_files_menu(get_menu_icon_style());
789 gtk_widget_set_sensitive(filer_new_window,
790 !o_unique_filer_windows.int_value);
791 gtk_widget_set_sensitive(filer_follow_sym,
792 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
793 gtk_widget_set_sensitive(filer_set_type,
794 xattr_supported(filer_window->real_path));
796 if (n_selected && o_menu_quick.int_value)
797 popup_menu = (state & GDK_CONTROL_MASK)
798 ? filer_menu
799 : filer_file_menu;
800 else
801 popup_menu = (state & GDK_CONTROL_MASK)
802 ? filer_file_menu
803 : filer_menu;
805 updating_menu--;
807 show_popup_menu(popup_menu, event,
808 popup_menu == filer_file_menu ? n_added : 1);
811 static void menu_closed(GtkWidget *widget)
813 if (window_with_focus == NULL || widget != popup_menu)
814 return; /* Close panel item chosen? */
816 popup_menu = NULL;
818 if (window_with_focus->temp_item_selected)
820 view_clear_selection(window_with_focus->view);
821 window_with_focus->temp_item_selected = FALSE;
824 appmenu_remove();
827 static void target_callback(FilerWindow *filer_window,
828 ViewIter *iter,
829 gpointer action)
831 g_return_if_fail(filer_window != NULL);
833 window_with_focus = filer_window;
835 /* Don't grab the primary selection */
836 filer_window->temp_item_selected = TRUE;
838 view_wink_item(filer_window->view, iter);
839 view_select_only(filer_window->view, iter);
840 file_op(NULL, GPOINTER_TO_INT(action), NULL);
842 view_clear_selection(filer_window->view);
843 filer_window->temp_item_selected = FALSE;
846 /* Set the text of the 'Shift Open...' menu item.
847 * If icon is NULL, reset the text and also shade it, unless 'next'.
849 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
851 guchar *shift_action = NULL;
853 if (item)
855 if (item->flags & ITEM_FLAG_MOUNT_POINT)
857 if (item->flags & ITEM_FLAG_MOUNTED)
858 shift_action = N_("Unmount");
859 else
860 shift_action = N_("Open unmounted");
862 else if (item->flags & ITEM_FLAG_SYMLINK)
863 shift_action = N_("Show Target");
864 else if (item->base_type == TYPE_DIRECTORY)
865 shift_action = N_("Look Inside");
866 else if (item->base_type == TYPE_FILE)
867 shift_action = N_("Open As Text");
869 gtk_label_set_text(GTK_LABEL(menu_item),
870 shift_action ? _(shift_action)
871 : _("Shift Open"));
872 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
875 /* Actions */
877 static void view_type(gpointer data, guint action, GtkWidget *widget)
879 ViewType view_type = (ViewType) action;
881 g_return_if_fail(window_with_focus != NULL);
883 if (view_type == VIEW_TYPE_COLLECTION)
884 display_set_layout(window_with_focus,
885 window_with_focus->display_style_wanted,
886 DETAILS_NONE, FALSE);
888 filer_set_view_type(window_with_focus, (ViewType) action);
891 static void change_size(gpointer data, guint action, GtkWidget *widget)
893 g_return_if_fail(window_with_focus != NULL);
895 display_change_size(window_with_focus, action == 1);
898 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
900 g_return_if_fail(window_with_focus != NULL);
902 if (updating_menu)
903 return;
905 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
906 display_set_layout(window_with_focus,
907 window_with_focus->display_style,
908 window_with_focus->details_type, FALSE);
909 else
910 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
911 window_with_focus->details_type, FALSE);
914 static void set_with(gpointer data, guint action, GtkWidget *widget)
916 DisplayStyle size;
918 g_return_if_fail(window_with_focus != NULL);
920 size = window_with_focus->display_style_wanted;
922 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
923 display_set_layout(window_with_focus, size, action, FALSE);
926 static void set_sort(gpointer data, guint action, GtkWidget *widget)
928 if (updating_menu)
929 return;
931 g_return_if_fail(window_with_focus != NULL);
933 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
936 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
938 GtkSortType order;
940 if (updating_menu)
941 return;
943 g_return_if_fail(window_with_focus != NULL);
945 order = window_with_focus->sort_order;
946 if (order == GTK_SORT_ASCENDING)
947 order = GTK_SORT_DESCENDING;
948 else
949 order = GTK_SORT_ASCENDING;
951 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
952 order);
955 static void hidden(gpointer data, guint action, GtkWidget *widget)
957 if (updating_menu)
958 return;
960 g_return_if_fail(window_with_focus != NULL);
962 display_set_hidden(window_with_focus,
963 !window_with_focus->show_hidden);
966 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
968 if (updating_menu)
969 return;
971 g_return_if_fail(window_with_focus != NULL);
973 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
976 static void refresh(gpointer data, guint action, GtkWidget *widget)
978 g_return_if_fail(window_with_focus != NULL);
980 filer_refresh(window_with_focus);
983 static void save_settings(gpointer data, guint action, GtkWidget *widget)
985 g_return_if_fail(window_with_focus != NULL);
987 filer_save_settings(window_with_focus);
990 static void delete(FilerWindow *filer_window)
992 GList *paths;
993 paths = filer_selected_items(filer_window);
994 action_delete(paths);
995 destroy_glist(&paths);
998 static void usage(FilerWindow *filer_window)
1000 GList *paths;
1001 paths = filer_selected_items(filer_window);
1002 action_usage(paths);
1003 destroy_glist(&paths);
1006 static void chmod_items(FilerWindow *filer_window)
1008 GList *paths;
1009 paths = filer_selected_items(filer_window);
1010 action_chmod(paths, FALSE, NULL);
1011 destroy_glist(&paths);
1014 static void set_type_items(FilerWindow *filer_window)
1016 GList *paths, *p;
1017 int npass=0, nfail=0;
1019 paths = filer_selected_items(filer_window);
1020 for(p=paths; p; p=g_list_next(p)) {
1021 if(xattr_supported((const char *) p->data))
1022 npass++;
1023 else
1024 nfail++;
1026 if(npass==0)
1027 report_error(_("Extended attributes, used to store types, are not supported for this "
1028 "file or files.\n"
1029 "This may be due to lack of support from the filesystem or the C library, "
1030 "or it may simply be that the filesystem needs to be mounted with "
1031 "the right mount option ('user_xattr' on Linux)."));
1032 else if(nfail>0)
1033 report_error(_("Setting type not supported for some of these files"));
1034 if(npass>0)
1035 action_settype(paths, FALSE, NULL);
1036 destroy_glist(&paths);
1039 static void find(FilerWindow *filer_window)
1041 GList *paths;
1042 paths = filer_selected_items(filer_window);
1043 action_find(paths);
1044 destroy_glist(&paths);
1047 /* This creates a new savebox widget, and allows the user to pick a new path
1048 * for the file.
1049 * Once the new path has been picked, the callback will be called with
1050 * both the current and new paths.
1051 * NOTE: This function unrefs 'image'!
1053 static void savebox_show(const gchar *action, const gchar *path,
1054 MaskedPixmap *image, SaveCb callback,
1055 GdkDragAction dnd_action)
1057 GtkWidget *savebox = NULL;
1058 GtkWidget *check_relative = NULL;
1060 g_return_if_fail(image != NULL);
1062 savebox = gtk_savebox_new(action);
1063 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1065 if (callback == link_cb)
1067 check_relative = gtk_check_button_new_with_mnemonic(
1068 _("_Relative link"));
1069 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1070 TRUE);
1072 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1073 gtk_tooltips_set_tip(tooltips, check_relative,
1074 _("If on, the symlink will store the path from the "
1075 "symlink to the target file. Use this if the symlink "
1076 "and the target will be moved together.\n"
1077 "If off, the path from the root directory is stored - "
1078 "use this if the symlink may move but the target will "
1079 "stay put."), NULL);
1080 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1081 check_relative, FALSE, TRUE, 0);
1082 gtk_widget_show(check_relative);
1085 g_signal_connect(savebox, "save_to_file",
1086 G_CALLBACK(save_to_file), NULL);
1088 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1089 g_strdup(path), g_free);
1090 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1091 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1093 gtk_window_set_title(GTK_WINDOW(savebox), action);
1095 if (g_utf8_validate(path, -1, NULL))
1096 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1097 else
1099 gchar *u8, *dir;
1100 dir = g_path_get_dirname(path);
1101 u8 = to_utf8(g_basename(path));
1102 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1103 make_path(dir, u8));
1104 g_free(u8);
1105 g_free(dir);
1107 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1108 g_object_unref(image);
1110 gtk_widget_show(savebox);
1113 static gint save_to_file(GObject *savebox,
1114 const gchar *pathname, gpointer data)
1116 SaveCb callback;
1117 const gchar *current_path;
1119 callback = g_object_get_data(savebox, "action_callback");
1120 current_path = g_object_get_data(savebox, "current_path");
1122 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1123 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1125 return callback(savebox, current_path, pathname)
1126 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1129 static gboolean copy_cb(GObject *savebox,
1130 const gchar *current, const gchar *new)
1132 return action_with_leaf(action_copy, current, new);
1135 static gboolean action_with_leaf(ActionFn action,
1136 const gchar *current, const gchar *new)
1138 const char *leaf;
1139 char *new_dir;
1140 GList *local_paths;
1142 if (new[0] != '/')
1144 report_error(_("New pathname is not absolute"));
1145 return FALSE;
1148 if (new[strlen(new) - 1] == '/')
1150 new_dir = g_strdup(new);
1151 leaf = NULL;
1153 else
1155 const gchar *slash;
1157 slash = strrchr(new, '/');
1158 new_dir = g_strndup(new, slash - new);
1159 leaf = slash + 1;
1162 local_paths = g_list_append(NULL, (gchar *) current);
1163 action(local_paths, new_dir, leaf, -1);
1164 g_list_free(local_paths);
1166 g_free(new_dir);
1168 return TRUE;
1171 /* Open a savebox to act on the selected file.
1172 * Call 'callback' later to perform the operation.
1174 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1175 const gchar *action, SaveCb callback,
1176 GdkDragAction dnd_action)
1178 g_object_ref(image);
1179 savebox_show(action, path, image, callback, dnd_action);
1182 static gboolean rename_cb(GObject *savebox,
1183 const gchar *current, const gchar *new)
1185 return action_with_leaf(action_move, current, new);
1188 static gboolean link_cb(GObject *savebox,
1189 const gchar *initial, const gchar *path)
1191 GtkToggleButton *check_relative;
1192 struct stat info;
1193 int err;
1194 gchar *link_path;
1196 check_relative = g_object_get_data(savebox, "check_relative");
1198 if (gtk_toggle_button_get_active(check_relative))
1199 link_path = get_relative_path(path, initial);
1200 else
1201 link_path = g_strdup(initial);
1203 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1205 GtkWidget *box, *button;
1206 gint ans;
1208 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1209 GTK_BUTTONS_CANCEL,
1210 _("Symlink from '%s' already exists. "
1211 "Replace it with a link to '%s'?"),
1212 path, link_path);
1214 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1216 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1217 gtk_widget_show(button);
1218 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1219 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1220 button, GTK_RESPONSE_OK);
1221 gtk_dialog_set_default_response(GTK_DIALOG(box),
1222 GTK_RESPONSE_OK);
1224 ans = gtk_dialog_run(GTK_DIALOG(box));
1225 gtk_widget_destroy(box);
1227 if (ans != GTK_RESPONSE_OK)
1229 g_free(link_path);
1230 return FALSE;
1233 unlink(path);
1236 err = symlink(link_path, path);
1237 g_free(link_path);
1239 if (err)
1241 report_error("symlink: %s", g_strerror(errno));
1242 return FALSE;
1245 dir_check_this(path);
1247 return TRUE;
1250 static void run_action(DirItem *item)
1252 if (can_set_run_action(item))
1253 type_set_handler_dialog(item->mime_type);
1254 else
1255 report_error(
1256 _("You can only set the run action for a "
1257 "regular file"));
1260 void open_home(gpointer data, guint action, GtkWidget *widget)
1262 filer_opendir(home_dir, NULL, NULL);
1265 static void select_all(gpointer data, guint action, GtkWidget *widget)
1267 g_return_if_fail(window_with_focus != NULL);
1269 window_with_focus->temp_item_selected = FALSE;
1270 view_select_all(window_with_focus->view);
1273 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1275 g_return_if_fail(window_with_focus != NULL);
1277 window_with_focus->temp_item_selected = FALSE;
1278 view_clear_selection(window_with_focus->view);
1281 static gboolean invert_cb(ViewIter *iter, gpointer data)
1283 return !view_get_selected((ViewIface *) data, iter);
1286 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1288 g_return_if_fail(window_with_focus != NULL);
1290 window_with_focus->temp_item_selected = FALSE;
1292 view_select_if(window_with_focus->view, invert_cb,
1293 window_with_focus->view);
1296 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1298 GtkWidget *win;
1300 win = options_show();
1302 if (win)
1304 number_of_windows++;
1305 g_signal_connect(win, "destroy",
1306 G_CALLBACK(one_less_window), NULL);
1310 static gboolean new_directory_cb(GObject *savebox,
1311 const gchar *initial, const gchar *path)
1313 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1315 report_error("mkdir: %s", g_strerror(errno));
1316 return FALSE;
1319 dir_check_this(path);
1321 if (filer_exists(window_with_focus))
1323 guchar *leaf;
1324 leaf = strrchr(path, '/');
1325 if (leaf)
1326 display_set_autoselect(window_with_focus, leaf + 1);
1329 return TRUE;
1332 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1334 g_return_if_fail(window_with_focus != NULL);
1336 savebox_show(_("Create"),
1337 make_path(window_with_focus->sym_path, _("NewDir")),
1338 type_to_icon(inode_directory), new_directory_cb,
1339 GDK_ACTION_COPY);
1342 static gboolean new_file_cb(GObject *savebox,
1343 const gchar *initial, const gchar *path)
1345 int fd;
1347 fd = open(path, O_CREAT | O_EXCL, 0666);
1349 if (fd == -1)
1351 report_error(_("Error creating '%s': %s"),
1352 path, g_strerror(errno));
1353 return FALSE;
1356 if (close(fd))
1357 report_error(_("Error creating '%s': %s"),
1358 path, g_strerror(errno));
1360 dir_check_this(path);
1362 if (filer_exists(window_with_focus))
1364 guchar *leaf;
1365 leaf = strrchr(path, '/');
1366 if (leaf)
1367 display_set_autoselect(window_with_focus, leaf + 1);
1370 return TRUE;
1373 static void new_file(gpointer data, guint action, GtkWidget *widget)
1375 g_return_if_fail(window_with_focus != NULL);
1377 savebox_show(_("Create"),
1378 make_path(window_with_focus->sym_path, _("NewFile")),
1379 type_to_icon(text_plain),
1380 new_file_cb, GDK_ACTION_COPY);
1383 static gboolean new_file_type_cb(GObject *savebox,
1384 const gchar *initial, const gchar *path)
1386 const gchar *oleaf, *leaf;
1387 gchar *templ, *templ_dname, *dest;
1388 GList *paths;
1390 /* We can work out the template path from the initial name */
1391 oleaf = g_basename(initial);
1392 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1393 if (!templ_dname)
1395 report_error(
1396 _("Error creating file: could not find the template for %s"),
1397 oleaf);
1398 return FALSE;
1401 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1402 g_free(templ_dname);
1404 dest = g_path_get_dirname(path);
1405 leaf = g_basename(path);
1406 paths = g_list_append(NULL, templ);
1408 action_copy(paths, dest, leaf, TRUE);
1410 g_list_free(paths);
1411 g_free(dest);
1412 g_free(templ);
1414 if (filer_exists(window_with_focus))
1415 display_set_autoselect(window_with_focus, leaf);
1417 return TRUE;
1420 static void do_send_to(gchar *templ)
1422 g_return_if_fail(send_to_paths != NULL);
1424 run_with_files(templ, send_to_paths);
1427 static void new_file_type(gchar *templ)
1429 const gchar *leaf;
1430 MIME_type *type;
1432 g_return_if_fail(window_with_focus != NULL);
1434 leaf = g_basename(templ);
1435 type = type_get_type(templ);
1437 savebox_show(_("Create"),
1438 make_path(window_with_focus->sym_path, leaf),
1439 type_to_icon(type),
1440 new_file_type_cb, GDK_ACTION_COPY);
1443 static void customise_send_to(gpointer data)
1445 GPtrArray *path;
1446 guchar *save;
1447 GString *dirs;
1448 int i;
1450 dirs = g_string_new(NULL);
1452 path = choices_list_xdg_dirs("", SITE);
1453 for (i = 0; i < path->len; i++)
1455 guchar *old = (guchar *) path->pdata[i];
1457 g_string_append(dirs, old);
1458 g_string_append(dirs, "/SendTo\n");
1460 choices_free_list(path);
1462 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1463 if (save)
1464 mkdir(save, 0777);
1466 info_message(
1467 _("The `Send To' menu provides a quick way to send some files "
1468 "to an application. The applications listed are those in "
1469 "the following directories:\n\n%s\n%s\n"
1470 "The `Send To' menu may be opened by Shift+Menu clicking "
1471 "over a file.\n\n"
1472 "Advanced use:\n"
1473 "You can also create subdirectories called "
1474 "`.text_html', `.text', etc which will only be "
1475 "shown for files of that type. `.group' is shown "
1476 "only when multiple files are selected."),
1477 dirs->str,
1478 save ? _("I'll show you your SendTo directory now; you should "
1479 "symlink (Ctrl+Shift drag) any applications you want "
1480 "into it.")
1481 : _("Your CHOICESPATH variable setting prevents "
1482 "customisations - sorry."));
1484 g_string_free(dirs, TRUE);
1486 if (save)
1487 filer_opendir(save, NULL, NULL);
1490 static void customise_new(gpointer data)
1492 GPtrArray *path;
1493 guchar *save;
1494 GString *dirs;
1495 int i;
1497 dirs = g_string_new(NULL);
1499 path = choices_list_xdg_dirs("", SITE);
1500 for (i = 0; i < path->len; i++)
1502 guchar *old = (guchar *) path->pdata[i];
1504 g_string_append(dirs, old);
1505 g_string_append(dirs, "/Templates\n");
1507 choices_free_list(path);
1509 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1510 if (save)
1511 mkdir(save, 0777);
1513 info_message(
1514 _("Any files placed in your Templates directories will "
1515 "appear on the `New' menu. Choosing one of them will make "
1516 "a copy of it as the new file.\n\n"
1517 "The following directories contain templates:\n\n%s\n%s\n"),
1518 dirs->str,
1519 save ? _("I'll show you your Templates directory now; you "
1520 "should place any template files you want inside it.")
1521 : _("Your CHOICESPATH variable setting prevents "
1522 "customisations - sorry."));
1524 g_string_free(dirs, TRUE);
1526 if (save)
1527 filer_opendir(save, NULL, NULL);
1530 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1531 * to the menu.
1533 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1535 gchar *searchdir;
1536 GPtrArray *paths;
1537 int i;
1539 if (subtype)
1540 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1541 else if (type)
1542 searchdir = g_strdup_printf("SendTo/.%s", type);
1543 else
1544 searchdir = g_strdup("SendTo");
1546 paths = choices_list_xdg_dirs(searchdir, SITE);
1547 g_free(searchdir);
1549 for (i = 0; i < paths->len; i++)
1551 GList *widgets = NULL;
1552 guchar *dir = (guchar *) paths->pdata[i];
1554 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1555 (CallbackFn) do_send_to,
1556 FALSE, FALSE, TRUE);
1558 if (widgets)
1559 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1560 gtk_menu_item_new());
1562 g_list_free(widgets); /* TODO: Get rid of this */
1565 choices_free_list(paths);
1568 /* Scan the SendTo dir and create and show the Send To menu.
1569 * The 'paths' list and every path in it is claimed, and will be
1570 * freed later -- don't free it yourself!
1572 static void show_send_to_menu(GList *paths, GdkEvent *event)
1574 GtkWidget *menu, *item;
1576 menu = gtk_menu_new();
1578 if (g_list_length(paths) == 1)
1580 DirItem *item;
1582 item = diritem_new("");
1583 diritem_restat(paths->data, item, NULL);
1585 add_sendto(menu,
1586 item->mime_type->media_type,
1587 item->mime_type->subtype);
1589 add_sendto(menu, item->mime_type->media_type, NULL);
1591 diritem_free(item);
1593 else
1595 GList *rover;
1596 gboolean same=TRUE, same_media=TRUE;
1597 MIME_type *type=NULL;
1598 DirItem *item;
1600 item = diritem_new("");
1601 for(rover=paths; rover; rover=g_list_next(rover))
1603 diritem_restat(rover->data, item, NULL);
1604 if(!type)
1605 type=item->mime_type;
1606 else
1608 if(type!=item->mime_type)
1610 same=FALSE;
1611 if(strcmp(type->media_type,
1612 item->mime_type->media_type)!=0)
1614 same_media=FALSE;
1615 break;
1620 diritem_free(item);
1622 if(type)
1624 if(same)
1625 add_sendto(menu, type->media_type,
1626 type->subtype);
1627 if(same_media)
1628 add_sendto(menu, type->media_type, NULL);
1631 add_sendto(menu, "group", NULL);
1634 add_sendto(menu, NULL, NULL);
1636 item = gtk_menu_item_new_with_label(_("Customise"));
1637 g_signal_connect_swapped(item, "activate",
1638 G_CALLBACK(customise_send_to), NULL);
1639 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1641 if (send_to_paths)
1642 destroy_glist(&send_to_paths);
1644 send_to_paths = paths;
1646 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1648 popup_menu = menu;
1649 show_popup_menu(menu, event, 0);
1652 static void send_to(FilerWindow *filer_window)
1654 GList *paths;
1655 GdkEvent *event;
1657 paths = filer_selected_items(filer_window);
1658 event = gtk_get_current_event();
1660 /* Eats paths */
1661 show_send_to_menu(paths, event);
1663 if (event)
1664 gdk_event_free(event);
1667 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1669 const char *argv[] = {"sh", "-c", NULL, NULL};
1670 gboolean close = action;
1672 argv[2] = o_menu_xterm.value;
1674 g_return_if_fail(window_with_focus != NULL);
1676 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1677 gtk_widget_destroy(window_with_focus->window);
1680 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1682 g_return_if_fail(window_with_focus != NULL);
1684 filer_change_to(window_with_focus, home_dir, NULL);
1687 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1689 g_return_if_fail(window_with_focus != NULL);
1691 bookmarks_show_menu(window_with_focus);
1694 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1696 g_return_if_fail(window_with_focus != NULL);
1698 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1699 filer_change_to(window_with_focus,
1700 window_with_focus->real_path, NULL);
1701 else
1702 delayed_error(_("This is already the canonical name "
1703 "for this directory."));
1706 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1708 g_return_if_fail(window_with_focus != NULL);
1710 filer_open_parent(window_with_focus);
1713 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1715 g_return_if_fail(window_with_focus != NULL);
1717 change_to_parent(window_with_focus);
1720 static void resize(gpointer data, guint action, GtkWidget *widget)
1722 g_return_if_fail(window_with_focus != NULL);
1724 view_autosize(window_with_focus->view);
1727 static void new_window(gpointer data, guint action, GtkWidget *widget)
1729 g_return_if_fail(window_with_focus != NULL);
1731 if (o_unique_filer_windows.int_value)
1733 report_error(_("You can't open a second view onto "
1734 "this directory because the `Unique Windows' option "
1735 "is turned on in the Options window."));
1737 else
1738 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1741 static void close_window(gpointer data, guint action, GtkWidget *widget)
1743 g_return_if_fail(window_with_focus != NULL);
1745 if (!filer_window_delete(window_with_focus->window, NULL,
1746 window_with_focus))
1747 gtk_widget_destroy(window_with_focus->window);
1750 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1752 MiniType type = (MiniType) action;
1754 g_return_if_fail(window_with_focus != NULL);
1756 /* Item needs to remain selected... */
1757 if (type == MINI_SHELL)
1758 window_with_focus->temp_item_selected = FALSE;
1760 minibuffer_show(window_with_focus, type);
1763 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1765 if (action == HELP_ABOUT)
1766 infobox_new(app_dir);
1767 else if (action == HELP_DIR)
1768 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1769 else if (action == HELP_MANUAL)
1771 gchar *manual = NULL;
1773 if (current_lang)
1775 manual = g_strconcat(app_dir, "/Help/Manual-",
1776 current_lang, ".html", NULL);
1777 if (!file_exists(manual) && strchr(current_lang, '_'))
1779 /* Try again without the territory */
1780 strcpy(strrchr(manual, '_'), ".html");
1782 if (!file_exists(manual))
1783 null_g_free(&manual);
1786 if (!manual)
1787 manual = g_strconcat(app_dir,
1788 "/Help/Manual.html", NULL);
1790 run_by_path(manual);
1792 g_free(manual);
1794 else
1795 g_warning("Unknown help action %d\n", action);
1798 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1799 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1801 GList *items, *item;
1803 items = gtk_container_get_children(GTK_CONTAINER(menu));
1805 item = g_list_nth(items, from);
1806 while (item && n--)
1808 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1809 item = item->next;
1811 g_list_free(items);
1814 static void save_menus(void)
1816 char *menurc;
1818 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1819 if (menurc)
1821 gtk_accel_map_save(menurc);
1822 g_free(menurc);
1826 static void select_nth_item(GtkMenuShell *shell, int n)
1828 GList *items;
1829 GtkWidget *item;
1831 items = gtk_container_get_children(GTK_CONTAINER(shell));
1832 item = g_list_nth_data(items, n);
1834 g_return_if_fail(item != NULL);
1836 g_list_free(items);
1838 gtk_menu_shell_select_item(shell, item);
1841 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1843 DirItem *item;
1844 const guchar *path;
1845 int n_selected;
1846 ViewIter iter;
1848 g_return_if_fail(window_with_focus != NULL);
1850 n_selected = view_count_selected(window_with_focus->view);
1852 if (n_selected < 1)
1854 const char *prompt;
1856 switch (action)
1858 case FILE_COPY_ITEM:
1859 prompt = _("Copy ... ?");
1860 break;
1861 case FILE_RENAME_ITEM:
1862 prompt = _("Rename ... ?");
1863 break;
1864 case FILE_LINK_ITEM:
1865 prompt = _("Symlink ... ?");
1866 break;
1867 case FILE_OPEN_FILE:
1868 prompt = _("Shift Open ... ?");
1869 break;
1870 case FILE_PROPERTIES:
1871 prompt = _("Properties of ... ?");
1872 break;
1873 case FILE_SET_TYPE:
1874 prompt = _("Set type of ... ?");
1875 break;
1876 case FILE_RUN_ACTION:
1877 prompt = _("Set run action for ... ?");
1878 break;
1879 case FILE_SET_ICON:
1880 prompt = _("Set icon for ... ?");
1881 break;
1882 case FILE_SEND_TO:
1883 prompt = _("Send ... to ... ?");
1884 break;
1885 case FILE_DELETE:
1886 prompt = _("DELETE ... ?");
1887 break;
1888 case FILE_USAGE:
1889 prompt = _("Count the size of ... ?");
1890 break;
1891 case FILE_CHMOD_ITEMS:
1892 prompt = _("Set permissions on ... ?");
1893 break;
1894 case FILE_FIND:
1895 prompt = _("Search inside ... ?");
1896 break;
1897 default:
1898 g_warning("Unknown action!");
1899 return;
1901 filer_target_mode(window_with_focus, target_callback,
1902 GINT_TO_POINTER(action), prompt);
1903 return;
1906 switch (action)
1908 case FILE_SEND_TO:
1909 send_to(window_with_focus);
1910 return;
1911 case FILE_DELETE:
1912 delete(window_with_focus);
1913 return;
1914 case FILE_USAGE:
1915 usage(window_with_focus);
1916 return;
1917 case FILE_CHMOD_ITEMS:
1918 chmod_items(window_with_focus);
1919 return;
1920 case FILE_SET_TYPE:
1921 set_type_items(window_with_focus);
1922 return;
1923 case FILE_FIND:
1924 find(window_with_focus);
1925 return;
1926 case FILE_PROPERTIES:
1928 GList *items;
1930 items = filer_selected_items(window_with_focus);
1931 infobox_show_list(items);
1932 destroy_glist(&items);
1933 return;
1935 case FILE_RENAME_ITEM:
1936 if (n_selected > 1)
1938 GList *items = NULL;
1939 ViewIter iter;
1941 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1942 while ((item = iter.next(&iter)))
1943 items = g_list_prepend(items, item->leafname);
1944 items = g_list_reverse(items);
1946 bulk_rename(window_with_focus->sym_path, items);
1947 g_list_free(items);
1948 return;
1950 break; /* Not a bulk rename... see below */
1951 default:
1952 break;
1955 /* All the following actions require exactly one file selected */
1957 if (n_selected > 1)
1959 report_error(_("You cannot do this to more than "
1960 "one item at a time"));
1961 return;
1964 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1966 item = iter.next(&iter);
1967 g_return_if_fail(item != NULL);
1968 /* iter may be passed to filer_openitem... */
1970 if (item->base_type == TYPE_UNKNOWN)
1971 item = dir_update_item(window_with_focus->directory,
1972 item->leafname);
1974 if (!item)
1976 report_error(_("Item no longer exists!"));
1977 return;
1980 path = make_path(window_with_focus->sym_path, item->leafname);
1982 switch (action)
1984 case FILE_COPY_ITEM:
1985 src_dest_action_item(path, di_image(item),
1986 _("Copy"), copy_cb,
1987 GDK_ACTION_COPY);
1988 break;
1989 case FILE_RENAME_ITEM:
1990 src_dest_action_item(path, di_image(item),
1991 _("Rename"), rename_cb,
1992 GDK_ACTION_MOVE);
1993 break;
1994 case FILE_LINK_ITEM:
1995 src_dest_action_item(path, di_image(item),
1996 _("Symlink"), link_cb,
1997 GDK_ACTION_LINK);
1998 break;
1999 case FILE_OPEN_FILE:
2000 filer_openitem(window_with_focus, &iter,
2001 OPEN_SAME_WINDOW | OPEN_SHIFT);
2002 break;
2003 case FILE_RUN_ACTION:
2004 run_action(item);
2005 break;
2006 case FILE_SET_ICON:
2007 icon_set_handler_dialog(item, path);
2008 break;
2009 default:
2010 g_warning("Unknown action!");
2011 return;
2015 static void show_key_help(GtkWidget *button, gpointer data)
2017 gboolean can_change_accels;
2019 g_object_get(G_OBJECT(gtk_settings_get_default()),
2020 "gtk-can-change-accels", &can_change_accels,
2021 NULL);
2023 if (!can_change_accels)
2025 info_message(_("User-definable shortcuts are disabled by "
2026 "default in Gtk2, and you have not enabled "
2027 "them. You can turn this feature on by:\n\n"
2028 "1) using an XSettings manager, such as ROX-Session "
2029 "or gnome-settings-daemon, or\n\n"
2030 "2) adding this line to ~/.gtkrc-2.0:\n"
2031 "\tgtk-can-change-accels = 1\n"
2032 "\t(this only works if NOT using XSETTINGS)"));
2033 return;
2036 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2037 "- Open the menu over a filer window,\n"
2038 "- Move the pointer over the item you want to use,\n"
2039 "- Press the key you want attached to it.\n\n"
2040 "The key will appear next to the menu item and you can just press "
2041 "that key without opening the menu in future."));
2044 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2046 GtkWidget *button, *align;
2048 g_return_val_if_fail(option == NULL, NULL);
2050 align = gtk_alignment_new(0, 0.5, 0, 0);
2051 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2052 gtk_container_add(GTK_CONTAINER(align), button);
2053 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2055 return g_list_append(NULL, align);