Fix up unused variables warnings.
[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->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 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
715 {
716         ViewFile *vf;
717
718         vf = g_new0(ViewFile, 1);
719         
720         vf->type = type;
721         vf->sort_method = SORT_NAME;
722         vf->sort_ascend = TRUE;
723
724         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
725         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
726         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
727                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
728         
729         vf->filter = vf_marks_filter_init(vf);
730
731         vf->widget = gtk_vbox_new(FALSE, 0);
732         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
733         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
734         gtk_widget_show(vf->scrolled);
735         
736         g_signal_connect(G_OBJECT(vf->widget), "destroy",
737                          G_CALLBACK(vf_destroy_cb), vf);
738
739         switch (type)
740         {
741         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
742         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
743         }
744
745         vf_dnd_init(vf);
746
747         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
748                          G_CALLBACK(vf_press_key_cb), vf);
749         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
750                          G_CALLBACK(vf_press_cb), vf);
751         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
752                          G_CALLBACK(vf_release_cb), vf);
753
754         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
755         gtk_widget_show(vf->listview);
756
757         if (dir_fd) vf_set_fd(vf, dir_fd);
758
759         return vf;
760 }
761
762 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
763 {
764         vf->func_status = func;
765         vf->data_status = data;
766 }
767
768 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
769 {
770         vf->func_thumb_status = func;
771         vf->data_thumb_status = data;
772 }
773
774 void vf_thumb_set(ViewFile *vf, gboolean enable)
775 {
776         switch (vf->type)
777         {
778         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
779         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
780         }
781 }
782
783
784 static gboolean vf_thumb_next(ViewFile *vf);
785
786 static gdouble vf_thumb_progress(ViewFile *vf)
787 {
788         gint count = 0;
789         gint done = 0;
790         
791         switch (vf->type)
792         {
793         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
794         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
795         }
796         
797         DEBUG_1("thumb progress: %d of %d", done, count);
798         return (gdouble)done / count;
799 }
800
801 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
802 {       
803         switch (vf->type)
804         {
805         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
806         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
807         }
808 }
809
810 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
811 {
812         if (vf->func_thumb_status)
813                 {
814                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
815                 }
816 }
817
818 static void vf_thumb_do(ViewFile *vf, FileData *fd)
819 {
820         if (!fd) return;
821
822         vf_set_thumb_fd(vf, fd);
823         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
824 }
825
826 void vf_thumb_cleanup(ViewFile *vf)
827 {
828         vf_thumb_status(vf, 0.0, NULL);
829
830         vf->thumbs_running = FALSE;
831
832         thumb_loader_free(vf->thumbs_loader);
833         vf->thumbs_loader = NULL;
834
835         vf->thumbs_filedata = NULL;
836 }
837
838 void vf_thumb_stop(ViewFile *vf)
839 {
840         if (vf->thumbs_running) vf_thumb_cleanup(vf);
841 }
842
843 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
844 {
845         ViewFile *vf = data;
846
847         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
848                 {
849                 vf_thumb_do(vf, vf->thumbs_filedata);
850                 }
851
852         while (vf_thumb_next(vf));
853 }
854
855 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
856 {
857         vf_thumb_common_cb(tl, data);
858 }
859
860 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
861 {
862         vf_thumb_common_cb(tl, data);
863 }
864
865 static gboolean vf_thumb_next(ViewFile *vf)
866 {
867         FileData *fd = NULL;
868
869         if (!GTK_WIDGET_REALIZED(vf->listview))
870                 {
871                 vf_thumb_status(vf, 0.0, NULL);
872                 return FALSE;
873                 }
874
875         switch (vf->type)
876         {
877         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
878         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
879         }
880
881         if (!fd)
882                 {
883                 /* done */
884                 vf_thumb_cleanup(vf);
885                 return FALSE;
886                 }
887
888         vf->thumbs_filedata = fd;
889
890         thumb_loader_free(vf->thumbs_loader);
891
892         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
893         thumb_loader_set_callbacks(vf->thumbs_loader,
894                                    vf_thumb_done_cb,
895                                    vf_thumb_error_cb,
896                                    NULL,
897                                    vf);
898
899         if (!thumb_loader_start(vf->thumbs_loader, fd))
900                 {
901                 /* set icon to unknown, continue */
902                 DEBUG_1("thumb loader start failed %s", fd->path);
903                 vf_thumb_do(vf, fd);
904
905                 return TRUE;
906                 }
907
908         return FALSE;
909 }
910
911 static void vf_thumb_reset_all(ViewFile *vf)
912 {
913         switch (vf->type)
914         {
915         case FILEVIEW_LIST: vflist_thumb_reset_all(vf); break;
916         case FILEVIEW_ICON: vficon_thumb_reset_all(vf); break;
917         }
918 }
919
920 void vf_thumb_update(ViewFile *vf)
921 {
922         vf_thumb_stop(vf);
923         
924         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
925
926         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
927         vf->thumbs_running = TRUE;
928
929         if (thumb_format_changed)
930                 {
931                 vf_thumb_reset_all(vf);
932                 thumb_format_changed = FALSE;
933                 }
934
935         while (vf_thumb_next(vf));
936 }
937
938
939 void vf_marks_set(ViewFile *vf, gboolean enable)
940 {
941         if (vf->marks_enabled == enable) return;
942
943         vf->marks_enabled = enable;
944
945         switch (vf->type)
946         {
947         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
948         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
949         }
950         if (enable)
951                 gtk_widget_show(vf->filter);
952         else
953                 gtk_widget_hide(vf->filter);
954
955         vf_refresh_idle(vf);
956 }
957
958 guint vf_marks_get_filter(ViewFile *vf)
959 {
960         guint ret = 0;
961         gint i;
962         if (!vf->marks_enabled) return 0;
963         
964         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
965                 {
966                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
967                         {
968                         ret |= 1 << i;
969                         }
970                 }
971         return ret;
972 }
973
974 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
975 {
976         vf->layout = layout;
977 }
978
979
980 /*
981  *-----------------------------------------------------------------------------
982  * maintenance (for rename, move, remove)
983  *-----------------------------------------------------------------------------
984  */
985
986 static gboolean vf_refresh_idle_cb(gpointer data)
987 {
988         ViewFile *vf = data;
989
990         vf_refresh(vf);
991         vf->refresh_idle_id = 0;
992         return FALSE;
993 }
994
995 void vf_refresh_idle_cancel(ViewFile *vf)
996 {
997         if (vf->refresh_idle_id)
998                 {
999                 g_source_remove(vf->refresh_idle_id);
1000                 vf->refresh_idle_id = 0;
1001                 }
1002 }
1003
1004
1005 void vf_refresh_idle(ViewFile *vf)
1006 {
1007         if (!vf->refresh_idle_id)
1008                 {
1009                 vf->refresh_idle_id = g_idle_add(vf_refresh_idle_cb, vf);
1010                 }
1011 }
1012
1013 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1014 {
1015         ViewFile *vf = data;
1016         gboolean refresh;
1017
1018         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1019         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1020         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1021
1022         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1023         
1024         refresh = (fd == vf->dir_fd);
1025
1026         if (!refresh)
1027                 {
1028                 gchar *base = remove_level_from_path(fd->path);
1029                 refresh = (strcmp(base, vf->dir_fd->path) == 0);
1030                 g_free(base);
1031                 }
1032
1033         if ((type & NOTIFY_CHANGE) && fd->change)
1034                 {
1035                 if (!refresh && fd->change->dest)
1036                         {
1037                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1038                         refresh = (strcmp(dest_base, vf->dir_fd->path) == 0);
1039                         g_free(dest_base);
1040                         }
1041
1042                 if (!refresh && fd->change->source)
1043                         {
1044                         gchar *source_base = remove_level_from_path(fd->change->source);
1045                         refresh = (strcmp(source_base, vf->dir_fd->path) == 0);
1046                         g_free(source_base);
1047                         }
1048                 }
1049         
1050         if (refresh)
1051                 {
1052                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1053                 vf_refresh_idle(vf);
1054                 }
1055 }
1056
1057 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */