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