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