r1219: Internal changes to the options system. See text at start of options.c.
[rox-filer.git] / ROX-Filer / src / menu.c
blob080aedd1cd5cbd4b1d982631ca47123e3a5fdbb9
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 Option o_menu_iconsize, o_menu_xterm;
107 /* Static prototypes */
109 static void save_menus(void);
110 static void menu_closed(GtkWidget *widget);
111 static void items_sensitive(gboolean state);
112 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
113 gboolean (*callback)(guchar *current, guchar *new));
114 static gint save_to_file(GtkSavebox *savebox, guchar *pathname);
115 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new);
116 static gboolean link_cb(guchar *initial, guchar *path);
117 static void select_nth_item(GtkMenuShell *shell, int n);
118 static void new_file_type(gchar *templ);
119 static void do_send_to(gchar *templ);
120 static void show_send_to_menu(GList *paths, GdkEvent *event);
122 /* Note that for most of these callbacks none of the arguments are used. */
124 /* (action used in these three - DetailsType) */
125 static void huge_with(gpointer data, guint action, GtkWidget *widget);
126 static void large_with(gpointer data, guint action, GtkWidget *widget);
127 static void small_with(gpointer data, guint action, GtkWidget *widget);
129 static void sort_name(gpointer data, guint action, GtkWidget *widget);
130 static void sort_type(gpointer data, guint action, GtkWidget *widget);
131 static void sort_size(gpointer data, guint action, GtkWidget *widget);
132 static void sort_date(gpointer data, guint action, GtkWidget *widget);
134 static void hidden(gpointer data, guint action, GtkWidget *widget);
135 static void show_thumbs(gpointer data, guint action, GtkWidget *widget);
136 static void refresh(gpointer data, guint action, GtkWidget *widget);
138 static void file_op(gpointer data, FileOp action, GtkWidget *widget);
140 static void select_all(gpointer data, guint action, GtkWidget *widget);
141 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
142 static void invert_selection(gpointer data, guint action, GtkWidget *widget);
143 static void new_directory(gpointer data, guint action, GtkWidget *widget);
144 static void new_file(gpointer data, guint action, GtkWidget *widget);
145 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
147 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
148 static void open_parent(gpointer data, guint action, GtkWidget *widget);
149 static void home_directory(gpointer data, guint action, GtkWidget *widget);
150 static void new_window(gpointer data, guint action, GtkWidget *widget);
151 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
152 static void close_window(gpointer data, guint action, GtkWidget *widget);
154 /* (action used in this - MiniType) */
155 static void mini_buffer(gpointer data, guint action, GtkWidget *widget);
156 static void resize(gpointer data, guint action, GtkWidget *widget);
158 #ifdef GTK2
159 #define MENUS_NAME "menus2"
160 static void keys_changed(gpointer data);
161 #else
162 # define MENUS_NAME "menus"
163 static void mark_menus_modified(gboolean mod);
164 #endif
166 static GtkWidget *filer_menu; /* The popup filer menu */
167 static GtkWidget *filer_file_item; /* The File '' label */
168 static GtkWidget *filer_file_menu; /* The File '' menu */
169 static GtkWidget *file_shift_item; /* Shift Open label */
170 GtkWidget *display_large_menu; /* Display->Large With... */
171 GtkWidget *display_small_menu; /* Display->Small With... */
172 #ifdef HAVE_LIBVFS
173 static GtkWidget *filer_vfs_menu; /* The Open VFS menu */
174 #endif
175 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
176 static GtkWidget *filer_thumb_menu; /* The Show Thumbs item */
177 static GtkWidget *filer_new_window; /* The New Window item */
178 static GtkWidget *filer_new_menu; /* The New submenu */
180 /* Used for Copy, etc */
181 static GtkWidget *savebox = NULL;
182 static GtkWidget *check_relative = NULL;
183 static guchar *current_path = NULL;
184 static gboolean (*current_savebox_callback)(guchar *current, guchar *new);
186 #undef N_
187 #define N_(x) x
189 static GtkItemFactoryEntry filer_menu_def[] = {
190 {N_("Display"), NULL, NULL, 0, "<Branch>"},
191 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
192 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
193 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
194 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
195 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
196 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
197 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
198 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
199 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
200 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
201 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
202 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
203 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
204 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
205 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
206 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
207 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
208 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
209 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
210 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
211 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
212 {">", NULL, NULL, 0, "<Separator>"},
213 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
214 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
215 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
216 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
217 {">", NULL, NULL, 0, "<Separator>"},
218 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
219 {">" N_("Show Thumbnails"), NULL, show_thumbs, 0, "<ToggleItem>"},
220 {">" N_("Refresh"), NULL, refresh, 0, NULL},
221 {N_("File"), NULL, NULL, 0, "<Branch>"},
222 {">" N_("Copy..."), NULL, file_op, FILE_COPY_ITEM, NULL},
223 {">" N_("Rename..."), NULL, file_op, FILE_RENAME_ITEM, NULL},
224 {">" N_("Link..."), NULL, file_op, FILE_LINK_ITEM, NULL},
225 {">" N_("Shift Open"), NULL, file_op, FILE_OPEN_FILE, NULL},
226 {">" N_("Help"), NULL, file_op, FILE_HELP, NULL},
227 {">" N_("Info"), NULL, file_op, FILE_SHOW_FILE_INFO, NULL},
228 {">" N_("Set Run Action..."), NULL, file_op, FILE_RUN_ACTION, NULL},
229 {">" N_("Set Icon..."), NULL, file_op, FILE_SET_ICON, NULL},
230 #ifdef HAVE_LIBVFS
231 {">" N_("Open VFS"), NULL, NULL, 0, "<Branch>"},
232 {">>" N_("Unzip"), NULL, file_op, FILE_OPEN_VFS_UZIP, NULL},
233 {">>" N_("Untar"), NULL, file_op, FILE_OPEN_VFS_UTAR, NULL},
234 {">>" N_("Deb"), NULL, file_op, FILE_OPEN_VFS_DEB, NULL},
235 {">>" N_("RPM"), NULL, file_op, FILE_OPEN_VFS_RPM, NULL},
236 #else
237 {">" N_("Open AVFS"), NULL, file_op, FILE_OPEN_VFS_AVFS, NULL},
238 #endif
239 {">", NULL, NULL, 0, "<Separator>"},
240 {">" N_("Send To..."), NULL, file_op, FILE_SEND_TO, NULL},
241 {">" N_("Delete"), NULL, file_op, FILE_DELETE, NULL},
242 {">" N_("Disk Usage"), NULL, file_op, FILE_USAGE, NULL},
243 {">" N_("Permissions"), NULL, file_op, FILE_CHMOD_ITEMS, NULL},
244 {">" N_("Find"), NULL, file_op, FILE_FIND, NULL},
245 {N_("Select"), NULL, NULL, 0, "<Branch>"},
246 {">" N_("Select All"), NULL, select_all, 0, NULL},
247 {">" N_("Clear Selection"), NULL, clear_selection, 0, NULL},
248 {">" N_("Invert Selection"), NULL, invert_selection, 0, NULL},
249 {">" N_("Select If..."), NULL, mini_buffer, MINI_SELECT_IF, NULL},
250 {N_("Options..."), NULL, menu_show_options, 0, NULL},
251 {N_("New"), NULL, NULL, 0, "<Branch>"},
252 {">" N_("Directory"), NULL, new_directory, 0, NULL},
253 {">" N_("Blank file"), NULL, new_file, 0, NULL},
254 {N_("Xterm Here"), NULL, xterm_here, 0, NULL},
255 {N_("Window"), NULL, NULL, 0, "<Branch>"},
256 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
257 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
258 {">" N_("New Window"), NULL, new_window, 0, NULL},
259 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
260 {">" N_("Resize Window"), NULL, resize, 0, NULL},
261 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
263 {">" N_("Close Window"), NULL, close_window, 0, NULL},
264 {">", NULL, NULL, 0, "<Separator>"},
265 {">" N_("Enter Path..."), "slash", mini_buffer, MINI_PATH, NULL},
266 {">" N_("Shell Command..."), NULL, mini_buffer, MINI_SHELL, NULL},
267 {">", NULL, NULL, 0, "<Separator>"},
268 {">" N_("Show ROX-Filer Help"), "F1", menu_rox_help, 0, NULL},
272 #define GET_MENU_ITEM(var, menu) \
273 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
275 #define GET_SMENU_ITEM(var, menu, sub) \
276 do { \
277 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
278 var = gtk_item_factory_get_widget(item_factory, tmp); \
279 g_free(tmp); \
280 } while (0)
282 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
283 do { \
284 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
285 var = gtk_item_factory_get_widget(item_factory, tmp); \
286 g_free(tmp); \
287 } while (0)
289 void menu_init(void)
291 char *menurc;
292 GList *items;
293 guchar *tmp;
294 GtkWidget *item;
295 GtkTooltips *tips;
296 GtkItemFactory *item_factory;
298 filer_keys = gtk_accel_group_new();
299 item_factory = menu_create(filer_menu_def,
300 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
301 "<filer>", filer_keys);
303 GET_MENU_ITEM(filer_menu, "filer");
304 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
305 #ifdef HAVE_LIBVFS
306 GET_SSMENU_ITEM(filer_vfs_menu, "filer", "File", "Open VFS");
307 #endif
308 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
309 GET_SSMENU_ITEM(filer_thumb_menu, "filer", "Display",
310 "Show Thumbnails");
312 GET_SSMENU_ITEM(display_large_menu, "filer",
313 "Display", "Large, With...");
314 GET_SSMENU_ITEM(display_small_menu, "filer",
315 "Display", "Small, With...");
317 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
319 /* File '' label... */
320 items = gtk_container_children(GTK_CONTAINER(filer_menu));
321 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
322 g_list_free(items);
324 /* Shift Open... label */
325 items = gtk_container_children(GTK_CONTAINER(filer_file_menu));
326 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
327 g_list_free(items);
329 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
330 filer_new_window = GTK_BIN(item)->child;
332 menurc = choices_find_path_load(MENUS_NAME, PROJECT);
333 if (menurc)
335 #ifdef GTK2
336 gtk_accel_map_load(menurc);
337 #else
338 gtk_item_factory_parse_rc(menurc);
339 mark_menus_modified(FALSE);
340 #endif
341 g_free(menurc);
344 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
345 GTK_SIGNAL_FUNC(menu_closed), NULL);
346 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
347 GTK_SIGNAL_FUNC(menu_closed), NULL);
349 option_add_string(&o_menu_xterm, "menu_xterm", "xterm");
350 option_add_int(&o_menu_iconsize, "menu_iconsize", MIS_SMALL);
351 option_add_saver(save_menus);
353 tips = gtk_tooltips_new();
354 check_relative = gtk_check_button_new_with_label(_("Relative link"));
355 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative), TRUE);
356 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
357 gtk_tooltips_set_tip(tips, check_relative,
358 _("If on, the symlink will store the path from the "
359 "symlink to the target file. Use this if the symlink "
360 "and the target will be moved together.\n"
361 "If off, the path from the root directory is stored - "
362 "use this if the symlink may move but the target will "
363 "stay put."), NULL);
365 savebox = gtk_savebox_new();
366 gtk_box_pack_start(GTK_BOX(GTK_SAVEBOX(savebox)->vbox),
367 check_relative, FALSE, TRUE, 0);
368 gtk_widget_show(check_relative);
370 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_to_file",
371 GTK_SIGNAL_FUNC(save_to_file), NULL);
372 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_done",
373 GTK_SIGNAL_FUNC(gtk_widget_hide),
374 GTK_OBJECT(savebox));
376 #ifdef GTK2
377 g_signal_connect_object(G_OBJECT(filer_keys), "accel_changed",
378 (GCallback) keys_changed, NULL, 0);
379 #else
380 atexit(save_menus);
381 #endif
384 /* Name is in the form "<panel>" */
385 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
386 guchar *name, GtkAccelGroup *keys)
388 GtkItemFactory *item_factory;
389 GtkItemFactoryEntry *translated;
391 if (!keys)
393 keys = gtk_accel_group_new();
394 gtk_accel_group_lock(keys);
397 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
399 translated = translate_entries(def, n_entries);
400 gtk_item_factory_create_items(item_factory, n_entries,
401 translated, NULL);
402 free_translated_entries(translated, n_entries);
404 return item_factory;
407 /* Prevent the user from setting a short-cut on this item */
408 void menuitem_no_shortcuts(GtkWidget *item)
410 #ifdef GTK2
412 GtkMenuItem *menuitem = GTK_MENU_ITEM(item);
414 _gtk_widget_set_accel_path(item, NULL, NULL);
415 g_free(menuitem->accel_path);
416 menuitem->accel_path = NULL;
418 #else
419 gtk_widget_lock_accelerators(item);
420 #endif
423 static void items_sensitive(gboolean state)
425 int n = 9;
426 GList *items, *item;
428 items = item = gtk_container_children(GTK_CONTAINER(filer_file_menu));
429 while (item && n--)
431 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
432 item = item->next;
434 g_list_free(items);
436 #ifdef HAVE_LIBVFS
437 items = item = gtk_container_children(GTK_CONTAINER(filer_vfs_menu));
438 while (item)
440 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
441 item = item->next;
443 g_list_free(items);
444 #endif
447 /* 'data' is an array of three ints:
448 * [ pointer_x, pointer_y, item_under_pointer ]
450 void position_menu(GtkMenu *menu, gint *x, gint *y,
451 #ifdef GTK2
452 gboolean *push_in,
453 #endif
454 gpointer data)
456 int *pos = (int *) data;
457 GtkRequisition requisition;
458 GList *items, *next;
459 int y_shift = 0;
460 int item = pos[2];
462 next = items = gtk_container_children(GTK_CONTAINER(menu));
464 while (item >= 0 && next)
466 int h = ((GtkWidget *) next->data)->requisition.height;
468 if (item > 0)
469 y_shift += h;
470 else
471 y_shift += h / 2;
473 next = next->next;
474 item--;
477 g_list_free(items);
479 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
481 *x = pos[0] - (requisition.width * 7 / 8);
482 *y = pos[1] - y_shift;
484 *x = CLAMP(*x, 0, screen_width - requisition.width);
485 *y = CLAMP(*y, 0, screen_height - requisition.height);
487 #ifdef GTK2
488 *push_in = FALSE;
489 #endif
492 #if 0
493 /* Used when you menu-click on the Large or Small toolbar tools */
494 void show_style_menu(FilerWindow *filer_window,
495 GdkEventButton *event,
496 GtkWidget *menu)
498 int pos[3];
500 pos[0] = event->x_root;
501 pos[1] = event->y_root;
502 pos[2] = 0;
504 window_with_focus = filer_window;
506 popup_menu = menu;
508 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
509 (gpointer) pos, event->button, event->time);
511 #endif
513 static GList *menu_from_dir(GtkWidget *menu, const gchar *dname,
514 MenuIconStyle style, CallbackFn func,
515 gboolean separator, gboolean strip_ext)
517 GList *widgets = NULL;
518 DirItem *ditem;
519 DIR *dir;
520 struct dirent *ent;
521 GtkWidget *item;
523 dir = opendir(dname);
524 if (!dir)
525 goto out;
527 if (separator)
529 item = gtk_menu_item_new();
530 widgets = g_list_append(widgets, item);
531 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
534 while ((ent = readdir(dir)))
536 char *dot, *leaf;
537 GtkWidget *hbox;
538 GtkWidget *img;
539 GtkWidget *label;
540 gchar *fname;
541 GdkPixmap *icon;
542 GdkBitmap *mask;
544 /* Ignore hidden files */
545 if (ent->d_name[0] == '.')
546 continue;
548 /* Strip off extension, if any */
549 dot = strchr(ent->d_name, '.');
550 if (strip_ext && dot)
551 leaf = g_strndup(ent->d_name, dot - ent->d_name);
552 else
553 leaf = g_strdup(ent->d_name);
555 fname = g_strconcat(dname, "/", ent->d_name, NULL);
556 ditem = diritem_new(NULL);
557 diritem_restat(fname, ditem);
559 if (ditem->image && style != MIS_NONE)
561 switch (style) {
562 case MIS_HUGE:
563 if (!ditem->image->huge_pixmap)
564 pixmap_make_huge(ditem->image);
565 icon = ditem->image->huge_pixmap;
566 mask = ditem->image->huge_mask;
567 break;
568 case MIS_LARGE:
569 icon = ditem->image->pixmap;
570 mask = ditem->image->mask;
571 break;
573 case MIS_SMALL:
574 default:
575 if (!ditem->image->sm_pixmap)
576 pixmap_make_small(ditem->image);
577 icon = ditem->image->sm_pixmap;
578 mask = ditem->image->sm_mask;
579 break;
582 item = gtk_menu_item_new();
583 /* TODO: Find a way to allow short-cuts */
584 menuitem_no_shortcuts(item);
586 hbox = gtk_hbox_new(FALSE, 2);
587 gtk_container_add(GTK_CONTAINER(item), hbox);
589 img = gtk_pixmap_new(icon, mask);
590 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
592 label = gtk_label_new(leaf);
593 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
594 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
596 diritem_free(ditem);
598 else
599 item = gtk_menu_item_new_with_label(leaf);
601 g_free(leaf);
603 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
604 GTK_SIGNAL_FUNC(func), (GtkObject *) fname);
605 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
606 gtk_signal_connect_object(GTK_OBJECT(item), "destroy",
607 GTK_SIGNAL_FUNC(g_free), (GtkObject *) fname);
609 widgets = g_list_append(widgets, item);
612 closedir(dir);
613 out:
615 return widgets;
618 /* Scan the templates dir and create entries for the New menu */
619 static void update_new_files_menu(MenuIconStyle style)
621 static GList *widgets = NULL;
623 gchar *templ_dname = NULL;
625 if (widgets)
627 GList *next;
629 for (next = widgets; next; next = next->next)
630 gtk_widget_destroy((GtkWidget *) next->data);
632 g_list_free(widgets);
633 widgets = NULL;
636 templ_dname = choices_find_path_load("Templates", "");
637 if (templ_dname)
639 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
640 (CallbackFn) new_file_type, TRUE, TRUE);
641 g_free(templ_dname);
643 gtk_widget_show_all(filer_new_menu);
646 /* 'item' is the number of the item to appear under the pointer. */
647 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
649 int pos[3];
650 int button = 0;
651 guint32 time = 0;
653 if (event && (event->type == GDK_BUTTON_PRESS ||
654 event->type == GDK_BUTTON_RELEASE))
656 GdkEventButton *bev = (GdkEventButton *) event;
658 pos[0] = bev->x_root;
659 pos[1] = bev->y_root;
660 button = bev->button;
661 time = bev->time;
663 else if (event && event->type == GDK_KEY_PRESS)
665 GdkEventKey *kev = (GdkEventKey *) event;
667 get_pointer_xy(pos, pos + 1);
668 time = kev->time;
670 else
671 get_pointer_xy(pos, pos + 1);
673 pos[2] = item;
675 gtk_widget_show_all(menu);
676 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
677 position_menu, (gpointer) pos, button, time);
678 select_nth_item(GTK_MENU_SHELL(menu), item);
681 static MenuIconStyle get_menu_icon_style(void)
683 MenuIconStyle mis;
684 int display;
686 mis = o_menu_iconsize.int_value;
688 switch (mis)
690 case MIS_NONE: case MIS_SMALL: case MIS_LARGE: case MIS_HUGE:
691 return mis;
692 default:
693 break;
696 if (mis == MIS_CURRENT && window_with_focus)
698 switch (window_with_focus->display_style)
700 case HUGE_ICONS:
701 return MIS_HUGE;
702 case LARGE_ICONS:
703 return MIS_LARGE;
704 case SMALL_ICONS:
705 return MIS_SMALL;
706 default:
707 break;
711 display = o_display_size.int_value;
712 switch (display)
714 case HUGE_ICONS:
715 return MIS_HUGE;
716 case LARGE_ICONS:
717 return MIS_LARGE;
718 case SMALL_ICONS:
719 return MIS_SMALL;
720 default:
721 break;
724 return MIS_SMALL;
727 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
729 DirItem *file_item = NULL;
730 GdkModifierType state = 0;
732 updating_menu++;
734 /* Remove previous AppMenu, if any */
735 appmenu_remove();
737 window_with_focus = filer_window;
739 if (event->type == GDK_BUTTON_PRESS)
740 state = ((GdkEventButton *) event)->state;
741 else if (event->type == GDK_KEY_PRESS)
742 state = ((GdkEventKey *) event)->state;
744 if (filer_window->collection->number_selected == 0 && item >= 0)
746 filer_window->temp_item_selected = TRUE;
747 collection_select_item(filer_window->collection, item);
749 else
751 filer_window->temp_item_selected = FALSE;
754 /* Short-cut to the Send To menu */
755 if (state & GDK_SHIFT_MASK)
757 GList *paths;
759 updating_menu--;
761 if (filer_window->collection->number_selected == 0)
763 report_error(
764 _("You should Shift+Menu click over a file to "
765 "send it somewhere"));
766 return;
769 paths = filer_selected_items(filer_window);
771 show_send_to_menu(paths, event); /* (paths eaten) */
773 return;
777 GtkWidget *file_label, *file_menu;
778 Collection *collection = filer_window->collection;
779 GString *buffer;
780 DirItem *item;
782 file_label = filer_file_item;
783 file_menu = filer_file_menu;
784 gtk_check_menu_item_set_active(
785 GTK_CHECK_MENU_ITEM(filer_thumb_menu),
786 filer_window->show_thumbs);
787 gtk_check_menu_item_set_active(
788 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
789 filer_window->show_hidden);
790 buffer = g_string_new(NULL);
792 switch (collection->number_selected)
794 case 0:
795 g_string_assign(buffer, _("Next Click"));
796 items_sensitive(TRUE);
797 break;
798 case 1:
799 item = selected_item(filer_window->collection);
800 if (!item->image)
801 dir_update_item(filer_window->directory,
802 item->leafname);
803 items_sensitive(TRUE);
804 file_item = selected_item(
805 filer_window->collection);
806 g_string_sprintf(buffer, "%s '%s'",
807 basetype_name(file_item),
808 file_item->leafname);
809 if (!can_set_run_action(file_item))
810 menu_set_items_shaded(filer_file_menu,
811 TRUE, 6, 1);
812 break;
813 default:
814 items_sensitive(FALSE);
815 g_string_sprintf(buffer, _("%d items"),
816 collection->number_selected);
817 break;
819 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
820 g_string_free(buffer, TRUE);
822 menu_show_shift_action(file_shift_item, file_item,
823 collection->number_selected == 0);
824 if (file_item)
825 appmenu_add(make_path(filer_window->path,
826 file_item->leafname)->str,
827 file_item, filer_file_menu);
830 update_new_files_menu(get_menu_icon_style());
832 gtk_widget_set_sensitive(filer_new_window,
833 !o_unique_filer_windows.int_value);
835 popup_menu = (state & GDK_CONTROL_MASK)
836 ? filer_file_menu
837 : filer_menu;
839 updating_menu--;
841 show_popup_menu(popup_menu, event,
842 popup_menu == filer_file_menu ? 5 : 1);
845 static void menu_closed(GtkWidget *widget)
847 if (window_with_focus == NULL || widget != popup_menu)
848 return; /* Close panel item chosen? */
850 popup_menu = NULL;
852 if (window_with_focus->temp_item_selected)
854 collection_clear_selection(window_with_focus->collection);
855 window_with_focus->temp_item_selected = FALSE;
859 void target_callback(FilerWindow *filer_window,
860 gint item,
861 gpointer action)
863 Collection *collection = filer_window->collection;
865 g_return_if_fail(window_with_focus != NULL);
866 g_return_if_fail(window_with_focus == filer_window);
868 /* Don't grab the primary selection */
869 filer_window->temp_item_selected = TRUE;
871 collection_wink_item(collection, item);
872 collection_clear_except(collection, item);
873 file_op(NULL, GPOINTER_TO_INT(action), GTK_WIDGET(collection));
875 if (item < collection->number_of_items)
876 collection_unselect_item(collection, item);
877 filer_window->temp_item_selected = FALSE;
880 /* Set the text of the 'Shift Open...' menu item.
881 * If icon is NULL, reset the text and also shade it, unless 'next'.
883 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
885 guchar *shift_action = NULL;
887 if (item)
889 if (item->flags & ITEM_FLAG_MOUNT_POINT)
891 if (item->flags & ITEM_FLAG_MOUNTED)
892 shift_action = N_("Unmount");
893 else
894 shift_action = N_("Mount");
896 else if (item->flags & ITEM_FLAG_SYMLINK)
897 shift_action = N_("Show Target");
898 else if (item->base_type == TYPE_DIRECTORY)
899 shift_action = N_("Look Inside");
900 else if (item->base_type == TYPE_FILE)
901 shift_action = N_("Open As Text");
903 gtk_label_set_text(GTK_LABEL(menu_item),
904 shift_action ? _(shift_action)
905 : _("Shift Open"));
906 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
909 /* Actions */
911 static void huge_with(gpointer data, guint action, GtkWidget *widget)
913 display_set_layout(window_with_focus, HUGE_ICONS, action);
916 static void large_with(gpointer data, guint action, GtkWidget *widget)
918 display_set_layout(window_with_focus, LARGE_ICONS, action);
921 static void small_with(gpointer data, guint action, GtkWidget *widget)
923 display_set_layout(window_with_focus, SMALL_ICONS, action);
926 static void sort_name(gpointer data, guint action, GtkWidget *widget)
928 g_return_if_fail(window_with_focus != NULL);
930 display_set_sort_fn(window_with_focus, sort_by_name);
933 static void sort_type(gpointer data, guint action, GtkWidget *widget)
935 g_return_if_fail(window_with_focus != NULL);
937 display_set_sort_fn(window_with_focus, sort_by_type);
940 static void sort_date(gpointer data, guint action, GtkWidget *widget)
942 g_return_if_fail(window_with_focus != NULL);
944 display_set_sort_fn(window_with_focus, sort_by_date);
947 static void sort_size(gpointer data, guint action, GtkWidget *widget)
949 g_return_if_fail(window_with_focus != NULL);
951 display_set_sort_fn(window_with_focus, sort_by_size);
954 static void hidden(gpointer data, guint action, GtkWidget *widget)
956 if (updating_menu)
957 return;
959 g_return_if_fail(window_with_focus != NULL);
961 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
964 static void show_thumbs(gpointer data, guint action, GtkWidget *widget)
966 if (updating_menu)
967 return;
969 g_return_if_fail(window_with_focus != NULL);
971 display_set_thumbs(window_with_focus, !window_with_focus->show_thumbs);
974 static void refresh(gpointer data, guint action, GtkWidget *widget)
976 g_return_if_fail(window_with_focus != NULL);
978 full_refresh();
979 filer_update_dir(window_with_focus, TRUE);
982 static void delete(FilerWindow *filer_window)
984 GList *paths;
985 paths = filer_selected_items(filer_window);
986 action_delete(paths);
987 g_list_foreach(paths, (GFunc) g_free, NULL);
988 g_list_free(paths);
991 static void usage(FilerWindow *filer_window)
993 GList *paths;
994 paths = filer_selected_items(filer_window);
995 action_usage(paths);
996 g_list_foreach(paths, (GFunc) g_free, NULL);
997 g_list_free(paths);
1000 static void chmod_items(FilerWindow *filer_window)
1002 GList *paths;
1003 paths = filer_selected_items(filer_window);
1004 action_chmod(paths);
1005 g_list_foreach(paths, (GFunc) g_free, NULL);
1006 g_list_free(paths);
1009 static void find(FilerWindow *filer_window)
1011 GList *paths;
1012 paths = filer_selected_items(filer_window);
1013 action_find(paths);
1014 g_list_foreach(paths, (GFunc) g_free, NULL);
1015 g_list_free(paths);
1018 /* This pops up our savebox widget, cancelling any currently open one,
1019 * and allows the user to pick a new path for it.
1020 * Once the new path has been picked, the callback will be called with
1021 * both the current and new paths.
1022 * NOTE: This function unrefs 'image'!
1024 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
1025 gboolean (*callback)(guchar *current, guchar *new))
1027 g_return_if_fail(image != NULL);
1029 if (GTK_WIDGET_VISIBLE(savebox))
1030 gtk_widget_hide(savebox);
1032 if (callback == link_cb)
1033 gtk_widget_show(check_relative);
1034 else
1035 gtk_widget_hide(check_relative);
1037 if (current_path)
1038 g_free(current_path);
1039 current_path = g_strdup(path);
1040 current_savebox_callback = callback;
1042 gtk_window_set_title(GTK_WINDOW(savebox), title);
1043 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), current_path);
1044 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixmap, image->mask);
1045 pixmap_unref(image);
1047 gtk_widget_show(savebox);
1050 static gint save_to_file(GtkSavebox *savebox, guchar *pathname)
1052 g_return_val_if_fail(current_savebox_callback != NULL,
1053 GTK_XDS_SAVE_ERROR);
1055 return current_savebox_callback(current_path, pathname)
1056 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1059 static gboolean copy_cb(guchar *current, guchar *new)
1061 return action_with_leaf(action_copy, current, new);
1064 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new)
1066 char *new_dir, *leaf;
1067 GList *local_paths;
1069 if (new[0] != '/')
1071 report_error(_("New pathname is not absolute"));
1072 return FALSE;
1075 if (new[strlen(new) - 1] == '/')
1077 new_dir = g_strdup(new);
1078 leaf = NULL;
1080 else
1082 guchar *slash;
1084 slash = strrchr(new, '/');
1085 new_dir = g_strndup(new, slash - new);
1086 leaf = slash + 1;
1089 local_paths = g_list_append(NULL, current);
1090 action(local_paths, new_dir, leaf, -1);
1091 g_list_free(local_paths);
1093 g_free(new_dir);
1095 return TRUE;
1098 /* Open a savebox to act on the selected file.
1099 * Call 'callback' later to perform the operation.
1101 static void src_dest_action_item(guchar *path, MaskedPixmap *image,
1102 guchar *title,
1103 gboolean (*callback)(guchar *, guchar *))
1105 pixmap_ref(image);
1106 savebox_show(title, path, image, callback);
1109 static gboolean rename_cb(guchar *current, guchar *new)
1111 return action_with_leaf(action_move, current, new);
1114 static gboolean link_cb(guchar *initial, guchar *path)
1116 int err;
1118 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_relative)))
1120 guchar *rpath;
1122 rpath = get_relative_path(path, initial);
1123 err = symlink(rpath, path);
1125 g_free(rpath);
1127 else
1128 err = symlink(initial, path);
1130 if (err)
1132 report_error("symlink: %s", g_strerror(errno));
1133 return FALSE;
1136 dir_check_this(path);
1138 return TRUE;
1141 static void run_action(DirItem *item)
1143 if (can_set_run_action(item))
1144 type_set_handler_dialog(item->mime_type);
1145 else
1146 report_error(
1147 _("You can only set the run action for a "
1148 "regular file"));
1151 void open_home(gpointer data, guint action, GtkWidget *widget)
1153 filer_opendir(home_dir, NULL);
1156 #ifdef HAVE_LIBVFS
1157 static void real_vfs_open(FilerWindow *filer_window, DirItem *item, char *fs)
1159 gchar *path;
1161 path = g_strconcat(filer_window->path,
1162 "/",
1163 item->leafname,
1164 "#", fs, NULL);
1166 filer_change_to(filer_window, path, NULL);
1167 g_free(path);
1169 #else
1170 static void open_vfs_avfs(FilerWindow *filer_window, DirItem *item)
1172 gchar *path;
1174 path = g_strconcat(filer_window->path,
1175 "/", item->leafname, "#", NULL);
1177 filer_change_to(filer_window, path, NULL);
1178 g_free(path);
1180 #endif
1182 static void select_all(gpointer data, guint action, GtkWidget *widget)
1184 g_return_if_fail(window_with_focus != NULL);
1186 window_with_focus->temp_item_selected = FALSE;
1187 collection_select_all(window_with_focus->collection);
1190 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1192 g_return_if_fail(window_with_focus != NULL);
1194 window_with_focus->temp_item_selected = FALSE;
1195 collection_clear_selection(window_with_focus->collection);
1198 static void invert_selection(gpointer data, guint action, GtkWidget *widget)
1200 g_return_if_fail(window_with_focus != NULL);
1202 window_with_focus->temp_item_selected = FALSE;
1203 collection_invert_selection(window_with_focus->collection);
1206 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1208 options_show();
1211 static gboolean new_directory_cb(guchar *initial, guchar *path)
1213 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1215 report_error("mkdir: %s", g_strerror(errno));
1216 return FALSE;
1219 dir_check_this(path);
1221 if (filer_exists(window_with_focus))
1223 guchar *leaf;
1224 leaf = strrchr(path, '/');
1225 if (leaf)
1226 display_set_autoselect(window_with_focus, leaf + 1);
1229 return TRUE;
1232 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1234 g_return_if_fail(window_with_focus != NULL);
1236 savebox_show(_("New Directory"),
1237 make_path(window_with_focus->path, _("NewDir"))->str,
1238 type_to_icon(special_directory),
1239 new_directory_cb);
1242 static gboolean new_file_cb(guchar *initial, guchar *path)
1244 int fd;
1246 fd = open(path, O_CREAT | O_EXCL, 0666);
1248 if (fd == -1)
1250 report_error(_("Error creating '%s': %s"),
1251 path, g_strerror(errno));
1252 return FALSE;
1255 if (close(fd))
1256 report_error(_("Error creating '%s': %s"),
1257 path, g_strerror(errno));
1259 dir_check_this(path);
1261 if (filer_exists(window_with_focus))
1263 guchar *leaf;
1264 leaf = strrchr(path, '/');
1265 if (leaf)
1266 display_set_autoselect(window_with_focus, leaf + 1);
1269 return TRUE;
1272 static void new_file(gpointer data, guint action, GtkWidget *widget)
1274 g_return_if_fail(window_with_focus != NULL);
1276 savebox_show(_("New File"),
1277 make_path(window_with_focus->path, _("NewFile"))->str,
1278 type_to_icon(text_plain),
1279 new_file_cb);
1282 static gboolean new_file_type_cb(guchar *initial, guchar *path)
1284 gchar *templ, *templ_dname, *oleaf, *dest, *leaf;
1285 GList *paths;
1287 /* We can work out the template path from the initial name */
1288 oleaf = g_basename(initial);
1289 templ_dname = choices_find_path_load("Templates", "");
1290 if (!templ_dname)
1292 report_error(
1293 _("Error creating file: could not find the template for %s"),
1294 oleaf);
1295 return FALSE;
1298 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1299 g_free(templ_dname);
1301 dest = g_dirname(path);
1302 leaf = g_basename(path);
1303 paths = g_list_append(NULL, templ);
1305 action_copy(paths, dest, leaf, -1);
1307 g_list_free(paths);
1308 g_free(dest);
1309 g_free(templ);
1311 if (filer_exists(window_with_focus))
1312 display_set_autoselect(window_with_focus, leaf);
1314 return TRUE;
1317 static void do_send_to(gchar *templ)
1319 g_return_if_fail(send_to_paths != NULL);
1321 run_with_files(templ, send_to_paths);
1324 static void new_file_type(gchar *templ)
1326 gchar *leaf;
1327 MIME_type *type;
1329 g_return_if_fail(window_with_focus != NULL);
1331 leaf = g_basename(templ);
1332 type = type_get_type(templ);
1334 savebox_show(_("New File"),
1335 make_path(window_with_focus->path, leaf)->str,
1336 type_to_icon(type),
1337 new_file_type_cb);
1340 static void customise_send_to(gpointer data)
1342 GPtrArray *path;
1343 guchar *save;
1344 GString *dirs;
1345 int i;
1347 dirs = g_string_new(NULL);
1349 path = choices_list_dirs("");
1350 for (i = 0; i < path->len; i++)
1352 guchar *old = (guchar *) path->pdata[i];
1354 g_string_append(dirs, old);
1355 g_string_append(dirs, "SendTo\n");
1357 choices_free_list(path);
1359 save = choices_find_path_save("", "SendTo", TRUE);
1360 if (save)
1361 mkdir(save, 0777);
1363 report_error(
1364 _("The `Send To' menu provides a quick way to send some files "
1365 "to an application. The applications listed are those in "
1366 "the following directories:\n\n%s\n%s\n"
1367 "The `Send To' menu may be opened by Shift+Menu clicking "
1368 "over a file."),
1369 dirs->str,
1370 save ? _("I'll show you your SendTo directory now; you should "
1371 "symlink (Ctrl+Shift drag) any applications you want "
1372 "into it.")
1373 : _("Your CHOICESPATH variable setting prevents "
1374 "customisations - sorry."));
1376 g_string_free(dirs, TRUE);
1378 if (save)
1379 filer_opendir(save, NULL);
1382 /* Scan the SendTo dir and create and show the Send To menu.
1383 * The 'paths' list and every path in it is claimed, and will be
1384 * freed later -- don't free it yourself!
1386 static void show_send_to_menu(GList *paths, GdkEvent *event)
1388 GtkWidget *menu, *item;
1389 GPtrArray *path;
1390 int i;
1392 menu = gtk_menu_new();
1394 path = choices_list_dirs("SendTo");
1396 for (i = 0; i < path->len; i++)
1398 GList *widgets = NULL;
1399 guchar *dir = (guchar *) path->pdata[i];
1401 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1402 (CallbackFn) do_send_to,
1403 FALSE, FALSE);
1405 if (widgets)
1406 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1407 gtk_menu_item_new());
1409 g_list_free(widgets); /* TODO: Get rid of this */
1412 choices_free_list(path);
1414 item = gtk_menu_item_new_with_label(_("Customise"));
1415 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
1416 GTK_SIGNAL_FUNC(customise_send_to), NULL);
1417 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1419 if (send_to_paths)
1421 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1422 g_list_free(send_to_paths);
1424 send_to_paths = paths;
1426 gtk_signal_connect(GTK_OBJECT(menu), "unmap_event",
1427 GTK_SIGNAL_FUNC(menu_closed), NULL);
1429 popup_menu = menu;
1430 show_popup_menu(menu, event, 0);
1433 static void send_to(FilerWindow *filer_window)
1435 GList *paths;
1436 GdkEvent *event;
1438 paths = filer_selected_items(filer_window);
1439 event = gtk_get_current_event();
1441 /* Eats paths */
1442 show_send_to_menu(paths, event);
1444 gdk_event_free(event);
1447 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1449 char *argv[] = {"sh", "-c", NULL, NULL};
1451 argv[2] = o_menu_xterm.value;
1453 g_return_if_fail(window_with_focus != NULL);
1455 rox_spawn(window_with_focus->path, argv);
1458 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1460 g_return_if_fail(window_with_focus != NULL);
1462 filer_change_to(window_with_focus, home_dir, NULL);
1465 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1467 g_return_if_fail(window_with_focus != NULL);
1469 filer_open_parent(window_with_focus);
1472 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1474 g_return_if_fail(window_with_focus != NULL);
1476 change_to_parent(window_with_focus);
1479 static void resize(gpointer data, guint action, GtkWidget *widget)
1481 g_return_if_fail(window_with_focus != NULL);
1483 filer_window_autosize(window_with_focus, TRUE);
1486 static void new_window(gpointer data, guint action, GtkWidget *widget)
1488 g_return_if_fail(window_with_focus != NULL);
1490 if (o_unique_filer_windows.int_value)
1492 report_error(_("You can't open a second view onto "
1493 "this directory because the `Unique Windows' option "
1494 "is turned on in the Options window."));
1496 else
1497 filer_opendir(window_with_focus->path, window_with_focus);
1500 #if 0
1501 static void su_to_user(GtkWidget *dialog)
1503 char *argv[] = {
1504 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1505 GtkEntry *user;
1506 guchar *path;
1508 g_return_if_fail(dialog != NULL);
1510 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1511 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1513 g_return_if_fail(user != NULL && path != NULL);
1515 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1516 argv[3] = gtk_entry_get_text(user);
1517 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1518 argv[5] = path;
1520 if (!spawn(argv))
1521 report_error(_("fork: %s"), g_strerror(errno));
1523 g_free(argv[2]);
1524 g_free(argv[4]);
1526 gtk_widget_destroy(dialog);
1529 static void new_user(gpointer data, guint action, GtkWidget *widget)
1531 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1533 g_return_if_fail(window_with_focus != NULL);
1535 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1536 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1537 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1538 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1539 g_strdup(window_with_focus->path), g_free);
1541 vbox = gtk_vbox_new(FALSE, 4);
1542 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1543 gtk_box_pack_start(GTK_BOX(vbox),
1544 gtk_label_new(_("Browse as which user?")),
1545 TRUE, TRUE, 2);
1547 hbox = gtk_hbox_new(FALSE, 4);
1548 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1550 gtk_box_pack_start(GTK_BOX(hbox),
1551 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1553 entry = gtk_entry_new();
1554 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1555 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1556 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1557 gtk_widget_grab_focus(entry);
1558 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1559 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1560 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1562 hbox = gtk_hbox_new(TRUE, 0);
1563 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1565 button = gtk_button_new_with_label(_("OK"));
1566 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1567 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1568 gtk_window_set_default(GTK_WINDOW(dialog), button);
1569 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1570 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1572 button = gtk_button_new_with_label(_("Cancel"));
1573 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1574 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1575 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1576 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1577 GTK_OBJECT(dialog));
1579 gtk_widget_show_all(dialog);
1581 #endif
1583 static void close_window(gpointer data, guint action, GtkWidget *widget)
1585 g_return_if_fail(window_with_focus != NULL);
1587 gtk_widget_destroy(window_with_focus->window);
1590 static void mini_buffer(gpointer data, guint action, GtkWidget *widget)
1592 MiniType type = (MiniType) action;
1594 g_return_if_fail(window_with_focus != NULL);
1596 /* Item needs to remain selected... */
1597 if (type == MINI_SHELL)
1598 window_with_focus->temp_item_selected = FALSE;
1600 minibuffer_show(window_with_focus, type);
1603 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1605 filer_opendir(make_path(app_dir, "Help")->str, NULL);
1608 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1609 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1611 GList *items, *item;
1613 items = gtk_container_children(GTK_CONTAINER(menu));
1615 item = g_list_nth(items, from);
1616 while (item && n--)
1618 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1619 item = item->next;
1621 g_list_free(items);
1624 #ifndef GTK2
1625 /* This is called for every modified menu entry. We just use it to
1626 * find out if the menu has changed at all.
1628 static void set_mod(gboolean *mod, guchar *str)
1630 if (str && str[0] == '(')
1631 *mod = TRUE;
1633 #endif
1635 static void save_menus(void)
1637 char *menurc;
1639 #ifdef GTK2
1640 menurc = choices_find_path_save(MENUS_NAME, PROJECT, TRUE);
1641 if (menurc)
1643 gtk_accel_map_save(menurc);
1644 g_free(menurc);
1646 #else
1647 menurc = choices_find_path_save(MENUS_NAME, PROJECT, FALSE);
1648 if (menurc)
1650 gboolean mod = FALSE;
1652 g_free(menurc);
1654 /* Find out if anything changed... */
1655 gtk_item_factory_dump_items(NULL, TRUE,
1656 (GtkPrintFunc) set_mod, &mod);
1658 /* Dump out if so... */
1659 if (mod)
1661 menurc = choices_find_path_save(MENUS_NAME,
1662 PROJECT, TRUE);
1663 g_return_if_fail(menurc != NULL);
1664 mark_menus_modified(TRUE);
1665 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
1666 mark_menus_modified(FALSE);
1667 g_free(menurc);
1670 #endif
1673 #ifdef GTK2
1674 static void keys_changed(gpointer data)
1676 save_menus();
1678 #else
1679 static void mark_modified(gpointer hash_key,
1680 gpointer value,
1681 gpointer user_data)
1683 GtkItemFactoryItem *item = (GtkItemFactoryItem *) value;
1685 item->modified = (gboolean) GPOINTER_TO_INT(user_data);
1688 /* Set or clear the 'modified' flag in all menu items. Messy... */
1689 static void mark_menus_modified(gboolean mod)
1691 GtkItemFactoryClass *class;
1693 class = gtk_type_class(GTK_TYPE_ITEM_FACTORY);
1695 g_hash_table_foreach(class->item_ht, mark_modified,
1696 GINT_TO_POINTER(mod));
1698 #endif
1700 static void select_nth_item(GtkMenuShell *shell, int n)
1702 GList *items, *nth;
1703 GtkWidget *item = NULL;
1705 items = gtk_container_children(GTK_CONTAINER(shell));
1706 nth = g_list_nth(items, n);
1708 g_return_if_fail(nth != NULL);
1710 item = (GtkWidget *) (nth->data);
1711 g_list_free(items);
1713 gtk_menu_shell_select_item(shell, item);
1716 static void file_op(gpointer data, FileOp action, GtkWidget *widget)
1718 Collection *collection;
1719 DirItem *item;
1720 gchar *path;
1722 g_return_if_fail(window_with_focus != NULL);
1724 collection = window_with_focus->collection;
1726 if (collection->number_selected < 1)
1728 char *prompt;
1730 switch (action)
1732 case FILE_COPY_ITEM:
1733 prompt = _("Copy ... ?");
1734 break;
1735 case FILE_RENAME_ITEM:
1736 prompt = _("Rename ... ?");
1737 break;
1738 case FILE_LINK_ITEM:
1739 prompt = _("Symlink ... ?");
1740 break;
1741 case FILE_OPEN_FILE:
1742 prompt = _("Shift Open ... ?");
1743 break;
1744 case FILE_HELP:
1745 prompt = _("Help about ... ?");
1746 break;
1747 case FILE_SHOW_FILE_INFO:
1748 prompt = _("Examine ... ?");
1749 break;
1750 case FILE_RUN_ACTION:
1751 prompt = _("Set run action for ... ?");
1752 break;
1753 case FILE_SET_ICON:
1754 prompt = _("Set icon for ... ?");
1755 break;
1756 case FILE_SEND_TO:
1757 prompt = _("Send ... to ... ?");
1758 break;
1759 case FILE_DELETE:
1760 prompt = _("DELETE ... ?");
1761 break;
1762 case FILE_USAGE:
1763 prompt = _("Count the size of ... ?");
1764 break;
1765 case FILE_CHMOD_ITEMS:
1766 prompt = _("Set permissions on ... ?");
1767 break;
1768 case FILE_FIND:
1769 prompt = _("Search inside ... ?");
1770 break;
1771 #ifdef HAVE_LIBVFS
1772 case FILE_OPEN_VFS_RPM:
1773 case FILE_OPEN_VFS_UTAR:
1774 case FILE_OPEN_VFS_UZIP:
1775 case FILE_OPEN_VFS_DEB:
1776 #else
1777 case FILE_OPEN_VFS_AVFS:
1778 #endif
1779 prompt = _("Look inside ... ?");
1780 break;
1781 default:
1782 g_warning("Unknown action!");
1783 return;
1785 filer_target_mode(window_with_focus, target_callback,
1786 GINT_TO_POINTER(action), prompt);
1787 return;
1790 switch (action)
1792 case FILE_SEND_TO:
1793 send_to(window_with_focus);
1794 return;
1795 case FILE_DELETE:
1796 delete(window_with_focus);
1797 return;
1798 case FILE_USAGE:
1799 usage(window_with_focus);
1800 return;
1801 case FILE_CHMOD_ITEMS:
1802 chmod_items(window_with_focus);
1803 return;
1804 case FILE_FIND:
1805 find(window_with_focus);
1806 return;
1807 default:
1808 break;
1811 /* All the following actions require exactly one file selected */
1813 if (collection->number_selected > 1)
1815 report_error(_("You cannot do this to more than "
1816 "one item at a time"));
1817 return;
1820 item = selected_item(collection);
1821 g_return_if_fail(item != NULL);
1822 if (!item->image)
1823 item = dir_update_item(window_with_focus->directory,
1824 item->leafname);
1826 if (!item)
1828 report_error(_("Item no longer exists!"));
1829 return;
1832 path = make_path(window_with_focus->path, item->leafname)->str;
1834 switch (action)
1836 case FILE_COPY_ITEM:
1837 src_dest_action_item(path, item->image,
1838 _("Copy"), copy_cb);
1839 break;
1840 case FILE_RENAME_ITEM:
1841 src_dest_action_item(path, item->image,
1842 _("Rename"), rename_cb);
1843 break;
1844 case FILE_LINK_ITEM:
1845 src_dest_action_item(path, item->image,
1846 _("Symlink"), link_cb);
1847 break;
1848 case FILE_OPEN_FILE:
1849 filer_openitem(window_with_focus,
1850 selected_item_number(collection),
1851 OPEN_SAME_WINDOW | OPEN_SHIFT);
1852 break;
1853 case FILE_HELP:
1854 show_item_help(path, item);
1855 break;
1856 case FILE_SHOW_FILE_INFO:
1857 infobox_new(path);
1858 break;
1859 case FILE_RUN_ACTION:
1860 run_action(item);
1861 break;
1862 case FILE_SET_ICON:
1863 icon_set_handler_dialog(item, path);
1864 break;
1865 #ifdef HAVE_LIBVFS
1866 case FILE_OPEN_VFS_RPM:
1867 real_vfs_open(window_with_focus, item, "rpm");
1868 break;
1869 case FILE_OPEN_VFS_UTAR:
1870 real_vfs_open(window_with_focus, item, "utar");
1871 break;
1872 case FILE_OPEN_VFS_UZIP:
1873 real_vfs_open(window_with_focus, item, "uzip");
1874 break;
1875 case FILE_OPEN_VFS_DEB:
1876 real_vfs_open(window_with_focus, item, "deb");
1877 break;
1878 #else
1879 case FILE_OPEN_VFS_AVFS:
1880 open_vfs_avfs(window_with_focus, item);
1881 break;
1882 #endif
1883 default:
1884 g_warning("Unknown action!");
1885 return;