]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wxPython/lib/PyCrust/filling.py
0b19af2f9f27dd1758ead35019e89106f83a8cc1
1 """PyCrust Filling is the gui tree control through which a user can navigate
2 the local namespace or any object."""
4 __author__
= "Patrick K. O'Brien <pobrien@orbtech.com>"
6 __date__
= "August 21, 2001"
7 __version__
= "$Revision$"[11:-2]
9 from wxPython
.wx
import *
10 from wxPython
.stc
import *
11 from version
import VERSION
19 class FillingTree(wxTreeCtrl
):
20 """PyCrust FillingTree based on wxTreeCtrl."""
22 name
= 'PyCrust Filling Tree'
23 revision
= __version__
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
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
)
43 def hasChildren(self
, object):
44 """Return true if object has children."""
45 if self
.getChildren(object):
50 def getChildren(self
, object):
51 """Return a dictionary with the attributes or contents of object."""
53 objtype
= type(object)
54 if objtype
is types
.DictType
:
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.
63 dict[key
] = getattr(object, key
)
68 def OnItemExpanding(self
, event
):
69 selection
= event
.GetItem()
70 if self
.IsExpanded(selection
):
72 object = self
.GetPyData(selection
)
73 children
= self
.getChildren(object)
76 list = children
.keys()
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
)):
87 child
= self
.AppendItem(selection
, itemtext
, -1, -1, \
88 wxTreeItemData(children
[item
]))
89 self
.SetItemHasChildren(child
, self
.hasChildren(children
[item
]))
91 def OnItemCollapsed(self
, event
):
92 """Remove all children from the item."""
93 item
= event
.GetItem()
94 self
.DeleteChildren(item
)
96 def OnSelChanged(self
, event
):
97 item
= event
.GetItem()
101 object = self
.GetPyData(item
)
103 text
+= self
.getFullName(item
)
104 text
+= '\n\nType: ' + str(type(object))[7:-2]
106 if type(object) is types
.StringType
:
108 text
+= '\n\nValue: ' + value
109 if type(object) is types
.InstanceType
:
111 text
+= '\n\nClass Definition:\n\n' + \
112 inspect
.getsource(object.__class
__)
115 text
+= '\n\n"""' + inspect
.getdoc(object).strip() + '"""'
120 text
+= '\n\nSource Code:\n\n' + \
121 inspect
.getsource(object)
124 text
+= '\n\n"""' + inspect
.getdoc(object).strip() + '"""'
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.
142 if partial
[0] == '[':
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
)
153 def setText(self
, text
):
154 """Display information about the current selection."""
156 # This method will most likely be replaced by the enclosing app
157 # to do something more interesting, like write to a text control.
160 def setStatusText(self
, text
):
161 """Display status information."""
163 # This method will most likely be replaced by the enclosing app
164 # to do something more interesting, like write to a status bar.
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',
176 'backcol': '#FFFFFF',
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):
185 faces
= { 'times' : 'Times',
187 'helv' : 'Helvetica',
188 'other' : 'new century schoolbook',
191 'backcol': '#FFFFFF',
195 class FillingText(wxStyledTextCtrl
):
196 """PyCrust FillingText based on wxStyledTextCtrl."""
198 name
= 'PyCrust Filling Text'
199 revision
= __version__
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.
209 """Configure shell based on user preferences."""
210 self
.SetMarginWidth(1, 0)
212 self
.SetLexer(wxSTC_LEX_PYTHON
)
213 self
.SetKeyWords(0, ' '.join(keyword
.kwlist
))
215 self
.setStyles(faces
)
216 self
.SetViewWhiteSpace(0)
220 def setStyles(self
, faces
):
221 """Configure font size, typeface and color for lexer."""
224 self
.StyleSetSpec(wxSTC_STYLE_DEFAULT
, "face:%(mono)s,size:%(size)d" % faces
)
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")
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
)
251 class Filling(wxSplitterWindow
):
252 """PyCrust Filling based on wxSplitterWindow."""
254 name
= 'PyCrust Filling'
255 revision
= __version__
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
)
274 class FillingFrame(wxFrame
):
275 """Frame containing the PyCrust filling, or namespace tree component."""
277 name
= 'PyCrust Filling Frame'
278 revision
= __version__
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
)
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
300 """PyFilling standalone application."""
303 self
.fillingFrame
= FillingFrame()
304 self
.fillingFrame
.Show(true
)
305 self
.SetTopWindow(self
.fillingFrame
)