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