2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "collect-table.h"
25 #include "cellrenderericon.h"
26 #include "collect-dlg.h"
27 #include "collect-io.h"
34 #include "layout-image.h"
37 #include "pixbuf-util.h"
40 #include "ui-fileops.h"
42 #include "ui-tree-edit.h"
43 #include "uri-utils.h"
44 #include "view-file.h"
46 #include "icons/marker.xpm"
47 #define MARKER_WIDTH 26
48 #define MARKER_HEIGHT 32
50 #include <gdk/gdkkeysyms.h> /* for keyboard values */
52 /* between these, the icon width is increased by thumb_max_width / 2 */
53 #define THUMB_MIN_ICON_WIDTH 128
54 #define THUMB_MAX_ICON_WIDTH 150
56 #define COLLECT_TABLE_MAX_COLUMNS 32
57 #define THUMB_BORDER_PADDING 2
59 #define COLLECT_TABLE_TIP_DELAY 500
60 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
64 CTABLE_COLUMN_POINTER = 0,
68 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
71 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
74 * This array must be kept in sync with the contents of:\n
75 * @link collection_table_press_key_cb @endlink \n
76 * @link collection_window_keypress @endlink \n
77 * @link collection_table_popup_menu @endlink
79 * See also @link hard_coded_window_keys @endlink
81 hard_coded_window_keys collection_window_keys[] = {
82 {GDK_CONTROL_MASK, 'C', N_("Copy")},
83 {GDK_CONTROL_MASK, 'M', N_("Move")},
84 {GDK_CONTROL_MASK, 'R', N_("Rename")},
85 {GDK_CONTROL_MASK, 'D', N_("Move to Trash")},
86 {GDK_CONTROL_MASK, 'W', N_("Close window")},
87 {0, GDK_KEY_Delete, N_("Remove")},
88 {0, GDK_KEY_Return, N_("View")},
89 {0, 'V', N_("View in new window")},
90 {GDK_CONTROL_MASK, 'A', N_("Select all")},
91 {GDK_CONTROL_MASK + GDK_SHIFT_MASK, 'A', N_("Select none")},
92 {GDK_MOD1_MASK, 'R', N_("Rectangular selection")},
93 {0, GDK_KEY_space, N_("Select single file")},
94 {GDK_CONTROL_MASK, GDK_KEY_space, N_("Toggle select image")},
95 {GDK_CONTROL_MASK, 'L', N_("Append from file selection")},
96 {0, 'A', N_("Append from collection")},
97 {0, 'S', N_("Save collection")},
98 {GDK_CONTROL_MASK, 'S', N_("Save collection as")},
99 {GDK_CONTROL_MASK, 'T', N_("Show filename text")},
100 {0, 'N', N_("Sort by name")},
101 {0, 'I', N_("Sort by number")},
102 {0, 'D', N_("Sort by date")},
103 {0, 'B', N_("Sort by size")},
104 {0, 'P', N_("Sort by path")},
105 {GDK_SHIFT_MASK, 'P', N_("Print")},
110 *-------------------------------------------------------------------
112 *-------------------------------------------------------------------
115 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
119 n = g_list_index(ct->cd->list, info);
121 if (n < 0) return FALSE;
123 *row = n / ct->columns;
124 *col = n - (*row * ct->columns);
129 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
134 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
135 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
136 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
137 if (column) *column = col;
142 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
147 if (row < 0 || col < 0) return NULL;
149 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
150 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
154 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
155 if (!list) return NULL;
159 return g_list_nth_data(list, col);
165 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
168 GtkTreeViewColumn *column;
174 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
175 &tpath, &column, NULL, NULL))
178 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
179 gtk_tree_model_get_iter(store, &row, tpath);
180 gtk_tree_path_free(tpath);
182 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
183 if (!list) return NULL;
185 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
186 if (iter) *iter = row;
187 return g_list_nth_data(list, n);
190 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
200 CollectInfo *ci = work->data;
208 return g_list_length(ct->cd->list);
211 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
218 work = ct->selection;
221 CollectInfo *ci = work->data;
229 return g_list_length(ct->selection);
232 static void collection_table_update_status(CollectTable *ct)
240 if (!ct->status_label) return;
242 n = collection_table_list_count(ct, &n_bytes);
243 s = collection_table_selection_count(ct, &s_bytes);
247 gchar *b = text_from_size_abrev(n_bytes);
248 gchar *sb = text_from_size_abrev(s_bytes);
249 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
255 gchar *b = text_from_size_abrev(n_bytes);
256 buf = g_strdup_printf(_("%s, %d images"), b, n);
261 buf = g_strdup(_("Empty"));
264 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
268 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
272 if (!ct->extra_label) return;
275 text = _("Loading thumbs...");
279 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
280 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
283 static void collection_table_toggle_filenames(CollectTable *ct)
285 GtkAllocation allocation;
286 ct->show_text = !ct->show_text;
287 options->show_icon_names = ct->show_text;
289 gtk_widget_get_allocation(ct->listview, &allocation);
290 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
293 static void collection_table_toggle_stars(CollectTable *ct)
295 GtkAllocation allocation;
296 ct->show_stars = !ct->show_stars;
297 options->show_star_rating = ct->show_stars;
299 gtk_widget_get_allocation(ct->listview, &allocation);
300 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
303 static gint collection_table_get_icon_width(CollectTable *ct)
307 if (!ct->show_text) return options->thumbnails.max_width;
309 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
310 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
311 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
317 *-------------------------------------------------------------------
319 *-------------------------------------------------------------------
322 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
329 if (info->flag_mask == value) return;
330 info->flag_mask = value;
332 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
335 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
336 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
342 if (collection_table_find_iter(ct, info, &row, NULL))
344 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
345 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
350 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
354 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
357 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
361 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
364 *-------------------------------------------------------------------
366 *-------------------------------------------------------------------
369 static void collection_table_verify_selections(CollectTable *ct)
373 work = ct->selection;
376 CollectInfo *info = work->data;
378 if (!g_list_find(ct->cd->list, info))
380 ct->selection = g_list_remove(ct->selection, info);
385 void collection_table_select_all(CollectTable *ct)
389 g_list_free(ct->selection);
390 ct->selection = NULL;
395 ct->selection = g_list_append(ct->selection, work->data);
396 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
400 collection_table_update_status(ct);
403 void collection_table_unselect_all(CollectTable *ct)
407 work = ct->selection;
410 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
414 g_list_free(ct->selection);
415 ct->selection = NULL;
417 collection_table_update_status(ct);
420 /* Invert the current collection's selection */
421 static void collection_table_select_invert_all(CollectTable *ct)
424 GList *new_selection = NULL;
429 CollectInfo *info = work->data;
431 if (INFO_SELECTED(info))
433 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
437 new_selection = g_list_append(new_selection, info);
438 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
445 g_list_free(ct->selection);
446 ct->selection = new_selection;
448 collection_table_update_status(ct);
451 void collection_table_select(CollectTable *ct, CollectInfo *info)
453 ct->prev_selection = info;
455 if (!info || INFO_SELECTED(info)) return;
457 ct->selection = g_list_append(ct->selection, info);
458 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
460 collection_table_update_status(ct);
463 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
465 ct->prev_selection = info;
467 if (!info || !INFO_SELECTED(info) ) return;
469 ct->selection = g_list_remove(ct->selection, info);
470 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
472 collection_table_update_status(ct);
475 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
479 collection_table_select(ct, info);
483 collection_table_unselect(ct, info);
487 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
494 if (!collection_table_find_position(ct, start, &row1, &col1) ||
495 !collection_table_find_position(ct, end, &row2, &col2) ) return;
497 ct->prev_selection = end;
499 if (!options->collections.rectangular_selection)
504 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
511 work = g_list_find(ct->cd->list, start);
515 collection_table_select_util(ct, info, select);
517 if (work->data != end)
538 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
540 for (i = row1; i <= row2; i++)
542 for (j = col1; j <= col2; j++)
544 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
545 if (info) collection_table_select_util(ct, info, select);
550 GList *collection_table_selection_get_list(CollectTable *ct)
552 return collection_list_to_filelist(ct->selection);
555 static GList *collection_table_get_list(CollectTable *ct)
557 return collection_list_to_filelist(ct->cd->list);
561 *-------------------------------------------------------------------
562 * tooltip type window
563 *-------------------------------------------------------------------
566 static void tip_show(CollectTable *ct)
574 if (ct->tip_window) return;
576 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(ct->listview)));
577 device = gdk_seat_get_pointer(seat);
578 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
579 device, &x, &y, NULL);
581 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
582 if (!ct->tip_info) return;
584 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
585 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
586 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
588 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
590 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
591 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
592 gtk_widget_show(label);
594 display = gdk_display_get_default();
595 seat = gdk_display_get_default_seat(display);
596 device = gdk_seat_get_pointer(seat);
597 gdk_device_get_position(device, NULL, &x, &y);
599 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
600 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
601 gtk_widget_show(ct->tip_window);
604 static void tip_hide(CollectTable *ct)
606 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
607 ct->tip_window = NULL;
610 static gboolean tip_schedule_cb(gpointer data)
612 CollectTable *ct = data;
614 if (!ct->tip_delay_id) return FALSE;
618 ct->tip_delay_id = 0;
622 static void tip_schedule(CollectTable *ct)
626 if (ct->tip_delay_id)
628 g_source_remove(ct->tip_delay_id);
629 ct->tip_delay_id = 0;
632 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
635 static void tip_unschedule(CollectTable *ct)
639 if (ct->tip_delay_id)
641 g_source_remove(ct->tip_delay_id);
642 ct->tip_delay_id = 0;
646 static void tip_update(CollectTable *ct, CollectInfo *info)
648 GdkDisplay *display = gdk_display_get_default();
649 GdkSeat *seat = gdk_display_get_default_seat(display);
650 GdkDevice *device = gdk_seat_get_pointer(seat);
657 gdk_device_get_position(device, NULL, &x, &y);
659 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
661 if (info != ct->tip_info)
672 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
673 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
679 *-------------------------------------------------------------------
681 *-------------------------------------------------------------------
684 static void collection_table_popup_save_as_cb(GtkWidget *UNUSED(widget), gpointer data)
686 CollectTable *ct = data;
688 collection_dialog_save_as(NULL, ct->cd);
691 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
693 CollectTable *ct = data;
697 collection_table_popup_save_as_cb(widget, data);
701 if (!collection_save(ct->cd, ct->cd->path))
703 log_printf("failed saving to collection path: %s\n", ct->cd->path);
707 static GList *collection_table_popup_file_list(CollectTable *ct)
709 if (!ct->click_info) return NULL;
711 if (INFO_SELECTED(ct->click_info))
713 return collection_table_selection_get_list(ct);
716 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
719 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
722 const gchar *key = data;
724 ct = submenu_item_get_data(widget);
728 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
731 static void collection_table_popup_copy_cb(GtkWidget *UNUSED(widget), gpointer data)
733 CollectTable *ct = data;
735 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
738 static void collection_table_popup_move_cb(GtkWidget *UNUSED(widget), gpointer data)
740 CollectTable *ct = data;
742 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
745 static void collection_table_popup_rename_cb(GtkWidget *UNUSED(widget), gpointer data)
747 CollectTable *ct = data;
749 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
752 static void collection_table_popup_delete_cb(GtkWidget *UNUSED(widget), gpointer data)
754 CollectTable *ct = data;
756 options->file_ops.safe_delete_enable = FALSE;
757 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
760 static void collection_table_popup_move_to_trash_cb(GtkWidget *UNUSED(widget), gpointer data)
762 CollectTable *ct = data;
764 options->file_ops.safe_delete_enable = TRUE;
765 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
768 static void collection_table_popup_copy_path_cb(GtkWidget *UNUSED(widget), gpointer data)
770 CollectTable *ct = data;
772 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), TRUE);
775 static void collection_table_popup_copy_path_unquoted_cb(GtkWidget *UNUSED(widget), gpointer data)
777 CollectTable *ct = data;
779 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), FALSE);
782 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
787 ct = submenu_item_get_data(widget);
791 type = (SortType)GPOINTER_TO_INT(data);
793 collection_set_sort_method(ct->cd, type);
796 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer UNUSED(data))
800 ct = submenu_item_get_data(widget);
804 collection_randomize(ct->cd);
807 static void collection_table_popup_view_new_cb(GtkWidget *UNUSED(widget), gpointer data)
809 CollectTable *ct = data;
811 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
813 view_window_new_from_collection(ct->cd, ct->click_info);
817 static void collection_table_popup_view_cb(GtkWidget *UNUSED(widget), gpointer data)
819 CollectTable *ct = data;
821 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
823 layout_image_set_collection(NULL, ct->cd, ct->click_info);
827 static void collection_table_popup_selectall_cb(GtkWidget *UNUSED(widget), gpointer data)
829 CollectTable *ct = data;
831 collection_table_select_all(ct);
832 ct->prev_selection= ct->click_info;
835 static void collection_table_popup_unselectall_cb(GtkWidget *UNUSED(widget), gpointer data)
837 CollectTable *ct = data;
839 collection_table_unselect_all(ct);
840 ct->prev_selection= ct->click_info;
843 static void collection_table_popup_select_invert_cb(GtkWidget *UNUSED(widget), gpointer data)
845 CollectTable *ct = data;
847 collection_table_select_invert_all(ct);
848 ct->prev_selection= ct->click_info;
851 static void collection_table_popup_rectangular_selection_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
853 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
856 static void collection_table_popup_remove_cb(GtkWidget *UNUSED(widget), gpointer data)
858 CollectTable *ct = data;
861 if (!ct->click_info) return;
863 if (INFO_SELECTED(ct->click_info))
865 list = g_list_copy(ct->selection);
869 list = g_list_append(NULL, ct->click_info);
872 collection_remove_by_info_list(ct->cd, list);
876 static void collection_table_popup_add_file_selection_cb(GtkWidget *UNUSED(widget), gpointer data)
878 CollectTable *ct = data;
880 LayoutWindow *lw = NULL;
882 if (!layout_valid(&lw)) return;
884 list = vf_selection_get_list(lw->vf);
888 collection_table_add_filelist(ct, list);
893 static void collection_table_popup_add_collection_cb(GtkWidget *UNUSED(widget), gpointer data)
895 CollectTable *ct = data;
897 collection_dialog_append(NULL, ct->cd);
900 static void collection_table_popup_goto_original_cb(GtkWidget *UNUSED(widget), gpointer data)
902 CollectTable *ct = data;
904 LayoutWindow *lw = NULL;
907 if (!layout_valid(&lw)) return;
908 list = collection_table_selection_get_list(ct);
914 layout_set_fd(lw, fd);
920 static void collection_table_popup_find_dupes_cb(GtkWidget *UNUSED(widget), gpointer data)
922 CollectTable *ct = data;
925 dw = dupe_window_new();
926 dupe_window_add_collection(dw, ct->cd);
929 static void collection_table_popup_print_cb(GtkWidget *UNUSED(widget), gpointer data)
931 CollectTable *ct = data;
934 fd = (ct->click_info) ? ct->click_info->fd : NULL;
936 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), gtk_widget_get_toplevel(ct->listview));
939 static void collection_table_popup_show_names_cb(GtkWidget *UNUSED(widget), gpointer data)
941 CollectTable *ct = data;
943 collection_table_toggle_filenames(ct);
946 static void collection_table_popup_show_stars_cb(GtkWidget *UNUSED(widget), gpointer data)
948 CollectTable *ct = data;
950 collection_table_toggle_stars(ct);
953 static void collection_table_popup_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
955 CollectTable *ct = data;
957 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
958 ct->click_info = NULL;
961 filelist_free(ct->drop_list);
962 ct->drop_list = NULL;
963 ct->drop_info = NULL;
965 filelist_free(ct->editmenu_fd_list);
966 ct->editmenu_fd_list = NULL;
969 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
974 GtkAccelGroup *accel_group;
976 menu = popup_menu_short_lived();
978 accel_group = gtk_accel_group_new();
979 gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
981 g_object_set_data(G_OBJECT(menu), "window_keys", collection_window_keys);
982 g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
984 g_signal_connect(G_OBJECT(menu), "destroy",
985 G_CALLBACK(collection_table_popup_destroy_cb), ct);
987 menu_item_add_sensitive(menu, _("_View"), over_icon,
988 G_CALLBACK(collection_table_popup_view_cb), ct);
989 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
990 G_CALLBACK(collection_table_popup_view_new_cb), ct);
991 menu_item_add_stock(menu, _("Go to original"), GTK_STOCK_FIND,
992 G_CALLBACK(collection_table_popup_goto_original_cb), ct);
993 menu_item_add_divider(menu);
994 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
995 G_CALLBACK(collection_table_popup_remove_cb), ct);
997 menu_item_add_stock(menu, _("Append from file selection"), GTK_STOCK_ADD,
998 G_CALLBACK(collection_table_popup_add_file_selection_cb), ct);
999 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
1000 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
1001 menu_item_add_divider(menu);
1003 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
1004 submenu = gtk_menu_new();
1005 menu_item_add(submenu, _("Select all"),
1006 G_CALLBACK(collection_table_popup_selectall_cb), ct);
1007 menu_item_add(submenu, _("Select none"),
1008 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
1009 menu_item_add(submenu, _("Invert selection"),
1010 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
1011 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
1012 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
1013 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1014 menu_item_add_divider(menu);
1017 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
1018 submenu_add_edit(menu, &item,
1019 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
1020 gtk_widget_set_sensitive(item, over_icon);
1022 menu_item_add_divider(menu);
1023 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
1024 G_CALLBACK(collection_table_popup_copy_cb), ct);
1025 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
1026 G_CALLBACK(collection_table_popup_move_cb), ct);
1027 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
1028 G_CALLBACK(collection_table_popup_rename_cb), ct);
1029 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
1030 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
1031 menu_item_add_sensitive(menu, _("_Copy path unquoted"), over_icon,
1032 G_CALLBACK(collection_table_popup_copy_path_unquoted_cb), ct);
1034 menu_item_add_divider(menu);
1035 menu_item_add_stock_sensitive(menu,
1036 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
1037 _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, over_icon,
1038 G_CALLBACK(collection_table_popup_move_to_trash_cb), ct);
1039 menu_item_add_stock_sensitive(menu,
1040 options->file_ops.confirm_delete ? _("_Delete...") :
1041 _("_Delete"), GTK_STOCK_DELETE, over_icon,
1042 G_CALLBACK(collection_table_popup_delete_cb), ct);
1044 menu_item_add_divider(menu);
1045 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
1046 menu_item_add_divider(submenu);
1047 menu_item_add(submenu, _("Randomize"),
1048 G_CALLBACK(collection_table_popup_randomize_cb), ct);
1049 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
1050 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1052 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
1053 G_CALLBACK(collection_table_popup_show_names_cb), ct);
1054 menu_item_add_check(menu, _("Show star rating"), ct->show_stars,
1055 G_CALLBACK(collection_table_popup_show_stars_cb), ct);
1056 menu_item_add_divider(menu);
1057 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
1058 G_CALLBACK(collection_table_popup_save_cb), ct);
1059 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
1060 G_CALLBACK(collection_table_popup_save_as_cb), ct);
1061 menu_item_add_divider(menu);
1062 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
1063 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
1064 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
1065 G_CALLBACK(collection_table_popup_print_cb), ct);
1070 *-------------------------------------------------------------------
1071 * keyboard callbacks
1072 *-------------------------------------------------------------------
1075 void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
1080 if (g_list_find(ct->cd->list, ct->focus_info))
1082 if (info == ct->focus_info)
1084 /* ensure focus row col are correct */
1085 collection_table_find_position(ct, ct->focus_info,
1086 &ct->focus_row, &ct->focus_column);
1089 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1092 if (!collection_table_find_position(ct, info, &row, &col))
1094 ct->focus_info = NULL;
1096 ct->focus_column = -1;
1100 ct->focus_info = info;
1101 ct->focus_row = row;
1102 ct->focus_column = col;
1103 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1105 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1108 GtkTreeViewColumn *column;
1109 GtkTreeModel *store;
1111 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1113 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1114 tpath = gtk_tree_model_get_path(store, &iter);
1115 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1116 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1117 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1118 gtk_tree_path_free(tpath);
1122 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1129 new_row = ct->focus_row;
1130 new_col = ct->focus_column;
1133 if (new_row < 0) new_row = 0;
1134 if (new_row >= ct->rows) new_row = ct->rows - 1;
1154 new_col = ct->columns - 1;
1161 if (new_col >= ct->columns)
1163 if (new_row < ct->rows - 1)
1170 new_col = ct->columns - 1;
1180 if (new_row >= ct->rows)
1183 new_row = ct->rows - 1;
1186 new_col = ct->columns - 1;
1188 if (new_col >= ct->columns) new_col = ct->columns - 1;
1191 if (new_row == ct->rows - 1)
1195 /* if we moved beyond the last image, go to the last image */
1197 l = g_list_length(ct->cd->list);
1198 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1199 if (new_col >= l) new_col = l - 1;
1202 if (new_row == -1 || new_col == -1)
1204 if (!ct->cd->list) return;
1205 new_row = new_col = 0;
1208 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1211 static void collection_table_update_focus(CollectTable *ct)
1216 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1218 /* first find the old focus, if it exists and is valid */
1222 /* (try to) stay where we were */
1223 new_row = ct->focus_row;
1224 new_col = ct->focus_column;
1227 collection_table_move_focus(ct, new_row, new_col, FALSE);
1230 /* used to figure the page up/down distances */
1231 static gint page_height(CollectTable *ct)
1238 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1239 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1241 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1242 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1244 ret = page_size / row_height;
1245 if (ret < 1) ret = 1;
1250 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1252 CollectTable *ct = data;
1256 gboolean stop_signal = TRUE;
1258 switch (event->keyval)
1260 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1263 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1266 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1269 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1272 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1273 focus_row = -page_height(ct);
1275 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1276 focus_row = page_height(ct);
1278 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1279 focus_row = -ct->focus_row;
1280 focus_col = -ct->focus_column;
1282 case GDK_KEY_End: case GDK_KEY_KP_End:
1283 focus_row = ct->rows - 1 - ct->focus_row;
1284 focus_col = ct->columns - 1 - ct->focus_column;
1287 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1290 ct->click_info = info;
1291 if (event->state & GDK_CONTROL_MASK)
1293 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1297 collection_table_unselect_all(ct);
1298 collection_table_select(ct, info);
1303 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1307 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1308 ct->click_info = info;
1310 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1313 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1314 gtk_menu_popup_at_widget(GTK_MENU(ct->popup), widget, GDK_GRAVITY_SOUTH, GDK_GRAVITY_CENTER, NULL);
1318 stop_signal = FALSE;
1322 if (focus_row != 0 || focus_col != 0)
1324 CollectInfo *new_info;
1325 CollectInfo *old_info;
1327 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1328 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1329 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1331 if (new_info != old_info)
1333 if (event->state & GDK_SHIFT_MASK)
1335 if (!options->collections.rectangular_selection)
1337 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1341 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1343 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1345 else if (event->state & GDK_CONTROL_MASK)
1347 ct->click_info = new_info;
1351 ct->click_info = new_info;
1352 collection_table_unselect_all(ct);
1353 collection_table_select(ct, new_info);
1367 *-------------------------------------------------------------------
1369 *-------------------------------------------------------------------
1372 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1373 gboolean use_coord, gint x, gint y)
1375 CollectInfo *info = NULL;
1376 GtkTreeModel *store;
1379 GtkTreeViewColumn *column;
1383 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1387 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(ct->listview)));
1388 device = gdk_seat_get_pointer(seat);
1389 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1390 device, &x, &y, NULL);
1396 if (collection_table_find_iter(ct, source, &iter, &col))
1398 tpath = gtk_tree_model_get_path(store, &iter);
1399 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1400 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1401 gtk_tree_path_free(tpath);
1404 *after = !!(x > cell->x + (cell->width / 2));
1409 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1410 &tpath, &column, NULL, NULL))
1415 gtk_tree_model_get_iter(store, &iter, tpath);
1416 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1418 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1419 info = g_list_nth_data(list, n);
1423 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1424 *after = !!(x > cell->x + (cell->width / 2));
1427 gtk_tree_path_free(tpath);
1434 work = g_list_last(ct->cd->list);
1442 if (collection_table_find_iter(ct, info, &iter, &col))
1444 tpath = gtk_tree_model_get_path(store, &iter);
1445 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1446 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1447 gtk_tree_path_free(tpath);
1455 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1459 gboolean after = FALSE;
1461 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1467 work = g_list_find(ct->cd->list, info);
1468 if (work && work->next)
1470 info = work->next->data;
1481 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1483 gboolean after = FALSE;
1486 gint x_parent, y_parent;
1488 parent = gtk_widget_get_window(gtk_widget_get_toplevel(ct->listview));
1489 gdk_window_get_position(parent, &x_parent, &y_parent);
1493 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1494 ct->marker_window = NULL;
1499 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1501 /* this setting does not take into account (after), but since it is not really used... */
1502 ct->marker_info = info;
1504 if (!ct->marker_window)
1506 GdkWindowAttr attributes;
1507 gint attributes_mask;
1508 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1509 gint w = gdk_pixbuf_get_width(pb);
1510 gint h = gdk_pixbuf_get_height(pb);
1512 attributes.window_type = GDK_WINDOW_CHILD;
1513 attributes.wclass = GDK_INPUT_OUTPUT;
1514 attributes.width = w;
1515 attributes.height = h;
1516 attributes.event_mask = gtk_widget_get_events(ct->listview);
1517 attributes_mask = 0;
1519 ct->marker_window = gdk_window_new(NULL, &attributes, attributes_mask);
1521 cairo_region_t *mask;
1522 cairo_pattern_t *pattern;
1523 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1524 cairo_t *cr = cairo_create(img);
1525 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1527 /** @FIXME this is a hack to get the background color */
1528 //~ pattern = cairo_pattern_create_for_surface(img);
1529 pattern = cairo_pattern_create_rgb (1.0, 0.0, 0.0);
1530 mask = gdk_cairo_region_create_from_surface(img);
1531 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1532 gdk_window_set_background_pattern(ct->marker_window, pattern);
1533 cairo_region_destroy(mask);
1534 cairo_pattern_destroy(pattern);
1536 cairo_surface_destroy(img);
1545 w = gdk_window_get_width(ct->marker_window);
1546 h = gdk_window_get_height(ct->marker_window);
1554 x = cell.x + cell.width;
1557 y = cell.y + (cell.height / 2) - (h / 2);
1562 gdk_window_move(ct->marker_window, x, y);
1564 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1568 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1573 *-------------------------------------------------------------------
1574 * mouse drag auto-scroll
1575 *-------------------------------------------------------------------
1578 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1582 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1587 collection_table_insert_marker(ct, info, TRUE);
1591 tip_update(ct, info);
1595 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1597 CollectTable *ct = data;
1604 if (!ct->drop_idle_id) return FALSE;
1606 window = gtk_widget_get_window(ct->listview);
1607 seat = gdk_display_get_default_seat(gdk_window_get_display(window));
1608 device = gdk_seat_get_pointer(seat);
1609 gdk_window_get_device_position(window, device, &x, &y, NULL);
1611 w = gdk_window_get_width(window);
1612 h = gdk_window_get_height(window);
1613 if (x >= 0 && x < w && y >= 0 && y < h)
1615 collection_table_motion_update(ct, x, y, TRUE);
1618 ct->drop_idle_id = 0;
1622 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *UNUSED(widget), gint UNUSED(x), gint UNUSED(y), gpointer data)
1624 CollectTable *ct = data;
1626 if (!ct->drop_idle_id)
1628 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1634 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1638 if (ct->drop_idle_id)
1640 g_source_remove(ct->drop_idle_id);
1641 ct->drop_idle_id = 0;
1643 widget_auto_scroll_stop(ct->listview);
1644 collection_table_insert_marker(ct, NULL, FALSE);
1648 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1649 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1650 collection_table_auto_scroll_notify_cb, ct);
1655 *-------------------------------------------------------------------
1657 *-------------------------------------------------------------------
1660 static gboolean collection_table_motion_cb(GtkWidget *UNUSED(widget), GdkEventMotion *event, gpointer data)
1662 CollectTable *ct = data;
1664 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1669 static gboolean collection_table_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *bevent, gpointer data)
1671 CollectTable *ct = data;
1677 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1679 ct->click_info = info;
1680 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1682 switch (bevent->button)
1684 case MOUSE_BUTTON_LEFT:
1685 if (bevent->type == GDK_2BUTTON_PRESS)
1689 layout_image_set_collection(NULL, ct->cd, info);
1692 else if (!gtk_widget_has_focus(ct->listview))
1694 gtk_widget_grab_focus(ct->listview);
1697 case MOUSE_BUTTON_RIGHT:
1698 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1699 gtk_menu_popup_at_pointer(GTK_MENU(ct->popup), NULL);
1708 static gboolean collection_table_release_cb(GtkWidget *UNUSED(widget), GdkEventButton *bevent, gpointer data)
1710 CollectTable *ct = data;
1712 CollectInfo *info = NULL;
1716 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1718 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1723 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1726 if (bevent->button == MOUSE_BUTTON_LEFT &&
1727 info && ct->click_info == info)
1729 collection_table_set_focus(ct, info);
1731 if (bevent->state & GDK_CONTROL_MASK)
1733 gboolean select = !INFO_SELECTED(info);
1735 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1737 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1741 collection_table_select_util(ct, info, select);
1746 collection_table_unselect_all(ct);
1748 if ((bevent->state & GDK_SHIFT_MASK) &&
1751 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1755 collection_table_select_util(ct, info, TRUE);
1759 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1760 info && ct->click_info == info)
1762 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1768 static gboolean collection_table_leave_cb(GtkWidget *UNUSED(widget), GdkEventCrossing *UNUSED(event), gpointer data)
1770 CollectTable *ct = data;
1777 *-------------------------------------------------------------------
1778 * populate, add, insert, etc.
1779 *-------------------------------------------------------------------
1782 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *UNUSED(tpath), GtkTreeIter *iter, gpointer UNUSED(data))
1786 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1792 static void collection_table_clear_store(CollectTable *ct)
1794 GtkTreeModel *store;
1796 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1797 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1799 gtk_list_store_clear(GTK_LIST_STORE(store));
1802 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1804 GtkListStore *store;
1808 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1810 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1811 gtk_list_store_append(store, iter);
1812 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1817 static void collection_table_populate(CollectTable *ct, gboolean resize)
1822 collection_table_verify_selections(ct);
1824 collection_table_clear_store(ct);
1831 thumb_width = collection_table_get_icon_width(ct);
1833 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1835 GtkTreeViewColumn *column;
1836 GtkCellRenderer *cell;
1839 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1840 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1841 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1843 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1844 cell = (list) ? list->data : NULL;
1847 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1849 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1850 "fixed_height", options->thumbnails.max_height,
1851 "show_text", ct->show_text || ct->show_stars, NULL);
1854 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1858 work = ct->cd->list;
1866 list = collection_table_add_row(ct, &iter);
1867 while (work && list)
1869 list->data = work->data;
1877 collection_table_update_focus(ct);
1878 collection_table_update_status(ct);
1881 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint UNUSED(h), gboolean force)
1886 thumb_width = collection_table_get_icon_width(ct);
1888 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1889 if (new_cols < 1) new_cols = 1;
1891 if (!force && new_cols == ct->columns) return;
1893 ct->columns = new_cols;
1895 collection_table_populate(ct, TRUE);
1897 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1900 static void collection_table_sync(CollectTable *ct)
1902 GtkTreeModel *store;
1907 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1912 work = ct->cd->list;
1918 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1920 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1921 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1925 list = collection_table_add_row(ct, &iter);
1950 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1954 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1955 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1961 collection_table_update_focus(ct);
1962 collection_table_update_status(ct);
1965 static gboolean collection_table_sync_idle_cb(gpointer data)
1967 CollectTable *ct = data;
1969 if (!ct->sync_idle_id) return FALSE;
1970 g_source_remove(ct->sync_idle_id);
1971 ct->sync_idle_id = 0;
1973 collection_table_sync(ct);
1977 static void collection_table_sync_idle(CollectTable *ct)
1979 if (!ct->sync_idle_id)
1981 /* high priority, the view needs to be resynced before a redraw
1982 * may contain invalid pointers at this time
1984 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1988 void collection_table_add_filelist(CollectTable *ct, GList *list)
1997 collection_add(ct->cd, (FileData *)work->data, FALSE);
2002 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
2011 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
2015 collection_table_sync_idle(ct);
2018 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
2021 GList *insert_pos = NULL;
2025 if (!info_list) return;
2027 info = collection_table_find_data(ct, row, col, NULL);
2029 if (!info_list->next && info_list->data == info) return;
2031 if (info) insert_pos = g_list_find(ct->cd->list, info);
2033 /** @FIXME this may get slow for large lists */
2035 while (insert_pos && work)
2037 if (insert_pos->data == work->data)
2039 insert_pos = insert_pos->next;
2051 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2055 /* place them back in */
2056 temp = g_list_copy(info_list);
2060 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2064 ct->cd->list = g_list_concat(temp, ct->cd->list);
2068 ct->cd->list = g_list_concat(ct->cd->list, temp);
2071 ct->cd->changed = TRUE;
2073 collection_table_sync_idle(ct);
2078 *-------------------------------------------------------------------
2080 *-------------------------------------------------------------------
2083 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2091 collection_table_update_extras(ct, FALSE, 0.0);
2095 if (!collection_table_find_position(ct, info, &row, &col)) return;
2097 if (ct->columns != 0 && ct->rows != 0)
2099 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2106 collection_table_update_extras(ct, TRUE, value);
2108 if (collection_table_find_iter(ct, info, &iter, NULL))
2110 GtkTreeModel *store;
2113 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2114 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2115 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2119 void collection_table_file_add(CollectTable *ct, CollectInfo *UNUSED(info))
2121 collection_table_sync_idle(ct);
2124 void collection_table_file_insert(CollectTable *ct, CollectInfo *UNUSED(ci))
2126 collection_table_sync_idle(ct);
2129 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2131 if (ci && INFO_SELECTED(ci))
2133 ct->selection = g_list_remove(ct->selection, ci);
2136 collection_table_sync_idle(ct);
2139 void collection_table_refresh(CollectTable *ct)
2141 collection_table_populate(ct, FALSE);
2145 *-------------------------------------------------------------------
2147 *-------------------------------------------------------------------
2150 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2156 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2159 f = filelist_filter(f, FALSE);
2160 d = filelist_filter(d, TRUE);
2162 f = filelist_sort_path(f);
2163 d = filelist_sort_path(d);
2165 collection_table_insert_filelist(ct, f, ct->marker_info);
2167 work = g_list_last(d);
2170 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2178 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2183 FileData *fd = work->data;
2185 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2187 collection_table_insert_filelist(ct, list, ct->marker_info);
2191 static void confirm_dir_list_add(GtkWidget *UNUSED(widget), gpointer data)
2193 CollectTable *ct = data;
2195 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2198 static void confirm_dir_list_recurse(GtkWidget *UNUSED(widget), gpointer data)
2200 CollectTable *ct = data;
2202 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2205 static void confirm_dir_list_skip(GtkWidget *UNUSED(widget), gpointer data)
2207 CollectTable *ct = data;
2209 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2212 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2216 menu = popup_menu_short_lived();
2217 g_signal_connect(G_OBJECT(menu), "destroy",
2218 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2220 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2221 menu_item_add_divider(menu);
2222 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2223 G_CALLBACK(confirm_dir_list_add), ct);
2224 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2225 G_CALLBACK(confirm_dir_list_recurse), ct);
2226 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2227 G_CALLBACK(confirm_dir_list_skip), ct);
2228 menu_item_add_divider(menu);
2229 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2235 *-------------------------------------------------------------------
2237 *-------------------------------------------------------------------
2240 static GtkTargetEntry collection_drag_types[] = {
2241 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2242 { "text/uri-list", 0, TARGET_URI_LIST },
2243 { "text/plain", 0, TARGET_TEXT_PLAIN }
2245 static gint n_collection_drag_types = 3;
2247 static GtkTargetEntry collection_drop_types[] = {
2248 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2249 { "text/uri-list", 0, TARGET_URI_LIST }
2251 static gint n_collection_drop_types = 2;
2254 static void collection_table_dnd_get(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context),
2255 GtkSelectionData *selection_data, guint info,
2256 guint UNUSED(time), gpointer data)
2258 CollectTable *ct = data;
2261 gchar *uri_text = NULL;
2264 if (!ct->click_info) return;
2266 selected = INFO_SELECTED(ct->click_info);
2270 case TARGET_APP_COLLECTION_MEMBER:
2273 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2277 list = g_list_append(NULL, ct->click_info);
2278 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2281 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2282 8, (guchar *)uri_text, total);
2285 case TARGET_URI_LIST:
2286 case TARGET_TEXT_PLAIN:
2290 list = collection_table_selection_get_list(ct);
2294 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2298 uri_selection_data_set_uris_from_filelist(selection_data, list);
2299 filelist_free(list);
2305 static void collection_table_dnd_receive(GtkWidget *UNUSED(widget), GdkDragContext *context,
2307 GtkSelectionData *selection_data, guint info,
2308 guint UNUSED(time), gpointer data)
2310 CollectTable *ct = data;
2312 GList *info_list = NULL;
2313 CollectionData *source;
2314 CollectInfo *drop_info;
2317 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2319 collection_table_scroll(ct, FALSE);
2320 collection_table_insert_marker(ct, NULL, FALSE);
2322 drop_info = collection_table_insert_point(ct, x, y);
2326 case TARGET_APP_COLLECTION_MEMBER:
2327 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2330 if (source == ct->cd)
2335 /* it is a move within a collection */
2336 filelist_free(list);
2341 collection_table_move_by_info_list(ct, info_list, -1, -1);
2343 else if (collection_table_find_position(ct, drop_info, &row, &col))
2345 collection_table_move_by_info_list(ct, info_list, row, col);
2350 /* it is a move/copy across collections */
2351 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2353 collection_remove_by_info_list(source, info_list);
2356 g_list_free(info_list);
2359 case TARGET_URI_LIST:
2360 list = uri_filelist_from_gtk_selection_data(selection_data);
2364 FileData *fd = work->data;
2365 if (isdir(fd->path))
2369 ct->drop_list = list;
2370 ct->drop_info = drop_info;
2371 menu = collection_table_drop_menu(ct);
2372 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
2385 collection_table_insert_filelist(ct, list, drop_info);
2386 filelist_free(list);
2390 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2392 CollectTable *ct = data;
2394 if (ct->click_info && ct->click_info->pixbuf)
2398 if (INFO_SELECTED(ct->click_info))
2399 items = g_list_length(ct->selection);
2402 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2406 static void collection_table_dnd_end(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context), gpointer data)
2408 CollectTable *ct = data;
2410 /* apparently a leave event is not generated on a drop */
2413 collection_table_scroll(ct, FALSE);
2416 static gint collection_table_dnd_motion(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context),
2417 gint x, gint y, guint UNUSED(time), gpointer data)
2419 CollectTable *ct = data;
2421 collection_table_motion_update(ct, x, y, TRUE);
2422 collection_table_scroll(ct, TRUE);
2427 static void collection_table_dnd_leave(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context), guint UNUSED(time), gpointer data)
2429 CollectTable *ct = data;
2431 collection_table_scroll(ct, FALSE);
2434 static void collection_table_dnd_init(CollectTable *ct)
2436 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2437 collection_drag_types, n_collection_drag_types,
2438 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2439 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2440 G_CALLBACK(collection_table_dnd_get), ct);
2441 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2442 G_CALLBACK(collection_table_dnd_begin), ct);
2443 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2444 G_CALLBACK(collection_table_dnd_end), ct);
2446 gtk_drag_dest_set(ct->listview,
2447 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2448 collection_drop_types, n_collection_drop_types,
2449 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2450 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2451 G_CALLBACK(collection_table_dnd_motion), ct);
2452 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2453 G_CALLBACK(collection_table_dnd_leave), ct);
2454 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2455 G_CALLBACK(collection_table_dnd_receive), ct);
2459 *-----------------------------------------------------------------------------
2461 *-----------------------------------------------------------------------------
2464 typedef struct _ColumnData ColumnData;
2471 static void collection_table_cell_data_cb(GtkTreeViewColumn *UNUSED(tree_column), GtkCellRenderer *cell,
2472 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2474 ColumnData *cd = data;
2481 gchar *star_rating = NULL;
2482 gchar *display_text = NULL;
2486 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2488 /** @FIXME this is a primitive hack to stop a crash.
2489 * When compiled with GTK3, if a Collection window containing
2490 * say, 50 or so, images has its width changed, there is a segfault
2491 * https://github.com/BestImageViewer/geeqie/issues/531
2493 if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2495 info = g_list_nth_data(list, cd->number);
2497 style = gtk_widget_get_style(ct->listview);
2498 if (info && (info->flag_mask & SELECTION_SELECTED) )
2500 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2501 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2505 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2506 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2509 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2511 shift_color(&color_bg, -1, 0);
2514 if (ct->show_stars && info && info->fd)
2516 star_rating = metadata_read_rating_stars(info->fd);
2520 star_rating = g_strdup("");
2523 if (info && info->fd)
2525 if (ct->show_text && ct->show_stars)
2527 display_text = g_strconcat(info->fd->name, "\n", star_rating, NULL);
2529 else if (ct->show_text)
2531 display_text = g_strdup(info->fd->name);
2533 else if (ct->show_stars)
2535 display_text = g_strdup(star_rating);
2539 display_text = g_strdup("");
2544 display_text = g_strdup("");
2547 if (GQV_IS_CELL_RENDERER_ICON(cell))
2551 g_object_set(cell, "pixbuf", info->pixbuf,
2552 "text", display_text,
2553 "cell-background-gdk", &color_bg,
2554 "cell-background-set", TRUE,
2555 "foreground-gdk", &color_fg,
2556 "foreground-set", TRUE,
2557 "has-focus", (ct->focus_info == info), NULL);
2561 g_object_set(cell, "pixbuf", NULL,
2563 "cell-background-set", FALSE,
2564 "foreground-set", FALSE,
2565 "has-focus", FALSE, NULL);
2569 g_free(display_text);
2570 g_free(star_rating);
2573 static void collection_table_append_column(CollectTable *ct, gint n)
2576 GtkTreeViewColumn *column;
2577 GtkCellRenderer *renderer;
2579 column = gtk_tree_view_column_new();
2580 gtk_tree_view_column_set_min_width(column, 0);
2582 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2583 gtk_tree_view_column_set_alignment(column, 0.5);
2585 renderer = gqv_cell_renderer_icon_new();
2586 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2587 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2588 "ypad", THUMB_BORDER_PADDING,
2589 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2591 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2593 cd = g_new0(ColumnData, 1);
2596 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2598 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2602 *-------------------------------------------------------------------
2604 *-------------------------------------------------------------------
2607 static void collection_table_destroy(GtkWidget *UNUSED(widget), gpointer data)
2609 CollectTable *ct = data;
2611 /* If there is no unsaved data, save the window geometry
2613 if (!ct->cd->changed)
2615 if (!collection_save(ct->cd, ct->cd->path))
2617 log_printf("failed saving to collection path: %s\n", ct->cd->path);
2623 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2625 gtk_widget_destroy(ct->popup);
2628 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2631 collection_table_scroll(ct, FALSE);
2636 static void collection_table_sized(GtkWidget *UNUSED(widget), GtkAllocation *allocation, gpointer data)
2638 CollectTable *ct = data;
2640 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2643 CollectTable *collection_table_new(CollectionData *cd)
2646 GtkListStore *store;
2647 GtkTreeSelection *selection;
2650 ct = g_new0(CollectTable, 1);
2653 ct->show_text = options->show_icon_names;
2654 ct->show_stars = options->show_star_rating;
2656 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2657 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2658 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2659 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2661 store = gtk_list_store_new(1, G_TYPE_POINTER);
2662 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2663 g_object_unref(store);
2665 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2666 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2668 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2669 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2671 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2673 collection_table_append_column(ct, i);
2676 /* zero width column to hide tree view focus, we draw it ourselves */
2677 collection_table_append_column(ct, i);
2678 /* end column to fill white space */
2679 collection_table_append_column(ct, i);
2681 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2682 G_CALLBACK(collection_table_destroy), ct);
2683 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2684 G_CALLBACK(collection_table_sized), ct);
2685 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2686 G_CALLBACK(collection_table_press_key_cb), ct);
2688 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2689 gtk_widget_show(ct->listview);
2691 collection_table_dnd_init(ct);
2693 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2694 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2695 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2696 G_CALLBACK(collection_table_press_cb), ct);
2697 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2698 G_CALLBACK(collection_table_release_cb), ct);
2699 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2700 G_CALLBACK(collection_table_motion_cb), ct);
2701 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2702 G_CALLBACK(collection_table_leave_cb), ct);
2707 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2709 ct->status_label = status;
2710 ct->extra_label = extra;
2711 collection_table_update_status(ct);
2712 collection_table_update_extras(ct, FALSE, 0.0);
2715 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2717 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2720 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */