1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
13 #----------------------------------------------------------------------
16 ## This version of the editor has been set up to edit Python source
17 ## code. Here is a copy of wxPython/demo/Main.py to play with.
22 #----------------------------------------------------------------------
25 if wx
.Platform
== '__WXMSW__':
26 faces
= { 'times': 'Times New Roman',
27 'mono' : 'Courier New',
29 'other': 'Comic Sans MS',
34 faces
= { 'times': 'Times',
37 'other': 'new century schoolbook',
43 #----------------------------------------------------------------------
45 class PythonSTC(stc
.StyledTextCtrl
):
49 def __init__(self
, parent
, ID
):
50 stc
.StyledTextCtrl
.__init
__(self
, parent
, ID
,
51 style
= wx
.NO_FULL_REPAINT_ON_RESIZE
)
53 self
.CmdKeyAssign(ord('B'), stc
.STC_SCMOD_CTRL
, stc
.STC_CMD_ZOOMIN
)
54 self
.CmdKeyAssign(ord('N'), stc
.STC_SCMOD_CTRL
, stc
.STC_CMD_ZOOMOUT
)
56 self
.SetLexer(stc
.STC_LEX_PYTHON
)
57 self
.SetKeyWords(0, " ".join(keyword
.kwlist
))
59 self
.SetProperty("fold", "1")
60 self
.SetProperty("tab.timmy.whinge.level", "1")
63 self
.SetViewWhiteSpace(False)
64 #self.SetBufferedDraw(False)
65 #self.SetViewEOL(True)
67 self
.SetEdgeMode(stc
.STC_EDGE_BACKGROUND
)
68 self
.SetEdgeColumn(78)
70 # Setup a margin to hold fold markers
71 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
72 self
.SetMarginType(2, stc
.STC_MARGIN_SYMBOL
)
73 self
.SetMarginMask(2, stc
.STC_MASK_FOLDERS
)
74 self
.SetMarginSensitive(2, True)
75 self
.SetMarginWidth(2, 12)
77 if self
.fold_symbols
== 0:
78 # Arrow pointing right for contracted folders, arrow pointing down for expanded
79 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
, stc
.STC_MARK_ARROWDOWN
, "black", "black");
80 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
, stc
.STC_MARK_ARROW
, "black", "black");
81 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
, stc
.STC_MARK_EMPTY
, "black", "black");
82 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
, stc
.STC_MARK_EMPTY
, "black", "black");
83 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
, stc
.STC_MARK_EMPTY
, "white", "black");
84 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_EMPTY
, "white", "black");
85 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_EMPTY
, "white", "black");
87 elif self
.fold_symbols
== 1:
88 # Plus for contracted folders, minus for expanded
89 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
, stc
.STC_MARK_MINUS
, "white", "black");
90 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
, stc
.STC_MARK_PLUS
, "white", "black");
91 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
, stc
.STC_MARK_EMPTY
, "white", "black");
92 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
, stc
.STC_MARK_EMPTY
, "white", "black");
93 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
, stc
.STC_MARK_EMPTY
, "white", "black");
94 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_EMPTY
, "white", "black");
95 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_EMPTY
, "white", "black");
97 elif self
.fold_symbols
== 2:
98 # Like a flattened tree control using circular headers and curved joins
99 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
, stc
.STC_MARK_CIRCLEMINUS
, "white", "#404040");
100 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
, stc
.STC_MARK_CIRCLEPLUS
, "white", "#404040");
101 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
, stc
.STC_MARK_VLINE
, "white", "#404040");
102 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
, stc
.STC_MARK_LCORNERCURVE
, "white", "#404040");
103 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
, stc
.STC_MARK_CIRCLEPLUSCONNECTED
, "white", "#404040");
104 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_CIRCLEMINUSCONNECTED
, "white", "#404040");
105 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_TCORNERCURVE
, "white", "#404040");
107 elif self
.fold_symbols
== 3:
108 # Like a flattened tree control using square headers
109 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPEN
, stc
.STC_MARK_BOXMINUS
, "white", "#808080")
110 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDER
, stc
.STC_MARK_BOXPLUS
, "white", "#808080")
111 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERSUB
, stc
.STC_MARK_VLINE
, "white", "#808080")
112 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERTAIL
, stc
.STC_MARK_LCORNER
, "white", "#808080")
113 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEREND
, stc
.STC_MARK_BOXPLUSCONNECTED
, "white", "#808080")
114 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDEROPENMID
, stc
.STC_MARK_BOXMINUSCONNECTED
, "white", "#808080")
115 self
.MarkerDefine(stc
.STC_MARKNUM_FOLDERMIDTAIL
, stc
.STC_MARK_TCORNER
, "white", "#808080")
118 self
.Bind(stc
.EVT_STC_UPDATEUI
, self
.OnUpdateUI
)
119 self
.Bind(stc
.EVT_STC_MARGINCLICK
, self
.OnMarginClick
)
120 self
.Bind(wx
.EVT_KEY_DOWN
, self
.OnKeyPressed
)
122 # Make some styles, The lexer defines what each style is used for, we
123 # just have to define what each style looks like. This set is adapted from
124 # Scintilla sample property files.
126 # Global default styles for all languages
127 self
.StyleSetSpec(stc
.STC_STYLE_DEFAULT
, "face:%(helv)s,size:%(size)d" % faces
)
128 self
.StyleClearAll() # Reset all to be like the default
130 # Global default styles for all languages
131 self
.StyleSetSpec(stc
.STC_STYLE_DEFAULT
, "face:%(helv)s,size:%(size)d" % faces
)
132 self
.StyleSetSpec(stc
.STC_STYLE_LINENUMBER
, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces
)
133 self
.StyleSetSpec(stc
.STC_STYLE_CONTROLCHAR
, "face:%(other)s" % faces
)
134 self
.StyleSetSpec(stc
.STC_STYLE_BRACELIGHT
, "fore:#FFFFFF,back:#0000FF,bold")
135 self
.StyleSetSpec(stc
.STC_STYLE_BRACEBAD
, "fore:#000000,back:#FF0000,bold")
139 self
.StyleSetSpec(stc
.STC_P_DEFAULT
, "fore:#000000,face:%(helv)s,size:%(size)d" % faces
)
141 self
.StyleSetSpec(stc
.STC_P_COMMENTLINE
, "fore:#007F00,face:%(other)s,size:%(size)d" % faces
)
143 self
.StyleSetSpec(stc
.STC_P_NUMBER
, "fore:#007F7F,size:%(size)d" % faces
)
145 self
.StyleSetSpec(stc
.STC_P_STRING
, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces
)
146 # Single quoted string
147 self
.StyleSetSpec(stc
.STC_P_CHARACTER
, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces
)
149 self
.StyleSetSpec(stc
.STC_P_WORD
, "fore:#00007F,bold,size:%(size)d" % faces
)
151 self
.StyleSetSpec(stc
.STC_P_TRIPLE
, "fore:#7F0000,size:%(size)d" % faces
)
152 # Triple double quotes
153 self
.StyleSetSpec(stc
.STC_P_TRIPLEDOUBLE
, "fore:#7F0000,size:%(size)d" % faces
)
154 # Class name definition
155 self
.StyleSetSpec(stc
.STC_P_CLASSNAME
, "fore:#0000FF,bold,underline,size:%(size)d" % faces
)
156 # Function or method name definition
157 self
.StyleSetSpec(stc
.STC_P_DEFNAME
, "fore:#007F7F,bold,size:%(size)d" % faces
)
159 self
.StyleSetSpec(stc
.STC_P_OPERATOR
, "bold,size:%(size)d" % faces
)
161 self
.StyleSetSpec(stc
.STC_P_IDENTIFIER
, "fore:#000000,face:%(helv)s,size:%(size)d" % faces
)
163 self
.StyleSetSpec(stc
.STC_P_COMMENTBLOCK
, "fore:#7F7F7F,size:%(size)d" % faces
)
164 # End of line where string is not closed
165 self
.StyleSetSpec(stc
.STC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces
)
167 self
.SetCaretForeground("BLUE")
170 # register some images for use in the AutoComplete box.
171 self
.RegisterImage(1, images
.getSmilesBitmap())
172 self
.RegisterImage(2, images
.getFile1Bitmap())
173 self
.RegisterImage(3, images
.getCopy2Bitmap())
176 def OnKeyPressed(self
, event
):
177 if self
.CallTipActive():
179 key
= event
.KeyCode()
181 if key
== 32 and event
.ControlDown():
182 pos
= self
.GetCurrentPos()
185 if event
.ShiftDown():
186 self
.CallTipSetBackground("yellow")
187 self
.CallTipShow(pos
, 'lots of of text: blah, blah, blah\n\n'
188 'show some suff, maybe parameters..\n\n'
189 'fubar(param1, param2)')
193 #for x in range(50000):
194 # lst.append('%05d' % x)
197 #self.AutoCompShow(0, st)
199 kw
= keyword
.kwlist
[:]
200 kw
.append("zzzzzz?2")
202 kw
.append("__init__?3")
203 kw
.append("zzaaaaa?2")
204 kw
.append("zzbaaaa?2")
205 kw
.append("this_is_a_longer_value")
206 #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value")
208 kw
.sort() # Python sorts are case sensitive
209 self
.AutoCompSetIgnoreCase(False) # so this needs to match
211 # Images are specified with a appended "?type"
212 for i
in range(len(kw
)):
213 if kw
[i
] in keyword
.kwlist
:
216 self
.AutoCompShow(0, " ".join(kw
))
221 def OnUpdateUI(self
, evt
):
222 # check for matching braces
226 caretPos
= self
.GetCurrentPos()
229 charBefore
= self
.GetCharAt(caretPos
- 1)
230 styleBefore
= self
.GetStyleAt(caretPos
- 1)
233 if charBefore
and chr(charBefore
) in "[]{}()" and styleBefore
== stc
.STC_P_OPERATOR
:
234 braceAtCaret
= caretPos
- 1
238 charAfter
= self
.GetCharAt(caretPos
)
239 styleAfter
= self
.GetStyleAt(caretPos
)
241 if charAfter
and chr(charAfter
) in "[]{}()" and styleAfter
== stc
.STC_P_OPERATOR
:
242 braceAtCaret
= caretPos
244 if braceAtCaret
>= 0:
245 braceOpposite
= self
.BraceMatch(braceAtCaret
)
247 if braceAtCaret
!= -1 and braceOpposite
== -1:
248 self
.BraceBadLight(braceAtCaret
)
250 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
251 #pt = self.PointFromPosition(braceOpposite)
252 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
257 def OnMarginClick(self
, evt
):
258 # fold and unfold as needed
259 if evt
.GetMargin() == 2:
260 if evt
.GetShift() and evt
.GetControl():
263 lineClicked
= self
.LineFromPosition(evt
.GetPosition())
265 if self
.GetFoldLevel(lineClicked
) & stc
.STC_FOLDLEVELHEADERFLAG
:
267 self
.SetFoldExpanded(lineClicked
, True)
268 self
.Expand(lineClicked
, True, True, 1)
269 elif evt
.GetControl():
270 if self
.GetFoldExpanded(lineClicked
):
271 self
.SetFoldExpanded(lineClicked
, False)
272 self
.Expand(lineClicked
, False, True, 0)
274 self
.SetFoldExpanded(lineClicked
, True)
275 self
.Expand(lineClicked
, True, True, 100)
277 self
.ToggleFold(lineClicked
)
281 lineCount
= self
.GetLineCount()
284 # find out if we are folding or unfolding
285 for lineNum
in range(lineCount
):
286 if self
.GetFoldLevel(lineNum
) & stc
.STC_FOLDLEVELHEADERFLAG
:
287 expanding
= not self
.GetFoldExpanded(lineNum
)
292 while lineNum
< lineCount
:
293 level
= self
.GetFoldLevel(lineNum
)
294 if level
& stc
.STC_FOLDLEVELHEADERFLAG
and \
295 (level
& stc
.STC_FOLDLEVELNUMBERMASK
) == stc
.STC_FOLDLEVELBASE
:
298 self
.SetFoldExpanded(lineNum
, True)
299 lineNum
= self
.Expand(lineNum
, True)
300 lineNum
= lineNum
- 1
302 lastChild
= self
.GetLastChild(lineNum
, -1)
303 self
.SetFoldExpanded(lineNum
, False)
305 if lastChild
> lineNum
:
306 self
.HideLines(lineNum
+1, lastChild
)
308 lineNum
= lineNum
+ 1
312 def Expand(self
, line
, doExpand
, force
=False, visLevels
=0, level
=-1):
313 lastChild
= self
.GetLastChild(line
, level
)
316 while line
<= lastChild
:
319 self
.ShowLines(line
, line
)
321 self
.HideLines(line
, line
)
324 self
.ShowLines(line
, line
)
327 level
= self
.GetFoldLevel(line
)
329 if level
& stc
.STC_FOLDLEVELHEADERFLAG
:
332 self
.SetFoldExpanded(line
, True)
334 self
.SetFoldExpanded(line
, False)
336 line
= self
.Expand(line
, doExpand
, force
, visLevels
-1)
339 if doExpand
and self
.GetFoldExpanded(line
):
340 line
= self
.Expand(line
, True, force
, visLevels
-1)
342 line
= self
.Expand(line
, False, force
, visLevels
-1)
349 #----------------------------------------------------------------------
353 def runTest(frame
, nb
, log
):
355 ed
= p
= PythonSTC(nb
, -1)
357 p
= wx
.Panel(nb
, -1, style
= wx
.NO_FULL_REPAINT_ON_RESIZE
)
358 ed
= PythonSTC(p
, -1)
359 s
= wx
.BoxSizer(wx
.HORIZONTAL
)
360 s
.Add(ed
, 1, wx
.EXPAND
)
362 p
.SetAutoLayout(True)
365 ed
.SetText(demoText
+ open('Main.py').read())
369 # line numbers in the margin
370 ed
.SetMarginType(1, stc
.STC_MARGIN_NUMBER
)
371 ed
.SetMarginWidth(1, 25)
377 #----------------------------------------------------------------------
382 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
383 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
389 if __name__
== '__main__':
392 run
.main(['', os
.path
.basename(sys
.argv
[0])])
398 #----------------------------------------------------------------------
399 #----------------------------------------------------------------------