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
)
43 self
.SetLexer(wxSTC_LEX_PYTHON
)
44 self
.SetKeyWords(0, string
.join(keyword
.kwlist
))
46 self
.SetProperty("fold", "1")
47 self
.SetProperty("tab.timmy.whinge.level", "1")
50 self
.SetViewWhiteSpace(false
)
51 #self.SetBufferedDraw(false)
53 self
.SetEdgeMode(wxSTC_EDGE_BACKGROUND
)
54 self
.SetEdgeColumn(78)
56 # Setup a margin to hold fold markers
57 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
58 self
.SetMarginType(2, wxSTC_MARGIN_SYMBOL
)
59 self
.SetMarginMask(2, wxSTC_MASK_FOLDERS
)
60 self
.SetMarginSensitive(2, true
)
61 self
.SetMarginWidth(2, 15)
62 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_ARROW
, "navy", "navy")
63 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_ARROWDOWN
, "navy", "navy")
66 EVT_STC_UPDATEUI(self
, ID
, self
.OnUpdateUI
)
67 EVT_STC_MARGINCLICK(self
, ID
, self
.OnMarginClick
)
70 # Make some styles, The lexer defines what each style is used for, we
71 # just have to define what each style looks like. This set is adapted from
72 # Scintilla sample property files.
76 # Global default styles for all languages
77 self
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "face:%(helv)s,size:%(size)d" % faces
)
78 self
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces
)
79 self
.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR
, "face:%(other)s" % faces
)
80 self
.StyleSetSpec(wxSTC_STYLE_BRACELIGHT
, "fore:#FFFFFF,back:#0000FF,bold")
81 self
.StyleSetSpec(wxSTC_STYLE_BRACEBAD
, "fore:#000000,back:#FF0000,bold")
85 self
.StyleSetSpec(wxSTC_P_DEFAULT
, "fore:#808080")
87 self
.StyleSetSpec(wxSTC_P_COMMENTLINE
, "fore:#007F00,face:%(other)s" % faces
)
89 self
.StyleSetSpec(wxSTC_P_NUMBER
, "fore:#007F7F")
91 self
.StyleSetSpec(wxSTC_P_STRING
, "fore:#7F007F,italic,face:%(times)s" % faces
)
92 # Single quoted string
93 self
.StyleSetSpec(wxSTC_P_CHARACTER
, "fore:#7F007F,italic,face:%(times)s" % faces
)
95 self
.StyleSetSpec(wxSTC_P_WORD
, "fore:#00007F,bold")
97 self
.StyleSetSpec(wxSTC_P_TRIPLE
, "fore:#7F0000")
98 # Triple double quotes
99 self
.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE
, "fore:#7F0000")
100 # Class name definition
101 self
.StyleSetSpec(wxSTC_P_CLASSNAME
, "fore:#0000FF,bold,underline")
102 # Function or method name definition
103 self
.StyleSetSpec(wxSTC_P_DEFNAME
, "fore:#007F7F,bold")
105 self
.StyleSetSpec(wxSTC_P_OPERATOR
, "bold")
107 #self.StyleSetSpec(wxSTC_P_IDENTIFIER, "bold")#,fore:#FF00FF")
109 self
.StyleSetSpec(wxSTC_P_COMMENTBLOCK
, "fore:#7F7F7F")
110 # End of line where string is not closed
111 self
.StyleSetSpec(wxSTC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces
)
114 self
.SetCaretForeground("BLUE")
116 EVT_KEY_UP(self
, self
.OnKeyPressed
)
119 def OnKeyPressed(self
, event
):
120 key
= event
.KeyCode()
121 if key
== 32 and event
.ControlDown():
122 pos
= self
.GetCurrentPos()
124 if event
.ShiftDown():
125 self
.CallTipSetBackground("yellow")
126 self
.CallTipShow(pos
, 'param1, param2')
130 #for x in range(50000):
131 # lst.append('%05d' % x)
132 #st = string.join(lst)
134 #self.AutoCompShow(0, st)
135 self
.AutoCompSetIgnoreCase(true
)
136 self
.AutoCompShow(0, string
.join(keyword
.kwlist
))
137 self
.AutoCompSelect('br')
142 def OnUpdateUI(self
, evt
):
143 # check for matching braces
147 caretPos
= self
.GetCurrentPos()
149 charBefore
= self
.GetCharAt(caretPos
- 1)
150 styleBefore
= self
.GetStyleAt(caretPos
- 1)
153 if charBefore
and chr(charBefore
) in "[]{}()" and styleBefore
== wxSTC_P_OPERATOR
:
154 braceAtCaret
= caretPos
- 1
158 charAfter
= self
.GetCharAt(caretPos
)
159 styleAfter
= self
.GetStyleAt(caretPos
)
160 if charAfter
and chr(charAfter
) in "[]{}()" and styleAfter
== wxSTC_P_OPERATOR
:
161 braceAtCaret
= caretPos
163 if braceAtCaret
>= 0:
164 braceOpposite
= self
.BraceMatch(braceAtCaret
)
166 if braceAtCaret
!= -1 and braceOpposite
== -1:
167 self
.BraceBadLight(braceAtCaret
)
169 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
170 #pt = self.PointFromPosition(braceOpposite)
171 #self.Refresh(true, wxRect(pt.x, pt.y, 5,5))
176 def OnMarginClick(self
, evt
):
177 # fold and unfold as needed
178 if evt
.GetMargin() == 2:
179 if evt
.GetShift() and evt
.GetControl():
182 lineClicked
= self
.LineFromPosition(evt
.GetPosition())
183 if self
.GetFoldLevel(lineClicked
) & wxSTC_FOLDLEVELHEADERFLAG
:
185 self
.SetFoldExpanded(lineClicked
, true
)
186 self
.Expand(lineClicked
, true
, true
, 1)
187 elif evt
.GetControl():
188 if self
.GetFoldExpanded(lineClicked
):
189 self
.SetFoldExpanded(lineClicked
, false
)
190 self
.Expand(lineClicked
, false
, true
, 0)
192 self
.SetFoldExpanded(lineClicked
, true
)
193 self
.Expand(lineClicked
, true
, true
, 100)
195 self
.ToggleFold(lineClicked
)
199 lineCount
= self
.GetLineCount()
202 # find out if we are folding or unfolding
203 for lineNum
in range(lineCount
):
204 if self
.GetFoldLevel(lineNum
) & wxSTC_FOLDLEVELHEADERFLAG
:
205 expanding
= not self
.GetFoldExpanded(lineNum
)
209 while lineNum
< lineCount
:
210 level
= self
.GetFoldLevel(lineNum
)
211 if level
& wxSTC_FOLDLEVELHEADERFLAG
and \
212 (level
& wxSTC_FOLDLEVELNUMBERMASK
) == wxSTC_FOLDLEVELBASE
:
215 self
.SetFoldExpanded(lineNum
, true
)
216 lineNum
= self
.Expand(lineNum
, true
)
217 lineNum
= lineNum
- 1
219 lastChild
= self
.GetLastChild(lineNum
, -1)
220 self
.SetFoldExpanded(lineNum
, false
)
221 if lastChild
> lineNum
:
222 self
.HideLines(lineNum
+1, lastChild
)
224 lineNum
= lineNum
+ 1
228 def Expand(self
, line
, doExpand
, force
=false
, visLevels
=0, level
=-1):
229 lastChild
= self
.GetLastChild(line
, level
)
231 while line
<= lastChild
:
234 self
.ShowLines(line
, line
)
236 self
.HideLines(line
, line
)
239 self
.ShowLines(line
, line
)
242 level
= self
.GetFoldLevel(line
)
244 if level
& wxSTC_FOLDLEVELHEADERFLAG
:
247 self
.SetFoldExpanded(line
, true
)
249 self
.SetFoldExpanded(line
, false
)
250 line
= self
.Expand(line
, doExpand
, force
, visLevels
-1)
253 if doExpand
and self
.GetFoldExpanded(line
):
254 line
= self
.Expand(line
, true
, force
, visLevels
-1)
256 line
= self
.Expand(line
, false
, force
, visLevels
-1)
263 #----------------------------------------------------------------------
265 def runTest(frame
, nb
, log
):
266 ed
= PythonSTC(nb
, -1)
268 ed
.SetText(demoText
+ open('Main.py').read())
272 # line numbers in the margin
273 ed
.SetMarginType(1, wxSTC_MARGIN_NUMBER
)
274 ed
.SetMarginWidth(1, 25)
280 #----------------------------------------------------------------------
285 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a>
286 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
292 if __name__
== '__main__':
294 app
= wxPySimpleApp()
295 frame
= wxFrame(None, -1, "Tester...", size
=(640, 480))
296 win
= runTest(frame
, frame
, sys
.stdout
)
305 #----------------------------------------------------------------------
306 #----------------------------------------------------------------------