]> git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/filling.py
0b19af2f9f27dd1758ead35019e89106f83a8cc1
[wxWidgets.git] / wxPython / wxPython / lib / PyCrust / filling.py
1 """PyCrust Filling is the gui tree control through which a user can navigate
2 the local namespace or any object."""
3
4 __author__ = "Patrick K. O'Brien <pobrien@orbtech.com>"
5 __cvsid__ = "$Id$"
6 __date__ = "August 21, 2001"
7 __version__ = "$Revision$"[11:-2]
8
9 from wxPython.wx import *
10 from wxPython.stc import *
11 from version import VERSION
12 import inspect
13 import introspect
14 import keyword
15 import sys
16 import types
17
18
19 class FillingTree(wxTreeCtrl):
20 """PyCrust FillingTree based on wxTreeCtrl."""
21
22 name = 'PyCrust Filling Tree'
23 revision = __version__
24
25 def __init__(self, parent, id=-1, pos=wxDefaultPosition, \
26 size=wxDefaultSize, style=wxTR_HAS_BUTTONS, \
27 rootObject=None, rootLabel=None, rootIsNamespace=0):
28 """Create a PyCrust FillingTree instance."""
29 wxTreeCtrl.__init__(self, parent, id, pos, size)
30 self.rootIsNamespace = rootIsNamespace
31 if not rootObject:
32 import __main__
33 rootObject = __main__
34 self.rootIsNamespace = 1
35 if not rootLabel: rootLabel = 'Ingredients'
36 rootData = wxTreeItemData(rootObject)
37 self.root = self.AddRoot(rootLabel, -1, -1, rootData)
38 self.SetItemHasChildren(self.root, self.hasChildren(self.root))
39 EVT_TREE_ITEM_EXPANDING(self, self.GetId(), self.OnItemExpanding)
40 EVT_TREE_ITEM_COLLAPSED(self, self.GetId(), self.OnItemCollapsed)
41 EVT_TREE_SEL_CHANGED(self, self.GetId(), self.OnSelChanged)
42
43 def hasChildren(self, object):
44 """Return true if object has children."""
45 if self.getChildren(object):
46 return true
47 else:
48 return false
49
50 def getChildren(self, object):
51 """Return a dictionary with the attributes or contents of object."""
52 dict = {}
53 objtype = type(object)
54 if objtype is types.DictType:
55 dict = object
56 elif (objtype in (types.InstanceType, types.ModuleType)) \
57 or hasattr(object, '__class__'):
58 for key in introspect.getAttributeNames(object):
59 # Believe it or not, some attributes can disappear, such as
60 # the exc_traceback attribute of the sys module. So this is
61 # nested in a try block.
62 try:
63 dict[key] = getattr(object, key)
64 except:
65 pass
66 return dict
67
68 def OnItemExpanding(self, event):
69 selection = event.GetItem()
70 if self.IsExpanded(selection):
71 return
72 object = self.GetPyData(selection)
73 children = self.getChildren(object)
74 if not children:
75 return
76 list = children.keys()
77 list.sort()
78 for item in list:
79 itemtext = str(item)
80 # Show string dictionary items with single quotes, except for
81 # the first level of items, if they represent a namespace.
82 if type(object) is types.DictType \
83 and type(item) is types.StringType \
84 and (selection != self.root \
85 or (selection == self.root and not self.rootIsNamespace)):
86 itemtext = repr(item)
87 child = self.AppendItem(selection, itemtext, -1, -1, \
88 wxTreeItemData(children[item]))
89 self.SetItemHasChildren(child, self.hasChildren(children[item]))
90
91 def OnItemCollapsed(self, event):
92 """Remove all children from the item."""
93 item = event.GetItem()
94 self.DeleteChildren(item)
95
96 def OnSelChanged(self, event):
97 item = event.GetItem()
98 if item == self.root:
99 self.setText('')
100 return
101 object = self.GetPyData(item)
102 text = ''
103 text += self.getFullName(item)
104 text += '\n\nType: ' + str(type(object))[7:-2]
105 value = str(object)
106 if type(object) is types.StringType:
107 value = repr(value)
108 text += '\n\nValue: ' + value
109 if type(object) is types.InstanceType:
110 try:
111 text += '\n\nClass Definition:\n\n' + \
112 inspect.getsource(object.__class__)
113 except:
114 try:
115 text += '\n\n"""' + inspect.getdoc(object).strip() + '"""'
116 except:
117 pass
118 else:
119 try:
120 text += '\n\nSource Code:\n\n' + \
121 inspect.getsource(object)
122 except:
123 try:
124 text += '\n\n"""' + inspect.getdoc(object).strip() + '"""'
125 except:
126 pass
127 self.setText(text)
128
129 def getFullName(self, item, partial=''):
130 """Return a syntactically proper name for item."""
131 parent = self.GetItemParent(item)
132 parentobject = self.GetPyData(parent)
133 name = self.GetItemText(item)
134 # Apply dictionary syntax to dictionary items, except the root
135 # and first level children of a namepace.
136 if type(parentobject) is types.DictType \
137 and ((item != self.root and parent != self.root) \
138 or (parent == self.root and not self.rootIsNamespace)):
139 name = '[' + name + ']'
140 # Apply dot syntax to multipart names.
141 if partial:
142 if partial[0] == '[':
143 name += partial
144 else:
145 name += '.' + partial
146 # Repeat for everything but the root item
147 # and first level children of a namespace.
148 if (item != self.root and parent != self.root) \
149 or (parent == self.root and not self.rootIsNamespace):
150 name = self.getFullName(parent, partial=name)
151 return name
152
153 def setText(self, text):
154 """Display information about the current selection."""
155
156 # This method will most likely be replaced by the enclosing app
157 # to do something more interesting, like write to a text control.
158 print text
159
160 def setStatusText(self, text):
161 """Display status information."""
162
163 # This method will most likely be replaced by the enclosing app
164 # to do something more interesting, like write to a status bar.
165 print text
166
167
168 if wxPlatform == '__WXMSW__':
169 faces = { 'times' : 'Times New Roman',
170 'mono' : 'Courier New',
171 'helv' : 'Lucida Console',
172 'lucida' : 'Lucida Console',
173 'other' : 'Comic Sans MS',
174 'size' : 10,
175 'lnsize' : 9,
176 'backcol': '#FFFFFF',
177 }
178 # Versions of wxPython prior to 2.3.2 had a sizing bug on Win platform.
179 # The font was 2 points too large. So we need to reduce the font size.
180 if ((wxMAJOR_VERSION, wxMINOR_VERSION) == (2, 3) and wxRELEASE_NUMBER < 2) \
181 or (wxMAJOR_VERSION <= 2 and wxMINOR_VERSION <= 2):
182 faces['size'] -= 2
183 faces['lnsize'] -= 2
184 else: # GTK
185 faces = { 'times' : 'Times',
186 'mono' : 'Courier',
187 'helv' : 'Helvetica',
188 'other' : 'new century schoolbook',
189 'size' : 12,
190 'lnsize' : 10,
191 'backcol': '#FFFFFF',
192 }
193
194
195 class FillingText(wxStyledTextCtrl):
196 """PyCrust FillingText based on wxStyledTextCtrl."""
197
198 name = 'PyCrust Filling Text'
199 revision = __version__
200
201 def __init__(self, parent, id=-1, pos=wxDefaultPosition, \
202 size=wxDefaultSize, style=wxCLIP_CHILDREN):
203 """Create a PyCrust FillingText instance."""
204 wxStyledTextCtrl.__init__(self, parent, id, pos, size, style)
205 # Configure various defaults and user preferences.
206 self.config()
207
208 def config(self):
209 """Configure shell based on user preferences."""
210 self.SetMarginWidth(1, 0)
211
212 self.SetLexer(wxSTC_LEX_PYTHON)
213 self.SetKeyWords(0, ' '.join(keyword.kwlist))
214
215 self.setStyles(faces)
216 self.SetViewWhiteSpace(0)
217 self.SetTabWidth(4)
218 self.SetUseTabs(0)
219
220 def setStyles(self, faces):
221 """Configure font size, typeface and color for lexer."""
222
223 # Default style
224 self.StyleSetSpec(wxSTC_STYLE_DEFAULT, "face:%(mono)s,size:%(size)d" % faces)
225
226 self.StyleClearAll()
227
228 # Built in styles
229 self.StyleSetSpec(wxSTC_STYLE_LINENUMBER, "back:#C0C0C0,face:%(mono)s,size:%(lnsize)d" % faces)
230 self.StyleSetSpec(wxSTC_STYLE_CONTROLCHAR, "face:%(mono)s" % faces)
231 self.StyleSetSpec(wxSTC_STYLE_BRACELIGHT, "fore:#0000FF,back:#FFFF88")
232 self.StyleSetSpec(wxSTC_STYLE_BRACEBAD, "fore:#FF0000,back:#FFFF88")
233
234 # Python styles
235 self.StyleSetSpec(wxSTC_P_DEFAULT, "face:%(mono)s" % faces)
236 self.StyleSetSpec(wxSTC_P_COMMENTLINE, "fore:#007F00,face:%(mono)s" % faces)
237 self.StyleSetSpec(wxSTC_P_NUMBER, "")
238 self.StyleSetSpec(wxSTC_P_STRING, "fore:#7F007F,face:%(mono)s" % faces)
239 self.StyleSetSpec(wxSTC_P_CHARACTER, "fore:#7F007F,face:%(mono)s" % faces)
240 self.StyleSetSpec(wxSTC_P_WORD, "fore:#00007F,bold")
241 self.StyleSetSpec(wxSTC_P_TRIPLE, "fore:#7F0000")
242 self.StyleSetSpec(wxSTC_P_TRIPLEDOUBLE, "fore:#000033,back:#FFFFE8")
243 self.StyleSetSpec(wxSTC_P_CLASSNAME, "fore:#0000FF,bold")
244 self.StyleSetSpec(wxSTC_P_DEFNAME, "fore:#007F7F,bold")
245 self.StyleSetSpec(wxSTC_P_OPERATOR, "")
246 self.StyleSetSpec(wxSTC_P_IDENTIFIER, "")
247 self.StyleSetSpec(wxSTC_P_COMMENTBLOCK, "fore:#7F7F7F")
248 self.StyleSetSpec(wxSTC_P_STRINGEOL, "fore:#000000,face:%(mono)s,back:#E0C0E0,eolfilled" % faces)
249
250
251 class Filling(wxSplitterWindow):
252 """PyCrust Filling based on wxSplitterWindow."""
253
254 name = 'PyCrust Filling'
255 revision = __version__
256
257 def __init__(self, parent, id=-1, pos=wxDefaultPosition, \
258 size=wxDefaultSize, style=wxSP_3D, name='Filling Window', \
259 rootObject=None, rootLabel=None, rootIsNamespace=0):
260 """Create a PyCrust Filling instance."""
261 wxSplitterWindow.__init__(self, parent, id, pos, size, style, name)
262 self.fillingTree = FillingTree(parent=self, rootObject=rootObject, \
263 rootLabel=rootLabel, \
264 rootIsNamespace=rootIsNamespace)
265 self.fillingText = FillingText(parent=self)
266 self.SplitVertically(self.fillingTree, self.fillingText, 200)
267 self.SetMinimumPaneSize(1)
268 # Override the filling so that descriptions go to fillingText.
269 self.fillingTree.setText = self.fillingText.SetText
270 # Select the root item.
271 self.fillingTree.SelectItem(self.fillingTree.root)
272
273
274 class FillingFrame(wxFrame):
275 """Frame containing the PyCrust filling, or namespace tree component."""
276
277 name = 'PyCrust Filling Frame'
278 revision = __version__
279
280 def __init__(self, parent=None, id=-1, title='PyFilling', \
281 pos=wxDefaultPosition, size=wxDefaultSize, \
282 style=wxDEFAULT_FRAME_STYLE, rootObject=None, \
283 rootLabel=None, rootIsNamespace=0):
284 """Create a PyCrust FillingFrame instance."""
285 wxFrame.__init__(self, parent, id, title, pos, size, style)
286 intro = 'Welcome To PyFilling - The Tastiest Namespace Inspector'
287 self.CreateStatusBar()
288 self.SetStatusText(intro)
289 if wxPlatform == '__WXMSW__':
290 icon = wxIcon('PyCrust.ico', wxBITMAP_TYPE_ICO)
291 self.SetIcon(icon)
292 self.filling = Filling(parent=self, rootObject=rootObject, \
293 rootLabel=rootLabel, \
294 rootIsNamespace=rootIsNamespace)
295 # Override the filling so that status messages go to the status bar.
296 self.filling.fillingTree.setStatusText = self.SetStatusText
297
298
299 class App(wxApp):
300 """PyFilling standalone application."""
301
302 def OnInit(self):
303 self.fillingFrame = FillingFrame()
304 self.fillingFrame.Show(true)
305 self.SetTopWindow(self.fillingFrame)
306 return true
307
308
309