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