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