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
.CmdKeyAssign(ord('B'), wxSTC_SCMOD_CTRL
, wxSTC_CMD_ZOOMIN
)
44 self
.CmdKeyAssign(ord('N'), wxSTC_SCMOD_CTRL
, wxSTC_CMD_ZOOMOUT
)
46 self
.SetLexer(wxSTC_LEX_PYTHON
)
47 self
.SetKeyWords(0, string
.join(keyword
.kwlist
))
49 self
.SetProperty("fold", "1")
50 self
.SetProperty("tab.timmy.whinge.level", "1")
53 self
.SetViewWhiteSpace(false
)
54 #self.SetBufferedDraw(false)
56 self
.SetEdgeMode(wxSTC_EDGE_BACKGROUND
)
57 self
.SetEdgeColumn(78)
59 # Setup a margin to hold fold markers
60 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
61 self
.SetMarginType(2, wxSTC_MARGIN_SYMBOL
)
62 self
.SetMarginMask(2, wxSTC_MASK_FOLDERS
)
63 self
.SetMarginSensitive(2, true
)
64 self
.SetMarginWidth(2, 15)
65 self
.MarkerDefine(wxSTC_MARKNUM_FOLDER
, wxSTC_MARK_ARROW
, "navy", "navy")
66 self
.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN
, wxSTC_MARK_ARROWDOWN
, "navy", "navy")
69 EVT_STC_UPDATEUI(self
, ID
, self
.OnUpdateUI
)
70 EVT_STC_MARGINCLICK(self
, ID
, self
.OnMarginClick
)
73 # Make some styles, The lexer defines what each style is used for, we
74 # just have to define what each style looks like. This set is adapted from
75 # Scintilla sample property files.
79 # Global default styles for all languages
80 self
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "face:%(helv)s,size:%(size)d" % faces
)
81 self
.StyleSetSpec(wxSTC_STYLE_LINENUMBER
, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces
)
82 self
.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR
, "face:%(other)s" % faces
)
83 self
.StyleSetSpec(wxSTC_STYLE_BRACELIGHT
, "fore:#FFFFFF,back:#0000FF,bold")
84 self
.StyleSetSpec(wxSTC_STYLE_BRACEBAD
, "fore:#000000,back:#FF0000,bold")
88 self
.StyleSetSpec(wxSTC_P_DEFAULT
, "fore:#808080,face:%(helv)s,size:%(size)d" % faces
)
90 self
.StyleSetSpec(wxSTC_P_COMMENTLINE
, "fore:#007F00,face:%(other)s,size:%(size)d" % faces
)
92 self
.StyleSetSpec(wxSTC_P_NUMBER
, "fore:#007F7F,size:%(size)d" % faces
)
94 self
.StyleSetSpec(wxSTC_P_STRING
, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % faces
)
95 # Single quoted string
96 self
.StyleSetSpec(wxSTC_P_CHARACTER
, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % faces
)
98 self
.StyleSetSpec(wxSTC_P_WORD
, "fore:#00007F,bold,size:%(size)d" % faces
)
100 self
.StyleSetSpec(wxSTC_P_TRIPLE
, "fore:#7F0000,size:%(size)d" % faces
)
101 # Triple double quotes
102 self
.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE
, "fore:#7F0000,size:%(size)d" % faces
)
103 # Class name definition
104 self
.StyleSetSpec(wxSTC_P_CLASSNAME
, "fore:#0000FF,bold,underline,size:%(size)d" % faces
)
105 # Function or method name definition
106 self
.StyleSetSpec(wxSTC_P_DEFNAME
, "fore:#007F7F,bold,size:%(size)d" % faces
)
108 self
.StyleSetSpec(wxSTC_P_OPERATOR
, "bold,size:%(size)d" % faces
)
110 self
.StyleSetSpec(wxSTC_P_IDENTIFIER
, "fore:#808080,face:%(helv)s,size:%(size)d" % faces
)
112 self
.StyleSetSpec(wxSTC_P_COMMENTBLOCK
, "fore:#7F7F7F,size:%(size)d" % faces
)
113 # End of line where string is not closed
114 self
.StyleSetSpec(wxSTC_P_STRINGEOL
, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces
)
117 self
.SetCaretForeground("BLUE")
119 EVT_KEY_DOWN(self
, self
.OnKeyPressed
)
122 def OnKeyPressed(self
, event
):
123 key
= event
.KeyCode()
124 if key
== 32 and event
.ControlDown():
125 pos
= self
.GetCurrentPos()
127 if event
.ShiftDown():
128 self
.CallTipSetBackground("yellow")
129 self
.CallTipShow(pos
, 'param1, param2')
133 #for x in range(50000):
134 # lst.append('%05d' % x)
135 #st = string.join(lst)
137 #self.AutoCompShow(0, st)
139 kw
= keyword
.kwlist
[:]
142 kw
.append("__init__")
145 kw
.append("this_is_a_longer_value")
146 kw
.append("this_is_a_much_much_much_much_much_much_much_longer_value")
148 kw
.sort() # Python sorts are case sensitive
149 self
.AutoCompSetIgnoreCase(false
) # so this needs to match
151 self
.AutoCompShow(0, string
.join(kw
))
156 def OnUpdateUI(self
, evt
):
157 # check for matching braces
161 caretPos
= self
.GetCurrentPos()
163 charBefore
= self
.GetCharAt(caretPos
- 1)
164 styleBefore
= self
.GetStyleAt(caretPos
- 1)
167 if charBefore
and chr(charBefore
) in "[]{}()" and styleBefore
== wxSTC_P_OPERATOR
:
168 braceAtCaret
= caretPos
- 1
172 charAfter
= self
.GetCharAt(caretPos
)
173 styleAfter
= self
.GetStyleAt(caretPos
)
174 if charAfter
and chr(charAfter
) in "[]{}()" and styleAfter
== wxSTC_P_OPERATOR
:
175 braceAtCaret
= caretPos
177 if braceAtCaret
>= 0:
178 braceOpposite
= self
.BraceMatch(braceAtCaret
)
180 if braceAtCaret
!= -1 and braceOpposite
== -1:
181 self
.BraceBadLight(braceAtCaret
)
183 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
184 #pt = self.PointFromPosition(braceOpposite)
185 #self.Refresh(true, wxRect(pt.x, pt.y, 5,5))
190 def OnMarginClick(self
, evt
):
191 # fold and unfold as needed
192 if evt
.GetMargin() == 2:
193 if evt
.GetShift() and evt
.GetControl():
196 lineClicked
= self
.LineFromPosition(evt
.GetPosition())
197 if self
.GetFoldLevel(lineClicked
) & wxSTC_FOLDLEVELHEADERFLAG
:
199 self
.SetFoldExpanded(lineClicked
, true
)
200 self
.Expand(lineClicked
, true
, true
, 1)
201 elif evt
.GetControl():
202 if self
.GetFoldExpanded(lineClicked
):
203 self
.SetFoldExpanded(lineClicked
, false
)
204 self
.Expand(lineClicked
, false
, true
, 0)
206 self
.SetFoldExpanded(lineClicked
, true
)
207 self
.Expand(lineClicked
, true
, true
, 100)
209 self
.ToggleFold(lineClicked
)
213 lineCount
= self
.GetLineCount()
216 # find out if we are folding or unfolding
217 for lineNum
in range(lineCount
):
218 if self
.GetFoldLevel(lineNum
) & wxSTC_FOLDLEVELHEADERFLAG
:
219 expanding
= not self
.GetFoldExpanded(lineNum
)
223 while lineNum
< lineCount
:
224 level
= self
.GetFoldLevel(lineNum
)
225 if level
& wxSTC_FOLDLEVELHEADERFLAG
and \
226 (level
& wxSTC_FOLDLEVELNUMBERMASK
) == wxSTC_FOLDLEVELBASE
:
229 self
.SetFoldExpanded(lineNum
, true
)
230 lineNum
= self
.Expand(lineNum
, true
)
231 lineNum
= lineNum
- 1
233 lastChild
= self
.GetLastChild(lineNum
, -1)
234 self
.SetFoldExpanded(lineNum
, false
)
235 if lastChild
> lineNum
:
236 self
.HideLines(lineNum
+1, lastChild
)
238 lineNum
= lineNum
+ 1
242 def Expand(self
, line
, doExpand
, force
=false
, visLevels
=0, level
=-1):
243 lastChild
= self
.GetLastChild(line
, level
)
245 while line
<= lastChild
:
248 self
.ShowLines(line
, line
)
250 self
.HideLines(line
, line
)
253 self
.ShowLines(line
, line
)
256 level
= self
.GetFoldLevel(line
)
258 if level
& wxSTC_FOLDLEVELHEADERFLAG
:
261 self
.SetFoldExpanded(line
, true
)
263 self
.SetFoldExpanded(line
, false
)
264 line
= self
.Expand(line
, doExpand
, force
, visLevels
-1)
267 if doExpand
and self
.GetFoldExpanded(line
):
268 line
= self
.Expand(line
, true
, force
, visLevels
-1)
270 line
= self
.Expand(line
, false
, force
, visLevels
-1)
277 #----------------------------------------------------------------------
281 def runTest(frame
, nb
, log
):
283 ed
= p
= PythonSTC(nb
, -1)
286 ed
= PythonSTC(p
, -1)
287 s
= wxBoxSizer(wxHORIZONTAL
)
288 s
.Add(ed
, 1, wxEXPAND
)
290 p
.SetAutoLayout(true
)
293 ed
.SetText(demoText
+ open('Main.py').read())
297 # line numbers in the margin
298 ed
.SetMarginType(1, wxSTC_MARGIN_NUMBER
)
299 ed
.SetMarginWidth(1, 25)
305 #----------------------------------------------------------------------
310 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
311 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
317 if __name__
== '__main__':
319 app
= wxPySimpleApp()
320 frame
= wxFrame(None, -1, "Tester...", size
=(640, 480))
321 win
= runTest(frame
, frame
, sys
.stdout
)
330 #----------------------------------------------------------------------
331 #----------------------------------------------------------------------