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