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