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