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