From ac03eed3f58fb80e3417a6ede625be51ec42677c Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Sat, 27 Aug 2011 13:38:01 -0400 Subject: [PATCH] ocarina: Show and hide play and pause buttons I hide the play button when music is playing and show it when music has been stopped. I hide the pause button when music is not playing and show it when music has been stopped. This can give the effect of one button replacing the other. --- gui/button.cpp | 64 +++++++++++++++++++++++++++++++++------- gui/callback.cpp | 6 ++++ gui/ocarina.cpp | 7 +++-- include/ocarina/button.h | 5 ++++ 4 files changed, 69 insertions(+), 13 deletions(-) diff --git a/gui/button.cpp b/gui/button.cpp index 2e00c07c..886a615d 100644 --- a/gui/button.cpp +++ b/gui/button.cpp @@ -1,22 +1,42 @@ +#include +using namespace std; + #include #include #include #include +static list play_buttons; +static list pause_buttons; + +static void show_list(list &l) +{ + list::iterator it; + for(it = l.begin(); it != l.end(); it++) + gtk_widget_show(*it); +} + +static void hide_list(list &l) +{ + list::iterator it; + for(it = l.begin(); it != l.end(); it++) + gtk_widget_hide(*it); +} + static GtkWidget *make_button(const gchar *stockid, GtkIconSize size, - void (* func)(GtkWidget *, GdkEvent *, gpointer)) + void (* func)(GtkWidget *, GdkEvent *, gpointer), + bool show) { GtkWidget *button = gtk_button_new(); - GtkWidget *box = gtk_hbox_new(FALSE, 0); GtkWidget *image = gtk_image_new_from_stock(stockid, size); - + gtk_widget_show(image); GTK_CONNECT(button, "clicked", func, NULL); - box_pack_start(box, image, FALSE, FALSE, 0); - container_add(button, box); - gtk_widget_show_all(button); + container_add(button, image); + if (show == true) + gtk_widget_show_all(button); return button; } @@ -27,7 +47,19 @@ static void on_click_play(GtkWidget *b, GdkEvent *e, gpointer d) GtkWidget *make_play_button(GtkIconSize size) { - return make_button(GTK_STOCK_MEDIA_PLAY, size, on_click_play); + GtkWidget *b = make_button(GTK_STOCK_MEDIA_PLAY, size, on_click_play, true); + play_buttons.push_back(b); + return b; +} + +void show_play_buttons() +{ + show_list(play_buttons); +} + +void hide_play_buttons() +{ + hide_list(play_buttons); } static void on_click_pause(GtkWidget *b, GdkEvent *e, gpointer d) @@ -37,7 +69,19 @@ static void on_click_pause(GtkWidget *b, GdkEvent *e, gpointer d) GtkWidget *make_pause_button(GtkIconSize size) { - return make_button(GTK_STOCK_MEDIA_PAUSE, size, on_click_pause); + GtkWidget *b = make_button(GTK_STOCK_MEDIA_PAUSE, size, on_click_pause, false); + pause_buttons.push_back(b); + return b; +} + +void show_pause_buttons() +{ + show_list(pause_buttons); +} + +void hide_pause_buttons() +{ + hide_list(pause_buttons); } static void on_click_stop(GtkWidget *b, GdkEvent *e, gpointer d) @@ -47,7 +91,7 @@ static void on_click_stop(GtkWidget *b, GdkEvent *e, gpointer d) GtkWidget *make_stop_button(GtkIconSize size) { - return make_button(GTK_STOCK_MEDIA_STOP, size, on_click_stop); + return make_button(GTK_STOCK_MEDIA_STOP, size, on_click_stop, true); } static void on_click_open_file(GtkWidget *b, GdkEvent *e, gpointer d) @@ -59,5 +103,5 @@ static void on_click_open_file(GtkWidget *b, GdkEvent *e, gpointer d) GtkWidget *make_open_button(GtkIconSize size) { - return make_button(GTK_STOCK_OPEN, size, on_click_open_file); + return make_button(GTK_STOCK_OPEN, size, on_click_open_file, true); } diff --git a/gui/callback.cpp b/gui/callback.cpp index 0e49e346..3a7ff371 100644 --- a/gui/callback.cpp +++ b/gui/callback.cpp @@ -1,13 +1,19 @@ #include +#include + void cb_play() { print("Ocarina PLAY callback!"); + hide_play_buttons(); + show_pause_buttons(); } void cb_pause() { print("Ocarina PAUSE callback!"); + hide_pause_buttons(); + show_play_buttons(); } static void add_callback(callback_t type, void (* func)()) diff --git a/gui/ocarina.cpp b/gui/ocarina.cpp index cddd4f9f..8a6fce7f 100644 --- a/gui/ocarina.cpp +++ b/gui/ocarina.cpp @@ -30,7 +30,6 @@ void ocarina_init(int argc, char **argv) box_pack_start(hbox, stop, FALSE, FALSE, 0); gtk_widget_show(hbox); window_add(hbox); - gtk_main(); } void ocarina_quit() @@ -44,11 +43,13 @@ int main(int argc, char **argv) libsaria_init(argc, argv); setup_callbacks(); + gtk_init(&argc, &argv); + ocarina_init(argc, argv); + if (argc > 1) libsaria_get()->load(argv[1]); - gtk_init(&argc, &argv); - ocarina_init(argc, argv); + gtk_main(); libsaria_quit(); return 0; } diff --git a/include/ocarina/button.h b/include/ocarina/button.h index 5a6e630a..bad06c97 100644 --- a/include/ocarina/button.h +++ b/include/ocarina/button.h @@ -11,4 +11,9 @@ GtkWidget *make_pause_button(GtkIconSize); GtkWidget *make_stop_button(GtkIconSize); GtkWidget *make_open_button(GtkIconSize); +void show_play_buttons(); +void hide_play_buttons(); +void show_pause_buttons(); +void hide_pause_buttons(); + #endif /* OCARINA_BUTTON_H */