clang-tidy: bugprone-integer-division
[geeqie.git] / src / view-dir.cc
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Laurent Monin
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include "main.h"
22 #include "view-dir.h"
23
24 #include "dnd.h"
25 #include "dupe.h"
26 #include "editors.h"
27 #include "filedata.h"
28 #include "layout.h"
29 #include "layout-image.h"
30 #include "menu.h"
31 #include "ui-fileops.h"
32 #include "ui-tree-edit.h"
33 #include "ui-menu.h"
34 #include "ui-misc.h"
35 #include "utilops.h"
36 #include "uri-utils.h"
37 #include "view-dir-list.h"
38 #include "view-dir-tree.h"
39
40 namespace
41 {
42
43 /** @FIXME Emblems should be attached to icons via e.g.:
44  * GIcon *....
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
51  */
52 GdkPixbuf *create_folder_icon_with_emblem(GtkIconTheme *icon_theme, const gchar *emblem, const gchar *fallback_icon, gint size)
53 {
54         if (!gtk_icon_theme_has_icon(icon_theme, emblem))
55                 {
56                 return gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
57                 }
58
59         GError *error = nullptr;
60         GdkPixbuf *icon = gtk_icon_theme_load_icon(icon_theme, emblem, size, GTK_ICON_LOOKUP_USE_BUILTIN, &error);
61         GdkPixbuf *pixbuf;
62         if (error)
63                 {
64                 log_printf("Error: %s\n", error->message);
65                 g_error_free(error);
66                 pixbuf = gq_gtk_icon_theme_load_icon_copy(icon_theme, fallback_icon, size, GTK_ICON_LOOKUP_USE_BUILTIN);
67                 }
68         else
69                 {
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);
73                 
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);
76                 }
77         g_object_unref(icon);
78
79         return pixbuf;
80 }
81
82 /* Folders icons to be used in tree or list directory view */
83 PixmapFolders *folder_icons_new()
84 {
85         auto pf = g_new0(PixmapFolders, 1);
86         GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
87
88         gint size;
89         if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &size, &size))
90                 {
91                 size = 16;
92                 }
93
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);
97
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);
101
102         return pf;
103 }
104
105 void folder_icons_free(PixmapFolders *pf)
106 {
107         if (!pf) return;
108
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);
115
116         g_free(pf);
117 }
118
119 }
120
121 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data);
122
123 static void vd_destroy_cb(GtkWidget *widget, gpointer data)
124 {
125         auto vd = static_cast<ViewDir *>(data);
126
127         file_data_unregister_notify_func(vd_notify_cb, vd);
128
129         if (vd->popup)
130                 {
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);
134                 }
135
136         switch (vd->type)
137         {
138         case DIRVIEW_LIST: vdlist_destroy_cb(widget, data); break;
139         case DIRVIEW_TREE: vdtree_destroy_cb(widget, data); break;
140         }
141
142         if (vd->pf) folder_icons_free(vd->pf);
143         if (vd->drop_list) filelist_free(vd->drop_list);
144
145         if (vd->dir_fd) file_data_unref(vd->dir_fd);
146         if (vd->info) g_free(vd->info);
147
148         g_free(vd);
149 }
150
151 ViewDir *vd_new(LayoutWindow *lw)
152 {
153         auto vd = g_new0(ViewDir, 1);
154
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);
159
160         vd->layout = lw;
161         vd->pf = folder_icons_new();
162
163         switch (lw->options.dir_view_type)
164                 {
165                 case DIRVIEW_LIST: vd = vdlist_new(vd, lw->dir_fd); break;
166                 case DIRVIEW_TREE: vd = vdtree_new(vd, lw->dir_fd); break;
167                 }
168
169         gtk_container_add(GTK_CONTAINER(vd->widget), vd->view);
170
171         vd_dnd_init(vd);
172
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);
183
184         file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
185
186         /* vd_set_fd expects that vd_notify_cb is already registered */
187         if (lw->dir_fd) vd_set_fd(vd, lw->dir_fd);
188
189         gtk_widget_show(vd->view);
190
191         return vd;
192 }
193
194 void vd_set_select_func(ViewDir *vd,
195                         void (*func)(ViewDir *vd, FileData *fd, gpointer data), gpointer data)
196 {
197         vd->select_func = func;
198         vd->select_data = data;
199 }
200
201 #pragma GCC diagnostic push
202 #pragma GCC diagnostic ignored "-Wunused-function"
203 void vd_set_layout_unused(ViewDir *vd, LayoutWindow *layout)
204 {
205         vd->layout = layout;
206 }
207 #pragma GCC diagnostic pop
208
209 gboolean vd_set_fd(ViewDir *vd, FileData *dir_fd)
210 {
211         gboolean ret = FALSE;
212
213         file_data_unregister_notify_func(vd_notify_cb, vd);
214
215         switch (vd->type)
216         {
217         case DIRVIEW_LIST: ret = vdlist_set_fd(vd, dir_fd); break;
218         case DIRVIEW_TREE: ret = vdtree_set_fd(vd, dir_fd); break;
219         }
220
221         file_data_register_notify_func(vd_notify_cb, vd, NOTIFY_PRIORITY_HIGH);
222
223         return ret;
224 }
225
226 void vd_refresh(ViewDir *vd)
227 {
228         switch (vd->type)
229         {
230         case DIRVIEW_LIST: vdlist_refresh(vd); break;
231         case DIRVIEW_TREE: vdtree_refresh(vd); break;
232         }
233 }
234
235 #pragma GCC diagnostic push
236 #pragma GCC diagnostic ignored "-Wunused-function"
237 const gchar *vd_row_get_path_unused(ViewDir *vd, gint row)
238 {
239         const gchar *ret = nullptr;
240
241         switch (vd->type)
242         {
243         case DIRVIEW_LIST: ret = vdlist_row_get_path(vd, row); break;
244         case DIRVIEW_TREE: ret = vdtree_row_get_path(vd, row); break;
245         }
246
247         return ret;
248 }
249 #pragma GCC diagnostic pop
250
251 /* the calling stack is this:
252    vd_select_row -> select_func -> layout_set_fd -> vd_set_fd
253 */
254 void vd_select_row(ViewDir *vd, FileData *fd)
255 {
256         if (fd && vd->select_func)
257                 {
258                 vd->select_func(vd, fd, vd->select_data);
259                 }
260 }
261
262 gboolean vd_find_row(ViewDir *vd, FileData *fd, GtkTreeIter *iter)
263 {
264         gboolean ret = FALSE;
265
266         switch (vd->type)
267         {
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;
270         }
271
272         return ret;
273 }
274
275 FileData *vd_get_fd_from_tree_path(ViewDir *vd, GtkTreeView *tview, GtkTreePath *tpath)
276 {
277         GtkTreeIter iter;
278         FileData *fd = nullptr;
279         GtkTreeModel *store;
280
281         store = gtk_tree_view_get_model(tview);
282         gtk_tree_model_get_iter(store, &iter, tpath);
283         switch (vd->type)
284                 {
285                 case DIRVIEW_LIST:
286                         gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
287                         break;
288                 case DIRVIEW_TREE:
289                         {
290                         NodeData *nd;
291                         gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
292                         fd = (nd) ? nd->fd : nullptr;
293                         };
294                         break;
295                 }
296
297         return fd;
298 }
299
300 static void vd_rename_finished_cb(gboolean success, const gchar *new_path, gpointer data)
301 {
302         auto vd = static_cast<ViewDir *>(data);
303         if (success)
304                 {
305                 FileData *fd = file_data_new_dir(new_path);
306                 GtkTreeIter iter;
307
308                 if (vd_find_row(vd, fd, &iter))
309                         {
310                         tree_view_row_make_visible(GTK_TREE_VIEW(vd->view), &iter, TRUE);
311                         }
312
313                 file_data_unref(fd);
314                 }
315 }
316
317 static gboolean vd_rename_cb(TreeEditData *td, const gchar *, const gchar *new_name, gpointer data)
318 {
319         auto vd = static_cast<ViewDir *>(data);
320         FileData *fd;
321         gchar *new_path;
322         gchar *base;
323
324         fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), td->path);
325         if (!fd) return FALSE;
326
327         base = remove_level_from_path(fd->path);
328         new_path = g_build_filename(base, new_name, NULL);
329         g_free(base);
330
331         file_util_rename_dir(fd, new_path, vd->view, vd_rename_finished_cb, vd);
332
333         g_free(new_path);
334
335         return FALSE;
336 }
337
338 static void vd_rename_by_data(ViewDir *vd, FileData *fd)
339 {
340         GtkTreeModel *store;
341         GtkTreePath *tpath;
342         GtkTreeIter iter;
343
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);
347
348         tree_edit_by_path(GTK_TREE_VIEW(vd->view), tpath, 0, fd->name,
349                           vd_rename_cb, vd);
350         gtk_tree_path_free(tpath);
351 }
352
353
354 void vd_color_set(ViewDir *vd, FileData *fd, gint color_set)
355 {
356         GtkTreeModel *store;
357         GtkTreeIter iter;
358
359         if (!vd_find_row(vd, fd, &iter)) return;
360         store = gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view));
361
362         switch (vd->type)
363         {
364         case DIRVIEW_LIST:
365                 gtk_list_store_set(GTK_LIST_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
366                 break;
367         case DIRVIEW_TREE:
368                 gtk_tree_store_set(GTK_TREE_STORE(store), &iter, DIR_COLUMN_COLOR, color_set, -1);
369                 break;
370         }
371 }
372
373 void vd_popup_destroy_cb(GtkWidget *, gpointer data)
374 {
375         auto vd = static_cast<ViewDir *>(data);
376
377         vd_color_set(vd, vd->click_fd, FALSE);
378         vd->click_fd = nullptr;
379         vd->popup = nullptr;
380
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;
385 }
386
387 /*
388  *-----------------------------------------------------------------------------
389  * drop menu (from dnd)
390  *-----------------------------------------------------------------------------
391  */
392
393 static void vd_drop_menu_copy_cb(GtkWidget *, gpointer data)
394 {
395         auto vd = static_cast<ViewDir *>(data);
396         const gchar *path;
397         GList *list;
398
399         if (!vd->drop_fd) return;
400
401         path = vd->drop_fd->path;
402         list = vd->drop_list;
403         vd->drop_list = nullptr;
404
405         file_util_copy_simple(list, path, vd->widget);
406 }
407
408 static void vd_drop_menu_move_cb(GtkWidget *, gpointer data)
409 {
410         auto vd = static_cast<ViewDir *>(data);
411         const gchar *path;
412         GList *list;
413
414         if (!vd->drop_fd) return;
415
416         path = vd->drop_fd->path;
417         list = vd->drop_list;
418
419         vd->drop_list = nullptr;
420
421         file_util_move_simple(list, path, vd->widget);
422 }
423
424 static void vd_drop_menu_filter_cb(GtkWidget *widget, gpointer data)
425 {
426         auto vd = static_cast<ViewDir *>(data);
427         const gchar *path;
428         GList *list;
429         const gchar *key;
430
431         if (!vd->drop_fd) return;
432
433         key = static_cast<const gchar *>(g_object_get_data(G_OBJECT(widget), "filter_key"));
434
435         path = vd->drop_fd->path;
436         list = vd->drop_list;
437
438         vd->drop_list = nullptr;
439
440         file_util_start_filter_from_filelist(key, list, path, vd->widget);
441 }
442
443 static void vd_drop_menu_edit_item_free(gpointer data)
444 {
445         g_free(data);
446 }
447
448 GtkWidget *vd_drop_menu(ViewDir *vd, gint active)
449 {
450         GtkWidget *menu;
451         GList *editors_list = editor_list_get();
452         GList *work = editors_list;
453
454         menu = popup_menu_short_lived();
455         g_signal_connect(G_OBJECT(menu), "destroy",
456                          G_CALLBACK(vd_popup_destroy_cb), vd);
457
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);
461
462         while (work)
463                 {
464                 GtkWidget *item;
465                 auto editor = static_cast<const EditorDescription *>(work->data);
466                 gchar *key;
467                 work = work->next;
468
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);
473                 }
474
475         g_list_free(editors_list);
476
477         menu_item_add_divider(menu);
478         menu_item_add_icon(menu, _("Cancel"), GQ_ICON_CANCEL, nullptr, vd);
479
480         return menu;
481 }
482
483 /*
484  *-----------------------------------------------------------------------------
485  * pop-up menu
486  *-----------------------------------------------------------------------------
487  */
488
489 static void vd_pop_menu_up_cb(GtkWidget *, gpointer data)
490 {
491         auto vd = static_cast<ViewDir *>(data);
492         gchar *path;
493
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);
496
497         if (vd->select_func)
498                 {
499                 FileData *fd = file_data_new_dir(path);
500                 vd->select_func(vd, fd, vd->select_data);
501                 file_data_unref(fd);
502                 }
503
504         g_free(path);
505 }
506
507 static void vd_pop_menu_slide_cb(GtkWidget *, gpointer data)
508 {
509         auto vd = static_cast<ViewDir *>(data);
510
511         if (!vd->layout) return;
512         if (!vd->click_fd) return;
513
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);
518 }
519
520 static void vd_pop_menu_slide_rec_cb(GtkWidget *, gpointer data)
521 {
522         auto vd = static_cast<ViewDir *>(data);
523         GList *list;
524
525         if (!vd->layout) return;
526         if (!vd->click_fd) return;
527
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);
529
530         layout_image_slideshow_stop(vd->layout);
531         layout_image_slideshow_start_from_list(vd->layout, list);
532 }
533
534 static void vd_pop_menu_dupe(ViewDir *vd, gint recursive)
535 {
536         DupeWindow *dw;
537         GList *list = nullptr;
538
539         if (!vd->click_fd) return;
540
541         if (recursive)
542                 {
543                 list = g_list_append(list, file_data_ref(vd->click_fd));
544                 }
545         else
546                 {
547                 filelist_read(vd->click_fd, &list, nullptr);
548                 list = filelist_filter(list, FALSE);
549                 }
550
551         dw = dupe_window_new();
552         dupe_window_add_files(dw, list, recursive);
553
554         filelist_free(list);
555 }
556
557 static void vd_pop_menu_dupe_cb(GtkWidget *, gpointer data)
558 {
559         auto vd = static_cast<ViewDir *>(data);
560         vd_pop_menu_dupe(vd, FALSE);
561 }
562
563 static void vd_pop_menu_dupe_rec_cb(GtkWidget *, gpointer data)
564 {
565         auto vd = static_cast<ViewDir *>(data);
566         vd_pop_menu_dupe(vd, TRUE);
567 }
568
569 static void vd_pop_menu_delete_cb(GtkWidget *, gpointer data)
570 {
571         auto vd = static_cast<ViewDir *>(data);
572
573         if (!vd->click_fd) return;
574         file_util_delete_dir(vd->click_fd, vd->widget);
575 }
576
577 static void vd_pop_menu_copy_path_cb(GtkWidget *, gpointer data)
578 {
579         auto vd = static_cast<ViewDir *>(data);
580
581         if (!vd->click_fd) return;
582
583         file_util_copy_path_to_clipboard(vd->click_fd, TRUE);
584 }
585
586 static void vd_pop_menu_copy_path_unquoted_cb(GtkWidget *, gpointer data)
587 {
588         auto vd = static_cast<ViewDir *>(data);
589
590         if (!vd->click_fd) return;
591
592         file_util_copy_path_to_clipboard(vd->click_fd, FALSE);
593 }
594
595 static void vd_pop_submenu_dir_view_as_cb(GtkWidget *widget, gpointer data)
596 {
597         auto vd = static_cast<ViewDir *>(data);
598
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);
601 }
602
603 static void vd_pop_menu_refresh_cb(GtkWidget *, gpointer data)
604 {
605         auto vd = static_cast<ViewDir *>(data);
606
607         if (vd->layout) layout_refresh(vd->layout);
608 }
609
610 static void vd_toggle_show_hidden_files_cb(GtkWidget *, gpointer data)
611 {
612         auto vd = static_cast<ViewDir *>(data);
613
614         options->file_filter.show_hidden_files = !options->file_filter.show_hidden_files;
615         if (vd->layout) layout_refresh(vd->layout);
616 }
617
618 static void vd_pop_menu_new_folder_cb(gboolean success, const gchar *new_path, gpointer data)
619 {
620         auto vd = static_cast<ViewDir *>(data);
621         FileData *fd = nullptr;
622         GtkTreeIter iter;
623         GtkTreePath *tpath;
624         GtkTreeModel *store;
625
626         if (!success) return;
627
628         switch (vd->type)
629                 {
630                 case DIRVIEW_LIST:
631                         {
632                         vd_refresh(vd);
633                         fd = vdlist_row_by_path(vd, new_path, nullptr);
634                         };
635                         break;
636                 case DIRVIEW_TREE:
637                         {
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);
641                         }
642                         break;
643                 }
644
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);
649
650         gtk_tree_path_free(tpath);
651 }
652
653 static void vd_pop_menu_new_cb(GtkWidget *, gpointer data)
654 {
655         auto vd = static_cast<ViewDir *>(data);
656         FileData *dir_fd = nullptr;
657
658         switch (vd->type)
659                 {
660                 case DIRVIEW_LIST:
661                         {
662                         if (!vd->dir_fd) return;
663                         dir_fd = vd->dir_fd;
664                         };
665                         break;
666                 case DIRVIEW_TREE:
667                         {
668                         if (!vd->click_fd) return;
669                         dir_fd = vd->click_fd;
670                         };
671                         break;
672                 }
673
674         file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
675 }
676
677 static void vd_pop_menu_rename_cb(GtkWidget *, gpointer data)
678 {
679         auto vd = static_cast<ViewDir *>(data);
680
681         vd_rename_by_data(vd, vd->click_fd);
682 }
683
684 static void vd_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
685 {
686         auto vd = static_cast<ViewDir *>(data);
687         gboolean ascend;
688
689         if (!vd) return;
690
691         if (!vd->layout) return;
692
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);
695
696         if (vd->layout) layout_refresh(vd->layout);
697 }
698
699 static void vd_pop_menu_sort_case_cb(GtkWidget *widget, gpointer data)
700 {
701         auto vd = static_cast<ViewDir *>(data);
702         gboolean case_sensitive;
703
704         if (!vd) return;
705
706         if (!vd->layout) return;
707
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);
710
711         if (vd->layout) layout_refresh(vd->layout);
712 }
713
714 static void vd_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
715 {
716         ViewDir *vd;
717         SortType type;
718
719         vd = static_cast<ViewDir *>(submenu_item_get_data(widget));
720
721         if (!vd) return;
722         if (!vd->layout) return;
723
724         type = static_cast<SortType>GPOINTER_TO_INT(data);
725
726         if (type == SORT_NAME || type == SORT_NUMBER || type == SORT_TIME)
727                 {
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);
729
730                 if (vd->layout) layout_refresh(vd->layout);
731                 }
732 }
733
734 GtkWidget *vd_pop_menu(ViewDir *vd, FileData *fd)
735 {
736         GtkWidget *menu;
737         gboolean active;
738         gboolean rename_delete_active = FALSE;
739         gboolean new_folder_active = FALSE;
740         GtkWidget *submenu;
741         GtkWidget *item;
742
743         active = (fd != nullptr);
744         switch (vd->type)
745                 {
746                 case DIRVIEW_LIST:
747                         {
748                         /* check using . (always row 0) */
749                         new_folder_active = (vd->dir_fd && access_file(vd->dir_fd->path , W_OK | X_OK));
750
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));
756                         };
757                         break;
758                 case DIRVIEW_TREE:
759                         {
760                         if (fd)
761                                 {
762                                 gchar *parent;
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);
766                                 g_free(parent);
767                                 };
768                         }
769                         break;
770                 }
771
772         menu = popup_menu_short_lived();
773         g_signal_connect(G_OBJECT(menu), "destroy",
774                          G_CALLBACK(vd_popup_destroy_cb), vd);
775
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);
779
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);
785
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);
791
792         menu_item_add_divider(menu);
793
794         menu_item_add_sensitive(menu, _("_New folder..."), new_folder_active,
795                                 G_CALLBACK(vd_pop_menu_new_cb), vd);
796
797         menu_item_add_sensitive(menu, _("_Rename..."), rename_delete_active,
798                                 G_CALLBACK(vd_pop_menu_rename_cb), vd);
799
800         menu_item_add(menu, _("_Copy path"),
801                       G_CALLBACK(vd_pop_menu_copy_path_cb), vd);
802
803         menu_item_add(menu, _("_Copy path unquoted"),
804                       G_CALLBACK(vd_pop_menu_copy_path_unquoted_cb), vd);
805
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);
809
810
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);
813
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);
816
817         if (vd->type == DIRVIEW_LIST)
818                 {
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);
824                 }
825
826         if (vd->type == DIRVIEW_TREE)
827                 {
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);
831                 }
832
833         menu_item_add_divider(menu);
834
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);
837
838         menu_item_add_icon(menu, _("Re_fresh"), GQ_ICON_REFRESH,
839                             G_CALLBACK(vd_pop_menu_refresh_cb), vd);
840
841         return menu;
842 }
843
844 void vd_new_folder(ViewDir *vd, FileData *dir_fd)
845 {
846         file_util_create_dir(dir_fd->path, vd->layout->window, vd_pop_menu_new_folder_cb, vd);
847 }
848
849 /*
850  *-----------------------------------------------------------------------------
851  * dnd
852  *-----------------------------------------------------------------------------
853  */
854
855 static GtkTargetEntry vd_dnd_drop_types[] = {
856         { const_cast<gchar *>("text/uri-list"), 0, TARGET_URI_LIST }
857 };
858 static gint vd_dnd_drop_types_count = 1;
859
860 static void vd_dest_set(ViewDir *vd, gint enable)
861 {
862         if (enable)
863                 {
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));
868                 }
869         else
870                 {
871                 gtk_drag_dest_unset(vd->view);
872                 }
873 }
874
875 static void vd_dnd_get(GtkWidget *, GdkDragContext *,
876                            GtkSelectionData *selection_data, guint info,
877                            guint, gpointer data)
878 {
879         auto vd = static_cast<ViewDir *>(data);
880         GList *list;
881
882         if (!vd->click_fd) return;
883
884         switch (info)
885                 {
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);
890                         g_list_free(list);
891                         break;
892                 }
893 }
894
895 static void vd_dnd_begin(GtkWidget *, GdkDragContext *, gpointer data)
896 {
897         auto vd = static_cast<ViewDir *>(data);
898
899         vd_color_set(vd, vd->click_fd, TRUE);
900         vd_dest_set(vd, FALSE);
901 }
902
903 static void vd_dnd_end(GtkWidget *, GdkDragContext *context, gpointer data)
904 {
905         auto vd = static_cast<ViewDir *>(data);
906
907         vd_color_set(vd, vd->click_fd, FALSE);
908
909         if (vd->type == DIRVIEW_LIST && gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
910                 {
911                 vd_refresh(vd);
912                 }
913         vd_dest_set(vd, TRUE);
914 }
915
916 static void vd_dnd_drop_receive(GtkWidget *widget, GdkDragContext *,
917                                 gint x, gint y,
918                                 GtkSelectionData *selection_data, guint info,
919                                 guint, gpointer data)
920 {
921         auto vd = static_cast<ViewDir *>(data);
922         GtkTreePath *tpath;
923         FileData *fd = nullptr;
924         GdkDragAction action = GDK_ACTION_ASK;
925
926         vd->click_fd = nullptr;
927
928         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), x, y,
929                                           &tpath, nullptr, nullptr, nullptr))
930                 {
931                 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
932                 gtk_tree_path_free(tpath);
933                 }
934
935         if (!fd) return;
936
937         if (info == TARGET_URI_LIST)
938                 {
939                 GList *list;
940                 gint active;
941                 gboolean done = FALSE;
942
943                 list = uri_filelist_from_gtk_selection_data(selection_data);
944                 if (!list) return;
945
946                 active = access_file(fd->path, W_OK | X_OK);
947
948                 vd_color_set(vd, fd, TRUE);
949
950                 if (active)
951                         {
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.
955  */
956                         GdkModifierType mask;
957
958                         gdk_window_get_pointer(gtk_widget_get_window(widget), nullptr, nullptr, &mask);
959                         if (mask & GDK_CONTROL_MASK)
960                                 {
961                                 action = GDK_ACTION_COPY;
962                                 }
963                         else if (mask & GDK_SHIFT_MASK)
964                                 {
965                                 action = GDK_ACTION_MOVE;
966                                 }
967
968                         if (action != GDK_ACTION_COPY && action != GDK_ACTION_MOVE)
969                                 {
970                                 if (options->dnd_default_action == DND_ACTION_COPY)
971                                         {
972                                         action = GDK_ACTION_COPY;
973                                         }
974                                 else if (options->dnd_default_action == DND_ACTION_MOVE)
975                                         {
976                                         action = GDK_ACTION_MOVE;
977                                         }
978                                 }
979
980                         if (action == GDK_ACTION_COPY)
981                                 {
982                                 file_util_copy_simple(list, fd->path, vd->widget);
983                                 done = TRUE;
984                                 list = nullptr;
985                                 }
986                         else if (action == GDK_ACTION_MOVE)
987                                 {
988                                 file_util_move_simple(list, fd->path, vd->widget);
989                                 done = TRUE;
990                                 list = nullptr;
991                                 }
992                         }
993
994                 if (done == FALSE)
995                         {
996                         vd->popup = vd_drop_menu(vd, active);
997                         gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
998                         }
999
1000                 vd->drop_fd = fd;
1001                 vd->drop_list = list;
1002                 }
1003 }
1004
1005 static void vd_dnd_drop_update(ViewDir *vd, gint x, gint y)
1006 {
1007         GtkTreePath *tpath;
1008         FileData *fd = nullptr;
1009
1010         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(vd->view), x, y,
1011                                           &tpath, nullptr, nullptr, nullptr))
1012                 {
1013                 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(vd->view), tpath);
1014                 gtk_tree_path_free(tpath);
1015                 }
1016
1017         if (fd != vd->drop_fd)
1018                 {
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);
1022                 }
1023
1024         vd->drop_fd = fd;
1025 }
1026
1027 void vd_dnd_drop_scroll_cancel(ViewDir *vd)
1028 {
1029         if (vd->drop_scroll_id)
1030                 {
1031                 g_source_remove(vd->drop_scroll_id);
1032                 vd->drop_scroll_id = 0;
1033                 }
1034 }
1035
1036 static gboolean vd_auto_scroll_idle_cb(gpointer data)
1037 {
1038         auto vd = static_cast<ViewDir *>(data);
1039         GdkSeat *seat;
1040         GdkDevice *device;
1041
1042         if (vd->drop_fd)
1043                 {
1044                 GdkWindow *window;
1045                 gint x, y;
1046                 gint w, h;
1047
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);
1052
1053                 w = gdk_window_get_width(window);
1054                 h = gdk_window_get_height(window);
1055                 if (x >= 0 && x < w && y >= 0 && y < h)
1056                         {
1057                         vd_dnd_drop_update(vd, x, y);
1058                         }
1059                 }
1060
1061         vd->drop_scroll_id = 0;
1062         return G_SOURCE_REMOVE;
1063 }
1064
1065 static gboolean vd_auto_scroll_notify_cb(GtkWidget *, gint, gint, gpointer data)
1066 {
1067         auto vd = static_cast<ViewDir *>(data);
1068
1069         if (!vd->drop_fd || vd->drop_list) return FALSE;
1070
1071         if (!vd->drop_scroll_id) vd->drop_scroll_id = g_idle_add(vd_auto_scroll_idle_cb, vd);
1072
1073         return TRUE;
1074 }
1075
1076 static gboolean vd_dnd_drop_motion(GtkWidget *, GdkDragContext *context, gint x, gint y, guint time, gpointer data)
1077 {
1078         auto vd = static_cast<ViewDir *>(data);
1079
1080         vd->click_fd = nullptr;
1081
1082         if (gtk_drag_get_source_widget(context) == vd->view)
1083                 {
1084                 /* from same window */
1085                 gdk_drag_status(context, GDK_ACTION_DEFAULT, time);
1086                 return TRUE;
1087                 }
1088
1089         gdk_drag_status(context, gdk_drag_context_get_suggested_action(context), time);
1090
1091         vd_dnd_drop_update(vd, x, y);
1092
1093         if (vd->drop_fd)
1094                 {
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);
1097                 }
1098
1099         return FALSE;
1100 }
1101
1102 static void vd_dnd_drop_leave(GtkWidget *, GdkDragContext *, guint, gpointer data)
1103 {
1104         auto vd = static_cast<ViewDir *>(data);
1105
1106         if (vd->drop_fd != vd->click_fd) vd_color_set(vd, vd->drop_fd, FALSE);
1107
1108         vd->drop_fd = nullptr;
1109
1110         if (vd->dnd_drop_leave_func) vd->dnd_drop_leave_func(vd);
1111 }
1112
1113 void vd_dnd_init(ViewDir *vd)
1114 {
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);
1124
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);
1132 }
1133
1134 /*
1135  *----------------------------------------------------------------------------
1136  * callbacks
1137  *----------------------------------------------------------------------------
1138  */
1139
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)
1143 {
1144         auto *vd = static_cast<ViewDir *>(data);
1145         GtkTreeModel *store;
1146         GtkTreeIter iter;
1147         GtkTreePath *tpath;
1148         gint cw, ch;
1149
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);
1155         *y += ch;
1156         popup_menu_position_clamp(menu, x, y, 0);
1157 }
1158 #pragma GCC diagnostic pop
1159
1160 void vd_activate_cb(GtkTreeView *tview, GtkTreePath *tpath, GtkTreeViewColumn *, gpointer data)
1161 {
1162         auto vd = static_cast<ViewDir *>(data);
1163         FileData *fd = vd_get_fd_from_tree_path(vd, tview, tpath);
1164
1165         vd_select_row(vd, fd);
1166 }
1167
1168 static GdkRGBA *vd_color_shifted(GtkWidget *widget)
1169 {
1170         static GdkRGBA color;
1171         static GdkRGBA color_style;
1172         static GtkWidget *done = nullptr;
1173
1174 #ifdef HAVE_GTK4
1175 /* @FIXME GTK4 no background color */
1176 #else
1177         if (done != widget)
1178                 {
1179                 GtkStyleContext *style_context;
1180
1181                 style_context = gtk_widget_get_style_context(widget);
1182                 gtk_style_context_get_background_color(style_context, GTK_STATE_FLAG_NORMAL, &color_style);
1183
1184                 memcpy(&color, &color_style, sizeof(color_style));
1185
1186                 shift_color(&color, -1, 0);
1187                 done = widget;
1188                 }
1189 #endif
1190
1191         return &color;
1192 }
1193
1194 void vd_color_cb(GtkTreeViewColumn *, GtkCellRenderer *cell, GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
1195 {
1196         auto vd = static_cast<ViewDir *>(data);
1197         gboolean set;
1198
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);
1203 }
1204
1205 gboolean vd_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1206 {
1207         auto vd = static_cast<ViewDir *>(data);
1208         GtkTreePath *tpath;
1209         FileData *fd = nullptr;
1210
1211         if (defined_mouse_buttons(widget, bevent, vd->layout))
1212                 {
1213                 return TRUE;
1214                 }
1215
1216         if (vd->type == DIRVIEW_LIST && !options->view_dir_list_single_click_enter)
1217                 return FALSE;
1218
1219         if (!vd->click_fd) return FALSE;
1220         vd_color_set(vd, vd->click_fd, FALSE);
1221
1222         if (bevent->button != MOUSE_BUTTON_LEFT) return TRUE;
1223
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))
1227                 {
1228                 fd = vd_get_fd_from_tree_path(vd, GTK_TREE_VIEW(widget), tpath);
1229                 gtk_tree_path_free(tpath);
1230                 }
1231
1232         if (fd && vd->click_fd == fd)
1233                 {
1234                 vd_select_row(vd, vd->click_fd);
1235                 }
1236
1237         return FALSE;
1238 }
1239
1240 gboolean vd_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1241 {
1242         auto vd = static_cast<ViewDir *>(data);
1243         gboolean ret = FALSE;
1244
1245         switch (vd->type)
1246         {
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;
1249         }
1250
1251         return ret;
1252 }
1253
1254 gboolean vd_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1255 {
1256         auto vd = static_cast<ViewDir *>(data);
1257         gboolean ret = FALSE;
1258         FileData *fd;
1259         GtkTreePath *tpath;
1260         GtkTreeIter iter;
1261         NodeData *nd = nullptr;
1262         GtkTreeModel *store;
1263
1264         if (bevent->button == MOUSE_BUTTON_RIGHT)
1265                 {
1266                 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), bevent->x, bevent->y, &tpath, nullptr, nullptr, nullptr))
1267                         {
1268                         store = gtk_tree_view_get_model(GTK_TREE_VIEW(widget));
1269                         gtk_tree_model_get_iter(store, &iter, tpath);
1270
1271                         switch (vd->type)
1272                                 {
1273                                 case DIRVIEW_LIST:
1274                                         gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &fd, -1);
1275                                         vd->click_fd = fd;
1276                                         break;
1277                                 case DIRVIEW_TREE:
1278                                         gtk_tree_model_get(store, &iter, DIR_COLUMN_POINTER, &nd, -1);
1279                                         vd->click_fd = (nd) ? nd->fd : nullptr;
1280                                 }
1281
1282                         if (vd->click_fd)
1283                                 {
1284                                 vd_color_set(vd, vd->click_fd, TRUE);
1285                                 }
1286                         }
1287
1288                 vd->popup = vd_pop_menu(vd, vd->click_fd);
1289                 gtk_menu_popup_at_pointer(GTK_MENU(vd->popup), nullptr);
1290
1291                 return TRUE;
1292                 }
1293
1294         switch (vd->type)
1295         {
1296         case DIRVIEW_LIST: ret = vdlist_press_cb(widget, bevent, data); break;
1297         case DIRVIEW_TREE: ret = vdtree_press_cb(widget, bevent, data); break;
1298         }
1299
1300         return ret;
1301 }
1302
1303 static void vd_notify_cb(FileData *fd, NotifyType type, gpointer data)
1304 {
1305         auto vd = static_cast<ViewDir *>(data);
1306         gboolean refresh;
1307         gchar *base;
1308
1309         if (!S_ISDIR(fd->mode)) return; /* this gives correct results even on recently deleted files/directories */
1310
1311         DEBUG_1("Notify vd: %s %04x", fd->path, type);
1312
1313         base = remove_level_from_path(fd->path);
1314
1315         if (vd->type == DIRVIEW_LIST)
1316                 {
1317                 refresh = (fd == vd->dir_fd);
1318
1319                 if (!refresh)
1320                         {
1321                         refresh = (strcmp(base, vd->dir_fd->path) == 0);
1322                         }
1323
1324                 if ((type & NOTIFY_CHANGE) && fd->change)
1325                         {
1326                         if (!refresh && fd->change->dest)
1327                                 {
1328                                 gchar *dest_base = remove_level_from_path(fd->change->dest);
1329                                 refresh = (strcmp(dest_base, vd->dir_fd->path) == 0);
1330                                 g_free(dest_base);
1331                                 }
1332
1333                         if (!refresh && fd->change->source)
1334                                 {
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);
1338                                 }
1339                         }
1340
1341                 if (refresh) vd_refresh(vd);
1342                 }
1343
1344         if (vd->type == DIRVIEW_TREE)
1345                 {
1346                 GtkTreeIter iter;
1347                 FileData *base_fd = file_data_new_dir(base);
1348
1349                 if (vd_find_row(vd, base_fd, &iter))
1350                         {
1351                         vdtree_populate_path_by_iter(vd, &iter, TRUE, vd->dir_fd);
1352                         }
1353
1354                 file_data_unref(base_fd);
1355                 }
1356
1357         g_free(base);
1358 }
1359 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */