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