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