ocarina/gui/tabs.cpp

204 lines
3.9 KiB
C++

/*
* Copyright 2014 (c) Anna Schumaker.
*/
extern "C" {
#include <core/collection.h>
#include <core/playlist.h>
#include <core/string.h>
#include <core/tempq.h>
#include <gui/builder.h>
#include <gui/queue.h>
#include <gui/sidebar.h>
}
#include <gui/tabs.h>
#include <map>
#include <sstream>
static std::map<queue *, Tab *> queue_mapping;
static void *tempq_init(struct queue *queue)
{
struct gui_queue *gq = gui_queue_alloc(queue, "Queued Tracks",
GQ_CAN_RANDOM | GQ_CAN_REPEAT | GQ_CAN_DISABLE);
queue->q_private = gq;
on_pq_created(queue, tempq_index(queue));
gui_sidebar_add(gq);
return gq;
}
static void tempq_added(struct queue *queue, unsigned int pos)
{
Tab *tab = find_tab(queue);
if (tab) {
tab->on_track_added(pos);
tempq_save(queue, Q_ENABLED);
}
gui_queue_added(queue, pos);
}
static bool tempq_erase(struct queue *queue, struct track *track)
{
return true;
}
static void tempq_removed(struct queue *queue, unsigned int pos)
{
find_tab(queue)->on_track_removed(pos);
tempq_save(queue, Q_ENABLED);
gui_queue_removed(queue, pos);
}
struct queue_ops tempq_ops = {
tempq_init,
gui_queue_free,
tempq_added,
tempq_erase,
tempq_removed,
gui_queue_cleared,
tempq_save,
gui_queue_updated,
};
/**
*
* Tab class basics
*
*/
Tab :: Tab(queue *pq)
: tab_sorting_count(0), tab_pq(pq), tab_label(NULL)
{
queue_mapping[tab_pq] = this;
tab_builder = Gtk::Builder::create();
tab_builder->add_from_file(gui :: share_file("QueueWindow.ui"));
tab_builder->get_widget_derived("QueueWindow", tab_window);
tab_window->init(tab_pq);
tab_vbox.set_margin_start(1);
tab_vbox.set_margin_end(1);
tab_vbox.set_homogeneous(false);
tab_vbox.show();
}
Tab :: ~Tab() {}
/**
*
* QNotifier implementation.
*
*/
void Tab :: on_track_added(unsigned int row)
{
tab_label->set_size();
}
void Tab :: on_track_removed(unsigned int row)
{
tab_label->set_size();
}
/**
*
* Tab internal helper functions
*
*/
int Tab :: tab_page_num()
{
Gtk::Notebook *notebook = Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false);
return notebook->page_num(tab_vbox);
}
bool Tab :: tab_is_cur()
{
Gtk::Notebook *notebook = Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false);
return notebook->page_num(tab_vbox) == notebook->get_current_page();
}
void Tab :: tab_unmap()
{
queue_mapping.erase(tab_pq);
}
/**
*
* Global functions
*
*/
Tab *find_tab(queue *pq)
{
std::map<queue *, Tab *>::iterator it;
it = queue_mapping.find(pq);
if (it != queue_mapping.end())
return it->second;
return NULL;
}
static bool on_window_key_pressed(GdkEventKey *event)
{
Gtk::Notebook *notebook = Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false);
std::string key = gdk_keyval_name(event->keyval);
if (key.size() >= 3) {
if (key.substr(0, 3) == "KP_")
key = key.substr(3);
}
if (key >= "0" && key <= "9") {
unsigned int n = atoi(key.c_str());
if (n < tempq_count())
notebook->set_current_page(n);
} else if (key == "c")
notebook->set_current_page(tempq_count());
else if (key == "h")
notebook->set_current_page(tempq_count() + 1);
else if (key == "m")
notebook->set_current_page(tempq_count() + 3);
else if (key == "p")
notebook->set_current_page(tempq_count() + 2);
else
return false;
return true;
}
void init_tabs()
{
Gtk::Window *window = Glib :: wrap(GTK_WINDOW(gui_builder_widget("o_window")), false);
/* Initialize other tabs */
init_queue_tabs();
/* Setup keyboard shortcuts */
window->signal_key_press_event().connect(sigc::ptr_fun(on_window_key_pressed));
}
void post_init_tabs()
{
post_init_queue_tabs();
unsigned int tab = 0;
for (tab = 0; tab < tempq_count(); tab++) {
if (queue_has_flag(tempq_get(tab), Q_ENABLED))
break;
}
Glib :: wrap(GTK_NOTEBOOK(gui_builder_widget("o_notebook")), false)->set_current_page(tab);
}
void cleanup_tabs()
{
while (queue_mapping.size() > 0)
delete queue_mapping.begin()->second;
}