]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxStyledTextCtrl_2.py
mention ShouldInheritColours() change
[wxWidgets.git] / wxPython / demo / wxStyledTextCtrl_2.py
1
2 from wxPython.wx import *
3 from wxPython.stc import *
4 import images
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' : 12,
33 'size2': 10,
34 }
35
36
37 #----------------------------------------------------------------------
38
39 class PythonSTC(wxStyledTextCtrl):
40
41 fold_symbols = 2
42
43 def __init__(self, parent, ID):
44 wxStyledTextCtrl.__init__(self, parent, ID,
45 style = wxNO_FULL_REPAINT_ON_RESIZE)
46
47 self.CmdKeyAssign(ord('B'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMIN)
48 self.CmdKeyAssign(ord('N'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMOUT)
49
50 self.SetLexer(wxSTC_LEX_PYTHON)
51 self.SetKeyWords(0, " ".join(keyword.kwlist))
52
53 self.SetProperty("fold", "1")
54 self.SetProperty("tab.timmy.whinge.level", "1")
55 self.SetMargins(0,0)
56
57 self.SetViewWhiteSpace(False)
58 #self.SetBufferedDraw(False)
59 #self.SetViewEOL(True)
60
61 self.SetEdgeMode(wxSTC_EDGE_BACKGROUND)
62 self.SetEdgeColumn(78)
63
64 # Setup a margin to hold fold markers
65 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
66 self.SetMarginType(2, wxSTC_MARGIN_SYMBOL)
67 self.SetMarginMask(2, wxSTC_MASK_FOLDERS)
68 self.SetMarginSensitive(2, True)
69 self.SetMarginWidth(2, 12)
70
71
72 if self.fold_symbols == 0:
73 # Arrow pointing right for contracted folders, arrow pointing down for expanded
74 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_ARROWDOWN, "black", "black");
75 self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_ARROW, "black", "black");
76 self.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, "black", "black");
77 self.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, "black", "black");
78 self.MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_EMPTY, "white", "black");
79 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_EMPTY, "white", "black");
80 self.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, "white", "black");
81
82 elif self.fold_symbols == 1:
83 # Plus for contracted folders, minus for expanded
84 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_MINUS, "white", "black");
85 self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_PLUS, "white", "black");
86 self.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_EMPTY, "white", "black");
87 self.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_EMPTY, "white", "black");
88 self.MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_EMPTY, "white", "black");
89 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_EMPTY, "white", "black");
90 self.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_EMPTY, "white", "black");
91
92 elif self.fold_symbols == 2:
93 # Like a flattened tree control using circular headers and curved joins
94 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_CIRCLEMINUS, "white", "#404040");
95 self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_CIRCLEPLUS, "white", "#404040");
96 self.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, "white", "#404040");
97 self.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNERCURVE, "white", "#404040");
98 self.MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_CIRCLEPLUSCONNECTED, "white", "#404040");
99 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_CIRCLEMINUSCONNECTED, "white", "#404040");
100 self.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNERCURVE, "white", "#404040");
101
102 elif self.fold_symbols == 3:
103 # Like a flattened tree control using square headers
104 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPEN, wxSTC_MARK_BOXMINUS, "white", "#808080")
105 self.MarkerDefine(wxSTC_MARKNUM_FOLDER, wxSTC_MARK_BOXPLUS, "white", "#808080")
106 self.MarkerDefine(wxSTC_MARKNUM_FOLDERSUB, wxSTC_MARK_VLINE, "white", "#808080")
107 self.MarkerDefine(wxSTC_MARKNUM_FOLDERTAIL, wxSTC_MARK_LCORNER, "white", "#808080")
108 self.MarkerDefine(wxSTC_MARKNUM_FOLDEREND, wxSTC_MARK_BOXPLUSCONNECTED, "white", "#808080")
109 self.MarkerDefine(wxSTC_MARKNUM_FOLDEROPENMID, wxSTC_MARK_BOXMINUSCONNECTED, "white", "#808080")
110 self.MarkerDefine(wxSTC_MARKNUM_FOLDERMIDTAIL, wxSTC_MARK_TCORNER, "white", "#808080")
111
112
113 EVT_STC_UPDATEUI(self, ID, self.OnUpdateUI)
114 EVT_STC_MARGINCLICK(self, ID, self.OnMarginClick)
115 EVT_KEY_DOWN(self, self.OnKeyPressed)
116
117
118 # Make some styles, The lexer defines what each style is used for, we
119 # just have to define what each style looks like. This set is adapted from
120 # Scintilla sample property files.
121
122 # Global default styles for all languages
123 self.StyleSetSpec(wxSTC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
124 self.StyleClearAll() # Reset all to be like the default
125
126 self.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
127 self.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
128 self.StyleSetSpec(wxSTC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold")
129 self.StyleSetSpec(wxSTC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
130
131 # Python styles
132 # Default
133 self.StyleSetSpec(wxSTC_P_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces)
134 # Comments
135 self.StyleSetSpec(wxSTC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces)
136 # Number
137 self.StyleSetSpec(wxSTC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces)
138 # String
139 self.StyleSetSpec(wxSTC_P_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces)
140 # Single quoted string
141 self.StyleSetSpec(wxSTC_P_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces)
142 # Keyword
143 self.StyleSetSpec(wxSTC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces)
144 # Triple quotes
145 self.StyleSetSpec(wxSTC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % faces)
146 # Triple double quotes
147 self.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % faces)
148 # Class name definition
149 self.StyleSetSpec(wxSTC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % faces)
150 # Function or method name definition
151 self.StyleSetSpec(wxSTC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % faces)
152 # Operators
153 self.StyleSetSpec(wxSTC_P_OPERATOR, "bold,size:%(size)d" % faces)
154 # Identifiers
155 self.StyleSetSpec(wxSTC_P_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces)
156 # Comment-blocks
157 self.StyleSetSpec(wxSTC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % faces)
158 # End of line where string is not closed
159 self.StyleSetSpec(wxSTC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces)
160
161 self.SetCaretForeground("BLUE")
162
163
164 # register some images for use in the AutoComplete box.
165 self.RegisterImage(1, images.getSmilesBitmap())
166 self.RegisterImage(2, images.getFile1Bitmap())
167 self.RegisterImage(3, images.getCopy2Bitmap())
168
169
170
171
172 def OnKeyPressed(self, event):
173 if self.CallTipActive():
174 self.CallTipCancel()
175 key = event.KeyCode()
176 if key == 32 and event.ControlDown():
177 pos = self.GetCurrentPos()
178 # Tips
179 if event.ShiftDown():
180 self.CallTipSetBackground("yellow")
181 self.CallTipShow(pos, 'lots of of text: blah, blah, blah\n\n'
182 'show some suff, maybe parameters..\n\n'
183 'fubar(param1, param2)')
184 # Code completion
185 else:
186 #lst = []
187 #for x in range(50000):
188 # lst.append('%05d' % x)
189 #st = " ".join(lst)
190 #print len(st)
191 #self.AutoCompShow(0, st)
192
193 kw = keyword.kwlist[:]
194 kw.append("zzzzzz?2")
195 kw.append("aaaaa?2")
196 kw.append("__init__?3")
197 kw.append("zzaaaaa?2")
198 kw.append("zzbaaaa?2")
199 kw.append("this_is_a_longer_value")
200 #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value")
201
202 kw.sort() # Python sorts are case sensitive
203 self.AutoCompSetIgnoreCase(False) # so this needs to match
204
205 # Images are specified with a appended "?type"
206 for i in range(len(kw)):
207 if kw[i] in keyword.kwlist:
208 kw[i] = kw[i] + "?1"
209
210 self.AutoCompShow(0, " ".join(kw))
211 else:
212 event.Skip()
213
214
215 def OnUpdateUI(self, evt):
216 # check for matching braces
217 braceAtCaret = -1
218 braceOpposite = -1
219 charBefore = None
220 caretPos = self.GetCurrentPos()
221 if caretPos > 0:
222 charBefore = self.GetCharAt(caretPos - 1)
223 styleBefore = self.GetStyleAt(caretPos - 1)
224
225 # check before
226 if charBefore and chr(charBefore) in "[]{}()" and styleBefore == wxSTC_P_OPERATOR:
227 braceAtCaret = caretPos - 1
228
229 # check after
230 if braceAtCaret < 0:
231 charAfter = self.GetCharAt(caretPos)
232 styleAfter = self.GetStyleAt(caretPos)
233 if charAfter and chr(charAfter) in "[]{}()" and styleAfter == wxSTC_P_OPERATOR:
234 braceAtCaret = caretPos
235
236 if braceAtCaret >= 0:
237 braceOpposite = self.BraceMatch(braceAtCaret)
238
239 if braceAtCaret != -1 and braceOpposite == -1:
240 self.BraceBadLight(braceAtCaret)
241 else:
242 self.BraceHighlight(braceAtCaret, braceOpposite)
243 #pt = self.PointFromPosition(braceOpposite)
244 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
245 #print pt
246 #self.Refresh(False)
247
248
249 def OnMarginClick(self, evt):
250 # fold and unfold as needed
251 if evt.GetMargin() == 2:
252 if evt.GetShift() and evt.GetControl():
253 self.FoldAll()
254 else:
255 lineClicked = self.LineFromPosition(evt.GetPosition())
256 if self.GetFoldLevel(lineClicked) & wxSTC_FOLDLEVELHEADERFLAG:
257 if evt.GetShift():
258 self.SetFoldExpanded(lineClicked, True)
259 self.Expand(lineClicked, True, True, 1)
260 elif evt.GetControl():
261 if self.GetFoldExpanded(lineClicked):
262 self.SetFoldExpanded(lineClicked, False)
263 self.Expand(lineClicked, False, True, 0)
264 else:
265 self.SetFoldExpanded(lineClicked, True)
266 self.Expand(lineClicked, True, True, 100)
267 else:
268 self.ToggleFold(lineClicked)
269
270
271 def FoldAll(self):
272 lineCount = self.GetLineCount()
273 expanding = True
274
275 # find out if we are folding or unfolding
276 for lineNum in range(lineCount):
277 if self.GetFoldLevel(lineNum) & wxSTC_FOLDLEVELHEADERFLAG:
278 expanding = not self.GetFoldExpanded(lineNum)
279 break;
280
281 lineNum = 0
282 while lineNum < lineCount:
283 level = self.GetFoldLevel(lineNum)
284 if level & wxSTC_FOLDLEVELHEADERFLAG and \
285 (level & wxSTC_FOLDLEVELNUMBERMASK) == wxSTC_FOLDLEVELBASE:
286
287 if expanding:
288 self.SetFoldExpanded(lineNum, True)
289 lineNum = self.Expand(lineNum, True)
290 lineNum = lineNum - 1
291 else:
292 lastChild = self.GetLastChild(lineNum, -1)
293 self.SetFoldExpanded(lineNum, False)
294 if lastChild > lineNum:
295 self.HideLines(lineNum+1, lastChild)
296
297 lineNum = lineNum + 1
298
299
300
301 def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
302 lastChild = self.GetLastChild(line, level)
303 line = line + 1
304 while line <= lastChild:
305 if force:
306 if visLevels > 0:
307 self.ShowLines(line, line)
308 else:
309 self.HideLines(line, line)
310 else:
311 if doExpand:
312 self.ShowLines(line, line)
313
314 if level == -1:
315 level = self.GetFoldLevel(line)
316
317 if level & wxSTC_FOLDLEVELHEADERFLAG:
318 if force:
319 if visLevels > 1:
320 self.SetFoldExpanded(line, True)
321 else:
322 self.SetFoldExpanded(line, False)
323 line = self.Expand(line, doExpand, force, visLevels-1)
324
325 else:
326 if doExpand and self.GetFoldExpanded(line):
327 line = self.Expand(line, True, force, visLevels-1)
328 else:
329 line = self.Expand(line, False, force, visLevels-1)
330 else:
331 line = line + 1;
332
333 return line
334
335
336 #----------------------------------------------------------------------
337
338 _USE_PANEL = 1
339
340 def runTest(frame, nb, log):
341 if not _USE_PANEL:
342 ed = p = PythonSTC(nb, -1)
343 else:
344 p = wxPanel(nb, -1, style = wxNO_FULL_REPAINT_ON_RESIZE)
345 ed = PythonSTC(p, -1)
346 s = wxBoxSizer(wxHORIZONTAL)
347 s.Add(ed, 1, wxEXPAND)
348 p.SetSizer(s)
349 p.SetAutoLayout(True)
350
351
352 ed.SetText(demoText + open('Main.py').read())
353 ed.EmptyUndoBuffer()
354 ed.Colourise(0, -1)
355
356 # line numbers in the margin
357 ed.SetMarginType(1, wxSTC_MARGIN_NUMBER)
358 ed.SetMarginWidth(1, 25)
359
360 return p
361
362
363
364 #----------------------------------------------------------------------
365
366
367 overview = """\
368 <html><body>
369 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
370 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
371 be helpful.
372 </body><html>
373 """
374
375
376 if __name__ == '__main__':
377 import sys,os
378 import run
379 run.main(['', os.path.basename(sys.argv[0])])
380
381
382
383
384
385 #----------------------------------------------------------------------
386 #----------------------------------------------------------------------
387