~ubuntu-branches/ubuntu/utopic/python-traitsui/utopic

« back to all changes in this revision

Viewing changes to traitsui/wx/history_control.py

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2011-07-09 13:57:39 UTC
  • Revision ID: james.westby@ubuntu.com-20110709135739-x5u20q86huissmn1
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#------------------------------------------------------------------------------
 
2
#
 
3
#  Copyright (c) 2005, Enthought, Inc.
 
4
#  All rights reserved.
 
5
#
 
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
 
10
#
 
11
#  Thanks for using Enthought open source!
 
12
#
 
13
#  Author: David C. Morrill
 
14
#  Date:   10/21/2004
 
15
#
 
16
#------------------------------------------------------------------------------
 
17
 
 
18
""" Defines the a text entry field (actually a combo-box) with a drop-down list
 
19
    of values previously entered into the control.
 
20
"""
 
21
 
 
22
#-------------------------------------------------------------------------------
 
23
#  Imports:
 
24
#-------------------------------------------------------------------------------
 
25
 
 
26
import wx
 
27
 
 
28
from traits.api \
 
29
    import HasPrivateTraits, Instance, Str, List, Int, Bool
 
30
 
 
31
from pyface.timer.api \
 
32
    import do_later
 
33
 
 
34
from constants \
 
35
    import OKColor, ErrorColor
 
36
 
 
37
#-------------------------------------------------------------------------------
 
38
#  'HistoryControl' class:
 
39
#-------------------------------------------------------------------------------
 
40
 
 
41
class HistoryControl ( HasPrivateTraits ):
 
42
 
 
43
    # The UI control:
 
44
    control = Instance( wx.Window )
 
45
 
 
46
    # The current value of the control:
 
47
    value = Str
 
48
 
 
49
    # Should 'value' be updated on every keystroke?
 
50
    auto_set = Bool( False )
 
51
 
 
52
    # The current history of the control:
 
53
    history = List( Str )
 
54
 
 
55
    # The maximum number of history entries allowed:
 
56
    entries = Int( 10 )
 
57
 
 
58
    # Is the current value valid?
 
59
    error = Bool( False )
 
60
 
 
61
    #-- Public Methods ---------------------------------------------------------
 
62
 
 
63
    def create_control ( self, parent ):
 
64
        """ Creates the control.
 
65
        """
 
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 )
 
73
        if self.auto_set:
 
74
            wx.EVT_TEXT( parent, control.GetId(), self._update_value_only )
 
75
 
 
76
        return control
 
77
 
 
78
    def dispose ( self ):
 
79
        """ Disposes of the control at the end of its life cycle.
 
80
        """
 
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 )
 
86
 
 
87
    def set_value ( self, value ):
 
88
        """ Sets the specified value and adds it to the history.
 
89
        """
 
90
        self._update( value )
 
91
 
 
92
    #-- Traits Event Handlers --------------------------------------------------
 
93
 
 
94
    def _value_changed ( self, value ):
 
95
        """ Handles the 'value' trait being changed.
 
96
        """
 
97
        if not self._no_update:
 
98
            control = self.control
 
99
            if control is not None:
 
100
                control.SetValue( value )
 
101
                self._restore = False
 
102
 
 
103
    def _history_changed ( self ):
 
104
        """ Handles the 'history' being changed.
 
105
        """
 
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]
 
111
 
 
112
            self._load_history( select = False )
 
113
 
 
114
    def _error_changed ( self, error ):
 
115
        """ Handles the 'error' trait being changed.
 
116
        """
 
117
        if error:
 
118
            self.control.SetBackgroundColour( ErrorColor )
 
119
        else:
 
120
            self.control.SetBackgroundColour( OKColor )
 
121
 
 
122
        self.control.Refresh()
 
123
 
 
124
    #-- Wx Event Handlers ------------------------------------------------------
 
125
 
 
126
    def _update_value ( self, event ):
 
127
        """ Handles the user selecting something from the drop-down list of the
 
128
            combobox.
 
129
        """
 
130
        self._update( event.GetString() )
 
131
 
 
132
    def _update_value_only ( self, event ):
 
133
        """ Handles the user typing into the text field in 'auto_set' mode.
 
134
        """
 
135
        self._no_update = True
 
136
        self.value      = event.GetString()
 
137
        self._no_update = False
 
138
 
 
139
    def _update_text_value ( self, event, select = True ):
 
140
        """ Handles the user typing something into the text field of the
 
141
            combobox.
 
142
        """
 
143
        if not self._no_update:
 
144
            self._update( self.control.GetValue(), select )
 
145
 
 
146
    def _kill_focus ( self, event ):
 
147
        """ Handles the combobox losing focus.
 
148
        """
 
149
        self._update_text_value( event, False )
 
150
        event.Skip()
 
151
 
 
152
    #-- Private Methods --------------------------------------------------------
 
153
 
 
154
    def _update ( self, value, select = True ):
 
155
        """ Updates the value and history list based on a specified value.
 
156
        """
 
157
        self._no_update = True
 
158
 
 
159
        if value.strip() != '':
 
160
            history = self.history
 
161
            if (len( history ) == 0) or (value != history[0]):
 
162
                if value in history:
 
163
                    history.remove( value )
 
164
                history.insert( 0, value )
 
165
                del history[ self.entries: ]
 
166
                self._load_history( value, select )
 
167
 
 
168
        self.value = value
 
169
 
 
170
        self._no_update = False
 
171
 
 
172
    def _load_history ( self, restore = None, select = True ):
 
173
        """ Loads the current history list into the control.
 
174
        """
 
175
        control = self.control
 
176
        control.Freeze()
 
177
 
 
178
        if restore is None:
 
179
            restore = control.GetValue()
 
180
 
 
181
        control.Clear()
 
182
        for value in self.history:
 
183
            control.Append( value )
 
184
 
 
185
        self._restore = True
 
186
        do_later( self._thaw_value, restore, select )
 
187
 
 
188
    def _thaw_value ( self, restore, select ):
 
189
        """ Restores the value of the combobox control.
 
190
        """
 
191
        control = self.control
 
192
        if control is not None:
 
193
           if self._restore:
 
194
               control.SetValue( restore )
 
195
 
 
196
               if select:
 
197
                   control.SetMark( 0, len( restore ) )
 
198
 
 
199
           control.Thaw()
 
200