controls/CustomEditableListBox.py
author laurent
Sun, 08 Jan 2012 18:22:43 +0100
changeset 614 8c4b57808f9c
parent 577 9dbb79722fbc
child 640 c32c169b8f63
permissions -rw-r--r--
Disabling drag for modifying grid row size
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
        
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    33
        self.GetListCtrl().Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    34
        
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    35
        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
    36
                                               (self.GetNewButton(), _("New item"), "_OnNewButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    37
                                               (self.GetDelButton(), _("Delete item"), "_OnDelButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    38
                                               (self.GetUpButton(), _("Move up"), "_OnUpButton"),
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    39
                                               (self.GetDownButton(), _("Move down"), "_OnDownButton")]:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    40
            button.SetToolTipString(tooltip)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    41
            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
    42
    
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    43
    def EnsureCurrentItemVisible(self):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    44
        listctrl = self.GetListCtrl()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    45
        listctrl.EnsureVisible(listctrl.GetFocusedItem())
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    46
    
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    47
    def GetButtonPressedFunction(self, call_function):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    48
        def OnButtonPressed(event):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    49
            func = getattr(self, call_function, None)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    50
            if func is not None:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    51
                wx.CallAfter(func, event)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    52
            wx.CallAfter(self.EnsureCurrentItemVisible)
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    53
            event.Skip()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    54
        return OnButtonPressed
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    55
    
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    56
    def OnKeyDown(self, event):
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    57
        button = None
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    58
        keycode = event.GetKeyCode()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    59
        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
    60
            button = self.GetNewButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    61
        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
    62
            button = self.GetDelButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    63
        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
    64
            button = self.GetUpButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    65
        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
    66
            button = self.GetDownButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    67
        elif keycode == wx.WXK_SPACE:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    68
            button = self.GetEditButton()
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    69
        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
    70
            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
    71
        else:
9dbb79722fbc Adding support for giving keyboard focus to the first control of every dialogs
laurent
parents:
diff changeset
    72
            event.Skip()