Bug fix #160: Replace print dialog by standard GTK dialog
[geeqie.git] / src / collect-table.c
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "collect-table.h"
24
25 #include "cellrenderericon.h"
26 #include "collect-dlg.h"
27 #include "collect-io.h"
28 #include "dnd.h"
29 #include "dupe.h"
30 #include "editors.h"
31 #include "filedata.h"
32 #include "img-view.h"
33 #include "layout.h"
34 #include "layout_image.h"
35 #include "menu.h"
36 #include "metadata.h"
37 #include "print.h"
38 #include "utilops.h"
39 #include "ui_fileops.h"
40 #include "ui_menu.h"
41 #include "ui_tree_edit.h"
42 #include "uri_utils.h"
43 #include "view_file.h"
44
45 #include "icons/marker.xpm"
46 #define MARKER_WIDTH 26
47 #define MARKER_HEIGHT 32
48
49 #include <gdk/gdkkeysyms.h> /* for keyboard values */
50
51 /* between these, the icon width is increased by thumb_max_width / 2 */
52 #define THUMB_MIN_ICON_WIDTH 128
53 #define THUMB_MAX_ICON_WIDTH 150
54
55 #define COLLECT_TABLE_MAX_COLUMNS 32
56 #define THUMB_BORDER_PADDING 2
57
58 #define COLLECT_TABLE_TIP_DELAY 500
59 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
60
61
62 enum {
63         CTABLE_COLUMN_POINTER = 0,
64         CTABLE_COLUMN_COUNT
65 };
66
67 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
68
69
70 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
71
72
73 /*
74  *-------------------------------------------------------------------
75  * more misc
76  *-------------------------------------------------------------------
77  */
78
79 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
80 {
81         gint n;
82
83         n = g_list_index(ct->cd->list, info);
84
85         if (n < 0) return FALSE;
86
87         *row = n / ct->columns;
88         *col = n - (*row * ct->columns);
89
90         return TRUE;
91 }
92
93 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
94 {
95         GtkTreeModel *store;
96         gint row, col;
97
98         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
99         if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
100         if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
101         if (column) *column = col;
102
103         return TRUE;
104 }
105
106 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
107 {
108         GtkTreeModel *store;
109         GtkTreeIter p;
110
111         if (row < 0 || col < 0) return NULL;
112
113         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
114         if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
115                 {
116                 GList *list;
117
118                 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
119                 if (!list) return NULL;
120
121                 if (iter) *iter = p;
122
123                 return g_list_nth_data(list, col);
124                 }
125
126         return NULL;
127 }
128
129 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
130 {
131         GtkTreePath *tpath;
132         GtkTreeViewColumn *column;
133         GtkTreeModel *store;
134         GtkTreeIter row;
135         GList *list;
136         gint n;
137
138         if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
139                                            &tpath, &column, NULL, NULL))
140                 return NULL;
141
142         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
143         gtk_tree_model_get_iter(store, &row, tpath);
144         gtk_tree_path_free(tpath);
145
146         gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
147         if (!list) return NULL;
148
149         n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
150         if (iter) *iter = row;
151         return g_list_nth_data(list, n);
152 }
153
154 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
155 {
156         if (bytes)
157                 {
158                 gint64 b = 0;
159                 GList *work;
160
161                 work = ct->cd->list;
162                 while (work)
163                         {
164                         CollectInfo *ci = work->data;
165                         work = work->next;
166                         b += ci->fd->size;
167                         }
168
169                 *bytes = b;
170                 }
171
172         return g_list_length(ct->cd->list);
173 }
174
175 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
176 {
177         if (bytes)
178                 {
179                 gint64 b = 0;
180                 GList *work;
181
182                 work = ct->selection;
183                 while (work)
184                         {
185                         CollectInfo *ci = work->data;
186                         work = work->next;
187                         b += ci->fd->size;
188                         }
189
190                 *bytes = b;
191                 }
192
193         return g_list_length(ct->selection);
194 }
195
196 static void collection_table_update_status(CollectTable *ct)
197 {
198         gchar *buf;
199         guint n;
200         gint64 n_bytes = 0;
201         guint s;
202         gint64 s_bytes = 0;
203
204         if (!ct->status_label) return;
205
206         n = collection_table_list_count(ct, &n_bytes);
207         s = collection_table_selection_count(ct, &s_bytes);
208
209         if (s > 0)
210                 {
211                 gchar *b = text_from_size_abrev(n_bytes);
212                 gchar *sb = text_from_size_abrev(s_bytes);
213                 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
214                 g_free(b);
215                 g_free(sb);
216                 }
217         else if (n > 0)
218                 {
219                 gchar *b = text_from_size_abrev(n_bytes);
220                 buf = g_strdup_printf(_("%s, %d images"), b, n);
221                 g_free(b);
222                 }
223         else
224                 {
225                 buf = g_strdup(_("Empty"));
226                 }
227
228         gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
229         g_free(buf);
230 }
231
232 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
233 {
234         gchar *text;
235
236         if (!ct->extra_label) return;
237
238         if (loading)
239                 text = _("Loading thumbs...");
240         else
241                 text = " ";
242
243         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
244         gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
245 }
246
247 static void collection_table_toggle_filenames(CollectTable *ct)
248 {
249         GtkAllocation allocation;
250         ct->show_text = !ct->show_text;
251         options->show_icon_names = ct->show_text;
252
253         gtk_widget_get_allocation(ct->listview, &allocation);
254         collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
255 }
256
257 static void collection_table_toggle_stars(CollectTable *ct)
258 {
259         GtkAllocation allocation;
260         ct->show_stars = !ct->show_stars;
261         options->show_star_rating = ct->show_stars;
262
263         gtk_widget_get_allocation(ct->listview, &allocation);
264         collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
265 }
266
267 static gint collection_table_get_icon_width(CollectTable *ct)
268 {
269         gint width;
270
271         if (!ct->show_text) return options->thumbnails.max_width;
272
273         width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
274         if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
275         if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
276
277         return width;
278 }
279
280 /*
281  *-------------------------------------------------------------------
282  * cell updates
283  *-------------------------------------------------------------------
284  */
285
286 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
287 {
288         GtkTreeModel *store;
289         GList *list;
290
291         if (!info) return;
292
293         if (info->flag_mask == value) return;
294         info->flag_mask = value;
295
296         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
297         if (iter)
298                 {
299                 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
300                 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
301                 }
302         else
303                 {
304                 GtkTreeIter row;
305
306                 if (collection_table_find_iter(ct, info, &row, NULL))
307                         {
308                         gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
309                         if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
310                         }
311                 }
312 }
313
314 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
315 {
316         if (!info) return;
317
318         collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
319 }
320
321 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
322 {
323         if (!info) return;
324
325         collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
326 }
327 /*
328  *-------------------------------------------------------------------
329  * selections
330  *-------------------------------------------------------------------
331  */
332
333 static void collection_table_verify_selections(CollectTable *ct)
334 {
335         GList *work;
336
337         work = ct->selection;
338         while (work)
339                 {
340                 CollectInfo *info = work->data;
341                 work = work->next;
342                 if (!g_list_find(ct->cd->list, info))
343                         {
344                         ct->selection = g_list_remove(ct->selection, info);
345                         }
346                 }
347 }
348
349 void collection_table_select_all(CollectTable *ct)
350 {
351         GList *work;
352
353         g_list_free(ct->selection);
354         ct->selection = NULL;
355
356         work = ct->cd->list;
357         while (work)
358                 {
359                 ct->selection = g_list_append(ct->selection, work->data);
360                 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
361                 work = work->next;
362                 }
363
364         collection_table_update_status(ct);
365 }
366
367 void collection_table_unselect_all(CollectTable *ct)
368 {
369         GList *work;
370
371         work = ct->selection;
372         while (work)
373                 {
374                 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
375                 work = work->next;
376                 }
377
378         g_list_free(ct->selection);
379         ct->selection = NULL;
380
381         collection_table_update_status(ct);
382 }
383
384 /* Invert the current collection's selection */
385 static void collection_table_select_invert_all(CollectTable *ct)
386 {
387         GList *work;
388         GList *new_selection = NULL;
389
390         work = ct->cd->list;
391         while (work)
392                 {
393                 CollectInfo *info = work->data;
394
395                 if (INFO_SELECTED(info))
396                         {
397                         collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
398                         }
399                 else
400                         {
401                         new_selection = g_list_append(new_selection, info);
402                         collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
403
404                         }
405
406                 work = work->next;
407                 }
408
409         g_list_free(ct->selection);
410         ct->selection = new_selection;
411
412         collection_table_update_status(ct);
413 }
414
415 void collection_table_select(CollectTable *ct, CollectInfo *info)
416 {
417         ct->prev_selection = info;
418
419         if (!info || INFO_SELECTED(info)) return;
420
421         ct->selection = g_list_append(ct->selection, info);
422         collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
423
424         collection_table_update_status(ct);
425 }
426
427 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
428 {
429         ct->prev_selection = info;
430
431         if (!info || !INFO_SELECTED(info) ) return;
432
433         ct->selection = g_list_remove(ct->selection, info);
434         collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
435
436         collection_table_update_status(ct);
437 }
438
439 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
440 {
441         if (select)
442                 {
443                 collection_table_select(ct, info);
444                 }
445         else
446                 {
447                 collection_table_unselect(ct, info);
448                 }
449 }
450
451 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
452 {
453         gint row1, col1;
454         gint row2, col2;
455         gint t;
456         gint i, j;
457
458         if (!collection_table_find_position(ct, start, &row1, &col1) ||
459             !collection_table_find_position(ct, end, &row2, &col2) ) return;
460
461         ct->prev_selection = end;
462
463         if (!options->collections.rectangular_selection)
464                 {
465                 GList *work;
466                 CollectInfo *info;
467
468                 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
469                         {
470                         info = start;
471                         start = end;
472                         end = info;
473                         }
474
475                 work = g_list_find(ct->cd->list, start);
476                 while (work)
477                         {
478                         info = work->data;
479                         collection_table_select_util(ct, info, select);
480
481                         if (work->data != end)
482                                 work = work->next;
483                         else
484                                 work = NULL;
485                         }
486                 return;
487                 }
488
489         if (row2 < row1)
490                 {
491                 t = row1;
492                 row1 = row2;
493                 row2 = t;
494                 }
495         if (col2 < col1)
496                 {
497                 t = col1;
498                 col1 = col2;
499                 col2 = t;
500                 }
501
502         DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
503
504         for (i = row1; i <= row2; i++)
505                 {
506                 for (j = col1; j <= col2; j++)
507                         {
508                         CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
509                         if (info) collection_table_select_util(ct, info, select);
510                         }
511                 }
512 }
513
514 GList *collection_table_selection_get_list(CollectTable *ct)
515 {
516         return collection_list_to_filelist(ct->selection);
517 }
518
519 static GList *collection_table_get_list(CollectTable *ct)
520 {
521         return collection_list_to_filelist(ct->cd->list);
522 }
523
524 /*
525  *-------------------------------------------------------------------
526  * tooltip type window
527  *-------------------------------------------------------------------
528  */
529
530 static void tip_show(CollectTable *ct)
531 {
532         GtkWidget *label;
533         gint x, y;
534 #if GTK_CHECK_VERSION(3,0,0)
535         GdkDisplay *display;
536         GdkDeviceManager *device_manager;
537         GdkDevice *device;
538 #endif
539
540         if (ct->tip_window) return;
541
542 #if GTK_CHECK_VERSION(3,0,0)
543         device_manager = gdk_display_get_device_manager(gdk_window_get_display(
544                                                                 gtk_widget_get_window(ct->listview)));
545         device = gdk_device_manager_get_client_pointer(device_manager);
546         gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
547                                                                 device, &x, &y, NULL);
548 #else
549         gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
550 #endif
551
552         ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
553         if (!ct->tip_info) return;
554
555         ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
556         gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
557         gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
558
559         label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
560
561         g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
562         gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
563         gtk_widget_show(label);
564
565 #if GTK_CHECK_VERSION(3,0,0)
566         display = gdk_display_get_default();
567         device_manager = gdk_display_get_device_manager(display);
568         device = gdk_device_manager_get_client_pointer(device_manager);
569         gdk_device_get_position(device, NULL, &x, &y);
570 #else
571         gdk_window_get_pointer(NULL, &x, &y, NULL);
572 #endif
573
574         if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
575         gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
576         gtk_widget_show(ct->tip_window);
577 }
578
579 static void tip_hide(CollectTable *ct)
580 {
581         if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
582         ct->tip_window = NULL;
583 }
584
585 static gboolean tip_schedule_cb(gpointer data)
586 {
587         CollectTable *ct = data;
588
589         if (!ct->tip_delay_id) return FALSE;
590
591         tip_show(ct);
592
593         ct->tip_delay_id = 0;
594         return FALSE;
595 }
596
597 static void tip_schedule(CollectTable *ct)
598 {
599         tip_hide(ct);
600
601         if (ct->tip_delay_id)
602                 {
603                 g_source_remove(ct->tip_delay_id);
604                 ct->tip_delay_id = 0;
605                 }
606
607         ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
608 }
609
610 static void tip_unschedule(CollectTable *ct)
611 {
612         tip_hide(ct);
613
614         if (ct->tip_delay_id)
615                 {
616                 g_source_remove(ct->tip_delay_id);
617                 ct->tip_delay_id = 0;
618                 }
619 }
620
621 static void tip_update(CollectTable *ct, CollectInfo *info)
622 {
623 #if GTK_CHECK_VERSION(3,0,0)
624         GdkDisplay *display = gdk_display_get_default();
625         GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
626         GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
627 #endif
628
629         tip_schedule(ct);
630
631         if (ct->tip_window)
632                 {
633                 gint x, y;
634 #if GTK_CHECK_VERSION(3,0,0)
635                 gdk_device_get_position(device, NULL, &x, &y);
636 #else
637                 gdk_window_get_pointer(NULL, &x, &y, NULL);
638 #endif
639                 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
640
641                 if (info != ct->tip_info)
642                         {
643                         GtkWidget *label;
644
645                         ct->tip_info = info;
646
647                         if (!ct->tip_info)
648                                 {
649                                 return;
650                                 }
651
652                         label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
653                         gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
654                         }
655                 }
656 }
657
658 /*
659  *-------------------------------------------------------------------
660  * popup menus
661  *-------------------------------------------------------------------
662  */
663
664 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
665 {
666         CollectTable *ct = data;
667
668         collection_dialog_save_as(NULL, ct->cd);
669 }
670
671 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
672 {
673         CollectTable *ct = data;
674
675         if (!ct->cd->path)
676                 {
677                 collection_table_popup_save_as_cb(widget, data);
678                 return;
679                 }
680
681         if (!collection_save(ct->cd, ct->cd->path))
682                 {
683                 log_printf("failed saving to collection path: %s\n", ct->cd->path);
684                 }
685 }
686
687 static GList *collection_table_popup_file_list(CollectTable *ct)
688 {
689         if (!ct->click_info) return NULL;
690
691         if (INFO_SELECTED(ct->click_info))
692                 {
693                 return collection_table_selection_get_list(ct);
694                 }
695
696         return g_list_append(NULL, file_data_ref(ct->click_info->fd));
697 }
698
699 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
700 {
701         CollectTable *ct;
702         const gchar *key = data;
703
704         ct = submenu_item_get_data(widget);
705
706         if (!ct) return;
707
708         file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
709 }
710
711 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
712 {
713         CollectTable *ct = data;
714
715         file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
716 }
717
718 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
719 {
720         CollectTable *ct = data;
721
722         file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
723 }
724
725 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
726 {
727         CollectTable *ct = data;
728
729         file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
730 }
731
732 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
733 {
734         CollectTable *ct = data;
735
736         file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
737 }
738
739 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
740 {
741         CollectTable *ct = data;
742
743         file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), TRUE);
744 }
745
746 static void collection_table_popup_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
747 {
748         CollectTable *ct = data;
749
750         file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), FALSE);
751 }
752
753 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
754 {
755         CollectTable *ct;
756         SortType type;
757
758         ct = submenu_item_get_data(widget);
759
760         if (!ct) return;
761
762         type = (SortType)GPOINTER_TO_INT(data);
763
764         collection_set_sort_method(ct->cd, type);
765 }
766
767 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
768 {
769         CollectTable *ct;
770
771         ct = submenu_item_get_data(widget);
772
773         if (!ct) return;
774
775         collection_randomize(ct->cd);
776 }
777
778 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
779 {
780         CollectTable *ct = data;
781
782         if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
783                 {
784                 view_window_new_from_collection(ct->cd, ct->click_info);
785                 }
786 }
787
788 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
789 {
790         CollectTable *ct = data;
791
792         if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
793                 {
794                 layout_image_set_collection(NULL, ct->cd, ct->click_info);
795                 }
796 }
797
798 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
799 {
800         CollectTable *ct = data;
801
802         collection_table_select_all(ct);
803         ct->prev_selection= ct->click_info;
804 }
805
806 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
807 {
808         CollectTable *ct = data;
809
810         collection_table_unselect_all(ct);
811         ct->prev_selection= ct->click_info;
812 }
813
814 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
815 {
816         CollectTable *ct = data;
817
818         collection_table_select_invert_all(ct);
819         ct->prev_selection= ct->click_info;
820 }
821
822 static void collection_table_popup_rectangular_selection_cb(GtkWidget *widget, gpointer data)
823 {
824         options->collections.rectangular_selection = !(options->collections.rectangular_selection);
825 }
826
827 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
828 {
829         CollectTable *ct = data;
830         GList *list;
831
832         if (!ct->click_info) return;
833
834         if (INFO_SELECTED(ct->click_info))
835                 {
836                 list = g_list_copy(ct->selection);
837                 }
838         else
839                 {
840                 list = g_list_append(NULL, ct->click_info);
841                 }
842
843         collection_remove_by_info_list(ct->cd, list);
844         g_list_free(list);
845 }
846
847 static void collection_table_popup_add_file_selection_cb(GtkWidget *widget, gpointer data)
848 {
849         CollectTable *ct = data;
850         GList *list;
851         LayoutWindow *lw = NULL;
852
853         if (!layout_valid(&lw)) return;
854
855         list = vf_selection_get_list(lw->vf);
856
857         if (list)
858                 {
859                 collection_table_add_filelist(ct, list);
860                 filelist_free(list);
861                 }
862 }
863
864 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
865 {
866         CollectTable *ct = data;
867
868         collection_dialog_append(NULL, ct->cd);
869 }
870
871 static void collection_table_popup_goto_original_cb(GtkWidget *widget, gpointer data)
872 {
873         CollectTable *ct = data;
874         GList *list;
875         LayoutWindow *lw = NULL;
876         FileData *fd;
877
878         if (!layout_valid(&lw)) return;
879         list = collection_table_selection_get_list(ct);
880         if (list)
881                 {
882                 fd = list->data;
883                 if (fd)
884                         {
885                         layout_set_fd(lw, fd);
886                         }
887                 }
888         g_list_free(list);
889 }
890
891 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
892 {
893         CollectTable *ct = data;
894         DupeWindow *dw;
895
896         dw = dupe_window_new();
897         dupe_window_add_collection(dw, ct->cd);
898 }
899
900 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
901 {
902         CollectTable *ct = data;
903         FileData *fd;
904
905         fd = (ct->click_info) ? ct->click_info->fd : NULL;
906
907         print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), gtk_widget_get_toplevel(ct->listview));
908 }
909
910 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
911 {
912         CollectTable *ct = data;
913
914         collection_table_toggle_filenames(ct);
915 }
916
917 static void collection_table_popup_show_stars_cb(GtkWidget *widget, gpointer data)
918 {
919         CollectTable *ct = data;
920
921         collection_table_toggle_stars(ct);
922 }
923
924 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
925 {
926         CollectTable *ct = data;
927
928         collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
929         ct->click_info = NULL;
930         ct->popup = NULL;
931
932         filelist_free(ct->drop_list);
933         ct->drop_list = NULL;
934         ct->drop_info = NULL;
935
936         filelist_free(ct->editmenu_fd_list);
937         ct->editmenu_fd_list = NULL;
938 }
939
940 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
941 {
942         GtkWidget *menu;
943         GtkWidget *item;
944         GtkWidget *submenu;
945
946         menu = popup_menu_short_lived();
947
948         g_signal_connect(G_OBJECT(menu), "destroy",
949                          G_CALLBACK(collection_table_popup_destroy_cb), ct);
950
951         menu_item_add_sensitive(menu, _("_View"), over_icon,
952                         G_CALLBACK(collection_table_popup_view_cb), ct);
953         menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
954                         G_CALLBACK(collection_table_popup_view_new_cb), ct);
955         menu_item_add_stock(menu, _("Go to original"), GTK_STOCK_FIND,
956                         G_CALLBACK(collection_table_popup_goto_original_cb), ct);
957         menu_item_add_divider(menu);
958         menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
959                         G_CALLBACK(collection_table_popup_remove_cb), ct);
960
961         menu_item_add_stock(menu, _("Append from file selection"), GTK_STOCK_ADD,
962                         G_CALLBACK(collection_table_popup_add_file_selection_cb), ct);
963         menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
964                         G_CALLBACK(collection_table_popup_add_collection_cb), ct);
965         menu_item_add_divider(menu);
966
967         item = menu_item_add(menu, _("_Selection"), NULL, NULL);
968         submenu = gtk_menu_new();
969         menu_item_add(submenu, _("Select all"),
970                         G_CALLBACK(collection_table_popup_selectall_cb), ct);
971         menu_item_add(submenu, _("Select none"),
972                         G_CALLBACK(collection_table_popup_unselectall_cb), ct);
973         menu_item_add(submenu, _("Invert selection"),
974                         G_CALLBACK(collection_table_popup_select_invert_cb), ct);
975         menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
976                         G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
977         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
978         menu_item_add_divider(menu);
979
980
981         ct->editmenu_fd_list = collection_table_selection_get_list(ct);
982         submenu_add_edit(menu, &item,
983                         G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
984         gtk_widget_set_sensitive(item, over_icon);
985
986         menu_item_add_divider(menu);
987         menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
988                         G_CALLBACK(collection_table_popup_copy_cb), ct);
989         menu_item_add_sensitive(menu, _("_Move..."), over_icon,
990                         G_CALLBACK(collection_table_popup_move_cb), ct);
991         menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
992                         G_CALLBACK(collection_table_popup_rename_cb), ct);
993         menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
994                                 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
995         menu_item_add_sensitive(menu, _("_Copy path unquoted"), over_icon,
996                                 G_CALLBACK(collection_table_popup_copy_path_unquoted_cb), ct);
997         menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
998                         G_CALLBACK(collection_table_popup_delete_cb), ct);
999         menu_item_add_divider(menu);
1000
1001         submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
1002         menu_item_add_divider(submenu);
1003         menu_item_add(submenu, _("Randomize"),
1004                         G_CALLBACK(collection_table_popup_randomize_cb), ct);
1005         item = menu_item_add(menu, _("_Sort"), NULL, NULL);
1006         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1007
1008         menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
1009                         G_CALLBACK(collection_table_popup_show_names_cb), ct);
1010         menu_item_add_check(menu, _("Show star rating"), ct->show_stars,
1011                                 G_CALLBACK(collection_table_popup_show_stars_cb), ct);
1012         menu_item_add_divider(menu);
1013         menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
1014                         G_CALLBACK(collection_table_popup_save_cb), ct);
1015         menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
1016                         G_CALLBACK(collection_table_popup_save_as_cb), ct);
1017         menu_item_add_divider(menu);
1018         menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
1019                         G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
1020         menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
1021                         G_CALLBACK(collection_table_popup_print_cb), ct);
1022
1023         return menu;
1024 }
1025 /*
1026  *-------------------------------------------------------------------
1027  * keyboard callbacks
1028  *-------------------------------------------------------------------
1029  */
1030
1031 void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
1032 {
1033         GtkTreeIter iter;
1034         gint row, col;
1035
1036         if (g_list_find(ct->cd->list, ct->focus_info))
1037                 {
1038                 if (info == ct->focus_info)
1039                         {
1040                         /* ensure focus row col are correct */
1041                         collection_table_find_position(ct, ct->focus_info,
1042                                                        &ct->focus_row, &ct->focus_column);
1043                         return;
1044                         }
1045                 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1046                 }
1047
1048         if (!collection_table_find_position(ct, info, &row, &col))
1049                 {
1050                 ct->focus_info = NULL;
1051                 ct->focus_row = -1;
1052                 ct->focus_column = -1;
1053                 return;
1054                 }
1055
1056         ct->focus_info = info;
1057         ct->focus_row = row;
1058         ct->focus_column = col;
1059         collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1060
1061         if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1062                 {
1063                 GtkTreePath *tpath;
1064                 GtkTreeViewColumn *column;
1065                 GtkTreeModel *store;
1066
1067                 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1068
1069                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1070                 tpath = gtk_tree_model_get_path(store, &iter);
1071                 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1072                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1073                 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1074                 gtk_tree_path_free(tpath);
1075                 }
1076 }
1077
1078 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1079 {
1080         gint new_row;
1081         gint new_col;
1082
1083         if (relative)
1084                 {
1085                 new_row = ct->focus_row;
1086                 new_col = ct->focus_column;
1087
1088                 new_row += row;
1089                 if (new_row < 0) new_row = 0;
1090                 if (new_row >= ct->rows) new_row = ct->rows - 1;
1091
1092                 while (col != 0)
1093                         {
1094                         if (col < 0)
1095                                 {
1096                                 new_col--;
1097                                 col++;
1098                                 }
1099                         else
1100                                 {
1101                                 new_col++;
1102                                 col--;
1103                                 }
1104
1105                         if (new_col < 0)
1106                                 {
1107                                 if (new_row > 0)
1108                                         {
1109                                         new_row--;
1110                                         new_col = ct->columns - 1;
1111                                         }
1112                                 else
1113                                         {
1114                                         new_col = 0;
1115                                         }
1116                                 }
1117                         if (new_col >= ct->columns)
1118                                 {
1119                                 if (new_row < ct->rows - 1)
1120                                         {
1121                                         new_row++;
1122                                         new_col = 0;
1123                                         }
1124                                 else
1125                                         {
1126                                         new_col = ct->columns - 1;
1127                                         }
1128                                 }
1129                         }
1130                 }
1131         else
1132                 {
1133                 new_row = row;
1134                 new_col = col;
1135
1136                 if (new_row >= ct->rows)
1137                         {
1138                         if (ct->rows > 0)
1139                                 new_row = ct->rows - 1;
1140                         else
1141                                 new_row = 0;
1142                         new_col = ct->columns - 1;
1143                         }
1144                 if (new_col >= ct->columns) new_col = ct->columns - 1;
1145                 }
1146
1147         if (new_row == ct->rows - 1)
1148                 {
1149                 gint l;
1150
1151                 /* if we moved beyond the last image, go to the last image */
1152
1153                 l = g_list_length(ct->cd->list);
1154                 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1155                 if (new_col >= l) new_col = l - 1;
1156                 }
1157
1158         if (new_row == -1 || new_col == -1)
1159                 {
1160                 if (!ct->cd->list) return;
1161                 new_row = new_col = 0;
1162                 }
1163
1164         collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1165 }
1166
1167 static void collection_table_update_focus(CollectTable *ct)
1168 {
1169         gint new_row = 0;
1170         gint new_col = 0;
1171
1172         if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1173                 {
1174                 /* first find the old focus, if it exists and is valid */
1175                 }
1176         else
1177                 {
1178                 /* (try to) stay where we were */
1179                 new_row = ct->focus_row;
1180                 new_col = ct->focus_column;
1181                 }
1182
1183         collection_table_move_focus(ct, new_row, new_col, FALSE);
1184 }
1185
1186 /* used to figure the page up/down distances */
1187 static gint page_height(CollectTable *ct)
1188 {
1189         GtkAdjustment *adj;
1190         gint page_size;
1191         gint row_height;
1192         gint ret;
1193
1194         adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1195         page_size = (gint)gtk_adjustment_get_page_increment(adj);
1196
1197         row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1198         if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1199
1200         ret = page_size / row_height;
1201         if (ret < 1) ret = 1;
1202
1203         return ret;
1204 }
1205
1206 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1207 {
1208         CollectTable *ct = data;
1209         GtkTreeModel *store;
1210         GtkTreeIter iter;
1211         gint column;
1212         GtkTreePath *tpath;
1213         gint cw, ch;
1214
1215         if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1216         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1217         tpath = gtk_tree_model_get_path(store, &iter);
1218         tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1219         gtk_tree_path_free(tpath);
1220         *y += ch;
1221         popup_menu_position_clamp(menu, x, y, 0);
1222 }
1223
1224 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1225 {
1226         CollectTable *ct = data;
1227         gint focus_row = 0;
1228         gint focus_col = 0;
1229         CollectInfo *info;
1230         gboolean stop_signal = TRUE;
1231
1232         switch (event->keyval)
1233                 {
1234                 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1235                         focus_col = -1;
1236                         break;
1237                 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1238                         focus_col = 1;
1239                         break;
1240                 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1241                         focus_row = -1;
1242                         break;
1243                 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1244                         focus_row = 1;
1245                         break;
1246                 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1247                         focus_row = -page_height(ct);
1248                         break;
1249                 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1250                         focus_row = page_height(ct);
1251                         break;
1252                 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1253                         focus_row = -ct->focus_row;
1254                         focus_col = -ct->focus_column;
1255                         break;
1256                 case GDK_KEY_End: case GDK_KEY_KP_End:
1257                         focus_row = ct->rows - 1 - ct->focus_row;
1258                         focus_col = ct->columns - 1 - ct->focus_column;
1259                         break;
1260                 case GDK_KEY_space:
1261                         info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1262                         if (info)
1263                                 {
1264                                 ct->click_info = info;
1265                                 if (event->state & GDK_CONTROL_MASK)
1266                                         {
1267                                         collection_table_select_util(ct, info, !INFO_SELECTED(info));
1268                                         }
1269                                 else
1270                                         {
1271                                         collection_table_unselect_all(ct);
1272                                         collection_table_select(ct, info);
1273                                         }
1274                                 }
1275                         break;
1276                 case 'T': case 't':
1277                         if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1278                         break;
1279                 case GDK_KEY_Menu:
1280                 case GDK_KEY_F10:
1281                         info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1282                         ct->click_info = info;
1283
1284                         collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1285                         tip_unschedule(ct);
1286
1287                         ct->popup = collection_table_popup_menu(ct, (info != NULL));
1288                         gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1289                         break;
1290                 default:
1291                         stop_signal = FALSE;
1292                         break;
1293                 }
1294
1295         if (focus_row != 0 || focus_col != 0)
1296                 {
1297                 CollectInfo *new_info;
1298                 CollectInfo *old_info;
1299
1300                 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1301                 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1302                 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1303
1304                 if (new_info != old_info)
1305                         {
1306                         if (event->state & GDK_SHIFT_MASK)
1307                                 {
1308                                 if (!options->collections.rectangular_selection)
1309                                         {
1310                                         collection_table_select_region_util(ct, old_info, new_info, FALSE);
1311                                         }
1312                                 else
1313                                         {
1314                                         collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1315                                         }
1316                                 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1317                                 }
1318                         else if (event->state & GDK_CONTROL_MASK)
1319                                 {
1320                                 ct->click_info = new_info;
1321                                 }
1322                         else
1323                                 {
1324                                 ct->click_info = new_info;
1325                                 collection_table_unselect_all(ct);
1326                                 collection_table_select(ct, new_info);
1327                                 }
1328                         }
1329                 }
1330
1331         if (stop_signal)
1332                 {
1333                 tip_unschedule(ct);
1334                 }
1335
1336         return stop_signal;
1337 }
1338
1339 /*
1340  *-------------------------------------------------------------------
1341  * insert marker
1342  *-------------------------------------------------------------------
1343  */
1344
1345 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1346                                                  gboolean use_coord, gint x, gint y)
1347 {
1348         CollectInfo *info = NULL;
1349         GtkTreeModel *store;
1350         GtkTreeIter iter;
1351         GtkTreePath *tpath;
1352         GtkTreeViewColumn *column;
1353 #if GTK_CHECK_VERSION(3,0,0)
1354         GdkDeviceManager *device_manager;
1355         GdkDevice *device;
1356 #endif
1357
1358         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1359
1360 #if GTK_CHECK_VERSION(3,0,0)
1361         if (!use_coord)
1362                 {
1363                 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1364                                                                         gtk_widget_get_window(ct->listview)));
1365                 device = gdk_device_manager_get_client_pointer(device_manager);
1366                 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1367                                                                         device, &x, &y, NULL);
1368                 }
1369 #else
1370         if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1371 #endif
1372         if (source)
1373                 {
1374                 gint col;
1375                 if (collection_table_find_iter(ct, source, &iter, &col))
1376                         {
1377                         tpath = gtk_tree_model_get_path(store, &iter);
1378                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1379                         gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1380                         gtk_tree_path_free(tpath);
1381
1382                         info = source;
1383                         *after = !!(x > cell->x + (cell->width / 2));
1384                         }
1385                 return info;
1386                 }
1387
1388         if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1389                                           &tpath, &column, NULL, NULL))
1390                 {
1391                 GList *list;
1392                 gint n;
1393
1394                 gtk_tree_model_get_iter(store, &iter, tpath);
1395                 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1396
1397                 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1398                 info = g_list_nth_data(list, n);
1399
1400                 if (info)
1401                         {
1402                         gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1403                         *after = !!(x > cell->x + (cell->width / 2));
1404                         }
1405
1406                 gtk_tree_path_free(tpath);
1407                 }
1408
1409         if (info == NULL)
1410                 {
1411                 GList *work;
1412
1413                 work = g_list_last(ct->cd->list);
1414                 if (work)
1415                         {
1416                         gint col;
1417
1418                         info = work->data;
1419                         *after = TRUE;
1420
1421                         if (collection_table_find_iter(ct, info, &iter, &col))
1422                                 {
1423                                 tpath = gtk_tree_model_get_path(store, &iter);
1424                                 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1425                                 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1426                                 gtk_tree_path_free(tpath);
1427                                 }
1428                         }
1429                 }
1430
1431         return info;
1432 }
1433
1434 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1435 {
1436         CollectInfo *info;
1437         GdkRectangle cell;
1438         gboolean after = FALSE;
1439
1440         info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1441
1442         if (info && after)
1443                 {
1444                 GList *work;
1445
1446                 work = g_list_find(ct->cd->list, info);
1447                 if (work && work->next)
1448                         {
1449                         info = work->next->data;
1450                         }
1451                 else
1452                         {
1453                         info = NULL;
1454                         }
1455                 }
1456
1457         return info;
1458 }
1459
1460 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1461 {
1462         gboolean after = FALSE;
1463         GdkRectangle cell;
1464
1465         if (!enable)
1466                 {
1467                 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1468                 ct->marker_window = NULL;
1469
1470                 return;
1471                 }
1472
1473         info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1474
1475         /* this setting does not take into account (after), but since it is not really used... */
1476         ct->marker_info = info;
1477
1478         if (!ct->marker_window)
1479                 {
1480                 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1481                 GdkWindowAttr attributes;
1482                 gint attributes_mask;
1483                 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1484                 gint w = gdk_pixbuf_get_width(pb);
1485                 gint h = gdk_pixbuf_get_height(pb);
1486
1487                 attributes.window_type = GDK_WINDOW_CHILD;
1488                 attributes.wclass = GDK_INPUT_OUTPUT;
1489                 attributes.width = w;
1490                 attributes.height = h;
1491                 attributes.event_mask = gtk_widget_get_events(ct->listview);
1492                 attributes_mask = 0;
1493
1494                 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1495
1496 #if GTK_CHECK_VERSION(3,0,0)
1497                 cairo_region_t *mask;
1498                 cairo_pattern_t *pattern;
1499                 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1500                 cairo_t *cr = cairo_create(img);
1501                 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1502                 cairo_paint(cr);
1503                 pattern = cairo_pattern_create_for_surface(img);
1504                 mask = gdk_cairo_region_create_from_surface(img);
1505                 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1506                 gdk_window_set_background_pattern(ct->marker_window, pattern);
1507                 cairo_region_destroy(mask);
1508                 cairo_pattern_destroy(pattern);
1509                 cairo_destroy(cr);
1510                 cairo_surface_destroy(img);
1511 #else
1512                 GdkPixmap *pixmap;
1513                 GdkBitmap *mask;
1514                 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1515
1516                 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1517                 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1518
1519                 g_object_unref(pixmap);
1520                 if (mask) g_object_unref(mask);
1521 #endif
1522                 g_object_unref(pb);
1523                 }
1524
1525         if (info)
1526                 {
1527                 gint x, y;
1528                 gint w, h;
1529
1530                 w = gdk_window_get_width(ct->marker_window);
1531                 h = gdk_window_get_height(ct->marker_window);
1532
1533                 if (!after)
1534                         {
1535                         x = cell.x;
1536                         }
1537                 else
1538                         {
1539                         x = cell.x + cell.width;
1540                         }
1541                 x -= (w / 2);
1542                 y = cell.y + (cell.height / 2) - (h / 2);
1543
1544                 gdk_window_move(ct->marker_window, x, y);
1545 #if !GTK_CHECK_VERSION(3,0,0)
1546                 gdk_window_clear(ct->marker_window);
1547 #endif
1548                 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1549                 }
1550         else
1551                 {
1552                 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1553                 }
1554 }
1555
1556 /*
1557  *-------------------------------------------------------------------
1558  * mouse drag auto-scroll
1559  *-------------------------------------------------------------------
1560  */
1561
1562 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1563 {
1564         CollectInfo *info;
1565
1566         info = collection_table_find_data_by_coord(ct, x, y, NULL);
1567
1568         if (drop_event)
1569                 {
1570                 tip_unschedule(ct);
1571                 collection_table_insert_marker(ct, info, TRUE);
1572                 }
1573         else
1574                 {
1575                 tip_update(ct, info);
1576                 }
1577 }
1578
1579 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1580 {
1581         CollectTable *ct = data;
1582         GdkWindow *window;
1583         gint x, y;
1584         gint w, h;
1585 #if GTK_CHECK_VERSION(3,0,0)
1586         GdkDeviceManager *device_manager;
1587         GdkDevice *device;
1588 #endif
1589
1590         if (!ct->drop_idle_id) return FALSE;
1591
1592         window = gtk_widget_get_window(ct->listview);
1593 #if GTK_CHECK_VERSION(3,0,0)
1594         device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1595         device = gdk_device_manager_get_client_pointer(device_manager);
1596         gdk_window_get_device_position(window, device, &x, &y, NULL);
1597 #else
1598         gdk_window_get_pointer(window, &x, &y, NULL);
1599 #endif
1600         w = gdk_window_get_width(window);
1601         h = gdk_window_get_height(window);
1602         if (x >= 0 && x < w && y >= 0 && y < h)
1603                 {
1604                 collection_table_motion_update(ct, x, y, TRUE);
1605                 }
1606
1607         ct->drop_idle_id = 0;
1608         return FALSE;
1609 }
1610
1611 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1612 {
1613         CollectTable *ct = data;
1614
1615         if (!ct->drop_idle_id)
1616                 {
1617                 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1618                 }
1619
1620         return TRUE;
1621 }
1622
1623 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1624 {
1625         if (!scroll)
1626                 {
1627                 if (ct->drop_idle_id)
1628                         {
1629                         g_source_remove(ct->drop_idle_id);
1630                         ct->drop_idle_id = 0;
1631                         }
1632                 widget_auto_scroll_stop(ct->listview);
1633                 collection_table_insert_marker(ct, NULL, FALSE);
1634                 }
1635         else
1636                 {
1637                 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1638                 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1639                                          collection_table_auto_scroll_notify_cb, ct);
1640                 }
1641 }
1642
1643 /*
1644  *-------------------------------------------------------------------
1645  * mouse callbacks
1646  *-------------------------------------------------------------------
1647  */
1648
1649 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1650 {
1651         CollectTable *ct = data;
1652
1653         collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1654
1655         return FALSE;
1656 }
1657
1658 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1659 {
1660         CollectTable *ct = data;
1661         GtkTreeIter iter;
1662         CollectInfo *info;
1663
1664         tip_unschedule(ct);
1665
1666         info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1667
1668         ct->click_info = info;
1669         collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1670
1671         switch (bevent->button)
1672                 {
1673                 case MOUSE_BUTTON_LEFT:
1674                         if (bevent->type == GDK_2BUTTON_PRESS)
1675                                 {
1676                                 if (info)
1677                                         {
1678                                         layout_image_set_collection(NULL, ct->cd, info);
1679                                         }
1680                                 }
1681                         else if (!gtk_widget_has_focus(ct->listview))
1682                                 {
1683                                 gtk_widget_grab_focus(ct->listview);
1684                                 }
1685                         break;
1686                 case MOUSE_BUTTON_RIGHT:
1687                         ct->popup = collection_table_popup_menu(ct, (info != NULL));
1688                         gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1689                         break;
1690                 default:
1691                         break;
1692                 }
1693
1694         return TRUE;
1695 }
1696
1697 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1698 {
1699         CollectTable *ct = data;
1700         GtkTreeIter iter;
1701         CollectInfo *info = NULL;
1702
1703         tip_schedule(ct);
1704
1705         if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1706                 {
1707                 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1708                 }
1709
1710         if (ct->click_info)
1711                 {
1712                 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1713                 }
1714
1715         if (bevent->button == MOUSE_BUTTON_LEFT &&
1716             info && ct->click_info == info)
1717                 {
1718                 collection_table_set_focus(ct, info);
1719
1720                 if (bevent->state & GDK_CONTROL_MASK)
1721                         {
1722                         gboolean select = !INFO_SELECTED(info);
1723
1724                         if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1725                                 {
1726                                 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1727                                 }
1728                         else
1729                                 {
1730                                 collection_table_select_util(ct, info, select);
1731                                 }
1732                         }
1733                 else
1734                         {
1735                         collection_table_unselect_all(ct);
1736
1737                         if ((bevent->state & GDK_SHIFT_MASK) &&
1738                             ct->prev_selection)
1739                                 {
1740                                 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1741                                 }
1742                         else
1743                                 {
1744                                 collection_table_select_util(ct, info, TRUE);
1745                                 }
1746                         }
1747                 }
1748         else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1749                  info && ct->click_info == info)
1750                 {
1751                 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1752                 }
1753
1754         return TRUE;
1755 }
1756
1757 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1758 {
1759         CollectTable *ct = data;
1760
1761         tip_unschedule(ct);
1762         return FALSE;
1763 }
1764
1765 /*
1766  *-------------------------------------------------------------------
1767  * populate, add, insert, etc.
1768  *-------------------------------------------------------------------
1769  */
1770
1771 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1772 {
1773         GList *list;
1774
1775         gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1776         g_list_free(list);
1777
1778         return FALSE;
1779 }
1780
1781 static void collection_table_clear_store(CollectTable *ct)
1782 {
1783         GtkTreeModel *store;
1784
1785         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1786         gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1787
1788         gtk_list_store_clear(GTK_LIST_STORE(store));
1789 }
1790
1791 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1792 {
1793         GtkListStore *store;
1794         GList *list = NULL;
1795         gint i;
1796
1797         for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1798
1799         store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1800         gtk_list_store_append(store, iter);
1801         gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1802
1803         return list;
1804 }
1805
1806 static void collection_table_populate(CollectTable *ct, gboolean resize)
1807 {
1808         gint row;
1809         GList *work;
1810
1811         collection_table_verify_selections(ct);
1812
1813         collection_table_clear_store(ct);
1814
1815         if (resize)
1816                 {
1817                 gint i;
1818                 gint thumb_width;
1819
1820                 thumb_width = collection_table_get_icon_width(ct);
1821
1822                 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1823                         {
1824                         GtkTreeViewColumn *column;
1825                         GtkCellRenderer *cell;
1826                         GList *list;
1827
1828                         column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1829                         gtk_tree_view_column_set_visible(column, (i < ct->columns));
1830                         gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1831
1832                         list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1833                         cell = (list) ? list->data : NULL;
1834                         g_list_free(list);
1835
1836                         if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1837                                 {
1838                                 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1839                                                              "fixed_height", options->thumbnails.max_height,
1840                                                              "show_text", ct->show_text || ct->show_stars, NULL);
1841                                 }
1842                         }
1843                 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1844                 }
1845
1846         row = -1;
1847         work = ct->cd->list;
1848         while (work)
1849                 {
1850                 GList *list;
1851                 GtkTreeIter iter;
1852
1853                 row++;
1854
1855                 list = collection_table_add_row(ct, &iter);
1856                 while (work && list)
1857                         {
1858                         list->data = work->data;
1859                         list = list->next;
1860                         work = work->next;
1861                         }
1862                 }
1863
1864         ct->rows = row + 1;
1865
1866         collection_table_update_focus(ct);
1867         collection_table_update_status(ct);
1868 }
1869
1870 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1871 {
1872         gint new_cols;
1873         gint thumb_width;
1874
1875         thumb_width = collection_table_get_icon_width(ct);
1876
1877         new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1878         if (new_cols < 1) new_cols = 1;
1879
1880         if (!force && new_cols == ct->columns) return;
1881
1882         ct->columns = new_cols;
1883
1884         collection_table_populate(ct, TRUE);
1885
1886         DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1887 }
1888
1889 static void collection_table_sync(CollectTable *ct)
1890 {
1891         GtkTreeModel *store;
1892         GtkTreeIter iter;
1893         GList *work;
1894         gint r, c;
1895
1896         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1897
1898         r = -1;
1899         c = 0;
1900
1901         work = ct->cd->list;
1902         while (work)
1903                 {
1904                 GList *list;
1905                 r++;
1906                 c = 0;
1907                 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1908                         {
1909                         gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1910                         gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1911                         }
1912                 else
1913                         {
1914                         list = collection_table_add_row(ct, &iter);
1915                         }
1916
1917                 while (list)
1918                         {
1919                         CollectInfo *info;
1920                         if (work)
1921                                 {
1922                                 info = work->data;
1923                                 work = work->next;
1924                                 c++;
1925                                 }
1926                         else
1927                                 {
1928                                 info = NULL;
1929                                 }
1930                         if (list)
1931                                 {
1932                                 list->data = info;
1933                                 list = list->next;
1934                                 }
1935                         }
1936                 }
1937
1938         r++;
1939         while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1940                 {
1941                 GList *list;
1942
1943                 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1944                 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1945                 g_list_free(list);
1946                 }
1947
1948         ct->rows = r;
1949
1950         collection_table_update_focus(ct);
1951         collection_table_update_status(ct);
1952 }
1953
1954 static gboolean collection_table_sync_idle_cb(gpointer data)
1955 {
1956         CollectTable *ct = data;
1957
1958         if (!ct->sync_idle_id) return FALSE;
1959         g_source_remove(ct->sync_idle_id);
1960         ct->sync_idle_id = 0;
1961
1962         collection_table_sync(ct);
1963         return FALSE;
1964 }
1965
1966 static void collection_table_sync_idle(CollectTable *ct)
1967 {
1968         if (!ct->sync_idle_id)
1969                 {
1970                 /* high priority, the view needs to be resynced before a redraw
1971                  * may contain invalid pointers at this time
1972                  */
1973                 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1974                 }
1975 }
1976
1977 void collection_table_add_filelist(CollectTable *ct, GList *list)
1978 {
1979         GList *work;
1980
1981         if (!list) return;
1982
1983         work = list;
1984         while (work)
1985                 {
1986                 collection_add(ct->cd, (FileData *)work->data, FALSE);
1987                 work = work->next;
1988                 }
1989 }
1990
1991 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1992 {
1993         GList *work;
1994
1995         if (!list) return;
1996
1997         work = list;
1998         while (work)
1999                 {
2000                 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
2001                 work = work->next;
2002                 }
2003
2004         collection_table_sync_idle(ct);
2005 }
2006
2007 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
2008 {
2009         GList *work;
2010         GList *insert_pos = NULL;
2011         GList *temp;
2012         CollectInfo *info;
2013
2014         if (!info_list) return;
2015
2016         info = collection_table_find_data(ct, row, col, NULL);
2017
2018         if (!info_list->next && info_list->data == info) return;
2019
2020         if (info) insert_pos = g_list_find(ct->cd->list, info);
2021
2022         /* FIXME: this may get slow for large lists */
2023         work = info_list;
2024         while (insert_pos && work)
2025                 {
2026                 if (insert_pos->data == work->data)
2027                         {
2028                         insert_pos = insert_pos->next;
2029                         work = info_list;
2030                         }
2031                 else
2032                         {
2033                         work = work->next;
2034                         }
2035                 }
2036
2037         work = info_list;
2038         while (work)
2039                 {
2040                 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2041                 work = work->next;
2042                 }
2043
2044         /* place them back in */
2045         temp = g_list_copy(info_list);
2046
2047         if (insert_pos)
2048                 {
2049                 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2050                 }
2051         else if (info)
2052                 {
2053                 ct->cd->list = g_list_concat(temp, ct->cd->list);
2054                 }
2055         else
2056                 {
2057                 ct->cd->list = g_list_concat(ct->cd->list, temp);
2058                 }
2059
2060         ct->cd->changed = TRUE;
2061
2062         collection_table_sync_idle(ct);
2063 }
2064
2065
2066 /*
2067  *-------------------------------------------------------------------
2068  * updating
2069  *-------------------------------------------------------------------
2070  */
2071
2072 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2073 {
2074         GtkTreeIter iter;
2075         gint row, col;
2076         gdouble value;
2077
2078         if (!info)
2079                 {
2080                 collection_table_update_extras(ct, FALSE, 0.0);
2081                 return;
2082                 }
2083
2084         if (!collection_table_find_position(ct, info, &row, &col)) return;
2085
2086         if (ct->columns != 0 && ct->rows != 0)
2087                 {
2088                 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2089                 }
2090         else
2091                 {
2092                 value = 0.0;
2093                 }
2094
2095         collection_table_update_extras(ct, TRUE, value);
2096
2097         if (collection_table_find_iter(ct, info, &iter, NULL))
2098                 {
2099                 GtkTreeModel *store;
2100                 GList *list;
2101
2102                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2103                 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2104                 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2105                 }
2106 }
2107
2108 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2109 {
2110         collection_table_sync_idle(ct);
2111 }
2112
2113 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2114 {
2115         collection_table_sync_idle(ct);
2116 }
2117
2118 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2119 {
2120         if (ci && INFO_SELECTED(ci))
2121                 {
2122                 ct->selection = g_list_remove(ct->selection, ci);
2123                 }
2124
2125         collection_table_sync_idle(ct);
2126 }
2127
2128 void collection_table_refresh(CollectTable *ct)
2129 {
2130         collection_table_populate(ct, FALSE);
2131 }
2132
2133 /*
2134  *-------------------------------------------------------------------
2135  * dnd
2136  *-------------------------------------------------------------------
2137  */
2138
2139 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2140 {
2141         GList *d;
2142         GList *f;
2143         GList *work;
2144
2145         if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2146                 return;
2147
2148         f = filelist_filter(f, FALSE);
2149         d = filelist_filter(d, TRUE);
2150
2151         f = filelist_sort_path(f);
2152         d = filelist_sort_path(d);
2153
2154         collection_table_insert_filelist(ct, f, ct->marker_info);
2155
2156         work = g_list_last(d);
2157         while (work)
2158                 {
2159                 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2160                 work = work->prev;
2161                 }
2162
2163         filelist_free(f);
2164         filelist_free(d);
2165 }
2166
2167 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2168 {
2169         GList *work = list;
2170         while (work)
2171                 {
2172                 FileData *fd = work->data;
2173                 work = work->next;
2174                 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2175                 }
2176         collection_table_insert_filelist(ct, list, ct->marker_info);
2177 }
2178
2179
2180 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2181 {
2182         CollectTable *ct = data;
2183
2184         confirm_dir_list_do(ct, ct->drop_list, FALSE);
2185 }
2186
2187 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2188 {
2189         CollectTable *ct = data;
2190
2191         confirm_dir_list_do(ct, ct->drop_list, TRUE);
2192 }
2193
2194 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2195 {
2196         CollectTable *ct = data;
2197
2198         collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2199 }
2200
2201 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2202 {
2203         GtkWidget *menu;
2204
2205         menu = popup_menu_short_lived();
2206         g_signal_connect(G_OBJECT(menu), "destroy",
2207                          G_CALLBACK(collection_table_popup_destroy_cb), ct);
2208
2209         menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2210         menu_item_add_divider(menu);
2211         menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2212                             G_CALLBACK(confirm_dir_list_add), ct);
2213         menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2214                             G_CALLBACK(confirm_dir_list_recurse), ct);
2215         menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2216                             G_CALLBACK(confirm_dir_list_skip), ct);
2217         menu_item_add_divider(menu);
2218         menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2219
2220         return menu;
2221 }
2222
2223 /*
2224  *-------------------------------------------------------------------
2225  * dnd
2226  *-------------------------------------------------------------------
2227  */
2228
2229 static GtkTargetEntry collection_drag_types[] = {
2230         { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2231         { "text/uri-list", 0, TARGET_URI_LIST },
2232         { "text/plain", 0, TARGET_TEXT_PLAIN }
2233 };
2234 static gint n_collection_drag_types = 3;
2235
2236 static GtkTargetEntry collection_drop_types[] = {
2237         { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2238         { "text/uri-list", 0, TARGET_URI_LIST }
2239 };
2240 static gint n_collection_drop_types = 2;
2241
2242
2243 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2244                                      GtkSelectionData *selection_data, guint info,
2245                                      guint time, gpointer data)
2246 {
2247         CollectTable *ct = data;
2248         gboolean selected;
2249         GList *list = NULL;
2250         gchar *uri_text = NULL;
2251         gint total;
2252
2253         if (!ct->click_info) return;
2254
2255         selected = INFO_SELECTED(ct->click_info);
2256
2257         switch (info)
2258                 {
2259                 case TARGET_APP_COLLECTION_MEMBER:
2260                         if (selected)
2261                                 {
2262                                 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2263                                 }
2264                         else
2265                                 {
2266                                 list = g_list_append(NULL, ct->click_info);
2267                                 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2268                                 g_list_free(list);
2269                                 }
2270                         gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2271                                                 8, (guchar *)uri_text, total);
2272                         g_free(uri_text);
2273                         break;
2274                 case TARGET_URI_LIST:
2275                 case TARGET_TEXT_PLAIN:
2276                 default:
2277                         if (selected)
2278                                 {
2279                                 list = collection_table_selection_get_list(ct);
2280                                 }
2281                         else
2282                                 {
2283                                 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2284                                 }
2285                         if (!list) return;
2286
2287                         uri_selection_data_set_uris_from_filelist(selection_data, list);
2288                         filelist_free(list);
2289                         break;
2290                 }
2291 }
2292
2293
2294 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2295                                           gint x, gint y,
2296                                           GtkSelectionData *selection_data, guint info,
2297                                           guint time, gpointer data)
2298 {
2299         CollectTable *ct = data;
2300         GList *list = NULL;
2301         GList *info_list = NULL;
2302         CollectionData *source;
2303         CollectInfo *drop_info;
2304         GList *work;
2305
2306         DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2307
2308         collection_table_scroll(ct, FALSE);
2309         collection_table_insert_marker(ct, NULL, FALSE);
2310
2311         drop_info = collection_table_insert_point(ct, x, y);
2312
2313         switch (info)
2314                 {
2315                 case TARGET_APP_COLLECTION_MEMBER:
2316                         source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2317                         if (source)
2318                                 {
2319                                 if (source == ct->cd)
2320                                         {
2321                                         gint row = -1;
2322                                         gint col = -1;
2323
2324                                         /* it is a move within a collection */
2325                                         filelist_free(list);
2326                                         list = NULL;
2327
2328                                         if (!drop_info)
2329                                                 {
2330                                                 collection_table_move_by_info_list(ct, info_list, -1, -1);
2331                                                 }
2332                                         else if (collection_table_find_position(ct, drop_info, &row, &col))
2333                                                 {
2334                                                 collection_table_move_by_info_list(ct, info_list, row, col);
2335                                                 }
2336                                         }
2337                                 else
2338                                         {
2339                                         /* it is a move/copy across collections */
2340                                         if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2341                                                 {
2342                                                 collection_remove_by_info_list(source, info_list);
2343                                                 }
2344                                         }
2345                                 g_list_free(info_list);
2346                                 }
2347                         break;
2348                 case TARGET_URI_LIST:
2349                         list = uri_filelist_from_gtk_selection_data(selection_data);
2350                         work = list;
2351                         while (work)
2352                                 {
2353                                 FileData *fd = work->data;
2354                                 if (isdir(fd->path))
2355                                         {
2356                                         GtkWidget *menu;
2357
2358                                         ct->drop_list = list;
2359                                         ct->drop_info = drop_info;
2360                                         menu = collection_table_drop_menu(ct);
2361                                         gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2362                                         return;
2363                                         }
2364                                 work = work->next;
2365                                 }
2366                         break;
2367                 default:
2368                         list = NULL;
2369                         break;
2370                 }
2371
2372         if (list)
2373                 {
2374                 collection_table_insert_filelist(ct, list, drop_info);
2375                 filelist_free(list);
2376                 }
2377 }
2378
2379 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2380 {
2381         CollectTable *ct = data;
2382
2383         if (ct->click_info && ct->click_info->pixbuf)
2384                 {
2385                 gint items;
2386
2387                 if (INFO_SELECTED(ct->click_info))
2388                         items = g_list_length(ct->selection);
2389                 else
2390                         items = 1;
2391                 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2392                 }
2393 }
2394
2395 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2396 {
2397         CollectTable *ct = data;
2398
2399         /* apparently a leave event is not generated on a drop */
2400         tip_unschedule(ct);
2401
2402         collection_table_scroll(ct, FALSE);
2403 }
2404
2405 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2406                                         gint x, gint y, guint time, gpointer data)
2407 {
2408         CollectTable *ct = data;
2409
2410         collection_table_motion_update(ct, x, y, TRUE);
2411         collection_table_scroll(ct, TRUE);
2412
2413         return FALSE;
2414 }
2415
2416 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2417 {
2418         CollectTable *ct = data;
2419
2420         collection_table_scroll(ct, FALSE);
2421 }
2422
2423 static void collection_table_dnd_init(CollectTable *ct)
2424 {
2425         gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2426                             collection_drag_types, n_collection_drag_types,
2427                             GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2428         g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2429                          G_CALLBACK(collection_table_dnd_get), ct);
2430         g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2431                          G_CALLBACK(collection_table_dnd_begin), ct);
2432         g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2433                          G_CALLBACK(collection_table_dnd_end), ct);
2434
2435         gtk_drag_dest_set(ct->listview,
2436                           GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2437                           collection_drop_types, n_collection_drop_types,
2438                           GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2439         g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2440                          G_CALLBACK(collection_table_dnd_motion), ct);
2441         g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2442                          G_CALLBACK(collection_table_dnd_leave), ct);
2443         g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2444                          G_CALLBACK(collection_table_dnd_receive), ct);
2445 }
2446
2447 /*
2448  *-----------------------------------------------------------------------------
2449  * draw, etc.
2450  *-----------------------------------------------------------------------------
2451  */
2452
2453 typedef struct _ColumnData ColumnData;
2454 struct _ColumnData
2455 {
2456         CollectTable *ct;
2457         gint number;
2458 };
2459
2460 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2461                                           GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2462 {
2463         ColumnData *cd = data;
2464         CollectTable *ct;
2465         GtkStyle *style;
2466         GList *list;
2467         CollectInfo *info;
2468         GdkColor color_fg;
2469         GdkColor color_bg;
2470         gchar *star_rating = NULL;
2471         gchar *display_text = NULL;
2472
2473         ct = cd->ct;
2474
2475         gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2476
2477 #if GTK_CHECK_VERSION(3,0,0)
2478         /* FIXME this is a primitive hack to stop a crash.
2479          * When compiled with GTK3, if a Collection window containing
2480          * say, 50 or so, images has its width changed, there is a segfault
2481          * https://github.com/BestImageViewer/geeqie/issues/531
2482          */
2483         if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2484 #endif
2485         info = g_list_nth_data(list, cd->number);
2486
2487         style = gtk_widget_get_style(ct->listview);
2488         if (info && (info->flag_mask & SELECTION_SELECTED) )
2489                 {
2490                 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2491                 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2492                 }
2493         else
2494                 {
2495                 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2496                 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2497                 }
2498
2499         if (info && (info->flag_mask & SELECTION_PRELIGHT))
2500                 {
2501                 shift_color(&color_bg, -1, 0);
2502                 }
2503
2504         if (ct->show_stars && info && info->fd)
2505                 {
2506                 star_rating = metadata_read_rating_stars(info->fd);
2507                 }
2508         else
2509                 {
2510                 star_rating = g_strdup("");
2511                 }
2512
2513         if (info && info->fd)
2514                 {
2515                 if (ct->show_text && ct->show_stars)
2516                         {
2517                         display_text = g_strconcat(info->fd->name, "\n", star_rating, NULL);
2518                         }
2519                 else if (ct->show_text)
2520                         {
2521                         display_text = g_strdup(info->fd->name);
2522                         }
2523                 else if (ct->show_stars)
2524                         {
2525                         display_text = g_strdup(star_rating);
2526                         }
2527                 else
2528                         {
2529                         display_text = g_strdup("");
2530                         }
2531                 }
2532         else
2533                 {
2534                 display_text = g_strdup("");
2535                 }
2536
2537         if (GQV_IS_CELL_RENDERER_ICON(cell))
2538                 {
2539                 if (info)
2540                         {
2541                         g_object_set(cell,      "pixbuf", info->pixbuf,
2542                                                 "text",  display_text,
2543                                                 "cell-background-gdk", &color_bg,
2544                                                 "cell-background-set", TRUE,
2545                                                 "foreground-gdk", &color_fg,
2546                                                 "foreground-set", TRUE,
2547                                                 "has-focus", (ct->focus_info == info), NULL);
2548                         }
2549                 else
2550                         {
2551                         g_object_set(cell,      "pixbuf", NULL,
2552                                                 "text", NULL,
2553                                                 "cell-background-set", FALSE,
2554                                                 "foreground-set", FALSE,
2555                                                 "has-focus", FALSE,  NULL);
2556                         }
2557                 }
2558
2559         g_free(display_text);
2560         g_free(star_rating);
2561 }
2562
2563 static void collection_table_append_column(CollectTable *ct, gint n)
2564 {
2565         ColumnData *cd;
2566         GtkTreeViewColumn *column;
2567         GtkCellRenderer *renderer;
2568
2569         column = gtk_tree_view_column_new();
2570         gtk_tree_view_column_set_min_width(column, 0);
2571
2572         gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2573         gtk_tree_view_column_set_alignment(column, 0.5);
2574
2575         renderer = gqv_cell_renderer_icon_new();
2576         gtk_tree_view_column_pack_start(column, renderer, FALSE);
2577         g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2578                                          "ypad", THUMB_BORDER_PADDING,
2579                                          "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2580
2581         g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2582
2583         cd = g_new0(ColumnData, 1);
2584         cd->ct = ct;
2585         cd->number = n;
2586         gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2587
2588         gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2589 }
2590
2591 /*
2592  *-------------------------------------------------------------------
2593  * init, destruction
2594  *-------------------------------------------------------------------
2595  */
2596
2597 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2598 {
2599         CollectTable *ct = data;
2600
2601         /* If there is no unsaved data, save the window geometry
2602          */
2603         if (!ct->cd->changed)
2604                 {
2605                 if (!collection_save(ct->cd, ct->cd->path))
2606                         {
2607                         log_printf("failed saving to collection path: %s\n", ct->cd->path);
2608                         }
2609                 }
2610
2611         if (ct->popup)
2612                 {
2613                 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2614                                                      0, 0, 0, NULL, ct);
2615                 gtk_widget_destroy(ct->popup);
2616                 }
2617
2618         if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2619
2620         tip_unschedule(ct);
2621         collection_table_scroll(ct, FALSE);
2622
2623         g_free(ct);
2624 }
2625
2626 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2627 {
2628         CollectTable *ct = data;
2629
2630         collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2631 }
2632
2633 CollectTable *collection_table_new(CollectionData *cd)
2634 {
2635         CollectTable *ct;
2636         GtkListStore *store;
2637         GtkTreeSelection *selection;
2638         gint i;
2639
2640         ct = g_new0(CollectTable, 1);
2641
2642         ct->cd = cd;
2643         ct->show_text = options->show_icon_names;
2644         ct->show_stars = options->show_star_rating;
2645
2646         ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2647         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2648         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2649                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2650
2651         store = gtk_list_store_new(1, G_TYPE_POINTER);
2652         ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2653         g_object_unref(store);
2654
2655         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2656         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2657
2658         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2659         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2660
2661         for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2662                 {
2663                 collection_table_append_column(ct, i);
2664                 }
2665
2666         /* zero width column to hide tree view focus, we draw it ourselves */
2667         collection_table_append_column(ct, i);
2668         /* end column to fill white space */
2669         collection_table_append_column(ct, i);
2670
2671         g_signal_connect(G_OBJECT(ct->listview), "destroy",
2672                          G_CALLBACK(collection_table_destroy), ct);
2673         g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2674                          G_CALLBACK(collection_table_sized), ct);
2675         g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2676                          G_CALLBACK(collection_table_press_key_cb), ct);
2677
2678         gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2679         gtk_widget_show(ct->listview);
2680
2681         collection_table_dnd_init(ct);
2682
2683         gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2684                               GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2685         g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2686                          G_CALLBACK(collection_table_press_cb), ct);
2687         g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2688                          G_CALLBACK(collection_table_release_cb), ct);
2689         g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2690                          G_CALLBACK(collection_table_motion_cb), ct);
2691         g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2692                          G_CALLBACK(collection_table_leave_cb), ct);
2693
2694         return ct;
2695 }
2696
2697 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2698 {
2699         ct->status_label = status;
2700         ct->extra_label = extra;
2701         collection_table_update_status(ct);
2702         collection_table_update_extras(ct, FALSE, 0.0);
2703 }
2704
2705 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2706 {
2707         return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2708 }
2709
2710 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */