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