controls/CustomEditableListBox.py
author laurent
Tue, 24 Jan 2012 21:42:21 +0100
changeset 630 9d7e38e271cb
parent 577 9dbb79722fbc
child 640 c32c169b8f63
permissions -rw-r--r--
Adding support for loading datatypes defined in plugins to allow to use them in PLC program
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
#based on the plcopen standard. 
#
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
#
#See COPYING file for copyrights details.
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library 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.  See the GNU
#General Public License for more details.
#
#You should have received a copy of the GNU General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import wx
import wx.gizmos

class CustomEditableListBox(wx.gizmos.EditableListBox):
    
    def __init__(self, *args, **kwargs):
        wx.gizmos.EditableListBox.__init__(self, *args, **kwargs)
        
        self.GetListCtrl().Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        
        for button, tooltip, call_function in [(self.GetEditButton(), _("Edit item"), "_OnEditButton"),
                                               (self.GetNewButton(), _("New item"), "_OnNewButton"),
                                               (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
                                               (self.GetUpButton(), _("Move up"), "_OnUpButton"),
                                               (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
            button.SetToolTipString(tooltip)
            button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
    
    def EnsureCurrentItemVisible(self):
        listctrl = self.GetListCtrl()
        listctrl.EnsureVisible(listctrl.GetFocusedItem())
    
    def GetButtonPressedFunction(self, call_function):
        def OnButtonPressed(event):
            func = getattr(self, call_function, None)
            if func is not None:
                wx.CallAfter(func, event)
            wx.CallAfter(self.EnsureCurrentItemVisible)
            event.Skip()
        return OnButtonPressed
    
    def OnKeyDown(self, event):
        button = None
        keycode = event.GetKeyCode()
        if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
            button = self.GetNewButton()
        elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE):
            button = self.GetDelButton()
        elif keycode == wx.WXK_UP and event.ShiftDown():
            button = self.GetUpButton()
        elif keycode == wx.WXK_DOWN and event.ShiftDown():
            button = self.GetDownButton()
        elif keycode == wx.WXK_SPACE:
            button = self.GetEditButton()
        if button is not None and button.IsEnabled():
            button.ProcessEvent(wx.CommandEvent(wx.EVT_BUTTON.typeId, button.GetId()))
        else:
            event.Skip()