#!/usr/bin/python

import pango
import gobject
import gtk
import time

class ExecutionTime(object):
    """ helper to make very crude performance measure """
    def __init__(self, info=""):
        self.info = info
    def __enter__(self):
        self.now = time.time()
    def __exit__(self, type, value, stack):
        while gtk.events_pending():
            gtk.main_iteration()
        print "%s: %s" % (self.info, time.time() - self.now)

def on_cursor_changed(treeview):
    (model, it) = treeview.get_selection().get_selected()
    prev = treeview.get_data("prev")
    if prev:
        model[prev][0] = "non active"
    model[it][0] = "active\nso active"
    treeview.set_data("prev", it)

if __name__ == "__main__":
    # setup model
    store = gtk.ListStore(str, bool)

    treeview = gtk.TreeView()
    cr = gtk.CellRendererText()
    column = gtk.TreeViewColumn("foo", cr, text=0)
    column.set_fixed_width(200)
    column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    treeview.append_column(column)
    treeview.connect("cursor-changed", on_cursor_changed)
    treeview.show()

    scroll = gtk.ScrolledWindow()
    scroll.add(treeview)
    scroll.show()
    win = gtk.Window()
    win.add(scroll)
    win.set_default_size(300,300)
    win.show()

    treeview.set_fixed_height_mode(True)
    #treeview.set_property("hacked-almost-fixed-height-mode", True)
    treeview.set_model(store)
    while gtk.events_pending():
        gtk.main_iteration()

    with ExecutionTime("normal mode (add)"):
        for i in range(1000):
            store.append(["%.20i" % i, False])
        while gtk.events_pending():
            gtk.main_iteration()

    with ExecutionTime("normal mode (change)"):
        for i in range(1000):
            store[i][0] = "%.20i" % (1000-i)
        while gtk.events_pending():
            gtk.main_iteration()

    gtk.main()

