]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/editwindow.py
cb1811f67578fa68f587b8053cc09f0deac6ebf0
[wxWidgets.git] / wxPython / wx / py / editwindow.py
1 """EditWindow class."""
2
3 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
4 __cvsid__ = "$Id$"
5 __revision__ = "$Revision$"[11:-2]
6
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
18
19 if 'wxMSW' in wx.PlatformInfo:
20 FACES = { 'times' : 'Times New Roman',
21 'mono' : 'Courier New',
22 'helv' : 'Lucida Console',
23 'lucida' : 'Lucida Console',
24 'other' : 'Comic Sans MS',
25 'size' : 10,
26 'lnsize' : 9,
27 'backcol' : '#FFFFFF',
28 'calltipbg' : '#FFFFB8',
29 'calltipfg' : '#404040',
30 }
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
44 else: # GTK1, OSX, etc.
45 FACES = { 'times' : 'Times',
46 'mono' : 'Courier',
47 'helv' : 'Helvetica',
48 'other' : 'new century schoolbook',
49 'size' : 12,
50 'lnsize' : 10,
51 'backcol' : '#FFFFFF',
52 'calltipbg' : '#FFFFB8',
53 'calltipfg' : '#404040',
54 }
55
56
57 class EditWindow(stc.StyledTextCtrl):
58 """EditWindow based on StyledTextCtrl."""
59
60 revision = __revision__
61
62 def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
63 size=wx.DefaultSize, style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER):
64 """Create EditWindow instance."""
65 stc.StyledTextCtrl.__init__(self, parent, id, pos, size, style)
66 self.__config()
67 stc.EVT_STC_UPDATEUI(self, id, self.OnUpdateUI)
68 dispatcher.connect(receiver=self._fontsizer, signal='FontIncrease')
69 dispatcher.connect(receiver=self._fontsizer, signal='FontDecrease')
70 dispatcher.connect(receiver=self._fontsizer, signal='FontDefault')
71
72 def _fontsizer(self, signal):
73 """Receiver for Font* signals."""
74 size = self.GetZoom()
75 if signal == 'FontIncrease':
76 size += 1
77 elif signal == 'FontDecrease':
78 size -= 1
79 elif signal == 'FontDefault':
80 size = 0
81 self.SetZoom(size)
82
83 def __config(self):
84 """Configure shell based on user preferences."""
85 self.SetMarginType(1, stc.STC_MARGIN_NUMBER)
86 self.SetMarginWidth(1, 40)
87
88 self.SetLexer(stc.STC_LEX_PYTHON)
89 self.SetKeyWords(0, ' '.join(keyword.kwlist))
90
91 self.setStyles(FACES)
92 self.SetViewWhiteSpace(False)
93 self.SetTabWidth(4)
94 self.SetUseTabs(False)
95 # Do we want to automatically pop up command completion options?
96 self.autoComplete = True
97 self.autoCompleteIncludeMagic = True
98 self.autoCompleteIncludeSingle = True
99 self.autoCompleteIncludeDouble = True
100 self.autoCompleteCaseInsensitive = True
101 self.AutoCompSetIgnoreCase(self.autoCompleteCaseInsensitive)
102 self.autoCompleteAutoHide = False
103 self.AutoCompSetAutoHide(self.autoCompleteAutoHide)
104 self.AutoCompStops(' .,;:([)]}\'"\\<>%^&+-=*/|`')
105 # Do we want to automatically pop up command argument help?
106 self.autoCallTip = True
107 self.CallTipSetBackground(FACES['calltipbg'])
108 self.CallTipSetForeground(FACES['calltipfg'])
109 self.SetWrapMode(False)
110 try:
111 self.SetEndAtLastLine(False)
112 except AttributeError:
113 pass
114
115 def setStyles(self, faces):
116 """Configure font size, typeface and color for lexer."""
117
118 # Default style
119 self.StyleSetSpec(stc.STC_STYLE_DEFAULT,
120 "face:%(mono)s,size:%(size)d,back:%(backcol)s" % \
121 faces)
122
123 self.StyleClearAll()
124
125 # Built in styles
126 self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,
127 "back:#C0C0C0,face:%(mono)s,size:%(lnsize)d" % faces)
128 self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR,
129 "face:%(mono)s" % faces)
130 self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,
131 "fore:#0000FF,back:#FFFF88")
132 self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,
133 "fore:#FF0000,back:#FFFF88")
134
135 # Python styles
136 self.StyleSetSpec(stc.STC_P_DEFAULT,
137 "face:%(mono)s" % faces)
138 self.StyleSetSpec(stc.STC_P_COMMENTLINE,
139 "fore:#007F00,face:%(mono)s" % faces)
140 self.StyleSetSpec(stc.STC_P_NUMBER,
141 "")
142 self.StyleSetSpec(stc.STC_P_STRING,
143 "fore:#7F007F,face:%(mono)s" % faces)
144 self.StyleSetSpec(stc.STC_P_CHARACTER,
145 "fore:#7F007F,face:%(mono)s" % faces)
146 self.StyleSetSpec(stc.STC_P_WORD,
147 "fore:#00007F,bold")
148 self.StyleSetSpec(stc.STC_P_TRIPLE,
149 "fore:#7F0000")
150 self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE,
151 "fore:#000033,back:#FFFFE8")
152 self.StyleSetSpec(stc.STC_P_CLASSNAME,
153 "fore:#0000FF,bold")
154 self.StyleSetSpec(stc.STC_P_DEFNAME,
155 "fore:#007F7F,bold")
156 self.StyleSetSpec(stc.STC_P_OPERATOR,
157 "")
158 self.StyleSetSpec(stc.STC_P_IDENTIFIER,
159 "")
160 self.StyleSetSpec(stc.STC_P_COMMENTBLOCK,
161 "fore:#7F7F7F")
162 self.StyleSetSpec(stc.STC_P_STRINGEOL,
163 "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces)
164
165 def OnUpdateUI(self, event):
166 """Check for matching braces."""
167 # If the auto-complete window is up let it do its thing.
168 if self.AutoCompActive() or self.CallTipActive():
169 return
170 braceAtCaret = -1
171 braceOpposite = -1
172 charBefore = None
173 caretPos = self.GetCurrentPos()
174 if caretPos > 0:
175 charBefore = self.GetCharAt(caretPos - 1)
176 styleBefore = self.GetStyleAt(caretPos - 1)
177
178 # Check before.
179 if charBefore and chr(charBefore) in '[]{}()' \
180 and styleBefore == stc.STC_P_OPERATOR:
181 braceAtCaret = caretPos - 1
182
183 # Check after.
184 if braceAtCaret < 0:
185 charAfter = self.GetCharAt(caretPos)
186 styleAfter = self.GetStyleAt(caretPos)
187 if charAfter and chr(charAfter) in '[]{}()' \
188 and styleAfter == stc.STC_P_OPERATOR:
189 braceAtCaret = caretPos
190
191 if braceAtCaret >= 0:
192 braceOpposite = self.BraceMatch(braceAtCaret)
193
194 if braceAtCaret != -1 and braceOpposite == -1:
195 self.BraceBadLight(braceAtCaret)
196 else:
197 self.BraceHighlight(braceAtCaret, braceOpposite)
198
199 def CanCopy(self):
200 """Return True if text is selected and can be copied."""
201 return self.GetSelectionStart() != self.GetSelectionEnd()
202
203 def CanCut(self):
204 """Return True if text is selected and can be cut."""
205 return self.CanCopy() and self.CanEdit()
206
207 def CanEdit(self):
208 """Return True if editing should succeed."""
209 return not self.GetReadOnly()
210
211 def CanPaste(self):
212 """Return True if pasting should succeed."""
213 return stc.StyledTextCtrl.CanPaste(self) and self.CanEdit()