⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stctexteditor.py

📁 wxPython的基本示例程序
💻 PY
📖 第 1 页 / 共 5 页
字号:
        or id == wx.ID_COPY        or id == wx.ID_CLEAR):            hasSelection = self.GetCtrl().GetSelectionStart() != self.GetCtrl().GetSelectionEnd()            event.Enable(hasSelection)            return True        elif id == wx.ID_PASTE:            event.Enable(self.GetCtrl().CanPaste())            return True        elif id == wx.ID_SELECTALL:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            return True        elif id == TEXT_ID:            event.Enable(True)            return True        elif id == VIEW_WHITESPACE_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            event.Check(self.GetCtrl().GetViewWhiteSpace())            return True        elif id == VIEW_EOL_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            event.Check(self.GetCtrl().GetViewEOL())            return True        elif id == VIEW_INDENTATION_GUIDES_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            event.Check(self.GetCtrl().GetIndentationGuides())            return True        elif id == VIEW_RIGHT_EDGE_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            event.Check(self.GetCtrl().GetViewRightEdge())            return True        elif id == VIEW_LINE_NUMBERS_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            event.Check(self.GetCtrl().GetViewLineNumbers())            return True        elif id == ZOOM_ID:            event.Enable(True)            return True        elif id == ZOOM_NORMAL_ID:            event.Enable(self.GetCtrl().GetZoom() != 0)            return True        elif id == ZOOM_IN_ID:            event.Enable(self.GetCtrl().GetZoom() < 20)            return True        elif id == ZOOM_OUT_ID:            event.Enable(self.GetCtrl().GetZoom() > -10)            return True        elif id == CHOOSE_FONT_ID:            event.Enable(True)            return True        elif id == WORD_WRAP_ID:            event.Enable(self.GetCtrl().CanWordWrap())            event.Check(self.GetCtrl().CanWordWrap() and self.GetCtrl().GetWordWrap())            return True        elif id == FindService.FindService.FIND_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            return True        elif id == FindService.FindService.FIND_PREVIOUS_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText and                         self._FindServiceHasString() and                         self.GetCtrl().GetSelection()[0] > 0)            return True        elif id == FindService.FindService.FIND_NEXT_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText and                         self._FindServiceHasString() and                         self.GetCtrl().GetSelection()[0] < self.GetCtrl().GetLength())            return True        elif id == FindService.FindService.REPLACE_ID:            hasText = self.GetCtrl().GetTextLength() > 0            event.Enable(hasText)            return True        elif id == FindService.FindService.GOTO_LINE_ID:            event.Enable(True)            return True        elif id == TEXT_STATUS_BAR_ID:            self.OnUpdateStatusBar(event)            return True        else:            return wx.lib.docview.View.ProcessUpdateUIEvent(self, event)    def _GetParentFrame(self):        return wx.GetTopLevelParent(self.GetFrame())    def _GetActiveCtrl(self, parent):	         """ Walk through the MultiSash windows and find the active Control """	         if isinstance(parent, wx.lib.multisash.MultiClient) and parent.selected:	             return parent.child	         if hasattr(parent, "GetChildren"):	             for child in parent.GetChildren():	                 found = self._GetActiveCtrl(child)	                 if found:	                     return found	         return None	  	     def _FindCtrl(self, parent):	         """ Walk through the MultiSash windows and find the first TextCtrl """	         if isinstance(parent, self.GetCtrlClass()):	             return parent	         if hasattr(parent, "GetChildren"):	             for child in parent.GetChildren():	                 found = self._FindCtrl(child)	                 if found:	                     return found	         return None	      #----------------------------------------------------------------------------    # Methods for TextDocument to call    #----------------------------------------------------------------------------    def IsModified(self):        if not self.GetCtrl():            return False        return self.GetCtrl().GetModify()    def SetModifyFalse(self):        self.GetCtrl().SetSavePoint()    def GetValue(self):        if self.GetCtrl():            return self.GetCtrl().GetText()        else:            return None    def SetValue(self, value):        self.GetCtrl().SetText(value)        self.GetCtrl().UpdateLineNumberMarginWidth()        self.GetCtrl().EmptyUndoBuffer()    #----------------------------------------------------------------------------    # STC events    #----------------------------------------------------------------------------    def OnUpdateStatusBar(self, event):        statusBar = self._GetParentFrame().GetStatusBar()        statusBar.SetInsertMode(self.GetCtrl().GetOvertype() == 0)        statusBar.SetLineNumber(self.GetCtrl().GetCurrentLine() + 1)        statusBar.SetColumnNumber(self.GetCtrl().GetColumn(self.GetCtrl().GetCurrentPos()) + 1)    #----------------------------------------------------------------------------    # Format methods    #----------------------------------------------------------------------------    def OnChooseFont(self):        data = wx.FontData()        data.EnableEffects(True)        data.SetInitialFont(self.GetCtrl().GetFont())        data.SetColour(self.GetCtrl().GetFontColor())        fontDialog = wx.FontDialog(self.GetFrame(), data)        fontDialog.CenterOnParent()        if fontDialog.ShowModal() == wx.ID_OK:            data = fontDialog.GetFontData()            self.GetCtrl().SetFont(data.GetChosenFont())            self.GetCtrl().SetFontColor(data.GetColour())            self.GetCtrl().UpdateStyles()        fontDialog.Destroy()    #----------------------------------------------------------------------------    # Find methods    #----------------------------------------------------------------------------    def OnFind(self, replace = False):        findService = wx.GetApp().GetService(FindService.FindService)        if findService:            findService.ShowFindReplaceDialog(findString = self.GetCtrl().GetSelectedText(), replace = replace)    def DoFind(self, forceFindNext = False, forceFindPrevious = False, replace = False, replaceAll = False):        findService = wx.GetApp().GetService(FindService.FindService)        if not findService:            return        findString = findService.GetFindString()        if len(findString) == 0:            return -1        replaceString = findService.GetReplaceString()        flags = findService.GetFlags()        startLoc, endLoc = self.GetCtrl().GetSelection()        wholeWord = flags & wx.FR_WHOLEWORD > 0        matchCase = flags & wx.FR_MATCHCASE > 0        regExp = flags & FindService.FindService.FR_REGEXP > 0        down = flags & wx.FR_DOWN > 0        wrap = flags & FindService.FindService.FR_WRAP > 0        if forceFindPrevious:   # this is from function keys, not dialog box            down = False            wrap = False        # user would want to know they're at the end of file        elif forceFindNext:            down = True            wrap = False        # user would want to know they're at the end of file        badSyntax = False                # On replace dialog operations, user is allowed to replace the currently highlighted text to determine if it should be replaced or not.        # Typically, it is the text from a previous find operation, but we must check to see if it isn't, user may have moved the cursor or selected some other text accidentally.        # If the text is a match, then replace it.        if replace:            result, start, end, replText = findService.DoFind(findString, replaceString, self.GetCtrl().GetSelectedText(), 0, 0, True, matchCase, wholeWord, regExp, replace)            if result > 0:                self.GetCtrl().ReplaceSelection(replText)                self.GetDocument().Modify(True)                wx.GetApp().GetTopWindow().PushStatusText(_("1 occurrence of \"%s\" replaced") % findString)                if down:                    startLoc += len(replText)  # advance start location past replacement string to new text                endLoc = startLoc            elif result == FindService.FIND_SYNTAXERROR:                badSyntax = True                wx.GetApp().GetTopWindow().PushStatusText(_("Invalid regular expression \"%s\"") % findString)        if not badSyntax:            text = self.GetCtrl().GetText()                # Find the next matching text occurance or if it is a ReplaceAll, replace all occurances            # Even if the user is Replacing, we should replace here, but only select the text and let the user replace it with the next Replace operation            result, start, end, text = findService.DoFind(findString, replaceString, text, startLoc, endLoc, down, matchCase, wholeWord, regExp, False, replaceAll, wrap)            if result > 0:                self.GetCtrl().SetTargetStart(0)                self.GetCtrl().SetTargetEnd(self.GetCtrl().GetLength())                self.GetCtrl().ReplaceTarget(text)  # Doing a SetText causes a clear document to be shown when undoing, so using replacetarget instead                self.GetDocument().Modify(True)                if result == 1:                    wx.GetApp().GetTopWindow().PushStatusText(_("1 occurrence of \"%s\" replaced") % findString)                else:                    wx.GetApp().GetTopWindow().PushStatusText(_("%i occurrences of \"%s\" replaced") % (result, findString))            elif result == 0:                self.GetCtrl().SetSelection(start, end)                self.GetCtrl().EnsureVisible(self.GetCtrl().LineFromPosition(end))  # show bottom then scroll up to top                self.GetCtrl().EnsureVisible(self.GetCtrl().LineFromPosition(start)) # do this after ensuring bottom is visible                wx.GetApp().GetTopWindow().PushStatusText(_("Found \"%s\".") % findString)            elif result == FindService.FIND_SYNTAXERROR:                # Dialog for this case gets popped up by the FindService.                wx.GetApp().GetTopWindow().PushStatusText(_("Invalid regular expression \"%s\"") % findString)            else:                wx.MessageBox(_("Can't find \"%s\".") % findString, "Find",                          wx.OK | wx.ICON_INFORMATION)    def _FindServiceHasString(self):        findService = wx.GetApp().GetService(FindService.FindService)        if not findService or not findService.GetFindString():            return False        return True    def OnGotoLine(self, event):        findService = wx.GetApp().GetService(FindService.FindService)        if findService:            line = findService.GetLineNumber(self.GetDocumentManager().FindSuitableParent())            if line > -1:                line = line - 1                self.GetCtrl().EnsureVisible(line)                self.GetCtrl().GotoLine(line)    def GotoLine(self, lineNum):        if lineNum > -1:            lineNum = lineNum - 1  # line numbering for editor is 0 based, we are 1 based.            self.GetCtrl().EnsureVisibleEnforcePolicy(lineNum)            self.GetCtrl().GotoLine(lineNum)    def SetSelection(self, start, end):        self.GetCtrl().SetSelection(start, end)    def EnsureVisible(self, line):        self.GetCtrl().EnsureVisible(line-1)  # line numbering for editor is 0 based, we are 1 based.    def EnsureVisibleEnforcePolicy(self, line):        self.GetCtrl().EnsureVisibleEnforcePolicy(line-1)  # line numbering for editor is 0 based, we are 1 based.    def LineFromPosition(self, pos):        return self.GetCtrl().LineFromPosition(pos)+1  # line numbering for editor is 0 based, we are 1 based.    def PositionFromLine(self, line):        return self.GetCtrl().PositionFromLine(line-1)  # line numbering for editor is 0 based, we are 1 based.    def GetLineEndPosition(self, line):        return self.GetCtrl().GetLineEndPosition(line-1)  # line numbering for editor is 0 based, we are 1 based.    def GetLine(self, lineNum):        return self.GetCtrl().GetLine(lineNum-1)  # line numbering for editor is 0 based, we are 1 based.    def MarkerDefine(self):        """ This must be called after the texteditor is instantiated """        self.GetCtrl().MarkerDefine(TextView.MARKER_NUM, wx.stc.STC_MARK_CIRCLE, wx.BLACK, wx.BLUE)    def MarkerToggle(self, lineNum = -1, marker_index=MARKER_NUM, mask=MARKER_MASK):

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -