]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/wxStyledTextCtrl_2.py
First draft of a cygwin script to create wxMSW distributions
[wxWidgets.git] / wxPython / demo / wxStyledTextCtrl_2.py
CommitLineData
f6bcfd97
BP
1
2from wxPython.wx import *
3from wxPython.stc import *
4
5import keyword
6
7#----------------------------------------------------------------------
8
9demoText = """\
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.
12
13
14"""
15
16#----------------------------------------------------------------------
17
18
19if wxPlatform == '__WXMSW__':
20 faces = { 'times': 'Times New Roman',
21 'mono' : 'Courier New',
22 'helv' : 'Arial',
23 'other': 'Comic Sans MS',
24 'size' : 8,
25 'size2': 6,
26 }
27else:
28 faces = { 'times': 'Times',
29 'mono' : 'Courier',
30 'helv' : 'Helvetica',
31 'other': 'new century schoolbook',
32 'size' : 11,
33 'size2': 9,
34 }
35
36
37#----------------------------------------------------------------------
38
39class PythonSTC(wxStyledTextCtrl):
40 def __init__(self, parent, ID):
41 wxStyledTextCtrl.__init__(self, parent, ID)
42
43 self.SetLexer(wxSTC_LEX_PYTHON)
44 self.SetKeywords(0, string.join(keyword.kwlist))
45
46 self.SetProperty("fold", "1")
47 self.SetProperty("tab.timmy.whinge.level", "1")
48 self.SetMargins(0,0)
49
50 self.SetViewWhitespace(false)
51 #self.SetBufferedDraw(false)
52
53 self.SetEdgeMode(wxSTC_EDGE_BACKGROUND)
54 self.SetEdgeColumn(78)
55
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")
64
65
66 EVT_STC_UPDATEUI(self, ID, self.OnUpdateUI)
67 EVT_STC_MARGINCLICK(self, ID, self.OnMarginClick)
68
69
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.
73
74 self.StyleClearAll()
75
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")
82
83 # Python styles
84 # White space
85 self.StyleSetSpec(SCE_P_DEFAULT, "fore:#808080")
86 # Comment
87 self.StyleSetSpec(SCE_P_COMMENTLINE, "fore:#007F00,face:%(other)s" % faces)
88 # Number
89 self.StyleSetSpec(SCE_P_NUMBER, "fore:#007F7F")
90 # String
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)
94 # Keyword
95 self.StyleSetSpec(SCE_P_WORD, "fore:#00007F,bold")
96 # Triple quotes
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")
104 # Operators
105 self.StyleSetSpec(SCE_P_OPERATOR, "bold")
106 # Identifiers
107 #self.StyleSetSpec(SCE_P_IDENTIFIER, "bold")#,fore:#FF00FF")
108 # Comment-blocks
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)
112
113
114 self.SetCaretForeground("BLUE")
115
116 EVT_KEY_UP(self, self.OnKeyPressed)
117
118
119 def OnKeyPressed(self, event):
120 key = event.KeyCode()
121 if key == 32 and event.ControlDown():
122 pos = self.GetCurrentPos()
123 # Tips
124 if event.ShiftDown():
125 self.CallTipSetBackground("yellow")
126 self.CallTipShow(pos, 'param1, param2')
127 # Code completion
128 else:
129 self.AutoCompShow('I love wxPython a b c')
130
131
132
133 def OnUpdateUI(self, evt):
134 # check for matching braces
135 braceAtCaret = -1
136 braceOpposite = -1
137 charBefore = None
138 caretPos = self.GetCurrentPos()
139 if caretPos > 0:
140 charBefore = self.GetCharAt(caretPos - 1)
141 styleBefore = self.GetStyleAt(caretPos - 1)
142
143 # check before
144 if charBefore and charBefore in "[]{}()" and ord(styleBefore) == SCE_P_OPERATOR:
145 braceAtCaret = caretPos - 1
146
147 # check after
148 if braceAtCaret < 0:
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
153
154 if braceAtCaret >= 0:
155 braceOpposite = self.BraceMatch(braceAtCaret)
156
157 if braceAtCaret != -1 and braceOpposite == -1:
158 self.BraceBadlight(braceAtCaret)
159 else:
160 self.BraceHighlight(braceAtCaret, braceOpposite)
161 #pt = self.PointFromPosition(braceOpposite)
162 #self.Refresh(true, wxRect(pt.x, pt.y, 5,5))
163 #print pt
164 #self.Refresh(false)
165
166
167 def OnMarginClick(self, evt):
168 # fold and unfold as needed
169 if evt.GetMargin() == 2:
170 if evt.GetShift() and evt.GetControl():
171 self.FoldAll()
172 else:
173 lineClicked = self.GetLineFromPos(evt.GetPosition())
174 if self.GetFoldLevel(lineClicked) & wxSTC_FOLDLEVELHEADERFLAG:
175 if evt.GetShift():
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)
182 else:
183 self.SetFoldExpanded(lineClicked, true)
184 self.Expand(lineClicked, true, true, 100)
185 else:
186 self.ToggleFold(lineClicked)
187
188
189 def FoldAll(self):
190 lineCount = self.GetLineCount()
191 expanding = true
192
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)
197 break;
198
199 lineNum = 0
200 while lineNum < lineCount:
201 level = self.GetFoldLevel(lineNum)
202 if level & wxSTC_FOLDLEVELHEADERFLAG and \
203 (level & wxSTC_FOLDLEVELNUMBERMASK) == wxSTC_FOLDLEVELBASE:
204
205 if expanding:
206 self.SetFoldExpanded(lineNum, true)
207 lineNum = self.Expand(lineNum, true)
208 lineNum = lineNum - 1
209 else:
210 lastChild = self.GetLastChild(lineNum, -1)
211 self.SetFoldExpanded(lineNum, false)
212 if lastChild > lineNum:
213 self.HideLines(lineNum+1, lastChild)
214
215 lineNum = lineNum + 1
216
217
218
219 def Expand(self, line, doExpand, force=false, visLevels=0, level=-1):
220 lastChild = self.GetLastChild(line, level)
221 line = line + 1
222 while line <= lastChild:
223 if force:
224 if visLevels > 0:
225 self.ShowLines(line, line)
226 else:
227 self.HideLines(line, line)
228 else:
229 if doExpand:
230 self.ShowLines(line, line)
231
232 if level == -1:
233 level = self.GetFoldLevel(line)
234
235 if level & wxSTC_FOLDLEVELHEADERFLAG:
236 if force:
237 if visLevels > 1:
238 self.SetFoldExpanded(line, true)
239 else:
240 self.SetFoldExpanded(line, false)
241 line = self.Expand(line, doExpand, force, visLevels-1)
242
243 else:
244 if doExpand and self.GetFoldExpanded(line):
245 line = self.Expand(line, true, force, visLevels-1)
246 else:
247 line = self.Expand(line, false, force, visLevels-1)
248 else:
249 line = line + 1;
250
251 return line
252
253
254#----------------------------------------------------------------------
255
256def runTest(frame, nb, log):
257 ed = PythonSTC(nb, -1)
258
259 ed.SetText(demoText + open('Main.py').read())
260 ed.EmptyUndoBuffer()
261
262
263 # line numbers in the margin
264 ed.SetMarginType(1, wxSTC_MARGIN_NUMBER)
265 ed.SetMarginWidth(1, 25)
266
267 return ed
268
269
270
271#----------------------------------------------------------------------
272
273
274overview = """\
275<html><body>
276Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a>
277and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
278be helpful.
279</body><html>
280"""
281
282
283if __name__ == '__main__':
284 import sys
285 app = wxPySimpleApp()
286 frame = wxFrame(None, -1, "Tester...", size=(640, 480))
287 win = runTest(frame, frame, sys.stdout)
288 frame.Show(true)
289 app.MainLoop()
290
291
292
293
294
295
296#----------------------------------------------------------------------
297#----------------------------------------------------------------------
298