]>
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' : 8, | |
25 | 'size2': 6, | |
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, 15) | |
65 | self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_ARROW, "navy", "navy") | |
66 | self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, "navy", "navy") | |
67 | ||
68 | ||
69 | EVT_STC_UPDATEUI(self, ID, self.OnUpdateUI) | |
70 | EVT_STC_MARGINCLICK(self, ID, self.OnMarginClick) | |
71 | ||
72 | ||
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. | |
76 | ||
77 | self.StyleClearAll() | |
78 | ||
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") | |
85 | ||
86 | # Python styles | |
87 | # White space | |
88 | self.StyleSetSpec(wxSTC_P_DEFAULT, "fore:#808080,face:%(helv)s,size:%(size)d" % faces) | |
89 | # Comment | |
90 | self.StyleSetSpec(wxSTC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces) | |
91 | # Number | |
92 | self.StyleSetSpec(wxSTC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces) | |
93 | # String | |
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) | |
97 | # Keyword | |
98 | self.StyleSetSpec(wxSTC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces) | |
99 | # Triple quotes | |
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) | |
107 | # Operators | |
108 | self.StyleSetSpec(wxSTC_P_OPERATOR, "bold,size:%(size)d" % faces) | |
109 | # Identifiers | |
110 | self.StyleSetSpec(wxSTC_P_IDENTIFIER, "fore:#808080,face:%(helv)s,size:%(size)d" % faces) | |
111 | # Comment-blocks | |
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,eolfilled,size:%(size)d" % faces) | |
115 | ||
116 | ||
117 | self.SetCaretForeground("BLUE") | |
118 | ||
119 | EVT_KEY_UP(self, self.OnKeyPressed) | |
120 | ||
121 | ||
122 | def OnKeyPressed(self, event): | |
123 | key = event.KeyCode() | |
124 | if key == 32 and event.ControlDown(): | |
125 | pos = self.GetCurrentPos() | |
126 | # Tips | |
127 | if event.ShiftDown(): | |
128 | self.CallTipSetBackground("yellow") | |
129 | self.CallTipShow(pos, 'param1, param2') | |
130 | # Code completion | |
131 | else: | |
132 | #lst = [] | |
133 | #for x in range(50000): | |
134 | # lst.append('%05d' % x) | |
135 | #st = string.join(lst) | |
136 | #print len(st) | |
137 | #self.AutoCompShow(0, st) | |
138 | self.AutoCompSetIgnoreCase(true) | |
139 | self.AutoCompShow(0, string.join(keyword.kwlist)) | |
140 | self.AutoCompSelect('br') | |
141 | else: | |
142 | event.Skip() | |
143 | ||
144 | ||
145 | def OnUpdateUI(self, evt): | |
146 | # check for matching braces | |
147 | braceAtCaret = -1 | |
148 | braceOpposite = -1 | |
149 | charBefore = None | |
150 | caretPos = self.GetCurrentPos() | |
151 | if caretPos > 0: | |
152 | charBefore = self.GetCharAt(caretPos - 1) | |
153 | styleBefore = self.GetStyleAt(caretPos - 1) | |
154 | ||
155 | # check before | |
156 | if charBefore and chr(charBefore) in "[]{}()" and styleBefore == wxSTC_P_OPERATOR: | |
157 | braceAtCaret = caretPos - 1 | |
158 | ||
159 | # check after | |
160 | if braceAtCaret < 0: | |
161 | charAfter = self.GetCharAt(caretPos) | |
162 | styleAfter = self.GetStyleAt(caretPos) | |
163 | if charAfter and chr(charAfter) in "[]{}()" and styleAfter == wxSTC_P_OPERATOR: | |
164 | braceAtCaret = caretPos | |
165 | ||
166 | if braceAtCaret >= 0: | |
167 | braceOpposite = self.BraceMatch(braceAtCaret) | |
168 | ||
169 | if braceAtCaret != -1 and braceOpposite == -1: | |
170 | self.BraceBadLight(braceAtCaret) | |
171 | else: | |
172 | self.BraceHighlight(braceAtCaret, braceOpposite) | |
173 | #pt = self.PointFromPosition(braceOpposite) | |
174 | #self.Refresh(true, wxRect(pt.x, pt.y, 5,5)) | |
175 | #print pt | |
176 | #self.Refresh(false) | |
177 | ||
178 | ||
179 | def OnMarginClick(self, evt): | |
180 | # fold and unfold as needed | |
181 | if evt.GetMargin() == 2: | |
182 | if evt.GetShift() and evt.GetControl(): | |
183 | self.FoldAll() | |
184 | else: | |
185 | lineClicked = self.LineFromPosition(evt.GetPosition()) | |
186 | if self.GetFoldLevel(lineClicked) & wxSTC_FOLDLEVELHEADERFLAG: | |
187 | if evt.GetShift(): | |
188 | self.SetFoldExpanded(lineClicked, true) | |
189 | self.Expand(lineClicked, true, true, 1) | |
190 | elif evt.GetControl(): | |
191 | if self.GetFoldExpanded(lineClicked): | |
192 | self.SetFoldExpanded(lineClicked, false) | |
193 | self.Expand(lineClicked, false, true, 0) | |
194 | else: | |
195 | self.SetFoldExpanded(lineClicked, true) | |
196 | self.Expand(lineClicked, true, true, 100) | |
197 | else: | |
198 | self.ToggleFold(lineClicked) | |
199 | ||
200 | ||
201 | def FoldAll(self): | |
202 | lineCount = self.GetLineCount() | |
203 | expanding = true | |
204 | ||
205 | # find out if we are folding or unfolding | |
206 | for lineNum in range(lineCount): | |
207 | if self.GetFoldLevel(lineNum) & wxSTC_FOLDLEVELHEADERFLAG: | |
208 | expanding = not self.GetFoldExpanded(lineNum) | |
209 | break; | |
210 | ||
211 | lineNum = 0 | |
212 | while lineNum < lineCount: | |
213 | level = self.GetFoldLevel(lineNum) | |
214 | if level & wxSTC_FOLDLEVELHEADERFLAG and \ | |
215 | (level & wxSTC_FOLDLEVELNUMBERMASK) == wxSTC_FOLDLEVELBASE: | |
216 | ||
217 | if expanding: | |
218 | self.SetFoldExpanded(lineNum, true) | |
219 | lineNum = self.Expand(lineNum, true) | |
220 | lineNum = lineNum - 1 | |
221 | else: | |
222 | lastChild = self.GetLastChild(lineNum, -1) | |
223 | self.SetFoldExpanded(lineNum, false) | |
224 | if lastChild > lineNum: | |
225 | self.HideLines(lineNum+1, lastChild) | |
226 | ||
227 | lineNum = lineNum + 1 | |
228 | ||
229 | ||
230 | ||
231 | def Expand(self, line, doExpand, force=false, visLevels=0, level=-1): | |
232 | lastChild = self.GetLastChild(line, level) | |
233 | line = line + 1 | |
234 | while line <= lastChild: | |
235 | if force: | |
236 | if visLevels > 0: | |
237 | self.ShowLines(line, line) | |
238 | else: | |
239 | self.HideLines(line, line) | |
240 | else: | |
241 | if doExpand: | |
242 | self.ShowLines(line, line) | |
243 | ||
244 | if level == -1: | |
245 | level = self.GetFoldLevel(line) | |
246 | ||
247 | if level & wxSTC_FOLDLEVELHEADERFLAG: | |
248 | if force: | |
249 | if visLevels > 1: | |
250 | self.SetFoldExpanded(line, true) | |
251 | else: | |
252 | self.SetFoldExpanded(line, false) | |
253 | line = self.Expand(line, doExpand, force, visLevels-1) | |
254 | ||
255 | else: | |
256 | if doExpand and self.GetFoldExpanded(line): | |
257 | line = self.Expand(line, true, force, visLevels-1) | |
258 | else: | |
259 | line = self.Expand(line, false, force, visLevels-1) | |
260 | else: | |
261 | line = line + 1; | |
262 | ||
263 | return line | |
264 | ||
265 | ||
266 | #---------------------------------------------------------------------- | |
267 | ||
268 | _USE_PANEL = 1 | |
269 | ||
270 | def runTest(frame, nb, log): | |
271 | if not _USE_PANEL: | |
272 | ed = p = PythonSTC(nb, -1) | |
273 | else: | |
274 | p = wxPanel(nb, -1) | |
275 | ed = PythonSTC(p, -1) | |
276 | s = wxBoxSizer(wxHORIZONTAL) | |
277 | s.Add(ed, 1, wxEXPAND) | |
278 | p.SetSizer(s) | |
279 | p.SetAutoLayout(true) | |
280 | ||
281 | ||
282 | ed.SetText(demoText + open('Main.py').read()) | |
283 | ed.EmptyUndoBuffer() | |
284 | ed.Colourise(0, -1) | |
285 | ||
286 | # line numbers in the margin | |
287 | ed.SetMarginType(1, wxSTC_MARGIN_NUMBER) | |
288 | ed.SetMarginWidth(1, 25) | |
289 | ||
290 | return p | |
291 | ||
292 | ||
293 | ||
294 | #---------------------------------------------------------------------- | |
295 | ||
296 | ||
297 | overview = """\ | |
298 | <html><body> | |
299 | Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h">this</a> | |
300 | and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should | |
301 | be helpful. | |
302 | </body><html> | |
303 | """ | |
304 | ||
305 | ||
306 | if __name__ == '__main__': | |
307 | import sys | |
308 | app = wxPySimpleApp() | |
309 | frame = wxFrame(None, -1, "Tester...", size=(640, 480)) | |
310 | win = runTest(frame, frame, sys.stdout) | |
311 | frame.Show(true) | |
312 | app.MainLoop() | |
313 | ||
314 | ||
315 | ||
316 | ||
317 | ||
318 | ||
319 | #---------------------------------------------------------------------- | |
320 | #---------------------------------------------------------------------- | |
321 |