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