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;
1483 gint x_parent, y_parent;
1485 parent = gtk_widget_get_window(gtk_widget_get_toplevel(ct->listview));
1486 gdk_window_get_position(parent, &x_parent, &y_parent);
1490 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1491 ct->marker_window = NULL;
1496 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1498 /* this setting does not take into account (after), but since it is not really used... */
1499 ct->marker_info = info;
1501 if (!ct->marker_window)
1503 GdkWindowAttr attributes;
1504 gint attributes_mask;
1505 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1506 gint w = gdk_pixbuf_get_width(pb);
1507 gint h = gdk_pixbuf_get_height(pb);
1509 attributes.window_type = GDK_WINDOW_CHILD;
1510 attributes.wclass = GDK_INPUT_OUTPUT;
1511 attributes.width = w;
1512 attributes.height = h;
1513 attributes.event_mask = gtk_widget_get_events(ct->listview);
1514 attributes_mask = 0;
1516 ct->marker_window = gdk_window_new(NULL, &attributes, attributes_mask);
1518 #if GTK_CHECK_VERSION(3,0,0)
1519 cairo_region_t *mask;
1520 cairo_pattern_t *pattern;
1521 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1522 cairo_t *cr = cairo_create(img);
1523 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1525 // FIXME: this is a hack to get the background color
1526 //~ pattern = cairo_pattern_create_for_surface(img);
1527 pattern = cairo_pattern_create_rgb (1.0, 0.0, 0.0);
1528 mask = gdk_cairo_region_create_from_surface(img);
1529 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1530 gdk_window_set_background_pattern(ct->marker_window, pattern);
1531 cairo_region_destroy(mask);
1532 cairo_pattern_destroy(pattern);
1534 cairo_surface_destroy(img);
1538 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1540 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1541 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1543 g_object_unref(pixmap);
1544 if (mask) g_object_unref(mask);
1554 w = gdk_window_get_width(ct->marker_window);
1555 h = gdk_window_get_height(ct->marker_window);
1563 x = cell.x + cell.width;
1566 y = cell.y + (cell.height / 2) - (h / 2);
1571 gdk_window_move(ct->marker_window, x, y);
1572 #if !GTK_CHECK_VERSION(3,0,0)
1573 gdk_window_clear(ct->marker_window);
1575 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1579 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1584 *-------------------------------------------------------------------
1585 * mouse drag auto-scroll
1586 *-------------------------------------------------------------------
1589 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1593 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1598 collection_table_insert_marker(ct, info, TRUE);
1602 tip_update(ct, info);
1606 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1608 CollectTable *ct = data;
1612 #if GTK_CHECK_VERSION(3,0,0)
1613 GdkDeviceManager *device_manager;
1617 if (!ct->drop_idle_id) return FALSE;
1619 window = gtk_widget_get_window(ct->listview);
1620 #if GTK_CHECK_VERSION(3,0,0)
1621 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1622 device = gdk_device_manager_get_client_pointer(device_manager);
1623 gdk_window_get_device_position(window, device, &x, &y, NULL);
1625 gdk_window_get_pointer(window, &x, &y, NULL);
1627 w = gdk_window_get_width(window);
1628 h = gdk_window_get_height(window);
1629 if (x >= 0 && x < w && y >= 0 && y < h)
1631 collection_table_motion_update(ct, x, y, TRUE);
1634 ct->drop_idle_id = 0;
1638 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1640 CollectTable *ct = data;
1642 if (!ct->drop_idle_id)
1644 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1650 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1654 if (ct->drop_idle_id)
1656 g_source_remove(ct->drop_idle_id);
1657 ct->drop_idle_id = 0;
1659 widget_auto_scroll_stop(ct->listview);
1660 collection_table_insert_marker(ct, NULL, FALSE);
1664 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1665 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1666 collection_table_auto_scroll_notify_cb, ct);
1671 *-------------------------------------------------------------------
1673 *-------------------------------------------------------------------
1676 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1678 CollectTable *ct = data;
1680 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1685 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1687 CollectTable *ct = data;
1693 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1695 ct->click_info = info;
1696 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1698 switch (bevent->button)
1700 case MOUSE_BUTTON_LEFT:
1701 if (bevent->type == GDK_2BUTTON_PRESS)
1705 layout_image_set_collection(NULL, ct->cd, info);
1708 else if (!gtk_widget_has_focus(ct->listview))
1710 gtk_widget_grab_focus(ct->listview);
1713 case MOUSE_BUTTON_RIGHT:
1714 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1715 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1724 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1726 CollectTable *ct = data;
1728 CollectInfo *info = NULL;
1732 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1734 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1739 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1742 if (bevent->button == MOUSE_BUTTON_LEFT &&
1743 info && ct->click_info == info)
1745 collection_table_set_focus(ct, info);
1747 if (bevent->state & GDK_CONTROL_MASK)
1749 gboolean select = !INFO_SELECTED(info);
1751 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1753 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1757 collection_table_select_util(ct, info, select);
1762 collection_table_unselect_all(ct);
1764 if ((bevent->state & GDK_SHIFT_MASK) &&
1767 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1771 collection_table_select_util(ct, info, TRUE);
1775 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1776 info && ct->click_info == info)
1778 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1784 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1786 CollectTable *ct = data;
1793 *-------------------------------------------------------------------
1794 * populate, add, insert, etc.
1795 *-------------------------------------------------------------------
1798 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1802 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1808 static void collection_table_clear_store(CollectTable *ct)
1810 GtkTreeModel *store;
1812 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1813 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1815 gtk_list_store_clear(GTK_LIST_STORE(store));
1818 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1820 GtkListStore *store;
1824 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1826 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1827 gtk_list_store_append(store, iter);
1828 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1833 static void collection_table_populate(CollectTable *ct, gboolean resize)
1838 collection_table_verify_selections(ct);
1840 collection_table_clear_store(ct);
1847 thumb_width = collection_table_get_icon_width(ct);
1849 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1851 GtkTreeViewColumn *column;
1852 GtkCellRenderer *cell;
1855 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1856 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1857 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1859 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1860 cell = (list) ? list->data : NULL;
1863 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1865 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1866 "fixed_height", options->thumbnails.max_height,
1867 "show_text", ct->show_text || ct->show_stars, NULL);
1870 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1874 work = ct->cd->list;
1882 list = collection_table_add_row(ct, &iter);
1883 while (work && list)
1885 list->data = work->data;
1893 collection_table_update_focus(ct);
1894 collection_table_update_status(ct);
1897 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1902 thumb_width = collection_table_get_icon_width(ct);
1904 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1905 if (new_cols < 1) new_cols = 1;
1907 if (!force && new_cols == ct->columns) return;
1909 ct->columns = new_cols;
1911 collection_table_populate(ct, TRUE);
1913 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1916 static void collection_table_sync(CollectTable *ct)
1918 GtkTreeModel *store;
1923 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1928 work = ct->cd->list;
1934 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1936 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1937 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1941 list = collection_table_add_row(ct, &iter);
1966 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1970 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1971 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1977 collection_table_update_focus(ct);
1978 collection_table_update_status(ct);
1981 static gboolean collection_table_sync_idle_cb(gpointer data)
1983 CollectTable *ct = data;
1985 if (!ct->sync_idle_id) return FALSE;
1986 g_source_remove(ct->sync_idle_id);
1987 ct->sync_idle_id = 0;
1989 collection_table_sync(ct);
1993 static void collection_table_sync_idle(CollectTable *ct)
1995 if (!ct->sync_idle_id)
1997 /* high priority, the view needs to be resynced before a redraw
1998 * may contain invalid pointers at this time
2000 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
2004 void collection_table_add_filelist(CollectTable *ct, GList *list)
2013 collection_add(ct->cd, (FileData *)work->data, FALSE);
2018 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
2027 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
2031 collection_table_sync_idle(ct);
2034 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
2037 GList *insert_pos = NULL;
2041 if (!info_list) return;
2043 info = collection_table_find_data(ct, row, col, NULL);
2045 if (!info_list->next && info_list->data == info) return;
2047 if (info) insert_pos = g_list_find(ct->cd->list, info);
2049 /* FIXME: this may get slow for large lists */
2051 while (insert_pos && work)
2053 if (insert_pos->data == work->data)
2055 insert_pos = insert_pos->next;
2067 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2071 /* place them back in */
2072 temp = g_list_copy(info_list);
2076 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2080 ct->cd->list = g_list_concat(temp, ct->cd->list);
2084 ct->cd->list = g_list_concat(ct->cd->list, temp);
2087 ct->cd->changed = TRUE;
2089 collection_table_sync_idle(ct);
2094 *-------------------------------------------------------------------
2096 *-------------------------------------------------------------------
2099 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2107 collection_table_update_extras(ct, FALSE, 0.0);
2111 if (!collection_table_find_position(ct, info, &row, &col)) return;
2113 if (ct->columns != 0 && ct->rows != 0)
2115 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2122 collection_table_update_extras(ct, TRUE, value);
2124 if (collection_table_find_iter(ct, info, &iter, NULL))
2126 GtkTreeModel *store;
2129 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2130 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2131 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2135 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2137 collection_table_sync_idle(ct);
2140 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2142 collection_table_sync_idle(ct);
2145 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2147 if (ci && INFO_SELECTED(ci))
2149 ct->selection = g_list_remove(ct->selection, ci);
2152 collection_table_sync_idle(ct);
2155 void collection_table_refresh(CollectTable *ct)
2157 collection_table_populate(ct, FALSE);
2161 *-------------------------------------------------------------------
2163 *-------------------------------------------------------------------
2166 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2172 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2175 f = filelist_filter(f, FALSE);
2176 d = filelist_filter(d, TRUE);
2178 f = filelist_sort_path(f);
2179 d = filelist_sort_path(d);
2181 collection_table_insert_filelist(ct, f, ct->marker_info);
2183 work = g_list_last(d);
2186 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2194 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2199 FileData *fd = work->data;
2201 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2203 collection_table_insert_filelist(ct, list, ct->marker_info);
2207 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2209 CollectTable *ct = data;
2211 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2214 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2216 CollectTable *ct = data;
2218 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2221 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2223 CollectTable *ct = data;
2225 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2228 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2232 menu = popup_menu_short_lived();
2233 g_signal_connect(G_OBJECT(menu), "destroy",
2234 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2236 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2237 menu_item_add_divider(menu);
2238 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2239 G_CALLBACK(confirm_dir_list_add), ct);
2240 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2241 G_CALLBACK(confirm_dir_list_recurse), ct);
2242 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2243 G_CALLBACK(confirm_dir_list_skip), ct);
2244 menu_item_add_divider(menu);
2245 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2251 *-------------------------------------------------------------------
2253 *-------------------------------------------------------------------
2256 static GtkTargetEntry collection_drag_types[] = {
2257 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2258 { "text/uri-list", 0, TARGET_URI_LIST },
2259 { "text/plain", 0, TARGET_TEXT_PLAIN }
2261 static gint n_collection_drag_types = 3;
2263 static GtkTargetEntry collection_drop_types[] = {
2264 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2265 { "text/uri-list", 0, TARGET_URI_LIST }
2267 static gint n_collection_drop_types = 2;
2270 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2271 GtkSelectionData *selection_data, guint info,
2272 guint time, gpointer data)
2274 CollectTable *ct = data;
2277 gchar *uri_text = NULL;
2280 if (!ct->click_info) return;
2282 selected = INFO_SELECTED(ct->click_info);
2286 case TARGET_APP_COLLECTION_MEMBER:
2289 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2293 list = g_list_append(NULL, ct->click_info);
2294 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2297 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2298 8, (guchar *)uri_text, total);
2301 case TARGET_URI_LIST:
2302 case TARGET_TEXT_PLAIN:
2306 list = collection_table_selection_get_list(ct);
2310 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2314 uri_selection_data_set_uris_from_filelist(selection_data, list);
2315 filelist_free(list);
2321 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2323 GtkSelectionData *selection_data, guint info,
2324 guint time, gpointer data)
2326 CollectTable *ct = data;
2328 GList *info_list = NULL;
2329 CollectionData *source;
2330 CollectInfo *drop_info;
2333 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2335 collection_table_scroll(ct, FALSE);
2336 collection_table_insert_marker(ct, NULL, FALSE);
2338 drop_info = collection_table_insert_point(ct, x, y);
2342 case TARGET_APP_COLLECTION_MEMBER:
2343 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2346 if (source == ct->cd)
2351 /* it is a move within a collection */
2352 filelist_free(list);
2357 collection_table_move_by_info_list(ct, info_list, -1, -1);
2359 else if (collection_table_find_position(ct, drop_info, &row, &col))
2361 collection_table_move_by_info_list(ct, info_list, row, col);
2366 /* it is a move/copy across collections */
2367 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2369 collection_remove_by_info_list(source, info_list);
2372 g_list_free(info_list);
2375 case TARGET_URI_LIST:
2376 list = uri_filelist_from_gtk_selection_data(selection_data);
2380 FileData *fd = work->data;
2381 if (isdir(fd->path))
2385 ct->drop_list = list;
2386 ct->drop_info = drop_info;
2387 menu = collection_table_drop_menu(ct);
2388 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2401 collection_table_insert_filelist(ct, list, drop_info);
2402 filelist_free(list);
2406 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2408 CollectTable *ct = data;
2410 if (ct->click_info && ct->click_info->pixbuf)
2414 if (INFO_SELECTED(ct->click_info))
2415 items = g_list_length(ct->selection);
2418 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2422 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2424 CollectTable *ct = data;
2426 /* apparently a leave event is not generated on a drop */
2429 collection_table_scroll(ct, FALSE);
2432 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2433 gint x, gint y, guint time, gpointer data)
2435 CollectTable *ct = data;
2437 collection_table_motion_update(ct, x, y, TRUE);
2438 collection_table_scroll(ct, TRUE);
2443 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2445 CollectTable *ct = data;
2447 collection_table_scroll(ct, FALSE);
2450 static void collection_table_dnd_init(CollectTable *ct)
2452 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2453 collection_drag_types, n_collection_drag_types,
2454 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2455 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2456 G_CALLBACK(collection_table_dnd_get), ct);
2457 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2458 G_CALLBACK(collection_table_dnd_begin), ct);
2459 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2460 G_CALLBACK(collection_table_dnd_end), ct);
2462 gtk_drag_dest_set(ct->listview,
2463 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2464 collection_drop_types, n_collection_drop_types,
2465 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2466 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2467 G_CALLBACK(collection_table_dnd_motion), ct);
2468 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2469 G_CALLBACK(collection_table_dnd_leave), ct);
2470 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2471 G_CALLBACK(collection_table_dnd_receive), ct);
2475 *-----------------------------------------------------------------------------
2477 *-----------------------------------------------------------------------------
2480 typedef struct _ColumnData ColumnData;
2487 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2488 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2490 ColumnData *cd = data;
2497 gchar *star_rating = NULL;
2498 gchar *display_text = NULL;
2502 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2504 #if GTK_CHECK_VERSION(3,0,0)
2505 /* FIXME this is a primitive hack to stop a crash.
2506 * When compiled with GTK3, if a Collection window containing
2507 * say, 50 or so, images has its width changed, there is a segfault
2508 * https://github.com/BestImageViewer/geeqie/issues/531
2510 if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2512 info = g_list_nth_data(list, cd->number);
2514 style = gtk_widget_get_style(ct->listview);
2515 if (info && (info->flag_mask & SELECTION_SELECTED) )
2517 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2518 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2522 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2523 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2526 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2528 shift_color(&color_bg, -1, 0);
2531 if (ct->show_stars && info && info->fd)
2533 star_rating = metadata_read_rating_stars(info->fd);
2537 star_rating = g_strdup("");
2540 if (info && info->fd)
2542 if (ct->show_text && ct->show_stars)
2544 display_text = g_strconcat(info->fd->name, "\n", star_rating, NULL);
2546 else if (ct->show_text)
2548 display_text = g_strdup(info->fd->name);
2550 else if (ct->show_stars)
2552 display_text = g_strdup(star_rating);
2556 display_text = g_strdup("");
2561 display_text = g_strdup("");
2564 if (GQV_IS_CELL_RENDERER_ICON(cell))
2568 g_object_set(cell, "pixbuf", info->pixbuf,
2569 "text", display_text,
2570 "cell-background-gdk", &color_bg,
2571 "cell-background-set", TRUE,
2572 "foreground-gdk", &color_fg,
2573 "foreground-set", TRUE,
2574 "has-focus", (ct->focus_info == info), NULL);
2578 g_object_set(cell, "pixbuf", NULL,
2580 "cell-background-set", FALSE,
2581 "foreground-set", FALSE,
2582 "has-focus", FALSE, NULL);
2586 g_free(display_text);
2587 g_free(star_rating);
2590 static void collection_table_append_column(CollectTable *ct, gint n)
2593 GtkTreeViewColumn *column;
2594 GtkCellRenderer *renderer;
2596 column = gtk_tree_view_column_new();
2597 gtk_tree_view_column_set_min_width(column, 0);
2599 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2600 gtk_tree_view_column_set_alignment(column, 0.5);
2602 renderer = gqv_cell_renderer_icon_new();
2603 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2604 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2605 "ypad", THUMB_BORDER_PADDING,
2606 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2608 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2610 cd = g_new0(ColumnData, 1);
2613 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2615 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2619 *-------------------------------------------------------------------
2621 *-------------------------------------------------------------------
2624 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2626 CollectTable *ct = data;
2628 /* If there is no unsaved data, save the window geometry
2630 if (!ct->cd->changed)
2632 if (!collection_save(ct->cd, ct->cd->path))
2634 log_printf("failed saving to collection path: %s\n", ct->cd->path);
2640 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2642 gtk_widget_destroy(ct->popup);
2645 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2648 collection_table_scroll(ct, FALSE);
2653 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2655 CollectTable *ct = data;
2657 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2660 CollectTable *collection_table_new(CollectionData *cd)
2663 GtkListStore *store;
2664 GtkTreeSelection *selection;
2667 ct = g_new0(CollectTable, 1);
2670 ct->show_text = options->show_icon_names;
2671 ct->show_stars = options->show_star_rating;
2673 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2674 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2675 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2676 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2678 store = gtk_list_store_new(1, G_TYPE_POINTER);
2679 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2680 g_object_unref(store);
2682 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2683 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2685 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2686 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2688 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2690 collection_table_append_column(ct, i);
2693 /* zero width column to hide tree view focus, we draw it ourselves */
2694 collection_table_append_column(ct, i);
2695 /* end column to fill white space */
2696 collection_table_append_column(ct, i);
2698 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2699 G_CALLBACK(collection_table_destroy), ct);
2700 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2701 G_CALLBACK(collection_table_sized), ct);
2702 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2703 G_CALLBACK(collection_table_press_key_cb), ct);
2705 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2706 gtk_widget_show(ct->listview);
2708 collection_table_dnd_init(ct);
2710 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2711 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2712 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2713 G_CALLBACK(collection_table_press_cb), ct);
2714 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2715 G_CALLBACK(collection_table_release_cb), ct);
2716 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2717 G_CALLBACK(collection_table_motion_cb), ct);
2718 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2719 G_CALLBACK(collection_table_leave_cb), ct);
2724 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2726 ct->status_label = status;
2727 ct->extra_label = extra;
2728 collection_table_update_status(ct);
2729 collection_table_update_extras(ct, FALSE, 0.0);
2732 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2734 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2737 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */