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