#!/usr/bin/env python
# [SNIPPET_NAME: gtk3 dialogs]
# [SNIPPET_CATEGORIES: gtk3]
# [SNIPPET_TAGS: widgets, gtk3]
# [SNIPPET_DESCRIPTION: GTK3 font colour and file dialogs]
# [SNIPPET_AUTHOR: Oliver Marks ]
# [SNIPPET_LICENSE: GPL]
from gi.repository import Gtk


class application_gui:
    """Tutorial 06 demo of file, colour and font dialogs."""

    def __init__(self):
        #load in our glade interface
        xml = Gtk.Builder()
        xml.add_from_file('tut06.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['entry'] = xml.get_object('entry2')
        self.widgets['color_button'] = xml.get_object('btnColour')
        self.widgets['font_button'] = xml.get_object('btnFont')
        self.widgets['open_button'] = xml.get_object('btnLoad')
        self.widgets['save_button'] = xml.get_object('btnSave')

        self.widgets['color_button'].connect('clicked', self.colour_selector)
        self.widgets['font_button'].connect('clicked', self.font_selector)
        self.widgets['save_button'].connect('clicked', self.save_file_selector)
        self.widgets['open_button'].connect('clicked', self.open_file_selector)
        
        self.widgets['open_dialog'] = xml.get_object('fileChooserLoad')
        self.widgets['open_dialog'].add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CLOSE)
        self.widgets['open_dialog'].add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
        
        self.widgets['save_dialog'] = xml.get_object('fileChooserSave')
        self.widgets['save_dialog'].add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CLOSE)
        self.widgets['save_dialog'].add_button(Gtk.STOCK_SAVE, Gtk.ResponseType.OK)

        self.widgets['color_dialog'] = xml.get_object('colorSelect')

        self.widgets['font_dialog'] = xml.get_object('fontSelect')

        #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 colour_selector(self, widget):
        self.widgets['color_dialog'].run()
        self.colour = self.widgets['color_dialog'].get_color_selection().get_current_color()
        
        #accessing the individual values as integers, converting to floats here
        colour = [self.colour.red / 65535.0,
                  self.colour.green / 65535.0,
                  self.colour.blue / 65535.0]

        self.widgets['color_dialog'].hide()
        self.text.set_text(self.widgets['color_dialog'].get_name() + ' ' + self.colour.to_string())

    def font_selector(self, widget):
        self.widgets['font_dialog'].run()
        self.font = self.widgets['font_dialog'].get_font_selection()
        self.text.set_text(self.widgets['font_dialog'].get_name() + ' ' + self.font.get_font_name() + ' ' + str(self.font.get_size()))
        self.widgets['font_dialog'].hide()

    def open_file_selector(self, widget):
        response = self.widgets['open_dialog'].run()
        if self.widgets['open_dialog'].get_filenames():
            self.text.set_text(self.widgets['open_dialog'].get_name() + ' '+ str(self.widgets['open_dialog'].get_filenames()))
        else:
            if self.widgets['open_dialog'].get_filename():
                self.text.set_text(self.widgets['open_dialog'].get_name() + ' '+ self.widgets['open_dialog'].get_filename())
        self.widgets['open_dialog'].hide()

    def save_file_selector(self, widget):
        response = self.widgets['save_dialog'].run()
        self.text.set_text(self.widgets['save_dialog'].get_name() + ' '+ self.widgets['save_dialog'].get_filename())
        self.widgets['save_dialog'].hide()


application = application_gui()
Gtk.main()
