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