Re: TreeView and Window resize?

On Sat, May 19, 2007 at 11:44:01AM -0700, OrangeCocktail wrote:
> I have got a question regarding the use of the Treeview.
> How do I make the TreeView resize when I resize the window of the
> application? I have put a TreeView inside a ScrolledWindow. As long as there
> is no model associated with the TreeView, the widget expands correctly when
> resizing the window. But once I have created a model and added it to the
> TreeView, the widget doesn't expand anymore, it's size stays fixed. How do I
> make a TreeVew resize together with the window, so that it fills all
> available space?

The attached code shows that a treeview packed as you
describe does not behave as you describe.  So, maybe you
pack the scrolled window into some other container without
expand & fill flags (thgough that would not explain the
modelless behaviour), maybe... who knows, there are too many
possible combinations of policies, modes and packing flags.
A compilable code worths a thousand words of description.



#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_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);
    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]