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