]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/editwindow.py
Rename the .tar.gz to a .tgz
[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.AutoCompSetAutoHide(False)
91 self.AutoCompStops(' .,;:([)]}\'"\\<>%^&+-=*/|`')
92 # Do we want to automatically pop up command argument help?
93 self.autoCallTip = True
94 self.CallTipSetBackground(wx.Colour(255, 255, 232))
95 self.SetWrapMode(False)
96 try:
97 self.SetEndAtLastLine(False)
98 except AttributeError:
99 pass
100
101 def setStyles(self, faces):
102 """Configure font size, typeface and color for lexer."""
103
104 # Default style
105 self.StyleSetSpec(stc.STC_STYLE_DEFAULT,
106 "face:%(mono)s,size:%(size)d,back:%(backcol)s" % \
107 faces)
108
109 self.StyleClearAll()
110
111 # Built in styles
112 self.StyleSetSpec(stc.STC_STYLE_LINENUMBER,
113 "back:#C0C0C0,face:%(mono)s,size:%(lnsize)d" % faces)
114 self.StyleSetSpec(stc.STC_STYLE_CONTROLCHAR,
115 "face:%(mono)s" % faces)
116 self.StyleSetSpec(stc.STC_STYLE_BRACELIGHT,
117 "fore:#0000FF,back:#FFFF88")
118 self.StyleSetSpec(stc.STC_STYLE_BRACEBAD,
119 "fore:#FF0000,back:#FFFF88")
120
121 # Python styles
122 self.StyleSetSpec(stc.STC_P_DEFAULT,
123 "face:%(mono)s" % faces)
124 self.StyleSetSpec(stc.STC_P_COMMENTLINE,
125 "fore:#007F00,face:%(mono)s" % faces)
126 self.StyleSetSpec(stc.STC_P_NUMBER,
127 "")
128 self.StyleSetSpec(stc.STC_P_STRING,
129 "fore:#7F007F,face:%(mono)s" % faces)
130 self.StyleSetSpec(stc.STC_P_CHARACTER,
131 "fore:#7F007F,face:%(mono)s" % faces)
132 self.StyleSetSpec(stc.STC_P_WORD,
133 "fore:#00007F,bold")
134 self.StyleSetSpec(stc.STC_P_TRIPLE,
135 "fore:#7F0000")
136 self.StyleSetSpec(stc.STC_P_TRIPLEDOUBLE,
137 "fore:#000033,back:#FFFFE8")
138 self.StyleSetSpec(stc.STC_P_CLASSNAME,
139 "fore:#0000FF,bold")
140 self.StyleSetSpec(stc.STC_P_DEFNAME,
141 "fore:#007F7F,bold")
142 self.StyleSetSpec(stc.STC_P_OPERATOR,
143 "")
144 self.StyleSetSpec(stc.STC_P_IDENTIFIER,
145 "")
146 self.StyleSetSpec(stc.STC_P_COMMENTBLOCK,
147 "fore:#7F7F7F")
148 self.StyleSetSpec(stc.STC_P_STRINGEOL,
149 "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces)
150
151 def OnUpdateUI(self, event):
152 """Check for matching braces."""
153 # If the auto-complete window is up let it do its thing.
154 if self.AutoCompActive() or self.CallTipActive():
155 return
156 braceAtCaret = -1
157 braceOpposite = -1
158 charBefore = None
159 caretPos = self.GetCurrentPos()
160 if caretPos > 0:
161 charBefore = self.GetCharAt(caretPos - 1)
162 styleBefore = self.GetStyleAt(caretPos - 1)
163
164 # Check before.
165 if charBefore and chr(charBefore) in '[]{}()' \
166 and styleBefore == stc.STC_P_OPERATOR:
167 braceAtCaret = caretPos - 1
168
169 # Check after.
170 if braceAtCaret < 0:
171 charAfter = self.GetCharAt(caretPos)
172 styleAfter = self.GetStyleAt(caretPos)
173 if charAfter and chr(charAfter) in '[]{}()' \
174 and styleAfter == stc.STC_P_OPERATOR:
175 braceAtCaret = caretPos
176
177 if braceAtCaret >= 0:
178 braceOpposite = self.BraceMatch(braceAtCaret)
179
180 if braceAtCaret != -1 and braceOpposite == -1:
181 self.BraceBadLight(braceAtCaret)
182 else:
183 self.BraceHighlight(braceAtCaret, braceOpposite)
184
185 def CanCopy(self):
186 """Return True if text is selected and can be copied."""
187 return self.GetSelectionStart() != self.GetSelectionEnd()
188
189 def CanCut(self):
190 """Return True if text is selected and can be cut."""
191 return self.CanCopy() and self.CanEdit()
192
193 def CanEdit(self):
194 """Return True if editing should succeed."""
195 return not self.GetReadOnly()
196
197 def CanPaste(self):
198 """Return True if pasting should succeed."""
199 return stc.StyledTextCtrl.CanPaste(self) and self.CanEdit()