etisserant@283: import keyword etisserant@283: import os etisserant@283: import wx etisserant@283: import wx.stc as stc etisserant@283: etisserant@283: #---------------------------------------------------------------------- etisserant@283: etisserant@283: """ etisserant@283: This Python editor class comes from the wxPython demo, a bit tweaked etisserant@283: """ etisserant@283: etisserant@283: #---------------------------------------------------------------------- etisserant@283: etisserant@283: etisserant@283: if wx.Platform == '__WXMSW__': etisserant@283: faces = { 'times': 'Times New Roman', etisserant@283: 'mono' : 'Courier New', etisserant@283: 'helv' : 'Arial', etisserant@283: 'other': 'Comic Sans MS', etisserant@283: 'size' : 10, etisserant@283: 'size2': 8, etisserant@283: } etisserant@283: elif wx.Platform == '__WXMAC__': etisserant@283: faces = { 'times': 'Times New Roman', etisserant@283: 'mono' : 'Monaco', etisserant@283: 'helv' : 'Arial', etisserant@283: 'other': 'Comic Sans MS', etisserant@283: 'size' : 12, etisserant@283: 'size2': 10, etisserant@283: } etisserant@283: else: etisserant@283: faces = { 'times': 'Times', etisserant@283: 'mono' : 'Courier', etisserant@283: 'helv' : 'Helvetica', etisserant@283: 'other': 'new century schoolbook', etisserant@283: 'size' : 12, etisserant@283: 'size2': 10, etisserant@283: } etisserant@283: etisserant@283: etisserant@283: #---------------------------------------------------------------------- etisserant@283: etisserant@283: class PythonSTC(stc.StyledTextCtrl): etisserant@283: etisserant@283: fold_symbols = 2 etisserant@283: etisserant@283: def __init__(self, parent, ID, etisserant@283: pos=wx.DefaultPosition, size=wx.DefaultSize, etisserant@283: style=0): etisserant@283: stc.StyledTextCtrl.__init__(self, parent, ID, pos, size, style) etisserant@283: etisserant@283: self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN) etisserant@283: self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT) etisserant@283: etisserant@283: self.SetLexer(stc.STC_LEX_PYTHON) etisserant@283: self.SetKeyWords(0, " ".join(keyword.kwlist)) etisserant@283: etisserant@283: self.SetProperty("fold", "1") etisserant@283: self.SetProperty("tab.timmy.whinge.level", "1") etisserant@283: self.SetMargins(0,0) etisserant@283: etisserant@283: self.SetViewWhiteSpace(False) etisserant@283: #self.SetBufferedDraw(False) etisserant@283: #self.SetViewEOL(True) etisserant@283: #self.SetEOLMode(stc.STC_EOL_CRLF) etisserant@283: #self.SetUseAntiAliasing(True) etisserant@283: etisserant@283: self.SetEdgeMode(stc.STC_EDGE_BACKGROUND) etisserant@283: self.SetEdgeColumn(78) etisserant@283: etisserant@283: # Setup a margin to hold fold markers etisserant@283: #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER? etisserant@283: self.SetMarginType(2, stc.STC_MARGIN_SYMBOL) etisserant@283: self.SetMarginMask(2, stc.STC_MASK_FOLDERS) etisserant@283: self.SetMarginSensitive(2, True) etisserant@283: self.SetMarginWidth(2, 12) etisserant@283: etisserant@283: if self.fold_symbols == 0: etisserant@283: # Arrow pointing right for contracted folders, arrow pointing down for expanded etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_ARROWDOWN, "black", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_ARROW, "black", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_EMPTY, "black", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_EMPTY, "black", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: etisserant@283: elif self.fold_symbols == 1: etisserant@283: # Plus for contracted folders, minus for expanded etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_MINUS, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_PLUS, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY, "white", "black") etisserant@283: etisserant@283: elif self.fold_symbols == 2: etisserant@283: # Like a flattened tree control using circular headers and curved joins etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_CIRCLEMINUS, "white", "#404040") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_CIRCLEPLUS, "white", "#404040") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE, "white", "#404040") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNERCURVE, "white", "#404040") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_CIRCLEPLUSCONNECTED, "white", "#404040") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_CIRCLEMINUSCONNECTED, "white", "#404040") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNERCURVE, "white", "#404040") etisserant@283: etisserant@283: elif self.fold_symbols == 3: etisserant@283: # Like a flattened tree control using square headers etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_BOXMINUS, "white", "#808080") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_BOXPLUS, "white", "#808080") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE, "white", "#808080") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNER, "white", "#808080") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_BOXPLUSCONNECTED, "white", "#808080") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER, "white", "#808080") etisserant@283: etisserant@283: etisserant@283: self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI) etisserant@283: self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick) etisserant@283: self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed) etisserant@283: etisserant@283: # Make some styles, The lexer defines what each style is used for, we etisserant@283: # just have to define what each style looks like. This set is adapted from etisserant@283: # Scintilla sample property files. etisserant@283: etisserant@283: # Global default styles for all languages etisserant@283: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces) etisserant@283: self.StyleClearAll() # Reset all to be like the default etisserant@283: etisserant@283: # Global default styles for all languages etisserant@283: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces) etisserant@283: self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces) etisserant@283: self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces) etisserant@283: self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold") etisserant@283: self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold") etisserant@283: etisserant@283: # Python styles etisserant@283: # Default etisserant@283: self.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) etisserant@283: # Comments etisserant@283: self.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) etisserant@283: # Number etisserant@283: self.StyleSetSpec(stc.STC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces) etisserant@283: # String etisserant@283: self.StyleSetSpec(stc.STC_P_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) etisserant@283: # Single quoted string etisserant@283: self.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces) etisserant@283: # Keyword etisserant@283: self.StyleSetSpec(stc.STC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces) etisserant@283: # Triple quotes etisserant@283: self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % faces) etisserant@283: # Triple double quotes etisserant@283: self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % faces) etisserant@283: # Class name definition etisserant@283: self.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % faces) etisserant@283: # Function or method name definition etisserant@283: self.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % faces) etisserant@283: # Operators etisserant@283: self.StyleSetSpec(stc.STC_P_OPERATOR, "bold,size:%(size)d" % faces) etisserant@283: # Identifiers etisserant@283: self.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces) etisserant@283: # Comment-blocks etisserant@283: self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % faces) etisserant@283: # End of line where string is not closed etisserant@283: self.StyleSetSpec(stc.STC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces) etisserant@283: etisserant@283: self.SetCaretForeground("BLUE") etisserant@283: etisserant@283: def OnKeyPressed(self, event): etisserant@283: if self.CallTipActive(): etisserant@283: self.CallTipCancel() etisserant@283: etisserant@283: event.Skip() etisserant@283: ## For later use etisserant@283: # key = event.GetKeyCode() etisserant@283: # if key == 32 and event.ControlDown(): etisserant@283: # pos = self.GetCurrentPos() etisserant@283: # etisserant@283: # # Tips etisserant@283: # if event.ShiftDown(): etisserant@283: # self.CallTipSetBackground("yellow") etisserant@283: # self.CallTipShow(pos, 'lots of of text: blah, blah, blah\n\n' etisserant@283: # 'show some suff, maybe parameters..\n\n' etisserant@283: # 'fubar(param1, param2)') etisserant@283: # # Code completion etisserant@283: # else: etisserant@283: # #lst = [] etisserant@283: # #for x in range(50000): etisserant@283: # # lst.append('%05d' % x) etisserant@283: # #st = " ".join(lst) etisserant@283: # #print len(st) etisserant@283: # #self.AutoCompShow(0, st) etisserant@283: # etisserant@283: # kw = keyword.kwlist[:] etisserant@283: # kw.append("zzzzzz?2") etisserant@283: # kw.append("aaaaa?2") etisserant@283: # kw.append("__init__?3") etisserant@283: # kw.append("zzaaaaa?2") etisserant@283: # kw.append("zzbaaaa?2") etisserant@283: # kw.append("this_is_a_longer_value") etisserant@283: # #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value") etisserant@283: # etisserant@283: # kw.sort() # Python sorts are case sensitive etisserant@283: # self.AutoCompSetIgnoreCase(False) # so this needs to match etisserant@283: # etisserant@283: # # Images are specified with a appended "?type" etisserant@283: # for i in range(len(kw)): etisserant@283: # if kw[i] in keyword.kwlist: etisserant@283: # kw[i] = kw[i] + "?1" etisserant@283: # etisserant@283: # self.AutoCompShow(0, " ".join(kw)) etisserant@283: # else: etisserant@283: # event.Skip() etisserant@283: etisserant@283: etisserant@283: def OnUpdateUI(self, evt): etisserant@283: # check for matching braces etisserant@283: braceAtCaret = -1 etisserant@283: braceOpposite = -1 etisserant@283: charBefore = None etisserant@283: caretPos = self.GetCurrentPos() etisserant@283: etisserant@283: if caretPos > 0: etisserant@283: charBefore = self.GetCharAt(caretPos - 1) etisserant@283: styleBefore = self.GetStyleAt(caretPos - 1) etisserant@283: etisserant@283: # check before etisserant@283: if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR: etisserant@283: braceAtCaret = caretPos - 1 etisserant@283: etisserant@283: # check after etisserant@283: if braceAtCaret < 0: etisserant@283: charAfter = self.GetCharAt(caretPos) etisserant@283: styleAfter = self.GetStyleAt(caretPos) etisserant@283: etisserant@283: if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR: etisserant@283: braceAtCaret = caretPos etisserant@283: etisserant@283: if braceAtCaret >= 0: etisserant@283: braceOpposite = self.BraceMatch(braceAtCaret) etisserant@283: etisserant@283: if braceAtCaret != -1 and braceOpposite == -1: etisserant@283: self.BraceBadLight(braceAtCaret) etisserant@283: else: etisserant@283: self.BraceHighlight(braceAtCaret, braceOpposite) etisserant@283: #pt = self.PointFromPosition(braceOpposite) etisserant@283: #self.Refresh(True, wxRect(pt.x, pt.y, 5,5)) etisserant@283: #print pt etisserant@283: #self.Refresh(False) etisserant@283: etisserant@283: etisserant@283: def OnMarginClick(self, evt): etisserant@283: # fold and unfold as needed etisserant@283: if evt.GetMargin() == 2: etisserant@283: if evt.GetShift() and evt.GetControl(): etisserant@283: self.FoldAll() etisserant@283: else: etisserant@283: lineClicked = self.LineFromPosition(evt.GetPosition()) etisserant@283: etisserant@283: if self.GetFoldLevel(lineClicked) & stc.STC_FOLDLEVELHEADERFLAG: etisserant@283: if evt.GetShift(): etisserant@283: self.SetFoldExpanded(lineClicked, True) etisserant@283: self.Expand(lineClicked, True, True, 1) etisserant@283: elif evt.GetControl(): etisserant@283: if self.GetFoldExpanded(lineClicked): etisserant@283: self.SetFoldExpanded(lineClicked, False) etisserant@283: self.Expand(lineClicked, False, True, 0) etisserant@283: else: etisserant@283: self.SetFoldExpanded(lineClicked, True) etisserant@283: self.Expand(lineClicked, True, True, 100) etisserant@283: else: etisserant@283: self.ToggleFold(lineClicked) etisserant@283: etisserant@283: etisserant@283: def FoldAll(self): etisserant@283: lineCount = self.GetLineCount() etisserant@283: expanding = True etisserant@283: etisserant@283: # find out if we are folding or unfolding etisserant@283: for lineNum in range(lineCount): etisserant@283: if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG: etisserant@283: expanding = not self.GetFoldExpanded(lineNum) etisserant@283: break etisserant@283: etisserant@283: lineNum = 0 etisserant@283: etisserant@283: while lineNum < lineCount: etisserant@283: level = self.GetFoldLevel(lineNum) etisserant@283: if level & stc.STC_FOLDLEVELHEADERFLAG and \ etisserant@283: (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE: etisserant@283: etisserant@283: if expanding: etisserant@283: self.SetFoldExpanded(lineNum, True) etisserant@283: lineNum = self.Expand(lineNum, True) etisserant@283: lineNum = lineNum - 1 etisserant@283: else: etisserant@283: lastChild = self.GetLastChild(lineNum, -1) etisserant@283: self.SetFoldExpanded(lineNum, False) etisserant@283: etisserant@283: if lastChild > lineNum: etisserant@283: self.HideLines(lineNum+1, lastChild) etisserant@283: etisserant@283: lineNum = lineNum + 1 etisserant@283: etisserant@283: etisserant@283: etisserant@283: def Expand(self, line, doExpand, force=False, visLevels=0, level=-1): etisserant@283: lastChild = self.GetLastChild(line, level) etisserant@283: line = line + 1 etisserant@283: etisserant@283: while line <= lastChild: etisserant@283: if force: etisserant@283: if visLevels > 0: etisserant@283: self.ShowLines(line, line) etisserant@283: else: etisserant@283: self.HideLines(line, line) etisserant@283: else: etisserant@283: if doExpand: etisserant@283: self.ShowLines(line, line) etisserant@283: etisserant@283: if level == -1: etisserant@283: level = self.GetFoldLevel(line) etisserant@283: etisserant@283: if level & stc.STC_FOLDLEVELHEADERFLAG: etisserant@283: if force: etisserant@283: if visLevels > 1: etisserant@283: self.SetFoldExpanded(line, True) etisserant@283: else: etisserant@283: self.SetFoldExpanded(line, False) etisserant@283: etisserant@283: line = self.Expand(line, doExpand, force, visLevels-1) etisserant@283: etisserant@283: else: etisserant@283: if doExpand and self.GetFoldExpanded(line): etisserant@283: line = self.Expand(line, True, force, visLevels-1) etisserant@283: else: etisserant@283: line = self.Expand(line, False, force, visLevels-1) etisserant@283: else: etisserant@283: line = line + 1 etisserant@283: etisserant@283: return line etisserant@283: etisserant@283: etisserant@283: #---------------------------------------------------------------------- etisserant@283: class PythonCodeEditor(PythonSTC): etisserant@283: def __init__(self, parent): etisserant@283: PythonSTC.__init__(self, parent, -1, style=wx.BORDER_NONE) etisserant@283: self.SetUpEditor() etisserant@283: etisserant@283: # Some methods to make it compatible with how the wxTextCtrl is used etisserant@283: def SetValue(self, value): etisserant@283: if wx.USE_UNICODE: greg@332: value = value.decode('utf-8') etisserant@283: self.SetText(value) etisserant@283: self.EmptyUndoBuffer() etisserant@283: self.SetSavePoint() etisserant@283: etisserant@283: def IsModified(self): etisserant@283: return self.GetModify() etisserant@283: etisserant@283: def Clear(self): etisserant@283: self.ClearAll() etisserant@283: etisserant@283: def SetInsertionPoint(self, pos): etisserant@283: self.SetCurrentPos(pos) etisserant@283: self.SetAnchor(pos) etisserant@283: etisserant@283: def ShowPosition(self, pos): etisserant@283: line = self.LineFromPosition(pos) etisserant@283: #self.EnsureVisible(line) etisserant@283: self.GotoLine(line) etisserant@283: etisserant@283: def GetLastPosition(self): etisserant@283: return self.GetLength() etisserant@283: etisserant@283: def GetPositionFromLine(self, line): etisserant@283: return self.PositionFromLine(line) etisserant@283: etisserant@283: def GetRange(self, start, end): etisserant@283: return self.GetTextRange(start, end) etisserant@283: etisserant@283: def GetSelection(self): etisserant@283: return self.GetAnchor(), self.GetCurrentPos() etisserant@283: etisserant@283: def SetSelection(self, start, end): etisserant@283: self.SetSelectionStart(start) etisserant@283: self.SetSelectionEnd(end) etisserant@283: etisserant@283: def SelectLine(self, line): etisserant@283: start = self.PositionFromLine(line) etisserant@283: end = self.GetLineEndPosition(line) etisserant@283: self.SetSelection(start, end) etisserant@283: etisserant@283: def SetUpEditor(self): etisserant@283: """ etisserant@283: This method carries out the work of setting up the demo editor. etisserant@283: It's seperate so as not to clutter up the init code. etisserant@283: """ etisserant@283: import keyword etisserant@283: etisserant@283: self.SetLexer(stc.STC_LEX_PYTHON) etisserant@283: self.SetKeyWords(0, " ".join(keyword.kwlist)) etisserant@283: etisserant@283: # Enable folding etisserant@283: self.SetProperty("fold", "1" ) etisserant@283: etisserant@283: # Highlight tab/space mixing (shouldn't be any) etisserant@283: self.SetProperty("tab.timmy.whinge.level", "1") etisserant@283: etisserant@283: # Set left and right margins etisserant@283: self.SetMargins(2,2) etisserant@283: etisserant@283: # Set up the numbers in the margin for margin #1 etisserant@283: self.SetMarginType(1, wx.stc.STC_MARGIN_NUMBER) etisserant@283: # Reasonable value for, say, 4-5 digits using a mono font (40 pix) etisserant@283: self.SetMarginWidth(1, 40) etisserant@283: etisserant@283: # Indentation and tab stuff etisserant@283: self.SetIndent(4) # Proscribed indent size for wx etisserant@283: self.SetIndentationGuides(True) # Show indent guides etisserant@283: self.SetBackSpaceUnIndents(True)# Backspace unindents rather than delete 1 space etisserant@283: self.SetTabIndents(True) # Tab key indents etisserant@283: self.SetTabWidth(4) # Proscribed tab size for wx etisserant@283: self.SetUseTabs(False) # Use spaces rather than tabs, or etisserant@283: # TabTimmy will complain! etisserant@283: # White space etisserant@283: self.SetViewWhiteSpace(False) # Don't view white space etisserant@283: etisserant@283: # EOL: Since we are loading/saving ourselves, and the etisserant@283: # strings will always have \n's in them, set the STC to etisserant@283: # edit them that way. etisserant@283: self.SetEOLMode(wx.stc.STC_EOL_LF) etisserant@283: self.SetViewEOL(False) etisserant@283: etisserant@283: # No right-edge mode indicator etisserant@283: self.SetEdgeMode(stc.STC_EDGE_NONE) etisserant@283: etisserant@283: # Setup a margin to hold fold markers etisserant@283: self.SetMarginType(2, stc.STC_MARGIN_SYMBOL) etisserant@283: self.SetMarginMask(2, stc.STC_MASK_FOLDERS) etisserant@283: self.SetMarginSensitive(2, True) etisserant@283: self.SetMarginWidth(2, 12) etisserant@283: etisserant@283: # and now set up the fold markers etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_BOXPLUSCONNECTED, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNER, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_BOXPLUS, "white", "black") etisserant@283: self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_BOXMINUS, "white", "black") etisserant@283: etisserant@283: # Global default style etisserant@283: if wx.Platform == '__WXMSW__': etisserant@283: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, etisserant@283: 'fore:#000000,back:#FFFFFF,face:Courier New') etisserant@283: elif wx.Platform == '__WXMAC__': etisserant@283: # TODO: if this looks fine on Linux too, remove the Mac-specific case etisserant@283: # and use this whenever OS != MSW. etisserant@283: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, etisserant@283: 'fore:#000000,back:#FFFFFF,face:Monaco') etisserant@283: else: etisserant@283: defsize = wx.SystemSettings.GetFont(wx.SYS_ANSI_FIXED_FONT).GetPointSize() etisserant@283: self.StyleSetSpec(stc.STC_STYLE_DEFAULT, etisserant@283: 'fore:#000000,back:#FFFFFF,face:Courier,size:%d'%defsize) etisserant@283: etisserant@283: # Clear styles and revert to default. etisserant@283: self.StyleClearAll() etisserant@283: etisserant@283: # Following style specs only indicate differences from default. etisserant@283: # The rest remains unchanged. etisserant@283: etisserant@283: # Line numbers in margin etisserant@283: self.StyleSetSpec(wx.stc.STC_STYLE_LINENUMBER,'fore:#000000,back:#99A9C2') etisserant@283: # Highlighted brace etisserant@283: self.StyleSetSpec(wx.stc.STC_STYLE_BRACELIGHT,'fore:#00009D,back:#FFFF00') etisserant@283: # Unmatched brace etisserant@283: self.StyleSetSpec(wx.stc.STC_STYLE_BRACEBAD,'fore:#00009D,back:#FF0000') etisserant@283: # Indentation guide etisserant@283: self.StyleSetSpec(wx.stc.STC_STYLE_INDENTGUIDE, "fore:#CDCDCD") etisserant@283: etisserant@283: # Python styles etisserant@283: self.StyleSetSpec(wx.stc.STC_P_DEFAULT, 'fore:#000000') etisserant@283: # Comments etisserant@283: self.StyleSetSpec(wx.stc.STC_P_COMMENTLINE, 'fore:#008000,back:#F0FFF0') etisserant@283: self.StyleSetSpec(wx.stc.STC_P_COMMENTBLOCK, 'fore:#008000,back:#F0FFF0') etisserant@283: # Numbers etisserant@283: self.StyleSetSpec(wx.stc.STC_P_NUMBER, 'fore:#008080') etisserant@283: # Strings and characters etisserant@283: self.StyleSetSpec(wx.stc.STC_P_STRING, 'fore:#800080') etisserant@283: self.StyleSetSpec(wx.stc.STC_P_CHARACTER, 'fore:#800080') etisserant@283: # Keywords etisserant@283: self.StyleSetSpec(wx.stc.STC_P_WORD, 'fore:#000080,bold') etisserant@283: # Triple quotes etisserant@283: self.StyleSetSpec(wx.stc.STC_P_TRIPLE, 'fore:#800080,back:#FFFFEA') etisserant@283: self.StyleSetSpec(wx.stc.STC_P_TRIPLEDOUBLE, 'fore:#800080,back:#FFFFEA') etisserant@283: # Class names etisserant@283: self.StyleSetSpec(wx.stc.STC_P_CLASSNAME, 'fore:#0000FF,bold') etisserant@283: # Function names etisserant@283: self.StyleSetSpec(wx.stc.STC_P_DEFNAME, 'fore:#008080,bold') etisserant@283: # Operators etisserant@283: self.StyleSetSpec(wx.stc.STC_P_OPERATOR, 'fore:#800000,bold') etisserant@283: # Identifiers. I leave this as not bold because everything seems etisserant@283: # to be an identifier if it doesn't match the above criterae etisserant@283: self.StyleSetSpec(wx.stc.STC_P_IDENTIFIER, 'fore:#000000') etisserant@283: etisserant@283: # Caret color etisserant@283: self.SetCaretForeground("BLUE") etisserant@283: # Selection background etisserant@283: self.SetSelBackground(1, '#66CCFF') etisserant@283: etisserant@283: self.SetSelBackground(True, wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT)) etisserant@283: self.SetSelForeground(True, wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)) etisserant@283: etisserant@283: def RegisterModifiedEvent(self, eventHandler): etisserant@283: self.Bind(wx.stc.EVT_STC_CHANGE, eventHandler) etisserant@283: etisserant@283: etisserant@283: class PythonCodePanel(wx.Panel): etisserant@283: """Panel for the 'Demo Code' tab""" etisserant@283: def __init__(self, parent, mainFrame): etisserant@283: wx.Panel.__init__(self, parent, size=(1,1)) etisserant@283: self.mainFrame = mainFrame etisserant@283: self.editor = PythonCodeEditor(self) etisserant@283: self.editor.RegisterModifiedEvent(self.OnCodeModified) etisserant@283: etisserant@283: self.btnSave = wx.Button(self, -1, "Save") etisserant@283: self.btnRestore = wx.Button(self, -1, "Restore") etisserant@283: self.btnSave.Enable(False) etisserant@283: self.btnSave.Bind(wx.EVT_BUTTON, self.OnSave) etisserant@283: self.btnRestore.Bind(wx.EVT_BUTTON, self.OnRestore) etisserant@283: etisserant@283: self.controlBox = wx.BoxSizer(wx.HORIZONTAL) etisserant@283: self.controlBox.Add(self.btnSave, 0, wx.RIGHT, 5) etisserant@283: self.controlBox.Add(self.btnRestore, 0) etisserant@283: etisserant@283: self.box = wx.BoxSizer(wx.VERTICAL) etisserant@283: self.box.Add(self.controlBox, 0, wx.EXPAND) etisserant@283: self.box.Add(wx.StaticLine(self), 0, wx.EXPAND) etisserant@283: self.box.Add(self.editor, 1, wx.EXPAND) etisserant@283: etisserant@283: self.box.Fit(self) etisserant@283: self.SetSizer(self.box) etisserant@283: etisserant@283: self.sourceFile = None greg@332: greg@332: self.Bind(wx.EVT_MENU, self.OnSave, id=wx.ID_SAVE) greg@332: accel = wx.AcceleratorTable([wx.AcceleratorEntry(wx.ACCEL_CTRL, 83, wx.ID_SAVE)]) greg@332: self.SetAcceleratorTable(accel) etisserant@283: etisserant@283: # Loads from a file object etisserant@283: def LoadSourceFile(self, filename): etisserant@283: self.sourceFile = filename etisserant@283: if os.path.exists(filename): etisserant@283: self.LoadSource(file(filename).read()) etisserant@283: etisserant@283: def LoadSource(self, source): etisserant@283: self.editor.Clear() etisserant@283: self.editor.SetValue(source) etisserant@283: self.JumpToLine(0) etisserant@283: self.btnSave.Enable(False) etisserant@283: etisserant@283: def JumpToLine(self, line, highlight=False): etisserant@283: self.editor.GotoLine(line) etisserant@283: self.editor.SetFocus() etisserant@283: if highlight: etisserant@283: self.editor.SelectLine(line) etisserant@283: etisserant@283: def OnCodeModified(self, event): etisserant@283: self.btnSave.Enable(self.editor.IsModified()) etisserant@283: # TODO : add callback etisserant@283: etisserant@283: def OnSave(self, event): etisserant@283: overwriteMsg = "You are about to overwrite that file\n" + \ etisserant@283: "Do you want to continue?" etisserant@283: dlg = wx.MessageDialog(self, overwriteMsg, "wxPython Demo", etisserant@283: wx.YES_NO | wx.NO_DEFAULT| wx.ICON_EXCLAMATION) etisserant@283: result = dlg.ShowModal() etisserant@283: if result == wx.ID_NO: etisserant@283: return etisserant@283: dlg.Destroy() etisserant@283: greg@332: source = self.editor.GetText().encode("utf-8") etisserant@283: etisserant@283: f = file(self.sourceFile, "w") etisserant@283: f.write(source) etisserant@283: f.close() etisserant@283: etisserant@283: # TODO etisserant@283: #self.mainFrame.SetTreeModified(True) etisserant@283: etisserant@283: etisserant@283: def OnRestore(self, event): etisserant@283: self.LoadSourceFile(self.sourceFile) etisserant@283: self.btnSave.Enable(self.editor.IsModified())