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