Move a part of pop up menu common code to view_file.[ch].
[geeqie.git] / src / view_file.c
1 /*
2  * Geeqie
3  * Copyright (C) 2008 The Geeqie Team
4  *
5  * Author: Laurent Monin
6  *
7  * This software is released under the GNU General Public License (GNU GPL).
8  * Please read the included file COPYING for more information.
9  * This software comes with no warranty of any kind, use at your own risk!
10  */
11
12 #include "main.h"
13 #include "view_file.h"
14
15 #include "debug.h"
16 #include "editors.h"
17 #include "info.h"
18 #include "layout.h"
19 #include "menu.h"
20 #include "utilops.h"
21 #include "view_file_list.h"
22 #include "view_file_icon.h"
23
24 /*
25  *-----------------------------------------------------------------------------
26  * signals
27  *-----------------------------------------------------------------------------
28  */
29
30 void vf_send_update(ViewFile *vf)
31 {
32         if (vf->func_status) vf->func_status(vf, vf->data_status);
33 }
34
35 /*
36  *-----------------------------------------------------------------------------
37  * misc
38  *-----------------------------------------------------------------------------
39  */
40
41 void vf_sort_set(ViewFile *vf, SortType type, gint ascend)
42 {
43         switch(vf->type)
44         {
45         case FILEVIEW_LIST: vflist_sort_set(vf, type, ascend); break;
46         case FILEVIEW_ICON: vficon_sort_set(vf, type, ascend); break;
47         }
48 }
49
50 /*
51  *-----------------------------------------------------------------------------
52  * row stuff
53  *-----------------------------------------------------------------------------
54  */
55
56 FileData *vf_index_get_data(ViewFile *vf, gint row)
57 {
58         FileData *fd = NULL;
59
60         switch(vf->type)
61         {
62         case FILEVIEW_LIST: fd = vflist_index_get_data(vf, row); break;
63         case FILEVIEW_ICON: fd = vficon_index_get_data(vf, row); break;
64         }
65
66         return fd;
67 }
68
69 gint vf_index_by_path(ViewFile *vf, const gchar *path)
70 {
71         gint index = -1;
72
73         switch(vf->type)
74         {
75         case FILEVIEW_LIST: index = vflist_index_by_path(vf, path); break;
76         case FILEVIEW_ICON: index = vficon_index_by_path(vf, path); break;
77         }
78
79         return index;
80 }
81
82 gint vf_count(ViewFile *vf, gint64 *bytes)
83 {
84         gint count = 0;
85
86         switch(vf->type)
87         {
88         case FILEVIEW_LIST: count = vflist_count(vf, bytes); break;
89         case FILEVIEW_ICON: count = vficon_count(vf, bytes); break;
90         }
91
92         return count;
93 }
94
95 GList *vf_get_list(ViewFile *vf)
96 {
97         GList *list = NULL;
98
99         switch(vf->type)
100         {
101         case FILEVIEW_LIST: list = vflist_get_list(vf); break;
102         case FILEVIEW_ICON: list = vficon_get_list(vf); break;
103         }
104
105         return list;
106 }
107
108
109 /*
110  *-------------------------------------------------------------------
111  * keyboard
112  *-------------------------------------------------------------------
113  */
114
115 static gint vf_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
116 {
117         ViewFile *vf = data;
118         gint ret = FALSE;
119
120         switch(vf->type)
121         {
122         case FILEVIEW_LIST: ret = vflist_press_key_cb(widget, event, data); break;
123         case FILEVIEW_ICON: ret = vficon_press_key_cb(widget, event, data); break;
124         }
125
126         return ret;
127 }
128
129 /*
130  *-------------------------------------------------------------------
131  * mouse
132  *-------------------------------------------------------------------
133  */
134
135 static gint vf_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
136 {
137         ViewFile *vf = data;
138         gint ret = FALSE;
139
140         switch(vf->type)
141         {
142         case FILEVIEW_LIST: ret = vflist_press_cb(widget, bevent, data); break;
143         case FILEVIEW_ICON: ret = vficon_press_cb(widget, bevent, data); break;
144         }
145
146         return ret;
147 }
148
149 static gint vf_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
150 {
151         ViewFile *vf = data;
152         gint ret = FALSE;
153
154         switch(vf->type)
155         {
156         case FILEVIEW_LIST: ret = vflist_release_cb(widget, bevent, data); break;
157         case FILEVIEW_ICON: ret = vficon_release_cb(widget, bevent, data); break;
158         }
159
160         return ret;
161 }
162
163
164 /*
165  *-----------------------------------------------------------------------------
166  * selections
167  *-----------------------------------------------------------------------------
168  */
169
170 gint vf_selection_count(ViewFile *vf, gint64 *bytes)
171 {
172         gint count = 0;
173
174         switch(vf->type)
175         {
176         case FILEVIEW_LIST: count = vflist_selection_count(vf, bytes); break;
177         case FILEVIEW_ICON: count = vficon_selection_count(vf, bytes); break;
178         }
179
180         return count;
181 }
182
183 GList *vf_selection_get_list(ViewFile *vf)
184 {
185         GList *list = NULL;
186
187         switch(vf->type)
188         {
189         case FILEVIEW_LIST: list = vflist_selection_get_list(vf); break;
190         case FILEVIEW_ICON: list = vficon_selection_get_list(vf); break;
191         }
192
193         return list;
194 }
195
196 GList *vf_selection_get_list_by_index(ViewFile *vf)
197 {
198         GList *list = NULL;
199
200         switch(vf->type)
201         {
202         case FILEVIEW_LIST: list = vflist_selection_get_list_by_index(vf); break;
203         case FILEVIEW_ICON: list = vficon_selection_get_list_by_index(vf); break;
204         }
205
206         return list;
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         GList *ret = NULL;
288
289         switch(vf->type)
290         {
291         case FILEVIEW_LIST: ret = vflist_pop_menu_file_list(vf); break;
292         case FILEVIEW_ICON: ret = vficon_pop_menu_file_list(vf); break;
293         }
294
295         return ret;
296 }
297
298 void vf_pop_menu_edit_cb(GtkWidget *widget, gpointer data)
299 {
300         ViewFile *vf;
301         gint n;
302         GList *list;
303
304         vf = submenu_item_get_data(widget);
305         n = GPOINTER_TO_INT(data);
306
307         if (!vf) return;
308
309         list = vf_pop_menu_file_list(vf);
310         start_editor_from_filelist(n, list);
311         filelist_free(list);
312 }
313
314 void vf_pop_menu_info_cb(GtkWidget *widget, gpointer data)
315 {
316         ViewFile *vf = data;
317
318         info_window_new(NULL, vf_pop_menu_file_list(vf), NULL);
319 }
320
321 void vf_pop_menu_copy_cb(GtkWidget *widget, gpointer data)
322 {
323         ViewFile *vf = data;
324
325         file_util_copy(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
326 }
327
328 void vf_pop_menu_move_cb(GtkWidget *widget, gpointer data)
329 {
330         ViewFile *vf = data;
331
332         file_util_move(NULL, vf_pop_menu_file_list(vf), NULL, vf->listview);
333 }
334
335 void vf_pop_menu_delete_cb(GtkWidget *widget, gpointer data)
336 {
337         ViewFile *vf = data;
338
339         file_util_delete(NULL, vf_pop_menu_file_list(vf), vf->listview);
340 }
341
342 void vf_pop_menu_copy_path_cb(GtkWidget *widget, gpointer data)
343 {
344         ViewFile *vf = data;
345
346         file_util_copy_path_list_to_clipboard(vf_pop_menu_file_list(vf));
347 }
348
349 void vf_pop_menu_sort_cb(GtkWidget *widget, gpointer data)
350 {
351         ViewFile *vf;
352         SortType type;
353
354         if (!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) return;
355
356         vf = submenu_item_get_data(widget);
357         if (!vf) return;
358
359         type = (SortType)GPOINTER_TO_INT(data);
360
361         if (vf->layout)
362                 {
363                 layout_sort_set(vf->layout, type, vf->sort_ascend);
364                 }
365         else
366                 {
367                 vf_sort_set(vf, type, vf->sort_ascend);
368                 }
369 }
370
371 void vf_pop_menu_sort_ascend_cb(GtkWidget *widget, gpointer data)
372 {
373         ViewFile *vf = data;
374
375         if (vf->layout)
376                 {
377                 layout_sort_set(vf->layout, vf->sort_method, !vf->sort_ascend);
378                 }
379         else
380                 {
381                 vf_sort_set(vf, vf->sort_method, !vf->sort_ascend);
382                 }
383 }
384
385 void vf_pop_menu_sel_mark_cb(GtkWidget *widget, gpointer data)
386 {
387         ViewFile *vf = data;
388         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_SET);
389 }
390
391 void vf_pop_menu_sel_mark_and_cb(GtkWidget *widget, gpointer data)
392 {
393         ViewFile *vf = data;
394         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_AND);
395 }
396
397 void vf_pop_menu_sel_mark_or_cb(GtkWidget *widget, gpointer data)
398 {
399         ViewFile *vf = data;
400         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_OR);
401 }
402
403 void vf_pop_menu_sel_mark_minus_cb(GtkWidget *widget, gpointer data)
404 {
405         ViewFile *vf = data;
406         vf_mark_to_selection(vf, vf->active_mark, MTS_MODE_MINUS);
407 }
408
409 void vf_pop_menu_set_mark_sel_cb(GtkWidget *widget, gpointer data)
410 {
411         ViewFile *vf = data;
412         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_SET);
413 }
414
415 void vf_pop_menu_res_mark_sel_cb(GtkWidget *widget, gpointer data)
416 {
417         ViewFile *vf = data;
418         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_RESET);
419 }
420
421 void vf_pop_menu_toggle_mark_sel_cb(GtkWidget *widget, gpointer data)
422 {
423         ViewFile *vf = data;
424         vf_selection_to_mark(vf, vf->active_mark, STM_MODE_TOGGLE);
425 }
426
427 void vf_pop_menu_toggle_view_type_cb(GtkWidget *widget, gpointer data)
428 {
429         ViewFile *vf = data;
430         
431         if (vf->layout) layout_views_set(vf->layout, vf->layout->dir_view_type, !vf->layout->file_view_type);
432 }
433
434
435
436
437 gint vf_refresh(ViewFile *vf)
438 {
439         gint ret = FALSE;
440
441         switch(vf->type)
442         {
443         case FILEVIEW_LIST: ret = vflist_refresh(vf); break;
444         case FILEVIEW_ICON: ret = vficon_refresh(vf); break;
445         }
446
447         return ret;
448 }
449
450 gint vf_set_path(ViewFile *vf, const gchar *path)
451 {
452         gint ret = FALSE;
453
454         switch(vf->type)
455         {
456         case FILEVIEW_LIST: ret = vflist_set_path(vf, path); break;
457         case FILEVIEW_ICON: ret = vficon_set_path(vf, path); break;
458         }
459         
460         return ret;
461 }
462
463 static void vf_destroy_cb(GtkWidget *widget, gpointer data)
464 {
465         ViewFile *vf = data;
466
467         switch(vf->type)
468         {
469         case FILEVIEW_LIST: vflist_destroy_cb(widget, data); break;
470         case FILEVIEW_ICON: vficon_destroy_cb(widget, data); break;
471         }
472
473         if (vf->popup)
474                 {
475                 g_signal_handlers_disconnect_matched(G_OBJECT(vf->popup), G_SIGNAL_MATCH_DATA,
476                                                      0, 0, 0, NULL, vf);
477                 gtk_widget_destroy(vf->popup);
478                 }
479
480         g_free(vf->path);
481         g_free(vf->info);
482         g_free(vf);
483 }
484
485 ViewFile *vf_new(FileViewType type, const gchar *path)
486 {
487         ViewFile *vf;
488
489         vf = g_new0(ViewFile, 1);
490         vf->type = type;
491
492         vf->info = NULL;
493         vf->path = NULL;
494         vf->list = NULL;
495
496         vf->sort_method = SORT_NAME;
497         vf->sort_ascend = TRUE;
498         
499         vf->thumbs_running = FALSE;
500         vf->thumbs_count = 0;
501         vf->thumbs_loader = NULL;
502         vf->thumbs_filedata = NULL;
503
504         vf->popup = NULL;
505
506         vf->widget = gtk_scrolled_window_new(NULL, NULL);
507         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(vf->widget), GTK_SHADOW_IN);
508         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vf->widget),
509                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
510         
511         g_signal_connect(G_OBJECT(vf->widget), "destroy",
512                          G_CALLBACK(vf_destroy_cb), vf);
513
514         switch(type)
515         {
516         case FILEVIEW_LIST: vf = vflist_new(vf, path); break;
517         case FILEVIEW_ICON: vf = vficon_new(vf, path); break;
518         }
519
520         vf_dnd_init(vf);
521
522         g_signal_connect(G_OBJECT(vf->listview), "key_press_event",
523                          G_CALLBACK(vf_press_key_cb), vf);
524         g_signal_connect(G_OBJECT(vf->listview), "button_press_event",
525                          G_CALLBACK(vf_press_cb), vf);
526         g_signal_connect(G_OBJECT(vf->listview), "button_release_event",
527                          G_CALLBACK(vf_release_cb), vf);
528
529         gtk_container_add(GTK_CONTAINER(vf->widget), vf->listview);
530         gtk_widget_show(vf->listview);
531
532         if (path) vf_set_path(vf, path);
533
534         return vf;
535 }
536
537 void vf_set_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gpointer data), gpointer data)
538 {
539         vf->func_status = func;
540         vf->data_status = data;
541 }
542
543 void vf_set_thumb_status_func(ViewFile *vf, void (*func)(ViewFile *vf, gdouble val, const gchar *text, gpointer data), gpointer data)
544 {
545         vf->func_thumb_status = func;
546         vf->data_thumb_status = data;
547 }
548
549 void vf_thumb_set(ViewFile *vf, gint enable)
550 {
551         switch(vf->type)
552         {
553         case FILEVIEW_LIST: vflist_thumb_set(vf, enable); break;
554         case FILEVIEW_ICON: /*vficon_thumb_set(vf, enable);*/ break;
555         }
556 }
557
558 void vf_marks_set(ViewFile *vf, gint enable)
559 {
560         switch(vf->type)
561         {
562         case FILEVIEW_LIST: vflist_marks_set(vf, enable); break;
563         case FILEVIEW_ICON: /*vficon_marks_set(vf, enable);*/ break;
564         }
565 }
566
567 void vf_set_layout(ViewFile *vf, LayoutWindow *layout)
568 {
569         vf->layout = layout;
570 }
571
572 /*
573  *-----------------------------------------------------------------------------
574  * maintenance (for rename, move, remove)
575  *-----------------------------------------------------------------------------
576  */
577
578 gint vf_maint_renamed(ViewFile *vf, FileData *fd)
579 {
580         gint ret = FALSE;
581
582         switch(vf->type)
583         {
584         case FILEVIEW_LIST: ret = vflist_maint_renamed(vf, fd); break;
585         case FILEVIEW_ICON: ret = vficon_maint_renamed(vf, fd); break;
586         }
587
588         return ret;
589 }
590
591 gint vf_maint_removed(ViewFile *vf, FileData *fd, GList *ignore_list)
592 {
593         gint ret = FALSE;
594
595         switch(vf->type)
596         {
597         case FILEVIEW_LIST: ret = vflist_maint_removed(vf, fd, ignore_list); break;
598         case FILEVIEW_ICON: ret = vficon_maint_removed(vf, fd, ignore_list); break;
599         }
600
601         return ret;
602 }
603
604 gint vf_maint_moved(ViewFile *vf, FileData *fd, GList *ignore_list)
605 {
606         gint ret = FALSE;
607
608         switch(vf->type)
609         {
610         case FILEVIEW_LIST: ret = vflist_maint_moved(vf, fd, ignore_list); break;
611         case FILEVIEW_ICON: ret = vficon_maint_moved(vf, fd, ignore_list); break;
612         }
613
614         return ret;
615 }