Show star rating in files pane
[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 "layout.h"
29 #include "menu.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 g_list_nth_data(vf->list, row);
74 }
75
76 gint vf_index_by_fd(ViewFile *vf, FileData *fd)
77 {
78         switch (vf->type)
79         {
80         case FILEVIEW_LIST: return vflist_index_by_fd(vf, fd);
81         case FILEVIEW_ICON: return vficon_index_by_fd(vf, fd);
82         }
83 }
84
85 guint vf_count(ViewFile *vf, gint64 *bytes)
86 {
87         if (bytes)
88                 {
89                 gint64 b = 0;
90                 GList *work;
91
92                 work = vf->list;
93                 while (work)
94                         {
95                         FileData *fd = work->data;
96                         work = work->next;
97
98                         b += fd->size;
99                         }
100
101                 *bytes = b;
102                 }
103
104         return g_list_length(vf->list);
105 }
106
107 GList *vf_get_list(ViewFile *vf)
108 {
109         GList *list = NULL;
110         GList *work;
111         for (work = vf->list; work; work = work->next)
112                 {
113                 FileData *fd = work->data;
114                 list = g_list_prepend(list, file_data_ref(fd));
115                 }
116
117         return g_list_reverse(list);
118 }
119
120 /*
121  *-------------------------------------------------------------------
122  * keyboard
123  *-------------------------------------------------------------------
124  */
125
126 static gboolean vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
127 {
128         ViewFile *vf = data;
129
130         switch (vf->type)
131         {
132         case FILEVIEW_LIST: return vflist_press_key_cb(widget, event, data);
133         case FILEVIEW_ICON: return vficon_press_key_cb(widget, event, data);
134         }
135 }
136
137 /*
138  *-------------------------------------------------------------------
139  * mouse
140  *-------------------------------------------------------------------
141  */
142
143 static gboolean vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
144 {
145         ViewFile *vf = data;
146
147         switch (vf->type)
148         {
149         case FILEVIEW_LIST: return vflist_press_cb(widget, bevent, data);
150         case FILEVIEW_ICON: return vficon_press_cb(widget, bevent, data);
151         }
152 }
153
154 static gboolean vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
155 {
156         ViewFile *vf = data;
157
158         switch (vf->type)
159         {
160         case FILEVIEW_LIST: return vflist_release_cb(widget, bevent, data);
161         case FILEVIEW_ICON: return vficon_release_cb(widget, bevent, data);
162         }
163 }
164
165
166 /*
167  *-----------------------------------------------------------------------------
168  * selections
169  *-----------------------------------------------------------------------------
170  */
171
172 gboolean vf_index_is_selected(ViewFile *vf, gint row)
173 {
174         switch (vf->type)
175         {
176         case FILEVIEW_LIST: return vflist_index_is_selected(vf, row);
177         case FILEVIEW_ICON: return vficon_index_is_selected(vf, row);
178         }
179 }
180
181
182 guint vf_selection_count(ViewFile *vf, gint64 *bytes)
183 {
184         switch (vf->type)
185         {
186         case FILEVIEW_LIST: return vflist_selection_count(vf, bytes);
187         case FILEVIEW_ICON: return vficon_selection_count(vf, bytes);
188         }
189 }
190
191 GList *vf_selection_get_list(ViewFile *vf)
192 {
193         switch (vf->type)
194         {
195         case FILEVIEW_LIST: return vflist_selection_get_list(vf);
196         case FILEVIEW_ICON: return vficon_selection_get_list(vf);
197         }
198 }
199
200 GList *vf_selection_get_list_by_index(ViewFile *vf)
201 {
202         switch (vf->type)
203         {
204         case FILEVIEW_LIST: return vflist_selection_get_list_by_index(vf);
205         case FILEVIEW_ICON: return vficon_selection_get_list_by_index(vf);
206         }
207 }
208
209 void vf_select_all(ViewFile *vf)
210 {
211         switch (vf->type)
212         {
213         case FILEVIEW_LIST: vflist_select_all(vf); break;
214         case FILEVIEW_ICON: vficon_select_all(vf); break;
215         }
216 }
217
218 void vf_select_none(ViewFile *vf)
219 {
220         switch (vf->type)
221         {
222         case FILEVIEW_LIST: vflist_select_none(vf); break;
223         case FILEVIEW_ICON: vficon_select_none(vf); break;
224         }
225 }
226
227 void vf_select_invert(ViewFile *vf)
228 {
229         switch (vf->type)
230         {
231         case FILEVIEW_LIST: vflist_select_invert(vf); break;
232         case FILEVIEW_ICON: vficon_select_invert(vf); break;
233         }
234 }
235
236 void vf_select_by_fd(ViewFile *vf, FileData *fd)
237 {
238         switch (vf->type)
239         {
240         case FILEVIEW_LIST: vflist_select_by_fd(vf, fd); break;
241         case FILEVIEW_ICON: vficon_select_by_fd(vf, fd); break;
242         }
243 }
244
245 void vf_mark_to_selection(ViewFile *vf, gint mark, MarkToSelectionMode mode)
246 {
247         switch (vf->type)
248         {
249         case FILEVIEW_LIST: vflist_mark_to_selection(vf, mark, mode); break;
250         case FILEVIEW_ICON: vficon_mark_to_selection(vf, mark, mode); break;
251         }
252 }
253
254 void vf_selection_to_mark(ViewFile *vf, gint mark, SelectionToMarkMode mode)
255 {
256         switch (vf->type)
257         {
258         case FILEVIEW_LIST: vflist_selection_to_mark(vf, mark, mode); break;
259         case FILEVIEW_ICON: vficon_selection_to_mark(vf, mark, mode); break;
260         }
261 }
262
263 /*
264  *-----------------------------------------------------------------------------
265  * dnd
266  *-----------------------------------------------------------------------------
267  */
268
269
270 static void vf_dnd_init(ViewFile *vf)
271 {
272         switch (vf->type)
273         {
274         case FILEVIEW_LIST: vflist_dnd_init(vf); break;
275         case FILEVIEW_ICON: vficon_dnd_init(vf); break;
276         }
277 }
278
279 /*
280  *-----------------------------------------------------------------------------
281  * pop-up menu
282  *-----------------------------------------------------------------------------
283  */
284
285 GList *vf_pop_menu_file_list(ViewFile *vf)
286 {
287         switch (vf->type)
288         {
289         case FILEVIEW_LIST: return vflist_pop_menu_file_list(vf);
290         case FILEVIEW_ICON: return vficon_pop_menu_file_list(vf);
291         }
292 }
293
294 GList *vf_selection_get_one(ViewFile *vf, FileData *fd)
295 {
296         switch (vf->type)
297         {
298         case FILEVIEW_LIST: return vflist_selection_get_one(vf, fd);
299         case FILEVIEW_ICON: return vficon_selection_get_one(vf, fd);
300         }
301 }
302
303 static void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
304 {
305         ViewFile *vf;
306         const gchar *key = data;
307
308         vf = submenu_item_get_data(widget);
309
310         if (!vf) return;
311
312         file_util_start_editor_from_filelist(key, vf_pop_menu_file_list(vf), vf->dir_fd->path, vf->listview);
313 }
314
315 static void vf_pop_menu_view_cb(GtkWidget *widget, gpointer data)
316 {
317         ViewFile *vf = data;
318
319         switch (vf->type)
320         {
321         case FILEVIEW_LIST: vflist_pop_menu_view_cb(widget, data); break;
322         case FILEVIEW_ICON: vficon_pop_menu_view_cb(widget, data); break;
323         }
324 }
325
326 static void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
327 {
328         ViewFile *vf = data;
329
330         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
331 }
332
333 static void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
334 {
335         ViewFile *vf = data;
336
337         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
338 }
339
340 static void vf_pop_menu_rename_cb(GtkWidget *widget, gpointer data)
341 {
342         ViewFile *vf = data;
343
344         switch (vf->type)
345         {
346         case FILEVIEW_LIST: vflist_pop_menu_rename_cb(widget, data); break;
347         case FILEVIEW_ICON: vficon_pop_menu_rename_cb(widget, data); break;
348         }
349 }
350
351 static void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
352 {
353         ViewFile *vf = data;
354
355         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
356 }
357
358 static void vf_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
359 {
360         ViewFile *vf = data;
361
362         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), TRUE);
363 }
364
365 static void vf_pop_menu_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
366 {
367         ViewFile *vf = data;
368
369         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf), FALSE);
370 }
371
372 static void vf_pop_menu_enable_grouping_cb(GtkWidget *widget, gpointer data)
373 {
374         ViewFile *vf = data;
375
376         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), FALSE);
377 }
378
379 static void vf_pop_menu_duplicates_cb(GtkWidget *widget, gpointer data)
380 {
381         ViewFile *vf = data;
382         DupeWindow *dw;
383
384         dw = dupe_window_new();
385         dupe_window_add_files(dw, vf_pop_menu_file_list(vf), FALSE);
386 }
387
388 static void vf_pop_menu_disable_grouping_cb(GtkWidget *widget, gpointer data)
389 {
390         ViewFile *vf = data;
391
392         file_data_disable_grouping_list(vf_pop_menu_file_list(vf), TRUE);
393 }
394
395 static void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
396 {
397         ViewFile *vf;
398         SortType type;
399
400         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
401
402         vf = submenu_item_get_data(widget);
403         if (!vf) return;
404
405         type = (SortType)GPOINTER_TO_INT(data);
406
407         if (type == SORT_EXIFTIME || type == SORT_EXIFTIMEDIGITIZED || type == SORT_RATING)
408                 {
409                 vf_read_metadata_in_idle(vf);
410                 }
411
412         if (vf->layout)
413                 {
414                 layout_sort_set(vf->layout, type, vf->sort_ascend);
415                 }
416         else
417                 {
418                 vf_sort_set(vf, type, vf->sort_ascend);
419                 }
420 }
421
422 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
423 {
424         ViewFile *vf = data;
425
426         if (vf->layout)
427                 {
428                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
429                 }
430         else
431                 {
432                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
433                 }
434 }
435
436 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
437 {
438         ViewFile *vf = data;
439         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
440 }
441
442 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
443 {
444         ViewFile *vf = data;
445         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
446 }
447
448 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
449 {
450         ViewFile *vf = data;
451         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
452 }
453
454 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
455 {
456         ViewFile *vf = data;
457         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
458 }
459
460 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
461 {
462         ViewFile *vf = data;
463         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
464 }
465
466 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
467 {
468         ViewFile *vf = data;
469         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
470 }
471
472 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
473 {
474         ViewFile *vf = data;
475         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
476 }
477
478 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
479 {
480         ViewFile *vf = data;
481         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
482         if (!vf->layout) return;
483
484         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
485 }
486
487 static void vf_pop_menu_toggle_star_rating(ViewFile *vf)
488 {
489         GtkAllocation allocation;
490
491         options->show_star_rating = !options->show_star_rating;
492
493         gtk_widget_get_allocation(vf->listview, &allocation);
494         vf_star_rating_set(vf, options->show_star_rating);
495 }
496
497 static void vf_pop_menu_show_star_rating_cb(GtkWidget *widget, gpointer data)
498 {
499         ViewFile *vf = data;
500
501         vf_pop_menu_toggle_star_rating(vf);
502 }
503
504 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
505 {
506         ViewFile *vf = data;
507
508         switch (vf->type)
509         {
510         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
511         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
512         }
513 }
514
515 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
516 {
517         ViewFile *vf = data;
518
519         switch (vf->type)
520         {
521         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
522         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
523         }
524
525         filelist_free(vf->editmenu_fd_list);
526         vf->editmenu_fd_list = NULL;
527 }
528
529 /**
530  * @brief Add file selection list to a collection
531  * @param[in] widget 
532  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
533  * 
534  * 
535  */
536 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
537 {
538         ViewFile *vf;
539         GList *selection_list;
540
541         vf = submenu_item_get_data(widget);
542         selection_list = vf_selection_get_list(vf);
543         pop_menu_collections(selection_list, data);
544
545         filelist_free(selection_list);
546 }
547
548 GtkWidget *vf_pop_menu(ViewFile *vf)
549 {
550         GtkWidget *menu;
551         GtkWidget *item;
552         GtkWidget *submenu;
553         gboolean active = FALSE;
554
555         switch (vf->type)
556         {
557         case FILEVIEW_LIST:
558                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
559                 active = (VFLIST(vf)->click_fd != NULL);
560                 break;
561         case FILEVIEW_ICON:
562                 active = (VFICON(vf)->click_fd != NULL);
563                 break;
564         }
565
566         menu = popup_menu_short_lived();
567
568         g_signal_connect(G_OBJECT(menu), "destroy",
569                          G_CALLBACK(vf_popup_destroy_cb), vf);
570
571         if (vf->clicked_mark > 0)
572                 {
573                 gint mark = vf->clicked_mark;
574                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
575                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
576                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
577                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
578                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
579                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
580                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
581
582                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
583
584                 vf->active_mark = mark;
585                 vf->clicked_mark = 0;
586
587                 menu_item_add_sensitive(menu, str_set_mark, active,
588                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
589
590                 menu_item_add_sensitive(menu, str_res_mark, active,
591                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
592
593                 menu_item_add_sensitive(menu, str_toggle_mark, active,
594                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
595
596                 menu_item_add_divider(menu);
597
598                 menu_item_add_sensitive(menu, str_sel_mark, active,
599                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
600                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
601                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
602                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
603                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
604                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
605                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
606
607                 menu_item_add_divider(menu);
608
609                 g_free(str_set_mark);
610                 g_free(str_res_mark);
611                 g_free(str_toggle_mark);
612                 g_free(str_sel_mark);
613                 g_free(str_sel_mark_and);
614                 g_free(str_sel_mark_or);
615                 g_free(str_sel_mark_minus);
616                 }
617
618         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
619         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
620         gtk_widget_set_sensitive(item, active);
621
622         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
623                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
624
625         menu_item_add_divider(menu);
626         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
627                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
628         menu_item_add_sensitive(menu, _("_Move..."), active,
629                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
630         menu_item_add_sensitive(menu, _("_Rename..."), active,
631                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
632         menu_item_add_sensitive(menu, _("_Copy path"), active,
633                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
634         menu_item_add_sensitive(menu, _("_Copy path unquoted"), active,
635                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
636         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
637                                       G_CALLBACK(vf_pop_menu_delete_cb), vf);
638         menu_item_add_divider(menu);
639
640         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
641                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
642         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
643                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
644
645         menu_item_add_divider(menu);
646         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
647                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
648         menu_item_add_divider(menu);
649
650         submenu = submenu_add_collections(menu, &item,
651                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
652         gtk_widget_set_sensitive(item, active);
653         menu_item_add_divider(menu);
654
655         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
656                                    FALSE, FALSE, TRUE, vf->sort_method);
657         menu_item_add_divider(submenu);
658         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
659                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
660
661         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
662         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
663
664         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
665                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
666
667         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
668                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
669
670         switch (vf->type)
671         {
672         case FILEVIEW_LIST:
673                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
674                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
675                 break;
676         case FILEVIEW_ICON:
677                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
678                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
679                 break;
680         }
681
682         switch (vf->type)
683         {
684         case FILEVIEW_LIST:
685                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
686                                     G_CALLBACK(vflist_pop_menu_show_star_rating_cb), vf);
687                 break;
688         case FILEVIEW_ICON:
689                 menu_item_add_check(menu, _("Show star rating"), options->show_star_rating,
690                                     G_CALLBACK(vficon_pop_menu_show_star_rating_cb), vf);
691                 break;
692         }
693
694         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
695
696         return menu;
697 }
698
699 gboolean vf_refresh(ViewFile *vf)
700 {
701         switch (vf->type)
702         {
703         case FILEVIEW_LIST: return vflist_refresh(vf);
704         case FILEVIEW_ICON: return vficon_refresh(vf);
705         }
706 }
707
708 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
709 {
710         switch (vf->type)
711         {
712         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
713         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
714         }
715 }
716
717 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
718 {
719         ViewFile *vf = data;
720
721         switch (vf->type)
722         {
723         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
724         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
725         }
726
727         if (vf->popup)
728                 {
729                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
730                                                      0, 0, 0, NULL, vf);
731                 gtk_widget_destroy(vf->popup);
732                 }
733
734         if (vf->read_metadata_in_idle_id)
735                 {
736                 g_idle_remove_by_data(vf);
737                 }
738         file_data_unref(vf->dir_fd);
739         g_free(vf->info);
740         g_free(vf);
741 }
742
743 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
744 {
745         ViewFile *vf = data;
746         vf_refresh_idle(vf);
747 }
748
749 typedef struct _MarksTextEntry MarksTextEntry;
750 struct _MarksTextEntry {
751         GenericDialog *gd;
752         gint mark_no;
753         GtkWidget *edit_widget;
754         gchar *text_entry;
755         GtkWidget *parent;
756 };
757
758 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
759 {
760         MarksTextEntry *mte = data;
761
762         g_free(mte->text_entry);
763         generic_dialog_close(gd);
764 }
765
766 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
767 {
768         MarksTextEntry *mte = data;
769
770         g_free(options->marks_tooltips[mte->mark_no]);
771         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
772
773         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
774
775         g_free(mte->text_entry);
776         generic_dialog_close(gd);
777 }
778
779 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
780                                                                         GdkEvent *event, gpointer userdata)
781 {
782         MarksTextEntry *mte = userdata;
783
784         g_free(mte->text_entry);
785         mte->text_entry = g_strdup("");
786         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
787 }
788
789 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
790 {
791         help_window_show("GuideImageMarks.html");
792 }
793
794 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
795                                                                                 GdkEventButton *event,
796                                                                                 gpointer user_data)
797 {
798         GtkWidget *table;
799         gint i = GPOINTER_TO_INT(user_data);
800         MarksTextEntry *mte;
801
802         if (event->button == MOUSE_BUTTON_RIGHT)
803                 {
804                 mte = g_new0(MarksTextEntry, 1);
805                 mte->mark_no = i;
806                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
807                 mte->parent = widget;
808
809                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
810                                                 widget, FALSE,
811                                                 vf_marks_tooltip_cancel_cb, mte);
812                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
813                                             _("This will set or clear the mark text."), FALSE);
814                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
815                                                         vf_marks_tooltip_ok_cb, TRUE);
816                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
817                                                 vf_marks_tooltip_help_cb, FALSE);
818
819                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
820                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
821                 mte->edit_widget = gtk_entry_new();
822                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
823                 if (mte->text_entry)
824                         {
825                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
826                         }
827                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
828                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
829
830                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
831                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
832                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
833                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
834                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
835                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
836
837                 gtk_widget_show(mte->edit_widget);
838                 gtk_widget_grab_focus(mte->edit_widget);
839                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
840
841                 return TRUE;
842                 }
843
844         return FALSE;
845 }
846
847
848 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
849 {
850         GtkWidget *frame = gtk_frame_new(NULL);
851         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
852
853         gint i;
854
855         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
856                 {
857                 GtkWidget *check = gtk_check_button_new();
858                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
859                 g_signal_connect(G_OBJECT(check), "toggled",
860                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
861                 g_signal_connect(G_OBJECT(check), "button_press_event",
862                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
863                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
864
865                 gtk_widget_show(check);
866                 vf->filter_check[i] = check;
867                 }
868         gtk_container_add(GTK_CONTAINER(frame), hbox);
869         gtk_widget_show(hbox);
870         return frame;
871 }
872
873 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
874 {
875         gint n = mark - 1;
876         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
877                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
878 }
879
880 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
881 {
882         ViewFile *vf;
883
884         vf = g_new0(ViewFile, 1);
885
886         vf->type = type;
887         vf->sort_method = SORT_NAME;
888         vf->sort_ascend = TRUE;
889         vf->read_metadata_in_idle_id = 0;
890
891         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
892         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
893         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
894                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
895
896         vf->filter = vf_marks_filter_init(vf);
897
898         vf->widget = gtk_vbox_new(FALSE, 0);
899         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
900         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
901         gtk_widget_show(vf->scrolled);
902
903         g_signal_connect(G_OBJECT(vf->widget), "destroy",
904                          G_CALLBACK(vf_destroy_cb), vf);
905
906         switch (type)
907         {
908         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
909         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
910         }
911
912         vf_dnd_init(vf);
913
914         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
915                          G_CALLBACK(vf_press_key_cb), vf);
916         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
917                          G_CALLBACK(vf_press_cb), vf);
918         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
919                          G_CALLBACK(vf_release_cb), vf);
920
921         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
922         gtk_widget_show(vf->listview);
923
924         if (dir_fd) vf_set_fd(vf, dir_fd);
925
926         return vf;
927 }
928
929 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
930 {
931         vf->func_status = func;
932         vf->data_status = data;
933 }
934
935 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
936 {
937         vf->func_thumb_status = func;
938         vf->data_thumb_status = data;
939 }
940
941 void vf_thumb_set(ViewFile *vf, gboolean enable)
942 {
943         switch (vf->type)
944         {
945         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
946         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
947         }
948 }
949
950
951 static gboolean vf_thumb_next(ViewFile *vf);
952
953 static gdouble vf_thumb_progress(ViewFile *vf)
954 {
955         gint count = 0;
956         gint done = 0;
957
958         switch (vf->type)
959         {
960         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
961         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
962         }
963
964         DEBUG_1("thumb progress: %d of %d", done, count);
965         return (gdouble)done / count;
966 }
967
968 static gdouble vf_read_metadata_in_idle_progress(ViewFile *vf)
969 {
970         gint count = 0;
971         gint done = 0;
972
973         switch (vf->type)
974                 {
975                 case FILEVIEW_LIST: vflist_read_metadata_progress_count(vf->list, &count, &done); break;
976                 case FILEVIEW_ICON: vficon_read_metadata_progress_count(vf->list, &count, &done); break;
977                 }
978
979         return (gdouble)done / count;
980 }
981
982 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
983 {
984         switch (vf->type)
985         {
986         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
987         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
988         }
989 }
990
991 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
992 {
993         if (vf->func_thumb_status)
994                 {
995                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
996                 }
997 }
998
999 static void vf_thumb_do(ViewFile *vf, FileData *fd)
1000 {
1001         if (!fd) return;
1002
1003         vf_set_thumb_fd(vf, fd);
1004         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
1005 }
1006
1007 void vf_thumb_cleanup(ViewFile *vf)
1008 {
1009         vf_thumb_status(vf, 0.0, NULL);
1010
1011         vf->thumbs_running = FALSE;
1012
1013         thumb_loader_free(vf->thumbs_loader);
1014         vf->thumbs_loader = NULL;
1015
1016         vf->thumbs_filedata = NULL;
1017 }
1018
1019 void vf_thumb_stop(ViewFile *vf)
1020 {
1021         if (vf->thumbs_running) vf_thumb_cleanup(vf);
1022 }
1023
1024 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
1025 {
1026         ViewFile *vf = data;
1027
1028         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
1029                 {
1030                 vf_thumb_do(vf, vf->thumbs_filedata);
1031                 }
1032
1033         while (vf_thumb_next(vf));
1034 }
1035
1036 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
1037 {
1038         vf_thumb_common_cb(tl, data);
1039 }
1040
1041 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
1042 {
1043         vf_thumb_common_cb(tl, data);
1044 }
1045
1046 static gboolean vf_thumb_next(ViewFile *vf)
1047 {
1048         FileData *fd = NULL;
1049
1050         if (!gtk_widget_get_realized(vf->listview))
1051                 {
1052                 vf_thumb_status(vf, 0.0, NULL);
1053                 return FALSE;
1054                 }
1055
1056         switch (vf->type)
1057         {
1058         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1059         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1060         }
1061
1062         if (!fd)
1063                 {
1064                 /* done */
1065                 vf_thumb_cleanup(vf);
1066                 return FALSE;
1067                 }
1068
1069         vf->thumbs_filedata = fd;
1070
1071         thumb_loader_free(vf->thumbs_loader);
1072
1073         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1074         thumb_loader_set_callbacks(vf->thumbs_loader,
1075                                    vf_thumb_done_cb,
1076                                    vf_thumb_error_cb,
1077                                    NULL,
1078                                    vf);
1079
1080         if (!thumb_loader_start(vf->thumbs_loader, fd))
1081                 {
1082                 /* set icon to unknown, continue */
1083                 DEBUG_1("thumb loader start failed %s", fd->path);
1084                 vf_thumb_do(vf, fd);
1085
1086                 return TRUE;
1087                 }
1088
1089         return FALSE;
1090 }
1091
1092 static void vf_thumb_reset_all(ViewFile *vf)
1093 {
1094         GList *work;
1095
1096         for (work = vf->list; work; work = work->next)
1097                 {
1098                 FileData *fd = work->data;
1099                 if (fd->thumb_pixbuf)
1100                         {
1101                         g_object_unref(fd->thumb_pixbuf);
1102                         fd->thumb_pixbuf = NULL;
1103                         }
1104                 }
1105 }
1106
1107 void vf_thumb_update(ViewFile *vf)
1108 {
1109         vf_thumb_stop(vf);
1110
1111         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1112
1113         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1114         vf->thumbs_running = TRUE;
1115
1116         if (thumb_format_changed)
1117                 {
1118                 vf_thumb_reset_all(vf);
1119                 thumb_format_changed = FALSE;
1120                 }
1121
1122         while (vf_thumb_next(vf));
1123 }
1124
1125
1126 void vf_marks_set(ViewFile *vf, gboolean enable)
1127 {
1128         if (vf->marks_enabled == enable) return;
1129
1130         vf->marks_enabled = enable;
1131
1132         switch (vf->type)
1133         {
1134         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1135         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1136         }
1137         if (enable)
1138                 gtk_widget_show(vf->filter);
1139         else
1140                 gtk_widget_hide(vf->filter);
1141
1142         vf_refresh_idle(vf);
1143 }
1144
1145 void vf_star_rating_set(ViewFile *vf, gboolean enable)
1146 {
1147         if (options->show_star_rating == enable) return;
1148         options->show_star_rating = enable;
1149
1150         switch (vf->type)
1151                 {
1152                 case FILEVIEW_LIST: vflist_star_rating_set(vf, enable); break;
1153                 case FILEVIEW_ICON: vficon_star_rating_set(vf, enable); break;
1154                 }
1155         vf_refresh_idle(vf);
1156 }
1157
1158 guint vf_marks_get_filter(ViewFile *vf)
1159 {
1160         guint ret = 0;
1161         gint i;
1162         if (!vf->marks_enabled) return 0;
1163
1164         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1165                 {
1166                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1167                         {
1168                         ret |= 1 << i;
1169                         }
1170                 }
1171         return ret;
1172 }
1173
1174 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1175 {
1176         vf->layout = layout;
1177 }
1178
1179
1180 /*
1181  *-----------------------------------------------------------------------------
1182  * maintenance (for rename, move, remove)
1183  *-----------------------------------------------------------------------------
1184  */
1185
1186 static gboolean vf_refresh_idle_cb(gpointer data)
1187 {
1188         ViewFile *vf = data;
1189
1190         vf_refresh(vf);
1191         vf->refresh_idle_id = 0;
1192         return FALSE;
1193 }
1194
1195 void vf_refresh_idle_cancel(ViewFile *vf)
1196 {
1197         if (vf->refresh_idle_id)
1198                 {
1199                 g_source_remove(vf->refresh_idle_id);
1200                 vf->refresh_idle_id = 0;
1201                 }
1202 }
1203
1204
1205 void vf_refresh_idle(ViewFile *vf)
1206 {
1207         if (!vf->refresh_idle_id)
1208                 {
1209                 vf->time_refresh_set = time(NULL);
1210                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1211                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1212                 }
1213         else if (time(NULL) - vf->time_refresh_set > 1)
1214                 {
1215                 /* more than 1 sec since last update - increase priority */
1216                 vf_refresh_idle_cancel(vf);
1217                 vf->time_refresh_set = time(NULL);
1218                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1219                 }
1220 }
1221
1222 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1223 {
1224         ViewFile *vf = data;
1225         gboolean refresh;
1226
1227         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1228         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1229         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1230
1231         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1232
1233         refresh = (fd == vf->dir_fd);
1234
1235         if (!refresh)
1236                 {
1237                 gchar *base = remove_level_from_path(fd->path);
1238                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1239                 g_free(base);
1240                 }
1241
1242         if ((type & NOTIFY_CHANGE) && fd->change)
1243                 {
1244                 if (!refresh && fd->change->dest)
1245                         {
1246                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1247                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1248                         g_free(dest_base);
1249                         }
1250
1251                 if (!refresh && fd->change->source)
1252                         {
1253                         gchar *source_base = remove_level_from_path(fd->change->source);
1254                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1255                         g_free(source_base);
1256                         }
1257                 }
1258
1259         if (refresh)
1260                 {
1261                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1262                 vf_refresh_idle(vf);
1263                 }
1264 }
1265
1266 static gboolean vf_read_metadata_in_idle_cb(gpointer data)
1267 {
1268         FileData *fd;
1269         ViewFile *vf = data;
1270         GList *list_entry;
1271         GList *work;
1272
1273         vf_thumb_status(vf, vf_read_metadata_in_idle_progress(vf), _("Loading meta..."));
1274
1275         work = vf->list;
1276
1277         while (work)
1278                 {
1279                 fd = work->data;
1280
1281                 if (fd && !fd->metadata_in_idle_loaded)
1282                         {
1283                         if (!fd->exifdate)
1284                                 {
1285                                 read_exif_time_data(fd);
1286                                 }
1287                         if (!fd->exifdate_digitized)
1288                                 {
1289                                 read_exif_time_digitized_data(fd);
1290                                 }
1291                         if (fd->rating == STAR_RATING_NOT_READ)
1292                                 {
1293                                 read_rating_data(fd);
1294                                 }
1295                         fd->metadata_in_idle_loaded = TRUE;
1296                         return TRUE;
1297                         }
1298                 work = work->next;
1299                 }
1300
1301         vf_thumb_status(vf, 0.0, NULL);
1302         vf->read_metadata_in_idle_id = 0;
1303         vf_refresh(vf);
1304         return FALSE;
1305 }
1306
1307 static void vf_read_metadata_in_idle_finished_cb(gpointer data)
1308 {
1309         ViewFile *vf = data;
1310
1311         vf_thumb_status(vf, 0.0, "Loading meta...");
1312         vf->read_metadata_in_idle_id = 0;
1313 }
1314
1315 void vf_read_metadata_in_idle(ViewFile *vf)
1316 {
1317         GList *work;
1318         FileData *fd;
1319
1320         if (!vf) return;
1321
1322         if (vf->read_metadata_in_idle_id)
1323                 {
1324                 g_idle_remove_by_data(vf);
1325                 }
1326         vf->read_metadata_in_idle_id = 0;
1327
1328         if (vf->list)
1329                 {
1330                 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);
1331                 }
1332
1333         return;
1334 }
1335
1336 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */