Use FileCluster from view_file_icon to enable clustering of images.
[geeqie.git] / src / view_file / view_file.c
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_file.h"
23
24 #include "dupe.h"
25 #include "collect.h"
26 #include "collect-table.h"
27 #include "editors.h"
28 #include "filecluster.h"
29 #include "layout.h"
30 #include "menu.h"
31 #include "thumb.h"
32 #include "ui_menu.h"
33 #include "ui_fileops.h"
34 #include "utilops.h"
35 #include "view_file/view_file_list.h"
36 #include "view_file/view_file_icon.h"
37
38 /*
39  *-----------------------------------------------------------------------------
40  * signals
41  *-----------------------------------------------------------------------------
42  */
43
44 void vf_send_update(ViewFile *vf)
45 {
46         if (vf->func_status) vf->func_status(vf, vf->data_status);
47 }
48
49 /*
50  *-----------------------------------------------------------------------------
51  * misc
52  *-----------------------------------------------------------------------------
53  */
54
55 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend)
56 {
57         switch (vf->type)
58         {
59         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend); break;
60         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend); break;
61         }
62 }
63
64 /*
65  *-----------------------------------------------------------------------------
66  * row stuff
67  *-----------------------------------------------------------------------------
68  */
69
70 FileData *vf_index_get_data(ViewFile *vf, gint row)
71 {
72         return g_list_nth_data(vf->list, row);
73 }
74
75 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
76 {
77         switch (vf->type)
78         {
79         case FILEVIEW_LIST: return vflist_index_by_fd(vf, fd);
80         case FILEVIEW_ICON: return vficon_index_by_fd(vf, fd);
81         }
82 }
83
84 guint vf_count(ViewFile *vf, gint64 *bytes)
85 {
86         if (bytes)
87                 {
88                 gint64 b = 0;
89                 GList *work;
90
91                 work = vf->list;
92                 while (work)
93                         {
94                         FileData *fd = work->data;
95                         work = work->next;
96
97                         b += fd->size;
98                         }
99
100                 *bytes = b;
101                 }
102
103         return g_list_length(vf->list);
104 }
105
106 GList *vf_get_list(ViewFile *vf)
107 {
108         GList *list = NULL;
109         GList *work;
110         for (work = vf->list; work; work = work->next)
111                 {
112                 FileData *fd = work->data;
113                 list = g_list_prepend(list, file_data_ref(fd));
114                 }
115
116         return g_list_reverse(list);
117 }
118
119 /*
120  *-------------------------------------------------------------------
121  * keyboard
122  *-------------------------------------------------------------------
123  */
124
125 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
126 {
127         ViewFile *vf = data;
128
129         switch (vf->type)
130         {
131         case FILEVIEW_LIST: return vflist_press_key_cb(widget, event, data);
132         case FILEVIEW_ICON: return vficon_press_key_cb(widget, event, data);
133         }
134 }
135
136 /*
137  *-------------------------------------------------------------------
138  * mouse
139  *-------------------------------------------------------------------
140  */
141
142 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
143 {
144         ViewFile *vf = data;
145
146         switch (vf->type)
147         {
148         case FILEVIEW_LIST: return vflist_press_cb(widget, bevent, data);
149         case FILEVIEW_ICON: return vficon_press_cb(widget, bevent, data);
150         }
151 }
152
153 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
154 {
155         ViewFile *vf = data;
156
157         switch (vf->type)
158         {
159         case FILEVIEW_LIST: return vflist_release_cb(widget, bevent, data);
160         case FILEVIEW_ICON: return vficon_release_cb(widget, bevent, data);
161         }
162 }
163
164
165 /*
166  *-----------------------------------------------------------------------------
167  * selections
168  *-----------------------------------------------------------------------------
169  */
170
171 gboolean vf_index_is_selected(ViewFile *vf, gint row)
172 {
173         switch (vf->type)
174         {
175         case FILEVIEW_LIST: return vflist_index_is_selected(vf, row);
176         case FILEVIEW_ICON: return vficon_index_is_selected(vf, row);
177         }
178 }
179
180
181 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
182 {
183         switch (vf->type)
184         {
185         case FILEVIEW_LIST: return vflist_selection_count(vf, bytes);
186         case FILEVIEW_ICON: return vficon_selection_count(vf, bytes);
187         }
188 }
189
190 GList *vf_selection_get_list(ViewFile *vf)
191 {
192         switch (vf->type)
193         {
194         case FILEVIEW_LIST: return vflist_selection_get_list(vf);
195         case FILEVIEW_ICON: return vficon_selection_get_list(vf);
196         }
197 }
198
199 GList *vf_selection_get_list_by_index(ViewFile *vf)
200 {
201         switch (vf->type)
202         {
203         case FILEVIEW_LIST: return vflist_selection_get_list_by_index(vf);
204         case FILEVIEW_ICON: return vficon_selection_get_list_by_index(vf);
205         }
206 }
207
208 void vf_select_all(ViewFile *vf)
209 {
210         switch (vf->type)
211         {
212         case FILEVIEW_LIST: vflist_select_all(vf); break;
213         case FILEVIEW_ICON: vficon_select_all(vf); break;
214         }
215 }
216
217 void vf_select_none(ViewFile *vf)
218 {
219         switch (vf->type)
220         {
221         case FILEVIEW_LIST: vflist_select_none(vf); break;
222         case FILEVIEW_ICON: vficon_select_none(vf); break;
223         }
224 }
225
226 void vf_select_invert(ViewFile *vf)
227 {
228         switch (vf->type)
229         {
230         case FILEVIEW_LIST: vflist_select_invert(vf); break;
231         case FILEVIEW_ICON: vficon_select_invert(vf); break;
232         }
233 }
234
235 void vf_select_by_fd(ViewFile *vf, FileData *fd)
236 {
237         switch (vf->type)
238         {
239         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
240         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
241         }
242 }
243
244 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
245 {
246         switch (vf->type)
247         {
248         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
249         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
250         }
251 }
252
253 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
254 {
255         switch (vf->type)
256         {
257         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
258         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
259         }
260 }
261
262 /*
263  *-----------------------------------------------------------------------------
264  * dnd
265  *-----------------------------------------------------------------------------
266  */
267
268
269 static void vf_dnd_init(ViewFile *vf)
270 {
271         switch (vf->type)
272         {
273         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
274         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
275         }
276 }
277
278 /*
279  *-----------------------------------------------------------------------------
280  * pop-up menu
281  *-----------------------------------------------------------------------------
282  */
283
284 GList *vf_pop_menu_file_list(ViewFile *vf)
285 {
286         switch (vf->type)
287         {
288         case FILEVIEW_LIST: return vflist_pop_menu_file_list(vf);
289         case FILEVIEW_ICON: return vficon_pop_menu_file_list(vf);
290         }
291 }
292
293 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
294 {
295         switch (vf->type)
296         {
297         case FILEVIEW_LIST: return vflist_selection_get_one(vf, fd);
298         case FILEVIEW_ICON: return vficon_selection_get_one(vf, fd);
299         }
300 }
301
302 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
303 {
304         ViewFile *vf;
305         const gchar *key = data;
306
307         vf = submenu_item_get_data(widget);
308
309         if (!vf) return;
310
311         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
312 }
313
314 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
315 {
316         ViewFile *vf = data;
317
318         switch (vf->type)
319         {
320         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
321         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
322         }
323 }
324
325 static void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
326 {
327         ViewFile *vf = data;
328
329         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
330 }
331
332 static void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
333 {
334         ViewFile *vf = data;
335
336         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
337 }
338
339 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
340 {
341         ViewFile *vf = data;
342
343         switch (vf->type)
344         {
345         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
346         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
347         }
348 }
349
350 static void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
351 {
352         ViewFile *vf = data;
353
354         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
355 }
356
357 static void vf_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
358 {
359         ViewFile *vf = data;
360
361         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf));
362 }
363
364 static void vf_pop_menu_enable_grouping_cb(GtkWidget *widget, gpointer data)
365 {
366         ViewFile *vf = data;
367
368         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
369 }
370
371 static void vf_pop_menu_duplicates_cb(GtkWidget *widget, gpointer data)
372 {
373         ViewFile *vf = data;
374         DupeWindow *dw;
375
376         dw = dupe_window_new();
377         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
378 }
379
380 static void vf_pop_menu_add_collection_cb(GtkWidget *widget, gpointer data)
381 {
382         ViewFile *vf = data;
383         CollectWindow *w;
384
385         w = collection_window_new(NULL);
386         collection_table_add_filelist(w->table, vf_pop_menu_file_list(vf));
387 }
388
389 static void vf_pop_menu_disable_grouping_cb(GtkWidget *widget, gpointer data)
390 {
391         ViewFile *vf = data;
392
393         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
394 }
395
396 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
397 {
398         ViewFile *vf;
399         SortType type;
400
401         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
402
403         vf = submenu_item_get_data(widget);
404         if (!vf) return;
405
406         type = (SortType)GPOINTER_TO_INT(data);
407
408         if (vf->layout)
409                 {
410                 layout_sort_set(vf->layout, type, vf->sort_ascend);
411                 }
412         else
413                 {
414                 vf_sort_set(vf, type, vf->sort_ascend);
415                 }
416 }
417
418 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
419 {
420         ViewFile *vf = data;
421
422         if (vf->layout)
423                 {
424                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
425                 }
426         else
427                 {
428                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
429                 }
430 }
431
432 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
433 {
434         ViewFile *vf = data;
435         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
436 }
437
438 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
439 {
440         ViewFile *vf = data;
441         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
442 }
443
444 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
445 {
446         ViewFile *vf = data;
447         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
448 }
449
450 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
451 {
452         ViewFile *vf = data;
453         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
454 }
455
456 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
457 {
458         ViewFile *vf = data;
459         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
460 }
461
462 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
463 {
464         ViewFile *vf = data;
465         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
466 }
467
468 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
469 {
470         ViewFile *vf = data;
471         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
472 }
473
474 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
475 {
476         ViewFile *vf = data;
477         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
478         if (!vf->layout) return;
479
480         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
481 }
482
483 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
484 {
485         ViewFile *vf = data;
486
487         switch (vf->type)
488         {
489         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
490         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
491         }
492 }
493
494 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
495 {
496         ViewFile *vf = data;
497
498         switch (vf->type)
499         {
500         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
501         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
502         }
503
504         filelist_free(vf->editmenu_fd_list);
505         vf->editmenu_fd_list = NULL;
506 }
507
508 GtkWidget *vf_pop_menu(ViewFile *vf)
509 {
510         GtkWidget *menu;
511         GtkWidget *item;
512         GtkWidget *submenu;
513         gboolean active = FALSE;
514
515         switch (vf->type)
516         {
517         case FILEVIEW_LIST:
518                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
519                 active = (VFLIST(vf)->click_fd != NULL);
520                 break;
521         case FILEVIEW_ICON:
522                 active = (VFICON(vf)->click_fd != NULL);
523                 break;
524         }
525
526         menu = popup_menu_short_lived();
527
528         g_signal_connect(G_OBJECT(menu), "destroy",
529                          G_CALLBACK(vf_popup_destroy_cb), vf);
530
531         if (vf->clicked_mark > 0)
532                 {
533                 gint mark = vf->clicked_mark;
534                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
535                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
536                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
537                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
538                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
539                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
540                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
541
542                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
543
544                 vf->active_mark = mark;
545                 vf->clicked_mark = 0;
546
547                 menu_item_add_sensitive(menu, str_set_mark, active,
548                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
549
550                 menu_item_add_sensitive(menu, str_res_mark, active,
551                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
552
553                 menu_item_add_sensitive(menu, str_toggle_mark, active,
554                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
555
556                 menu_item_add_divider(menu);
557
558                 menu_item_add_sensitive(menu, str_sel_mark, active,
559                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
560                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
561                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
562                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
563                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
564                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
565                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
566
567                 menu_item_add_divider(menu);
568
569                 g_free(str_set_mark);
570                 g_free(str_res_mark);
571                 g_free(str_toggle_mark);
572                 g_free(str_sel_mark);
573                 g_free(str_sel_mark_and);
574                 g_free(str_sel_mark_or);
575                 g_free(str_sel_mark_minus);
576                 }
577
578         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
579         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
580         gtk_widget_set_sensitive(item, active);
581
582         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
583                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
584
585         menu_item_add_divider(menu);
586         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
587                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
588         menu_item_add_sensitive(menu, _("_Move..."), active,
589                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
590         menu_item_add_sensitive(menu, _("_Rename..."), active,
591                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
592         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
593                                       G_CALLBACK(vf_pop_menu_delete_cb), vf);
594         menu_item_add_sensitive(menu, _("_Copy path"), active,
595                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
596
597         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
598                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
599         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
600                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
601
602         menu_item_add_divider(menu);
603         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
604                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
605         menu_item_add_divider(menu);
606         menu_item_add_stock_sensitive(menu, _("Add to new collection"), GTK_STOCK_INDEX, active,
607                                 G_CALLBACK(vf_pop_menu_add_collection_cb), vf);
608         menu_item_add_divider(menu);
609
610         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
611                                    FALSE, FALSE, TRUE, vf->sort_method);
612         menu_item_add_divider(submenu);
613         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
614                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
615
616         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
617         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
618
619         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
620                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
621
622         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
623                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
624
625         switch (vf->type)
626         {
627         case FILEVIEW_LIST:
628                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
629                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
630                 break;
631         case FILEVIEW_ICON:
632                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
633                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
634                 break;
635         }
636
637         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
638
639         return menu;
640 }
641
642 gboolean vf_refresh(ViewFile *vf)
643 {
644         switch (vf->type)
645         {
646         case FILEVIEW_LIST: return vflist_refresh(vf);
647         case FILEVIEW_ICON: return vficon_refresh(vf);
648         }
649 }
650
651 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
652 {
653         switch (vf->type)
654         {
655         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
656         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
657         }
658 }
659
660 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
661 {
662         ViewFile *vf = data;
663
664         switch (vf->type)
665         {
666         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
667         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
668         }
669
670         if (vf->popup)
671                 {
672                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
673                                                      0, 0, 0, NULL, vf);
674                 gtk_widget_destroy(vf->popup);
675                 }
676
677         fileclusterlist_free(vf->cluster_list);
678         file_data_unref(vf->dir_fd);
679         g_free(vf->info);
680         g_free(vf);
681 }
682
683 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
684 {
685         ViewFile *vf = data;
686         vf_refresh_idle(vf);
687 }
688
689
690 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
691 {
692         GtkWidget *frame = gtk_frame_new(NULL);
693         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
694
695         gint i;
696
697         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
698                 {
699                 GtkWidget *check = gtk_check_button_new();
700                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
701                 g_signal_connect(G_OBJECT(check), "toggled",
702                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
703
704                 gtk_widget_show(check);
705                 vf->filter_check[i] = check;
706                 }
707         gtk_container_add(GTK_CONTAINER(frame), hbox);
708         gtk_widget_show(hbox);
709         return frame;
710 }
711
712 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
713 {
714         gint n = mark - 1;
715         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
716                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
717 }
718
719 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
720 {
721         ViewFile *vf;
722
723         vf = g_new0(ViewFile, 1);
724
725         vf->type = type;
726         vf->sort_method = SORT_NAME;
727         vf->sort_ascend = TRUE;
728
729         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
730         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
731         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
732                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
733
734         vf->cluster_list = fileclusterlist_new();
735         vf->filter = vf_marks_filter_init(vf);
736
737         vf->widget = gtk_vbox_new(FALSE, 0);
738         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
739         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
740         gtk_widget_show(vf->scrolled);
741
742         g_signal_connect(G_OBJECT(vf->widget), "destroy",
743                          G_CALLBACK(vf_destroy_cb), vf);
744
745         switch (type)
746         {
747         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
748         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
749         }
750
751         vf_dnd_init(vf);
752
753         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
754                          G_CALLBACK(vf_press_key_cb), vf);
755         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
756                          G_CALLBACK(vf_press_cb), vf);
757         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
758                          G_CALLBACK(vf_release_cb), vf);
759
760         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
761         gtk_widget_show(vf->listview);
762
763         if (dir_fd) vf_set_fd(vf, dir_fd);
764
765         return vf;
766 }
767
768 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
769 {
770         vf->func_status = func;
771         vf->data_status = data;
772 }
773
774 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
775 {
776         vf->func_thumb_status = func;
777         vf->data_thumb_status = data;
778 }
779
780 void vf_thumb_set(ViewFile *vf, gboolean enable)
781 {
782         switch (vf->type)
783         {
784         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
785         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
786         }
787 }
788
789
790 static gboolean vf_thumb_next(ViewFile *vf);
791
792 static gdouble vf_thumb_progress(ViewFile *vf)
793 {
794         gint count = 0;
795         gint done = 0;
796
797         switch (vf->type)
798         {
799         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
800         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
801         }
802
803         DEBUG_1("thumb progress: %d of %d", done, count);
804         return (gdouble)done / count;
805 }
806
807 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
808 {
809         switch (vf->type)
810         {
811         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
812         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
813         }
814 }
815
816 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
817 {
818         if (vf->func_thumb_status)
819                 {
820                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
821                 }
822 }
823
824 static void vf_thumb_do(ViewFile *vf, FileData *fd)
825 {
826         if (!fd) return;
827
828         vf_set_thumb_fd(vf, fd);
829         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
830 }
831
832 void vf_thumb_cleanup(ViewFile *vf)
833 {
834         vf_thumb_status(vf, 0.0, NULL);
835
836         vf->thumbs_running = FALSE;
837
838         thumb_loader_free(vf->thumbs_loader);
839         vf->thumbs_loader = NULL;
840
841         vf->thumbs_filedata = NULL;
842 }
843
844 void vf_thumb_stop(ViewFile *vf)
845 {
846         if (vf->thumbs_running) vf_thumb_cleanup(vf);
847 }
848
849 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
850 {
851         ViewFile *vf = data;
852
853         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
854                 {
855                 vf_thumb_do(vf, vf->thumbs_filedata);
856                 }
857
858         while (vf_thumb_next(vf));
859 }
860
861 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
862 {
863         vf_thumb_common_cb(tl, data);
864 }
865
866 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
867 {
868         vf_thumb_common_cb(tl, data);
869 }
870
871 static gboolean vf_thumb_next(ViewFile *vf)
872 {
873         FileData *fd = NULL;
874
875         if (!gtk_widget_get_realized(vf->listview))
876                 {
877                 vf_thumb_status(vf, 0.0, NULL);
878                 return FALSE;
879                 }
880
881         switch (vf->type)
882         {
883         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
884         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
885         }
886
887         if (!fd)
888                 {
889                 /* done */
890                 vf_thumb_cleanup(vf);
891                 return FALSE;
892                 }
893
894         vf->thumbs_filedata = fd;
895
896         thumb_loader_free(vf->thumbs_loader);
897
898         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
899         thumb_loader_set_callbacks(vf->thumbs_loader,
900                                    vf_thumb_done_cb,
901                                    vf_thumb_error_cb,
902                                    NULL,
903                                    vf);
904
905         if (!thumb_loader_start(vf->thumbs_loader, fd))
906                 {
907                 /* set icon to unknown, continue */
908                 DEBUG_1("thumb loader start failed %s", fd->path);
909                 vf_thumb_do(vf, fd);
910
911                 return TRUE;
912                 }
913
914         return FALSE;
915 }
916
917 static void vf_thumb_reset_all(ViewFile *vf)
918 {
919         GList *work;
920
921         for (work = vf->list; work; work = work->next)
922                 {
923                 FileData *fd = work->data;
924                 if (fd->thumb_pixbuf)
925                         {
926                         g_object_unref(fd->thumb_pixbuf);
927                         fd->thumb_pixbuf = NULL;
928                         }
929                 }
930 }
931
932 void vf_thumb_update(ViewFile *vf)
933 {
934         vf_thumb_stop(vf);
935
936         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
937
938         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
939         vf->thumbs_running = TRUE;
940
941         if (thumb_format_changed)
942                 {
943                 vf_thumb_reset_all(vf);
944                 thumb_format_changed = FALSE;
945                 }
946
947         while (vf_thumb_next(vf));
948 }
949
950
951 void vf_marks_set(ViewFile *vf, gboolean enable)
952 {
953         if (vf->marks_enabled == enable) return;
954
955         vf->marks_enabled = enable;
956
957         switch (vf->type)
958         {
959         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
960         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
961         }
962         if (enable)
963                 gtk_widget_show(vf->filter);
964         else
965                 gtk_widget_hide(vf->filter);
966
967         vf_refresh_idle(vf);
968 }
969
970 guint vf_marks_get_filter(ViewFile *vf)
971 {
972         guint ret = 0;
973         gint i;
974         if (!vf->marks_enabled) return 0;
975
976         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
977                 {
978                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
979                         {
980                         ret |= 1 << i;
981                         }
982                 }
983         return ret;
984 }
985
986 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
987 {
988         vf->layout = layout;
989 }
990
991
992 /*
993  *-----------------------------------------------------------------------------
994  * maintenance (for rename, move, remove)
995  *-----------------------------------------------------------------------------
996  */
997
998 static gboolean vf_refresh_idle_cb(gpointer data)
999 {
1000         ViewFile *vf = data;
1001
1002         vf_refresh(vf);
1003         vf->refresh_idle_id = 0;
1004         return FALSE;
1005 }
1006
1007 void vf_refresh_idle_cancel(ViewFile *vf)
1008 {
1009         if (vf->refresh_idle_id)
1010                 {
1011                 g_source_remove(vf->refresh_idle_id);
1012                 vf->refresh_idle_id = 0;
1013                 }
1014 }
1015
1016
1017 void vf_refresh_idle(ViewFile *vf)
1018 {
1019         if (!vf->refresh_idle_id)
1020                 {
1021                 vf->time_refresh_set = time(NULL);
1022                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1023                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1024                 }
1025         else if (time(NULL) - vf->time_refresh_set > 1)
1026                 {
1027                 /* more than 1 sec since last update - increase priority */
1028                 vf_refresh_idle_cancel(vf);
1029                 vf->time_refresh_set = time(NULL);
1030                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1031                 }
1032 }
1033
1034 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1035 {
1036         ViewFile *vf = data;
1037         gboolean refresh;
1038
1039         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1040         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1041         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1042
1043         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1044
1045         refresh = (fd == vf->dir_fd);
1046
1047         if (!refresh)
1048                 {
1049                 gchar *base = remove_level_from_path(fd->path);
1050                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1051                 g_free(base);
1052                 }
1053
1054         if ((type & NOTIFY_CHANGE) && fd->change)
1055                 {
1056                 if (!refresh && fd->change->dest)
1057                         {
1058                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1059                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1060                         g_free(dest_base);
1061                         }
1062
1063                 if (!refresh && fd->change->source)
1064                         {
1065                         gchar *source_base = remove_level_from_path(fd->change->source);
1066                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1067                         g_free(source_base);
1068                         }
1069                 }
1070
1071         if (refresh)
1072                 {
1073                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1074                 vf_refresh_idle(vf);
1075                 }
1076 }
1077
1078 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */