#!/usr/bin/python
# test TextCtrl with AutoComplete

import wx   # http://wxpython.org/download.php - 2.9 minimum

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")

class AutoTextCtrl(wx.TextCtrl):

    def __init__(self, *args, **kwargs):
        items = kwargs.pop('items', [])    # defaults to empty list
        self.items = items
        autocomplete = kwargs.pop('autocomplete',False) # default to False
        keybinder = kwargs.pop('keybinder')             # required
        
        wx.TextCtrl.__init__(self, *args, **kwargs)
        if autocomplete:
            self.AutoComplete(self.items)
        self.Bind(keybinder,self._OnKey)
        
    def _OnKey(self, evt):
        key = evt.GetKeyCode()
        print('KeyCode={0}'.format(key))
        if key == 13 :
            print "Enter Pressed"
            if len(self.GetValue()) == 0:
                shell.SendKeys("{DOWN}")
            ##how to popup list of choice automatically ?
        evt.Skip()

class _TestWindow(wx.Frame):

    def __init__(self,parent):
        self.formname = 'test window'
        wx.Frame.__init__(self, parent, wx.ID_ANY, self.formname)
        self.Bind(wx.EVT_CLOSE, self.onClose)

        self.panel = wx.Panel(self)

        self.vbox = wx.BoxSizer(wx.VERTICAL)
        items=[
            '124 North Market St, Frederick, MD, USA', 'ABC Text', 'Sidhu', 'Sandra',
            'Santha cruz', 'Keys', 'Sibu Test', 'Particulars', 'Viswanathan Anand', 'Abdukalam Azad',
            '748 SW Bay Blvd, Newport, OR, USA', '30 Germania St, Boston, MA, USA',
            ]
        # tuples are (autocomplete,keybinder)
        self.trials = [(True,'wx.EVT_KEY_DOWN'),(True,'wx.EVT_CHAR'),(False,'wx.EVT_KEY_DOWN'),(False,'wx.EVT_CHAR')]
        hbox = {}
        st = {}
        self.tc={}
        autocomplete = True
        keybinderstr = 'wx.EVT_KEY_DOWN'
        keybinder = eval(keybinderstr)
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        st = wx.StaticText(self.panel, label='AutoComplete={0}, {1}'.format(autocomplete,keybinderstr))
        hbox.Add(st, proportion=1, flag=wx.RIGHT, border=8)
        self.tc= AutoTextCtrl(self.panel,
                              autocomplete=autocomplete,
                              keybinder=keybinder,
                              style=wx.TE_PROCESS_ENTER,
                              items=items)
        hbox.Add(self.tc, proportion=3, border=8)
        self.vbox.Add(hbox, flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.TOP, border=10)
        self.vbox.Add((-1, 10))

        self.Bind(wx.EVT_TEXT_ENTER, self.onEnter)

        self.panel.SetSizerAndFit(self.vbox)
        self.Fit()
        self.Show()
    def onEnter(self, evt):
        print('wx.EVT_TEXT_ENTER')

    def onClose(self, evt):
        self.tc.Destroy()
        self.Destroy()

class _MyApp(wx.App):

    def __init__(self):
        wx.App.__init__(self, False)
        self.frame = _TestWindow(None)

def test():

    app = _MyApp()
    app.MainLoop()

if __name__ == "__main__":
    test()

