from wxPython.wx import *

class taskbarapp:
    def __init__(self, icon):
        self.tbicon = wxTaskBarIcon()
        self.tbicon.SetIcon(icon, "Personal Torrent Collector Ver:"+self.PTC['app']['version'])
        EVT_TASKBAR_LEFT_DCLICK(self.tbicon, self.OnTaskBarActivate)
        EVT_TASKBAR_RIGHT_UP(self.tbicon, self.OnTaskBarMenu)
        EVT_MENU(self.tbicon, self.TBMENU_RESTORE, self.OnTaskBarActivate)
        EVT_MENU(self.tbicon, self.TBMENU_CLOSE, self.OnTaskBarClose)
        EVT_ICONIZE(self, self.OnIconify)
    def OnIconify(self, evt):
        #This makes the app disappear off the taskbar when it is minimized
        self.Hide()
        return
    def OnTaskBarActivate(self, evt):
        if self.IsIconized():
            self.Iconize(false)
        if not self.IsShown():
            self.Show(True)
        self.Raise()
        return
    TBMENU_RESTORE = wxNewId()
    TBMENU_CLOSE   = wxNewId()
    def OnTaskBarMenu(self, evt):
        menu = wxMenu()
        menu.Append(self.TBMENU_RESTORE, "Restore")
        menu.Append(self.TBMENU_CLOSE,   "Close")
        self.tbicon.PopupMenu(menu)
        menu.Destroy()
        
    def OnTaskBarClose(self, evt):
        self.tbicon.RemoveIcon() #Remove the icon from the tray
#        wxPostEvent(self, wxCloseEvent() )
        self.PTC['functions']['QuitProgram']()
        #self.OnClose()#Change this line to point to your quit function
        # - * remember that this is a sibling of the main app and
        # not a child so you can use self.CloseFunction()
        
        # because of the way wxTaskBarIcon.PopupMenu is implemented we have to
        # prod the main idle handler a bit to get the window to actually close
        #wxGetApp().ProcessIdle()
        #self.QuitProgram() # really out from processes


class testFrame(wxFrame,taskbarapp):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, -1, title,
                         style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
        self.icon = wxIcon('systray.ico', wxBITMAP_TYPE_ICO)
        self.SetIcon(self.icon)
        if wxPlatform == '__WXMSW__':
            taskbarapp.__init__(self, self.icon)
        EVT_CLOSE(self, self.OnClose)
    def OnClose(self, event = None):
        self.Destroy()
    
class MyApp(wxApp):
    def __init__(self):
        wxApp.__init__(self)
        
    def OnInit(self):
        frame = testFrame(None, -1,"taskbarapp.py Demo")
        frame.Show(True)
        return True

def main():
    app = MyApp()
    app.MainLoop()
    
if __name__ == '__main__':
    main()
