#! /usr/bin/env python
#
# GUI module generated by PAGE version 4.7
# In conjunction with Tcl version 8.6
#    Mar 22, 2016 10:04:14 AM
import sys

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

try:
    import ttk
    py3 = 0
except ImportError:
    import tkinter.ttk as ttk
    py3 = 1

import androidtool_support


def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = Tk()
    androidtool_support.set_Tk_var()
    top = Android_Tools(root)
    androidtool_support.init(root, top)
    #ubuntutool_support.init(root, top)
    root.mainloop()

w = None


def create_Android_Tools(root, *args, **kwargs):
    '''Starting point when module is imported by another program.'''
    global w, w_win, rt
    rt = root
    w = Toplevel(root)
    androidtool_support.set_Tk_var()
    top = Android_Tools(w)
    androidtool_support.init(w, top, *args, **kwargs)

    return (w, top)


def destroy_Android_Tools():
    global w
    w.destroy()
    w = None


class Android_Tools:

    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#d9d9d9' # X11 color: 'gray85'
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.', background=_bgcolor)
        self.style.configure('.', foreground=_fgcolor)
        self.style.configure('.', font="TkDefaultFont")
        self.style.map('.', background=
            [('selected', _compcolor), ('active', _ana2color)])

        top.geometry("700x600+1129+678")
        top.title("Android Tools")
        top.configure(highlightcolor="black")

        self.TL_select = ttk.Label(top)
        self.TL_select.place(relx=0.01, rely=0.02, height=19, width=147)
        self.TL_select.configure(background=_bgcolor)
        self.TL_select.configure(foreground="#000000")
        self.TL_select.configure(relief=FLAT)
        self.TL_select.configure(text='''Please select target devices:''')

        #self.TB_refreshdev = ttk.Button(top)
        #self.TB_refreshdev.place(relx=0.31, rely=0.03, height=28, width=74)
        #self.TB_refreshdev.configure(command=androidtool_support.refresh_devices)

        #self.TB_refreshdev.configure(takefocus="")
        #self.TB_refreshdev.configure(text='''Refresh''')

        self.TC_devices = ttk.Combobox(top, postcommand=androidtool_support.refresh_devices)
        self.TC_devices.place(relx=0.01, rely=0.05, relheight=0.04, relwidth=0.3)

        self.TC_devices.configure(textvariable=androidtool_support.combobox)
        self.TC_devices.configure(takefocus="")
        self.TC_devices.bind("<<ComboboxSelected>>", androidtool_support.newselection)

        self.TL_devicesinfo = ttk.Label(top)
        self.TL_devicesinfo.place(relx=0.44, rely=0.02, height=21, relwidth=0.45)
        self.TL_devicesinfo.configure(background=_bgcolor)
        self.TL_devicesinfo.configure(foreground="#000000")
        self.TL_devicesinfo.configure(relief=FLAT)
        self.TL_devicesinfo.configure(textvariable= androidtool_support.device_info)
        self.TL_devicesinfo.configure(width=193)

        self.List_local = ScrolledListBox(top)
        self.List_local.place(relx=0.01, rely=0.12, relheight=0.86, relwidth=0.45)
        self.List_local.configure(background="white")
        self.List_local.configure(font="TkFixedFont")
        self.List_local.configure(highlightcolor="#d9d9d9")
        self.List_local.configure(selectbackground="#c4c4c4")
        self.List_local.configure(width=10)
        self.List_local.bind("<Double-Button-1>", androidtool_support.double_click)

        self.List_device = ScrolledListBox(top)
        self.List_device.place(relx=0.54, rely=0.12, relheight=0.86, relwidth=0.45)
        self.List_device.configure(background="white")
        self.List_device.configure(font="TkFixedFont")
        self.List_device.configure(highlightcolor="#d9d9d9")
        self.List_device.configure(selectbackground="#c4c4c4")
        self.List_device.configure(width=10)
        self.List_device.bind("<Double-Button-1>", androidtool_support.double_click2)

        self.TB_push = ttk.Button(top)
        self.TB_push.place(relx=0.47, rely=0.13, height=38, width=44)
        self.TB_push.configure(command=androidtool_support.push_file)
        self.TB_push.configure(takefocus="")
        self.TB_push.configure(text='''->''')

        self.TB_pull = ttk.Button(top)
        self.TB_pull.place(relx=0.47, rely=0.22, height=38, width=44)
        self.TB_pull.configure(command=androidtool_support.pull_file)
        self.TB_pull.configure(takefocus="")
        self.TB_pull.configure(text='''<-''')

        self.TB_refreshdir = ttk.Button(top)
        self.TB_refreshdir.place(relx=0.47, rely=0.3, height=38, width=44)
        self.TB_refreshdir.configure(command=androidtool_support.refresh_dir)
        self.TB_refreshdir.configure(takefocus="")
        self.TB_refreshdir.configure(text='''@''')

        self.TB_install = ttk.Button(top)
        self.TB_install.place(relx=0.47, rely=0.38, height=38, width=44)
        self.TB_install.configure(command=androidtool_support.install_apk)
        self.TB_install.configure(takefocus="")
        self.TB_install.configure(text='''X''')

        self.TL_localpath = ttk.Label(top)
        self.TL_localpath.place(relx=0.01, rely=0.09, height=19, relwidth=0.44)
        self.TL_localpath.configure(background=_bgcolor)
        self.TL_localpath.configure(foreground="#000000")
        self.TL_localpath.configure(relief=FLAT)
        self.TL_localpath.configure(text='''Local Path:''')

        self.TL_devicepath = ttk.Label(top)
        self.TL_devicepath.place(relx=0.54, rely=0.09, height=19, relwidth=0.44)
        self.TL_devicepath.configure(background=_bgcolor)
        self.TL_devicepath.configure(foreground="#000000")
        self.TL_devicepath.configure(relief=FLAT)
        self.TL_devicepath.configure(text='''Device Path:''')

        self.TProgressbar1 = ttk.Progressbar(top)
        self.TProgressbar1.place(relx=0.44, rely=0.05, relwidth=0.54,
                                 relheight=0.0, height=19)
        self.TProgressbar1.configure(length="0")

        self.TB_Browse = ttk.Button(top)
        self.TB_Browse.place(relx=0.31, rely=0.03, height=28, width=74)
        self.TB_Browse.configure(takefocus="")
        self.TB_Browse.configure(text='''Browse Dir''')
        self.TB_Browse.configure(command=androidtool_support.browse_dir)
        self.TB_Browse.configure(width=64)


