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