deduplicate shared code in view_file_icon and view_file_list
[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_add_collection_cb(GtkWidget *widget, gpointer data)
380 {
381         ViewFile *vf = data;
382         CollectWindow *w;
383
384         w = collection_window_new(NULL);
385         collection_table_add_filelist(w->table, vf_pop_menu_file_list(vf));
386 }
387
388 static void vf_pop_menu_disable_grouping_cb(GtkWidget *widget, gpointer data)
389 {
390         ViewFile *vf = data;
391
392         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
393 }
394
395 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
396 {
397         ViewFile *vf;
398         SortType type;
399
400         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
401
402         vf = submenu_item_get_data(widget);
403         if (!vf) return;
404
405         type = (SortType)GPOINTER_TO_INT(data);
406
407         if (vf->layout)
408                 {
409                 layout_sort_set(vf->layout, type, vf->sort_ascend);
410                 }
411         else
412                 {
413                 vf_sort_set(vf, type, vf->sort_ascend);
414                 }
415 }
416
417 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
418 {
419         ViewFile *vf = data;
420
421         if (vf->layout)
422                 {
423                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
424                 }
425         else
426                 {
427                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
428                 }
429 }
430
431 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
432 {
433         ViewFile *vf = data;
434         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
435 }
436
437 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
438 {
439         ViewFile *vf = data;
440         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
441 }
442
443 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
444 {
445         ViewFile *vf = data;
446         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
447 }
448
449 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
450 {
451         ViewFile *vf = data;
452         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
453 }
454
455 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
456 {
457         ViewFile *vf = data;
458         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
459 }
460
461 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
462 {
463         ViewFile *vf = data;
464         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
465 }
466
467 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
468 {
469         ViewFile *vf = data;
470         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
471 }
472
473 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
474 {
475         ViewFile *vf = data;
476         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
477         if (!vf->layout) return;
478
479         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
480 }
481
482 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
483 {
484         ViewFile *vf = data;
485
486         switch (vf->type)
487         {
488         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
489         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
490         }
491 }
492
493 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
494 {
495         ViewFile *vf = data;
496
497         switch (vf->type)
498         {
499         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
500         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
501         }
502
503         filelist_free(vf->editmenu_fd_list);
504         vf->editmenu_fd_list = NULL;
505 }
506
507 GtkWidget *vf_pop_menu(ViewFile *vf)
508 {
509         GtkWidget *menu;
510         GtkWidget *item;
511         GtkWidget *submenu;
512         gboolean active = FALSE;
513
514         switch (vf->type)
515         {
516         case FILEVIEW_LIST:
517                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
518                 active = (VFLIST(vf)->click_fd != NULL);
519                 break;
520         case FILEVIEW_ICON:
521                 active = (VFICON(vf)->click_fd != NULL);
522                 break;
523         }
524
525         menu = popup_menu_short_lived();
526
527         g_signal_connect(G_OBJECT(menu), "destroy",
528                          G_CALLBACK(vf_popup_destroy_cb), vf);
529
530         if (vf->clicked_mark > 0)
531                 {
532                 gint mark = vf->clicked_mark;
533                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
534                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
535                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
536                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
537                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
538                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
539                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
540
541                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
542
543                 vf->active_mark = mark;
544                 vf->clicked_mark = 0;
545
546                 menu_item_add_sensitive(menu, str_set_mark, active,
547                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
548
549                 menu_item_add_sensitive(menu, str_res_mark, active,
550                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
551
552                 menu_item_add_sensitive(menu, str_toggle_mark, active,
553                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
554
555                 menu_item_add_divider(menu);
556
557                 menu_item_add_sensitive(menu, str_sel_mark, active,
558                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
559                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
560                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
561                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
562                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
563                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
564                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
565
566                 menu_item_add_divider(menu);
567
568                 g_free(str_set_mark);
569                 g_free(str_res_mark);
570                 g_free(str_toggle_mark);
571                 g_free(str_sel_mark);
572                 g_free(str_sel_mark_and);
573                 g_free(str_sel_mark_or);
574                 g_free(str_sel_mark_minus);
575                 }
576
577         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
578         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
579         gtk_widget_set_sensitive(item, active);
580
581         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
582                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
583
584         menu_item_add_divider(menu);
585         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
586                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
587         menu_item_add_sensitive(menu, _("_Move..."), active,
588                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
589         menu_item_add_sensitive(menu, _("_Rename..."), active,
590                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
591         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
592                                       G_CALLBACK(vf_pop_menu_delete_cb), vf);
593         menu_item_add_sensitive(menu, _("_Copy path"), active,
594                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
595
596         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
597                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
598         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
599                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
600
601         menu_item_add_divider(menu);
602         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
603                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
604         menu_item_add_divider(menu);
605         menu_item_add_stock_sensitive(menu, _("Add to new collection"), GTK_STOCK_INDEX, active,
606                                 G_CALLBACK(vf_pop_menu_add_collection_cb), vf);
607         menu_item_add_divider(menu);
608
609         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
610                                    FALSE, FALSE, TRUE, vf->sort_method);
611         menu_item_add_divider(submenu);
612         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
613                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
614
615         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
616         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
617
618         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
619                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
620
621         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
622                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
623
624         switch (vf->type)
625         {
626         case FILEVIEW_LIST:
627                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
628                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
629                 break;
630         case FILEVIEW_ICON:
631                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
632                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
633                 break;
634         }
635
636         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
637
638         return menu;
639 }
640
641 gboolean vf_refresh(ViewFile *vf)
642 {
643         switch (vf->type)
644         {
645         case FILEVIEW_LIST: return vflist_refresh(vf);
646         case FILEVIEW_ICON: return vficon_refresh(vf);
647         }
648 }
649
650 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
651 {
652         switch (vf->type)
653         {
654         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
655         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
656         }
657 }
658
659 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
660 {
661         ViewFile *vf = data;
662
663         switch (vf->type)
664         {
665         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
666         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
667         }
668
669         if (vf->popup)
670                 {
671                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
672                                                      0, 0, 0, NULL, vf);
673                 gtk_widget_destroy(vf->popup);
674                 }
675
676         file_data_unref(vf->dir_fd);
677         g_free(vf->info);
678         g_free(vf);
679 }
680
681 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
682 {
683         ViewFile *vf = data;
684         vf_refresh_idle(vf);
685 }
686
687
688 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
689 {
690         GtkWidget *frame = gtk_frame_new(NULL);
691         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
692
693         gint i;
694
695         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
696                 {
697                 GtkWidget *check = gtk_check_button_new();
698                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
699                 g_signal_connect(G_OBJECT(check), "toggled",
700                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
701
702                 gtk_widget_show(check);
703                 vf->filter_check[i] = check;
704                 }
705         gtk_container_add(GTK_CONTAINER(frame), hbox);
706         gtk_widget_show(hbox);
707         return frame;
708 }
709
710 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
711 {
712         gint n = mark - 1;
713         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
714                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
715 }
716
717 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
718 {
719         ViewFile *vf;
720
721         vf = g_new0(ViewFile, 1);
722
723         vf->type = type;
724         vf->sort_method = SORT_NAME;
725         vf->sort_ascend = TRUE;
726
727         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
728         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
729         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
730                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
731
732         vf->filter = vf_marks_filter_init(vf);
733
734         vf->widget = gtk_vbox_new(FALSE, 0);
735         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
736         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
737         gtk_widget_show(vf->scrolled);
738
739         g_signal_connect(G_OBJECT(vf->widget), "destroy",
740                          G_CALLBACK(vf_destroy_cb), vf);
741
742         switch (type)
743         {
744         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
745         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
746         }
747
748         vf_dnd_init(vf);
749
750         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
751                          G_CALLBACK(vf_press_key_cb), vf);
752         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
753                          G_CALLBACK(vf_press_cb), vf);
754         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
755                          G_CALLBACK(vf_release_cb), vf);
756
757         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
758         gtk_widget_show(vf->listview);
759
760         if (dir_fd) vf_set_fd(vf, dir_fd);
761
762         return vf;
763 }
764
765 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
766 {
767         vf->func_status = func;
768         vf->data_status = data;
769 }
770
771 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
772 {
773         vf->func_thumb_status = func;
774         vf->data_thumb_status = data;
775 }
776
777 void vf_thumb_set(ViewFile *vf, gboolean enable)
778 {
779         switch (vf->type)
780         {
781         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
782         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
783         }
784 }
785
786
787 static gboolean vf_thumb_next(ViewFile *vf);
788
789 static gdouble vf_thumb_progress(ViewFile *vf)
790 {
791         gint count = 0;
792         gint done = 0;
793
794         switch (vf->type)
795         {
796         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
797         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
798         }
799
800         DEBUG_1("thumb progress: %d of %d", done, count);
801         return (gdouble)done / count;
802 }
803
804 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
805 {
806         switch (vf->type)
807         {
808         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
809         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
810         }
811 }
812
813 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
814 {
815         if (vf->func_thumb_status)
816                 {
817                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
818                 }
819 }
820
821 static void vf_thumb_do(ViewFile *vf, FileData *fd)
822 {
823         if (!fd) return;
824
825         vf_set_thumb_fd(vf, fd);
826         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
827 }
828
829 void vf_thumb_cleanup(ViewFile *vf)
830 {
831         vf_thumb_status(vf, 0.0, NULL);
832
833         vf->thumbs_running = FALSE;
834
835         thumb_loader_free(vf->thumbs_loader);
836         vf->thumbs_loader = NULL;
837
838         vf->thumbs_filedata = NULL;
839 }
840
841 void vf_thumb_stop(ViewFile *vf)
842 {
843         if (vf->thumbs_running) vf_thumb_cleanup(vf);
844 }
845
846 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
847 {
848         ViewFile *vf = data;
849
850         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
851                 {
852                 vf_thumb_do(vf, vf->thumbs_filedata);
853                 }
854
855         while (vf_thumb_next(vf));
856 }
857
858 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
859 {
860         vf_thumb_common_cb(tl, data);
861 }
862
863 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
864 {
865         vf_thumb_common_cb(tl, data);
866 }
867
868 static gboolean vf_thumb_next(ViewFile *vf)
869 {
870         FileData *fd = NULL;
871
872         if (!gtk_widget_get_realized(vf->listview))
873                 {
874                 vf_thumb_status(vf, 0.0, NULL);
875                 return FALSE;
876                 }
877
878         switch (vf->type)
879         {
880         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
881         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
882         }
883
884         if (!fd)
885                 {
886                 /* done */
887                 vf_thumb_cleanup(vf);
888                 return FALSE;
889                 }
890
891         vf->thumbs_filedata = fd;
892
893         thumb_loader_free(vf->thumbs_loader);
894
895         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
896         thumb_loader_set_callbacks(vf->thumbs_loader,
897                                    vf_thumb_done_cb,
898                                    vf_thumb_error_cb,
899                                    NULL,
900                                    vf);
901
902         if (!thumb_loader_start(vf->thumbs_loader, fd))
903                 {
904                 /* set icon to unknown, continue */
905                 DEBUG_1("thumb loader start failed %s", fd->path);
906                 vf_thumb_do(vf, fd);
907
908                 return TRUE;
909                 }
910
911         return FALSE;
912 }
913
914 static void vf_thumb_reset_all(ViewFile *vf)
915 {
916         GList *work;
917
918         for (work = vf->list; work; work = work->next)
919                 {
920                 FileData *fd = work->data;
921                 if (fd->thumb_pixbuf)
922                         {
923                         g_object_unref(fd->thumb_pixbuf);
924                         fd->thumb_pixbuf = NULL;
925                         }
926                 }
927 }
928
929 void vf_thumb_update(ViewFile *vf)
930 {
931         vf_thumb_stop(vf);
932
933         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
934
935         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
936         vf->thumbs_running = TRUE;
937
938         if (thumb_format_changed)
939                 {
940                 vf_thumb_reset_all(vf);
941                 thumb_format_changed = FALSE;
942                 }
943
944         while (vf_thumb_next(vf));
945 }
946
947
948 void vf_marks_set(ViewFile *vf, gboolean enable)
949 {
950         if (vf->marks_enabled == enable) return;
951
952         vf->marks_enabled = enable;
953
954         switch (vf->type)
955         {
956         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
957         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
958         }
959         if (enable)
960                 gtk_widget_show(vf->filter);
961         else
962                 gtk_widget_hide(vf->filter);
963
964         vf_refresh_idle(vf);
965 }
966
967 guint vf_marks_get_filter(ViewFile *vf)
968 {
969         guint ret = 0;
970         gint i;
971         if (!vf->marks_enabled) return 0;
972
973         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
974                 {
975                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
976                         {
977                         ret |= 1 << i;
978                         }
979                 }
980         return ret;
981 }
982
983 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
984 {
985         vf->layout = layout;
986 }
987
988
989 /*
990  *-----------------------------------------------------------------------------
991  * maintenance (for rename, move, remove)
992  *-----------------------------------------------------------------------------
993  */
994
995 static gboolean vf_refresh_idle_cb(gpointer data)
996 {
997         ViewFile *vf = data;
998
999         vf_refresh(vf);
1000         vf->refresh_idle_id = 0;
1001         return FALSE;
1002 }
1003
1004 void vf_refresh_idle_cancel(ViewFile *vf)
1005 {
1006         if (vf->refresh_idle_id)
1007                 {
1008                 g_source_remove(vf->refresh_idle_id);
1009                 vf->refresh_idle_id = 0;
1010                 }
1011 }
1012
1013
1014 void vf_refresh_idle(ViewFile *vf)
1015 {
1016         if (!vf->refresh_idle_id)
1017                 {
1018                 vf->time_refresh_set = time(NULL);
1019                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1020                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1021                 }
1022         else if (time(NULL) - vf->time_refresh_set > 1)
1023                 {
1024                 /* more than 1 sec since last update - increase priority */
1025                 vf_refresh_idle_cancel(vf);
1026                 vf->time_refresh_set = time(NULL);
1027                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1028                 }
1029 }
1030
1031 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1032 {
1033         ViewFile *vf = data;
1034         gboolean refresh;
1035
1036         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1037         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1038         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1039
1040         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1041
1042         refresh = (fd == vf->dir_fd);
1043
1044         if (!refresh)
1045                 {
1046                 gchar *base = remove_level_from_path(fd->path);
1047                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1048                 g_free(base);
1049                 }
1050
1051         if ((type & NOTIFY_CHANGE) && fd->change)
1052                 {
1053                 if (!refresh && fd->change->dest)
1054                         {
1055                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1056                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1057                         g_free(dest_base);
1058                         }
1059
1060                 if (!refresh && fd->change->source)
1061                         {
1062                         gchar *source_base = remove_level_from_path(fd->change->source);
1063                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1064                         g_free(source_base);
1065                         }
1066                 }
1067
1068         if (refresh)
1069                 {
1070                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1071                 vf_refresh_idle(vf);
1072                 }
1073 }
1074
1075 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */