fb80dacac7a95bad168f7f4caa9e55899ea112e2
[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 (vf->layout)
408                 {
409                 layout_sort_set(vf->layout, type, vf->sort_ascend);
410                 }
411         else
412                 {
413                 vf_sort_set(vf, type, vf->sort_ascend);
414                 }
415 }
416
417 static void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
418 {
419         ViewFile *vf = data;
420
421         if (vf->layout)
422                 {
423                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
424                 }
425         else
426                 {
427                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
428                 }
429 }
430
431 static void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
432 {
433         ViewFile *vf = data;
434         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
435 }
436
437 static void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
438 {
439         ViewFile *vf = data;
440         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
441 }
442
443 static void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
444 {
445         ViewFile *vf = data;
446         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
447 }
448
449 static void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
450 {
451         ViewFile *vf = data;
452         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
453 }
454
455 static void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
456 {
457         ViewFile *vf = data;
458         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
459 }
460
461 static void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
462 {
463         ViewFile *vf = data;
464         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
465 }
466
467 static void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
468 {
469         ViewFile *vf = data;
470         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
471 }
472
473 static void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
474 {
475         ViewFile *vf = data;
476         FileViewType new_type = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "menu_item_radio_data"));
477         if (!vf->layout) return;
478
479         layout_views_set(vf->layout, vf->layout->options.dir_view_type, new_type);
480 }
481
482 static void vf_pop_menu_refresh_cb(GtkWidget *widget, gpointer data)
483 {
484         ViewFile *vf = data;
485
486         switch (vf->type)
487         {
488         case FILEVIEW_LIST: vflist_pop_menu_refresh_cb(widget, data); break;
489         case FILEVIEW_ICON: vficon_pop_menu_refresh_cb(widget, data); break;
490         }
491 }
492
493 static void vf_popup_destroy_cb(GtkWidget *widget, gpointer data)
494 {
495         ViewFile *vf = data;
496
497         switch (vf->type)
498         {
499         case FILEVIEW_LIST: vflist_popup_destroy_cb(widget, data); break;
500         case FILEVIEW_ICON: vficon_popup_destroy_cb(widget, data); break;
501         }
502
503         filelist_free(vf->editmenu_fd_list);
504         vf->editmenu_fd_list = NULL;
505 }
506
507 /**
508  * @brief Add file selection list to a collection
509  * @param[in] widget 
510  * @param[in] data Index to the collection list menu item selected, or -1 for new collection
511  * 
512  * 
513  */
514 static void vf_pop_menu_collections_cb(GtkWidget *widget, gpointer data)
515 {
516         ViewFile *vf;
517         GList *selection_list;
518
519         vf = submenu_item_get_data(widget);
520         selection_list = vf_selection_get_list(vf);
521         pop_menu_collections(selection_list, data);
522
523         filelist_free(selection_list);
524 }
525
526 GtkWidget *vf_pop_menu(ViewFile *vf)
527 {
528         GtkWidget *menu;
529         GtkWidget *item;
530         GtkWidget *submenu;
531         gboolean active = FALSE;
532
533         switch (vf->type)
534         {
535         case FILEVIEW_LIST:
536                 vflist_color_set(vf, VFLIST(vf)->click_fd, TRUE);
537                 active = (VFLIST(vf)->click_fd != NULL);
538                 break;
539         case FILEVIEW_ICON:
540                 active = (VFICON(vf)->click_fd != NULL);
541                 break;
542         }
543
544         menu = popup_menu_short_lived();
545
546         g_signal_connect(G_OBJECT(menu), "destroy",
547                          G_CALLBACK(vf_popup_destroy_cb), vf);
548
549         if (vf->clicked_mark > 0)
550                 {
551                 gint mark = vf->clicked_mark;
552                 gchar *str_set_mark = g_strdup_printf(_("_Set mark %d"), mark);
553                 gchar *str_res_mark = g_strdup_printf(_("_Reset mark %d"), mark);
554                 gchar *str_toggle_mark = g_strdup_printf(_("_Toggle mark %d"), mark);
555                 gchar *str_sel_mark = g_strdup_printf(_("_Select mark %d"), mark);
556                 gchar *str_sel_mark_or = g_strdup_printf(_("_Add mark %d"), mark);
557                 gchar *str_sel_mark_and = g_strdup_printf(_("_Intersection with mark %d"), mark);
558                 gchar *str_sel_mark_minus = g_strdup_printf(_("_Unselect mark %d"), mark);
559
560                 g_assert(mark >= 1 && mark <= FILEDATA_MARKS_SIZE);
561
562                 vf->active_mark = mark;
563                 vf->clicked_mark = 0;
564
565                 menu_item_add_sensitive(menu, str_set_mark, active,
566                                         G_CALLBACK(vf_pop_menu_set_mark_sel_cb), vf);
567
568                 menu_item_add_sensitive(menu, str_res_mark, active,
569                                         G_CALLBACK(vf_pop_menu_res_mark_sel_cb), vf);
570
571                 menu_item_add_sensitive(menu, str_toggle_mark, active,
572                                         G_CALLBACK(vf_pop_menu_toggle_mark_sel_cb), vf);
573
574                 menu_item_add_divider(menu);
575
576                 menu_item_add_sensitive(menu, str_sel_mark, active,
577                                         G_CALLBACK(vf_pop_menu_sel_mark_cb), vf);
578                 menu_item_add_sensitive(menu, str_sel_mark_or, active,
579                                         G_CALLBACK(vf_pop_menu_sel_mark_or_cb), vf);
580                 menu_item_add_sensitive(menu, str_sel_mark_and, active,
581                                         G_CALLBACK(vf_pop_menu_sel_mark_and_cb), vf);
582                 menu_item_add_sensitive(menu, str_sel_mark_minus, active,
583                                         G_CALLBACK(vf_pop_menu_sel_mark_minus_cb), vf);
584
585                 menu_item_add_divider(menu);
586
587                 g_free(str_set_mark);
588                 g_free(str_res_mark);
589                 g_free(str_toggle_mark);
590                 g_free(str_sel_mark);
591                 g_free(str_sel_mark_and);
592                 g_free(str_sel_mark_or);
593                 g_free(str_sel_mark_minus);
594                 }
595
596         vf->editmenu_fd_list = vf_pop_menu_file_list(vf);
597         submenu_add_edit(menu, &item, G_CALLBACK(vf_pop_menu_edit_cb), vf, vf->editmenu_fd_list);
598         gtk_widget_set_sensitive(item, active);
599
600         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, active,
601                                       G_CALLBACK(vf_pop_menu_view_cb), vf);
602
603         menu_item_add_divider(menu);
604         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, active,
605                                       G_CALLBACK(vf_pop_menu_copy_cb), vf);
606         menu_item_add_sensitive(menu, _("_Move..."), active,
607                                 G_CALLBACK(vf_pop_menu_move_cb), vf);
608         menu_item_add_sensitive(menu, _("_Rename..."), active,
609                                 G_CALLBACK(vf_pop_menu_rename_cb), vf);
610         menu_item_add_sensitive(menu, _("_Copy path"), active,
611                                 G_CALLBACK(vf_pop_menu_copy_path_cb), vf);
612         menu_item_add_sensitive(menu, _("_Copy path unquoted"), active,
613                                 G_CALLBACK(vf_pop_menu_copy_path_unquoted_cb), vf);
614         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, active,
615                                       G_CALLBACK(vf_pop_menu_delete_cb), vf);
616         menu_item_add_divider(menu);
617
618         menu_item_add_sensitive(menu, _("Enable file _grouping"), active,
619                                 G_CALLBACK(vf_pop_menu_enable_grouping_cb), vf);
620         menu_item_add_sensitive(menu, _("Disable file groupi_ng"), active,
621                                 G_CALLBACK(vf_pop_menu_disable_grouping_cb), vf);
622
623         menu_item_add_divider(menu);
624         menu_item_add_stock_sensitive(menu, _("_Find duplicates..."), GTK_STOCK_FIND, active,
625                                 G_CALLBACK(vf_pop_menu_duplicates_cb), vf);
626         menu_item_add_divider(menu);
627
628         submenu = submenu_add_collections(menu, &item,
629                                 G_CALLBACK(vf_pop_menu_collections_cb), vf);
630         gtk_widget_set_sensitive(item, active);
631         menu_item_add_divider(menu);
632
633         submenu = submenu_add_sort(NULL, G_CALLBACK(vf_pop_menu_sort_cb), vf,
634                                    FALSE, FALSE, TRUE, vf->sort_method);
635         menu_item_add_divider(submenu);
636         menu_item_add_check(submenu, _("Ascending"), vf->sort_ascend,
637                             G_CALLBACK(vf_pop_menu_sort_ascend_cb), vf);
638
639         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
640         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
641
642         item = menu_item_add_radio(menu, _("View as _List"), GINT_TO_POINTER(FILEVIEW_LIST), vf->type == FILEVIEW_LIST,
643                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
644
645         item = menu_item_add_radio(menu, _("View as _Icons"), GINT_TO_POINTER(FILEVIEW_ICON), vf->type == FILEVIEW_ICON,
646                                            G_CALLBACK(vf_pop_menu_toggle_view_type_cb), vf);
647
648         switch (vf->type)
649         {
650         case FILEVIEW_LIST:
651                 menu_item_add_check(menu, _("Show _thumbnails"), VFLIST(vf)->thumbs_enabled,
652                                     G_CALLBACK(vflist_pop_menu_thumbs_cb), vf);
653                 break;
654         case FILEVIEW_ICON:
655                 menu_item_add_check(menu, _("Show filename _text"), VFICON(vf)->show_text,
656                                     G_CALLBACK(vficon_pop_menu_show_names_cb), vf);
657                 break;
658         }
659
660         menu_item_add_stock(menu, _("Re_fresh"), GTK_STOCK_REFRESH, G_CALLBACK(vf_pop_menu_refresh_cb), vf);
661
662         return menu;
663 }
664
665 gboolean vf_refresh(ViewFile *vf)
666 {
667         switch (vf->type)
668         {
669         case FILEVIEW_LIST: return vflist_refresh(vf);
670         case FILEVIEW_ICON: return vficon_refresh(vf);
671         }
672 }
673
674 gboolean vf_set_fd(ViewFile *vf, FileData *dir_fd)
675 {
676         switch (vf->type)
677         {
678         case FILEVIEW_LIST: return vflist_set_fd(vf, dir_fd);
679         case FILEVIEW_ICON: return vficon_set_fd(vf, dir_fd);
680         }
681 }
682
683 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
684 {
685         ViewFile *vf = data;
686
687         switch (vf->type)
688         {
689         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
690         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
691         }
692
693         if (vf->popup)
694                 {
695                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
696                                                      0, 0, 0, NULL, vf);
697                 gtk_widget_destroy(vf->popup);
698                 }
699
700         file_data_unref(vf->dir_fd);
701         g_free(vf->info);
702         g_free(vf);
703 }
704
705 static void vf_marks_filter_toggle_cb(GtkWidget *widget, gpointer data)
706 {
707         ViewFile *vf = data;
708         vf_refresh_idle(vf);
709 }
710
711 typedef struct _MarksTextEntry MarksTextEntry;
712 struct _MarksTextEntry {
713         GenericDialog *gd;
714         gint mark_no;
715         GtkWidget *edit_widget;
716         gchar *text_entry;
717         GtkWidget *parent;
718 };
719
720 static void vf_marks_tooltip_cancel_cb(GenericDialog *gd, gpointer data)
721 {
722         MarksTextEntry *mte = data;
723
724         g_free(mte->text_entry);
725         generic_dialog_close(gd);
726 }
727
728 static void vf_marks_tooltip_ok_cb(GenericDialog *gd, gpointer data)
729 {
730         MarksTextEntry *mte = data;
731
732         g_free(options->marks_tooltips[mte->mark_no]);
733         options->marks_tooltips[mte->mark_no] = g_strdup(gtk_entry_get_text(GTK_ENTRY(mte->edit_widget)));
734
735         gtk_widget_set_tooltip_text(mte->parent, options->marks_tooltips[mte->mark_no]);
736
737         g_free(mte->text_entry);
738         generic_dialog_close(gd);
739 }
740
741 void vf_marks_filter_on_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
742                                                                         GdkEvent *event, gpointer userdata)
743 {
744         MarksTextEntry *mte = userdata;
745
746         g_free(mte->text_entry);
747         mte->text_entry = g_strdup("");
748         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), "");
749 }
750
751 static void vf_marks_tooltip_help_cb(GenericDialog *gd, gpointer data)
752 {
753         help_window_show("GuideImageMarks.html");
754 }
755
756 static gboolean vf_marks_tooltip_cb(GtkWidget *widget,
757                                                                                 GdkEventButton *event,
758                                                                                 gpointer user_data)
759 {
760         GtkWidget *table;
761         gint i = GPOINTER_TO_INT(user_data);
762         MarksTextEntry *mte;
763
764         if (event->button == MOUSE_BUTTON_RIGHT)
765                 {
766                 mte = g_new0(MarksTextEntry, 1);
767                 mte->mark_no = i;
768                 mte->text_entry = g_strdup(options->marks_tooltips[i]);
769                 mte->parent = widget;
770
771                 mte->gd = generic_dialog_new(_("Mark text"), "mark_text",
772                                                 widget, FALSE,
773                                                 vf_marks_tooltip_cancel_cb, mte);
774                 generic_dialog_add_message(mte->gd, GTK_STOCK_DIALOG_QUESTION, _("Set mark text"),
775                                             _("This will set or clear the mark text."), FALSE);
776                 generic_dialog_add_button(mte->gd, GTK_STOCK_OK, NULL,
777                                                         vf_marks_tooltip_ok_cb, TRUE);
778                 generic_dialog_add_button(mte->gd, GTK_STOCK_HELP, NULL,
779                                                 vf_marks_tooltip_help_cb, FALSE);
780
781                 table = pref_table_new(mte->gd->vbox, 3, 1, FALSE, TRUE);
782                 pref_table_label(table, 0, 0, g_strdup_printf("%s%d", _("Mark "), mte->mark_no + 1), 1.0);
783                 mte->edit_widget = gtk_entry_new();
784                 gtk_widget_set_size_request(mte->edit_widget, 300, -1);
785                 if (mte->text_entry)
786                         {
787                         gtk_entry_set_text(GTK_ENTRY(mte->edit_widget), mte->text_entry);
788                         }
789                 gtk_table_attach_defaults(GTK_TABLE(table), mte->edit_widget, 1, 2, 0, 1);
790                 generic_dialog_attach_default(mte->gd, mte->edit_widget);
791
792                 gtk_entry_set_icon_from_stock(GTK_ENTRY(mte->edit_widget),
793                                                         GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
794                 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(mte->edit_widget),
795                                                         GTK_ENTRY_ICON_SECONDARY, "Clear");
796                 g_signal_connect(GTK_ENTRY(mte->edit_widget), "icon-press",
797                                                         G_CALLBACK(vf_marks_filter_on_icon_press), mte);
798
799                 gtk_widget_show(mte->edit_widget);
800                 gtk_widget_grab_focus(mte->edit_widget);
801                 gtk_widget_show(GTK_WIDGET(mte->gd->dialog));
802
803                 return TRUE;
804                 }
805
806         return FALSE;
807 }
808
809
810 static GtkWidget *vf_marks_filter_init(ViewFile *vf)
811 {
812         GtkWidget *frame = gtk_frame_new(NULL);
813         GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
814
815         gint i;
816
817         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
818                 {
819                 GtkWidget *check = gtk_check_button_new();
820                 gtk_box_pack_start(GTK_BOX(hbox), check, FALSE, FALSE, 0);
821                 g_signal_connect(G_OBJECT(check), "toggled",
822                          G_CALLBACK(vf_marks_filter_toggle_cb), vf);
823                 g_signal_connect(G_OBJECT(check), "button_press_event",
824                          G_CALLBACK(vf_marks_tooltip_cb), GINT_TO_POINTER(i));
825                 gtk_widget_set_tooltip_text(check, options->marks_tooltips[i]);
826
827                 gtk_widget_show(check);
828                 vf->filter_check[i] = check;
829                 }
830         gtk_container_add(GTK_CONTAINER(frame), hbox);
831         gtk_widget_show(hbox);
832         return frame;
833 }
834
835 void vf_mark_filter_toggle(ViewFile *vf, gint mark)
836 {
837         gint n = mark - 1;
838         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vf->filter_check[n]),
839                                      !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[n])));
840 }
841
842 ViewFile *vf_new(FileViewType type, FileData *dir_fd)
843 {
844         ViewFile *vf;
845
846         vf = g_new0(ViewFile, 1);
847
848         vf->type = type;
849         vf->sort_method = SORT_NAME;
850         vf->sort_ascend = TRUE;
851
852         vf->scrolled = gtk_scrolled_window_new(NULL, NULL);
853         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->scrolled), GTK_SHADOW_IN);
854         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->scrolled),
855                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
856
857         vf->filter = vf_marks_filter_init(vf);
858
859         vf->widget = gtk_vbox_new(FALSE, 0);
860         gtk_box_pack_start(GTK_BOX(vf->widget), vf->filter, FALSE, FALSE, 0);
861         gtk_box_pack_start(GTK_BOX(vf->widget), vf->scrolled, TRUE, TRUE, 0);
862         gtk_widget_show(vf->scrolled);
863
864         g_signal_connect(G_OBJECT(vf->widget), "destroy",
865                          G_CALLBACK(vf_destroy_cb), vf);
866
867         switch (type)
868         {
869         case FILEVIEW_LIST: vf = vflist_new(vf, dir_fd); break;
870         case FILEVIEW_ICON: vf = vficon_new(vf, dir_fd); break;
871         }
872
873         vf_dnd_init(vf);
874
875         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
876                          G_CALLBACK(vf_press_key_cb), vf);
877         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
878                          G_CALLBACK(vf_press_cb), vf);
879         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
880                          G_CALLBACK(vf_release_cb), vf);
881
882         gtk_container_add(GTK_CONTAINER(vf->scrolled), vf->listview);
883         gtk_widget_show(vf->listview);
884
885         if (dir_fd) vf_set_fd(vf, dir_fd);
886
887         return vf;
888 }
889
890 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
891 {
892         vf->func_status = func;
893         vf->data_status = data;
894 }
895
896 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
897 {
898         vf->func_thumb_status = func;
899         vf->data_thumb_status = data;
900 }
901
902 void vf_thumb_set(ViewFile *vf, gboolean enable)
903 {
904         switch (vf->type)
905         {
906         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
907         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
908         }
909 }
910
911
912 static gboolean vf_thumb_next(ViewFile *vf);
913
914 static gdouble vf_thumb_progress(ViewFile *vf)
915 {
916         gint count = 0;
917         gint done = 0;
918
919         switch (vf->type)
920         {
921         case FILEVIEW_LIST: vflist_thumb_progress_count(vf->list, &count, &done); break;
922         case FILEVIEW_ICON: vficon_thumb_progress_count(vf->list, &count, &done); break;
923         }
924
925         DEBUG_1("thumb progress: %d of %d", done, count);
926         return (gdouble)done / count;
927 }
928
929 static void vf_set_thumb_fd(ViewFile *vf, FileData *fd)
930 {
931         switch (vf->type)
932         {
933         case FILEVIEW_LIST: vflist_set_thumb_fd(vf, fd); break;
934         case FILEVIEW_ICON: vficon_set_thumb_fd(vf, fd); break;
935         }
936 }
937
938 static void vf_thumb_status(ViewFile *vf, gdouble val, const gchar *text)
939 {
940         if (vf->func_thumb_status)
941                 {
942                 vf->func_thumb_status(vf, val, text, vf->data_thumb_status);
943                 }
944 }
945
946 static void vf_thumb_do(ViewFile *vf, FileData *fd)
947 {
948         if (!fd) return;
949
950         vf_set_thumb_fd(vf, fd);
951         vf_thumb_status(vf, vf_thumb_progress(vf), _("Loading thumbs..."));
952 }
953
954 void vf_thumb_cleanup(ViewFile *vf)
955 {
956         vf_thumb_status(vf, 0.0, NULL);
957
958         vf->thumbs_running = FALSE;
959
960         thumb_loader_free(vf->thumbs_loader);
961         vf->thumbs_loader = NULL;
962
963         vf->thumbs_filedata = NULL;
964 }
965
966 void vf_thumb_stop(ViewFile *vf)
967 {
968         if (vf->thumbs_running) vf_thumb_cleanup(vf);
969 }
970
971 static void vf_thumb_common_cb(ThumbLoader *tl, gpointer data)
972 {
973         ViewFile *vf = data;
974
975         if (vf->thumbs_filedata && vf->thumbs_loader == tl)
976                 {
977                 vf_thumb_do(vf, vf->thumbs_filedata);
978                 }
979
980         while (vf_thumb_next(vf));
981 }
982
983 static void vf_thumb_error_cb(ThumbLoader *tl, gpointer data)
984 {
985         vf_thumb_common_cb(tl, data);
986 }
987
988 static void vf_thumb_done_cb(ThumbLoader *tl, gpointer data)
989 {
990         vf_thumb_common_cb(tl, data);
991 }
992
993 static gboolean vf_thumb_next(ViewFile *vf)
994 {
995         FileData *fd = NULL;
996
997         if (!gtk_widget_get_realized(vf->listview))
998                 {
999                 vf_thumb_status(vf, 0.0, NULL);
1000                 return FALSE;
1001                 }
1002
1003         switch (vf->type)
1004         {
1005         case FILEVIEW_LIST: fd = vflist_thumb_next_fd(vf); break;
1006         case FILEVIEW_ICON: fd = vficon_thumb_next_fd(vf); break;
1007         }
1008
1009         if (!fd)
1010                 {
1011                 /* done */
1012                 vf_thumb_cleanup(vf);
1013                 return FALSE;
1014                 }
1015
1016         vf->thumbs_filedata = fd;
1017
1018         thumb_loader_free(vf->thumbs_loader);
1019
1020         vf->thumbs_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
1021         thumb_loader_set_callbacks(vf->thumbs_loader,
1022                                    vf_thumb_done_cb,
1023                                    vf_thumb_error_cb,
1024                                    NULL,
1025                                    vf);
1026
1027         if (!thumb_loader_start(vf->thumbs_loader, fd))
1028                 {
1029                 /* set icon to unknown, continue */
1030                 DEBUG_1("thumb loader start failed %s", fd->path);
1031                 vf_thumb_do(vf, fd);
1032
1033                 return TRUE;
1034                 }
1035
1036         return FALSE;
1037 }
1038
1039 static void vf_thumb_reset_all(ViewFile *vf)
1040 {
1041         GList *work;
1042
1043         for (work = vf->list; work; work = work->next)
1044                 {
1045                 FileData *fd = work->data;
1046                 if (fd->thumb_pixbuf)
1047                         {
1048                         g_object_unref(fd->thumb_pixbuf);
1049                         fd->thumb_pixbuf = NULL;
1050                         }
1051                 }
1052 }
1053
1054 void vf_thumb_update(ViewFile *vf)
1055 {
1056         vf_thumb_stop(vf);
1057
1058         if (vf->type == FILEVIEW_LIST && !VFLIST(vf)->thumbs_enabled) return;
1059
1060         vf_thumb_status(vf, 0.0, _("Loading thumbs..."));
1061         vf->thumbs_running = TRUE;
1062
1063         if (thumb_format_changed)
1064                 {
1065                 vf_thumb_reset_all(vf);
1066                 thumb_format_changed = FALSE;
1067                 }
1068
1069         while (vf_thumb_next(vf));
1070 }
1071
1072
1073 void vf_marks_set(ViewFile *vf, gboolean enable)
1074 {
1075         if (vf->marks_enabled == enable) return;
1076
1077         vf->marks_enabled = enable;
1078
1079         switch (vf->type)
1080         {
1081         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
1082         case FILEVIEW_ICON: vficon_marks_set(vf, enable); break;
1083         }
1084         if (enable)
1085                 gtk_widget_show(vf->filter);
1086         else
1087                 gtk_widget_hide(vf->filter);
1088
1089         vf_refresh_idle(vf);
1090 }
1091
1092 guint vf_marks_get_filter(ViewFile *vf)
1093 {
1094         guint ret = 0;
1095         gint i;
1096         if (!vf->marks_enabled) return 0;
1097
1098         for (i = 0; i < FILEDATA_MARKS_SIZE ; i++)
1099                 {
1100                 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(vf->filter_check[i])))
1101                         {
1102                         ret |= 1 << i;
1103                         }
1104                 }
1105         return ret;
1106 }
1107
1108 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
1109 {
1110         vf->layout = layout;
1111 }
1112
1113
1114 /*
1115  *-----------------------------------------------------------------------------
1116  * maintenance (for rename, move, remove)
1117  *-----------------------------------------------------------------------------
1118  */
1119
1120 static gboolean vf_refresh_idle_cb(gpointer data)
1121 {
1122         ViewFile *vf = data;
1123
1124         vf_refresh(vf);
1125         vf->refresh_idle_id = 0;
1126         return FALSE;
1127 }
1128
1129 void vf_refresh_idle_cancel(ViewFile *vf)
1130 {
1131         if (vf->refresh_idle_id)
1132                 {
1133                 g_source_remove(vf->refresh_idle_id);
1134                 vf->refresh_idle_id = 0;
1135                 }
1136 }
1137
1138
1139 void vf_refresh_idle(ViewFile *vf)
1140 {
1141         if (!vf->refresh_idle_id)
1142                 {
1143                 vf->time_refresh_set = time(NULL);
1144                 /* file operations run with G_PRIORITY_DEFAULT_IDLE */
1145                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE + 50, vf_refresh_idle_cb, vf, NULL);
1146                 }
1147         else if (time(NULL) - vf->time_refresh_set > 1)
1148                 {
1149                 /* more than 1 sec since last update - increase priority */
1150                 vf_refresh_idle_cancel(vf);
1151                 vf->time_refresh_set = time(NULL);
1152                 vf->refresh_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE - 50, vf_refresh_idle_cb, vf, NULL);
1153                 }
1154 }
1155
1156 void vf_notify_cb(FileData *fd, NotifyType type, gpointer data)
1157 {
1158         ViewFile *vf = data;
1159         gboolean refresh;
1160
1161         NotifyType interested = NOTIFY_CHANGE | NOTIFY_REREAD | NOTIFY_GROUPING;
1162         if (vf->marks_enabled) interested |= NOTIFY_MARKS | NOTIFY_METADATA;
1163         /* FIXME: NOTIFY_METADATA should be checked by the keyword-to-mark functions and converted to NOTIFY_MARKS only if there was a change */
1164
1165         if (!(type & interested) || vf->refresh_idle_id || !vf->dir_fd) return;
1166
1167         refresh = (fd == vf->dir_fd);
1168
1169         if (!refresh)
1170                 {
1171                 gchar *base = remove_level_from_path(fd->path);
1172                 refresh = (g_strcmp0(base, vf->dir_fd->path) == 0);
1173                 g_free(base);
1174                 }
1175
1176         if ((type & NOTIFY_CHANGE) && fd->change)
1177                 {
1178                 if (!refresh && fd->change->dest)
1179                         {
1180                         gchar *dest_base = remove_level_from_path(fd->change->dest);
1181                         refresh = (g_strcmp0(dest_base, vf->dir_fd->path) == 0);
1182                         g_free(dest_base);
1183                         }
1184
1185                 if (!refresh && fd->change->source)
1186                         {
1187                         gchar *source_base = remove_level_from_path(fd->change->source);
1188                         refresh = (g_strcmp0(source_base, vf->dir_fd->path) == 0);
1189                         g_free(source_base);
1190                         }
1191                 }
1192
1193         if (refresh)
1194                 {
1195                 DEBUG_1("Notify vf: %s %04x", fd->path, type);
1196                 vf_refresh_idle(vf);
1197                 }
1198 }
1199
1200 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */