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