2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
23 #include "collect-table.h"
25 #include "cellrenderericon.h"
26 #include "collect-dlg.h"
27 #include "collect-io.h"
34 #include "layout_image.h"
38 #include "ui_fileops.h"
40 #include "ui_tree_edit.h"
41 #include "uri_utils.h"
42 #include "view_file.h"
44 #include "icons/marker.xpm"
45 #define MARKER_WIDTH 26
46 #define MARKER_HEIGHT 32
48 #include <gdk/gdkkeysyms.h> /* for keyboard values */
50 /* between these, the icon width is increased by thumb_max_width / 2 */
51 #define THUMB_MIN_ICON_WIDTH 128
52 #define THUMB_MAX_ICON_WIDTH 150
54 #define COLLECT_TABLE_MAX_COLUMNS 32
55 #define THUMB_BORDER_PADDING 2
57 #define COLLECT_TABLE_TIP_DELAY 500
58 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
62 CTABLE_COLUMN_POINTER = 0,
66 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
69 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
73 *-------------------------------------------------------------------
75 *-------------------------------------------------------------------
78 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
82 n = g_list_index(ct->cd->list, info);
84 if (n < 0) return FALSE;
86 *row = n / ct->columns;
87 *col = n - (*row * ct->columns);
92 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
97 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
98 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
99 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
100 if (column) *column = col;
105 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
110 if (row < 0 || col < 0) return NULL;
112 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
113 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
117 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
118 if (!list) return NULL;
122 return g_list_nth_data(list, col);
128 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
131 GtkTreeViewColumn *column;
137 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
138 &tpath, &column, NULL, NULL))
141 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
142 gtk_tree_model_get_iter(store, &row, tpath);
143 gtk_tree_path_free(tpath);
145 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
146 if (!list) return NULL;
148 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
149 if (iter) *iter = row;
150 return g_list_nth_data(list, n);
153 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
163 CollectInfo *ci = work->data;
171 return g_list_length(ct->cd->list);
174 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
181 work = ct->selection;
184 CollectInfo *ci = work->data;
192 return g_list_length(ct->selection);
195 static void collection_table_update_status(CollectTable *ct)
203 if (!ct->status_label) return;
205 n = collection_table_list_count(ct, &n_bytes);
206 s = collection_table_selection_count(ct, &s_bytes);
210 gchar *b = text_from_size_abrev(n_bytes);
211 gchar *sb = text_from_size_abrev(s_bytes);
212 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
218 gchar *b = text_from_size_abrev(n_bytes);
219 buf = g_strdup_printf(_("%s, %d images"), b, n);
224 buf = g_strdup(_("Empty"));
227 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
231 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
235 if (!ct->extra_label) return;
238 text = _("Loading thumbs...");
242 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
243 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
246 static void collection_table_toggle_filenames(CollectTable *ct)
248 GtkAllocation allocation;
249 ct->show_text = !ct->show_text;
250 options->show_icon_names = ct->show_text;
252 gtk_widget_get_allocation(ct->listview, &allocation);
253 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
256 static gint collection_table_get_icon_width(CollectTable *ct)
260 if (!ct->show_text) return options->thumbnails.max_width;
262 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
263 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
264 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
270 *-------------------------------------------------------------------
272 *-------------------------------------------------------------------
275 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
282 if (info->flag_mask == value) return;
283 info->flag_mask = value;
285 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
288 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
289 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
295 if (collection_table_find_iter(ct, info, &row, NULL))
297 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
298 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
303 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
307 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
310 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
314 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
317 *-------------------------------------------------------------------
319 *-------------------------------------------------------------------
322 static void collection_table_verify_selections(CollectTable *ct)
326 work = ct->selection;
329 CollectInfo *info = work->data;
331 if (!g_list_find(ct->cd->list, info))
333 ct->selection = g_list_remove(ct->selection, info);
338 void collection_table_select_all(CollectTable *ct)
342 g_list_free(ct->selection);
343 ct->selection = NULL;
348 ct->selection = g_list_append(ct->selection, work->data);
349 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
353 collection_table_update_status(ct);
356 void collection_table_unselect_all(CollectTable *ct)
360 work = ct->selection;
363 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
367 g_list_free(ct->selection);
368 ct->selection = NULL;
370 collection_table_update_status(ct);
373 /* Invert the current collection's selection */
374 static void collection_table_select_invert_all(CollectTable *ct)
377 GList *new_selection = NULL;
382 CollectInfo *info = work->data;
384 if (INFO_SELECTED(info))
386 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
390 new_selection = g_list_append(new_selection, info);
391 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
398 g_list_free(ct->selection);
399 ct->selection = new_selection;
401 collection_table_update_status(ct);
404 void collection_table_select(CollectTable *ct, CollectInfo *info)
406 ct->prev_selection = info;
408 if (!info || INFO_SELECTED(info)) return;
410 ct->selection = g_list_append(ct->selection, info);
411 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
413 collection_table_update_status(ct);
416 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
418 ct->prev_selection = info;
420 if (!info || !INFO_SELECTED(info) ) return;
422 ct->selection = g_list_remove(ct->selection, info);
423 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
425 collection_table_update_status(ct);
428 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
432 collection_table_select(ct, info);
436 collection_table_unselect(ct, info);
440 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
447 if (!collection_table_find_position(ct, start, &row1, &col1) ||
448 !collection_table_find_position(ct, end, &row2, &col2) ) return;
450 ct->prev_selection = end;
452 if (!options->collections.rectangular_selection)
457 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
464 work = g_list_find(ct->cd->list, start);
468 collection_table_select_util(ct, info, select);
470 if (work->data != end)
491 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
493 for (i = row1; i <= row2; i++)
495 for (j = col1; j <= col2; j++)
497 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
498 if (info) collection_table_select_util(ct, info, select);
503 GList *collection_table_selection_get_list(CollectTable *ct)
505 return collection_list_to_filelist(ct->selection);
508 static GList *collection_table_get_list(CollectTable *ct)
510 return collection_list_to_filelist(ct->cd->list);
514 *-------------------------------------------------------------------
515 * tooltip type window
516 *-------------------------------------------------------------------
519 static void tip_show(CollectTable *ct)
523 #if GTK_CHECK_VERSION(3,0,0)
525 GdkDeviceManager *device_manager;
529 if (ct->tip_window) return;
531 #if GTK_CHECK_VERSION(3,0,0)
532 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
533 gtk_widget_get_window(ct->listview)));
534 device = gdk_device_manager_get_client_pointer(device_manager);
535 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
536 device, &x, &y, NULL);
538 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
541 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
542 if (!ct->tip_info) return;
544 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
545 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
546 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
548 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
550 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
551 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
552 gtk_widget_show(label);
554 #if GTK_CHECK_VERSION(3,0,0)
555 display = gdk_display_get_default();
556 device_manager = gdk_display_get_device_manager(display);
557 device = gdk_device_manager_get_client_pointer(device_manager);
558 gdk_device_get_position(device, NULL, &x, &y);
560 gdk_window_get_pointer(NULL, &x, &y, NULL);
563 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
564 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
565 gtk_widget_show(ct->tip_window);
568 static void tip_hide(CollectTable *ct)
570 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
571 ct->tip_window = NULL;
574 static gboolean tip_schedule_cb(gpointer data)
576 CollectTable *ct = data;
578 if (!ct->tip_delay_id) return FALSE;
582 ct->tip_delay_id = 0;
586 static void tip_schedule(CollectTable *ct)
590 if (ct->tip_delay_id)
592 g_source_remove(ct->tip_delay_id);
593 ct->tip_delay_id = 0;
596 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
599 static void tip_unschedule(CollectTable *ct)
603 if (ct->tip_delay_id)
605 g_source_remove(ct->tip_delay_id);
606 ct->tip_delay_id = 0;
610 static void tip_update(CollectTable *ct, CollectInfo *info)
612 #if GTK_CHECK_VERSION(3,0,0)
613 GdkDisplay *display = gdk_display_get_default();
614 GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
615 GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
623 #if GTK_CHECK_VERSION(3,0,0)
624 gdk_device_get_position(device, NULL, &x, &y);
626 gdk_window_get_pointer(NULL, &x, &y, NULL);
628 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
630 if (info != ct->tip_info)
641 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
642 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
648 *-------------------------------------------------------------------
650 *-------------------------------------------------------------------
653 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
655 CollectTable *ct = data;
657 collection_dialog_save_as(NULL, ct->cd);
660 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
662 CollectTable *ct = data;
666 collection_table_popup_save_as_cb(widget, data);
670 if (!collection_save(ct->cd, ct->cd->path))
672 log_printf("failed saving to collection path: %s\n", ct->cd->path);
676 static GList *collection_table_popup_file_list(CollectTable *ct)
678 if (!ct->click_info) return NULL;
680 if (INFO_SELECTED(ct->click_info))
682 return collection_table_selection_get_list(ct);
685 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
688 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
691 const gchar *key = data;
693 ct = submenu_item_get_data(widget);
697 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
700 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
702 CollectTable *ct = data;
704 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
707 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
709 CollectTable *ct = data;
711 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
714 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
716 CollectTable *ct = data;
718 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
721 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
723 CollectTable *ct = data;
725 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
728 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
730 CollectTable *ct = data;
732 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), TRUE);
735 static void collection_table_popup_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
737 CollectTable *ct = data;
739 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), FALSE);
742 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
747 ct = submenu_item_get_data(widget);
751 type = (SortType)GPOINTER_TO_INT(data);
753 collection_set_sort_method(ct->cd, type);
756 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
760 ct = submenu_item_get_data(widget);
764 collection_randomize(ct->cd);
767 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
769 CollectTable *ct = data;
771 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
773 view_window_new_from_collection(ct->cd, ct->click_info);
777 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
779 CollectTable *ct = data;
781 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
783 layout_image_set_collection(NULL, ct->cd, ct->click_info);
787 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
789 CollectTable *ct = data;
791 collection_table_select_all(ct);
792 ct->prev_selection= ct->click_info;
795 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
797 CollectTable *ct = data;
799 collection_table_unselect_all(ct);
800 ct->prev_selection= ct->click_info;
803 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
805 CollectTable *ct = data;
807 collection_table_select_invert_all(ct);
808 ct->prev_selection= ct->click_info;
811 static void collection_table_popup_rectangular_selection_cb(GtkWidget *widget, gpointer data)
813 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
816 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
818 CollectTable *ct = data;
821 if (!ct->click_info) return;
823 if (INFO_SELECTED(ct->click_info))
825 list = g_list_copy(ct->selection);
829 list = g_list_append(NULL, ct->click_info);
832 collection_remove_by_info_list(ct->cd, list);
836 static void collection_table_popup_add_file_selection_cb(GtkWidget *widget, gpointer data)
838 CollectTable *ct = data;
840 LayoutWindow *lw = NULL;
842 if (!layout_valid(&lw)) return;
844 list = vf_selection_get_list(lw->vf);
848 collection_table_add_filelist(ct, list);
853 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
855 CollectTable *ct = data;
857 collection_dialog_append(NULL, ct->cd);
860 static void collection_table_popup_goto_original_cb(GtkWidget *widget, gpointer data)
862 CollectTable *ct = data;
864 LayoutWindow *lw = NULL;
867 if (!layout_valid(&lw)) return;
868 list = collection_table_selection_get_list(ct);
874 layout_set_fd(lw, fd);
880 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
882 CollectTable *ct = data;
885 dw = dupe_window_new();
886 dupe_window_add_collection(dw, ct->cd);
889 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
891 CollectTable *ct = data;
894 fd = (ct->click_info) ? ct->click_info->fd : NULL;
896 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
899 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
901 CollectTable *ct = data;
903 collection_table_toggle_filenames(ct);
906 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
908 CollectTable *ct = data;
910 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
911 ct->click_info = NULL;
914 filelist_free(ct->drop_list);
915 ct->drop_list = NULL;
916 ct->drop_info = NULL;
918 filelist_free(ct->editmenu_fd_list);
919 ct->editmenu_fd_list = NULL;
922 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
928 menu = popup_menu_short_lived();
930 g_signal_connect(G_OBJECT(menu), "destroy",
931 G_CALLBACK(collection_table_popup_destroy_cb), ct);
933 menu_item_add_sensitive(menu, _("_View"), over_icon,
934 G_CALLBACK(collection_table_popup_view_cb), ct);
935 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
936 G_CALLBACK(collection_table_popup_view_new_cb), ct);
937 menu_item_add_stock(menu, _("Go to original"), GTK_STOCK_FIND,
938 G_CALLBACK(collection_table_popup_goto_original_cb), ct);
939 menu_item_add_divider(menu);
940 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
941 G_CALLBACK(collection_table_popup_remove_cb), ct);
943 menu_item_add_stock(menu, _("Append from file selection"), GTK_STOCK_ADD,
944 G_CALLBACK(collection_table_popup_add_file_selection_cb), ct);
945 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
946 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
947 menu_item_add_divider(menu);
949 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
950 submenu = gtk_menu_new();
951 menu_item_add(submenu, _("Select all"),
952 G_CALLBACK(collection_table_popup_selectall_cb), ct);
953 menu_item_add(submenu, _("Select none"),
954 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
955 menu_item_add(submenu, _("Invert selection"),
956 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
957 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
958 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
959 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
960 menu_item_add_divider(menu);
963 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
964 submenu_add_edit(menu, &item,
965 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
966 gtk_widget_set_sensitive(item, over_icon);
968 menu_item_add_divider(menu);
969 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
970 G_CALLBACK(collection_table_popup_copy_cb), ct);
971 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
972 G_CALLBACK(collection_table_popup_move_cb), ct);
973 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
974 G_CALLBACK(collection_table_popup_rename_cb), ct);
975 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
976 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
977 menu_item_add_sensitive(menu, _("_Copy path unquoted"), over_icon,
978 G_CALLBACK(collection_table_popup_copy_path_unquoted_cb), ct);
979 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
980 G_CALLBACK(collection_table_popup_delete_cb), ct);
981 menu_item_add_divider(menu);
983 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
984 menu_item_add_divider(submenu);
985 menu_item_add(submenu, _("Randomize"),
986 G_CALLBACK(collection_table_popup_randomize_cb), ct);
987 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
988 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
990 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
991 G_CALLBACK(collection_table_popup_show_names_cb), ct);
992 menu_item_add_divider(menu);
993 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
994 G_CALLBACK(collection_table_popup_save_cb), ct);
995 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
996 G_CALLBACK(collection_table_popup_save_as_cb), ct);
997 menu_item_add_divider(menu);
998 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
999 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
1000 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
1001 G_CALLBACK(collection_table_popup_print_cb), ct);
1006 *-------------------------------------------------------------------
1007 * keyboard callbacks
1008 *-------------------------------------------------------------------
1011 void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
1016 if (g_list_find(ct->cd->list, ct->focus_info))
1018 if (info == ct->focus_info)
1020 /* ensure focus row col are correct */
1021 collection_table_find_position(ct, ct->focus_info,
1022 &ct->focus_row, &ct->focus_column);
1025 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1028 if (!collection_table_find_position(ct, info, &row, &col))
1030 ct->focus_info = NULL;
1032 ct->focus_column = -1;
1036 ct->focus_info = info;
1037 ct->focus_row = row;
1038 ct->focus_column = col;
1039 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1041 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1044 GtkTreeViewColumn *column;
1045 GtkTreeModel *store;
1047 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1049 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1050 tpath = gtk_tree_model_get_path(store, &iter);
1051 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1052 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1053 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1054 gtk_tree_path_free(tpath);
1058 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1065 new_row = ct->focus_row;
1066 new_col = ct->focus_column;
1069 if (new_row < 0) new_row = 0;
1070 if (new_row >= ct->rows) new_row = ct->rows - 1;
1090 new_col = ct->columns - 1;
1097 if (new_col >= ct->columns)
1099 if (new_row < ct->rows - 1)
1106 new_col = ct->columns - 1;
1116 if (new_row >= ct->rows)
1119 new_row = ct->rows - 1;
1122 new_col = ct->columns - 1;
1124 if (new_col >= ct->columns) new_col = ct->columns - 1;
1127 if (new_row == ct->rows - 1)
1131 /* if we moved beyond the last image, go to the last image */
1133 l = g_list_length(ct->cd->list);
1134 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1135 if (new_col >= l) new_col = l - 1;
1138 if (new_row == -1 || new_col == -1)
1140 if (!ct->cd->list) return;
1141 new_row = new_col = 0;
1144 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1147 static void collection_table_update_focus(CollectTable *ct)
1152 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1154 /* first find the old focus, if it exists and is valid */
1158 /* (try to) stay where we were */
1159 new_row = ct->focus_row;
1160 new_col = ct->focus_column;
1163 collection_table_move_focus(ct, new_row, new_col, FALSE);
1166 /* used to figure the page up/down distances */
1167 static gint page_height(CollectTable *ct)
1174 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1175 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1177 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1178 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1180 ret = page_size / row_height;
1181 if (ret < 1) ret = 1;
1186 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1188 CollectTable *ct = data;
1189 GtkTreeModel *store;
1195 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1196 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1197 tpath = gtk_tree_model_get_path(store, &iter);
1198 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1199 gtk_tree_path_free(tpath);
1201 popup_menu_position_clamp(menu, x, y, 0);
1204 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1206 CollectTable *ct = data;
1210 gboolean stop_signal = TRUE;
1212 switch (event->keyval)
1214 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1217 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1220 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1223 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1226 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1227 focus_row = -page_height(ct);
1229 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1230 focus_row = page_height(ct);
1232 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1233 focus_row = -ct->focus_row;
1234 focus_col = -ct->focus_column;
1236 case GDK_KEY_End: case GDK_KEY_KP_End:
1237 focus_row = ct->rows - 1 - ct->focus_row;
1238 focus_col = ct->columns - 1 - ct->focus_column;
1241 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1244 ct->click_info = info;
1245 if (event->state & GDK_CONTROL_MASK)
1247 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1251 collection_table_unselect_all(ct);
1252 collection_table_select(ct, info);
1257 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1261 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1262 ct->click_info = info;
1264 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1267 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1268 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1271 stop_signal = FALSE;
1275 if (focus_row != 0 || focus_col != 0)
1277 CollectInfo *new_info;
1278 CollectInfo *old_info;
1280 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1281 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1282 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1284 if (new_info != old_info)
1286 if (event->state & GDK_SHIFT_MASK)
1288 if (!options->collections.rectangular_selection)
1290 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1294 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1296 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1298 else if (event->state & GDK_CONTROL_MASK)
1300 ct->click_info = new_info;
1304 ct->click_info = new_info;
1305 collection_table_unselect_all(ct);
1306 collection_table_select(ct, new_info);
1320 *-------------------------------------------------------------------
1322 *-------------------------------------------------------------------
1325 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1326 gboolean use_coord, gint x, gint y)
1328 CollectInfo *info = NULL;
1329 GtkTreeModel *store;
1332 GtkTreeViewColumn *column;
1333 #if GTK_CHECK_VERSION(3,0,0)
1334 GdkDeviceManager *device_manager;
1338 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1340 #if GTK_CHECK_VERSION(3,0,0)
1343 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1344 gtk_widget_get_window(ct->listview)));
1345 device = gdk_device_manager_get_client_pointer(device_manager);
1346 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1347 device, &x, &y, NULL);
1350 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1355 if (collection_table_find_iter(ct, source, &iter, &col))
1357 tpath = gtk_tree_model_get_path(store, &iter);
1358 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1359 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1360 gtk_tree_path_free(tpath);
1363 *after = !!(x > cell->x + (cell->width / 2));
1368 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1369 &tpath, &column, NULL, NULL))
1374 gtk_tree_model_get_iter(store, &iter, tpath);
1375 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1377 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1378 info = g_list_nth_data(list, n);
1382 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1383 *after = !!(x > cell->x + (cell->width / 2));
1386 gtk_tree_path_free(tpath);
1393 work = g_list_last(ct->cd->list);
1401 if (collection_table_find_iter(ct, info, &iter, &col))
1403 tpath = gtk_tree_model_get_path(store, &iter);
1404 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1405 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1406 gtk_tree_path_free(tpath);
1414 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1418 gboolean after = FALSE;
1420 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1426 work = g_list_find(ct->cd->list, info);
1427 if (work && work->next)
1429 info = work->next->data;
1440 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1442 gboolean after = FALSE;
1447 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1448 ct->marker_window = NULL;
1453 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1455 /* this setting does not take into account (after), but since it is not really used... */
1456 ct->marker_info = info;
1458 if (!ct->marker_window)
1460 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1461 GdkWindowAttr attributes;
1462 gint attributes_mask;
1463 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1464 gint w = gdk_pixbuf_get_width(pb);
1465 gint h = gdk_pixbuf_get_height(pb);
1467 attributes.window_type = GDK_WINDOW_CHILD;
1468 attributes.wclass = GDK_INPUT_OUTPUT;
1469 attributes.width = w;
1470 attributes.height = h;
1471 attributes.event_mask = gtk_widget_get_events(ct->listview);
1472 attributes_mask = 0;
1474 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1476 #if GTK_CHECK_VERSION(3,0,0)
1477 cairo_region_t *mask;
1478 cairo_pattern_t *pattern;
1479 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1480 cairo_t *cr = cairo_create(img);
1481 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1483 pattern = cairo_pattern_create_for_surface(img);
1484 mask = gdk_cairo_region_create_from_surface(img);
1485 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1486 gdk_window_set_background_pattern(ct->marker_window, pattern);
1487 cairo_region_destroy(mask);
1488 cairo_pattern_destroy(pattern);
1490 cairo_surface_destroy(img);
1494 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1496 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1497 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1499 g_object_unref(pixmap);
1500 if (mask) g_object_unref(mask);
1510 w = gdk_window_get_width(ct->marker_window);
1511 h = gdk_window_get_height(ct->marker_window);
1519 x = cell.x + cell.width;
1522 y = cell.y + (cell.height / 2) - (h / 2);
1524 gdk_window_move(ct->marker_window, x, y);
1525 #if !GTK_CHECK_VERSION(3,0,0)
1526 gdk_window_clear(ct->marker_window);
1528 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1532 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1537 *-------------------------------------------------------------------
1538 * mouse drag auto-scroll
1539 *-------------------------------------------------------------------
1542 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1546 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1551 collection_table_insert_marker(ct, info, TRUE);
1555 tip_update(ct, info);
1559 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1561 CollectTable *ct = data;
1565 #if GTK_CHECK_VERSION(3,0,0)
1566 GdkDeviceManager *device_manager;
1570 if (!ct->drop_idle_id) return FALSE;
1572 window = gtk_widget_get_window(ct->listview);
1573 #if GTK_CHECK_VERSION(3,0,0)
1574 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1575 device = gdk_device_manager_get_client_pointer(device_manager);
1576 gdk_window_get_device_position(window, device, &x, &y, NULL);
1578 gdk_window_get_pointer(window, &x, &y, NULL);
1580 w = gdk_window_get_width(window);
1581 h = gdk_window_get_height(window);
1582 if (x >= 0 && x < w && y >= 0 && y < h)
1584 collection_table_motion_update(ct, x, y, TRUE);
1587 ct->drop_idle_id = 0;
1591 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1593 CollectTable *ct = data;
1595 if (!ct->drop_idle_id)
1597 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1603 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1607 if (ct->drop_idle_id)
1609 g_source_remove(ct->drop_idle_id);
1610 ct->drop_idle_id = 0;
1612 widget_auto_scroll_stop(ct->listview);
1613 collection_table_insert_marker(ct, NULL, FALSE);
1617 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1618 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1619 collection_table_auto_scroll_notify_cb, ct);
1624 *-------------------------------------------------------------------
1626 *-------------------------------------------------------------------
1629 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1631 CollectTable *ct = data;
1633 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1638 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1640 CollectTable *ct = data;
1646 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1648 ct->click_info = info;
1649 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1651 switch (bevent->button)
1653 case MOUSE_BUTTON_LEFT:
1654 if (bevent->type == GDK_2BUTTON_PRESS)
1658 layout_image_set_collection(NULL, ct->cd, info);
1661 else if (!gtk_widget_has_focus(ct->listview))
1663 gtk_widget_grab_focus(ct->listview);
1666 case MOUSE_BUTTON_RIGHT:
1667 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1668 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1677 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1679 CollectTable *ct = data;
1681 CollectInfo *info = NULL;
1685 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1687 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1692 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1695 if (bevent->button == MOUSE_BUTTON_LEFT &&
1696 info && ct->click_info == info)
1698 collection_table_set_focus(ct, info);
1700 if (bevent->state & GDK_CONTROL_MASK)
1702 gboolean select = !INFO_SELECTED(info);
1704 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1706 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1710 collection_table_select_util(ct, info, select);
1715 collection_table_unselect_all(ct);
1717 if ((bevent->state & GDK_SHIFT_MASK) &&
1720 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1724 collection_table_select_util(ct, info, TRUE);
1728 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1729 info && ct->click_info == info)
1731 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1737 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1739 CollectTable *ct = data;
1746 *-------------------------------------------------------------------
1747 * populate, add, insert, etc.
1748 *-------------------------------------------------------------------
1751 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1755 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1761 static void collection_table_clear_store(CollectTable *ct)
1763 GtkTreeModel *store;
1765 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1766 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1768 gtk_list_store_clear(GTK_LIST_STORE(store));
1771 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1773 GtkListStore *store;
1777 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1779 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1780 gtk_list_store_append(store, iter);
1781 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1786 static void collection_table_populate(CollectTable *ct, gboolean resize)
1791 collection_table_verify_selections(ct);
1793 collection_table_clear_store(ct);
1800 thumb_width = collection_table_get_icon_width(ct);
1802 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1804 GtkTreeViewColumn *column;
1805 GtkCellRenderer *cell;
1808 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1809 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1810 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1812 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1813 cell = (list) ? list->data : NULL;
1816 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1818 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1819 "fixed_height", options->thumbnails.max_height,
1820 "show_text", ct->show_text, NULL);
1823 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1827 work = ct->cd->list;
1835 list = collection_table_add_row(ct, &iter);
1836 while (work && list)
1838 list->data = work->data;
1846 collection_table_update_focus(ct);
1847 collection_table_update_status(ct);
1850 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1855 thumb_width = collection_table_get_icon_width(ct);
1857 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1858 if (new_cols < 1) new_cols = 1;
1860 if (!force && new_cols == ct->columns) return;
1862 ct->columns = new_cols;
1864 collection_table_populate(ct, TRUE);
1866 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1869 static void collection_table_sync(CollectTable *ct)
1871 GtkTreeModel *store;
1876 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1881 work = ct->cd->list;
1887 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1889 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1890 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1894 list = collection_table_add_row(ct, &iter);
1919 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1923 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1924 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1930 collection_table_update_focus(ct);
1931 collection_table_update_status(ct);
1934 static gboolean collection_table_sync_idle_cb(gpointer data)
1936 CollectTable *ct = data;
1938 if (!ct->sync_idle_id) return FALSE;
1939 g_source_remove(ct->sync_idle_id);
1940 ct->sync_idle_id = 0;
1942 collection_table_sync(ct);
1946 static void collection_table_sync_idle(CollectTable *ct)
1948 if (!ct->sync_idle_id)
1950 /* high priority, the view needs to be resynced before a redraw
1951 * may contain invalid pointers at this time
1953 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1957 void collection_table_add_filelist(CollectTable *ct, GList *list)
1966 collection_add(ct->cd, (FileData *)work->data, FALSE);
1971 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1980 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1984 collection_table_sync_idle(ct);
1987 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1990 GList *insert_pos = NULL;
1994 if (!info_list) return;
1996 info = collection_table_find_data(ct, row, col, NULL);
1998 if (!info_list->next && info_list->data == info) return;
2000 if (info) insert_pos = g_list_find(ct->cd->list, info);
2002 /* FIXME: this may get slow for large lists */
2004 while (insert_pos && work)
2006 if (insert_pos->data == work->data)
2008 insert_pos = insert_pos->next;
2020 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2024 /* place them back in */
2025 temp = g_list_copy(info_list);
2029 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2033 ct->cd->list = g_list_concat(temp, ct->cd->list);
2037 ct->cd->list = g_list_concat(ct->cd->list, temp);
2040 ct->cd->changed = TRUE;
2042 collection_table_sync_idle(ct);
2047 *-------------------------------------------------------------------
2049 *-------------------------------------------------------------------
2052 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2060 collection_table_update_extras(ct, FALSE, 0.0);
2064 if (!collection_table_find_position(ct, info, &row, &col)) return;
2066 if (ct->columns != 0 && ct->rows != 0)
2068 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2075 collection_table_update_extras(ct, TRUE, value);
2077 if (collection_table_find_iter(ct, info, &iter, NULL))
2079 GtkTreeModel *store;
2082 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2083 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2084 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2088 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2090 collection_table_sync_idle(ct);
2093 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2095 collection_table_sync_idle(ct);
2098 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2100 if (ci && INFO_SELECTED(ci))
2102 ct->selection = g_list_remove(ct->selection, ci);
2105 collection_table_sync_idle(ct);
2108 void collection_table_refresh(CollectTable *ct)
2110 collection_table_populate(ct, FALSE);
2114 *-------------------------------------------------------------------
2116 *-------------------------------------------------------------------
2119 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2125 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2128 f = filelist_filter(f, FALSE);
2129 d = filelist_filter(d, TRUE);
2131 f = filelist_sort_path(f);
2132 d = filelist_sort_path(d);
2134 collection_table_insert_filelist(ct, f, ct->marker_info);
2136 work = g_list_last(d);
2139 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2147 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2152 FileData *fd = work->data;
2154 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2156 collection_table_insert_filelist(ct, list, ct->marker_info);
2160 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2162 CollectTable *ct = data;
2164 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2167 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2169 CollectTable *ct = data;
2171 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2174 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2176 CollectTable *ct = data;
2178 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2181 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2185 menu = popup_menu_short_lived();
2186 g_signal_connect(G_OBJECT(menu), "destroy",
2187 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2189 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2190 menu_item_add_divider(menu);
2191 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2192 G_CALLBACK(confirm_dir_list_add), ct);
2193 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2194 G_CALLBACK(confirm_dir_list_recurse), ct);
2195 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2196 G_CALLBACK(confirm_dir_list_skip), ct);
2197 menu_item_add_divider(menu);
2198 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2204 *-------------------------------------------------------------------
2206 *-------------------------------------------------------------------
2209 static GtkTargetEntry collection_drag_types[] = {
2210 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2211 { "text/uri-list", 0, TARGET_URI_LIST },
2212 { "text/plain", 0, TARGET_TEXT_PLAIN }
2214 static gint n_collection_drag_types = 3;
2216 static GtkTargetEntry collection_drop_types[] = {
2217 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2218 { "text/uri-list", 0, TARGET_URI_LIST }
2220 static gint n_collection_drop_types = 2;
2223 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2224 GtkSelectionData *selection_data, guint info,
2225 guint time, gpointer data)
2227 CollectTable *ct = data;
2230 gchar *uri_text = NULL;
2233 if (!ct->click_info) return;
2235 selected = INFO_SELECTED(ct->click_info);
2239 case TARGET_APP_COLLECTION_MEMBER:
2242 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2246 list = g_list_append(NULL, ct->click_info);
2247 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2250 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2251 8, (guchar *)uri_text, total);
2254 case TARGET_URI_LIST:
2255 case TARGET_TEXT_PLAIN:
2259 list = collection_table_selection_get_list(ct);
2263 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2267 uri_selection_data_set_uris_from_filelist(selection_data, list);
2268 filelist_free(list);
2274 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2276 GtkSelectionData *selection_data, guint info,
2277 guint time, gpointer data)
2279 CollectTable *ct = data;
2281 GList *info_list = NULL;
2282 CollectionData *source;
2283 CollectInfo *drop_info;
2286 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2288 collection_table_scroll(ct, FALSE);
2289 collection_table_insert_marker(ct, NULL, FALSE);
2291 drop_info = collection_table_insert_point(ct, x, y);
2295 case TARGET_APP_COLLECTION_MEMBER:
2296 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2299 if (source == ct->cd)
2304 /* it is a move within a collection */
2305 filelist_free(list);
2310 collection_table_move_by_info_list(ct, info_list, -1, -1);
2312 else if (collection_table_find_position(ct, drop_info, &row, &col))
2314 collection_table_move_by_info_list(ct, info_list, row, col);
2319 /* it is a move/copy across collections */
2320 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2322 collection_remove_by_info_list(source, info_list);
2325 g_list_free(info_list);
2328 case TARGET_URI_LIST:
2329 list = uri_filelist_from_gtk_selection_data(selection_data);
2333 FileData *fd = work->data;
2334 if (isdir(fd->path))
2338 ct->drop_list = list;
2339 ct->drop_info = drop_info;
2340 menu = collection_table_drop_menu(ct);
2341 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2354 collection_table_insert_filelist(ct, list, drop_info);
2355 filelist_free(list);
2359 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2361 CollectTable *ct = data;
2363 if (ct->click_info && ct->click_info->pixbuf)
2367 if (INFO_SELECTED(ct->click_info))
2368 items = g_list_length(ct->selection);
2371 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2375 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2377 CollectTable *ct = data;
2379 /* apparently a leave event is not generated on a drop */
2382 collection_table_scroll(ct, FALSE);
2385 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2386 gint x, gint y, guint time, gpointer data)
2388 CollectTable *ct = data;
2390 collection_table_motion_update(ct, x, y, TRUE);
2391 collection_table_scroll(ct, TRUE);
2396 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2398 CollectTable *ct = data;
2400 collection_table_scroll(ct, FALSE);
2403 static void collection_table_dnd_init(CollectTable *ct)
2405 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2406 collection_drag_types, n_collection_drag_types,
2407 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2408 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2409 G_CALLBACK(collection_table_dnd_get), ct);
2410 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2411 G_CALLBACK(collection_table_dnd_begin), ct);
2412 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2413 G_CALLBACK(collection_table_dnd_end), ct);
2415 gtk_drag_dest_set(ct->listview,
2416 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2417 collection_drop_types, n_collection_drop_types,
2418 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2419 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2420 G_CALLBACK(collection_table_dnd_motion), ct);
2421 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2422 G_CALLBACK(collection_table_dnd_leave), ct);
2423 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2424 G_CALLBACK(collection_table_dnd_receive), ct);
2428 *-----------------------------------------------------------------------------
2430 *-----------------------------------------------------------------------------
2433 typedef struct _ColumnData ColumnData;
2440 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2441 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2443 ColumnData *cd = data;
2453 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2455 #if GTK_CHECK_VERSION(3,0,0)
2456 /* FIXME this is a primitive hack to stop a crash.
2457 * When compiled with GTK3, if a Collection window containing
2458 * say, 50 or so, images has its width changed, there is a segfault
2459 * https://github.com/BestImageViewer/geeqie/issues/531
2461 if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2463 info = g_list_nth_data(list, cd->number);
2465 style = gtk_widget_get_style(ct->listview);
2466 if (info && (info->flag_mask & SELECTION_SELECTED) )
2468 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2469 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2473 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2474 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2477 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2479 shift_color(&color_bg, -1, 0);
2482 if (GQV_IS_CELL_RENDERER_ICON(cell))
2486 g_object_set(cell, "pixbuf", info->pixbuf,
2487 "text", info->fd->name,
2488 "cell-background-gdk", &color_bg,
2489 "cell-background-set", TRUE,
2490 "foreground-gdk", &color_fg,
2491 "foreground-set", TRUE,
2492 "has-focus", (ct->focus_info == info), NULL);
2496 g_object_set(cell, "pixbuf", NULL,
2498 "cell-background-set", FALSE,
2499 "foreground-set", FALSE,
2500 "has-focus", FALSE, NULL);
2505 static void collection_table_append_column(CollectTable *ct, gint n)
2508 GtkTreeViewColumn *column;
2509 GtkCellRenderer *renderer;
2511 column = gtk_tree_view_column_new();
2512 gtk_tree_view_column_set_min_width(column, 0);
2514 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2515 gtk_tree_view_column_set_alignment(column, 0.5);
2517 renderer = gqv_cell_renderer_icon_new();
2518 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2519 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2520 "ypad", THUMB_BORDER_PADDING,
2521 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2523 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2525 cd = g_new0(ColumnData, 1);
2528 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2530 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2534 *-------------------------------------------------------------------
2536 *-------------------------------------------------------------------
2539 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2541 CollectTable *ct = data;
2543 /* If there is no unsaved data, save the window geometry
2545 if (!ct->cd->changed)
2547 if (!collection_save(ct->cd, ct->cd->path))
2549 log_printf("failed saving to collection path: %s\n", ct->cd->path);
2555 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2557 gtk_widget_destroy(ct->popup);
2560 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2563 collection_table_scroll(ct, FALSE);
2568 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2570 CollectTable *ct = data;
2572 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2575 CollectTable *collection_table_new(CollectionData *cd)
2578 GtkListStore *store;
2579 GtkTreeSelection *selection;
2582 ct = g_new0(CollectTable, 1);
2585 ct->show_text = options->show_icon_names;
2587 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2588 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2589 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2590 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2592 store = gtk_list_store_new(1, G_TYPE_POINTER);
2593 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2594 g_object_unref(store);
2596 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2597 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2599 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2600 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2602 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2604 collection_table_append_column(ct, i);
2607 /* zero width column to hide tree view focus, we draw it ourselves */
2608 collection_table_append_column(ct, i);
2609 /* end column to fill white space */
2610 collection_table_append_column(ct, i);
2612 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2613 G_CALLBACK(collection_table_destroy), ct);
2614 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2615 G_CALLBACK(collection_table_sized), ct);
2616 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2617 G_CALLBACK(collection_table_press_key_cb), ct);
2619 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2620 gtk_widget_show(ct->listview);
2622 collection_table_dnd_init(ct);
2624 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2625 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2626 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2627 G_CALLBACK(collection_table_press_cb), ct);
2628 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2629 G_CALLBACK(collection_table_release_cb), ct);
2630 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2631 G_CALLBACK(collection_table_motion_cb), ct);
2632 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2633 G_CALLBACK(collection_table_leave_cb), ct);
2638 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2640 ct->status_label = status;
2641 ct->extra_label = extra;
2642 collection_table_update_status(ct);
2643 collection_table_update_extras(ct, FALSE, 0.0);
2646 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2648 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2651 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */