diff --git a/gui/collection.c b/gui/collection.c index f18d7b0e..6f3dfa2d 100644 --- a/gui/collection.c +++ b/gui/collection.c @@ -1,6 +1,9 @@ /* * Copyright 2015 (c) Anna Schumaker. */ +#include +#include +#include #include #include @@ -15,6 +18,61 @@ enum collection_sidebar_columns { static GtkTreeModel *c_model; +static void __collection_set_header(GtkTreeIter *iter, const gchar *image, + GtkIconSize size, const gchar *text) +{ + gtk_tree_store_set(GTK_TREE_STORE(c_model), iter, C_SB_IMAGE, image, + C_SB_IMAGE_SZ, size, C_SB_PATH, text, -1); +} + +static void __collection_set_library(GtkTreeIter *iter, struct library *library) +{ + gtk_tree_store_set(GTK_TREE_STORE(c_model), iter, + C_SB_ENABLED, library->li_enabled, + C_SB_SHOW_ENABLED, true, + C_SB_PATH, library->li_path, + C_SB_LIBRARY, library, -1); +} + +static struct library *__collection_get_library(GtkTreeIter *iter) +{ + struct library *library; + + gtk_tree_model_get(c_model, iter, C_SB_LIBRARY, &library, -1); + return library; +} + +void __collection_add(GtkButton *button, GtkFileChooser *chooser) +{ + gchar *filename = gtk_file_chooser_get_filename(chooser); + GtkTreeIter iter, last; + + gtk_tree_model_get_iter_first(c_model, &iter); + gtk_tree_model_iter_nth_child(c_model, &last, &iter, 0); + while (__collection_get_library(&last) != NULL) + gtk_tree_model_iter_next(c_model, &last); + + gtk_tree_store_insert_before(GTK_TREE_STORE(c_model), &iter, NULL, &last); + __collection_set_library(&iter, collection_add(filename)); + + gui_collection_idle_enable(); + g_free(filename); +} + +void __collection_selection_changed(GtkTreeSelection *selection, + gpointer data) +{ + GtkNotebook *notebook = GTK_NOTEBOOK(gui_builder_widget("o_notebook")); + GtkTreeIter iter; + + if (gtk_tree_selection_get_selected(selection, &c_model, &iter)) { + if (!__collection_get_library(&iter)) { + gtk_notebook_set_current_page(notebook, + tempq_count() + 3); + } + } +} + static gboolean __collection_select(GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean selected, gpointer data) @@ -22,23 +80,48 @@ static gboolean __collection_select(GtkTreeSelection *selection, return gtk_tree_path_get_depth(path) != 1; } +static gboolean __collection_on_idle(gpointer data) +{ + GtkProgressBar *progress = GTK_PROGRESS_BAR(data); + + if (idle_run_task()) { + gtk_progress_bar_set_fraction(progress, idle_progress()); + return G_SOURCE_CONTINUE; + } else { + gtk_widget_hide(GTK_WIDGET(progress)); + return G_SOURCE_REMOVE; + } +} + void gui_collection_init() { + GtkTreeIter parent, iter; GtkTreeView *treeview; - GtkTreeIter parent; c_model = GTK_TREE_MODEL(gui_builder_object("o_collection_store")); /* Add "Collection" header. */ gtk_tree_store_insert(GTK_TREE_STORE(c_model), &parent, NULL, -1); - gtk_tree_store_set(GTK_TREE_STORE(c_model), &parent, - C_SB_IMAGE, "system-file-manager", - C_SB_IMAGE_SZ, GTK_ICON_SIZE_LARGE_TOOLBAR, - C_SB_PATH, "Collection", - -1); + __collection_set_header(&parent, "system-file-manager", + GTK_ICON_SIZE_LARGE_TOOLBAR, + "Collection"); + + /* Add "Add new Library" entry. */ + gtk_tree_store_insert(GTK_TREE_STORE(c_model), &iter, &parent, -1); + __collection_set_header(&iter, "folder-new", GTK_ICON_SIZE_MENU, + "<Add new path>"); treeview = GTK_TREE_VIEW(gui_builder_widget("o_collection_view")); + gtk_tree_view_expand_all(treeview); gtk_tree_selection_set_select_function( gtk_tree_view_get_selection(treeview), __collection_select, NULL, NULL); } + +void gui_collection_idle_enable() +{ + GtkWidget *progress = gui_builder_widget("o_idle_progress"); + + gtk_widget_show(progress); + g_idle_add(__collection_on_idle, progress); +} diff --git a/include/gui/collection.h b/include/gui/collection.h index 8f92d948..5c0af9ac 100644 --- a/include/gui/collection.h +++ b/include/gui/collection.h @@ -7,4 +7,7 @@ /* Called to initialize the GUI collection code. */ void gui_collection_init(); +/* Called to enable processing idle queue tasks. */ +void gui_collection_idle_enable(); + #endif /* OCARINA_GUI_COLLECTION_H */ diff --git a/include/tests/gui.h b/include/tests/gui.h index ea22c924..b4f9affc 100644 --- a/include/tests/gui.h +++ b/include/tests/gui.h @@ -15,4 +15,9 @@ void __audio_seek() {} void __sidebar_resize() {} #endif /* TEST_NEED_SIDEBAR */ +#ifdef TEST_NEED_COLLECTION +void __collection_add() {} +void __collection_selection_changed() {} +#endif /* TEST_NEED_COLLECTION */ + #endif /* TESTS_GUI_H */ diff --git a/share/ocarina/ocarina6.glade b/share/ocarina/ocarina6.glade index a476de0e..ac7257d7 100644 --- a/share/ocarina/ocarina6.glade +++ b/share/ocarina/ocarina6.glade @@ -24,6 +24,14 @@ inode/directory + + True + False + 5 + 5 + dialog-ok + 3 + True False @@ -621,7 +629,9 @@ 10 True - + + + @@ -698,7 +708,6 @@ False 5 5 - 5 select-folder False filefilter1 @@ -886,11 +895,45 @@ False True - 5 1 + + + True + False + 0.96999999999999997 + center + start + + + Add Selected Path + True + True + True + center + image11 + + + + True + True + end + 0 + + + + + False + False + 5 + 2 + + + + False + @@ -1072,7 +1115,6 @@ Manager True False - True False @@ -1125,7 +1167,7 @@ Manager False - True + False 1 4 diff --git a/tests/gui/collection.c b/tests/gui/collection.c index b38e5921..1774f5af 100644 --- a/tests/gui/collection.c +++ b/tests/gui/collection.c @@ -10,16 +10,18 @@ static void test_collection_sidebar() { GtkTreeSelection *selection; + GtkTreeIter iter, child; + GtkNotebook *notebook; GtkTreeView *treeview; GtkTreeModel *model; GtkTreePath *path; - GtkTreeIter iter; int argc = 0; gtk_init(&argc, NULL); gui_builder_init("share/ocarina/ocarina6.glade"); gui_collection_init(); + notebook = GTK_NOTEBOOK(gui_builder_widget("o_notebook")); treeview = GTK_TREE_VIEW(gui_builder_widget("o_collection_view")); selection = gtk_tree_view_get_selection(treeview); model = GTK_TREE_MODEL(gui_builder_object("o_collection_store")); @@ -28,9 +30,16 @@ static void test_collection_sidebar() path = gtk_tree_model_get_path(model, &iter); gtk_tree_view_set_cursor(treeview, path, NULL, false); gtk_tree_path_free(path); - test_equal(gtk_tree_selection_count_selected_rows(selection), 0); + gtk_tree_model_iter_nth_child(model, &child, &iter, 0); + path = gtk_tree_model_get_path(model, &child); + gtk_tree_view_set_cursor(treeview, path, NULL, false); + gtk_tree_path_free(path); + test_equal(gtk_tree_selection_count_selected_rows(selection), 1); + test_equal(gtk_notebook_get_current_page(notebook), + gtk_notebook_get_n_pages(notebook) - 1); + gui_builder_deinit(); } diff --git a/tests/gui/sidebar.c b/tests/gui/sidebar.c index 6eaa0eaa..b29bdb79 100644 --- a/tests/gui/sidebar.c +++ b/tests/gui/sidebar.c @@ -2,6 +2,7 @@ * Copyright 2015 (c) Anna Schumaker. */ #define TEST_NEED_AUDIO +#define TEST_NEED_COLLECTION #include #include #include diff --git a/tests/gui/window.c b/tests/gui/window.c index b9b23637..9742bcd1 100644 --- a/tests/gui/window.c +++ b/tests/gui/window.c @@ -3,6 +3,7 @@ */ #define TEST_NEED_AUDIO #define TEST_NEED_SIDEBAR +#define TEST_NEED_COLLECTION #include #include #include