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"
37 #include "pixbuf_util.h"
40 #include "ui_fileops.h"
42 #include "ui_tree_edit.h"
43 #include "uri_utils.h"
44 #include "view_file.h"
46 #include "icons/marker.xpm"
47 #define MARKER_WIDTH 26
48 #define MARKER_HEIGHT 32
50 #include <gdk/gdkkeysyms.h> /* for keyboard values */
52 /* between these, the icon width is increased by thumb_max_width / 2 */
53 #define THUMB_MIN_ICON_WIDTH 128
54 #define THUMB_MAX_ICON_WIDTH 150
56 #define COLLECT_TABLE_MAX_COLUMNS 32
57 #define THUMB_BORDER_PADDING 2
59 #define COLLECT_TABLE_TIP_DELAY 500
60 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
64 CTABLE_COLUMN_POINTER = 0,
68 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
71 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
75 *-------------------------------------------------------------------
77 *-------------------------------------------------------------------
80 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
84 n = g_list_index(ct->cd->list, info);
86 if (n < 0) return FALSE;
88 *row = n / ct->columns;
89 *col = n - (*row * ct->columns);
94 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
99 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
100 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
101 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
102 if (column) *column = col;
107 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
112 if (row < 0 || col < 0) return NULL;
114 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
115 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
119 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
120 if (!list) return NULL;
124 return g_list_nth_data(list, col);
130 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
133 GtkTreeViewColumn *column;
139 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
140 &tpath, &column, NULL, NULL))
143 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
144 gtk_tree_model_get_iter(store, &row, tpath);
145 gtk_tree_path_free(tpath);
147 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
148 if (!list) return NULL;
150 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
151 if (iter) *iter = row;
152 return g_list_nth_data(list, n);
155 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
165 CollectInfo *ci = work->data;
173 return g_list_length(ct->cd->list);
176 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
183 work = ct->selection;
186 CollectInfo *ci = work->data;
194 return g_list_length(ct->selection);
197 static void collection_table_update_status(CollectTable *ct)
205 if (!ct->status_label) return;
207 n = collection_table_list_count(ct, &n_bytes);
208 s = collection_table_selection_count(ct, &s_bytes);
212 gchar *b = text_from_size_abrev(n_bytes);
213 gchar *sb = text_from_size_abrev(s_bytes);
214 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
220 gchar *b = text_from_size_abrev(n_bytes);
221 buf = g_strdup_printf(_("%s, %d images"), b, n);
226 buf = g_strdup(_("Empty"));
229 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
233 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
237 if (!ct->extra_label) return;
240 text = _("Loading thumbs...");
244 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
245 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
248 static void collection_table_toggle_filenames(CollectTable *ct)
250 GtkAllocation allocation;
251 ct->show_text = !ct->show_text;
252 options->show_icon_names = ct->show_text;
254 gtk_widget_get_allocation(ct->listview, &allocation);
255 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
258 static void collection_table_toggle_stars(CollectTable *ct)
260 GtkAllocation allocation;
261 ct->show_stars = !ct->show_stars;
262 options->show_star_rating = ct->show_stars;
264 gtk_widget_get_allocation(ct->listview, &allocation);
265 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
268 static gint collection_table_get_icon_width(CollectTable *ct)
272 if (!ct->show_text) return options->thumbnails.max_width;
274 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
275 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
276 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
282 *-------------------------------------------------------------------
284 *-------------------------------------------------------------------
287 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
294 if (info->flag_mask == value) return;
295 info->flag_mask = value;
297 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
300 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
301 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
307 if (collection_table_find_iter(ct, info, &row, NULL))
309 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
310 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
315 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
319 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
322 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
326 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
329 *-------------------------------------------------------------------
331 *-------------------------------------------------------------------
334 static void collection_table_verify_selections(CollectTable *ct)
338 work = ct->selection;
341 CollectInfo *info = work->data;
343 if (!g_list_find(ct->cd->list, info))
345 ct->selection = g_list_remove(ct->selection, info);
350 void collection_table_select_all(CollectTable *ct)
354 g_list_free(ct->selection);
355 ct->selection = NULL;
360 ct->selection = g_list_append(ct->selection, work->data);
361 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
365 collection_table_update_status(ct);
368 void collection_table_unselect_all(CollectTable *ct)
372 work = ct->selection;
375 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
379 g_list_free(ct->selection);
380 ct->selection = NULL;
382 collection_table_update_status(ct);
385 /* Invert the current collection's selection */
386 static void collection_table_select_invert_all(CollectTable *ct)
389 GList *new_selection = NULL;
394 CollectInfo *info = work->data;
396 if (INFO_SELECTED(info))
398 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
402 new_selection = g_list_append(new_selection, info);
403 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
410 g_list_free(ct->selection);
411 ct->selection = new_selection;
413 collection_table_update_status(ct);
416 void collection_table_select(CollectTable *ct, CollectInfo *info)
418 ct->prev_selection = info;
420 if (!info || INFO_SELECTED(info)) return;
422 ct->selection = g_list_append(ct->selection, info);
423 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
425 collection_table_update_status(ct);
428 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
430 ct->prev_selection = info;
432 if (!info || !INFO_SELECTED(info) ) return;
434 ct->selection = g_list_remove(ct->selection, info);
435 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
437 collection_table_update_status(ct);
440 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
444 collection_table_select(ct, info);
448 collection_table_unselect(ct, info);
452 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
459 if (!collection_table_find_position(ct, start, &row1, &col1) ||
460 !collection_table_find_position(ct, end, &row2, &col2) ) return;
462 ct->prev_selection = end;
464 if (!options->collections.rectangular_selection)
469 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
476 work = g_list_find(ct->cd->list, start);
480 collection_table_select_util(ct, info, select);
482 if (work->data != end)
503 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
505 for (i = row1; i <= row2; i++)
507 for (j = col1; j <= col2; j++)
509 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
510 if (info) collection_table_select_util(ct, info, select);
515 GList *collection_table_selection_get_list(CollectTable *ct)
517 return collection_list_to_filelist(ct->selection);
520 static GList *collection_table_get_list(CollectTable *ct)
522 return collection_list_to_filelist(ct->cd->list);
526 *-------------------------------------------------------------------
527 * tooltip type window
528 *-------------------------------------------------------------------
531 static void tip_show(CollectTable *ct)
535 #if GTK_CHECK_VERSION(3,0,0)
537 GdkDeviceManager *device_manager;
541 if (ct->tip_window) return;
543 #if GTK_CHECK_VERSION(3,0,0)
544 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
545 gtk_widget_get_window(ct->listview)));
546 device = gdk_device_manager_get_client_pointer(device_manager);
547 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
548 device, &x, &y, NULL);
550 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
553 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
554 if (!ct->tip_info) return;
556 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
557 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
558 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
560 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
562 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
563 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
564 gtk_widget_show(label);
566 #if GTK_CHECK_VERSION(3,0,0)
567 display = gdk_display_get_default();
568 device_manager = gdk_display_get_device_manager(display);
569 device = gdk_device_manager_get_client_pointer(device_manager);
570 gdk_device_get_position(device, NULL, &x, &y);
572 gdk_window_get_pointer(NULL, &x, &y, NULL);
575 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
576 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
577 gtk_widget_show(ct->tip_window);
580 static void tip_hide(CollectTable *ct)
582 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
583 ct->tip_window = NULL;
586 static gboolean tip_schedule_cb(gpointer data)
588 CollectTable *ct = data;
590 if (!ct->tip_delay_id) return FALSE;
594 ct->tip_delay_id = 0;
598 static void tip_schedule(CollectTable *ct)
602 if (ct->tip_delay_id)
604 g_source_remove(ct->tip_delay_id);
605 ct->tip_delay_id = 0;
608 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
611 static void tip_unschedule(CollectTable *ct)
615 if (ct->tip_delay_id)
617 g_source_remove(ct->tip_delay_id);
618 ct->tip_delay_id = 0;
622 static void tip_update(CollectTable *ct, CollectInfo *info)
624 #if GTK_CHECK_VERSION(3,0,0)
625 GdkDisplay *display = gdk_display_get_default();
626 GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
627 GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
635 #if GTK_CHECK_VERSION(3,0,0)
636 gdk_device_get_position(device, NULL, &x, &y);
638 gdk_window_get_pointer(NULL, &x, &y, NULL);
640 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
642 if (info != ct->tip_info)
653 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
654 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
660 *-------------------------------------------------------------------
662 *-------------------------------------------------------------------
665 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
667 CollectTable *ct = data;
669 collection_dialog_save_as(NULL, ct->cd);
672 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
674 CollectTable *ct = data;
678 collection_table_popup_save_as_cb(widget, data);
682 if (!collection_save(ct->cd, ct->cd->path))
684 log_printf("failed saving to collection path: %s\n", ct->cd->path);
688 static GList *collection_table_popup_file_list(CollectTable *ct)
690 if (!ct->click_info) return NULL;
692 if (INFO_SELECTED(ct->click_info))
694 return collection_table_selection_get_list(ct);
697 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
700 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
703 const gchar *key = data;
705 ct = submenu_item_get_data(widget);
709 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
712 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
714 CollectTable *ct = data;
716 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
719 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
721 CollectTable *ct = data;
723 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
726 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
728 CollectTable *ct = data;
730 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
733 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
735 CollectTable *ct = data;
737 options->file_ops.safe_delete_enable = FALSE;
738 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
741 static void collection_table_popup_move_to_trash_cb(GtkWidget *widget, gpointer data)
743 CollectTable *ct = data;
745 options->file_ops.safe_delete_enable = TRUE;
746 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
749 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
751 CollectTable *ct = data;
753 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), TRUE);
756 static void collection_table_popup_copy_path_unquoted_cb(GtkWidget *widget, gpointer data)
758 CollectTable *ct = data;
760 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), FALSE);
763 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
768 ct = submenu_item_get_data(widget);
772 type = (SortType)GPOINTER_TO_INT(data);
774 collection_set_sort_method(ct->cd, type);
777 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
781 ct = submenu_item_get_data(widget);
785 collection_randomize(ct->cd);
788 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
790 CollectTable *ct = data;
792 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
794 view_window_new_from_collection(ct->cd, ct->click_info);
798 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
800 CollectTable *ct = data;
802 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
804 layout_image_set_collection(NULL, ct->cd, ct->click_info);
808 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
810 CollectTable *ct = data;
812 collection_table_select_all(ct);
813 ct->prev_selection= ct->click_info;
816 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
818 CollectTable *ct = data;
820 collection_table_unselect_all(ct);
821 ct->prev_selection= ct->click_info;
824 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
826 CollectTable *ct = data;
828 collection_table_select_invert_all(ct);
829 ct->prev_selection= ct->click_info;
832 static void collection_table_popup_rectangular_selection_cb(GtkWidget *widget, gpointer data)
834 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
837 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
839 CollectTable *ct = data;
842 if (!ct->click_info) return;
844 if (INFO_SELECTED(ct->click_info))
846 list = g_list_copy(ct->selection);
850 list = g_list_append(NULL, ct->click_info);
853 collection_remove_by_info_list(ct->cd, list);
857 static void collection_table_popup_add_file_selection_cb(GtkWidget *widget, gpointer data)
859 CollectTable *ct = data;
861 LayoutWindow *lw = NULL;
863 if (!layout_valid(&lw)) return;
865 list = vf_selection_get_list(lw->vf);
869 collection_table_add_filelist(ct, list);
874 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
876 CollectTable *ct = data;
878 collection_dialog_append(NULL, ct->cd);
881 static void collection_table_popup_goto_original_cb(GtkWidget *widget, gpointer data)
883 CollectTable *ct = data;
885 LayoutWindow *lw = NULL;
888 if (!layout_valid(&lw)) return;
889 list = collection_table_selection_get_list(ct);
895 layout_set_fd(lw, fd);
901 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
903 CollectTable *ct = data;
906 dw = dupe_window_new();
907 dupe_window_add_collection(dw, ct->cd);
910 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
912 CollectTable *ct = data;
915 fd = (ct->click_info) ? ct->click_info->fd : NULL;
917 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), gtk_widget_get_toplevel(ct->listview));
920 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
922 CollectTable *ct = data;
924 collection_table_toggle_filenames(ct);
927 static void collection_table_popup_show_stars_cb(GtkWidget *widget, gpointer data)
929 CollectTable *ct = data;
931 collection_table_toggle_stars(ct);
934 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
936 CollectTable *ct = data;
938 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
939 ct->click_info = NULL;
942 filelist_free(ct->drop_list);
943 ct->drop_list = NULL;
944 ct->drop_info = NULL;
946 filelist_free(ct->editmenu_fd_list);
947 ct->editmenu_fd_list = NULL;
950 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
956 menu = popup_menu_short_lived();
958 g_signal_connect(G_OBJECT(menu), "destroy",
959 G_CALLBACK(collection_table_popup_destroy_cb), ct);
961 menu_item_add_sensitive(menu, _("_View"), over_icon,
962 G_CALLBACK(collection_table_popup_view_cb), ct);
963 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
964 G_CALLBACK(collection_table_popup_view_new_cb), ct);
965 menu_item_add_stock(menu, _("Go to original"), GTK_STOCK_FIND,
966 G_CALLBACK(collection_table_popup_goto_original_cb), ct);
967 menu_item_add_divider(menu);
968 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
969 G_CALLBACK(collection_table_popup_remove_cb), ct);
971 menu_item_add_stock(menu, _("Append from file selection"), GTK_STOCK_ADD,
972 G_CALLBACK(collection_table_popup_add_file_selection_cb), ct);
973 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
974 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
975 menu_item_add_divider(menu);
977 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
978 submenu = gtk_menu_new();
979 menu_item_add(submenu, _("Select all"),
980 G_CALLBACK(collection_table_popup_selectall_cb), ct);
981 menu_item_add(submenu, _("Select none"),
982 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
983 menu_item_add(submenu, _("Invert selection"),
984 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
985 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
986 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
987 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
988 menu_item_add_divider(menu);
991 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
992 submenu_add_edit(menu, &item,
993 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
994 gtk_widget_set_sensitive(item, over_icon);
996 menu_item_add_divider(menu);
997 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
998 G_CALLBACK(collection_table_popup_copy_cb), ct);
999 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
1000 G_CALLBACK(collection_table_popup_move_cb), ct);
1001 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
1002 G_CALLBACK(collection_table_popup_rename_cb), ct);
1003 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
1004 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
1005 menu_item_add_sensitive(menu, _("_Copy path unquoted"), over_icon,
1006 G_CALLBACK(collection_table_popup_copy_path_unquoted_cb), ct);
1008 menu_item_add_divider(menu);
1009 menu_item_add_stock_sensitive(menu,
1010 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
1011 _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, over_icon,
1012 G_CALLBACK(collection_table_popup_move_to_trash_cb), ct);
1013 menu_item_add_stock_sensitive(menu,
1014 options->file_ops.confirm_delete ? _("_Delete...") :
1015 _("_Delete"), GTK_STOCK_DELETE, over_icon,
1016 G_CALLBACK(collection_table_popup_delete_cb), ct);
1018 menu_item_add_divider(menu);
1019 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
1020 menu_item_add_divider(submenu);
1021 menu_item_add(submenu, _("Randomize"),
1022 G_CALLBACK(collection_table_popup_randomize_cb), ct);
1023 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
1024 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1026 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
1027 G_CALLBACK(collection_table_popup_show_names_cb), ct);
1028 menu_item_add_check(menu, _("Show star rating"), ct->show_stars,
1029 G_CALLBACK(collection_table_popup_show_stars_cb), ct);
1030 menu_item_add_divider(menu);
1031 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
1032 G_CALLBACK(collection_table_popup_save_cb), ct);
1033 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
1034 G_CALLBACK(collection_table_popup_save_as_cb), ct);
1035 menu_item_add_divider(menu);
1036 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
1037 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
1038 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
1039 G_CALLBACK(collection_table_popup_print_cb), ct);
1044 *-------------------------------------------------------------------
1045 * keyboard callbacks
1046 *-------------------------------------------------------------------
1049 void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
1054 if (g_list_find(ct->cd->list, ct->focus_info))
1056 if (info == ct->focus_info)
1058 /* ensure focus row col are correct */
1059 collection_table_find_position(ct, ct->focus_info,
1060 &ct->focus_row, &ct->focus_column);
1063 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1066 if (!collection_table_find_position(ct, info, &row, &col))
1068 ct->focus_info = NULL;
1070 ct->focus_column = -1;
1074 ct->focus_info = info;
1075 ct->focus_row = row;
1076 ct->focus_column = col;
1077 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1079 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1082 GtkTreeViewColumn *column;
1083 GtkTreeModel *store;
1085 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1087 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1088 tpath = gtk_tree_model_get_path(store, &iter);
1089 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1090 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1091 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1092 gtk_tree_path_free(tpath);
1096 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1103 new_row = ct->focus_row;
1104 new_col = ct->focus_column;
1107 if (new_row < 0) new_row = 0;
1108 if (new_row >= ct->rows) new_row = ct->rows - 1;
1128 new_col = ct->columns - 1;
1135 if (new_col >= ct->columns)
1137 if (new_row < ct->rows - 1)
1144 new_col = ct->columns - 1;
1154 if (new_row >= ct->rows)
1157 new_row = ct->rows - 1;
1160 new_col = ct->columns - 1;
1162 if (new_col >= ct->columns) new_col = ct->columns - 1;
1165 if (new_row == ct->rows - 1)
1169 /* if we moved beyond the last image, go to the last image */
1171 l = g_list_length(ct->cd->list);
1172 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1173 if (new_col >= l) new_col = l - 1;
1176 if (new_row == -1 || new_col == -1)
1178 if (!ct->cd->list) return;
1179 new_row = new_col = 0;
1182 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1185 static void collection_table_update_focus(CollectTable *ct)
1190 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1192 /* first find the old focus, if it exists and is valid */
1196 /* (try to) stay where we were */
1197 new_row = ct->focus_row;
1198 new_col = ct->focus_column;
1201 collection_table_move_focus(ct, new_row, new_col, FALSE);
1204 /* used to figure the page up/down distances */
1205 static gint page_height(CollectTable *ct)
1212 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1213 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1215 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1216 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1218 ret = page_size / row_height;
1219 if (ret < 1) ret = 1;
1224 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1226 CollectTable *ct = data;
1227 GtkTreeModel *store;
1233 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1234 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1235 tpath = gtk_tree_model_get_path(store, &iter);
1236 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1237 gtk_tree_path_free(tpath);
1239 popup_menu_position_clamp(menu, x, y, 0);
1242 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1244 CollectTable *ct = data;
1248 gboolean stop_signal = TRUE;
1250 switch (event->keyval)
1252 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1255 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1258 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1261 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1264 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1265 focus_row = -page_height(ct);
1267 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1268 focus_row = page_height(ct);
1270 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1271 focus_row = -ct->focus_row;
1272 focus_col = -ct->focus_column;
1274 case GDK_KEY_End: case GDK_KEY_KP_End:
1275 focus_row = ct->rows - 1 - ct->focus_row;
1276 focus_col = ct->columns - 1 - ct->focus_column;
1279 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1282 ct->click_info = info;
1283 if (event->state & GDK_CONTROL_MASK)
1285 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1289 collection_table_unselect_all(ct);
1290 collection_table_select(ct, info);
1295 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1299 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1300 ct->click_info = info;
1302 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1305 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1306 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1309 stop_signal = FALSE;
1313 if (focus_row != 0 || focus_col != 0)
1315 CollectInfo *new_info;
1316 CollectInfo *old_info;
1318 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1319 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1320 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1322 if (new_info != old_info)
1324 if (event->state & GDK_SHIFT_MASK)
1326 if (!options->collections.rectangular_selection)
1328 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1332 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1334 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1336 else if (event->state & GDK_CONTROL_MASK)
1338 ct->click_info = new_info;
1342 ct->click_info = new_info;
1343 collection_table_unselect_all(ct);
1344 collection_table_select(ct, new_info);
1358 *-------------------------------------------------------------------
1360 *-------------------------------------------------------------------
1363 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1364 gboolean use_coord, gint x, gint y)
1366 CollectInfo *info = NULL;
1367 GtkTreeModel *store;
1370 GtkTreeViewColumn *column;
1371 #if GTK_CHECK_VERSION(3,0,0)
1372 GdkDeviceManager *device_manager;
1376 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1378 #if GTK_CHECK_VERSION(3,0,0)
1381 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1382 gtk_widget_get_window(ct->listview)));
1383 device = gdk_device_manager_get_client_pointer(device_manager);
1384 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1385 device, &x, &y, NULL);
1388 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1393 if (collection_table_find_iter(ct, source, &iter, &col))
1395 tpath = gtk_tree_model_get_path(store, &iter);
1396 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1397 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1398 gtk_tree_path_free(tpath);
1401 *after = !!(x > cell->x + (cell->width / 2));
1406 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1407 &tpath, &column, NULL, NULL))
1412 gtk_tree_model_get_iter(store, &iter, tpath);
1413 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1415 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1416 info = g_list_nth_data(list, n);
1420 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1421 *after = !!(x > cell->x + (cell->width / 2));
1424 gtk_tree_path_free(tpath);
1431 work = g_list_last(ct->cd->list);
1439 if (collection_table_find_iter(ct, info, &iter, &col))
1441 tpath = gtk_tree_model_get_path(store, &iter);
1442 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1443 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1444 gtk_tree_path_free(tpath);
1452 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1456 gboolean after = FALSE;
1458 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1464 work = g_list_find(ct->cd->list, info);
1465 if (work && work->next)
1467 info = work->next->data;
1478 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1480 gboolean after = FALSE;
1485 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1486 ct->marker_window = NULL;
1491 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1493 /* this setting does not take into account (after), but since it is not really used... */
1494 ct->marker_info = info;
1496 if (!ct->marker_window)
1498 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1499 GdkWindowAttr attributes;
1500 gint attributes_mask;
1501 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1502 gint w = gdk_pixbuf_get_width(pb);
1503 gint h = gdk_pixbuf_get_height(pb);
1505 attributes.window_type = GDK_WINDOW_CHILD;
1506 attributes.wclass = GDK_INPUT_OUTPUT;
1507 attributes.width = w;
1508 attributes.height = h;
1509 attributes.event_mask = gtk_widget_get_events(ct->listview);
1510 attributes_mask = 0;
1512 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1514 #if GTK_CHECK_VERSION(3,0,0)
1515 cairo_region_t *mask;
1516 cairo_pattern_t *pattern;
1517 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1518 cairo_t *cr = cairo_create(img);
1519 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1521 pattern = cairo_pattern_create_for_surface(img);
1522 mask = gdk_cairo_region_create_from_surface(img);
1523 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1524 gdk_window_set_background_pattern(ct->marker_window, pattern);
1525 cairo_region_destroy(mask);
1526 cairo_pattern_destroy(pattern);
1528 cairo_surface_destroy(img);
1532 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1534 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1535 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1537 g_object_unref(pixmap);
1538 if (mask) g_object_unref(mask);
1548 w = gdk_window_get_width(ct->marker_window);
1549 h = gdk_window_get_height(ct->marker_window);
1557 x = cell.x + cell.width;
1560 y = cell.y + (cell.height / 2) - (h / 2);
1562 gdk_window_move(ct->marker_window, x, y);
1563 #if !GTK_CHECK_VERSION(3,0,0)
1564 gdk_window_clear(ct->marker_window);
1566 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1570 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1575 *-------------------------------------------------------------------
1576 * mouse drag auto-scroll
1577 *-------------------------------------------------------------------
1580 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1584 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1589 collection_table_insert_marker(ct, info, TRUE);
1593 tip_update(ct, info);
1597 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1599 CollectTable *ct = data;
1603 #if GTK_CHECK_VERSION(3,0,0)
1604 GdkDeviceManager *device_manager;
1608 if (!ct->drop_idle_id) return FALSE;
1610 window = gtk_widget_get_window(ct->listview);
1611 #if GTK_CHECK_VERSION(3,0,0)
1612 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1613 device = gdk_device_manager_get_client_pointer(device_manager);
1614 gdk_window_get_device_position(window, device, &x, &y, NULL);
1616 gdk_window_get_pointer(window, &x, &y, NULL);
1618 w = gdk_window_get_width(window);
1619 h = gdk_window_get_height(window);
1620 if (x >= 0 && x < w && y >= 0 && y < h)
1622 collection_table_motion_update(ct, x, y, TRUE);
1625 ct->drop_idle_id = 0;
1629 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1631 CollectTable *ct = data;
1633 if (!ct->drop_idle_id)
1635 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1641 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1645 if (ct->drop_idle_id)
1647 g_source_remove(ct->drop_idle_id);
1648 ct->drop_idle_id = 0;
1650 widget_auto_scroll_stop(ct->listview);
1651 collection_table_insert_marker(ct, NULL, FALSE);
1655 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1656 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1657 collection_table_auto_scroll_notify_cb, ct);
1662 *-------------------------------------------------------------------
1664 *-------------------------------------------------------------------
1667 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1669 CollectTable *ct = data;
1671 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1676 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1678 CollectTable *ct = data;
1684 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1686 ct->click_info = info;
1687 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1689 switch (bevent->button)
1691 case MOUSE_BUTTON_LEFT:
1692 if (bevent->type == GDK_2BUTTON_PRESS)
1696 layout_image_set_collection(NULL, ct->cd, info);
1699 else if (!gtk_widget_has_focus(ct->listview))
1701 gtk_widget_grab_focus(ct->listview);
1704 case MOUSE_BUTTON_RIGHT:
1705 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1706 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1715 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1717 CollectTable *ct = data;
1719 CollectInfo *info = NULL;
1723 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1725 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1730 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1733 if (bevent->button == MOUSE_BUTTON_LEFT &&
1734 info && ct->click_info == info)
1736 collection_table_set_focus(ct, info);
1738 if (bevent->state & GDK_CONTROL_MASK)
1740 gboolean select = !INFO_SELECTED(info);
1742 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1744 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1748 collection_table_select_util(ct, info, select);
1753 collection_table_unselect_all(ct);
1755 if ((bevent->state & GDK_SHIFT_MASK) &&
1758 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1762 collection_table_select_util(ct, info, TRUE);
1766 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1767 info && ct->click_info == info)
1769 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1775 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1777 CollectTable *ct = data;
1784 *-------------------------------------------------------------------
1785 * populate, add, insert, etc.
1786 *-------------------------------------------------------------------
1789 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1793 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1799 static void collection_table_clear_store(CollectTable *ct)
1801 GtkTreeModel *store;
1803 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1804 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1806 gtk_list_store_clear(GTK_LIST_STORE(store));
1809 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1811 GtkListStore *store;
1815 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1817 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1818 gtk_list_store_append(store, iter);
1819 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1824 static void collection_table_populate(CollectTable *ct, gboolean resize)
1829 collection_table_verify_selections(ct);
1831 collection_table_clear_store(ct);
1838 thumb_width = collection_table_get_icon_width(ct);
1840 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1842 GtkTreeViewColumn *column;
1843 GtkCellRenderer *cell;
1846 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1847 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1848 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1850 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1851 cell = (list) ? list->data : NULL;
1854 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1856 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1857 "fixed_height", options->thumbnails.max_height,
1858 "show_text", ct->show_text || ct->show_stars, NULL);
1861 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1865 work = ct->cd->list;
1873 list = collection_table_add_row(ct, &iter);
1874 while (work && list)
1876 list->data = work->data;
1884 collection_table_update_focus(ct);
1885 collection_table_update_status(ct);
1888 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1893 thumb_width = collection_table_get_icon_width(ct);
1895 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1896 if (new_cols < 1) new_cols = 1;
1898 if (!force && new_cols == ct->columns) return;
1900 ct->columns = new_cols;
1902 collection_table_populate(ct, TRUE);
1904 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1907 static void collection_table_sync(CollectTable *ct)
1909 GtkTreeModel *store;
1914 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1919 work = ct->cd->list;
1925 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1927 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1928 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1932 list = collection_table_add_row(ct, &iter);
1957 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1961 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1962 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1968 collection_table_update_focus(ct);
1969 collection_table_update_status(ct);
1972 static gboolean collection_table_sync_idle_cb(gpointer data)
1974 CollectTable *ct = data;
1976 if (!ct->sync_idle_id) return FALSE;
1977 g_source_remove(ct->sync_idle_id);
1978 ct->sync_idle_id = 0;
1980 collection_table_sync(ct);
1984 static void collection_table_sync_idle(CollectTable *ct)
1986 if (!ct->sync_idle_id)
1988 /* high priority, the view needs to be resynced before a redraw
1989 * may contain invalid pointers at this time
1991 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1995 void collection_table_add_filelist(CollectTable *ct, GList *list)
2004 collection_add(ct->cd, (FileData *)work->data, FALSE);
2009 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
2018 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
2022 collection_table_sync_idle(ct);
2025 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
2028 GList *insert_pos = NULL;
2032 if (!info_list) return;
2034 info = collection_table_find_data(ct, row, col, NULL);
2036 if (!info_list->next && info_list->data == info) return;
2038 if (info) insert_pos = g_list_find(ct->cd->list, info);
2040 /* FIXME: this may get slow for large lists */
2042 while (insert_pos && work)
2044 if (insert_pos->data == work->data)
2046 insert_pos = insert_pos->next;
2058 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2062 /* place them back in */
2063 temp = g_list_copy(info_list);
2067 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2071 ct->cd->list = g_list_concat(temp, ct->cd->list);
2075 ct->cd->list = g_list_concat(ct->cd->list, temp);
2078 ct->cd->changed = TRUE;
2080 collection_table_sync_idle(ct);
2085 *-------------------------------------------------------------------
2087 *-------------------------------------------------------------------
2090 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2098 collection_table_update_extras(ct, FALSE, 0.0);
2102 if (!collection_table_find_position(ct, info, &row, &col)) return;
2104 if (ct->columns != 0 && ct->rows != 0)
2106 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2113 collection_table_update_extras(ct, TRUE, value);
2115 if (collection_table_find_iter(ct, info, &iter, NULL))
2117 GtkTreeModel *store;
2120 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2121 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2122 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2126 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2128 collection_table_sync_idle(ct);
2131 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2133 collection_table_sync_idle(ct);
2136 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2138 if (ci && INFO_SELECTED(ci))
2140 ct->selection = g_list_remove(ct->selection, ci);
2143 collection_table_sync_idle(ct);
2146 void collection_table_refresh(CollectTable *ct)
2148 collection_table_populate(ct, FALSE);
2152 *-------------------------------------------------------------------
2154 *-------------------------------------------------------------------
2157 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2163 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2166 f = filelist_filter(f, FALSE);
2167 d = filelist_filter(d, TRUE);
2169 f = filelist_sort_path(f);
2170 d = filelist_sort_path(d);
2172 collection_table_insert_filelist(ct, f, ct->marker_info);
2174 work = g_list_last(d);
2177 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2185 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2190 FileData *fd = work->data;
2192 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2194 collection_table_insert_filelist(ct, list, ct->marker_info);
2198 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2200 CollectTable *ct = data;
2202 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2205 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2207 CollectTable *ct = data;
2209 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2212 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2214 CollectTable *ct = data;
2216 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2219 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2223 menu = popup_menu_short_lived();
2224 g_signal_connect(G_OBJECT(menu), "destroy",
2225 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2227 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2228 menu_item_add_divider(menu);
2229 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2230 G_CALLBACK(confirm_dir_list_add), ct);
2231 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2232 G_CALLBACK(confirm_dir_list_recurse), ct);
2233 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2234 G_CALLBACK(confirm_dir_list_skip), ct);
2235 menu_item_add_divider(menu);
2236 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2242 *-------------------------------------------------------------------
2244 *-------------------------------------------------------------------
2247 static GtkTargetEntry collection_drag_types[] = {
2248 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2249 { "text/uri-list", 0, TARGET_URI_LIST },
2250 { "text/plain", 0, TARGET_TEXT_PLAIN }
2252 static gint n_collection_drag_types = 3;
2254 static GtkTargetEntry collection_drop_types[] = {
2255 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2256 { "text/uri-list", 0, TARGET_URI_LIST }
2258 static gint n_collection_drop_types = 2;
2261 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2262 GtkSelectionData *selection_data, guint info,
2263 guint time, gpointer data)
2265 CollectTable *ct = data;
2268 gchar *uri_text = NULL;
2271 if (!ct->click_info) return;
2273 selected = INFO_SELECTED(ct->click_info);
2277 case TARGET_APP_COLLECTION_MEMBER:
2280 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2284 list = g_list_append(NULL, ct->click_info);
2285 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2288 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2289 8, (guchar *)uri_text, total);
2292 case TARGET_URI_LIST:
2293 case TARGET_TEXT_PLAIN:
2297 list = collection_table_selection_get_list(ct);
2301 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2305 uri_selection_data_set_uris_from_filelist(selection_data, list);
2306 filelist_free(list);
2312 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2314 GtkSelectionData *selection_data, guint info,
2315 guint time, gpointer data)
2317 CollectTable *ct = data;
2319 GList *info_list = NULL;
2320 CollectionData *source;
2321 CollectInfo *drop_info;
2324 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2326 collection_table_scroll(ct, FALSE);
2327 collection_table_insert_marker(ct, NULL, FALSE);
2329 drop_info = collection_table_insert_point(ct, x, y);
2333 case TARGET_APP_COLLECTION_MEMBER:
2334 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2337 if (source == ct->cd)
2342 /* it is a move within a collection */
2343 filelist_free(list);
2348 collection_table_move_by_info_list(ct, info_list, -1, -1);
2350 else if (collection_table_find_position(ct, drop_info, &row, &col))
2352 collection_table_move_by_info_list(ct, info_list, row, col);
2357 /* it is a move/copy across collections */
2358 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2360 collection_remove_by_info_list(source, info_list);
2363 g_list_free(info_list);
2366 case TARGET_URI_LIST:
2367 list = uri_filelist_from_gtk_selection_data(selection_data);
2371 FileData *fd = work->data;
2372 if (isdir(fd->path))
2376 ct->drop_list = list;
2377 ct->drop_info = drop_info;
2378 menu = collection_table_drop_menu(ct);
2379 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2392 collection_table_insert_filelist(ct, list, drop_info);
2393 filelist_free(list);
2397 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2399 CollectTable *ct = data;
2401 if (ct->click_info && ct->click_info->pixbuf)
2405 if (INFO_SELECTED(ct->click_info))
2406 items = g_list_length(ct->selection);
2409 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2413 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2415 CollectTable *ct = data;
2417 /* apparently a leave event is not generated on a drop */
2420 collection_table_scroll(ct, FALSE);
2423 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2424 gint x, gint y, guint time, gpointer data)
2426 CollectTable *ct = data;
2428 collection_table_motion_update(ct, x, y, TRUE);
2429 collection_table_scroll(ct, TRUE);
2434 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2436 CollectTable *ct = data;
2438 collection_table_scroll(ct, FALSE);
2441 static void collection_table_dnd_init(CollectTable *ct)
2443 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2444 collection_drag_types, n_collection_drag_types,
2445 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2446 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2447 G_CALLBACK(collection_table_dnd_get), ct);
2448 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2449 G_CALLBACK(collection_table_dnd_begin), ct);
2450 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2451 G_CALLBACK(collection_table_dnd_end), ct);
2453 gtk_drag_dest_set(ct->listview,
2454 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2455 collection_drop_types, n_collection_drop_types,
2456 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2457 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2458 G_CALLBACK(collection_table_dnd_motion), ct);
2459 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2460 G_CALLBACK(collection_table_dnd_leave), ct);
2461 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2462 G_CALLBACK(collection_table_dnd_receive), ct);
2466 *-----------------------------------------------------------------------------
2468 *-----------------------------------------------------------------------------
2471 typedef struct _ColumnData ColumnData;
2478 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2479 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2481 ColumnData *cd = data;
2488 gchar *star_rating = NULL;
2489 gchar *display_text = NULL;
2493 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2495 #if GTK_CHECK_VERSION(3,0,0)
2496 /* FIXME this is a primitive hack to stop a crash.
2497 * When compiled with GTK3, if a Collection window containing
2498 * say, 50 or so, images has its width changed, there is a segfault
2499 * https://github.com/BestImageViewer/geeqie/issues/531
2501 if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2503 info = g_list_nth_data(list, cd->number);
2505 style = gtk_widget_get_style(ct->listview);
2506 if (info && (info->flag_mask & SELECTION_SELECTED) )
2508 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2509 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2513 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2514 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2517 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2519 shift_color(&color_bg, -1, 0);
2522 if (ct->show_stars && info && info->fd)
2524 star_rating = metadata_read_rating_stars(info->fd);
2528 star_rating = g_strdup("");
2531 if (info && info->fd)
2533 if (ct->show_text && ct->show_stars)
2535 display_text = g_strconcat(info->fd->name, "\n", star_rating, NULL);
2537 else if (ct->show_text)
2539 display_text = g_strdup(info->fd->name);
2541 else if (ct->show_stars)
2543 display_text = g_strdup(star_rating);
2547 display_text = g_strdup("");
2552 display_text = g_strdup("");
2555 if (GQV_IS_CELL_RENDERER_ICON(cell))
2559 g_object_set(cell, "pixbuf", info->pixbuf,
2560 "text", display_text,
2561 "cell-background-gdk", &color_bg,
2562 "cell-background-set", TRUE,
2563 "foreground-gdk", &color_fg,
2564 "foreground-set", TRUE,
2565 "has-focus", (ct->focus_info == info), NULL);
2569 g_object_set(cell, "pixbuf", NULL,
2571 "cell-background-set", FALSE,
2572 "foreground-set", FALSE,
2573 "has-focus", FALSE, NULL);
2577 g_free(display_text);
2578 g_free(star_rating);
2581 static void collection_table_append_column(CollectTable *ct, gint n)
2584 GtkTreeViewColumn *column;
2585 GtkCellRenderer *renderer;
2587 column = gtk_tree_view_column_new();
2588 gtk_tree_view_column_set_min_width(column, 0);
2590 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2591 gtk_tree_view_column_set_alignment(column, 0.5);
2593 renderer = gqv_cell_renderer_icon_new();
2594 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2595 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2596 "ypad", THUMB_BORDER_PADDING,
2597 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2599 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2601 cd = g_new0(ColumnData, 1);
2604 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2606 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2610 *-------------------------------------------------------------------
2612 *-------------------------------------------------------------------
2615 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2617 CollectTable *ct = data;
2619 /* If there is no unsaved data, save the window geometry
2621 if (!ct->cd->changed)
2623 if (!collection_save(ct->cd, ct->cd->path))
2625 log_printf("failed saving to collection path: %s\n", ct->cd->path);
2631 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2633 gtk_widget_destroy(ct->popup);
2636 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2639 collection_table_scroll(ct, FALSE);
2644 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2646 CollectTable *ct = data;
2648 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2651 CollectTable *collection_table_new(CollectionData *cd)
2654 GtkListStore *store;
2655 GtkTreeSelection *selection;
2658 ct = g_new0(CollectTable, 1);
2661 ct->show_text = options->show_icon_names;
2662 ct->show_stars = options->show_star_rating;
2664 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2665 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2666 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2667 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2669 store = gtk_list_store_new(1, G_TYPE_POINTER);
2670 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2671 g_object_unref(store);
2673 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2674 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2676 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2677 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2679 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2681 collection_table_append_column(ct, i);
2684 /* zero width column to hide tree view focus, we draw it ourselves */
2685 collection_table_append_column(ct, i);
2686 /* end column to fill white space */
2687 collection_table_append_column(ct, i);
2689 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2690 G_CALLBACK(collection_table_destroy), ct);
2691 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2692 G_CALLBACK(collection_table_sized), ct);
2693 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2694 G_CALLBACK(collection_table_press_key_cb), ct);
2696 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2697 gtk_widget_show(ct->listview);
2699 collection_table_dnd_init(ct);
2701 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2702 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2703 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2704 G_CALLBACK(collection_table_press_cb), ct);
2705 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2706 G_CALLBACK(collection_table_release_cb), ct);
2707 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2708 G_CALLBACK(collection_table_motion_cb), ct);
2709 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2710 G_CALLBACK(collection_table_leave_cb), ct);
2715 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2717 ct->status_label = status;
2718 ct->extra_label = extra;
2719 collection_table_update_status(ct);
2720 collection_table_update_extras(ct, FALSE, 0.0);
2723 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2725 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2728 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */