forked from tweecode/twine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchpanels.py
263 lines (203 loc) · 9.93 KB
/
searchpanels.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import re, wx
import metrics
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 __init__(self, parent, onFind = None, onClose = None):
self.findCallback = onFind
self.closeCallback = onClose
wx.Panel.__init__(self, parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
# find text and label
findSizer = wx.BoxSizer(wx.HORIZONTAL)
findSizer.Add(wx.StaticText(self, label = 'Find'), flag = wx.BOTTOM | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, \
border = metrics.size('relatedControls'), proportion = 0)
self.findField = wx.TextCtrl(self)
findSizer.Add(self.findField, proportion = 1, flag = wx.BOTTOM | wx.EXPAND, \
border = metrics.size('relatedControls'))
sizer.Add(findSizer, flag = wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, border = metrics.size('windowBorder'))
# option checkboxes
optionSizer = wx.BoxSizer(wx.HORIZONTAL)
self.caseCheckbox = wx.CheckBox(self, label = 'Match Case')
self.wholeWordCheckbox = wx.CheckBox(self, label = 'Whole Word')
self.regexpCheckbox = wx.CheckBox(self, label = 'Regular Expression')
optionSizer.Add(self.caseCheckbox, flag = wx.BOTTOM | wx.RIGHT, border = metrics.size('relatedControls'))
optionSizer.Add(self.wholeWordCheckbox, flag = wx.BOTTOM | wx.LEFT | wx.RIGHT, \
border = metrics.size('relatedControls'))
optionSizer.Add(self.regexpCheckbox, flag = wx.BOTTOM | wx.LEFT, \
border = metrics.size('relatedControls'))
sizer.Add(optionSizer, flag = wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, \
border = metrics.size('windowBorder'))
# find and close buttons
buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.closeButton = wx.Button(self, label = 'Close')
self.closeButton.Bind(wx.EVT_BUTTON, self.onClose)
self.findButton = wx.Button(self, label = 'Find Next')
self.findButton.Bind(wx.EVT_BUTTON, self.onFind)
buttonSizer.Add(self.closeButton, flag = wx.TOP | wx.RIGHT, border = metrics.size('buttonSpace'))
buttonSizer.Add(self.findButton, flag = wx.TOP, border = metrics.size('buttonSpace'))
sizer.Add(buttonSizer, flag = wx.ALIGN_RIGHT | wx.BOTTOM | wx.LEFT | wx.RIGHT, \
border = metrics.size('windowBorder'))
sizer.Fit(self)
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 __init__(self, parent, allowIncremental = True, \
onFind = None, onReplace = None, onReplaceAll = None, onClose = None):
self.allowIncremental = allowIncremental
self.findCallback = onFind
self.replaceCallback = onReplace
self.replaceAllCallback = onReplaceAll
self.closeCallback = onClose
wx.Panel.__init__(self, parent)
sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(sizer)
fieldSizer = wx.FlexGridSizer(2, 2)
fieldSizer.AddGrowableCol(1, 1)
# find text and label
fieldSizer.Add(wx.StaticText(self, label = 'Find'), \
flag = wx.BOTTOM | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, \
border = metrics.size('relatedControls'), proportion = 0)
self.findField = wx.TextCtrl(self)
fieldSizer.Add(self.findField, proportion = 1, flag = wx.BOTTOM | wx.EXPAND, \
border = metrics.size('relatedControls'))
# replace text and label
fieldSizer.Add(wx.StaticText(self, label = 'Replace With'), \
flag = wx.BOTTOM | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, \
border = metrics.size('relatedControls'), proportion = 0)
self.replaceField = wx.TextCtrl(self)
fieldSizer.Add(self.replaceField, proportion = 1, flag = wx.BOTTOM | wx.EXPAND | wx.ALIGN_CENTER_VERTICAL, \
border = metrics.size('relatedControls'))
sizer.Add(fieldSizer, flag = wx.EXPAND | wx.TOP | wx.LEFT | wx.RIGHT, border = metrics.size('windowBorder'))
# option checkboxes
optionSizer = wx.BoxSizer(wx.HORIZONTAL)
self.caseCheckbox = wx.CheckBox(self, label = 'Match Case')
self.wholeWordCheckbox = wx.CheckBox(self, label = 'Whole Word')
self.regexpCheckbox = wx.CheckBox(self, label = 'Regular Expression')
optionSizer.Add(self.caseCheckbox, flag = wx.BOTTOM | wx.TOP | wx.RIGHT, \
border = metrics.size('relatedControls'))
optionSizer.Add(self.wholeWordCheckbox, flag = wx.BOTTOM | wx.TOP | wx.LEFT | wx.RIGHT, \
border = metrics.size('relatedControls'))
optionSizer.Add(self.regexpCheckbox, flag = wx.BOTTOM | wx.TOP | wx.LEFT, \
border = metrics.size('relatedControls'))
sizer.Add(optionSizer, flag = wx.LEFT | wx.RIGHT, border = metrics.size('windowBorder'))
# find and close buttons
buttonSizer = wx.BoxSizer(wx.HORIZONTAL)
self.closeButton = wx.Button(self, label = 'Close')
self.closeButton.Bind(wx.EVT_BUTTON, self.onClose)
buttonSizer.Add(self.closeButton, flag = wx.TOP | wx.RIGHT, border = metrics.size('buttonSpace'))
if allowIncremental:
buttonSizer.Add(wx.Panel(self))
self.findButton = wx.Button(self, label = 'Find Next')
self.findButton.Bind(wx.EVT_BUTTON, self.onFind)
buttonSizer.Add(self.findButton, flag = wx.TOP | wx.LEFT | wx.RIGHT, \
border = metrics.size('buttonSpace'))
self.replaceButton = wx.Button(self, label = 'Replace')
self.replaceButton.Bind(wx.EVT_BUTTON, self.onReplace)
buttonSizer.Add(self.replaceButton, flag = wx.TOP | wx.RIGHT, border = metrics.size('buttonSpace'))
self.replaceAllButton = wx.Button(self, label = 'Replace All')
self.replaceAllButton.Bind(wx.EVT_BUTTON, self.onReplaceAll)
buttonSizer.Add(self.replaceAllButton, flag = wx.TOP, border = metrics.size('buttonSpace'))
sizer.Add(buttonSizer, flag = wx.ALIGN_RIGHT | wx.LEFT | wx.RIGHT | wx.BOTTOM, \
border = metrics.size('windowBorder'))
sizer.Fit(self)
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'
return result