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"
39 #include "ui_fileops.h"
41 #include "ui_tree_edit.h"
42 #include "uri_utils.h"
43 #include "view_file.h"
45 #include "icons/marker.xpm"
46 #define MARKER_WIDTH 26
47 #define MARKER_HEIGHT 32
49 #include <gdk/gdkkeysyms.h> /* for keyboard values */
51 /* between these, the icon width is increased by thumb_max_width / 2 */
52 #define THUMB_MIN_ICON_WIDTH 128
53 #define THUMB_MAX_ICON_WIDTH 150
55 #define COLLECT_TABLE_MAX_COLUMNS 32
56 #define THUMB_BORDER_PADDING 2
58 #define COLLECT_TABLE_TIP_DELAY 500
59 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
63 CTABLE_COLUMN_POINTER = 0,
67 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
70 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
74 *-------------------------------------------------------------------
76 *-------------------------------------------------------------------
79 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
83 n = g_list_index(ct->cd->list, info);
85 if (n < 0) return FALSE;
87 *row = n / ct->columns;
88 *col = n - (*row * ct->columns);
93 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
98 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
99 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
100 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
101 if (column) *column = col;
106 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
111 if (row < 0 || col < 0) return NULL;
113 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
114 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
118 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
119 if (!list) return NULL;
123 return g_list_nth_data(list, col);
129 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
132 GtkTreeViewColumn *column;
138 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
139 &tpath, &column, NULL, NULL))
142 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
143 gtk_tree_model_get_iter(store, &row, tpath);
144 gtk_tree_path_free(tpath);
146 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
147 if (!list) return NULL;
149 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
150 if (iter) *iter = row;
151 return g_list_nth_data(list, n);
154 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
164 CollectInfo *ci = work->data;
172 return g_list_length(ct->cd->list);
175 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
182 work = ct->selection;
185 CollectInfo *ci = work->data;
193 return g_list_length(ct->selection);
196 static void collection_table_update_status(CollectTable *ct)
204 if (!ct->status_label) return;
206 n = collection_table_list_count(ct, &n_bytes);
207 s = collection_table_selection_count(ct, &s_bytes);
211 gchar *b = text_from_size_abrev(n_bytes);
212 gchar *sb = text_from_size_abrev(s_bytes);
213 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
219 gchar *b = text_from_size_abrev(n_bytes);
220 buf = g_strdup_printf(_("%s, %d images"), b, n);
225 buf = g_strdup(_("Empty"));
228 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
232 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
236 if (!ct->extra_label) return;
239 text = _("Loading thumbs...");
243 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
244 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
247 static void collection_table_toggle_filenames(CollectTable *ct)
249 GtkAllocation allocation;
250 ct->show_text = !ct->show_text;
251 options->show_icon_names = ct->show_text;
253 gtk_widget_get_allocation(ct->listview, &allocation);
254 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
257 static void collection_table_toggle_stars(CollectTable *ct)
259 GtkAllocation allocation;
260 ct->show_stars = !ct->show_stars;
261 options->show_star_rating = ct->show_stars;
263 gtk_widget_get_allocation(ct->listview, &allocation);
264 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
267 static gint collection_table_get_icon_width(CollectTable *ct)
271 if (!ct->show_text) return options->thumbnails.max_width;
273 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
274 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
275 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
281 *-------------------------------------------------------------------
283 *-------------------------------------------------------------------
286 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
293 if (info->flag_mask == value) return;
294 info->flag_mask = value;
296 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
299 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
300 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
306 if (collection_table_find_iter(ct, info, &row, NULL))
308 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
309 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
314 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
318 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
321 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
325 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
328 *-------------------------------------------------------------------
330 *-------------------------------------------------------------------
333 static void collection_table_verify_selections(CollectTable *ct)
337 work = ct->selection;
340 CollectInfo *info = work->data;
342 if (!g_list_find(ct->cd->list, info))
344 ct->selection = g_list_remove(ct->selection, info);
349 void collection_table_select_all(CollectTable *ct)
353 g_list_free(ct->selection);
354 ct->selection = NULL;
359 ct->selection = g_list_append(ct->selection, work->data);
360 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
364 collection_table_update_status(ct);
367 void collection_table_unselect_all(CollectTable *ct)
371 work = ct->selection;
374 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
378 g_list_free(ct->selection);
379 ct->selection = NULL;
381 collection_table_update_status(ct);
384 /* Invert the current collection's selection */
385 static void collection_table_select_invert_all(CollectTable *ct)
388 GList *new_selection = NULL;
393 CollectInfo *info = work->data;
395 if (INFO_SELECTED(info))
397 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
401 new_selection = g_list_append(new_selection, info);
402 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
409 g_list_free(ct->selection);
410 ct->selection = new_selection;
412 collection_table_update_status(ct);
415 void collection_table_select(CollectTable *ct, CollectInfo *info)
417 ct->prev_selection = info;
419 if (!info || INFO_SELECTED(info)) return;
421 ct->selection = g_list_append(ct->selection, info);
422 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
424 collection_table_update_status(ct);
427 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
429 ct->prev_selection = info;
431 if (!info || !INFO_SELECTED(info) ) return;
433 ct->selection = g_list_remove(ct->selection, info);
434 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
436 collection_table_update_status(ct);
439 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
443 collection_table_select(ct, info);
447 collection_table_unselect(ct, info);
451 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
458 if (!collection_table_find_position(ct, start, &row1, &col1) ||
459 !collection_table_find_position(ct, end, &row2, &col2) ) return;
461 ct->prev_selection = end;
463 if (!options->collections.rectangular_selection)
468 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
475 work = g_list_find(ct->cd->list, start);
479 collection_table_select_util(ct, info, select);
481 if (work->data != end)
502 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
504 for (i = row1; i <= row2; i++)
506 for (j = col1; j <= col2; j++)
508 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
509 if (info) collection_table_select_util(ct, info, select);
514 GList *collection_table_selection_get_list(CollectTable *ct)
516 return collection_list_to_filelist(ct->selection);
519 static GList *collection_table_get_list(CollectTable *ct)
521 return collection_list_to_filelist(ct->cd->list);
525 *-------------------------------------------------------------------
526 * tooltip type window
527 *-------------------------------------------------------------------
530 static void tip_show(CollectTable *ct)
534 #if GTK_CHECK_VERSION(3,0,0)
536 GdkDeviceManager *device_manager;
540 if (ct->tip_window) return;
542 #if GTK_CHECK_VERSION(3,0,0)
543 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
544 gtk_widget_get_window(ct->listview)));
545 device = gdk_device_manager_get_client_pointer(device_manager);
546 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
547 device, &x, &y, NULL);
549 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
552 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
553 if (!ct->tip_info) return;
555 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
556 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
557 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
559 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
561 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
562 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
563 gtk_widget_show(label);
565 #if GTK_CHECK_VERSION(3,0,0)
566 display = gdk_display_get_default();
567 device_manager = gdk_display_get_device_manager(display);
568 device = gdk_device_manager_get_client_pointer(device_manager);
569 gdk_device_get_position(device, NULL, &x, &y);
571 gdk_window_get_pointer(NULL, &x, &y, NULL);
574 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
575 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
576 gtk_widget_show(ct->tip_window);
579 static void tip_hide(CollectTable *ct)
581 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
582 ct->tip_window = NULL;
585 static gboolean tip_schedule_cb(gpointer data)
587 CollectTable *ct = data;
589 if (!ct->tip_delay_id) return FALSE;
593 ct->tip_delay_id = 0;
597 static void tip_schedule(CollectTable *ct)
601 if (ct->tip_delay_id)
603 g_source_remove(ct->tip_delay_id);
604 ct->tip_delay_id = 0;
607 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
610 static void tip_unschedule(CollectTable *ct)
614 if (ct->tip_delay_id)
616 g_source_remove(ct->tip_delay_id);
617 ct->tip_delay_id = 0;
621 static void tip_update(CollectTable *ct, CollectInfo *info)
623 #if GTK_CHECK_VERSION(3,0,0)
624 GdkDisplay *display = gdk_display_get_default();
625 GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
626 GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
634 #if GTK_CHECK_VERSION(3,0,0)
635 gdk_device_get_position(device, NULL, &x, &y);
637 gdk_window_get_pointer(NULL, &x, &y, NULL);
639 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
641 if (info != ct->tip_info)
652 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
653 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
659 *-------------------------------------------------------------------
661 *-------------------------------------------------------------------
664 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
666 CollectTable *ct = data;
668 collection_dialog_save_as(NULL, ct->cd);
671 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
673 CollectTable *ct = data;
677 collection_table_popup_save_as_cb(widget, data);
681 if (!collection_save(ct->cd, ct->cd->path))
683 log_printf("failed saving to collection path: %s\n", ct->cd->path);
687 static GList *collection_table_popup_file_list(CollectTable *ct)
689 if (!ct->click_info) return NULL;
691 if (INFO_SELECTED(ct->click_info))
693 return collection_table_selection_get_list(ct);
696 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
699 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
702 const gchar *key = data;
704 ct = submenu_item_get_data(widget);
708 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
711 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
713 CollectTable *ct = data;
715 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
718 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
720 CollectTable *ct = data;
722 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
725 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
727 CollectTable *ct = data;
729 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
732 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
734 CollectTable *ct = data;
736 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
739 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
741 CollectTable *ct = data;
743 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), TRUE);
746 static void collection_table_popup_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
748 CollectTable *ct = data;
750 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), FALSE);
753 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
758 ct = submenu_item_get_data(widget);
762 type = (SortType)GPOINTER_TO_INT(data);
764 collection_set_sort_method(ct->cd, type);
767 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
771 ct = submenu_item_get_data(widget);
775 collection_randomize(ct->cd);
778 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
780 CollectTable *ct = data;
782 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
784 view_window_new_from_collection(ct->cd, ct->click_info);
788 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
790 CollectTable *ct = data;
792 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
794 layout_image_set_collection(NULL, ct->cd, ct->click_info);
798 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
800 CollectTable *ct = data;
802 collection_table_select_all(ct);
803 ct->prev_selection= ct->click_info;
806 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
808 CollectTable *ct = data;
810 collection_table_unselect_all(ct);
811 ct->prev_selection= ct->click_info;
814 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
816 CollectTable *ct = data;
818 collection_table_select_invert_all(ct);
819 ct->prev_selection= ct->click_info;
822 static void collection_table_popup_rectangular_selection_cb(GtkWidget *widget, gpointer data)
824 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
827 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
829 CollectTable *ct = data;
832 if (!ct->click_info) return;
834 if (INFO_SELECTED(ct->click_info))
836 list = g_list_copy(ct->selection);
840 list = g_list_append(NULL, ct->click_info);
843 collection_remove_by_info_list(ct->cd, list);
847 static void collection_table_popup_add_file_selection_cb(GtkWidget *widget, gpointer data)
849 CollectTable *ct = data;
851 LayoutWindow *lw = NULL;
853 if (!layout_valid(&lw)) return;
855 list = vf_selection_get_list(lw->vf);
859 collection_table_add_filelist(ct, list);
864 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
866 CollectTable *ct = data;
868 collection_dialog_append(NULL, ct->cd);
871 static void collection_table_popup_goto_original_cb(GtkWidget *widget, gpointer data)
873 CollectTable *ct = data;
875 LayoutWindow *lw = NULL;
878 if (!layout_valid(&lw)) return;
879 list = collection_table_selection_get_list(ct);
885 layout_set_fd(lw, fd);
891 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
893 CollectTable *ct = data;
896 dw = dupe_window_new();
897 dupe_window_add_collection(dw, ct->cd);
900 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
902 CollectTable *ct = data;
905 fd = (ct->click_info) ? ct->click_info->fd : NULL;
907 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
910 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
912 CollectTable *ct = data;
914 collection_table_toggle_filenames(ct);
917 static void collection_table_popup_show_stars_cb(GtkWidget *widget, gpointer data)
919 CollectTable *ct = data;
921 collection_table_toggle_stars(ct);
924 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
926 CollectTable *ct = data;
928 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
929 ct->click_info = NULL;
932 filelist_free(ct->drop_list);
933 ct->drop_list = NULL;
934 ct->drop_info = NULL;
936 filelist_free(ct->editmenu_fd_list);
937 ct->editmenu_fd_list = NULL;
940 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
946 menu = popup_menu_short_lived();
948 g_signal_connect(G_OBJECT(menu), "destroy",
949 G_CALLBACK(collection_table_popup_destroy_cb), ct);
951 menu_item_add_sensitive(menu, _("_View"), over_icon,
952 G_CALLBACK(collection_table_popup_view_cb), ct);
953 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
954 G_CALLBACK(collection_table_popup_view_new_cb), ct);
955 menu_item_add_stock(menu, _("Go to original"), GTK_STOCK_FIND,
956 G_CALLBACK(collection_table_popup_goto_original_cb), ct);
957 menu_item_add_divider(menu);
958 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
959 G_CALLBACK(collection_table_popup_remove_cb), ct);
961 menu_item_add_stock(menu, _("Append from file selection"), GTK_STOCK_ADD,
962 G_CALLBACK(collection_table_popup_add_file_selection_cb), ct);
963 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
964 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
965 menu_item_add_divider(menu);
967 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
968 submenu = gtk_menu_new();
969 menu_item_add(submenu, _("Select all"),
970 G_CALLBACK(collection_table_popup_selectall_cb), ct);
971 menu_item_add(submenu, _("Select none"),
972 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
973 menu_item_add(submenu, _("Invert selection"),
974 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
975 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
976 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
977 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
978 menu_item_add_divider(menu);
981 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
982 submenu_add_edit(menu, &item,
983 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
984 gtk_widget_set_sensitive(item, over_icon);
986 menu_item_add_divider(menu);
987 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
988 G_CALLBACK(collection_table_popup_copy_cb), ct);
989 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
990 G_CALLBACK(collection_table_popup_move_cb), ct);
991 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
992 G_CALLBACK(collection_table_popup_rename_cb), ct);
993 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
994 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
995 menu_item_add_sensitive(menu, _("_Copy path unquoted"), over_icon,
996 G_CALLBACK(collection_table_popup_copy_path_unquoted_cb), ct);
997 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
998 G_CALLBACK(collection_table_popup_delete_cb), ct);
999 menu_item_add_divider(menu);
1001 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
1002 menu_item_add_divider(submenu);
1003 menu_item_add(submenu, _("Randomize"),
1004 G_CALLBACK(collection_table_popup_randomize_cb), ct);
1005 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
1006 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1008 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
1009 G_CALLBACK(collection_table_popup_show_names_cb), ct);
1010 menu_item_add_check(menu, _("Show star rating"), ct->show_stars,
1011 G_CALLBACK(collection_table_popup_show_stars_cb), ct);
1012 menu_item_add_divider(menu);
1013 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
1014 G_CALLBACK(collection_table_popup_save_cb), ct);
1015 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
1016 G_CALLBACK(collection_table_popup_save_as_cb), ct);
1017 menu_item_add_divider(menu);
1018 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
1019 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
1020 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
1021 G_CALLBACK(collection_table_popup_print_cb), ct);
1026 *-------------------------------------------------------------------
1027 * keyboard callbacks
1028 *-------------------------------------------------------------------
1031 void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
1036 if (g_list_find(ct->cd->list, ct->focus_info))
1038 if (info == ct->focus_info)
1040 /* ensure focus row col are correct */
1041 collection_table_find_position(ct, ct->focus_info,
1042 &ct->focus_row, &ct->focus_column);
1045 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1048 if (!collection_table_find_position(ct, info, &row, &col))
1050 ct->focus_info = NULL;
1052 ct->focus_column = -1;
1056 ct->focus_info = info;
1057 ct->focus_row = row;
1058 ct->focus_column = col;
1059 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1061 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1064 GtkTreeViewColumn *column;
1065 GtkTreeModel *store;
1067 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1069 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1070 tpath = gtk_tree_model_get_path(store, &iter);
1071 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1072 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1073 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1074 gtk_tree_path_free(tpath);
1078 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1085 new_row = ct->focus_row;
1086 new_col = ct->focus_column;
1089 if (new_row < 0) new_row = 0;
1090 if (new_row >= ct->rows) new_row = ct->rows - 1;
1110 new_col = ct->columns - 1;
1117 if (new_col >= ct->columns)
1119 if (new_row < ct->rows - 1)
1126 new_col = ct->columns - 1;
1136 if (new_row >= ct->rows)
1139 new_row = ct->rows - 1;
1142 new_col = ct->columns - 1;
1144 if (new_col >= ct->columns) new_col = ct->columns - 1;
1147 if (new_row == ct->rows - 1)
1151 /* if we moved beyond the last image, go to the last image */
1153 l = g_list_length(ct->cd->list);
1154 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1155 if (new_col >= l) new_col = l - 1;
1158 if (new_row == -1 || new_col == -1)
1160 if (!ct->cd->list) return;
1161 new_row = new_col = 0;
1164 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1167 static void collection_table_update_focus(CollectTable *ct)
1172 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1174 /* first find the old focus, if it exists and is valid */
1178 /* (try to) stay where we were */
1179 new_row = ct->focus_row;
1180 new_col = ct->focus_column;
1183 collection_table_move_focus(ct, new_row, new_col, FALSE);
1186 /* used to figure the page up/down distances */
1187 static gint page_height(CollectTable *ct)
1194 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1195 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1197 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1198 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1200 ret = page_size / row_height;
1201 if (ret < 1) ret = 1;
1206 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1208 CollectTable *ct = data;
1209 GtkTreeModel *store;
1215 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1216 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1217 tpath = gtk_tree_model_get_path(store, &iter);
1218 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1219 gtk_tree_path_free(tpath);
1221 popup_menu_position_clamp(menu, x, y, 0);
1224 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1226 CollectTable *ct = data;
1230 gboolean stop_signal = TRUE;
1232 switch (event->keyval)
1234 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1237 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1240 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1243 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1246 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1247 focus_row = -page_height(ct);
1249 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1250 focus_row = page_height(ct);
1252 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1253 focus_row = -ct->focus_row;
1254 focus_col = -ct->focus_column;
1256 case GDK_KEY_End: case GDK_KEY_KP_End:
1257 focus_row = ct->rows - 1 - ct->focus_row;
1258 focus_col = ct->columns - 1 - ct->focus_column;
1261 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1264 ct->click_info = info;
1265 if (event->state & GDK_CONTROL_MASK)
1267 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1271 collection_table_unselect_all(ct);
1272 collection_table_select(ct, info);
1277 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1281 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1282 ct->click_info = info;
1284 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1287 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1288 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1291 stop_signal = FALSE;
1295 if (focus_row != 0 || focus_col != 0)
1297 CollectInfo *new_info;
1298 CollectInfo *old_info;
1300 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1301 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1302 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1304 if (new_info != old_info)
1306 if (event->state & GDK_SHIFT_MASK)
1308 if (!options->collections.rectangular_selection)
1310 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1314 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1316 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1318 else if (event->state & GDK_CONTROL_MASK)
1320 ct->click_info = new_info;
1324 ct->click_info = new_info;
1325 collection_table_unselect_all(ct);
1326 collection_table_select(ct, new_info);
1340 *-------------------------------------------------------------------
1342 *-------------------------------------------------------------------
1345 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1346 gboolean use_coord, gint x, gint y)
1348 CollectInfo *info = NULL;
1349 GtkTreeModel *store;
1352 GtkTreeViewColumn *column;
1353 #if GTK_CHECK_VERSION(3,0,0)
1354 GdkDeviceManager *device_manager;
1358 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1360 #if GTK_CHECK_VERSION(3,0,0)
1363 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1364 gtk_widget_get_window(ct->listview)));
1365 device = gdk_device_manager_get_client_pointer(device_manager);
1366 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1367 device, &x, &y, NULL);
1370 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1375 if (collection_table_find_iter(ct, source, &iter, &col))
1377 tpath = gtk_tree_model_get_path(store, &iter);
1378 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1379 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1380 gtk_tree_path_free(tpath);
1383 *after = !!(x > cell->x + (cell->width / 2));
1388 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1389 &tpath, &column, NULL, NULL))
1394 gtk_tree_model_get_iter(store, &iter, tpath);
1395 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1397 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1398 info = g_list_nth_data(list, n);
1402 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1403 *after = !!(x > cell->x + (cell->width / 2));
1406 gtk_tree_path_free(tpath);
1413 work = g_list_last(ct->cd->list);
1421 if (collection_table_find_iter(ct, info, &iter, &col))
1423 tpath = gtk_tree_model_get_path(store, &iter);
1424 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1425 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1426 gtk_tree_path_free(tpath);
1434 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1438 gboolean after = FALSE;
1440 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1446 work = g_list_find(ct->cd->list, info);
1447 if (work && work->next)
1449 info = work->next->data;
1460 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1462 gboolean after = FALSE;
1467 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1468 ct->marker_window = NULL;
1473 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1475 /* this setting does not take into account (after), but since it is not really used... */
1476 ct->marker_info = info;
1478 if (!ct->marker_window)
1480 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1481 GdkWindowAttr attributes;
1482 gint attributes_mask;
1483 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1484 gint w = gdk_pixbuf_get_width(pb);
1485 gint h = gdk_pixbuf_get_height(pb);
1487 attributes.window_type = GDK_WINDOW_CHILD;
1488 attributes.wclass = GDK_INPUT_OUTPUT;
1489 attributes.width = w;
1490 attributes.height = h;
1491 attributes.event_mask = gtk_widget_get_events(ct->listview);
1492 attributes_mask = 0;
1494 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1496 #if GTK_CHECK_VERSION(3,0,0)
1497 cairo_region_t *mask;
1498 cairo_pattern_t *pattern;
1499 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1500 cairo_t *cr = cairo_create(img);
1501 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1503 pattern = cairo_pattern_create_for_surface(img);
1504 mask = gdk_cairo_region_create_from_surface(img);
1505 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1506 gdk_window_set_background_pattern(ct->marker_window, pattern);
1507 cairo_region_destroy(mask);
1508 cairo_pattern_destroy(pattern);
1510 cairo_surface_destroy(img);
1514 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1516 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1517 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1519 g_object_unref(pixmap);
1520 if (mask) g_object_unref(mask);
1530 w = gdk_window_get_width(ct->marker_window);
1531 h = gdk_window_get_height(ct->marker_window);
1539 x = cell.x + cell.width;
1542 y = cell.y + (cell.height / 2) - (h / 2);
1544 gdk_window_move(ct->marker_window, x, y);
1545 #if !GTK_CHECK_VERSION(3,0,0)
1546 gdk_window_clear(ct->marker_window);
1548 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1552 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1557 *-------------------------------------------------------------------
1558 * mouse drag auto-scroll
1559 *-------------------------------------------------------------------
1562 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1566 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1571 collection_table_insert_marker(ct, info, TRUE);
1575 tip_update(ct, info);
1579 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1581 CollectTable *ct = data;
1585 #if GTK_CHECK_VERSION(3,0,0)
1586 GdkDeviceManager *device_manager;
1590 if (!ct->drop_idle_id) return FALSE;
1592 window = gtk_widget_get_window(ct->listview);
1593 #if GTK_CHECK_VERSION(3,0,0)
1594 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1595 device = gdk_device_manager_get_client_pointer(device_manager);
1596 gdk_window_get_device_position(window, device, &x, &y, NULL);
1598 gdk_window_get_pointer(window, &x, &y, NULL);
1600 w = gdk_window_get_width(window);
1601 h = gdk_window_get_height(window);
1602 if (x >= 0 && x < w && y >= 0 && y < h)
1604 collection_table_motion_update(ct, x, y, TRUE);
1607 ct->drop_idle_id = 0;
1611 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1613 CollectTable *ct = data;
1615 if (!ct->drop_idle_id)
1617 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1623 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1627 if (ct->drop_idle_id)
1629 g_source_remove(ct->drop_idle_id);
1630 ct->drop_idle_id = 0;
1632 widget_auto_scroll_stop(ct->listview);
1633 collection_table_insert_marker(ct, NULL, FALSE);
1637 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1638 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1639 collection_table_auto_scroll_notify_cb, ct);
1644 *-------------------------------------------------------------------
1646 *-------------------------------------------------------------------
1649 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1651 CollectTable *ct = data;
1653 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1658 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1660 CollectTable *ct = data;
1666 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1668 ct->click_info = info;
1669 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1671 switch (bevent->button)
1673 case MOUSE_BUTTON_LEFT:
1674 if (bevent->type == GDK_2BUTTON_PRESS)
1678 layout_image_set_collection(NULL, ct->cd, info);
1681 else if (!gtk_widget_has_focus(ct->listview))
1683 gtk_widget_grab_focus(ct->listview);
1686 case MOUSE_BUTTON_RIGHT:
1687 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1688 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1697 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1699 CollectTable *ct = data;
1701 CollectInfo *info = NULL;
1705 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1707 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1712 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1715 if (bevent->button == MOUSE_BUTTON_LEFT &&
1716 info && ct->click_info == info)
1718 collection_table_set_focus(ct, info);
1720 if (bevent->state & GDK_CONTROL_MASK)
1722 gboolean select = !INFO_SELECTED(info);
1724 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1726 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1730 collection_table_select_util(ct, info, select);
1735 collection_table_unselect_all(ct);
1737 if ((bevent->state & GDK_SHIFT_MASK) &&
1740 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1744 collection_table_select_util(ct, info, TRUE);
1748 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1749 info && ct->click_info == info)
1751 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1757 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1759 CollectTable *ct = data;
1766 *-------------------------------------------------------------------
1767 * populate, add, insert, etc.
1768 *-------------------------------------------------------------------
1771 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1775 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1781 static void collection_table_clear_store(CollectTable *ct)
1783 GtkTreeModel *store;
1785 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1786 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1788 gtk_list_store_clear(GTK_LIST_STORE(store));
1791 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1793 GtkListStore *store;
1797 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1799 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1800 gtk_list_store_append(store, iter);
1801 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1806 static void collection_table_populate(CollectTable *ct, gboolean resize)
1811 collection_table_verify_selections(ct);
1813 collection_table_clear_store(ct);
1820 thumb_width = collection_table_get_icon_width(ct);
1822 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1824 GtkTreeViewColumn *column;
1825 GtkCellRenderer *cell;
1828 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1829 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1830 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1832 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1833 cell = (list) ? list->data : NULL;
1836 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1838 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1839 "fixed_height", options->thumbnails.max_height,
1840 "show_text", ct->show_text || ct->show_stars, NULL);
1843 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1847 work = ct->cd->list;
1855 list = collection_table_add_row(ct, &iter);
1856 while (work && list)
1858 list->data = work->data;
1866 collection_table_update_focus(ct);
1867 collection_table_update_status(ct);
1870 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1875 thumb_width = collection_table_get_icon_width(ct);
1877 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1878 if (new_cols < 1) new_cols = 1;
1880 if (!force && new_cols == ct->columns) return;
1882 ct->columns = new_cols;
1884 collection_table_populate(ct, TRUE);
1886 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1889 static void collection_table_sync(CollectTable *ct)
1891 GtkTreeModel *store;
1896 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1901 work = ct->cd->list;
1907 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1909 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1910 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1914 list = collection_table_add_row(ct, &iter);
1939 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1943 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1944 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1950 collection_table_update_focus(ct);
1951 collection_table_update_status(ct);
1954 static gboolean collection_table_sync_idle_cb(gpointer data)
1956 CollectTable *ct = data;
1958 if (!ct->sync_idle_id) return FALSE;
1959 g_source_remove(ct->sync_idle_id);
1960 ct->sync_idle_id = 0;
1962 collection_table_sync(ct);
1966 static void collection_table_sync_idle(CollectTable *ct)
1968 if (!ct->sync_idle_id)
1970 /* high priority, the view needs to be resynced before a redraw
1971 * may contain invalid pointers at this time
1973 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1977 void collection_table_add_filelist(CollectTable *ct, GList *list)
1986 collection_add(ct->cd, (FileData *)work->data, FALSE);
1991 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
2000 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
2004 collection_table_sync_idle(ct);
2007 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
2010 GList *insert_pos = NULL;
2014 if (!info_list) return;
2016 info = collection_table_find_data(ct, row, col, NULL);
2018 if (!info_list->next && info_list->data == info) return;
2020 if (info) insert_pos = g_list_find(ct->cd->list, info);
2022 /* FIXME: this may get slow for large lists */
2024 while (insert_pos && work)
2026 if (insert_pos->data == work->data)
2028 insert_pos = insert_pos->next;
2040 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2044 /* place them back in */
2045 temp = g_list_copy(info_list);
2049 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2053 ct->cd->list = g_list_concat(temp, ct->cd->list);
2057 ct->cd->list = g_list_concat(ct->cd->list, temp);
2060 ct->cd->changed = TRUE;
2062 collection_table_sync_idle(ct);
2067 *-------------------------------------------------------------------
2069 *-------------------------------------------------------------------
2072 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2080 collection_table_update_extras(ct, FALSE, 0.0);
2084 if (!collection_table_find_position(ct, info, &row, &col)) return;
2086 if (ct->columns != 0 && ct->rows != 0)
2088 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2095 collection_table_update_extras(ct, TRUE, value);
2097 if (collection_table_find_iter(ct, info, &iter, NULL))
2099 GtkTreeModel *store;
2102 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2103 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2104 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2108 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2110 collection_table_sync_idle(ct);
2113 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2115 collection_table_sync_idle(ct);
2118 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2120 if (ci && INFO_SELECTED(ci))
2122 ct->selection = g_list_remove(ct->selection, ci);
2125 collection_table_sync_idle(ct);
2128 void collection_table_refresh(CollectTable *ct)
2130 collection_table_populate(ct, FALSE);
2134 *-------------------------------------------------------------------
2136 *-------------------------------------------------------------------
2139 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2145 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2148 f = filelist_filter(f, FALSE);
2149 d = filelist_filter(d, TRUE);
2151 f = filelist_sort_path(f);
2152 d = filelist_sort_path(d);
2154 collection_table_insert_filelist(ct, f, ct->marker_info);
2156 work = g_list_last(d);
2159 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2167 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2172 FileData *fd = work->data;
2174 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2176 collection_table_insert_filelist(ct, list, ct->marker_info);
2180 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2182 CollectTable *ct = data;
2184 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2187 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2189 CollectTable *ct = data;
2191 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2194 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2196 CollectTable *ct = data;
2198 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2201 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2205 menu = popup_menu_short_lived();
2206 g_signal_connect(G_OBJECT(menu), "destroy",
2207 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2209 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2210 menu_item_add_divider(menu);
2211 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2212 G_CALLBACK(confirm_dir_list_add), ct);
2213 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2214 G_CALLBACK(confirm_dir_list_recurse), ct);
2215 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2216 G_CALLBACK(confirm_dir_list_skip), ct);
2217 menu_item_add_divider(menu);
2218 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2224 *-------------------------------------------------------------------
2226 *-------------------------------------------------------------------
2229 static GtkTargetEntry collection_drag_types[] = {
2230 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2231 { "text/uri-list", 0, TARGET_URI_LIST },
2232 { "text/plain", 0, TARGET_TEXT_PLAIN }
2234 static gint n_collection_drag_types = 3;
2236 static GtkTargetEntry collection_drop_types[] = {
2237 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2238 { "text/uri-list", 0, TARGET_URI_LIST }
2240 static gint n_collection_drop_types = 2;
2243 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2244 GtkSelectionData *selection_data, guint info,
2245 guint time, gpointer data)
2247 CollectTable *ct = data;
2250 gchar *uri_text = NULL;
2253 if (!ct->click_info) return;
2255 selected = INFO_SELECTED(ct->click_info);
2259 case TARGET_APP_COLLECTION_MEMBER:
2262 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2266 list = g_list_append(NULL, ct->click_info);
2267 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2270 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2271 8, (guchar *)uri_text, total);
2274 case TARGET_URI_LIST:
2275 case TARGET_TEXT_PLAIN:
2279 list = collection_table_selection_get_list(ct);
2283 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2287 uri_selection_data_set_uris_from_filelist(selection_data, list);
2288 filelist_free(list);
2294 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2296 GtkSelectionData *selection_data, guint info,
2297 guint time, gpointer data)
2299 CollectTable *ct = data;
2301 GList *info_list = NULL;
2302 CollectionData *source;
2303 CollectInfo *drop_info;
2306 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2308 collection_table_scroll(ct, FALSE);
2309 collection_table_insert_marker(ct, NULL, FALSE);
2311 drop_info = collection_table_insert_point(ct, x, y);
2315 case TARGET_APP_COLLECTION_MEMBER:
2316 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2319 if (source == ct->cd)
2324 /* it is a move within a collection */
2325 filelist_free(list);
2330 collection_table_move_by_info_list(ct, info_list, -1, -1);
2332 else if (collection_table_find_position(ct, drop_info, &row, &col))
2334 collection_table_move_by_info_list(ct, info_list, row, col);
2339 /* it is a move/copy across collections */
2340 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2342 collection_remove_by_info_list(source, info_list);
2345 g_list_free(info_list);
2348 case TARGET_URI_LIST:
2349 list = uri_filelist_from_gtk_selection_data(selection_data);
2353 FileData *fd = work->data;
2354 if (isdir(fd->path))
2358 ct->drop_list = list;
2359 ct->drop_info = drop_info;
2360 menu = collection_table_drop_menu(ct);
2361 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2374 collection_table_insert_filelist(ct, list, drop_info);
2375 filelist_free(list);
2379 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2381 CollectTable *ct = data;
2383 if (ct->click_info && ct->click_info->pixbuf)
2387 if (INFO_SELECTED(ct->click_info))
2388 items = g_list_length(ct->selection);
2391 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2395 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2397 CollectTable *ct = data;
2399 /* apparently a leave event is not generated on a drop */
2402 collection_table_scroll(ct, FALSE);
2405 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2406 gint x, gint y, guint time, gpointer data)
2408 CollectTable *ct = data;
2410 collection_table_motion_update(ct, x, y, TRUE);
2411 collection_table_scroll(ct, TRUE);
2416 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2418 CollectTable *ct = data;
2420 collection_table_scroll(ct, FALSE);
2423 static void collection_table_dnd_init(CollectTable *ct)
2425 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2426 collection_drag_types, n_collection_drag_types,
2427 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2428 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2429 G_CALLBACK(collection_table_dnd_get), ct);
2430 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2431 G_CALLBACK(collection_table_dnd_begin), ct);
2432 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2433 G_CALLBACK(collection_table_dnd_end), ct);
2435 gtk_drag_dest_set(ct->listview,
2436 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2437 collection_drop_types, n_collection_drop_types,
2438 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2439 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2440 G_CALLBACK(collection_table_dnd_motion), ct);
2441 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2442 G_CALLBACK(collection_table_dnd_leave), ct);
2443 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2444 G_CALLBACK(collection_table_dnd_receive), ct);
2448 *-----------------------------------------------------------------------------
2450 *-----------------------------------------------------------------------------
2453 typedef struct _ColumnData ColumnData;
2460 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2461 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2463 ColumnData *cd = data;
2470 gchar *star_rating = NULL;
2471 gchar *display_text = NULL;
2475 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2477 #if GTK_CHECK_VERSION(3,0,0)
2478 /* FIXME this is a primitive hack to stop a crash.
2479 * When compiled with GTK3, if a Collection window containing
2480 * say, 50 or so, images has its width changed, there is a segfault
2481 * https://github.com/BestImageViewer/geeqie/issues/531
2483 if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2485 info = g_list_nth_data(list, cd->number);
2487 style = gtk_widget_get_style(ct->listview);
2488 if (info && (info->flag_mask & SELECTION_SELECTED) )
2490 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2491 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2495 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2496 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2499 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2501 shift_color(&color_bg, -1, 0);
2504 if (ct->show_stars && info && info->fd)
2506 star_rating = metadata_read_rating_stars(info->fd);
2510 star_rating = g_strdup("");
2513 if (info && info->fd)
2515 if (ct->show_text && ct->show_stars)
2517 display_text = g_strconcat(info->fd->name, "\n", star_rating, NULL);
2519 else if (ct->show_text)
2521 display_text = g_strdup(info->fd->name);
2523 else if (ct->show_stars)
2525 display_text = g_strdup(star_rating);
2529 display_text = g_strdup("");
2534 display_text = g_strdup("");
2537 if (GQV_IS_CELL_RENDERER_ICON(cell))
2541 g_object_set(cell, "pixbuf", info->pixbuf,
2542 "text", display_text,
2543 "cell-background-gdk", &color_bg,
2544 "cell-background-set", TRUE,
2545 "foreground-gdk", &color_fg,
2546 "foreground-set", TRUE,
2547 "has-focus", (ct->focus_info == info), NULL);
2551 g_object_set(cell, "pixbuf", NULL,
2553 "cell-background-set", FALSE,
2554 "foreground-set", FALSE,
2555 "has-focus", FALSE, NULL);
2559 g_free(display_text);
2560 g_free(star_rating);
2563 static void collection_table_append_column(CollectTable *ct, gint n)
2566 GtkTreeViewColumn *column;
2567 GtkCellRenderer *renderer;
2569 column = gtk_tree_view_column_new();
2570 gtk_tree_view_column_set_min_width(column, 0);
2572 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2573 gtk_tree_view_column_set_alignment(column, 0.5);
2575 renderer = gqv_cell_renderer_icon_new();
2576 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2577 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2578 "ypad", THUMB_BORDER_PADDING,
2579 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2581 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2583 cd = g_new0(ColumnData, 1);
2586 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2588 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2592 *-------------------------------------------------------------------
2594 *-------------------------------------------------------------------
2597 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2599 CollectTable *ct = data;
2601 /* If there is no unsaved data, save the window geometry
2603 if (!ct->cd->changed)
2605 if (!collection_save(ct->cd, ct->cd->path))
2607 log_printf("failed saving to collection path: %s\n", ct->cd->path);
2613 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2615 gtk_widget_destroy(ct->popup);
2618 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2621 collection_table_scroll(ct, FALSE);
2626 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2628 CollectTable *ct = data;
2630 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2633 CollectTable *collection_table_new(CollectionData *cd)
2636 GtkListStore *store;
2637 GtkTreeSelection *selection;
2640 ct = g_new0(CollectTable, 1);
2643 ct->show_text = options->show_icon_names;
2644 ct->show_stars = options->show_star_rating;
2646 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2647 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2648 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2649 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2651 store = gtk_list_store_new(1, G_TYPE_POINTER);
2652 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2653 g_object_unref(store);
2655 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2656 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2658 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2659 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2661 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2663 collection_table_append_column(ct, i);
2666 /* zero width column to hide tree view focus, we draw it ourselves */
2667 collection_table_append_column(ct, i);
2668 /* end column to fill white space */
2669 collection_table_append_column(ct, i);
2671 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2672 G_CALLBACK(collection_table_destroy), ct);
2673 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2674 G_CALLBACK(collection_table_sized), ct);
2675 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2676 G_CALLBACK(collection_table_press_key_cb), ct);
2678 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2679 gtk_widget_show(ct->listview);
2681 collection_table_dnd_init(ct);
2683 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2684 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2685 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2686 G_CALLBACK(collection_table_press_cb), ct);
2687 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2688 G_CALLBACK(collection_table_release_cb), ct);
2689 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2690 G_CALLBACK(collection_table_motion_cb), ct);
2691 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2692 G_CALLBACK(collection_table_leave_cb), ct);
2697 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2699 ct->status_label = status;
2700 ct->extra_label = extra;
2701 collection_table_update_status(ct);
2702 collection_table_update_extras(ct, FALSE, 0.0);
2705 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2707 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2710 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */