2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "collect-table.h"
25 #include "cellrenderericon.h"
26 #include "collect-dlg.h"
27 #include "collect-io.h"
34 #include "layout_image.h"
38 #include "ui_fileops.h"
40 #include "ui_tree_edit.h"
41 #include "uri_utils.h"
43 #include "icons/marker.xpm"
44 #define MARKER_WIDTH 26
45 #define MARKER_HEIGHT 32
47 #include <gdk/gdkkeysyms.h> /* for keyboard values */
49 /* between these, the icon width is increased by thumb_max_width / 2 */
50 #define THUMB_MIN_ICON_WIDTH 128
51 #define THUMB_MAX_ICON_WIDTH 150
53 #define COLLECT_TABLE_MAX_COLUMNS 32
54 #define THUMB_BORDER_PADDING 2
56 #define COLLECT_TABLE_TIP_DELAY 500
57 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
61 CTABLE_COLUMN_POINTER = 0,
67 SELECTION_SELECTED = 1 << 0,
68 SELECTION_PRELIGHT = 1 << 1,
69 SELECTION_FOCUS = 1 << 2
73 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
76 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
80 *-------------------------------------------------------------------
82 *-------------------------------------------------------------------
85 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
89 n = g_list_index(ct->cd->list, info);
91 if (n < 0) return FALSE;
93 *row = n / ct->columns;
94 *col = n - (*row * ct->columns);
99 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
104 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
105 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
106 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
107 if (column) *column = col;
112 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
117 if (row < 0 || col < 0) return NULL;
119 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
120 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
124 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
125 if (!list) return NULL;
129 return g_list_nth_data(list, col);
135 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
138 GtkTreeViewColumn *column;
144 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
145 &tpath, &column, NULL, NULL))
148 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
149 gtk_tree_model_get_iter(store, &row, tpath);
150 gtk_tree_path_free(tpath);
152 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
153 if (!list) return NULL;
155 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
156 if (iter) *iter = row;
157 return g_list_nth_data(list, n);
160 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
170 CollectInfo *ci = work->data;
178 return g_list_length(ct->cd->list);
181 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
188 work = ct->selection;
191 CollectInfo *ci = work->data;
199 return g_list_length(ct->selection);
202 static void collection_table_update_status(CollectTable *ct)
210 if (!ct->status_label) return;
212 n = collection_table_list_count(ct, &n_bytes);
213 s = collection_table_selection_count(ct, &s_bytes);
217 gchar *b = text_from_size_abrev(n_bytes);
218 gchar *sb = text_from_size_abrev(s_bytes);
219 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
225 gchar *b = text_from_size_abrev(n_bytes);
226 buf = g_strdup_printf(_("%s, %d images"), b, n);
231 buf = g_strdup(_("Empty"));
234 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
238 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
242 if (!ct->extra_label) return;
245 text = _("Loading thumbs...");
249 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
250 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
253 static void collection_table_toggle_filenames(CollectTable *ct)
255 GtkAllocation allocation;
256 ct->show_text = !ct->show_text;
257 options->show_icon_names = ct->show_text;
259 gtk_widget_get_allocation(ct->listview, &allocation);
260 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
263 static gint collection_table_get_icon_width(CollectTable *ct)
267 if (!ct->show_text) return options->thumbnails.max_width;
269 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
270 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
271 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
277 *-------------------------------------------------------------------
279 *-------------------------------------------------------------------
282 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
289 if (info->flag_mask == value) return;
290 info->flag_mask = value;
292 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
295 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
296 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
302 if (collection_table_find_iter(ct, info, &row, NULL))
304 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
305 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
310 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
314 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
317 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
321 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
324 *-------------------------------------------------------------------
326 *-------------------------------------------------------------------
329 static void collection_table_verify_selections(CollectTable *ct)
333 work = ct->selection;
336 CollectInfo *info = work->data;
338 if (!g_list_find(ct->cd->list, info))
340 ct->selection = g_list_remove(ct->selection, info);
345 void collection_table_select_all(CollectTable *ct)
349 g_list_free(ct->selection);
350 ct->selection = NULL;
355 ct->selection = g_list_append(ct->selection, work->data);
356 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
360 collection_table_update_status(ct);
363 void collection_table_unselect_all(CollectTable *ct)
367 work = ct->selection;
370 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
374 g_list_free(ct->selection);
375 ct->selection = NULL;
377 collection_table_update_status(ct);
380 /* Invert the current collection's selection */
381 static void collection_table_select_invert_all(CollectTable *ct)
384 GList *new_selection = NULL;
389 CollectInfo *info = work->data;
391 if (INFO_SELECTED(info))
393 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
397 new_selection = g_list_append(new_selection, info);
398 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
405 g_list_free(ct->selection);
406 ct->selection = new_selection;
408 collection_table_update_status(ct);
411 static void collection_table_select(CollectTable *ct, CollectInfo *info)
413 ct->prev_selection = info;
415 if (!info || INFO_SELECTED(info)) return;
417 ct->selection = g_list_append(ct->selection, info);
418 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
420 collection_table_update_status(ct);
423 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
425 ct->prev_selection = info;
427 if (!info || !INFO_SELECTED(info) ) return;
429 ct->selection = g_list_remove(ct->selection, info);
430 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
432 collection_table_update_status(ct);
435 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
439 collection_table_select(ct, info);
443 collection_table_unselect(ct, info);
447 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
454 if (!collection_table_find_position(ct, start, &row1, &col1) ||
455 !collection_table_find_position(ct, end, &row2, &col2) ) return;
457 ct->prev_selection = end;
459 if (!options->collections.rectangular_selection)
464 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
471 work = g_list_find(ct->cd->list, start);
475 collection_table_select_util(ct, info, select);
477 if (work->data != end)
498 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
500 for (i = row1; i <= row2; i++)
502 for (j = col1; j <= col2; j++)
504 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
505 if (info) collection_table_select_util(ct, info, select);
510 GList *collection_table_selection_get_list(CollectTable *ct)
512 return collection_list_to_filelist(ct->selection);
515 static GList *collection_table_get_list(CollectTable *ct)
517 return collection_list_to_filelist(ct->cd->list);
521 *-------------------------------------------------------------------
522 * tooltip type window
523 *-------------------------------------------------------------------
526 static void tip_show(CollectTable *ct)
530 #if GTK_CHECK_VERSION(3,0,0)
532 GdkDeviceManager *device_manager;
536 if (ct->tip_window) return;
538 #if GTK_CHECK_VERSION(3,0,0)
539 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
540 gtk_widget_get_window(ct->listview)));
541 device = gdk_device_manager_get_client_pointer(device_manager);
542 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
543 device, &x, &y, NULL);
545 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
548 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
549 if (!ct->tip_info) return;
551 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
552 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
553 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
555 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
557 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
558 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
559 gtk_widget_show(label);
561 #if GTK_CHECK_VERSION(3,0,0)
562 display = gdk_display_get_default();
563 device_manager = gdk_display_get_device_manager(display);
564 device = gdk_device_manager_get_client_pointer(device_manager);
565 gdk_device_get_position(device, NULL, &x, &y);
567 gdk_window_get_pointer(NULL, &x, &y, NULL);
570 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
571 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
572 gtk_widget_show(ct->tip_window);
575 static void tip_hide(CollectTable *ct)
577 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
578 ct->tip_window = NULL;
581 static gboolean tip_schedule_cb(gpointer data)
583 CollectTable *ct = data;
585 if (!ct->tip_delay_id) return FALSE;
589 ct->tip_delay_id = 0;
593 static void tip_schedule(CollectTable *ct)
597 if (ct->tip_delay_id)
599 g_source_remove(ct->tip_delay_id);
600 ct->tip_delay_id = 0;
603 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
606 static void tip_unschedule(CollectTable *ct)
610 if (ct->tip_delay_id)
612 g_source_remove(ct->tip_delay_id);
613 ct->tip_delay_id = 0;
617 static void tip_update(CollectTable *ct, CollectInfo *info)
619 #if GTK_CHECK_VERSION(3,0,0)
620 GdkDisplay *display = gdk_display_get_default();
621 GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
622 GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
630 #if GTK_CHECK_VERSION(3,0,0)
631 gdk_device_get_position(device, NULL, &x, &y);
633 gdk_window_get_pointer(NULL, &x, &y, NULL);
635 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
637 if (info != ct->tip_info)
648 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
649 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
655 *-------------------------------------------------------------------
657 *-------------------------------------------------------------------
660 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
662 CollectTable *ct = data;
664 collection_dialog_save_as(NULL, ct->cd);
667 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
669 CollectTable *ct = data;
673 collection_table_popup_save_as_cb(widget, data);
677 if (!collection_save(ct->cd, ct->cd->path))
679 log_printf("failed saving to collection path: %s\n", ct->cd->path);
683 static GList *collection_table_popup_file_list(CollectTable *ct)
685 if (!ct->click_info) return NULL;
687 if (INFO_SELECTED(ct->click_info))
689 return collection_table_selection_get_list(ct);
692 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
695 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
698 const gchar *key = data;
700 ct = submenu_item_get_data(widget);
704 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
707 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
709 CollectTable *ct = data;
711 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
714 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
716 CollectTable *ct = data;
718 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
721 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
723 CollectTable *ct = data;
725 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
728 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
730 CollectTable *ct = data;
732 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
735 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
737 CollectTable *ct = data;
739 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct));
742 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
747 ct = submenu_item_get_data(widget);
751 type = (SortType)GPOINTER_TO_INT(data);
753 collection_set_sort_method(ct->cd, type);
756 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
760 ct = submenu_item_get_data(widget);
764 collection_randomize(ct->cd);
767 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
769 CollectTable *ct = data;
771 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
773 view_window_new_from_collection(ct->cd, ct->click_info);
777 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
779 CollectTable *ct = data;
781 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
783 layout_image_set_collection(NULL, ct->cd, ct->click_info);
787 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
789 CollectTable *ct = data;
791 collection_table_select_all(ct);
792 ct->prev_selection= ct->click_info;
795 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
797 CollectTable *ct = data;
799 collection_table_unselect_all(ct);
800 ct->prev_selection= ct->click_info;
803 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
805 CollectTable *ct = data;
807 collection_table_select_invert_all(ct);
808 ct->prev_selection= ct->click_info;
811 static void collection_table_popup_rectangular_selection_cb(GtkWidget *widget, gpointer data)
813 CollectTable *ct = data;
815 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
818 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
820 CollectTable *ct = data;
823 if (!ct->click_info) return;
825 if (INFO_SELECTED(ct->click_info))
827 list = g_list_copy(ct->selection);
831 list = g_list_append(NULL, ct->click_info);
834 collection_remove_by_info_list(ct->cd, list);
838 static void collection_table_popup_add_filelist_cb(GtkWidget *widget, gpointer data)
840 CollectTable *ct = data;
843 list = layout_list(NULL);
847 collection_table_add_filelist(ct, list);
852 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
854 CollectTable *ct = data;
856 collection_dialog_append(NULL, ct->cd);
859 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
861 CollectTable *ct = data;
864 dw = dupe_window_new();
865 dupe_window_add_collection(dw, ct->cd);
868 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
870 CollectTable *ct = data;
873 fd = (ct->click_info) ? ct->click_info->fd : NULL;
875 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
878 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
880 CollectTable *ct = data;
882 collection_table_toggle_filenames(ct);
885 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
887 CollectTable *ct = data;
889 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
890 ct->click_info = NULL;
893 filelist_free(ct->drop_list);
894 ct->drop_list = NULL;
895 ct->drop_info = NULL;
897 filelist_free(ct->editmenu_fd_list);
898 ct->editmenu_fd_list = NULL;
901 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
907 menu = popup_menu_short_lived();
909 g_signal_connect(G_OBJECT(menu), "destroy",
910 G_CALLBACK(collection_table_popup_destroy_cb), ct);
912 menu_item_add_sensitive(menu, _("_View"), over_icon,
913 G_CALLBACK(collection_table_popup_view_cb), ct);
914 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
915 G_CALLBACK(collection_table_popup_view_new_cb), ct);
916 menu_item_add_divider(menu);
917 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
918 G_CALLBACK(collection_table_popup_remove_cb), ct);
920 menu_item_add_stock(menu, _("Append from file list"), GTK_STOCK_ADD,
921 G_CALLBACK(collection_table_popup_add_filelist_cb), ct);
922 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
923 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
924 menu_item_add_divider(menu);
926 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
927 submenu = gtk_menu_new();
928 menu_item_add(submenu, _("Select all"),
929 G_CALLBACK(collection_table_popup_selectall_cb), ct);
930 menu_item_add(submenu, _("Select none"),
931 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
932 menu_item_add(submenu, _("Invert selection"),
933 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
934 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
935 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
936 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
937 menu_item_add_divider(menu);
940 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
941 submenu_add_edit(menu, &item,
942 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
943 gtk_widget_set_sensitive(item, over_icon);
945 menu_item_add_divider(menu);
946 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
947 G_CALLBACK(collection_table_popup_copy_cb), ct);
948 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
949 G_CALLBACK(collection_table_popup_move_cb), ct);
950 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
951 G_CALLBACK(collection_table_popup_rename_cb), ct);
952 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
953 G_CALLBACK(collection_table_popup_delete_cb), ct);
954 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
955 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
956 menu_item_add_divider(menu);
958 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
959 menu_item_add_divider(submenu);
960 menu_item_add(submenu, _("Randomize"),
961 G_CALLBACK(collection_table_popup_randomize_cb), ct);
962 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
963 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
965 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
966 G_CALLBACK(collection_table_popup_show_names_cb), ct);
967 menu_item_add_divider(menu);
968 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
969 G_CALLBACK(collection_table_popup_save_cb), ct);
970 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
971 G_CALLBACK(collection_table_popup_save_as_cb), ct);
972 menu_item_add_divider(menu);
973 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
974 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
975 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
976 G_CALLBACK(collection_table_popup_print_cb), ct);
981 *-------------------------------------------------------------------
983 *-------------------------------------------------------------------
986 static void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
991 if (g_list_find(ct->cd->list, ct->focus_info))
993 if (info == ct->focus_info)
995 /* ensure focus row col are correct */
996 collection_table_find_position(ct, ct->focus_info,
997 &ct->focus_row, &ct->focus_column);
1000 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1003 if (!collection_table_find_position(ct, info, &row, &col))
1005 ct->focus_info = NULL;
1007 ct->focus_column = -1;
1011 ct->focus_info = info;
1012 ct->focus_row = row;
1013 ct->focus_column = col;
1014 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1016 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1019 GtkTreeViewColumn *column;
1020 GtkTreeModel *store;
1022 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1024 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1025 tpath = gtk_tree_model_get_path(store, &iter);
1026 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1027 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1028 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1029 gtk_tree_path_free(tpath);
1033 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1040 new_row = ct->focus_row;
1041 new_col = ct->focus_column;
1044 if (new_row < 0) new_row = 0;
1045 if (new_row >= ct->rows) new_row = ct->rows - 1;
1065 new_col = ct->columns - 1;
1072 if (new_col >= ct->columns)
1074 if (new_row < ct->rows - 1)
1081 new_col = ct->columns - 1;
1091 if (new_row >= ct->rows)
1094 new_row = ct->rows - 1;
1097 new_col = ct->columns - 1;
1099 if (new_col >= ct->columns) new_col = ct->columns - 1;
1102 if (new_row == ct->rows - 1)
1106 /* if we moved beyond the last image, go to the last image */
1108 l = g_list_length(ct->cd->list);
1109 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1110 if (new_col >= l) new_col = l - 1;
1113 if (new_row == -1 || new_col == -1)
1115 if (!ct->cd->list) return;
1116 new_row = new_col = 0;
1119 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1122 static void collection_table_update_focus(CollectTable *ct)
1127 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1129 /* first find the old focus, if it exists and is valid */
1133 /* (try to) stay where we were */
1134 new_row = ct->focus_row;
1135 new_col = ct->focus_column;
1138 collection_table_move_focus(ct, new_row, new_col, FALSE);
1141 /* used to figure the page up/down distances */
1142 static gint page_height(CollectTable *ct)
1149 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1150 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1152 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1153 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1155 ret = page_size / row_height;
1156 if (ret < 1) ret = 1;
1161 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1163 CollectTable *ct = data;
1164 GtkTreeModel *store;
1170 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1171 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1172 tpath = gtk_tree_model_get_path(store, &iter);
1173 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1174 gtk_tree_path_free(tpath);
1176 popup_menu_position_clamp(menu, x, y, 0);
1179 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1181 CollectTable *ct = data;
1185 gboolean stop_signal = TRUE;
1187 switch (event->keyval)
1189 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1192 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1195 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1198 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1201 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1202 focus_row = -page_height(ct);
1204 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1205 focus_row = page_height(ct);
1207 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1208 focus_row = -ct->focus_row;
1209 focus_col = -ct->focus_column;
1211 case GDK_KEY_End: case GDK_KEY_KP_End:
1212 focus_row = ct->rows - 1 - ct->focus_row;
1213 focus_col = ct->columns - 1 - ct->focus_column;
1216 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1219 ct->click_info = info;
1220 if (event->state & GDK_CONTROL_MASK)
1222 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1226 collection_table_unselect_all(ct);
1227 collection_table_select(ct, info);
1232 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1236 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1237 ct->click_info = info;
1239 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1242 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1243 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1246 stop_signal = FALSE;
1250 if (focus_row != 0 || focus_col != 0)
1252 CollectInfo *new_info;
1253 CollectInfo *old_info;
1255 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1256 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1257 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1259 if (new_info != old_info)
1261 if (event->state & GDK_SHIFT_MASK)
1263 if (!options->collections.rectangular_selection)
1265 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1269 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1271 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1273 else if (event->state & GDK_CONTROL_MASK)
1275 ct->click_info = new_info;
1279 ct->click_info = new_info;
1280 collection_table_unselect_all(ct);
1281 collection_table_select(ct, new_info);
1295 *-------------------------------------------------------------------
1297 *-------------------------------------------------------------------
1300 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1301 gboolean use_coord, gint x, gint y)
1303 CollectInfo *info = NULL;
1304 GtkTreeModel *store;
1307 GtkTreeViewColumn *column;
1308 #if GTK_CHECK_VERSION(3,0,0)
1309 GdkDeviceManager *device_manager;
1313 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1315 #if GTK_CHECK_VERSION(3,0,0)
1318 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1319 gtk_widget_get_window(ct->listview)));
1320 device = gdk_device_manager_get_client_pointer(device_manager);
1321 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1322 device, &x, &y, NULL);
1325 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1330 if (collection_table_find_iter(ct, source, &iter, &col))
1332 tpath = gtk_tree_model_get_path(store, &iter);
1333 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1334 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1335 gtk_tree_path_free(tpath);
1338 *after = !!(x > cell->x + (cell->width / 2));
1343 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1344 &tpath, &column, NULL, NULL))
1349 gtk_tree_model_get_iter(store, &iter, tpath);
1350 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1352 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1353 info = g_list_nth_data(list, n);
1357 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1358 *after = !!(x > cell->x + (cell->width / 2));
1361 gtk_tree_path_free(tpath);
1368 work = g_list_last(ct->cd->list);
1376 if (collection_table_find_iter(ct, info, &iter, &col))
1378 tpath = gtk_tree_model_get_path(store, &iter);
1379 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1380 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1381 gtk_tree_path_free(tpath);
1389 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1393 gboolean after = FALSE;
1395 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1401 work = g_list_find(ct->cd->list, info);
1402 if (work && work->next)
1404 info = work->next->data;
1415 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1417 gboolean after = FALSE;
1422 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1423 ct->marker_window = NULL;
1428 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1430 /* this setting does not take into account (after), but since it is not really used... */
1431 ct->marker_info = info;
1433 if (!ct->marker_window)
1435 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1436 GdkWindowAttr attributes;
1437 gint attributes_mask;
1438 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1439 gint w = gdk_pixbuf_get_width(pb);
1440 gint h = gdk_pixbuf_get_height(pb);
1442 attributes.window_type = GDK_WINDOW_CHILD;
1443 attributes.wclass = GDK_INPUT_OUTPUT;
1444 attributes.width = w;
1445 attributes.height = h;
1446 attributes.event_mask = gtk_widget_get_events(ct->listview);
1447 attributes_mask = 0;
1449 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1451 #if GTK_CHECK_VERSION(3,0,0)
1452 cairo_region_t *mask;
1453 cairo_pattern_t *pattern;
1454 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1455 cairo_t *cr = cairo_create(img);
1456 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1458 pattern = cairo_pattern_create_for_surface(img);
1459 mask = gdk_cairo_region_create_from_surface(img);
1460 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1461 gdk_window_set_background_pattern(ct->marker_window, pattern);
1462 cairo_region_destroy(mask);
1463 cairo_pattern_destroy(pattern);
1465 cairo_surface_destroy(img);
1469 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1471 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1472 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1474 g_object_unref(pixmap);
1475 if (mask) g_object_unref(mask);
1485 w = gdk_window_get_width(ct->marker_window);
1486 h = gdk_window_get_height(ct->marker_window);
1494 x = cell.x + cell.width;
1497 y = cell.y + (cell.height / 2) - (h / 2);
1499 gdk_window_move(ct->marker_window, x, y);
1500 #if !GTK_CHECK_VERSION(3,0,0)
1501 gdk_window_clear(ct->marker_window);
1503 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1507 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1512 *-------------------------------------------------------------------
1513 * mouse drag auto-scroll
1514 *-------------------------------------------------------------------
1517 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1521 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1526 collection_table_insert_marker(ct, info, TRUE);
1530 tip_update(ct, info);
1534 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1536 CollectTable *ct = data;
1540 #if GTK_CHECK_VERSION(3,0,0)
1541 GdkDeviceManager *device_manager;
1545 if (!ct->drop_idle_id) return FALSE;
1547 window = gtk_widget_get_window(ct->listview);
1548 #if GTK_CHECK_VERSION(3,0,0)
1549 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1550 device = gdk_device_manager_get_client_pointer(device_manager);
1551 gdk_window_get_device_position(window, device, &x, &y, NULL);
1553 gdk_window_get_pointer(window, &x, &y, NULL);
1555 w = gdk_window_get_width(window);
1556 h = gdk_window_get_height(window);
1557 if (x >= 0 && x < w && y >= 0 && y < h)
1559 collection_table_motion_update(ct, x, y, TRUE);
1562 ct->drop_idle_id = 0;
1566 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1568 CollectTable *ct = data;
1570 if (!ct->drop_idle_id)
1572 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1578 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1582 if (ct->drop_idle_id)
1584 g_source_remove(ct->drop_idle_id);
1585 ct->drop_idle_id = 0;
1587 widget_auto_scroll_stop(ct->listview);
1588 collection_table_insert_marker(ct, NULL, FALSE);
1592 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1593 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1594 collection_table_auto_scroll_notify_cb, ct);
1599 *-------------------------------------------------------------------
1601 *-------------------------------------------------------------------
1604 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1606 CollectTable *ct = data;
1608 collection_table_motion_update(ct, (gint)bevent->x, (gint)bevent->y, FALSE);
1613 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1615 CollectTable *ct = data;
1621 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1623 ct->click_info = info;
1624 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1626 switch (bevent->button)
1628 case MOUSE_BUTTON_LEFT:
1629 if (bevent->type == GDK_2BUTTON_PRESS)
1633 layout_image_set_collection(NULL, ct->cd, info);
1636 else if (!gtk_widget_has_focus(ct->listview))
1638 gtk_widget_grab_focus(ct->listview);
1641 case MOUSE_BUTTON_RIGHT:
1642 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1643 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1652 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1654 CollectTable *ct = data;
1656 CollectInfo *info = NULL;
1660 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1662 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1667 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1670 if (bevent->button == MOUSE_BUTTON_LEFT &&
1671 info && ct->click_info == info)
1673 collection_table_set_focus(ct, info);
1675 if (bevent->state & GDK_CONTROL_MASK)
1677 gboolean select = !INFO_SELECTED(info);
1679 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1681 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1685 collection_table_select_util(ct, info, select);
1690 collection_table_unselect_all(ct);
1692 if ((bevent->state & GDK_SHIFT_MASK) &&
1695 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1699 collection_table_select_util(ct, info, TRUE);
1703 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1704 info && ct->click_info == info)
1706 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1712 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1714 CollectTable *ct = data;
1721 *-------------------------------------------------------------------
1722 * populate, add, insert, etc.
1723 *-------------------------------------------------------------------
1726 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1730 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1736 static void collection_table_clear_store(CollectTable *ct)
1738 GtkTreeModel *store;
1740 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1741 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1743 gtk_list_store_clear(GTK_LIST_STORE(store));
1746 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1748 GtkListStore *store;
1752 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1754 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1755 gtk_list_store_append(store, iter);
1756 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1761 static void collection_table_populate(CollectTable *ct, gboolean resize)
1766 collection_table_verify_selections(ct);
1768 collection_table_clear_store(ct);
1775 thumb_width = collection_table_get_icon_width(ct);
1777 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1779 GtkTreeViewColumn *column;
1780 GtkCellRenderer *cell;
1783 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1784 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1785 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1787 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1788 cell = (list) ? list->data : NULL;
1791 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1793 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1794 "fixed_height", options->thumbnails.max_height,
1795 "show_text", ct->show_text, NULL);
1798 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1802 work = ct->cd->list;
1810 list = collection_table_add_row(ct, &iter);
1811 while (work && list)
1813 list->data = work->data;
1821 collection_table_update_focus(ct);
1822 collection_table_update_status(ct);
1825 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1830 thumb_width = collection_table_get_icon_width(ct);
1832 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1833 if (new_cols < 1) new_cols = 1;
1835 if (!force && new_cols == ct->columns) return;
1837 ct->columns = new_cols;
1839 collection_table_populate(ct, TRUE);
1841 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1844 static void collection_table_sync(CollectTable *ct)
1846 GtkTreeModel *store;
1851 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1856 work = ct->cd->list;
1862 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1864 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1865 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1869 list = collection_table_add_row(ct, &iter);
1894 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1898 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1899 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1905 collection_table_update_focus(ct);
1906 collection_table_update_status(ct);
1909 static gboolean collection_table_sync_idle_cb(gpointer data)
1911 CollectTable *ct = data;
1913 if (!ct->sync_idle_id) return FALSE;
1914 g_source_remove(ct->sync_idle_id);
1915 ct->sync_idle_id = 0;
1917 collection_table_sync(ct);
1921 static void collection_table_sync_idle(CollectTable *ct)
1923 if (!ct->sync_idle_id)
1925 /* high priority, the view needs to be resynced before a redraw
1926 * may contain invalid pointers at this time
1928 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1932 void collection_table_add_filelist(CollectTable *ct, GList *list)
1941 collection_add(ct->cd, (FileData *)work->data, FALSE);
1946 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1955 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1959 collection_table_sync_idle(ct);
1962 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1965 GList *insert_pos = NULL;
1969 if (!info_list) return;
1971 info = collection_table_find_data(ct, row, col, NULL);
1973 if (!info_list->next && info_list->data == info) return;
1975 if (info) insert_pos = g_list_find(ct->cd->list, info);
1977 /* FIXME: this may get slow for large lists */
1979 while (insert_pos && work)
1981 if (insert_pos->data == work->data)
1983 insert_pos = insert_pos->next;
1995 ct->cd->list = g_list_remove(ct->cd->list, work->data);
1999 /* place them back in */
2000 temp = g_list_copy(info_list);
2004 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2008 ct->cd->list = g_list_concat(temp, ct->cd->list);
2012 ct->cd->list = g_list_concat(ct->cd->list, temp);
2015 ct->cd->changed = TRUE;
2017 collection_table_sync_idle(ct);
2022 *-------------------------------------------------------------------
2024 *-------------------------------------------------------------------
2027 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2035 collection_table_update_extras(ct, FALSE, 0.0);
2039 if (!collection_table_find_position(ct, info, &row, &col)) return;
2041 if (ct->columns != 0 && ct->rows != 0)
2043 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2050 collection_table_update_extras(ct, TRUE, value);
2052 if (collection_table_find_iter(ct, info, &iter, NULL))
2054 GtkTreeModel *store;
2057 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2058 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2059 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2063 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2065 collection_table_sync_idle(ct);
2068 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2070 collection_table_sync_idle(ct);
2073 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2075 if (ci && INFO_SELECTED(ci))
2077 ct->selection = g_list_remove(ct->selection, ci);
2080 collection_table_sync_idle(ct);
2083 void collection_table_refresh(CollectTable *ct)
2085 collection_table_populate(ct, FALSE);
2089 *-------------------------------------------------------------------
2091 *-------------------------------------------------------------------
2094 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2100 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2103 f = filelist_filter(f, FALSE);
2104 d = filelist_filter(d, TRUE);
2106 f = filelist_sort_path(f);
2107 d = filelist_sort_path(d);
2109 collection_table_insert_filelist(ct, f, ct->marker_info);
2111 work = g_list_last(d);
2114 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2122 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2127 FileData *fd = work->data;
2129 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2131 collection_table_insert_filelist(ct, list, ct->marker_info);
2135 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2137 CollectTable *ct = data;
2139 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2142 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2144 CollectTable *ct = data;
2146 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2149 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2151 CollectTable *ct = data;
2153 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2156 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2160 menu = popup_menu_short_lived();
2161 g_signal_connect(G_OBJECT(menu), "destroy",
2162 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2164 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2165 menu_item_add_divider(menu);
2166 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2167 G_CALLBACK(confirm_dir_list_add), ct);
2168 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2169 G_CALLBACK(confirm_dir_list_recurse), ct);
2170 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2171 G_CALLBACK(confirm_dir_list_skip), ct);
2172 menu_item_add_divider(menu);
2173 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2179 *-------------------------------------------------------------------
2181 *-------------------------------------------------------------------
2184 static GtkTargetEntry collection_drag_types[] = {
2185 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2186 { "text/uri-list", 0, TARGET_URI_LIST },
2187 { "text/plain", 0, TARGET_TEXT_PLAIN }
2189 static gint n_collection_drag_types = 3;
2191 static GtkTargetEntry collection_drop_types[] = {
2192 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2193 { "text/uri-list", 0, TARGET_URI_LIST }
2195 static gint n_collection_drop_types = 2;
2198 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2199 GtkSelectionData *selection_data, guint info,
2200 guint time, gpointer data)
2202 CollectTable *ct = data;
2205 gchar *uri_text = NULL;
2208 if (!ct->click_info) return;
2210 selected = INFO_SELECTED(ct->click_info);
2214 case TARGET_APP_COLLECTION_MEMBER:
2217 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2221 list = g_list_append(NULL, ct->click_info);
2222 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2225 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2226 8, (guchar *)uri_text, total);
2229 case TARGET_URI_LIST:
2230 case TARGET_TEXT_PLAIN:
2234 list = collection_table_selection_get_list(ct);
2238 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2242 uri_selection_data_set_uris_from_filelist(selection_data, list);
2243 filelist_free(list);
2249 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2251 GtkSelectionData *selection_data, guint info,
2252 guint time, gpointer data)
2254 CollectTable *ct = data;
2256 GList *info_list = NULL;
2257 CollectionData *source;
2258 CollectInfo *drop_info;
2261 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2263 collection_table_scroll(ct, FALSE);
2264 collection_table_insert_marker(ct, NULL, FALSE);
2266 drop_info = collection_table_insert_point(ct, x, y);
2270 case TARGET_APP_COLLECTION_MEMBER:
2271 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2274 if (source == ct->cd)
2279 /* it is a move within a collection */
2280 filelist_free(list);
2285 collection_table_move_by_info_list(ct, info_list, -1, -1);
2287 else if (collection_table_find_position(ct, drop_info, &row, &col))
2289 collection_table_move_by_info_list(ct, info_list, row, col);
2294 /* it is a move/copy across collections */
2295 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2297 collection_remove_by_info_list(source, info_list);
2300 g_list_free(info_list);
2303 case TARGET_URI_LIST:
2304 list = uri_filelist_from_gtk_selection_data(selection_data);
2308 FileData *fd = work->data;
2309 if (isdir(fd->path))
2313 ct->drop_list = list;
2314 ct->drop_info = drop_info;
2315 menu = collection_table_drop_menu(ct);
2316 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2329 collection_table_insert_filelist(ct, list, drop_info);
2330 filelist_free(list);
2334 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2336 CollectTable *ct = data;
2338 if (ct->click_info && ct->click_info->pixbuf)
2342 if (INFO_SELECTED(ct->click_info))
2343 items = g_list_length(ct->selection);
2346 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2350 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2352 CollectTable *ct = data;
2354 /* apparently a leave event is not generated on a drop */
2357 collection_table_scroll(ct, FALSE);
2360 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2361 gint x, gint y, guint time, gpointer data)
2363 CollectTable *ct = data;
2365 collection_table_motion_update(ct, x, y, TRUE);
2366 collection_table_scroll(ct, TRUE);
2371 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2373 CollectTable *ct = data;
2375 collection_table_scroll(ct, FALSE);
2378 static void collection_table_dnd_init(CollectTable *ct)
2380 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2381 collection_drag_types, n_collection_drag_types,
2382 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2383 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2384 G_CALLBACK(collection_table_dnd_get), ct);
2385 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2386 G_CALLBACK(collection_table_dnd_begin), ct);
2387 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2388 G_CALLBACK(collection_table_dnd_end), ct);
2390 gtk_drag_dest_set(ct->listview,
2391 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2392 collection_drop_types, n_collection_drop_types,
2393 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2394 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2395 G_CALLBACK(collection_table_dnd_motion), ct);
2396 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2397 G_CALLBACK(collection_table_dnd_leave), ct);
2398 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2399 G_CALLBACK(collection_table_dnd_receive), ct);
2403 *-----------------------------------------------------------------------------
2405 *-----------------------------------------------------------------------------
2408 typedef struct _ColumnData ColumnData;
2415 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2416 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2418 ColumnData *cd = data;
2428 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2429 info = g_list_nth_data(list, cd->number);
2431 style = gtk_widget_get_style(ct->listview);
2432 if (info && (info->flag_mask & SELECTION_SELECTED) )
2434 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2435 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2439 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2440 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2443 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2445 shift_color(&color_bg, -1, 0);
2448 if (GQV_IS_CELL_RENDERER_ICON(cell))
2452 g_object_set(cell, "pixbuf", info->pixbuf,
2453 "text", info->fd->name,
2454 "cell-background-gdk", &color_bg,
2455 "cell-background-set", TRUE,
2456 "foreground-gdk", &color_fg,
2457 "foreground-set", TRUE,
2458 "has-focus", (ct->focus_info == info), NULL);
2462 g_object_set(cell, "pixbuf", NULL,
2464 "cell-background-set", FALSE,
2465 "foreground-set", FALSE,
2466 "has-focus", FALSE, NULL);
2471 static void collection_table_append_column(CollectTable *ct, gint n)
2474 GtkTreeViewColumn *column;
2475 GtkCellRenderer *renderer;
2477 column = gtk_tree_view_column_new();
2478 gtk_tree_view_column_set_min_width(column, 0);
2480 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2481 gtk_tree_view_column_set_alignment(column, 0.5);
2483 renderer = gqv_cell_renderer_icon_new();
2484 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2485 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2486 "ypad", THUMB_BORDER_PADDING,
2487 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2489 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2491 cd = g_new0(ColumnData, 1);
2494 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2496 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2500 *-------------------------------------------------------------------
2502 *-------------------------------------------------------------------
2505 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2507 CollectTable *ct = data;
2511 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2513 gtk_widget_destroy(ct->popup);
2516 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2519 collection_table_scroll(ct, FALSE);
2524 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2526 CollectTable *ct = data;
2528 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2531 CollectTable *collection_table_new(CollectionData *cd)
2534 GtkListStore *store;
2535 GtkTreeSelection *selection;
2538 ct = g_new0(CollectTable, 1);
2541 ct->show_text = options->show_icon_names;
2543 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2544 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2545 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2546 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2548 store = gtk_list_store_new(1, G_TYPE_POINTER);
2549 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2550 g_object_unref(store);
2552 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2553 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2555 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2556 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2558 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2560 collection_table_append_column(ct, i);
2563 /* zero width column to hide tree view focus, we draw it ourselves */
2564 collection_table_append_column(ct, i);
2565 /* end column to fill white space */
2566 collection_table_append_column(ct, i);
2568 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2569 G_CALLBACK(collection_table_destroy), ct);
2570 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2571 G_CALLBACK(collection_table_sized), ct);
2572 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2573 G_CALLBACK(collection_table_press_key_cb), ct);
2575 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2576 gtk_widget_show(ct->listview);
2578 collection_table_dnd_init(ct);
2580 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2581 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2582 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2583 G_CALLBACK(collection_table_press_cb), ct);
2584 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2585 G_CALLBACK(collection_table_release_cb), ct);
2586 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2587 G_CALLBACK(collection_table_motion_cb), ct);
2588 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2589 G_CALLBACK(collection_table_leave_cb), ct);
2594 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2596 ct->status_label = status;
2597 ct->extra_label = extra;
2598 collection_table_update_status(ct);
2599 collection_table_update_extras(ct, FALSE, 0.0);
2602 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2604 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2607 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */