# -*- coding: UTF-8 -*-
# generated by wxGlade 0.4.1 on Mon Aug 28 14:02:59 2006

#-------------------------------------------------------------------------------
# Name:        pyterm.MainFrame
# Purpose:     pyTerm main frame.
#
# Original
# Authors:      Thomas Pani
#
# History:
#   None (at the moment).
#
# Created:     28-August-2006
# Copyright:   (c) 2006 by Thomas Pani
# Licence:     MIT
#-------------------------------------------------------------------------------


import os

import wx
import wx.lib.dialogs

from pyterm.Configuration import Configuration
from pyterm.PortConfigurationDialog import PortConfigurationDialog
from pyterm.Terminal import Terminal


# begin wxGlade: dependencies
# end wxGlade

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MainFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        
        # Menu Bar
        self.main_frame_menubar = wx.MenuBar()
        self.SetMenuBar(self.main_frame_menubar)
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(101, "&Clear Screen\tCtrl+L", "Clear the terminal.", wx.ITEM_NORMAL)
        wxglade_tmp_menu.Append(102, "S&end File\tCtrl+O", "Open a file and send it.", wx.ITEM_NORMAL)
        wxglade_tmp_menu_sub = wx.Menu()
        wxglade_tmp_menu_sub.Append(111, "&Show Log", "Show the log.", wx.ITEM_NORMAL)
        wxglade_tmp_menu_sub.Append(112, "Sa&ve Log\tCtrl+S", "Save the log to a file.", wx.ITEM_NORMAL)
        wxglade_tmp_menu_sub.Append(113, "&Clear Log", "Reset the log.", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendMenu(wx.NewId(), "&Log", wxglade_tmp_menu_sub, "")
        wxglade_tmp_menu.AppendSeparator()
        wxglade_tmp_menu.Append(109, "&Quit\tCtrl+Q", "Quit pyTerm.", wx.ITEM_NORMAL)
        self.main_frame_menubar.Append(wxglade_tmp_menu, "&File")
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(201, "&Port\tCtrl+P", "Serial port preferences.", wx.ITEM_NORMAL)
        wxglade_tmp_menu.Append(202, "&Terminal\tCtrl+T", "Terminal preferences.", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendSeparator()
        wxglade_tmp_menu.Append(203, "Local &Echo\tCtrl+E", "Turn local echo on/off.", wx.ITEM_CHECK)
        self.main_frame_menubar.Append(wxglade_tmp_menu, "&Configuration")
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(301, "Send &Break\tCtrl+B", "Send a break signal.", wx.ITEM_NORMAL)
        self.main_frame_menubar.Append(wxglade_tmp_menu, "Control &Signals")
        wxglade_tmp_menu = wx.Menu()
        wxglade_tmp_menu.Append(501, "&Help\tF1", "Show the help.", wx.ITEM_NORMAL)
        wxglade_tmp_menu.AppendSeparator()
        wxglade_tmp_menu.Append(508, "&License", "Show licensing information.", wx.ITEM_NORMAL)
        wxglade_tmp_menu.Append(509, "&About", "Show information about pyTerm.", wx.ITEM_NORMAL)
        self.main_frame_menubar.Append(wxglade_tmp_menu, "&Help")
        # Menu Bar end
        self.main_frame_statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
        self.terminal = Terminal(self, -1)

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_MENU, self.OnClearScreen, id=101)
        self.Bind(wx.EVT_MENU, self.OnSendFile, id=102)
        self.Bind(wx.EVT_MENU, self.OnShowLog, id=111)
        self.Bind(wx.EVT_MENU, self.OnSaveLog, id=112)
        self.Bind(wx.EVT_MENU, self.OnClearLog, id=113)
        self.Bind(wx.EVT_MENU, self.OnQuit, id=109)
        self.Bind(wx.EVT_MENU, self.OnPortConfiguration, id=201)
        self.Bind(wx.EVT_MENU, self.OnTerminalConfiguration, id=202)
        self.Bind(wx.EVT_MENU, self.OnLocalEcho, id=203)
        self.Bind(wx.EVT_MENU, self.OnSendBreak, id=301)
        self.Bind(wx.EVT_MENU, self.OnHelp, id=501)
        self.Bind(wx.EVT_MENU, self.OnLicense, id=508)
        self.Bind(wx.EVT_MENU, self.OnAbout, id=509)
        # end wxGlade
        self.Bind(wx.EVT_WINDOW_DESTROY, self.OnDestroy)
        
        self.configuration = None
        self.__init_configuration()

    def __set_properties(self):
        # begin wxGlade: MainFrame.__set_properties
        self.SetTitle("pyTerm")
        self.main_frame_statusbar.SetStatusWidths([-1, 200])
        # statusbar fields
        main_frame_statusbar_fields = ["", ""]
        for i in range(len(main_frame_statusbar_fields)):
            self.main_frame_statusbar.SetStatusText(main_frame_statusbar_fields[i], i)
        self.terminal.SetFocus()
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MainFrame.__do_layout
        sizer_1 = wx.FlexGridSizer(1, 1, 0, 0)
        sizer_1.Add(self.terminal, 1, wx.EXPAND, 0)
        self.SetAutoLayout(True)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        sizer_1.SetSizeHints(self)
        sizer_1.AddGrowableRow(0)
        sizer_1.AddGrowableCol(0)
        self.Layout()
        # end wxGlade
        
    def __init_configuration(self):
        self.configuration = Configuration()
        self.update_configuration(self.configuration.port_configuration)
        
        self.terminal.SetLocalEcho(self.configuration.local_echo)
        if self.configuration.local_echo:
            self.main_frame_menubar.GetMenu(1).GetMenuItems()[3].Check()
        
    def update_configuration(self, port_configuration):
        self.configuration.port_configuration = port_configuration
        self.SetStatusText(str(self.configuration.port_configuration), 1)
        self.terminal.SetPortConfiguration(self.configuration.port_configuration)

    def OnClearScreen(self, event): # wxGlade: MainFrame.<event_handler>
        self.terminal.ClearScreen()
        event.Skip()

    def OnSendFile(self, event): # wxGlade: MainFrame.<event_handler>
        wildcard = "Text Files (*.conf;*.log;*.txt)|*.conf;*.log;*.txt|All Files (*.*)|*.*"
        dlg = wx.FileDialog(self, defaultDir=os.getcwd(),
                            wildcard=wildcard, style=wx.OPEN)
        
        if dlg.ShowModal() == wx.ID_OK:
            path = os.path.join(dlg.GetDirectory(), dlg.GetFilename())
            self.terminal.SendFile(path)
            
        
        event.Skip()

    def OnQuit(self, event): # wxGlade: MainFrame.<event_handler>
        self.Close()
        event.Skip()

    def OnPortConfiguration(self, event): # wxGlade: MainFrame.<event_handler>
        dlg = PortConfigurationDialog(self.configuration.port_configuration, self)
        dlg.ShowModal()
        self.update_configuration(dlg.GetPortConfiguration())
        
        event.Skip()

    def OnLocalEcho(self, event): # wxGlade: MainFrame.<event_handler>
        self.terminal.SetLocalEcho(event.IsChecked())
        self.configuration.local_echo = event.IsChecked()
        event.Skip()

    def OnSendBreak(self, event): # wxGlade: MainFrame.<event_handler>
        dlg = wx.MessageDialog(self, 'Send Break is not yet implemented!',
                               'Information', wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        event.Skip()

    def OnHelp(self, event): # wxGlade: MainFrame.<event_handler>
        dlg = wx.MessageDialog(self, 'Help is not yet implemented!',
                               'Information', wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        event.Skip()

    def OnAbout(self, event): # wxGlade: MainFrame.<event_handler>
        title = self.GetTitle()
        dlg = wx.MessageDialog(self, '''\
%s. A serial port (RS232) terminal emulator.

Copyright (c) 2006, Thomas Pani
Please see Help/License for licensing information.''' % title,
                               'About %s' % title, wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()

        event.Skip()

    def OnLicense(self, event): # wxGlade: MainFrame.<event_handler>
        dlg = wx.lib.dialogs.ScrolledMessageDialog(self, '''\
Copyright (c) 2006 Thomas Pani

Permission is hereby granted, free of charge, to any person obtaining a copy of \
this software and associated documentation files (the "Software"), to deal in \
the Software without restriction, including without limitation the rights to \
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies \
of the Software, and to permit persons to whom the Software is furnished to do \
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all \
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR \
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS \
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR \
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN \
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION \
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.''', "License")
        dlg.ShowModal()
        dlg.Destroy()
        event.Skip()

    def OnShowLog(self, event): # wxGlade: MainFrame.<event_handler>
        dlg = wx.lib.dialogs.ScrolledMessageDialog(self,
                                                   self.terminal.GetLog(),
                                                   "Log")
        dlg.ShowModal()
        event.Skip()

    def OnSaveLog(self, event): # wxGlade: MainFrame.<event_handler>
        wildcard = "Text Files (*.log;*.txt)|*.log;*.txt|All Files (*.*)|*.*"
        dlg = wx.FileDialog(self, message="Save As ...", defaultDir=os.getcwd(),
                            wildcard=wildcard, style=wx.SAVE)
        
        if dlg.ShowModal() == wx.ID_OK:
            path = os.path.join(dlg.GetDirectory(), dlg.GetFilename())
            fd = open(path, 'w')
            fd.write(self.terminal.GetLog())
            fd.close()
        
        event.Skip()

    def OnClearLog(self, event): # wxGlade: MainFrame.<event_handler>
        self.terminal.ClearLog()
        dlg = wx.MessageDialog(self, 'The log has been cleared successfully!',
                               'Information', wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        event.Skip()

    def OnTerminalConfiguration(self, event): # wxGlade: MainFrame.<event_handler>
        dlg = wx.MessageDialog(self, 'Terminal Configuration is not yet implemented!',
                               'Information', wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        event.Skip()

    def OnDestroy(self, event):
        self.configuration.write_config()
        event.Skip()

# end of class MainFrame
