From e409c2317fddbd39df325c320c927136e758562f Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Mon, 7 Nov 2011 08:32:01 -0500 Subject: [PATCH] ocarina: Added a random button Random hasn't been implemented in libsaria yet, but I can still create the button to test preferences. The random button should set itself to the value store in the preferences file. --- include/ocarina/button.h | 2 ++ ocarina/buttons/toggle.cpp | 48 +++++++++++++++++++++++++++++++++++++ ocarina/footer/controls.cpp | 3 ++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 ocarina/buttons/toggle.cpp diff --git a/include/ocarina/button.h b/include/ocarina/button.h index 93623406..af20b473 100644 --- a/include/ocarina/button.h +++ b/include/ocarina/button.h @@ -23,6 +23,8 @@ GtkWidget *make_rewind_button(); GtkWidget *make_forward_button(); GtkWidget *make_next_button(); +GtkWidget *make_random_button(); + GtkWidget *make_open_button(); GtkWidget *make_volume_button(); diff --git a/ocarina/buttons/toggle.cpp b/ocarina/buttons/toggle.cpp new file mode 100644 index 00000000..368ff776 --- /dev/null +++ b/ocarina/buttons/toggle.cpp @@ -0,0 +1,48 @@ + +#include +#include +#include + +static GtkWidget *get_toggle_button(void (* func)(GtkWidget *, GdkEvent *, gpointer), + bool state, bool show) +{ + GtkWidget *button = gtk_toggle_button_new(); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), state == TRUE); + GTK_CONNECT(button, "toggled", func, NULL); + if (show == true) + gtk_widget_show(button); + return button; +} + +static GtkWidget *get_image(string path) +{ + GtkWidget *image = gtk_image_new(); + gtk_image_set_from_file(GTK_IMAGE(image), path.c_str()); + gtk_widget_show(image); + return image; +} + +static GtkWidget *make_toggle_button(string file, + void (* func)(GtkWidget *, GdkEvent *, gpointer), + bool state, + bool show) +{ + GtkWidget *button = get_toggle_button(func, state, show); + GtkWidget *image = get_image(file); + container_add(button, image); + return button; +} + +static void toggle_random(GtkWidget *b, GdkEvent *e, gpointer d) +{ + bool active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(b)) == TRUE; + libsaria::prefs::set("random", active); +} + +GtkWidget *make_random_button() +{ + return make_toggle_button("images/random.png", + toggle_random, + libsaria::prefs::get_bool("random"), + true); +} diff --git a/ocarina/footer/controls.cpp b/ocarina/footer/controls.cpp index 1d901615..5d172a69 100644 --- a/ocarina/footer/controls.cpp +++ b/ocarina/footer/controls.cpp @@ -25,7 +25,8 @@ static GtkWidget *make_top_row() GtkWidget *buttons = gtk_hbox_new(FALSE, 0); box_pack_end(buttons, make_volume_button(), FALSE, FALSE, 0); - box_pack_end(buttons, make_open_button(), FALSE, FALSE, 0); + box_pack_end(buttons, make_random_button(), FALSE, FALSE, 0); + box_pack_end(buttons, make_open_button(), FALSE, FALSE, 0); gtk_widget_show_all(buttons); return buttons;