2 from wxPython
.wx
import *
3 from wxPython
.stc
import *
7 #----------------------------------------------------------------------
10 ## This version of the editor has been set up to edit Python source
11 ## code. Here is a copy of wxPython/demo/Main.py to play with.
16 #----------------------------------------------------------------------
19 if wxPlatform
== '__WXMSW__':
20 faces
= { 'times': 'Times New Roman',
21 'mono' : 'Courier New',
23 'other': 'Comic Sans MS',
28 faces
= { 'times': 'Times',
31 'other': 'new century schoolbook',
37 #----------------------------------------------------------------------
39 class PythonSTC(wxStyledTextCtrl
):
40 def __init__(self
, parent
, ID
):
41 wxStyledTextCtrl
.__init
__(self
, parent
, ID
,
42 style
= wxNO_FULL_REPAINT_ON_RESIZE
)
44 self
.CmdKeyAssign(ord('B'), wxSTC_SCMOD_CTRL
, wxSTC_CMD_ZOOMIN
)
45 self
.CmdKeyAssign(ord('N'), wxSTC_SCMOD_CTRL
, wxSTC_CMD_ZOOMOUT
)
47 self
.SetLexer(wxSTC_LEX_PYTHON
)
48 self
.SetKeyWords(0, string
.join(keyword
.kwlist
))
50 self
.SetProperty("fold", "1")
51 self
.SetProperty("tab.timmy.whinge.level", "1")
54 self
.SetViewWhiteSpace(false
)
55 #self.SetBufferedDraw(false)
57 self
.SetEdgeMode(wxSTC_EDGE_BACKGROUND
)
58 self
.SetEdgeColumn(78)
60 # Setup a margin to hold fold markers
61 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
62 self
.SetMarginType(2, wxSTC_MARGIN_SYMBOL
)
63 self
.SetMarginMask(2, wxSTC_MASK_FOLDERS
)
64 self
.SetMarginSensitive(2, true
)
65 self
.SetMarginWidth(2, 12)
67 if 0: # simple folder marks, like the old version
68 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_ARROW
, "navy", "navy")
69 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_ARROWDOWN
, "navy", "navy")
70 # Set these to an invisible mark
71 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_BACKGROUND
, "white", "black")
72 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_BACKGROUND
, "white", "black")
73 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_BACKGROUND
, "white", "black")
74 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_BACKGROUND
, "white", "black")
76 else: # more involved "outlining" folder marks
77 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEREND
, wxSTC_MARK_BOXPLUSCONNECTED
, "white", "black")
78 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_BOXMINUSCONNECTED
, "white", "black")
79 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_TCORNER
, "white", "black")
80 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_LCORNER
, "white", "black")
81 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_VLINE
, "white", "black")
82 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_BOXPLUS
, "white", "black")
83 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_BOXMINUS
, "white", "black")
86 EVT_STC_UPDATEUI(self
, ID
, self
.OnUpdateUI
)
87 EVT_STC_MARGINCLICK(self
, ID
, self
.OnMarginClick
)
90 # Make some styles, The lexer defines what each style is used for, we
91 # just have to define what each style looks like. This set is adapted from
92 # Scintilla sample property files.
96 # Global default styles for all languages
97 self
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "face:%(helv)s,size:%(size)d" % faces
)
98 self
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces
)
99 self
.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR
, "face:%(other)s" % faces
)
100 self
.StyleSetSpec(wxSTC_STYLE_BRACELIGHT
, "fore:#FFFFFF,back:#0000FF,bold")
101 self
.StyleSetSpec(wxSTC_STYLE_BRACEBAD
, "fore:#000000,back:#FF0000,bold")
105 self
.StyleSetSpec(wxSTC_P_DEFAULT
, "fore:#808080,face:%(helv)s,size:%(size)d" % faces
)
107 self
.StyleSetSpec(wxSTC_P_COMMENTLINE
, "fore:#007F00,face:%(other)s,size:%(size)d" % faces
)
109 self
.StyleSetSpec(wxSTC_P_NUMBER
, "fore:#007F7F,size:%(size)d" % faces
)
111 self
.StyleSetSpec(wxSTC_P_STRING
, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % faces
)
112 # Single quoted string
113 self
.StyleSetSpec(wxSTC_P_CHARACTER
, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % faces
)
115 self
.StyleSetSpec(wxSTC_P_WORD
, "fore:#00007F,bold,size:%(size)d" % faces
)
117 self
.StyleSetSpec(wxSTC_P_TRIPLE
, "fore:#7F0000,size:%(size)d" % faces
)
118 # Triple double quotes
119 self
.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE
, "fore:#7F0000,size:%(size)d" % faces
)
120 # Class name definition
121 self
.StyleSetSpec(wxSTC_P_CLASSNAME
, "fore:#0000FF,bold,underline,size:%(size)d" % faces
)
122 # Function or method name definition
123 self
.StyleSetSpec(wxSTC_P_DEFNAME
, "fore:#007F7F,bold,size:%(size)d" % faces
)
125 self
.StyleSetSpec(wxSTC_P_OPERATOR
, "bold,size:%(size)d" % faces
)
127 self
.StyleSetSpec(wxSTC_P_IDENTIFIER
, "fore:#808080,face:%(helv)s,size:%(size)d" % faces
)
129 self
.StyleSetSpec(wxSTC_P_COMMENTBLOCK
, "fore:#7F7F7F,size:%(size)d" % faces
)
130 # End of line where string is not closed
131 self
.StyleSetSpec(wxSTC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces
)
134 self
.SetCaretForeground("BLUE")
136 EVT_KEY_DOWN(self
, self
.OnKeyPressed
)
139 def OnKeyPressed(self
, event
):
140 if self
.CallTipActive():
142 key
= event
.KeyCode()
143 if key
== 32 and event
.ControlDown():
144 pos
= self
.GetCurrentPos()
146 if event
.ShiftDown():
147 self
.CallTipSetBackground("yellow")
148 self
.CallTipShow(pos
, 'param1, param2')
152 #for x in range(50000):
153 # lst.append('%05d' % x)
154 #st = string.join(lst)
156 #self.AutoCompShow(0, st)
158 kw
= keyword
.kwlist
[:]
161 kw
.append("__init__")
164 kw
.append("this_is_a_longer_value")
165 kw
.append("this_is_a_much_much_much_much_much_much_much_longer_value")
167 kw
.sort() # Python sorts are case sensitive
168 self
.AutoCompSetIgnoreCase(false
) # so this needs to match
170 self
.AutoCompShow(0, string
.join(kw
))
175 def OnUpdateUI(self
, evt
):
176 # check for matching braces
180 caretPos
= self
.GetCurrentPos()
182 charBefore
= self
.GetCharAt(caretPos
- 1)
183 styleBefore
= self
.GetStyleAt(caretPos
- 1)
186 if charBefore
and chr(charBefore
) in "[]{}()" and styleBefore
== wxSTC_P_OPERATOR
:
187 braceAtCaret
= caretPos
- 1
191 charAfter
= self
.GetCharAt(caretPos
)
192 styleAfter
= self
.GetStyleAt(caretPos
)
193 if charAfter
and chr(charAfter
) in "[]{}()" and styleAfter
== wxSTC_P_OPERATOR
:
194 braceAtCaret
= caretPos
196 if braceAtCaret
>= 0:
197 braceOpposite
= self
.BraceMatch(braceAtCaret
)
199 if braceAtCaret
!= -1 and braceOpposite
== -1:
200 self
.BraceBadLight(braceAtCaret
)
202 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
203 #pt = self.PointFromPosition(braceOpposite)
204 #self.Refresh(true, wxRect(pt.x, pt.y, 5,5))
209 def OnMarginClick(self
, evt
):
210 # fold and unfold as needed
211 if evt
.GetMargin() == 2:
212 if evt
.GetShift() and evt
.GetControl():
215 lineClicked
= self
.LineFromPosition(evt
.GetPosition())
216 if self
.GetFoldLevel(lineClicked
) & wxSTC_FOLDLEVELHEADERFLAG
:
218 self
.SetFoldExpanded(lineClicked
, true
)
219 self
.Expand(lineClicked
, true
, true
, 1)
220 elif evt
.GetControl():
221 if self
.GetFoldExpanded(lineClicked
):
222 self
.SetFoldExpanded(lineClicked
, false
)
223 self
.Expand(lineClicked
, false
, true
, 0)
225 self
.SetFoldExpanded(lineClicked
, true
)
226 self
.Expand(lineClicked
, true
, true
, 100)
228 self
.ToggleFold(lineClicked
)
232 lineCount
= self
.GetLineCount()
235 # find out if we are folding or unfolding
236 for lineNum
in range(lineCount
):
237 if self
.GetFoldLevel(lineNum
) & wxSTC_FOLDLEVELHEADERFLAG
:
238 expanding
= not self
.GetFoldExpanded(lineNum
)
242 while lineNum
< lineCount
:
243 level
= self
.GetFoldLevel(lineNum
)
244 if level
& wxSTC_FOLDLEVELHEADERFLAG
and \
245 (level
& wxSTC_FOLDLEVELNUMBERMASK
) == wxSTC_FOLDLEVELBASE
:
248 self
.SetFoldExpanded(lineNum
, true
)
249 lineNum
= self
.Expand(lineNum
, true
)
250 lineNum
= lineNum
- 1
252 lastChild
= self
.GetLastChild(lineNum
, -1)
253 self
.SetFoldExpanded(lineNum
, false
)
254 if lastChild
> lineNum
:
255 self
.HideLines(lineNum
+1, lastChild
)
257 lineNum
= lineNum
+ 1
261 def Expand(self
, line
, doExpand
, force
=false
, visLevels
=0, level
=-1):
262 lastChild
= self
.GetLastChild(line
, level
)
264 while line
<= lastChild
:
267 self
.ShowLines(line
, line
)
269 self
.HideLines(line
, line
)
272 self
.ShowLines(line
, line
)
275 level
= self
.GetFoldLevel(line
)
277 if level
& wxSTC_FOLDLEVELHEADERFLAG
:
280 self
.SetFoldExpanded(line
, true
)
282 self
.SetFoldExpanded(line
, false
)
283 line
= self
.Expand(line
, doExpand
, force
, visLevels
-1)
286 if doExpand
and self
.GetFoldExpanded(line
):
287 line
= self
.Expand(line
, true
, force
, visLevels
-1)
289 line
= self
.Expand(line
, false
, force
, visLevels
-1)
296 #----------------------------------------------------------------------
300 def runTest(frame
, nb
, log
):
302 ed
= p
= PythonSTC(nb
, -1)
304 p
= wxPanel(nb
, -1, style
= wxNO_FULL_REPAINT_ON_RESIZE
)
305 ed
= PythonSTC(p
, -1)
306 s
= wxBoxSizer(wxHORIZONTAL
)
307 s
.Add(ed
, 1, wxEXPAND
)
309 p
.SetAutoLayout(true
)
312 ed
.SetText(demoText
+ open('Main.py').read())
316 # line numbers in the margin
317 ed
.SetMarginType(1, wxSTC_MARGIN_NUMBER
)
318 ed
.SetMarginWidth(1, 25)
324 #----------------------------------------------------------------------
329 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
330 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
336 if __name__
== '__main__':
339 run
.main(['', os
.path
.basename(sys
.argv
[0])])
345 #----------------------------------------------------------------------
346 #----------------------------------------------------------------------