Laurent@814: #!/usr/bin/env python
Laurent@814: # -*- coding: utf-8 -*-
Laurent@814: 
Laurent@814: #This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
Laurent@814: #based on the plcopen standard. 
Laurent@814: #
Laurent@814: #Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
Laurent@814: #
Laurent@814: #See COPYING file for copyrights details.
Laurent@814: #
Laurent@814: #This library is free software; you can redistribute it and/or
Laurent@814: #modify it under the terms of the GNU General Public
Laurent@814: #License as published by the Free Software Foundation; either
Laurent@814: #version 2.1 of the License, or (at your option) any later version.
Laurent@814: #
Laurent@814: #This library is distributed in the hope that it will be useful,
Laurent@814: #but WITHOUT ANY WARRANTY; without even the implied warranty of
Laurent@814: #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Laurent@814: #General Public License for more details.
Laurent@814: #
Laurent@814: #You should have received a copy of the GNU General Public
Laurent@814: #License along with this library; if not, write to the Free Software
Laurent@814: #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Laurent@814: 
Laurent@814: import wx
Laurent@814: import wx.gizmos
Laurent@814: 
Laurent@814: class CustomEditableListBox(wx.gizmos.EditableListBox):
Laurent@814:     
Laurent@814:     def __init__(self, *args, **kwargs):
Laurent@814:         wx.gizmos.EditableListBox.__init__(self, *args, **kwargs)
Laurent@814:         
Laurent@814:         listbox = self.GetListCtrl()
Laurent@814:         listbox.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
Laurent@814:         listbox.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnLabelBeginEdit)
Laurent@814:         listbox.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnLabelEndEdit)
Laurent@814:         
Laurent@814:         for button, tooltip, call_function in [
Laurent@814:                 (self.GetEditButton(), _("Edit item"), "_OnEditButton"),
Laurent@814:                 (self.GetNewButton(), _("New item"), "_OnNewButton"),
Laurent@814:                 (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
Laurent@814:                 (self.GetUpButton(), _("Move up"), "_OnUpButton"),
Laurent@814:                 (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
Laurent@814:             button.SetToolTipString(tooltip)
Laurent@814:             button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
Laurent@814:     
Laurent@814:         self.Editing = False
Laurent@814:     
Laurent@814:     def EnsureCurrentItemVisible(self):
Laurent@814:         listctrl = self.GetListCtrl()
Laurent@814:         listctrl.EnsureVisible(listctrl.GetFocusedItem())
Laurent@814:     
Laurent@814:     def OnLabelBeginEdit(self, event):
Laurent@814:         self.Editing = True
Laurent@814:         func = getattr(self, "_OnLabelBeginEdit", None)
Laurent@814:         if func is not None:
Laurent@814:             func(event)
Laurent@814:         else:
Laurent@814:             event.Skip()
Laurent@814:         
Laurent@814:     def OnLabelEndEdit(self, event):
Laurent@814:         self.Editing = False
Laurent@814:         func = getattr(self, "_OnLabelEndEdit", None)
Laurent@814:         if func is not None:
Laurent@814:             func(event)
Laurent@814:         else:
Laurent@814:             event.Skip()
Laurent@814:     
Laurent@814:     def GetButtonPressedFunction(self, call_function):
Laurent@814:         def OnButtonPressed(event):
Laurent@814:             if wx.Platform != '__WXMSW__' or not self.Editing:
Laurent@814:                 func = getattr(self, call_function, None)
Laurent@814:                 if func is not None:
Laurent@814:                     func(event)
Laurent@814:                     wx.CallAfter(self.EnsureCurrentItemVisible)
Laurent@814:                 else:
Laurent@814:                     wx.CallAfter(self.EnsureCurrentItemVisible)
Laurent@814:                     event.Skip()
Laurent@814:         return OnButtonPressed
Laurent@814:     
Laurent@814:     def OnKeyDown(self, event):
Laurent@814:         button = None
Laurent@814:         keycode = event.GetKeyCode()
Laurent@814:         if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
Laurent@814:             button = self.GetNewButton()
Laurent@814:         elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE):
Laurent@814:             button = self.GetDelButton()
Laurent@814:         elif keycode == wx.WXK_UP and event.ShiftDown():
Laurent@814:             button = self.GetUpButton()
Laurent@814:         elif keycode == wx.WXK_DOWN and event.ShiftDown():
Laurent@814:             button = self.GetDownButton()
Laurent@814:         elif keycode == wx.WXK_SPACE:
Laurent@814:             button = self.GetEditButton()
Laurent@814:         if button is not None and button.IsEnabled():
Laurent@814:             button.ProcessEvent(wx.CommandEvent(wx.EVT_BUTTON.typeId, button.GetId()))
Laurent@814:         else:
Laurent@814:             event.Skip()