]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/StyledTextCtrl_2.py
More demo conversion and cleanup from Jeff
[wxWidgets.git] / wxPython / demo / StyledTextCtrl_2.py
1
2 import keyword
3
4 import wx
5 import wx.stc as stc
6
7 import images
8
9 #----------------------------------------------------------------------
10
11 demoText = """\
12 ## This version of the editor has been set up to edit Python source
13 ## code. Here is a copy of wxPython/demo/Main.py to play with.
14
15
16 """
17
18 #----------------------------------------------------------------------
19
20
21 if wx.Platform == '__WXMSW__':
22 faces = { 'times': 'Times New Roman',
23 'mono' : 'Courier New',
24 'helv' : 'Arial',
25 'other': 'Comic Sans MS',
26 'size' : 10,
27 'size2': 8,
28 }
29 else:
30 faces = { 'times': 'Times',
31 'mono' : 'Courier',
32 'helv' : 'Helvetica',
33 'other': 'new century schoolbook',
34 'size' : 12,
35 'size2': 10,
36 }
37
38
39 #----------------------------------------------------------------------
40
41 class PythonSTC(stc.StyledTextCtrl):
42
43 fold_symbols = 2
44
45 def __init__(self, parent, ID):
46 stc.StyledTextCtrl.__init__(self, parent, ID,
47 style = wx.NO_FULL_REPAINT_ON_RESIZE)
48
49 self.CmdKeyAssign(ord('B'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN)
50 self.CmdKeyAssign(ord('N'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMOUT)
51
52 self.SetLexer(stc.STC_LEX_PYTHON)
53 self.SetKeyWords(0, " ".join(keyword.kwlist))
54
55 self.SetProperty("fold", "1")
56 self.SetProperty("tab.timmy.whinge.level", "1")
57 self.SetMargins(0,0)
58
59 self.SetViewWhiteSpace(False)
60 #self.SetBufferedDraw(False)
61 #self.SetViewEOL(True)
62
63 self.SetEdgeMode(stc.STC_EDGE_BACKGROUND)
64 self.SetEdgeColumn(78)
65
66 # Setup a margin to hold fold markers
67 #self.SetFoldFlags(16) ### WHAT IS THIS VALUE? WHAT ARE THE OTHER FLAGS? DOES IT MATTER?
68 self.SetMarginType(2, stc.STC_MARGIN_SYMBOL)
69 self.SetMarginMask(2, stc.STC_MASK_FOLDERS)
70 self.SetMarginSensitive(2, True)
71 self.SetMarginWidth(2, 12)
72
73 if self.fold_symbols == 0:
74 # Arrow pointing right for contracted folders, arrow pointing down for expanded
75 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_ARROWDOWN, "black", "black");
76 self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_ARROW, "black", "black");
77 self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_EMPTY, "black", "black");
78 self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_EMPTY, "black", "black");
79 self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_EMPTY, "white", "black");
80 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY, "white", "black");
81 self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY, "white", "black");
82
83 elif self.fold_symbols == 1:
84 # Plus for contracted folders, minus for expanded
85 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_MINUS, "white", "black");
86 self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_PLUS, "white", "black");
87 self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_EMPTY, "white", "black");
88 self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_EMPTY, "white", "black");
89 self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_EMPTY, "white", "black");
90 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_EMPTY, "white", "black");
91 self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_EMPTY, "white", "black");
92
93 elif self.fold_symbols == 2:
94 # Like a flattened tree control using circular headers and curved joins
95 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_CIRCLEMINUS, "white", "#404040");
96 self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_CIRCLEPLUS, "white", "#404040");
97 self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE, "white", "#404040");
98 self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNERCURVE, "white", "#404040");
99 self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_CIRCLEPLUSCONNECTED, "white", "#404040");
100 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_CIRCLEMINUSCONNECTED, "white", "#404040");
101 self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNERCURVE, "white", "#404040");
102
103 elif self.fold_symbols == 3:
104 # Like a flattened tree control using square headers
105 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPEN, stc.STC_MARK_BOXMINUS, "white", "#808080")
106 self.MarkerDefine(stc.STC_MARKNUM_FOLDER, stc.STC_MARK_BOXPLUS, "white", "#808080")
107 self.MarkerDefine(stc.STC_MARKNUM_FOLDERSUB, stc.STC_MARK_VLINE, "white", "#808080")
108 self.MarkerDefine(stc.STC_MARKNUM_FOLDERTAIL, stc.STC_MARK_LCORNER, "white", "#808080")
109 self.MarkerDefine(stc.STC_MARKNUM_FOLDEREND, stc.STC_MARK_BOXPLUSCONNECTED, "white", "#808080")
110 self.MarkerDefine(stc.STC_MARKNUM_FOLDEROPENMID, stc.STC_MARK_BOXMINUSCONNECTED, "white", "#808080")
111 self.MarkerDefine(stc.STC_MARKNUM_FOLDERMIDTAIL, stc.STC_MARK_TCORNER, "white", "#808080")
112
113
114 self.Bind(stc.EVT_STC_UPDATEUI, self.OnUpdateUI)
115 self.Bind(stc.EVT_STC_MARGINCLICK, self.OnMarginClick)
116 self.Bind(wx.EVT_KEY_DOWN, self.OnKeyPressed)
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(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
124 self.StyleClearAll() # Reset all to be like the default
125
126 # Global default styles for all languages
127 self.StyleSetSpec(stc.STC_STYLE_DEFAULT, "face:%(helv)s,size:%(size)d" % faces)
128 self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(helv)s,size:%(size2)d" % faces)
129 self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, "face:%(other)s" % faces)
130 self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, "fore:#FFFFFF,back:#0000FF,bold")
131 self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, "fore:#000000,back:#FF0000,bold")
132
133 # Python styles
134 # Default
135 self.StyleSetSpec(stc.STC_P_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces)
136 # Comments
137 self.StyleSetSpec(stc.STC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces)
138 # Number
139 self.StyleSetSpec(stc.STC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces)
140 # String
141 self.StyleSetSpec(stc.STC_P_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces)
142 # Single quoted string
143 self.StyleSetSpec(stc.STC_P_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces)
144 # Keyword
145 self.StyleSetSpec(stc.STC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces)
146 # Triple quotes
147 self.StyleSetSpec(stc.STC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % faces)
148 # Triple double quotes
149 self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % faces)
150 # Class name definition
151 self.StyleSetSpec(stc.STC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % faces)
152 # Function or method name definition
153 self.StyleSetSpec(stc.STC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % faces)
154 # Operators
155 self.StyleSetSpec(stc.STC_P_OPERATOR, "bold,size:%(size)d" % faces)
156 # Identifiers
157 self.StyleSetSpec(stc.STC_P_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces)
158 # Comment-blocks
159 self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % faces)
160 # End of line where string is not closed
161 self.StyleSetSpec(stc.STC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces)
162
163 self.SetCaretForeground("BLUE")
164
165
166 # register some images for use in the AutoComplete box.
167 self.RegisterImage(1, images.getSmilesBitmap())
168 self.RegisterImage(2, images.getFile1Bitmap())
169 self.RegisterImage(3, images.getCopy2Bitmap())
170
171
172 def OnKeyPressed(self, event):
173 if self.CallTipActive():
174 self.CallTipCancel()
175 key = event.KeyCode()
176
177 if key == 32 and event.ControlDown():
178 pos = self.GetCurrentPos()
179
180 # Tips
181 if event.ShiftDown():
182 self.CallTipSetBackground("yellow")
183 self.CallTipShow(pos, 'lots of of text: blah, blah, blah\n\n'
184 'show some suff, maybe parameters..\n\n'
185 'fubar(param1, param2)')
186 # Code completion
187 else:
188 #lst = []
189 #for x in range(50000):
190 # lst.append('%05d' % x)
191 #st = " ".join(lst)
192 #print len(st)
193 #self.AutoCompShow(0, st)
194
195 kw = keyword.kwlist[:]
196 kw.append("zzzzzz?2")
197 kw.append("aaaaa?2")
198 kw.append("__init__?3")
199 kw.append("zzaaaaa?2")
200 kw.append("zzbaaaa?2")
201 kw.append("this_is_a_longer_value")
202 #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value")
203
204 kw.sort() # Python sorts are case sensitive
205 self.AutoCompSetIgnoreCase(False) # so this needs to match
206
207 # Images are specified with a appended "?type"
208 for i in range(len(kw)):
209 if kw[i] in keyword.kwlist:
210 kw[i] = kw[i] + "?1"
211
212 self.AutoCompShow(0, " ".join(kw))
213 else:
214 event.Skip()
215
216
217 def OnUpdateUI(self, evt):
218 # check for matching braces
219 braceAtCaret = -1
220 braceOpposite = -1
221 charBefore = None
222 caretPos = self.GetCurrentPos()
223
224 if caretPos > 0:
225 charBefore = self.GetCharAt(caretPos - 1)
226 styleBefore = self.GetStyleAt(caretPos - 1)
227
228 # check before
229 if charBefore and chr(charBefore) in "[]{}()" and styleBefore == stc.STC_P_OPERATOR:
230 braceAtCaret = caretPos - 1
231
232 # check after
233 if braceAtCaret < 0:
234 charAfter = self.GetCharAt(caretPos)
235 styleAfter = self.GetStyleAt(caretPos)
236
237 if charAfter and chr(charAfter) in "[]{}()" and styleAfter == stc.STC_P_OPERATOR:
238 braceAtCaret = caretPos
239
240 if braceAtCaret >= 0:
241 braceOpposite = self.BraceMatch(braceAtCaret)
242
243 if braceAtCaret != -1 and braceOpposite == -1:
244 self.BraceBadLight(braceAtCaret)
245 else:
246 self.BraceHighlight(braceAtCaret, braceOpposite)
247 #pt = self.PointFromPosition(braceOpposite)
248 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
249 #print pt
250 #self.Refresh(False)
251
252
253 def OnMarginClick(self, evt):
254 # fold and unfold as needed
255 if evt.GetMargin() == 2:
256 if evt.GetShift() and evt.GetControl():
257 self.FoldAll()
258 else:
259 lineClicked = self.LineFromPosition(evt.GetPosition())
260
261 if self.GetFoldLevel(lineClicked) & stc.STC_FOLDLEVELHEADERFLAG:
262 if evt.GetShift():
263 self.SetFoldExpanded(lineClicked, True)
264 self.Expand(lineClicked, True, True, 1)
265 elif evt.GetControl():
266 if self.GetFoldExpanded(lineClicked):
267 self.SetFoldExpanded(lineClicked, False)
268 self.Expand(lineClicked, False, True, 0)
269 else:
270 self.SetFoldExpanded(lineClicked, True)
271 self.Expand(lineClicked, True, True, 100)
272 else:
273 self.ToggleFold(lineClicked)
274
275
276 def FoldAll(self):
277 lineCount = self.GetLineCount()
278 expanding = True
279
280 # find out if we are folding or unfolding
281 for lineNum in range(lineCount):
282 if self.GetFoldLevel(lineNum) & stc.STC_FOLDLEVELHEADERFLAG:
283 expanding = not self.GetFoldExpanded(lineNum)
284 break;
285
286 lineNum = 0
287
288 while lineNum < lineCount:
289 level = self.GetFoldLevel(lineNum)
290 if level & stc.STC_FOLDLEVELHEADERFLAG and \
291 (level & stc.STC_FOLDLEVELNUMBERMASK) == stc.STC_FOLDLEVELBASE:
292
293 if expanding:
294 self.SetFoldExpanded(lineNum, True)
295 lineNum = self.Expand(lineNum, True)
296 lineNum = lineNum - 1
297 else:
298 lastChild = self.GetLastChild(lineNum, -1)
299 self.SetFoldExpanded(lineNum, False)
300
301 if lastChild > lineNum:
302 self.HideLines(lineNum+1, lastChild)
303
304 lineNum = lineNum + 1
305
306
307
308 def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
309 lastChild = self.GetLastChild(line, level)
310 line = line + 1
311
312 while line <= lastChild:
313 if force:
314 if visLevels > 0:
315 self.ShowLines(line, line)
316 else:
317 self.HideLines(line, line)
318 else:
319 if doExpand:
320 self.ShowLines(line, line)
321
322 if level == -1:
323 level = self.GetFoldLevel(line)
324
325 if level & stc.STC_FOLDLEVELHEADERFLAG:
326 if force:
327 if visLevels > 1:
328 self.SetFoldExpanded(line, True)
329 else:
330 self.SetFoldExpanded(line, False)
331
332 line = self.Expand(line, doExpand, force, visLevels-1)
333
334 else:
335 if doExpand and self.GetFoldExpanded(line):
336 line = self.Expand(line, True, force, visLevels-1)
337 else:
338 line = self.Expand(line, False, force, visLevels-1)
339 else:
340 line = line + 1;
341
342 return line
343
344
345 #----------------------------------------------------------------------
346
347 _USE_PANEL = 1
348
349 def runTest(frame, nb, log):
350 if not _USE_PANEL:
351 ed = p = PythonSTC(nb, -1)
352 else:
353 p = wx.Panel(nb, -1, style = wx.NO_FULL_REPAINT_ON_RESIZE)
354 ed = PythonSTC(p, -1)
355 s = wx.BoxSizer(wx.HORIZONTAL)
356 s.Add(ed, 1, wx.EXPAND)
357 p.SetSizer(s)
358 p.SetAutoLayout(True)
359
360
361 ed.SetText(demoText + open('Main.py').read())
362 ed.EmptyUndoBuffer()
363 ed.Colourise(0, -1)
364
365 # line numbers in the margin
366 ed.SetMarginType(1, stc.STC_MARGIN_NUMBER)
367 ed.SetMarginWidth(1, 25)
368
369 return p
370
371
372
373 #----------------------------------------------------------------------
374
375
376 overview = """\
377 <html><body>
378 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
379 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
380 be helpful.
381 </body><html>
382 """
383
384
385 if __name__ == '__main__':
386 import sys,os
387 import run
388 run.main(['', os.path.basename(sys.argv[0])])
389
390
391
392
393
394 #----------------------------------------------------------------------
395 #----------------------------------------------------------------------
396