]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | from wxPython.wx import * | |
3 | from wxPython.stc import * | |
4 | ||
5 | import keyword | |
6 | ||
7 | #---------------------------------------------------------------------- | |
8 | ||
9 | demoText = """\ | |
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 | ||
19 | if wxPlatform == '__WXMSW__': | |
20 | faces = { 'times': 'Times New Roman', | |
21 | 'mono' : 'Courier New', | |
22 | 'helv' : 'Arial', | |
23 | 'other': 'Comic Sans MS', | |
24 | 'size' : 10, | |
25 | 'size2': 8, | |
26 | } | |
27 | else: | |
28 | faces = { 'times': 'Times', | |
29 | 'mono' : 'Courier', | |
30 | 'helv' : 'Helvetica', | |
31 | 'other': 'new century schoolbook', | |
32 | 'size' : 13, | |
33 | 'size2': 11, | |
34 | } | |
35 | ||
36 | ||
37 | #---------------------------------------------------------------------- | |
38 | ||
39 | class PythonSTC(wxStyledTextCtrl): | |
40 | def __init__(self, parent, ID): | |
41 | wxStyledTextCtrl.__init__(self, parent, ID) | |
42 | ||
43 | self.CmdKeyAssign(ord('B'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMIN) | |
44 | self.CmdKeyAssign(ord('N'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMOUT) | |
45 | ||
46 | self.SetLexer(wxSTC_LEX_PYTHON) | |
47 | self.SetKeyWords(0, string.join(keyword.kwlist)) | |
48 | ||
49 | self.SetProperty("fold", "1") | |
50 | self.SetProperty("tab.timmy.whinge.level", "1") | |
51 | self.SetMargins(0,0) | |
52 | ||
53 | self.SetViewWhiteSpace(false) | |
54 | #self.SetBufferedDraw(false) | |
55 | ||
56 | self.SetEdgeMode(wxSTC_EDGE_BACKGROUND) | |
57 | self.SetEdgeColumn(78) | |
58 | ||
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, 12) | |
65 | ||
66 | if 0: # simple folder marks, like the old version | |
67 | self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_ARROW, "navy", "navy") | |
68 | self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, "navy", "navy") | |
69 | # Set these to an invisible mark | |
70 | self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BACKGROUND, "white", "black") | |
71 | self.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_BACKGROUND, "white", "black") | |
72 | self.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_BACKGROUND, "white", "black") | |
73 | self.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_BACKGROUND, "white", "black") | |
74 | ||
75 | else: # more involved "outlining" folder marks | |
76 | self.MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_BOXPLUSCONNECTED, "white", "black") | |
77 | self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BOXMINUSCONNECTED, "white", "black") | |
78 | self.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, "white", "black") | |
79 | self.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, "white", "black") | |
80 | self.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, "white", "black") | |
81 | self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUS, "white", "black") | |
82 | self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUS, "white", "black") | |
83 | ||
84 | ||
85 | EVT_STC_UPDATEUI(self, ID, self.OnUpdateUI) | |
86 | EVT_STC_MARGINCLICK(self, ID, self.OnMarginClick) | |
87 | ||
88 | ||
89 | # Make some styles, The lexer defines what each style is used for, we | |
90 | # just have to define what each style looks like. This set is adapted from | |
91 | # Scintilla sample property files. | |
92 | ||
93 | self.StyleClearAll() | |
94 | ||
95 | # Global default styles for all languages | |
96 | self.StyleSetSpec(wxSTC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces) | |
97 | self.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces) | |
98 | self.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR, "face:%(other)s" % faces) | |
99 | self.StyleSetSpec(wxSTC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold") | |
100 | self.StyleSetSpec(wxSTC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold") | |
101 | ||
102 | # Python styles | |
103 | # White space | |
104 | self.StyleSetSpec(wxSTC_P_DEFAULT, "fore:#808080,face:%(helv)s,size:%(size)d" % faces) | |
105 | # Comment | |
106 | self.StyleSetSpec(wxSTC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) | |
107 | # Number | |
108 | self.StyleSetSpec(wxSTC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces) | |
109 | # String | |
110 | self.StyleSetSpec(wxSTC_P_STRING, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % faces) | |
111 | # Single quoted string | |
112 | self.StyleSetSpec(wxSTC_P_CHARACTER, "fore:#7F007F,italic,face:%(times)s,size:%(size)d" % faces) | |
113 | # Keyword | |
114 | self.StyleSetSpec(wxSTC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces) | |
115 | # Triple quotes | |
116 | self.StyleSetSpec(wxSTC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % faces) | |
117 | # Triple double quotes | |
118 | self.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % faces) | |
119 | # Class name definition | |
120 | self.StyleSetSpec(wxSTC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % faces) | |
121 | # Function or method name definition | |
122 | self.StyleSetSpec(wxSTC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % faces) | |
123 | # Operators | |
124 | self.StyleSetSpec(wxSTC_P_OPERATOR, "bold,size:%(size)d" % faces) | |
125 | # Identifiers | |
126 | self.StyleSetSpec(wxSTC_P_IDENTIFIER, "fore:#808080,face:%(helv)s,size:%(size)d" % faces) | |
127 | # Comment-blocks | |
128 | self.StyleSetSpec(wxSTC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % faces) | |
129 | # End of line where string is not closed | |
130 | self.StyleSetSpec(wxSTC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces) | |
131 | ||
132 | ||
133 | self.SetCaretForeground("BLUE") | |
134 | ||
135 | EVT_KEY_DOWN(self, self.OnKeyPressed) | |
136 | ||
137 | ||
138 | def OnKeyPressed(self, event): | |
139 | if self.CallTipActive(): | |
140 | self.CallTipCancel() | |
141 | key = event.KeyCode() | |
142 | if key == 32 and event.ControlDown(): | |
143 | pos = self.GetCurrentPos() | |
144 | # Tips | |
145 | if event.ShiftDown(): | |
146 | self.CallTipSetBackground("yellow") | |
147 | self.CallTipShow(pos, 'param1, param2') | |
148 | # Code completion | |
149 | else: | |
150 | #lst = [] | |
151 | #for x in range(50000): | |
152 | # lst.append('%05d' % x) | |
153 | #st = string.join(lst) | |
154 | #print len(st) | |
155 | #self.AutoCompShow(0, st) | |
156 | ||
157 | kw = keyword.kwlist[:] | |
158 | kw.append("zzzzzz") | |
159 | kw.append("aaaaa") | |
160 | kw.append("__init__") | |
161 | kw.append("zzaaaaa") | |
162 | kw.append("zzbaaaa") | |
163 | kw.append("this_is_a_longer_value") | |
164 | kw.append("this_is_a_much_much_much_much_much_much_much_longer_value") | |
165 | ||
166 | kw.sort() # Python sorts are case sensitive | |
167 | self.AutoCompSetIgnoreCase(false) # so this needs to match | |
168 | ||
169 | self.AutoCompShow(0, string.join(kw)) | |
170 | else: | |
171 | event.Skip() | |
172 | ||
173 | ||
174 | def OnUpdateUI(self, evt): | |
175 | # check for matching braces | |
176 | braceAtCaret = -1 | |
177 | braceOpposite = -1 | |
178 | charBefore = None | |
179 | caretPos = self.GetCurrentPos() | |
180 | if caretPos > 0: | |
181 | charBefore = self.GetCharAt(caretPos - 1) | |
182 | styleBefore = self.GetStyleAt(caretPos - 1) | |
183 | ||
184 | # check before | |
185 | if charBefore and chr(charBefore) in "[]{}()" and styleBefore == wxSTC_P_OPERATOR: | |
186 | braceAtCaret = caretPos - 1 | |
187 | ||
188 | # check after | |
189 | if braceAtCaret < 0: | |
190 | charAfter = self.GetCharAt(caretPos) | |
191 | styleAfter = self.GetStyleAt(caretPos) | |
192 | if charAfter and chr(charAfter) in "[]{}()" and styleAfter == wxSTC_P_OPERATOR: | |
193 | braceAtCaret = caretPos | |
194 | ||
195 | if braceAtCaret >= 0: | |
196 | braceOpposite = self.BraceMatch(braceAtCaret) | |
197 | ||
198 | if braceAtCaret != -1 and braceOpposite == -1: | |
199 | self.BraceBadLight(braceAtCaret) | |
200 | else: | |
201 | self.BraceHighlight(braceAtCaret, braceOpposite) | |
202 | #pt = self.PointFromPosition(braceOpposite) | |
203 | #self.Refresh(true, wxRect(pt.x, pt.y, 5,5)) | |
204 | #print pt | |
205 | #self.Refresh(false) | |
206 | ||
207 | ||
208 | def OnMarginClick(self, evt): | |
209 | # fold and unfold as needed | |
210 | if evt.GetMargin() == 2: | |
211 | if evt.GetShift() and evt.GetControl(): | |
212 | self.FoldAll() | |
213 | else: | |
214 | lineClicked = self.LineFromPosition(evt.GetPosition()) | |
215 | if self.GetFoldLevel(lineClicked) & wxSTC_FOLDLEVELHEADERFLAG: | |
216 | if evt.GetShift(): | |
217 | self.SetFoldExpanded(lineClicked, true) | |
218 | self.Expand(lineClicked, true, true, 1) | |
219 | elif evt.GetControl(): | |
220 | if self.GetFoldExpanded(lineClicked): | |
221 | self.SetFoldExpanded(lineClicked, false) | |
222 | self.Expand(lineClicked, false, true, 0) | |
223 | else: | |
224 | self.SetFoldExpanded(lineClicked, true) | |
225 | self.Expand(lineClicked, true, true, 100) | |
226 | else: | |
227 | self.ToggleFold(lineClicked) | |
228 | ||
229 | ||
230 | def FoldAll(self): | |
231 | lineCount = self.GetLineCount() | |
232 | expanding = true | |
233 | ||
234 | # find out if we are folding or unfolding | |
235 | for lineNum in range(lineCount): | |
236 | if self.GetFoldLevel(lineNum) & wxSTC_FOLDLEVELHEADERFLAG: | |
237 | expanding = not self.GetFoldExpanded(lineNum) | |
238 | break; | |
239 | ||
240 | lineNum = 0 | |
241 | while lineNum < lineCount: | |
242 | level = self.GetFoldLevel(lineNum) | |
243 | if level & wxSTC_FOLDLEVELHEADERFLAG and \ | |
244 | (level & wxSTC_FOLDLEVELNUMBERMASK) == wxSTC_FOLDLEVELBASE: | |
245 | ||
246 | if expanding: | |
247 | self.SetFoldExpanded(lineNum, true) | |
248 | lineNum = self.Expand(lineNum, true) | |
249 | lineNum = lineNum - 1 | |
250 | else: | |
251 | lastChild = self.GetLastChild(lineNum, -1) | |
252 | self.SetFoldExpanded(lineNum, false) | |
253 | if lastChild > lineNum: | |
254 | self.HideLines(lineNum+1, lastChild) | |
255 | ||
256 | lineNum = lineNum + 1 | |
257 | ||
258 | ||
259 | ||
260 | def Expand(self, line, doExpand, force=false, visLevels=0, level=-1): | |
261 | lastChild = self.GetLastChild(line, level) | |
262 | line = line + 1 | |
263 | while line <= lastChild: | |
264 | if force: | |
265 | if visLevels > 0: | |
266 | self.ShowLines(line, line) | |
267 | else: | |
268 | self.HideLines(line, line) | |
269 | else: | |
270 | if doExpand: | |
271 | self.ShowLines(line, line) | |
272 | ||
273 | if level == -1: | |
274 | level = self.GetFoldLevel(line) | |
275 | ||
276 | if level & wxSTC_FOLDLEVELHEADERFLAG: | |
277 | if force: | |
278 | if visLevels > 1: | |
279 | self.SetFoldExpanded(line, true) | |
280 | else: | |
281 | self.SetFoldExpanded(line, false) | |
282 | line = self.Expand(line, doExpand, force, visLevels-1) | |
283 | ||
284 | else: | |
285 | if doExpand and self.GetFoldExpanded(line): | |
286 | line = self.Expand(line, true, force, visLevels-1) | |
287 | else: | |
288 | line = self.Expand(line, false, force, visLevels-1) | |
289 | else: | |
290 | line = line + 1; | |
291 | ||
292 | return line | |
293 | ||
294 | ||
295 | #---------------------------------------------------------------------- | |
296 | ||
297 | _USE_PANEL = 1 | |
298 | ||
299 | def runTest(frame, nb, log): | |
300 | if not _USE_PANEL: | |
301 | ed = p = PythonSTC(nb, -1) | |
302 | else: | |
303 | p = wxPanel(nb, -1) | |
304 | ed = PythonSTC(p, -1) | |
305 | s = wxBoxSizer(wxHORIZONTAL) | |
306 | s.Add(ed, 1, wxEXPAND) | |
307 | p.SetSizer(s) | |
308 | p.SetAutoLayout(true) | |
309 | ||
310 | ||
311 | ed.SetText(demoText + open('Main.py').read()) | |
312 | ed.EmptyUndoBuffer() | |
313 | ed.Colourise(0, -1) | |
314 | ||
315 | # line numbers in the margin | |
316 | ed.SetMarginType(1, wxSTC_MARGIN_NUMBER) | |
317 | ed.SetMarginWidth(1, 25) | |
318 | ||
319 | return p | |
320 | ||
321 | ||
322 | ||
323 | #---------------------------------------------------------------------- | |
324 | ||
325 | ||
326 | overview = """\ | |
327 | <html><body> | |
328 | Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a> | |
329 | and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should | |
330 | be helpful. | |
331 | </body><html> | |
332 | """ | |
333 | ||
334 | ||
335 | if __name__ == '__main__': | |
336 | import sys | |
337 | app = wxPySimpleApp() | |
338 | frame = wxFrame(None, -1, "Tester...", size=(640, 480)) | |
339 | win = runTest(frame, frame, sys.stdout) | |
340 | frame.Show(true) | |
341 | app.MainLoop() | |
342 | ||
343 | ||
344 | ||
345 | ||
346 | ||
347 | ||
348 | #---------------------------------------------------------------------- | |
349 | #---------------------------------------------------------------------- | |
350 |