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