Re: Storing GObject in GtkListStore



On Wed, 2006-08-02 at 15:51 +0200, Tomasz Jankowski wrote:
Hi!

Hey Tomasz!

Generally it isn't a problem, I'm only nosy :P I'd like to know if there is
some other (more recommended) way to store, for example GObject in
GtkListStore/GtkTreeStore. So far I do it, by creating column with type
G_TYPE_INT and storing there pointer to object.

Use G_TYPE_OBJECT if you want it to work behind for example a language
binding.

Checkout tinymail for this.

https://svn.tinymail.org/svn/tinymail/trunk/libtinymail-test/tinymail-python-test.py

What you see here is a GObject (actually a GTypeInterface) of the type
TnyMsgHeaderIface and in this case an implementation called TnyMsgHeader
being put in the store model, which is of type TnyMsgHeaderListModel.
This list-model type implements GtkTreeModel and TnyListIface.

I assume you at some point want language bindings to work, else you
perhaps shouldn't develop GObject/C but rather pick a higher programming
language like C#, D or Python, Ruby, whatevery (but that's a personal
opinion, remove this last line .. hehe)

def on_headerstree_selected (treeselection, msgview) :
        model, iter = treeselection.get_selected ()
        if iter:
                header = model.get_value (iter, tinymail.uigtk.MSG_HEADER_LIST_MODEL_INSTANCE_COLUMN)
                if header:
                        folder = header.get_folder ()
                        msg = folder.get_message (header)
                        msgview.set_msg (msg)

If you don't use G_TYPE_OBJECT, you would get a PyGTK error/warning here.

You can see the "INSTANCE_COLUMN" in tny-msg-header-list-model.c

https://svn.tinymail.org/svn/tinymail/trunk/libtinymailui-gtk/tny-msg-header-list-model.c

static GType
tny_msg_header_list_model_get_column_type (GtkTreeModel *self, gint column)
{
...
    case TNY_MSG_HEADER_LIST_MODEL_INSTANCE_COLUMN:
                        retval = G_TYPE_OBJECT;
                        break;
...
}

It's a custom tree model. But you can also do this with a GtkListStore implementation of GtkTreeModel.

In tinymail you can see such a GtkTreeModel at 

https://svn.tinymail.org/svn/tinymail/trunk/libtinymailui-gtk/tny-attach-list-model.c

and

https://svn.tinymail.org/svn/tinymail/trunk/libtinymailui-gtk/tny-account-tree-model.c

For example look at this:

static void
tny_account_tree_model_instance_init (GTypeInstance *instance, gpointer g_class)
{
        GtkTreeStore *store = (GtkTreeStore*) instance;
        TnyAccountTreeModel *me = (TnyAccountTreeModel*) instance;
        static GType types[] = { G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_OBJECT };

        me->iterator_lock = g_mutex_new ();

        gtk_tree_store_set_column_types (store, 
                TNY_ACCOUNT_TREE_MODEL_N_COLUMNS, types);

        return;
}





-- 
Philip Van Hoof, software developer at x-tend 
home: me at pvanhoof dot be 
gnome: pvanhoof at gnome dot org 
work: vanhoof at x-tend dot be 
http://www.pvanhoof.be - http://www.x-tend.be




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