r3287: Renamed some functions stolen from Gtk to avoid possible conflicts with
[rox-filer.git] / ROX-Filer / src / menu.c
blobff5179e9a920526301842479b02aa617f80bccda
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 show_thumbs(gpointer data, guint action, GtkWidget *widget);
132 static void refresh(gpointer data, guint action, GtkWidget *widget);
134 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
136 static void select_all(gpointer data, guint action, GtkWidget *widget);
137 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
138 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
139 static void new_directory(gpointer data, guint action, GtkWidget *widget);
140 static void new_file(gpointer data, guint action, GtkWidget *widget);
141 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
143 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
144 static void open_parent(gpointer data, guint action, GtkWidget *widget);
145 static void home_directory(gpointer data, guint action, GtkWidget *widget);
146 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget);
147 static void new_window(gpointer data, guint action, GtkWidget *widget);
148 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
149 static void close_window(gpointer data, guint action, GtkWidget *widget);
150 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget);
152 /* (action used in this - MiniType) */
153 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
154 static void resize(gpointer data, guint action, GtkWidget *widget);
156 #define MENUS_NAME "menus2"
158 static GtkWidget *filer_menu; /* The popup filer menu */
159 static GtkWidget *filer_file_item; /* The File '' label */
160 static GtkWidget *filer_file_menu; /* The File '' menu */
161 static GtkWidget *file_shift_item; /* Shift Open label */
162 static GtkWidget *filer_auto_size_menu; /* The Automatic item */
163 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
164 static GtkWidget *filer_reverse_menu; /* The Reversed item */
165 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
166 static GtkWidget *filer_new_window; /* The New Window item */
167 static GtkWidget *filer_new_menu; /* The New submenu */
168 static GtkWidget *filer_follow_sym; /* Follow symbolic links item */
170 #undef N_
171 #define N_(x) x
173 static GtkItemFactoryEntry filer_menu_def[] = {
174 {N_("Display"), NULL, NULL, 0, "<Branch>"},
175 {">" N_("Icons View"), NULL, view_type, VIEW_TYPE_COLLECTION, NULL},
176 {">" N_("Icons, With..."), NULL, NULL, 0, "<Branch>"},
177 {">>" N_("Sizes"), NULL, set_with, DETAILS_SIZE, NULL},
178 {">>" N_("Permissions"), NULL, set_with, DETAILS_PERMISSIONS, NULL},
179 {">>" N_("Types"), NULL, set_with, DETAILS_TYPE, NULL},
180 {">>" N_("Times"), NULL, set_with, DETAILS_TIMES, NULL},
181 {">" N_("List View"), NULL, view_type, VIEW_TYPE_DETAILS, "<StockItem>", ROX_STOCK_SHOW_DETAILS},
182 {">", NULL, NULL, 0, "<Separator>"},
183 {">" N_("Bigger Icons"), NULL, change_size, 1, "<StockItem>", GTK_STOCK_ZOOM_IN},
184 {">" N_("Smaller Icons"), NULL, change_size, -1, "<StockItem>", GTK_STOCK_ZOOM_OUT},
185 {">" N_("Automatic"), NULL, change_size_auto, 0, "<ToggleItem>"},
186 {">", NULL, NULL, 0, "<Separator>"},
187 {">" N_("Sort by Name"), NULL, set_sort, SORT_NAME, NULL},
188 {">" N_("Sort by Type"), NULL, set_sort, SORT_TYPE, NULL},
189 {">" N_("Sort by Date"), NULL, set_sort, SORT_DATE, NULL},
190 {">" N_("Sort by Size"), NULL, set_sort, SORT_SIZE, NULL},
191 {">" N_("Sort by Owner"), NULL, set_sort, SORT_OWNER, NULL},
192 {">" N_("Sort by Group"), NULL, set_sort, SORT_GROUP, NULL},
193 {">" N_("Reversed"), NULL, reverse_sort, 0, "<ToggleItem>"},
194 {">", NULL, NULL, 0, "<Separator>"},
195 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
196 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
197 {">" N_("Refresh"), NULL, refresh, 0, "<StockItem>", GTK_STOCK_REFRESH},
198 {N_("File"), NULL, NULL, 0, "<Branch>"},
199 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, "<StockItem>", GTK_STOCK_COPY},
200 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
201 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
202 {">" N_("Delete"), NULL, file_op, FILE_DELETE, "<StockItem>", GTK_STOCK_DELETE},
203 {">", NULL, NULL, 0, "<Separator>"},
204 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE},
205 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, "<StockItem>", GTK_STOCK_OPEN},
206 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
207 {">", NULL, NULL, 0, "<Separator>"},
208 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, "<StockItem>", GTK_STOCK_EXECUTE},
209 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
210 {">" N_("Properties"), NULL, file_op, FILE_PROPERTIES, "<StockItem>", GTK_STOCK_PROPERTIES},
211 {">" N_("Count"), NULL, file_op, FILE_USAGE, NULL},
212 {">" N_("Set type..."), NULL, file_op, FILE_SET_TYPE, NULL},
213 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
214 {">", NULL, NULL, 0, "<Separator>"},
215 {">" N_("Find"), NULL, file_op, FILE_FIND, "<StockItem>", GTK_STOCK_FIND},
216 {N_("Select"), NULL, NULL, 0, "<Branch>"},
217 {">" N_("Select All"), NULL, select_all, 0, NULL},
218 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
219 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
220 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
221 {N_("Options..."), NULL, menu_show_options, 0, "<StockItem>", GTK_STOCK_PREFERENCES},
222 {N_("New"), NULL, NULL, 0, "<Branch>"},
223 {">" N_("Directory"), NULL, new_directory, 0, NULL},
224 {">" N_("Blank file"), NULL, new_file, 0, "<StockItem>", GTK_STOCK_NEW},
225 {N_("Window"), NULL, NULL, 0, "<Branch>"},
226 {">" N_("Parent, New Window"), NULL, open_parent, 0, "<StockItem>", GTK_STOCK_GO_UP},
227 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
228 {">" N_("New Window"), NULL, new_window, 0, NULL},
229 {">" N_("Home Directory"), NULL, home_directory, 0, "<StockItem>", GTK_STOCK_HOME},
230 {">" N_("Show Bookmarks"), "<Ctrl>B", show_bookmarks, 0, "<StockItem>", ROX_STOCK_BOOKMARKS},
231 {">" N_("Follow Symbolic Links"), NULL, follow_symlinks, 0, NULL},
232 {">" N_("Resize Window"), NULL, resize, 0, NULL},
233 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
235 {">" N_("Close Window"), NULL, close_window, 0, "<StockItem>", GTK_STOCK_CLOSE},
236 {">", NULL, NULL, 0, "<Separator>"},
237 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
238 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
239 {">" N_("Xterm Here"), NULL, xterm_here, FALSE, NULL},
240 {">" N_("Switch to xterm"), NULL, xterm_here, TRUE, NULL},
241 {N_("Help"), NULL, NULL, 0, "<Branch>"},
242 {">" N_("About ROX-Filer..."), NULL, menu_rox_help, HELP_ABOUT, NULL},
243 {">" N_("Show Help Files"), "F1", menu_rox_help, HELP_DIR, "<StockItem>", GTK_STOCK_HELP},
244 {">" N_("Manual"), NULL, menu_rox_help, HELP_MANUAL, NULL},
248 #define GET_MENU_ITEM(var, menu) \
249 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
251 #define GET_SMENU_ITEM(var, menu, sub) \
252 do { \
253 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
254 var = gtk_item_factory_get_widget(item_factory, tmp); \
255 g_free(tmp); \
256 } while (0)
258 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
259 do { \
260 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
261 var = gtk_item_factory_get_widget(item_factory, tmp); \
262 g_free(tmp); \
263 } while (0)
265 /* Returns TRUE if the keys were installed (first call only) */
266 gboolean ensure_filer_menu(void)
268 GList *items;
269 guchar *tmp;
270 GtkWidget *item;
271 GtkItemFactory *item_factory;
273 if (!filer_keys_need_init)
274 return FALSE;
275 filer_keys_need_init = FALSE;
277 item_factory = menu_create(filer_menu_def,
278 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
279 "<filer>", filer_keys);
281 GET_MENU_ITEM(filer_menu, "filer");
282 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
283 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
284 GET_SSMENU_ITEM(filer_reverse_menu, "filer", "Display", "Reversed");
285 GET_SSMENU_ITEM(filer_auto_size_menu, "filer", "Display", "Automatic");
286 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
287 "Show Thumbnails");
289 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
290 GET_SSMENU_ITEM(item, "filer", "Window", "Follow Symbolic Links");
291 filer_follow_sym = GTK_BIN(item)->child;
293 /* File '' label... */
294 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
295 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
296 g_list_free(items);
298 /* Shift Open... label */
299 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
300 file_shift_item = GTK_BIN(g_list_nth(items, 5)->data)->child;
301 g_list_free(items);
303 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
304 filer_new_window = GTK_BIN(item)->child;
306 g_signal_connect(filer_menu, "unmap_event",
307 G_CALLBACK(menu_closed), NULL);
308 g_signal_connect(filer_file_menu, "unmap_event",
309 G_CALLBACK(menu_closed), NULL);
311 g_signal_connect(filer_keys, "accel_changed",
312 G_CALLBACK(save_menus), NULL);
314 return TRUE;
317 void menu_init(void)
319 char *menurc;
321 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
322 if (menurc)
324 gtk_accel_map_load(menurc);
325 g_free(menurc);
328 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
329 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
330 option_add_saver(save_menus);
332 option_register_widget("menu-set-keys", set_keys_button);
334 filer_keys = gtk_accel_group_new();
337 /* Name is in the form "<panel>" */
338 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
339 const gchar *name, GtkAccelGroup *keys)
341 GtkItemFactory *item_factory;
342 GtkItemFactoryEntry *translated;
344 if (!keys)
346 keys = gtk_accel_group_new();
347 gtk_accel_group_lock(keys);
350 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
352 translated = translate_entries(def, n_entries);
353 gtk_item_factory_create_items(item_factory, n_entries,
354 translated, NULL);
355 free_translated_entries(translated, n_entries);
357 return item_factory;
360 /* Prevent the user from setting a short-cut on this item */
361 static void menuitem_no_shortcuts(GtkWidget *item)
363 /* XXX */
364 #if 0
365 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
367 _gtk_widget_set_accel_path(item, NULL, NULL);
368 null_g_free(&menuitem->accel_path);
369 #endif
372 /* Shade items that only work on single files */
373 static void shade_file_menu_items(gboolean shaded)
375 menu_set_items_shaded(filer_file_menu, shaded, 0, 3);
376 menu_set_items_shaded(filer_file_menu, shaded, 5, 2);
377 menu_set_items_shaded(filer_file_menu, shaded, 9, 2);
380 /* 'data' is an array of three ints:
381 * [ pointer_x, pointer_y, item_under_pointer ]
383 void position_menu(GtkMenu *menu, gint *x, gint *y,
384 gboolean *push_in, gpointer data)
386 int *pos = (int *) data;
387 GtkRequisition requisition;
388 GList *items, *next;
389 int y_shift = 0;
390 int item = pos[2];
392 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
394 while (item >= 0 && next)
396 int h = ((GtkWidget *) next->data)->requisition.height;
398 if (item > 0)
399 y_shift += h;
400 else
401 y_shift += h / 2;
403 next = next->next;
404 item--;
407 g_list_free(items);
409 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
411 *x = pos[0] - (requisition.width * 7 / 8);
412 *y = pos[1] - y_shift;
414 *x = CLAMP(*x, 0, screen_width - requisition.width);
415 *y = CLAMP(*y, 0, screen_height - requisition.height);
417 *push_in = FALSE;
420 GtkWidget *make_send_to_item(DirItem *ditem, const char *label,
421 MenuIconStyle style)
423 GtkWidget *item;
425 if (ditem->image && style != MIS_NONE)
427 GdkPixbuf *pixbuf;
429 switch (style)
431 case MIS_LARGE:
432 pixbuf = ditem->image->pixbuf;
433 break;
434 default:
435 if (!ditem->image->sm_pixbuf)
436 pixmap_make_small(ditem->image);
437 pixbuf = ditem->image->sm_pixbuf;
438 break;
441 item = gtk_image_menu_item_new_with_label(label);
442 /* TODO: Find a way to allow short-cuts */
443 menuitem_no_shortcuts(item);
445 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),
446 gtk_image_new_from_pixbuf(pixbuf));
447 gtk_widget_show_all(item);
449 else
450 item = gtk_menu_item_new_with_label(label);
452 return item;
455 static GList *menu_from_dir(GtkWidget *menu, const gchar *dir_name,
456 MenuIconStyle style, CallbackFn func,
457 gboolean separator, gboolean strip_ext,
458 gboolean recurse)
460 GList *widgets = NULL;
461 DirItem *ditem;
462 int i;
463 GtkWidget *item;
464 char *dname = NULL;
465 GPtrArray *names;
467 dname = pathdup(dir_name);
469 names = list_dir(dname);
470 if (!names)
471 goto out;
473 if (separator)
475 item = gtk_menu_item_new();
476 widgets = g_list_append(widgets, item);
477 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
480 for (i = 0; i < names->len; i++)
482 char *leaf = names->pdata[i];
483 gchar *fname;
485 fname = g_strconcat(dname, "/", leaf, NULL);
487 /* Strip off extension, if any */
488 if (strip_ext)
490 char *dot;
491 dot = strchr(leaf, '.');
492 if (dot)
493 *dot = '\0';
496 ditem = diritem_new("");
497 diritem_restat(fname, ditem, NULL);
499 item = make_send_to_item(ditem, leaf, style);
501 g_free(leaf);
503 /* If it is a directory (but NOT an AppDir) and we are
504 * recursing then set up a sub menu.
506 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
507 !(ditem->flags & ITEM_FLAG_APPDIR))
509 GtkWidget *sub;
510 GList *new_widgets;
512 sub = gtk_menu_new();
513 new_widgets = menu_from_dir(sub, fname, style, func,
514 separator, strip_ext, FALSE);
515 g_list_free(new_widgets);
516 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
518 else
519 g_signal_connect_swapped(item, "activate",
520 G_CALLBACK(func), fname);
522 diritem_free(ditem);
524 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
525 g_signal_connect_swapped(item, "destroy",
526 G_CALLBACK(g_free), fname);
528 widgets = g_list_append(widgets, item);
531 g_ptr_array_free(names, TRUE);
532 out:
533 g_free(dname);
535 return widgets;
538 /* Scan the templates dir and create entries for the New menu */
539 static void update_new_files_menu(MenuIconStyle style)
541 static GList *widgets = NULL;
543 gchar *templ_dname = NULL;
545 if (widgets)
547 GList *next;
549 for (next = widgets; next; next = next->next)
550 gtk_widget_destroy((GtkWidget *) next->data);
552 g_list_free(widgets);
553 widgets = NULL;
556 templ_dname = choices_find_path_load("Templates", "");
557 if (templ_dname)
559 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
560 (CallbackFn) new_file_type, TRUE, TRUE,
561 FALSE);
562 g_free(templ_dname);
564 gtk_widget_show_all(filer_new_menu);
567 /* 'item' is the number of the item to appear under the pointer. */
568 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
570 int pos[3];
571 int button = 0;
572 guint32 time = 0;
574 if (event && (event->type == GDK_BUTTON_PRESS ||
575 event->type == GDK_BUTTON_RELEASE))
577 GdkEventButton *bev = (GdkEventButton *) event;
579 pos[0] = bev->x_root;
580 pos[1] = bev->y_root;
581 button = bev->button;
582 time = bev->time;
584 else if (event && event->type == GDK_KEY_PRESS)
586 GdkEventKey *kev = (GdkEventKey *) event;
588 get_pointer_xy(pos, pos + 1);
589 time = kev->time;
591 else
592 get_pointer_xy(pos, pos + 1);
594 pos[2] = item;
596 gtk_widget_show_all(menu);
597 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
598 position_menu, (gpointer) pos, button, time);
599 select_nth_item(GTK_MENU_SHELL(menu), item);
602 /* Hide the popup menu, if any */
603 void menu_popdown(void)
605 if (popup_menu)
606 gtk_menu_popdown(GTK_MENU(popup_menu));
609 static MenuIconStyle get_menu_icon_style(void)
611 MenuIconStyle mis;
612 int display;
614 mis = o_menu_iconsize.int_value;
616 switch (mis)
618 case MIS_NONE: case MIS_SMALL: case MIS_LARGE:
619 return mis;
620 default:
621 break;
624 if (mis == MIS_CURRENT && window_with_focus)
626 switch (window_with_focus->display_style)
628 case HUGE_ICONS:
629 case LARGE_ICONS:
630 return MIS_LARGE;
631 case SMALL_ICONS:
632 return MIS_SMALL;
633 default:
634 break;
638 display = o_display_size.int_value;
639 switch (display)
641 case HUGE_ICONS:
642 case LARGE_ICONS:
643 return MIS_LARGE;
644 case SMALL_ICONS:
645 return MIS_SMALL;
646 default:
647 break;
650 return MIS_SMALL;
653 /* iter->peek() is the clicked item, or NULL if none */
654 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, ViewIter *iter)
656 DirItem *file_item = NULL;
657 GdkModifierType state = 0;
658 int n_selected;
659 int n_added = 0;
661 n_selected = view_count_selected(filer_window->view);
663 ensure_filer_menu();
665 updating_menu++;
667 /* Remove previous AppMenu, if any */
668 appmenu_remove();
670 window_with_focus = filer_window;
672 if (!event)
673 event = gtk_get_current_event();
675 if (event->type == GDK_BUTTON_PRESS)
676 state = ((GdkEventButton *) event)->state;
677 else if (event->type == GDK_KEY_PRESS)
678 state = ((GdkEventKey *) event)->state;
680 if (n_selected == 0 && iter && iter->peek(iter) != NULL)
682 filer_window->temp_item_selected = TRUE;
683 view_set_selected(filer_window->view, iter, TRUE);
684 n_selected = view_count_selected(filer_window->view);
686 else
688 filer_window->temp_item_selected = FALSE;
691 /* Short-cut to the Send To menu */
692 if (state & GDK_SHIFT_MASK)
694 GList *paths;
696 updating_menu--;
698 if (n_selected == 0)
700 report_error(
701 _("You should Shift+Menu click over a file to "
702 "send it somewhere"));
703 return;
706 paths = filer_selected_items(filer_window);
708 show_send_to_menu(paths, event); /* (paths eaten) */
710 return;
714 GtkWidget *file_label, *file_menu;
715 GString *buffer;
716 DirItem *item;
718 file_label = filer_file_item;
719 file_menu = filer_file_menu;
720 gtk_check_menu_item_set_active(
721 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
722 filer_window->show_thumbs);
723 gtk_check_menu_item_set_active(
724 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
725 filer_window->show_hidden);
726 gtk_check_menu_item_set_active(
727 GTK_CHECK_MENU_ITEM(filer_reverse_menu),
728 filer_window->sort_order != GTK_SORT_ASCENDING);
729 gtk_check_menu_item_set_active(
730 GTK_CHECK_MENU_ITEM(filer_auto_size_menu),
731 filer_window->display_style_wanted == AUTO_SIZE_ICONS);
732 buffer = g_string_new(NULL);
734 switch (n_selected)
736 case 0:
737 g_string_assign(buffer, _("Next Click"));
738 shade_file_menu_items(FALSE);
739 break;
740 case 1:
741 item = filer_selected_item(filer_window);
742 if (!item->image)
743 dir_update_item(filer_window->directory,
744 item->leafname);
745 shade_file_menu_items(FALSE);
746 file_item = filer_selected_item(filer_window);
747 g_string_printf(buffer, _("%s '%s'"),
748 basetype_name(file_item),
749 g_utf8_validate(file_item->leafname,
750 -1, NULL)
751 ? file_item->leafname
752 : _("(bad utf-8)"));
753 if (!can_set_run_action(file_item))
754 menu_set_items_shaded(filer_file_menu,
755 TRUE, 9, 1);
756 break;
757 default:
758 shade_file_menu_items(TRUE);
759 g_string_printf(buffer, _("%d items"),
760 n_selected);
761 break;
763 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
764 g_string_free(buffer, TRUE);
766 menu_show_shift_action(file_shift_item, file_item,
767 n_selected == 0);
768 if (file_item)
769 n_added = appmenu_add(make_path(filer_window->sym_path,
770 file_item->leafname),
771 file_item, filer_file_menu);
774 update_new_files_menu(get_menu_icon_style());
776 gtk_widget_set_sensitive(filer_new_window,
777 !o_unique_filer_windows.int_value);
778 gtk_widget_set_sensitive(filer_follow_sym,
779 strcmp(filer_window->sym_path, filer_window->real_path) != 0);
781 popup_menu = (state & GDK_CONTROL_MASK)
782 ? filer_file_menu
783 : filer_menu;
785 updating_menu--;
787 show_popup_menu(popup_menu, event,
788 popup_menu == filer_file_menu ? (n_added + 7) : 1);
791 static void menu_closed(GtkWidget *widget)
793 if (window_with_focus == NULL || widget != popup_menu)
794 return; /* Close panel item chosen? */
796 popup_menu = NULL;
798 if (window_with_focus->temp_item_selected)
800 view_clear_selection(window_with_focus->view);
801 window_with_focus->temp_item_selected = FALSE;
804 appmenu_remove();
807 static void target_callback(FilerWindow *filer_window,
808 ViewIter *iter,
809 gpointer action)
811 g_return_if_fail(filer_window != NULL);
813 window_with_focus = filer_window;
815 /* Don't grab the primary selection */
816 filer_window->temp_item_selected = TRUE;
818 view_wink_item(filer_window->view, iter);
819 view_select_only(filer_window->view, iter);
820 file_op(NULL, GPOINTER_TO_INT(action), NULL);
822 view_clear_selection(filer_window->view);
823 filer_window->temp_item_selected = FALSE;
826 /* Set the text of the 'Shift Open...' menu item.
827 * If icon is NULL, reset the text and also shade it, unless 'next'.
829 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
831 guchar *shift_action = NULL;
833 if (item)
835 if (item->flags & ITEM_FLAG_MOUNT_POINT)
837 if (item->flags & ITEM_FLAG_MOUNTED)
838 shift_action = N_("Unmount");
839 else
840 shift_action = N_("Open unmounted");
842 else if (item->flags & ITEM_FLAG_SYMLINK)
843 shift_action = N_("Show Target");
844 else if (item->base_type == TYPE_DIRECTORY)
845 shift_action = N_("Look Inside");
846 else if (item->base_type == TYPE_FILE)
847 shift_action = N_("Open As Text");
849 gtk_label_set_text(GTK_LABEL(menu_item),
850 shift_action ? _(shift_action)
851 : _("Shift Open"));
852 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
855 /* Actions */
857 static void view_type(gpointer data, guint action, GtkWidget *widget)
859 ViewType view_type = (ViewType) action;
861 g_return_if_fail(window_with_focus != NULL);
863 if (view_type == VIEW_TYPE_COLLECTION)
864 display_set_layout(window_with_focus,
865 window_with_focus->display_style_wanted,
866 DETAILS_NONE, FALSE);
868 filer_set_view_type(window_with_focus, (ViewType) action);
871 static void change_size(gpointer data, guint action, GtkWidget *widget)
873 g_return_if_fail(window_with_focus != NULL);
875 display_change_size(window_with_focus, action == 1);
878 static void change_size_auto(gpointer data, guint action, GtkWidget *widget)
880 g_return_if_fail(window_with_focus != NULL);
882 if (updating_menu)
883 return;
885 if (window_with_focus->display_style_wanted == AUTO_SIZE_ICONS)
886 display_set_layout(window_with_focus,
887 window_with_focus->display_style,
888 window_with_focus->details_type, FALSE);
889 else
890 display_set_layout(window_with_focus, AUTO_SIZE_ICONS,
891 window_with_focus->details_type, FALSE);
894 static void set_with(gpointer data, guint action, GtkWidget *widget)
896 DisplayStyle size;
898 g_return_if_fail(window_with_focus != NULL);
900 size = window_with_focus->display_style_wanted;
902 filer_set_view_type(window_with_focus, VIEW_TYPE_COLLECTION);
903 display_set_layout(window_with_focus, size, action, FALSE);
906 static void set_sort(gpointer data, guint action, GtkWidget *widget)
908 if (updating_menu)
909 return;
911 g_return_if_fail(window_with_focus != NULL);
913 display_set_sort_type(window_with_focus, action, GTK_SORT_ASCENDING);
916 static void reverse_sort(gpointer data, guint action, GtkWidget *widget)
918 GtkSortType order;
920 if (updating_menu)
921 return;
923 g_return_if_fail(window_with_focus != NULL);
925 order = window_with_focus->sort_order;
926 if (order == GTK_SORT_ASCENDING)
927 order = GTK_SORT_DESCENDING;
928 else
929 order = GTK_SORT_ASCENDING;
931 display_set_sort_type(window_with_focus, window_with_focus->sort_type,
932 order);
935 static void hidden(gpointer data, guint action, GtkWidget *widget)
937 if (updating_menu)
938 return;
940 g_return_if_fail(window_with_focus != NULL);
942 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
945 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
947 if (updating_menu)
948 return;
950 g_return_if_fail(window_with_focus != NULL);
952 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
955 static void refresh(gpointer data, guint action, GtkWidget *widget)
957 g_return_if_fail(window_with_focus != NULL);
959 filer_refresh(window_with_focus);
962 static void delete(FilerWindow *filer_window)
964 GList *paths;
965 paths = filer_selected_items(filer_window);
966 action_delete(paths);
967 destroy_glist(&paths);
970 static void usage(FilerWindow *filer_window)
972 GList *paths;
973 paths = filer_selected_items(filer_window);
974 action_usage(paths);
975 destroy_glist(&paths);
978 static void chmod_items(FilerWindow *filer_window)
980 GList *paths;
981 paths = filer_selected_items(filer_window);
982 action_chmod(paths, FALSE, NULL);
983 destroy_glist(&paths);
986 static void set_type_items(FilerWindow *filer_window)
988 GList *paths;
989 paths = filer_selected_items(filer_window);
990 action_settype(paths, FALSE, NULL);
991 destroy_glist(&paths);
994 static void find(FilerWindow *filer_window)
996 GList *paths;
997 paths = filer_selected_items(filer_window);
998 action_find(paths);
999 destroy_glist(&paths);
1002 /* This creates a new savebox widget, and allows the user to pick a new path
1003 * for the file.
1004 * Once the new path has been picked, the callback will be called with
1005 * both the current and new paths.
1006 * NOTE: This function unrefs 'image'!
1008 static void savebox_show(const gchar *action, const gchar *path,
1009 MaskedPixmap *image, SaveCb callback,
1010 GdkDragAction dnd_action)
1012 GtkWidget *savebox = NULL;
1013 GtkWidget *check_relative = NULL;
1015 g_return_if_fail(image != NULL);
1017 savebox = gtk_savebox_new(action);
1018 gtk_savebox_set_action(GTK_SAVEBOX(savebox), dnd_action);
1020 if (callback == link_cb)
1022 check_relative = gtk_check_button_new_with_mnemonic(
1023 _("_Relative link"));
1024 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
1025 TRUE);
1027 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
1028 gtk_tooltips_set_tip(tooltips, check_relative,
1029 _("If on, the symlink will store the path from the "
1030 "symlink to the target file. Use this if the symlink "
1031 "and the target will be moved together.\n"
1032 "If off, the path from the root directory is stored - "
1033 "use this if the symlink may move but the target will "
1034 "stay put."), NULL);
1035 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1036 check_relative, FALSE, TRUE, 0);
1037 gtk_widget_show(check_relative);
1040 g_signal_connect(savebox, "save_to_file",
1041 G_CALLBACK(save_to_file), NULL);
1043 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1044 g_strdup(path), g_free);
1045 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1046 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1048 gtk_window_set_title(GTK_WINDOW(savebox), action);
1050 if (g_utf8_validate(path, -1, NULL))
1051 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1052 else
1054 gchar *u8, *dir;
1055 dir = g_path_get_dirname(path);
1056 u8 = to_utf8(g_basename(path));
1057 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox),
1058 make_path(dir, u8));
1059 g_free(u8);
1060 g_free(dir);
1062 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixbuf);
1063 g_object_unref(image);
1065 gtk_widget_show(savebox);
1068 static gint save_to_file(GObject *savebox,
1069 const gchar *pathname, gpointer data)
1071 SaveCb callback;
1072 const gchar *current_path;
1074 callback = g_object_get_data(savebox, "action_callback");
1075 current_path = g_object_get_data(savebox, "current_path");
1077 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1078 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1080 return callback(savebox, current_path, pathname)
1081 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1084 static gboolean copy_cb(GObject *savebox,
1085 const gchar *current, const gchar *new)
1087 return action_with_leaf(action_copy, current, new);
1090 static gboolean action_with_leaf(ActionFn action,
1091 const gchar *current, const gchar *new)
1093 const char *leaf;
1094 char *new_dir;
1095 GList *local_paths;
1097 if (new[0] != '/')
1099 report_error(_("New pathname is not absolute"));
1100 return FALSE;
1103 if (new[strlen(new) - 1] == '/')
1105 new_dir = g_strdup(new);
1106 leaf = NULL;
1108 else
1110 const gchar *slash;
1112 slash = strrchr(new, '/');
1113 new_dir = g_strndup(new, slash - new);
1114 leaf = slash + 1;
1117 local_paths = g_list_append(NULL, (gchar *) current);
1118 action(local_paths, new_dir, leaf, -1);
1119 g_list_free(local_paths);
1121 g_free(new_dir);
1123 return TRUE;
1126 /* Open a savebox to act on the selected file.
1127 * Call 'callback' later to perform the operation.
1129 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1130 const gchar *action, SaveCb callback,
1131 GdkDragAction dnd_action)
1133 g_object_ref(image);
1134 savebox_show(action, path, image, callback, dnd_action);
1137 static gboolean rename_cb(GObject *savebox,
1138 const gchar *current, const gchar *new)
1140 return action_with_leaf(action_move, current, new);
1143 static gboolean link_cb(GObject *savebox,
1144 const gchar *initial, const gchar *path)
1146 GtkToggleButton *check_relative;
1147 struct stat info;
1148 int err;
1149 gchar *link_path;
1151 check_relative = g_object_get_data(savebox, "check_relative");
1153 if (gtk_toggle_button_get_active(check_relative))
1154 link_path = get_relative_path(path, initial);
1155 else
1156 link_path = g_strdup(initial);
1158 if (mc_lstat(path, &info) == 0 && S_ISLNK(info.st_mode))
1160 GtkWidget *box, *button;
1161 gint ans;
1163 box = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_QUESTION,
1164 GTK_BUTTONS_CANCEL,
1165 _("Symlink from '%s' already exists. "
1166 "Replace it with a link to '%s'?"),
1167 path, link_path);
1169 gtk_window_set_position(GTK_WINDOW(box), GTK_WIN_POS_MOUSE);
1171 button = button_new_mixed(GTK_STOCK_YES, _("_Replace"));
1172 gtk_widget_show(button);
1173 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1174 gtk_dialog_add_action_widget(GTK_DIALOG(box),
1175 button, GTK_RESPONSE_OK);
1176 gtk_dialog_set_default_response(GTK_DIALOG(box),
1177 GTK_RESPONSE_OK);
1179 ans = gtk_dialog_run(GTK_DIALOG(box));
1180 gtk_widget_destroy(box);
1182 if (ans != GTK_RESPONSE_OK)
1184 g_free(link_path);
1185 return FALSE;
1188 unlink(path);
1191 err = symlink(link_path, path);
1192 g_free(link_path);
1194 if (err)
1196 report_error("symlink: %s", g_strerror(errno));
1197 return FALSE;
1200 dir_check_this(path);
1202 return TRUE;
1205 static void run_action(DirItem *item)
1207 if (can_set_run_action(item))
1208 type_set_handler_dialog(item->mime_type);
1209 else
1210 report_error(
1211 _("You can only set the run action for a "
1212 "regular file"));
1215 void open_home(gpointer data, guint action, GtkWidget *widget)
1217 filer_opendir(home_dir, NULL, NULL);
1220 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1222 gchar *path;
1224 path = g_strconcat(filer_window->sym_path,
1225 "/", item->leafname, "#", NULL);
1227 filer_change_to(filer_window, path, NULL);
1228 g_free(path);
1231 static void select_all(gpointer data, guint action, GtkWidget *widget)
1233 g_return_if_fail(window_with_focus != NULL);
1235 window_with_focus->temp_item_selected = FALSE;
1236 view_select_all(window_with_focus->view);
1239 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1241 g_return_if_fail(window_with_focus != NULL);
1243 window_with_focus->temp_item_selected = FALSE;
1244 view_clear_selection(window_with_focus->view);
1247 static gboolean invert_cb(ViewIter *iter, gpointer data)
1249 return !view_get_selected((ViewIface *) data, iter);
1252 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1254 g_return_if_fail(window_with_focus != NULL);
1256 window_with_focus->temp_item_selected = FALSE;
1258 view_select_if(window_with_focus->view, invert_cb,
1259 window_with_focus->view);
1262 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1264 GtkWidget *win;
1266 win = options_show();
1268 if (win)
1270 number_of_windows++;
1271 g_signal_connect(win, "destroy",
1272 G_CALLBACK(one_less_window), NULL);
1276 static gboolean new_directory_cb(GObject *savebox,
1277 const gchar *initial, const gchar *path)
1279 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1281 report_error("mkdir: %s", g_strerror(errno));
1282 return FALSE;
1285 dir_check_this(path);
1287 if (filer_exists(window_with_focus))
1289 guchar *leaf;
1290 leaf = strrchr(path, '/');
1291 if (leaf)
1292 display_set_autoselect(window_with_focus, leaf + 1);
1295 return TRUE;
1298 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1300 g_return_if_fail(window_with_focus != NULL);
1302 savebox_show(_("Create"),
1303 make_path(window_with_focus->sym_path, _("NewDir")),
1304 type_to_icon(inode_directory), new_directory_cb,
1305 GDK_ACTION_COPY);
1308 static gboolean new_file_cb(GObject *savebox,
1309 const gchar *initial, const gchar *path)
1311 int fd;
1313 fd = open(path, O_CREAT | O_EXCL, 0666);
1315 if (fd == -1)
1317 report_error(_("Error creating '%s': %s"),
1318 path, g_strerror(errno));
1319 return FALSE;
1322 if (close(fd))
1323 report_error(_("Error creating '%s': %s"),
1324 path, g_strerror(errno));
1326 dir_check_this(path);
1328 if (filer_exists(window_with_focus))
1330 guchar *leaf;
1331 leaf = strrchr(path, '/');
1332 if (leaf)
1333 display_set_autoselect(window_with_focus, leaf + 1);
1336 return TRUE;
1339 static void new_file(gpointer data, guint action, GtkWidget *widget)
1341 g_return_if_fail(window_with_focus != NULL);
1343 savebox_show(_("Create"),
1344 make_path(window_with_focus->sym_path, _("NewFile")),
1345 type_to_icon(text_plain),
1346 new_file_cb, GDK_ACTION_COPY);
1349 static gboolean new_file_type_cb(GObject *savebox,
1350 const gchar *initial, const gchar *path)
1352 const gchar *oleaf, *leaf;
1353 gchar *templ, *templ_dname, *dest;
1354 GList *paths;
1356 /* We can work out the template path from the initial name */
1357 oleaf = g_basename(initial);
1358 templ_dname = choices_find_path_load("Templates", "");
1359 if (!templ_dname)
1361 report_error(
1362 _("Error creating file: could not find the template for %s"),
1363 oleaf);
1364 return FALSE;
1367 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1368 g_free(templ_dname);
1370 dest = g_path_get_dirname(path);
1371 leaf = g_basename(path);
1372 paths = g_list_append(NULL, templ);
1374 action_copy(paths, dest, leaf, TRUE);
1376 g_list_free(paths);
1377 g_free(dest);
1378 g_free(templ);
1380 if (filer_exists(window_with_focus))
1381 display_set_autoselect(window_with_focus, leaf);
1383 return TRUE;
1386 static void do_send_to(gchar *templ)
1388 g_return_if_fail(send_to_paths != NULL);
1390 run_with_files(templ, send_to_paths);
1393 static void new_file_type(gchar *templ)
1395 const gchar *leaf;
1396 MIME_type *type;
1398 g_return_if_fail(window_with_focus != NULL);
1400 leaf = g_basename(templ);
1401 type = type_get_type(templ);
1403 savebox_show(_("Create"),
1404 make_path(window_with_focus->sym_path, leaf),
1405 type_to_icon(type),
1406 new_file_type_cb, GDK_ACTION_COPY);
1409 static void customise_send_to(gpointer data)
1411 GPtrArray *path;
1412 guchar *save;
1413 GString *dirs;
1414 int i;
1416 dirs = g_string_new(NULL);
1418 path = choices_list_dirs("");
1419 for (i = 0; i < path->len; i++)
1421 guchar *old = (guchar *) path->pdata[i];
1423 g_string_append(dirs, old);
1424 g_string_append(dirs, "SendTo\n");
1426 choices_free_list(path);
1428 save = choices_find_path_save("", "SendTo", TRUE);
1429 if (save)
1430 mkdir(save, 0777);
1432 info_message(
1433 _("The `Send To' menu provides a quick way to send some files "
1434 "to an application. The applications listed are those in "
1435 "the following directories:\n\n%s\n%s\n"
1436 "The `Send To' menu may be opened by Shift+Menu clicking "
1437 "over a file.\n\n"
1438 "Advanced use:\n"
1439 "You can also create subdirectories called "
1440 "`.text_html', `.text', etc which will only be "
1441 "shown for files of that type. `.group' is shown "
1442 "only when multiple files are selected."),
1443 dirs->str,
1444 save ? _("I'll show you your SendTo directory now; you should "
1445 "symlink (Ctrl+Shift drag) any applications you want "
1446 "into it.")
1447 : _("Your CHOICESPATH variable setting prevents "
1448 "customisations - sorry."));
1450 g_string_free(dirs, TRUE);
1452 if (save)
1453 filer_opendir(save, NULL, NULL);
1456 /* Add everything in the directory <Choices>/SendTo/[.type[_subtype]]
1457 * to the menu.
1459 static void add_sendto(GtkWidget *menu, const gchar *type, const gchar *subtype)
1461 gchar *searchdir;
1462 GPtrArray *paths;
1463 int i;
1465 if (subtype)
1466 searchdir = g_strdup_printf("SendTo/.%s_%s", type, subtype);
1467 else if (type)
1468 searchdir = g_strdup_printf("SendTo/.%s", type);
1469 else
1470 searchdir = g_strdup("SendTo");
1472 paths = choices_list_dirs(searchdir);
1473 g_free(searchdir);
1475 for (i = 0; i < paths->len; i++)
1477 GList *widgets = NULL;
1478 guchar *dir = (guchar *) paths->pdata[i];
1480 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1481 (CallbackFn) do_send_to,
1482 FALSE, FALSE, TRUE);
1484 if (widgets)
1485 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1486 gtk_menu_item_new());
1488 g_list_free(widgets); /* TODO: Get rid of this */
1491 choices_free_list(paths);
1494 /* Scan the SendTo dir and create and show the Send To menu.
1495 * The 'paths' list and every path in it is claimed, and will be
1496 * freed later -- don't free it yourself!
1498 static void show_send_to_menu(GList *paths, GdkEvent *event)
1500 GtkWidget *menu, *item;
1502 menu = gtk_menu_new();
1504 if (g_list_length(paths) == 1)
1506 DirItem *item;
1508 item = diritem_new("");
1509 diritem_restat(paths->data, item, NULL);
1511 add_sendto(menu,
1512 item->mime_type->media_type,
1513 item->mime_type->subtype);
1515 add_sendto(menu, item->mime_type->media_type, NULL);
1517 diritem_free(item);
1519 else
1520 add_sendto(menu, "group", NULL);
1522 add_sendto(menu, NULL, NULL);
1524 item = gtk_menu_item_new_with_label(_("Customise"));
1525 g_signal_connect_swapped(item, "activate",
1526 G_CALLBACK(customise_send_to), NULL);
1527 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1529 if (send_to_paths)
1530 destroy_glist(&send_to_paths);
1532 send_to_paths = paths;
1534 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1536 popup_menu = menu;
1537 show_popup_menu(menu, event, 0);
1540 static void send_to(FilerWindow *filer_window)
1542 GList *paths;
1543 GdkEvent *event;
1545 paths = filer_selected_items(filer_window);
1546 event = gtk_get_current_event();
1548 /* Eats paths */
1549 show_send_to_menu(paths, event);
1551 gdk_event_free(event);
1554 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1556 const char *argv[] = {"sh", "-c", NULL, NULL};
1557 gboolean close = action;
1559 argv[2] = o_menu_xterm.value;
1561 g_return_if_fail(window_with_focus != NULL);
1563 if (rox_spawn(window_with_focus->sym_path, argv) && close)
1564 gtk_widget_destroy(window_with_focus->window);
1567 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1569 g_return_if_fail(window_with_focus != NULL);
1571 filer_change_to(window_with_focus, home_dir, NULL);
1574 static void show_bookmarks(gpointer data, guint action, GtkWidget *widget)
1576 g_return_if_fail(window_with_focus != NULL);
1578 bookmarks_show_menu(window_with_focus);
1581 static void follow_symlinks(gpointer data, guint action, GtkWidget *widget)
1583 g_return_if_fail(window_with_focus != NULL);
1585 if (strcmp(window_with_focus->real_path, window_with_focus->sym_path))
1586 filer_change_to(window_with_focus,
1587 window_with_focus->real_path, NULL);
1588 else
1589 delayed_error(_("This is already the canonical name "
1590 "for this directory."));
1593 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1595 g_return_if_fail(window_with_focus != NULL);
1597 filer_open_parent(window_with_focus);
1600 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1602 g_return_if_fail(window_with_focus != NULL);
1604 change_to_parent(window_with_focus);
1607 static void resize(gpointer data, guint action, GtkWidget *widget)
1609 g_return_if_fail(window_with_focus != NULL);
1611 view_autosize(window_with_focus->view);
1614 static void new_window(gpointer data, guint action, GtkWidget *widget)
1616 g_return_if_fail(window_with_focus != NULL);
1618 if (o_unique_filer_windows.int_value)
1620 report_error(_("You can't open a second view onto "
1621 "this directory because the `Unique Windows' option "
1622 "is turned on in the Options window."));
1624 else
1625 filer_opendir(window_with_focus->sym_path, window_with_focus, NULL);
1628 static void close_window(gpointer data, guint action, GtkWidget *widget)
1630 g_return_if_fail(window_with_focus != NULL);
1632 if (!filer_window_delete(window_with_focus->window, NULL,
1633 window_with_focus))
1634 gtk_widget_destroy(window_with_focus->window);
1637 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1639 MiniType type = (MiniType) action;
1641 g_return_if_fail(window_with_focus != NULL);
1643 /* Item needs to remain selected... */
1644 if (type == MINI_SHELL)
1645 window_with_focus->temp_item_selected = FALSE;
1647 minibuffer_show(window_with_focus, type);
1650 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1652 if (action == HELP_ABOUT)
1653 infobox_new(app_dir);
1654 else if (action == HELP_DIR)
1655 filer_opendir(make_path(app_dir, "Help"), NULL, NULL);
1656 else if (action == HELP_MANUAL)
1658 gchar *manual = NULL;
1660 if (current_lang)
1662 manual = g_strconcat(app_dir, "/Help/Manual-",
1663 current_lang, ".html", NULL);
1664 if (access(manual, F_OK))
1665 null_g_free(&manual);
1668 if (!manual)
1669 manual = g_strconcat(app_dir,
1670 "/Help/Manual.html", NULL);
1672 run_by_path(manual);
1674 g_free(manual);
1676 else
1677 g_warning("Unknown help action %d\n", action);
1680 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1681 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1683 GList *items, *item;
1685 items = gtk_container_get_children(GTK_CONTAINER(menu));
1687 item = g_list_nth(items, from);
1688 while (item && n--)
1690 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1691 item = item->next;
1693 g_list_free(items);
1696 static void save_menus(void)
1698 char *menurc;
1700 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1701 if (menurc)
1703 gtk_accel_map_save(menurc);
1704 g_free(menurc);
1708 static void select_nth_item(GtkMenuShell *shell, int n)
1710 GList *items;
1711 GtkWidget *item;
1713 items = gtk_container_get_children(GTK_CONTAINER(shell));
1714 item = g_list_nth_data(items, n);
1716 g_return_if_fail(item != NULL);
1718 g_list_free(items);
1720 gtk_menu_shell_select_item(shell, item);
1723 static void file_op(gpointer data, FileOp action, GtkWidget *unused)
1725 DirItem *item;
1726 const guchar *path;
1727 int n_selected;
1728 ViewIter iter;
1730 g_return_if_fail(window_with_focus != NULL);
1732 n_selected = view_count_selected(window_with_focus->view);
1734 if (n_selected < 1)
1736 const char *prompt;
1738 switch (action)
1740 case FILE_COPY_ITEM:
1741 prompt = _("Copy ... ?");
1742 break;
1743 case FILE_RENAME_ITEM:
1744 prompt = _("Rename ... ?");
1745 break;
1746 case FILE_LINK_ITEM:
1747 prompt = _("Symlink ... ?");
1748 break;
1749 case FILE_OPEN_FILE:
1750 prompt = _("Shift Open ... ?");
1751 break;
1752 case FILE_PROPERTIES:
1753 prompt = _("Properties of ... ?");
1754 break;
1755 case FILE_SET_TYPE:
1756 prompt = _("Set type of ... ?");
1757 break;
1758 case FILE_RUN_ACTION:
1759 prompt = _("Set run action for ... ?");
1760 break;
1761 case FILE_SET_ICON:
1762 prompt = _("Set icon for ... ?");
1763 break;
1764 case FILE_SEND_TO:
1765 prompt = _("Send ... to ... ?");
1766 break;
1767 case FILE_DELETE:
1768 prompt = _("DELETE ... ?");
1769 break;
1770 case FILE_USAGE:
1771 prompt = _("Count the size of ... ?");
1772 break;
1773 case FILE_CHMOD_ITEMS:
1774 prompt = _("Set permissions on ... ?");
1775 break;
1776 case FILE_FIND:
1777 prompt = _("Search inside ... ?");
1778 break;
1779 case FILE_OPEN_VFS_AVFS:
1780 prompt = _("Look inside ... ?");
1781 break;
1782 default:
1783 g_warning("Unknown action!");
1784 return;
1786 filer_target_mode(window_with_focus, target_callback,
1787 GINT_TO_POINTER(action), prompt);
1788 return;
1791 switch (action)
1793 case FILE_SEND_TO:
1794 send_to(window_with_focus);
1795 return;
1796 case FILE_DELETE:
1797 delete(window_with_focus);
1798 return;
1799 case FILE_USAGE:
1800 usage(window_with_focus);
1801 return;
1802 case FILE_CHMOD_ITEMS:
1803 chmod_items(window_with_focus);
1804 return;
1805 case FILE_SET_TYPE:
1806 set_type_items(window_with_focus);
1807 return;
1808 case FILE_FIND:
1809 find(window_with_focus);
1810 return;
1811 case FILE_PROPERTIES:
1813 GList *items;
1815 items = filer_selected_items(window_with_focus);
1816 infobox_show_list(items);
1817 destroy_glist(&items);
1818 return;
1820 default:
1821 break;
1824 /* All the following actions require exactly one file selected */
1826 if (n_selected > 1)
1828 report_error(_("You cannot do this to more than "
1829 "one item at a time"));
1830 return;
1833 view_get_iter(window_with_focus->view, &iter, VIEW_ITER_SELECTED);
1835 item = iter.next(&iter);
1836 g_return_if_fail(item != NULL);
1837 /* iter may be passed to filer_openitem... */
1839 if (!item->image)
1840 item = dir_update_item(window_with_focus->directory,
1841 item->leafname);
1843 if (!item)
1845 report_error(_("Item no longer exists!"));
1846 return;
1849 path = make_path(window_with_focus->sym_path, item->leafname);
1851 switch (action)
1853 case FILE_COPY_ITEM:
1854 src_dest_action_item(path, item->image,
1855 _("Copy"), copy_cb,
1856 GDK_ACTION_COPY);
1857 break;
1858 case FILE_RENAME_ITEM:
1859 src_dest_action_item(path, item->image,
1860 _("Rename"), rename_cb,
1861 GDK_ACTION_MOVE);
1862 break;
1863 case FILE_LINK_ITEM:
1864 src_dest_action_item(path, item->image,
1865 _("Symlink"), link_cb,
1866 GDK_ACTION_LINK);
1867 break;
1868 case FILE_OPEN_FILE:
1869 filer_openitem(window_with_focus, &iter,
1870 OPEN_SAME_WINDOW | OPEN_SHIFT);
1871 break;
1872 case FILE_RUN_ACTION:
1873 run_action(item);
1874 break;
1875 case FILE_SET_ICON:
1876 icon_set_handler_dialog(item, path);
1877 break;
1878 case FILE_OPEN_VFS_AVFS:
1879 open_vfs_avfs(window_with_focus, item);
1880 break;
1881 default:
1882 g_warning("Unknown action!");
1883 return;
1887 static void show_key_help(GtkWidget *button, gpointer data)
1889 gboolean can_change_accels;
1891 g_object_get(G_OBJECT(gtk_settings_get_default()),
1892 "gtk-can-change-accels", &can_change_accels,
1893 NULL);
1895 if (!can_change_accels)
1897 info_message(_("User-definable shortcuts are disabled by "
1898 "default in Gtk2, and you have not enabled "
1899 "them. You can turn this feature on by:\n\n"
1900 "1) using an XSettings manager, such as ROX-Session "
1901 "or gnome-settings-daemon, or\n\n"
1902 "2) adding this line to ~/.gtkrc-2.0:\n"
1903 "\tgtk-can-change-accels = 1\n"
1904 "\t(this only works if NOT using XSETTINGS)"));
1905 return;
1908 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1909 "- Open the menu over a filer window,\n"
1910 "- Move the pointer over the item you want to use,\n"
1911 "- Press the key you want attached to it.\n\n"
1912 "The key will appear next to the menu item and you can just press "
1913 "that key without opening the menu in future."));
1916 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1918 GtkWidget *button, *align;
1920 g_return_val_if_fail(option == NULL, NULL);
1922 align = gtk_alignment_new(0, 0.5, 0, 0);
1923 button = gtk_button_new_with_label(_("Set keyboard shortcuts"));
1924 gtk_container_add(GTK_CONTAINER(align), button);
1925 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1927 return g_list_append(NULL, align);