ocarina/gui/queue_tab.cpp

206 lines
4.1 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
extern "C" {
#include <core/string.h>
#include <core/tempq.h>
#include <gui/builder.h>
#include <gui/sidebar.h>
}
#include <gui/tabs.h>
#include <gui/queue/label.h>
static void renumber_queues();
static class QueueColumns : public Gtk::TreeModelColumnRecord {
public:
QueueColumns()
{ add(q_col_track); add(q_col_title); add(q_col_length);
add(q_col_artist); add(q_col_album); add(q_col_year);
add(q_col_genre); add(q_col_count); add(q_col_played);
add(q_col_path); }
Gtk::TreeModelColumn<unsigned int> q_col_track;
Gtk::TreeModelColumn<std::string> q_col_title;
Gtk::TreeModelColumn<std::string> q_col_length;
Gtk::TreeModelColumn<std::string> q_col_artist;
Gtk::TreeModelColumn<std::string> q_col_album;
Gtk::TreeModelColumn<unsigned int> q_col_year;
Gtk::TreeModelColumn<std::string> q_col_genre;
Gtk::TreeModelColumn<unsigned int> q_col_count;
Gtk::TreeModelColumn<std::string> q_col_played;
Gtk::TreeModelColumn<std::string> q_col_path;
} queue_cols;
class QueueTab : public Tab {
private:
/**
* Queue tab widgets
*/
TempLabel *q_label;
public:
QueueTab(queue *, unsigned int num);
~QueueTab();
void on_track_removed(unsigned int);
/**
* Helper functions
*/
bool on_key_press_event(const std::string &);
void queue_set_number(unsigned int);
void on_tab_reordered();
void on_move_queue(int);
/**
* GTK-MM Callbacks
*/
void on_close_clicked();
};
static std::map<Gtk::Widget *, QueueTab *> queue_mapping;
QueueTab :: QueueTab(queue *pq, unsigned int num)
: Tab(pq)
{
Gtk::Notebook *notebook;
tab_builder->add_from_file(gui :: share_file("QueueLabel.ui"));
/*
* Create our tab widget
*/
tab_builder->get_widget_derived("TempLabel", q_label);
q_label->temp_close->signal_clicked().connect(sigc::mem_fun(*this,
&QueueTab::on_close_clicked));
q_label->init(tab_pq);
tab_label = q_label;
queue_set_number(num);
/*
* Fill in the page!
*/
tab_vbox.pack_start(*tab_window, true, true);
queue_mapping[&tab_vbox] = this;
notebook = Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false);
notebook->insert_page(tab_vbox, *q_label, num);
notebook->set_tab_reorderable(tab_vbox);
}
QueueTab :: ~QueueTab()
{
tab_window->q_treeview->unset_model();
queue_mapping.erase(&tab_vbox);
tab_unmap();
gui_sidebar_remove(gui_queue(tab_pq));
Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false)->remove_page(tab_vbox);
renumber_queues();
}
void QueueTab :: on_track_removed(unsigned int row)
{
this->Tab :: on_track_removed(row);
if (queue_size(tab_pq) == 0)
delete this;
}
/**
*
* Queue tab helper functions
*
*/
void QueueTab :: queue_set_number(unsigned int num)
{
gchar *text = g_strdup_printf("%u. ", num);
q_label->temp_number->set_text(text);
g_free(text);
}
void QueueTab :: on_tab_reordered()
{
queue_set_number(tab_page_num());
}
void QueueTab :: on_move_queue(int num)
{
tempq_move(tab_pq, num);
}
/**
*
* Gtk-MM Callback Functions
*
*/
void QueueTab :: on_close_clicked()
{
tempq_free(tab_pq);
delete this;
}
/**
*
* Global functions
*
*/
static void renumber_queues()
{
std::map<Gtk::Widget *, QueueTab *>::iterator it;
for (it = queue_mapping.begin(); it != queue_mapping.end(); it++)
it->second->on_tab_reordered();
}
void on_pq_created(queue *pq, unsigned int num)
{
new QueueTab(pq, num);
}
static void on_page_reordered(Gtk::Widget *page, int num)
{
Gtk::Notebook *notebook = Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false);
if ((unsigned int)num >= tempq_count()) {
notebook->reorder_child(*page, tempq_count() - 1);
return;
}
std::map<Gtk::Widget *, QueueTab *>::iterator it;
it = queue_mapping.find(page);
if (it != queue_mapping.end()) {
it->second->on_move_queue(num);
renumber_queues();
}
}
void init_queue_tabs()
{
Gtk::Notebook *notebook = Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false);
notebook->signal_page_reordered().connect(sigc::ptr_fun(on_page_reordered));
}
void post_init_queue_tabs()
{
unsigned int i;
for (i = 0; i < tempq_count(); i++)
on_pq_created(tempq_get(i), i);
}