class FindPanel(wx.Panel):
"""
This allows the user to enter a search term and select various
criteria (i.e. "match case", etc.) There are two callbacks:
onFind (regexp, flags)
Regexp corresponds to the user's search, and flags should be used
when performing that search.
onClose()
When the user clicks the Close button.
"""
def focus(self):
"""
Focuses the proper text input and sets our default button.
"""
self.findField.SetFocus()
self.findButton.SetDefault()
def updateUI(self, event):
pass
def onFind(self, event):
"""
Assembles a regexp based on field values and passes it on to our callback.
"""
if self.findCallback:
regexp = self.findField.GetValue()
flags = None
if not self.caseCheckbox.GetValue():
flags = re.IGNORECASE
if not self.regexpCheckbox.GetValue():
regexp = re.escape(regexp)
if self.wholeWordCheckbox.GetValue():
regexp = r'\b' + regexp + r'\b'
self.findCallback(regexp, flags)
def onClose(self, event):
"""
Passes on a close message to our callback.
"""
if self.closeCallback: self.closeCallback()
class ReplacePanel(wx.Panel):
"""
This allows the user to enter a search and replace term and select
various criteria (i.e. "match case", etc.) There are two callbacks:
onFind (regexp, flags)
Regexp corresponds to the user's search, and flags should be used
when performing that search.
onReplace (regexp, flags, replaceTerm)
Like find, only with a replaceTerm.
onReplaceAll (regexp, flags, replaceTerm)
Like replace, only the user is signalling that they want to replace
all instances at once.
onClose()
When the user clicks the Close button.
You may also pass in a parameter to set whether users can perform
incremental searches, or if they may only replace all.
"""
def focus(self):
"""
Focuses the proper text input and sets our default button.
"""
self.findField.SetFocus()
if self.allowIncremental:
self.replaceButton.SetDefault()
else:
self.replaceAllButton.SetDefault()
def onFind(self, event):
"""
Passes a find message to our callback.
"""
if self.findCallback:
regexps = self.assembleRegexps()
self.findCallback(regexps['find'], regexps['flags'])
def onReplace(self, event):
"""
Passes a replace message to our callback.
"""
if self.replaceCallback:
regexps = self.assembleRegexps()
self.replaceCallback(regexps['find'], regexps['flags'], regexps['replace'])
def onReplaceAll(self, event):
"""
Passes a replace all message to our callback.
"""
if self.replaceAllCallback:
regexps = self.assembleRegexps()
self.replaceAllCallback(regexps['find'], regexps['flags'], regexps['replace'])
def onClose(self, event):
"""
Passes on a close message to our callback.
"""
if self.closeCallback: self.closeCallback()
def assembleRegexps(self):
"""
Builds up the regexp the user is searching for. Returns a dictionary with
keys 'find', 'replace', and 'flags'.
"""
result = {}
result['find'] = self.findField.GetValue()
result['replace'] = self.replaceField.GetValue()
result['flags'] = None
if not self.regexpCheckbox.GetValue():
result['find'] = re.escape(result['find'])
if not self.caseCheckbox.GetValue():
result['flags'] = re.IGNORECASE
if self.wholeWordCheckbox.GetValue():
result['find'] = r'\b' + result['find'] + r'\b'