import wx
import threading
import Queue
import win32api, win32con

class SplashMessage ( wx.Frame ):

    def __init__( self, parent, msg, showtime):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 381,116 ), style = wx.STAY_ON_TOP|wx.DOUBLE_BORDER )
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.close, self.timer)
        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        bSizer1.SetMinSize( wx.Size( 1,3 ) )
        self.m_staticText1 = wx.StaticText( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText1.Wrap( -1 )
        bSizer1.Add( self.m_staticText1, 0, wx.ALL|wx.EXPAND, 5 )
        self.m_staticText4 = wx.StaticText( self, wx.ID_ANY, '  %s  ' % msg, wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
        self.m_staticText4.Wrap( -1 )
        self.m_staticText4.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), 70, 90, 92, False, wx.EmptyString ) )
        bSizer1.Add( self.m_staticText4, 0, wx.ALL|wx.EXPAND, 5 )
        self.m_staticText3 = wx.StaticText( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.m_staticText3.Wrap( -1 )
        bSizer1.Add( self.m_staticText3, 0, wx.ALL|wx.EXPAND, 5 )
        self.SetSizer( bSizer1 )
        bSizer1.Fit(bSizer1.GetContainingWindow())
        self.Layout()
        self.Centre( wx.BOTH )
        self.showtime = showtime
        self.timer.Start(showtime)

    def close(self, event):
        self.Close()
        self.Destroy()


class wxAppThread(threading.Thread):

    def __init__(self):
        super(wxAppThread, self).__init__()
        self.messageq = Queue.Queue()
        self.resultq = Queue.Queue()
        self.abortrequest = threading.Event()
        self.app = None
        self.lock = threading.Lock()
        self.lock.acquire()

    def run(self):
        self.app = wx.App(0, useBestVisual=True)
        self.lock.release()
        while not self.abortrequest.is_set():
            try:
                message = self.messageq.get(True, 0.05)
                if message[0] == 1:
                    pass
                elif message[0] == 2:
                    self.lock.acquire()
                    splash = SplashMessage(None, message[1], message[2])
                    splash.Show()
                    self.lock.release()
                    self.app.MainLoop()
                elif message[0] == 3:
                    pass
                elif message[0] == 4:
                    self.lock.acquire()
                    print 'Sleeping Display'
                    x = win32api.PostMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
                    self.lock.release()
            except Queue.Empty:
                continue
        wx.App_CleanUp()

    def abort(self):
        self.abortrequest.set()
        super(wxAppThread, self).join()

    def showSplashMessage(self, msg, showtime=1000):
        self.messageq.put([2, msg, showtime])

    def sleepDisplay(self):
        self.messageq.put([4,])

if __name__ == '__main__':
    import time
    app = wxAppThread()
    app.start()
    app.showSplashMessage('Test Starting')
    time.sleep(2)
    app.sleepDisplay()
    time.sleep(10)
    app.showSplashMessage('Testing Resleep', 1000)
    while True:
        time.sleep(.25)
    app.abort()
    pass