controls/CustomEditableListBox.py
changeset 577 9dbb79722fbc
child 640 c32c169b8f63
equal deleted inserted replaced
576:3f2024f30553 577:9dbb79722fbc
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
       
     5 #based on the plcopen standard. 
       
     6 #
       
     7 #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
       
     8 #
       
     9 #See COPYING file for copyrights details.
       
    10 #
       
    11 #This library is free software; you can redistribute it and/or
       
    12 #modify it under the terms of the GNU General Public
       
    13 #License as published by the Free Software Foundation; either
       
    14 #version 2.1 of the License, or (at your option) any later version.
       
    15 #
       
    16 #This library is distributed in the hope that it will be useful,
       
    17 #but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    19 #General Public License for more details.
       
    20 #
       
    21 #You should have received a copy of the GNU General Public
       
    22 #License along with this library; if not, write to the Free Software
       
    23 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    24 
       
    25 import wx
       
    26 import wx.gizmos
       
    27 
       
    28 class CustomEditableListBox(wx.gizmos.EditableListBox):
       
    29     
       
    30     def __init__(self, *args, **kwargs):
       
    31         wx.gizmos.EditableListBox.__init__(self, *args, **kwargs)
       
    32         
       
    33         self.GetListCtrl().Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
       
    34         
       
    35         for button, tooltip, call_function in [(self.GetEditButton(), _("Edit item"), "_OnEditButton"),
       
    36                                                (self.GetNewButton(), _("New item"), "_OnNewButton"),
       
    37                                                (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
       
    38                                                (self.GetUpButton(), _("Move up"), "_OnUpButton"),
       
    39                                                (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
       
    40             button.SetToolTipString(tooltip)
       
    41             button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
       
    42     
       
    43     def EnsureCurrentItemVisible(self):
       
    44         listctrl = self.GetListCtrl()
       
    45         listctrl.EnsureVisible(listctrl.GetFocusedItem())
       
    46     
       
    47     def GetButtonPressedFunction(self, call_function):
       
    48         def OnButtonPressed(event):
       
    49             func = getattr(self, call_function, None)
       
    50             if func is not None:
       
    51                 wx.CallAfter(func, event)
       
    52             wx.CallAfter(self.EnsureCurrentItemVisible)
       
    53             event.Skip()
       
    54         return OnButtonPressed
       
    55     
       
    56     def OnKeyDown(self, event):
       
    57         button = None
       
    58         keycode = event.GetKeyCode()
       
    59         if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
       
    60             button = self.GetNewButton()
       
    61         elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE):
       
    62             button = self.GetDelButton()
       
    63         elif keycode == wx.WXK_UP and event.ShiftDown():
       
    64             button = self.GetUpButton()
       
    65         elif keycode == wx.WXK_DOWN and event.ShiftDown():
       
    66             button = self.GetDownButton()
       
    67         elif keycode == wx.WXK_SPACE:
       
    68             button = self.GetEditButton()
       
    69         if button is not None and button.IsEnabled():
       
    70             button.ProcessEvent(wx.CommandEvent(wx.EVT_BUTTON.typeId, button.GetId()))
       
    71         else:
       
    72             event.Skip()