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