r288: Removed the borders from the toolbar buttons and put the new help icon
[rox-filer/ma.git] / ROX-Filer / src / menu.c
blob260e31df3a0f096a13f19ef7d3b60fc2bd9c23f1
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
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/stat.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/param.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <string.h>
36 #include <gdk/gdkx.h>
37 #include <gtk/gtk.h>
39 #include "run.h"
40 #include "action.h"
41 #include "filer.h"
42 #include "type.h"
43 #include "support.h"
44 #include "gui_support.h"
45 #include "options.h"
46 #include "choices.h"
47 #include "gtksavebox.h"
48 #include "mount.h"
49 #include "minibuffer.h"
50 #include "i18n.h"
52 #define C_ "<control>"
54 #define MENU_MARGIN 32
56 GtkAccelGroup *filer_keys;
57 GtkAccelGroup *panel_keys;
59 static GtkWidget *popup_menu = NULL; /* Currently open menu */
61 static gint updating_menu = 0; /* Non-zero => ignore activations */
63 /* Options */
64 static GtkWidget *xterm_here_entry;
65 static char *xterm_here_value;
67 /* Static prototypes */
69 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data);
70 static void menu_closed(GtkWidget *widget);
71 static void items_sensitive(gboolean state);
72 static char *load_xterm_here(char *data);
73 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
74 gboolean (*callback)(guchar *current, guchar *new));
75 static gint save_to_file(GtkSavebox *savebox, guchar *pathname);
77 /* Note that for these callbacks none of the arguments are used. */
78 static void large(gpointer data, guint action, GtkWidget *widget);
79 static void small(gpointer data, guint action, GtkWidget *widget);
80 static void full_info(gpointer data, guint action, GtkWidget *widget);
82 static void sort_name(gpointer data, guint action, GtkWidget *widget);
83 static void sort_type(gpointer data, guint action, GtkWidget *widget);
84 static void sort_size(gpointer data, guint action, GtkWidget *widget);
85 static void sort_date(gpointer data, guint action, GtkWidget *widget);
87 static void hidden(gpointer data, guint action, GtkWidget *widget);
88 static void refresh(gpointer data, guint action, GtkWidget *widget);
90 static void copy_item(gpointer data, guint action, GtkWidget *widget);
91 static void rename_item(gpointer data, guint action, GtkWidget *widget);
92 static void link_item(gpointer data, guint action, GtkWidget *widget);
93 static void open_file(gpointer data, guint action, GtkWidget *widget);
94 static void help(gpointer data, guint action, GtkWidget *widget);
95 static void show_file_info(gpointer data, guint action, GtkWidget *widget);
96 static void mount(gpointer data, guint action, GtkWidget *widget);
97 static void delete(gpointer data, guint action, GtkWidget *widget);
98 static void remove_link(gpointer data, guint action, GtkWidget *widget);
99 static void usage(gpointer data, guint action, GtkWidget *widget);
100 static void chmod_items(gpointer data, guint action, GtkWidget *widget);
101 static void find(gpointer data, guint action, GtkWidget *widget);
103 static void open_vfs_rpm(gpointer data, guint action, GtkWidget *widget);
104 static void open_vfs_utar(gpointer data, guint action, GtkWidget *widget);
105 static void open_vfs_uzip(gpointer data, guint action, GtkWidget *widget);
107 static void select_all(gpointer data, guint action, GtkWidget *widget);
108 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
109 static void show_options(gpointer data, guint action, GtkWidget *widget);
110 static void new_directory(gpointer data, guint action, GtkWidget *widget);
111 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
113 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
114 static void open_parent(gpointer data, guint action, GtkWidget *widget);
115 static void new_window(gpointer data, guint action, GtkWidget *widget);
116 static void close_window(gpointer data, guint action, GtkWidget *widget);
117 static void enter_path(gpointer data, guint action, GtkWidget *widget);
118 static void shell_command(gpointer data, guint action, GtkWidget *widget);
119 static void run_action(gpointer data, guint action, GtkWidget *widget);
120 static void select_if(gpointer data, guint action, GtkWidget *widget);
121 static void rox_help(gpointer data, guint action, GtkWidget *widget);
123 static void open_as_dir(gpointer data, guint action, GtkWidget *widget);
124 static void close_panel(gpointer data, guint action, GtkWidget *widget);
126 static GtkWidget *create_options();
127 static void update_options();
128 static void set_options();
129 static void save_options();
131 static OptionsSection options =
133 N_("Menu options"),
134 create_options,
135 update_options,
136 set_options,
137 save_options
141 static GtkWidget *filer_menu; /* The popup filer menu */
142 static GtkWidget *filer_file_item; /* The File '' label */
143 static GtkWidget *filer_file_menu; /* The File '' menu */
144 static GtkWidget *filer_vfs_menu; /* The Open VFS menu */
145 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
146 static GtkWidget *filer_new_window; /* The New Window item */
147 static GtkWidget *panel_menu; /* The popup panel menu */
148 static GtkWidget *panel_hidden_menu; /* The Show Hidden item */
150 /* Used for Copy, etc */
151 static GtkWidget *savebox = NULL;
152 static guchar *current_path = NULL;
153 static gboolean (*current_savebox_callback)(guchar *current, guchar *new);
155 static gint screen_width, screen_height;
157 #undef N_
158 #define N_(x) x
160 static GtkItemFactoryEntry filer_menu_def[] = {
161 {N_("Display"), NULL, NULL, 0, "<Branch>"},
162 {">" N_("Large Icons"), NULL, large, 0, NULL},
163 {">" N_("Small Icons"), NULL, small, 0, NULL},
164 {">" N_("Full Info"), NULL, full_info, 0, NULL},
165 {">", NULL, NULL, 0, "<Separator>"},
166 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
167 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
168 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
169 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
170 {">", NULL, NULL, 0, "<Separator>"},
171 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
172 {">" N_("Refresh"), NULL, refresh, 0, NULL},
173 {N_("File"), NULL, NULL, 0, "<Branch>"},
174 {">" N_("Copy..."), NULL, copy_item, 0, NULL},
175 {">" N_("Rename..."), NULL, rename_item, 0, NULL},
176 {">" N_("Link..."), NULL, link_item, 0, NULL},
177 {">" N_("Shift Open"), NULL, open_file, 0, NULL},
178 {">" N_("Help"), NULL, help, 0, NULL},
179 {">" N_("Info"), NULL, show_file_info, 0, NULL},
180 {">" N_("Open VFS"), NULL, NULL, 0, "<Branch>"},
181 {">>" N_("Unzip"), NULL, open_vfs_uzip, 0, NULL},
182 {">>" N_("Untar"), NULL, open_vfs_utar, 0, NULL},
183 {">>" N_("RPM"), NULL, open_vfs_rpm, 0, NULL},
184 {">", NULL, NULL, 0, "<Separator>"},
185 {">" N_("Mount"), NULL, mount, 0, NULL},
186 {">" N_("Delete"), NULL, delete, 0, NULL},
187 {">" N_("Disk Usage"), NULL, usage, 0, NULL},
188 {">" N_("Permissions"), NULL, chmod_items, 0, NULL},
189 {">" N_("Find"), NULL, find, 0, NULL},
190 {N_("Select All"), NULL, select_all, 0, NULL},
191 {N_("Clear Selection"), NULL, clear_selection, 0, NULL},
192 {N_("Options..."), NULL, show_options, 0, NULL},
193 {N_("New Directory..."), NULL, new_directory, 0, NULL},
194 {N_("Xterm Here"), NULL, xterm_here, 0, NULL},
195 {N_("Window"), NULL, NULL, 0, "<Branch>"},
196 {">" N_("Parent, New Window"), NULL, open_parent, 0, NULL},
197 {">" N_("Parent, Same Window"), NULL, open_parent_same, 0, NULL},
198 {">" N_("New Window"), NULL, new_window, 0, NULL},
199 {">" N_("Close Window"), NULL, close_window, 0, NULL},
200 {">", NULL, NULL, 0, "<Separator>"},
201 {">" N_("Enter Path..."), NULL, enter_path, 0, NULL},
202 {">" N_("Shell Command..."), NULL, shell_command, 0, NULL},
203 {">" N_("Set Run Action..."), NULL, run_action, 0, NULL},
204 {">" N_("Select If..."), NULL, select_if, 0, NULL},
205 {">", NULL, NULL, 0, "<Separator>"},
206 {">" N_("Show ROX-Filer Help"), NULL, rox_help, 0, NULL},
209 static GtkItemFactoryEntry panel_menu_def[] = {
210 {N_("Display"), NULL, NULL, 0, "<Branch>"},
211 {">" N_("Sort by Name"), NULL, sort_name, 0, NULL},
212 {">" N_("Sort by Type"), NULL, sort_type, 0, NULL},
213 {">" N_("Sort by Date"), NULL, sort_date, 0, NULL},
214 {">" N_("Sort by Size"), NULL, sort_size, 0, NULL},
215 {">", NULL, NULL, 0, "<Separator>"},
216 {">" N_("Show Hidden"), NULL, hidden, 0, "<ToggleItem>"},
217 {">" N_("Refresh"), NULL, refresh, 0, NULL},
218 {N_("Open Panel as Directory"), NULL, open_as_dir, 0, NULL},
219 {N_("Close Panel"), NULL, close_panel, 0, NULL},
220 {"", NULL, NULL, 0, "<Separator>"},
221 {N_("ROX-Filer Help"), NULL, rox_help, 0, NULL},
222 {N_("ROX-Filer Options..."), NULL, show_options, 0, NULL},
223 {"", NULL, NULL, 0, "<Separator>"},
224 {N_("Show Help"), NULL, help, 0, NULL},
225 {N_("Remove Item"), NULL, remove_link, 0, NULL},
228 typedef struct _FileStatus FileStatus;
230 /* This is for the 'file(1) says...' thing */
231 struct _FileStatus
233 int fd; /* FD to read from, -1 if closed */
234 int input; /* Input watcher tag if fd valid */
235 GtkLabel *label; /* Widget to output to */
236 gboolean start; /* No output yet */
239 #define GET_MENU_ITEM(var, menu) \
240 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
242 #define GET_SMENU_ITEM(var, menu, sub) \
243 do { \
244 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
245 var = gtk_item_factory_get_widget(item_factory, tmp); \
246 g_free(tmp); \
247 } while (0)
249 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
250 do { \
251 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
252 var = gtk_item_factory_get_widget(item_factory, tmp); \
253 g_free(tmp); \
254 } while (0)
256 void menu_init()
258 GtkItemFactory *item_factory;
259 char *menurc;
260 GList *items;
261 guchar *tmp;
262 GtkWidget *item;
263 int n_entries;
264 GtkItemFactoryEntry *translated;
266 /* This call starts returning strange values after a while, so get
267 * the result here during init.
269 gdk_window_get_size(GDK_ROOT_PARENT(), &screen_width, &screen_height);
271 filer_keys = gtk_accel_group_new();
272 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
273 "<filer>",
274 filer_keys);
276 n_entries = sizeof(filer_menu_def) / sizeof(*filer_menu_def);
277 translated = translate_entries(filer_menu_def, n_entries);
278 gtk_item_factory_create_items (item_factory, n_entries,
279 translated, NULL);
280 free_translated_entries(translated, n_entries);
282 GET_MENU_ITEM(filer_menu, "filer");
283 GET_SMENU_ITEM(filer_file_menu, "filer", "File");
284 GET_SSMENU_ITEM(filer_vfs_menu, "filer", "File", "Open VFS");
285 GET_SSMENU_ITEM(filer_hidden_menu, "filer", "Display", "Show Hidden");
287 items = gtk_container_children(GTK_CONTAINER(filer_menu));
288 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
289 g_list_free(items);
291 GET_SSMENU_ITEM(item, "filer", "Window", "New Window");
292 filer_new_window = GTK_BIN(item)->child;
294 panel_keys = gtk_accel_group_new();
295 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
296 "<panel>",
297 panel_keys);
299 n_entries = sizeof(panel_menu_def) / sizeof(*panel_menu_def);
300 translated = translate_entries(panel_menu_def, n_entries);
301 gtk_item_factory_create_items (item_factory, n_entries,
302 translated, NULL);
303 free_translated_entries(translated, n_entries);
305 GET_MENU_ITEM(panel_menu, "panel");
306 GET_SSMENU_ITEM(panel_hidden_menu, "panel", "Display", "Show Hidden");
308 menurc = choices_find_path_load("menus", PROJECT);
309 if (menurc)
310 gtk_item_factory_parse_rc(menurc);
312 gtk_accel_group_lock(panel_keys);
314 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
315 GTK_SIGNAL_FUNC(menu_closed), NULL);
316 gtk_signal_connect(GTK_OBJECT(panel_menu), "unmap_event",
317 GTK_SIGNAL_FUNC(menu_closed), NULL);
318 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
319 GTK_SIGNAL_FUNC(menu_closed), NULL);
321 options_sections = g_slist_prepend(options_sections, &options);
322 xterm_here_value = g_strdup("xterm");
323 option_register("xterm_here", load_xterm_here);
325 savebox = gtk_savebox_new();
326 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_to_file",
327 GTK_SIGNAL_FUNC(save_to_file), NULL);
328 gtk_signal_connect_object(GTK_OBJECT(savebox), "save_done",
329 GTK_SIGNAL_FUNC(gtk_widget_hide),
330 GTK_OBJECT(savebox));
333 /* Build up some option widgets to go in the options dialog, but don't
334 * fill them in yet.
336 static GtkWidget *create_options()
338 GtkWidget *table, *label;
340 table = gtk_table_new(2, 2, FALSE);
341 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
343 label = gtk_label_new(
344 _("To set the keyboard short-cuts you simply open "
345 "the menu over a filer window, move the pointer over "
346 "the item you want to use and press a key. The key "
347 "will appear next to the menu item and you can just "
348 "press that key without opening the menu in future. "
349 "To save the current menu short-cuts for next time, "
350 "click the Save button at the bottom of this window."));
351 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
352 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 2, 0, 1);
354 label = gtk_label_new(_("'Xterm here' program:"));
355 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
356 xterm_here_entry = gtk_entry_new();
357 gtk_table_attach_defaults(GTK_TABLE(table), xterm_here_entry,
358 1, 2, 1, 2);
360 return table;
363 static char *load_xterm_here(char *data)
365 g_free(xterm_here_value);
366 xterm_here_value = g_strdup(data);
367 return NULL;
370 static void update_options()
372 gtk_entry_set_text(GTK_ENTRY(xterm_here_entry), xterm_here_value);
375 static void set_options()
377 g_free(xterm_here_value);
378 xterm_here_value = g_strdup(gtk_entry_get_text(
379 GTK_ENTRY(xterm_here_entry)));
382 static void save_options()
384 char *menurc;
386 menurc = choices_find_path_save("menus", PROJECT, TRUE);
387 if (menurc)
388 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
390 option_write("xterm_here", xterm_here_value);
394 static void items_sensitive(gboolean state)
396 int n = 7;
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 items = item = gtk_container_children(GTK_CONTAINER(filer_vfs_menu));
408 while (item)
410 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
411 item = item->next;
413 g_list_free(items);
416 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data)
418 int *pos = (int *) data;
419 GtkRequisition requisition;
421 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
423 if (pos[0] == -1)
424 *x = screen_width - MENU_MARGIN - requisition.width;
425 else if (pos[0] == -2)
426 *x = MENU_MARGIN;
427 else
428 *x = pos[0] - (requisition.width >> 2);
430 if (pos[1] == -1)
431 *y = screen_height - MENU_MARGIN - requisition.height;
432 else if (pos[1] == -2)
433 *y = MENU_MARGIN;
434 else
435 *y = pos[1] - (requisition.height >> 2);
437 *x = CLAMP(*x, 0, screen_width - requisition.width);
438 *y = CLAMP(*y, 0, screen_height - requisition.height);
441 void show_filer_menu(FilerWindow *filer_window, GdkEventButton *event,
442 int item)
444 DirItem *file_item;
445 int pos[2];
447 updating_menu++;
449 pos[0] = event->x_root;
450 pos[1] = event->y_root;
452 window_with_focus = filer_window;
454 switch (filer_window->panel_type)
456 case PANEL_TOP:
457 pos[1] = -2;
458 break;
459 case PANEL_BOTTOM:
460 pos[1] = -1;
461 break;
462 default:
463 break;
466 if (filer_window->panel_type)
467 collection_clear_selection(filer_window->collection); /* ??? */
469 if (filer_window->collection->number_selected == 0 && item >= 0)
471 collection_select_item(filer_window->collection, item);
472 filer_window->temp_item_selected = TRUE;
474 else
475 filer_window->temp_item_selected = FALSE;
477 if (filer_window->panel_type)
479 gtk_check_menu_item_set_active(
480 GTK_CHECK_MENU_ITEM(panel_hidden_menu),
481 filer_window->show_hidden);
483 else
485 GtkWidget *file_label, *file_menu;
486 Collection *collection = filer_window->collection;
487 GString *buffer;
489 file_label = filer_file_item;
490 file_menu = filer_file_menu;
491 gtk_check_menu_item_set_active(
492 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
493 filer_window->show_hidden);
494 buffer = g_string_new(NULL);
495 switch (collection->number_selected)
497 case 0:
498 g_string_assign(buffer, _("Next Click"));
499 items_sensitive(TRUE);
500 break;
501 case 1:
502 items_sensitive(TRUE);
503 file_item = selected_item(
504 filer_window->collection);
505 g_string_sprintf(buffer, "%s '%s'",
506 basetype_name(file_item),
507 file_item->leafname);
508 break;
509 default:
510 items_sensitive(FALSE);
511 g_string_sprintf(buffer, _("%d items"),
512 collection->number_selected);
513 break;
515 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
516 g_string_free(buffer, TRUE);
520 gtk_widget_set_sensitive(filer_new_window, !o_unique_filer_windows);
522 if (filer_window->panel_type)
523 popup_menu = panel_menu;
524 else
525 popup_menu = (event->state & GDK_CONTROL_MASK)
526 ? filer_file_menu
527 : filer_menu;
529 updating_menu--;
531 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
532 (gpointer) pos, event->button, event->time);
535 static void menu_closed(GtkWidget *widget)
537 if (window_with_focus == NULL || widget != popup_menu)
538 return; /* Close panel item chosen? */
540 if (window_with_focus->temp_item_selected)
542 collection_clear_selection(window_with_focus->collection);
543 window_with_focus->temp_item_selected = FALSE;
547 void target_callback(Collection *collection, gint item, gpointer real_fn)
549 g_return_if_fail(window_with_focus != NULL);
550 g_return_if_fail(window_with_focus->collection == collection);
551 g_return_if_fail(real_fn != NULL);
553 collection_wink_item(collection, item);
554 collection_clear_selection(collection);
555 collection_select_item(collection, item);
556 ((CollectionTargetFunc)real_fn)(NULL, 0, collection);
557 if (item < collection->number_of_items)
558 collection_unselect_item(collection, item);
561 /* Actions */
563 static void large(gpointer data, guint action, GtkWidget *widget)
565 g_return_if_fail(window_with_focus != NULL);
567 filer_style_set(window_with_focus, LARGE_ICONS);
570 static void small(gpointer data, guint action, GtkWidget *widget)
572 g_return_if_fail(window_with_focus != NULL);
574 filer_style_set(window_with_focus, SMALL_ICONS);
577 static void full_info(gpointer data, guint action, GtkWidget *widget)
579 g_return_if_fail(window_with_focus != NULL);
581 filer_style_set(window_with_focus, FULL_INFO);
584 static void sort_name(gpointer data, guint action, GtkWidget *widget)
586 g_return_if_fail(window_with_focus != NULL);
588 filer_set_sort_fn(window_with_focus, sort_by_name);
591 static void sort_type(gpointer data, guint action, GtkWidget *widget)
593 g_return_if_fail(window_with_focus != NULL);
595 filer_set_sort_fn(window_with_focus, sort_by_type);
598 static void sort_date(gpointer data, guint action, GtkWidget *widget)
600 g_return_if_fail(window_with_focus != NULL);
602 filer_set_sort_fn(window_with_focus, sort_by_date);
605 static void sort_size(gpointer data, guint action, GtkWidget *widget)
607 g_return_if_fail(window_with_focus != NULL);
609 filer_set_sort_fn(window_with_focus, sort_by_size);
612 static void hidden(gpointer data, guint action, GtkWidget *widget)
614 if (updating_menu)
615 return;
617 g_return_if_fail(window_with_focus != NULL);
619 filer_set_hidden(window_with_focus, !window_with_focus->show_hidden);
622 static void refresh(gpointer data, guint action, GtkWidget *widget)
624 g_return_if_fail(window_with_focus != NULL);
626 full_refresh();
627 filer_update_dir(window_with_focus, TRUE);
630 static void mount(gpointer data, guint action, GtkWidget *widget)
632 g_return_if_fail(window_with_focus != NULL);
634 if (window_with_focus->collection->number_selected == 0)
635 collection_target(window_with_focus->collection,
636 target_callback, mount);
637 else
638 action_mount(window_with_focus, NULL);
641 static void delete(gpointer data, guint action, GtkWidget *widget)
643 g_return_if_fail(window_with_focus != NULL);
645 if (window_with_focus->collection->number_selected == 0)
646 collection_target(window_with_focus->collection,
647 target_callback, delete);
648 else
649 action_delete(window_with_focus);
652 static void remove_link(gpointer data, guint action, GtkWidget *widget)
654 g_return_if_fail(window_with_focus != NULL);
656 if (window_with_focus->collection->number_selected > 1)
658 report_error(PROJECT,
659 _("You can only remove one link at a time"));
660 return;
662 else if (window_with_focus->collection->number_selected == 0)
663 collection_target(window_with_focus->collection,
664 target_callback, remove_link);
665 else
667 struct stat info;
668 DirItem *item;
669 guchar *path;
671 item = selected_item(window_with_focus->collection);
673 path = make_path(window_with_focus->path, item->leafname)->str;
674 if (lstat(path, &info))
675 report_error(PROJECT, g_strerror(errno));
676 else if (!S_ISLNK(info.st_mode))
677 report_error(PROJECT,
678 _("You can only remove symbolic links this way - "
679 "try the 'Open Panel as Directory' item."));
680 else if (unlink(path))
681 report_error(PROJECT, g_strerror(errno));
682 else
683 filer_update_dir(window_with_focus, TRUE);
687 static void usage(gpointer data, guint action, GtkWidget *widget)
689 g_return_if_fail(window_with_focus != NULL);
691 if (window_with_focus->collection->number_selected == 0)
692 collection_target(window_with_focus->collection,
693 target_callback, usage);
694 else
695 action_usage(window_with_focus);
698 static void chmod_items(gpointer data, guint action, GtkWidget *widget)
700 g_return_if_fail(window_with_focus != NULL);
702 if (window_with_focus->collection->number_selected == 0)
703 collection_target(window_with_focus->collection,
704 target_callback, chmod_items);
705 else
706 action_chmod(window_with_focus);
709 static void find(gpointer data, guint action, GtkWidget *widget)
711 g_return_if_fail(window_with_focus != NULL);
713 if (window_with_focus->collection->number_selected == 0)
714 collection_target(window_with_focus->collection,
715 target_callback, find);
716 else
717 action_find(window_with_focus);
720 /* This pops up our savebox widget, cancelling any currently open one,
721 * and allows the user to pick a new path for it.
722 * Once the new path has been picked, the callback will be called with
723 * both the current and new paths.
725 static void savebox_show(guchar *title, guchar *path, MaskedPixmap *image,
726 gboolean (*callback)(guchar *current, guchar *new))
728 if (GTK_WIDGET_VISIBLE(savebox))
729 gtk_widget_hide(savebox);
731 if (current_path)
732 g_free(current_path);
733 current_path = g_strdup(path);
734 current_savebox_callback = callback;
736 gtk_window_set_title(GTK_WINDOW(savebox), title);
737 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox), current_path);
738 gtk_savebox_set_icon(GTK_SAVEBOX(savebox), image->pixmap, image->mask);
740 gtk_widget_grab_focus(GTK_SAVEBOX(savebox)->entry);
741 gtk_widget_show(savebox);
744 static gint save_to_file(GtkSavebox *savebox, guchar *pathname)
746 g_return_val_if_fail(current_savebox_callback != NULL,
747 GTK_XDS_SAVE_ERROR);
749 return current_savebox_callback(current_path, pathname)
750 ? GTK_XDS_SAVED : GTK_XDS_SAVE_ERROR;
753 static gboolean copy_cb(guchar *current, guchar *new)
755 char *new_dir, *leaf;
756 GSList *local_paths;
758 if (new[0] != '/')
760 report_error(PROJECT, _("New pathname is not absolute"));
761 return FALSE;
764 if (new[strlen(new) - 1] == '/')
766 new_dir = g_strdup(new);
767 leaf = NULL;
769 else
771 guchar *slash;
773 slash = strrchr(new, '/');
774 new_dir = g_strndup(new, slash - new);
775 leaf = slash + 1;
778 local_paths = g_slist_append(NULL, current);
779 action_copy(local_paths, new_dir, leaf);
780 g_slist_free(local_paths);
782 g_free(new_dir);
784 return TRUE;
787 #define SHOW_SAVEBOX(title, callback) \
789 DirItem *item; \
790 guchar *path; \
791 item = selected_item(collection); \
792 path = make_path(window_with_focus->path, item->leafname)->str; \
793 savebox_show(title, path, item->image, callback); \
796 static void copy_item(gpointer data, guint action, GtkWidget *widget)
798 Collection *collection;
800 g_return_if_fail(window_with_focus != NULL);
802 collection = window_with_focus->collection;
803 if (collection->number_selected > 1)
805 report_error(PROJECT, _("You cannot do this to more than "
806 "one item at a time"));
807 return;
809 else if (collection->number_selected != 1)
810 collection_target(collection, target_callback, copy_item);
811 else
812 SHOW_SAVEBOX(_("Copy"), copy_cb);
815 static gboolean rename_cb(guchar *current, guchar *new)
817 if (rename(current, new))
819 report_error("ROX-Filer: rename()", g_strerror(errno));
820 return FALSE;
822 return TRUE;
825 static void rename_item(gpointer data, guint action, GtkWidget *widget)
827 Collection *collection;
829 g_return_if_fail(window_with_focus != NULL);
831 collection = window_with_focus->collection;
832 if (collection->number_selected > 1)
834 report_error(PROJECT, _("You cannot do this to more than "
835 "one item at a time"));
836 return;
838 else if (collection->number_selected != 1)
839 collection_target(collection, target_callback, rename_item);
840 else
841 SHOW_SAVEBOX(_("Rename"), rename_cb);
844 static gboolean link_cb(guchar *initial, guchar *path)
846 if (symlink(initial, path))
848 report_error("ROX-Filer: symlink()", g_strerror(errno));
849 return FALSE;
851 return TRUE;
854 static void link_item(gpointer data, guint action, GtkWidget *widget)
856 Collection *collection;
858 g_return_if_fail(window_with_focus != NULL);
860 collection = window_with_focus->collection;
861 if (collection->number_selected > 1)
863 report_error(PROJECT, _("You cannot do this to more than "
864 "one item at a time"));
865 return;
867 else if (collection->number_selected != 1)
868 collection_target(collection, target_callback, link_item);
869 else
870 SHOW_SAVEBOX(_("Symlink"), link_cb);
873 static void open_file(gpointer data, guint action, GtkWidget *widget)
875 Collection *collection;
877 g_return_if_fail(window_with_focus != NULL);
879 collection = window_with_focus->collection;
880 if (collection->number_selected > 1)
882 report_error(PROJECT, _("You cannot do this to more than "
883 "one item at a time"));
884 return;
886 else if (collection->number_selected != 1)
887 collection_target(collection, target_callback, open_file);
888 else
889 filer_openitem(window_with_focus,
890 selected_item_number(collection),
891 OPEN_SAME_WINDOW | OPEN_SHIFT);
894 /* Got some data from file(1) - stick it in the window. */
895 static void add_file_output(FileStatus *fs,
896 gint source, GdkInputCondition condition)
898 char buffer[20];
899 char *str;
900 int got;
902 got = read(source, buffer, sizeof(buffer) - 1);
903 if (got <= 0)
905 int err = errno;
906 gtk_input_remove(fs->input);
907 close(source);
908 fs->fd = -1;
909 if (got < 0)
910 delayed_error(_("ROX-Filer: file(1) says..."),
911 g_strerror(err));
912 return;
914 buffer[got] = '\0';
916 if (fs->start)
918 str = "";
919 fs->start = FALSE;
921 else
922 gtk_label_get(fs->label, &str);
924 str = g_strconcat(str, buffer, NULL);
925 gtk_label_set_text(fs->label, str);
926 g_free(str);
929 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs)
931 if (fs->fd != -1)
933 gtk_input_remove(fs->input);
934 close(fs->fd);
937 g_free(fs);
940 /* g_free() the result */
941 guchar *pretty_type(DirItem *file, guchar *path)
943 if (file->flags & ITEM_FLAG_SYMLINK)
945 char p[MAXPATHLEN + 1];
946 int got;
947 got = readlink(path, p, MAXPATHLEN);
948 if (got > 0 && got <= MAXPATHLEN)
950 p[got] = '\0';
951 return g_strconcat(_("Symbolic link to "), p, NULL);
954 return g_strdup(_("Symbolic link"));
957 if (file->flags & ITEM_FLAG_EXEC_FILE)
958 return g_strdup(_("Executable file"));
960 if (file->flags & ITEM_FLAG_APPDIR)
961 return g_strdup(_("ROX application"));
963 if (file->flags & ITEM_FLAG_MOUNT_POINT)
965 MountPoint *mp;
966 if ((file->flags & ITEM_FLAG_MOUNTED) &&
967 (mp = g_hash_table_lookup(mtab_mounts, path)))
968 return g_strconcat(_("Mount point for "),
969 mp->name, NULL);
971 return g_strdup(_("Mount point"));
974 if (file->mime_type)
975 return g_strconcat(file->mime_type->media_type, "/",
976 file->mime_type->subtype, NULL);
978 return g_strdup("-");
981 #define LABEL(text, row) \
982 label = gtk_label_new(text); \
983 gtk_misc_set_alignment(GTK_MISC(label), 1, .5); \
984 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT); \
985 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, row, row + 1);
987 #define VALUE(label, row) \
988 gtk_misc_set_alignment(GTK_MISC(label), 0, .5); \
989 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, row, row + 1);
991 static void show_file_info(gpointer data, guint action, GtkWidget *widget)
993 GtkWidget *window, *table, *label, *button, *frame, *value;
994 GtkWidget *file_label;
995 GString *gstring;
996 int file_data[2];
997 guchar *path, *tmp;
998 char *argv[] = {"file", "-b", NULL, NULL};
999 Collection *collection;
1000 DirItem *file;
1001 struct stat info;
1002 FileStatus *fs = NULL;
1003 guint perm;
1005 g_return_if_fail(window_with_focus != NULL);
1007 collection = window_with_focus->collection;
1008 if (collection->number_selected > 1)
1010 report_error(PROJECT, _("You cannot do this to more than "
1011 "one item at a time"));
1012 return;
1014 else if (collection->number_selected != 1)
1016 collection_target(collection, target_callback, show_file_info);
1017 return;
1019 file = selected_item(collection);
1020 path = make_path(window_with_focus->path,
1021 file->leafname)->str;
1022 if (lstat(path, &info))
1024 delayed_error(PROJECT, g_strerror(errno));
1025 return;
1028 gstring = g_string_new(NULL);
1030 window = gtk_window_new(GTK_WINDOW_DIALOG);
1031 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
1032 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
1033 gtk_window_set_title(GTK_WINDOW(window), path);
1035 table = gtk_table_new(10, 2, FALSE);
1036 gtk_container_add(GTK_CONTAINER(window), table);
1037 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
1038 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
1040 value = gtk_label_new(file->leafname);
1041 LABEL(_("Name:"), 0);
1042 VALUE(value, 0);
1044 g_string_sprintf(gstring, "%s, %s", user_name(info.st_uid),
1045 group_name(info.st_gid));
1046 value = gtk_label_new(gstring->str);
1047 LABEL(_("Owner, Group:"), 1);
1048 VALUE(value, 1);
1050 if (info.st_size >= PRETTY_SIZE_LIMIT)
1052 g_string_sprintf(gstring, "%s (%ld %s)",
1053 format_size((unsigned long) info.st_size),
1054 (unsigned long) info.st_size, _("bytes"));
1056 else
1058 g_string_assign(gstring,
1059 format_size((unsigned long) info.st_size));
1061 value = gtk_label_new(gstring->str);
1062 LABEL(_("Size:"), 2);
1063 VALUE(value, 2);
1065 value = gtk_label_new(pretty_time(&info.st_ctime));
1066 LABEL(_("Change time:"), 3);
1067 VALUE(value, 3);
1069 value = gtk_label_new(pretty_time(&info.st_mtime));
1070 LABEL(_("Modify time:"), 4);
1071 VALUE(value, 4);
1073 value = gtk_label_new(pretty_time(&info.st_atime));
1074 LABEL(_("Access time:"), 5);
1075 VALUE(value, 5);
1077 value = gtk_label_new(pretty_permissions(info.st_mode));
1078 perm = applicable(info.st_uid, info.st_gid);
1079 gtk_label_set_pattern(GTK_LABEL(value),
1080 perm == 0 ? "___ " :
1081 perm == 1 ? " ___ " :
1082 " ___");
1083 gtk_widget_set_style(value, fixed_style);
1084 LABEL(_("Permissions:"), 6);
1085 VALUE(value, 6);
1087 tmp = pretty_type(file, path);
1088 value = gtk_label_new(tmp);
1089 g_free(tmp);
1090 LABEL(_("Type:"), 7);
1091 VALUE(value, 7);
1093 frame = gtk_frame_new(_("file(1) says..."));
1094 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 2, 8, 9);
1095 file_label = gtk_label_new(_("<nothing yet>"));
1096 gtk_misc_set_padding(GTK_MISC(file_label), 4, 4);
1097 gtk_label_set_line_wrap(GTK_LABEL(file_label), TRUE);
1098 gtk_container_add(GTK_CONTAINER(frame), file_label);
1100 button = gtk_button_new_with_label(_("OK"));
1101 gtk_window_set_focus(GTK_WINDOW(window), button);
1102 gtk_table_attach(GTK_TABLE(table), button, 0, 2, 10, 11,
1103 GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 40, 4);
1104 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
1105 gtk_widget_destroy, GTK_OBJECT(window));
1107 gtk_widget_show_all(window);
1108 gdk_flush();
1110 if (pipe(file_data))
1112 g_string_sprintf(gstring, "pipe(): %s", g_strerror(errno));
1113 g_string_free(gstring, TRUE);
1114 return;
1116 switch (fork())
1118 case -1:
1119 g_string_sprintf(gstring, "fork(): %s",
1120 g_strerror(errno));
1121 gtk_label_set_text(GTK_LABEL(file_label), gstring->str);
1122 g_string_free(gstring, TRUE);
1123 close(file_data[0]);
1124 close(file_data[1]);
1125 break;
1126 case 0:
1127 /* We are the child */
1128 close(file_data[0]);
1129 dup2(file_data[1], STDOUT_FILENO);
1130 dup2(file_data[1], STDERR_FILENO);
1131 #ifdef FILE_B_FLAG
1132 argv[2] = path;
1133 #else
1134 argv[1] = file->leafname;
1135 chdir(window_with_focus->path);
1136 #endif
1137 if (execvp(argv[0], argv))
1138 fprintf(stderr, "execvp() error: %s\n",
1139 g_strerror(errno));
1140 _exit(0);
1141 default:
1142 /* We are the parent */
1143 close(file_data[1]);
1144 fs = g_new(FileStatus, 1);
1145 fs->label = GTK_LABEL(file_label);
1146 fs->fd = file_data[0];
1147 fs->start = TRUE;
1148 fs->input = gdk_input_add(fs->fd, GDK_INPUT_READ,
1149 (GdkInputFunction) add_file_output, fs);
1150 gtk_signal_connect(GTK_OBJECT(window), "destroy",
1151 GTK_SIGNAL_FUNC(file_info_destroyed), fs);
1152 g_string_free(gstring, TRUE);
1153 break;
1157 static void app_show_help(char *path)
1159 char *help_dir;
1160 struct stat info;
1162 help_dir = g_strconcat(path, "/Help", NULL);
1164 if (mc_stat(help_dir, &info))
1165 delayed_error(_("Application"),
1166 _("This is an application directory - you can "
1167 "run it as a program, or open it (hold down "
1168 "Shift while you open it). Most applications provide "
1169 "their own help here, but this one doesn't."));
1170 else
1171 filer_opendir(help_dir, PANEL_NO);
1174 static void help(gpointer data, guint action, GtkWidget *widget)
1176 Collection *collection;
1177 DirItem *item;
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)
1190 collection_target(collection, target_callback, help);
1191 return;
1193 item = selected_item(collection);
1194 switch (item->base_type)
1196 case TYPE_FILE:
1197 if (item->flags & ITEM_FLAG_EXEC_FILE)
1198 delayed_error(_("Executable file"),
1199 _("This is a file with an eXecute bit "
1200 "set - it can be run as a program."));
1201 else
1202 delayed_error(_("File"), _(
1203 "This is a data file. Try using the "
1204 "Info menu item to find out more..."));
1205 break;
1206 case TYPE_DIRECTORY:
1207 if (item->flags & ITEM_FLAG_APPDIR)
1208 app_show_help(
1209 make_path(window_with_focus->path,
1210 item->leafname)->str);
1211 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
1212 delayed_error(_("Mount point"), _(
1213 "A mount point is a directory which another "
1214 "filing system can be mounted on. Everything "
1215 "on the mounted filesystem then appears to be "
1216 "inside the directory."));
1217 else
1218 delayed_error(_("Directory"), _(
1219 "This is a directory. It contains an index to "
1220 "other items - open it to see the list."));
1221 break;
1222 case TYPE_CHAR_DEVICE:
1223 case TYPE_BLOCK_DEVICE:
1224 delayed_error(_("Device file"), _(
1225 "Device files allow you to read from or write "
1226 "to a device driver as though it was an "
1227 "ordinary file."));
1228 break;
1229 case TYPE_PIPE:
1230 delayed_error(_("Named pipe"), _(
1231 "Pipes allow different programs to "
1232 "communicate. One program writes data to the "
1233 "pipe while another one reads it out again."));
1234 break;
1235 case TYPE_SOCKET:
1236 delayed_error(_("Socket"), _(
1237 "Sockets allow processes to communicate."));
1238 break;
1239 default:
1240 delayed_error(_("Unknown type"), _(
1241 "I couldn't find out what kind of file this "
1242 "is. Maybe it doesn't exist anymore or you "
1243 "don't have search permission on the directory "
1244 "it's in?"));
1245 break;
1249 #define OPEN_VFS(fs) \
1250 static void open_vfs_ ## fs (gpointer data, guint action, GtkWidget *widget) \
1252 Collection *collection; \
1254 g_return_if_fail(window_with_focus != NULL); \
1256 collection = window_with_focus->collection; \
1257 if (collection->number_selected < 1) \
1258 collection_target(collection, target_callback, \
1259 open_vfs_ ## fs); \
1260 else \
1261 real_vfs_open(#fs); \
1264 static void real_vfs_open(char *fs)
1266 gchar *path;
1267 DirItem *item;
1269 if (window_with_focus->collection->number_selected != 1)
1271 report_error(PROJECT, _("You must select a single file "
1272 "to open as a Virtual File System"));
1273 return;
1276 item = selected_item(window_with_focus->collection);
1278 path = g_strconcat(window_with_focus->path,
1279 "/",
1280 item->leafname,
1281 "#", fs, NULL);
1283 filer_change_to(window_with_focus, path, NULL);
1284 g_free(path);
1287 OPEN_VFS(rpm)
1288 OPEN_VFS(utar)
1289 OPEN_VFS(uzip)
1291 static void select_all(gpointer data, guint action, GtkWidget *widget)
1293 g_return_if_fail(window_with_focus != NULL);
1295 collection_select_all(window_with_focus->collection);
1296 window_with_focus->temp_item_selected = FALSE;
1299 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1301 g_return_if_fail(window_with_focus != NULL);
1303 collection_clear_selection(window_with_focus->collection);
1304 window_with_focus->temp_item_selected = FALSE;
1307 static void show_options(gpointer data, guint action, GtkWidget *widget)
1309 g_return_if_fail(window_with_focus != NULL);
1311 options_show(window_with_focus);
1314 static gboolean new_directory_cb(guchar *initial, guchar *path)
1316 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1318 report_error("mkdir", g_strerror(errno));
1319 return FALSE;
1321 return TRUE;
1324 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1326 g_return_if_fail(window_with_focus != NULL);
1328 savebox_show(_("New Directory"),
1329 make_path(window_with_focus->path, _("NewDir"))->str,
1330 type_to_icon(&special_directory),
1331 new_directory_cb);
1334 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1336 char *argv[] = {NULL, NULL};
1338 argv[0] = xterm_here_value;
1340 g_return_if_fail(window_with_focus != NULL);
1342 if (!spawn_full(argv, window_with_focus->path))
1343 report_error(PROJECT, "Failed to fork() child "
1344 "process");
1347 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1349 g_return_if_fail(window_with_focus != NULL);
1351 filer_open_parent(window_with_focus);
1354 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1356 g_return_if_fail(window_with_focus != NULL);
1358 change_to_parent(window_with_focus);
1361 static void new_window(gpointer data, guint action, GtkWidget *widget)
1363 g_return_if_fail(window_with_focus != NULL);
1365 if (o_unique_filer_windows)
1366 report_error(PROJECT, _("You can't open a second view onto "
1367 "this directory because the `Unique Windows' option "
1368 "is turned on in the Options window."));
1369 else
1370 filer_opendir(window_with_focus->path, PANEL_NO);
1373 static void close_window(gpointer data, guint action, GtkWidget *widget)
1375 g_return_if_fail(window_with_focus != NULL);
1377 gtk_widget_destroy(window_with_focus->window);
1380 static void enter_path(gpointer data, guint action, GtkWidget *widget)
1382 g_return_if_fail(window_with_focus != NULL);
1384 minibuffer_show(window_with_focus, MINI_PATH);
1387 static void shell_command(gpointer data, guint action, GtkWidget *widget)
1389 g_return_if_fail(window_with_focus != NULL);
1391 minibuffer_show(window_with_focus, MINI_SHELL);
1394 static void run_action(gpointer data, guint action, GtkWidget *widget)
1396 g_return_if_fail(window_with_focus != NULL);
1398 minibuffer_show(window_with_focus, MINI_RUN_ACTION);
1401 static void select_if(gpointer data, guint action, GtkWidget *widget)
1403 g_return_if_fail(window_with_focus != NULL);
1405 minibuffer_show(window_with_focus, MINI_SELECT_IF);
1408 static void rox_help(gpointer data, guint action, GtkWidget *widget)
1410 g_return_if_fail(window_with_focus != NULL);
1412 filer_opendir(make_path(getenv("APP_DIR"), "Help")->str, PANEL_NO);
1415 static void open_as_dir(gpointer data, guint action, GtkWidget *widget)
1417 g_return_if_fail(window_with_focus != NULL);
1419 filer_opendir(window_with_focus->path, PANEL_NO);
1422 static void close_panel(gpointer data, guint action, GtkWidget *widget)
1424 g_return_if_fail(window_with_focus != NULL);
1426 gtk_widget_destroy(window_with_focus->window);