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