r1127: When using 'Shell Command...' from the menu with an item selected by opening
[rox-filer.git] / ROX-Filer / src / menu.c
blob9b79e099f4df472ee4a3ec93d505d079b97174f3
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 menu */
24 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/wait.h>
28 #include <sys/param.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <dirent.h>
34 #include <gtk/gtk.h>
36 #include "global.h"
38 #include "menu.h"
39 #include "run.h"
40 #include "action.h"
41 #include "filer.h"
42 #include "pixmaps.h"
43 #include "type.h"
44 #include "support.h"
45 #include "gui_support.h"
46 #include "options.h"
47 #include "choices.h"
48 #include "gtksavebox.h"
49 #include "mount.h"
50 #include "minibuffer.h"
51 #include "i18n.h"
52 #include "main.h"
53 #include "pinboard.h"
54 #include "dir.h"
55 #include "diritem.h"
56 #include "appmenu.h"
57 #include "usericons.h"
58 #include "infobox.h"
59 #include "collection.h"
60 #include "display.h"
62 #define C_ "<control>"
64 typedef enum {
65 FILE_COPY_ITEM,
66 FILE_RENAME_ITEM,
67 FILE_LINK_ITEM,
68 FILE_OPEN_FILE,
69 FILE_HELP,
70 FILE_SHOW_FILE_INFO,
71 FILE_RUN_ACTION,
72 FILE_SET_ICON,
73 FILE_SEND_TO,
74 FILE_DELETE,
75 FILE_USAGE,
76 FILE_CHMOD_ITEMS,
77 FILE_FIND,
78 #ifdef HAVE_LIBVFS
79 FILE_OPEN_VFS_RPM,
80 FILE_OPEN_VFS_UTAR,
81 FILE_OPEN_VFS_UZIP,
82 FILE_OPEN_VFS_DEB,
83 #else
84 FILE_OPEN_VFS_AVFS,
85 #endif
86 } FileOp;
88 typedef enum menu_icon_style {
89 MIS_NONE, MIS_SMALL, MIS_LARGE, MIS_HUGE,
90 MIS_CURRENT, /* As per current filer window */
91 MIS_DEFAULT
92 } MenuIconStyle;
94 typedef void (*ActionFn)(GList *paths, char *dest_dir, char *leaf, int quiet);
95 typedef void MenuCallback(GtkWidget *widget, gpointer data);
97 GtkAccelGroup *filer_keys;
98 GtkAccelGroup *pinboard_keys;
100 GtkWidget *popup_menu = NULL; /* Currently open menu */
102 static gint updating_menu = 0; /* Non-zero => ignore activations */
103 static GList *send_to_paths = NULL;
105 /* Static prototypes */
107 static void save_menus(void);
108 static void menu_closed(GtkWidget *widget);
109 static void items_sensitive(gboolean state);
110 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
111 gboolean (*callback)(guchar *current, guchar *new));
112 static gint save_to_file(GtkSavebox *savebox, guchar *pathname);
113 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new);
114 static gboolean link_cb(guchar *initial, guchar *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);
120 /* Note that for most of these callbacks none of the arguments are used. */
122 /* (action used in these three - DetailsType) */
123 static void huge_with(gpointer data, guint action, GtkWidget *widget);
124 static void large_with(gpointer data, guint action, GtkWidget *widget);
125 static void small_with(gpointer data, guint action, GtkWidget *widget);
127 static void sort_name(gpointer data, guint action, GtkWidget *widget);
128 static void sort_type(gpointer data, guint action, GtkWidget *widget);
129 static void sort_size(gpointer data, guint action, GtkWidget *widget);
130 static void sort_date(gpointer data, guint action, GtkWidget *widget);
132 static void hidden(gpointer data, guint action, GtkWidget *widget);
133 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
134 static void refresh(gpointer data, guint action, GtkWidget *widget);
136 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
138 static void select_all(gpointer data, guint action, GtkWidget *widget);
139 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
140 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
141 static void new_directory(gpointer data, guint action, GtkWidget *widget);
142 static void new_file(gpointer data, guint action, GtkWidget *widget);
143 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
145 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
146 static void open_parent(gpointer data, guint action, GtkWidget *widget);
147 static void home_directory(gpointer data, guint action, GtkWidget *widget);
148 static void new_window(gpointer data, guint action, GtkWidget *widget);
149 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
150 static void close_window(gpointer data, guint action, GtkWidget *widget);
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 #ifdef GTK2
157 #define MENUS_NAME "menus2"
158 static void keys_changed(gpointer data);
159 #else
160 # define MENUS_NAME "menus"
161 static void mark_menus_modified(gboolean mod);
162 #endif
164 static GtkWidget *filer_menu; /* The popup filer menu */
165 static GtkWidget *filer_file_item; /* The File '' label */
166 static GtkWidget *filer_file_menu; /* The File '' menu */
167 static GtkWidget *file_shift_item; /* Shift Open label */
168 GtkWidget *display_large_menu; /* Display->Large With... */
169 GtkWidget *display_small_menu; /* Display->Small With... */
170 #ifdef HAVE_LIBVFS
171 static GtkWidget *filer_vfs_menu; /* The Open VFS menu */
172 #endif
173 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
174 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
175 static GtkWidget *filer_new_window; /* The New Window item */
176 static GtkWidget *filer_new_menu; /* The New submenu */
178 /* Used for Copy, etc */
179 static GtkWidget *savebox = NULL;
180 static GtkWidget *check_relative = NULL;
181 static guchar *current_path = NULL;
182 static gboolean (*current_savebox_callback)(guchar *current, guchar *new);
184 #undef N_
185 #define N_(x) x
187 static GtkItemFactoryEntry filer_menu_def[] = {
188 {N_("Display"), NULL, NULL, 0, "<Branch>"},
189 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
190 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
191 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
192 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
193 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
194 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
195 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
196 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
197 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
198 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
199 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
200 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
201 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
202 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
203 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
204 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
205 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
206 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
207 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
208 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
209 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
210 {">", NULL, NULL, 0, "<Separator>"},
211 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
212 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
213 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
214 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
215 {">", NULL, NULL, 0, "<Separator>"},
216 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
217 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
218 {">" N_("Refresh"), NULL, refresh, 0, NULL},
219 {N_("File"), NULL, NULL, 0, "<Branch>"},
220 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
221 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
222 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
223 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
224 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
225 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
226 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
227 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
228 #ifdef HAVE_LIBVFS
229 {">" N_("Open VFS"), NULL, NULL, 0, "<Branch>"},
230 {">>" N_("Unzip"), NULL, file_op, FILE_OPEN_VFS_UZIP, NULL},
231 {">>" N_("Untar"), NULL, file_op, FILE_OPEN_VFS_UTAR, NULL},
232 {">>" N_("Deb"), NULL, file_op, FILE_OPEN_VFS_DEB, NULL},
233 {">>" N_("RPM"), NULL, file_op, FILE_OPEN_VFS_RPM, NULL},
234 #else
235 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
236 #endif
237 {">", NULL, NULL, 0, "<Separator>"},
238 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
239 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
240 {">" N_("Disk Usage"), NULL, file_op, FILE_USAGE, NULL},
241 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
242 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
243 {N_("Select"), NULL, NULL, 0, "<Branch>"},
244 {">" N_("Select All"), NULL, select_all, 0, NULL},
245 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
246 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
247 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
248 {N_("Options..."), NULL, menu_show_options, 0, NULL},
249 {N_("New"), NULL, NULL, 0, "<Branch>"},
250 {">" N_("Directory"), NULL, new_directory, 0, NULL},
251 {">" N_("Blank file"), NULL, new_file, 0, NULL},
252 {N_("Xterm Here"), NULL, xterm_here, 0, NULL},
253 {N_("Window"), NULL, NULL, 0, "<Branch>"},
254 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
255 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
256 {">" N_("New Window"), NULL, new_window, 0, NULL},
257 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
258 {">" N_("Resize Window"), NULL, resize, 0, NULL},
259 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
261 {">" N_("Close Window"), NULL, close_window, 0, NULL},
262 {">", NULL, NULL, 0, "<Separator>"},
263 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
264 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
265 {">", NULL, NULL, 0, "<Separator>"},
266 {">" N_("Show ROX-Filer Help"), "F1", menu_rox_help, 0, NULL},
270 #define GET_MENU_ITEM(var, menu) \
271 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
273 #define GET_SMENU_ITEM(var, menu, sub) \
274 do { \
275 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
276 var = gtk_item_factory_get_widget(item_factory, tmp); \
277 g_free(tmp); \
278 } while (0)
280 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
281 do { \
282 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
283 var = gtk_item_factory_get_widget(item_factory, tmp); \
284 g_free(tmp); \
285 } while (0)
287 void menu_init(void)
289 char *menurc;
290 GList *items;
291 guchar *tmp;
292 GtkWidget *item;
293 GtkTooltips *tips;
294 GtkItemFactory *item_factory;
296 filer_keys = gtk_accel_group_new();
297 item_factory = menu_create(filer_menu_def,
298 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
299 "<filer>", filer_keys);
301 GET_MENU_ITEM(filer_menu, "filer");
302 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
303 #ifdef HAVE_LIBVFS
304 GET_SSMENU_ITEM(filer_vfs_menu, "filer", "File", "Open VFS");
305 #endif
306 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
307 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
308 "Show Thumbnails");
310 GET_SSMENU_ITEM(display_large_menu, "filer",
311 "Display", "Large, With...");
312 GET_SSMENU_ITEM(display_small_menu, "filer",
313 "Display", "Small, With...");
315 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
317 /* File '' label... */
318 items = gtk_container_children(GTK_CONTAINER(filer_menu));
319 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
320 g_list_free(items);
322 /* Shift Open... label */
323 items = gtk_container_children(GTK_CONTAINER(filer_file_menu));
324 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
325 g_list_free(items);
327 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
328 filer_new_window = GTK_BIN(item)->child;
330 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
331 if (menurc)
333 #ifdef GTK2
334 gtk_accel_map_load(menurc);
335 #else
336 gtk_item_factory_parse_rc(menurc);
337 mark_menus_modified(FALSE);
338 #endif
339 g_free(menurc);
342 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
343 GTK_SIGNAL_FUNC(menu_closed), NULL);
344 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
345 GTK_SIGNAL_FUNC(menu_closed), NULL);
347 option_add_string("menu_xterm", "xterm", NULL);
348 option_add_int("menu_iconsize", MIS_SMALL, NULL);
349 option_add_saver(save_menus);
351 tips = gtk_tooltips_new();
352 check_relative = gtk_check_button_new_with_label(_("Relative link"));
353 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative), TRUE);
354 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
355 gtk_tooltips_set_tip(tips, check_relative,
356 _("If on, the symlink will store the path from the "
357 "symlink to the target file. Use this if the symlink "
358 "and the target will be moved together.\n"
359 "If off, the path from the root directory is stored - "
360 "use this if the symlink may move but the target will "
361 "stay put."), NULL);
363 savebox = gtk_savebox_new();
364 gtk_box_pack_start(GTK_BOX(GTK_SAVEBOX(savebox)->vbox),
365 check_relative, FALSE, TRUE, 0);
366 gtk_widget_show(check_relative);
368 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_to_file",
369 GTK_SIGNAL_FUNC(save_to_file), NULL);
370 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_done",
371 GTK_SIGNAL_FUNC(gtk_widget_hide),
372 GTK_OBJECT(savebox));
374 #ifdef GTK2
375 g_signal_connect_object(G_OBJECT(filer_keys), "accel_changed",
376 (GCallback) keys_changed, NULL, 0);
377 #else
378 atexit(save_menus);
379 #endif
382 /* Name is in the form "<panel>" */
383 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
384 guchar *name, GtkAccelGroup *keys)
386 GtkItemFactory *item_factory;
387 GtkItemFactoryEntry *translated;
389 if (!keys)
391 keys = gtk_accel_group_new();
392 gtk_accel_group_lock(keys);
395 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
397 translated = translate_entries(def, n_entries);
398 gtk_item_factory_create_items(item_factory, n_entries,
399 translated, NULL);
400 free_translated_entries(translated, n_entries);
402 return item_factory;
405 /* Prevent the user from setting a short-cut on this item */
406 void menuitem_no_shortcuts(GtkWidget *item)
408 #ifdef GTK2
410 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
412 _gtk_widget_set_accel_path(item, NULL, NULL);
413 g_free(menuitem->accel_path);
414 menuitem->accel_path = NULL;
416 #else
417 gtk_widget_lock_accelerators(item);
418 #endif
421 static void items_sensitive(gboolean state)
423 int n = 9;
424 GList *items, *item;
426 items = item = gtk_container_children(GTK_CONTAINER(filer_file_menu));
427 while (item && n--)
429 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
430 item = item->next;
432 g_list_free(items);
434 #ifdef HAVE_LIBVFS
435 items = item = gtk_container_children(GTK_CONTAINER(filer_vfs_menu));
436 while (item)
438 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
439 item = item->next;
441 g_list_free(items);
442 #endif
445 /* 'data' is an array of three ints:
446 * [ pointer_x, pointer_y, item_under_pointer ]
448 void position_menu(GtkMenu *menu, gint *x, gint *y,
449 #ifdef GTK2
450 gboolean *push_in,
451 #endif
452 gpointer data)
454 int *pos = (int *) data;
455 GtkRequisition requisition;
456 GList *items, *next;
457 int y_shift = 0;
458 int item = pos[2];
460 next = items = gtk_container_children(GTK_CONTAINER(menu));
462 while (item >= 0 && next)
464 int h = ((GtkWidget *) next->data)->requisition.height;
466 if (item > 0)
467 y_shift += h;
468 else
469 y_shift += h / 2;
471 next = next->next;
472 item--;
475 g_list_free(items);
477 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
479 *x = pos[0] - (requisition.width * 7 / 8);
480 *y = pos[1] - y_shift;
482 *x = CLAMP(*x, 0, screen_width - requisition.width);
483 *y = CLAMP(*y, 0, screen_height - requisition.height);
485 #ifdef GTK2
486 *push_in = FALSE;
487 #endif
490 #if 0
491 /* Used when you menu-click on the Large or Small toolbar tools */
492 void show_style_menu(FilerWindow *filer_window,
493 GdkEventButton *event,
494 GtkWidget *menu)
496 int pos[3];
498 pos[0] = event->x_root;
499 pos[1] = event->y_root;
500 pos[2] = 0;
502 window_with_focus = filer_window;
504 popup_menu = menu;
506 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
507 (gpointer) pos, event->button, event->time);
509 #endif
511 static GList *menu_from_dir(GtkWidget *menu, const gchar *dname,
512 MenuIconStyle style, CallbackFn func,
513 gboolean separator, gboolean strip_ext)
515 GList *widgets = NULL;
516 DirItem *ditem;
517 DIR *dir;
518 struct dirent *ent;
519 GtkWidget *item;
521 dir = opendir(dname);
522 if (!dir)
523 goto out;
525 if (separator)
527 item = gtk_menu_item_new();
528 widgets = g_list_append(widgets, item);
529 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
532 while ((ent = readdir(dir)))
534 char *dot, *leaf;
535 GtkWidget *hbox;
536 GtkWidget *img;
537 GtkWidget *label;
538 gchar *fname;
539 GdkPixmap *icon;
540 GdkBitmap *mask;
542 /* Ignore hidden files */
543 if (ent->d_name[0] == '.')
544 continue;
546 /* Strip off extension, if any */
547 dot = strchr(ent->d_name, '.');
548 if (strip_ext && dot)
549 leaf = g_strndup(ent->d_name, dot - ent->d_name);
550 else
551 leaf = g_strdup(ent->d_name);
553 fname = g_strconcat(dname, "/", ent->d_name, NULL);
554 ditem = diritem_new(NULL);
555 diritem_restat(fname, ditem);
557 if (ditem->image && style != MIS_NONE)
559 switch (style) {
560 case MIS_HUGE:
561 if (!ditem->image->huge_pixmap)
562 pixmap_make_huge(ditem->image);
563 icon = ditem->image->huge_pixmap;
564 mask = ditem->image->huge_mask;
565 break;
566 case MIS_LARGE:
567 icon = ditem->image->pixmap;
568 mask = ditem->image->mask;
569 break;
571 case MIS_SMALL:
572 default:
573 if (!ditem->image->sm_pixmap)
574 pixmap_make_small(ditem->image);
575 icon = ditem->image->sm_pixmap;
576 mask = ditem->image->sm_mask;
577 break;
580 item = gtk_menu_item_new();
581 /* TODO: Find a way to allow short-cuts */
582 menuitem_no_shortcuts(item);
584 hbox = gtk_hbox_new(FALSE, 2);
585 gtk_container_add(GTK_CONTAINER(item), hbox);
587 img = gtk_pixmap_new(icon, mask);
588 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
590 label = gtk_label_new(leaf);
591 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
592 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
594 diritem_free(ditem);
596 else
597 item = gtk_menu_item_new_with_label(leaf);
599 g_free(leaf);
601 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
602 GTK_SIGNAL_FUNC(func), (GtkObject *) fname);
603 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
604 gtk_signal_connect_object(GTK_OBJECT(item), "destroy",
605 GTK_SIGNAL_FUNC(g_free), (GtkObject *) fname);
607 widgets = g_list_append(widgets, item);
610 closedir(dir);
611 out:
613 return widgets;
616 /* Scan the templates dir and create entries for the New menu */
617 static void update_new_files_menu(MenuIconStyle style)
619 static GList *widgets = NULL;
621 gchar *templ_dname = NULL;
623 if (widgets)
625 GList *next;
627 for (next = widgets; next; next = next->next)
628 gtk_widget_destroy((GtkWidget *) next->data);
630 g_list_free(widgets);
631 widgets = NULL;
634 templ_dname = choices_find_path_load("Templates", "");
635 if (templ_dname)
637 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
638 (CallbackFn) new_file_type, TRUE, TRUE);
639 g_free(templ_dname);
641 gtk_widget_show_all(filer_new_menu);
644 /* 'item' is the number of the item to appear under the pointer. */
645 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
647 int pos[3];
648 int button = 0;
649 guint32 time = 0;
651 if (event && (event->type == GDK_BUTTON_PRESS ||
652 event->type == GDK_BUTTON_RELEASE))
654 GdkEventButton *bev = (GdkEventButton *) event;
656 pos[0] = bev->x_root;
657 pos[1] = bev->y_root;
658 button = bev->button;
659 time = bev->time;
661 else if (event && event->type == GDK_KEY_PRESS)
663 GdkEventKey *kev = (GdkEventKey *) event;
665 get_pointer_xy(pos, pos + 1);
666 time = kev->time;
668 else
669 get_pointer_xy(pos, pos + 1);
671 pos[2] = item;
673 gtk_widget_show_all(menu);
674 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
675 position_menu, (gpointer) pos, button, time);
676 select_nth_item(GTK_MENU_SHELL(menu), item);
679 static MenuIconStyle get_menu_icon_style(void)
681 MenuIconStyle mis;
682 int display;
684 mis = option_get_int("menu_iconsize");
686 switch (mis)
688 case MIS_NONE: case MIS_SMALL: case MIS_LARGE: case MIS_HUGE:
689 return mis;
690 default:
691 break;
694 if (mis == MIS_CURRENT && window_with_focus)
696 switch (window_with_focus->display_style)
698 case HUGE_ICONS:
699 return MIS_HUGE;
700 case LARGE_ICONS:
701 return MIS_LARGE;
702 case SMALL_ICONS:
703 return MIS_SMALL;
704 default:
705 break;
709 display = option_get_int("display_size");
710 switch (display)
712 case HUGE_ICONS:
713 return MIS_HUGE;
714 case LARGE_ICONS:
715 return MIS_LARGE;
716 case SMALL_ICONS:
717 return MIS_SMALL;
718 default:
719 break;
722 return MIS_SMALL;
725 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
727 DirItem *file_item = NULL;
728 GdkModifierType state = 0;
730 updating_menu++;
732 /* Remove previous AppMenu, if any */
733 appmenu_remove();
735 window_with_focus = filer_window;
737 if (event->type == GDK_BUTTON_PRESS)
738 state = ((GdkEventButton *) event)->state;
739 else if (event->type == GDK_KEY_PRESS)
740 state = ((GdkEventKey *) event)->state;
742 if (filer_window->collection->number_selected == 0 && item >= 0)
744 filer_window->temp_item_selected = TRUE;
745 collection_select_item(filer_window->collection, item);
747 else
749 filer_window->temp_item_selected = FALSE;
752 /* Short-cut to the Send To menu */
753 if (state & GDK_SHIFT_MASK)
755 GList *paths;
757 updating_menu--;
759 if (filer_window->collection->number_selected == 0)
761 report_error(
762 _("You should Shift+Menu click over a file to "
763 "send it somewhere"));
764 return;
767 paths = filer_selected_items(filer_window);
769 show_send_to_menu(paths, event); /* (paths eaten) */
771 return;
775 GtkWidget *file_label, *file_menu;
776 Collection *collection = filer_window->collection;
777 GString *buffer;
778 DirItem *item;
780 file_label = filer_file_item;
781 file_menu = filer_file_menu;
782 gtk_check_menu_item_set_active(
783 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
784 filer_window->show_thumbs);
785 gtk_check_menu_item_set_active(
786 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
787 filer_window->show_hidden);
788 buffer = g_string_new(NULL);
790 if (collection->number_selected == 1)
792 item = selected_item(filer_window->collection);
793 if (!item->image)
794 dir_update_item(filer_window->directory,
795 item->leafname);
798 switch (collection->number_selected)
800 case 0:
801 g_string_assign(buffer, _("Next Click"));
802 items_sensitive(TRUE);
803 break;
804 case 1:
805 items_sensitive(TRUE);
806 file_item = selected_item(
807 filer_window->collection);
808 g_string_sprintf(buffer, "%s '%s'",
809 basetype_name(file_item),
810 file_item->leafname);
811 if (!can_set_run_action(file_item))
812 menu_set_items_shaded(filer_file_menu,
813 TRUE, 6, 1);
814 break;
815 default:
816 items_sensitive(FALSE);
817 g_string_sprintf(buffer, _("%d items"),
818 collection->number_selected);
819 break;
821 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
822 g_string_free(buffer, TRUE);
824 menu_show_shift_action(file_shift_item, file_item,
825 collection->number_selected == 0);
826 if (file_item)
827 appmenu_add(make_path(filer_window->path,
828 file_item->leafname)->str,
829 file_item, filer_file_menu);
832 update_new_files_menu(get_menu_icon_style());
834 gtk_widget_set_sensitive(filer_new_window, !o_unique_filer_windows);
836 popup_menu = (state & GDK_CONTROL_MASK)
837 ? filer_file_menu
838 : filer_menu;
840 updating_menu--;
842 show_popup_menu(popup_menu, event,
843 popup_menu == filer_file_menu ? 5 : 1);
846 static void menu_closed(GtkWidget *widget)
848 if (window_with_focus == NULL || widget != popup_menu)
849 return; /* Close panel item chosen? */
851 popup_menu = NULL;
853 if (window_with_focus->temp_item_selected)
855 collection_clear_selection(window_with_focus->collection);
856 window_with_focus->temp_item_selected = FALSE;
860 void target_callback(FilerWindow *filer_window,
861 gint item,
862 gpointer action)
864 Collection *collection = filer_window->collection;
866 g_return_if_fail(window_with_focus != NULL);
867 g_return_if_fail(window_with_focus == filer_window);
869 /* Don't grab the primary selection */
870 filer_window->temp_item_selected = TRUE;
872 collection_wink_item(collection, item);
873 collection_clear_except(collection, item);
874 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
876 if (item < collection->number_of_items)
877 collection_unselect_item(collection, item);
878 filer_window->temp_item_selected = FALSE;
881 /* Set the text of the 'Shift Open...' menu item.
882 * If icon is NULL, reset the text and also shade it, unless 'next'.
884 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
886 guchar *shift_action = NULL;
888 if (item)
890 if (item->flags & ITEM_FLAG_MOUNT_POINT)
892 if (item->flags & ITEM_FLAG_MOUNTED)
893 shift_action = N_("Unmount");
894 else
895 shift_action = N_("Mount");
897 else if (item->flags & ITEM_FLAG_SYMLINK)
898 shift_action = N_("Show Target");
899 else if (item->base_type == TYPE_DIRECTORY)
900 shift_action = N_("Look Inside");
901 else if (item->base_type == TYPE_FILE)
902 shift_action = N_("Open As Text");
904 gtk_label_set_text(GTK_LABEL(menu_item),
905 shift_action ? _(shift_action)
906 : _("Shift Open"));
907 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
910 /* Actions */
912 static void huge_with(gpointer data, guint action, GtkWidget *widget)
914 display_set_layout(window_with_focus, HUGE_ICONS, action);
917 static void large_with(gpointer data, guint action, GtkWidget *widget)
919 display_set_layout(window_with_focus, LARGE_ICONS, action);
922 static void small_with(gpointer data, guint action, GtkWidget *widget)
924 display_set_layout(window_with_focus, SMALL_ICONS, action);
927 static void sort_name(gpointer data, guint action, GtkWidget *widget)
929 g_return_if_fail(window_with_focus != NULL);
931 display_set_sort_fn(window_with_focus, sort_by_name);
934 static void sort_type(gpointer data, guint action, GtkWidget *widget)
936 g_return_if_fail(window_with_focus != NULL);
938 display_set_sort_fn(window_with_focus, sort_by_type);
941 static void sort_date(gpointer data, guint action, GtkWidget *widget)
943 g_return_if_fail(window_with_focus != NULL);
945 display_set_sort_fn(window_with_focus, sort_by_date);
948 static void sort_size(gpointer data, guint action, GtkWidget *widget)
950 g_return_if_fail(window_with_focus != NULL);
952 display_set_sort_fn(window_with_focus, sort_by_size);
955 static void hidden(gpointer data, guint action, GtkWidget *widget)
957 if (updating_menu)
958 return;
960 g_return_if_fail(window_with_focus != NULL);
962 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
965 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
967 if (updating_menu)
968 return;
970 g_return_if_fail(window_with_focus != NULL);
972 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
975 static void refresh(gpointer data, guint action, GtkWidget *widget)
977 g_return_if_fail(window_with_focus != NULL);
979 full_refresh();
980 filer_update_dir(window_with_focus, TRUE);
983 static void delete(FilerWindow *filer_window)
985 GList *paths;
986 paths = filer_selected_items(filer_window);
987 action_delete(paths);
988 g_list_foreach(paths, (GFunc) g_free, NULL);
989 g_list_free(paths);
992 static void usage(FilerWindow *filer_window)
994 GList *paths;
995 paths = filer_selected_items(filer_window);
996 action_usage(paths);
997 g_list_foreach(paths, (GFunc) g_free, NULL);
998 g_list_free(paths);
1001 static void chmod_items(FilerWindow *filer_window)
1003 GList *paths;
1004 paths = filer_selected_items(filer_window);
1005 action_chmod(paths);
1006 g_list_foreach(paths, (GFunc) g_free, NULL);
1007 g_list_free(paths);
1010 static void find(FilerWindow *filer_window)
1012 GList *paths;
1013 paths = filer_selected_items(filer_window);
1014 action_find(paths);
1015 g_list_foreach(paths, (GFunc) g_free, NULL);
1016 g_list_free(paths);
1019 /* This pops up our savebox widget, cancelling any currently open one,
1020 * and allows the user to pick a new path for it.
1021 * Once the new path has been picked, the callback will be called with
1022 * both the current and new paths.
1023 * NOTE: This function unrefs 'image'!
1025 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
1026 gboolean (*callback)(guchar *current, guchar *new))
1028 g_return_if_fail(image != NULL);
1030 if (GTK_WIDGET_VISIBLE(savebox))
1031 gtk_widget_hide(savebox);
1033 if (callback == link_cb)
1034 gtk_widget_show(check_relative);
1035 else
1036 gtk_widget_hide(check_relative);
1038 if (current_path)
1039 g_free(current_path);
1040 current_path = g_strdup(path);
1041 current_savebox_callback = callback;
1043 gtk_window_set_title(GTK_WINDOW(savebox), title);
1044 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), current_path);
1045 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixmap, image->mask);
1046 pixmap_unref(image);
1048 gtk_widget_show(savebox);
1051 static gint save_to_file(GtkSavebox *savebox, guchar *pathname)
1053 g_return_val_if_fail(current_savebox_callback != NULL,
1054 GTK_XDS_SAVE_ERROR);
1056 return current_savebox_callback(current_path, pathname)
1057 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1060 static gboolean copy_cb(guchar *current, guchar *new)
1062 return action_with_leaf(action_copy, current, new);
1065 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new)
1067 char *new_dir, *leaf;
1068 GList *local_paths;
1070 if (new[0] != '/')
1072 report_error(_("New pathname is not absolute"));
1073 return FALSE;
1076 if (new[strlen(new) - 1] == '/')
1078 new_dir = g_strdup(new);
1079 leaf = NULL;
1081 else
1083 guchar *slash;
1085 slash = strrchr(new, '/');
1086 new_dir = g_strndup(new, slash - new);
1087 leaf = slash + 1;
1090 local_paths = g_list_append(NULL, current);
1091 action(local_paths, new_dir, leaf, -1);
1092 g_list_free(local_paths);
1094 g_free(new_dir);
1096 return TRUE;
1099 /* Open a savebox to act on the selected file.
1100 * Call 'callback' later to perform the operation.
1102 static void src_dest_action_item(guchar *path, MaskedPixmap *image,
1103 guchar *title,
1104 gboolean (*callback)(guchar *, guchar *))
1106 pixmap_ref(image);
1107 savebox_show(title, path, image, callback);
1110 static gboolean rename_cb(guchar *current, guchar *new)
1112 return action_with_leaf(action_move, current, new);
1115 static gboolean link_cb(guchar *initial, guchar *path)
1117 int err;
1119 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_relative)))
1121 guchar *rpath;
1123 rpath = get_relative_path(path, initial);
1124 err = symlink(rpath, path);
1126 g_free(rpath);
1128 else
1129 err = symlink(initial, path);
1131 if (err)
1133 report_error("symlink: %s", g_strerror(errno));
1134 return FALSE;
1137 dir_check_this(path);
1139 return TRUE;
1142 static void run_action(DirItem *item)
1144 if (can_set_run_action(item))
1145 type_set_handler_dialog(item->mime_type);
1146 else
1147 report_error(
1148 _("You can only set the run action for a "
1149 "regular file"));
1152 void open_home(gpointer data, guint action, GtkWidget *widget)
1154 filer_opendir(home_dir, NULL);
1157 #ifdef HAVE_LIBVFS
1158 static void real_vfs_open(FilerWindow *filer_window, DirItem *item, char *fs)
1160 gchar *path;
1162 path = g_strconcat(filer_window->path,
1163 "/",
1164 item->leafname,
1165 "#", fs, NULL);
1167 filer_change_to(filer_window, path, NULL);
1168 g_free(path);
1170 #else
1171 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1173 gchar *path;
1175 path = g_strconcat(filer_window->path,
1176 "/", item->leafname, "#", NULL);
1178 filer_change_to(filer_window, path, NULL);
1179 g_free(path);
1181 #endif
1183 static void select_all(gpointer data, guint action, GtkWidget *widget)
1185 g_return_if_fail(window_with_focus != NULL);
1187 window_with_focus->temp_item_selected = FALSE;
1188 collection_select_all(window_with_focus->collection);
1191 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1193 g_return_if_fail(window_with_focus != NULL);
1195 window_with_focus->temp_item_selected = FALSE;
1196 collection_clear_selection(window_with_focus->collection);
1199 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1201 g_return_if_fail(window_with_focus != NULL);
1203 window_with_focus->temp_item_selected = FALSE;
1204 collection_invert_selection(window_with_focus->collection);
1207 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1209 options_show();
1212 static gboolean new_directory_cb(guchar *initial, guchar *path)
1214 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1216 report_error("mkdir: %s", g_strerror(errno));
1217 return FALSE;
1220 dir_check_this(path);
1222 if (filer_exists(window_with_focus))
1224 guchar *leaf;
1225 leaf = strrchr(path, '/');
1226 if (leaf)
1227 display_set_autoselect(window_with_focus, leaf + 1);
1230 return TRUE;
1233 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1235 g_return_if_fail(window_with_focus != NULL);
1237 savebox_show(_("New Directory"),
1238 make_path(window_with_focus->path, _("NewDir"))->str,
1239 type_to_icon(special_directory),
1240 new_directory_cb);
1243 static gboolean new_file_cb(guchar *initial, guchar *path)
1245 int fd;
1247 fd = open(path, O_CREAT | O_EXCL, 0666);
1249 if (fd == -1)
1251 report_error(_("Error creating '%s': %s"),
1252 path, g_strerror(errno));
1253 return FALSE;
1256 if (close(fd))
1257 report_error(_("Error creating '%s': %s"),
1258 path, g_strerror(errno));
1260 dir_check_this(path);
1262 if (filer_exists(window_with_focus))
1264 guchar *leaf;
1265 leaf = strrchr(path, '/');
1266 if (leaf)
1267 display_set_autoselect(window_with_focus, leaf + 1);
1270 return TRUE;
1273 static void new_file(gpointer data, guint action, GtkWidget *widget)
1275 g_return_if_fail(window_with_focus != NULL);
1277 savebox_show(_("New File"),
1278 make_path(window_with_focus->path, _("NewFile"))->str,
1279 type_to_icon(text_plain),
1280 new_file_cb);
1283 static gboolean new_file_type_cb(guchar *initial, guchar *path)
1285 gchar *templ, *templ_dname, *oleaf, *dest, *leaf;
1286 GList *paths;
1288 /* We can work out the template path from the initial name */
1289 oleaf = g_basename(initial);
1290 templ_dname = choices_find_path_load("Templates", "");
1291 if (!templ_dname)
1293 report_error(
1294 _("Error creating file: could not find the template for %s"),
1295 oleaf);
1296 return FALSE;
1299 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1300 g_free(templ_dname);
1302 dest = g_dirname(path);
1303 leaf = g_basename(path);
1304 paths = g_list_append(NULL, templ);
1306 action_copy(paths, dest, leaf, -1);
1308 g_list_free(paths);
1309 g_free(dest);
1310 g_free(templ);
1312 if (filer_exists(window_with_focus))
1313 display_set_autoselect(window_with_focus, leaf);
1315 return TRUE;
1318 static void do_send_to(gchar *templ)
1320 g_return_if_fail(send_to_paths != NULL);
1322 run_with_files(templ, send_to_paths);
1325 static void new_file_type(gchar *templ)
1327 gchar *leaf;
1328 MIME_type *type;
1330 g_return_if_fail(window_with_focus != NULL);
1332 leaf = g_basename(templ);
1333 type = type_get_type(templ);
1335 savebox_show(_("New File"),
1336 make_path(window_with_focus->path, leaf)->str,
1337 type_to_icon(type),
1338 new_file_type_cb);
1341 static void customise_send_to(gpointer data)
1343 GPtrArray *path;
1344 guchar *save;
1345 GString *dirs;
1346 int i;
1348 dirs = g_string_new(NULL);
1350 path = choices_list_dirs("");
1351 for (i = 0; i < path->len; i++)
1353 guchar *old = (guchar *) path->pdata[i];
1355 g_string_append(dirs, old);
1356 g_string_append(dirs, "SendTo\n");
1358 choices_free_list(path);
1360 save = choices_find_path_save("", "SendTo", TRUE);
1361 if (save)
1362 mkdir(save, 0777);
1364 report_error(
1365 _("The `Send To' menu provides a quick way to send some files "
1366 "to an application. The applications listed are those in "
1367 "the following directories:\n\n%s\n%s\n"
1368 "The `Send To' menu may be opened by Shift+Menu clicking "
1369 "over a file."),
1370 dirs->str,
1371 save ? _("I'll show you your SendTo directory now; you should "
1372 "symlink (Ctrl+Shift drag) any applications you want "
1373 "into it.")
1374 : _("Your CHOICESPATH variable setting prevents "
1375 "customisations - sorry."));
1377 g_string_free(dirs, TRUE);
1379 if (save)
1380 filer_opendir(save, NULL);
1383 /* Scan the SendTo dir and create and show the Send To menu.
1384 * The 'paths' list and every path in it is claimed, and will be
1385 * freed later -- don't free it yourself!
1387 static void show_send_to_menu(GList *paths, GdkEvent *event)
1389 GtkWidget *menu, *item;
1390 GPtrArray *path;
1391 int i;
1393 menu = gtk_menu_new();
1395 path = choices_list_dirs("SendTo");
1397 for (i = 0; i < path->len; i++)
1399 GList *widgets = NULL;
1400 guchar *dir = (guchar *) path->pdata[i];
1402 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1403 (CallbackFn) do_send_to,
1404 FALSE, FALSE);
1406 if (widgets)
1407 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1408 gtk_menu_item_new());
1410 g_list_free(widgets); /* TODO: Get rid of this */
1413 choices_free_list(path);
1415 item = gtk_menu_item_new_with_label(_("Customise"));
1416 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
1417 GTK_SIGNAL_FUNC(customise_send_to), NULL);
1418 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1420 if (send_to_paths)
1422 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1423 g_list_free(send_to_paths);
1425 send_to_paths = paths;
1427 gtk_signal_connect(GTK_OBJECT(menu), "unmap_event",
1428 GTK_SIGNAL_FUNC(menu_closed), NULL);
1430 popup_menu = menu;
1431 show_popup_menu(menu, event, 0);
1434 static void send_to(FilerWindow *filer_window)
1436 GList *paths;
1437 GdkEvent *event;
1439 paths = filer_selected_items(filer_window);
1440 event = gtk_get_current_event();
1442 /* Eats paths */
1443 show_send_to_menu(paths, event);
1445 gdk_event_free(event);
1448 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1450 char *argv[] = {"sh", "-c", NULL, NULL};
1452 argv[2] = option_get_static_string("menu_xterm");
1454 g_return_if_fail(window_with_focus != NULL);
1456 rox_spawn(window_with_focus->path, argv);
1459 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1461 g_return_if_fail(window_with_focus != NULL);
1463 filer_change_to(window_with_focus, home_dir, NULL);
1466 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1468 g_return_if_fail(window_with_focus != NULL);
1470 filer_open_parent(window_with_focus);
1473 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1475 g_return_if_fail(window_with_focus != NULL);
1477 change_to_parent(window_with_focus);
1480 static void resize(gpointer data, guint action, GtkWidget *widget)
1482 g_return_if_fail(window_with_focus != NULL);
1484 filer_window_autosize(window_with_focus, TRUE);
1487 static void new_window(gpointer data, guint action, GtkWidget *widget)
1489 g_return_if_fail(window_with_focus != NULL);
1491 if (o_unique_filer_windows)
1493 report_error(_("You can't open a second view onto "
1494 "this directory because the `Unique Windows' option "
1495 "is turned on in the Options window."));
1497 else
1498 filer_opendir(window_with_focus->path, window_with_focus);
1501 #if 0
1502 static void su_to_user(GtkWidget *dialog)
1504 char *argv[] = {
1505 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1506 GtkEntry *user;
1507 guchar *path;
1509 g_return_if_fail(dialog != NULL);
1511 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1512 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1514 g_return_if_fail(user != NULL && path != NULL);
1516 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1517 argv[3] = gtk_entry_get_text(user);
1518 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1519 argv[5] = path;
1521 if (!spawn(argv))
1522 report_error(_("fork: %s"), g_strerror(errno));
1524 g_free(argv[2]);
1525 g_free(argv[4]);
1527 gtk_widget_destroy(dialog);
1530 static void new_user(gpointer data, guint action, GtkWidget *widget)
1532 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1534 g_return_if_fail(window_with_focus != NULL);
1536 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1537 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1538 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1539 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1540 g_strdup(window_with_focus->path), g_free);
1542 vbox = gtk_vbox_new(FALSE, 4);
1543 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1544 gtk_box_pack_start(GTK_BOX(vbox),
1545 gtk_label_new(_("Browse as which user?")),
1546 TRUE, TRUE, 2);
1548 hbox = gtk_hbox_new(FALSE, 4);
1549 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1551 gtk_box_pack_start(GTK_BOX(hbox),
1552 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1554 entry = gtk_entry_new();
1555 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1556 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1557 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1558 gtk_widget_grab_focus(entry);
1559 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1560 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1561 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1563 hbox = gtk_hbox_new(TRUE, 0);
1564 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1566 button = gtk_button_new_with_label(_("OK"));
1567 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1568 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1569 gtk_window_set_default(GTK_WINDOW(dialog), button);
1570 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1571 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1573 button = gtk_button_new_with_label(_("Cancel"));
1574 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1575 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1576 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1577 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1578 GTK_OBJECT(dialog));
1580 gtk_widget_show_all(dialog);
1582 #endif
1584 static void close_window(gpointer data, guint action, GtkWidget *widget)
1586 g_return_if_fail(window_with_focus != NULL);
1588 gtk_widget_destroy(window_with_focus->window);
1591 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1593 MiniType type = (MiniType) action;
1595 g_return_if_fail(window_with_focus != NULL);
1597 /* Item needs to remain selected... */
1598 if (type == MINI_SHELL)
1599 window_with_focus->temp_item_selected = FALSE;
1601 minibuffer_show(window_with_focus, type);
1604 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1606 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1609 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1610 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1612 GList *items, *item;
1614 items = gtk_container_children(GTK_CONTAINER(menu));
1616 item = g_list_nth(items, from);
1617 while (item && n--)
1619 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1620 item = item->next;
1622 g_list_free(items);
1625 #ifndef GTK2
1626 /* This is called for every modified menu entry. We just use it to
1627 * find out if the menu has changed at all.
1629 static void set_mod(gboolean *mod, guchar *str)
1631 if (str && str[0] == '(')
1632 *mod = TRUE;
1634 #endif
1636 static void save_menus(void)
1638 char *menurc;
1640 #ifdef GTK2
1641 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1642 if (menurc)
1644 gtk_accel_map_save(menurc);
1645 g_free(menurc);
1647 #else
1648 menurc = choices_find_path_save(MENUS_NAME, PROJECT, FALSE);
1649 if (menurc)
1651 gboolean mod = FALSE;
1653 g_free(menurc);
1655 /* Find out if anything changed... */
1656 gtk_item_factory_dump_items(NULL, TRUE,
1657 (GtkPrintFunc) set_mod, &mod);
1659 /* Dump out if so... */
1660 if (mod)
1662 menurc = choices_find_path_save(MENUS_NAME,
1663 PROJECT, TRUE);
1664 g_return_if_fail(menurc != NULL);
1665 mark_menus_modified(TRUE);
1666 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
1667 mark_menus_modified(FALSE);
1668 g_free(menurc);
1671 #endif
1674 #ifdef GTK2
1675 static void keys_changed(gpointer data)
1677 save_menus();
1679 #else
1680 static void mark_modified(gpointer hash_key,
1681 gpointer value,
1682 gpointer user_data)
1684 GtkItemFactoryItem *item = (GtkItemFactoryItem *) value;
1686 item->modified = (gboolean) GPOINTER_TO_INT(user_data);
1689 /* Set or clear the 'modified' flag in all menu items. Messy... */
1690 static void mark_menus_modified(gboolean mod)
1692 GtkItemFactoryClass *class;
1694 class = gtk_type_class(GTK_TYPE_ITEM_FACTORY);
1696 g_hash_table_foreach(class->item_ht, mark_modified,
1697 GINT_TO_POINTER(mod));
1699 #endif
1701 static void select_nth_item(GtkMenuShell *shell, int n)
1703 GList *items, *nth;
1704 GtkWidget *item = NULL;
1706 items = gtk_container_children(GTK_CONTAINER(shell));
1707 nth = g_list_nth(items, n);
1709 g_return_if_fail(nth != NULL);
1711 item = (GtkWidget *) (nth->data);
1712 g_list_free(items);
1714 gtk_menu_shell_select_item(shell, item);
1717 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1719 Collection *collection;
1720 DirItem *item;
1721 gchar *path;
1723 g_return_if_fail(window_with_focus != NULL);
1725 collection = window_with_focus->collection;
1727 if (collection->number_selected < 1)
1729 char *prompt;
1731 switch (action)
1733 case FILE_COPY_ITEM:
1734 prompt = _("Copy ... ?");
1735 break;
1736 case FILE_RENAME_ITEM:
1737 prompt = _("Rename ... ?");
1738 break;
1739 case FILE_LINK_ITEM:
1740 prompt = _("Symlink ... ?");
1741 break;
1742 case FILE_OPEN_FILE:
1743 prompt = _("Shift Open ... ?");
1744 break;
1745 case FILE_HELP:
1746 prompt = _("Help about ... ?");
1747 break;
1748 case FILE_SHOW_FILE_INFO:
1749 prompt = _("Examine ... ?");
1750 break;
1751 case FILE_RUN_ACTION:
1752 prompt = _("Set run action for ... ?");
1753 break;
1754 case FILE_SET_ICON:
1755 prompt = _("Set icon for ... ?");
1756 break;
1757 case FILE_SEND_TO:
1758 prompt = _("Send ... to ... ?");
1759 break;
1760 case FILE_DELETE:
1761 prompt = _("DELETE ... ?");
1762 break;
1763 case FILE_USAGE:
1764 prompt = _("Count the size of ... ?");
1765 break;
1766 case FILE_CHMOD_ITEMS:
1767 prompt = _("Set permissions on ... ?");
1768 break;
1769 case FILE_FIND:
1770 prompt = _("Search inside ... ?");
1771 break;
1772 #ifdef HAVE_LIBVFS
1773 case FILE_OPEN_VFS_RPM:
1774 case FILE_OPEN_VFS_UTAR:
1775 case FILE_OPEN_VFS_UZIP:
1776 case FILE_OPEN_VFS_DEB:
1777 #else
1778 case FILE_OPEN_VFS_AVFS:
1779 #endif
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_FIND:
1806 find(window_with_focus);
1807 return;
1808 default:
1809 break;
1812 /* All the following actions require exactly one file selected */
1814 if (collection->number_selected > 1)
1816 report_error(_("You cannot do this to more than "
1817 "one item at a time"));
1818 return;
1821 item = selected_item(collection);
1822 g_return_if_fail(item != NULL);
1823 if (!item->image)
1824 item = dir_update_item(window_with_focus->directory,
1825 item->leafname);
1827 if (!item)
1829 report_error(_("Item no longer exists!"));
1830 return;
1833 path = make_path(window_with_focus->path, item->leafname)->str;
1835 switch (action)
1837 case FILE_COPY_ITEM:
1838 src_dest_action_item(path, item->image,
1839 _("Copy"), copy_cb);
1840 break;
1841 case FILE_RENAME_ITEM:
1842 src_dest_action_item(path, item->image,
1843 _("Rename"), rename_cb);
1844 break;
1845 case FILE_LINK_ITEM:
1846 src_dest_action_item(path, item->image,
1847 _("Symlink"), link_cb);
1848 break;
1849 case FILE_OPEN_FILE:
1850 filer_openitem(window_with_focus,
1851 selected_item_number(collection),
1852 OPEN_SAME_WINDOW | OPEN_SHIFT);
1853 break;
1854 case FILE_HELP:
1855 show_item_help(path, item);
1856 break;
1857 case FILE_SHOW_FILE_INFO:
1858 infobox_new(path);
1859 break;
1860 case FILE_RUN_ACTION:
1861 run_action(item);
1862 break;
1863 case FILE_SET_ICON:
1864 icon_set_handler_dialog(item, path);
1865 break;
1866 #ifdef HAVE_LIBVFS
1867 case FILE_OPEN_VFS_RPM:
1868 real_vfs_open(window_with_focus, item, "rpm");
1869 break;
1870 case FILE_OPEN_VFS_UTAR:
1871 real_vfs_open(window_with_focus, item, "utar");
1872 break;
1873 case FILE_OPEN_VFS_UZIP:
1874 real_vfs_open(window_with_focus, item, "uzip");
1875 break;
1876 case FILE_OPEN_VFS_DEB:
1877 real_vfs_open(window_with_focus, item, "deb");
1878 break;
1879 #else
1880 case FILE_OPEN_VFS_AVFS:
1881 open_vfs_avfs(window_with_focus, item);
1882 break;
1883 default:
1884 g_warning("Unknown action!");
1885 return;
1886 #endif