"""
FtpCube
Copyright (C) Michael Gilfix
This file is part of FtpCube.
You should have received a file COPYING containing license terms
along with this program; if not, write to Michael Gilfix
(mgilfix@eecs.tufts.edu) for a copy.
This version of FtpCube is open source; you can redistribute it and/or
modify it under the terms listed in the file COPYING.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
"""
import events
import utils
import version
import wx
class ConsoleWindow(wx.TextCtrl):
"""Main application console window.
This serves as the primary console for displaying FTP commands and initialization
information. The console window is color coded, differentiating between local, remote,
and error messages. To write to the console window, widgets should post console
events."""
idPOPUP_CLEAR = wx.NewId()
# Message kinds
LOCAL = 'local'
REMOTE = 'remote'
ERROR = 'error'
def __init__(self, parent):
"""Creates the console window."""
if __debug__:
print "Making console window."
wx.TextCtrl.__init__(self, parent, -1, style=wx.TE_MULTILINE | wx.TE_READONLY |
wx.NO_BORDER | wx.TE_RICH2)
self.format = {
self.LOCAL : _("LOCAL > %s\n"),
self.REMOTE : _("REMOTE> %s\n"),
self.ERROR : _("! - %s\n"),
}
self.colors = {
self.LOCAL : None,
self.REMOTE : None,
self.ERROR : None,
}
self.wxColors = {
self.LOCAL : None,
self.REMOTE : None,
self.ERROR : None,
}
# Set ourselves to receive all console events
self.Bind(events.EVT_CONSOLE, self.onConsoleEvent)
evt_registry = events.getEventRegistry()
evt_registry.registerEventListener(events.EVT_CONSOLE_TYPE, self)
# Set up the console menu handler
self.Bind(wx.EVT_RIGHT_DOWN, self.onRightClick, self)
def displayBanner(self):
"""Displays the application startup banner."""
self.insertColorText(self.LOCAL, _("Welcome to Ftpcube v%(ver)s")
%{ 'ver' : version.getVersion() })
self.insertColorText(self.LOCAL, _("Copyright (C) Michael Gilfix"))
self.insertColorText(self.LOCAL, '===============================================')
self.insertColorText(self.LOCAL, '')
def insertColorText(self, kind, text):
"""Writes text to the console window in the appropriate color for the kind of
message."""
color = self.getColor(kind)
text = self.format[kind] %text
self.SetDefaultStyle(wx.TextAttr(color))
self.AppendText(text)
def getColor(self, kind):
"""Gets the wx color object for the specified text kind."""
config = utils.getApplicationConfiguration()
current_color = config[kind + '_color']
if self.colors[kind] == current_color:
wx_color = self.wxColors[kind]
else:
wx_color = apply(wx.Color, current_color)
self.colors[kind] = current_color
self.wxColors[kind] = wx_color
return wx_color
def onConsoleEvent(self, event):
"""Processes a console event by displaying the contents of the event to the console
window."""
self.insertColorText(event.kind, event.msg)
def onRightClick(self, event):
"""Handles a right click by displaying the popupmenu at the click point."""
menu = self.makePopupMenu()
self.PopupMenu(menu, event.GetPosition())
def makePopupMenu(self):
"""Creates the popup menu for operations on the console window."""
menu = wx.Menu()
menu.Append(self.idPOPUP_CLEAR, _("Clear Console"))
self.Bind(wx.EVT_MENU, self.onClear, id=self.idPOPUP_CLEAR)
return menu
def onClear(self, event):
"""Processes a clearing of the console window event."""
if __debug__:
print "Clearing console window."
self.Clear()
|