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