import os
import subprocess
import sys

import gobject
import gtk
import ooldtp


def do_ldtp():
    app = ooldtp.context("frmcheckrow")
    app.checkrow("tblTV", 0, 1)


def make_example():
    def on_toggled(renderer, path, store):
        iter_ = store.get_iter(path)
        store.set_value(iter_, 1, not store[iter_][1])
    vbox = gtk.VBox()
    model = gtk.ListStore(str, bool)
    treeview = gtk.TreeView(model)
    treeview.get_accessible().set_name("TV")
    for i in range(3):
        model.append(("Check box %d" % i, False))
    text_renderer = gtk.CellRendererText()
    toggle_renderer = gtk.CellRendererToggle()
    toggle_renderer.connect("toggled", on_toggled, model)
    treeview.append_column(gtk.TreeViewColumn("Label", text_renderer, text=0))
    treeview.append_column(gtk.TreeViewColumn("Checkbox", toggle_renderer, active=1))
    window = gtk.Window()
    vbox.add(treeview)
    button = gtk.Button("Check first row")
    button.connect("clicked", lambda b: gobject.spawn_async([sys.executable, __file__, "ldtp"]))
    vbox.add(button)
    window.add(vbox)
    window.show_all()
    window.set_title("checkrow")
    window.connect("destroy", lambda x: gtk.main_quit())
    gtk.main()

if __name__ == "__main__":
    if len(sys.argv) > 1 and sys.argv[1] == "ldtp":
        do_ldtp()
    else:
        make_example()
