controls/CustomEditableListBox.py
author Laurent Bessard
Wed, 05 Sep 2012 12:39:50 +0200
changeset 761 996515c4b394
parent 714 131ea7f237b9
permissions -rw-r--r--
Fix bug when drag'n dropping location with undefined direction on Windows
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
        
714
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 640
diff changeset
    38
        for button, tooltip, call_function in [
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 640
diff changeset
    39
                (self.GetEditButton(), _("Edit item"), "_OnEditButton"),
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 640
diff changeset
    40
                (self.GetNewButton(), _("New item"), "_OnNewButton"),
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 640
diff changeset
    41
                (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 640
diff changeset
    42
                (self.GetUpButton(), _("Move up"), "_OnUpButton"),
131ea7f237b9 Replacing buttons with text by buttons with icons
Laurent Bessard
parents: 640
diff changeset
    43
                (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    44
            button.SetToolTipString(tooltip)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    45
            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
    46
    
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
    47
        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
    48
    
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    49
    def EnsureCurrentItemVisible(self):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    50
        listctrl = self.GetListCtrl()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    51
        listctrl.EnsureVisible(listctrl.GetFocusedItem())
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    52
    
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
    53
    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
    54
        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
    55
        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
    56
        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
    57
            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
    58
        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
    59
            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
    60
        
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
    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
    62
        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
    63
        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
    64
        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
    65
            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
    66
        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
    67
            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
    68
    
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    69
    def GetButtonPressedFunction(self, call_function):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    70
        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
    71
            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
    72
                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
    73
                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
    74
                    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
    75
                    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
    76
                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
    77
                    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
    78
                    event.Skip()
577
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    79
        return OnButtonPressed
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    80
    
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    81
    def OnKeyDown(self, event):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    82
        button = None
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    83
        keycode = event.GetKeyCode()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    84
        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
    85
            button = self.GetNewButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    86
        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
    87
            button = self.GetDelButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    88
        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
    89
            button = self.GetUpButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    90
        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
    91
            button = self.GetDownButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    92
        elif keycode == wx.WXK_SPACE:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    93
            button = self.GetEditButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    94
        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
    95
            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
    96
        else:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    97
            event.Skip()