controls/CustomEditableListBox.py
changeset 1784 64beb9e9c749
parent 1736 7e61baa047f0
child 1881 091005ec69c4
equal deleted inserted replaced
1729:31e63e25b4cc 1784:64beb9e9c749
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    24 
    24 
    25 import wx
    25 import wx
    26 import wx.gizmos
    26 import wx.gizmos
    27 
    27 
       
    28 
    28 class CustomEditableListBox(wx.gizmos.EditableListBox):
    29 class CustomEditableListBox(wx.gizmos.EditableListBox):
    29     
    30 
    30     def __init__(self, *args, **kwargs):
    31     def __init__(self, *args, **kwargs):
    31         wx.gizmos.EditableListBox.__init__(self, *args, **kwargs)
    32         wx.gizmos.EditableListBox.__init__(self, *args, **kwargs)
    32         
    33 
    33         listbox = self.GetListCtrl()
    34         listbox = self.GetListCtrl()
    34         listbox.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
    35         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_BEGIN_LABEL_EDIT, self.OnLabelBeginEdit)
    36         listbox.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnLabelEndEdit)
    37         listbox.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnLabelEndEdit)
    37         
    38 
    38         for button, tooltip, call_function in [
    39         for button, tooltip, call_function in [
    39                 (self.GetEditButton(), _("Edit item"), "_OnEditButton"),
    40                 (self.GetEditButton(), _("Edit item"), "_OnEditButton"),
    40                 (self.GetNewButton(), _("New item"), "_OnNewButton"),
    41                 (self.GetNewButton(), _("New item"), "_OnNewButton"),
    41                 (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
    42                 (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
    42                 (self.GetUpButton(), _("Move up"), "_OnUpButton"),
    43                 (self.GetUpButton(), _("Move up"), "_OnUpButton"),
    43                 (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
    44                 (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
    44             button.SetToolTipString(tooltip)
    45             button.SetToolTipString(tooltip)
    45             button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
    46             button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
    46     
    47 
    47         self.Editing = False
    48         self.Editing = False
    48     
    49 
    49     def EnsureCurrentItemVisible(self):
    50     def EnsureCurrentItemVisible(self):
    50         listctrl = self.GetListCtrl()
    51         listctrl = self.GetListCtrl()
    51         listctrl.EnsureVisible(listctrl.GetFocusedItem())
    52         listctrl.EnsureVisible(listctrl.GetFocusedItem())
    52     
    53 
    53     def OnLabelBeginEdit(self, event):
    54     def OnLabelBeginEdit(self, event):
    54         self.Editing = True
    55         self.Editing = True
    55         func = getattr(self, "_OnLabelBeginEdit", None)
    56         func = getattr(self, "_OnLabelBeginEdit", None)
    56         if func is not None:
    57         if func is not None:
    57             func(event)
    58             func(event)
    58         else:
    59         else:
    59             event.Skip()
    60             event.Skip()
    60         
    61 
    61     def OnLabelEndEdit(self, event):
    62     def OnLabelEndEdit(self, event):
    62         self.Editing = False
    63         self.Editing = False
    63         func = getattr(self, "_OnLabelEndEdit", None)
    64         func = getattr(self, "_OnLabelEndEdit", None)
    64         if func is not None:
    65         if func is not None:
    65             func(event)
    66             func(event)
    66         else:
    67         else:
    67             event.Skip()
    68             event.Skip()
    68     
    69 
    69     def GetButtonPressedFunction(self, call_function):
    70     def GetButtonPressedFunction(self, call_function):
    70         def OnButtonPressed(event):
    71         def OnButtonPressed(event):
    71             if wx.Platform != '__WXMSW__' or not self.Editing:
    72             if wx.Platform != '__WXMSW__' or not self.Editing:
    72                 func = getattr(self, call_function, None)
    73                 func = getattr(self, call_function, None)
    73                 if func is not None:
    74                 if func is not None:
    75                     wx.CallAfter(self.EnsureCurrentItemVisible)
    76                     wx.CallAfter(self.EnsureCurrentItemVisible)
    76                 else:
    77                 else:
    77                     wx.CallAfter(self.EnsureCurrentItemVisible)
    78                     wx.CallAfter(self.EnsureCurrentItemVisible)
    78                     event.Skip()
    79                     event.Skip()
    79         return OnButtonPressed
    80         return OnButtonPressed
    80     
    81 
    81     def OnKeyDown(self, event):
    82     def OnKeyDown(self, event):
    82         button = None
    83         button = None
    83         keycode = event.GetKeyCode()
    84         keycode = event.GetKeyCode()
    84         if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
    85         if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
    85             button = self.GetNewButton()
    86             button = self.GetNewButton()