From ddbc040a3b64a96d624cf5002f4dbf82e00d9227 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 14 Jan 2016 08:59:26 -0500 Subject: [PATCH] gui/queue: Set state of random and repeat buttons I need to change the state of these toggle buttons whenever the sidebar's current selection changes. Some gtk themes have trouble showing the state of toggle buttons, so I change the sensitivity of both the button and associated image so users know the current state. Signed-off-by: Anna Schumaker --- gui/collection.c | 2 +- gui/playlist.c | 3 +- gui/queue.c | 26 ++++++++++++++++ gui/sidebar.c | 7 +++-- include/gui/queue.h | 3 ++ include/gui/sidebar.h | 2 +- include/tests/gui.h | 5 ++++ share/ocarina/ocarina6.glade | 36 +++++++++++----------- tests/gui/queue.c | 58 +++++++++++++++++++++++++++++------- 9 files changed, 108 insertions(+), 34 deletions(-) diff --git a/gui/collection.c b/gui/collection.c index fa60eb86..b3eb9596 100644 --- a/gui/collection.c +++ b/gui/collection.c @@ -160,7 +160,7 @@ void __collection_selection_changed(GtkTreeSelection *selection, gtk_notebook_set_current_page(notebook, tempq_count() + 3); library = __collection_get_library(&iter); - gui_sidebar_selected(SB_COLLECTION); + gui_sidebar_selected(SB_COLLECTION, NULL); if (library) gtk_file_chooser_set_current_folder(chooser, library->li_path); diff --git a/gui/playlist.c b/gui/playlist.c index 3c768214..7c30203e 100644 --- a/gui/playlist.c +++ b/gui/playlist.c @@ -41,7 +41,8 @@ void __playlist_selection_changed(GtkTreeSelection *selection, gpointer data) gtk_tree_model_get(model, &iter, P_SB_PLAYLIST, &p_cur, -1); gtk_notebook_set_current_page(notebook, tempq_count() + 2); - gui_sidebar_selected(SB_PLAYLIST); + gui_sidebar_selected(SB_PLAYLIST, + gui_queue(playlist_get_queue())); playlist_select(p_cur); } } diff --git a/gui/queue.c b/gui/queue.c index 6e890cbb..2a8bcffd 100644 --- a/gui/queue.c +++ b/gui/queue.c @@ -1,6 +1,7 @@ /* * Copyright 2016 (c) Anna Schumaker. */ +#include #include struct gui_queue *gui_queue_alloc(struct queue *queue, const gchar *text, @@ -24,3 +25,28 @@ void gui_queue_free(struct queue *queue) g_free(gq->gq_text); g_free(gq); } + +void gui_queue_show(struct gui_queue *queue) +{ + GtkButton *random = GTK_BUTTON(gui_builder_widget("o_random")); + GtkButton *repeat = GTK_BUTTON(gui_builder_widget("o_repeat")); + bool has_random = false, has_repeat = false; + + gtk_widget_set_sensitive(GTK_WIDGET(random), gui_queue_can_random(queue)); + gtk_widget_set_sensitive(GTK_WIDGET(repeat), gui_queue_can_repeat(queue)); + + if (queue) { + has_random = queue_has_flag(queue->gq_queue, Q_RANDOM); + has_repeat = queue_has_flag(queue->gq_queue, Q_REPEAT); + } + + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), has_random); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(repeat), has_repeat); + + /* + * Some GTK themes have trouble with toggle buttons, + * so let's help users know what the current state is. + */ + gtk_widget_set_sensitive(gtk_button_get_image(random), has_random); + gtk_widget_set_sensitive(gtk_button_get_image(repeat), has_repeat); +} diff --git a/gui/sidebar.c b/gui/sidebar.c index 1b2bbe65..8b77801a 100644 --- a/gui/sidebar.c +++ b/gui/sidebar.c @@ -108,7 +108,7 @@ void __sidebar_selection_changed(GtkTreeSelection *selection, gpointer data) unsigned int page = gtk_tree_path_get_indices(path)[0]; gtk_notebook_set_current_page(notebook, page); - gui_sidebar_selected(SB_SIDEBAR); + gui_sidebar_selected(SB_SIDEBAR, __sidebar_get_queue(&iter)); gtk_tree_path_free(path); } } @@ -152,7 +152,8 @@ gboolean gui_sidebar_on_select(GtkTreeSelection *selection, return gtk_tree_path_get_depth(path) != 1; } -void gui_sidebar_selected(enum sidebar_selection_t selected) +void gui_sidebar_selected(enum sidebar_selection_t selected, + struct gui_queue *queue) { if (selected != SB_COLLECTION) __sidebar_deselect("o_collection_view"); @@ -160,6 +161,8 @@ void gui_sidebar_selected(enum sidebar_selection_t selected) __sidebar_deselect("o_playlist_view"); if (selected != SB_SIDEBAR) __sidebar_deselect("o_sidebar_view"); + + gui_queue_show(queue); } void gui_sidebar_add(struct gui_queue *queue) diff --git a/include/gui/queue.h b/include/gui/queue.h index 45cad9cb..f5f47b5d 100644 --- a/include/gui/queue.h +++ b/include/gui/queue.h @@ -47,4 +47,7 @@ static inline bool gui_queue_can_repeat(struct gui_queue *gq) return false; } +/* Called to set the correct state of the random and repeat buttons. */ +void gui_queue_show(struct gui_queue *); + #endif /* OCARINA_GUI_QUEUE_H */ diff --git a/include/gui/sidebar.h b/include/gui/sidebar.h index 965373c5..5a29f770 100644 --- a/include/gui/sidebar.h +++ b/include/gui/sidebar.h @@ -21,7 +21,7 @@ gboolean gui_sidebar_on_select(GtkTreeSelection *, GtkTreeModel *, GtkTreePath *path, gboolean, gpointer); /* Called to tell the sidebar that the selection has changed. */ -void gui_sidebar_selected(enum sidebar_selection_t); +void gui_sidebar_selected(enum sidebar_selection_t, struct gui_queue *); /* Called to add a queue to the sidebar. */ void gui_sidebar_add(struct gui_queue *); diff --git a/include/tests/gui.h b/include/tests/gui.h index 9c3d3a84..27ee5288 100644 --- a/include/tests/gui.h +++ b/include/tests/gui.h @@ -30,4 +30,9 @@ void __collection_toggled() {} void __playlist_selection_changed() {} #endif /* TEST_NEED_PLAYLIST */ +#ifdef TEST_NEED_WINDOW +void __window_configure() {} +void __window_state() {} +#endif /* TEST_NEED_WINDOW */ + #endif /* TESTS_GUI_H */ diff --git a/share/ocarina/ocarina6.glade b/share/ocarina/ocarina6.glade index 2b3af9e5..1b79a59c 100644 --- a/share/ocarina/ocarina6.glade +++ b/share/ocarina/ocarina6.glade @@ -24,6 +24,13 @@ inode/directory + + True + False + 10 + 2 + media-playlist-repeat + True False @@ -210,6 +217,13 @@ False window-close + + True + False + 10 + 2 + media-playlist-shuffle + True False @@ -223,7 +237,7 @@ - + @@ -620,15 +634,7 @@ True True center - - - True - False - 10 - 2 - media-playlist-shuffle - - + image9 False @@ -643,15 +649,7 @@ True True center - - - True - False - 10 - 2 - media-playlist-repeat - - + image10 False diff --git a/tests/gui/queue.c b/tests/gui/queue.c index d94a03b1..eb8b4a6d 100644 --- a/tests/gui/queue.c +++ b/tests/gui/queue.c @@ -1,7 +1,14 @@ /* * Copyright 2016 (c) Anna Schumaker. */ +#define TEST_NEED_AUDIO +#define TEST_NEED_COLLECTION +#define TEST_NEED_PLAYLIST +#define TEST_NEED_SIDEBAR +#define TEST_NEED_WINDOW +#include #include +#include #include static void *test_queue_init(struct queue *queue) @@ -20,26 +27,57 @@ static const struct queue_ops test_ops = { static void test_queue() { + GtkToggleButton *random, *repeat; + GtkWidget *random_img, *repeat_img; struct gui_queue *gq; struct queue q; + int argc = 0; - test_equal(gui_queue_can_random(NULL), (bool)false); - test_equal(gui_queue_can_repeat(NULL), (bool)false); + gtk_init(&argc, NULL); + gui_builder_init("share/ocarina/ocarina6.glade"); + random = GTK_TOGGLE_BUTTON(gui_builder_widget("o_random")); + repeat = GTK_TOGGLE_BUTTON(gui_builder_widget("o_repeat")); + random_img = gtk_button_get_image(GTK_BUTTON(random)); + repeat_img = gtk_button_get_image(GTK_BUTTON(repeat)); + /* Test initialization */ queue_init(&q, 0, &test_ops); gq = gui_queue(&q); - - test_not_equal((void *)gq, NULL); - test_equal((void *)gq->gq_queue, (void *)&q); - test_equal(gq->gq_text, "Test Queue"); - - test_equal(gq->gq_flags, GQ_CAN_RANDOM | GQ_CAN_REPEAT); test_equal(gui_queue_can_random(gq), (bool)true); test_equal(gui_queue_can_repeat(gq), (bool)true); gq->gq_flags = 0; - test_equal(gui_queue_can_random(gq), (bool)false); - test_equal(gui_queue_can_repeat(gq), (bool)false); + test_equal(gui_queue_can_random(NULL), (bool)false); + test_equal(gui_queue_can_repeat(NULL), (bool)false); + + /* Show a queue where random and repeat are disabled */ + gui_queue_show(gq); + test_equal(gtk_widget_get_sensitive(GTK_WIDGET(random)), false); + test_equal(gtk_widget_get_sensitive(GTK_WIDGET(repeat)), false); + test_equal(gtk_toggle_button_get_active(random), false); + test_equal(gtk_toggle_button_get_active(repeat), false); + test_equal(gtk_widget_get_sensitive(random_img), false); + test_equal(gtk_widget_get_sensitive(repeat_img), false); + + /* Show a queue where random and repeat are enabled */ + gq->gq_flags = GQ_CAN_RANDOM | GQ_CAN_REPEAT; + q.q_flags = Q_RANDOM | Q_REPEAT; + gui_queue_show(gq); + test_equal(gtk_widget_get_sensitive(GTK_WIDGET(random)), true); + test_equal(gtk_widget_get_sensitive(GTK_WIDGET(repeat)), true); + test_equal(gtk_toggle_button_get_active(random), true); + test_equal(gtk_toggle_button_get_active(repeat), true); + test_equal(gtk_widget_get_sensitive(random_img), true); + test_equal(gtk_widget_get_sensitive(repeat_img), true); + + /* Attempt to show a NULL pointer */ + gui_queue_show(NULL); + test_equal(gtk_widget_get_sensitive(GTK_WIDGET(random)), false); + test_equal(gtk_widget_get_sensitive(GTK_WIDGET(repeat)), false); + test_equal(gtk_toggle_button_get_active(random), false); + test_equal(gtk_toggle_button_get_active(repeat), false); + test_equal(gtk_widget_get_sensitive(random_img), false); + test_equal(gtk_widget_get_sensitive(repeat_img), false); queue_deinit(&q); test_equal((void *)gui_queue(&q), NULL);