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