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