1
#------------------------------------------------------------------------------
3
# Copyright (c) 2005, Enthought, Inc.
6
# This software is provided without warranty under the terms of the BSD
7
# license included in enthought/LICENSE.txt and may be redistributed only
8
# under the conditions described in the aforementioned license. The license
9
# is also available online at http://www.enthought.com/licenses/BSD.txt
11
# Thanks for using Enthought open source!
13
# Author: David C. Morrill
16
#------------------------------------------------------------------------------
18
""" Defines the a text entry field (actually a combo-box) with a drop-down list
19
of values previously entered into the control.
22
#-------------------------------------------------------------------------------
24
#-------------------------------------------------------------------------------
29
import HasPrivateTraits, Instance, Str, List, Int, Bool
31
from pyface.timer.api \
35
import OKColor, ErrorColor
37
#-------------------------------------------------------------------------------
38
# 'HistoryControl' class:
39
#-------------------------------------------------------------------------------
41
class HistoryControl ( HasPrivateTraits ):
44
control = Instance( wx.Window )
46
# The current value of the control:
49
# Should 'value' be updated on every keystroke?
50
auto_set = Bool( False )
52
# The current history of the control:
55
# The maximum number of history entries allowed:
58
# Is the current value valid?
61
#-- Public Methods ---------------------------------------------------------
63
def create_control ( self, parent ):
64
""" Creates the control.
66
self.control = control = wx.ComboBox( parent, -1, self.value,
67
wx.Point( 0, 0 ), wx.Size( -1, -1 ),
68
self.history, style = wx.CB_DROPDOWN )
69
wx.EVT_COMBOBOX( parent, control.GetId(), self._update_value )
70
wx.EVT_KILL_FOCUS( control, self._kill_focus )
71
wx.EVT_TEXT_ENTER( parent, control.GetId(),
72
self._update_text_value )
74
wx.EVT_TEXT( parent, control.GetId(), self._update_value_only )
79
""" Disposes of the control at the end of its life cycle.
81
control, self.control = self.control, None
82
parent = control.GetParent()
83
wx.EVT_COMBOBOX( parent, control.GetId(), None )
84
wx.EVT_TEXT_ENTER( parent, control.GetId(), None )
85
wx.EVT_KILL_FOCUS( control, None )
87
def set_value ( self, value ):
88
""" Sets the specified value and adds it to the history.
92
#-- Traits Event Handlers --------------------------------------------------
94
def _value_changed ( self, value ):
95
""" Handles the 'value' trait being changed.
97
if not self._no_update:
98
control = self.control
99
if control is not None:
100
control.SetValue( value )
101
self._restore = False
103
def _history_changed ( self ):
104
""" Handles the 'history' being changed.
106
if not self._no_update:
107
if self._first_time is None:
108
self._first_time = False
109
if (self.value == '') and (len( self.history ) > 0):
110
self.value = self.history[0]
112
self._load_history( select = False )
114
def _error_changed ( self, error ):
115
""" Handles the 'error' trait being changed.
118
self.control.SetBackgroundColour( ErrorColor )
120
self.control.SetBackgroundColour( OKColor )
122
self.control.Refresh()
124
#-- Wx Event Handlers ------------------------------------------------------
126
def _update_value ( self, event ):
127
""" Handles the user selecting something from the drop-down list of the
130
self._update( event.GetString() )
132
def _update_value_only ( self, event ):
133
""" Handles the user typing into the text field in 'auto_set' mode.
135
self._no_update = True
136
self.value = event.GetString()
137
self._no_update = False
139
def _update_text_value ( self, event, select = True ):
140
""" Handles the user typing something into the text field of the
143
if not self._no_update:
144
self._update( self.control.GetValue(), select )
146
def _kill_focus ( self, event ):
147
""" Handles the combobox losing focus.
149
self._update_text_value( event, False )
152
#-- Private Methods --------------------------------------------------------
154
def _update ( self, value, select = True ):
155
""" Updates the value and history list based on a specified value.
157
self._no_update = True
159
if value.strip() != '':
160
history = self.history
161
if (len( history ) == 0) or (value != history[0]):
163
history.remove( value )
164
history.insert( 0, value )
165
del history[ self.entries: ]
166
self._load_history( value, select )
170
self._no_update = False
172
def _load_history ( self, restore = None, select = True ):
173
""" Loads the current history list into the control.
175
control = self.control
179
restore = control.GetValue()
182
for value in self.history:
183
control.Append( value )
186
do_later( self._thaw_value, restore, select )
188
def _thaw_value ( self, restore, select ):
189
""" Restores the value of the combobox control.
191
control = self.control
192
if control is not None:
194
control.SetValue( restore )
197
control.SetMark( 0, len( restore ) )