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