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