r1358: Allow subdirectories in the Send To menu (Stephen Watson).
[rox-filer.git] / ROX-Filer / src / menu.c
blob71b35708b4f89d47ce20f6b434df72f8f77e320d
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2002, 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 "collection.h"
61 #include "display.h"
63 typedef enum {
64 FILE_COPY_ITEM,
65 FILE_RENAME_ITEM,
66 FILE_LINK_ITEM,
67 FILE_OPEN_FILE,
68 FILE_HELP,
69 FILE_SHOW_FILE_INFO,
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 } FileOp;
80 typedef enum menu_icon_style {
81 MIS_NONE, MIS_SMALL, MIS_LARGE, MIS_HUGE,
82 MIS_CURRENT, /* As per current filer window */
83 MIS_DEFAULT
84 } MenuIconStyle;
86 typedef void (*ActionFn)(GList *paths,
87 const char *dest_dir, const char *leaf, int quiet);
88 typedef void MenuCallback(GtkWidget *widget, gpointer data);
90 typedef gboolean (*SaveCb)(GObject *savebox,
91 const gchar *current, const gchar *new);
93 GtkAccelGroup *filer_keys = NULL;
95 static GtkWidget *popup_menu = NULL; /* Currently open menu */
97 static gint updating_menu = 0; /* Non-zero => ignore activations */
98 static GList *send_to_paths = NULL;
100 static Option o_menu_iconsize, o_menu_xterm;
102 /* Static prototypes */
104 static void save_menus(void);
105 static void menu_closed(GtkWidget *widget);
106 static void items_sensitive(gboolean state);
107 static void savebox_show(const gchar *action, const gchar *path,
108 MaskedPixmap *image, SaveCb callback);
109 static gint save_to_file(GObject *savebox,
110 const gchar *pathname, gpointer data);
111 static gboolean action_with_leaf(ActionFn action,
112 const gchar *current, const gchar *new);
113 static gboolean link_cb(GObject *savebox,
114 const gchar *initial, const gchar *path);
115 static void select_nth_item(GtkMenuShell *shell, int n);
116 static void new_file_type(gchar *templ);
117 static void do_send_to(gchar *templ);
118 static void show_send_to_menu(GList *paths, GdkEvent *event);
119 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label);
121 /* Note that for most of these callbacks none of the arguments are used. */
123 /* (action used in these three - DetailsType) */
124 static void huge_with(gpointer data, guint action, GtkWidget *widget);
125 static void large_with(gpointer data, guint action, GtkWidget *widget);
126 static void small_with(gpointer data, guint action, GtkWidget *widget);
128 static void sort_name(gpointer data, guint action, GtkWidget *widget);
129 static void sort_type(gpointer data, guint action, GtkWidget *widget);
130 static void sort_size(gpointer data, guint action, GtkWidget *widget);
131 static void sort_date(gpointer data, guint action, GtkWidget *widget);
133 static void hidden(gpointer data, guint action, GtkWidget *widget);
134 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
135 static void refresh(gpointer data, guint action, GtkWidget *widget);
137 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
139 static void select_all(gpointer data, guint action, GtkWidget *widget);
140 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
141 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
142 static void new_directory(gpointer data, guint action, GtkWidget *widget);
143 static void new_file(gpointer data, guint action, GtkWidget *widget);
144 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
146 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent(gpointer data, guint action, GtkWidget *widget);
148 static void home_directory(gpointer data, guint action, GtkWidget *widget);
149 static void new_window(gpointer data, guint action, GtkWidget *widget);
150 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
151 static void close_window(gpointer data, guint action, GtkWidget *widget);
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"
158 static void keys_changed(gpointer data);
160 static GtkWidget *filer_menu; /* The popup filer menu */
161 static GtkWidget *filer_file_item; /* The File '' label */
162 static GtkWidget *filer_file_menu; /* The File '' menu */
163 static GtkWidget *file_shift_item; /* Shift Open label */
164 static GtkWidget *filer_hidden_menu; /* The Show Hidden 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 */
169 #undef N_
170 #define N_(x) x
172 static GtkItemFactoryEntry filer_menu_def[] = {
173 {N_("Display"), NULL, NULL, 0, "<Branch>"},
174 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
175 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
176 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
177 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
178 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
179 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
180 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
181 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
182 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
183 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
184 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
185 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
186 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
187 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
188 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
189 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
190 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
191 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
192 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
193 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
194 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
195 {">", NULL, NULL, 0, "<Separator>"},
196 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
197 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
198 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
199 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
200 {">", NULL, NULL, 0, "<Separator>"},
201 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
202 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
203 {">" N_("Refresh"), NULL, refresh, 0, NULL},
204 {N_("File"), NULL, NULL, 0, "<Branch>"},
205 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
206 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
207 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
208 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
209 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
210 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
211 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
212 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
213 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
214 {">", NULL, NULL, 0, "<Separator>"},
215 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
216 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
217 {">" N_("Disk Usage"), NULL, file_op, FILE_USAGE, NULL},
218 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
219 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
220 {N_("Select"), NULL, NULL, 0, "<Branch>"},
221 {">" N_("Select All"), NULL, select_all, 0, NULL},
222 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
223 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
224 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
225 {N_("Options..."), NULL, menu_show_options, 0, NULL},
226 {N_("New"), NULL, NULL, 0, "<Branch>"},
227 {">" N_("Directory"), NULL, new_directory, 0, NULL},
228 {">" N_("Blank file"), NULL, new_file, 0, NULL},
229 {N_("Xterm Here"), NULL, xterm_here, 0, NULL},
230 {N_("Window"), NULL, NULL, 0, "<Branch>"},
231 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
232 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
233 {">" N_("New Window"), NULL, new_window, 0, NULL},
234 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
235 {">" N_("Resize Window"), NULL, resize, 0, NULL},
236 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
238 {">" N_("Close Window"), NULL, close_window, 0, NULL},
239 {">", NULL, NULL, 0, "<Separator>"},
240 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
241 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
242 {">", NULL, NULL, 0, "<Separator>"},
243 {">" N_("Show ROX-Filer Help"), "F1", menu_rox_help, 0, NULL},
247 #define GET_MENU_ITEM(var, menu) \
248 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
250 #define GET_SMENU_ITEM(var, menu, sub) \
251 do { \
252 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
253 var = gtk_item_factory_get_widget(item_factory, tmp); \
254 g_free(tmp); \
255 } while (0)
257 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
258 do { \
259 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
260 var = gtk_item_factory_get_widget(item_factory, tmp); \
261 g_free(tmp); \
262 } while (0)
264 void ensure_filer_menu(void)
266 GList *items;
267 guchar *tmp;
268 GtkWidget *item;
269 GtkItemFactory *item_factory;
271 if (filer_keys)
272 return;
274 filer_keys = gtk_accel_group_new();
275 item_factory = menu_create(filer_menu_def,
276 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
277 "<filer>", filer_keys);
279 GET_MENU_ITEM(filer_menu, "filer");
280 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
281 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
282 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
283 "Show Thumbnails");
285 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
287 /* File '' label... */
288 items = gtk_container_get_children(GTK_CONTAINER(filer_menu));
289 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
290 g_list_free(items);
292 /* Shift Open... label */
293 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
294 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
295 g_list_free(items);
297 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
298 filer_new_window = GTK_BIN(item)->child;
300 g_signal_connect(filer_menu, "unmap_event",
301 G_CALLBACK(menu_closed), NULL);
302 g_signal_connect(filer_file_menu, "unmap_event",
303 G_CALLBACK(menu_closed), NULL);
305 g_signal_connect_object(G_OBJECT(filer_keys), "accel_changed",
306 (GCallback) keys_changed, NULL, 0);
309 void menu_init(void)
311 char *menurc;
313 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
314 if (menurc)
316 gtk_accel_map_load(menurc);
317 g_free(menurc);
320 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
321 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
322 option_add_saver(save_menus);
324 option_register_widget("menu-set-keys", set_keys_button);
327 /* Name is in the form "<panel>" */
328 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
329 const gchar *name, GtkAccelGroup *keys)
331 GtkItemFactory *item_factory;
332 GtkItemFactoryEntry *translated;
334 if (!keys)
336 keys = gtk_accel_group_new();
337 gtk_accel_group_lock(keys);
340 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
342 translated = translate_entries(def, n_entries);
343 gtk_item_factory_create_items(item_factory, n_entries,
344 translated, NULL);
345 free_translated_entries(translated, n_entries);
347 return item_factory;
350 /* Prevent the user from setting a short-cut on this item */
351 void menuitem_no_shortcuts(GtkWidget *item)
353 /* XXX */
354 #if 0
355 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
357 _gtk_widget_set_accel_path(item, NULL, NULL);
358 g_free(menuitem->accel_path);
359 menuitem->accel_path = NULL;
360 #endif
363 static void items_sensitive(gboolean state)
365 int n = 9;
366 GList *items, *item;
368 items = gtk_container_get_children(GTK_CONTAINER(filer_file_menu));
369 item = items;
371 while (item && n--)
373 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
374 item = item->next;
376 g_list_free(items);
379 /* 'data' is an array of three ints:
380 * [ pointer_x, pointer_y, item_under_pointer ]
382 void position_menu(GtkMenu *menu, gint *x, gint *y,
383 gboolean *push_in, gpointer data)
385 int *pos = (int *) data;
386 GtkRequisition requisition;
387 GList *items, *next;
388 int y_shift = 0;
389 int item = pos[2];
391 next = items = gtk_container_get_children(GTK_CONTAINER(menu));
393 while (item >= 0 && next)
395 int h = ((GtkWidget *) next->data)->requisition.height;
397 if (item > 0)
398 y_shift += h;
399 else
400 y_shift += h / 2;
402 next = next->next;
403 item--;
406 g_list_free(items);
408 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
410 *x = pos[0] - (requisition.width * 7 / 8);
411 *y = pos[1] - y_shift;
413 *x = CLAMP(*x, 0, screen_width - requisition.width);
414 *y = CLAMP(*y, 0, screen_height - requisition.height);
416 *push_in = FALSE;
419 #if 0
420 /* Used when you menu-click on the Large or Small toolbar tools */
421 void show_style_menu(FilerWindow *filer_window,
422 GdkEventButton *event,
423 GtkWidget *menu)
425 int pos[3];
427 pos[0] = event->x_root;
428 pos[1] = event->y_root;
429 pos[2] = 0;
431 window_with_focus = filer_window;
433 popup_menu = menu;
435 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
436 (gpointer) pos, event->button, event->time);
438 #endif
440 static GList *menu_from_dir(GtkWidget *menu, const gchar *dname,
441 MenuIconStyle style, CallbackFn func,
442 gboolean separator, gboolean strip_ext,
443 gboolean recurse)
445 GList *widgets = NULL;
446 DirItem *ditem;
447 DIR *dir;
448 struct dirent *ent;
449 GtkWidget *item;
451 dir = opendir(dname);
452 if (!dir)
453 goto out;
455 if (separator)
457 item = gtk_menu_item_new();
458 widgets = g_list_append(widgets, item);
459 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
462 while ((ent = readdir(dir)))
464 char *dot, *leaf;
465 GtkWidget *hbox;
466 GtkWidget *img;
467 GtkWidget *label;
468 gchar *fname;
469 GdkPixmap *icon;
470 GdkBitmap *mask;
472 /* Ignore hidden files */
473 if (ent->d_name[0] == '.')
474 continue;
476 /* Strip off extension, if any */
477 dot = strchr(ent->d_name, '.');
478 if (strip_ext && dot)
479 leaf = g_strndup(ent->d_name, dot - ent->d_name);
480 else
481 leaf = g_strdup(ent->d_name);
483 fname = g_strconcat(dname, "/", ent->d_name, NULL);
484 ditem = diritem_new("");
485 diritem_restat(fname, ditem, NULL);
487 if (ditem->image && style != MIS_NONE)
489 switch (style) {
490 case MIS_HUGE:
491 if (!ditem->image->huge_pixmap)
492 pixmap_make_huge(ditem->image);
493 icon = ditem->image->huge_pixmap;
494 mask = ditem->image->huge_mask;
495 break;
496 case MIS_LARGE:
497 icon = ditem->image->pixmap;
498 mask = ditem->image->mask;
499 break;
501 case MIS_SMALL:
502 default:
503 if (!ditem->image->sm_pixmap)
504 pixmap_make_small(ditem->image);
505 icon = ditem->image->sm_pixmap;
506 mask = ditem->image->sm_mask;
507 break;
510 item = gtk_menu_item_new();
511 /* TODO: Find a way to allow short-cuts */
512 menuitem_no_shortcuts(item);
514 hbox = gtk_hbox_new(FALSE, 2);
515 gtk_container_add(GTK_CONTAINER(item), hbox);
517 img = gtk_image_new_from_pixmap(icon, mask);
518 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
520 label = gtk_label_new(leaf);
521 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
522 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
525 else
526 item = gtk_menu_item_new_with_label(leaf);
528 /* If it is a directory (but NOT an AppDir) and we are
529 * recursing then set up a sub menu.
531 if (recurse && ditem->base_type == TYPE_DIRECTORY &&
532 !(ditem->flags & ITEM_FLAG_APPDIR))
534 GtkWidget *sub;
535 GList *new_widgets;
537 sub = gtk_menu_new();
538 new_widgets = menu_from_dir(sub, fname, style, func,
539 separator, strip_ext, FALSE);
540 g_list_free(new_widgets);
541 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), sub);
543 else
544 g_signal_connect_swapped(item, "activate",
545 G_CALLBACK(func), fname);
547 diritem_free(ditem);
548 g_free(leaf);
550 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
551 g_signal_connect_swapped(item, "destroy",
552 G_CALLBACK(g_free), fname);
554 widgets = g_list_append(widgets, item);
557 closedir(dir);
558 out:
560 return widgets;
563 /* Scan the templates dir and create entries for the New menu */
564 static void update_new_files_menu(MenuIconStyle style)
566 static GList *widgets = NULL;
568 gchar *templ_dname = NULL;
570 if (widgets)
572 GList *next;
574 for (next = widgets; next; next = next->next)
575 gtk_widget_destroy((GtkWidget *) next->data);
577 g_list_free(widgets);
578 widgets = NULL;
581 templ_dname = choices_find_path_load("Templates", "");
582 if (templ_dname)
584 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
585 (CallbackFn) new_file_type, TRUE, TRUE,
586 FALSE);
587 g_free(templ_dname);
589 gtk_widget_show_all(filer_new_menu);
592 /* 'item' is the number of the item to appear under the pointer. */
593 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
595 int pos[3];
596 int button = 0;
597 guint32 time = 0;
599 if (event && (event->type == GDK_BUTTON_PRESS ||
600 event->type == GDK_BUTTON_RELEASE))
602 GdkEventButton *bev = (GdkEventButton *) event;
604 pos[0] = bev->x_root;
605 pos[1] = bev->y_root;
606 button = bev->button;
607 time = bev->time;
609 else if (event && event->type == GDK_KEY_PRESS)
611 GdkEventKey *kev = (GdkEventKey *) event;
613 get_pointer_xy(pos, pos + 1);
614 time = kev->time;
616 else
617 get_pointer_xy(pos, pos + 1);
619 pos[2] = item;
621 gtk_widget_show_all(menu);
622 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
623 position_menu, (gpointer) pos, button, time);
624 select_nth_item(GTK_MENU_SHELL(menu), item);
627 /* Hide the popup menu, if any */
628 void menu_popdown(void)
630 if (popup_menu)
631 gtk_menu_popdown(GTK_MENU(popup_menu));
634 static MenuIconStyle get_menu_icon_style(void)
636 MenuIconStyle mis;
637 int display;
639 mis = o_menu_iconsize.int_value;
641 switch (mis)
643 case MIS_NONE: case MIS_SMALL: case MIS_LARGE: case MIS_HUGE:
644 return mis;
645 default:
646 break;
649 if (mis == MIS_CURRENT && window_with_focus)
651 switch (window_with_focus->display_style)
653 case HUGE_ICONS:
654 return MIS_HUGE;
655 case LARGE_ICONS:
656 return MIS_LARGE;
657 case SMALL_ICONS:
658 return MIS_SMALL;
659 default:
660 break;
664 display = o_display_size.int_value;
665 switch (display)
667 case HUGE_ICONS:
668 return MIS_HUGE;
669 case LARGE_ICONS:
670 return MIS_LARGE;
671 case SMALL_ICONS:
672 return MIS_SMALL;
673 default:
674 break;
677 return MIS_SMALL;
680 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
682 DirItem *file_item = NULL;
683 GdkModifierType state = 0;
685 ensure_filer_menu();
687 updating_menu++;
689 /* Remove previous AppMenu, if any */
690 appmenu_remove();
692 window_with_focus = filer_window;
694 if (event->type == GDK_BUTTON_PRESS)
695 state = ((GdkEventButton *) event)->state;
696 else if (event->type == GDK_KEY_PRESS)
697 state = ((GdkEventKey *) event)->state;
699 if (filer_window->collection->number_selected == 0 && item >= 0)
701 filer_window->temp_item_selected = TRUE;
702 collection_select_item(filer_window->collection, item);
704 else
706 filer_window->temp_item_selected = FALSE;
709 /* Short-cut to the Send To menu */
710 if (state & GDK_SHIFT_MASK)
712 GList *paths;
714 updating_menu--;
716 if (filer_window->collection->number_selected == 0)
718 report_error(
719 _("You should Shift+Menu click over a file to "
720 "send it somewhere"));
721 return;
724 paths = filer_selected_items(filer_window);
726 show_send_to_menu(paths, event); /* (paths eaten) */
728 return;
732 GtkWidget *file_label, *file_menu;
733 Collection *collection = filer_window->collection;
734 GString *buffer;
735 DirItem *item;
737 file_label = filer_file_item;
738 file_menu = filer_file_menu;
739 gtk_check_menu_item_set_active(
740 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
741 filer_window->show_thumbs);
742 gtk_check_menu_item_set_active(
743 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
744 filer_window->show_hidden);
745 buffer = g_string_new(NULL);
747 switch (collection->number_selected)
749 case 0:
750 g_string_assign(buffer, _("Next Click"));
751 items_sensitive(TRUE);
752 break;
753 case 1:
754 item = selected_item(filer_window->collection);
755 if (!item->image)
756 dir_update_item(filer_window->directory,
757 item->leafname);
758 items_sensitive(TRUE);
759 file_item = selected_item(
760 filer_window->collection);
761 g_string_sprintf(buffer, "%s '%s'",
762 basetype_name(file_item),
763 file_item->leafname);
764 if (!can_set_run_action(file_item))
765 menu_set_items_shaded(filer_file_menu,
766 TRUE, 6, 1);
767 break;
768 default:
769 items_sensitive(FALSE);
770 g_string_sprintf(buffer, _("%d items"),
771 collection->number_selected);
772 break;
774 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
775 g_string_free(buffer, TRUE);
777 menu_show_shift_action(file_shift_item, file_item,
778 collection->number_selected == 0);
779 if (file_item)
780 appmenu_add(make_path(filer_window->path,
781 file_item->leafname)->str,
782 file_item, filer_file_menu);
785 update_new_files_menu(get_menu_icon_style());
787 gtk_widget_set_sensitive(filer_new_window,
788 !o_unique_filer_windows.int_value);
790 popup_menu = (state & GDK_CONTROL_MASK)
791 ? filer_file_menu
792 : filer_menu;
794 updating_menu--;
796 show_popup_menu(popup_menu, event,
797 popup_menu == filer_file_menu ? 5 : 1);
800 static void menu_closed(GtkWidget *widget)
802 if (window_with_focus == NULL || widget != popup_menu)
803 return; /* Close panel item chosen? */
805 popup_menu = NULL;
807 if (window_with_focus->temp_item_selected)
809 collection_clear_selection(window_with_focus->collection);
810 window_with_focus->temp_item_selected = FALSE;
814 void target_callback(FilerWindow *filer_window,
815 gint item,
816 gpointer action)
818 Collection *collection = filer_window->collection;
820 g_return_if_fail(window_with_focus != NULL);
821 g_return_if_fail(window_with_focus == filer_window);
823 /* Don't grab the primary selection */
824 filer_window->temp_item_selected = TRUE;
826 collection_wink_item(collection, item);
827 collection_clear_except(collection, item);
828 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
830 if (item < collection->number_of_items)
831 collection_unselect_item(collection, item);
832 filer_window->temp_item_selected = FALSE;
835 /* Set the text of the 'Shift Open...' menu item.
836 * If icon is NULL, reset the text and also shade it, unless 'next'.
838 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
840 guchar *shift_action = NULL;
842 if (item)
844 if (item->flags & ITEM_FLAG_MOUNT_POINT)
846 if (item->flags & ITEM_FLAG_MOUNTED)
847 shift_action = N_("Unmount");
848 else
849 shift_action = N_("Mount");
851 else if (item->flags & ITEM_FLAG_SYMLINK)
852 shift_action = N_("Show Target");
853 else if (item->base_type == TYPE_DIRECTORY)
854 shift_action = N_("Look Inside");
855 else if (item->base_type == TYPE_FILE)
856 shift_action = N_("Open As Text");
858 gtk_label_set_text(GTK_LABEL(menu_item),
859 shift_action ? _(shift_action)
860 : _("Shift Open"));
861 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
864 /* Actions */
866 static void huge_with(gpointer data, guint action, GtkWidget *widget)
868 display_set_layout(window_with_focus, HUGE_ICONS, action);
871 static void large_with(gpointer data, guint action, GtkWidget *widget)
873 display_set_layout(window_with_focus, LARGE_ICONS, action);
876 static void small_with(gpointer data, guint action, GtkWidget *widget)
878 display_set_layout(window_with_focus, SMALL_ICONS, action);
881 static void sort_name(gpointer data, guint action, GtkWidget *widget)
883 g_return_if_fail(window_with_focus != NULL);
885 display_set_sort_fn(window_with_focus, sort_by_name);
888 static void sort_type(gpointer data, guint action, GtkWidget *widget)
890 g_return_if_fail(window_with_focus != NULL);
892 display_set_sort_fn(window_with_focus, sort_by_type);
895 static void sort_date(gpointer data, guint action, GtkWidget *widget)
897 g_return_if_fail(window_with_focus != NULL);
899 display_set_sort_fn(window_with_focus, sort_by_date);
902 static void sort_size(gpointer data, guint action, GtkWidget *widget)
904 g_return_if_fail(window_with_focus != NULL);
906 display_set_sort_fn(window_with_focus, sort_by_size);
909 static void hidden(gpointer data, guint action, GtkWidget *widget)
911 if (updating_menu)
912 return;
914 g_return_if_fail(window_with_focus != NULL);
916 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
919 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
921 if (updating_menu)
922 return;
924 g_return_if_fail(window_with_focus != NULL);
926 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
929 static void refresh(gpointer data, guint action, GtkWidget *widget)
931 g_return_if_fail(window_with_focus != NULL);
933 full_refresh();
934 filer_update_dir(window_with_focus, TRUE);
937 static void delete(FilerWindow *filer_window)
939 GList *paths;
940 paths = filer_selected_items(filer_window);
941 action_delete(paths);
942 g_list_foreach(paths, (GFunc) g_free, NULL);
943 g_list_free(paths);
946 static void usage(FilerWindow *filer_window)
948 GList *paths;
949 paths = filer_selected_items(filer_window);
950 action_usage(paths);
951 g_list_foreach(paths, (GFunc) g_free, NULL);
952 g_list_free(paths);
955 static void chmod_items(FilerWindow *filer_window)
957 GList *paths;
958 paths = filer_selected_items(filer_window);
959 action_chmod(paths);
960 g_list_foreach(paths, (GFunc) g_free, NULL);
961 g_list_free(paths);
964 static void find(FilerWindow *filer_window)
966 GList *paths;
967 paths = filer_selected_items(filer_window);
968 action_find(paths);
969 g_list_foreach(paths, (GFunc) g_free, NULL);
970 g_list_free(paths);
973 /* This creates a new savebox widget, and allows the user to pick a new path
974 * for the file.
975 * Once the new path has been picked, the callback will be called with
976 * both the current and new paths.
977 * NOTE: This function unrefs 'image'!
979 static void savebox_show(const gchar *action, const gchar *path,
980 MaskedPixmap *image, SaveCb callback)
982 GtkWidget *savebox = NULL;
983 GtkWidget *check_relative = NULL;
985 g_return_if_fail(image != NULL);
987 savebox = gtk_savebox_new(action);
989 if (callback == link_cb)
991 check_relative = gtk_check_button_new_with_mnemonic(
992 _("_Relative link"));
993 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative),
994 TRUE);
996 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
997 gtk_tooltips_set_tip(tooltips, check_relative,
998 _("If on, the symlink will store the path from the "
999 "symlink to the target file. Use this if the symlink "
1000 "and the target will be moved together.\n"
1001 "If off, the path from the root directory is stored - "
1002 "use this if the symlink may move but the target will "
1003 "stay put."), NULL);
1004 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(savebox)->vbox),
1005 check_relative, FALSE, TRUE, 0);
1006 gtk_widget_show(check_relative);
1009 g_signal_connect(savebox, "save_to_file",
1010 G_CALLBACK(save_to_file), NULL);
1012 g_object_set_data_full(G_OBJECT(savebox), "current_path",
1013 g_strdup(path), g_free);
1014 g_object_set_data(G_OBJECT(savebox), "action_callback", callback);
1015 g_object_set_data(G_OBJECT(savebox), "check_relative", check_relative);
1017 gtk_window_set_title(GTK_WINDOW(savebox), action);
1019 if (g_utf8_validate(path, -1, NULL))
1020 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), path);
1021 else
1023 gchar *u8;
1024 u8 = to_utf8(path);
1025 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), u8);
1026 g_free(u8);
1028 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixmap, image->mask);
1029 g_object_unref(image);
1031 gtk_widget_show(savebox);
1034 static gint save_to_file(GObject *savebox,
1035 const gchar *pathname, gpointer data)
1037 SaveCb callback;
1038 const gchar *current_path;
1040 callback = g_object_get_data(savebox, "action_callback");
1041 current_path = g_object_get_data(savebox, "current_path");
1043 g_return_val_if_fail(callback != NULL, GTK_XDS_SAVE_ERROR);
1044 g_return_val_if_fail(current_path != NULL, GTK_XDS_SAVE_ERROR);
1046 return callback(savebox, current_path, pathname)
1047 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1050 static gboolean copy_cb(GObject *savebox,
1051 const gchar *current, const gchar *new)
1053 return action_with_leaf(action_copy, current, new);
1056 static gboolean action_with_leaf(ActionFn action,
1057 const gchar *current, const gchar *new)
1059 const char *leaf;
1060 char *new_dir;
1061 GList *local_paths;
1063 if (new[0] != '/')
1065 report_error(_("New pathname is not absolute"));
1066 return FALSE;
1069 if (new[strlen(new) - 1] == '/')
1071 new_dir = g_strdup(new);
1072 leaf = NULL;
1074 else
1076 const gchar *slash;
1078 slash = strrchr(new, '/');
1079 new_dir = g_strndup(new, slash - new);
1080 leaf = slash + 1;
1083 local_paths = g_list_append(NULL, (gchar *) current);
1084 action(local_paths, new_dir, leaf, -1);
1085 g_list_free(local_paths);
1087 g_free(new_dir);
1089 return TRUE;
1092 /* Open a savebox to act on the selected file.
1093 * Call 'callback' later to perform the operation.
1095 static void src_dest_action_item(const gchar *path, MaskedPixmap *image,
1096 const gchar *action, SaveCb callback)
1098 g_object_ref(image);
1099 savebox_show(action, path, image, callback);
1102 static gboolean rename_cb(GObject *savebox,
1103 const gchar *current, const gchar *new)
1105 return action_with_leaf(action_move, current, new);
1108 static gboolean link_cb(GObject *savebox,
1109 const gchar *initial, const gchar *path)
1111 GtkToggleButton *check_relative;
1112 int err;
1114 check_relative = g_object_get_data(savebox, "check_relative");
1116 if (gtk_toggle_button_get_active(check_relative))
1118 guchar *rpath;
1120 rpath = get_relative_path(path, initial);
1121 err = symlink(rpath, path);
1123 g_free(rpath);
1125 else
1126 err = symlink(initial, path);
1128 if (err)
1130 report_error("symlink: %s", g_strerror(errno));
1131 return FALSE;
1134 dir_check_this(path);
1136 return TRUE;
1139 static void run_action(DirItem *item)
1141 if (can_set_run_action(item))
1142 type_set_handler_dialog(item->mime_type);
1143 else
1144 report_error(
1145 _("You can only set the run action for a "
1146 "regular file"));
1149 void open_home(gpointer data, guint action, GtkWidget *widget)
1151 filer_opendir(home_dir, NULL);
1154 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1156 gchar *path;
1158 path = g_strconcat(filer_window->path,
1159 "/", item->leafname, "#", NULL);
1161 filer_change_to(filer_window, path, NULL);
1162 g_free(path);
1165 static void select_all(gpointer data, guint action, GtkWidget *widget)
1167 g_return_if_fail(window_with_focus != NULL);
1169 window_with_focus->temp_item_selected = FALSE;
1170 collection_select_all(window_with_focus->collection);
1173 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1175 g_return_if_fail(window_with_focus != NULL);
1177 window_with_focus->temp_item_selected = FALSE;
1178 collection_clear_selection(window_with_focus->collection);
1181 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1183 g_return_if_fail(window_with_focus != NULL);
1185 window_with_focus->temp_item_selected = FALSE;
1186 collection_invert_selection(window_with_focus->collection);
1189 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1191 options_show();
1194 static gboolean new_directory_cb(GObject *savebox,
1195 const gchar *initial, const gchar *path)
1197 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1199 report_error("mkdir: %s", g_strerror(errno));
1200 return FALSE;
1203 dir_check_this(path);
1205 if (filer_exists(window_with_focus))
1207 guchar *leaf;
1208 leaf = strrchr(path, '/');
1209 if (leaf)
1210 display_set_autoselect(window_with_focus, leaf + 1);
1213 return TRUE;
1216 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1218 g_return_if_fail(window_with_focus != NULL);
1220 savebox_show(_("Create"),
1221 make_path(window_with_focus->path, _("NewDir"))->str,
1222 type_to_icon(special_directory),
1223 new_directory_cb);
1226 static gboolean new_file_cb(GObject *savebox,
1227 const gchar *initial, const gchar *path)
1229 int fd;
1231 fd = open(path, O_CREAT | O_EXCL, 0666);
1233 if (fd == -1)
1235 report_error(_("Error creating '%s': %s"),
1236 path, g_strerror(errno));
1237 return FALSE;
1240 if (close(fd))
1241 report_error(_("Error creating '%s': %s"),
1242 path, g_strerror(errno));
1244 dir_check_this(path);
1246 if (filer_exists(window_with_focus))
1248 guchar *leaf;
1249 leaf = strrchr(path, '/');
1250 if (leaf)
1251 display_set_autoselect(window_with_focus, leaf + 1);
1254 return TRUE;
1257 static void new_file(gpointer data, guint action, GtkWidget *widget)
1259 g_return_if_fail(window_with_focus != NULL);
1261 savebox_show(_("Create"),
1262 make_path(window_with_focus->path, _("NewFile"))->str,
1263 type_to_icon(text_plain),
1264 new_file_cb);
1267 static gboolean new_file_type_cb(GObject *savebox,
1268 const gchar *initial, const gchar *path)
1270 const gchar *oleaf, *leaf;
1271 gchar *templ, *templ_dname, *dest;
1272 GList *paths;
1274 /* We can work out the template path from the initial name */
1275 oleaf = g_basename(initial);
1276 templ_dname = choices_find_path_load("Templates", "");
1277 if (!templ_dname)
1279 report_error(
1280 _("Error creating file: could not find the template for %s"),
1281 oleaf);
1282 return FALSE;
1285 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1286 g_free(templ_dname);
1288 dest = g_dirname(path);
1289 leaf = g_basename(path);
1290 paths = g_list_append(NULL, templ);
1292 action_copy(paths, dest, leaf, -1);
1294 g_list_free(paths);
1295 g_free(dest);
1296 g_free(templ);
1298 if (filer_exists(window_with_focus))
1299 display_set_autoselect(window_with_focus, leaf);
1301 return TRUE;
1304 static void do_send_to(gchar *templ)
1306 g_return_if_fail(send_to_paths != NULL);
1308 run_with_files(templ, send_to_paths);
1311 static void new_file_type(gchar *templ)
1313 const gchar *leaf;
1314 MIME_type *type;
1316 g_return_if_fail(window_with_focus != NULL);
1318 leaf = g_basename(templ);
1319 type = type_get_type(templ);
1321 savebox_show(_("Create"),
1322 make_path(window_with_focus->path, leaf)->str,
1323 type_to_icon(type),
1324 new_file_type_cb);
1327 static void customise_send_to(gpointer data)
1329 GPtrArray *path;
1330 guchar *save;
1331 GString *dirs;
1332 int i;
1334 dirs = g_string_new(NULL);
1336 path = choices_list_dirs("");
1337 for (i = 0; i < path->len; i++)
1339 guchar *old = (guchar *) path->pdata[i];
1341 g_string_append(dirs, old);
1342 g_string_append(dirs, "SendTo\n");
1344 choices_free_list(path);
1346 save = choices_find_path_save("", "SendTo", TRUE);
1347 if (save)
1348 mkdir(save, 0777);
1350 info_message(
1351 _("The `Send To' menu provides a quick way to send some files "
1352 "to an application. The applications listed are those in "
1353 "the following directories:\n\n%s\n%s\n"
1354 "The `Send To' menu may be opened by Shift+Menu clicking "
1355 "over a file."),
1356 dirs->str,
1357 save ? _("I'll show you your SendTo directory now; you should "
1358 "symlink (Ctrl+Shift drag) any applications you want "
1359 "into it.")
1360 : _("Your CHOICESPATH variable setting prevents "
1361 "customisations - sorry."));
1363 g_string_free(dirs, TRUE);
1365 if (save)
1366 filer_opendir(save, NULL);
1369 /* Scan the SendTo dir and create and show the Send To menu.
1370 * The 'paths' list and every path in it is claimed, and will be
1371 * freed later -- don't free it yourself!
1373 static void show_send_to_menu(GList *paths, GdkEvent *event)
1375 GtkWidget *menu, *item;
1376 GPtrArray *path;
1377 int i;
1379 menu = gtk_menu_new();
1381 path = choices_list_dirs("SendTo");
1383 for (i = 0; i < path->len; i++)
1385 GList *widgets = NULL;
1386 guchar *dir = (guchar *) path->pdata[i];
1388 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1389 (CallbackFn) do_send_to,
1390 FALSE, FALSE, TRUE);
1392 if (widgets)
1393 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1394 gtk_menu_item_new());
1396 g_list_free(widgets); /* TODO: Get rid of this */
1399 choices_free_list(path);
1401 item = gtk_menu_item_new_with_label(_("Customise"));
1402 g_signal_connect_swapped(item, "activate",
1403 G_CALLBACK(customise_send_to), NULL);
1404 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1406 if (send_to_paths)
1408 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1409 g_list_free(send_to_paths);
1411 send_to_paths = paths;
1413 g_signal_connect(menu, "unmap_event", G_CALLBACK(menu_closed), NULL);
1415 popup_menu = menu;
1416 show_popup_menu(menu, event, 0);
1419 static void send_to(FilerWindow *filer_window)
1421 GList *paths;
1422 GdkEvent *event;
1424 paths = filer_selected_items(filer_window);
1425 event = gtk_get_current_event();
1427 /* Eats paths */
1428 show_send_to_menu(paths, event);
1430 gdk_event_free(event);
1433 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1435 const char *argv[] = {"sh", "-c", NULL, NULL};
1437 argv[2] = o_menu_xterm.value;
1439 g_return_if_fail(window_with_focus != NULL);
1441 rox_spawn(window_with_focus->path, argv);
1444 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1446 g_return_if_fail(window_with_focus != NULL);
1448 filer_change_to(window_with_focus, home_dir, NULL);
1451 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1453 g_return_if_fail(window_with_focus != NULL);
1455 filer_open_parent(window_with_focus);
1458 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1460 g_return_if_fail(window_with_focus != NULL);
1462 change_to_parent(window_with_focus);
1465 static void resize(gpointer data, guint action, GtkWidget *widget)
1467 g_return_if_fail(window_with_focus != NULL);
1469 filer_window_autosize(window_with_focus, TRUE);
1472 static void new_window(gpointer data, guint action, GtkWidget *widget)
1474 g_return_if_fail(window_with_focus != NULL);
1476 if (o_unique_filer_windows.int_value)
1478 report_error(_("You can't open a second view onto "
1479 "this directory because the `Unique Windows' option "
1480 "is turned on in the Options window."));
1482 else
1483 filer_opendir(window_with_focus->path, window_with_focus);
1486 #if 0
1487 static void su_to_user(GtkWidget *dialog)
1489 char *argv[] = {
1490 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1491 GtkEntry *user;
1492 guchar *path;
1494 g_return_if_fail(dialog != NULL);
1496 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1497 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1499 g_return_if_fail(user != NULL && path != NULL);
1501 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1502 argv[3] = gtk_entry_get_text(user);
1503 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1504 argv[5] = path;
1506 if (!spawn(argv))
1507 report_error(_("fork: %s"), g_strerror(errno));
1509 g_free(argv[2]);
1510 g_free(argv[4]);
1512 gtk_widget_destroy(dialog);
1515 static void new_user(gpointer data, guint action, GtkWidget *widget)
1517 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1519 g_return_if_fail(window_with_focus != NULL);
1521 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1522 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1523 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1524 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1525 g_strdup(window_with_focus->path), g_free);
1527 vbox = gtk_vbox_new(FALSE, 4);
1528 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1529 gtk_box_pack_start(GTK_BOX(vbox),
1530 gtk_label_new(_("Browse as which user?")),
1531 TRUE, TRUE, 2);
1533 hbox = gtk_hbox_new(FALSE, 4);
1534 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1536 gtk_box_pack_start(GTK_BOX(hbox),
1537 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1539 entry = gtk_entry_new();
1540 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1541 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1542 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1543 gtk_widget_grab_focus(entry);
1544 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1545 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1546 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1548 hbox = gtk_hbox_new(TRUE, 0);
1549 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1551 button = gtk_button_new_with_label(_("OK"));
1552 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1553 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1554 gtk_window_set_default(GTK_WINDOW(dialog), button);
1555 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1556 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1558 button = gtk_button_new_with_label(_("Cancel"));
1559 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1560 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1561 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1562 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1563 GTK_OBJECT(dialog));
1565 gtk_widget_show_all(dialog);
1567 #endif
1569 static void close_window(gpointer data, guint action, GtkWidget *widget)
1571 g_return_if_fail(window_with_focus != NULL);
1573 gtk_widget_destroy(window_with_focus->window);
1576 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1578 MiniType type = (MiniType) action;
1580 g_return_if_fail(window_with_focus != NULL);
1582 /* Item needs to remain selected... */
1583 if (type == MINI_SHELL)
1584 window_with_focus->temp_item_selected = FALSE;
1586 minibuffer_show(window_with_focus, type);
1589 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1591 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1594 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1595 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1597 GList *items, *item;
1599 items = gtk_container_get_children(GTK_CONTAINER(menu));
1601 item = g_list_nth(items, from);
1602 while (item && n--)
1604 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1605 item = item->next;
1607 g_list_free(items);
1610 static void save_menus(void)
1612 char *menurc;
1614 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1615 if (menurc)
1617 gtk_accel_map_save(menurc);
1618 g_free(menurc);
1622 static void keys_changed(gpointer data)
1624 save_menus();
1627 static void select_nth_item(GtkMenuShell *shell, int n)
1629 GList *items, *nth;
1630 GtkWidget *item = NULL;
1632 items = gtk_container_get_children(GTK_CONTAINER(shell));
1633 nth = g_list_nth(items, n);
1635 g_return_if_fail(nth != NULL);
1637 item = (GtkWidget *) (nth->data);
1638 g_list_free(items);
1640 gtk_menu_shell_select_item(shell, item);
1643 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1645 Collection *collection;
1646 DirItem *item;
1647 gchar *path;
1649 g_return_if_fail(window_with_focus != NULL);
1651 collection = window_with_focus->collection;
1653 if (collection->number_selected < 1)
1655 const char *prompt;
1657 switch (action)
1659 case FILE_COPY_ITEM:
1660 prompt = _("Copy ... ?");
1661 break;
1662 case FILE_RENAME_ITEM:
1663 prompt = _("Rename ... ?");
1664 break;
1665 case FILE_LINK_ITEM:
1666 prompt = _("Symlink ... ?");
1667 break;
1668 case FILE_OPEN_FILE:
1669 prompt = _("Shift Open ... ?");
1670 break;
1671 case FILE_HELP:
1672 prompt = _("Help about ... ?");
1673 break;
1674 case FILE_SHOW_FILE_INFO:
1675 prompt = _("Examine ... ?");
1676 break;
1677 case FILE_RUN_ACTION:
1678 prompt = _("Set run action for ... ?");
1679 break;
1680 case FILE_SET_ICON:
1681 prompt = _("Set icon for ... ?");
1682 break;
1683 case FILE_SEND_TO:
1684 prompt = _("Send ... to ... ?");
1685 break;
1686 case FILE_DELETE:
1687 prompt = _("DELETE ... ?");
1688 break;
1689 case FILE_USAGE:
1690 prompt = _("Count the size of ... ?");
1691 break;
1692 case FILE_CHMOD_ITEMS:
1693 prompt = _("Set permissions on ... ?");
1694 break;
1695 case FILE_FIND:
1696 prompt = _("Search inside ... ?");
1697 break;
1698 case FILE_OPEN_VFS_AVFS:
1699 prompt = _("Look inside ... ?");
1700 break;
1701 default:
1702 g_warning("Unknown action!");
1703 return;
1705 filer_target_mode(window_with_focus, target_callback,
1706 GINT_TO_POINTER(action), prompt);
1707 return;
1710 switch (action)
1712 case FILE_SEND_TO:
1713 send_to(window_with_focus);
1714 return;
1715 case FILE_DELETE:
1716 delete(window_with_focus);
1717 return;
1718 case FILE_USAGE:
1719 usage(window_with_focus);
1720 return;
1721 case FILE_CHMOD_ITEMS:
1722 chmod_items(window_with_focus);
1723 return;
1724 case FILE_FIND:
1725 find(window_with_focus);
1726 return;
1727 default:
1728 break;
1731 /* All the following actions require exactly one file selected */
1733 if (collection->number_selected > 1)
1735 report_error(_("You cannot do this to more than "
1736 "one item at a time"));
1737 return;
1740 item = selected_item(collection);
1741 g_return_if_fail(item != NULL);
1742 if (!item->image)
1743 item = dir_update_item(window_with_focus->directory,
1744 item->leafname);
1746 if (!item)
1748 report_error(_("Item no longer exists!"));
1749 return;
1752 path = make_path(window_with_focus->path, item->leafname)->str;
1754 switch (action)
1756 case FILE_COPY_ITEM:
1757 src_dest_action_item(path, item->image,
1758 _("Copy"), copy_cb);
1759 break;
1760 case FILE_RENAME_ITEM:
1761 src_dest_action_item(path, item->image,
1762 _("Rename"), rename_cb);
1763 break;
1764 case FILE_LINK_ITEM:
1765 src_dest_action_item(path, item->image,
1766 _("Symlink"), link_cb);
1767 break;
1768 case FILE_OPEN_FILE:
1769 filer_openitem(window_with_focus,
1770 selected_item_number(collection),
1771 OPEN_SAME_WINDOW | OPEN_SHIFT);
1772 break;
1773 case FILE_HELP:
1774 show_item_help(path, item);
1775 break;
1776 case FILE_SHOW_FILE_INFO:
1777 infobox_new(path);
1778 break;
1779 case FILE_RUN_ACTION:
1780 run_action(item);
1781 break;
1782 case FILE_SET_ICON:
1783 icon_set_handler_dialog(item, path);
1784 break;
1785 case FILE_OPEN_VFS_AVFS:
1786 open_vfs_avfs(window_with_focus, item);
1787 break;
1788 default:
1789 g_warning("Unknown action!");
1790 return;
1794 static void show_key_help(GtkWidget *button, gpointer data)
1796 info_message(_("To set a keyboard short-cut for a menu item:\n\n"
1797 "- Open the menu over a filer window,\n"
1798 "- Move the pointer over the item you want to use,\n"
1799 "- Press the key you want attached to it.\n\n"
1800 "The key will appear next to the menu item and you can just press "
1801 "that key without opening the menu in future."));
1804 static GList *set_keys_button(Option *option, xmlNode *node, guchar *label)
1806 GtkWidget *button, *align;
1808 g_return_val_if_fail(option == NULL, NULL);
1810 align = gtk_alignment_new(0.5, 0.5, 0, 0);
1811 button = button_new_mixed(GTK_STOCK_DIALOG_INFO,
1812 _("Set keyboard shortcuts"));
1813 gtk_container_add(GTK_CONTAINER(align), button);
1814 g_signal_connect(button, "clicked", G_CALLBACK(show_key_help), NULL);
1816 return g_list_append(NULL, align);