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', 
  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 elif 'wxMac' in wx
.PlatformInfo
: 
  45     FACES 
= { 'times'     : 'Lucida Grande', 
  46               'mono'      : 'Courier New', 
  48               'other'     : 'new century schoolbook', 
  51               'backcol'   : '#FFFFFF', 
  52               'calltipbg' : '#FFFFB8', 
  53               'calltipfg' : '#404040', 
  57     FACES 
= { 'times'     : 'Times', 
  60               'other'     : 'new century schoolbook', 
  63               'backcol'   : '#FFFFFF', 
  64               'calltipbg' : '#FFFFB8', 
  65               'calltipfg' : '#404040', 
  69 class EditWindow(stc
.StyledTextCtrl
): 
  70     """EditWindow based on StyledTextCtrl.""" 
  72     revision 
= __revision__
 
  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
) 
  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') 
  84     def _fontsizer(self
, signal
): 
  85         """Receiver for Font* signals.""" 
  87         if signal 
== 'FontIncrease': 
  89         elif signal 
== 'FontDecrease': 
  91         elif signal 
== 'FontDefault': 
  96         """Configure shell based on user preferences.""" 
  97         self
.SetMarginType(1, stc
.STC_MARGIN_NUMBER
) 
  98         self
.SetMarginWidth(1, 40) 
 100         self
.SetLexer(stc
.STC_LEX_PYTHON
) 
 101         self
.SetKeyWords(0, ' '.join(keyword
.kwlist
)) 
 103         self
.setStyles(FACES
) 
 104         self
.SetViewWhiteSpace(False) 
 106         self
.SetUseTabs(False) 
 107         # Do we want to automatically pop up command completion options? 
 108         self
.autoComplete 
= True 
 109         self
.autoCompleteIncludeMagic 
= True 
 110         self
.autoCompleteIncludeSingle 
= True 
 111         self
.autoCompleteIncludeDouble 
= True 
 112         self
.autoCompleteCaseInsensitive 
= True 
 113         self
.AutoCompSetIgnoreCase(self
.autoCompleteCaseInsensitive
) 
 114         self
.autoCompleteAutoHide 
= False 
 115         self
.AutoCompSetAutoHide(self
.autoCompleteAutoHide
) 
 116         self
.AutoCompStops(' .,;:([)]}\'"\\<>%^&+-=*/|`') 
 117         # Do we want to automatically pop up command argument help? 
 118         self
.autoCallTip 
= True 
 119         self
.CallTipSetBackground(FACES
['calltipbg']) 
 120         self
.CallTipSetForeground(FACES
['calltipfg']) 
 121         self
.SetWrapMode(False) 
 123             self
.SetEndAtLastLine(False) 
 124         except AttributeError: 
 127     def setStyles(self
, faces
): 
 128         """Configure font size, typeface and color for lexer.""" 
 131         self
.StyleSetSpec(stc
.STC_STYLE_DEFAULT
, 
 132                           "face:%(mono)s,size:%(size)d,back:%(backcol)s" % \
 
 138         self
.StyleSetSpec(stc
.STC_STYLE_LINENUMBER
, 
 139                           "back:#C0C0C0,face:%(mono)s,size:%(lnsize)d" % faces
) 
 140         self
.StyleSetSpec(stc
.STC_STYLE_CONTROLCHAR
, 
 141                           "face:%(mono)s" % faces
) 
 142         self
.StyleSetSpec(stc
.STC_STYLE_BRACELIGHT
, 
 143                           "fore:#0000FF,back:#FFFF88") 
 144         self
.StyleSetSpec(stc
.STC_STYLE_BRACEBAD
, 
 145                           "fore:#FF0000,back:#FFFF88") 
 148         self
.StyleSetSpec(stc
.STC_P_DEFAULT
, 
 149                           "face:%(mono)s" % faces
) 
 150         self
.StyleSetSpec(stc
.STC_P_COMMENTLINE
, 
 151                           "fore:#007F00,face:%(mono)s" % faces
) 
 152         self
.StyleSetSpec(stc
.STC_P_NUMBER
, 
 154         self
.StyleSetSpec(stc
.STC_P_STRING
, 
 155                           "fore:#7F007F,face:%(mono)s" % faces
) 
 156         self
.StyleSetSpec(stc
.STC_P_CHARACTER
, 
 157                           "fore:#7F007F,face:%(mono)s" % faces
) 
 158         self
.StyleSetSpec(stc
.STC_P_WORD
, 
 160         self
.StyleSetSpec(stc
.STC_P_TRIPLE
, 
 162         self
.StyleSetSpec(stc
.STC_P_TRIPLEDOUBLE
, 
 163                           "fore:#000033,back:#FFFFE8") 
 164         self
.StyleSetSpec(stc
.STC_P_CLASSNAME
, 
 166         self
.StyleSetSpec(stc
.STC_P_DEFNAME
, 
 168         self
.StyleSetSpec(stc
.STC_P_OPERATOR
, 
 170         self
.StyleSetSpec(stc
.STC_P_IDENTIFIER
, 
 172         self
.StyleSetSpec(stc
.STC_P_COMMENTBLOCK
, 
 174         self
.StyleSetSpec(stc
.STC_P_STRINGEOL
, 
 175                           "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces
) 
 177     def OnUpdateUI(self
, event
): 
 178         """Check for matching braces.""" 
 179         # If the auto-complete window is up let it do its thing. 
 180         if self
.AutoCompActive() or self
.CallTipActive(): 
 185         caretPos 
= self
.GetCurrentPos() 
 187             charBefore 
= self
.GetCharAt(caretPos 
- 1) 
 188             styleBefore 
= self
.GetStyleAt(caretPos 
- 1) 
 191         if charBefore 
and chr(charBefore
) in '[]{}()' \
 
 192         and styleBefore 
== stc
.STC_P_OPERATOR
: 
 193             braceAtCaret 
= caretPos 
- 1 
 197             charAfter 
= self
.GetCharAt(caretPos
) 
 198             styleAfter 
= self
.GetStyleAt(caretPos
) 
 199             if charAfter 
and chr(charAfter
) in '[]{}()' \
 
 200             and styleAfter 
== stc
.STC_P_OPERATOR
: 
 201                 braceAtCaret 
= caretPos
 
 203         if braceAtCaret 
>= 0: 
 204             braceOpposite 
= self
.BraceMatch(braceAtCaret
) 
 206         if braceAtCaret 
!= -1  and braceOpposite 
== -1: 
 207             self
.BraceBadLight(braceAtCaret
) 
 209             self
.BraceHighlight(braceAtCaret
, braceOpposite
) 
 212         """Return True if text is selected and can be copied.""" 
 213         return self
.GetSelectionStart() != self
.GetSelectionEnd() 
 216         """Return True if text is selected and can be cut.""" 
 217         return self
.CanCopy() and self
.CanEdit() 
 220         """Return True if editing should succeed.""" 
 221         return not self
.GetReadOnly() 
 224         """Return True if pasting should succeed.""" 
 225         return stc
.StyledTextCtrl
.CanPaste(self
) and self
.CanEdit()