/usr/lib/python2.7/dist-packages/traitsui/wx/key_binding_editor.py is in python-traitsui 4.4.0-1.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | #-------------------------------------------------------------------------------
#
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
#
# Author: David C. Morrill
# Date: 05/20/2005
#
#-------------------------------------------------------------------------------
""" Defines the key binding editor for use with the KeyBinding class. This is a
specialized editor used to associate a particular key with a control (i.e.,
the key binding editor).
"""
#-------------------------------------------------------------------------------
# Imports:
#-------------------------------------------------------------------------------
import wx
from traits.api \
import Event, false
# FIXME: ToolkitEditorFactory is a proxy class defined here just for backward
# compatibility. The class has been moved to the
# traitsui.editors.key_binding_editor file.
from traitsui.editors.key_binding_editor \
import KeyBindingEditor as ToolkitEditorFactory
from pyface.wx.dialog \
import confirmation
from editor \
import Editor
from key_event_to_name \
import key_event_to_name
#-------------------------------------------------------------------------------
# 'KeyBindingEditor' class:
#-------------------------------------------------------------------------------
class KeyBindingEditor ( Editor ):
""" An editor for modifying bindings of keys to controls.
"""
#---------------------------------------------------------------------------
# Trait definitions:
#---------------------------------------------------------------------------
# Does the editor's control have focus currently?
has_focus = false
# Keyboard event
key = Event
# Clear field event
clear = Event
#---------------------------------------------------------------------------
# Finishes initializing the editor by creating the underlying toolkit
# widget:
#---------------------------------------------------------------------------
def init ( self, parent ):
""" Finishes initializing the editor by creating the underlying toolkit
widget.
"""
self.control = KeyBindingCtrl( self, parent, size = wx.Size( 160, 19 ) )
#---------------------------------------------------------------------------
# Handles the user entering input data in the edit control:
#---------------------------------------------------------------------------
def update_object ( self, event ):
""" Handles the user entering input data in the edit control.
"""
try:
self.value = value = key_event_to_name( event )
self._binding.text = value
except:
pass
#---------------------------------------------------------------------------
# Updates the editor when the object trait changes external to the editor:
#---------------------------------------------------------------------------
def update_editor ( self ):
""" Updates the editor when the object trait changes externally to the
editor.
"""
self.control.Refresh()
#---------------------------------------------------------------------------
# Updates the current focus setting of the control:
#---------------------------------------------------------------------------
def update_focus ( self, has_focus ):
""" Updates the current focus setting of the control.
"""
if has_focus:
self._binding.border_size = 1
self.object.owner.focus_owner = self._binding
#---------------------------------------------------------------------------
# Handles a keyboard event:
#---------------------------------------------------------------------------
def _key_changed ( self, event ):
""" Handles a keyboard event.
"""
binding = self.object
key_name = key_event_to_name( event )
cur_binding = binding.owner.key_binding_for( binding, key_name )
if cur_binding is not None:
if confirmation( None,
"'%s' has already been assigned to '%s'.\n"
"Do you wish to continue?" % (
key_name, cur_binding.description ),
'Duplicate Key Definition' ) == 5104:
return
self.value = key_name
#---------------------------------------------------------------------------
# Handles a clear field event:
#---------------------------------------------------------------------------
def _clear_changed ( self ):
""" Handles a clear field event.
"""
self.value = ''
#-------------------------------------------------------------------------------
# 'KeyBindingCtrl' class:
#-------------------------------------------------------------------------------
class KeyBindingCtrl ( wx.Window ):
""" wxPython control for editing key bindings.
"""
#---------------------------------------------------------------------------
# Initialize the object:
#---------------------------------------------------------------------------
def __init__ ( self, editor, parent, wid = -1, pos = wx.DefaultPosition,
size = wx.DefaultSize ):
super( KeyBindingCtrl, self ).__init__( parent, wid, pos, size,
style = wx.CLIP_CHILDREN |
wx.WANTS_CHARS )
# Save the reference to the controlling editor object:
self.editor = editor
# Indicate we don't have the focus right now:
editor.has_focus = False
# Set up the 'erase background' event handler:
wx.EVT_ERASE_BACKGROUND( self, self._on_erase_background )
# Set up the 'paint' event handler:
wx.EVT_PAINT( self, self._paint )
# Set up the focus change handlers:
wx.EVT_SET_FOCUS( self, self._get_focus )
wx.EVT_KILL_FOCUS( self, self._lose_focus )
# Set up mouse event handlers:
wx.EVT_LEFT_DOWN( self, self._set_focus )
wx.EVT_LEFT_DCLICK( self, self._clear_contents )
# Handle key events:
wx.EVT_CHAR( self, self._on_char )
#---------------------------------------------------------------------------
# Handle keyboard keys being pressed:
#---------------------------------------------------------------------------
def _on_char ( self, event ):
""" Handle keyboard keys being pressed.
"""
self.editor.key = event
#---------------------------------------------------------------------------
# Erase background event handler:
#---------------------------------------------------------------------------
def _on_erase_background ( self, event ):
pass
#---------------------------------------------------------------------------
# Do a GUI toolkit specific screen update:
#---------------------------------------------------------------------------
def _paint ( self, event ):
""" Updates the screen.
"""
wdc = wx.PaintDC( self )
dx, dy = self.GetSizeTuple()
if self.editor.has_focus:
wdc.SetPen( wx.Pen( wx.RED, 2 ) )
wdc.DrawRectangle( 1, 1, dx - 1, dy - 1 )
else:
wdc.SetPen( wx.Pen( wx.BLACK ) )
wdc.DrawRectangle( 0, 0, dx, dy )
wdc.SetFont( self.GetFont() )
wdc.DrawText( self.editor.str_value, 5, 3 )
#---------------------------------------------------------------------------
# Sets the keyboard focus to this window:
#---------------------------------------------------------------------------
def _set_focus ( self, event ):
""" Sets the keyboard focus to this window.
"""
self.SetFocus()
#---------------------------------------------------------------------------
# Handles getting/losing the focus:
#---------------------------------------------------------------------------
def _get_focus ( self, event ):
""" Handles getting the focus.
"""
self.editor.has_focus = True
self.Refresh()
def _lose_focus ( self, event ):
""" Handles losing the focus.
"""
self.editor.has_focus = False
self.Refresh()
#---------------------------------------------------------------------------
# Handles the user double clicking the control to clear its contents:
#---------------------------------------------------------------------------
def _clear_contents ( self, event ):
""" Handles the user double clicking the control to clear its contents.
"""
self.editor.clear = True
### EOF #######################################################################
|