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
):
43 def __init__(self
, parent
, ID
):
44 wxStyledTextCtrl
.__init
__(self
, parent
, ID
,
45 style
= wxNO_FULL_REPAINT_ON_RESIZE
)
47 self
.CmdKeyAssign(ord('B'), wxSTC_SCMOD_CTRL
, wxSTC_CMD_ZOOMIN
)
48 self
.CmdKeyAssign(ord('N'), wxSTC_SCMOD_CTRL
, wxSTC_CMD_ZOOMOUT
)
50 self
.SetLexer(wxSTC_LEX_PYTHON
)
51 self
.SetKeyWords(0, " ".join(keyword
.kwlist
))
53 self
.SetProperty("fold", "1")
54 self
.SetProperty("tab.timmy.whinge.level", "1")
57 self
.SetViewWhiteSpace(False)
58 #self.SetBufferedDraw(False)
59 #self.SetViewEOL(True)
61 self
.SetEdgeMode(wxSTC_EDGE_BACKGROUND
)
62 self
.SetEdgeColumn(78)
64 # Setup a margin to hold fold markers
65 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
66 self
.SetMarginType(2, wxSTC_MARGIN_SYMBOL
)
67 self
.SetMarginMask(2, wxSTC_MASK_FOLDERS
)
68 self
.SetMarginSensitive(2, True)
69 self
.SetMarginWidth(2, 12)
72 if self
.fold_symbols
== 0:
73 # Arrow pointing right for contracted folders, arrow pointing down for expanded
74 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_ARROWDOWN
, "black", "black");
75 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_ARROW
, "black", "black");
76 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_EMPTY
, "black", "black");
77 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_EMPTY
, "black", "black");
78 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEREND
, wxSTC_MARK_EMPTY
, "white", "black");
79 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_EMPTY
, "white", "black");
80 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_EMPTY
, "white", "black");
82 elif self
.fold_symbols
== 1:
83 # Plus for contracted folders, minus for expanded
84 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_MINUS
, "white", "black");
85 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_PLUS
, "white", "black");
86 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_EMPTY
, "white", "black");
87 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_EMPTY
, "white", "black");
88 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEREND
, wxSTC_MARK_EMPTY
, "white", "black");
89 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_EMPTY
, "white", "black");
90 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_EMPTY
, "white", "black");
92 elif self
.fold_symbols
== 2:
93 # Like a flattened tree control using circular headers and curved joins
94 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_CIRCLEMINUS
, "white", "#404040");
95 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_CIRCLEPLUS
, "white", "#404040");
96 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_VLINE
, "white", "#404040");
97 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_LCORNERCURVE
, "white", "#404040");
98 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEREND
, wxSTC_MARK_CIRCLEPLUSCONNECTED
, "white", "#404040");
99 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_CIRCLEMINUSCONNECTED
, "white", "#404040");
100 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_TCORNERCURVE
, "white", "#404040");
102 elif self
.fold_symbols
== 3:
103 # Like a flattened tree control using square headers
104 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_BOXMINUS
, "white", "#808080")
105 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_BOXPLUS
, "white", "#808080")
106 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB
, wxSTC_MARK_VLINE
, "white", "#808080")
107 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL
, wxSTC_MARK_LCORNER
, "white", "#808080")
108 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEREND
, wxSTC_MARK_BOXPLUSCONNECTED
, "white", "#808080")
109 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID
, wxSTC_MARK_BOXMINUSCONNECTED
, "white", "#808080")
110 self
.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL
, wxSTC_MARK_TCORNER
, "white", "#808080")
113 EVT_STC_UPDATEUI(self
, ID
, self
.OnUpdateUI
)
114 EVT_STC_MARGINCLICK(self
, ID
, self
.OnMarginClick
)
115 EVT_KEY_DOWN(self
, self
.OnKeyPressed
)
118 # Make some styles, The lexer defines what each style is used for, we
119 # just have to define what each style looks like. This set is adapted from
120 # Scintilla sample property files.
122 # Global default styles for all languages
123 self
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "face:%(helv)s,size:%(size)d" % faces
)
124 self
.StyleClearAll() # Reset all to be like the default
126 self
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces
)
127 self
.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR
, "face:%(other)s" % faces
)
128 self
.StyleSetSpec(wxSTC_STYLE_BRACELIGHT
, "fore:#FFFFFF,back:#0000FF,bold")
129 self
.StyleSetSpec(wxSTC_STYLE_BRACEBAD
, "fore:#000000,back:#FF0000,bold")
133 self
.StyleSetSpec(wxSTC_P_DEFAULT
, "fore:#000000,face:%(helv)s,size:%(size)d" % faces
)
135 self
.StyleSetSpec(wxSTC_P_COMMENTLINE
, "fore:#007F00,face:%(other)s,size:%(size)d" % faces
)
137 self
.StyleSetSpec(wxSTC_P_NUMBER
, "fore:#007F7F,size:%(size)d" % faces
)
139 self
.StyleSetSpec(wxSTC_P_STRING
, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces
)
140 # Single quoted string
141 self
.StyleSetSpec(wxSTC_P_CHARACTER
, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces
)
143 self
.StyleSetSpec(wxSTC_P_WORD
, "fore:#00007F,bold,size:%(size)d" % faces
)
145 self
.StyleSetSpec(wxSTC_P_TRIPLE
, "fore:#7F0000,size:%(size)d" % faces
)
146 # Triple double quotes
147 self
.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE
, "fore:#7F0000,size:%(size)d" % faces
)
148 # Class name definition
149 self
.StyleSetSpec(wxSTC_P_CLASSNAME
, "fore:#0000FF,bold,underline,size:%(size)d" % faces
)
150 # Function or method name definition
151 self
.StyleSetSpec(wxSTC_P_DEFNAME
, "fore:#007F7F,bold,size:%(size)d" % faces
)
153 self
.StyleSetSpec(wxSTC_P_OPERATOR
, "bold,size:%(size)d" % faces
)
155 self
.StyleSetSpec(wxSTC_P_IDENTIFIER
, "fore:#000000,face:%(helv)s,size:%(size)d" % faces
)
157 self
.StyleSetSpec(wxSTC_P_COMMENTBLOCK
, "fore:#7F7F7F,size:%(size)d" % faces
)
158 # End of line where string is not closed
159 self
.StyleSetSpec(wxSTC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces
)
161 self
.SetCaretForeground("BLUE")
164 # register some images for use in the AutoComplete box.
165 self
.RegisterImage(1, images
.getSmilesBitmap())
166 self
.RegisterImage(2, images
.getFile1Bitmap())
167 self
.RegisterImage(3, images
.getCopy2Bitmap())
172 def OnKeyPressed(self
, event
):
173 if self
.CallTipActive():
175 key
= event
.KeyCode()
176 if key
== 32 and event
.ControlDown():
177 pos
= self
.GetCurrentPos()
179 if event
.ShiftDown():
180 self
.CallTipSetBackground("yellow")
181 self
.CallTipShow(pos
, 'lots of of text: blah, blah, blah\n\n'
182 'show some suff, maybe parameters..\n\n'
183 'fubar(param1, param2)')
187 #for x in range(50000):
188 # lst.append('%05d' % x)
191 #self.AutoCompShow(0, st)
193 kw
= keyword
.kwlist
[:]
194 kw
.append("zzzzzz?2")
196 kw
.append("__init__?3")
197 kw
.append("zzaaaaa?2")
198 kw
.append("zzbaaaa?2")
199 kw
.append("this_is_a_longer_value")
200 #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value")
202 kw
.sort() # Python sorts are case sensitive
203 self
.AutoCompSetIgnoreCase(False) # so this needs to match
205 # Images are specified with a appended "?type"
206 for i
in range(len(kw
)):
207 if kw
[i
] in keyword
.kwlist
:
210 self
.AutoCompShow(0, " ".join(kw
))
215 def OnUpdateUI(self
, evt
):
216 # check for matching braces
220 caretPos
= self
.GetCurrentPos()
222 charBefore
= self
.GetCharAt(caretPos
- 1)
223 styleBefore
= self
.GetStyleAt(caretPos
- 1)
226 if charBefore
and chr(charBefore
) in "[]{}()" and styleBefore
== wxSTC_P_OPERATOR
:
227 braceAtCaret
= caretPos
- 1
231 charAfter
= self
.GetCharAt(caretPos
)
232 styleAfter
= self
.GetStyleAt(caretPos
)
233 if charAfter
and chr(charAfter
) in "[]{}()" and styleAfter
== wxSTC_P_OPERATOR
:
234 braceAtCaret
= caretPos
236 if braceAtCaret
>= 0:
237 braceOpposite
= self
.BraceMatch(braceAtCaret
)
239 if braceAtCaret
!= -1 and braceOpposite
== -1:
240 self
.BraceBadLight(braceAtCaret
)
242 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
243 #pt = self.PointFromPosition(braceOpposite)
244 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
249 def OnMarginClick(self
, evt
):
250 # fold and unfold as needed
251 if evt
.GetMargin() == 2:
252 if evt
.GetShift() and evt
.GetControl():
255 lineClicked
= self
.LineFromPosition(evt
.GetPosition())
256 if self
.GetFoldLevel(lineClicked
) & wxSTC_FOLDLEVELHEADERFLAG
:
258 self
.SetFoldExpanded(lineClicked
, True)
259 self
.Expand(lineClicked
, True, True, 1)
260 elif evt
.GetControl():
261 if self
.GetFoldExpanded(lineClicked
):
262 self
.SetFoldExpanded(lineClicked
, False)
263 self
.Expand(lineClicked
, False, True, 0)
265 self
.SetFoldExpanded(lineClicked
, True)
266 self
.Expand(lineClicked
, True, True, 100)
268 self
.ToggleFold(lineClicked
)
272 lineCount
= self
.GetLineCount()
275 # find out if we are folding or unfolding
276 for lineNum
in range(lineCount
):
277 if self
.GetFoldLevel(lineNum
) & wxSTC_FOLDLEVELHEADERFLAG
:
278 expanding
= not self
.GetFoldExpanded(lineNum
)
282 while lineNum
< lineCount
:
283 level
= self
.GetFoldLevel(lineNum
)
284 if level
& wxSTC_FOLDLEVELHEADERFLAG
and \
285 (level
& wxSTC_FOLDLEVELNUMBERMASK
) == wxSTC_FOLDLEVELBASE
:
288 self
.SetFoldExpanded(lineNum
, True)
289 lineNum
= self
.Expand(lineNum
, True)
290 lineNum
= lineNum
- 1
292 lastChild
= self
.GetLastChild(lineNum
, -1)
293 self
.SetFoldExpanded(lineNum
, False)
294 if lastChild
> lineNum
:
295 self
.HideLines(lineNum
+1, lastChild
)
297 lineNum
= lineNum
+ 1
301 def Expand(self
, line
, doExpand
, force
=False, visLevels
=0, level
=-1):
302 lastChild
= self
.GetLastChild(line
, level
)
304 while line
<= lastChild
:
307 self
.ShowLines(line
, line
)
309 self
.HideLines(line
, line
)
312 self
.ShowLines(line
, line
)
315 level
= self
.GetFoldLevel(line
)
317 if level
& wxSTC_FOLDLEVELHEADERFLAG
:
320 self
.SetFoldExpanded(line
, True)
322 self
.SetFoldExpanded(line
, False)
323 line
= self
.Expand(line
, doExpand
, force
, visLevels
-1)
326 if doExpand
and self
.GetFoldExpanded(line
):
327 line
= self
.Expand(line
, True, force
, visLevels
-1)
329 line
= self
.Expand(line
, False, force
, visLevels
-1)
336 #----------------------------------------------------------------------
340 def runTest(frame
, nb
, log
):
342 ed
= p
= PythonSTC(nb
, -1)
344 p
= wxPanel(nb
, -1, style
= wxNO_FULL_REPAINT_ON_RESIZE
)
345 ed
= PythonSTC(p
, -1)
346 s
= wxBoxSizer(wxHORIZONTAL
)
347 s
.Add(ed
, 1, wxEXPAND
)
349 p
.SetAutoLayout(True)
352 ed
.SetText(demoText
+ open('Main.py').read())
356 # line numbers in the margin
357 ed
.SetMarginType(1, wxSTC_MARGIN_NUMBER
)
358 ed
.SetMarginWidth(1, 25)
364 #----------------------------------------------------------------------
369 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
370 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
376 if __name__
== '__main__':
379 run
.main(['', os
.path
.basename(sys
.argv
[0])])
385 #----------------------------------------------------------------------
386 #----------------------------------------------------------------------