X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/02b800ce7c2bb1c1f08075f22be2e7412124b47f..908f8fc481c0503bf8be5002e5d5208666da7649:/wxPython/samples/ide/activegrid/tool/FindInDirService.py diff --git a/wxPython/samples/ide/activegrid/tool/FindInDirService.py b/wxPython/samples/ide/activegrid/tool/FindInDirService.py index 1fd557b002..da4ed91e19 100644 --- a/wxPython/samples/ide/activegrid/tool/FindInDirService.py +++ b/wxPython/samples/ide/activegrid/tool/FindInDirService.py @@ -27,6 +27,7 @@ _ = wx.GetTranslation #---------------------------------------------------------------------------- FILENAME_MARKER = _("Found in file: ") PROJECT_MARKER = _("Searching project: ") +FILE_MARKER = _("Searching file: ") FIND_MATCHDIR = "FindMatchDir" FIND_MATCHDIRSUBFOLDERS = "FindMatchDirSubfolders" @@ -39,6 +40,7 @@ class FindInDirService(FindService.FindService): #---------------------------------------------------------------------------- # Constants #---------------------------------------------------------------------------- + FINDFILE_ID = wx.NewId() # for bringing up Find in File dialog box FINDALL_ID = wx.NewId() # for bringing up Find All dialog box FINDDIR_ID = wx.NewId() # for bringing up Find Dir dialog box @@ -47,29 +49,39 @@ class FindInDirService(FindService.FindService): FindService.FindService.InstallControls(self, frame, menuBar, toolBar, statusBar, document) editMenu = menuBar.GetMenu(menuBar.FindMenu(_("&Edit"))) + wx.EVT_MENU(frame, FindInDirService.FINDFILE_ID, self.ProcessEvent) + wx.EVT_UPDATE_UI(frame, FindInDirService.FINDFILE_ID, self.ProcessUpdateUIEvent) + editMenu.Append(FindInDirService.FINDFILE_ID, _("Find in File...\tCtrl+Shift+F"), _("Searches for the specified text in the current file")) wx.EVT_MENU(frame, FindInDirService.FINDALL_ID, self.ProcessEvent) wx.EVT_UPDATE_UI(frame, FindInDirService.FINDALL_ID, self.ProcessUpdateUIEvent) - editMenu.Append(FindInDirService.FINDALL_ID, _("Find in Project...\tCtrl+Shift+F"), _("Searches for the specified text in all the files in the project")) + editMenu.Append(FindInDirService.FINDALL_ID, _("Find in Project...\tCtrl+Shift+P"), _("Searches for the specified text in all the files in the project")) wx.EVT_MENU(frame, FindInDirService.FINDDIR_ID, self.ProcessEvent) wx.EVT_UPDATE_UI(frame, FindInDirService.FINDDIR_ID, self.ProcessUpdateUIEvent) - editMenu.Append(FindInDirService.FINDDIR_ID, _("Find in Directory..."), _("Searches for the specified text in all the files in the directory")) + editMenu.Append(FindInDirService.FINDDIR_ID, _("Find in Directory...\tCtrl+Shift+D"), _("Searches for the specified text in all the files in the directory")) def ProcessEvent(self, event): id = event.GetId() - if id == FindInDirService.FINDALL_ID: + if id == FindInDirService.FINDFILE_ID: view = wx.GetApp().GetDocumentManager().GetCurrentView() if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): - self.ShowFindAllDialog(view.GetCtrl().GetSelectedText()) + self.ShowFindInFileDialog(view.GetCtrl().GetSelectedText()) else: - self.ShowFindAllDialog() + self.ShowFindInFileDialog() + return True + elif id == FindInDirService.FINDALL_ID: + view = wx.GetApp().GetDocumentManager().GetCurrentView() + if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): + self.ShowFindInProjectDialog(view.GetCtrl().GetSelectedText()) + else: + self.ShowFindInProjectDialog() return True elif id == FindInDirService.FINDDIR_ID: view = wx.GetApp().GetDocumentManager().GetCurrentView() if hasattr(view, "GetCtrl") and view.GetCtrl() and hasattr(view.GetCtrl(), "GetSelectedText"): - self.ShowFindDirDialog(view.GetCtrl().GetSelectedText()) + self.ShowFindInDirDialog(view.GetCtrl().GetSelectedText()) else: - self.ShowFindDirDialog() + self.ShowFindInDirDialog() return True else: return FindService.FindService.ProcessEvent(self, event) @@ -77,7 +89,14 @@ class FindInDirService(FindService.FindService): def ProcessUpdateUIEvent(self, event): id = event.GetId() - if id == FindInDirService.FINDALL_ID: + if id == FindInDirService.FINDFILE_ID: + view = wx.GetApp().GetDocumentManager().GetCurrentView() + if view and view.GetDocument() and not isinstance(view.GetDocument(), ProjectEditor.ProjectDocument): # don't search project model + event.Enable(True) + else: + event.Enable(False) + return True + elif id == FindInDirService.FINDALL_ID: projectService = wx.GetApp().GetService(ProjectEditor.ProjectService) if projectService.GetFilesFromCurrentProject(): event.Enable(True) @@ -90,7 +109,7 @@ class FindInDirService(FindService.FindService): return FindService.FindService.ProcessUpdateUIEvent(self, event) - def ShowFindDirDialog(self, findString=None): + def ShowFindInDirDialog(self, findString=None): config = wx.ConfigBase_Get() frame = wx.Dialog(wx.GetApp().GetTopWindow(), -1, _("Find in Directory"), size= (320,200)) @@ -105,7 +124,7 @@ class FindInDirService(FindService.FindService): findDirButton = wx.Button(frame, -1, _("Browse...")) lineSizer.Add(findDirButton, 0, wx.LEFT, HALF_SPACE) contentSizer.Add(lineSizer, 0, wx.BOTTOM, SPACE) - + def OnBrowseButton(event): dlg = wx.DirDialog(frame, _("Choose a directory:"), style=wx.DD_DEFAULT_STYLE) dir = dirCtrl.GetValue() @@ -126,10 +145,10 @@ class FindInDirService(FindService.FindService): lineSizer = wx.BoxSizer(wx.VERTICAL) # let the line expand horizontally without vertical expansion lineSizer.Add(wx.StaticLine(frame, -1, size = (10,-1)), 0, flag=wx.EXPAND) contentSizer.Add(lineSizer, flag=wx.EXPAND|wx.ALIGN_CENTER_VERTICAL|wx.BOTTOM, border=HALF_SPACE) - + if wx.Platform == "__WXMAC__": contentSizer.Add((-1, 10), 0, wx.EXPAND) - + lineSizer = wx.BoxSizer(wx.HORIZONTAL) lineSizer.Add(wx.StaticText(frame, -1, _("Find what:")), 0, wx.ALIGN_CENTER | wx.RIGHT, HALF_SPACE) if not findString: @@ -192,13 +211,13 @@ class FindInDirService(FindService.FindService): status = frame.ShowModal() else: passedCheck = True - + # save user choice state for this and other Find Dialog Boxes dirString = dirCtrl.GetValue() searchSubfolders = subfolderCtrl.IsChecked() - self.SaveFindDirConfig(dirString, searchSubfolders) - + self.SaveFindInDirConfig(dirString, searchSubfolders) + findString = findCtrl.GetValue() matchCase = matchCaseCtrl.IsChecked() wholeWord = wholeWordCtrl.IsChecked() @@ -206,52 +225,23 @@ class FindInDirService(FindService.FindService): self.SaveFindConfig(findString, wholeWord, matchCase, regExpr) frame.Destroy() - if status == wx.ID_OK: + if status == wx.ID_OK: messageService = wx.GetApp().GetService(MessageService.MessageService) messageService.ShowWindow() view = messageService.GetView() if view: wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_WAIT)) - view.ClearLines() - view.SetCallback(self.OnJumpToFoundLine) - - view.AddLines(_("Searching for '%s' in '%s'\n\n") % (findString, dirString)) - if os.path.isfile(dirString): - try: - docFile = file(dirString, 'r') - lineNum = 1 - needToDisplayFilename = True - line = docFile.readline() - while line: - count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) - if count != -1: - if needToDisplayFilename: - view.AddLines(FILENAME_MARKER + dirString + "\n") - needToDisplayFilename = False - line = repr(lineNum).zfill(4) + ":" + line - view.AddLines(line) - line = docFile.readline() - lineNum += 1 - if not needToDisplayFilename: - view.AddLines("\n") - except IOError, (code, message): - print _("Warning, unable to read file: '%s'. %s") % (dirString, message) - else: - # do search in files on disk - for root, dirs, files in os.walk(dirString): - if not searchSubfolders and root != dirString: - break - - for name in files: - filename = os.path.join(root, name) - try: - docFile = file(filename, 'r') - except IOError, (code, message): - print _("Warning, unable to read file: '%s'. %s") % (filename, message) - continue - + try: + view.ClearLines() + view.SetCallback(self.OnJumpToFoundLine) + + view.AddLines(_("Searching for '%s' in '%s'\n\n") % (findString, dirString)) + + if os.path.isfile(dirString): + try: + docFile = file(dirString, 'r') lineNum = 1 needToDisplayFilename = True line = docFile.readline() @@ -259,7 +249,7 @@ class FindInDirService(FindService.FindService): count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) if count != -1: if needToDisplayFilename: - view.AddLines(FILENAME_MARKER + filename + "\n") + view.AddLines(FILENAME_MARKER + dirString + "\n") needToDisplayFilename = False line = repr(lineNum).zfill(4) + ":" + line view.AddLines(line) @@ -267,27 +257,186 @@ class FindInDirService(FindService.FindService): lineNum += 1 if not needToDisplayFilename: view.AddLines("\n") - - view.AddLines(_("Search completed.")) - wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) + except IOError, (code, message): + print _("Warning, unable to read file: '%s'. %s") % (dirString, message) + else: + # do search in files on disk + for root, dirs, files in os.walk(dirString): + if not searchSubfolders and root != dirString: + break + + for name in files: + filename = os.path.join(root, name) + try: + docFile = file(filename, 'r') + except IOError, (code, message): + print _("Warning, unable to read file: '%s'. %s") % (filename, message) + continue + + lineNum = 1 + needToDisplayFilename = True + line = docFile.readline() + while line: + count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) + if count != -1: + if needToDisplayFilename: + view.AddLines(FILENAME_MARKER + filename + "\n") + needToDisplayFilename = False + line = repr(lineNum).zfill(4) + ":" + line + view.AddLines(line) + line = docFile.readline() + lineNum += 1 + if not needToDisplayFilename: + view.AddLines("\n") + + view.AddLines(_("Search completed.")) + + finally: + wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) return True else: return False - - def SaveFindDirConfig(self, dirString, searchSubfolders): + + def SaveFindInDirConfig(self, dirString, searchSubfolders): """ Save search dir patterns and flags to registry. - + dirString = search directory searchSubfolders = Search subfolders """ config = wx.ConfigBase_Get() config.Write(FIND_MATCHDIR, dirString) config.WriteInt(FIND_MATCHDIRSUBFOLDERS, searchSubfolders) + + + def DoFindIn(self, findString, matchCase, wholeWord, regExpr, currFileOnly=False, jumpToFound=False): + messageService = wx.GetApp().GetService(MessageService.MessageService) + if not messageService: + return + + messageService.ShowWindow() + + view = messageService.GetView() + if not view: + return + + wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_WAIT)) + + try: + #Switch to messages tab. + view.GetControl().GetParent().SetSelection(0) + view.ClearLines() + view.SetCallback(self.OnJumpToFoundLine) + + projectService = wx.GetApp().GetService(ProjectEditor.ProjectService) + + if wx.GetApp().GetDocumentManager().GetCurrentView(): + currDoc = wx.GetApp().GetDocumentManager().GetCurrentView().GetDocument() + else: + currDoc = None + if currFileOnly: + if currDoc: + projectFilenames = [currDoc.GetFilename()] + view.AddLines(FILE_MARKER + currDoc.GetFilename() + "\n\n") + else: + projectFilenames = [] + else: + projectFilenames = projectService.GetFilesFromCurrentProject() + + projView = projectService.GetView() + if projView: + projName = wx.lib.docview.FileNameFromPath(projView.GetDocument().GetFilename()) + view.AddLines(PROJECT_MARKER + projName + "\n\n") + + firstDef = -1 + + # do search in open files first, open files may have been modified and different from disk because it hasn't been saved + openDocs = wx.GetApp().GetDocumentManager().GetDocuments() + openDocsInProject = filter(lambda openDoc: openDoc.GetFilename() in projectFilenames, openDocs) + if currDoc and currDoc in openDocsInProject: + # make sure current document is searched first. + openDocsInProject.remove(currDoc) + openDocsInProject.insert(0, currDoc) + for openDoc in openDocsInProject: + if isinstance(openDoc, ProjectEditor.ProjectDocument): # don't search project model + continue + + openDocView = openDoc.GetFirstView() + # some views don't have a in memory text object to search through such as the PM and the DM + # even if they do have a non-text searchable object, how do we display it in the message window? + if not hasattr(openDocView, "GetValue"): + continue + text = openDocView.GetValue() + + lineNum = 1 + needToDisplayFilename = True + start = 0 + end = 0 + count = 0 + while count != -1: + count, foundStart, foundEnd, newText = self.DoFind(findString, None, text, start, end, True, matchCase, wholeWord, regExpr) + if count != -1: + if needToDisplayFilename: + view.AddLines(FILENAME_MARKER + openDoc.GetFilename() + "\n") + needToDisplayFilename = False + + lineNum = openDocView.LineFromPosition(foundStart) + line = repr(lineNum).zfill(4) + ":" + openDocView.GetLine(lineNum) + view.AddLines(line) + if firstDef == -1: + firstDef = view.GetControl().GetCurrentLine() - 1 + + start = text.find("\n", foundStart) + if start == -1: + break + end = start + if not needToDisplayFilename: + view.AddLines("\n") + wx.GetApp().Yield(True) + openDocNames = map(lambda openDoc: openDoc.GetFilename(), openDocs) + + # do search in closed files, skipping the open ones we already searched + filenames = filter(lambda filename: filename not in openDocNames, projectFilenames) + for filename in filenames: + try: + docFile = file(filename, 'r') + except IOError, (code, message): + print _("Warning, unable to read file: '%s'. %s") % (filename, message) + continue + + lineNum = 1 + needToDisplayFilename = True + line = docFile.readline() + while line: + count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) + if count != -1: + if needToDisplayFilename: + view.AddLines(FILENAME_MARKER + filename + "\n") + needToDisplayFilename = False + line = repr(lineNum).zfill(4) + ":" + line + view.AddLines(line) + if firstDef == -1: + firstDef = view.GetControl().GetCurrentLine() - 1 + line = docFile.readline() + lineNum += 1 + if not needToDisplayFilename: + view.AddLines("\n") + wx.GetApp().Yield(True) + + view.AddLines(_("Search for '%s' completed.") % findString) + if jumpToFound: + self.OnJumpToFoundLine(event=None, defLineNum=firstDef) - def ShowFindAllDialog(self, findString=None): + finally: + wx.GetApp().GetTopWindow().SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT)) + + def FindInProject(self, findString): + self.DoFindIn(findString, matchCase=True, wholeWord=True, regExpr=True, jumpToFound=True) + + + def ShowFindInProjectDialog(self, findString=None): config = wx.ConfigBase_Get() frame = wx.Dialog(wx.GetApp().GetTopWindow(), -1, _("Find in Project"), size= (320,200)) @@ -336,97 +485,80 @@ class FindInDirService(FindService.FindService): self.SaveFindConfig(findString, wholeWord, matchCase, regExpr) frame.Destroy() + if status == wx.ID_OK: - messageService = wx.GetApp().GetService(MessageService.MessageService) - messageService.ShowWindow() + self.DoFindIn(findString, matchCase, wholeWord, regExpr) + return True + else: + return False - view = messageService.GetView() - if view: - view.ClearLines() - view.SetCallback(self.OnJumpToFoundLine) - projectService = wx.GetApp().GetService(ProjectEditor.ProjectService) - projectFilenames = projectService.GetFilesFromCurrentProject() + def ShowFindInFileDialog(self, findString=None): + config = wx.ConfigBase_Get() - projView = projectService.GetView() - if projView: - projName = wx.lib.docview.FileNameFromPath(projView.GetDocument().GetFilename()) - view.AddLines(PROJECT_MARKER + projName + "\n\n") + frame = wx.Dialog(wx.GetApp().GetTopWindow(), -1, _("Find in File"), size= (320,200)) + borderSizer = wx.BoxSizer(wx.HORIZONTAL) - # do search in open files first, open files may have been modified and different from disk because it hasn't been saved - openDocs = wx.GetApp().GetDocumentManager().GetDocuments() - openDocsInProject = filter(lambda openDoc: openDoc.GetFilename() in projectFilenames, openDocs) - for openDoc in openDocsInProject: - if isinstance(openDoc, ProjectEditor.ProjectDocument): # don't search project model - continue - - openDocView = openDoc.GetFirstView() - # some views don't have a in memory text object to search through such as the PM and the DM - # even if they do have a non-text searchable object, how do we display it in the message window? - if not hasattr(openDocView, "GetValue"): - continue - text = openDocView.GetValue() - - lineNum = 1 - needToDisplayFilename = True - start = 0 - end = 0 - count = 0 - while count != -1: - count, foundStart, foundEnd, newText = self.DoFind(findString, None, text, start, end, True, matchCase, wholeWord, regExpr) - if count != -1: - if needToDisplayFilename: - view.AddLines(FILENAME_MARKER + openDoc.GetFilename() + "\n") - needToDisplayFilename = False - - lineNum = openDocView.LineFromPosition(foundStart) - line = repr(lineNum).zfill(4) + ":" + openDocView.GetLine(lineNum) - view.AddLines(line) - - start = text.find("\n", foundStart) - if start == -1: - break - end = start - if not needToDisplayFilename: - view.AddLines("\n") - openDocNames = map(lambda openDoc: openDoc.GetFilename(), openDocs) - - # do search in closed files, skipping the open ones we already searched - filenames = filter(lambda filename: filename not in openDocNames, projectFilenames) - for filename in filenames: - try: - docFile = file(filename, 'r') - except IOError, (code, message): - print _("Warning, unable to read file: '%s'. %s") % (filename, message) - continue - - lineNum = 1 - needToDisplayFilename = True - line = docFile.readline() - while line: - count, foundStart, foundEnd, newText = self.DoFind(findString, None, line, 0, 0, True, matchCase, wholeWord, regExpr) - if count != -1: - if needToDisplayFilename: - view.AddLines(FILENAME_MARKER + filename + "\n") - needToDisplayFilename = False - line = repr(lineNum).zfill(4) + ":" + line - view.AddLines(line) - line = docFile.readline() - lineNum += 1 - if not needToDisplayFilename: - view.AddLines("\n") - - view.AddLines(_("Search for '%s' completed.") % findString) + contentSizer = wx.BoxSizer(wx.VERTICAL) + lineSizer = wx.BoxSizer(wx.HORIZONTAL) + lineSizer.Add(wx.StaticText(frame, -1, _("Find what:")), 0, wx.ALIGN_CENTER | wx.RIGHT, HALF_SPACE) + if not findString: + findString = config.Read(FindService.FIND_MATCHPATTERN, "") + findCtrl = wx.TextCtrl(frame, -1, findString, size=(200,-1)) + lineSizer.Add(findCtrl, 0, wx.LEFT, HALF_SPACE) + contentSizer.Add(lineSizer, 0, wx.BOTTOM, SPACE) + wholeWordCtrl = wx.CheckBox(frame, -1, _("Match whole word only")) + wholeWordCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHWHOLEWORD, False)) + matchCaseCtrl = wx.CheckBox(frame, -1, _("Match case")) + matchCaseCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHCASE, False)) + regExprCtrl = wx.CheckBox(frame, -1, _("Regular expression")) + regExprCtrl.SetValue(config.ReadInt(FindService.FIND_MATCHREGEXPR, False)) + contentSizer.Add(wholeWordCtrl, 0, wx.BOTTOM, SPACE) + contentSizer.Add(matchCaseCtrl, 0, wx.BOTTOM, SPACE) + contentSizer.Add(regExprCtrl, 0, wx.BOTTOM, SPACE) + borderSizer.Add(contentSizer, 0, wx.TOP | wx.BOTTOM | wx.LEFT, SPACE) + + buttonSizer = wx.BoxSizer(wx.VERTICAL) + findBtn = wx.Button(frame, wx.ID_OK, _("Find")) + findBtn.SetDefault() + BTM_SPACE = HALF_SPACE + if wx.Platform == "__WXMAC__": + BTM_SPACE = SPACE + buttonSizer.Add(findBtn, 0, wx.BOTTOM, BTM_SPACE) + buttonSizer.Add(wx.Button(frame, wx.ID_CANCEL), 0) + borderSizer.Add(buttonSizer, 0, wx.ALL, SPACE) + + frame.SetSizer(borderSizer) + frame.Fit() + + frame.CenterOnParent() + status = frame.ShowModal() + + # save user choice state for this and other Find Dialog Boxes + findString = findCtrl.GetValue() + matchCase = matchCaseCtrl.IsChecked() + wholeWord = wholeWordCtrl.IsChecked() + regExpr = regExprCtrl.IsChecked() + self.SaveFindConfig(findString, wholeWord, matchCase, regExpr) + frame.Destroy() + + if status == wx.ID_OK: + self.DoFindIn(findString, matchCase, wholeWord, regExpr, currFileOnly=True) return True else: return False - def OnJumpToFoundLine(self, event): + def OnJumpToFoundLine(self, event=None, defLineNum=-1): messageService = wx.GetApp().GetService(MessageService.MessageService) - lineText, pos = messageService.GetView().GetCurrLine() - if lineText == "\n" or lineText.find(FILENAME_MARKER) != -1 or lineText.find(PROJECT_MARKER) != -1: + if defLineNum == -1: + lineText, pos = messageService.GetView().GetCurrLine() + else: + lineText = messageService.GetView().GetControl().GetLine(defLineNum) + pos = 0 + + if lineText == "\n" or lineText.find(FILENAME_MARKER) != -1 or lineText.find(PROJECT_MARKER) != -1 or lineText.find(FILE_MARKER) != -1: return lineEnd = lineText.find(":") if lineEnd == -1: @@ -435,7 +567,10 @@ class FindInDirService(FindService.FindService): lineNum = int(lineText[0:lineEnd]) text = messageService.GetView().GetText() - curPos = messageService.GetView().GetCurrentPos() + if defLineNum == -1: + curPos = messageService.GetView().GetCurrentPos() + else: + curPos = messageService.GetView().GetControl().GetLineEndPosition(defLineNum) startPos = text.rfind(FILENAME_MARKER, 0, curPos) endPos = text.find("\n", startPos) @@ -464,5 +599,3 @@ class FindInDirService(FindService.FindService): # time, we don't see the selection, it is scrolled off screen foundView.SetSelection(startPos - 1 + len(lineText[lineEnd:].rstrip("\n")), startPos) wx.GetApp().GetService(OutlineService.OutlineService).LoadOutline(foundView, position=startPos) - -