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