Skip to content

Commit

Permalink
gtkui: Save and restore column widths on startup, and seamlessly sync…
Browse files Browse the repository at this point in the history
…hronize them between playlists. Note that this will only work with GTK+ 3.8.
  • Loading branch information
jlindgren90 committed Jan 15, 2013
1 parent 9bb6812 commit 71f39cd
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/gtkui/columns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

Expand Down
59 changes: 51 additions & 8 deletions src/gtkui/ui_playlist_notebook.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* the use of this software.
*/

#include "config.h"
#include <stdlib.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
Expand All @@ -27,9 +27,11 @@
#include <audacious/misc.h>
#include <audacious/playlist.h>
#include <audacious/plugin.h>
#include <libaudcore/hook.h>
#include <libaudgui/list.h>
#include <libaudgui/libaudgui.h>

#include "config.h"
#include "gtkui.h"
#include "ui_playlist_notebook.h"
#include "ui_playlist_widget.h"
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
34 changes: 34 additions & 0 deletions src/gtkui/ui_playlist_widget.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <audacious/i18n.h>
#include <audacious/misc.h>
#include <audacious/playlist.h>
#include <libaudcore/audstrings.h>
#include <libaudgui/libaudgui.h>
#include <libaudgui/libaudgui-gtk.h>
#include <libaudgui/list.h>
Expand Down Expand Up @@ -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]);
}
}
5 changes: 5 additions & 0 deletions src/gtkui/ui_playlist_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 71f39cd

Please sign in to comment.