from gi.repository import Gtk
from gi.repository import GObject

class MyModel(GObject.Object, Gtk.TreeModel):
    def __init__(self):
        GObject.Object.__init__(self)
        self.myiter = Gtk.TreeIter()
        self.myiter.stamp = 0
        print '__init__', self.myiter, self.myiter.stamp
        self.striter = 0
        self.path = Gtk.TreePath(0)

    def do_get_n_columns(self):
        return 1

    def do_get_column_type(self, idx):
        return str

    def do_get_iter(self, path):
        print "do get iter", path, type(path)
        #self.path = path
        #self.myiter = Gtk.TreeIter()
        #print 'created iter', self.myiter
        #self.myiter.stamp = 0
        return (True, self.myiter)

    def do_iter_has_child(self, iter):
        print 'has_child', iter
        return False

    def do_iter_next(self, handle):
        print 'iter next'
        return (False, None)

    def do_iter_previous(self, handle):
        print 'iter previous'
        return (False, None)

    def do_get_flags(self):
        """
        Returns the GtkTreeModelFlags for this particular type of model
        See Gtk.TreeModel
        """
        print "get_flags"
        return Gtk.TreeModelFlags.LIST_ONLY | Gtk.TreeModelFlags.ITERS_PERSIST

    def do_get_iter_first(self):
        print 'get iter first'

    def do_get_path(self, handle):
        """
        Return the tree path 
        """
        print "do get_path", handle, handle.stamp
        return self.path

    def do_get_iter_from_string(self, string):
        print '***do_get_iter_from_string****'

    def do_get_value(self, handle, col):
        """
        See Gtk.TreeModel. 
        col is the model column that is needed, not the visible column!
        """
        print 'do get_value', handle, handle.stamp, col
        #b.set_data('text', 'test')
        #return self._convert_value(handle, col, b)
        #return self._convert_value(handle, col, 'test')
        return "Foo"

    def do_iter_children(self, handle):
        """
        Return the first child of the node
        See Gtk.TreeModel
        """
        print "iter_children"
        return (False, None)

    def do_iter_n_children(self, handle):
        """
        See Gtk.TreeModel
        """
        print 'iter_n_children', handle
        if handle is None:
            return 1
        else:
            return 0
    
    def do_iter_nth_child(self, handle, nth):
        """
        See Gtk.TreeModel
        """
        print 'iter_nth_child', handle, nth
        if handle is None:
            return (True, self.myiter)
        else:
            return (False, None)

    def do_iter_parent(self, handle):
        """
        Returns the parent of this node
        See Gtk.TreeModel
        """
        print 'iter_parent', handle
        return (False, None)

    def do_ref_node(self, iter):
        print 'ref node'

    def do_unref_node(self, iter):
        print 'unref node'

mymodel = MyModel()

##view = Gtk.TreeView.new_with_model(mymodel)
view = Gtk.TreeView(model=mymodel)

col = Gtk.TreeViewColumn()
col.set_title("col")
renderer = Gtk.CellRendererText()
col.pack_start(renderer, True)
col.add_attribute(renderer, "text", 0)
view.append_column(col)

w = Gtk.Window()
w.add(view)
w.show_all()
Gtk.main()
