""" A log window, for displaying stdout/stderr stuff from Necrofamicon. """ import wx import wx class Log(wx.stc.StyledTextCtrl): """ Subclass the StyledTextCtrl to provide additions and initializations to make it useful as a log window. """ def __init__(self, Parent, Style = wx.SIMPLE_BORDER): """ Constructor """ stc.StyledTextCtrl.__init__(self, Parent, style = Style) self._styles = [None]*32 self._free = 1 def GetStyle(self, c='black'): """ Returns a style for a given colour if one exists. If no style exists for the colour, make a new style. If we run out of styles, (only 32 allowed here) we go to the top of the list and reuse previous styles. """ free = self._free if c and isinstance(c, (str, unicode)): c = c.lower() else: c = 'black' try: style = self._styles.index(c) return style except ValueError: style = free self._styles[style] = c self.StyleSetForeground(style, wx.NamedColour(c)) free += 1 if free >31: free = 0 self._free = free return style def write(self, Text, c = None): """ Add the text to the end of the control using colour c which should be suitable for feeding directly to wx.NamedColour. 'text' should be a unicode string or contain only ascii data. """ Style = self.GetStyle(c) TextLength = len(text.encode('utf8')) End = self.GetLength() self.AddText(Text) self.StartStyling(End, 31) self.SetStyling(TextLength, Style) self.EnsureCaretVisible() __call__ = write