###############################################################################
# File:        wxListLogTarget.py
# Appointment: Log target based on the ListControl
# Project:     wx
# Author:      AR
# History:
# 14.04.2014 Created
###############################################################################
import wxTools
import zxLog

import wx


#===============================================================================
# List control
#===============================================================================
class CListLogTarget(wxTools.CListCtrl, zxLog.CLogTarget):
    #---------------------------------------------------------------------------
    # aReportMessages - list of report messages
    def __init__(self, parent, aMaxNumberOfItems = 1000):
        wxTools.CListCtrl.__init__(self, parent)
        self.MaxNumberOfItems = aMaxNumberOfItems
        self.IsEditable = False
        self.SetSorting(False) # avoid sorting
        self.SetBackgroundColour((0,0,0))
        self.SetForegroundColour((200,200,200))
        
        self.__items = []
        self.SetItemCount(0)
        
        # Add columns
        self.AddColumn("Severity", 100, 
                       lambda row, col: zxLog.SeverityToString(self.__items[row][0]))
        self.AddColumn("Message", 2000, 
                       lambda row, col: self.__items[row][1])

        # Colour attributes
        self.__attrSuccess = wx.ListItemAttr()
        self.__attrSuccess.SetTextColour((0,255,0))

        self.__attrWarning = wx.ListItemAttr()
        self.__attrWarning.SetTextColour((255,255,0))

        self.__attrError = wx.ListItemAttr()
        self.__attrError.SetTextColour((255,0,0))

        self.__attrFatalError = wx.ListItemAttr()
        self.__attrFatalError.SetTextColour((255,0,255))

        self.__attrUnexpectedError = wx.ListItemAttr()
        self.__attrUnexpectedError.SetTextColour((255,0,150))

    #---------------------------------------------------------------------------
    # Redefined virtual from CLogTarget
    def Log(self, aSeverity, aMessage):
        self.__items.append((aSeverity, aMessage))
        if len(self.__items) > self.MaxNumberOfItems:
            del(self.__items[0])
        self.SetItemCount(len(self.__items))
        self.Focus(len(self.__items) - 1)
        self.Refresh()

    #---------------------------------------------------------------------------
    def OnGetItemAttr(self, item):
        aSeverity = self.__items[item][0]
        if aSeverity == zxLog.LOG_SUCCESS:
            return self.__attrSuccess
        elif aSeverity == zxLog.LOG_WARNING:
            return self.__attrWarning
        elif aSeverity == zxLog.LOG_ERROR:
            return self.__attrError
        elif aSeverity == zxLog.LOG_FATAL_ERROR:
            return self.__attrFatalError
        elif aSeverity == zxLog.LOG_UNEXPECTED_ERROR:
            return self.__attrUnexpectedError
        return None
#-------------------------------------------------------------------------------

