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.
29 #include "layout-image.h"
31 #include "ui-fileops.h"
32 #include "ui-tree-edit.h"
36 #include "uri-utils.h"
37 #include "view-dir-list.h"
38 #include "view-dir-tree.h"
43 /** @FIXME Emblems should be attached to icons via e.g.:
45 * icon = g_themed_icon_new("folder");
46 * emblem_icon = g_themed_icon_new("emblem_symbolic_link");
47 * emblem = g_emblem_new(emblem_icon);
48 * emblemed = g_emblemed_icon_new(icon, emblem);
49 * gtk_icon_info_load_icon(icon_info, NULL)
50 * But there does not seem to be a way to get GtkIconInfo from a GIcon
52 GdkPixbuf *create_folder_icon_with_emblem(GtkIconTheme *icon_theme, const gchar *emblem, const gchar *fallback_icon, gint size)
54 if (!gtk_icon_theme_has_icon(icon_theme, emblem))
56 return gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
59 GError *error = nullptr;
60 GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, emblem, size, GTK_ICON_LOOKUP_USE_BUILTIN, &error);
64 log_printf("Error: %s\n", error->message);
66 pixbuf = gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
70 GdkPixbuf *directory_pixbuf = gtk_icon_theme_load_icon(icon_theme, GQ_ICON_DIRECTORY, size, GTK_ICON_LOOKUP_USE_BUILTIN, nullptr);
71 pixbuf = gdk_pixbuf_copy(directory_pixbuf);
72 g_object_unref(directory_pixbuf);
74 gint scale = gdk_pixbuf_get_width(icon) / 2;
75 gdk_pixbuf_composite(icon, pixbuf, scale, scale, scale, scale, scale, scale, 0.5, 0.5, GDK_INTERP_HYPER, 255);
82 /* Folders icons to be used in tree or list directory view */
83 PixmapFolders *folder_icons_new()
85 auto pf = g_new0(PixmapFolders, 1);
86 GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
89 if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &size, &size))
94 pf->close = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_DIRECTORY, size, GTK_ICON_LOOKUP_USE_BUILTIN);
95 pf->open = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_OPEN, size, GTK_ICON_LOOKUP_USE_BUILTIN);
96 pf->parent = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_GO_UP, size, GTK_ICON_LOOKUP_USE_BUILTIN);
98 pf->deny = create_folder_icon_with_emblem(icon_theme, GQ_ICON_UNREADABLE, GQ_ICON_STOP, size);
99 pf->link = create_folder_icon_with_emblem(icon_theme, GQ_ICON_LINK, GQ_ICON_REDO, size);
100 pf->read_only = create_folder_icon_with_emblem(icon_theme, GQ_ICON_READONLY, GQ_ICON_DIRECTORY, size);
105 void folder_icons_free(PixmapFolders *pf)
109 g_object_unref(pf->close);
110 g_object_unref(pf->open);
111 g_object_unref(pf->deny);
112 g_object_unref(pf->parent);
113 g_object_unref(pf->link);
114 g_object_unref(pf->read_only);
121 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data);
123 static void vd_destroy_cb(GtkWidget *widget, gpointer data)
125 auto vd = static_cast<ViewDir *>(data);
127 file_data_unregister_notify_func(vd_notify_cb, vd);
131 g_signal_handlers_disconnect_matched(G_OBJECT(vd->popup), G_SIGNAL_MATCH_DATA,
132 0, 0, nullptr, nullptr, vd);
133 g_object_unref(vd->popup);
138 case DIRVIEW_LIST: vdlist_destroy_cb(widget, data); break;
139 case DIRVIEW_TREE: vdtree_destroy_cb(widget, data); break;
142 if (vd->pf) folder_icons_free(vd->pf);
143 if (vd->drop_list) filelist_free(vd->drop_list);
145 if (vd->dir_fd) file_data_unref(vd->dir_fd);
146 if (vd->info) g_free(vd->info);
151 ViewDir *vd_new(LayoutWindow *lw)
153 auto vd = g_new0(ViewDir, 1);
155 vd->widget = gq_gtk_scrolled_window_new(nullptr, nullptr);
156 gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vd->widget), GTK_SHADOW_IN);
157 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vd->widget),
158 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
161 vd->pf = folder_icons_new();
163 switch (lw->options.dir_view_type)
165 case DIRVIEW_LIST: vd = vdlist_new(vd, lw->dir_fd); break;
166 case DIRVIEW_TREE: vd = vdtree_new(vd, lw->dir_fd); break;
169 gtk_container_add(GTK_CONTAINER(vd->widget), vd->view);
173 g_signal_connect(G_OBJECT(vd->view), "row_activated",
174 G_CALLBACK(vd_activate_cb), vd);
175 g_signal_connect(G_OBJECT(vd->widget), "destroy",
176 G_CALLBACK(vd_destroy_cb), vd);
177 g_signal_connect(G_OBJECT(vd->view), "key_press_event",
178 G_CALLBACK(vd_press_key_cb), vd);
179 g_signal_connect(G_OBJECT(vd->view), "button_press_event",
180 G_CALLBACK(vd_press_cb), vd);
181 g_signal_connect(G_OBJECT(vd->view), "button_release_event",
182 G_CALLBACK(vd_release_cb), vd);
184 file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
186 /* vd_set_fd expects that vd_notify_cb is already registered */
187 if (lw->dir_fd) vd_set_fd(vd, lw->dir_fd);
189 gtk_widget_show(vd->view);
194 void vd_set_select_func(ViewDir *vd,
195 void (*func)(ViewDir *vd, FileData *fd, gpointer data), gpointer data)
197 vd->select_func = func;
198 vd->select_data = data;
201 #pragma GCC diagnostic push
202 #pragma GCC diagnostic ignored "-Wunused-function"
203 void vd_set_layout_unused(ViewDir *vd, LayoutWindow *layout)
207 #pragma GCC diagnostic pop
209 gboolean vd_set_fd(ViewDir *vd, FileData *dir_fd)
211 gboolean ret = FALSE;
213 file_data_unregister_notify_func(vd_notify_cb, vd);
217 case DIRVIEW_LIST: ret = vdlist_set_fd(vd, dir_fd); break;
218 case DIRVIEW_TREE: ret = vdtree_set_fd(vd, dir_fd); break;
221 file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
226 void vd_refresh(ViewDir *vd)
230 case DIRVIEW_LIST: vdlist_refresh(vd); break;
231 case DIRVIEW_TREE: vdtree_refresh(vd); break;
235 #pragma GCC diagnostic push
236 #pragma GCC diagnostic ignored "-Wunused-function"
237 const gchar *vd_row_get_path_unused(ViewDir *vd, gint row)
239 const gchar *ret = nullptr;
243 case DIRVIEW_LIST: ret = vdlist_row_get_path(vd, row); break;
244 case DIRVIEW_TREE: ret = vdtree_row_get_path(vd, row); break;
249 #pragma GCC diagnostic pop
251 /* the calling stack is this:
252 vd_select_row -> select_func -> layout_set_fd -> vd_set_fd
254 void vd_select_row(ViewDir *vd, FileData *fd)
256 if (fd && vd->select_func)
258 vd->select_func(vd, fd, vd->select_data);
262 gboolean vd_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
264 gboolean ret = FALSE;
268 case DIRVIEW_LIST: ret = vdlist_find_row(vd, fd, iter); break;
269 case DIRVIEW_TREE: ret = vdtree_find_row(vd, fd, iter, nullptr); break;
275 FileData *vd_get_fd_from_tree_path(ViewDir *vd, GtkTreeView *tview, GtkTreePath *tpath)
278 FileData *fd = nullptr;
281 store = gtk_tree_view_get_model(tview);
282 gtk_tree_model_get_iter(store, &iter, tpath);
286 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
291 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
292 fd = (nd) ? nd->fd : nullptr;
300 static void vd_rename_finished_cb(gboolean success, const gchar *new_path, gpointer data)
302 auto vd = static_cast<ViewDir *>(data);
305 FileData *fd = file_data_new_dir(new_path);
308 if (vd_find_row(vd, fd, &iter))
310 tree_view_row_make_visible(GTK_TREE_VIEW(vd->view), &iter, TRUE);
317 static gboolean vd_rename_cb(TreeEditData *td, const gchar *, const gchar *new_name, gpointer data)
319 auto vd = static_cast<ViewDir *>(data);
324 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), td->path);
325 if (!fd) return FALSE;
327 base = remove_level_from_path(fd->path);
328 new_path = g_build_filename(base, new_name, NULL);
331 file_util_rename_dir(fd, new_path, vd->view, vd_rename_finished_cb, vd);
338 static void vd_rename_by_data(ViewDir *vd, FileData *fd)
344 if (!fd || !vd_find_row(vd, fd, &iter)) return;
345 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
346 tpath = gtk_tree_model_get_path(store, &iter);
348 tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
350 gtk_tree_path_free(tpath);
354 void vd_color_set(ViewDir *vd, FileData *fd, gint color_set)
359 if (!vd_find_row(vd, fd, &iter)) return;
360 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
365 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
368 gtk_tree_store_set(GTK_TREE_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
373 void vd_popup_destroy_cb(GtkWidget *, gpointer data)
375 auto vd = static_cast<ViewDir *>(data);
377 vd_color_set(vd, vd->click_fd, FALSE);
378 vd->click_fd = nullptr;
381 vd_color_set(vd, vd->drop_fd, FALSE);
382 filelist_free(vd->drop_list);
383 vd->drop_list = nullptr;
384 vd->drop_fd = nullptr;
388 *-----------------------------------------------------------------------------
389 * drop menu (from dnd)
390 *-----------------------------------------------------------------------------
393 static void vd_drop_menu_copy_cb(GtkWidget *, gpointer data)
395 auto vd = static_cast<ViewDir *>(data);
399 if (!vd->drop_fd) return;
401 path = vd->drop_fd->path;
402 list = vd->drop_list;
403 vd->drop_list = nullptr;
405 file_util_copy_simple(list, path, vd->widget);
408 static void vd_drop_menu_move_cb(GtkWidget *, gpointer data)
410 auto vd = static_cast<ViewDir *>(data);
414 if (!vd->drop_fd) return;
416 path = vd->drop_fd->path;
417 list = vd->drop_list;
419 vd->drop_list = nullptr;
421 file_util_move_simple(list, path, vd->widget);
424 static void vd_drop_menu_filter_cb(GtkWidget *widget, gpointer data)
426 auto vd = static_cast<ViewDir *>(data);
431 if (!vd->drop_fd) return;
433 key = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "filter_key"));
435 path = vd->drop_fd->path;
436 list = vd->drop_list;
438 vd->drop_list = nullptr;
440 file_util_start_filter_from_filelist(key, list, path, vd->widget);
443 static void vd_drop_menu_edit_item_free(gpointer data)
448 GtkWidget *vd_drop_menu(ViewDir *vd, gint active)
451 GList *editors_list = editor_list_get();
452 GList *work = editors_list;
454 menu = popup_menu_short_lived();
455 g_signal_connect(G_OBJECT(menu), "destroy",
456 G_CALLBACK(vd_popup_destroy_cb), vd);
458 menu_item_add_icon_sensitive(menu, _("_Copy"), GQ_ICON_COPY, active,
459 G_CALLBACK(vd_drop_menu_copy_cb), vd);
460 menu_item_add_sensitive(menu, _("_Move"), active, G_CALLBACK(vd_drop_menu_move_cb), vd);
465 auto editor = static_cast<const EditorDescription *>(work->data);
469 if (!editor_is_filter(editor->key)) continue;
470 key = g_strdup(editor->key);
471 item = menu_item_add_sensitive(menu, editor->name, active, G_CALLBACK(vd_drop_menu_filter_cb), vd);
472 g_object_set_data_full(G_OBJECT(item), "filter_key", key, vd_drop_menu_edit_item_free);
475 g_list_free(editors_list);
477 menu_item_add_divider(menu);
478 menu_item_add_icon(menu, _("Cancel"), GQ_ICON_CANCEL, nullptr, vd);
484 *-----------------------------------------------------------------------------
486 *-----------------------------------------------------------------------------
489 static void vd_pop_menu_up_cb(GtkWidget *, gpointer data)
491 auto vd = static_cast<ViewDir *>(data);
494 if (!vd->dir_fd || strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) == 0) return;
495 path = remove_level_from_path(vd->dir_fd->path);
499 FileData *fd = file_data_new_dir(path);
500 vd->select_func(vd, fd, vd->select_data);
507 static void vd_pop_menu_slide_cb(GtkWidget *, gpointer data)
509 auto vd = static_cast<ViewDir *>(data);
511 if (!vd->layout) return;
512 if (!vd->click_fd) return;
514 layout_set_fd(vd->layout, vd->click_fd);
515 layout_select_none(vd->layout);
516 layout_image_slideshow_stop(vd->layout);
517 layout_image_slideshow_start(vd->layout);
520 static void vd_pop_menu_slide_rec_cb(GtkWidget *, gpointer data)
522 auto vd = static_cast<ViewDir *>(data);
525 if (!vd->layout) return;
526 if (!vd->click_fd) return;
528 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);
530 layout_image_slideshow_stop(vd->layout);
531 layout_image_slideshow_start_from_list(vd->layout, list);
534 static void vd_pop_menu_dupe(ViewDir *vd, gint recursive)
537 GList *list = nullptr;
539 if (!vd->click_fd) return;
543 list = g_list_append(list, file_data_ref(vd->click_fd));
547 filelist_read(vd->click_fd, &list, nullptr);
548 list = filelist_filter(list, FALSE);
551 dw = dupe_window_new();
552 dupe_window_add_files(dw, list, recursive);
557 static void vd_pop_menu_dupe_cb(GtkWidget *, gpointer data)
559 auto vd = static_cast<ViewDir *>(data);
560 vd_pop_menu_dupe(vd, FALSE);
563 static void vd_pop_menu_dupe_rec_cb(GtkWidget *, gpointer data)
565 auto vd = static_cast<ViewDir *>(data);
566 vd_pop_menu_dupe(vd, TRUE);
569 static void vd_pop_menu_delete_cb(GtkWidget *, gpointer data)
571 auto vd = static_cast<ViewDir *>(data);
573 if (!vd->click_fd) return;
574 file_util_delete_dir(vd->click_fd, vd->widget);
577 static void vd_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
579 auto vd = static_cast<ViewDir *>(data);
581 if (!vd->click_fd) return;
583 file_util_copy_path_to_clipboard(vd->click_fd, TRUE);
586 static void vd_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
588 auto vd = static_cast<ViewDir *>(data);
590 if (!vd->click_fd) return;
592 file_util_copy_path_to_clipboard(vd->click_fd, FALSE);
595 static void vd_pop_submenu_dir_view_as_cb(GtkWidget *widget, gpointer data)
597 auto vd = static_cast<ViewDir *>(data);
599 auto new_type = static_cast<DirViewType>(GPOINTER_TO_INT((g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"))));
600 layout_views_set(vd->layout, new_type, vd->layout->options.file_view_type);
603 static void vd_pop_menu_refresh_cb(GtkWidget *, gpointer data)
605 auto vd = static_cast<ViewDir *>(data);
607 if (vd->layout) layout_refresh(vd->layout);
610 static void vd_toggle_show_hidden_files_cb(GtkWidget *, gpointer data)
612 auto vd = static_cast<ViewDir *>(data);
614 options->file_filter.show_hidden_files = !options->file_filter.show_hidden_files;
615 if (vd->layout) layout_refresh(vd->layout);
618 static void vd_pop_menu_new_folder_cb(gboolean success, const gchar *new_path, gpointer data)
620 auto vd = static_cast<ViewDir *>(data);
621 FileData *fd = nullptr;
626 if (!success) return;
633 fd = vdlist_row_by_path(vd, new_path, nullptr);
638 FileData *new_fd = file_data_new_dir(new_path);
639 fd = vdtree_populate_path(vd, new_fd, TRUE, TRUE);
640 file_data_unref(new_fd);
645 if (!fd || !vd_find_row(vd, fd, &iter)) return;
646 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
647 tpath = gtk_tree_model_get_path(store, &iter);
648 gtk_tree_view_set_cursor(GTK_TREE_VIEW(vd->view), tpath, nullptr, FALSE);
650 gtk_tree_path_free(tpath);
653 static void vd_pop_menu_new_cb(GtkWidget *, gpointer data)
655 auto vd = static_cast<ViewDir *>(data);
656 FileData *dir_fd = nullptr;
662 if (!vd->dir_fd) return;
668 if (!vd->click_fd) return;
669 dir_fd = vd->click_fd;
674 file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
677 static void vd_pop_menu_rename_cb(GtkWidget *, gpointer data)
679 auto vd = static_cast<ViewDir *>(data);
681 vd_rename_by_data(vd, vd->click_fd);
684 static void vd_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
686 auto vd = static_cast<ViewDir *>(data);
691 if (!vd->layout) return;
693 ascend = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
694 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);
696 if (vd->layout) layout_refresh(vd->layout);
699 static void vd_pop_menu_sort_case_cb(GtkWidget *widget, gpointer data)
701 auto vd = static_cast<ViewDir *>(data);
702 gboolean case_sensitive;
706 if (!vd->layout) return;
708 case_sensitive = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
709 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);
711 if (vd->layout) layout_refresh(vd->layout);
714 static void vd_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
719 vd = static_cast<ViewDir *>(submenu_item_get_data(widget));
722 if (!vd->layout) return;
724 type = static_cast<SortType>GPOINTER_TO_INT(data);
726 if (type == SORT_NAME || type == SORT_NUMBER || type == SORT_TIME)
728 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);
730 if (vd->layout) layout_refresh(vd->layout);
734 GtkWidget *vd_pop_menu(ViewDir *vd, FileData *fd)
738 gboolean rename_delete_active = FALSE;
739 gboolean new_folder_active = FALSE;
743 active = (fd != nullptr);
748 /* check using . (always row 0) */
749 new_folder_active = (vd->dir_fd && access_file(vd->dir_fd->path , W_OK | X_OK));
751 /* ignore .. and . */
752 rename_delete_active = (new_folder_active && fd &&
753 strcmp(fd->name, ".") != 0 &&
754 strcmp(fd->name, "..") != 0 &&
755 access_file(fd->path, W_OK | X_OK));
763 new_folder_active = (fd && access_file(fd->path, W_OK | X_OK));
764 parent = remove_level_from_path(fd->path);
765 rename_delete_active = access_file(parent, W_OK | X_OK);
772 menu = popup_menu_short_lived();
773 g_signal_connect(G_OBJECT(menu), "destroy",
774 G_CALLBACK(vd_popup_destroy_cb), vd);
776 menu_item_add_icon_sensitive(menu, _("_Up to parent"), GQ_ICON_GO_UP,
777 (vd->dir_fd && strcmp(vd->dir_fd->path, G_DIR_SEPARATOR_S) != 0),
778 G_CALLBACK(vd_pop_menu_up_cb), vd);
780 menu_item_add_divider(menu);
781 menu_item_add_sensitive(menu, _("_Slideshow"), active,
782 G_CALLBACK(vd_pop_menu_slide_cb), vd);
783 menu_item_add_sensitive(menu, _("Slideshow recursive"), active,
784 G_CALLBACK(vd_pop_menu_slide_rec_cb), vd);
786 menu_item_add_divider(menu);
787 menu_item_add_icon_sensitive(menu, _("Find _duplicates..."), GQ_ICON_FIND, active,
788 G_CALLBACK(vd_pop_menu_dupe_cb), vd);
789 menu_item_add_icon_sensitive(menu, _("Find duplicates recursive..."), GQ_ICON_FIND, active,
790 G_CALLBACK(vd_pop_menu_dupe_rec_cb), vd);
792 menu_item_add_divider(menu);
794 menu_item_add_sensitive(menu, _("_New folder..."), new_folder_active,
795 G_CALLBACK(vd_pop_menu_new_cb), vd);
797 menu_item_add_sensitive(menu, _("_Rename..."), rename_delete_active,
798 G_CALLBACK(vd_pop_menu_rename_cb), vd);
800 menu_item_add(menu, _("_Copy path"),
801 G_CALLBACK(vd_pop_menu_copy_path_cb), vd);
803 menu_item_add(menu, _("_Copy path unquoted"),
804 G_CALLBACK(vd_pop_menu_copy_path_unquoted_cb), vd);
806 menu_item_add_icon_sensitive(menu, _("_Delete..."), GQ_ICON_DELETE, rename_delete_active,
807 G_CALLBACK(vd_pop_menu_delete_cb), vd);
808 menu_item_add_divider(menu);
811 menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(DIRVIEW_LIST), vd->type == DIRVIEW_LIST,
812 G_CALLBACK(vd_pop_submenu_dir_view_as_cb), vd);
814 menu_item_add_radio(menu, _("View as _Tree"), GINT_TO_POINTER(DIRVIEW_TREE), vd->type == DIRVIEW_TREE,
815 G_CALLBACK(vd_pop_submenu_dir_view_as_cb), vd);
817 if (vd->type == DIRVIEW_LIST)
819 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);
820 menu_item_add_check(submenu, _("Ascending"), vd->layout->options.dir_view_list_sort.ascend, G_CALLBACK(vd_pop_menu_sort_ascend_cb), (vd));
821 menu_item_add_check(submenu, _("Case"), vd->layout->options.dir_view_list_sort.case_sensitive, G_CALLBACK(vd_pop_menu_sort_case_cb), (vd));
822 item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
823 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
826 if (vd->type == DIRVIEW_TREE)
828 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);
829 item = menu_item_add(menu, _("_Sort"), nullptr, nullptr);
830 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
833 menu_item_add_divider(menu);
835 menu_item_add_check(menu, _("Show _hidden files"), options->file_filter.show_hidden_files,
836 G_CALLBACK(vd_toggle_show_hidden_files_cb), vd);
838 menu_item_add_icon(menu, _("Re_fresh"), GQ_ICON_REFRESH,
839 G_CALLBACK(vd_pop_menu_refresh_cb), vd);
844 void vd_new_folder(ViewDir *vd, FileData *dir_fd)
846 file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
850 *-----------------------------------------------------------------------------
852 *-----------------------------------------------------------------------------
855 static GtkTargetEntry vd_dnd_drop_types[] = {
856 { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
858 static gint vd_dnd_drop_types_count = 1;
860 static void vd_dest_set(ViewDir *vd, gint enable)
864 gtk_drag_dest_set(vd->view,
865 static_cast<GtkDestDefaults>(GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP),
866 vd_dnd_drop_types, vd_dnd_drop_types_count,
867 static_cast<GdkDragAction>(GDK_ACTION_MOVE | GDK_ACTION_COPY));
871 gtk_drag_dest_unset(vd->view);
875 static void vd_dnd_get(GtkWidget *, GdkDragContext *,
876 GtkSelectionData *selection_data, guint info,
877 guint, gpointer data)
879 auto vd = static_cast<ViewDir *>(data);
882 if (!vd->click_fd) return;
886 case TARGET_URI_LIST:
887 case TARGET_TEXT_PLAIN:
888 list = g_list_prepend(nullptr, vd->click_fd);
889 uri_selection_data_set_uris_from_filelist(selection_data, list);
895 static void vd_dnd_begin(GtkWidget *, GdkDragContext *, gpointer data)
897 auto vd = static_cast<ViewDir *>(data);
899 vd_color_set(vd, vd->click_fd, TRUE);
900 vd_dest_set(vd, FALSE);
903 static void vd_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
905 auto vd = static_cast<ViewDir *>(data);
907 vd_color_set(vd, vd->click_fd, FALSE);
909 if (vd->type == DIRVIEW_LIST && gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
913 vd_dest_set(vd, TRUE);
916 static void vd_dnd_drop_receive(GtkWidget *widget, GdkDragContext *,
918 GtkSelectionData *selection_data, guint info,
919 guint, gpointer data)
921 auto vd = static_cast<ViewDir *>(data);
923 FileData *fd = nullptr;
924 GdkDragAction action = GDK_ACTION_ASK;
926 vd->click_fd = nullptr;
928 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y,
929 &tpath, nullptr, nullptr, nullptr))
931 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
932 gtk_tree_path_free(tpath);
937 if (info == TARGET_URI_LIST)
941 gboolean done = FALSE;
943 list = uri_filelist_from_gtk_selection_data(selection_data);
946 active = access_file(fd->path, W_OK | X_OK);
948 vd_color_set(vd, fd, TRUE);
952 /** @FIXME With GTK2 gdk_drag_context_get_actions() shows the state of the
953 * shift and control keys during the drag operation. With GTK3 this is not
954 * so. This is a workaround.
956 GdkModifierType mask;
958 gdk_window_get_pointer(gtk_widget_get_window(widget), nullptr, nullptr, &mask);
959 if (mask & GDK_CONTROL_MASK)
961 action = GDK_ACTION_COPY;
963 else if (mask & GDK_SHIFT_MASK)
965 action = GDK_ACTION_MOVE;
968 if (action != GDK_ACTION_COPY && action != GDK_ACTION_MOVE)
970 if (options->dnd_default_action == DND_ACTION_COPY)
972 action = GDK_ACTION_COPY;
974 else if (options->dnd_default_action == DND_ACTION_MOVE)
976 action = GDK_ACTION_MOVE;
980 if (action == GDK_ACTION_COPY)
982 file_util_copy_simple(list, fd->path, vd->widget);
986 else if (action == GDK_ACTION_MOVE)
988 file_util_move_simple(list, fd->path, vd->widget);
996 vd->popup = vd_drop_menu(vd, active);
997 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1001 vd->drop_list = list;
1005 static void vd_dnd_drop_update(ViewDir *vd, gint x, gint y)
1008 FileData *fd = nullptr;
1010 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vd->view), x, y,
1011 &tpath, nullptr, nullptr, nullptr))
1013 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), tpath);
1014 gtk_tree_path_free(tpath);
1017 if (fd != vd->drop_fd)
1019 vd_color_set(vd, vd->drop_fd, FALSE);
1020 vd_color_set(vd, fd, TRUE);
1021 if (fd && vd->dnd_drop_update_func) vd->dnd_drop_update_func(vd);
1027 void vd_dnd_drop_scroll_cancel(ViewDir *vd)
1029 if (vd->drop_scroll_id)
1031 g_source_remove(vd->drop_scroll_id);
1032 vd->drop_scroll_id = 0;
1036 static gboolean vd_auto_scroll_idle_cb(gpointer data)
1038 auto vd = static_cast<ViewDir *>(data);
1048 window = gtk_widget_get_window(vd->view);
1049 seat = gdk_display_get_default_seat(gdk_window_get_display(window));
1050 device = gdk_seat_get_pointer(seat);
1051 gdk_window_get_device_position(window, device, &x, &y, nullptr);
1053 w = gdk_window_get_width(window);
1054 h = gdk_window_get_height(window);
1055 if (x >= 0 && x < w && y >= 0 && y < h)
1057 vd_dnd_drop_update(vd, x, y);
1061 vd->drop_scroll_id = 0;
1062 return G_SOURCE_REMOVE;
1065 static gboolean vd_auto_scroll_notify_cb(GtkWidget *, gint, gint, gpointer data)
1067 auto vd = static_cast<ViewDir *>(data);
1069 if (!vd->drop_fd || vd->drop_list) return FALSE;
1071 if (!vd->drop_scroll_id) vd->drop_scroll_id = g_idle_add(vd_auto_scroll_idle_cb, vd);
1076 static gboolean vd_dnd_drop_motion(GtkWidget *, GdkDragContext *context, gint x, gint y, guint time, gpointer data)
1078 auto vd = static_cast<ViewDir *>(data);
1080 vd->click_fd = nullptr;
1082 if (gtk_drag_get_source_widget(context) == vd->view)
1084 /* from same window */
1085 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1089 gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
1091 vd_dnd_drop_update(vd, x, y);
1095 GtkAdjustment *adj = gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(vd->view));
1096 widget_auto_scroll_start(vd->view, adj, -1, -1, vd_auto_scroll_notify_cb, vd);
1102 static void vd_dnd_drop_leave(GtkWidget *, GdkDragContext *, guint, gpointer data)
1104 auto vd = static_cast<ViewDir *>(data);
1106 if (vd->drop_fd != vd->click_fd) vd_color_set(vd, vd->drop_fd, FALSE);
1108 vd->drop_fd = nullptr;
1110 if (vd->dnd_drop_leave_func) vd->dnd_drop_leave_func(vd);
1113 void vd_dnd_init(ViewDir *vd)
1115 gtk_drag_source_set(vd->view, static_cast<GdkModifierType>(GDK_BUTTON1_MASK | GDK_BUTTON2_MASK),
1116 dnd_file_drag_types, dnd_file_drag_types_count,
1117 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK));
1118 g_signal_connect(G_OBJECT(vd->view), "drag_data_get",
1119 G_CALLBACK(vd_dnd_get), vd);
1120 g_signal_connect(G_OBJECT(vd->view), "drag_begin",
1121 G_CALLBACK(vd_dnd_begin), vd);
1122 g_signal_connect(G_OBJECT(vd->view), "drag_end",
1123 G_CALLBACK(vd_dnd_end), vd);
1125 vd_dest_set(vd, TRUE);
1126 g_signal_connect(G_OBJECT(vd->view), "drag_data_received",
1127 G_CALLBACK(vd_dnd_drop_receive), vd);
1128 g_signal_connect(G_OBJECT(vd->view), "drag_motion",
1129 G_CALLBACK(vd_dnd_drop_motion), vd);
1130 g_signal_connect(G_OBJECT(vd->view), "drag_leave",
1131 G_CALLBACK(vd_dnd_drop_leave), vd);
1135 *----------------------------------------------------------------------------
1137 *----------------------------------------------------------------------------
1140 #pragma GCC diagnostic push
1141 #pragma GCC diagnostic ignored "-Wunused-function"
1142 void vd_menu_position_cb_unused(GtkMenu *menu, gint *x, gint *y, gboolean *, gpointer data)
1144 auto *vd = static_cast<ViewDir *>(data);
1145 GtkTreeModel *store;
1150 if (!vd_find_row(vd, vd->click_fd, &iter)) return;
1151 store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
1152 tpath = gtk_tree_model_get_path(store, &iter);
1153 tree_view_get_cell_clamped(GTK_TREE_VIEW(vd->view), tpath, 0, TRUE, x, y, &cw, &ch);
1154 gtk_tree_path_free(tpath);
1156 popup_menu_position_clamp(menu, x, y, 0);
1158 #pragma GCC diagnostic pop
1160 void vd_activate_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *, gpointer data)
1162 auto vd = static_cast<ViewDir *>(data);
1163 FileData *fd = vd_get_fd_from_tree_path(vd, tview, tpath);
1165 vd_select_row(vd, fd);
1168 static GdkRGBA *vd_color_shifted(GtkWidget *widget)
1170 static GdkRGBA color;
1171 static GdkRGBA color_style;
1172 static GtkWidget *done = nullptr;
1175 /* @FIXME GTK4 no background color */
1179 GtkStyleContext *style_context;
1181 style_context = gtk_widget_get_style_context(widget);
1182 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &color_style);
1184 memcpy(&color, &color_style, sizeof(color_style));
1186 shift_color(&color, -1, 0);
1194 void vd_color_cb(GtkTreeViewColumn *, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1196 auto vd = static_cast<ViewDir *>(data);
1199 gtk_tree_model_get(tree_model, iter, DIR_COLUMN_COLOR, &set, -1);
1200 g_object_set(G_OBJECT(cell),
1201 "cell-background-rgba", vd_color_shifted(vd->view),
1202 "cell-background-set", set, NULL);
1205 gboolean vd_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1207 auto vd = static_cast<ViewDir *>(data);
1209 FileData *fd = nullptr;
1211 if (defined_mouse_buttons(widget, bevent, vd->layout))
1216 if (vd->type == DIRVIEW_LIST && !options->view_dir_list_single_click_enter)
1219 if (!vd->click_fd) return FALSE;
1220 vd_color_set(vd, vd->click_fd, FALSE);
1222 if (bevent->button != MOUSE_BUTTON_LEFT) return TRUE;
1224 if ((bevent->x != 0 || bevent->y != 0) &&
1225 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y,
1226 &tpath, nullptr, nullptr, nullptr))
1228 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
1229 gtk_tree_path_free(tpath);
1232 if (fd && vd->click_fd == fd)
1234 vd_select_row(vd, vd->click_fd);
1240 gboolean vd_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1242 auto vd = static_cast<ViewDir *>(data);
1243 gboolean ret = FALSE;
1247 case DIRVIEW_LIST: ret = vdlist_press_key_cb(widget, event, data); break;
1248 case DIRVIEW_TREE: ret = vdtree_press_key_cb(widget, event, data); break;
1254 gboolean vd_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1256 auto vd = static_cast<ViewDir *>(data);
1257 gboolean ret = FALSE;
1261 NodeData *nd = nullptr;
1262 GtkTreeModel *store;
1264 if (bevent->button == MOUSE_BUTTON_RIGHT)
1266 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, &tpath, nullptr, nullptr, nullptr))
1268 store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1269 gtk_tree_model_get_iter(store, &iter, tpath);
1274 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
1278 gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
1279 vd->click_fd = (nd) ? nd->fd : nullptr;
1284 vd_color_set(vd, vd->click_fd, TRUE);
1288 vd->popup = vd_pop_menu(vd, vd->click_fd);
1289 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1296 case DIRVIEW_LIST: ret = vdlist_press_cb(widget, bevent, data); break;
1297 case DIRVIEW_TREE: ret = vdtree_press_cb(widget, bevent, data); break;
1303 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data)
1305 auto vd = static_cast<ViewDir *>(data);
1309 if (!S_ISDIR(fd->mode)) return; /* this gives correct results even on recently deleted files/directories */
1311 DEBUG_1("Notify vd: %s %04x", fd->path, type);
1313 base = remove_level_from_path(fd->path);
1315 if (vd->type == DIRVIEW_LIST)
1317 refresh = (fd == vd->dir_fd);
1321 refresh = (strcmp(base, vd->dir_fd->path) == 0);
1324 if ((type & NOTIFY_CHANGE) && fd->change)
1326 if (!refresh && fd->change->dest)
1328 gchar *dest_base = remove_level_from_path(fd->change->dest);
1329 refresh = (strcmp(dest_base, vd->dir_fd->path) == 0);
1333 if (!refresh && fd->change->source)
1335 gchar *source_base = remove_level_from_path(fd->change->source);
1336 refresh = (strcmp(source_base, vd->dir_fd->path) == 0);
1337 g_free(source_base);
1341 if (refresh) vd_refresh(vd);
1344 if (vd->type == DIRVIEW_TREE)
1347 FileData *base_fd = file_data_new_dir(base);
1349 if (vd_find_row(vd, base_fd, &iter))
1351 vdtree_populate_path_by_iter(vd, &iter, TRUE, vd->dir_fd);
1354 file_data_unref(base_fd);
1359 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */