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