r3353: Changed hidden files code, now supports filtering by name.
[rox-filer.git] / ROX-Filer / src / menu.c
blob0612d92c878cebb70c8b17fd6e2b98073bbd409d
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, 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"
64 typedef enum {
65 FILE_COPY_ITEM,
66 FILE_RENAME_ITEM,
67 FILE_LINK_ITEM,
68 FILE_OPEN_FILE,
69 FILE_PROPERTIES,
70 FILE_RUN_ACTION,
71 FILE_SET_ICON,
72 FILE_SEND_TO,
73 FILE_DELETE,
74 FILE_USAGE,
75 FILE_CHMOD_ITEMS,
76 FILE_FIND,
77 FILE_OPEN_VFS_AVFS,
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;
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 test_filter(gpointer data, guint action, GtkWidget *widget);
132 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
133 static void refresh(gpointer data, guint action, GtkWidget *widget);
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 xterm_here(gpointer data, guint action, GtkWidget *widget);
144 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
145 static void open_parent(gpointer data, guint action, GtkWidget *widget);
146 static void home_directory(gpointer data, guint action, GtkWidget *widget);
147 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
148 static void new_window(gpointer data, guint action, GtkWidget *widget);
149 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
150 static void close_window(gpointer data, guint action, GtkWidget *widget);
151 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
153 /* (action used in this - MiniType) */
154 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
155 static void resize(gpointer data, guint action, GtkWidget *widget);
157 #define MENUS_NAME "menus2"
159 static GtkWidget *filer_menu; /* The popup filer menu */
160 static GtkWidget *filer_file_item; /* The File '' label */
161 static GtkWidget *filer_file_menu; /* The File '' menu */
162 static GtkWidget *file_shift_item; /* Shift Open label */
163 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
164 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
165 static GtkWidget *filer_reverse_menu; /* The Reversed item */
166 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
167 static GtkWidget *filer_new_window; /* The New Window item */
168 static GtkWidget *filer_new_menu; /* The New submenu */
169 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
171 #undef N_
172 #define N_(x) x
174 static GtkItemFactoryEntry filer_menu_def[] = {
175 {N_("Display"), NULL, NULL, 0, "<Branch>"},
176 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
177 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
178 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
179 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
180 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
181 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
182 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
183 {">", NULL, NULL, 0, "<Separator>"},
184 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
185 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
186 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
187 {">", NULL, NULL, 0, "<Separator>"},
188 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
189 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
190 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
191 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
192 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
193 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
194 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
195 {">", NULL, NULL, 0, "<Separator>"},
196 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
197 {">" N_("Filter files"), NULL, mini_buffer, MINI_FILTER, NULL},
198 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
199 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
200 {N_("File"), NULL, NULL, 0, "<Branch>"},
201 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
202 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
203 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
204 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
205 {">", NULL, NULL, 0, "<Separator>"},
206 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
207 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
208 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
209 {">", NULL, NULL, 0, "<Separator>"},
210 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
211 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
212 {">" N_("Properties"), NULL, file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
213 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
214 {">" N_("Set Type..."), NULL, file_op, FILE_SET_TYPE, NULL},
215 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
216 {">", NULL, NULL, 0, "<Separator>"},
217 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
218 {N_("Select"), NULL, NULL, 0, "<Branch>"},
219 {">" N_("Select All"), NULL, select_all, 0, NULL},
220 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
221 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
222 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
223 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
224 {N_("New"), NULL, NULL, 0, "<Branch>"},
225 {">" N_("Directory"), NULL, new_directory, 0, NULL},
226 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
227 {N_("Window"), NULL, NULL, 0, "<Branch>"},
228 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
229 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
230 {">" N_("New Window"), NULL, new_window, 0, NULL},
231 {">" N_("Home Directory"), NULL, home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
232 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
233 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
234 {">" N_("Resize Window"), NULL, resize, 0, NULL},
235 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
237 {">" N_("Close Window"), NULL, close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
238 {">", NULL, NULL, 0, "<Separator>"},
239 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
240 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
241 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
242 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
243 {N_("Help"), NULL, NULL, 0, "<Branch>"},
244 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
245 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
246 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
250 #define GET_MENU_ITEM(var, menu) \
251 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
253 #define GET_SMENU_ITEM(var, menu, sub) \
254 do { \
255 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
256 var = gtk_item_factory_get_widget(item_factory, tmp); \
257 g_free(tmp); \
258 } while (0)
260 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
261 do { \
262 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
263 var = gtk_item_factory_get_widget(item_factory, tmp); \
264 g_free(tmp); \
265 } while (0)
267 /* Returns TRUE if the keys were installed (first call only) */
268 gboolean ensure_filer_menu(void)
270 GList *items;
271 guchar *tmp;
272 GtkWidget *item;
273 GtkItemFactory *item_factory;
275 if (!filer_keys_need_init)
276 return FALSE;
277 filer_keys_need_init = FALSE;
279 item_factory = menu_create(filer_menu_def,
280 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
281 "<filer>", filer_keys);
283 GET_MENU_ITEM(filer_menu, "filer");
284 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
285 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
286 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
287 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
288 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
289 "Show Thumbnails");
291 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
292 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
293 filer_follow_sym = GTK_BIN(item)->child;
295 /* File '' label... */
296 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
297 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
298 g_list_free(items);
300 /* Shift Open... label */
301 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
302 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
303 g_list_free(items);
305 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
306 filer_new_window = GTK_BIN(item)->child;
308 g_signal_connect(filer_menu, "unmap_event",
309 G_CALLBACK(menu_closed), NULL);
310 g_signal_connect(filer_file_menu, "unmap_event",
311 G_CALLBACK(menu_closed), NULL);
313 g_signal_connect(filer_keys, "accel_changed",
314 G_CALLBACK(save_menus), NULL);
316 return TRUE;
319 void menu_init(void)
321 char *menurc;
323 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
324 if (menurc)
326 gtk_accel_map_load(menurc);
327 g_free(menurc);
330 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
331 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
332 option_add_saver(save_menus);
334 option_register_widget("menu-set-keys", set_keys_button);
336 filer_keys = gtk_accel_group_new();
339 /* Name is in the form "<panel>" */
340 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
341 const gchar *name, GtkAccelGroup *keys)
343 GtkItemFactory *item_factory;
344 GtkItemFactoryEntry *translated;
346 if (!keys)
348 keys = gtk_accel_group_new();
349 gtk_accel_group_lock(keys);
352 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
354 translated = translate_entries(def, n_entries);
355 gtk_item_factory_create_items(item_factory, n_entries,
356 translated, NULL);
357 free_translated_entries(translated, n_entries);
359 return item_factory;
362 /* Prevent the user from setting a short-cut on this item */
363 static void menuitem_no_shortcuts(GtkWidget *item)
365 /* XXX */
366 #if 0
367 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
369 _gtk_widget_set_accel_path(item, NULL, NULL);
370 null_g_free(&menuitem->accel_path);
371 #endif
374 /* Shade items that only work on single files */
375 static void shade_file_menu_items(gboolean shaded)
377 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
378 menu_set_items_shaded(filer_file_menu, shaded, 5, 2);
379 menu_set_items_shaded(filer_file_menu, shaded, 9, 2);
382 /* 'data' is an array of three ints:
383 * [ pointer_x, pointer_y, item_under_pointer ]
385 void position_menu(GtkMenu *menu, gint *x, gint *y,
386 gboolean *push_in, gpointer data)
388 int *pos = (int *) data;
389 GtkRequisition requisition;
390 GList *items, *next;
391 int y_shift = 0;
392 int item = pos[2];
394 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
396 while (item >= 0 && next)
398 int h = ((GtkWidget *) next->data)->requisition.height;
400 if (item > 0)
401 y_shift += h;
402 else
403 y_shift += h / 2;
405 next = next->next;
406 item--;
409 g_list_free(items);
411 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
413 *x = pos[0] - (requisition.width * 7 / 8);
414 *y = pos[1] - y_shift;
416 *x = CLAMP(*x, 0, screen_width - requisition.width);
417 *y = CLAMP(*y, 0, screen_height - requisition.height);
419 *push_in = FALSE;
422 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
423 MenuIconStyle style)
425 GtkWidget *item;
427 if (ditem->image && style != MIS_NONE)
429 GdkPixbuf *pixbuf;
431 switch (style)
433 case MIS_LARGE:
434 pixbuf = ditem->image->pixbuf;
435 break;
436 default:
437 if (!ditem->image->sm_pixbuf)
438 pixmap_make_small(ditem->image);
439 pixbuf = ditem->image->sm_pixbuf;
440 break;
443 item = gtk_image_menu_item_new_with_label(label);
444 /* TODO: Find a way to allow short-cuts */
445 menuitem_no_shortcuts(item);
447 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
448 gtk_image_new_from_pixbuf(pixbuf));
449 gtk_widget_show_all(item);
451 else
452 item = gtk_menu_item_new_with_label(label);
454 return item;
457 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
458 MenuIconStyle style, CallbackFn func,
459 gboolean separator, gboolean strip_ext,
460 gboolean recurse)
462 GList *widgets = NULL;
463 DirItem *ditem;
464 int i;
465 GtkWidget *item;
466 char *dname = NULL;
467 GPtrArray *names;
469 dname = pathdup(dir_name);
471 names = list_dir(dname);
472 if (!names)
473 goto out;
475 if (separator)
477 item = gtk_menu_item_new();
478 widgets = g_list_append(widgets, item);
479 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
482 for (i = 0; i < names->len; i++)
484 char *leaf = names->pdata[i];
485 gchar *fname;
487 fname = g_strconcat(dname, "/", leaf, NULL);
489 /* Strip off extension, if any */
490 if (strip_ext)
492 char *dot;
493 dot = strchr(leaf, '.');
494 if (dot)
495 *dot = '\0';
498 ditem = diritem_new("");
499 diritem_restat(fname, ditem, NULL);
501 item = make_send_to_item(ditem, leaf, style);
503 g_free(leaf);
505 /* If it is a directory (but NOT an AppDir) and we are
506 * recursing then set up a sub menu.
508 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
509 !(ditem->flags & ITEM_FLAG_APPDIR))
511 GtkWidget *sub;
512 GList *new_widgets;
514 sub = gtk_menu_new();
515 new_widgets = menu_from_dir(sub, fname, style, func,
516 separator, strip_ext, FALSE);
517 g_list_free(new_widgets);
518 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
520 else
521 g_signal_connect_swapped(item, "activate",
522 G_CALLBACK(func), fname);
524 diritem_free(ditem);
526 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
527 g_signal_connect_swapped(item, "destroy",
528 G_CALLBACK(g_free), fname);
530 widgets = g_list_append(widgets, item);
533 g_ptr_array_free(names, TRUE);
534 out:
535 g_free(dname);
537 return widgets;
540 /* Scan the templates dir and create entries for the New menu */
541 static void update_new_files_menu(MenuIconStyle style)
543 static GList *widgets = NULL;
545 gchar *templ_dname = NULL;
547 if (widgets)
549 GList *next;
551 for (next = widgets; next; next = next->next)
552 gtk_widget_destroy((GtkWidget *) next->data);
554 g_list_free(widgets);
555 widgets = NULL;
558 templ_dname = choices_find_path_load("Templates", "");
559 if (templ_dname)
561 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
562 (CallbackFn) new_file_type, TRUE, TRUE,
563 FALSE);
564 g_free(templ_dname);
566 gtk_widget_show_all(filer_new_menu);
569 /* 'item' is the number of the item to appear under the pointer. */
570 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
572 int pos[3];
573 int button = 0;
574 guint32 time = 0;
576 if (event && (event->type == GDK_BUTTON_PRESS ||
577 event->type == GDK_BUTTON_RELEASE))
579 GdkEventButton *bev = (GdkEventButton *) event;
581 pos[0] = bev->x_root;
582 pos[1] = bev->y_root;
583 button = bev->button;
584 time = bev->time;
586 else if (event && event->type == GDK_KEY_PRESS)
588 GdkEventKey *kev = (GdkEventKey *) event;
590 get_pointer_xy(pos, pos + 1);
591 time = kev->time;
593 else
594 get_pointer_xy(pos, pos + 1);
596 pos[2] = item;
598 gtk_widget_show_all(menu);
599 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
600 position_menu, (gpointer) pos, button, time);
601 select_nth_item(GTK_MENU_SHELL(menu), item);
604 /* Hide the popup menu, if any */
605 void menu_popdown(void)
607 if (popup_menu)
608 gtk_menu_popdown(GTK_MENU(popup_menu));
611 static MenuIconStyle get_menu_icon_style(void)
613 MenuIconStyle mis;
614 int display;
616 mis = o_menu_iconsize.int_value;
618 switch (mis)
620 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
621 return mis;
622 default:
623 break;
626 if (mis == MIS_CURRENT && window_with_focus)
628 switch (window_with_focus->display_style)
630 case HUGE_ICONS:
631 case LARGE_ICONS:
632 return MIS_LARGE;
633 case SMALL_ICONS:
634 return MIS_SMALL;
635 default:
636 break;
640 display = o_display_size.int_value;
641 switch (display)
643 case HUGE_ICONS:
644 case LARGE_ICONS:
645 return MIS_LARGE;
646 case SMALL_ICONS:
647 return MIS_SMALL;
648 default:
649 break;
652 return MIS_SMALL;
655 /* iter->peek() is the clicked item, or NULL if none */
656 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
658 DirItem *file_item = NULL;
659 GdkModifierType state = 0;
660 int n_selected;
661 int n_added = 0;
663 n_selected = view_count_selected(filer_window->view);
665 ensure_filer_menu();
667 updating_menu++;
669 /* Remove previous AppMenu, if any */
670 appmenu_remove();
672 window_with_focus = filer_window;
674 if (!event)
675 event = gtk_get_current_event();
677 if (event->type == GDK_BUTTON_PRESS)
678 state = ((GdkEventButton *) event)->state;
679 else if (event->type == GDK_KEY_PRESS)
680 state = ((GdkEventKey *) event)->state;
682 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
684 filer_window->temp_item_selected = TRUE;
685 view_set_selected(filer_window->view, iter, TRUE);
686 n_selected = view_count_selected(filer_window->view);
688 else
690 filer_window->temp_item_selected = FALSE;
693 /* Short-cut to the Send To menu */
694 if (state & GDK_SHIFT_MASK)
696 GList *paths;
698 updating_menu--;
700 if (n_selected == 0)
702 report_error(
703 _("You should Shift+Menu click over a file to "
704 "send it somewhere"));
705 return;
708 paths = filer_selected_items(filer_window);
710 show_send_to_menu(paths, event); /* (paths eaten) */
712 return;
716 GtkWidget *file_label, *file_menu;
717 GString *buffer;
718 DirItem *item;
720 file_label = filer_file_item;
721 file_menu = filer_file_menu;
722 gtk_check_menu_item_set_active(
723 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
724 filer_window->show_thumbs);
725 gtk_check_menu_item_set_active(
726 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
727 filer_window->show_hidden);
728 gtk_check_menu_item_set_active(
729 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
730 filer_window->sort_order != GTK_SORT_ASCENDING);
731 gtk_check_menu_item_set_active(
732 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
733 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
734 buffer = g_string_new(NULL);
736 switch (n_selected)
738 case 0:
739 g_string_assign(buffer, _("Next Click"));
740 shade_file_menu_items(FALSE);
741 break;
742 case 1:
743 item = filer_selected_item(filer_window);
744 if (!item->image)
745 dir_update_item(filer_window->directory,
746 item->leafname);
747 shade_file_menu_items(FALSE);
748 file_item = filer_selected_item(filer_window);
749 g_string_printf(buffer, _("%s '%s'"),
750 basetype_name(file_item),
751 g_utf8_validate(file_item->leafname,
752 -1, NULL)
753 ? file_item->leafname
754 : _("(bad utf-8)"));
755 if (!can_set_run_action(file_item))
756 menu_set_items_shaded(filer_file_menu,
757 TRUE, 9, 1);
758 break;
759 default:
760 shade_file_menu_items(TRUE);
761 g_string_printf(buffer, _("%d items"),
762 n_selected);
763 break;
765 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
766 g_string_free(buffer, TRUE);
768 menu_show_shift_action(file_shift_item, file_item,
769 n_selected == 0);
770 if (file_item)
771 n_added = appmenu_add(make_path(filer_window->sym_path,
772 file_item->leafname),
773 file_item, filer_file_menu);
776 update_new_files_menu(get_menu_icon_style());
778 gtk_widget_set_sensitive(filer_new_window,
779 !o_unique_filer_windows.int_value);
780 gtk_widget_set_sensitive(filer_follow_sym,
781 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
783 popup_menu = (state & GDK_CONTROL_MASK)
784 ? filer_file_menu
785 : filer_menu;
787 updating_menu--;
789 show_popup_menu(popup_menu, event,
790 popup_menu == filer_file_menu ? (n_added + 7) : 1);
793 static void menu_closed(GtkWidget *widget)
795 if (window_with_focus == NULL || widget != popup_menu)
796 return; /* Close panel item chosen? */
798 popup_menu = NULL;
800 if (window_with_focus->temp_item_selected)
802 view_clear_selection(window_with_focus->view);
803 window_with_focus->temp_item_selected = FALSE;
806 appmenu_remove();
809 static void target_callback(FilerWindow *filer_window,
810 ViewIter *iter,
811 gpointer action)
813 g_return_if_fail(filer_window != NULL);
815 window_with_focus = filer_window;
817 /* Don't grab the primary selection */
818 filer_window->temp_item_selected = TRUE;
820 view_wink_item(filer_window->view, iter);
821 view_select_only(filer_window->view, iter);
822 file_op(NULL, GPOINTER_TO_INT(action), NULL);
824 view_clear_selection(filer_window->view);
825 filer_window->temp_item_selected = FALSE;
828 /* Set the text of the 'Shift Open...' menu item.
829 * If icon is NULL, reset the text and also shade it, unless 'next'.
831 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
833 guchar *shift_action = NULL;
835 if (item)
837 if (item->flags & ITEM_FLAG_MOUNT_POINT)
839 if (item->flags & ITEM_FLAG_MOUNTED)
840 shift_action = N_("Unmount");
841 else
842 shift_action = N_("Open unmounted");
844 else if (item->flags & ITEM_FLAG_SYMLINK)
845 shift_action = N_("Show Target");
846 else if (item->base_type == TYPE_DIRECTORY)
847 shift_action = N_("Look Inside");
848 else if (item->base_type == TYPE_FILE)
849 shift_action = N_("Open As Text");
851 gtk_label_set_text(GTK_LABEL(menu_item),
852 shift_action ? _(shift_action)
853 : _("Shift Open"));
854 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
857 /* Actions */
859 static void view_type(gpointer data, guint action, GtkWidget *widget)
861 ViewType view_type = (ViewType) action;
863 g_return_if_fail(window_with_focus != NULL);
865 if (view_type == VIEW_TYPE_COLLECTION)
866 display_set_layout(window_with_focus,
867 window_with_focus->display_style_wanted,
868 DETAILS_NONE, FALSE);
870 filer_set_view_type(window_with_focus, (ViewType) action);
873 static void change_size(gpointer data, guint action, GtkWidget *widget)
875 g_return_if_fail(window_with_focus != NULL);
877 display_change_size(window_with_focus, action == 1);
880 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
882 g_return_if_fail(window_with_focus != NULL);
884 if (updating_menu)
885 return;
887 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
888 display_set_layout(window_with_focus,
889 window_with_focus->display_style,
890 window_with_focus->details_type, FALSE);
891 else
892 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
893 window_with_focus->details_type, FALSE);
896 static void set_with(gpointer data, guint action, GtkWidget *widget)
898 DisplayStyle size;
900 g_return_if_fail(window_with_focus != NULL);
902 size = window_with_focus->display_style_wanted;
904 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
905 display_set_layout(window_with_focus, size, action, FALSE);
908 static void set_sort(gpointer data, guint action, GtkWidget *widget)
910 if (updating_menu)
911 return;
913 g_return_if_fail(window_with_focus != NULL);
915 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
918 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
920 GtkSortType order;
922 if (updating_menu)
923 return;
925 g_return_if_fail(window_with_focus != NULL);
927 order = window_with_focus->sort_order;
928 if (order == GTK_SORT_ASCENDING)
929 order = GTK_SORT_DESCENDING;
930 else
931 order = GTK_SORT_ASCENDING;
933 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
934 order);
937 static void hidden(gpointer data, guint action, GtkWidget *widget)
939 if (updating_menu)
940 return;
942 g_return_if_fail(window_with_focus != NULL);
944 display_set_hidden(window_with_focus,
945 !window_with_focus->show_hidden);
948 static void test_filter(gpointer data, guint action, GtkWidget *widget)
950 if (updating_menu)
951 return;
953 g_return_if_fail(window_with_focus != NULL);
955 display_set_filter(window_with_focus,
956 FILER_SHOW_GLOB, "*.c");
959 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
961 if (updating_menu)
962 return;
964 g_return_if_fail(window_with_focus != NULL);
966 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
969 static void refresh(gpointer data, guint action, GtkWidget *widget)
971 g_return_if_fail(window_with_focus != NULL);
973 filer_refresh(window_with_focus);
976 static void delete(FilerWindow *filer_window)
978 GList *paths;
979 paths = filer_selected_items(filer_window);
980 action_delete(paths);
981 destroy_glist(&paths);
984 static void usage(FilerWindow *filer_window)
986 GList *paths;
987 paths = filer_selected_items(filer_window);
988 action_usage(paths);
989 destroy_glist(&paths);
992 static void chmod_items(FilerWindow *filer_window)
994 GList *paths;
995 paths = filer_selected_items(filer_window);
996 action_chmod(paths, FALSE, NULL);
997 destroy_glist(&paths);
1000 static void set_type_items(FilerWindow *filer_window)
1002 GList *paths;
1003 paths = filer_selected_items(filer_window);
1004 action_settype(paths, FALSE, NULL);
1005 destroy_glist(&paths);
1008 static void find(FilerWindow *filer_window)
1010 GList *paths;
1011 paths = filer_selected_items(filer_window);
1012 action_find(paths);
1013 destroy_glist(&paths);
1016 /* This creates a new savebox widget, and allows the user to pick a new path
1017 * for the file.
1018 * Once the new path has been picked, the callback will be called with
1019 * both the current and new paths.
1020 * NOTE: This function unrefs 'image'!
1022 static void savebox_show(const gchar *action, const gchar *path,
1023 MaskedPixmap *image, SaveCb callback,
1024 GdkDragAction dnd_action)
1026 GtkWidget *savebox = NULL;
1027 GtkWidget *check_relative = NULL;
1029 g_return_if_fail(image != NULL);
1031 savebox = gtk_savebox_new(action);
1032 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1034 if (callback == link_cb)
1036 check_relative = gtk_check_button_new_with_mnemonic(
1037 _("_Relative link"));
1038 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1039 TRUE);
1041 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1042 gtk_tooltips_set_tip(tooltips, check_relative,
1043 _("If on, the symlink will store the path from the "
1044 "symlink to the target file. Use this if the symlink "
1045 "and the target will be moved together.\n"
1046 "If off, the path from the root directory is stored - "
1047 "use this if the symlink may move but the target will "
1048 "stay put."), NULL);
1049 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1050 check_relative, FALSE, TRUE, 0);
1051 gtk_widget_show(check_relative);
1054 g_signal_connect(savebox, "save_to_file",
1055 G_CALLBACK(save_to_file), NULL);
1057 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1058 g_strdup(path), g_free);
1059 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1060 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1062 gtk_window_set_title(GTK_WINDOW(savebox), action);
1064 if (g_utf8_validate(path, -1, NULL))
1065 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1066 else
1068 gchar *u8, *dir;
1069 dir = g_path_get_dirname(path);
1070 u8 = to_utf8(g_basename(path));
1071 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1072 make_path(dir, u8));
1073 g_free(u8);
1074 g_free(dir);
1076 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1077 g_object_unref(image);
1079 gtk_widget_show(savebox);
1082 static gint save_to_file(GObject *savebox,
1083 const gchar *pathname, gpointer data)
1085 SaveCb callback;
1086 const gchar *current_path;
1088 callback = g_object_get_data(savebox, "action_callback");
1089 current_path = g_object_get_data(savebox, "current_path");
1091 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1092 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1094 return callback(savebox, current_path, pathname)
1095 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1098 static gboolean copy_cb(GObject *savebox,
1099 const gchar *current, const gchar *new)
1101 return action_with_leaf(action_copy, current, new);
1104 static gboolean action_with_leaf(ActionFn action,
1105 const gchar *current, const gchar *new)
1107 const char *leaf;
1108 char *new_dir;
1109 GList *local_paths;
1111 if (new[0] != '/')
1113 report_error(_("New pathname is not absolute"));
1114 return FALSE;
1117 if (new[strlen(new) - 1] == '/')
1119 new_dir = g_strdup(new);
1120 leaf = NULL;
1122 else
1124 const gchar *slash;
1126 slash = strrchr(new, '/');
1127 new_dir = g_strndup(new, slash - new);
1128 leaf = slash + 1;
1131 local_paths = g_list_append(NULL, (gchar *) current);
1132 action(local_paths, new_dir, leaf, -1);
1133 g_list_free(local_paths);
1135 g_free(new_dir);
1137 return TRUE;
1140 /* Open a savebox to act on the selected file.
1141 * Call 'callback' later to perform the operation.
1143 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1144 const gchar *action, SaveCb callback,
1145 GdkDragAction dnd_action)
1147 g_object_ref(image);
1148 savebox_show(action, path, image, callback, dnd_action);
1151 static gboolean rename_cb(GObject *savebox,
1152 const gchar *current, const gchar *new)
1154 return action_with_leaf(action_move, current, new);
1157 static gboolean link_cb(GObject *savebox,
1158 const gchar *initial, const gchar *path)
1160 GtkToggleButton *check_relative;
1161 struct stat info;
1162 int err;
1163 gchar *link_path;
1165 check_relative = g_object_get_data(savebox, "check_relative");
1167 if (gtk_toggle_button_get_active(check_relative))
1168 link_path = get_relative_path(path, initial);
1169 else
1170 link_path = g_strdup(initial);
1172 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1174 GtkWidget *box, *button;
1175 gint ans;
1177 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1178 GTK_BUTTONS_CANCEL,
1179 _("Symlink from '%s' already exists. "
1180 "Replace it with a link to '%s'?"),
1181 path, link_path);
1183 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1185 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1186 gtk_widget_show(button);
1187 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1188 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1189 button, GTK_RESPONSE_OK);
1190 gtk_dialog_set_default_response(GTK_DIALOG(box),
1191 GTK_RESPONSE_OK);
1193 ans = gtk_dialog_run(GTK_DIALOG(box));
1194 gtk_widget_destroy(box);
1196 if (ans != GTK_RESPONSE_OK)
1198 g_free(link_path);
1199 return FALSE;
1202 unlink(path);
1205 err = symlink(link_path, path);
1206 g_free(link_path);
1208 if (err)
1210 report_error("symlink: %s", g_strerror(errno));
1211 return FALSE;
1214 dir_check_this(path);
1216 return TRUE;
1219 static void run_action(DirItem *item)
1221 if (can_set_run_action(item))
1222 type_set_handler_dialog(item->mime_type);
1223 else
1224 report_error(
1225 _("You can only set the run action for a "
1226 "regular file"));
1229 void open_home(gpointer data, guint action, GtkWidget *widget)
1231 filer_opendir(home_dir, NULL, NULL);
1234 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1236 gchar *path;
1238 path = g_strconcat(filer_window->sym_path,
1239 "/", item->leafname, "#", NULL);
1241 filer_change_to(filer_window, path, NULL);
1242 g_free(path);
1245 static void select_all(gpointer data, guint action, GtkWidget *widget)
1247 g_return_if_fail(window_with_focus != NULL);
1249 window_with_focus->temp_item_selected = FALSE;
1250 view_select_all(window_with_focus->view);
1253 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1255 g_return_if_fail(window_with_focus != NULL);
1257 window_with_focus->temp_item_selected = FALSE;
1258 view_clear_selection(window_with_focus->view);
1261 static gboolean invert_cb(ViewIter *iter, gpointer data)
1263 return !view_get_selected((ViewIface *) data, iter);
1266 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1268 g_return_if_fail(window_with_focus != NULL);
1270 window_with_focus->temp_item_selected = FALSE;
1272 view_select_if(window_with_focus->view, invert_cb,
1273 window_with_focus->view);
1276 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1278 GtkWidget *win;
1280 win = options_show();
1282 if (win)
1284 number_of_windows++;
1285 g_signal_connect(win, "destroy",
1286 G_CALLBACK(one_less_window), NULL);
1290 static gboolean new_directory_cb(GObject *savebox,
1291 const gchar *initial, const gchar *path)
1293 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1295 report_error("mkdir: %s", g_strerror(errno));
1296 return FALSE;
1299 dir_check_this(path);
1301 if (filer_exists(window_with_focus))
1303 guchar *leaf;
1304 leaf = strrchr(path, '/');
1305 if (leaf)
1306 display_set_autoselect(window_with_focus, leaf + 1);
1309 return TRUE;
1312 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1314 g_return_if_fail(window_with_focus != NULL);
1316 savebox_show(_("Create"),
1317 make_path(window_with_focus->sym_path, _("NewDir")),
1318 type_to_icon(inode_directory), new_directory_cb,
1319 GDK_ACTION_COPY);
1322 static gboolean new_file_cb(GObject *savebox,
1323 const gchar *initial, const gchar *path)
1325 int fd;
1327 fd = open(path, O_CREAT | O_EXCL, 0666);
1329 if (fd == -1)
1331 report_error(_("Error creating '%s': %s"),
1332 path, g_strerror(errno));
1333 return FALSE;
1336 if (close(fd))
1337 report_error(_("Error creating '%s': %s"),
1338 path, g_strerror(errno));
1340 dir_check_this(path);
1342 if (filer_exists(window_with_focus))
1344 guchar *leaf;
1345 leaf = strrchr(path, '/');
1346 if (leaf)
1347 display_set_autoselect(window_with_focus, leaf + 1);
1350 return TRUE;
1353 static void new_file(gpointer data, guint action, GtkWidget *widget)
1355 g_return_if_fail(window_with_focus != NULL);
1357 savebox_show(_("Create"),
1358 make_path(window_with_focus->sym_path, _("NewFile")),
1359 type_to_icon(text_plain),
1360 new_file_cb, GDK_ACTION_COPY);
1363 static gboolean new_file_type_cb(GObject *savebox,
1364 const gchar *initial, const gchar *path)
1366 const gchar *oleaf, *leaf;
1367 gchar *templ, *templ_dname, *dest;
1368 GList *paths;
1370 /* We can work out the template path from the initial name */
1371 oleaf = g_basename(initial);
1372 templ_dname = choices_find_path_load("Templates", "");
1373 if (!templ_dname)
1375 report_error(
1376 _("Error creating file: could not find the template for %s"),
1377 oleaf);
1378 return FALSE;
1381 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1382 g_free(templ_dname);
1384 dest = g_path_get_dirname(path);
1385 leaf = g_basename(path);
1386 paths = g_list_append(NULL, templ);
1388 action_copy(paths, dest, leaf, TRUE);
1390 g_list_free(paths);
1391 g_free(dest);
1392 g_free(templ);
1394 if (filer_exists(window_with_focus))
1395 display_set_autoselect(window_with_focus, leaf);
1397 return TRUE;
1400 static void do_send_to(gchar *templ)
1402 g_return_if_fail(send_to_paths != NULL);
1404 run_with_files(templ, send_to_paths);
1407 static void new_file_type(gchar *templ)
1409 const gchar *leaf;
1410 MIME_type *type;
1412 g_return_if_fail(window_with_focus != NULL);
1414 leaf = g_basename(templ);
1415 type = type_get_type(templ);
1417 savebox_show(_("Create"),
1418 make_path(window_with_focus->sym_path, leaf),
1419 type_to_icon(type),
1420 new_file_type_cb, GDK_ACTION_COPY);
1423 static void customise_send_to(gpointer data)
1425 GPtrArray *path;
1426 guchar *save;
1427 GString *dirs;
1428 int i;
1430 dirs = g_string_new(NULL);
1432 path = choices_list_dirs("");
1433 for (i = 0; i < path->len; i++)
1435 guchar *old = (guchar *) path->pdata[i];
1437 g_string_append(dirs, old);
1438 g_string_append(dirs, "SendTo\n");
1440 choices_free_list(path);
1442 save = choices_find_path_save("", "SendTo", TRUE);
1443 if (save)
1444 mkdir(save, 0777);
1446 info_message(
1447 _("The `Send To' menu provides a quick way to send some files "
1448 "to an application. The applications listed are those in "
1449 "the following directories:\n\n%s\n%s\n"
1450 "The `Send To' menu may be opened by Shift+Menu clicking "
1451 "over a file.\n\n"
1452 "Advanced use:\n"
1453 "You can also create subdirectories called "
1454 "`.text_html', `.text', etc which will only be "
1455 "shown for files of that type. `.group' is shown "
1456 "only when multiple files are selected."),
1457 dirs->str,
1458 save ? _("I'll show you your SendTo directory now; you should "
1459 "symlink (Ctrl+Shift drag) any applications you want "
1460 "into it.")
1461 : _("Your CHOICESPATH variable setting prevents "
1462 "customisations - sorry."));
1464 g_string_free(dirs, TRUE);
1466 if (save)
1467 filer_opendir(save, NULL, NULL);
1470 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1471 * to the menu.
1473 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1475 gchar *searchdir;
1476 GPtrArray *paths;
1477 int i;
1479 if (subtype)
1480 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1481 else if (type)
1482 searchdir = g_strdup_printf("SendTo/.%s", type);
1483 else
1484 searchdir = g_strdup("SendTo");
1486 paths = choices_list_dirs(searchdir);
1487 g_free(searchdir);
1489 for (i = 0; i < paths->len; i++)
1491 GList *widgets = NULL;
1492 guchar *dir = (guchar *) paths->pdata[i];
1494 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1495 (CallbackFn) do_send_to,
1496 FALSE, FALSE, TRUE);
1498 if (widgets)
1499 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1500 gtk_menu_item_new());
1502 g_list_free(widgets); /* TODO: Get rid of this */
1505 choices_free_list(paths);
1508 /* Scan the SendTo dir and create and show the Send To menu.
1509 * The 'paths' list and every path in it is claimed, and will be
1510 * freed later -- don't free it yourself!
1512 static void show_send_to_menu(GList *paths, GdkEvent *event)
1514 GtkWidget *menu, *item;
1516 menu = gtk_menu_new();
1518 if (g_list_length(paths) == 1)
1520 DirItem *item;
1522 item = diritem_new("");
1523 diritem_restat(paths->data, item, NULL);
1525 add_sendto(menu,
1526 item->mime_type->media_type,
1527 item->mime_type->subtype);
1529 add_sendto(menu, item->mime_type->media_type, NULL);
1531 diritem_free(item);
1533 else
1534 add_sendto(menu, "group", NULL);
1536 add_sendto(menu, NULL, NULL);
1538 item = gtk_menu_item_new_with_label(_("Customise"));
1539 g_signal_connect_swapped(item, "activate",
1540 G_CALLBACK(customise_send_to), NULL);
1541 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1543 if (send_to_paths)
1544 destroy_glist(&send_to_paths);
1546 send_to_paths = paths;
1548 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1550 popup_menu = menu;
1551 show_popup_menu(menu, event, 0);
1554 static void send_to(FilerWindow *filer_window)
1556 GList *paths;
1557 GdkEvent *event;
1559 paths = filer_selected_items(filer_window);
1560 event = gtk_get_current_event();
1562 /* Eats paths */
1563 show_send_to_menu(paths, event);
1565 gdk_event_free(event);
1568 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1570 const char *argv[] = {"sh", "-c", NULL, NULL};
1571 gboolean close = action;
1573 argv[2] = o_menu_xterm.value;
1575 g_return_if_fail(window_with_focus != NULL);
1577 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1578 gtk_widget_destroy(window_with_focus->window);
1581 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1583 g_return_if_fail(window_with_focus != NULL);
1585 filer_change_to(window_with_focus, home_dir, NULL);
1588 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1590 g_return_if_fail(window_with_focus != NULL);
1592 bookmarks_show_menu(window_with_focus);
1595 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1597 g_return_if_fail(window_with_focus != NULL);
1599 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1600 filer_change_to(window_with_focus,
1601 window_with_focus->real_path, NULL);
1602 else
1603 delayed_error(_("This is already the canonical name "
1604 "for this directory."));
1607 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1609 g_return_if_fail(window_with_focus != NULL);
1611 filer_open_parent(window_with_focus);
1614 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1616 g_return_if_fail(window_with_focus != NULL);
1618 change_to_parent(window_with_focus);
1621 static void resize(gpointer data, guint action, GtkWidget *widget)
1623 g_return_if_fail(window_with_focus != NULL);
1625 view_autosize(window_with_focus->view);
1628 static void new_window(gpointer data, guint action, GtkWidget *widget)
1630 g_return_if_fail(window_with_focus != NULL);
1632 if (o_unique_filer_windows.int_value)
1634 report_error(_("You can't open a second view onto "
1635 "this directory because the `Unique Windows' option "
1636 "is turned on in the Options window."));
1638 else
1639 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1642 static void close_window(gpointer data, guint action, GtkWidget *widget)
1644 g_return_if_fail(window_with_focus != NULL);
1646 if (!filer_window_delete(window_with_focus->window, NULL,
1647 window_with_focus))
1648 gtk_widget_destroy(window_with_focus->window);
1651 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1653 MiniType type = (MiniType) action;
1655 g_return_if_fail(window_with_focus != NULL);
1657 /* Item needs to remain selected... */
1658 if (type == MINI_SHELL)
1659 window_with_focus->temp_item_selected = FALSE;
1661 minibuffer_show(window_with_focus, type);
1664 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1666 if (action == HELP_ABOUT)
1667 infobox_new(app_dir);
1668 else if (action == HELP_DIR)
1669 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1670 else if (action == HELP_MANUAL)
1672 gchar *manual = NULL;
1674 if (current_lang)
1676 manual = g_strconcat(app_dir, "/Help/Manual-",
1677 current_lang, ".html", NULL);
1678 if (access(manual, F_OK))
1679 null_g_free(&manual);
1682 if (!manual)
1683 manual = g_strconcat(app_dir,
1684 "/Help/Manual.html", NULL);
1686 run_by_path(manual);
1688 g_free(manual);
1690 else
1691 g_warning("Unknown help action %d\n", action);
1694 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1695 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1697 GList *items, *item;
1699 items = gtk_container_get_children(GTK_CONTAINER(menu));
1701 item = g_list_nth(items, from);
1702 while (item && n--)
1704 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1705 item = item->next;
1707 g_list_free(items);
1710 static void save_menus(void)
1712 char *menurc;
1714 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1715 if (menurc)
1717 gtk_accel_map_save(menurc);
1718 g_free(menurc);
1722 static void select_nth_item(GtkMenuShell *shell, int n)
1724 GList *items;
1725 GtkWidget *item;
1727 items = gtk_container_get_children(GTK_CONTAINER(shell));
1728 item = g_list_nth_data(items, n);
1730 g_return_if_fail(item != NULL);
1732 g_list_free(items);
1734 gtk_menu_shell_select_item(shell, item);
1737 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1739 DirItem *item;
1740 const guchar *path;
1741 int n_selected;
1742 ViewIter iter;
1744 g_return_if_fail(window_with_focus != NULL);
1746 n_selected = view_count_selected(window_with_focus->view);
1748 if (n_selected < 1)
1750 const char *prompt;
1752 switch (action)
1754 case FILE_COPY_ITEM:
1755 prompt = _("Copy ... ?");
1756 break;
1757 case FILE_RENAME_ITEM:
1758 prompt = _("Rename ... ?");
1759 break;
1760 case FILE_LINK_ITEM:
1761 prompt = _("Symlink ... ?");
1762 break;
1763 case FILE_OPEN_FILE:
1764 prompt = _("Shift Open ... ?");
1765 break;
1766 case FILE_PROPERTIES:
1767 prompt = _("Properties of ... ?");
1768 break;
1769 case FILE_SET_TYPE:
1770 prompt = _("Set type of ... ?");
1771 break;
1772 case FILE_RUN_ACTION:
1773 prompt = _("Set run action for ... ?");
1774 break;
1775 case FILE_SET_ICON:
1776 prompt = _("Set icon for ... ?");
1777 break;
1778 case FILE_SEND_TO:
1779 prompt = _("Send ... to ... ?");
1780 break;
1781 case FILE_DELETE:
1782 prompt = _("DELETE ... ?");
1783 break;
1784 case FILE_USAGE:
1785 prompt = _("Count the size of ... ?");
1786 break;
1787 case FILE_CHMOD_ITEMS:
1788 prompt = _("Set permissions on ... ?");
1789 break;
1790 case FILE_FIND:
1791 prompt = _("Search inside ... ?");
1792 break;
1793 case FILE_OPEN_VFS_AVFS:
1794 prompt = _("Look inside ... ?");
1795 break;
1796 default:
1797 g_warning("Unknown action!");
1798 return;
1800 filer_target_mode(window_with_focus, target_callback,
1801 GINT_TO_POINTER(action), prompt);
1802 return;
1805 switch (action)
1807 case FILE_SEND_TO:
1808 send_to(window_with_focus);
1809 return;
1810 case FILE_DELETE:
1811 delete(window_with_focus);
1812 return;
1813 case FILE_USAGE:
1814 usage(window_with_focus);
1815 return;
1816 case FILE_CHMOD_ITEMS:
1817 chmod_items(window_with_focus);
1818 return;
1819 case FILE_SET_TYPE:
1820 set_type_items(window_with_focus);
1821 return;
1822 case FILE_FIND:
1823 find(window_with_focus);
1824 return;
1825 case FILE_PROPERTIES:
1827 GList *items;
1829 items = filer_selected_items(window_with_focus);
1830 infobox_show_list(items);
1831 destroy_glist(&items);
1832 return;
1834 default:
1835 break;
1838 /* All the following actions require exactly one file selected */
1840 if (n_selected > 1)
1842 report_error(_("You cannot do this to more than "
1843 "one item at a time"));
1844 return;
1847 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1849 item = iter.next(&iter);
1850 g_return_if_fail(item != NULL);
1851 /* iter may be passed to filer_openitem... */
1853 if (!item->image)
1854 item = dir_update_item(window_with_focus->directory,
1855 item->leafname);
1857 if (!item)
1859 report_error(_("Item no longer exists!"));
1860 return;
1863 path = make_path(window_with_focus->sym_path, item->leafname);
1865 switch (action)
1867 case FILE_COPY_ITEM:
1868 src_dest_action_item(path, item->image,
1869 _("Copy"), copy_cb,
1870 GDK_ACTION_COPY);
1871 break;
1872 case FILE_RENAME_ITEM:
1873 src_dest_action_item(path, item->image,
1874 _("Rename"), rename_cb,
1875 GDK_ACTION_MOVE);
1876 break;
1877 case FILE_LINK_ITEM:
1878 src_dest_action_item(path, item->image,
1879 _("Symlink"), link_cb,
1880 GDK_ACTION_LINK);
1881 break;
1882 case FILE_OPEN_FILE:
1883 filer_openitem(window_with_focus, &iter,
1884 OPEN_SAME_WINDOW | OPEN_SHIFT);
1885 break;
1886 case FILE_RUN_ACTION:
1887 run_action(item);
1888 break;
1889 case FILE_SET_ICON:
1890 icon_set_handler_dialog(item, path);
1891 break;
1892 case FILE_OPEN_VFS_AVFS:
1893 open_vfs_avfs(window_with_focus, item);
1894 break;
1895 default:
1896 g_warning("Unknown action!");
1897 return;
1901 static void show_key_help(GtkWidget *button, gpointer data)
1903 gboolean can_change_accels;
1905 g_object_get(G_OBJECT(gtk_settings_get_default()),
1906 "gtk-can-change-accels", &can_change_accels,
1907 NULL);
1909 if (!can_change_accels)
1911 info_message(_("User-definable shortcuts are disabled by "
1912 "default in Gtk2, and you have not enabled "
1913 "them. You can turn this feature on by:\n\n"
1914 "1) using an XSettings manager, such as ROX-Session "
1915 "or gnome-settings-daemon, or\n\n"
1916 "2) adding this line to ~/.gtkrc-2.0:\n"
1917 "\tgtk-can-change-accels = 1\n"
1918 "\t(this only works if NOT using XSETTINGS)"));
1919 return;
1922 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1923 "- Open the menu over a filer window,\n"
1924 "- Move the pointer over the item you want to use,\n"
1925 "- Press the key you want attached to it.\n\n"
1926 "The key will appear next to the menu item and you can just press "
1927 "that key without opening the menu in future."));
1930 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1932 GtkWidget *button, *align;
1934 g_return_val_if_fail(option == NULL, NULL);
1936 align = gtk_alignment_new(0, 0.5, 0, 0);
1937 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1938 gtk_container_add(GTK_CONTAINER(align), button);
1939 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1941 return g_list_append(NULL, align);