controls/CustomEditableListBox.py
author Laurent Bessard
Wed, 06 Jun 2012 16:57:58 +0200
changeset 704 aca0c83ed82e
parent 640 c32c169b8f63
child 714 131ea7f237b9
permissions -rw-r--r--
Replacing dialog asking for a name when drag'n drop function block to viewer by automatically generated name
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     1
#!/usr/bin/env python
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     3
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     5
#based on the plcopen standard. 
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     6
#
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     8
#
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
     9
#See COPYING file for copyrights details.
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    10
#
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    12
#modify it under the terms of the GNU General Public
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    15
#
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    19
#General Public License for more details.
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    20
#
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    21
#You should have received a copy of the GNU General Public
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    24
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    25
import wx
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    26
import wx.gizmos
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    27
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    28
class CustomEditableListBox(wx.gizmos.EditableListBox):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    29
    
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    30
    def __init__(self, *args, **kwargs):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    31
        wx.gizmos.EditableListBox.__init__(self, *args, **kwargs)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    32
        
640
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    33
        listbox = self.GetListCtrl()
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    34
        listbox.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    35
        listbox.Bind(wx.EVT_LIST_BEGIN_LABEL_EDIT, self.OnLabelBeginEdit)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    36
        listbox.Bind(wx.EVT_LIST_END_LABEL_EDIT, self.OnLabelEndEdit)
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    37
        
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    38
        for button, tooltip, call_function in [(self.GetEditButton(), _("Edit item"), "_OnEditButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    39
                                               (self.GetNewButton(), _("New item"), "_OnNewButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    40
                                               (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    41
                                               (self.GetUpButton(), _("Move up"), "_OnUpButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    42
                                               (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    43
            button.SetToolTipString(tooltip)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    44
            button.Bind(wx.EVT_BUTTON, self.GetButtonPressedFunction(call_function))
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    45
    
640
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    46
        self.Editing = False
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    47
    
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    48
    def EnsureCurrentItemVisible(self):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    49
        listctrl = self.GetListCtrl()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    50
        listctrl.EnsureVisible(listctrl.GetFocusedItem())
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    51
    
640
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    52
    def OnLabelBeginEdit(self, event):
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    53
        self.Editing = True
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    54
        func = getattr(self, "_OnLabelBeginEdit", None)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    55
        if func is not None:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    56
            func(event)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    57
        else:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    58
            event.Skip()
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    59
        
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    60
    def OnLabelEndEdit(self, event):
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    61
        self.Editing = False
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    62
        func = getattr(self, "_OnLabelEndEdit", None)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    63
        if func is not None:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    64
            func(event)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    65
        else:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    66
            event.Skip()
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    67
    
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    68
    def GetButtonPressedFunction(self, call_function):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    69
        def OnButtonPressed(event):
640
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    70
            if wx.Platform != '__WXMSW__' or not self.Editing:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    71
                func = getattr(self, call_function, None)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    72
                if func is not None:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    73
                    func(event)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    74
                    wx.CallAfter(self.EnsureCurrentItemVisible)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    75
                else:
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    76
                    wx.CallAfter(self.EnsureCurrentItemVisible)
c32c169b8f63 Fixing segmentation fault in CustomEditableListBox on Windows when clicking on an header button while a value is being edited
laurent
parents: 577
diff changeset
    77
                    event.Skip()
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    78
        return OnButtonPressed
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    79
    
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    80
    def OnKeyDown(self, event):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    81
        button = None
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    82
        keycode = event.GetKeyCode()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    83
        if keycode in (wx.WXK_ADD, wx.WXK_NUMPAD_ADD):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    84
            button = self.GetNewButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    85
        elif keycode in (wx.WXK_DELETE, wx.WXK_NUMPAD_DELETE):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    86
            button = self.GetDelButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    87
        elif keycode == wx.WXK_UP and event.ShiftDown():
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    88
            button = self.GetUpButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    89
        elif keycode == wx.WXK_DOWN and event.ShiftDown():
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    90
            button = self.GetDownButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    91
        elif keycode == wx.WXK_SPACE:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    92
            button = self.GetEditButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    93
        if button is not None and button.IsEnabled():
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    94
            button.ProcessEvent(wx.CommandEvent(wx.EVT_BUTTON.typeId, button.GetId()))
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    95
        else:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    96
            event.Skip()