Skip to content

Commit 9d335cb

Browse files
committed
added autoCapitalize
1 parent e0d08e4 commit 9d335cb

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

direct/src/gui/DirectEntry.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(self, parent = None, **kw):
6464
# Sounds to be used for button events
6565
('rolloverSound', DGG.getDefaultRolloverSound(), self.setRolloverSound),
6666
('clickSound', DGG.getDefaultClickSound(), self.setClickSound),
67+
('autoCapitalize', 0, self.autoCapitalizeFunc),
6768
)
6869
# Merge keyword options with default options
6970
self.defineoptions(kw, optiondefs)
@@ -119,8 +120,7 @@ def __init__(self, parent = None, **kw):
119120
self.set(self['initialText'])
120121

121122
def destroy(self):
122-
self.ignore(self.guiItem.getFocusInEvent())
123-
self.ignore(self.guiItem.getFocusOutEvent())
123+
self.ignoreAll()
124124
DirectFrame.destroy(self)
125125

126126
def setup(self):
@@ -166,13 +166,47 @@ def failedCommandFunc(self, event):
166166
# Pass any extra args
167167
apply(self['failedCommand'], [self.get()] + self['failedExtraArgs'])
168168

169+
def autoCapitalizeFunc(self):
170+
if self['autoCapitalize']:
171+
self.accept(self.guiItem.getTypeEvent(), self._handleTyping)
172+
self.accept(self.guiItem.getEraseEvent(), self._handleErasing)
173+
else:
174+
self.ignore(self.guiItem.getTypeEvent())
175+
self.ignore(self.guiItem.getEraseEvent())
176+
169177
def focusInCommandFunc(self):
170178
if self['focusInCommand']:
171179
apply(self['focusInCommand'], self['focusInExtraArgs'])
180+
if self['autoCapitalize']:
181+
self.accept(self.guiItem.getTypeEvent(), self._handleTyping)
182+
self.accept(self.guiItem.getEraseEvent(), self._handleErasing)
183+
184+
def _handleTyping(self, guiEvent):
185+
self._autoCapitalize()
186+
def _handleErasing(self, guiEvent):
187+
self._autoCapitalize()
188+
189+
def _autoCapitalize(self):
190+
name = self.get().decode('utf-8')
191+
# capitalize each word
192+
capName = ''
193+
for i in xrange(len(name)):
194+
character = name[i]
195+
# is it a letter?
196+
# This assumes that string.lower and string.upper will return different
197+
# values for all unicode letters.
198+
if string.lower(character) != string.upper(character):
199+
# if it's not preceded by a letter, capitalize it
200+
if (i == 0) or string.lower(name[i-1]) == string.upper(name[i-1]):
201+
character = string.upper(character)
202+
capName += character
203+
self.enterText(capName.encode('utf-8'))
172204

173205
def focusOutCommandFunc(self):
174206
if self['focusOutCommand']:
175207
apply(self['focusOutCommand'], self['focusOutExtraArgs'])
208+
self.ignore(self.guiItem.getTypeEvent())
209+
self.ignore(self.guiItem.getEraseEvent())
176210

177211
def set(self, text):
178212
""" Changes the text currently showing in the typable region;

0 commit comments

Comments
 (0)