Re: gtk_tree_model_iter_next works, what should I do about prev]
- From: David Nečas (Yeti) <yeti physics muni cz>
- To: gtk-app-devel-list gnome org
- Subject: Re: gtk_tree_model_iter_next works, what should I do about prev]
- Date: Fri, 10 Aug 2007 13:39:02 +0200
On Wed, Aug 08, 2007 at 07:32:18AM -0600, Kevin DeKorte wrote:
I've been having some great luck using a GtkListStore, but recently I
wanted to see the previous node on the list. Obviously, the
gtk_tree_model_iter_prev doesn't exist. So what is the best way to get
the previous node.
I tried something like the code below, but path was always NULL and so
it didn't work. Any hints on this?
// global
GtkListStore *playliststore;
GtkTreeIter iter;
// in the callback
GtkTreePath *path;
gchar *filename;
gint count;
gint playlist;
path = gtk_tree_model_get_path(GTK_TREE_MODEL(playliststore),&iter);
if (gtk_tree_path_prev(path)) {
if(gtk_tree_model_get_iter(GTK_TREE_MODEL(playliststore),&iter,path)) {
gtk_tree_model_get(GTK_TREE_MODEL(playliststore), &iter,
ITEM_COLUMN,&filename, COUNT_COLUMN,&count,PLAYLIST_COLUMN,&playlist,-1);
shutdown();
set_media_info(filename);
play_file(filename, playlist);
gtk_list_store_set(playliststore,&iter,COUNT_COLUMN,count+1, -1);
g_free(filename);
}
}
gtk_tree_path_free(path);
This is not selfcontained and compilable, so it's hard to
say why it does not work. In any case the global iter
variable is the prime suspect. Also,
gtk_tree_model_get_path() does not normally return NULL,
doesn't it print any warning to the console?
Anyway, this works (it selects the previous row than you
clicked on):
====================================================================
#include <time.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#include <glib/gstdio.h>
enum {
COLUMN_NAME,
COLUMN_SIZE,
COLUMN_DATE,
N_COLUMNS
};
static void
fill_model(GtkListStore *store)
{
GDir *dir;
if ((dir = g_dir_open(".", 0, NULL))) {
const gchar *name;
while ((name = g_dir_read_name(dir))) {
struct stat st;
if (!g_stat(name, &st)) {
guint64 time, size;
GtkTreeIter iter;
time = st.st_mtime;
size = st.st_size;
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
COLUMN_NAME, name,
COLUMN_SIZE, size,
COLUMN_DATE, time,
-1);
}
/* else something */
}
}
/* else something */
}
static void
render_date(G_GNUC_UNUSED GtkTreeViewColumn *column,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
G_GNUC_UNUSED gpointer data)
{
guint64 i;
time_t time;
struct tm *tm;
gchar s[24];
gtk_tree_model_get(model, iter, COLUMN_DATE, &i, -1);
time = i;
tm = localtime(&time);
strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", tm); /* ISO 8601 */
g_object_set(G_OBJECT(renderer), "text", s, NULL);
}
static void
selection_changed(GtkTreeSelection *selection)
{
static gboolean in_selection_changed = FALSE;
GtkTreeModel *model;
GtkTreePath *path;
GtkTreeIter iter;
if (in_selection_changed
|| !gtk_tree_selection_get_selected(selection, &model, &iter))
return;
in_selection_changed = TRUE;
path = gtk_tree_model_get_path(model, &iter);
if (gtk_tree_path_prev(path)) {
gtk_tree_model_get_iter(model, &iter, path);
gtk_tree_selection_select_iter(selection, &iter);
}
gtk_tree_path_free(path);
in_selection_changed = FALSE;
}
int
main(int argc,
char *argv[])
{
GtkWidget *window, *view, *scrlwindow;
GtkTreeSelection *selection;
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkListStore *store;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 360, 240);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
scrlwindow = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrlwindow),
GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(window), scrlwindow);
store = gtk_list_store_new(N_COLUMNS,
G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
fill_model(store);
view = gtk_tree_view_new();
gtk_container_add(GTK_CONTAINER(scrlwindow), view);
gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(store));
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Name", renderer,
"text", COLUMN_NAME,
NULL);
gtk_tree_view_column_set_expand(column, TRUE);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
renderer = gtk_cell_renderer_text_new();
g_object_set(G_OBJECT(renderer), "xalign", 1.0, NULL);
column = gtk_tree_view_column_new_with_attributes("Size", renderer,
"text", COLUMN_SIZE,
NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Date", renderer,
NULL);
gtk_tree_view_column_set_cell_data_func(column, renderer,
&render_date, NULL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
g_signal_connect(selection, "changed",
G_CALLBACK(selection_changed), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
====================================================================
Yeti
--
http://gwyddion.net/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]