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,
65 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
68 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
72 *-------------------------------------------------------------------
74 *-------------------------------------------------------------------
77 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
81 n = g_list_index(ct->cd->list, info);
83 if (n < 0) return FALSE;
85 *row = n / ct->columns;
86 *col = n - (*row * ct->columns);
91 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
96 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
97 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
98 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
99 if (column) *column = col;
104 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
109 if (row < 0 || col < 0) return NULL;
111 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
112 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
116 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
117 if (!list) return NULL;
121 return g_list_nth_data(list, col);
127 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
130 GtkTreeViewColumn *column;
136 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
137 &tpath, &column, NULL, NULL))
140 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
141 gtk_tree_model_get_iter(store, &row, tpath);
142 gtk_tree_path_free(tpath);
144 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
145 if (!list) return NULL;
147 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
148 if (iter) *iter = row;
149 return g_list_nth_data(list, n);
152 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
162 CollectInfo *ci = work->data;
170 return g_list_length(ct->cd->list);
173 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
180 work = ct->selection;
183 CollectInfo *ci = work->data;
191 return g_list_length(ct->selection);
194 static void collection_table_update_status(CollectTable *ct)
202 if (!ct->status_label) return;
204 n = collection_table_list_count(ct, &n_bytes);
205 s = collection_table_selection_count(ct, &s_bytes);
209 gchar *b = text_from_size_abrev(n_bytes);
210 gchar *sb = text_from_size_abrev(s_bytes);
211 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
217 gchar *b = text_from_size_abrev(n_bytes);
218 buf = g_strdup_printf(_("%s, %d images"), b, n);
223 buf = g_strdup(_("Empty"));
226 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
230 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
234 if (!ct->extra_label) return;
237 text = _("Loading thumbs...");
241 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
242 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
245 static void collection_table_toggle_filenames(CollectTable *ct)
247 GtkAllocation allocation;
248 ct->show_text = !ct->show_text;
249 options->show_icon_names = ct->show_text;
251 gtk_widget_get_allocation(ct->listview, &allocation);
252 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
255 static gint collection_table_get_icon_width(CollectTable *ct)
259 if (!ct->show_text) return options->thumbnails.max_width;
261 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
262 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
263 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
269 *-------------------------------------------------------------------
271 *-------------------------------------------------------------------
274 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
281 if (info->flag_mask == value) return;
282 info->flag_mask = value;
284 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
287 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
288 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
294 if (collection_table_find_iter(ct, info, &row, NULL))
296 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
297 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
302 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
306 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
309 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
313 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
316 *-------------------------------------------------------------------
318 *-------------------------------------------------------------------
321 static void collection_table_verify_selections(CollectTable *ct)
325 work = ct->selection;
328 CollectInfo *info = work->data;
330 if (!g_list_find(ct->cd->list, info))
332 ct->selection = g_list_remove(ct->selection, info);
337 void collection_table_select_all(CollectTable *ct)
341 g_list_free(ct->selection);
342 ct->selection = NULL;
347 ct->selection = g_list_append(ct->selection, work->data);
348 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
352 collection_table_update_status(ct);
355 void collection_table_unselect_all(CollectTable *ct)
359 work = ct->selection;
362 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
366 g_list_free(ct->selection);
367 ct->selection = NULL;
369 collection_table_update_status(ct);
372 /* Invert the current collection's selection */
373 static void collection_table_select_invert_all(CollectTable *ct)
376 GList *new_selection = NULL;
381 CollectInfo *info = work->data;
383 if (INFO_SELECTED(info))
385 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
389 new_selection = g_list_append(new_selection, info);
390 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
397 g_list_free(ct->selection);
398 ct->selection = new_selection;
400 collection_table_update_status(ct);
403 static void collection_table_select(CollectTable *ct, CollectInfo *info)
405 ct->prev_selection = info;
407 if (!info || INFO_SELECTED(info)) return;
409 ct->selection = g_list_append(ct->selection, info);
410 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
412 collection_table_update_status(ct);
415 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
417 ct->prev_selection = info;
419 if (!info || !INFO_SELECTED(info) ) return;
421 ct->selection = g_list_remove(ct->selection, info);
422 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
424 collection_table_update_status(ct);
427 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
431 collection_table_select(ct, info);
435 collection_table_unselect(ct, info);
439 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
446 if (!collection_table_find_position(ct, start, &row1, &col1) ||
447 !collection_table_find_position(ct, end, &row2, &col2) ) return;
449 ct->prev_selection = end;
451 if (!options->collections.rectangular_selection)
456 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
463 work = g_list_find(ct->cd->list, start);
467 collection_table_select_util(ct, info, select);
469 if (work->data != end)
490 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
492 for (i = row1; i <= row2; i++)
494 for (j = col1; j <= col2; j++)
496 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
497 if (info) collection_table_select_util(ct, info, select);
502 GList *collection_table_selection_get_list(CollectTable *ct)
504 return collection_list_to_filelist(ct->selection);
507 static GList *collection_table_get_list(CollectTable *ct)
509 return collection_list_to_filelist(ct->cd->list);
513 *-------------------------------------------------------------------
514 * tooltip type window
515 *-------------------------------------------------------------------
518 static void tip_show(CollectTable *ct)
522 #if GTK_CHECK_VERSION(3,0,0)
524 GdkDeviceManager *device_manager;
528 if (ct->tip_window) return;
530 #if GTK_CHECK_VERSION(3,0,0)
531 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
532 gtk_widget_get_window(ct->listview)));
533 device = gdk_device_manager_get_client_pointer(device_manager);
534 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
535 device, &x, &y, NULL);
537 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
540 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
541 if (!ct->tip_info) return;
543 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
544 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
545 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
547 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
549 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
550 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
551 gtk_widget_show(label);
553 #if GTK_CHECK_VERSION(3,0,0)
554 display = gdk_display_get_default();
555 device_manager = gdk_display_get_device_manager(display);
556 device = gdk_device_manager_get_client_pointer(device_manager);
557 gdk_device_get_position(device, NULL, &x, &y);
559 gdk_window_get_pointer(NULL, &x, &y, NULL);
562 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
563 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
564 gtk_widget_show(ct->tip_window);
567 static void tip_hide(CollectTable *ct)
569 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
570 ct->tip_window = NULL;
573 static gboolean tip_schedule_cb(gpointer data)
575 CollectTable *ct = data;
577 if (!ct->tip_delay_id) return FALSE;
581 ct->tip_delay_id = 0;
585 static void tip_schedule(CollectTable *ct)
589 if (ct->tip_delay_id)
591 g_source_remove(ct->tip_delay_id);
592 ct->tip_delay_id = 0;
595 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
598 static void tip_unschedule(CollectTable *ct)
602 if (ct->tip_delay_id)
604 g_source_remove(ct->tip_delay_id);
605 ct->tip_delay_id = 0;
609 static void tip_update(CollectTable *ct, CollectInfo *info)
611 #if GTK_CHECK_VERSION(3,0,0)
612 GdkDisplay *display = gdk_display_get_default();
613 GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
614 GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
622 #if GTK_CHECK_VERSION(3,0,0)
623 gdk_device_get_position(device, NULL, &x, &y);
625 gdk_window_get_pointer(NULL, &x, &y, NULL);
627 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
629 if (info != ct->tip_info)
640 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
641 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
647 *-------------------------------------------------------------------
649 *-------------------------------------------------------------------
652 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
654 CollectTable *ct = data;
656 collection_dialog_save_as(NULL, ct->cd);
659 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
661 CollectTable *ct = data;
665 collection_table_popup_save_as_cb(widget, data);
669 if (!collection_save(ct->cd, ct->cd->path))
671 log_printf("failed saving to collection path: %s\n", ct->cd->path);
675 static GList *collection_table_popup_file_list(CollectTable *ct)
677 if (!ct->click_info) return NULL;
679 if (INFO_SELECTED(ct->click_info))
681 return collection_table_selection_get_list(ct);
684 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
687 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
690 const gchar *key = data;
692 ct = submenu_item_get_data(widget);
696 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
699 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
701 CollectTable *ct = data;
703 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
706 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
708 CollectTable *ct = data;
710 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
713 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
715 CollectTable *ct = data;
717 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
720 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
722 CollectTable *ct = data;
724 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
727 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
729 CollectTable *ct = data;
731 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct));
734 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
739 ct = submenu_item_get_data(widget);
743 type = (SortType)GPOINTER_TO_INT(data);
745 collection_set_sort_method(ct->cd, type);
748 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
752 ct = submenu_item_get_data(widget);
756 collection_randomize(ct->cd);
759 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
761 CollectTable *ct = data;
763 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
765 view_window_new_from_collection(ct->cd, ct->click_info);
769 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
771 CollectTable *ct = data;
773 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
775 layout_image_set_collection(NULL, ct->cd, ct->click_info);
779 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
781 CollectTable *ct = data;
783 collection_table_select_all(ct);
784 ct->prev_selection= ct->click_info;
787 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
789 CollectTable *ct = data;
791 collection_table_unselect_all(ct);
792 ct->prev_selection= ct->click_info;
795 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
797 CollectTable *ct = data;
799 collection_table_select_invert_all(ct);
800 ct->prev_selection= ct->click_info;
803 static void collection_table_popup_rectangular_selection_cb(GtkWidget *widget, gpointer data)
805 CollectTable *ct = data;
807 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
810 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
812 CollectTable *ct = data;
815 if (!ct->click_info) return;
817 if (INFO_SELECTED(ct->click_info))
819 list = g_list_copy(ct->selection);
823 list = g_list_append(NULL, ct->click_info);
826 collection_remove_by_info_list(ct->cd, list);
830 static void collection_table_popup_add_filelist_cb(GtkWidget *widget, gpointer data)
832 CollectTable *ct = data;
835 list = layout_list(NULL);
839 collection_table_add_filelist(ct, list);
844 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
846 CollectTable *ct = data;
848 collection_dialog_append(NULL, ct->cd);
851 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
853 CollectTable *ct = data;
856 dw = dupe_window_new();
857 dupe_window_add_collection(dw, ct->cd);
860 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
862 CollectTable *ct = data;
865 fd = (ct->click_info) ? ct->click_info->fd : NULL;
867 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
870 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
872 CollectTable *ct = data;
874 collection_table_toggle_filenames(ct);
877 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
879 CollectTable *ct = data;
881 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
882 ct->click_info = NULL;
885 filelist_free(ct->drop_list);
886 ct->drop_list = NULL;
887 ct->drop_info = NULL;
889 filelist_free(ct->editmenu_fd_list);
890 ct->editmenu_fd_list = NULL;
893 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
899 menu = popup_menu_short_lived();
901 g_signal_connect(G_OBJECT(menu), "destroy",
902 G_CALLBACK(collection_table_popup_destroy_cb), ct);
904 menu_item_add_sensitive(menu, _("_View"), over_icon,
905 G_CALLBACK(collection_table_popup_view_cb), ct);
906 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
907 G_CALLBACK(collection_table_popup_view_new_cb), ct);
908 menu_item_add_divider(menu);
909 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
910 G_CALLBACK(collection_table_popup_remove_cb), ct);
912 menu_item_add_stock(menu, _("Append from file list"), GTK_STOCK_ADD,
913 G_CALLBACK(collection_table_popup_add_filelist_cb), ct);
914 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
915 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
916 menu_item_add_divider(menu);
918 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
919 submenu = gtk_menu_new();
920 menu_item_add(submenu, _("Select all"),
921 G_CALLBACK(collection_table_popup_selectall_cb), ct);
922 menu_item_add(submenu, _("Select none"),
923 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
924 menu_item_add(submenu, _("Invert selection"),
925 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
926 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
927 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
928 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
929 menu_item_add_divider(menu);
932 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
933 submenu_add_edit(menu, &item,
934 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
935 gtk_widget_set_sensitive(item, over_icon);
937 menu_item_add_divider(menu);
938 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
939 G_CALLBACK(collection_table_popup_copy_cb), ct);
940 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
941 G_CALLBACK(collection_table_popup_move_cb), ct);
942 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
943 G_CALLBACK(collection_table_popup_rename_cb), ct);
944 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
945 G_CALLBACK(collection_table_popup_delete_cb), ct);
946 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
947 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
948 menu_item_add_divider(menu);
950 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
951 menu_item_add_divider(submenu);
952 menu_item_add(submenu, _("Randomize"),
953 G_CALLBACK(collection_table_popup_randomize_cb), ct);
954 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
955 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
957 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
958 G_CALLBACK(collection_table_popup_show_names_cb), ct);
959 menu_item_add_divider(menu);
960 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
961 G_CALLBACK(collection_table_popup_save_cb), ct);
962 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
963 G_CALLBACK(collection_table_popup_save_as_cb), ct);
964 menu_item_add_divider(menu);
965 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
966 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
967 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
968 G_CALLBACK(collection_table_popup_print_cb), ct);
973 *-------------------------------------------------------------------
975 *-------------------------------------------------------------------
978 static void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
983 if (g_list_find(ct->cd->list, ct->focus_info))
985 if (info == ct->focus_info)
987 /* ensure focus row col are correct */
988 collection_table_find_position(ct, ct->focus_info,
989 &ct->focus_row, &ct->focus_column);
992 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
995 if (!collection_table_find_position(ct, info, &row, &col))
997 ct->focus_info = NULL;
999 ct->focus_column = -1;
1003 ct->focus_info = info;
1004 ct->focus_row = row;
1005 ct->focus_column = col;
1006 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1008 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1011 GtkTreeViewColumn *column;
1012 GtkTreeModel *store;
1014 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1016 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1017 tpath = gtk_tree_model_get_path(store, &iter);
1018 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1019 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1020 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1021 gtk_tree_path_free(tpath);
1025 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1032 new_row = ct->focus_row;
1033 new_col = ct->focus_column;
1036 if (new_row < 0) new_row = 0;
1037 if (new_row >= ct->rows) new_row = ct->rows - 1;
1057 new_col = ct->columns - 1;
1064 if (new_col >= ct->columns)
1066 if (new_row < ct->rows - 1)
1073 new_col = ct->columns - 1;
1083 if (new_row >= ct->rows)
1086 new_row = ct->rows - 1;
1089 new_col = ct->columns - 1;
1091 if (new_col >= ct->columns) new_col = ct->columns - 1;
1094 if (new_row == ct->rows - 1)
1098 /* if we moved beyond the last image, go to the last image */
1100 l = g_list_length(ct->cd->list);
1101 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1102 if (new_col >= l) new_col = l - 1;
1105 if (new_row == -1 || new_col == -1)
1107 if (!ct->cd->list) return;
1108 new_row = new_col = 0;
1111 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1114 static void collection_table_update_focus(CollectTable *ct)
1119 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1121 /* first find the old focus, if it exists and is valid */
1125 /* (try to) stay where we were */
1126 new_row = ct->focus_row;
1127 new_col = ct->focus_column;
1130 collection_table_move_focus(ct, new_row, new_col, FALSE);
1133 /* used to figure the page up/down distances */
1134 static gint page_height(CollectTable *ct)
1141 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1142 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1144 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1145 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1147 ret = page_size / row_height;
1148 if (ret < 1) ret = 1;
1153 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1155 CollectTable *ct = data;
1156 GtkTreeModel *store;
1162 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1163 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1164 tpath = gtk_tree_model_get_path(store, &iter);
1165 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1166 gtk_tree_path_free(tpath);
1168 popup_menu_position_clamp(menu, x, y, 0);
1171 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1173 CollectTable *ct = data;
1177 gboolean stop_signal = TRUE;
1179 switch (event->keyval)
1181 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1184 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1187 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1190 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1193 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1194 focus_row = -page_height(ct);
1196 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1197 focus_row = page_height(ct);
1199 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1200 focus_row = -ct->focus_row;
1201 focus_col = -ct->focus_column;
1203 case GDK_KEY_End: case GDK_KEY_KP_End:
1204 focus_row = ct->rows - 1 - ct->focus_row;
1205 focus_col = ct->columns - 1 - ct->focus_column;
1208 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1211 ct->click_info = info;
1212 if (event->state & GDK_CONTROL_MASK)
1214 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1218 collection_table_unselect_all(ct);
1219 collection_table_select(ct, info);
1224 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1228 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1229 ct->click_info = info;
1231 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1234 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1235 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1238 stop_signal = FALSE;
1242 if (focus_row != 0 || focus_col != 0)
1244 CollectInfo *new_info;
1245 CollectInfo *old_info;
1247 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1248 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1249 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1251 if (new_info != old_info)
1253 if (event->state & GDK_SHIFT_MASK)
1255 if (!options->collections.rectangular_selection)
1257 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1261 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1263 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1265 else if (event->state & GDK_CONTROL_MASK)
1267 ct->click_info = new_info;
1271 ct->click_info = new_info;
1272 collection_table_unselect_all(ct);
1273 collection_table_select(ct, new_info);
1287 *-------------------------------------------------------------------
1289 *-------------------------------------------------------------------
1292 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1293 gboolean use_coord, gint x, gint y)
1295 CollectInfo *info = NULL;
1296 GtkTreeModel *store;
1299 GtkTreeViewColumn *column;
1300 #if GTK_CHECK_VERSION(3,0,0)
1301 GdkDeviceManager *device_manager;
1305 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1307 #if GTK_CHECK_VERSION(3,0,0)
1310 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1311 gtk_widget_get_window(ct->listview)));
1312 device = gdk_device_manager_get_client_pointer(device_manager);
1313 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1314 device, &x, &y, NULL);
1317 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1322 if (collection_table_find_iter(ct, source, &iter, &col))
1324 tpath = gtk_tree_model_get_path(store, &iter);
1325 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1326 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1327 gtk_tree_path_free(tpath);
1330 *after = !!(x > cell->x + (cell->width / 2));
1335 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1336 &tpath, &column, NULL, NULL))
1341 gtk_tree_model_get_iter(store, &iter, tpath);
1342 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1344 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1345 info = g_list_nth_data(list, n);
1349 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1350 *after = !!(x > cell->x + (cell->width / 2));
1353 gtk_tree_path_free(tpath);
1360 work = g_list_last(ct->cd->list);
1368 if (collection_table_find_iter(ct, info, &iter, &col))
1370 tpath = gtk_tree_model_get_path(store, &iter);
1371 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1372 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1373 gtk_tree_path_free(tpath);
1381 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1385 gboolean after = FALSE;
1387 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1393 work = g_list_find(ct->cd->list, info);
1394 if (work && work->next)
1396 info = work->next->data;
1407 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1409 gboolean after = FALSE;
1414 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1415 ct->marker_window = NULL;
1420 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1422 /* this setting does not take into account (after), but since it is not really used... */
1423 ct->marker_info = info;
1425 if (!ct->marker_window)
1427 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1428 GdkWindowAttr attributes;
1429 gint attributes_mask;
1430 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1431 gint w = gdk_pixbuf_get_width(pb);
1432 gint h = gdk_pixbuf_get_height(pb);
1434 attributes.window_type = GDK_WINDOW_CHILD;
1435 attributes.wclass = GDK_INPUT_OUTPUT;
1436 attributes.width = w;
1437 attributes.height = h;
1438 attributes.event_mask = gtk_widget_get_events(ct->listview);
1439 attributes_mask = 0;
1441 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1443 #if GTK_CHECK_VERSION(3,0,0)
1444 cairo_region_t *mask;
1445 cairo_pattern_t *pattern;
1446 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1447 cairo_t *cr = cairo_create(img);
1448 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1450 pattern = cairo_pattern_create_for_surface(img);
1451 mask = gdk_cairo_region_create_from_surface(img);
1452 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1453 gdk_window_set_background_pattern(ct->marker_window, pattern);
1454 cairo_region_destroy(mask);
1455 cairo_pattern_destroy(pattern);
1457 cairo_surface_destroy(img);
1461 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1463 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1464 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1466 g_object_unref(pixmap);
1467 if (mask) g_object_unref(mask);
1477 w = gdk_window_get_width(ct->marker_window);
1478 h = gdk_window_get_height(ct->marker_window);
1486 x = cell.x + cell.width;
1489 y = cell.y + (cell.height / 2) - (h / 2);
1491 gdk_window_move(ct->marker_window, x, y);
1492 #if !GTK_CHECK_VERSION(3,0,0)
1493 gdk_window_clear(ct->marker_window);
1495 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1499 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1504 *-------------------------------------------------------------------
1505 * mouse drag auto-scroll
1506 *-------------------------------------------------------------------
1509 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1513 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1518 collection_table_insert_marker(ct, info, TRUE);
1522 tip_update(ct, info);
1526 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1528 CollectTable *ct = data;
1532 #if GTK_CHECK_VERSION(3,0,0)
1533 GdkDeviceManager *device_manager;
1537 if (!ct->drop_idle_id) return FALSE;
1539 window = gtk_widget_get_window(ct->listview);
1540 #if GTK_CHECK_VERSION(3,0,0)
1541 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1542 device = gdk_device_manager_get_client_pointer(device_manager);
1543 gdk_window_get_device_position(window, device, &x, &y, NULL);
1545 gdk_window_get_pointer(window, &x, &y, NULL);
1547 w = gdk_window_get_width(window);
1548 h = gdk_window_get_height(window);
1549 if (x >= 0 && x < w && y >= 0 && y < h)
1551 collection_table_motion_update(ct, x, y, TRUE);
1554 ct->drop_idle_id = 0;
1558 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1560 CollectTable *ct = data;
1562 if (!ct->drop_idle_id)
1564 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1570 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1574 if (ct->drop_idle_id)
1576 g_source_remove(ct->drop_idle_id);
1577 ct->drop_idle_id = 0;
1579 widget_auto_scroll_stop(ct->listview);
1580 collection_table_insert_marker(ct, NULL, FALSE);
1584 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1585 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1586 collection_table_auto_scroll_notify_cb, ct);
1591 *-------------------------------------------------------------------
1593 *-------------------------------------------------------------------
1596 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventMotion *event, gpointer data)
1598 CollectTable *ct = data;
1600 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1605 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1607 CollectTable *ct = data;
1613 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1615 ct->click_info = info;
1616 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1618 switch (bevent->button)
1620 case MOUSE_BUTTON_LEFT:
1621 if (bevent->type == GDK_2BUTTON_PRESS)
1625 layout_image_set_collection(NULL, ct->cd, info);
1628 else if (!gtk_widget_has_focus(ct->listview))
1630 gtk_widget_grab_focus(ct->listview);
1633 case MOUSE_BUTTON_RIGHT:
1634 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1635 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1644 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1646 CollectTable *ct = data;
1648 CollectInfo *info = NULL;
1652 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1654 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1659 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1662 if (bevent->button == MOUSE_BUTTON_LEFT &&
1663 info && ct->click_info == info)
1665 collection_table_set_focus(ct, info);
1667 if (bevent->state & GDK_CONTROL_MASK)
1669 gboolean select = !INFO_SELECTED(info);
1671 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1673 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1677 collection_table_select_util(ct, info, select);
1682 collection_table_unselect_all(ct);
1684 if ((bevent->state & GDK_SHIFT_MASK) &&
1687 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1691 collection_table_select_util(ct, info, TRUE);
1695 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1696 info && ct->click_info == info)
1698 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1704 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1706 CollectTable *ct = data;
1713 *-------------------------------------------------------------------
1714 * populate, add, insert, etc.
1715 *-------------------------------------------------------------------
1718 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1722 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1728 static void collection_table_clear_store(CollectTable *ct)
1730 GtkTreeModel *store;
1732 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1733 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1735 gtk_list_store_clear(GTK_LIST_STORE(store));
1738 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1740 GtkListStore *store;
1744 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1746 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1747 gtk_list_store_append(store, iter);
1748 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1753 static void collection_table_populate(CollectTable *ct, gboolean resize)
1758 collection_table_verify_selections(ct);
1760 collection_table_clear_store(ct);
1767 thumb_width = collection_table_get_icon_width(ct);
1769 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1771 GtkTreeViewColumn *column;
1772 GtkCellRenderer *cell;
1775 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1776 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1777 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1779 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1780 cell = (list) ? list->data : NULL;
1783 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1785 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1786 "fixed_height", options->thumbnails.max_height,
1787 "show_text", ct->show_text, NULL);
1790 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1794 work = ct->cd->list;
1802 list = collection_table_add_row(ct, &iter);
1803 while (work && list)
1805 list->data = work->data;
1813 collection_table_update_focus(ct);
1814 collection_table_update_status(ct);
1817 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1822 thumb_width = collection_table_get_icon_width(ct);
1824 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1825 if (new_cols < 1) new_cols = 1;
1827 if (!force && new_cols == ct->columns) return;
1829 ct->columns = new_cols;
1831 collection_table_populate(ct, TRUE);
1833 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1836 static void collection_table_sync(CollectTable *ct)
1838 GtkTreeModel *store;
1843 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1848 work = ct->cd->list;
1854 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1856 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1857 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1861 list = collection_table_add_row(ct, &iter);
1886 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1890 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1891 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1897 collection_table_update_focus(ct);
1898 collection_table_update_status(ct);
1901 static gboolean collection_table_sync_idle_cb(gpointer data)
1903 CollectTable *ct = data;
1905 if (!ct->sync_idle_id) return FALSE;
1906 g_source_remove(ct->sync_idle_id);
1907 ct->sync_idle_id = 0;
1909 collection_table_sync(ct);
1913 static void collection_table_sync_idle(CollectTable *ct)
1915 if (!ct->sync_idle_id)
1917 /* high priority, the view needs to be resynced before a redraw
1918 * may contain invalid pointers at this time
1920 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1924 void collection_table_add_filelist(CollectTable *ct, GList *list)
1933 collection_add(ct->cd, (FileData *)work->data, FALSE);
1938 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1947 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1951 collection_table_sync_idle(ct);
1954 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1957 GList *insert_pos = NULL;
1961 if (!info_list) return;
1963 info = collection_table_find_data(ct, row, col, NULL);
1965 if (!info_list->next && info_list->data == info) return;
1967 if (info) insert_pos = g_list_find(ct->cd->list, info);
1969 /* FIXME: this may get slow for large lists */
1971 while (insert_pos && work)
1973 if (insert_pos->data == work->data)
1975 insert_pos = insert_pos->next;
1987 ct->cd->list = g_list_remove(ct->cd->list, work->data);
1991 /* place them back in */
1992 temp = g_list_copy(info_list);
1996 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2000 ct->cd->list = g_list_concat(temp, ct->cd->list);
2004 ct->cd->list = g_list_concat(ct->cd->list, temp);
2007 ct->cd->changed = TRUE;
2009 collection_table_sync_idle(ct);
2014 *-------------------------------------------------------------------
2016 *-------------------------------------------------------------------
2019 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2027 collection_table_update_extras(ct, FALSE, 0.0);
2031 if (!collection_table_find_position(ct, info, &row, &col)) return;
2033 if (ct->columns != 0 && ct->rows != 0)
2035 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2042 collection_table_update_extras(ct, TRUE, value);
2044 if (collection_table_find_iter(ct, info, &iter, NULL))
2046 GtkTreeModel *store;
2049 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2050 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2051 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2055 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2057 collection_table_sync_idle(ct);
2060 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2062 collection_table_sync_idle(ct);
2065 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2067 if (ci && INFO_SELECTED(ci))
2069 ct->selection = g_list_remove(ct->selection, ci);
2072 collection_table_sync_idle(ct);
2075 void collection_table_refresh(CollectTable *ct)
2077 collection_table_populate(ct, FALSE);
2081 *-------------------------------------------------------------------
2083 *-------------------------------------------------------------------
2086 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2092 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2095 f = filelist_filter(f, FALSE);
2096 d = filelist_filter(d, TRUE);
2098 f = filelist_sort_path(f);
2099 d = filelist_sort_path(d);
2101 collection_table_insert_filelist(ct, f, ct->marker_info);
2103 work = g_list_last(d);
2106 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2114 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2119 FileData *fd = work->data;
2121 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2123 collection_table_insert_filelist(ct, list, ct->marker_info);
2127 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2129 CollectTable *ct = data;
2131 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2134 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2136 CollectTable *ct = data;
2138 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2141 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2143 CollectTable *ct = data;
2145 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2148 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2152 menu = popup_menu_short_lived();
2153 g_signal_connect(G_OBJECT(menu), "destroy",
2154 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2156 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2157 menu_item_add_divider(menu);
2158 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2159 G_CALLBACK(confirm_dir_list_add), ct);
2160 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2161 G_CALLBACK(confirm_dir_list_recurse), ct);
2162 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2163 G_CALLBACK(confirm_dir_list_skip), ct);
2164 menu_item_add_divider(menu);
2165 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2171 *-------------------------------------------------------------------
2173 *-------------------------------------------------------------------
2176 static GtkTargetEntry collection_drag_types[] = {
2177 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2178 { "text/uri-list", 0, TARGET_URI_LIST },
2179 { "text/plain", 0, TARGET_TEXT_PLAIN }
2181 static gint n_collection_drag_types = 3;
2183 static GtkTargetEntry collection_drop_types[] = {
2184 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2185 { "text/uri-list", 0, TARGET_URI_LIST }
2187 static gint n_collection_drop_types = 2;
2190 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2191 GtkSelectionData *selection_data, guint info,
2192 guint time, gpointer data)
2194 CollectTable *ct = data;
2197 gchar *uri_text = NULL;
2200 if (!ct->click_info) return;
2202 selected = INFO_SELECTED(ct->click_info);
2206 case TARGET_APP_COLLECTION_MEMBER:
2209 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2213 list = g_list_append(NULL, ct->click_info);
2214 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2217 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2218 8, (guchar *)uri_text, total);
2221 case TARGET_URI_LIST:
2222 case TARGET_TEXT_PLAIN:
2226 list = collection_table_selection_get_list(ct);
2230 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2234 uri_selection_data_set_uris_from_filelist(selection_data, list);
2235 filelist_free(list);
2241 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2243 GtkSelectionData *selection_data, guint info,
2244 guint time, gpointer data)
2246 CollectTable *ct = data;
2248 GList *info_list = NULL;
2249 CollectionData *source;
2250 CollectInfo *drop_info;
2253 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2255 collection_table_scroll(ct, FALSE);
2256 collection_table_insert_marker(ct, NULL, FALSE);
2258 drop_info = collection_table_insert_point(ct, x, y);
2262 case TARGET_APP_COLLECTION_MEMBER:
2263 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2266 if (source == ct->cd)
2271 /* it is a move within a collection */
2272 filelist_free(list);
2277 collection_table_move_by_info_list(ct, info_list, -1, -1);
2279 else if (collection_table_find_position(ct, drop_info, &row, &col))
2281 collection_table_move_by_info_list(ct, info_list, row, col);
2286 /* it is a move/copy across collections */
2287 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2289 collection_remove_by_info_list(source, info_list);
2292 g_list_free(info_list);
2295 case TARGET_URI_LIST:
2296 list = uri_filelist_from_gtk_selection_data(selection_data);
2300 FileData *fd = work->data;
2301 if (isdir(fd->path))
2305 ct->drop_list = list;
2306 ct->drop_info = drop_info;
2307 menu = collection_table_drop_menu(ct);
2308 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2321 collection_table_insert_filelist(ct, list, drop_info);
2322 filelist_free(list);
2326 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2328 CollectTable *ct = data;
2330 if (ct->click_info && ct->click_info->pixbuf)
2334 if (INFO_SELECTED(ct->click_info))
2335 items = g_list_length(ct->selection);
2338 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2342 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2344 CollectTable *ct = data;
2346 /* apparently a leave event is not generated on a drop */
2349 collection_table_scroll(ct, FALSE);
2352 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2353 gint x, gint y, guint time, gpointer data)
2355 CollectTable *ct = data;
2357 collection_table_motion_update(ct, x, y, TRUE);
2358 collection_table_scroll(ct, TRUE);
2363 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2365 CollectTable *ct = data;
2367 collection_table_scroll(ct, FALSE);
2370 static void collection_table_dnd_init(CollectTable *ct)
2372 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2373 collection_drag_types, n_collection_drag_types,
2374 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2375 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2376 G_CALLBACK(collection_table_dnd_get), ct);
2377 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2378 G_CALLBACK(collection_table_dnd_begin), ct);
2379 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2380 G_CALLBACK(collection_table_dnd_end), ct);
2382 gtk_drag_dest_set(ct->listview,
2383 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2384 collection_drop_types, n_collection_drop_types,
2385 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2386 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2387 G_CALLBACK(collection_table_dnd_motion), ct);
2388 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2389 G_CALLBACK(collection_table_dnd_leave), ct);
2390 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2391 G_CALLBACK(collection_table_dnd_receive), ct);
2395 *-----------------------------------------------------------------------------
2397 *-----------------------------------------------------------------------------
2400 typedef struct _ColumnData ColumnData;
2407 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2408 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2410 ColumnData *cd = data;
2420 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2421 info = g_list_nth_data(list, cd->number);
2423 style = gtk_widget_get_style(ct->listview);
2424 if (info && (info->flag_mask & SELECTION_SELECTED) )
2426 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2427 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2431 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2432 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2435 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2437 shift_color(&color_bg, -1, 0);
2440 if (GQV_IS_CELL_RENDERER_ICON(cell))
2444 g_object_set(cell, "pixbuf", info->pixbuf,
2445 "text", info->fd->name,
2446 "cell-background-gdk", &color_bg,
2447 "cell-background-set", TRUE,
2448 "foreground-gdk", &color_fg,
2449 "foreground-set", TRUE,
2450 "has-focus", (ct->focus_info == info), NULL);
2454 g_object_set(cell, "pixbuf", NULL,
2456 "cell-background-set", FALSE,
2457 "foreground-set", FALSE,
2458 "has-focus", FALSE, NULL);
2463 static void collection_table_append_column(CollectTable *ct, gint n)
2466 GtkTreeViewColumn *column;
2467 GtkCellRenderer *renderer;
2469 column = gtk_tree_view_column_new();
2470 gtk_tree_view_column_set_min_width(column, 0);
2472 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2473 gtk_tree_view_column_set_alignment(column, 0.5);
2475 renderer = gqv_cell_renderer_icon_new();
2476 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2477 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2478 "ypad", THUMB_BORDER_PADDING,
2479 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2481 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2483 cd = g_new0(ColumnData, 1);
2486 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2488 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2492 *-------------------------------------------------------------------
2494 *-------------------------------------------------------------------
2497 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2499 CollectTable *ct = data;
2503 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2505 gtk_widget_destroy(ct->popup);
2508 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2511 collection_table_scroll(ct, FALSE);
2516 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2518 CollectTable *ct = data;
2520 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2523 CollectTable *collection_table_new(CollectionData *cd)
2526 GtkListStore *store;
2527 GtkTreeSelection *selection;
2530 ct = g_new0(CollectTable, 1);
2533 ct->show_text = options->show_icon_names;
2535 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2536 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2537 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2538 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2540 store = gtk_list_store_new(1, G_TYPE_POINTER);
2541 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2542 g_object_unref(store);
2544 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2545 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2547 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2548 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2550 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2552 collection_table_append_column(ct, i);
2555 /* zero width column to hide tree view focus, we draw it ourselves */
2556 collection_table_append_column(ct, i);
2557 /* end column to fill white space */
2558 collection_table_append_column(ct, i);
2560 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2561 G_CALLBACK(collection_table_destroy), ct);
2562 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2563 G_CALLBACK(collection_table_sized), ct);
2564 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2565 G_CALLBACK(collection_table_press_key_cb), ct);
2567 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2568 gtk_widget_show(ct->listview);
2570 collection_table_dnd_init(ct);
2572 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2573 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2574 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2575 G_CALLBACK(collection_table_press_cb), ct);
2576 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2577 G_CALLBACK(collection_table_release_cb), ct);
2578 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2579 G_CALLBACK(collection_table_motion_cb), ct);
2580 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2581 G_CALLBACK(collection_table_leave_cb), ct);
2586 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2588 ct->status_label = status;
2589 ct->extra_label = extra;
2590 collection_table_update_status(ct);
2591 collection_table_update_extras(ct, FALSE, 0.0);
2594 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2596 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2599 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */