gui: Move GtkBuilder object into the gui

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-01-21 08:29:46 -05:00
parent 7f6f6395d7
commit dc3e770c28
12 changed files with 98 additions and 93 deletions

View File

@ -20,11 +20,11 @@ public:
CollectionTab :: CollectionTab() CollectionTab :: CollectionTab()
: Tab(library::get_queue()) : Tab(library::get_queue())
{ {
tab_random = lib :: get_widget<Gtk::ToggleButton>("o_collection_random"); tab_random = gui :: get_widget<Gtk::ToggleButton>("o_collection_random");
tab_search = lib :: get_widget<Gtk::SearchEntry>("o_collection_entry"); tab_search = gui :: get_widget<Gtk::SearchEntry>("o_collection_entry");
tab_size = lib :: get_widget<Gtk::Label>("o_collection_size"); tab_size = gui :: get_widget<Gtk::Label>("o_collection_size");
tab_treeview = lib :: get_widget<Gtk::TreeView>("o_collection_pq_treeview"); tab_treeview = gui :: get_widget<Gtk::TreeView>("o_collection_pq_treeview");
tab_widget = lib :: get_widget<Gtk::Widget>("o_collection_page"); tab_widget = gui :: get_widget<Gtk::Widget>("o_collection_page");
tab_finish_init(); tab_finish_init();
} }

View File

@ -208,22 +208,22 @@ void gst :: init(int *argc, char ***argv)
gst_bus = gst_pipeline_get_bus(GST_PIPELINE(gst_player)); gst_bus = gst_pipeline_get_bus(GST_PIPELINE(gst_player));
gst_driver = new GSTDriver(); gst_driver = new GSTDriver();
o_next = lib :: get_widget<Gtk::Button>("o_next"); o_next = gui :: get_widget<Gtk::Button>("o_next");
o_pause = lib :: get_widget<Gtk::Button>("o_pause"); o_pause = gui :: get_widget<Gtk::Button>("o_pause");
o_play = lib :: get_widget<Gtk::Button>("o_play"); o_play = gui :: get_widget<Gtk::Button>("o_play");
o_prev = lib :: get_widget<Gtk::Button>("o_prev"); o_prev = gui :: get_widget<Gtk::Button>("o_prev");
o_seek = lib :: get_widget<Gtk::Scale>("o_seek"); o_seek = gui :: get_widget<Gtk::Scale>("o_seek");
o_stop = lib :: get_widget<Gtk::Button>("o_stop"); o_stop = gui :: get_widget<Gtk::Button>("o_stop");
o_count = lib :: get_widget<Gtk::SpinButton>("o_pause_count"); o_count = gui :: get_widget<Gtk::SpinButton>("o_pause_count");
o_enabled = lib :: get_widget<Gtk::CheckButton>("o_pause_enabled"); o_enabled = gui :: get_widget<Gtk::CheckButton>("o_pause_enabled");
o_album = lib :: get_widget<Gtk::Label>("o_album"); o_album = gui :: get_widget<Gtk::Label>("o_album");
o_artist = lib :: get_widget<Gtk::Label>("o_artist"); o_artist = gui :: get_widget<Gtk::Label>("o_artist");
o_duration = lib :: get_widget<Gtk::Label>("o_duration"); o_duration = gui :: get_widget<Gtk::Label>("o_duration");
o_position = lib :: get_widget<Gtk::Label>("o_position"); o_position = gui :: get_widget<Gtk::Label>("o_position");
o_title = lib :: get_widget<Gtk::Label>("o_title"); o_title = gui :: get_widget<Gtk::Label>("o_title");
o_progress = lib :: get_object<Gtk::Adjustment>("o_progress"); o_progress = gui :: get_object<Gtk::Adjustment>("o_progress");
o_next->signal_clicked().connect(sigc::ptr_fun(next)); o_next->signal_clicked().connect(sigc::ptr_fun(next));
o_pause->signal_clicked().connect(sigc::ptr_fun(audio :: pause)); o_pause->signal_clicked().connect(sigc::ptr_fun(audio :: pause));

