#!/usr/bin/env python
from gi.repository import Gtk


class application_gui:
    """Tutorial 04 text input, spin input, drop down options"""

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut04.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['spin1'] = xml.get_object('spinbutton1')
        self.widgets['spin2'] = xml.get_object('spinbutton2')
        
        #simple dropdown just text
        self.widgets['comboboxtext'] = xml.get_object('comboboxtext1')
        
        #more complex multi row multi data types in dropdown using renderers
        self.widgets['combobox'] = xml.get_object('combobox1')
        self.widgets['combobox_liststore']= xml.get_object('liststore1')
        #self.widgets['combobox_cell_renderer']= xml.get_object('liststore1')

        #add some items to the drop down 
        self.widgets['combobox_liststore'].append(['new row test'])
        self.widgets['combobox_liststore'].append(['second row test'])

        #adding new rows to the dropdown
        self.widgets['comboboxtext'].append_text('row4')
        self.widgets['comboboxtext'].append_text('row5')

        #connect to events
        self.widgets['comboboxtext'].connect('changed', self.dropdown_event_text)
        self.widgets['spin1'].connect('value-changed', self.spin_button_event)
        self.widgets['spin2'].connect('value-changed', self.spin_button_event)
        self.widgets['combobox'].connect('changed', self.dropdown_event)
        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_all()

    def dropdown_event_text(self, widget):
        self.text.set_text(widget.get_active_text())

    def dropdown_event(self, widget):
        list_view_model = widget.get_model()
        active_iter_index = widget.get_active()
        row_iter = list_view_model.get_iter(active_iter_index)
        self.text.set_text(widget.get_name() + ' ' +list_view_model.get_value(row_iter, 0 ))

    def spin_button_event(self, widget):
        self.text.set_text(widget.get_name() + ' ' + ' ' + str(widget.get_value()))


application = application_gui()
Gtk.main()
