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