From 6c5412a3d57991d13b4314fdf346863580602e5f Mon Sep 17 00:00:00 2001 From: Bryan Schumaker Date: Thu, 27 Oct 2011 17:13:54 -0400 Subject: [PATCH] ocarina: Move songlist init code into a new file Doing it all in one function was messy and hard to follow. I've broken it into several smaller functions, so now I know what is going on. --- include/ocarina/songlist.h | 3 +- ocarina/songlist/init.cpp | 94 +++++++++++++++++++++++++++++++++++ ocarina/songlist/songlist.cpp | 63 +---------------------- ocarina/songlist/songlist.h | 6 +++ 4 files changed, 103 insertions(+), 63 deletions(-) create mode 100644 ocarina/songlist/init.cpp create mode 100644 ocarina/songlist/songlist.h diff --git a/include/ocarina/songlist.h b/include/ocarina/songlist.h index 9ffb28bf..fa2d5d5c 100644 --- a/include/ocarina/songlist.h +++ b/include/ocarina/songlist.h @@ -28,10 +28,9 @@ class SongList GtkWidget *treeview; GtkListStore *liststore; - GtkCellRenderer *text_cell; + GtkCellRenderer *textcell; void set_label_text(); - void add_column(unsigned int); void freeze(); void thaw(); diff --git a/ocarina/songlist/init.cpp b/ocarina/songlist/init.cpp new file mode 100644 index 00000000..c464ec2c --- /dev/null +++ b/ocarina/songlist/init.cpp @@ -0,0 +1,94 @@ + +#include +#include "songlist.h" + +SongList::SongList() {} +SongList::~SongList() {} + +static string columns[7] = { "Id", "#", "Title", "Length", "Artist", "Album", "Year" }; +static int col_width[7] = { 2, 20, 300, 60, 125, 125, 50 }; + +static GtkWidget *setup_label(string text) +{ + GtkWidget *label = gtk_label_new(text.c_str()); + gtk_label_set_angle(GTK_LABEL(label), 90); + return label; +} + +static GtkCellRenderer *setup_renderer() +{ + GtkCellRenderer *textcell = gtk_cell_renderer_text_new(); + gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(textcell), 1); + return textcell; +} + +static GtkListStore *setup_liststore() +{ + GtkListStore *liststore; + liststore = gtk_list_store_new(7, G_TYPE_LONG, G_TYPE_INT, G_TYPE_STRING, + G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, + G_TYPE_INT); + return liststore; +} + +static GtkWidget *setup_treeview(GtkListStore *liststore) +{ + GtkWidget *treeview = gtk_tree_view_new(); + + GTK_CONNECT(treeview, "row-activated", songlist_row_activated, NULL); + gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE); + gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore)); + return treeview; +} + +static GtkWidget *setup_window(GtkWidget *treeview) +{ + GtkWidget *window = gtk_scrolled_window_new(NULL, NULL); + gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + container_add(window, treeview); + return window; +} + +static void add_column(GtkCellRenderer *textcell, GtkWidget *treeview, int index) +{ + GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( + columns[index].c_str(), textcell, + "text", index, NULL); + gtk_tree_view_column_set_resizable(column, TRUE); + gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED); + gtk_tree_view_column_set_min_width(column, 2); + gtk_tree_view_column_set_max_width(column, 700); + gtk_tree_view_column_set_fixed_width(column, col_width[index]); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); +} + +static void setup_columns(GtkCellRenderer *textcell, GtkWidget *treeview) +{ + for (unsigned int i = 0; i < 7; i++) + add_column(textcell, treeview, i); +} + +/* + * I need an init() function to set up the scrolled window AFTER + * the gtk_init() function has been called. This allows the songlst + * to be used as a static global variable in other files. + */ +void SongList::init(string text, SongListFuncs *funcs) +{ + /* Initialize helper variables */ + list_funcs = funcs; + name = text; + + /* Set up the gtk widgets */ + label = setup_label(name); + textcell = setup_renderer(); + liststore = setup_liststore(); + treeview = setup_treeview(liststore); + window = setup_window(treeview); + setup_columns(textcell, treeview); + + gtk_widget_show(label); + gtk_widget_show_all(window); +} diff --git a/ocarina/songlist/songlist.cpp b/ocarina/songlist/songlist.cpp index 20fc0e8f..5840442e 100644 --- a/ocarina/songlist/songlist.cpp +++ b/ocarina/songlist/songlist.cpp @@ -6,19 +6,9 @@ using namespace std; #include #include #include +#include "songlist.h" -SongList::SongList() -{ -} - -SongList::~SongList() -{ -} - -string columns[7] = { "Id", "#", "Title", "Length", "Artist", "Album", "Year" }; -int col_width[7] = { 2, 20, 300, 60, 125, 125, 50 }; - -static void songlist_row_activated(GtkTreeView *treeview, GtkTreePath *path, +void songlist_row_activated(GtkTreeView *treeview, GtkTreePath *path, GtkTreeViewColumn *column, gpointer data) { GtkTreeIter iter; @@ -31,55 +21,6 @@ static void songlist_row_activated(GtkTreeView *treeview, GtkTreePath *path, libsaria::library::play_id(inode); } -void SongList::add_column(unsigned int index) -{ - GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes( - columns[index].c_str(), text_cell, - "text", index, NULL); - gtk_tree_view_column_set_resizable(column, TRUE); - gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED); - gtk_tree_view_column_set_min_width(column, 2); - gtk_tree_view_column_set_max_width(column, 700); - gtk_tree_view_column_set_fixed_width(column, col_width[index]); - gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column); -} - -/* - * I need an init() function to set up the scrolled window AFTER - * the gtk_init() function has been called. - */ -void SongList::init(string text, SongListFuncs *funcs) -{ - list_funcs = funcs; - name = text; - - window = gtk_scrolled_window_new(NULL, NULL); - gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window), - GTK_POLICY_AUTOMATIC, - GTK_POLICY_AUTOMATIC); - - treeview = gtk_tree_view_new(); - text_cell = gtk_cell_renderer_text_new(); - gtk_cell_renderer_text_set_fixed_height_from_font( - GTK_CELL_RENDERER_TEXT(text_cell), 1); - GTK_CONNECT(treeview, "row-activated", songlist_row_activated, NULL); - - liststore = gtk_list_store_new(7, G_TYPE_LONG, G_TYPE_INT, G_TYPE_STRING, - G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, - G_TYPE_INT); - for (unsigned int i = 0; i < 7; i++) - add_column(i); - - gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(treeview), TRUE); - gtk_tree_view_set_model(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(liststore)); - container_add(window, treeview); - gtk_widget_show_all(window); - - label = gtk_label_new(text.c_str()); - gtk_label_set_angle(GTK_LABEL(label), 90); - gtk_widget_show(label); -} - GtkWidget *SongList::get_window() { return window; diff --git a/ocarina/songlist/songlist.h b/ocarina/songlist/songlist.h new file mode 100644 index 00000000..1fd00f26 --- /dev/null +++ b/ocarina/songlist/songlist.h @@ -0,0 +1,6 @@ +#ifndef OCARINA_SONGLIST_PRIVATE_H +#define OCARINA_SONGLIST_PRIVATE_H + +void songlist_row_activated(GtkTreeView *, GtkTreePath *, GtkTreeViewColumn *, gpointer); + +#endif /* OCARINA_SONGLIST_PRIVATE_H */