View File

@ -15,10 +15,10 @@ public:
HistoryTab :: HistoryTab() HistoryTab :: HistoryTab()
: Tab(deck :: get_queue()) : Tab(deck :: get_queue())
{ {
tab_search = lib :: get_widget<Gtk::SearchEntry>("o_history_entry"); tab_search = gui :: get_widget<Gtk::SearchEntry>("o_history_entry");
tab_size = lib :: get_widget<Gtk::Label>("o_history_size"); tab_size = gui :: get_widget<Gtk::Label>("o_history_size");
tab_treeview = lib :: get_widget<Gtk::TreeView>("o_history_treeview"); tab_treeview = gui :: get_widget<Gtk::TreeView>("o_history_treeview");
tab_widget = lib :: get_widget<Gtk::Widget>("o_history_page"); tab_widget = gui :: get_widget<Gtk::Widget>("o_history_page");
tab_finish_init(); tab_finish_init();
} }

View File

@ -7,11 +7,22 @@
#include <gui/ocarina.h> #include <gui/ocarina.h>
#include <gui/tabs.h> #include <gui/tabs.h>
namespace gui
{
Glib::RefPtr<Gtk::Builder> __O_BUILDER;
}
Gtk::Window *ocarina_init(int *argc, char ***argv) Gtk::Window *ocarina_init(int *argc, char ***argv)
{ {
Gtk::Window *window; Gtk::Window *window;
lib :: init(argc, argv, "ocarina6.glade"); lib :: init(argc, argv);
gui :: __O_BUILDER = Gtk::Builder::create();
if (!gui :: __O_BUILDER->add_from_file(lib :: share_file("ocarina6.glade")))
exit(1);
gst :: init(argc, argv); gst :: init(argc, argv);
plist :: init(); plist :: init();

View File

@ -175,14 +175,14 @@ static void on_toggled(const Glib::ustring &str)
void manager :: init() void manager :: init()
{ {
c_add = lib :: get_widget<Gtk::Button>("colmgr_add"); c_add = gui :: get_widget<Gtk::Button>("colmgr_add");
c_update = lib :: get_widget<Gtk::Button>("colmgr_update"); c_update = gui :: get_widget<Gtk::Button>("colmgr_update");
c_chooser = lib :: get_widget<Gtk::FileChooserWidget>("colmgr_chooser"); c_chooser = gui :: get_widget<Gtk::FileChooserWidget>("colmgr_chooser");
c_progress = lib :: get_widget<Gtk::ProgressBar>("o_idle_progress"); c_progress = gui :: get_widget<Gtk::ProgressBar>("o_idle_progress");
c_toggle = lib :: get_object<Gtk::CellRendererToggle>("colmgr_toggle"); c_toggle = gui :: get_object<Gtk::CellRendererToggle>("colmgr_toggle");
c_list = lib :: get_object<Gtk::ListStore>("colmgr_list"); c_list = gui :: get_object<Gtk::ListStore>("colmgr_list");
c_treeview = lib :: get_widget<Gtk::TreeView>("colmgr_treeview"); c_treeview = gui :: get_widget<Gtk::TreeView>("colmgr_treeview");
c_add->signal_clicked().connect(sigc::ptr_fun(on_add)); c_add->signal_clicked().connect(sigc::ptr_fun(on_add));
c_update->signal_clicked().connect(sigc::ptr_fun(on_update)); c_update->signal_clicked().connect(sigc::ptr_fun(on_update));

View File

@ -26,9 +26,9 @@ static class PlaylistTab : public Tab {
public: public:
PlaylistTab() : Tab(playlist :: get_queue()) PlaylistTab() : Tab(playlist :: get_queue())
{ {
tab_search = lib :: get_widget<Gtk::SearchEntry>("o_playlist_entry"); tab_search = gui :: get_widget<Gtk::SearchEntry>("o_playlist_entry");
tab_treeview = lib :: get_widget<Gtk::TreeView>("o_playlist_pq_treeview"); tab_treeview = gui :: get_widget<Gtk::TreeView>("o_playlist_pq_treeview");
tab_widget = lib :: get_widget<Gtk::Widget>("o_playlist_page"); tab_widget = gui :: get_widget<Gtk::Widget>("o_playlist_page");
tab_finish_init(); tab_finish_init();
} }
@ -111,14 +111,14 @@ void plist :: track_loaded(Track *track)
void plist :: init() void plist :: init()
{ {
p_tab = new PlaylistTab; p_tab = new PlaylistTab;
p_list = lib :: get_object<Gtk::ListStore>("plist_list"); p_list = gui :: get_object<Gtk::ListStore>("plist_list");
p_treeview = lib :: get_widget<Gtk::TreeView>("plist_treeview"); p_treeview = gui :: get_widget<Gtk::TreeView>("plist_treeview");
p_treeview->signal_cursor_changed().connect(sigc::ptr_fun(on_cursor_changed)); p_treeview->signal_cursor_changed().connect(sigc::ptr_fun(on_cursor_changed));
p_treeview->signal_button_press_event().connect(sigc::ptr_fun(on_clicked)); p_treeview->signal_button_press_event().connect(sigc::ptr_fun(on_clicked));
o_ban = lib :: get_widget<Gtk::ToggleButton>("o_ban"); o_ban = gui :: get_widget<Gtk::ToggleButton>("o_ban");
o_fav = lib :: get_widget<Gtk::ToggleButton>("o_favorite"); o_fav = gui :: get_widget<Gtk::ToggleButton>("o_favorite");
o_ban->signal_toggled().connect(sigc::ptr_fun(on_ban)); o_ban->signal_toggled().connect(sigc::ptr_fun(on_ban));
o_fav->signal_toggled().connect(sigc::ptr_fun(on_favorite)); o_fav->signal_toggled().connect(sigc::ptr_fun(on_favorite));

View File

@ -165,8 +165,8 @@ QueueTab :: QueueTab(Queue *pq, unsigned int num)
tab_finish_init(); tab_finish_init();
queue_mapping[tab_widget] = this; queue_mapping[tab_widget] = this;
lib :: get_widget<Gtk::Notebook>("o_notebook")->insert_page(q_vbox, q_tab_box, num); gui :: get_widget<Gtk::Notebook>("o_notebook")->insert_page(q_vbox, q_tab_box, num);
lib :: get_widget<Gtk::Notebook>("o_notebook")->set_tab_reorderable(q_vbox); gui :: get_widget<Gtk::Notebook>("o_notebook")->set_tab_reorderable(q_vbox);
q_vbox.show_all(); q_vbox.show_all();
} }
@ -174,7 +174,7 @@ QueueTab :: ~QueueTab()
{ {
queue_mapping.erase(tab_widget); queue_mapping.erase(tab_widget);
tab_unmap(); tab_unmap();
lib :: get_widget<Gtk::Notebook>("o_notebook")->remove_page(q_vbox); gui :: get_widget<Gtk::Notebook>("o_notebook")->remove_page(q_vbox);
} }
@ -336,7 +336,7 @@ static void on_pq_removed(Queue *pq)
static void on_page_reordered(Gtk::Widget *page, int num) static void on_page_reordered(Gtk::Widget *page, int num)
{ {
Gtk::Notebook *notebook = lib :: get_widget<Gtk::Notebook>("o_notebook"); Gtk::Notebook *notebook = gui :: get_widget<Gtk::Notebook>("o_notebook");
if ((unsigned int)num >= deck :: get_queues().size()) { if ((unsigned int)num >= deck :: get_queues().size()) {
notebook->reorder_child(*page, deck::get_queues().size() - 1); notebook->reorder_child(*page, deck::get_queues().size() - 1);
@ -356,7 +356,7 @@ void init_queue_tabs()
struct Callbacks *cb = get_callbacks(); struct Callbacks *cb = get_callbacks();
cb->on_pq_removed = on_pq_removed; cb->on_pq_removed = on_pq_removed;
Gtk::Notebook *notebook = lib :: get_widget<Gtk::Notebook>("o_notebook"); Gtk::Notebook *notebook = gui :: get_widget<Gtk::Notebook>("o_notebook");
notebook->signal_page_reordered().connect(sigc::ptr_fun(on_page_reordered)); notebook->signal_page_reordered().connect(sigc::ptr_fun(on_page_reordered));
} }

View File

@ -96,20 +96,20 @@ void Tab :: tab_toggle_button(Gtk::ToggleButton *button, queue_flags flag)
int Tab :: tab_page_num() int Tab :: tab_page_num()
{ {
Gtk::Notebook *notebook = lib :: get_widget<Gtk::Notebook>("o_notebook"); Gtk::Notebook *notebook = gui :: get_widget<Gtk::Notebook>("o_notebook");
return notebook->page_num(*tab_widget); return notebook->page_num(*tab_widget);
} }
bool Tab :: tab_is_cur() bool Tab :: tab_is_cur()
{ {
Gtk::Notebook *notebook = lib :: get_widget<Gtk::Notebook>("o_notebook"); Gtk::Notebook *notebook = gui :: get_widget<Gtk::Notebook>("o_notebook");
return notebook->page_num(*tab_widget) == notebook->get_current_page(); return notebook->page_num(*tab_widget) == notebook->get_current_page();
} }
void Tab :: tab_runtime_changed() void Tab :: tab_runtime_changed()
{ {
if (tab_is_cur()) if (tab_is_cur())
lib :: get_widget<Gtk::Label>("o_queue_time")->set_text(tab_pq->length_str()); gui :: get_widget<Gtk::Label>("o_queue_time")->set_text(tab_pq->length_str());
} }
void Tab :: tab_display_sorting() void Tab :: tab_display_sorting()
@ -117,7 +117,7 @@ void Tab :: tab_display_sorting()
std::string text = ""; std::string text = "";
if ((tab_sorting_count > 0) && tab_is_cur()) if ((tab_sorting_count > 0) && tab_is_cur())
text = "Sorting within " + tab_sorting_title; text = "Sorting within " + tab_sorting_title;
lib :: get_widget<Gtk::Label>("o_sorting_indicator")->set_text(text); gui :: get_widget<Gtk::Label>("o_sorting_indicator")->set_text(text);
} }
void Tab :: tab_dec_sort_count() void Tab :: tab_dec_sort_count()
@ -258,23 +258,23 @@ void Tab :: on_show_rc_menu()
unsigned int size = deck :: get_queues().size(); unsigned int size = deck :: get_queues().size();
if (size == 0) { if (size == 0) {
lib :: get_widget<Gtk::MenuItem>("o_add_to_pq")->hide(); gui :: get_widget<Gtk::MenuItem>("o_add_to_pq")->hide();
return; return;
} }
lib :: get_widget<Gtk::MenuItem>("o_add_to_pq")->show(); gui :: get_widget<Gtk::MenuItem>("o_add_to_pq")->show();
if (size == 10) if (size == 10)
lib :: get_widget<Gtk::MenuItem>("o_new_pq")->hide(); gui :: get_widget<Gtk::MenuItem>("o_new_pq")->hide();
else else
lib :: get_widget<Gtk::MenuItem>("o_new_pq")->show(); gui :: get_widget<Gtk::MenuItem>("o_new_pq")->show();
for (unsigned int i = 0; i < 10; i++) { for (unsigned int i = 0; i < 10; i++) {
item = "o_pq_"; item = "o_pq_";
item += '0' + i; item += '0' + i;
if (i < size) if (i < size)
lib :: get_widget<Gtk::MenuItem>(item)->show(); gui :: get_widget<Gtk::MenuItem>(item)->show();
else else
lib :: get_widget<Gtk::MenuItem>(item)->hide(); gui :: get_widget<Gtk::MenuItem>(item)->hide();
} }
} }
@ -338,7 +338,7 @@ bool Tab :: on_button_pressed(GdkEventButton *button)
tab_treeview->get_selection()->select(path); tab_treeview->get_selection()->select(path);
on_show_rc_menu(); on_show_rc_menu();
lib :: get_widget<Gtk::Menu>("o_rc_menu")->popup(button->button, button->time); gui :: get_widget<Gtk::Menu>("o_rc_menu")->popup(button->button, button->time);
return true; return true;
} }
@ -440,12 +440,12 @@ static void on_switch_page(Gtk::Widget *page, int num)
tab->tab_runtime_changed(); tab->tab_runtime_changed();
tab->tab_display_sorting(); tab->tab_display_sorting();
} else } else
lib :: get_widget<Gtk::Label>("o_queue_time")->set_text(""); gui :: get_widget<Gtk::Label>("o_queue_time")->set_text("");
} }
void tab_focus_search() void tab_focus_search()
{ {
int page = lib :: get_widget<Gtk::Notebook>("o_notebook")->get_current_page(); int page = gui :: get_widget<Gtk::Notebook>("o_notebook")->get_current_page();
Tab *tab = find_tab(page); Tab *tab = find_tab(page);
if (tab) if (tab)
tab->tab_focus_search(); tab->tab_focus_search();
@ -482,7 +482,7 @@ static void on_add_to_queue(unsigned int n)
static void init_menu_item(const std::string &pq, unsigned int n) static void init_menu_item(const std::string &pq, unsigned int n)
{ {
lib :: get_widget<Gtk::MenuItem>(pq)->signal_activate().connect( gui :: get_widget<Gtk::MenuItem>(pq)->signal_activate().connect(
sigc::bind<unsigned int> (sigc::ptr_fun(on_add_to_queue), n)); sigc::bind<unsigned int> (sigc::ptr_fun(on_add_to_queue), n));
} }
@ -494,16 +494,16 @@ void init_tabs()
cb->on_queue_track_changed = on_track_changed; cb->on_queue_track_changed = on_track_changed;
/* Notebook signals */ /* Notebook signals */
struct Gtk::Notebook *notebook = lib :: get_widget<Gtk::Notebook>("o_notebook"); struct Gtk::Notebook *notebook = gui :: get_widget<Gtk::Notebook>("o_notebook");
notebook->signal_switch_page().connect(sigc::ptr_fun(on_switch_page)); notebook->signal_switch_page().connect(sigc::ptr_fun(on_switch_page));
/* Menu signals */ /* Menu signals */
lib :: get_widget<Gtk::Menu>("o_rc_menu")->show_all(); gui :: get_widget<Gtk::Menu>("o_rc_menu")->show_all();
lib :: get_widget<Gtk::MenuItem>("o_new_pq")->signal_activate().connect( gui :: get_widget<Gtk::MenuItem>("o_new_pq")->signal_activate().connect(
sigc::ptr_fun(on_new_pq)); sigc::ptr_fun(on_new_pq));
lib :: get_widget<Gtk::MenuItem>("o_add_to_favorites")->signal_activate().connect( gui :: get_widget<Gtk::MenuItem>("o_add_to_favorites")->signal_activate().connect(
sigc::ptr_fun(on_add_to_favs)); sigc::ptr_fun(on_add_to_favs));
lib :: get_widget<Gtk::MenuItem>("o_add_to_banned")->signal_activate().connect( gui :: get_widget<Gtk::MenuItem>("o_add_to_banned")->signal_activate().connect(
sigc::ptr_fun(on_add_to_banned)); sigc::ptr_fun(on_add_to_banned));
for (unsigned int i = 0; i < 10; i++) { for (unsigned int i = 0; i < 10; i++) {
std::string pq = "o_pq_"; std::string pq = "o_pq_";
@ -530,7 +530,7 @@ void post_init_tabs()
if ((deck :: get(tab)->has_flag(Q_ENABLED))) if ((deck :: get(tab)->has_flag(Q_ENABLED)))
break; break;
} }
lib :: get_widget<Gtk::Notebook>("o_notebook")->set_current_page(tab); gui :: get_widget<Gtk::Notebook>("o_notebook")->set_current_page(tab);
} }
void cleanup_tabs() void cleanup_tabs()

