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