controls/CustomEditableListBox.py
changeset 814 5743cbdff669
child 1571 486f94a8032c
equal deleted inserted replaced
813:1460273f40ed 814:5743cbdff669
       
     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         listbox = self.GetListCtrl()
       
    34         listbox.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
       
    35         listbox.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnLabelBeginEdit)
       
    36         listbox.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnLabelEndEdit)
       
    37         
       
    38         for button, tooltip, call_function in [
       
    39                 (self.GetEditButton(), _("Edit item"), "_OnEditButton"),
       
    40                 (self.GetNewButton(), _("New item"), "_OnNewButton"),
       
    41                 (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
       
    42                 (self.GetUpButton(), _("Move up"), "_OnUpButton"),
       
    43                 (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
       
    44             button.SetToolTipString(tooltip)
       
    45             button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
       
    46     
       
    47         self.Editing = False
       
    48     
       
    49     def EnsureCurrentItemVisible(self):
       
    50         listctrl = self.GetListCtrl()
       
    51         listctrl.EnsureVisible(listctrl.GetFocusedItem())
       
    52     
       
    53     def OnLabelBeginEdit(self, event):
       
    54         self.Editing = True
       
    55         func = getattr(self, "_OnLabelBeginEdit", None)
       
    56         if func is not None:
       
    57             func(event)
       
    58         else:
       
    59             event.Skip()
       
    60         
       
    61     def OnLabelEndEdit(self, event):
       
    62         self.Editing = False
       
    63         func = getattr(self, "_OnLabelEndEdit", None)
       
    64         if func is not None:
       
    65             func(event)
       
    66         else:
       
    67             event.Skip()
       
    68     
       
    69     def GetButtonPressedFunction(self, call_function):
       
    70         def OnButtonPressed(event):
       
    71             if wx.Platform != '__WXMSW__' or not self.Editing:
       
    72                 func = getattr(self, call_function, None)
       
    73                 if func is not None:
       
    74                     func(event)
       
    75                     wx.CallAfter(self.EnsureCurrentItemVisible)
       
    76                 else:
       
    77                     wx.CallAfter(self.EnsureCurrentItemVisible)
       
    78                     event.Skip()
       
    79         return OnButtonPressed
       
    80     
       
    81     def OnKeyDown(self, event):
       
    82         button = None
       
    83         keycode = event.GetKeyCode()
       
    84         if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
       
    85             button = self.GetNewButton()
       
    86         elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE):
       
    87             button = self.GetDelButton()
       
    88         elif keycode == wx.WXK_UP and event.ShiftDown():
       
    89             button = self.GetUpButton()
       
    90         elif keycode == wx.WXK_DOWN and event.ShiftDown():
       
    91             button = self.GetDownButton()
       
    92         elif keycode == wx.WXK_SPACE:
       
    93             button = self.GetEditButton()
       
    94         if button is not None and button.IsEnabled():
       
    95             button.ProcessEvent(wx.CommandEvent(wx.EVT_BUTTON.typeId, button.GetId()))
       
    96         else:
       
    97             event.Skip()