File filter - include Clear icon
[geeqie.git] / src / view_file / view_file.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Author: Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "main.h"
22 #include "view_file.h"
23
24 #include "dupe.h"
25 #include "collect.h"
26 #include "collect-table.h"
27 #include "editors.h"
28 #include "history_list.h"
29 #include "layout.h"
30 #include "menu.h"
31 #include "misc.h"
32 #include "pixbuf_util.h"
33 #include "thumb.h"
34 #include "ui_menu.h"
35 #include "ui_fileops.h"
36 #include "ui_misc.h"
37 #include "utilops.h"
38 #include "view_file/view_file_list.h"
39 #include "view_file/view_file_icon.h"
40 #include "window.h"
41
42 /*
43  *-----------------------------------------------------------------------------
44  * signals
45  *-----------------------------------------------------------------------------
46  */
47
48 void vf_send_update(ViewFile *vf)
49 {
50         if (vf->func_status) vf->func_status(vf, vf->data_status);
51 }
52
53 /*
54  *-----------------------------------------------------------------------------
55  * misc
56  *-----------------------------------------------------------------------------
57  */
58
59 void vf_sort_set(ViewFile *vf, SortType type, gboolean ascend)
60 {
61         switch (vf->type)
62         {
63         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend); break;
64         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend); break;
65         }
66 }
67
68 /*
69  *-----------------------------------------------------------------------------
70  * row stuff
71  *-----------------------------------------------------------------------------
72  */
73
74 FileData *vf_index_get_data(ViewFile *vf, gint row)
75 {
76         return g_list_nth_data(vf->list, row);
77 }
78
79 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
80 {
81         gint ret;
82
83         switch (vf->type)
84         {
85         case FILEVIEW_LIST: ret = vflist_index_by_fd(vf, fd); break;
86         case FILEVIEW_ICON: ret = vficon_index_by_fd(vf, fd); break;
87         default: ret = 0;
88         }
89
90         return ret;
91 }
92
93 guint vf_count(ViewFile *vf, gint64 *bytes)
94 {
95         if (bytes)
96                 {
97                 gint64 b = 0;
98                 GList *work;
99
100                 work = vf->list;
101                 while (work)
102                         {
103                         FileData *fd = work->data;
104                         work = work->next;
105
106                         b += fd->size;
107                         }
108
109                 *bytes = b;
110                 }
111
112         return g_list_length(vf->list);
113 }
114
115 GList *vf_get_list(ViewFile *vf)
116 {
117         GList *list = NULL;
118         GList *work;
119         for (work = vf->list; work; work = work->next)
120                 {
121                 FileData *fd = work->data;
122                 list = g_list_prepend(list, file_data_ref(fd));
123                 }
124
125         return g_list_reverse(list);
126 }
127
128 /*
129  *-------------------------------------------------------------------
130  * keyboard
131  *-------------------------------------------------------------------
132  */
133
134 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
135 {
136         ViewFile *vf = data;
137         gboolean ret;
138
139         switch (vf->type)
140         {
141         case FILEVIEW_LIST: ret = vflist_press_key_cb(widget, event, data); break;
142         case FILEVIEW_ICON: ret = vficon_press_key_cb(widget, event, data); break;
143         default: ret = FALSE;
144         }
145
146         return ret;
147 }
148
149 /*
150  *-------------------------------------------------------------------
151  * mouse
152  *-------------------------------------------------------------------
153  */
154
155 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
156 {
157         ViewFile *vf = data;
158         gboolean ret;
159
160         switch (vf->type)
161         {
162         case FILEVIEW_LIST: ret = vflist_press_cb(widget, bevent, data); break;
163         case FILEVIEW_ICON: ret = vficon_press_cb(widget, bevent, data); break;
164         default: ret = FALSE;
165         }
166
167         return ret;
168 }
169
170 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
171 {
172         ViewFile *vf = data;
173         gboolean ret;
174
175         switch (vf->type)
176         {
177         case FILEVIEW_LIST: ret = vflist_release_cb(widget, bevent, data); break;
178         case FILEVIEW_ICON: ret = vficon_release_cb(widget, bevent, data); break;
179         default: ret = FALSE;
180         }
181
182         return ret;
183 }
184
185
186 /*
187  *-----------------------------------------------------------------------------
188  * selections
189  *-----------------------------------------------------------------------------
190  */
191
192 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
193 {
194         guint ret;
195
196         switch (vf->type)
197         {
198         case FILEVIEW_LIST: ret = vflist_selection_count(vf, bytes); break;
199         case FILEVIEW_ICON: ret = vficon_selection_count(vf, bytes); break;
200         default: ret = 0;
201         }
202
203         return ret;
204 }
205
206 GList *vf_selection_get_list(ViewFile *vf)
207 {
208         GList *ret;
209
210         switch (vf->type)
211         {
212         case FILEVIEW_LIST: ret = vflist_selection_get_list(vf); break;
213         case FILEVIEW_ICON: ret = vficon_selection_get_list(vf); break;
214         default: ret = NULL;
215         }
216
217         return ret;
218 }
219
220 GList *vf_selection_get_list_by_index(ViewFile *vf)
221 {
222         GList *ret;
223
224         switch (vf->type)
225         {
226         case FILEVIEW_LIST: ret = vflist_selection_get_list_by_index(vf); break;
227         case FILEVIEW_ICON: ret = vficon_selection_get_list_by_index(vf); break;
228         default: ret = NULL;
229         }
230
231         return ret;
232 }
233
234 void vf_select_all(ViewFile *vf)
235 {
236         switch (vf->type)
237         {
238         case FILEVIEW_LIST: vflist_select_all(vf); break;
239         case FILEVIEW_ICON: vficon_select_all(vf); break;
240         }
241 }
242
243 void vf_select_none(ViewFile *vf)
244 {
245         switch (vf->type)
246         {
247         case FILEVIEW_LIST: vflist_select_none(vf); break;
248         case FILEVIEW_ICON: vficon_select_none(vf); break;
249         }
250 }
251
252 void vf_select_invert(ViewFile *vf)
253 {
254         switch (vf->type)
255         {
256         case FILEVIEW_LIST: vflist_select_invert(vf); break;
257         case FILEVIEW_ICON: vficon_select_invert(vf); break;
258         }
259 }
260
261 void vf_select_by_fd(ViewFile *vf, FileData *fd)
262 {
263         switch (vf->type)
264         {
265         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
266         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
267         }
268 }
269
270 void vf_select_list(ViewFile *vf, GList *list)
271 {
272         switch (vf->type)
273         {
274         case FILEVIEW_LIST: vflist_select_list(vf, list); break;
275         case FILEVIEW_ICON: vficon_select_list(vf, list); break;
276         }
277 }
278
279 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
280 {
281         switch (vf->type)
282         {
283         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
284         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
285         }
286 }
287
288 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
289 {
290         switch (vf->type)
291         {
292         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
293         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
294         }
295 }
296
297 /*
298  *-----------------------------------------------------------------------------
299  * dnd
300  *-----------------------------------------------------------------------------
301  */
302
303
304 static void vf_dnd_init(ViewFile *vf)
305 {
306         switch (vf->type)
307         {
308         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
309         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
310         }
311 }
312
313 /*
314  *-----------------------------------------------------------------------------
315  * pop-up menu
316  *-----------------------------------------------------------------------------
317  */
318
319 GList *vf_pop_menu_file_list(ViewFile *vf)
320 {
321         GList *ret;
322
323         switch (vf->type)
324         {
325         case FILEVIEW_LIST: ret = vflist_pop_menu_file_list(vf); break;
326         case FILEVIEW_ICON: ret = vficon_pop_menu_file_list(vf); break;
327         default: ret = NULL;
328         }
329
330         return ret;
331 }
332
333 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
334 {
335         GList *ret;
336
337         switch (vf->type)
338         {
339         case FILEVIEW_LIST: ret = vflist_selection_get_one(vf, fd); break;
340         case FILEVIEW_ICON: ret = vficon_selection_get_one(vf, fd); break;
341         default: ret = NULL;
342         }
343
344         return ret;
345 }
346
347 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
348 {
349         ViewFile *vf;
350         const gchar *key = data;
351
352         vf = submenu_item_get_data(widget);
353
354         if (!vf) return;
355
356         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
357 }
358
359 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
360 {
361         ViewFile *vf = data;
362
363         switch (vf->type)
364         {
365         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
366         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
367         }
368 }
369
370 static void vf_pop_menu_open_archive_cb(GtkWidget *widget, gpointer data)
371 {
372         ViewFile *vf = data;
373         LayoutWindow *lw_new;
374         FileData *fd;
375         gchar *dest_dir;
376
377         switch (vf->type)
378         {
379         case FILEVIEW_LIST:
380                 fd = (VFLIST(vf)->click_fd);
381                 break;
382         case FILEVIEW_ICON:
383                 fd = (VFICON(vf)->click_fd);
384                 break;
385         }
386
387         dest_dir = open_archive(fd);
388         if (dest_dir)
389                 {
390                 lw_new = layout_new_from_default();
391                 layout_set_path(lw_new, dest_dir);
392                 g_free(dest_dir);
393                 }
394         else
395                 {
396                 warning_dialog(_("Cannot open archive file"), _("See the Log Window"), GTK_STOCK_DIALOG_WARNING, NULL);
397                 }
398 }
399
400 static void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
401 {
402         ViewFile *vf = data;
403
404         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
405 }
406
407 static void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
408 {
409         ViewFile *vf = data;
410
411         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
412 }
413
414 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
415 {
416         ViewFile *vf = data;
417
418         switch (vf->type)
419         {
420         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
421         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
422         }
423 }
424
425 static void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
426 {
427         ViewFile *vf = data;
428
429         options->file_ops.safe_delete_enable = FALSE;
430         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
431 }
432
433 static void vf_pop_menu_move_to_trash_cb(GtkWidget *widget, gpointer data)
434 {
435         ViewFile *vf = data;
436
437         options->file_ops.safe_delete_enable = TRUE;
438         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
439 }
440
441 static void vf_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
442 {
443         ViewFile *vf = data;
444
445         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), TRUE);
446 }
447
448 static void vf_pop_menu_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
449 {
450         ViewFile *vf = data;
451
452         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE);
453 }
454
455 static void vf_pop_menu_enable_grouping_cb(GtkWidget *widget, gpointer data)
456 {
457         ViewFile *vf = data;
458
459         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
460 }
461
462 static void vf_pop_menu_duplicates_cb(GtkWidget *widget, gpointer data)
463 {
464         ViewFile *vf = data;
465         DupeWindow *dw;
466
467         dw = dupe_window_new();
468         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
469 }
470
471 static void vf_pop_menu_disable_grouping_cb(GtkWidget *widget, gpointer data)
472 {
473         ViewFile *vf = data;
474
475         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
476 }
477
478 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
479 {
480         ViewFile *vf;
481         SortType type;
482
483         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
484
485         vf = submenu_item_get_data(widget);
486         if (!vf) return;
487
488         type = (SortType)GPOINTER_TO_INT(data);
489
490         if (type == SORT_EXIFTIME || type == SORT_EXIFTIMEDIGITIZED || type == SORT_RATING)
491                 {
492                 vf_read_metadata_in_idle(vf);
493                 }
494
495         if (vf->layout)
496                 {
497                 layout_sort_set(vf->layout, type, vf->sort_ascend);
498                 }
499         else
500                 {
501                 vf_sort_set(vf, type, vf->sort_ascend);
502                 }
503 }
504
505 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
506 {
507         ViewFile *vf = data;
508
509         if (vf->layout)
510                 {
511                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
512                 }
513         else
514                 {
515                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
516                 }
517 }
518
519 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
520 {
521         ViewFile *vf = data;
522         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
523 }
524
525 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
526 {
527         ViewFile *vf = data;
528         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
529 }
530
531 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
532 {
533         ViewFile *vf = data;
534         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
535 }
536
537 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
538 {
539         ViewFile *vf = data;
540         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
541 }
542
543 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
544 {
545         ViewFile *vf = data;
546         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
547 }
548
549 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
550 {
551         ViewFile *vf = data;
552         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
553 }
554
555 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
556 {
557         ViewFile *vf = data;
558         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
559 }
560
561 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
562 {
563         ViewFile *vf = data;
564         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
565         if (!vf->layout) return;
566
567         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
568 }
569
570 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
571 {
572         ViewFile *vf = data;
573
574         switch (vf->type)
575         {
576         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
577         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
578         }
579 }
580
581 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
582 {
583         ViewFile *vf = data;
584
585         switch (vf->type)
586         {
587         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
588         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
589         }
590
591         filelist_free(vf->editmenu_fd_list);
592         vf->editmenu_fd_list = NULL;
593 }
594
595 /**
596  * @brief Add file selection list to a collection
597  * @param[in] widget 
598  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
599  * 
600  * 
601  */
602 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
603 {
604         ViewFile *vf;
605         GList *selection_list;
606
607         vf = submenu_item_get_data(widget);
608         selection_list = vf_selection_get_list(vf);
609         pop_menu_collections(selection_list, data);
610
611         filelist_free(selection_list);
612 }
613
614 GtkWidget *vf_pop_menu(ViewFile *vf)
615 {
616         GtkWidget *menu;
617         GtkWidget *item;
618         GtkWidget *submenu;
619         gboolean active = FALSE;
620         gboolean class_archive = FALSE;
621         GtkAccelGroup *accel_group;
622
623         switch (vf->type)
624         {
625         case FILEVIEW_LIST:
626                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
627                 active = (VFLIST(vf)->click_fd != NULL);
628                 class_archive = (VFLIST(vf)->click_fd != NULL && VFLIST(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
629                 break;
630         case FILEVIEW_ICON:
631                 active = (VFICON(vf)->click_fd != NULL);
632                 class_archive = (VFICON(vf)->click_fd != NULL && VFICON(vf)->click_fd->format_class == FORMAT_CLASS_ARCHIVE);
633                 break;
634         }
635
636         menu = popup_menu_short_lived();
637
638         accel_group = gtk_accel_group_new();
639         gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
640
641         g_object_set_data(G_OBJECT(menu), "window_keys", NULL);
642         g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
643
644         g_signal_connect(G_OBJECT(menu), "destroy",
645                          G_CALLBACK(vf_popup_destroy_cb), vf);
646
647         if (vf->clicked_mark > 0)
648                 {
649                 gint mark = vf->clicked_mark;
650                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
651                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
652                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
653                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
654                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
655                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
656                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
657
658                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
659
660                 vf->active_mark = mark;
661                 vf->clicked_mark = 0;
662
663                 menu_item_add_sensitive(menu, str_set_mark, active,
664                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
665
666                 menu_item_add_sensitive(menu, str_res_mark, active,
667                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
668
669                 menu_item_add_sensitive(menu, str_toggle_mark, active,
670                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
671
672                 menu_item_add_divider(menu);
673
674                 menu_item_add_sensitive(menu, str_sel_mark, active,
675                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
676                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
677                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
678                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
679                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
680                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
681                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
682
683                 menu_item_add_divider(menu);
684
685                 g_free(str_set_mark);
686                 g_free(str_res_mark);
687                 g_free(str_toggle_mark);
688                 g_free(str_sel_mark);
689                 g_free(str_sel_mark_and);
690                 g_free(str_sel_mark_or);
691                 g_free(str_sel_mark_minus);
692                 }
693
694         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
695         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
696         gtk_widget_set_sensitive(item, active);
697
698         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
699                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
700
701         menu_item_add_stock_sensitive(menu, _("Open archive"), GTK_STOCK_OPEN, active & class_archive, G_CALLBACK(vf_pop_menu_open_archive_cb), vf);
702
703         menu_item_add_divider(menu);
704         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
705                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
706         menu_item_add_sensitive(menu, _("_Move..."), active,
707                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
708         menu_item_add_sensitive(menu, _("_Rename..."), active,
709                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
710         menu_item_add_sensitive(menu, _("_Copy path to clipboard"), active,
711                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
712         menu_item_add_sensitive(menu, _("_Copy path unquoted to clipboard"), active,
713                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
714         menu_item_add_divider(menu);
715         menu_item_add_stock_sensitive(menu,
716                                 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
717                                         _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, active,
718                                 G_CALLBACK(vf_pop_menu_move_to_trash_cb), vf);
719         menu_item_add_stock_sensitive(menu,
720                                 options->file_ops.confirm_delete ? _("_Delete...") :
721                                         _("_Delete"), GTK_STOCK_DELETE, active,
722                                 G_CALLBACK(vf_pop_menu_delete_cb), vf);
723         menu_item_add_divider(menu);
724
725         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
726                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
727         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
728                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
729
730         menu_item_add_divider(menu);
731         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
732                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
733         menu_item_add_divider(menu);
734
735         submenu = submenu_add_collections(menu, &item,
736                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
737         gtk_widget_set_sensitive(item, active);
738         menu_item_add_divider(menu);
739
740         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
741                                    FALSE, FALSE, TRUE, vf->sort_method);
742         menu_item_add_divider(submenu);
743         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
744                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
745
746         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
747         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
748
749         item = menu_item_add_radio(menu, _("Images as List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
750                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
751
752         item = menu_item_add_radio(menu, _("Images as Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
753                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
754
755         switch (vf->type)
756         {
757         case FILEVIEW_LIST:
758                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
759                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
760                 break;
761         case FILEVIEW_ICON:
762                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
763                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
764                 break;
765         }
766
767         switch (vf->type)
768         {
769         case FILEVIEW_LIST:
770                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
771                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
772                 break;
773         case FILEVIEW_ICON:
774                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
775                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
776                 break;
777         }
778
779         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
780
781         return menu;
782 }
783
784 gboolean vf_refresh(ViewFile *vf)
785 {
786         gboolean ret;
787
788         switch (vf->type)
789         {
790         case FILEVIEW_LIST: ret = vflist_refresh(vf); break;
791         case FILEVIEW_ICON: ret = vficon_refresh(vf); break;
792         default: ret = FALSE;
793         }
794
795         return ret;
796 }
797
798 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
799 {
800         gboolean ret;
801
802         switch (vf->type)
803         {
804         case FILEVIEW_LIST: ret = vflist_set_fd(vf, dir_fd); break;
805         case FILEVIEW_ICON: ret = vficon_set_fd(vf, dir_fd); break;
806         default: ret = FALSE;
807         }
808
809         return ret;
810 }
811
812 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
813 {
814         ViewFile *vf = data;
815
816         switch (vf->type)
817         {
818         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
819         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
820         }
821
822         if (vf->popup)
823                 {
824                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
825                                                      0, 0, 0, NULL, vf);
826                 gtk_widget_destroy(vf->popup);
827                 }
828
829         if (vf->read_metadata_in_idle_id)
830                 {
831                 g_idle_remove_by_data(vf);
832                 }
833         file_data_unref(vf->dir_fd);
834         g_free(vf->info);
835         g_free(vf);
836 }
837
838 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
839 {
840         ViewFile *vf = data;
841         vf_refresh_idle(vf);
842 }
843
844 typedef struct _MarksTextEntry MarksTextEntry;
845 struct _MarksTextEntry {
846         GenericDialog *gd;
847         gint mark_no;
848         GtkWidget *edit_widget;
849         gchar *text_entry;
850         GtkWidget *parent;
851 };
852
853 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
854 {
855         MarksTextEntry *mte = data;
856
857         g_free(mte->text_entry);
858         generic_dialog_close(gd);
859 }
860
861 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
862 {
863         MarksTextEntry *mte = data;
864
865         g_free(options->marks_tooltips[mte->mark_no]);
866         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
867
868         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
869
870         g_free(mte->text_entry);
871         generic_dialog_close(gd);
872 }
873
874 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
875                                                                         GdkEvent *event, gpointer userdata)
876 {
877         MarksTextEntry *mte = userdata;
878
879         g_free(mte->text_entry);
880         mte->text_entry = g_strdup("");
881         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
882 }
883
884 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
885 {
886         help_window_show("GuideImageMarks.html");
887 }
888
889 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
890                                                                                 GdkEventButton *event,
891                                                                                 gpointer user_data)
892 {
893         GtkWidget *table;
894         gint i = GPOINTER_TO_INT(user_data);
895         MarksTextEntry *mte;
896
897         if (event->button == MOUSE_BUTTON_RIGHT)
898                 {
899                 mte = g_new0(MarksTextEntry, 1);
900                 mte->mark_no = i;
901                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
902                 mte->parent = widget;
903
904                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
905                                                 widget, FALSE,
906                                                 vf_marks_tooltip_cancel_cb, mte);
907                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
908                                             _("This will set or clear the mark text."), FALSE);
909                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
910                                                         vf_marks_tooltip_ok_cb, TRUE);
911                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
912                                                 vf_marks_tooltip_help_cb, FALSE);
913
914                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
915                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
916                 mte->edit_widget = gtk_entry_new();
917                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
918                 if (mte->text_entry)
919                         {
920                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
921                         }
922                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
923                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
924
925                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
926                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
927                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
928                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
929                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
930                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
931
932                 gtk_widget_show(mte->edit_widget);
933                 gtk_widget_grab_focus(mte->edit_widget);
934                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
935
936                 return TRUE;
937                 }
938
939         return FALSE;
940 }
941
942 static void vf_file_filter_save_cb(GtkWidget *widget, gpointer data)
943 {
944         ViewFile *vf = data;
945         gchar *entry_text;
946         gchar *remove_text = NULL;
947         gchar *index_text = NULL;
948         gboolean text_found = FALSE;
949         gint i;
950
951         entry_text = g_strdup(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)))));
952
953         if (entry_text[0] == '\0' && vf->file_filter.last_selected >= 0)
954                 {
955                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.last_selected);
956                 remove_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
957                 history_list_item_remove("file_filter", remove_text);
958                 gtk_combo_box_text_remove(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), vf->file_filter.last_selected);
959                 g_free(remove_text);
960
961                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), -1);
962                 vf->file_filter.last_selected = - 1;
963                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo))), "");
964                 vf->file_filter.count--;
965                 }
966         else
967                 {
968                 if (entry_text[0] != '\0')
969                         {
970                         for (i = 0; i < vf->file_filter.count; i++)
971                                 {
972                                 if (index_text)
973                                         {
974                                         g_free(index_text);
975                                         }
976                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), i);
977                                 index_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
978
979                                 if (g_strcmp0(index_text, entry_text) == 0)
980                                         {
981                                         text_found = TRUE;
982                                         break;
983                                         }
984                                 }
985
986                         g_free(index_text);
987                         if (!text_found)
988                                 {
989                                 history_list_add_to_key("file_filter", entry_text, 10);
990                                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), entry_text);
991                                 vf->file_filter.count++;
992                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), vf->file_filter.count - 1);
993                                 }
994                         }
995                 }
996         vf_refresh(vf);
997
998         g_free(entry_text);
999 }
1000
1001 static void vf_file_filter_cb(GtkWidget *widget, gpointer data)
1002 {
1003         ViewFile *vf = data;
1004
1005         vf_refresh(vf);
1006 }
1007
1008 static gboolean vf_file_filter_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1009 {
1010         ViewFile *vf = data;
1011         vf->file_filter.last_selected = gtk_combo_box_get_active(GTK_COMBO_BOX(vf->file_filter.combo));
1012
1013         gtk_widget_grab_focus(widget);
1014
1015         return TRUE;
1016 }
1017
1018 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
1019 {
1020         GtkWidget *frame = gtk_frame_new(NULL);
1021         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1022
1023         gint i;
1024
1025         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1026                 {
1027                 GtkWidget *check = gtk_check_button_new();
1028                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
1029                 g_signal_connect(G_OBJECT(check), "toggled",
1030                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
1031                 g_signal_connect(G_OBJECT(check), "button_press_event",
1032                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
1033                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
1034
1035                 gtk_widget_show(check);
1036                 vf->filter_check[i] = check;
1037                 }
1038         gtk_container_add(GTK_CONTAINER(frame), hbox);
1039         gtk_widget_show(hbox);
1040         return frame;
1041 }
1042
1043 void vf_file_filter_set(ViewFile *vf, gboolean enable)
1044 {
1045         if (enable)
1046                 {
1047                 gtk_widget_show(vf->file_filter.combo);
1048                 gtk_widget_show(vf->file_filter.frame);
1049                 }
1050         else
1051                 {
1052                 gtk_widget_hide(vf->file_filter.combo);
1053                 gtk_widget_hide(vf->file_filter.frame);
1054                 }
1055
1056         vf_refresh(vf);
1057 }
1058
1059 static gboolean vf_file_filter_class_cb(GtkWidget *widget, gpointer data)
1060 {
1061         ViewFile *vf = data;
1062         gint i;
1063
1064         gboolean state = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget));
1065
1066         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1067                 {
1068                 if (g_strcmp0(format_class_list[i], gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1069                         {
1070                         options->class_filter[i] = state;
1071                         }
1072                 }
1073         vf_refresh(vf);
1074
1075         return TRUE;
1076 }
1077
1078 static gboolean vf_file_filter_class_set_all_cb(GtkWidget *widget, gpointer data)
1079 {
1080         ViewFile *vf = data;
1081         GtkWidget *parent;
1082         GList *children;
1083         GtkWidget *child;
1084         gint i;
1085         gboolean state;
1086
1087         if (g_strcmp0(_("Select all"), gtk_menu_item_get_label(GTK_MENU_ITEM(widget))) == 0)
1088                 {
1089                 state = TRUE;
1090                 }
1091         else
1092                 {
1093                 state = FALSE;
1094                 }
1095
1096         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1097                 {
1098                 options->class_filter[i] = state;
1099                 }
1100
1101         i = 0;
1102         parent = gtk_widget_get_parent(widget);
1103         children = gtk_container_get_children(GTK_CONTAINER(parent));
1104         while (children)
1105                 {
1106                 child = children->data;
1107                 if (i < FILE_FORMAT_CLASSES)
1108                         {
1109                         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(child), state);
1110                         }
1111                 i++;
1112                 children = children->next;
1113                 }
1114         g_list_free(children);
1115         vf_refresh(vf);
1116
1117         return TRUE;
1118 }
1119
1120 static GtkWidget *class_filter_menu (ViewFile *vf)
1121 {
1122         GtkWidget *menu;
1123         GtkWidget *menu_item;
1124         int i;
1125
1126         menu = gtk_menu_new();
1127
1128         for (i = 0; i < FILE_FORMAT_CLASSES; i++)
1129             {
1130                 menu_item = gtk_check_menu_item_new_with_label(format_class_list[i]);
1131                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(menu_item), options->class_filter[i]);
1132                 g_signal_connect(G_OBJECT(menu_item), "toggled", G_CALLBACK(vf_file_filter_class_cb), vf);
1133                 gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1134                 gtk_widget_show(menu_item);
1135                 }
1136
1137         menu_item = gtk_menu_item_new_with_label(_("Select all"));
1138         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1139         gtk_widget_show(menu_item);
1140         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1141
1142         menu_item = gtk_menu_item_new_with_label(_("Select none"));
1143         gtk_menu_shell_append(GTK_MENU_SHELL (menu), menu_item);
1144         gtk_widget_show(menu_item);
1145         g_signal_connect(G_OBJECT(menu_item), "activate", G_CALLBACK(vf_file_filter_class_set_all_cb), vf);
1146
1147         return menu;
1148 }
1149
1150 static void case_sensitive_cb(GtkWidget *widget, gpointer data)
1151 {
1152         ViewFile *vf = data;
1153
1154         vf->file_filter.case_sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
1155         vf_refresh(vf);
1156 }
1157
1158 static void file_filter_clear_cb(GtkEntry *entry, GtkEntryIconPosition pos, GdkEvent *event, gpointer userdata)
1159 {
1160         if (pos == GTK_ENTRY_ICON_SECONDARY)
1161                 {
1162                 gtk_entry_set_text(GTK_ENTRY(userdata), "");
1163                 gtk_widget_grab_focus(GTK_WIDGET(userdata));
1164                 }
1165 }
1166
1167 static GtkWidget *vf_file_filter_init(ViewFile *vf)
1168 {
1169         GtkWidget *frame = gtk_frame_new(NULL);
1170         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
1171         GList *work;
1172         gint n = 0;
1173         GtkWidget *combo_entry;
1174         GtkWidget *menubar;
1175         GtkWidget *menuitem;
1176         GtkWidget *case_sensitive;
1177         GtkWidget *box;
1178         GtkWidget *icon;
1179         GtkWidget *label;
1180
1181         vf->file_filter.combo = gtk_combo_box_text_new_with_entry();
1182         combo_entry = gtk_bin_get_child(GTK_BIN(vf->file_filter.combo));
1183         gtk_widget_show(gtk_bin_get_child(GTK_BIN(vf->file_filter.combo)));
1184         gtk_widget_show((GTK_WIDGET(vf->file_filter.combo)));
1185         gtk_widget_set_tooltip_text(GTK_WIDGET(vf->file_filter.combo), _("Use regular expressions"));
1186
1187         gtk_entry_set_icon_from_stock(GTK_ENTRY(combo_entry), GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
1188         gtk_entry_set_icon_tooltip_text (GTK_ENTRY(combo_entry), GTK_ENTRY_ICON_SECONDARY, _("Clear"));
1189         g_signal_connect(GTK_ENTRY(combo_entry), "icon-press", G_CALLBACK(file_filter_clear_cb), combo_entry);
1190
1191         work = history_list_get_by_key("file_filter");
1192         while (work)
1193                 {
1194                 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo), (gchar *)work->data);
1195                 work = work->next;
1196                 n++;
1197                 vf->file_filter.count = n;
1198                 }
1199         gtk_combo_box_set_active(GTK_COMBO_BOX(vf->file_filter.combo), 0);
1200
1201         g_signal_connect(G_OBJECT(combo_entry), "activate",
1202                 G_CALLBACK(vf_file_filter_save_cb), vf);
1203                 
1204         g_signal_connect(G_OBJECT(vf->file_filter.combo), "changed",
1205                 G_CALLBACK(vf_file_filter_cb), vf);
1206
1207         g_signal_connect(G_OBJECT(combo_entry), "button_press_event",
1208                          G_CALLBACK(vf_file_filter_press_cb), vf);
1209
1210         gtk_box_pack_start(GTK_BOX(hbox), vf->file_filter.combo, FALSE, FALSE, 0);
1211         gtk_widget_show(vf->file_filter.combo);
1212         gtk_container_add(GTK_CONTAINER(frame), hbox);
1213         gtk_widget_show(hbox);
1214
1215         case_sensitive = gtk_check_button_new_with_label(_("Case"));
1216         gtk_box_pack_start(GTK_BOX(hbox), case_sensitive, FALSE, FALSE, 0);
1217         gtk_widget_set_tooltip_text(GTK_WIDGET(case_sensitive), _("Case sensitive"));
1218         g_signal_connect(G_OBJECT(case_sensitive), "clicked", G_CALLBACK(case_sensitive_cb), vf);
1219         gtk_widget_show(case_sensitive);
1220
1221         menubar = gtk_menu_bar_new();
1222         gtk_box_pack_start(GTK_BOX(hbox), menubar, FALSE, TRUE, 0);
1223         gtk_widget_show(menubar);
1224
1225         box = gtk_hbox_new(GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1226         icon = gtk_image_new_from_icon_name("pan-down", GTK_ICON_SIZE_MENU);
1227         label = gtk_label_new(_("Class"));
1228
1229         gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
1230         gtk_box_pack_end(GTK_BOX(box), icon, FALSE, FALSE, 0);
1231
1232         menuitem = gtk_menu_item_new();
1233
1234         gtk_widget_set_tooltip_text(GTK_WIDGET(menuitem), _("Select Class filter"));
1235         gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), class_filter_menu(vf));
1236         gtk_menu_shell_append(GTK_MENU_SHELL(menubar), menuitem);
1237         gtk_container_add(GTK_CONTAINER(menuitem), box);
1238         gtk_widget_show_all(menuitem);
1239
1240         return frame;
1241 }
1242
1243 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
1244 {
1245         gint n = mark - 1;
1246         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
1247                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
1248 }
1249
1250 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
1251 {
1252         ViewFile *vf;
1253
1254         vf = g_new0(ViewFile, 1);
1255
1256         vf->type = type;
1257         vf->sort_method = SORT_NAME;
1258         vf->sort_ascend = TRUE;
1259         vf->read_metadata_in_idle_id = 0;
1260
1261         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
1262         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
1263         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
1264                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1265
1266         vf->filter = vf_marks_filter_init(vf);
1267         vf->file_filter.frame = vf_file_filter_init(vf);
1268
1269         vf->widget = gtk_vbox_new(FALSE, 0);
1270         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
1271         gtk_box_pack_start(GTK_BOX(vf->widget), vf->file_filter.frame, FALSE, FALSE, 0);
1272         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
1273         gtk_widget_show(vf->scrolled);
1274
1275         g_signal_connect(G_OBJECT(vf->widget), "destroy",
1276                          G_CALLBACK(vf_destroy_cb), vf);
1277
1278         switch (type)
1279         {
1280         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
1281         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
1282         }
1283
1284         vf_dnd_init(vf);
1285
1286         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
1287                          G_CALLBACK(vf_press_key_cb), vf);
1288         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
1289                          G_CALLBACK(vf_press_cb), vf);
1290         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
1291                          G_CALLBACK(vf_release_cb), vf);
1292
1293         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
1294         gtk_widget_show(vf->listview);
1295
1296         if (dir_fd) vf_set_fd(vf, dir_fd);
1297
1298         return vf;
1299 }
1300
1301 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
1302 {
1303         vf->func_status = func;
1304         vf->data_status = data;
1305 }
1306
1307 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
1308 {
1309         vf->func_thumb_status = func;
1310         vf->data_thumb_status = data;
1311 }
1312
1313 void vf_thumb_set(ViewFile *vf, gboolean enable)
1314 {
1315         switch (vf->type)
1316         {
1317         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
1318         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
1319         }
1320 }
1321
1322
1323 static gboolean vf_thumb_next(ViewFile *vf);
1324
1325 static gdouble vf_thumb_progress(ViewFile *vf)
1326 {
1327         gint count = 0;
1328         gint done = 0;
1329
1330         switch (vf->type)
1331         {
1332         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
1333         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
1334         }
1335
1336         DEBUG_1("thumb progress: %d of %d", done, count);
1337         return (gdouble)done / count;
1338 }
1339
1340 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
1341 {
1342         gint count = 0;
1343         gint done = 0;
1344
1345         switch (vf->type)
1346                 {
1347                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
1348                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
1349                 }
1350
1351         return (gdouble)done / count;
1352 }
1353
1354 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
1355 {
1356         switch (vf->type)
1357         {
1358         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
1359         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
1360         }
1361 }
1362
1363 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
1364 {
1365         if (vf->func_thumb_status)
1366                 {
1367                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
1368                 }
1369 }
1370
1371 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1372 {
1373         if (!fd) return;
1374
1375         vf_set_thumb_fd(vf, fd);
1376         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1377 }
1378
1379 void vf_thumb_cleanup(ViewFile *vf)
1380 {
1381         vf_thumb_status(vf, 0.0, NULL);
1382
1383         vf->thumbs_running = FALSE;
1384
1385         thumb_loader_free(vf->thumbs_loader);
1386         vf->thumbs_loader = NULL;
1387
1388         vf->thumbs_filedata = NULL;
1389 }
1390
1391 void vf_thumb_stop(ViewFile *vf)
1392 {
1393         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1394 }
1395
1396 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1397 {
1398         ViewFile *vf = data;
1399
1400         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1401                 {
1402                 vf_thumb_do(vf, vf->thumbs_filedata);
1403                 }
1404
1405         while (vf_thumb_next(vf));
1406 }
1407
1408 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1409 {
1410         vf_thumb_common_cb(tl, data);
1411 }
1412
1413 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1414 {
1415         vf_thumb_common_cb(tl, data);
1416 }
1417
1418 static gboolean vf_thumb_next(ViewFile *vf)
1419 {
1420         FileData *fd = NULL;
1421
1422         if (!gtk_widget_get_realized(vf->listview))
1423                 {
1424                 vf_thumb_status(vf, 0.0, NULL);
1425                 return FALSE;
1426                 }
1427
1428         switch (vf->type)
1429         {
1430         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1431         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1432         }
1433
1434         if (!fd)
1435                 {
1436                 /* done */
1437                 vf_thumb_cleanup(vf);
1438                 return FALSE;
1439                 }
1440
1441         vf->thumbs_filedata = fd;
1442
1443         thumb_loader_free(vf->thumbs_loader);
1444
1445         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1446         thumb_loader_set_callbacks(vf->thumbs_loader,
1447                                    vf_thumb_done_cb,
1448                                    vf_thumb_error_cb,
1449                                    NULL,
1450                                    vf);
1451
1452         if (!thumb_loader_start(vf->thumbs_loader, fd))
1453                 {
1454                 /* set icon to unknown, continue */
1455                 DEBUG_1("thumb loader start failed %s", fd->path);
1456                 vf_thumb_do(vf, fd);
1457
1458                 return TRUE;
1459                 }
1460
1461         return FALSE;
1462 }
1463
1464 static void vf_thumb_reset_all(ViewFile *vf)
1465 {
1466         GList *work;
1467
1468         for (work = vf->list; work; work = work->next)
1469                 {
1470                 FileData *fd = work->data;
1471                 if (fd->thumb_pixbuf)
1472                         {
1473                         g_object_unref(fd->thumb_pixbuf);
1474                         fd->thumb_pixbuf = NULL;
1475                         }
1476                 }
1477 }
1478
1479 void vf_thumb_update(ViewFile *vf)
1480 {
1481         vf_thumb_stop(vf);
1482
1483         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1484
1485         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1486         vf->thumbs_running = TRUE;
1487
1488         if (thumb_format_changed)
1489                 {
1490                 vf_thumb_reset_all(vf);
1491                 thumb_format_changed = FALSE;
1492                 }
1493
1494         while (vf_thumb_next(vf));
1495 }
1496
1497 void vf_star_cleanup(ViewFile *vf)
1498 {
1499         if (vf->stars_id != 0)
1500                 {
1501                 g_source_remove(vf->stars_id);
1502                 }
1503
1504         vf->stars_id = 0;
1505         vf->stars_filedata = NULL;
1506 }
1507
1508 void vf_star_stop(ViewFile *vf)
1509 {
1510          vf_star_cleanup(vf);
1511 }
1512
1513 static void vf_set_star_fd(ViewFile *vf, FileData *fd)
1514 {
1515         switch (vf->type)
1516                 {
1517                 case FILEVIEW_LIST: vflist_set_star_fd(vf, fd); break;
1518                 case FILEVIEW_ICON: vficon_set_star_fd(vf, fd); break;
1519                 default: break;
1520                 }
1521 }
1522
1523 static void vf_star_do(ViewFile *vf, FileData *fd)
1524 {
1525         if (!fd) return;
1526
1527         vf_set_star_fd(vf, fd);
1528 }
1529
1530 static gboolean vf_star_next(ViewFile *vf)
1531 {
1532         FileData *fd = NULL;
1533
1534         switch (vf->type)
1535                 {
1536                 case FILEVIEW_LIST: fd = vflist_star_next_fd(vf); break;
1537                 case FILEVIEW_ICON: fd = vficon_star_next_fd(vf); break;
1538                 default: break;
1539                 }
1540
1541         if (!fd)
1542                 {
1543                 /* done */
1544                 vf_star_cleanup(vf);
1545                 return FALSE;
1546                 }
1547
1548         return TRUE;
1549 }
1550
1551 gboolean vf_stars_cb(gpointer data)
1552 {
1553         ViewFile *vf = data;
1554         FileData *fd = vf->stars_filedata;
1555
1556         if (fd)
1557                 {
1558                 read_rating_data(fd);
1559
1560                 vf_star_do(vf, fd);
1561
1562                 if (vf_star_next(vf))
1563                         {
1564                         return TRUE;
1565                         }
1566                 else
1567                         {
1568                         vf->stars_filedata = NULL;
1569                         vf->stars_id = 0;
1570                         return FALSE;
1571                         }
1572                 }
1573
1574         return FALSE;
1575 }
1576
1577 void vf_star_update(ViewFile *vf)
1578 {
1579         vf_star_stop(vf);
1580
1581         if (!options->show_star_rating)
1582                 {
1583                 return;
1584                 }
1585
1586         vf_star_next(vf);
1587 }
1588
1589 void vf_marks_set(ViewFile *vf, gboolean enable)
1590 {
1591         if (vf->marks_enabled == enable) return;
1592
1593         vf->marks_enabled = enable;
1594
1595         switch (vf->type)
1596         {
1597         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1598         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1599         }
1600         if (enable)
1601                 gtk_widget_show(vf->filter);
1602         else
1603                 gtk_widget_hide(vf->filter);
1604
1605         vf_refresh_idle(vf);
1606 }
1607
1608 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1609 {
1610         if (options->show_star_rating == enable) return;
1611         options->show_star_rating = enable;
1612
1613         switch (vf->type)
1614                 {
1615                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1616                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1617                 }
1618         vf_refresh_idle(vf);
1619 }
1620
1621 guint vf_marks_get_filter(ViewFile *vf)
1622 {
1623         guint ret = 0;
1624         gint i;
1625         if (!vf->marks_enabled) return 0;
1626
1627         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1628                 {
1629                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1630                         {
1631                         ret |= 1 << i;
1632                         }
1633                 }
1634         return ret;
1635 }
1636
1637 GRegex *vf_file_filter_get_filter(ViewFile *vf)
1638 {
1639         GRegex *ret = NULL;
1640         GError *error = NULL;
1641         gchar *file_filter_text = NULL;
1642
1643         if (!gtk_widget_get_visible(vf->file_filter.combo))
1644                 {
1645                 return g_regex_new("", 0, 0, NULL);
1646                 }
1647
1648         file_filter_text = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(vf->file_filter.combo));
1649
1650         if (file_filter_text[0] != '\0')
1651                 {
1652                 ret = g_regex_new(file_filter_text, vf->file_filter.case_sensitive ? 0 : G_REGEX_CASELESS, 0, &error);
1653                 if (error)
1654                         {
1655                         log_printf("Error: could not compile regular expression %s\n%s\n", file_filter_text, error->message);
1656                         g_error_free(error);
1657                         error = NULL;
1658                         ret = g_regex_new("", 0, 0, NULL);
1659                         }
1660                 g_free(file_filter_text);
1661                 }
1662         else
1663                 {
1664                 ret = g_regex_new("", 0, 0, NULL);
1665                 }
1666
1667         return ret;
1668 }
1669
1670 guint vf_class_get_filter(ViewFile *vf)
1671 {
1672         guint ret = 0;
1673         gint i;
1674
1675         if (!gtk_widget_get_visible(vf->file_filter.combo))
1676                 {
1677                 return G_MAXUINT;
1678                 }
1679
1680         for ( i = 0; i < FILE_FORMAT_CLASSES; i++)
1681                 {
1682                 if (options->class_filter[i])
1683                         {
1684                         ret |= 1 << i;
1685                         }
1686                 }
1687
1688         return ret;
1689 }
1690
1691 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1692 {
1693         vf->layout = layout;
1694 }
1695
1696
1697 /*
1698  *-----------------------------------------------------------------------------
1699  * maintenance (for rename, move, remove)
1700  *-----------------------------------------------------------------------------
1701  */
1702
1703 static gboolean vf_refresh_idle_cb(gpointer data)
1704 {
1705         ViewFile *vf = data;
1706
1707         vf_refresh(vf);
1708         vf->refresh_idle_id = 0;
1709         return FALSE;
1710 }
1711
1712 void vf_refresh_idle_cancel(ViewFile *vf)
1713 {
1714         if (vf->refresh_idle_id)
1715                 {
1716                 g_source_remove(vf->refresh_idle_id);
1717                 vf->refresh_idle_id = 0;
1718                 }
1719 }
1720
1721
1722 void vf_refresh_idle(ViewFile *vf)
1723 {
1724         if (!vf->refresh_idle_id)
1725                 {
1726                 vf->time_refresh_set = time(NULL);
1727                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1728                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1729                 }
1730         else if (time(NULL) - vf->time_refresh_set > 1)
1731                 {
1732                 /* more than 1 sec since last update - increase priority */
1733                 vf_refresh_idle_cancel(vf);
1734                 vf->time_refresh_set = time(NULL);
1735                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1736                 }
1737 }
1738
1739 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1740 {
1741         ViewFile *vf = data;
1742         gboolean refresh;
1743
1744         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1745         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1746         /** @FIXME NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1747
1748         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1749
1750         refresh = (fd == vf->dir_fd);
1751
1752         if (!refresh)
1753                 {
1754                 gchar *base = remove_level_from_path(fd->path);
1755                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1756                 g_free(base);
1757                 }
1758
1759         if ((type & NOTIFY_CHANGE) && fd->change)
1760                 {
1761                 if (!refresh && fd->change->dest)
1762                         {
1763                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1764                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1765                         g_free(dest_base);
1766                         }
1767
1768                 if (!refresh && fd->change->source)
1769                         {
1770                         gchar *source_base = remove_level_from_path(fd->change->source);
1771                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1772                         g_free(source_base);
1773                         }
1774                 }
1775
1776         if (refresh)
1777                 {
1778                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1779                 vf_refresh_idle(vf);
1780                 }
1781 }
1782
1783 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1784 {
1785         FileData *fd;
1786         ViewFile *vf = data;
1787         GList *work;
1788
1789         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1790
1791         work = vf->list;
1792
1793         while (work)
1794                 {
1795                 fd = work->data;
1796
1797                 if (fd && !fd->metadata_in_idle_loaded)
1798                         {
1799                         if (!fd->exifdate)
1800                                 {
1801                                 read_exif_time_data(fd);
1802                                 }
1803                         if (!fd->exifdate_digitized)
1804                                 {
1805                                 read_exif_time_digitized_data(fd);
1806                                 }
1807                         if (fd->rating == STAR_RATING_NOT_READ)
1808                                 {
1809                                 read_rating_data(fd);
1810                                 }
1811                         fd->metadata_in_idle_loaded = TRUE;
1812                         return TRUE;
1813                         }
1814                 work = work->next;
1815                 }
1816
1817         vf_thumb_status(vf, 0.0, NULL);
1818         vf->read_metadata_in_idle_id = 0;
1819         vf_refresh(vf);
1820         return FALSE;
1821 }
1822
1823 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1824 {
1825         ViewFile *vf = data;
1826
1827         vf_thumb_status(vf, 0.0, "Loading meta...");
1828         vf->read_metadata_in_idle_id = 0;
1829 }
1830
1831 void vf_read_metadata_in_idle(ViewFile *vf)
1832 {
1833         if (!vf) return;
1834
1835         if (vf->read_metadata_in_idle_id)
1836                 {
1837                 g_idle_remove_by_data(vf);
1838                 }
1839         vf->read_metadata_in_idle_id = 0;
1840
1841         if (vf->list)
1842                 {
1843                 vf->read_metadata_in_idle_id = g_idle_add_full(G_PRIORITY_LOW, vf_read_metadata_in_idle_cb, vf, vf_read_metadata_in_idle_finished_cb);
1844                 }
1845
1846         return;
1847 }
1848
1849 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */