4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@users.sourceforge.net>.
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/param.h>
44 #include "gui_support.h"
47 #include "gtksavebox.h"
49 #include "minibuffer.h"
55 #define C_ "<control>"
57 typedef void (*ActionFn
)(GSList
*paths
, char *dest_dir
, char *leaf
);
59 GtkAccelGroup
*filer_keys
;
60 GtkAccelGroup
*pinboard_keys
;
62 GtkWidget
*popup_menu
= NULL
; /* Currently open menu */
64 static gint updating_menu
= 0; /* Non-zero => ignore activations */
67 static GtkWidget
*xterm_here_entry
;
68 char *xterm_here_value
;
70 /* TRUE if we selected an icon automatically when the menu was opened */
71 static gboolean pin_temp_item_selected
;
73 /* Static prototypes */
75 static void save_menus(void);
76 static void position_menu(GtkMenu
*menu
, gint
*x
, gint
*y
, gpointer data
);
77 static void pin_menu_closed(GtkWidget
*widget
);
78 static void menu_closed(GtkWidget
*widget
);
79 static void items_sensitive(gboolean state
);
80 static char *load_xterm_here(char *data
);
81 static void savebox_show(guchar
*title
, guchar
*path
, MaskedPixmap
*image
,
82 gboolean (*callback
)(guchar
*current
, guchar
*new));
83 static gint
save_to_file(GtkSavebox
*savebox
, guchar
*pathname
);
84 static GList
*list_paths(FilerWindow
*filer_window
);
85 static void free_paths(GList
*paths
);
86 static void mark_menus_modified(gboolean mod
);
87 static gboolean
action_with_leaf(ActionFn action
, guchar
*current
, guchar
*new);
89 /* Note that for most of these callbacks none of the arguments are used. */
90 static void large(gpointer data
, guint action
, GtkWidget
*widget
);
91 static void small(gpointer data
, guint action
, GtkWidget
*widget
);
93 /* (action used in these two - (DetailsType) */
94 static void large_with(gpointer data
, guint action
, GtkWidget
*widget
);
95 static void small_with(gpointer data
, guint action
, GtkWidget
*widget
);
97 static void sort_name(gpointer data
, guint action
, GtkWidget
*widget
);
98 static void sort_type(gpointer data
, guint action
, GtkWidget
*widget
);
99 static void sort_size(gpointer data
, guint action
, GtkWidget
*widget
);
100 static void sort_date(gpointer data
, guint action
, GtkWidget
*widget
);
102 static void hidden(gpointer data
, guint action
, GtkWidget
*widget
);
103 static void refresh(gpointer data
, guint action
, GtkWidget
*widget
);
105 static void copy_item(gpointer data
, guint action
, GtkWidget
*widget
);
106 static void rename_item(gpointer data
, guint action
, GtkWidget
*widget
);
107 static void link_item(gpointer data
, guint action
, GtkWidget
*widget
);
108 static void open_file(gpointer data
, guint action
, GtkWidget
*widget
);
109 static void help(gpointer data
, guint action
, GtkWidget
*widget
);
110 static void show_file_info(gpointer data
, guint action
, GtkWidget
*widget
);
111 static void mount(gpointer data
, guint action
, GtkWidget
*widget
);
112 static void delete(gpointer data
, guint action
, GtkWidget
*widget
);
113 static void remove_link(gpointer data
, guint action
, GtkWidget
*widget
);
114 static void usage(gpointer data
, guint action
, GtkWidget
*widget
);
115 static void chmod_items(gpointer data
, guint action
, GtkWidget
*widget
);
116 static void find(gpointer data
, guint action
, GtkWidget
*widget
);
118 static void open_vfs_rpm(gpointer data
, guint action
, GtkWidget
*widget
);
119 static void open_vfs_utar(gpointer data
, guint action
, GtkWidget
*widget
);
120 static void open_vfs_uzip(gpointer data
, guint action
, GtkWidget
*widget
);
122 static void select_all(gpointer data
, guint action
, GtkWidget
*widget
);
123 static void clear_selection(gpointer data
, guint action
, GtkWidget
*widget
);
124 static void new_directory(gpointer data
, guint action
, GtkWidget
*widget
);
125 static void xterm_here(gpointer data
, guint action
, GtkWidget
*widget
);
127 static void open_parent_same(gpointer data
, guint action
, GtkWidget
*widget
);
128 static void open_parent(gpointer data
, guint action
, GtkWidget
*widget
);
129 static void new_window(gpointer data
, guint action
, GtkWidget
*widget
);
130 static void close_window(gpointer data
, guint action
, GtkWidget
*widget
);
131 static void enter_path(gpointer data
, guint action
, GtkWidget
*widget
);
132 static void shell_command(gpointer data
, guint action
, GtkWidget
*widget
);
133 static void run_action(gpointer data
, guint action
, GtkWidget
*widget
);
134 static void select_if(gpointer data
, guint action
, GtkWidget
*widget
);
136 static void pin_help(gpointer data
, guint action
, GtkWidget
*widget
);
137 static void pin_remove(gpointer data
, guint action
, GtkWidget
*widget
);
139 static GtkWidget
*create_options();
140 static void update_options();
141 static void set_options();
142 static void save_options();
144 static OptionsSection options
=
154 static GtkWidget
*filer_menu
; /* The popup filer menu */
155 static GtkWidget
*filer_file_item
; /* The File '' label */
156 static GtkWidget
*filer_file_menu
; /* The File '' menu */
157 static GtkWidget
*filer_vfs_menu
; /* The Open VFS menu */
158 static GtkWidget
*filer_hidden_menu
; /* The Show Hidden item */
159 static GtkWidget
*filer_new_window
; /* The New Window item */
160 static GtkWidget
*pinboard_menu
; /* The popup pinboard menu */
162 /* Used for Copy, etc */
163 static GtkWidget
*savebox
= NULL
;
164 static guchar
*current_path
= NULL
;
165 static gboolean (*current_savebox_callback
)(guchar
*current
, guchar
*new);
170 static GtkItemFactoryEntry filer_menu_def
[] = {
171 {N_("Display"), NULL
, NULL
, 0, "<Branch>"},
172 {">" N_("Large Icons"), NULL
, large
, 0, NULL
},
173 {">" N_("Small Icons"), NULL
, small
, 0, NULL
},
174 {">" N_("Large, With..."), NULL
, NULL
, 0, "<Branch>"},
175 {">>" N_("Summary"), NULL
, large_with
, DETAILS_SUMMARY
, NULL
},
176 {">>" N_("Sizes"), NULL
, large_with
, DETAILS_SIZE
, NULL
},
177 {">>" N_("Size Bars"), NULL
, large_with
, DETAILS_SIZE_BARS
, NULL
},
178 {">" N_("Small, With..."), NULL
, NULL
, 0, "<Branch>"},
179 {">>" N_("Summary"), NULL
, small_with
, DETAILS_SUMMARY
, NULL
},
180 {">>" N_("Sizes"), NULL
, small_with
, DETAILS_SIZE
, NULL
},
181 {">>" N_("Size Bars"), NULL
, small_with
, DETAILS_SIZE_BARS
, NULL
},
182 {">", NULL
, NULL
, 0, "<Separator>"},
183 {">" N_("Sort by Name"), NULL
, sort_name
, 0, NULL
},
184 {">" N_("Sort by Type"), NULL
, sort_type
, 0, NULL
},
185 {">" N_("Sort by Date"), NULL
, sort_date
, 0, NULL
},
186 {">" N_("Sort by Size"), NULL
, sort_size
, 0, NULL
},
187 {">", NULL
, NULL
, 0, "<Separator>"},
188 {">" N_("Show Hidden"), NULL
, hidden
, 0, "<ToggleItem>"},
189 {">" N_("Refresh"), NULL
, refresh
, 0, NULL
},
190 {N_("File"), NULL
, NULL
, 0, "<Branch>"},
191 {">" N_("Copy..."), NULL
, copy_item
, 0, NULL
},
192 {">" N_("Rename..."), NULL
, rename_item
, 0, NULL
},
193 {">" N_("Link..."), NULL
, link_item
, 0, NULL
},
194 {">" N_("Shift Open"), NULL
, open_file
, 0, NULL
},
195 {">" N_("Help"), NULL
, help
, 0, NULL
},
196 {">" N_("Info"), NULL
, show_file_info
, 0, NULL
},
197 {">" N_("Open VFS"), NULL
, NULL
, 0, "<Branch>"},
198 {">>" N_("Unzip"), NULL
, open_vfs_uzip
, 0, NULL
},
199 {">>" N_("Untar"), NULL
, open_vfs_utar
, 0, NULL
},
200 {">>" N_("RPM"), NULL
, open_vfs_rpm
, 0, NULL
},
201 {">", NULL
, NULL
, 0, "<Separator>"},
202 {">" N_("Mount"), NULL
, mount
, 0, NULL
},
203 {">" N_("Delete"), NULL
, delete, 0, NULL
},
204 {">" N_("Disk Usage"), NULL
, usage
, 0, NULL
},
205 {">" N_("Permissions"), NULL
, chmod_items
, 0, NULL
},
206 {">" N_("Find"), NULL
, find
, 0, NULL
},
207 {N_("Select All"), NULL
, select_all
, 0, NULL
},
208 {N_("Clear Selection"), NULL
, clear_selection
, 0, NULL
},
209 {N_("Options..."), NULL
, menu_show_options
, 0, NULL
},
210 {N_("New Directory..."), NULL
, new_directory
, 0, NULL
},
211 {N_("Xterm Here"), NULL
, xterm_here
, 0, NULL
},
212 {N_("Window"), NULL
, NULL
, 0, "<Branch>"},
213 {">" N_("Parent, New Window"), NULL
, open_parent
, 0, NULL
},
214 {">" N_("Parent, Same Window"), NULL
, open_parent_same
, 0, NULL
},
215 {">" N_("New Window"), NULL
, new_window
, 0, NULL
},
216 {">" N_("Close Window"), NULL
, close_window
, 0, NULL
},
217 {">", NULL
, NULL
, 0, "<Separator>"},
218 {">" N_("Enter Path..."), NULL
, enter_path
, 0, NULL
},
219 {">" N_("Shell Command..."), NULL
, shell_command
, 0, NULL
},
220 {">" N_("Set Run Action..."), NULL
, run_action
, 0, NULL
},
221 {">" N_("Select If..."), NULL
, select_if
, 0, NULL
},
222 {">", NULL
, NULL
, 0, "<Separator>"},
223 {">" N_("Show ROX-Filer Help"), NULL
, menu_rox_help
, 0, NULL
},
226 static GtkItemFactoryEntry pinboard_menu_def
[] = {
227 {N_("ROX-Filer Help"), NULL
, menu_rox_help
, 0, NULL
},
228 {N_("ROX-Filer Options..."), NULL
, menu_show_options
, 0, NULL
},
229 {"", NULL
, NULL
, 0, "<Separator>"},
230 {N_("Show Help"), NULL
, pin_help
, 0, NULL
},
231 {N_("Remove Item(s)"), NULL
, pin_remove
, 0, NULL
},
235 typedef struct _FileStatus FileStatus
;
237 /* This is for the 'file(1) says...' thing */
240 int fd
; /* FD to read from, -1 if closed */
241 int input
; /* Input watcher tag if fd valid */
242 GtkLabel
*label
; /* Widget to output to */
243 gboolean start
; /* No output yet */
246 #define GET_MENU_ITEM(var, menu) \
247 var = gtk_item_factory_get_widget(item_factory, "<" menu ">");
249 #define GET_SMENU_ITEM(var, menu, sub) \
251 tmp = g_strdup_printf("<" menu ">/%s", _(sub)); \
252 var = gtk_item_factory_get_widget(item_factory, tmp); \
256 #define GET_SSMENU_ITEM(var, menu, sub, subsub) \
258 tmp = g_strdup_printf("<" menu ">/%s/%s", _(sub), _(subsub)); \
259 var = gtk_item_factory_get_widget(item_factory, tmp); \
263 /* Creates menu <name> from the <name>_menu_def array.
264 * The accel group <name>_keys must also have been created.
265 * All menu items are translated. Sets 'item_factory'.
267 #define MAKE_MENU(name) \
269 GtkItemFactoryEntry *translated; \
272 item_factory = gtk_item_factory_new(GTK_TYPE_MENU, \
273 "<" #name ">", name ## _keys); \
275 n_entries = sizeof(name ## _menu_def) / sizeof(*name ## _menu_def); \
276 translated = translate_entries(name ## _menu_def, n_entries); \
277 gtk_item_factory_create_items(item_factory, n_entries, \
279 free_translated_entries(translated, n_entries); \
288 GtkItemFactory
*item_factory
;
290 filer_keys
= gtk_accel_group_new();
293 GET_MENU_ITEM(filer_menu
, "filer");
294 GET_SMENU_ITEM(filer_file_menu
, "filer", "File");
295 GET_SSMENU_ITEM(filer_vfs_menu
, "filer", "File", "Open VFS");
296 GET_SSMENU_ITEM(filer_hidden_menu
, "filer", "Display", "Show Hidden");
298 items
= gtk_container_children(GTK_CONTAINER(filer_menu
));
299 filer_file_item
= GTK_BIN(g_list_nth(items
, 1)->data
)->child
;
302 GET_SSMENU_ITEM(item
, "filer", "Window", "New Window");
303 filer_new_window
= GTK_BIN(item
)->child
;
305 menurc
= choices_find_path_load("menus", PROJECT
);
308 gtk_item_factory_parse_rc(menurc
);
309 mark_menus_modified(FALSE
);
312 pinboard_keys
= gtk_accel_group_new();
314 gtk_accel_group_lock(pinboard_keys
);
315 GET_MENU_ITEM(pinboard_menu
, "pinboard");
317 gtk_signal_connect(GTK_OBJECT(pinboard_menu
), "unmap_event",
318 GTK_SIGNAL_FUNC(pin_menu_closed
), NULL
);
319 gtk_signal_connect(GTK_OBJECT(filer_menu
), "unmap_event",
320 GTK_SIGNAL_FUNC(menu_closed
), NULL
);
321 gtk_signal_connect(GTK_OBJECT(filer_file_menu
), "unmap_event",
322 GTK_SIGNAL_FUNC(menu_closed
), NULL
);
324 options_sections
= g_slist_prepend(options_sections
, &options
);
325 xterm_here_value
= g_strdup("xterm");
326 option_register("xterm_here", load_xterm_here
);
328 savebox
= gtk_savebox_new();
329 gtk_signal_connect_object(GTK_OBJECT(savebox
), "save_to_file",
330 GTK_SIGNAL_FUNC(save_to_file
), NULL
);
331 gtk_signal_connect_object(GTK_OBJECT(savebox
), "save_done",
332 GTK_SIGNAL_FUNC(gtk_widget_hide
),
333 GTK_OBJECT(savebox
));
338 /* Name is in the form "<panel>" */
339 GtkWidget
*menu_create(GtkItemFactoryEntry
*def
, int n_entries
, guchar
*name
)
341 GtkItemFactory
*item_factory
;
342 GtkItemFactoryEntry
*translated
;
346 keys
= gtk_accel_group_new();
348 item_factory
= gtk_item_factory_new(GTK_TYPE_MENU
, name
, keys
);
350 translated
= translate_entries(def
, n_entries
);
351 gtk_item_factory_create_items(item_factory
, n_entries
,
353 free_translated_entries(translated
, n_entries
);
355 menu
= gtk_item_factory_get_widget(item_factory
, name
);
357 gtk_accel_group_lock(keys
);
359 gtk_signal_connect(GTK_OBJECT(menu
), "unmap_event",
360 GTK_SIGNAL_FUNC(pin_menu_closed
), NULL
);
365 /* Build up some option widgets to go in the options dialog, but don't
368 static GtkWidget
*create_options()
370 GtkWidget
*table
, *label
;
372 table
= gtk_table_new(2, 2, FALSE
);
373 gtk_container_set_border_width(GTK_CONTAINER(table
), 4);
375 label
= gtk_label_new(
376 _("To set the keyboard short-cuts, simply open "
377 "the menu over a filer window, move the pointer over "
378 "the item you want to use and press a key. The key "
379 "will appear next to the menu item and you can just "
380 "press that key without opening the menu in future."));
381 gtk_label_set_line_wrap(GTK_LABEL(label
), TRUE
);
382 gtk_table_attach_defaults(GTK_TABLE(table
), label
, 0, 2, 0, 1);
384 label
= gtk_label_new(_("'Xterm here' program:"));
385 gtk_table_attach_defaults(GTK_TABLE(table
), label
, 0, 1, 1, 2);
386 xterm_here_entry
= gtk_entry_new();
387 gtk_table_attach_defaults(GTK_TABLE(table
), xterm_here_entry
,
393 static char *load_xterm_here(char *data
)
395 g_free(xterm_here_value
);
396 xterm_here_value
= g_strdup(data
);
400 static void update_options()
402 gtk_entry_set_text(GTK_ENTRY(xterm_here_entry
), xterm_here_value
);
405 static void set_options()
407 g_free(xterm_here_value
);
408 xterm_here_value
= g_strdup(gtk_entry_get_text(
409 GTK_ENTRY(xterm_here_entry
)));
412 static void save_options()
415 option_write("xterm_here", xterm_here_value
);
419 static void items_sensitive(gboolean state
)
424 items
= item
= gtk_container_children(GTK_CONTAINER(filer_file_menu
));
427 gtk_widget_set_sensitive(GTK_BIN(item
->data
)->child
, state
);
432 items
= item
= gtk_container_children(GTK_CONTAINER(filer_vfs_menu
));
435 gtk_widget_set_sensitive(GTK_BIN(item
->data
)->child
, state
);
441 static void position_menu(GtkMenu
*menu
, gint
*x
, gint
*y
, gpointer data
)
443 int *pos
= (int *) data
;
444 GtkRequisition requisition
;
446 gtk_widget_size_request(GTK_WIDGET(menu
), &requisition
);
448 *x
= pos
[0] - (requisition
.width
>> 2);
449 *y
= pos
[1] - (requisition
.height
>> 2);
451 *x
= CLAMP(*x
, 0, screen_width
- requisition
.width
);
452 *y
= CLAMP(*y
, 0, screen_height
- requisition
.height
);
455 /* Display the pinboard menu. Set icon to NULL if no particular icon
458 void show_pinboard_menu(GdkEventButton
*event
, PinIcon
*icon
)
465 if (pinboard_is_selected(icon
))
466 pin_temp_item_selected
= FALSE
;
469 pinboard_select_only(icon
);
470 pin_temp_item_selected
= TRUE
;
474 icons
= pinboard_get_selected();
476 pos
[0] = event
->x_root
;
477 pos
[1] = event
->y_root
;
481 menu_set_items_shaded(pinboard_menu
,
482 icons
->next
? TRUE
: FALSE
, 3, 1);
484 menu_set_items_shaded(pinboard_menu
, FALSE
, 4, 1);
487 menu_set_items_shaded(pinboard_menu
, TRUE
, 3, 2);
489 gtk_menu_popup(GTK_MENU(pinboard_menu
), NULL
, NULL
, position_menu
,
490 (gpointer
) pos
, event
->button
, event
->time
);
493 void show_filer_menu(FilerWindow
*filer_window
, GdkEventButton
*event
,
501 pos
[0] = event
->x_root
;
502 pos
[1] = event
->y_root
;
504 window_with_focus
= filer_window
;
506 if (filer_window
->collection
->number_selected
== 0 && item
>= 0)
508 collection_select_item(filer_window
->collection
, item
);
509 filer_window
->temp_item_selected
= TRUE
;
512 filer_window
->temp_item_selected
= FALSE
;
515 GtkWidget
*file_label
, *file_menu
;
516 Collection
*collection
= filer_window
->collection
;
519 file_label
= filer_file_item
;
520 file_menu
= filer_file_menu
;
521 gtk_check_menu_item_set_active(
522 GTK_CHECK_MENU_ITEM(filer_hidden_menu
),
523 filer_window
->show_hidden
);
524 buffer
= g_string_new(NULL
);
525 switch (collection
->number_selected
)
528 g_string_assign(buffer
, _("Next Click"));
529 items_sensitive(TRUE
);
532 items_sensitive(TRUE
);
533 file_item
= selected_item(
534 filer_window
->collection
);
535 g_string_sprintf(buffer
, "%s '%s'",
536 basetype_name(file_item
),
537 file_item
->leafname
);
540 items_sensitive(FALSE
);
541 g_string_sprintf(buffer
, _("%d items"),
542 collection
->number_selected
);
545 gtk_label_set_text(GTK_LABEL(file_label
), buffer
->str
);
546 g_string_free(buffer
, TRUE
);
550 gtk_widget_set_sensitive(filer_new_window
, !o_unique_filer_windows
);
552 popup_menu
= (event
->state
& GDK_CONTROL_MASK
)
558 gtk_menu_popup(GTK_MENU(popup_menu
), NULL
, NULL
, position_menu
,
559 (gpointer
) pos
, event
->button
, event
->time
);
562 static void pin_menu_closed(GtkWidget
*widget
)
564 if (pin_temp_item_selected
)
565 pinboard_clear_selection();
568 static void menu_closed(GtkWidget
*widget
)
570 if (window_with_focus
== NULL
|| widget
!= popup_menu
)
571 return; /* Close panel item chosen? */
575 if (window_with_focus
->temp_item_selected
)
577 collection_clear_selection(window_with_focus
->collection
);
578 window_with_focus
->temp_item_selected
= FALSE
;
582 void target_callback(Collection
*collection
, gint item
, gpointer real_fn
)
584 g_return_if_fail(window_with_focus
!= NULL
);
585 g_return_if_fail(window_with_focus
->collection
== collection
);
586 g_return_if_fail(real_fn
!= NULL
);
588 collection_wink_item(collection
, item
);
589 collection_clear_selection(collection
);
590 collection_select_item(collection
, item
);
591 ((CollectionTargetFunc
)real_fn
)(NULL
, 0, collection
);
592 if (item
< collection
->number_of_items
)
593 collection_unselect_item(collection
, item
);
598 static void large(gpointer data
, guint action
, GtkWidget
*widget
)
600 g_return_if_fail(window_with_focus
!= NULL
);
602 display_set_layout(window_with_focus
, "Large");
605 static void small(gpointer data
, guint action
, GtkWidget
*widget
)
607 g_return_if_fail(window_with_focus
!= NULL
);
609 display_set_layout(window_with_focus
, "Small");
612 static void set_layout(gboolean large
, DetailsType details
)
616 g_return_if_fail(window_with_focus
!= NULL
);
618 style
= g_strdup_printf("%s+%s",
619 large
? "Large" : "Small",
620 details
== DETAILS_SUMMARY
? "Summary" :
621 details
== DETAILS_SIZE_BARS
? "SizeBars" :
624 display_set_layout(window_with_focus
, style
);
628 static void large_with(gpointer data
, guint action
, GtkWidget
*widget
)
630 set_layout(TRUE
, (DetailsType
) action
);
633 static void small_with(gpointer data
, guint action
, GtkWidget
*widget
)
635 set_layout(FALSE
, (DetailsType
) action
);
638 static void sort_name(gpointer data
, guint action
, GtkWidget
*widget
)
640 g_return_if_fail(window_with_focus
!= NULL
);
642 display_set_sort_fn(window_with_focus
, sort_by_name
);
645 static void sort_type(gpointer data
, guint action
, GtkWidget
*widget
)
647 g_return_if_fail(window_with_focus
!= NULL
);
649 display_set_sort_fn(window_with_focus
, sort_by_type
);
652 static void sort_date(gpointer data
, guint action
, GtkWidget
*widget
)
654 g_return_if_fail(window_with_focus
!= NULL
);
656 display_set_sort_fn(window_with_focus
, sort_by_date
);
659 static void sort_size(gpointer data
, guint action
, GtkWidget
*widget
)
661 g_return_if_fail(window_with_focus
!= NULL
);
663 display_set_sort_fn(window_with_focus
, sort_by_size
);
666 static void hidden(gpointer data
, guint action
, GtkWidget
*widget
)
671 g_return_if_fail(window_with_focus
!= NULL
);
673 display_set_hidden(window_with_focus
, !window_with_focus
->show_hidden
);
676 static void refresh(gpointer data
, guint action
, GtkWidget
*widget
)
678 g_return_if_fail(window_with_focus
!= NULL
);
681 filer_update_dir(window_with_focus
, TRUE
);
684 static void mount(gpointer data
, guint action
, GtkWidget
*widget
)
686 g_return_if_fail(window_with_focus
!= NULL
);
688 if (window_with_focus
->collection
->number_selected
== 0)
689 collection_target(window_with_focus
->collection
,
690 target_callback
, mount
);
695 paths
= list_paths(window_with_focus
);
701 static void delete(gpointer data
, guint action
, GtkWidget
*widget
)
703 g_return_if_fail(window_with_focus
!= NULL
);
705 if (window_with_focus
->collection
->number_selected
== 0)
706 collection_target(window_with_focus
->collection
,
707 target_callback
, delete);
709 action_delete(window_with_focus
);
712 static void remove_link(gpointer data
, guint action
, GtkWidget
*widget
)
714 g_return_if_fail(window_with_focus
!= NULL
);
716 if (window_with_focus
->collection
->number_selected
> 1)
718 report_error(PROJECT
,
719 _("You can only remove one link at a time"));
722 else if (window_with_focus
->collection
->number_selected
== 0)
723 collection_target(window_with_focus
->collection
,
724 target_callback
, remove_link
);
731 item
= selected_item(window_with_focus
->collection
);
733 path
= make_path(window_with_focus
->path
, item
->leafname
)->str
;
734 if (lstat(path
, &info
))
735 report_error(PROJECT
, g_strerror(errno
));
736 else if (!S_ISLNK(info
.st_mode
))
737 report_error(PROJECT
,
738 _("You can only remove symbolic links this way - "
739 "try the 'Open Panel as Directory' item."));
740 else if (unlink(path
))
741 report_error(PROJECT
, g_strerror(errno
));
743 filer_update_dir(window_with_focus
, TRUE
);
747 static void usage(gpointer data
, guint action
, GtkWidget
*widget
)
749 g_return_if_fail(window_with_focus
!= NULL
);
751 if (window_with_focus
->collection
->number_selected
== 0)
752 collection_target(window_with_focus
->collection
,
753 target_callback
, usage
);
755 action_usage(window_with_focus
);
758 static void chmod_items(gpointer data
, guint action
, GtkWidget
*widget
)
760 g_return_if_fail(window_with_focus
!= NULL
);
762 if (window_with_focus
->collection
->number_selected
== 0)
763 collection_target(window_with_focus
->collection
,
764 target_callback
, chmod_items
);
766 action_chmod(window_with_focus
);
769 static void find(gpointer data
, guint action
, GtkWidget
*widget
)
771 g_return_if_fail(window_with_focus
!= NULL
);
773 if (window_with_focus
->collection
->number_selected
== 0)
774 collection_target(window_with_focus
->collection
,
775 target_callback
, find
);
777 action_find(window_with_focus
);
780 /* This pops up our savebox widget, cancelling any currently open one,
781 * and allows the user to pick a new path for it.
782 * Once the new path has been picked, the callback will be called with
783 * both the current and new paths.
785 static void savebox_show(guchar
*title
, guchar
*path
, MaskedPixmap
*image
,
786 gboolean (*callback
)(guchar
*current
, guchar
*new))
788 if (GTK_WIDGET_VISIBLE(savebox
))
789 gtk_widget_hide(savebox
);
792 g_free(current_path
);
793 current_path
= g_strdup(path
);
794 current_savebox_callback
= callback
;
796 gtk_window_set_title(GTK_WINDOW(savebox
), title
);
797 gtk_savebox_set_pathname(GTK_SAVEBOX(savebox
), current_path
);
798 gtk_savebox_set_icon(GTK_SAVEBOX(savebox
), image
->pixmap
, image
->mask
);
800 gtk_widget_grab_focus(GTK_SAVEBOX(savebox
)->entry
);
801 gtk_widget_show(savebox
);
804 static gint
save_to_file(GtkSavebox
*savebox
, guchar
*pathname
)
806 g_return_val_if_fail(current_savebox_callback
!= NULL
,
809 return current_savebox_callback(current_path
, pathname
)
810 ? GTK_XDS_SAVED
: GTK_XDS_SAVE_ERROR
;
813 static gboolean
copy_cb(guchar
*current
, guchar
*new)
815 return action_with_leaf(action_copy
, current
, new);
818 static gboolean
action_with_leaf(ActionFn action
, guchar
*current
, guchar
*new)
820 char *new_dir
, *leaf
;
825 report_error(PROJECT
, _("New pathname is not absolute"));
829 if (new[strlen(new) - 1] == '/')
831 new_dir
= g_strdup(new);
838 slash
= strrchr(new, '/');
839 new_dir
= g_strndup(new, slash
- new);
843 local_paths
= g_slist_append(NULL
, current
);
844 action(local_paths
, new_dir
, leaf
);
845 g_slist_free(local_paths
);
852 #define SHOW_SAVEBOX(title, callback) \
856 item = selected_item(collection); \
857 path = make_path(window_with_focus->path, item->leafname)->str; \
858 savebox_show(title, path, item->image, callback); \
861 static void copy_item(gpointer data
, guint action
, GtkWidget
*widget
)
863 Collection
*collection
;
865 g_return_if_fail(window_with_focus
!= NULL
);
867 collection
= window_with_focus
->collection
;
868 if (collection
->number_selected
> 1)
870 report_error(PROJECT
, _("You cannot do this to more than "
871 "one item at a time"));
874 else if (collection
->number_selected
!= 1)
875 collection_target(collection
, target_callback
, copy_item
);
877 SHOW_SAVEBOX(_("Copy"), copy_cb
);
880 static gboolean
rename_cb(guchar
*current
, guchar
*new)
882 return action_with_leaf(action_move
, current
, new);
885 static void rename_item(gpointer data
, guint action
, GtkWidget
*widget
)
887 Collection
*collection
;
889 g_return_if_fail(window_with_focus
!= NULL
);
891 collection
= window_with_focus
->collection
;
892 if (collection
->number_selected
> 1)
894 report_error(PROJECT
, _("You cannot do this to more than "
895 "one item at a time"));
898 else if (collection
->number_selected
!= 1)
899 collection_target(collection
, target_callback
, rename_item
);
901 SHOW_SAVEBOX(_("Rename"), rename_cb
);
904 static gboolean
link_cb(guchar
*initial
, guchar
*path
)
906 if (symlink(initial
, path
))
908 report_error("ROX-Filer: symlink()", g_strerror(errno
));
914 static void link_item(gpointer data
, guint action
, GtkWidget
*widget
)
916 Collection
*collection
;
918 g_return_if_fail(window_with_focus
!= NULL
);
920 collection
= window_with_focus
->collection
;
921 if (collection
->number_selected
> 1)
923 report_error(PROJECT
, _("You cannot do this to more than "
924 "one item at a time"));
927 else if (collection
->number_selected
!= 1)
928 collection_target(collection
, target_callback
, link_item
);
930 SHOW_SAVEBOX(_("Symlink"), link_cb
);
933 static void open_file(gpointer data
, guint action
, GtkWidget
*widget
)
935 Collection
*collection
;
937 g_return_if_fail(window_with_focus
!= NULL
);
939 collection
= window_with_focus
->collection
;
940 if (collection
->number_selected
> 1)
942 report_error(PROJECT
, _("You cannot do this to more than "
943 "one item at a time"));
946 else if (collection
->number_selected
!= 1)
947 collection_target(collection
, target_callback
, open_file
);
949 filer_openitem(window_with_focus
,
950 selected_item_number(collection
),
951 OPEN_SAME_WINDOW
| OPEN_SHIFT
);
954 /* Got some data from file(1) - stick it in the window. */
955 static void add_file_output(FileStatus
*fs
,
956 gint source
, GdkInputCondition condition
)
962 got
= read(source
, buffer
, sizeof(buffer
) - 1);
966 gtk_input_remove(fs
->input
);
970 delayed_error(_("ROX-Filer: file(1) says..."),
982 gtk_label_get(fs
->label
, &str
);
984 str
= g_strconcat(str
, buffer
, NULL
);
985 gtk_label_set_text(fs
->label
, str
);
989 static void file_info_destroyed(GtkWidget
*widget
, FileStatus
*fs
)
993 gtk_input_remove(fs
->input
);
1000 /* g_free() the result */
1001 guchar
*pretty_type(DirItem
*file
, guchar
*path
)
1003 if (file
->flags
& ITEM_FLAG_SYMLINK
)
1005 char p
[MAXPATHLEN
+ 1];
1007 got
= readlink(path
, p
, MAXPATHLEN
);
1008 if (got
> 0 && got
<= MAXPATHLEN
)
1011 return g_strconcat(_("Symbolic link to "), p
, NULL
);
1014 return g_strdup(_("Symbolic link"));
1017 if (file
->flags
& ITEM_FLAG_EXEC_FILE
)
1018 return g_strdup(_("Executable file"));
1020 if (file
->flags
& ITEM_FLAG_APPDIR
)
1021 return g_strdup(_("ROX application"));
1023 if (file
->flags
& ITEM_FLAG_MOUNT_POINT
)
1026 if ((file
->flags
& ITEM_FLAG_MOUNTED
) &&
1027 (mp
= g_hash_table_lookup(mtab_mounts
, path
)))
1028 return g_strconcat(_("Mount point for "),
1031 return g_strdup(_("Mount point"));
1034 if (file
->mime_type
)
1035 return g_strconcat(file
->mime_type
->media_type
, "/",
1036 file
->mime_type
->subtype
, NULL
);
1038 return g_strdup("-");
1041 #define LABEL(text, row) \
1042 label = gtk_label_new(text); \
1043 gtk_misc_set_alignment(GTK_MISC(label), 1, .5); \
1044 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_RIGHT); \
1045 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, row, row + 1);
1047 #define VALUE(label, row) \
1048 gtk_misc_set_alignment(GTK_MISC(label), 0, .5); \
1049 gtk_table_attach_defaults(GTK_TABLE(table), label, 1, 2, row, row + 1);
1051 static void show_file_info(gpointer data
, guint action
, GtkWidget
*widget
)
1053 GtkWidget
*window
, *table
, *label
, *button
, *frame
, *value
;
1054 GtkWidget
*file_label
;
1058 char *argv
[] = {"file", "-b", NULL
, NULL
};
1059 Collection
*collection
;
1062 FileStatus
*fs
= NULL
;
1065 g_return_if_fail(window_with_focus
!= NULL
);
1067 collection
= window_with_focus
->collection
;
1068 if (collection
->number_selected
> 1)
1070 report_error(PROJECT
, _("You cannot do this to more than "
1071 "one item at a time"));
1074 else if (collection
->number_selected
!= 1)
1076 collection_target(collection
, target_callback
, show_file_info
);
1079 file
= selected_item(collection
);
1080 path
= make_path(window_with_focus
->path
,
1081 file
->leafname
)->str
;
1082 if (lstat(path
, &info
))
1084 delayed_error(PROJECT
, g_strerror(errno
));
1088 gstring
= g_string_new(NULL
);
1090 window
= gtk_window_new(GTK_WINDOW_DIALOG
);
1091 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_MOUSE
);
1092 gtk_container_set_border_width(GTK_CONTAINER(window
), 4);
1093 gtk_window_set_title(GTK_WINDOW(window
), path
);
1095 table
= gtk_table_new(10, 2, FALSE
);
1096 gtk_container_add(GTK_CONTAINER(window
), table
);
1097 gtk_table_set_row_spacings(GTK_TABLE(table
), 8);
1098 gtk_table_set_col_spacings(GTK_TABLE(table
), 4);
1100 value
= gtk_label_new(file
->leafname
);
1101 LABEL(_("Name:"), 0);
1104 g_string_sprintf(gstring
, "%s, %s", user_name(info
.st_uid
),
1105 group_name(info
.st_gid
));
1106 value
= gtk_label_new(gstring
->str
);
1107 LABEL(_("Owner, Group:"), 1);
1110 if (info
.st_size
>= PRETTY_SIZE_LIMIT
)
1112 g_string_sprintf(gstring
, "%s (%ld %s)",
1113 format_size((unsigned long) info
.st_size
),
1114 (unsigned long) info
.st_size
, _("bytes"));
1118 g_string_assign(gstring
,
1119 format_size((unsigned long) info
.st_size
));
1121 value
= gtk_label_new(gstring
->str
);
1122 LABEL(_("Size:"), 2);
1125 value
= gtk_label_new(pretty_time(&info
.st_ctime
));
1126 LABEL(_("Change time:"), 3);
1129 value
= gtk_label_new(pretty_time(&info
.st_mtime
));
1130 LABEL(_("Modify time:"), 4);
1133 value
= gtk_label_new(pretty_time(&info
.st_atime
));
1134 LABEL(_("Access time:"), 5);
1137 value
= gtk_label_new(pretty_permissions(info
.st_mode
));
1138 perm
= applicable(info
.st_uid
, info
.st_gid
);
1139 gtk_label_set_pattern(GTK_LABEL(value
),
1140 perm
== 0 ? "___ " :
1141 perm
== 1 ? " ___ " :
1143 gtk_widget_set_style(value
, fixed_style
);
1144 LABEL(_("Permissions:"), 6);
1147 tmp
= pretty_type(file
, path
);
1148 value
= gtk_label_new(tmp
);
1150 LABEL(_("Type:"), 7);
1153 frame
= gtk_frame_new(_("file(1) says..."));
1154 gtk_table_attach_defaults(GTK_TABLE(table
), frame
, 0, 2, 8, 9);
1155 file_label
= gtk_label_new(_("<nothing yet>"));
1156 gtk_misc_set_padding(GTK_MISC(file_label
), 4, 4);
1157 gtk_label_set_line_wrap(GTK_LABEL(file_label
), TRUE
);
1158 gtk_container_add(GTK_CONTAINER(frame
), file_label
);
1160 button
= gtk_button_new_with_label(_("OK"));
1161 gtk_window_set_focus(GTK_WINDOW(window
), button
);
1162 gtk_table_attach(GTK_TABLE(table
), button
, 0, 2, 10, 11,
1163 GTK_EXPAND
| GTK_FILL
| GTK_SHRINK
, 0, 40, 4);
1164 gtk_signal_connect_object(GTK_OBJECT(button
), "clicked",
1165 gtk_widget_destroy
, GTK_OBJECT(window
));
1167 gtk_widget_show_all(window
);
1170 if (pipe(file_data
))
1172 g_string_sprintf(gstring
, "pipe(): %s", g_strerror(errno
));
1173 g_string_free(gstring
, TRUE
);
1179 g_string_sprintf(gstring
, "fork(): %s",
1181 gtk_label_set_text(GTK_LABEL(file_label
), gstring
->str
);
1182 g_string_free(gstring
, TRUE
);
1183 close(file_data
[0]);
1184 close(file_data
[1]);
1187 /* We are the child */
1188 close(file_data
[0]);
1189 dup2(file_data
[1], STDOUT_FILENO
);
1190 dup2(file_data
[1], STDERR_FILENO
);
1194 argv
[1] = file
->leafname
;
1195 chdir(window_with_focus
->path
);
1197 if (execvp(argv
[0], argv
))
1198 fprintf(stderr
, "execvp() error: %s\n",
1202 /* We are the parent */
1203 close(file_data
[1]);
1204 fs
= g_new(FileStatus
, 1);
1205 fs
->label
= GTK_LABEL(file_label
);
1206 fs
->fd
= file_data
[0];
1208 fs
->input
= gdk_input_add(fs
->fd
, GDK_INPUT_READ
,
1209 (GdkInputFunction
) add_file_output
, fs
);
1210 gtk_signal_connect(GTK_OBJECT(window
), "destroy",
1211 GTK_SIGNAL_FUNC(file_info_destroyed
), fs
);
1212 g_string_free(gstring
, TRUE
);
1217 static void help(gpointer data
, guint action
, GtkWidget
*widget
)
1219 Collection
*collection
;
1222 g_return_if_fail(window_with_focus
!= NULL
);
1224 collection
= window_with_focus
->collection
;
1225 if (collection
->number_selected
> 1)
1227 report_error(PROJECT
, _("You cannot do this to more than "
1228 "one item at a time"));
1231 else if (collection
->number_selected
!= 1)
1233 collection_target(collection
, target_callback
, help
);
1236 item
= selected_item(collection
);
1238 show_item_help(make_path(window_with_focus
->path
, item
->leafname
)->str
,
1242 #define OPEN_VFS(fs) \
1243 static void open_vfs_ ## fs (gpointer data, guint action, GtkWidget *widget) \
1245 Collection *collection; \
1247 g_return_if_fail(window_with_focus != NULL); \
1249 collection = window_with_focus->collection; \
1250 if (collection->number_selected < 1) \
1251 collection_target(collection, target_callback, \
1254 real_vfs_open(#fs); \
1257 static void real_vfs_open(char *fs
)
1262 if (window_with_focus
->collection
->number_selected
!= 1)
1264 report_error(PROJECT
, _("You must select a single file "
1265 "to open as a Virtual File System"));
1269 item
= selected_item(window_with_focus
->collection
);
1271 path
= g_strconcat(window_with_focus
->path
,
1276 filer_change_to(window_with_focus
, path
, NULL
);
1284 static void select_all(gpointer data
, guint action
, GtkWidget
*widget
)
1286 g_return_if_fail(window_with_focus
!= NULL
);
1288 collection_select_all(window_with_focus
->collection
);
1289 window_with_focus
->temp_item_selected
= FALSE
;
1292 static void clear_selection(gpointer data
, guint action
, GtkWidget
*widget
)
1294 g_return_if_fail(window_with_focus
!= NULL
);
1296 collection_clear_selection(window_with_focus
->collection
);
1297 window_with_focus
->temp_item_selected
= FALSE
;
1300 void menu_show_options(gpointer data
, guint action
, GtkWidget
*widget
)
1305 static gboolean
new_directory_cb(guchar
*initial
, guchar
*path
)
1307 if (mkdir(path
, S_IRWXU
| S_IRWXG
| S_IRWXO
))
1309 report_error("mkdir", g_strerror(errno
));
1313 dir_check_this(path
);
1317 static void new_directory(gpointer data
, guint action
, GtkWidget
*widget
)
1319 g_return_if_fail(window_with_focus
!= NULL
);
1321 savebox_show(_("New Directory"),
1322 make_path(window_with_focus
->path
, _("NewDir"))->str
,
1323 type_to_icon(&special_directory
),
1327 static void xterm_here(gpointer data
, guint action
, GtkWidget
*widget
)
1329 char *argv
[] = {NULL
, NULL
};
1331 argv
[0] = xterm_here_value
;
1333 g_return_if_fail(window_with_focus
!= NULL
);
1335 if (!spawn_full(argv
, window_with_focus
->path
))
1336 report_error(PROJECT
, _("Failed to fork() child process"));
1339 static void open_parent(gpointer data
, guint action
, GtkWidget
*widget
)
1341 g_return_if_fail(window_with_focus
!= NULL
);
1343 filer_open_parent(window_with_focus
);
1346 static void open_parent_same(gpointer data
, guint action
, GtkWidget
*widget
)
1348 g_return_if_fail(window_with_focus
!= NULL
);
1350 change_to_parent(window_with_focus
);
1353 static void new_window(gpointer data
, guint action
, GtkWidget
*widget
)
1355 g_return_if_fail(window_with_focus
!= NULL
);
1357 if (o_unique_filer_windows
)
1358 report_error(PROJECT
, _("You can't open a second view onto "
1359 "this directory because the `Unique Windows' option "
1360 "is turned on in the Options window."));
1362 filer_opendir(window_with_focus
->path
);
1365 static void close_window(gpointer data
, guint action
, GtkWidget
*widget
)
1367 g_return_if_fail(window_with_focus
!= NULL
);
1369 gtk_widget_destroy(window_with_focus
->window
);
1372 static void enter_path(gpointer data
, guint action
, GtkWidget
*widget
)
1374 g_return_if_fail(window_with_focus
!= NULL
);
1376 minibuffer_show(window_with_focus
, MINI_PATH
);
1379 static void shell_command(gpointer data
, guint action
, GtkWidget
*widget
)
1381 g_return_if_fail(window_with_focus
!= NULL
);
1383 minibuffer_show(window_with_focus
, MINI_SHELL
);
1386 static void run_action(gpointer data
, guint action
, GtkWidget
*widget
)
1388 g_return_if_fail(window_with_focus
!= NULL
);
1390 minibuffer_show(window_with_focus
, MINI_RUN_ACTION
);
1393 static void select_if(gpointer data
, guint action
, GtkWidget
*widget
)
1395 g_return_if_fail(window_with_focus
!= NULL
);
1397 minibuffer_show(window_with_focus
, MINI_SELECT_IF
);
1400 void menu_rox_help(gpointer data
, guint action
, GtkWidget
*widget
)
1402 filer_opendir(make_path(app_dir
, "Help")->str
);
1405 /* Return a list of full paths of all the selected items */
1406 static GList
*list_paths(FilerWindow
*filer_window
)
1408 Collection
*collection
= filer_window
->collection
;
1409 GList
*paths
= NULL
;
1412 for (i
= 0; i
< collection
->number_of_items
; i
++)
1414 CollectionItem
*colitem
= &collection
->items
[i
];
1416 if (colitem
->selected
)
1418 DirItem
*item
= (DirItem
*) colitem
->data
;
1420 paths
= g_list_prepend(paths
,
1421 g_strdup(make_path(filer_window
->path
,
1422 item
->leafname
)->str
));
1429 static void free_paths(GList
*paths
)
1436 for (next
= paths
; next
; next
= next
->next
)
1442 static void pin_help(gpointer data
, guint action
, GtkWidget
*widget
)
1446 icon
= pinboard_selected_icon();
1449 pinboard_show_help(icon
);
1451 delayed_error(PROJECT
,
1452 _("You must first select a single pinned icon to get "
1456 static void pin_remove(gpointer data
, guint action
, GtkWidget
*widget
)
1458 pinboard_unpin_selection();
1461 /* Set n items from position 'from' in 'menu' to the 'shaded' state */
1462 void menu_set_items_shaded(GtkWidget
*menu
, gboolean shaded
, int from
, int n
)
1464 GList
*items
, *item
;
1466 items
= gtk_container_children(GTK_CONTAINER(menu
));
1468 item
= g_list_nth(items
, from
);
1471 gtk_widget_set_sensitive(GTK_BIN(item
->data
)->child
, !shaded
);
1477 /* This is called for every modified menu entry. We just use it to
1478 * find out if the menu has changed at all.
1480 static void set_mod(gboolean
*mod
, guchar
*str
)
1482 if (str
&& str
[0] == '(')
1486 static void save_menus(void)
1490 menurc
= choices_find_path_save("menus", PROJECT
, TRUE
);
1493 gboolean mod
= FALSE
;
1495 /* Find out if anything changed... */
1496 gtk_item_factory_dump_items(NULL
, TRUE
,
1497 (GtkPrintFunc
) set_mod
, &mod
);
1499 /* Dump out if so... */
1502 mark_menus_modified(TRUE
);
1503 gtk_item_factory_dump_rc(menurc
, NULL
, TRUE
);
1504 mark_menus_modified(FALSE
);
1509 static void mark_modified(gpointer hash_key
,
1513 GtkItemFactoryItem
*item
= (GtkItemFactoryItem
*) value
;
1515 item
->modified
= (gboolean
) user_data
;
1518 /* Set or clear the 'modified' flag in all menu items. Messy... */
1519 static void mark_menus_modified(gboolean mod
)
1521 GtkItemFactoryClass
*class;
1523 class = gtk_type_class(GTK_TYPE_ITEM_FACTORY
);
1525 g_hash_table_foreach(class->item_ht
, mark_modified
, (gpointer
) mod
);