#!/usr/bin/env python
# [SNIPPET_NAME: gtk3 app chooser and scale widget]
# [SNIPPET_CATEGORIES: gtk3]
# [SNIPPET_TAGS: widgets, gtk3]
# [SNIPPET_DESCRIPTION: GTK3 scale widget and app chooser example]
# [SNIPPET_AUTHOR: Oliver Marks ]
# [SNIPPET_LICENSE: GPL]
from gi.repository import Gtk, GLib, Gio


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

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut07.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['scale1'] = xml.get_object('scalebutton1')
        self.widgets['scale2'] = xml.get_object('scalebutton2')
        self.widgets['appchooseraudio'] = xml.get_object('appchooserbutton1')
        self.widgets['appchoosertext'] = xml.get_object('appchooserbutton2')

        self.widgets['appchooseraudio'].connect('changed', self.app_chooser)
        self.widgets['appchoosertext'].connect('changed', self.app_chooser)
        self.widgets['scale1'].connect('value-changed', self.scale)
        self.widgets['scale2'].connect('value-changed', self.scale)

        #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()

    def app_chooser(self, widget):
        list_view_model = widget.get_model()
        active_iter_index = widget.get_active()
        row_iter = list_view_model.get_iter(active_iter_index)
        app_info = list_view_model.get_value(row_iter, 0)
        
        gio_file = Gio.File.new_for_path('/tmp/tut07-appchooser-test.txt')
        app_info.launch((gio_file,), None)
        self.text.set_text(widget.get_name() + ' ' + app_info.get_name())

    def scale(self, widget, value):
        self.text.set_text(widget.get_name() + ' ' + str(value))


application = application_gui()
Gtk.main()
