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