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)
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
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 */
28 #include <sys/types.h>
30 #include <sys/param.h>
44 #include "gui_support.h"
47 #include "gtksavebox.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 static gint updating_menu
= 0; /* Non-zero => ignore activations */
63 static GtkWidget
*xterm_here_entry
;
64 static char *xterm_here_value
;
66 /* Static prototypes */
68 static void position_menu(GtkMenu
*menu
, gint
*x
, gint
*y
, gpointer data
);
69 static void menu_closed(GtkWidget
*widget
);
70 static void items_sensitive(gboolean state
);
71 static char *load_xterm_here(char *data
);
72 static void savebox_show(guchar
*title
, guchar
*path
, MaskedPixmap
*image
,
73 gboolean (*callback
)(guchar
*current
, guchar
*new));
74 static gint
save_to_file(GtkSavebox
*savebox
, guchar
*pathname
);
76 /* Note that for these callbacks none of the arguments are used. */
77 static void large(gpointer data
, guint action
, GtkWidget
*widget
);
78 static void small(gpointer data
, guint action
, GtkWidget
*widget
);
79 static void full_info(gpointer data
, guint action
, GtkWidget
*widget
);
81 static void sort_name(gpointer data
, guint action
, GtkWidget
*widget
);
82 static void sort_type(gpointer data
, guint action
, GtkWidget
*widget
);
83 static void sort_size(gpointer data
, guint action
, GtkWidget
*widget
);
84 static void sort_date(gpointer data
, guint action
, GtkWidget
*widget
);
86 static void hidden(gpointer data
, guint action
, GtkWidget
*widget
);
87 static void refresh(gpointer data
, guint action
, GtkWidget
*widget
);
89 static void copy_item(gpointer data
, guint action
, GtkWidget
*widget
);
90 static void rename_item(gpointer data
, guint action
, GtkWidget
*widget
);
91 static void link_item(gpointer data
, guint action
, GtkWidget
*widget
);
92 static void open_file(gpointer data
, guint action
, GtkWidget
*widget
);
93 static void help(gpointer data
, guint action
, GtkWidget
*widget
);
94 static void show_file_info(gpointer data
, guint action
, GtkWidget
*widget
);
95 static void mount(gpointer data
, guint action
, GtkWidget
*widget
);
96 static void delete(gpointer data
, guint action
, GtkWidget
*widget
);
97 static void remove_link(gpointer data
, guint action
, GtkWidget
*widget
);
98 static void usage(gpointer data
, guint action
, GtkWidget
*widget
);
99 static void chmod_items(gpointer data
, guint action
, GtkWidget
*widget
);
100 static void find(gpointer data
, guint action
, GtkWidget
*widget
);
102 static void open_vfs_rpm(gpointer data
, guint action
, GtkWidget
*widget
);
103 static void open_vfs_utar(gpointer data
, guint action
, GtkWidget
*widget
);
104 static void open_vfs_uzip(gpointer data
, guint action
, GtkWidget
*widget
);
106 static void select_all(gpointer data
, guint action
, GtkWidget
*widget
);
107 static void clear_selection(gpointer data
, guint action
, GtkWidget
*widget
);
108 static void show_options(gpointer data
, guint action
, GtkWidget
*widget
);
109 static void new_directory(gpointer data
, guint action
, GtkWidget
*widget
);
110 static void xterm_here(gpointer data
, guint action
, GtkWidget
*widget
);
112 static void open_parent_same(gpointer data
, guint action
, GtkWidget
*widget
);
113 static void open_parent(gpointer data
, guint action
, GtkWidget
*widget
);
114 static void new_window(gpointer data
, guint action
, GtkWidget
*widget
);
115 static void close_window(gpointer data
, guint action
, GtkWidget
*widget
);
116 static void enter_path(gpointer data
, guint action
, GtkWidget
*widget
);
117 static void shell_command(gpointer data
, guint action
, GtkWidget
*widget
);
118 static void rox_help(gpointer data
, guint action
, GtkWidget
*widget
);
120 static void open_as_dir(gpointer data
, guint action
, GtkWidget
*widget
);
121 static void close_panel(gpointer data
, guint action
, GtkWidget
*widget
);
123 static GtkWidget
*create_options();
124 static void update_options();
125 static void set_options();
126 static void save_options();
128 static OptionsSection options
=
138 static GtkWidget
*filer_menu
; /* The popup filer menu */
139 static GtkWidget
*filer_file_item
; /* The File '' label */
140 static GtkWidget
*filer_file_menu
; /* The File '' menu */
141 static GtkWidget
*filer_vfs_menu
; /* The Open VFS menu */
142 static GtkWidget
*filer_hidden_menu
; /* The Show Hidden item */
143 static GtkWidget
*filer_new_window
; /* The New Window item */
144 static GtkWidget
*panel_menu
; /* The popup panel menu */
145 static GtkWidget
*panel_hidden_menu
; /* The Show Hidden item */
147 /* Used for Copy, etc */
148 static GtkWidget
*savebox
= NULL
;
149 static guchar
*current_path
= NULL
;
150 static gboolean (*current_savebox_callback
)(guchar
*current
, guchar
*new);
152 static gint screen_width
, screen_height
;
154 static GtkItemFactoryEntry filer_menu_def
[] = {
155 {"/Display", NULL
, NULL
, 0, "<Branch>"},
156 {"/Display/Large Icons", NULL
, large
, 0, NULL
},
157 {"/Display/Small Icons", NULL
, small
, 0, NULL
},
158 {"/Display/Full Info", NULL
, full_info
, 0, NULL
},
159 {"/Display/Separator", NULL
, NULL
, 0, "<Separator>"},
160 {"/Display/Sort by Name", NULL
, sort_name
, 0, NULL
},
161 {"/Display/Sort by Type", NULL
, sort_type
, 0, NULL
},
162 {"/Display/Sort by Date", NULL
, sort_date
, 0, NULL
},
163 {"/Display/Sort by Size", NULL
, sort_size
, 0, NULL
},
164 {"/Display/Separator", NULL
, NULL
, 0, "<Separator>"},
165 {"/Display/Show Hidden", C_
"H", hidden
, 0, "<ToggleItem>"},
166 {"/Display/Refresh", C_
"L", refresh
, 0, NULL
},
167 {"/File", NULL
, NULL
, 0, "<Branch>"},
168 {"/File/Copy...", NULL
, copy_item
, 0, NULL
},
169 {"/File/Rename...", NULL
, rename_item
, 0, NULL
},
170 {"/File/Link...", NULL
, link_item
, 0, NULL
},
171 {"/File/Shift Open", NULL
, open_file
, 0, NULL
},
172 {"/File/Help", "F1", help
, 0, NULL
},
173 {"/File/Info", "I", show_file_info
, 0, NULL
},
174 {"/File/Open VFS", NULL
, NULL
, 0, "<Branch>"},
175 {"/File/Open VFS/Unzip", NULL
, open_vfs_uzip
, 0, NULL
},
176 {"/File/Open VFS/Untar", NULL
, open_vfs_utar
, 0, NULL
},
177 {"/File/Open VFS/RPM", NULL
, open_vfs_rpm
, 0, NULL
},
178 {"/File/Separator", NULL
, NULL
, 0, "<Separator>"},
179 {"/File/Mount", "M", mount
, 0, NULL
},
180 {"/File/Delete", C_
"X", delete, 0, NULL
},
181 {"/File/Disk Usage", "U", usage
, 0, NULL
},
182 {"/File/Permissions", NULL
, chmod_items
, 0, NULL
},
183 {"/File/Find", NULL
, find
, 0, NULL
},
184 {"/Select All", C_
"A", select_all
, 0, NULL
},
185 {"/Clear Selection", C_
"Z", clear_selection
, 0, NULL
},
186 {"/Options...", NULL
, show_options
, 0, NULL
},
187 {"/New Directory...", NULL
, new_directory
, 0, NULL
},
188 {"/Xterm Here", NULL
, xterm_here
, 0, NULL
},
189 {"/Window", NULL
, NULL
, 0, "<Branch>"},
190 {"/Window/Parent, New Window", NULL
, open_parent
, 0, NULL
},
191 {"/Window/Parent, Same Window", NULL
, open_parent_same
, 0, NULL
},
192 {"/Window/New Window", NULL
, new_window
, 0, NULL
},
193 {"/Window/Close Window", C_
"Q", close_window
, 0, NULL
},
194 {"/Window/Enter Path", NULL
, enter_path
, 0, NULL
},
195 {"/Window/Shell Command", NULL
, shell_command
, 0, NULL
},
196 {"/Window/Separator", NULL
, NULL
, 0, "<Separator>"},
197 {"/Window/Show ROX-Filer help", NULL
, rox_help
, 0, NULL
},
200 static GtkItemFactoryEntry panel_menu_def
[] = {
201 {"/Display", NULL
, NULL
, 0, "<Branch>"},
202 {"/Display/Sort by Name", NULL
, sort_name
, 0, NULL
},
203 {"/Display/Sort by Type", NULL
, sort_type
, 0, NULL
},
204 {"/Display/Sort by Date", NULL
, sort_date
, 0, NULL
},
205 {"/Display/Sort by Size", NULL
, sort_size
, 0, NULL
},
206 {"/Display/Separator", NULL
, NULL
, 0, "<Separator>"},
207 {"/Display/Show Hidden", NULL
, hidden
, 0, "<ToggleItem>"},
208 {"/Display/Refresh", NULL
, refresh
, 0, NULL
},
209 {"/Open Panel as Directory", NULL
, open_as_dir
, 0, NULL
},
210 {"/Close Panel", NULL
, close_panel
, 0, NULL
},
211 {"/Separator", NULL
, NULL
, 0, "<Separator>"},
212 {"/ROX-Filer Help", NULL
, rox_help
, 0, NULL
},
213 {"/ROX-Filer Options...", NULL
, show_options
, 0, NULL
},
214 {"/Separator", NULL
, NULL
, 0, "<Separator>"},
215 {"/Show Help", NULL
, help
, 0, NULL
},
216 {"/Remove Item", NULL
, remove_link
, 0, NULL
},
219 typedef struct _FileStatus FileStatus
;
221 /* This is for the 'file(1) says...' thing */
224 int fd
; /* FD to read from, -1 if closed */
225 int input
; /* Input watcher tag if fd valid */
226 GtkLabel
*label
; /* Widget to output to */
227 gboolean start
; /* No output yet */
232 GtkItemFactory
*item_factory
;
236 /* This call starts returning strange values after a while, so get
237 * the result here during init.
239 gdk_window_get_size(GDK_ROOT_PARENT(), &screen_width
, &screen_height
);
241 filer_keys
= gtk_accel_group_new();
242 item_factory
= gtk_item_factory_new(GTK_TYPE_MENU
,
245 gtk_item_factory_create_items(item_factory
,
246 sizeof(filer_menu_def
) / sizeof(*filer_menu_def
),
249 filer_menu
= gtk_item_factory_get_widget(item_factory
, "<filer>");
250 filer_file_menu
= gtk_item_factory_get_widget(item_factory
,
252 filer_vfs_menu
= gtk_item_factory_get_widget(item_factory
,
253 "<filer>/File/Open VFS");
254 filer_hidden_menu
= gtk_item_factory_get_widget(item_factory
,
255 "<filer>/Display/Show Hidden");
256 items
= gtk_container_children(GTK_CONTAINER(filer_menu
));
257 filer_file_item
= GTK_BIN(g_list_nth(items
, 1)->data
)->child
;
259 filer_new_window
= GTK_BIN(gtk_item_factory_get_widget(item_factory
,
260 "<filer>/Window/New Window"))->child
;
262 panel_keys
= gtk_accel_group_new();
263 item_factory
= gtk_item_factory_new(GTK_TYPE_MENU
,
266 gtk_item_factory_create_items(item_factory
,
267 sizeof(panel_menu_def
) / sizeof(*panel_menu_def
),
270 panel_menu
= gtk_item_factory_get_widget(item_factory
, "<panel>");
271 panel_hidden_menu
= gtk_item_factory_get_widget(item_factory
,
272 "<panel>/Display/Show Hidden");
274 menurc
= choices_find_path_load("menus", "ROX-Filer");
276 gtk_item_factory_parse_rc(menurc
);
278 gtk_accel_group_lock(panel_keys
);
280 gtk_signal_connect(GTK_OBJECT(filer_menu
), "unmap_event",
281 GTK_SIGNAL_FUNC(menu_closed
), NULL
);
282 gtk_signal_connect(GTK_OBJECT(panel_menu
), "unmap_event",
283 GTK_SIGNAL_FUNC(menu_closed
), NULL
);
284 gtk_signal_connect(GTK_OBJECT(filer_file_menu
), "unmap_event",
285 GTK_SIGNAL_FUNC(menu_closed
), NULL
);
287 options_sections
= g_slist_prepend(options_sections
, &options
);
288 xterm_here_value
= g_strdup("xterm");
289 option_register("xterm_here", load_xterm_here
);
291 savebox
= gtk_savebox_new();
292 gtk_signal_connect_object(GTK_OBJECT(savebox
), "save_to_file",
293 GTK_SIGNAL_FUNC(save_to_file
), NULL
);
294 gtk_signal_connect_object(GTK_OBJECT(savebox
), "save_done",
295 GTK_SIGNAL_FUNC(gtk_widget_hide
),
296 GTK_OBJECT(savebox
));
299 /* Build up some option widgets to go in the options dialog, but don't
302 static GtkWidget
*create_options()
304 GtkWidget
*table
, *label
;
306 table
= gtk_table_new(2, 2, FALSE
);
307 gtk_container_set_border_width(GTK_CONTAINER(table
), 4);
309 label
= gtk_label_new("To set the keyboard short-cuts you simply open "
310 "the menu over a filer window, move the pointer over "
311 "the item you want to use and press a key. The key "
312 "will appear next to the menu item and you can just "
313 "press that key without opening the menu in future. "
314 "To save the current menu short-cuts for next time, "
315 "click the Save button at the bottom of this window.");
316 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
317 gtk_table_attach_defaults(GTK_TABLE(table
), label
, 0, 2, 0, 1);
319 label
= gtk_label_new("'Xterm here' program:");
320 gtk_table_attach_defaults(GTK_TABLE(table
), label
, 0, 1, 1, 2);
321 xterm_here_entry
= gtk_entry_new();
322 gtk_table_attach_defaults(GTK_TABLE(table
), xterm_here_entry
,
328 static char *load_xterm_here(char *data
)
330 g_free(xterm_here_value
);
331 xterm_here_value
= g_strdup(data
);
335 static void update_options()
337 gtk_entry_set_text(GTK_ENTRY(xterm_here_entry
), xterm_here_value
);
340 static void set_options()
342 g_free(xterm_here_value
);
343 xterm_here_value
= g_strdup(gtk_entry_get_text(
344 GTK_ENTRY(xterm_here_entry
)));
347 static void save_options()
351 menurc
= choices_find_path_save("menus", "ROX-Filer", TRUE
);
353 gtk_item_factory_dump_rc(menurc
, NULL
, TRUE
);
355 option_write("xterm_here", xterm_here_value
);
359 static void items_sensitive(gboolean state
)
364 items
= item
= gtk_container_children(GTK_CONTAINER(filer_file_menu
));
367 gtk_widget_set_sensitive(GTK_BIN(item
->data
)->child
, state
);
372 items
= item
= gtk_container_children(GTK_CONTAINER(filer_vfs_menu
));
375 gtk_widget_set_sensitive(GTK_BIN(item
->data
)->child
, state
);
381 static void position_menu(GtkMenu
*menu
, gint
*x
, gint
*y
, gpointer data
)
383 int *pos
= (int *) data
;
384 GtkRequisition requisition
;
386 gtk_widget_size_request(GTK_WIDGET(menu
), &requisition
);
389 *x
= screen_width
- MENU_MARGIN
- requisition
.width
;
390 else if (pos
[0] == -2)
393 *x
= pos
[0] - (requisition
.width
>> 2);
396 *y
= screen_height
- MENU_MARGIN
- requisition
.height
;
397 else if (pos
[1] == -2)
400 *y
= pos
[1] - (requisition
.height
>> 2);
402 *x
= CLAMP(*x
, 0, screen_width
- requisition
.width
);
403 *y
= CLAMP(*y
, 0, screen_height
- requisition
.height
);
406 void show_filer_menu(FilerWindow
*filer_window
, GdkEventButton
*event
,
414 pos
[0] = event
->x_root
;
415 pos
[1] = event
->y_root
;
417 window_with_focus
= filer_window
;
419 switch (filer_window
->panel_type
)
431 if (filer_window
->panel_type
)
432 collection_clear_selection(filer_window
->collection
); /* ??? */
434 if (filer_window
->collection
->number_selected
== 0 && item
>= 0)
436 collection_select_item(filer_window
->collection
, item
);
437 filer_window
->temp_item_selected
= TRUE
;
440 filer_window
->temp_item_selected
= FALSE
;
442 if (filer_window
->panel_type
)
444 gtk_check_menu_item_set_active(
445 GTK_CHECK_MENU_ITEM(panel_hidden_menu
),
446 filer_window
->show_hidden
);
450 GtkWidget
*file_label
, *file_menu
;
451 Collection
*collection
= filer_window
->collection
;
454 file_label
= filer_file_item
;
455 file_menu
= filer_file_menu
;
456 gtk_check_menu_item_set_active(
457 GTK_CHECK_MENU_ITEM(filer_hidden_menu
),
458 filer_window
->show_hidden
);
459 buffer
= g_string_new(NULL
);
460 switch (collection
->number_selected
)
463 g_string_assign(buffer
, "Next Click");
464 items_sensitive(TRUE
);
467 items_sensitive(TRUE
);
468 file_item
= selected_item(
469 filer_window
->collection
);
470 g_string_sprintf(buffer
, "%s '%s'",
471 basetype_name(file_item
),
472 file_item
->leafname
);
475 items_sensitive(FALSE
);
476 g_string_sprintf(buffer
, "%d items",
477 collection
->number_selected
);
480 gtk_label_set_text(GTK_LABEL(file_label
), buffer
->str
);
481 g_string_free(buffer
, TRUE
);
485 gtk_widget_set_sensitive(filer_new_window
, !o_unique_filer_windows
);
487 if (filer_window
->panel_type
)
488 popup_menu
= panel_menu
;
490 popup_menu
= (event
->state
& GDK_CONTROL_MASK
)
496 gtk_menu_popup(GTK_MENU(popup_menu
), NULL
, NULL
, position_menu
,
497 (gpointer
) pos
, event
->button
, event
->time
);
500 static void menu_closed(GtkWidget
*widget
)
502 if (window_with_focus
== NULL
|| widget
!= popup_menu
)
503 return; /* Close panel item chosen? */
505 if (window_with_focus
->temp_item_selected
)
507 collection_clear_selection(window_with_focus
->collection
);
508 window_with_focus
->temp_item_selected
= FALSE
;
512 void target_callback(Collection
*collection
, gint item
, gpointer real_fn
)
514 g_return_if_fail(window_with_focus
!= NULL
);
515 g_return_if_fail(window_with_focus
->collection
== collection
);
516 g_return_if_fail(real_fn
!= NULL
);
518 collection_wink_item(collection
, item
);
519 collection_clear_selection(collection
);
520 collection_select_item(collection
, item
);
521 ((CollectionTargetFunc
)real_fn
)(NULL
, 0, collection
);
522 if (item
< collection
->number_of_items
)
523 collection_unselect_item(collection
, item
);
528 static void large(gpointer data
, guint action
, GtkWidget
*widget
)
530 g_return_if_fail(window_with_focus
!= NULL
);
532 filer_style_set(window_with_focus
, LARGE_ICONS
);
535 static void small(gpointer data
, guint action
, GtkWidget
*widget
)
537 g_return_if_fail(window_with_focus
!= NULL
);
539 filer_style_set(window_with_focus
, SMALL_ICONS
);
542 static void full_info(gpointer data
, guint action
, GtkWidget
*widget
)
544 g_return_if_fail(window_with_focus
!= NULL
);
546 filer_style_set(window_with_focus
, FULL_INFO
);
549 static void sort_name(gpointer data
, guint action
, GtkWidget
*widget
)
551 g_return_if_fail(window_with_focus
!= NULL
);
553 filer_set_sort_fn(window_with_focus
, sort_by_name
);
556 static void sort_type(gpointer data
, guint action
, GtkWidget
*widget
)
558 g_return_if_fail(window_with_focus
!= NULL
);
560 filer_set_sort_fn(window_with_focus
, sort_by_type
);
563 static void sort_date(gpointer data
, guint action
, GtkWidget
*widget
)
565 g_return_if_fail(window_with_focus
!= NULL
);
567 filer_set_sort_fn(window_with_focus
, sort_by_date
);
570 static void sort_size(gpointer data
, guint action
, GtkWidget
*widget
)
572 g_return_if_fail(window_with_focus
!= NULL
);
574 filer_set_sort_fn(window_with_focus
, sort_by_size
);
577 static void hidden(gpointer data
, guint action
, GtkWidget
*widget
)
582 g_return_if_fail(window_with_focus
!= NULL
);
584 filer_set_hidden(window_with_focus
, !window_with_focus
->show_hidden
);
587 static void refresh(gpointer data
, guint action
, GtkWidget
*widget
)
589 g_return_if_fail(window_with_focus
!= NULL
);
592 update_dir(window_with_focus
, TRUE
);
595 static void mount(gpointer data
, guint action
, GtkWidget
*widget
)
597 g_return_if_fail(window_with_focus
!= NULL
);
599 if (window_with_focus
->collection
->number_selected
== 0)
600 collection_target(window_with_focus
->collection
,
601 target_callback
, mount
);
603 action_mount(window_with_focus
, NULL
);
606 static void delete(gpointer data
, guint action
, GtkWidget
*widget
)
608 g_return_if_fail(window_with_focus
!= NULL
);
610 if (window_with_focus
->collection
->number_selected
== 0)
611 collection_target(window_with_focus
->collection
,
612 target_callback
, delete);
614 action_delete(window_with_focus
);
617 static void remove_link(gpointer data
, guint action
, GtkWidget
*widget
)
619 g_return_if_fail(window_with_focus
!= NULL
);
621 if (window_with_focus
->collection
->number_selected
> 1)
623 report_error("ROX-Filer", "You can only remove one link "
627 else if (window_with_focus
->collection
->number_selected
== 0)
628 collection_target(window_with_focus
->collection
,
629 target_callback
, remove_link
);
636 item
= selected_item(window_with_focus
->collection
);
638 path
= make_path(window_with_focus
->path
, item
->leafname
)->str
;
639 if (lstat(path
, &info
))
640 report_error("ROX-Filer", g_strerror(errno
));
641 else if (!S_ISLNK(info
.st_mode
))
642 report_error("ROX-Filer",
643 "You can only remove symbolic links this way - "
644 "try the 'Open Panel as Directory' item.");
645 else if (unlink(path
))
646 report_error("ROX-Filer", g_strerror(errno
));
648 update_dir(window_with_focus
, TRUE
);
652 static void usage(gpointer data
, guint action
, GtkWidget
*widget
)
654 g_return_if_fail(window_with_focus
!= NULL
);
656 if (window_with_focus
->collection
->number_selected
== 0)
657 collection_target(window_with_focus
->collection
,
658 target_callback
, usage
);
660 action_usage(window_with_focus
);
663 static void chmod_items(gpointer data
, guint action
, GtkWidget
*widget
)
665 g_return_if_fail(window_with_focus
!= NULL
);
667 if (window_with_focus
->collection
->number_selected
== 0)
668 collection_target(window_with_focus
->collection
,
669 target_callback
, chmod_items
);
671 action_chmod(window_with_focus
);
674 static void find(gpointer data
, guint action
, GtkWidget
*widget
)
676 g_return_if_fail(window_with_focus
!= NULL
);
678 if (window_with_focus
->collection
->number_selected
== 0)
679 collection_target(window_with_focus
->collection
,
680 target_callback
, find
);
682 action_find(window_with_focus
);
685 /* This pops up our savebox widget, cancelling any currently open one,
686 * and allows the user to pick a new path for it.
687 * Once the new path has been picked, the callback will be called with
688 * both the current and new paths.
690 static void savebox_show(guchar
*title
, guchar
*path
, MaskedPixmap
*image
,
691 gboolean (*callback
)(guchar
*current
, guchar
*new))
693 if (GTK_WIDGET_VISIBLE(savebox
))
694 gtk_widget_hide(savebox
);
697 g_free(current_path
);
698 current_path
= g_strdup(path
);
699 current_savebox_callback
= callback
;
701 gtk_window_set_title(GTK_WINDOW(savebox
), title
);
702 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox
), current_path
);
703 gtk_savebox_set_icon(GTK_SAVEBOX(savebox
), image
->pixmap
, image
->mask
);
705 gtk_widget_grab_focus(GTK_SAVEBOX(savebox
)->entry
);
706 gtk_widget_show(savebox
);
709 static gint
save_to_file(GtkSavebox
*savebox
, guchar
*pathname
)
711 g_return_val_if_fail(current_savebox_callback
!= NULL
,
714 return current_savebox_callback(current_path
, pathname
)
715 ? GTK_XDS_SAVED
: GTK_XDS_SAVE_ERROR
;
718 static gboolean
copy_cb(guchar
*current
, guchar
*new)
720 char *new_dir
, *leaf
;
725 report_error("ROX-Filer", "New pathname is not absolute");
729 if (new[strlen(new) - 1] == '/')
731 new_dir
= g_strdup(new);
738 slash
= strrchr(new, '/');
739 new_dir
= g_strndup(new, slash
- new);
743 local_paths
= g_slist_append(NULL
, current
);
744 action_copy(local_paths
, new_dir
, leaf
);
745 g_slist_free(local_paths
);
752 #define SHOW_SAVEBOX(title, callback) \
756 item = selected_item(collection); \
757 path = make_path(window_with_focus->path, item->leafname)->str; \
758 savebox_show(title, path, item->image, callback); \
761 static void copy_item(gpointer data
, guint action
, GtkWidget
*widget
)
763 Collection
*collection
;
765 g_return_if_fail(window_with_focus
!= NULL
);
767 collection
= window_with_focus
->collection
;
768 if (collection
->number_selected
> 1)
770 report_error("ROX-Filer", "You cannot do this to more than "
771 "one item at a time");
774 else if (collection
->number_selected
!= 1)
775 collection_target(collection
, target_callback
, copy_item
);
777 SHOW_SAVEBOX("Copy", copy_cb
);
780 static gboolean
rename_cb(guchar
*current
, guchar
*new)
782 if (rename(current
, new))
784 report_error("ROX-Filer: rename()", g_strerror(errno
));
790 static void rename_item(gpointer data
, guint action
, GtkWidget
*widget
)
792 Collection
*collection
;
794 g_return_if_fail(window_with_focus
!= NULL
);
796 collection
= window_with_focus
->collection
;
797 if (collection
->number_selected
> 1)
799 report_error("ROX-Filer", "You cannot do this to more than "
800 "one item at a time");
803 else if (collection
->number_selected
!= 1)
804 collection_target(collection
, target_callback
, rename_item
);
806 SHOW_SAVEBOX("Rename", rename_cb
);
809 static gboolean
link_cb(guchar
*initial
, guchar
*path
)
811 if (symlink(initial
, path
))
813 report_error("ROX-Filer: symlink()", g_strerror(errno
));
819 static void link_item(gpointer data
, guint action
, GtkWidget
*widget
)
821 Collection
*collection
;
823 g_return_if_fail(window_with_focus
!= NULL
);
825 collection
= window_with_focus
->collection
;
826 if (collection
->number_selected
> 1)
828 report_error("ROX-Filer", "You cannot do this to more than "
829 "one item at a time");
832 else if (collection
->number_selected
!= 1)
833 collection_target(collection
, target_callback
, link_item
);
835 SHOW_SAVEBOX("Symlink", link_cb
);
838 static void open_file(gpointer data
, guint action
, GtkWidget
*widget
)
840 Collection
*collection
;
842 g_return_if_fail(window_with_focus
!= NULL
);
844 collection
= window_with_focus
->collection
;
845 if (collection
->number_selected
> 1)
847 report_error("ROX-Filer", "You cannot do this to more than "
848 "one item at a time");
851 else if (collection
->number_selected
!= 1)
852 collection_target(collection
, target_callback
, open_file
);
854 filer_openitem(window_with_focus
,
855 selected_item_number(collection
),
856 OPEN_SAME_WINDOW
| OPEN_SHIFT
);
859 /* Got some data from file(1) - stick it in the window. */
860 static void add_file_output(FileStatus
*fs
,
861 gint source
, GdkInputCondition condition
)
867 got
= read(source
, buffer
, sizeof(buffer
) - 1);
871 gtk_input_remove(fs
->input
);
875 delayed_error("ROX-Filer: file(1) says...",
887 gtk_label_get(fs
->label
, &str
);
889 str
= g_strconcat(str
, buffer
, NULL
);
890 gtk_label_set_text(fs
->label
, str
);
894 static void file_info_destroyed(GtkWidget
*widget
, FileStatus
*fs
)
898 gtk_input_remove(fs
->input
);
905 /* g_free() the result */
906 guchar
*pretty_type(DirItem
*file
, guchar
*path
)
909 return g_strconcat(file
->mime_type
->media_type
, "/",
910 file
->mime_type
->subtype
, NULL
);
912 if (file
->flags
& ITEM_FLAG_APPDIR
)
913 return g_strdup("ROX application");
915 if (file
->flags
& ITEM_FLAG_SYMLINK
)
917 char p
[MAXPATHLEN
+ 1];
919 got
= readlink(path
, p
, MAXPATHLEN
);
920 if (got
> 0 && got
<= MAXPATHLEN
)
923 return g_strconcat("Symbolic link to ", p
, NULL
);
926 return g_strdup("Symbolic link");
929 if (file
->flags
& ITEM_FLAG_MOUNT_POINT
)
932 if ((file
->flags
& ITEM_FLAG_MOUNTED
) &&
933 (mp
= g_hash_table_lookup(mtab_mounts
, path
)))
934 return g_strconcat("Mount point for ", mp
->name
, NULL
);
936 return g_strdup("Mount point");
939 return g_strdup("-");
942 #define LABEL(text, row) \
943 label = gtk_label_new(text ":"); \
944 gtk_misc_set_alignment(GTK_MISC(label), 1, .5); \
945 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT); \
946 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, row, row + 1);
948 #define VALUE(label, row) \
949 gtk_misc_set_alignment(GTK_MISC(label), 0, .5); \
950 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, row, row + 1);
952 static void show_file_info(gpointer data
, guint action
, GtkWidget
*widget
)
954 GtkWidget
*window
, *table
, *label
, *button
, *frame
, *value
;
955 GtkWidget
*file_label
;
959 char *argv
[] = {"file", "-b", NULL
, NULL
};
960 Collection
*collection
;
963 FileStatus
*fs
= NULL
;
966 g_return_if_fail(window_with_focus
!= NULL
);
968 collection
= window_with_focus
->collection
;
969 if (collection
->number_selected
> 1)
971 report_error("ROX-Filer", "You cannot do this to more than "
972 "one item at a time");
975 else if (collection
->number_selected
!= 1)
977 collection_target(collection
, target_callback
, show_file_info
);
980 file
= selected_item(collection
);
981 path
= make_path(window_with_focus
->path
,
982 file
->leafname
)->str
;
983 if (lstat(path
, &info
))
985 delayed_error("ROX-Filer", g_strerror(errno
));
989 gstring
= g_string_new(NULL
);
991 window
= gtk_window_new(GTK_WINDOW_DIALOG
);
992 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_MOUSE
);
993 gtk_container_set_border_width(GTK_CONTAINER(window
), 4);
994 gtk_window_set_title(GTK_WINDOW(window
), path
);
996 table
= gtk_table_new(10, 2, FALSE
);
997 gtk_container_add(GTK_CONTAINER(window
), table
);
998 gtk_table_set_row_spacings(GTK_TABLE(table
), 8);
999 gtk_table_set_col_spacings(GTK_TABLE(table
), 4);
1001 value
= gtk_label_new(file
->leafname
);
1005 g_string_sprintf(gstring
, "%s, %s", user_name(info
.st_uid
),
1006 group_name(info
.st_gid
));
1007 value
= gtk_label_new(gstring
->str
);
1008 LABEL("Owner, Group", 1);
1011 if (info
.st_size
>= PRETTY_SIZE_LIMIT
)
1013 g_string_sprintf(gstring
, "%s (%ld bytes)",
1014 format_size((unsigned long) info
.st_size
),
1015 (unsigned long) info
.st_size
);
1019 g_string_sprintf(gstring
, "%ld byte%s",
1020 (unsigned long) info
.st_size
,
1021 info
.st_size
== 1 ? "" : "s");
1023 value
= gtk_label_new(gstring
->str
);
1027 value
= gtk_label_new(pretty_time(&info
.st_ctime
));
1028 LABEL("Change time", 3);
1031 value
= gtk_label_new(pretty_time(&info
.st_mtime
));
1032 LABEL("Modify time", 4);
1035 value
= gtk_label_new(pretty_time(&info
.st_atime
));
1036 LABEL("Access time", 5);
1039 value
= gtk_label_new(pretty_permissions(info
.st_mode
));
1040 perm
= applicable(info
.st_uid
, info
.st_gid
);
1041 gtk_label_set_pattern(GTK_LABEL(value
),
1042 perm
== 0 ? "___ " :
1043 perm
== 1 ? " ___ " :
1045 gtk_widget_set_style(value
, fixed_style
);
1046 LABEL("Permissions", 6);
1049 tmp
= pretty_type(file
, path
);
1050 value
= gtk_label_new(tmp
);
1055 frame
= gtk_frame_new("file(1) says...");
1056 gtk_table_attach_defaults(GTK_TABLE(table
), frame
, 0, 2, 8, 9);
1057 file_label
= gtk_label_new("<nothing yet>");
1058 gtk_misc_set_padding(GTK_MISC(file_label
), 4, 4);
1059 gtk_label_set_line_wrap(GTK_LABEL(file_label
), TRUE
);
1060 gtk_container_add(GTK_CONTAINER(frame
), file_label
);
1062 button
= gtk_button_new_with_label("OK");
1063 gtk_window_set_focus(GTK_WINDOW(window
), button
);
1064 gtk_table_attach(GTK_TABLE(table
), button
, 0, 2, 10, 11,
1065 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
, 0, 40, 4);
1066 gtk_signal_connect_object(GTK_OBJECT(button
), "clicked",
1067 gtk_widget_destroy
, GTK_OBJECT(window
));
1069 gtk_widget_show_all(window
);
1072 if (pipe(file_data
))
1074 g_string_sprintf(gstring
, "pipe(): %s", g_strerror(errno
));
1075 g_string_free(gstring
, TRUE
);
1081 g_string_sprintf(gstring
, "fork(): %s",
1083 gtk_label_set_text(GTK_LABEL(file_label
), gstring
->str
);
1084 g_string_free(gstring
, TRUE
);
1085 close(file_data
[0]);
1086 close(file_data
[1]);
1089 /* We are the child */
1090 close(file_data
[0]);
1091 dup2(file_data
[1], STDOUT_FILENO
);
1092 dup2(file_data
[1], STDERR_FILENO
);
1096 argv
[1] = file
->leafname
;
1097 chdir(window_with_focus
->path
);
1099 if (execvp(argv
[0], argv
))
1100 fprintf(stderr
, "execvp() error: %s\n",
1104 /* We are the parent */
1105 close(file_data
[1]);
1106 fs
= g_new(FileStatus
, 1);
1107 fs
->label
= GTK_LABEL(file_label
);
1108 fs
->fd
= file_data
[0];
1110 fs
->input
= gdk_input_add(fs
->fd
, GDK_INPUT_READ
,
1111 (GdkInputFunction
) add_file_output
, fs
);
1112 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
1113 GTK_SIGNAL_FUNC(file_info_destroyed
), fs
);
1114 g_string_free(gstring
, TRUE
);
1119 static void app_show_help(char *path
)
1124 help_dir
= g_strconcat(path
, "/Help", NULL
);
1126 if (mc_stat(help_dir
, &info
))
1127 delayed_error("Application",
1128 "This is an application directory - you can "
1129 "run it as a program, or open it (hold down "
1130 "Shift while you open it). Most applications provide "
1131 "their own help here, but this one doesn't.");
1133 filer_opendir(help_dir
, PANEL_NO
);
1136 static void help(gpointer data
, guint action
, GtkWidget
*widget
)
1138 Collection
*collection
;
1141 g_return_if_fail(window_with_focus
!= NULL
);
1143 collection
= window_with_focus
->collection
;
1144 if (collection
->number_selected
> 1)
1146 report_error("ROX-Filer", "You cannot do this to more than "
1147 "one item at a time");
1150 else if (collection
->number_selected
!= 1)
1152 collection_target(collection
, target_callback
, help
);
1155 item
= selected_item(collection
);
1156 switch (item
->base_type
)
1159 if (item
->flags
& ITEM_FLAG_EXEC_FILE
)
1160 delayed_error("Executable file",
1161 "This is a file with an eXecute bit "
1162 "set - it can be run as a program.");
1164 delayed_error("File",
1165 "This is a data file. Try using the "
1166 "Info menu item to find out more...");
1168 case TYPE_DIRECTORY
:
1169 if (item
->flags
& ITEM_FLAG_APPDIR
)
1171 make_path(window_with_focus
->path
,
1172 item
->leafname
)->str
);
1173 else if (item
->flags
& ITEM_FLAG_MOUNT_POINT
)
1174 delayed_error("Mount point",
1175 "A mount point is a directory which another "
1176 "filing system can be mounted on. Everything "
1177 "on the mounted filesystem then appears to be "
1178 "inside the directory.");
1180 delayed_error("Directory",
1181 "This is a directory. It contains an index to "
1182 "other items - open it to see the list.");
1184 case TYPE_CHAR_DEVICE
:
1185 case TYPE_BLOCK_DEVICE
:
1186 delayed_error("Device file",
1187 "Device files allow you to read from or write "
1188 "to a device driver as though it was an "
1192 delayed_error("Named pipe",
1193 "Pipes allow different programs to "
1194 "communicate. One program writes data to the "
1195 "pipe while another one reads it out again.");
1198 delayed_error("Socket",
1199 "Sockets allow processes to communicate.");
1202 delayed_error("Unknown type",
1203 "I couldn't find out what kind of file this "
1204 "is. Maybe it doesn't exist anymore or you "
1205 "don't have search permission on the directory "
1211 #define OPEN_VFS(fs) \
1212 static void open_vfs_ ## fs (gpointer data, guint action, GtkWidget *widget) \
1214 Collection *collection; \
1216 g_return_if_fail(window_with_focus != NULL); \
1218 collection = window_with_focus->collection; \
1219 if (collection->number_selected < 1) \
1220 collection_target(collection, target_callback, \
1223 real_vfs_open(#fs); \
1226 static void real_vfs_open(char *fs
)
1231 if (window_with_focus
->collection
->number_selected
!= 1)
1233 report_error("ROX-Filer", "You must select a single file "
1234 "to open as a Virtual File System");
1238 item
= selected_item(window_with_focus
->collection
);
1240 path
= g_strconcat(window_with_focus
->path
,
1245 filer_change_to(window_with_focus
, path
, NULL
);
1253 static void select_all(gpointer data
, guint action
, GtkWidget
*widget
)
1255 g_return_if_fail(window_with_focus
!= NULL
);
1257 collection_select_all(window_with_focus
->collection
);
1258 window_with_focus
->temp_item_selected
= FALSE
;
1261 static void clear_selection(gpointer data
, guint action
, GtkWidget
*widget
)
1263 g_return_if_fail(window_with_focus
!= NULL
);
1265 collection_clear_selection(window_with_focus
->collection
);
1266 window_with_focus
->temp_item_selected
= FALSE
;
1269 static void show_options(gpointer data
, guint action
, GtkWidget
*widget
)
1271 g_return_if_fail(window_with_focus
!= NULL
);
1273 options_show(window_with_focus
);
1276 static gboolean
new_directory_cb(guchar
*initial
, guchar
*path
)
1278 if (mkdir(path
, S_IRWXU
| S_IRWXG
| S_IRWXO
))
1280 report_error("mkdir", g_strerror(errno
));
1286 static void new_directory(gpointer data
, guint action
, GtkWidget
*widget
)
1288 g_return_if_fail(window_with_focus
!= NULL
);
1290 savebox_show("New Directory",
1291 make_path(window_with_focus
->path
, "NewDir")->str
,
1292 default_pixmap
+ TYPE_DIRECTORY
,
1296 static void xterm_here(gpointer data
, guint action
, GtkWidget
*widget
)
1298 char *argv
[] = {NULL
, NULL
};
1300 argv
[0] = xterm_here_value
;
1302 g_return_if_fail(window_with_focus
!= NULL
);
1304 if (!spawn_full(argv
, window_with_focus
->path
))
1305 report_error("ROX-Filer", "Failed to fork() child "
1309 static void open_parent(gpointer data
, guint action
, GtkWidget
*widget
)
1314 g_return_if_fail(window_with_focus
!= NULL
);
1316 if (window_with_focus
->path
[0] == '/'
1317 && window_with_focus
->path
[1] == '\0')
1318 return; /* Already in the root */
1320 copy
= g_strdup(window_with_focus
->path
);
1321 slash
= strrchr(copy
, '/');
1326 filer_opendir(*copy
? copy
: "/", PANEL_NO
);
1329 g_warning("No / in directory path!\n");
1334 static void open_parent_same(gpointer data
, guint action
, GtkWidget
*widget
)
1336 g_return_if_fail(window_with_focus
!= NULL
);
1338 change_to_parent(window_with_focus
);
1341 static void new_window(gpointer data
, guint action
, GtkWidget
*widget
)
1343 g_return_if_fail(window_with_focus
!= NULL
);
1345 if (o_unique_filer_windows
)
1346 report_error("ROX-Filer", "You can't open a second view onto "
1347 "this directory because the `Unique Windows' option "
1348 "is turned on in the Options window.");
1350 filer_opendir(window_with_focus
->path
, PANEL_NO
);
1353 static void close_window(gpointer data
, guint action
, GtkWidget
*widget
)
1355 g_return_if_fail(window_with_focus
!= NULL
);
1357 gtk_widget_destroy(window_with_focus
->window
);
1360 static void enter_path(gpointer data
, guint action
, GtkWidget
*widget
)
1362 g_return_if_fail(window_with_focus
!= NULL
);
1364 minibuffer_show(window_with_focus
, MINI_PATH
);
1367 static void shell_command(gpointer data
, guint action
, GtkWidget
*widget
)
1369 g_return_if_fail(window_with_focus
!= NULL
);
1371 minibuffer_show(window_with_focus
, MINI_SHELL
);
1374 static void rox_help(gpointer data
, guint action
, GtkWidget
*widget
)
1376 g_return_if_fail(window_with_focus
!= NULL
);
1378 filer_opendir(make_path(getenv("APP_DIR"), "Help")->str
, PANEL_NO
);
1381 static void open_as_dir(gpointer data
, guint action
, GtkWidget
*widget
)
1383 g_return_if_fail(window_with_focus
!= NULL
);
1385 filer_opendir(window_with_focus
->path
, PANEL_NO
);
1388 static void close_panel(gpointer data
, guint action
, GtkWidget
*widget
)
1390 g_return_if_fail(window_with_focus
!= NULL
);
1392 gtk_widget_destroy(window_with_focus
->window
);