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