import wxversion
#wxversion.select("2.7")
wxversion.select("2.8")
import wx
import threading
import time

from pywinauto import application

n = "ComboBoxTest"

class WorkerThread(threading.Thread):
    def __init__(self, handle):
        threading.Thread.__init__(self)
        self.handle = handle
        
    def run(self):
        app = application.Application()
        app.connect_(handle=self.handle)
        dlg = app[n]
        
        # let's see what else this thing can see - dumps to stdout
        dlg.PrintControlIdentifiers()

        # now use those names to get the combo box
        combo_box = dlg.ComboBox
        text = dlg.Static
        
        #
        # scenario #1 - open the pull down with a click, and close it with a click
        #
        
        # type something 
        combo_box.TypeKeys("lose_this_typing")
        time.sleep(2)
        # click to pull down the pull-down
        combo_box.Click(coords=(190,10))
        time.sleep(2)
        # click it again to bring it back up w/o selecting anything
        combo_box.Click(coords=(190,10))
        time.sleep(2)
        
        #
        # scenario #2 - open pull-down and click elsewhere
        #
        
        # type something 
        combo_box.TypeKeys("lose_this_typing_again")
        time.sleep(2)
        # click to pull down the pull-down
        combo_box.Click(coords=(190,10))
        time.sleep(2)
        # put the focus elsewhere
        text.SetFocus()
        
        #
        # scenarion #3 - down arrow doesn't open pull-down (up-arrow too)
        # Note: Notepad doesn't do this either
        #
        
        combo_box.TypeKeys("lose_this_typing_yet_again")
        time.sleep(2)
        combo_box.TypeKeys("{DOWN}")
        time.sleep(2)
        combo_box.TypeKeys("{DOWN}")

        # if you want to play with the dialog after the test, just comment out the close.
        #dlg.Close()
        

class ComboBoxTest(wx.Frame):
    
    def __init__(self, parent=None):
        wx.Frame.__init__(self,
                          parent=parent,
                          id=wx.ID_ANY,
                          title=n,
                          pos=(-1,-1),
                          size=(-1,-1),
                          style=wx.DEFAULT_FRAME_STYLE,
                          name=n)
        panel = wx.Panel(self)
        self.status_bar = self.CreateStatusBar()
        text_label = wx.StaticText(panel, wx.ID_ANY, n, style=wx.ALIGN_RIGHT)
        cb = wx.ComboBox(panel, wx.ID_ANY, value="testing", choices=['one','two','three'], size=(200,-1))
        
        cb.Bind(wx.EVT_MOTION, self.OnMotion)
        
        hs = wx.BoxSizer(wx.HORIZONTAL)
        hs.Add(text_label,    proportion=0, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL,           border=10)
        hs.Add(cb,            proportion=1, flag=wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_LEFT |wx.ALL|wx.EXPAND, border=10)
        
        panel.SetSizer(hs)
        hs.Fit(self)
        
        thread = WorkerThread(self.GetHandle())
        thread.start()
        
    def OnMotion(self, event):
        self.status_bar.SetStatusText("Position %s" % str(event.GetPositionTuple()))
        event.Skip()

if __name__ == '__main__':
    print "wx version: " + wx.VERSION_STRING
    app = wx.PySimpleApp()
    frame = ComboBoxTest()
    frame.Show(True)
    app.MainLoop()
