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(SCE_P_DEFAULT
, "fore:#808080")
87 self
.StyleSetSpec(SCE_P_COMMENTLINE
, "fore:#007F00,face:%(other)s" % faces
)
89 self
.StyleSetSpec(SCE_P_NUMBER
, "fore:#007F7F")
91 self
.StyleSetSpec(SCE_P_STRING
, "fore:#7F007F,italic,face:%(times)s" % faces
)
92 # Single quoted string
93 self
.StyleSetSpec(SCE_P_CHARACTER
, "fore:#7F007F,italic,face:%(times)s" % faces
)
95 self
.StyleSetSpec(SCE_P_WORD
, "fore:#00007F,bold")
97 self
.StyleSetSpec(SCE_P_TRIPLE
, "fore:#7F0000")
98 # Triple double quotes
99 self
.StyleSetSpec(SCE_P_TRIPLEDOUBLE
, "fore:#7F0000")
100 # Class name definition
101 self
.StyleSetSpec(SCE_P_CLASSNAME
, "fore:#0000FF,bold,underline")
102 # Function or method name definition
103 self
.StyleSetSpec(SCE_P_DEFNAME
, "fore:#007F7F,bold")
105 self
.StyleSetSpec(SCE_P_OPERATOR
, "bold")
107 #self.StyleSetSpec(SCE_P_IDENTIFIER, "bold")#,fore:#FF00FF")
109 self
.StyleSetSpec(SCE_P_COMMENTBLOCK
, "fore:#7F7F7F")
110 # End of line where string is not closed
111 self
.StyleSetSpec(SCE_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')
129 self
.AutoCompShow('I love wxPython a b c')
133 def OnUpdateUI(self
, evt
):
134 # check for matching braces
138 caretPos
= self
.GetCurrentPos()
140 charBefore
= self
.GetCharAt(caretPos
- 1)
141 styleBefore
= self
.GetStyleAt(caretPos
- 1)
144 if charBefore
and charBefore
in "[]{}()" and ord(styleBefore
) == SCE_P_OPERATOR
:
145 braceAtCaret
= caretPos
- 1
149 charAfter
= self
.GetCharAt(caretPos
)
150 styleAfter
= self
.GetStyleAt(caretPos
)
151 if charAfter
and charAfter
in "[]{}()" and ord(styleAfter
) == SCE_P_OPERATOR
:
152 braceAtCaret
= caretPos
154 if braceAtCaret
>= 0:
155 braceOpposite
= self
.BraceMatch(braceAtCaret
)
157 if braceAtCaret
!= -1 and braceOpposite
== -1:
158 self
.BraceBadlight(braceAtCaret
)
160 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
161 #pt = self.PointFromPosition(braceOpposite)
162 #self.Refresh(true, wxRect(pt.x, pt.y, 5,5))
167 def OnMarginClick(self
, evt
):
168 # fold and unfold as needed
169 if evt
.GetMargin() == 2:
170 if evt
.GetShift() and evt
.GetControl():
173 lineClicked
= self
.GetLineFromPos(evt
.GetPosition())
174 if self
.GetFoldLevel(lineClicked
) & wxSTC_FOLDLEVELHEADERFLAG
:
176 self
.SetFoldExpanded(lineClicked
, true
)
177 self
.Expand(lineClicked
, true
, true
, 1)
178 elif evt
.GetControl():
179 if self
.GetFoldExpanded(lineClicked
):
180 self
.SetFoldExpanded(lineClicked
, false
)
181 self
.Expand(lineClicked
, false
, true
, 0)
183 self
.SetFoldExpanded(lineClicked
, true
)
184 self
.Expand(lineClicked
, true
, true
, 100)
186 self
.ToggleFold(lineClicked
)
190 lineCount
= self
.GetLineCount()
193 # find out if we are folding or unfolding
194 for lineNum
in range(lineCount
):
195 if self
.GetFoldLevel(lineNum
) & wxSTC_FOLDLEVELHEADERFLAG
:
196 expanding
= not self
.GetFoldExpanded(lineNum
)
200 while lineNum
< lineCount
:
201 level
= self
.GetFoldLevel(lineNum
)
202 if level
& wxSTC_FOLDLEVELHEADERFLAG
and \
203 (level
& wxSTC_FOLDLEVELNUMBERMASK
) == wxSTC_FOLDLEVELBASE
:
206 self
.SetFoldExpanded(lineNum
, true
)
207 lineNum
= self
.Expand(lineNum
, true
)
208 lineNum
= lineNum
- 1
210 lastChild
= self
.GetLastChild(lineNum
, -1)
211 self
.SetFoldExpanded(lineNum
, false
)
212 if lastChild
> lineNum
:
213 self
.HideLines(lineNum
+1, lastChild
)
215 lineNum
= lineNum
+ 1
219 def Expand(self
, line
, doExpand
, force
=false
, visLevels
=0, level
=-1):
220 lastChild
= self
.GetLastChild(line
, level
)
222 while line
<= lastChild
:
225 self
.ShowLines(line
, line
)
227 self
.HideLines(line
, line
)
230 self
.ShowLines(line
, line
)
233 level
= self
.GetFoldLevel(line
)
235 if level
& wxSTC_FOLDLEVELHEADERFLAG
:
238 self
.SetFoldExpanded(line
, true
)
240 self
.SetFoldExpanded(line
, false
)
241 line
= self
.Expand(line
, doExpand
, force
, visLevels
-1)
244 if doExpand
and self
.GetFoldExpanded(line
):
245 line
= self
.Expand(line
, true
, force
, visLevels
-1)
247 line
= self
.Expand(line
, false
, force
, visLevels
-1)
254 #----------------------------------------------------------------------
256 def runTest(frame
, nb
, log
):
257 ed
= PythonSTC(nb
, -1)
259 ed
.SetText(demoText
+ open('Main.py').read())
263 # line numbers in the margin
264 ed
.SetMarginType(1, wxSTC_MARGIN_NUMBER
)
265 ed
.SetMarginWidth(1, 25)
271 #----------------------------------------------------------------------
276 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a>
277 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
283 if __name__
== '__main__':
285 app
= wxPySimpleApp()
286 frame
= wxFrame(None, -1, "Tester...", size
=(640, 480))
287 win
= runTest(frame
, frame
, sys
.stdout
)
296 #----------------------------------------------------------------------
297 #----------------------------------------------------------------------