2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "collect-table.h"
25 #include "cellrenderericon.h"
26 #include "collect-dlg.h"
27 #include "collect-io.h"
34 #include "layout_image.h"
38 #include "ui_fileops.h"
40 #include "ui_tree_edit.h"
41 #include "uri_utils.h"
43 #include "icons/marker.xpm"
44 #define MARKER_WIDTH 26
45 #define MARKER_HEIGHT 32
47 #include <gdk/gdkkeysyms.h> /* for keyboard values */
49 /* between these, the icon width is increased by thumb_max_width / 2 */
50 #define THUMB_MIN_ICON_WIDTH 128
51 #define THUMB_MAX_ICON_WIDTH 150
53 #define COLLECT_TABLE_MAX_COLUMNS 32
54 #define THUMB_BORDER_PADDING 2
56 #define COLLECT_TABLE_TIP_DELAY 500
57 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
61 CTABLE_COLUMN_POINTER = 0,
67 SELECTION_SELECTED = 1 << 0,
68 SELECTION_PRELIGHT = 1 << 1,
69 SELECTION_FOCUS = 1 << 2
73 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
76 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
80 *-------------------------------------------------------------------
82 *-------------------------------------------------------------------
85 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
89 n = g_list_index(ct->cd->list, info);
91 if (n < 0) return FALSE;
93 *row = n / ct->columns;
94 *col = n - (*row * ct->columns);
99 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
104 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
105 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
106 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
107 if (column) *column = col;
112 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
117 if (row < 0 || col < 0) return NULL;
119 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
120 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
124 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
125 if (!list) return NULL;
129 return g_list_nth_data(list, col);
135 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
138 GtkTreeViewColumn *column;
144 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
145 &tpath, &column, NULL, NULL))
148 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
149 gtk_tree_model_get_iter(store, &row, tpath);
150 gtk_tree_path_free(tpath);
152 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
153 if (!list) return NULL;
155 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
156 if (iter) *iter = row;
157 return g_list_nth_data(list, n);
160 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
170 CollectInfo *ci = work->data;
178 return g_list_length(ct->cd->list);
181 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
188 work = ct->selection;
191 CollectInfo *ci = work->data;
199 return g_list_length(ct->selection);
202 static void collection_table_update_status(CollectTable *ct)
210 if (!ct->status_label) return;
212 n = collection_table_list_count(ct, &n_bytes);
213 s = collection_table_selection_count(ct, &s_bytes);
217 gchar *b = text_from_size_abrev(n_bytes);
218 gchar *sb = text_from_size_abrev(s_bytes);
219 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
225 gchar *b = text_from_size_abrev(n_bytes);
226 buf = g_strdup_printf(_("%s, %d images"), b, n);
231 buf = g_strdup(_("Empty"));
234 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
238 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
242 if (!ct->extra_label) return;
245 text = _("Loading thumbs...");
249 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
250 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
253 static void collection_table_toggle_filenames(CollectTable *ct)
255 GtkAllocation allocation;
256 ct->show_text = !ct->show_text;
257 options->show_icon_names = ct->show_text;
259 gtk_widget_get_allocation(ct->listview, &allocation);
260 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
263 static gint collection_table_get_icon_width(CollectTable *ct)
267 if (!ct->show_text) return options->thumbnails.max_width;
269 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
270 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
271 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
277 *-------------------------------------------------------------------
279 *-------------------------------------------------------------------
282 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
289 if (info->flag_mask == value) return;
290 info->flag_mask = value;
292 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
295 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
296 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
302 if (collection_table_find_iter(ct, info, &row, NULL))
304 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
305 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
310 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
314 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
317 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
321 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
324 *-------------------------------------------------------------------
326 *-------------------------------------------------------------------
329 static void collection_table_verify_selections(CollectTable *ct)
333 work = ct->selection;
336 CollectInfo *info = work->data;
338 if (!g_list_find(ct->cd->list, info))
340 ct->selection = g_list_remove(ct->selection, info);
345 void collection_table_select_all(CollectTable *ct)
349 g_list_free(ct->selection);
350 ct->selection = NULL;
355 ct->selection = g_list_append(ct->selection, work->data);
356 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
360 collection_table_update_status(ct);
363 void collection_table_unselect_all(CollectTable *ct)
367 work = ct->selection;
370 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
374 g_list_free(ct->selection);
375 ct->selection = NULL;
377 collection_table_update_status(ct);
380 /* Invert the current collection's selection */
381 static void collection_table_select_invert_all(CollectTable *ct)
384 GList *new_selection = NULL;
389 CollectInfo *info = work->data;
391 if (INFO_SELECTED(info))
393 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
397 new_selection = g_list_append(new_selection, info);
398 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
405 g_list_free(ct->selection);
406 ct->selection = new_selection;
408 collection_table_update_status(ct);
411 static void collection_table_select(CollectTable *ct, CollectInfo *info)
413 ct->prev_selection = info;
415 if (!info || INFO_SELECTED(info)) return;
417 ct->selection = g_list_append(ct->selection, info);
418 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
420 collection_table_update_status(ct);
423 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
425 ct->prev_selection = info;
427 if (!info || !INFO_SELECTED(info) ) return;
429 ct->selection = g_list_remove(ct->selection, info);
430 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
432 collection_table_update_status(ct);
435 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
439 collection_table_select(ct, info);
443 collection_table_unselect(ct, info);
447 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
454 if (!collection_table_find_position(ct, start, &row1, &col1) ||
455 !collection_table_find_position(ct, end, &row2, &col2) ) return;
457 ct->prev_selection = end;
459 if (!options->collections.rectangular_selection)
464 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
471 work = g_list_find(ct->cd->list, start);
475 collection_table_select_util(ct, info, select);
477 if (work->data != end)
498 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
500 for (i = row1; i <= row2; i++)
502 for (j = col1; j <= col2; j++)
504 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
505 if (info) collection_table_select_util(ct, info, select);
510 GList *collection_table_selection_get_list(CollectTable *ct)
512 return collection_list_to_filelist(ct->selection);
515 static GList *collection_table_get_list(CollectTable *ct)
517 return collection_list_to_filelist(ct->cd->list);
521 *-------------------------------------------------------------------
522 * tooltip type window
523 *-------------------------------------------------------------------
526 static void tip_show(CollectTable *ct)
530 #if GTK_CHECK_VERSION(3,0,0)
532 GdkDeviceManager *device_manager;
536 if (ct->tip_window) return;
538 #if GTK_CHECK_VERSION(3,0,0)
539 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
540 gtk_widget_get_window(ct->listview)));
541 device = gdk_device_manager_get_client_pointer(device_manager);
542 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
543 device, &x, &y, NULL);
545 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
548 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
549 if (!ct->tip_info) return;
551 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
552 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
553 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
555 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
557 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
558 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
559 gtk_widget_show(label);
561 #if GTK_CHECK_VERSION(3,0,0)
562 display = gdk_display_get_default();
563 device_manager = gdk_display_get_device_manager(display);
564 device = gdk_device_manager_get_client_pointer(device_manager);
565 gdk_device_get_position(device, NULL, &x, &y);
567 gdk_window_get_pointer(NULL, &x, &y, NULL);
570 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
571 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
572 gtk_widget_show(ct->tip_window);
575 static void tip_hide(CollectTable *ct)
577 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
578 ct->tip_window = NULL;
581 static gboolean tip_schedule_cb(gpointer data)
583 CollectTable *ct = data;
585 if (!ct->tip_delay_id) return FALSE;
589 ct->tip_delay_id = 0;
593 static void tip_schedule(CollectTable *ct)
597 if (ct->tip_delay_id)
599 g_source_remove(ct->tip_delay_id);
600 ct->tip_delay_id = 0;
603 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
606 static void tip_unschedule(CollectTable *ct)
610 if (ct->tip_delay_id)
612 g_source_remove(ct->tip_delay_id);
613 ct->tip_delay_id = 0;
617 static void tip_update(CollectTable *ct, CollectInfo *info)
619 #if GTK_CHECK_VERSION(3,0,0)
620 GdkDisplay *display = gdk_display_get_default();
621 GdkDeviceManager *device_manager = gdk_display_get_device_manager(display);
622 GdkDevice *device = gdk_device_manager_get_client_pointer(device_manager);
630 #if GTK_CHECK_VERSION(3,0,0)
631 gdk_device_get_position(device, NULL, &x, &y);
633 gdk_window_get_pointer(NULL, &x, &y, NULL);
635 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
637 if (info != ct->tip_info)
648 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
649 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
655 *-------------------------------------------------------------------
657 *-------------------------------------------------------------------
660 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
662 CollectTable *ct = data;
664 collection_dialog_save_as(NULL, ct->cd);
667 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
669 CollectTable *ct = data;
673 collection_table_popup_save_as_cb(widget, data);
677 if (!collection_save(ct->cd, ct->cd->path))
679 log_printf("failed saving to collection path: %s\n", ct->cd->path);
683 static GList *collection_table_popup_file_list(CollectTable *ct)
685 if (!ct->click_info) return NULL;
687 if (INFO_SELECTED(ct->click_info))
689 return collection_table_selection_get_list(ct);
692 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
695 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
698 const gchar *key = data;
700 ct = submenu_item_get_data(widget);
704 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
707 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
709 CollectTable *ct = data;
711 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
714 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
716 CollectTable *ct = data;
718 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
721 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
723 CollectTable *ct = data;
725 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
728 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
730 CollectTable *ct = data;
732 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
735 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
737 CollectTable *ct = data;
739 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct));
742 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
747 ct = submenu_item_get_data(widget);
751 type = (SortType)GPOINTER_TO_INT(data);
753 collection_set_sort_method(ct->cd, type);
756 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
760 ct = submenu_item_get_data(widget);
764 collection_randomize(ct->cd);
767 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
769 CollectTable *ct = data;
771 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
773 view_window_new_from_collection(ct->cd, ct->click_info);
777 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
779 CollectTable *ct = data;
781 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
783 layout_image_set_collection(NULL, ct->cd, ct->click_info);
787 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
789 CollectTable *ct = data;
791 collection_table_select_all(ct);
792 ct->prev_selection= ct->click_info;
795 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
797 CollectTable *ct = data;
799 collection_table_unselect_all(ct);
800 ct->prev_selection= ct->click_info;
803 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
805 CollectTable *ct = data;
807 collection_table_select_invert_all(ct);
808 ct->prev_selection= ct->click_info;
811 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
813 CollectTable *ct = data;
816 if (!ct->click_info) return;
818 if (INFO_SELECTED(ct->click_info))
820 list = g_list_copy(ct->selection);
824 list = g_list_append(NULL, ct->click_info);
827 collection_remove_by_info_list(ct->cd, list);
831 static void collection_table_popup_add_filelist_cb(GtkWidget *widget, gpointer data)
833 CollectTable *ct = data;
836 list = layout_list(NULL);
840 collection_table_add_filelist(ct, list);
845 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
847 CollectTable *ct = data;
849 collection_dialog_append(NULL, ct->cd);
852 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
854 CollectTable *ct = data;
857 dw = dupe_window_new(DUPE_MATCH_NAME);
858 dupe_window_add_collection(dw, ct->cd);
861 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
863 CollectTable *ct = data;
866 fd = (ct->click_info) ? ct->click_info->fd : NULL;
868 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
871 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
873 CollectTable *ct = data;
875 collection_table_toggle_filenames(ct);
878 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
880 CollectTable *ct = data;
882 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
883 ct->click_info = NULL;
886 filelist_free(ct->drop_list);
887 ct->drop_list = NULL;
888 ct->drop_info = NULL;
890 filelist_free(ct->editmenu_fd_list);
891 ct->editmenu_fd_list = NULL;
894 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
900 menu = popup_menu_short_lived();
902 g_signal_connect(G_OBJECT(menu), "destroy",
903 G_CALLBACK(collection_table_popup_destroy_cb), ct);
905 menu_item_add_sensitive(menu, _("_View"), over_icon,
906 G_CALLBACK(collection_table_popup_view_cb), ct);
907 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
908 G_CALLBACK(collection_table_popup_view_new_cb), ct);
909 menu_item_add_divider(menu);
910 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
911 G_CALLBACK(collection_table_popup_remove_cb), ct);
913 menu_item_add_stock(menu, _("Append from file list"), GTK_STOCK_ADD,
914 G_CALLBACK(collection_table_popup_add_filelist_cb), ct);
915 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
916 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
917 menu_item_add_divider(menu);
919 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
920 submenu = gtk_menu_new();
921 menu_item_add(submenu, _("Select all"),
922 G_CALLBACK(collection_table_popup_selectall_cb), ct);
923 menu_item_add(submenu, _("Select none"),
924 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
925 menu_item_add(submenu, _("Invert selection"),
926 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
927 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
928 menu_item_add_divider(menu);
931 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
932 submenu_add_edit(menu, &item,
933 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
934 gtk_widget_set_sensitive(item, over_icon);
936 menu_item_add_divider(menu);
937 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
938 G_CALLBACK(collection_table_popup_copy_cb), ct);
939 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
940 G_CALLBACK(collection_table_popup_move_cb), ct);
941 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
942 G_CALLBACK(collection_table_popup_rename_cb), ct);
943 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
944 G_CALLBACK(collection_table_popup_delete_cb), ct);
945 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
946 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
947 menu_item_add_divider(menu);
949 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
950 menu_item_add_divider(submenu);
951 menu_item_add(submenu, _("Randomize"),
952 G_CALLBACK(collection_table_popup_randomize_cb), ct);
953 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
954 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
956 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
957 G_CALLBACK(collection_table_popup_show_names_cb), ct);
958 menu_item_add_divider(menu);
959 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
960 G_CALLBACK(collection_table_popup_save_cb), ct);
961 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
962 G_CALLBACK(collection_table_popup_save_as_cb), ct);
963 menu_item_add_divider(menu);
964 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
965 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
966 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
967 G_CALLBACK(collection_table_popup_print_cb), ct);
972 *-------------------------------------------------------------------
974 *-------------------------------------------------------------------
977 static void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
982 if (g_list_find(ct->cd->list, ct->focus_info))
984 if (info == ct->focus_info)
986 /* ensure focus row col are correct */
987 collection_table_find_position(ct, ct->focus_info,
988 &ct->focus_row, &ct->focus_column);
991 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
994 if (!collection_table_find_position(ct, info, &row, &col))
996 ct->focus_info = NULL;
998 ct->focus_column = -1;
1002 ct->focus_info = info;
1003 ct->focus_row = row;
1004 ct->focus_column = col;
1005 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1007 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1010 GtkTreeViewColumn *column;
1011 GtkTreeModel *store;
1013 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1015 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1016 tpath = gtk_tree_model_get_path(store, &iter);
1017 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1018 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1019 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1020 gtk_tree_path_free(tpath);
1024 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1031 new_row = ct->focus_row;
1032 new_col = ct->focus_column;
1035 if (new_row < 0) new_row = 0;
1036 if (new_row >= ct->rows) new_row = ct->rows - 1;
1056 new_col = ct->columns - 1;
1063 if (new_col >= ct->columns)
1065 if (new_row < ct->rows - 1)
1072 new_col = ct->columns - 1;
1082 if (new_row >= ct->rows)
1085 new_row = ct->rows - 1;
1088 new_col = ct->columns - 1;
1090 if (new_col >= ct->columns) new_col = ct->columns - 1;
1093 if (new_row == ct->rows - 1)
1097 /* if we moved beyond the last image, go to the last image */
1099 l = g_list_length(ct->cd->list);
1100 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1101 if (new_col >= l) new_col = l - 1;
1104 if (new_row == -1 || new_col == -1)
1106 if (!ct->cd->list) return;
1107 new_row = new_col = 0;
1110 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1113 static void collection_table_update_focus(CollectTable *ct)
1118 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1120 /* first find the old focus, if it exists and is valid */
1124 /* (try to) stay where we were */
1125 new_row = ct->focus_row;
1126 new_col = ct->focus_column;
1129 collection_table_move_focus(ct, new_row, new_col, FALSE);
1132 /* used to figure the page up/down distances */
1133 static gint page_height(CollectTable *ct)
1140 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1141 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1143 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1144 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1146 ret = page_size / row_height;
1147 if (ret < 1) ret = 1;
1152 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1154 CollectTable *ct = data;
1155 GtkTreeModel *store;
1161 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1162 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1163 tpath = gtk_tree_model_get_path(store, &iter);
1164 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1165 gtk_tree_path_free(tpath);
1167 popup_menu_position_clamp(menu, x, y, 0);
1170 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1172 CollectTable *ct = data;
1176 gboolean stop_signal = TRUE;
1178 switch (event->keyval)
1180 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1183 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1186 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1189 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1192 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1193 focus_row = -page_height(ct);
1195 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1196 focus_row = page_height(ct);
1198 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1199 focus_row = -ct->focus_row;
1200 focus_col = -ct->focus_column;
1202 case GDK_KEY_End: case GDK_KEY_KP_End:
1203 focus_row = ct->rows - 1 - ct->focus_row;
1204 focus_col = ct->columns - 1 - ct->focus_column;
1207 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1210 ct->click_info = info;
1211 if (event->state & GDK_CONTROL_MASK)
1213 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1217 collection_table_unselect_all(ct);
1218 collection_table_select(ct, info);
1223 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1227 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1228 ct->click_info = info;
1230 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1233 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1234 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1237 stop_signal = FALSE;
1241 if (focus_row != 0 || focus_col != 0)
1243 CollectInfo *new_info;
1244 CollectInfo *old_info;
1246 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1247 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1248 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1250 if (new_info != old_info)
1252 if (event->state & GDK_SHIFT_MASK)
1254 if (!options->collections.rectangular_selection)
1256 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1260 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1262 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1264 else if (event->state & GDK_CONTROL_MASK)
1266 ct->click_info = new_info;
1270 ct->click_info = new_info;
1271 collection_table_unselect_all(ct);
1272 collection_table_select(ct, new_info);
1286 *-------------------------------------------------------------------
1288 *-------------------------------------------------------------------
1291 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1292 gboolean use_coord, gint x, gint y)
1294 CollectInfo *info = NULL;
1295 GtkTreeModel *store;
1298 GtkTreeViewColumn *column;
1299 #if GTK_CHECK_VERSION(3,0,0)
1300 GdkDeviceManager *device_manager;
1304 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1306 #if GTK_CHECK_VERSION(3,0,0)
1309 device_manager = gdk_display_get_device_manager(gdk_window_get_display(
1310 gtk_widget_get_window(ct->listview)));
1311 device = gdk_device_manager_get_client_pointer(device_manager);
1312 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1313 device, &x, &y, NULL);
1316 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1321 if (collection_table_find_iter(ct, source, &iter, &col))
1323 tpath = gtk_tree_model_get_path(store, &iter);
1324 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1325 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1326 gtk_tree_path_free(tpath);
1329 *after = !!(x > cell->x + (cell->width / 2));
1334 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1335 &tpath, &column, NULL, NULL))
1340 gtk_tree_model_get_iter(store, &iter, tpath);
1341 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1343 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1344 info = g_list_nth_data(list, n);
1348 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1349 *after = !!(x > cell->x + (cell->width / 2));
1352 gtk_tree_path_free(tpath);
1359 work = g_list_last(ct->cd->list);
1367 if (collection_table_find_iter(ct, info, &iter, &col))
1369 tpath = gtk_tree_model_get_path(store, &iter);
1370 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1371 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1372 gtk_tree_path_free(tpath);
1380 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1384 gboolean after = FALSE;
1386 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1392 work = g_list_find(ct->cd->list, info);
1393 if (work && work->next)
1395 info = work->next->data;
1406 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1408 gboolean after = FALSE;
1413 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1414 ct->marker_window = NULL;
1419 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1421 /* this setting does not take into account (after), but since it is not really used... */
1422 ct->marker_info = info;
1424 if (!ct->marker_window)
1426 GdkWindow *parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1427 GdkWindowAttr attributes;
1428 gint attributes_mask;
1429 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1430 gint w = gdk_pixbuf_get_width(pb);
1431 gint h = gdk_pixbuf_get_height(pb);
1433 attributes.window_type = GDK_WINDOW_CHILD;
1434 attributes.wclass = GDK_INPUT_OUTPUT;
1435 attributes.width = w;
1436 attributes.height = h;
1437 attributes.event_mask = gtk_widget_get_events(ct->listview);
1438 attributes_mask = 0;
1440 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1442 #if GTK_CHECK_VERSION(3,0,0)
1443 cairo_region_t *mask;
1444 cairo_pattern_t *pattern;
1445 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1446 cairo_t *cr = cairo_create(img);
1447 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1449 pattern = cairo_pattern_create_for_surface(img);
1450 mask = gdk_cairo_region_create_from_surface(img);
1451 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1452 gdk_window_set_background_pattern(ct->marker_window, pattern);
1453 cairo_region_destroy(mask);
1454 cairo_pattern_destroy(pattern);
1456 cairo_surface_destroy(img);
1460 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1462 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1463 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1465 g_object_unref(pixmap);
1466 if (mask) g_object_unref(mask);
1476 w = gdk_window_get_width(ct->marker_window);
1477 h = gdk_window_get_height(ct->marker_window);
1485 x = cell.x + cell.width;
1488 y = cell.y + (cell.height / 2) - (h / 2);
1490 gdk_window_move(ct->marker_window, x, y);
1491 #if !GTK_CHECK_VERSION(3,0,0)
1492 gdk_window_clear(ct->marker_window);
1494 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1498 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1503 *-------------------------------------------------------------------
1504 * mouse drag auto-scroll
1505 *-------------------------------------------------------------------
1508 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1512 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1517 collection_table_insert_marker(ct, info, TRUE);
1521 tip_update(ct, info);
1525 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1527 CollectTable *ct = data;
1531 #if GTK_CHECK_VERSION(3,0,0)
1532 GdkDeviceManager *device_manager;
1536 if (!ct->drop_idle_id) return FALSE;
1538 window = gtk_widget_get_window(ct->listview);
1539 #if GTK_CHECK_VERSION(3,0,0)
1540 device_manager = gdk_display_get_device_manager(gdk_window_get_display(window));
1541 device = gdk_device_manager_get_client_pointer(device_manager);
1542 gdk_window_get_device_position(window, device, &x, &y, NULL);
1544 gdk_window_get_pointer(window, &x, &y, NULL);
1546 w = gdk_window_get_width(window);
1547 h = gdk_window_get_height(window);
1548 if (x >= 0 && x < w && y >= 0 && y < h)
1550 collection_table_motion_update(ct, x, y, TRUE);
1553 ct->drop_idle_id = 0;
1557 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1559 CollectTable *ct = data;
1561 if (!ct->drop_idle_id)
1563 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1569 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1573 if (ct->drop_idle_id)
1575 g_source_remove(ct->drop_idle_id);
1576 ct->drop_idle_id = 0;
1578 widget_auto_scroll_stop(ct->listview);
1579 collection_table_insert_marker(ct, NULL, FALSE);
1583 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1584 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1585 collection_table_auto_scroll_notify_cb, ct);
1590 *-------------------------------------------------------------------
1592 *-------------------------------------------------------------------
1595 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1597 CollectTable *ct = data;
1599 collection_table_motion_update(ct, (gint)bevent->x, (gint)bevent->y, FALSE);
1604 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1606 CollectTable *ct = data;
1612 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1614 ct->click_info = info;
1615 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1617 switch (bevent->button)
1619 case MOUSE_BUTTON_LEFT:
1620 if (bevent->type == GDK_2BUTTON_PRESS)
1624 layout_image_set_collection(NULL, ct->cd, info);
1627 else if (!gtk_widget_has_focus(ct->listview))
1629 gtk_widget_grab_focus(ct->listview);
1632 case MOUSE_BUTTON_RIGHT:
1633 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1634 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1643 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1645 CollectTable *ct = data;
1647 CollectInfo *info = NULL;
1651 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1653 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1658 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1661 if (bevent->button == MOUSE_BUTTON_LEFT &&
1662 info && ct->click_info == info)
1664 collection_table_set_focus(ct, info);
1666 if (bevent->state & GDK_CONTROL_MASK)
1668 gboolean select = !INFO_SELECTED(info);
1670 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1672 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1676 collection_table_select_util(ct, info, select);
1681 collection_table_unselect_all(ct);
1683 if ((bevent->state & GDK_SHIFT_MASK) &&
1686 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1690 collection_table_select_util(ct, info, TRUE);
1694 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1695 info && ct->click_info == info)
1697 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1703 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1705 CollectTable *ct = data;
1712 *-------------------------------------------------------------------
1713 * populate, add, insert, etc.
1714 *-------------------------------------------------------------------
1717 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1721 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1727 static void collection_table_clear_store(CollectTable *ct)
1729 GtkTreeModel *store;
1731 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1732 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1734 gtk_list_store_clear(GTK_LIST_STORE(store));
1737 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1739 GtkListStore *store;
1743 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1745 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1746 gtk_list_store_append(store, iter);
1747 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1752 static void collection_table_populate(CollectTable *ct, gboolean resize)
1757 collection_table_verify_selections(ct);
1759 collection_table_clear_store(ct);
1766 thumb_width = collection_table_get_icon_width(ct);
1768 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1770 GtkTreeViewColumn *column;
1771 GtkCellRenderer *cell;
1774 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1775 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1776 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1778 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1779 cell = (list) ? list->data : NULL;
1782 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1784 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1785 "fixed_height", options->thumbnails.max_height,
1786 "show_text", ct->show_text, NULL);
1789 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1793 work = ct->cd->list;
1801 list = collection_table_add_row(ct, &iter);
1802 while (work && list)
1804 list->data = work->data;
1812 collection_table_update_focus(ct);
1813 collection_table_update_status(ct);
1816 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1821 thumb_width = collection_table_get_icon_width(ct);
1823 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1824 if (new_cols < 1) new_cols = 1;
1826 if (!force && new_cols == ct->columns) return;
1828 ct->columns = new_cols;
1830 collection_table_populate(ct, TRUE);
1832 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1835 static void collection_table_sync(CollectTable *ct)
1837 GtkTreeModel *store;
1842 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1847 work = ct->cd->list;
1853 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1855 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1856 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1860 list = collection_table_add_row(ct, &iter);
1885 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1889 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1890 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1896 collection_table_update_focus(ct);
1897 collection_table_update_status(ct);
1900 static gboolean collection_table_sync_idle_cb(gpointer data)
1902 CollectTable *ct = data;
1904 if (!ct->sync_idle_id) return FALSE;
1905 g_source_remove(ct->sync_idle_id);
1906 ct->sync_idle_id = 0;
1908 collection_table_sync(ct);
1912 static void collection_table_sync_idle(CollectTable *ct)
1914 if (!ct->sync_idle_id)
1916 /* high priority, the view needs to be resynced before a redraw
1917 * may contain invalid pointers at this time
1919 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1923 void collection_table_add_filelist(CollectTable *ct, GList *list)
1932 collection_add(ct->cd, (FileData *)work->data, FALSE);
1937 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1946 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1950 collection_table_sync_idle(ct);
1953 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1956 GList *insert_pos = NULL;
1960 if (!info_list) return;
1962 info = collection_table_find_data(ct, row, col, NULL);
1964 if (!info_list->next && info_list->data == info) return;
1966 if (info) insert_pos = g_list_find(ct->cd->list, info);
1968 /* FIXME: this may get slow for large lists */
1970 while (insert_pos && work)
1972 if (insert_pos->data == work->data)
1974 insert_pos = insert_pos->next;
1986 ct->cd->list = g_list_remove(ct->cd->list, work->data);
1990 /* place them back in */
1991 temp = g_list_copy(info_list);
1995 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
1999 ct->cd->list = g_list_concat(temp, ct->cd->list);
2003 ct->cd->list = g_list_concat(ct->cd->list, temp);
2006 ct->cd->changed = TRUE;
2008 collection_table_sync_idle(ct);
2013 *-------------------------------------------------------------------
2015 *-------------------------------------------------------------------
2018 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2026 collection_table_update_extras(ct, FALSE, 0.0);
2030 if (!collection_table_find_position(ct, info, &row, &col)) return;
2032 if (ct->columns != 0 && ct->rows != 0)
2034 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2041 collection_table_update_extras(ct, TRUE, value);
2043 if (collection_table_find_iter(ct, info, &iter, NULL))
2045 GtkTreeModel *store;
2048 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2049 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2050 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2054 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2056 collection_table_sync_idle(ct);
2059 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2061 collection_table_sync_idle(ct);
2064 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2066 if (ci && INFO_SELECTED(ci))
2068 ct->selection = g_list_remove(ct->selection, ci);
2071 collection_table_sync_idle(ct);
2074 void collection_table_refresh(CollectTable *ct)
2076 collection_table_populate(ct, FALSE);
2080 *-------------------------------------------------------------------
2082 *-------------------------------------------------------------------
2085 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2091 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2094 f = filelist_filter(f, FALSE);
2095 d = filelist_filter(d, TRUE);
2097 f = filelist_sort_path(f);
2098 d = filelist_sort_path(d);
2100 collection_table_insert_filelist(ct, f, ct->marker_info);
2102 work = g_list_last(d);
2105 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2113 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2118 FileData *fd = work->data;
2120 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2122 collection_table_insert_filelist(ct, list, ct->marker_info);
2126 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2128 CollectTable *ct = data;
2130 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2133 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2135 CollectTable *ct = data;
2137 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2140 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2142 CollectTable *ct = data;
2144 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2147 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2151 menu = popup_menu_short_lived();
2152 g_signal_connect(G_OBJECT(menu), "destroy",
2153 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2155 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2156 menu_item_add_divider(menu);
2157 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2158 G_CALLBACK(confirm_dir_list_add), ct);
2159 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2160 G_CALLBACK(confirm_dir_list_recurse), ct);
2161 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2162 G_CALLBACK(confirm_dir_list_skip), ct);
2163 menu_item_add_divider(menu);
2164 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2170 *-------------------------------------------------------------------
2172 *-------------------------------------------------------------------
2175 static GtkTargetEntry collection_drag_types[] = {
2176 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2177 { "text/uri-list", 0, TARGET_URI_LIST },
2178 { "text/plain", 0, TARGET_TEXT_PLAIN }
2180 static gint n_collection_drag_types = 3;
2182 static GtkTargetEntry collection_drop_types[] = {
2183 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2184 { "text/uri-list", 0, TARGET_URI_LIST }
2186 static gint n_collection_drop_types = 2;
2189 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2190 GtkSelectionData *selection_data, guint info,
2191 guint time, gpointer data)
2193 CollectTable *ct = data;
2196 gchar *uri_text = NULL;
2199 if (!ct->click_info) return;
2201 selected = INFO_SELECTED(ct->click_info);
2205 case TARGET_APP_COLLECTION_MEMBER:
2208 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2212 list = g_list_append(NULL, ct->click_info);
2213 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2216 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2217 8, (guchar *)uri_text, total);
2220 case TARGET_URI_LIST:
2221 case TARGET_TEXT_PLAIN:
2225 list = collection_table_selection_get_list(ct);
2229 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2233 uri_selection_data_set_uris_from_filelist(selection_data, list);
2234 filelist_free(list);
2240 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2242 GtkSelectionData *selection_data, guint info,
2243 guint time, gpointer data)
2245 CollectTable *ct = data;
2247 GList *info_list = NULL;
2248 CollectionData *source;
2249 CollectInfo *drop_info;
2252 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2254 collection_table_scroll(ct, FALSE);
2255 collection_table_insert_marker(ct, NULL, FALSE);
2257 drop_info = collection_table_insert_point(ct, x, y);
2261 case TARGET_APP_COLLECTION_MEMBER:
2262 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2265 if (source == ct->cd)
2270 /* it is a move within a collection */
2271 filelist_free(list);
2276 collection_table_move_by_info_list(ct, info_list, -1, -1);
2278 else if (collection_table_find_position(ct, drop_info, &row, &col))
2280 collection_table_move_by_info_list(ct, info_list, row, col);
2285 /* it is a move/copy across collections */
2286 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2288 collection_remove_by_info_list(source, info_list);
2291 g_list_free(info_list);
2294 case TARGET_URI_LIST:
2295 list = uri_filelist_from_gtk_selection_data(selection_data);
2299 FileData *fd = work->data;
2300 if (isdir(fd->path))
2304 ct->drop_list = list;
2305 ct->drop_info = drop_info;
2306 menu = collection_table_drop_menu(ct);
2307 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2320 collection_table_insert_filelist(ct, list, drop_info);
2321 filelist_free(list);
2325 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2327 CollectTable *ct = data;
2329 if (ct->click_info && ct->click_info->pixbuf)
2333 if (INFO_SELECTED(ct->click_info))
2334 items = g_list_length(ct->selection);
2337 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2341 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2343 CollectTable *ct = data;
2345 /* apparently a leave event is not generated on a drop */
2348 collection_table_scroll(ct, FALSE);
2351 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2352 gint x, gint y, guint time, gpointer data)
2354 CollectTable *ct = data;
2356 collection_table_motion_update(ct, x, y, TRUE);
2357 collection_table_scroll(ct, TRUE);
2362 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2364 CollectTable *ct = data;
2366 collection_table_scroll(ct, FALSE);
2369 static void collection_table_dnd_init(CollectTable *ct)
2371 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2372 collection_drag_types, n_collection_drag_types,
2373 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2374 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2375 G_CALLBACK(collection_table_dnd_get), ct);
2376 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2377 G_CALLBACK(collection_table_dnd_begin), ct);
2378 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2379 G_CALLBACK(collection_table_dnd_end), ct);
2381 gtk_drag_dest_set(ct->listview,
2382 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2383 collection_drop_types, n_collection_drop_types,
2384 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2385 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2386 G_CALLBACK(collection_table_dnd_motion), ct);
2387 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2388 G_CALLBACK(collection_table_dnd_leave), ct);
2389 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2390 G_CALLBACK(collection_table_dnd_receive), ct);
2394 *-----------------------------------------------------------------------------
2396 *-----------------------------------------------------------------------------
2399 typedef struct _ColumnData ColumnData;
2406 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2407 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2409 ColumnData *cd = data;
2419 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2420 info = g_list_nth_data(list, cd->number);
2422 style = gtk_widget_get_style(ct->listview);
2423 if (info && (info->flag_mask & SELECTION_SELECTED) )
2425 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2426 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2430 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2431 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2434 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2436 shift_color(&color_bg, -1, 0);
2439 if (GQV_IS_CELL_RENDERER_ICON(cell))
2443 g_object_set(cell, "pixbuf", info->pixbuf,
2444 "text", info->fd->name,
2445 "cell-background-gdk", &color_bg,
2446 "cell-background-set", TRUE,
2447 "foreground-gdk", &color_fg,
2448 "foreground-set", TRUE,
2449 "has-focus", (ct->focus_info == info), NULL);
2453 g_object_set(cell, "pixbuf", NULL,
2455 "cell-background-set", FALSE,
2456 "foreground-set", FALSE,
2457 "has-focus", FALSE, NULL);
2462 static void collection_table_append_column(CollectTable *ct, gint n)
2465 GtkTreeViewColumn *column;
2466 GtkCellRenderer *renderer;
2468 column = gtk_tree_view_column_new();
2469 gtk_tree_view_column_set_min_width(column, 0);
2471 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2472 gtk_tree_view_column_set_alignment(column, 0.5);
2474 renderer = gqv_cell_renderer_icon_new();
2475 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2476 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2477 "ypad", THUMB_BORDER_PADDING,
2478 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2480 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2482 cd = g_new0(ColumnData, 1);
2485 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2487 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2491 *-------------------------------------------------------------------
2493 *-------------------------------------------------------------------
2496 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2498 CollectTable *ct = data;
2502 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2504 gtk_widget_destroy(ct->popup);
2507 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2510 collection_table_scroll(ct, FALSE);
2515 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2517 CollectTable *ct = data;
2519 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2522 CollectTable *collection_table_new(CollectionData *cd)
2525 GtkListStore *store;
2526 GtkTreeSelection *selection;
2529 ct = g_new0(CollectTable, 1);
2532 ct->show_text = options->show_icon_names;
2534 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2535 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2536 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2537 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2539 store = gtk_list_store_new(1, G_TYPE_POINTER);
2540 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2541 g_object_unref(store);
2543 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2544 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2546 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2547 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2549 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2551 collection_table_append_column(ct, i);
2554 /* zero width column to hide tree view focus, we draw it ourselves */
2555 collection_table_append_column(ct, i);
2556 /* end column to fill white space */
2557 collection_table_append_column(ct, i);
2559 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2560 G_CALLBACK(collection_table_destroy), ct);
2561 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2562 G_CALLBACK(collection_table_sized), ct);
2563 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2564 G_CALLBACK(collection_table_press_key_cb), ct);
2566 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2567 gtk_widget_show(ct->listview);
2569 collection_table_dnd_init(ct);
2571 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2572 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2573 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2574 G_CALLBACK(collection_table_press_cb), ct);
2575 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2576 G_CALLBACK(collection_table_release_cb), ct);
2577 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2578 G_CALLBACK(collection_table_motion_cb), ct);
2579 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2580 G_CALLBACK(collection_table_leave_cb), ct);
2585 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2587 ct->status_label = status;
2588 ct->extra_label = extra;
2589 collection_table_update_status(ct);
2590 collection_table_update_extras(ct, FALSE, 0.0);
2593 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2595 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2598 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */