2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Author: Laurent Monin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <glib-object.h>
39 #include "layout-image.h"
41 #include "main-defines.h"
44 #include "ui-fileops.h"
47 #include "ui-tree-edit.h"
48 #include "uri-utils.h"
50 #include "view-dir-list.h"
51 #include "view-dir-tree.h"
56 /** @FIXME Emblems should be attached to icons via e.g.:
58 * icon = g_themed_icon_new("folder");
59 * emblem_icon = g_themed_icon_new("emblem_symbolic_link");
60 * emblem = g_emblem_new(emblem_icon);
61 * emblemed = g_emblemed_icon_new(icon, emblem);
62 * gtk_icon_info_load_icon(icon_info, NULL)
63 * But there does not seem to be a way to get GtkIconInfo from a GIcon
65 GdkPixbuf *create_folder_icon_with_emblem(GtkIconTheme *icon_theme, const gchar *emblem, const gchar *fallback_icon, gint size)
67 if (!gtk_icon_theme_has_icon(icon_theme, emblem))
69 return gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
72 GError *error = nullptr;
73 GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, emblem, size, GTK_ICON_LOOKUP_USE_BUILTIN, &error);
77 log_printf("Error: %s\n", error->message);
79 pixbuf = gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
83 GdkPixbuf *directory_pixbuf = gtk_icon_theme_load_icon(icon_theme, GQ_ICON_DIRECTORY, size, GTK_ICON_LOOKUP_USE_BUILTIN, nullptr);
84 pixbuf = gdk_pixbuf_copy(directory_pixbuf);
85 g_object_unref(directory_pixbuf);
87 gint scale = gdk_pixbuf_get_width(icon) / 2;
88 gdk_pixbuf_composite(icon, pixbuf, scale, scale, scale, scale, scale, scale, 0.5, 0.5, GDK_INTERP_HYPER, 255);
95 /* Folders icons to be used in tree or list directory view */
96 PixmapFolders *folder_icons_new()
98 auto pf = g_new0(PixmapFolders, 1);
99 GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
102 if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &size, &size))
107 pf->close = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_DIRECTORY, size, GTK_ICON_LOOKUP_USE_BUILTIN);
108 pf->open = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_OPEN, size, GTK_ICON_LOOKUP_USE_BUILTIN);
109 pf->parent = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_GO_UP, size, GTK_ICON_LOOKUP_USE_BUILTIN);
111 pf->deny = create_folder_icon_with_emblem(icon_theme, GQ_ICON_UNREADABLE, GQ_ICON_STOP, size);
112 pf->link = create_folder_icon_with_emblem(icon_theme, GQ_ICON_LINK, GQ_ICON_REDO, size);
113 pf->read_only = create_folder_icon_with_emblem(icon_theme, GQ_ICON_READONLY, GQ_ICON_DIRECTORY, size);
118 void folder_icons_free(PixmapFolders *pf)
122 g_object_unref(pf->close);
123 g_object_unref(pf->open);
124 g_object_unref(pf->deny);
125 g_object_unref(pf->parent);
126 g_object_unref(pf->link);
127 g_object_unref(pf->read_only);
134 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data);
136 static void vd_destroy_cb(GtkWidget *widget, gpointer data)
138 auto vd = static_cast<ViewDir *>(data);
140 file_data_unregister_notify_func(vd_notify_cb, vd);
144 g_signal_handlers_disconnect_matched(G_OBJECT(vd->popup), G_SIGNAL_MATCH_DATA,
145 0, 0, nullptr, nullptr, vd);
146 g_object_unref(vd->popup);
151 case DIRVIEW_LIST: vdlist_destroy_cb(widget, data); break;
152 case DIRVIEW_TREE: vdtree_destroy_cb(widget, data); break;
155 if (vd->pf) folder_icons_free(vd->pf);
156 if (vd->drop_list) filelist_free(vd->drop_list);
158 if (vd->dir_fd) file_data_unref(vd->dir_fd);
159 if (vd->info) g_free(vd->info);
164 ViewDir *vd_new(LayoutWindow *lw)
166 auto vd = g_new0(ViewDir, 1);
168 vd->widget = gq_gtk_scrolled_window_new(nullptr, nullptr);
169 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vd->widget), GTK_SHADOW_IN);
170 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vd->widget),
171 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
174 vd->pf = folder_icons_new();
176 switch (lw->options.dir_view_type)
178 case DIRVIEW_LIST: vd = vdlist_new(vd, lw->dir_fd); break;
179 case DIRVIEW_TREE: vd = vdtree_new(vd, lw->dir_fd); break;
182 gtk_container_add(GTK_CONTAINER(vd->widget), vd->view);
186 g_signal_connect(G_OBJECT(vd->view), "row_activated",
187 G_CALLBACK(vd_activate_cb), vd);
188 g_signal_connect(G_OBJECT(vd->widget), "destroy",
189 G_CALLBACK(vd_destroy_cb), vd);
190 g_signal_connect(G_OBJECT(vd->view), "key_press_event",
191 G_CALLBACK(vd_press_key_cb), vd);
192 g_signal_connect(G_OBJECT(vd->view), "button_press_event",
193 G_CALLBACK(vd_press_cb), vd);
194 g_signal_connect(G_OBJECT(vd->view), "button_release_event",
195 G_CALLBACK(vd_release_cb), vd);
197 file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
199 /* vd_set_fd expects that vd_notify_cb is already registered */
200 if (lw->dir_fd) vd_set_fd(vd, lw->dir_fd);
202 gtk_widget_show(vd->view);
207 void vd_set_select_func(ViewDir *vd,
208 void (*func)(ViewDir *vd, FileData *fd, gpointer data), gpointer data)
210 vd->select_func = func;
211 vd->select_data = data;
214 #pragma GCC diagnostic push
215 #pragma GCC diagnostic ignored "-Wunused-function"
216 void vd_set_layout_unused(ViewDir *vd, LayoutWindow *layout)
220 #pragma GCC diagnostic pop
222 gboolean vd_set_fd(ViewDir *vd, FileData *dir_fd)
224 gboolean ret = FALSE;
226 file_data_unregister_notify_func(vd_notify_cb, vd);
230 case DIRVIEW_LIST: ret = vdlist_set_fd(vd, dir_fd); break;
231 case DIRVIEW_TREE: ret = vdtree_set_fd(vd, dir_fd); break;
234 file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
239 void vd_refresh(ViewDir *vd)
243 case DIRVIEW_LIST: vdlist_refresh(vd); break;
244 case DIRVIEW_TREE: vdtree_refresh(vd); break;
248 #pragma GCC diagnostic push
249 #pragma GCC diagnostic ignored "-Wunused-function"
250 const gchar *vd_row_get_path_unused(ViewDir *vd, gint row)
252 const gchar *ret = nullptr;
256 case DIRVIEW_LIST: ret = vdlist_row_get_path(vd, row); break;
257 case DIRVIEW_TREE: ret = vdtree_row_get_path(vd, row); break;
262 #pragma GCC diagnostic pop
264 /* the calling stack is this:
265 vd_select_row -> select_func -> layout_set_fd -> vd_set_fd
267 void vd_select_row(ViewDir *vd, FileData *fd)
269 if (fd && vd->select_func)
271 vd->select_func(vd, fd, vd->select_data);
275 gboolean vd_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
277 gboolean ret = FALSE;
281 case DIRVIEW_LIST: ret = vdlist_find_row(vd, fd, iter); break;
282 case DIRVIEW_TREE: ret = vdtree_find_row(vd, fd, iter, nullptr); break;
288 FileData *vd_get_fd_from_tree_path(ViewDir *vd, GtkTreeView *tview, GtkTreePath *tpath)
291 FileData *fd = nullptr;
294 store = gtk_tree_view_get_model(tview);
295 gtk_tree_model_get_iter(store, &iter, tpath);
299 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
304 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
305 fd = (nd) ? nd->fd : nullptr;
313 static void vd_rename_finished_cb(gboolean success, const gchar *new_path, gpointer data)
315 auto vd = static_cast<ViewDir *>(data);
318 FileData *fd = file_data_new_dir(new_path);
321 if (vd_find_row(vd, fd, &iter))
323 tree_view_row_make_visible(GTK_TREE_VIEW(vd->view), &iter, TRUE);
330 static gboolean vd_rename_cb(TreeEditData *td, const gchar *, const gchar *new_name, gpointer data)
332 auto vd = static_cast<ViewDir *>(data);
337 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), td->path);
338 if (!fd) return FALSE;
340 base = remove_level_from_path(fd->path);
341 new_path = g_build_filename(base, new_name, NULL);
344 file_util_rename_dir(fd, new_path, vd->view, vd_rename_finished_cb, vd);
351 static void vd_rename_by_data(ViewDir *vd, FileData *fd)
357 if (!fd || !vd_find_row(vd, fd, &iter)) return;
358 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
359 tpath = gtk_tree_model_get_path(store, &iter);
361 tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
363 gtk_tree_path_free(tpath);
367 void vd_color_set(ViewDir *vd, FileData *fd, gint color_set)
372 if (!vd_find_row(vd, fd, &iter)) return;
373 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
378 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
381 gtk_tree_store_set(GTK_TREE_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
386 void vd_popup_destroy_cb(GtkWidget *, gpointer data)
388 auto vd = static_cast<ViewDir *>(data);
390 vd_color_set(vd, vd->click_fd, FALSE);
391 vd->click_fd = nullptr;
394 vd_color_set(vd, vd->drop_fd, FALSE);
395 filelist_free(vd->drop_list);
396 vd->drop_list = nullptr;
397 vd->drop_fd = nullptr;
401 *-----------------------------------------------------------------------------
402 * drop menu (from dnd)
403 *-----------------------------------------------------------------------------
406 static void vd_drop_menu_copy_cb(GtkWidget *, gpointer data)
408 auto vd = static_cast<ViewDir *>(data);
412 if (!vd->drop_fd) return;
414 path = vd->drop_fd->path;
415 list = vd->drop_list;
416 vd->drop_list = nullptr;
418 file_util_copy_simple(list, path, vd->widget);
421 static void vd_drop_menu_move_cb(GtkWidget *, gpointer data)
423 auto vd = static_cast<ViewDir *>(data);
427 if (!vd->drop_fd) return;
429 path = vd->drop_fd->path;
430 list = vd->drop_list;
432 vd->drop_list = nullptr;
434 file_util_move_simple(list, path, vd->widget);
437 static void vd_drop_menu_filter_cb(GtkWidget *widget, gpointer data)
439 auto vd = static_cast<ViewDir *>(data);
444 if (!vd->drop_fd) return;
446 key = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "filter_key"));
448 path = vd->drop_fd->path;
449 list = vd->drop_list;
451 vd->drop_list = nullptr;
453 file_util_start_filter_from_filelist(key, list, path, vd->widget);
456 static void vd_drop_menu_edit_item_free(gpointer data)
461 GtkWidget *vd_drop_menu(ViewDir *vd, gint active)
464 GList *editors_list = editor_list_get();
465 GList *work = editors_list;
467 menu = popup_menu_short_lived();
468 g_signal_connect(G_OBJECT(menu), "destroy",
469 G_CALLBACK(vd_popup_destroy_cb), vd);
471 menu_item_add_icon_sensitive(menu, _("_Copy"), GQ_ICON_COPY, active,
472 G_CALLBACK(vd_drop_menu_copy_cb), vd);
473 menu_item_add_sensitive(menu, _("_Move"), active, G_CALLBACK(vd_drop_menu_move_cb), vd);
478 auto editor = static_cast<const EditorDescription *>(work->data);
482 if (!editor_is_filter(editor->key)) continue;
483 key = g_strdup(editor->key);
484 item = menu_item_add_sensitive(menu, editor->name, active, G_CALLBACK(vd_drop_menu_filter_cb), vd);
485 g_object_set_data_full(G_OBJECT(item), "filter_key", key, vd_drop_menu_edit_item_free);
488 g_list_free(editors_list);
490 menu_item_add_divider(menu);
491 menu_item_add_icon(menu, _("Cancel"), GQ_ICON_CANCEL, nullptr, vd);
497 *-----------------------------------------------------------------------------
499 *-----------------------------------------------------------------------------
502 static void vd_pop_menu_up_cb(GtkWidget *, gpointer data)
504 auto vd = static_cast<ViewDir *>(data);
507 if (!vd->dir_fd || strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) == 0) return;
508 path = remove_level_from_path(vd->dir_fd->path);
512 FileData *fd = file_data_new_dir(path);
513 vd->select_func(vd, fd, vd->select_data);
520 static void vd_pop_menu_slide_cb(GtkWidget *, gpointer data)
522 auto vd = static_cast<ViewDir *>(data);
524 if (!vd->layout) return;
525 if (!vd->click_fd) return;
527 layout_set_fd(vd->layout, vd->click_fd);
528 layout_select_none(vd->layout);
529 layout_image_slideshow_stop(vd->layout);
530 layout_image_slideshow_start(vd->layout);
533 static void vd_pop_menu_slide_rec_cb(GtkWidget *, gpointer data)
535 auto vd = static_cast<ViewDir *>(data);
538 if (!vd->layout) return;
539 if (!vd->click_fd) return;
541 list = filelist_recursive_full(vd->click_fd, vd->layout->options.file_view_list_sort.method, vd->layout->options.file_view_list_sort.ascend, vd->layout->options.file_view_list_sort.case_sensitive);
543 layout_image_slideshow_stop(vd->layout);
544 layout_image_slideshow_start_from_list(vd->layout, list);
547 static void vd_pop_menu_dupe(ViewDir *vd, gint recursive)
550 GList *list = nullptr;
552 if (!vd->click_fd) return;
556 list = g_list_append(list, file_data_ref(vd->click_fd));
560 filelist_read(vd->click_fd, &list, nullptr);
561 list = filelist_filter(list, FALSE);
564 dw = dupe_window_new();
565 dupe_window_add_files(dw, list, recursive);
570 static void vd_pop_menu_dupe_cb(GtkWidget *, gpointer data)
572 auto vd = static_cast<ViewDir *>(data);
573 vd_pop_menu_dupe(vd, FALSE);
576 static void vd_pop_menu_dupe_rec_cb(GtkWidget *, gpointer data)
578 auto vd = static_cast<ViewDir *>(data);
579 vd_pop_menu_dupe(vd, TRUE);
582 static void vd_pop_menu_delete_cb(GtkWidget *, gpointer data)
584 auto vd = static_cast<ViewDir *>(data);
586 if (!vd->click_fd) return;
587 file_util_delete_dir(vd->click_fd, vd->widget);
590 static void vd_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
592 auto vd = static_cast<ViewDir *>(data);
594 if (!vd->click_fd) return;
596 file_util_copy_path_to_clipboard(vd->click_fd, TRUE);
599 static void vd_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
601 auto vd = static_cast<ViewDir *>(data);
603 if (!vd->click_fd) return;
605 file_util_copy_path_to_clipboard(vd->click_fd, FALSE);
608 static void vd_pop_submenu_dir_view_as_cb(GtkWidget *widget, gpointer data)
610 auto vd = static_cast<ViewDir *>(data);
612 auto new_type = static_cast<DirViewType>(GPOINTER_TO_INT((g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"))));
613 layout_views_set(vd->layout, new_type, vd->layout->options.file_view_type);
616 static void vd_pop_menu_refresh_cb(GtkWidget *, gpointer data)
618 auto vd = static_cast<ViewDir *>(data);
620 if (vd->layout) layout_refresh(vd->layout);
623 static void vd_toggle_show_hidden_files_cb(GtkWidget *, gpointer data)
625 auto vd = static_cast<ViewDir *>(data);
627 options->file_filter.show_hidden_files = !options->file_filter.show_hidden_files;
628 if (vd->layout) layout_refresh(vd->layout);
631 static void vd_pop_menu_new_folder_cb(gboolean success, const gchar *new_path, gpointer data)
633 auto vd = static_cast<ViewDir *>(data);
634 FileData *fd = nullptr;
639 if (!success) return;
646 fd = vdlist_row_by_path(vd, new_path, nullptr);
651 FileData *new_fd = file_data_new_dir(new_path);
652 fd = vdtree_populate_path(vd, new_fd, TRUE, TRUE);
653 file_data_unref(new_fd);
658 if (!fd || !vd_find_row(vd, fd, &iter)) return;
659 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
660 tpath = gtk_tree_model_get_path(store, &iter);
661 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, nullptr, FALSE);
663 gtk_tree_path_free(tpath);
666 static void vd_pop_menu_new_cb(GtkWidget *, gpointer data)
668 auto vd = static_cast<ViewDir *>(data);
669 FileData *dir_fd = nullptr;
675 if (!vd->dir_fd) return;
681 if (!vd->click_fd) return;
682 dir_fd = vd->click_fd;
687 file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
690 static void vd_pop_menu_rename_cb(GtkWidget *, gpointer data)
692 auto vd = static_cast<ViewDir *>(data);
694 vd_rename_by_data(vd, vd->click_fd);
697 static void vd_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
699 auto vd = static_cast<ViewDir *>(data);
704 if (!vd->layout) return;
706 ascend = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
707 layout_views_set_sort_dir(vd->layout, vd->layout->options.dir_view_list_sort.method, ascend, vd->layout->options.dir_view_list_sort.case_sensitive);
709 if (vd->layout) layout_refresh(vd->layout);
712 static void vd_pop_menu_sort_case_cb(GtkWidget *widget, gpointer data)
714 auto vd = static_cast<ViewDir *>(data);
715 gboolean case_sensitive;
719 if (!vd->layout) return;
721 case_sensitive = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
722 layout_views_set_sort_dir(vd->layout, vd->layout->options.dir_view_list_sort.method, vd->layout->options.dir_view_list_sort.ascend, case_sensitive);
724 if (vd->layout) layout_refresh(vd->layout);
727 static void vd_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
732 vd = static_cast<ViewDir *>(submenu_item_get_data(widget));
735 if (!vd->layout) return;
737 type = static_cast<SortType>GPOINTER_TO_INT(data);
739 if (type == SORT_NAME || type == SORT_NUMBER || type == SORT_TIME)
741 layout_views_set_sort_dir(vd->layout, type, vd->layout->options.dir_view_list_sort.ascend, vd->layout->options.dir_view_list_sort.case_sensitive);
743 if (vd->layout) layout_refresh(vd->layout);
747 GtkWidget *vd_pop_menu(ViewDir *vd, FileData *fd)
751 gboolean rename_delete_active = FALSE;
752 gboolean new_folder_active = FALSE;
756 active = (fd != nullptr);
761 /* check using . (always row 0) */
762 new_folder_active = (vd->dir_fd && access_file(vd->dir_fd->path , W_OK | X_OK));
764 /* ignore .. and . */
765 rename_delete_active = (new_folder_active && fd &&
766 strcmp(fd->name, ".") != 0 &&
767 strcmp(fd->name, "..") != 0 &&
768 access_file(fd->path, W_OK | X_OK));
776 new_folder_active = (fd && access_file(fd->path, W_OK | X_OK));
777 parent = remove_level_from_path(fd->path);
778 rename_delete_active = access_file(parent, W_OK | X_OK);
785 menu = popup_menu_short_lived();
786 g_signal_connect(G_OBJECT(menu), "destroy",
787 G_CALLBACK(vd_popup_destroy_cb), vd);
789 menu_item_add_icon_sensitive(menu, _("_Up to parent"), GQ_ICON_GO_UP,
790 (vd->dir_fd && strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) != 0),
791 G_CALLBACK(vd_pop_menu_up_cb), vd);
793 menu_item_add_divider(menu);
794 menu_item_add_sensitive(menu, _("_Slideshow"), active,
795 G_CALLBACK(vd_pop_menu_slide_cb), vd);
796 menu_item_add_sensitive(menu, _("Slideshow recursive"), active,
797 G_CALLBACK(vd_pop_menu_slide_rec_cb), vd);
799 menu_item_add_divider(menu);
800 menu_item_add_icon_sensitive(menu, _("Find _duplicates..."), GQ_ICON_FIND, active,
801 G_CALLBACK(vd_pop_menu_dupe_cb), vd);
802 menu_item_add_icon_sensitive(menu, _("Find duplicates recursive..."), GQ_ICON_FIND, active,
803 G_CALLBACK(vd_pop_menu_dupe_rec_cb), vd);
805 menu_item_add_divider(menu);
807 menu_item_add_sensitive(menu, _("_New folder..."), new_folder_active,
808 G_CALLBACK(vd_pop_menu_new_cb), vd);
810 menu_item_add_sensitive(menu, _("_Rename..."), rename_delete_active,
811 G_CALLBACK(vd_pop_menu_rename_cb), vd);
813 menu_item_add(menu, _("_Copy path"),
814 G_CALLBACK(vd_pop_menu_copy_path_cb), vd);
816 menu_item_add(menu, _("_Copy path unquoted"),
817 G_CALLBACK(vd_pop_menu_copy_path_unquoted_cb), vd);
819 menu_item_add_icon_sensitive(menu, _("_Delete..."), GQ_ICON_DELETE, rename_delete_active,
820 G_CALLBACK(vd_pop_menu_delete_cb), vd);
821 menu_item_add_divider(menu);
824 menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(DIRVIEW_LIST), vd->type == DIRVIEW_LIST,
825 G_CALLBACK(vd_pop_submenu_dir_view_as_cb), vd);
827 menu_item_add_radio(menu, _("View as _Tree"), GINT_TO_POINTER(DIRVIEW_TREE), vd->type == DIRVIEW_TREE,
828 G_CALLBACK(vd_pop_submenu_dir_view_as_cb), vd);
830 if (vd->type == DIRVIEW_LIST)
832 submenu = submenu_add_dir_sort(nullptr, G_CALLBACK(vd_pop_menu_sort_cb), vd, FALSE, FALSE, TRUE, vd->layout->options.dir_view_list_sort.method);
833 menu_item_add_check(submenu, _("Ascending"), vd->layout->options.dir_view_list_sort.ascend, G_CALLBACK(vd_pop_menu_sort_ascend_cb), (vd));
834 menu_item_add_check(submenu, _("Case"), vd->layout->options.dir_view_list_sort.case_sensitive, G_CALLBACK(vd_pop_menu_sort_case_cb), (vd));
835 item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
836 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
839 if (vd->type == DIRVIEW_TREE)
841 submenu = submenu_add_dir_sort(nullptr, G_CALLBACK(vd_pop_menu_sort_cb), vd, FALSE, FALSE, TRUE, vd->layout->options.dir_view_list_sort.method);
842 item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
843 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
846 menu_item_add_divider(menu);
848 menu_item_add_check(menu, _("Show _hidden files"), options->file_filter.show_hidden_files,
849 G_CALLBACK(vd_toggle_show_hidden_files_cb), vd);
851 menu_item_add_icon(menu, _("Re_fresh"), GQ_ICON_REFRESH,
852 G_CALLBACK(vd_pop_menu_refresh_cb), vd);
857 void vd_new_folder(ViewDir *vd, FileData *dir_fd)
859 file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
863 *-----------------------------------------------------------------------------
865 *-----------------------------------------------------------------------------
868 static GtkTargetEntry vd_dnd_drop_types[] = {
869 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
871 static gint vd_dnd_drop_types_count = 1;
873 static void vd_dest_set(ViewDir *vd, gint enable)
877 gtk_drag_dest_set(vd->view,
878 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
879 vd_dnd_drop_types, vd_dnd_drop_types_count,
880 static_cast<GdkDragAction>(GDK_ACTION_MOVE | GDK_ACTION_COPY));
884 gtk_drag_dest_unset(vd->view);
888 static void vd_dnd_get(GtkWidget *, GdkDragContext *,
889 GtkSelectionData *selection_data, guint info,
890 guint, gpointer data)
892 auto vd = static_cast<ViewDir *>(data);
895 if (!vd->click_fd) return;
899 case TARGET_URI_LIST:
900 case TARGET_TEXT_PLAIN:
901 list = g_list_prepend(nullptr, vd->click_fd);
902 uri_selection_data_set_uris_from_filelist(selection_data, list);
908 static void vd_dnd_begin(GtkWidget *, GdkDragContext *, gpointer data)
910 auto vd = static_cast<ViewDir *>(data);
912 vd_color_set(vd, vd->click_fd, TRUE);
913 vd_dest_set(vd, FALSE);
916 static void vd_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
918 auto vd = static_cast<ViewDir *>(data);
920 vd_color_set(vd, vd->click_fd, FALSE);
922 if (vd->type == DIRVIEW_LIST && gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
926 vd_dest_set(vd, TRUE);
929 static void vd_dnd_drop_receive(GtkWidget *widget, GdkDragContext *,
931 GtkSelectionData *selection_data, guint info,
932 guint, gpointer data)
934 auto vd = static_cast<ViewDir *>(data);
936 FileData *fd = nullptr;
937 GdkDragAction action = GDK_ACTION_ASK;
939 vd->click_fd = nullptr;
941 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y,
942 &tpath, nullptr, nullptr, nullptr))
944 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
945 gtk_tree_path_free(tpath);
950 if (info == TARGET_URI_LIST)
954 gboolean done = FALSE;
956 list = uri_filelist_from_gtk_selection_data(selection_data);
959 active = access_file(fd->path, W_OK | X_OK);
961 vd_color_set(vd, fd, TRUE);
965 /** @FIXME With GTK2 gdk_drag_context_get_actions() shows the state of the
966 * shift and control keys during the drag operation. With GTK3 this is not
967 * so. This is a workaround.
969 GdkModifierType mask;
971 gdk_window_get_pointer(gtk_widget_get_window(widget), nullptr, nullptr, &mask);
972 if (mask & GDK_CONTROL_MASK)
974 action = GDK_ACTION_COPY;
976 else if (mask & GDK_SHIFT_MASK)
978 action = GDK_ACTION_MOVE;
981 if (action != GDK_ACTION_COPY && action != GDK_ACTION_MOVE)
983 if (options->dnd_default_action == DND_ACTION_COPY)
985 action = GDK_ACTION_COPY;
987 else if (options->dnd_default_action == DND_ACTION_MOVE)
989 action = GDK_ACTION_MOVE;
993 if (action == GDK_ACTION_COPY)
995 file_util_copy_simple(list, fd->path, vd->widget);
999 else if (action == GDK_ACTION_MOVE)
1001 file_util_move_simple(list, fd->path, vd->widget);
1009 vd->popup = vd_drop_menu(vd, active);
1010 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1014 vd->drop_list = list;
1018 static void vd_dnd_drop_update(ViewDir *vd, gint x, gint y)
1021 FileData *fd = nullptr;
1023 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vd->view), x, y,
1024 &tpath, nullptr, nullptr, nullptr))
1026 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), tpath);
1027 gtk_tree_path_free(tpath);
1030 if (fd != vd->drop_fd)
1032 vd_color_set(vd, vd->drop_fd, FALSE);
1033 vd_color_set(vd, fd, TRUE);
1034 if (fd && vd->dnd_drop_update_func) vd->dnd_drop_update_func(vd);
1040 void vd_dnd_drop_scroll_cancel(ViewDir *vd)
1042 if (vd->drop_scroll_id)
1044 g_source_remove(vd->drop_scroll_id);
1045 vd->drop_scroll_id = 0;
1049 static gboolean vd_auto_scroll_idle_cb(gpointer data)
1051 auto vd = static_cast<ViewDir *>(data);
1063 window = gtk_widget_get_window(vd->view);
1064 seat = gdk_display_get_default_seat(gdk_window_get_display(window));
1065 device = gdk_seat_get_pointer(seat);
1066 gdk_window_get_device_position(window, device, &x, &y, nullptr);
1068 w = gdk_window_get_width(window);
1069 h = gdk_window_get_height(window);
1070 if (x >= 0 && x < w && y >= 0 && y < h)
1072 vd_dnd_drop_update(vd, x, y);
1076 vd->drop_scroll_id = 0;
1077 return G_SOURCE_REMOVE;
1080 static gboolean vd_auto_scroll_notify_cb(GtkWidget *, gint, gint, gpointer data)
1082 auto vd = static_cast<ViewDir *>(data);
1084 if (!vd->drop_fd || vd->drop_list) return FALSE;
1086 if (!vd->drop_scroll_id) vd->drop_scroll_id = g_idle_add(vd_auto_scroll_idle_cb, vd);
1091 static gboolean vd_dnd_drop_motion(GtkWidget *, GdkDragContext *context, gint x, gint y, guint time, gpointer data)
1093 auto vd = static_cast<ViewDir *>(data);
1095 vd->click_fd = nullptr;
1097 if (gtk_drag_get_source_widget(context) == vd->view)
1099 /* from same window */
1100 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1104 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
1106 vd_dnd_drop_update(vd, x, y);
1110 GtkAdjustment *adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vd->view));
1111 widget_auto_scroll_start(vd->view, adj, -1, -1, vd_auto_scroll_notify_cb, vd);
1117 static void vd_dnd_drop_leave(GtkWidget *, GdkDragContext *, guint, gpointer data)
1119 auto vd = static_cast<ViewDir *>(data);
1121 if (vd->drop_fd != vd->click_fd) vd_color_set(vd, vd->drop_fd, FALSE);
1123 vd->drop_fd = nullptr;
1125 if (vd->dnd_drop_leave_func) vd->dnd_drop_leave_func(vd);
1128 void vd_dnd_init(ViewDir *vd)
1130 gtk_drag_source_set(vd->view, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
1131 dnd_file_drag_types, dnd_file_drag_types_count,
1132 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK));
1133 g_signal_connect(G_OBJECT(vd->view), "drag_data_get",
1134 G_CALLBACK(vd_dnd_get), vd);
1135 g_signal_connect(G_OBJECT(vd->view), "drag_begin",
1136 G_CALLBACK(vd_dnd_begin), vd);
1137 g_signal_connect(G_OBJECT(vd->view), "drag_end",
1138 G_CALLBACK(vd_dnd_end), vd);
1140 vd_dest_set(vd, TRUE);
1141 g_signal_connect(G_OBJECT(vd->view), "drag_data_received",
1142 G_CALLBACK(vd_dnd_drop_receive), vd);
1143 g_signal_connect(G_OBJECT(vd->view), "drag_motion",
1144 G_CALLBACK(vd_dnd_drop_motion), vd);
1145 g_signal_connect(G_OBJECT(vd->view), "drag_leave",
1146 G_CALLBACK(vd_dnd_drop_leave), vd);
1150 *----------------------------------------------------------------------------
1152 *----------------------------------------------------------------------------
1155 #pragma GCC diagnostic push
1156 #pragma GCC diagnostic ignored "-Wunused-function"
1157 void vd_menu_position_cb_unused(GtkMenu *menu, gint *x, gint *y, gboolean *, gpointer data)
1159 auto *vd = static_cast<ViewDir *>(data);
1160 GtkTreeModel *store;
1166 if (!vd_find_row(vd, vd->click_fd, &iter)) return;
1167 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
1168 tpath = gtk_tree_model_get_path(store, &iter);
1169 tree_view_get_cell_clamped(GTK_TREE_VIEW(vd->view), tpath, 0, TRUE, x, y, &cw, &ch);
1170 gtk_tree_path_free(tpath);
1172 popup_menu_position_clamp(menu, x, y, 0);
1174 #pragma GCC diagnostic pop
1176 void vd_activate_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *, gpointer data)
1178 auto vd = static_cast<ViewDir *>(data);
1179 FileData *fd = vd_get_fd_from_tree_path(vd, tview, tpath);
1181 vd_select_row(vd, fd);
1184 static GdkRGBA *vd_color_shifted(GtkWidget *widget)
1186 static GdkRGBA color;
1187 static GtkWidget *done = nullptr;
1190 /* @FIXME GTK4 no background color */
1194 GtkStyleContext *style_context;
1196 style_context = gtk_widget_get_style_context(widget);
1197 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &color);
1199 shift_color(&color, -1, 0);
1207 void vd_color_cb(GtkTreeViewColumn *, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1209 auto vd = static_cast<ViewDir *>(data);
1212 gtk_tree_model_get(tree_model, iter, DIR_COLUMN_COLOR, &set, -1);
1213 g_object_set(G_OBJECT(cell),
1214 "cell-background-rgba", vd_color_shifted(vd->view),
1215 "cell-background-set", set, NULL);
1218 gboolean vd_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1220 auto vd = static_cast<ViewDir *>(data);
1222 FileData *fd = nullptr;
1224 if (defined_mouse_buttons(widget, bevent, vd->layout))
1229 if (vd->type == DIRVIEW_LIST && !options->view_dir_list_single_click_enter)
1232 if (!vd->click_fd) return FALSE;
1233 vd_color_set(vd, vd->click_fd, FALSE);
1235 if (bevent->button != MOUSE_BUTTON_LEFT) return TRUE;
1237 if ((bevent->x != 0 || bevent->y != 0) &&
1238 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
1239 &tpath, nullptr, nullptr, nullptr))
1241 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
1242 gtk_tree_path_free(tpath);
1245 if (fd && vd->click_fd == fd)
1247 vd_select_row(vd, vd->click_fd);
1253 gboolean vd_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1255 auto vd = static_cast<ViewDir *>(data);
1256 gboolean ret = FALSE;
1260 case DIRVIEW_LIST: ret = vdlist_press_key_cb(widget, event, data); break;
1261 case DIRVIEW_TREE: ret = vdtree_press_key_cb(widget, event, data); break;
1267 gboolean vd_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1269 auto vd = static_cast<ViewDir *>(data);
1270 gboolean ret = FALSE;
1274 NodeData *nd = nullptr;
1275 GtkTreeModel *store;
1277 if (bevent->button == MOUSE_BUTTON_RIGHT)
1279 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, &tpath, nullptr, nullptr, nullptr))
1281 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1282 gtk_tree_model_get_iter(store, &iter, tpath);
1287 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
1291 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
1292 vd->click_fd = (nd) ? nd->fd : nullptr;
1297 vd_color_set(vd, vd->click_fd, TRUE);
1301 vd->popup = vd_pop_menu(vd, vd->click_fd);
1302 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1309 case DIRVIEW_LIST: ret = vdlist_press_cb(widget, bevent, data); break;
1310 case DIRVIEW_TREE: ret = vdtree_press_cb(widget, bevent, data); break;
1316 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data)
1318 auto vd = static_cast<ViewDir *>(data);
1322 if (!S_ISDIR(fd->mode)) return; /* this gives correct results even on recently deleted files/directories */
1324 DEBUG_1("Notify vd: %s %04x", fd->path, type);
1326 base = remove_level_from_path(fd->path);
1328 if (vd->type == DIRVIEW_LIST)
1330 refresh = (fd == vd->dir_fd);
1334 refresh = (strcmp(base, vd->dir_fd->path) == 0);
1337 if ((type & NOTIFY_CHANGE) && fd->change)
1339 if (!refresh && fd->change->dest)
1341 gchar *dest_base = remove_level_from_path(fd->change->dest);
1342 refresh = (strcmp(dest_base, vd->dir_fd->path) == 0);
1346 if (!refresh && fd->change->source)
1348 gchar *source_base = remove_level_from_path(fd->change->source);
1349 refresh = (strcmp(source_base, vd->dir_fd->path) == 0);
1350 g_free(source_base);
1354 if (refresh) vd_refresh(vd);
1357 if (vd->type == DIRVIEW_TREE)
1360 FileData *base_fd = file_data_new_dir(base);
1362 if (vd_find_row(vd, base_fd, &iter))
1364 vdtree_populate_path_by_iter(vd, &iter, TRUE, vd->dir_fd);
1367 file_data_unref(base_fd);
1372 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */