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