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