txtentry.py
3.3 KB
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
###############################################################################
# Name: txtentry.py #
# Purpose: Text entry control base clases #
# Author: Cody Precord <cprecord@editra.org> #
# Copyright: (c) 2008 Cody Precord <staff@editra.org> #
# License: wxWindows License #
###############################################################################
"""
Editra Control Library: TextEntry
Text entry base and helper classes.
"""
__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: txtentry.py 67500 2011-04-15 22:51:55Z CJP $"
__revision__ = "$Revision: 67500 $"
__all__ = ["CommandEntryBase",]
#-----------------------------------------------------------------------------#
# Imports
import wx
#-----------------------------------------------------------------------------#
class CommandEntryBase(wx.SearchCtrl):
"""Base single line text control with key event handling callbacks."""
def __init__(self, parent, id=wx.ID_ANY, value='', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=0, validator=wx.DefaultValidator,
name="CommandEntryBase"):
clone = None
if validator != wx.DefaultValidator:
clone = validator.Clone()
super(CommandEntryBase, self).__init__(parent, id, value, pos,
size, style, validator, name)
# Attributes
self._txtctrl = None # For msw/gtk
self._enterhook = None
# Hide the search button and text by default
self.ShowSearchButton(False)
self.ShowCancelButton(False)
self.SetDescriptiveText(wx.EmptyString)
# MSW/GTK HACK need to bind directly to the text control component
if wx.Platform in ['__WXGTK__', '__WXMSW__']:
for child in self.GetChildren():
if isinstance(child, wx.TextCtrl):
if clone is not None:
child.SetValidator(clone)
self._txtctrl = child
child.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
child.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
break
else:
self._txtctrl = self
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
# Event management
self.Bind(wx.EVT_TEXT_ENTER, self.OnEnter)
EnterCallback = property(lambda self: self._enterhook,
lambda self, cback: setattr(self, '_enterhook', cback))
def GetTextControl(self):
"""Get the wx.TextCtrl window.
@note: only for msw/gtk
"""
return self._txtctrl
def OnKeyDown(self, evt):
"""Handle KeyDown events"""
evt.Skip()
def OnKeyUp(self, evt):
"""Handle KeyUp events"""
evt.Skip()
def OnEnter(self, evt):
"""Handle the Enter key event"""
if self.EnterCallback:
self.EnterCallback()
else:
evt.Skip()
def SetFocus(self):
"""Set the focus and select the text in the field"""
super(CommandEntryBase, self).SetFocus()
self.SelectAll()