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 <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-01-14 08:59:26 -05:00
parent 2c676631a4
commit ddbc040a3b
9 changed files with 108 additions and 34 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -1,6 +1,7 @@
/*
* Copyright 2016 (c) Anna Schumaker.
*/
#include <gui/builder.h>
#include <gui/queue.h>
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);
}

View File

@ -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)

View File

@ -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 */

View File

@ -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 *);

View File

@ -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 */

View File

@ -24,6 +24,13 @@
<mime-type>inode/directory</mime-type>
</mime-types>
</object>
<object class="GtkImage" id="image10">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xpad">10</property>
<property name="ypad">2</property>
<property name="icon_name">media-playlist-repeat</property>
</object>
<object class="GtkImage" id="image11">
<property name="visible">True</property>
<property name="can_focus">False</property>
@ -210,6 +217,13 @@
<property name="can_focus">False</property>
<property name="icon_name">window-close</property>
</object>
<object class="GtkImage" id="image9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xpad">10</property>
<property name="ypad">2</property>
<property name="icon_name">media-playlist-shuffle</property>
</object>
<object class="GtkMenu" id="o_collection_rc">
<property name="visible">True</property>
<property name="can_focus">False</property>
@ -223,7 +237,7 @@
</object>
</child>
</object>
<object class="GtkTreeStore" id="o_collection_store">
<object class="GtkTreeStore" id="o_collection_store">
<columns>
<!-- column-name Image -->
<column type="gchararray"/>
@ -620,15 +634,7 @@
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<child>
<object class="GtkImage" id="image10">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xpad">10</property>
<property name="ypad">2</property>
<property name="icon_name">media-playlist-shuffle</property>
</object>
</child>
<property name="image">image9</property>
</object>
<packing>
<property name="expand">False</property>
@ -643,15 +649,7 @@
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="valign">center</property>
<child>
<object class="GtkImage" id="image9">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xpad">10</property>
<property name="ypad">2</property>
<property name="icon_name">media-playlist-repeat</property>
</object>
</child>
<property name="image">image10</property>
</object>
<packing>
<property name="expand">False</property>

View File

@ -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 <gui/builder.h>
#include <gui/queue.h>
#include <tests/gui.h>
#include <tests/test.h>
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);