# test GTK3+ GtkTreeModel in PyGI
from gi.repository import Gtk

class Base():
    _COL_ADDRESS = 0
    _COL_TITLE = 1

    def __init__(self):
        self.window = Gtk.Window()
        self.window.connect("delete-event", self.__quit_cb)
        self.window.show()

        self.store = Gtk.ListStore(str, str)
        self.view = Gtk.TreeView()
        self.view.props.headers_visible = False

        self.view.set_model(self.store)
        self.window.add(self.view)        
        self.view.show()

        column = Gtk.TreeViewColumn()
        self.view.append_column(column)

        cell = Gtk.CellRendererText()
        column.pack_start(cell, True)
        column.add_attribute(cell, 'text', self._COL_ADDRESS)

        cell = Gtk.CellRendererText()
        column.pack_start(cell, True)
        column.add_attribute(cell, 'text', self._COL_TITLE)

        # some data to get the item selected
        # each append returns an iter
        self.store.append(['Test 1', 'http://test1.com'])
        self.store.append(['Test 2', 'http://test2.com'])
        self.store.append(['Test 3', 'http://test3.com'])
        self.store.append(['Test 4', 'http://test4.com'])

        self.selection = self.view.get_selection()
        self.selection.connect("changed", self.__selection_changed_cb)

    def main(self):
        Gtk.main()

    def __quit_cb(self, *args):
        Gtk.main_quit()
        
    def __selection_changed_cb(self, *args):
        model, selected = self.selection.get_selected()
        
        if selected:
            next_selection = model.iter_next(selected)
            previous_selection = model.iter_previous(selected)
            
            print 'next: %s' % next_selection
            print 'previous: %s' % previous_selection


if __name__ == '__main__':
    base = Base()
    base.main()
