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