r794: Send To menu lists contents of all Send To directories in Choices, not just
[rox-filer.git] / ROX-Filer / src / menu.c
blob15a5ec6bfc65de1ca579c743e01601b59f9f6960
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 void (*ActionFn)(GList *paths, char *dest_dir, char *leaf);
62 typedef void MenuCallback(GtkWidget *widget, gpointer data);
64 GtkAccelGroup *filer_keys;
65 GtkAccelGroup *pinboard_keys;
67 GtkWidget *popup_menu = NULL; /* Currently open menu */
69 static gint updating_menu = 0; /* Non-zero => ignore activations */
70 static GList *send_to_paths = NULL;
72 /* Static prototypes */
74 static void save_menus(void);
75 static void menu_closed(GtkWidget *widget);
76 static void items_sensitive(gboolean state);
77 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
78 gboolean (*callback)(guchar *current, guchar *new));
79 static gint save_to_file(GtkSavebox *savebox, guchar *pathname);
80 static void mark_menus_modified(gboolean mod);
81 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new);
82 static gboolean link_cb(guchar *initial, guchar *path);
83 static void select_nth_item(GtkMenuShell *shell, int n);
84 static void new_file_type(gchar *templ);
85 static void do_send_to(gchar *templ);
86 static void show_send_to_menu(GList *paths, GdkEvent *event);
88 /* Note that for most of these callbacks none of the arguments are used. */
90 /* (action used in these two - DetailsType) */
91 static void huge_with(gpointer data, guint action, GtkWidget *widget);
92 static void large_with(gpointer data, guint action, GtkWidget *widget);
93 static void small_with(gpointer data, guint action, GtkWidget *widget);
95 static void sort_name(gpointer data, guint action, GtkWidget *widget);
96 static void sort_type(gpointer data, guint action, GtkWidget *widget);
97 static void sort_size(gpointer data, guint action, GtkWidget *widget);
98 static void sort_date(gpointer data, guint action, GtkWidget *widget);
100 static void hidden(gpointer data, guint action, GtkWidget *widget);
101 static void refresh(gpointer data, guint action, GtkWidget *widget);
102 static void create_thumbs(gpointer data, guint action, GtkWidget *widget);
104 static void copy_item(gpointer data, guint action, GtkWidget *widget);
105 static void rename_item(gpointer data, guint action, GtkWidget *widget);
106 static void link_item(gpointer data, guint action, GtkWidget *widget);
107 static void open_file(gpointer data, guint action, GtkWidget *widget);
108 static void help(gpointer data, guint action, GtkWidget *widget);
109 static void show_file_info(gpointer data, guint action, GtkWidget *widget);
110 static void send_to(gpointer data, guint action, GtkWidget *widget);
111 static void delete(gpointer data, guint action, GtkWidget *widget);
112 static void usage(gpointer data, guint action, GtkWidget *widget);
113 static void chmod_items(gpointer data, guint action, GtkWidget *widget);
114 static void find(gpointer data, guint action, GtkWidget *widget);
116 #ifdef HAVE_LIBVFS
117 static void open_vfs_rpm(gpointer data, guint action, GtkWidget *widget);
118 static void open_vfs_utar(gpointer data, guint action, GtkWidget *widget);
119 static void open_vfs_uzip(gpointer data, guint action, GtkWidget *widget);
120 static void open_vfs_deb(gpointer data, guint action, GtkWidget *widget);
121 #else
122 static void open_vfs_avfs(gpointer data, guint action, GtkWidget *widget);
123 #endif
125 static void select_all(gpointer data, guint action, GtkWidget *widget);
126 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
127 static void new_directory(gpointer data, guint action, GtkWidget *widget);
128 static void new_file(gpointer data, guint action, GtkWidget *widget);
129 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
131 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
132 static void open_parent(gpointer data, guint action, GtkWidget *widget);
133 static void home_directory(gpointer data, guint action, GtkWidget *widget);
134 static void new_window(gpointer data, guint action, GtkWidget *widget);
135 /* static void new_user(gpointer data, guint action, GtkWidget *widget); */
136 static void close_window(gpointer data, guint action, GtkWidget *widget);
137 static void enter_path(gpointer data, guint action, GtkWidget *widget);
138 static void shell_command(gpointer data, guint action, GtkWidget *widget);
139 static void run_action(gpointer data, guint action, GtkWidget *widget);
140 static void set_icon(gpointer data, guint action, GtkWidget *widget);
141 static void select_if(gpointer data, guint action, GtkWidget *widget);
142 static void resize(gpointer data, guint action, GtkWidget *widget);
145 static GtkWidget *filer_menu; /* The popup filer menu */
146 static GtkWidget *filer_file_item; /* The File '' label */
147 static GtkWidget *filer_file_menu; /* The File '' menu */
148 static GtkWidget *file_shift_item; /* Shift Open label */
149 GtkWidget *display_large_menu; /* Display->Large With... */
150 GtkWidget *display_small_menu; /* Display->Small With... */
151 #ifdef HAVE_LIBVFS
152 static GtkWidget *filer_vfs_menu; /* The Open VFS menu */
153 #endif
154 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
155 static GtkWidget *filer_new_window; /* The New Window item */
156 static GtkWidget *filer_new_menu; /* The New submenu */
158 /* Used for Copy, etc */
159 static GtkWidget *savebox = NULL;
160 static GtkWidget *check_relative = NULL;
161 static guchar *current_path = NULL;
162 static gboolean (*current_savebox_callback)(guchar *current, guchar *new);
164 #undef N_
165 #define N_(x) x
167 static GtkItemFactoryEntry filer_menu_def[] = {
168 {N_("Display"), NULL, NULL, 0, "<Branch>"},
169 {">" N_("Huge Icons"), NULL, huge_with, DETAILS_NONE, NULL},
170 {">" N_("Large Icons"), NULL, large_with, DETAILS_NONE, NULL},
171 {">" N_("Small Icons"), NULL, small_with, DETAILS_NONE, NULL},
172 {">" N_("Huge, With..."), NULL, NULL, 0, "<Branch>"},
173 {">>" N_("Summary"), NULL, huge_with, DETAILS_SUMMARY, NULL},
174 {">>" N_("Sizes"), NULL, huge_with, DETAILS_SIZE, NULL},
175 {">>" N_("Permissions"), NULL, huge_with, DETAILS_PERMISSIONS, NULL},
176 {">>" N_("Type"), NULL, huge_with, DETAILS_TYPE, NULL},
177 {">>" N_("Times"), NULL, huge_with, DETAILS_TIMES, NULL},
178 {">" N_("Large, With..."), NULL, NULL, 0, "<Branch>"},
179 {">>" N_("Summary"), NULL, large_with, DETAILS_SUMMARY, NULL},
180 {">>" N_("Sizes"), NULL, large_with, DETAILS_SIZE, NULL},
181 {">>" N_("Permissions"), NULL, large_with, DETAILS_PERMISSIONS, NULL},
182 {">>" N_("Type"), NULL, large_with, DETAILS_TYPE, NULL},
183 {">>" N_("Times"), NULL, large_with, DETAILS_TIMES, NULL},
184 {">" N_("Small, With..."), NULL, NULL, 0, "<Branch>"},
185 {">>" N_("Summary"), NULL, small_with, DETAILS_SUMMARY, NULL},
186 {">>" N_("Sizes"), NULL, small_with, DETAILS_SIZE, NULL},
187 {">>" N_("Permissions"), NULL, small_with, DETAILS_PERMISSIONS, NULL},
188 {">>" N_("Type"), NULL, small_with, DETAILS_TYPE, NULL},
189 {">>" N_("Times"), NULL, small_with, DETAILS_TIMES, NULL},
190 {">", NULL, NULL, 0, "<Separator>"},
191 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
192 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
193 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
194 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
195 {">", NULL, NULL, 0, "<Separator>"},
196 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
197 {">" N_("Refresh"), NULL, refresh, 0, NULL},
198 {">" N_("Create Thumbs"), NULL, create_thumbs, 0, NULL},
199 {N_("File"), NULL, NULL, 0, "<Branch>"},
200 {">" N_("Copy..."), NULL, copy_item, 0, NULL},
201 {">" N_("Rename..."), NULL, rename_item, 0, NULL},
202 {">" N_("Link..."), NULL, link_item, 0, NULL},
203 {">" N_("Shift Open"), NULL, open_file, 0, NULL},
204 {">" N_("Help"), NULL, help, 0, NULL},
205 {">" N_("Info"), NULL, show_file_info, 0, NULL},
206 {">" N_("Set Run Action..."), NULL, run_action, 0, NULL},
207 {">" N_("Set Icon..."), NULL, set_icon, 0, NULL},
208 #ifdef HAVE_LIBVFS
209 {">" N_("Open VFS"), NULL, NULL, 0, "<Branch>"},
210 {">>" N_("Unzip"), NULL, open_vfs_uzip, 0, NULL},
211 {">>" N_("Untar"), NULL, open_vfs_utar, 0, NULL},
212 {">>" N_("Deb"), NULL, open_vfs_deb, 0, NULL},
213 {">>" N_("RPM"), NULL, open_vfs_rpm, 0, NULL},
214 #else
215 {">" N_("Open AVFS"), NULL, open_vfs_avfs, 0, NULL},
216 #endif
217 {">", NULL, NULL, 0, "<Separator>"},
218 {">" N_("Send To..."), NULL, send_to, 0, NULL},
219 {">" N_("Delete"), NULL, delete, 0, NULL},
220 {">" N_("Disk Usage"), NULL, usage, 0, NULL},
221 {">" N_("Permissions"), NULL, chmod_items, 0, NULL},
222 {">" N_("Find"), NULL, find, 0, NULL},
223 {N_("Select All"), NULL, select_all, 0, NULL},
224 {N_("Clear Selection"), NULL, clear_selection, 0, NULL},
225 {N_("Options..."), NULL, menu_show_options, 0, NULL},
226 {N_("New"), NULL, NULL, 0, "<Branch>"},
227 {">" N_("Directory"), NULL, new_directory, 0, NULL},
228 {">" N_("Blank file"), NULL, new_file, 0, NULL},
229 {N_("Xterm Here"), NULL, xterm_here, 0, NULL},
230 {N_("Window"), NULL, NULL, 0, "<Branch>"},
231 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
232 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
233 {">" N_("New Window"), NULL, new_window, 0, NULL},
234 {">" N_("Home Directory"), NULL, home_directory, 0, NULL},
235 {">" N_("Resize Window"), NULL, resize, 0, NULL},
236 /* {">" N_("New, As User..."), NULL, new_user, 0, NULL}, */
238 {">" N_("Close Window"), NULL, close_window, 0, NULL},
239 {">", NULL, NULL, 0, "<Separator>"},
240 {">" N_("Enter Path..."), NULL, enter_path, 0, NULL},
241 {">" N_("Shell Command..."), NULL, shell_command, 0, NULL},
242 {">" N_("Select If..."), NULL, select_if, 0, NULL},
243 {">", NULL, NULL, 0, "<Separator>"},
244 {">" N_("Show ROX-Filer Help"), NULL, menu_rox_help, 0, NULL},
248 #define GET_MENU_ITEM(var, menu) \
249 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
251 #define GET_SMENU_ITEM(var, menu, sub) \
252 do { \
253 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
254 var = gtk_item_factory_get_widget(item_factory, tmp); \
255 g_free(tmp); \
256 } while (0)
258 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
259 do { \
260 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
261 var = gtk_item_factory_get_widget(item_factory, tmp); \
262 g_free(tmp); \
263 } while (0)
265 /* Creates menu <name> from the <name>_menu_def array.
266 * The accel group <name>_keys must also have been created.
267 * All menu items are translated. Sets 'item_factory'.
269 #define MAKE_MENU(name) \
270 do { \
271 GtkItemFactoryEntry *translated; \
272 int n_entries; \
274 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, \
275 "<" #name ">", name ## _keys); \
277 n_entries = sizeof(name ## _menu_def) / sizeof(*name ## _menu_def); \
278 translated = translate_entries(name ## _menu_def, n_entries); \
279 gtk_item_factory_create_items(item_factory, n_entries, \
280 translated, NULL); \
281 free_translated_entries(translated, n_entries); \
282 } while (0)
284 void menu_init(void)
286 char *menurc;
287 GList *items;
288 guchar *tmp;
289 GtkWidget *item;
290 GtkTooltips *tips;
291 GtkItemFactory *item_factory;
293 filer_keys = gtk_accel_group_new();
294 MAKE_MENU(filer);
296 GET_MENU_ITEM(filer_menu, "filer");
297 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
298 #ifdef HAVE_LIBVFS
299 GET_SSMENU_ITEM(filer_vfs_menu, "filer", "File", "Open VFS");
300 #endif
301 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
303 GET_SSMENU_ITEM(display_large_menu, "filer",
304 "Display", "Large, With...");
305 GET_SSMENU_ITEM(display_small_menu, "filer",
306 "Display", "Small, With...");
308 GET_SMENU_ITEM(filer_new_menu, "filer", "New");
310 /* File '' label... */
311 items = gtk_container_children(GTK_CONTAINER(filer_menu));
312 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
313 g_list_free(items);
315 /* Shift Open... label */
316 items = gtk_container_children(GTK_CONTAINER(filer_file_menu));
317 file_shift_item = GTK_BIN(g_list_nth(items, 3)->data)->child;
318 g_list_free(items);
320 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
321 filer_new_window = GTK_BIN(item)->child;
323 menurc = choices_find_path_load("menus", PROJECT);
324 if (menurc)
326 gtk_item_factory_parse_rc(menurc);
327 mark_menus_modified(FALSE);
328 g_free(menurc);
331 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
332 GTK_SIGNAL_FUNC(menu_closed), NULL);
333 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
334 GTK_SIGNAL_FUNC(menu_closed), NULL);
336 option_add_string("menu_xterm", "xterm", NULL);
337 option_add_saver(save_menus);
339 tips = gtk_tooltips_new();
340 check_relative = gtk_check_button_new_with_label(_("Relative link"));
341 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check_relative), TRUE);
342 GTK_WIDGET_UNSET_FLAGS(check_relative, GTK_CAN_FOCUS);
343 gtk_tooltips_set_tip(tips, check_relative,
344 _("If on, the symlink will store the path from the "
345 "symlink to the target file. Use this if the symlink "
346 "and the target will be moved together.\n"
347 "If off, the path from the root directory is stored - "
348 "use this if the symlink may move but the target will "
349 "stay put."), NULL);
351 savebox = gtk_savebox_new();
352 gtk_box_pack_start(GTK_BOX(GTK_SAVEBOX(savebox)->vbox),
353 check_relative, FALSE, TRUE, 0);
354 gtk_widget_show(check_relative);
356 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_to_file",
357 GTK_SIGNAL_FUNC(save_to_file), NULL);
358 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_done",
359 GTK_SIGNAL_FUNC(gtk_widget_hide),
360 GTK_OBJECT(savebox));
362 atexit(save_menus);
365 /* Name is in the form "<panel>" */
366 GtkItemFactory *menu_create(GtkItemFactoryEntry *def, int n_entries,
367 guchar *name)
369 GtkItemFactory *item_factory;
370 GtkItemFactoryEntry *translated;
371 GtkAccelGroup *keys;
373 keys = gtk_accel_group_new();
375 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, name, keys);
377 translated = translate_entries(def, n_entries);
378 gtk_item_factory_create_items(item_factory, n_entries,
379 translated, NULL);
380 free_translated_entries(translated, n_entries);
382 gtk_accel_group_lock(keys);
384 return item_factory;
387 static void items_sensitive(gboolean state)
389 int n = 9;
390 GList *items, *item;
392 items = item = gtk_container_children(GTK_CONTAINER(filer_file_menu));
393 while (item && n--)
395 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
396 item = item->next;
398 g_list_free(items);
400 #ifdef HAVE_LIBVFS
401 items = item = gtk_container_children(GTK_CONTAINER(filer_vfs_menu));
402 while (item)
404 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
405 item = item->next;
407 g_list_free(items);
408 #endif
411 /* 'data' is an array of three ints:
412 * [ pointer_x, pointer_y, item_under_pointer ]
414 void position_menu(GtkMenu *menu, gint *x, gint *y,
415 #ifdef GTK2
416 gboolean *push_in,
417 #endif
418 gpointer data)
420 int *pos = (int *) data;
421 GtkRequisition requisition;
422 GList *items, *next;
423 int y_shift = 0;
424 int item = pos[2];
426 next = items = gtk_container_children(GTK_CONTAINER(menu));
428 while (item >= 0 && next)
430 int h = ((GtkWidget *) next->data)->requisition.height;
432 if (item > 0)
433 y_shift += h;
434 else
435 y_shift += h / 2;
437 next = next->next;
438 item--;
441 g_list_free(items);
443 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
445 *x = pos[0] - (requisition.width * 7 / 8);
446 *y = pos[1] - y_shift;
448 *x = CLAMP(*x, 0, screen_width - requisition.width);
449 *y = CLAMP(*y, 0, screen_height - requisition.height);
451 #ifdef GTK2
452 *push_in = FALSE;
453 #endif
456 #if 0
457 /* Used when you menu-click on the Large or Small toolbar tools */
458 void show_style_menu(FilerWindow *filer_window,
459 GdkEventButton *event,
460 GtkWidget *menu)
462 int pos[3];
464 pos[0] = event->x_root;
465 pos[1] = event->y_root;
466 pos[2] = 0;
468 window_with_focus = filer_window;
470 popup_menu = menu;
472 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
473 (gpointer) pos, event->button, event->time);
475 #endif
477 /* XXX: Is there any point to this? */
478 typedef enum menu_icon_style {
479 MIS_NONE, MIS_SMALL, MIS_LARGE, MIS_HUGE
480 } MenuIconStyle;
482 static GList *menu_from_dir(GtkWidget *menu, const gchar *dname,
483 MenuIconStyle style, CallbackFn func,
484 gboolean separator, gboolean strip_ext)
486 GList *widgets = NULL;
487 DirItem ditem;
488 DIR *dir;
489 struct dirent *ent;
490 GtkWidget *item;
492 dir = opendir(dname);
493 if (!dir)
494 goto out;
496 if (separator)
498 item = gtk_menu_item_new();
499 widgets = g_list_append(widgets, item);
500 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
503 while ((ent = readdir(dir)))
505 char *dot, *leaf;
506 GtkWidget *hbox;
507 GtkWidget *img;
508 GtkWidget *label;
509 gchar *fname;
510 GdkPixmap *icon;
511 GdkBitmap *mask;
513 /* Ignore hidden files */
514 if (ent->d_name[0] == '.')
515 continue;
517 /* Strip off extension, if any */
518 dot = strchr(ent->d_name, '.');
519 if (strip_ext && dot)
520 leaf = g_strndup(ent->d_name, dot - ent->d_name);
521 else
522 leaf = g_strdup(ent->d_name);
524 fname = g_strconcat(dname, "/", ent->d_name, NULL);
525 diritem_stat(fname, &ditem, FALSE);
527 if (ditem.image && style != MIS_NONE)
529 switch (style) {
530 case MIS_HUGE:
531 if (!ditem.image->huge_pixmap)
532 pixmap_make_huge(ditem.image);
533 icon = ditem.image->huge_pixmap;
534 mask = ditem.image->huge_mask;
535 break;
536 case MIS_LARGE:
537 icon = ditem.image->pixmap;
538 mask = ditem.image->mask;
539 break;
541 case MIS_SMALL:
542 default:
543 if (!ditem.image->sm_pixmap)
544 pixmap_make_small(ditem.image);
545 icon = ditem.image->sm_pixmap;
546 mask = ditem.image->sm_mask;
547 break;
550 item = gtk_menu_item_new();
551 /* TODO: Find a way to allow short-cuts */
552 gtk_widget_lock_accelerators(item);
554 hbox = gtk_hbox_new(FALSE, 2);
555 gtk_container_add(GTK_CONTAINER(item), hbox);
557 img = gtk_pixmap_new(icon, mask);
558 gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 2);
560 label = gtk_label_new(leaf);
561 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
562 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 2);
564 diritem_clear(&ditem);
566 else
567 item = gtk_menu_item_new_with_label(leaf);
569 g_free(leaf);
571 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
572 GTK_SIGNAL_FUNC(func), (GtkObject *) fname);
573 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
574 gtk_signal_connect_object(GTK_OBJECT(item), "destroy",
575 GTK_SIGNAL_FUNC(g_free), (GtkObject *) fname);
577 widgets = g_list_append(widgets, item);
580 closedir(dir);
581 out:
583 return widgets;
586 /* Scan the templates dir and create entries for the New menu */
587 static void update_new_files_menu(MenuIconStyle style)
589 static GList *widgets = NULL;
591 gchar *templ_dname = NULL;
593 if (widgets)
595 GList *next;
597 for (next = widgets; next; next = next->next)
598 gtk_widget_destroy((GtkWidget *) next->data);
600 g_list_free(widgets);
601 widgets = NULL;
604 templ_dname = choices_find_path_load("Templates", "");
605 if (templ_dname)
607 widgets = menu_from_dir(filer_new_menu, templ_dname, style,
608 (CallbackFn) new_file_type, TRUE, TRUE);
609 g_free(templ_dname);
611 gtk_widget_show_all(filer_new_menu);
614 /* 'item' is the number of the item to appear under the pointer. */
615 static void show_popup_menu(GtkWidget *menu, GdkEvent *event, int item)
617 int pos[3];
618 int button;
619 guint32 time = 0;
621 if (event->type == GDK_BUTTON_PRESS ||
622 event->type == GDK_BUTTON_RELEASE)
624 GdkEventButton *bev = (GdkEventButton *) event;
626 pos[0] = bev->x_root;
627 pos[1] = bev->y_root;
628 button = bev->button;
629 time = bev->time;
631 else
633 GdkEventKey *kev = (GdkEventKey *) event;
635 g_return_if_fail(event->type == GDK_KEY_PRESS);
637 get_pointer_xy(pos, pos + 1);
638 button = 0;
639 time = kev->time;
642 pos[2] = item;
644 gtk_widget_show_all(menu);
645 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
646 position_menu, (gpointer) pos, button, time);
647 select_nth_item(GTK_MENU_SHELL(menu), item);
650 void show_filer_menu(FilerWindow *filer_window, GdkEvent *event, int item)
652 DirItem *file_item = NULL;
653 GdkModifierType state = 0;
655 updating_menu++;
657 /* Remove previous AppMenu, if any */
658 appmenu_remove();
660 window_with_focus = filer_window;
662 if (event->type == GDK_BUTTON_PRESS)
663 state = ((GdkEventButton *) event)->state;
664 else if (event->type == GDK_KEY_PRESS)
665 state = ((GdkEventKey *) event)->state;
667 if (filer_window->collection->number_selected == 0 && item >= 0)
669 collection_select_item(filer_window->collection, item);
670 filer_window->temp_item_selected = TRUE;
672 else
674 filer_window->temp_item_selected = FALSE;
677 /* Short-cut to the Send To menu */
678 if (state & GDK_SHIFT_MASK)
680 GList *paths;
682 if (filer_window->collection->number_selected == 0)
684 report_error(PROJECT,
685 _("You should Shift+Menu click over a file to "
686 "send it somewhere"));
687 return;
690 paths = filer_selected_items(filer_window);
692 updating_menu--;
694 show_send_to_menu(paths, event); /* (paths eaten) */
696 return;
700 GtkWidget *file_label, *file_menu;
701 Collection *collection = filer_window->collection;
702 GString *buffer;
704 file_label = filer_file_item;
705 file_menu = filer_file_menu;
706 gtk_check_menu_item_set_active(
707 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
708 filer_window->show_hidden);
709 buffer = g_string_new(NULL);
710 switch (collection->number_selected)
712 case 0:
713 g_string_assign(buffer, _("Next Click"));
714 items_sensitive(TRUE);
715 break;
716 case 1:
717 items_sensitive(TRUE);
718 file_item = selected_item(
719 filer_window->collection);
720 g_string_sprintf(buffer, "%s '%s'",
721 basetype_name(file_item),
722 file_item->leafname);
723 if (!can_set_run_action(file_item))
724 menu_set_items_shaded(filer_file_menu,
725 TRUE, 6, 1);
726 break;
727 default:
728 items_sensitive(FALSE);
729 g_string_sprintf(buffer, _("%d items"),
730 collection->number_selected);
731 break;
733 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
734 g_string_free(buffer, TRUE);
736 menu_show_shift_action(file_shift_item, file_item,
737 collection->number_selected == 0);
738 if (file_item)
739 appmenu_add(make_path(filer_window->path,
740 file_item->leafname)->str,
741 file_item, filer_file_menu);
744 update_new_files_menu(MIS_SMALL);
746 gtk_widget_set_sensitive(filer_new_window, !o_unique_filer_windows);
748 popup_menu = (state & GDK_CONTROL_MASK)
749 ? filer_file_menu
750 : filer_menu;
752 updating_menu--;
754 show_popup_menu(popup_menu, event,
755 popup_menu == filer_file_menu ? 5 : 1);
758 static void menu_closed(GtkWidget *widget)
760 if (window_with_focus == NULL || widget != popup_menu)
761 return; /* Close panel item chosen? */
763 popup_menu = NULL;
765 if (window_with_focus->temp_item_selected)
767 collection_clear_selection(window_with_focus->collection);
768 window_with_focus->temp_item_selected = FALSE;
772 void target_callback(FilerWindow *filer_window,
773 gint item,
774 gpointer real_fn)
776 Collection *collection = filer_window->collection;
778 g_return_if_fail(window_with_focus != NULL);
779 g_return_if_fail(window_with_focus == filer_window);
780 g_return_if_fail(real_fn != NULL);
782 collection_wink_item(collection, item);
783 collection_clear_selection(collection);
784 collection_select_item(collection, item);
785 ((GtkItemFactoryCallback1) real_fn)(NULL, 0, GTK_WIDGET(collection));
786 if (item < collection->number_of_items)
787 collection_unselect_item(collection, item);
790 /* Set the text of the 'Shift Open...' menu item.
791 * If icon is NULL, reset the text and also shade it, unless 'next'.
793 void menu_show_shift_action(GtkWidget *menu_item, DirItem *item, gboolean next)
795 guchar *shift_action = NULL;
797 if (item)
799 if (item->flags & ITEM_FLAG_MOUNT_POINT)
801 if (item->flags & ITEM_FLAG_MOUNTED)
802 shift_action = N_("Unmount");
803 else
804 shift_action = N_("Mount");
806 else if (item->flags & ITEM_FLAG_SYMLINK)
807 shift_action = N_("Show Target");
808 else if (item->base_type == TYPE_DIRECTORY)
809 shift_action = N_("Look Inside");
810 else if (item->base_type == TYPE_FILE)
811 shift_action = N_("Open As Text");
813 gtk_label_set_text(GTK_LABEL(menu_item),
814 shift_action ? _(shift_action)
815 : _("Shift Open"));
816 gtk_widget_set_sensitive(menu_item, shift_action != NULL || next);
819 /* Actions */
821 static void huge_with(gpointer data, guint action, GtkWidget *widget)
823 display_set_layout(window_with_focus, HUGE_ICONS, action);
826 static void large_with(gpointer data, guint action, GtkWidget *widget)
828 display_set_layout(window_with_focus, LARGE_ICONS, action);
831 static void small_with(gpointer data, guint action, GtkWidget *widget)
833 display_set_layout(window_with_focus, SMALL_ICONS, action);
836 static void sort_name(gpointer data, guint action, GtkWidget *widget)
838 g_return_if_fail(window_with_focus != NULL);
840 display_set_sort_fn(window_with_focus, sort_by_name);
843 static void sort_type(gpointer data, guint action, GtkWidget *widget)
845 g_return_if_fail(window_with_focus != NULL);
847 display_set_sort_fn(window_with_focus, sort_by_type);
850 static void sort_date(gpointer data, guint action, GtkWidget *widget)
852 g_return_if_fail(window_with_focus != NULL);
854 display_set_sort_fn(window_with_focus, sort_by_date);
857 static void sort_size(gpointer data, guint action, GtkWidget *widget)
859 g_return_if_fail(window_with_focus != NULL);
861 display_set_sort_fn(window_with_focus, sort_by_size);
864 static void hidden(gpointer data, guint action, GtkWidget *widget)
866 if (updating_menu)
867 return;
869 g_return_if_fail(window_with_focus != NULL);
871 display_set_hidden(window_with_focus, !window_with_focus->show_hidden);
874 static void refresh(gpointer data, guint action, GtkWidget *widget)
876 g_return_if_fail(window_with_focus != NULL);
878 full_refresh();
879 filer_update_dir(window_with_focus, TRUE);
882 static void create_thumbs(gpointer data, guint action, GtkWidget *widget)
884 g_return_if_fail(window_with_focus != NULL);
886 dir_rescan_with_thumbs(window_with_focus->directory,
887 window_with_focus->path);
890 static void delete(gpointer data, guint action, GtkWidget *widget)
892 g_return_if_fail(window_with_focus != NULL);
894 if (window_with_focus->collection->number_selected == 0)
895 filer_target_mode(window_with_focus,
896 target_callback, delete,
897 _("DELETE ... ?"));
898 else
899 action_delete(window_with_focus);
902 static void usage(gpointer data, guint action, GtkWidget *widget)
904 g_return_if_fail(window_with_focus != NULL);
906 if (window_with_focus->collection->number_selected == 0)
907 filer_target_mode(window_with_focus,
908 target_callback, usage,
909 _("Count the size of ... ?"));
910 else
911 action_usage(window_with_focus);
914 static void chmod_items(gpointer data, guint action, GtkWidget *widget)
916 g_return_if_fail(window_with_focus != NULL);
918 if (window_with_focus->collection->number_selected == 0)
919 filer_target_mode(window_with_focus,
920 target_callback, chmod_items,
921 _("Set permissions on ... ?"));
922 else
923 action_chmod(window_with_focus);
926 static void find(gpointer data, guint action, GtkWidget *widget)
928 g_return_if_fail(window_with_focus != NULL);
930 if (window_with_focus->collection->number_selected == 0)
931 filer_target_mode(window_with_focus,
932 target_callback, find,
933 _("Search inside ... ?"));
934 else
935 action_find(window_with_focus);
938 /* This pops up our savebox widget, cancelling any currently open one,
939 * and allows the user to pick a new path for it.
940 * Once the new path has been picked, the callback will be called with
941 * both the current and new paths.
942 * NOTE: This function unrefs 'image'!
944 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
945 gboolean (*callback)(guchar *current, guchar *new))
947 if (GTK_WIDGET_VISIBLE(savebox))
948 gtk_widget_hide(savebox);
950 if (callback == link_cb)
951 gtk_widget_show(check_relative);
952 else
953 gtk_widget_hide(check_relative);
955 if (current_path)
956 g_free(current_path);
957 current_path = g_strdup(path);
958 current_savebox_callback = callback;
960 gtk_window_set_title(GTK_WINDOW(savebox), title);
961 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), current_path);
962 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixmap, image->mask);
963 pixmap_unref(image);
965 gtk_widget_grab_focus(GTK_SAVEBOX(savebox)->entry);
966 gtk_widget_show(savebox);
969 static gint save_to_file(GtkSavebox *savebox, guchar *pathname)
971 g_return_val_if_fail(current_savebox_callback != NULL,
972 GTK_XDS_SAVE_ERROR);
974 return current_savebox_callback(current_path, pathname)
975 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
978 static gboolean copy_cb(guchar *current, guchar *new)
980 return action_with_leaf(action_copy, current, new);
983 static gboolean action_with_leaf(ActionFn action, guchar *current, guchar *new)
985 char *new_dir, *leaf;
986 GList *local_paths;
988 if (new[0] != '/')
990 report_error(PROJECT, _("New pathname is not absolute"));
991 return FALSE;
994 if (new[strlen(new) - 1] == '/')
996 new_dir = g_strdup(new);
997 leaf = NULL;
999 else
1001 guchar *slash;
1003 slash = strrchr(new, '/');
1004 new_dir = g_strndup(new, slash - new);
1005 leaf = slash + 1;
1008 local_paths = g_list_append(NULL, current);
1009 action(local_paths, new_dir, leaf);
1010 g_list_free(local_paths);
1012 g_free(new_dir);
1014 return TRUE;
1017 #define SHOW_SAVEBOX(title, callback) \
1019 DirItem *item; \
1020 guchar *path; \
1021 item = selected_item(collection); \
1022 path = make_path(window_with_focus->path, item->leafname)->str; \
1023 pixmap_ref(item->image); \
1024 savebox_show(title, path, item->image, callback); \
1027 static void copy_item(gpointer data, guint action, GtkWidget *widget)
1029 Collection *collection;
1031 g_return_if_fail(window_with_focus != NULL);
1033 collection = window_with_focus->collection;
1034 if (collection->number_selected > 1)
1036 report_error(PROJECT, _("You cannot do this to more than "
1037 "one item at a time"));
1038 return;
1040 else if (collection->number_selected != 1)
1041 filer_target_mode(window_with_focus,
1042 target_callback, copy_item,
1043 _("Copy ... ?"));
1044 else
1045 SHOW_SAVEBOX(_("Copy"), copy_cb);
1048 static gboolean rename_cb(guchar *current, guchar *new)
1050 return action_with_leaf(action_move, current, new);
1053 static void rename_item(gpointer data, guint action, GtkWidget *widget)
1055 Collection *collection;
1057 g_return_if_fail(window_with_focus != NULL);
1059 collection = window_with_focus->collection;
1060 if (collection->number_selected > 1)
1062 report_error(PROJECT, _("You cannot do this to more than "
1063 "one item at a time"));
1064 return;
1066 else if (collection->number_selected != 1)
1067 filer_target_mode(window_with_focus,
1068 target_callback, rename_item,
1069 _("Rename ... ?"));
1070 else
1071 SHOW_SAVEBOX(_("Rename"), rename_cb);
1074 static gboolean link_cb(guchar *initial, guchar *path)
1076 int err;
1078 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check_relative)))
1080 guchar *rpath;
1082 rpath = get_relative_path(path, initial);
1083 err = symlink(rpath, path);
1085 g_free(rpath);
1087 else
1088 err = symlink(initial, path);
1090 if (err)
1092 report_error("ROX-Filer: symlink()", g_strerror(errno));
1093 return FALSE;
1095 return TRUE;
1098 static void link_item(gpointer data, guint action, GtkWidget *widget)
1100 Collection *collection;
1102 g_return_if_fail(window_with_focus != NULL);
1104 collection = window_with_focus->collection;
1105 if (collection->number_selected > 1)
1107 report_error(PROJECT, _("You cannot do this to more than "
1108 "one item at a time"));
1109 return;
1111 else if (collection->number_selected != 1)
1112 filer_target_mode(window_with_focus,
1113 target_callback, link_item,
1114 _("Symlink ... ?"));
1115 else
1116 SHOW_SAVEBOX(_("Symlink"), link_cb);
1119 static void open_file(gpointer data, guint action, GtkWidget *widget)
1121 Collection *collection;
1123 g_return_if_fail(window_with_focus != NULL);
1125 collection = window_with_focus->collection;
1126 if (collection->number_selected > 1)
1128 report_error(PROJECT, _("You cannot do this to more than "
1129 "one item at a time"));
1130 return;
1132 else if (collection->number_selected != 1)
1133 filer_target_mode(window_with_focus,
1134 target_callback, open_file,
1135 _("Shift Open ... ?"));
1136 else
1137 filer_openitem(window_with_focus,
1138 selected_item_number(collection),
1139 OPEN_SAME_WINDOW | OPEN_SHIFT);
1142 static void run_action(gpointer data, guint action, GtkWidget *widget)
1144 Collection *collection;
1146 g_return_if_fail(window_with_focus != NULL);
1148 collection = window_with_focus->collection;
1149 if (collection->number_selected > 1)
1151 report_error(PROJECT, _("You cannot do this to more than "
1152 "one item at a time"));
1153 return;
1155 else if (collection->number_selected != 1)
1156 filer_target_mode(window_with_focus,
1157 target_callback, run_action,
1158 _("Set run action for ... ?"));
1159 else
1161 DirItem *item;
1163 item = selected_item(collection);
1164 g_return_if_fail(item != NULL);
1166 if (can_set_run_action(item))
1167 type_set_handler_dialog(item->mime_type);
1168 else
1169 report_error(PROJECT,
1170 _("You can only set the run action for a "
1171 "regular file"));
1175 static void set_icon(gpointer data, guint action, GtkWidget *widget)
1177 Collection *collection;
1179 g_return_if_fail(window_with_focus != NULL);
1181 collection = window_with_focus->collection;
1182 if (collection->number_selected > 1)
1184 report_error(PROJECT, _("You cannot do this to more than "
1185 "one item at a time"));
1186 return;
1188 else if (collection->number_selected != 1)
1189 filer_target_mode(window_with_focus,
1190 target_callback, set_icon,
1191 _("Set icon for ... ?"));
1192 else
1194 DirItem *item;
1195 guchar *path;
1197 item = selected_item(collection);
1198 g_return_if_fail(item != NULL);
1200 path = make_path(window_with_focus->path, item->leafname)->str;
1202 icon_set_handler_dialog(item, path);
1206 static void show_file_info(gpointer unused, guint action, GtkWidget *widget)
1208 DirItem *file;
1209 Collection *collection;
1211 g_return_if_fail(window_with_focus != NULL);
1213 collection = window_with_focus->collection;
1214 if (collection->number_selected > 1)
1216 report_error(PROJECT, _("You cannot do this to more than "
1217 "one item at a time"));
1218 return;
1220 else if (collection->number_selected != 1)
1222 filer_target_mode(window_with_focus,
1223 target_callback, show_file_info,
1224 _("Examine ... ?"));
1225 return;
1228 file = selected_item(collection);
1229 infobox_new(make_path(window_with_focus->path, file->leafname)->str);
1232 void open_home(gpointer data, guint action, GtkWidget *widget)
1234 filer_opendir(home_dir);
1237 static void help(gpointer data, guint action, GtkWidget *widget)
1239 Collection *collection;
1240 DirItem *item;
1242 g_return_if_fail(window_with_focus != NULL);
1244 collection = window_with_focus->collection;
1245 if (collection->number_selected > 1)
1247 report_error(PROJECT, _("You cannot do this to more than "
1248 "one item at a time"));
1249 return;
1251 else if (collection->number_selected != 1)
1253 filer_target_mode(window_with_focus, target_callback, help,
1254 _("Help about ... ?"));
1255 return;
1257 item = selected_item(collection);
1259 show_item_help(make_path(window_with_focus->path, item->leafname)->str,
1260 item);
1263 #ifdef HAVE_LIBVFS
1264 #define OPEN_VFS(fs) \
1265 static void open_vfs_ ## fs (gpointer data, guint action, GtkWidget *widget) \
1267 Collection *collection; \
1269 g_return_if_fail(window_with_focus != NULL); \
1271 collection = window_with_focus->collection; \
1272 if (collection->number_selected < 1) \
1273 filer_target_mode(window_with_focus, target_callback, \
1274 open_vfs_ ## fs, \
1275 _("Look inside ... ?")); \
1276 else \
1277 real_vfs_open(#fs); \
1280 static void real_vfs_open(char *fs)
1282 gchar *path;
1283 DirItem *item;
1285 if (window_with_focus->collection->number_selected != 1)
1287 report_error(PROJECT, _("You must select a single file "
1288 "to open as a Virtual File System"));
1289 return;
1292 item = selected_item(window_with_focus->collection);
1294 path = g_strconcat(window_with_focus->path,
1295 "/",
1296 item->leafname,
1297 "#", fs, NULL);
1299 filer_change_to(window_with_focus, path, NULL);
1300 g_free(path);
1303 OPEN_VFS(rpm)
1304 OPEN_VFS(utar)
1305 OPEN_VFS(uzip)
1306 OPEN_VFS(deb)
1307 #else
1308 static void open_vfs_avfs(gpointer data, guint action, GtkWidget *widget)
1310 gchar *path;
1311 DirItem *item;
1312 Collection *collection;
1314 g_return_if_fail(window_with_focus != NULL);
1316 collection = window_with_focus->collection;
1317 if (collection->number_selected < 1)
1319 filer_target_mode(window_with_focus, target_callback,
1320 open_vfs_avfs,
1321 _("Look inside ... ?"));
1322 return;
1324 else if (collection->number_selected != 1)
1326 report_error(PROJECT, _("You must select a single file "
1327 "to open as a Virtual File System"));
1328 return;
1331 item = selected_item(collection);
1333 path = g_strconcat(window_with_focus->path,
1334 "/", item->leafname, "#", NULL);
1336 filer_change_to(window_with_focus, path, NULL);
1337 g_free(path);
1339 #endif
1341 static void select_all(gpointer data, guint action, GtkWidget *widget)
1343 g_return_if_fail(window_with_focus != NULL);
1345 collection_select_all(window_with_focus->collection);
1346 window_with_focus->temp_item_selected = FALSE;
1349 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1351 g_return_if_fail(window_with_focus != NULL);
1353 collection_clear_selection(window_with_focus->collection);
1354 window_with_focus->temp_item_selected = FALSE;
1357 void menu_show_options(gpointer data, guint action, GtkWidget *widget)
1359 options_show();
1362 static gboolean new_directory_cb(guchar *initial, guchar *path)
1364 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1366 report_error("mkdir", g_strerror(errno));
1367 return FALSE;
1370 dir_check_this(path);
1372 if (filer_exists(window_with_focus))
1374 guchar *leaf;
1375 leaf = strrchr(path, '/');
1376 if (leaf)
1377 display_set_autoselect(window_with_focus, leaf + 1);
1380 return TRUE;
1383 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1385 g_return_if_fail(window_with_focus != NULL);
1387 savebox_show(_("New Directory"),
1388 make_path(window_with_focus->path, _("NewDir"))->str,
1389 type_to_icon(&special_directory),
1390 new_directory_cb);
1393 static gboolean new_file_cb(guchar *initial, guchar *path)
1395 int fd;
1397 fd = open(path, O_CREAT | O_EXCL, 0666);
1399 if (fd == -1)
1401 report_error(_("Error creating file"), g_strerror(errno));
1402 return FALSE;
1405 if (close(fd))
1406 report_error(_("Error creating file"), g_strerror(errno));
1408 dir_check_this(path);
1410 if (filer_exists(window_with_focus))
1412 guchar *leaf;
1413 leaf = strrchr(path, '/');
1414 if (leaf)
1415 display_set_autoselect(window_with_focus, leaf + 1);
1418 return TRUE;
1421 static void new_file(gpointer data, guint action, GtkWidget *widget)
1423 g_return_if_fail(window_with_focus != NULL);
1425 savebox_show(_("New File"),
1426 make_path(window_with_focus->path, _("NewFile"))->str,
1427 type_to_icon(&text_plain),
1428 new_file_cb);
1431 static gboolean new_file_type_cb(guchar *initial, guchar *path)
1433 gchar *templ, *templ_dname, *oleaf, *dest, *leaf;
1434 GList *paths;
1436 /* We can work out the template path from the initial name */
1437 oleaf = g_basename(initial);
1438 templ_dname = choices_find_path_load("Templates", "");
1439 if (!templ_dname)
1441 char *mess;
1443 mess = g_strconcat(_("Could not find the template for "),
1444 oleaf, NULL);
1445 report_error(_("Error creating file"), mess);
1446 g_free(mess);
1448 return FALSE;
1451 templ = g_strconcat(templ_dname, "/", oleaf, NULL);
1452 g_free(templ_dname);
1454 dest = g_dirname(path);
1455 leaf = g_basename(path);
1456 paths = g_list_append(NULL, templ);
1458 action_copy(paths, dest, leaf);
1460 g_list_free(paths);
1461 g_free(dest);
1462 g_free(templ);
1464 if (filer_exists(window_with_focus))
1465 display_set_autoselect(window_with_focus, leaf);
1467 return TRUE;
1470 static void do_send_to(gchar *templ)
1472 g_return_if_fail(send_to_paths != NULL);
1474 run_with_files(templ, send_to_paths);
1477 static void new_file_type(gchar *templ)
1479 gchar *leaf;
1480 MIME_type *type;
1482 g_return_if_fail(window_with_focus != NULL);
1484 leaf = g_basename(templ);
1485 type = type_get_type(templ);
1487 savebox_show(_("New File"),
1488 make_path(window_with_focus->path, leaf)->str,
1489 type_to_icon(type),
1490 new_file_type_cb);
1493 static void customise_send_to(gpointer data)
1495 GPtrArray *path;
1496 guchar *msg, *save;
1497 GString *dirs;
1498 int i;
1500 dirs = g_string_new(NULL);
1502 path = choices_list_dirs("");
1503 for (i = 0; i < path->len; i++)
1505 guchar *old = (guchar *) path->pdata[i];
1507 g_string_append(dirs, old);
1508 g_string_append(dirs, "SendTo\n");
1510 choices_free_list(path);
1512 save = choices_find_path_save("", "SendTo", TRUE);
1513 if (save)
1514 mkdir(save, 0777);
1516 msg = g_strdup_printf(
1517 _("The `Send To' menu provides a quick way to send some files "
1518 "to an application. The applications listed are those in "
1519 "the following directories:\n\n%s\n%s\n"
1520 "The `Send To' menu may be opened by Shift+Menu clicking "
1521 "over a file."),
1522 dirs->str,
1523 save ? _("I'll show you your SendTo directory now; you should "
1524 "symlink (Ctrl+Shift drag) any applications you want "
1525 "into it.")
1526 : _("Your CHOICESPATH variable setting prevents "
1527 "customisations - sorry."));
1529 g_string_free(dirs, TRUE);
1531 report_error(PROJECT, msg);
1533 if (save)
1534 filer_opendir(save);
1536 g_free(msg);
1539 /* Scan the SendTo dir and create and show the Send To menu.
1540 * The 'paths' list and every path in it is claimed, and will be
1541 * freed later -- don't free it yourself!
1543 static void show_send_to_menu(GList *paths, GdkEvent *event)
1545 GtkWidget *menu, *item;
1546 GPtrArray *path;
1547 int i;
1549 menu = gtk_menu_new();
1551 path = choices_list_dirs("SendTo");
1553 for (i = 0; i < path->len; i++)
1555 GList *widgets = NULL;
1556 guchar *dir = (guchar *) path->pdata[i];
1558 widgets = menu_from_dir(menu, dir, MIS_SMALL,
1559 (CallbackFn) do_send_to,
1560 FALSE, FALSE);
1562 if (widgets)
1563 gtk_menu_shell_append(GTK_MENU_SHELL(menu),
1564 gtk_menu_item_new());
1566 g_list_free(widgets); /* TODO: Get rid of this */
1569 choices_free_list(path);
1571 item = gtk_menu_item_new_with_label(_("Customise"));
1572 gtk_signal_connect_object(GTK_OBJECT(item), "activate",
1573 GTK_SIGNAL_FUNC(customise_send_to), NULL);
1574 gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
1576 if (send_to_paths)
1578 g_list_foreach(send_to_paths, (GFunc) g_free, NULL);
1579 g_list_free(send_to_paths);
1581 send_to_paths = paths;
1583 gtk_signal_connect(GTK_OBJECT(menu), "unmap_event",
1584 GTK_SIGNAL_FUNC(menu_closed), NULL);
1586 popup_menu = menu;
1587 show_popup_menu(menu, event, 0);
1590 static void send_to(gpointer data, guint action, GtkWidget *widget)
1592 Collection *collection;
1594 g_return_if_fail(window_with_focus != NULL);
1596 collection = window_with_focus->collection;
1598 if (collection->number_selected < 1)
1599 filer_target_mode(window_with_focus, target_callback,
1600 send_to, _("Send ... to ... ?"));
1601 else
1603 GList *paths;
1604 GdkEvent *event;
1606 paths = filer_selected_items(window_with_focus);
1607 event = gtk_get_current_event();
1609 /* Eats paths */
1610 show_send_to_menu(paths, event);
1612 gdk_event_free(event);
1616 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1618 char *argv[] = {"sh", "-c", NULL, NULL};
1620 argv[2] = option_get_static_string("menu_xterm");
1622 g_return_if_fail(window_with_focus != NULL);
1624 if (!spawn_full(argv, window_with_focus->path))
1625 report_error(PROJECT, _("Failed to fork() child process"));
1628 static void home_directory(gpointer data, guint action, GtkWidget *widget)
1630 g_return_if_fail(window_with_focus != NULL);
1632 filer_change_to(window_with_focus, home_dir, NULL);
1635 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1637 g_return_if_fail(window_with_focus != NULL);
1639 filer_open_parent(window_with_focus);
1642 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1644 g_return_if_fail(window_with_focus != NULL);
1646 change_to_parent(window_with_focus);
1649 static void resize(gpointer data, guint action, GtkWidget *widget)
1651 g_return_if_fail(window_with_focus != NULL);
1653 filer_window_autosize(window_with_focus, TRUE);
1656 static void new_window(gpointer data, guint action, GtkWidget *widget)
1658 g_return_if_fail(window_with_focus != NULL);
1660 if (o_unique_filer_windows)
1662 report_error(PROJECT, _("You can't open a second view onto "
1663 "this directory because the `Unique Windows' option "
1664 "is turned on in the Options window."));
1666 else
1667 filer_opendir(window_with_focus->path);
1670 #if 0
1671 static void su_to_user(GtkWidget *dialog)
1673 char *argv[] = {
1674 "xterm", "-e", "su_rox", "USER", "APP_RUN", "DIR", NULL};
1675 GtkEntry *user;
1676 guchar *path;
1678 g_return_if_fail(dialog != NULL);
1680 path = gtk_object_get_data(GTK_OBJECT(dialog), "dir_path");
1681 user = gtk_object_get_data(GTK_OBJECT(dialog), "user_name");
1683 g_return_if_fail(user != NULL && path != NULL);
1685 argv[2] = g_strconcat(app_dir, "/su_rox", NULL);
1686 argv[3] = gtk_entry_get_text(user);
1687 argv[4] = g_strconcat(app_dir, "/AppRun", NULL);
1688 argv[5] = path;
1690 if (!spawn(argv))
1691 report_error(_("fork() failed"), g_strerror(errno));
1693 g_free(argv[2]);
1694 g_free(argv[4]);
1696 gtk_widget_destroy(dialog);
1699 static void new_user(gpointer data, guint action, GtkWidget *widget)
1701 GtkWidget *dialog, *vbox, *hbox, *entry, *button;
1703 g_return_if_fail(window_with_focus != NULL);
1705 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1706 gtk_window_set_title(GTK_WINDOW(dialog), _("New window, as user..."));
1707 gtk_container_set_border_width(GTK_CONTAINER(dialog), 4);
1708 gtk_object_set_data_full(GTK_OBJECT(dialog), "dir_path",
1709 g_strdup(window_with_focus->path), g_free);
1711 vbox = gtk_vbox_new(FALSE, 4);
1712 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1713 gtk_box_pack_start(GTK_BOX(vbox),
1714 gtk_label_new(_("Browse as which user?")),
1715 TRUE, TRUE, 2);
1717 hbox = gtk_hbox_new(FALSE, 4);
1718 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1720 gtk_box_pack_start(GTK_BOX(hbox),
1721 gtk_label_new(_("User:")), FALSE, TRUE, 2);
1723 entry = gtk_entry_new();
1724 gtk_entry_set_text(GTK_ENTRY(entry), "root");
1725 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1726 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 2);
1727 gtk_widget_grab_focus(entry);
1728 gtk_object_set_data(GTK_OBJECT(dialog), "user_name", entry);
1729 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1730 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1732 hbox = gtk_hbox_new(TRUE, 0);
1733 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 2);
1735 button = gtk_button_new_with_label(_("OK"));
1736 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1737 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1738 gtk_window_set_default(GTK_WINDOW(dialog), button);
1739 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1740 GTK_SIGNAL_FUNC(su_to_user), GTK_OBJECT(dialog));
1742 button = gtk_button_new_with_label(_("Cancel"));
1743 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1744 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
1745 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1746 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1747 GTK_OBJECT(dialog));
1749 gtk_widget_show_all(dialog);
1751 #endif
1753 static void close_window(gpointer data, guint action, GtkWidget *widget)
1755 g_return_if_fail(window_with_focus != NULL);
1757 gtk_widget_destroy(window_with_focus->window);
1760 static void enter_path(gpointer data, guint action, GtkWidget *widget)
1762 g_return_if_fail(window_with_focus != NULL);
1764 minibuffer_show(window_with_focus, MINI_PATH);
1767 static void shell_command(gpointer data, guint action, GtkWidget *widget)
1769 g_return_if_fail(window_with_focus != NULL);
1771 minibuffer_show(window_with_focus, MINI_SHELL);
1774 static void select_if(gpointer data, guint action, GtkWidget *widget)
1776 g_return_if_fail(window_with_focus != NULL);
1778 minibuffer_show(window_with_focus, MINI_SELECT_IF);
1781 void menu_rox_help(gpointer data, guint action, GtkWidget *widget)
1783 filer_opendir(make_path(app_dir, "Help")->str);
1786 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1787 void menu_set_items_shaded(GtkWidget *menu, gboolean shaded, int from, int n)
1789 GList *items, *item;
1791 items = gtk_container_children(GTK_CONTAINER(menu));
1793 item = g_list_nth(items, from);
1794 while (item && n--)
1796 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, !shaded);
1797 item = item->next;
1799 g_list_free(items);
1802 /* This is called for every modified menu entry. We just use it to
1803 * find out if the menu has changed at all.
1805 static void set_mod(gboolean *mod, guchar *str)
1807 if (str && str[0] == '(')
1808 *mod = TRUE;
1811 static void save_menus(void)
1813 char *menurc;
1815 menurc = choices_find_path_save("menus", PROJECT, FALSE);
1816 if (menurc)
1818 gboolean mod = FALSE;
1820 g_free(menurc);
1822 /* Find out if anything changed... */
1823 gtk_item_factory_dump_items(NULL, TRUE,
1824 (GtkPrintFunc) set_mod, &mod);
1826 /* Dump out if so... */
1827 if (mod)
1829 menurc = choices_find_path_save("menus", PROJECT, TRUE);
1830 g_return_if_fail(menurc != NULL);
1831 mark_menus_modified(TRUE);
1832 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
1833 mark_menus_modified(FALSE);
1834 g_free(menurc);
1839 static void mark_modified(gpointer hash_key,
1840 gpointer value,
1841 gpointer user_data)
1843 GtkItemFactoryItem *item = (GtkItemFactoryItem *) value;
1845 item->modified = (gboolean) GPOINTER_TO_INT(user_data);
1848 /* Set or clear the 'modified' flag in all menu items. Messy... */
1849 static void mark_menus_modified(gboolean mod)
1851 GtkItemFactoryClass *class;
1853 class = gtk_type_class(GTK_TYPE_ITEM_FACTORY);
1855 g_hash_table_foreach(class->item_ht, mark_modified,
1856 GINT_TO_POINTER(mod));
1859 static void select_nth_item(GtkMenuShell *shell, int n)
1861 GList *items, *nth;
1862 GtkWidget *item = NULL;
1864 items = gtk_container_children(GTK_CONTAINER(shell));
1865 nth = g_list_nth(items, n);
1867 g_return_if_fail(nth != NULL);
1869 item = (GtkWidget *) (nth->data);
1870 g_list_free(items);
1872 gtk_menu_shell_select_item(shell, item);