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