4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
15 #include "collect-table.h"
17 #include "cellrenderericon.h"
18 #include "collect-dlg.h"
19 #include "collect-io.h"
26 #include "layout_image.h"
30 #include "ui_fileops.h"
32 #include "ui_tree_edit.h"
33 #include "uri_utils.h"
35 #include "icons/marker.xpm"
36 #define MARKER_WIDTH 26
37 #define MARKER_HEIGHT 32
39 #include <gdk/gdkkeysyms.h> /* for keyboard values */
41 /* between these, the icon width is increased by thumb_max_width / 2 */
42 #define THUMB_MIN_ICON_WIDTH 128
43 #define THUMB_MAX_ICON_WIDTH 150
45 #define COLLECT_TABLE_MAX_COLUMNS 32
46 #define THUMB_BORDER_PADDING 2
48 #define COLLECT_TABLE_TIP_DELAY 500
49 #define COLLECT_TABLE_TIP_DELAY_PATH (COLLECT_TABLE_TIP_DELAY * 1.7)
53 CTABLE_COLUMN_POINTER = 0,
59 SELECTION_SELECTED = 1 << 0,
60 SELECTION_PRELIGHT = 1 << 1,
61 SELECTION_FOCUS = 1 << 2
65 #define INFO_SELECTED(x) (x->flag_mask & SELECTION_SELECTED)
68 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force);
72 *-------------------------------------------------------------------
74 *-------------------------------------------------------------------
77 static gboolean collection_table_find_position(CollectTable *ct, CollectInfo *info, gint *row, gint *col)
81 n = g_list_index(ct->cd->list, info);
83 if (n < 0) return FALSE;
85 *row = n / ct->columns;
86 *col = n - (*row * ct->columns);
91 static gboolean collection_table_find_iter(CollectTable *ct, CollectInfo *info, GtkTreeIter *iter, gint *column)
96 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
97 if (!collection_table_find_position(ct, info, &row, &col)) return FALSE;
98 if (!gtk_tree_model_iter_nth_child(store, iter, NULL, row)) return FALSE;
99 if (column) *column = col;
104 static CollectInfo *collection_table_find_data(CollectTable *ct, gint row, gint col, GtkTreeIter *iter)
109 if (row < 0 || col < 0) return NULL;
111 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
112 if (gtk_tree_model_iter_nth_child(store, &p, NULL, row))
116 gtk_tree_model_get(store, &p, CTABLE_COLUMN_POINTER, &list, -1);
117 if (!list) return NULL;
121 return g_list_nth_data(list, col);
127 static CollectInfo *collection_table_find_data_by_coord(CollectTable *ct, gint x, gint y, GtkTreeIter *iter)
130 GtkTreeViewColumn *column;
136 if (!gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
137 &tpath, &column, NULL, NULL))
140 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
141 gtk_tree_model_get_iter(store, &row, tpath);
142 gtk_tree_path_free(tpath);
144 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
145 if (!list) return NULL;
147 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
148 if (iter) *iter = row;
149 return g_list_nth_data(list, n);
152 static guint collection_table_list_count(CollectTable *ct, gint64 *bytes)
162 CollectInfo *ci = work->data;
170 return g_list_length(ct->cd->list);
173 static guint collection_table_selection_count(CollectTable *ct, gint64 *bytes)
180 work = ct->selection;
183 CollectInfo *ci = work->data;
191 return g_list_length(ct->selection);
194 static void collection_table_update_status(CollectTable *ct)
202 if (!ct->status_label) return;
204 n = collection_table_list_count(ct, &n_bytes);
205 s = collection_table_selection_count(ct, &s_bytes);
209 gchar *b = text_from_size_abrev(n_bytes);
210 gchar *sb = text_from_size_abrev(s_bytes);
211 buf = g_strdup_printf(_("%s, %d images (%s, %d)"), b, n, sb, s);
217 gchar *b = text_from_size_abrev(n_bytes);
218 buf = g_strdup_printf(_("%s, %d images"), b, n);
223 buf = g_strdup(_("Empty"));
226 gtk_label_set_text(GTK_LABEL(ct->status_label), buf);
230 static void collection_table_update_extras(CollectTable *ct, gboolean loading, gdouble value)
234 if (!ct->extra_label) return;
237 text = _("Loading thumbs...");
241 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(ct->extra_label), value);
242 gtk_progress_bar_set_text(GTK_PROGRESS_BAR(ct->extra_label), text);
245 static void collection_table_toggle_filenames(CollectTable *ct)
247 GtkAllocation allocation;
248 ct->show_text = !ct->show_text;
249 options->show_icon_names = ct->show_text;
251 gtk_widget_get_allocation(ct->listview, &allocation);
252 collection_table_populate_at_new_size(ct, allocation.width, allocation.height, TRUE);
255 static gint collection_table_get_icon_width(CollectTable *ct)
259 if (!ct->show_text) return options->thumbnails.max_width;
261 width = options->thumbnails.max_width + options->thumbnails.max_width / 2;
262 if (width < THUMB_MIN_ICON_WIDTH) width = THUMB_MIN_ICON_WIDTH;
263 if (width > THUMB_MAX_ICON_WIDTH) width = options->thumbnails.max_width;
269 *-------------------------------------------------------------------
271 *-------------------------------------------------------------------
274 static void collection_table_selection_set(CollectTable *ct, CollectInfo *info, SelectionType value, GtkTreeIter *iter)
281 if (info->flag_mask == value) return;
282 info->flag_mask = value;
284 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
287 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
288 if (list) gtk_list_store_set(GTK_LIST_STORE(store), iter, CTABLE_COLUMN_POINTER, list, -1);
294 if (collection_table_find_iter(ct, info, &row, NULL))
296 gtk_tree_model_get(store, &row, CTABLE_COLUMN_POINTER, &list, -1);
297 if (list) gtk_list_store_set(GTK_LIST_STORE(store), &row, CTABLE_COLUMN_POINTER, list, -1);
302 static void collection_table_selection_add(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
306 collection_table_selection_set(ct, info, info->flag_mask | mask, iter);
309 static void collection_table_selection_remove(CollectTable *ct, CollectInfo *info, SelectionType mask, GtkTreeIter *iter)
313 collection_table_selection_set(ct, info, info->flag_mask & ~mask, iter);
316 *-------------------------------------------------------------------
318 *-------------------------------------------------------------------
321 static void collection_table_verify_selections(CollectTable *ct)
325 work = ct->selection;
328 CollectInfo *info = work->data;
330 if (!g_list_find(ct->cd->list, info))
332 ct->selection = g_list_remove(ct->selection, info);
337 void collection_table_select_all(CollectTable *ct)
341 g_list_free(ct->selection);
342 ct->selection = NULL;
347 ct->selection = g_list_append(ct->selection, work->data);
348 collection_table_selection_add(ct, work->data, SELECTION_SELECTED, NULL);
352 collection_table_update_status(ct);
355 void collection_table_unselect_all(CollectTable *ct)
359 work = ct->selection;
362 collection_table_selection_remove(ct, work->data, SELECTION_SELECTED, NULL);
366 g_list_free(ct->selection);
367 ct->selection = NULL;
369 collection_table_update_status(ct);
372 /* Invert the current collection's selection */
373 static void collection_table_select_invert_all(CollectTable *ct)
376 GList *new_selection = NULL;
381 CollectInfo *info = work->data;
383 if (INFO_SELECTED(info))
385 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
389 new_selection = g_list_append(new_selection, info);
390 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
397 g_list_free(ct->selection);
398 ct->selection = new_selection;
400 collection_table_update_status(ct);
403 static void collection_table_select(CollectTable *ct, CollectInfo *info)
405 ct->prev_selection = info;
407 if (!info || INFO_SELECTED(info)) return;
409 ct->selection = g_list_append(ct->selection, info);
410 collection_table_selection_add(ct, info, SELECTION_SELECTED, NULL);
412 collection_table_update_status(ct);
415 static void collection_table_unselect(CollectTable *ct, CollectInfo *info)
417 ct->prev_selection = info;
419 if (!info || !INFO_SELECTED(info) ) return;
421 ct->selection = g_list_remove(ct->selection, info);
422 collection_table_selection_remove(ct, info, SELECTION_SELECTED, NULL);
424 collection_table_update_status(ct);
427 static void collection_table_select_util(CollectTable *ct, CollectInfo *info, gboolean select)
431 collection_table_select(ct, info);
435 collection_table_unselect(ct, info);
439 static void collection_table_select_region_util(CollectTable *ct, CollectInfo *start, CollectInfo *end, gboolean select)
446 if (!collection_table_find_position(ct, start, &row1, &col1) ||
447 !collection_table_find_position(ct, end, &row2, &col2) ) return;
449 ct->prev_selection = end;
451 if (!options->collections.rectangular_selection)
456 if (g_list_index(ct->cd->list, start) > g_list_index(ct->cd->list, end))
463 work = g_list_find(ct->cd->list, start);
467 collection_table_select_util(ct, info, select);
469 if (work->data != end)
490 DEBUG_1("table: %d x %d to %d x %d", row1, col1, row2, col2);
492 for (i = row1; i <= row2; i++)
494 for (j = col1; j <= col2; j++)
496 CollectInfo *info = collection_table_find_data(ct, i, j, NULL);
497 if (info) collection_table_select_util(ct, info, select);
502 GList *collection_table_selection_get_list(CollectTable *ct)
504 return collection_list_to_filelist(ct->selection);
507 static GList *collection_table_get_list(CollectTable *ct)
509 return collection_list_to_filelist(ct->cd->list);
513 *-------------------------------------------------------------------
514 * tooltip type window
515 *-------------------------------------------------------------------
518 static void tip_show(CollectTable *ct)
523 if (ct->tip_window) return;
525 gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
527 ct->tip_info = collection_table_find_data_by_coord(ct, x, y, NULL);
528 if (!ct->tip_info) return;
530 ct->tip_window = gtk_window_new(GTK_WINDOW_POPUP);
531 gtk_window_set_resizable(GTK_WINDOW(ct->tip_window), FALSE);
532 gtk_container_set_border_width(GTK_CONTAINER(ct->tip_window), 2);
534 label = gtk_label_new(ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
536 g_object_set_data(G_OBJECT(ct->tip_window), "tip_label", label);
537 gtk_container_add(GTK_CONTAINER(ct->tip_window), label);
538 gtk_widget_show(label);
540 gdk_window_get_pointer(NULL, &x, &y, NULL);
542 #if GTK_CHECK_VERSION(2,20,0)
543 if (!gtk_widget_get_realized(ct->tip_window)) gtk_widget_realize(ct->tip_window);
545 if (!GTK_WIDGET_REALIZED(ct->tip_window)) gtk_widget_realize(ct->tip_window);
547 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
548 gtk_widget_show(ct->tip_window);
551 static void tip_hide(CollectTable *ct)
553 if (ct->tip_window) gtk_widget_destroy(ct->tip_window);
554 ct->tip_window = NULL;
557 static gboolean tip_schedule_cb(gpointer data)
559 CollectTable *ct = data;
561 if (!ct->tip_delay_id) return FALSE;
565 ct->tip_delay_id = 0;
569 static void tip_schedule(CollectTable *ct)
573 if (ct->tip_delay_id)
575 g_source_remove(ct->tip_delay_id);
576 ct->tip_delay_id = 0;
579 ct->tip_delay_id = g_timeout_add(ct->show_text ? COLLECT_TABLE_TIP_DELAY_PATH : COLLECT_TABLE_TIP_DELAY, tip_schedule_cb, ct);
582 static void tip_unschedule(CollectTable *ct)
586 if (ct->tip_delay_id)
588 g_source_remove(ct->tip_delay_id);
589 ct->tip_delay_id = 0;
593 static void tip_update(CollectTable *ct, CollectInfo *info)
601 gdk_window_get_pointer(NULL, &x, &y, NULL);
602 gtk_window_move(GTK_WINDOW(ct->tip_window), x + 16, y + 16);
604 if (info != ct->tip_info)
615 label = g_object_get_data(G_OBJECT(ct->tip_window), "tip_label");
616 gtk_label_set_text(GTK_LABEL(label), ct->show_text ? ct->tip_info->fd->path : ct->tip_info->fd->name);
622 *-------------------------------------------------------------------
624 *-------------------------------------------------------------------
627 static void collection_table_popup_save_as_cb(GtkWidget *widget, gpointer data)
629 CollectTable *ct = data;
631 collection_dialog_save_as(NULL, ct->cd);
634 static void collection_table_popup_save_cb(GtkWidget *widget, gpointer data)
636 CollectTable *ct = data;
640 collection_table_popup_save_as_cb(widget, data);
644 if (!collection_save(ct->cd, ct->cd->path))
646 log_printf("failed saving to collection path: %s\n", ct->cd->path);
650 static GList *collection_table_popup_file_list(CollectTable *ct)
652 if (!ct->click_info) return NULL;
654 if (INFO_SELECTED(ct->click_info))
656 return collection_table_selection_get_list(ct);
659 return g_list_append(NULL, file_data_ref(ct->click_info->fd));
662 static void collection_table_popup_edit_cb(GtkWidget *widget, gpointer data)
665 const gchar *key = data;
667 ct = submenu_item_get_data(widget);
671 file_util_start_editor_from_filelist(key, collection_table_popup_file_list(ct), NULL, ct->listview);
674 static void collection_table_popup_copy_cb(GtkWidget *widget, gpointer data)
676 CollectTable *ct = data;
678 file_util_copy(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
681 static void collection_table_popup_move_cb(GtkWidget *widget, gpointer data)
683 CollectTable *ct = data;
685 file_util_move(NULL, collection_table_popup_file_list(ct), NULL, ct->listview);
688 static void collection_table_popup_rename_cb(GtkWidget *widget, gpointer data)
690 CollectTable *ct = data;
692 file_util_rename(NULL, collection_table_popup_file_list(ct), ct->listview);
695 static void collection_table_popup_delete_cb(GtkWidget *widget, gpointer data)
697 CollectTable *ct = data;
699 file_util_delete(NULL, collection_table_popup_file_list(ct), ct->listview);
702 static void collection_table_popup_copy_path_cb(GtkWidget *widget, gpointer data)
704 CollectTable *ct = data;
706 file_util_copy_path_list_to_clipboard(collection_table_popup_file_list(ct));
709 static void collection_table_popup_sort_cb(GtkWidget *widget, gpointer data)
714 ct = submenu_item_get_data(widget);
718 type = (SortType)GPOINTER_TO_INT(data);
720 collection_set_sort_method(ct->cd, type);
723 static void collection_table_popup_randomize_cb(GtkWidget *widget, gpointer data)
727 ct = submenu_item_get_data(widget);
731 collection_randomize(ct->cd);
734 static void collection_table_popup_view_new_cb(GtkWidget *widget, gpointer data)
736 CollectTable *ct = data;
738 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
740 view_window_new_from_collection(ct->cd, ct->click_info);
744 static void collection_table_popup_view_cb(GtkWidget *widget, gpointer data)
746 CollectTable *ct = data;
748 if (ct->click_info && g_list_find(ct->cd->list, ct->click_info))
750 layout_image_set_collection(NULL, ct->cd, ct->click_info);
754 static void collection_table_popup_selectall_cb(GtkWidget *widget, gpointer data)
756 CollectTable *ct = data;
758 collection_table_select_all(ct);
759 ct->prev_selection= ct->click_info;
762 static void collection_table_popup_unselectall_cb(GtkWidget *widget, gpointer data)
764 CollectTable *ct = data;
766 collection_table_unselect_all(ct);
767 ct->prev_selection= ct->click_info;
770 static void collection_table_popup_select_invert_cb(GtkWidget *widget, gpointer data)
772 CollectTable *ct = data;
774 collection_table_select_invert_all(ct);
775 ct->prev_selection= ct->click_info;
778 static void collection_table_popup_remove_cb(GtkWidget *widget, gpointer data)
780 CollectTable *ct = data;
783 if (!ct->click_info) return;
785 if (INFO_SELECTED(ct->click_info))
787 list = g_list_copy(ct->selection);
791 list = g_list_append(NULL, ct->click_info);
794 collection_remove_by_info_list(ct->cd, list);
798 static void collection_table_popup_add_filelist_cb(GtkWidget *widget, gpointer data)
800 CollectTable *ct = data;
803 list = layout_list(NULL);
807 collection_table_add_filelist(ct, list);
812 static void collection_table_popup_add_collection_cb(GtkWidget *widget, gpointer data)
814 CollectTable *ct = data;
816 collection_dialog_append(NULL, ct->cd);
819 static void collection_table_popup_find_dupes_cb(GtkWidget *widget, gpointer data)
821 CollectTable *ct = data;
824 dw = dupe_window_new(DUPE_MATCH_NAME);
825 dupe_window_add_collection(dw, ct->cd);
828 static void collection_table_popup_print_cb(GtkWidget *widget, gpointer data)
830 CollectTable *ct = data;
833 fd = (ct->click_info) ? ct->click_info->fd : NULL;
835 print_window_new(fd, collection_table_selection_get_list(ct), collection_table_get_list(ct), ct->listview);
838 static void collection_table_popup_show_names_cb(GtkWidget *widget, gpointer data)
840 CollectTable *ct = data;
842 collection_table_toggle_filenames(ct);
845 static void collection_table_popup_destroy_cb(GtkWidget *widget, gpointer data)
847 CollectTable *ct = data;
849 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
850 ct->click_info = NULL;
853 filelist_free(ct->drop_list);
854 ct->drop_list = NULL;
855 ct->drop_info = NULL;
857 filelist_free(ct->editmenu_fd_list);
858 ct->editmenu_fd_list = NULL;
861 static GtkWidget *collection_table_popup_menu(CollectTable *ct, gboolean over_icon)
867 menu = popup_menu_short_lived();
869 g_signal_connect(G_OBJECT(menu), "destroy",
870 G_CALLBACK(collection_table_popup_destroy_cb), ct);
872 menu_item_add_sensitive(menu, _("_View"), over_icon,
873 G_CALLBACK(collection_table_popup_view_cb), ct);
874 menu_item_add_stock_sensitive(menu, _("View in _new window"), GTK_STOCK_NEW, over_icon,
875 G_CALLBACK(collection_table_popup_view_new_cb), ct);
876 menu_item_add_divider(menu);
877 menu_item_add_stock_sensitive(menu, _("Rem_ove"), GTK_STOCK_REMOVE, over_icon,
878 G_CALLBACK(collection_table_popup_remove_cb), ct);
880 menu_item_add_stock(menu, _("Append from file list"), GTK_STOCK_ADD,
881 G_CALLBACK(collection_table_popup_add_filelist_cb), ct);
882 menu_item_add_stock(menu, _("Append from collection..."), GTK_STOCK_OPEN,
883 G_CALLBACK(collection_table_popup_add_collection_cb), ct);
884 menu_item_add_divider(menu);
886 item = menu_item_add(menu, _("_Selection"), NULL, NULL);
887 submenu = gtk_menu_new();
888 menu_item_add(submenu, _("Select all"),
889 G_CALLBACK(collection_table_popup_selectall_cb), ct);
890 menu_item_add(submenu, _("Select none"),
891 G_CALLBACK(collection_table_popup_unselectall_cb), ct);
892 menu_item_add(submenu, _("Invert selection"),
893 G_CALLBACK(collection_table_popup_select_invert_cb), ct);
894 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
895 menu_item_add_divider(menu);
898 ct->editmenu_fd_list = collection_table_selection_get_list(ct);
899 submenu_add_edit(menu, &item,
900 G_CALLBACK(collection_table_popup_edit_cb), ct, ct->editmenu_fd_list);
901 gtk_widget_set_sensitive(item, over_icon);
903 menu_item_add_divider(menu);
904 menu_item_add_stock_sensitive(menu, _("_Copy..."), GTK_STOCK_COPY, over_icon,
905 G_CALLBACK(collection_table_popup_copy_cb), ct);
906 menu_item_add_sensitive(menu, _("_Move..."), over_icon,
907 G_CALLBACK(collection_table_popup_move_cb), ct);
908 menu_item_add_sensitive(menu, _("_Rename..."), over_icon,
909 G_CALLBACK(collection_table_popup_rename_cb), ct);
910 menu_item_add_stock_sensitive(menu, _("_Delete..."), GTK_STOCK_DELETE, over_icon,
911 G_CALLBACK(collection_table_popup_delete_cb), ct);
912 menu_item_add_sensitive(menu, _("_Copy path"), over_icon,
913 G_CALLBACK(collection_table_popup_copy_path_cb), ct);
914 menu_item_add_divider(menu);
916 submenu = submenu_add_sort(NULL, G_CALLBACK(collection_table_popup_sort_cb), ct, FALSE, TRUE, FALSE, 0);
917 menu_item_add_divider(submenu);
918 menu_item_add(submenu, _("Randomize"),
919 G_CALLBACK(collection_table_popup_randomize_cb), ct);
920 item = menu_item_add(menu, _("_Sort"), NULL, NULL);
921 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), submenu);
923 menu_item_add_check(menu, _("Show filename _text"), ct->show_text,
924 G_CALLBACK(collection_table_popup_show_names_cb), ct);
925 menu_item_add_divider(menu);
926 menu_item_add_stock(menu, _("_Save collection"), GTK_STOCK_SAVE,
927 G_CALLBACK(collection_table_popup_save_cb), ct);
928 menu_item_add_stock(menu, _("Save collection _as..."), GTK_STOCK_SAVE_AS,
929 G_CALLBACK(collection_table_popup_save_as_cb), ct);
930 menu_item_add_divider(menu);
931 menu_item_add_stock(menu, _("_Find duplicates..."), GTK_STOCK_FIND,
932 G_CALLBACK(collection_table_popup_find_dupes_cb), ct);
933 menu_item_add_stock_sensitive(menu, _("Print..."), GTK_STOCK_PRINT, over_icon,
934 G_CALLBACK(collection_table_popup_print_cb), ct);
939 *-------------------------------------------------------------------
941 *-------------------------------------------------------------------
944 static void collection_table_set_focus(CollectTable *ct, CollectInfo *info)
949 if (g_list_find(ct->cd->list, ct->focus_info))
951 if (info == ct->focus_info)
953 /* ensure focus row col are correct */
954 collection_table_find_position(ct, ct->focus_info,
955 &ct->focus_row, &ct->focus_column);
958 collection_table_selection_remove(ct, ct->focus_info, SELECTION_FOCUS, NULL);
961 if (!collection_table_find_position(ct, info, &row, &col))
963 ct->focus_info = NULL;
965 ct->focus_column = -1;
969 ct->focus_info = info;
971 ct->focus_column = col;
972 collection_table_selection_add(ct, ct->focus_info, SELECTION_FOCUS, NULL);
974 if (collection_table_find_iter(ct, ct->focus_info, &iter, NULL))
977 GtkTreeViewColumn *column;
980 tree_view_row_make_visible(GTK_TREE_VIEW(ct->listview), &iter, FALSE);
982 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
983 tpath = gtk_tree_model_get_path(store, &iter);
984 /* focus is set to an extra column with 0 width to hide focus, we draw it ourself */
985 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), COLLECT_TABLE_MAX_COLUMNS);
986 gtk_tree_view_set_cursor(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE);
987 gtk_tree_path_free(tpath);
991 static void collection_table_move_focus(CollectTable *ct, gint row, gint col, gboolean relative)
998 new_row = ct->focus_row;
999 new_col = ct->focus_column;
1002 if (new_row < 0) new_row = 0;
1003 if (new_row >= ct->rows) new_row = ct->rows - 1;
1023 new_col = ct->columns - 1;
1030 if (new_col >= ct->columns)
1032 if (new_row < ct->rows - 1)
1039 new_col = ct->columns - 1;
1049 if (new_row >= ct->rows)
1052 new_row = ct->rows - 1;
1055 new_col = ct->columns - 1;
1057 if (new_col >= ct->columns) new_col = ct->columns - 1;
1060 if (new_row == ct->rows - 1)
1064 /* if we moved beyond the last image, go to the last image */
1066 l = g_list_length(ct->cd->list);
1067 if (ct->rows > 1) l -= (ct->rows - 1) * ct->columns;
1068 if (new_col >= l) new_col = l - 1;
1071 if (new_row == -1 || new_col == -1)
1073 if (!ct->cd->list) return;
1074 new_row = new_col = 0;
1077 collection_table_set_focus(ct, collection_table_find_data(ct, new_row, new_col, NULL));
1080 static void collection_table_update_focus(CollectTable *ct)
1085 if (ct->focus_info && collection_table_find_position(ct, ct->focus_info, &new_row, &new_col))
1087 /* first find the old focus, if it exists and is valid */
1091 /* (try to) stay where we were */
1092 new_row = ct->focus_row;
1093 new_col = ct->focus_column;
1096 collection_table_move_focus(ct, new_row, new_col, FALSE);
1099 /* used to figure the page up/down distances */
1100 static gint page_height(CollectTable *ct)
1107 adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1108 page_size = (gint)gtk_adjustment_get_page_increment(adj);
1110 row_height = options->thumbnails.max_height + THUMB_BORDER_PADDING * 2;
1111 if (ct->show_text) row_height += options->thumbnails.max_height / 3;
1113 ret = page_size / row_height;
1114 if (ret < 1) ret = 1;
1119 static void collection_table_menu_pos_cb(GtkMenu *menu, gint *x, gint *y, gboolean *push_in, gpointer data)
1121 CollectTable *ct = data;
1122 GtkTreeModel *store;
1128 if (!collection_table_find_iter(ct, ct->click_info, &iter, &column)) return;
1129 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1130 tpath = gtk_tree_model_get_path(store, &iter);
1131 tree_view_get_cell_clamped(GTK_TREE_VIEW(ct->listview), tpath, column, FALSE, x, y, &cw, &ch);
1132 gtk_tree_path_free(tpath);
1134 popup_menu_position_clamp(menu, x, y, 0);
1137 static gboolean collection_table_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1139 CollectTable *ct = data;
1143 gboolean stop_signal = TRUE;
1145 switch (event->keyval)
1147 case GDK_Left: case GDK_KP_Left:
1150 case GDK_Right: case GDK_KP_Right:
1153 case GDK_Up: case GDK_KP_Up:
1156 case GDK_Down: case GDK_KP_Down:
1159 case GDK_Page_Up: case GDK_KP_Page_Up:
1160 focus_row = -page_height(ct);
1162 case GDK_Page_Down: case GDK_KP_Page_Down:
1163 focus_row = page_height(ct);
1165 case GDK_Home: case GDK_KP_Home:
1166 focus_row = -ct->focus_row;
1167 focus_col = -ct->focus_column;
1169 case GDK_End: case GDK_KP_End:
1170 focus_row = ct->rows - 1 - ct->focus_row;
1171 focus_col = ct->columns - 1 - ct->focus_column;
1174 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1177 ct->click_info = info;
1178 if (event->state & GDK_CONTROL_MASK)
1180 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1184 collection_table_unselect_all(ct);
1185 collection_table_select(ct, info);
1190 if (event->state & GDK_CONTROL_MASK) collection_table_toggle_filenames(ct);
1194 info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1195 ct->click_info = info;
1197 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1200 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1201 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, collection_table_menu_pos_cb, ct, 0, GDK_CURRENT_TIME);
1204 stop_signal = FALSE;
1208 if (focus_row != 0 || focus_col != 0)
1210 CollectInfo *new_info;
1211 CollectInfo *old_info;
1213 old_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1214 collection_table_move_focus(ct, focus_row, focus_col, TRUE);
1215 new_info = collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
1217 if (new_info != old_info)
1219 if (event->state & GDK_SHIFT_MASK)
1221 if (!options->collections.rectangular_selection)
1223 collection_table_select_region_util(ct, old_info, new_info, FALSE);
1227 collection_table_select_region_util(ct, ct->click_info, old_info, FALSE);
1229 collection_table_select_region_util(ct, ct->click_info, new_info, TRUE);
1231 else if (event->state & GDK_CONTROL_MASK)
1233 ct->click_info = new_info;
1237 ct->click_info = new_info;
1238 collection_table_unselect_all(ct);
1239 collection_table_select(ct, new_info);
1247 g_signal_stop_emission_by_name(GTK_OBJECT(widget), "key_press_event");
1256 *-------------------------------------------------------------------
1258 *-------------------------------------------------------------------
1261 static CollectInfo *collection_table_insert_find(CollectTable *ct, CollectInfo *source, gboolean *after, GdkRectangle *cell,
1262 gboolean use_coord, gint x, gint y)
1264 CollectInfo *info = NULL;
1265 GtkTreeModel *store;
1268 GtkTreeViewColumn *column;
1270 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1272 if (!use_coord) gdk_window_get_pointer(gtk_widget_get_window(ct->listview), &x, &y, NULL);
1277 if (collection_table_find_iter(ct, source, &iter, &col))
1279 tpath = gtk_tree_model_get_path(store, &iter);
1280 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1281 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1282 gtk_tree_path_free(tpath);
1285 *after = !!(x > cell->x + (cell->width / 2));
1290 if (gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(ct->listview), x, y,
1291 &tpath, &column, NULL, NULL))
1296 gtk_tree_model_get_iter(store, &iter, tpath);
1297 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1299 n = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(column), "column_number"));
1300 info = g_list_nth_data(list, n);
1304 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1305 *after = !!(x > cell->x + (cell->width / 2));
1308 gtk_tree_path_free(tpath);
1315 work = g_list_last(ct->cd->list);
1323 if (collection_table_find_iter(ct, info, &iter, &col))
1325 tpath = gtk_tree_model_get_path(store, &iter);
1326 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), col);
1327 gtk_tree_view_get_background_area(GTK_TREE_VIEW(ct->listview), tpath, column, cell);
1328 gtk_tree_path_free(tpath);
1336 static CollectInfo *collection_table_insert_point(CollectTable *ct, gint x, gint y)
1340 gboolean after = FALSE;
1342 info = collection_table_insert_find(ct, NULL, &after, &cell, TRUE, x, y);
1348 work = g_list_find(ct->cd->list, info);
1349 if (work && work->next)
1351 info = work->next->data;
1362 static void collection_table_insert_marker(CollectTable *ct, CollectInfo *info, gboolean enable)
1365 gboolean after = FALSE;
1370 if (ct->marker_window) gdk_window_destroy(ct->marker_window);
1371 ct->marker_window = NULL;
1376 info = collection_table_insert_find(ct, info, &after, &cell, FALSE, 0, 0);
1378 /* this setting does not take into account (after), but since it is not really used... */
1379 ct->marker_info = info;
1384 if (!ct->marker_window)
1387 GdkWindowAttr attributes;
1388 gint attributes_mask;
1394 parent = gtk_tree_view_get_bin_window(GTK_TREE_VIEW(ct->listview));
1396 pb = gdk_pixbuf_new_from_xpm_data((const gchar **)marker_xpm);
1397 gdk_pixbuf_render_pixmap_and_mask(pb, &pixmap, &mask, 128);
1400 gdk_pixmap_get_size(pixmap, &w, &h);
1402 attributes.window_type = GDK_WINDOW_CHILD;
1403 attributes.wclass = GDK_INPUT_OUTPUT;
1404 attributes.width = w;
1405 attributes.height = h;
1406 attributes.event_mask = gtk_widget_get_events(ct->listview);
1407 attributes_mask = 0;
1409 ct->marker_window = gdk_window_new(parent, &attributes, attributes_mask);
1410 gdk_window_set_back_pixmap(ct->marker_window, pixmap, FALSE);
1411 gdk_window_shape_combine_mask(ct->marker_window, mask, 0, 0);
1413 g_object_unref(pixmap);
1414 if (mask) g_object_unref(mask);
1422 w = gdk_window_get_width(ct->marker_window);
1423 h = gdk_window_get_height(ct->marker_window);
1431 x = cell.x + cell.width;
1434 y = cell.y + (cell.height / 2) - (h / 2);
1436 gdk_window_move(ct->marker_window, x, y);
1437 gdk_window_clear(ct->marker_window);
1438 if (!gdk_window_is_visible(ct->marker_window)) gdk_window_show(ct->marker_window);
1442 if (gdk_window_is_visible(ct->marker_window)) gdk_window_hide(ct->marker_window);
1447 *-------------------------------------------------------------------
1448 * mouse drag auto-scroll
1449 *-------------------------------------------------------------------
1452 static void collection_table_motion_update(CollectTable *ct, gint x, gint y, gboolean drop_event)
1456 info = collection_table_find_data_by_coord(ct, x, y, NULL);
1461 collection_table_insert_marker(ct, info, TRUE);
1465 tip_update(ct, info);
1469 static gboolean collection_table_auto_scroll_idle_cb(gpointer data)
1471 CollectTable *ct = data;
1476 if (!ct->drop_idle_id) return FALSE;
1478 window = gtk_widget_get_window(ct->listview);
1479 gdk_window_get_pointer(window, &x, &y, NULL);
1480 w = gdk_window_get_width(window);
1481 h = gdk_window_get_height(window);
1482 if (x >= 0 && x < w && y >= 0 && y < h)
1484 collection_table_motion_update(ct, x, y, TRUE);
1487 ct->drop_idle_id = 0;
1491 static gboolean collection_table_auto_scroll_notify_cb(GtkWidget *widget, gint x, gint y, gpointer data)
1493 CollectTable *ct = data;
1495 if (!ct->drop_idle_id)
1497 ct->drop_idle_id = g_idle_add(collection_table_auto_scroll_idle_cb, ct);
1503 static void collection_table_scroll(CollectTable *ct, gboolean scroll)
1507 if (ct->drop_idle_id)
1509 g_source_remove(ct->drop_idle_id);
1510 ct->drop_idle_id = 0;
1512 widget_auto_scroll_stop(ct->listview);
1513 collection_table_insert_marker(ct, NULL, FALSE);
1517 GtkAdjustment *adj = gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(ct->listview));
1518 widget_auto_scroll_start(ct->listview, adj, -1, options->thumbnails.max_height / 2,
1519 collection_table_auto_scroll_notify_cb, ct);
1524 *-------------------------------------------------------------------
1526 *-------------------------------------------------------------------
1529 static gboolean collection_table_motion_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1531 CollectTable *ct = data;
1533 collection_table_motion_update(ct, (gint)bevent->x, (gint)bevent->y, FALSE);
1538 static gboolean collection_table_press_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1540 CollectTable *ct = data;
1546 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1548 ct->click_info = info;
1549 collection_table_selection_add(ct, ct->click_info, SELECTION_PRELIGHT, &iter);
1551 switch (bevent->button)
1553 case MOUSE_BUTTON_LEFT:
1554 if (bevent->type == GDK_2BUTTON_PRESS)
1558 layout_image_set_collection(NULL, ct->cd, info);
1561 #if GTK_CHECK_VERSION(2,20,0)
1562 else if (!gtk_widget_has_focus(ct->listview))
1564 else if (!GTK_WIDGET_HAS_FOCUS(ct->listview))
1567 gtk_widget_grab_focus(ct->listview);
1570 case MOUSE_BUTTON_RIGHT:
1571 ct->popup = collection_table_popup_menu(ct, (info != NULL));
1572 gtk_menu_popup(GTK_MENU(ct->popup), NULL, NULL, NULL, NULL, bevent->button, bevent->time);
1581 static gboolean collection_table_release_cb(GtkWidget *widget, GdkEventButton *bevent, gpointer data)
1583 CollectTable *ct = data;
1585 CollectInfo *info = NULL;
1589 if ((gint)bevent->x != 0 || (gint)bevent->y != 0)
1591 info = collection_table_find_data_by_coord(ct, (gint)bevent->x, (gint)bevent->y, &iter);
1596 collection_table_selection_remove(ct, ct->click_info, SELECTION_PRELIGHT, NULL);
1599 if (bevent->button == MOUSE_BUTTON_LEFT &&
1600 info && ct->click_info == info)
1602 collection_table_set_focus(ct, info);
1604 if (bevent->state & GDK_CONTROL_MASK)
1606 gboolean select = !INFO_SELECTED(info);
1608 if ((bevent->state & GDK_SHIFT_MASK) && ct->prev_selection)
1610 collection_table_select_region_util(ct, ct->prev_selection, info, select);
1614 collection_table_select_util(ct, info, select);
1619 collection_table_unselect_all(ct);
1621 if ((bevent->state & GDK_SHIFT_MASK) &&
1624 collection_table_select_region_util(ct, ct->prev_selection, info, TRUE);
1628 collection_table_select_util(ct, info, TRUE);
1632 else if (bevent->button == MOUSE_BUTTON_MIDDLE &&
1633 info && ct->click_info == info)
1635 collection_table_select_util(ct, info, !INFO_SELECTED(info));
1641 static gboolean collection_table_leave_cb(GtkWidget *widget, GdkEventCrossing *event, gpointer data)
1643 CollectTable *ct = data;
1650 *-------------------------------------------------------------------
1651 * populate, add, insert, etc.
1652 *-------------------------------------------------------------------
1655 static gboolean collection_table_destroy_node_cb(GtkTreeModel *store, GtkTreePath *tpath, GtkTreeIter *iter, gpointer data)
1659 gtk_tree_model_get(store, iter, CTABLE_COLUMN_POINTER, &list, -1);
1665 static void collection_table_clear_store(CollectTable *ct)
1667 GtkTreeModel *store;
1669 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1670 gtk_tree_model_foreach(store, collection_table_destroy_node_cb, NULL);
1672 gtk_list_store_clear(GTK_LIST_STORE(store));
1675 static GList *collection_table_add_row(CollectTable *ct, GtkTreeIter *iter)
1677 GtkListStore *store;
1681 for (i = 0; i < ct->columns; i++) list = g_list_prepend(list, NULL);
1683 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview)));
1684 gtk_list_store_append(store, iter);
1685 gtk_list_store_set(store, iter, CTABLE_COLUMN_POINTER, list, -1);
1690 static void collection_table_populate(CollectTable *ct, gboolean resize)
1695 collection_table_verify_selections(ct);
1697 collection_table_clear_store(ct);
1704 thumb_width = collection_table_get_icon_width(ct);
1706 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
1708 GtkTreeViewColumn *column;
1709 GtkCellRenderer *cell;
1712 column = gtk_tree_view_get_column(GTK_TREE_VIEW(ct->listview), i);
1713 gtk_tree_view_column_set_visible(column, (i < ct->columns));
1714 gtk_tree_view_column_set_fixed_width(column, thumb_width + (THUMB_BORDER_PADDING * 6));
1716 #if GTK_CHECK_VERSION(2,18,0)
1717 list = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(column));
1719 list = gtk_tree_view_column_get_cell_renderers(column);
1721 cell = (list) ? list->data : NULL;
1724 if (cell && GQV_IS_CELL_RENDERER_ICON(cell))
1726 g_object_set(G_OBJECT(cell), "fixed_width", thumb_width,
1727 "fixed_height", options->thumbnails.max_height,
1728 "show_text", ct->show_text, NULL);
1731 #if GTK_CHECK_VERSION(2,20,0)
1732 if (gtk_widget_get_realized(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1734 if (GTK_WIDGET_REALIZED(ct->listview)) gtk_tree_view_columns_autosize(GTK_TREE_VIEW(ct->listview));
1739 work = ct->cd->list;
1747 list = collection_table_add_row(ct, &iter);
1748 while (work && list)
1750 list->data = work->data;
1758 collection_table_update_focus(ct);
1759 collection_table_update_status(ct);
1762 static void collection_table_populate_at_new_size(CollectTable *ct, gint w, gint h, gboolean force)
1767 thumb_width = collection_table_get_icon_width(ct);
1769 new_cols = w / (thumb_width + (THUMB_BORDER_PADDING * 6));
1770 if (new_cols < 1) new_cols = 1;
1772 if (!force && new_cols == ct->columns) return;
1774 ct->columns = new_cols;
1776 collection_table_populate(ct, TRUE);
1778 DEBUG_1("col tab pop cols=%d rows=%d", ct->columns, ct->rows);
1781 static void collection_table_sync(CollectTable *ct)
1783 GtkTreeModel *store;
1788 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1793 work = ct->cd->list;
1799 if (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1801 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1802 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
1806 list = collection_table_add_row(ct, &iter);
1831 while (gtk_tree_model_iter_nth_child(store, &iter, NULL, r))
1835 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1836 gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1842 collection_table_update_focus(ct);
1843 collection_table_update_status(ct);
1846 static gboolean collection_table_sync_idle_cb(gpointer data)
1848 CollectTable *ct = data;
1850 if (!ct->sync_idle_id) return FALSE;
1851 g_source_remove(ct->sync_idle_id);
1852 ct->sync_idle_id = 0;
1854 collection_table_sync(ct);
1858 static void collection_table_sync_idle(CollectTable *ct)
1860 if (!ct->sync_idle_id)
1862 /* high priority, the view needs to be resynced before a redraw
1863 * may contain invalid pointers at this time
1865 ct->sync_idle_id = g_idle_add_full(G_PRIORITY_HIGH, collection_table_sync_idle_cb, ct, NULL);
1869 void collection_table_add_filelist(CollectTable *ct, GList *list)
1878 collection_add(ct->cd, (FileData *)work->data, FALSE);
1883 static void collection_table_insert_filelist(CollectTable *ct, GList *list, CollectInfo *insert_info)
1892 collection_insert(ct->cd, (FileData *)work->data, insert_info, FALSE);
1896 collection_table_sync_idle(ct);
1899 static void collection_table_move_by_info_list(CollectTable *ct, GList *info_list, gint row, gint col)
1902 GList *insert_pos = NULL;
1906 if (!info_list) return;
1908 info = collection_table_find_data(ct, row, col, NULL);
1910 if (!info_list->next && info_list->data == info) return;
1912 if (info) insert_pos = g_list_find(ct->cd->list, info);
1914 /* FIXME: this may get slow for large lists */
1916 while (insert_pos && work)
1918 if (insert_pos->data == work->data)
1920 insert_pos = insert_pos->next;
1932 ct->cd->list = g_list_remove(ct->cd->list, work->data);
1936 /* place them back in */
1937 temp = g_list_copy(info_list);
1941 ct->cd->list = uig_list_insert_list(ct->cd->list, insert_pos, temp);
1945 ct->cd->list = g_list_concat(temp, ct->cd->list);
1949 ct->cd->list = g_list_concat(ct->cd->list, temp);
1952 ct->cd->changed = TRUE;
1954 collection_table_sync_idle(ct);
1959 *-------------------------------------------------------------------
1961 *-------------------------------------------------------------------
1964 void collection_table_file_update(CollectTable *ct, CollectInfo *info)
1972 collection_table_update_extras(ct, FALSE, 0.0);
1976 if (!collection_table_find_position(ct, info, &row, &col)) return;
1978 if (ct->columns != 0 && ct->rows != 0)
1980 value = (gdouble)(row * ct->columns + col) / (ct->columns * ct->rows);
1987 collection_table_update_extras(ct, TRUE, value);
1989 if (collection_table_find_iter(ct, info, &iter, NULL))
1991 GtkTreeModel *store;
1994 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ct->listview));
1995 gtk_tree_model_get(store, &iter, CTABLE_COLUMN_POINTER, &list, -1);
1996 gtk_list_store_set(GTK_LIST_STORE(store), &iter, CTABLE_COLUMN_POINTER, list, -1);
2000 void collection_table_file_add(CollectTable *ct, CollectInfo *info)
2002 collection_table_sync_idle(ct);
2005 void collection_table_file_insert(CollectTable *ct, CollectInfo *ci)
2007 collection_table_sync_idle(ct);
2010 void collection_table_file_remove(CollectTable *ct, CollectInfo *ci)
2012 if (ci && INFO_SELECTED(ci))
2014 ct->selection = g_list_remove(ct->selection, ci);
2017 collection_table_sync_idle(ct);
2020 void collection_table_refresh(CollectTable *ct)
2022 collection_table_populate(ct, FALSE);
2026 *-------------------------------------------------------------------
2028 *-------------------------------------------------------------------
2031 static void collection_table_add_dir_recursive(CollectTable *ct, FileData *dir_fd, gboolean recursive)
2037 if (!filelist_read(dir_fd, &f, recursive ? &d : NULL))
2040 f = filelist_filter(f, FALSE);
2041 d = filelist_filter(d, TRUE);
2043 f = filelist_sort_path(f);
2044 d = filelist_sort_path(d);
2046 collection_table_insert_filelist(ct, f, ct->marker_info);
2048 work = g_list_last(d);
2051 collection_table_add_dir_recursive(ct, (FileData *)work->data, TRUE);
2059 static void confirm_dir_list_do(CollectTable *ct, GList *list, gboolean recursive)
2064 FileData *fd = work->data;
2066 if (isdir(fd->path)) collection_table_add_dir_recursive(ct, fd, recursive);
2068 collection_table_insert_filelist(ct, list, ct->marker_info);
2072 static void confirm_dir_list_add(GtkWidget *widget, gpointer data)
2074 CollectTable *ct = data;
2076 confirm_dir_list_do(ct, ct->drop_list, FALSE);
2079 static void confirm_dir_list_recurse(GtkWidget *widget, gpointer data)
2081 CollectTable *ct = data;
2083 confirm_dir_list_do(ct, ct->drop_list, TRUE);
2086 static void confirm_dir_list_skip(GtkWidget *widget, gpointer data)
2088 CollectTable *ct = data;
2090 collection_table_insert_filelist(ct, ct->drop_list, ct->marker_info);
2093 static GtkWidget *collection_table_drop_menu(CollectTable *ct)
2097 menu = popup_menu_short_lived();
2098 g_signal_connect(G_OBJECT(menu), "destroy",
2099 G_CALLBACK(collection_table_popup_destroy_cb), ct);
2101 menu_item_add_stock(menu, _("Dropped list includes folders."), GTK_STOCK_DND_MULTIPLE, NULL, NULL);
2102 menu_item_add_divider(menu);
2103 menu_item_add_stock(menu, _("_Add contents"), GTK_STOCK_OK,
2104 G_CALLBACK(confirm_dir_list_add), ct);
2105 menu_item_add_stock(menu, _("Add contents _recursive"), GTK_STOCK_ADD,
2106 G_CALLBACK(confirm_dir_list_recurse), ct);
2107 menu_item_add_stock(menu, _("_Skip folders"), GTK_STOCK_REMOVE,
2108 G_CALLBACK(confirm_dir_list_skip), ct);
2109 menu_item_add_divider(menu);
2110 menu_item_add_stock(menu, _("Cancel"), GTK_STOCK_CANCEL, NULL, ct);
2116 *-------------------------------------------------------------------
2118 *-------------------------------------------------------------------
2121 static GtkTargetEntry collection_drag_types[] = {
2122 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2123 { "text/uri-list", 0, TARGET_URI_LIST },
2124 { "text/plain", 0, TARGET_TEXT_PLAIN }
2126 static gint n_collection_drag_types = 3;
2128 static GtkTargetEntry collection_drop_types[] = {
2129 { TARGET_APP_COLLECTION_MEMBER_STRING, 0, TARGET_APP_COLLECTION_MEMBER },
2130 { "text/uri-list", 0, TARGET_URI_LIST }
2132 static gint n_collection_drop_types = 2;
2135 static void collection_table_dnd_get(GtkWidget *widget, GdkDragContext *context,
2136 GtkSelectionData *selection_data, guint info,
2137 guint time, gpointer data)
2139 CollectTable *ct = data;
2142 gchar *uri_text = NULL;
2145 if (!ct->click_info) return;
2147 selected = INFO_SELECTED(ct->click_info);
2151 case TARGET_APP_COLLECTION_MEMBER:
2154 uri_text = collection_info_list_to_dnd_data(ct->cd, ct->selection, &total);
2158 list = g_list_append(NULL, ct->click_info);
2159 uri_text = collection_info_list_to_dnd_data(ct->cd, list, &total);
2163 case TARGET_URI_LIST:
2164 case TARGET_TEXT_PLAIN:
2168 list = collection_table_selection_get_list(ct);
2172 list = g_list_append(NULL, file_data_ref(ct->click_info->fd));
2176 uri_text = uri_text_from_filelist(list, &total, (info == TARGET_TEXT_PLAIN));
2177 filelist_free(list);
2181 gtk_selection_data_set_text(selection_data, uri_text, total);
2186 static void collection_table_dnd_receive(GtkWidget *widget, GdkDragContext *context,
2188 GtkSelectionData *selection_data, guint info,
2189 guint time, gpointer data)
2191 CollectTable *ct = data;
2193 GList *info_list = NULL;
2194 CollectionData *source;
2195 CollectInfo *drop_info;
2198 DEBUG_1("%s", gtk_selection_data_get_data(selection_data));
2200 collection_table_scroll(ct, FALSE);
2201 collection_table_insert_marker(ct, NULL, FALSE);
2203 drop_info = collection_table_insert_point(ct, x, y);
2207 case TARGET_APP_COLLECTION_MEMBER:
2208 source = collection_from_dnd_data((gchar *)gtk_selection_data_get_data(selection_data), &list, &info_list);
2211 if (source == ct->cd)
2216 /* it is a move within a collection */
2217 filelist_free(list);
2222 collection_table_move_by_info_list(ct, info_list, -1, -1);
2224 else if (collection_table_find_position(ct, drop_info, &row, &col))
2226 collection_table_move_by_info_list(ct, info_list, row, col);
2231 /* it is a move/copy across collections */
2232 if (gdk_drag_context_get_selected_action(context) == GDK_ACTION_MOVE)
2234 collection_remove_by_info_list(source, info_list);
2237 g_list_free(info_list);
2240 case TARGET_URI_LIST:
2241 list = uri_filelist_from_text((gchar *)gtk_selection_data_get_data(selection_data), TRUE);
2245 FileData *fd = work->data;
2246 if (isdir(fd->path))
2250 ct->drop_list = list;
2251 ct->drop_info = drop_info;
2252 menu = collection_table_drop_menu(ct);
2253 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, 0, time);
2266 collection_table_insert_filelist(ct, list, drop_info);
2267 filelist_free(list);
2271 static void collection_table_dnd_begin(GtkWidget *widget, GdkDragContext *context, gpointer data)
2273 CollectTable *ct = data;
2275 if (ct->click_info && ct->click_info->pixbuf)
2279 if (INFO_SELECTED(ct->click_info))
2280 items = g_list_length(ct->selection);
2283 dnd_set_drag_icon(widget, context, ct->click_info->pixbuf, items);
2287 static void collection_table_dnd_end(GtkWidget *widget, GdkDragContext *context, gpointer data)
2289 CollectTable *ct = data;
2291 /* apparently a leave event is not generated on a drop */
2294 collection_table_scroll(ct, FALSE);
2297 static gint collection_table_dnd_motion(GtkWidget *widget, GdkDragContext *context,
2298 gint x, gint y, guint time, gpointer data)
2300 CollectTable *ct = data;
2302 collection_table_motion_update(ct, x, y, TRUE);
2303 collection_table_scroll(ct, TRUE);
2308 static void collection_table_dnd_leave(GtkWidget *widget, GdkDragContext *context, guint time, gpointer data)
2310 CollectTable *ct = data;
2312 collection_table_scroll(ct, FALSE);
2315 static void collection_table_dnd_init(CollectTable *ct)
2317 gtk_drag_source_set(ct->listview, GDK_BUTTON1_MASK | GDK_BUTTON2_MASK,
2318 collection_drag_types, n_collection_drag_types,
2319 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_LINK);
2320 g_signal_connect(G_OBJECT(ct->listview), "drag_data_get",
2321 G_CALLBACK(collection_table_dnd_get), ct);
2322 g_signal_connect(G_OBJECT(ct->listview), "drag_begin",
2323 G_CALLBACK(collection_table_dnd_begin), ct);
2324 g_signal_connect(G_OBJECT(ct->listview), "drag_end",
2325 G_CALLBACK(collection_table_dnd_end), ct);
2327 gtk_drag_dest_set(ct->listview,
2328 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_HIGHLIGHT | GTK_DEST_DEFAULT_DROP,
2329 collection_drop_types, n_collection_drop_types,
2330 GDK_ACTION_COPY | GDK_ACTION_MOVE | GDK_ACTION_ASK);
2331 g_signal_connect(G_OBJECT(ct->listview), "drag_motion",
2332 G_CALLBACK(collection_table_dnd_motion), ct);
2333 g_signal_connect(G_OBJECT(ct->listview), "drag_leave",
2334 G_CALLBACK(collection_table_dnd_leave), ct);
2335 g_signal_connect(G_OBJECT(ct->listview), "drag_data_received",
2336 G_CALLBACK(collection_table_dnd_receive), ct);
2340 *-----------------------------------------------------------------------------
2342 *-----------------------------------------------------------------------------
2345 typedef struct _ColumnData ColumnData;
2352 static void collection_table_cell_data_cb(GtkTreeViewColumn *tree_column, GtkCellRenderer *cell,
2353 GtkTreeModel *tree_model, GtkTreeIter *iter, gpointer data)
2355 ColumnData *cd = data;
2365 gtk_tree_model_get(tree_model, iter, CTABLE_COLUMN_POINTER, &list, -1);
2366 info = g_list_nth_data(list, cd->number);
2368 style = gtk_widget_get_style(ct->listview);
2369 if (info && (info->flag_mask & SELECTION_SELECTED) )
2371 memcpy(&color_fg, &style->text[GTK_STATE_SELECTED], sizeof(color_fg));
2372 memcpy(&color_bg, &style->base[GTK_STATE_SELECTED], sizeof(color_bg));
2376 memcpy(&color_fg, &style->text[GTK_STATE_NORMAL], sizeof(color_fg));
2377 memcpy(&color_bg, &style->base[GTK_STATE_NORMAL], sizeof(color_bg));
2380 if (info && (info->flag_mask & SELECTION_PRELIGHT))
2383 shift_color(&color_fg, -1, 0);
2385 shift_color(&color_bg, -1, 0);
2388 if (GQV_IS_CELL_RENDERER_ICON(cell))
2392 g_object_set(cell, "pixbuf", info->pixbuf,
2393 "text", info->fd->name,
2394 "cell-background-gdk", &color_bg,
2395 "cell-background-set", TRUE,
2396 "foreground-gdk", &color_fg,
2397 "foreground-set", TRUE,
2398 "has-focus", (ct->focus_info == info), NULL);
2402 g_object_set(cell, "pixbuf", NULL,
2404 "cell-background-set", FALSE,
2405 "foreground-set", FALSE,
2406 "has-focus", FALSE, NULL);
2411 static void collection_table_append_column(CollectTable *ct, gint n)
2414 GtkTreeViewColumn *column;
2415 GtkCellRenderer *renderer;
2417 column = gtk_tree_view_column_new();
2418 gtk_tree_view_column_set_min_width(column, 0);
2420 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
2421 gtk_tree_view_column_set_alignment(column, 0.5);
2423 renderer = gqv_cell_renderer_icon_new();
2424 gtk_tree_view_column_pack_start(column, renderer, FALSE);
2425 g_object_set(G_OBJECT(renderer), "xpad", THUMB_BORDER_PADDING * 2,
2426 "ypad", THUMB_BORDER_PADDING,
2427 "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE, NULL);
2429 g_object_set_data(G_OBJECT(column), "column_number", GINT_TO_POINTER(n));
2431 cd = g_new0(ColumnData, 1);
2434 gtk_tree_view_column_set_cell_data_func(column, renderer, collection_table_cell_data_cb, cd, g_free);
2436 gtk_tree_view_append_column(GTK_TREE_VIEW(ct->listview), column);
2440 *-------------------------------------------------------------------
2442 *-------------------------------------------------------------------
2445 static void collection_table_destroy(GtkWidget *widget, gpointer data)
2447 CollectTable *ct = data;
2451 g_signal_handlers_disconnect_matched(GTK_OBJECT(ct->popup), G_SIGNAL_MATCH_DATA,
2453 gtk_widget_destroy(ct->popup);
2456 if (ct->sync_idle_id) g_source_remove(ct->sync_idle_id);
2459 collection_table_scroll(ct, FALSE);
2464 static void collection_table_sized(GtkWidget *widget, GtkAllocation *allocation, gpointer data)
2466 CollectTable *ct = data;
2468 collection_table_populate_at_new_size(ct, allocation->width, allocation->height, FALSE);
2471 CollectTable *collection_table_new(CollectionData *cd)
2474 GtkListStore *store;
2475 GtkTreeSelection *selection;
2478 ct = g_new0(CollectTable, 1);
2481 ct->show_text = options->show_icon_names;
2483 ct->scrolled = gtk_scrolled_window_new(NULL, NULL);
2484 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(ct->scrolled), GTK_SHADOW_IN);
2485 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ct->scrolled),
2486 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
2488 store = gtk_list_store_new(1, G_TYPE_POINTER);
2489 ct->listview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
2490 g_object_unref(store);
2492 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ct->listview));
2493 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_NONE);
2495 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(ct->listview), FALSE);
2496 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(ct->listview), FALSE);
2498 for (i = 0; i < COLLECT_TABLE_MAX_COLUMNS; i++)
2500 collection_table_append_column(ct, i);
2503 /* zero width column to hide tree view focus, we draw it ourselves */
2504 collection_table_append_column(ct, i);
2505 /* end column to fill white space */
2506 collection_table_append_column(ct, i);
2508 g_signal_connect(G_OBJECT(ct->listview), "destroy",
2509 G_CALLBACK(collection_table_destroy), ct);
2510 g_signal_connect(G_OBJECT(ct->listview), "size_allocate",
2511 G_CALLBACK(collection_table_sized), ct);
2512 g_signal_connect(G_OBJECT(ct->listview), "key_press_event",
2513 G_CALLBACK(collection_table_press_key_cb), ct);
2515 gtk_container_add(GTK_CONTAINER(ct->scrolled), ct->listview);
2516 gtk_widget_show(ct->listview);
2518 collection_table_dnd_init(ct);
2520 gtk_widget_set_events(ct->listview, GDK_POINTER_MOTION_MASK | GDK_BUTTON_RELEASE_MASK |
2521 GDK_BUTTON_PRESS_MASK | GDK_LEAVE_NOTIFY_MASK);
2522 g_signal_connect(G_OBJECT(ct->listview),"button_press_event",
2523 G_CALLBACK(collection_table_press_cb), ct);
2524 g_signal_connect(G_OBJECT(ct->listview),"button_release_event",
2525 G_CALLBACK(collection_table_release_cb), ct);
2526 g_signal_connect(G_OBJECT(ct->listview),"motion_notify_event",
2527 G_CALLBACK(collection_table_motion_cb), ct);
2528 g_signal_connect(G_OBJECT(ct->listview), "leave_notify_event",
2529 G_CALLBACK(collection_table_leave_cb), ct);
2534 void collection_table_set_labels(CollectTable *ct, GtkWidget *status, GtkWidget *extra)
2536 ct->status_label = status;
2537 ct->extra_label = extra;
2538 collection_table_update_status(ct);
2539 collection_table_update_extras(ct, FALSE, 0.0);
2542 CollectInfo *collection_table_get_focus_info(CollectTable *ct)
2544 return collection_table_find_data(ct, ct->focus_row, ct->focus_column, NULL);
2547 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */