1 """EditWindow class."""
3 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __revision__
= "$Revision$"[11:-2]
16 from version
import VERSION
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',
27 'backcol' : '#FFFFFF',
28 'calltipbg' : '#FFFFB8',
29 'calltipfg' : '#404040',
32 elif 'wxGTK' in wx
.PlatformInfo
and 'gtk2' in wx
.PlatformInfo
:
33 FACES
= { 'times' : 'Serif',
36 'other' : 'new century schoolbook',
39 'backcol' : '#FFFFFF',
40 'calltipbg' : '#FFFFB8',
41 'calltipfg' : '#404040',
44 else: # GTK1, OSX, etc.
45 FACES
= { 'times' : 'Times',
48 'other' : 'new century schoolbook',
51 'backcol' : '#FFFFFF',
52 'calltipbg' : '#FFFFB8',
53 'calltipfg' : '#404040',
57 class EditWindow(stc
.StyledTextCtrl
):
58 """EditWindow based on StyledTextCtrl."""
60 revision
= __revision__
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
)
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')
72 def _fontsizer(self
, signal
):
73 """Receiver for Font* signals."""
75 if signal
== 'FontIncrease':
77 elif signal
== 'FontDecrease':
79 elif signal
== 'FontDefault':
84 """Configure shell based on user preferences."""
85 self
.SetMarginType(1, stc
.STC_MARGIN_NUMBER
)
86 self
.SetMarginWidth(1, 40)
88 self
.SetLexer(stc
.STC_LEX_PYTHON
)
89 self
.SetKeyWords(0, ' '.join(keyword
.kwlist
))
92 self
.SetViewWhiteSpace(False)
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)
111 self
.SetEndAtLastLine(False)
112 except AttributeError:
115 def setStyles(self
, faces
):
116 """Configure font size, typeface and color for lexer."""
119 self
.StyleSetSpec(stc
.STC_STYLE_DEFAULT
,
120 "face:%(mono)s,size:%(size)d,back:%(backcol)s" % \
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")
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
,
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
,
148 self
.StyleSetSpec(stc
.STC_P_TRIPLE
,
150 self
.StyleSetSpec(stc
.STC_P_TRIPLEDOUBLE
,
151 "fore:#000033,back:#FFFFE8")
152 self
.StyleSetSpec(stc
.STC_P_CLASSNAME
,
154 self
.StyleSetSpec(stc
.STC_P_DEFNAME
,
156 self
.StyleSetSpec(stc
.STC_P_OPERATOR
,
158 self
.StyleSetSpec(stc
.STC_P_IDENTIFIER
,
160 self
.StyleSetSpec(stc
.STC_P_COMMENTBLOCK
,
162 self
.StyleSetSpec(stc
.STC_P_STRINGEOL
,
163 "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces
)
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():
173 caretPos
= self
.GetCurrentPos()
175 charBefore
= self
.GetCharAt(caretPos
- 1)
176 styleBefore
= self
.GetStyleAt(caretPos
- 1)
179 if charBefore
and chr(charBefore
) in '[]{}()' \
180 and styleBefore
== stc
.STC_P_OPERATOR
:
181 braceAtCaret
= caretPos
- 1
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
191 if braceAtCaret
>= 0:
192 braceOpposite
= self
.BraceMatch(braceAtCaret
)
194 if braceAtCaret
!= -1 and braceOpposite
== -1:
195 self
.BraceBadLight(braceAtCaret
)
197 self
.BraceHighlight(braceAtCaret
, braceOpposite
)
200 """Return True if text is selected and can be copied."""
201 return self
.GetSelectionStart() != self
.GetSelectionEnd()
204 """Return True if text is selected and can be cut."""
205 return self
.CanCopy() and self
.CanEdit()
208 """Return True if editing should succeed."""
209 return not self
.GetReadOnly()
212 """Return True if pasting should succeed."""
213 return stc
.StyledTextCtrl
.CanPaste(self
) and self
.CanEdit()