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