View File

@ -11,8 +11,8 @@ const std::string appname = "Ocarina ";
static bool on_window_key_pressed(GdkEventKey *event) static bool on_window_key_pressed(GdkEventKey *event)
{ {
Gtk::Notebook *notebook = lib :: get_widget<Gtk::Notebook>("o_notebook"); Gtk::Notebook *notebook = gui :: get_widget<Gtk::Notebook>("o_notebook");
Gtk::Window *window = lib :: get_widget<Gtk::Window>("o_window"); Gtk::Window *window = gui :: get_widget<Gtk::Window>("o_window");
std::string key = gdk_keyval_name(event->keyval); std::string key = gdk_keyval_name(event->keyval);
if (key.size() >= 3) { if (key.size() >= 3) {
@ -58,7 +58,7 @@ static bool on_window_key_released(GdkEventKey *event)
Gtk::Window *window_init() Gtk::Window *window_init()
{ {
Gtk::Window *window = lib :: get_widget<Gtk::Window>("o_window"); Gtk::Window *window = gui :: get_widget<Gtk::Window>("o_window");
window->set_can_focus(); window->set_can_focus();
window->set_title(appname + get_version()); window->set_title(appname + get_version());

View File

@ -9,6 +9,25 @@
/* main.cpp */ /* main.cpp */
namespace gui
{
extern Glib::RefPtr<Gtk::Builder> __O_BUILDER;
template <class T>
static inline T *get_widget(const std::string &name)
{
T *widget;
__O_BUILDER->get_widget(name, widget);
return widget;
}
template <class T>
static inline Glib::RefPtr<T> get_object(const std::string &name)
{
return Glib::RefPtr<T>::cast_static(__O_BUILDER->get_object(name));
}
}
Gtk::Window *ocarina_init(int *, char ***); Gtk::Window *ocarina_init(int *, char ***);

View File

@ -10,23 +10,8 @@
namespace lib namespace lib
{ {
void init(int *, char ***, const std::string &); void init(int *, char ***);
const std::string share_file(const std::string &); const std::string share_file(const std::string &);
Glib::RefPtr<Gtk::Builder> &get_builder();
template <class T>
static inline T *get_widget(const std::string &name)
{
T *widget;
get_builder()->get_widget(name, widget);
return widget;
}
template <class T>
static inline Glib::RefPtr<T> get_object(const std::string &name)
{
return Glib::RefPtr<T>::cast_static(get_builder()->get_object(name));
}
} }

View File

@ -5,7 +5,6 @@
#include <lib/lib.h> #include <lib/lib.h>
#include <gtkmm.h> #include <gtkmm.h>
static Glib::RefPtr<Gtk::Builder> builder;
static std::string share_dir = ""; static std::string share_dir = "";
static void setup_share(const std::string &path) static void setup_share(const std::string &path)
@ -22,21 +21,12 @@ static void setup_share(const std::string &path)
share_dir = share_dir + "share/ocarina/"; share_dir = share_dir + "share/ocarina/";
} }
void lib :: init(int *argc, char ***argv, const std::string &gtk_xml) void lib :: init(int *argc, char ***argv)
{ {
setup_share((*argv)[0]); setup_share((*argv)[0]);
builder = Gtk::Builder::create();
if (!builder->add_from_file(lib :: share_file(gtk_xml)))
exit(1);
} }
const std::string lib :: share_file(const std::string &f) const std::string lib :: share_file(const std::string &f)
{ {
return share_dir + f; return share_dir + f;
} }
Glib::RefPtr<Gtk::Builder> &lib :: get_builder()
{
return builder;
}