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