]>
Commit | Line | Data |
---|---|---|
d14a1e28 | 1 | """EditWindow class.""" |
1fded56b | 2 | |
d14a1e28 RD |
3 | __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>" |
4 | __cvsid__ = "$Id$" | |
5 | __revision__ = "$Revision$"[11:-2] | |
1fded56b | 6 | |
d14a1e28 RD |
7 | import wx |
8 | from wx import stc | |
9 | ||
10 | import keyword | |
11 | import os | |
12 | import sys | |
13 | import time | |
14 | ||
15 | import dispatcher | |
16 | from version import VERSION | |
17 | ||
d14a1e28 | 18 | |
99b486ab RD |
19 | if 'wxMSW' in wx.PlatformInfo: |
20 | FACES = { 'times' : 'Times New Roman', | |
21 | 'mono' : 'Courier New', | |
4263de1e | 22 | 'helv' : 'Arial', |
99b486ab RD |
23 | 'lucida' : 'Lucida Console', |
24 | 'other' : 'Comic Sans MS', | |
25 | 'size' : 10, | |
31fbbf78 | 26 | 'lnsize' : 8, |
99b486ab RD |
27 | 'backcol' : '#FFFFFF', |
28 | 'calltipbg' : '#FFFFB8', | |
29 | 'calltipfg' : '#404040', | |
d14a1e28 | 30 | } |
99b486ab RD |
31 | |
32 | elif 'wxGTK' in wx.PlatformInfo and 'gtk2' in wx.PlatformInfo: | |
33 | FACES = { 'times' : 'Serif', | |
34 | 'mono' : 'Monospace', | |
35 | 'helv' : 'Sans', | |
36 | 'other' : 'new century schoolbook', | |
37 | 'size' : 10, | |
38 | 'lnsize' : 9, | |
39 | 'backcol' : '#FFFFFF', | |
40 | 'calltipbg' : '#FFFFB8', | |
41 | 'calltipfg' : '#404040', | |
42 | } | |
43 | ||
4263de1e RD |
44 | elif 'wxMac' in wx.PlatformInfo: |
45 | FACES = { 'times' : 'Lucida Grande', | |
46 | 'mono' : 'Courier New', | |
47 | 'helv' : 'Geneva', | |
48 | 'other' : 'new century schoolbook', | |
49 | 'size' : 13, | |
50 | 'lnsize' : 10, | |
51 | 'backcol' : '#FFFFFF', | |
52 | 'calltipbg' : '#FFFFB8', | |
53 | 'calltipfg' : '#404040', | |
54 | } | |
55 | ||
56 | else: # GTK1, etc. | |
99b486ab RD |
57 | FACES = { 'times' : 'Times', |
58 | 'mono' : 'Courier', | |
59 | 'helv' : 'Helvetica', | |
60 | 'other' : 'new century schoolbook', | |
61 | 'size' : 12, | |
62 | 'lnsize' : 10, | |
63 | 'backcol' : '#FFFFFF', | |
64 | 'calltipbg' : '#FFFFB8', | |
65 | 'calltipfg' : '#404040', | |
d14a1e28 RD |
66 | } |
67 | ||
68 | ||
69 | class EditWindow(stc.StyledTextCtrl): | |
70 | """EditWindow based on StyledTextCtrl.""" | |
71 | ||
72 | revision = __revision__ | |
73 | ||
74 | def __init__(self, parent, id=-1, pos=wx.DefaultPosition, | |
75 | size=wx.DefaultSize, style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER): | |
76 | """Create EditWindow instance.""" | |
77 | stc.StyledTextCtrl.__init__(self, parent, id, pos, size, style) | |
78 | self.__config() | |
79 | stc.EVT_STC_UPDATEUI(self, id, self.OnUpdateUI) | |
80 | dispatcher.connect(receiver=self._fontsizer, signal='FontIncrease') | |
81 | dispatcher.connect(receiver=self._fontsizer, signal='FontDecrease') | |
82 | dispatcher.connect(receiver=self._fontsizer, signal='FontDefault') | |
83 | ||
84 | def _fontsizer(self, signal): | |
85 | """Receiver for Font* signals.""" | |
86 | size = self.GetZoom() | |
87 | if signal == 'FontIncrease': | |
88 | size += 1 | |
89 | elif signal == 'FontDecrease': | |
90 | size -= 1 | |
91 | elif signal == 'FontDefault': | |
92 | size = 0 | |
93 | self.SetZoom(size) | |
94 | ||
d14a1e28 | 95 | |
02b800ce RD |
96 | def __config(self): |
97 | self.setDisplayLineNumbers(False) | |
98 | ||
d14a1e28 RD |
99 | self.SetLexer(stc.STC_LEX_PYTHON) |
100 | self.SetKeyWords(0, ' '.join(keyword.kwlist)) | |
101 | ||
102 | self.setStyles(FACES) | |
103 | self.SetViewWhiteSpace(False) | |
104 | self.SetTabWidth(4) | |
105 | self.SetUseTabs(False) | |
106 | # Do we want to automatically pop up command completion options? | |
107 | self.autoComplete = True | |
108 | self.autoCompleteIncludeMagic = True | |
109 | self.autoCompleteIncludeSingle = True | |
110 | self.autoCompleteIncludeDouble = True | |
111 | self.autoCompleteCaseInsensitive = True | |
112 | self.AutoCompSetIgnoreCase(self.autoCompleteCaseInsensitive) | |
f65958cc | 113 | self.autoCompleteAutoHide = False |
d351525a | 114 | self.AutoCompSetAutoHide(self.autoCompleteAutoHide) |
d14a1e28 RD |
115 | self.AutoCompStops(' .,;:([)]}\'"\\<>%^&+-=*/|`') |
116 | # Do we want to automatically pop up command argument help? | |
117 | self.autoCallTip = True | |
02b800ce | 118 | self.callTipInsert = True |
99b486ab RD |
119 | self.CallTipSetBackground(FACES['calltipbg']) |
120 | self.CallTipSetForeground(FACES['calltipfg']) | |
d14a1e28 RD |
121 | self.SetWrapMode(False) |
122 | try: | |
123 | self.SetEndAtLastLine(False) | |
124 | except AttributeError: | |
125 | pass | |
126 | ||
02b800ce RD |
127 | def setDisplayLineNumbers(self, state): |
128 | self.lineNumbers = state | |
129 | if state: | |
130 | self.SetMarginType(1, stc.STC_MARGIN_NUMBER) | |
131 | self.SetMarginWidth(1, 40) | |
132 | else: | |
133 | # Leave a small margin so the feature hidden lines marker can be seen | |
134 | self.SetMarginType(1, 0) | |
135 | self.SetMarginWidth(1, 10) | |
136 | ||
d14a1e28 RD |
137 | def setStyles(self, faces): |
138 | """Configure font size, typeface and color for lexer.""" | |
139 | ||
140 | # Default style | |
141 | self.StyleSetSpec(stc.STC_STYLE_DEFAULT, | |
142 | "face:%(mono)s,size:%(size)d,back:%(backcol)s" % \ | |
143 | faces) | |
144 | ||
145 | self.StyleClearAll() | |
3e470207 RD |
146 | self.SetSelForeground(True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)) |
147 | self.SetSelBackground(True, wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT)) | |
148 | ||
d14a1e28 RD |
149 | # Built in styles |
150 | self.StyleSetSpec(stc.STC_STYLE_LINENUMBER, | |
02b800ce | 151 | "back:#C0C0C0,face:%(mono)s,size:%(lnsize)d" % FACES) |
d14a1e28 RD |
152 | self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR, |
153 | "face:%(mono)s" % faces) | |
154 | self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT, | |
155 | "fore:#0000FF,back:#FFFF88") | |
156 | self.StyleSetSpec(stc.STC_STYLE_BRACEBAD, | |
157 | "fore:#FF0000,back:#FFFF88") | |
158 | ||
159 | # Python styles | |
160 | self.StyleSetSpec(stc.STC_P_DEFAULT, | |
161 | "face:%(mono)s" % faces) | |
162 | self.StyleSetSpec(stc.STC_P_COMMENTLINE, | |
163 | "fore:#007F00,face:%(mono)s" % faces) | |
164 | self.StyleSetSpec(stc.STC_P_NUMBER, | |
165 | "") | |
166 | self.StyleSetSpec(stc.STC_P_STRING, | |
167 | "fore:#7F007F,face:%(mono)s" % faces) | |
168 | self.StyleSetSpec(stc.STC_P_CHARACTER, | |
169 | "fore:#7F007F,face:%(mono)s" % faces) | |
170 | self.StyleSetSpec(stc.STC_P_WORD, | |
171 | "fore:#00007F,bold") | |
172 | self.StyleSetSpec(stc.STC_P_TRIPLE, | |
173 | "fore:#7F0000") | |
174 | self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE, | |
175 | "fore:#000033,back:#FFFFE8") | |
176 | self.StyleSetSpec(stc.STC_P_CLASSNAME, | |
177 | "fore:#0000FF,bold") | |
178 | self.StyleSetSpec(stc.STC_P_DEFNAME, | |
179 | "fore:#007F7F,bold") | |
180 | self.StyleSetSpec(stc.STC_P_OPERATOR, | |
181 | "") | |
182 | self.StyleSetSpec(stc.STC_P_IDENTIFIER, | |
183 | "") | |
184 | self.StyleSetSpec(stc.STC_P_COMMENTBLOCK, | |
185 | "fore:#7F7F7F") | |
186 | self.StyleSetSpec(stc.STC_P_STRINGEOL, | |
187 | "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces) | |
188 | ||
189 | def OnUpdateUI(self, event): | |
190 | """Check for matching braces.""" | |
191 | # If the auto-complete window is up let it do its thing. | |
192 | if self.AutoCompActive() or self.CallTipActive(): | |
193 | return | |
194 | braceAtCaret = -1 | |
195 | braceOpposite = -1 | |
196 | charBefore = None | |
197 | caretPos = self.GetCurrentPos() | |
198 | if caretPos > 0: | |
199 | charBefore = self.GetCharAt(caretPos - 1) | |
200 | styleBefore = self.GetStyleAt(caretPos - 1) | |
201 | ||
202 | # Check before. | |
203 | if charBefore and chr(charBefore) in '[]{}()' \ | |
204 | and styleBefore == stc.STC_P_OPERATOR: | |
205 | braceAtCaret = caretPos - 1 | |
206 | ||
207 | # Check after. | |
208 | if braceAtCaret < 0: | |
209 | charAfter = self.GetCharAt(caretPos) | |
210 | styleAfter = self.GetStyleAt(caretPos) | |
211 | if charAfter and chr(charAfter) in '[]{}()' \ | |
212 | and styleAfter == stc.STC_P_OPERATOR: | |
213 | braceAtCaret = caretPos | |
214 | ||
215 | if braceAtCaret >= 0: | |
216 | braceOpposite = self.BraceMatch(braceAtCaret) | |
217 | ||
218 | if braceAtCaret != -1 and braceOpposite == -1: | |
219 | self.BraceBadLight(braceAtCaret) | |
220 | else: | |
221 | self.BraceHighlight(braceAtCaret, braceOpposite) | |
222 | ||
223 | def CanCopy(self): | |
224 | """Return True if text is selected and can be copied.""" | |
225 | return self.GetSelectionStart() != self.GetSelectionEnd() | |
226 | ||
227 | def CanCut(self): | |
228 | """Return True if text is selected and can be cut.""" | |
229 | return self.CanCopy() and self.CanEdit() | |
230 | ||
231 | def CanEdit(self): | |
232 | """Return True if editing should succeed.""" | |
233 | return not self.GetReadOnly() | |
234 | ||
235 | def CanPaste(self): | |
236 | """Return True if pasting should succeed.""" | |
237 | return stc.StyledTextCtrl.CanPaste(self) and self.CanEdit() | |
02b800ce RD |
238 | |
239 | ||
240 | def GetLastPosition(self): | |
241 | return self.GetLength() | |
242 | ||
243 | def GetRange(self, start, end): | |
244 | return self.GetTextRange(start, end) | |
245 | ||
246 | def GetSelection(self): | |
247 | return self.GetAnchor(), self.GetCurrentPos() | |
248 | ||
02b800ce RD |
249 | def ShowPosition(self, pos): |
250 | line = self.LineFromPosition(pos) | |
251 | #self.EnsureVisible(line) | |
252 | self.GotoLine(line) | |
253 | ||
254 | def DoFindNext(self, findData, findDlg=None): | |
255 | backward = not (findData.GetFlags() & wx.FR_DOWN) | |
e773f79b | 256 | matchcase = (findData.GetFlags() & wx.FR_MATCHCASE) != 0 |
02b800ce RD |
257 | end = self.GetLastPosition() |
258 | textstring = self.GetRange(0, end) | |
259 | findstring = findData.GetFindString() | |
260 | if not matchcase: | |
e773f79b RD |
261 | textstring = textstring.lower() |
262 | findstring = findstring.lower() | |
02b800ce RD |
263 | if backward: |
264 | start = self.GetSelection()[0] | |
265 | loc = textstring.rfind(findstring, 0, start) | |
266 | else: | |
267 | start = self.GetSelection()[1] | |
268 | loc = textstring.find(findstring, start) | |
269 | ||
270 | # if it wasn't found then restart at begining | |
271 | if loc == -1 and start != 0: | |
272 | if backward: | |
273 | start = end | |
274 | loc = textstring.rfind(findstring, 0, start) | |
275 | else: | |
276 | start = 0 | |
277 | loc = textstring.find(findstring, start) | |
278 | ||
279 | # was it still not found? | |
280 | if loc == -1: | |
281 | dlg = wx.MessageDialog(self, 'Unable to find the search text.', | |
282 | 'Not found!', | |
283 | wx.OK | wx.ICON_INFORMATION) | |
284 | dlg.ShowModal() | |
285 | dlg.Destroy() | |
286 | if findDlg: | |
287 | if loc == -1: | |
288 | wx.CallAfter(findDlg.SetFocus) | |
289 | return | |
290 | else: | |
291 | findDlg.Close() | |
292 | ||
293 | # show and select the found text | |
294 | self.ShowPosition(loc) | |
295 | self.SetSelection(loc, loc + len(findstring)) |