#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Fri Feb 27 18:49:27 2009

import wx
import serial
from globals import *
#from serialFrame import DEFAULT_TITLE
# begin wxGlade: extracode
# end wxGlade


class SerialDialog(wx.Dialog):
    def __init__(self, output_window, *args, **kwds):
        # begin wxGlade: SerialDialog.__init__
        self.serial = kwds['serial']
        del kwds['serial']
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE

	self.outputwin = output_window

        wx.Dialog.__init__(self, *args, **kwds)
        self.sizer_4_staticbox = wx.StaticBox(self, -1, "port settings")
        self.label_port = wx.StaticText(self, -1, "port")
        self.combo_box_port = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.label_baudrate = wx.StaticText(self, -1, "baudrate")
        self.combo_box_baudrate = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.label_bytesize = wx.StaticText(self, -1, "bytesize")
        self.combo_box_bytesize = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.label_parity = wx.StaticText(self, -1, "parity")
        self.combo_box_parity = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.label_stopbits = wx.StaticText(self, -1, "stopbits")
        self.combo_box_stopbits = wx.ComboBox(self, -1, choices=[], style=wx.CB_DROPDOWN)
        self.radio_box_rtscts = wx.RadioBox(self, -1, "RTS/CTS", choices=["off", "on"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
        self.radio_box_xonxoff = wx.RadioBox(self, -1, "xon/xoff", choices=["off", "on"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
        self.button_cancel = wx.Button(self, wx.ID_CANCEL, "")
        self.button_ok = wx.Button(self, wx.ID_OK, "", style=wx.BU_RIGHT)

        self.__set_properties()
        self.__do_layout()
        self.__combo_init()

        self.Bind(wx.EVT_BUTTON, self.onButtonOk, self.button_ok)
        self.Bind(wx.EVT_BUTTON, self.onButtonCancel, self.button_cancel)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: SerialDialog.__set_properties
        self.SetTitle("Port Settings")
        _icon = wx.EmptyIcon()

	path = self.outputwin.workingdir

        _icon.CopyFromBitmap(wx.Bitmap(path + "/../images/zulu_logo16x16.png", wx.BITMAP_TYPE_ANY))
        self.SetIcon(_icon)
        self.combo_box_port.SetMinSize((120, 29))
        self.combo_box_baudrate.SetMinSize((120, 29))
        self.combo_box_bytesize.SetMinSize((120, 29))
        self.combo_box_parity.SetMinSize((120, 29))
        self.combo_box_stopbits.SetMinSize((120, 29))
        self.radio_box_rtscts.SetSelection(0)
        self.radio_box_xonxoff.SetSelection(0)
        self.button_cancel.SetDefault()
        self.button_ok.SetDefault()
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: SerialDialog.__do_layout
        sizer_4 = wx.StaticBoxSizer(self.sizer_4_staticbox, wx.HORIZONTAL)
        grid_sizer_3 = wx.FlexGridSizer(7, 2, 2, 2)
        grid_sizer_3.Add(self.label_port, 0, wx.LEFT, 8)
        grid_sizer_3.Add(self.combo_box_port, 0, 0, 0)
        grid_sizer_3.Add(self.label_baudrate, 0, wx.LEFT, 8)
        grid_sizer_3.Add(self.combo_box_baudrate, 0, 0, 0)
        grid_sizer_3.Add(self.label_bytesize, 0, wx.LEFT, 8)
        grid_sizer_3.Add(self.combo_box_bytesize, 0, 0, 0)
        grid_sizer_3.Add(self.label_parity, 0, wx.LEFT, 8)
        grid_sizer_3.Add(self.combo_box_parity, 0, 0, 0)
        grid_sizer_3.Add(self.label_stopbits, 0, wx.LEFT, 8)
        grid_sizer_3.Add(self.combo_box_stopbits, 0, 0, 0)
        grid_sizer_3.Add(self.radio_box_rtscts, 0, wx.RIGHT|wx.TOP|wx.BOTTOM, 8)
        grid_sizer_3.Add(self.radio_box_xonxoff, 0, wx.LEFT|wx.TOP|wx.BOTTOM|wx.ALIGN_RIGHT, 8)
        grid_sizer_3.Add(self.button_cancel, 0, 0, 0)
        grid_sizer_3.Add(self.button_ok, 0, wx.ALIGN_RIGHT, 0)
        sizer_4.Add(grid_sizer_3, 1, wx.ALL|wx.EXPAND, 6)
        self.SetSizer(sizer_4)
        sizer_4.Fit(self)
        self.Layout()
        # end wxGlade
    
    def __combo_init(self):
        #port init
        self.combo_box_port.Clear()
        cnt = 0
        for i in range(20):
            self.combo_box_port.Append(serial.device(i))
            if self.serial.portstr == serial.device(i):
                cnt = i
        self.combo_box_port.SetValue(serial.device(cnt))
        
        #baudrate init
        self.combo_box_baudrate.Clear()
        cnt = 0
        for i, item in enumerate(self.serial.BAUDRATES):
            self.combo_box_baudrate.Append(str(item))
            if item == self.serial.baudrate:
                cnt = i
        self.combo_box_baudrate.SetValue(str(self.serial.BAUDRATES[cnt]))
        
        #bytesize init
        self.combo_box_bytesize.Clear()
        cnt = 0
        for i, item in enumerate(self.serial.BYTESIZES):
            self.combo_box_bytesize.Append(str(item))
            if item == self.serial.bytesize:
                cnt = i
        self.combo_box_bytesize.SetValue(str(self.serial.BYTESIZES[cnt]))

        #stopbits init
        self.combo_box_stopbits.Clear()
        cnt = 0
        for i, item in enumerate(self.serial.STOPBITS):
            self.combo_box_stopbits.Append(str(item))
            if item == self.serial.stopbits:
                cnt = i
        self.combo_box_stopbits.SetValue(str(self.serial.STOPBITS[cnt]))
        
        #baudrate init
        self.combo_box_parity.Clear()
        cnt = 0
        for i, item in enumerate(self.serial.PARITIES):
            self.combo_box_parity.Append(str(item))
            if item == self.serial.parity:
                cnt = i
        self.combo_box_parity.SetValue(str(self.serial.PARITIES[cnt]))
        self.radio_box_rtscts.SetSelection(self.serial.rtscts)
        self.radio_box_xonxoff.SetSelection(self.serial.xonxoff)
        
    def onButtonOk(self, event): # wxGlade: SerialDialog.<event_handler>
        #print type(self.combo_box_port.GetValue())
        try:
            self.serial.port = str(self.combo_box_port.GetValue())
            self.serial.baudrate = long(self.combo_box_baudrate.GetValue())
            self.serial.bytesize = int(self.combo_box_bytesize.GetValue())
            self.serial.stopbits = int(self.combo_box_stopbits.GetValue())
            self.serial.parity   = self.combo_box_parity.GetValue()
            self.serial.rtscts   = self.radio_box_rtscts.GetSelection()
            self.serial.xonxoff  = self.radio_box_xonxoff.GetSelection()
        except Exception,e:
            #print 'exception: %s'%str(e)
            dlg = wx.MessageDialog(self,'exception occured: %s, please check your configuration'%str(e), style = wx.OK|wx.ICON_INFORMATION)
            dlg.ShowModal()
            dlg.Destroy()
            #self.SetTitle(DEFAULT_TITLE)
            #event.Skip()
##        print '----------------before------------------'
##        print 'port:', self.serial.port
##        print 'baudrate:',self.serial.baudrate
##        print 'bytesize:', self.serial.bytesize
##        print 'stopbits:', self.serial.stopbits
##        print 'parity:', self.serial.parity
##        print 'rtscts:', self.serial.rtscts
##        print 'xonxoff:', self.serial.xonxoff
        self.EndModal(wx.ID_OK)
        
    def onButtonCancel(self, event):
        self.EndModal(wx.ID_CANCEL)
# end of class SerialDialog


if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    serial_dialog = SerialDialog(None, -1, "", serial = serial.Serial(0))
    app.SetTopWindow(serial_dialog)
    serial_dialog.Show()
    app.MainLoop()