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