]> git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxStyledTextCtrl_2.py
fixed enumerating of entries/groups under '/' in wxRegConfig
[wxWidgets.git] / wxPython / demo / wxStyledTextCtrl_2.py
1
2 from wxPython.wx import *
3 from wxPython.stc import *
4 import images
5 import keyword
6
7 #----------------------------------------------------------------------
8
9 demoText = """\
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 """
15
16 #----------------------------------------------------------------------
17
18
19 if wxPlatform == '__WXMSW__':
20 faces = { 'times': 'Times New Roman',
21 'mono' : 'Courier New',
22 'helv' : 'Arial',
23 'other': 'Comic Sans MS',
24 'size' : 10,
25 'size2': 8,
26 }
27 else:
28 faces = { 'times': 'Times',
29 'mono' : 'Courier',
30 'helv' : 'Helvetica',
31 'other': 'new century schoolbook',
32 'size' : 12,
33 'size2': 10,
34 }
35
36
37 #----------------------------------------------------------------------
38
39 class PythonSTC(wxStyledTextCtrl):
40 def __init__(self, parent, ID):
41 wxStyledTextCtrl.__init__(self, parent, ID,
42 style = wxNO_FULL_REPAINT_ON_RESIZE)
43
44 self.CmdKeyAssign(ord('B'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMIN)
45 self.CmdKeyAssign(ord('N'), wxSTC_SCMOD_CTRL, wxSTC_CMD_ZOOMOUT)
46
47 self.SetLexer(wxSTC_LEX_PYTHON)
48 self.SetKeyWords(0, " ".join(keyword.kwlist))
49
50 self.SetProperty("fold", "1")
51 self.SetProperty("tab.timmy.whinge.level", "1")
52 self.SetMargins(0,0)
53
54 self.SetViewWhiteSpace(False)
55 #self.SetBufferedDraw(False)
56 #self.SetViewEOL(True)
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)
65 self.SetMarginSensitive(2, True)
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")
85
86
87 EVT_STC_UPDATEUI(self, ID, self.OnUpdateUI)
88 EVT_STC_MARGINCLICK(self, ID, self.OnMarginClick)
89 EVT_KEY_DOWN(self, self.OnKeyPressed)
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 # Default
107 self.StyleSetSpec(wxSTC_P_DEFAULT, "fore:#000000,face:%(helv)s,size:%(size)d" % faces)
108 # Comments
109 self.StyleSetSpec(wxSTC_P_COMMENTLINE, "fore:#007F00,face:%(other)s,size:%(size)d" % faces)
110 # Number
111 self.StyleSetSpec(wxSTC_P_NUMBER, "fore:#007F7F,size:%(size)d" % faces)
112 # String
113 self.StyleSetSpec(wxSTC_P_STRING, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces)
114 # Single quoted string
115 self.StyleSetSpec(wxSTC_P_CHARACTER, "fore:#7F007F,face:%(helv)s,size:%(size)d" % faces)
116 # Keyword
117 self.StyleSetSpec(wxSTC_P_WORD, "fore:#00007F,bold,size:%(size)d" % faces)
118 # Triple quotes
119 self.StyleSetSpec(wxSTC_P_TRIPLE, "fore:#7F0000,size:%(size)d" % faces)
120 # Triple double quotes
121 self.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE, "fore:#7F0000,size:%(size)d" % faces)
122 # Class name definition
123 self.StyleSetSpec(wxSTC_P_CLASSNAME, "fore:#0000FF,bold,underline,size:%(size)d" % faces)
124 # Function or method name definition
125 self.StyleSetSpec(wxSTC_P_DEFNAME, "fore:#007F7F,bold,size:%(size)d" % faces)
126 # Operators
127 self.StyleSetSpec(wxSTC_P_OPERATOR, "bold,size:%(size)d" % faces)
128 # Identifiers
129 self.StyleSetSpec(wxSTC_P_IDENTIFIER, "fore:#000000,face:%(helv)s,size:%(size)d" % faces)
130 # Comment-blocks
131 self.StyleSetSpec(wxSTC_P_COMMENTBLOCK, "fore:#7F7F7F,size:%(size)d" % faces)
132 # End of line where string is not closed
133 self.StyleSetSpec(wxSTC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eol,size:%(size)d" % faces)
134
135 self.SetCaretForeground("BLUE")
136
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.getCopy2Bitmap())
142
143
144
145
146 def OnKeyPressed(self, event):
147 if self.CallTipActive():
148 self.CallTipCancel()
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")
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)')
158 # Code completion
159 else:
160 #lst = []
161 #for x in range(50000):
162 # lst.append('%05d' % x)
163 #st = " ".join(lst)
164 #print len(st)
165 #self.AutoCompShow(0, st)
166
167 kw = keyword.kwlist[:]
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")
173 kw.append("this_is_a_longer_value")
174 #kw.append("this_is_a_much_much_much_much_much_much_much_longer_value")
175
176 kw.sort() # Python sorts are case sensitive
177 self.AutoCompSetIgnoreCase(False) # so this needs to match
178
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
184 self.AutoCompShow(0, " ".join(kw))
185 else:
186 event.Skip()
187
188
189 def OnUpdateUI(self, evt):
190 # check for matching braces
191 braceAtCaret = -1
192 braceOpposite = -1
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
200 if charBefore and chr(charBefore) in "[]{}()" and styleBefore == wxSTC_P_OPERATOR:
201 braceAtCaret = caretPos - 1
202
203 # check after
204 if braceAtCaret < 0:
205 charAfter = self.GetCharAt(caretPos)
206 styleAfter = self.GetStyleAt(caretPos)
207 if charAfter and chr(charAfter) in "[]{}()" and styleAfter == wxSTC_P_OPERATOR:
208 braceAtCaret = caretPos
209
210 if braceAtCaret >= 0:
211 braceOpposite = self.BraceMatch(braceAtCaret)
212
213 if braceAtCaret != -1 and braceOpposite == -1:
214 self.BraceBadLight(braceAtCaret)
215 else:
216 self.BraceHighlight(braceAtCaret, braceOpposite)
217 #pt = self.PointFromPosition(braceOpposite)
218 #self.Refresh(True, wxRect(pt.x, pt.y, 5,5))
219 #print pt
220 #self.Refresh(False)
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:
229 lineClicked = self.LineFromPosition(evt.GetPosition())
230 if self.GetFoldLevel(lineClicked) & wxSTC_FOLDLEVELHEADERFLAG:
231 if evt.GetShift():
232 self.SetFoldExpanded(lineClicked, True)
233 self.Expand(lineClicked, True, True, 1)
234 elif evt.GetControl():
235 if self.GetFoldExpanded(lineClicked):
236 self.SetFoldExpanded(lineClicked, False)
237 self.Expand(lineClicked, False, True, 0)
238 else:
239 self.SetFoldExpanded(lineClicked, True)
240 self.Expand(lineClicked, True, True, 100)
241 else:
242 self.ToggleFold(lineClicked)
243
244
245 def FoldAll(self):
246 lineCount = self.GetLineCount()
247 expanding = True
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:
262 self.SetFoldExpanded(lineNum, True)
263 lineNum = self.Expand(lineNum, True)
264 lineNum = lineNum - 1
265 else:
266 lastChild = self.GetLastChild(lineNum, -1)
267 self.SetFoldExpanded(lineNum, False)
268 if lastChild > lineNum:
269 self.HideLines(lineNum+1, lastChild)
270
271 lineNum = lineNum + 1
272
273
274
275 def Expand(self, line, doExpand, force=False, visLevels=0, level=-1):
276 lastChild = self.GetLastChild(line, level)
277 line = line + 1
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:
294 self.SetFoldExpanded(line, True)
295 else:
296 self.SetFoldExpanded(line, False)
297 line = self.Expand(line, doExpand, force, visLevels-1)
298
299 else:
300 if doExpand and self.GetFoldExpanded(line):
301 line = self.Expand(line, True, force, visLevels-1)
302 else:
303 line = self.Expand(line, False, force, visLevels-1)
304 else:
305 line = line + 1;
306
307 return line
308
309
310 #----------------------------------------------------------------------
311
312 _USE_PANEL = 1
313
314 def runTest(frame, nb, log):
315 if not _USE_PANEL:
316 ed = p = PythonSTC(nb, -1)
317 else:
318 p = wxPanel(nb, -1, style = wxNO_FULL_REPAINT_ON_RESIZE)
319 ed = PythonSTC(p, -1)
320 s = wxBoxSizer(wxHORIZONTAL)
321 s.Add(ed, 1, wxEXPAND)
322 p.SetSizer(s)
323 p.SetAutoLayout(True)
324
325
326 ed.SetText(demoText + open('Main.py').read())
327 ed.EmptyUndoBuffer()
328 ed.Colourise(0, -1)
329
330 # line numbers in the margin
331 ed.SetMarginType(1, wxSTC_MARGIN_NUMBER)
332 ed.SetMarginWidth(1, 25)
333
334 return p
335
336
337
338 #----------------------------------------------------------------------
339
340
341 overview = """\
342 <html><body>
343 Once again, no docs yet. <b>Sorry.</b> But <a href="data/stc.h.html">this</a>
344 and <a href="http://www.scintilla.org/ScintillaDoc.html">this</a> should
345 be helpful.
346 </body><html>
347 """
348
349
350 if __name__ == '__main__':
351 import sys,os
352 import run
353 run.main(['', os.path.basename(sys.argv[0])])
354
355
356
357
358
359 #----------------------------------------------------------------------
360 #----------------------------------------------------------------------
361