import wx
import wx.lib.mixins.listctrl  as  listmix
from TBIcon import DemoTaskBarIcon

class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    def __init__(self, parent, ID, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=0):
        wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
        listmix.ListCtrlAutoWidthMixin.__init__(self)

class Reminder(wx.App):
    def __init__(self, alerts='', redirect=False):
        wx.App.__init__(self, redirect)
        
        self.frame = wx.Frame(None, -1, title='Reminder', size=(450,295),
                              style=wx.STAY_ON_TOP
                              |wx.SYSTEM_MENU
                              |wx.CAPTION
                              |wx.CLOSE_BOX
                              |wx.MINIMIZE_BOX)
        self.panel = wx.Panel(self.frame, -1)

        # icon code:
        try:            
            self.tbicon = DemoTaskBarIcon(self.frame)            
            print 'iconized!'
        except Exception, e:
            print 'Exception => %s' % e
            self.tbicon = None

        # bind close event to make window "x" get rid of icon
        self.frame.Bind(wx.EVT_CLOSE, self.onClose) 
            
        self.alertdata = alerts
        print 'alerts: ', self.alertdata
        # create the interface
        self.createWidgets(self.panel)
        self.layout(self.panel)
        self.frame.Show(True)
        
    def createWidgets(self, p):        
        self.list_ctrl = TestListCtrl(p, -1,
                                      style=wx.LC_REPORT
                                      |wx.BORDER_SUNKEN
                                      )
        
        self.list_ctrl.InsertColumn(0, 'Subject')
        self.list_ctrl.InsertColumn(1, 'Due')
        self.list_ctrl.InsertColumn(2, 'Location', width=125)
        self.list_ctrl.InsertColumn(3, '', width=0) 
        self.list_ctrl.InsertColumn(4, '', width=0) 
        index = 0
        items = self.alertdata
        
        for data in items:
            print items[data][0]
            self.list_ctrl.InsertStringItem(index, items[data][0])            
            self.list_ctrl.SetStringItem(index, 1, items[data][1])
            index += 1
            
        self.list_ctrl.SetColumnWidth(0, 200) # Subject column width
        self.list_ctrl.SetColumnWidth(1, 100)  # Due column width
        self.closeBtn = wx.Button(p, -1, 'Close')
        self.Bind(wx.EVT_BUTTON, self.onClose, self.closeBtn)        
        
    def layout(self, p):
        ''' Layout the widgets in the window '''
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 1, wx.EXPAND)
        mainSizer.Add(sizer, 0, wx.ALL|wx.EXPAND, 5)
        mainSizer.Add(self.closeBtn, 0, wx.ALL, 5)        
        p.SetSizer(mainSizer)

    def onClose(self, event):
        '''
        Closes the program and deletes the icon from the system tray.
        '''
        print 'in onClose'
        try:            
            self.tbicon.Destroy()
            wx.GetApp().ProcessIdle()            
        except:
            pass        
        self.frame.Destroy()

if __name__ == '__main__':
    alertdata = {
            1: ['Birthday', '1178825456.5309999'],
            2: ['Meeting', '1178830556']
            }
    #print alertdata    
    app = Reminder(alertdata)
    app.MainLoop()   
        