# The following code is added to facilitate the Scrolled widgets you specified.

class AutoScroll(object):
    '''Configure the scrollbars for a widget.'''

    def __init__(self, master):
        #  Rozen. Added the try-except clauses so that this class
        #  could be used for scrolled entry widget for which vertical
        #  scrolling is not supported. 5/7/14.
        try:
            vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
        except:
            pass
        hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)

        #self.configure(yscrollcommand=self._autoscroll(vsb),
        #    xscrollcommand=self._autoscroll(hsb))
        try:
            self.configure(yscrollcommand=self._autoscroll(vsb))
        except:
            pass
        self.configure(xscrollcommand=self._autoscroll(hsb))

        self.grid(column=0, row=0, sticky='nsew')
        try:
            vsb.grid(column=1, row=0, sticky='ns')
        except:
            pass
        hsb.grid(column=0, row=1, sticky='ew')

        master.grid_columnconfigure(0, weight=1)
        master.grid_rowconfigure(0, weight=1)

        # Copy geometry methods of master  (taken from ScrolledText.py)
        if py3:
            methods = Pack.__dict__.keys() | Grid.__dict__.keys() \
                  | Place.__dict__.keys()
        else:
            methods = Pack.__dict__.keys() + Grid.__dict__.keys() \
                  + Place.__dict__.keys()

        for meth in methods:
            if meth[0] != '_' and meth not in ('config', 'configure'):
                setattr(self, meth, getattr(master, meth))

    @staticmethod
    def _autoscroll(sbar):
        '''Hide and show scrollbar as needed.'''

        def wrapped(first, last):
            first, last = float(first), float(last)
            if first <= 0 and last >= 1:
                sbar.grid_remove()
            else:
                sbar.grid()
            sbar.set(first, last)
        return wrapped

    def __str__(self):
        return str(self.master)


def _create_container(func):
    '''Creates a ttk Frame with a given master, and use this new frame to
    place the scrollbars and the widget.'''

    def wrapped(cls, master, **kw):
        container = ttk.Frame(master)
        return func(cls, container, **kw)
    return wrapped


class ScrolledListBox(AutoScroll, Listbox):
    '''A standard Tkinter Text widget with scrollbars that will
    automatically show/hide as needed.'''

    @_create_container
    def __init__(self, master, **kw):
        Listbox.__init__(self, master, **kw)
        AutoScroll.__init__(self, master)

if __name__ == '__main__':
    vp_start_gui()
