ocarina/gui/controls.cpp

95 lines
1.7 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
#include <lib/lib.h>
#include <core/audio.h>
#include <core/driver.h>
#include <gui/ocarina.h>
static Gtk::Label *o_position;
static Gtk::Button *o_play_button;
static Gtk::Button *o_pause_button;
static Glib::RefPtr<Gtk::Adjustment> o_pos_bar;
static inline bool audio_playing()
{
return audio :: get_driver()->is_playing();
}
void o_fix_buttons()
{
if (audio_playing()) {
o_play_button->hide();
o_pause_button->show();
} else {
o_play_button->show();
o_pause_button->hide();
}
}
/*
* Timeout function is called every half-second.
*/
static bool on_timeout()
{
o_position->set_text(audio :: position_str());
o_pos_bar->set_upper(audio :: duration());
o_pos_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();
}
bool o_seek(Gtk::ScrollType type, double value)
{
audio :: seek_to(value);
return true;
}
void controls_init()
{
Gtk::Scale *pos_scale = lib :: get_widget<Gtk::Scale>("o_position_scale");
o_position = lib :: get_widget<Gtk::Label>("o_cur_position");
o_pos_bar = lib :: get_object<Gtk::Adjustment>("o_progress");
o_play_button = lib :: get_widget<Gtk::Button>("o_play");
o_pause_button = lib :: get_widget<Gtk::Button>("o_pause");
pos_scale->signal_change_value().connect(sigc::ptr_fun(o_seek));
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);
}