From 71f39cd4b2b0976c6812ccf538472fa125f28ca8 Mon Sep 17 00:00:00 2001 From: John Lindgren Date: Tue, 15 Jan 2013 18:59:23 -0500 Subject: [PATCH] gtkui: Save and restore column widths on startup, and seamlessly synchronize them between playlists. Note that this will only work with GTK+ 3.8. --- src/gtkui/columns.c | 3 ++ src/gtkui/ui_playlist_notebook.c | 59 +++++++++++++++++++++++++++----- src/gtkui/ui_playlist_widget.c | 34 ++++++++++++++++++ src/gtkui/ui_playlist_widget.h | 5 +++ 4 files changed, 93 insertions(+), 8 deletions(-) diff --git a/src/gtkui/columns.c b/src/gtkui/columns.c index 00deffbb7e..3cacc7a0f8 100644 --- a/src/gtkui/columns.c +++ b/src/gtkui/columns.c @@ -212,6 +212,9 @@ static void response_cb (GtkWidget * widget, gint response, void * unused) pw_cols[pw_num_cols] = ((Column *) index_get (chosen, pw_num_cols)) ->column; + aud_set_string ("gtkui", "column_widths", ""); + aud_set_string ("gtkui", "column_expand", ""); + ui_playlist_notebook_populate (); } diff --git a/src/gtkui/ui_playlist_notebook.c b/src/gtkui/ui_playlist_notebook.c index b944d1957b..cb99e5fc40 100644 --- a/src/gtkui/ui_playlist_notebook.c +++ b/src/gtkui/ui_playlist_notebook.c @@ -17,7 +17,7 @@ * the use of this software. */ -#include "config.h" +#include #include #include @@ -27,9 +27,11 @@ #include #include #include +#include #include #include +#include "config.h" #include "gtkui.h" #include "ui_playlist_notebook.h" #include "ui_playlist_widget.h" @@ -123,6 +125,33 @@ GtkNotebook *ui_playlist_get_notebook(void) return GTK_NOTEBOOK(notebook); } +static void save_column_widths () +{ + int current = gtk_notebook_get_current_page ((GtkNotebook *) notebook); + GtkWidget * treeview = playlist_get_treeview (current); + + char * widths, * expand; + ui_playlist_widget_get_column_widths (treeview, & widths, & expand); + + aud_set_string ("gtkui", "column_widths", widths); + aud_set_string ("gtkui", "column_expand", expand); + + free (widths); + free (expand); +} + +static void apply_column_widths (GtkWidget * treeview) +{ + char * widths = aud_get_string ("gtkui", "column_widths"); + char * expand = aud_get_string ("gtkui", "column_expand"); + + if (widths && widths[0] && expand && expand[0]) + ui_playlist_widget_set_column_widths (treeview, widths, expand); + + free (widths); + free (expand); +} + static void tab_title_reset(GtkWidget *ebox) { GtkWidget *label = g_object_get_data(G_OBJECT(ebox), "label"); @@ -169,10 +198,10 @@ static gboolean tab_button_press_cb(GtkWidget *ebox, GdkEventButton *event, gpoi static void tab_changed (GtkNotebook * notebook, GtkWidget * page, gint page_num, void * unused) { - GtkWidget * treeview = playlist_get_treeview (page_num); + save_column_widths (); + apply_column_widths (playlist_get_treeview (page_num)); - if (treeview != NULL) - aud_playlist_set_active (page_num); + aud_playlist_set_active (page_num); } static void tab_reordered(GtkNotebook *notebook, GtkWidget *child, guint page_num, gpointer user_data) @@ -233,6 +262,8 @@ void ui_playlist_notebook_create_tab(gint playlist) vscroll = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scrollwin)); treeview = ui_playlist_widget_new(playlist); + apply_column_widths (treeview); + g_object_set_data(G_OBJECT(scrollwin), "treeview", treeview); gtk_container_add(GTK_CONTAINER(scrollwin), treeview); @@ -320,6 +351,11 @@ void ui_playlist_notebook_empty (void) static void add_remove_pages (void) { + g_signal_handlers_block_by_func (notebook, (void *) tab_changed, NULL); + g_signal_handlers_block_by_func (notebook, (void *) tab_reordered, NULL); + + save_column_widths (); + gint lists = aud_playlist_count (); gint pages = gtk_notebook_get_n_pages ((GtkNotebook *) notebook); @@ -333,9 +369,7 @@ static void add_remove_pages (void) /* do we have an orphaned treeview? */ if (aud_playlist_by_unique_id (tree_id) < 0) { - g_signal_handlers_block_by_func (notebook, (void *) tab_changed, NULL); gtk_notebook_remove_page ((GtkNotebook *) notebook, i); - g_signal_handlers_unblock_by_func (notebook, (void *) tab_changed, NULL); pages --; continue; } @@ -362,9 +396,7 @@ static void add_remove_pages (void) /* found it? move it to the right place */ if (tree_id == list_id) { - g_signal_handlers_block_by_func (notebook, (void *) tab_reordered, NULL); gtk_notebook_reorder_child ((GtkNotebook *) notebook, page, i); - g_signal_handlers_unblock_by_func (notebook, (void *) tab_reordered, NULL); found = TRUE; break; } @@ -385,6 +417,13 @@ static void add_remove_pages (void) ui_playlist_notebook_create_tab (pages); pages ++; } + + int active = aud_playlist_get_active (); + apply_column_widths (playlist_get_treeview (active)); + gtk_notebook_set_current_page ((GtkNotebook *) notebook, active); + + g_signal_handlers_unblock_by_func (notebook, (void *) tab_changed, NULL); + g_signal_handlers_unblock_by_func (notebook, (void *) tab_reordered, NULL); } void ui_playlist_notebook_update (void * data, void * user) @@ -461,6 +500,8 @@ void ui_playlist_notebook_set_playing (void * data, void * user) static void destroy_cb (void) { + hook_dissociate ("config save", (HookFunction) save_column_widths); + notebook = NULL; switch_handler = 0; reorder_handler = 0; @@ -472,6 +513,8 @@ GtkWidget * ui_playlist_notebook_new (void) gtk_notebook_set_scrollable ((GtkNotebook *) notebook, TRUE); make_add_button (notebook); + hook_associate ("config save", (HookFunction) save_column_widths, NULL); + g_signal_connect (notebook, "destroy", (GCallback) destroy_cb, NULL); return notebook; } diff --git a/src/gtkui/ui_playlist_widget.c b/src/gtkui/ui_playlist_widget.c index 1c61ae5cd1..49ff2174e4 100644 --- a/src/gtkui/ui_playlist_widget.c +++ b/src/gtkui/ui_playlist_widget.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -477,3 +478,36 @@ void ui_playlist_widget_scroll (GtkWidget * widget) else popup_hide (data); } + +void ui_playlist_widget_get_column_widths (GtkWidget * widget, char * * widths, + char * * expand) +{ + int w[pw_num_cols], ex[pw_num_cols]; + + for (int i = 0; i < pw_num_cols; i ++) + { + GtkTreeViewColumn * col = gtk_tree_view_get_column ((GtkTreeView *) widget, i); + w[i] = gtk_tree_view_column_get_fixed_width (col); + ex[i] = gtk_tree_view_column_get_expand (col); + } + + * widths = int_array_to_string (w, pw_num_cols); + * expand = int_array_to_string (ex, pw_num_cols); +} + +void ui_playlist_widget_set_column_widths (GtkWidget * widget, + const char * widths, const char * expand) +{ + int w[pw_num_cols], ex[pw_num_cols]; + + if (! string_to_int_array (widths, w, pw_num_cols) || + ! string_to_int_array (expand, ex, pw_num_cols)) + return; + + for (int i = 0; i < pw_num_cols; i ++) + { + GtkTreeViewColumn * col = gtk_tree_view_get_column ((GtkTreeView *) widget, i); + gtk_tree_view_column_set_fixed_width (col, w[i]); + gtk_tree_view_column_set_expand (col, ex[i]); + } +} diff --git a/src/gtkui/ui_playlist_widget.h b/src/gtkui/ui_playlist_widget.h index b415e4b566..4150b7b2e9 100644 --- a/src/gtkui/ui_playlist_widget.h +++ b/src/gtkui/ui_playlist_widget.h @@ -29,6 +29,11 @@ void ui_playlist_widget_update (GtkWidget * widget, gint type, gint at, gint count); void ui_playlist_widget_scroll (GtkWidget * widget); +void ui_playlist_widget_get_column_widths (GtkWidget * widget, char * * widths, + char * * expand); +void ui_playlist_widget_set_column_widths (GtkWidget * widget, + const char * widths, const char * expand); + enum {PW_COL_NUMBER, PW_COL_TITLE, PW_COL_ARTIST, PW_COL_YEAR, PW_COL_ALBUM, PW_COL_TRACK, PW_COL_GENRE, PW_COL_QUEUED, PW_COL_LENGTH, PW_COL_PATH, PW_COL_FILENAME, PW_COL_CUSTOM, PW_COL_BITRATE, PW_COLS};