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