Laurent@814: #!/usr/bin/env python Laurent@814: # -*- coding: utf-8 -*- Laurent@814: andrej@1571: # This file is part of Beremiz, a Integrated Development Environment for andrej@1571: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival. Laurent@814: # andrej@1571: # Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD Laurent@814: # andrej@1571: # See COPYING file for copyrights details. Laurent@814: # andrej@1571: # This program is free software; you can redistribute it and/or andrej@1571: # modify it under the terms of the GNU General Public License andrej@1571: # as published by the Free Software Foundation; either version 2 andrej@1571: # of the License, or (at your option) any later version. Laurent@814: # andrej@1571: # This program is distributed in the hope that it will be useful, andrej@1571: # but WITHOUT ANY WARRANTY; without even the implied warranty of andrej@1571: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the andrej@1571: # GNU General Public License for more details. Laurent@814: # andrej@1571: # You should have received a copy of the GNU General Public License andrej@1571: # along with this program; if not, write to the Free Software andrej@1571: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Laurent@814: andrej@1881: andrej@1881: from __future__ import absolute_import Laurent@814: import wx Laurent@814: import wx.gizmos Laurent@814: andrej@1736: Laurent@814: class CustomEditableListBox(wx.gizmos.EditableListBox): andrej@1735: Laurent@814: def __init__(self, *args, **kwargs): Laurent@814: wx.gizmos.EditableListBox.__init__(self, *args, **kwargs) andrej@1735: 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) andrej@1735: 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)) andrej@1735: Laurent@814: self.Editing = False andrej@1735: Laurent@814: def EnsureCurrentItemVisible(self): Laurent@814: listctrl = self.GetListCtrl() Laurent@814: listctrl.EnsureVisible(listctrl.GetFocusedItem()) andrej@1735: 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() andrej@1735: 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() andrej@1735: 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 andrej@1735: 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()