r173: Finished the minibuffer code. It's nice!
[rox-filer.git] / ROX-Filer / src / menu.c
blob9c160888ce4d43e1fee9f8f76e61ba587028a3d2
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 1999, 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 <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <time.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 "savebox.h"
48 #include "mount.h"
49 #include "minibuffer.h"
51 #define C_ "<control>"
53 #define MENU_MARGIN 32
55 GtkAccelGroup *filer_keys;
56 GtkAccelGroup *panel_keys;
58 static GtkWidget *popup_menu = NULL; /* Currently open menu */
60 /* Options */
61 static GtkWidget *xterm_here_entry;
62 static char *xterm_here_value;
64 /* Static prototypes */
66 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data);
67 static void menu_closed(GtkWidget *widget);
68 static void items_sensitive(GtkWidget *menu, int from, int n, gboolean state);
69 static char *load_xterm_here(char *data);
71 /* Note that for these callbacks none of the arguments are used. */
72 static void not_yet(gpointer data, guint action, GtkWidget *widget);
74 static void large(gpointer data, guint action, GtkWidget *widget);
75 static void small(gpointer data, guint action, GtkWidget *widget);
76 static void full_info(gpointer data, guint action, GtkWidget *widget);
78 static void sort_name(gpointer data, guint action, GtkWidget *widget);
79 static void sort_type(gpointer data, guint action, GtkWidget *widget);
80 static void sort_size(gpointer data, guint action, GtkWidget *widget);
81 static void sort_date(gpointer data, guint action, GtkWidget *widget);
83 static void hidden(gpointer data, guint action, GtkWidget *widget);
84 static void refresh(gpointer data, guint action, GtkWidget *widget);
86 static void copy_item(gpointer data, guint action, GtkWidget *widget);
87 static void rename_item(gpointer data, guint action, GtkWidget *widget);
88 static void link_item(gpointer data, guint action, GtkWidget *widget);
89 static void open_file(gpointer data, guint action, GtkWidget *widget);
90 static void help(gpointer data, guint action, GtkWidget *widget);
91 static void show_file_info(gpointer data, guint action, GtkWidget *widget);
92 static void mount(gpointer data, guint action, GtkWidget *widget);
93 static void delete(gpointer data, guint action, GtkWidget *widget);
94 static void usage(gpointer data, guint action, GtkWidget *widget);
96 static void select_all(gpointer data, guint action, GtkWidget *widget);
97 static void clear_selection(gpointer data, guint action, GtkWidget *widget);
98 static void show_options(gpointer data, guint action, GtkWidget *widget);
99 static void new_directory(gpointer data, guint action, GtkWidget *widget);
100 static void xterm_here(gpointer data, guint action, GtkWidget *widget);
102 static void open_parent_same(gpointer data, guint action, GtkWidget *widget);
103 static void open_parent(gpointer data, guint action, GtkWidget *widget);
104 static void new_window(gpointer data, guint action, GtkWidget *widget);
105 static void close_window(gpointer data, guint action, GtkWidget *widget);
106 static void enter_path(gpointer data, guint action, GtkWidget *widget);
107 static void rox_help(gpointer data, guint action, GtkWidget *widget);
109 static void open_as_dir(gpointer data, guint action, GtkWidget *widget);
110 static void close_panel(gpointer data, guint action, GtkWidget *widget);
112 static GtkWidget *create_options();
113 static void update_options();
114 static void set_options();
115 static void save_options();
117 static OptionsSection options =
119 "Menu options",
120 create_options,
121 update_options,
122 set_options,
123 save_options
127 static GtkWidget *filer_menu; /* The popup filer menu */
128 static GtkWidget *filer_file_item; /* The File '' label */
129 static GtkWidget *filer_file_menu; /* The File '' menu */
130 static GtkWidget *filer_hidden_menu; /* The Show Hidden item */
131 static GtkWidget *panel_menu; /* The popup panel menu */
132 static GtkWidget *panel_file_item; /* The File '' label */
133 static GtkWidget *panel_file_menu; /* The File '' menu */
134 static GtkWidget *panel_hidden_menu; /* The Show Hidden item */
136 static gint screen_width, screen_height;
138 static GtkItemFactoryEntry filer_menu_def[] = {
139 {"/Display", NULL, NULL, 0, "<Branch>"},
140 {"/Display/Large Icons", NULL, large, 0, "<RadioItem>"},
141 {"/Display/Small Icons", NULL, small, 0, "/Display/Large Icons"},
142 {"/Display/Full Info", NULL, full_info, 0, "/Display/Large Icons"},
143 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
144 {"/Display/Sort by Name", NULL, sort_name, 0, "<RadioItem>"},
145 {"/Display/Sort by Type", NULL, sort_type, 0, "/Display/Sort by Name"},
146 {"/Display/Sort by Date", NULL, sort_date, 0, "/Display/Sort by Name"},
147 {"/Display/Sort by Size", NULL, sort_size, 0, "/Display/Sort by Name"},
148 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
149 {"/Display/Show Hidden", C_"H", hidden, 0, "<ToggleItem>"},
150 {"/Display/Refresh", C_"L", refresh, 0, NULL},
151 {"/File", NULL, NULL, 0, "<Branch>"},
152 {"/File/Copy...", NULL, copy_item, 0, NULL},
153 {"/File/Rename...", NULL, rename_item, 0, NULL},
154 {"/File/Link...", NULL, link_item, 0, NULL},
155 {"/File/Shift Open", NULL, open_file, 0, NULL},
156 {"/File/Help", "F1", help, 0, NULL},
157 {"/File/Info", "I", show_file_info, 0, NULL},
158 {"/File/Separator", NULL, NULL, 0, "<Separator>"},
159 {"/File/Mount", "M", mount, 0, NULL},
160 {"/File/Delete", C_"X", delete, 0, NULL},
161 {"/File/Disk Usage", "U", usage, 0, NULL},
162 {"/File/Permissions", NULL, not_yet, 0, NULL},
163 {"/File/Touch", NULL, not_yet, 0, NULL},
164 {"/File/Find", NULL, not_yet, 0, NULL},
165 {"/Select All", C_"A", select_all, 0, NULL},
166 {"/Clear Selection", C_"Z", clear_selection, 0, NULL},
167 {"/Options...", NULL, show_options, 0, NULL},
168 {"/New Directory...", NULL, new_directory, 0, NULL},
169 {"/Xterm Here", NULL, xterm_here, 0, NULL},
170 {"/Window", NULL, NULL, 0, "<Branch>"},
171 {"/Window/Parent, New Window", NULL, open_parent, 0, NULL},
172 {"/Window/Parent, Same Window", NULL, open_parent_same, 0, NULL},
173 {"/Window/New Window", NULL, new_window, 0, NULL},
174 {"/Window/Close Window", C_"Q", close_window, 0, NULL},
175 {"/Window/Enter path", NULL, enter_path, 0, NULL},
176 {"/Window/Separator", NULL, NULL, 0, "<Separator>"},
177 {"/Window/Show ROX-Filer help", NULL, rox_help, 0, NULL},
180 static GtkItemFactoryEntry panel_menu_def[] = {
181 {"/Display", NULL, NULL, 0, "<Branch>"},
182 {"/Display/Large Icons", NULL, large, 0, "<RadioItem>"},
183 {"/Display/Small Icons", NULL, small, 0, "/Display/Large Icons"},
184 {"/Display/Full Info", NULL, full_info, 0, "/Display/Large Icons"},
185 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
186 {"/Display/Sort by Name", NULL, sort_name, 0, "<RadioItem>"},
187 {"/Display/Sort by Type", NULL, sort_type, 0, "/Display/Sort by Name"},
188 {"/Display/Sort by Date", NULL, sort_date, 0, "/Display/Sort by Name"},
189 {"/Display/Sort by Size", NULL, sort_size, 0, "/Display/Sort by Name"},
190 {"/Display/Separator", NULL, NULL, 0, "<Separator>"},
191 {"/Display/Show Hidden", NULL, hidden, 0, "<ToggleItem>"},
192 {"/Display/Refresh", NULL, refresh, 0, NULL},
193 {"/File", NULL, NULL, 0, "<Branch>"},
194 {"/File/Help", NULL, help, 0, NULL},
195 {"/File/Info", NULL, show_file_info, 0, NULL},
196 {"/File/Delete", NULL, delete, 0, NULL},
197 {"/Open as directory", NULL, open_as_dir, 0, NULL},
198 {"/Close panel", NULL, close_panel, 0, NULL},
199 {"/Separator", NULL, NULL, 0, "<Separator>"},
200 {"/Show ROX-Filer help", NULL, rox_help, 0, NULL},
203 typedef struct _FileStatus FileStatus;
205 /* This is for the 'file(1) says...' thing */
206 struct _FileStatus
208 int fd; /* FD to read from, -1 if closed */
209 int input; /* Input watcher tag if fd valid */
210 GtkLabel *label; /* Widget to output to */
211 gboolean start; /* No output yet */
214 void menu_init()
216 GtkItemFactory *item_factory;
217 char *menurc;
218 GList *items;
220 /* This call starts returning strange values after a while, so get
221 * the result here during init.
223 gdk_window_get_size(GDK_ROOT_PARENT(), &screen_width, &screen_height);
225 filer_keys = gtk_accel_group_new();
226 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
227 "<filer>",
228 filer_keys);
229 gtk_item_factory_create_items(item_factory,
230 sizeof(filer_menu_def) / sizeof(*filer_menu_def),
231 filer_menu_def,
232 NULL);
233 filer_menu = gtk_item_factory_get_widget(item_factory, "<filer>");
234 filer_file_menu = gtk_item_factory_get_widget(item_factory,
235 "<filer>/File");
236 filer_hidden_menu = gtk_item_factory_get_widget(item_factory,
237 "<filer>/Display/Show Hidden");
238 items = gtk_container_children(GTK_CONTAINER(filer_menu));
239 filer_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
240 g_list_free(items);
242 panel_keys = gtk_accel_group_new();
243 item_factory = gtk_item_factory_new(GTK_TYPE_MENU,
244 "<panel>",
245 panel_keys);
246 gtk_item_factory_create_items(item_factory,
247 sizeof(panel_menu_def) / sizeof(*panel_menu_def),
248 panel_menu_def,
249 NULL);
250 panel_menu = gtk_item_factory_get_widget(item_factory, "<panel>");
251 panel_file_menu = gtk_item_factory_get_widget(item_factory,
252 "<panel>/File");
253 panel_hidden_menu = gtk_item_factory_get_widget(item_factory,
254 "<panel>/Display/Show Hidden");
255 items = gtk_container_children(GTK_CONTAINER(panel_menu));
256 panel_file_item = GTK_BIN(g_list_nth(items, 1)->data)->child;
257 g_list_free(items);
259 menurc = choices_find_path_load("menus");
260 if (menurc)
261 gtk_item_factory_parse_rc(menurc);
263 gtk_accel_group_lock(panel_keys);
265 gtk_signal_connect(GTK_OBJECT(filer_menu), "unmap_event",
266 GTK_SIGNAL_FUNC(menu_closed), NULL);
267 gtk_signal_connect(GTK_OBJECT(panel_menu), "unmap_event",
268 GTK_SIGNAL_FUNC(menu_closed), NULL);
269 gtk_signal_connect(GTK_OBJECT(filer_file_menu), "unmap_event",
270 GTK_SIGNAL_FUNC(menu_closed), NULL);
272 options_sections = g_slist_prepend(options_sections, &options);
273 xterm_here_value = g_strdup("xterm");
274 option_register("xterm_here", load_xterm_here);
277 /* Build up some option widgets to go in the options dialog, but don't
278 * fill them in yet.
280 static GtkWidget *create_options()
282 GtkWidget *table, *label;
284 table = gtk_table_new(2, 2, FALSE);
285 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
287 label = gtk_label_new("To set the keyboard short-cuts you simply open "
288 "the menu over a filer window, move the pointer over "
289 "the item you want to use and press a key. The key "
290 "will appear next to the menu item and you can just "
291 "press that key without opening the menu in future. "
292 "To save the current menu short-cuts for next time, "
293 "click the Save button at the bottom of this window.");
294 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
295 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 2, 0, 1);
297 label = gtk_label_new("'Xterm here' program:");
298 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
299 xterm_here_entry = gtk_entry_new();
300 gtk_table_attach_defaults(GTK_TABLE(table), xterm_here_entry,
301 1, 2, 1, 2);
303 return table;
306 static char *load_xterm_here(char *data)
308 g_free(xterm_here_value);
309 xterm_here_value = g_strdup(data);
310 return NULL;
313 static void update_options()
315 gtk_entry_set_text(GTK_ENTRY(xterm_here_entry), xterm_here_value);
318 static void set_options()
320 g_free(xterm_here_value);
321 xterm_here_value = g_strdup(gtk_entry_get_text(
322 GTK_ENTRY(xterm_here_entry)));
325 static void save_options()
327 char *menurc;
329 menurc = choices_find_path_save("menus", TRUE);
330 if (menurc)
331 gtk_item_factory_dump_rc(menurc, NULL, TRUE);
333 option_write("xterm_here", xterm_here_value);
337 static void items_sensitive(GtkWidget *menu, int from, int n, gboolean state)
339 GList *items, *item;
341 items = gtk_container_children(GTK_CONTAINER(menu));
343 item = g_list_nth(items, from);
344 while (item && n--)
346 gtk_widget_set_sensitive(GTK_BIN(item->data)->child, state);
347 item = item->next;
350 g_list_free(items);
353 static void position_menu(GtkMenu *menu, gint *x, gint *y, gpointer data)
355 int *pos = (int *) data;
356 GtkRequisition requisition;
358 gtk_widget_size_request(GTK_WIDGET(menu), &requisition);
360 if (pos[0] == -1)
361 *x = screen_width - MENU_MARGIN - requisition.width;
362 else if (pos[0] == -2)
363 *x = MENU_MARGIN;
364 else
365 *x = pos[0] - (requisition.width >> 2);
367 if (pos[1] == -1)
368 *y = screen_height - MENU_MARGIN - requisition.height;
369 else if (pos[1] == -2)
370 *y = MENU_MARGIN;
371 else
372 *y = pos[1] - (requisition.height >> 2);
374 *x = CLAMP(*x, 0, screen_width - requisition.width);
375 *y = CLAMP(*y, 0, screen_height - requisition.height);
378 void show_filer_menu(FilerWindow *filer_window, GdkEventButton *event,
379 int item)
381 GString *buffer;
382 GtkWidget *file_label, *file_menu;
383 DirItem *file_item;
384 int pos[2];
386 pos[0] = event->x_root;
387 pos[1] = event->y_root;
389 window_with_focus = filer_window;
391 if (filer_window->panel)
393 switch (filer_window->panel_side)
395 case TOP: pos[1] = -2; break;
396 case BOTTOM: pos[1] = -1; break;
397 case LEFT: pos[0] = -2; break;
398 case RIGHT: pos[0] = -1; break;
402 if (filer_window->panel)
403 collection_clear_selection(filer_window->collection); /* ??? */
405 if (filer_window->collection->number_selected == 0 && item >= 0)
407 collection_select_item(filer_window->collection, item);
408 filer_window->temp_item_selected = TRUE;
410 else
411 filer_window->temp_item_selected = FALSE;
413 if (filer_window->panel)
415 file_label = panel_file_item;
416 file_menu = panel_file_menu;
418 gtk_check_menu_item_set_active(
419 GTK_CHECK_MENU_ITEM(panel_hidden_menu),
420 filer_window->show_hidden);
422 else
424 file_label = filer_file_item;
425 file_menu = filer_file_menu;
427 gtk_check_menu_item_set_active(
428 GTK_CHECK_MENU_ITEM(filer_hidden_menu),
429 filer_window->show_hidden);
432 buffer = g_string_new(NULL);
433 switch (filer_window->collection->number_selected)
435 case 0:
436 g_string_assign(buffer, "Next click");
437 items_sensitive(file_menu, 0, 6, TRUE);
438 break;
439 case 1:
440 items_sensitive(file_menu, 0, 6, TRUE);
441 file_item = selected_item(filer_window->collection);
442 g_string_sprintf(buffer, "%s '%s'",
443 basetype_name(file_item),
444 file_item->leafname);
445 break;
446 default:
447 items_sensitive(file_menu, 0, 6, FALSE);
448 g_string_sprintf(buffer, "%d items",
449 filer_window->collection->number_selected);
450 break;
453 gtk_label_set_text(GTK_LABEL(file_label), buffer->str);
455 g_string_free(buffer, TRUE);
457 if (filer_window->panel)
458 popup_menu = panel_menu;
459 else
460 popup_menu = (event->state & GDK_CONTROL_MASK)
461 ? filer_file_menu
462 : filer_menu;
464 gtk_menu_popup(GTK_MENU(popup_menu), NULL, NULL, position_menu,
465 (gpointer) pos, event->button, event->time);
468 static void menu_closed(GtkWidget *widget)
470 if (window_with_focus == NULL || widget != popup_menu)
471 return; /* Close panel item chosen? */
473 if (window_with_focus->temp_item_selected)
475 collection_clear_selection(window_with_focus->collection);
476 window_with_focus->temp_item_selected = FALSE;
480 void target_callback(Collection *collection, gint item, gpointer real_fn)
482 g_return_if_fail(window_with_focus != NULL);
483 g_return_if_fail(window_with_focus->collection == collection);
484 g_return_if_fail(real_fn != NULL);
486 collection_wink_item(collection, item);
487 collection_clear_selection(collection);
488 collection_select_item(collection, item);
489 ((CollectionTargetFunc)real_fn)(NULL, 0, collection);
490 if (item < collection->number_of_items)
491 collection_unselect_item(collection, item);
494 /* Actions */
496 /* Fake action to warn when a menu item does nothing */
497 static void not_yet(gpointer data, guint action, GtkWidget *widget)
499 delayed_error("ROX-Filer", "Sorry, that feature isn't implemented yet");
502 static void large(gpointer data, guint action, GtkWidget *widget)
504 g_return_if_fail(window_with_focus != NULL);
506 filer_style_set(window_with_focus, LARGE_ICONS);
509 static void small(gpointer data, guint action, GtkWidget *widget)
511 g_return_if_fail(window_with_focus != NULL);
513 filer_style_set(window_with_focus, SMALL_ICONS);
516 static void full_info(gpointer data, guint action, GtkWidget *widget)
518 g_return_if_fail(window_with_focus != NULL);
520 filer_style_set(window_with_focus, FULL_INFO);
523 static void sort_name(gpointer data, guint action, GtkWidget *widget)
525 g_return_if_fail(window_with_focus != NULL);
527 filer_set_sort_fn(window_with_focus, sort_by_name);
530 static void sort_type(gpointer data, guint action, GtkWidget *widget)
532 g_return_if_fail(window_with_focus != NULL);
534 filer_set_sort_fn(window_with_focus, sort_by_type);
537 static void sort_date(gpointer data, guint action, GtkWidget *widget)
539 g_return_if_fail(window_with_focus != NULL);
541 filer_set_sort_fn(window_with_focus, sort_by_date);
544 static void sort_size(gpointer data, guint action, GtkWidget *widget)
546 g_return_if_fail(window_with_focus != NULL);
548 filer_set_sort_fn(window_with_focus, sort_by_size);
551 static void hidden(gpointer data, guint action, GtkWidget *widget)
553 gboolean new;
554 GtkWidget *item;
556 g_return_if_fail(window_with_focus != NULL);
558 item = window_with_focus->panel ? panel_hidden_menu : filer_hidden_menu;
559 new = GTK_CHECK_MENU_ITEM(item)->active;
561 filer_set_hidden(window_with_focus, new);
564 static void refresh(gpointer data, guint action, GtkWidget *widget)
566 g_return_if_fail(window_with_focus != NULL);
568 full_refresh();
569 update_dir(window_with_focus, TRUE);
572 static void mount(gpointer data, guint action, GtkWidget *widget)
574 g_return_if_fail(window_with_focus != NULL);
576 if (window_with_focus->collection->number_selected == 0)
577 collection_target(window_with_focus->collection,
578 target_callback, mount);
579 else
580 action_mount(window_with_focus, NULL);
583 static void delete(gpointer data, guint action, GtkWidget *widget)
585 g_return_if_fail(window_with_focus != NULL);
587 if (window_with_focus->collection->number_selected == 0)
588 collection_target(window_with_focus->collection,
589 target_callback, delete);
590 else
591 action_delete(window_with_focus);
594 static void usage(gpointer data, guint action, GtkWidget *widget)
596 g_return_if_fail(window_with_focus != NULL);
598 if (window_with_focus->collection->number_selected == 0)
599 collection_target(window_with_focus->collection,
600 target_callback, usage);
601 else
602 action_usage(window_with_focus);
605 static gboolean copy_cb(char *initial, char *path)
607 char *new_dir, *slash;
608 int len;
609 GString *command;
610 gboolean retval = TRUE;
612 slash = strrchr(path, '/');
613 if (!slash)
615 report_error("ROX-Filer", "Missing '/' in new pathname");
616 return FALSE;
619 if (access(path, F_OK) == 0)
621 report_error("ROX-Filer",
622 "An item with this name already exists");
623 return FALSE;
626 len = slash - path;
627 new_dir = g_malloc(len + 1);
628 memcpy(new_dir, path, len);
629 new_dir[len] = '\0';
631 command = g_string_new(NULL);
632 g_string_sprintf(command, "cp -a %s %s", initial, path);
634 if (system(command->str))
636 g_string_append(command, " failed!");
637 report_error("ROX-Filer", command->str);
638 retval = FALSE;
641 g_string_free(command, TRUE);
643 refresh_dirs(new_dir);
644 return retval;
647 static void copy_item(gpointer data, guint action, GtkWidget *widget)
649 Collection *collection;
651 g_return_if_fail(window_with_focus != NULL);
653 collection = window_with_focus->collection;
654 if (collection->number_selected > 1)
656 report_error("ROX-Filer", "You cannot do this to more than "
657 "one item at a time");
658 return;
660 else if (collection->number_selected != 1)
661 collection_target(collection, target_callback, copy_item);
662 else
664 DirItem *item = selected_item(collection);
666 savebox_show(window_with_focus, "Copy",
667 window_with_focus->path,
668 item->leafname,
669 item->image, copy_cb);
673 static gboolean rename_cb(char *initial, char *path)
675 if (rename(initial, path))
677 report_error("ROX-Filer: rename()", g_strerror(errno));
678 return FALSE;
680 return TRUE;
683 static void rename_item(gpointer data, guint action, GtkWidget *widget)
685 Collection *collection;
687 g_return_if_fail(window_with_focus != NULL);
689 collection = window_with_focus->collection;
690 if (collection->number_selected > 1)
692 report_error("ROX-Filer", "You cannot do this to more than "
693 "one item at a time");
694 return;
696 else if (collection->number_selected != 1)
697 collection_target(collection, target_callback, rename_item);
698 else
700 DirItem *item = selected_item(collection);
702 savebox_show(window_with_focus, "Rename",
703 window_with_focus->path,
704 item->leafname,
705 item->image, rename_cb);
709 static gboolean link_cb(char *initial, char *path)
711 if (symlink(initial, path))
713 report_error("ROX-Filer: symlink()", g_strerror(errno));
714 return FALSE;
716 return TRUE;
719 static void link_item(gpointer data, guint action, GtkWidget *widget)
721 Collection *collection;
723 g_return_if_fail(window_with_focus != NULL);
725 collection = window_with_focus->collection;
726 if (collection->number_selected > 1)
728 report_error("ROX-Filer", "You cannot do this to more than "
729 "one item at a time");
730 return;
732 else if (collection->number_selected != 1)
733 collection_target(collection, target_callback, link_item);
734 else
736 DirItem *item = selected_item(collection);
738 savebox_show(window_with_focus, "Symlink",
739 window_with_focus->path,
740 item->leafname,
741 item->image, link_cb);
745 static void open_file(gpointer data, guint action, GtkWidget *widget)
747 Collection *collection;
749 g_return_if_fail(window_with_focus != NULL);
751 collection = window_with_focus->collection;
752 if (collection->number_selected > 1)
754 report_error("ROX-Filer", "You cannot do this to more than "
755 "one item at a time");
756 return;
758 else if (collection->number_selected != 1)
759 collection_target(collection, target_callback, open_file);
760 else
761 filer_openitem(window_with_focus,
762 selected_item_number(collection),
763 TRUE, FALSE);
766 /* Got some data from file(1) - stick it in the window. */
767 static void add_file_output(FileStatus *fs,
768 gint source, GdkInputCondition condition)
770 char buffer[20];
771 char *str;
772 int got;
774 got = read(source, buffer, sizeof(buffer) - 1);
775 if (got <= 0)
777 int err = errno;
778 gtk_input_remove(fs->input);
779 close(source);
780 fs->fd = -1;
781 if (got < 0)
782 delayed_error("ROX-Filer: file(1) says...",
783 g_strerror(err));
784 return;
786 buffer[got] = '\0';
788 if (fs->start)
790 str = "";
791 fs->start = FALSE;
793 else
794 gtk_label_get(fs->label, &str);
796 str = g_strconcat(str, buffer, NULL);
797 gtk_label_set_text(fs->label, str);
798 g_free(str);
801 static void file_info_destroyed(GtkWidget *widget, FileStatus *fs)
803 if (fs->fd != -1)
805 gtk_input_remove(fs->input);
806 close(fs->fd);
809 g_free(fs);
812 static void show_file_info(gpointer data, guint action, GtkWidget *widget)
814 GtkWidget *window, *table, *label, *button, *frame;
815 GtkWidget *file_label;
816 GString *gstring;
817 char *string;
818 int file_data[2];
819 char *path;
820 char *argv[] = {"file", "-b", NULL, NULL};
821 Collection *collection;
822 DirItem *file;
823 struct stat info;
824 FileStatus *fs = NULL;
826 g_return_if_fail(window_with_focus != NULL);
828 collection = window_with_focus->collection;
829 if (collection->number_selected > 1)
831 report_error("ROX-Filer", "You cannot do this to more than "
832 "one item at a time");
833 return;
835 else if (collection->number_selected != 1)
837 collection_target(collection, target_callback, show_file_info);
838 return;
840 file = selected_item(collection);
841 path = make_path(window_with_focus->path,
842 file->leafname)->str;
843 if (lstat(path, &info))
845 delayed_error("ROX-Filer", g_strerror(errno));
846 return;
849 gstring = g_string_new(NULL);
851 window = gtk_window_new(GTK_WINDOW_DIALOG);
852 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_MOUSE);
853 gtk_container_set_border_width(GTK_CONTAINER(window), 4);
854 gtk_window_set_title(GTK_WINDOW(window), path);
856 table = gtk_table_new(9, 2, FALSE);
857 gtk_container_add(GTK_CONTAINER(window), table);
858 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
859 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
861 label = gtk_label_new("Owner, group:");
862 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
863 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT);
864 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
865 g_string_sprintf(gstring, "%s, %s", user_name(info.st_uid),
866 group_name(info.st_gid));
867 label = gtk_label_new(gstring->str);
868 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
869 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 0, 1);
871 label = gtk_label_new("Size:");
872 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
873 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
874 if (info.st_size >=2048)
876 g_string_sprintf(gstring, "%s (%ld bytes)",
877 format_size((unsigned long) info.st_size),
878 (unsigned long) info.st_size);
880 else
882 g_string_sprintf(gstring, "%ld bytes",
883 (unsigned long) info.st_size);
885 label = gtk_label_new(gstring->str);
886 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
887 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 1, 2);
889 label = gtk_label_new("Change time:");
890 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
891 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2, 3);
892 g_string_sprintf(gstring, "%s", ctime(&info.st_ctime));
893 g_string_truncate(gstring, gstring->len - 1);
894 label = gtk_label_new(gstring->str);
895 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
896 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 2, 3);
898 label = gtk_label_new("Modify time:");
899 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
900 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 3, 4);
901 g_string_sprintf(gstring, "%s", ctime(&info.st_mtime));
902 g_string_truncate(gstring, gstring->len - 1);
903 label = gtk_label_new(gstring->str);
904 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
905 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 3, 4);
907 label = gtk_label_new("Access time:");
908 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
909 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 4, 5);
910 g_string_sprintf(gstring, "%s", ctime(&info.st_atime));
911 g_string_truncate(gstring, gstring->len - 1);
912 label = gtk_label_new(gstring->str);
913 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
914 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 4, 5);
916 label = gtk_label_new("Permissions:");
917 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
918 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 5, 6);
919 g_string_sprintf(gstring, "%o", (unsigned int) info.st_mode);
920 label = gtk_label_new(gstring->str);
921 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
922 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 5, 6);
924 label = gtk_label_new("MIME type:");
925 gtk_misc_set_alignment(GTK_MISC(label), 1, .5);
926 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 6, 7);
927 if (file->mime_type)
929 string = g_strconcat(file->mime_type->media_type, "/",
930 file->mime_type->subtype, NULL);
931 label = gtk_label_new(string);
932 g_free(string);
934 else
935 label = gtk_label_new("-");
936 gtk_misc_set_alignment(GTK_MISC(label), 0, .5);
937 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, 6, 7);
939 frame = gtk_frame_new("file(1) says...");
940 gtk_table_attach_defaults(GTK_TABLE(table), frame, 0, 2, 7, 8);
941 file_label = gtk_label_new("<nothing yet>");
942 gtk_misc_set_padding(GTK_MISC(file_label), 4, 4);
943 gtk_label_set_line_wrap(GTK_LABEL(file_label), TRUE);
944 gtk_container_add(GTK_CONTAINER(frame), file_label);
946 button = gtk_button_new_with_label("OK");
947 gtk_window_set_focus(GTK_WINDOW(window), button);
948 gtk_table_attach(GTK_TABLE(table), button, 0, 2, 8, 9,
949 GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 40, 4);
950 gtk_signal_connect_object(GTK_OBJECT(button), "clicked",
951 gtk_widget_destroy, GTK_OBJECT(window));
953 gtk_widget_show_all(window);
954 gdk_flush();
956 if (pipe(file_data))
958 g_string_sprintf(gstring, "pipe(): %s", g_strerror(errno));
959 g_string_free(gstring, TRUE);
960 return;
962 switch (fork())
964 case -1:
965 g_string_sprintf(gstring, "fork(): %s",
966 g_strerror(errno));
967 gtk_label_set_text(GTK_LABEL(file_label), gstring->str);
968 g_string_free(gstring, TRUE);
969 close(file_data[0]);
970 close(file_data[1]);
971 break;
972 case 0:
973 /* We are the child */
974 close(file_data[0]);
975 dup2(file_data[1], STDOUT_FILENO);
976 dup2(file_data[1], STDERR_FILENO);
977 #ifdef FILE_B_FLAG
978 argv[2] = path;
979 #else
980 argv[1] = path;
981 #endif
982 if (execvp(argv[0], argv))
983 fprintf(stderr, "execvp() error: %s\n",
984 g_strerror(errno));
985 _exit(0);
986 default:
987 /* We are the parent */
988 close(file_data[1]);
989 fs = g_new(FileStatus, 1);
990 fs->label = GTK_LABEL(file_label);
991 fs->fd = file_data[0];
992 fs->start = TRUE;
993 fs->input = gdk_input_add(fs->fd, GDK_INPUT_READ,
994 (GdkInputFunction) add_file_output, fs);
995 gtk_signal_connect(GTK_OBJECT(window), "destroy",
996 GTK_SIGNAL_FUNC(file_info_destroyed), fs);
997 g_string_free(gstring, TRUE);
998 break;
1002 static void app_show_help(char *path)
1004 char *help_dir;
1005 struct stat info;
1007 help_dir = g_strconcat(path, "/Help", NULL);
1009 if (stat(help_dir, &info))
1010 delayed_error("Application",
1011 "This is an application directory - you can "
1012 "run it as a program, or open it (hold down "
1013 "Shift while you open it). Most applications provide "
1014 "their own help here, but this one doesn't.");
1015 else
1016 filer_opendir(help_dir, FALSE, BOTTOM);
1019 static void help(gpointer data, guint action, GtkWidget *widget)
1021 Collection *collection;
1022 DirItem *item;
1024 g_return_if_fail(window_with_focus != NULL);
1026 collection = window_with_focus->collection;
1027 if (collection->number_selected > 1)
1029 report_error("ROX-Filer", "You cannot do this to more than "
1030 "one item at a time");
1031 return;
1033 else if (collection->number_selected != 1)
1035 collection_target(collection, target_callback, help);
1036 return;
1038 item = selected_item(collection);
1039 switch (item->base_type)
1041 case TYPE_FILE:
1042 if (item->flags & ITEM_FLAG_EXEC_FILE)
1043 delayed_error("Executable file",
1044 "This is a file with an eXecute bit "
1045 "set - it can be run as a program.");
1046 else
1047 delayed_error("File",
1048 "This is a data file. Try using the "
1049 "Info menu item to find out more...");
1050 break;
1051 case TYPE_DIRECTORY:
1052 if (item->flags & ITEM_FLAG_APPDIR)
1053 app_show_help(
1054 make_path(window_with_focus->path,
1055 item->leafname)->str);
1056 else if (item->flags & ITEM_FLAG_MOUNT_POINT)
1057 delayed_error("Mount point",
1058 "A mount point is a directory which another "
1059 "filing system can be mounted on. Everything "
1060 "on the mounted filesystem then appears to be "
1061 "inside the directory.");
1062 else
1063 delayed_error("Directory",
1064 "This is a directory. It contains an index to "
1065 "other items - open it to see the list.");
1066 break;
1067 case TYPE_CHAR_DEVICE:
1068 case TYPE_BLOCK_DEVICE:
1069 delayed_error("Device file",
1070 "Device files allow you to read from or write "
1071 "to a device driver as though it was an "
1072 "ordinary file.");
1073 break;
1074 case TYPE_PIPE:
1075 delayed_error("Named pipe",
1076 "Pipes allow different programs to "
1077 "communicate. One program writes data to the "
1078 "pipe while another one reads it out again.");
1079 break;
1080 case TYPE_SOCKET:
1081 delayed_error("Socket",
1082 "Sockets allow processes to communicate.");
1083 break;
1084 default:
1085 delayed_error("Unknown type",
1086 "I couldn't find out what kind of file this "
1087 "is. Maybe it doesn't exist anymore or you "
1088 "don't have search permission on the directory "
1089 "it's in?");
1090 break;
1094 static void select_all(gpointer data, guint action, GtkWidget *widget)
1096 g_return_if_fail(window_with_focus != NULL);
1098 collection_select_all(window_with_focus->collection);
1099 window_with_focus->temp_item_selected = FALSE;
1102 static void clear_selection(gpointer data, guint action, GtkWidget *widget)
1104 g_return_if_fail(window_with_focus != NULL);
1106 collection_clear_selection(window_with_focus->collection);
1107 window_with_focus->temp_item_selected = FALSE;
1110 static void show_options(gpointer data, guint action, GtkWidget *widget)
1112 g_return_if_fail(window_with_focus != NULL);
1114 options_show(window_with_focus);
1117 static gboolean new_directory_cb(char *initial, char *path)
1119 if (mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO))
1121 report_error("mkdir", g_strerror(errno));
1122 return FALSE;
1124 return TRUE;
1127 static void new_directory(gpointer data, guint action, GtkWidget *widget)
1129 g_return_if_fail(window_with_focus != NULL);
1131 savebox_show(window_with_focus, "Create directory",
1132 window_with_focus->path, "NewDir",
1133 default_pixmap + TYPE_DIRECTORY, new_directory_cb);
1136 static void xterm_here(gpointer data, guint action, GtkWidget *widget)
1138 char *argv[] = {NULL, NULL};
1140 argv[0] = xterm_here_value;
1142 g_return_if_fail(window_with_focus != NULL);
1144 if (!spawn_full(argv, window_with_focus->path, 0))
1145 report_error("ROX-Filer", "Failed to fork() child "
1146 "process");
1149 static void open_parent(gpointer data, guint action, GtkWidget *widget)
1151 g_return_if_fail(window_with_focus != NULL);
1153 filer_opendir(make_path(window_with_focus->path, "/..")->str,
1154 FALSE, BOTTOM);
1157 static void open_parent_same(gpointer data, guint action, GtkWidget *widget)
1159 g_return_if_fail(window_with_focus != NULL);
1161 change_to_parent(window_with_focus);
1164 static void new_window(gpointer data, guint action, GtkWidget *widget)
1166 g_return_if_fail(window_with_focus != NULL);
1168 filer_opendir(window_with_focus->path, FALSE, BOTTOM);
1171 static void close_window(gpointer data, guint action, GtkWidget *widget)
1173 g_return_if_fail(window_with_focus != NULL);
1175 gtk_widget_destroy(window_with_focus->window);
1178 static void enter_path(gpointer data, guint action, GtkWidget *widget)
1180 g_return_if_fail(window_with_focus != NULL);
1182 minibuffer_show(window_with_focus);
1185 static void rox_help(gpointer data, guint action, GtkWidget *widget)
1187 g_return_if_fail(window_with_focus != NULL);
1189 filer_opendir(make_path(getenv("APP_DIR"), "Help")->str, FALSE, BOTTOM);
1192 static void open_as_dir(gpointer data, guint action, GtkWidget *widget)
1194 g_return_if_fail(window_with_focus != NULL);
1196 filer_opendir(window_with_focus->path, FALSE, BOTTOM);
1199 static void close_panel(gpointer data, guint action, GtkWidget *widget)
1201 g_return_if_fail(window_with_focus != NULL);
1203 gtk_widget_destroy(window_with_focus->window);