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