###############################################################################
# File:        guListView.py
# Appointment: List viewer helper
# Project:     GalaxyU
# Author:      AR
# History:
# 15.10.2013 Created
###############################################################################
import wxTools
import guTools
import guShipCalc
import guApp

import wx
import zx

#===============================================================================
# List view frame
#===============================================================================
class CListViewFrame(guTools.CCfgFrame):
    #---------------------------------------------------------------------------
    # aFuncGridCreator - function creating a grid control. Accepts panel and list as parameter
    def __init__(self, parent, aListCreator, aFuncGridCreator, aCfgObjectName, aTitle = ""):
        guTools.CCfgFrame.__init__(self, parent, guApp.Application.Config, "GUI", aCfgObjectName)
        
        # Title
        self.SetTitle(aTitle)

        # Min window size
        self.SetMinSize((700, 400))
        
        # Grid panel
        componentsPanel = wx.Panel(\
            self, -1, size=(self.GetClientSize().GetWidth(), self.GetClientSize().GetHeight()/2))
        grid = aFuncGridCreator(componentsPanel, aListCreator)
        grid.SetSize(componentsPanel.GetClientSize())

        gridSizer = wx.BoxSizer(wx.VERTICAL)
        gridSizer.Add(grid, 1, wx.EXPAND | wx.ALL)
        grid.SetFocus()
        componentsPanel.SetSizer(gridSizer)
        if grid.GetItemCount() > 0:
            grid.Select(0)
            
        # Add default key events
        grid.SpecialKeyEvents[wx.WXK_ESCAPE] = lambda e: self.Close()
#-------------------------------------------------------------------------------


#===============================================================================
# List control with builder
#===============================================================================
class CListViewCtrlWithBuilder(wxTools.CListCtrl):
    #---------------------------------------------------------------------------
    def __init__(self, parent, aListCreator):
        wxTools.CListCtrl.__init__(self, parent)

        self.__listCreator = aListCreator

        self.__builder = wxTools.CColDataBuilder()
        
        # Call virtual to prepapare builder
        self.PrepareBuilder(self.__builder)

        # Populate data
        self.__RefreshData()

        # Subscribe for refresh message
        wxTools.PublisherSubscribe(self.__MessageRefreshListener, guApp.MESSAGE_REFRESH_DATA)

    #---------------------------------------------------------------------------
    def __del__(self):
        wxTools.PublisherUnsubscribe(self.__MessageRefreshListener, guApp.MESSAGE_REFRESH_DATA)

    #---------------------------------------------------------------------------
    # Virtual to be redefined
    def PrepareBuilder(self, aBuilder):
        pass

    #---------------------------------------------------------------------------
    # Virtual to be redefined: initial data sorting
    def SortData(self, aItems):
        pass

    #---------------------------------------------------------------------------
    def GetItems(self):
        return self.__data 

    #---------------------------------------------------------------------------
    def __RefreshData(self, aIsRefresh = False):
        self.__data = self.__listCreator()
        self.SortData(self.__data) # call virtual
        self.SetItemCount(len(self.__data))
        self.__builder.Prepare(self.__data, self, aIsRefresh)

    #---------------------------------------------------------------------------
    def __MessageRefreshListener(self, arg = ""):
        # Get last positions to restore after refresh
        list_total = self.GetItemCount()
        list_top = self.GetTopItem()
        if not zx.IsWindows():
            list_top -= 1
        pp = self.GetCountPerPage()
        list_bottom = min(list_top + pp, list_total - 1)
        selected = self.GetFirstSelected()

        # Refresh
        self.DeleteAllItems()
        self.__RefreshData(True)

        # Restore positions
        self.EnsureVisible((list_bottom - 1))
        if selected != -1:
            self.Select(selected)
