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