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