r821: Fixed some bugs in the session handling.
[rox-filer.git] / ROX-Filer / src / menu.c
blob368a01da46771aed74fa812a40b57800c9431a6d
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2001, 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 "appmenu.h"
56 #include "usericons.h"
57 #include "infobox.h"
59 #define C_ "<control>"
61 typedef enum menu_icon_style {
62 MIS_NONE, MIS_SMALL, MIS_LARGE, MIS_HUGE,
63 MIS_CURRENT, /* As per current filer window */
64 MIS_DEFAULT
65 } MenuIconStyle;
67 typedef void (*ActionFn)(GList *paths, char *dest_dir, char *leaf);
68 typedef void MenuCallback(GtkWidget *widget, gpointer data);
70 GtkAccelGroup *filer_keys;
71 GtkAccelGroup *pinboard_keys;
73 GtkWidget *popup_menu = NULL; /* Currently open menu */
75 static gint updating_menu = 0; /* Non-zero => ignore activations */
76 static GList *send_to_paths = NULL;
78 /* Static prototypes */
80 static void save_menus(void);
81 static void menu_closed(GtkWidget *widget);
82 static void items_sensitive(gboolean state);
83 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
84 gboolean (*callback)(guchar *current, guchar *new));
85 static gint save_to_file(GtkSavebox *savebox, guchar *pathname);
86 static void mark_menus_modified(gboolean mod);
87 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new);
88 static gboolean link_cb(guchar *initial, guchar *path);
89 static void select_nth_item(GtkMenuShell *shell, int n);
90 static void new_file_type(gchar *templ);
91 static void do_send_to(gchar *templ);
92 static void show_send_to_menu(GList *paths, GdkEvent *event);
94 /* Note that for most of these callbacks none of the arguments are used. */
96 /* (action used in these two - DetailsType) */
97 static void huge_with(gpointer data, guint action, GtkWidget *widget);
98 static void large_with(gpointer data, guint action, GtkWidget *widget);
99 static void small_with(gpointer data, guint action, GtkWidget *widget);
101 static void sort_name(gpointer data, guint action, GtkWidget *widget);
102 static void sort_type(gpointer data, guint action, GtkWidget *widget);
103 static void sort_size(gpointer data, guint action, GtkWidget *widget);
104 static void sort_date(gpointer data, guint action, GtkWidget *widget);
106 static void hidden(gpointer data, guint action, GtkWidget *widget);
107 static void refresh(gpointer data, guint action, GtkWidget *widget);
108 static void create_thumbs(gpointer data, guint action, GtkWidget *widget);
110 static void copy_item(gpointer data, guint action, GtkWidget *widget);
111 static void rename_item(gpointer data, guint action, GtkWidget *widget);
112 static void link_item(gpointer data, guint action, GtkWidget *widget);
113 static void open_file(gpointer data, guint action, GtkWidget *widget);
114 static void help(gpointer data, guint action, GtkWidget *widget);
115 static void show_file_info(gpointer data, guint action, GtkWidget *widget);
116 static void send_to(gpointer data, guint action, GtkWidget *widget);
117 static void delete(gpointer data, guint action, GtkWidget *widget);
118 static void usage(gpointer data, guint action, GtkWidget *widget);
119 static void chmod_items(gpointer data, guint action, GtkWidget *widget);
120 static void find(gpointer data, guint action, GtkWidget *widget);
122 #ifdef HAVE_LIBVFS
123 static void open_vfs_rpm(gpointer data, guint action, GtkWidget *widget);
124 static void open_vfs_utar(gpointer data, guint action, GtkWidget *widget);
125 static void open_vfs_uzip(gpointer data, guint action, GtkWidget *widget);
126 static void open_vfs_deb(gpointer data, guint action, GtkWidget *widget);
127 #else
128 static void open_vfs_avfs(gpointer data, guint action, GtkWidget *widget);
129 #endif
131 static void select_all(gpointer data, guint action, GtkWidget *widget);
132 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
133 static void new_directory(gpointer data, guint action, GtkWidget *widget);
134 static void new_file(gpointer data, guint action, GtkWidget *widget);
135 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
137 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
138 static void open_parent(gpointer data, guint action, GtkWidget *widget);
139 static void home_directory(gpointer data, guint action, GtkWidget *widget);
140 static void new_window(gpointer data, guint action, GtkWidget *widget);
141 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
142 static void close_window(gpointer data, guint action, GtkWidget *widget);
143 static void enter_path(gpointer data, guint action, GtkWidget *widget);
144 static void shell_command(gpointer data, guint action, GtkWidget *widget);
145 static void run_action(gpointer data, guint action, GtkWidget *widget);
146 static void set_icon(gpointer data, guint action, GtkWidget *widget);
147 static void select_if(gpointer data, guint action, GtkWidget *widget);
148 static void resize(gpointer data, guint action, GtkWidget *widget);
151 static GtkWidget *filer_menu; /* The popup filer menu */
152 static GtkWidget *filer_file_item; /* The File '' label */
153 static GtkWidget *filer_file_menu; /* The File '' menu */
154 static GtkWidget *file_shift_item; /* Shift Open label */
155 GtkWidget *display_large_menu; /* Display->Large With... */
156 GtkWidget *display_small_menu; /* Display->Small With... */
157 #ifdef HAVE_LIBVFS
158 static GtkWidget *filer_vfs_menu; /* The Open VFS menu */
159 #endif
160 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
161 static GtkWidget *filer_new_window; /* The New Window item */
162 static GtkWidget *filer_new_menu; /* The New submenu */
164 /* Used for Copy, etc */
165 static GtkWidget *savebox = NULL;
166 static GtkWidget *check_relative = NULL;
167 static guchar *current_path = NULL;
168 static gboolean (*current_savebox_callback)(guchar *current, guchar *new);
170 #undef N_
171 #define N_(x) x
173 static GtkItemFactoryEntry filer_menu_def[] = {
174 {N_("Display"), NULL, NULL, 0, "<Branch>"},
175 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
176 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
177 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
178 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
179 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
180 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
181 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
182 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
183 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
184 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
185 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
186 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
187 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
188 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
189 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
190 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
191 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
192 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
193 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
194 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
195 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
196 {">", NULL, NULL, 0, "<Separator>"},
197 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
198 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
199 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
200 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
201 {">", NULL, NULL, 0, "<Separator>"},
202 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
203 {">" N_("Refresh"), NULL, refresh, 0, NULL},
204 {">" N_("Create Thumbs"), NULL, create_thumbs, 0, NULL},
205 {N_("File"), NULL, NULL, 0, "<Branch>"},
206 {">" N_("Copy..."), NULL, copy_item, 0, NULL},
207 {">" N_("Rename..."), NULL, rename_item, 0, NULL},
208 {">" N_("Link..."), NULL, link_item, 0, NULL},
209 {">" N_("Shift Open"), NULL, open_file, 0, NULL},
210 {">" N_("Help"), NULL, help, 0, NULL},
211 {">" N_("Info"), NULL, show_file_info, 0, NULL},
212 {">" N_("Set Run Action..."), NULL, run_action, 0, NULL},
213 {">" N_("Set Icon..."), NULL, set_icon, 0, NULL},
214 #ifdef HAVE_LIBVFS
215 {">" N_("Open VFS"), NULL, NULL, 0, "<Branch>"},
216 {">>" N_("Unzip"), NULL, open_vfs_uzip, 0, NULL},
217 {">>" N_("Untar"), NULL, open_vfs_utar, 0, NULL},
218 {">>" N_("Deb"), NULL, open_vfs_deb, 0, NULL},
219 {">>" N_("RPM"), NULL, open_vfs_rpm, 0, NULL},
220 #else
221 {">" N_("Open AVFS"), NULL, open_vfs_avfs, 0, NULL},
222 #endif
223 {">", NULL, NULL, 0, "<Separator>"},
224 {">" N_("Send To..."), NULL, send_to, 0, NULL},
225 {">" N_("Delete"), NULL, delete, 0, NULL},
226 {">" N_("Disk Usage"), NULL, usage, 0, NULL},
227 {">" N_("Permissions"), NULL, chmod_items, 0, NULL},
228 {">" N_("Find"), NULL, find, 0, NULL},
229 {N_("Select All"), NULL, select_all, 0, NULL},
230 {N_("Clear Selection"), NULL, clear_selection, 0, NULL},
231 {N_("Options..."), NULL, menu_show_options, 0, NULL},
232 {N_("New"), NULL, NULL, 0, "<Branch>"},
233 {">" N_("Directory"), NULL, new_directory, 0, NULL},
234 {">" N_("Blank file"), NULL, new_file, 0, NULL},
235 {N_("Xterm Here"), NULL, xterm_here, 0, NULL},
236 {N_("Window"), NULL, NULL, 0, "<Branch>"},
237 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
238 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
239 {">" N_("New Window"), NULL, new_window, 0, NULL},
240 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
241 {">" N_("Resize Window"), NULL, resize, 0, NULL},
242 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
244 {">" N_("Close Window"), NULL, close_window, 0, NULL},
245 {">", NULL, NULL, 0, "<Separator>"},
246 {">" N_("Enter Path..."), NULL, enter_path, 0, NULL},
247 {">" N_("Shell Command..."), NULL, shell_command, 0, NULL},
248 {">" N_("Select If..."), NULL, select_if, 0, NULL},
249 {">", NULL, NULL, 0, "<Separator>"},
250 {">" N_("Show ROX-Filer Help"), NULL, menu_rox_help, 0, NULL},
254 #define GET_MENU_ITEM(var, menu) \
255 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
257 #define GET_SMENU_ITEM(var, menu, sub) \
258 do { \
259 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
260 var = gtk_item_factory_get_widget(item_factory, tmp); \
261 g_free(tmp); \
262 } while (0)
264 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
265 do { \
266 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
267 var = gtk_item_factory_get_widget(item_factory, tmp); \
268 g_free(tmp); \
269 } while (0)
271 /* Creates menu <name> from the <name>_menu_def array.
272 * The accel group <name>_keys must also have been created.
273 * All menu items are translated. Sets 'item_factory'.
275 #define MAKE_MENU(name) \
276 do { \
277 GtkItemFactoryEntry *translated; \
278 int n_entries; \
280 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, \
281 "<" #name ">", name ## _keys); \
283 n_entries = sizeof(name ## _menu_def) / sizeof(*name ## _menu_def); \
284 translated = translate_entries(name ## _menu_def, n_entries); \
285 gtk_item_factory_create_items(item_factory, n_entries, \
286 translated, NULL); \
287 free_translated_entries(translated, n_entries); \
288 } while (0)
290 void menu_init(void)
292 char *menurc;
293 GList *items;
294 guchar *tmp;
295 GtkWidget *item;
296 GtkTooltips *tips;
297 GtkItemFactory *item_factory;
299 filer_keys = gtk_accel_group_new();
300 MAKE_MENU(filer);
302 GET_MENU_ITEM(filer_menu, "filer");
303 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
304 #ifdef HAVE_LIBVFS
305 GET_SSMENU_ITEM(filer_vfs_menu, "filer", "File", "Open VFS");
306 #endif
307 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
309 GET_SSMENU_ITEM(display_large_menu, "filer",
310 "Display", "Large, With...");
311 GET_SSMENU_ITEM(display_small_menu, "filer",
312 "Display", "Small, With...");
314 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
316 /* File '' label... */
317 items = gtk_container_children(GTK_CONTAINER(filer_menu));
318 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
319 g_list_free(items);
321 /* Shift Open... label */
322 items = gtk_container_children(GTK_CONTAINER(filer_file_menu));
323 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
324 g_list_free(items);
326 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
327 filer_new_window = GTK_BIN(item)->child;
329 menurc = choices_find_path_load("menus", PROJECT);
330 if (menurc)
332 gtk_item_factory_parse_rc(menurc);
333 mark_menus_modified(FALSE);
334 g_free(menurc);
337 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
338 GTK_SIGNAL_FUNC(menu_closed), NULL);
339 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
340 GTK_SIGNAL_FUNC(menu_closed), NULL);
342 option_add_string("menu_xterm", "xterm", NULL);
343 option_add_int("menu_iconsize", MIS_SMALL, NULL);
344 option_add_saver(save_menus);
346 tips = gtk_tooltips_new();
347 check_relative = gtk_check_button_new_with_label(_("Relative link"));
348 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative), TRUE);
349 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
350 gtk_tooltips_set_tip(tips, check_relative,
351 _("If on, the symlink will store the path from the "
352 "symlink to the target file. Use this if the symlink "
353 "and the target will be moved together.\n"
354 "If off, the path from the root directory is stored - "
355 "use this if the symlink may move but the target will "
356 "stay put."), NULL);
358 savebox = gtk_savebox_new();
359 gtk_box_pack_start(GTK_BOX(GTK_SAVEBOX(savebox)->vbox),
360 check_relative, FALSE, TRUE, 0);
361 gtk_widget_show(check_relative);
363 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_to_file",
364 GTK_SIGNAL_FUNC(save_to_file), NULL);
365 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_done",
366 GTK_SIGNAL_FUNC(gtk_widget_hide),
367 GTK_OBJECT(savebox));
369 atexit(save_menus);
372 /* Name is in the form "<panel>" */
373 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
374 guchar *name)
376 GtkItemFactory *item_factory;
377 GtkItemFactoryEntry *translated;
378 GtkAccelGroup *keys;
380 keys = gtk_accel_group_new();
382 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
384 translated = translate_entries(def, n_entries);
385 gtk_item_factory_create_items(item_factory, n_entries,
386 translated, NULL);
387 free_translated_entries(translated, n_entries);
389 gtk_accel_group_lock(keys);
391 return item_factory;
394 static void items_sensitive(gboolean state)
396 int n = 9;
397 GList *items, *item;
399 items = item = gtk_container_children(GTK_CONTAINER(filer_file_menu));
400 while (item && n--)
402 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
403 item = item->next;
405 g_list_free(items);
407 #ifdef HAVE_LIBVFS
408 items = item = gtk_container_children(GTK_CONTAINER(filer_vfs_menu));
409 while (item)
411 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
412 item = item->next;
414 g_list_free(items);
415 #endif
418 /* 'data' is an array of three ints:
419 * [ pointer_x, pointer_y, item_under_pointer ]
421 void position_menu(GtkMenu *menu, gint *x, gint *y,
422 #ifdef GTK2
423 gboolean *push_in,
424 #endif
425 gpointer data)
427 int *pos = (int *) data;
428 GtkRequisition requisition;
429 GList *items, *next;
430 int y_shift = 0;
431 int item = pos[2];
433 next = items = gtk_container_children(GTK_CONTAINER(menu));
435 while (item >= 0 && next)
437 int h = ((GtkWidget *) next->data)->requisition.height;
439 if (item > 0)
440 y_shift += h;
441 else
442 y_shift += h / 2;
444 next = next->next;
445 item--;
448 g_list_free(items);
450 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
452 *x = pos[0] - (requisition.width * 7 / 8);
453 *y = pos[1] - y_shift;
455 *x = CLAMP(*x, 0, screen_width - requisition.width);
456 *y = CLAMP(*y, 0, screen_height - requisition.height);
458 #ifdef GTK2
459 *push_in = FALSE;
460 #endif
463 #if 0
464 /* Used when you menu-click on the Large or Small toolbar tools */
465 void show_style_menu(FilerWindow *filer_window,
466 GdkEventButton *event,
467 GtkWidget *menu)
469 int pos[3];
471 pos[0] = event->x_root;
472 pos[1] = event->y_root;
473 pos[2] = 0;
475 window_with_focus = filer_window;
477 popup_menu = menu;
479 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
480 (gpointer) pos, event->button, event->time);
482 #endif
484 static GList *menu_from_dir(GtkWidget *menu, const gchar *dname,
485 MenuIconStyle style, CallbackFn func,
486 gboolean separator, gboolean strip_ext)
488 GList *widgets = NULL;
489 DirItem ditem;
490 DIR *dir;
491 struct dirent *ent;
492 GtkWidget *item;
494 dir = opendir(dname);
495 if (!dir)
496 goto out;
498 if (separator)
500 item = gtk_menu_item_new();
501 widgets = g_list_append(widgets, item);
502 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
505 while ((ent = readdir(dir)))
507 char *dot, *leaf;
508 GtkWidget *hbox;
509 GtkWidget *img;
510 GtkWidget *label;
511 gchar *fname;
512 GdkPixmap *icon;
513 GdkBitmap *mask;
515 /* Ignore hidden files */
516 if (ent->d_name[0] == '.')
517 continue;
519 /* Strip off extension, if any */
520 dot = strchr(ent->d_name, '.');
521 if (strip_ext && dot)
522 leaf = g_strndup(ent->d_name, dot - ent->d_name);
523 else
524 leaf = g_strdup(ent->d_name);
526 fname = g_strconcat(dname, "/", ent->d_name, NULL);
527 diritem_stat(fname, &ditem, FALSE);
529 if (ditem.image && style != MIS_NONE)
531 switch (style) {
532 case MIS_HUGE:
533 if (!ditem.image->huge_pixmap)
534 pixmap_make_huge(ditem.image);
535 icon = ditem.image->huge_pixmap;
536 mask = ditem.image->huge_mask;
537 break;
538 case MIS_LARGE:
539 icon = ditem.image->pixmap;
540 mask = ditem.image->mask;
541 break;
543 case MIS_SMALL:
544 default:
545 if (!ditem.image->sm_pixmap)
546 pixmap_make_small(ditem.image);
547 icon = ditem.image->sm_pixmap;
548 mask = ditem.image->sm_mask;
549 break;
552 item = gtk_menu_item_new();
553 /* TODO: Find a way to allow short-cuts */
554 gtk_widget_lock_accelerators(item);
556 hbox = gtk_hbox_new(FALSE, 2);
557 gtk_container_add(GTK_CONTAINER(item), hbox);
559 img = gtk_pixmap_new(icon, mask);
560 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
562 label = gtk_label_new(leaf);
563 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
564 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
566 diritem_clear(&ditem);
568 else
569 item = gtk_menu_item_new_with_label(leaf);
571 g_free(leaf);
573 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
574 GTK_SIGNAL_FUNC(func), (GtkObject *) fname);
575 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
576 gtk_signal_connect_object(GTK_OBJECT(item), "destroy",
577 GTK_SIGNAL_FUNC(g_free), (GtkObject *) fname);
579 widgets = g_list_append(widgets, item);
582 closedir(dir);
583 out:
585 return widgets;
588 /* Scan the templates dir and create entries for the New menu */
589 static void update_new_files_menu(MenuIconStyle style)
591 static GList *widgets = NULL;
593 gchar *templ_dname = NULL;
595 if (widgets)
597 GList *next;
599 for (next = widgets; next; next = next->next)
600 gtk_widget_destroy((GtkWidget *) next->data);
602 g_list_free(widgets);
603 widgets = NULL;
606 templ_dname = choices_find_path_load("Templates", "");
607 if (templ_dname)
609 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
610 (CallbackFn) new_file_type, TRUE, TRUE);
611 g_free(templ_dname);
613 gtk_widget_show_all(filer_new_menu);
616 /* 'item' is the number of the item to appear under the pointer. */
617 void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
619 int pos[3];
620 int button = 0;
621 guint32 time = 0;
623 if (event && (event->type == GDK_BUTTON_PRESS ||
624 event->type == GDK_BUTTON_RELEASE))
626 GdkEventButton *bev = (GdkEventButton *) event;
628 pos[0] = bev->x_root;
629 pos[1] = bev->y_root;
630 button = bev->button;
631 time = bev->time;
633 else if (event && event->type == GDK_KEY_PRESS)
635 GdkEventKey *kev = (GdkEventKey *) event;
637 get_pointer_xy(pos, pos + 1);
638 time = kev->time;
640 else
641 get_pointer_xy(pos, pos + 1);
643 pos[2] = item;
645 gtk_widget_show_all(menu);
646 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
647 position_menu, (gpointer) pos, button, time);
648 select_nth_item(GTK_MENU_SHELL(menu), item);
651 static MenuIconStyle get_menu_icon_style(void)
653 MenuIconStyle mis;
654 int display;
656 mis = option_get_int("menu_iconsize");
658 switch (mis)
660 case MIS_NONE: case MIS_SMALL: case MIS_LARGE: case MIS_HUGE:
661 return mis;
662 default:
663 break;
666 if (mis == MIS_CURRENT && window_with_focus)
668 switch (window_with_focus->display_style)
670 case HUGE_ICONS:
671 return MIS_HUGE;
672 case LARGE_ICONS:
673 return MIS_LARGE;
674 case SMALL_ICONS:
675 return MIS_SMALL;
676 default:
677 break;
681 display = option_get_int("display_size");
682 switch (display)
684 case HUGE_ICONS:
685 return MIS_HUGE;
686 case LARGE_ICONS:
687 return MIS_LARGE;
688 case SMALL_ICONS:
689 return MIS_SMALL;
690 default:
691 break;
694 return MIS_SMALL;
697 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
699 DirItem *file_item = NULL;
700 GdkModifierType state = 0;
702 updating_menu++;
704 /* Remove previous AppMenu, if any */
705 appmenu_remove();
707 window_with_focus = filer_window;
709 if (event->type == GDK_BUTTON_PRESS)
710 state = ((GdkEventButton *) event)->state;
711 else if (event->type == GDK_KEY_PRESS)
712 state = ((GdkEventKey *) event)->state;
714 if (filer_window->collection->number_selected == 0 && item >= 0)
716 collection_select_item(filer_window->collection, item);
717 filer_window->temp_item_selected = TRUE;
719 else
721 filer_window->temp_item_selected = FALSE;
724 /* Short-cut to the Send To menu */
725 if (state & GDK_SHIFT_MASK)
727 GList *paths;
729 if (filer_window->collection->number_selected == 0)
731 report_error(PROJECT,
732 _("You should Shift+Menu click over a file to "
733 "send it somewhere"));
734 return;
737 paths = filer_selected_items(filer_window);
739 updating_menu--;
741 show_send_to_menu(paths, event); /* (paths eaten) */
743 return;
747 GtkWidget *file_label, *file_menu;
748 Collection *collection = filer_window->collection;
749 GString *buffer;
751 file_label = filer_file_item;
752 file_menu = filer_file_menu;
753 gtk_check_menu_item_set_active(
754 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
755 filer_window->show_hidden);
756 buffer = g_string_new(NULL);
757 switch (collection->number_selected)
759 case 0:
760 g_string_assign(buffer, _("Next Click"));
761 items_sensitive(TRUE);
762 break;
763 case 1:
764 items_sensitive(TRUE);
765 file_item = selected_item(
766 filer_window->collection);
767 g_string_sprintf(buffer, "%s '%s'",
768 basetype_name(file_item),
769 file_item->leafname);
770 if (!can_set_run_action(file_item))
771 menu_set_items_shaded(filer_file_menu,
772 TRUE, 6, 1);
773 break;
774 default:
775 items_sensitive(FALSE);
776 g_string_sprintf(buffer, _("%d items"),
777 collection->number_selected);
778 break;
780 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
781 g_string_free(buffer, TRUE);
783 menu_show_shift_action(file_shift_item, file_item,
784 collection->number_selected == 0);
785 if (file_item)
786 appmenu_add(make_path(filer_window->path,
787 file_item->leafname)->str,
788 file_item, filer_file_menu);
791 update_new_files_menu(get_menu_icon_style());
793 gtk_widget_set_sensitive(filer_new_window, !o_unique_filer_windows);
795 popup_menu = (state & GDK_CONTROL_MASK)
796 ? filer_file_menu
797 : filer_menu;
799 updating_menu--;
801 show_popup_menu(popup_menu, event,
802 popup_menu == filer_file_menu ? 5 : 1);
805 static void menu_closed(GtkWidget *widget)
807 if (window_with_focus == NULL || widget != popup_menu)
808 return; /* Close panel item chosen? */
810 popup_menu = NULL;
812 if (window_with_focus->temp_item_selected)
814 collection_clear_selection(window_with_focus->collection);
815 window_with_focus->temp_item_selected = FALSE;
819 void target_callback(FilerWindow *filer_window,
820 gint item,
821 gpointer real_fn)
823 Collection *collection = filer_window->collection;
825 g_return_if_fail(window_with_focus != NULL);
826 g_return_if_fail(window_with_focus == filer_window);
827 g_return_if_fail(real_fn != NULL);
829 collection_wink_item(collection, item);
830 collection_clear_selection(collection);
831 collection_select_item(collection, item);
832 ((GtkItemFactoryCallback1) real_fn)(NULL, 0, GTK_WIDGET(collection));
833 if (item < collection->number_of_items)
834 collection_unselect_item(collection, item);
837 /* Set the text of the 'Shift Open...' menu item.
838 * If icon is NULL, reset the text and also shade it, unless 'next'.
840 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
842 guchar *shift_action = NULL;
844 if (item)
846 if (item->flags & ITEM_FLAG_MOUNT_POINT)
848 if (item->flags & ITEM_FLAG_MOUNTED)
849 shift_action = N_("Unmount");
850 else
851 shift_action = N_("Mount");
853 else if (item->flags & ITEM_FLAG_SYMLINK)
854 shift_action = N_("Show Target");
855 else if (item->base_type == TYPE_DIRECTORY)
856 shift_action = N_("Look Inside");
857 else if (item->base_type == TYPE_FILE)
858 shift_action = N_("Open As Text");
860 gtk_label_set_text(GTK_LABEL(menu_item),
861 shift_action ? _(shift_action)
862 : _("Shift Open"));
863 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
866 /* Actions */
868 static void huge_with(gpointer data, guint action, GtkWidget *widget)
870 display_set_layout(window_with_focus, HUGE_ICONS, action);
873 static void large_with(gpointer data, guint action, GtkWidget *widget)
875 display_set_layout(window_with_focus, LARGE_ICONS, action);
878 static void small_with(gpointer data, guint action, GtkWidget *widget)
880 display_set_layout(window_with_focus, SMALL_ICONS, action);
883 static void sort_name(gpointer data, guint action, GtkWidget *widget)
885 g_return_if_fail(window_with_focus != NULL);
887 display_set_sort_fn(window_with_focus, sort_by_name);
890 static void sort_type(gpointer data, guint action, GtkWidget *widget)
892 g_return_if_fail(window_with_focus != NULL);
894 display_set_sort_fn(window_with_focus, sort_by_type);
897 static void sort_date(gpointer data, guint action, GtkWidget *widget)
899 g_return_if_fail(window_with_focus != NULL);
901 display_set_sort_fn(window_with_focus, sort_by_date);
904 static void sort_size(gpointer data, guint action, GtkWidget *widget)
906 g_return_if_fail(window_with_focus != NULL);
908 display_set_sort_fn(window_with_focus, sort_by_size);
911 static void hidden(gpointer data, guint action, GtkWidget *widget)
913 if (updating_menu)
914 return;
916 g_return_if_fail(window_with_focus != NULL);
918 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
921 static void refresh(gpointer data, guint action, GtkWidget *widget)
923 g_return_if_fail(window_with_focus != NULL);
925 full_refresh();
926 filer_update_dir(window_with_focus, TRUE);
929 static void create_thumbs(gpointer data, guint action, GtkWidget *widget)
931 g_return_if_fail(window_with_focus != NULL);
933 dir_rescan_with_thumbs(window_with_focus->directory,
934 window_with_focus->path);
937 static void delete(gpointer data, guint action, GtkWidget *widget)
939 g_return_if_fail(window_with_focus != NULL);
941 if (window_with_focus->collection->number_selected == 0)
942 filer_target_mode(window_with_focus,
943 target_callback, delete,
944 _("DELETE ... ?"));
945 else
946 action_delete(window_with_focus);
949 static void usage(gpointer data, guint action, GtkWidget *widget)
951 g_return_if_fail(window_with_focus != NULL);
953 if (window_with_focus->collection->number_selected == 0)
954 filer_target_mode(window_with_focus,
955 target_callback, usage,
956 _("Count the size of ... ?"));
957 else
958 action_usage(window_with_focus);
961 static void chmod_items(gpointer data, guint action, GtkWidget *widget)
963 g_return_if_fail(window_with_focus != NULL);
965 if (window_with_focus->collection->number_selected == 0)
966 filer_target_mode(window_with_focus,
967 target_callback, chmod_items,
968 _("Set permissions on ... ?"));
969 else
970 action_chmod(window_with_focus);
973 static void find(gpointer data, guint action, GtkWidget *widget)
975 g_return_if_fail(window_with_focus != NULL);
977 if (window_with_focus->collection->number_selected == 0)
978 filer_target_mode(window_with_focus,
979 target_callback, find,
980 _("Search inside ... ?"));
981 else
982 action_find(window_with_focus);
985 /* This pops up our savebox widget, cancelling any currently open one,
986 * and allows the user to pick a new path for it.
987 * Once the new path has been picked, the callback will be called with
988 * both the current and new paths.
989 * NOTE: This function unrefs 'image'!
991 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
992 gboolean (*callback)(guchar *current, guchar *new))
994 if (GTK_WIDGET_VISIBLE(savebox))
995 gtk_widget_hide(savebox);
997 if (callback == link_cb)
998 gtk_widget_show(check_relative);
999 else
1000 gtk_widget_hide(check_relative);
1002 if (current_path)
1003 g_free(current_path);
1004 current_path = g_strdup(path);
1005 current_savebox_callback = callback;
1007 gtk_window_set_title(GTK_WINDOW(savebox), title);
1008 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), current_path);
1009 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixmap, image->mask);
1010 pixmap_unref(image);
1012 gtk_widget_grab_focus(GTK_SAVEBOX(savebox)->entry);
1013 gtk_widget_show(savebox);
1016 static gint save_to_file(GtkSavebox *savebox, guchar *pathname)
1018 g_return_val_if_fail(current_savebox_callback != NULL,
1019 GTK_XDS_SAVE_ERROR);
1021 return current_savebox_callback(current_path, pathname)
1022 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
1025 static gboolean copy_cb(guchar *current, guchar *new)
1027 return action_with_leaf(action_copy, current, new);
1030 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new)
1032 char *new_dir, *leaf;
1033 GList *local_paths;
1035 if (new[0] != '/')
1037 report_error(PROJECT, _("New pathname is not absolute"));
1038 return FALSE;
1041 if (new[strlen(new) - 1] == '/')
1043 new_dir = g_strdup(new);
1044 leaf = NULL;
1046 else
1048 guchar *slash;
1050 slash = strrchr(new, '/');
1051 new_dir = g_strndup(new, slash - new);
1052 leaf = slash + 1;
1055 local_paths = g_list_append(NULL, current);
1056 action(local_paths, new_dir, leaf);
1057 g_list_free(local_paths);
1059 g_free(new_dir);
1061 return TRUE;
1064 #define SHOW_SAVEBOX(title, callback) \
1066 DirItem *item; \
1067 guchar *path; \
1068 item = selected_item(collection); \
1069 path = make_path(window_with_focus->path, item->leafname)->str; \
1070 pixmap_ref(item->image); \
1071 savebox_show(title, path, item->image, callback); \
1074 static void copy_item(gpointer data, guint action, GtkWidget *widget)
1076 Collection *collection;
1078 g_return_if_fail(window_with_focus != NULL);
1080 collection = window_with_focus->collection;
1081 if (collection->number_selected > 1)
1083 report_error(PROJECT, _("You cannot do this to more than "
1084 "one item at a time"));
1085 return;
1087 else if (collection->number_selected != 1)
1088 filer_target_mode(window_with_focus,
1089 target_callback, copy_item,
1090 _("Copy ... ?"));
1091 else
1092 SHOW_SAVEBOX(_("Copy"), copy_cb);
1095 static gboolean rename_cb(guchar *current, guchar *new)
1097 return action_with_leaf(action_move, current, new);
1100 static void rename_item(gpointer data, guint action, GtkWidget *widget)
1102 Collection *collection;
1104 g_return_if_fail(window_with_focus != NULL);
1106 collection = window_with_focus->collection;
1107 if (collection->number_selected > 1)
1109 report_error(PROJECT, _("You cannot do this to more than "
1110 "one item at a time"));
1111 return;
1113 else if (collection->number_selected != 1)
1114 filer_target_mode(window_with_focus,
1115 target_callback, rename_item,
1116 _("Rename ... ?"));
1117 else
1118 SHOW_SAVEBOX(_("Rename"), rename_cb);
1121 static gboolean link_cb(guchar *initial, guchar *path)
1123 int err;
1125 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_relative)))
1127 guchar *rpath;
1129 rpath = get_relative_path(path, initial);
1130 err = symlink(rpath, path);
1132 g_free(rpath);
1134 else
1135 err = symlink(initial, path);
1137 if (err)
1139 report_error("ROX-Filer: symlink()", g_strerror(errno));
1140 return FALSE;
1142 return TRUE;
1145 static void link_item(gpointer data, guint action, GtkWidget *widget)
1147 Collection *collection;
1149 g_return_if_fail(window_with_focus != NULL);
1151 collection = window_with_focus->collection;
1152 if (collection->number_selected > 1)
1154 report_error(PROJECT, _("You cannot do this to more than "
1155 "one item at a time"));
1156 return;
1158 else if (collection->number_selected != 1)
1159 filer_target_mode(window_with_focus,
1160 target_callback, link_item,
1161 _("Symlink ... ?"));
1162 else
1163 SHOW_SAVEBOX(_("Symlink"), link_cb);
1166 static void open_file(gpointer data, guint action, GtkWidget *widget)
1168 Collection *collection;
1170 g_return_if_fail(window_with_focus != NULL);
1172 collection = window_with_focus->collection;
1173 if (collection->number_selected > 1)
1175 report_error(PROJECT, _("You cannot do this to more than "
1176 "one item at a time"));
1177 return;
1179 else if (collection->number_selected != 1)
1180 filer_target_mode(window_with_focus,
1181 target_callback, open_file,
1182 _("Shift Open ... ?"));
1183 else
1184 filer_openitem(window_with_focus,
1185 selected_item_number(collection),
1186 OPEN_SAME_WINDOW | OPEN_SHIFT);
1189 static void run_action(gpointer data, guint action, GtkWidget *widget)
1191 Collection *collection;
1193 g_return_if_fail(window_with_focus != NULL);
1195 collection = window_with_focus->collection;
1196 if (collection->number_selected > 1)
1198 report_error(PROJECT, _("You cannot do this to more than "
1199 "one item at a time"));
1200 return;
1202 else if (collection->number_selected != 1)
1203 filer_target_mode(window_with_focus,
1204 target_callback, run_action,
1205 _("Set run action for ... ?"));
1206 else
1208 DirItem *item;
1210 item = selected_item(collection);
1211 g_return_if_fail(item != NULL);
1213 if (can_set_run_action(item))
1214 type_set_handler_dialog(item->mime_type);
1215 else
1216 report_error(PROJECT,
1217 _("You can only set the run action for a "
1218 "regular file"));
1222 static void set_icon(gpointer data, guint action, GtkWidget *widget)
1224 Collection *collection;
1226 g_return_if_fail(window_with_focus != NULL);
1228 collection = window_with_focus->collection;
1229 if (collection->number_selected > 1)
1231 report_error(PROJECT, _("You cannot do this to more than "
1232 "one item at a time"));
1233 return;
1235 else if (collection->number_selected != 1)
1236 filer_target_mode(window_with_focus,
1237 target_callback, set_icon,
1238 _("Set icon for ... ?"));
1239 else
1241 DirItem *item;
1242 guchar *path;
1244 item = selected_item(collection);
1245 g_return_if_fail(item != NULL);
1247 path = make_path(window_with_focus->path, item->leafname)->str;
1249 icon_set_handler_dialog(item, path);
1253 static void show_file_info(gpointer unused, guint action, GtkWidget *widget)
1255 DirItem *file;
1256 Collection *collection;
1258 g_return_if_fail(window_with_focus != NULL);
1260 collection = window_with_focus->collection;
1261 if (collection->number_selected > 1)
1263 report_error(PROJECT, _("You cannot do this to more than "
1264 "one item at a time"));
1265 return;
1267 else if (collection->number_selected != 1)
1269 filer_target_mode(window_with_focus,
1270 target_callback, show_file_info,
1271 _("Examine ... ?"));
1272 return;
1275 file = selected_item(collection);
1276 infobox_new(make_path(window_with_focus->path, file->leafname)->str);
1279 void open_home(gpointer data, guint action, GtkWidget *widget)
1281 filer_opendir(home_dir);
1284 static void help(gpointer data, guint action, GtkWidget *widget)
1286 Collection *collection;
1287 DirItem *item;
1289 g_return_if_fail(window_with_focus != NULL);
1291 collection = window_with_focus->collection;
1292 if (collection->number_selected > 1)
1294 report_error(PROJECT, _("You cannot do this to more than "
1295 "one item at a time"));
1296 return;
1298 else if (collection->number_selected != 1)
1300 filer_target_mode(window_with_focus, target_callback, help,
1301 _("Help about ... ?"));
1302 return;
1304 item = selected_item(collection);
1306 show_item_help(make_path(window_with_focus->path, item->leafname)->str,
1307 item);
1310 #ifdef HAVE_LIBVFS
1311 #define OPEN_VFS(fs) \
1312 static void open_vfs_ ## fs (gpointer data, guint action, GtkWidget *widget) \
1314 Collection *collection; \
1316 g_return_if_fail(window_with_focus != NULL); \
1318 collection = window_with_focus->collection; \
1319 if (collection->number_selected < 1) \
1320 filer_target_mode(window_with_focus, target_callback, \
1321 open_vfs_ ## fs, \
1322 _("Look inside ... ?")); \
1323 else \
1324 real_vfs_open(#fs); \
1327 static void real_vfs_open(char *fs)
1329 gchar *path;
1330 DirItem *item;
1332 if (window_with_focus->collection->number_selected != 1)
1334 report_error(PROJECT, _("You must select a single file "
1335 "to open as a Virtual File System"));
1336 return;
1339 item = selected_item(window_with_focus->collection);
1341 path = g_strconcat(window_with_focus->path,
1342 "/",
1343 item->leafname,
1344 "#", fs, NULL);
1346 filer_change_to(window_with_focus, path, NULL);
1347 g_free(path);
1350 OPEN_VFS(rpm)
1351 OPEN_VFS(utar)
1352 OPEN_VFS(uzip)
1353 OPEN_VFS(deb)
1354 #else
1355 static void open_vfs_avfs(gpointer data, guint action, GtkWidget *widget)
1357 gchar *path;
1358 DirItem *item;
1359 Collection *collection;
1361 g_return_if_fail(window_with_focus != NULL);
1363 collection = window_with_focus->collection;
1364 if (collection->number_selected < 1)
1366 filer_target_mode(window_with_focus, target_callback,
1367 open_vfs_avfs,
1368 _("Look inside ... ?"));
1369 return;
1371 else if (collection->number_selected != 1)
1373 report_error(PROJECT, _("You must select a single file "
1374 "to open as a Virtual File System"));
1375 return;
1378 item = selected_item(collection);
1380 path = g_strconcat(window_with_focus->path,
1381 "/", item->leafname, "#", NULL);
1383 filer_change_to(window_with_focus, path, NULL);
1384 g_free(path);
1386 #endif
1388 static void select_all(gpointer data, guint action, GtkWidget *widget)
1390 g_return_if_fail(window_with_focus != NULL);
1392 collection_select_all(window_with_focus->collection);
1393 window_with_focus->temp_item_selected = FALSE;
1396 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1398 g_return_if_fail(window_with_focus != NULL);
1400 collection_clear_selection(window_with_focus->collection);
1401 window_with_focus->temp_item_selected = FALSE;
1404 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1406 options_show();
1409 static gboolean new_directory_cb(guchar *initial, guchar *path)
1411 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1413 report_error("mkdir", g_strerror(errno));
1414 return FALSE;
1417 dir_check_this(path);
1419 if (filer_exists(window_with_focus))
1421 guchar *leaf;
1422 leaf = strrchr(path, '/');
1423 if (leaf)
1424 display_set_autoselect(window_with_focus, leaf + 1);
1427 return TRUE;
1430 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1432 g_return_if_fail(window_with_focus != NULL);
1434 savebox_show(_("New Directory"),
1435 make_path(window_with_focus->path, _("NewDir"))->str,
1436 type_to_icon(&special_directory),
1437 new_directory_cb);
1440 static gboolean new_file_cb(guchar *initial, guchar *path)
1442 int fd;
1444 fd = open(path, O_CREAT | O_EXCL, 0666);
1446 if (fd == -1)
1448 report_error(_("Error creating file"), g_strerror(errno));
1449 return FALSE;
1452 if (close(fd))
1453 report_error(_("Error creating file"), g_strerror(errno));
1455 dir_check_this(path);
1457 if (filer_exists(window_with_focus))
1459 guchar *leaf;
1460 leaf = strrchr(path, '/');
1461 if (leaf)
1462 display_set_autoselect(window_with_focus, leaf + 1);
1465 return TRUE;
1468 static void new_file(gpointer data, guint action, GtkWidget *widget)
1470 g_return_if_fail(window_with_focus != NULL);
1472 savebox_show(_("New File"),
1473 make_path(window_with_focus->path, _("NewFile"))->str,
1474 type_to_icon(&text_plain),
1475 new_file_cb);
1478 static gboolean new_file_type_cb(guchar *initial, guchar *path)
1480 gchar *templ, *templ_dname, *oleaf, *dest, *leaf;
1481 GList *paths;
1483 /* We can work out the template path from the initial name */
1484 oleaf = g_basename(initial);
1485 templ_dname = choices_find_path_load("Templates", "");
1486 if (!templ_dname)
1488 char *mess;
1490 mess = g_strconcat(_("Could not find the template for "),
1491 oleaf, NULL);
1492 report_error(_("Error creating file"), mess);
1493 g_free(mess);
1495 return FALSE;
1498 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1499 g_free(templ_dname);
1501 dest = g_dirname(path);
1502 leaf = g_basename(path);
1503 paths = g_list_append(NULL, templ);
1505 action_copy(paths, dest, leaf);
1507 g_list_free(paths);
1508 g_free(dest);
1509 g_free(templ);
1511 if (filer_exists(window_with_focus))
1512 display_set_autoselect(window_with_focus, leaf);
1514 return TRUE;
1517 static void do_send_to(gchar *templ)
1519 g_return_if_fail(send_to_paths != NULL);
1521 run_with_files(templ, send_to_paths);
1524 static void new_file_type(gchar *templ)
1526 gchar *leaf;
1527 MIME_type *type;
1529 g_return_if_fail(window_with_focus != NULL);
1531 leaf = g_basename(templ);
1532 type = type_get_type(templ);
1534 savebox_show(_("New File"),
1535 make_path(window_with_focus->path, leaf)->str,
1536 type_to_icon(type),
1537 new_file_type_cb);
1540 static void customise_send_to(gpointer data)
1542 GPtrArray *path;
1543 guchar *msg, *save;
1544 GString *dirs;
1545 int i;
1547 dirs = g_string_new(NULL);
1549 path = choices_list_dirs("");
1550 for (i = 0; i < path->len; i++)
1552 guchar *old = (guchar *) path->pdata[i];
1554 g_string_append(dirs, old);
1555 g_string_append(dirs, "SendTo\n");
1557 choices_free_list(path);
1559 save = choices_find_path_save("", "SendTo", TRUE);
1560 if (save)
1561 mkdir(save, 0777);
1563 msg = g_strdup_printf(
1564 _("The `Send To' menu provides a quick way to send some files "
1565 "to an application. The applications listed are those in "
1566 "the following directories:\n\n%s\n%s\n"
1567 "The `Send To' menu may be opened by Shift+Menu clicking "
1568 "over a file."),
1569 dirs->str,
1570 save ? _("I'll show you your SendTo directory now; you should "
1571 "symlink (Ctrl+Shift drag) any applications you want "
1572 "into it.")
1573 : _("Your CHOICESPATH variable setting prevents "
1574 "customisations - sorry."));
1576 g_string_free(dirs, TRUE);
1578 report_error(PROJECT, msg);
1580 if (save)
1581 filer_opendir(save);
1583 g_free(msg);
1586 /* Scan the SendTo dir and create and show the Send To menu.
1587 * The 'paths' list and every path in it is claimed, and will be
1588 * freed later -- don't free it yourself!
1590 static void show_send_to_menu(GList *paths, GdkEvent *event)
1592 GtkWidget *menu, *item;
1593 GPtrArray *path;
1594 int i;
1596 menu = gtk_menu_new();
1598 path = choices_list_dirs("SendTo");
1600 for (i = 0; i < path->len; i++)
1602 GList *widgets = NULL;
1603 guchar *dir = (guchar *) path->pdata[i];
1605 widgets = menu_from_dir(menu, dir, get_menu_icon_style(),
1606 (CallbackFn) do_send_to,
1607 FALSE, FALSE);
1609 if (widgets)
1610 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1611 gtk_menu_item_new());
1613 g_list_free(widgets); /* TODO: Get rid of this */
1616 choices_free_list(path);
1618 item = gtk_menu_item_new_with_label(_("Customise"));
1619 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
1620 GTK_SIGNAL_FUNC(customise_send_to), NULL);
1621 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1623 if (send_to_paths)
1625 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1626 g_list_free(send_to_paths);
1628 send_to_paths = paths;
1630 gtk_signal_connect(GTK_OBJECT(menu), "unmap_event",
1631 GTK_SIGNAL_FUNC(menu_closed), NULL);
1633 popup_menu = menu;
1634 show_popup_menu(menu, event, 0);
1637 static void send_to(gpointer data, guint action, GtkWidget *widget)
1639 Collection *collection;
1641 g_return_if_fail(window_with_focus != NULL);
1643 collection = window_with_focus->collection;
1645 if (collection->number_selected < 1)
1646 filer_target_mode(window_with_focus, target_callback,
1647 send_to, _("Send ... to ... ?"));
1648 else
1650 GList *paths;
1651 GdkEvent *event;
1653 paths = filer_selected_items(window_with_focus);
1654 event = gtk_get_current_event();
1656 /* Eats paths */
1657 show_send_to_menu(paths, event);
1659 gdk_event_free(event);
1663 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1665 char *argv[] = {"sh", "-c", NULL, NULL};
1667 argv[2] = option_get_static_string("menu_xterm");
1669 g_return_if_fail(window_with_focus != NULL);
1671 if (!spawn_full(argv, window_with_focus->path))
1672 report_error(PROJECT, _("Failed to fork() child process"));
1675 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1677 g_return_if_fail(window_with_focus != NULL);
1679 filer_change_to(window_with_focus, home_dir, NULL);
1682 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1684 g_return_if_fail(window_with_focus != NULL);
1686 filer_open_parent(window_with_focus);
1689 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1691 g_return_if_fail(window_with_focus != NULL);
1693 change_to_parent(window_with_focus);
1696 static void resize(gpointer data, guint action, GtkWidget *widget)
1698 g_return_if_fail(window_with_focus != NULL);
1700 filer_window_autosize(window_with_focus, TRUE);
1703 static void new_window(gpointer data, guint action, GtkWidget *widget)
1705 g_return_if_fail(window_with_focus != NULL);
1707 if (o_unique_filer_windows)
1709 report_error(PROJECT, _("You can't open a second view onto "
1710 "this directory because the `Unique Windows' option "
1711 "is turned on in the Options window."));
1713 else
1714 filer_opendir(window_with_focus->path);
1717 #if 0
1718 static void su_to_user(GtkWidget *dialog)
1720 char *argv[] = {
1721 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1722 GtkEntry *user;
1723 guchar *path;
1725 g_return_if_fail(dialog != NULL);
1727 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1728 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1730 g_return_if_fail(user != NULL && path != NULL);
1732 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1733 argv[3] = gtk_entry_get_text(user);
1734 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1735 argv[5] = path;
1737 if (!spawn(argv))
1738 report_error(_("fork() failed"), g_strerror(errno));
1740 g_free(argv[2]);
1741 g_free(argv[4]);
1743 gtk_widget_destroy(dialog);
1746 static void new_user(gpointer data, guint action, GtkWidget *widget)
1748 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1750 g_return_if_fail(window_with_focus != NULL);
1752 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1753 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1754 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1755 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1756 g_strdup(window_with_focus->path), g_free);
1758 vbox = gtk_vbox_new(FALSE, 4);
1759 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1760 gtk_box_pack_start(GTK_BOX(vbox),
1761 gtk_label_new(_("Browse as which user?")),
1762 TRUE, TRUE, 2);
1764 hbox = gtk_hbox_new(FALSE, 4);
1765 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1767 gtk_box_pack_start(GTK_BOX(hbox),
1768 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1770 entry = gtk_entry_new();
1771 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1772 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1773 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1774 gtk_widget_grab_focus(entry);
1775 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1776 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1777 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1779 hbox = gtk_hbox_new(TRUE, 0);
1780 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1782 button = gtk_button_new_with_label(_("OK"));
1783 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1784 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1785 gtk_window_set_default(GTK_WINDOW(dialog), button);
1786 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1787 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1789 button = gtk_button_new_with_label(_("Cancel"));
1790 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1791 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1792 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1793 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1794 GTK_OBJECT(dialog));
1796 gtk_widget_show_all(dialog);
1798 #endif
1800 static void close_window(gpointer data, guint action, GtkWidget *widget)
1802 g_return_if_fail(window_with_focus != NULL);
1804 gtk_widget_destroy(window_with_focus->window);
1807 static void enter_path(gpointer data, guint action, GtkWidget *widget)
1809 g_return_if_fail(window_with_focus != NULL);
1811 minibuffer_show(window_with_focus, MINI_PATH);
1814 static void shell_command(gpointer data, guint action, GtkWidget *widget)
1816 g_return_if_fail(window_with_focus != NULL);
1818 minibuffer_show(window_with_focus, MINI_SHELL);
1821 static void select_if(gpointer data, guint action, GtkWidget *widget)
1823 g_return_if_fail(window_with_focus != NULL);
1825 minibuffer_show(window_with_focus, MINI_SELECT_IF);
1828 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1830 filer_opendir(make_path(app_dir, "Help")->str);
1833 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1834 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1836 GList *items, *item;
1838 items = gtk_container_children(GTK_CONTAINER(menu));
1840 item = g_list_nth(items, from);
1841 while (item && n--)
1843 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1844 item = item->next;
1846 g_list_free(items);
1849 /* This is called for every modified menu entry. We just use it to
1850 * find out if the menu has changed at all.
1852 static void set_mod(gboolean *mod, guchar *str)
1854 if (str && str[0] == '(')
1855 *mod = TRUE;
1858 static void save_menus(void)
1860 char *menurc;
1862 menurc = choices_find_path_save("menus", PROJECT, FALSE);
1863 if (menurc)
1865 gboolean mod = FALSE;
1867 g_free(menurc);
1869 /* Find out if anything changed... */
1870 gtk_item_factory_dump_items(NULL, TRUE,
1871 (GtkPrintFunc) set_mod, &mod);
1873 /* Dump out if so... */
1874 if (mod)
1876 menurc = choices_find_path_save("menus", PROJECT, TRUE);
1877 g_return_if_fail(menurc != NULL);
1878 mark_menus_modified(TRUE);
1879 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
1880 mark_menus_modified(FALSE);
1881 g_free(menurc);
1886 static void mark_modified(gpointer hash_key,
1887 gpointer value,
1888 gpointer user_data)
1890 GtkItemFactoryItem *item = (GtkItemFactoryItem *) value;
1892 item->modified = (gboolean) GPOINTER_TO_INT(user_data);
1895 /* Set or clear the 'modified' flag in all menu items. Messy... */
1896 static void mark_menus_modified(gboolean mod)
1898 GtkItemFactoryClass *class;
1900 class = gtk_type_class(GTK_TYPE_ITEM_FACTORY);
1902 g_hash_table_foreach(class->item_ht, mark_modified,
1903 GINT_TO_POINTER(mod));
1906 static void select_nth_item(GtkMenuShell *shell, int n)
1908 GList *items, *nth;
1909 GtkWidget *item = NULL;
1911 items = gtk_container_children(GTK_CONTAINER(shell));
1912 nth = g_list_nth(items, n);
1914 g_return_if_fail(nth != NULL);
1916 item = (GtkWidget *) (nth->data);
1917 g_list_free(items);
1919 gtk_menu_shell_select_item(shell, item);