Re: Treeview columns used only for sorting

On Sat, Jun 25, 2005 at 11:17:57PM +0300, Razvan Gavril wrote:
I'm kind of new to gtk programming and have a question about how to do a
tree view sorting. I have a treeview that displays a list of files. In that
treeview i have 2 columns: one that display the date when the file was last
modified and one that display it's size. I want to be able to sort this
treeview according to this 2 columns.

The way i do it now is like this: the treemodel has columns for size(hold
the size in bytes) and date(holds a t_time value) that are hidden in the
treeview, i use this 2 columns just to do the sorting, but also has 2 more
columns that display the human readable size and date. I would like to use
only 2 columns in the treemodel, the visible ones, and still be able to do
the sorting. Rereading the size and date when sorting is not an option. The
only idea that i have so far was to make my custom cell renderer for size
and date but i got stuck and i wanted to know if it isn't any simpler method
for what i want.

Using a cell data func (see gtk_tree_view_column_set_cell_data_func())
and/or sort func (see gtk_tree_sortable_set_sort_func())
you can override both/either presentation and comparison
method of the underlying data.  See attached simple example
that stores both time and size in raw, directly comparable
form and uses a cell data func to format date -- as you can
see that's fairly simple.


A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

#include <time.h>
#include <sys/stat.h>
#include <gtk/gtk.h>
#include <glib/gstdio.h>

enum {

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,
            /* 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);

main(int argc,
     char *argv[])
    GtkWidget *window, *view, *scrlwindow;
    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_container_add(GTK_CONTAINER(window), scrlwindow);

    store = gtk_list_store_new(N_COLUMNS,
                               G_TYPE_STRING, G_TYPE_INT64, G_TYPE_INT64);
    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));
                                         COLUMN_NAME, GTK_SORT_ASCENDING);

    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Name", renderer,
                                                      "text", COLUMN_NAME,
    gtk_tree_view_column_set_expand(column, TRUE);
    gtk_tree_view_column_set_sort_column_id(column, COLUMN_NAME);
    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,
    gtk_tree_view_column_set_sort_column_id(column, COLUMN_SIZE);
    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,
    gtk_tree_view_column_set_cell_data_func(column, renderer,
                                            &render_date, NULL, NULL);
    gtk_tree_view_column_set_sort_column_id(column, COLUMN_DATE);
    gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);


    return 0;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]