ocarina/gui/controls.cpp

83 lines
1.4 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <lib/lib.h>
#include <core/audio.h>
#include <core/driver.h>
#include <gui/ocarina.h>
static inline bool audio_playing()
{
return driver :: get_driver()->is_playing();
}
static void hide_show_buttons(const std::string &hide, const std::string &show)
{
lib :: get_widget<Gtk::Button>(hide)->hide();
lib :: get_widget<Gtk::Button>(show)->show();
}
void o_fix_buttons()
{
if (audio_playing())
hide_show_buttons("o_play", "o_pause");
else
hide_show_buttons("o_pause", "o_play");
}
/*
* Timeout function is called every half-second.
*/
static bool on_timeout()
{
Gtk::Label *position = lib :: get_widget<Gtk::Label>("o_cur_position");
Glib::RefPtr<Gtk::Adjustment> bar = lib :: get_object<Gtk::Adjustment>("o_progress");
position->set_text(audio :: position_str());
bar->set_upper(audio :: duration());
bar->set_value(audio :: position());
o_fix_buttons();
return audio_playing();
}
static void o_play()
{
audio :: play();
o_fix_buttons();
lib :: schedule(on_timeout, 500);
}
static void o_pause()
{
audio :: pause();
o_fix_buttons();
}
void o_toggle()
{
if (audio_playing())
o_pause();
else
o_play();
}
void o_next()
{
audio :: next();
o_play();
}
void controls_init()
{
connect_button("o_play", o_play);
connect_button("o_pause", o_pause);
connect_button("o_stop", audio::stop);
connect_button("o_prev", audio::prev);
connect_button("o_next", o_next);
}