from wxPython.wx import *
import os, string

ID_ABOUT = 101
ID_EXIT  = 102

class ComboBoxPanel(wxPanel):
    def __init__(self, parent, ID = -1, size = wxDefaultSize, style= -1):
        wxPanel.__init__(self, parent, ID, wxDefaultPosition,size,style=wxSUNKEN_BORDER)

        self.SetBackgroundColour("DARKBLUE")

        self.List=['Select Command','00', '06', '09', '10', '1D', '5A', '0B']

        #self.outputbox =wxButton(self, 10, "", wxDefaultPosition)
        #self.outputbox=wxStaticText(self, -1, "Hi", style=wxALIGN_CENTRE)
        self.outputbox = wxTextCtrl(self, -1, "",size=wxDefaultSize)
        #EVT_BUTTON(self, 10, self.OnClick)
        print "outputbox size:",self.outputbox.GetBestSize()
        combolabel = wxStaticText(self,-1,"Please select a command:")
        print "label size:", combolabel.GetBestSize()

        combo=wxComboBox(self, 30,
                         "Select Command",
                         wxPoint(wxALIGN_LEFT),
                         wxDefaultSize,
                         self.List,wxCB_DROPDOWN)
        print "combo size:",combo.GetBestSize()
        EVT_COMBOBOX(self, 30, self.CommandCallback)

        box = wxBoxSizer(wxVERTICAL)
        box.Add(self.outputbox, 2, wxEXPAND|wxALL, 5)
        box.Add(combolabel, 1, wxEXPAND|wxALL, 5)
        box.Add(combo, 1, wxEXPAND|wxALL, 5)

        self.SetAutoLayout(true)
        self.SetSizer(box)
        box.Fit(self)
        self.Layout()
        print "label size:", combolabel.GetBestSize()
        print "label size:", combolabel.GetBestSize()
        print "combo size:",combo.GetBestSize()
        self.Fit()

    def SendCommand(self, command):
        print "in SendCommand"
        if command in ('00','09','06'):
            print "in if loop"
            port = serial.Serial(0, 9600, 8, 'N', 2, timeout=2)
            print command
            port.write(command)
            old=self.outputbox.GetLabel()
            print old
            new=os.path.join([old, command],"")
            print 'New: ', new
            output=port.read()
            if output:
                returnedval=hex(ord(output))
                if returnedval:
                    print returnedval
                    returnedval = `returnedval`
                    print returnedval
                    print output
                    old=self.outputbox.GetLabel()
                    print old
                    new=os.path.join(old, output)
                    print new
                    self.outputbox.SetLabel(new)
        port.flush()
        port.close()

    def CommandCallback(self,event):
        cb = event.GetEventObject()
        selection = self.List[cb.GetSelection()]
        print selection
        old=self.outputbox.GetLabel()
        self.outputbox.SetLabel(old+selection)
        self.SendCommand(selection)

    def OnClick(self,event):
        print self.outputbox.SetLabel("")
 

class SecondPanel(wxPanel):
    def __init__(self, parent, ID = -1, size = wxDefaultSize, style= -1):
        wxPanel.__init__(self, parent, ID, wxDefaultPosition,style=wxSUNKEN_BORDER)

        self.SetBackgroundColour("LightBLUE") #just temporarily for distinction
        self.button = wxButton(self, wxID_OK, " OK ",
                               wxPoint(5, 14),wxDefaultSize).SetDefault()
        self.Fit()

class MyFrame(wxFrame):
    def __init__(self, parent, ID, title):
        wxFrame.__init__(self, parent, ID, title, size=wxDefaultSize)

        self.CreateStatusBar()
        self.SetStatusText("This is the status bar")

        menu = wxMenu()
        menu.Append(ID_ABOUT, "&About",
                    "More information about this program")
        menu.AppendSeparator()
        menu.Append(ID_EXIT, "E&xit", "Terminate the program")

        menuBar = wxMenuBar()
        menuBar.Append(menu, "&File");

        self.SetMenuBar(menuBar)

        panel1 = ComboBoxPanel(self,-1, wxDefaultSize, style=wxSUNKEN_BORDER)
        panel2 = SecondPanel(self, -1, wxDefaultSize, style=wxSUNKEN_BORDER)

        box = wxBoxSizer(wxVERTICAL)
        box.Add(panel1, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 2)
        box.Add(panel2, 1, wxGROW|wxALIGN_CENTER_VERTICAL|wxALL, 2)

        self.SetAutoLayout(true)
        self.SetSizer(box)
        self.Layout()
        self.Fit()
        print "outputbox size:",panel1.outputbox.GetSize()
        EVT_MENU(self, ID_ABOUT, self.OnAbout)
        EVT_MENU(self, ID_EXIT,  self.TimeToQuit)

    def OnAbout(self, event):
        dlg = wxMessageDialog(self, "This program sends commands to a \n"
                              "SSS or Advantage III meter attached via \n"
                              "the serial port.",
                              "About Me", wxOK | wxICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()

    def TimeToQuit(self, event):
        self.Close(true)

app = wxPySimpleApp()
frame = MyFrame(NULL, -1, "Communication Tool")
frame.Show()
app.MainLoop()

