4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2003, the ROX-Filer team.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
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 /* toolbar.c - for the button bars that go along the tops of windows */
43 #include "view_iface.h"
44 #include "bookmarks.h"
45 #include "gui_support.h"
47 typedef struct _Tool Tool
;
49 typedef enum {DROP_NONE
, DROP_TO_PARENT
, DROP_TO_HOME
, DROP_BOOKMARK
} DropDest
;
54 const gchar
*tip
; /* Tooltip */
55 void (*clicked
)(GtkWidget
*w
, FilerWindow
*filer_window
);
58 gboolean menu
; /* Activate on button-press */
61 Option o_toolbar
, o_toolbar_info
, o_toolbar_disable
;
63 static GtkTooltips
*tooltips
= NULL
;
65 /* TRUE if the button presses (or released) should open a new window,
66 * rather than reusing the existing one.
68 #define NEW_WIN_BUTTON(button_event) \
69 (o_new_button_1.int_value \
70 ? ((GdkEventButton *) button_event)->button == 1 \
71 : ((GdkEventButton *) button_event)->button != 1)
73 /* Static prototypes */
74 static void toolbar_close_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
);
75 static void toolbar_up_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
);
76 static void toolbar_home_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
);
77 static void toolbar_bookmarks_clicked(GtkWidget
*widget
,
78 FilerWindow
*filer_window
);
79 static void toolbar_help_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
);
80 static void toolbar_refresh_clicked(GtkWidget
*widget
,
81 FilerWindow
*filer_window
);
82 static void toolbar_size_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
);
83 static void toolbar_autosize_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
);
84 static void toolbar_details_clicked(GtkWidget
*widget
,
85 FilerWindow
*filer_window
);
86 static void toolbar_hidden_clicked(GtkWidget
*widget
,
87 FilerWindow
*filer_window
);
88 static void toolbar_select_clicked(GtkWidget
*widget
,
89 FilerWindow
*filer_window
);
90 static void toolbar_sort_clicked(GtkWidget
*widget
,
91 FilerWindow
*filer_window
);
92 static GtkWidget
*add_button(GtkWidget
*bar
, Tool
*tool
,
93 FilerWindow
*filer_window
);
94 static GtkWidget
*create_toolbar(FilerWindow
*filer_window
);
95 static gboolean
drag_motion(GtkWidget
*widget
,
96 GdkDragContext
*context
,
100 FilerWindow
*filer_window
);
101 static void drag_leave(GtkWidget
*widget
,
102 GdkDragContext
*context
,
104 FilerWindow
*filer_window
);
105 static void handle_drops(FilerWindow
*filer_window
,
108 static void toggle_selected(GtkToggleButton
*widget
, gpointer data
);
109 static void option_notify(void);
110 static GList
*build_tool_options(Option
*option
, xmlNode
*node
, guchar
*label
);
111 static void tally_items(gpointer key
, gpointer value
, gpointer data
);
113 static Tool all_tools
[] = {
114 {N_("Close"), GTK_STOCK_CLOSE
, N_("Close filer window"),
115 toolbar_close_clicked
, DROP_NONE
, FALSE
,
118 {N_("Up"), GTK_STOCK_GO_UP
, N_("Change to parent directory"),
119 toolbar_up_clicked
, DROP_TO_PARENT
, TRUE
,
122 {N_("Home"), GTK_STOCK_HOME
, N_("Change to home directory"),
123 toolbar_home_clicked
, DROP_TO_HOME
, TRUE
,
126 {N_("Bookmarks"), ROX_STOCK_BOOKMARKS
, N_("Bookmarks menu"),
127 toolbar_bookmarks_clicked
, DROP_BOOKMARK
, FALSE
,
130 {N_("Scan"), GTK_STOCK_REFRESH
, N_("Rescan directory contents"),
131 toolbar_refresh_clicked
, DROP_NONE
, TRUE
,
134 {N_("Size"), GTK_STOCK_ZOOM_IN
, N_("Change icon size"),
135 toolbar_size_clicked
, DROP_NONE
, TRUE
,
138 {N_("Size"), GTK_STOCK_ZOOM_FIT
, N_("Automatic size mode"),
139 toolbar_autosize_clicked
, DROP_NONE
, TRUE
,
142 {N_("Details"), ROX_STOCK_SHOW_DETAILS
, N_("Show extra details"),
143 toolbar_details_clicked
, DROP_NONE
, TRUE
,
146 {N_("Sort"), GTK_STOCK_SORT_ASCENDING
, N_("Change sort criteria"),
147 toolbar_sort_clicked
, DROP_NONE
, FALSE
,
150 {N_("Hidden"), ROX_STOCK_SHOW_HIDDEN
, N_("Show/hide hidden files"),
151 toolbar_hidden_clicked
, DROP_NONE
, TRUE
,
154 {N_("Select"), ROX_STOCK_SELECT
, N_("Select all/invert selection"),
155 toolbar_select_clicked
, DROP_NONE
, FALSE
,
158 {N_("Help"), GTK_STOCK_HELP
, N_("Show ROX-Filer help"),
159 toolbar_help_clicked
, DROP_NONE
, TRUE
,
164 /****************************************************************
165 * EXTERNAL INTERFACE *
166 ****************************************************************/
168 void toolbar_init(void)
170 option_add_int(&o_toolbar
, "toolbar_type", TOOLBAR_NORMAL
);
171 option_add_int(&o_toolbar_info
, "toolbar_show_info", 1);
172 option_add_string(&o_toolbar_disable
, "toolbar_disable",
174 option_add_notify(option_notify
);
176 tooltips
= gtk_tooltips_new();
178 option_register_widget("tool-options", build_tool_options
);
181 void toolbar_update_info(FilerWindow
*filer_window
)
187 if (o_toolbar
.int_value
== TOOLBAR_NONE
|| !o_toolbar_info
.int_value
)
188 return; /* Not showing info */
190 if (filer_window
->target_cb
)
193 view
= filer_window
->view
;
195 n_selected
= view_count_selected(view
);
202 if (filer_window
->scanning
)
205 GTK_LABEL(filer_window
->toolbar_text
), "");
209 if (!filer_window
->show_hidden
)
211 GHashTable
*hash
= filer_window
->directory
->known_items
;
214 g_hash_table_foreach(hash
, tally_items
, &tally
);
217 s
= g_strdup_printf(_(" (%u hidden)"), tally
);
220 n_items
= view_count_items(view
);
223 label
= g_strdup_printf("%d %s%s",
225 n_items
!= 1 ? _("items") : _("item"),
227 else /* (French plurals work differently for zero) */
228 label
= g_strdup_printf(_("No items%s"),
238 view_get_iter(filer_window
->view
, &iter
, VIEW_ITER_SELECTED
);
240 while ((item
= iter
.next(&iter
)))
241 if (item
->base_type
!= TYPE_DIRECTORY
)
242 size
+= (double) item
->size
;
244 label
= g_strdup_printf(_("%u selected (%s)"),
245 n_selected
, format_double_size(size
));
248 gtk_label_set_text(GTK_LABEL(filer_window
->toolbar_text
), label
);
252 /* Create, destroy or recreate toolbar for this window so that it
253 * matches the option setting.
255 void toolbar_update_toolbar(FilerWindow
*filer_window
)
257 g_return_if_fail(filer_window
!= NULL
);
259 if (filer_window
->toolbar
)
261 gtk_widget_destroy(filer_window
->toolbar
);
262 filer_window
->toolbar
= NULL
;
263 filer_window
->toolbar_text
= NULL
;
266 if (o_toolbar
.int_value
!= TOOLBAR_NONE
)
268 filer_window
->toolbar
= create_toolbar(filer_window
);
269 gtk_box_pack_start(filer_window
->toplevel_vbox
,
270 filer_window
->toolbar
, FALSE
, TRUE
, 0);
271 gtk_box_reorder_child(filer_window
->toplevel_vbox
,
272 filer_window
->toolbar
, 0);
273 gtk_widget_show_all(filer_window
->toolbar
);
276 filer_target_mode(filer_window
, NULL
, NULL
, NULL
);
277 toolbar_update_info(filer_window
);
280 /****************************************************************
281 * INTERNAL FUNCTIONS *
282 ****************************************************************/
284 static void toolbar_help_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
288 event
= gtk_get_current_event();
289 if (event
->type
== GDK_BUTTON_RELEASE
&&
290 ((GdkEventButton
*) event
)->button
!= 1)
291 menu_rox_help(NULL
, HELP_MANUAL
, NULL
);
293 filer_opendir(make_path(app_dir
, "Help"), NULL
, NULL
);
296 static void toolbar_refresh_clicked(GtkWidget
*widget
,
297 FilerWindow
*filer_window
)
301 event
= gtk_get_current_event();
302 if (event
->type
== GDK_BUTTON_RELEASE
&&
303 ((GdkEventButton
*) event
)->button
!= 1)
305 filer_opendir(filer_window
->sym_path
, filer_window
, NULL
);
308 filer_refresh(filer_window
);
311 static void toolbar_home_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
315 event
= gtk_get_current_event();
316 if (event
->type
== GDK_BUTTON_RELEASE
&& NEW_WIN_BUTTON(event
))
318 filer_opendir(home_dir
, filer_window
, NULL
);
321 filer_change_to(filer_window
, home_dir
, NULL
);
324 static void toolbar_bookmarks_clicked(GtkWidget
*widget
,
325 FilerWindow
*filer_window
)
329 g_return_if_fail(filer_window
!= NULL
);
331 event
= gtk_get_current_event();
332 if (event
->type
== GDK_BUTTON_PRESS
&&
333 ((GdkEventButton
*) event
)->button
== 1)
335 bookmarks_show_menu(filer_window
);
337 else if (event
->type
== GDK_BUTTON_RELEASE
&&
338 ((GdkEventButton
*) event
)->button
!= 1)
344 static void toolbar_close_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
348 g_return_if_fail(filer_window
!= NULL
);
350 event
= gtk_get_current_event();
351 if (event
->type
== GDK_BUTTON_RELEASE
&&
352 ((GdkEventButton
*) event
)->button
!= 1)
354 filer_opendir(filer_window
->sym_path
, filer_window
, NULL
);
356 else if (!filer_window_delete(filer_window
->window
, NULL
, filer_window
))
357 gtk_widget_destroy(filer_window
->window
);
360 static void toolbar_up_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
364 event
= gtk_get_current_event();
365 if (event
->type
== GDK_BUTTON_RELEASE
&& NEW_WIN_BUTTON(event
))
367 filer_open_parent(filer_window
);
370 change_to_parent(filer_window
);
373 static void toolbar_autosize_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
377 bev
= (GdkEventButton
*) gtk_get_current_event();
378 if (bev
->type
!= GDK_BUTTON_RELEASE
)
381 display_set_layout(filer_window
, AUTO_SIZE_ICONS
, filer_window
->details_type
,
385 static void toolbar_size_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
389 bev
= (GdkEventButton
*) gtk_get_current_event();
390 if (bev
->type
!= GDK_BUTTON_RELEASE
)
393 display_change_size(filer_window
, bev
->button
== 1);
396 static void toolbar_sort_clicked(GtkWidget
*widget
,
397 FilerWindow
*filer_window
)
400 int i
, current
, next
, next_wrapped
;
405 static const SortType sorts
[]={
406 SORT_NAME
, SORT_TYPE
, SORT_DATE
, SORT_SIZE
,
407 SORT_OWNER
, SORT_GROUP
,
409 static const char *sort_names
[] = {
410 N_("Sort by name"), N_("Sort by type"), N_("Sort by date"),
411 N_("Sort by size"), N_("Sort by owner"), N_("Sort by group"),
414 bev
= (GdkEventButton
*) gtk_get_current_event();
415 adjust
= (bev
->button
!= 1) && bev
->type
== GDK_BUTTON_RELEASE
;
418 dir
= filer_window
->sort_order
;
419 for (i
=0; i
< G_N_ELEMENTS(sort_names
); i
++)
421 if (filer_window
->sort_type
== sorts
[i
])
435 next_wrapped
= next
% G_N_ELEMENTS(sorts
);
437 if (next_wrapped
!= next
)
438 dir
= (dir
== GTK_SORT_ASCENDING
)
439 ? GTK_SORT_DESCENDING
: GTK_SORT_ASCENDING
;
441 display_set_sort_type(filer_window
, sorts
[next_wrapped
], dir
);
442 tip
= g_strconcat(_(sort_names
[next_wrapped
]), ", ",
443 dir
== GTK_SORT_ASCENDING
444 ? _("ascending") : _("descending"),
450 static void toolbar_details_clicked(GtkWidget
*widget
,
451 FilerWindow
*filer_window
)
453 if (filer_window
->view_type
== VIEW_TYPE_DETAILS
)
454 filer_set_view_type(filer_window
, VIEW_TYPE_COLLECTION
);
456 filer_set_view_type(filer_window
, VIEW_TYPE_DETAILS
);
459 static void toolbar_hidden_clicked(GtkWidget
*widget
,
460 FilerWindow
*filer_window
)
462 display_set_hidden(filer_window
, !filer_window
->show_hidden
);
465 static gboolean
invert_cb(ViewIter
*iter
, gpointer data
)
467 return !view_get_selected((ViewIface
*) data
, iter
);
470 static void toolbar_select_clicked(GtkWidget
*widget
, FilerWindow
*filer_window
)
474 event
= gtk_get_current_event();
475 if (event
->type
== GDK_BUTTON_RELEASE
)
477 if (((GdkEventButton
*) event
)->button
== 1)
478 view_select_all(filer_window
->view
);
480 view_select_if(filer_window
->view
, invert_cb
,
483 filer_window
->temp_item_selected
= FALSE
;
486 /* If filer_window is NULL, the toolbar is for the options window */
487 static GtkWidget
*create_toolbar(FilerWindow
*filer_window
)
493 bar
= gtk_toolbar_new();
495 gtk_widget_set_size_request(bar
, 100, -1);
497 if (o_toolbar
.int_value
== TOOLBAR_NORMAL
|| !filer_window
)
498 gtk_toolbar_set_style(GTK_TOOLBAR(bar
), GTK_TOOLBAR_ICONS
);
499 else if (o_toolbar
.int_value
== TOOLBAR_HORIZONTAL
)
500 gtk_toolbar_set_style(GTK_TOOLBAR(bar
), GTK_TOOLBAR_BOTH_HORIZ
);
502 gtk_toolbar_set_style(GTK_TOOLBAR(bar
), GTK_TOOLBAR_BOTH
);
504 for (i
= 0; i
< sizeof(all_tools
) / sizeof(*all_tools
); i
++)
506 Tool
*tool
= &all_tools
[i
];
508 if (filer_window
&& !tool
->enabled
)
511 b
= add_button(bar
, tool
, filer_window
);
512 if (filer_window
&& tool
->drop_action
!= DROP_NONE
)
513 handle_drops(filer_window
, b
, tool
->drop_action
);
518 filer_window
->toolbar_text
= gtk_label_new("");
519 gtk_misc_set_alignment(GTK_MISC(filer_window
->toolbar_text
),
521 gtk_toolbar_append_widget(GTK_TOOLBAR(bar
),
522 filer_window
->toolbar_text
, NULL
, NULL
);
528 /* This is used to simulate a click when button 3 is used (GtkButton
529 * normally ignores this).
531 static gint toolbar_other_button
= 0;
532 static gint
toolbar_button_pressed(GtkButton
*button
,
533 GdkEventButton
*event
,
534 FilerWindow
*filer_window
)
536 gint b
= event
->button
;
539 tool
= g_object_get_data(G_OBJECT(button
), "rox-tool");
540 g_return_val_if_fail(tool
!= NULL
, TRUE
);
542 if (tool
->menu
&& b
== 1)
544 tool
->clicked((GtkWidget
*) button
, filer_window
);
548 if ((b
== 2 || b
== 3) && toolbar_other_button
== 0)
550 toolbar_other_button
= event
->button
;
551 gtk_grab_add(GTK_WIDGET(button
));
552 gtk_button_pressed(button
);
560 static gint
toolbar_button_released(GtkButton
*button
,
561 GdkEventButton
*event
,
562 FilerWindow
*filer_window
)
564 if (event
->button
== toolbar_other_button
)
566 toolbar_other_button
= 0;
567 gtk_grab_remove(GTK_WIDGET(button
));
568 gtk_button_released(button
);
576 /* If filer_window is NULL, the toolbar is for the options window */
577 static GtkWidget
*add_button(GtkWidget
*bar
, Tool
*tool
,
578 FilerWindow
*filer_window
)
580 GtkWidget
*button
, *icon_widget
;
582 icon_widget
= gtk_image_new_from_stock(tool
->name
,
583 GTK_ICON_SIZE_LARGE_TOOLBAR
);
585 button
= gtk_toolbar_insert_element(GTK_TOOLBAR(bar
),
586 filer_window
? GTK_TOOLBAR_CHILD_BUTTON
587 : GTK_TOOLBAR_CHILD_TOGGLEBUTTON
,
592 NULL
, NULL
, /* CB, userdata */
593 GTK_TOOLBAR(bar
)->num_children
);
595 if (o_toolbar
.int_value
== TOOLBAR_HORIZONTAL
)
597 GtkWidget
*hbox
, *label
;
599 hbox
= GTK_BIN(button
)->child
;
600 kids
= gtk_container_get_children(GTK_CONTAINER(hbox
));
601 label
= g_list_nth_data(kids
, 1);
606 gtk_box_set_child_packing(GTK_BOX(hbox
), label
,
607 TRUE
, TRUE
, 0, GTK_PACK_END
);
608 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
612 g_object_set_data(G_OBJECT(button
), "rox-tool", tool
);
616 g_signal_connect(button
, "clicked",
617 G_CALLBACK(tool
->clicked
), filer_window
);
618 g_signal_connect(button
, "button_press_event",
619 G_CALLBACK(toolbar_button_pressed
), filer_window
);
620 g_signal_connect(button
, "button_release_event",
621 G_CALLBACK(toolbar_button_released
), filer_window
);
625 g_signal_connect(button
, "clicked",
626 G_CALLBACK(toggle_selected
), NULL
);
627 g_object_set_data(G_OBJECT(button
), "tool_name",
628 (gpointer
) tool
->name
);
634 static void toggle_selected(GtkToggleButton
*widget
, gpointer data
)
636 option_check_widget(&o_toolbar_disable
);
639 /* Called during the drag when the mouse is in a widget registered
640 * as a drop target. Returns TRUE if we can accept the drop.
642 static gboolean
drag_motion(GtkWidget
*widget
,
643 GdkDragContext
*context
,
647 FilerWindow
*filer_window
)
649 GdkDragAction action
= context
->suggested_action
;
651 gpointer type
= (gpointer
) drop_dest_dir
;
653 dest
= (DropDest
) g_object_get_data(G_OBJECT(widget
), "toolbar_dest");
655 if ((context
->actions
& GDK_ACTION_ASK
) && o_dnd_left_menu
.int_value
&&
656 dest
!= DROP_BOOKMARK
)
659 gdk_window_get_pointer(NULL
, NULL
, NULL
, &state
);
660 if (state
& GDK_BUTTON1_MASK
)
661 action
= GDK_ACTION_ASK
;
664 if (dest
== DROP_TO_HOME
)
665 g_dataset_set_data(context
, "drop_dest_path",
667 else if (dest
== DROP_BOOKMARK
)
668 type
= (gpointer
) drop_dest_bookmark
;
670 g_dataset_set_data_full(context
, "drop_dest_path",
671 g_path_get_dirname(filer_window
->sym_path
),
674 g_dataset_set_data(context
, "drop_dest_type", type
);
675 gdk_drag_status(context
, action
, time
);
677 dnd_spring_load(context
, filer_window
);
678 gtk_button_set_relief(GTK_BUTTON(widget
), GTK_RELIEF_NORMAL
);
683 static void drag_leave(GtkWidget
*widget
,
684 GdkDragContext
*context
,
686 FilerWindow
*filer_window
)
688 gtk_button_set_relief(GTK_BUTTON(widget
), GTK_RELIEF_NONE
);
692 static void handle_drops(FilerWindow
*filer_window
,
696 make_drop_target(button
, 0);
697 g_signal_connect(button
, "drag_motion",
698 G_CALLBACK(drag_motion
), filer_window
);
699 g_signal_connect(button
, "drag_leave",
700 G_CALLBACK(drag_leave
), filer_window
);
701 g_object_set_data(G_OBJECT(button
), "toolbar_dest", (gpointer
) dest
);
704 static void tally_items(gpointer key
, gpointer value
, gpointer data
)
706 guchar
*leafname
= (guchar
*) key
;
707 int *tally
= (int *) data
;
709 if (leafname
[0] == '.')
713 static void option_notify(void)
716 gboolean changed
= FALSE
;
717 guchar
*list
= o_toolbar_disable
.value
;
719 for (i
= 0; i
< sizeof(all_tools
) / sizeof(*all_tools
); i
++)
721 Tool
*tool
= &all_tools
[i
];
722 gboolean old
= tool
->enabled
;
724 tool
->enabled
= !in_list(tool
->name
, list
);
726 if (old
!= tool
->enabled
)
730 if (changed
|| o_toolbar
.has_changed
|| o_toolbar_info
.has_changed
)
734 for (next
= all_filer_windows
; next
; next
= next
->next
)
736 FilerWindow
*filer_window
= (FilerWindow
*) next
->data
;
738 toolbar_update_toolbar(filer_window
);
743 static void update_tools(Option
*option
)
747 kids
= gtk_container_get_children(GTK_CONTAINER(option
->widget
));
749 for (next
= kids
; next
; next
= next
->next
)
751 GtkToggleButton
*kid
= (GtkToggleButton
*) next
->data
;
754 name
= g_object_get_data(G_OBJECT(kid
), "tool_name");
756 g_return_if_fail(name
!= NULL
);
758 gtk_toggle_button_set_active(kid
,
759 !in_list(name
, option
->value
));
765 static guchar
*read_tools(Option
*option
)
771 list
= g_string_new(NULL
);
773 kids
= gtk_container_get_children(GTK_CONTAINER(option
->widget
));
775 for (next
= kids
; next
; next
= next
->next
)
777 GtkToggleButton
*kid
= (GtkToggleButton
*) next
->data
;
780 if (!gtk_toggle_button_get_active(kid
))
782 name
= g_object_get_data(G_OBJECT(kid
), "tool_name");
783 g_return_val_if_fail(name
!= NULL
, list
->str
);
786 g_string_append(list
, ", ");
787 g_string_append(list
, name
);
793 g_string_free(list
, FALSE
);
798 static GList
*build_tool_options(Option
*option
, xmlNode
*node
, guchar
*label
)
802 g_return_val_if_fail(option
!= NULL
, NULL
);
804 bar
= create_toolbar(NULL
);
806 option
->update_widget
= update_tools
;
807 option
->read_widget
= read_tools
;
808 option
->widget
= bar
;
810 return g_list_append(NULL
, bar
);