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