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 gint x_win = 0, y_win = 0;
532 if (ct->tip_window) return;
534 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
536 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
537 if (!ct->tip_info) return;
539 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
540 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
541 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
543 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
545 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
546 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
547 gtk_widget_show(label);
549 #if GTK_CHECK_VERSION(3,0,0)
550 gdk_window_get_origin(gtk_widget_get_window(ct->listview), &x_win, &y_win);
552 gdk_window_get_pointer(NULL, &x, &y, NULL);
555 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
556 gtk_window_move(GTK_WINDOW(ct->tip_window), x_win + x + 16, y_win + y + 16);
557 gtk_widget_show(ct->tip_window);
560 static void tip_hide(CollectTable *ct)
562 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
563 ct->tip_window = NULL;
566 static gboolean tip_schedule_cb(gpointer data)
568 CollectTable *ct = data;
570 if (!ct->tip_delay_id) return FALSE;
574 ct->tip_delay_id = 0;
578 static void tip_schedule(CollectTable *ct)
582 if (ct->tip_delay_id)
584 g_source_remove(ct->tip_delay_id);
585 ct->tip_delay_id = 0;
588 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
591 static void tip_unschedule(CollectTable *ct)
595 if (ct->tip_delay_id)
597 g_source_remove(ct->tip_delay_id);
598 ct->tip_delay_id = 0;
602 static void tip_update(CollectTable *ct, CollectInfo *info)
610 gdk_window_get_pointer(NULL, &x, &y, NULL);
611 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
613 if (info != ct->tip_info)
624 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
625 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
631 *-------------------------------------------------------------------
633 *-------------------------------------------------------------------
636 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
638 CollectTable *ct = data;
640 collection_dialog_save_as(NULL, ct->cd);
643 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
645 CollectTable *ct = data;
649 collection_table_popup_save_as_cb(widget, data);
653 if (!collection_save(ct->cd, ct->cd->path))
655 log_printf("failed saving to collection path: %s\n", ct->cd->path);
659 static GList *collection_table_popup_file_list(CollectTable *ct)
661 if (!ct->click_info) return NULL;
663 if (INFO_SELECTED(ct->click_info))
665 return collection_table_selection_get_list(ct);
668 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
671 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
674 const gchar *key = data;
676 ct = submenu_item_get_data(widget);
680 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
683 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
685 CollectTable *ct = data;
687 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
690 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
692 CollectTable *ct = data;
694 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
697 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
699 CollectTable *ct = data;
701 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
704 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
706 CollectTable *ct = data;
708 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
711 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
713 CollectTable *ct = data;
715 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct));
718 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
723 ct = submenu_item_get_data(widget);
727 type = (SortType)GPOINTER_TO_INT(data);
729 collection_set_sort_method(ct->cd, type);
732 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
736 ct = submenu_item_get_data(widget);
740 collection_randomize(ct->cd);
743 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
745 CollectTable *ct = data;
747 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
749 view_window_new_from_collection(ct->cd, ct->click_info);
753 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
755 CollectTable *ct = data;
757 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
759 layout_image_set_collection(NULL, ct->cd, ct->click_info);
763 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
765 CollectTable *ct = data;
767 collection_table_select_all(ct);
768 ct->prev_selection= ct->click_info;
771 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
773 CollectTable *ct = data;
775 collection_table_unselect_all(ct);
776 ct->prev_selection= ct->click_info;
779 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
781 CollectTable *ct = data;
783 collection_table_select_invert_all(ct);
784 ct->prev_selection= ct->click_info;
787 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
789 CollectTable *ct = data;
792 if (!ct->click_info) return;
794 if (INFO_SELECTED(ct->click_info))
796 list = g_list_copy(ct->selection);
800 list = g_list_append(NULL, ct->click_info);
803 collection_remove_by_info_list(ct->cd, list);
807 static void collection_table_popup_add_filelist_cb(GtkWidget *widget, gpointer data)
809 CollectTable *ct = data;
812 list = layout_list(NULL);
816 collection_table_add_filelist(ct, list);
821 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
823 CollectTable *ct = data;
825 collection_dialog_append(NULL, ct->cd);
828 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
830 CollectTable *ct = data;
833 dw = dupe_window_new(DUPE_MATCH_NAME);
834 dupe_window_add_collection(dw, ct->cd);
837 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
839 CollectTable *ct = data;
842 fd = (ct->click_info) ? ct->click_info->fd : NULL;
844 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
847 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
849 CollectTable *ct = data;
851 collection_table_toggle_filenames(ct);
854 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
856 CollectTable *ct = data;
858 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
859 ct->click_info = NULL;
862 filelist_free(ct->drop_list);
863 ct->drop_list = NULL;
864 ct->drop_info = NULL;
866 filelist_free(ct->editmenu_fd_list);
867 ct->editmenu_fd_list = NULL;
870 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
876 menu = popup_menu_short_lived();
878 g_signal_connect(G_OBJECT(menu), "destroy",
879 G_CALLBACK(collection_table_popup_destroy_cb), ct);
881 menu_item_add_sensitive(menu, _("_View"), over_icon,
882 G_CALLBACK(collection_table_popup_view_cb), ct);
883 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
884 G_CALLBACK(collection_table_popup_view_new_cb), ct);
885 menu_item_add_divider(menu);
886 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
887 G_CALLBACK(collection_table_popup_remove_cb), ct);
889 menu_item_add_stock(menu, _("Append from file list"), GTK_STOCK_ADD,
890 G_CALLBACK(collection_table_popup_add_filelist_cb), ct);
891 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
892 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
893 menu_item_add_divider(menu);
895 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
896 submenu = gtk_menu_new();
897 menu_item_add(submenu, _("Select all"),
898 G_CALLBACK(collection_table_popup_selectall_cb), ct);
899 menu_item_add(submenu, _("Select none"),
900 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
901 menu_item_add(submenu, _("Invert selection"),
902 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
903 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
904 menu_item_add_divider(menu);
907 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
908 submenu_add_edit(menu, &item,
909 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
910 gtk_widget_set_sensitive(item, over_icon);
912 menu_item_add_divider(menu);
913 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
914 G_CALLBACK(collection_table_popup_copy_cb), ct);
915 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
916 G_CALLBACK(collection_table_popup_move_cb), ct);
917 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
918 G_CALLBACK(collection_table_popup_rename_cb), ct);
919 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
920 G_CALLBACK(collection_table_popup_delete_cb), ct);
921 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
922 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
923 menu_item_add_divider(menu);
925 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
926 menu_item_add_divider(submenu);
927 menu_item_add(submenu, _("Randomize"),
928 G_CALLBACK(collection_table_popup_randomize_cb), ct);
929 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
930 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
932 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
933 G_CALLBACK(collection_table_popup_show_names_cb), ct);
934 menu_item_add_divider(menu);
935 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
936 G_CALLBACK(collection_table_popup_save_cb), ct);
937 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
938 G_CALLBACK(collection_table_popup_save_as_cb), ct);
939 menu_item_add_divider(menu);
940 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
941 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
942 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
943 G_CALLBACK(collection_table_popup_print_cb), ct);
948 *-------------------------------------------------------------------
950 *-------------------------------------------------------------------
953 static void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
958 if (g_list_find(ct->cd->list, ct->focus_info))
960 if (info == ct->focus_info)
962 /* ensure focus row col are correct */
963 collection_table_find_position(ct, ct->focus_info,
964 &ct->focus_row, &ct->focus_column);
967 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
970 if (!collection_table_find_position(ct, info, &row, &col))
972 ct->focus_info = NULL;
974 ct->focus_column = -1;
978 ct->focus_info = info;
980 ct->focus_column = col;
981 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
983 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
986 GtkTreeViewColumn *column;
989 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
991 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
992 tpath = gtk_tree_model_get_path(store, &iter);
993 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
994 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
995 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
996 gtk_tree_path_free(tpath);
1000 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1007 new_row = ct->focus_row;
1008 new_col = ct->focus_column;
1011 if (new_row < 0) new_row = 0;
1012 if (new_row >= ct->rows) new_row = ct->rows - 1;
1032 new_col = ct->columns - 1;
1039 if (new_col >= ct->columns)
1041 if (new_row < ct->rows - 1)
1048 new_col = ct->columns - 1;
1058 if (new_row >= ct->rows)
1061 new_row = ct->rows - 1;
1064 new_col = ct->columns - 1;
1066 if (new_col >= ct->columns) new_col = ct->columns - 1;
1069 if (new_row == ct->rows - 1)
1073 /* if we moved beyond the last image, go to the last image */
1075 l = g_list_length(ct->cd->list);
1076 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1077 if (new_col >= l) new_col = l - 1;
1080 if (new_row == -1 || new_col == -1)
1082 if (!ct->cd->list) return;
1083 new_row = new_col = 0;
1086 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1089 static void collection_table_update_focus(CollectTable *ct)
1094 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1096 /* first find the old focus, if it exists and is valid */
1100 /* (try to) stay where we were */
1101 new_row = ct->focus_row;
1102 new_col = ct->focus_column;
1105 collection_table_move_focus(ct, new_row, new_col, FALSE);
1108 /* used to figure the page up/down distances */
1109 static gint page_height(CollectTable *ct)
1116 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1117 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1119 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1120 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1122 ret = page_size / row_height;
1123 if (ret < 1) ret = 1;
1128 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1130 CollectTable *ct = data;
1131 GtkTreeModel *store;
1137 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1138 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1139 tpath = gtk_tree_model_get_path(store, &iter);
1140 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1141 gtk_tree_path_free(tpath);
1143 popup_menu_position_clamp(menu, x, y, 0);
1146 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1148 CollectTable *ct = data;
1152 gboolean stop_signal = TRUE;
1154 switch (event->keyval)
1156 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1159 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1162 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1165 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1168 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1169 focus_row = -page_height(ct);
1171 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1172 focus_row = page_height(ct);
1174 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1175 focus_row = -ct->focus_row;
1176 focus_col = -ct->focus_column;
1178 case GDK_KEY_End: case GDK_KEY_KP_End:
1179 focus_row = ct->rows - 1 - ct->focus_row;
1180 focus_col = ct->columns - 1 - ct->focus_column;
1183 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1186 ct->click_info = info;
1187 if (event->state & GDK_CONTROL_MASK)
1189 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1193 collection_table_unselect_all(ct);
1194 collection_table_select(ct, info);
1199 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1203 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1204 ct->click_info = info;
1206 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1209 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1210 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1213 stop_signal = FALSE;
1217 if (focus_row != 0 || focus_col != 0)
1219 CollectInfo *new_info;
1220 CollectInfo *old_info;
1222 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1223 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1224 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1226 if (new_info != old_info)
1228 if (event->state & GDK_SHIFT_MASK)
1230 if (!options->collections.rectangular_selection)
1232 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1236 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1238 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1240 else if (event->state & GDK_CONTROL_MASK)
1242 ct->click_info = new_info;
1246 ct->click_info = new_info;
1247 collection_table_unselect_all(ct);
1248 collection_table_select(ct, new_info);
1262 *-------------------------------------------------------------------
1264 *-------------------------------------------------------------------
1267 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1268 gboolean use_coord, gint x, gint y)
1270 CollectInfo *info = NULL;
1271 GtkTreeModel *store;
1274 GtkTreeViewColumn *column;
1276 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1278 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1283 if (collection_table_find_iter(ct, source, &iter, &col))
1285 tpath = gtk_tree_model_get_path(store, &iter);
1286 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1287 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1288 gtk_tree_path_free(tpath);
1291 *after = !!(x > cell->x + (cell->width / 2));
1296 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1297 &tpath, &column, NULL, NULL))
1302 gtk_tree_model_get_iter(store, &iter, tpath);
1303 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1305 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1306 info = g_list_nth_data(list, n);
1310 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1311 *after = !!(x > cell->x + (cell->width / 2));
1314 gtk_tree_path_free(tpath);
1321 work = g_list_last(ct->cd->list);
1329 if (collection_table_find_iter(ct, info, &iter, &col))
1331 tpath = gtk_tree_model_get_path(store, &iter);
1332 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1333 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1334 gtk_tree_path_free(tpath);
1342 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1346 gboolean after = FALSE;
1348 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1354 work = g_list_find(ct->cd->list, info);
1355 if (work && work->next)
1357 info = work->next->data;
1368 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1370 gboolean after = FALSE;
1375 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1376 ct->marker_window = NULL;
1381 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1383 /* this setting does not take into account (after), but since it is not really used... */
1384 ct->marker_info = info;
1386 if (!ct->marker_window)
1388 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1389 GdkWindowAttr attributes;
1390 gint attributes_mask;
1391 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1392 gint w = gdk_pixbuf_get_width(pb);
1393 gint h = gdk_pixbuf_get_height(pb);
1395 attributes.window_type = GDK_WINDOW_CHILD;
1396 attributes.wclass = GDK_INPUT_OUTPUT;
1397 attributes.width = w;
1398 attributes.height = h;
1399 attributes.event_mask = gtk_widget_get_events(ct->listview);
1400 attributes_mask = 0;
1402 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1404 #if GTK_CHECK_VERSION(3,0,0)
1405 cairo_region_t *mask;
1406 cairo_pattern_t *pattern;
1407 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1408 cairo_t *cr = cairo_create(img);
1409 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1411 pattern = cairo_pattern_create_for_surface(img);
1412 mask = gdk_cairo_region_create_from_surface(img);
1413 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1414 gdk_window_set_background_pattern(ct->marker_window, pattern);
1415 cairo_region_destroy(mask);
1416 cairo_pattern_destroy(pattern);
1418 cairo_surface_destroy(img);
1422 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1424 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1425 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1427 g_object_unref(pixmap);
1428 if (mask) g_object_unref(mask);
1438 w = gdk_window_get_width(ct->marker_window);
1439 h = gdk_window_get_height(ct->marker_window);
1447 x = cell.x + cell.width;
1450 y = cell.y + (cell.height / 2) - (h / 2);
1452 gdk_window_move(ct->marker_window, x, y);
1453 #if !GTK_CHECK_VERSION(3,0,0)
1454 gdk_window_clear(ct->marker_window);
1456 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1460 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1465 *-------------------------------------------------------------------
1466 * mouse drag auto-scroll
1467 *-------------------------------------------------------------------
1470 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1474 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1479 collection_table_insert_marker(ct, info, TRUE);
1483 tip_update(ct, info);
1487 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1489 CollectTable *ct = data;
1494 if (!ct->drop_idle_id) return FALSE;
1496 window = gtk_widget_get_window(ct->listview);
1497 gdk_window_get_pointer(window, &x, &y, NULL);
1498 w = gdk_window_get_width(window);
1499 h = gdk_window_get_height(window);
1500 if (x >= 0 && x < w && y >= 0 && y < h)
1502 collection_table_motion_update(ct, x, y, TRUE);
1505 ct->drop_idle_id = 0;
1509 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1511 CollectTable *ct = data;
1513 if (!ct->drop_idle_id)
1515 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1521 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1525 if (ct->drop_idle_id)
1527 g_source_remove(ct->drop_idle_id);
1528 ct->drop_idle_id = 0;
1530 widget_auto_scroll_stop(ct->listview);
1531 collection_table_insert_marker(ct, NULL, FALSE);
1535 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1536 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1537 collection_table_auto_scroll_notify_cb, ct);
1542 *-------------------------------------------------------------------
1544 *-------------------------------------------------------------------
1547 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1549 CollectTable *ct = data;
1551 collection_table_motion_update(ct, (gint)bevent->x, (gint)bevent->y, FALSE);
1556 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1558 CollectTable *ct = data;
1564 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1566 ct->click_info = info;
1567 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1569 switch (bevent->button)
1571 case MOUSE_BUTTON_LEFT:
1572 if (bevent->type == GDK_2BUTTON_PRESS)
1576 layout_image_set_collection(NULL, ct->cd, info);
1579 else if (!gtk_widget_has_focus(ct->listview))
1581 gtk_widget_grab_focus(ct->listview);
1584 case MOUSE_BUTTON_RIGHT:
1585 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1586 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1595 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1597 CollectTable *ct = data;
1599 CollectInfo *info = NULL;
1603 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1605 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1610 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1613 if (bevent->button == MOUSE_BUTTON_LEFT &&
1614 info && ct->click_info == info)
1616 collection_table_set_focus(ct, info);
1618 if (bevent->state & GDK_CONTROL_MASK)
1620 gboolean select = !INFO_SELECTED(info);
1622 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1624 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1628 collection_table_select_util(ct, info, select);
1633 collection_table_unselect_all(ct);
1635 if ((bevent->state & GDK_SHIFT_MASK) &&
1638 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1642 collection_table_select_util(ct, info, TRUE);
1646 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1647 info && ct->click_info == info)
1649 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1655 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1657 CollectTable *ct = data;
1664 *-------------------------------------------------------------------
1665 * populate, add, insert, etc.
1666 *-------------------------------------------------------------------
1669 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1673 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1679 static void collection_table_clear_store(CollectTable *ct)
1681 GtkTreeModel *store;
1683 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1684 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1686 gtk_list_store_clear(GTK_LIST_STORE(store));
1689 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1691 GtkListStore *store;
1695 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1697 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1698 gtk_list_store_append(store, iter);
1699 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1704 static void collection_table_populate(CollectTable *ct, gboolean resize)
1709 collection_table_verify_selections(ct);
1711 collection_table_clear_store(ct);
1718 thumb_width = collection_table_get_icon_width(ct);
1720 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1722 GtkTreeViewColumn *column;
1723 GtkCellRenderer *cell;
1726 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1727 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1728 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1730 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1731 cell = (list) ? list->data : NULL;
1734 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1736 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1737 "fixed_height", options->thumbnails.max_height,
1738 "show_text", ct->show_text, NULL);
1741 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1745 work = ct->cd->list;
1753 list = collection_table_add_row(ct, &iter);
1754 while (work && list)
1756 list->data = work->data;
1764 collection_table_update_focus(ct);
1765 collection_table_update_status(ct);
1768 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1773 thumb_width = collection_table_get_icon_width(ct);
1775 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1776 if (new_cols < 1) new_cols = 1;
1778 if (!force && new_cols == ct->columns) return;
1780 ct->columns = new_cols;
1782 collection_table_populate(ct, TRUE);
1784 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1787 static void collection_table_sync(CollectTable *ct)
1789 GtkTreeModel *store;
1794 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1799 work = ct->cd->list;
1805 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1807 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1808 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1812 list = collection_table_add_row(ct, &iter);
1837 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1841 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1842 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1848 collection_table_update_focus(ct);
1849 collection_table_update_status(ct);
1852 static gboolean collection_table_sync_idle_cb(gpointer data)
1854 CollectTable *ct = data;
1856 if (!ct->sync_idle_id) return FALSE;
1857 g_source_remove(ct->sync_idle_id);
1858 ct->sync_idle_id = 0;
1860 collection_table_sync(ct);
1864 static void collection_table_sync_idle(CollectTable *ct)
1866 if (!ct->sync_idle_id)
1868 /* high priority, the view needs to be resynced before a redraw
1869 * may contain invalid pointers at this time
1871 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1875 void collection_table_add_filelist(CollectTable *ct, GList *list)
1884 collection_add(ct->cd, (FileData *)work->data, FALSE);
1889 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1898 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1902 collection_table_sync_idle(ct);
1905 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1908 GList *insert_pos = NULL;
1912 if (!info_list) return;
1914 info = collection_table_find_data(ct, row, col, NULL);
1916 if (!info_list->next && info_list->data == info) return;
1918 if (info) insert_pos = g_list_find(ct->cd->list, info);
1920 /* FIXME: this may get slow for large lists */
1922 while (insert_pos && work)
1924 if (insert_pos->data == work->data)
1926 insert_pos = insert_pos->next;
1938 ct->cd->list = g_list_remove(ct->cd->list, work->data);
1942 /* place them back in */
1943 temp = g_list_copy(info_list);
1947 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
1951 ct->cd->list = g_list_concat(temp, ct->cd->list);
1955 ct->cd->list = g_list_concat(ct->cd->list, temp);
1958 ct->cd->changed = TRUE;
1960 collection_table_sync_idle(ct);
1965 *-------------------------------------------------------------------
1967 *-------------------------------------------------------------------
1970 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
1978 collection_table_update_extras(ct, FALSE, 0.0);
1982 if (!collection_table_find_position(ct, info, &row, &col)) return;
1984 if (ct->columns != 0 && ct->rows != 0)
1986 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
1993 collection_table_update_extras(ct, TRUE, value);
1995 if (collection_table_find_iter(ct, info, &iter, NULL))
1997 GtkTreeModel *store;
2000 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2001 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2002 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2006 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2008 collection_table_sync_idle(ct);
2011 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2013 collection_table_sync_idle(ct);
2016 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2018 if (ci && INFO_SELECTED(ci))
2020 ct->selection = g_list_remove(ct->selection, ci);
2023 collection_table_sync_idle(ct);
2026 void collection_table_refresh(CollectTable *ct)
2028 collection_table_populate(ct, FALSE);
2032 *-------------------------------------------------------------------
2034 *-------------------------------------------------------------------
2037 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2043 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2046 f = filelist_filter(f, FALSE);
2047 d = filelist_filter(d, TRUE);
2049 f = filelist_sort_path(f);
2050 d = filelist_sort_path(d);
2052 collection_table_insert_filelist(ct, f, ct->marker_info);
2054 work = g_list_last(d);
2057 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2065 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2070 FileData *fd = work->data;
2072 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2074 collection_table_insert_filelist(ct, list, ct->marker_info);
2078 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2080 CollectTable *ct = data;
2082 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2085 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2087 CollectTable *ct = data;
2089 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2092 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2094 CollectTable *ct = data;
2096 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2099 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2103 menu = popup_menu_short_lived();
2104 g_signal_connect(G_OBJECT(menu), "destroy",
2105 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2107 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2108 menu_item_add_divider(menu);
2109 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2110 G_CALLBACK(confirm_dir_list_add), ct);
2111 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2112 G_CALLBACK(confirm_dir_list_recurse), ct);
2113 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2114 G_CALLBACK(confirm_dir_list_skip), ct);
2115 menu_item_add_divider(menu);
2116 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2122 *-------------------------------------------------------------------
2124 *-------------------------------------------------------------------
2127 static GtkTargetEntry collection_drag_types[] = {
2128 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2129 { "text/uri-list", 0, TARGET_URI_LIST },
2130 { "text/plain", 0, TARGET_TEXT_PLAIN }
2132 static gint n_collection_drag_types = 3;
2134 static GtkTargetEntry collection_drop_types[] = {
2135 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2136 { "text/uri-list", 0, TARGET_URI_LIST }
2138 static gint n_collection_drop_types = 2;
2141 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2142 GtkSelectionData *selection_data, guint info,
2143 guint time, gpointer data)
2145 CollectTable *ct = data;
2148 gchar *uri_text = NULL;
2151 if (!ct->click_info) return;
2153 selected = INFO_SELECTED(ct->click_info);
2157 case TARGET_APP_COLLECTION_MEMBER:
2160 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2164 list = g_list_append(NULL, ct->click_info);
2165 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2168 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2169 8, (guchar *)uri_text, total);
2172 case TARGET_URI_LIST:
2173 case TARGET_TEXT_PLAIN:
2177 list = collection_table_selection_get_list(ct);
2181 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2185 uri_selection_data_set_uris_from_filelist(selection_data, list);
2186 filelist_free(list);
2192 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2194 GtkSelectionData *selection_data, guint info,
2195 guint time, gpointer data)
2197 CollectTable *ct = data;
2199 GList *info_list = NULL;
2200 CollectionData *source;
2201 CollectInfo *drop_info;
2204 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2206 collection_table_scroll(ct, FALSE);
2207 collection_table_insert_marker(ct, NULL, FALSE);
2209 drop_info = collection_table_insert_point(ct, x, y);
2213 case TARGET_APP_COLLECTION_MEMBER:
2214 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2217 if (source == ct->cd)
2222 /* it is a move within a collection */
2223 filelist_free(list);
2228 collection_table_move_by_info_list(ct, info_list, -1, -1);
2230 else if (collection_table_find_position(ct, drop_info, &row, &col))
2232 collection_table_move_by_info_list(ct, info_list, row, col);
2237 /* it is a move/copy across collections */
2238 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2240 collection_remove_by_info_list(source, info_list);
2243 g_list_free(info_list);
2246 case TARGET_URI_LIST:
2247 list = uri_filelist_from_gtk_selection_data(selection_data);
2251 FileData *fd = work->data;
2252 if (isdir(fd->path))
2256 ct->drop_list = list;
2257 ct->drop_info = drop_info;
2258 menu = collection_table_drop_menu(ct);
2259 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2272 collection_table_insert_filelist(ct, list, drop_info);
2273 filelist_free(list);
2277 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2279 CollectTable *ct = data;
2281 if (ct->click_info && ct->click_info->pixbuf)
2285 if (INFO_SELECTED(ct->click_info))
2286 items = g_list_length(ct->selection);
2289 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2293 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2295 CollectTable *ct = data;
2297 /* apparently a leave event is not generated on a drop */
2300 collection_table_scroll(ct, FALSE);
2303 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2304 gint x, gint y, guint time, gpointer data)
2306 CollectTable *ct = data;
2308 collection_table_motion_update(ct, x, y, TRUE);
2309 collection_table_scroll(ct, TRUE);
2314 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2316 CollectTable *ct = data;
2318 collection_table_scroll(ct, FALSE);
2321 static void collection_table_dnd_init(CollectTable *ct)
2323 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2324 collection_drag_types, n_collection_drag_types,
2325 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2326 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2327 G_CALLBACK(collection_table_dnd_get), ct);
2328 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2329 G_CALLBACK(collection_table_dnd_begin), ct);
2330 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2331 G_CALLBACK(collection_table_dnd_end), ct);
2333 gtk_drag_dest_set(ct->listview,
2334 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2335 collection_drop_types, n_collection_drop_types,
2336 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2337 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2338 G_CALLBACK(collection_table_dnd_motion), ct);
2339 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2340 G_CALLBACK(collection_table_dnd_leave), ct);
2341 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2342 G_CALLBACK(collection_table_dnd_receive), ct);
2346 *-----------------------------------------------------------------------------
2348 *-----------------------------------------------------------------------------
2351 typedef struct _ColumnData ColumnData;
2358 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2359 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2361 ColumnData *cd = data;
2371 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2372 info = g_list_nth_data(list, cd->number);
2374 style = gtk_widget_get_style(ct->listview);
2375 if (info && (info->flag_mask & SELECTION_SELECTED) )
2377 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2378 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2382 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2383 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2386 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2388 shift_color(&color_bg, -1, 0);
2391 if (GQV_IS_CELL_RENDERER_ICON(cell))
2395 g_object_set(cell, "pixbuf", info->pixbuf,
2396 "text", info->fd->name,
2397 "cell-background-gdk", &color_bg,
2398 "cell-background-set", TRUE,
2399 "foreground-gdk", &color_fg,
2400 "foreground-set", TRUE,
2401 "has-focus", (ct->focus_info == info), NULL);
2405 g_object_set(cell, "pixbuf", NULL,
2407 "cell-background-set", FALSE,
2408 "foreground-set", FALSE,
2409 "has-focus", FALSE, NULL);
2414 static void collection_table_append_column(CollectTable *ct, gint n)
2417 GtkTreeViewColumn *column;
2418 GtkCellRenderer *renderer;
2420 column = gtk_tree_view_column_new();
2421 gtk_tree_view_column_set_min_width(column, 0);
2423 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2424 gtk_tree_view_column_set_alignment(column, 0.5);
2426 renderer = gqv_cell_renderer_icon_new();
2427 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2428 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2429 "ypad", THUMB_BORDER_PADDING,
2430 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2432 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2434 cd = g_new0(ColumnData, 1);
2437 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2439 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2443 *-------------------------------------------------------------------
2445 *-------------------------------------------------------------------
2448 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2450 CollectTable *ct = data;
2454 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2456 gtk_widget_destroy(ct->popup);
2459 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2462 collection_table_scroll(ct, FALSE);
2467 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2469 CollectTable *ct = data;
2471 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2474 CollectTable *collection_table_new(CollectionData *cd)
2477 GtkListStore *store;
2478 GtkTreeSelection *selection;
2481 ct = g_new0(CollectTable, 1);
2484 ct->show_text = options->show_icon_names;
2486 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2487 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2488 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2489 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2491 store = gtk_list_store_new(1, G_TYPE_POINTER);
2492 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2493 g_object_unref(store);
2495 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2496 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2498 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2499 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2501 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2503 collection_table_append_column(ct, i);
2506 /* zero width column to hide tree view focus, we draw it ourselves */
2507 collection_table_append_column(ct, i);
2508 /* end column to fill white space */
2509 collection_table_append_column(ct, i);
2511 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2512 G_CALLBACK(collection_table_destroy), ct);
2513 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2514 G_CALLBACK(collection_table_sized), ct);
2515 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2516 G_CALLBACK(collection_table_press_key_cb), ct);
2518 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2519 gtk_widget_show(ct->listview);
2521 collection_table_dnd_init(ct);
2523 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2524 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2525 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2526 G_CALLBACK(collection_table_press_cb), ct);
2527 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2528 G_CALLBACK(collection_table_release_cb), ct);
2529 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2530 G_CALLBACK(collection_table_motion_cb), ct);
2531 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2532 G_CALLBACK(collection_table_leave_cb), ct);
2537 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2539 ct->status_label = status;
2540 ct->extra_label = extra;
2541 collection_table_update_status(ct);
2542 collection_table_update_extras(ct, FALSE, 0.0);
2545 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2547 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2550 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */