r4240: Removed 'Open AVFS' from menu. It doesn't do anything on most systems, and you
[rox-filer.git] / ROX-Filer / src / menu.c
blobb3b511c8e5c11fbf1f2fd184f4beba1256110727
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2005, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 /* menu.c - code for handling the popup menus */
24 #include "config.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/wait.h>
29 #include <sys/param.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <dirent.h>
35 #include <gtk/gtk.h>
37 #include "global.h"
39 #include "menu.h"
40 #include "run.h"
41 #include "action.h"
42 #include "filer.h"
43 #include "pixmaps.h"
44 #include "type.h"
45 #include "support.h"
46 #include "gui_support.h"
47 #include "options.h"
48 #include "choices.h"
49 #include "gtksavebox.h"
50 #include "mount.h"
51 #include "minibuffer.h"
52 #include "i18n.h"
53 #include "main.h"
54 #include "pinboard.h"
55 #include "dir.h"
56 #include "diritem.h"
57 #include "appmenu.h"
58 #include "usericons.h"
59 #include "infobox.h"
60 #include "view_iface.h"
61 #include "display.h"
62 #include "bookmarks.h"
63 #include "panel.h"
64 #include "bulk_rename.h"
65 #include "xtypes.h"
67 typedef enum {
68 FILE_COPY_ITEM,
69 FILE_RENAME_ITEM,
70 FILE_LINK_ITEM,
71 FILE_OPEN_FILE,
72 FILE_PROPERTIES,
73 FILE_RUN_ACTION,
74 FILE_SET_ICON,
75 FILE_SEND_TO,
76 FILE_DELETE,
77 FILE_USAGE,
78 FILE_CHMOD_ITEMS,
79 FILE_FIND,
80 FILE_SET_TYPE,
81 } FileOp;
83 typedef void (*ActionFn)(GList *paths,
84 const char *dest_dir, const char *leaf, int quiet);
85 typedef void MenuCallback(GtkWidget *widget, gpointer data);
87 typedef gboolean (*SaveCb)(GObject *savebox,
88 const gchar *current, const gchar *new);
90 GtkAccelGroup *filer_keys = NULL;
91 static gboolean filer_keys_need_init = TRUE;
93 static GtkWidget *popup_menu = NULL; /* Currently open menu */
95 static gint updating_menu = 0; /* Non-zero => ignore activations */
96 static GList *send_to_paths = NULL;
98 static Option o_menu_iconsize, o_menu_xterm, o_menu_quick;
100 /* Static prototypes */
102 static void save_menus(void);
103 static void menu_closed(GtkWidget *widget);
104 static void shade_file_menu_items(gboolean shaded);
105 static void savebox_show(const gchar *action, const gchar *path,
106 MaskedPixmap *image, SaveCb callback,
107 GdkDragAction dnd_action);
108 static gint save_to_file(GObject *savebox,
109 const gchar *pathname, gpointer data);
110 static gboolean action_with_leaf(ActionFn action,
111 const gchar *current, const gchar *new);
112 static gboolean link_cb(GObject *savebox,
113 const gchar *initial, const gchar *path);
114 static void select_nth_item(GtkMenuShell *shell, int n);
115 static void new_file_type(gchar *templ);
116 static void do_send_to(gchar *templ);
117 static void show_send_to_menu(GList *paths, GdkEvent *event);
118 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
120 /* Note that for most of these callbacks none of the arguments are used. */
122 static void view_type(gpointer data, guint action, GtkWidget *widget);
124 /* (action used in these three - DetailsType) */
125 static void change_size(gpointer data, guint action, GtkWidget *widget);
126 static void change_size_auto(gpointer data, guint action, GtkWidget *widget);
127 static void set_with(gpointer data, guint action, GtkWidget *widget);
129 static void set_sort(gpointer data, guint action, GtkWidget *widget);
130 static void reverse_sort(gpointer data, guint action, GtkWidget *widget);
132 static void hidden(gpointer data, guint action, GtkWidget *widget);
133 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
134 static void refresh(gpointer data, guint action, GtkWidget *widget);
135 static void save_settings(gpointer data, guint action, GtkWidget *widget);
137 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
139 static void select_all(gpointer data, guint action, GtkWidget *widget);
140 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
141 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
142 static void new_directory(gpointer data, guint action, GtkWidget *widget);
143 static void new_file(gpointer data, guint action, GtkWidget *widget);
144 static void customise_new(gpointer data);
145 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent(gpointer data, guint action, GtkWidget *widget);
149 static void home_directory(gpointer data, guint action, GtkWidget *widget);
150 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
151 static void new_window(gpointer data, guint action, GtkWidget *widget);
152 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
153 static void close_window(gpointer data, guint action, GtkWidget *widget);
154 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
156 /* (action used in this - MiniType) */
157 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
158 static void resize(gpointer data, guint action, GtkWidget *widget);
160 #define MENUS_NAME "menus2"
162 static GtkWidget *filer_menu; /* The popup filer menu */
163 static GtkWidget *filer_file_item; /* The File '' label */
164 static GtkWidget *filer_file_menu; /* The File '' menu */
165 static GtkWidget *file_shift_item; /* Shift Open label */
166 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
167 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
168 static GtkWidget *filer_reverse_menu; /* The Reversed item */
169 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
170 static GtkWidget *filer_new_window; /* The New Window item */
171 static GtkWidget *filer_new_menu; /* The New submenu */
172 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
173 static GtkWidget *filer_set_type; /* Set type item */
175 #undef N_
176 #define N_(x) x
178 static GtkItemFactoryEntry filer_menu_def[] = {
179 {N_("Display"), NULL, NULL, 0, "<Branch>"},
180 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
181 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
182 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
183 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
184 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
185 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
186 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
187 {">", NULL, NULL, 0, "<Separator>"},
188 {">" N_("Bigger Icons"), "equal", change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
189 {">" N_("Smaller Icons"), "minus", change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
190 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
191 {">", NULL, NULL, 0, "<Separator>"},
192 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
193 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
194 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
195 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
196 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
197 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
198 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
199 {">", NULL, NULL, 0, "<Separator>"},
200 {">" N_("Show Hidden"), "<Ctrl>H", hidden, 0, "<ToggleItem>"},
201 {">" N_("Filter Files..."), NULL, mini_buffer, MINI_FILTER, NULL},
202 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
203 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
204 {">" N_("Save 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 show_thumbs(gpointer data, guint action, GtkWidget *widget)
970 if (updating_menu)
971 return;
973 g_return_if_fail(window_with_focus != NULL);
975 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
978 static void refresh(gpointer data, guint action, GtkWidget *widget)
980 g_return_if_fail(window_with_focus != NULL);
982 filer_refresh(window_with_focus);
985 static void save_settings(gpointer data, guint action, GtkWidget *widget)
987 g_return_if_fail(window_with_focus != NULL);
989 filer_save_settings(window_with_focus);
992 static void delete(FilerWindow *filer_window)
994 GList *paths;
995 paths = filer_selected_items(filer_window);
996 action_delete(paths);
997 destroy_glist(&paths);
1000 static void usage(FilerWindow *filer_window)
1002 GList *paths;
1003 paths = filer_selected_items(filer_window);
1004 action_usage(paths);
1005 destroy_glist(&paths);
1008 static void chmod_items(FilerWindow *filer_window)
1010 GList *paths;
1011 paths = filer_selected_items(filer_window);
1012 action_chmod(paths, FALSE, NULL);
1013 destroy_glist(&paths);
1016 static void set_type_items(FilerWindow *filer_window)
1018 GList *paths, *p;
1019 int npass=0, nfail=0;
1021 paths = filer_selected_items(filer_window);
1022 for(p=paths; p; p=g_list_next(p)) {
1023 if(xattr_supported((const char *) p->data))
1024 npass++;
1025 else
1026 nfail++;
1028 if(npass==0)
1029 report_error(_("Extended attributes, used to store types, are not supported for this "
1030 "file or files.\n"
1031 "This may be due to lack of support from the filesystem or the C library, "
1032 "or it may simply be that the filesystem needs to be mounted with "
1033 "the right mount option ('user_xattr' on Linux)."));
1034 else if(nfail>0)
1035 report_error(_("Setting type not supported for some of these files"));
1036 if(npass>0)
1037 action_settype(paths, FALSE, NULL);
1038 destroy_glist(&paths);
1041 static void find(FilerWindow *filer_window)
1043 GList *paths;
1044 paths = filer_selected_items(filer_window);
1045 action_find(paths);
1046 destroy_glist(&paths);
1049 /* This creates a new savebox widget, and allows the user to pick a new path
1050 * for the file.
1051 * Once the new path has been picked, the callback will be called with
1052 * both the current and new paths.
1053 * NOTE: This function unrefs 'image'!
1055 static void savebox_show(const gchar *action, const gchar *path,
1056 MaskedPixmap *image, SaveCb callback,
1057 GdkDragAction dnd_action)
1059 GtkWidget *savebox = NULL;
1060 GtkWidget *check_relative = NULL;
1062 g_return_if_fail(image != NULL);
1064 savebox = gtk_savebox_new(action);
1065 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1067 if (callback == link_cb)
1069 check_relative = gtk_check_button_new_with_mnemonic(
1070 _("_Relative link"));
1071 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1072 TRUE);
1074 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1075 gtk_tooltips_set_tip(tooltips, check_relative,
1076 _("If on, the symlink will store the path from the "
1077 "symlink to the target file. Use this if the symlink "
1078 "and the target will be moved together.\n"
1079 "If off, the path from the root directory is stored - "
1080 "use this if the symlink may move but the target will "
1081 "stay put."), NULL);
1082 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1083 check_relative, FALSE, TRUE, 0);
1084 gtk_widget_show(check_relative);
1087 g_signal_connect(savebox, "save_to_file",
1088 G_CALLBACK(save_to_file), NULL);
1090 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1091 g_strdup(path), g_free);
1092 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1093 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1095 gtk_window_set_title(GTK_WINDOW(savebox), action);
1097 if (g_utf8_validate(path, -1, NULL))
1098 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1099 else
1101 gchar *u8, *dir;
1102 dir = g_path_get_dirname(path);
1103 u8 = to_utf8(g_basename(path));
1104 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1105 make_path(dir, u8));
1106 g_free(u8);
1107 g_free(dir);
1109 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1110 g_object_unref(image);
1112 gtk_widget_show(savebox);
1115 static gint save_to_file(GObject *savebox,
1116 const gchar *pathname, gpointer data)
1118 SaveCb callback;
1119 const gchar *current_path;
1121 callback = g_object_get_data(savebox, "action_callback");
1122 current_path = g_object_get_data(savebox, "current_path");
1124 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1125 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1127 return callback(savebox, current_path, pathname)
1128 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1131 static gboolean copy_cb(GObject *savebox,
1132 const gchar *current, const gchar *new)
1134 return action_with_leaf(action_copy, current, new);
1137 static gboolean action_with_leaf(ActionFn action,
1138 const gchar *current, const gchar *new)
1140 const char *leaf;
1141 char *new_dir;
1142 GList *local_paths;
1144 if (new[0] != '/')
1146 report_error(_("New pathname is not absolute"));
1147 return FALSE;
1150 if (new[strlen(new) - 1] == '/')
1152 new_dir = g_strdup(new);
1153 leaf = NULL;
1155 else
1157 const gchar *slash;
1159 slash = strrchr(new, '/');
1160 new_dir = g_strndup(new, slash - new);
1161 leaf = slash + 1;
1164 local_paths = g_list_append(NULL, (gchar *) current);
1165 action(local_paths, new_dir, leaf, -1);
1166 g_list_free(local_paths);
1168 g_free(new_dir);
1170 return TRUE;
1173 /* Open a savebox to act on the selected file.
1174 * Call 'callback' later to perform the operation.
1176 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1177 const gchar *action, SaveCb callback,
1178 GdkDragAction dnd_action)
1180 g_object_ref(image);
1181 savebox_show(action, path, image, callback, dnd_action);
1184 static gboolean rename_cb(GObject *savebox,
1185 const gchar *current, const gchar *new)
1187 return action_with_leaf(action_move, current, new);
1190 static gboolean link_cb(GObject *savebox,
1191 const gchar *initial, const gchar *path)
1193 GtkToggleButton *check_relative;
1194 struct stat info;
1195 int err;
1196 gchar *link_path;
1198 check_relative = g_object_get_data(savebox, "check_relative");
1200 if (gtk_toggle_button_get_active(check_relative))
1201 link_path = get_relative_path(path, initial);
1202 else
1203 link_path = g_strdup(initial);
1205 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1207 GtkWidget *box, *button;
1208 gint ans;
1210 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1211 GTK_BUTTONS_CANCEL,
1212 _("Symlink from '%s' already exists. "
1213 "Replace it with a link to '%s'?"),
1214 path, link_path);
1216 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1218 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1219 gtk_widget_show(button);
1220 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1221 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1222 button, GTK_RESPONSE_OK);
1223 gtk_dialog_set_default_response(GTK_DIALOG(box),
1224 GTK_RESPONSE_OK);
1226 ans = gtk_dialog_run(GTK_DIALOG(box));
1227 gtk_widget_destroy(box);
1229 if (ans != GTK_RESPONSE_OK)
1231 g_free(link_path);
1232 return FALSE;
1235 unlink(path);
1238 err = symlink(link_path, path);
1239 g_free(link_path);
1241 if (err)
1243 report_error("symlink: %s", g_strerror(errno));
1244 return FALSE;
1247 dir_check_this(path);
1249 return TRUE;
1252 static void run_action(DirItem *item)
1254 if (can_set_run_action(item))
1255 type_set_handler_dialog(item->mime_type);
1256 else
1257 report_error(
1258 _("You can only set the run action for a "
1259 "regular file"));
1262 void open_home(gpointer data, guint action, GtkWidget *widget)
1264 filer_opendir(home_dir, NULL, NULL);
1267 static void select_all(gpointer data, guint action, GtkWidget *widget)
1269 g_return_if_fail(window_with_focus != NULL);
1271 window_with_focus->temp_item_selected = FALSE;
1272 view_select_all(window_with_focus->view);
1275 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1277 g_return_if_fail(window_with_focus != NULL);
1279 window_with_focus->temp_item_selected = FALSE;
1280 view_clear_selection(window_with_focus->view);
1283 static gboolean invert_cb(ViewIter *iter, gpointer data)
1285 return !view_get_selected((ViewIface *) data, iter);
1288 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1290 g_return_if_fail(window_with_focus != NULL);
1292 window_with_focus->temp_item_selected = FALSE;
1294 view_select_if(window_with_focus->view, invert_cb,
1295 window_with_focus->view);
1298 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1300 GtkWidget *win;
1302 win = options_show();
1304 if (win)
1306 number_of_windows++;
1307 g_signal_connect(win, "destroy",
1308 G_CALLBACK(one_less_window), NULL);
1312 static gboolean new_directory_cb(GObject *savebox,
1313 const gchar *initial, const gchar *path)
1315 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1317 report_error("mkdir: %s", g_strerror(errno));
1318 return FALSE;
1321 dir_check_this(path);
1323 if (filer_exists(window_with_focus))
1325 guchar *leaf;
1326 leaf = strrchr(path, '/');
1327 if (leaf)
1328 display_set_autoselect(window_with_focus, leaf + 1);
1331 return TRUE;
1334 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1336 g_return_if_fail(window_with_focus != NULL);
1338 savebox_show(_("Create"),
1339 make_path(window_with_focus->sym_path, _("NewDir")),
1340 type_to_icon(inode_directory), new_directory_cb,
1341 GDK_ACTION_COPY);
1344 static gboolean new_file_cb(GObject *savebox,
1345 const gchar *initial, const gchar *path)
1347 int fd;
1349 fd = open(path, O_CREAT | O_EXCL, 0666);
1351 if (fd == -1)
1353 report_error(_("Error creating '%s': %s"),
1354 path, g_strerror(errno));
1355 return FALSE;
1358 if (close(fd))
1359 report_error(_("Error creating '%s': %s"),
1360 path, g_strerror(errno));
1362 dir_check_this(path);
1364 if (filer_exists(window_with_focus))
1366 guchar *leaf;
1367 leaf = strrchr(path, '/');
1368 if (leaf)
1369 display_set_autoselect(window_with_focus, leaf + 1);
1372 return TRUE;
1375 static void new_file(gpointer data, guint action, GtkWidget *widget)
1377 g_return_if_fail(window_with_focus != NULL);
1379 savebox_show(_("Create"),
1380 make_path(window_with_focus->sym_path, _("NewFile")),
1381 type_to_icon(text_plain),
1382 new_file_cb, GDK_ACTION_COPY);
1385 static gboolean new_file_type_cb(GObject *savebox,
1386 const gchar *initial, const gchar *path)
1388 const gchar *oleaf, *leaf;
1389 gchar *templ, *templ_dname, *dest;
1390 GList *paths;
1392 /* We can work out the template path from the initial name */
1393 oleaf = g_basename(initial);
1394 templ_dname = choices_find_xdg_path_load("Templates", "", SITE);
1395 if (!templ_dname)
1397 report_error(
1398 _("Error creating file: could not find the template for %s"),
1399 oleaf);
1400 return FALSE;
1403 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1404 g_free(templ_dname);
1406 dest = g_path_get_dirname(path);
1407 leaf = g_basename(path);
1408 paths = g_list_append(NULL, templ);
1410 action_copy(paths, dest, leaf, TRUE);
1412 g_list_free(paths);
1413 g_free(dest);
1414 g_free(templ);
1416 if (filer_exists(window_with_focus))
1417 display_set_autoselect(window_with_focus, leaf);
1419 return TRUE;
1422 static void do_send_to(gchar *templ)
1424 g_return_if_fail(send_to_paths != NULL);
1426 run_with_files(templ, send_to_paths);
1429 static void new_file_type(gchar *templ)
1431 const gchar *leaf;
1432 MIME_type *type;
1434 g_return_if_fail(window_with_focus != NULL);
1436 leaf = g_basename(templ);
1437 type = type_get_type(templ);
1439 savebox_show(_("Create"),
1440 make_path(window_with_focus->sym_path, leaf),
1441 type_to_icon(type),
1442 new_file_type_cb, GDK_ACTION_COPY);
1445 static void customise_send_to(gpointer data)
1447 GPtrArray *path;
1448 guchar *save;
1449 GString *dirs;
1450 int i;
1452 dirs = g_string_new(NULL);
1454 path = choices_list_xdg_dirs("", SITE);
1455 for (i = 0; i < path->len; i++)
1457 guchar *old = (guchar *) path->pdata[i];
1459 g_string_append(dirs, old);
1460 g_string_append(dirs, "/SendTo\n");
1462 choices_free_list(path);
1464 save = choices_find_xdg_path_save("", "SendTo", SITE, TRUE);
1465 if (save)
1466 mkdir(save, 0777);
1468 info_message(
1469 _("The `Send To' menu provides a quick way to send some files "
1470 "to an application. The applications listed are those in "
1471 "the following directories:\n\n%s\n%s\n"
1472 "The `Send To' menu may be opened by Shift+Menu clicking "
1473 "over a file.\n\n"
1474 "Advanced use:\n"
1475 "You can also create subdirectories called "
1476 "`.text_html', `.text', etc which will only be "
1477 "shown for files of that type. `.group' is shown "
1478 "only when multiple files are selected."),
1479 dirs->str,
1480 save ? _("I'll show you your SendTo directory now; you should "
1481 "symlink (Ctrl+Shift drag) any applications you want "
1482 "into it.")
1483 : _("Your CHOICESPATH variable setting prevents "
1484 "customisations - sorry."));
1486 g_string_free(dirs, TRUE);
1488 if (save)
1489 filer_opendir(save, NULL, NULL);
1492 static void customise_new(gpointer data)
1494 GPtrArray *path;
1495 guchar *save;
1496 GString *dirs;
1497 int i;
1499 dirs = g_string_new(NULL);
1501 path = choices_list_xdg_dirs("", SITE);
1502 for (i = 0; i < path->len; i++)
1504 guchar *old = (guchar *) path->pdata[i];
1506 g_string_append(dirs, old);
1507 g_string_append(dirs, "/Templates\n");
1509 choices_free_list(path);
1511 save = choices_find_xdg_path_save("", "Templates", SITE, TRUE);
1512 if (save)
1513 mkdir(save, 0777);
1515 info_message(
1516 _("Any files placed in your Templates directories will "
1517 "appear on the `New' menu. Choosing one of them will make "
1518 "a copy of it as the new file.\n\n"
1519 "The following directories contain templates:\n\n%s\n%s\n"),
1520 dirs->str,
1521 save ? _("I'll show you your Templates directory now; you "
1522 "should place any template files you want inside it.")
1523 : _("Your CHOICESPATH variable setting prevents "
1524 "customisations - sorry."));
1526 g_string_free(dirs, TRUE);
1528 if (save)
1529 filer_opendir(save, NULL, NULL);
1532 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1533 * to the menu.
1535 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1537 gchar *searchdir;
1538 GPtrArray *paths;
1539 int i;
1541 if (subtype)
1542 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1543 else if (type)
1544 searchdir = g_strdup_printf("SendTo/.%s", type);
1545 else
1546 searchdir = g_strdup("SendTo");
1548 paths = choices_list_xdg_dirs(searchdir, SITE);
1549 g_free(searchdir);
1551 for (i = 0; i < paths->len; i++)
1553 GList *widgets = NULL;
1554 guchar *dir = (guchar *) paths->pdata[i];
1556 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1557 (CallbackFn) do_send_to,
1558 FALSE, FALSE, TRUE);
1560 if (widgets)
1561 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1562 gtk_menu_item_new());
1564 g_list_free(widgets); /* TODO: Get rid of this */
1567 choices_free_list(paths);
1570 /* Scan the SendTo dir and create and show the Send To menu.
1571 * The 'paths' list and every path in it is claimed, and will be
1572 * freed later -- don't free it yourself!
1574 static void show_send_to_menu(GList *paths, GdkEvent *event)
1576 GtkWidget *menu, *item;
1578 menu = gtk_menu_new();
1580 if (g_list_length(paths) == 1)
1582 DirItem *item;
1584 item = diritem_new("");
1585 diritem_restat(paths->data, item, NULL);
1587 add_sendto(menu,
1588 item->mime_type->media_type,
1589 item->mime_type->subtype);
1591 add_sendto(menu, item->mime_type->media_type, NULL);
1593 diritem_free(item);
1595 else
1597 GList *rover;
1598 gboolean same=TRUE, same_media=TRUE;
1599 MIME_type *type=NULL;
1600 DirItem *item;
1602 item = diritem_new("");
1603 for(rover=paths; rover; rover=g_list_next(rover))
1605 diritem_restat(rover->data, item, NULL);
1606 if(!type)
1607 type=item->mime_type;
1608 else
1610 if(type!=item->mime_type)
1612 same=FALSE;
1613 if(strcmp(type->media_type,
1614 item->mime_type->media_type)!=0)
1616 same_media=FALSE;
1617 break;
1622 diritem_free(item);
1624 if(type)
1626 if(same)
1627 add_sendto(menu, type->media_type,
1628 type->subtype);
1629 if(same_media)
1630 add_sendto(menu, type->media_type, NULL);
1633 add_sendto(menu, "group", NULL);
1636 add_sendto(menu, NULL, NULL);
1638 item = gtk_menu_item_new_with_label(_("Customise"));
1639 g_signal_connect_swapped(item, "activate",
1640 G_CALLBACK(customise_send_to), NULL);
1641 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1643 if (send_to_paths)
1644 destroy_glist(&send_to_paths);
1646 send_to_paths = paths;
1648 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1650 popup_menu = menu;
1651 show_popup_menu(menu, event, 0);
1654 static void send_to(FilerWindow *filer_window)
1656 GList *paths;
1657 GdkEvent *event;
1659 paths = filer_selected_items(filer_window);
1660 event = gtk_get_current_event();
1662 /* Eats paths */
1663 show_send_to_menu(paths, event);
1665 if (event)
1666 gdk_event_free(event);
1669 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1671 const char *argv[] = {"sh", "-c", NULL, NULL};
1672 gboolean close = action;
1674 argv[2] = o_menu_xterm.value;
1676 g_return_if_fail(window_with_focus != NULL);
1678 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1679 gtk_widget_destroy(window_with_focus->window);
1682 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1684 g_return_if_fail(window_with_focus != NULL);
1686 filer_change_to(window_with_focus, home_dir, NULL);
1689 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1691 g_return_if_fail(window_with_focus != NULL);
1693 bookmarks_show_menu(window_with_focus);
1696 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1698 g_return_if_fail(window_with_focus != NULL);
1700 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1701 filer_change_to(window_with_focus,
1702 window_with_focus->real_path, NULL);
1703 else
1704 delayed_error(_("This is already the canonical name "
1705 "for this directory."));
1708 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1710 g_return_if_fail(window_with_focus != NULL);
1712 filer_open_parent(window_with_focus);
1715 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1717 g_return_if_fail(window_with_focus != NULL);
1719 change_to_parent(window_with_focus);
1722 static void resize(gpointer data, guint action, GtkWidget *widget)
1724 g_return_if_fail(window_with_focus != NULL);
1726 view_autosize(window_with_focus->view);
1729 static void new_window(gpointer data, guint action, GtkWidget *widget)
1731 g_return_if_fail(window_with_focus != NULL);
1733 if (o_unique_filer_windows.int_value)
1735 report_error(_("You can't open a second view onto "
1736 "this directory because the `Unique Windows' option "
1737 "is turned on in the Options window."));
1739 else
1740 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1743 static void close_window(gpointer data, guint action, GtkWidget *widget)
1745 g_return_if_fail(window_with_focus != NULL);
1747 if (!filer_window_delete(window_with_focus->window, NULL,
1748 window_with_focus))
1749 gtk_widget_destroy(window_with_focus->window);
1752 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1754 MiniType type = (MiniType) action;
1756 g_return_if_fail(window_with_focus != NULL);
1758 /* Item needs to remain selected... */
1759 if (type == MINI_SHELL)
1760 window_with_focus->temp_item_selected = FALSE;
1762 minibuffer_show(window_with_focus, type);
1765 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1767 if (action == HELP_ABOUT)
1768 infobox_new(app_dir);
1769 else if (action == HELP_DIR)
1770 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1771 else if (action == HELP_MANUAL)
1773 gchar *manual = NULL;
1775 if (current_lang)
1777 manual = g_strconcat(app_dir, "/Help/Manual-",
1778 current_lang, ".html", NULL);
1779 if (!file_exists(manual) && strchr(current_lang, '_'))
1781 /* Try again without the territory */
1782 strcpy(strrchr(manual, '_'), ".html");
1784 if (!file_exists(manual))
1785 null_g_free(&manual);
1788 if (!manual)
1789 manual = g_strconcat(app_dir,
1790 "/Help/Manual.html", NULL);
1792 run_by_path(manual);
1794 g_free(manual);
1796 else
1797 g_warning("Unknown help action %d\n", action);
1800 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1801 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1803 GList *items, *item;
1805 items = gtk_container_get_children(GTK_CONTAINER(menu));
1807 item = g_list_nth(items, from);
1808 while (item && n--)
1810 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1811 item = item->next;
1813 g_list_free(items);
1816 static void save_menus(void)
1818 char *menurc;
1820 menurc = choices_find_xdg_path_save(MENUS_NAME, PROJECT, SITE, TRUE);
1821 if (menurc)
1823 gtk_accel_map_save(menurc);
1824 g_free(menurc);
1828 static void select_nth_item(GtkMenuShell *shell, int n)
1830 GList *items;
1831 GtkWidget *item;
1833 items = gtk_container_get_children(GTK_CONTAINER(shell));
1834 item = g_list_nth_data(items, n);
1836 g_return_if_fail(item != NULL);
1838 g_list_free(items);
1840 gtk_menu_shell_select_item(shell, item);
1843 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1845 DirItem *item;
1846 const guchar *path;
1847 int n_selected;
1848 ViewIter iter;
1850 g_return_if_fail(window_with_focus != NULL);
1852 n_selected = view_count_selected(window_with_focus->view);
1854 if (n_selected < 1)
1856 const char *prompt;
1858 switch (action)
1860 case FILE_COPY_ITEM:
1861 prompt = _("Copy ... ?");
1862 break;
1863 case FILE_RENAME_ITEM:
1864 prompt = _("Rename ... ?");
1865 break;
1866 case FILE_LINK_ITEM:
1867 prompt = _("Symlink ... ?");
1868 break;
1869 case FILE_OPEN_FILE:
1870 prompt = _("Shift Open ... ?");
1871 break;
1872 case FILE_PROPERTIES:
1873 prompt = _("Properties of ... ?");
1874 break;
1875 case FILE_SET_TYPE:
1876 prompt = _("Set type of ... ?");
1877 break;
1878 case FILE_RUN_ACTION:
1879 prompt = _("Set run action for ... ?");
1880 break;
1881 case FILE_SET_ICON:
1882 prompt = _("Set icon for ... ?");
1883 break;
1884 case FILE_SEND_TO:
1885 prompt = _("Send ... to ... ?");
1886 break;
1887 case FILE_DELETE:
1888 prompt = _("DELETE ... ?");
1889 break;
1890 case FILE_USAGE:
1891 prompt = _("Count the size of ... ?");
1892 break;
1893 case FILE_CHMOD_ITEMS:
1894 prompt = _("Set permissions on ... ?");
1895 break;
1896 case FILE_FIND:
1897 prompt = _("Search inside ... ?");
1898 break;
1899 default:
1900 g_warning("Unknown action!");
1901 return;
1903 filer_target_mode(window_with_focus, target_callback,
1904 GINT_TO_POINTER(action), prompt);
1905 return;
1908 switch (action)
1910 case FILE_SEND_TO:
1911 send_to(window_with_focus);
1912 return;
1913 case FILE_DELETE:
1914 delete(window_with_focus);
1915 return;
1916 case FILE_USAGE:
1917 usage(window_with_focus);
1918 return;
1919 case FILE_CHMOD_ITEMS:
1920 chmod_items(window_with_focus);
1921 return;
1922 case FILE_SET_TYPE:
1923 set_type_items(window_with_focus);
1924 return;
1925 case FILE_FIND:
1926 find(window_with_focus);
1927 return;
1928 case FILE_PROPERTIES:
1930 GList *items;
1932 items = filer_selected_items(window_with_focus);
1933 infobox_show_list(items);
1934 destroy_glist(&items);
1935 return;
1937 case FILE_RENAME_ITEM:
1938 if (n_selected > 1)
1940 GList *items = NULL;
1941 ViewIter iter;
1943 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1944 while ((item = iter.next(&iter)))
1945 items = g_list_prepend(items, item->leafname);
1946 items = g_list_reverse(items);
1948 bulk_rename(window_with_focus->sym_path, items);
1949 g_list_free(items);
1950 return;
1952 break; /* Not a bulk rename... see below */
1953 default:
1954 break;
1957 /* All the following actions require exactly one file selected */
1959 if (n_selected > 1)
1961 report_error(_("You cannot do this to more than "
1962 "one item at a time"));
1963 return;
1966 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1968 item = iter.next(&iter);
1969 g_return_if_fail(item != NULL);
1970 /* iter may be passed to filer_openitem... */
1972 if (item->base_type == TYPE_UNKNOWN)
1973 item = dir_update_item(window_with_focus->directory,
1974 item->leafname);
1976 if (!item)
1978 report_error(_("Item no longer exists!"));
1979 return;
1982 path = make_path(window_with_focus->sym_path, item->leafname);
1984 switch (action)
1986 case FILE_COPY_ITEM:
1987 src_dest_action_item(path, di_image(item),
1988 _("Copy"), copy_cb,
1989 GDK_ACTION_COPY);
1990 break;
1991 case FILE_RENAME_ITEM:
1992 src_dest_action_item(path, di_image(item),
1993 _("Rename"), rename_cb,
1994 GDK_ACTION_MOVE);
1995 break;
1996 case FILE_LINK_ITEM:
1997 src_dest_action_item(path, di_image(item),
1998 _("Symlink"), link_cb,
1999 GDK_ACTION_LINK);
2000 break;
2001 case FILE_OPEN_FILE:
2002 filer_openitem(window_with_focus, &iter,
2003 OPEN_SAME_WINDOW | OPEN_SHIFT);
2004 break;
2005 case FILE_RUN_ACTION:
2006 run_action(item);
2007 break;
2008 case FILE_SET_ICON:
2009 icon_set_handler_dialog(item, path);
2010 break;
2011 default:
2012 g_warning("Unknown action!");
2013 return;
2017 static void show_key_help(GtkWidget *button, gpointer data)
2019 gboolean can_change_accels;
2021 g_object_get(G_OBJECT(gtk_settings_get_default()),
2022 "gtk-can-change-accels", &can_change_accels,
2023 NULL);
2025 if (!can_change_accels)
2027 info_message(_("User-definable shortcuts are disabled by "
2028 "default in Gtk2, and you have not enabled "
2029 "them. You can turn this feature on by:\n\n"
2030 "1) using an XSettings manager, such as ROX-Session "
2031 "or gnome-settings-daemon, or\n\n"
2032 "2) adding this line to ~/.gtkrc-2.0:\n"
2033 "\tgtk-can-change-accels = 1\n"
2034 "\t(this only works if NOT using XSETTINGS)"));
2035 return;
2038 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
2039 "- Open the menu over a filer window,\n"
2040 "- Move the pointer over the item you want to use,\n"
2041 "- Press the key you want attached to it.\n\n"
2042 "The key will appear next to the menu item and you can just press "
2043 "that key without opening the menu in future."));
2046 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
2048 GtkWidget *button, *align;
2050 g_return_val_if_fail(option == NULL, NULL);
2052 align = gtk_alignment_new(0, 0.5, 0, 0);
2053 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
2054 gtk_container_add(GTK_CONTAINER(align), button);
2055 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
2057 return g_list_append(NULL, align);