]>
Commit | Line | Data |
---|---|---|
1f780e48 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: IDEFindService.py | |
3 | # Purpose: Find Service for pydocview | |
4 | # | |
5 | # Author: Morgan Hua | |
6 | # | |
7 | # Created: 8/15/03 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2004-2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | import wx | |
14 | import wx.lib.docview | |
15 | import os | |
16 | from os.path import join | |
17 | import re | |
18 | import ProjectEditor | |
19 | import MessageService | |
20 | import FindService | |
21 | import OutlineService | |
22 | _ = wx.GetTranslation | |
23 | ||
24 | ||
25 | #---------------------------------------------------------------------------- | |
26 | # Constants | |
27 | #---------------------------------------------------------------------------- | |
28 | FILENAME_MARKER = _("Found in file: ") | |
29 | PROJECT_MARKER = _("Searching project: ") | |
aca310e5 | 30 | FILE_MARKER = _("Searching file: ") |
1f780e48 RD |
31 | FIND_MATCHDIR = "FindMatchDir" |
32 | FIND_MATCHDIRSUBFOLDERS = "FindMatchDirSubfolders" | |
33 | ||
34 | SPACE = 10 | |
35 | HALF_SPACE = 5 | |
36 | ||
37 | ||
38 | class FindInDirService(FindService.FindService): | |
39 | ||
40 | #---------------------------------------------------------------------------- | |
41 | # Constants | |
42 | #---------------------------------------------------------------------------- | |
aca310e5 | 43 | FINDFILE_ID = wx.NewId() # for bringing up Find in File dialog box |
1f780e48 RD |
44 | FINDALL_ID = wx.NewId() # for bringing up Find All dialog box |
45 | FINDDIR_ID = wx.NewId() # for bringing up Find Dir dialog box | |
46 | ||
47 | ||
48 | def InstallControls(self, frame, menuBar = None, toolBar = None, statusBar = None, document = None): | |
49 | FindService.FindService.InstallControls(self, frame, menuBar, toolBar, statusBar, document) | |
50 | ||
51 | editMenu = menuBar.GetMenu(menuBar.FindMenu(_("&Edit"))) | |
aca310e5 RD |
52 | wx.EVT_MENU(frame, FindInDirService.FINDFILE_ID, self.ProcessEvent) |
53 | wx.EVT_UPDATE_UI(frame, FindInDirService.FINDFILE_ID, self.ProcessUpdateUIEvent) | |
54 | editMenu.Append(FindInDirService.FINDFILE_ID, _("Find in File...\tCtrl+Shift+F"), _("Searches for the specified text in the current file")) | |
1f780e48 RD |
55 | wx.EVT_MENU(frame, FindInDirService.FINDALL_ID, self.ProcessEvent) |
56 | wx.EVT_UPDATE_UI(frame, FindInDirService.FINDALL_ID, self.ProcessUpdateUIEvent) | |
aca310e5 | 57 | editMenu.Append(FindInDirService.FINDALL_ID, _("Find in Project...\tCtrl+Shift+P"), _("Searches for the specified text in all the files in the project")) |
1f780e48 RD |
58 | wx.EVT_MENU(frame, FindInDirService.FINDDIR_ID, self.ProcessEvent) |
59 | wx.EVT_UPDATE_UI(frame, FindInDirService.FINDDIR_ID, self.ProcessUpdateUIEvent) | |
aca310e5 | 60 | editMenu.Append(FindInDirService.FINDDIR_ID, _("Find in Directory...\tCtrl+Shift+D"), _("Searches for the specified text in all the files in the directory")) |
1f780e48 RD |
61 | |
62 | ||
63 | def ProcessEvent(self, event): | |
64 | id = event.GetId() | |
aca310e5 | 65 | if id == FindInDirService.FINDFILE_ID: |
bbf7159c RD |
66 | view = wx.GetApp().GetDocumentManager().GetCurrentView() |
67 | if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): | |
aca310e5 | 68 | self.ShowFindInFileDialog(view.GetCtrl().GetSelectedText()) |
bbf7159c | 69 | else: |
aca310e5 RD |
70 | self.ShowFindInFileDialog() |
71 | return True | |
72 | elif id == FindInDirService.FINDALL_ID: | |
73 | view = wx.GetApp().GetDocumentManager().GetCurrentView() | |
74 | if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): | |
75 | self.ShowFindInProjectDialog(view.GetCtrl().GetSelectedText()) | |
76 | else: | |
77 | self.ShowFindInProjectDialog() | |
1f780e48 RD |
78 | return True |
79 | elif id == FindInDirService.FINDDIR_ID: | |
bbf7159c RD |
80 | view = wx.GetApp().GetDocumentManager().GetCurrentView() |
81 | if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): | |
aca310e5 | 82 | self.ShowFindInDirDialog(view.GetCtrl().GetSelectedText()) |
bbf7159c | 83 | else: |
aca310e5 | 84 | self.ShowFindInDirDialog() |
1f780e48 RD |
85 | return True |
86 | else: | |
87 | return FindService.FindService.ProcessEvent(self, event) | |
88 | ||
89 | ||
90 | def ProcessUpdateUIEvent(self, event): | |
91 | id = event.GetId() | |
aca310e5 RD |
92 | if id == FindInDirService.FINDFILE_ID: |
93 | view = wx.GetApp().GetDocumentManager().GetCurrentView() | |
94 | if view and view.GetDocument() and not isinstance(view.GetDocument(), ProjectEditor.ProjectDocument): # don't search project model | |
95 | event.Enable(True) | |
96 | else: | |
97 | event.Enable(False) | |
98 | return True | |
99 | elif id == FindInDirService.FINDALL_ID: | |
1f780e48 | 100 | projectService = wx.GetApp().GetService(ProjectEditor.ProjectService) |
02b800ce | 101 | if projectService.GetFilesFromCurrentProject(): |
1f780e48 RD |
102 | event.Enable(True) |
103 | else: | |
104 | event.Enable(False) | |
105 | return True | |
106 | elif id == FindInDirService.FINDDIR_ID: | |
107 | event.Enable(True) | |
108 | else: | |
109 | return FindService.FindService.ProcessUpdateUIEvent(self, event) | |
110 | ||
111 | ||
aca310e5 | 112 | def ShowFindInDirDialog(self, findString=None): |
1f780e48 RD |
113 | config = wx.ConfigBase_Get() |
114 | ||
02b800ce | 115 | frame = wx.Dialog(wx.GetApp().GetTopWindow(), -1, _("Find in Directory"), size= (320,200)) |
1f780e48 RD |
116 | borderSizer = wx.BoxSizer(wx.HORIZONTAL) |
117 | ||
118 | contentSizer = wx.BoxSizer(wx.VERTICAL) | |
119 | lineSizer = wx.BoxSizer(wx.HORIZONTAL) | |
120 | lineSizer.Add(wx.StaticText(frame, -1, _("Directory:")), 0, wx.ALIGN_CENTER | wx.RIGHT, HALF_SPACE) | |
121 | dirCtrl = wx.TextCtrl(frame, -1, config.Read(FIND_MATCHDIR, ""), size=(200,-1)) | |
122 | dirCtrl.SetToolTipString(dirCtrl.GetValue()) | |
123 | lineSizer.Add(dirCtrl, 0, wx.LEFT, HALF_SPACE) | |
6f1a3f9c | 124 | findDirButton = wx.Button(frame, -1, _("Browse...")) |
1f780e48 RD |
125 | lineSizer.Add(findDirButton, 0, wx.LEFT, HALF_SPACE) |
126 | contentSizer.Add(lineSizer, 0, wx.BOTTOM, SPACE) | |
aca310e5 | 127 | |
1f780e48 RD |
128 | def OnBrowseButton(event): |
129 | dlg = wx.DirDialog(frame, _("Choose a directory:"), style=wx.DD_DEFAULT_STYLE) | |
130 | dir = dirCtrl.GetValue() | |
131 | if len(dir): | |
132 | dlg.SetPath(dir) | |
02b800ce | 133 | dlg.CenterOnParent() |
1f780e48 RD |
134 | if dlg.ShowModal() == wx.ID_OK: |
135 | dirCtrl.SetValue(dlg.GetPath()) | |
136 | dirCtrl.SetToolTipString(dirCtrl.GetValue()) | |
137 | dirCtrl.SetInsertionPointEnd() | |
1f780e48 RD |
138 | dlg.Destroy() |
139 | wx.EVT_BUTTON(findDirButton, -1, OnBrowseButton) | |
140 | ||
b792147d | 141 | subfolderCtrl = wx.CheckBox(frame, -1, _("Search in subdirectories")) |
1f780e48 RD |
142 | subfolderCtrl.SetValue(config.ReadInt(FIND_MATCHDIRSUBFOLDERS, True)) |
143 | contentSizer.Add(subfolderCtrl, 0, wx.BOTTOM, SPACE) | |
144 | ||
145 | lineSizer = wx.BoxSizer(wx.VERTICAL) # let the line expand horizontally without vertical expansion | |
146 | lineSizer.Add(wx.StaticLine(frame, -1, size = (10,-1)), 0, flag=wx.EXPAND) | |
147 | contentSizer.Add(lineSizer, flag=wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.BOTTOM, border=HALF_SPACE) | |
aca310e5 | 148 | |
02b800ce RD |
149 | if wx.Platform == "__WXMAC__": |
150 | contentSizer.Add((-1, 10), 0, wx.EXPAND) | |
aca310e5 | 151 | |
1f780e48 RD |
152 | lineSizer = wx.BoxSizer(wx.HORIZONTAL) |
153 | lineSizer.Add(wx.StaticText(frame, -1, _("Find what:")), 0, wx.ALIGN_CENTER | wx.RIGHT, HALF_SPACE) | |
bbf7159c RD |
154 | if not findString: |
155 | findString = config.Read(FindService.FIND_MATCHPATTERN, "") | |
156 | findCtrl = wx.TextCtrl(frame, -1, findString, size=(200,-1)) | |
157 | findCtrl.SetFocus() | |
158 | findCtrl.SetSelection(0,-1) | |
1f780e48 RD |
159 | lineSizer.Add(findCtrl, 0, wx.LEFT, HALF_SPACE) |
160 | contentSizer.Add(lineSizer, 0, wx.BOTTOM, SPACE) | |
161 | wholeWordCtrl = wx.CheckBox(frame, -1, _("Match whole word only")) | |
162 | wholeWordCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHWHOLEWORD, False)) | |
163 | matchCaseCtrl = wx.CheckBox(frame, -1, _("Match case")) | |
164 | matchCaseCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHCASE, False)) | |
165 | regExprCtrl = wx.CheckBox(frame, -1, _("Regular expression")) | |
166 | regExprCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHREGEXPR, False)) | |
167 | contentSizer.Add(wholeWordCtrl, 0, wx.BOTTOM, SPACE) | |
168 | contentSizer.Add(matchCaseCtrl, 0, wx.BOTTOM, SPACE) | |
169 | contentSizer.Add(regExprCtrl, 0, wx.BOTTOM, SPACE) | |
170 | borderSizer.Add(contentSizer, 0, wx.TOP | wx.BOTTOM | wx.LEFT, SPACE) | |
171 | ||
172 | buttonSizer = wx.BoxSizer(wx.VERTICAL) | |
173 | findBtn = wx.Button(frame, wx.ID_OK, _("Find")) | |
174 | findBtn.SetDefault() | |
02b800ce RD |
175 | BTM_SPACE = HALF_SPACE |
176 | if wx.Platform == "__WXMAC__": | |
177 | BTM_SPACE = SPACE | |
178 | buttonSizer.Add(findBtn, 0, wx.BOTTOM, BTM_SPACE) | |
b792147d | 179 | buttonSizer.Add(wx.Button(frame, wx.ID_CANCEL), 0) |
1f780e48 RD |
180 | borderSizer.Add(buttonSizer, 0, wx.ALL, SPACE) |
181 | ||
182 | frame.SetSizer(borderSizer) | |
183 | frame.Fit() | |
184 | ||
02b800ce | 185 | frame.CenterOnParent() |
1f780e48 RD |
186 | status = frame.ShowModal() |
187 | ||
188 | passedCheck = False | |
189 | while status == wx.ID_OK and not passedCheck: | |
190 | if not os.path.exists(dirCtrl.GetValue()): | |
191 | dlg = wx.MessageDialog(frame, | |
192 | _("'%s' does not exist.") % dirCtrl.GetValue(), | |
193 | _("Find in Directory"), | |
194 | wx.OK | wx.ICON_EXCLAMATION | |
195 | ) | |
02b800ce | 196 | dlg.CenterOnParent() |
1f780e48 RD |
197 | dlg.ShowModal() |
198 | dlg.Destroy() | |
199 | ||
200 | status = frame.ShowModal() | |
201 | elif len(findCtrl.GetValue()) == 0: | |
202 | dlg = wx.MessageDialog(frame, | |
203 | _("'Find what:' cannot be empty."), | |
204 | _("Find in Directory"), | |
205 | wx.OK | wx.ICON_EXCLAMATION | |
206 | ) | |
02b800ce | 207 | dlg.CenterOnParent() |
1f780e48 RD |
208 | dlg.ShowModal() |
209 | dlg.Destroy() | |
210 | ||
211 | status = frame.ShowModal() | |
212 | else: | |
213 | passedCheck = True | |
aca310e5 | 214 | |
1f780e48 RD |
215 | |
216 | # save user choice state for this and other Find Dialog Boxes | |
217 | dirString = dirCtrl.GetValue() | |
218 | searchSubfolders = subfolderCtrl.IsChecked() | |
aca310e5 RD |
219 | self.SaveFindInDirConfig(dirString, searchSubfolders) |
220 | ||
1f780e48 RD |
221 | findString = findCtrl.GetValue() |
222 | matchCase = matchCaseCtrl.IsChecked() | |
223 | wholeWord = wholeWordCtrl.IsChecked() | |
224 | regExpr = regExprCtrl.IsChecked() | |
225 | self.SaveFindConfig(findString, wholeWord, matchCase, regExpr) | |
226 | ||
02b800ce | 227 | frame.Destroy() |
aca310e5 | 228 | if status == wx.ID_OK: |
1f780e48 RD |
229 | messageService = wx.GetApp().GetService(MessageService.MessageService) |
230 | messageService.ShowWindow() | |
231 | ||
232 | view = messageService.GetView() | |
233 | if view: | |
234 | wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_WAIT)) | |
1f780e48 | 235 | |
aca310e5 RD |
236 | try: |
237 | view.ClearLines() | |
238 | view.SetCallback(self.OnJumpToFoundLine) | |
239 | ||
240 | view.AddLines(_("Searching for '%s' in '%s'\n\n") % (findString, dirString)) | |
241 | ||
242 | if os.path.isfile(dirString): | |
243 | try: | |
244 | docFile = file(dirString, 'r') | |
1f780e48 RD |
245 | lineNum = 1 |
246 | needToDisplayFilename = True | |
247 | line = docFile.readline() | |
248 | while line: | |
249 | count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) | |
250 | if count != -1: | |
251 | if needToDisplayFilename: | |
aca310e5 | 252 | view.AddLines(FILENAME_MARKER + dirString + "\n") |
1f780e48 RD |
253 | needToDisplayFilename = False |
254 | line = repr(lineNum).zfill(4) + ":" + line | |
255 | view.AddLines(line) | |
256 | line = docFile.readline() | |
257 | lineNum += 1 | |
258 | if not needToDisplayFilename: | |
259 | view.AddLines("\n") | |
aca310e5 RD |
260 | except IOError, (code, message): |
261 | print _("Warning, unable to read file: '%s'. %s") % (dirString, message) | |
262 | else: | |
263 | # do search in files on disk | |
264 | for root, dirs, files in os.walk(dirString): | |
265 | if not searchSubfolders and root != dirString: | |
266 | break | |
267 | ||
268 | for name in files: | |
269 | filename = os.path.join(root, name) | |
270 | try: | |
271 | docFile = file(filename, 'r') | |
272 | except IOError, (code, message): | |
273 | print _("Warning, unable to read file: '%s'. %s") % (filename, message) | |
274 | continue | |
275 | ||
276 | lineNum = 1 | |
277 | needToDisplayFilename = True | |
278 | line = docFile.readline() | |
279 | while line: | |
280 | count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) | |
281 | if count != -1: | |
282 | if needToDisplayFilename: | |
283 | view.AddLines(FILENAME_MARKER + filename + "\n") | |
284 | needToDisplayFilename = False | |
285 | line = repr(lineNum).zfill(4) + ":" + line | |
286 | view.AddLines(line) | |
287 | line = docFile.readline() | |
288 | lineNum += 1 | |
289 | if not needToDisplayFilename: | |
290 | view.AddLines("\n") | |
291 | ||
292 | view.AddLines(_("Search completed.")) | |
293 | ||
294 | finally: | |
295 | wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) | |
1f780e48 RD |
296 | |
297 | return True | |
298 | else: | |
1f780e48 | 299 | return False |
1f780e48 | 300 | |
aca310e5 RD |
301 | |
302 | def SaveFindInDirConfig(self, dirString, searchSubfolders): | |
1f780e48 | 303 | """ Save search dir patterns and flags to registry. |
aca310e5 | 304 | |
1f780e48 RD |
305 | dirString = search directory |
306 | searchSubfolders = Search subfolders | |
307 | """ | |
308 | config = wx.ConfigBase_Get() | |
309 | config.Write(FIND_MATCHDIR, dirString) | |
310 | config.WriteInt(FIND_MATCHDIRSUBFOLDERS, searchSubfolders) | |
aca310e5 RD |
311 | |
312 | ||
313 | def DoFindIn(self, findString, matchCase, wholeWord, regExpr, currFileOnly=False, jumpToFound=False): | |
314 | messageService = wx.GetApp().GetService(MessageService.MessageService) | |
315 | if not messageService: | |
316 | return | |
317 | ||
318 | messageService.ShowWindow() | |
319 | ||
320 | view = messageService.GetView() | |
321 | if not view: | |
322 | return | |
323 | ||
324 | wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_WAIT)) | |
325 | ||
326 | try: | |
327 | #Switch to messages tab. | |
328 | view.GetControl().GetParent().SetSelection(0) | |
329 | view.ClearLines() | |
330 | view.SetCallback(self.OnJumpToFoundLine) | |
331 | ||
332 | projectService = wx.GetApp().GetService(ProjectEditor.ProjectService) | |
333 | ||
334 | if wx.GetApp().GetDocumentManager().GetCurrentView(): | |
335 | currDoc = wx.GetApp().GetDocumentManager().GetCurrentView().GetDocument() | |
336 | else: | |
337 | currDoc = None | |
338 | if currFileOnly: | |
339 | if currDoc: | |
340 | projectFilenames = [currDoc.GetFilename()] | |
341 | view.AddLines(FILE_MARKER + currDoc.GetFilename() + "\n\n") | |
342 | else: | |
343 | projectFilenames = [] | |
344 | else: | |
345 | projectFilenames = projectService.GetFilesFromCurrentProject() | |
346 | ||
347 | projView = projectService.GetView() | |
348 | if projView: | |
349 | projName = wx.lib.docview.FileNameFromPath(projView.GetDocument().GetFilename()) | |
350 | view.AddLines(PROJECT_MARKER + projName + "\n\n") | |
351 | ||
352 | firstDef = -1 | |
353 | ||
354 | # do search in open files first, open files may have been modified and different from disk because it hasn't been saved | |
355 | openDocs = wx.GetApp().GetDocumentManager().GetDocuments() | |
356 | openDocsInProject = filter(lambda openDoc: openDoc.GetFilename() in projectFilenames, openDocs) | |
357 | if currDoc and currDoc in openDocsInProject: | |
358 | # make sure current document is searched first. | |
359 | openDocsInProject.remove(currDoc) | |
360 | openDocsInProject.insert(0, currDoc) | |
361 | for openDoc in openDocsInProject: | |
362 | if isinstance(openDoc, ProjectEditor.ProjectDocument): # don't search project model | |
363 | continue | |
364 | ||
365 | openDocView = openDoc.GetFirstView() | |
366 | # some views don't have a in memory text object to search through such as the PM and the DM | |
367 | # even if they do have a non-text searchable object, how do we display it in the message window? | |
368 | if not hasattr(openDocView, "GetValue"): | |
369 | continue | |
370 | text = openDocView.GetValue() | |
371 | ||
372 | lineNum = 1 | |
373 | needToDisplayFilename = True | |
374 | start = 0 | |
375 | end = 0 | |
376 | count = 0 | |
377 | while count != -1: | |
378 | count, foundStart, foundEnd, newText = self.DoFind(findString, None, text, start, end, True, matchCase, wholeWord, regExpr) | |
379 | if count != -1: | |
380 | if needToDisplayFilename: | |
381 | view.AddLines(FILENAME_MARKER + openDoc.GetFilename() + "\n") | |
382 | needToDisplayFilename = False | |
383 | ||
384 | lineNum = openDocView.LineFromPosition(foundStart) | |
385 | line = repr(lineNum).zfill(4) + ":" + openDocView.GetLine(lineNum) | |
386 | view.AddLines(line) | |
387 | if firstDef == -1: | |
388 | firstDef = view.GetControl().GetCurrentLine() - 1 | |
389 | ||
390 | start = text.find("\n", foundStart) | |
391 | if start == -1: | |
392 | break | |
393 | end = start | |
394 | if not needToDisplayFilename: | |
395 | view.AddLines("\n") | |
396 | wx.GetApp().Yield(True) | |
397 | openDocNames = map(lambda openDoc: openDoc.GetFilename(), openDocs) | |
398 | ||
399 | # do search in closed files, skipping the open ones we already searched | |
400 | filenames = filter(lambda filename: filename not in openDocNames, projectFilenames) | |
401 | for filename in filenames: | |
402 | try: | |
403 | docFile = file(filename, 'r') | |
404 | except IOError, (code, message): | |
405 | print _("Warning, unable to read file: '%s'. %s") % (filename, message) | |
406 | continue | |
407 | ||
408 | lineNum = 1 | |
409 | needToDisplayFilename = True | |
410 | line = docFile.readline() | |
411 | while line: | |
412 | count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) | |
413 | if count != -1: | |
414 | if needToDisplayFilename: | |
415 | view.AddLines(FILENAME_MARKER + filename + "\n") | |
416 | needToDisplayFilename = False | |
417 | line = repr(lineNum).zfill(4) + ":" + line | |
418 | view.AddLines(line) | |
419 | if firstDef == -1: | |
420 | firstDef = view.GetControl().GetCurrentLine() - 1 | |
421 | line = docFile.readline() | |
422 | lineNum += 1 | |
423 | if not needToDisplayFilename: | |
424 | view.AddLines("\n") | |
425 | wx.GetApp().Yield(True) | |
426 | ||
427 | view.AddLines(_("Search for '%s' completed.") % findString) | |
1f780e48 | 428 | |
aca310e5 RD |
429 | if jumpToFound: |
430 | self.OnJumpToFoundLine(event=None, defLineNum=firstDef) | |
1f780e48 | 431 | |
aca310e5 RD |
432 | finally: |
433 | wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) | |
434 | ||
435 | def FindInProject(self, findString): | |
436 | self.DoFindIn(findString, matchCase=True, wholeWord=True, regExpr=True, jumpToFound=True) | |
437 | ||
438 | ||
439 | def ShowFindInProjectDialog(self, findString=None): | |
1f780e48 RD |
440 | config = wx.ConfigBase_Get() |
441 | ||
02b800ce | 442 | frame = wx.Dialog(wx.GetApp().GetTopWindow(), -1, _("Find in Project"), size= (320,200)) |
1f780e48 RD |
443 | borderSizer = wx.BoxSizer(wx.HORIZONTAL) |
444 | ||
445 | contentSizer = wx.BoxSizer(wx.VERTICAL) | |
446 | lineSizer = wx.BoxSizer(wx.HORIZONTAL) | |
447 | lineSizer.Add(wx.StaticText(frame, -1, _("Find what:")), 0, wx.ALIGN_CENTER | wx.RIGHT, HALF_SPACE) | |
bbf7159c RD |
448 | if not findString: |
449 | findString = config.Read(FindService.FIND_MATCHPATTERN, "") | |
450 | findCtrl = wx.TextCtrl(frame, -1, findString, size=(200,-1)) | |
1f780e48 RD |
451 | lineSizer.Add(findCtrl, 0, wx.LEFT, HALF_SPACE) |
452 | contentSizer.Add(lineSizer, 0, wx.BOTTOM, SPACE) | |
453 | wholeWordCtrl = wx.CheckBox(frame, -1, _("Match whole word only")) | |
454 | wholeWordCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHWHOLEWORD, False)) | |
455 | matchCaseCtrl = wx.CheckBox(frame, -1, _("Match case")) | |
456 | matchCaseCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHCASE, False)) | |
457 | regExprCtrl = wx.CheckBox(frame, -1, _("Regular expression")) | |
458 | regExprCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHREGEXPR, False)) | |
459 | contentSizer.Add(wholeWordCtrl, 0, wx.BOTTOM, SPACE) | |
460 | contentSizer.Add(matchCaseCtrl, 0, wx.BOTTOM, SPACE) | |
461 | contentSizer.Add(regExprCtrl, 0, wx.BOTTOM, SPACE) | |
462 | borderSizer.Add(contentSizer, 0, wx.TOP | wx.BOTTOM | wx.LEFT, SPACE) | |
463 | ||
464 | buttonSizer = wx.BoxSizer(wx.VERTICAL) | |
465 | findBtn = wx.Button(frame, wx.ID_OK, _("Find")) | |
466 | findBtn.SetDefault() | |
02b800ce RD |
467 | BTM_SPACE = HALF_SPACE |
468 | if wx.Platform == "__WXMAC__": | |
469 | BTM_SPACE = SPACE | |
470 | buttonSizer.Add(findBtn, 0, wx.BOTTOM, BTM_SPACE) | |
b792147d | 471 | buttonSizer.Add(wx.Button(frame, wx.ID_CANCEL), 0) |
1f780e48 RD |
472 | borderSizer.Add(buttonSizer, 0, wx.ALL, SPACE) |
473 | ||
474 | frame.SetSizer(borderSizer) | |
475 | frame.Fit() | |
476 | ||
02b800ce | 477 | frame.CenterOnParent() |
1f780e48 RD |
478 | status = frame.ShowModal() |
479 | ||
480 | # save user choice state for this and other Find Dialog Boxes | |
481 | findString = findCtrl.GetValue() | |
482 | matchCase = matchCaseCtrl.IsChecked() | |
483 | wholeWord = wholeWordCtrl.IsChecked() | |
484 | regExpr = regExprCtrl.IsChecked() | |
485 | self.SaveFindConfig(findString, wholeWord, matchCase, regExpr) | |
486 | ||
02b800ce | 487 | frame.Destroy() |
aca310e5 | 488 | |
1f780e48 | 489 | if status == wx.ID_OK: |
aca310e5 RD |
490 | self.DoFindIn(findString, matchCase, wholeWord, regExpr) |
491 | return True | |
492 | else: | |
493 | return False | |
1f780e48 | 494 | |
1f780e48 | 495 | |
aca310e5 RD |
496 | def ShowFindInFileDialog(self, findString=None): |
497 | config = wx.ConfigBase_Get() | |
1f780e48 | 498 | |
aca310e5 RD |
499 | frame = wx.Dialog(wx.GetApp().GetTopWindow(), -1, _("Find in File"), size= (320,200)) |
500 | borderSizer = wx.BoxSizer(wx.HORIZONTAL) | |
1f780e48 | 501 | |
aca310e5 RD |
502 | contentSizer = wx.BoxSizer(wx.VERTICAL) |
503 | lineSizer = wx.BoxSizer(wx.HORIZONTAL) | |
504 | lineSizer.Add(wx.StaticText(frame, -1, _("Find what:")), 0, wx.ALIGN_CENTER | wx.RIGHT, HALF_SPACE) | |
505 | if not findString: | |
506 | findString = config.Read(FindService.FIND_MATCHPATTERN, "") | |
507 | findCtrl = wx.TextCtrl(frame, -1, findString, size=(200,-1)) | |
508 | lineSizer.Add(findCtrl, 0, wx.LEFT, HALF_SPACE) | |
509 | contentSizer.Add(lineSizer, 0, wx.BOTTOM, SPACE) | |
510 | wholeWordCtrl = wx.CheckBox(frame, -1, _("Match whole word only")) | |
511 | wholeWordCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHWHOLEWORD, False)) | |
512 | matchCaseCtrl = wx.CheckBox(frame, -1, _("Match case")) | |
513 | matchCaseCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHCASE, False)) | |
514 | regExprCtrl = wx.CheckBox(frame, -1, _("Regular expression")) | |
515 | regExprCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHREGEXPR, False)) | |
516 | contentSizer.Add(wholeWordCtrl, 0, wx.BOTTOM, SPACE) | |
517 | contentSizer.Add(matchCaseCtrl, 0, wx.BOTTOM, SPACE) | |
518 | contentSizer.Add(regExprCtrl, 0, wx.BOTTOM, SPACE) | |
519 | borderSizer.Add(contentSizer, 0, wx.TOP | wx.BOTTOM | wx.LEFT, SPACE) | |
520 | ||
521 | buttonSizer = wx.BoxSizer(wx.VERTICAL) | |
522 | findBtn = wx.Button(frame, wx.ID_OK, _("Find")) | |
523 | findBtn.SetDefault() | |
524 | BTM_SPACE = HALF_SPACE | |
525 | if wx.Platform == "__WXMAC__": | |
526 | BTM_SPACE = SPACE | |
527 | buttonSizer.Add(findBtn, 0, wx.BOTTOM, BTM_SPACE) | |
528 | buttonSizer.Add(wx.Button(frame, wx.ID_CANCEL), 0) | |
529 | borderSizer.Add(buttonSizer, 0, wx.ALL, SPACE) | |
530 | ||
531 | frame.SetSizer(borderSizer) | |
532 | frame.Fit() | |
533 | ||
534 | frame.CenterOnParent() | |
535 | status = frame.ShowModal() | |
536 | ||
537 | # save user choice state for this and other Find Dialog Boxes | |
538 | findString = findCtrl.GetValue() | |
539 | matchCase = matchCaseCtrl.IsChecked() | |
540 | wholeWord = wholeWordCtrl.IsChecked() | |
541 | regExpr = regExprCtrl.IsChecked() | |
542 | self.SaveFindConfig(findString, wholeWord, matchCase, regExpr) | |
1f780e48 | 543 | |
aca310e5 RD |
544 | frame.Destroy() |
545 | ||
546 | if status == wx.ID_OK: | |
547 | self.DoFindIn(findString, matchCase, wholeWord, regExpr, currFileOnly=True) | |
1f780e48 RD |
548 | return True |
549 | else: | |
1f780e48 RD |
550 | return False |
551 | ||
552 | ||
aca310e5 | 553 | def OnJumpToFoundLine(self, event=None, defLineNum=-1): |
1f780e48 | 554 | messageService = wx.GetApp().GetService(MessageService.MessageService) |
aca310e5 RD |
555 | if defLineNum == -1: |
556 | lineText, pos = messageService.GetView().GetCurrLine() | |
557 | else: | |
558 | lineText = messageService.GetView().GetControl().GetLine(defLineNum) | |
559 | pos = 0 | |
560 | ||
561 | if lineText == "\n" or lineText.find(FILENAME_MARKER) != -1 or lineText.find(PROJECT_MARKER) != -1 or lineText.find(FILE_MARKER) != -1: | |
1f780e48 RD |
562 | return |
563 | lineEnd = lineText.find(":") | |
564 | if lineEnd == -1: | |
565 | return | |
566 | else: | |
567 | lineNum = int(lineText[0:lineEnd]) | |
568 | ||
569 | text = messageService.GetView().GetText() | |
aca310e5 RD |
570 | if defLineNum == -1: |
571 | curPos = messageService.GetView().GetCurrentPos() | |
572 | else: | |
573 | curPos = messageService.GetView().GetControl().GetLineEndPosition(defLineNum) | |
1f780e48 RD |
574 | |
575 | startPos = text.rfind(FILENAME_MARKER, 0, curPos) | |
576 | endPos = text.find("\n", startPos) | |
577 | filename = text[startPos + len(FILENAME_MARKER):endPos] | |
578 | ||
579 | foundView = None | |
580 | openDocs = wx.GetApp().GetDocumentManager().GetDocuments() | |
581 | for openDoc in openDocs: | |
582 | if openDoc.GetFilename() == filename: | |
583 | foundView = openDoc.GetFirstView() | |
584 | break | |
585 | ||
586 | if not foundView: | |
02b800ce RD |
587 | doc = wx.GetApp().GetDocumentManager().CreateDocument(filename, wx.lib.docview.DOC_SILENT|wx.lib.docview.DOC_OPEN_ONCE) |
588 | if doc: | |
589 | foundView = doc.GetFirstView() | |
1f780e48 RD |
590 | |
591 | if foundView: | |
592 | foundView.GetFrame().SetFocus() | |
593 | foundView.Activate() | |
594 | if hasattr(foundView, "GotoLine"): | |
595 | foundView.GotoLine(lineNum) | |
596 | startPos = foundView.PositionFromLine(lineNum) | |
597 | # wxBug: Need to select in reverse order, (end, start) to put cursor at head of line so positioning is correct | |
598 | # Also, if we use the correct positioning order (start, end), somehow, when we open a edit window for the first | |
599 | # time, we don't see the selection, it is scrolled off screen | |
600 | foundView.SetSelection(startPos - 1 + len(lineText[lineEnd:].rstrip("\n")), startPos) | |
601 | wx.GetApp().GetService(OutlineService.OutlineService).LoadOutline(foundView, position=startPos) |