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.
32 #include "layout-image.h"
34 #include "main-defines.h"
36 #include "ui-fileops.h"
39 #include "ui-tree-edit.h"
40 #include "uri-utils.h"
42 #include "view-dir-list.h"
43 #include "view-dir-tree.h"
48 /** @FIXME Emblems should be attached to icons via e.g.:
50 * icon = g_themed_icon_new("folder");
51 * emblem_icon = g_themed_icon_new("emblem_symbolic_link");
52 * emblem = g_emblem_new(emblem_icon);
53 * emblemed = g_emblemed_icon_new(icon, emblem);
54 * gtk_icon_info_load_icon(icon_info, NULL)
55 * But there does not seem to be a way to get GtkIconInfo from a GIcon
57 GdkPixbuf *create_folder_icon_with_emblem(GtkIconTheme *icon_theme, const gchar *emblem, const gchar *fallback_icon, gint size)
59 if (!gtk_icon_theme_has_icon(icon_theme, emblem))
61 return gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
64 GError *error = nullptr;
65 GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, emblem, size, GTK_ICON_LOOKUP_USE_BUILTIN, &error);
69 log_printf("Error: %s\n", error->message);
71 pixbuf = gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
75 GdkPixbuf *directory_pixbuf = gtk_icon_theme_load_icon(icon_theme, GQ_ICON_DIRECTORY, size, GTK_ICON_LOOKUP_USE_BUILTIN, nullptr);
76 pixbuf = gdk_pixbuf_copy(directory_pixbuf);
77 g_object_unref(directory_pixbuf);
79 gint scale = gdk_pixbuf_get_width(icon) / 2;
80 gdk_pixbuf_composite(icon, pixbuf, scale, scale, scale, scale, scale, scale, 0.5, 0.5, GDK_INTERP_HYPER, 255);
87 /* Folders icons to be used in tree or list directory view */
88 PixmapFolders *folder_icons_new()
90 auto pf = g_new0(PixmapFolders, 1);
91 GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
94 if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &size, &size))
99 pf->close = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_DIRECTORY, size, GTK_ICON_LOOKUP_USE_BUILTIN);
100 pf->open = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_OPEN, size, GTK_ICON_LOOKUP_USE_BUILTIN);
101 pf->parent = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_GO_UP, size, GTK_ICON_LOOKUP_USE_BUILTIN);
103 pf->deny = create_folder_icon_with_emblem(icon_theme, GQ_ICON_UNREADABLE, GQ_ICON_STOP, size);
104 pf->link = create_folder_icon_with_emblem(icon_theme, GQ_ICON_LINK, GQ_ICON_REDO, size);
105 pf->read_only = create_folder_icon_with_emblem(icon_theme, GQ_ICON_READONLY, GQ_ICON_DIRECTORY, size);
110 void folder_icons_free(PixmapFolders *pf)
114 g_object_unref(pf->close);
115 g_object_unref(pf->open);
116 g_object_unref(pf->deny);
117 g_object_unref(pf->parent);
118 g_object_unref(pf->link);
119 g_object_unref(pf->read_only);
126 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data);
128 static void vd_destroy_cb(GtkWidget *widget, gpointer data)
130 auto vd = static_cast<ViewDir *>(data);
132 file_data_unregister_notify_func(vd_notify_cb, vd);
136 g_signal_handlers_disconnect_matched(G_OBJECT(vd->popup), G_SIGNAL_MATCH_DATA,
137 0, 0, nullptr, nullptr, vd);
138 g_object_unref(vd->popup);
143 case DIRVIEW_LIST: vdlist_destroy_cb(widget, data); break;
144 case DIRVIEW_TREE: vdtree_destroy_cb(widget, data); break;
147 if (vd->pf) folder_icons_free(vd->pf);
148 if (vd->drop_list) filelist_free(vd->drop_list);
150 if (vd->dir_fd) file_data_unref(vd->dir_fd);
151 if (vd->info) g_free(vd->info);
156 ViewDir *vd_new(LayoutWindow *lw)
158 auto vd = g_new0(ViewDir, 1);
160 vd->widget = gq_gtk_scrolled_window_new(nullptr, nullptr);
161 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vd->widget), GTK_SHADOW_IN);
162 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vd->widget),
163 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
166 vd->pf = folder_icons_new();
168 switch (lw->options.dir_view_type)
170 case DIRVIEW_LIST: vd = vdlist_new(vd, lw->dir_fd); break;
171 case DIRVIEW_TREE: vd = vdtree_new(vd, lw->dir_fd); break;
174 gtk_container_add(GTK_CONTAINER(vd->widget), vd->view);
178 g_signal_connect(G_OBJECT(vd->view), "row_activated",
179 G_CALLBACK(vd_activate_cb), vd);
180 g_signal_connect(G_OBJECT(vd->widget), "destroy",
181 G_CALLBACK(vd_destroy_cb), vd);
182 g_signal_connect(G_OBJECT(vd->view), "key_press_event",
183 G_CALLBACK(vd_press_key_cb), vd);
184 g_signal_connect(G_OBJECT(vd->view), "button_press_event",
185 G_CALLBACK(vd_press_cb), vd);
186 g_signal_connect(G_OBJECT(vd->view), "button_release_event",
187 G_CALLBACK(vd_release_cb), vd);
189 file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
191 /* vd_set_fd expects that vd_notify_cb is already registered */
192 if (lw->dir_fd) vd_set_fd(vd, lw->dir_fd);
194 gtk_widget_show(vd->view);
199 void vd_set_select_func(ViewDir *vd,
200 void (*func)(ViewDir *vd, FileData *fd, gpointer data), gpointer data)
202 vd->select_func = func;
203 vd->select_data = data;
206 #pragma GCC diagnostic push
207 #pragma GCC diagnostic ignored "-Wunused-function"
208 void vd_set_layout_unused(ViewDir *vd, LayoutWindow *layout)
212 #pragma GCC diagnostic pop
214 gboolean vd_set_fd(ViewDir *vd, FileData *dir_fd)
216 gboolean ret = FALSE;
218 file_data_unregister_notify_func(vd_notify_cb, vd);
222 case DIRVIEW_LIST: ret = vdlist_set_fd(vd, dir_fd); break;
223 case DIRVIEW_TREE: ret = vdtree_set_fd(vd, dir_fd); break;
226 file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
231 void vd_refresh(ViewDir *vd)
235 case DIRVIEW_LIST: vdlist_refresh(vd); break;
236 case DIRVIEW_TREE: vdtree_refresh(vd); break;
240 #pragma GCC diagnostic push
241 #pragma GCC diagnostic ignored "-Wunused-function"
242 const gchar *vd_row_get_path_unused(ViewDir *vd, gint row)
244 const gchar *ret = nullptr;
248 case DIRVIEW_LIST: ret = vdlist_row_get_path(vd, row); break;
249 case DIRVIEW_TREE: ret = vdtree_row_get_path(vd, row); break;
254 #pragma GCC diagnostic pop
256 /* the calling stack is this:
257 vd_select_row -> select_func -> layout_set_fd -> vd_set_fd
259 void vd_select_row(ViewDir *vd, FileData *fd)
261 if (fd && vd->select_func)
263 vd->select_func(vd, fd, vd->select_data);
267 gboolean vd_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
269 gboolean ret = FALSE;
273 case DIRVIEW_LIST: ret = vdlist_find_row(vd, fd, iter); break;
274 case DIRVIEW_TREE: ret = vdtree_find_row(vd, fd, iter, nullptr); break;
280 FileData *vd_get_fd_from_tree_path(ViewDir *vd, GtkTreeView *tview, GtkTreePath *tpath)
283 FileData *fd = nullptr;
286 store = gtk_tree_view_get_model(tview);
287 gtk_tree_model_get_iter(store, &iter, tpath);
291 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
296 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
297 fd = (nd) ? nd->fd : nullptr;
305 static void vd_rename_finished_cb(gboolean success, const gchar *new_path, gpointer data)
307 auto vd = static_cast<ViewDir *>(data);
310 FileData *fd = file_data_new_dir(new_path);
313 if (vd_find_row(vd, fd, &iter))
315 tree_view_row_make_visible(GTK_TREE_VIEW(vd->view), &iter, TRUE);
322 static gboolean vd_rename_cb(TreeEditData *td, const gchar *, const gchar *new_name, gpointer data)
324 auto vd = static_cast<ViewDir *>(data);
329 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), td->path);
330 if (!fd) return FALSE;
332 base = remove_level_from_path(fd->path);
333 new_path = g_build_filename(base, new_name, NULL);
336 file_util_rename_dir(fd, new_path, vd->view, vd_rename_finished_cb, vd);
343 static void vd_rename_by_data(ViewDir *vd, FileData *fd)
349 if (!fd || !vd_find_row(vd, fd, &iter)) return;
350 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
351 tpath = gtk_tree_model_get_path(store, &iter);
353 tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
355 gtk_tree_path_free(tpath);
359 void vd_color_set(ViewDir *vd, FileData *fd, gint color_set)
364 if (!vd_find_row(vd, fd, &iter)) return;
365 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
370 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
373 gtk_tree_store_set(GTK_TREE_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
378 void vd_popup_destroy_cb(GtkWidget *, gpointer data)
380 auto vd = static_cast<ViewDir *>(data);
382 vd_color_set(vd, vd->click_fd, FALSE);
383 vd->click_fd = nullptr;
386 vd_color_set(vd, vd->drop_fd, FALSE);
387 filelist_free(vd->drop_list);
388 vd->drop_list = nullptr;
389 vd->drop_fd = nullptr;
393 *-----------------------------------------------------------------------------
394 * drop menu (from dnd)
395 *-----------------------------------------------------------------------------
398 static void vd_drop_menu_copy_cb(GtkWidget *, gpointer data)
400 auto vd = static_cast<ViewDir *>(data);
404 if (!vd->drop_fd) return;
406 path = vd->drop_fd->path;
407 list = vd->drop_list;
408 vd->drop_list = nullptr;
410 file_util_copy_simple(list, path, vd->widget);
413 static void vd_drop_menu_move_cb(GtkWidget *, gpointer data)
415 auto vd = static_cast<ViewDir *>(data);
419 if (!vd->drop_fd) return;
421 path = vd->drop_fd->path;
422 list = vd->drop_list;
424 vd->drop_list = nullptr;
426 file_util_move_simple(list, path, vd->widget);
429 static void vd_drop_menu_filter_cb(GtkWidget *widget, gpointer data)
431 auto vd = static_cast<ViewDir *>(data);
436 if (!vd->drop_fd) return;
438 key = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "filter_key"));
440 path = vd->drop_fd->path;
441 list = vd->drop_list;
443 vd->drop_list = nullptr;
445 file_util_start_filter_from_filelist(key, list, path, vd->widget);
448 static void vd_drop_menu_edit_item_free(gpointer data)
453 GtkWidget *vd_drop_menu(ViewDir *vd, gint active)
456 GList *editors_list = editor_list_get();
457 GList *work = editors_list;
459 menu = popup_menu_short_lived();
460 g_signal_connect(G_OBJECT(menu), "destroy",
461 G_CALLBACK(vd_popup_destroy_cb), vd);
463 menu_item_add_icon_sensitive(menu, _("_Copy"), GQ_ICON_COPY, active,
464 G_CALLBACK(vd_drop_menu_copy_cb), vd);
465 menu_item_add_sensitive(menu, _("_Move"), active, G_CALLBACK(vd_drop_menu_move_cb), vd);
470 auto editor = static_cast<const EditorDescription *>(work->data);
474 if (!editor_is_filter(editor->key)) continue;
475 key = g_strdup(editor->key);
476 item = menu_item_add_sensitive(menu, editor->name, active, G_CALLBACK(vd_drop_menu_filter_cb), vd);
477 g_object_set_data_full(G_OBJECT(item), "filter_key", key, vd_drop_menu_edit_item_free);
480 g_list_free(editors_list);
482 menu_item_add_divider(menu);
483 menu_item_add_icon(menu, _("Cancel"), GQ_ICON_CANCEL, nullptr, vd);
489 *-----------------------------------------------------------------------------
491 *-----------------------------------------------------------------------------
494 static void vd_pop_menu_up_cb(GtkWidget *, gpointer data)
496 auto vd = static_cast<ViewDir *>(data);
499 if (!vd->dir_fd || strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) == 0) return;
500 path = remove_level_from_path(vd->dir_fd->path);
504 FileData *fd = file_data_new_dir(path);
505 vd->select_func(vd, fd, vd->select_data);
512 static void vd_pop_menu_slide_cb(GtkWidget *, gpointer data)
514 auto vd = static_cast<ViewDir *>(data);
516 if (!vd->layout) return;
517 if (!vd->click_fd) return;
519 layout_set_fd(vd->layout, vd->click_fd);
520 layout_select_none(vd->layout);
521 layout_image_slideshow_stop(vd->layout);
522 layout_image_slideshow_start(vd->layout);
525 static void vd_pop_menu_slide_rec_cb(GtkWidget *, gpointer data)
527 auto vd = static_cast<ViewDir *>(data);
530 if (!vd->layout) return;
531 if (!vd->click_fd) return;
533 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);
535 layout_image_slideshow_stop(vd->layout);
536 layout_image_slideshow_start_from_list(vd->layout, list);
539 static void vd_pop_menu_dupe(ViewDir *vd, gint recursive)
542 GList *list = nullptr;
544 if (!vd->click_fd) return;
548 list = g_list_append(list, file_data_ref(vd->click_fd));
552 filelist_read(vd->click_fd, &list, nullptr);
553 list = filelist_filter(list, FALSE);
556 dw = dupe_window_new();
557 dupe_window_add_files(dw, list, recursive);
562 static void vd_pop_menu_dupe_cb(GtkWidget *, gpointer data)
564 auto vd = static_cast<ViewDir *>(data);
565 vd_pop_menu_dupe(vd, FALSE);
568 static void vd_pop_menu_dupe_rec_cb(GtkWidget *, gpointer data)
570 auto vd = static_cast<ViewDir *>(data);
571 vd_pop_menu_dupe(vd, TRUE);
574 static void vd_pop_menu_delete_cb(GtkWidget *, gpointer data)
576 auto vd = static_cast<ViewDir *>(data);
578 if (!vd->click_fd) return;
579 file_util_delete_dir(vd->click_fd, vd->widget);
582 static void vd_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
584 auto vd = static_cast<ViewDir *>(data);
586 if (!vd->click_fd) return;
588 file_util_copy_path_to_clipboard(vd->click_fd, TRUE);
591 static void vd_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
593 auto vd = static_cast<ViewDir *>(data);
595 if (!vd->click_fd) return;
597 file_util_copy_path_to_clipboard(vd->click_fd, FALSE);
600 static void vd_pop_submenu_dir_view_as_cb(GtkWidget *widget, gpointer data)
602 auto vd = static_cast<ViewDir *>(data);
604 auto new_type = static_cast<DirViewType>(GPOINTER_TO_INT((g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"))));
605 layout_views_set(vd->layout, new_type, vd->layout->options.file_view_type);
608 static void vd_pop_menu_refresh_cb(GtkWidget *, gpointer data)
610 auto vd = static_cast<ViewDir *>(data);
612 if (vd->layout) layout_refresh(vd->layout);
615 static void vd_toggle_show_hidden_files_cb(GtkWidget *, gpointer data)
617 auto vd = static_cast<ViewDir *>(data);
619 options->file_filter.show_hidden_files = !options->file_filter.show_hidden_files;
620 if (vd->layout) layout_refresh(vd->layout);
623 static void vd_pop_menu_new_folder_cb(gboolean success, const gchar *new_path, gpointer data)
625 auto vd = static_cast<ViewDir *>(data);
626 FileData *fd = nullptr;
631 if (!success) return;
638 fd = vdlist_row_by_path(vd, new_path, nullptr);
643 FileData *new_fd = file_data_new_dir(new_path);
644 fd = vdtree_populate_path(vd, new_fd, TRUE, TRUE);
645 file_data_unref(new_fd);
650 if (!fd || !vd_find_row(vd, fd, &iter)) return;
651 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
652 tpath = gtk_tree_model_get_path(store, &iter);
653 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, nullptr, FALSE);
655 gtk_tree_path_free(tpath);
658 static void vd_pop_menu_new_cb(GtkWidget *, gpointer data)
660 auto vd = static_cast<ViewDir *>(data);
661 FileData *dir_fd = nullptr;
667 if (!vd->dir_fd) return;
673 if (!vd->click_fd) return;
674 dir_fd = vd->click_fd;
679 file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
682 static void vd_pop_menu_rename_cb(GtkWidget *, gpointer data)
684 auto vd = static_cast<ViewDir *>(data);
686 vd_rename_by_data(vd, vd->click_fd);
689 static void vd_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
691 auto vd = static_cast<ViewDir *>(data);
696 if (!vd->layout) return;
698 ascend = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
699 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);
701 if (vd->layout) layout_refresh(vd->layout);
704 static void vd_pop_menu_sort_case_cb(GtkWidget *widget, gpointer data)
706 auto vd = static_cast<ViewDir *>(data);
707 gboolean case_sensitive;
711 if (!vd->layout) return;
713 case_sensitive = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
714 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);
716 if (vd->layout) layout_refresh(vd->layout);
719 static void vd_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
724 vd = static_cast<ViewDir *>(submenu_item_get_data(widget));
727 if (!vd->layout) return;
729 type = static_cast<SortType>GPOINTER_TO_INT(data);
731 if (type == SORT_NAME || type == SORT_NUMBER || type == SORT_TIME)
733 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);
735 if (vd->layout) layout_refresh(vd->layout);
739 GtkWidget *vd_pop_menu(ViewDir *vd, FileData *fd)
743 gboolean rename_delete_active = FALSE;
744 gboolean new_folder_active = FALSE;
748 active = (fd != nullptr);
753 /* check using . (always row 0) */
754 new_folder_active = (vd->dir_fd && access_file(vd->dir_fd->path , W_OK | X_OK));
756 /* ignore .. and . */
757 rename_delete_active = (new_folder_active && fd &&
758 strcmp(fd->name, ".") != 0 &&
759 strcmp(fd->name, "..") != 0 &&
760 access_file(fd->path, W_OK | X_OK));
768 new_folder_active = (fd && access_file(fd->path, W_OK | X_OK));
769 parent = remove_level_from_path(fd->path);
770 rename_delete_active = access_file(parent, W_OK | X_OK);
777 menu = popup_menu_short_lived();
778 g_signal_connect(G_OBJECT(menu), "destroy",
779 G_CALLBACK(vd_popup_destroy_cb), vd);
781 menu_item_add_icon_sensitive(menu, _("_Up to parent"), GQ_ICON_GO_UP,
782 (vd->dir_fd && strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) != 0),
783 G_CALLBACK(vd_pop_menu_up_cb), vd);
785 menu_item_add_divider(menu);
786 menu_item_add_sensitive(menu, _("_Slideshow"), active,
787 G_CALLBACK(vd_pop_menu_slide_cb), vd);
788 menu_item_add_sensitive(menu, _("Slideshow recursive"), active,
789 G_CALLBACK(vd_pop_menu_slide_rec_cb), vd);
791 menu_item_add_divider(menu);
792 menu_item_add_icon_sensitive(menu, _("Find _duplicates..."), GQ_ICON_FIND, active,
793 G_CALLBACK(vd_pop_menu_dupe_cb), vd);
794 menu_item_add_icon_sensitive(menu, _("Find duplicates recursive..."), GQ_ICON_FIND, active,
795 G_CALLBACK(vd_pop_menu_dupe_rec_cb), vd);
797 menu_item_add_divider(menu);
799 menu_item_add_sensitive(menu, _("_New folder..."), new_folder_active,
800 G_CALLBACK(vd_pop_menu_new_cb), vd);
802 menu_item_add_sensitive(menu, _("_Rename..."), rename_delete_active,
803 G_CALLBACK(vd_pop_menu_rename_cb), vd);
805 menu_item_add(menu, _("_Copy path"),
806 G_CALLBACK(vd_pop_menu_copy_path_cb), vd);
808 menu_item_add(menu, _("_Copy path unquoted"),
809 G_CALLBACK(vd_pop_menu_copy_path_unquoted_cb), vd);
811 menu_item_add_icon_sensitive(menu, _("_Delete..."), GQ_ICON_DELETE, rename_delete_active,
812 G_CALLBACK(vd_pop_menu_delete_cb), vd);
813 menu_item_add_divider(menu);
816 menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(DIRVIEW_LIST), vd->type == DIRVIEW_LIST,
817 G_CALLBACK(vd_pop_submenu_dir_view_as_cb), vd);
819 menu_item_add_radio(menu, _("View as _Tree"), GINT_TO_POINTER(DIRVIEW_TREE), vd->type == DIRVIEW_TREE,
820 G_CALLBACK(vd_pop_submenu_dir_view_as_cb), vd);
822 if (vd->type == DIRVIEW_LIST)
824 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);
825 menu_item_add_check(submenu, _("Ascending"), vd->layout->options.dir_view_list_sort.ascend, G_CALLBACK(vd_pop_menu_sort_ascend_cb), (vd));
826 menu_item_add_check(submenu, _("Case"), vd->layout->options.dir_view_list_sort.case_sensitive, G_CALLBACK(vd_pop_menu_sort_case_cb), (vd));
827 item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
828 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
831 if (vd->type == DIRVIEW_TREE)
833 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);
834 item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
835 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
838 menu_item_add_divider(menu);
840 menu_item_add_check(menu, _("Show _hidden files"), options->file_filter.show_hidden_files,
841 G_CALLBACK(vd_toggle_show_hidden_files_cb), vd);
843 menu_item_add_icon(menu, _("Re_fresh"), GQ_ICON_REFRESH,
844 G_CALLBACK(vd_pop_menu_refresh_cb), vd);
849 void vd_new_folder(ViewDir *vd, FileData *dir_fd)
851 file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
855 *-----------------------------------------------------------------------------
857 *-----------------------------------------------------------------------------
860 static GtkTargetEntry vd_dnd_drop_types[] = {
861 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
863 static gint vd_dnd_drop_types_count = 1;
865 static void vd_dest_set(ViewDir *vd, gint enable)
869 gtk_drag_dest_set(vd->view,
870 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
871 vd_dnd_drop_types, vd_dnd_drop_types_count,
872 static_cast<GdkDragAction>(GDK_ACTION_MOVE | GDK_ACTION_COPY));
876 gtk_drag_dest_unset(vd->view);
880 static void vd_dnd_get(GtkWidget *, GdkDragContext *,
881 GtkSelectionData *selection_data, guint info,
882 guint, gpointer data)
884 auto vd = static_cast<ViewDir *>(data);
887 if (!vd->click_fd) return;
891 case TARGET_URI_LIST:
892 case TARGET_TEXT_PLAIN:
893 list = g_list_prepend(nullptr, vd->click_fd);
894 uri_selection_data_set_uris_from_filelist(selection_data, list);
900 static void vd_dnd_begin(GtkWidget *, GdkDragContext *, gpointer data)
902 auto vd = static_cast<ViewDir *>(data);
904 vd_color_set(vd, vd->click_fd, TRUE);
905 vd_dest_set(vd, FALSE);
908 static void vd_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
910 auto vd = static_cast<ViewDir *>(data);
912 vd_color_set(vd, vd->click_fd, FALSE);
914 if (vd->type == DIRVIEW_LIST && gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
918 vd_dest_set(vd, TRUE);
921 static void vd_dnd_drop_receive(GtkWidget *widget, GdkDragContext *,
923 GtkSelectionData *selection_data, guint info,
924 guint, gpointer data)
926 auto vd = static_cast<ViewDir *>(data);
928 FileData *fd = nullptr;
929 GdkDragAction action = GDK_ACTION_ASK;
931 vd->click_fd = nullptr;
933 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y,
934 &tpath, nullptr, nullptr, nullptr))
936 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
937 gtk_tree_path_free(tpath);
942 if (info == TARGET_URI_LIST)
946 gboolean done = FALSE;
948 list = uri_filelist_from_gtk_selection_data(selection_data);
951 active = access_file(fd->path, W_OK | X_OK);
953 vd_color_set(vd, fd, TRUE);
957 /** @FIXME With GTK2 gdk_drag_context_get_actions() shows the state of the
958 * shift and control keys during the drag operation. With GTK3 this is not
959 * so. This is a workaround.
961 GdkModifierType mask;
963 gdk_window_get_pointer(gtk_widget_get_window(widget), nullptr, nullptr, &mask);
964 if (mask & GDK_CONTROL_MASK)
966 action = GDK_ACTION_COPY;
968 else if (mask & GDK_SHIFT_MASK)
970 action = GDK_ACTION_MOVE;
973 if (action != GDK_ACTION_COPY && action != GDK_ACTION_MOVE)
975 if (options->dnd_default_action == DND_ACTION_COPY)
977 action = GDK_ACTION_COPY;
979 else if (options->dnd_default_action == DND_ACTION_MOVE)
981 action = GDK_ACTION_MOVE;
985 if (action == GDK_ACTION_COPY)
987 file_util_copy_simple(list, fd->path, vd->widget);
991 else if (action == GDK_ACTION_MOVE)
993 file_util_move_simple(list, fd->path, vd->widget);
1001 vd->popup = vd_drop_menu(vd, active);
1002 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1006 vd->drop_list = list;
1010 static void vd_dnd_drop_update(ViewDir *vd, gint x, gint y)
1013 FileData *fd = nullptr;
1015 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vd->view), x, y,
1016 &tpath, nullptr, nullptr, nullptr))
1018 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), tpath);
1019 gtk_tree_path_free(tpath);
1022 if (fd != vd->drop_fd)
1024 vd_color_set(vd, vd->drop_fd, FALSE);
1025 vd_color_set(vd, fd, TRUE);
1026 if (fd && vd->dnd_drop_update_func) vd->dnd_drop_update_func(vd);
1032 void vd_dnd_drop_scroll_cancel(ViewDir *vd)
1034 if (vd->drop_scroll_id)
1036 g_source_remove(vd->drop_scroll_id);
1037 vd->drop_scroll_id = 0;
1041 static gboolean vd_auto_scroll_idle_cb(gpointer data)
1043 auto vd = static_cast<ViewDir *>(data);
1055 window = gtk_widget_get_window(vd->view);
1056 seat = gdk_display_get_default_seat(gdk_window_get_display(window));
1057 device = gdk_seat_get_pointer(seat);
1058 gdk_window_get_device_position(window, device, &x, &y, nullptr);
1060 w = gdk_window_get_width(window);
1061 h = gdk_window_get_height(window);
1062 if (x >= 0 && x < w && y >= 0 && y < h)
1064 vd_dnd_drop_update(vd, x, y);
1068 vd->drop_scroll_id = 0;
1069 return G_SOURCE_REMOVE;
1072 static gboolean vd_auto_scroll_notify_cb(GtkWidget *, gint, gint, gpointer data)
1074 auto vd = static_cast<ViewDir *>(data);
1076 if (!vd->drop_fd || vd->drop_list) return FALSE;
1078 if (!vd->drop_scroll_id) vd->drop_scroll_id = g_idle_add(vd_auto_scroll_idle_cb, vd);
1083 static gboolean vd_dnd_drop_motion(GtkWidget *, GdkDragContext *context, gint x, gint y, guint time, gpointer data)
1085 auto vd = static_cast<ViewDir *>(data);
1087 vd->click_fd = nullptr;
1089 if (gtk_drag_get_source_widget(context) == vd->view)
1091 /* from same window */
1092 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1096 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
1098 vd_dnd_drop_update(vd, x, y);
1102 GtkAdjustment *adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vd->view));
1103 widget_auto_scroll_start(vd->view, adj, -1, -1, vd_auto_scroll_notify_cb, vd);
1109 static void vd_dnd_drop_leave(GtkWidget *, GdkDragContext *, guint, gpointer data)
1111 auto vd = static_cast<ViewDir *>(data);
1113 if (vd->drop_fd != vd->click_fd) vd_color_set(vd, vd->drop_fd, FALSE);
1115 vd->drop_fd = nullptr;
1117 if (vd->dnd_drop_leave_func) vd->dnd_drop_leave_func(vd);
1120 void vd_dnd_init(ViewDir *vd)
1122 gtk_drag_source_set(vd->view, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
1123 dnd_file_drag_types, dnd_file_drag_types_count,
1124 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK));
1125 g_signal_connect(G_OBJECT(vd->view), "drag_data_get",
1126 G_CALLBACK(vd_dnd_get), vd);
1127 g_signal_connect(G_OBJECT(vd->view), "drag_begin",
1128 G_CALLBACK(vd_dnd_begin), vd);
1129 g_signal_connect(G_OBJECT(vd->view), "drag_end",
1130 G_CALLBACK(vd_dnd_end), vd);
1132 vd_dest_set(vd, TRUE);
1133 g_signal_connect(G_OBJECT(vd->view), "drag_data_received",
1134 G_CALLBACK(vd_dnd_drop_receive), vd);
1135 g_signal_connect(G_OBJECT(vd->view), "drag_motion",
1136 G_CALLBACK(vd_dnd_drop_motion), vd);
1137 g_signal_connect(G_OBJECT(vd->view), "drag_leave",
1138 G_CALLBACK(vd_dnd_drop_leave), vd);
1142 *----------------------------------------------------------------------------
1144 *----------------------------------------------------------------------------
1147 #pragma GCC diagnostic push
1148 #pragma GCC diagnostic ignored "-Wunused-function"
1149 void vd_menu_position_cb_unused(GtkMenu *menu, gint *x, gint *y, gboolean *, gpointer data)
1151 auto *vd = static_cast<ViewDir *>(data);
1152 GtkTreeModel *store;
1158 if (!vd_find_row(vd, vd->click_fd, &iter)) return;
1159 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
1160 tpath = gtk_tree_model_get_path(store, &iter);
1161 tree_view_get_cell_clamped(GTK_TREE_VIEW(vd->view), tpath, 0, TRUE, x, y, &cw, &ch);
1162 gtk_tree_path_free(tpath);
1164 popup_menu_position_clamp(menu, x, y, 0);
1166 #pragma GCC diagnostic pop
1168 void vd_activate_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *, gpointer data)
1170 auto vd = static_cast<ViewDir *>(data);
1171 FileData *fd = vd_get_fd_from_tree_path(vd, tview, tpath);
1173 vd_select_row(vd, fd);
1176 static GdkRGBA *vd_color_shifted(GtkWidget *widget)
1178 static GdkRGBA color;
1179 static GtkWidget *done = nullptr;
1182 /* @FIXME GTK4 no background color */
1186 GtkStyleContext *style_context;
1188 style_context = gtk_widget_get_style_context(widget);
1189 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &color);
1191 shift_color(&color, -1, 0);
1199 void vd_color_cb(GtkTreeViewColumn *, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1201 auto vd = static_cast<ViewDir *>(data);
1204 gtk_tree_model_get(tree_model, iter, DIR_COLUMN_COLOR, &set, -1);
1205 g_object_set(G_OBJECT(cell),
1206 "cell-background-rgba", vd_color_shifted(vd->view),
1207 "cell-background-set", set, NULL);
1210 gboolean vd_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1212 auto vd = static_cast<ViewDir *>(data);
1214 FileData *fd = nullptr;
1216 if (defined_mouse_buttons(widget, bevent, vd->layout))
1221 if (vd->type == DIRVIEW_LIST && !options->view_dir_list_single_click_enter)
1224 if (!vd->click_fd) return FALSE;
1225 vd_color_set(vd, vd->click_fd, FALSE);
1227 if (bevent->button != MOUSE_BUTTON_LEFT) return TRUE;
1229 if ((bevent->x != 0 || bevent->y != 0) &&
1230 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
1231 &tpath, nullptr, nullptr, nullptr))
1233 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
1234 gtk_tree_path_free(tpath);
1237 if (fd && vd->click_fd == fd)
1239 vd_select_row(vd, vd->click_fd);
1245 gboolean vd_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1247 auto vd = static_cast<ViewDir *>(data);
1248 gboolean ret = FALSE;
1252 case DIRVIEW_LIST: ret = vdlist_press_key_cb(widget, event, data); break;
1253 case DIRVIEW_TREE: ret = vdtree_press_key_cb(widget, event, data); break;
1259 gboolean vd_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1261 auto vd = static_cast<ViewDir *>(data);
1262 gboolean ret = FALSE;
1266 NodeData *nd = nullptr;
1267 GtkTreeModel *store;
1269 if (bevent->button == MOUSE_BUTTON_RIGHT)
1271 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, &tpath, nullptr, nullptr, nullptr))
1273 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1274 gtk_tree_model_get_iter(store, &iter, tpath);
1279 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
1283 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
1284 vd->click_fd = (nd) ? nd->fd : nullptr;
1289 vd_color_set(vd, vd->click_fd, TRUE);
1293 vd->popup = vd_pop_menu(vd, vd->click_fd);
1294 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1301 case DIRVIEW_LIST: ret = vdlist_press_cb(widget, bevent, data); break;
1302 case DIRVIEW_TREE: ret = vdtree_press_cb(widget, bevent, data); break;
1308 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data)
1310 auto vd = static_cast<ViewDir *>(data);
1314 if (!S_ISDIR(fd->mode)) return; /* this gives correct results even on recently deleted files/directories */
1316 DEBUG_1("Notify vd: %s %04x", fd->path, type);
1318 base = remove_level_from_path(fd->path);
1320 if (vd->type == DIRVIEW_LIST)
1322 refresh = (fd == vd->dir_fd);
1326 refresh = (strcmp(base, vd->dir_fd->path) == 0);
1329 if ((type & NOTIFY_CHANGE) && fd->change)
1331 if (!refresh && fd->change->dest)
1333 gchar *dest_base = remove_level_from_path(fd->change->dest);
1334 refresh = (strcmp(dest_base, vd->dir_fd->path) == 0);
1338 if (!refresh && fd->change->source)
1340 gchar *source_base = remove_level_from_path(fd->change->source);
1341 refresh = (strcmp(source_base, vd->dir_fd->path) == 0);
1342 g_free(source_base);
1346 if (refresh) vd_refresh(vd);
1349 if (vd->type == DIRVIEW_TREE)
1352 FileData *base_fd = file_data_new_dir(base);
1354 if (vd_find_row(vd, base_fd, &iter))
1356 vdtree_populate_path_by_iter(vd, &iter, TRUE, vd->dir_fd);
1359 file_data_unref(base_fd);
1364 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */