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"
33 #include "layout-image.h"
36 #include "pixbuf-util.h"
39 #include "ui-fileops.h"
41 #include "ui-tree-edit.h"
42 #include "uri-utils.h"
43 #include "view-file.h"
45 #include "icons/marker.xpm"
46 #define MARKER_WIDTH 26
47 #define MARKER_HEIGHT 32
49 /* between these, the icon width is increased by thumb_max_width / 2 */
50 #define THUMB_MIN_ICON_WIDTH 128
51 #define THUMB_MAX_ICON_WIDTH 150
53 #define COLLECT_TABLE_MAX_COLUMNS 32
54 #define THUMB_BORDER_PADDING 2
56 #define COLLECT_TABLE_TIP_DELAY 500
57 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
61 CTABLE_COLUMN_POINTER = 0,
65 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
68 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
71 * This array must be kept in sync with the contents of:\n
72 * @link collection_table_press_key_cb @endlink \n
73 * @link collection_window_keypress @endlink \n
74 * @link collection_table_popup_menu @endlink
76 * See also @link hard_coded_window_keys @endlink
78 hard_coded_window_keys collection_window_keys[] = {
79 {GDK_CONTROL_MASK, 'C', N_("Copy")},
80 {GDK_CONTROL_MASK, 'M', N_("Move")},
81 {GDK_CONTROL_MASK, 'R', N_("Rename")},
82 {GDK_CONTROL_MASK, 'D', N_("Move to Trash")},
83 {GDK_CONTROL_MASK, 'W', N_("Close window")},
84 {0, GDK_KEY_Delete, N_("Remove")},
85 {0, GDK_KEY_Return, N_("View")},
86 {0, 'V', N_("View in new window")},
87 {GDK_CONTROL_MASK, 'A', N_("Select all")},
88 {GDK_CONTROL_MASK + GDK_SHIFT_MASK, 'A', N_("Select none")},
89 {GDK_MOD1_MASK, 'R', N_("Rectangular selection")},
90 {0, GDK_KEY_space, N_("Select single file")},
91 {GDK_CONTROL_MASK, GDK_KEY_space, N_("Toggle select image")},
92 {GDK_CONTROL_MASK, 'L', N_("Append from file selection")},
93 {0, 'A', N_("Append from collection")},
94 {0, 'S', N_("Save collection")},
95 {GDK_CONTROL_MASK, 'S', N_("Save collection as")},
96 {GDK_CONTROL_MASK, 'T', N_("Show filename text")},
97 {0, 'N', N_("Sort by name")},
98 {0, 'I', N_("Sort by number")},
99 {0, 'D', N_("Sort by date")},
100 {0, 'B', N_("Sort by size")},
101 {0, 'P', N_("Sort by path")},
102 {GDK_SHIFT_MASK, 'P', N_("Print")},
107 *-------------------------------------------------------------------
109 *-------------------------------------------------------------------
112 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
116 n = g_list_index(ct->cd->list, info);
118 if (n < 0) return FALSE;
120 *row = n / ct->columns;
121 *col = n - (*row * ct->columns);
126 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
131 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
132 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
133 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
134 if (column) *column = col;
139 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
144 if (row < 0 || col < 0) return NULL;
146 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
147 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
151 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
152 if (!list) return NULL;
156 return g_list_nth_data(list, col);
162 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
165 GtkTreeViewColumn *column;
171 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
172 &tpath, &column, NULL, NULL))
175 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
176 gtk_tree_model_get_iter(store, &row, tpath);
177 gtk_tree_path_free(tpath);
179 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
180 if (!list) return NULL;
182 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
183 if (iter) *iter = row;
184 return g_list_nth_data(list, n);
187 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
197 CollectInfo *ci = work->data;
205 return g_list_length(ct->cd->list);
208 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
215 work = ct->selection;
218 CollectInfo *ci = work->data;
226 return g_list_length(ct->selection);
229 static void collection_table_update_status(CollectTable *ct)
237 if (!ct->status_label) return;
239 n = collection_table_list_count(ct, &n_bytes);
240 s = collection_table_selection_count(ct, &s_bytes);
244 gchar *b = text_from_size_abrev(n_bytes);
245 gchar *sb = text_from_size_abrev(s_bytes);
246 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
252 gchar *b = text_from_size_abrev(n_bytes);
253 buf = g_strdup_printf(_("%s, %d images"), b, n);
258 buf = g_strdup(_("Empty"));
261 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
265 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
269 if (!ct->extra_label) return;
272 text = _("Loading thumbs...");
276 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
277 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
280 static void collection_table_toggle_filenames(CollectTable *ct)
282 GtkAllocation allocation;
283 ct->show_text = !ct->show_text;
284 options->show_icon_names = ct->show_text;
286 gtk_widget_get_allocation(ct->listview, &allocation);
287 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
290 static void collection_table_toggle_stars(CollectTable *ct)
292 GtkAllocation allocation;
293 ct->show_stars = !ct->show_stars;
294 options->show_star_rating = ct->show_stars;
296 gtk_widget_get_allocation(ct->listview, &allocation);
297 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
300 static gint collection_table_get_icon_width(CollectTable *ct)
304 if (!ct->show_text) return options->thumbnails.max_width;
306 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
307 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
308 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
314 *-------------------------------------------------------------------
316 *-------------------------------------------------------------------
319 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
326 if (info->flag_mask == value) return;
327 info->flag_mask = value;
329 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
332 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
333 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
339 if (collection_table_find_iter(ct, info, &row, NULL))
341 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
342 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
347 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
351 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
354 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
358 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
361 *-------------------------------------------------------------------
363 *-------------------------------------------------------------------
366 static void collection_table_verify_selections(CollectTable *ct)
370 work = ct->selection;
373 CollectInfo *info = work->data;
375 if (!g_list_find(ct->cd->list, info))
377 ct->selection = g_list_remove(ct->selection, info);
382 void collection_table_select_all(CollectTable *ct)
386 g_list_free(ct->selection);
387 ct->selection = NULL;
392 ct->selection = g_list_append(ct->selection, work->data);
393 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
397 collection_table_update_status(ct);
400 void collection_table_unselect_all(CollectTable *ct)
404 work = ct->selection;
407 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
411 g_list_free(ct->selection);
412 ct->selection = NULL;
414 collection_table_update_status(ct);
417 /* Invert the current collection's selection */
418 static void collection_table_select_invert_all(CollectTable *ct)
421 GList *new_selection = NULL;
426 CollectInfo *info = work->data;
428 if (INFO_SELECTED(info))
430 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
434 new_selection = g_list_append(new_selection, info);
435 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
442 g_list_free(ct->selection);
443 ct->selection = new_selection;
445 collection_table_update_status(ct);
448 void collection_table_select(CollectTable *ct, CollectInfo *info)
450 ct->prev_selection = info;
452 if (!info || INFO_SELECTED(info)) return;
454 ct->selection = g_list_append(ct->selection, info);
455 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
457 collection_table_update_status(ct);
460 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
462 ct->prev_selection = info;
464 if (!info || !INFO_SELECTED(info) ) return;
466 ct->selection = g_list_remove(ct->selection, info);
467 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
469 collection_table_update_status(ct);
472 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
476 collection_table_select(ct, info);
480 collection_table_unselect(ct, info);
484 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
491 if (!collection_table_find_position(ct, start, &row1, &col1) ||
492 !collection_table_find_position(ct, end, &row2, &col2) ) return;
494 ct->prev_selection = end;
496 if (!options->collections.rectangular_selection)
501 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
508 work = g_list_find(ct->cd->list, start);
512 collection_table_select_util(ct, info, select);
514 if (work->data != end)
535 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
537 for (i = row1; i <= row2; i++)
539 for (j = col1; j <= col2; j++)
541 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
542 if (info) collection_table_select_util(ct, info, select);
547 GList *collection_table_selection_get_list(CollectTable *ct)
549 return collection_list_to_filelist(ct->selection);
552 static GList *collection_table_get_list(CollectTable *ct)
554 return collection_list_to_filelist(ct->cd->list);
558 *-------------------------------------------------------------------
559 * tooltip type window
560 *-------------------------------------------------------------------
563 static void tip_show(CollectTable *ct)
571 if (ct->tip_window) return;
573 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(ct->listview)));
574 device = gdk_seat_get_pointer(seat);
575 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
576 device, &x, &y, NULL);
578 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
579 if (!ct->tip_info) return;
581 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
582 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
583 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
585 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
587 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
588 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
589 gtk_widget_show(label);
591 display = gdk_display_get_default();
592 seat = gdk_display_get_default_seat(display);
593 device = gdk_seat_get_pointer(seat);
594 gdk_device_get_position(device, NULL, &x, &y);
596 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
597 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
598 gtk_widget_show(ct->tip_window);
601 static void tip_hide(CollectTable *ct)
603 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
604 ct->tip_window = NULL;
607 static gboolean tip_schedule_cb(gpointer data)
609 CollectTable *ct = data;
611 if (!ct->tip_delay_id) return FALSE;
615 ct->tip_delay_id = 0;
619 static void tip_schedule(CollectTable *ct)
623 if (ct->tip_delay_id)
625 g_source_remove(ct->tip_delay_id);
626 ct->tip_delay_id = 0;
629 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
632 static void tip_unschedule(CollectTable *ct)
636 if (ct->tip_delay_id)
638 g_source_remove(ct->tip_delay_id);
639 ct->tip_delay_id = 0;
643 static void tip_update(CollectTable *ct, CollectInfo *info)
645 GdkDisplay *display = gdk_display_get_default();
646 GdkSeat *seat = gdk_display_get_default_seat(display);
647 GdkDevice *device = gdk_seat_get_pointer(seat);
654 gdk_device_get_position(device, NULL, &x, &y);
656 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
658 if (info != ct->tip_info)
669 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
670 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
676 *-------------------------------------------------------------------
678 *-------------------------------------------------------------------
681 static void collection_table_popup_save_as_cb(GtkWidget *UNUSED(widget), gpointer data)
683 CollectTable *ct = data;
685 collection_dialog_save_as(NULL, ct->cd);
688 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
690 CollectTable *ct = data;
694 collection_table_popup_save_as_cb(widget, data);
698 if (!collection_save(ct->cd, ct->cd->path))
700 log_printf("failed saving to collection path: %s\n", ct->cd->path);
704 static GList *collection_table_popup_file_list(CollectTable *ct)
706 if (!ct->click_info) return NULL;
708 if (INFO_SELECTED(ct->click_info))
710 return collection_table_selection_get_list(ct);
713 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
716 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
719 const gchar *key = data;
721 ct = submenu_item_get_data(widget);
725 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
728 static void collection_table_popup_copy_cb(GtkWidget *UNUSED(widget), gpointer data)
730 CollectTable *ct = data;
732 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
735 static void collection_table_popup_move_cb(GtkWidget *UNUSED(widget), gpointer data)
737 CollectTable *ct = data;
739 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
742 static void collection_table_popup_rename_cb(GtkWidget *UNUSED(widget), gpointer data)
744 CollectTable *ct = data;
746 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
749 static void collection_table_popup_delete_cb(GtkWidget *UNUSED(widget), gpointer data)
751 CollectTable *ct = data;
753 options->file_ops.safe_delete_enable = FALSE;
754 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
757 static void collection_table_popup_move_to_trash_cb(GtkWidget *UNUSED(widget), gpointer data)
759 CollectTable *ct = data;
761 options->file_ops.safe_delete_enable = TRUE;
762 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
765 static void collection_table_popup_copy_path_cb(GtkWidget *UNUSED(widget), gpointer data)
767 CollectTable *ct = data;
769 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), TRUE);
772 static void collection_table_popup_copy_path_unquoted_cb(GtkWidget *UNUSED(widget), gpointer data)
774 CollectTable *ct = data;
776 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct), FALSE);
779 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
784 ct = submenu_item_get_data(widget);
788 type = (SortType)GPOINTER_TO_INT(data);
790 collection_set_sort_method(ct->cd, type);
793 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer UNUSED(data))
797 ct = submenu_item_get_data(widget);
801 collection_randomize(ct->cd);
804 static void collection_table_popup_view_new_cb(GtkWidget *UNUSED(widget), gpointer data)
806 CollectTable *ct = data;
808 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
810 view_window_new_from_collection(ct->cd, ct->click_info);
814 static void collection_table_popup_view_cb(GtkWidget *UNUSED(widget), gpointer data)
816 CollectTable *ct = data;
818 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
820 layout_image_set_collection(NULL, ct->cd, ct->click_info);
824 static void collection_table_popup_selectall_cb(GtkWidget *UNUSED(widget), gpointer data)
826 CollectTable *ct = data;
828 collection_table_select_all(ct);
829 ct->prev_selection= ct->click_info;
832 static void collection_table_popup_unselectall_cb(GtkWidget *UNUSED(widget), gpointer data)
834 CollectTable *ct = data;
836 collection_table_unselect_all(ct);
837 ct->prev_selection= ct->click_info;
840 static void collection_table_popup_select_invert_cb(GtkWidget *UNUSED(widget), gpointer data)
842 CollectTable *ct = data;
844 collection_table_select_invert_all(ct);
845 ct->prev_selection= ct->click_info;
848 static void collection_table_popup_rectangular_selection_cb(GtkWidget *UNUSED(widget), gpointer UNUSED(data))
850 options->collections.rectangular_selection = !(options->collections.rectangular_selection);
853 static void collection_table_popup_remove_cb(GtkWidget *UNUSED(widget), gpointer data)
855 CollectTable *ct = data;
858 if (!ct->click_info) return;
860 if (INFO_SELECTED(ct->click_info))
862 list = g_list_copy(ct->selection);
866 list = g_list_append(NULL, ct->click_info);
869 collection_remove_by_info_list(ct->cd, list);
873 static void collection_table_popup_add_file_selection_cb(GtkWidget *UNUSED(widget), gpointer data)
875 CollectTable *ct = data;
877 LayoutWindow *lw = NULL;
879 if (!layout_valid(&lw)) return;
881 list = vf_selection_get_list(lw->vf);
885 collection_table_add_filelist(ct, list);
890 static void collection_table_popup_add_collection_cb(GtkWidget *UNUSED(widget), gpointer data)
892 CollectTable *ct = data;
894 collection_dialog_append(NULL, ct->cd);
897 static void collection_table_popup_goto_original_cb(GtkWidget *UNUSED(widget), gpointer data)
899 CollectTable *ct = data;
901 LayoutWindow *lw = NULL;
904 if (!layout_valid(&lw)) return;
905 list = collection_table_selection_get_list(ct);
911 layout_set_fd(lw, fd);
917 static void collection_table_popup_find_dupes_cb(GtkWidget *UNUSED(widget), gpointer data)
919 CollectTable *ct = data;
922 dw = dupe_window_new();
923 dupe_window_add_collection(dw, ct->cd);
926 static void collection_table_popup_print_cb(GtkWidget *UNUSED(widget), gpointer data)
928 CollectTable *ct = data;
931 fd = (ct->click_info) ? ct->click_info->fd : NULL;
933 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), gtk_widget_get_toplevel(ct->listview));
936 static void collection_table_popup_show_names_cb(GtkWidget *UNUSED(widget), gpointer data)
938 CollectTable *ct = data;
940 collection_table_toggle_filenames(ct);
943 static void collection_table_popup_show_stars_cb(GtkWidget *UNUSED(widget), gpointer data)
945 CollectTable *ct = data;
947 collection_table_toggle_stars(ct);
950 static void collection_table_popup_destroy_cb(GtkWidget *UNUSED(widget), gpointer data)
952 CollectTable *ct = data;
954 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
955 ct->click_info = NULL;
958 filelist_free(ct->drop_list);
959 ct->drop_list = NULL;
960 ct->drop_info = NULL;
962 filelist_free(ct->editmenu_fd_list);
963 ct->editmenu_fd_list = NULL;
966 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
971 GtkAccelGroup *accel_group;
973 menu = popup_menu_short_lived();
975 accel_group = gtk_accel_group_new();
976 gtk_menu_set_accel_group(GTK_MENU(menu), accel_group);
978 g_object_set_data(G_OBJECT(menu), "window_keys", collection_window_keys);
979 g_object_set_data(G_OBJECT(menu), "accel_group", accel_group);
981 g_signal_connect(G_OBJECT(menu), "destroy",
982 G_CALLBACK(collection_table_popup_destroy_cb), ct);
984 menu_item_add_sensitive(menu, _("_View"), over_icon,
985 G_CALLBACK(collection_table_popup_view_cb), ct);
986 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
987 G_CALLBACK(collection_table_popup_view_new_cb), ct);
988 menu_item_add_stock(menu, _("Go to original"), GTK_STOCK_FIND,
989 G_CALLBACK(collection_table_popup_goto_original_cb), ct);
990 menu_item_add_divider(menu);
991 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
992 G_CALLBACK(collection_table_popup_remove_cb), ct);
994 menu_item_add_stock(menu, _("Append from file selection"), GTK_STOCK_ADD,
995 G_CALLBACK(collection_table_popup_add_file_selection_cb), ct);
996 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
997 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
998 menu_item_add_divider(menu);
1000 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
1001 submenu = gtk_menu_new();
1002 menu_item_add(submenu, _("Select all"),
1003 G_CALLBACK(collection_table_popup_selectall_cb), ct);
1004 menu_item_add(submenu, _("Select none"),
1005 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
1006 menu_item_add(submenu, _("Invert selection"),
1007 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
1008 menu_item_add_check(submenu, _("Rectangular selection"), (options->collections.rectangular_selection != FALSE),
1009 G_CALLBACK(collection_table_popup_rectangular_selection_cb), ct);
1010 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1011 menu_item_add_divider(menu);
1014 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
1015 submenu_add_edit(menu, &item,
1016 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
1017 gtk_widget_set_sensitive(item, over_icon);
1019 menu_item_add_divider(menu);
1020 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
1021 G_CALLBACK(collection_table_popup_copy_cb), ct);
1022 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
1023 G_CALLBACK(collection_table_popup_move_cb), ct);
1024 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
1025 G_CALLBACK(collection_table_popup_rename_cb), ct);
1026 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
1027 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
1028 menu_item_add_sensitive(menu, _("_Copy path unquoted"), over_icon,
1029 G_CALLBACK(collection_table_popup_copy_path_unquoted_cb), ct);
1031 menu_item_add_divider(menu);
1032 menu_item_add_stock_sensitive(menu,
1033 options->file_ops.confirm_move_to_trash ? _("Move to Trash...") :
1034 _("Move to Trash"), PIXBUF_INLINE_ICON_TRASH, over_icon,
1035 G_CALLBACK(collection_table_popup_move_to_trash_cb), ct);
1036 menu_item_add_stock_sensitive(menu,
1037 options->file_ops.confirm_delete ? _("_Delete...") :
1038 _("_Delete"), GTK_STOCK_DELETE, over_icon,
1039 G_CALLBACK(collection_table_popup_delete_cb), ct);
1041 menu_item_add_divider(menu);
1042 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
1043 menu_item_add_divider(submenu);
1044 menu_item_add(submenu, _("Randomize"),
1045 G_CALLBACK(collection_table_popup_randomize_cb), ct);
1046 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
1047 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
1049 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
1050 G_CALLBACK(collection_table_popup_show_names_cb), ct);
1051 menu_item_add_check(menu, _("Show star rating"), ct->show_stars,
1052 G_CALLBACK(collection_table_popup_show_stars_cb), ct);
1053 menu_item_add_divider(menu);
1054 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
1055 G_CALLBACK(collection_table_popup_save_cb), ct);
1056 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
1057 G_CALLBACK(collection_table_popup_save_as_cb), ct);
1058 menu_item_add_divider(menu);
1059 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
1060 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
1061 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
1062 G_CALLBACK(collection_table_popup_print_cb), ct);
1067 *-------------------------------------------------------------------
1068 * keyboard callbacks
1069 *-------------------------------------------------------------------
1072 void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
1077 if (g_list_find(ct->cd->list, ct->focus_info))
1079 if (info == ct->focus_info)
1081 /* ensure focus row col are correct */
1082 collection_table_find_position(ct, ct->focus_info,
1083 &ct->focus_row, &ct->focus_column);
1086 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1089 if (!collection_table_find_position(ct, info, &row, &col))
1091 ct->focus_info = NULL;
1093 ct->focus_column = -1;
1097 ct->focus_info = info;
1098 ct->focus_row = row;
1099 ct->focus_column = col;
1100 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
1102 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
1105 GtkTreeViewColumn *column;
1106 GtkTreeModel *store;
1108 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
1110 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1111 tpath = gtk_tree_model_get_path(store, &iter);
1112 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
1113 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
1114 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
1115 gtk_tree_path_free(tpath);
1119 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
1126 new_row = ct->focus_row;
1127 new_col = ct->focus_column;
1130 if (new_row < 0) new_row = 0;
1131 if (new_row >= ct->rows) new_row = ct->rows - 1;
1151 new_col = ct->columns - 1;
1158 if (new_col >= ct->columns)
1160 if (new_row < ct->rows - 1)
1167 new_col = ct->columns - 1;
1177 if (new_row >= ct->rows)
1180 new_row = ct->rows - 1;
1183 new_col = ct->columns - 1;
1185 if (new_col >= ct->columns) new_col = ct->columns - 1;
1188 if (new_row == ct->rows - 1)
1192 /* if we moved beyond the last image, go to the last image */
1194 l = g_list_length(ct->cd->list);
1195 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1196 if (new_col >= l) new_col = l - 1;
1199 if (new_row == -1 || new_col == -1)
1201 if (!ct->cd->list) return;
1202 new_row = new_col = 0;
1205 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1208 static void collection_table_update_focus(CollectTable *ct)
1213 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1215 /* first find the old focus, if it exists and is valid */
1219 /* (try to) stay where we were */
1220 new_row = ct->focus_row;
1221 new_col = ct->focus_column;
1224 collection_table_move_focus(ct, new_row, new_col, FALSE);
1227 /* used to figure the page up/down distances */
1228 static gint page_height(CollectTable *ct)
1235 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1236 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1238 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1239 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1241 ret = page_size / row_height;
1242 if (ret < 1) ret = 1;
1247 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1249 CollectTable *ct = data;
1253 gboolean stop_signal = TRUE;
1255 switch (event->keyval)
1257 case GDK_KEY_Left: case GDK_KEY_KP_Left:
1260 case GDK_KEY_Right: case GDK_KEY_KP_Right:
1263 case GDK_KEY_Up: case GDK_KEY_KP_Up:
1266 case GDK_KEY_Down: case GDK_KEY_KP_Down:
1269 case GDK_KEY_Page_Up: case GDK_KEY_KP_Page_Up:
1270 focus_row = -page_height(ct);
1272 case GDK_KEY_Page_Down: case GDK_KEY_KP_Page_Down:
1273 focus_row = page_height(ct);
1275 case GDK_KEY_Home: case GDK_KEY_KP_Home:
1276 focus_row = -ct->focus_row;
1277 focus_col = -ct->focus_column;
1279 case GDK_KEY_End: case GDK_KEY_KP_End:
1280 focus_row = ct->rows - 1 - ct->focus_row;
1281 focus_col = ct->columns - 1 - ct->focus_column;
1284 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1287 ct->click_info = info;
1288 if (event->state & GDK_CONTROL_MASK)
1290 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1294 collection_table_unselect_all(ct);
1295 collection_table_select(ct, info);
1300 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1304 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1305 ct->click_info = info;
1307 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1310 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1311 gtk_menu_popup_at_widget(GTK_MENU(ct->popup), widget, GDK_GRAVITY_SOUTH, GDK_GRAVITY_CENTER, NULL);
1315 stop_signal = FALSE;
1319 if (focus_row != 0 || focus_col != 0)
1321 CollectInfo *new_info;
1322 CollectInfo *old_info;
1324 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1325 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1326 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1328 if (new_info != old_info)
1330 if (event->state & GDK_SHIFT_MASK)
1332 if (!options->collections.rectangular_selection)
1334 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1338 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1340 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1342 else if (event->state & GDK_CONTROL_MASK)
1344 ct->click_info = new_info;
1348 ct->click_info = new_info;
1349 collection_table_unselect_all(ct);
1350 collection_table_select(ct, new_info);
1364 *-------------------------------------------------------------------
1366 *-------------------------------------------------------------------
1369 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1370 gboolean use_coord, gint x, gint y)
1372 CollectInfo *info = NULL;
1373 GtkTreeModel *store;
1376 GtkTreeViewColumn *column;
1380 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1384 seat = gdk_display_get_default_seat(gdk_window_get_display(gtk_widget_get_window(ct->listview)));
1385 device = gdk_seat_get_pointer(seat);
1386 gdk_window_get_device_position(gtk_widget_get_window(ct->listview),
1387 device, &x, &y, NULL);
1393 if (collection_table_find_iter(ct, source, &iter, &col))
1395 tpath = gtk_tree_model_get_path(store, &iter);
1396 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1397 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1398 gtk_tree_path_free(tpath);
1401 *after = !!(x > cell->x + (cell->width / 2));
1406 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1407 &tpath, &column, NULL, NULL))
1412 gtk_tree_model_get_iter(store, &iter, tpath);
1413 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1415 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1416 info = g_list_nth_data(list, n);
1420 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1421 *after = !!(x > cell->x + (cell->width / 2));
1424 gtk_tree_path_free(tpath);
1431 work = g_list_last(ct->cd->list);
1439 if (collection_table_find_iter(ct, info, &iter, &col))
1441 tpath = gtk_tree_model_get_path(store, &iter);
1442 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1443 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1444 gtk_tree_path_free(tpath);
1452 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1456 gboolean after = FALSE;
1458 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1464 work = g_list_find(ct->cd->list, info);
1465 if (work && work->next)
1467 info = work->next->data;
1478 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1480 gboolean after = FALSE;
1483 gint x_parent, y_parent;
1485 parent = gtk_widget_get_window(gtk_widget_get_toplevel(ct->listview));
1486 gdk_window_get_position(parent, &x_parent, &y_parent);
1490 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1491 ct->marker_window = NULL;
1496 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1498 /* this setting does not take into account (after), but since it is not really used... */
1499 ct->marker_info = info;
1501 if (!ct->marker_window)
1503 GdkWindowAttr attributes;
1504 gint attributes_mask;
1505 GdkPixbuf *pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1506 gint w = gdk_pixbuf_get_width(pb);
1507 gint h = gdk_pixbuf_get_height(pb);
1509 attributes.window_type = GDK_WINDOW_CHILD;
1510 attributes.wclass = GDK_INPUT_OUTPUT;
1511 attributes.width = w;
1512 attributes.height = h;
1513 attributes.event_mask = gtk_widget_get_events(ct->listview);
1514 attributes_mask = 0;
1516 ct->marker_window = gdk_window_new(NULL, &attributes, attributes_mask);
1518 cairo_region_t *mask;
1519 cairo_pattern_t *pattern;
1520 cairo_surface_t *img = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
1521 cairo_t *cr = cairo_create(img);
1522 gdk_cairo_set_source_pixbuf(cr, pb, 0, 0);
1524 /** @FIXME this is a hack to get the background color */
1525 //~ pattern = cairo_pattern_create_for_surface(img);
1526 pattern = cairo_pattern_create_rgb (1.0, 0.0, 0.0);
1527 mask = gdk_cairo_region_create_from_surface(img);
1528 gdk_window_shape_combine_region(ct->marker_window, mask, 0, 0);
1529 gdk_window_set_background_pattern(ct->marker_window, pattern);
1530 cairo_region_destroy(mask);
1531 cairo_pattern_destroy(pattern);
1533 cairo_surface_destroy(img);
1542 w = gdk_window_get_width(ct->marker_window);
1543 h = gdk_window_get_height(ct->marker_window);
1551 x = cell.x + cell.width;
1554 y = cell.y + (cell.height / 2) - (h / 2);
1559 gdk_window_move(ct->marker_window, x, y);
1561 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1565 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1570 *-------------------------------------------------------------------
1571 * mouse drag auto-scroll
1572 *-------------------------------------------------------------------
1575 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1579 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1584 collection_table_insert_marker(ct, info, TRUE);
1588 tip_update(ct, info);
1592 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1594 CollectTable *ct = data;
1601 if (!ct->drop_idle_id) return FALSE;
1603 window = gtk_widget_get_window(ct->listview);
1604 seat = gdk_display_get_default_seat(gdk_window_get_display(window));
1605 device = gdk_seat_get_pointer(seat);
1606 gdk_window_get_device_position(window, device, &x, &y, NULL);
1608 w = gdk_window_get_width(window);
1609 h = gdk_window_get_height(window);
1610 if (x >= 0 && x < w && y >= 0 && y < h)
1612 collection_table_motion_update(ct, x, y, TRUE);
1615 ct->drop_idle_id = 0;
1619 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *UNUSED(widget), gint UNUSED(x), gint UNUSED(y), gpointer data)
1621 CollectTable *ct = data;
1623 if (!ct->drop_idle_id)
1625 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1631 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1635 if (ct->drop_idle_id)
1637 g_source_remove(ct->drop_idle_id);
1638 ct->drop_idle_id = 0;
1640 widget_auto_scroll_stop(ct->listview);
1641 collection_table_insert_marker(ct, NULL, FALSE);
1645 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1646 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1647 collection_table_auto_scroll_notify_cb, ct);
1652 *-------------------------------------------------------------------
1654 *-------------------------------------------------------------------
1657 static gboolean collection_table_motion_cb(GtkWidget *UNUSED(widget), GdkEventMotion *event, gpointer data)
1659 CollectTable *ct = data;
1661 collection_table_motion_update(ct, (gint)event->x, (gint)event->y, FALSE);
1666 static gboolean collection_table_press_cb(GtkWidget *UNUSED(widget), GdkEventButton *bevent, gpointer data)
1668 CollectTable *ct = data;
1674 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1676 ct->click_info = info;
1677 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1679 switch (bevent->button)
1681 case MOUSE_BUTTON_LEFT:
1682 if (bevent->type == GDK_2BUTTON_PRESS)
1686 layout_image_set_collection(NULL, ct->cd, info);
1689 else if (!gtk_widget_has_focus(ct->listview))
1691 gtk_widget_grab_focus(ct->listview);
1694 case MOUSE_BUTTON_RIGHT:
1695 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1696 gtk_menu_popup_at_pointer(GTK_MENU(ct->popup), NULL);
1705 static gboolean collection_table_release_cb(GtkWidget *UNUSED(widget), GdkEventButton *bevent, gpointer data)
1707 CollectTable *ct = data;
1709 CollectInfo *info = NULL;
1713 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1715 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1720 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1723 if (bevent->button == MOUSE_BUTTON_LEFT &&
1724 info && ct->click_info == info)
1726 collection_table_set_focus(ct, info);
1728 if (bevent->state & GDK_CONTROL_MASK)
1730 gboolean select = !INFO_SELECTED(info);
1732 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1734 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1738 collection_table_select_util(ct, info, select);
1743 collection_table_unselect_all(ct);
1745 if ((bevent->state & GDK_SHIFT_MASK) &&
1748 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1752 collection_table_select_util(ct, info, TRUE);
1756 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1757 info && ct->click_info == info)
1759 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1765 static gboolean collection_table_leave_cb(GtkWidget *UNUSED(widget), GdkEventCrossing *UNUSED(event), gpointer data)
1767 CollectTable *ct = data;
1774 *-------------------------------------------------------------------
1775 * populate, add, insert, etc.
1776 *-------------------------------------------------------------------
1779 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *UNUSED(tpath), GtkTreeIter *iter, gpointer UNUSED(data))
1783 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1789 static void collection_table_clear_store(CollectTable *ct)
1791 GtkTreeModel *store;
1793 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1794 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1796 gtk_list_store_clear(GTK_LIST_STORE(store));
1799 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1801 GtkListStore *store;
1805 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1807 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1808 gtk_list_store_append(store, iter);
1809 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1814 static void collection_table_populate(CollectTable *ct, gboolean resize)
1819 collection_table_verify_selections(ct);
1821 collection_table_clear_store(ct);
1828 thumb_width = collection_table_get_icon_width(ct);
1830 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1832 GtkTreeViewColumn *column;
1833 GtkCellRenderer *cell;
1836 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1837 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1838 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1840 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1841 cell = (list) ? list->data : NULL;
1844 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1846 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1847 "fixed_height", options->thumbnails.max_height,
1848 "show_text", ct->show_text || ct->show_stars, NULL);
1851 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1855 work = ct->cd->list;
1863 list = collection_table_add_row(ct, &iter);
1864 while (work && list)
1866 list->data = work->data;
1874 collection_table_update_focus(ct);
1875 collection_table_update_status(ct);
1878 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint UNUSED(h), gboolean force)
1883 thumb_width = collection_table_get_icon_width(ct);
1885 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1886 if (new_cols < 1) new_cols = 1;
1888 if (!force && new_cols == ct->columns) return;
1890 ct->columns = new_cols;
1892 collection_table_populate(ct, TRUE);
1894 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1897 static void collection_table_sync(CollectTable *ct)
1899 GtkTreeModel *store;
1904 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1909 work = ct->cd->list;
1915 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1917 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1918 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1922 list = collection_table_add_row(ct, &iter);
1947 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1951 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1952 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1958 collection_table_update_focus(ct);
1959 collection_table_update_status(ct);
1962 static gboolean collection_table_sync_idle_cb(gpointer data)
1964 CollectTable *ct = data;
1966 if (!ct->sync_idle_id) return FALSE;
1967 g_source_remove(ct->sync_idle_id);
1968 ct->sync_idle_id = 0;
1970 collection_table_sync(ct);
1974 static void collection_table_sync_idle(CollectTable *ct)
1976 if (!ct->sync_idle_id)
1978 /* high priority, the view needs to be resynced before a redraw
1979 * may contain invalid pointers at this time
1981 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1985 void collection_table_add_filelist(CollectTable *ct, GList *list)
1994 collection_add(ct->cd, (FileData *)work->data, FALSE);
1999 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
2008 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
2012 collection_table_sync_idle(ct);
2015 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
2018 GList *insert_pos = NULL;
2022 if (!info_list) return;
2024 info = collection_table_find_data(ct, row, col, NULL);
2026 if (!info_list->next && info_list->data == info) return;
2028 if (info) insert_pos = g_list_find(ct->cd->list, info);
2030 /** @FIXME this may get slow for large lists */
2032 while (insert_pos && work)
2034 if (insert_pos->data == work->data)
2036 insert_pos = insert_pos->next;
2048 ct->cd->list = g_list_remove(ct->cd->list, work->data);
2052 /* place them back in */
2053 temp = g_list_copy(info_list);
2057 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
2061 ct->cd->list = g_list_concat(temp, ct->cd->list);
2065 ct->cd->list = g_list_concat(ct->cd->list, temp);
2068 ct->cd->changed = TRUE;
2070 collection_table_sync_idle(ct);
2075 *-------------------------------------------------------------------
2077 *-------------------------------------------------------------------
2080 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
2088 collection_table_update_extras(ct, FALSE, 0.0);
2092 if (!collection_table_find_position(ct, info, &row, &col)) return;
2094 if (ct->columns != 0 && ct->rows != 0)
2096 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
2103 collection_table_update_extras(ct, TRUE, value);
2105 if (collection_table_find_iter(ct, info, &iter, NULL))
2107 GtkTreeModel *store;
2110 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
2111 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
2112 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2116 void collection_table_file_add(CollectTable *ct, CollectInfo *UNUSED(info))
2118 collection_table_sync_idle(ct);
2121 void collection_table_file_insert(CollectTable *ct, CollectInfo *UNUSED(ci))
2123 collection_table_sync_idle(ct);
2126 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2128 if (ci && INFO_SELECTED(ci))
2130 ct->selection = g_list_remove(ct->selection, ci);
2133 collection_table_sync_idle(ct);
2136 void collection_table_refresh(CollectTable *ct)
2138 collection_table_populate(ct, FALSE);
2142 *-------------------------------------------------------------------
2144 *-------------------------------------------------------------------
2147 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2153 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2156 f = filelist_filter(f, FALSE);
2157 d = filelist_filter(d, TRUE);
2159 f = filelist_sort_path(f);
2160 d = filelist_sort_path(d);
2162 collection_table_insert_filelist(ct, f, ct->marker_info);
2164 work = g_list_last(d);
2167 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2175 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2180 FileData *fd = work->data;
2182 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2184 collection_table_insert_filelist(ct, list, ct->marker_info);
2188 static void confirm_dir_list_add(GtkWidget *UNUSED(widget), gpointer data)
2190 CollectTable *ct = data;
2192 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2195 static void confirm_dir_list_recurse(GtkWidget *UNUSED(widget), gpointer data)
2197 CollectTable *ct = data;
2199 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2202 static void confirm_dir_list_skip(GtkWidget *UNUSED(widget), gpointer data)
2204 CollectTable *ct = data;
2206 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2209 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2213 menu = popup_menu_short_lived();
2214 g_signal_connect(G_OBJECT(menu), "destroy",
2215 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2217 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2218 menu_item_add_divider(menu);
2219 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2220 G_CALLBACK(confirm_dir_list_add), ct);
2221 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2222 G_CALLBACK(confirm_dir_list_recurse), ct);
2223 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2224 G_CALLBACK(confirm_dir_list_skip), ct);
2225 menu_item_add_divider(menu);
2226 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2232 *-------------------------------------------------------------------
2234 *-------------------------------------------------------------------
2237 static GtkTargetEntry collection_drag_types[] = {
2238 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2239 { "text/uri-list", 0, TARGET_URI_LIST },
2240 { "text/plain", 0, TARGET_TEXT_PLAIN }
2242 static gint n_collection_drag_types = 3;
2244 static GtkTargetEntry collection_drop_types[] = {
2245 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2246 { "text/uri-list", 0, TARGET_URI_LIST }
2248 static gint n_collection_drop_types = 2;
2251 static void collection_table_dnd_get(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context),
2252 GtkSelectionData *selection_data, guint info,
2253 guint UNUSED(time), gpointer data)
2255 CollectTable *ct = data;
2258 gchar *uri_text = NULL;
2261 if (!ct->click_info) return;
2263 selected = INFO_SELECTED(ct->click_info);
2267 case TARGET_APP_COLLECTION_MEMBER:
2270 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2274 list = g_list_append(NULL, ct->click_info);
2275 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2278 gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data),
2279 8, (guchar *)uri_text, total);
2282 case TARGET_URI_LIST:
2283 case TARGET_TEXT_PLAIN:
2287 list = collection_table_selection_get_list(ct);
2291 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2295 uri_selection_data_set_uris_from_filelist(selection_data, list);
2296 filelist_free(list);
2302 static void collection_table_dnd_receive(GtkWidget *UNUSED(widget), GdkDragContext *context,
2304 GtkSelectionData *selection_data, guint info,
2305 guint UNUSED(time), gpointer data)
2307 CollectTable *ct = data;
2309 GList *info_list = NULL;
2310 CollectionData *source;
2311 CollectInfo *drop_info;
2314 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2316 collection_table_scroll(ct, FALSE);
2317 collection_table_insert_marker(ct, NULL, FALSE);
2319 drop_info = collection_table_insert_point(ct, x, y);
2323 case TARGET_APP_COLLECTION_MEMBER:
2324 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2327 if (source == ct->cd)
2332 /* it is a move within a collection */
2333 filelist_free(list);
2338 collection_table_move_by_info_list(ct, info_list, -1, -1);
2340 else if (collection_table_find_position(ct, drop_info, &row, &col))
2342 collection_table_move_by_info_list(ct, info_list, row, col);
2347 /* it is a move/copy across collections */
2348 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2350 collection_remove_by_info_list(source, info_list);
2353 g_list_free(info_list);
2356 case TARGET_URI_LIST:
2357 list = uri_filelist_from_gtk_selection_data(selection_data);
2361 FileData *fd = work->data;
2362 if (isdir(fd->path))
2366 ct->drop_list = list;
2367 ct->drop_info = drop_info;
2368 menu = collection_table_drop_menu(ct);
2369 gtk_menu_popup_at_pointer(GTK_MENU(menu), NULL);
2382 collection_table_insert_filelist(ct, list, drop_info);
2383 filelist_free(list);
2387 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2389 CollectTable *ct = data;
2391 if (ct->click_info && ct->click_info->pixbuf)
2395 if (INFO_SELECTED(ct->click_info))
2396 items = g_list_length(ct->selection);
2399 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2403 static void collection_table_dnd_end(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context), gpointer data)
2405 CollectTable *ct = data;
2407 /* apparently a leave event is not generated on a drop */
2410 collection_table_scroll(ct, FALSE);
2413 static gint collection_table_dnd_motion(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context),
2414 gint x, gint y, guint UNUSED(time), gpointer data)
2416 CollectTable *ct = data;
2418 collection_table_motion_update(ct, x, y, TRUE);
2419 collection_table_scroll(ct, TRUE);
2424 static void collection_table_dnd_leave(GtkWidget *UNUSED(widget), GdkDragContext *UNUSED(context), guint UNUSED(time), gpointer data)
2426 CollectTable *ct = data;
2428 collection_table_scroll(ct, FALSE);
2431 static void collection_table_dnd_init(CollectTable *ct)
2433 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2434 collection_drag_types, n_collection_drag_types,
2435 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2436 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2437 G_CALLBACK(collection_table_dnd_get), ct);
2438 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2439 G_CALLBACK(collection_table_dnd_begin), ct);
2440 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2441 G_CALLBACK(collection_table_dnd_end), ct);
2443 gtk_drag_dest_set(ct->listview,
2444 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2445 collection_drop_types, n_collection_drop_types,
2446 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2447 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2448 G_CALLBACK(collection_table_dnd_motion), ct);
2449 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2450 G_CALLBACK(collection_table_dnd_leave), ct);
2451 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2452 G_CALLBACK(collection_table_dnd_receive), ct);
2456 *-----------------------------------------------------------------------------
2458 *-----------------------------------------------------------------------------
2461 typedef struct _ColumnData ColumnData;
2468 static void collection_table_cell_data_cb(GtkTreeViewColumn *UNUSED(tree_column), GtkCellRenderer *cell,
2469 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2471 ColumnData *cd = data;
2478 gchar *star_rating = NULL;
2479 gchar *display_text = NULL;
2483 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2485 /** @FIXME this is a primitive hack to stop a crash.
2486 * When compiled with GTK3, if a Collection window containing
2487 * say, 50 or so, images has its width changed, there is a segfault
2488 * https://github.com/BestImageViewer/geeqie/issues/531
2490 if (cd->number == COLLECT_TABLE_MAX_COLUMNS) return;
2492 info = g_list_nth_data(list, cd->number);
2494 style = gtk_widget_get_style(ct->listview);
2495 if (info && (info->flag_mask & SELECTION_SELECTED) )
2497 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2498 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2502 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2503 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2506 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2508 shift_color(&color_bg, -1, 0);
2511 if (ct->show_stars && info && info->fd)
2513 star_rating = metadata_read_rating_stars(info->fd);
2517 star_rating = g_strdup("");
2520 if (info && info->fd)
2522 if (ct->show_text && ct->show_stars)
2524 display_text = g_strconcat(info->fd->name, "\n", star_rating, NULL);
2526 else if (ct->show_text)
2528 display_text = g_strdup(info->fd->name);
2530 else if (ct->show_stars)
2532 display_text = g_strdup(star_rating);
2536 display_text = g_strdup("");
2541 display_text = g_strdup("");
2544 if (GQV_IS_CELL_RENDERER_ICON(cell))
2548 g_object_set(cell, "pixbuf", info->pixbuf,
2549 "text", display_text,
2550 "cell-background-gdk", &color_bg,
2551 "cell-background-set", TRUE,
2552 "foreground-gdk", &color_fg,
2553 "foreground-set", TRUE,
2554 "has-focus", (ct->focus_info == info), NULL);
2558 g_object_set(cell, "pixbuf", NULL,
2560 "cell-background-set", FALSE,
2561 "foreground-set", FALSE,
2562 "has-focus", FALSE, NULL);
2566 g_free(display_text);
2567 g_free(star_rating);
2570 static void collection_table_append_column(CollectTable *ct, gint n)
2573 GtkTreeViewColumn *column;
2574 GtkCellRenderer *renderer;
2576 column = gtk_tree_view_column_new();
2577 gtk_tree_view_column_set_min_width(column, 0);
2579 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2580 gtk_tree_view_column_set_alignment(column, 0.5);
2582 renderer = gqv_cell_renderer_icon_new();
2583 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2584 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2585 "ypad", THUMB_BORDER_PADDING,
2586 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2588 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2590 cd = g_new0(ColumnData, 1);
2593 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2595 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2599 *-------------------------------------------------------------------
2601 *-------------------------------------------------------------------
2604 static void collection_table_destroy(GtkWidget *UNUSED(widget), gpointer data)
2606 CollectTable *ct = data;
2608 /* If there is no unsaved data, save the window geometry
2610 if (!ct->cd->changed)
2612 if (!collection_save(ct->cd, ct->cd->path))
2614 log_printf("failed saving to collection path: %s\n", ct->cd->path);
2620 g_signal_handlers_disconnect_matched(G_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2622 gtk_widget_destroy(ct->popup);
2625 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2628 collection_table_scroll(ct, FALSE);
2633 static void collection_table_sized(GtkWidget *UNUSED(widget), GtkAllocation *allocation, gpointer data)
2635 CollectTable *ct = data;
2637 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2640 CollectTable *collection_table_new(CollectionData *cd)
2643 GtkListStore *store;
2644 GtkTreeSelection *selection;
2647 ct = g_new0(CollectTable, 1);
2650 ct->show_text = options->show_icon_names;
2651 ct->show_stars = options->show_star_rating;
2653 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2654 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2655 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2656 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2658 store = gtk_list_store_new(1, G_TYPE_POINTER);
2659 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2660 g_object_unref(store);
2662 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2663 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2665 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2666 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2668 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2670 collection_table_append_column(ct, i);
2673 /* zero width column to hide tree view focus, we draw it ourselves */
2674 collection_table_append_column(ct, i);
2675 /* end column to fill white space */
2676 collection_table_append_column(ct, i);
2678 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2679 G_CALLBACK(collection_table_destroy), ct);
2680 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2681 G_CALLBACK(collection_table_sized), ct);
2682 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2683 G_CALLBACK(collection_table_press_key_cb), ct);
2685 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2686 gtk_widget_show(ct->listview);
2688 collection_table_dnd_init(ct);
2690 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2691 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2692 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2693 G_CALLBACK(collection_table_press_cb), ct);
2694 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2695 G_CALLBACK(collection_table_release_cb), ct);
2696 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2697 G_CALLBACK(collection_table_motion_cb), ct);
2698 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2699 G_CALLBACK(collection_table_leave_cb), ct);
2704 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2706 ct->status_label = status;
2707 ct->extra_label = extra;
2708 collection_table_update_status(ct);
2709 collection_table_update_extras(ct, FALSE, 0.0);
2712 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2714 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2717 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */