Ocarina: Clean up the settings page

The new code is a bit simpler, and it correctly fills in the list of
library paths during startup (I still have no idea why this didn't
happen with the old code).

Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
This commit is contained in:
Bryan Schumaker 2012-01-25 08:20:57 -05:00
parent 808927da45
commit 4b9fde453b
1 changed files with 60 additions and 97 deletions

View File

@ -1,6 +1,6 @@
#include <sstream>
#include <list>
#include <map>
using namespace std;
#include <ocarina/settings.h>
@ -9,20 +9,12 @@ using namespace std;
#include <ocarina/gtk.h>
#include <libsaria/library.h>
#include <libsaria/libpath.h>
#include <libsaria/print.h>
struct PathInfo
{
GtkWidget *widget;
GtkWidget *remove;
GtkWidget *update;
string path;
};
/* This is a gtk vbox */
static GtkWidget *library_settings = NULL;
static GtkWidget *path_panels = NULL;
static list<PathInfo> path_widgets;
static map<GtkWidget *, string> widget_mapping;
static void on_click_add(GtkWidget *b, GdkEvent *e, gpointer d)
{
@ -40,58 +32,12 @@ static void on_click_update(GtkWidget *b, GdkEvent *e, gpointer d)
static void on_click_update_path(GtkWidget *b, GdkEvent *e, gpointer d)
{
list<PathInfo>::iterator it;
for (it = path_widgets.begin(); it != path_widgets.end(); it++) {
if ((*it).update == b) {
libsaria::library::update_path((*it).path);
break;
}
}
libsaria::library::update_path(widget_mapping[b]);
}
static void on_click_remove(GtkWidget *b, GdkEvent *e, gpointer d)
{
list<PathInfo>::iterator it;
for (it = path_widgets.begin(); it != path_widgets.end(); it++) {
if ((*it).remove == b) {
libsaria::library::remove_path((*it).path);
break;
}
}
}
static GtkWidget *make_add_button()
{
return make_text_button(GTK_STOCK_ADD,
"Add to Library",
on_click_add,
true);
}
static GtkWidget *make_update_button()
{
return make_text_button(GTK_STOCK_REFRESH,
"Update Library",
on_click_update,
true);
}
static GtkWidget *make_update_path_button()
{
return make_text_button(GTK_STOCK_REFRESH,
"Update Path",
on_click_update_path,
true);
}
static GtkWidget *make_remove_button()
{
return make_text_button(GTK_STOCK_REMOVE,
"Remove Path",
on_click_remove,
true);
libsaria::library::remove_path(widget_mapping[b]);
}
static GtkWidget *markup_label(string markup)
@ -119,68 +65,85 @@ static GtkWidget *make_path_info(string path, unsigned int size)
return box;
}
static GtkWidget *add_buttons(GtkWidget *info_box, struct PathInfo &path)
static void add_content(GtkWidget *panel, libsaria::LibraryPath *path)
{
GtkWidget *button_box = gtk_hbox_new(FALSE, 0);
path.remove = make_remove_button();
path.update = make_update_path_button();
GtkWidget *content = gtk_hbox_new(FALSE, 0);
GtkWidget *info = make_path_info(path->get_path(), path->get_size());
GtkWidget *update = make_text_button(GTK_STOCK_REFRESH,
"Update Path",
on_click_update_path,
true);
GtkWidget *remove = make_text_button(GTK_STOCK_REMOVE,
"Remove Path",
on_click_remove,
true);
box_pack_start(button_box, info_box, TRUE, TRUE, 0);
box_pack_start(button_box, path.update, FALSE, FALSE, 0);
box_pack_start(button_box, path.remove, FALSE, FALSE, 0);
return button_box;
box_pack_start(content, info, TRUE, TRUE, 0);
box_pack_start(content, update, FALSE, FALSE, 0);
box_pack_start(content, remove, FALSE, FALSE, 0);
box_pack_start(panel, content, TRUE, TRUE, 0);
widget_mapping[update] = path->get_path();
widget_mapping[remove] = path->get_path();
}
static void add_library_panel(GtkWidget *content, struct PathInfo &path)
static void add_library_path(libsaria::LibraryPath *path)
{
GtkWidget *panel_box = gtk_vbox_new(FALSE, 0);
GtkWidget *sep = gtk_hseparator_new();
GtkWidget *panel = gtk_vbox_new(FALSE, 0);
GtkWidget *sep = gtk_hseparator_new();
box_pack_start(panel_box, content, TRUE, TRUE, 0);
box_pack_start(panel_box, sep, TRUE, TRUE, 0);
box_pack_start(path_panels, panel_box, FALSE, FALSE, 0);
add_content(panel, path);
path.widget = panel_box;
path_widgets.push_back(path);
box_pack_start(panel, sep, TRUE, TRUE, 0);
box_pack_start(library_settings, panel, FALSE, FALSE, 0);
}
static void add_library_path(struct libsaria::library::PathInfo &info)
static GtkWidget *get_button_row()
{
struct PathInfo path;
GtkWidget *main_box = gtk_hbox_new(FALSE, 0);
GtkWidget *info_box = make_path_info(info.path, info.size);
GtkWidget *buttons = add_buttons(info_box, path);
GtkWidget *button_row = gtk_hbox_new(FALSE, 0);
GtkWidget *add_button = make_text_button(GTK_STOCK_ADD,
"Add to Library",
on_click_add,
true);
GtkWidget *update_button = make_text_button(GTK_STOCK_REFRESH,
"Update Library",
on_click_update,
true);
path.path = info.path;
box_pack_end(button_row, add_button, FALSE, FALSE, 0);
box_pack_end(button_row, update_button, FALSE, FALSE, 0);
box_pack_start(main_box, buttons, TRUE, TRUE, 0);
add_library_panel(main_box, path);
return button_row;
}
void library_refresh_remove(GtkWidget *widget, gpointer data)
{
gtk_widget_destroy(widget);
}
void library_settings_refresh()
{
while (path_widgets.size() != 0) {
gtk_widget_destroy(path_widgets.front().widget);
path_widgets.pop_front();
libsaria::LibraryPath *path;
gtk_container_foreach(GTK_CONTAINER(library_settings), library_refresh_remove, NULL);
widget_mapping.clear();
libsaria::library::pathiter::reset();
while (!libsaria::library::pathiter::end()) {
path = libsaria::library::pathiter::next();
add_library_path(path);
}
libsaria::library::for_each_path(add_library_path);
box_pack_start(library_settings, get_button_row(), FALSE, FALSE, 0);
gtk_widget_show_all(library_settings);
}
void library_settings_init()
{
GtkWidget *button_row = gtk_hbox_new(FALSE, 0);
library_settings = gtk_vbox_new(FALSE, 0);
path_panels = gtk_vbox_new(FALSE, 0);
libsaria::library::for_each_path(add_library_path);
box_pack_start(library_settings, path_panels, FALSE, FALSE, 0);
box_pack_start(library_settings, button_row, FALSE, FALSE, 0);
box_pack_end(button_row, make_add_button(), FALSE, FALSE, 0);
box_pack_end(button_row, make_update_button(), FALSE, FALSE, 0);
library_settings = gtk_vbox_new(FALSE, 0);
box_pack_start(library_settings, get_button_row(), FALSE, FALSE, 0);
gtk_widget_show_all(library_settings);
add_settings_page("Library", library_settings);
}