#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
This file is part of LibreLight.
LibreLight is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2 of the License.
LibreLight is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LibreLight.  If not, see .
(c) 2012 micha@uxsrv.de
"""
import random
rnd_id = str(random.randint(1000,9000))
rnd_id += " beta 22.10 "
import subprocess
import string
_gcmd=['git', 'rev-parse', '--short', 'HEAD']
try:
    r = subprocess.check_output(_gcmd)
    rnd_id += r.decode('ascii').strip()
except Exception as e:
    rnd_id += " no git" 
    #rnd_id += " ".join(_gcmd) +str(e)
try:
    xtitle = __file__
except:
    xtitle = "__file__"
if "/" in xtitle:
    xtitle = xtitle.split("/")[-1]
import sys
sys.stdout.write("\x1b]2;"+str(xtitle)+" "+str(rnd_id)+"\x07") # terminal title
import json
import time
import sys
import os 
import _thread as thread
import traceback
import tkinter
import tkinter as tk
from tkinter import font
space_font = None
import tkinter.simpledialog
from idlelib.tooltip import Hovertip
_global_short_key = 1
class Dialog():
    def __init__(self):
        self.d = tkinter.simpledialog
        self._exit = None
        self._cb = self.dummy_cb
    def dummy_cb(self,_return):
        print("dialog.dummy_cb()",self,_return)
        pass
    def askstring(self,title="title",prompt="prompt:",initialvalue=""):
        old = 0
        if old:
            title = "*"+title
            self._exit = self.askstring_old(title=title,prompt=prompt,initialvalue=initialvalue)
            self._cb(self._exit)
        else:
            title = "#"+title
            self.askstring_new(title=title,prompt=prompt,initialvalue=initialvalue)
    
    def askstring_old(self,title="title",prompt="prompt:",initialvalue=""):
        print(self.d)
        print(dir(self.d))
        txt = self.d.askstring(title=title,prompt=prompt,initialvalue=initialvalue)
        return txt
    def _close(self):
        print("dialog._close()",self._exit)
        self.tk.destroy()
    def close(self):
        self._close()
        time.sleep(0.1)
        input_event_blocker.unlock()
        #self._cb(None)
        return self._exit
    def ok(self):
        t=self.e_txt.get()#[:-1]
        self._exit = t
        self._close()
        time.sleep(0.1)
        input_event_blocker.unlock()
        self._cb(self._exit)
    def event(self,event,**args):
        #if _global_short_key == 1 and event.keysym == "Escape":
        #    pass
        if 1:#else:
            input_event_blocker.set( self.e , self.e_txt)
            input_event_blocker.event(event) #,args)
        if "keysym" in dir(event):
            if event.keysym == "Return":# or event.keysym == "Tab" or event.keysym == "ISO_Left_Tab":
                self.ok()
            if _global_short_key == 0:
                if event.keysym == "Escape":
                    self.close()
    def ask_exec_config(self,prompt="",txt="",cfg={},_cb=None):
        print(self,"ask_exec_config()",[txt,cfg])
        self._exit = None
        #    #self.tk.quit()
        #    print(dir(self.tk))
        try:
            self.close()
        except Exception as e:print(e)
        #self.tk = tkinter.Tk()
        self.tk = tkinter.Toplevel()
        #self.tk.withdraw() # do not draw
        self.tk.iconify()
        self.tk.geometry("390x200") #.format(120+c))
        self.tk.title("EXEC-CONFIG" )#+" "+":"+str(rnd_id))
        self.tk.attributes('-topmost',True)
        self.tk.protocol("WM_DELETE_WINDOW", self.close)
        self.tk.resizable(0,0)
        bg = "#e0e"
        bg = "#cd5"
        bg = "lightgrey"
        self.tk["bg"] = bg
        #self.tk.overrideredirect(1)
        #self.tk.attributes('-toolwindow', True)
        #self.tk.state(newstate='iconic')
        self.fo = tk.Frame(self.tk,bd=1) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.fo["bg"] = "red"
        self.fo["bg"] = "#eee"#lightgrey"
        self.fo.pack(side="top")
    
        self.fl = tk.Frame(self.fo,bd=2) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.fl["bg"] = "green"
        self.fl["bg"] = "#eee"#lightgrey"
        self.fl.pack(side="left")
        self.fm = tk.Frame(self.fo,width=20,bd=2) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.fm["bg"] = "#eee"#lightgrey"
        self.fm.pack(side="left",expand=1,fill="y")
        self.fr = tk.Frame(self.fo,bd=2) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.fr["bg"] = "blue"
        self.fr["bg"] = "#eee"#lightgrey"
        self.fr.pack(side="left")
        from_= 33
        to   = 0
        self.ff = tk.Frame(self.fr,bd=2) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.ff["bg"] = "#99a"
        self.ff.pack(side="left")
        self.b = tk.Scale(self.ff,bg="lightblue", width=28,from_=from_,to=to,command=self.event)
        self.b.pack(side="top") #fill=tk.Y, side=tk.TOP)
        self.el = tk.Button(self.ff,text="Master",bg="lightblue",width=4)
        self.el.pack(side="top")
        self.ff = tk.Frame(self.fr,bd=2) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.ff["bg"] = "#99a"
        self.ff.pack(side="left")
        self.b = tk.Scale(self.ff,bg="lightblue", width=28,from_=from_,to=to,command=self.event)
        self.b.pack(side="top") #fill=tk.Y, side=tk.TOP)
        self.el = tk.Button(self.ff,text="Speed",bg="lightblue",width=4)
        self.el.pack(side="top")
        self.ff = tk.Frame(self.fr,bd=2) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.ff["bg"] = "#99a"
        self.ff.pack(side="left")
        self.b = tk.Scale(self.ff,bg="lightblue", width=28,from_=from_,to=to,command=self.event)
        self.b.pack(side="top") #fill=tk.Y, side=tk.TOP)
        self.el = tk.Button(self.ff,text="Size",bg="lightblue",width=4)
        self.el.pack(side="top")
        #self.f = tk.Frame(self.fl) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        #self.f.pack(side="top")
        #self.elx = tk.Label(self.f,text="")
        #self.elx["bg"] = bg
        #self.elx.pack(side="left")
        self.f = tk.Frame(self.fl) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.f.pack(side="top")
        self.el = tk.Label(self.f,text=str("NR:"),anchor="w",width=10)
        self.el["bg"] = bg
        self.el.pack(side="left",expand=1,fill="y")
        self.el = tk.Label(self.f,text=str(prompt),anchor="w",width=10)
        self.el["bg"] = bg
        self.el.pack(side="left",expand=1,fill="y")
        self.f = tk.Frame(self.fl) #, highlightbackground = "lightgrey", highlightthickness = 1, bd=0)
        self.f.pack(side="top")
        
        self.f2 = tk.Frame(self.f) 
        self.f2.pack(side="top",expand=1,fill="y")
        self.e_txt = tk.StringVar()
        self.el = tk.Label(self.f2,text="Name",anchor="w",width=10)
        self.el.pack(side="left")
        self.e = tk.Entry(self.f2,textvariable=self.e_txt,width=10)
        self.e["bg"] = "#eee"
        self.e.config(highlightthickness=2)
        self.e.config(highlightcolor= "red")
        #self.e_txt.set(str(initialvalue)+"<")
        self.e_txt.set(str(""))
        #self.e.icursor(999)
        #self.e.selection_range(0, 999)#"end")
        self.e.bind("",self.event)
        self.e.bind("