]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/py/crust.py
ef9d235804987fa088c36b7cadfae68fe051681a
[wxWidgets.git] / wxPython / wx / py / crust.py
1 """Crust combines the shell and filling into one control."""
2
3 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
4 __cvsid__ = "$Id$"
5 __revision__ = "$Revision$"[11:-2]
6
7 import wx
8
9 import os
10 import pprint
11 import sys
12
13 import dispatcher
14 import editwindow
15 from filling import Filling
16 import frame
17 from shell import Shell
18 from version import VERSION
19
20 try:
21 True
22 except NameError:
23 True = 1==1
24 False = 1==0
25
26
27 class Crust(wx.SplitterWindow):
28 """Crust based on SplitterWindow."""
29
30 name = 'Crust'
31 revision = __revision__
32
33 def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
34 size=wx.DefaultSize, style=wx.SP_3D,
35 name='Crust Window', rootObject=None, rootLabel=None,
36 rootIsNamespace=True, intro='', locals=None,
37 InterpClass=None, *args, **kwds):
38 """Create Crust instance."""
39 wx.SplitterWindow.__init__(self, parent, id, pos, size, style, name)
40 self.shell = Shell(parent=self, introText=intro,
41 locals=locals, InterpClass=InterpClass,
42 *args, **kwds)
43 self.editor = self.shell
44 if rootObject is None:
45 rootObject = self.shell.interp.locals
46 self.notebook = wx.Notebook(parent=self, id=-1)
47 self.shell.interp.locals['notebook'] = self.notebook
48 self.filling = Filling(parent=self.notebook,
49 rootObject=rootObject,
50 rootLabel=rootLabel,
51 rootIsNamespace=rootIsNamespace)
52 # Add 'filling' to the interpreter's locals.
53 self.shell.interp.locals['filling'] = self.filling
54 self.notebook.AddPage(page=self.filling, text='Namespace', select=True)
55 self.display = Display(parent=self.notebook)
56 self.notebook.AddPage(page=self.display, text='Display')
57 # Add 'pp' (pretty print) to the interpreter's locals.
58 self.shell.interp.locals['pp'] = self.display.setItem
59 self.calltip = Calltip(parent=self.notebook)
60 self.notebook.AddPage(page=self.calltip, text='Calltip')
61 self.sessionlisting = SessionListing(parent=self.notebook)
62 self.notebook.AddPage(page=self.sessionlisting, text='Session')
63 self.dispatcherlisting = DispatcherListing(parent=self.notebook)
64 self.notebook.AddPage(page=self.dispatcherlisting, text='Dispatcher')
65 ## from wxd import wx_
66 ## self.wxdocs = Filling(parent=self.notebook,
67 ## rootObject=wx_,
68 ## rootLabel='wx',
69 ## rootIsNamespace=False,
70 ## static=True)
71 ## self.notebook.AddPage(page=self.wxdocs, text='wxPython Docs')
72 ## from wxd import stc_
73 ## self.stcdocs = Filling(parent=self.notebook,
74 ## rootObject=stc_.StyledTextCtrl,
75 ## rootLabel='StyledTextCtrl',
76 ## rootIsNamespace=False,
77 ## static=True)
78 ## self.notebook.AddPage(page=self.stcdocs, text='StyledTextCtrl Docs')
79 self.SplitHorizontally(self.shell, self.notebook, 300)
80 self.SetMinimumPaneSize(1)
81
82
83 class Display(editwindow.EditWindow):
84 """STC used to display an object using Pretty Print."""
85
86 def __init__(self, parent, id=-1, pos=wx.DefaultPosition,
87 size=wx.DefaultSize,
88 style=wx.CLIP_CHILDREN | wx.SUNKEN_BORDER,
89 static=False):
90 """Create Display instance."""
91 editwindow.EditWindow.__init__(self, parent, id, pos, size, style)
92 # Configure various defaults and user preferences.
93 self.SetReadOnly(True)
94 self.SetWrapMode(False)
95 if not static:
96 dispatcher.connect(receiver=self.push, signal='Interpreter.push')
97
98 def push(self, command, more):
99 """Receiver for Interpreter.push signal."""
100 self.Refresh()
101
102 def Refresh(self):
103 if not hasattr(self, "item"):
104 return
105 self.SetReadOnly(False)
106 text = pprint.pformat(self.item)
107 self.SetText(text)
108 self.SetReadOnly(True)
109
110 def setItem(self, item):
111 """Set item to pretty print in the notebook Display tab."""
112 self.item = item
113 self.Refresh()
114
115
116 class Calltip(wx.TextCtrl):
117 """Text control containing the most recent shell calltip."""
118
119 def __init__(self, parent=None, id=-1):
120 style = (wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH2)
121 wx.TextCtrl.__init__(self, parent, id, style=style)
122 self.SetBackgroundColour(wx.Colour(255, 255, 232))
123 dispatcher.connect(receiver=self.display, signal='Shell.calltip')
124
125 def display(self, calltip):
126 """Receiver for Shell.calltip signal."""
127 ## self.SetValue(calltip) # Caused refresh problem on Windows.
128 self.Clear()
129 self.AppendText(calltip)
130
131
132 class SessionListing(wx.TextCtrl):
133 """Text control containing all commands for session."""
134
135 def __init__(self, parent=None, id=-1):
136 style = (wx.TE_MULTILINE | wx.TE_READONLY |
137 wx.TE_RICH2 | wx.TE_DONTWRAP)
138 wx.TextCtrl.__init__(self, parent, id, style=style)
139 dispatcher.connect(receiver=self.push, signal='Interpreter.push')
140
141 def push(self, command, more):
142 """Receiver for Interpreter.push signal."""
143 if command and not more:
144 self.SetInsertionPointEnd()
145 start, end = self.GetSelection()
146 if start != end:
147 self.SetSelection(0, 0)
148 self.AppendText(command + '\n')
149
150
151 class DispatcherListing(wx.TextCtrl):
152 """Text control containing all dispatches for session."""
153
154 def __init__(self, parent=None, id=-1):
155 style = (wx.TE_MULTILINE | wx.TE_READONLY |
156 wx.TE_RICH2 | wx.TE_DONTWRAP)
157 wx.TextCtrl.__init__(self, parent, id, style=style)
158 dispatcher.connect(receiver=self.spy)
159
160 def spy(self, signal, sender):
161 """Receiver for Any signal from Any sender."""
162 text = '%r from %s' % (signal, sender)
163 self.SetInsertionPointEnd()
164 start, end = self.GetSelection()
165 if start != end:
166 self.SetSelection(0, 0)
167 self.AppendText(text + '\n')
168
169
170 class CrustFrame(frame.Frame):
171 """Frame containing all the PyCrust components."""
172
173 name = 'CrustFrame'
174 revision = __revision__
175
176 def __init__(self, parent=None, id=-1, title='PyCrust',
177 pos=wx.DefaultPosition, size=wx.DefaultSize,
178 style=wx.DEFAULT_FRAME_STYLE,
179 rootObject=None, rootLabel=None, rootIsNamespace=True,
180 locals=None, InterpClass=None, *args, **kwds):
181 """Create CrustFrame instance."""
182 frame.Frame.__init__(self, parent, id, title, pos, size, style)
183 intro = 'PyCrust %s - The Flakiest Python Shell' % VERSION
184 intro += '\nSponsored by Orbtech - '
185 intro += 'Your source for Python programming expertise.'
186 self.SetStatusText(intro.replace('\n', ', '))
187 self.crust = Crust(parent=self, intro=intro,
188 rootObject=rootObject,
189 rootLabel=rootLabel,
190 rootIsNamespace=rootIsNamespace,
191 locals=locals,
192 InterpClass=InterpClass, *args, **kwds)
193 self.shell = self.crust.shell
194 # Override the filling so that status messages go to the status bar.
195 self.crust.filling.tree.setStatusText = self.SetStatusText
196 # Override the shell so that status messages go to the status bar.
197 self.shell.setStatusText = self.SetStatusText
198 # Fix a problem with the sash shrinking to nothing.
199 self.crust.filling.SetSashPosition(200)
200 # Set focus to the shell editor.
201 self.shell.SetFocus()
202
203 def OnClose(self, event):
204 """Event handler for closing."""
205 self.crust.shell.destroy()
206 self.Destroy()
207
208 def OnAbout(self, event):
209 """Display an About window."""
210 title = 'About PyCrust'
211 text = 'PyCrust %s\n\n' % VERSION + \
212 'Yet another Python shell, only flakier.\n\n' + \
213 'Half-baked by Patrick K. O\'Brien,\n' + \
214 'the other half is still in the oven.\n\n' + \
215 'Shell Revision: %s\n' % self.shell.revision + \
216 'Interpreter Revision: %s\n\n' % self.shell.interp.revision + \
217 'Python Version: %s\n' % sys.version.split()[0] + \
218 'wxPython Version: %s\n' % wx.VERSION_STRING + \
219 'Platform: %s\n' % sys.platform
220 dialog = wx.MessageDialog(self, text, title,
221 wx.OK | wx.ICON_INFORMATION)
222 dialog.ShowModal()
223 dialog.Destroy()