Re: Dynamic changing values at a GtkTreeStore with pointers



On Wed, Mar 21, 2007 at 03:01:39PM -0300, Diogo Ramos wrote:
> This question is cracking my head off. I am days at it and i can't figure it
> out, although I thing a got pretty close.
> Here is the deal:
> I have a GtkTreeStore that I use to show some values.
> This values can be changed.
> So, my idea is to store a pointer at a cell so, every time I change the
> value of my data, the data is changed at the GtkTreeStore throw the pointer.
> I don't know if it's possible, but I am trying, :-)
> My last idea was change the GtkCellRendererText using
> gtk_tree_view_column_set_cell_data_func so, every time the render is called,
> a function of mine would translate a pointer, which is stored in the tree,
> to a text. But it didn't work. It wont stop complaining.
> Here is the menssage: GLib-GObject-WARNING **: unable to set property `text'
> of type `gchararray' from value of type `gpointer'
> I think it's because I set G_TYPE_POINTER at the model but the render I am
> using is for text, although I change the value when It has to be rendered.

If I understand what you are trying to achieve (some code
would be a better description), you probably call

  gtk_tree_view_column_add_attribute(column, renderer, "text", id);

*in addition* to setting up a cell data function.  In that
case don't.  It tells the tree view to attempt to set the
"text" property itself -- which inevitably fails as it
doesn't know how to make a string from a pointer.

On the other hand, if `translate' means just type-cast, then
you should use a G_TYPE_STRING column directly.

Anyway, working code using a G_TYPE_POINTER model column
with a cell data function to render text in
GtkCellRendererText view columns is attached.

Yeti

--
http://gwyddion.net/


============================================================================
#include <gtk/gtk.h>

static void
render_name(G_GNUC_UNUSED GtkCellLayout *layout,
            GtkCellRenderer *renderer,
            GtkTreeModel *model,
            GtkTreeIter *iter,
            G_GNUC_UNUSED gpointer user_data)
{
    GParamSpec *prop;

    gtk_tree_model_get(model, iter, 0, &prop, -1);
    g_object_set(renderer, "text", prop->name, NULL);
}

static void
render_value_type(G_GNUC_UNUSED GtkCellLayout *layout,
                  GtkCellRenderer *renderer,
                  GtkTreeModel *model,
                  GtkTreeIter *iter,
                  G_GNUC_UNUSED gpointer user_data)
{
    GParamSpec *prop;

    gtk_tree_model_get(model, iter, 0, &prop, -1);
    g_object_set(renderer, "text", g_type_name(prop->value_type), NULL);
}

static void
render_owner_type(G_GNUC_UNUSED GtkCellLayout *layout,
                  GtkCellRenderer *renderer,
                  GtkTreeModel *model,
                  GtkTreeIter *iter,
                  G_GNUC_UNUSED gpointer user_data)
{
    GParamSpec *prop;

    gtk_tree_model_get(model, iter, 0, &prop, -1);
    g_object_set(renderer, "text", g_type_name(prop->owner_type), NULL);
}

static void
add_column(GtkTreeView *treeview,
           const gchar *title,
           GtkCellLayoutDataFunc func,
           gpointer func_data)
{
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;

    renderer = gtk_cell_renderer_text_new();
    g_object_set(renderer, "family", "Monospace", NULL);
    column = gtk_tree_view_column_new();
    gtk_tree_view_column_set_title(column, title);
    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(column), renderer, TRUE);
    gtk_cell_layout_set_cell_data_func(GTK_CELL_LAYOUT(column), renderer,
                                       func, func_data, NULL);
    gtk_tree_view_append_column(GTK_TREE_VIEW(treeview), column);
}

int
main(int argc, char *argv[])
{
    GtkWidget *window, *treeview, *scwin;
    GtkListStore *store;
    GtkTreeIter iter;
    GParamSpec **props;
    GObjectClass *klass;
    GType type;
    guint i, n;

    gtk_init(&argc, &argv);
    type = GTK_TYPE_TREE_VIEW;

    store = gtk_list_store_new(1, G_TYPE_POINTER);
    klass = G_OBJECT_CLASS(g_type_class_ref(type));
    props = g_object_class_list_properties(klass, &n);
    for (i = 0; i < n; i++)
        gtk_list_store_insert_with_values(store, &iter, G_MAXINT,
                                          0, props[i],
                                          -1);
    g_free(props);
    g_type_class_unref(klass);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), -1, 400);
    gtk_window_set_title(GTK_WINDOW(window), g_type_name(type));
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    scwin = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scwin),
                                   GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
    gtk_container_add(GTK_CONTAINER(window), scwin);

    treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
    gtk_container_add(GTK_CONTAINER(scwin), treeview);
    add_column(GTK_TREE_VIEW(treeview), "Name", render_name, NULL);
    add_column(GTK_TREE_VIEW(treeview), "Value Type", render_value_type, NULL);
    add_column(GTK_TREE_VIEW(treeview), "Owner Type", render_owner_type, NULL);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}




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