#+TITLE: GTK-3 Treeview in liststore mode
#+DATE: 2014-04-12 12:00:00 UTC
#+DESCRIPTION: Example of using a treeview, the treeview display can be adjusted using different store models in this case we use the liststore model.
#+FILETAGS: GTK-3:python:widgets
#+CATEGORY: python
#+SLUG: 09-treeview-liststore-mode
#+THUMBNAIL: ../../../images/gtk/tut09-treeview-listview.png
#+BEGIN_COMMENT
.. title: GTK-3 Treeview in liststore mode
.. slug: 09-treeview-liststore-mode
.. date: 2014-04-12 12:00:00 UTC
.. tags: GTK-3, python, widgets
.. category: python
.. description: Example of using a treeview, the treeview display can be adjusted using different store models in this case we use the liststore model.
.. type: text
#+END_COMMENT

#+CAPTION: Treeview in list mode 
[[../../../images/gtk/tut09-treeview-listview.png]]

The below example demonstrates using the treeview to display table style data, it generates a few rows and shows row selection from a mouse click.")

#+BEGIN_SRC python
#!/usr/bin/env python
from gi.repository import Gtk, GLib, GObject


class application_gui:
    """Tutorial 09 treeview in list mode."""
    count = 0

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut09.glade')

        #grab our widget using get_object this is the name of the widget from glade, window1 is the default name
        self.window = xml.get_object('window1')
        self.text = xml.get_object('entry1')

        #load our widgets from the glade file
        self.widgets = {}
        self.widgets['treeview'] = xml.get_object('treeview1')
        treeview(self.widgets['treeview'], self.text)

        #connect to events, in this instance just quit our application
        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        #show the window else there is nothing to see :)
        self.window.show()


class treeview:
    treeview = None
    treemodel = None

    selected = 'workspace'

    def __init__(self, treeview, entry):
        self.entry = entry
        self.treeview = treeview

        self.treeview.connect('row-activated', self.selection)
        self.treeview.connect('button_press_event', self.mouse_click)

        #create a storage model in this case a treemodel
        self.treemodel = Gtk.ListStore(str, str, str)
        self.treeview.set_model(self.treemodel)

        #add columns usually only one in case of the treeview
        column1 = Gtk.TreeViewColumn("Column 01")
        self.treeview.append_column(column1)
        
        column2 = Gtk.TreeViewColumn("Column 02")
        self.treeview.append_column(column2)
        
        column3 = Gtk.TreeViewColumn("Column 03")
        self.treeview.append_column(column3)

        #add in a text renderer so we can see the items we add 
        cell = Gtk.CellRendererText()
        column1.pack_start(cell, False)
        column1.add_attribute(cell, "text", 0)
        
        cell = Gtk.CellRendererText()
        column2.pack_start(cell, False)
        column2.add_attribute(cell, "text", 0)
        
        cell = Gtk.CellRendererText()
        column3.pack_start(cell, False)
        column3.add_attribute(cell, "text", 0)
        
        self.populate()
        self.menu()

    def populate(self):
        self.treemodel.clear()
        #populate the treeview with some list items
        for item1 in range(0, 5):
            iter_level_1 = self.append_tree('Item ' + str(item1))

    def append_tree(self, name, parent=None):
        """
            append to the treeview if parent is null append to root level.
            if parent is a valid iter (possibly returned from previous append) then append under the parent
        """
        myiter = self.treemodel.insert_after(parent, None)
        self.treemodel.set_value(myiter, 0, name)
        return myiter

    def menu(self):
        """
        popover menu shown on right clicking a treeview item.
        """
        self.treeview_menu = Gtk.Menu()
        for item in range(0, 5):
            menu_item = Gtk.MenuItem("Menu " + str(item))
            self.treeview_menu.append(menu_item)

    def mouse_click(self, tv, event):
        if event.button == 3:
            # right mouse button pressed popup the menu
            self.treeview_menu.show_all()
            self.treeview_menu.popup(None, None, None, None, 1, 0)

    def selection(self, tv, treepath, tvcolumn):
        """ 
            on double click get the value of the item we clicked
        """
        model = tv.get_model()
        treeiter = model.get_iter(treepath)
        self.selected = model.get_value(treeiter, 0)
        self.entry.set_text(self.selected)
        


application = application_gui()
Gtk.main()
#+END_SRC

